Open History to see the last commit
1import type { StatusTone } from "../../components/StatusPill";
2import {
3 threadRuntimeIsActive,
4 type EnvironmentThreadShell,
5} from "@t3tools/client-runtime/state/shell";
6
7export type ThreadStatusKind =
8 | "pending-approval"
9 | "awaiting-input"
10 | "working"
11 | "connecting"
12 | "error"
13 | "plan-ready";
14
15export interface ThreadStatusPresentation extends StatusTone {
16 readonly kind: ThreadStatusKind;
17 /** Foreground color for the leading status icon. */
18 readonly iconColor: string;
19 /** Background color for the leading status icon circle. */
20 readonly iconBackground: string;
21 /** Whether the indicator represents in-flight activity. */
22 readonly pulse: boolean;
23}
24
25function isLatestRunSettled(thread: EnvironmentThreadShell): boolean {
26 if (!thread.latestRun?.startedAt) return false;
27 if (!thread.latestRun.completedAt) return false;
28 return !threadRuntimeIsActive(thread.runtime);
29}
30
31/**
32 * Resolves the user-facing status of a thread, in priority order. Returns
33 * `null` for quiescent threads so rows stay free of "Idle"-style noise.
34 * Mirrors `resolveThreadStatusPill` in apps/web/src/components/Sidebar.logic.ts.
35 */
36export function resolveThreadStatus(
37 thread: EnvironmentThreadShell,
38): ThreadStatusPresentation | null {
39 if (thread.hasPendingApprovals) {
40 return {
41 kind: "pending-approval",
42 label: "Needs Approval",
43 pillClassName: "bg-adaptive-amber-500-a12-a16",
44 textClassName: "text-adaptive-amber-700-300",
45 iconColor: "#ff9f0a",
46 iconBackground: "rgba(255,159,10,0.22)",
47 pulse: false,
48 };
49 }
50
51 if (thread.hasPendingUserInput) {
52 return {
53 kind: "awaiting-input",
54 label: "Awaiting Input",
55 pillClassName: "bg-adaptive-indigo-500-a12-a16",
56 textClassName: "text-adaptive-indigo-700-300",
57 iconColor: "#5e5ce6",
58 iconBackground: "rgba(94,92,230,0.22)",
59 pulse: false,
60 };
61 }
62
63 const runtimeStatus = thread.runtime?.status;
64
65 if (runtimeStatus === "running" || runtimeStatus === "waiting") {
66 return {
67 kind: "working",
68 label: "Working",
69 pillClassName: "bg-adaptive-sky-500-a12-a16",
70 textClassName: "text-adaptive-sky-700-300",
71 iconColor: "#0a84ff",
72 iconBackground: "rgba(10,132,255,0.22)",
73 pulse: true,
74 };
75 }
76
77 if (runtimeStatus === "preparing" || runtimeStatus === "queued" || runtimeStatus === "starting") {
78 return {
79 kind: "connecting",
80 label: "Connecting",
81 pillClassName: "bg-adaptive-sky-500-a12-a16",
82 textClassName: "text-adaptive-sky-700-300",
83 iconColor: "#0a84ff",
84 iconBackground: "rgba(10,132,255,0.22)",
85 pulse: true,
86 };
87 }
88
89 if (runtimeStatus === "failed" || thread.latestRun?.status === "failed") {
90 return {
91 kind: "error",
92 label: "Error",
93 pillClassName: "bg-adaptive-rose-500-a12-a16",
94 textClassName: "text-adaptive-rose-700-300",
95 iconColor: "#ff453a",
96 iconBackground: "rgba(255,69,58,0.22)",
97 pulse: false,
98 };
99 }
100
101 const hasPlanReadyPrompt =
102 thread.interactionMode === "plan" &&
103 isLatestRunSettled(thread) &&
104 thread.hasActionableProposedPlan;
105 if (hasPlanReadyPrompt) {
106 return {
107 kind: "plan-ready",
108 label: "Plan Ready",
109 pillClassName: "bg-adaptive-violet-500-a12-a16",
110 textClassName: "text-adaptive-violet-700-300",
111 iconColor: "#bf5af2",
112 iconBackground: "rgba(191,90,242,0.22)",
113 pulse: false,
114 };
115 }
116
117 return null;
118}