UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

132 lines (131 loc) • 5.4 kB
import { o as runSqliteReadSnapshotSync } from "../../sqlite-transaction-BHHu6P5-.mjs"; import { a as getSqliteWorkerStateContext } from "../../sqlite-worker-state-context-CR8h__Vh.mjs"; import { i as openOpenClawAgentDatabase, s as runOpenClawAgentWriteTransaction } from "../../openclaw-agent-db-CwXW4hGa.mjs"; import { i as toDatabaseOptions } from "../../session-accessor.sqlite-scope-helpers-5_tKUULP.mjs"; import { a as resolveSqliteTranscriptScope } from "../../session-accessor.sqlite-scope-Cbj2BdoG.mjs"; import { r as readTranscriptMessageIdempotencyKey } from "../../session-transcript-entry-message-BrwStPpF.mjs"; import { n as prepareTranscriptCommitFromSnapshot, t as isCommittedAgentMessage } from "../../transcript-commit-policy-ZzM2zTBP.mjs"; import { l as readTranscriptContextVersionInTransaction } from "../../session-accessor.sqlite-transcript-state-C98y5mme.mjs"; import { f as loadTranscriptReadSnapshotSync } from "../../session-accessor.sqlite-read-DA1Bsaij.mjs"; import { i as loadSessionEntry, t as replaceSessionEntrySync } from "../../session-accessor.sqlite-entry-replace-_LDjfasW.mjs"; import { n as appendTranscriptMessageSnapshotSync } from "../../session-accessor.sqlite-transcript-write.kernel-Bcf2zQj4.mjs"; import { t as bindSqliteWorkerDatabase } from "../../sqlite-worker-binding-B4BB5u5i.mjs"; //#region src/gateway/worker-environments/transcript-commit.kernel.ts function prepareTranscriptCommit(e) { return prepareTranscriptCommitFromSnapshot(e, loadSessionEntry(e.scope), () => loadTranscriptReadSnapshotSync(e.scope)); } /** The caller owns one synchronous transaction around validation, append, and metadata. */ function applyPreparedTranscriptCommit(e, t, n, r, i) { if (!t.result.ok) return t.result; const a = loadSessionEntry(e.scope); if (!a || a.sessionId !== e.scope.sessionId) return { ok: false, reason: "session-not-attached" }; if (a.lifecycleRevision !== t.result.lifecycleRevision) return { ok: false, reason: "invalid-batch" }; const o = openOpenClawAgentDatabase(toDatabaseOptions(resolveSqliteTranscriptScope(e.scope))); const s = readTranscriptContextVersionInTransaction(o, e.scope.sessionId); if (!t.version || s.generation !== t.version.generation || s.rawSeq !== t.version.rawSeq || s.updatedAt !== t.version.updatedAt) return { ok: false, reason: "stale-base-leaf" }; if (t.result.messages.length === e.messages.length) return t.result; const c = t.result.messages.length; if (n.length !== e.messages.length - c || !n.every((t, n) => isCommittedAgentMessage(t) && readTranscriptMessageIdempotencyKey(t)?.trim() === readTranscriptMessageIdempotencyKey(e.messages[c + n])?.trim())) return { ok: false, reason: "invalid-batch" }; const l = [...t.result.messages]; let u = t.parentId; let d = t.nextMessageSeq; for (const t of n) { const n = appendTranscriptMessageSnapshotSync(e.scope, { message: t, cwd: e.cwd, parentId: u, appendIntent: "active-branch", idempotencyLookup: "caller-checked" }, void 0, { scheduleProjectionReconcile: false, onProjectionReconcileNeeded: r }); if (!n.ok || !n.value.result?.appended) throw new Error("Worker transcript message was not persisted", { cause: n.ok ? void 0 : n.error }); const i = n.value.result; u = i.messageId; d += 1; l.push({ appended: true, message: i.message, messageId: i.messageId, messageSeq: d }); } const f = loadSessionEntry(e.scope); if (!f || f.sessionId !== e.scope.sessionId || f.lifecycleRevision !== t.result.lifecycleRevision) throw new Error("Worker transcript session changed inside its transaction"); const p = { ...f, updatedAt: Math.max(f.updatedAt ?? 0, Date.now()) }; replaceSessionEntrySync(e.scope, p); i?.(p); return { ...t.result, messages: l }; } //#endregion //#region src/gateway/worker-environments/transcript-commit.worker.ts /** The canonical agent actor owns the connection and writer admission. */ function bindSqliteWorkerBackend(e, t) { let n; return bindSqliteWorkerDatabase(t.database, { execute(e) { if (e.type === "transcript.prepare") { if (n) throw new Error("Worker transcript batch is already prepared"); const r = { ...e.input, scope: { ...e.input.scope, storePath: t.databasePath, env: getSqliteWorkerStateContext().environment } }; if (e.input.scope.storePath !== t.databasePath) throw new Error("Worker transcript target changed its database owner"); const i = runSqliteReadSnapshotSync(t.database, () => prepareTranscriptCommit(r)); n = { input: r, plan: i }; return i.result; } if (!n) throw new Error("Worker transcript batch was not prepared"); const { input: r, plan: i } = n; n = void 0; return runOpenClawAgentWriteTransaction((n) => { if (n.db !== t.database) throw new Error("Worker transcript lost its canonical connection"); t.admit("transaction"); let a = false; let o; const s = applyPreparedTranscriptCommit(r, i, e.input.messages, () => { a = true; }, (e) => { o = e; }); t.admit("commit"); return { result: s, projectionNeedsReconcile: a, entry: o }; }, toDatabaseOptions(resolveSqliteTranscriptScope(r.scope)), { operationLabel: "worker.transcript.commit" }); }, close() { n = void 0; } }); } //#endregion export { bindSqliteWorkerBackend };