update agent_loop and ga logic

This commit is contained in:
Liang Jiaqing
2026-01-18 12:28:56 +08:00
parent 00e9d8f5e4
commit 3c1525e427
2 changed files with 67 additions and 50 deletions

View File

@@ -32,17 +32,23 @@ def json_default(o):
if isinstance(o, set): return list(o)
return str(o)
def get_pretty_json(data):
if isinstance(data, dict) and "script" in data:
data = data.copy()
data["script"] = data["script"].replace("; ", ";\n ")
return json.dumps(data, indent=2, ensure_ascii=False).replace('\\n', '\n')
def agent_runner_loop(client, system_prompt, user_input, handler, tools_schema, max_turns=15):
messages = [
{"role": "system", "content": system_prompt},
{"role": "user", "content": user_input}
]
for turn in range(max_turns):
yield f"\n[🤖 LLM Thinking (Turn {turn+1})] ..."
yield f"**LLM Running (Turn {turn+1}) ...**\n\n"
response = client.chat(messages=messages, tools=tools_schema)
if response.thinking: yield '<thinking>' + response.thinking + '</thinking>\n'
yield response.content
if response.thinking: yield '<thinking>' + response.thinking + '</thinking>\n\n'
yield response.content + '\n\n'
if not response.tool_calls:
tool_name, args = 'no_tool', {}
@@ -52,8 +58,12 @@ def agent_runner_loop(client, system_prompt, user_input, handler, tools_schema,
args = json.loads(tool_call.function.arguments)
if tool_name == 'no_tool': pass
else: yield f"\n\n正在调用工具: {tool_name},参数: {args}\n"
else:
yield f"🛠️ **正在调用工具:** `{tool_name}` 📥**参数:**\n"
yield f"````text\n{get_pretty_json(args)}\n````\n"
yield '`````\n'
outcome = yield from handler.dispatch(tool_name, args, response)
yield '`````\n'
if outcome.next_prompt is None: return {'result': 'CURRENT_TASK_DONE', 'data': outcome.data}
if outcome.should_exit: return {'result': 'EXITED', 'data': outcome.data}