From 0422b5e29d91bcf4e401f7f1633fe2b4708ccf18 Mon Sep 17 00:00:00 2001 From: Oleg Yermolenko Date: Fri, 28 Nov 2025 19:45:11 +0200 Subject: [PATCH] fix formatting --- backend/open_webui/env.py | 4 +++- backend/open_webui/utils/middleware.py | 8 ++++++-- src/lib/components/chat/Chat.svelte | 1 + src/lib/utils/index.ts | 23 ++++++++++------------- 4 files changed, 20 insertions(+), 16 deletions(-) diff --git a/backend/open_webui/env.py b/backend/open_webui/env.py index d9ef8eb446..f0084ed5f7 100644 --- a/backend/open_webui/env.py +++ b/backend/open_webui/env.py @@ -374,7 +374,9 @@ ENABLE_REALTIME_CHAT_SAVE = ( ENABLE_QUERIES_CACHE = os.environ.get("ENABLE_QUERIES_CACHE", "False").lower() == "true" -ENABLE_WRAP_TOOL_RESULT = os.environ.get("ENABLE_WRAP_TOOL_RESULT", "True").lower() == "true" +ENABLE_WRAP_TOOL_RESULT = ( + os.environ.get("ENABLE_WRAP_TOOL_RESULT", "True").lower() == "true" +) TOOL_RESULT_INDENT_SIZE = os.environ.get("TOOL_RESULT_INDENT_SIZE", 2) diff --git a/backend/open_webui/utils/middleware.py b/backend/open_webui/utils/middleware.py index 51b338f896..e4bab6e95a 100644 --- a/backend/open_webui/utils/middleware.py +++ b/backend/open_webui/utils/middleware.py @@ -286,10 +286,14 @@ def process_tool_result( return tool_result, tool_result_files, tool_result_embeds + def dump_tool_result_to_json(model, ensure_ascii=True): indent_size = None if TOOL_RESULT_INDENT_SIZE == 0 else TOOL_RESULT_INDENT_SIZE - separators = None if indent_size and indent_size > 0 else (',', ':') - return json.dumps(model, indent=indent_size, separators=separators, ensure_ascii=ensure_ascii) + separators = None if indent_size and indent_size > 0 else (",", ":") + return json.dumps( + model, indent=indent_size, separators=separators, ensure_ascii=ensure_ascii + ) + async def chat_completion_tools_handler( request: Request, body: dict, extra_params: dict, user: UserModel, models, tools diff --git a/src/lib/components/chat/Chat.svelte b/src/lib/components/chat/Chat.svelte index a2dd12038a..0f16372569 100644 --- a/src/lib/components/chat/Chat.svelte +++ b/src/lib/components/chat/Chat.svelte @@ -1877,6 +1877,7 @@ for (const message of _messages) { let content = message?.merged?.content ?? message?.content; let processedMessages = processDetailsAndExtractToolCalls(content ?? ''); + let nonToolMesssage = null; let toolCallIndex = 0; diff --git a/src/lib/utils/index.ts b/src/lib/utils/index.ts index 077a0eb86a..d1e53d9137 100644 --- a/src/lib/utils/index.ts +++ b/src/lib/utils/index.ts @@ -862,14 +862,14 @@ const detailsAttributesRegex = /(\w+)="([^"]*)"/g; export const processDetailsAndExtractToolCalls = (content) => { content = removeDetails(content, ['reasoning', 'code_interpreter']); - + // Split text and tool calls into messages array let messages = []; const matches = content.match(toolCallsDetailsRegex); if (matches && matches.length > 0) { let previousDetailsEndIndex = 0; for (const match of matches) { - + let detailsStartIndex = content.indexOf(match, previousDetailsEndIndex); let assistantMessage = content.substr(previousDetailsEndIndex, detailsStartIndex - previousDetailsEndIndex); previousDetailsEndIndex = detailsStartIndex + match.length; @@ -884,46 +884,43 @@ export const processDetailsAndExtractToolCalls = (content) => { while ((attributeMatch = detailsAttributesRegex.exec(match)) !== null) { attributes[attributeMatch[1]] = attributeMatch[2]; } - + if (!attributes.id) { continue; } - + let toolCall = { id: attributes.id, name: attributes.name, arguments: unescapeHtml(attributes.arguments ?? ''), result: unescapeHtml(attributes.result ?? '') } - + toolCall.arguments = parseDoubleEncodedString(toolCall.arguments); toolCall.result = parseDoubleEncodedString(toolCall.result); messages.push(toolCall); } - + let finalAssistantMessage = content.substr(previousDetailsEndIndex); finalAssistantMessage = finalAssistantMessage.trim('\n'); if (finalAssistantMessage.length > 0) { messages.push(finalAssistantMessage); } - } - else if (content.length > 0) { + } else if (content.length > 0) { messages.push(content); } - + return messages; }; function parseDoubleEncodedString(value) { - try - { + try { let parsedValue = JSON.parse(value); if (typeof parsedValue == "string") { return parsedValue; } - } - catch {} + } catch {} return value; }