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

ComposerPrimaryActions.tsx

402 lines14.7 KB

1import { memo, type MouseEventHandler, type PointerEventHandler } from "react";
2import { CheckIcon, ChevronDownIcon, ChevronLeftIcon, PlayIcon } from "lucide-react";
3import { CornerUpRight, ListPlus } from "lucide";
4import { MorphIcon } from "~/components/MorphIcon";
5import { useEnvironmentIdentificationMode } from "~/hooks/useSettings";
6import { formatContextWindowTokens } from "~/lib/contextWindow";
7import { cn } from "~/lib/utils";
8import { useShortcutModifierState } from "../../shortcutModifierState";
9import { StageBackdropButtonArt, useSidebarStageBackdropVariant } from "../SidebarStageBackdrop";
10import { Button } from "../ui/button";
11import { Menu, MenuItem, MenuPopup, MenuTrigger } from "../ui/menu";
12import { Spinner } from "../ui/spinner";
13import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip";
14import { composerFloatingLayerProps } from "./composerEventScope";
15import {
16 alternateComposerDispatchAction,
17 resolveComposerDispatchMode,
18} from "@t3tools/client-runtime/state/composer-dispatch";
19​
20interface PendingActionState {
21 questionIndex: number;
22 isLastQuestion: boolean;
23 canAdvance: boolean;
24 isResponding: boolean;
25 isComplete: boolean;
27​
28interface ComposerPrimaryActionsProps {
29 compact: boolean;
30 canOperateThread: boolean;
31 pendingAction: PendingActionState | null;
32 /** The turn is running: sending steers or queues instead of starting a turn. */
33 isRunning: boolean;
34 /** Stop can reach a run, including one still preparing or starting. */
35 canInterrupt: boolean;
36 followUpBehavior?: "queue" | "steer";
37 alternateShortcutLabel?: string | null;
38 showPlanFollowUpPrompt: boolean;
39 promptHasText: boolean;
40 isSendBusy: boolean;
41 sendDisabledReason: string | null;
42 isConnecting: boolean;
43 isEnvironmentUnavailable: boolean;
44 isPreparingWorktree: boolean;
45 hasSendableContent: boolean;
46 canResume?: boolean;
47 preserveComposerFocusOnPointerDown?: boolean;
48 isEditingQueuedMessage?: boolean;
49 onSubmitMessage?: MouseEventHandler<HTMLButtonElement>;
50 onResume?: () => void;
51 onPreviousPendingQuestion: () => void;
52 onInterrupt: () => void;
53 onImplementPlanInNewThread: () => void;
54 /** Tokens a stale session would re-read. When set, Enter compacts first and the button says so. */
55 compactBeforeSendTokens?: number | null;
56 onSendWithFullHistory?: () => void;
58​
59const formatPendingPrimaryActionLabel = (input: {
60 compact: boolean;
61 isLastQuestion: boolean;
62 isResponding: boolean;
63 questionIndex: number;
64}) => {
65 if (input.isResponding) {
66 return "Submitting...";
67 }
68 if (input.compact) {
69 return input.isLastQuestion ? "Submit" : "Next";
70 }
71 if (!input.isLastQuestion) {
72 return "Next question";
73 }
74 return input.questionIndex > 0 ? "Submit answers" : "Submit answer";
75};
76​
77// The composer's labeled primary actions (Submit, Refine, Implement) share the send button's
78// message-action pill, so they are composer-owned buttons rather than restyled Buttons.
79const messageActionPillClassName =
80 "inline-flex shrink-0 cursor-pointer items-center justify-center gap-1.5 whitespace-nowrap rounded-full bg-message-action font-medium text-base text-message-action-foreground shadow-xs shadow-message-action/24 outline-none hover:bg-message-action-hover focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-1 focus-visible:ring-offset-background disabled:pointer-events-none disabled:opacity-64 disabled:shadow-none sm:text-sm";
81​
82const preventPointerFocus: PointerEventHandler<HTMLElement> = (event) => {
83 event.preventDefault();
84};
85​
86export const ComposerPrimaryActions = memo(function ComposerPrimaryActions({
87 compact,
88 canOperateThread,
89 pendingAction,
90 isRunning,
91 canInterrupt,
92 followUpBehavior = "steer",
93 alternateShortcutLabel = null,
94 showPlanFollowUpPrompt,
95 promptHasText,
96 isSendBusy,
97 sendDisabledReason,
98 isConnecting,
99 isEnvironmentUnavailable,
100 isPreparingWorktree,
101 hasSendableContent,
102 canResume = false,
103 preserveComposerFocusOnPointerDown = false,
104 isEditingQueuedMessage = false,
105 onSubmitMessage,
106 onResume,
107 onPreviousPendingQuestion,
108 onInterrupt,
109 onImplementPlanInNewThread,
110 compactBeforeSendTokens = null,
111 onSendWithFullHistory,
112}: ComposerPrimaryActionsProps) {
113 const pointerFocusProps = preserveComposerFocusOnPointerDown
114 ? { onPointerDown: preventPointerFocus }
115 : undefined;
116 const environmentIdentificationMode = useEnvironmentIdentificationMode();
117 const shortcutModifiers = useShortcutModifierState();
118 const isQueuing =
119 !isEditingQueuedMessage &&
120 resolveComposerDispatchMode({
121 running: isRunning,
122 activeTurnDefault: followUpBehavior,
123 alternateModifier: shortcutModifiers.metaKey || shortcutModifiers.ctrlKey,
124 }) === "queue";
125 const alternateAction = alternateComposerDispatchAction(followUpBehavior);
126 const isSendDisabled = !canOperateThread || sendDisabledReason !== null;
127 const stageBackdropVariant = useSidebarStageBackdropVariant(
128 environmentIdentificationMode === "artwork",
129 );
130​
131 const renderStopGenerationButton = (insidePendingAction: boolean) => (
132 <Tooltip key="interrupt">
133 <TooltipTrigger
134 render={
135 <button
136 type="button"
137 className={cn(
138 "flex cursor-pointer items-center justify-center rounded-full bg-destructive/90 text-white shadow-xs shadow-destructive/24 inset-shadow-control-highlight transition-all duration-150 hover:bg-destructive hover:scale-105 active:inset-shadow-control-pressed active:shadow-none [&_svg]:pointer-events-none",
139 insidePendingAction ? "size-8 sm:size-7" : "size-8 sm:h-8 sm:w-8",
140 )}
141 {...pointerFocusProps}
142 disabled={!canOperateThread}
143 onClick={() => {
144 if (canOperateThread) onInterrupt();
145 }}
146 aria-label="Stop generation"
147 />
148 }
149 >
150 <svg width="12" height="12" viewBox="0 0 12 12" fill="currentColor" aria-hidden="true">
151 <rect x="2" y="2" width="8" height="8" rx="1.5" />
152 </svg>
153 </TooltipTrigger>
154 <TooltipPopup>Interrupt</TooltipPopup>
155 </Tooltip>
156 );
157​
158 if (pendingAction) {
159 return (
160 <div className={cn("flex items-center justify-end", compact ? "gap-1.5" : "gap-2")}>
161 {canInterrupt ? renderStopGenerationButton(true) : null}
162 {pendingAction.questionIndex > 0 ? (
163 compact ? (
164 <Button
165 size="icon-sm"
166 variant="outline"
167 {...pointerFocusProps}
168 onClick={onPreviousPendingQuestion}
169 disabled={pendingAction.isResponding}
170 aria-label="Previous question"
171 >
172 <ChevronLeftIcon className="size-3.5" />
173 </Button>
174 ) : (
175 <Button
176 size="sm"
177 variant="outline"
178 {...pointerFocusProps}
179 onClick={onPreviousPendingQuestion}
180 disabled={pendingAction.isResponding}
181 >
182 Previous
183 </Button>
184 )
185 ) : null}
186 <button
187 type="submit"
188 className={cn(messageActionPillClassName, "h-8 sm:h-7", compact ? "px-3" : "px-4")}
189 {...pointerFocusProps}
190 disabled={
191 !canOperateThread ||
192 isEnvironmentUnavailable ||
193 pendingAction.isResponding ||
194 (pendingAction.isLastQuestion ? !pendingAction.isComplete : !pendingAction.canAdvance)
195 }
196 >
197 {formatPendingPrimaryActionLabel({
198 compact,
199 isLastQuestion: pendingAction.isLastQuestion,
200 isResponding: pendingAction.isResponding,
201 questionIndex: pendingAction.questionIndex,
202 })}
203 </button>
204 </div>
205 );
206 }
207​
208 if (showPlanFollowUpPrompt && (promptHasText || !canResume)) {
209 if (promptHasText) {
210 return (
211 <button
212 type="submit"
213 className={cn(messageActionPillClassName, "h-9 sm:h-8", compact ? "px-3" : "px-4")}
214 {...pointerFocusProps}
215 disabled={isSendBusy || isSendDisabled || isConnecting || isEnvironmentUnavailable}
216 >
217 {isConnecting || isSendBusy ? "Sending..." : "Refine"}
218 </button>
219 );
220 }
221​
222 return (
223 <div data-chat-composer-implement-actions="true" className="flex items-center justify-end">
224 <button
225 type="submit"
226 className={cn(messageActionPillClassName, "h-9 rounded-r-none px-4 sm:h-8")}
227 {...pointerFocusProps}
228 disabled={isSendBusy || isSendDisabled || isConnecting || isEnvironmentUnavailable}
229 >
230 {isConnecting || isSendBusy ? "Sending..." : "Implement"}
231 </button>
232 <Menu>
233 <MenuTrigger
234 render={
235 <button
236 type="button"
237 className={cn(
238 messageActionPillClassName,
239 "h-9 rounded-l-none border-l border-message-action-foreground/20 px-2 sm:h-8",
240 )}
241 aria-label="Implementation actions"
242 {...pointerFocusProps}
243 disabled={isSendBusy || isSendDisabled || isConnecting || isEnvironmentUnavailable}
244 />
245 }
246 >
247 <ChevronDownIcon className="size-3.5" />
248 </MenuTrigger>
249 <MenuPopup align="end" side="top" {...composerFloatingLayerProps}>
250 <MenuItem
251 disabled={isSendBusy || isSendDisabled || isConnecting || isEnvironmentUnavailable}
252 onClick={() => {
253 if (canOperateThread) onImplementPlanInNewThread();
254 }}
255 >
256 Implement in a new thread
257 </MenuItem>
258 </MenuPopup>
259 </Menu>
260 </div>
261 );
262 }
263​
264 if (canInterrupt && !hasSendableContent && !isEditingQueuedMessage) {
265 return renderStopGenerationButton(false);
266 }
267​
268 const showResume = canResume && !hasSendableContent && !isEditingQueuedMessage;
269 const sendBlocked = isSendBusy || isSendDisabled || isConnecting || isEnvironmentUnavailable;
270​
271 if (compactBeforeSendTokens !== null && !showResume && !isEditingQueuedMessage) {
272 const tokens = formatContextWindowTokens(compactBeforeSendTokens);
273 return (
274 <div data-chat-composer-compact-send="true" className="flex items-center justify-end">
275 <Tooltip>
276 <TooltipTrigger
277 render={
278 <button
279 type="submit"
280 className={cn(
281 messageActionPillClassName,
282 "h-9 rounded-r-none sm:h-8",
283 compact ? "px-3" : "px-4",
284 )}
285 {...pointerFocusProps}
286 onClick={onSubmitMessage}
287 disabled={sendBlocked || !hasSendableContent}
288 />
289 }
290 >
291 {isConnecting || isSendBusy ? "Sending..." : "Compact and send"}
292 </TooltipTrigger>
293 <TooltipPopup>Summarize {tokens} tokens of history, then send</TooltipPopup>
294 </Tooltip>
295 <Menu>
296 <MenuTrigger
297 render={
298 <button
299 type="button"
300 className={cn(
301 messageActionPillClassName,
302 "h-9 rounded-l-none border-l border-message-action-foreground/20 px-2 sm:h-8",
303 )}
304 aria-label="Send options"
305 {...pointerFocusProps}
306 disabled={sendBlocked || !hasSendableContent}
307 />
308 }
309 >
310 <ChevronDownIcon className="size-3.5" />
311 </MenuTrigger>
312 <MenuPopup align="end" side="top" {...composerFloatingLayerProps}>
313 <MenuItem disabled={sendBlocked} onClick={onSendWithFullHistory}>
314 Send with full history ({tokens} tokens)
315 </MenuItem>
316 </MenuPopup>
317 </Menu>
318 </div>
319 );
320 }
321​
322 const submitLabel = showResume
323 ? "Resume thread"
324 : isEditingQueuedMessage
325 ? "Update queued message"
326 : isQueuing
327 ? "Queue message"
328 : isRunning
329 ? "Steer message"
330 : "Submit message";
331 const submitStatus = isEnvironmentUnavailable
332 ? "Environment disconnected"
333 : (sendDisabledReason ??
334 (isConnecting
335 ? "Connecting"
336 : isPreparingWorktree
337 ? "Preparing worktree"
338 : isSendBusy
339 ? isEditingQueuedMessage
340 ? "Updating queued message"
341 : "Submitting message"
342 : null));
343 const submitTooltip =
344 submitStatus ??
345 (isRunning && !isEditingQueuedMessage
346 ? `Click to ${followUpBehavior}, Ctrl/⌘-click${alternateShortcutLabel ? ` or ${alternateShortcutLabel}` : ""} to ${alternateAction}`
347 : submitLabel);
348​
349 const sendButton = (
350 <button
351 type={showResume ? "button" : "submit"}
352 className={cn(
353 "relative isolate flex h-9 w-9 items-center justify-center overflow-hidden rounded-full shadow-xs transition-all duration-150 enabled:cursor-pointer enabled:inset-shadow-control-highlight hover:scale-105 active:inset-shadow-control-pressed active:shadow-none disabled:pointer-events-none disabled:opacity-64 disabled:shadow-none disabled:hover:scale-100 sm:h-8 sm:w-8 [&_svg]:pointer-events-none",
354 stageBackdropVariant
355 ? "bg-transparent text-white enabled:shadow-black/24 enabled:hover:brightness-110"
356 : "bg-message-action text-message-action-foreground enabled:shadow-message-action/24 hover:bg-message-action-hover",
357 )}
358 {...pointerFocusProps}
359 onClick={showResume ? onResume : onSubmitMessage}
360 disabled={
361 isSendBusy ||
362 isSendDisabled ||
363 isConnecting ||
364 isEnvironmentUnavailable ||
365 (!hasSendableContent && !showResume)
366 }
367 aria-label={submitStatus ?? submitLabel}
368 >
369 {stageBackdropVariant ? (
370 <span className="pointer-events-none absolute inset-0 -z-10" aria-hidden="true">
371 <StageBackdropButtonArt variant={stageBackdropVariant} />
372 </span>
373 ) : null}
374 {isConnecting || isSendBusy ? (
375 <Spinner size="sm" aria-hidden="true" />
376 ) : showResume ? (
377 <PlayIcon className="size-4 fill-current" aria-hidden="true" />
378 ) : isEditingQueuedMessage ? (
379 <CheckIcon className="size-4" aria-hidden="true" />
380 ) : isRunning ? (
381 <MorphIcon className="size-4" icon={isQueuing ? ListPlus : CornerUpRight} />
382 ) : (
383 <svg width="14" height="14" viewBox="0 0 14 14" fill="none" aria-hidden="true">
384 <path
385 d="M7 11.5V2.5M7 2.5L3 6.5M7 2.5L11 6.5"
386 stroke="currentColor"
387 strokeWidth="1.8"
388 strokeLinecap="round"
389 strokeLinejoin="round"
390 />
391 </svg>
392 )}
393 </button>
394 );
395​
396 return (
397 <Tooltip key="submit">
398 <TooltipTrigger render={<span className="inline-flex" />}>{sendButton}</TooltipTrigger>
399 <TooltipPopup>{submitTooltip}</TooltipPopup>
400 </Tooltip>
401 );
402});