Files
openclaw/src/channels/session.ts
Peter Steinberger 00d8d7ead0 refactor: extract normalization core package
Extract shared normalization/coercion helpers into private @openclaw/normalization-core workspace package while preserving existing plugin SDK helper subpaths.\n\nAlso keeps direct normalization-core imports internal, wires UI/build/loader resolution, and replaces the slow PR network CodeQL lane with a fast added-line boundary scan while retaining full CodeQL for scheduled/manual runs.\n\nVerification: local moved tests, plugin SDK boundary tests, extension loader tests, agents-support shard, UI build/test, build artifacts, lint, workflow guards, autoreview, and GitHub CI passed on PR head 963d893715.
2026-05-31 01:33:00 +01:00

81 lines
2.7 KiB
TypeScript

import { normalizeLowercaseStringOrEmpty } from "@openclaw/normalization-core/string-coerce";
import type { MsgContext } from "../auto-reply/templating.js";
import type { GroupKeyResolution } from "../config/sessions/types.js";
import { normalizeSessionKeyPreservingOpaquePeerIds } from "../sessions/session-key-utils.js";
import type { InboundLastRouteUpdate } from "./session.types.js";
export type { InboundLastRouteUpdate, RecordInboundSession } from "./session.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 shouldSkipPinnedMainDmRouteUpdate(
pin: InboundLastRouteUpdate["mainDmOwnerPin"] | undefined,
): boolean {
if (!pin) {
return false;
}
const owner = normalizeLowercaseStringOrEmpty(pin.ownerRecipient);
const sender = normalizeLowercaseStringOrEmpty(pin.senderRecipient);
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;
trackSessionMetaTask?: (task: Promise<unknown>) => void;
}): Promise<void> {
const { storePath, sessionKey, ctx, groupResolution, createIfMissing } = params;
const canonicalSessionKey = normalizeSessionKeyPreservingOpaquePeerIds(sessionKey);
const runtime = await loadInboundSessionRuntime();
const metaTask = runtime
.recordSessionMetaFromInbound({
storePath,
sessionKey: canonicalSessionKey,
ctx,
groupResolution,
createIfMissing,
})
.catch(params.onRecordError);
params.trackSessionMetaTask?.(metaTask);
void metaTask;
const update = params.updateLastRoute;
if (!update) {
return;
}
if (shouldSkipPinnedMainDmRouteUpdate(update.mainDmOwnerPin)) {
return;
}
const targetSessionKey = normalizeSessionKeyPreservingOpaquePeerIds(update.sessionKey);
await runtime.updateLastRoute({
storePath,
sessionKey: targetSessionKey,
route: update.route,
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,
createIfMissing,
});
}