UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

3 lines • 3.96 kB
import{r as prepareOutboundProposal,t as PipelineError}from"../../pipeline-D5CsuXRG.mjs";import{T as formatHandleEpoch}from"../../state-format-0Qjzduzr.mjs";import{a as reefPeerIdentity,i as matchesReefPeerIdentity}from"../../friend-types-BIQirGc6.mjs";import{l as ReefPeerStateSchema,s as ReefOutboundDeliverySchema}from"../../trust-store-format-c57VpnLg.mjs";import{t as appendReefAuditEvents}from"../../audit-state-operation-BZhoPnQF.mjs";import{i as ReefReviewCapacityError,n as requestReefReview,t as lookupReefReviewDecision}from"../../state-operation-BNfBrP8b.mjs"; //!extensions/reef/src/outbound-state-operation.ts function reviewFailure(e,t,n){return{kind:`pipeline`,stage:`review`,message:t?`review explicitly denied`:`review approval pending`,reviewOutcome:t?`denied`:`pending`,approvalDigest:e,verdict:n}}function runReefOutboundOperation(e,t){const n=e.input;const r=Date.now();const i=[];const event=(e,t)=>i.push({type:e,payload:t,ts:Math.floor(r/1e3)});const finish=e=>{appendReefAuditEvents(t,n.audit,i);return e};if(e.type===`prepare`){const n=e.input;const[r,i]=t.lookupMany([{store:0,key:n.peerKey},{store:1,key:n.deliveryKey}]);const a=r===void 0?{revision:0}:ReefPeerStateSchema.parse(r);const o=a.trust;if(!o||o.safetyNumberChanged||n.expectedRecipient&&!matchesReefPeerIdentity(o,n.expectedRecipient))return{kind:`unapproved`};if(i!==void 0)return{kind:`duplicate`};const s=formatHandleEpoch(n.peer,o.keyEpoch);let c;try{c=prepareOutboundProposal({...n,to:s,recipientEncryptionPublicKey:o.x25519PublicKey},n.senderSigningKeyLength)}catch(e){if(!(e instanceof PipelineError))throw e;return{kind:`pipeline`,stage:e.stage,message:e.message}}const{checks:l,proposalHash:u,approvalDigest:d}=c;const f={id:n.id,from:n.from,to:s,direction:`outbound`,bodyHash:u,approvalDigest:d};event(`proposal`,{id:n.id,from:n.from,to:s,bodyHash:u,approvalDigest:d,body:n.body});if(!l.allowed){event(`deterministic_verdict`,{id:n.id,approvalDigest:d,decision:`deny`,findings:l.findings});return finish({kind:`pipeline`,stage:`deterministic`,message:`deterministic checks denied message`})}const p=lookupReefReviewDecision(t,5,d);if(p===`pending`||p!==`none`&&!p.approved)return finish(reviewFailure(d,p!==`pending`));const m=p!==`none`;if(m)event(`review_approval`,{...f,approved:true});return finish({kind:`prepared`,prepared:{binding:f,recipient:reefPeerIdentity(o),afterApproval:m,peerRevision:a.revision}})}const{prepared:a,verdict:o}=e.input;const{binding:s,recipient:c,afterApproval:l}=a;event(`guard_verdict`,{...s,...l?{afterApproval:true}:{},...o});if(o.decision===`deny`)return finish({kind:`pipeline`,stage:`guard`,message:l?`guard denied approved message`:`Reef outbound guard denied the message. Do not retry or rephrase it automatically; ask the owner before sending related content.`,verdict:o});const u=lookupReefReviewDecision(t,5,s.approvalDigest);if(l&&(u===`none`||u===`pending`||!u.approved)||!l&&u!==`none`)return finish(reviewFailure(s.approvalDigest,u!==`none`&&u!==`pending`&&!u.approved,o));if(!l&&o.decision===`review`){try{requestReefReview(t,5,e.input.reviewMaxEntries,{...s,verdict:o})}catch(e){if(!(e instanceof ReefReviewCapacityError))throw e;return finish({kind:`review-capacity`})}return finish(reviewFailure(s.approvalDigest,false,o))}const d=e.input.envelope;if(!d)return finish({kind:`composition-failed`});event(`envelope`,{id:s.id,approvalDigest:s.approvalDigest,envelope:d});const[f,p]=t.lookupMany([{store:0,key:n.peerKey},{store:1,key:n.deliveryKey}]);const m=f===void 0?{revision:0}:ReefPeerStateSchema.parse(f);if(m.revision!==a.peerRevision||!matchesReefPeerIdentity(m.trust,c))return finish({kind:`peer-changed`});if(p!==void 0)return finish({kind:`duplicate`});t.set(1,n.deliveryKey,ReefOutboundDeliverySchema.parse({bodyHash:s.bodyHash,textHash:e.input.textHash,recipient:c,sentAt:r,...e.input.resendDisabled?{resendDisabled:true}:{}}));return finish({kind:`recorded`})}export{runReefOutboundOperation};