Matrix: extract inbound route resolution

This commit is contained in:
Gustavo Madeira Santana
2026-03-09 04:28:42 -04:00
parent d2290351e2
commit 6de0456738
2 changed files with 90 additions and 45 deletions

View File

@@ -3,11 +3,8 @@ import {
createTypingCallbacks,
ensureConfiguredAcpRouteReady,
formatAllowlistMatchMeta,
getSessionBindingService,
logInboundDrop,
logTypingFailure,
resolveAgentIdFromSessionKey,
resolveConfiguredAcpRoute,
resolveControlCommandGate,
type PluginRuntime,
type ReplyPayload,
@@ -32,6 +29,7 @@ import { resolveMentions } from "./mentions.js";
import { handleInboundMatrixReaction } from "./reaction-events.js";
import { deliverMatrixReplies } from "./replies.js";
import { resolveMatrixRoomConfig } from "./rooms.js";
import { resolveMatrixInboundRoute } from "./route.js";
import { createMatrixThreadContextResolver } from "./thread-context.js";
import { resolveMatrixThreadRootId, resolveMatrixThreadTarget } from "./threads.js";
import type { MatrixRawEvent, RoomMessageEventContent } from "./types.js";
@@ -512,38 +510,18 @@ export function createMatrixRoomMessageHandler(params: MatrixMonitorHandlerParam
? await resolveThreadContext({ roomId, threadRootId })
: undefined;
const baseRoute = core.channel.routing.resolveAgentRoute({
const { route, configuredBinding } = resolveMatrixInboundRoute({
cfg,
channel: "matrix",
accountId,
peer: {
kind: isDirectMessage ? "direct" : "channel",
id: isDirectMessage ? senderId : roomId,
},
roomId,
senderId,
isDirectMessage,
messageId,
threadRootId,
eventTs: eventTs ?? undefined,
resolveAgentRoute: core.channel.routing.resolveAgentRoute,
});
const bindingConversationId =
threadRootId && threadRootId !== messageId ? threadRootId : roomId;
const bindingParentConversationId = bindingConversationId === roomId ? undefined : roomId;
const sessionBindingService = getSessionBindingService();
const runtimeBinding = sessionBindingService.resolveByConversation({
channel: "matrix",
accountId,
conversationId: bindingConversationId,
parentConversationId: bindingParentConversationId,
});
const configuredRoute =
runtimeBinding == null
? resolveConfiguredAcpRoute({
cfg,
route: baseRoute,
channel: "matrix",
accountId,
conversationId: bindingConversationId,
parentConversationId: bindingParentConversationId,
})
: null;
const configuredBinding = configuredRoute?.configuredBinding ?? null;
if (!runtimeBinding && configuredBinding) {
if (configuredBinding) {
const ensured = await ensureConfiguredAcpRouteReady({
cfg,
configuredBinding,
@@ -558,19 +536,6 @@ export function createMatrixRoomMessageHandler(params: MatrixMonitorHandlerParam
return;
}
}
const boundSessionKey = runtimeBinding?.targetSessionKey?.trim();
const route =
runtimeBinding && boundSessionKey
? {
...baseRoute,
sessionKey: boundSessionKey,
agentId: resolveAgentIdFromSessionKey(boundSessionKey) || baseRoute.agentId,
matchedBy: "binding.channel" as const,
}
: (configuredRoute?.route ?? baseRoute);
if (runtimeBinding) {
sessionBindingService.touch(runtimeBinding.bindingId, eventTs);
}
const envelopeFrom = isDirectMessage ? senderName : (roomName ?? roomId);
const textWithId = `${bodyText}\n[matrix event id: ${messageId} room: ${roomId}]`;
const storePath = core.channel.session.resolveStorePath(cfg.session?.store, {

View File

@@ -0,0 +1,80 @@
import {
getSessionBindingService,
resolveAgentIdFromSessionKey,
resolveConfiguredAcpRoute,
type PluginRuntime,
} from "openclaw/plugin-sdk/matrix";
import type { CoreConfig } from "../../types.js";
type MatrixResolvedRoute = ReturnType<PluginRuntime["channel"]["routing"]["resolveAgentRoute"]>;
export function resolveMatrixInboundRoute(params: {
cfg: CoreConfig;
accountId: string;
roomId: string;
senderId: string;
isDirectMessage: boolean;
messageId: string;
threadRootId?: string;
eventTs?: number;
resolveAgentRoute: PluginRuntime["channel"]["routing"]["resolveAgentRoute"];
}): {
route: MatrixResolvedRoute;
configuredBinding: ReturnType<typeof resolveConfiguredAcpRoute>["configuredBinding"];
} {
const baseRoute = params.resolveAgentRoute({
cfg: params.cfg,
channel: "matrix",
accountId: params.accountId,
peer: {
kind: params.isDirectMessage ? "direct" : "channel",
id: params.isDirectMessage ? params.senderId : params.roomId,
},
});
const bindingConversationId =
params.threadRootId && params.threadRootId !== params.messageId
? params.threadRootId
: params.roomId;
const bindingParentConversationId =
bindingConversationId === params.roomId ? undefined : params.roomId;
const sessionBindingService = getSessionBindingService();
const runtimeBinding = sessionBindingService.resolveByConversation({
channel: "matrix",
accountId: params.accountId,
conversationId: bindingConversationId,
parentConversationId: bindingParentConversationId,
});
const boundSessionKey = runtimeBinding?.targetSessionKey?.trim();
if (runtimeBinding) {
sessionBindingService.touch(runtimeBinding.bindingId, params.eventTs);
}
if (runtimeBinding && boundSessionKey) {
return {
route: {
...baseRoute,
sessionKey: boundSessionKey,
agentId: resolveAgentIdFromSessionKey(boundSessionKey) || baseRoute.agentId,
matchedBy: "binding.channel",
},
configuredBinding: null,
};
}
const configuredRoute =
runtimeBinding == null
? resolveConfiguredAcpRoute({
cfg: params.cfg,
route: baseRoute,
channel: "matrix",
accountId: params.accountId,
conversationId: bindingConversationId,
parentConversationId: bindingParentConversationId,
})
: null;
return {
route: configuredRoute?.route ?? baseRoute,
configuredBinding: configuredRoute?.configuredBinding ?? null,
};
}