diff --git a/backend/open_webui/utils/automations.py b/backend/open_webui/utils/automations.py index 5cf1ef393b..eb123d02e7 100644 --- a/backend/open_webui/utils/automations.py +++ b/backend/open_webui/utils/automations.py @@ -62,6 +62,8 @@ from open_webui.utils.machines import ( resolve_machine_connection, ) from pydantic_monty import AsyncMonty +from open_webui.socket.main import get_event_emitter +from open_webui.utils.tool_approval import pending_tool_calls from starlette.datastructures import Headers log = logging.getLogger(__name__) @@ -620,6 +622,12 @@ async def _execute_automation( memory = await Config.get('memories.enable') if memory: form_data.setdefault('features', {})['memory'] = True + # Tools ask for approval the way they do when the owner chats there: the chat's own mode, else their default. + approval_mode = ((chat.chat or {}).get('params') or {}).get('tool_approval_mode') or ( + ui_settings.get('params') or {} + ).get('tool_approval_mode') + if approval_mode: + form_data['params'] = {'tool_approval_mode': approval_mode} # Call the full chat completion pipeline (same as POST /api/chat/completions). # The handler reference is stored on app.state to avoid circular imports. @@ -638,6 +646,31 @@ async def _execute_automation( reply = await Chats.get_message_by_id_and_message_id(chat.id, assistant_msg_id) if reply and reply.get('error'): raise ValueError(str(reply['error']['content'])) + if reply and not reply.get('done') and pending_tool_calls(reply.get('output') or []): + # Nobody is there to approve, so close the reply instead of leaving it waiting. + reason = 'needed approval, and nobody was there to give it' + async with Chats.edit_message_output(chat.id, assistant_msg_id) as message: + names = [] + for call in pending_tool_calls(message.get('output') or []): + call['status'] = 'rejected' + names.append(call.get('name') or 'A tool') + message['output'].append( + { + 'type': 'function_call_output', + 'id': f"fco_{call.get('call_id')}", + 'call_id': call.get('call_id'), + 'output': [{'type': 'input_text', 'text': f'Error: tool call {reason}.'}], + 'status': 'rejected', + } + ) + message['done'] = True + message['error'] = {'content': f"{', '.join(names)} {reason}."} + emitter = await get_event_emitter( + {'user_id': user.id, 'chat_id': chat.id, 'message_id': assistant_msg_id}, update_db=False + ) + if emitter: + await emitter({'type': 'chat:active', 'data': {'active': False}}) + raise ValueError(message['error']['content']) if not reply or not reply.get('done'): raise ValueError('Automation reply did not complete')