Skip to content

Commit 951f3f9

Browse files
committed
Plugins: split message discovery and dispatch
1 parent da948a8 commit 951f3f9

10 files changed

Lines changed: 373 additions & 382 deletions

‎src/agents/channel-tools.ts‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
import { getChannelPlugin, listChannelPlugins } from "../channels/plugins/index.js";
22
import {
33
createMessageActionDiscoveryContext,
4+
resolveMessageActionDiscoveryForPlugin,
45
resolveMessageActionDiscoveryChannelId,
5-
} from "../channels/plugins/message-action-discovery.js";
6-
import {
76
__testing as messageActionTesting,
8-
resolveMessageActionDiscoveryForPlugin,
9-
} from "../channels/plugins/message-actions.js";
7+
} from "../channels/plugins/message-action-discovery.js";
108
import type { ChannelAgentTool, ChannelMessageActionName } from "../channels/plugins/types.js";
119
import { normalizeAnyChannelId } from "../channels/registry.js";
1210
import type { OpenClawConfig } from "../config/config.js";
@@ -32,7 +30,7 @@ export function listChannelSupportedActions(params: {
3230
return [];
3331
}
3432
const plugin = getChannelPlugin(channelId as Parameters<typeof getChannelPlugin>[0]);
35-
if (!plugin?.actions?.listActions) {
33+
if (!plugin?.actions) {
3634
return [];
3735
}
3836
return resolveMessageActionDiscoveryForPlugin({

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
channelSupportsMessageCapabilityForChannel,
66
listChannelMessageActions,
77
resolveChannelMessageToolSchemaProperties,
8-
} from "../../channels/plugins/message-actions.js";
8+
} from "../../channels/plugins/message-action-discovery.js";
99
import type { ChannelMessageCapability } from "../../channels/plugins/message-capabilities.js";
1010
import {
1111
CHANNEL_MESSAGE_ACTION_NAMES,

‎src/channels/plugins/message-action-discovery.ts‎

Lines changed: 333 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
1+
import type { TSchema } from "@sinclair/typebox";
12
import type { OpenClawConfig } from "../../config/config.js";
3+
import { defaultRuntime } from "../../runtime.js";
24
import { normalizeAnyChannelId } from "../registry.js";
3-
import type { ChannelMessageActionDiscoveryContext } from "./types.js";
5+
import { getChannelPlugin, listChannelPlugins } from "./index.js";
6+
import type { ChannelMessageCapability } from "./message-capabilities.js";
7+
import type {
8+
ChannelMessageActionDiscoveryContext,
9+
ChannelMessageActionName,
10+
ChannelMessageToolDiscovery,
11+
ChannelMessageToolSchemaContribution,
12+
} from "./types.js";
413

514
export type ChannelMessageActionDiscoveryInput = {
615
cfg?: OpenClawConfig;
@@ -16,6 +25,10 @@ export type ChannelMessageActionDiscoveryInput = {
1625
requesterSenderId?: string | null;
1726
};
1827

28+
type ChannelActions = NonNullable<NonNullable<ReturnType<typeof getChannelPlugin>>["actions"]>;
29+
30+
const loggedMessageActionErrors = new Set<string>();
31+
1932
export function resolveMessageActionDiscoveryChannelId(raw?: string | null): string | undefined {
2033
const normalized = normalizeAnyChannelId(raw);
2134
if (normalized) {
@@ -44,3 +57,322 @@ export function createMessageActionDiscoveryContext(
4457
requesterSenderId: params.requesterSenderId,
4558
};
4659
}
60+
61+
function logMessageActionError(params: {
62+
pluginId: string;
63+
operation: "describeMessageTool" | "getCapabilities" | "getToolSchema" | "listActions";
64+
error: unknown;
65+
}) {
66+
const message = params.error instanceof Error ? params.error.message : String(params.error);
67+
const key = `${params.pluginId}:${params.operation}:${message}`;
68+
if (loggedMessageActionErrors.has(key)) {
69+
return;
70+
}
71+
loggedMessageActionErrors.add(key);
72+
const stack = params.error instanceof Error && params.error.stack ? params.error.stack : null;
73+
defaultRuntime.error?.(
74+
`[message-action-discovery] ${params.pluginId}.actions.${params.operation} failed: ${stack ?? message}`,
75+
);
76+
}
77+
78+
function runListActionsSafely(params: {
79+
pluginId: string;
80+
context: ChannelMessageActionDiscoveryContext;
81+
listActions: NonNullable<ChannelActions["listActions"]>;
82+
}): ChannelMessageActionName[] {
83+
try {
84+
const listed = params.listActions(params.context);
85+
return Array.isArray(listed) ? listed : [];
86+
} catch (error) {
87+
logMessageActionError({
88+
pluginId: params.pluginId,
89+
operation: "listActions",
90+
error,
91+
});
92+
return [];
93+
}
94+
}
95+
96+
function describeMessageToolSafely(params: {
97+
pluginId: string;
98+
context: ChannelMessageActionDiscoveryContext;
99+
describeMessageTool: NonNullable<ChannelActions["describeMessageTool"]>;
100+
}): ChannelMessageToolDiscovery | null {
101+
try {
102+
return params.describeMessageTool(params.context) ?? null;
103+
} catch (error) {
104+
logMessageActionError({
105+
pluginId: params.pluginId,
106+
operation: "describeMessageTool",
107+
error,
108+
});
109+
return null;
110+
}
111+
}
112+
113+
function listCapabilitiesSafely(params: {
114+
pluginId: string;
115+
actions: ChannelActions;
116+
context: ChannelMessageActionDiscoveryContext;
117+
}): readonly ChannelMessageCapability[] {
118+
try {
119+
return params.actions.getCapabilities?.(params.context) ?? [];
120+
} catch (error) {
121+
logMessageActionError({
122+
pluginId: params.pluginId,
123+
operation: "getCapabilities",
124+
error,
125+
});
126+
return [];
127+
}
128+
}
129+
130+
function runGetToolSchemaSafely(params: {
131+
pluginId: string;
132+
context: ChannelMessageActionDiscoveryContext;
133+
getToolSchema: NonNullable<ChannelActions["getToolSchema"]>;
134+
}):
135+
| ChannelMessageToolSchemaContribution
136+
| ChannelMessageToolSchemaContribution[]
137+
| null
138+
| undefined {
139+
try {
140+
return params.getToolSchema(params.context);
141+
} catch (error) {
142+
logMessageActionError({
143+
pluginId: params.pluginId,
144+
operation: "getToolSchema",
145+
error,
146+
});
147+
return null;
148+
}
149+
}
150+
151+
function normalizeToolSchemaContributions(
152+
value:
153+
| ChannelMessageToolSchemaContribution
154+
| ChannelMessageToolSchemaContribution[]
155+
| null
156+
| undefined,
157+
): ChannelMessageToolSchemaContribution[] {
158+
if (!value) {
159+
return [];
160+
}
161+
return Array.isArray(value) ? value : [value];
162+
}
163+
164+
type ResolvedChannelMessageActionDiscovery = {
165+
actions: ChannelMessageActionName[];
166+
capabilities: readonly ChannelMessageCapability[];
167+
schemaContributions: ChannelMessageToolSchemaContribution[];
168+
};
169+
170+
export function resolveMessageActionDiscoveryForPlugin(params: {
171+
pluginId: string;
172+
actions?: ChannelActions;
173+
context: ChannelMessageActionDiscoveryContext;
174+
includeActions?: boolean;
175+
includeCapabilities?: boolean;
176+
includeSchema?: boolean;
177+
}): ResolvedChannelMessageActionDiscovery {
178+
const adapter = params.actions;
179+
if (!adapter) {
180+
return {
181+
actions: [],
182+
capabilities: [],
183+
schemaContributions: [],
184+
};
185+
}
186+
187+
if (adapter.describeMessageTool) {
188+
const described = describeMessageToolSafely({
189+
pluginId: params.pluginId,
190+
context: params.context,
191+
describeMessageTool: adapter.describeMessageTool,
192+
});
193+
return {
194+
actions:
195+
params.includeActions && Array.isArray(described?.actions) ? [...described.actions] : [],
196+
capabilities:
197+
params.includeCapabilities && Array.isArray(described?.capabilities)
198+
? described.capabilities
199+
: [],
200+
schemaContributions: params.includeSchema
201+
? normalizeToolSchemaContributions(described?.schema)
202+
: [],
203+
};
204+
}
205+
206+
return {
207+
actions:
208+
params.includeActions && adapter.listActions
209+
? runListActionsSafely({
210+
pluginId: params.pluginId,
211+
context: params.context,
212+
listActions: adapter.listActions,
213+
})
214+
: [],
215+
capabilities:
216+
params.includeCapabilities && adapter.getCapabilities
217+
? listCapabilitiesSafely({
218+
pluginId: params.pluginId,
219+
actions: adapter,
220+
context: params.context,
221+
})
222+
: [],
223+
schemaContributions:
224+
params.includeSchema && adapter.getToolSchema
225+
? normalizeToolSchemaContributions(
226+
runGetToolSchemaSafely({
227+
pluginId: params.pluginId,
228+
context: params.context,
229+
getToolSchema: adapter.getToolSchema,
230+
}),
231+
)
232+
: [],
233+
};
234+
}
235+
236+
export function listChannelMessageActions(cfg: OpenClawConfig): ChannelMessageActionName[] {
237+
const actions = new Set<ChannelMessageActionName>(["send", "broadcast"]);
238+
for (const plugin of listChannelPlugins()) {
239+
for (const action of resolveMessageActionDiscoveryForPlugin({
240+
pluginId: plugin.id,
241+
actions: plugin.actions,
242+
context: { cfg },
243+
includeActions: true,
244+
}).actions) {
245+
actions.add(action);
246+
}
247+
}
248+
return Array.from(actions);
249+
}
250+
251+
export function listChannelMessageCapabilities(cfg: OpenClawConfig): ChannelMessageCapability[] {
252+
const capabilities = new Set<ChannelMessageCapability>();
253+
for (const plugin of listChannelPlugins()) {
254+
for (const capability of resolveMessageActionDiscoveryForPlugin({
255+
pluginId: plugin.id,
256+
actions: plugin.actions,
257+
context: { cfg },
258+
includeCapabilities: true,
259+
}).capabilities) {
260+
capabilities.add(capability);
261+
}
262+
}
263+
return Array.from(capabilities);
264+
}
265+
266+
export function listChannelMessageCapabilitiesForChannel(params: {
267+
cfg: OpenClawConfig;
268+
channel?: string;
269+
currentChannelId?: string | null;
270+
currentThreadTs?: string | null;
271+
currentMessageId?: string | number | null;
272+
accountId?: string | null;
273+
sessionKey?: string | null;
274+
sessionId?: string | null;
275+
agentId?: string | null;
276+
requesterSenderId?: string | null;
277+
}): ChannelMessageCapability[] {
278+
const channelId = resolveMessageActionDiscoveryChannelId(params.channel);
279+
if (!channelId) {
280+
return [];
281+
}
282+
const plugin = getChannelPlugin(channelId as Parameters<typeof getChannelPlugin>[0]);
283+
return plugin?.actions
284+
? Array.from(
285+
resolveMessageActionDiscoveryForPlugin({
286+
pluginId: plugin.id,
287+
actions: plugin.actions,
288+
context: createMessageActionDiscoveryContext(params),
289+
includeCapabilities: true,
290+
}).capabilities,
291+
)
292+
: [];
293+
}
294+
295+
function mergeToolSchemaProperties(
296+
target: Record<string, TSchema>,
297+
source: Record<string, TSchema> | undefined,
298+
) {
299+
if (!source) {
300+
return;
301+
}
302+
for (const [name, schema] of Object.entries(source)) {
303+
if (!(name in target)) {
304+
target[name] = schema;
305+
}
306+
}
307+
}
308+
309+
export function resolveChannelMessageToolSchemaProperties(params: {
310+
cfg: OpenClawConfig;
311+
channel?: string;
312+
currentChannelId?: string | null;
313+
currentThreadTs?: string | null;
314+
currentMessageId?: string | number | null;
315+
accountId?: string | null;
316+
sessionKey?: string | null;
317+
sessionId?: string | null;
318+
agentId?: string | null;
319+
requesterSenderId?: string | null;
320+
}): Record<string, TSchema> {
321+
const properties: Record<string, TSchema> = {};
322+
const currentChannel = resolveMessageActionDiscoveryChannelId(params.channel);
323+
const discoveryBase = createMessageActionDiscoveryContext(params);
324+
325+
for (const plugin of listChannelPlugins()) {
326+
if (!plugin.actions) {
327+
continue;
328+
}
329+
for (const contribution of resolveMessageActionDiscoveryForPlugin({
330+
pluginId: plugin.id,
331+
actions: plugin.actions,
332+
context: discoveryBase,
333+
includeSchema: true,
334+
}).schemaContributions) {
335+
const visibility = contribution.visibility ?? "current-channel";
336+
if (currentChannel) {
337+
if (visibility === "all-configured" || plugin.id === currentChannel) {
338+
mergeToolSchemaProperties(properties, contribution.properties);
339+
}
340+
continue;
341+
}
342+
mergeToolSchemaProperties(properties, contribution.properties);
343+
}
344+
}
345+
346+
return properties;
347+
}
348+
349+
export function channelSupportsMessageCapability(
350+
cfg: OpenClawConfig,
351+
capability: ChannelMessageCapability,
352+
): boolean {
353+
return listChannelMessageCapabilities(cfg).includes(capability);
354+
}
355+
356+
export function channelSupportsMessageCapabilityForChannel(
357+
params: {
358+
cfg: OpenClawConfig;
359+
channel?: string;
360+
currentChannelId?: string | null;
361+
currentThreadTs?: string | null;
362+
currentMessageId?: string | number | null;
363+
accountId?: string | null;
364+
sessionKey?: string | null;
365+
sessionId?: string | null;
366+
agentId?: string | null;
367+
requesterSenderId?: string | null;
368+
},
369+
capability: ChannelMessageCapability,
370+
): boolean {
371+
return listChannelMessageCapabilitiesForChannel(params).includes(capability);
372+
}
373+
374+
export const __testing = {
375+
resetLoggedMessageActionErrors() {
376+
loggedMessageActionErrors.clear();
377+
},
378+
};

0 commit comments

Comments
 (0)