This commit is contained in:
Timothy Jaeryang Baek
2026-07-27 00:27:38 -04:00
parent 20647bd2d5
commit 57e60423b9
4 changed files with 349 additions and 47 deletions
+9 -4
View File
@@ -278,9 +278,9 @@ def get_citation_source_from_tool_result(
- document: list of document contents
- metadata: list of metadata objects with source, file_id, name fields
Returns a list of sources (usually one, but query_knowledge_files may return multiple).
Returns a list of sources (usually one, but query_knowledge_files/query_chat_files may return multiple).
"""
_EXPECTS_LIST = {'search_web', 'query_knowledge_files'}
_EXPECTS_LIST = {'search_web', 'query_knowledge_files', 'query_chat_files'}
_EXPECTS_DICT = {'view_knowledge_file', 'view_file'}
try:
@@ -369,7 +369,7 @@ def get_citation_source_from_tool_result(
}
]
elif tool_name == 'query_knowledge_files':
elif tool_name in ('query_knowledge_files', 'query_chat_files'):
chunks = tool_result
# Group chunks by source for better citation display
@@ -1565,7 +1565,11 @@ async def add_file_context(messages: list, chat_id: str, user) -> list:
stored_messages = get_message_list(history.get('messages', {}), history.get('currentId'))
def format_file_tag(file):
attrs = f'type="{file.get("type", "file")}" url="{file["url"]}"'
file_id = file.get('id') or file.get('url')
attrs = f'type="{file.get("type", "file")}"'
if file_id:
attrs += f' id="{file_id}"'
attrs += f' url="{file["url"]}"'
if file.get('content_type'):
attrs += f' content_type="{file["content_type"]}"'
if file.get('name'):
@@ -4892,6 +4896,7 @@ async def streaming_chat_response_handler(response, ctx):
'view_file',
'view_knowledge_file',
'query_knowledge_files',
'query_chat_files',
]
and tool_result
):
+32 -1
View File
@@ -62,14 +62,17 @@ from open_webui.tools.builtin import (
fetch_url,
generate_image,
get_current_timestamp,
grep_chat_files,
grep_knowledge_files,
kb_exec,
list_chat_files,
list_automations,
list_knowledge,
list_knowledge_bases,
list_memories,
list_memory_paths,
notify,
query_chat_files,
query_knowledge_bases,
query_knowledge_files,
read_memory_path,
@@ -519,10 +522,38 @@ async def get_builtin_tools(
await Config.get('user.permissions'),
)
async def has_user_chat_permission(permission_key: str) -> bool:
if user.get('role') == 'admin':
return True
return await has_permission(
user.get('id', ''),
f'chat.{permission_key}',
await Config.get('user.permissions'),
)
# Time utilities - available for date calculations
if is_builtin_tool_enabled('time'):
builtin_functions.extend([get_current_timestamp, calculate_timestamp])
metadata = extra_params.get('__metadata__') or {}
chat_files = metadata.get('files') or extra_params.get('__files__') or []
has_chat_files = any(
isinstance(item, dict)
and item.get('type', 'file') == 'file'
and (item.get('id') or item.get('url'))
and not str(item.get('id') or item.get('url')).startswith(('http://', 'https://', 'data:'))
for item in chat_files
)
if (
is_builtin_tool_enabled('files')
and get_model_capability('file_upload')
and not get_model_capability('file_context')
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])
# Knowledge base tools - conditional injection based on model knowledge
# If model has attached knowledge (any type), only provide query_knowledge_files
# Otherwise, provide all KB browsing tools
@@ -640,7 +671,6 @@ async def get_builtin_tools(
):
builtin_functions.append(execute_code)
metadata = extra_params.get('__metadata__') or {}
chat_id = metadata.get('chat_id') or ''
chat = None
if is_saved_chat_id(chat_id):
@@ -709,6 +739,7 @@ async def get_builtin_tools(
'__event_emitter__': extra_params.get('__event_emitter__'),
'__event_call__': extra_params.get('__event_call__'),
'__metadata__': extra_params.get('__metadata__'),
'__files__': chat_files,
'__chat_id__': extra_params.get('__chat_id__'),
'__message_id__': extra_params.get('__message_id__'),
'__model_knowledge__': model_knowledge,