UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

88 lines (87 loc) • 4.05 kB
import { l as isRecord } from "./record-coerce-CVPuDqcc.mjs"; import { t as safeParseJson } from "./json-coercion-CaJLaav-.mjs"; import { a as getOrLoadSqliteDatabaseAdmissionForPath, b as publishSqliteDatabaseAdmission } from "./sqlite-database-admission-DQIqro3d.mjs"; import { i as executeSqliteQuerySync, s as getNodeSqliteKysely } from "./kysely-sync-YJo2lmGV.mjs"; //#region src/config/io.health-state.kernel.ts const healthReceipt = { name: "config.health", read: (e) => isRecord(e) ? e : void 0, lifetime: "process" }; function readConfigHealthReceipt(e) { return getOrLoadSqliteDatabaseAdmissionForPath(e, healthReceipt, () => ({})); } function publishConfigHealthChanged(e) { publishSqliteDatabaseAdmission(e, healthReceipt, {}); } function parseFingerprint(e) { const t = safeParseJson(e ?? ""); return t && typeof t === "object" ? t : void 0; } function stringifyFingerprint(e) { return e ? JSON.stringify(e) : null; } function selectConfigHealthRows(e) { const t = getNodeSqliteKysely(e).selectFrom("config_health_entries").select([ "config_path", "last_known_good_json", "last_promoted_good_json", "last_observed_suspicious_signature", "updated_at_ms" ]); return executeSqliteQuerySync(e, t.orderBy("config_path", "asc")).rows; } function decodeConfigHealthRows(e) { return { entries: Object.fromEntries(e.map((e) => [e.config_path, { lastKnownGood: parseFingerprint(e.last_known_good_json), lastPromotedGood: parseFingerprint(e.last_promoted_good_json), lastObservedSuspiciousSignature: e.last_observed_suspicious_signature }])) }; } function readConfigHealthStateInDatabase(e) { return decodeConfigHealthRows(selectConfigHealthRows(e)); } /** Omitted fields remain untouched; explicit undefined and null clear their stored value. */ function prepareConfigHealthPatch(e) { return { ...Object.hasOwn(e, "lastKnownGood") ? { last_known_good_json: stringifyFingerprint(e.lastKnownGood) } : {}, ...Object.hasOwn(e, "lastPromotedGood") ? { last_promoted_good_json: stringifyFingerprint(e.lastPromotedGood) } : {}, ...Object.hasOwn(e, "lastObservedSuspiciousSignature") ? { last_observed_suspicious_signature: e.lastObservedSuspiciousSignature ?? null } : {} }; } /** Compare the original read and merge in one write transaction, preserving untouched JSON. */ function patchConfigHealthEntryInDatabase(e, t, n, r, i) { if (r === void 0) return false; const a = getNodeSqliteKysely(e); const o = r === null ? a.insertInto("config_health_entries").values({ config_path: t, last_known_good_json: null, last_promoted_good_json: null, last_observed_suspicious_signature: null, ...n, updated_at_ms: i }).onConflict((e) => e.column("config_path").doNothing()) : a.updateTable("config_health_entries").set({ ...n, updated_at_ms: i }).where("config_path", "=", t).where("last_known_good_json", r.lastKnownGoodJson === null ? "is" : "=", r.lastKnownGoodJson).where("last_promoted_good_json", r.lastPromotedGoodJson === null ? "is" : "=", r.lastPromotedGoodJson).where("last_observed_suspicious_signature", r.suspiciousSignature === null ? "is" : "=", r.suspiciousSignature).where("updated_at_ms", "=", r.updatedAtMs); const s = executeSqliteQuerySync(e, o).numAffectedRows === 1n; if (s) publishConfigHealthChanged(e); return s; } /** The caller owns the transaction; omitted fields and sibling paths remain unchanged. */ function writeConfigHealthPatchInDatabase(e, t, n, r) { executeSqliteQuerySync(e, getNodeSqliteKysely(e).insertInto("config_health_entries").values({ config_path: t, last_known_good_json: null, last_promoted_good_json: null, last_observed_suspicious_signature: null, ...n, updated_at_ms: r }).onConflict((e) => e.column("config_path").doUpdateSet({ ...n, updated_at_ms: r }))); publishConfigHealthChanged(e); } //#endregion export { readConfigHealthStateInDatabase as a, readConfigHealthReceipt as i, prepareConfigHealthPatch as n, writeConfigHealthPatchInDatabase as o, publishConfigHealthChanged as r, patchConfigHealthEntryInDatabase as t };