Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
137 changes: 73 additions & 64 deletions apps/web/src/components/chat/ChatComposer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1392,9 +1392,30 @@ const ComposerFooterPrimaryActions = memo(function ComposerFooterPrimaryActions(
compactBeforeSendTokens: number | null;
keepFullHistory: boolean;
onToggleKeepFullHistory: () => void;
/** Opens the file picker. Null hides the attach button. */
onAttachFiles: (() => void) | null;
}) {
return (
const leadingActions = (
<>
{props.onAttachFiles ? (
<Tooltip>
<TooltipTrigger
render={
<Button
type="button"
variant="ghost"
size="icon-sm"
onPointerDown={(event) => event.preventDefault()}
onClick={props.onAttachFiles}
aria-label="Attach files"
/>
}
>
<PaperclipIcon />
</TooltipTrigger>
<TooltipPopup>Attach files</TooltipPopup>
</Tooltip>
) : null}
{props.activeContextWindow ? (
<ContextWindowMeter
usage={props.activeContextWindow}
Expand All @@ -1406,36 +1427,39 @@ const ComposerFooterPrimaryActions = memo(function ComposerFooterPrimaryActions(
) : props.reserveContextWindowMeter ? (
<ContextWindowMeterPlaceholder />
) : null}
<ComposerPrimaryActions
compact={props.compact}
canOperateThread={props.canOperateThread}
pendingAction={props.pendingAction}
isRunning={props.isRunning}
canInterrupt={props.canInterrupt}
followUpBehavior={props.followUpBehavior}
alternateShortcutLabel={props.alternateShortcutLabel}
showPlanFollowUpPrompt={props.showPlanFollowUpPrompt}
promptHasText={props.promptHasText}
isSendBusy={props.isSendBusy}
sendDisabledReason={props.sendDisabledReason}
isConnecting={props.isConnecting}
isEnvironmentUnavailable={props.isEnvironmentUnavailable}
isPreparingWorktree={props.isPreparingWorktree}
hasSendableContent={props.hasSendableContent}
canResume={props.canResume}
preserveComposerFocusOnPointerDown={props.preserveComposerFocusOnPointerDown ?? false}
isEditingQueuedMessage={props.isEditingQueuedMessage}
onSubmitMessage={props.onSubmitMessage}
onResume={props.onResume}
onPreviousPendingQuestion={props.onPreviousPendingQuestion}
onInterrupt={props.onInterrupt}
onImplementPlanInNewThread={props.onImplementPlanInNewThread}
compactBeforeSendTokens={props.compactBeforeSendTokens}
keepFullHistory={props.keepFullHistory}
onToggleKeepFullHistory={props.onToggleKeepFullHistory}
/>
</>
);
return (
<ComposerPrimaryActions
leadingActions={leadingActions}
compact={props.compact}
canOperateThread={props.canOperateThread}
pendingAction={props.pendingAction}
isRunning={props.isRunning}
canInterrupt={props.canInterrupt}
followUpBehavior={props.followUpBehavior}
alternateShortcutLabel={props.alternateShortcutLabel}
showPlanFollowUpPrompt={props.showPlanFollowUpPrompt}
promptHasText={props.promptHasText}
isSendBusy={props.isSendBusy}
sendDisabledReason={props.sendDisabledReason}
isConnecting={props.isConnecting}
isEnvironmentUnavailable={props.isEnvironmentUnavailable}
isPreparingWorktree={props.isPreparingWorktree}
hasSendableContent={props.hasSendableContent}
canResume={props.canResume}
preserveComposerFocusOnPointerDown={props.preserveComposerFocusOnPointerDown ?? false}
isEditingQueuedMessage={props.isEditingQueuedMessage}
onSubmitMessage={props.onSubmitMessage}
onResume={props.onResume}
onPreviousPendingQuestion={props.onPreviousPendingQuestion}
onInterrupt={props.onInterrupt}
onImplementPlanInNewThread={props.onImplementPlanInNewThread}
compactBeforeSendTokens={props.compactBeforeSendTokens}
keepFullHistory={props.keepFullHistory}
onToggleKeepFullHistory={props.onToggleKeepFullHistory}
/>
);
});

// --------------------------------------------------------------------------
Expand Down Expand Up @@ -2474,6 +2498,9 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
names: new Set(),
});
const attachmentInputRef = useRef<HTMLInputElement>(null);
const openAttachmentPicker = useCallback(() => {
attachmentInputRef.current?.click();
}, []);
const composerFormRef = useRef<HTMLFormElement>(null);
const composerSurfaceRef = useRef<HTMLDivElement>(null);
const providerInputRejectedRef = useRef(false);
Expand Down Expand Up @@ -7616,43 +7643,25 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
className="flex shrink-0 flex-nowrap items-center justify-end gap-2"
>
{showComposerAttachAction ? (
<>
<input
ref={attachmentInputRef}
type="file"
multiple
className="hidden"
onChange={(event) => {
const files = Array.from(event.currentTarget.files ?? []);
event.currentTarget.value = "";
// Inserting a chip refocuses the editor after the draft renders;
// focusing synchronously here would report the editor's stale text
// over the prompt that was just written.
void addComposerAttachments(files).then((inserted) => {
if (!inserted) focusComposer();
});
}}
/>
<Tooltip>
<TooltipTrigger
render={
<Button
type="button"
variant="ghost"
size="icon-sm"
onPointerDown={(event) => event.preventDefault()}
onClick={() => attachmentInputRef.current?.click()}
aria-label="Attach files"
/>
}
>
<PaperclipIcon />
</TooltipTrigger>
<TooltipPopup>Attach files</TooltipPopup>
</Tooltip>
</>
<input
ref={attachmentInputRef}
type="file"
multiple
className="hidden"
onChange={(event) => {
const files = Array.from(event.currentTarget.files ?? []);
event.currentTarget.value = "";
// Inserting a chip refocuses the editor after the draft renders;
// focusing synchronously here would report the editor's stale text
// over the prompt that was just written.
void addComposerAttachments(files).then((inserted) => {
if (!inserted) focusComposer();
});
}}
/>
) : null}
<ComposerFooterPrimaryActions
onAttachFiles={showComposerAttachAction ? openAttachmentPicker : null}
compact={isComposerResting || isComposerPrimaryActionsCompact}
canOperateThread={canOperateThread}
activeContextWindow={
Expand Down
32 changes: 30 additions & 2 deletions apps/web/src/components/chat/ComposerPrimaryActions.test.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createElement } from "react";
import { createElement, type ComponentProps } from "react";
import { renderToStaticMarkup } from "react-dom/server";
import { afterEach, describe, expect, it, vi } from "vite-plus/test";

