From 05b8768fb9df2f079591f91fd544ad5793c793a5 Mon Sep 17 00:00:00 2001 From: Timothy Jaeryang Baek Date: Tue, 17 Feb 2026 00:48:49 -0600 Subject: [PATCH] refac --- backend/open_webui/tools/builtin.py | 63 +++++++++++++++++++++++++++++ backend/open_webui/utils/tools.py | 11 ++++- 2 files changed, 73 insertions(+), 1 deletion(-) diff --git a/backend/open_webui/tools/builtin.py b/backend/open_webui/tools/builtin.py index cec0375ab..31c79484a 100644 --- a/backend/open_webui/tools/builtin.py +++ b/backend/open_webui/tools/builtin.py @@ -1532,6 +1532,69 @@ async def search_knowledge_files( return json.dumps({"error": str(e)}) +async def view_file( + file_id: str, + __request__: Request = None, + __user__: dict = None, + __model_knowledge__: Optional[list[dict]] = None, +) -> str: + """ + Get the full content of a file by its ID. + + :param file_id: The ID of the file to retrieve + :return: JSON with the file's id, filename, and full text content + """ + if __request__ is None: + return json.dumps({"error": "Request context not available"}) + + if not __user__: + return json.dumps({"error": "User context not available"}) + + try: + from open_webui.models.files import Files + from open_webui.routers.files import has_access_to_file + + user_id = __user__.get("id") + user_role = __user__.get("role", "user") + + file = Files.get_file_by_id(file_id) + if not file: + return json.dumps({"error": "File not found"}) + + if ( + file.user_id != user_id + and user_role != "admin" + and not any( + item.get("type") == "file" and item.get("id") == file_id + for item in (__model_knowledge__ or []) + ) + and not has_access_to_file( + file_id=file_id, + access_type="read", + user=UserModel(**__user__), + ) + ): + return json.dumps({"error": "File not found"}) + + content = "" + if file.data: + content = file.data.get("content", "") + + return json.dumps( + { + "id": file.id, + "filename": file.filename, + "content": content, + "updated_at": file.updated_at, + "created_at": file.created_at, + }, + ensure_ascii=False, + ) + except Exception as e: + log.exception(f"view_file error: {e}") + return json.dumps({"error": str(e)}) + + async def view_knowledge_file( file_id: str, __request__: Request = None, diff --git a/backend/open_webui/utils/tools.py b/backend/open_webui/utils/tools.py index e7c504078..f89c9b09d 100644 --- a/backend/open_webui/utils/tools.py +++ b/backend/open_webui/utils/tools.py @@ -77,6 +77,7 @@ from open_webui.tools.builtin import ( query_knowledge_bases, search_knowledge_files, query_knowledge_files, + view_file, view_knowledge_file, view_skill, ) @@ -444,7 +445,15 @@ def get_builtin_tools( if is_builtin_tool_enabled("knowledge"): if model_knowledge: # Model has attached knowledge - only allow semantic search within it - builtin_functions.extend([query_knowledge_files, view_knowledge_file]) + builtin_functions.append(query_knowledge_files) + + knowledge_types = { + item.get("type") for item in model_knowledge + } + if "file" in knowledge_types or "collection" in knowledge_types: + builtin_functions.append(view_file) + if "note" in knowledge_types: + builtin_functions.append(view_note) else: # No model knowledge - allow full KB browsing builtin_functions.extend(