Open History to see the last commit
1import ActivityKit
2import Foundation
3
4enum T3AgentActivityPhase: String, Codable, Hashable, Sendable {
5 case starting
6 case running
7 case waitingForApproval = "waiting_for_approval"
8 case waitingForInput = "waiting_for_input"
9 case completed
10 case failed
11 case stale
12
13 var systemImage: String {
14 switch self {
15 case .starting:
16 "circle.dotted"
17 case .running:
18 "arrow.trianglehead.2.clockwise.rotate.90"
19 case .waitingForApproval:
20 "exclamationmark.circle.fill"
21 case .waitingForInput:
22 "questionmark.circle.fill"
23 case .completed:
24 "checkmark.circle.fill"
25 case .failed:
26 "xmark.octagon.fill"
27 case .stale:
28 "clock.arrow.circlepath"
29 }
30 }
31}
32
33/// Mirrors `RelayAgentActivityAggregateRow` in packages/contracts/src/relay.ts.
34struct T3RelayAgentActivityAggregateRow: Codable, Hashable, Identifiable, Sendable {
35 var environmentId: String
36 var threadId: String
37 var projectTitle: String
38 var threadTitle: String
39 var modelTitle: String
40 var phase: T3AgentActivityPhase
41 var status: String
42 var updatedAt: String
43 var deepLink: String
44
45 var id: String { "\(environmentId):\(threadId)" }
46
47 /// Generate the native query route rather than trusting a web-shaped path.
48 var nativeDeepLinkURL: URL? {
49 var components = URLComponents()
50 components.scheme = T3SharedContainer.urlScheme
51 components.host = "threads"
52 components.queryItems = [
53 URLQueryItem(name: "environment", value: environmentId),
54 URLQueryItem(name: "thread", value: threadId),
55 ]
56 return components.url
57 }
58}
59
60/// Mirrors `RelayAgentActivityAggregateState` in packages/contracts/src/relay.ts.
61struct T3RelayAgentActivityAggregateState: Codable, Hashable, Sendable {
62 var title: String
63 var subtitle: String
64 var activeCount: Int
65 var updatedAt: String
66 var activities: [T3RelayAgentActivityAggregateRow]
67
68 var attentionFirstActivities: [T3RelayAgentActivityAggregateRow] {
69 activities.sorted { left, right in
70 let leftPriority = left.phase.presentationPriority
71 let rightPriority = right.phase.presentationPriority
72 return leftPriority == rightPriority
73 ? left.updatedAt > right.updatedAt
74 : leftPriority < rightPriority
75 }
76 }
77}
78
79/// This exact type and state envelope are part of the relay/APNs protocol.
80/// The relay sends `attributes-type: LiveActivityAttributes`, empty attributes,
81/// and `{ name: "AgentActivity", props: "<aggregate JSON>" }` as content state.
82struct LiveActivityAttributes: ActivityAttributes, Hashable {
83 struct ContentState: Codable, Hashable, Sendable {
84 var name: String
85 var props: String
86
87 var aggregate: T3RelayAgentActivityAggregateState? {
88 guard name == LiveActivityAttributes.activityName,
89 let data = props.data(using: .utf8)
90 else {
91 return nil
92 }
93 return try? JSONDecoder().decode(T3RelayAgentActivityAggregateState.self, from: data)
94 }
95
96 init(name: String, props: String) {
97 self.name = name
98 self.props = props
99 }
100
101 init(aggregate: T3RelayAgentActivityAggregateState) throws {
102 name = LiveActivityAttributes.activityName
103 props = String(decoding: try JSONEncoder().encode(aggregate), as: UTF8.self)
104 }
105 }
106
107 static let activityName = "AgentActivity"
108}
109
110extension T3AgentActivityPhase {
111 fileprivate var presentationPriority: Int {
112 switch self {
113 case .waitingForApproval, .waitingForInput: 0
114 case .failed: 1
115 case .starting, .running: 2
116 case .completed, .stale: 3
117 }
118 }
119}