refactor(sessions): simplify provider normalizer matching

This commit is contained in:
Peter Steinberger
2026-03-08 01:26:43 +00:00
parent e381ab630e
commit 8cc477b873
2 changed files with 50 additions and 22 deletions

View File

@@ -39,6 +39,19 @@ describe("normalizeExplicitSessionKey", () => {
).toBe("discord:direct:123456"); ).toBe("discord:direct:123456");
}); });
it("uses Provider when Surface is absent", () => {
expect(
normalizeExplicitSessionKey(
"agent:fina:discord:dm:123456",
makeCtx({
Provider: "Discord",
ChatType: "direct",
SenderId: "123456",
}),
),
).toBe("agent:fina:discord:direct:123456");
});
it("lowercases and passes through unknown providers unchanged", () => { it("lowercases and passes through unknown providers unchanged", () => {
expect( expect(
normalizeExplicitSessionKey( normalizeExplicitSessionKey(

View File

@@ -2,34 +2,49 @@ import type { MsgContext } from "../../auto-reply/templating.js";
import { normalizeExplicitDiscordSessionKey } from "../../discord/session-key-normalization.js"; import { normalizeExplicitDiscordSessionKey } from "../../discord/session-key-normalization.js";
type ExplicitSessionKeyNormalizer = (sessionKey: string, ctx: MsgContext) => string; type ExplicitSessionKeyNormalizer = (sessionKey: string, ctx: MsgContext) => string;
type ExplicitSessionKeyNormalizerEntry = {
const EXPLICIT_SESSION_KEY_NORMALIZERS: Record<string, ExplicitSessionKeyNormalizer> = { provider: string;
discord: normalizeExplicitDiscordSessionKey, normalize: ExplicitSessionKeyNormalizer;
matches: (params: {
sessionKey: string;
provider?: string;
surface?: string;
from: string;
}) => boolean;
}; };
function resolveExplicitSessionKeyProvider( 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, sessionKey: string,
ctx: Pick<MsgContext, "From" | "Provider" | "Surface">, ctx: Pick<MsgContext, "From" | "Provider" | "Surface">,
): string | undefined { ): ExplicitSessionKeyNormalizer | undefined {
const explicitProvider = [ctx.Surface, ctx.Provider] const normalizedProvider = ctx.Provider?.trim().toLowerCase();
.map((entry) => entry?.trim().toLowerCase()) const normalizedSurface = ctx.Surface?.trim().toLowerCase();
.find((entry) => entry && entry in EXPLICIT_SESSION_KEY_NORMALIZERS); const normalizedFrom = (ctx.From ?? "").trim().toLowerCase();
if (explicitProvider) { return EXPLICIT_SESSION_KEY_NORMALIZERS.find((entry) =>
return explicitProvider; entry.matches({
} sessionKey,
provider: normalizedProvider,
const from = (ctx.From ?? "").trim().toLowerCase(); surface: normalizedSurface,
if (from.startsWith("discord:")) { from: normalizedFrom,
return "discord"; }),
} )?.normalize;
if (sessionKey.startsWith("discord:") || sessionKey.includes(":discord:")) {
return "discord";
}
return undefined;
} }
export function normalizeExplicitSessionKey(sessionKey: string, ctx: MsgContext): string { export function normalizeExplicitSessionKey(sessionKey: string, ctx: MsgContext): string {
const normalized = sessionKey.trim().toLowerCase(); const normalized = sessionKey.trim().toLowerCase();
const provider = resolveExplicitSessionKeyProvider(normalized, ctx); const normalize = resolveExplicitSessionKeyNormalizer(normalized, ctx);
return provider ? EXPLICIT_SESSION_KEY_NORMALIZERS[provider](normalized, ctx) : normalized; return normalize ? normalize(normalized, ctx) : normalized;
} }