Files
openclaw/src/channels/session.ts
2026-03-29 22:42:06 +01:00

89 lines
2.6 KiB
TypeScript

import type { MsgContext } from "../auto-reply/templating.js";
import type { GroupKeyResolution, SessionEntry } from "../config/sessions/types.js";
let inboundSessionRuntimePromise: Promise<
typeof import("../config/sessions/inbound.runtime.js")
> | null = null;
function loadInboundSessionRuntime() {
inboundSessionRuntimePromise ??= import("../config/sessions/inbound.runtime.js");
return inboundSessionRuntimePromise;
}
function normalizeSessionStoreKey(sessionKey: string): string {
return sessionKey.trim().toLowerCase();
}
export type InboundLastRouteUpdate = {
sessionKey: string;
channel: SessionEntry["lastChannel"];
to: string;
accountId?: string;
threadId?: string | number;
mainDmOwnerPin?: {
ownerRecipient: string;
senderRecipient: string;
onSkip?: (params: { ownerRecipient: string; senderRecipient: string }) => void;
};
};
function shouldSkipPinnedMainDmRouteUpdate(
pin: InboundLastRouteUpdate["mainDmOwnerPin"] | undefined,
): boolean {
if (!pin) {
return false;
}
const owner = pin.ownerRecipient.trim().toLowerCase();
const sender = pin.senderRecipient.trim().toLowerCase();
if (!owner || !sender || owner === sender) {
return false;
}
pin.onSkip?.({ ownerRecipient: pin.ownerRecipient, senderRecipient: pin.senderRecipient });
return true;
}
export async function recordInboundSession(params: {
storePath: string;
sessionKey: string;
ctx: MsgContext;
groupResolution?: GroupKeyResolution | null;
createIfMissing?: boolean;
updateLastRoute?: InboundLastRouteUpdate;
onRecordError: (err: unknown) => void;
}): Promise<void> {
const { storePath, sessionKey, ctx, groupResolution, createIfMissing } = params;
const canonicalSessionKey = normalizeSessionStoreKey(sessionKey);
const runtime = await loadInboundSessionRuntime();
void runtime
.recordSessionMetaFromInbound({
storePath,
sessionKey: canonicalSessionKey,
ctx,
groupResolution,
createIfMissing,
})
.catch(params.onRecordError);
const update = params.updateLastRoute;
if (!update) {
return;
}
if (shouldSkipPinnedMainDmRouteUpdate(update.mainDmOwnerPin)) {
return;
}
const targetSessionKey = normalizeSessionStoreKey(update.sessionKey);
await runtime.updateLastRoute({
storePath,
sessionKey: targetSessionKey,
deliveryContext: {
channel: update.channel,
to: update.to,
accountId: update.accountId,
threadId: update.threadId,
},
// Avoid leaking inbound origin metadata into a different target session.
ctx: targetSessionKey === canonicalSessionKey ? ctx : undefined,
groupResolution,
});
}