diff --git a/docs/webui.md b/docs/webui.md index 566a62303..f8afdb3f9 100644 --- a/docs/webui.md +++ b/docs/webui.md @@ -185,6 +185,8 @@ On touch-primary phones and tablets, **Enter** inserts a newline; tap the send a During a response, desktop Enter appends input to one waiting message, preserving order, attachments and references. It sends when the response finishes. **Send now** or Enter on an empty input sends it immediately as guidance; the send arrow submits the waiting content together with any draft. **Edit message** and **Delete message** act on the whole waiting message. Additions that exceed limits stay in the input. Stopping a turn or a failed send preserves the waiting message for manual sending. +Automatic sending continues across topic switches and short reconnects while the page stays live. Reloading, reopening, leaving the page, or the browser freezing it restores waiting content for manual sending; choose **Send now** or edit and send it when ready. + For image generation, configure an image provider first and then use the WebUI image mode from the composer. See [`image-generation.md`](./image-generation.md) for provider setup and output behavior. ## Channels diff --git a/webui/browser/fixture.tsx b/webui/browser/fixture.tsx index b65cbd2f8..829fe54d8 100644 --- a/webui/browser/fixture.tsx +++ b/webui/browser/fixture.tsx @@ -32,6 +32,7 @@ import { SidebarActivityFixture } from "./sidebar-activity-fixture"; import { HistoryHydrationFixture } from "./history-hydration-fixture"; import { IdleCompactionFixture, installIdleCompactionApi } from "./idle-compaction-fixture"; import { FileEditPathFixture } from "./file-edit-path-fixture"; +import { QueuedGuidanceFixture } from "./queued-guidance-fixture"; const history: UIMessage[] = [ { id: "question", role: "user", content: "Earlier question", createdAt: 1_700_000_000_000 }, @@ -173,7 +174,9 @@ if (parameters.get("case") === "skeleton-additions") installSkeletonPreviewApi() if (parameters.get("case") === "idle-compaction") installIdleCompactionApi(); await initializeI18n(); createRoot(document.getElementById("root")!).render( - {parameters.get("case") === "file-edit-path" + {parameters.get("case") === "queued-guidance" + ? + : parameters.get("case") === "file-edit-path" ? : parameters.get("case") === "answer-order" ? diff --git a/webui/browser/queued-guidance-fixture.tsx b/webui/browser/queued-guidance-fixture.tsx new file mode 100644 index 000000000..16fa4f543 --- /dev/null +++ b/webui/browser/queued-guidance-fixture.tsx @@ -0,0 +1,28 @@ +import { useState } from "react"; + +import { ThreadComposer } from "@/components/thread/ThreadComposer"; +import type { SendAttachment, SendOptions } from "@/hooks/useNanobotStream"; + +export function QueuedGuidanceFixture() { + const [away, setAway] = useState(false); + const [completed, setCompleted] = useState(new URLSearchParams(location.search).has("complete")); + const [sent, setSent] = useState>([]); + return
+
+ + +
+ {away ?

Another topic

: setCompleted(true)} + onSend={(text, images, options) => { + setSent((current) => [...current, { text, images, options }]); + return true; + }} + />} + {JSON.stringify(sent)} +
; +} diff --git a/webui/browser/queued-guidance.spec.ts b/webui/browser/queued-guidance.spec.ts new file mode 100644 index 000000000..0ac62aba8 --- /dev/null +++ b/webui/browser/queued-guidance.spec.ts @@ -0,0 +1,67 @@ +import { expect, test, type Page } from "@playwright/test"; + +const fixture = "/browser/fixture.html?case=queued-guidance"; +const storageKey = "nanobot.webui.composerQueuedGuidance.v1:queued-fixture"; + +async function queue(page: Page, text = "Confirm this instruction") { + const input = page.getByRole("textbox", { name: "Message input", exact: true }); + await input.fill(text); + await input.press("Enter"); + await expect(page.getByRole("group", { name: "Waiting to send" })).toContainText(text); +} + +for (const width of [1440, 390]) { + test(`a new page retains waiting content without executing it (${width}px)`, async ({ page }) => { + await page.setViewportSize({ width, height: 900 }); + await page.goto(fixture); + await queue(page); + await page.goto(`${fixture}&complete`); + await expect(page.getByRole("group", { name: "Waiting to send" })).toContainText("Confirm this instruction"); + await expect(page.getByLabel("Submitted messages")).toHaveText("[]"); + await page.getByRole("button", { name: "Send now", exact: true }).click(); + await expect(page.getByLabel("Submitted messages")).toHaveText('[{"text":"Confirm this instruction"}]'); + await expect(page.getByRole("group", { name: "Waiting to send" })).toHaveCount(0); + }); +} + +test("switching topics in the live page retains one automatic send", async ({ page }) => { + await page.goto(fixture); + await queue(page); + await page.getByRole("button", { name: "Switch topic" }).click(); + await page.getByRole("button", { name: "Complete original reply" }).click(); + await page.getByRole("button", { name: "Switch topic" }).click(); + await expect(page.getByLabel("Submitted messages")).toHaveText('[{"text":"Confirm this instruction"}]'); + await expect(page.getByRole("group", { name: "Waiting to send" })).toHaveCount(0); +}); + +test("leaving and returning through browser history requires manual delivery", async ({ page }) => { + await page.goto(fixture); + await queue(page); + await page.goto("about:blank"); + await page.goBack(); + await page.getByRole("button", { name: "Complete original reply" }).click(); + await expect(page.getByRole("group", { name: "Waiting to send" })).toContainText("Confirm this instruction"); + await expect(page.getByLabel("Submitted messages")).toHaveText("[]"); + await page.getByRole("button", { name: "Send now", exact: true }).click(); + await expect(page.getByLabel("Submitted messages")).toHaveText('[{"text":"Confirm this instruction"}]'); +}); + +test("legacy completed-turn drafts preserve every field until an explicit send", async ({ page }) => { + await page.goto(fixture); + const draft = { + id: "queued-prompt-1792147773397-1", text: "Remember @alpha", autoAfterTurnId: "turn-a", + quotedContext: "Original quote", sessionMentions: [{ name: "alpha", title: "Alpha", session_key: "websocket:alpha" }], + images: [{ dataUrl: "data:text/plain;base64,aGVsbG8=", kind: "file", name: "notes.txt" }], + }; + await page.evaluate(({ key, value }) => localStorage.setItem(key, JSON.stringify([value])), { key: storageKey, value: draft }); + await page.goto(`${fixture}&complete`); + await expect(page.getByRole("group", { name: "Waiting to send" })).toContainText("Remember @alpha"); + await expect(page.getByLabel("Submitted messages")).toHaveText("[]"); + await page.getByRole("button", { name: "Send now", exact: true }).click(); + const sent = JSON.parse(await page.getByLabel("Submitted messages").textContent() ?? "null"); + expect(sent).toEqual([{ + text: draft.text, + images: [{ media: { data_url: draft.images[0].dataUrl, name: "notes.txt" }, preview: { kind: "file", url: draft.images[0].dataUrl, name: "notes.txt" } }], + options: { quotedContext: draft.quotedContext, sessionMentions: draft.sessionMentions }, + }]); +}); diff --git a/webui/src/components/thread/ThreadComposer.tsx b/webui/src/components/thread/ThreadComposer.tsx index c5a0a4b84..90b79f72f 100644 --- a/webui/src/components/thread/ThreadComposer.tsx +++ b/webui/src/components/thread/ThreadComposer.tsx @@ -361,10 +361,21 @@ interface QueuedPrompt { sessionMentions?: SessionMention[]; autoAfterTurnId?: string; autoPaused?: boolean; + autoSendPageId?: string; +} + +let queuedPromptPageId: string | undefined; + +function getQueuedPromptPageId(): string { + return queuedPromptPageId ??= crypto.randomUUID(); +} + +function canAutoSendQueuedPrompt(prompt: QueuedPrompt): boolean { + return !prompt.autoPaused && prompt.autoSendPageId === getQueuedPromptPageId(); } function pauseQueuedPrompt(prompt: QueuedPrompt): QueuedPrompt { - return { ...prompt, autoAfterTurnId: undefined, autoPaused: true }; + return { ...prompt, autoAfterTurnId: undefined, autoSendPageId: undefined, autoPaused: true }; } function mergeQueuedPrompt(first: QueuedPrompt, next: QueuedPrompt): QueuedPrompt { @@ -383,6 +394,7 @@ function mergeQueuedPrompt(first: QueuedPrompt, next: QueuedPrompt): QueuedPromp sessionMentions: [...mentions.values()], autoAfterTurnId: first.autoAfterTurnId, autoPaused: prompts.some((prompt) => prompt.autoPaused), + autoSendPageId: first.autoSendPageId, }; } @@ -571,7 +583,7 @@ function normalizeQueuedPrompt(item: unknown, index: number): QueuedPrompt | nul const id = typeof record.id === "string" && record.id.trim() ? record.id : `queued-prompt-restored-${index}`; - return { + const prompt: QueuedPrompt = { id, text, ...(images.length > 0 ? { images } : {}), @@ -581,7 +593,9 @@ function normalizeQueuedPrompt(item: unknown, index: number): QueuedPrompt | nul ? { autoAfterTurnId: record.autoAfterTurnId.trim() } : {}), ...(record.autoPaused === true ? { autoPaused: true } : {}), + ...(typeof record.autoSendPageId === "string" ? { autoSendPageId: record.autoSendPageId } : {}), }; + return canAutoSendQueuedPrompt(prompt) ? prompt : pauseQueuedPrompt(prompt); } function readQueuedPrompt(storageKey: string): QueuedPrompt | null { @@ -693,6 +707,19 @@ function queuedPromptStoreFor(storageKey: string | null): QueuedPromptStore { return store; } +function expireQueuedPromptAuthorization(): void { + queuedPromptPageId = undefined; + for (const store of queuedPromptStores.values()) { + store.set((prompt) => prompt && !prompt.autoPaused ? pauseQueuedPrompt(prompt) : prompt); + } +} + +// Saved content survives a page's lifetime; permission to execute it does not. +if (typeof window !== "undefined") { + window.addEventListener("pagehide", expireQueuedPromptAuthorization); + document.addEventListener("freeze", expireQueuedPromptAuthorization); +} + function readyImagesToQueuedImages( images: Array, ): QueuedPromptImage[] { @@ -2009,6 +2036,7 @@ export function ThreadComposer({ const prompt: QueuedPrompt = { id: `queued-prompt-${Date.now()}-${queuedPromptCounterRef.current}`, text: value.trim(), + autoSendPageId: getQueuedPromptPageId(), images: readyImagesToQueuedImages(readyImages), ...(activeTurnId ? { autoAfterTurnId: activeTurnId } : {}), ...(normalizedQuotedContext ? { quotedContext: normalizedQuotedContext } : {}), @@ -2094,7 +2122,7 @@ export function ThreadComposer({ if (!isStreaming) secondEnterPromptIdRef.current = null; // Switching to an idle session is not completion of the previous session's run. if (previous.key !== pendingQueueKey) return; - if (isStreaming || interactionDisabled || !queuedPrompt || queuedPrompt.autoPaused) return; + if (isStreaming || interactionDisabled || !queuedPrompt || !canAutoSendQueuedPrompt(queuedPrompt)) return; const waitingTurnCompleted = queuedPromptStorageKey !== null && queuedPrompt.autoAfterTurnId !== undefined ? completedTurnIds.includes(queuedPrompt.autoAfterTurnId) : previous.isStreaming; diff --git a/webui/src/tests/queued-guidance.test.tsx b/webui/src/tests/queued-guidance.test.tsx index 9b36f271b..592a68907 100644 --- a/webui/src/tests/queued-guidance.test.tsx +++ b/webui/src/tests/queued-guidance.test.tsx @@ -215,14 +215,16 @@ describe("merged waiting messages", () => { expect(onQuotedContextChange).toHaveBeenLastCalledWith("new selection"); }); - it("auto-sends attachment-only guidance after completion", async () => { + it("keeps restored attachment-only guidance manual after historical completion", () => { window.localStorage.setItem(STORAGE_KEY, JSON.stringify([ { id: "first", text: "", images: [PNG], autoAfterTurnId: "turn-a" }, { id: "second", text: "", images: [DOCUMENT], autoAfterTurnId: "turn-a" }, ])); const onSend = vi.fn(); render(); - await waitFor(() => expect(onSend).toHaveBeenCalledTimes(1)); + expect(onSend).not.toHaveBeenCalled(); + fireEvent.click(screen.getByRole("button", { name: "Send now" })); + expect(onSend).toHaveBeenCalledTimes(1); expect(onSend).toHaveBeenCalledWith("", [ expect.objectContaining({ media: expect.objectContaining({ name: "first.png" }) }), expect.objectContaining({ media: expect.objectContaining({ name: "second.txt" }) }), @@ -291,3 +293,46 @@ describe("merged waiting messages", () => { expect(onSend).toHaveBeenCalledWith("stopped\nnew input"); }); }); + + +describe("waiting-message execution authorization", () => { + it.each([undefined, "earlier-page"])("retains an earlier page's completed-turn draft for manual confirmation (page=%s)", (autoSendPageId) => { + const target = mention("alpha"); + window.localStorage.setItem(STORAGE_KEY, JSON.stringify([{ + id: "queued-prompt-1792147773397-1", text: "old instruction @alpha", images: [PNG, DOCUMENT], + quotedContext: "original quote", sessionMentions: [target], autoAfterTurnId: "old-turn", autoSendPageId, + }])); + const onSend = vi.fn(); + const view = render(); + expect(screen.getByRole("group", { name: "Waiting to send" })).toHaveTextContent("old instruction @alpha"); + expect(onSend).not.toHaveBeenCalled(); + view.rerender(); + view.rerender(); + expect(onSend).not.toHaveBeenCalled(); + fireEvent.click(screen.getByRole("button", { name: "Send now" })); + expect(onSend).toHaveBeenCalledTimes(1); + expect(onSend).toHaveBeenCalledWith("old instruction @alpha", [ + expect.objectContaining({ media: { data_url: PNG.dataUrl, name: PNG.name } }), + expect.objectContaining({ media: { data_url: DOCUMENT.dataUrl, name: DOCUMENT.name } }), + ], { quotedContext: "original quote", sessionMentions: [target] }); + }); + + it.each([ + { event: "pagehide", leaveTopic: true }, + { event: "freeze", leaveTopic: false }, + ])("revokes automatic delivery on $event while retaining the waiting message", ({ event, leaveTopic }) => { + const onSend = vi.fn(); + const first = render(); + enter("confirm after returning"); + if (leaveTopic) first.unmount(); + act(() => (event === "pagehide" ? window : document).dispatchEvent(new Event(event))); + const props = { onSend, pendingQueueKey: "chat-a", completedTurnIds: ["turn-a"] }; + if (leaveTopic) render(); + else first.rerender(); + expect(screen.getByRole("group", { name: "Waiting to send" })).toHaveTextContent("confirm after returning"); + expect(onSend).not.toHaveBeenCalled(); + fireEvent.click(screen.getByRole("button", { name: "Send now" })); + expect(onSend).toHaveBeenCalledOnce(); + expect(onSend).toHaveBeenCalledWith("confirm after returning"); + }); +});