Mattermost: move outbound session routing behind plugin boundary

This commit is contained in:
Gustavo Madeira Santana 2026-03-18 04:03:28 +00:00
parent 028f3c4d15
commit d6c13d9dc0
No known key found for this signature in database
2 changed files with 54 additions and 0 deletions

View File

@ -39,6 +39,7 @@ import {
type ChannelPlugin,
} from "./runtime-api.js";
import { getMattermostRuntime } from "./runtime.js";
import { resolveMattermostOutboundSessionRoute } from "./session-route.js";
import { mattermostSetupAdapter } from "./setup-core.js";
import { mattermostSetupWizard } from "./setup-surface.js";
@ -348,6 +349,7 @@ export const mattermostPlugin: ChannelPlugin<ResolvedMattermostAccount> = {
},
messaging: {
normalizeTarget: normalizeMattermostMessagingTarget,
resolveOutboundSessionRoute: (params) => resolveMattermostOutboundSessionRoute(params),
targetResolver: {
looksLikeId: looksLikeMattermostTargetId,
hint: "<channelId|user:ID|channel:ID>",

View File

@ -0,0 +1,52 @@
import {
buildChannelOutboundSessionRoute,
normalizeOutboundThreadId,
resolveThreadSessionKeys,
stripChannelTargetPrefix,
stripTargetKindPrefix,
type ChannelOutboundSessionRouteParams,
} from "openclaw/plugin-sdk/core";
export function resolveMattermostOutboundSessionRoute(params: ChannelOutboundSessionRouteParams) {
let trimmed = stripChannelTargetPrefix(params.target, "mattermost");
if (!trimmed) {
return null;
}
const lower = trimmed.toLowerCase();
const resolvedKind = params.resolvedTarget?.kind;
const isUser =
resolvedKind === "user" ||
(resolvedKind !== "channel" &&
resolvedKind !== "group" &&
(lower.startsWith("user:") || trimmed.startsWith("@")));
if (trimmed.startsWith("@")) {
trimmed = trimmed.slice(1).trim();
}
const rawId = stripTargetKindPrefix(trimmed);
if (!rawId) {
return null;
}
const baseRoute = buildChannelOutboundSessionRoute({
cfg: params.cfg,
agentId: params.agentId,
channel: "mattermost",
accountId: params.accountId,
peer: {
kind: isUser ? "direct" : "channel",
id: rawId,
},
chatType: isUser ? "direct" : "channel",
from: isUser ? `mattermost:${rawId}` : `mattermost:channel:${rawId}`,
to: isUser ? `user:${rawId}` : `channel:${rawId}`,
});
const threadId = normalizeOutboundThreadId(params.replyToId ?? params.threadId);
const threadKeys = resolveThreadSessionKeys({
baseSessionKey: baseRoute.baseSessionKey,
threadId,
});
return {
...baseRoute,
sessionKey: threadKeys.sessionKey,
...(threadId !== undefined ? { threadId } : {}),
};
}