Files
test-openclaw/src/cli/program/command-registry.ts
T

249 lines
6.7 KiB
TypeScript

import type { Command } from "commander";
import { resolveCliArgvInvocation } from "../argv-invocation.js";
import { shouldRegisterPrimaryCommandOnly } from "../command-registration-policy.js";
import type { ProgramContext } from "./context.js";
import {
type CoreCliCommandDescriptor,
getCoreCliCommandDescriptors,
getCoreCliCommandsWithSubcommands,
} from "./core-command-descriptors.js";
import {
registerCommandGroupByName,
registerCommandGroups,
type CommandGroupEntry,
} from "./register-command-groups.js";
import { registerSubCliCommands } from "./register.subclis.js";
export { getCoreCliCommandDescriptors, getCoreCliCommandsWithSubcommands };
type CommandRegisterParams = {
program: Command;
ctx: ProgramContext;
argv: string[];
};
export type CommandRegistration = {
id: string;
register: (params: CommandRegisterParams) => void;
};
type CoreCliEntry = {
commands: CoreCliCommandDescriptor[];
registerWithParams: (params: CommandRegisterParams) => Promise<void> | void;
};
// Note for humans and agents:
// If you update the list of commands, also check whether they have subcommands
// and set the flag accordingly.
const coreEntries: CoreCliEntry[] = [
{
commands: [
{
name: "setup",
description: "Initialize local config and agent workspace",
hasSubcommands: false,
},
],
registerWithParams: async ({ program }) => {
const mod = await import("./register.setup.js");
mod.registerSetupCommand(program);
},
},
{
commands: [
{
name: "onboard",
description: "Interactive onboarding for gateway, workspace, and skills",
hasSubcommands: false,
},
],
registerWithParams: async ({ program }) => {
const mod = await import("./register.onboard.js");
mod.registerOnboardCommand(program);
},
},
{
commands: [
{
name: "configure",
description:
"Interactive configuration for credentials, channels, gateway, and agent defaults",
hasSubcommands: false,
},
],
registerWithParams: async ({ program }) => {
const mod = await import("./register.configure.js");
mod.registerConfigureCommand(program);
},
},
{
commands: [
{
name: "config",
description:
"Non-interactive config helpers (get/set/unset/file/validate). Default: starts guided setup.",
hasSubcommands: true,
},
],
registerWithParams: async ({ program }) => {
const mod = await import("../config-cli.js");
mod.registerConfigCli(program);
},
},
{
commands: [
{
name: "backup",
description: "Create and verify local backup archives for OpenClaw state",
hasSubcommands: true,
},
],
registerWithParams: async ({ program }) => {
const mod = await import("./register.backup.js");
mod.registerBackupCommand(program);
},
},
{
commands: [
{
name: "doctor",
description: "Health checks + quick fixes for the gateway and channels",
hasSubcommands: false,
},
{
name: "dashboard",
description: "Open the Control UI with your current token",
hasSubcommands: false,
},
{
name: "reset",
description: "Reset local config/state (keeps the CLI installed)",
hasSubcommands: false,
},
{
name: "uninstall",
description: "Uninstall the gateway service + local data (CLI remains)",
hasSubcommands: false,
},
],
registerWithParams: async ({ program }) => {
const mod = await import("./register.maintenance.js");
mod.registerMaintenanceCommands(program);
},
},
{
commands: [
{
name: "message",
description: "Send, read, and manage messages",
hasSubcommands: true,
},
],
registerWithParams: async ({ program, ctx }) => {
const mod = await import("./register.message.js");
mod.registerMessageCommands(program, ctx);
},
},
{
commands: [
{
name: "mcp",
description: "Manage OpenClaw MCP config and channel bridge",
hasSubcommands: true,
},
],
registerWithParams: async ({ program }) => {
const mod = await import("../mcp-cli.js");
mod.registerMcpCli(program);
},
},
{
commands: [
{
name: "agent",
description: "Run one agent turn via the Gateway",
hasSubcommands: false,
},
{
name: "agents",
description: "Manage isolated agents (workspaces, auth, routing)",
hasSubcommands: true,
},
],
registerWithParams: async ({ program, ctx }) => {
const mod = await import("./register.agent.js");
mod.registerAgentCommands(program, {
agentChannelOptions: ctx.agentChannelOptions,
});
},
},
{
commands: [
{
name: "status",
description: "Show channel health and recent session recipients",
hasSubcommands: false,
},
{
name: "health",
description: "Fetch health from the running gateway",
hasSubcommands: false,
},
{
name: "sessions",
description: "List stored conversation sessions",
hasSubcommands: true,
},
{
name: "tasks",
description: "Inspect durable background task state",
hasSubcommands: true,
},
],
registerWithParams: async ({ program }) => {
const mod = await import("./register.status-health-sessions.js");
mod.registerStatusHealthSessionsCommands(program);
},
},
];
function resolveCoreCommandGroups(ctx: ProgramContext, argv: string[]): CommandGroupEntry[] {
return coreEntries.map((entry) => ({
placeholders: entry.commands,
register: async (program) => {
await entry.registerWithParams({ program, ctx, argv });
},
}));
}
export function getCoreCliCommandNames(): string[] {
return getCoreCliCommandDescriptors().map((command) => command.name);
}
export async function registerCoreCliByName(
program: Command,
ctx: ProgramContext,
name: string,
argv: string[] = process.argv,
): Promise<boolean> {
return registerCommandGroupByName(program, resolveCoreCommandGroups(ctx, argv), name);
}
export function registerCoreCliCommands(program: Command, ctx: ProgramContext, argv: string[]) {
const { primary } = resolveCliArgvInvocation(argv);
registerCommandGroups(program, resolveCoreCommandGroups(ctx, argv), {
eager: false,
primary,
registerPrimaryOnly: Boolean(primary && shouldRegisterPrimaryCommandOnly(argv)),
});
}
export function registerProgramCommands(
program: Command,
ctx: ProgramContext,
argv: string[] = process.argv,
) {
registerCoreCliCommands(program, ctx, argv);
registerSubCliCommands(program, argv);
}