File size: 18,827 Bytes
f778c12 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 | // Cron status/list/add command registration and create-payload normalization.
import {
normalizeLowercaseStringOrEmpty,
normalizeOptionalString,
readNonBlankString,
} from "@openclaw/normalization-core/string-coerce";
import type { Command } from "commander";
import { theme } from "../../../packages/terminal-core/src/theme.js";
import type { CronJob } from "../../cron/types.js";
import { normalizeHttpWebhookUrl } from "../../cron/webhook-url.js";
import { sanitizeAgentId } from "../../routing/session-key.js";
import { defaultRuntime } from "../../runtime.js";
import type { GatewayRpcOpts } from "../gateway-rpc.js";
import { addGatewayClientOptions, callGatewayFromCli } from "../gateway-rpc.js";
import { CronCliError } from "./cron-cli-error.js";
import { listCronJobsFromGateway } from "./list-jobs.js";
import { createCronOutputCommand } from "./output-mode.js";
import { registerCronMutationOptions } from "./register.cron-options.js";
import { resolveCronCreateScheduleFromArgs } from "./schedule-options.js";
import {
coerceCronDeliveryPreviews,
enrichCronJsonWithStatus,
handleCronCliError,
parseCronCommandArgv,
parseCronCommandEnv,
parseCronIntegerOption,
parseCronNoOutputTimeoutOption,
parseCronStringList,
printCronJson,
printCronList,
warnIfCronSchedulerDisabled,
} from "./shared.js";
import { normalizeCronSessionTargetOption, parseCronThreadIdOption } from "./thread-id-shared.js";
import { readCronPayloadScript, readCronTriggerScript } from "./trigger-options.js";
export function registerCronStatusCommand(cron: Command) {
addGatewayClientOptions(
createCronOutputCommand(cron, "status")
.description("Show automations scheduler status")
.action(async (opts) => {
try {
const res = await callGatewayFromCli("cron.status", opts, {});
printCronJson(res);
} catch (err) {
handleCronCliError(err);
}
}),
);
}
export function registerCronListCommand(cron: Command) {
addGatewayClientOptions(
cron
.command("list")
.description("List automations")
.option("--all", "Include disabled jobs", false)
.option("--agent <id>", "Filter by agent id")
.option("--json", "Output JSON", false)
.action(async (opts) => {
try {
const listParams: { includeDisabled: boolean; agentId?: string } = {
includeDisabled: Boolean(opts.all),
};
const agentId = normalizeOptionalString(opts.agent);
if (typeof opts.agent === "string" && !agentId) {
throw new CronCliError("--agent must not be blank");
}
if (agentId) {
listParams.agentId = sanitizeAgentId(agentId);
}
const res = await listCronJobsFromGateway(opts, listParams);
if (opts.json) {
printCronJson(enrichCronJsonWithStatus(res));
return;
}
const jobs = (res as { jobs?: CronJob[] } | null)?.jobs ?? [];
const deliveryPreviews = coerceCronDeliveryPreviews(res);
printCronList(jobs, defaultRuntime, { deliveryPreviews });
} catch (err) {
handleCronCliError(err);
}
}),
);
}
export function registerCronAddCommand(cron: Command) {
addGatewayClientOptions(
registerCronMutationOptions(
createCronOutputCommand(cron, "add")
.description("Add an automation")
.argument("[scheduleOrName]", "Schedule string, or job name when using --at/--every/--cron")
.argument("[message]", "Agent message when using a positional schedule"),
"add",
)
.option("--declaration-key <key>", "Idempotent declaration identity key")
.option("--disabled", "Create job disabled", false)
.action(
async (
nameArg: string | undefined,
messageArg: string | undefined,
opts: GatewayRpcOpts & Record<string, unknown>,
cmd: Command,
) => {
try {
const hasScheduleFlag =
typeof opts.at === "string" ||
typeof opts.cron === "string" ||
typeof opts.every === "string" ||
typeof opts.onExit === "string" ||
typeof opts.streamCommand === "string";
const positionalSchedule = hasScheduleFlag ? undefined : nameArg;
const schedule = resolveCronCreateScheduleFromArgs({ ...opts, positionalSchedule });
const wakeMode = normalizeOptionalString(opts.wake) ?? "now";
if (wakeMode !== "now" && wakeMode !== "next-heartbeat") {
throw new CronCliError("--wake must be now or next-heartbeat");
}
const rawAgentId = normalizeOptionalString(opts.agent);
const agentId = rawAgentId ? sanitizeAgentId(rawAgentId) : undefined;
const hasAnnounce = Boolean(opts.announce) || opts.deliver === true;
const hasNoDeliver = opts.deliver === false;
const webhookUrl =
typeof opts.webhook === "string" ? normalizeHttpWebhookUrl(opts.webhook) : null;
if (typeof opts.webhook === "string" && !webhookUrl) {
throw new CronCliError("--webhook must be a valid http(s) URL");
}
const hasWebhook = Boolean(webhookUrl);
const deliveryFlagCount = [hasAnnounce, hasNoDeliver, hasWebhook].filter(
Boolean,
).length;
if (deliveryFlagCount > 1) {
throw new CronCliError(
"Choose at most one of --announce, --no-deliver, or --webhook",
);
}
const resolvedPayload = await (async () => {
// Main-session jobs use system events; isolated/current/session jobs use messages.
const systemEvent = normalizeOptionalString(opts.systemEvent) ?? "";
const optionMessage = normalizeOptionalString(opts.message);
const positionalMessage = normalizeOptionalString(messageArg);
const commandShell = normalizeOptionalString(opts.command);
const commandArgv = parseCronCommandArgv(opts.commandArgv);
// File arguments identify exact local paths; trimming can select another file.
const scriptPath = readNonBlankString(opts.script);
if (typeof opts.script === "string" && !scriptPath) {
throw new CronCliError("--script must not be blank");
}
const toolsAllow = parseCronStringList(opts.tools);
if (optionMessage && positionalMessage && optionMessage !== positionalMessage) {
throw new CronCliError(
"Pass the automation message either positionally or with --message, not both.",
);
}
const message = optionMessage ?? positionalMessage ?? "";
if (commandShell && commandArgv) {
throw new CronCliError(
"Pass command payload either with --command or --command-argv, not both.",
);
}
const chosen = [
Boolean(systemEvent),
Boolean(message),
Boolean(commandShell) || Boolean(commandArgv),
Boolean(scriptPath),
].filter(Boolean).length;
if (chosen !== 1) {
throw new CronCliError(
"Choose exactly one payload: --system-event, --message, --command, or --script",
);
}
if (systemEvent) {
return {
kind: "systemEvent" as const,
text: systemEvent,
...(toolsAllow ? { toolsAllow } : {}),
};
}
if (scriptPath) {
if (opts.timeoutSeconds !== undefined) {
throw new CronCliError(
"Use --script-timeout-seconds for script jobs, not --timeout-seconds.",
);
}
const scriptTimeoutSeconds = parseCronIntegerOption(
opts.scriptTimeoutSeconds,
"--script-timeout-seconds",
);
const scriptToolBudget = parseCronIntegerOption(
opts.scriptToolBudget,
"--script-tool-budget",
);
return {
kind: "script" as const,
timeoutSeconds: scriptTimeoutSeconds,
toolBudget: scriptToolBudget,
toolsAllow,
script: await readCronPayloadScript(scriptPath),
};
}
const timeoutSeconds = parseCronIntegerOption(
opts.timeoutSeconds,
"--timeout-seconds",
"non-negative",
);
if (commandShell || commandArgv) {
const noOutputTimeoutSeconds = parseCronNoOutputTimeoutOption(opts);
const outputMaxBytes = parseCronIntegerOption(
opts.outputMaxBytes,
"--output-max-bytes",
);
return {
kind: "command" as const,
argv: commandArgv ?? ["sh", "-lc", commandShell ?? ""],
cwd: normalizeOptionalString(opts.commandCwd),
env: parseCronCommandEnv(opts.commandEnv),
input: typeof opts.commandInput === "string" ? opts.commandInput : undefined,
timeoutSeconds,
noOutputTimeoutSeconds,
outputMaxBytes,
...(toolsAllow ? { toolsAllow } : {}),
};
}
return {
kind: "agentTurn" as const,
message,
model: normalizeOptionalString(opts.model),
fallbacks: parseCronStringList(opts.fallbacks),
thinking: normalizeOptionalString(opts.thinking),
timeoutSeconds,
lightContext: opts.lightContext === true ? true : undefined,
toolsAllow,
};
})();
const sessionSource = cmd.getOptionValueSource("session");
const sessionTargetRaw = normalizeOptionalString(opts.session) ?? "";
const inferredSessionTarget =
resolvedPayload.kind === "agentTurn" ||
resolvedPayload.kind === "command" ||
resolvedPayload.kind === "script"
? "isolated"
: "main";
const sessionTarget =
sessionSource === "cli"
? normalizeCronSessionTargetOption(sessionTargetRaw) || ""
: inferredSessionTarget;
const isCustomSessionTarget =
normalizeLowercaseStringOrEmpty(sessionTarget).startsWith("session:") &&
Boolean(normalizeOptionalString(sessionTarget.slice(8)));
const isIsolatedLikeSessionTarget =
sessionTarget === "isolated" || sessionTarget === "current" || isCustomSessionTarget;
if (sessionTarget !== "main" && !isIsolatedLikeSessionTarget) {
throw new CronCliError("--session must be main, isolated, current, or session:<id>");
}
if (opts.deleteAfterRun && opts.keepAfterRun) {
throw new CronCliError("Choose --delete-after-run or --keep-after-run, not both");
}
if (
sessionTarget === "main" &&
resolvedPayload.kind !== "systemEvent" &&
resolvedPayload.kind !== "script"
) {
throw new CronCliError("Main jobs require --system-event or --script.");
}
if (
resolvedPayload.kind === "script" &&
sessionTarget !== "main" &&
sessionTarget !== "isolated"
) {
throw new CronCliError("Script jobs require --session main or --session isolated.");
}
if (
isIsolatedLikeSessionTarget &&
resolvedPayload.kind !== "agentTurn" &&
resolvedPayload.kind !== "command" &&
resolvedPayload.kind !== "script"
) {
throw new CronCliError("Isolated jobs require --message, --command, or --script.");
}
if (
(opts.announce || typeof opts.deliver === "boolean") &&
(!isIsolatedLikeSessionTarget ||
(resolvedPayload.kind !== "agentTurn" &&
resolvedPayload.kind !== "command" &&
resolvedPayload.kind !== "script"))
) {
throw new CronCliError(
"--announce/--no-deliver require a non-main agentTurn, command, or script session target.",
);
}
const accountId = normalizeOptionalString(opts.account);
const threadId = parseCronThreadIdOption(opts.threadId);
const hasThreadId = typeof threadId === "number";
const hasChatDeliveryTarget =
cmd.getOptionValueSource("channel") === "cli" ||
typeof opts.to === "string" ||
Boolean(accountId) ||
hasThreadId;
if (
hasChatDeliveryTarget &&
(!isIsolatedLikeSessionTarget ||
(resolvedPayload.kind !== "agentTurn" &&
resolvedPayload.kind !== "command" &&
resolvedPayload.kind !== "script"))
) {
throw new CronCliError(
"--channel, --to, --account, and --thread-id require a non-main agentTurn, command, or script job with delivery.",
);
}
if (hasWebhook && hasChatDeliveryTarget) {
throw new CronCliError("--webhook cannot be combined with chat delivery options.");
}
const deliveryMode = hasWebhook
? "webhook"
: isIsolatedLikeSessionTarget &&
(resolvedPayload.kind === "agentTurn" ||
resolvedPayload.kind === "command" ||
resolvedPayload.kind === "script")
? hasAnnounce
? "announce"
: hasNoDeliver
? "none"
: "announce"
: undefined;
const optionName = normalizeOptionalString(opts.name);
const positionalName = hasScheduleFlag ? normalizeOptionalString(nameArg) : undefined;
if (optionName && positionalName && optionName !== positionalName) {
throw new CronCliError(
"Pass the automation name either positionally or with --name, not both.",
);
}
const name = optionName ?? positionalName ?? "";
if (!name) {
throw new CronCliError("Cron job name is required. Pass a name or --name <name>.");
}
const description = normalizeOptionalString(opts.description);
const declarationKey = normalizeOptionalString(opts.declarationKey);
if (typeof opts.declarationKey === "string" && !declarationKey) {
throw new CronCliError("--declaration-key must not be blank");
}
const displayName = normalizeOptionalString(opts.displayName);
if (typeof opts.displayName === "string" && !displayName) {
throw new CronCliError("--display-name must not be blank");
}
const pacingMin = normalizeOptionalString(opts.pacingMin);
const pacingMax = normalizeOptionalString(opts.pacingMax);
if (typeof opts.pacingMin === "string" && !pacingMin) {
throw new CronCliError("--pacing-min must not be blank");
}
if (typeof opts.pacingMax === "string" && !pacingMax) {
throw new CronCliError("--pacing-max must not be blank");
}
const sessionKey = normalizeOptionalString(opts.sessionKey);
const triggerScriptPath = readNonBlankString(opts.triggerScript);
if ((opts.triggerOnce || opts.triggerScript !== undefined) && !triggerScriptPath) {
throw new CronCliError(
`--trigger-${opts.triggerOnce ? "once requires --trigger-script" : "script must not be blank"}`,
);
}
const trigger = triggerScriptPath && {
script: await readCronTriggerScript(triggerScriptPath),
...(opts.triggerOnce ? { once: true } : {}),
};
if (
(resolvedPayload.kind === "agentTurn" || resolvedPayload.kind === "script") &&
!agentId
) {
defaultRuntime.error(
theme.warn(
"No --agent specified; the job will run with the configured default agent. " +
"Specify --agent to choose a specific agent, or set agents.defaults.systemAgent.agentId.",
),
);
}
const params = {
name,
declarationKey,
displayName,
description,
...(declarationKey && cmd.getOptionValueSource("disabled") !== "cli"
? {}
: { enabled: !opts.disabled }),
deleteAfterRun: opts.deleteAfterRun ? true : opts.keepAfterRun ? false : undefined,
agentId,
sessionKey,
schedule,
...(pacingMin || pacingMax
? {
pacing: {
...(pacingMin ? { min: pacingMin } : {}),
...(pacingMax ? { max: pacingMax } : {}),
},
}
: {}),
trigger,
sessionTarget,
wakeMode,
payload: resolvedPayload,
delivery: deliveryMode
? {
mode: deliveryMode,
channel: hasWebhook ? undefined : normalizeOptionalString(opts.channel),
to: hasWebhook ? webhookUrl : normalizeOptionalString(opts.to),
threadId: hasWebhook ? undefined : threadId,
accountId: hasWebhook ? undefined : accountId,
bestEffort: opts.bestEffortDeliver ? true : undefined,
}
: undefined,
};
const res = await callGatewayFromCli("cron.add", opts, params);
printCronJson(res);
await warnIfCronSchedulerDisabled(opts);
} catch (err) {
handleCronCliError(err);
}
},
),
);
}
|