Open History to see the last commit
1import Foundation
2
3public struct ServerProviderAuthSnapshot: Codable, Equatable, Sendable {
4 public let status: String
5 public let type: String?
6 public let label: String?
7 public let email: String?
8}
9
10public struct ServerProviderOptionChoice: Codable, Identifiable, Equatable, Sendable {
11 public let id: String
12 public let label: String
13 public let description: String?
14 public let isDefault: Bool?
15}
16
17public struct ServerSelectOptionDescriptor: Codable, Identifiable, Equatable, Sendable {
18 public let id: String
19 public let label: String
20 public let description: String?
21 public let options: [ServerProviderOptionChoice]
22 public let currentValue: String?
23 public let promptInjectedValues: [String]?
24}
25
26public struct ServerBooleanOptionDescriptor: Codable, Identifiable, Equatable, Sendable {
27 public let id: String
28 public let label: String
29 public let description: String?
30 public let currentValue: Bool?
31}
32
33public enum ServerProviderOptionDescriptor: Codable, Equatable, Sendable {
34 case select(ServerSelectOptionDescriptor)
35 case boolean(ServerBooleanOptionDescriptor)
36
37 private enum CodingKeys: String, CodingKey { case type }
38
39 public init(from decoder: any Decoder) throws {
40 let container = try decoder.container(keyedBy: CodingKeys.self)
41 switch try container.decode(String.self, forKey: .type) {
42 case "select":
43 self = .select(try ServerSelectOptionDescriptor(from: decoder))
44 case "boolean":
45 self = .boolean(try ServerBooleanOptionDescriptor(from: decoder))
46 case let type:
47 throw DecodingError.dataCorruptedError(
48 forKey: .type,
49 in: container,
50 debugDescription: "Unknown provider option type \(type)"
51 )
52 }
53 }
54
55 public func encode(to encoder: any Encoder) throws {
56 switch self {
57 case let .select(value):
58 try value.encode(to: encoder)
59 var container = encoder.container(keyedBy: CodingKeys.self)
60 try container.encode("select", forKey: .type)
61 case let .boolean(value):
62 try value.encode(to: encoder)
63 var container = encoder.container(keyedBy: CodingKeys.self)
64 try container.encode("boolean", forKey: .type)
65 }
66 }
67}
68
69public struct ServerModelCapabilities: Codable, Equatable, Sendable {
70 public let optionDescriptors: [ServerProviderOptionDescriptor]?
71}
72
73public struct ServerProviderModelSnapshot: Codable, Identifiable, Equatable, Sendable {
74 public var id: String { slug }
75
76 public let slug: String
77 public let name: String
78 public let shortName: String?
79 public let subProvider: String?
80 public let isCustom: Bool
81 public let isDefault: Bool?
82 public let isLegacy: Bool?
83 public let capabilities: ServerModelCapabilities?
84}
85
86public struct ServerProviderSlashCommandSnapshot: Codable, Equatable, Sendable {
87 public struct Input: Codable, Equatable, Sendable {
88 public let hint: String
89 }
90
91 public let name: String
92 public let description: String?
93 public let input: Input?
94}
95
96public struct ServerProviderSkillSnapshot: Codable, Equatable, Sendable {
97 public let name: String
98 public let description: String?
99 public let path: String
100 public let scope: String?
101 public let enabled: Bool
102 public let displayName: String?
103 public let shortDescription: String?
104 public var userInvocationOnly: Bool? = nil
105 public var userInvocable: Bool? = nil
106}
107
108public struct ServerProviderWorkspaceSnapshot: Codable, Equatable, Sendable {
109 public let cwd: String
110 public let checkedAt: String
111 public let slashCommands: [ServerProviderSlashCommandSnapshot]
112 public let skills: [ServerProviderSkillSnapshot]
113}
114
115public struct ServerProviderSnapshot: Codable, Identifiable, Equatable, Sendable {
116 public var id: String { instanceId }
117
118 public let instanceId: String
119 public let driver: String
120 public let displayName: String?
121 public let accentColor: String?
122 public let badgeLabel: String?
123 public let showInteractionModeToggle: Bool?
124 public let requiresNewThreadForModelChange: Bool?
125 public let enabled: Bool
126 public let installed: Bool
127 public let version: String?
128 public let status: String
129 public let auth: ServerProviderAuthSnapshot
130 public let checkedAt: String
131 public let message: String?
132 public let availability: String?
133 public let unavailableReason: String?
134 public let models: [ServerProviderModelSnapshot]
135 public let slashCommands: [ServerProviderSlashCommandSnapshot]?
136 public let skills: [ServerProviderSkillSnapshot]?
137 public var workspaceSnapshots: [ServerProviderWorkspaceSnapshot]? = nil
138 public var setup: ProviderSetupCapabilities? = nil
139}
140
141public enum ServerThreadEnvironmentMode: String, Codable, Equatable, Sendable {
142 case local
143 case worktree
144}
145
146public enum ServerProjectGroupingMode: String, Codable, Equatable, Sendable {
147 case repository
148 case repositoryPath = "repository_path"
149 case separate
150}
151
152/// New-thread preferences are server-authoritative, so every saved environment
153/// can resolve these differently even though they share one mobile client.
154public struct ServerSettingsSnapshot: Codable, Equatable, Sendable {
155 public let defaultThreadEnvMode: ServerThreadEnvironmentMode
156 public let newWorktreesStartFromOrigin: Bool
157 public let sidebarProjectGroupingMode: ServerProjectGroupingMode?
158 public let sidebarProjectGroupingOverrides: [String: ServerProjectGroupingMode]?
159 public let sidebarAutoSettleOnMerge: Bool
160 public let sidebarAutoSettleAfterDays: Double?
161 public var environmentIcon: String? = nil
162 public var sourceControlWritingStyle: JSONValue? = nil
163
164 public var sharedPatch: JSONValue {
165 var fields: [String: JSONValue] = [
166 "sidebarAutoSettleAfterDays": sidebarAutoSettleAfterDays.map(JSONValue.number) ?? .null,
167 "sidebarAutoSettleOnMerge": .bool(sidebarAutoSettleOnMerge),
168 "defaultThreadEnvMode": .string(defaultThreadEnvMode.rawValue),
169 "newWorktreesStartFromOrigin": .bool(newWorktreesStartFromOrigin),
170 ]
171 if let sourceControlWritingStyle { fields["sourceControlWritingStyle"] = sourceControlWritingStyle }
172 return .object(fields)
173 }
174
175 public init(
176 defaultThreadEnvMode: ServerThreadEnvironmentMode = .local,
177 newWorktreesStartFromOrigin: Bool = true,
178 sidebarProjectGroupingMode: ServerProjectGroupingMode? = nil,
179 sidebarProjectGroupingOverrides: [String: ServerProjectGroupingMode]? = nil,
180 sidebarAutoSettleOnMerge: Bool = true,
181 sidebarAutoSettleAfterDays: Double? = 3
182 ) {
183 self.defaultThreadEnvMode = defaultThreadEnvMode
184 self.newWorktreesStartFromOrigin = newWorktreesStartFromOrigin
185 self.sidebarProjectGroupingMode = sidebarProjectGroupingMode
186 self.sidebarProjectGroupingOverrides = sidebarProjectGroupingOverrides
187 self.sidebarAutoSettleOnMerge = sidebarAutoSettleOnMerge
188 self.sidebarAutoSettleAfterDays = sidebarAutoSettleAfterDays
189 }
190
191 private enum CodingKeys: String, CodingKey {
192 case defaultThreadEnvMode
193 case newWorktreesStartFromOrigin
194 case sidebarProjectGroupingMode
195 case sidebarProjectGroupingOverrides
196 case sidebarAutoSettleOnMerge
197 case sidebarAutoSettleAfterDays
198 case environmentIcon
199 case sourceControlWritingStyle
200 }
201
202 public init(from decoder: any Decoder) throws {
203 let container = try decoder.container(keyedBy: CodingKeys.self)
204 environmentIcon = try container.decodeIfPresent(String.self, forKey: .environmentIcon)
205 sourceControlWritingStyle = try container.decodeIfPresent(JSONValue.self, forKey: .sourceControlWritingStyle)
206 defaultThreadEnvMode = try container.decodeIfPresent(
207 ServerThreadEnvironmentMode.self,
208 forKey: .defaultThreadEnvMode
209 ) ?? .local
210 newWorktreesStartFromOrigin = try container.decodeIfPresent(
211 Bool.self,
212 forKey: .newWorktreesStartFromOrigin
213 ) ?? true
214 sidebarProjectGroupingMode = try container.decodeIfPresent(
215 ServerProjectGroupingMode.self,
216 forKey: .sidebarProjectGroupingMode
217 )
218 sidebarProjectGroupingOverrides = try container.decodeIfPresent(
219 [String: ServerProjectGroupingMode].self,
220 forKey: .sidebarProjectGroupingOverrides
221 )
222 sidebarAutoSettleOnMerge = try container.decodeIfPresent(
223 Bool.self,
224 forKey: .sidebarAutoSettleOnMerge
225 ) ?? true
226 sidebarAutoSettleAfterDays = if container.contains(.sidebarAutoSettleAfterDays) {
227 try container.decodeIfPresent(Double.self, forKey: .sidebarAutoSettleAfterDays)
228 } else {
229 3
230 }
231 }
232}
233
234public enum ServerSettingsChange: Equatable, Sendable {
235 case sidebarAutoSettleOnMerge(Bool)
236 case sidebarAutoSettleAfterDays(Double?)
237 case defaultThreadEnvMode(ServerThreadEnvironmentMode)
238 case newWorktreesStartFromOrigin(Bool)
239 case environmentIcon(String?)
240 case sharedPreferences(JSONValue)
241
242 public var jsonValue: JSONValue {
243 switch self {
244 case let .defaultThreadEnvMode(value): .object(["defaultThreadEnvMode": .string(value.rawValue)])
245 case let .newWorktreesStartFromOrigin(value): .object(["newWorktreesStartFromOrigin": .bool(value)])
246 case let .environmentIcon(value): .object(["environmentIcon": value.map(JSONValue.string) ?? .null])
247 case let .sharedPreferences(value): value
248 case let .sidebarAutoSettleOnMerge(value):
249 .object(["sidebarAutoSettleOnMerge": .bool(value)])
250 case let .sidebarAutoSettleAfterDays(value):
251 .object(["sidebarAutoSettleAfterDays": value.map(JSONValue.number) ?? .null])
252 }
253 }
254}
255
256/// Narrow decode view of the much larger `ServerConfig` RPC result.
257public struct ServerConfigSnapshot: Codable, Equatable, Sendable {
258 public let providers: [ServerProviderSnapshot]
259 public let settings: ServerSettingsSnapshot?
260 public let threadSnapshotPagination: Bool?
261 public let threadResumeCompletionMarker: Bool?
262 public let environment: EnvironmentDescriptor?
263
264 public init(
265 providers: [ServerProviderSnapshot],
266 settings: ServerSettingsSnapshot? = nil,
267 threadSnapshotPagination: Bool? = nil,
268 threadResumeCompletionMarker: Bool? = nil,
269 environment: EnvironmentDescriptor? = nil
270 ) {
271 self.providers = providers
272 self.settings = settings
273 self.threadSnapshotPagination = threadSnapshotPagination
274 self.threadResumeCompletionMarker = threadResumeCompletionMarker
275 self.environment = environment
276 }
277
278 private enum CodingKeys: String, CodingKey {
279 case providers, settings, threadSnapshotPagination, threadResumeCompletionMarker, environment
280 }
281
282 public init(from decoder: any Decoder) throws {
283 let container = try decoder.container(keyedBy: CodingKeys.self)
284 providers = try container.decode(
285 [LossyDecodableElement<ServerProviderSnapshot>].self,
286 forKey: .providers
287 ).compactMap(\.value)
288 settings = try container.decodeIfPresent(ServerSettingsSnapshot.self, forKey: .settings)
289 threadSnapshotPagination = try container.decodeIfPresent(
290 Bool.self,
291 forKey: .threadSnapshotPagination
292 )
293 environment = try container.decodeIfPresent(EnvironmentDescriptor.self, forKey: .environment)
294 threadResumeCompletionMarker = try container.decodeIfPresent(
295 Bool.self, forKey: .threadResumeCompletionMarker
296 )
297 }
298
299 public func encode(to encoder: any Encoder) throws {
300 var container = encoder.container(keyedBy: CodingKeys.self)
301 try container.encode(providers, forKey: .providers)
302 try container.encodeIfPresent(settings, forKey: .settings)
303 try container.encodeIfPresent(
304 threadSnapshotPagination,
305 forKey: .threadSnapshotPagination
306 )
307 try container.encodeIfPresent(environment, forKey: .environment)
308 try container.encodeIfPresent(threadResumeCompletionMarker, forKey: .threadResumeCompletionMarker)
309 }
310}
311
312private struct LossyDecodableElement<Value: Decodable>: Decodable {
313 let value: Value?
314
315 init(from decoder: any Decoder) throws {
316 value = try? Value(from: decoder)
317 }
318}
319
320public enum ServerConfigStreamEvent: Decodable, Sendable {
321 case snapshot(ServerConfigSnapshot)
322 case providerStatuses([ServerProviderSnapshot])
323 case settingsUpdated(ServerSettingsSnapshot)
324 case unrelated(type: String)
325
326 private enum CodingKeys: String, CodingKey { case type, config, payload }
327 private struct ProviderPayload: Decodable {
328 let providers: [ServerProviderSnapshot]
329
330 private enum CodingKeys: String, CodingKey { case providers }
331
332 init(from decoder: any Decoder) throws {
333 let container = try decoder.container(keyedBy: CodingKeys.self)
334 providers = try container.decode(
335 [LossyDecodableElement<ServerProviderSnapshot>].self,
336 forKey: .providers
337 ).compactMap(\.value)
338 }
339 }
340 private struct SettingsPayload: Decodable { let settings: ServerSettingsSnapshot }
341
342 public init(from decoder: any Decoder) throws {
343 let container = try decoder.container(keyedBy: CodingKeys.self)
344 let type = try container.decode(String.self, forKey: .type)
345 switch type {
346 case "snapshot":
347 self = .snapshot(
348 try container.decode(ServerConfigSnapshot.self, forKey: .config)
349 )
350 case "providerStatuses":
351 self = .providerStatuses(
352 try container.decode(ProviderPayload.self, forKey: .payload).providers
353 )
354 case "settingsUpdated":
355 self = .settingsUpdated(
356 try container.decode(SettingsPayload.self, forKey: .payload).settings
357 )
358 default:
359 self = .unrelated(type: type)
360 }
361 }
362}
363
364public struct ServerRefreshProvidersResult: Codable, Equatable, Sendable {
365 public let providers: [ServerProviderSnapshot]
366
367 public init(providers: [ServerProviderSnapshot]) {
368 self.providers = providers
369 }
370}