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 if (thread.pendingRuntimeRequest?.kind === "user_input") return "waiting_for_input";
102 if (
103 thread.pendingRuntimeRequest !== null &&
104 thread.pendingRuntimeRequest.kind !== "auth_refresh"
105 ) {
106 return "waiting_for_approval";
107 }
108 switch (thread.activityRunStatus ?? thread.status) {
109 case "preparing":
110 case "starting":
111 return "starting";
112 case "running":
113 case "waiting":
114 return "running";
115 case "completed":
116 return backgroundWorkHoldsCompletion(thread.pendingBackgroundTasks ?? [])
117 ? "running"
118 : "completed";
119 case "failed":
120 return "failed";
121 case "idle":
122 case "queued":
123 case "interrupted":
124 case "cancelled":
125 case "rolled_back":
126 return null;
127 }
128}
129
130function headlineForPhase(phase: AgentAwarenessPhase): string {
131 switch (phase) {
132 case "starting":
133 return "Starting agent";
134 case "running":
135 return "Agent is working";
136 case "waiting_for_approval":
137 return "Approval needed";
138 case "waiting_for_input":
139 return "Waiting for input";
140 case "completed":
141 return "Agent finished";
142 case "failed":
143 return "Agent failed";
144 case "stale":
145 return "Update delayed";
146 }
147}