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

threadPresentation.ts

129 lines3.8 KB

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