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

agentAwareness.ts

133 lines3.4 KB

1import type {
2 EnvironmentId,
3 OrchestrationV2ThreadShell,
4 Project,
5 ThreadId,
6} from "@t3tools/contracts";
7import * as DateTime from "effect/DateTime";
8​
9export type AgentAwarenessPhase =
10 | "starting"
11 | "running"
12 | "waiting_for_approval"
13 | "waiting_for_input"
14 | "completed"
15 | "failed"
16 | "stale";
17​
18export interface AgentAwarenessState {
19 readonly environmentId: EnvironmentId;
20 readonly threadId: ThreadId;
21 readonly projectTitle: string;
22 readonly threadTitle: string;
23 readonly phase: AgentAwarenessPhase;
24 readonly headline: string;
25 readonly detail?: string;
26 readonly modelTitle: string;
27 readonly updatedAt: string;
28 readonly deepLink: string;
30​
31function buildAgentAwarenessDeepLink(input: {
32 readonly environmentId: EnvironmentId;
33 readonly threadId: ThreadId;
34}): string {
35 return `/threads/${encodeURIComponent(input.environmentId)}/${encodeURIComponent(input.threadId)}`;
37​
38export interface ProjectThreadAwarenessV2Input {
39 readonly environmentId: EnvironmentId;
40 readonly project: Pick<Project, "title">;
41 readonly thread: Pick<
42 OrchestrationV2ThreadShell,
43 | "activityRunStatus"
44 | "id"
45 | "lineage"
46 | "modelSelection"
47 | "pendingRuntimeRequest"
48 | "status"
49 | "title"
50 | "updatedAt"
51 >;
53​
54/** Build relay activity directly from the V2 shell projection. */
55export function projectThreadAwarenessV2(
56 input: ProjectThreadAwarenessV2Input,
57): AgentAwarenessState | null {
58 const { environmentId, project, thread } = input;
59 if (thread.lineage.relationshipToParent === "subagent") return null;
60 const phase = resolveThreadAwarenessPhaseV2(thread);
61 if (phase === null) {
62 return null;
63 }
64 const detail =
65 phase === "completed"
66 ? "Review the completed task."
67 : phase === "failed"
68 ? "The agent run failed."
69 : undefined;
70 return {
71 environmentId,
72 threadId: thread.id,
73 projectTitle: project.title,
74 threadTitle: thread.title,
75 phase,
76 headline: headlineForPhase(phase),
77 ...(detail === undefined ? {} : { detail }),
78 modelTitle: thread.modelSelection.model,
79 updatedAt: DateTime.formatIso(thread.updatedAt),
80 deepLink: buildAgentAwarenessDeepLink({ environmentId, threadId: thread.id }),
81 };
83​
84function resolveThreadAwarenessPhaseV2(
85 thread: ProjectThreadAwarenessV2Input["thread"],
86): AgentAwarenessPhase | null {
87 if (thread.pendingRuntimeRequest?.kind === "user_input") {
88 return "waiting_for_input";
89 }
90 if (
91 thread.pendingRuntimeRequest !== null &&
92 thread.pendingRuntimeRequest.kind !== "auth_refresh"
93 ) {
94 return "waiting_for_approval";
95 }
96 switch (thread.activityRunStatus ?? thread.status) {
97 case "preparing":
98 case "starting":
99 return "starting";
100 case "running":
101 case "waiting":
102 return "running";
103 case "completed":
104 return "completed";
105 case "failed":
106 return "failed";
107 case "idle":
108 case "queued":
109 case "interrupted":
110 case "cancelled":
111 case "rolled_back":
112 return null;
113 }
115​
116function headlineForPhase(phase: AgentAwarenessPhase): string {
117 switch (phase) {
118 case "starting":
119 return "Starting agent";
120 case "running":
121 return "Agent is working";
122 case "waiting_for_approval":
123 return "Approval needed";
124 case "waiting_for_input":
125 return "Waiting for input";
126 case "completed":
127 return "Agent finished";
128 case "failed":
129 return "Agent failed";
130 case "stale":
131 return "Update delayed";
132 }