Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

ServerConfigModels.swift

256 lines9.0 KB

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?
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]?
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?
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 }
68​
69public struct ServerModelCapabilities: Codable, Equatable, Sendable {
70 public let optionDescriptors: [ServerProviderOptionDescriptor]?
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?
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?
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?
105​
106public struct ServerProviderSnapshot: Codable, Identifiable, Equatable, Sendable {
107 public var id: String { instanceId }
108​
109 public let instanceId: String
110 public let driver: String
111 public let displayName: String?
112 public let accentColor: String?
113 public let badgeLabel: String?
114 public let showInteractionModeToggle: Bool?
115 public let requiresNewThreadForModelChange: Bool?
116 public let enabled: Bool
117 public let installed: Bool
118 public let version: String?
119 public let status: String
120 public let auth: ServerProviderAuthSnapshot
121 public let checkedAt: String
122 public let message: String?
123 public let availability: String?
124 public let unavailableReason: String?
125 public let models: [ServerProviderModelSnapshot]
126 public let slashCommands: [ServerProviderSlashCommandSnapshot]?
127 public let skills: [ServerProviderSkillSnapshot]?
129​
130public enum ServerThreadEnvironmentMode: String, Codable, Equatable, Sendable {
131 case local
132 case worktree
134​
135public enum ServerProjectGroupingMode: String, Codable, Equatable, Sendable {
136 case repository
137 case repositoryPath = "repository_path"
138 case separate
140​
141/// New-thread preferences are server-authoritative, so every saved environment
142/// can resolve these differently even though they share one mobile client.
143public struct ServerSettingsSnapshot: Codable, Equatable, Sendable {
144 public let defaultThreadEnvMode: ServerThreadEnvironmentMode
145 public let newWorktreesStartFromOrigin: Bool
146 public let sidebarProjectGroupingMode: ServerProjectGroupingMode?
147 public let sidebarProjectGroupingOverrides: [String: ServerProjectGroupingMode]?
148​
149 public init(
150 defaultThreadEnvMode: ServerThreadEnvironmentMode = .local,
151 newWorktreesStartFromOrigin: Bool = true,
152 sidebarProjectGroupingMode: ServerProjectGroupingMode? = nil,
153 sidebarProjectGroupingOverrides: [String: ServerProjectGroupingMode]? = nil
154 ) {
155 self.defaultThreadEnvMode = defaultThreadEnvMode
156 self.newWorktreesStartFromOrigin = newWorktreesStartFromOrigin
157 self.sidebarProjectGroupingMode = sidebarProjectGroupingMode
158 self.sidebarProjectGroupingOverrides = sidebarProjectGroupingOverrides
159 }
161​
162/// Narrow decode view of the much larger `ServerConfig` RPC result.
163public struct ServerConfigSnapshot: Codable, Equatable, Sendable {
164 public let providers: [ServerProviderSnapshot]
165 public let settings: ServerSettingsSnapshot?
166 public let threadSnapshotPagination: Bool?
167​
168 public init(
169 providers: [ServerProviderSnapshot],
170 settings: ServerSettingsSnapshot? = nil,
171 threadSnapshotPagination: Bool? = nil
172 ) {
173 self.providers = providers
174 self.settings = settings
175 self.threadSnapshotPagination = threadSnapshotPagination
176 }
177​
178 private enum CodingKeys: String, CodingKey {
179 case providers, settings, threadSnapshotPagination
180 }
181​
182 public init(from decoder: any Decoder) throws {
183 let container = try decoder.container(keyedBy: CodingKeys.self)
184 providers = try container.decode(
185 [LossyDecodableElement<ServerProviderSnapshot>].self,
186 forKey: .providers
187 ).compactMap(\.value)
188 settings = try container.decodeIfPresent(ServerSettingsSnapshot.self, forKey: .settings)
189 threadSnapshotPagination = try container.decodeIfPresent(
190 Bool.self,
191 forKey: .threadSnapshotPagination
192 )
193 }
194​
195 public func encode(to encoder: any Encoder) throws {
196 var container = encoder.container(keyedBy: CodingKeys.self)
197 try container.encode(providers, forKey: .providers)
198 try container.encodeIfPresent(settings, forKey: .settings)
199 try container.encodeIfPresent(
200 threadSnapshotPagination,
201 forKey: .threadSnapshotPagination
202 )
203 }
205​
206private struct LossyDecodableElement<Value: Decodable>: Decodable {
207 let value: Value?
208​
209 init(from decoder: any Decoder) throws {
210 value = try? Value(from: decoder)
211 }
213​
214public enum ServerConfigStreamEvent: Decodable, Sendable {
215 case snapshot(ServerConfigSnapshot)
216 case providerStatuses([ServerProviderSnapshot])
217 case settingsUpdated(ServerSettingsSnapshot)
218 case unrelated(type: String)
219​
220 private enum CodingKeys: String, CodingKey { case type, config, payload }
221 private struct ProviderPayload: Decodable {
222 let providers: [ServerProviderSnapshot]
223​
224 private enum CodingKeys: String, CodingKey { case providers }
225​
226 init(from decoder: any Decoder) throws {
227 let container = try decoder.container(keyedBy: CodingKeys.self)
228 providers = try container.decode(
229 [LossyDecodableElement<ServerProviderSnapshot>].self,
230 forKey: .providers
231 ).compactMap(\.value)
232 }
233 }
234 private struct SettingsPayload: Decodable { let settings: ServerSettingsSnapshot }
235​
236 public init(from decoder: any Decoder) throws {
237 let container = try decoder.container(keyedBy: CodingKeys.self)
238 let type = try container.decode(String.self, forKey: .type)
239 switch type {
240 case "snapshot":
241 self = .snapshot(
242 try container.decode(ServerConfigSnapshot.self, forKey: .config)
243 )
244 case "providerStatuses":
245 self = .providerStatuses(
246 try container.decode(ProviderPayload.self, forKey: .payload).providers
247 )
248 case "settingsUpdated":
249 self = .settingsUpdated(
250 try container.decode(SettingsPayload.self, forKey: .payload).settings
251 )
252 default:
253 self = .unrelated(type: type)
254 }
255 }