Skip to content

Commit 6ee409c

Browse files
authored
refactor(commands): share native plugin command merging (#120972)
1 parent 0dbdf99 commit 6ee409c

14 files changed

Lines changed: 610 additions & 106 deletions

‎docs/.generated/plugin-sdk-api-baseline.sha256‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ f1f206b2b9d4283be0aabc13a05008fd8e4121d85eb540d67379159e157e4e76 module/channel
3030
40f8e726de245e17cf7bf908bb7d6fb084f4927f5ccc20b0c089f47af1bd7a0e module/channel-dm-policy
3131
45fd5148ed2ca6b6fdcf0c244e2b51a2fe4d5b6e4d1c3d9875c0e07a31928b12 module/channel-entry-contract
3232
56b185eb99eb4981056befd38cd9511b655923934acb1c22aeec7d9c504bdf96 module/channel-feedback
33-
23673efb723395ee091ba21a986c7b295fc794875c509eb04aa43e06c503bca5 module/channel-inbound
33+
8cb9b544e252c40900ad056a16937b3487a2c4ee67b235cf33a083df9ae6b5d4 module/channel-inbound
3434
780abefd1600c55da219da43c769c873d071ab616b5e02d22f365677ed5a7be4 module/channel-inbound-debounce
3535
b72485da4fe5d7f73352f4474d00e30b749b64c8a2066755e104958cd96036c2 module/channel-ingress-runtime
3636
f83762680c0d0aa6fa1bad9e53c70bfdf3694126dbdd9491db764a69ed3f6d67 module/channel-lifecycle
@@ -51,10 +51,10 @@ e787a315cec9681a49a9e5d1af8fe4fae345561d30996888e5ef6593faae0a75 module/channel
5151
67df67da5ae72e9eaeb19d41b6bd2432ec4fd8b7b63b2b616fb98f3b4e0ec41d module/channel-streaming-config
5252
fdeffe356c7c4edeec9f8fd03edcadc375eabc7a9412e582b10c3180e3ef40fc module/cli-argv
5353
ad12670dbfe538f8d0ebf4fb2b68080e93a760278278e6b1ce9bb129d4b2d533 module/collection-runtime
54-
575158115f2a8528a61e5765781534dda5eee8729b61dd409819dcabd68c1ab4 module/command-auth
55-
b908f15287dc66d3e28a8092eeeffe4bfd3ce2956acabf660c306b213e7185fb module/command-auth-native
54+
f3a698172fca42c4effadf11ebf372b1b7d9b3c598ec9967a47db0cbe9faa989 module/command-auth
55+
42c8abf6c5579cb751e803781ddf9e1a6b73d26769e0fb88bd3bad54b02d67e4 module/command-auth-native
5656
396f60be14d8fe1144076cffeafc2e2ddc0379b5e664f9fd45d13a0edc4a9f2f module/command-detection
57-
5155909167a810ca258ec9baf4814e7d495cf25bd287fbb22be55eb1a8d88dfe module/command-primitives-runtime
57+
841389ea61d1389d1dbcc9350832e5cb5307ab5b380cbc14800ea62796e06ed7 module/command-primitives-runtime
5858
0ab02df5c3904386bfb062e7dce0a24e2ed835b54dcc0112e367346624cb0efe module/command-status
5959
02011f70a5de5f1861609206038b101d3e9bbf39bf45a3cfaaae56596299225b module/config-contracts
6060
7ee5bc35d30165be33ab229a96dd6a6e3e04c620fa4c41753d568798c514feb3 module/config-mutation
@@ -96,7 +96,7 @@ e5d2b540090c17602a1c36b42559f7b516e20730e06bbdd8606597c66ef2ef95 module/memory-
9696
577d82ef09e96de6af83a22c9b89cc1ddf2d76964abac5808b102ce04ced294f module/model-session-runtime
9797
e775d34923ab24ed4be8bf60b115e6869a9cd5bfca4d8eb9324c5b67209edce9 module/models-provider-runtime
9898
b518b4caa7f138d448fe08f5fc9e5f67da5d4b26aafaee2370feb2300e1baa21 module/native-command-config-runtime
99-
1e8b5e7bd8234919d339e750368cbe6425e7463d7e63eb5200d728cc17d6e959 module/native-command-registry
99+
7a5b56c37e5d862feab8258eb9f79f44e6946333c93b0c22dd3f792b33a7c19a module/native-command-registry
100100
15d1d490e4f7909d2d563b83988258a9cc5fa1344cffb0cd3dcd7592c0467632 module/param-readers
101101
ca7a56bb1a6169b4cf9befbf5aa21da280a8086fdc49fca4eec520a7a7c98549 module/persistent-dedupe
102102
f8b250da3eab2f85b2bf358872f910b29e541ea04358498a6deab353fa0520fc module/plugin-config-runtime
@@ -110,7 +110,7 @@ f319c52a0f1655de752efa55a983893e56e4afa0727f6cc4896c3238c984e91c module/reply-c
110110
43562c8fd0c976227051da084698c9aab757a769685b9630cbda8b8cc4cdbb16 module/reply-dispatch-runtime
111111
73f861fa3179d5af1159853c5acab0eec7a6c8f9398dcb75ea770e784fca6727 module/reply-history
112112
ecc6e3190892ff416ce69d26c8ce0aa08a468c8689ca3606c18b0271e2d87597 module/reply-payload
113-
2622e09ff245d5813d305ccb2aadfe485d503484172da9a903508bc4edb344c7 module/reply-runtime
113+
1dec24891dabc4d1556aa184add3754389f2233683041db3dbcefb7b11a40eca module/reply-runtime
114114
aa07d85d99fdd2b1e0cbe9975fb6dcae66b8bdce2607c6bd5402ae68bb15118c module/root-walk
115115
292405cda407e2569f731212f6f981b12bfe682836d5f6e58c6ae194ba27f144 module/routing
116116
7877a7e58fa32a64107154e5b714c6d165e96989d4aa5f43e0afac085a187af0 module/run-command
Lines changed: 262 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,262 @@
1+
import { listNativeCommandSpecsForConfig as listRealNativeCommandSpecsForConfig } from "openclaw/plugin-sdk/command-auth-native";
2+
import type { OpenClawConfig } from "openclaw/plugin-sdk/config-contracts";
3+
import type { NativeCommandSpec } from "openclaw/plugin-sdk/native-command-registry";
4+
import {
5+
createTestRegistry,
6+
resetPluginRuntimeStateForTest,
7+
setActivePluginRegistry,
8+
} from "openclaw/plugin-sdk/plugin-test-runtime";
9+
import { danger, warn, type RuntimeEnv } from "openclaw/plugin-sdk/runtime-env";
10+
import { normalizeLowercaseStringOrEmpty } from "openclaw/plugin-sdk/string-coerce-runtime";
11+
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
12+
import { discordSetupPlugin } from "../channel.setup.js";
13+
import { DISCORD_VOICE_COMMAND_SPEC } from "../voice/command.js";
14+
import { resolveDiscordProviderCommandSpecs } from "./provider.commands.js";
15+
16+
type ResolverParams = Parameters<typeof resolveDiscordProviderCommandSpecs>[0];
17+
type SkillCommands = ReturnType<NonNullable<ResolverParams["listSkillCommandsForAgents"]>>;
18+
19+
const cfg: OpenClawConfig = {};
20+
const skillCommands = [
21+
{ name: "skill-only", skillName: "Skill Only", description: "Skill only" },
22+
{ name: "extra-skill", skillName: "Extra Skill", description: "Extra skill" },
23+
];
24+
25+
function createResolverHarness(
26+
options: {
27+
pluginCommandSpecs?: NativeCommandSpec[];
28+
voiceEnabled?: boolean;
29+
nativeCommandSpecs?: NativeCommandSpec[];
30+
skillCommands?: SkillCommands;
31+
maxDiscordCommands?: number;
32+
nativeSkillsEnabled?: boolean;
33+
} = {},
34+
) {
35+
const error = vi.fn();
36+
const log = vi.fn();
37+
const runtime: RuntimeEnv = { error, log, exit: vi.fn() };
38+
const configuredSkillCommands = options.skillCommands ?? skillCommands;
39+
const nativeCommandSpecs = options.nativeCommandSpecs ?? [
40+
{ name: "built-in", description: "Built in", acceptsArgs: false },
41+
];
42+
const listSkillCommandsForAgents = vi.fn(() => configuredSkillCommands);
43+
const listNativeCommandSpecsForConfig = vi.fn(
44+
(
45+
_config: OpenClawConfig,
46+
listOptions?: Parameters<NonNullable<ResolverParams["listNativeCommandSpecsForConfig"]>>[1],
47+
): NativeCommandSpec[] => [
48+
...nativeCommandSpecs,
49+
...(listOptions?.skillCommands ?? []).map((skill) => ({
50+
name: skill.name,
51+
description: skill.description,
52+
acceptsArgs: true,
53+
})),
54+
],
55+
);
56+
const getPluginCommandSpecs = vi.fn(() => options.pluginCommandSpecs ?? []);
57+
58+
return {
59+
error,
60+
getPluginCommandSpecs,
61+
listNativeCommandSpecsForConfig,
62+
listSkillCommandsForAgents,
63+
log,
64+
resolve: () =>
65+
resolveDiscordProviderCommandSpecs({
66+
cfg,
67+
runtime,
68+
nativeEnabled: true,
69+
nativeSkillsEnabled: options.nativeSkillsEnabled ?? true,
70+
voiceEnabled: options.voiceEnabled ?? false,
71+
maxDiscordCommands: options.maxDiscordCommands ?? 3,
72+
listSkillCommandsForAgents,
73+
listNativeCommandSpecsForConfig,
74+
getPluginCommandSpecs,
75+
}),
76+
};
77+
}
78+
79+
describe("resolveDiscordProviderCommandSpecs", () => {
80+
beforeEach(() => {
81+
resetPluginRuntimeStateForTest();
82+
});
83+
84+
afterEach(() => {
85+
resetPluginRuntimeStateForTest();
86+
});
87+
88+
it("discards provisional skill collisions when command overflow removes skills", async () => {
89+
const harness = createResolverHarness({
90+
voiceEnabled: true,
91+
maxDiscordCommands: 4,
92+
pluginCommandSpecs: [
93+
{
94+
name: "skill-only",
95+
description: "Plugin skill alias",
96+
descriptionLocalizations: { de: "Plugin-Fertigkeitsalias" },
97+
acceptsArgs: false,
98+
},
99+
{ name: "plugin-unique", description: "Unique plugin", acceptsArgs: false },
100+
],
101+
});
102+
103+
const resolved = await harness.resolve();
104+
105+
expect(resolved.skillCommands).toEqual([]);
106+
expect(resolved.commandSpecs.map((command) => command.name)).toEqual([
107+
"built-in",
108+
"vc",
109+
"skill-only",
110+
"plugin-unique",
111+
]);
112+
expect(resolved.commandSpecs[2]).toEqual({
113+
name: "skill-only",
114+
description: "Plugin skill alias",
115+
descriptionLocalizations: { de: "Plugin-Fertigkeitsalias" },
116+
acceptsArgs: false,
117+
});
118+
expect(harness.error).not.toHaveBeenCalled();
119+
expect(harness.getPluginCommandSpecs).toHaveBeenCalledOnce();
120+
expect(harness.getPluginCommandSpecs).toHaveBeenCalledWith("discord", { config: cfg });
121+
expect(harness.listNativeCommandSpecsForConfig).toHaveBeenCalledTimes(2);
122+
expect(harness.log).toHaveBeenCalledOnce();
123+
expect(harness.log).toHaveBeenCalledWith(
124+
warn(
125+
"5 commands exceed the 4-command Discord limit; removing per-skill commands and keeping /skill.",
126+
),
127+
);
128+
});
129+
130+
it("logs a final built-in collision once when command overflow retries without skills", async () => {
131+
const harness = createResolverHarness({
132+
voiceEnabled: true,
133+
maxDiscordCommands: 4,
134+
pluginCommandSpecs: [
135+
{ name: "built-in", description: "Built-in collision", acceptsArgs: false },
136+
{ name: "plugin-unique", description: "Unique plugin", acceptsArgs: false },
137+
],
138+
});
139+
140+
const resolved = await harness.resolve();
141+
142+
expect(resolved.skillCommands).toEqual([]);
143+
expect(resolved.commandSpecs.map((command) => command.name)).toEqual([
144+
"built-in",
145+
"vc",
146+
"plugin-unique",
147+
]);
148+
expect(harness.error).toHaveBeenCalledOnce();
149+
expect(harness.error).toHaveBeenCalledWith(
150+
danger(
151+
'discord: plugin command "/built-in" duplicates an existing native command. Skipping.',
152+
),
153+
);
154+
expect(harness.getPluginCommandSpecs).toHaveBeenCalledOnce();
155+
expect(harness.getPluginCommandSpecs).toHaveBeenCalledWith("discord", { config: cfg });
156+
expect(harness.listNativeCommandSpecsForConfig).toHaveBeenCalledTimes(2);
157+
});
158+
159+
it("counts voice in the exact Discord command limit", async () => {
160+
const harness = createResolverHarness({
161+
voiceEnabled: true,
162+
nativeSkillsEnabled: false,
163+
maxDiscordCommands: 100,
164+
nativeCommandSpecs: Array.from({ length: 99 }, (_value, index) => ({
165+
name: `command-${String(index + 1)}`,
166+
description: `Command ${String(index + 1)}`,
167+
acceptsArgs: false,
168+
})),
169+
});
170+
171+
const resolved = await harness.resolve();
172+
173+
expect(resolved.commandSpecs).toHaveLength(100);
174+
expect(resolved.commandSpecs.at(-1)).toBe(DISCORD_VOICE_COMMAND_SPEC);
175+
expect(harness.log).not.toHaveBeenCalled();
176+
});
177+
178+
it("retains voice ownership when a plugin claims vc", async () => {
179+
const harness = createResolverHarness({
180+
voiceEnabled: true,
181+
nativeSkillsEnabled: false,
182+
maxDiscordCommands: 100,
183+
pluginCommandSpecs: [{ name: "vc", description: "Plugin voice", acceptsArgs: false }],
184+
});
185+
186+
const resolved = await harness.resolve();
187+
188+
expect(resolved.commandSpecs.map((command) => command.name)).toEqual(["built-in", "vc"]);
189+
expect(resolved.commandSpecs[1]).toBe(DISCORD_VOICE_COMMAND_SPEC);
190+
expect(harness.error).toHaveBeenCalledOnce();
191+
expect(harness.error).toHaveBeenCalledWith(
192+
danger('discord: plugin command "/vc" duplicates an existing native command. Skipping.'),
193+
);
194+
expect(harness.getPluginCommandSpecs).toHaveBeenCalledOnce();
195+
});
196+
197+
it("keeps a skill named vc from shadowing or duplicating voice", async () => {
198+
const vcSkillCommands: SkillCommands = [
199+
{ name: "vc", skillName: "Voice Skill", description: "Voice skill" },
200+
];
201+
const harness = createResolverHarness({
202+
voiceEnabled: true,
203+
maxDiscordCommands: 100,
204+
skillCommands: vcSkillCommands,
205+
});
206+
207+
const resolved = await harness.resolve();
208+
209+
expect(resolved.skillCommands).toEqual(vcSkillCommands);
210+
expect(resolved.commandSpecs.map((command) => command.name)).toEqual(["built-in", "vc"]);
211+
expect(resolved.commandSpecs[1]).toBe(DISCORD_VOICE_COMMAND_SPEC);
212+
expect(harness.error).not.toHaveBeenCalled();
213+
});
214+
215+
it("deduplicates provider-renamed primary specs before Discord cap planning", async () => {
216+
setActivePluginRegistry(
217+
createTestRegistry([{ pluginId: "discord", plugin: discordSetupPlugin, source: "test" }]),
218+
);
219+
const voiceSkill: SkillCommands[number] = {
220+
name: "voice",
221+
skillName: "Voice Skill",
222+
description: "Skill voice",
223+
};
224+
const config: OpenClawConfig = { commands: { native: true, nativeSkills: true } };
225+
const rawPrimary = listRealNativeCommandSpecsForConfig(config, {
226+
provider: "discord",
227+
skillCommands: [voiceSkill],
228+
});
229+
const rawVoice = rawPrimary.filter(
230+
(spec) => normalizeLowercaseStringOrEmpty(spec.name) === "voice",
231+
);
232+
const uniqueCount = new Set(
233+
rawPrimary.map((spec) => normalizeLowercaseStringOrEmpty(spec.name)).filter(Boolean),
234+
).size;
235+
expect(rawVoice).toHaveLength(2);
236+
expect(rawVoice.map((spec) => spec.description)).toEqual([
237+
"Control text-to-speech (TTS).",
238+
"Skill voice",
239+
]);
240+
const log = vi.fn();
241+
242+
const resolved = await resolveDiscordProviderCommandSpecs({
243+
cfg: config,
244+
runtime: { log, error: vi.fn(), exit: vi.fn() },
245+
nativeEnabled: true,
246+
nativeSkillsEnabled: true,
247+
voiceEnabled: false,
248+
maxDiscordCommands: uniqueCount,
249+
listSkillCommandsForAgents: vi.fn(() => [voiceSkill]),
250+
getPluginCommandSpecs: vi.fn(() => []),
251+
});
252+
253+
expect(resolved.skillCommands).toEqual([voiceSkill]);
254+
expect(resolved.commandSpecs).toHaveLength(uniqueCount);
255+
expect(
256+
resolved.commandSpecs.filter(
257+
(spec) => normalizeLowercaseStringOrEmpty(spec.name) === "voice",
258+
),
259+
).toEqual([expect.objectContaining({ description: "Control text-to-speech (TTS)." })]);
260+
expect(log).not.toHaveBeenCalled();
261+
});
262+
});

0 commit comments

Comments
 (0)