UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

82 lines (81 loc) • 4.22 kB
import { l as isRecord } from "./record-coerce-CVPuDqcc.mjs"; import { n as sha256Hex } from "./node-crypto-B6yuTjmm.mjs"; import { h as SessionGoalOperationError } from "./openclaw-state-worker-error-C8y1JGQN.mjs"; import { a as buildUpdatedSessionGoalStatus, i as buildUpdatedSessionGoalObjective, n as accountSessionGoalUsage, r as buildCreatedSessionGoal, t as SessionGoalTransitionError } from "./goals-transitions-D4AKKFj3.mjs"; import { isDeepStrictEqual } from "node:util"; //#region src/config/sessions/goals-operation-policy.ts const OPERATION_VALIDITY_MS = 864e5; const OPERATION_FUTURE_SKEW_MS = 3e5; const MAX_SESSION_RECEIPTS = 4096; function assertSessionGoalOperationTime(e, t) { if (!Number.isSafeInteger(e.issuedAtMs) || e.issuedAtMs > t + OPERATION_FUTURE_SKEW_MS) throw new SessionGoalOperationError("invalid", "Goal operation time is invalid; refresh and try again."); if (e.issuedAtMs + 864e5 <= t) throw new SessionGoalOperationError("expired", "Goal operation expired; review the current Goal before trying again."); } function operationFingerprint(e) { return sha256Hex(JSON.stringify([ e.issuedAtMs, e.requestFingerprint, e.action, "goalId" in e ? e.goalId : null, "objective" in e ? e.objective : null, "tokenBudget" in e ? e.tokenBudget : null, "note" in e ? e.note : null, ...e.action === "resume" && e.preserveBudget ? ["preserve-budget"] : [], ...e.action === "replace" ? [e.expectedRunId] : [] ])); } /** Apply the same policy used by text commands to the fresh row inside the commit section. */ function applySessionGoalOperation(e, t, n) { try { if (t.action === "start") return buildCreatedSessionGoal(e, t, n); if (!e.goal || e.goal.id !== t.goalId) throw new SessionGoalOperationError("goal-rebound", "Goal changed or was cleared; refresh before trying again."); if (t.action === "replace") { if ((e.lifecycleRunId ?? e.lastRunId) !== t.expectedRunId || e.goal.status === "complete") throw new SessionGoalOperationError("goal-rebound", "Goal or turn changed before interruption was recorded."); return e.goal; } if (t.action === "clear") return; if (t.action === "edit") return buildUpdatedSessionGoalObjective(e, t.objective, n); if (t.action === "resume" && t.replacementSource && ((e.lifecycleRunId ?? e.lastRunId) !== t.replacementSource.runId || !isDeepStrictEqual(e.goal, t.replacementSource.goal))) throw new SessionGoalOperationError("goal-rebound", "The goal or turn changed before replacement resumed; review its current state."); if (t.action === "resume" && t.preserveBudget) { const t = accountSessionGoalUsage(e, n); if (t?.tokenBudget !== void 0 && t.tokensUsed >= t.tokenBudget) throw new SessionGoalOperationError("invalid", "The goal budget was reached. Replacement remains paused; explicitly review its budget before resuming."); } return buildUpdatedSessionGoalStatus(e, { status: t.action === "resume" ? "active" : t.action === "pause" ? "paused" : t.action === "block" ? "blocked" : "complete", note: t.note }, n); } catch (e) { if (e instanceof SessionGoalTransitionError) throw new SessionGoalOperationError("invalid", e.message); throw e; } } function prepareSessionTurnGoalMessage(e, t, n) { if (!t || !n || !isRecord(e) || e.role !== "user") return e; return { ...e, __openclaw: { ...isRecord(e["__openclaw"]) ? e["__openclaw"] : {}, intent: { kind: t.operation.action === "start" ? "session-goal-start" : "session-goal-resume", version: 1, goalId: n, operationId: t.operation.operationId } } }; } function createSessionGoalOperationResult(e, t, n, r) { const i = n?.id ?? ("goalId" in t ? t.goalId : void 0); if (!i) throw new Error("Goal creation did not produce a Goal identity."); return { operationId: t.operationId, action: t.action, sessionId: e, goalId: i, status: r ? "started" : t.action === "clear" ? "cleared" : "updated", ...n ? { goal: n } : {}, ...r ? { runId: r } : {} }; } //#endregion export { createSessionGoalOperationResult as a, assertSessionGoalOperationTime as i, OPERATION_VALIDITY_MS as n, operationFingerprint as o, applySessionGoalOperation as r, prepareSessionTurnGoalMessage as s, MAX_SESSION_RECEIPTS as t };