Open History to see the last commit
1import Foundation
2
3/// The app-owned adapter between the native feature layer and T3's WebSocket/Core runtime.
4/// Implementations are main-actor isolated so UI state never depends on locking.
5@MainActor
6public protocol FeatureClient: AnyObject {
7 func initialSnapshot() async throws -> FeatureSnapshot
8 /// Performs one bounded refresh without starting long-lived subscriptions.
9 /// Background tasks use this instead of the foreground bootstrap path.
10 func backgroundSnapshot() async throws -> FeatureSnapshot
11 func events() -> AsyncStream<FeatureEvent>
12 func resumeAfterBackground(reconnect: Bool) async
13
14 func preuploadAttachment(
15 _ attachment: FeatureUploadAttachment,
16 environmentID: String
17 ) async throws -> FeatureUploadedAttachmentReference?
18
19 func pair(endpoint: String, token: String?) async throws
20 func setEnvironmentEnabled(id: String, enabled: Bool) async throws
21 func removeEnvironment(id: String) async throws
22 func disconnect() async
23
24 func addProject(path: String) async throws
25 func createThread(projectID: String, title: String?, selection: FeatureSelection?) async throws -> FeatureThread
26 func createThreadAndSend(
27 projectID: String,
28 prompt: String,
29 selection: FeatureSelection?,
30 runtimeMode: FeatureRuntimeMode,
31 interactionMode: FeatureInteractionMode,
32 attachments: [FeatureUploadAttachment]
33 ) async throws -> FeatureThread
34 func createThreadAndSend(
35 projectID: String,
36 prompt: String,
37 selection: FeatureSelection?,
38 runtimeMode: FeatureRuntimeMode,
39 interactionMode: FeatureInteractionMode,
40 workspaceMode: FeatureWorkspaceMode,
41 branch: String?,
42 worktreePath: String?,
43 startFromOrigin: Bool,
44 attachments: [FeatureUploadAttachment]
45 ) async throws -> FeatureThread
46 func createThreadAndSend(
47 projectID: String,
48 prompt: String,
49 selection: FeatureSelection?,
50 runtimeMode: FeatureRuntimeMode,
51 interactionMode: FeatureInteractionMode,
52 workspaceMode: FeatureWorkspaceMode,
53 branch: String?,
54 worktreePath: String?,
55 startFromOrigin: Bool,
56 attachments: [FeatureUploadAttachment],
57 identity: FeatureSubmissionIdentity
58 ) async throws -> FeatureThread
59 func listWorkspaceBranches(
60 projectID: String,
61 refresh: Bool
62 ) async throws -> [FeatureWorkspaceBranch]
63 func renameThread(id: String, title: String) async throws
64 func regenerateThreadTitle(id: String) async throws
65 func setThreadArchived(id: String, archived: Bool) async throws
66 func setThreadSettled(id: String, settled: Bool) async throws
67 func setThreadSnoozed(id: String, until: Date?) async throws
68 func setThreadPinned(id: String, pinned: Bool) async throws
69 func setRuntimeMode(id: String, mode: FeatureRuntimeMode) async throws
70 func setInteractionMode(id: String, mode: FeatureInteractionMode) async throws
71 func deleteThread(id: String) async throws
72
73 func loadThread(id: String) async throws -> FeatureThreadDetail
74 func loadThread(id: String, fresh: Bool) async throws -> FeatureThreadDetail
75 func loadEarlierThreadTurns(id: String) async throws -> FeatureThreadDetail?
76 func releaseThread(id: String)
77 func sendMessage(threadID: String, text: String, selection: FeatureSelection?) async throws
78 func sendMessage(
79 threadID: String,
80 text: String,
81 selection: FeatureSelection?,
82 attachments: [FeatureUploadAttachment]
83 ) async throws
84 func sendMessage(
85 threadID: String,
86 text: String,
87 selection: FeatureSelection?,
88 attachments: [FeatureUploadAttachment],
89 identity: FeatureSubmissionIdentity
90 ) async throws
91 func sendMessage(
92 threadID: String,
93 text: String,
94 selection: FeatureSelection?,
95 runtimeMode: FeatureRuntimeMode,
96 attachments: [FeatureUploadAttachment],
97 identity: FeatureSubmissionIdentity
98 ) async throws
99 func cancelTurn(threadID: String) async throws
100 func resolveApproval(id: String, decision: FeatureApprovalDecision) async throws
101 func resolveUserInput(id: String, answers: [String: FeatureInputAnswer]) async throws
102
103 func saveSettings(_ settings: FeatureSettings) async throws
104 func serverPreferences(environmentID: String) async throws -> ServerSettingsSnapshot
105 func updateServerPreferences(environmentID: String, change: ServerSettingsChange) async throws
106 func sharedPreferenceMismatches(environmentID: String) -> [String]
107 func refreshProviders(environmentID: String) async throws -> [FeatureProvider]
108 func refreshWorkspaceProviders(environmentID: String, cwd: String, instanceID: String) async throws -> [FeatureProvider]
109 func providerSetup(environmentID: String, instanceID: String, action: ProviderSetupAction) async throws -> ProviderSetupEvent
110 func providerSetupEvents(environmentID: String, instanceID: String) -> AsyncThrowingStream<ProviderSetupEvent, Error>
111 func setProviderEnabled(environmentID: String, instanceID: String, enabled: Bool) async throws
112 func updateAutomaticSettlement(
113 environmentID: String,
114 change: FeatureAutomaticSettlementChange
115 ) async throws -> FeatureAutomaticSettlementSettings
116
117 func usageSummaries(_ input: UsageSummaryInput) async throws -> [FeatureEnvironmentUsage]
118 func usageSummaries(_ input: UsageSummaryInput, refreshPricing: Bool) async throws -> [FeatureEnvironmentUsage]
119 func pullRequestLists(_ input: PullRequestListInput) async throws
120 -> [FeaturePullRequestEnvironmentList]
121 func pullRequestLists(
122 _ input: PullRequestListInput,
123 environmentID: String
124 ) async throws -> [FeaturePullRequestEnvironmentList]
125 func pullRequestDetail(_ target: FeaturePullRequestTarget) async throws -> PullRequestDetail
126 func pullRequestActivity(_ target: FeaturePullRequestTarget) async throws
127 -> PullRequestActivity
128 func pullRequestDiff(_ target: FeaturePullRequestTarget, cursor: String?) async throws
129 -> PullRequestDiffResult
130 func runPullRequestAction(
131 _ target: FeaturePullRequestTarget,
132 action: PullRequestAction,
133 mergeMethod: PullRequestMergeMethod?,
134 updateMethod: PullRequestUpdateMethod?
135 ) async throws
136 func updatePullRequest(
137 _ target: FeaturePullRequestTarget,
138 title: String?,
139 body: String?
140 ) async throws
141 func commentOnPullRequest(_ target: FeaturePullRequestTarget, body: String) async throws
142 func submitPullRequestReview(
143 _ target: FeaturePullRequestTarget,
144 verdict: PullRequestReviewVerdict,
145 body: String,
146 comments: [PullRequestReviewCommentDraft]
147 ) async throws
148 func replyToPullRequestThread(
149 _ target: FeaturePullRequestTarget,
150 threadID: String,
151 body: String
152 ) async throws
153 func setPullRequestThreadResolved(
154 _ target: FeaturePullRequestTarget,
155 threadID: String,
156 resolved: Bool
157 ) async throws
158 func setPullRequestReaction(
159 _ target: FeaturePullRequestTarget,
160 subjectID: String?,
161 content: PullRequestReactionContent,
162 reacted: Bool
163 ) async throws
164 func pullRequestReviewerCandidates(_ target: FeaturePullRequestTarget) async throws
165 -> PullRequestReviewerCandidateList
166 func requestPullRequestReviewers(
167 _ target: FeaturePullRequestTarget,
168 reviewers: [PullRequestReviewerCandidate],
169 requested: Bool
170 ) async throws
171 func invalidatePullRequests(_ target: FeaturePullRequestTarget?) async throws
172
173 func cachedProjectFavicon(
174 environmentID: String,
175 workspaceRoot: String
176 ) async -> Data?
177 func refreshProjectFavicon(
178 environmentID: String,
179 workspaceRoot: String
180 ) async -> Data?
181
182 func listFiles(threadID: String, path: String?) async throws -> [FeatureFileEntry]
183 func searchProjectFiles(
184 projectID: String,
185 query: String,
186 limit: Int
187 ) async throws -> [FeatureFileEntry]
188 func searchThreadFiles(
189 threadID: String,
190 query: String,
191 limit: Int
192 ) async throws -> [FeatureFileEntry]
193 func readFile(threadID: String, path: String) async throws -> FeatureFileContent
194 func loadReview(threadID: String) async throws -> FeatureReview
195 func loadReviewFileContents(
196 threadID: String,
197 file: FeatureReviewFile
198 ) async throws -> FeatureReviewFileContents?
199
200 func sourceControlStatus(threadID: String) async throws -> FeatureSourceControlStatus
201 func sourceControlStatusEvents(threadID: String) -> AsyncStream<FeatureSourceControlStatus>
202 func performSourceControlAction(
203 threadID: String,
204 action: FeatureSourceControlAction,
205 message: String?
206 ) async throws -> FeatureSourceControlStatus
207
208 func terminalSnapshot(threadID: String, terminalID: String) async throws -> FeatureTerminalSnapshot
209 func terminalEvents(threadID: String, terminalID: String) -> AsyncStream<FeatureTerminalSnapshot>
210 func terminalSessions(threadID: String) -> AsyncStream<[FeatureTerminalSnapshot]>
211 func openTerminal(threadID: String, terminalID: String, columns: Int, rows: Int) async throws
212 func writeTerminal(threadID: String, terminalID: String, data: String) async throws
213 func resizeTerminal(
214 threadID: String,
215 terminalID: String,
216 columns: Int,
217 rows: Int
218 ) async throws
219 func clearTerminal(threadID: String, terminalID: String) async throws
220 func closeTerminal(threadID: String, terminalID: String) async throws
221}
222
223public extension FeatureClient {
224 func serverPreferences(environmentID: String) async throws -> ServerSettingsSnapshot {
225 throw FeatureCapabilityUnavailable("Server preferences")
226 }
227 func updateServerPreferences(environmentID: String, change: ServerSettingsChange) async throws {
228 throw FeatureCapabilityUnavailable("Server preferences")
229 }
230 func sharedPreferenceMismatches(environmentID: String) -> [String] { [] }
231
232 func loadThread(id: String, fresh: Bool) async throws -> FeatureThreadDetail {
233 try await loadThread(id: id)
234 }
235
236 func usageSummaries(_ input: UsageSummaryInput, refreshPricing: Bool) async throws -> [FeatureEnvironmentUsage] {
237 try await usageSummaries(input)
238 }
239
240 func setProviderEnabled(environmentID: String, instanceID: String, enabled: Bool) async throws {
241 throw FeatureCapabilityUnavailable("Provider settings")
242 }
243
244 func providerSetup(environmentID: String, instanceID: String, action: ProviderSetupAction) async throws -> ProviderSetupEvent {
245 throw FeatureCapabilityUnavailable("Provider setup")
246 }
247
248 func providerSetupEvents(environmentID: String, instanceID: String) -> AsyncThrowingStream<ProviderSetupEvent, Error> {
249 AsyncThrowingStream { $0.finish() }
250 }
251
252 func resumeAfterBackground(reconnect: Bool) async {}
253
254 func preuploadAttachment(
255 _ attachment: FeatureUploadAttachment,
256 environmentID: String
257 ) async throws -> FeatureUploadedAttachmentReference? {
258 nil
259 }
260}
261
262public extension FeatureClient {
263 func backgroundSnapshot() async throws -> FeatureSnapshot {
264 try await initialSnapshot()
265 }
266
267 func regenerateThreadTitle(id _: String) async throws {
268 throw FeatureCapabilityUnavailable("Thread title regeneration")
269 }
270
271 func loadEarlierThreadTurns(id _: String) async throws -> FeatureThreadDetail? {
272 nil
273 }
274}
275
276public extension FeatureClient {
277 func events() -> AsyncStream<FeatureEvent> {
278 AsyncStream { continuation in continuation.finish() }
279 }
280
281 func setEnvironmentEnabled(id: String, enabled: Bool) async throws {}
282 func removeEnvironment(id: String) async throws {}
283 func disconnect() async {}
284 func refreshWorkspaceProviders(environmentID: String, cwd: String, instanceID: String) async throws -> [FeatureProvider] {
285 throw FeatureCapabilityUnavailable("Workspace provider catalog")
286 }
287
288 func refreshProviders(environmentID _: String) async throws -> [FeatureProvider] {
289 throw FeatureCapabilityUnavailable("Provider refresh")
290 }
291 func updateAutomaticSettlement(
292 environmentID _: String,
293 change _: FeatureAutomaticSettlementChange
294 ) async throws -> FeatureAutomaticSettlementSettings {
295 throw FeatureCapabilityUnavailable("Automatic settlement settings")
296 }
297 func addProject(path: String) async throws {}
298 func usageSummaries(_ input: UsageSummaryInput) async throws -> [FeatureEnvironmentUsage] {
299 []
300 }
301 func pullRequestLists(_ input: PullRequestListInput) async throws
302 -> [FeaturePullRequestEnvironmentList]
303 {
304 []
305 }
306 func pullRequestLists(
307 _ input: PullRequestListInput,
308 environmentID: String
309 ) async throws -> [FeaturePullRequestEnvironmentList] {
310 throw FeatureCapabilityUnavailable("Environment-specific pull request pagination")
311 }
312 func pullRequestDetail(_ target: FeaturePullRequestTarget) async throws -> PullRequestDetail {
313 throw FeatureCapabilityUnavailable("Pull requests")
314 }
315 func pullRequestActivity(_ target: FeaturePullRequestTarget) async throws
316 -> PullRequestActivity
317 {
318 throw FeatureCapabilityUnavailable("Pull request activity")
319 }
320 func pullRequestDiff(_ target: FeaturePullRequestTarget, cursor: String?) async throws
321 -> PullRequestDiffResult
322 {
323 throw FeatureCapabilityUnavailable("Pull request diffs")
324 }
325 func runPullRequestAction(
326 _ target: FeaturePullRequestTarget,
327 action: PullRequestAction,
328 mergeMethod: PullRequestMergeMethod?,
329 updateMethod: PullRequestUpdateMethod?
330 ) async throws { throw FeatureCapabilityUnavailable("Pull request actions") }
331 func updatePullRequest(
332 _ target: FeaturePullRequestTarget,
333 title: String?,
334 body: String?
335 ) async throws { throw FeatureCapabilityUnavailable("Pull request editing") }
336 func commentOnPullRequest(_ target: FeaturePullRequestTarget, body: String) async throws {
337 throw FeatureCapabilityUnavailable("Pull request comments")
338 }
339 func submitPullRequestReview(
340 _ target: FeaturePullRequestTarget,
341 verdict: PullRequestReviewVerdict,
342 body: String,
343 comments: [PullRequestReviewCommentDraft]
344 ) async throws { throw FeatureCapabilityUnavailable("Pull request reviews") }
345 func replyToPullRequestThread(
346 _ target: FeaturePullRequestTarget,
347 threadID: String,
348 body: String
349 ) async throws { throw FeatureCapabilityUnavailable("Pull request replies") }
350 func setPullRequestThreadResolved(
351 _ target: FeaturePullRequestTarget,
352 threadID: String,
353 resolved: Bool
354 ) async throws { throw FeatureCapabilityUnavailable("Pull request review threads") }
355 func setPullRequestReaction(
356 _ target: FeaturePullRequestTarget,
357 subjectID: String?,
358 content: PullRequestReactionContent,
359 reacted: Bool
360 ) async throws { throw FeatureCapabilityUnavailable("Pull request reactions") }
361 func pullRequestReviewerCandidates(_ target: FeaturePullRequestTarget) async throws
362 -> PullRequestReviewerCandidateList
363 {
364 throw FeatureCapabilityUnavailable("Pull request reviewers")
365 }
366 func requestPullRequestReviewers(
367 _ target: FeaturePullRequestTarget,
368 reviewers: [PullRequestReviewerCandidate],
369 requested: Bool
370 ) async throws { throw FeatureCapabilityUnavailable("Pull request reviewers") }
371 func invalidatePullRequests(_ target: FeaturePullRequestTarget?) async throws {}
372 func cachedProjectFavicon(environmentID: String, workspaceRoot: String) async -> Data? {
373 nil
374 }
375 func refreshProjectFavicon(environmentID: String, workspaceRoot: String) async -> Data? {
376 nil
377 }
378 func releaseThread(id: String) {}
379 func resolveUserInput(id: String, answers: [String: FeatureInputAnswer]) async throws {}
380
381 /// Keeps simple text-only callers source-compatible while the typed API
382 /// preserves multi-select answers as arrays.
383 func resolveUserInput(id: String, answers: [String: String]) async throws {
384 try await resolveUserInput(
385 id: id,
386 answers: answers.mapValues(FeatureInputAnswer.text)
387 )
388 }
389 func setThreadSettled(id: String, settled: Bool) async throws {}
390 func setThreadSnoozed(id: String, until: Date?) async throws {}
391 func setThreadPinned(id: String, pinned: Bool) async throws {}
392 func setRuntimeMode(id: String, mode: FeatureRuntimeMode) async throws {}
393 func setInteractionMode(id: String, mode: FeatureInteractionMode) async throws {}
394 func loadReviewFileContents(
395 threadID: String,
396 file: FeatureReviewFile
397 ) async throws -> FeatureReviewFileContents? {
398 nil
399 }
400
401 func listWorkspaceBranches(
402 projectID: String,
403 refresh: Bool
404 ) async throws -> [FeatureWorkspaceBranch] {
405 []
406 }
407
408 /// Legacy clients still create in the current checkout. Native clients
409 /// override this overload to prepare worktrees atomically with the first turn.
410 func createThreadAndSend(
411 projectID: String,
412 prompt: String,
413 selection: FeatureSelection?,
414 runtimeMode: FeatureRuntimeMode,
415 interactionMode: FeatureInteractionMode,
416 workspaceMode: FeatureWorkspaceMode,
417 branch: String?,
418 worktreePath: String?,
419 startFromOrigin: Bool,
420 attachments: [FeatureUploadAttachment]
421 ) async throws -> FeatureThread {
422 try await createThreadAndSend(
423 projectID: projectID,
424 prompt: prompt,
425 selection: selection,
426 runtimeMode: runtimeMode,
427 interactionMode: interactionMode,
428 attachments: attachments
429 )
430 }
431
432 func createThreadAndSend(
433 projectID: String,
434 prompt: String,
435 selection: FeatureSelection?,
436 runtimeMode: FeatureRuntimeMode,
437 interactionMode: FeatureInteractionMode,
438 attachments: [FeatureUploadAttachment]
439 ) async throws -> FeatureThread {
440 let thread = try await createThread(
441 projectID: projectID,
442 title: prompt,
443 selection: selection
444 )
445 try await sendMessage(
446 threadID: thread.id,
447 text: prompt,
448 selection: selection,
449 attachments: attachments
450 )
451 return thread
452 }
453
454 /// Clients that understand stable command identities override this method.
455 /// The compatibility path remains functional but cannot guarantee
456 /// idempotence across a process death after an ambiguous network failure.
457 func createThreadAndSend(
458 projectID: String,
459 prompt: String,
460 selection: FeatureSelection?,
461 runtimeMode: FeatureRuntimeMode,
462 interactionMode: FeatureInteractionMode,
463 workspaceMode: FeatureWorkspaceMode,
464 branch: String?,
465 worktreePath: String?,
466 startFromOrigin: Bool,
467 attachments: [FeatureUploadAttachment],
468 identity: FeatureSubmissionIdentity
469 ) async throws -> FeatureThread {
470 try await createThreadAndSend(
471 projectID: projectID,
472 prompt: prompt,
473 selection: selection,
474 runtimeMode: runtimeMode,
475 interactionMode: interactionMode,
476 workspaceMode: workspaceMode,
477 branch: branch,
478 worktreePath: worktreePath,
479 startFromOrigin: startFromOrigin,
480 attachments: attachments
481 )
482 }
483
484 func sendMessage(
485 threadID: String,
486 text: String,
487 selection: FeatureSelection?,
488 attachments: [FeatureUploadAttachment]
489 ) async throws {
490 guard attachments.isEmpty else {
491 throw FeatureCapabilityUnavailable("Image attachments")
492 }
493 try await sendMessage(threadID: threadID, text: text, selection: selection)
494 }
495
496 func sendMessage(
497 threadID: String,
498 text: String,
499 selection: FeatureSelection?,
500 attachments: [FeatureUploadAttachment],
501 identity: FeatureSubmissionIdentity
502 ) async throws {
503 try await sendMessage(
504 threadID: threadID,
505 text: text,
506 selection: selection,
507 attachments: attachments
508 )
509 }
510
511 /// Durable submissions carry the modes that were active when the user
512 /// sent them. Older clients can ignore them, while native retries preserve
513 /// the original permission instead of reading a later thread value.
514 func sendMessage(
515 threadID: String,
516 text: String,
517 selection: FeatureSelection?,
518 runtimeMode: FeatureRuntimeMode,
519 attachments: [FeatureUploadAttachment],
520 identity: FeatureSubmissionIdentity
521 ) async throws {
522 try await sendMessage(
523 threadID: threadID,
524 text: text,
525 selection: selection,
526 attachments: attachments,
527 identity: identity
528 )
529 }
530
531 func listFiles(threadID: String, path: String?) async throws -> [FeatureFileEntry] {
532 throw FeatureCapabilityUnavailable("Files")
533 }
534
535 func searchProjectFiles(
536 projectID: String,
537 query: String,
538 limit: Int
539 ) async throws -> [FeatureFileEntry] {
540 throw FeatureCapabilityUnavailable("File search")
541 }
542
543 func searchThreadFiles(
544 threadID: String,
545 query: String,
546 limit: Int
547 ) async throws -> [FeatureFileEntry] {
548 throw FeatureCapabilityUnavailable("File search")
549 }
550
551 func readFile(threadID: String, path: String) async throws -> FeatureFileContent {
552 throw FeatureCapabilityUnavailable("File preview")
553 }
554
555 func loadReview(threadID: String) async throws -> FeatureReview {
556 throw FeatureCapabilityUnavailable("Review")
557 }
558
559 func sourceControlStatus(threadID: String) async throws -> FeatureSourceControlStatus {
560 throw FeatureCapabilityUnavailable("Source control")
561 }
562
563 func sourceControlStatusEvents(threadID: String) -> AsyncStream<FeatureSourceControlStatus> {
564 AsyncStream { $0.finish() }
565 }
566
567 func performSourceControlAction(
568 threadID: String,
569 action: FeatureSourceControlAction,
570 message: String?
571 ) async throws -> FeatureSourceControlStatus {
572 throw FeatureCapabilityUnavailable("Source control actions")
573 }
574
575 func terminalSnapshot(threadID: String, terminalID _: String) async throws -> FeatureTerminalSnapshot {
576 throw FeatureCapabilityUnavailable("Terminal")
577 }
578
579 func terminalEvents(threadID: String, terminalID _: String) -> AsyncStream<FeatureTerminalSnapshot> {
580 AsyncStream { $0.finish() }
581 }
582
583 func terminalSessions(threadID _: String) -> AsyncStream<[FeatureTerminalSnapshot]> {
584 AsyncStream { $0.finish() }
585 }
586
587 func openTerminal(
588 threadID: String,
589 terminalID _: String,
590 columns: Int,
591 rows: Int
592 ) async throws {
593 throw FeatureCapabilityUnavailable("Terminal")
594 }
595
596 func writeTerminal(threadID: String, terminalID _: String, data: String) async throws {
597 throw FeatureCapabilityUnavailable("Terminal")
598 }
599
600 func resizeTerminal(
601 threadID: String,
602 terminalID _: String,
603 columns: Int,
604 rows: Int
605 ) async throws {
606 throw FeatureCapabilityUnavailable("Terminal")
607 }
608
609 func clearTerminal(threadID: String, terminalID _: String) async throws {
610 throw FeatureCapabilityUnavailable("Terminal")
611 }
612
613 func closeTerminal(threadID: String, terminalID _: String) async throws {
614 throw FeatureCapabilityUnavailable("Terminal")
615 }
616}