Skip to content

AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.

hotfix(backend/copilot): capture tool results in transcript (#12323)

Zamil Majdy7ead4c040fea
diff --git a/autogpt_platform/backend/backend/copilot/sdk/service.py b/autogpt_platform/backend/backend/copilot/sdk/service.py
index e3c0f0bb94..85d86187c8 100644
--- a/autogpt_platform/backend/backend/copilot/sdk/service.py
+++ b/autogpt_platform/backend/backend/copilot/sdk/service.py
@@ -1133,19 +1133,18 @@ async def stream_chat_completion_sdk(
json.dumps(user_msg) + "\n"
)
# Capture user message in transcript (multimodal)
- transcript_builder.add_user_message(content=content_blocks)
+ transcript_builder.append_user(content=content_blocks)
else:
await client.query(query_message, session_id=session_id)
# Capture actual user message in transcript (not the engineered query)
# query_message may include context wrappers, but transcript needs raw input
- transcript_builder.add_user_message(content=current_message)
+ transcript_builder.append_user(content=current_message)
assistant_response = ChatMessage(role="assistant", content="")
accumulated_tool_calls: list[dict[str, Any]] = []
has_appended_assistant = False
has_tool_results = False
ended_with_stream_error = False
-
# Use an explicit async iterator with non-cancelling heartbeats.
# CRITICAL: we must NOT cancel __anext__() mid-flight — doing so
# (via asyncio.timeout or wait_for) corrupts the SDK's internal
@@ -1216,15 +1215,6 @@ async def stream_chat_completion_sdk(
len(adapter.resolved_tool_calls),
)
- # Capture SDK messages in transcript
- if isinstance(sdk_msg, AssistantMessage):
- content_blocks = _format_sdk_content_blocks(sdk_msg.content)
- model_name = getattr(sdk_msg, "model", "")
- transcript_builder.add_assistant_message(
- content_blocks=content_blocks,
- model=model_name,
- )
-
# Race-condition fix: SDK hooks (PostToolUse) are
# executed asynchronously via start_soon() — the next
# message can arrive before the hook stashes output.
@@ -1355,22 +1345,37 @@ async def stream_chat_completion_sdk(
has_appended_assistant = True
elif isinstance(response, StreamToolOutputAvailable):
+ content = (
+ response.output
+ if isinstance(response.output, str)
+ else json.dumps(response.output, ensure_ascii=False)
+ )
session.messages.append(
ChatMessage(
role="tool",
- content=(
- response.output
- if isinstance(response.output, str)
- else str(response.output)
- ),
+ content=content,
tool_call_id=response.toolCallId,
)
)
+ transcript_builder.append_tool_result(
+ tool_use_id=response.toolCallId,
+ content=content,
+ )
has_tool_results = True
elif isinstance(response, StreamFinish):
stream_completed = True
+ # Append assistant entry AFTER convert_message so that
+ # any stashed tool results from the previous turn are
+ # recorded first, preserving the required API order:
+ # assistant(tool_use) → tool_result → assistant(text).
+ if isinstance(sdk_msg, AssistantMessage):
+ transcript_builder.append_assistant(
+ content_blocks=_format_sdk_content_blocks(sdk_msg.content),
+ model=sdk_msg.model,
+ )
+
except asyncio.CancelledError:
# Task/generator was cancelled (e.g. client disconnect,
# server shutdown). Log and let the safety-net / finally
@@ -1419,6 +1424,15 @@ async def stream_chat_completion_sdk(
type(response).__name__,
getattr(response, "toolName", "N/A"),
)
+ if isinstance(response, StreamToolOutputAvailable):
+ transcript_builder.append_tool_result(
+ tool_use_id=response.toolCallId,
+ content=(
+ response.output
+ if isinstance(response.output, str)
+ else json.dumps(response.output, ensure_ascii=False)
+ ),
+ )
yield response
# If the stream ended without a ResultMessage, the SDK
diff --git a/autogpt_platform/backend/backend/copilot/sdk/transcript_builder.py b/autogpt_platform/backend/backend/copilot/sdk/transcript_builder.py
index 17f7ffc8c6..8cd24e7273 100644
--- a/autogpt_platform/backend/backend/copilot/sdk/transcript_builder.py
+++ b/autogpt_platform/backend/backend/copilot/sdk/transcript_builder.py
@@ -44,6 +44,15 @@ class TranscriptBuilder:
self._entries: list[TranscriptEntry] = []
self._last_uuid: str | None = None
+ def _last_is_assistant(self) -> bool:
+ return bool(self._entries) and self._entries[-1].type == "assistant"
+
+ def _last_message_id(self) -> str:
+ """Return the message.id of the last entry, or '' if none."""
+ if self._entries:
+ return self._entries[-1].message.get("id", "")
+ return ""
+
def load_previous(self, content: str, log_prefix: str = "[Transcript]") -> None:
"""Load complete previous transcript.
@@ -91,10 +100,8 @@ class TranscriptBuilder:
self._last_uuid[:12] if self._last_uuid else None,
)
- def add_user_message(
- self, content: str | list[dict], uuid: str | None = None
- ) -> None:
- """Add user message to the complete context."""
+ def append_user(self, content: str | list[dict], uuid: str | None = None) -> None:
+ """Append a user entry."""
msg_uuid = uuid or str(uuid4())
self._entries.append(
@@ -107,10 +114,34 @@ class TranscriptBuilder:
)
self._last_uuid = msg_uuid
- def add_assistant_message(
- self, content_blocks: list[dict], model: str = ""
+ def append_tool_result(self, tool_use_id: str, content: str) -> None:
+ """Append a tool result as a user entry (one per tool call)."""
+ self.append_user(
+ content=[
+ {"type": "tool_result", "tool_use_id": tool_use_id, "content": content}
+ ]
+ )
+
+ def append_assistant(
+ self,
+ content_blocks: list[dict],
+ model: str = "",
+ stop_reason: str | None = None,
) -> None:
- """Add assistant message to the complete context."""
+ """Append an assistant entry.
+
+ Consecutive assistant entries automatically share the same message ID
+ so the CLI can merge them (thinking → text → tool_use) into a single
+ API message on ``--resume``. A new ID is assigned whenever an
+ assistant entry follows a non-assistant entry (user message or tool
+ result), because that marks the start of a new API response.
+ """
+ message_id = (
+ self._last_message_id()
+ if self._last_is_assistant()
+ else f"msg_sdk_{uuid4().hex[:24]}"
+ )
+
msg_uuid = str(uuid4())
self._entries.append(
@@ -121,7 +152,11 @@ class TranscriptBuilder:
message={
"role": "assistant",
"model": model,
+ "id": message_id,
+ "type": "message",
"content": content_blocks,
+ "stop_reason": stop_reason,
+ "stop_sequence": None,
},
)
)
@@ -130,6 +165,9 @@ class TranscriptBuilder:
def to_jsonl(self) -> str:
"""Export complete context as JSONL.
+ Consecutive assistant entries are kept separate to match the
+ native CLI format — the SDK merges them internally on resume.
+
Returns the FULL conversation state (all entries), not incremental.
This output REPLACES any previous transcript.
"""