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

agentAwareness.ts

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