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