openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
3 lines • 7.59 kB
JavaScript
import{c as normalizeOptionalLowercaseString}from"./string-coerce-CpTFcSwY.mjs";import{E as parseStrictNonNegativeInteger}from"./number-coercion-B3CfI447.mjs";import{r as defaultRuntime}from"./runtime-DIcLIHcp.mjs";import{n as sanitizeTerminalText}from"./safe-text-B_QTgn0s.mjs";import{n as createLazyPromise}from"./lazy-promise-CismJCQR.mjs";import{t as formatCliCommand}from"./command-format-D7KhNLP0.mjs";import{i as getBundledChannelSetupPlugin}from"./bundled-CcedQErj.mjs";import{t as normalizeAnyChannelId}from"./registry-normalize-BP0gv89S.mjs";import{n as getLoadedChannelPlugin}from"./registry-CpNh6QcT.mjs";import"./plugins-DxOycUNJ.mjs";import{n as WizardCancelledError}from"./prompts-BviNnQo2.mjs";import{r as parseOptionalDelimitedEntries}from"./helpers-1A2j6LGz.mjs";import{a as formatUnknownChannelMessage,o as formatUnsupportedChannelActionMessage}from"./error-format-D9Q6t3gZ.mjs";import{r as isTerminalInteractive}from"./terminal-interactivity-ijJuF5PT.mjs";import{t as createClackPrompter}from"./clack-prompter-CkhFfk2k.mjs";import{n as resolveChannelSetupCliOptionMetadata,t as channelOmitsEnvBackedSetupOption}from"./cli-add-options-CMsgoebw.mjs";import{t as parseAccountSelector}from"./account-selector-j6B-yj9o.mjs";import{t as resolveChannelSetupOwner}from"./owner-DONBZpm3.mjs";import{i as withCommandPluginMetadata,r as requireValidConfigForWrite}from"./config-validation-BexrhW6U.mjs";import{t as normalizeExternalChannelSetupConfig}from"./config-compatibility-CHAw8-yb.mjs";import{t as persistChannelPluginConfig}from"./plugin-config-persistence-C297sR0X.mjs";import{i as prepareChannelAccountConfiguration,r as applyPreparedChannelAccountConfiguration,t as channelLabel}from"./runtime-label-DAo8graT.mjs";import"./shared-DqVdi65N.mjs";
//!src/commands/channels/add.ts
const loadOnboardChannels=createLazyPromise(()=>import(`./channel-setup-CmxGfPDH.mjs`));const CHANNEL_ADD_CONTROL_OPTION_KEYS=/* @__PURE__ */ new Set([`agent`,`channel`,`account`]);function buildChannelSetupInput(e,t){const n=t===`legacy`?resolveChannelSetupCliOptionMetadata(e.channel).valueMetadataByAttributeName:null;const r=Object.entries(e).filter(([e,t])=>!CHANNEL_ADD_CONTROL_OPTION_KEYS.has(e)&&t!==void 0);if(!n)return Object.fromEntries(r);const i={};for(const[e,t]of r){const r=n.get(e);if(r?.valueType!==`int`){i[e]=r?.valueType===`list`?Array.isArray(t)?t.filter(e=>typeof e===`string`):parseOptionalDelimitedEntries(typeof t===`string`?t:void 0):t;continue}if(t===null){i[e]=void 0;continue}const a=parseStrictNonNegativeInteger(t);if(a===void 0)throw new Error(`${r.longFlag} must be a non-negative integer.`);i[e]=a}return i}async function channelsAddCommand(e,t=defaultRuntime,n){try{parseAccountSelector(e.account);const r=await requireValidConfigForWrite(t);if(!r)return;const i=r.snapshot.sourceConfig;let a=i;let o=false;const effectContext=()=>({runtime:t,...n?.beforePersistentEffect?{beforePersistentEffect:n.beforePersistentEffect}:{}});if(n?.hasFlags===false){const{resolveInitialWizardChannelTarget:n,runChannelsAddWizardFlow:a,selectChannelSetupOwner:o}=await import(`./add-wizard-Bd5VRO41.mjs`);const s=createClackPrompter();if(!isTerminalInteractive()){t.error(channelOmitsEnvBackedSetupOption(e.channel)?`Interactive channel setup requires a TTY. Run ${formatCliCommand(`openclaw channels add --channel ${e.channel?.trim()||`<id>`} --help`)} to list the setup flags this channel accepts, then pass them for non-interactive setup.`:"Interactive channel setup requires a TTY. Use `openclaw channels add --channel <id> --use-env` or pass the channel's credential flags for non-interactive setup.");t.exit(1);return}const{agentId:c,workspaceDir:l}=await o(r,s,e.agent);const u=await n(e.channel,i,l);if(u.kind===`unresolved`){t.error(u.message);t.exit(1);return}await a({writeSnapshot:r,agentId:c,prompter:s,workspaceDir:l,...u.kind===`resolved`?{initialChannel:u.channel}:{},...effectContext()});return}const s=e.channel??``;let c=normalizeAnyChannelId(s);let l;const resolveWorkspaceDir=()=>l??=resolveChannelSetupOwner(i,e.agent).workspaceDir;const u=normalizeOptionalLowercaseString(s);let d=u?(await import(`./trusted-catalog-HaKV4eKy.mjs`)).resolveTrustedChannelCatalogInput(u,{cfg:a,workspaceDir:resolveWorkspaceDir()}):void 0;const loadScopedPlugin=async(e,n)=>{const r=getLoadedChannelPlugin(e);if(r?.setupContract?.applyAccountConfig||r?.setup?.applyAccountConfig)return r;const{loadChannelSetupPluginRegistrySnapshotForChannel:i}=await import(`./plugin-install-DL062hGa.mjs`);const o=await i({cfg:a,runtime:t,channel:e,...n?{pluginId:n}:{},workspaceDir:resolveWorkspaceDir(),forceSetupOnlyChannelPlugins:true});return o.channelSetups.find(t=>t.plugin.id===e)?.plugin??getBundledChannelSetupPlugin(e)??o.channels.find(t=>t.plugin.id===e)?.plugin??r};if(d){const e=resolveWorkspaceDir();const{isCatalogChannelInstalled:n}=await import(`./discovery-DQ7F9ibH.mjs`);const r=c?getLoadedChannelPlugin(c):void 0;const i=c?getBundledChannelSetupPlugin(c):void 0;if(!r&&!i&&!n({cfg:a,entry:d,workspaceDir:e})){const{ensureChannelSetupPluginInstalled:n}=await import(`./plugin-install-DL062hGa.mjs`);const r=createClackPrompter();const i=await n({cfg:a,entry:d,prompter:r,workspaceDir:e,promptInstall:false,promptLocalFallback:isTerminalInteractive(),failureOutput:`caller`,...effectContext()});if(!i.installed){const e=i.error||(i.status===`skipped`?`No usable install source.`:i.status===`timed_out`?`The install timed out.`:``);t.error(`Could not install the plugin for channel "${sanitizeTerminalText(d.id)}". Install the plugin manually or verify the install source, then retry.${e?`\n${e}`:``}`);t.exit(1);return}a=i.cfg;o=true;d={...d,...i.pluginId?{pluginId:i.pluginId}:{}}}c??=normalizeAnyChannelId(d.id)??d.id}if(!c){const e=d?`Plugin ${d.meta.label} could not be loaded after install. Run openclaw doctor --fix, then retry openclaw channels add.`:formatUnknownChannelMessage({channel:s});t.error(e);t.exit(1);return}const f=c;const unsupportedAddMessage=()=>`${formatUnsupportedChannelActionMessage({channel:f,action:`non-interactive add`})} Run ${formatCliCommand(`openclaw channels add`)} with no flags for guided setup.`;return await withCommandPluginMetadata({config:a,workspaceDir:resolveWorkspaceDir()},async()=>{const s=await loadScopedPlugin(f,d?.pluginId);if(!s){t.error(unsupportedAddMessage());t.exit(1);return}const c=await prepareChannelAccountConfiguration({cfg:a,plugin:s,requestedAccountId:e.account,resolveInput:()=>buildChannelSetupInput(e,s.setupContract?`contract`:`legacy`),...effectContext()});if(!c.ok){t.error(c.error.kind===`unsupported`?unsupportedAddMessage():c.error.message);t.exit(1);return}const l=await applyPreparedChannelAccountConfiguration({cfg:a,channel:f,prepared:c.value,...effectContext()});a=normalizeExternalChannelSetupConfig({cfg:l.nextConfig,channel:f});const u=await persistChannelPluginConfig({cfg:a,setup:{previousConfig:i,beforePersistentEffect:n?.beforePersistentEffect,assertCurrent:r.writeOptions.assertConfigPathForWrite},pluginInstalled:o,writeOptions:r.writeOptions,baseHash:r.snapshot.hash,runtime:t});t.log(`Added ${s.meta.label??channelLabel(f)} account "${l.accountId}".`);const p=l.afterAccountConfigWritten;if(p){const{runCollectedChannelOnboardingPostWriteHooks:e}=await loadOnboardChannels();await e({hooks:[{channel:f,accountId:l.accountId,run:async({cfg:e,runtime:t})=>await p({previousCfg:i,cfg:e,accountId:l.accountId,input:l.input,runtime:t})}],configPath:u.path,...effectContext()})}})}catch(e){if(e instanceof WizardCancelledError){t.exit(1);return}throw e}}export{channelsAddCommand};