Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 19 additions & 5 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -689,13 +689,27 @@ function sliceLastTurn(
const TOOL_MAX = 6000;
kept = kept.map((msg: any) => {
if (msg.role !== "tool" && msg.role !== "toolResult") return msg;
const text = typeof msg.content === "string"
? msg.content
: JSON.stringify(msg.content ?? "");
if (text.length <= TOOL_MAX) return msg;
// Extract text from both string and array content formats
let text: string;
if (typeof msg.content === "string") {
text = msg.content;
} else if (Array.isArray(msg.content)) {
text = msg.content
.filter((b: any) => b && typeof b === "object" && typeof b.text === "string")
.map((b: any) => b.text)
.join("\n");
} else {
text = JSON.stringify(msg.content ?? "");
}
if (text.length <= TOOL_MAX) {
// Ensure content is always in array format for OpenClaw core compatibility
if (Array.isArray(msg.content)) return msg;
return { ...msg, content: [{ type: "text", text }] };
}
const head = Math.floor(TOOL_MAX * 0.6);
const tail = Math.floor(TOOL_MAX * 0.3);
return { ...msg, content: text.slice(0, head) + `\n...[truncated ${text.length - head - tail} chars]...\n` + text.slice(-tail) };
const truncated = text.slice(0, head) + `\n...[truncated ${text.length - head - tail} chars]...\n` + text.slice(-tail);
return { ...msg, content: [{ type: "text", text: truncated }] };
});

let tokens = 0;
Expand Down