From fb52623d3e99e4cb3e7de1608c56824f88d87fe0 Mon Sep 17 00:00:00 2001 From: Dita Aji Pratama Date: Mon, 22 Jun 2026 11:47:29 +0700 Subject: [PATCH] Better modular execute_tool for TUI agent --- scripts/agent_loop.py | 37 ++++++++++++++++++++++--------------- tui/agent.py | 29 ++++++----------------------- 2 files changed, 28 insertions(+), 38 deletions(-) diff --git a/scripts/agent_loop.py b/scripts/agent_loop.py index cdbef1a..1d12f5a 100644 --- a/scripts/agent_loop.py +++ b/scripts/agent_loop.py @@ -4,25 +4,32 @@ from datetime import datetime def _ts(): return datetime.now().strftime('%H:%M:%S') -def execute_tool(tool_call, TOOL_HANDLERS): +def execute_tool(tool_call, TOOL_HANDLERS, app=None): tname = tool_call['function']['name'] targs = json.loads(tool_call['function']['arguments']) handler = TOOL_HANDLERS.get(tname) if not handler: - 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)}' + 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 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 0fd18ac..4a5e31a 100644 --- a/tui/agent.py +++ b/tui/agent.py @@ -2,7 +2,8 @@ import json import threading from datetime import datetime import config -from scripts import ntro +from scripts import ntro, agent_loop + def _add_msg(app, role, content, **kwargs): msg = {"role": role, "content": content} @@ -150,7 +151,8 @@ def _agent_loop(app): "arguments": targs, })) app.scroll = 999999 - execute_tool(app, tc) + agent_loop.execute_tool(tc, app.TOOL_HANDLERS, app=app) + # Log content AI setelah tools (jika ada) if response.content and response.content.strip(): @@ -171,25 +173,6 @@ def _agent_loop(app): app.agent_done.set() -def execute_tool(app, tool_call): - tname = tool_call["function"]["name"] - targs = json.loads(tool_call["function"]["arguments"]) - handler = app.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)}" + app.agent_done.set() + ntro.end(stamp) - _add_msg(app, "tool", str(result), tool_call_id=tool_call["id"])