Open History to see the last commit
1import type {
2 EnvironmentId,
3 OrchestrationProjectShell,
4 OrchestrationThreadShell,
5 ThreadId,
6} from "@t3tools/contracts";
7
8export type AgentAwarenessPhase =
9 | "starting"
10 | "running"
11 | "waiting_for_approval"
12 | "waiting_for_input"
13 | "completed"
14 | "failed"
15 | "stale";
16
17export interface AgentAwarenessState {
18 readonly environmentId: EnvironmentId;
19 readonly threadId: ThreadId;
20 readonly projectTitle: string;
21 readonly threadTitle: string;
22 readonly phase: AgentAwarenessPhase;
23 readonly headline: string;
24 readonly detail?: string;
25 readonly modelTitle: string;
26 readonly updatedAt: string;
27 readonly deepLink: string;
28}
29
30export interface ProjectThreadAwarenessInput {
31 readonly environmentId: EnvironmentId;
32 readonly project: Pick<OrchestrationProjectShell, "title">;
33 readonly thread: Pick<
34 OrchestrationThreadShell,
35 | "id"
36 | "title"
37 | "modelSelection"
38 | "session"
39 | "latestTurn"
40 | "updatedAt"
41 | "hasPendingApprovals"
42 | "hasPendingUserInput"
43 >;
44}
45
46export function buildAgentAwarenessDeepLink(input: {
47 readonly environmentId: EnvironmentId;
48 readonly threadId: ThreadId;
49}): string {
50 return `/threads/${encodeURIComponent(input.environmentId)}/${encodeURIComponent(input.threadId)}`;
51}
52
53export function isTerminalAgentAwarenessPhase(phase: AgentAwarenessPhase): boolean {
54 return phase === "completed" || phase === "failed";
55}
56
57export function isInterruptiveAgentAwarenessPhase(phase: AgentAwarenessPhase): boolean {
58 return phase === "waiting_for_approval" || phase === "waiting_for_input" || phase === "failed";
59}
60
61export function projectThreadAwareness(
62 input: ProjectThreadAwarenessInput,
63): AgentAwarenessState | null {
64 const { environmentId, project, thread } = input;
65 const phase = resolveThreadAwarenessPhase(thread);
66 if (!phase) {
67 return null;
68 }
69
70 const detail = detailForPhase(phase, thread);
71 return {
72 environmentId,
73 threadId: thread.id,
74 projectTitle: project.title,
75 threadTitle: thread.title,
76 phase,
77 headline: headlineForPhase(phase),
78 ...(detail === undefined ? {} : { detail }),
79 modelTitle: thread.modelSelection.model,
80 updatedAt: thread.updatedAt,
81 deepLink: buildAgentAwarenessDeepLink({ environmentId, threadId: thread.id }),
82 };
83}
84
85function resolveThreadAwarenessPhase(
86 thread: ProjectThreadAwarenessInput["thread"],
87): AgentAwarenessPhase | null {
88 if (thread.hasPendingApprovals) {
89 return "waiting_for_approval";
90 }
91 if (thread.hasPendingUserInput) {
92 return "waiting_for_input";
93 }
94 if (thread.session?.status === "error" || thread.latestTurn?.state === "error") {
95 return "failed";
96 }
97 if (thread.session?.status === "starting") {
98 return "starting";
99 }
100 if (thread.session?.status === "running" || thread.latestTurn?.state === "running") {
101 return "running";
102 }
103 if (thread.latestTurn?.state === "completed") {
104 return "completed";
105 }
106 // A turn that finished can still read as "interrupted" here: session
107 // teardown settles still-running turns by session status, and that write
108 // can race the turn.completed one. completedAt survives the race — a turn
109 // that has a completion timestamp finished, whatever the state column says.
110 // Without this, quick finish-then-teardown threads resolve to null
111 // persistently and get tombstoned instead of published as completed.
112 if (thread.latestTurn?.state === "interrupted" && thread.latestTurn.completedAt !== null) {
113 return "completed";
114 }
115 // Threads whose turns never produce a checkpoint (no code changes) have no
116 // materialized latestTurn in the shell at all, and the session-set
117 // projection clears latest_turn_id the moment the session settles. The
118 // session status is then the only surviving completion signal: a live
119 // session at "ready"/"idle" with nothing pending and nothing running means
120 // the agent finished and is waiting for the next prompt — Done.
121 if (thread.session?.status === "ready" || thread.session?.status === "idle") {
122 return "completed";
123 }
124 return null;
125}
126
127function headlineForPhase(phase: AgentAwarenessPhase): string {
128 switch (phase) {
129 case "starting":
130 return "Starting agent";
131 case "running":
132 return "Agent is working";
133 case "waiting_for_approval":
134 return "Approval needed";
135 case "waiting_for_input":
136 return "Waiting for input";
137 case "completed":
138 return "Agent finished";
139 case "failed":
140 return "Agent failed";
141 case "stale":
142 return "Update delayed";
143 }
144}
145
146function detailForPhase(
147 phase: AgentAwarenessPhase,
148 thread: ProjectThreadAwarenessInput["thread"],
149): string | undefined {
150 if (phase === "failed") {
151 return thread.session?.lastError ?? undefined;
152 }
153 if (phase === "completed") {
154 return "Review the completed task.";
155 }
156 if (phase === "running" && thread.session?.providerName) {
157 return `${thread.session.providerName} is active.`;
158 }
159 return undefined;
160}