fix: register plugin commands natively (#1558) (thanks @Glucksberg)
This commit is contained in:
parent
29043209c9
commit
c2940adc80
@ -15,15 +15,18 @@ import {
|
|||||||
shouldHandleTextCommands,
|
shouldHandleTextCommands,
|
||||||
} from "./commands-registry.js";
|
} from "./commands-registry.js";
|
||||||
import type { ChatCommandDefinition } from "./commands-registry.types.js";
|
import type { ChatCommandDefinition } from "./commands-registry.types.js";
|
||||||
|
import { clearPluginCommands, registerPluginCommand } from "../plugins/commands.js";
|
||||||
import { setActivePluginRegistry } from "../plugins/runtime.js";
|
import { setActivePluginRegistry } from "../plugins/runtime.js";
|
||||||
import { createTestRegistry } from "../test-utils/channel-plugins.js";
|
import { createTestRegistry } from "../test-utils/channel-plugins.js";
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
setActivePluginRegistry(createTestRegistry([]));
|
setActivePluginRegistry(createTestRegistry([]));
|
||||||
|
clearPluginCommands();
|
||||||
});
|
});
|
||||||
|
|
||||||
afterEach(() => {
|
afterEach(() => {
|
||||||
setActivePluginRegistry(createTestRegistry([]));
|
setActivePluginRegistry(createTestRegistry([]));
|
||||||
|
clearPluginCommands();
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("commands registry", () => {
|
describe("commands registry", () => {
|
||||||
@ -85,6 +88,19 @@ describe("commands registry", () => {
|
|||||||
expect(native.find((spec) => spec.name === "demo_skill")).toBeTruthy();
|
expect(native.find((spec) => spec.name === "demo_skill")).toBeTruthy();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("includes plugin commands in native specs", () => {
|
||||||
|
registerPluginCommand("plugin-core", {
|
||||||
|
name: "plugstatus",
|
||||||
|
description: "Plugin status",
|
||||||
|
handler: () => ({ text: "ok" }),
|
||||||
|
});
|
||||||
|
const native = listNativeCommandSpecsForConfig(
|
||||||
|
{ commands: { config: false, debug: false, native: true } },
|
||||||
|
{ skillCommands: [] },
|
||||||
|
);
|
||||||
|
expect(native.find((spec) => spec.name === "plugstatus")).toBeTruthy();
|
||||||
|
});
|
||||||
|
|
||||||
it("detects known text commands", () => {
|
it("detects known text commands", () => {
|
||||||
const detection = getCommandDetection();
|
const detection = getCommandDetection();
|
||||||
expect(detection.exact.has("/commands")).toBe(true);
|
expect(detection.exact.has("/commands")).toBe(true);
|
||||||
|
|||||||
@ -1,6 +1,7 @@
|
|||||||
import type { ClawdbotConfig } from "../config/types.js";
|
import type { ClawdbotConfig } from "../config/types.js";
|
||||||
import type { SkillCommandSpec } from "../agents/skills.js";
|
import type { SkillCommandSpec } from "../agents/skills.js";
|
||||||
import { getChatCommands, getNativeCommandSurfaces } from "./commands-registry.data.js";
|
import { getChatCommands, getNativeCommandSurfaces } from "./commands-registry.data.js";
|
||||||
|
import { getPluginCommandSpecs } from "../plugins/commands.js";
|
||||||
import { DEFAULT_MODEL, DEFAULT_PROVIDER } from "../agents/defaults.js";
|
import { DEFAULT_MODEL, DEFAULT_PROVIDER } from "../agents/defaults.js";
|
||||||
import { resolveConfiguredModelRef } from "../agents/model-selection.js";
|
import { resolveConfiguredModelRef } from "../agents/model-selection.js";
|
||||||
import type {
|
import type {
|
||||||
@ -108,7 +109,7 @@ export function listChatCommandsForConfig(
|
|||||||
export function listNativeCommandSpecs(params?: {
|
export function listNativeCommandSpecs(params?: {
|
||||||
skillCommands?: SkillCommandSpec[];
|
skillCommands?: SkillCommandSpec[];
|
||||||
}): NativeCommandSpec[] {
|
}): NativeCommandSpec[] {
|
||||||
return listChatCommands({ skillCommands: params?.skillCommands })
|
const base = listChatCommands({ skillCommands: params?.skillCommands })
|
||||||
.filter((command) => command.scope !== "text" && command.nativeName)
|
.filter((command) => command.scope !== "text" && command.nativeName)
|
||||||
.map((command) => ({
|
.map((command) => ({
|
||||||
name: command.nativeName ?? command.key,
|
name: command.nativeName ?? command.key,
|
||||||
@ -116,13 +117,18 @@ export function listNativeCommandSpecs(params?: {
|
|||||||
acceptsArgs: Boolean(command.acceptsArgs),
|
acceptsArgs: Boolean(command.acceptsArgs),
|
||||||
args: command.args,
|
args: command.args,
|
||||||
}));
|
}));
|
||||||
|
const pluginSpecs = getPluginCommandSpecs();
|
||||||
|
if (pluginSpecs.length === 0) return base;
|
||||||
|
const seen = new Set(base.map((spec) => spec.name.toLowerCase()));
|
||||||
|
const extras = pluginSpecs.filter((spec) => !seen.has(spec.name.toLowerCase()));
|
||||||
|
return extras.length > 0 ? [...base, ...extras] : base;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function listNativeCommandSpecsForConfig(
|
export function listNativeCommandSpecsForConfig(
|
||||||
cfg: ClawdbotConfig,
|
cfg: ClawdbotConfig,
|
||||||
params?: { skillCommands?: SkillCommandSpec[] },
|
params?: { skillCommands?: SkillCommandSpec[] },
|
||||||
): NativeCommandSpec[] {
|
): NativeCommandSpec[] {
|
||||||
return listChatCommandsForConfig(cfg, params)
|
const base = listChatCommandsForConfig(cfg, params)
|
||||||
.filter((command) => command.scope !== "text" && command.nativeName)
|
.filter((command) => command.scope !== "text" && command.nativeName)
|
||||||
.map((command) => ({
|
.map((command) => ({
|
||||||
name: command.nativeName ?? command.key,
|
name: command.nativeName ?? command.key,
|
||||||
@ -130,6 +136,11 @@ export function listNativeCommandSpecsForConfig(
|
|||||||
acceptsArgs: Boolean(command.acceptsArgs),
|
acceptsArgs: Boolean(command.acceptsArgs),
|
||||||
args: command.args,
|
args: command.args,
|
||||||
}));
|
}));
|
||||||
|
const pluginSpecs = getPluginCommandSpecs();
|
||||||
|
if (pluginSpecs.length === 0) return base;
|
||||||
|
const seen = new Set(base.map((spec) => spec.name.toLowerCase()));
|
||||||
|
const extras = pluginSpecs.filter((spec) => !seen.has(spec.name.toLowerCase()));
|
||||||
|
return extras.length > 0 ? [...base, ...extras] : base;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function findCommandByNativeName(name: string): ChatCommandDefinition | undefined {
|
export function findCommandByNativeName(name: string): ChatCommandDefinition | undefined {
|
||||||
|
|||||||
@ -271,9 +271,11 @@ export function listPluginCommands(): Array<{
|
|||||||
export function getPluginCommandSpecs(): Array<{
|
export function getPluginCommandSpecs(): Array<{
|
||||||
name: string;
|
name: string;
|
||||||
description: string;
|
description: string;
|
||||||
|
acceptsArgs: boolean;
|
||||||
}> {
|
}> {
|
||||||
return Array.from(pluginCommands.values()).map((cmd) => ({
|
return Array.from(pluginCommands.values()).map((cmd) => ({
|
||||||
name: cmd.name,
|
name: cmd.name,
|
||||||
description: cmd.description,
|
description: cmd.description,
|
||||||
|
acceptsArgs: Boolean(cmd.acceptsArgs),
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user