Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

agentAwareness.ts

160 lines4.4 KB

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;
32​
33function buildAgentAwarenessDeepLink(input: {
34 readonly environmentId: EnvironmentId;
35 readonly threadId: ThreadId;
36}): string {
37 return `/threads/${encodeURIComponent(input.environmentId)}/${encodeURIComponent(input.threadId)}`;
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 | "latestRunStopRequested"
49 | "latestRunSilent"
50 | "modelSelection"
51 | "pendingBackgroundTasks"
52 | "pendingRuntimeRequest"
53 | "settledOverride"
54 | "status"
55 | "title"
56 | "updatedAt"
57 >;
59​
60export function projectThreadAwarenessV2(
61 input: ProjectThreadAwarenessV2Input,
62): AgentAwarenessState | null {
63 const { environmentId, project, thread } = input;
64 if (thread.lineage.relationshipToParent === "subagent") 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 if (
71 thread.latestRunSilent === true &&
72 (phase === "starting" || phase === "running" || phase === "completed")
73 )
74 return null;
75 // Resolve the phase first so live or waiting work stays visible after settlement.
76 if (
77 thread.settledOverride === "settled" &&
78 (phase === "completed" || phase === "failed") &&
79 !backgroundWorkHoldsCompletion(thread.pendingBackgroundTasks ?? [])
80 ) {
81 return null;
82 }
83 const detail =
84 phase === "completed"
85 ? "Review the completed task."
86 : phase === "failed"
87 ? "The agent run failed."
88 : undefined;
89 return {
90 environmentId,
91 threadId: thread.id,
92 projectTitle: project.title,
93 threadTitle: thread.title,
94 phase,
95 headline: headlineForPhase(phase),
96 ...(detail === undefined ? {} : { detail }),
97 modelTitle: thread.modelSelection.model,
98 updatedAt: DateTime.formatIso(thread.updatedAt),
99 deepLink: buildAgentAwarenessDeepLink({ environmentId, threadId: thread.id }),
100 };
102​
103function resolveThreadAwarenessPhaseV2(
104 thread: ProjectThreadAwarenessV2Input["thread"],
105): AgentAwarenessPhase | null {
106 const runStatus = thread.activityRunStatus ?? thread.status;
107 if (
108 thread.pendingRuntimeRequest?.kind === "user_input" &&
109 (thread.pendingRuntimeRequest.blocking !== false ||
110 !(runStatus === "preparing" || runStatus === "starting" || runStatus === "running"))
111 ) {
112 return "waiting_for_input";
113 }
114 if (
115 thread.pendingRuntimeRequest !== null &&
116 thread.pendingRuntimeRequest.kind !== "user_input" &&
117 thread.pendingRuntimeRequest.kind !== "auth_refresh"
118 ) {
119 return "waiting_for_approval";
120 }
121 switch (runStatus) {
122 case "preparing":
123 case "starting":
124 return "starting";
125 case "running":
126 case "waiting":
127 return "running";
128 case "completed":
129 return backgroundWorkHoldsCompletion(thread.pendingBackgroundTasks ?? [])
130 ? "running"
131 : "completed";
132 case "failed":
133 return "failed";
134 case "idle":
135 case "queued":
136 case "interrupted":
137 case "cancelled":
138 case "rolled_back":
139 return null;
140 }
142​
143function headlineForPhase(phase: AgentAwarenessPhase): string {
144 switch (phase) {
145 case "starting":
146 return "Starting agent";
147 case "running":
148 return "Agent is working";
149 case "waiting_for_approval":
150 return "Approval needed";
151 case "waiting_for_input":
152 return "Waiting for input";
153 case "completed":
154 return "Agent finished";
155 case "failed":
156 return "Agent failed";
157 case "stale":
158 return "Update delayed";
159 }