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

threadPresentation.ts

129 lines3.9 KB

1import type { StatusTone } from "../../components/StatusPill";
2import type { OrchestrationLatestTurn, OrchestrationSession } from "@t3tools/contracts";
3import { EnvironmentThreadShell } from "@t3tools/client-runtime/state/shell";
4​
5export function threadSortValue(thread: EnvironmentThreadShell): number {
6 const candidate = Date.parse(thread.updatedAt ?? thread.createdAt);
7 return Number.isNaN(candidate) ? 0 : candidate;
8}
9​
10export type ThreadStatusKind =
11 | "pending-approval"
12 | "awaiting-input"
13 | "working"
14 | "connecting"
15 | "error"
16 | "plan-ready";
17​
18export interface ThreadStatusPresentation extends StatusTone {
19 readonly kind: ThreadStatusKind;
20 /** Foreground color for the leading status icon. */
21 readonly iconColor: string;
22 /** Background color for the leading status icon circle. */
23 readonly iconBackground: string;
24 /** Whether the indicator represents in-flight activity. */
25 readonly pulse: boolean;
27​
28/** Neutral icon colors for threads with no actionable status. */
29export const THREAD_STATUS_NEUTRAL_ICON = {
30 iconColor: "#8e8e93",
31 iconBackground: "rgba(142,142,147,0.22)",
32} as const;
33​
34function isLatestTurnSettled(
35 latestTurn: OrchestrationLatestTurn | null,
36 session: OrchestrationSession | null,
37): boolean {
38 if (!latestTurn?.startedAt) return false;
39 if (!latestTurn.completedAt) return false;
40 if (!session) return true;
41 return session.status !== "running";
43​
44/**
45 * Resolves the user-facing status of a thread, in priority order. Returns
46 * `null` for quiescent threads so rows stay free of "Idle"-style noise.
47 * Mirrors `resolveThreadStatusPill` in apps/web/src/components/Sidebar.logic.ts.
48 */
49export function resolveThreadStatus(
50 thread: EnvironmentThreadShell,
51): ThreadStatusPresentation | null {
52 if (thread.hasPendingApprovals) {
53 return {
54 kind: "pending-approval",
55 label: "Needs Approval",
56 pillClassName: "bg-amber-500/12 dark:bg-amber-500/16",
57 textClassName: "text-amber-700 dark:text-amber-300",
58 iconColor: "#ff9f0a",
59 iconBackground: "rgba(255,159,10,0.22)",
60 pulse: false,
61 };
62 }
63​
64 if (thread.hasPendingUserInput) {
65 return {
66 kind: "awaiting-input",
67 label: "Awaiting Input",
68 pillClassName: "bg-indigo-500/12 dark:bg-indigo-500/16",
69 textClassName: "text-indigo-700 dark:text-indigo-300",
70 iconColor: "#5e5ce6",
71 iconBackground: "rgba(94,92,230,0.22)",
72 pulse: false,
73 };
74 }
75​
76 if (thread.session?.status === "running") {
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 (thread.session?.status === "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 (thread.session?.status === "error" || thread.latestTurn?.state === "error") {
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 isLatestTurnSettled(thread.latestTurn, thread.session) &&
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;