UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

78 lines (77 loc) • 3.16 kB
import { o as isSilentAssistantTranscriptMessage } from "./chat-message-content-DNUdbbJ8.mjs"; import { s as stripHeartbeatToken } from "./heartbeat-DHK32_6H.mjs"; import { i as isToolCallBlockType } from "./tool-block-contract-BsnlYLrK.mjs"; //#region src/agents/compaction-real-conversation.ts /** * Classifies transcript messages that contain real user-visible conversation * for compaction and history pruning. */ const TOOL_RESULT_REAL_CONVERSATION_LOOKBACK = 20; const NON_CONVERSATION_BLOCK_TYPES = /* @__PURE__ */ new Set(["thinking", "reasoning"]); const CONVERSATION_ANCHOR_ROLES = /* @__PURE__ */ new Set([ "user", "custom", "bashExecution", "branchSummary", "compactionSummary" ]); function hasMeaningfulText(e) { const t = e.trim(); if (!t) return false; const n = stripHeartbeatToken(t, { mode: "message" }); return !n.didStrip || n.text.trim().length > 0; } /** Returns whether a message has content worth preserving as conversation. */ function hasMeaningfulConversationContent(e) { if ("excludeFromContext" in e && e.excludeFromContext === true) return false; if (isSilentAssistantTranscriptMessage(e)) return false; if (e.role === "custom") { const t = e; return t.display !== false && hasMeaningfulMessageContent(t.content); } if (e.role === "bashExecution") return hasMeaningfulText(`${typeof e.command === "string" ? e.command : ""}\n${typeof e.output === "string" ? e.output : ""}`); if (e.role === "branchSummary" || e.role === "compactionSummary") { const t = e.summary; return typeof t === "string" && hasMeaningfulText(t); } const t = e.content; return hasMeaningfulMessageContent(t); } function hasMeaningfulMessageContent(e) { if (typeof e === "string") return hasMeaningfulText(e); if (!Array.isArray(e)) return false; let t = false; for (const n of e) { if (!n || typeof n !== "object") continue; const { type: e, text: r } = n; if (e === "text") { if (typeof r === "string" && hasMeaningfulText(r)) return true; } else if (!isToolCallBlockType(e) && !NON_CONVERSATION_BLOCK_TYPES.has(String(e))) t = true; } return t; } function isToolResultConversationAnchor(e) { return CONVERSATION_ANCHOR_ROLES.has(e.role) && hasMeaningfulConversationContent(e); } /** Returns whether a transcript message should count as real conversation. */ function isRealConversationMessage(e, t, n) { if (e.role === "assistant" || CONVERSATION_ANCHOR_ROLES.has(e.role)) return hasMeaningfulConversationContent(e); if (e.role !== "toolResult") return false; const r = Math.max(0, n - TOOL_RESULT_REAL_CONVERSATION_LOOKBACK); for (let e = n - 1; e >= r; e -= 1) { const n = t[e]; if (n && isToolResultConversationAnchor(n)) return true; } return false; } /** Classifies an ordered transcript without retaining earlier pages. */ function createRealConversationClassifier() { let e = 0; return (t) => { const n = t.role === "toolResult" ? e > 0 : isRealConversationMessage(t, [], 0); e = isToolResultConversationAnchor(t) ? TOOL_RESULT_REAL_CONVERSATION_LOOKBACK : Math.max(0, e - 1); return n; }; } //#endregion export { isRealConversationMessage as n, createRealConversationClassifier as t };