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 / AskRoute.swift
| 1 | import Foundation |
| 2 | |
| 3 | /// Which model Ask uses when nobody picked one: the person's own model first. |
| 4 | /// |
| 5 | /// An own model is an OpenAI-compatible model in the settings whose address is this Mac's own (127.0.0.1, |
| 6 | /// localhost) or on the person's network (a `.local` name), judged by the address alone, the same way the line |
| 7 | /// under an answer is (`ModelEndpoint.place`). When one answers its `/health`, Ask uses it. When none answers, |
| 8 | /// Ask uses what it used before (`defaultModel`, else the first model in the list) and says so in one line. |
| 9 | /// An explicit `defaultModel` always wins, and `preferOwnModel: false` switches this off. With no own model in |
| 10 | /// the settings nothing here runs, so a fresh install behaves as it did before, and no request is made. |
| 11 | enum AskRoute { |
| 12 | struct Choice: Equatable { |
| 13 | var name: String // the model Ask uses |
| 14 | var note: String? // one plain line when an own model did not answer; nil otherwise |
| 15 | } |
| 16 | |
| 17 | /// The configured own models, in the order of the settings. |
| 18 | static func ownModels(_ cfg: HopConfig) -> [ModelEndpoint] { |
| 19 | cfg.models.filter { $0.kind == "openai" && $0.place != .online } |
| 20 | } |
| 21 | |
| 22 | /// What Ask used before this preference existed. |
| 23 | static func previous(_ cfg: HopConfig) -> String { cfg.defaultModel ?? cfg.models.first?.name ?? "" } |
| 24 | |
| 25 | /// On unless switched off, and only when there is an own model to prefer. |
| 26 | static func preferOwn(_ cfg: HopConfig) -> Bool { (cfg.preferOwnModel ?? true) && !ownModels(cfg).isEmpty } |
| 27 | |
| 28 | /// Whether choosing can give anything other than `previous`: no explicit `defaultModel`, preference on. |
| 29 | static func applies(_ cfg: HopConfig) -> Bool { cfg.defaultModel == nil && preferOwn(cfg) } |
| 30 | |
| 31 | /// The choice. `answers` is asked about own models only, one at a time, until one answers. |
| 32 | static func choose(_ cfg: HopConfig, answers: (ModelEndpoint) async -> Bool) async -> Choice { |
| 33 | if let d = cfg.defaultModel { return Choice(name: d, note: nil) } // an explicit default always wins |
| 34 | guard preferOwn(cfg) else { return Choice(name: previous(cfg), note: nil) } |
| 35 | var silent: [ModelEndpoint] = [] |
| 36 | for ep in ownModels(cfg) { |
| 37 | if await answers(ep) { return Choice(name: ep.name, note: nil) } |
| 38 | silent.append(ep) |
| 39 | } |
| 40 | return Choice(name: previous(cfg), note: fallbackNote(silent, using: previous(cfg))) |
| 41 | } |
| 42 | |
| 43 | /// One plain line: which own model did not answer, where, and what Ask uses instead. |
| 44 | static func fallbackNote(_ silent: [ModelEndpoint], using: String) -> String { |
| 45 | let which = silent.map { "\($0.name) (\($0.baseURL))" }.joined(separator: ", ") |
| 46 | return "Your own model \(which) did not answer, so Ask uses \(using) as before." |
| 47 | } |
| 48 | |
| 49 | /// The existing `/health` request (`ModelStatus.probe`): a 200 answer, ready or busy, counts. |
| 50 | static func answers(_ ep: ModelEndpoint) async -> Bool { |
| 51 | let s = await ModelStatus.probe(ep) |
| 52 | return s == .free || s == .busy |
| 53 | } |
| 54 | |
| 55 | /// The words under an answer that say who answered: the model id the server (or the Claude CLI) reported, |
| 56 | /// or, when it reported none, the configured name, labelled as such. Nothing is guessed. |
| 57 | static func who(_ ep: ModelEndpoint, reported: String?) -> String { |
| 58 | if let r = reported?.trimmingCharacters(in: .whitespacesAndNewlines), !r.isEmpty { return "answered by \(r)" } |
| 59 | let teller = ep.kind == "claude-cli" ? "the Claude CLI" : "the server" |
| 60 | return "answered by \(ep.name) (the configured name; \(teller) reported no model id)" |
| 61 | } |
| 62 | |
| 63 | /// The model id named in one reply event, or nil. An OpenAI-compatible chunk carries `model`; the Claude CLI's |
| 64 | /// stream-json carries it in its first `system` event, in `message.model` of an assistant event, and in |
| 65 | /// `event.message.model` of a `message_start` stream event. An empty string counts as none. |
| 66 | static func reportedModel(_ j: [String: Any]) -> String? { |
| 67 | let found: [Any?] = [j["model"], (j["message"] as? [String: Any])?["model"], |
| 68 | ((j["event"] as? [String: Any])?["message"] as? [String: Any])?["model"]] |
| 69 | for case let v? in found { |
| 70 | guard let s = v as? String else { continue } |
| 71 | let t = s.trimmingCharacters(in: .whitespacesAndNewlines) |
| 72 | if !t.isEmpty { return t } |
| 73 | } |
| 74 | return nil |
| 75 | } |
| 76 | } |