UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

3 lines • 7.6 kB
import{l as isRecord}from"./record-coerce-BTbqdJ3-.mjs";import{t as appendConfigPathSegment}from"./dot-path-B1bYAWM5.mjs";import{a as coerceSecretRef}from"./types.secrets-UWdyCdE6.mjs";import{n as normalizeAccountId}from"./account-id-3yqrEsRn.mjs";import"./shared-D0HXA3v0.mjs";import{c as isChannelAccountEffectivelyEnabled,l as isEnabledFlag,r as collectSecretInputAssignment,s as hasOwnProperty}from"./runtime-shared-ByPlHLvN.mjs"; //!src/secrets/channel-secret-basic-runtime.ts function buildChannelSecretTargetRegistryEntry(e){const t=typeof e.spec===`string`?{path:e.spec}:e.spec;const n=e.scope===`account`?`channels.${e.channelKey}.accounts.*`:`channels.${e.channelKey}`;const r=`${n}.${t.path}`;return{id:r,targetType:t.targetType??r,...t.targetTypeAliases?{targetTypeAliases:t.targetTypeAliases}:{},configFile:`openclaw.json`,pathPattern:r,...t.refPath?{refPathPattern:`${n}.${t.refPath}`}:{},secretShape:t.secretShape??`secret_input`,expectedResolvedValue:t.expectedResolvedValue??`string`,includeInPlan:true,includeInConfigure:true,includeInAudit:true,...t.accountIdPathSegmentIndex!==void 0?{accountIdPathSegmentIndex:t.accountIdPathSegmentIndex}:{}}}function createChannelSecretTargetRegistryEntries(e){const entriesForScope=t=>(e[t]??[]).map(n=>buildChannelSecretTargetRegistryEntry({channelKey:e.channelKey,scope:t,spec:n}));return[...entriesForScope(`account`),...entriesForScope(`channel`)]}function createChannelSecretContract(e){return{secretTargetRegistryEntries:createChannelSecretTargetRegistryEntries(e),collectRuntimeConfigAssignments(t){const n=getChannelRecord(t.config,e.channelKey);if(!n)return;e.collect({...t,channelKey:e.channelKey,channel:n,surface:resolveChannelAccountSurface(n,e.hasImplicitDefaultAccount?.({...t,channel:n})),defaults:t.defaults})}}}function createSimpleChannelSecretContract(e){const t=createChannelSecretTargetRegistryEntries({channelKey:e.channelKey,account:e.accountFields,channel:e.channelFields});const n=typeof e.mode===`object`?e.mode.collectionFields:[.../* @__PURE__ */ new Set([...e.accountFields,...e.channelFields])];const collectRuntimeConfigAssignments=t=>{const r=e.mode===`channel-only`;const i=getChannelRecord(t.config,e.channelKey);if(!i)return;if(r){for(const r of n)collectSecretInputAssignment({value:i[r],path:`channels.${e.channelKey}.${r}`,expected:`string`,defaults:t.defaults,context:t.context,active:i.enabled!==false,inactiveReason:`${e.label} channel is disabled.`,owner:{ownerKind:`account`,ownerId:`${e.channelKey}:default`,requiredForGateway:false,disposition:`isolate`,contract:i},apply:e=>{i[r]=e}});return}const a=resolveChannelAccountSurface(i);for(const r of n){const n=e.mode===`channel-surface`?`${e.label} channel is disabled.`:e.mode===`account-inheritance`?`no enabled account inherits this top-level ${e.label} ${r}.`:`no enabled ${e.label} surface inherits this top-level ${r}.`;collectSimpleChannelFieldAssignments({channelKey:e.channelKey,field:r,channel:i,surface:a,defaults:t.defaults,context:t.context,topInactiveReason:n,accountInactiveReason:e.mode===`channel-surface`?`${e.label} channel is disabled.`:`${e.label} account is disabled.`})}};return{secretTargetRegistryEntries:t,collectRuntimeConfigAssignments}}function createChannelAccountSecretOwner(e,t,n,r,i){const{accounts:a,...o}=n;return{ownerKind:`account`,ownerId:`${e}:${normalizeAccountId(t)}`,requiredForGateway:false,disposition:`isolate`,contract:i??{channel:o,account:r}}}function getChannelRecord(e,t){const n=e.channels;if(!isRecord(n))return;const r=n[t];return isRecord(r)?r:void 0}function getChannelSurface(e,t){const n=getChannelRecord(e,t);if(!n)return null;return{channel:n,surface:resolveChannelAccountSurface(n)}}function resolveChannelAccountSurface(e,t=false){const n=isEnabledFlag(e);const r=e.accounts;if(!isRecord(r)||Object.keys(r).length===0)return{hasExplicitAccounts:false,channelEnabled:n,accounts:[{accountId:`default`,account:e,enabled:n}]};const i=[];for(const[t,n]of Object.entries(r)){if(!isRecord(n))continue;i.push({accountId:t,account:n,enabled:isChannelAccountEffectivelyEnabled(e,n)})}if(t&&!Object.keys(r).some(e=>normalizeAccountId(e)==="default"))i.push({accountId:`default`,account:{},enabled:n});return{hasExplicitAccounts:true,channelEnabled:n,accounts:i}}function isBaseFieldActiveForChannelSurface(e,t){if(!e.channelEnabled)return false;if(!e.hasExplicitAccounts)return true;return e.accounts.some(({account:e,enabled:n})=>n&&!hasOwnProperty(e,t))}function normalizeSecretStringValue(e){return typeof e===`string`?e.trim():``}function hasConfiguredSecretInputValue(e,t){return normalizeSecretStringValue(e).length>0||coerceSecretRef(e,t)!==null}function collectSimpleChannelFieldAssignments(e){collectConditionalChannelFieldAssignments({...e,topLevelActiveWithoutAccounts:true,topLevelInheritedAccountActive:({account:t,enabled:n})=>n&&!hasOwnProperty(t,e.field),accountActive:({enabled:e})=>e})}function collectConditionalChannelFieldAssignments(e){collectChannelFieldAssignments({...e,nestedKey:void 0,topLevelActiveWithoutAccounts:e.surface.channelEnabled&&e.topLevelActiveWithoutAccounts})}function collectNestedChannelFieldAssignments(e){collectChannelFieldAssignments({...e,topLevelActiveWithoutAccounts:e.topLevelActive,topLevelInheritedAccountActive:e.topLevelInheritedAccountActive??(({account:t,enabled:n})=>e.topLevelActive&&n&&!hasOwnProperty(t,e.nestedKey))})}function collectChannelFieldAssignments(e){const t=e.nestedKey===void 0?e.field:`${e.nestedKey}.${e.field}`;const targetFor=t=>{const n=e.nestedKey===void 0?t:t[e.nestedKey];return isRecord(n)?n:void 0};const n=targetFor(e.channel);if(n){const{channelKey:r,channel:i}=e;const a=n[e.field];const o=`channels.${e.channelKey}.${t}`;const{surface:s,defaults:c,context:l}=e;const u=e.topLevelActiveWithoutAccounts;const d=e.topLevelInheritedAccountActive;const f=e.topInactiveReason;const apply=t=>{n[e.field]=t};const p=s.hasExplicitAccounts?s.accounts.filter(d):u?[{accountId:`default`,account:{},enabled:true}]:[];if(p.length===0)collectSecretInputAssignment({value:a,path:o,expected:`string`,defaults:c,context:l,active:false,inactiveReason:f,apply});else{const{accounts:e,...t}=i;const n={channel:t,consumers:p.map(({accountId:e,account:t})=>({accountId:normalizeAccountId(e),account:t})).toSorted((e,t)=>e.accountId.localeCompare(t.accountId))};for(const{accountId:e,account:t}of p)collectSecretInputAssignment({value:a,path:o,expected:`string`,defaults:c,context:l,owner:createChannelAccountSecretOwner(r,e,i,t,n),apply})}}if(!e.surface.hasExplicitAccounts)return;for(const n of e.surface.accounts){const r=targetFor(n.account);if(!r||e.nestedKey===void 0&&!hasOwnProperty(r,e.field))continue;collectSecretInputAssignment({value:r[e.field],path:`${appendConfigPathSegment(`channels.${e.channelKey}.accounts`,n.accountId)}.${t}`,expected:`string`,defaults:e.defaults,context:e.context,active:e.accountActive(n),inactiveReason:typeof e.accountInactiveReason===`function`?e.accountInactiveReason(n):e.accountInactiveReason,owner:createChannelAccountSecretOwner(e.channelKey,n.accountId,e.channel,n.account),apply:t=>{r[e.field]=t}})}}export{createChannelSecretContract as a,getChannelRecord as c,isBaseFieldActiveForChannelSurface as d,normalizeSecretStringValue as f,createChannelAccountSecretOwner as i,getChannelSurface as l,collectNestedChannelFieldAssignments as n,createChannelSecretTargetRegistryEntries as o,resolveChannelAccountSurface as p,collectSimpleChannelFieldAssignments as r,createSimpleChannelSecretContract as s,collectConditionalChannelFieldAssignments as t,hasConfiguredSecretInputValue as u};