Fixing bug di tools dan di agent loop

This commit is contained in:
Dita Aji Pratama 2026-10-06 09:34:47 +07:00
parent 12eb51b1eb
commit e52df1bc8b
2 changed files with 29 additions and 8 deletions

View File

@ -1,5 +1,6 @@
import json import json
from datetime import datetime from datetime import datetime
import config
from lib.personality import PERSONALITY from lib.personality import PERSONALITY
from tools.vision import ImagePayload from tools.vision import ImagePayload
from scripts import loggorilla from scripts import loggorilla
@ -7,25 +8,36 @@ from scripts import loggorilla
def _ts(): def _ts():
return datetime.now().strftime('%H:%M:%S') return datetime.now().strftime('%H:%M:%S')
def _cap_output(result):
# Jaring pengaman ukuran output tool. Hormati semantics config:
# AGENT_MAX_TOOL_OUTPUT <= 0 = unlimited (tidak dipotong).
limit = getattr(config, "AGENT_MAX_TOOL_OUTPUT", 0)
if not limit or limit <= 0:
return result
if isinstance(result, str) and len(result) > limit:
omitted = len(result) - limit
return result[:limit] + f"\n... [truncated: {omitted} chars omitted]"
return result
def execute_tool(tool_call, TOOL_HANDLERS): 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:
return f'Tool {tname} not found' return _cap_output(f'Tool {tname} not found')
try: try:
if tname == 'search_code': if tname == 'search_code':
return handler( return _cap_output(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']) return _cap_output(handler(args=targs['args']))
else: else:
return handler(**targs) return _cap_output(handler(**targs))
except Exception as e: except Exception as e:
return f'Error executing tool: {str(e)}' return _cap_output(f'Error executing tool: {str(e)}')
def run_agent_loop(session, llm_client, TOOLS, TOOL_HANDLERS, max_iterations, on_tool_calls=None, tool_reminder=None): def run_agent_loop(session, llm_client, TOOLS, TOOL_HANDLERS, max_iterations, on_tool_calls=None, tool_reminder=None):
session_ended = False session_ended = False

View File

@ -6,6 +6,15 @@ import glob as glob_module
from lib import workspace from lib import workspace
# Batas panjang satu baris hasil pencarian. Mencegah satu baris raksasa
# (mis. file minified/generated satu-line) menembus context window model.
MAX_MATCH_LINE = 500
def _clip(line):
if len(line) > MAX_MATCH_LINE:
return line[:MAX_MATCH_LINE] + f"... [+{len(line) - MAX_MATCH_LINE} chars]"
return line
schema_read_file = { schema_read_file = {
"type": "function", "type": "function",
"function": { "function": {
@ -145,7 +154,7 @@ def search_code(pattern, search_type, path="."):
with open(file_path, 'r', encoding='utf-8', errors='replace') as f: with open(file_path, 'r', encoding='utf-8', errors='replace') as f:
for i, line in enumerate(f.readlines(), 1): for i, line in enumerate(f.readlines(), 1):
if re.search(pattern, line): if re.search(pattern, line):
results.append(f"{file_path}:{i}: {line.strip()}") results.append(f"{file_path}:{i}: {_clip(line.strip())}")
except: except:
continue continue
return "\n".join(results[:50]) if results else "No matches found" return "\n".join(results[:50]) if results else "No matches found"
@ -211,7 +220,7 @@ def search_grep(pattern, path=".", include=None):
with open(file_path, 'r', encoding='utf-8', errors='replace') as f: with open(file_path, 'r', encoding='utf-8', errors='replace') as f:
for i, line in enumerate(f.readlines(), 1): for i, line in enumerate(f.readlines(), 1):
if re.search(pattern, line): if re.search(pattern, line):
results.append(f"{file_path}:{i}: {line.strip()}") results.append(f"{file_path}:{i}: {_clip(line.strip())}")
except: except:
continue continue
return "\n".join(results[:100]) if results else "No matches found" return "\n".join(results[:100]) if results else "No matches found"