Skip to content

Commit ec8a087

Browse files
perf(web): typing no longer re-renders the traits picker or attach button
The traits picker took the whole prompt to detect Ultrathink, so every keystroke re-rendered it. It now takes the three-state Ultrathink reading it renders from, and receives the live prompt only when a pick edits it. The attach button's add-attachments handler closed over the cursor through the non-stable insert helpers; it now calls their stable forms. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 9179178 commit ec8a087

7 files changed

Lines changed: 144 additions & 119 deletions

File tree

‎apps/web/src/components/chat/ChatComposer.tsx‎

Lines changed: 87 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,7 @@ import {
290290
renderProviderTraitsMenuContent,
291291
renderProviderTraitsPicker,
292292
} from "./composerProviderState";
293+
import { readPromptUltrathink } from "./TraitsPicker";
293294
import {
294295
providerSupportsManualCompaction,
295296
resolveContextWindowModelDisplayName,
@@ -2307,7 +2308,10 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
23072308
activePendingProgress.activeQuestion?.allowCustomAnswer !== false));
23082309
const composerFooterHasWideActions = showPlanFollowUpPrompt || activePendingProgress !== null;
23092310

2310-
const setPromptFromTraits = (nextPrompt: string) => {
2311+
const editPromptFromTraits = (edit: (prompt: string) => string) => {
2312+
const currentPrompt = getComposerDraft(composerDraftTarget)?.prompt ?? "";
2313+
const nextPrompt = edit(currentPrompt);
2314+
if (nextPrompt === currentPrompt) return;
23112315
if (nextPrompt === promptRef.current) {
23122316
scheduleComposerFocus();
23132317
return;
@@ -2319,7 +2323,8 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
23192323
setComposerTrigger(detectComposerTrigger(nextPrompt, nextPrompt.length));
23202324
scheduleComposerFocus();
23212325
};
2322-
const setPromptFromTraitsStable = useStableCallback(setPromptFromTraits);
2326+
const editPromptFromTraitsStable = useStableCallback(editPromptFromTraits);
2327+
const promptUltrathink = readPromptUltrathink(prompt);
23232328

23242329
const providerTraitsMenuContent = renderProviderTraitsMenuContent({
23252330
provider: selectedProvider,
@@ -2330,8 +2335,8 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
23302335
models: selectedProviderModels,
23312336
modelOptions: composerModelOptions?.[selectedInstanceId],
23322337
reportedModelSelection,
2333-
prompt,
2334-
onPromptChange: setPromptFromTraitsStable,
2338+
promptUltrathink,
2339+
onPromptEdit: editPromptFromTraitsStable,
23352340
planModeEnabled: settings.planModeEnabled,
23362341
});
23372342
const providerTraitsPickerInput = {
@@ -2343,8 +2348,8 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
23432348
models: selectedProviderModels,
23442349
modelOptions: composerModelOptions?.[selectedInstanceId],
23452350
reportedModelSelection,
2346-
prompt,
2347-
onPromptChange: setPromptFromTraitsStable,
2351+
promptUltrathink,
2352+
onPromptEdit: editPromptFromTraitsStable,
23482353
planModeEnabled: settings.planModeEnabled,
23492354
isComposerOwned: true,
23502355
} satisfies Parameters<typeof renderProviderTraitsPicker>[0];
@@ -4736,6 +4741,82 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
47364741
terminalOpen,
47374742
]);
47384743

4744+
const insertComposerText = (
4745+
text: string,
4746+
position: "cursor" | "end",
4747+
options?: {
4748+
ensureLeadingBoundary?: boolean;
4749+
citationCommentAnchor?: AssistantCitationSourceAnchor;
4750+
clipboardData?: DataTransfer;
4751+
},
4752+
): boolean => {
4753+
if (
4754+
text.length === 0 ||
4755+
isConnecting ||
4756+
activePendingIsResponding ||
4757+
(pendingUserInputs.length > 0 && !activePendingProgress?.activeQuestion) ||
4758+
projectSelectionRequired ||
4759+
(options?.citationCommentAnchor && !composerEditorRef.current)
4760+
) {
4761+
return false;
4762+
}
4763+
if (options?.clipboardData) {
4764+
text = importPastedComposerText(options.clipboardData, importContextFragment);
4765+
}
4766+
const prompt = promptRef.current;
4767+
const cursor = position === "cursor" ? readComposerSnapshot().expandedCursor : prompt.length;
4768+
const needsLeadingSpace =
4769+
(options?.ensureLeadingBoundary ?? false) &&
4770+
cursor > 0 &&
4771+
!/\s/.test(prompt[cursor - 1] ?? "");
4772+
const rangeEnd = extendReplacementRangeForTrailingSpace(prompt, cursor, text);
4773+
return applyPromptReplacement(
4774+
cursor,
4775+
rangeEnd,
4776+
needsLeadingSpace ? ` ${text}` : text,
4777+
options?.citationCommentAnchor
4778+
? {
4779+
citationComment: {
4780+
start: cursor + (needsLeadingSpace ? 1 : 0),
4781+
sourceAnchor: options.citationCommentAnchor,
4782+
insertedSpaces: { before: needsLeadingSpace, after: rangeEnd === cursor },
4783+
},
4784+
focusEditorAfterReplace: false,
4785+
}
4786+
: undefined,
4787+
);
4788+
};
4789+
const insertComposerTextStable = useStableCallback(insertComposerText);
4790+
4791+
const insertAttachmentReferences = (
4792+
references: ReadonlyArray<ComposerContextReference>,
4793+
selection?: { start: number; end: number },
4794+
): boolean => {
4795+
if (references.length === 0) return false;
4796+
// Question answers carry attachments beside the answer, never as chips. Falling back to
4797+
// the thread prompt here would hide the file behind a reference the question never shows.
4798+
if (questionAttachmentTarget) return false;
4799+
return insertAttachmentReferencesForDraft({
4800+
target: composerDraftTarget,
4801+
liveAttachmentTargetKey: attachmentTargetKeyRef.current,
4802+
references,
4803+
insertReferences: () => {
4804+
if (selection) {
4805+
const edit = inlineContextReferenceReplacement(promptRef.current, selection, references);
4806+
return applyPromptReplacementStable(edit.start, edit.end, edit.text);
4807+
}
4808+
const text = references.map(formatInlineContextReference).join(" ");
4809+
const inserted = insertComposerTextStable(`${text} `, "cursor", {
4810+
ensureLeadingBoundary: true,
4811+
});
4812+
if (!inserted) {
4813+
setPrompt(ensureInlineContextReferences(promptRef.current, references));
4814+
}
4815+
return true;
4816+
},
4817+
});
4818+
};
4819+
47394820
const addComposerAttachments = async (
47404821
files: File[],
47414822
options?: {
@@ -4944,33 +5025,6 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
49445025
});
49455026
};
49465027

