Files
openclaw/src/cli/system-cli.ts
Kaspre 4ddd942f5f feat(wake): expose typed sessionKey on wake protocol + system event CLI
Adds an optional sessionKey to the WakeParamsSchema and threads it through
the gateway wake handler, CronService.wake(), and the underlying timer.wake()
ops so callers can target a specific session for async-task completion
relays instead of always hitting the agent's main session.

Also adds --session-key to `openclaw system event`.

The schema rejects empty/non-string sessionKey at the gateway boundary;
mismatched session keys (a key that does not belong to the resolving agent)
fall back to the agent's main session inside resolveCronSessionKey, which
is the existing safety path.

Refs #52305 (companion to PR #50818, which closes the related cron-run
remap slice at internal enqueue sites). Doesn't depend on #50818.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-11 17:24:30 +01:00

152 lines
4.5 KiB
TypeScript

import type { Command } from "commander";
import { danger } from "../globals.js";
import { defaultRuntime } from "../runtime.js";
import { normalizeOptionalString } from "../shared/string-coerce.js";
import { formatDocsLink } from "../terminal/links.js";
import { theme } from "../terminal/theme.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,
});
});
});
}