openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
132 lines (131 loc) • 5.64 kB
JavaScript
import { b as publishSqliteDatabaseAdmission, o as getSqliteDatabaseAdmission } from "./sqlite-database-admission-DQIqro3d.mjs";
import { a as executeSqliteQueryTakeFirstSync, i as executeSqliteQuerySync, p as sqliteStringSet, s as getNodeSqliteKysely } from "./kysely-sync-YJo2lmGV.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 { l as runOpenClawStateWriteTransaction } from "./openclaw-state-db-ARhwdPJg.mjs";
import { a as mentionStoreHeadSchema, i as mentionStoreHeadAdmission, n as MENTION_RETENTION_MS, o as mentionStoreSourceSchema, t as MAX_MENTION_SOURCES } from "./mention-inbox-store-YCmXh6N0.mjs";
//#region src/gateway/mention-inbox-store.worker.ts
const HEAD_KEY = "notifications.mentions.head";
const SOURCE_PREFIX = "notifications.mentions.source.";
const SOURCE_END = "notifications.mentions.source/";
/** The existing machine-state primary key owns lookup; this feature creates no schema. */
function readMentionStoreHead(e) {
const t = getSqliteDatabaseAdmission(e, mentionStoreHeadAdmission);
if (t) return t;
const n = getNodeSqliteKysely(e);
const r = executeSqliteQueryTakeFirstSync(e, n.selectFrom("config_machine_state").select("value_json").where("state_key", "=", HEAD_KEY));
const i = r ? mentionStoreHeadSchema.parse(JSON.parse(r.value_json)) : {
revision: 0,
nextSequence: 0
};
publishSqliteDatabaseAdmission(e, mentionStoreHeadAdmission, i);
return i;
}
function readMentionStoreSnapshot(e, t) {
const n = getNodeSqliteKysely(t);
if (getSqliteDatabaseAdmission(t, mentionStoreHeadAdmission)?.revision === e) return;
const r = executeSqliteQuerySync(t, n.selectFrom("config_machine_state").select(["state_key", "value_json"]).where((e) => e.or([e("state_key", "=", HEAD_KEY), e.and([e("state_key", ">=", SOURCE_PREFIX), e("state_key", "<", SOURCE_END)])])).limit(MAX_MENTION_SOURCES + 2)).rows;
const i = r.find((e) => e.state_key === HEAD_KEY);
const a = i ? mentionStoreHeadSchema.parse(JSON.parse(i.value_json)) : {
revision: 0,
nextSequence: 0
};
const o = getSqliteDatabaseAdmission(t, mentionStoreHeadAdmission);
if (!o || o.revision <= a.revision) publishSqliteDatabaseAdmission(t, mentionStoreHeadAdmission, a);
if (a.revision === e) return;
const s = r.filter((e) => e.state_key !== HEAD_KEY);
if (s.length > 1e4) throw new Error("Mention retention exceeds its source budget");
const c = /* @__PURE__ */ new Set();
const l = /* @__PURE__ */ new Set();
const u = s.map((e) => {
if (e.value_json.length > 32768) throw new Error("Mention source exceeds its record budget");
const t = mentionStoreSourceSchema.parse(JSON.parse(e.value_json));
if (e.state_key !== `${SOURCE_PREFIX}${t.key}` || t.sequence >= a.nextSequence || l.has(t.sequence) || new Set(t.recipients.map(([e]) => e)).size !== t.recipients.length) throw new Error("Invalid mention source identity");
l.add(t.sequence);
for (const [, e] of t.recipients) {
if (e === null) continue;
if (!t.message || c.has(e)) throw new Error("Invalid retained mention");
c.add(e);
}
if (t.message && t.expiresAt !== t.message.content.createdAt + 6048e5) throw new Error("Invalid mention retention window");
return t;
});
if (c.size > 1e4) throw new Error("Mention retention exceeds its item budget");
return {
head: a,
sources: u.toSorted((e, t) => e.sequence - t.sequence)
};
}
/** Called only inside the owning SQLite write transaction. */
function writeMentionStoreChanges(e, t, n) {
if (n.size === 0) return t;
const r = mentionStoreHeadSchema.parse({
...t,
revision: t.revision + 1
});
const i = getNodeSqliteKysely(e);
const a = Date.now();
const writeValue = (t, n) => executeSqliteQuerySync(e, i.insertInto("config_machine_state").values({
state_key: t,
value_json: n,
updated_at_ms: a
}).onConflict((e) => e.column("state_key").doUpdateSet({
value_json: n,
updated_at_ms: a
})));
const o = [];
const flushDeletes = () => {
if (o.length === 0) return;
const t = i.deleteFrom("config_machine_state");
executeSqliteQuerySync(e, o.length === 1 ? t.where("state_key", "=", o[0]) : t.where("state_key", "in", sqliteStringSet(o)));
o.length = 0;
};
for (const [e, t] of n) {
const n = `${SOURCE_PREFIX}${e}`;
if (!t) {
o.push(n);
continue;
}
flushDeletes();
writeValue(n, JSON.stringify(t));
}
flushDeletes();
writeValue(HEAD_KEY, JSON.stringify(r));
publishSqliteDatabaseAdmission(e, mentionStoreHeadAdmission, r);
return r;
}
//#endregion
//#region src/gateway/mention-inbox.worker.ts
const mentionWorkerOperations = { "mentions.mutate": (e, { open: t, stateOptions: n }) => runOpenClawStateWriteTransaction(({ db: t }) => {
requestSqliteWorkerOperationAdmission({
stage: "transaction",
facts: void 0
});
const n = readMentionStoreHead(t);
let r;
if (n.revision !== e.expectedHead.revision || n.nextSequence !== e.expectedHead.nextSequence) {
const e = readMentionStoreSnapshot(-1, t);
if (!e) throw new Error("Mention snapshot is unavailable inside its transaction");
r = {
kind: "conflict",
snapshot: e
};
} else r = {
kind: "committed",
head: writeMentionStoreChanges(t, {
...n,
nextSequence: e.nextSequence
}, new Map(e.changes))
};
requestSqliteWorkerOperationAdmission({
stage: "commit",
facts: void 0
});
if (r.kind === "committed") deferSqliteWorkerCommitReceipt(t, r);
return r;
}, {
database: t(),
...n()
}, { operationLabel: "mentions.mutate" }) };
//#endregion
export { mentionWorkerOperations };