openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
84 lines (83 loc) • 3.41 kB
JavaScript
import { c as normalizeOptionalLowercaseString, l as normalizeOptionalString } from "./string-coerce-CCIiB4H7.mjs";
import { l as isRecord } from "./record-coerce-CVPuDqcc.mjs";
import { t as safeParseJson } from "./json-coercion-CaJLaav-.mjs";
import { randomUUID } from "node:crypto";
//#region src/config/sessions/conversation-route-context.ts
const MAX_ROUTE_CONTEXT_ID_LENGTH = 512;
const MAX_ROUTE_CONTEXT_ROLE_IDS = 256;
const MAX_STORED_ROUTE_CONTEXT_LENGTH = 14e4;
function normalizeBoundedId(e) {
const t = normalizeOptionalString(e);
return t && t.length <= MAX_ROUTE_CONTEXT_ID_LENGTH ? t : void 0;
}
function normalizeRoleIds(e) {
if (e === void 0) return { valid: true };
if (!Array.isArray(e) || e.length > MAX_ROUTE_CONTEXT_ROLE_IDS) return { valid: false };
const t = [];
for (const n of e) {
const e = normalizeBoundedId(n);
if (!e) return { valid: false };
t.push(e);
}
const n = [...new Set(t)].toSorted();
return n.length > 0 ? {
valid: true,
value: n
} : { valid: true };
}
/** Parses the closed, bounded route facts used to replay configured routing precedence. */
function parseConversationRouteContext(e) {
if (!isRecord(e)) return;
const t = {};
for (const n of [
"peerId",
"guildId",
"teamId",
"parentPeerId"
]) {
const r = normalizeBoundedId(e[n]);
if (e[n] !== void 0 && !r) return;
if (r) t[n] = r;
}
const n = normalizeRoleIds(e.memberRoleIds);
if (!n.valid) return;
if (n.value) t.memberRoleIds = n.value;
return Object.keys(t).length > 0 ? t : void 0;
}
/** Captures only authoritative inbound facts needed to replay configured route precedence. */
function conversationRouteContextFromMsgContext(e) {
const t = normalizeOptionalLowercaseString(e.OriginatingChannel ?? e.Provider);
const n = normalizeBoundedId(e.GroupSpace);
const r = normalizeBoundedId(e.ThreadParentId);
return parseConversationRouteContext({
...e.ConversationRoutePeerId !== void 0 ? { peerId: e.ConversationRoutePeerId } : {},
...t === "discord" && n ? { guildId: n } : {},
...(t === "slack" || t === "mattermost" || t === "msteams") && n ? { teamId: n } : {},
...r ? { parentPeerId: r } : {},
...e.MemberRoleIds !== void 0 ? { memberRoleIds: e.MemberRoleIds } : {}
});
}
function serializeStoredConversationRouteContext(e, t) {
const n = e === null ? null : parseConversationRouteContext(e);
if (e !== null && !n) throw new Error("Invalid conversation route context");
return JSON.stringify({
version: 1,
writeId: randomUUID(),
observedAt: t,
context: n ?? null
});
}
function parseStoredConversationRouteContext(e, t) {
if (!e || e.length > MAX_STORED_ROUTE_CONTEXT_LENGTH) return;
const n = safeParseJson(e);
if (!isRecord(n) || n.version !== 1 || typeof n.writeId !== "string" || n.writeId.length === 0 || typeof n.observedAt !== "number" || n.observedAt !== t) return;
const r = parseConversationRouteContext(n.context);
if (n.context !== null && !r) return;
return r ? { context: r } : {};
}
function refreshStoredConversationRouteContext(e, t, n) {
const r = parseStoredConversationRouteContext(e, t);
return r ? serializeStoredConversationRouteContext(r.context ?? null, n) : null;
}
//#endregion
export { serializeStoredConversationRouteContext as a, refreshStoredConversationRouteContext as i, parseConversationRouteContext as n, parseStoredConversationRouteContext as r, conversationRouteContextFromMsgContext as t };