openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
131 lines (130 loc) • 6.25 kB
JavaScript
import { a as executeSqliteQueryTakeFirstSync, i as executeSqliteQuerySync, s as getNodeSqliteKysely } from "./kysely-sync-YJo2lmGV.mjs";
import { a as tableExists } from "./openclaw-state-db-schema-helpers-nImZNMq_.mjs";
import { a as deferSqliteWorkerCommitReceipt } from "./sqlite-worker-operation-settlement-OZQM-wtd.mjs";
import { f as requestSqliteWorkerOperationAdmission } from "./sqlite-worker-operation-admission-9E_X-oRy.mjs";
import { m as withExistingOpenClawStateDatabaseReadOnly } from "./openclaw-state-db-readonly-B6YHb1oF.mjs";
import { l as runOpenClawStateWriteTransaction } from "./openclaw-state-db-ARhwdPJg.mjs";
import { p as ensureNodeWorkerPreparedWorkspaceSchema } from "./openclaw-state-db-schema-additive-BOxd1NE5.mjs";
import { t as nodePreparedWorkspacePublication } from "./node-worker-prepared-workspace-publication-BHZAZGva.mjs";
//#region src/node-host/node-worker-prepared-workspace-store.kernel.ts
const TABLE = "node_worker_prepared_workspaces";
function query(e) {
return getNodeSqliteKysely(e);
}
function selectRow(e, t) {
return executeSqliteQueryTakeFirstSync(e, query(e).selectFrom(TABLE).selectAll().where("preparation_key", "=", t));
}
function requireUnchanged(e, t) {
if (!e || JSON.stringify(e) !== JSON.stringify(t)) throw new Error("INVALID_REQUEST: prepared workspace ownership changed");
return e;
}
/** One fresh dedicated node owns one immutable registration and one session binding. */
var NodeWorkerPreparedWorkspaceKernel = class {
constructor(e) {
this.options = e;
}
write(e, t) {
return runOpenClawStateWriteTransaction(({ db: e }) => {
requestSqliteWorkerOperationAdmission({
stage: "transaction",
facts: { kind: "node-worker-journal" }
});
const { result: n, receipt: r } = nodePreparedWorkspacePublication.capture(e, () => t(e));
deferSqliteWorkerCommitReceipt(e, {
kind: "node-prepared-workspace",
receipt: nodePreparedWorkspacePublication.bound(r)
}, r.facts.size === 0 ? "settlement" : "commit");
return n;
}, this.options, { operationLabel: e });
}
find(e) {
return withExistingOpenClawStateDatabaseReadOnly(({ db: t }) => {
if (!tableExists(t, TABLE)) return;
const n = executeSqliteQuerySync(t, query(t).selectFrom(TABLE).selectAll().limit(2)).rows;
if (n.length > 1 || n[0] && n[0].environment_id !== e) throw new Error("INVALID_REQUEST: this prepared node belongs to another environment");
return n[0];
}, this.options);
}
list(e) {
return withExistingOpenClawStateDatabaseReadOnly(({ db: t }) => {
if (!tableExists(t, TABLE)) return [];
return executeSqliteQuerySync(t, query(t).selectFrom(TABLE).selectAll().where("gateway_namespace", "=", e)).rows;
}, this.options) ?? [];
}
register(e) {
return this.write("prepared-workspace.register", (t) => {
ensureNodeWorkerPreparedWorkspaceSchema(t);
const n = executeSqliteQueryTakeFirstSync(t, query(t).selectFrom(TABLE).selectAll().limit(1));
if (n) {
if (n.state !== "available" || n.preparation_key !== e.preparationKey || n.cache_key !== e.cacheKey || n.gateway_namespace !== e.gatewayNamespace || n.environment_id !== e.environmentId || n.workspace_dir !== e.workspaceDir || n.home_dir !== e.homeDir || n.source_manifest_ref !== e.sourceManifestRef || n.prepared_manifest_ref !== e.preparedManifestRef) throw new Error("INVALID_REQUEST: this node already owns a prepared workspace");
return n;
}
const r = {
preparation_key: e.preparationKey,
cache_key: e.cacheKey,
gateway_namespace: e.gatewayNamespace,
environment_id: e.environmentId,
workspace_dir: e.workspaceDir,
home_dir: e.homeDir,
source_manifest_ref: e.sourceManifestRef,
prepared_manifest_ref: e.preparedManifestRef,
state: "available",
session_id: null,
session_key: null,
owner_epoch: null,
created_at_ms: Date.now(),
bound_at_ms: null,
retired_at_ms: null
};
executeSqliteQuerySync(t, query(t).insertInto(TABLE).values(r));
nodePreparedWorkspacePublication.stagePostimages(t, [r]);
return r;
});
}
bind(e) {
return this.write("prepared-workspace.bind", (t) => {
if (!tableExists(t, TABLE)) throw new Error("INVALID_REQUEST: prepared workspace registration is missing");
const n = selectRow(t, e.preparationKey);
if (!n || n.cache_key !== e.cacheKey || n.gateway_namespace !== e.gatewayNamespace || n.environment_id !== e.environmentId) throw new Error("INVALID_REQUEST: prepared workspace registration does not match this environment");
if (n.state === "bound" && n.session_id === e.sessionId && n.session_key === e.sessionKey && n.owner_epoch === e.ownerEpoch) return n;
if (n.state !== "available" || n.session_id !== null || n.session_key !== null || n.owner_epoch !== null || n.bound_at_ms !== null) throw new Error("INVALID_REQUEST: prepared workspace has already been consumed");
const r = {
...n,
state: "bound",
session_id: e.sessionId,
session_key: e.sessionKey,
owner_epoch: e.ownerEpoch,
bound_at_ms: Math.max(Date.now(), n.created_at_ms)
};
executeSqliteQuerySync(t, query(t).updateTable(TABLE).set(r).where("preparation_key", "=", e.preparationKey));
nodePreparedWorkspacePublication.stagePostimages(t, [r]);
return r;
});
}
completeMutation(e) {
this.write("prepared-workspace.finish-mutation", (t) => {
requireUnchanged(selectRow(t, e.preparation_key), e);
executeSqliteQuerySync(t, query(t).updateTable(TABLE).set({ state: "bound" }).where("preparation_key", "=", e.preparation_key));
nodePreparedWorkspacePublication.stagePostimages(t, [{
...e,
state: "bound"
}]);
});
}
retire(e, t = false) {
return this.write("prepared-workspace.retire", (n) => {
const r = requireUnchanged(selectRow(n, e.preparation_key), e);
if (r.state === "retired") return r;
const i = {
...r,
state: t ? "retired" : "retiring",
retired_at_ms: t ? Math.max(Date.now(), r.bound_at_ms ?? r.created_at_ms) : null
};
executeSqliteQuerySync(n, query(n).updateTable(TABLE).set(i).where("preparation_key", "=", r.preparation_key));
nodePreparedWorkspacePublication.stagePostimages(n, [i]);
return i;
});
}
};
//#endregion
export { NodeWorkerPreparedWorkspaceKernel as t };