refactor(routing): centralize inbound last-route policy

This commit is contained in:
Peter Steinberger
2026-03-08 02:02:00 +00:00
parent b2f8f5e4dd
commit 6a8081a7f3
10 changed files with 172 additions and 9 deletions

View File

@@ -1,6 +1,6 @@
import type { loadConfig } from "../../../config/config.js";
import type { resolveAgentRoute } from "../../../routing/resolve-route.js";
import { buildAgentSessionKey } from "../../../routing/resolve-route.js";
import { buildAgentSessionKey, deriveLastRoutePolicy } from "../../../routing/resolve-route.js";
import {
buildAgentMainSessionKey,
DEFAULT_MAIN_KEY,
@@ -70,6 +70,23 @@ export async function maybeBroadcastMessage(params: {
agentId: normalizedAgentId,
mainKey: DEFAULT_MAIN_KEY,
}),
lastRoutePolicy: deriveLastRoutePolicy({
sessionKey: buildAgentSessionKey({
agentId: normalizedAgentId,
channel: "whatsapp",
accountId: params.route.accountId,
peer: {
kind: params.msg.chatType === "group" ? "group" : "direct",
id: params.peerId,
},
dmScope: params.cfg.session?.dmScope,
identityLinks: params.cfg.session?.identityLinks,
}),
mainSessionKey: buildAgentMainSessionKey({
agentId: normalizedAgentId,
mainKey: DEFAULT_MAIN_KEY,
}),
}),
};
try {

View File

@@ -19,7 +19,10 @@ import { recordSessionMetaFromInbound } from "../../../config/sessions.js";
import { logVerbose, shouldLogVerbose } from "../../../globals.js";
import type { getChildLogger } from "../../../logging.js";
import { getAgentScopedMediaLocalRoots } from "../../../media/local-roots.js";
import type { resolveAgentRoute } from "../../../routing/resolve-route.js";
import {
resolveInboundLastRouteSessionKey,
type resolveAgentRoute,
} from "../../../routing/resolve-route.js";
import {
readStoreAllowFromForDmPolicy,
resolvePinnedMainDmOwnerFromAllowlist,
@@ -339,9 +342,13 @@ export async function processMessage(params: {
});
const shouldUpdateMainLastRoute =
!pinnedMainDmRecipient || pinnedMainDmRecipient === dmRouteTarget;
const inboundLastRouteSessionKey = resolveInboundLastRouteSessionKey({
route: params.route,
sessionKey: params.route.sessionKey,
});
if (
dmRouteTarget &&
params.route.sessionKey === params.route.mainSessionKey &&
inboundLastRouteSessionKey === params.route.mainSessionKey &&
shouldUpdateMainLastRoute
) {
updateLastRouteInBackground({
@@ -357,7 +364,7 @@ export async function processMessage(params: {
});
} else if (
dmRouteTarget &&
params.route.sessionKey === params.route.mainSessionKey &&
inboundLastRouteSessionKey === params.route.mainSessionKey &&
pinnedMainDmRecipient
) {
logVerbose(