fix(webui): keep restored waiting messages manual (NAN-275) (#6393)
This commit is contained in:
1 parent
abfe510ce1
commit
7dfeff9ddf
6 files changed
+179
-6
No files matched your search
@@ -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<AttachedImage & { dataUrl: string }>,
|
||||
): 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;
|
||||
|
||||
@@ -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(<ThreadComposer onSend={onSend} pendingQueueKey="chat-a" completedTurnIds={["turn-a"]} />);
|
||||
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(<ThreadComposer onSend={onSend} pendingQueueKey="chat-a" completedTurnIds={["old-turn"]} />);
|
||||
expect(screen.getByRole("group", { name: "Waiting to send" })).toHaveTextContent("old instruction @alpha");
|
||||
expect(onSend).not.toHaveBeenCalled();
|
||||
view.rerender(<ThreadComposer onSend={onSend} pendingQueueKey="chat-a" isStreaming activeTurnId="new-turn" completedTurnIds={["old-turn"]} />);
|
||||
view.rerender(<ThreadComposer onSend={onSend} pendingQueueKey="chat-a" completedTurnIds={["old-turn", "new-turn"]} />);
|
||||
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(<ThreadComposer onSend={onSend} pendingQueueKey="chat-a" isStreaming activeTurnId="turn-a" />);
|
||||
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(<ThreadComposer {...props} />);
|
||||
else first.rerender(<ThreadComposer {...props} />);
|
||||
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");
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user