openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
5 lines • 7.49 kB
JavaScript
import{c as normalizeOptionalLowercaseString}from"./string-coerce-CpTFcSwY.mjs";import{l as isRecord}from"./record-coerce-BTbqdJ3-.mjs";import"./session-key-BPgKfU2m.mjs";import{n as normalizeAccountId,t as DEFAULT_ACCOUNT_ID}from"./account-id-3yqrEsRn.mjs";import{i as listAgentIds}from"./agent-roster-4XrgUgPK.mjs";import{D as tryResolveAgentOperationAgentId,h as resolveConfiguredAgentId,t as AgentSelectionRequiredError}from"./agent-scope-config-CG9CFpGX.mjs";import{n as resolveChannelSetupExecutionAdapter}from"./setup-contract-OX3vOUb4.mjs";import{f as readCurrentConfigForPolicyCheckAsync,l as readConfigFileSnapshotForWrite}from"./io.runtime-BvSeVFIY.mjs";import{n as getLoadedChannelPlugin,t as getChannelPlugin}from"./registry-CpNh6QcT.mjs";import{n as describeChannelAccount,r as resolveChannelAccount}from"./account-resolution-BMEKSThh.mjs";import"./plugins-DxOycUNJ.mjs";import"./config-DkZ8y8Gv.mjs";import{a as formatUnknownChannelMessage}from"./error-format-D9Q6t3gZ.mjs";import{t as applyAgentBindings}from"./agents.bindings-Bnd-pbi6.mjs";import{t as resolveChannelSetupOwner}from"./owner-DONBZpm3.mjs";import{i as withCommandPluginMetadata}from"./config-validation-BexrhW6U.mjs";import{t as describeBinding}from"./agents.binding-format-CQkTjZnX.mjs";import{t as persistChannelPluginConfig}from"./plugin-config-persistence-C297sR0X.mjs";
//!src/commands/channels/add-wizard.ts
function unresolvedInitialWizardChannelTarget(e){return{kind:`unresolved`,message:formatUnknownChannelMessage({channel:e})}}async function selectChannelSetupOwner(e,t,n){const r=e.snapshot.sourceConfig;try{return resolveChannelSetupOwner(r,n)}catch(e){if(!(e instanceof AgentSelectionRequiredError))throw e}const i=await t.select({message:`Set up channels for agent`,options:listAgentIds(r).map(e=>({value:{agentId:e},label:e}))});if(!isRecord(i)||typeof i.agentId!==`string`)throw new Error(`Invalid channel setup owner selection`);e.writeOptions.assertConfigPathForWrite?.();const a=await readCurrentConfigForPolicyCheckAsync({configPath:e.snapshot.path,env:process.env});e.writeOptions.assertConfigPathForWrite?.();const o=resolveConfiguredAgentId(a,i.agentId);return resolveChannelSetupOwner(a,o)}async function resolveInitialWizardChannelTarget(e,t,n){if(e===void 0)return{kind:`omitted`};const r=normalizeOptionalLowercaseString(e);if(!r)return unresolvedInitialWizardChannelTarget(``);const[{listActiveChannelSetupPlugins:i},{resolveChannelSetupEntries:a}]=await Promise.all([import(`./setup-registry-XPBiucKu.mjs`),import(`./discovery-DQ7F9ibH.mjs`)]);const o=a({cfg:t,installedPlugins:i(),workspaceDir:n??resolveChannelSetupOwner(t).workspaceDir});const s=o.entries.find(e=>normalizeOptionalLowercaseString(e.id)===r)??o.entries.find(e=>(e.meta.aliases??[]).some(e=>normalizeOptionalLowercaseString(e)===r));return s?{kind:`resolved`,channel:s.id}:unresolvedInitialWizardChannelTarget(e.trim())}async function runChannelsAddWizardFlow(e){const{writeSnapshot:t,runtime:n,prompter:r}=e;const{sourceConfig:i,hash:a}=t.snapshot;const[{buildAgentSummaries:o},s]=await Promise.all([import(`./agents.config-mTRZLAqS.mjs`),import(`./channel-setup-CmxGfPDH.mjs`)]);const c=s.createChannelSetupHooks({runtime:n,...e.beforePersistentEffect?{beforePersistentEffect:e.beforePersistentEffect}:{}});let l=[];const u={};const d=/* @__PURE__ */ new Map;await r.intro(`Channel setup`);let f=await s.setupChannels(i,n,r,{...e.workspaceDir?{workspaceDir:e.workspaceDir}:{},...e.initialChannel?{initialSelection:[e.initialChannel],finishAfterInitialSelection:true}:{},allowDisable:false,allowIMessageInstall:true,allowSignalInstall:true,...e.beforePersistentEffect?{beforePersistentEffect:e.beforePersistentEffect}:{},...e.assertPersistentEffectCurrent?{assertPersistentEffectCurrent:e.assertPersistentEffectCurrent}:{},...e.deferDeviceLinkToClient?{deferDeviceLinkToClient:true}:{},onPostWriteHook:c.onPostWriteHook,promptAccountIds:true,deferStatusUntilSelection:true,skipStatusNote:true,onSelection:e=>{l=e},onAccountId:(e,t)=>{u[e]=t},onResolvedPlugin:(e,t)=>{d.set(e,t)}});const commitWizardConfig=async r=>{const o=await persistChannelPluginConfig({cfg:r,setup:{previousConfig:i,beforePersistentEffect:e.beforePersistentEffect,assertCurrent:()=>{t.writeOptions.assertConfigPathForWrite?.();e.assertPersistentEffectCurrent?.()}},pluginInstalled:false,writeOptions:t.writeOptions,baseHash:a,runtime:n});await c.runPostWriteHooks(o.path)};if(l.length===0){if(f!==i){await commitWizardConfig(f);await r.outro(`Channels updated.`);return}await r.outro(`No channel changes made.`);return}const p=e.initialChannel!==void 0&&l.length===1&&l[0]===e.initialChannel;if(p?false:await r.confirm({message:`Name these channel accounts now? (optional)`,initialValue:false}))await withCommandPluginMetadata({config:f,workspaceDir:e.workspaceDir},async()=>{for(const e of l){const t=u[e]??`default`;const n=d.get(e)??getLoadedChannelPlugin(e);const i=n?await resolveChannelAccount({plugin:n,cfg:f,accountId:t}):void 0;const a=(n?await describeChannelAccount({plugin:n,account:i,cfg:f}):void 0)?.name??i?.name;const o=await r.text({message:`${e} display name for account "${t}"`,initialValue:a});if(o?.trim()){const r=n??getChannelPlugin(e);const i=r?resolveChannelSetupExecutionAdapter(r)?.applyAccountName:void 0;if(i)f=i({cfg:f,accountId:normalizeAccountId(t),name:o})}}});const m=l.flatMap(e=>{const t=u[e]?.trim();return t?[{channel:e,accountId:t}]:[]});if(m.length>0){const t=await o(f);if(p&&t.length<=1?false:p?true:await r.confirm({message:`Route these channel accounts to agents now?`,initialValue:true})){const n=tryResolveAgentOperationAgentId(f);const i=n===void 0?void 0:resolveConfiguredAgentId(f,n);for(const n of m){const a=await r.select({message:`Send ${n.channel}/${n.accountId} messages to agent`,options:t.map(e=>({value:e.id,label:e.isDefault?`${e.id} (default)`:e.id})),initialValue:e.agentId??i});const o=applyAgentBindings(f,[{agentId:a,match:{channel:n.channel,accountId:n.accountId}}]);f=o.config;if(o.added.length>0||o.updated.length>0)await r.note([...o.added.map(e=>`Added: ${describeBinding(e)}`),...o.updated.map(e=>`Updated: ${describeBinding(e)}`)].join(`
`),`Routing bindings`);if(o.conflicts.length>0)await r.note([`Skipped bindings already claimed by another agent:`,...o.conflicts.map(e=>`- ${describeBinding(e.binding)} (agent=${e.existingAgentId})`)].join(`
`),`Routing bindings`)}}}await commitWizardConfig(f);e.onConfigured?.(l.map(e=>({channel:e,accountId:u[e]??`default`})));await r.outro(`Channels updated.`)}async function runChannelsSetupWizard(e,t,n){const r=await readConfigFileSnapshotForWrite();const{snapshot:i}=r;if(i.exists&&!i.valid)throw new Error("OpenClaw config is invalid; run `openclaw doctor --fix`, then retry channel setup.");const a=i.sourceConfig;const{agentId:o,workspaceDir:s}=await selectChannelSetupOwner(r,n);const c=await resolveInitialWizardChannelTarget(e.channel,a,s);if(c.kind===`unresolved`)throw new Error(c.message);await runChannelsAddWizardFlow({writeSnapshot:r,agentId:o,runtime:t,prompter:n,workspaceDir:s,...c.kind===`resolved`?{initialChannel:c.channel}:{},deferDeviceLinkToClient:true,...e.onConfigured?{onConfigured:e.onConfigured}:{},...e.beforePersistentEffect?{beforePersistentEffect:e.beforePersistentEffect}:{},...e.assertPersistentEffectCurrent?{assertPersistentEffectCurrent:e.assertPersistentEffectCurrent}:{}})}export{resolveInitialWizardChannelTarget,runChannelsAddWizardFlow,runChannelsSetupWizard,selectChannelSetupOwner};