Fix empty tool-use response handling

This commit is contained in:
Haoran
2026-08-15 00:03:45 +08:00
parent 985456f4ad
commit 168fff86dd
90 changed files with 885 additions and 503 deletions

View File

@@ -108,13 +108,12 @@ def check_permission(block) -> bool:
return True
# agent_loop で — s02 のループに 1 行追加するだけ:
for block in response.content:
if block.type == "tool_use":
if not check_permission(block): # ← 新規
results.append({... "content": "Permission denied."})
continue
output = TOOL_HANDLERS[block.name](**block.input) # s02 既存
results.append(...)
for block in tool_calls:
if not check_permission(block): # ← 新規
results.append({... "content": "Permission denied."})
continue
output = TOOL_HANDLERS[block.name](**block.input) # s02 既存
results.append(...)
```
---

View File

@@ -108,13 +108,12 @@ def check_permission(block) -> bool:
return True
# In agent_loop — s02's loop with just one line added:
for block in response.content:
if block.type == "tool_use":
if not check_permission(block): # ← NEW
results.append({... "content": "Permission denied."})
continue
output = TOOL_HANDLERS[block.name](**block.input) # s02 original
results.append(...)
for block in tool_calls:
if not check_permission(block): # ← NEW
results.append({... "content": "Permission denied."})
continue
output = TOOL_HANDLERS[block.name](**block.input) # s02 original
results.append(...)
```
---

View File

@@ -108,13 +108,12 @@ def check_permission(block) -> bool:
return True
# 在 agent_loop 中——s02 的循环只加了一行:
for block in response.content:
if block.type == "tool_use":
if not check_permission(block): # ← 新增
results.append({... "content": "Permission denied."})
continue
output = TOOL_HANDLERS[block.name](**block.input) # s02 原有
results.append(...)
for block in tool_calls:
if not check_permission(block): # ← 新增
results.append({... "content": "Permission denied."})
continue
output = TOOL_HANDLERS[block.name](**block.input) # s02 原有
results.append(...)
```
---

View File

@@ -197,14 +197,14 @@ def agent_loop(messages: list):
)
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":
continue
for block in tool_calls:
print(f"\033[36m> {block.name}\033[0m")
# s03 change: run through permission pipeline before executing

View File

@@ -36,7 +36,7 @@
<!-- LLM -->
<rect x="230" y="84" width="130" height="48" rx="8" fill="#fff" stroke="#2563eb" stroke-width="1.5"/>
<text x="295" y="104" fill="#1e3a5f" font-size="13" font-weight="700" text-anchor="middle">LLM</text>
<text x="295" y="122" fill="#64748b" font-size="10" text-anchor="middle">stop_reason?</text>
<text x="295" y="122" fill="#64748b" font-size="10" text-anchor="middle">tool_use block?</text>
<!-- No → return -->
<line x1="295" y1="132" x2="295" y2="156" stroke="#16a34a" stroke-width="1.5" marker-end="url(#arrow)"/>

Before

Width:  |  Height:  |  Size: 6.1 KiB

After

Width:  |  Height:  |  Size: 6.1 KiB

View File

@@ -36,7 +36,7 @@
<!-- LLM -->
<rect x="230" y="84" width="130" height="48" rx="8" fill="#fff" stroke="#2563eb" stroke-width="1.5"/>
<text x="295" y="104" fill="#1e3a5f" font-size="13" font-weight="700" text-anchor="middle">LLM</text>
<text x="295" y="122" fill="#64748b" font-size="10" text-anchor="middle">stop_reason?</text>
<text x="295" y="122" fill="#64748b" font-size="10" text-anchor="middle">tool_use block?</text>
<!-- No → 戻る -->
<line x1="295" y1="132" x2="295" y2="156" stroke="#16a34a" stroke-width="1.5" marker-end="url(#arrow)"/>

Before

Width:  |  Height:  |  Size: 6.2 KiB

After

Width:  |  Height:  |  Size: 6.2 KiB

View File

@@ -36,7 +36,7 @@
<!-- LLM -->
<rect x="230" y="84" width="130" height="48" rx="8" fill="#fff" stroke="#2563eb" stroke-width="1.5"/>
<text x="295" y="104" fill="#1e3a5f" font-size="13" font-weight="700" text-anchor="middle">LLM</text>
<text x="295" y="122" fill="#64748b" font-size="10" text-anchor="middle">stop_reason?</text>
<text x="295" y="122" fill="#64748b" font-size="10" text-anchor="middle">tool_use block?</text>
<!-- 否 → 返回 -->
<line x1="295" y1="132" x2="295" y2="156" stroke="#16a34a" stroke-width="1.5" marker-end="url(#arrow)"/>

Before

Width:  |  Height:  |  Size: 6.1 KiB

After

Width:  |  Height:  |  Size: 6.1 KiB