UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

1 lines • 2.38 kB
import{o as e,v as t}from"./openclaw-state-read-sqlite-database-admission-CrkztOzk.mjs";import{_ as n,d as r,f as i,g as a,i as o}from"./openclaw-state-read-schemas-YhOPoIHM.mjs";import"./openclaw-state-read-sqlite-worker-operation-admission-DdAi_0af.mjs";import{a as s,r as c}from"./openclaw-state-read-kysely-sync-Bcu0Qb7F.mjs";import"./openclaw-state-read-openclaw-state-db-B3dGXm7m.mjs";const l=a().min(1).max(256),u=r().int().nonnegative().max(2**53-1),d=i({revision:u,nextSequence:u}),f=i({sessionId:l,content:i({senderProfileId:l,sessionKey:a().min(1).max(512),agentId:l,messageId:l,createdAt:u,excerpt:a().max(280).optional()})}),p=i({key:a().regex(/^[a-f0-9]{64}$/),sequence:u,expiresAt:u,recipients:o(n([l,l.nullable()])).max(10),message:f.optional()}),m={name:`state.mention-head`,read:e=>d.safeParse(e).data},h=`notifications.mentions.head`,g=`notifications.mentions.source.`;function readMentionStoreSnapshot(n,r){let i=s(r);if(e(r,m)?.revision===n)return;let a=c(r,i.selectFrom(`config_machine_state`).select([`state_key`,`value_json`]).where(e=>e.or([e(`state_key`,`=`,h),e.and([e(`state_key`,`>=`,g),e(`state_key`,`<`,`notifications.mentions.source/`)])])).limit(10002)).rows,o=a.find(e=>e.state_key===h),l=o?d.parse(JSON.parse(o.value_json)):{revision:0,nextSequence:0},u=e(r,m);if((!u||u.revision<=l.revision)&&t(r,m,l),l.revision===n)return;let f=a.filter(e=>e.state_key!==h);if(f.length>1e4)throw Error(`Mention retention exceeds its source budget`);let _=/* @__PURE__ */ new Set,v=/* @__PURE__ */ new Set,y=f.map(e=>{if(e.value_json.length>32768)throw Error(`Mention source exceeds its record budget`);let t=p.parse(JSON.parse(e.value_json));if(e.state_key!==`${g}${t.key}`||t.sequence>=l.nextSequence||v.has(t.sequence)||new Set(t.recipients.map(([e])=>e)).size!==t.recipients.length)throw Error(`Invalid mention source identity`);v.add(t.sequence);for(let[,e]of t.recipients)if(e!==null){if(!t.message||_.has(e))throw Error(`Invalid retained mention`);_.add(e)}if(t.message&&t.expiresAt!==t.message.content.createdAt+6048e5)throw Error(`Invalid mention retention window`);return t});if(_.size>1e4)throw Error(`Mention retention exceeds its item budget`);return{head:l,sources:y.toSorted((e,t)=>e.sequence-t.sequence)}}const _={"mentions.snapshot":(e,t)=>({type:`mentions.snapshot`,snapshot:readMentionStoreSnapshot(e,t)})};export{_ as mentionReadOperations};