Skip to content

fix: ensure tool result content is always array format#9

Open
yjjheizhu wants to merge 1 commit intoadoresever:mainfrom
yjjheizhu:fix/tool-content-format
Open

fix: ensure tool result content is always array format#9
yjjheizhu wants to merge 1 commit intoadoresever:mainfrom
yjjheizhu:fix/tool-content-format

Conversation

@yjjheizhu
Copy link

Problem

OpenClaw core expects toolMsg.content to be an array of {type, text} objects. When the context compactor encounters a short tool result (≤6000 chars), it returns the original message unchanged. If msg.content is a raw string, the core code crashes:

toolMsg.content.filter is not a function

This causes the entire agent session to crash silently, losing all context and memory. The agent appears to "amnesia" after 2-3 rounds of conversation.

Root Cause

In contextCompactor, the short message path:

const text = typeof msg.content === "string" ? msg.content : JSON.stringify(...);
if (text.length <= TOOL_MAX) return msg;  // ← returns string content unchanged

The truncated path also returns a plain string:

return { ...msg, content: "truncated string" };  // ← wrong format

Fix

  1. Extract text from both string and array content formats — properly handles {type: "text", text} array elements
  2. Short messages: ensure content is always [{type: "text", text}] array format
  3. Truncated messages: return array format instead of raw string
  4. Avoid JSON.stringify on arrays — prevents double-encoding of structured content

Testing

Tested locally with graph-memory plugin loaded. Agent no longer crashes when context compactor processes tool results. Memory and conversation continuity preserved across multiple rounds.

OpenClaw core expects toolMsg.content to be an array of {type, text} objects.
When content is a raw string, the core's toolMsg.content.filter() call crashes
with 'filter is not a function', causing the agent session to crash and lose context.

Changes:
- Extract text from both string and array content formats
- Return [{type: 'text', text}] array for short messages
- Return [{type: 'text', text}] array for truncated messages
- Avoid JSON.stringify on array content (causes double-encoding)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant