UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

78 lines (77 loc) • 3.54 kB
import { c as normalizeOptionalLowercaseString, o as normalizeLowercaseStringOrEmpty } from "./string-coerce-CCIiB4H7.mjs"; import { a as asOptionalRecord } from "./record-coerce-CVPuDqcc.mjs"; import { r as normalizeProviderId } from "./provider-id-b5XGSf9C.mjs"; import { d as resolveLinkedDirectPeerId } from "./session-key-CqAyZeVk.mjs"; import { n as normalizeAccountId } from "./account-id-BqKT3GuY.mjs"; import { t as normalizeChatType } from "./chat-type-CL5nBDaO.mjs"; import { r as resolveChannelAccountEntry } from "./account-lookup-FhQZVFfo.mjs"; import { a as resolveChannelEntryMatchWithFallback } from "./channel-config-BaCM1Vmv.mjs"; //#region src/agents/embedded-agent-runner/history.ts const THREAD_SUFFIX_REGEX = /^(.*)(?::(?:thread|topic):\d+)$/i; const SESSION_HISTORY_PRELUDE = Symbol.for("openclaw.sessionHistoryPrelude"); function stripThreadSuffix(e) { return e.match(THREAD_SUFFIX_REGEX)?.[1] ?? e; } /** * Leading non-conversation messages (e.g. compactionSummary, branchSummary) * placed at index 0 by buildSessionContext are always preserved, since they * carry summarized pre-compaction context that history limiting must not drop. */ function limitHistoryTurns(e, t, n = {}) { if (!t || t <= 0 || e.length === 0) return e; const r = e.findIndex((e) => !e?.[SESSION_HISTORY_PRELUDE] && (e?.role === "user" || e?.role === "assistant")); if (r < 0) return e; const i = []; for (let t = r; t < e.length; t++) if (e[t]?.role === "user" && (!n.groupConsecutiveUserTurns || e[t - 1]?.role !== "user")) i.push(t); const a = Math.floor(t); const o = Math.ceil(a * 1.5); const s = i.length; if (s <= o) return e; const c = o - a + 1; const l = a + (s - a) % c; const u = i.at(-l) ?? e.length; return [...e.slice(0, r), ...e.slice(u)]; } function getHistoryLimitFromSessionKey(e, t, n) { if (!e || !t) return; const r = e.split(":"); const i = r.length >= 3 && r[0] === "agent" ? r.slice(2) : r; const a = normalizeProviderId(i[0] ?? ""); if (!a) return; const o = [0, 1].flatMap((e) => { const t = normalizeChatType(i[1 + e]); const r = e ? i[1] : void 0; if (!t || n?.chatType && t !== n.chatType || e && t !== "direct") return []; return [{ kind: t, accountId: r, userId: i.slice(2 + e).join(":") }]; }); const s = n?.peerId?.trim(); const c = s ? normalizeOptionalLowercaseString(resolveLinkedDirectPeerId({ identityLinks: t.session?.identityLinks, channel: a, peerId: s }) ?? s) : void 0; const l = c ? o.filter((e) => e.kind !== "direct" || e.userId === c || stripThreadSuffix(e.userId) === c) : o; const u = l.length === 1 ? l[0] : void 0; const d = u?.kind ?? (o.every((e) => e.kind === o[0]?.kind) ? o[0]?.kind : void 0); const f = u && (c ?? stripThreadSuffix(u.userId)); const p = n?.accountId ?? (o.length === 1 ? o[0]?.accountId : u?.accountId); const m = asOptionalRecord(Object.entries(t.channels ?? {}).find(([e]) => normalizeProviderId(e) === a)?.[1]); if (!m) return; const h = p?.trim(); const g = h ? resolveChannelAccountEntry(m.accounts, normalizeAccountId(h), a, normalizeAccountId) : void 0; if (d === "direct") { const e = f ? resolveChannelEntryMatchWithFallback({ entries: g?.dms ?? m.dms, keys: [f], normalizeKey: normalizeLowercaseStringOrEmpty }).entry?.historyLimit : void 0; return e !== void 0 ? e : g?.dmHistoryLimit ?? m.dmHistoryLimit; } return d === "channel" || d === "group" ? g?.historyLimit ?? m.historyLimit : void 0; } //#endregion export { limitHistoryTurns as n, getHistoryLimitFromSessionKey as t };