Expand Down Expand Up @@ -46,7 +46,10 @@ function renderPendingActions(isRunning: boolean) {
);
}

function renderSendButton(sendDisabledReason: string | null = null) {
function renderSendButton(
sendDisabledReason: string | null = null,
overrides: Partial<ComponentProps<typeof ComposerPrimaryActions>> = {},
) {
return renderToStaticMarkup(
createElement(ComposerPrimaryActions, {
compact: true,
Expand All @@ -65,6 +68,7 @@ function renderSendButton(sendDisabledReason: string | null = null) {
onPreviousPendingQuestion: () => {},
onInterrupt: () => {},
onImplementPlanInNewThread: () => {},
...overrides,
}),
);
}
Expand All @@ -90,6 +94,30 @@ describe("ComposerPrimaryActions", () => {
expect(renderPendingActions(false)).not.toContain('aria-label="Stop generation"');
});

it("puts the Compact chip ahead of the leading actions in reading and focus order", () => {
const markup = renderSendButton(null, {
compactBeforeSendTokens: 169_000,
leadingActions: createElement("button", { type: "button" }, "Attach"),
});

const chip = markup.indexOf("Compact 169k tokens of history before sending");
const attach = markup.indexOf("Attach</button>");
const send = markup.indexOf('aria-label="Compact and send"');
expect(chip).toBeGreaterThan(-1);
expect(attach).toBeGreaterThan(chip);
expect(send).toBeGreaterThan(attach);
});

it("keeps the leading actions before the send button when there is no chip", () => {
const markup = renderSendButton(null, {
leadingActions: createElement("button", { type: "button" }, "Attach"),
});

const attach = markup.indexOf("Attach</button>");
expect(attach).toBeGreaterThan(-1);
expect(markup.indexOf('aria-label="Submit message"')).toBeGreaterThan(attach);
});

it("renders stage artwork inside the send button when artwork identification is active", () => {
stageArtworkState.mode = "artwork";
stageArtworkState.variant = "nightly";
Expand Down
34 changes: 23 additions & 11 deletions apps/web/src/components/chat/ComposerPrimaryActions.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { memo, type MouseEventHandler, type PointerEventHandler } from "react";
import { memo, type MouseEventHandler, type PointerEventHandler, type ReactNode } from "react";
import { CheckIcon, ChevronDownIcon, ChevronLeftIcon, Minimize2Icon, PlayIcon } from "lucide-react";
import { CornerUpRight, ListPlus } from "lucide";
import { MorphIcon } from "~/components/MorphIcon";
Expand Down Expand Up @@ -56,6 +56,8 @@ interface ComposerPrimaryActionsProps {
/** The Compact chip is turned off, so the next send keeps full history. */
keepFullHistory?: boolean;
onToggleKeepFullHistory?: () => void;
/** Controls shown before the primary action. The Compact chip renders ahead of them. */
leadingActions?: ReactNode;
}

const formatPendingPrimaryActionLabel = (input: {
Expand Down Expand Up @@ -112,6 +114,7 @@ export const ComposerPrimaryActions = memo(function ComposerPrimaryActions({
compactBeforeSendTokens = null,
keepFullHistory = false,
onToggleKeepFullHistory,
leadingActions,
}: ComposerPrimaryActionsProps) {
const pointerFocusProps = preserveComposerFocusOnPointerDown
? { onPointerDown: preventPointerFocus }
Expand All @@ -130,6 +133,12 @@ export const ComposerPrimaryActions = memo(function ComposerPrimaryActions({
const stageBackdropVariant = useSidebarStageBackdropVariant(
environmentIdentificationMode === "artwork",
);
const withLeadingActions = (action: ReactNode) => (
<>
{leadingActions}
{action}
</>
);

const renderStopGenerationButton = (insidePendingAction: boolean) => (
<Tooltip key="interrupt">
Expand Down Expand Up @@ -159,7 +168,7 @@ export const ComposerPrimaryActions = memo(function ComposerPrimaryActions({
);

if (pendingAction) {
return (
return withLeadingActions(
<div className={cn("flex items-center justify-end", compact ? "gap-1.5" : "gap-2")}>
{canInterrupt ? renderStopGenerationButton(true) : null}
{pendingAction.questionIndex > 0 ? (
Expand Down Expand Up @@ -204,25 +213,25 @@ export const ComposerPrimaryActions = memo(function ComposerPrimaryActions({
questionIndex: pendingAction.questionIndex,
})}
</button>
</div>
</div>,
);
}

if (showPlanFollowUpPrompt && (promptHasText || !canResume)) {
if (promptHasText) {
return (
return withLeadingActions(
<button
type="submit"
className={cn(messageActionPillClassName, "h-9 sm:h-8", compact ? "px-3" : "px-4")}
{...pointerFocusProps}
disabled={isSendBusy || isSendDisabled || isConnecting || isEnvironmentUnavailable}
>
{isConnecting || isSendBusy ? "Sending..." : "Refine"}
</button>
</button>,
);
}

return (
return withLeadingActions(
<div data-chat-composer-implement-actions="true" className="flex items-center justify-end">
<button
type="submit"
Expand Down Expand Up @@ -260,12 +269,12 @@ export const ComposerPrimaryActions = memo(function ComposerPrimaryActions({
</MenuItem>
</MenuPopup>
</Menu>
</div>
</div>,
);
}

if (canInterrupt && !hasSendableContent && !isEditingQueuedMessage) {
return renderStopGenerationButton(false);
return withLeadingActions(renderStopGenerationButton(false));
}

const showResume = canResume && !hasSendableContent && !isEditingQueuedMessage;
Expand Down Expand Up @@ -360,10 +369,12 @@ export const ComposerPrimaryActions = memo(function ComposerPrimaryActions({
<TooltipPopup>{submitTooltip}</TooltipPopup>
</Tooltip>
);
if (compactTokens === null) return submit;
if (compactTokens === null) return withLeadingActions(submit);

// The chip's label changes width when toggled, so it leads the composer's right-aligned
// actions: only its own left edge moves, and the actions beside the send button stay put.
return (
<div data-chat-composer-compact-send="true" className="flex items-center gap-2">
<>
<Tooltip>
<TooltipTrigger
render={
Expand Down Expand Up @@ -393,7 +404,8 @@ export const ComposerPrimaryActions = memo(function ComposerPrimaryActions({
: `Next send compacts ${compactTokens} tokens first. Click to keep full history`}
</TooltipPopup>
</Tooltip>
{leadingActions}
{submit}
</div>
</>
);
});
Loading