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

agentAwareness.ts

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