Open History to see the last commit
1import type {
2 OrchestrationV2PendingBackgroundTask,
3 OrchestrationV2ProviderGoal,
4 ThreadId,
5} from "@t3tools/contracts";
6import type { EnvironmentThreadShell, ThreadStructure } from "@t3tools/client-runtime/state/shell";
7import {
8 presentPendingBackgroundWork,
9 presentProviderGoal,
10} from "@t3tools/client-runtime/state/thread-execution";
11import { childThreadInputsTitle } from "@t3tools/client-runtime/state/threads";
12import { formatDuration } from "@t3tools/shared/orchestrationTiming";
13import * as DateTime from "effect/DateTime";
14import {
15 CircleDashedIcon,
16 GitBranchIcon,
17 MessageCircleIcon,
18 ShieldQuestionIcon,
19 TargetIcon,
20 TerminalIcon,
21} from "lucide-react";
22import { Fragment } from "react";
23import { tickTextContent } from "~/lib/tickTextContent";
24import { cn } from "~/lib/utils";
25import { Button, InlineButton } from "../ui/button";
26import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip";
27import type { ComposerBannerStackItem } from "./ComposerBannerStack";
28import type { ThreadNowItem } from "./ThreadNow";
29
30/** Subagents waiting on the user, unless the parent is itself waiting on them. */
31export function childInputNowItem(input: {
32 readonly childThreadInputs: ReadonlyArray<EnvironmentThreadShell>;
33 readonly parentAwaitingUser: boolean;
34 readonly onOpenThread: (threadId: ThreadId) => void;
35}): ThreadNowItem | null {
36 const { childThreadInputs, onOpenThread } = input;
37 const first = childThreadInputs[0];
38 if (!first || input.parentAwaitingUser) return null;
39 return {
40 id: `child-input:${first.id}`,
41 // The only line at the end of the timeline that needs you gets the info color.
42 icon: first.hasPendingApprovals ? (
43 <ShieldQuestionIcon className="text-info-foreground" />
44 ) : (
45 <MessageCircleIcon className="text-info-foreground" />
46 ),
47 title: childThreadInputsTitle(childThreadInputs),
48 description: childThreadInputs.map((child, index) => (
49 <Fragment key={child.id}>
50 {index > 0 ? " " : null}
51 <span className="whitespace-nowrap">
52 <InlineButton tone="muted" onClick={() => onOpenThread(child.id)}>
53 {child.title}
54 </InlineButton>
55 {index < childThreadInputs.length - 1 ? "," : null}
56 </span>
57 </Fragment>
58 )),
59 actions: (
60 <InlineButton onClick={() => onOpenThread(first.id)}>
61 {first.hasPendingApprovals ? "Open approval" : "Open question"}
62 </InlineButton>
63 ),
64 };
65}
66
67/** The thread's goal, for the end of the timeline. */
68export function goalNowItem(input: {
69 readonly threadId: ThreadId;
70 readonly goal: OrchestrationV2ProviderGoal;
71 readonly isWorking: boolean;
72 readonly onSendCommand: (text: string, failureMessage: string) => Promise<void>;
73}): ThreadNowItem {
74 const { isWorking, onSendCommand } = input;
75 const presentation = presentProviderGoal(input.goal, isWorking);
76 return {
77 id: `goal:${input.threadId}`,
78 icon: <TargetIcon />,
79 title:
80 presentation.usage === null
81 ? presentation.title
82 : `${presentation.title} · ${presentation.usage}`,
83 description: presentation.objective,
84 // Clear stops goal work; Resume would stop unrelated work, so it waits.
85 actions: (
86 <>
87 {presentation.canResume && !isWorking ? (
88 <>
89 <InlineButton
90 onClick={() => void onSendCommand("/goal resume", "Failed to resume goal.")}
91 >
92 Resume
93 </InlineButton>
94 <span aria-hidden>·</span>
95 </>
96 ) : null}
97 <InlineButton
98 tone="muted"
99 onClick={() => void onSendCommand("/goal clear", "Failed to clear goal.")}
100 >
101 Clear
102 </InlineButton>
103 </>
104 ),
105 };
106}
107
108/** Background commands and subagents, for the end of the timeline. */
109export function backgroundWorkNowItem(input: {
110 readonly threadId: ThreadId;
111 readonly tasks: ReadonlyArray<OrchestrationV2PendingBackgroundTask>;
112 readonly turnItems: ThreadStructure["turnItems"];
113 readonly stopDisabled: boolean;
114 readonly stopping: boolean;
115 readonly onStop: () => Promise<void>;
116 readonly onOpenThread: (threadId: ThreadId) => void;
117}): ThreadNowItem | null {
118 const { onOpenThread } = input;
119 const presentation = presentPendingBackgroundWork(input.tasks);
120 if (presentation === null) return null;
121 const rows = presentation.items.map((task) => {
122 // Same key derivePendingBackgroundWork gives turn-item tasks; subagent items carry progress.
123 const item = input.turnItems.findLast(
124 (candidate) => (candidate.nativeItemRef?.nativeId || candidate.id) === task.taskId,
125 );
126 const subagent = item?.type === "subagent" ? item : undefined;
127 return {
128 task,
129 activity: subagent?.progress?.replace(/\s+/gu, " ").trim() || null,
130 startedAtMs: item?.startedAt ? DateTime.toEpochMillis(item.startedAt) : null,
131 updatedAtMs: subagent ? DateTime.toEpochMillis(subagent.updatedAt) : 0,
132 childThreadId: task.childThreadId ?? subagent?.childThreadId ?? undefined,
133 };
134 });
135 const [only] = rows.length === 1 ? rows : [];
136 const newest = rows
137 .filter((row) => row.activity)
138 .toSorted((left, right) => right.updatedAtMs - left.updatedAtMs)[0];
139 const isCommand = (row: (typeof rows)[number]) => row.task.kind !== "subagent";
140 const verb = presentation.waiting ? "Waiting on" : "Running";
141 const onlyThreadId = only?.childThreadId;
142 const startedAtMs = only?.startedAtMs ?? null;
143 return {
144 id: `background-work:${input.threadId}`,
145 icon: rows.every(isCommand) ? <TerminalIcon /> : <CircleDashedIcon />,
146 title: only ? (
147 <>
148 {verb} <span className={cn(isCommand(only) && "font-mono")}>{only.task.label}</span>
149 </>
150 ) : (
151 presentation.title
152 ),
153 description: only ? (
154 only.activity
155 ) : newest ? (
156 `${newest.task.label}: ${newest.activity}`
157 ) : rows.every(isCommand) ? (
158 <span className="font-mono">{rows.map((row) => row.task.label).join(", ")}</span>
159 ) : (
160 rows.map((row) => row.task.label).join(", ")
161 ),
162 actions: (
163 <>
164 {startedAtMs === null ? null : (
165 <span
166 ref={tickTextContent(
167 (nowMs) => formatDuration(Math.max(1_000, nowMs - startedAtMs)),
168 true,
169 )}
170 className="tabular-nums"
171 />
172 )}
173 {onlyThreadId === undefined ? null : (
174 <InlineButton tone="muted" onClick={() => onOpenThread(onlyThreadId)}>
175 Open
176 </InlineButton>
177 )}
178 <InlineButton disabled={input.stopDisabled} onClick={() => void input.onStop()}>
179 {input.stopping ? "Stopping..." : "Stop"}
180 </InlineButton>
181 </>
182 ),
183 };
184}
185
186/** The local checkout moved off the branch this thread last ran on. */
187export function branchMismatchBannerItem(input: {
188 readonly mismatchKey: string;
189 readonly mismatch: { readonly threadBranch: string; readonly currentBranch: string };
190 readonly restoreDisabled: boolean;
191 readonly restoring: boolean;
192 readonly onRestore: () => void;
193 readonly onDismiss: () => void;
194}): ComposerBannerStackItem {
195 const { mismatch } = input;
196 return {
197 id: `branch-mismatch:${input.mismatchKey}`,
198 variant: "info",
199 icon: <GitBranchIcon />,
200 title: (
201 <span className="flex min-w-0 items-baseline gap-1.5">
202 <span className="shrink-0 font-normal text-muted-foreground">Branch changed from</span>
203 <Tooltip>
204 <TooltipTrigger
205 render={
206 <code className="min-w-0 truncate font-medium text-foreground">
207 {mismatch.threadBranch}
208 </code>
209 }
210 />
211 <TooltipPopup side="top">
212 This thread last ran on {mismatch.threadBranch}. Sending will continue on{" "}
213 {mismatch.currentBranch}.
214 </TooltipPopup>
215 </Tooltip>
216 </span>
217 ),
218 actions: (
219 <Button size="xs" variant="ghost" disabled={input.restoreDisabled} onClick={input.onRestore}>
220 {input.restoring ? "Restoring..." : "Restore branch"}
221 </Button>
222 ),
223 dismissLabel: "Dismiss branch change notice",
224 onDismiss: input.onDismiss,
225 };
226}