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