mirror of
https://github.com/open-webui/open-webui.git
synced 2026-11-24 14:30:20 -05:00
refac
This commit is contained in:
1 parent
3e6f8b7f48
commit
d878373728
3 files changed
+373
-119
No files matched your search
@@ -1,4 +1,3 @@
|
||||
import ast
|
||||
import asyncio
|
||||
import base64
|
||||
import copy
|
||||
@@ -156,6 +155,14 @@ from open_webui.utils.task import (
|
||||
from open_webui.utils.tether import get_terminal_connections
|
||||
from open_webui.utils.terminals import filter_user_terminal_tools, upload_files_to_terminal
|
||||
from open_webui.utils.tool_approval import complete_tool_call, pending_tool_calls
|
||||
from open_webui.utils.tool_calling import (
|
||||
INCOMPLETE_CALL_ERROR,
|
||||
final_answer_payload,
|
||||
prepare_tool_call,
|
||||
recover_tool_calls,
|
||||
split_tool_calls,
|
||||
tool_repetition_error,
|
||||
)
|
||||
from open_webui.utils.tool_search import apply_tool_search, resolve_tool_call
|
||||
from open_webui.utils.tools import (
|
||||
connect_mcp_server,
|
||||
@@ -397,64 +404,6 @@ def merge_streamed_reasoning_details(target: list, details) -> None:
|
||||
existing[key] = value
|
||||
|
||||
|
||||
def _split_tool_calls(
|
||||
tool_calls: list[dict],
|
||||
) -> list[dict]:
|
||||
"""Expand tool calls whose arguments contain multiple back-to-back JSON objects.
|
||||
|
||||
Some models (e.g. GPT-5.4) send multiple complete JSON argument objects
|
||||
under the same tool call index, producing concatenated invalid JSON like:
|
||||
'{"query":"A","count":5}{"query":"B","count":5}'
|
||||
|
||||
Each such tool call is split into separate entries so each gets executed
|
||||
independently. Single-object arguments pass through unchanged.
|
||||
"""
|
||||
|
||||
def split_json_objects(raw: str) -> list[str]:
|
||||
if not isinstance(raw, str):
|
||||
raw = '' if raw is None else JSONCodec.dumps(raw)
|
||||
|
||||
decoder = json.JSONDecoder()
|
||||
results = []
|
||||
position = 0
|
||||
|
||||
while position < len(raw):
|
||||
while position < len(raw) and raw[position].isspace():
|
||||
position += 1
|
||||
if position >= len(raw):
|
||||
break
|
||||
try:
|
||||
_, end = decoder.raw_decode(raw, position)
|
||||
results.append(raw[position:end].strip())
|
||||
position = end
|
||||
except JSONCodec.JSONDecodeError:
|
||||
return [raw]
|
||||
|
||||
return results or [raw]
|
||||
|
||||
expanded = []
|
||||
for tool_call in tool_calls:
|
||||
function = tool_call.setdefault('function', {})
|
||||
arguments = function.get('arguments')
|
||||
if not isinstance(arguments, str):
|
||||
arguments = '' if arguments is None else JSONCodec.dumps(arguments)
|
||||
function['arguments'] = arguments
|
||||
split_arguments = split_json_objects(arguments)
|
||||
|
||||
if len(split_arguments) <= 1:
|
||||
expanded.append(tool_call)
|
||||
else:
|
||||
for position, argument in enumerate(split_arguments):
|
||||
cloned = copy.deepcopy(tool_call)
|
||||
# The first split keeps the original id so it maps to the streamed output item.
|
||||
if position > 0:
|
||||
cloned['id'] = f'call_{uuid4().hex[:24]}'
|
||||
cloned['function']['arguments'] = argument
|
||||
expanded.append(cloned)
|
||||
|
||||
return expanded
|
||||
|
||||
|
||||
CITATION_TOOL_NAMES = ('fetch_url', 'view_file', 'view_knowledge_file', 'query_knowledge_files', 'query_chat_files')
|
||||
|
||||
|
||||
@@ -1124,10 +1073,10 @@ async def chat_completion_tools_handler(
|
||||
tool_type = tool.get('type', '')
|
||||
direct_tool = tool.get('direct', False)
|
||||
|
||||
if tool_type != 'mcp':
|
||||
spec = tool.get('spec', {})
|
||||
allowed_params = spec.get('parameters', {}).get('properties', {}).keys()
|
||||
tool_function_params = {k: v for k, v in tool_function_params.items() if k in allowed_params}
|
||||
prepared = {'name': tool_function_name, 'arguments': tool_function_params}
|
||||
if error := prepare_tool_call(prepared, tool.get('spec', {}).get('parameters')):
|
||||
raise ValueError(error)
|
||||
tool_function_params = JSONCodec.loads(prepared['arguments'])
|
||||
|
||||
if tool.get('direct', False):
|
||||
tool_result = await event_caller(
|
||||
@@ -3093,33 +3042,22 @@ async def build_chat_response_context(request, form_data, user, model, metadata,
|
||||
}
|
||||
|
||||
|
||||
async def execute_tool_call(form_data, metadata, event_caller, tool_call):
|
||||
"""Execute the saved function_call item, already resolved before approval staging."""
|
||||
async def execute_tool_call(form_data, metadata, event_caller, tool_call, history=None):
|
||||
"""Execute a resolved call; resumed approvals use the same validation and loop policy."""
|
||||
name = tool_call.get('name', '')
|
||||
tool_args = tool_call.get('arguments', '{}')
|
||||
params = {}
|
||||
if tool_args and tool_args.strip():
|
||||
try:
|
||||
params = JSONCodec.loads(tool_args)
|
||||
except Exception:
|
||||
try:
|
||||
params = ast.literal_eval(tool_args)
|
||||
except Exception as e:
|
||||
log.debug(e)
|
||||
return {}, 'Error: Tool arguments are not valid JSON.', None, None, False
|
||||
if not isinstance(params, dict):
|
||||
return (
|
||||
{},
|
||||
f'Error: Tool call arguments for `{name}` must be a JSON object. Please try again.',
|
||||
None,
|
||||
None,
|
||||
False,
|
||||
)
|
||||
tool_call['arguments'] = JSONCodec.dumps(params)
|
||||
|
||||
tool = metadata.get('tools', {}).get(name)
|
||||
if not tool:
|
||||
return params, f'Error: Tool "{name}" not found.', None, None, False
|
||||
error = (
|
||||
prepare_tool_call(tool_call, tool.get('spec', {}).get('parameters'))
|
||||
if tool
|
||||
else f'Error: Tool "{name}" not found.'
|
||||
)
|
||||
if history is not None:
|
||||
error = error or tool_repetition_error(tool_call, history)
|
||||
history.append(tool_call)
|
||||
tool_call['status'] = 'failed' if error else 'in_progress'
|
||||
if error:
|
||||
return {}, error, None, None, False
|
||||
params = JSONCodec.loads(tool_call['arguments'])
|
||||
if name == 'display_file' and (
|
||||
str(params.get('path', '')).startswith('/api/v1/files/')
|
||||
or re.fullmatch(r'[0-9a-fA-F]{8}(?:-[0-9a-fA-F]{4}){3}-[0-9a-fA-F]{12}', str(params.get('path', '')))
|
||||
@@ -3127,14 +3065,10 @@ async def execute_tool_call(form_data, metadata, event_caller, tool_call):
|
||||
if tool.get('file_tool'):
|
||||
tool = tool['file_tool']
|
||||
elif tool.get('type') == 'terminal' or tool.get('server', {}).get('is_terminal') is True:
|
||||
tool_call['status'] = 'failed'
|
||||
return params, 'Error: Uploaded file display is not available.', None, None, False
|
||||
tool_type = tool.get('type', '')
|
||||
direct_tool = tool.get('direct', False)
|
||||
# MCP servers validate their own arguments
|
||||
if tool_type != 'mcp':
|
||||
allowed_params = tool.get('spec', {}).get('parameters', {}).get('properties', {}).keys()
|
||||
params = {key: value for key, value in params.items() if key in allowed_params}
|
||||
|
||||
try:
|
||||
if (
|
||||
not is_saved_chat_id(metadata.get('chat_id'))
|
||||
@@ -3151,6 +3085,7 @@ async def execute_tool_call(form_data, metadata, event_caller, tool_call):
|
||||
is not True # Disconnection and timeout replies are error objects, not approval.
|
||||
)
|
||||
):
|
||||
tool_call['status'] = 'failed'
|
||||
return params, 'Error: tool call was not approved.', tool, tool_type, direct_tool
|
||||
|
||||
tool_call['started_at'] = time.time()
|
||||
@@ -3186,6 +3121,7 @@ async def execute_tool_call(form_data, metadata, event_caller, tool_call):
|
||||
tool_call['duration'] = time.perf_counter() - started
|
||||
except Exception as e:
|
||||
result = {'error': str(e)}
|
||||
tool_call['status'] = 'failed' if _is_tool_result_error(result) else 'completed'
|
||||
return params, result, tool, tool_type, direct_tool
|
||||
|
||||
|
||||
@@ -3267,8 +3203,10 @@ async def resume_tool_calls(request, form_data, user, model, metadata, message)
|
||||
item = copy.deepcopy(item)
|
||||
|
||||
try:
|
||||
history = [call for call in output if call.get('type') == 'function_call']
|
||||
history = history[: next(i for i, call in enumerate(history) if call.get('call_id') == item['call_id'])]
|
||||
params, result, tool, tool_type, direct_tool = await execute_tool_call(
|
||||
form_data, metadata, event_caller, item
|
||||
form_data, metadata, event_caller, item, history
|
||||
)
|
||||
files, embeds = [], []
|
||||
if result is None and tool is None:
|
||||
@@ -4700,6 +4638,7 @@ async def streaming_chat_response_handler(response, ctx):
|
||||
nonlocal stream_size
|
||||
|
||||
response_tool_calls = []
|
||||
finish_reason = None
|
||||
prior_usage = usage
|
||||
current_usage = {}
|
||||
form_data['model'] = current_form_data['model']
|
||||
@@ -4875,6 +4814,10 @@ async def streaming_chat_response_handler(response, ctx):
|
||||
# Check for Responses API events
|
||||
elif data.get('type', '').startswith('response.') or data.get('type', '') == 'error':
|
||||
response_data_type = data.get('type', '')
|
||||
if response_data_type in {'response.incomplete', 'response.failed', 'error'}:
|
||||
finish_reason = 'incomplete'
|
||||
elif response_data_type == 'response.completed':
|
||||
finish_reason = data.get('response', {}).get('status', 'completed')
|
||||
response_data_is_delta = response_data_type.endswith('.delta')
|
||||
output, response_metadata = handle_responses_streaming_event(data, output)
|
||||
|
||||
@@ -5544,6 +5487,49 @@ async def streaming_chat_response_handler(response, ctx):
|
||||
)
|
||||
reasoning_item['status'] = 'completed'
|
||||
|
||||
incomplete = finish_reason in {'length', 'content_filter', 'incomplete', 'failed'}
|
||||
if (
|
||||
not response_tool_calls
|
||||
and not incomplete
|
||||
and current_form_data.get('tool_choice') != 'none'
|
||||
and current_form_data.get('response_format') in (None, {'type': 'text'})
|
||||
):
|
||||
text_items = [item for item in output if item.get('type') == 'message']
|
||||
text = ''.join(
|
||||
part.get('text', '')
|
||||
for item in text_items
|
||||
for part in item.get('content', [])
|
||||
if part.get('type') == 'output_text'
|
||||
)
|
||||
allowed = {
|
||||
spec.get('function', spec).get('name') for spec in current_form_data.get('tools') or []
|
||||
}
|
||||
choice = current_form_data.get('tool_choice')
|
||||
if isinstance(choice, dict):
|
||||
allowed &= {choice.get('function', choice).get('name')}
|
||||
recovered = recover_tool_calls(text, allowed)
|
||||
if recovered and not any(item.get('type') == 'function_call' for item in output):
|
||||
response_tool_calls = recovered
|
||||
output = [item for item in output if item not in text_items]
|
||||
content_parts[:] = [get_output_text(full_output())]
|
||||
for call in recovered:
|
||||
output.append(
|
||||
{
|
||||
'type': 'function_call',
|
||||
'id': call['id'],
|
||||
'call_id': call['id'],
|
||||
**call['function'],
|
||||
'status': 'in_progress',
|
||||
'recovered_from_text': True,
|
||||
}
|
||||
)
|
||||
await event_emitter(
|
||||
{
|
||||
'type': 'chat:completion',
|
||||
'data': {'output': full_output(), 'flush': True},
|
||||
}
|
||||
)
|
||||
|
||||
if response_tool_calls:
|
||||
for tc in response_tool_calls:
|
||||
call_id = tc.get('id', '')
|
||||
@@ -5568,7 +5554,11 @@ async def streaming_chat_response_handler(response, ctx):
|
||||
}
|
||||
)
|
||||
break
|
||||
tool_calls.append(_split_tool_calls(response_tool_calls))
|
||||
calls = split_tool_calls(response_tool_calls) if not incomplete else response_tool_calls
|
||||
if incomplete:
|
||||
for call in calls:
|
||||
call['execution_error'] = INCOMPLETE_CALL_ERROR
|
||||
tool_calls.append(calls)
|
||||
|
||||
# Responses API path: extract function_call items from output
|
||||
if not response_tool_calls and output:
|
||||
@@ -5589,6 +5579,11 @@ async def streaming_chat_response_handler(response, ctx):
|
||||
{
|
||||
'id': call_id,
|
||||
'index': len(responses_api_tool_calls),
|
||||
**(
|
||||
{'execution_error': INCOMPLETE_CALL_ERROR}
|
||||
if incomplete or item.get('status') != 'completed'
|
||||
else {}
|
||||
),
|
||||
'function': {
|
||||
'name': item.get('name', ''),
|
||||
'arguments': (
|
||||
@@ -5598,7 +5593,7 @@ async def streaming_chat_response_handler(response, ctx):
|
||||
}
|
||||
)
|
||||
if responses_api_tool_calls:
|
||||
tool_calls.append(_split_tool_calls(responses_api_tool_calls))
|
||||
tool_calls.append(split_tool_calls(responses_api_tool_calls))
|
||||
|
||||
await save_current_response_stream()
|
||||
if not current_usage:
|
||||
@@ -5613,12 +5608,13 @@ async def streaming_chat_response_handler(response, ctx):
|
||||
if response.background:
|
||||
await response.background()
|
||||
|
||||
tool_history = [item for item in prior_output if item.get('type') == 'function_call']
|
||||
tool_call_iterations = 0
|
||||
max_tool_call_iterations = getattr(
|
||||
request.state,
|
||||
'max_tool_call_iterations',
|
||||
CHAT_RESPONSE_MAX_TOOL_CALL_ITERATIONS,
|
||||
request.state, 'max_tool_call_iterations', CHAT_RESPONSE_MAX_TOOL_CALL_ITERATIONS
|
||||
)
|
||||
final_answer_requested = form_data.get('tool_choice') == 'none'
|
||||
stop_reason = None
|
||||
tool_call_sources = [] # Track citation sources from tool results
|
||||
all_tool_call_sources = [] # Accumulated sources across all iterations
|
||||
user_message = get_last_user_message(form_data['messages'])
|
||||
@@ -5672,10 +5668,14 @@ async def streaming_chat_response_handler(response, ctx):
|
||||
}
|
||||
)
|
||||
|
||||
while tool_calls and (
|
||||
max_tool_call_iterations is None or tool_call_iterations < max_tool_call_iterations
|
||||
):
|
||||
tool_call_iterations += 1
|
||||
while tool_calls:
|
||||
round_error = stop_reason
|
||||
if max_tool_call_iterations is not None and tool_call_iterations >= max_tool_call_iterations:
|
||||
round_error = 'Tool-call iteration limit reached.'
|
||||
if final_answer_requested:
|
||||
round_error = round_error or 'Tool calls are disabled for this response.'
|
||||
if not round_error:
|
||||
tool_call_iterations += 1
|
||||
|
||||
# Resolve dispatch before approvals and special handling such as ask_user.
|
||||
response_tool_calls = []
|
||||
@@ -5683,7 +5683,9 @@ async def streaming_chat_response_handler(response, ctx):
|
||||
item.get('call_id'): item for item in output if item.get('type') == 'function_call'
|
||||
}
|
||||
for tool_call in tool_calls.pop(0):
|
||||
error = resolve_tool_call(tool_call, metadata.get('tools', {}))
|
||||
error = tool_call.get('execution_error') or resolve_tool_call(
|
||||
tool_call, metadata.get('tools', {})
|
||||
)
|
||||
call_id = tool_call.get('id') or output_id('fc')
|
||||
tool_call['id'] = call_id
|
||||
function = tool_call.get('function', {})
|
||||
@@ -5697,6 +5699,19 @@ async def streaming_chat_response_handler(response, ctx):
|
||||
arguments=function.get('arguments', '{}'),
|
||||
status='failed' if error else item.get('status', 'in_progress'),
|
||||
)
|
||||
tool = metadata.get('tools', {}).get(item['name'])
|
||||
error = error or (
|
||||
prepare_tool_call(item, tool.get('spec', {}).get('parameters'))
|
||||
if tool
|
||||
else f'Error: Tool "{item["name"]}" not found.'
|
||||
)
|
||||
blocked = tool_repetition_error(item, tool_history)
|
||||
stop_reason = stop_reason or round_error or blocked or tool_call.get('execution_error')
|
||||
error = f'Error: {round_error}' if round_error else error or blocked
|
||||
if error:
|
||||
tool_history.append(item)
|
||||
function['arguments'] = item['arguments']
|
||||
item['status'] = 'failed' if error else item['status']
|
||||
if 'provider_function' in tool_call:
|
||||
item['provider_function'] = tool_call['provider_function']
|
||||
if error:
|
||||
@@ -5771,14 +5786,14 @@ async def streaming_chat_response_handler(response, ctx):
|
||||
for tool_call in response_tool_calls:
|
||||
if tool_call.get('name') != 'delegate_task':
|
||||
tool_results[tool_call['call_id']] = await execute_tool_call(
|
||||
form_data, metadata, event_caller, tool_call
|
||||
form_data, metadata, event_caller, tool_call, tool_history
|
||||
)
|
||||
tool_results.update(
|
||||
zip(
|
||||
[tool_call['call_id'] for tool_call in delegate_calls],
|
||||
await asyncio.gather(
|
||||
*(
|
||||
execute_tool_call(form_data, metadata, event_caller, tool_call)
|
||||
execute_tool_call(form_data, metadata, event_caller, tool_call, tool_history)
|
||||
for tool_call in delegate_calls
|
||||
)
|
||||
),
|
||||
@@ -5946,6 +5961,9 @@ async def streaming_chat_response_handler(response, ctx):
|
||||
tool_call_sources.clear()
|
||||
|
||||
await emit_output()
|
||||
if final_answer_requested:
|
||||
await emit_message_error(round_error or 'The provider requested tools after tool use stopped.')
|
||||
break
|
||||
|
||||
try:
|
||||
new_form_data = {
|
||||
@@ -6103,6 +6121,11 @@ async def streaming_chat_response_handler(response, ctx):
|
||||
extra_params=extra_params,
|
||||
)
|
||||
|
||||
final_answer_requested = bool(stop_reason) or (
|
||||
max_tool_call_iterations is not None and tool_call_iterations >= max_tool_call_iterations
|
||||
)
|
||||
if final_answer_requested:
|
||||
new_form_data = final_answer_payload(new_form_data)
|
||||
new_form_data = normalize_messages_for_model(new_form_data)
|
||||
|
||||
res = await generate_chat_completion(
|
||||
@@ -6143,15 +6166,6 @@ async def streaming_chat_response_handler(response, ctx):
|
||||
await emit_message_error(error_content)
|
||||
break
|
||||
|
||||
if (
|
||||
max_tool_call_iterations is not None
|
||||
and tool_calls
|
||||
and tool_call_iterations >= max_tool_call_iterations
|
||||
):
|
||||
log.warning('Tool-call iteration limit reached (%s)', max_tool_call_iterations)
|
||||
error_content = f'Tool-call limit reached ({max_tool_call_iterations} iterations).'
|
||||
await emit_message_error(error_content)
|
||||
|
||||
if DETECT_CODE_INTERPRETER:
|
||||
MAX_RETRIES = 5
|
||||
retries = 0
|
||||
|
||||
@@ -0,0 +1,241 @@
|
||||
"""Tool-call parsing, validation, and repetition checks.
|
||||
|
||||
Pure helpers for complete argument parsing, schema normalization, explicit tool
|
||||
markup, and repetition checks. Callers own execution, history, and persistence.
|
||||
"""
|
||||
|
||||
import ast
|
||||
import copy
|
||||
import json
|
||||
import math
|
||||
import re
|
||||
from uuid import uuid4
|
||||
|
||||
from jsonschema import Draft202012Validator
|
||||
from referencing import Registry
|
||||
from referencing.exceptions import Unresolvable
|
||||
|
||||
FINAL_ANSWER_PROMPT = (
|
||||
'Tool use has stopped for this response. Use the results already available to answer the user. '
|
||||
'Explain any unfinished work briefly. Do not request or describe additional tool calls.'
|
||||
)
|
||||
INCOMPLETE_CALL_ERROR = (
|
||||
'Error: the model response was incomplete; its tool calls were not executed. Retry a complete call.'
|
||||
)
|
||||
|
||||
|
||||
def parse_arguments(raw):
|
||||
"""Accept complete JSON or JSON-compatible Python literals, never invent missing values."""
|
||||
if isinstance(raw, str):
|
||||
text = raw.strip()
|
||||
fence = re.fullmatch(r'```(?:json)?\s*\n(.*?)\n```', text, re.DOTALL)
|
||||
if fence:
|
||||
text = fence[1]
|
||||
try:
|
||||
raw = json.loads(text or '{}')
|
||||
except (ValueError, RecursionError):
|
||||
try:
|
||||
# Literal recovery is bounded independently of valid JSON/code arguments.
|
||||
if len(text) > 65_536:
|
||||
raise ValueError('Arguments are too large for literal recovery.')
|
||||
raw = ast.literal_eval(text)
|
||||
except (ValueError, SyntaxError, RecursionError, MemoryError) as exc:
|
||||
raise ValueError('Tool arguments must be a complete JSON object.') from exc
|
||||
if not isinstance(raw, dict):
|
||||
raise ValueError('Tool arguments must be a JSON object.')
|
||||
_check_json_value(raw)
|
||||
return raw
|
||||
|
||||
|
||||
def _check_json_value(value, depth=0):
|
||||
if depth > 32:
|
||||
raise ValueError('Tool arguments are nested too deeply.')
|
||||
if isinstance(value, dict):
|
||||
for key, child in value.items():
|
||||
if not isinstance(key, str):
|
||||
raise ValueError('Tool argument keys must be strings.')
|
||||
_check_json_value(child, depth + 1)
|
||||
elif isinstance(value, list):
|
||||
for child in value:
|
||||
_check_json_value(child, depth + 1)
|
||||
elif isinstance(value, float) and not math.isfinite(value):
|
||||
raise ValueError('Tool arguments must contain finite numbers.')
|
||||
elif value is not None and not isinstance(value, (str, int, float, bool)):
|
||||
raise ValueError('Tool arguments must contain JSON values only.')
|
||||
|
||||
|
||||
def _coerce_scalar(value, kind):
|
||||
if kind == 'string' and isinstance(value, (int, float)) and not isinstance(value, bool):
|
||||
return str(value)
|
||||
if not isinstance(value, str):
|
||||
return value
|
||||
text = value.strip()
|
||||
if kind == 'boolean' and text.lower() in {'true', 'false'}:
|
||||
return text.lower() == 'true'
|
||||
if kind not in {'integer', 'number', 'object', 'array'}:
|
||||
return value
|
||||
try:
|
||||
parsed = json.loads(text)
|
||||
except (ValueError, RecursionError):
|
||||
return value
|
||||
if kind in {'object', 'array'}:
|
||||
return parsed if isinstance(parsed, dict if kind == 'object' else list) else value
|
||||
if type(parsed) is int or (kind == 'number' and type(parsed) is float and math.isfinite(parsed)):
|
||||
return parsed
|
||||
return value
|
||||
|
||||
|
||||
def _coerce(value, schema, depth=0):
|
||||
if not isinstance(schema, dict) or depth > 32:
|
||||
return value
|
||||
# Ambiguous unions/references must be validated without guessing which branch was intended.
|
||||
if any(key in schema for key in ('$ref', 'oneOf', 'anyOf', 'allOf', 'if')):
|
||||
return value
|
||||
kind = schema.get('type')
|
||||
if isinstance(kind, list):
|
||||
kinds = [item for item in kind if item != 'null']
|
||||
kind = kinds[0] if len(kinds) == 1 else None
|
||||
value = _coerce_scalar(value, kind)
|
||||
if isinstance(value, dict):
|
||||
properties = schema.get('properties', {})
|
||||
return {key: _coerce(child, properties.get(key, {}), depth + 1) for key, child in value.items()}
|
||||
if isinstance(value, list) and isinstance(schema.get('items'), dict):
|
||||
return [_coerce(child, schema['items'], depth + 1) for child in value]
|
||||
return value
|
||||
|
||||
|
||||
def normalize_arguments(raw, schema=None):
|
||||
params = parse_arguments(raw)
|
||||
if schema is not None:
|
||||
params = _coerce(params, schema)
|
||||
try:
|
||||
# Schemas may use local $defs; fetching arbitrary external refs is never part of tool execution.
|
||||
error = next(Draft202012Validator(schema, registry=Registry()).iter_errors(params), None)
|
||||
except Unresolvable as exc:
|
||||
raise ValueError('Tool schema contains an unresolved reference.') from exc
|
||||
if error:
|
||||
path = '.'.join(str(part) for part in error.absolute_path) or 'arguments'
|
||||
raise ValueError(f'Invalid {path}: {error.message}')
|
||||
_check_json_value(params)
|
||||
return params
|
||||
|
||||
|
||||
def split_tool_calls(calls):
|
||||
"""Split complete concatenated objects, preserving each requested call."""
|
||||
expanded = []
|
||||
decoder = json.JSONDecoder()
|
||||
for call in calls:
|
||||
call = copy.deepcopy(call)
|
||||
function = call.setdefault('function', {})
|
||||
raw = function.get('arguments')
|
||||
raw = raw if isinstance(raw, str) else json.dumps(raw) if raw is not None else ''
|
||||
function['arguments'] = raw
|
||||
objects, position = [], 0
|
||||
try:
|
||||
while position < len(raw):
|
||||
if raw[position].isspace():
|
||||
position += 1
|
||||
continue
|
||||
value, position = decoder.raw_decode(raw, position)
|
||||
if not isinstance(value, dict):
|
||||
raise ValueError('Expected an object')
|
||||
objects.append(value)
|
||||
except (ValueError, RecursionError):
|
||||
objects = []
|
||||
if len(objects) <= 1:
|
||||
expanded.append(call)
|
||||
continue
|
||||
for index, value in enumerate(objects):
|
||||
cloned = copy.deepcopy(call)
|
||||
if index:
|
||||
cloned['id'] = f'call_{uuid4().hex[:24]}'
|
||||
cloned['function']['arguments'] = json.dumps(value, ensure_ascii=False)
|
||||
expanded.append(cloned)
|
||||
return expanded
|
||||
|
||||
|
||||
_CALL_BLOCK = re.compile(r'<tool_call>(.*?)</tool_call>|<function=([\w.\-]+)>(.*?)</function>', re.DOTALL)
|
||||
_PARAMETER = re.compile(r'<parameter=([\w.\-]+)>(.*?)</parameter>', re.DOTALL)
|
||||
|
||||
|
||||
def recover_tool_calls(text, allowed_names):
|
||||
"""Recover a message consisting ONLY of complete call wrappers; examples and prose stay text."""
|
||||
if not isinstance(text, str) or len(text) > 65_536:
|
||||
return []
|
||||
calls, cursor = [], 0
|
||||
for match in _CALL_BLOCK.finditer(text):
|
||||
if text[cursor : match.start()].strip():
|
||||
return []
|
||||
try:
|
||||
if match[1] is not None:
|
||||
data = json.loads(match[1])
|
||||
function = data.get('function', data)
|
||||
name = function['name']
|
||||
arguments = function.get('arguments', function.get('parameters', {}))
|
||||
else:
|
||||
name, body = match[2], match[3]
|
||||
arguments, end = {}, 0
|
||||
for parameter in _PARAMETER.finditer(body):
|
||||
if body[end : parameter.start()].strip() or parameter[1] in arguments:
|
||||
return []
|
||||
arguments[parameter[1]] = parameter[2]
|
||||
end = parameter.end()
|
||||
if body[end:].strip():
|
||||
return []
|
||||
if name not in allowed_names:
|
||||
return []
|
||||
arguments = parse_arguments(arguments)
|
||||
except (ValueError, TypeError, KeyError, AttributeError, RecursionError):
|
||||
return []
|
||||
calls.append(
|
||||
{
|
||||
'id': f'call_{uuid4().hex[:24]}',
|
||||
'type': 'function',
|
||||
'function': {'name': name, 'arguments': json.dumps(arguments, ensure_ascii=False)},
|
||||
}
|
||||
)
|
||||
cursor = match.end()
|
||||
return calls if not text[cursor:].strip() else []
|
||||
|
||||
|
||||
def tool_call_key(call):
|
||||
"""Compare calls by name and arguments, independently of provider IDs."""
|
||||
arguments = call.get('arguments', '{}')
|
||||
try:
|
||||
arguments = parse_arguments(arguments)
|
||||
except ValueError:
|
||||
arguments = arguments if isinstance(arguments, str) else repr(arguments)
|
||||
return call.get('name', ''), json.dumps(arguments, sort_keys=True, ensure_ascii=False)
|
||||
|
||||
|
||||
def tool_repetition_error(call, previous_calls):
|
||||
"""Stop unchanged retries after two consecutive failures; successful polls can repeat."""
|
||||
key = tool_call_key(call)
|
||||
recent = previous_calls[-2:]
|
||||
if len(recent) == 2 and all(tool_call_key(item) == key and item.get('status') == 'failed' for item in recent):
|
||||
return 'Error: the same tool call failed twice. Finish using the available results.'
|
||||
return None
|
||||
|
||||
|
||||
def prepare_tool_call(call, schema=None):
|
||||
"""Normalize a call before approval; return an actionable error for the model."""
|
||||
raw = call.get('arguments', '{}')
|
||||
try:
|
||||
arguments = normalize_arguments(raw, schema)
|
||||
except ValueError as exc:
|
||||
return f'Error: {exc} Correct the arguments before retrying.'
|
||||
normalized = json.dumps(arguments, ensure_ascii=False)
|
||||
if normalized != raw:
|
||||
call.setdefault('original_arguments', raw)
|
||||
call['arguments'] = normalized
|
||||
return None
|
||||
|
||||
|
||||
def final_answer_payload(payload):
|
||||
"""Make one OpenAI-compatible continuation request with tools disabled."""
|
||||
return {
|
||||
**payload,
|
||||
'tools': [],
|
||||
'tool_choice': 'none',
|
||||
'messages': [*payload.get('messages', []), {'role': 'system', 'content': FINAL_ANSWER_PROMPT}],
|
||||
}
|
||||
@@ -12,6 +12,7 @@ import regex
|
||||
from open_webui.models.config import Config
|
||||
from open_webui.utils.json_codec import JSONCodec
|
||||
from open_webui.utils.misc import add_or_update_system_message
|
||||
from open_webui.utils.tool_calling import parse_arguments
|
||||
|
||||
SEARCH_TOOL_NAME = 'search_tools'
|
||||
CALL_TOOL_NAME = 'call_tool'
|
||||
@@ -141,7 +142,7 @@ def resolve_tool_call(tool_call: dict, tools: dict[str, dict]) -> str | None:
|
||||
return None
|
||||
|
||||
try:
|
||||
arguments = JSONCodec.loads(function.get('arguments', ''))
|
||||
arguments = parse_arguments(function.get('arguments', ''))
|
||||
if not isinstance(arguments, dict) or set(arguments) != {'tool_name', 'params'}:
|
||||
raise ValueError('call_tool requires tool_name and params.')
|
||||
name = arguments['tool_name']
|
||||
@@ -151,9 +152,7 @@ def resolve_tool_call(tool_call: dict, tools: dict[str, dict]) -> str | None:
|
||||
raise ValueError('call_tool cannot invoke search_tools or itself.')
|
||||
if name not in tools:
|
||||
raise ValueError(f'Tool "{name}" is not available. Search for an available tool.')
|
||||
if not isinstance(arguments['params'], str):
|
||||
raise ValueError('params must be a JSON-encoded object string.')
|
||||
params = JSONCodec.loads(arguments['params'])
|
||||
params = parse_arguments(arguments['params'])
|
||||
if not isinstance(params, dict):
|
||||
raise ValueError('params must decode to a JSON object.')
|
||||
except (TypeError, ValueError) as error:
|
||||
|
||||
Reference in new issue
Block a user