perf(cli): speed up help/config paths and route config get/unset

This commit is contained in:
Peter Steinberger
2026-02-14 00:27:30 +00:00
parent 386bb0c618
commit 4d1461011d
8 changed files with 223 additions and 59 deletions

View File

@@ -23,4 +23,16 @@ describe("program routes", () => {
it("does not match unknown routes", () => {
expect(findRoutedCommand(["definitely-not-real"])).toBeNull();
});
it("returns false for config get route when path argument is missing", async () => {
const route = findRoutedCommand(["config", "get"]);
expect(route).not.toBeNull();
await expect(route?.run(["node", "openclaw", "config", "get", "--json"])).resolves.toBe(false);
});
it("returns false for config unset route when path argument is missing", async () => {
const route = findRoutedCommand(["config", "unset"]);
expect(route).not.toBeNull();
await expect(route?.run(["node", "openclaw", "config", "unset"])).resolves.toBe(false);
});
});

View File

@@ -88,12 +88,58 @@ const routeMemoryStatus: RouteSpec = {
},
};
function getCommandPositionals(argv: string[]): string[] {
const out: string[] = [];
const args = argv.slice(2);
for (const arg of args) {
if (!arg || arg === "--") {
break;
}
if (arg.startsWith("-")) {
continue;
}
out.push(arg);
}
return out;
}
const routeConfigGet: RouteSpec = {
match: (path) => path[0] === "config" && path[1] === "get",
run: async (argv) => {
const positionals = getCommandPositionals(argv);
const pathArg = positionals[2];
if (!pathArg) {
return false;
}
const json = hasFlag(argv, "--json");
const { runConfigGet } = await import("../config-cli.js");
await runConfigGet({ path: pathArg, json });
return true;
},
};
const routeConfigUnset: RouteSpec = {
match: (path) => path[0] === "config" && path[1] === "unset",
run: async (argv) => {
const positionals = getCommandPositionals(argv);
const pathArg = positionals[2];
if (!pathArg) {
return false;
}
const { runConfigUnset } = await import("../config-cli.js");
await runConfigUnset({ path: pathArg });
return true;
},
};
const routes: RouteSpec[] = [
routeHealth,
routeStatus,
routeSessions,
routeAgentsList,
routeMemoryStatus,
routeConfigGet,
routeConfigUnset,
];
export function findRoutedCommand(path: string[]): RouteSpec | null {