OpenTalon / app / native / Sources / OpenTalon / ConnectionSetup.swift
| 1 | import AppKit |
| 2 | import SwiftUI |
| 3 | import UniformTypeIdentifiers |
| 4 | |
| 5 | private enum Client: String, CaseIterable, Identifiable { |
| 6 | case claudeCode = "claude-code", codex, gemini, claude |
| 7 | var id: Self { self } |
| 8 | var title: String { |
| 9 | switch self { |
| 10 | case .claudeCode: "Claude" |
| 11 | case .codex: "Codex" |
| 12 | case .gemini: "Gemini CLI" |
| 13 | case .claude: "Claude chat" |
| 14 | } |
| 15 | } |
| 16 | var agent: Agent { |
| 17 | switch self { |
| 18 | case .codex: .codex |
| 19 | case .gemini: .gemini |
| 20 | case .claude, .claudeCode: .claude |
| 21 | } |
| 22 | } |
| 23 | var guide: String { |
| 24 | switch self { |
| 25 | case .claudeCode: "https://code.claude.com/docs/en/desktop#connect-external-tools" |
| 26 | case .gemini: "https://geminicli.com/docs/tools/mcp-server/" |
| 27 | case .codex: "https://learn.chatgpt.com/docs/extend/mcp?surface=desktop" |
| 28 | case .claude: "https://support.claude.com/en/articles/10949351-getting-started-with-local-mcp-servers-on-claude-desktop" |
| 29 | } |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | private struct SetupField: Decodable, Identifiable { |
| 34 | let id: String |
| 35 | let label: String |
| 36 | let value: String |
| 37 | } |
| 38 | |
| 39 | private struct ProjectSetup: Decodable { |
| 40 | let id: String |
| 41 | let replacing: Bool |
| 42 | } |
| 43 | |
| 44 | struct ConnectionSetup: View { |
| 45 | var showsTitle = true |
| 46 | @Environment(WorkspaceModel.self) private var model |
| 47 | @Environment(\.palette) private var palette |
| 48 | @State private var client = Client.claudeCode |
| 49 | @State private var terminalClient = Client.claudeCode |
| 50 | @State private var connection: Connection? |
| 51 | @State private var fields: [SetupField] = [] |
| 52 | @State private var error: String? |
| 53 | @State private var note: String? |
| 54 | @State private var testResult: Result<Void, Error>? |
| 55 | @State private var busy = false |
| 56 | @State private var confirmReset = false |
| 57 | @State private var replacement: ProjectSetup? |
| 58 | |
| 59 | var body: some View { |
| 60 | VStack(alignment: .leading, spacing: 11) { |
| 61 | if showsTitle { |
| 62 | Text("Agent connection").font(.system(size: 16, weight: .semibold)).accessibilityAddTraits(.isHeader) |
| 63 | } |
| 64 | Text("Connect an agent to your local boards and tasks. OpenTalon stays available when you close its window.") |
| 65 | .font(.system(size: 12)).foregroundStyle(palette.muted) |
| 66 | if let connection { |
| 67 | HStack { |
| 68 | Toggle("Enable MCP connection", isOn: Binding(get: { connection.enabled }, set: toggle)) |
| 69 | .accessibilityLabel("Enable MCP connection") |
| 70 | Spacer() |
| 71 | Button("Test connection", systemImage: "checkmark.shield", action: testConnection) |
| 72 | .disabled(!connection.running) |
| 73 | } |
| 74 | if let testResult { |
| 75 | switch testResult { |
| 76 | case .success: |
| 77 | Label("OpenTalon answered the local read-only test.", systemImage: "checkmark.circle.fill") |
| 78 | .foregroundStyle(palette.text).font(.caption) |
| 79 | Text("In your connected client, try “List my OpenTalon projects.”") |
| 80 | .font(.callout).textSelection(.enabled) |
| 81 | case .failure(let failure): |
| 82 | Label(failure.localizedDescription, systemImage: "exclamationmark.circle") |
| 83 | .foregroundStyle(palette.danger).font(.caption) |
| 84 | } |
| 85 | } |
| 86 | if connection.running { |
| 87 | Label("Running locally", systemImage: "checkmark.circle").font(.caption).foregroundStyle(palette.muted) |
| 88 | HStack(spacing: 8) { |
| 89 | ForEach(Client.allCases) { option in |
| 90 | Button { client = option } label: { AgentBrandLabel(agent: option.agent, title: option.title) } |
| 91 | .buttonStyle(GlassActionStyle(prominent: option == client)) |
| 92 | .accessibilityAddTraits(option == client ? .isSelected : []) |
| 93 | } |
| 94 | } |
| 95 | .accessibilityElement(children: .contain) |
| 96 | .accessibilityLabel("Client") |
| 97 | instructions |
| 98 | DisclosureGroup("Advanced setup") { advanced.padding(.top, 8) } |
| 99 | } |
| 100 | if !connection.error.isEmpty { Text(connection.error).foregroundStyle(palette.danger).font(.caption) } |
| 101 | } else { |
| 102 | ProgressView().controlSize(.small) |
| 103 | } |
| 104 | if let error { Text(error).foregroundStyle(palette.danger).font(.caption) } |
| 105 | if let note { Text(note).foregroundStyle(palette.muted).font(.caption).textSelection(.enabled) } |
| 106 | } |
| 107 | .disabled(busy) |
| 108 | .task(id: model.connectionSignal) { |
| 109 | do { try await reload() } |
| 110 | catch { self.error = error.localizedDescription } |
| 111 | } |
| 112 | .alert("Reset the connection key?", isPresented: $confirmReset) { |
| 113 | Button("Cancel", role: .cancel) {} |
| 114 | Button("Reset key", role: .destructive) { |
| 115 | run { |
| 116 | connection = try await model.service.call("reset_connection_key") |
| 117 | try await reload() |
| 118 | note = "Connection key reset. Refresh any direct HTTP configuration." |
| 119 | } |
| 120 | } |
| 121 | } message: { Text("Clients with the old direct HTTP key will lose access.") } |
| 122 | .alert("Replace this project’s OpenTalon connection?", isPresented: Binding( |
| 123 | get: { replacement != nil }, set: { if !$0 { replacement = nil } } |
| 124 | )) { |
| 125 | Button("Cancel", role: .cancel) { replacement = nil } |
| 126 | Button("Replace OpenTalon connection") { |
| 127 | if let setup = replacement { |
| 128 | replacement = nil |
| 129 | run { try await writeProject(setup) } |
| 130 | } |
| 131 | } |
| 132 | } message: { Text("Other servers and settings in this folder will be preserved.") } |
| 133 | } |
| 134 | |
| 135 | private var instructions: some View { |
| 136 | VStack(alignment: .leading, spacing: 9) { |
| 137 | switch client { |
| 138 | case .claudeCode: |
| 139 | Text("1. Save the connection in your Claude working folder.") |
| 140 | Text("2. Open Claude, select Code, and open that folder. Approve the project’s OpenTalon connection when prompted.") |
| 141 | HStack { |
| 142 | Button("Save project connection…", action: saveProject) |
| 143 | Button { openApp("com.anthropic.claudefordesktop") } label: { AgentBrandLabel(agent: .claude, title: "Open Claude") } |
| 144 | } |
| 145 | case .codex: |
| 146 | Text("1. In Codex, open Settings → MCP servers and add a server.") |
| 147 | Text("2. Choose STDIO and copy the fields below. Save, then reload the MCP connection.") |
| 148 | ForEach(fields) { field in |
| 149 | HStack { |
| 150 | Text(field.label).fontWeight(.medium) |
| 151 | Spacer() |
| 152 | Text(field.value).lineLimit(1).truncationMode(.middle).foregroundStyle(palette.muted) |
| 153 | Button("Copy") { copy(field.value, message: "Copied \(field.label).") } |
| 154 | .accessibilityLabel("Copy \(field.label)") |
| 155 | } |
| 156 | } |
| 157 | Button { openApp("com.openai.codex") } label: { AgentBrandLabel(agent: .codex, title: "Open Codex") } |
| 158 | case .gemini: |
| 159 | Text("1. Open Terminal in a project folder you trust in Gemini CLI. Run the copied command to connect just that project.") |
| 160 | HStack { |
| 161 | Button("Copy setup command") { copyTerminalSetup(.gemini) } |
| 162 | Button("Open Terminal") { openApp("com.apple.Terminal") } |
| 163 | } |
| 164 | Text("2. Run gemini mcp list. When OpenTalon shows Connected, start Gemini and ask “List my OpenTalon projects.”") |
| 165 | .textSelection(.enabled) |
| 166 | WebLink(title: "Install Gemini CLI", url: "https://geminicli.com/docs/get-started/installation/", symbol: "arrow.up.right") |
| 167 | case .claude: |
| 168 | Text("1. Save the OpenTalon extension.") |
| 169 | Text("2. Open Claude Settings → Extensions and install the local .mcpb file.") |
| 170 | HStack { |
| 171 | Button(action: saveExtension) { AgentBrandLabel(agent: .claude, title: "Save Claude extension…") } |
| 172 | Button { openApp("com.anthropic.claudefordesktop") } label: { AgentBrandLabel(agent: .claude, title: "Open Claude") } |
| 173 | } |
| 174 | } |
| 175 | WebLink(title: "Official setup guide", url: client.guide, symbol: "arrow.up.right") |
| 176 | } |
| 177 | .font(.system(size: 12)) |
| 178 | .padding(12) |
| 179 | .frame(maxWidth: .infinity, alignment: .leading) |
| 180 | .surface() |
| 181 | } |
| 182 | |
| 183 | private var advanced: some View { |
| 184 | VStack(alignment: .leading, spacing: 9) { |
| 185 | HStack { |
| 186 | Button("Copy STDIO JSON") { configuration("stdio") } |
| 187 | Button("Copy HTTP JSON") { configuration("http") } |
| 188 | Button("Reset connection key…") { confirmReset = true } |
| 189 | } |
| 190 | HStack { |
| 191 | BoardDropdown(selection: $terminalClient, title: "Terminal setup", symbol: "terminal", |
| 192 | choices: [.claudeCode, .codex, .gemini], compact: true, agentBrand: \.agent, label: \.title) |
| 193 | .frame(width: 170) |
| 194 | Button("Copy command") { copyTerminalSetup(terminalClient) } |
| 195 | Button("Open Terminal") { openApp("com.apple.Terminal") } |
| 196 | } |
| 197 | if let endpoint = connection?.endpoint { |
| 198 | Text(endpoint).font(.caption2.monospaced()).foregroundStyle(palette.muted).textSelection(.enabled) |
| 199 | } |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | private func copyTerminalSetup(_ client: Client) { |
| 204 | run { |
| 205 | let result: ValueResponse = try await model.service.call("copy_terminal_setup", ["client": client.rawValue]) |
| 206 | copy(result.value, message: "Terminal command copied.") |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | private func toggle(_ enabled: Bool) { |
| 211 | run { |
| 212 | connection = try await model.service.call("set_connection", ["enabled": enabled]) |
| 213 | model.connectionSignal += 1 |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | private func testConnection() { |
| 218 | testResult = nil |
| 219 | run { |
| 220 | do { |
| 221 | let _: EmptyResponse = try await model.service.call("native_test_connection") |
| 222 | testResult = .success(()) |
| 223 | } catch { |
| 224 | testResult = .failure(error) |
| 225 | } |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | private func reload() async throws { |
| 230 | testResult = nil |
| 231 | let current: Connection = try await model.service.call("connection_status") |
| 232 | connection = current |
| 233 | fields = current.running ? try await model.service.call("agent_setup_fields") : [] |
| 234 | } |
| 235 | |
| 236 | private func copy(_ value: String, message: String) { |
| 237 | NSPasteboard.general.clearContents() |
| 238 | NSPasteboard.general.setString(value, forType: .string) |
| 239 | note = message |
| 240 | } |
| 241 | |
| 242 | private func configuration(_ transport: String) { |
| 243 | run { |
| 244 | let result: ValueResponse = try await model.service.call("client_configuration", ["transport": transport]) |
| 245 | copy(result.value, message: "Configuration copied.") |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | private func saveProject() { |
| 250 | run { |
| 251 | guard let folder = await chooseDirectory("Choose your Claude working folder", prompt: "Save project connection") else { return } |
| 252 | let setup: ProjectSetup = try await model.service.call("native_prepare_project", ["path": folder.path]) |
| 253 | if setup.replacing { replacement = setup } |
| 254 | else { try await writeProject(setup) } |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | private func writeProject(_ setup: ProjectSetup) async throws { |
| 259 | let _: EmptyResponse = try await model.service.call("native_write_project", ["id": setup.id]) |
| 260 | note = "Project connection saved." |
| 261 | } |
| 262 | |
| 263 | private func saveExtension() { |
| 264 | run { |
| 265 | let panel = NSSavePanel() |
| 266 | panel.title = "Save Claude Desktop extension" |
| 267 | panel.nameFieldStringValue = "OpenTalon.mcpb" |
| 268 | if let type = UTType(filenameExtension: "mcpb") { panel.allowedContentTypes = [type] } |
| 269 | guard await panel.begin() == .OK, let url = panel.url else { return } |
| 270 | let result: PathResponse = try await model.service.call("native_save_extension", ["path": url.path]) |
| 271 | note = "Extension saved." |
| 272 | NSWorkspace.shared.activateFileViewerSelecting([URL(fileURLWithPath: result.path)]) |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | private func openApp(_ id: String) { |
| 277 | if let url = NSWorkspace.shared.urlForApplication(withBundleIdentifier: id) { |
| 278 | NSWorkspace.shared.openApplication(at: url, configuration: NSWorkspace.OpenConfiguration()) { _, error in |
| 279 | if let error { Task { @MainActor in self.error = error.localizedDescription } } |
| 280 | } |
| 281 | } else { |
| 282 | error = "This app could not be opened. Check that it’s installed." |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | private func run(_ operation: @escaping @MainActor () async throws -> Void) { |
| 287 | busy = true |
| 288 | error = nil |
| 289 | note = nil |
| 290 | Task { |
| 291 | defer { busy = false } |
| 292 | do { try await operation() } |
| 293 | catch let failure { error = failure.localizedDescription } |
| 294 | } |
| 295 | } |
| 296 | } |