diff --git a/src/sessions/send-policy.ts b/src/sessions/send-policy.ts index 6f635c1ae..54cc1ef77 100644 --- a/src/sessions/send-policy.ts +++ b/src/sessions/send-policy.ts @@ -20,11 +20,24 @@ function normalizeMatchValue(raw?: string | null) { return value ? value : undefined; } -function deriveChannelFromKey(key?: string) { +function stripAgentSessionKeyPrefix(key?: string): string | undefined { if (!key) { return undefined; } const parts = key.split(":").filter(Boolean); + // Canonical agent session keys: agent:: + if (parts.length >= 3 && parts[0] === "agent") { + return parts.slice(2).join(":"); + } + return key; +} + +function deriveChannelFromKey(key?: string) { + const normalizedKey = stripAgentSessionKeyPrefix(key); + if (!normalizedKey) { + return undefined; + } + const parts = normalizedKey.split(":").filter(Boolean); if (parts.length >= 3 && (parts[1] === "group" || parts[1] === "channel")) { return normalizeMatchValue(parts[0]); } @@ -32,13 +45,14 @@ function deriveChannelFromKey(key?: string) { } function deriveChatTypeFromKey(key?: string): SessionChatType | undefined { - if (!key) { + const normalizedKey = stripAgentSessionKeyPrefix(key); + if (!normalizedKey) { return undefined; } - if (key.includes(":group:")) { + if (normalizedKey.includes(":group:")) { return "group"; } - if (key.includes(":channel:")) { + if (normalizedKey.includes(":channel:")) { return "channel"; } return undefined; @@ -69,7 +83,8 @@ export function resolveSendPolicy(params: { const chatType = normalizeChatType(params.chatType ?? params.entry?.chatType) ?? normalizeChatType(deriveChatTypeFromKey(params.sessionKey)); - const sessionKey = params.sessionKey ?? ""; + const rawSessionKey = params.sessionKey ?? ""; + const strippedSessionKey = stripAgentSessionKeyPrefix(rawSessionKey) ?? ""; let allowedMatch = false; for (const rule of policy.rules ?? []) { @@ -88,7 +103,11 @@ export function resolveSendPolicy(params: { if (matchChatType && matchChatType !== chatType) { continue; } - if (matchPrefix && !sessionKey.startsWith(matchPrefix)) { + if ( + matchPrefix && + !rawSessionKey.startsWith(matchPrefix) && + !strippedSessionKey.startsWith(matchPrefix) + ) { continue; } if (action === "deny") {