fix(gateway): safely extract text from content arrays in prompt builder (#24946)
* fix(gateway): safely extract text from message content arrays in prompt builder
When HistoryEntry.body is a content array (e.g. [{type:"text",
text:"hello"}]) rather than a plain string, template literal
interpolation produces "[object Object]" instead of the actual message
text. This affects users whose session messages were stored with array
content format.
Add a safeBody helper that detects non-string body values and uses
extractTextFromChatContent to extract the text, preventing the
[object Object] serialization in both the current-message return path
and the history formatting path.
Fixes openclaw#24688
Co-authored-by: Cursor <cursoragent@cursor.com>
* fix: format gateway agent prompt helper (#24946)
---------
Co-authored-by: Cursor <cursoragent@cursor.com>
Co-authored-by: Peter Steinberger <steipete@gmail.com>
This commit is contained in:
@@ -1,10 +1,23 @@
|
||||
import { buildHistoryContextFromEntries, type HistoryEntry } from "../auto-reply/reply/history.js";
|
||||
import { extractTextFromChatContent } from "../shared/chat-content.js";
|
||||
|
||||
export type ConversationEntry = {
|
||||
role: "user" | "assistant" | "tool";
|
||||
entry: HistoryEntry;
|
||||
};
|
||||
|
||||
/**
|
||||
* Coerce body to string. Handles cases where body is a content array
|
||||
* (e.g. [{type:"text", text:"hello"}]) that would serialize as
|
||||
* [object Object] if used directly in a template literal.
|
||||
*/
|
||||
function safeBody(body: unknown): string {
|
||||
if (typeof body === "string") {
|
||||
return body;
|
||||
}
|
||||
return extractTextFromChatContent(body) ?? "";
|
||||
}
|
||||
|
||||
export function buildAgentMessageFromConversationEntries(entries: ConversationEntry[]): string {
|
||||
if (entries.length === 0) {
|
||||
return "";
|
||||
@@ -31,10 +44,10 @@ export function buildAgentMessageFromConversationEntries(entries: ConversationEn
|
||||
|
||||
const historyEntries = entries.slice(0, currentIndex).map((e) => e.entry);
|
||||
if (historyEntries.length === 0) {
|
||||
return currentEntry.body;
|
||||
return safeBody(currentEntry.body);
|
||||
}
|
||||
|
||||
const formatEntry = (entry: HistoryEntry) => `${entry.sender}: ${entry.body}`;
|
||||
const formatEntry = (entry: HistoryEntry) => `${entry.sender}: ${safeBody(entry.body)}`;
|
||||
return buildHistoryContextFromEntries({
|
||||
entries: [...historyEntries, currentEntry],
|
||||
currentMessage: formatEntry(currentEntry),
|
||||
|
||||
Reference in New Issue
Block a user