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

@@ -726,13 +726,12 @@ def agent_loop(messages: list, context: dict) -> dict:
"tool_use_id": block.id,
"content": output})
# Merge background notifications + tool results into one user message
user_content = []
# Merge background tool results + notifications into 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})
user_content.extend(results)
messages.append({"role": "user", "content": user_content})
context = update_context(context, messages)
system = get_system_prompt(context)