openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
121 lines (120 loc) • 4.61 kB
JavaScript
import { a as asOptionalRecord } from "./record-coerce-CVPuDqcc.mjs";
import { n as normalizeAgentId } from "./agent-id-CQjtp2ft.mjs";
import "./session-key-CqAyZeVk.mjs";
import { O as tryResolveAmbientOwnerAgentId } from "./agent-scope-config-DmXgFGKP.mjs";
import { t as createSubsystemLogger } from "./subsystem-BHJiy01V.mjs";
import { r as cloneEnvWithPlatformSemantics } from "./config-env-vars-ByeGN-9N.mjs";
import { r as captureOpenClawStateWorkerContext } from "./openclaw-state-worker-context-DAKL_dnZ.mjs";
import { i as runOpenClawStateWorkerOperation } from "./openclaw-state-worker-store-2LoAv_8B.mjs";
import { n as resolveBundledProviderPolicySurface } from "./provider-public-artifacts-CzxPvvri.mjs";
import { n as resolveDefaultModelForAgent } from "./model-selection-config-Bvf5WqHo.mjs";
import { r as resolveAgentMainSessionKey } from "./main-session-BNL3-mRV.mjs";
import "./model-selection-C2A4Wyix.mjs";
import { l as resolveCronStoreSelectionAsync } from "./paths-CY4cvrR6.mjs";
import { t as runCronRuntimeMutation } from "./runtime-mutation-Datf9FCJ.mjs";
import { c as publishCronJobsStoreMutation, s as noteCronJobsStoreCommit } from "./store-DaG-8LOU.mjs";
import { n as computeJobNextRunAtMs } from "./jobs-scheduling-D3arlYs4.mjs";
import { n as readDefaultProactiveJobReceiptsByKeyAsync } from "./proactive-job-receipt-DquAVkxB.mjs";
import { randomUUID } from "node:crypto";
//#region src/cron/default-proactive-job.ts
/** Setup-owned default automation identity. Startup and config reload never provision jobs. */
const DEFAULT_PROACTIVE_PROMPT = "Review your automation scratch checklist and relevant session context. Do not infer or repeat old tasks from prior conversations. Take useful action only when needed. Use NO_REPLY when there is nothing to tell the user.";
function createDefaultProactiveJob(e, t, n, r = 18e5) {
const i = normalizeAgentId(t);
const a = e.session?.scope === "global" ? "global" : resolveAgentMainSessionKey({
cfg: e,
agentId: i
});
const o = {
id: randomUUID(),
agentId: i,
name: `Proactive check (${i})`,
enabled: true,
createdAtMs: n,
updatedAtMs: n,
schedule: {
kind: "every",
everyMs: r,
anchorMs: n
},
sessionTarget: `session:${a}`,
sessionKey: a,
wakeMode: "now",
idleOnly: true,
payload: {
kind: "agentTurn",
message: DEFAULT_PROACTIVE_PROMPT,
skipIfScratchEmpty: true
},
delivery: {
mode: "announce",
target: "owner"
},
state: {}
};
o.state.nextRunAtMs = computeJobNextRunAtMs(o, n);
return o;
}
/** Resolve setup's provider-owned cadence before publishing a staged first agent. */
function resolveDefaultProactiveCadenceMs(e, t, n = process.env) {
const r = resolveDefaultModelForAgent({
cfg: e,
agentId: t
}).provider;
return resolveBundledProviderPolicySurface(r)?.resolveProactiveCadenceMs?.({
provider: r,
config: e,
env: n
});
}
/** Explicit setup creates the ambient owner's job once; a deletion remains a deletion. */
async function provisionDefaultProactiveJob(e, t, n = {}) {
const r = normalizeAgentId(t);
if (r !== tryResolveAmbientOwnerAgentId(e)) return;
const i = structuredClone(n.recoveryHoldPredicate);
const a = cloneEnvWithPlatformSemantics(n.env ?? process.env);
const o = captureOpenClawStateWorkerContext(n);
const assertCurrent = () => {
o.admission.assertCurrent();
n.commitGuard?.();
};
assertCurrent();
const s = asOptionalRecord(e.cron)?.store;
const c = typeof s === "string" ? s : void 0;
await runOpenClawStateWorkerOperation(o, async () => assertCurrent(), { assertCurrent });
const l = await resolveCronStoreSelectionAsync(c, a, o);
assertCurrent();
if (l.status === "unresolved") {
createSubsystemLogger("cron").warn(l.message);
return;
}
const u = (await readDefaultProactiveJobReceiptsByKeyAsync(l.storeKey, [r], n, {
context: o,
current: true
}))[r];
assertCurrent();
const d = createDefaultProactiveJob(e, r, Date.now(), n.cadenceMs ?? (u ? void 0 : resolveDefaultProactiveCadenceMs(e, r, n.env)));
let f;
await runCronRuntimeMutation({
context: o,
type: "cron.provisionDefaultProactive",
input: {
storeKey: l.storeKey,
agentId: r,
planned: d,
recoveryHoldPredicate: i
},
assertCurrent,
snapshot: {},
onSettled: (e) => {
if (e === "unknown") noteCronJobsStoreCommit();
},
publish: (e) => {
if (e.created) publishCronJobsStoreMutation(e.storeKey);
f = e.job;
}
});
return f;
}
//#endregion
export { resolveDefaultProactiveCadenceMs as i, createDefaultProactiveJob as n, provisionDefaultProactiveJob as r, DEFAULT_PROACTIVE_PROMPT as t };