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

agentAwareness.ts

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