fix(skills): deduplicate slash commands by skillName across all interfaces

Move skill-command deduplication by skillName from the Discord-only
`dedupeSkillCommandsForDiscord` into `listSkillCommandsForAgents` so
every interface (TUI, Slack, text) consistently sees a clean command
list without platform-specific workarounds.

When multiple agents share a skill with the same name the old code
emitted `github` + `github_2` and relied on Discord to collapse them.
Now `listSkillCommandsForAgents` returns only the first registration
per skillName, and the Discord-specific wrapper is removed.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Shivam
2026-02-26 09:07:14 +05:30
committed by Shakker
parent 5d5fa0dac8
commit 48decefbf4
4 changed files with 70 additions and 49 deletions

View File

@@ -69,9 +69,10 @@ vi.mock("../agents/skills.js", () => {
let listSkillCommandsForAgents: typeof import("./skill-commands.js").listSkillCommandsForAgents;
let resolveSkillCommandInvocation: typeof import("./skill-commands.js").resolveSkillCommandInvocation;
let skillCommandsTesting: typeof import("./skill-commands.js").__testing;
beforeAll(async () => {
({ listSkillCommandsForAgents, resolveSkillCommandInvocation } =
({ listSkillCommandsForAgents, resolveSkillCommandInvocation, __testing: skillCommandsTesting } =
await import("./skill-commands.js"));
});
@@ -125,7 +126,7 @@ describe("listSkillCommandsForAgents", () => {
);
});
it("lists all agents when agentIds is omitted", async () => {
it("deduplicates by skillName across agents, keeping the first registration", async () => {
const baseDir = await makeTempDir("openclaw-skills-");
const mainWorkspace = path.join(baseDir, "main");
const researchWorkspace = path.join(baseDir, "research");
@@ -143,8 +144,10 @@ describe("listSkillCommandsForAgents", () => {
},
});
const names = commands.map((entry) => entry.name);
// demo-skill appears in both workspaces; only the first registration (demo_skill) survives.
expect(names).toContain("demo_skill");
expect(names).toContain("demo_skill_2");
expect(names).not.toContain("demo_skill_2");
// extra-skill is unique to the research workspace and should be present.
expect(names).toContain("extra_skill");
});
@@ -297,3 +300,38 @@ describe("listSkillCommandsForAgents", () => {
expect(commands.map((entry) => entry.skillName)).toContain("demo-skill");
});
});
describe("dedupeBySkillName", () => {
it("keeps the first entry when multiple commands share a skillName", () => {
const input = [
{ name: "github", skillName: "github", description: "GitHub" },
{ name: "github_2", skillName: "github", description: "GitHub" },
{ name: "weather", skillName: "weather", description: "Weather" },
{ name: "weather_2", skillName: "weather", description: "Weather" },
];
const output = skillCommandsTesting.dedupeBySkillName(input);
expect(output.map((e) => e.name)).toEqual(["github", "weather"]);
});
it("matches skillName case-insensitively", () => {
const input = [
{ name: "ClawHub", skillName: "ClawHub", description: "ClawHub" },
{ name: "clawhub_2", skillName: "clawhub", description: "ClawHub" },
];
const output = skillCommandsTesting.dedupeBySkillName(input);
expect(output).toHaveLength(1);
expect(output[0]?.name).toBe("ClawHub");
});
it("passes through commands with an empty skillName", () => {
const input = [
{ name: "a", skillName: "", description: "A" },
{ name: "b", skillName: "", description: "B" },
];
expect(skillCommandsTesting.dedupeBySkillName(input)).toHaveLength(2);
});
it("returns an empty array for empty input", () => {
expect(skillCommandsTesting.dedupeBySkillName([])).toEqual([]);
});
});

View File

@@ -46,6 +46,26 @@ export function listSkillCommandsForWorkspace(params: {
});
}
// Deduplicate skill commands by skillName, keeping the first registration.
// When multiple agents have a skill with the same name (e.g. one with a
// workspace override and one from bundled), the suffix-renamed entries
// (github_2, github_3…) are dropped so every interface sees a clean list.
function dedupeBySkillName(commands: SkillCommandSpec[]): SkillCommandSpec[] {
const seen = new Set<string>();
const out: SkillCommandSpec[] = [];
for (const cmd of commands) {
const key = cmd.skillName.trim().toLowerCase();
if (key && seen.has(key)) {
continue;
}
if (key) {
seen.add(key);
}
out.push(cmd);
}
return out;
}
export function listSkillCommandsForAgents(params: {
cfg: OpenClawConfig;
agentIds?: string[];
@@ -109,9 +129,16 @@ export function listSkillCommandsForAgents(params: {
entries.push(command);
}
}
return entries;
// Dedupe by skillName across workspaces so every interface (Discord, TUI,
// Slack, text) sees a consistent command list without platform-specific
// workarounds.
return dedupeBySkillName(entries);
}
export const __testing = {
dedupeBySkillName,
};
function normalizeSkillCommandLookup(value: string): string {
return value
.trim()

View File

@@ -1,30 +1,6 @@
import { describe, expect, it } from "vitest";
import { __testing } from "./provider.js";
describe("dedupeSkillCommandsForDiscord", () => {
it("keeps first command per skillName and drops suffix duplicates", () => {
const input = [
{ name: "github", skillName: "github", description: "GitHub" },
{ name: "github_2", skillName: "github", description: "GitHub" },
{ name: "weather", skillName: "weather", description: "Weather" },
{ name: "weather_2", skillName: "weather", description: "Weather" },
];
const output = __testing.dedupeSkillCommandsForDiscord(input);
expect(output.map((entry) => entry.name)).toEqual(["github", "weather"]);
});
it("treats skillName case-insensitively", () => {
const input = [
{ name: "ClawHub", skillName: "ClawHub", description: "ClawHub" },
{ name: "clawhub_2", skillName: "clawhub", description: "ClawHub" },
];
const output = __testing.dedupeSkillCommandsForDiscord(input);
expect(output).toHaveLength(1);
expect(output[0]?.name).toBe("ClawHub");
});
});
describe("resolveThreadBindingsEnabled", () => {
it("defaults to enabled when unset", () => {
expect(

View File

@@ -126,25 +126,6 @@ function formatThreadBindingDurationForConfigLabel(durationMs: number): string {
return label === "disabled" ? "off" : label;
}
function dedupeSkillCommandsForDiscord(
skillCommands: ReturnType<typeof listSkillCommandsForAgents>,
) {
const seen = new Set<string>();
const deduped: ReturnType<typeof listSkillCommandsForAgents> = [];
for (const command of skillCommands) {
const key = command.skillName.trim().toLowerCase();
if (!key) {
deduped.push(command);
continue;
}
if (seen.has(key)) {
continue;
}
seen.add(key);
deduped.push(command);
}
return deduped;
}
function appendPluginCommandSpecs(params: {
commandSpecs: NativeCommandSpec[];
@@ -434,7 +415,7 @@ export async function monitorDiscordProvider(opts: MonitorDiscordOpts = {}) {
const maxDiscordCommands = 100;
let skillCommands =
nativeEnabled && nativeSkillsEnabled
? dedupeSkillCommandsForDiscord(listSkillCommandsForAgents({ cfg }))
? listSkillCommandsForAgents({ cfg })
: [];
let commandSpecs = nativeEnabled
? listNativeCommandSpecsForConfig(cfg, { skillCommands, provider: "discord" })
@@ -819,7 +800,6 @@ async function clearDiscordNativeCommands(params: {
export const __testing = {
createDiscordGatewayPlugin,
dedupeSkillCommandsForDiscord,
resolveDiscordRuntimeGroupPolicy: resolveOpenProviderRuntimeGroupPolicy,
resolveDefaultGroupPolicy,
resolveDiscordRestFetch,