fix(webui): resume queued prompts after turn completion (#6242)
* fix(webui): resume queued prompts after turn completion Persist the lifecycle turn each queued prompt waits for and use canonical completion identities to resume after navigation or reconnect. Release one entry at a time, and retain stopped or rejected entries for manual sending. * fix(webui): drain temporary chat prompts on live completion * perf(webui): resume queued prompts from live completion
This commit is contained in:
1 parent
3abcfe70c5
commit
836092424d
6 files changed
+339
-36
No files matched your search
@@ -214,6 +214,8 @@ interface ThreadComposerProps {
|
||||
inputAriaLabel?: string;
|
||||
compactWhenIdle?: boolean;
|
||||
isStreaming?: boolean;
|
||||
activeTurnId?: string | null;
|
||||
completedTurnIds?: readonly string[];
|
||||
modelLabel?: string | null;
|
||||
modelDetail?: string | null;
|
||||
modelPreset?: string | null;
|
||||
@@ -295,6 +297,7 @@ const SLASH_PALETTE_CHROME_PX = 12;
|
||||
const SLASH_RECENTS_STORAGE_KEY = "nanobot.webui.slashCommandRecents";
|
||||
const SLASH_RECENTS_LIMIT = 5;
|
||||
const QUEUED_PROMPTS_STORAGE_PREFIX = "nanobot.webui.composerQueuedGuidance.v1:";
|
||||
const EMPTY_COMPLETED_TURN_IDS: readonly string[] = [];
|
||||
const QUEUED_PROMPTS_LIMIT = 20;
|
||||
const QUEUED_PROMPT_MAX_CHARS = 4000;
|
||||
const SESSION_MENTIONS_LIMIT = 8;
|
||||
@@ -351,6 +354,16 @@ interface QueuedPrompt {
|
||||
images?: QueuedPromptImage[];
|
||||
quotedContext?: string;
|
||||
sessionMentions?: SessionMention[];
|
||||
autoAfterTurnId?: string;
|
||||
autoPaused?: boolean;
|
||||
}
|
||||
|
||||
function pauseQueuedPrompt(prompt: QueuedPrompt): QueuedPrompt {
|
||||
return { ...prompt, autoAfterTurnId: undefined, autoPaused: true };
|
||||
}
|
||||
|
||||
function waitForNextQueuedTurn(prompt: QueuedPrompt): QueuedPrompt {
|
||||
return { ...prompt, autoAfterTurnId: undefined, autoPaused: false };
|
||||
}
|
||||
|
||||
interface QueuedPromptImage {
|
||||
@@ -538,6 +551,10 @@ function normalizeQueuedPrompt(item: unknown, index: number): QueuedPrompt | nul
|
||||
...(images.length > 0 ? { images } : {}),
|
||||
...(quotedContext ? { quotedContext } : {}),
|
||||
...(sessionMentions.length > 0 ? { sessionMentions } : {}),
|
||||
...(typeof record.autoAfterTurnId === "string" && record.autoAfterTurnId.trim()
|
||||
? { autoAfterTurnId: record.autoAfterTurnId.trim() }
|
||||
: {}),
|
||||
...(record.autoPaused === true ? { autoPaused: true } : {}),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -574,6 +591,8 @@ function storeQueuedPrompts(storageKey: string, prompts: QueuedPrompt[]): void {
|
||||
...(prompt.sessionMentions?.length
|
||||
? { sessionMentions: prompt.sessionMentions.slice(0, SESSION_MENTIONS_LIMIT) }
|
||||
: {}),
|
||||
...(prompt.autoAfterTurnId ? { autoAfterTurnId: prompt.autoAfterTurnId } : {}),
|
||||
...(prompt.autoPaused ? { autoPaused: true } : {}),
|
||||
})),
|
||||
),
|
||||
);
|
||||
@@ -941,6 +960,8 @@ export function ThreadComposer({
|
||||
inputAriaLabel,
|
||||
compactWhenIdle = false,
|
||||
isStreaming = false,
|
||||
activeTurnId = null,
|
||||
completedTurnIds = EMPTY_COMPLETED_TURN_IDS,
|
||||
modelLabel = null,
|
||||
modelDetail = null,
|
||||
modelPreset = null,
|
||||
@@ -1052,7 +1073,6 @@ export function ThreadComposer({
|
||||
const draggedQueuedPromptIdRef = useRef<string | null>(null);
|
||||
const previousPendingQueueKeyRef = useRef(pendingQueueKey);
|
||||
const previousQueueRunRef = useRef({ key: pendingQueueKey, isStreaming });
|
||||
const skipNextQueuedFlushRef = useRef(false);
|
||||
const skipQueuedPromptPersistRef = useRef(false);
|
||||
const voiceShortcutDownRef = useRef(false);
|
||||
const voiceErrorFadeTimerRef = useRef<ReturnType<typeof setTimeout> | null>(null);
|
||||
@@ -1079,6 +1099,20 @@ export function ThreadComposer({
|
||||
setQueuedPrompts(queuedPromptStorageKey ? readQueuedPrompts(queuedPromptStorageKey) : []);
|
||||
}, [pendingQueueKey, queuedPromptStorageKey]);
|
||||
|
||||
useEffect(() => {
|
||||
if (!isStreaming || !activeTurnId) return;
|
||||
// The next lifecycle owner, not a guidance input, releases the remaining queue.
|
||||
setQueuedPrompts((items) => {
|
||||
let changed = false;
|
||||
const next = items.map((item) => {
|
||||
if (item.autoPaused || item.autoAfterTurnId === activeTurnId) return item;
|
||||
changed = true;
|
||||
return { ...item, autoAfterTurnId: activeTurnId };
|
||||
});
|
||||
return changed ? next : items;
|
||||
});
|
||||
}, [activeTurnId, isStreaming, pendingQueueKey]);
|
||||
|
||||
useEffect(() => {
|
||||
if (!queuedPromptStorageKey) return;
|
||||
if (skipQueuedPromptPersistRef.current) {
|
||||
@@ -1751,10 +1785,18 @@ export function ThreadComposer({
|
||||
};
|
||||
}, [onTranscribeAudio, voiceRecorder.beginShortcutHold, voiceRecorder.endShortcutHold]);
|
||||
|
||||
const handleStop = useCallback(() => {
|
||||
secondEnterPromptIdRef.current = null;
|
||||
if (queuedPrompts.length > 0) {
|
||||
setQueuedPrompts((items) => items.map(pauseQueuedPrompt));
|
||||
}
|
||||
onStop?.();
|
||||
}, [onStop, queuedPrompts.length]);
|
||||
|
||||
const chooseSlashCommand = useCallback(
|
||||
(command: SlashPaletteCommand) => {
|
||||
if (command.command === "/stop" && isStreaming && onStop) {
|
||||
onStop();
|
||||
handleStop();
|
||||
setValue("");
|
||||
setSlashMenuDismissed(true);
|
||||
setCliAppMenuDismissed(false);
|
||||
@@ -1784,7 +1826,7 @@ export function ThreadComposer({
|
||||
setInlineError(null);
|
||||
resizeTextarea();
|
||||
},
|
||||
[isStreaming, onStop, recentSlashCommands, replaceMentionInput, resizeTextarea, skillQuery, value],
|
||||
[handleStop, isStreaming, onStop, recentSlashCommands, replaceMentionInput, resizeTextarea, skillQuery, value],
|
||||
);
|
||||
|
||||
const insertMentionCandidate = useCallback(
|
||||
@@ -1922,6 +1964,7 @@ export function ThreadComposer({
|
||||
{
|
||||
id,
|
||||
text,
|
||||
...(activeTurnId ? { autoAfterTurnId: activeTurnId } : {}),
|
||||
...(queuedImages.length > 0 ? { images: queuedImages } : {}),
|
||||
...(normalizedQuotedContext ? { quotedContext: normalizedQuotedContext } : {}),
|
||||
...(activeSessionMentions.length > 0
|
||||
@@ -1934,6 +1977,7 @@ export function ThreadComposer({
|
||||
onQuotedContextChange?.(null);
|
||||
}, [
|
||||
activeSessionMentions,
|
||||
activeTurnId,
|
||||
canQueueGuidance,
|
||||
clear,
|
||||
clearComposerText,
|
||||
@@ -1984,8 +2028,9 @@ export function ThreadComposer({
|
||||
|
||||
const observeQueuedSend = useCallback((prompt: QueuedPrompt, result: void | boolean | Promise<void | boolean>) => {
|
||||
const restore = (error?: unknown) => {
|
||||
skipNextQueuedFlushRef.current = true;
|
||||
setQueuedPrompts((items) => items.some((item) => item.id === prompt.id) ? items : [prompt, ...items]);
|
||||
setQueuedPrompts((items) => (items.some((item) => item.id === prompt.id)
|
||||
? items
|
||||
: [prompt, ...items]).map(pauseQueuedPrompt));
|
||||
setInlineError(error instanceof Error ? error.message : "Message not sent; queued draft retained");
|
||||
};
|
||||
if (result instanceof Promise) void result.then((accepted) => { if (accepted === false) restore(); }).catch(restore);
|
||||
@@ -1997,7 +2042,10 @@ export function ThreadComposer({
|
||||
secondEnterPromptIdRef.current = null;
|
||||
const text = prompt.text.trim();
|
||||
const queuedImages = queuedImagesToSendImages(prompt.images);
|
||||
setQueuedPrompts((items) => items.filter((item) => item.id !== prompt.id));
|
||||
setQueuedPrompts((items) => {
|
||||
const remaining = items.filter((item) => item.id !== prompt.id);
|
||||
return isStreaming ? remaining : remaining.map(waitForNextQueuedTurn);
|
||||
});
|
||||
if (text || queuedImages?.length) {
|
||||
const options: SendOptions | undefined = (
|
||||
prompt.quotedContext
|
||||
@@ -2026,7 +2074,7 @@ export function ThreadComposer({
|
||||
setQueuedPrompts([]);
|
||||
return;
|
||||
}
|
||||
setQueuedPrompts((items) => items.filter((item) => item.id !== nextPrompt.id));
|
||||
setQueuedPrompts((items) => items.filter((item) => item.id !== nextPrompt.id).map(waitForNextQueuedTurn));
|
||||
const queuedImages = queuedImagesToSendImages(nextPrompt.images);
|
||||
const options: SendOptions | undefined = (
|
||||
nextPrompt.quotedContext || nextPrompt.sessionMentions?.length
|
||||
@@ -2051,25 +2099,16 @@ export function ThreadComposer({
|
||||
previousQueueRunRef.current = { key: pendingQueueKey, isStreaming };
|
||||
if (!isStreaming) secondEnterPromptIdRef.current = null;
|
||||
// Switching to an idle session is not completion of the previous session's run.
|
||||
if (previous.key !== pendingQueueKey) {
|
||||
skipNextQueuedFlushRef.current = false;
|
||||
return;
|
||||
}
|
||||
if (!previous.isStreaming || isStreaming || queuedPrompts.length === 0) return;
|
||||
if (skipNextQueuedFlushRef.current) {
|
||||
skipNextQueuedFlushRef.current = false;
|
||||
return;
|
||||
}
|
||||
if (previous.key !== pendingQueueKey) return;
|
||||
if (isStreaming || interactionDisabled || queuedPrompts.length === 0) return;
|
||||
const first = queuedPrompts[0];
|
||||
if (first.autoPaused) return;
|
||||
const waitingTurnCompleted = queuedPromptStorageKey !== null && first.autoAfterTurnId !== undefined
|
||||
? completedTurnIds.includes(first.autoAfterTurnId)
|
||||
: previous.isStreaming;
|
||||
if (!waitingTurnCompleted) return;
|
||||
sendNextQueuedPrompt();
|
||||
}, [sendNextQueuedPrompt, isStreaming, pendingQueueKey, queuedPrompts.length]);
|
||||
|
||||
const handleStop = useCallback(() => {
|
||||
secondEnterPromptIdRef.current = null;
|
||||
if (queuedPrompts.length > 0) {
|
||||
skipNextQueuedFlushRef.current = true;
|
||||
}
|
||||
onStop?.();
|
||||
}, [onStop, queuedPrompts.length]);
|
||||
}, [completedTurnIds, interactionDisabled, sendNextQueuedPrompt, isStreaming, pendingQueueKey, queuedPrompts, queuedPromptStorageKey]);
|
||||
|
||||
const submit = useCallback(() => {
|
||||
if (modelNeedsSetup) {
|
||||
|
||||
@@ -1259,6 +1259,23 @@ export function ThreadShell({
|
||||
// its history response can race with another send or running event.
|
||||
}, [chatId, client, historyKey]);
|
||||
|
||||
const canonicalCompletedTurnIds = useMemo(() => Array.from(new Set([
|
||||
...completedTurnIds,
|
||||
...(!completionIdsProvided && !hasPendingToolCalls && historyActiveTurnId === null
|
||||
? completedAssistantTurnIds(projectWebuiThreadMessages(historical))
|
||||
: []),
|
||||
])), [completedTurnIds, completionIdsProvided, hasPendingToolCalls, historical, historyActiveTurnId]);
|
||||
|
||||
const lastRunTurnId = chatId ? client.getRunTurnId(chatId) : null;
|
||||
const completedRunTurnId = chatId && lastRunTurnId && client.isRunCompleted(chatId, lastRunTurnId)
|
||||
? lastRunTurnId : null;
|
||||
const queueCompletedTurnIds = useMemo(() => {
|
||||
const canonical = hasPendingToolCalls ? [] : canonicalCompletedTurnIds;
|
||||
// Live terminal facts can release the queue before the history refresh finishes.
|
||||
return completedRunTurnId && !canonical.includes(completedRunTurnId)
|
||||
? [...canonical, completedRunTurnId] : canonical;
|
||||
}, [canonicalCompletedTurnIds, completedRunTurnId, hasPendingToolCalls]);
|
||||
|
||||
useEffect(() => {
|
||||
if (!historyKey || !chatId || loading) return;
|
||||
const pendingCanonicalHydrate = pendingCanonicalHydrateRef.current.get(chatId);
|
||||
@@ -1274,12 +1291,6 @@ export function ThreadShell({
|
||||
)
|
||||
);
|
||||
const normalizedHistory = projectWebuiThreadMessages(historical);
|
||||
const canonicalCompletedTurnIds = Array.from(new Set([
|
||||
...completedTurnIds,
|
||||
...(!completionIdsProvided && !hasPendingToolCalls && historyActiveTurnId === null
|
||||
? completedAssistantTurnIds(normalizedHistory)
|
||||
: []),
|
||||
]));
|
||||
// Completion is a fact about a turn, even when a newer local turn prevents
|
||||
// this snapshot from settling the conversation's current run.
|
||||
client.fenceCanonicalCompletedTurns(chatId, canonicalCompletedTurnIds);
|
||||
@@ -1386,6 +1397,7 @@ export function ThreadShell({
|
||||
chatId,
|
||||
client,
|
||||
completedTurnIds,
|
||||
canonicalCompletedTurnIds,
|
||||
completionIdsProvided,
|
||||
historical,
|
||||
historyVersion,
|
||||
@@ -1874,6 +1886,9 @@ export function ThreadShell({
|
||||
disabled={!chatId}
|
||||
inputAriaLabel={composerInputAriaLabel}
|
||||
isStreaming={turnActive}
|
||||
activeTurnId={turnActive && chatId ? client.getRunTurnId(chatId) ?? historyActiveTurnId : null}
|
||||
completedTurnIds={messagesReady && !recoveryNeedsDecision
|
||||
&& chatId && !client.hasUnsettledRun(chatId) ? queueCompletedTurnIds : undefined}
|
||||
placeholder={
|
||||
composerVariant === "hero"
|
||||
? t("thread.composer.placeholderHero")
|
||||
|
||||
Reference in new issue
Block a user