4947-
const insertAttachmentReferences = (
4948-
references: ReadonlyArray<ComposerContextReference>,
4949-
selection?: { start: number; end: number },
4950-
): boolean => {
4951-
if (references.length === 0) return false;
4952-
// Question answers carry attachments beside the answer, never as chips. Falling back to
4953-
// the thread prompt here would hide the file behind a reference the question never shows.
4954-
if (questionAttachmentTarget) return false;
4955-
return insertAttachmentReferencesForDraft({
4956-
target: composerDraftTarget,
4957-
liveAttachmentTargetKey: attachmentTargetKeyRef.current,
4958-
references,
4959-
insertReferences: () => {
4960-
if (selection) {
4961-
const edit = inlineContextReferenceReplacement(promptRef.current, selection, references);
4962-
return applyPromptReplacement(edit.start, edit.end, edit.text);
4963-
}
4964-
const text = references.map(formatInlineContextReference).join(" ");
4965-
const inserted = insertComposerText(`${text} `, "cursor", { ensureLeadingBoundary: true });
4966-
if (!inserted) {
4967-
setPrompt(ensureInlineContextReferences(promptRef.current, references));
4968-
}
4969-
return true;
4970-
},
4971-
});
4972-
};
4973-
49745028
const removeComposerImage = (imageId: string) => {
49755029
const image = composerImagesRef.current.find((candidate) => candidate.id === imageId);
49765030
const referenced = collectInlineContextIds(promptRef.current).includes(
@@ -5075,53 +5129,6 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
50755129
() => resolveActiveComposerTrigger().trigger?.kind === "path",
50765130
);
50775131

5078-
const insertComposerText = (
5079-
text: string,
5080-
position: "cursor" | "end",
5081-
options?: {
5082-
ensureLeadingBoundary?: boolean;
5083-
citationCommentAnchor?: AssistantCitationSourceAnchor;
5084-
clipboardData?: DataTransfer;
5085-
},
5086-
): boolean => {
5087-
if (
5088-
text.length === 0 ||
5089-
isConnecting ||
5090-
activePendingIsResponding ||
5091-
(pendingUserInputs.length > 0 && !activePendingProgress?.activeQuestion) ||
5092-
projectSelectionRequired ||
5093-
(options?.citationCommentAnchor && !composerEditorRef.current)
5094-
) {
5095-
return false;
5096-
}
5097-
if (options?.clipboardData) {
5098-
text = importPastedComposerText(options.clipboardData, importContextFragment);
5099-
}
5100-
const prompt = promptRef.current;
5101-
const cursor = position === "cursor" ? readComposerSnapshot().expandedCursor : prompt.length;
5102-
const needsLeadingSpace =
5103-
(options?.ensureLeadingBoundary ?? false) &&
5104-
cursor > 0 &&
5105-
!/\s/.test(prompt[cursor - 1] ?? "");
5106-
const rangeEnd = extendReplacementRangeForTrailingSpace(prompt, cursor, text);
5107-
return applyPromptReplacement(
5108-
cursor,
5109-
rangeEnd,
5110-
needsLeadingSpace ? ` ${text}` : text,
5111-
options?.citationCommentAnchor
5112-
? {
5113-
citationComment: {
5114-
start: cursor + (needsLeadingSpace ? 1 : 0),
5115-
sourceAnchor: options.citationCommentAnchor,
5116-
insertedSpaces: { before: needsLeadingSpace, after: rangeEnd === cursor },
5117-
},
5118-
focusEditorAfterReplace: false,
5119-
}
5120-
: undefined,
5121-
);
5122-
};
5123-
const insertComposerTextStable = useStableCallback(insertComposerText);
5124-
51255132
const insertComposerTextAtEnd: ChatComposerHandle["insertTextAtEnd"] = (text, options) => {
51265133
const inserted = insertComposerText(text, "end", options);
51275134
if (inserted && isComposerCollapsedMobile) {

0 commit comments

Comments
 (0)