diff --git a/backend/open_webui/tools/builtin.py b/backend/open_webui/tools/builtin.py index 72dd8422a7..9bfb4c5806 100644 --- a/backend/open_webui/tools/builtin.py +++ b/backend/open_webui/tools/builtin.py @@ -3104,6 +3104,54 @@ async def view_knowledge_file( return JSONCodec.dumps({'error': str(e)}) +async def display_file( + file_id: str, + page: Optional[int] = None, + __request__: Request = None, + __user__: dict = None, + __model_knowledge__: Optional[list[dict]] = None, +) -> str: + """ + Show a chat or knowledge base file to the user inline in the chat. + After display_file succeeds, do not display the same file again or emit Markdown for it. + + :param file_id: The ID of the file to show + :param page: For PDF and PPTX files, open the preview at this 1-based page or slide number + :return: JSON with the displayed file's id, name, and content type + """ + if __request__ is None: + return JSONCodec.dumps({'error': 'Request context not available'}) + + if not __user__: + return JSONCodec.dumps({'error': 'User context not available'}) + + try: + from open_webui.models.files import Files + + file = await Files.get_file_by_id(file_id) + if not file: + return JSONCodec.dumps({'error': 'File not found'}) + + if not await _has_read_access_to_file(file, __user__, __model_knowledge__): + return JSONCodec.dumps({'error': 'File not found'}) + + file_meta = file.meta or {} + result = { + 'type': 'file', + 'source': 'open_webui', + 'displayed': True, + 'id': file.id, + 'name': file_meta.get('name') or file.filename, + 'content_type': file_meta.get('content_type'), + } + if page: + result['page'] = page + return JSONCodec.dumps(result, ensure_ascii=False) + except Exception as e: + log.exception(f'display_file error: {e}') + return JSONCodec.dumps({'error': str(e)}) + + async def list_knowledge( knowledge_id: Optional[str] = None, skip: int = 0, diff --git a/backend/open_webui/utils/middleware.py b/backend/open_webui/utils/middleware.py index 8a4e2ba902..cda54aa234 100644 --- a/backend/open_webui/utils/middleware.py +++ b/backend/open_webui/utils/middleware.py @@ -155,6 +155,7 @@ from open_webui.utils.terminals import upload_files_to_terminal from open_webui.utils.tool_approval import complete_tool_call, pending_tool_calls from open_webui.utils.tool_search import apply_tool_search, resolve_tool_call from open_webui.utils.tools import ( + add_display_file_id_param, connect_mcp_server, get_attached_knowledge, get_builtin_tools, @@ -2869,6 +2870,11 @@ async def process_chat_payload(request, form_data, user, metadata, model): for name, tool_dict in builtin_tools.items(): if name not in tools_dict: tools_dict[name] = tool_dict + elif name == 'display_file' and ( + tools_dict[name].get('type') == 'terminal' + or tools_dict[name].get('server', {}).get('is_terminal') is True + ): + tools_dict[name] = add_display_file_id_param(tools_dict[name], tool_dict) # Connections can keep shell tools advertised across browser disconnects for prompt caching. shell_tools = { @@ -3106,6 +3112,8 @@ async def execute_tool_call(form_data, metadata, event_caller, tool_call): tool = metadata.get('tools', {}).get(name) if not tool: return params, f'Error: Tool "{name}" not found.', None, None, False + if params.get('file_id') and tool.get('file_tool'): + tool = tool['file_tool'] tool_type = tool.get('type', '') direct_tool = tool.get('direct', False) # MCP servers validate their own arguments diff --git a/backend/open_webui/utils/tools.py b/backend/open_webui/utils/tools.py index f523b6ea0d..8880e33502 100644 --- a/backend/open_webui/utils/tools.py +++ b/backend/open_webui/utils/tools.py @@ -56,6 +56,7 @@ from open_webui.tools.builtin import ( delete_automation, delete_calendar_event, delete_memory, + display_file, edit_image, execute_code, fetch_url, @@ -716,7 +717,7 @@ async def get_builtin_tools( and has_chat_files and await has_user_chat_permission('file_upload') ): - builtin_functions.extend([list_chat_files, query_chat_files, grep_chat_files, view_file]) + builtin_functions.extend([list_chat_files, query_chat_files, grep_chat_files, view_file, display_file]) # Knowledge base tools - conditional injection based on model knowledge # If model has attached knowledge (any type), only provide query_knowledge_files @@ -728,6 +729,7 @@ async def get_builtin_tools( if ENABLE_KB_EXEC: builtin_functions.append(kb_exec) builtin_functions.append(query_knowledge_files) + builtin_functions.append(display_file) # Notes attached to the model need view_note since kb_exec is file-only if model_knowledge: knowledge_types = {item.get('type') for item in model_knowledge} @@ -743,7 +745,7 @@ async def get_builtin_tools( knowledge_types = {item.get('type') for item in model_knowledge} if 'file' in knowledge_types or 'collection' in knowledge_types: - builtin_functions.extend([view_file, view_knowledge_file]) + builtin_functions.extend([view_file, view_knowledge_file, display_file]) if 'note' in knowledge_types: builtin_functions.append(view_note) else: @@ -756,6 +758,7 @@ async def get_builtin_tools( search_knowledge_files, query_knowledge_files, view_knowledge_file, + display_file, ] ) @@ -1102,6 +1105,20 @@ def add_terminal_display_file_inline_param(spec: dict) -> dict: return spec +def add_display_file_id_param(terminal_tool: dict, file_tool: dict) -> dict: + """Let the terminal display_file also show chat and knowledge base files, run by the builtin tool.""" + spec = copy.deepcopy(terminal_tool['spec']) + spec['description'] = ( + f'{spec.get("description", "")} ' + 'To show a chat or knowledge base file instead, pass its file_id, which takes precedence over path; ' + 'it is always shown inline and page applies to PDF and PPTX files.' + ).strip() + parameters = spec['parameters'] + parameters['properties']['file_id'] = file_tool['spec']['parameters']['properties']['file_id'] + parameters['required'] = [name for name in parameters.get('required', []) if name != 'path'] + return {**terminal_tool, 'spec': spec, 'file_tool': file_tool} + + @cache def get_builtin_function_introspection(func: Callable): try: diff --git a/src/lib/components/chat/Messages/TerminalOutputFile.svelte b/src/lib/components/chat/Messages/TerminalOutputFile.svelte index c78b351b81..6769114b7f 100644 --- a/src/lib/components/chat/Messages/TerminalOutputFile.svelte +++ b/src/lib/components/chat/Messages/TerminalOutputFile.svelte @@ -15,6 +15,7 @@ terminalServers } from '$lib/stores'; import { downloadFileBlob, downloadFilePreview, readFile } from '$lib/apis/terminal'; + import { getFileContentById } from '$lib/apis/files'; import FilePreview from '$lib/components/chat/FileNav/FilePreview.svelte'; import Icon from '$lib/components/chat/FileNav/Icon.svelte'; import { fileIconName } from '$lib/components/chat/FileNav/fileIcon'; @@ -51,13 +52,15 @@ const SQLITE_EXTS = new Set(['db', 'sqlite', 'sqlite3', 'db3']); const OFFICE_EXTS = new Set(['docx', 'xlsx', 'xls', 'pptx']); - $: path = String(item?.full_path || item?.path || ''); + // Open WebUI files have no path; the name carries the extension. + $: path = String(item?.full_path || item?.path || item?.name || ''); + $: fileId = item?.source === 'open_webui' ? String(item.id) : ''; $: name = String(item?.name || path.split('/').filter(Boolean).at(-1) || 'file'); $: targetPage = normalizeDocumentTargetPage(item?.page); $: selector = item?.terminal_selector; $: terminal = resolveTerminal(); - $: unavailable = !terminal; - $: previewKey = terminal ? `${terminal.url}|${terminal.key}|${path}|${chatId}` : ''; + $: unavailable = !terminal && !fileId; + $: previewKey = fileId || (terminal ? `${terminal.url}|${terminal.key}|${path}|${chatId}` : ''); $: isPerChatTerminal = ($terminalServers ?? []).find((server: any) => server.id === selector)?.contexts?.chat ?.context_id === 'chat_id'; @@ -74,7 +77,7 @@ getExt(path) === 'svg' ? 'h-96' : 'h-72'; - $: if (expanded && terminal && path && previewKey !== loadedKey && !loading) { + $: if (expanded && !unavailable && path && previewKey !== loadedKey && !loading) { void loadPreview(previewKey); } @@ -125,6 +128,12 @@ } async function blobForPreview() { + if (fileId) { + const data = await getFileContentById(fileId).catch(() => null); + return data + ? { blob: new Blob([data], { type: item?.content_type ?? '' }), filename: name } + : null; + } if (!terminal) return null; return downloadFileBlob(terminal.url, terminal.key, path, chatId || undefined); } @@ -167,12 +176,9 @@ const arrayBuffer = await result.blob.arrayBuffer(); fileSqliteData = arrayBuffer; } else if (ext === 'docx') { - const preview = await downloadFilePreview( - terminal.url, - terminal.key, - path, - chatId || undefined - ); + const preview = terminal + ? await downloadFilePreview(terminal.url, terminal.key, path, chatId || undefined) + : null; if (preview) { filePdfData = await preview.blob.arrayBuffer(); } else { @@ -191,12 +197,9 @@ await loadExcelSheet(excelSheetNames[0]); } } else if (ext === 'pptx') { - const preview = await downloadFilePreview( - terminal.url, - terminal.key, - path, - chatId || undefined - ); + const preview = terminal + ? await downloadFilePreview(terminal.url, terminal.key, path, chatId || undefined) + : null; if (preview) { filePdfData = await preview.blob.arrayBuffer(); } else { @@ -208,6 +211,10 @@ fileOfficeSlides = resultImages.images; } } + } else if (fileId) { + const result = await blobForPreview(); + if (!result) throw new Error(t('Preview failed')); + fileContent = await result.blob.text(); } else if (terminal) { fileContent = await readFile(terminal.url, terminal.key, path, chatId || undefined); } @@ -225,8 +232,8 @@ } async function downloadFile() { - if (!terminal || !path) return; - const result = await downloadFileBlob(terminal.url, terminal.key, path, chatId || undefined); + if (unavailable || !path) return; + const result = await blobForPreview(); if (!result) { toast.error(t('Download failed')); return; @@ -274,15 +281,17 @@ > - + {#if !fileId} + + {/if} {#if expanded} diff --git a/src/lib/components/chat/Messages/structuredOutput.ts b/src/lib/components/chat/Messages/structuredOutput.ts index df7dfef139..dc9e4f5f8c 100644 --- a/src/lib/components/chat/Messages/structuredOutput.ts +++ b/src/lib/components/chat/Messages/structuredOutput.ts @@ -181,14 +181,15 @@ function getInlineFileFromToolOutput(callItem?: OutputItem, resultItem?: OutputI } const result = parseJSONStringValue(getToolResultText(resultItem)) as Record; + const isTerminalFile = + result?.source === 'open_terminal' && !!result.path && !!result.terminal_selector; + const isOpenWebUIFile = result?.source === 'open_webui' && !!result.id; if ( !result || typeof result !== 'object' || result.type !== 'file' || - result.source !== 'open_terminal' || result.exists === false || - !result.path || - !result.terminal_selector + !(isTerminalFile || isOpenWebUIFile) ) { return null; }