feat(agent): 任务执行加结构化日志(LLM推理/tool调用query与返回/产出)
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> (cherry picked from commit 099f270cc2a092a4eae7e5d06d99c4d7e076465e)
This commit is contained in:
+17
-2
@@ -342,6 +342,8 @@ Return ONLY the JSON array, no other text."""
|
|||||||
content = ""
|
content = ""
|
||||||
try:
|
try:
|
||||||
description = subtask["description"]
|
description = subtask["description"]
|
||||||
|
logger.info("════════ [任务开始] task=%s role=%s\n 描述: %s",
|
||||||
|
task_id, context.get("specialist_role", "general"), description)
|
||||||
workspace_files = self._summarize_workspace()
|
workspace_files = self._summarize_workspace()
|
||||||
workspace_context = self._collect_workspace_context()
|
workspace_context = self._collect_workspace_context()
|
||||||
user_prompt = context.get("user_prompt") or context.get("run_goal") or context.get("root_task_description") or ""
|
user_prompt = context.get("user_prompt") or context.get("run_goal") or context.get("root_task_description") or ""
|
||||||
@@ -437,6 +439,10 @@ Return your response as JSON with this structure:
|
|||||||
Return ONLY the JSON, no other text."""
|
Return ONLY the JSON, no other text."""
|
||||||
content = await self._complete(prompt, max_tokens=4000)
|
content = await self._complete(prompt, max_tokens=4000)
|
||||||
result = self._parse_json_response(content)
|
result = self._parse_json_response(content)
|
||||||
|
logger.info("──────── [任务产出] task=%s status=%s 文件=%s\n 说明: %s",
|
||||||
|
task_id, result.get("status"),
|
||||||
|
[f.get("path") for f in (result.get("files") or [])],
|
||||||
|
(result.get("changes") or result.get("summary") or "")[:600])
|
||||||
if result.get("status") == "completed":
|
if result.get("status") == "completed":
|
||||||
apply_result = await self._apply_file_changes(result.get("files", []))
|
apply_result = await self._apply_file_changes(result.get("files", []))
|
||||||
result["files_modified"] = apply_result["files_modified"]
|
result["files_modified"] = apply_result["files_modified"]
|
||||||
@@ -539,7 +545,7 @@ Return ONLY the JSON, no other text."""
|
|||||||
tools = await self._load_jina_tools()
|
tools = await self._load_jina_tools()
|
||||||
messages = [{"role": "user", "content": prompt}]
|
messages = [{"role": "user", "content": prompt}]
|
||||||
|
|
||||||
for _ in range(8): # max 8 tool-call rounds
|
for round_i in range(8): # max 8 tool-call rounds
|
||||||
kwargs: dict = dict(
|
kwargs: dict = dict(
|
||||||
model=self.model,
|
model=self.model,
|
||||||
messages=messages,
|
messages=messages,
|
||||||
@@ -553,6 +559,10 @@ Return ONLY the JSON, no other text."""
|
|||||||
self._record_openai_usage(response)
|
self._record_openai_usage(response)
|
||||||
msg = response.choices[0].message
|
msg = response.choices[0].message
|
||||||
|
|
||||||
|
# 记录这一轮 LLM 的"思考/回复"(可观测 agent 怎么想的)
|
||||||
|
if msg.content:
|
||||||
|
logger.info("[LLM·第%d轮] 思考/回复:\n%s", round_i + 1, msg.content.strip()[:1200])
|
||||||
|
|
||||||
if not msg.tool_calls:
|
if not msg.tool_calls:
|
||||||
return msg.content or ""
|
return msg.content or ""
|
||||||
|
|
||||||
@@ -560,8 +570,13 @@ Return ONLY the JSON, no other text."""
|
|||||||
messages.append(msg.model_dump(exclude_unset=True))
|
messages.append(msg.model_dump(exclude_unset=True))
|
||||||
for tc in msg.tool_calls:
|
for tc in msg.tool_calls:
|
||||||
args = json.loads(tc.function.arguments or "{}")
|
args = json.loads(tc.function.arguments or "{}")
|
||||||
|
# 记录调用了哪个 tool、query 是什么
|
||||||
|
logger.info("[TOOL·调用] %s 参数=%s", tc.function.name,
|
||||||
|
json.dumps(args, ensure_ascii=False)[:400])
|
||||||
result = await self._call_jina_tool(tc.function.name, args)
|
result = await self._call_jina_tool(tc.function.name, args)
|
||||||
logger.info("Jina tool %s → %d chars", tc.function.name, len(result))
|
# 记录 tool 返回了什么(搜索结果内容,截断)
|
||||||
|
logger.info("[TOOL·返回] %s (%d字):\n%s", tc.function.name, len(result),
|
||||||
|
(result or "").strip()[:1000])
|
||||||
messages.append({
|
messages.append({
|
||||||
"role": "tool",
|
"role": "tool",
|
||||||
"tool_call_id": tc.id,
|
"tool_call_id": tc.id,
|
||||||
|
|||||||
Reference in New Issue
Block a user