feat(web): recall sent prompts with the up arrow #9173
Mergedt3dotgg(GitHub profile, opens in a new tab)merged 13 commits into
main fromrefs/pull/9173/headmerged by t3dotgg(GitHub profile, opens in a new tab) View on GitHubgithub-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:trusted
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:L
- apps/web/src/components/chat/ChatComposer.tsx line 3008Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -2285,0 +2301,21 @@2301+ // on every streamed delta and ArrowUp is rare.2302+ const promptHistoryMessagesRef = useRef(promptHistoryMessages);2303+ promptHistoryMessagesRef.current = promptHistoryMessages;2304+2305+ const replacePromptFromHistory = useCallback(2306+ (nextPrompt: string) => {2307+ promptRef.current = nextPrompt;2308+ setComposerDraftPrompt(composerDraftTarget, nextPrompt);2309+ setComposerCursor(collapseExpandedComposerCursor(nextPrompt, nextPrompt.length));2310+ setComposerTrigger(null);2311+ setComposerHighlightedItemId(null);2312+ },2313+ [composerDraftTarget, promptRef, setComposerDraftPrompt],2314+ );2315+2316+ const navigatePromptHistory = useCallback(2317+ (direction: "backward" | "forward", event: KeyboardEvent): boolean => {2318+ if (event.shiftKey || event.altKey || event.metaKey || event.ctrlKey || event.isComposing) {2319+ return false;2320+ }2321+ if (isComposerApprovalState || pendingUserInputs.length > 0) return false;
- apps/web/src/components/ComposerPromptEditor.tsx line 955OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -922,0 +935,21 @@935+ * measuring a wrapped parent and misreporting the line.936+ */937+function caretLineRect(range: Range): DOMRect | null {938+ const collapsedRect = Array.from(range.getClientRects()).find((rect) => rect.height > 0);939+ if (collapsedRect) return collapsedRect;940+941+ const container = range.startContainer;942+ if (container.nodeType === Node.TEXT_NODE) {943+ const textNode = container as Text;944+ if (textNode.data.length === 0) return null;945+ const probeStart = Math.min(range.startOffset, textNode.data.length - 1);946+ const probeRange = document.createRange();947+ probeRange.setStart(textNode, probeStart);948+ probeRange.setEnd(textNode, probeStart + 1);949+ const probeRect = Array.from(probeRange.getClientRects()).find((rect) => rect.height > 0);950+ if (probeRect) return probeRect;951+ const boundingRect = probeRange.getBoundingClientRect();952+ return boundingRect.height > 0 ? boundingRect : null;953+ }954+955+ if (container instanceof HTMLElement && container.textContent?.length === 0) {
- apps/web/src/components/chat/composerPromptHistory.ts line 40Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -0,0 +15,21 @@15+const CLAUDE_ULTRATHINK_PREFIX = "Ultrathink:\n";16+17+export interface ComposerPromptHistoryMessage {18+ readonly id: string;19+ readonly role: string;20+ readonly text: string;21+}22+23+export interface ComposerPromptHistoryEntry {24+ readonly id: string;25+ readonly prompt: string;26+}27+28+/**29+ * Active recall. `entryId` is resolved against the current entries on every30+ * step, so a server ack replacing an optimistic message or an older page31+ * loading cannot move the position. `recalled` is the text put in the32+ * composer; once the composer no longer matches it, the user has edited or33+ * sent and browsing is over.34+ */35+export interface ComposerPromptHistoryPosition {apps/web/src/components/chat/ChatComposer.tsx line 3033Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -2285,0 +2318,21 @@2318+ if (event.shiftKey || event.altKey || event.metaKey || event.ctrlKey || event.isComposing) {2319+ return false;2320+ }2321+ if (isComposerApprovalState || pendingUserInputs.length > 0) return false;2322+ // A composer holding an image, file, or picked element is not empty.2323+ // Recalling text into it would send the old prompt with the new2324+ // attachment, which is never what ArrowUp meant.2325+ if (2326+ composerImagesRef.current.length > 0 ||2327+ composerFilesRef.current.length > 0 ||2328+ composerElementContextsRef.current.length > 02329+ ) {2330+ return false;2331+ }2332+ const editor = composerEditorRef.current;2333+ if (!editor?.isCaretOnVisualEdge(direction === "backward" ? "start" : "end")) {2334+ return false;2335+ }2336+ const step = stepComposerPromptHistory({2337+ direction,2338+ entries: buildComposerPromptHistoryEntries(promptHistoryMessagesRef.current),
- apps/web/src/components/chat/composerPromptHistory.ts line 139Resolvedby t3dotgg(GitHub profile, opens in a new tab)@@ -0,0 +83,21 @@83+ if (prompt.startsWith(CLAUDE_ULTRATHINK_PREFIX)) {84+ prompt = prompt.slice(CLAUDE_ULTRATHINK_PREFIX.length);85+ }86+87+ while (prompt.length > 0) {88+ const withoutReviewComments = stripTrailingReviewComments(prompt);89+ if (withoutReviewComments !== prompt) {90+ prompt = withoutReviewComments;91+ continue;92+ }93+ const previewAnnotation = extractTrailingPreviewAnnotation(prompt);94+ if (previewAnnotation.annotation) {95+ prompt = previewAnnotation.promptText;96+ continue;97+ }98+ const elementContexts = extractTrailingElementContexts(prompt);99+ if (elementContexts.contextCount > 0) {100+ prompt = elementContexts.promptText;101+ continue;102+ }103+ const terminalContexts = extractTrailingTerminalContexts(prompt);apps/web/src/components/chat/composerPromptHistory.ts line 124Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -0,0 +68,21 @@68+ return segments69+ .filter((segment) => segment.kind === "text")70+ .map((segment) => segment.text)71+ .join("")72+ .trimEnd();73+}74+75+/**76+ * Reduce a sent message to the text the user typed. Send-time appends77+ * (terminal and element context blocks, preview annotations, review78+ * comments, the Claude ultrathink prefix) are stripped so a recalled prompt79+ * never carries stale context from another turn.80+ */81+export function recallableComposerPrompt(messageText: string): string {82+ let prompt = messageText.trim();83+ if (prompt.startsWith(CLAUDE_ULTRATHINK_PREFIX)) {84+ prompt = prompt.slice(CLAUDE_ULTRATHINK_PREFIX.length);85+ }86+87+ while (prompt.length > 0) {88+ const withoutReviewComments = stripTrailingReviewComments(prompt);
- apps/web/src/components/chat/ChatComposer.tsx lines 3013 to 3021Resolvedby t3dotgg(GitHub profile, opens in a new tab)@@ -2285,0 +2311,21 @@2311+ setComposerHighlightedItemId(null);2312+ },2313+ [composerDraftTarget, promptRef, setComposerDraftPrompt],2314+ );2315+2316+ const navigatePromptHistory = useCallback(2317+ (direction: "backward" | "forward", event: KeyboardEvent): boolean => {2318+ if (event.shiftKey || event.altKey || event.metaKey || event.ctrlKey || event.isComposing) {2319+ return false;2320+ }2321+ if (isComposerApprovalState || pendingUserInputs.length > 0) return false;2322+ // A composer holding an image, file, or picked element is not empty.2323+ // Recalling text into it would send the old prompt with the new2324+ // attachment, which is never what ArrowUp meant.2325+ if (2326+ composerImagesRef.current.length > 0 ||2327+ composerFilesRef.current.length > 0 ||2328+ composerElementContextsRef.current.length > 02329+ ) {2330+ return false;2331+ }
- apps/web/src/components/chat/composerPromptHistory.ts lines 100 to 100OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -0,0 +80,21 @@80+ return segments81+ .map((segment) => (segment.kind === "text" ? segment.text : ""))82+ .join("")83+ .trimEnd();84+}85+86+/**87+ * Inline terminal chips are sent as `@terminal-1:12-13` labels in the text88+ * with their content in the trailing block. Once the block is stripped the89+ * label points at nothing, so remove it too. Block headers look like90+ * `Terminal 1 lines 12-13`.91+ */92+function stripInlineTerminalLabels(prompt: string, headers: ReadonlyArray<string>): string {93+ let result = prompt;94+ for (const header of headers) {95+ const match = /^(.+?) lines? (\d+(?:-\d+)?)$/.exec(header);96+ if (!match) continue;97+ const label = match[1]!.trim().toLowerCase().replace(/\s+/g, "-");98+ result = result.split(`@${label}:${match[2]}`).join("");99+ }100+ return result.replace(/[ \t]{2,}/g, " ").replace(/ +$/gm, "");apps/web/src/components/chat/composerPromptHistory.ts lines 98 to 98OutdatedResolvedby t3dotgg(GitHub profile, opens in a new tab)@@ -0,0 +78,21 @@78+ }79+ if (!changed) return prompt;80+ return segments81+ .map((segment) => (segment.kind === "text" ? segment.text : ""))82+ .join("")83+ .trimEnd();84+}85+86+/**87+ * Inline terminal chips are sent as `@terminal-1:12-13` labels in the text88+ * with their content in the trailing block. Once the block is stripped the89+ * label points at nothing, so remove it too. Block headers look like90+ * `Terminal 1 lines 12-13`.91+ */92+function stripInlineTerminalLabels(prompt: string, headers: ReadonlyArray<string>): string {93+ let result = prompt;94+ for (const header of headers) {95+ const match = /^(.+?) lines? (\d+(?:-\d+)?)$/.exec(header);96+ if (!match) continue;97+ const label = match[1]!.trim().toLowerCase().replace(/\s+/g, "-");98+ result = result.split(`@${label}:${match[2]}`).join("");
- apps/web/src/components/ComposerPromptEditor.tsx lines 984 to 994Resolvedby t3dotgg(GitHub profile, opens in a new tab)@@ -922,0 +949,21 @@949+ const probeRect = Array.from(probeRange.getClientRects()).find((rect) => rect.height > 0);950+ if (probeRect) return probeRect;951+ const boundingRect = probeRange.getBoundingClientRect();952+ return boundingRect.height > 0 ? boundingRect : null;953+ }954+955+ if (!(container instanceof HTMLElement)) return null;956+ // The caret sits between the paragraph's children, which is where Lexical957+ // puts it next to an inline chip. Measure the neighbouring child.958+ const neighbour =959+ container.childNodes[Math.max(0, range.startOffset - 1)] ??960+ container.childNodes[range.startOffset];961+ if (neighbour instanceof HTMLElement) {962+ const neighbourRect = neighbour.getBoundingClientRect();963+ if (neighbourRect.height > 0) return neighbourRect;964+ } else if (neighbour instanceof Text && neighbour.data.length > 0) {965+ const probeRange = document.createRange();966+ probeRange.selectNodeContents(neighbour);967+ const probeRect = Array.from(probeRange.getClientRects()).find((rect) => rect.height > 0);968+ if (probeRect) return probeRect;969+ }
- apps/web/src/components/chat/composerPromptHistory.ts line 56Resolvedby t3dotgg(GitHub profile, opens in a new tab)@@ -0,0 +35,21 @@35+ * loading cannot move the position. `recalled` is the text put in the36+ * composer; once the composer no longer matches it, the user has edited or37+ * sent and browsing is over.38+ */39+export interface ComposerPromptHistoryPosition {40+ readonly entryId: string;41+ readonly recalled: string;42+}43+44+/**45+ * Prefer the id. A consecutive duplicate collapse can retire the recalled46+ * id while the same text lives on under a newer one, so fall back to the47+ * newest entry with matching text.48+ */49+function findActive(50+ entries: ReadonlyArray<ComposerPromptHistoryEntry>,51+ position: ComposerPromptHistoryPosition,52+): number {53+ const byId = entries.findIndex((entry) => entry.id === position.entryId);54+ if (byId >= 0) return byId;55+ return entries.findLastIndex((entry) => entry.prompt === position.recalled);apps/web/src/components/chat/ChatComposer.tsx line 3031Resolvedby t3dotgg(GitHub profile, opens in a new tab)@@ -2341,0 +2375,21 @@2375+ return false;2376+ }2377+ if (isComposerApprovalState || pendingUserInputs.length > 0) return false;2378+ // A composer holding an image, file, picked element, preview2379+ // annotation, or review comment is not empty. Recalling text into it2380+ // would send the old prompt with the new context, which is never what2381+ // ArrowUp meant.2382+ if (2383+ composerImagesRef.current.length > 0 ||2384+ composerFilesRef.current.length > 0 ||2385+ composerElementContextsRef.current.length > 0 ||2386+ composerPreviewAnnotations.length > 0 ||2387+ composerReviewComments.length > 02388+ ) {2389+ return false;2390+ }2391+ const editor = composerEditorRef.current;2392+ if (!editor?.isCaretOnVisualEdge(direction === "backward" ? "start" : "end")) {2393+ return false;2394+ }2395+ const step = stepComposerPromptHistory({
- apps/web/src/components/chat/composerPromptHistory.ts line 106Resolvedby t3dotgg(GitHub profile, opens in a new tab)@@ -0,0 +86,21 @@86+/**87+ * Inline terminal chips are sent as `@terminal-1:12-13` labels in the text88+ * with their content in the trailing block. Once the block is stripped the89+ * label points at nothing, so remove it too. Each block entry removes one90+ * label (the first match) and the single space beside it. Nothing else in91+ * the prompt is touched, so indented code and typed labels survive. Block92+ * headers look like `Terminal 1 lines 12-13`.93+ */94+function stripInlineTerminalLabels(prompt: string, headers: ReadonlyArray<string>): string {95+ let result = prompt;96+ for (const header of headers) {97+ const match = /^(.+?) lines? (\d+(?:-\d+)?)$/.exec(header);98+ if (!match) continue;99+ const label = `@${match[1]!.trim().toLowerCase().replace(/\s+/g, "-")}:${match[2]}`;100+ const index = result.indexOf(label);101+ if (index < 0) continue;102+ let end = index + label.length;103+ let start = index;104+ if (result[end] === " ") end += 1;105+ else if (result[start - 1] === " ") start -= 1;106+ result = result.slice(0, start) + result.slice(end);apps/web/src/components/chat/composerPromptHistory.ts line 83OutdatedResolvedby t3dotgg(GitHub profile, opens in a new tab)@@ -0,0 +63,21 @@63+/**64+ * Drop only the review comments appended at send time, which sit at the65+ * end. A review comment block the user typed mid-prompt stays put.66+ */67+function stripTrailingReviewComments(prompt: string): string {68+ const segments = [...parseReviewCommentMessageSegments(prompt)];69+ let changed = false;70+ while (segments.length > 0) {71+ const last = segments[segments.length - 1]!;72+ if (last.kind === "review-comment" || (changed && last.text.trim().length === 0)) {73+ segments.pop();74+ changed = true;75+ continue;76+ }77+ break;78+ }79+ if (!changed) return prompt;80+ return segments81+ .map((segment) => (segment.kind === "text" ? segment.text : ""))82+ .join("")83+ .trimEnd();
- apps/web/src/components/chat/composerPromptHistory.ts line 159Resolvedby t3dotgg(GitHub profile, opens in a new tab)@@ -0,0 +131,21 @@131+ continue;132+ }133+ const elementContexts = extractTrailingElementContexts(prompt);134+ if (elementContexts.contextCount > 0) {135+ prompt = elementContexts.promptText;136+ continue;137+ }138+ const terminalContexts = extractTrailingTerminalContexts(prompt);139+ if (terminalContexts.contextCount > 0) {140+ prompt = stripInlineTerminalLabels(141+ terminalContexts.promptText,142+ terminalContexts.contexts.map((context) => context.header),143+ );144+ continue;145+ }146+ break;147+ }148+149+ const trimmed = prompt.trim();150+ return trimmed === ATTACHMENT_ONLY_BOOTSTRAP_PROMPT ? "" : trimmed;151+}
t3dotgg(GitHub profile, opens in a new tab) closed this
Sign in to join this conversation.


Thread transfer impact#
✅ Thread transfer remains within every enforced ceiling.
Baseline:
df8e0eb· PR result:5fe0b5e· Source CI: successScenario and decoded snapshot size
10 historical turns, 5 command tools per turn, 878.9 KiB retained MCP result per historical turn, and a 1.05 MiB retained result in the measured turn.
Updated in place by a trusted workflow. PR artifacts are strictly validated and never executed.