From 18865a9fef1bb154603b7b8af0116a10560e03ac Mon Sep 17 00:00:00 2001 From: Timothy Jaeryang Baek Date: Sun, 1 Mar 2026 12:30:03 -0600 Subject: [PATCH] refac --- backend/open_webui/utils/middleware.py | 54 ++++++++++++++++---------- src/lib/components/chat/Chat.svelte | 18 ++++++--- src/lib/components/chat/FileNav.svelte | 33 +++++++++++++--- src/lib/stores/index.ts | 1 + src/routes/+layout.svelte | 7 +++- 5 files changed, 81 insertions(+), 32 deletions(-) diff --git a/backend/open_webui/utils/middleware.py b/backend/open_webui/utils/middleware.py index 2369de887..f858758bb 100644 --- a/backend/open_webui/utils/middleware.py +++ b/backend/open_webui/utils/middleware.py @@ -1043,37 +1043,49 @@ def process_tool_result( return tool_result, tool_result_files, tool_result_embeds -async def display_file_handler( +async def terminal_event_handler( tool_function_name: str, tool_function_params: dict, tool_result, event_emitter, ): - """Emit a display_file event if the tool is display_file and the file exists.""" - if not event_emitter or tool_function_name != "display_file": + """Emit terminal:* events for Open Terminal tools. + + - display_file → emits 'terminal:display_file' to open the file preview. + - write_file / replace_file_content → emits 'terminal:write_file' or + 'terminal:replace_file_content' to silently refresh the file browser. + """ + if not event_emitter: return path = tool_function_params.get("path", "") if not path: return - # Parse the result to check if the file exists - parsed = tool_result - if isinstance(parsed, str): - try: - parsed = json.loads(parsed) - except (json.JSONDecodeError, TypeError): - pass + if tool_function_name == "display_file": + # Only emit if the file actually exists + parsed = tool_result + if isinstance(parsed, str): + try: + parsed = json.loads(parsed) + except (json.JSONDecodeError, TypeError): + pass + if isinstance(parsed, dict) and parsed.get("exists") is False: + return - if isinstance(parsed, dict) and parsed.get("exists") is False: - return - - await event_emitter( - { - "type": "display_file", - "data": {"path": path}, - } - ) + await event_emitter( + { + "type": f"terminal:{tool_function_name}", + "data": {"path": path}, + } + ) + elif tool_function_name in ("write_file", "replace_file_content"): + await event_emitter( + { + "type": f"terminal:{tool_function_name}", + "data": {"path": path}, + } + ) async def chat_completion_tools_handler( @@ -1230,7 +1242,7 @@ async def chat_completion_tools_handler( ) if event_emitter: - await display_file_handler( + await terminal_event_handler( tool_function_name, tool_function_params, tool_result, @@ -4224,7 +4236,7 @@ async def streaming_chat_response_handler(response, ctx): ) ) - await display_file_handler( + await terminal_event_handler( tool_function_name, tool_function_params, tool_result, diff --git a/src/lib/components/chat/Chat.svelte b/src/lib/components/chat/Chat.svelte index 23a584dee..aaa2bded0 100644 --- a/src/lib/components/chat/Chat.svelte +++ b/src/lib/components/chat/Chat.svelte @@ -44,7 +44,8 @@ pinnedChats, showEmbeds, selectedTerminalId, - showFileNavPath + showFileNavPath, + showFileNavDir } from '$lib/stores'; import { WEBUI_API_BASE_URL } from '$lib/constants'; @@ -446,6 +447,15 @@ saveChatHandler(_chatId, history); }; + const terminalEventHandler = (type: string, data: any) => { + if (!data?.path) return; + if (type === 'terminal:display_file') { + displayFileHandler(data.path, { showControls, showFileNavPath }); + } else if (type === 'terminal:write_file' || type === 'terminal:replace_file_content') { + showFileNavDir.set(data.path); + } + }; + const chatEventHandler = async (event, cb) => { console.log(event); @@ -579,10 +589,8 @@ eventConfirmationInputPlaceholder = data.placeholder; eventConfirmationInputValue = data?.value ?? ''; eventConfirmationInputType = data?.type ?? ''; - } else if (type === 'display_file') { - if (data?.path) { - displayFileHandler(data.path, { showControls, showFileNavPath }); - } + } else if (type.startsWith('terminal:')) { + terminalEventHandler(type, data); } else { console.log('Unknown message type', data); } diff --git a/src/lib/components/chat/FileNav.svelte b/src/lib/components/chat/FileNav.svelte index efeb1ff34..8b2575d1d 100644 --- a/src/lib/components/chat/FileNav.svelte +++ b/src/lib/components/chat/FileNav.svelte @@ -6,7 +6,13 @@