Skip to content

Commit d499b14

Browse files
Jarvis DeploySebastianAlmeida
authored andcommitted
feat(routing): add per-account-channel-peer session scope
Adds a new dmScope option that includes accountId in session keys, enabling isolated sessions per channel account for multi-bot setups. - Add 'per-account-channel-peer' to DmScope type - Update session key generation to include accountId - Pass accountId through routing chain - Add tests for new routing behavior (13/13 passing) Closes #3094 Co-authored-by: Sebastian Almeida <[email protected]>
1 parent 93c2d65 commit d499b14

6 files changed

Lines changed: 63 additions & 4 deletions

File tree

‎src/config/types.base.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import type { NormalizedChatType } from "../channels/chat-type.js";
33
export type ReplyMode = "text" | "command";
44
export type TypingMode = "never" | "instant" | "thinking" | "message";
55
export type SessionScope = "per-sender" | "global";
6-
export type DmScope = "main" | "per-peer" | "per-channel-peer";
6+
export type DmScope = "main" | "per-peer" | "per-channel-peer" | "per-account-channel-peer";
77
export type ReplyToMode = "off" | "first" | "all";
88
export type GroupPolicy = "open" | "disabled" | "allowlist";
99
export type DmPolicy = "pairing" | "allowlist" | "open" | "disabled";

‎src/config/zod-schema.session.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,12 @@ export const SessionSchema = z
2020
.object({
2121
scope: z.union([z.literal("per-sender"), z.literal("global")]).optional(),
2222
dmScope: z
23-
.union([z.literal("main"), z.literal("per-peer"), z.literal("per-channel-peer")])
23+
.union([
24+
z.literal("main"),
25+
z.literal("per-peer"),
26+
z.literal("per-channel-peer"),
27+
z.literal("per-account-channel-peer"),
28+
])
2429
.optional(),
2530
identityLinks: z.record(z.string(), z.array(z.string())).optional(),
2631
resetTriggers: z.array(z.string()).optional(),

‎src/infra/outbound/outbound-session.ts‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,11 +103,13 @@ function buildBaseSessionKey(params: {
103103
cfg: MoltbotConfig;
104104
agentId: string;
105105
channel: ChannelId;
106+
accountId?: string | null;
106107
peer: RoutePeer;
107108
}): string {
108109
return buildAgentSessionKey({
109110
agentId: params.agentId,
110111
channel: params.channel,
112+
accountId: params.accountId,
111113
peer: params.peer,
112114
dmScope: params.cfg.session?.dmScope ?? "main",
113115
identityLinks: params.cfg.session?.identityLinks,
@@ -200,6 +202,7 @@ async function resolveSlackSession(
200202
cfg: params.cfg,
201203
agentId: params.agentId,
202204
channel: "slack",
205+
accountId: params.accountId,
203206
peer,
204207
});
205208
const threadId = normalizeThreadId(params.threadId ?? params.replyToId);
@@ -237,6 +240,7 @@ function resolveDiscordSession(
237240
cfg: params.cfg,
238241
agentId: params.agentId,
239242
channel: "discord",
243+
accountId: params.accountId,
240244
peer,
241245
});
242246
const explicitThreadId = normalizeThreadId(params.threadId);
@@ -285,6 +289,7 @@ function resolveTelegramSession(
285289
cfg: params.cfg,
286290
agentId: params.agentId,
287291
channel: "telegram",
292+
accountId: params.accountId,
288293
peer,
289294
});
290295
return {
@@ -312,6 +317,7 @@ function resolveWhatsAppSession(
312317
cfg: params.cfg,
313318
agentId: params.agentId,
314319
channel: "whatsapp",
320+
accountId: params.accountId,
315321
peer,
316322
});
317323
return {
@@ -337,6 +343,7 @@ function resolveSignalSession(
337343
cfg: params.cfg,
338344
agentId: params.agentId,
339345
channel: "signal",
346+
accountId: params.accountId,
340347
peer,
341348
});
342349
return {
@@ -371,6 +378,7 @@ function resolveSignalSession(
371378
cfg: params.cfg,
372379
agentId: params.agentId,
373380
channel: "signal",
381+
accountId: params.accountId,
374382
peer,
375383
});
376384
return {
@@ -395,6 +403,7 @@ function resolveIMessageSession(
395403
cfg: params.cfg,
396404
agentId: params.agentId,
397405
channel: "imessage",
406+
accountId: params.accountId,
398407
peer,
399408
});
400409
return {
@@ -419,6 +428,7 @@ function resolveIMessageSession(
419428
cfg: params.cfg,
420429
agentId: params.agentId,
421430
channel: "imessage",
431+
accountId: params.accountId,
422432
peer,
423433
});
424434
const toPrefix =
@@ -450,6 +460,7 @@ function resolveMatrixSession(
450460
cfg: params.cfg,
451461
agentId: params.agentId,
452462
channel: "matrix",
463+
accountId: params.accountId,
453464
peer,
454465
});
455466
return {
@@ -483,6 +494,7 @@ function resolveMSTeamsSession(
483494
cfg: params.cfg,
484495
agentId: params.agentId,
485496
channel: "msteams",
497+
accountId: params.accountId,
486498
peer,
487499
});
488500
return {
@@ -517,6 +529,7 @@ function resolveMattermostSession(
517529
cfg: params.cfg,
518530
agentId: params.agentId,
519531
channel: "mattermost",
532+
accountId: params.accountId,
520533
peer,
521534
});
522535
const threadId = normalizeThreadId(params.replyToId ?? params.threadId);
@@ -561,6 +574,7 @@ function resolveBlueBubblesSession(
561574
cfg: params.cfg,
562575
agentId: params.agentId,
563576
channel: "bluebubbles",
577+
accountId: params.accountId,
564578
peer,
565579
});
566580
return {
@@ -586,6 +600,7 @@ function resolveNextcloudTalkSession(
586600
cfg: params.cfg,
587601
agentId: params.agentId,
588602
channel: "nextcloud-talk",
603+
accountId: params.accountId,
589604
peer,
590605
});
591606
return {
@@ -612,6 +627,7 @@ function resolveZaloSession(
612627
cfg: params.cfg,
613628
agentId: params.agentId,
614629
channel: "zalo",
630+
accountId: params.accountId,
615631
peer,
616632
});
617633
return {
@@ -639,6 +655,7 @@ function resolveZalouserSession(
639655
cfg: params.cfg,
640656
agentId: params.agentId,
641657
channel: "zalouser",
658+
accountId: params.accountId,
642659
peer,
643660
});
644661
return {
@@ -661,6 +678,7 @@ function resolveNostrSession(
661678
cfg: params.cfg,
662679
agentId: params.agentId,
663680
channel: "nostr",
681+
accountId: params.accountId,
664682
peer,
665683
});
666684
return {
@@ -719,6 +737,7 @@ function resolveTlonSession(
719737
cfg: params.cfg,
720738
agentId: params.agentId,
721739
channel: "tlon",
740+
accountId: params.accountId,
722741
peer,
723742
});
724743
return {

‎src/routing/resolve-route.test.ts‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,3 +227,29 @@ describe("resolveAgentRoute", () => {
227227
expect(route.sessionKey).toBe("agent:home:main");
228228
});
229229
});
230+
231+
test("dmScope=per-account-channel-peer isolates DM sessions per account, channel and sender", () => {
232+
const cfg: MoltbotConfig = {
233+
session: { dmScope: "per-account-channel-peer" },
234+
};
235+
const route = resolveAgentRoute({
236+
cfg,
237+
channel: "telegram",
238+
accountId: "tasks",
239+
peer: { kind: "dm", id: "7550356539" },
240+
});
241+
expect(route.sessionKey).toBe("agent:main:telegram:tasks:dm:7550356539");
242+
});
243+
244+
test("dmScope=per-account-channel-peer uses default accountId when not provided", () => {
245+
const cfg: MoltbotConfig = {
246+
session: { dmScope: "per-account-channel-peer" },
247+
};
248+
const route = resolveAgentRoute({
249+
cfg,
250+
channel: "telegram",
251+
accountId: null,
252+
peer: { kind: "dm", id: "7550356539" },
253+
});
254+
expect(route.sessionKey).toBe("agent:main:telegram:default:dm:7550356539");
255+
});

