UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

3 lines • 5.88 kB
import{i as matchesReefPeerIdentity,o as sameReefPeerIdentity,r as ReefPeerTrustSchema,t as ReefAutonomySchema}from"../../friend-types-BIQirGc6.mjs";import{_ as mergeReefRejectionNotice,b as reefOutboundRequestStatus,c as ReefOutboundRejectionSchema,d as ReefRejectionNoticeStateSchema,g as matchesReefDeliveryBinding,h as listReefPeerTrust,l as ReefPeerStateSchema,p as currentReefDeliveries,s as ReefOutboundDeliverySchema,w as withoutReefOutboundRequest,y as parseReefPeerState}from"../../trust-store-format-c57VpnLg.mjs"; //!extensions/reef/src/trust-state-operation.ts function transitionReefPeer(e,t){switch(e.type){case`set`:return{...t,revision:t.revision+1,trust:ReefPeerTrustSchema.parse(e.input.trust)};case`remove`:return{revision:t.revision+1};case`setAutonomy`:return t.trust?{...t,trust:{...t.trust,autonomy:ReefAutonomySchema.parse(e.input.autonomy)}}:void 0;case`markSafetyNumberChanged`:return t.revision===e.input.expectedRevision&&t.trust?{...t,revision:t.revision+1,trust:{...t.trust,safetyNumberChanged:true}}:void 0;case`commitPeerTrust`:{const{friend:n,expectedRevision:r,expectedOutboundRequestId:i,approvedAt:a}=e.input;if(t.revision!==r||i!==void 0&&t.outboundRequests?.[i]===void 0)return;return{revision:t.revision+1,trust:ReefPeerTrustSchema.parse({autonomy:t.trust?.autonomy??`bounded`,ed25519PublicKey:n.ed25519_pub,x25519PublicKey:n.x25519_pub,keyEpoch:n.key_epoch,safetyNumberChanged:false,approvedAt:a}),...t.rejectionNotice?{rejectionNotice:t.rejectionNotice}:{}}}case`beginRequest`:return ReefPeerStateSchema.parse({...t,outboundRequests:{...t.outboundRequests,[e.input.requestId]:e.input.requestedAt}});case`removeRequest`:return withoutReefOutboundRequest(t,e.input.requestId)}return e}function rejectReefDelivery(e,t,n,r){const i=ReefOutboundDeliverySchema.safeParse(e);if(!i.success||!matchesReefDeliveryBinding(i.data,t))return;return{changed:i.data.rejection===void 0,delivery:i.data.rejection?i.data:{...i.data,rejection:ReefOutboundRejectionSchema.parse({...n?{category:n}:{},...i.data.resendDisabled?{notice:{lastRejectionAt:r}}:{}})}}}function reserveReefRejection(e,t,n){const r=ReefOutboundDeliverySchema.safeParse(e);if(!r.success||!r.data.rejection||!sameReefPeerIdentity(r.data.recipient,t))return;const i=r.data.rejection.notice;return{delivery:i?r.data:{...r.data,rejection:{...r.data.rejection,notice:ReefRejectionNoticeStateSchema.parse(n)}},outcome:i?{kind:`existing`,state:i}:{kind:`reserved`}}}function overdueReefDeliveries(e,t,n){return e.flatMap(({peer:e,id:r,delivery:i})=>i.rejection||i.overdueNotifiedAt!==void 0||i.sentAt===void 0||i.sentAt+t>n?[]:[{peer:e,id:r,sentAt:i.sentAt}])}function pendingReefRejections(e){return e.flatMap(({peer:e,id:t,delivery:n})=>n.rejection?[{peer:e,id:t,recipient:n.recipient,...n.textHash?{textHash:n.textHash}:{},...n.rejection.category?{category:n.rejection.category}:{},...n.rejection.notice?{reservedNotice:n.rejection.notice}:{}}]:[])}function readReefPeerState(e,t){return parseReefPeerState(e.lookup(0,t))}function deliveriesForCurrentPeers(e,t,n=false){const r=e.entries(1);if(r.length===0)return[];const i=new Map(listReefPeerTrust(e.entries(0),t).map(({peer:e,trust:t})=>[e,t]));return currentReefDeliveries(r,t,e=>i.get(e),n)}function runReefTrustStateOperation(e,t){switch(e.type){case`snapshot`:return readReefPeerState(t,e.input.key);case`list`:return listReefPeerTrust(t.entries(0),e.input.prefix);case`set`:case`remove`:case`setAutonomy`:case`markSafetyNumberChanged`:case`commitPeerTrust`:case`beginRequest`:case`removeRequest`:{const n=transitionReefPeer(e,readReefPeerState(t,e.input.key));if(n)t.set(0,e.input.key,n);return e.type===`set`||e.type===`beginRequest`?void 0:Boolean(n)}case`requestStatus`:return reefOutboundRequestStatus(readReefPeerState(t,e.input.key),e.input.requestId);case`readDelivery`:{const[n,r]=t.lookupMany([{store:0,key:e.input.peerKey},{store:1,key:e.input.deliveryKey}]);return r===void 0?void 0:{delivery:ReefOutboundDeliverySchema.parse(r),trust:parseReefPeerState(n).trust}}case`consumeDelivery`:case`discardDelivery`:{const{deliveryKey:n,expected:r}=e.input;const i=ReefOutboundDeliverySchema.safeParse(t.lookup(1,n));const a=i.success&&matchesReefDeliveryBinding(i.data,r);if(e.type===`consumeDelivery`&&i.success&&i.data.rejection)return`rejected`;if(a)t.delete(1,n);return e.type===`consumeDelivery`?a?`consumed`:`unavailable`:a}case`rejectDelivery`:{const{deliveryKey:n,expected:r,category:i,rejectedAt:a}=e.input;const o=rejectReefDelivery(t.lookup(1,n),r,i,a);if(o?.changed)t.set(1,n,o.delivery);return o?.delivery.rejection}case`reserveRejection`:{const{peerKey:n,deliveryKey:r,recipient:i,notice:a}=e.input;const[o,s]=t.lookupMany([{store:0,key:n},{store:1,key:r}]);if(!matchesReefPeerIdentity(parseReefPeerState(o).trust,i))return{kind:`peer-changed`};const c=reserveReefRejection(s,i,a);if(!c)return{kind:`unavailable`};t.set(1,r,c.delivery);return c.outcome}case`completeRejection`:{const{peerKey:n,deliveryKey:r}=e.input;const i=ReefRejectionNoticeStateSchema.parse(e.input.notice);const[a,o]=t.lookupMany([{store:0,key:n},{store:1,key:r}]);const s=parseReefPeerState(a);t.set(0,n,{...s,rejectionNotice:mergeReefRejectionNotice(s.rejectionNotice,i)});const c=ReefOutboundDeliverySchema.safeParse(o);const l=c.success&&c.data.rejection?.notice!==void 0;if(l)t.delete(1,r);return l||o===void 0}case`overdueDeliveries`:return overdueReefDeliveries(deliveriesForCurrentPeers(t,e.input.prefix),e.input.olderThanMs,e.input.now);case`markDeliveryOverdue`:{const{deliveryKey:n,now:r}=e.input;const i=ReefOutboundDeliverySchema.safeParse(t.lookup(1,n));if(!i.success||i.data.rejection||i.data.overdueNotifiedAt!==void 0)return false;t.set(1,n,{...i.data,overdueNotifiedAt:r});return true}case`pendingRejections`:return pendingReefRejections(deliveriesForCurrentPeers(t,e.input.prefix,true))}}export{runReefTrustStateOperation};