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

ThreadActivityBanners.tsx

202 lines6.9 KB

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 };
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 // Clear stops goal work; Resume would stop unrelated work, so it waits.
72 actions: (
73 <>
74 {presentation.canResume && !isWorking ? (
75 <Button
76 size="xs"
77 variant="ghost"
78 onClick={() => void onSendCommand("/goal resume", "Failed to resume goal.")}
79 >
80 Resume
81 </Button>
82 ) : null}
83 <Button
84 size="xs"
85 variant="ghost"
86 onClick={() => void onSendCommand("/goal clear", "Failed to clear goal.")}
87 >
88 Clear
89 </Button>
90 </>
91 ),
92 };
94​
95export function backgroundWorkBannerItem(input: {
96 readonly threadId: ThreadId;
97 readonly tasks: ReadonlyArray<OrchestrationV2PendingBackgroundTask>;
98 readonly stopDisabled: boolean;
99 readonly stopping: boolean;
100 readonly onStop: () => Promise<void>;
101 readonly onOpenThread: (threadId: ThreadId) => void;
102}): ComposerBannerStackItem | null {
103 const { onOpenThread } = input;
104 const presentation = presentPendingBackgroundWork(input.tasks);
105 if (presentation === null) return null;
106 return {
107 id: `background-work:${input.threadId}`,
108 variant: "default",
109 priority: "activity",
110 icon: (
111 <span
112 className={cn(
113 "size-1.5 rounded-full bg-foreground",
114 presentation.waiting && "motion-safe:animate-status-pulse",
115 )}
116 aria-hidden="true"
117 />
118 ),
119 title: presentation.title,
120 description:
121 presentation.items.length === 1 && presentation.items[0]?.childThreadId === undefined
122 ? undefined
123 : presentation.items.map((item, index) => {
124 const childThreadId = item.childThreadId;
125 const separator = index < presentation.items.length - 1 ? "," : null;
126 return (
127 <Fragment key={item.taskId}>
128 {index > 0 ? " " : null}
129 {childThreadId === undefined ? (
130 <>
131 {item.label}
132 {separator}
133 </>
134 ) : (
135 <span className="whitespace-nowrap">
136 <InlineButton
137 tone="muted"
138 aria-label={`${presentation.items.length === 1 ? "Show" : "Open"} subagent ${item.label}`}
139 onClick={() => onOpenThread(childThreadId)}
140 >
141 {presentation.items.length === 1 ? "Show" : item.label}
142 </InlineButton>
143 {separator}
144 </span>
145 )}
146 </Fragment>
147 );
148 }),
149 actions: (
150 <Button
151 size="xs"
152 variant="ghost"
153 disabled={input.stopDisabled}
154 onClick={() => void input.onStop()}
155 >
156 {input.stopping ? "Stopping..." : "Stop"}
157 </Button>
158 ),
159 };
161​
162/** The local checkout moved off the branch this thread last ran on. */
163export function branchMismatchBannerItem(input: {
164 readonly mismatchKey: string;
165 readonly mismatch: { readonly threadBranch: string; readonly currentBranch: string };
166 readonly restoreDisabled: boolean;
167 readonly restoring: boolean;
168 readonly onRestore: () => void;
169 readonly onDismiss: () => void;
170}): ComposerBannerStackItem {
171 const { mismatch } = input;
172 return {
173 id: `branch-mismatch:${input.mismatchKey}`,
174 variant: "info",
175 icon: <GitBranchIcon />,
176 title: (
177 <span className="flex min-w-0 items-baseline gap-1.5">
178 <span className="shrink-0 font-normal text-muted-foreground">Branch changed from</span>
179 <Tooltip>
180 <TooltipTrigger
181 render={
182 <code className="min-w-0 truncate font-medium text-foreground">
183 {mismatch.threadBranch}
184 </code>
185 }
186 />
187 <TooltipPopup side="top">
188 This thread last ran on {mismatch.threadBranch}. Sending will continue on{" "}
189 {mismatch.currentBranch}.
190 </TooltipPopup>
191 </Tooltip>
192 </span>
193 ),
194 actions: (
195 <Button size="xs" variant="ghost" disabled={input.restoreDisabled} onClick={input.onRestore}>
196 {input.restoring ? "Restoring..." : "Restore branch"}
197 </Button>
198 ),
199 dismissLabel: "Dismiss branch change notice",
200 onDismiss: input.onDismiss,
201 };