refactor: prune legacy group prefixes

This commit is contained in:
Peter Steinberger
2026-01-17 08:46:19 +00:00
parent ab49fe0e92
commit 13b931c006
44 changed files with 160 additions and 179 deletions

View File

@@ -220,7 +220,7 @@ describe("resolveHeartbeatDeliveryTarget", () => {
const entry = {
...baseEntry,
lastChannel: "whatsapp" as const,
lastTo: "whatsapp:group:120363401234567890@G.US",
lastTo: "whatsapp:120363401234567890@G.US",
};
expect(resolveHeartbeatDeliveryTarget({ cfg, entry })).toEqual({
channel: "whatsapp",

View File

@@ -106,7 +106,7 @@ describe("runMessageAction context isolation", () => {
action: "send",
params: {
channel: "whatsapp",
target: "group:123@g.us",
target: "123@g.us",
message: "hi",
},
toolContext: { currentChannelId: "123@g.us" },

View File

@@ -32,7 +32,7 @@ describe("resolveOutboundTarget", () => {
name: "normalizes prefixed/uppercase whatsapp group targets",
input: {
channel: "whatsapp" as const,
to: " WhatsApp:Group:120363401234567890@G.US ",
to: " WhatsApp:120363401234567890@G.US ",
},
expected: { ok: true as const, to: "120363401234567890@g.us" },
},

View File

@@ -61,7 +61,15 @@ function isSurfaceGroupKey(key: string): boolean {
}
function isLegacyGroupKey(key: string): boolean {
return key.startsWith("group:") || key.includes("@g.us");
const trimmed = key.trim();
if (!trimmed) return false;
if (trimmed.startsWith("group:")) return true;
const lower = trimmed.toLowerCase();
if (!lower.includes("@g.us")) return false;
// Legacy WhatsApp group keys: bare JID or "whatsapp:<jid>" without explicit ":group:" kind.
if (!trimmed.includes(":")) return true;
if (lower.startsWith("whatsapp:") && !trimmed.includes(":group:")) return true;
return false;
}
function normalizeSessionKeyForAgent(key: string, agentId: string): string {
@@ -72,6 +80,22 @@ function normalizeSessionKeyForAgent(key: string, agentId: string): string {
const rest = raw.slice("subagent:".length);
return `agent:${normalizeAgentId(agentId)}:subagent:${rest}`;
}
if (raw.startsWith("group:")) {
const id = raw.slice("group:".length).trim();
if (!id) return raw;
const channel = id.toLowerCase().includes("@g.us") ? "whatsapp" : "unknown";
return `agent:${normalizeAgentId(agentId)}:${channel}:group:${id}`;
}
if (!raw.includes(":") && raw.toLowerCase().includes("@g.us")) {
return `agent:${normalizeAgentId(agentId)}:whatsapp:group:${raw}`;
}
if (raw.toLowerCase().startsWith("whatsapp:") && raw.toLowerCase().includes("@g.us")) {
const remainder = raw.slice("whatsapp:".length).trim();
const cleaned = remainder.replace(/^group:/i, "").trim();
if (cleaned && !isSurfaceGroupKey(raw)) {
return `agent:${normalizeAgentId(agentId)}:whatsapp:group:${cleaned}`;
}
}
if (isSurfaceGroupKey(raw)) {
return `agent:${normalizeAgentId(agentId)}:${raw}`;
}