openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
394 lines (393 loc) • 15 kB
JavaScript
import { c as normalizeOptionalLowercaseString, l as normalizeOptionalString, m as readNonBlankString, o as normalizeLowercaseStringOrEmpty } from "./string-coerce-CCIiB4H7.mjs";
import { V as timestampMsToIsoString } from "./number-coercion-D6u-4YD8.mjs";
import { l as isRecord } from "./record-coerce-CVPuDqcc.mjs";
import { t as parseBoolean } from "./boolean-coercion-B6407eeV.mjs";
import { m as normalizeTrimmedStringList } from "./string-normalization-DLmfYZXI.mjs";
import "./utils-C8YECn2Q.mjs";
import { p as sanitizeAgentId } from "./session-key-CqAyZeVk.mjs";
import { r as normalizeOptionalAccountId } from "./account-id-BqKT3GuY.mjs";
import { a as normalizeCronToolsAllowExecTarget, d as snapshotOwnCronRecord, i as normalizeCronScheduledToolPolicy, o as normalizeCronToolsAllowExecTargetRequirement } from "./scheduled-tool-policy-C_pucbBp.mjs";
import { a as resolveCronCurrentSessionTarget, t as assertSafeCronSessionTargetId } from "./session-target-C-4MLah6.mjs";
import { n as normalizeCronRuntimeAuthority } from "./runtime-authority-Ca4Ng1RL.mjs";
import { n as normalizeCronToolsAllowProvenance } from "./tools-allow-provenance-Bf4KsdEb.mjs";
import { n as shouldDefaultCronDeliveryToAnnounce } from "./delivery-defaults-K6HKsSBE.mjs";
import { a as TimeoutSecondsFieldSchema, c as parseOptionalField, o as TrimmedNonEmptyStringFieldSchema, s as parseDeliveryInput, t as inferCronJobName } from "./normalize-4WPiSYN5.mjs";
import { n as normalizeCronInferenceBinding } from "./inference-binding-WWjqX5o7.mjs";
import { t as parseAbsoluteTimeMs } from "./parse-CSwpxE6Q.mjs";
import { t as coerceFiniteScheduleNumber } from "./schedule-number-uhcarNZz.mjs";
import { r as resolveDefaultCronStaggerMs, t as normalizeCronStaggerMs } from "./stagger-Bj0_2RgJ.mjs";
import { a as normalizeCronStreamBatching } from "./stream-schedule-BFX6oe0X.mjs";
//#region src/cron/normalize-payload.ts
function normalizeTrimmedStringArray(e) {
if (Array.isArray(e)) {
const t = normalizeTrimmedStringList(e);
if (t.length === 0 && e.length > 0) return;
return t;
}
return e === null ? null : void 0;
}
function normalizeCommandEnv(e) {
if (!isRecord(e)) throw new Error("command env must be an object with non-blank keys and string values");
const t = [];
for (const [n, r] of Object.entries(e)) {
const e = normalizeOptionalString(n);
if (!e || typeof r !== "string") throw new Error("command env must be an object with non-blank keys and string values");
t.push([e, r]);
}
return Object.fromEntries(t);
}
function normalizeCronCommandArgv(e) {
if (!Array.isArray(e) || e.length === 0) return;
if (e.some((e) => typeof e !== "string" || e.length === 0)) return;
return [...e];
}
function hasAgentTurnOnlyPayloadHint(e) {
return "model" in e || "fallbacks" in e || "thinking" in e || "timeoutSeconds" in e || typeof e.lightContext === "boolean" || typeof e.skipIfScratchEmpty === "boolean" || typeof e.includeReasoning === "boolean" || typeof e.allowUnsafeExternalContent === "boolean";
}
function normalizeCronPayload(e) {
const t = snapshotOwnCronRecord(e);
const n = normalizeLowercaseStringOrEmpty(t.kind);
if (n === "agentturn") t.kind = "agentTurn";
else if (n === "systemevent") t.kind = "systemEvent";
else if (n) t.kind = n;
if (typeof t.message === "string" && !t.message.trim()) t.message = "";
if (typeof t.text === "string") t.text = normalizeOptionalString(t.text) ?? "";
if (typeof t.script === "string") t.script = t.script.trim();
for (const e of ["model", "thinking"]) if (e in t && t[e] !== null) {
const n = parseOptionalField(TrimmedNonEmptyStringFieldSchema, t[e]);
if (n !== void 0) t[e] = n;
else delete t[e];
}
for (const e of [
"timeoutSeconds",
"noOutputTimeoutSeconds",
"outputMaxBytes",
"toolBudget"
]) {
if (!(e in t) || e === "timeoutSeconds" && t[e] === null) continue;
const n = parseOptionalField(TimeoutSecondsFieldSchema, t[e]);
const r = e === "outputMaxBytes" || e === "toolBudget";
if (n !== void 0 && (!r || n > 0)) t[e] = r ? Math.floor(n) : n;
else delete t[e];
}
for (const e of ["fallbacks", "toolsAllow"]) if (e in t) {
const n = normalizeTrimmedStringArray(t[e]);
if (n !== void 0) t[e] = n;
else delete t[e];
}
if ("argv" in t) {
const e = normalizeCronCommandArgv(t.argv);
if (e) t.argv = e;
else delete t.argv;
}
if ("cwd" in t) {
const e = readNonBlankString(t.cwd);
if (e !== void 0) t.cwd = e;
else delete t.cwd;
}
if ("env" in t) t.env = normalizeCommandEnv(t.env);
if ("input" in t && typeof t.input !== "string") delete t.input;
if ("allowUnsafeExternalContent" in t && typeof t.allowUnsafeExternalContent !== "boolean") delete t.allowUnsafeExternalContent;
if (!("kind" in t) && typeof t.text === "string" && hasAgentTurnOnlyPayloadHint(t)) {
t.kind = "agentTurn";
t.message = t.text;
}
const r = t.kind;
if (r === "systemEvent" || r === "agentTurn" || r === "command" || r === "script") {
if (r !== "systemEvent") delete t.text;
else delete t.timeoutSeconds;
if (r !== "agentTurn") {
delete t.message;
delete t.model;
delete t.fallbacks;
delete t.thinking;
delete t.lightContext;
delete t.skipIfScratchEmpty;
delete t.includeReasoning;
delete t.allowUnsafeExternalContent;
}
if (r !== "command") {
delete t.argv;
delete t.cwd;
delete t.env;
delete t.input;
delete t.noOutputTimeoutSeconds;
delete t.outputMaxBytes;
}
if (r !== "script") {
delete t.script;
delete t.toolBudget;
}
}
return { ...t };
}
//#endregion
//#region src/cron/normalize.ts
/** Normalizes cron create/patch payloads before validation and persistence. */
function coerceSchedule(e) {
const t = snapshotOwnCronRecord(e);
const n = normalizeLowercaseStringOrEmpty(t.kind);
const r = n === "at" || n === "every" || n === "cron" || n === "on-exit" || n === "stream" || n === "event" ? n : void 0;
const i = normalizeOptionalString(t.expr) ?? "";
const a = normalizeOptionalString(t.tz);
const o = readNonBlankString(t.command) ?? "";
const s = normalizeCronCommandArgv(t.command);
const c = readNonBlankString(t.cwd) ?? "";
const l = normalizeOptionalLowercaseString(t.mode);
const u = typeof t.match === "string" ? t.match : void 0;
const d = coerceFiniteScheduleNumber(t.everyMs);
const f = coerceFiniteScheduleNumber(t.anchorMs);
const p = normalizeOptionalString(t.at) ?? "";
const m = p ? parseAbsoluteTimeMs(p) : null;
if (r) t.kind = r;
const h = m !== null ? timestampMsToIsoString(m) : void 0;
if (p) t.at = h ?? p;
if (i) t.expr = i;
else if ("expr" in t) delete t.expr;
if (a) t.tz = a;
else if ("tz" in t) delete t.tz;
if (d !== void 0 && d >= 1) t.everyMs = Math.floor(d);
if (f !== void 0 && f >= 0) t.anchorMs = Math.floor(f);
if (r === "stream" && s) t.command = s;
else if (o) t.command = o;
else if ("command" in t) delete t.command;
if (c) t.cwd = c;
else if ("cwd" in t) delete t.cwd;
if (r === "stream") {
if (l === "line" || l === "match") t.mode = l;
else if ("mode" in t) delete t.mode;
if (u !== void 0) t.match = u;
else if ("match" in t) delete t.match;
normalizeCronStreamBatching(t);
}
const g = normalizeCronStaggerMs(t.staggerMs);
if (g !== void 0) t.staggerMs = g;
else if (t.kind === "cron" && t.staggerMs !== void 0) throw new TypeError("schedule.staggerMs must be a valid number or non-negative integer string");
else if ("staggerMs" in t) delete t.staggerMs;
if (r) {
if (r !== "at") delete t.at;
if (r !== "every") {
delete t.everyMs;
delete t.anchorMs;
}
if (r !== "cron") {
delete t.expr;
delete t.tz;
delete t.staggerMs;
}
}
if (t.kind !== "on-exit" && t.kind !== "stream") {
delete t.command;
delete t.cwd;
}
if (t.kind !== "stream") {
delete t.mode;
delete t.match;
delete t.batchMs;
delete t.maxBatchBytes;
}
if (t.kind !== "event") {
delete t.source;
delete t.options;
}
return { ...t };
}
function coerceTrigger(e) {
const t = snapshotOwnCronRecord(e);
const n = typeof t.script === "string" ? t.script.trim() : "";
const r = parseBoolean(t.once);
return {
script: n,
...r !== void 0 ? { once: r } : {}
};
}
function coerceDelivery(e) {
const t = snapshotOwnCronRecord(e);
const n = parseDeliveryInput(t);
if (n.mode !== void 0) t.mode = n.mode;
for (const e of [
"channel",
"to",
"threadId",
"accountId"
]) {
if (t[e] === null) continue;
if (n[e] !== void 0) t[e] = n[e];
else delete t[e];
}
if ("failureDestination" in t && t.failureDestination !== null) {
if (isRecord(t.failureDestination)) t.failureDestination = coerceFailureDestination(t.failureDestination);
else delete t.failureDestination;
}
if ("completionDestination" in t && t.completionDestination !== null) {
const e = isRecord(t.completionDestination) ? coerceCompletionDestination(t.completionDestination) : null;
if (e) t.completionDestination = e;
else delete t.completionDestination;
}
return { ...t };
}
function coerceCompletionDestination(e) {
const t = snapshotOwnCronRecord(e);
const n = normalizeOptionalLowercaseString(t.mode);
const r = normalizeOptionalString(t.to);
if (n !== "webhook") return null;
return {
mode: n,
...r ? { to: r } : {}
};
}
function coerceFailureDestination(e) {
const t = snapshotOwnCronRecord(e);
for (const [e, n] of [
["channel", normalizeOptionalLowercaseString],
["to", normalizeOptionalString],
["accountId", normalizeOptionalString]
]) {
if (t[e] == null) continue;
const r = n(t[e]);
if (r) t[e] = r;
else delete t[e];
}
if (t.mode != null) {
const e = normalizeOptionalLowercaseString(t.mode);
if (e === "announce" || e === "webhook") t.mode = e;
else delete t.mode;
}
return { ...t };
}
function normalizeSessionTarget(e) {
if (typeof e !== "string") return;
const t = e.trim();
const n = normalizeLowercaseStringOrEmpty(t);
if (n === "main" || n === "isolated" || n === "current") return n;
if (n.startsWith("session:")) return `session:${assertSafeCronSessionTargetId(t.slice(8))}`;
}
function normalizeWakeMode(e) {
const t = normalizeOptionalLowercaseString(e);
if (t === "now" || t === "next-heartbeat") return t;
}
/** Normalizes raw cron job input without deciding whether create-time defaults apply. */
function normalizeCronJobInput(e, t = { applyDefaults: false }) {
if (!isRecord(e)) return null;
const n = snapshotOwnCronRecord(e);
const r = snapshotOwnCronRecord(n);
for (const e of ["declarationKey", "displayName"]) if (e in n && typeof n[e] === "string") {
const t = n[e].trim();
if (t) r[e] = t;
else delete r[e];
}
if (isRecord(n.owner)) {
const e = snapshotOwnCronRecord(n.owner);
const t = normalizeOptionalString(e.agentId);
const i = normalizeOptionalString(e.sessionKey);
const a = normalizeOptionalAccountId(typeof e.accountId === "string" ? e.accountId : void 0);
if (t || i || a) r.owner = {
...t ? { agentId: sanitizeAgentId(t) } : {},
...i ? { sessionKey: i } : {},
...a ? { accountId: a } : {}
};
else delete r.owner;
}
for (const [e, t] of [
["scheduledToolPolicy", normalizeCronScheduledToolPolicy],
["toolsAllowProvenance", normalizeCronToolsAllowProvenance],
["toolsAllowExecTarget", normalizeCronToolsAllowExecTarget],
["toolsAllowExecTargetRequirement", normalizeCronToolsAllowExecTargetRequirement],
["runtimeAuthority", normalizeCronRuntimeAuthority],
["inferenceBinding", normalizeCronInferenceBinding]
]) {
if (!(e in n)) continue;
const i = t(n[e]);
if (i) r[e] = i;
else delete r[e];
}
if (n.runtimeAuthorityRecoveryRequired === true) r.runtimeAuthorityRecoveryRequired = true;
else delete r.runtimeAuthorityRecoveryRequired;
for (const e of ["agentId", "sessionKey"]) if (typeof n[e] === "string") {
const t = n[e].trim();
if (t) r[e] = e === "agentId" ? sanitizeAgentId(t) : t;
else delete r[e];
}
if ("enabled" in n) {
const e = parseBoolean(n.enabled);
if (e !== void 0) r.enabled = e;
}
if ("sessionTarget" in n) {
const e = normalizeSessionTarget(n.sessionTarget);
if (e) r.sessionTarget = e;
else delete r.sessionTarget;
}
if ("wakeMode" in n) {
const e = normalizeWakeMode(n.wakeMode);
if (e) r.wakeMode = e;
else delete r.wakeMode;
}
if (isRecord(n.schedule)) r.schedule = coerceSchedule(n.schedule);
if (isRecord(n.payload)) r.payload = normalizeCronPayload(n.payload);
if ("trigger" in n && n.trigger !== null) {
if (isRecord(n.trigger)) r.trigger = coerceTrigger(n.trigger);
else delete r.trigger;
}
if (isRecord(n.delivery)) r.delivery = coerceDelivery(n.delivery);
if (t.applyDefaults) {
if (!r.wakeMode) r.wakeMode = "now";
if (typeof r.enabled !== "boolean") r.enabled = true;
if ((typeof r.name !== "string" || !r.name.trim()) && isRecord(r.schedule) && isRecord(r.payload)) r.name = inferCronJobName({
schedule: r.schedule,
payload: r.payload
});
else if (typeof r.name === "string") {
const e = r.name.trim();
if (e) r.name = e;
}
if (!r.sessionTarget && isRecord(r.payload)) {
const e = typeof r.payload.kind === "string" ? r.payload.kind : "";
if (e === "systemEvent") r.sessionTarget = "main";
else if (e === "agentTurn") r.sessionTarget = "current";
else if (e === "command" || e === "script") r.sessionTarget = "isolated";
}
const e = typeof r.sessionTarget === "string" ? r.sessionTarget : void 0;
const n = t.sessionContext?.sessionKey ?? (typeof r.sessionKey === "string" ? r.sessionKey : void 0);
const i = resolveCronCurrentSessionTarget({
sessionTarget: e,
sessionKey: n
});
if (i !== void 0) {
r.sessionTarget = i;
if (r.sessionTarget !== "isolated" && e === "current" && n?.trim()) r.sessionKey = assertSafeCronSessionTargetId(n);
} else delete r.sessionTarget;
if ("schedule" in r && isRecord(r.schedule) && r.schedule.kind === "at" && !("deleteAfterRun" in r)) r.deleteAfterRun = true;
if ("schedule" in r && isRecord(r.schedule) && r.schedule.kind === "cron") {
const e = r.schedule;
if (e.staggerMs === void 0) {
const t = typeof e.expr === "string" ? e.expr : "";
const n = resolveDefaultCronStaggerMs(t);
if (n !== void 0) e.staggerMs = n;
}
}
const a = isRecord(r.payload) ? r.payload : null;
const o = a && typeof a.kind === "string" ? a.kind : "";
const s = typeof r.sessionTarget === "string" ? r.sessionTarget : "";
if (!("delivery" in r && r.delivery !== void 0) && shouldDefaultCronDeliveryToAnnounce({
payloadKind: o,
sessionTarget: s
})) r.delivery = { mode: "announce" };
}
return { ...r };
}
/** Normalizes a raw cron create request and applies create-time defaults. */
function normalizeCronJobCreate(e, t) {
return normalizeCronJobInput(e, {
applyDefaults: true,
...t
});
}
/** Normalizes a raw cron patch request without filling omitted fields. */
function normalizeCronJobPatch(e, t) {
return normalizeCronJobInput(e, {
applyDefaults: false,
...t
});
}
//#endregion
export { normalizeCronJobInput as n, normalizeCronJobPatch as r, normalizeCronJobCreate as t };