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 /** Whether the indicator represents in-flight activity. */
18 readonly pulse: boolean;
19}
20
21function isLatestRunSettled(thread: EnvironmentThreadShell): boolean {
22 if (!thread.latestRun?.startedAt) return false;
23 if (!thread.latestRun.completedAt) return false;
24 return !threadRuntimeIsActive(thread.runtime);
25}
26
27/**
28 * Resolves the user-facing status of a thread, in priority order. Returns
29 * `null` for quiescent threads so rows stay free of "Idle"-style noise.
30 * Mirrors `resolveThreadStatusPill` in apps/web/src/components/Sidebar.logic.ts.
31 */
32export function resolveThreadStatus(
33 thread: EnvironmentThreadShell,
34): ThreadStatusPresentation | null {
35 if (thread.hasPendingApprovals) {
36 return {
37 kind: "pending-approval",
38 label: "Needs Approval",
39 pillClassName: "bg-warning",
40 textClassName: "text-warning-foreground",
41 pulse: false,
42 };
43 }
44
45 if (thread.hasPendingUserInput) {
46 return {
47 kind: "awaiting-input",
48 label: "Awaiting Input",
49 pillClassName: "bg-adaptive-indigo-500-a12-a16",
50 textClassName: "text-adaptive-indigo-600-300",
51 pulse: false,
52 };
53 }
54
55 const runtimeStatus = thread.runtime?.status;
56
57 if (runtimeStatus === "running" || runtimeStatus === "waiting") {
58 return {
59 kind: "working",
60 label: "Working",
61 pillClassName: "bg-adaptive-sky-500-a12-a16",
62 textClassName: "text-adaptive-sky-600-400",
63 pulse: true,
64 };
65 }
66
67 if (runtimeStatus === "preparing" || runtimeStatus === "queued" || runtimeStatus === "starting") {
68 return {
69 kind: "connecting",
70 label: "Connecting",
71 pillClassName: "bg-adaptive-sky-500-a12-a16",
72 textClassName: "text-adaptive-sky-600-400",
73 pulse: true,
74 };
75 }
76
77 if (runtimeStatus === "failed" || thread.latestRun?.status === "failed") {
78 return {
79 kind: "error",
80 label: "Error",
81 pillClassName: "bg-danger",
82 textClassName: "text-danger-foreground",
83 pulse: false,
84 };
85 }
86
87 const hasPlanReadyPrompt =
88 thread.interactionMode === "plan" &&
89 isLatestRunSettled(thread) &&
90 thread.hasActionableProposedPlan;
91 if (hasPlanReadyPrompt) {
92 return {
93 kind: "plan-ready",
94 label: "Plan Ready",
95 pillClassName: "bg-adaptive-violet-500-a12-a16",
96 textClassName: "text-adaptive-violet-600-400",
97 pulse: false,
98 };
99 }
100
101 return null;
102}