mirror of
https://github.com/openclaw/openclaw.git
synced 2026-06-05 14:52:55 +00:00
perf(cli): cache routed command imports
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import type { Command } from "commander";
|
||||
import { createLazyImportLoader } from "../../shared/lazy-promise.js";
|
||||
import { resolveCliArgvInvocation } from "../argv-invocation.js";
|
||||
import { resolveCliCommandPathPolicy } from "../command-path-policy.js";
|
||||
import {
|
||||
@@ -29,12 +30,17 @@ export type SubCliRegistrationContext = {
|
||||
purpose?: "runtime" | "completion";
|
||||
};
|
||||
|
||||
type PluginCliModule = typeof import("../../plugins/cli.js");
|
||||
type SubCliRegistrar = (
|
||||
program: Command,
|
||||
argv: string[],
|
||||
context: SubCliRegistrationContext,
|
||||
) => Promise<void> | void;
|
||||
|
||||
const pluginCliLoader = createLazyImportLoader<PluginCliModule>(
|
||||
() => import("../../plugins/cli.js"),
|
||||
);
|
||||
|
||||
function shouldRegisterGatewayRunOnly(name: string, argv: string[]): boolean {
|
||||
if (name !== "gateway") {
|
||||
return false;
|
||||
@@ -68,12 +74,12 @@ async function registerSubCliWithPluginCommands(
|
||||
!invocation.hasHelpOrVersion &&
|
||||
resolveCliCommandPathPolicy(invocation.commandPath).loadPlugins !== "never";
|
||||
if (pluginCliPosition === "before" && shouldRegisterPluginCommands) {
|
||||
const { registerPluginCliCommandsFromValidatedConfig } = await import("../../plugins/cli.js");
|
||||
const { registerPluginCliCommandsFromValidatedConfig } = await pluginCliLoader.load();
|
||||
await registerPluginCliCommandsFromValidatedConfig(program);
|
||||
}
|
||||
await registerSubCli();
|
||||
if (pluginCliPosition === "after" && shouldRegisterPluginCommands) {
|
||||
const { registerPluginCliCommandsFromValidatedConfig } = await import("../../plugins/cli.js");
|
||||
const { registerPluginCliCommandsFromValidatedConfig } = await pluginCliLoader.load();
|
||||
await registerPluginCliCommandsFromValidatedConfig(program);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,6 +24,7 @@ type AgentsListCommandModule = typeof import("../../commands/agents.commands.lis
|
||||
type ConfigCliModule = typeof import("../config-cli.js");
|
||||
type ModelsListCommandModule = typeof import("../../commands/models/list.list-command.js");
|
||||
type ModelsStatusCommandModule = typeof import("../../commands/models/list.status-command.js");
|
||||
type TasksJsonCommandModule = typeof import("../../commands/tasks-json.js");
|
||||
|
||||
export type RoutedCommandDefinition<TParse extends RouteArgParser<unknown>> = {
|
||||
parseArgs: TParse;
|
||||
@@ -51,6 +52,9 @@ const modelsListCommandLoader = createLazyImportLoader<ModelsListCommandModule>(
|
||||
const modelsStatusCommandLoader = createLazyImportLoader<ModelsStatusCommandModule>(
|
||||
() => import("../../commands/models/list.status-command.js"),
|
||||
);
|
||||
const tasksJsonCommandLoader = createLazyImportLoader<TasksJsonCommandModule>(
|
||||
() => import("../../commands/tasks-json.js"),
|
||||
);
|
||||
|
||||
function loadConfigCli(): Promise<ConfigCliModule> {
|
||||
return configCliLoader.load();
|
||||
@@ -68,6 +72,10 @@ function loadModelsStatusCommand(): Promise<ModelsStatusCommandModule> {
|
||||
return modelsStatusCommandLoader.load();
|
||||
}
|
||||
|
||||
function loadTasksJsonCommand(): Promise<TasksJsonCommandModule> {
|
||||
return tasksJsonCommandLoader.load();
|
||||
}
|
||||
|
||||
export const routedCommandDefinitions = {
|
||||
health: defineRoutedCommand({
|
||||
parseArgs: parseHealthRouteArgs,
|
||||
@@ -148,14 +156,14 @@ export const routedCommandDefinitions = {
|
||||
"tasks-list": defineRoutedCommand({
|
||||
parseArgs: parseTasksListRouteArgs,
|
||||
runParsedArgs: async (args) => {
|
||||
const { tasksListJsonCommand } = await import("../../commands/tasks-json.js");
|
||||
const { tasksListJsonCommand } = await loadTasksJsonCommand();
|
||||
await tasksListJsonCommand(args, defaultRuntime);
|
||||
},
|
||||
}),
|
||||
"tasks-audit": defineRoutedCommand({
|
||||
parseArgs: parseTasksAuditRouteArgs,
|
||||
runParsedArgs: async (args) => {
|
||||
const { tasksAuditJsonCommand } = await import("../../commands/tasks-json.js");
|
||||
const { tasksAuditJsonCommand } = await loadTasksJsonCommand();
|
||||
await tasksAuditJsonCommand(args, defaultRuntime);
|
||||
},
|
||||
}),
|
||||
|
||||
Reference in New Issue
Block a user