diff --git a/src/lib/components/chat/Chat.svelte b/src/lib/components/chat/Chat.svelte index 9188bff3f0..e5b8465f51 100644 --- a/src/lib/components/chat/Chat.svelte +++ b/src/lib/components/chat/Chat.svelte @@ -87,6 +87,7 @@ import { applyResponseStreamEvent, getOutputText, + getPendingAskUserFromMessage, setOutputText } from './Messages/structuredOutput'; @@ -622,41 +623,6 @@ } }; - const parseToolArguments = (args) => { - if (!args) { - return {}; - } - let value = args; - while (typeof value === 'string') { - try { - value = JSON.parse(value); - } catch { - break; - } - } - return typeof value === 'object' && value !== null && !Array.isArray(value) ? value : {}; - }; - - const getPendingAskUserFromMessage = (message) => { - if (message?.role !== 'assistant' || !Array.isArray(message.output)) { - return null; - } - - const call = message.output.find( - (item) => - item?.type === 'function_call' && - item?.name === 'ask_user' && - item?.status === 'pending' && - (item?.call_id || item?.id) - ); - - if (call) { - return { message, call, args: parseToolArguments(call.arguments) }; - } - - return null; - }; - const findPendingAskUser = (chatHistory) => { if (!chatHistory?.messages) { return null; diff --git a/src/lib/components/chat/ChatTabs/SideChat.svelte b/src/lib/components/chat/ChatTabs/SideChat.svelte index 5456068cc6..47cf90e046 100644 --- a/src/lib/components/chat/ChatTabs/SideChat.svelte +++ b/src/lib/components/chat/ChatTabs/SideChat.svelte @@ -22,9 +22,18 @@ import { getSkills } from '$lib/apis/skills'; import { generateOpenAIChatCompletion, getErrorMessage } from '$lib/apis/openai'; import { getTaskIdsByChatId, resumeTemporaryChat, stopTasksByChatId } from '$lib/apis'; - import { createNewChat, getAllTags, getChatById, updateChatById } from '$lib/apis/chats'; + import { + createNewChat, + getAllTags, + getChatById, + resolveChatMessageToolCall, + updateChatById + } from '$lib/apis/chats'; import { refreshChatList, refreshFolderChatLists } from '$lib/stores/chatList'; - import { applyResponseStreamEvent } from '../Messages/structuredOutput'; + import { + applyResponseStreamEvent, + getPendingAskUserFromMessage + } from '../Messages/structuredOutput'; import Messages from '../Messages.svelte'; import MessageInput from '../MessageInput.svelte'; import FilesOverlay from '../MessageInput/FilesOverlay.svelte'; @@ -162,6 +171,43 @@ onConfirm: (value: any) => resolveInteraction(interaction, value), onCancel: () => resolveInteraction(interaction, { status: 'cancelled', answers: {} }) }; + const resolvePendingAskUser = async (action: 'answer' | 'reject', answers?: unknown) => { + const { message, call } = pendingAskUser; + error = ''; + try { + await resolveChatMessageToolCall( + localStorage.token, + id, + message.id, + call.call_id || call.id, + action, + { answers } + ); + // reconnect reads done to tell whether the resumed reply is still running + message.done = false; + } catch (err) { + error = getErrorMessage(err); + } + void reconnect(); + }; + $: pendingAskUser = + [...messages] + .reverse() + .filter((message) => (message.user_id ? message.user_id === $user?.id : !isShared)) + .map(getPendingAskUserFromMessage) + .find(Boolean) ?? null; + $: savedAskUserPrompt = pendingAskUser + ? { + show: true, + questions: Array.isArray(pendingAskUser.args?.questions) + ? pendingAskUser.args.questions + : [], + allowOther: pendingAskUser.args?.allow_other !== false, + timeoutMs: null, + onConfirm: (value: any) => void resolvePendingAskUser('answer', value?.answers ?? {}), + onCancel: () => void resolvePendingAskUser('reject') + } + : null; let system = [ systemPrompt, 'This is a separate side conversation. Earlier messages are reference context only. Answer the side question; do not continue the parent task.' @@ -333,7 +379,7 @@ const send = async () => { if (!canSend || disposed) return; const question = draft.trim(); - if ((!question && !files.length) || busy || sending || recovering) return; + if ((!question && !files.length) || busy || sending || recovering || pendingAskUser) return; if (!atSelectedModel && selectedModels.length !== 1) { error = $i18n.t('Select only one model'); return; @@ -400,6 +446,7 @@ parentId: userMessage.id, childrenIds: [] as string[], role: 'assistant', + user_id: $user?.id, content: '', output: [], done: false, @@ -633,7 +680,7 @@ onToolApprovalModeChange={(mode: string) => { params = { ...params, tool_approval_mode: mode }; }} - {askUser} + askUser={savedAskUserPrompt ?? askUser} dropzoneId={`${inputId}-pane`} {inputId} {messagesContainerId} diff --git a/src/lib/components/chat/Messages/structuredOutput.ts b/src/lib/components/chat/Messages/structuredOutput.ts index aac9d2189e..8dc32ba4ab 100644 --- a/src/lib/components/chat/Messages/structuredOutput.ts +++ b/src/lib/components/chat/Messages/structuredOutput.ts @@ -780,3 +780,42 @@ export function setOutputText(output: OutputItem[], text: string): OutputItem[] : item ); } + +function parseToolArguments(args: unknown): Record { + if (!args) { + return {}; + } + let value = args; + while (typeof value === 'string') { + try { + value = JSON.parse(value); + } catch { + break; + } + } + return typeof value === 'object' && value !== null && !Array.isArray(value) + ? (value as Record) + : {}; +} + +export function getPendingAskUserFromMessage( + message: T +) { + if (message?.role !== 'assistant' || !Array.isArray(message.output)) { + return null; + } + + const call = message.output.find( + (item: OutputItem) => + item?.type === 'function_call' && + item?.name === 'ask_user' && + item?.status === 'pending' && + (item?.call_id || item?.id) + ); + + if (call) { + return { message, call, args: parseToolArguments(call.arguments) }; + } + + return null; +}