Skip to content

Commit 85b7bc7

Browse files
committed
refactor: remove dock shim and move session routing into plugins
1 parent 5ca26bc commit 85b7bc7

30 files changed

Lines changed: 565 additions & 1332 deletions

‎extensions/discord/src/channel.ts‎

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
import { Separator, TextDisplay } from "@buape/carbon";
2+
import {
3+
buildAgentSessionKey,
4+
resolveThreadSessionKeys,
5+
type RoutePeer,
6+
} from "openclaw/plugin-sdk";
27
import { createScopedChannelConfigBase } from "openclaw/plugin-sdk/compat";
38
import {
49
buildAccountScopedAllowlistConfigEditor,
@@ -196,6 +201,102 @@ function parseDiscordExplicitTarget(raw: string) {
196201
}
197202
}
198203

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+
199300
const discordConfigAccessors = createScopedAccountConfigAccessors({
200301
resolveAccount: ({ cfg, accountId }) => resolveDiscordAccount({ cfg, accountId }),
201302
resolveAllowFrom: (account: ResolvedDiscordAccount) => account.config.dm?.allowFrom,
@@ -341,6 +442,7 @@ export const discordPlugin: ChannelPlugin<ResolvedDiscordAccount> = {
341442
parseExplicitTarget: ({ raw }) => parseDiscordExplicitTarget(raw),
342443
inferTargetChatType: ({ to }) => parseDiscordExplicitTarget(to)?.chatType,
343444
buildCrossContextComponents: buildDiscordCrossContextComponents,
445+
resolveOutboundSessionRoute: (params) => resolveDiscordOutboundSessionRoute(params),
344446
targetResolver: {
345447
looksLikeId: looksLikeDiscordTargetId,
346448
hint: "<channelId|user:ID|channel:ID>",

‎extensions/discord/src/draft-chunking.ts‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { resolveTextChunkLimit } from "../../../src/auto-reply/chunk.js";
2-
import { getChannelDock } from "../../../src/channels/dock.js";
32
import type { OpenClawConfig } from "../../../src/config/config.js";
43
import { resolveAccountEntry } from "../../../src/routing/account-lookup.js";
54
import { normalizeAccountId } from "../../../src/routing/session-key.js";
5+
import { DISCORD_TEXT_CHUNK_LIMIT } from "./outbound-adapter.js";
66

77
const DEFAULT_DISCORD_DRAFT_STREAM_MIN = 200;
88
const DEFAULT_DISCORD_DRAFT_STREAM_MAX = 800;
@@ -15,9 +15,8 @@ export function resolveDiscordDraftStreamingChunking(
1515
maxChars: number;
1616
breakPreference: "paragraph" | "newline" | "sentence";
1717
} {
18-
const providerChunkLimit = getChannelDock("discord")?.outbound?.textChunkLimit;
1918
const textLimit = resolveTextChunkLimit(cfg, "discord", accountId, {
20-
fallbackLimit: providerChunkLimit,
19+
fallbackLimit: DISCORD_TEXT_CHUNK_LIMIT,
2120
});
2221
const normalizedAccountId = normalizeAccountId(accountId);
2322
const accountCfg = resolveAccountEntry(cfg?.channels?.discord?.accounts, normalizedAccountId);

‎extensions/discord/src/outbound-adapter.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ import {
1818
} from "./send.js";
1919
import { buildDiscordInteractiveComponents } from "./shared-interactive.js";
2020

21+
export const DISCORD_TEXT_CHUNK_LIMIT = 2000;
22+
2123
function resolveDiscordOutboundTarget(params: {
2224
to: string;
2325
threadId?: string | number | null;
@@ -86,7 +88,7 @@ async function maybeSendDiscordWebhookText(params: {
8688
export const discordOutbound: ChannelOutboundAdapter = {
8789
deliveryMode: "direct",
8890
chunker: null,
89-
textChunkLimit: 2000,
91+
textChunkLimit: DISCORD_TEXT_CHUNK_LIMIT,
9092
pollMaxOptions: 10,
9193
resolveTarget: ({ to }) => normalizeDiscordOutboundTarget(to),
9294
sendPayload: async (ctx) => {

‎extensions/googlechat/index.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { OpenClawPluginApi } from "openclaw/plugin-sdk/googlechat";
22
import { emptyPluginConfigSchema } from "openclaw/plugin-sdk/googlechat";
3-
import { googlechatDock, googlechatPlugin } from "./src/channel.js";
3+
import { googlechatPlugin } from "./src/channel.js";
44
import { setGoogleChatRuntime } from "./src/runtime.js";
55

66
const plugin = {
@@ -10,7 +10,7 @@ const plugin = {
1010
configSchema: emptyPluginConfigSchema(),
1111
register(api: OpenClawPluginApi) {
1212
setGoogleChatRuntime(api.runtime);
13-
api.registerChannel({ plugin: googlechatPlugin, dock: googlechatDock });
13+
api.registerChannel(googlechatPlugin);
1414
},
1515
};
1616

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import { googlechatDock, googlechatPlugin } from "./src/channel.js";
1+
import { googlechatPlugin } from "./src/channel.js";
22

33
export default {
44
plugin: googlechatPlugin,
5-
dock: googlechatDock,
65
};

‎extensions/googlechat/src/channel.ts‎

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import {
1919
resolveChannelMediaMaxBytes,
2020
resolveGoogleChatGroupRequireMention,
2121
runPassiveAccountLifecycle,
22-
type ChannelDock,
2322
type ChannelMessageActionAdapter,
2423
type ChannelPlugin,
2524
type ChannelStatusIssue,
@@ -94,33 +93,6 @@ const resolveGoogleChatDmPolicy = createScopedDmSecurityResolver<ResolvedGoogleC
9493
normalizeEntry: (raw) => formatAllowFromEntry(raw),
9594
});
9695

97-
export const googlechatDock: ChannelDock = {
98-
id: "googlechat",
99-
capabilities: {
100-
chatTypes: ["direct", "group", "thread"],
101-
reactions: true,
102-
media: true,
103-
threads: true,
104-
blockStreaming: true,
105-
},
106-
outbound: { textChunkLimit: 4000 },
107-
config: googleChatConfigAccessors,
108-
groups: {
109-
resolveRequireMention: resolveGoogleChatGroupRequireMention,
110-
},
111-
threading: {
112-
resolveReplyToMode: ({ cfg }) => cfg.channels?.["googlechat"]?.replyToMode ?? "off",
113-
buildToolContext: ({ context, hasRepliedRef }) => {
114-
const threadId = context.MessageThreadId ?? context.ReplyToId;
115-
return {
116-
currentChannelId: context.To?.trim() || undefined,
117-
currentThreadTs: threadId != null ? String(threadId) : undefined,
118-
hasRepliedRef,
119-
};
120-
},
121-
},
122-
};
123-
12496
const googlechatActions: ChannelMessageActionAdapter = {
12597
listActions: (ctx) => googlechatMessageActions.listActions?.(ctx) ?? [],
12698
extractToolSend: (ctx) => googlechatMessageActions.extractToolSend?.(ctx) ?? null,

‎extensions/imessage/src/channel.ts‎

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { buildAgentSessionKey, type RoutePeer } from "openclaw/plugin-sdk";
12
import {
23
buildAccountScopedAllowlistConfigEditor,
34
buildAccountScopedDmSecurityPolicy,
@@ -30,6 +31,7 @@ import { resolveOutboundSendDep } from "../../../src/infra/outbound/send-deps.js
3031
import { buildPassiveProbedChannelStatusSummary } from "../../shared/channel-status-summary.js";
3132
import { getIMessageRuntime } from "./runtime.js";
3233
import { createIMessageSetupWizardProxy, imessageSetupAdapter } from "./setup-core.js";
34+
import { normalizeIMessageHandle, parseIMessageTarget } from "./targets.js";
3335

3436
const meta = getChatChannelMeta("imessage");
3537

@@ -75,6 +77,83 @@ async function sendIMessageOutbound(params: {
7577
});
7678
}
7779

80+
function buildIMessageBaseSessionKey(params: {
81+
cfg: Parameters<typeof resolveIMessageAccount>[0]["cfg"];
82+
agentId: string;
83+
accountId?: string | null;
84+
peer: RoutePeer;
85+
}) {
86+
return buildAgentSessionKey({
87+
agentId: params.agentId,
88+
channel: "imessage",
89+
accountId: params.accountId,
90+
peer: params.peer,
91+
dmScope: params.cfg.session?.dmScope ?? "main",
92+
identityLinks: params.cfg.session?.identityLinks,
93+
});
94+
}
95+
96+
function resolveIMessageOutboundSessionRoute(params: {
97+
cfg: Parameters<typeof resolveIMessageAccount>[0]["cfg"];
98+
agentId: string;
99+
accountId?: string | null;
100+
target: string;
101+
}) {
102+
const parsed = parseIMessageTarget(params.target);
103+
if (parsed.kind === "handle") {
104+
const handle = normalizeIMessageHandle(parsed.to);
105+
if (!handle) {
106+
return null;
107+
}
108+
const peer: RoutePeer = { kind: "direct", id: handle };
109+
const baseSessionKey = buildIMessageBaseSessionKey({
110+
cfg: params.cfg,
111+
agentId: params.agentId,
112+
accountId: params.accountId,
113+
peer,
114+
});
115+
return {
116+
sessionKey: baseSessionKey,
117+
baseSessionKey,
118+
peer,
119+
chatType: "direct" as const,
120+
from: `imessage:${handle}`,
121+
to: `imessage:${handle}`,
122+
};
123+
}
124+
125+
const peerId =
126+
parsed.kind === "chat_id"
127+
? String(parsed.chatId)
128+
: parsed.kind === "chat_guid"
129+
? parsed.chatGuid
130+
: parsed.chatIdentifier;
131+
if (!peerId) {
132+
return null;
133+
}
134+
const peer: RoutePeer = { kind: "group", id: peerId };
135+
const baseSessionKey = buildIMessageBaseSessionKey({
136+
cfg: params.cfg,
137+
agentId: params.agentId,
138+
accountId: params.accountId,
139+
peer,
140+
});
141+
const toPrefix =
142+
parsed.kind === "chat_id"
143+
? "chat_id"
144+
: parsed.kind === "chat_guid"
145+
? "chat_guid"
146+
: "chat_identifier";
147+
return {
148+
sessionKey: baseSessionKey,
149+
baseSessionKey,
150+
peer,
151+
chatType: "group" as const,
152+
from: `imessage:group:${peerId}`,
153+
to: `${toPrefix}:${peerId}`,
154+
};
155+
}
156+
78157
export const imessagePlugin: ChannelPlugin<ResolvedIMessageAccount> = {
79158
id: "imessage",
80159
meta: {
@@ -176,6 +255,7 @@ export const imessagePlugin: ChannelPlugin<ResolvedIMessageAccount> = {
176255
},
177256
messaging: {
178257
normalizeTarget: normalizeIMessageMessagingTarget,
258+
resolveOutboundSessionRoute: (params) => resolveIMessageOutboundSessionRoute(params),
179259
targetResolver: {
180260
looksLikeId: looksLikeIMessageTargetId,
181261
hint: "<handle|chat_id:ID>",

0 commit comments

Comments
 (0)