hop
Hop: a small native macOS launcher. Clipboard history, find, on-device dictation and your coding agent behind one key.
Brought by drakestapleton
hop / Sources / CliCompatibility.swift
| 1 | import Foundation |
| 2 | |
| 3 | extension AppModel { |
| 4 | nonisolated static func claudeAskArgs(model: String, text: String, known: String, session: String?) -> [String] { |
| 5 | // Short answers cost fewer tokens and read faster. |
| 6 | var system = "Answer short. Plain words. No preamble." |
| 7 | if !known.isEmpty { system += "\n\n" + cortexNotesHeader + known } |
| 8 | var argv = ["claude", "-p", text, "--output-format", "stream-json", "--verbose", "--include-partial-messages", |
| 9 | "--tools", "", "--strict-mcp-config", "--model", model.isEmpty ? "sonnet" : model, |
| 10 | "--append-system-prompt", system] |
| 11 | if let session { argv += ["--resume", session] } |
| 12 | return argv |
| 13 | } |
| 14 | nonisolated static func claudeMeta(_ ep: ModelEndpoint, reported: String?, tokens: Int, seconds: Double?) -> String { |
| 15 | ([ModelEndpoint.Place.online.rawValue, "\(Savings.short(tokens)) tokens", seconds.map { String(format: "%.1f s", $0) }, |
| 16 | AskRoute.who(ep, reported: reported)] as [String?]).compactMap { $0 }.joined(separator: " ยท ") |
| 17 | } |
| 18 | } |
| 19 | |
| 20 | extension AgentEvents { |
| 21 | /// What the agent said in one line of its output, whole: every text block of a Claude CLI event joined, or |
| 22 | /// another agent's plain line. nil for a line that says nothing (a step, a result, an error note). This is the |
| 23 | /// answer Hop remembers for a run, whatever the screen is set to show. |
| 24 | static func said(_ line: String) -> String? { |
| 25 | guard line.hasPrefix("{"), let d = line.data(using: .utf8), |
| 26 | let j = try? JSONSerialization.jsonObject(with: d) as? [String: Any] else { |
| 27 | return line.isEmpty || line.hasPrefix("!") ? nil : line |
| 28 | } |
| 29 | guard j["type"] as? String == "assistant" else { return nil } |
| 30 | let words = (((j["message"] as? [String: Any])?["content"] as? [[String: Any]]) ?? []) |
| 31 | .compactMap { $0["type"] as? String == "text" ? $0["text"] as? String : nil }.filter { !$0.isEmpty } |
| 32 | return words.isEmpty ? nil : words.joined(separator: "\n") |
| 33 | } |
| 34 | |
| 35 | /// What is behind one tool step, from the tool's input: the shell command, the file, the search words or the |
| 36 | /// address, whichever the tool was given. A tool with none of these shows its name alone. |
| 37 | static func behind(_ input: [String: Any]) -> String? { |
| 38 | for key in ["command", "file_path", "path", "pattern", "query", "url", "description", "prompt"] { |
| 39 | if let v = input[key] as? String, !v.isEmpty { return v } |
| 40 | } |
| 41 | return nil |
| 42 | } |
| 43 | } |