openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
3 lines • 3.86 kB
JavaScript
import{t as safeParseJson}from"./json-coercion-DNNwq4_v.mjs";import{a as executeSqliteQueryTakeFirstSync,i as executeSqliteQuerySync}from"./kysely-sync-R08Pt8qn.mjs";import{a as getAdmittedSqlSchemaFacts}from"./sqlite-schema-facts-CWOeTpUB.mjs";import{a as tableExists}from"./openclaw-state-db-schema-helpers-rYTimzjJ.mjs";import{h as SessionGoalOperationError}from"./openclaw-state-worker-error-Ca5E4dEV.mjs";import{o as runSqliteReadSnapshotSync}from"./sqlite-transaction-CON-dGDv.mjs";import{c as SESSION_GOAL_OPERATIONS_TABLE}from"./openclaw-agent-db-schema-compatibility-CvbEWJYZ.mjs";import{t as getSessionKysely}from"./session-accessor.sqlite-scope-helpers-_Zyjm1vW.mjs";import{f as readSessionEntryRow}from"./session-accessor.sqlite-entry-read-DBsgM_K3.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-Dqo_4G9A.mjs";import{n as validateSessionsGoalMutationResult}from"./session-result-validators-Cr1cDZ59.mjs";
//!src/config/sessions/goals-operation-receipts.ts
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})}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}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}export{readSessionGoalOperationReceipt as n,writeSessionGoalOperationReceipt as r,readSessionGoalOperationInDatabase as t};