OpenTalon / app · opentalon.ai / native / Sources / OpenTalon / AgentModelCatalog.swift
| 1 | import CompanionCore |
| 2 | import SwiftUI |
| 3 | |
| 4 | private struct ModelOverride: Codable { |
| 5 | let agent: PhoneAgent |
| 6 | let id: String |
| 7 | let title: String |
| 8 | let hidden: Bool |
| 9 | } |
| 10 | |
| 11 | private struct ModelCatalog: Decodable { |
| 12 | let models: [AgentModelChoice] |
| 13 | let defaults: [AgentModelChoice] |
| 14 | let overrides: [ModelOverride] |
| 15 | let catalogRevision: Int |
| 16 | } |
| 17 | |
| 18 | private struct EditableModel: Identifiable { |
| 19 | let id = UUID() |
| 20 | let agent: PhoneAgent |
| 21 | var modelID: String |
| 22 | var title: String |
| 23 | } |
| 24 | |
| 25 | struct AgentModelCatalogEditor: View { |
| 26 | @Environment(WorkspaceModel.self) private var model |
| 27 | @Environment(\.palette) private var palette |
| 28 | @Environment(\.dismiss) private var dismiss |
| 29 | @State private var catalog: ModelCatalog? |
| 30 | @State private var rows: [EditableModel] = [] |
| 31 | @State private var agent = PhoneAgent.codex |
| 32 | @State private var busy = false |
| 33 | @State private var error: String? |
| 34 | @State private var resetAgents: Set<PhoneAgent> = [] |
| 35 | |
| 36 | private var valid: Bool { |
| 37 | let keys = rows.map { $0.agent.rawValue + ":" + $0.modelID.trimmingCharacters(in: .whitespacesAndNewlines) } |
| 38 | return Set(keys).count == keys.count && rows.allSatisfy { |
| 39 | !$0.modelID.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty && |
| 40 | !$0.title.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | var body: some View { |
| 45 | VStack(alignment: .leading, spacing: 16) { |
| 46 | HStack { |
| 47 | Text("Agent models").font(palette.display(27)).accessibilityAddTraits(.isHeader) |
| 48 | Spacer() |
| 49 | Button("Cancel") { dismiss() }.frame(minWidth: 72).keyboardShortcut(.cancelAction) |
| 50 | } |
| 51 | Text("Your iPhone and iPad receive this list from your Mac. App updates add current models while keeping your custom names, models, and hidden choices.") |
| 52 | .font(.callout).foregroundStyle(palette.muted).fixedSize(horizontal: false, vertical: true) |
| 53 | BoardDropdown(selection: $agent, title: "Agent", symbol: "terminal", |
| 54 | choices: PhoneAgent.allCases, compact: true, |
| 55 | agentBrand: { Agent(rawValue: $0.rawValue) ?? .codex }, label: \.title) |
| 56 | ThemedScrollView { |
| 57 | VStack(spacing: 12) { |
| 58 | ForEach($rows) { $row in |
| 59 | if row.agent == agent { |
| 60 | VStack(alignment: .leading, spacing: 8) { |
| 61 | HStack { |
| 62 | TextField("Display name", text: $row.title) |
| 63 | .accessibilityLabel("Model display name") |
| 64 | Button { |
| 65 | rows.removeAll { $0.id == row.id } |
| 66 | } label: { Label("Hide model", systemImage: "minus.circle") } |
| 67 | .labelStyle(.iconOnly) |
| 68 | .help("Hide this model from your choices") |
| 69 | } |
| 70 | TextField("Model ID", text: $row.modelID) |
| 71 | .font(.system(.caption, design: .monospaced)) |
| 72 | .accessibilityLabel("Model ID") |
| 73 | } |
| 74 | .textFieldStyle(.plain).padding(12).surface(10) |
| 75 | } |
| 76 | } |
| 77 | }.padding(.trailing, 5) |
| 78 | }.frame(height: min(360, max(180, CGFloat(rows.filter { $0.agent == agent }.count) * 90))) |
| 79 | HStack { |
| 80 | Button("Add model", systemImage: "plus") { |
| 81 | rows.append(EditableModel(agent: agent, modelID: "", title: "")) |
| 82 | } |
| 83 | Button("Use current defaults") { |
| 84 | resetAgents.insert(agent) |
| 85 | rows.removeAll { $0.agent == agent } |
| 86 | rows.append(contentsOf: (catalog?.defaults ?? []).filter { $0.agent == agent }.map { |
| 87 | EditableModel(agent: $0.agent, modelID: $0.id, title: $0.title) |
| 88 | }) |
| 89 | } |
| 90 | .disabled(catalog == nil) |
| 91 | } |
| 92 | Text(agent == .gemini |
| 93 | ? "Agent default sends the first Gemini model listed here. A custom model must be supported by Gemini and your Google plan." |
| 94 | : "Agent default is always available. A custom model must be supported by its agent and your account.") |
| 95 | .font(.caption).foregroundStyle(palette.muted).fixedSize(horizontal: false, vertical: true) |
| 96 | if let error { |
| 97 | Text(error).font(.caption).foregroundStyle(palette.danger) |
| 98 | Button("Reload and discard edits") { Task { await load() } } |
| 99 | } |
| 100 | HStack { |
| 101 | Spacer() |
| 102 | Button("Save", action: save).glassPrimary().frame(minWidth: 72) |
| 103 | .disabled(catalog == nil || !valid) |
| 104 | } |
| 105 | } |
| 106 | .padding(24).frame(width: 630) |
| 107 | .foregroundStyle(palette.text).tint(palette.accent).background(palette.panel) |
| 108 | .buttonStyle(GlassActionStyle()).disabled(busy) |
| 109 | .interactiveDismissDisabled(busy) |
| 110 | .task { await load() } |
| 111 | } |
| 112 | |
| 113 | private func load() async { |
| 114 | busy = true |
| 115 | defer { busy = false } |
| 116 | do { |
| 117 | let value: ModelCatalog = try await model.service.call("get_agent_models") |
| 118 | catalog = value |
| 119 | rows = value.models.map { EditableModel(agent: $0.agent, modelID: $0.id, title: $0.title) } |
| 120 | error = nil |
| 121 | resetAgents = [] |
| 122 | } catch { self.error = error.localizedDescription } |
| 123 | } |
| 124 | |
| 125 | private func save() { |
| 126 | guard let catalog else { return } |
| 127 | busy = true |
| 128 | Task { |
| 129 | defer { busy = false } |
| 130 | do { |
| 131 | let visible = rows.map { (agent: $0.agent, id: $0.modelID.trimmingCharacters(in: .whitespacesAndNewlines), |
| 132 | title: $0.title.trimmingCharacters(in: .whitespacesAndNewlines)) } |
| 133 | var overrides: [[String: Any]] = catalog.defaults.compactMap { original in |
| 134 | guard !visible.contains(where: { $0.agent == original.agent && $0.id == original.id }) else { return nil } |
| 135 | return ["agent": original.agent.rawValue, "id": original.id, "title": original.title, "hidden": true] |
| 136 | } |
| 137 | // Keep hidden older choices even when a later app no longer ships them. |
| 138 | overrides += catalog.overrides.filter { old in |
| 139 | old.hidden && !resetAgents.contains(old.agent) && !catalog.defaults.contains(where: { $0.agent == old.agent && $0.id == old.id }) && |
| 140 | !visible.contains(where: { $0.agent == old.agent && $0.id == old.id }) |
| 141 | }.map { ["agent": $0.agent.rawValue, "id": $0.id, "title": $0.title, "hidden": true] } |
| 142 | overrides += visible.compactMap { entry in |
| 143 | let explicitlyKept = !resetAgents.contains(entry.agent) && catalog.overrides.contains { |
| 144 | !$0.hidden && $0.agent == entry.agent && $0.id == entry.id && $0.title == entry.title |
| 145 | } |
| 146 | if !explicitlyKept && catalog.defaults.contains(where: { $0.agent == entry.agent && $0.id == entry.id && $0.title == entry.title }) { return nil } |
| 147 | return ["agent": entry.agent.rawValue, "id": entry.id, "title": entry.title, "hidden": false] |
| 148 | } |
| 149 | let _: ModelCatalog = try await model.write("set_agent_models", [ |
| 150 | "expectedCatalogRevision": catalog.catalogRevision, "overrides": overrides |
| 151 | ]) |
| 152 | dismiss() |
| 153 | } catch { self.error = error.localizedDescription } |
| 154 | } |
| 155 | } |
| 156 | } |