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