fixes
This commit is contained in:
22
ai_client.py
22
ai_client.py
@@ -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",
|
||||
|
||||
Reference in New Issue
Block a user