UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

62 lines (61 loc) • 4.45 kB
import { t as safeParseJson } from "./json-coercion-CaJLaav-.mjs"; import { a as executeSqliteQueryTakeFirstSync, i as executeSqliteQuerySync } from "./kysely-sync-YJo2lmGV.mjs"; import { a as getAdmittedSqlSchemaFacts } from "./sqlite-schema-facts-B1bVE8IB.mjs"; import { a as tableExists } from "./openclaw-state-db-schema-helpers-nImZNMq_.mjs"; import { o as runSqliteReadSnapshotSync } from "./sqlite-transaction-BHHu6P5-.mjs"; import { h as SessionGoalOperationError } from "./openclaw-state-worker-error-C8y1JGQN.mjs"; import { c as SESSION_GOAL_OPERATIONS_TABLE } from "./openclaw-agent-db-schema-compatibility-DMUbFiMM.mjs"; import { t as getSessionKysely } from "./session-accessor.sqlite-scope-helpers-5_tKUULP.mjs"; import { p as readSessionEntryRow } from "./session-accessor.sqlite-entry-read-csOyyi3K.mjs"; import { a as createSessionGoalOperationResult, i as assertSessionGoalOperationTime, n as OPERATION_VALIDITY_MS, o as operationFingerprint, t as MAX_SESSION_RECEIPTS } from "./goals-operation-policy-D3lqf5_y.mjs"; import { n as validateSessionsGoalMutationResult } from "./session-result-validators-DclgBrJW.mjs"; //#region src/config/sessions/goals-operation-receipts.ts /** Read the receipt and its session generation from one admitted snapshot. */ function readSessionGoalOperationInDatabase(e, t) { assertSessionGoalOperationTime(t.operation, Date.now()); const { db: n } = e; return runSqliteReadSnapshotSync(n, () => { if (!getAdmittedSqlSchemaFacts(n)) throw new Error("Goal receipt reads require admitted schema facts"); if (!tableExists(n, "session_goal_operations")) return; const r = readSessionGoalOperationReceipt(n, t.sessionKey, t.expectedSessionId, t.operation); if (r && readSessionEntryRow(e, t.sessionKey)?.entry.sessionId !== t.expectedSessionId) throw new SessionGoalOperationError("session-rebound", "Session changed after this Goal operation; refresh before trying again."); return r; }); } /** The caller has installed the schema before BEGIN; this read participates in its transaction. */ function readSessionGoalOperationReceipt(e, t, n, r) { assertSessionGoalOperationTime(r, Date.now()); const i = executeSqliteQueryTakeFirstSync(e, getSessionKysely(e).selectFrom(SESSION_GOAL_OPERATIONS_TABLE).selectAll().where("session_key", "=", t).where("operation_id", "=", r.operationId)); if (!i) return; if (i.request_fingerprint !== operationFingerprint(r)) throw new SessionGoalOperationError("operation-conflict", "Goal operation ID was already used for a different request."); if (i.session_id !== n) throw new SessionGoalOperationError("session-rebound", "Session changed after this Goal operation; refresh before trying again."); const a = safeParseJson(i.result_json); if (!validateSessionsGoalMutationResult(a) || a.operationId !== r.operationId || a.action !== r.action || a.sessionId !== n || "goalId" in r && a.goalId !== r.goalId || a.goal !== void 0 && a.goal.id !== a.goalId) throw new SessionGoalOperationError("receipt-invalid", "Stored Goal operation receipt is invalid; inspect the session before retrying."); return a; } /** Called only after every Goal/turn/lifecycle write succeeds, in that same transaction. */ function writeSessionGoalOperationReceipt(e, t, n, r, i, a) { const o = Date.now(); assertSessionGoalOperationTime(r, o); const s = getSessionKysely(e); executeSqliteQuerySync(e, s.deleteFrom(SESSION_GOAL_OPERATIONS_TABLE).where("expires_at", "<=", o)); const c = createSessionGoalOperationResult(n, r, i, a); if (executeSqliteQuerySync(e, s.insertInto("session_goal_operations").columns([ "session_key", "operation_id", "session_id", "request_fingerprint", "result_json", "expires_at" ]).expression(s.selectNoFrom((e) => [ e.val(t).as("session_key"), e.val(r.operationId).as("operation_id"), e.val(n).as("session_id"), e.val(operationFingerprint(r)).as("request_fingerprint"), e.val(JSON.stringify(c)).as("result_json"), e.val(r.issuedAtMs + 864e5).as("expires_at") ]).where(s.selectFrom("session_goal_operations").select(({ fn: e }) => e.countAll().as("count")).where("session_key", "=", t), "<", 4096))).numAffectedRows === 0n) throw new SessionGoalOperationError("capacity", "Too many recent Goal operations; wait for older requests to expire before trying again."); return c; } //#endregion export { readSessionGoalOperationReceipt as n, writeSessionGoalOperationReceipt as r, readSessionGoalOperationInDatabase as t };