Better modular execute_tool for TUI agent
This commit is contained in:
parent
7aa1f56124
commit
fb52623d3e
@ -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):
|
||||
|
||||
29
tui/agent.py
29
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"])
|
||||
|
||||
Loading…
Reference in New Issue
Block a user