import Foundation public enum NotifyKind: String, Codable, Equatable { case permission case question case turnEnded case error case generic } public struct NotifyEvent: Codable, Equatable { public var kind: NotifyKind public var title: String public var message: String public var cwd: String? public var timestamp: Date /// App that owns the terminal running luxe, e.g. com.mitchellh.ghostty. Optional so events from /// older senders still decode, and so a hook without the variable still notifies. public var terminalBundleID: String? public init(kind: NotifyKind, title: String, message: String, cwd: String? = nil, timestamp: Date = Date(), terminalBundleID: String? = nil) { self.kind = kind self.title = title self.message = message self.cwd = cwd self.timestamp = timestamp self.terminalBundleID = terminalBundleID } } public enum HookParseError: Error, CustomStringConvertible, Equatable { case notJSON(String) case notAnObject public var description: String { switch self { case .notJSON(let reason): return "hook stdin is not valid JSON: \(reason)" case .notAnObject: return "hook stdin must be a JSON object" } } } public enum HookEventParser { /// `hookPoint` comes from the CLI's argument, because luxe's stdin payload does not name the point. public static func parse(_ raw: Data, hookPoint: String, cwd: String? = nil, now: Date = Date()) throws -> NotifyEvent { let object = try decodeObject(raw) let other = object["Other"] as? [String: Any] switch normalize(hookPoint) { case "permissionrequested": let tool = other?["tool"] as? String ?? "a tool" return NotifyEvent(kind: .permission, title: "luxe needs approval", message: "Approve or reject \(tool)", cwd: cwd, timestamp: now) case "agentend": return NotifyEvent(kind: .turnEnded, title: "luxe is waiting", message: "The turn finished; it needs your next message", cwd: cwd, timestamp: now) case "error": let detail = other?["message"] as? String ?? "a turn failed" return NotifyEvent(kind: .error, title: "luxe hit an error", message: detail, cwd: cwd, timestamp: now) case "beforetooluse": return toolUseEvent(object, hookPoint: hookPoint, cwd: cwd, now: now) default: return NotifyEvent(kind: .generic, title: "luxe", message: "Event: \(hookPoint)", cwd: cwd, timestamp: now) } } /// Only the `question` tool means luxe is waiting on the user. Any other tool stays generic, so a /// hook configured without a matcher cannot turn every tool call into a "question". private static func toolUseEvent(_ object: [String: Any], hookPoint: String, cwd: String?, now: Date) -> NotifyEvent { let toolUse = object["ToolUse"] as? [String: Any] guard toolUse?["tool_name"] as? String == "question" else { return NotifyEvent(kind: .generic, title: "luxe", message: "Event: \(hookPoint)", cwd: cwd, timestamp: now) } let args = toolUse?["args"] as? [String: Any] let prompt = (args?["prompt"] as? String) ?? "luxe is waiting for your answer" return NotifyEvent(kind: .question, title: "luxe has a question", message: String(prompt.prefix(200)), cwd: cwd, timestamp: now) } private static func decodeObject(_ raw: Data) throws -> [String: Any] { let value: Any do { value = try JSONSerialization.jsonObject(with: raw) } catch { throw HookParseError.notJSON(error.localizedDescription) } guard let object = value as? [String: Any] else { throw HookParseError.notAnObject } return object } private static func normalize(_ point: String) -> String { point.lowercased().replacingOccurrences(of: "_", with: "") } }