Files
openclaw/src/cli/system-cli.ts
Peter Steinberger 00d8d7ead0 refactor: extract normalization core package
Extract shared normalization/coercion helpers into private @openclaw/normalization-core workspace package while preserving existing plugin SDK helper subpaths.\n\nAlso keeps direct normalization-core imports internal, wires UI/build/loader resolution, and replaces the slow PR network CodeQL lane with a fast added-line boundary scan while retaining full CodeQL for scheduled/manual runs.\n\nVerification: local moved tests, plugin SDK boundary tests, extension loader tests, agents-support shard, UI build/test, build artifacts, lint, workflow guards, autoreview, and GitHub CI passed on PR head 963d893715.
2026-05-31 01:33:00 +01:00

152 lines
4.6 KiB
TypeScript

import { normalizeOptionalString } from "@openclaw/normalization-core/string-coerce";
import type { Command } from "commander";
import { formatDocsLink } from "../../packages/terminal-core/src/links.js";
import { theme } from "../../packages/terminal-core/src/theme.js";
import { danger } from "../globals.js";
import { defaultRuntime } from "../runtime.js";
import { formatCliCommand } from "./command-format.js";
import type { GatewayRpcOpts } from "./gateway-rpc.js";
import { addGatewayClientOptions, callGatewayFromCli } from "./gateway-rpc.js";
type SystemEventOpts = GatewayRpcOpts & {
text?: string;
mode?: string;
sessionKey?: string;
json?: boolean;
};
type SystemGatewayOpts = GatewayRpcOpts & { json?: boolean };
const normalizeWakeMode = (raw: unknown) => {
const mode = normalizeOptionalString(raw) ?? "";
if (!mode) {
return "next-heartbeat" as const;
}
if (mode === "now" || mode === "next-heartbeat") {
return mode;
}
throw new Error("--mode must be now or next-heartbeat");
};
async function runSystemGatewayCommand(
opts: SystemGatewayOpts,
action: () => Promise<unknown>,
successText?: string,
): Promise<void> {
try {
const result = await action();
if (opts.json || successText === undefined) {
defaultRuntime.writeJson(result);
} else {
defaultRuntime.log(successText);
}
} catch (err) {
defaultRuntime.error(danger(String(err)));
defaultRuntime.exit(1);
}
}
export function registerSystemCli(program: Command) {
const system = program
.command("system")
.description("System tools (events, heartbeat, presence)")
.addHelpText(
"after",
() =>
`\n${theme.muted("Docs:")} ${formatDocsLink("/cli/system", "docs.openclaw.ai/cli/system")}\n`,
);
addGatewayClientOptions(
system
.command("event")
.description("Enqueue a system event and optionally trigger a heartbeat")
.requiredOption("--text <text>", "System event text")
.option("--mode <mode>", "Wake mode (now|next-heartbeat)", "next-heartbeat")
.option(
"--session-key <sessionKey>",
"Target a specific session for the event (defaults to the agent's main session)",
)
.option("--json", "Output JSON", false),
).action(async (opts: SystemEventOpts) => {
await runSystemGatewayCommand(
opts,
async () => {
const text = normalizeOptionalString(opts.text) ?? "";
if (!text) {
throw new Error(
`--text is required. Example: ${formatCliCommand('openclaw system event --text "deploy finished"')}.`,
);
}
const mode = normalizeWakeMode(opts.mode);
const sessionKey = normalizeOptionalString(opts.sessionKey);
return await callGatewayFromCli(
"wake",
opts,
sessionKey ? { mode, text, sessionKey } : { mode, text },
{ expectFinal: false },
);
},
"ok",
);
});
const heartbeat = system.command("heartbeat").description("Heartbeat controls");
addGatewayClientOptions(
heartbeat
.command("last")
.description("Show the last heartbeat event")
.option("--json", "Output JSON", false),
).action(async (opts: SystemGatewayOpts) => {
await runSystemGatewayCommand(opts, async () => {
return await callGatewayFromCli("last-heartbeat", opts, undefined, {
expectFinal: false,
});
});
});
addGatewayClientOptions(
heartbeat
.command("enable")
.description("Enable heartbeats")
.option("--json", "Output JSON", false),
).action(async (opts: SystemGatewayOpts) => {
await runSystemGatewayCommand(opts, async () => {
return await callGatewayFromCli(
"set-heartbeats",
opts,
{ enabled: true },
{ expectFinal: false },
);
});
});
addGatewayClientOptions(
heartbeat
.command("disable")
.description("Disable heartbeats")
.option("--json", "Output JSON", false),
).action(async (opts: SystemGatewayOpts) => {
await runSystemGatewayCommand(opts, async () => {
return await callGatewayFromCli(
"set-heartbeats",
opts,
{ enabled: false },
{ expectFinal: false },
);
});
});
addGatewayClientOptions(
system
.command("presence")
.description("List system presence entries")
.option("--json", "Output JSON", false),
).action(async (opts: SystemGatewayOpts) => {
await runSystemGatewayCommand(opts, async () => {
return await callGatewayFromCli("system-presence", opts, undefined, {
expectFinal: false,
});
});
});
}