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