diff --git a/scripts/agent_loop.py b/scripts/agent_loop.py index 1d12f5a..cdbef1a 100644 --- a/scripts/agent_loop.py +++ b/scripts/agent_loop.py @@ -4,32 +4,25 @@ from datetime import datetime def _ts(): return datetime.now().strftime('%H:%M:%S') -def execute_tool(tool_call, TOOL_HANDLERS, app=None): +def execute_tool(tool_call, TOOL_HANDLERS): tname = tool_call['function']['name'] targs = json.loads(tool_call['function']['arguments']) handler = TOOL_HANDLERS.get(tname) if not handler: - result = f'Tool {tname} not found' - else: - try: - if tname == 'search_code': - result = handler( - pattern=targs['pattern'], - search_type=targs['search_type'], - path=targs.get('path', '.'), - ) - elif tname == 'git_operation': - result = handler(args=targs['args']) - else: - result = handler(**targs) - except Exception as e: - result = f'Error executing tool: {str(e)}' - - if app: - from tui.agent import _add_msg - _add_msg(app, "tool", str(result), tool_call_id=tool_call["id"]) - - return result + return f'Tool {tname} not found' + try: + if tname == 'search_code': + return handler( + pattern=targs['pattern'], + search_type=targs['search_type'], + path=targs.get('path', '.'), + ) + elif tname == 'git_operation': + return handler(args=targs['args']) + else: + return handler(**targs) + except Exception as e: + return f'Error executing tool: {str(e)}' def run_agent_loop(session, llm_client, TOOLS, TOOL_HANDLERS, max_iterations, on_tool_calls=None): for step in range(max_iterations): diff --git a/tui/agent.py b/tui/agent.py index 4a5e31a..b0fb0e7 100644 --- a/tui/agent.py +++ b/tui/agent.py @@ -151,7 +151,8 @@ def _agent_loop(app): "arguments": targs, })) app.scroll = 999999 - agent_loop.execute_tool(tc, app.TOOL_HANDLERS, app=app) + result = agent_loop.execute_tool(tc, app.TOOL_HANDLERS) + _add_msg(app, "tool", str(result), tool_call_id=tc["id"]) # Log content AI setelah tools (jika ada)