‎src/routing/resolve-route.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,10 @@ function matchesAccountId(match: string | undefined, actual: string): boolean {
6969
export function buildAgentSessionKey(params: {
7070
agentId: string;
7171
channel: string;
72+
accountId?: string | null;
7273
peer?: RoutePeer | null;
7374
/** DM session scope. */
74-
dmScope?: "main" | "per-peer" | "per-channel-peer";
75+
dmScope?: "main" | "per-peer" | "per-channel-peer" | "per-account-channel-peer";
7576
identityLinks?: Record<string, string[]>;
7677
}): string {
7778
const channel = normalizeToken(params.channel) || "unknown";
@@ -80,6 +81,7 @@ export function buildAgentSessionKey(params: {
8081
agentId: params.agentId,
8182
mainKey: DEFAULT_MAIN_KEY,
8283
channel,
84+
accountId: params.accountId,
8385
peerKind: peer?.kind ?? "dm",
8486
peerId: peer ? normalizeId(peer.id) || "unknown" : null,
8587
dmScope: params.dmScope,
@@ -160,6 +162,7 @@ export function resolveAgentRoute(input: ResolveAgentRouteInput): ResolvedAgentR
160162
const sessionKey = buildAgentSessionKey({
161163
agentId: resolvedAgentId,
162164
channel,
165+
accountId,
163166
peer,
164167
dmScope,
165168
identityLinks,

‎src/routing/session-key.ts‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,11 +111,12 @@ export function buildAgentPeerSessionKey(params: {
111111
agentId: string;
112112
mainKey?: string | undefined;
113113
channel: string;
114+
accountId?: string | null;
114115
peerKind?: "dm" | "group" | "channel" | null;
115116
peerId?: string | null;
116117
identityLinks?: Record<string, string[]>;
117118
/** DM session scope. */
118-
dmScope?: "main" | "per-peer" | "per-channel-peer";
119+
dmScope?: "main" | "per-peer" | "per-channel-peer" | "per-account-channel-peer";
119120
}): string {
120121
const peerKind = params.peerKind ?? "dm";
121122
if (peerKind === "dm") {
@@ -131,6 +132,11 @@ export function buildAgentPeerSessionKey(params: {
131132
});
132133
if (linkedPeerId) peerId = linkedPeerId;
133134
peerId = peerId.toLowerCase();
135+
if (dmScope === "per-account-channel-peer" && peerId) {
136+
const channel = (params.channel ?? "").trim().toLowerCase() || "unknown";
137+
const accountId = normalizeAccountId(params.accountId);
138+
return `agent:${normalizeAgentId(params.agentId)}:${channel}:${accountId}:dm:${peerId}`;
139+
}
134140
if (dmScope === "per-channel-peer" && peerId) {
135141
const channel = (params.channel ?? "").trim().toLowerCase() || "unknown";
136142
return `agent:${normalizeAgentId(params.agentId)}:${channel}:dm:${peerId}`;

0 commit comments

Comments
 (0)