Open History to see the last commit
1import type {
2 EnvironmentId,
3 OrchestrationV2ThreadShell,
4 Project,
5 ThreadId,
6} from "@t3tools/contracts";
7import * as DateTime from "effect/DateTime";
8
9export type AgentAwarenessPhase =
10 | "starting"
11 | "running"
12 | "waiting_for_approval"
13 | "waiting_for_input"
14 | "completed"
15 | "failed"
16 | "stale";
17
18export interface AgentAwarenessState {
19 readonly environmentId: EnvironmentId;
20 readonly threadId: ThreadId;
21 readonly projectTitle: string;
22 readonly threadTitle: string;
23 readonly phase: AgentAwarenessPhase;
24 readonly headline: string;
25 readonly detail?: string;
26 readonly modelTitle: string;
27 readonly updatedAt: string;
28 readonly deepLink: string;
29}
30
31export function buildAgentAwarenessDeepLink(input: {
32 readonly environmentId: EnvironmentId;
33 readonly threadId: ThreadId;
34}): string {
35 return `/threads/${encodeURIComponent(input.environmentId)}/${encodeURIComponent(input.threadId)}`;
36}
37
38export interface ProjectThreadAwarenessV2Input {
39 readonly environmentId: EnvironmentId;
40 readonly project: Pick<Project, "title">;
41 readonly thread: Pick<
42 OrchestrationV2ThreadShell,
43 | "activityRunStatus"
44 | "id"
45 | "modelSelection"
46 | "pendingRuntimeRequest"
47 | "status"
48 | "title"
49 | "updatedAt"
50 >;
51}
52
53/** Build relay activity directly from the V2 shell projection. */
54export function projectThreadAwarenessV2(
55 input: ProjectThreadAwarenessV2Input,
56): AgentAwarenessState | null {
57 const { environmentId, project, thread } = input;
58 const phase = resolveThreadAwarenessPhaseV2(thread);
59 if (phase === null) {
60 return null;
61 }
62 const detail =
63 phase === "completed"
64 ? "Review the completed task."
65 : phase === "failed"
66 ? "The agent run failed."
67 : undefined;
68 return {
69 environmentId,
70 threadId: thread.id,
71 projectTitle: project.title,
72 threadTitle: thread.title,
73 phase,
74 headline: headlineForPhase(phase),
75 ...(detail === undefined ? {} : { detail }),
76 modelTitle: thread.modelSelection.model,
77 updatedAt: DateTime.formatIso(thread.updatedAt),
78 deepLink: buildAgentAwarenessDeepLink({ environmentId, threadId: thread.id }),
79 };
80}
81
82function resolveThreadAwarenessPhaseV2(
83 thread: ProjectThreadAwarenessV2Input["thread"],
84): AgentAwarenessPhase | null {
85 if (thread.pendingRuntimeRequest?.kind === "user_input") {
86 return "waiting_for_input";
87 }
88 if (
89 thread.pendingRuntimeRequest !== null &&
90 thread.pendingRuntimeRequest.kind !== "auth_refresh"
91 ) {
92 return "waiting_for_approval";
93 }
94 switch (thread.activityRunStatus ?? thread.status) {
95 case "preparing":
96 case "starting":
97 return "starting";
98 case "running":
99 case "waiting":
100 return "running";
101 case "completed":
102 return "completed";
103 case "failed":
104 return "failed";
105 case "idle":
106 case "queued":
107 case "interrupted":
108 case "cancelled":
109 case "rolled_back":
110 return null;
111 }
112}
113
114function headlineForPhase(phase: AgentAwarenessPhase): string {
115 switch (phase) {
116 case "starting":
117 return "Starting agent";
118 case "running":
119 return "Agent is working";
120 case "waiting_for_approval":
121 return "Approval needed";
122 case "waiting_for_input":
123 return "Waiting for input";
124 case "completed":
125 return "Agent finished";
126 case "failed":
127 return "Agent failed";
128 case "stale":
129 return "Update delayed";
130 }
131}