openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
308 lines (307 loc) • 9.59 kB
JavaScript
import { r as isIncognitoSessionKey } from "./session-key-DW1jcHbM.mjs";
import { M as requiresFoldedSessionKeyAliasProof } from "./session-key-CqAyZeVk.mjs";
import { l as resolveSessionStorePathCore } from "./paths-DvQ5AJvD.mjs";
import { h as readRuntimeConfig } from "./runtime-snapshot-WbwbY5b7.mjs";
import "./io-4QsrsWTJ.mjs";
import { r as hasDeliveryTargetFields } from "./delivery-context.shared-CRlipPTj.mjs";
import { t as deliveryContextFromSession } from "./delivery-context.read-BrYAJ7RK.mjs";
import { a as readSessionEntrySummariesInWorker, i as readSessionEntryReadOnlyInWorker, n as readSessionEntriesFromStoreInWorker } from "./session-entry-read-runtime-1Fp1sybj.mjs";
import { a as normalizeStoreSessionKey, i as isConfirmedLowercasedLegacyAlias, n as foldedSessionKeyAliasCandidates, r as hasMismatchedCaseSensitiveDeliveryProof } from "./store-entry-BTUJi0xl.mjs";
import { n as captureMemoryExactSessionReader } from "./session-accessor.memory-exact-read-DXDGhyTq.mjs";
import { a as resolveSessionStoreIdentity, o as resolveSessionStoreKey } from "./session-store-key-CEwU3noi.mjs";
import { i as resolveSessionThreadInfo } from "./session-conversation-ANXEVkYh.mjs";
import { n as resolveAllAgentSessionStoreTargetsAsync } from "./targets-runtime-pFH3Bycg.mjs";
//#region src/config/sessions/delivery-info.ts
/** Reads only the current session; missing delivery must not widen into alias discovery. */
async function readExactSessionDeliveryContext(e) {
const t = e.sessionKey?.trim();
if (!t) return;
try {
const { agentId: n, canonicalKey: r } = resolveSessionStoreIdentity({
cfg: e.cfg,
sessionKey: t
});
const i = await readSessionEntryReadOnlyInWorker({
agentId: n,
storePath: resolveSessionStorePathCore(e.cfg.session?.store, { agentId: n }),
sessionKey: r,
projection: "list"
});
if (e.sessionId && i?.sessionId !== e.sessionId) return;
return deliveryContextFromSession(i);
} catch {
return;
}
}
/**
* Extracts the routable delivery context and thread id for a persisted session key.
*
* Thread/topic keys first try their exact store entry, then fall back to the base session when
* the thread entry has no delivery route of its own.
*/
async function extractDeliveryInfo(e, t) {
return (await extractDeliveryInfoBatch([{
sessionKey: e,
agentId: t?.agentId
}], t))[0];
}
/** Resolves one batch through the session owner; only detached delivery facts leave the read scope. */
async function extractDeliveryInfoBatch(e, t) {
const n = e.map(({ sessionKey: e, agentId: t }) => ({
sessionKey: e,
agentId: t,
...resolveSessionThreadInfo(e)
}));
const r = n.map(({ threadId: e }) => ({
deliveryContext: void 0,
threadId: e
}));
if (!n.some(({ sessionKey: e, baseSessionKey: t }) => e && t)) return r;
let i;
try {
i = t?.cfg ?? readRuntimeConfig();
} catch {
return r;
}
let a;
try {
a = await resolveAllAgentSessionStoreTargetsAsync(i);
} catch {
return r;
}
function prepareDeliveryLookup(e, t, n) {
const { agentId: r, canonicalKey: o } = resolveSessionStoreIdentity({
cfg: i,
sessionKey: t,
agentId: n
});
const s = resolveSessionStoreKey({
cfg: i,
sessionKey: e,
storeAgentId: r
});
const c = isIncognitoSessionKey(e) ? captureMemoryExactSessionReader({
agentId: r,
sessionKey: e
}) : void 0;
const l = /* @__PURE__ */ new Set([c?.path ?? resolveSessionStorePathCore(i.session?.store, { agentId: r })]);
for (const e of c ? [] : a) if (e.agentId === r) l.add(e.storePath);
return {
agentId: r,
sessionKeys: [e, s],
baseKeys: [t, o],
exactKeys: deliveryLookupExactKeys([
e,
s,
t,
o
]),
storePaths: [...l]
};
}
const o = n.flatMap(({ sessionKey: e, baseSessionKey: t, agentId: n }, r) => {
if (!e || !t) return [];
try {
return [{
index: r,
sessionKey: e,
baseSessionKey: t,
lookup: prepareDeliveryLookup(e, t, n)
}];
} catch {
return [];
}
});
const s = /* @__PURE__ */ new Map();
for (const { lookup: e } of o) {
if (e.sessionKeys.some(isIncognitoSessionKey)) continue;
const t = JSON.stringify([e.agentId, e.storePaths[0]]);
let n = s.get(t);
if (!n) {
n = {
keySets: /* @__PURE__ */ new Set(),
keys: /* @__PURE__ */ new Set()
};
s.set(t, n);
}
n.keySets.add(JSON.stringify(e.exactKeys));
for (const t of e.exactKeys) n.keys.add(t);
}
const c = /* @__PURE__ */ new Map();
const readIndependent = (e, t) => {
const n = t.exactKeys;
const r = JSON.stringify([
t.agentId,
e,
n
]);
let i = c.get(r);
if (!i) {
i = t.sessionKeys.some(isIncognitoSessionKey) ? Promise.all(n.map(async (n) => {
const r = await readSessionEntryReadOnlyInWorker({
agentId: t.agentId,
storePath: e,
sessionKey: n,
projection: "list"
});
return r ? [{
sessionKey: n,
entry: r
}] : [];
})).then((e) => ({ entries: e.flat() })) : readSessionEntriesFromStoreInWorker({
agentId: t.agentId,
storePath: e,
sessionKeys: n,
projection: "list",
snapshotFields: []
});
c.set(r, i);
}
return i;
};
const readExact = async (e, t) => {
const n = !t.sessionKeys.some(isIncognitoSessionKey) && e === t.storePaths[0] ? s.get(JSON.stringify([t.agentId, e])) : void 0;
if (n && n.keySets.size > 1) {
n.read ??= readSessionEntriesFromStoreInWorker({
agentId: t.agentId,
storePath: e,
sessionKeys: [...n.keys],
projection: "list",
snapshotFields: []
}).then(({ entries: e, source: t }) => ({
entries: new Map(e.map((e) => [e.sessionKey, e])),
source: t
})).catch(() => void 0);
const r = await n.read;
if (r) return {
entries: t.exactKeys.flatMap((e) => {
const t = r.entries.get(e);
return t ? [t] : [];
}),
source: r.source
};
}
return readIndependent(e, t);
};
const l = /* @__PURE__ */ new Map();
for (const { index: e, sessionKey: t, baseSessionKey: n, lookup: i } of o) try {
const a = await loadDeliverySessionEntry(i, async (e) => {
const t = await readExact(e, i);
const n = t.source;
const r = n ? `${n.agentId}\u0000${n.path}` : e;
let a = l.get(r);
if (!a) {
a = lazyFoldedSignalIndex(n ? {
storePath: n.path,
agentId: n.agentId
} : { storePath: e });
l.set(r, a);
}
const o = new Map(t.entries.map(({ sessionKey: e, entry: t }) => [e, t]));
return {
get: (e) => o.get(e),
foldedSignalIndex: a
};
});
let o = deliveryContextFromSession(a.entry);
if (!hasDeliveryTargetFields(o) && n !== t) o = deliveryContextFromSession(a.baseEntry);
if (hasDeliveryTargetFields(o)) r[e].deliveryContext = {
channel: o.channel,
to: o.to,
accountId: o.accountId,
threadId: o.threadId
};
} catch {}
return r;
}
function deliveryLookupExactKeys(e) {
return [...new Set(e.flatMap((e) => {
const t = normalizeStoreSessionKey(e);
return [
t,
...foldedSessionKeyAliasCandidates(t),
e.trim()
];
}))];
}
function lazyFoldedSignalIndex(e) {
let t;
return () => t ??= readSessionEntrySummariesInWorker(e).then(buildFoldedSignalSessionEntryIndex);
}
async function findSessionEntryInStore(e, t) {
let n;
let r = 0;
let i = false;
let a = false;
const acceptCandidate = (e, t = false) => {
if (!e) return;
const o = hasDeliveryTargetFields(deliveryContextFromSession(e));
const s = e.updatedAt ?? 0;
if (!n || o && !i || o === i && t && !a || o === i && t === a && s > r) {
n = e;
r = s;
i = o;
a = t;
}
};
for (const n of t) {
const t = n.trim();
const r = normalizeStoreSessionKey(n);
const i = foldedSessionKeyAliasCandidates(r);
const a = requiresFoldedSessionKeyAliasProof(r);
let o = false;
const s = e.get(r);
if (s && !hasMismatchedCaseSensitiveDeliveryProof(s, r)) {
o ||= hasDeliveryTargetFields(deliveryContextFromSession(s));
acceptCandidate(s, a);
}
for (const t of i) {
const n = e.get(t);
if (!n || !isConfirmedLowercasedLegacyAlias(n, r)) continue;
o ||= hasDeliveryTargetFields(deliveryContextFromSession(n));
acceptCandidate(n);
}
const c = t !== r ? e.get(t) : void 0;
if (c && !hasMismatchedCaseSensitiveDeliveryProof(c, r)) {
o ||= hasDeliveryTargetFields(deliveryContextFromSession(c));
acceptCandidate(c);
}
if (!a && r.includes(":signal:group:") && (t !== r || !o)) acceptCandidate((await e.foldedSignalIndex()).get(r.toLowerCase()));
}
return n;
}
function buildFoldedSignalSessionEntryIndex(e) {
const t = /* @__PURE__ */ new Map();
for (const { sessionKey: n, entry: r } of e) {
if (!n.includes(":signal:group:") || requiresFoldedSessionKeyAliasProof(n)) continue;
const e = n.toLowerCase();
const i = t.get(e);
const a = hasDeliveryTargetFields(deliveryContextFromSession(r));
const o = hasDeliveryTargetFields(deliveryContextFromSession(i));
if (!i || a && !o || a === o && (r.updatedAt ?? 0) > (i.updatedAt ?? 0)) t.set(e, r);
}
return t;
}
async function loadDeliverySessionEntry(e, t) {
let n;
for (const r of e.storePaths) {
const i = await t(r);
const a = await findSessionEntryInStore(i, e.sessionKeys);
const o = await findSessionEntryInStore(i, e.baseKeys);
if (!a && !o) continue;
n ??= {
entry: a,
baseEntry: o
};
if (hasDeliveryTargetFields(deliveryContextFromSession(a)) || hasDeliveryTargetFields(deliveryContextFromSession(o))) return {
entry: a,
baseEntry: o
};
}
return n ?? {
entry: void 0,
baseEntry: void 0
};
}
//#endregion
export { extractDeliveryInfoBatch as n, readExactSessionDeliveryContext as r, extractDeliveryInfo as t };