refactor: optimize non-verbose output - tool icons, compact args, clean content
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
import json, re
|
import json, re, os
|
||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
from typing import Any, Optional
|
from typing import Any, Optional
|
||||||
@dataclass
|
@dataclass
|
||||||
@@ -6,11 +6,9 @@ class StepOutcome:
|
|||||||
data: Any
|
data: Any
|
||||||
next_prompt: Optional[str] = None
|
next_prompt: Optional[str] = None
|
||||||
should_exit: bool = False
|
should_exit: bool = False
|
||||||
|
|
||||||
def try_call_generator(func, *args, **kwargs):
|
def try_call_generator(func, *args, **kwargs):
|
||||||
ret = func(*args, **kwargs)
|
ret = func(*args, **kwargs)
|
||||||
if hasattr(ret, '__iter__') and not isinstance(ret, (str, bytes, dict, list)):
|
if hasattr(ret, '__iter__') and not isinstance(ret, (str, bytes, dict, list)): ret = yield from ret
|
||||||
ret = yield from ret
|
|
||||||
return ret
|
return ret
|
||||||
|
|
||||||
class BaseHandler:
|
class BaseHandler:
|
||||||
@@ -42,10 +40,12 @@ def exhaust(g):
|
|||||||
|
|
||||||
def get_pretty_json(data):
|
def get_pretty_json(data):
|
||||||
if isinstance(data, dict) and "script" in data:
|
if isinstance(data, dict) and "script" in data:
|
||||||
data = data.copy()
|
data = data.copy(); data["script"] = data["script"].replace("; ", ";\n ")
|
||||||
data["script"] = data["script"].replace("; ", ";\n ")
|
|
||||||
return json.dumps(data, indent=2, ensure_ascii=False).replace('\\n', '\n')
|
return json.dumps(data, indent=2, ensure_ascii=False).replace('\\n', '\n')
|
||||||
|
|
||||||
|
_TOOL_ICONS = {'file_read': '📖', 'file_write': '✏️', 'file_patch': '✏️', 'code_run': '⚙️',
|
||||||
|
'web_scan': '🌐', 'web_execute_js': '🌐', 'update_working_checkpoint': '💾', 'ask_user': '❓', 'start_long_term_update': '💾'}
|
||||||
|
|
||||||
def agent_runner_loop(client, system_prompt, user_input, handler, tools_schema, max_turns=40, verbose=True, initial_user_content=None):
|
def agent_runner_loop(client, system_prompt, user_input, handler, tools_schema, max_turns=40, verbose=True, initial_user_content=None):
|
||||||
messages = [
|
messages = [
|
||||||
{"role": "system", "content": system_prompt},
|
{"role": "system", "content": system_prompt},
|
||||||
@@ -53,7 +53,8 @@ def agent_runner_loop(client, system_prompt, user_input, handler, tools_schema,
|
|||||||
]
|
]
|
||||||
turn = 0; handler._done_hooks = []; handler.max_turns = max_turns
|
turn = 0; handler._done_hooks = []; handler.max_turns = max_turns
|
||||||
while turn < handler.max_turns:
|
while turn < handler.max_turns:
|
||||||
turn += 1; yield f"**LLM Running (Turn {turn}) ...**\n\n"
|
turn += 1; md = '**' if verbose else ''
|
||||||
|
yield f"{md}LLM Running (Turn {turn}) ...{md}\n\n"
|
||||||
if turn%10 == 0: client.last_tools = '' # 每10轮重置一次工具描述,避免上下文过大导致的模型性能下降
|
if turn%10 == 0: client.last_tools = '' # 每10轮重置一次工具描述,避免上下文过大导致的模型性能下降
|
||||||
response_gen = client.chat(messages=messages, tools=tools_schema)
|
response_gen = client.chat(messages=messages, tools=tools_schema)
|
||||||
if verbose:
|
if verbose:
|
||||||
@@ -61,7 +62,8 @@ def agent_runner_loop(client, system_prompt, user_input, handler, tools_schema,
|
|||||||
yield '\n\n'
|
yield '\n\n'
|
||||||
else:
|
else:
|
||||||
response = exhaust(response_gen)
|
response = exhaust(response_gen)
|
||||||
yield response.content
|
cleaned = _clean_content(response.content)
|
||||||
|
if cleaned: yield cleaned + '\n'
|
||||||
|
|
||||||
if not response.tool_calls: tool_calls = [{'tool_name': 'no_tool', 'args': {}}]
|
if not response.tool_calls: tool_calls = [{'tool_name': 'no_tool', 'args': {}}]
|
||||||
else: tool_calls = [{'tool_name': tc.function.name, 'args': json.loads(tc.function.arguments), 'id': tc.id}
|
else: tool_calls = [{'tool_name': tc.function.name, 'args': json.loads(tc.function.arguments), 'id': tc.id}
|
||||||
@@ -70,19 +72,21 @@ def agent_runner_loop(client, system_prompt, user_input, handler, tools_schema,
|
|||||||
tool_results = []; next_prompts = set(); should_exit = None
|
tool_results = []; next_prompts = set(); should_exit = None
|
||||||
for ii, tc in enumerate(tool_calls):
|
for ii, tc in enumerate(tool_calls):
|
||||||
tool_name, args, tid = tc['tool_name'], tc['args'], tc.get('id', '')
|
tool_name, args, tid = tc['tool_name'], tc['args'], tc.get('id', '')
|
||||||
|
icon = _TOOL_ICONS.get(tool_name, '🛠️')
|
||||||
if tool_name == 'no_tool': pass
|
if tool_name == 'no_tool': pass
|
||||||
else:
|
else:
|
||||||
showarg = get_pretty_json(args)
|
if verbose: yield f"{icon} 正在调用工具: `{tool_name}` 📥参数:\n````text\n{get_pretty_json(args)}\n````\n"
|
||||||
if not verbose and len(showarg) > 200: showarg = showarg[:200] + ' ...'
|
else: yield f"{icon} {tool_name}({_compact_tool_args(tool_name, args)})\n\n\n"
|
||||||
yield f"🛠️ **正在调用工具:** `{tool_name}` 📥**参数:**\n````text\n{showarg}\n````\n"
|
|
||||||
handler.current_turn = turn
|
handler.current_turn = turn
|
||||||
gen = handler.dispatch(tool_name, args, response, index=ii)
|
gen = handler.dispatch(tool_name, args, response, index=ii)
|
||||||
if verbose:
|
try:
|
||||||
yield '`````\n'
|
v = next(gen)
|
||||||
outcome = yield from gen
|
def proxy(): yield v; return (yield from gen)
|
||||||
yield '`````\n'
|
if verbose: yield '`````\n'
|
||||||
else: outcome = exhaust(gen)
|
outcome = (yield from proxy()) if verbose else exhaust(proxy())
|
||||||
|
if verbose: yield '`````\n'
|
||||||
|
except StopIteration as e: outcome = e.value
|
||||||
|
|
||||||
if outcome.should_exit: return {'result': 'EXITED', 'data': outcome.data} # should_exit is only used for immediate exit
|
if outcome.should_exit: return {'result': 'EXITED', 'data': outcome.data} # should_exit is only used for immediate exit
|
||||||
if not outcome.next_prompt:
|
if not outcome.next_prompt:
|
||||||
should_exit = {'result': 'CURRENT_TASK_DONE', 'data': outcome.data}; break
|
should_exit = {'result': 'CURRENT_TASK_DONE', 'data': outcome.data}; break
|
||||||
@@ -97,3 +101,24 @@ def agent_runner_loop(client, system_prompt, user_input, handler, tools_schema,
|
|||||||
next_prompt = handler.next_prompt_patcher("\n".join(next_prompts), None, turn)
|
next_prompt = handler.next_prompt_patcher("\n".join(next_prompts), None, turn)
|
||||||
messages = [{"role": "user", "content": next_prompt, "tool_results": tool_results}] # just new message, history is kept in *Session
|
messages = [{"role": "user", "content": next_prompt, "tool_results": tool_results}] # just new message, history is kept in *Session
|
||||||
return {'result': 'MAX_TURNS_EXCEEDED'}
|
return {'result': 'MAX_TURNS_EXCEEDED'}
|
||||||
|
|
||||||
|
def _clean_content(text):
|
||||||
|
if not text: return ''
|
||||||
|
def _shrink_code(m):
|
||||||
|
lines = m.group(0).split('\n')
|
||||||
|
lang = lines[0].replace('```','').strip()
|
||||||
|
body = [l for l in lines[1:-1] if l.strip()] # 去掉```行和空行
|
||||||
|
if len(body) <= 6: return m.group(0) # 短代码保留
|
||||||
|
preview = '\n'.join(body[:5])
|
||||||
|
return f'```{lang}\n{preview}\n ... ({len(body)} lines)\n```'
|
||||||
|
text = re.sub(r'```[\s\S]*?```', _shrink_code, text)
|
||||||
|
for p in [r'<file_content>[\s\S]*?</file_content>', r'<tool_(?:use|call)>[\s\S]*?</tool_(?:use|call)>', r'(\r?\n){3,}']:
|
||||||
|
text = re.sub(p, '\n\n' if '\\n' in p else '', text)
|
||||||
|
return text.strip()
|
||||||
|
|
||||||
|
def _compact_tool_args(name, args):
|
||||||
|
a = {k: v for k, v in args.items() if k != '_index'}
|
||||||
|
for k in ('path',): # 只缩短路径
|
||||||
|
if k in a: a[k] = os.path.basename(a[k])
|
||||||
|
if name == 'update_working_checkpoint': s = a.get('key_info', ''); return (s[:60]+'...') if len(s)>60 else s
|
||||||
|
s = json.dumps(a, ensure_ascii=False); return (s[:120]+'...') if len(s)>120 else s
|
||||||
|
|||||||
Reference in New Issue
Block a user