UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

158 lines (157 loc) • 5.26 kB
import { g as createNativeSessionBindingLifecycleV2 } from "./agent-harness-session-runtime-DdGYgwgv.mjs"; import { n as readRecord, t as bindingSchema } from "./agentsapi-binding-record-D6aB6bEI.mjs"; //#region extensions/agentsapi/agentsapi-bindings.ts /** Native identity is plugin-owned; shared runtime owns mutation and lease coordination. */ function createAgentsApiBindings(e, t) { const invalidRow = (e) => /* @__PURE__ */ new Error(`Invalid Agents API binding row: ${e}; original state retained. Back up OpenClaw state and restore this binding from a valid pre-upgrade backup before retrying.`); const n = { namespace: "agentsapi-sessions", maxEntries: 1e5, overflowPolicy: "reject-new" }; const r = e.state.openKeyedStoreV2(n); const i = createNativeSessionBindingLifecycleV2({ lookup: r.lookup.bind(r), withCurrent(t) { return e.state.openKeyedStoreV2(n, t); } }, { workerCodec: "agentsapi", readRecord, lease: { staleMs: 65e3, waitMs: 7e4, retryIntervalMs: 1e3, renewIntervalMs: 21e3 }, releaseTtlMs: (e, t) => t.sessionId ? void 0 : 1, errors: { atomicUpdatesRequired: "Agents API bindings require atomic plugin-state updates", invalidRow, lostLease: (e, t) => new Error(`Agents API binding lease lost: ${e}`, { cause: t }), leaseTimeout: (e) => /* @__PURE__ */ new Error(`Timed out waiting for Agents API binding lease: ${e}`), acquisitionRejected: (e) => /* @__PURE__ */ new Error(`Agents API binding acquisition rejected: ${e}`), mutationBlocked: "Agents API binding mutation blocked during an exclusive operation", conditionalDeletionRequired: "Agents API deletion requires conditional plugin-state deletion", deletionChanged: "Agents API binding changed before session deletion", rollbackChanged: "Agents API binding changed before session deletion rollback" } }); const acquisition = (e) => ({ assertCurrent: e, prepareLease: (e, t) => ({ ...e, lease: t }) }); return { withExclusiveMutationFence: i.withExclusiveMutationFence, async withSession(e, t, n, a) { return await i.withMutation(() => i.withLease(e, async () => { t(); const o = i.captureLeaseAssertion(e); let s = true; const bind = async (n, r) => { t(); if (!s) throw new Error("Agents API binding operation is no longer active"); o(); const a = bindingSchema.parse(n); await i.transact(e, (e) => { if (r && (e?.sessionId !== r.sessionId || e.authFingerprint !== r.authFingerprint)) throw new Error("Agents API legacy binding changed during migration; retry with the retained binding"); return { next: { ...a, ...e?.lease ? { lease: e.lease } : {} }, result: void 0 }; }, void 0, t); t(); }; try { const i = readRecord(await r.lookup(e)); t(); o(); let s; if (i?.sessionId && i.authFingerprint) { const assertMigrationCurrent = () => { t(); o(); }; s = await a({ sessionId: i.sessionId, authFingerprint: i.authFingerprint }, assertMigrationCurrent); assertMigrationCurrent(); if (s.sessionId !== i.sessionId) throw new Error("Agents API migration cannot replace the saved native session"); await bind(s, { sessionId: i.sessionId, authFingerprint: i.authFingerprint }); assertMigrationCurrent(); } else if (i?.sessionId && i.configFingerprint) s = { sessionId: i.sessionId, configFingerprint: i.configFingerprint }; return await n(s, bind, o); } finally { s = false; } }, acquisition(t))); }, async reset(e, n, a) { await i.withMutation(() => i.withLease(e, async () => { const o = i.captureLeaseAssertion(e); const assertResetCurrent = () => { n(); o(); }; const s = nativeBinding(readRecord(await r.lookup(e))); assertResetCurrent(); if (s) { if (!t) throw new Error("Agents API native session cleanup is unavailable"); await t.settle(e, s, assertResetCurrent, a); assertResetCurrent(); } await i.transact(e, (e) => ({ next: e?.lease ? { lease: e.lease } : {}, result: void 0 }), void 0, n); }, acquisition(n))); }, async resolveContextResetSessionId(e, t) { const n = await r.lookup(e); const i = readRecord(n); if (n !== void 0 && !i) throw invalidRow(e); return i?.sessionId || !t ? e : t; }, async withSessionDeletion(e, n) { return await i.withDeletion(e.sessionId, { ...acquisition(e.assertCurrent), assertRecordCurrent: () => e.assertCurrent() }, async (r, a) => { const o = nativeBinding(r); if (o) { const n = i.captureLeaseAssertion(e.sessionId); const assertDeletionCurrent = () => { e.assertCurrent(); n(); }; const r = t; if (!r) throw new Error("Agents API native session cleanup is unavailable"); await r.settle(e.sessionId, o, assertDeletionCurrent, e.agentId); assertDeletionCurrent(); } return await n(a); }); } }; } function nativeBinding(e) { return e?.sessionId ? { sessionId: e.sessionId, configFingerprint: e.configFingerprint } : void 0; } //#endregion export { createAgentsApiBindings as t };