mirror of
https://github.com/shareAI-lab/analysis_claude_code.git
synced 2026-09-20 12:13:38 +08:00
Fix empty tool-use response handling
This commit is contained in:
24
README-zh.md
24
README-zh.md
@@ -143,7 +143,7 @@ Claude Code = 一个 agent loop
|
||||
|
||||
User --> messages[] --> LLM --> response
|
||||
|
|
||||
stop_reason == "tool_use"?
|
||||
包含 tool_use block?
|
||||
/ \
|
||||
yes no
|
||||
| |
|
||||
@@ -210,18 +210,20 @@ def agent_loop(messages):
|
||||
messages.append({"role": "assistant",
|
||||
"content": response.content})
|
||||
|
||||
if response.stop_reason != "tool_use":
|
||||
tool_calls = [
|
||||
block for block in response.content if block.type == "tool_use"
|
||||
]
|
||||
if not tool_calls:
|
||||
return
|
||||
|
||||
results = []
|
||||
for block in response.content:
|
||||
if block.type == "tool_use":
|
||||
output = TOOL_HANDLERS[block.name](**block.input)
|
||||
results.append({
|
||||
"type": "tool_result",
|
||||
"tool_use_id": block.id,
|
||||
"content": output,
|
||||
})
|
||||
for block in tool_calls:
|
||||
output = TOOL_HANDLERS[block.name](**block.input)
|
||||
results.append({
|
||||
"type": "tool_result",
|
||||
"tool_use_id": block.id,
|
||||
"content": output,
|
||||
})
|
||||
messages.append({"role": "user", "content": results})
|
||||
```
|
||||
|
||||
@@ -350,7 +352,7 @@ flowchart TD
|
||||
|
||||
| 章节 | 主题 | 关键概念 |
|
||||
|---|---|---|
|
||||
| [s01](./s01_agent_loop/) | Agent Loop | `messages` / `while True` / `stop_reason` |
|
||||
| [s01](./s01_agent_loop/) | Agent Loop | `messages` / `while True` / `tool_use` |
|
||||
| [s02](./s02_tool_use/) | Tool Use | `TOOL_HANDLERS` / dispatch map / 并发 |
|
||||
| [s03](./s03_permission/) | Permission | `PermissionRule` / 审批管线 |
|
||||
| [s04](./s04_hooks/) | Hooks | `PreToolUse` / `PostToolUse` / 扩展点 |
|
||||
|
||||
Reference in New Issue
Block a user