import{t as e}from"./rolldown-runtime-CNxR59P3.js";import{t}from"./paths-B4IRk3wi.js";import{C as n,S as r,t as i,x as a,y as o}from"./utils-D1PlBTFH.js";import{Eo as s,Ml as c,Nc as l,Oc as u,Si as d,kc as f,vl as p,xi as m,xo as ee,yo as te}from"./model-selection-p21GBPS6.js";import{D as h,P as g,v as _}from"./agent-scope-DW6ZDCxj.js";import{t as v}from"./exec-BdtFni8N.js";import{s as y}from"./paths-yLEk_25I.js";import{n as b}from"./call-_dlYen_W.js";import{t as x}from"./wsl-Ok7GaSJ6.js";import{r as S}from"./prompt-style-DpAh867d.js";import C from"node:path";import{inspect as w}from"node:util";import T from"node:fs/promises";import E from"node:crypto";import{cancel as D,isCancel as O}from"@clack/prompts";const k=`/avatar`;function A(e){if(!e)return``;let t=e.trim();return!t||(t.startsWith(`/`)||(t=`/${t}`),t===`/`)?``:(t.endsWith(`/`)&&(t=t.slice(0,-1)),t)}function j(e,t){return e?`${e}${k}/${t}`:`${k}/${t}`}function ne(e){let t=e.avatar?.trim();if(!t)return;if(u(t)||f(t))return t;let n=A(e.basePath),r=n?`${n}${k}/`:`${k}/`;return n&&t.startsWith(`/avatar/`)?`${n}${t}`:t.startsWith(r)||!e.agentId?t:l(t)?j(n,e.agentId):t}var re=e({DEFAULT_WORKSPACE:()=>Q,applyWizardMetadata:()=>R,detectBinary:()=>J,detectBrowserOpenSupport:()=>B,ensureWorkspaceAndSessions:()=>W,formatControlUiSshHint:()=>V,guardCancel:()=>M,handleReset:()=>q,moveToTrash:()=>K,normalizeGatewayTokenInput:()=>F,openUrl:()=>U,printWizardHeader:()=>L,probeGatewayReachable:()=>X,randomToken:()=>P,resolveBrowserOpenCommand:()=>z,resolveControlUiLinks:()=>$,resolveNodeManagerOptions:()=>G,summarizeExistingConfig:()=>N,validateGatewayPasswordInput:()=>I,waitForGatewayReachable:()=>Z});function M(e,t){if(O(e))throw D(S(`Setup cancelled.`)??`Setup cancelled.`),t.exit(0),Error(`unreachable`);return e}function N(e){let t=[],n=e.agents?.defaults;if(n?.workspace&&t.push(a(`workspace: ${n.workspace}`)),n?.model){let e=g(n.model);e&&t.push(a(`model: ${e}`))}return e.gateway?.mode&&t.push(a(`gateway.mode: ${e.gateway.mode}`)),typeof e.gateway?.port==`number`&&t.push(a(`gateway.port: ${e.gateway.port}`)),e.gateway?.bind&&t.push(a(`gateway.bind: ${e.gateway.bind}`)),e.gateway?.remote?.url&&t.push(a(`gateway.remote.url: ${e.gateway.remote.url}`)),e.skills?.install?.nodeManager&&t.push(a(`skills.nodeManager: ${e.skills.install.nodeManager}`)),t.length?t.join(`
`):`No key settings detected.`}function P(){return E.randomBytes(24).toString(`hex`)}function F(e){if(typeof e!=`string`)return``;let t=e.trim();return t===`undefined`||t===`null`?``:t}function I(e){if(typeof e!=`string`)return`Required`;let t=e.trim();if(!t)return`Required`;if(t===`undefined`||t===`null`)return`Cannot be the literal string "undefined" or "null"`}function L(e){let t=[`▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄`,`██░▄▄▄░██░▄▄░██░▄▄▄██░▀██░██░▄▄▀██░████░▄▄▀██░███░██`,`██░███░██░▀▀░██░▄▄▄██░█░█░██░█████░████░▀▀░██░█░█░██`,`██░▀▀▀░██░█████░▀▀▀██░██▄░██░▀▀▄██░▀▀░█░██░██▄▀▄▀▄██`,`▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀`,`                  🦞 OPENCLAW 🦞                    `,` `].join(`
`);e.log(t)}function R(e,t){let n=process.env.GIT_COMMIT?.trim()||process.env.GIT_SHA?.trim()||void 0;return{...e,wizard:{...e.wizard,lastRunAt:new Date().toISOString(),lastRunVersion:c,lastRunCommit:n,lastRunCommand:t.command,lastRunMode:t.mode}}}async function z(){let e=process.platform,t=!!(process.env.DISPLAY||process.env.WAYLAND_DISPLAY);if((process.env.SSH_CLIENT||process.env.SSH_TTY||process.env.SSH_CONNECTION)&&!t&&e!==`win32`)return{argv:null,reason:`ssh-no-display`};if(e===`win32`)return{argv:[`cmd`,`/c`,`start`,``],command:`cmd`,quoteUrl:!0};if(e===`darwin`)return await J(`open`)?{argv:[`open`],command:`open`}:{argv:null,reason:`missing-open`};if(e===`linux`){let e=await x();if(!t&&!e)return{argv:null,reason:`no-display`};if(e){if(await J(`wslview`))return{argv:[`wslview`],command:`wslview`};if(!t)return{argv:null,reason:`wsl-no-wslview`}}return await J(`xdg-open`)?{argv:[`xdg-open`],command:`xdg-open`}:{argv:null,reason:`missing-xdg-open`}}return{argv:null,reason:`unsupported-platform`}}async function B(){let e=await z();return e.argv?{ok:!0,command:e.command}:{ok:!1,reason:e.reason}}function V(e){let t=A(e.basePath),n=t?`${t}/`:`/`,r=`http://localhost:${e.port}${n}`,i=e.token?`${r}#token=${encodeURIComponent(e.token)}`:void 0,a=H();return[`No GUI detected. Open from your computer:`,`ssh -N -L ${e.port}:127.0.0.1:${e.port} ${a}`,`Then open:`,r,i,`Docs:`,`https://docs.openclaw.ai/gateway/remote`,`https://docs.openclaw.ai/web/control-ui`].filter(Boolean).join(`
`)}function H(){return`${process.env.USER||process.env.LOGNAME||`user`}@${process.env.SSH_CONNECTION?.trim().split(/\s+/)?.[2]??`<host>`}`}async function U(e){if(Y())return!1;let t=await z();if(!t.argv)return!1;let n=t.quoteUrl===!0,r=[...t.argv];n?(r.at(-1)===``&&(r[r.length-1]=`""`),r.push(`"${e}"`)):r.push(e);try{return await v(r,{timeoutMs:5e3,windowsVerbatimArguments:n}),!0}catch{return!1}}async function W(e,t,n){let i=await h({dir:e,ensureBootstrapFiles:!n?.skipBootstrap});t.log(`Workspace OK: ${r(i.dir)}`);let a=y(n?.agentId);await T.mkdir(a,{recursive:!0}),t.log(`Sessions OK: ${r(a)}`)}function G(){return[{value:`npm`,label:`npm`},{value:`pnpm`,label:`pnpm`},{value:`bun`,label:`bun`}]}async function K(e,t){if(e){try{await T.access(e)}catch{return}try{await v([`trash`,e],{timeoutMs:5e3}),t.log(`Moved to Trash: ${r(e)}`)}catch{t.log(`Failed to move to Trash (manual delete): ${r(e)}`)}}}async function q(e,n,r){await K(t,r),e!==`config`&&(await K(C.join(i,`credentials`),r),await K(y(),r),e===`full`&&await K(n,r))}async function J(e){if(!e?.trim()||!p(e))return!1;let t=e.startsWith(`~`)?o(e):e;if(C.isAbsolute(t)||t.startsWith(`.`)||t.includes(`/`)||t.includes(`\\`))try{return await T.access(t),!0}catch{return!1}let n=process.platform===`win32`?[`where`,e]:[`/usr/bin/env`,`which`,e];try{let e=await v(n,{timeoutMs:2e3});return e.code===0&&e.stdout.trim().length>0}catch{return!1}}function Y(){return!!process.env.VITEST}async function X(e){let t=e.url.trim(),n=e.timeoutMs??1500;try{return await b({url:t,token:e.token,password:e.password,method:`health`,timeoutMs:n,clientName:d.PROBE,mode:m.PROBE}),{ok:!0}}catch(e){return{ok:!1,detail:ie(e)}}}async function Z(e){let t=e.deadlineMs??15e3,r=e.pollMs??400,i=e.probeTimeoutMs??1500,a=Date.now(),o;for(;Date.now()-a<t;){let t=await X({url:e.url,token:e.token,password:e.password,timeoutMs:i});if(t.ok)return t;o=t.detail,await n(r)}return{ok:!1,detail:o}}function ie(e){let t=`unknown error`;e instanceof Error?t=e.message||t:typeof e==`string`?t=e||t:e!==void 0&&(t=w(e,{depth:2}));let n=t.split(`
`).map(e=>e.trim()).find(Boolean)??t;return n.length>120?`${n.slice(0,119)}…`:n}const Q=_;function $(e){let t=e.port,n=e.bind??`loopback`,r=e.customBindHost?.trim(),i=s(),a=n===`custom`&&r&&te(r)?r:n===`tailnet`&&i?i??`127.0.0.1`:n===`lan`?ee()??`127.0.0.1`:`127.0.0.1`,o=A(e.basePath),c=o?`${o}/`:`/`,l=o||``;return{httpUrl:`http://${a}:${t}${c}`,wsUrl:`ws://${a}:${t}${l}`}}export{A as C,j as S,G as _,W as a,Z as b,q as c,re as d,U as f,$ as g,P as h,B as i,K as l,X as m,R as n,V as o,L as p,J as r,M as s,Q as t,F as u,N as v,ne as w,k as x,I as y};
