openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
105 lines (104 loc) • 4.52 kB
JavaScript
import { n as normalizeConfigMachineStateKey, r as publishConfigMachineStateRow } from "./config-machine-state-row-XCKCxfWg.mjs";
import { a as executeSqliteQueryTakeFirstSync, i as executeSqliteQuerySync, s as getNodeSqliteKysely } from "./kysely-sync-YJo2lmGV.mjs";
import { l as runOpenClawStateWriteTransaction } from "./openclaw-state-db-ARhwdPJg.mjs";
import { l as withAuthProfileCredentialWrite } from "./user-model-account-authority-CRj924IP.mjs";
//#region src/state/config-machine-state-write.ts
function serializeStateValue(e) {
const t = JSON.stringify(e);
if (t === void 0) throw new Error("config machine state value must be JSON-serializable");
return t;
}
function publishMachineStateWrite(e, t, n, r, i) {
return t === "authProfiles.store" ? withAuthProfileCredentialWrite(e, n === void 0 ? void 0 : JSON.parse(n), r, i) : r();
}
function upsertConfigMachineState(e, t, n, r) {
publishMachineStateWrite(e, t, n, () => executeSqliteQuerySync(e, getNodeSqliteKysely(e).insertInto("config_machine_state").values({
state_key: t,
value_json: n,
updated_at_ms: r
}).onConflict((e) => e.column("state_key").doUpdateSet({
value_json: n,
updated_at_ms: r
}))));
publishConfigMachineStateRow(e, t, {
value_json: n,
updated_at_ms: r
});
}
function writeConfigMachineState(e, t, n = {}) {
const r = normalizeConfigMachineStateKey(e);
const i = serializeStateValue(t);
const a = Date.now();
runOpenClawStateWriteTransaction(({ db: e }) => upsertConfigMachineState(e, r, i, a), n, { operationLabel: "config-machine-state.write" });
}
/** Write an already-normalized key on the caller's admitted connection. */
function writeConfigMachineStateInDatabase(e, t, n) {
const r = Date.now();
upsertConfigMachineState(e, t, serializeStateValue(n), r);
return r;
}
function updateConfigMachineState(e, t, n = {}) {
const r = normalizeConfigMachineStateKey(e);
const i = Date.now();
return runOpenClawStateWriteTransaction(({ db: e }) => updateConfigMachineStateInDatabase(e, r, t, i), n, { operationLabel: "config-machine-state.update" });
}
function updateConfigMachineStateInDatabase(e, t, n, r) {
const i = getNodeSqliteKysely(e);
const a = executeSqliteQueryTakeFirstSync(e, i.selectFrom("config_machine_state").select("value_json").where("state_key", "=", t));
const o = n(a ? JSON.parse(a.value_json) : void 0);
if (o === void 0) {
if (a) publishMachineStateWrite(e, t, void 0, () => executeSqliteQuerySync(e, i.deleteFrom("config_machine_state").where("state_key", "=", t)));
publishConfigMachineStateRow(e, t, void 0);
return;
}
upsertConfigMachineState(e, t, serializeStateValue(o), r);
return o;
}
/** Delete one machine-state value, reporting whether a stored value existed. */
function deleteConfigMachineState(e, t = {}) {
const n = normalizeConfigMachineStateKey(e);
return runOpenClawStateWriteTransaction((e) => {
const t = getNodeSqliteKysely(e.db);
const r = publishMachineStateWrite(e.db, n, void 0, () => executeSqliteQuerySync(e.db, t.deleteFrom("config_machine_state").where("state_key", "=", n)));
publishConfigMachineStateRow(e.db, n, void 0);
return (r.numAffectedRows ?? 0n) > 0n;
}, t, { operationLabel: "config-machine-state.delete" });
}
/** Import retired config values without replacing newer canonical database state. */
function importConfigMachineState(e, t = {}) {
if (e.length === 0) return {
imported: [],
kept: []
};
const n = e.map(([e, t]) => ({
key: normalizeConfigMachineStateKey(e),
valueJson: serializeStateValue(t)
}));
const r = Date.now();
return runOpenClawStateWriteTransaction((e) => {
const t = getNodeSqliteKysely(e.db);
const i = [];
const a = [];
for (const o of n) {
if (!publishMachineStateWrite(e.db, o.key, o.valueJson, () => executeSqliteQueryTakeFirstSync(e.db, t.insertInto("config_machine_state").values({
state_key: o.key,
value_json: o.valueJson,
updated_at_ms: r
}).onConflict((e) => e.column("state_key").doNothing()).returning("state_key")), (e) => e !== void 0)) {
a.push(o.key);
continue;
}
publishConfigMachineStateRow(e.db, o.key, {
value_json: o.valueJson,
updated_at_ms: r
});
i.push(o.key);
}
return {
imported: i,
kept: a
};
}, t, { operationLabel: "config-machine-state.import" });
}
//#endregion
export { writeConfigMachineState as a, updateConfigMachineStateInDatabase as i, importConfigMachineState as n, writeConfigMachineStateInDatabase as o, updateConfigMachineState as r, deleteConfigMachineState as t };