mirror of
https://github.com/openclaw/openclaw.git
synced 2026-04-16 03:31:10 +00:00
refactor: share plugin cli registration helpers
This commit is contained in:
@@ -1,10 +1,6 @@
|
||||
import { Command } from "commander";
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import {
|
||||
loadValidatedConfigForPluginRegistration,
|
||||
registerSubCliByName,
|
||||
registerSubCliCommands,
|
||||
} from "./register.subclis.js";
|
||||
import { registerSubCliByName, registerSubCliCommands } from "./register.subclis.js";
|
||||
|
||||
const { acpAction, registerAcpCli } = vi.hoisted(() => {
|
||||
const action = vi.fn();
|
||||
@@ -30,15 +26,9 @@ const { registerQaCli } = vi.hoisted(() => ({
|
||||
}),
|
||||
}));
|
||||
|
||||
const configModule = vi.hoisted(() => ({
|
||||
loadConfig: vi.fn(),
|
||||
readConfigFileSnapshot: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock("../acp-cli.js", () => ({ registerAcpCli }));
|
||||
vi.mock("../nodes-cli.js", () => ({ registerNodesCli }));
|
||||
vi.mock("../qa-cli.js", () => ({ registerQaCli }));
|
||||
vi.mock("../../config/config.js", () => configModule);
|
||||
|
||||
describe("registerSubCliCommands", () => {
|
||||
const originalArgv = process.argv;
|
||||
@@ -64,8 +54,6 @@ describe("registerSubCliCommands", () => {
|
||||
acpAction.mockClear();
|
||||
registerNodesCli.mockClear();
|
||||
nodesAction.mockClear();
|
||||
configModule.loadConfig.mockReset();
|
||||
configModule.readConfigFileSnapshot.mockReset();
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
@@ -99,28 +87,6 @@ describe("registerSubCliCommands", () => {
|
||||
expect(registerAcpCli).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("returns null for plugin registration when the config snapshot is invalid", async () => {
|
||||
configModule.readConfigFileSnapshot.mockResolvedValueOnce({
|
||||
valid: false,
|
||||
config: { plugins: { load: { paths: ["/tmp/evil"] } } },
|
||||
});
|
||||
|
||||
await expect(loadValidatedConfigForPluginRegistration()).resolves.toBeNull();
|
||||
expect(configModule.loadConfig).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("loads validated config for plugin registration when the snapshot is valid", async () => {
|
||||
const loadedConfig = { plugins: { enabled: true } };
|
||||
configModule.readConfigFileSnapshot.mockResolvedValueOnce({
|
||||
valid: true,
|
||||
config: loadedConfig,
|
||||
});
|
||||
configModule.loadConfig.mockReturnValueOnce(loadedConfig);
|
||||
|
||||
await expect(loadValidatedConfigForPluginRegistration()).resolves.toBe(loadedConfig);
|
||||
expect(configModule.loadConfig).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it("re-parses argv for lazy subcommands", async () => {
|
||||
const program = createRegisteredProgram(["node", "openclaw", "nodes", "list"], "openclaw");
|
||||
|
||||
|
||||
@@ -1,10 +1,14 @@
|
||||
import type { Command } from "commander";
|
||||
import type { OpenClawConfig } from "../../config/config.js";
|
||||
import { resolveCliArgvInvocation } from "../argv-invocation.js";
|
||||
import {
|
||||
shouldEagerRegisterSubcommands,
|
||||
shouldRegisterPrimarySubcommandOnly,
|
||||
} from "../command-registration-policy.js";
|
||||
import {
|
||||
buildCommandGroupEntries,
|
||||
defineImportedProgramCommandGroupSpecs,
|
||||
type CommandGroupDescriptorSpec,
|
||||
} from "./command-group-descriptors.js";
|
||||
import {
|
||||
registerCommandGroupByName,
|
||||
registerCommandGroups,
|
||||
@@ -20,305 +24,197 @@ export { getSubCliCommandsWithSubcommands };
|
||||
|
||||
type SubCliRegistrar = (program: Command) => Promise<void> | void;
|
||||
|
||||
type SubCliEntry = SubCliDescriptor & {
|
||||
register: SubCliRegistrar;
|
||||
};
|
||||
|
||||
export const loadValidatedConfigForPluginRegistration =
|
||||
async (): Promise<OpenClawConfig | null> => {
|
||||
const mod = await import("../../config/config.js");
|
||||
const snapshot = await mod.readConfigFileSnapshot();
|
||||
if (!snapshot.valid) {
|
||||
return null;
|
||||
}
|
||||
return mod.loadConfig();
|
||||
};
|
||||
async function registerSubCliWithPluginCommands(
|
||||
program: Command,
|
||||
registerSubCli: () => Promise<void>,
|
||||
pluginCliPosition: "before" | "after",
|
||||
) {
|
||||
const { registerPluginCliCommandsFromValidatedConfig } = await import("../../plugins/cli.js");
|
||||
if (pluginCliPosition === "before") {
|
||||
await registerPluginCliCommandsFromValidatedConfig(program);
|
||||
}
|
||||
await registerSubCli();
|
||||
if (pluginCliPosition === "after") {
|
||||
await registerPluginCliCommandsFromValidatedConfig(program);
|
||||
}
|
||||
}
|
||||
|
||||
// Note for humans and agents:
|
||||
// If you update the list of commands, also check whether they have subcommands
|
||||
// and set the flag accordingly.
|
||||
const entries: SubCliEntry[] = [
|
||||
{
|
||||
name: "acp",
|
||||
description: "Agent Control Protocol tools",
|
||||
hasSubcommands: true,
|
||||
register: async (program) => {
|
||||
const mod = await import("../acp-cli.js");
|
||||
mod.registerAcpCli(program);
|
||||
const entrySpecs: readonly CommandGroupDescriptorSpec<SubCliRegistrar>[] = [
|
||||
...defineImportedProgramCommandGroupSpecs([
|
||||
{
|
||||
commandNames: ["acp"],
|
||||
loadModule: () => import("../acp-cli.js"),
|
||||
exportName: "registerAcpCli",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "gateway",
|
||||
description: "Run, inspect, and query the WebSocket Gateway",
|
||||
hasSubcommands: true,
|
||||
register: async (program) => {
|
||||
const mod = await import("../gateway-cli.js");
|
||||
mod.registerGatewayCli(program);
|
||||
{
|
||||
commandNames: ["gateway"],
|
||||
loadModule: () => import("../gateway-cli.js"),
|
||||
exportName: "registerGatewayCli",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "daemon",
|
||||
description: "Gateway service (legacy alias)",
|
||||
hasSubcommands: true,
|
||||
register: async (program) => {
|
||||
const mod = await import("../daemon-cli.js");
|
||||
mod.registerDaemonCli(program);
|
||||
{
|
||||
commandNames: ["daemon"],
|
||||
loadModule: () => import("../daemon-cli.js"),
|
||||
exportName: "registerDaemonCli",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "logs",
|
||||
description: "Tail gateway file logs via RPC",
|
||||
hasSubcommands: false,
|
||||
register: async (program) => {
|
||||
const mod = await import("../logs-cli.js");
|
||||
mod.registerLogsCli(program);
|
||||
{
|
||||
commandNames: ["logs"],
|
||||
loadModule: () => import("../logs-cli.js"),
|
||||
exportName: "registerLogsCli",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "system",
|
||||
description: "System events, heartbeat, and presence",
|
||||
hasSubcommands: true,
|
||||
register: async (program) => {
|
||||
const mod = await import("../system-cli.js");
|
||||
mod.registerSystemCli(program);
|
||||
{
|
||||
commandNames: ["system"],
|
||||
loadModule: () => import("../system-cli.js"),
|
||||
exportName: "registerSystemCli",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "models",
|
||||
description: "Discover, scan, and configure models",
|
||||
hasSubcommands: true,
|
||||
register: async (program) => {
|
||||
const mod = await import("../models-cli.js");
|
||||
mod.registerModelsCli(program);
|
||||
{
|
||||
commandNames: ["models"],
|
||||
loadModule: () => import("../models-cli.js"),
|
||||
exportName: "registerModelsCli",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "approvals",
|
||||
description: "Manage exec approvals (gateway or node host)",
|
||||
hasSubcommands: true,
|
||||
register: async (program) => {
|
||||
const mod = await import("../exec-approvals-cli.js");
|
||||
mod.registerExecApprovalsCli(program);
|
||||
{
|
||||
commandNames: ["approvals"],
|
||||
loadModule: () => import("../exec-approvals-cli.js"),
|
||||
exportName: "registerExecApprovalsCli",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "nodes",
|
||||
description: "Manage gateway-owned node pairing and node commands",
|
||||
hasSubcommands: true,
|
||||
register: async (program) => {
|
||||
const mod = await import("../nodes-cli.js");
|
||||
mod.registerNodesCli(program);
|
||||
{
|
||||
commandNames: ["nodes"],
|
||||
loadModule: () => import("../nodes-cli.js"),
|
||||
exportName: "registerNodesCli",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "devices",
|
||||
description: "Device pairing + token management",
|
||||
hasSubcommands: true,
|
||||
register: async (program) => {
|
||||
const mod = await import("../devices-cli.js");
|
||||
mod.registerDevicesCli(program);
|
||||
{
|
||||
commandNames: ["devices"],
|
||||
loadModule: () => import("../devices-cli.js"),
|
||||
exportName: "registerDevicesCli",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "node",
|
||||
description: "Run and manage the headless node host service",
|
||||
hasSubcommands: true,
|
||||
register: async (program) => {
|
||||
const mod = await import("../node-cli.js");
|
||||
mod.registerNodeCli(program);
|
||||
{
|
||||
commandNames: ["node"],
|
||||
loadModule: () => import("../node-cli.js"),
|
||||
exportName: "registerNodeCli",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "sandbox",
|
||||
description: "Manage sandbox containers for agent isolation",
|
||||
hasSubcommands: true,
|
||||
register: async (program) => {
|
||||
const mod = await import("../sandbox-cli.js");
|
||||
mod.registerSandboxCli(program);
|
||||
{
|
||||
commandNames: ["sandbox"],
|
||||
loadModule: () => import("../sandbox-cli.js"),
|
||||
exportName: "registerSandboxCli",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "tui",
|
||||
description: "Open a terminal UI connected to the Gateway",
|
||||
hasSubcommands: false,
|
||||
register: async (program) => {
|
||||
const mod = await import("../tui-cli.js");
|
||||
mod.registerTuiCli(program);
|
||||
{
|
||||
commandNames: ["tui"],
|
||||
loadModule: () => import("../tui-cli.js"),
|
||||
exportName: "registerTuiCli",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "cron",
|
||||
description: "Manage cron jobs via the Gateway scheduler",
|
||||
hasSubcommands: true,
|
||||
register: async (program) => {
|
||||
const mod = await import("../cron-cli.js");
|
||||
mod.registerCronCli(program);
|
||||
{
|
||||
commandNames: ["cron"],
|
||||
loadModule: () => import("../cron-cli.js"),
|
||||
exportName: "registerCronCli",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "dns",
|
||||
description: "DNS helpers for wide-area discovery (Tailscale + CoreDNS)",
|
||||
hasSubcommands: true,
|
||||
register: async (program) => {
|
||||
const mod = await import("../dns-cli.js");
|
||||
mod.registerDnsCli(program);
|
||||
{
|
||||
commandNames: ["dns"],
|
||||
loadModule: () => import("../dns-cli.js"),
|
||||
exportName: "registerDnsCli",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "docs",
|
||||
description: "Search the live OpenClaw docs",
|
||||
hasSubcommands: false,
|
||||
register: async (program) => {
|
||||
const mod = await import("../docs-cli.js");
|
||||
mod.registerDocsCli(program);
|
||||
{
|
||||
commandNames: ["docs"],
|
||||
loadModule: () => import("../docs-cli.js"),
|
||||
exportName: "registerDocsCli",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "qa",
|
||||
description: "Run QA scenarios and launch the private QA debugger UI",
|
||||
hasSubcommands: true,
|
||||
register: async (program) => {
|
||||
const mod = await import("../qa-cli.js");
|
||||
mod.registerQaCli(program);
|
||||
{
|
||||
commandNames: ["qa"],
|
||||
loadModule: () => import("../qa-cli.js"),
|
||||
exportName: "registerQaCli",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "hooks",
|
||||
description: "Manage internal agent hooks",
|
||||
hasSubcommands: true,
|
||||
register: async (program) => {
|
||||
const mod = await import("../hooks-cli.js");
|
||||
mod.registerHooksCli(program);
|
||||
{
|
||||
commandNames: ["hooks"],
|
||||
loadModule: () => import("../hooks-cli.js"),
|
||||
exportName: "registerHooksCli",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "webhooks",
|
||||
description: "Webhook helpers and integrations",
|
||||
hasSubcommands: true,
|
||||
register: async (program) => {
|
||||
const mod = await import("../webhooks-cli.js");
|
||||
mod.registerWebhooksCli(program);
|
||||
{
|
||||
commandNames: ["webhooks"],
|
||||
loadModule: () => import("../webhooks-cli.js"),
|
||||
exportName: "registerWebhooksCli",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "qr",
|
||||
description: "Generate mobile pairing QR/setup code",
|
||||
hasSubcommands: false,
|
||||
register: async (program) => {
|
||||
const mod = await import("../qr-cli.js");
|
||||
mod.registerQrCli(program);
|
||||
{
|
||||
commandNames: ["qr"],
|
||||
loadModule: () => import("../qr-cli.js"),
|
||||
exportName: "registerQrCli",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "clawbot",
|
||||
description: "Legacy clawbot command aliases",
|
||||
hasSubcommands: true,
|
||||
register: async (program) => {
|
||||
const mod = await import("../clawbot-cli.js");
|
||||
mod.registerClawbotCli(program);
|
||||
{
|
||||
commandNames: ["clawbot"],
|
||||
loadModule: () => import("../clawbot-cli.js"),
|
||||
exportName: "registerClawbotCli",
|
||||
},
|
||||
},
|
||||
]),
|
||||
{
|
||||
name: "pairing",
|
||||
description: "Secure DM pairing (approve inbound requests)",
|
||||
hasSubcommands: true,
|
||||
commandNames: ["pairing"],
|
||||
register: async (program) => {
|
||||
// Initialize plugins before registering pairing CLI.
|
||||
// The pairing CLI calls listPairingChannels() at registration time,
|
||||
// which requires the plugin registry to be populated with channel plugins.
|
||||
const { registerPluginCliCommands } = await import("../../plugins/cli.js");
|
||||
const config = await loadValidatedConfigForPluginRegistration();
|
||||
if (config) {
|
||||
await registerPluginCliCommands(program, config);
|
||||
}
|
||||
const mod = await import("../pairing-cli.js");
|
||||
mod.registerPairingCli(program);
|
||||
await registerSubCliWithPluginCommands(
|
||||
program,
|
||||
async () => {
|
||||
const mod = await import("../pairing-cli.js");
|
||||
mod.registerPairingCli(program);
|
||||
},
|
||||
"before",
|
||||
);
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "plugins",
|
||||
description: "Manage OpenClaw plugins and extensions",
|
||||
hasSubcommands: true,
|
||||
commandNames: ["plugins"],
|
||||
register: async (program) => {
|
||||
const mod = await import("../plugins-cli.js");
|
||||
mod.registerPluginsCli(program);
|
||||
const { registerPluginCliCommands } = await import("../../plugins/cli.js");
|
||||
const config = await loadValidatedConfigForPluginRegistration();
|
||||
if (config) {
|
||||
await registerPluginCliCommands(program, config);
|
||||
}
|
||||
await registerSubCliWithPluginCommands(
|
||||
program,
|
||||
async () => {
|
||||
const mod = await import("../plugins-cli.js");
|
||||
mod.registerPluginsCli(program);
|
||||
},
|
||||
"after",
|
||||
);
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "channels",
|
||||
description: "Manage connected chat channels (Telegram, Discord, etc.)",
|
||||
hasSubcommands: true,
|
||||
register: async (program) => {
|
||||
const mod = await import("../channels-cli.js");
|
||||
mod.registerChannelsCli(program);
|
||||
...defineImportedProgramCommandGroupSpecs([
|
||||
{
|
||||
commandNames: ["channels"],
|
||||
loadModule: () => import("../channels-cli.js"),
|
||||
exportName: "registerChannelsCli",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "directory",
|
||||
description: "Lookup contact and group IDs (self, peers, groups) for supported chat channels",
|
||||
hasSubcommands: true,
|
||||
register: async (program) => {
|
||||
const mod = await import("../directory-cli.js");
|
||||
mod.registerDirectoryCli(program);
|
||||
{
|
||||
commandNames: ["directory"],
|
||||
loadModule: () => import("../directory-cli.js"),
|
||||
exportName: "registerDirectoryCli",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "security",
|
||||
description: "Security tools and local config audits",
|
||||
hasSubcommands: true,
|
||||
register: async (program) => {
|
||||
const mod = await import("../security-cli.js");
|
||||
mod.registerSecurityCli(program);
|
||||
{
|
||||
commandNames: ["security"],
|
||||
loadModule: () => import("../security-cli.js"),
|
||||
exportName: "registerSecurityCli",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "secrets",
|
||||
description: "Secrets runtime reload controls",
|
||||
hasSubcommands: true,
|
||||
register: async (program) => {
|
||||
const mod = await import("../secrets-cli.js");
|
||||
mod.registerSecretsCli(program);
|
||||
{
|
||||
commandNames: ["secrets"],
|
||||
loadModule: () => import("../secrets-cli.js"),
|
||||
exportName: "registerSecretsCli",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "skills",
|
||||
description: "List and inspect available skills",
|
||||
hasSubcommands: true,
|
||||
register: async (program) => {
|
||||
const mod = await import("../skills-cli.js");
|
||||
mod.registerSkillsCli(program);
|
||||
{
|
||||
commandNames: ["skills"],
|
||||
loadModule: () => import("../skills-cli.js"),
|
||||
exportName: "registerSkillsCli",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "update",
|
||||
description: "Update OpenClaw and inspect update channel status",
|
||||
hasSubcommands: true,
|
||||
register: async (program) => {
|
||||
const mod = await import("../update-cli.js");
|
||||
mod.registerUpdateCli(program);
|
||||
{
|
||||
commandNames: ["update"],
|
||||
loadModule: () => import("../update-cli.js"),
|
||||
exportName: "registerUpdateCli",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "completion",
|
||||
description: "Generate shell completion script",
|
||||
hasSubcommands: false,
|
||||
register: async (program) => {
|
||||
const mod = await import("../completion-cli.js");
|
||||
mod.registerCompletionCli(program);
|
||||
{
|
||||
commandNames: ["completion"],
|
||||
loadModule: () => import("../completion-cli.js"),
|
||||
exportName: "registerCompletionCli",
|
||||
},
|
||||
},
|
||||
]),
|
||||
];
|
||||
|
||||
function resolveSubCliCommandGroups(): CommandGroupEntry[] {
|
||||
return entries.map((entry) => ({
|
||||
placeholders: [entry],
|
||||
register: entry.register,
|
||||
}));
|
||||
return buildCommandGroupEntries(getSubCliEntryDescriptors(), entrySpecs, (register) => register);
|
||||
}
|
||||
|
||||
export function getSubCliEntries(): ReadonlyArray<SubCliDescriptor> {
|
||||
|
||||
Reference in New Issue
Block a user