Self-evolving Context Database for AI Agents. Unify Agent Memory, Knowledge RAG and Skills.
Python agent-contextagent-memoryagent-pluginsagentic-ragcontext-databasedsh-pluginknowledge-basememorymemory-managementself-evolvingvector-database
fix(openclaw-plugin): stop capturing tool placeholders (#2534)
Mingjian Queba8c8ec0587d
diff --git a/examples/openclaw-plugin/tests/ut/text-utils.test.ts b/examples/openclaw-plugin/tests/ut/text-utils.test.tsindex f375425a8..983da5934 100644--- a/examples/openclaw-plugin/tests/ut/text-utils.test.ts+++ b/examples/openclaw-plugin/tests/ut/text-utils.test.ts@@ -57,6 +57,16 @@ describe("sanitizeUserTextForCapture", () => {expect(sanitizeUserTextForCapture(" ")).toBe("");});+ it("drops legacy synthetic tool placeholders", () => {+ expect(sanitizeUserTextForCapture("[tool: exec]")).toBe("");+ expect(sanitizeUserTextForCapture(" [toolUse: grep] ")).toBe("");+ });++ it("preserves ordinary text that mentions a tool placeholder", () => {+ const input = "The model echoed [tool: exec] in a previous answer.";+ expect(sanitizeUserTextForCapture(input)).toBe(input);+ });+it("strips leading timestamp prefix", () => {const input = "[2026-03-29T10:00:00Z] actual content here";const result = sanitizeUserTextForCapture(input);@@ -245,7 +255,7 @@ describe("extractNewTurnTexts", () => {});describe("extractNewTurnMessages", () => {- it("keeps assistant toolCall messages that have no text block", () => {+ it("drops assistant toolCall messages that have no text block", () => {const messages = [{role: "assistant",@@ -259,15 +269,10 @@ describe("extractNewTurnMessages", () => {const { messages: extracted, newCount } = extractNewTurnMessages(messages, 0);expect(newCount).toBe(1);- expect(extracted).toEqual([- {- role: "assistant",- parts: [{ type: "text", text: "[tool: read_file]" }],- },- ]);+ expect(extracted).toEqual([]);});- it("keeps assistant toolUse messages that have no text block", () => {+ it("drops assistant toolUse messages that have no text block", () => {const messages = [{role: "assistant",@@ -281,15 +286,10 @@ describe("extractNewTurnMessages", () => {const { messages: extracted, newCount } = extractNewTurnMessages(messages, 0);expect(newCount).toBe(1);- expect(extracted).toEqual([- {- role: "assistant",- parts: [{ type: "text", text: "[tool: grep]" }],- },- ]);+ expect(extracted).toEqual([]);});- it("keeps multiple textless assistant tool calls in one placeholder", () => {+ it("does not synthesize placeholders for multiple textless assistant tool calls", () => {const messages = [{role: "assistant",@@ -302,12 +302,7 @@ describe("extractNewTurnMessages", () => {const { messages: extracted } = extractNewTurnMessages(messages, 0);- expect(extracted).toEqual([- {- role: "assistant",- parts: [{ type: "text", text: "[tool: read_file, grep]" }],- },- ]);+ expect(extracted).toEqual([]);});it("does not create a placeholder for textless assistant messages without tool calls", () => {diff --git a/examples/openclaw-plugin/text-utils.ts b/examples/openclaw-plugin/text-utils.tsindex a0494df42..972879509 100644--- a/examples/openclaw-plugin/text-utils.ts+++ b/examples/openclaw-plugin/text-utils.ts@@ -45,12 +45,17 @@ function looksLikeMetadataJsonBlock(content: string): boolean {}const HEARTBEAT_RE = /\bHEARTBEAT(?:\.md|_OK)\b/;+const TOOL_PLACEHOLDER_RE = /^\s*\[tool(?::\s*|Use:\s*)[^\]]+\]\s*$/i;export function sanitizeUserTextForCapture(text: string): string {// 过滤 HEARTBEAT 健康检查消息if (HEARTBEAT_RE.test(text)) {return "";}+ // Drop legacy synthetic tool placeholders before they reach memory extraction.+ if (TOOL_PLACEHOLDER_RE.test(text)) {+ return "";+ }// 处理 Compactor 系统消息,提取实际用户输入// 格式: "System: [时间] Compacted ... Context ... [时间] 实际内容"if (COMPACTED_SYSTEM_MSG_RE.test(text)) {@@ -436,40 +441,6 @@ export function extractNewTurnMessages(}],});}- } else {- /// 如果原始消息有 toolCall,提取所有工具名并生成占位符- if (role === "assistant" && Array.isArray(content)) {- const toolNames: string[] = [];-- // 收集所有 toolCall 的 tool_name- for (const block of content) {- const b = block as Record<string, unknown>;- const blockType = b?.type as string;- if (- blockType === "toolCall" ||- blockType === "toolUse" ||- blockType === "tool_use" ||- blockType === "tool_call"- ) {- const name = b?.name as string || b?.toolName as string;- if (name && typeof name === "string" && name.trim()) {- toolNames.push(name.trim());- }- }- }-- // 只有找到 toolCall 时才添加占位符- if (toolNames.length > 0) {- const toolNamesStr = toolNames.join(", ");- result.push({- role: "assistant",- parts: [{- type: "text",- text: `[tool: ${toolNamesStr}]`,- }],- });- }- }}}