Skip to content

Commit ad9aaa8

Browse files
fix(web): keep a trailing chip's space at stash time, not on restore
Restoring appended a space after any trailing @/$ token, so an unfinished query like `@Rea` stashed mid-typing came back as a mention chip. Stashing now keeps one trailing space only when the prompt ends in a parsed chip followed by whitespace, and restore is back to main's behaviour. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 20edbd0 commit ad9aaa8

2 files changed

Lines changed: 36 additions & 13 deletions

File tree

‎apps/web/src/components/ChatView.browser.test.tsx‎

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,7 @@ it("a new draft does not load its reserved thread before the server lists it", a
6363
expect(threadRequests).toEqual([]);
6464
});
6565

66-
it("a stashed prompt ending in a file chip restores with the chip", async () => {
67-
using _environment = installFakeEnvironment([]);
66+
async function draftRouterWithPrompt(prompt: string) {
6867
__setClientSettingsForTests({
6968
...DEFAULT_CLIENT_SETTINGS,
7069
onboardingCompletedAt: "2026-01-01T00:00:00.000Z",
@@ -74,10 +73,15 @@ it("a stashed prompt ending in a file chip restores with the chip", async () =>
7473
drafts.setProjectDraftThreadId(scopeProjectRef(ENVIRONMENT_ID, PROJECT_ID), draftId, {
7574
threadId: ThreadId.make("thread-28338"),
7675
});
77-
drafts.setPrompt(draftId, "Check [package.json](package.json) ");
78-
76+
drafts.setPrompt(draftId, prompt);
7977
const router = getRouter(createMemoryHistory({ initialEntries: [`/draft/${draftId}`] }));
8078
await router.load();
79+
return router;
80+
}
81+
82+
it("a stashed prompt ending in a file chip restores with the chip", async () => {
83+
using _environment = installFakeEnvironment([]);
84+
const router = await draftRouterWithPrompt("Check [package.json](package.json) ");
8185
await using _app = await render(<AppRoot router={router} />, {
8286
// AppRoot brings its own router.
8387
router: false,
@@ -95,3 +99,22 @@ it("a stashed prompt ending in a file chip restores with the chip", async () =>
9599
expect(editor.element().textContent).not.toContain("[package.json](package.json)");
96100
await expect.element(chip).toBeVisible();
97101
});
102+
103+
it("a stashed unfinished mention query restores as text", async () => {
104+
using _environment = installFakeEnvironment([]);
105+
const router = await draftRouterWithPrompt("Check @Rea");
106+
await using _app = await render(<AppRoot router={router} />, {
107+
// AppRoot brings its own router.
108+
router: false,
109+
});
110+
const editor = page.getByTestId("composer-editor");
111+
await vi.waitFor(() => expect(editor.element().textContent).toContain("Check @Rea"));
112+
113+
await userEvent.click(editor);
114+
await userEvent.keyboard("{ControlOrMeta>}s{/ControlOrMeta}");
115+
await vi.waitFor(() => expect(editor.element().textContent).not.toContain("Check"));
116+
await userEvent.keyboard("{ControlOrMeta>}s{/ControlOrMeta}");
117+
118+
await vi.waitFor(() => expect(editor.element().textContent).toContain("Check @Rea"));
119+
await expect.element(page.getByRole("button", { name: "Preview Rea" })).not.toBeInTheDocument();
120+
});

‎apps/web/src/components/chat/useComposerStash.ts‎

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ export function useComposerStash(input: {
208208
const rewrittenContextIds = entry.records
209209
? importContextRecords(entry.records, null, composerDraftTarget)
210210
: new Map<string, string>();
211-
const rewrittenPrompt = replaceComposerContextReferences(entry.prompt, (reference) => {
211+
const restoredPrompt = replaceComposerContextReferences(entry.prompt, (reference) => {
212212
const contextId = rewrittenContextIds.get(reference.contextId);
213213
return contextId
214214
? formatInlineContextReference({
@@ -218,13 +218,6 @@ export function useComposerStash(input: {
218218
})
219219
: reference.source;
220220
});
221-
// Stash entries are stored trimmed, and a chip token only parses when whitespace follows it.
222-
const lastToken = collectComposerPromptInlineTokens(`${rewrittenPrompt}\n`).at(-1);
223-
const restoredPrompt =
224-
(lastToken?.type === "mention" || lastToken?.type === "skill") &&
225-
lastToken.end === rewrittenPrompt.length
226-
? `${rewrittenPrompt} `
227-
: rewrittenPrompt;
228221
const currentPrompt = promptRef.current;
229222
const nextPrompt =
230223
restoredPrompt.length === 0
@@ -463,7 +456,14 @@ export function useComposerStash(input: {
463456
});
464457
return;
465458
}
466-
const prompt = promptRef.current.trim();
459+
const rawPrompt = promptRef.current;
460+
const lastToken = collectComposerPromptInlineTokens(rawPrompt).at(-1);
461+
// A trailing chip only parses with whitespace after it, so keep one space to restore it.
462+
const prompt =
463+
(lastToken?.type === "mention" || lastToken?.type === "skill") &&
464+
rawPrompt.slice(lastToken.end).trim().length === 0
465+
? `${rawPrompt.trim()} `
466+
: rawPrompt.trim();
467467
const attachmentDraft = readAttachmentDraft();
468468
const images = [...attachmentDraft.images];
469469
const files = [...attachmentDraft.files];

0 commit comments

Comments
 (0)