Files
openclaw/extensions/mattermost/src/session-route.ts
2026-04-22 02:56:23 +01:00

51 lines
1.7 KiB
TypeScript

import {
buildChannelOutboundSessionRoute,
buildThreadAwareOutboundSessionRoute,
stripChannelTargetPrefix,
stripTargetKindPrefix,
type ChannelOutboundSessionRouteParams,
} from "openclaw/plugin-sdk/core";
import { normalizeLowercaseStringOrEmpty } from "openclaw/plugin-sdk/text-runtime";
export function resolveMattermostOutboundSessionRoute(params: ChannelOutboundSessionRouteParams) {
let trimmed = stripChannelTargetPrefix(params.target, "mattermost");
if (!trimmed) {
return null;
}
const lower = normalizeLowercaseStringOrEmpty(trimmed);
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}`,
});
return buildThreadAwareOutboundSessionRoute({
route: baseRoute,
replyToId: params.replyToId,
threadId: params.threadId,
currentSessionKey: params.currentSessionKey,
canRecoverCurrentThread: ({ route }) =>
route.chatType !== "direct" || (params.cfg.session?.dmScope ?? "main") !== "main",
});
}