Skip to content

Commit a32c7e1

Browse files
committed
Plugin SDK: normalize and harden message action discovery
1 parent df284fe commit a32c7e1

7 files changed

Lines changed: 186 additions & 84 deletions

File tree

‎src/agents/channel-tools.test.ts‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,4 +84,31 @@ describe("channel tools", () => {
8484
expect(listChannelSupportedActions({ cfg, channel: "polltest" })).toEqual([]);
8585
expect(listAllChannelSupportedActions({ cfg })).toEqual([]);
8686
});
87+
88+
it("normalizes channel aliases before listing supported actions", () => {
89+
const plugin: ChannelPlugin = {
90+
id: "telegram",
91+
meta: {
92+
id: "telegram",
93+
label: "Telegram",
94+
selectionLabel: "Telegram",
95+
docsPath: "/channels/telegram",
96+
blurb: "telegram plugin",
97+
aliases: ["tg"],
98+
},
99+
capabilities: { chatTypes: ["direct"] },
100+
config: {
101+
listAccountIds: () => [],
102+
resolveAccount: () => ({}),
103+
},
104+
actions: {
105+
listActions: () => ["react"],
106+
},
107+
};
108+
109+
setActivePluginRegistry(createTestRegistry([{ pluginId: "telegram", source: "test", plugin }]));
110+
111+
const cfg = {} as OpenClawConfig;
112+
expect(listChannelSupportedActions({ cfg, channel: "tg" })).toEqual(["react"]);
113+
});
87114
});

‎src/agents/channel-tools.ts‎

