refactor(discord): extract session key normalization

This commit is contained in:
Peter Steinberger
2026-03-08 01:11:05 +00:00
parent c1d07b09ce
commit 74e3c071b2
3 changed files with 63 additions and 19 deletions

View File

@@ -1,5 +1,5 @@
import type { MsgContext } from "../../auto-reply/templating.js";
import { normalizeChatType } from "../../channels/chat-type.js";
import { normalizeExplicitDiscordSessionKey } from "../../discord/session-key-normalization.js";
import {
buildAgentMainSessionKey,
DEFAULT_AGENT_ID,
@@ -29,24 +29,7 @@ export function deriveSessionKey(scope: SessionScope, ctx: MsgContext) {
export function resolveSessionKey(scope: SessionScope, ctx: MsgContext, mainKey?: string) {
const explicit = ctx.SessionKey?.trim();
if (explicit) {
let normalized = explicit.toLowerCase();
if (normalizeChatType(ctx.ChatType) === "direct") {
normalized = normalized.replace(/^(agent:[^:]+:discord:)dm:/, "$1direct:");
const match = normalized.match(/^((?:agent:[^:]+:)?)discord:channel:([^:]+)$/);
if (match) {
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;
if (directId && directId === match[2]) {
normalized = `${match[1]}discord:direct:${match[2]}`;
}
}
}
return normalized;
return normalizeExplicitDiscordSessionKey(explicit, ctx);
}
const raw = deriveSessionKey(scope, ctx);
if (scope === "global") {