This commit is contained in:
2026-02-21 20:04:35 -05:00
parent 687a8b076f
commit 4a9c23b2da
3 changed files with 14 additions and 38 deletions

View File

@@ -429,19 +429,16 @@ def _repair_anthropic_history(history: list[dict]):
if last.get("role") != "assistant":
return
content = last.get("content", [])
# Find tool_use blocks (content may be a list of dicts or SDK objects)
tool_use_ids = []
for block in content:
if isinstance(block, dict):
if block.get("type") == "tool_use":
tool_use_ids.append(block["id"])
else:
# SDK object
if getattr(block, "type", None) == "tool_use":
tool_use_ids.append(block.id)
if not tool_use_ids:
return
# Append a synthetic tool_result for each dangling tool_use
history.append({
"role": "user",
"content": [
@@ -454,6 +451,7 @@ def _repair_anthropic_history(history: list[dict]):
],
})
def _send_anthropic(md_content: str, user_message: str, base_dir: str) -> str:
try:
_ensure_anthropic_client()
@@ -545,25 +543,9 @@ def _send_anthropic(md_content: str, user_message: str, base_dir: str) -> str:
"tool_use_id": block.id,
"content": output,
})
elif block.type == "tool_use":
# Unknown tool - return an error result so history stays valid
tool_results.append({
"type": "tool_result",
"tool_use_id": block.id,
"content": f"ERROR: unknown tool '{block.name}'",
})
# Always append tool_results when stop_reason was tool_use,
# even if the list is somehow empty (shouldn't happen, but be safe)
if not tool_results:
# Synthesise a result for every tool_use block to keep history valid
for block in response.content:
if block.type == "tool_use":
tool_results.append({
"type": "tool_result",
"tool_use_id": block.id,
"content": "ERROR: tool could not be executed.",
})
break
_anthropic_history.append({
"role": "user",