Files
openclaw/extensions/discord/src/session-key-normalization.ts
Vincent Koc 041f0b87ec perf(inbound): trim cold startup import graph (#52082)
* perf(inbound): trim cold startup import graph

* chore(reply): drop redundant inline action type import

* fix(inbound): restore warning and maintenance seams

* fix(reply): restore type seam and secure forked transcripts
2026-03-21 22:32:21 -07:00

46 lines
1.4 KiB
TypeScript

type DiscordSessionKeyContext = {
ChatType?: string;
From?: string;
SenderId?: string;
};
function normalizeDiscordChatType(raw?: string): "direct" | "group" | "channel" | undefined {
const normalized = (raw ?? "").trim().toLowerCase();
if (!normalized) {
return undefined;
}
if (normalized === "dm") {
return "direct";
}
if (normalized === "group" || normalized === "channel" || normalized === "direct") {
return normalized;
}
return undefined;
}
export function normalizeExplicitDiscordSessionKey(
sessionKey: string,
ctx: DiscordSessionKeyContext,
): string {
let normalized = sessionKey.trim().toLowerCase();
if (normalizeDiscordChatType(ctx.ChatType) !== "direct") {
return normalized;
}
normalized = normalized.replace(/^(discord:)dm:/, "$1direct:");
normalized = normalized.replace(/^(agent:[^:]+:discord:)dm:/, "$1direct:");
const match = normalized.match(/^((?:agent:[^:]+:)?)discord:channel:([^:]+)$/);
if (!match) {
return normalized;
}
const from = (ctx.From ?? "").trim().toLowerCase();
const senderId = (ctx.SenderId ?? "").trim().toLowerCase();
const fromDiscordId =
from.startsWith("discord:") && !from.includes(":channel:") && !from.includes(":group:")
? from.slice("discord:".length)
: "";
const directId = senderId || fromDiscordId;
return directId && directId === match[2] ? `${match[1]}discord:direct:${match[2]}` : normalized;
}