Fix a bug where tool_result messages must appear before text in user messages

This commit is contained in:
Leiber Lyu
2026-05-31 14:29:41 +10:00
parent 5c955480ee
commit 3fe4b128b5
5 changed files with 9 additions and 14 deletions

View File

@@ -444,15 +444,14 @@ def agent_loop(messages: list, context: dict):
"tool_use_id": block.id,
"content": output})
# Inject background notifications + tool results in one user message
user_content = []
# Inject tool results + background notifications in one user message
user_content = list(results)
bg_notifications = collect_background_results()
if bg_notifications:
for notif in bg_notifications:
user_content.append({"type": "text", "text": notif})
print(f" \033[32m[inject] {len(bg_notifications)} background "
f"notification(s)\033[0m")
user_content.extend(results)
messages.append({"role": "user", "content": user_content})
context = update_context(context, messages)
system = get_system_prompt(context)