Plugins: honor native command aliases at dispatch

This commit is contained in:
Vincent Koc
2026-03-16 21:01:10 -07:00
parent 095a9f6e1d
commit f90d432de3
4 changed files with 147 additions and 1 deletions

View File

@@ -7,6 +7,7 @@ import {
executePluginCommand,
getPluginCommandSpecs,
listPluginCommands,
matchPluginCommand,
registerPluginCommand,
} from "./commands.js";
import { setActivePluginRegistry } from "./runtime.js";
@@ -107,6 +108,29 @@ describe("registerPluginCommand", () => {
expect(getPluginCommandSpecs("slack")).toEqual([]);
});
it("matches provider-specific native aliases back to the canonical command", () => {
const result = registerPluginCommand("demo-plugin", {
name: "voice",
nativeNames: {
default: "talkvoice",
discord: "discordvoice",
},
description: "Demo command",
acceptsArgs: true,
handler: async () => ({ text: "ok" }),
});
expect(result).toEqual({ ok: true });
expect(matchPluginCommand("/talkvoice now")).toMatchObject({
command: expect.objectContaining({ name: "voice", pluginId: "demo-plugin" }),
args: "now",
});
expect(matchPluginCommand("/discordvoice now")).toMatchObject({
command: expect.objectContaining({ name: "voice", pluginId: "demo-plugin" }),
args: "now",
});
});
it("resolves Discord DM command bindings with the user target prefix intact", () => {
expect(
__testing.resolveBindingConversationFromCommand({

View File

@@ -219,7 +219,11 @@ export function matchPluginCommand(
const args = spaceIndex === -1 ? undefined : trimmed.slice(spaceIndex + 1).trim();
const key = commandName.toLowerCase();
const command = pluginCommands.get(key);
const command =
pluginCommands.get(key) ??
Array.from(pluginCommands.values()).find((candidate) =>
listPluginInvocationNames(candidate).includes(key),
);
if (!command) {
return null;
@@ -458,6 +462,24 @@ function resolvePluginNativeName(
return command.name;
}
function listPluginInvocationNames(command: OpenClawPluginCommandDefinition): string[] {
const names = new Set<string>();
const push = (value: string | undefined) => {
const normalized = value?.trim().toLowerCase();
if (!normalized) {
return;
}
names.add(`/${normalized}`);
};
push(command.name);
push(command.nativeNames?.default);
push(command.nativeNames?.telegram);
push(command.nativeNames?.discord);
return [...names];
}
/**
* Get plugin command specs for native command registration (e.g., Telegram).
*/