Open History to see the last commit
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 }
36}
37
38public enum EnvironmentKind: String, Codable, Sendable {
39 case bearer
40 case local
41 case managedDPoP = "managed-dpop"
42}
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 }
91}
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 var threadRestartContinuation: Bool? = nil
113 public let threadSnooze: Bool?
114 public let threadPinning: Bool?
115 public let threadPinReorder: Bool?
116 public let threadActiveReorder: Bool?
117 public let threadTitleRegeneration: Bool?
118 public let threadPullRequestLinking: Bool?
119 public var threadPullRequests: Bool? = nil
120 public var pullRequestStackActions: Bool? = nil
121 public let serverSelfUpdate: String?
122 public let serverSelfUpdateProgress: Bool?
123 public var environmentIcon: Bool? = nil
124 public var usageLimitSources: Bool? = nil
125 public var questionAttachments: Bool? = nil
126 public var projectSettingsOverrides: Bool? = nil
127 public var inlineMessageContext: Bool? = nil
128
129 private enum CodingKeys: String, CodingKey {
130 case repositoryIdentity
131 case connectionProbe
132 case attachmentUploads
133 case fileAttachments
134 case pullRequests
135 case threadSettlement
136 case threadAutoSettlement
137 case threadRestartContinuation
138 case threadSnooze
139 case threadPinning
140 case threadPinReorder
141 case threadActiveReorder
142 case threadTitleRegeneration
143 case threadPullRequestLinking
144 case threadPullRequests
145 case pullRequestStackActions
146 case serverSelfUpdate
147 case serverSelfUpdateProgress
148 case environmentIcon
149 case usageLimitSources
150 case questionAttachments
151 case projectSettingsOverrides
152 case inlineMessageContext
153 }
154
155 public init(from decoder: any Decoder) throws {
156 let container = try decoder.container(keyedBy: CodingKeys.self)
157 environmentIcon = try container.decodeIfPresent(Bool.self, forKey: .environmentIcon)
158 usageLimitSources = try container.decodeIfPresent(Bool.self, forKey: .usageLimitSources)
159 questionAttachments = try container.decodeIfPresent(Bool.self, forKey: .questionAttachments)
160 projectSettingsOverrides = try container.decodeIfPresent(Bool.self, forKey: .projectSettingsOverrides)
161 inlineMessageContext = try container.decodeIfPresent(Bool.self, forKey: .inlineMessageContext)
162 repositoryIdentity =
163 try container.decodeIfPresent(Bool.self, forKey: .repositoryIdentity) ?? false
164 connectionProbe = try container.decodeIfPresent(Bool.self, forKey: .connectionProbe)
165 attachmentUploads = try container.decodeIfPresent(Bool.self, forKey: .attachmentUploads)
166 fileAttachments = try container.decodeIfPresent(
167 FileAttachments.self,
168 forKey: .fileAttachments
169 )
170 pullRequests = try container.decodeIfPresent(Bool.self, forKey: .pullRequests)
171 threadSettlement = try container.decodeIfPresent(Bool.self, forKey: .threadSettlement)
172 threadAutoSettlement = try container.decodeIfPresent(
173 Bool.self,
174 forKey: .threadAutoSettlement
175 )
176 threadRestartContinuation = try container.decodeIfPresent(
177 Bool.self,
178 forKey: .threadRestartContinuation
179 )
180 threadSnooze = try container.decodeIfPresent(Bool.self, forKey: .threadSnooze)
181 threadPinning = try container.decodeIfPresent(Bool.self, forKey: .threadPinning)
182 threadPinReorder = try container.decodeIfPresent(Bool.self, forKey: .threadPinReorder)
183 threadActiveReorder = try container.decodeIfPresent(
184 Bool.self,
185 forKey: .threadActiveReorder
186 )
187 threadTitleRegeneration = try container.decodeIfPresent(
188 Bool.self,
189 forKey: .threadTitleRegeneration
190 )
191 threadPullRequestLinking = try container.decodeIfPresent(
192 Bool.self,
193 forKey: .threadPullRequestLinking
194 )
195 threadPullRequests = try container.decodeIfPresent(Bool.self, forKey: .threadPullRequests)
196 pullRequestStackActions = try container.decodeIfPresent(Bool.self, forKey: .pullRequestStackActions)
197 serverSelfUpdate = try container.decodeIfPresent(String.self, forKey: .serverSelfUpdate)
198 serverSelfUpdateProgress = try container.decodeIfPresent(
199 Bool.self,
200 forKey: .serverSelfUpdateProgress
201 )
202 }
203 }
204
205 public let environmentId: String
206 public let label: String
207 public let platform: Platform
208 public let serverVersion: String
209 public let capabilities: Capabilities
210}
211
212public struct ProviderUploadFeedbackResult: Codable, Equatable, Sendable {
213 public let feedbackId: String
214}
215
216public enum EnvironmentCredentialAuthorizationMethod: String, Codable, Sendable {
217 case bearer
218 case dpop
219}
220
221public struct EnvironmentCredential: Codable, Equatable, Sendable,
222 CustomStringConvertible, CustomDebugStringConvertible
223{
224 public let accessToken: String
225 public let expiresAt: Date?
226 public let scopes: [String]
227 public let authorizationMethod: EnvironmentCredentialAuthorizationMethod
228 public let managedEnvironmentID: String?
229 public let proofKeyThumbprint: String?
230
231 public init(accessToken: String, expiresAt: Date? = nil, scopes: [String] = []) {
232 self.accessToken = accessToken
233 self.expiresAt = expiresAt
234 self.scopes = scopes
235 authorizationMethod = .bearer
236 managedEnvironmentID = nil
237 proofKeyThumbprint = nil
238 }
239
240 public static func managedDPoP(
241 accessToken: String,
242 expiresAt: Date,
243 scopes: [String],
244 environmentID: String,
245 proofKeyThumbprint: String
246 ) -> EnvironmentCredential {
247 EnvironmentCredential(
248 accessToken: accessToken,
249 expiresAt: expiresAt,
250 scopes: scopes,
251 authorizationMethod: .dpop,
252 managedEnvironmentID: environmentID,
253 proofKeyThumbprint: proofKeyThumbprint
254 )
255 }
256
257 public var description: String {
258 "EnvironmentCredential(method: \(authorizationMethod.rawValue), token: <redacted>)"
259 }
260
261 public var debugDescription: String { description }
262
263 private init(
264 accessToken: String,
265 expiresAt: Date?,
266 scopes: [String],
267 authorizationMethod: EnvironmentCredentialAuthorizationMethod,
268 managedEnvironmentID: String?,
269 proofKeyThumbprint: String?
270 ) {
271 self.accessToken = accessToken
272 self.expiresAt = expiresAt
273 self.scopes = scopes
274 self.authorizationMethod = authorizationMethod
275 self.managedEnvironmentID = managedEnvironmentID
276 self.proofKeyThumbprint = proofKeyThumbprint
277 }
278
279 private enum CodingKeys: String, CodingKey {
280 case accessToken
281 case expiresAt
282 case scopes
283 case authorizationMethod
284 case managedEnvironmentID
285 case proofKeyThumbprint
286 }
287
288 public init(from decoder: any Decoder) throws {
289 let container = try decoder.container(keyedBy: CodingKeys.self)
290 accessToken = try container.decode(String.self, forKey: .accessToken)
291 expiresAt = try container.decodeIfPresent(Date.self, forKey: .expiresAt)
292 scopes = try container.decodeIfPresent([String].self, forKey: .scopes) ?? []
293 authorizationMethod = try container.decodeIfPresent(
294 EnvironmentCredentialAuthorizationMethod.self,
295 forKey: .authorizationMethod
296 ) ?? .bearer
297 managedEnvironmentID = try container.decodeIfPresent(
298 String.self,
299 forKey: .managedEnvironmentID
300 )
301 proofKeyThumbprint = try container.decodeIfPresent(
302 String.self,
303 forKey: .proofKeyThumbprint
304 )
305
306 if authorizationMethod == .dpop {
307 guard expiresAt != nil,
308 managedEnvironmentID?.isEmpty == false,
309 proofKeyThumbprint?.isEmpty == false else {
310 throw DecodingError.dataCorruptedError(
311 forKey: .authorizationMethod,
312 in: container,
313 debugDescription: "A DPoP credential is missing its binding metadata."
314 )
315 }
316 }
317 }
318
319 public func encode(to encoder: any Encoder) throws {
320 var container = encoder.container(keyedBy: CodingKeys.self)
321 try container.encode(accessToken, forKey: .accessToken)
322 try container.encodeIfPresent(expiresAt, forKey: .expiresAt)
323 try container.encode(scopes, forKey: .scopes)
324 try container.encode(authorizationMethod, forKey: .authorizationMethod)
325 try container.encodeIfPresent(managedEnvironmentID, forKey: .managedEnvironmentID)
326 try container.encodeIfPresent(proofKeyThumbprint, forKey: .proofKeyThumbprint)
327 }
328}
329
330public struct ModelSelection: Codable, Equatable, Sendable {
331 public struct OptionSelection: Codable, Equatable, Sendable {
332 public let id: String
333 public let value: JSONValue
334
335 public init(id: String, value: JSONValue) {
336 self.id = id
337 self.value = value
338 }
339 }
340
341 public let instanceId: String
342 public let model: String
343 public let options: [OptionSelection]?
344
345 public init(instanceId: String, model: String, options: [OptionSelection]? = nil) {
346 self.instanceId = instanceId
347 self.model = model
348 self.options = options
349 }
350
351 private enum CodingKeys: String, CodingKey {
352 case instanceId, provider, model, options
353 }
354
355 public init(from decoder: any Decoder) throws {
356 let container = try decoder.container(keyedBy: CodingKeys.self)
357 instanceId = try container.decodeIfPresent(String.self, forKey: .instanceId)
358 ?? container.decode(String.self, forKey: .provider)
359 model = try container.decode(String.self, forKey: .model)
360 if let canonical = try? container.decode([OptionSelection].self, forKey: .options) {
361 options = canonical
362 } else if let legacy = try? container.decode(
363 [String: JSONValue].self,
364 forKey: .options
365 ) {
366 options = legacy.keys.sorted().map { OptionSelection(id: $0, value: legacy[$0]!) }
367 } else {
368 options = nil
369 }
370 }
371
372 public func encode(to encoder: any Encoder) throws {
373 var container = encoder.container(keyedBy: CodingKeys.self)
374 try container.encode(instanceId, forKey: .instanceId)
375 try container.encode(model, forKey: .model)
376 try container.encodeIfPresent(options, forKey: .options)
377 }
378}
379
380public struct RepositoryIdentity: Codable, Equatable, Sendable {
381 public struct Locator: Codable, Equatable, Sendable {
382 public let source: String
383 public let remoteName: String
384 public let remoteUrl: String
385 }
386
387 public let canonicalKey: String
388 public let locator: Locator
389 public let rootPath: String?
390 public let displayName: String?
391 public let provider: String?
392 public let owner: String?
393 public let name: String?
394}
395
396public struct ProjectScript: Codable, Identifiable, Equatable, Sendable {
397 public let id: String
398 public let name: String
399 public let command: String
400 public let icon: String
401 public let runOnWorktreeCreate: Bool
402 public let previewUrl: String?
403 public let autoOpenPreview: Bool?
404}
405
406public struct OrchestrationProject: Codable, Identifiable, Equatable, Sendable {
407 public let id: String
408 public let title: String
409 public let workspaceRoot: String
410 public let repositoryIdentity: RepositoryIdentity?
411 public let defaultModelSelection: ModelSelection?
412 public let scripts: [ProjectScript]
413 public let createdAt: String
414 public let updatedAt: String
415 public let deletedAt: String?
416 public var projectIcon: ProjectIconOverride? = nil
417 public var defaultThreadEnvMode: ServerThreadEnvironmentMode? = nil
418}
419
420public struct ProjectIconOverride: Codable, Equatable, Hashable, Sendable {
421 public let kind: String
422 public var name: String? = nil
423 public var color: String? = nil
424 public var emoji: String? = nil
425}
426
427public enum RuntimeMode: String, Codable, CaseIterable, Sendable {
428 case approvalRequired = "approval-required"
429 case autoAcceptEdits = "auto-accept-edits"
430 case auto
431 case fullAccess = "full-access"
432}
433
434public enum InteractionMode: String, Codable, CaseIterable, Sendable {
435 case `default`
436 case plan
437}
438
439public struct OrchestrationLatestTurn: Codable, Equatable, Sendable {
440 public let turnId: String
441 public let state: String
442 public let requestedAt: String
443 public let startedAt: String?
444 public let completedAt: String?
445 public let assistantMessageId: String?
446}
447
448public struct OrchestrationSession: Codable, Equatable, Sendable {
449 public let threadId: String
450 public let status: String
451 public let providerName: String?
452 public let providerInstanceId: String?
453 public let runtimeMode: RuntimeMode
454 public let activeTurnId: String?
455 public let lastError: String?
456 public let updatedAt: String
457}
458
459public enum OrchestrationBackgroundLiveness: String, Codable, Equatable, Sendable {
460 case working
461 case monitoring
462}
463
464public struct ThreadLinkedPullRequest: Codable, Equatable, Hashable, Sendable {
465 public let projectId: String
466 public let repository: String
467 public let number: Int
468 public let url: String
469
470 public init(projectId: String, repository: String, number: Int, url: String) {
471 self.projectId = projectId
472 self.repository = repository
473 self.number = number
474 self.url = url
475 }
476}
477
478/// Present on a thread while the server is generating a new title for it.
479/// Cleared by the server when the regeneration completes or the thread is renamed.
480public struct ThreadTitleRegeneration: Codable, Equatable, Sendable {
481 public let requestId: String
482 public let startedAt: String
483}
484
485public struct OrchestrationThreadShell: Codable, Identifiable, Equatable, Sendable {
486 public let id: String
487 public let projectId: String
488 public let title: String
489 public let modelSelection: ModelSelection
490 public let runtimeMode: RuntimeMode
491 public let interactionMode: InteractionMode
492 public let branch: String?
493 public let worktreePath: String?
494 public var linkedPullRequest: ThreadLinkedPullRequest? = nil
495 public var pullRequests: [ThreadPullRequestLink]? = nil
496 public var branchPullRequest: ThreadLinkedPullRequest? = nil
497 public let latestTurn: OrchestrationLatestTurn?
498 public let createdAt: String
499 public let updatedAt: String
500 public let archivedAt: String?
501 public let settledOverride: String?
502 public let settledAt: String?
503 public var unsettledAt: String? = nil
504 public var activeOrderKey: String? = nil
505 public let snoozedUntil: String?
506 public let snoozedAt: String?
507 public let pinnedAt: String?
508 public var pinOrderKey: String? = nil
509 public var titleRegeneration: ThreadTitleRegeneration? = nil
510 public let session: OrchestrationSession?
511 public let latestUserMessageAt: String?
512 public let hasPendingApprovals: Bool
513 public let hasPendingUserInput: Bool
514 public let hasActionableProposedPlan: Bool
515 public let backgroundLiveness: OrchestrationBackgroundLiveness?
516}
517
518public struct OrchestrationMessage: Codable, Identifiable, Equatable, Sendable {
519 public let id: String
520 public let role: String
521 public let text: String
522 public let attachments: [ChatAttachment]?
523 public let turnId: String?
524 public let streaming: Bool
525 public let createdAt: String
526 public let updatedAt: String
527 public var context: OrchestrationMessageContext? = nil
528}
529
530public struct ChatAttachment: Codable, Identifiable, Equatable, Sendable {
531 public let type: String
532 public let id: String
533 public let name: String
534 public let mimeType: String
535 public let sizeBytes: Int
536 public var source: JSONValue? = nil
537}
538
539public struct OrchestrationActivity: Codable, Identifiable, Equatable, Sendable {
540 public let id: String
541 public let tone: String
542 public let kind: String
543 public let summary: String
544 public let payload: JSONValue
545 public let turnId: String?
546 public let sequence: Int?
547 public let createdAt: String
548}
549
550public struct CheckpointFile: Codable, Equatable, Sendable {
551 public let path: String
552 public let kind: String
553 public let additions: Int
554 public let deletions: Int
555}
556
557public struct CheckpointSummary: Codable, Equatable, Sendable {
558 public let turnId: String
559 public let checkpointTurnCount: Int
560 public let checkpointRef: String
561 public let status: String
562 public let files: [CheckpointFile]
563 public let assistantMessageId: String?
564 public let completedAt: String
565}
566
567public struct OrchestrationThread: Codable, Identifiable, Equatable, Sendable {
568 public let id: String
569 public let projectId: String
570 public let title: String
571 public let modelSelection: ModelSelection
572 public let runtimeMode: RuntimeMode
573 public let interactionMode: InteractionMode
574 public let branch: String?
575 public let worktreePath: String?
576 public var linkedPullRequest: ThreadLinkedPullRequest? = nil
577 public var pullRequests: [ThreadPullRequestLink]? = nil
578 public var branchPullRequest: ThreadLinkedPullRequest? = nil
579 public let latestTurn: OrchestrationLatestTurn?
580 public let createdAt: String
581 public let updatedAt: String
582 public let archivedAt: String?
583 public let settledOverride: String?
584 public let settledAt: String?
585 public var unsettledAt: String? = nil
586 public var activeOrderKey: String? = nil
587 public let snoozedUntil: String?
588 public let snoozedAt: String?
589 public let pinnedAt: String?
590 public var pinOrderKey: String? = nil
591 public var titleRegeneration: ThreadTitleRegeneration? = nil
592 public let deletedAt: String?
593 @ForwardCompatibleArray public var messages: [OrchestrationMessage]
594 @ForwardCompatibleArray public var activities: [OrchestrationActivity]
595 @ForwardCompatibleArray public var checkpoints: [CheckpointSummary]
596 public let session: OrchestrationSession?
597}
598
599public struct OrchestrationShellSnapshot: Codable, Equatable, Sendable {
600 public let snapshotSequence: Int
601 @ForwardCompatibleArray public var projects: [OrchestrationProject]
602 @ForwardCompatibleArray public var threads: [OrchestrationThreadShell]
603 public let updatedAt: String
604}
605
606public struct OrchestrationReadModel: Codable, Equatable, Sendable {
607 public let snapshotSequence: Int
608 @ForwardCompatibleArray public var projects: [OrchestrationProject]
609 @ForwardCompatibleArray public var threads: [OrchestrationThread]
610 public let updatedAt: String
611}
612
613public struct OrchestrationThreadDetailSnapshot: Codable, Equatable, Sendable {
614 public let snapshotSequence: Int
615 public let thread: OrchestrationThread
616 public let page: OrchestrationThreadDetailPage?
617
618 public init(
619 snapshotSequence: Int,
620 thread: OrchestrationThread,
621 page: OrchestrationThreadDetailPage? = nil
622 ) {
623 self.snapshotSequence = snapshotSequence
624 self.thread = thread
625 self.page = page
626 }
627}
628
629public struct OrchestrationThreadDetailPage: Codable, Equatable, Sendable {
630 public let beforeCursor: String?
631 public let hasMore: Bool
632 public let snapshotSequence: Int
633 public let threadSequence: Int?
634
635 public init(
636 beforeCursor: String?,
637 hasMore: Bool,
638 snapshotSequence: Int,
639 threadSequence: Int? = nil
640 ) {
641 self.beforeCursor = beforeCursor
642 self.hasMore = hasMore
643 self.snapshotSequence = snapshotSequence
644 self.threadSequence = threadSequence
645 }
646}
647
648public enum ShellStreamItem: Decodable, Sendable {
649 case synchronized
650 case snapshot(OrchestrationShellSnapshot)
651 case projectUpserted(sequence: Int, project: OrchestrationProject)
652 case projectRemoved(sequence: Int, projectID: String)
653 case threadUpserted(sequence: Int, thread: OrchestrationThreadShell)
654 case threadRemoved(sequence: Int, threadID: String)
655 /// A newer server emitted a delta this build cannot reduce. The live client
656 /// should fetch an authoritative shell snapshot and keep the stream alive.
657 case refreshRequired
658
659 private enum CodingKeys: String, CodingKey {
660 case kind, sequence, snapshot, project, projectId, thread, threadId
661 }
662
663 public init(from decoder: any Decoder) throws {
664 let container = try decoder.container(keyedBy: CodingKeys.self)
665 let kind = try container.decode(String.self, forKey: .kind)
666 switch kind {
667 case "synchronized":
668 self = .synchronized
669 case "snapshot":
670 guard let snapshot = try? container.decode(
671 OrchestrationShellSnapshot.self,
672 forKey: .snapshot
673 ) else {
674 self = .refreshRequired
675 return
676 }
677 self = .snapshot(snapshot)
678 case "project-upserted":
679 guard let sequence = try? container.decode(Int.self, forKey: .sequence),
680 let project = try? container.decode(
681 OrchestrationProject.self,
682 forKey: .project
683 ) else {
684 self = .refreshRequired
685 return
686 }
687 self = .projectUpserted(
688 sequence: sequence,
689 project: project
690 )
691 case "project-removed":
692 guard let sequence = try? container.decode(Int.self, forKey: .sequence),
693 let projectID = try? container.decode(String.self, forKey: .projectId) else {
694 self = .refreshRequired
695 return
696 }
697 self = .projectRemoved(
698 sequence: sequence,
699 projectID: projectID
700 )
701 case "thread-upserted":
702 guard let sequence = try? container.decode(Int.self, forKey: .sequence),
703 let thread = try? container.decode(
704 OrchestrationThreadShell.self,
705 forKey: .thread
706 ) else {
707 self = .refreshRequired
708 return
709 }
710 self = .threadUpserted(
711 sequence: sequence,
712 thread: thread
713 )
714 case "thread-removed":
715 guard let sequence = try? container.decode(Int.self, forKey: .sequence),
716 let threadID = try? container.decode(String.self, forKey: .threadId) else {
717 self = .refreshRequired
718 return
719 }
720 self = .threadRemoved(
721 sequence: sequence,
722 threadID: threadID
723 )
724 default:
725 self = .refreshRequired
726 }
727 }
728}
729
730public enum ThreadStreamItem: Decodable, Sendable {
731 case synchronized
732 case snapshot(OrchestrationThreadDetailSnapshot)
733 case event(JSONValue)
734
735 private enum CodingKeys: String, CodingKey { case kind, snapshot, event }
736
737 public init(from decoder: any Decoder) throws {
738 let container = try decoder.container(keyedBy: CodingKeys.self)
739 let kind = try container.decode(String.self, forKey: .kind)
740 switch kind {
741 case "synchronized":
742 self = .synchronized
743 case "snapshot":
744 guard let snapshot = try? container.decode(
745 OrchestrationThreadDetailSnapshot.self,
746 forKey: .snapshot
747 ) else {
748 self = .event(.null)
749 return
750 }
751 self = .snapshot(snapshot)
752 case "event":
753 self = .event((try? container.decode(JSONValue.self, forKey: .event)) ?? .null)
754 default:
755 // The detail reducer already treats an unrecognized event as an
756 // authoritative-refresh request. Reuse that path without adding a
757 // second stream state or terminating the subscription.
758 self = .event(.null)
759 }
760 }
761}