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

Models.swift

708 lines24.8 KB

1import Foundation
2​
3/// Arrays sent by the server are allowed to grow new element variants before a
4/// mobile release catches up. Decode each element independently so one future
5/// project, thread, message, or activity cannot discard the rest of a snapshot.
6@propertyWrapper
7public struct ForwardCompatibleArray<Element>: Codable, Equatable, Sendable
8where Element: Codable & Equatable & Sendable {
9 public var wrappedValue: [Element]
10​
11 public init(wrappedValue: [Element]) {
12 self.wrappedValue = wrappedValue
13 }
14​
15 public init(from decoder: any Decoder) throws {
16 var container = try decoder.unkeyedContainer()
17 var values: [Element] = []
18 values.reserveCapacity(container.count ?? 0)
19 while !container.isAtEnd {
20 // `superDecoder` advances the unkeyed container even when the
21 // element itself is not understood by this client.
22 let elementDecoder = try container.superDecoder()
23 if let value = try? Element(from: elementDecoder) {
24 values.append(value)
25 }
26 }
27 wrappedValue = values
28 }
29​
30 public func encode(to encoder: any Encoder) throws {
31 var container = encoder.unkeyedContainer()
32 for value in wrappedValue {
33 try container.encode(value)
34 }
35 }
37​
38public enum EnvironmentKind: String, Codable, Sendable {
39 case bearer
40 case local
41 case managedDPoP = "managed-dpop"
43​
44public struct Environment: Codable, Identifiable, Equatable, Sendable {
45 public let id: String
46 public var label: String
47 public var httpBaseURL: URL
48 public var webSocketBaseURL: URL
49 public var kind: EnvironmentKind
50 public var descriptor: EnvironmentDescriptor?
51 public var isEnabled: Bool
52​
53 public init(
54 id: String,
55 label: String,
56 httpBaseURL: URL,
57 webSocketBaseURL: URL,
58 kind: EnvironmentKind = .bearer,
59 descriptor: EnvironmentDescriptor? = nil,
60 isEnabled: Bool = true
61 ) {
62 self.id = id
63 self.label = label
64 self.httpBaseURL = httpBaseURL
65 self.webSocketBaseURL = webSocketBaseURL
66 self.kind = kind
67 self.descriptor = descriptor
68 self.isEnabled = isEnabled
69 }
70​
71 private enum CodingKeys: String, CodingKey {
72 case id
73 case label
74 case httpBaseURL
75 case webSocketBaseURL
76 case kind
77 case descriptor
78 case isEnabled
79 }
80​
81 public init(from decoder: any Decoder) throws {
82 let container = try decoder.container(keyedBy: CodingKeys.self)
83 id = try container.decode(String.self, forKey: .id)
84 label = try container.decode(String.self, forKey: .label)
85 httpBaseURL = try container.decode(URL.self, forKey: .httpBaseURL)
86 webSocketBaseURL = try container.decode(URL.self, forKey: .webSocketBaseURL)
87 kind = try container.decodeIfPresent(EnvironmentKind.self, forKey: .kind) ?? .bearer
88 descriptor = try container.decodeIfPresent(EnvironmentDescriptor.self, forKey: .descriptor)
89 isEnabled = try container.decodeIfPresent(Bool.self, forKey: .isEnabled) ?? true
90 }
92​
93public struct EnvironmentDescriptor: Codable, Equatable, Sendable {
94 public struct Platform: Codable, Equatable, Sendable {
95 public let os: String
96 public let arch: String
97 public var machine: String? = nil
98 }
99​
100 public struct Capabilities: Codable, Equatable, Sendable {
101 public struct FileAttachments: Codable, Equatable, Sendable {
102 public let maxUploadBytes: Int
103 }
104​
105 public let repositoryIdentity: Bool
106 public let connectionProbe: Bool?
107 public let attachmentUploads: Bool?
108 public let fileAttachments: FileAttachments?
109 public let pullRequests: Bool?
110 public let threadSettlement: Bool?
111 public let threadAutoSettlement: Bool?
112 public let threadSnooze: Bool?
113 public let threadPinning: Bool?
114 public let threadTitleRegeneration: Bool?
115 public let threadPullRequestLinking: Bool?
116 public let serverSelfUpdate: String?
117 public let serverSelfUpdateProgress: Bool?
118 public var environmentIcon: Bool? = nil
119​
120 private enum CodingKeys: String, CodingKey {
121 case repositoryIdentity
122 case connectionProbe
123 case attachmentUploads
124 case fileAttachments
125 case pullRequests
126 case threadSettlement
127 case threadAutoSettlement
128 case threadSnooze
129 case threadPinning
130 case threadTitleRegeneration
131 case threadPullRequestLinking
132 case serverSelfUpdate
133 case serverSelfUpdateProgress
134 case environmentIcon
135 }
136​
137 public init(from decoder: any Decoder) throws {
138 let container = try decoder.container(keyedBy: CodingKeys.self)
139 environmentIcon = try container.decodeIfPresent(Bool.self, forKey: .environmentIcon)
140 repositoryIdentity =
141 try container.decodeIfPresent(Bool.self, forKey: .repositoryIdentity) ?? false
142 connectionProbe = try container.decodeIfPresent(Bool.self, forKey: .connectionProbe)
143 attachmentUploads = try container.decodeIfPresent(Bool.self, forKey: .attachmentUploads)
144 fileAttachments = try container.decodeIfPresent(
145 FileAttachments.self,
146 forKey: .fileAttachments
147 )
148 pullRequests = try container.decodeIfPresent(Bool.self, forKey: .pullRequests)
149 threadSettlement = try container.decodeIfPresent(Bool.self, forKey: .threadSettlement)
150 threadAutoSettlement = try container.decodeIfPresent(
151 Bool.self,
152 forKey: .threadAutoSettlement
153 )
154 threadSnooze = try container.decodeIfPresent(Bool.self, forKey: .threadSnooze)
155 threadPinning = try container.decodeIfPresent(Bool.self, forKey: .threadPinning)
156 threadTitleRegeneration = try container.decodeIfPresent(
157 Bool.self,
158 forKey: .threadTitleRegeneration
159 )
160 threadPullRequestLinking = try container.decodeIfPresent(
161 Bool.self,
162 forKey: .threadPullRequestLinking
163 )
164 serverSelfUpdate = try container.decodeIfPresent(String.self, forKey: .serverSelfUpdate)
165 serverSelfUpdateProgress = try container.decodeIfPresent(
166 Bool.self,
167 forKey: .serverSelfUpdateProgress
168 )
169 }
170 }
171​
172 public let environmentId: String
173 public let label: String
174 public let platform: Platform
175 public let serverVersion: String
176 public let capabilities: Capabilities
178​
179public struct ProviderUploadFeedbackResult: Codable, Equatable, Sendable {
180 public let feedbackId: String
182​
183public enum EnvironmentCredentialAuthorizationMethod: String, Codable, Sendable {
184 case bearer
185 case dpop
187​
188public struct EnvironmentCredential: Codable, Equatable, Sendable,
189 CustomStringConvertible, CustomDebugStringConvertible
191 public let accessToken: String
192 public let expiresAt: Date?
193 public let scopes: [String]
194 public let authorizationMethod: EnvironmentCredentialAuthorizationMethod
195 public let managedEnvironmentID: String?
196 public let proofKeyThumbprint: String?
197​
198 public init(accessToken: String, expiresAt: Date? = nil, scopes: [String] = []) {
199 self.accessToken = accessToken
200 self.expiresAt = expiresAt
201 self.scopes = scopes
202 authorizationMethod = .bearer
203 managedEnvironmentID = nil
204 proofKeyThumbprint = nil
205 }
206​
207 public static func managedDPoP(
208 accessToken: String,
209 expiresAt: Date,
210 scopes: [String],
211 environmentID: String,
212 proofKeyThumbprint: String
213 ) -> EnvironmentCredential {
214 EnvironmentCredential(
215 accessToken: accessToken,
216 expiresAt: expiresAt,
217 scopes: scopes,
218 authorizationMethod: .dpop,
219 managedEnvironmentID: environmentID,
220 proofKeyThumbprint: proofKeyThumbprint
221 )
222 }
223​
224 public var description: String {
225 "EnvironmentCredential(method: \(authorizationMethod.rawValue), token: <redacted>)"
226 }
227​
228 public var debugDescription: String { description }
229​
230 private init(
231 accessToken: String,
232 expiresAt: Date?,
233 scopes: [String],
234 authorizationMethod: EnvironmentCredentialAuthorizationMethod,
235 managedEnvironmentID: String?,
236 proofKeyThumbprint: String?
237 ) {
238 self.accessToken = accessToken
239 self.expiresAt = expiresAt
240 self.scopes = scopes
241 self.authorizationMethod = authorizationMethod
242 self.managedEnvironmentID = managedEnvironmentID
243 self.proofKeyThumbprint = proofKeyThumbprint
244 }
245​
246 private enum CodingKeys: String, CodingKey {
247 case accessToken
248 case expiresAt
249 case scopes
250 case authorizationMethod
251 case managedEnvironmentID
252 case proofKeyThumbprint
253 }
254​
255 public init(from decoder: any Decoder) throws {
256 let container = try decoder.container(keyedBy: CodingKeys.self)
257 accessToken = try container.decode(String.self, forKey: .accessToken)
258 expiresAt = try container.decodeIfPresent(Date.self, forKey: .expiresAt)
259 scopes = try container.decodeIfPresent([String].self, forKey: .scopes) ?? []
260 authorizationMethod = try container.decodeIfPresent(
261 EnvironmentCredentialAuthorizationMethod.self,
262 forKey: .authorizationMethod
263 ) ?? .bearer
264 managedEnvironmentID = try container.decodeIfPresent(
265 String.self,
266 forKey: .managedEnvironmentID
267 )
268 proofKeyThumbprint = try container.decodeIfPresent(
269 String.self,
270 forKey: .proofKeyThumbprint
271 )
272​
273 if authorizationMethod == .dpop {
274 guard expiresAt != nil,
275 managedEnvironmentID?.isEmpty == false,
276 proofKeyThumbprint?.isEmpty == false else {
277 throw DecodingError.dataCorruptedError(
278 forKey: .authorizationMethod,
279 in: container,
280 debugDescription: "A DPoP credential is missing its binding metadata."
281 )
282 }
283 }
284 }
285​
286 public func encode(to encoder: any Encoder) throws {
287 var container = encoder.container(keyedBy: CodingKeys.self)
288 try container.encode(accessToken, forKey: .accessToken)
289 try container.encodeIfPresent(expiresAt, forKey: .expiresAt)
290 try container.encode(scopes, forKey: .scopes)
291 try container.encode(authorizationMethod, forKey: .authorizationMethod)
292 try container.encodeIfPresent(managedEnvironmentID, forKey: .managedEnvironmentID)
293 try container.encodeIfPresent(proofKeyThumbprint, forKey: .proofKeyThumbprint)
294 }
296​
297public struct ModelSelection: Codable, Equatable, Sendable {
298 public struct OptionSelection: Codable, Equatable, Sendable {
299 public let id: String
300 public let value: JSONValue
301​
302 public init(id: String, value: JSONValue) {
303 self.id = id
304 self.value = value
305 }
306 }
307​
308 public let instanceId: String
309 public let model: String
310 public let options: [OptionSelection]?
311​
312 public init(instanceId: String, model: String, options: [OptionSelection]? = nil) {
313 self.instanceId = instanceId
314 self.model = model
315 self.options = options
316 }
317​
318 private enum CodingKeys: String, CodingKey {
319 case instanceId, provider, model, options
320 }
321​
322 public init(from decoder: any Decoder) throws {
323 let container = try decoder.container(keyedBy: CodingKeys.self)
324 instanceId = try container.decodeIfPresent(String.self, forKey: .instanceId)
325 ?? container.decode(String.self, forKey: .provider)
326 model = try container.decode(String.self, forKey: .model)
327 if let canonical = try? container.decode([OptionSelection].self, forKey: .options) {
328 options = canonical
329 } else if let legacy = try? container.decode(
330 [String: JSONValue].self,
331 forKey: .options
332 ) {
333 options = legacy.keys.sorted().map { OptionSelection(id: $0, value: legacy[$0]!) }
334 } else {
335 options = nil
336 }
337 }
338​
339 public func encode(to encoder: any Encoder) throws {
340 var container = encoder.container(keyedBy: CodingKeys.self)
341 try container.encode(instanceId, forKey: .instanceId)
342 try container.encode(model, forKey: .model)
343 try container.encodeIfPresent(options, forKey: .options)
344 }
346​
347public struct RepositoryIdentity: Codable, Equatable, Sendable {
348 public struct Locator: Codable, Equatable, Sendable {
349 public let source: String
350 public let remoteName: String
351 public let remoteUrl: String
352 }
353​
354 public let canonicalKey: String
355 public let locator: Locator
356 public let rootPath: String?
357 public let displayName: String?
358 public let provider: String?
359 public let owner: String?
360 public let name: String?
362​
363public struct ProjectScript: Codable, Identifiable, Equatable, Sendable {
364 public let id: String
365 public let name: String
366 public let command: String
367 public let icon: String
368 public let runOnWorktreeCreate: Bool
369 public let previewUrl: String?
370 public let autoOpenPreview: Bool?
372​
373public struct OrchestrationProject: Codable, Identifiable, Equatable, Sendable {
374 public let id: String
375 public let title: String
376 public let workspaceRoot: String
377 public let repositoryIdentity: RepositoryIdentity?
378 public let defaultModelSelection: ModelSelection?
379 public let scripts: [ProjectScript]
380 public let createdAt: String
381 public let updatedAt: String
382 public let deletedAt: String?
383 public var projectIcon: ProjectIconOverride? = nil
385​
386public struct ProjectIconOverride: Codable, Equatable, Hashable, Sendable {
387 public let kind: String
388 public var name: String? = nil
389 public var color: String? = nil
390 public var emoji: String? = nil
392​
393public enum RuntimeMode: String, Codable, CaseIterable, Sendable {
394 case approvalRequired = "approval-required"
395 case autoAcceptEdits = "auto-accept-edits"
396 case auto
397 case fullAccess = "full-access"
399​
400public enum InteractionMode: String, Codable, CaseIterable, Sendable {
401 case `default`
402 case plan
404​
405public struct OrchestrationLatestTurn: Codable, Equatable, Sendable {
406 public let turnId: String
407 public let state: String
408 public let requestedAt: String
409 public let startedAt: String?
410 public let completedAt: String?
411 public let assistantMessageId: String?
413​
414public struct OrchestrationSession: Codable, Equatable, Sendable {
415 public let threadId: String
416 public let status: String
417 public let providerName: String?
418 public let providerInstanceId: String?
419 public let runtimeMode: RuntimeMode
420 public let activeTurnId: String?
421 public let lastError: String?
422 public let updatedAt: String
424​
425public enum OrchestrationBackgroundLiveness: String, Codable, Equatable, Sendable {
426 case working
427 case monitoring
429​
430public struct ThreadLinkedPullRequest: Codable, Equatable, Hashable, Sendable {
431 public let projectId: String
432 public let repository: String
433 public let number: Int
434 public let url: String
435​
436 public init(projectId: String, repository: String, number: Int, url: String) {
437 self.projectId = projectId
438 self.repository = repository
439 self.number = number
440 self.url = url
441 }
443​
444public struct OrchestrationThreadShell: Codable, Identifiable, Equatable, Sendable {
445 public let id: String
446 public let projectId: String
447 public let title: String
448 public let modelSelection: ModelSelection
449 public let runtimeMode: RuntimeMode
450 public let interactionMode: InteractionMode
451 public let branch: String?
452 public let worktreePath: String?
453 public var linkedPullRequest: ThreadLinkedPullRequest? = nil
454 public let latestTurn: OrchestrationLatestTurn?
455 public let createdAt: String
456 public let updatedAt: String
457 public let archivedAt: String?
458 public let settledOverride: String?
459 public let settledAt: String?
460 public var unsettledAt: String? = nil
461 public let snoozedUntil: String?
462 public let snoozedAt: String?
463 public let pinnedAt: String?
464 public let session: OrchestrationSession?
465 public let latestUserMessageAt: String?
466 public let hasPendingApprovals: Bool
467 public let hasPendingUserInput: Bool
468 public let hasActionableProposedPlan: Bool
469 public let backgroundLiveness: OrchestrationBackgroundLiveness?
471​
472public struct OrchestrationMessage: Codable, Identifiable, Equatable, Sendable {
473 public let id: String
474 public let role: String
475 public let text: String
476 public let attachments: [ChatAttachment]?
477 public let turnId: String?
478 public let streaming: Bool
479 public let createdAt: String
480 public let updatedAt: String
482​
483public struct ChatAttachment: Codable, Identifiable, Equatable, Sendable {
484 public let type: String
485 public let id: String
486 public let name: String
487 public let mimeType: String
488 public let sizeBytes: Int
490​
491public struct OrchestrationActivity: Codable, Identifiable, Equatable, Sendable {
492 public let id: String
493 public let tone: String
494 public let kind: String
495 public let summary: String
496 public let payload: JSONValue
497 public let turnId: String?
498 public let sequence: Int?
499 public let createdAt: String
501​
502public struct CheckpointFile: Codable, Equatable, Sendable {
503 public let path: String
504 public let kind: String
505 public let additions: Int
506 public let deletions: Int
508​
509public struct CheckpointSummary: Codable, Equatable, Sendable {
510 public let turnId: String
511 public let checkpointTurnCount: Int
512 public let checkpointRef: String
513 public let status: String
514 public let files: [CheckpointFile]
515 public let assistantMessageId: String?
516 public let completedAt: String
518​
519public struct OrchestrationThread: Codable, Identifiable, Equatable, Sendable {
520 public let id: String
521 public let projectId: String
522 public let title: String
523 public let modelSelection: ModelSelection
524 public let runtimeMode: RuntimeMode
525 public let interactionMode: InteractionMode
526 public let branch: String?
527 public let worktreePath: String?
528 public var linkedPullRequest: ThreadLinkedPullRequest? = nil
529 public let latestTurn: OrchestrationLatestTurn?
530 public let createdAt: String
531 public let updatedAt: String
532 public let archivedAt: String?
533 public let settledOverride: String?
534 public let settledAt: String?
535 public var unsettledAt: String? = nil
536 public let snoozedUntil: String?
537 public let snoozedAt: String?
538 public let pinnedAt: String?
539 public let deletedAt: String?
540 @ForwardCompatibleArray public var messages: [OrchestrationMessage]
541 @ForwardCompatibleArray public var activities: [OrchestrationActivity]
542 @ForwardCompatibleArray public var checkpoints: [CheckpointSummary]
543 public let session: OrchestrationSession?
545​
546public struct OrchestrationShellSnapshot: Codable, Equatable, Sendable {
547 public let snapshotSequence: Int
548 @ForwardCompatibleArray public var projects: [OrchestrationProject]
549 @ForwardCompatibleArray public var threads: [OrchestrationThreadShell]
550 public let updatedAt: String
552​
553public struct OrchestrationReadModel: Codable, Equatable, Sendable {
554 public let snapshotSequence: Int
555 @ForwardCompatibleArray public var projects: [OrchestrationProject]
556 @ForwardCompatibleArray public var threads: [OrchestrationThread]
557 public let updatedAt: String
559​
560public struct OrchestrationThreadDetailSnapshot: Codable, Equatable, Sendable {
561 public let snapshotSequence: Int
562 public let thread: OrchestrationThread
563 public let page: OrchestrationThreadDetailPage?
564​
565 public init(
566 snapshotSequence: Int,
567 thread: OrchestrationThread,
568 page: OrchestrationThreadDetailPage? = nil
569 ) {
570 self.snapshotSequence = snapshotSequence
571 self.thread = thread
572 self.page = page
573 }
575​
576public struct OrchestrationThreadDetailPage: Codable, Equatable, Sendable {
577 public let beforeCursor: String?
578 public let hasMore: Bool
579 public let snapshotSequence: Int
580 public let threadSequence: Int?
581​
582 public init(
583 beforeCursor: String?,
584 hasMore: Bool,
585 snapshotSequence: Int,
586 threadSequence: Int? = nil
587 ) {
588 self.beforeCursor = beforeCursor
589 self.hasMore = hasMore
590 self.snapshotSequence = snapshotSequence
591 self.threadSequence = threadSequence
592 }
594​
595public enum ShellStreamItem: Decodable, Sendable {
596 case synchronized
597 case snapshot(OrchestrationShellSnapshot)
598 case projectUpserted(sequence: Int, project: OrchestrationProject)
599 case projectRemoved(sequence: Int, projectID: String)
600 case threadUpserted(sequence: Int, thread: OrchestrationThreadShell)
601 case threadRemoved(sequence: Int, threadID: String)
602 /// A newer server emitted a delta this build cannot reduce. The live client
603 /// should fetch an authoritative shell snapshot and keep the stream alive.
604 case refreshRequired
605​
606 private enum CodingKeys: String, CodingKey {
607 case kind, sequence, snapshot, project, projectId, thread, threadId
608 }
609​
610 public init(from decoder: any Decoder) throws {
611 let container = try decoder.container(keyedBy: CodingKeys.self)
612 let kind = try container.decode(String.self, forKey: .kind)
613 switch kind {
614 case "synchronized":
615 self = .synchronized
616 case "snapshot":
617 guard let snapshot = try? container.decode(
618 OrchestrationShellSnapshot.self,
619 forKey: .snapshot
620 ) else {
621 self = .refreshRequired
622 return
623 }
624 self = .snapshot(snapshot)
625 case "project-upserted":
626 guard let sequence = try? container.decode(Int.self, forKey: .sequence),
627 let project = try? container.decode(
628 OrchestrationProject.self,
629 forKey: .project
630 ) else {
631 self = .refreshRequired
632 return
633 }
634 self = .projectUpserted(
635 sequence: sequence,
636 project: project
637 )
638 case "project-removed":
639 guard let sequence = try? container.decode(Int.self, forKey: .sequence),
640 let projectID = try? container.decode(String.self, forKey: .projectId) else {
641 self = .refreshRequired
642 return
643 }
644 self = .projectRemoved(
645 sequence: sequence,
646 projectID: projectID
647 )
648 case "thread-upserted":
649 guard let sequence = try? container.decode(Int.self, forKey: .sequence),
650 let thread = try? container.decode(
651 OrchestrationThreadShell.self,
652 forKey: .thread
653 ) else {
654 self = .refreshRequired
655 return
656 }
657 self = .threadUpserted(
658 sequence: sequence,
659 thread: thread
660 )
661 case "thread-removed":
662 guard let sequence = try? container.decode(Int.self, forKey: .sequence),
663 let threadID = try? container.decode(String.self, forKey: .threadId) else {
664 self = .refreshRequired
665 return
666 }
667 self = .threadRemoved(
668 sequence: sequence,
669 threadID: threadID
670 )
671 default:
672 self = .refreshRequired
673 }
674 }
676​
677public enum ThreadStreamItem: Decodable, Sendable {
678 case synchronized
679 case snapshot(OrchestrationThreadDetailSnapshot)
680 case event(JSONValue)
681​
682 private enum CodingKeys: String, CodingKey { case kind, snapshot, event }
683​
684 public init(from decoder: any Decoder) throws {
685 let container = try decoder.container(keyedBy: CodingKeys.self)
686 let kind = try container.decode(String.self, forKey: .kind)
687 switch kind {
688 case "synchronized":
689 self = .synchronized
690 case "snapshot":
691 guard let snapshot = try? container.decode(
692 OrchestrationThreadDetailSnapshot.self,
693 forKey: .snapshot
694 ) else {
695 self = .event(.null)
696 return
697 }
698 self = .snapshot(snapshot)
699 case "event":
700 self = .event((try? container.decode(JSONValue.self, forKey: .event)) ?? .null)
701 default:
702 // The detail reducer already treats an unrecognized event as an
703 // authoritative-refresh request. Reuse that path without adding a
704 // second stream state or terminating the subscription.
705 self = .event(.null)
706 }
707 }