import{c as e,s as t,u as n}from"./envUtils-BWeoiL4Y.js";import{In as r,Ln as i}from"./src-Di342QoJ.js";import{Ct as ee,U as a,cn as o,dr as s,jr as c,lr as l,ut as te}from"./state-sIHsFpDu.js";import{Cc as u,Cn as ne,at as d,dc as f,it as p,lc as re,on as m,rt as h,st as g,yc as _}from"./paths-Ci1iwL5Y.js";import{U as v,z as y}from"./debug-otxXY3j-.js";import{n as b,r as x}from"./analytics-DqMQntaB.js";import{n as S,t as C}from"./cwd--iizewsn.js";import{F as w,P as T,f as ie,n as E,r as D,v as O}from"./git-C0IuvuIy.js";import{a as k,n as A}from"./env-C2A9gV_G.js";import{o as j,s as M}from"./log-DSV4Ht5l.js";import{a as N,n as P}from"./startupProfiler-CAntGAbs.js";import{Cn as F,Di as I,Fr as L,It as R,KE as z,Ot as B,Qk as V,Sn as H,St as U,Zi as W,_C as G,by as K,dy as q,gC as J,gx as Y,hC as ae,mh as X,na as oe,ph as se,qE as ce,qk as le,ux as ue,uy as de,vy as fe,wt as Z,xt as pe}from"./loadAgentsDir-C_wFhytF.js";import{l as me,u as he}from"./terminalSetup-C1X-sXwp.js";import{c as ge,n as _e}from"./releaseNotes-C8KQGZHq.js";import{s as ve,u as ye}from"./logoV2Utils-BJHvwGup.js";import{i as be,r as xe}from"./runtimeObserver-DaKaKNVW.js";import{n as Q,r as Se}from"./sessionMemory-Ctauybzs.js";import{t as Ce}from"./sinks-Dc9V7k6a.js";import{homedir as we}from"os";import{join as Te}from"path";import{copyFile as Ee,stat as De}from"fs/promises";t(),i(),o(),b(),S(),v(),T(),k(),O(),m(),g(),h(),V(),ce(),J(),he(),de(),se(),oe(),L(),F(),ge(),be(),Se(),_(),j();function $(){u(e=>({...e,iterm2SetupInProgress:!1}))}function Oe(){let e=f();return{inProgress:e.iterm2SetupInProgress??!1,backupPath:e.iterm2BackupPath||null}}function ke(){return Te(we(),`Library`,`Preferences`,`com.googlecode.iterm2.plist`)}async function Ae(){let{inProgress:e,backupPath:t}=Oe();if(!e)return{status:`no_backup`};if(!t)return $(),{status:`no_backup`};try{await De(t)}catch{return $(),{status:`no_backup`}}try{return await Ee(t,ke()),$(),{status:`restored`}}catch(e){return M(Error(`Failed to restore iTerm2 settings with: ${e}`)),$(),{status:`failed`,backupPath:t}}}_(),j(),ye(),ue(),K(),P(),B();async function je(t,i,o,u,m,h,g,_,v){w(`info`,`setup_started`);let b=process.version.match(/^v(\d+)\./)?.[1];if((!b||parseInt(b,10)<18)&&(console.error(r.bold.red(`Error: Claude Code requires Node.js version 18 or higher.`)),process.exit(1)),g&&c(z(g)),e(),!e()&&p()){let{captureTeammateModeSnapshot:e}=await import(`./teammateModeSnapshot-BDJ1JTcl.js`);e()}if(!a()){if(p()){let e=await Ae();e.status===`restored`?console.log(r.yellow(`Detected an interrupted iTerm2 setup. Your original settings have been restored. You may need to restart iTerm2 for the changes to take effect.`)):e.status===`failed`&&console.error(r.red(`Failed to restore iTerm2 settings. Please manually restore your original settings with: defaults import com.googlecode.iterm2 ${e.backupPath}.`))}try{let e=await me();e.status===`restored`?console.log(r.yellow(`Detected an interrupted Terminal.app setup. Your original settings have been restored. You may need to restart Terminal.app for the changes to take effect.`)):e.status===`failed`&&console.error(r.red(`Failed to restore Terminal.app settings. Please manually restore your original settings with: defaults import com.apple.Terminal ${e.backupPath}.`))}catch(e){M(e)}}X(t);let S=Date.now();if(ae(),w(`info`,`setup_hooks_captured`,{duration_ms:Date.now()-S}),q(t),u){let e=H(),n=await ie();!e&&!n&&(process.stderr.write(r.red(`Error: Can only use --worktree in a git repository, but ${r.bold(t)} is not a git repository. Configure a WorktreeCreate hook in settings.json to use --worktree with other VCS systems.\n`)),process.exit(1));let i=_?`pr-${_}`:m??fe(),a;if(n){let e=E(C());e||(process.stderr.write(r.red(`Error: Could not determine the main git repository root.
`)),process.exit(1)),e!==(D(C())??C())&&(w(`info`,`worktree_resolved_to_main_repo`),process.chdir(e),X(e)),a=h?Z(e,R(i)):void 0}else a=h?Z(C(),R(i)):void 0;let o;try{o=await U(ee(),i,a,_?{prNumber:_}:void 0)}catch(e){process.stderr.write(r.red(`Error creating worktree: ${y(e)}\n`)),process.exit(1)}if(x(`tengu_worktree_created`,{tmux_enabled:h}),h&&a){let e=await pe(a,o.worktreePath);e.created?console.log(r.green(`Created tmux session: ${r.bold(a)}\nTo attach: ${r.bold(`tmux attach -t ${a}`)}`)):console.error(r.yellow(`Warning: Failed to create tmux session: ${e.error}`))}process.chdir(o.worktreePath),X(o.worktreePath),l(C()),s(C()),I(o),le(),G()}w(`info`,`setup_background_jobs_starting`),e()||(Q(),xe()),Y(),w(`info`,`setup_background_jobs_launched`),N(`setup_before_prefetch`),w(`info`,`setup_prefetch_starting`);let T=a()&&n(process.env.CLAUDE_CODE_SYNC_PLUGIN_INSTALL)||e();if(T||W(te()),import(`./loadPluginHooks-CYZjzxyO.js`).then(e=>{T||(e.loadPluginHooks(),e.setupPluginHookHotReload())}),e()||(process.env.USER_TYPE===`ant`&&import(`./commitAttribution-CH9C_hoL.js`).then(async e=>{if(await e.isInternalModelRepo()){let{clearSystemPromptSections:e}=await import(`./systemPromptSections-CVpefuSD.js`);e()}}),setImmediate(()=>{import(`./attributionHooks-VGSpz4mk.js`).then(({registerAttributionHooks:e})=>{e()})}),import(`./sessionFileAccessHooks-BXsK8Dwf.js`).then(e=>e.registerSessionFileAccessHooks())),Ce(),x(`tengu_started`,{}),ne(a()),N(`setup_after_prefetch`),!e()){let{hasReleaseNotes:e}=await _e(f().lastReleaseNotesSeen);e&&await ve()}if((i===`bypassPermissions`||o)&&(process.platform!==`win32`&&typeof process.getuid==`function`&&process.getuid()===0&&process.env.IS_SANDBOX!==`1`&&!n(process.env.CLAUDE_CODE_BUBBLEWRAP)&&(console.error(`--dangerously-skip-permissions cannot be used with root/sudo privileges for security reasons`),process.exit(1)),process.env.USER_TYPE===`ant`&&process.env.CLAUDE_CODE_ENTRYPOINT!==`local-agent`&&process.env.CLAUDE_CODE_ENTRYPOINT!==`claude-desktop`)){let[e,t]=await Promise.all([d.getIsDocker(),A.hasInternetAccess()]),n=d.getIsBubblewrapSandbox(),r=process.env.IS_SANDBOX===`1`;(!(e||n||r)||t)&&(console.error(`--dangerously-skip-permissions can only be used in Docker/sandbox containers with no internet access but got Docker: ${e}, Bubblewrap: ${n}, IS_SANDBOX: ${r}, hasInternet: ${t}`),process.exit(1))}let O=re();O.lastCost!==void 0&&O.lastDuration!==void 0&&x(`tengu_exit`,{last_session_cost:O.lastCost,last_session_api_duration:O.lastAPIDuration,last_session_tool_duration:O.lastToolDuration,last_session_duration:O.lastDuration,last_session_lines_added:O.lastLinesAdded,last_session_lines_removed:O.lastLinesRemoved,last_session_total_input_tokens:O.lastTotalInputTokens,last_session_total_output_tokens:O.lastTotalOutputTokens,last_session_total_cache_creation_input_tokens:O.lastTotalCacheCreationInputTokens,last_session_total_cache_read_input_tokens:O.lastTotalCacheReadInputTokens,last_session_fps_average:O.lastFpsAverage,last_session_fps_low_1_pct:O.lastFpsLow1Pct,last_session_id:O.lastSessionId,...O.lastSessionMetrics})}export{je as setup};