openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
6 lines • 17.3 kB
JavaScript
//!src/channels/message-access/admission-decision-receipt.ts
function createChannelAdmissionDecisionReceipt(e){const t=e.coverageState===`unknown`?[`channel.admission_evidence`]:e.coverageState===`unsupported`?[`channel.adapter_identity`]:e.coverageState===`attribution-only`?[`decision.participant_effect`]:[];const n=e.identifierAuthentication===`affected`||e.identifierAuthentication===`evaluated`;return{schemaVersion:1,receiptId:`${e.contextId}:channel-admission`,contextId:e.contextId,executionId:e.executionId,runId:e.runId,occurredAt:e.occurredAt,action:{family:`channel`,operation:`admission`,summary:`Channel ingress admitted this agent execution.`},decision:{outcome:e.coverageState===`unknown`||e.coverageState===`unsupported`?`unknown`:`allowed`,reasonCode:e.identifierAuthentication===`affected`?`channel_ingress_identifier_authentication_applied`:e.coverageState===`enforced`?`channel_ingress_participant_enforced`:e.coverageState===`attribution-only`?`channel_ingress_attribution_only`:e.coverageState===`unsupported`?`channel_ingress_identity_unsupported`:`channel_ingress_identity_unknown`},enforcement:{coverageState:e.coverageState,evaluatorRef:`channel-ingress`,policyRefs:n?[`channel.identifier-authentication`]:[],grantRefs:[],contextFieldsUsed:[...e.coverageState===`enforced`?[`invoker.principal`]:[],...e.identifierAuthentication===`affected`?[`channel.identifier-authentication`]:[]]},source:{owner:`channel-ingress`,recordRef:`${e.contextId}:channel-admission`,decisionBoundary:`channel-ingress.run-admission`},missingEvidence:t,remediation:e.coverageState===`enforced`?[]:[{code:`treat_as_diagnostic_provenance`,text:`Treat this receipt as diagnostic provenance, not authorization.`}]}}
//!src/channels/message-access/admission-evidence-scope-key.ts
const MAX_CHANNEL_ADMISSION_SCOPE_BYTES=32768;const MAX_CHANNEL_ADMISSION_SCOPE_NODES=256;const INVALID_SCOPE_VALUE=Symbol(`invalid-channel-admission-scope-value`);function snapshotOwnedData(e,t={nodes:0},n=0){t.nodes+=1;if(t.nodes>MAX_CHANNEL_ADMISSION_SCOPE_NODES||n>6)return INVALID_SCOPE_VALUE;if(e===void 0||e===null||typeof e===`string`||typeof e===`boolean`)return e;if(typeof e===`number`)return Number.isFinite(e)?e:INVALID_SCOPE_VALUE;if(typeof e!==`object`)return INVALID_SCOPE_VALUE;let r;try{r=Object.getOwnPropertyDescriptors(e);if(Object.getOwnPropertySymbols(e).some(t=>Object.getOwnPropertyDescriptor(e,t)?.enumerable))return INVALID_SCOPE_VALUE}catch{return INVALID_SCOPE_VALUE}const i=Object.keys(r).filter(e=>r[e]?.enumerable).toSorted();const a=[];for(const e of i){const i=r[e];if(!i||!(`value`in i))return INVALID_SCOPE_VALUE;const o=snapshotOwnedData(i.value,t,n+1);if(o===INVALID_SCOPE_VALUE)return INVALID_SCOPE_VALUE;a.push([e,o])}return Array.isArray(e)?[`array`,a]:[`record`,a]}function stableOwnedScopeKey(e){const t=snapshotOwnedData(e);if(t===INVALID_SCOPE_VALUE)return;try{const e=JSON.stringify(t);return e.length<=MAX_CHANNEL_ADMISSION_SCOPE_BYTES?e:void 0}catch{return}}function safeOwnPropertyDescriptor(e,t){try{return Object.getOwnPropertyDescriptor(e,t)}catch{return INVALID_SCOPE_VALUE}}function ownDataValue(e,t){const n=safeOwnPropertyDescriptor(e,t);if(n===INVALID_SCOPE_VALUE)return INVALID_SCOPE_VALUE;if(!n)return;return`value`in n?n.value:INVALID_SCOPE_VALUE}function publicResultScopeKey(e){const t=ownDataValue(e,`state`);if(!t||typeof t!==`object`)return;const n=ownDataValue(t,`routeFacts`);if(!Array.isArray(n))return;const r=ownDataValue(n,`length`);if(typeof r!==`number`||r>MAX_CHANNEL_ADMISSION_SCOPE_NODES)return;const i=[];for(let e=0;e<r;e+=1){const t=ownDataValue(n,String(e));if(!t||typeof t!==`object`)return;i.push({id:ownDataValue(t,`id`),kind:ownDataValue(t,`kind`),gate:ownDataValue(t,`gate`),effect:ownDataValue(t,`effect`),precedence:ownDataValue(t,`precedence`),senderPolicy:ownDataValue(t,`senderPolicy`)})}return stableOwnedScopeKey({accountId:ownDataValue(t,`accountId`),channelId:ownDataValue(t,`channelId`),conversationKind:ownDataValue(t,`conversationKind`),event:ownDataValue(t,`event`),routeFacts:i})}const FINALIZED_CONTEXT_SCOPE_FIELDS=[`OriginatingChannel`,`AccountId`,`SenderId`,`ChatType`,`ChatId`,`SessionKey`,`AgentId`,`DmScope`,`ParentSessionKey`,`ModelParentSessionKey`,`MessageSid`,`MessageSidFull`,`ReplyToId`,`ReplyToIdFull`,`To`,`From`,`OriginatingTo`,`MessageThreadId`,`NativeChannelId`,`ThreadParentId`,`InboundEventKind`,`Provider`,`Surface`,`NativeDirectUserId`];function finalizedContextScopeKey(e){const t=[];for(const n of FINALIZED_CONTEXT_SCOPE_FIELDS){const r=safeOwnPropertyDescriptor(e,n);if(r===INVALID_SCOPE_VALUE)return;if(!r){t.push([n,`absent`]);continue}if(!(`value`in r))return;const i=r.value;if(i!==void 0&&i!==null&&typeof i!==`string`&&typeof i!==`number`&&typeof i!==`boolean`)return;t.push([n,`present`,i])}return stableOwnedScopeKey(t)}function scopedParticipantRef(e){const t=e.channelId;const n=e.accountId||`default`;const r=e.rawPrincipalRef==null?``:String(e.rawPrincipalRef);if(!t||!r)return;const i=JSON.stringify([t,n,r]);return i.length<=4096?i:void 0}function snapshotContextBinding(e){if(!e||typeof e!==`object`)return;const t=ownDataValue(e,`agentId`);const n=ownDataValue(e,`sessionKey`);const r=ownDataValue(e,`messageId`);const i=ownDataValue(e,`nativeChannelId`);const a=ownDataValue(e,`inboundEventKind`);if(typeof t!==`string`||typeof n!==`string`||r!==void 0&&typeof r!==`string`||i!==void 0&&typeof i!==`string`||a!==`user_request`&&a!==`room_event`)return;return{agentId:t,sessionKey:n,messageId:r,nativeChannelId:i,inboundEventKind:a}}function normalizeScopeId(e){if(e===void 0||e===null)return;return typeof e===`string`||typeof e===`number`?String(e):INVALID_SCOPE_VALUE}function contextHandoffMatches(e){const t=ownDataValue(e.contextParams,`conversation`);const n=ownDataValue(e.contextParams,`route`);const r=ownDataValue(e.contextParams,`reply`);const i=ownDataValue(e.contextParams,`message`);if(!t||typeof t!==`object`||!n||typeof n!==`object`||!r||typeof r!==`object`||!i||typeof i!==`object`)return false;const a=e.binding.scope?.conversation;const o=e.binding.contextBinding;if(!a||!o)return false;const s=ownDataValue(n,`accountId`);const c=s===void 0?e.accountId:normalizeScopeId(s);if(c===INVALID_SCOPE_VALUE)return false;const l=ownDataValue(t,`kind`);const u=normalizeScopeId(ownDataValue(t,`id`));const d=normalizeScopeId(ownDataValue(t,`parentId`));const f=normalizeScopeId(ownDataValue(t,`threadId`));const p=normalizeScopeId(ownDataValue(r,`messageThreadId`));const m=normalizeScopeId(ownDataValue(r,`threadParentId`));const h=normalizeScopeId(ownDataValue(t,`nativeChannelId`));const g=normalizeScopeId(ownDataValue(r,`nativeChannelId`));const _=normalizeScopeId(ownDataValue(n,`agentId`));const v=normalizeScopeId(ownDataValue(n,`dispatchSessionKey`));const y=normalizeScopeId(ownDataValue(n,`routeSessionKey`));const b=ownDataValue(i,`inboundEventKind`);const x=b===void 0||b===null?`user_request`:normalizeScopeId(b);if([u,d,f,p,m,h,g,_,v,y,x].includes(INVALID_SCOPE_VALUE))return false;const S=g??h;if(o.nativeChannelId!==void 0&&S!==o.nativeChannelId||o.nativeChannelId===void 0&&typeof S===`string`&&![a.id,a.parentId,a.threadId].includes(S))return false;if(p!==void 0&&f!==void 0&&p!==f||m!==void 0&&d!==void 0&&m!==d||g!==void 0&&h!==void 0&&g!==h)return false;return scopedParticipantRef(e.binding)===scopedParticipantRef({channelId:e.channelId,accountId:c,rawPrincipalRef:e.rawPrincipalRef})&&l===a.kind&&u===a.id&&(m??d)===a.parentId&&(p??f)===a.threadId&&_===o.agentId&&(v??y)===o.sessionKey&&x===o.inboundEventKind}
//!src/channels/message-access/admission-evidence.ts
const CHANNEL_ADMISSION_EVIDENCE_MAX_CONTRIBUTIONS=16;const CHANNEL_ADMISSION_EVIDENCE_MAX_AGE_MS=2592e6;const CONTEXT_ADMISSION=Symbol(`openclaw.channelContextAdmission`);var ChannelAdmissionAudit=class{#e;#t=false;#n={};#r;constructor(e){this.#e=e.enabled;this.#r=e.decisionSink}get enabled(){return this.#e}configure(e){if(this.#t||e===this.#e)return;this.#e=e;this.#n={}}captureCurrent(){const e=this.#n;return()=>this.#e&&this.#n===e}recordDecision(e){return this.#e?this.#r?.(e)??false:false}close(){this.#t=true;this.#e=false;this.#n={};this.#r=void 0}};function createChannelAdmissionAudit(e){return new ChannelAdmissionAudit(e)}var AdmissionEvidence=class{#e;#t;#n;#r;constructor(e,t){this.kind=`channel-admission-evidence`;this.#r=false;this.#e=e;this.#t=t;this.#n=t?.captureCurrent();Object.setPrototypeOf(this,null);Object.freeze(this)}static read(e){return e!==void 0&&e!==null&&typeof e===`object`&&#e in e&&(e.#n?.()??true)?{payload:e.#e,audit:e.#t,consumed:e.#r}:void 0}static consume(e){if(e!==null&&typeof e===`object`&&#e in e)e.#r=true}};var PreparedChannelAdmissionEvidence=class{#e=false;#t;#n;#r;#i;constructor(e,t,n){this.#t=e;this.#n=t;this.#r=n;this.#i=n?.resolveGatewayContext?.();Object.setPrototypeOf(this,null);Object.freeze(this)}static take(e){if(!e||typeof e!==`object`||!(#e in e)||e.#e)return;e.#e=true;const t=!e.#r||e.#r.isLive()&&e.#r.resolveGatewayContext?.()===e.#i;return{evidence:t?e.#t:unknownChannelAdmissionEvidence(AdmissionEvidence.read(e.#t)?.audit),resolver:t?e.#n:void 0}}};var ContextAdmission=class{#e;#t;constructor(e,t){this.#e=e;this.#t=t;Object.setPrototypeOf(this,null);Object.freeze(this)}static read(e,t){return e!==null&&typeof e===`object`&&#e in e&&e.#e===t?e.#t:void 0}};function readContextAdmission(e){return ContextAdmission.read(ownDataValue(e,CONTEXT_ADMISSION),e)}function writeContextAdmission(e,t){Object.defineProperty(e,CONTEXT_ADMISSION,{value:new ContextAdmission(e,t),configurable:true})}var HostIngressResolution=class{#e;#t;constructor(e,t){this.#e=e;this.#t=t;Object.setPrototypeOf(this,null)}static read(e){return e!==null&&typeof e===`object`&&#e in e?e.#e:void 0}static takeParticipant(e){if(e===null||typeof e!==`object`||!(#e in e))return;const t=e.#e.handoff.accepted?e.#t:void 0;e.#t=void 0;return t}};function takeChannelParticipantInput(e){return HostIngressResolution.takeParticipant(e)}function mintChannelAdmissionEvidence(e,t){if(!e?.enabled)return;return new AdmissionEvidence({...t,createdAt:Date.now()},e)}function unsupportedChannelAdmissionEvidence(e){const t={kind:`leaf`,contribution:{participant:{state:`unsupported`}}};return e?mintChannelAdmissionEvidence(e,t):new AdmissionEvidence({...t,createdAt:Date.now()},void 0)}function participantContribution(e){const t=scopedParticipantRef(e);return t?{participant:{state:`present`,rawPrincipalRef:t}}:{participant:{state:`unknown`}}}function recordChannelIngressResolution(e){const t=e.owner;if(!t||t.channelId!==e.channelId||!t.isLive())return e.result;const n={channelId:e.channelId,accountId:e.accountId,rawPrincipalRef:e.rawPrincipalRef,participantOutcomeAffecting:e.participantOutcomeAffecting,identifierAuthentication:e.identifierAuthentication,owner:t,gatewayContext:t.resolveGatewayContext?.(),scope:{conversation:{...e.scope.conversation}},contextBinding:snapshotContextBinding(e.scope.contextBinding),publicScopeKey:publicResultScopeKey(e.result),handoff:{consumed:false}};return Object.assign(new HostIngressResolution(n,e.participantInput),e.result)}function unknownChannelAdmissionEvidence(e){return mintChannelAdmissionEvidence(e,{kind:`leaf`,contribution:{participant:{state:`unknown`}}})}function prepareHostChannelContextAdmissionEvidence(e){const t=e.owner?.resolveGatewayContext?.()?.channelAdmissionAudit;if(e.ingress===`unsupported`)return new PreparedChannelAdmissionEvidence(unsupportedChannelAdmissionEvidence(t),void 0,e.owner);const n=e.ingress===void 0?[]:Array.isArray(e.ingress)?e.ingress:[e.ingress];const r=/* @__PURE__ */ new Set;const i=[];let a=n.length>0&&n.length<=CHANNEL_ADMISSION_EVIDENCE_MAX_CONTRIBUTIONS;for(const[t,o]of n.entries()){const s=HostIngressResolution.read(o);const c=s!==void 0&&!s.handoff.consumed&&!r.has(o);if(s&&!s.handoff.consumed)s.handoff.consumed=true;r.add(o);const l=e.owner!==void 0&&s?.owner===e.owner&&s.gatewayContext===e.owner.resolveGatewayContext?.()&&e.owner.isLive();const u=ownDataValue(o,`ingress`);const d=s?.publicScopeKey!==void 0&&publicResultScopeKey(o)===s.publicScopeKey&&u!==null&&typeof u===`object`&&ownDataValue(u,`admission`)===`dispatch`;const f=s!==void 0&&contextHandoffMatches({...e,binding:s,rawPrincipalRef:t===n.length-1?e.rawPrincipalRef:s.rawPrincipalRef});if(!c||!l||!d||!f||!s)a=false;else i.push(s)}const o=normalizeScopeId(ownDataValue(e.contextParams,`messageId`));const s=i.at(-1)?.contextBinding?.messageId;if(o===INVALID_SCOPE_VALUE||s!==void 0&&o!==s)a=false;if(a)for(const e of i)e.handoff.accepted=true;const c=a?i.map(e=>{const n=participantContribution(e);return mintChannelAdmissionEvidence(t,{kind:`leaf`,contribution:{...n,decision:{participantAware:n.participant.state===`present`,outcomeAffecting:e.participantOutcomeAffecting,identifierAuthentication:e.identifierAuthentication}}})}):[];return new PreparedChannelAdmissionEvidence(a?combineChannelAdmissionEvidence(c):unknownChannelAdmissionEvidence(t),a?e.owner?.resolveGatewayContext:void 0,e.owner)}function bindHostChannelContextAdmissionEvidence(e){const t=PreparedChannelAdmissionEvidence.take(e.preparation);const n=finalizedContextScopeKey(e.context);const r=AdmissionEvidence.read(t?.evidence)?.audit;writeContextAdmission(e.context,{scope:n,evidence:n!==void 0?t?.evidence:unknownChannelAdmissionEvidence(r),resolver:n!==void 0?t?.resolver:void 0})}function readChannelContextAdmissionEvidence(e){return readContextAdmission(e)?.evidence}function readChannelContextGatewayContextResolver(e){return readContextAdmission(e)?.resolver}function copyChannelParticipantAdmissionEvidence(e,t){const n=readContextAdmission(e);if(!n)return;const r=finalizedContextScopeKey(t);const i=n.scope!==void 0&&r===n.scope;const a=i&&activePayload(n.evidence,Date.now())!==void 0?n.evidence:unknownChannelAdmissionEvidence(AdmissionEvidence.read(n.evidence)?.audit);const o=readContextAdmission(t);const s=o?.resolverConflict===true||Boolean(o?.resolver&&n.resolver&&o.resolver!==n.resolver);writeContextAdmission(t,{scope:r,evidence:a,resolver:i&&!s?n.resolver:void 0,resolverConflict:s})}function activePayload(e,t){const n=AdmissionEvidence.read(e);if(!n||n.consumed)return;const{payload:r}=n;return t-r.createdAt<=CHANNEL_ADMISSION_EVIDENCE_MAX_AGE_MS?r:void 0}function combineChannelAdmissionEvidence(e){if(e.length===1)return e[0];const t=e.map(e=>AdmissionEvidence.read(e)?.audit).find(e=>e!==void 0);if(!t?.enabled)return;if(e.length>CHANNEL_ADMISSION_EVIDENCE_MAX_CONTRIBUTIONS||e.some(e=>AdmissionEvidence.read(e)?.audit!==t))return unknownChannelAdmissionEvidence(t);return mintChannelAdmissionEvidence(t,{kind:`aggregate`,sources:[...e]})}function readContributions(e){const t=activePayload(e.evidence,e.now);if(!t||!e.evidence||e.seen.has(e.evidence))return[{participant:{state:`unknown`}}];e.seen.add(e.evidence);if(e.consume)AdmissionEvidence.consume(e.evidence);if(t.kind===`leaf`)return[t.contribution];const n=t.sources.flatMap(t=>readContributions({...e,evidence:t}));return!e.consume||n.length<=CHANNEL_ADMISSION_EVIDENCE_MAX_CONTRIBUTIONS?n:[{participant:{state:`unknown`}}]}function sharedParticipantRef(e){const t=e[0]?.participant;return t?.state===`present`&&e.every(({participant:e})=>e.state===`present`&&e.rawPrincipalRef===t.rawPrincipalRef)?t.rawPrincipalRef:void 0}function compareChannelAdmissionParticipants(e){const t=e.flatMap(e=>readContributions({evidence:e,now:Date.now(),seen:/* @__PURE__ */ new Set,consume:false}));if(t.length===0||t.length>CHANNEL_ADMISSION_EVIDENCE_MAX_CONTRIBUTIONS)return`mixed-or-unknown`;return sharedParticipantRef(t)!==void 0?`same`:`mixed-or-unknown`}function freezeConsumed(e){return Object.freeze({...e,invoker:Object.freeze(e.invoker)})}function consumeChannelAdmissionEvidence(e){const t=readContributions({evidence:e,now:Date.now(),seen:/* @__PURE__ */ new Set,consume:true});const n=t.length>0&&t.every(({participant:e})=>e.state===`unsupported`);const r=n?void 0:sharedParticipantRef(t);if(r===void 0){const e=n?`unsupported`:`unknown`;return freezeConsumed({ingressState:e,invoker:{state:`unknown`},decisionCoverage:e,identifierAuthentication:`unknown`})}const i=t.every(e=>e.decision?.participantAware&&e.decision.outcomeAffecting);const a=t.some(e=>e.decision?.identifierAuthentication===`affected`)?`affected`:t.some(e=>e.decision?.identifierAuthentication===`evaluated`)?`evaluated`:t.every(e=>e.decision?.identifierAuthentication===`not-evaluated`)?`not-evaluated`:`unknown`;return freezeConsumed({ingressState:`present`,invoker:{state:`present`,kind:`person`,rawPrincipalRef:r},assuranceRef:`channel-admission`,decisionCoverage:i?`enforced`:`attribution-only`,identifierAuthentication:a})}function recordChannelAdmissionDecision(e,t){const n=AdmissionEvidence.read(e);return n?.audit?n.audit.recordDecision(createChannelAdmissionDecisionReceipt(t)):false}export{copyChannelParticipantAdmissionEvidence as a,readChannelContextAdmissionEvidence as c,recordChannelIngressResolution as d,takeChannelParticipantInput as f,consumeChannelAdmissionEvidence as i,readChannelContextGatewayContextResolver as l,combineChannelAdmissionEvidence as n,createChannelAdmissionAudit as o,compareChannelAdmissionParticipants as r,prepareHostChannelContextAdmissionEvidence as s,bindHostChannelContextAdmissionEvidence as t,recordChannelAdmissionDecision as u};