openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
60 lines (59 loc) • 3.34 kB
JavaScript
import { l as prepareSqliteQuerySync, n as createSqliteQueryCache, o as getNodeSqliteKysely } from "./kysely-sync-DUWzQvCU.mjs";
import { i as getAdmittedSqliteSchemaFacts, o as getSqliteReadScopeRevision } from "./sqlite-schema-facts-BVf0E_0z.mjs";
import { t as SqliteSchemaMismatchError } from "./sqlite-schema-issues-BnRd2KGO.mjs";
import { i as tableExists } from "./openclaw-state-db-schema-helpers-DKz5xGgh.mjs";
import "./openclaw-state-db-contract-BYv0nUQr.mjs";
import { a as readSqliteUserVersion, n as createNewerSqliteSchemaVersionError } from "./sqlite-user-version-DLMl1A7W.mjs";
import { r as normalizeOpenClawStateSchemaReadError } from "./openclaw-state-db-schema-migration-required-DBia8oMG.mjs";
//#region src/state/openclaw-state-db-schema-version.ts
const CONTENT_VERSION_KEY = "state.schema.contentVersion";
const contentVersionQueries = createSqliteQueryCache((db) => {
const query = prepareSqliteQuerySync(db, () => getNodeSqliteKysely(db).selectFrom("config_machine_state").select("value_json").where("state_key", "=", CONTENT_VERSION_KEY));
let retained;
return {
readRow: () => query().rows[0],
readVersion(readRow = readStateSchemaContentVersionRow) {
const revision = getSqliteReadScopeRevision(db);
if (revision && retained?.revision === revision) return retained.version;
retained = void 0;
const version = parseContentVersion(tableExists(db, "config_machine_state") ? readRow(db)?.value_json : void 0);
if (revision && getSqliteReadScopeRevision(db) === revision) retained = {
revision,
version
};
return version;
}
};
});
/** Content and its marker commit together, even while older readers retain their version floor. */
function readStateSchemaContentVersion(db, published, readRow) {
const schema = getAdmittedSqliteSchemaFacts(db);
const version = published ?? schema?.userVersion ?? readSqliteUserVersion(db);
return Math.max(version, contentVersionQueries(db).readVersion(readRow));
}
function readStateSchemaContentVersionRow(db) {
return contentVersionQueries(db).readRow();
}
function parseContentVersion(valueJson) {
if (valueJson === void 0) return 0;
let contentVersion;
try {
contentVersion = valueJson === null ? null : JSON.parse(valueJson);
} catch (cause) {
throw new SqliteSchemaMismatchError(`Invalid shared state schema content version in ${CONTENT_VERSION_KEY}.`, { cause });
}
if (typeof contentVersion !== "number" || !Number.isSafeInteger(contentVersion) || contentVersion < 0) throw new SqliteSchemaMismatchError(`Invalid shared state schema content version in ${CONTENT_VERSION_KEY}.`);
return contentVersion;
}
function assertSupportedStateSchemaVersion(db, pathname, prepared, readRow) {
try {
const userVersion = prepared?.userVersion ?? getAdmittedSqliteSchemaFacts(db)?.userVersion ?? readSqliteUserVersion(db);
const contentVersion = prepared?.contentVersion ?? (userVersion > 20 ? userVersion : readStateSchemaContentVersion(db, userVersion, readRow));
if (contentVersion > 20) throw createNewerSqliteSchemaVersionError("OpenClaw state database", pathname, contentVersion, 20);
return userVersion;
} catch (error) {
throw normalizeOpenClawStateSchemaReadError(error, pathname);
}
}
//#endregion
export { assertSupportedStateSchemaVersion as n, readStateSchemaContentVersion as r, CONTENT_VERSION_KEY as t };