openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
40 lines (39 loc) • 2.87 kB
JavaScript
import { l as isRecord } from "../record-coerce-CVPuDqcc.mjs";
import { i as readAgentDatabaseExecutionAuthority, n as readSqliteWorkerRequestCustody } from "../sqlite-worker-request-custody-ZC2aFr6n.mjs";
import { t as readProfileAuthority } from "../user-profile-authority-DQSDeZkW.mjs";
import { n as serveWorkerTasks } from "../worker-task-server-J1vG-XRl.mjs";
import { MessagePort, workerData } from "node:worker_threads";
//#region src/infra/sqlite-writer-admission.worker.ts
function isSharedCell(e) {
return e instanceof Int32Array && e.buffer instanceof SharedArrayBuffer && e.length === 1;
}
function isBrokerRequest(e) {
return isRecord(e) && isSharedCell(e.decision) && isRecord(e.custody) && isSharedCell(e.custody.slot) && isSharedCell(e.custody.request) && (e.executionAuthority === void 0 || isRecord(e.executionAuthority) && isSharedCell(e.executionAuthority.logical) && isSharedCell(e.executionAuthority.native) && typeof e.executionAuthority.physicalIdentity === "string" && typeof e.executionAuthority.incarnation === "string" && (e.executionAuthority.birthtime === void 0 || typeof e.executionAuthority.birthtime === "string"));
}
function isProfileAuthority(e) {
return isRecord(e) && e.generation instanceof SharedArrayBuffer && e.generation.byteLength === 2 * Int32Array.BYTES_PER_ELEMENT && isRecord(e.lifetime) && e.lifetime.revision instanceof SharedArrayBuffer && e.lifetime.revision.byteLength === BigInt64Array.BYTES_PER_ELEMENT && typeof e.lifetime.epoch === "bigint" && Array.isArray(e.entries) && e.entries.every((e) => isRecord(e) && e.state instanceof SharedArrayBuffer && e.state.byteLength === 2 * Int32Array.BYTES_PER_ELEMENT && (e.revision === void 0 || Number.isInteger(e.revision)));
}
serveWorkerTasks((e) => {
if (isRecord(e) && e.kind === "connect" && e.port instanceof MessagePort) {
if (!isRecord(workerData) || !isSharedCell(workerData.admissionLifetime)) throw new Error("SQLite admission service requires its owner lifetime");
const t = workerData.admissionLifetime;
const n = e.port;
n.on("message", (e) => {
if (!isBrokerRequest(e)) {
n.close();
return;
}
const r = Atomics.load(t, 0) === 1 && readSqliteWorkerRequestCustody(e.custody) && (!e.executionAuthority || readAgentDatabaseExecutionAuthority(e.executionAuthority));
Atomics.compareExchange(e.decision, 0, 0, r ? 1 : 2);
Atomics.notify(e.decision, 0);
});
return {
kind: "connected",
lifetime: t
};
}
if (!isRecord(e) || e.kind !== "assert-bound-revision" && e.kind !== "read-current-settled-facts" || !isProfileAuthority(e.authority) || e.authority.entries.some((t) => e.kind === "assert-bound-revision" ? t.revision === void 0 : t.revision !== void 0)) throw new Error("SQLite admission requires a typed profile authority descriptor");
return readProfileAuthority(e.authority);
});
//#endregion
export {};