Glimmer / app · glimmertoday.ai / native / Glimmer / CoachAgentSettings.swift
| 1 | import FoundationModels |
| 2 | import SwiftUI |
| 3 | |
| 4 | #if os(macOS) |
| 5 | /// Settings → Coach: who answers in Coach and the Toolkit, with model, effort and time limit, |
| 6 | /// following Inkspring's writing agent and OpenTalon's task agent settings. |
| 7 | struct CoachAgentSettings: View { |
| 8 | @AppStorage(CoachAgent.preference) private var savedAgent = "" |
| 9 | @AppStorage(CoachAgentChoice.timeoutKey) private var savedTimeout = 90 |
| 10 | @State private var revision = 0 |
| 11 | @State private var customModel = "" |
| 12 | @State private var testing: Task<Void, Never>? |
| 13 | @State private var testResult: String? |
| 14 | @State private var testFailed = false |
| 15 | |
| 16 | private var choice: CoachAgentChoice { |
| 17 | _ = (savedAgent, savedTimeout, revision) |
| 18 | return CoachAgentChoice.current |
| 19 | } |
| 20 | |
| 21 | var body: some View { |
| 22 | let choice = choice |
| 23 | VStack(alignment: .leading, spacing: 14) { |
| 24 | Text("Choose who answers in Coach and the Toolkit. Local agents use the account you signed in to on this Mac.") |
| 25 | .font(.callout).foregroundStyle(.secondary) |
| 26 | .fixedSize(horizontal: false, vertical: true) |
| 27 | VStack(spacing: 6) { |
| 28 | ForEach(CoachAgent.allCases) { agent in |
| 29 | let selected = agent == choice.agent |
| 30 | Button { select(agent) } label: { |
| 31 | RowLabel(title: agent.title, detail: detail(agent), icon: agent.symbol, tint: GlimmerColor.ember, |
| 32 | trailing: selected ? "checkmark" : nil, selected: selected) |
| 33 | } |
| 34 | .glassRow(tint: GlimmerColor.ember, selected: selected) |
| 35 | .accessibilityIdentifier("coach.agent.\(agent.rawValue)") |
| 36 | } |
| 37 | } |
| 38 | .glassGroup(spacing: 6) |
| 39 | |
| 40 | if choice.agent.isLocalAgent { |
| 41 | agentOptions(choice) |
| 42 | } else if choice.agent == .server { |
| 43 | CoachServerFields() |
| 44 | } else { |
| 45 | Text(CoachDeviceRoute.summary) |
| 46 | .font(.callout).foregroundStyle(.secondary) |
| 47 | .fixedSize(horizontal: false, vertical: true) |
| 48 | } |
| 49 | |
| 50 | if choice.agent != .device { |
| 51 | Divider() |
| 52 | HStack(spacing: 10) { |
| 53 | Button(testing == nil ? "Test Coach" : "Testing…", systemImage: "checkmark.circle") { test(choice) } |
| 54 | .glimmerButton() |
| 55 | .disabled(testing != nil || !choice.isReady) |
| 56 | .accessibilityIdentifier("coach.test") |
| 57 | if testing != nil { ProgressView().controlSize(.small).accessibilityLabel("Testing Coach") } |
| 58 | } |
| 59 | if let testResult { |
| 60 | Label(testResult, systemImage: testFailed ? "exclamationmark.triangle.fill" : "checkmark.circle.fill") |
| 61 | .font(.callout) |
| 62 | .foregroundStyle(testFailed ? GlimmerColor.ember : GlimmerColor.sage) |
| 63 | .fixedSize(horizontal: false, vertical: true) |
| 64 | .textSelection(.enabled) |
| 65 | .accessibilityIdentifier("coach.testResult") |
| 66 | } |
| 67 | } |
| 68 | } |
| 69 | .onChange(of: choice.agent) { _, _ in |
| 70 | testResult = nil |
| 71 | customModel = "" |
| 72 | } |
| 73 | .onDisappear { testing?.cancel() } |
| 74 | #if DEBUG |
| 75 | .task { |
| 76 | let environment = ProcessInfo.processInfo.environment |
| 77 | if environment["GLIMMER_TEST_JOURNAL"] != nil, environment["GLIMMER_COACH_TEST"] == "1" { test(choice) } |
| 78 | } |
| 79 | #endif |
| 80 | } |
| 81 | |
| 82 | @ViewBuilder private func agentOptions(_ choice: CoachAgentChoice) -> some View { |
| 83 | let agent = choice.agent |
| 84 | if agent.executable == nil, let guide = agent.guide { |
| 85 | Link(destination: guide) { |
| 86 | RowLabel(title: "Install \(agent.title)", detail: "Then sign in once in Terminal", icon: "arrow.down.circle.fill", |
| 87 | tint: GlimmerColor.ember, trailing: "arrow.up.right") |
| 88 | } |
| 89 | .glassRow(tint: GlimmerColor.ember) |
| 90 | } |
| 91 | VStack(alignment: .leading, spacing: 8) { |
| 92 | Eyebrow("Model") |
| 93 | FlowLayout(spacing: 8) { |
| 94 | let options = (agent == .gemini ? [] : [""]) + agent.models.map(\.id) |
| 95 | let current = choice.model.isEmpty ? (agent.defaultModel ?? "") : choice.model |
| 96 | ForEach(options, id: \.self) { id in |
| 97 | Button { |
| 98 | save(id, key: CoachAgentChoice.modelKey(agent)) |
| 99 | } label: { |
| 100 | HStack(spacing: 6) { |
| 101 | Text(agent.modelName(id)) |
| 102 | if current == id { Image(systemName: "checkmark").accessibilityHidden(true) } |
| 103 | } |
| 104 | } |
| 105 | .glimmerButton(current == id ? .selected : .standard) |
| 106 | .accessibilityAddTraits(current == id ? .isSelected : []) |
| 107 | } |
| 108 | if !options.contains(current) { |
| 109 | Button { } label: { |
| 110 | HStack(spacing: 6) { Text(current); Image(systemName: "checkmark").accessibilityHidden(true) } |
| 111 | } |
| 112 | .glimmerButton(.selected) |
| 113 | .accessibilityAddTraits(.isSelected) |
| 114 | } |
| 115 | } |
| 116 | .accessibilityIdentifier("coach.model") |
| 117 | if agent != .gemini { |
| 118 | HStack(spacing: 8) { |
| 119 | TextField("Custom model ID", text: $customModel).glimmerField() |
| 120 | .onSubmit { useCustomModel(agent) } |
| 121 | Button("Use model") { useCustomModel(agent) } |
| 122 | .glimmerButton().besideField() |
| 123 | .disabled(customModel.trimmingCharacters(in: .whitespaces).isEmpty) |
| 124 | } |
| 125 | } |
| 126 | } |
| 127 | VStack(alignment: .leading, spacing: 8) { |
| 128 | Eyebrow("Reasoning effort") |
| 129 | let efforts = (agent == .gemini ? [] : [""]) + agent.efforts(model: choice.model.isEmpty ? agent.defaultModel : choice.model) |
| 130 | let effort = efforts.contains(choice.effort) ? choice.effort : (agent == .gemini ? "high" : "") |
| 131 | GlassSegments(label: "Reasoning effort", |
| 132 | options: efforts.map { GlassSegment(value: $0, title: CoachAgent.effortName($0)) }, |
| 133 | selection: Binding(get: { effort }, set: { save($0, key: CoachAgentChoice.effortKey(agent)) })) |
| 134 | .accessibilityIdentifier("coach.effort") |
| 135 | } |
| 136 | VStack(alignment: .leading, spacing: 8) { |
| 137 | Eyebrow("Time limit") |
| 138 | GlassSegments(label: "Time limit", |
| 139 | options: CoachAgentChoice.timeouts.map { GlassSegment(value: $0, title: $0 < 120 ? "\($0) s" : "2 min") }, |
| 140 | selection: Binding(get: { choice.timeout }, set: { savedTimeout = $0 })) |
| 141 | .frame(maxWidth: 360) |
| 142 | } |
| 143 | Text("Coach sends \(agent.title) your question, your recent Coach messages and the practices it can suggest. Your journal stays on this device. \(agent == .gemini ? "Gemini runs through Google’s Antigravity CLI (agy) with your Google sign-in." : "Agent default uses the model set in \(agent.title).")") |
| 144 | .font(.caption).foregroundStyle(.secondary) |
| 145 | .fixedSize(horizontal: false, vertical: true) |
| 146 | } |
| 147 | |
| 148 | private func detail(_ agent: CoachAgent) -> String { |
| 149 | switch agent { |
| 150 | case .device: CoachDeviceRoute.title |
| 151 | case .server: |
| 152 | CoachMCP.url(UserDefaults.standard.string(forKey: CoachMCP.preference) ?? "") == nil ? "Not connected" : "Connected" |
| 153 | default: |
| 154 | agent.executable.map(Self.location) ?? "Not installed on this Mac" |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | /// "~/.local/bin/claude", or "Installed with ChatGPT" for a tool inside an app. |
| 159 | static func location(_ url: URL) -> String { |
| 160 | if let app = url.pathComponents.first(where: { $0.hasSuffix(".app") }) { |
| 161 | return "Installed with " + app.dropLast(4) |
| 162 | } |
| 163 | return url.path.replacingOccurrences(of: CoachAgentRunner.userHome, with: "~") |
| 164 | } |
| 165 | |
| 166 | private func select(_ agent: CoachAgent) { |
| 167 | savedAgent = agent.rawValue |
| 168 | revision += 1 |
| 169 | } |
| 170 | |
| 171 | private func save(_ value: String, key: String) { |
| 172 | UserDefaults.standard.set(value, forKey: key) |
| 173 | revision += 1 |
| 174 | testResult = nil |
| 175 | } |
| 176 | |
| 177 | private func useCustomModel(_ agent: CoachAgent) { |
| 178 | let id = customModel.trimmingCharacters(in: .whitespacesAndNewlines) |
| 179 | guard !id.isEmpty else { return } |
| 180 | save(String(id.prefix(120)), key: CoachAgentChoice.modelKey(agent)) |
| 181 | customModel = "" |
| 182 | } |
| 183 | |
| 184 | private func test(_ choice: CoachAgentChoice) { |
| 185 | testResult = nil |
| 186 | testing = Task { |
| 187 | defer { testing = nil } |
| 188 | do { |
| 189 | testResult = try await CoachAgentRoute.test(choice) |
| 190 | testFailed = false |
| 191 | } catch is CancellationError { |
| 192 | } catch { |
| 193 | testResult = error.localizedDescription |
| 194 | testFailed = true |
| 195 | } |
| 196 | revision += 1 |
| 197 | } |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | /// A sheet version of the Coach settings, opened from the Coach composer. |
| 202 | struct CoachAgentSheet: View { |
| 203 | @Environment(\.dismiss) private var dismiss |
| 204 | |
| 205 | var body: some View { |
| 206 | ScrollView { |
| 207 | VStack(alignment: .leading, spacing: 16) { |
| 208 | Text("Coach replies").font(.title2.weight(.semibold)).accessibilityAddTraits(.isHeader) |
| 209 | CoachAgentSettings() |
| 210 | HStack { |
| 211 | Spacer() |
| 212 | Button("Done") { dismiss() }.glimmerButton(.prominent).keyboardShortcut(.defaultAction) |
| 213 | } |
| 214 | } |
| 215 | .padding(24) |
| 216 | } |
| 217 | .background { PageBackdrop() } |
| 218 | #if os(macOS) |
| 219 | .frame(width: 560, height: 640) |
| 220 | #endif |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | #endif |
| 225 | |
| 226 | /// How Coach answers without an agent. |
| 227 | enum CoachDeviceRoute { |
| 228 | static var hasAppleModel: Bool { |
| 229 | if #available(iOS 26, macOS 26, *) { return SystemLanguageModel.default.isAvailable } |
| 230 | return false |
| 231 | } |
| 232 | |
| 233 | static var title: String { hasAppleModel ? "Apple Intelligence" : "Glimmer’s practice guide" } |
| 234 | |
| 235 | static var summary: String { |
| 236 | hasAppleModel |
| 237 | ? "Coach answers with Apple Intelligence on this device. Your words and journal stay here." |
| 238 | : "Apple Intelligence isn’t available here, so Coach suggests practices from Glimmer’s guide." |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | /// Settings → Coach diagnostics: what answered last and why it failed, for support. Never questions or replies. |
| 243 | struct CoachDiagnosticsPanel: View { |
| 244 | @AppStorage(CoachAgent.preference) private var savedAgent = "" |
| 245 | |
| 246 | var body: some View { |
| 247 | let choice = CoachAgentChoice.current |
| 248 | Panel(title: "Coach diagnostics") { |
| 249 | VStack(spacing: 6) { |
| 250 | SettingsValueRow(label: "Replies with", value: choice.agent == .device ? CoachDeviceRoute.title : choice.title) |
| 251 | #if os(macOS) |
| 252 | if choice.agent.isLocalAgent { |
| 253 | SettingsValueRow(label: "Command line tool", value: choice.agent.executable?.path ?? "Not found") |
| 254 | SettingsValueRow(label: "Reasoning effort", value: CoachAgent.effortName(choice.effort.isEmpty && choice.agent == .gemini ? "high" : choice.effort)) |
| 255 | } |
| 256 | #endif |
| 257 | SettingsValueRow(label: "Last agent reply", value: SyncTimestamp.text(CoachAgentLog.lastSuccess)) |
| 258 | if let detail = CoachAgentLog.lastFailureDetail { |
| 259 | SettingsValueRow(label: "Last error", value: SyncTimestamp.text(CoachAgentLog.lastFailure)) |
| 260 | Text(detail).font(.callout).foregroundStyle(GlimmerColor.ember) |
| 261 | .frame(maxWidth: .infinity, alignment: .leading) |
| 262 | .fixedSize(horizontal: false, vertical: true) |
| 263 | .textSelection(.enabled) |
| 264 | .accessibilityIdentifier("coach.lastError") |
| 265 | } |
| 266 | } |
| 267 | .font(.callout) |
| 268 | ShareLink(item: report(choice)) { |
| 269 | RowLabel(title: "Share Coach Diagnostics", icon: "square.and.arrow.up", trailing: nil) |
| 270 | } |
| 271 | .glassRow() |
| 272 | .accessibilityIdentifier("settings.shareCoachDiagnostics") |
| 273 | Text("Includes the agent, model, tool location and recent errors. Your questions, Coach replies and journal are not included.") |
| 274 | .font(.callout).foregroundStyle(.secondary) |
| 275 | .fixedSize(horizontal: false, vertical: true) |
| 276 | } |
| 277 | .id(savedAgent) |
| 278 | } |
| 279 | |
| 280 | private func report(_ choice: CoachAgentChoice) -> String { |
| 281 | let version = Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") as? String ?? "1.0" |
| 282 | let build = Bundle.main.object(forInfoDictionaryKey: "CFBundleVersion") as? String ?? "—" |
| 283 | var lines = ["Glimmer \(version) (\(build))", "Coach: \(choice.agent.title)", |
| 284 | "Apple Intelligence: \(CoachDeviceRoute.hasAppleModel ? "available" : "unavailable")"] |
| 285 | #if os(macOS) |
| 286 | if choice.agent.isLocalAgent { |
| 287 | lines += ["Model: \(choice.resolvedModel ?? "agent default")", "Effort: \(choice.resolvedEffort ?? "default")", |
| 288 | "Time limit: \(choice.timeout) s", "Tool: \(choice.agent.executable?.path ?? "not found")"] |
| 289 | } |
| 290 | #endif |
| 291 | lines.append("Last agent reply: \(SyncTimestamp.text(CoachAgentLog.lastSuccess))") |
| 292 | if let detail = CoachAgentLog.lastSuccessDetail { lines.append(detail) } |
| 293 | if let detail = CoachAgentLog.lastFailureDetail { |
| 294 | lines.append("Last error: \(SyncTimestamp.text(CoachAgentLog.lastFailure))") |
| 295 | lines.append(detail) |
| 296 | } |
| 297 | return lines.joined(separator: "\n") |
| 298 | } |
| 299 | } |