import{m as e,p as t}from"./subsystem-kTFXkQM5.js";import{u as n}from"./paths-BjD3T_xq.js";import{Bo as r,Fo as i}from"./auth-profiles-qXC1L8hs.js";import{t as a}from"./command-format-CLxIPuLR.js";import{v as o}from"./utils-7Gdj77QB.js";import{s}from"./types.secrets-D4tbc3Wq.js";import{At as c}from"./plugins-CCU8jDti.js";import{a as l,b as u,c as d,g as f,h as p,n as m,t as h,u as g}from"./onboard-helpers-DX-C8lCE.js";import{t as _}from"./prompts-CMzglboN.js";import{t as v}from"./clack-prompter-B1OXkeHa.js";import{t as y}from"./runtime-guard-BFK4A89C.js";import"./daemon-runtime-VcpeBRJV.js";import{t as b}from"./onboarding-CSHa4-b0.js";import{n as x}from"./logging-DDgQDeNF.js";import{i as S,r as C,t as w}from"./onboard-provider-auth-flags-CqbipO7D.js";import{t as T}from"./onboard-config-B7U20YRw.js";async function E(n,r=t){let i=v(),a=null;try{await b(n,r,i)}catch(e){if(e instanceof _){a=1;return}throw e}finally{e(`onboarding finish`,{resumeStdinIfPaused:!1}),a!==null&&r.exit(a)}}function D(e){return typeof e==`string`?e.trim().length>0:!!e}function O(e){let t=w.filter(({optionKey:t})=>D(e[t])).map(e=>({optionKey:e.optionKey,authChoice:e.authChoice,label:e.cliFlag}));return(D(e.customBaseUrl)||D(e.customModelId)||D(e.customApiKey))&&t.push({optionKey:`customBaseUrl`,authChoice:`custom-api-key`,label:`--custom-base-url/--custom-model-id/--custom-api-key`}),{choice:t[0]?.authChoice,matches:t}}function k(e){let{opts:t,runtime:n}=e,r=t.gatewayPort!==void 0;if(r&&(!Number.isFinite(t.gatewayPort)||(t.gatewayPort??0)<=0))return n.error(`Invalid --gateway-port`),n.exit(1),null;let i=r?t.gatewayPort:e.defaultPort,a=t.gatewayBind??`loopback`,o=t.gatewayAuth??`token`;if(o!==`token`&&o!==`password`)return n.error(`Invalid --gateway-auth (use token|password).`),n.exit(1),null;let l=o,u=t.tailscale??`off`,d=!!t.tailscaleResetOnExit;u!==`off`&&a!==`loopback`&&(a=`loopback`),u===`funnel`&&l!==`password`&&(l=`password`);let f=e.nextConfig,m=g(t.gatewayToken),h=g(process.env.OPENCLAW_GATEWAY_TOKEN),_=m||h||void 0,v=String(t.gatewayTokenRefEnv??``).trim();if(l===`token`)if(v){if(!s(v))return n.error(`Invalid --gateway-token-ref-env (use env var name like OPENCLAW_GATEWAY_TOKEN).`),n.exit(1),null;if(m)return n.error(`Use either --gateway-token or --gateway-token-ref-env, not both.`),n.exit(1),null;let e=process.env[v]?.trim();if(!e)return n.error(`Environment variable "${v}" is missing or empty.`),n.exit(1),null;_=e,f={...f,gateway:{...f.gateway,auth:{...f.gateway?.auth,mode:`token`,token:{source:`env`,provider:c(f,`env`,{preferFirstProviderForSource:!0}),id:v}}}}}else _||=p(),f={...f,gateway:{...f.gateway,auth:{...f.gateway?.auth,mode:`token`,token:_}}};if(l===`password`){let e=t.gatewayPassword?.trim();if(!e)return n.error(`Missing --gateway-password for password auth.`),n.exit(1),null;f={...f,gateway:{...f.gateway,auth:{...f.gateway?.auth,mode:`password`,password:e}}}}return f={...f,gateway:{...f.gateway,port:i,bind:a,tailscale:{...f.gateway?.tailscale,mode:u,resetOnExit:d}}},{nextConfig:f,port:i,bind:a,authMode:l,tailscaleMode:u,tailscaleResetOnExit:d,gatewayToken:_}}function A(e){e.opts.json&&e.runtime.log(JSON.stringify({mode:e.mode,workspace:e.workspaceDir,authChoice:e.authChoice,gateway:e.gateway,installDaemon:!!e.installDaemon,daemonRuntime:e.daemonRuntime,skipSkills:!!e.skipSkills,skipHealth:!!e.skipHealth},null,2))}function j(e){let{nextConfig:t,opts:n,runtime:r}=e;if(n.skipSkills)return t;let i=n.nodeManager??`npm`;return[`npm`,`pnpm`,`bun`].includes(i)?{...t,skills:{...t.skills,install:{...t.skills?.install,nodeManager:i}}}:(r.error(`Invalid --node-manager (use npm, pnpm, or bun)`),r.exit(1),t)}function M(e){return o((e.opts.workspace??e.baseConfig.agents?.defaults?.workspace??e.defaultWorkspaceDir).trim())}async function N(e){let{opts:t,runtime:i,baseConfig:o}=e,s=`local`,c=M({opts:t,baseConfig:o,defaultWorkspaceDir:h}),d=T(o,c),p=O(t);if(!t.authChoice&&p.matches.length>1){i.error([`Multiple API key flags were provided for non-interactive onboarding.`,`Use a single provider flag or pass --auth-choice explicitly.`,`Flags: ${p.matches.map(e=>e.label).join(`, `)}`].join(`
`)),i.exit(1);return}let g=t.authChoice??p.choice??`skip`;if(g!==`skip`){let{applyNonInteractiveAuthChoice:e}=await import(`./auth-choice-B7HdmV3A.js`),n=await e({nextConfig:d,authChoice:g,opts:t,runtime:i,baseConfig:o});if(!n)return;d=n}let _=n(o),v=k({nextConfig:d,opts:t,runtime:i,defaultPort:_});if(!v)return;if(d=v.nextConfig,d=j({nextConfig:d,opts:t,runtime:i}),d=m(d,{command:`onboard`,mode:s}),await r(d),x(i),await l(c,i,{skipBootstrap:!!d.agents?.defaults?.skipBootstrap}),t.installDaemon){let{installGatewayDaemonNonInteractive:e}=await import(`./daemon-install-CF7U5y50.js`);await e({nextConfig:d,opts:t,runtime:i,port:v.port})}let y=t.daemonRuntime??`node`;if(!t.skipHealth){let{healthCommand:e}=await import(`./health-DXnmCj1T.js`).then(e=>e.i);await u({url:f({bind:v.bind,port:v.port,customBindHost:d.gateway?.customBindHost,basePath:void 0}).wsUrl,token:v.gatewayToken,deadlineMs:15e3}),await e({json:!1,timeoutMs:1e4},i)}A({opts:t,runtime:i,mode:s,workspaceDir:c,authChoice:g,gateway:{port:v.port,bind:v.bind,authMode:v.authMode,tailscaleMode:v.tailscaleMode},installDaemon:!!t.installDaemon,daemonRuntime:t.installDaemon?y:void 0,skipSkills:!!t.skipSkills,skipHealth:!!t.skipHealth}),t.json||i.log(`Tip: run \`${a(`openclaw configure --section web`)}\` to store your Brave API key for web_search. Docs: https://docs.openclaw.ai/tools/web`)}async function P(e){let{opts:t,runtime:n,baseConfig:i}=e,o=`remote`,s=t.remoteUrl?.trim();if(!s){n.error(`Missing --remote-url for remote mode.`),n.exit(1);return}let c={...i,gateway:{...i.gateway,mode:`remote`,remote:{url:s,token:t.remoteToken?.trim()||void 0}}};c=m(c,{command:`onboard`,mode:o}),await r(c),x(n);let l={mode:o,remoteUrl:s,auth:t.remoteToken?`token`:`none`};t.json?n.log(JSON.stringify(l,null,2)):(n.log(`Remote gateway: ${s}`),n.log(`Auth: ${l.auth}`),n.log(`Tip: run \`${a(`openclaw configure --section web`)}\` to store your Brave API key for web_search. Docs: https://docs.openclaw.ai/tools/web`))}async function F(e,n=t){let r=await i();if(r.exists&&!r.valid){n.error(`Config invalid. Run \`${a(`openclaw doctor`)}\` to repair it, then re-run onboarding.`),n.exit(1);return}let o=r.valid&&r.exists?r.config:{},s=e.mode??`local`;if(s!==`local`&&s!==`remote`){n.error(`Invalid --mode "${String(s)}" (use local|remote).`),n.exit(1);return}if(s===`remote`){await P({opts:e,runtime:n,baseConfig:o});return}await N({opts:e,runtime:n,baseConfig:o})}const I=new Set([`config`,`config+creds+sessions`,`full`]);async function L(e,n=t){y(n);let r=e.authChoice,s=S(r);if(e.nonInteractive&&C(r)){n.error([`Auth choice "${String(r)}" is deprecated.`,`Use "--auth-choice token" (Anthropic setup-token) or "--auth-choice openai-codex".`].join(`
`)),n.exit(1);return}r===`claude-cli`&&n.log(`Auth choice "claude-cli" is deprecated; using setup-token flow instead.`),r===`codex-cli`&&n.log(`Auth choice "codex-cli" is deprecated; using OpenAI Codex OAuth instead.`);let c=e.flow===`manual`?`advanced`:e.flow,l=s===e.authChoice&&c===e.flow?e:{...e,authChoice:s,flow:c};if(l.secretInputMode&&l.secretInputMode!==`plaintext`&&l.secretInputMode!==`ref`){n.error(`Invalid --secret-input-mode. Use "plaintext" or "ref".`),n.exit(1);return}if(l.resetScope&&!I.has(l.resetScope)){n.error(`Invalid --reset-scope. Use "config", "config+creds+sessions", or "full".`),n.exit(1);return}if(l.nonInteractive&&l.acceptRisk!==!0){n.error([`Non-interactive onboarding requires explicit risk acknowledgement.`,`Read: https://docs.openclaw.ai/security`,`Re-run with: ${a(`openclaw onboard --non-interactive --accept-risk ...`)}`].join(`
`)),n.exit(1);return}if(l.reset){let e=await i(),t=e.valid?e.config:{},r=l.workspace??t.agents?.defaults?.workspace??h;await d(l.resetScope??`config+creds+sessions`,o(r),n)}if(process.platform===`win32`&&n.log([`Windows detected — OpenClaw runs great on WSL2!`,`Native Windows might be trickier.`,`Quick setup: wsl --install (one command, one reboot)`,`Guide: https://docs.openclaw.ai/windows`].join(`
`)),l.nonInteractive){await F(l,n);return}await E(l,n)}export{L as t};
