Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
13 changes: 11 additions & 2 deletions frontend/components/chat/composer-prefill-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,17 @@ import { createContext, useContext } from "react";
* it. SessionView provides it; surfaces outside the composer (e.g. a conflicted
* workpiece proposal offering "Ask agent to redo") consume it. Null outside a
* session - the standalone artifacts page has no composer, so the affordance
* that depends on it hides itself. */
export type PrefillComposer = (text: string) => void;
* that depends on it hides itself. The text replaces the draft unless `append`
* asks to add it under a non-empty one (diff comments joining a half-written reply). */
export type PrefillComposer = (text: string, options?: { readonly append?: boolean }) => void;

/** One prefill request as the composer receives it. The `nonce` makes each request
* re-apply even when the text repeats. */
export interface ComposerPrefill {
readonly text: string;
readonly nonce: number;
readonly append?: boolean;
}

const ComposerPrefillContext = createContext<PrefillComposer | null>(null);

Expand Down
18 changes: 11 additions & 7 deletions frontend/components/chat/composer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import { ThreadErrorBanner, type ThreadErrorResend } from "@/components/session-
import { ComposerAttachmentRow, ComposerUploadNotice } from "@/components/pro/composer-attachments";
import { ComposerAddButton } from "@/components/pro/composer-panel/composer-panel";
import { composerPlaceholder, getComposerAction } from "@/components/chat/composer-model";
import type { ComposerPrefill } from "@/components/chat/composer-prefill-context";
import { useIsMobile } from "@/hooks/use-is-mobile";
import { cx as cn } from "@/utils/cx";
type Variant = "hero" | "compact";
Expand Down Expand Up @@ -160,8 +161,9 @@ export type ComposerProps = {
/** Externally seed the composer with a ready-to-send message and focus it. The
* `nonce` makes each request re-apply even when the text repeats (e.g. a second
* "Ask agent to redo"); the text replaces the current draft so the user can send
* or edit it. Absent leaves the composer fully user-driven. */
prefill?: { readonly text: string; readonly nonce: number } | null;
* or edit it, or with `append` goes under a non-empty draft. Absent leaves the
* composer fully user-driven. */
prefill?: ComposerPrefill | null;
/** The status tray under the card (location, branch, project, engine, spend, context). Compact only. */
tab?: ReactNode;
/** The permission chip for the footer's second column. Compact only. */
Expand Down Expand Up @@ -232,20 +234,22 @@ export function Composer({
else window.localStorage.removeItem(key);
}, [draftKey, value]);
// External prefill (e.g. "Ask agent to redo" on a conflicted proposal): apply
// once per nonce so a repeat request still lands, replacing the draft and moving
// focus + caret to the end so the message is one keystroke from sending.
// once per nonce so a repeat request still lands, replacing the draft (or adding
// under it, for an `append` request) and moving focus + caret to the end so the
// message is one keystroke from sending.
const rootRef = useRef<HTMLDivElement>(null);
const appliedPrefillNonce = useRef(0);
useEffect(() => {
if (!prefill || prefill.nonce === appliedPrefillNonce.current) return;
appliedPrefillNonce.current = prefill.nonce;
setValue(prefill.text);
const next = prefill.append && value.trim() ? `${value.trimEnd()}\n\n${prefill.text}` : prefill.text;
setValue(next);
const textarea = rootRef.current?.querySelector("textarea");
if (textarea) {
textarea.focus();
textarea.setSelectionRange(prefill.text.length, prefill.text.length);
textarea.setSelectionRange(next.length, next.length);
}
}, [prefill]);
}, [prefill, value]);
// Single fixed engine here; there is no setter (this composer serves replies -
// a thread is pinned to one engine - and the no-sandbox Chat surface). Engine
// SELECTION for a new task lives in NewTaskComposer. Kept as state so `engineProp`
Expand Down
3 changes: 2 additions & 1 deletion frontend/components/chat/conversation-composer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { useMemo, useRef, useState } from "react";
import type { PendingApproval } from "@/components/chat/approval-state";
import type { CommandCatalogState } from "@/components/chat/canonical-timeline";
import type { ComposerSubmit } from "@/components/chat/composer";
import type { ComposerPrefill } from "@/components/chat/composer-prefill-context";
import type { AssistantIdentity, Turn } from "@/components/chat/conversation";
import { toGatewayChildSession } from "@/components/chat/gateway-children";
import { latestThreadContext } from "@/components/chat/native-events";
Expand Down Expand Up @@ -115,7 +116,7 @@ export function ConversationComposer({
onDismissHandoffNotice?: () => void;
engineUnavailable?: boolean;
engineUnavailableMessage?: string;
prefill?: { readonly text: string; readonly nonce: number } | null;
prefill?: ComposerPrefill | null;
resourceMentions?: boolean;
repoRevisions?: Readonly<Record<string, string | null>>;
}) {
Expand Down
3 changes: 2 additions & 1 deletion frontend/components/chat/conversation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
} from "@/components/chat/canonical-timeline";
import { chatCitationsFromSteps } from "@/components/chat/chat-citations";
import type { ComposerSubmit } from "@/components/chat/composer";
import type { ComposerPrefill } from "@/components/chat/composer-prefill-context";
import { useEnabledEngineConfig } from "@/components/chat/engine-picker";
import { GatewayApprovalCard } from "@/components/chat/gateway-approval-card";
import { groupApprovalsByRun } from "@/components/chat/gateway-approval-state";
Expand Down Expand Up @@ -515,7 +516,7 @@ export const Conversation = memo(function Conversation({
runStartedAt?: string | null;
/** Externally seed the reply composer (e.g. "Ask agent to redo" on a conflicted
* proposal); each request carries a fresh nonce so repeats re-apply. */
prefill?: { readonly text: string; readonly nonce: number } | null;
prefill?: ComposerPrefill | null;
repoRevisions?: Readonly<Record<string, string | null>>;
resourceMentions?: boolean;
composerLocked?: boolean;
Expand Down
16 changes: 15 additions & 1 deletion frontend/components/chat/diff-pane.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
// sections recovered from the steps' own recorded patch text.

import { useMemo } from "react";
import { useComposerPrefill } from "@/components/chat/composer-prefill-context";
import {
buildTimelineFromCanonical,
shouldUseCanonicalTimeline,
Expand All @@ -25,6 +26,7 @@ const CANONICAL_TIMELINE = process.env.NEXT_PUBLIC_CANONICAL_TIMELINE === "1";
/** The slice of a conversation Turn this pane reads (structural, so
* conversation's Turn assigns without importing its module). */
export interface DiffTurn {
readonly run?: { readonly thread_id: string };
readonly steps: ApiStep[];
readonly live: boolean;
readonly native?: NativeSnapshot;
Expand Down Expand Up @@ -58,6 +60,18 @@ export function DiffPane({ turns }: { turns: readonly DiffTurn[] }) {
[turns],
);
const live = turns.some((turn) => turn.live);
// Line comments go back to the agent through the reply composer, so they are
// offered only inside a session that has one.
const prefill = useComposerPrefill();
const threadId = turns[0]?.run?.thread_id;
const review = useMemo(
// Appended, so sending the review never wipes a half-written reply.
() =>
prefill && threadId
? { key: threadId, onSend: (message: string) => prefill(message, { append: true }) }
: null,
[prefill, threadId],
);

if (files.length === 0) {
return (
Expand All @@ -71,7 +85,7 @@ export function DiffPane({ turns }: { turns: readonly DiffTurn[] }) {

return (
<div className="h-full overflow-y-auto pt-3" data-testid="diff-pane">
<FileDiffView files={files} patches={patches} />
<FileDiffView files={files} patches={patches} review={review} />
</div>
);
}
3 changes: 2 additions & 1 deletion frontend/components/chat/reply-composer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
type CommandCatalogState,
} from "@/components/chat/canonical-timeline";
import { Composer, type ComposerSubmit } from "@/components/chat/composer";
import type { ComposerPrefill } from "@/components/chat/composer-prefill-context";
import { ModelKeysContext } from "@/components/chat/catalog-model-picker";
import { StartFreePrompt, useStartFree } from "@/app/(workspace)/agent/new/start-free-prompt";
import type { ReactNode } from "react";
Expand Down Expand Up @@ -75,7 +76,7 @@ export function ReplyComposer({
/** Thread key for per-thread draft persistence (the root run id). */
draftKey?: string | null;
/** Externally seed the composer (conflicted-proposal "Ask agent to redo"). */
prefill?: { readonly text: string; readonly nonce: number } | null;
prefill?: ComposerPrefill | null;
enableMentions?: boolean;
enableUploads?: boolean;
repoRevisions?: Readonly<Record<string, string | null>>;
Expand Down
8 changes: 4 additions & 4 deletions frontend/components/chat/session-view.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ import {
import { useAgentsRailDeepLink } from "@/components/chat/use-agents-rail-deep-link";
import { terminalRunIdForThread } from "@/components/chat/terminal-run-state";
import type { TimelineArtifact } from "@/components/chat/timeline";
import { ComposerPrefillProvider } from "@/components/chat/composer-prefill-context";
import { type ComposerPrefill, ComposerPrefillProvider, type PrefillComposer } from "@/components/chat/composer-prefill-context";
import { ConversationAnnouncer } from "@/components/chat/conversation-announcer";
import { SessionLatestRunProvider } from "@/components/chat/session-run-context";
import { SessionThreadBreadcrumb } from "@/components/chat/session-thread-breadcrumb";
Expand Down Expand Up @@ -530,9 +530,9 @@ export function SessionView({ initialThread, initialOutline = null, initialRelat
// Lets a rail surface (e.g. a conflicted workpiece proposal) seed the reply
// composer. The nonce re-applies a repeat of the same text; SessionView owns it
// and hands the setter to the rail via context.
const [composerPrefill, setComposerPrefill] = useState<{ text: string; nonce: number } | null>(null);
const prefillComposer = useCallback((text: string) => {
setComposerPrefill((prev) => ({ text, nonce: (prev?.nonce ?? 0) + 1 }));
const [composerPrefill, setComposerPrefill] = useState<ComposerPrefill | null>(null);
const prefillComposer = useCallback<PrefillComposer>((text, options) => {
setComposerPrefill((prev) => ({ text, append: options?.append, nonce: (prev?.nonce ?? 0) + 1 }));
// Uncover the composer behind an OPEN sheet; closing a closed one collapses the wide layout.
if (sheetDialogOpen) closeSheet();
}, [sheetDialogOpen, closeSheet]);
Expand Down
Loading
Loading