UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

179 lines (178 loc) • 6.54 kB
import { t as pruneMapToMaxSize } from "./map-size-BBr20tbc.mjs"; //#region src/auto-reply/reply/history.ts const HISTORY_CONTEXT_MARKER = "[Chat messages since your last reply - for context]"; const RECENT_HISTORY_CONTEXT_MARKER = "[Recent chat messages - for context]"; const CURRENT_MESSAGE_MARKER = "[Current message - respond to this]"; const MAX_HISTORY_KEYS = 1e3; /** * Evict oldest keys from a history map when it exceeds MAX_HISTORY_KEYS. * Uses Map's insertion order for LRU-like behavior. */ function evictOldHistoryKeys(e, t = MAX_HISTORY_KEYS) { pruneMapToMaxSize(e, t); } function buildHistoryContext(e) { const { historyText: t, currentMessage: n } = e; const r = e.lineBreak ?? "\n"; if (!t.trim()) return n; return [ e.historyKind === "recent" ? RECENT_HISTORY_CONTEXT_MARKER : HISTORY_CONTEXT_MARKER, t, "", CURRENT_MESSAGE_MARKER, n ].join(r); } /** Appends one history entry, enforces per-session limit, and refreshes LRU key order. */ function recordChannelHistoryEntryIfEnabled(e) { const { historyMap: t, historyKey: n, entry: r } = e; if (!r || e.limit <= 0) return []; const i = t.get(n) ?? []; i.push(r); const a = i.length - e.limit; if (a > 0) i.splice(0, a); t.delete(n); t.set(n, i); evictOldHistoryKeys(t); return i; } /** * @deprecated Plugin message-turn code should use `createChannelHistoryWindow(...).record(...)`. * This helper remains for core internals and older plugin compatibility. */ const recordPendingHistoryEntryIfEnabled = recordChannelHistoryEntryIfEnabled; const DEFAULT_HISTORY_MEDIA_LIMIT = 4; function isLocalHistoryMediaPath(e) { return /^[a-z]:[\\/]/i.test(e) || !/^[a-z][a-z0-9+.-]*:/i.test(e); } function isImageHistoryMediaEntry(e) { if (e.kind && e.kind !== "unknown") return e.kind === "image" || e.kind === "sticker"; return e.contentType?.split(";")[0]?.trim().toLowerCase().startsWith("image/") === true; } /** Filters history media to local image entries safe to re-attach to prompt context. */ function normalizeHistoryMediaEntries(e) { const t = Math.max(0, e.limit ?? DEFAULT_HISTORY_MEDIA_LIMIT); if (t <= 0 || !e.media?.length) return []; const n = []; const r = /* @__PURE__ */ new Set(); for (const i of e.media) { if (!isImageHistoryMediaEntry(i)) continue; const a = i.path?.trim(); if (!a || !isLocalHistoryMediaPath(a)) continue; const o = `${i.messageId ?? e.messageId ?? ""}\0${a}`; if (r.has(o)) continue; r.add(o); n.push({ path: a, contentType: i.contentType, kind: i.kind === "sticker" ? "sticker" : "image", messageId: i.messageId ?? e.messageId }); if (n.length >= t) break; } return n; } async function recordChannelHistoryEntryWithMedia(e) { if (!e.entry || e.limit <= 0) return []; if (e.shouldRecord && !e.shouldRecord()) return []; const t = e.entry; const n = typeof e.media === "function" ? recordChannelHistoryEntryIfEnabled(e) : void 0; const r = typeof e.media === "function" ? await e.media() : e.media; if (e.shouldRecord && !e.shouldRecord()) return n ?? []; const i = normalizeHistoryMediaEntries({ media: r, limit: e.mediaLimit, messageId: e.messageId ?? e.entry.messageId }); if (n) { const r = e.historyMap.get(e.historyKey); const a = r?.indexOf(t) ?? -1; if (i.length > 0 && r && a >= 0) r[a] = { ...t, media: i }; return n; } return recordChannelHistoryEntryIfEnabled({ ...e, entry: i.length > 0 ? { ...e.entry, media: i } : e.entry }); } /** * @deprecated Plugin message-turn code should use * `createChannelHistoryWindow(...).recordWithMedia(...)`. */ const recordPendingHistoryEntryWithMedia = recordChannelHistoryEntryWithMedia; function buildChannelPendingHistoryContext(e) { return buildHistoryContextFromMap({ ...e, entry: void 0, excludeLast: false }); } /** * @deprecated Plugin message-turn code should use * `createChannelHistoryWindow(...).buildPendingContext(...)`. */ const buildPendingHistoryContextFromMap = buildChannelPendingHistoryContext; function buildChannelInboundHistory(e) { return buildInboundHistoryFromEntries({ entries: e.historyMap.get(e.historyKey) ?? [], limit: e.limit }); } /** * @deprecated Plugin message-turn code should use * `createChannelHistoryWindow(...).buildInboundHistory(...)`. */ const buildInboundHistoryFromMap = buildChannelInboundHistory; function buildInboundHistoryFromEntries(e) { if (e.limit <= 0) return; return e.entries.slice(-e.limit).map((e) => { const t = { sender: e.sender, body: e.body, timestamp: e.timestamp }; if (e.messageId) t.messageId = e.messageId; if (e.media?.length) t.media = e.media; return t; }); } /** * @deprecated Prefer `buildHistoryContextFromEntries(...)` for existing entry * arrays, or `createChannelHistoryWindow(...)` when working from a history map. * This helper remains for older plugin compatibility. */ function buildHistoryContextFromMap(e) { if (e.limit <= 0) return e.currentMessage; return buildHistoryContextFromEntries({ entries: e.entry ? recordChannelHistoryEntryIfEnabled(e) : e.historyMap.get(e.historyKey) ?? [], currentMessage: e.currentMessage, formatEntry: e.formatEntry, lineBreak: e.lineBreak, excludeLast: e.excludeLast }); } function clearChannelHistoryIfEnabled(e) { if (e.limit > 0) e.historyMap.set(e.historyKey, []); } /** * @deprecated Plugin message-turn code should use `createChannelHistoryWindow(...).clear(...)`. * This helper remains for core internals and older plugin compatibility. */ const clearHistoryEntriesIfEnabled = clearChannelHistoryIfEnabled; function buildHistoryContextFromEntries(e) { const t = e.lineBreak ?? "\n"; return buildHistoryContext({ historyText: (e.excludeLast === false ? e.entries : e.entries.slice(0, -1)).map(e.formatEntry).join(t), currentMessage: e.currentMessage, lineBreak: t, historyKind: e.historyKind }); } //#endregion export { recordChannelHistoryEntryWithMedia as _, buildChannelPendingHistoryContext as a, buildHistoryContextFromMap as c, buildPendingHistoryContextFromMap as d, clearChannelHistoryIfEnabled as f, recordChannelHistoryEntryIfEnabled as g, normalizeHistoryMediaEntries as h, buildChannelInboundHistory as i, buildInboundHistoryFromEntries as l, evictOldHistoryKeys as m, HISTORY_CONTEXT_MARKER as n, buildHistoryContext as o, clearHistoryEntriesIfEnabled as p, RECENT_HISTORY_CONTEXT_MARKER as r, buildHistoryContextFromEntries as s, CURRENT_MESSAGE_MARKER as t, buildInboundHistoryFromMap as u, recordPendingHistoryEntryIfEnabled as v, recordPendingHistoryEntryWithMedia as y };