openclaw/src/config/sessions/explicit-session-key-normalization.ts
scoootscooob 439c21e078
refactor: remove channel shim directories, point all imports to extensions (#45967)
* refactor: remove channel shim directories, point all imports to extensions

Delete the 6 backward-compat shim directories (src/telegram, src/discord,
src/slack, src/signal, src/imessage, src/web) that were re-exporting from
extensions. Update all 112+ source files to import directly from
extensions/{channel}/src/ instead of through the shims.

Also:
- Move src/channels/telegram/ (allow-from, api) to extensions/telegram/src/
- Fix outbound adapters to use resolveOutboundSendDep (fixes 5 pre-existing TS errors)
- Update cross-extension imports (src/web/media.js → extensions/whatsapp/src/media.js)
- Update vitest, tsdown, knip, labeler, and script configs for new paths
- Update guard test allowlists for extension paths

After this, src/ has zero channel-specific implementation code — only the
generic plugin framework remains.

* fix: update raw-fetch guard allowlist line numbers after shim removal

* refactor: document direct extension channel imports

* test: mock transcript module in delivery helpers
2026-03-14 03:43:07 -07:00

51 lines
1.7 KiB
TypeScript

import { normalizeExplicitDiscordSessionKey } from "../../../extensions/discord/src/session-key-normalization.js";
import type { MsgContext } from "../../auto-reply/templating.js";
type ExplicitSessionKeyNormalizer = (sessionKey: string, ctx: MsgContext) => string;
type ExplicitSessionKeyNormalizerEntry = {
provider: string;
normalize: ExplicitSessionKeyNormalizer;
matches: (params: {
sessionKey: string;
provider?: string;
surface?: string;
from: string;
}) => boolean;
};
const EXPLICIT_SESSION_KEY_NORMALIZERS: ExplicitSessionKeyNormalizerEntry[] = [
{
provider: "discord",
normalize: normalizeExplicitDiscordSessionKey,
matches: ({ sessionKey, provider, surface, from }) =>
surface === "discord" ||
provider === "discord" ||
from.startsWith("discord:") ||
sessionKey.startsWith("discord:") ||
sessionKey.includes(":discord:"),
},
];
function resolveExplicitSessionKeyNormalizer(
sessionKey: string,
ctx: Pick<MsgContext, "From" | "Provider" | "Surface">,
): ExplicitSessionKeyNormalizer | undefined {
const normalizedProvider = ctx.Provider?.trim().toLowerCase();
const normalizedSurface = ctx.Surface?.trim().toLowerCase();
const normalizedFrom = (ctx.From ?? "").trim().toLowerCase();
return EXPLICIT_SESSION_KEY_NORMALIZERS.find((entry) =>
entry.matches({
sessionKey,
provider: normalizedProvider,
surface: normalizedSurface,
from: normalizedFrom,
}),
)?.normalize;
}
export function normalizeExplicitSessionKey(sessionKey: string, ctx: MsgContext): string {
const normalized = sessionKey.trim().toLowerCase();
const normalize = resolveExplicitSessionKeyNormalizer(normalized, ctx);
return normalize ? normalize(normalized, ctx) : normalized;
}