Lines changed: 15 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
import { getChannelPlugin, listChannelPlugins } from "../channels/plugins/index.js";
2+
import {
3+
createMessageActionDiscoveryContext,
4+
resolveMessageActionDiscoveryChannelId,
5+
} from "../channels/plugins/message-action-discovery.js";
26
import type {
37
ChannelAgentTool,
48
ChannelMessageActionName,
@@ -24,26 +28,15 @@ export function listChannelSupportedActions(params: {
2428
agentId?: string | null;
2529
requesterSenderId?: string | null;
2630
}): ChannelMessageActionName[] {
27-
if (!params.channel) {
31+
const channelId = resolveMessageActionDiscoveryChannelId(params.channel);
32+
if (!channelId) {
2833
return [];
2934
}
30-
const plugin = getChannelPlugin(params.channel as Parameters<typeof getChannelPlugin>[0]);
35+
const plugin = getChannelPlugin(channelId as Parameters<typeof getChannelPlugin>[0]);
3136
if (!plugin?.actions?.listActions) {
3237
return [];
3338
}
34-
const cfg = params.cfg ?? ({} as OpenClawConfig);
35-
return runPluginListActions(plugin, {
36-
cfg,
37-
currentChannelId: params.currentChannelId,
38-
currentChannelProvider: params.channel,
39-
currentThreadTs: params.currentThreadTs,
40-
currentMessageId: params.currentMessageId,
41-
accountId: params.accountId,
42-
sessionKey: params.sessionKey,
43-
sessionId: params.sessionId,
44-
agentId: params.agentId,
45-
requesterSenderId: params.requesterSenderId,
46-
});
39+
return runPluginListActions(plugin, createMessageActionDiscoveryContext(params));
4740
}
4841

4942
/**
@@ -65,19 +58,13 @@ export function listAllChannelSupportedActions(params: {
6558
if (!plugin.actions?.listActions) {
6659
continue;
6760
}
68-
const cfg = params.cfg ?? ({} as OpenClawConfig);
69-
const channelActions = runPluginListActions(plugin, {
70-
cfg,
71-
currentChannelId: params.currentChannelId,
72-
currentChannelProvider: plugin.id,
73-
currentThreadTs: params.currentThreadTs,
74-
currentMessageId: params.currentMessageId,
75-
accountId: params.accountId,
76-
sessionKey: params.sessionKey,
77-
sessionId: params.sessionId,
78-
agentId: params.agentId,
79-
requesterSenderId: params.requesterSenderId,
80-
});
61+
const channelActions = runPluginListActions(
62+
plugin,
63+
createMessageActionDiscoveryContext({
64+
...params,
65+
currentChannelProvider: plugin.id,
66+
}),
67+
);
8168
for (const action of channelActions) {
8269
actions.add(action);
8370
}

‎src/agents/tools/message-tool.test.ts‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ function createChannelPlugin(params: {
8686
label: string;
8787
docsPath: string;
8888
blurb: string;
89+
aliases?: string[];
8990
actions?: ChannelMessageActionName[];
9091
listActions?: NonNullable<NonNullable<ChannelPlugin["actions"]>["listActions"]>;
9192
capabilities?: readonly ChannelMessageCapability[];
@@ -101,6 +102,7 @@ function createChannelPlugin(params: {
101102
selectionLabel: params.label,
102103
docsPath: params.docsPath,
103104
blurb: params.blurb,
105+
aliases: params.aliases,
104106
},
105107
capabilities: { chatTypes: ["direct", "group"], media: true },
106108
config: {
@@ -641,6 +643,28 @@ describe("message tool description", () => {
641643
expect(tool.description).toContain("telegram (delete, edit, react, send, topic-create)");
642644
});
643645

646+
it("normalizes channel aliases before building the current channel description", () => {
647+
const signalPlugin = createChannelPlugin({
648+
id: "signal",
649+
label: "Signal",
650+
docsPath: "/channels/signal",
651+
blurb: "Signal test plugin.",
652+
aliases: ["sig"],
653+
actions: ["send", "react"],
654+
});
655+
656+
setActivePluginRegistry(
657+
createTestRegistry([{ pluginId: "signal", source: "test", plugin: signalPlugin }]),
658+
);
659+
660+
const tool = createMessageTool({
661+
config: {} as never,
662+
currentChannelProvider: "sig",
663+
});
664+
665+
expect(tool.description).toContain("Current channel (signal) supports: react, send.");
666+
});
667+
644668
it("does not include 'Other configured channels' when only one channel is configured", () => {
645669
setActivePluginRegistry(
646670
createTestRegistry([{ pluginId: "bluebubbles", source: "test", plugin: bluebubblesPlugin }]),

‎src/agents/tools/message-tool.ts‎

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Type } from "@sinclair/typebox";
1+
import { Type, type TSchema } from "@sinclair/typebox";
22
import { listChannelPlugins } from "../../channels/plugins/index.js";
33
import {
44
channelSupportsMessageCapability,
@@ -82,7 +82,7 @@ const interactiveMessageSchema = Type.Object(
8282
);
8383

8484
function buildSendSchema(options: { includeInteractive: boolean }) {
85-
const props: Record<string, unknown> = {
85+
const props: Record<string, TSchema> = {
8686
message: Type.Optional(Type.String()),
8787
effectId: Type.Optional(
8888
Type.String({
@@ -167,7 +167,7 @@ function buildFetchSchema() {
167167
}
168168

169169
function buildPollSchema() {
170-
const props: Record<string, unknown> = {
170+
const props: Record<string, TSchema> = {
171171
pollId: Type.Optional(Type.String()),
172172
pollOptionId: Type.Optional(
173173
Type.String({
@@ -346,7 +346,7 @@ function buildChannelManagementSchema() {
346346

347347
function buildMessageToolSchemaProps(options: {
348348
includeInteractive: boolean;
349-
extraProperties?: Record<string, unknown>;
349+
extraProperties?: Record<string, TSchema>;
350350
}) {
351351
return {
352352
...buildRoutingSchema(),
@@ -370,7 +370,7 @@ function buildMessageToolSchemaFromActions(
370370
actions: readonly string[],
371371
options: {
372372
includeInteractive: boolean;
373-
extraProperties?: Record<string, unknown>;
373+
extraProperties?: Record<string, TSchema>;
374374
},
375375
) {
376376
const props = buildMessageToolSchemaProps(options);
@@ -547,34 +547,39 @@ function buildMessageToolDescription(options?: {
547547
requesterSenderId?: string;
548548
}): string {
549549
const baseDescription = "Send, delete, and manage messages via channel plugins.";
550+
const resolvedOptions = options ?? {};
551+
const currentChannel = normalizeMessageChannel(resolvedOptions.currentChannel);
550552

551553
// If we have a current channel, show its actions and list other configured channels
552-
if (options?.currentChannel) {
554+
if (currentChannel) {
553555
const channelActions = listChannelSupportedActions({
554-
cfg: options.config,
555-
channel: options.currentChannel,
556-
currentChannelId: options.currentChannelId,
557-
currentThreadTs: options.currentThreadTs,
558-
currentMessageId: options.currentMessageId,
559-
accountId: options.currentAccountId,
560-
sessionKey: options.sessionKey,
561-
sessionId: options.sessionId,
562-
agentId: options.agentId,
563-
requesterSenderId: options.requesterSenderId,
556+
cfg: resolvedOptions.config,
557+
channel: currentChannel,
558+
currentChannelId: resolvedOptions.currentChannelId,
559+
currentThreadTs: resolvedOptions.currentThreadTs,
560+
currentMessageId: resolvedOptions.currentMessageId,
561+
accountId: resolvedOptions.currentAccountId,
562+
sessionKey: resolvedOptions.sessionKey,
563+
sessionId: resolvedOptions.sessionId,
564+
agentId: resolvedOptions.agentId,
565+
requesterSenderId: resolvedOptions.requesterSenderId,
564566
});
565567
if (channelActions.length > 0) {
566568
// Always include "send" as a base action
567569
const allActions = new Set(["send", ...channelActions]);
568570
const actionList = Array.from(allActions).toSorted().join(", ");
569-
let desc = `${baseDescription} Current channel (${options.currentChannel}) supports: ${actionList}.`;
571+
let desc = `${baseDescription} Current channel (${currentChannel}) supports: ${actionList}.`;
570572

571573
// Include other configured channels so cron/isolated agents can discover them
572574
const otherChannels: string[] = [];
573575
for (const plugin of listChannelPlugins()) {
574-
if (plugin.id === options.currentChannel) {
576+
if (plugin.id === currentChannel) {
575577
continue;
576578
}
577-
const actions = listChannelSupportedActions({ cfg: options.config, channel: plugin.id });
579+
const actions = listChannelSupportedActions({
580+
cfg: resolvedOptions.config,
581+
channel: plugin.id,
582+
});
578583
if (actions.length > 0) {
579584
const all = new Set(["send", ...actions]);
580585
otherChannels.push(`${plugin.id} (${Array.from(all).toSorted().join(", ")})`);
@@ -589,8 +594,8 @@ function buildMessageToolDescription(options?: {
589594
}
590595

591596
// Fallback to generic description with all configured actions
592-
if (options?.config) {
593-
const actions = listChannelMessageActions(options.config);
597+
if (resolvedOptions.config) {
598+
const actions = listChannelMessageActions(resolvedOptions.config);
594599
if (actions.length > 0) {
595600
return `${baseDescription} Supports actions: ${actions.join(", ")}.`;
596601
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import type { OpenClawConfig } from "../../config/config.js";
2+
import { normalizeAnyChannelId } from "../registry.js";
3+
import type { ChannelMessageActionDiscoveryContext } from "./types.js";
4+
5+
export type ChannelMessageActionDiscoveryInput = {
6+
cfg?: OpenClawConfig;
7+
channel?: string | null;
8+
currentChannelProvider?: string | null;
9+
currentChannelId?: string | null;
10+
currentThreadTs?: string | null;
11+
currentMessageId?: string | number | null;
12+
accountId?: string | null;
13+
sessionKey?: string | null;
14+
sessionId?: string | null;
15+
agentId?: string | null;
16+
requesterSenderId?: string | null;
17+
};
18+
19+
export function resolveMessageActionDiscoveryChannelId(raw?: string | null): string | undefined {
20+
const normalized = normalizeAnyChannelId(raw);
21+
if (normalized) {
22+
return normalized;
23+
}
24+
const trimmed = raw?.trim();
25+
return trimmed || undefined;
26+
}
27+
28+
export function createMessageActionDiscoveryContext(
29+
params: ChannelMessageActionDiscoveryInput,
30+
): ChannelMessageActionDiscoveryContext {
31+
const currentChannelProvider = resolveMessageActionDiscoveryChannelId(
32+
params.channel ?? params.currentChannelProvider,
33+
);
34+
return {
35+
cfg: params.cfg ?? ({} as OpenClawConfig),
36+
currentChannelId: params.currentChannelId,
37+
currentChannelProvider,
38+
currentThreadTs: params.currentThreadTs,
39+
currentMessageId: params.currentMessageId,
40+
accountId: params.accountId,
41+
sessionKey: params.sessionKey,
42+
sessionId: params.sessionId,
43+
agentId: params.agentId,
44+
requesterSenderId: params.requesterSenderId,
45+
};
46+
}

‎src/channels/plugins/message-actions.test.ts‎

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,22 @@ const emptyRegistry = createTestRegistry([]);
2222
function createMessageActionsPlugin(params: {
2323
id: "discord" | "telegram";
2424
capabilities: readonly ChannelMessageCapability[];
25+
aliases?: string[];
2526
}): ChannelPlugin {
27+
const base = createChannelTestPluginBase({
28+
id: params.id,
29+
label: params.id === "discord" ? "Discord" : "Telegram",
30+
capabilities: { chatTypes: ["direct", "group"] },
31+
config: {
32+
listAccountIds: () => ["default"],
33+
},
34+
});
2635
return {
27-
...createChannelTestPluginBase({
28-
id: params.id,
29-
label: params.id === "discord" ? "Discord" : "Telegram",
30-
capabilities: { chatTypes: ["direct", "group"] },
31-
config: {
32-
listAccountIds: () => ["default"],
33-
},
34-
}),
36+
...base,
37+
meta: {
38+
...base.meta,
39+
...(params.aliases ? { aliases: params.aliases } : {}),
40+
},
3541
actions: {
3642
listActions: () => ["send"],
3743
getCapabilities: () => params.capabilities,
@@ -130,6 +136,29 @@ describe("message action capability checks", () => {
130136
);
131137
});
132138

139+
it("normalizes channel aliases for per-channel capability checks", () => {
140+
setActivePluginRegistry(
141+
createTestRegistry([
142+
{
143+
pluginId: "telegram",
144+
source: "test",
145+
plugin: createMessageActionsPlugin({
146+
id: "telegram",
147+
aliases: ["tg"],
148+
capabilities: ["cards"],
149+
}),
150+
},
151+
]),
152+
);
153+
154+
expect(
155+
listChannelMessageCapabilitiesForChannel({
156+
cfg: {} as OpenClawConfig,
157+
channel: "tg",
158+
}),
159+
).toEqual(["cards"]);
160+
});
161+
133162
it("skips crashing action/capability discovery paths and logs once", () => {
134163
const crashingPlugin: ChannelPlugin = {
135164
...createChannelTestPluginBase({

0 commit comments

Comments
 (0)