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 public var usageLimits: ServerProviderUsageLimits? = nil
140 public var supportsConversationRollback: Bool? = nil
141}
142
143public enum ServerThreadEnvironmentMode: String, Codable, Equatable, Sendable {
144 case local
145 case worktree
146}
147
148public enum ServerProjectGroupingMode: String, Codable, Equatable, Sendable {
149 case repository
150 case repositoryPath = "repository_path"
151 case separate
152}
153
154/// New-thread preferences are server-authoritative, so every saved environment
155/// can resolve these differently even though they share one mobile client.
156public struct ServerSettingsSnapshot: Codable, Equatable, Sendable {
157 public var defaultModelSelection: ModelSelection? = nil
158 public var defaultThreadEnvMode: ServerThreadEnvironmentMode
159 public var newWorktreesStartFromOrigin: Bool
160 public let sidebarProjectGroupingMode: ServerProjectGroupingMode?
161 public let sidebarProjectGroupingOverrides: [String: ServerProjectGroupingMode]?
162 public var sidebarAutoSettleOnMerge: Bool
163 public var sidebarAutoSettleAfterDays: Double?
164 public var continueThreadsAfterServerUpdate: Bool
165 public var defaultAutoPull = false
166 /// Missing on servers that do not support the current streaming setting.
167 public var responseStreamingMode: ResponseStreamingMode? = nil
168 public var projectSettingsOverrides: [String: [String: JSONValue]] = [:]
169 public var projectSettingsFolded = false
170 public var environmentIcon: String? = nil
171 public var sourceControlWritingStyle: JSONValue? = nil
172
173 public var sharedPatch: JSONValue {
174 sharedPatch(supportsRestartContinuation: false)
175 }
176
177 /// Include restart continuation only when both environments support it.
178 public func sharedPatch(supportsRestartContinuation: Bool) -> JSONValue {
179 var fields: [String: JSONValue] = [
180 "sidebarAutoSettleAfterDays": sidebarAutoSettleAfterDays.map(JSONValue.number) ?? .null,
181 "sidebarAutoSettleOnMerge": .bool(sidebarAutoSettleOnMerge),
182 "defaultThreadEnvMode": .string(defaultThreadEnvMode.rawValue),
183 "newWorktreesStartFromOrigin": .bool(newWorktreesStartFromOrigin),
184 ]
185 if let sourceControlWritingStyle { fields["sourceControlWritingStyle"] = sourceControlWritingStyle }
186 if supportsRestartContinuation {
187 fields["continueThreadsAfterServerUpdate"] = .bool(continueThreadsAfterServerUpdate)
188 }
189 return .object(fields)
190 }
191
192 public init(
193 defaultThreadEnvMode: ServerThreadEnvironmentMode = .local,
194 newWorktreesStartFromOrigin: Bool = true,
195 sidebarProjectGroupingMode: ServerProjectGroupingMode? = nil,
196 sidebarProjectGroupingOverrides: [String: ServerProjectGroupingMode]? = nil,
197 sidebarAutoSettleOnMerge: Bool = true,
198 sidebarAutoSettleAfterDays: Double? = 3,
199 continueThreadsAfterServerUpdate: Bool = false
200 ) {
201 self.defaultThreadEnvMode = defaultThreadEnvMode
202 self.newWorktreesStartFromOrigin = newWorktreesStartFromOrigin
203 self.sidebarProjectGroupingMode = sidebarProjectGroupingMode
204 self.sidebarProjectGroupingOverrides = sidebarProjectGroupingOverrides
205 self.sidebarAutoSettleOnMerge = sidebarAutoSettleOnMerge
206 self.sidebarAutoSettleAfterDays = sidebarAutoSettleAfterDays
207 self.continueThreadsAfterServerUpdate = continueThreadsAfterServerUpdate
208 }
209
210 private enum CodingKeys: String, CodingKey {
211 case defaultModelSelection
212 case defaultThreadEnvMode
213 case newWorktreesStartFromOrigin
214 case sidebarProjectGroupingMode
215 case sidebarProjectGroupingOverrides
216 case sidebarAutoSettleOnMerge
217 case sidebarAutoSettleAfterDays
218 case continueThreadsAfterServerUpdate
219 case environmentIcon
220 case sourceControlWritingStyle
221 case defaultAutoPull, responseStreamingMode, projectSettingsOverrides, projectSettingsFolded
222 }
223
224 public init(from decoder: any Decoder) throws {
225 let container = try decoder.container(keyedBy: CodingKeys.self)
226 defaultModelSelection = try container.decodeIfPresent(ModelSelection.self, forKey: .defaultModelSelection)
227 environmentIcon = try container.decodeIfPresent(String.self, forKey: .environmentIcon)
228 sourceControlWritingStyle = try container.decodeIfPresent(JSONValue.self, forKey: .sourceControlWritingStyle)
229 defaultAutoPull = try container.decodeIfPresent(Bool.self, forKey: .defaultAutoPull) ?? false
230 responseStreamingMode = try container.decodeIfPresent(ResponseStreamingMode.self, forKey: .responseStreamingMode)
231 projectSettingsOverrides = try container.decodeIfPresent([String: [String: JSONValue]].self, forKey: .projectSettingsOverrides) ?? [:]
232 projectSettingsFolded = try container.decodeIfPresent(Bool.self, forKey: .projectSettingsFolded) ?? false
233 continueThreadsAfterServerUpdate = try container.decodeIfPresent(
234 Bool.self,
235 forKey: .continueThreadsAfterServerUpdate
236 ) ?? false
237 defaultThreadEnvMode = try container.decodeIfPresent(
238 ServerThreadEnvironmentMode.self,
239 forKey: .defaultThreadEnvMode
240 ) ?? .local
241 newWorktreesStartFromOrigin = try container.decodeIfPresent(
242 Bool.self,
243 forKey: .newWorktreesStartFromOrigin
244 ) ?? true
245 sidebarProjectGroupingMode = try container.decodeIfPresent(
246 ServerProjectGroupingMode.self,
247 forKey: .sidebarProjectGroupingMode
248 )
249 sidebarProjectGroupingOverrides = try container.decodeIfPresent(
250 [String: ServerProjectGroupingMode].self,
251 forKey: .sidebarProjectGroupingOverrides
252 )
253 sidebarAutoSettleOnMerge = try container.decodeIfPresent(
254 Bool.self,
255 forKey: .sidebarAutoSettleOnMerge
256 ) ?? true
257 sidebarAutoSettleAfterDays = if container.contains(.sidebarAutoSettleAfterDays) {
258 try container.decodeIfPresent(Double.self, forKey: .sidebarAutoSettleAfterDays)
259 } else {
260 3
261 }
262 }
263}
264
265public enum ServerSettingsChange: Equatable, Sendable {
266 case sidebarAutoSettleOnMerge(Bool)
267 case sidebarAutoSettleAfterDays(Double?)
268 case defaultThreadEnvMode(ServerThreadEnvironmentMode)
269 case newWorktreesStartFromOrigin(Bool)
270 case continueThreadsAfterServerUpdate(Bool)
271 case environmentIcon(String?)
272 case sharedPreferences(JSONValue)
273 case responseStreamingMode(ResponseStreamingMode)
274 case projectSettingsOverrides(projectID: String, entry: [String: JSONValue]?)
275
276 public var jsonValue: JSONValue {
277 switch self {
278 case let .defaultThreadEnvMode(value): .object(["defaultThreadEnvMode": .string(value.rawValue)])
279 case let .newWorktreesStartFromOrigin(value): .object(["newWorktreesStartFromOrigin": .bool(value)])
280 case let .continueThreadsAfterServerUpdate(value):
281 .object(["continueThreadsAfterServerUpdate": .bool(value)])
282 case let .environmentIcon(value): .object(["environmentIcon": value.map(JSONValue.string) ?? .null])
283 case let .sharedPreferences(value): value
284 case let .responseStreamingMode(value): .object(["responseStreamingMode": .string(value.rawValue)])
285 case let .projectSettingsOverrides(projectID, entry):
286 .object(["projectSettingsOverrides": .object([projectID: entry.map(JSONValue.object) ?? .null])])
287 case let .sidebarAutoSettleOnMerge(value):
288 .object(["sidebarAutoSettleOnMerge": .bool(value)])
289 case let .sidebarAutoSettleAfterDays(value):
290 .object(["sidebarAutoSettleAfterDays": value.map(JSONValue.number) ?? .null])
291 }
292 }
293}
294
295/// Narrow decode view of the much larger `ServerConfig` RPC result.
296public struct ServerConfigSnapshot: Codable, Equatable, Sendable {
297 public let providers: [ServerProviderSnapshot]
298 public let settings: ServerSettingsSnapshot?
299 public let threadSnapshotPagination: Bool?
300 public let threadResumeCompletionMarker: Bool?
301 public let environment: EnvironmentDescriptor?
302 public var usageLimitSources: [UsageLimitSourceSnapshot]
303
304 public init(
305 providers: [ServerProviderSnapshot],
306 settings: ServerSettingsSnapshot? = nil,
307 threadSnapshotPagination: Bool? = nil,
308 threadResumeCompletionMarker: Bool? = nil,
309 environment: EnvironmentDescriptor? = nil,
310 usageLimitSources: [UsageLimitSourceSnapshot] = []
311 ) {
312 self.providers = providers
313 self.settings = settings
314 self.threadSnapshotPagination = threadSnapshotPagination
315 self.threadResumeCompletionMarker = threadResumeCompletionMarker
316 self.environment = environment
317 self.usageLimitSources = usageLimitSources
318 }
319
320 private enum CodingKeys: String, CodingKey {
321 case providers, settings, threadSnapshotPagination, threadResumeCompletionMarker, environment
322 case usageLimitSources
323 }
324
325 public init(from decoder: any Decoder) throws {
326 let container = try decoder.container(keyedBy: CodingKeys.self)
327 providers = try container.decode(
328 [LossyDecodableElement<ServerProviderSnapshot>].self,
329 forKey: .providers
330 ).compactMap(\.value)
331 settings = try container.decodeIfPresent(ServerSettingsSnapshot.self, forKey: .settings)
332 threadSnapshotPagination = try container.decodeIfPresent(
333 Bool.self,
334 forKey: .threadSnapshotPagination
335 )
336 environment = try container.decodeIfPresent(EnvironmentDescriptor.self, forKey: .environment)
337 threadResumeCompletionMarker = try container.decodeIfPresent(
338 Bool.self, forKey: .threadResumeCompletionMarker
339 )
340 usageLimitSources = try container.decodeIfPresent(
341 ForwardCompatibleArray<UsageLimitSourceSnapshot>.self,
342 forKey: .usageLimitSources
343 )?.wrappedValue ?? []
344 }
345
346 public func encode(to encoder: any Encoder) throws {
347 var container = encoder.container(keyedBy: CodingKeys.self)
348 try container.encode(providers, forKey: .providers)
349 try container.encodeIfPresent(settings, forKey: .settings)
350 try container.encodeIfPresent(
351 threadSnapshotPagination,
352 forKey: .threadSnapshotPagination
353 )
354 try container.encodeIfPresent(environment, forKey: .environment)
355 try container.encodeIfPresent(threadResumeCompletionMarker, forKey: .threadResumeCompletionMarker)
356 try container.encode(usageLimitSources, forKey: .usageLimitSources)
357 }
358}
359
360private struct LossyDecodableElement<Value: Decodable>: Decodable {
361 let value: Value?
362
363 init(from decoder: any Decoder) throws {
364 value = try? Value(from: decoder)
365 }
366}
367
368public enum ServerConfigStreamEvent: Decodable, Sendable {
369 case snapshot(ServerConfigSnapshot)
370 case providerStatuses([ServerProviderSnapshot])
371 case settingsUpdated(ServerSettingsSnapshot)
372 case usageLimitSourcesUpdated([UsageLimitSourceSnapshot])
373 case unrelated(type: String)
374
375 private enum CodingKeys: String, CodingKey { case type, config, payload }
376 private struct ProviderPayload: Decodable {
377 let providers: [ServerProviderSnapshot]
378
379 private enum CodingKeys: String, CodingKey { case providers }
380
381 init(from decoder: any Decoder) throws {
382 let container = try decoder.container(keyedBy: CodingKeys.self)
383 providers = try container.decode(
384 [LossyDecodableElement<ServerProviderSnapshot>].self,
385 forKey: .providers
386 ).compactMap(\.value)
387 }
388 }
389 private struct SettingsPayload: Decodable { let settings: ServerSettingsSnapshot }
390 private struct UsageLimitSourcesPayload: Decodable {
391 @ForwardCompatibleArray var sources: [UsageLimitSourceSnapshot]
392 }
393
394 public init(from decoder: any Decoder) throws {
395 let container = try decoder.container(keyedBy: CodingKeys.self)
396 let type = try container.decode(String.self, forKey: .type)
397 switch type {
398 case "snapshot":
399 self = .snapshot(
400 try container.decode(ServerConfigSnapshot.self, forKey: .config)
401 )
402 case "providerStatuses":
403 self = .providerStatuses(
404 try container.decode(ProviderPayload.self, forKey: .payload).providers
405 )
406 case "settingsUpdated":
407 self = .settingsUpdated(
408 try container.decode(SettingsPayload.self, forKey: .payload).settings
409 )
410 case "usageLimitSourcesUpdated":
411 self = .usageLimitSourcesUpdated(
412 try container.decode(UsageLimitSourcesPayload.self, forKey: .payload).sources
413 )
414 default:
415 self = .unrelated(type: type)
416 }
417 }
418}
419
420public struct ServerRefreshProvidersResult: Codable, Equatable, Sendable {
421 @ForwardCompatibleArray public var providers: [ServerProviderSnapshot]
422
423 public init(providers: [ServerProviderSnapshot]) {
424 self.providers = providers
425 }
426}