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 function isTerminalAgentAwarenessPhase(phase: AgentAwarenessPhase): boolean {
39 return phase === "completed" || phase === "failed";
40}
41
42export function isInterruptiveAgentAwarenessPhase(phase: AgentAwarenessPhase): boolean {
43 return phase === "waiting_for_approval" || phase === "waiting_for_input" || phase === "failed";
44}
45
46export interface ProjectThreadAwarenessV2Input {
47 readonly environmentId: EnvironmentId;
48 readonly project: Pick<Project, "title">;
49 readonly thread: Pick<
50 OrchestrationV2ThreadShell,
51 "id" | "title" | "modelSelection" | "status" | "pendingRuntimeRequest" | "updatedAt"
52 >;
53}
54
55/** Build relay activity directly from the V2 shell projection. */
56export function projectThreadAwarenessV2(
57 input: ProjectThreadAwarenessV2Input,
58): AgentAwarenessState | null {
59 const { environmentId, project, thread } = input;
60 const phase = resolveThreadAwarenessPhaseV2(thread);
61 if (phase === null) {
62 return null;
63 }
64 const detail =
65 phase === "completed"
66 ? "Review the completed task."
67 : phase === "failed"
68 ? "The agent run failed."
69 : undefined;
70 return {
71 environmentId,
72 threadId: thread.id,
73 projectTitle: project.title,
74 threadTitle: thread.title,
75 phase,
76 headline: headlineForPhase(phase),
77 ...(detail === undefined ? {} : { detail }),
78 modelTitle: thread.modelSelection.model,
79 updatedAt: DateTime.formatIso(thread.updatedAt),
80 deepLink: buildAgentAwarenessDeepLink({ environmentId, threadId: thread.id }),
81 };
82}
83
84function resolveThreadAwarenessPhaseV2(
85 thread: ProjectThreadAwarenessV2Input["thread"],
86): AgentAwarenessPhase | null {
87 if (thread.pendingRuntimeRequest?.kind === "user_input") {
88 return "waiting_for_input";
89 }
90 if (thread.pendingRuntimeRequest !== null) {
91 return "waiting_for_approval";
92 }
93 switch (thread.status) {
94 case "preparing":
95 case "starting":
96 return "starting";
97 case "running":
98 case "waiting":
99 return "running";
100 case "completed":
101 return "completed";
102 case "failed":
103 return "failed";
104 case "idle":
105 case "queued":
106 case "interrupted":
107 case "cancelled":
108 case "rolled_back":
109 return null;
110 }
111}
112
113function headlineForPhase(phase: AgentAwarenessPhase): string {
114 switch (phase) {
115 case "starting":
116 return "Starting agent";
117 case "running":
118 return "Agent is working";
119 case "waiting_for_approval":
120 return "Approval needed";
121 case "waiting_for_input":
122 return "Waiting for input";
123 case "completed":
124 return "Agent finished";
125 case "failed":
126 return "Agent failed";
127 case "stale":
128 return "Update delayed";
129 }
130}