Open History to see the last commit
1import type {
2 EnvironmentId,
3 OrchestrationProjectShell,
4 OrchestrationThreadShell,
5 ThreadId,
6} from "@t3tools/contracts";
7
8export type AgentAwarenessPhase =
9 | "starting"
10 | "running"
11 | "waiting_for_approval"
12 | "waiting_for_input"
13 | "completed"
14 | "failed"
15 | "stale";
16
17export interface AgentAwarenessState {
18 readonly environmentId: EnvironmentId;
19 readonly threadId: ThreadId;
20 readonly projectTitle: string;
21 readonly threadTitle: string;
22 readonly phase: AgentAwarenessPhase;
23 readonly headline: string;
24 readonly detail?: string;
25 readonly modelTitle: string;
26 readonly updatedAt: string;
27 readonly deepLink: string;
28}
29
30export interface ProjectThreadAwarenessInput {
31 readonly environmentId: EnvironmentId;
32 readonly project: Pick<OrchestrationProjectShell, "title">;
33 readonly thread: Pick<
34 OrchestrationThreadShell,
35 | "id"
36 | "title"
37 | "modelSelection"
38 | "session"
39 | "latestTurn"
40 | "updatedAt"
41 | "hasPendingApprovals"
42 | "hasPendingUserInput"
43 >;
44}
45
46export function buildAgentAwarenessDeepLink(input: {
47 readonly environmentId: EnvironmentId;
48 readonly threadId: ThreadId;
49}): string {
50 return `/threads/${encodeURIComponent(input.environmentId)}/${encodeURIComponent(input.threadId)}`;
51}
52
53export function isTerminalAgentAwarenessPhase(phase: AgentAwarenessPhase): boolean {
54 return phase === "completed" || phase === "failed";
55}
56
57export function isInterruptiveAgentAwarenessPhase(phase: AgentAwarenessPhase): boolean {
58 return phase === "waiting_for_approval" || phase === "waiting_for_input" || phase === "failed";
59}
60
61export function projectThreadAwareness(
62 input: ProjectThreadAwarenessInput,
63): AgentAwarenessState | null {
64 const { environmentId, project, thread } = input;
65 const phase = resolveThreadAwarenessPhase(thread);
66 if (!phase) {
67 return null;
68 }
69
70 const detail = detailForPhase(phase, thread);
71 return {
72 environmentId,
73 threadId: thread.id,
74 projectTitle: project.title,
75 threadTitle: thread.title,
76 phase,
77 headline: headlineForPhase(phase),
78 ...(detail === undefined ? {} : { detail }),
79 modelTitle: thread.modelSelection.model,
80 updatedAt: thread.updatedAt,
81 deepLink: buildAgentAwarenessDeepLink({ environmentId, threadId: thread.id }),
82 };
83}
84
85function resolveThreadAwarenessPhase(
86 thread: ProjectThreadAwarenessInput["thread"],
87): AgentAwarenessPhase | null {
88 if (thread.hasPendingApprovals) {
89 return "waiting_for_approval";
90 }
91 if (thread.hasPendingUserInput) {
92 return "waiting_for_input";
93 }
94 if (thread.session?.status === "error" || thread.latestTurn?.state === "error") {
95 return "failed";
96 }
97 if (thread.session?.status === "starting") {
98 return "starting";
99 }
100 if (thread.session?.status === "running" || thread.latestTurn?.state === "running") {
101 return "running";
102 }
103 if (thread.latestTurn?.state === "completed") {
104 return "completed";
105 }
106 return null;
107}
108
109function headlineForPhase(phase: AgentAwarenessPhase): string {
110 switch (phase) {
111 case "starting":
112 return "Starting agent";
113 case "running":
114 return "Agent is working";
115 case "waiting_for_approval":
116 return "Approval needed";
117 case "waiting_for_input":
118 return "Waiting for input";
119 case "completed":
120 return "Agent finished";
121 case "failed":
122 return "Agent failed";
123 case "stale":
124 return "Update delayed";
125 }
126}
127
128function detailForPhase(
129 phase: AgentAwarenessPhase,
130 thread: ProjectThreadAwarenessInput["thread"],
131): string | undefined {
132 if (phase === "failed") {
133 return thread.session?.lastError ?? undefined;
134 }
135 if (phase === "completed") {
136 return "Review the completed task.";
137 }
138 if (phase === "running" && thread.session?.providerName) {
139 return `${thread.session.providerName} is active.`;
140 }
141 return undefined;
142}