Cleanup function

This commit is contained in:
Dita Aji Pratama 2026-06-22 11:54:17 +07:00
parent fb52623d3e
commit 28bd27f79c
2 changed files with 17 additions and 23 deletions

View File

@ -4,32 +4,25 @@ from datetime import datetime
def _ts(): def _ts():
return datetime.now().strftime('%H:%M:%S') 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'] tname = tool_call['function']['name']
targs = json.loads(tool_call['function']['arguments']) targs = json.loads(tool_call['function']['arguments'])
handler = TOOL_HANDLERS.get(tname) handler = TOOL_HANDLERS.get(tname)
if not handler: if not handler:
result = f'Tool {tname} not found' return f'Tool {tname} not found'
else: try:
try: if tname == 'search_code':
if tname == 'search_code': return handler(
result = handler( pattern=targs['pattern'],
pattern=targs['pattern'], search_type=targs['search_type'],
search_type=targs['search_type'], path=targs.get('path', '.'),
path=targs.get('path', '.'), )
) elif tname == 'git_operation':
elif tname == 'git_operation': return handler(args=targs['args'])
result = handler(args=targs['args']) else:
else: return handler(**targs)
result = handler(**targs) except Exception as e:
except Exception as e: return f'Error executing tool: {str(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): def run_agent_loop(session, llm_client, TOOLS, TOOL_HANDLERS, max_iterations, on_tool_calls=None):
for step in range(max_iterations): for step in range(max_iterations):

View File

@ -151,7 +151,8 @@ def _agent_loop(app):
"arguments": targs, "arguments": targs,
})) }))
app.scroll = 999999 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) # Log content AI setelah tools (jika ada)