|
1 | 1 | import { Separator, TextDisplay } from "@buape/carbon"; |
| 2 | +import { |
| 3 | + buildAgentSessionKey, |
| 4 | + resolveThreadSessionKeys, |
| 5 | + type RoutePeer, |
| 6 | +} from "openclaw/plugin-sdk"; |
2 | 7 | import { createScopedChannelConfigBase } from "openclaw/plugin-sdk/compat"; |
3 | 8 | import { |
4 | 9 | buildAccountScopedAllowlistConfigEditor, |
@@ -196,6 +201,102 @@ function parseDiscordExplicitTarget(raw: string) { |
196 | 201 | } |
197 | 202 | } |
198 | 203 |
|
| 204 | +function normalizeOutboundThreadId(value?: string | number | null): string | undefined { |
| 205 | + if (value == null) { |
| 206 | + return undefined; |
| 207 | + } |
| 208 | + if (typeof value === "number") { |
| 209 | + if (!Number.isFinite(value)) { |
| 210 | + return undefined; |
| 211 | + } |
| 212 | + return String(Math.trunc(value)); |
| 213 | + } |
| 214 | + const trimmed = value.trim(); |
| 215 | + return trimmed ? trimmed : undefined; |
| 216 | +} |
| 217 | + |
| 218 | +function buildDiscordBaseSessionKey(params: { |
| 219 | + cfg: OpenClawConfig; |
| 220 | + agentId: string; |
| 221 | + accountId?: string | null; |
| 222 | + peer: RoutePeer; |
| 223 | +}) { |
| 224 | + return buildAgentSessionKey({ |
| 225 | + agentId: params.agentId, |
| 226 | + channel: "discord", |
| 227 | + accountId: params.accountId, |
| 228 | + peer: params.peer, |
| 229 | + dmScope: params.cfg.session?.dmScope ?? "main", |
| 230 | + identityLinks: params.cfg.session?.identityLinks, |
| 231 | + }); |
| 232 | +} |
| 233 | + |
| 234 | +function resolveDiscordOutboundTargetKindHint(params: { |
| 235 | + target: string; |
| 236 | + resolvedTarget?: { kind: string }; |
| 237 | +}): "user" | "channel" | undefined { |
| 238 | + const resolvedKind = params.resolvedTarget?.kind; |
| 239 | + if (resolvedKind === "user") { |
| 240 | + return "user"; |
| 241 | + } |
| 242 | + if (resolvedKind === "group" || resolvedKind === "channel") { |
| 243 | + return "channel"; |
| 244 | + } |
| 245 | + |
| 246 | + const target = params.target.trim(); |
| 247 | + if (/^channel:/i.test(target)) { |
| 248 | + return "channel"; |
| 249 | + } |
| 250 | + if (/^(user:|discord:|@|<@!?)/i.test(target)) { |
| 251 | + return "user"; |
| 252 | + } |
| 253 | + return undefined; |
| 254 | +} |
| 255 | + |
| 256 | +function resolveDiscordOutboundSessionRoute(params: { |
| 257 | + cfg: OpenClawConfig; |
| 258 | + agentId: string; |
| 259 | + accountId?: string | null; |
| 260 | + target: string; |
| 261 | + resolvedTarget?: { kind: string }; |
| 262 | + replyToId?: string | null; |
| 263 | + threadId?: string | number | null; |
| 264 | +}) { |
| 265 | + const parsed = parseDiscordTarget(params.target, { |
| 266 | + defaultKind: resolveDiscordOutboundTargetKindHint(params), |
| 267 | + }); |
| 268 | + if (!parsed) { |
| 269 | + return null; |
| 270 | + } |
| 271 | + const isDm = parsed.kind === "user"; |
| 272 | + const peer: RoutePeer = { |
| 273 | + kind: isDm ? "direct" : "channel", |
| 274 | + id: parsed.id, |
| 275 | + }; |
| 276 | + const baseSessionKey = buildDiscordBaseSessionKey({ |
| 277 | + cfg: params.cfg, |
| 278 | + agentId: params.agentId, |
| 279 | + accountId: params.accountId, |
| 280 | + peer, |
| 281 | + }); |
| 282 | + const explicitThreadId = normalizeOutboundThreadId(params.threadId); |
| 283 | + const threadCandidate = explicitThreadId ?? normalizeOutboundThreadId(params.replyToId); |
| 284 | + const threadKeys = resolveThreadSessionKeys({ |
| 285 | + baseSessionKey, |
| 286 | + threadId: threadCandidate, |
| 287 | + useSuffix: false, |
| 288 | + }); |
| 289 | + return { |
| 290 | + sessionKey: threadKeys.sessionKey, |
| 291 | + baseSessionKey, |
| 292 | + peer, |
| 293 | + chatType: isDm ? ("direct" as const) : ("channel" as const), |
| 294 | + from: isDm ? `discord:${parsed.id}` : `discord:channel:${parsed.id}`, |
| 295 | + to: isDm ? `user:${parsed.id}` : `channel:${parsed.id}`, |
| 296 | + threadId: explicitThreadId ?? undefined, |
| 297 | + }; |
| 298 | +} |
| 299 | + |
199 | 300 | const discordConfigAccessors = createScopedAccountConfigAccessors({ |
200 | 301 | resolveAccount: ({ cfg, accountId }) => resolveDiscordAccount({ cfg, accountId }), |
201 | 302 | resolveAllowFrom: (account: ResolvedDiscordAccount) => account.config.dm?.allowFrom, |
@@ -341,6 +442,7 @@ export const discordPlugin: ChannelPlugin<ResolvedDiscordAccount> = { |
341 | 442 | parseExplicitTarget: ({ raw }) => parseDiscordExplicitTarget(raw), |
342 | 443 | inferTargetChatType: ({ to }) => parseDiscordExplicitTarget(to)?.chatType, |
343 | 444 | buildCrossContextComponents: buildDiscordCrossContextComponents, |
| 445 | + resolveOutboundSessionRoute: (params) => resolveDiscordOutboundSessionRoute(params), |
344 | 446 | targetResolver: { |
345 | 447 | looksLikeId: looksLikeDiscordTargetId, |
346 | 448 | hint: "<channelId|user:ID|channel:ID>", |
|
0 commit comments