Mattermost: move outbound session routing behind plugin boundary
This commit is contained in:
parent
028f3c4d15
commit
d6c13d9dc0
@ -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>",
|
||||
|
||||
52
extensions/mattermost/src/session-route.ts
Normal file
52
extensions/mattermost/src/session-route.ts
Normal 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 } : {}),
|
||||
};
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user