2026-01-11 12:11:12 +00:00
|
|
|
import type { Command } from "commander";
|
2026-02-18 01:29:02 +00:00
|
|
|
import { resolveAgentWorkspaceDir, resolveDefaultAgentId } from "../agents/agent-scope.js";
|
2026-02-18 01:34:35 +00:00
|
|
|
import type { OpenClawConfig } from "../config/config.js";
|
2026-01-11 12:11:12 +00:00
|
|
|
import { loadConfig } from "../config/config.js";
|
2026-01-18 23:24:17 +00:00
|
|
|
import { createSubsystemLogger } from "../logging/subsystem.js";
|
2026-01-30 03:15:10 +01:00
|
|
|
import { loadOpenClawPlugins } from "./loader.js";
|
2026-02-18 01:34:35 +00:00
|
|
|
import type { PluginLogger } from "./types.js";
|
2026-01-11 12:11:12 +00:00
|
|
|
|
|
|
|
|
const log = createSubsystemLogger("plugins");
|
|
|
|
|
|
2026-01-30 03:15:10 +01:00
|
|
|
export function registerPluginCliCommands(program: Command, cfg?: OpenClawConfig) {
|
2026-01-11 12:11:12 +00:00
|
|
|
const config = cfg ?? loadConfig();
|
2026-01-14 14:31:43 +00:00
|
|
|
const workspaceDir = resolveAgentWorkspaceDir(config, resolveDefaultAgentId(config));
|
2026-01-11 12:11:12 +00:00
|
|
|
const logger: PluginLogger = {
|
|
|
|
|
info: (msg: string) => log.info(msg),
|
|
|
|
|
warn: (msg: string) => log.warn(msg),
|
|
|
|
|
error: (msg: string) => log.error(msg),
|
|
|
|
|
debug: (msg: string) => log.debug(msg),
|
|
|
|
|
};
|
2026-01-30 03:15:10 +01:00
|
|
|
const registry = loadOpenClawPlugins({
|
2026-01-11 12:11:12 +00:00
|
|
|
config,
|
|
|
|
|
workspaceDir,
|
|
|
|
|
logger,
|
|
|
|
|
});
|
|
|
|
|
|
2026-01-21 07:36:39 +00:00
|
|
|
const existingCommands = new Set(program.commands.map((cmd) => cmd.name()));
|
|
|
|
|
|
2026-01-11 12:11:12 +00:00
|
|
|
for (const entry of registry.cliRegistrars) {
|
2026-01-21 07:36:39 +00:00
|
|
|
if (entry.commands.length > 0) {
|
|
|
|
|
const overlaps = entry.commands.filter((command) => existingCommands.has(command));
|
|
|
|
|
if (overlaps.length > 0) {
|
|
|
|
|
log.debug(
|
|
|
|
|
`plugin CLI register skipped (${entry.pluginId}): command already registered (${overlaps.join(
|
|
|
|
|
", ",
|
|
|
|
|
)})`,
|
|
|
|
|
);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-01-11 12:11:12 +00:00
|
|
|
try {
|
|
|
|
|
const result = entry.register({
|
|
|
|
|
program,
|
|
|
|
|
config,
|
|
|
|
|
workspaceDir,
|
|
|
|
|
logger,
|
|
|
|
|
});
|
2026-01-31 16:03:28 +09:00
|
|
|
if (result && typeof result.then === "function") {
|
|
|
|
|
void result.catch((err) => {
|
2026-01-14 14:31:43 +00:00
|
|
|
log.warn(`plugin CLI register failed (${entry.pluginId}): ${String(err)}`);
|
2026-01-11 12:11:12 +00:00
|
|
|
});
|
|
|
|
|
}
|
2026-01-21 07:36:39 +00:00
|
|
|
for (const command of entry.commands) {
|
|
|
|
|
existingCommands.add(command);
|
|
|
|
|
}
|
2026-01-11 12:11:12 +00:00
|
|
|
} catch (err) {
|
2026-01-14 14:31:43 +00:00
|
|
|
log.warn(`plugin CLI register failed (${entry.pluginId}): ${String(err)}`);
|
2026-01-11 12:11:12 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|