@openchamber/web 1.15.0 → 1.16.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (102) hide show
  1. package/README.md +5 -5
  2. package/dist/assets/{DiagramView-HR2xZxJ8.js → DiagramView-Elkp8b4l.js} +1 -1
  3. package/dist/assets/{MarkdownRendererImpl-sm01Q7R7.js → MarkdownRendererImpl-DLhrL6oX.js} +3 -3
  4. package/dist/assets/{MultiRunWindow-DlFUaNQK.js → MultiRunWindow-DVpNCqnE.js} +1 -1
  5. package/dist/assets/{OnboardingScreen-DVRz82mJ.js → OnboardingScreen-CpcR0gyq.js} +1 -1
  6. package/dist/assets/SessionSwitcherDropdown-DHO5Je6v.js +1 -0
  7. package/dist/assets/{SettingsWindow-CkInZcj7.js → SettingsWindow-Bewo7vwh.js} +1 -1
  8. package/dist/assets/{TerminalView-D_9BAjtv.js → TerminalView-D9C7fNLm.js} +3 -3
  9. package/dist/assets/es-D9nOo8Wx.js +15 -0
  10. package/dist/assets/fr-DEF71lgt.js +13 -0
  11. package/dist/assets/index-BklNmnVr.js +2 -0
  12. package/dist/assets/index-C3uMIExE.css +1 -0
  13. package/dist/assets/ja-CpNfaEje.js +15 -0
  14. package/dist/assets/ko-DLS_rnE-.js +15 -0
  15. package/dist/assets/main-B0CxEOP5.js +237 -0
  16. package/dist/assets/{main-DzNa8r0N.js → main-Ck3ZNw8-.js} +2 -2
  17. package/dist/assets/miniChat-BAJj2Ome.js +2 -0
  18. package/dist/assets/mobile-CJoX4Cdj.js +2 -0
  19. package/dist/assets/pl-BjtFn5-n.js +15 -0
  20. package/dist/assets/pt-BR-C0_4Rnzn.js +15 -0
  21. package/dist/assets/renderElectronMiniChatApp-D4-OXCXM.js +2 -0
  22. package/dist/assets/renderMobileApp-DHWdI195.js +16 -0
  23. package/dist/assets/runtimeEndpointReset-B0P2DED1.js +167 -0
  24. package/dist/assets/uk-BDu4E0s0.js +15 -0
  25. package/dist/assets/{useAppFontEffects-DzBg8hrC.js → useAppFontEffects-9LFMCKYW.js} +195 -136
  26. package/dist/assets/zh-CN-BcwMqxIR.js +15 -0
  27. package/dist/assets/zh-TW-bNDQjKEu.js +15 -0
  28. package/dist/index.html +3 -3
  29. package/dist/mini-chat.html +3 -3
  30. package/dist/mobile.html +3 -3
  31. package/package.json +1 -1
  32. package/server/index.js +106 -1
  33. package/server/lib/notifications/DOCUMENTATION.md +1 -1
  34. package/server/lib/notifications/apns-runtime.js +3 -1
  35. package/server/lib/notifications/runtime.js +135 -33
  36. package/server/lib/opencode/DOCUMENTATION.md +1 -0
  37. package/server/lib/opencode/agents.js +3 -0
  38. package/server/lib/opencode/bootstrap-runtime.js +5 -0
  39. package/server/lib/opencode/core-routes.js +75 -2
  40. package/server/lib/opencode/core-routes.test.js +81 -0
  41. package/server/lib/opencode/feature-routes-runtime.js +6 -0
  42. package/server/lib/opencode/settings-helpers.js +30 -0
  43. package/server/lib/opencode/settings-helpers.test.js +14 -0
  44. package/server/lib/opencode/settings-normalization-runtime.js +19 -4
  45. package/server/lib/opencode/settings-normalization-runtime.test.js +21 -0
  46. package/server/lib/opencode/settings-runtime.js +25 -0
  47. package/server/lib/opencode/shutdown-runtime.js +2 -0
  48. package/server/lib/opencode/tunnel-auth.js +4 -9
  49. package/server/lib/package-manager.js +1 -1
  50. package/server/lib/permission-auto-accept/DOCUMENTATION.md +34 -0
  51. package/server/lib/permission-auto-accept/runtime.js +263 -0
  52. package/server/lib/permission-auto-accept/runtime.test.js +137 -0
  53. package/server/lib/projects/project-config.js +8 -0
  54. package/server/lib/quota/DOCUMENTATION.md +5 -2
  55. package/server/lib/quota/credentials/providers.js +43 -0
  56. package/server/lib/quota/credentials/store.js +48 -0
  57. package/server/lib/quota/credentials/store.test.js +26 -0
  58. package/server/lib/quota/opencode-go-credentials.js +11 -0
  59. package/server/lib/quota/opencode-go-credentials.test.js +32 -0
  60. package/server/lib/quota/providers/codex.js +7 -4
  61. package/server/lib/quota/providers/codex.test.js +48 -0
  62. package/server/lib/quota/providers/cursor.js +28 -39
  63. package/server/lib/quota/providers/index.js +7 -0
  64. package/server/lib/quota/providers/ollama-cloud.js +23 -42
  65. package/server/lib/quota/providers/ollama-cloud.test.js +12 -0
  66. package/server/lib/quota/providers/opencode-go.js +70 -0
  67. package/server/lib/quota/providers/opencode-go.test.js +17 -0
  68. package/server/lib/quota/routes.js +78 -7
  69. package/server/lib/quota/routes.test.js +40 -0
  70. package/server/lib/relay/DOCUMENTATION.md +20 -0
  71. package/server/lib/relay/host-lock.js +101 -0
  72. package/server/lib/relay/host-lock.test.js +129 -0
  73. package/server/lib/relay/identity.js +24 -4
  74. package/server/lib/relay/service.js +95 -8
  75. package/server/lib/relay/signing-key.js +30 -7
  76. package/server/lib/scheduled-tasks/runtime.js +104 -0
  77. package/server/lib/session-goal/DOCUMENTATION.md +176 -0
  78. package/server/lib/session-goal/objectives.js +61 -0
  79. package/server/lib/session-goal/routes.js +36 -0
  80. package/server/lib/session-goal/runtime.js +764 -0
  81. package/server/lib/small-model/DOCUMENTATION.md +3 -1
  82. package/server/lib/small-model/call.js +45 -8
  83. package/server/lib/small-model/call.test.js +338 -0
  84. package/server/lib/small-model/index.js +1 -0
  85. package/dist/assets/SessionSwitcherDropdown-DvoOalMY.js +0 -1
  86. package/dist/assets/es-cjUissX4.js +0 -15
  87. package/dist/assets/fr-DqbXnTdK.js +0 -13
  88. package/dist/assets/index-Dg0nmvQU.css +0 -1
  89. package/dist/assets/index-ltrmNGAf.js +0 -2
  90. package/dist/assets/ja-Cetyq6Eg.js +0 -15
  91. package/dist/assets/ko-DtunWisP.js +0 -15
  92. package/dist/assets/main-BC0m8s1v.js +0 -236
  93. package/dist/assets/miniChat-BRYDE-Se.js +0 -2
  94. package/dist/assets/mobile-C3QJVA97.js +0 -2
  95. package/dist/assets/pl-DctGLgve.js +0 -15
  96. package/dist/assets/pt-BR-CXPhf78S.js +0 -15
  97. package/dist/assets/renderElectronMiniChatApp-CVFbDC5A.js +0 -2
  98. package/dist/assets/renderMobileApp-wkBjh5WR.js +0 -16
  99. package/dist/assets/runtimeEndpointReset-C42u8PvG.js +0 -167
  100. package/dist/assets/uk-Bdrc-kqF.js +0 -15
  101. package/dist/assets/zh-CN-CeCVKrHP.js +0 -15
  102. package/dist/assets/zh-TW-CoqiQING.js +0 -15
@@ -1,8 +1,8 @@
1
- import{R as n,j as f,k as vt,cm as xt,cn as Tt,co as kt}from"./vendor-.bun-BE757pvp.js";import{c as bt,y as Ct,eE as Et,v as et,d as Rt,z as St,W as At,aF as It,G as ht,e as Bt,I as fe,B as oe}from"./useAppFontEffects-DzBg8hrC.js";import{u as q}from"./main-BC0m8s1v.js";import{h as Mt,u as Me,cb as Pt,ca as it,as as pt}from"./index-ltrmNGAf.js";import{y as Dt,z as Nt}from"./runtimeEndpointReset-C42u8PvG.js";import"./SessionSwitcherDropdown-DvoOalMY.js";import"./mobileLayoutPreference-Dio-WWkI.js";function jt(u){const{colors:l}=u,d=l.syntax.base;return{background:l.surface.background,foreground:d.foreground,cursor:l.interactive.cursor,cursorAccent:l.surface.background,selectionBackground:l.interactive.selection,selectionForeground:l.interactive.selectionForeground,selectionInactiveBackground:l.interactive.selection+"50",black:l.surface.muted,red:l.status.error,green:l.status.success,yellow:l.status.warning,blue:d.function,magenta:d.keyword,cyan:d.type,white:d.foreground,brightBlack:d.comment,brightRed:l.status.error,brightGreen:l.status.success,brightYellow:l.status.warning,brightBlue:d.function,brightMagenta:d.keyword,brightCyan:d.type,brightWhite:l.surface.elevatedForeground}}function Vt(u,l,d,I,E=!1){const _=`${u}, "JetBrainsMonoNL Nerd Font", "FiraCode Nerd Font", "Cascadia Code PL", "Fira Code", "JetBrains Mono", "SFMono-Regular", Menlo, Consolas, "Liberation Mono", "Courier New", monospace`;return{cursorBlink:!1,cursorStyle:"bar",fontSize:l,lineHeight:1.15,fontFamily:_,allowTransparency:!1,theme:{background:d.background,foreground:d.foreground,cursor:d.cursor,cursorAccent:d.cursorAccent,selectionBackground:d.selectionBackground,selectionForeground:d.selectionForeground,black:d.black,red:d.red,green:d.green,yellow:d.yellow,blue:d.blue,magenta:d.magenta,cyan:d.cyan,white:d.white,brightBlack:d.brightBlack,brightRed:d.brightRed,brightGreen:d.brightGreen,brightYellow:d.brightYellow,brightBlue:d.brightBlue,brightMagenta:d.brightMagenta,brightCyan:d.brightCyan,brightWhite:d.brightWhite},scrollback:1e4,ghostty:I,disableStdin:E}}let mt=null;function Lt(){return mt||(mt=kt.load()),mt}function Ft(u){if(typeof window>"u")return null;const l=[u,...Array.from(u.querySelectorAll("*"))];let d=null;for(const I of l){const X=window.getComputedStyle(I).overflowY;if(!(X!=="auto"&&X!=="scroll")){if(I.scrollHeight-I.clientHeight>2)return I;d||(d=I)}}return d}const gt=n.forwardRef(({sessionKey:u,chunks:l,onInput:d,onResize:I,theme:E,fontFamily:X,fontSize:_,className:ye,enableTouchScroll:R,autoFocus:at=!0,isVisible:Ne=!0},Se)=>{const{t:J}=bt(),O=n.useRef(null),je=n.useRef(null),B=n.useRef(null),tt=n.useRef(null),K=n.useRef(d),Ue=n.useRef(I),v=n.useRef(null),Ae=n.useRef(""),se=n.useRef(null),Ve=n.useRef(!1),Le=n.useRef(null),be=n.useRef(!0),ie=n.useRef(null),Q=n.useRef(null),Ie=n.useRef(0),ae=n.useRef(null),We=n.useRef(null),de=n.useRef(!1),ee=n.useRef(!1),ge=n.useRef(null),V=n.useRef(null),D=n.useRef(null),he=n.useRef(""),Fe=n.useRef(null),L=n.useRef(!R),nt=n.useRef(Ne),[,dt]=n.useReducer(e=>e+1,0),[rt,ct]=n.useReducer(e=>e+1,0);K.current=d,Ue.current=I;const lt=typeof navigator<"u"&&(/Android/i.test(navigator.userAgent)||navigator.userAgentData?.platform==="Android"),b=!!R;n.useEffect(()=>{if(!R){L.current=!0,nt.current=Ne;return}!nt.current&&Ne&&(L.current=!1),nt.current=Ne},[R,Ne]);const ot=n.useCallback(()=>{if(!b)return;const e=O.current,c=ae.current;if(!e)return;const a=[];e.getAttribute("contenteditable")==="true"&&a.push(e),a.push(...Array.from(e.querySelectorAll('[contenteditable="true"]'))),a.forEach(s=>{s.setAttribute("data-terminal-disabled-contenteditable","true"),s.setAttribute("contenteditable","false"),s.setAttribute("aria-hidden","true"),s.tabIndex=-1,s.style.setProperty("caret-color","transparent"),s.style.color="transparent",s.style.setProperty("-webkit-text-fill-color","transparent"),s.style.background="transparent",s.style.outline="none",s.style.boxShadow="none",s.style.textShadow="none",s.style.setProperty("user-select","none"),s.style.setProperty("-webkit-user-select","none")}),e.tabIndex=-1,e.removeAttribute("role"),e.removeAttribute("aria-multiline"),Array.from(e.querySelectorAll("textarea")).forEach(s=>{s.style.setProperty("caret-color","transparent"),s.style.color="transparent",s.style.setProperty("-webkit-text-fill-color","transparent"),s.style.background="transparent",s.style.border="0",s.style.outline="none",s.style.boxShadow="none",s.style.textShadow="none",s.style.fontSize="0",s.style.lineHeight="0",s!==c&&s.getAttribute("data-terminal-disabled-input")!=="true"&&(s.setAttribute("data-terminal-disabled-input","true"),s.setAttribute("aria-hidden","true"),s.tabIndex=-1,s.disabled=!0,s.style.position="absolute",s.style.opacity="0",s.style.width="0px",s.style.height="0px",s.style.pointerEvents="none",s.style.zIndex="-1")})},[b]),A=n.useCallback(e=>{if(Fe.current===e)return;const c=B.current;if(c)try{if(typeof c.setOption=="function"){c.setOption("cursorBlink",e),Fe.current=e;return}c.options&&(c.options.cursorBlink=e,Fe.current=e)}catch{}},[]),pe=b&&lt,S=n.useCallback((e,c)=>{const a=pe?We.current:ae.current,w=O.current;if(!a||!w)return;const s=w.getBoundingClientRect(),T=typeof window<"u"?window.innerWidth:s.width,h=typeof window<"u"?window.innerHeight:s.height,te=s.left+s.width/2,M=s.top+s.height-12,o=typeof e=="number"?e:te,P=typeof c=="number"?c:M,Y=8,me=Math.max(Y,Math.min(T-Y,o)),$=Math.max(Y,Math.min(h-Y,P));a.style.left=`${me}px`,a.style.top=`${$}px`,a.style.bottom="",(a instanceof HTMLTextAreaElement||a instanceof HTMLInputElement)&&(a.disabled=!1,a.readOnly=!1,a.removeAttribute("disabled"),a.removeAttribute("readonly"));try{a.focus({preventScroll:!0})}catch{try{a.focus()}catch{}}},[pe]),F=n.useCallback(()=>{if(b){S(),A(!0);return}B.current?.focus(),A(!0)},[S,A,b]),N=n.useCallback(e=>e instanceof HTMLTextAreaElement||e instanceof HTMLInputElement?e.value:e.textContent??"",[]),U=n.useCallback(e=>{if(e instanceof HTMLTextAreaElement||e instanceof HTMLInputElement){e.value="";return}e.textContent=""},[]),Ye=n.useCallback(e=>{if(typeof window>"u"||pe)return;D.current!==null&&(window.clearTimeout(D.current),D.current=null);let c=0;const a=3,w=()=>{D.current=window.setTimeout(()=>{D.current=null;const s=N(e);if(!s){if(c+=1,c<a){w();return}return}const T=he.current;he.current=s;const h=s.startsWith(T)?s.slice(T.length):s;h&&K.current(h.replace(/\r\n|\r|\n/g,"\r")),U(e),he.current=""},c===0?0:24)};w()},[U,N,pe]);n.useEffect(()=>{const e=O.current;if(!b||!e||R)return;const c=a=>{const w=a.target;if(w&&w.getAttribute("data-terminal-hidden-input")!=="true"&&e.contains(w)){try{w.blur()}catch{}S()}};return e.addEventListener("focusin",c,!0),()=>{e.removeEventListener("focusin",c,!0)}},[R,b,S]);const qe=n.useCallback(()=>{const e=B.current;if(!e||typeof e.getSelection!="function")return"";const c=e.getSelection();return typeof c=="string"?c:""},[]),Xe=n.useCallback(()=>{if(typeof window>"u")return"";const e=window.getSelection();if(!e)return"";const c=e.toString();if(!c.trim())return"";const a=O.current;if(!a)return"";const w=e.anchorNode,s=e.focusNode;return w&&!a.contains(w)||s&&!a.contains(s)?"":c},[]),ve=n.useCallback(async()=>{if(typeof document>"u")return;const e=qe()||Xe();e.trim()&&await Ct(e)},[Xe,qe]),Z=n.useCallback(()=>qe().trim()?!0:!!Xe().trim(),[Xe,qe]);n.useEffect(()=>{if(typeof window>"u")return;const e=c=>{Z()&&(c.preventDefault(),ve())};return window.addEventListener("openchamber:copy",e),()=>{window.removeEventListener("openchamber:copy",e)}},[ve,Z]);const ce=n.useCallback(()=>{Ae.current="",se.current!==null&&typeof window<"u"&&window.cancelAnimationFrame(se.current),se.current=null,Ve.current=!1,Le.current=null},[]),z=n.useCallback(()=>{const e=tt.current,c=B.current,a=O.current;if(!e||!c||!a)return;const w=a.getBoundingClientRect();if(!(w.width<24||w.height<24))try{e.fit();const s={cols:c.cols,rows:c.rows},T=v.current;(!T||T.cols!==s.cols||T.rows!==s.rows)&&(v.current=s,Ue.current(s.cols,s.rows))}catch{}},[]),xe=n.useCallback(()=>{if(Ve.current)return;const e=B.current;if(!e){ce();return}if(!Ae.current)return;const c=Ae.current;Ae.current="",Ve.current=!0,e.write(c,()=>{Ve.current=!1,Ae.current&&(typeof window<"u"?se.current=window.requestAnimationFrame(()=>{se.current=null,xe()}):xe())})},[ce]),Pe=n.useCallback(()=>{se.current===null&&(typeof window<"u"?se.current=window.requestAnimationFrame(()=>{se.current=null,xe()}):xe())},[xe]),$e=n.useCallback(e=>{e&&(Ae.current+=e,Pe())},[Pe]),De=n.useCallback(()=>{if(ie.current?.(),ie.current=null,Q.current!==null&&typeof window<"u"&&(window.clearTimeout(Q.current),Q.current=null),!R){Ie.current=0;return}const e=O.current;if(!e)return;const c=B.current;if(!c)return;Ie.current=0;const a=2.2,w=2.8,s=25,T=.25,h=8,te=.05,M=.015,o={lastY:null,lastTime:null,velocity:0,rafId:null,startX:null,startY:null,didMove:!1},P=()=>typeof performance<"u"?performance.now():Date.now(),Y=Math.max(12,Math.round(_*1.35));let me=0;const $=i=>{if(!i)return!1;const W=c.getViewportY(),re=me+i,le=Math.trunc(re/Y);return me=re-le*Y,le!==0&&c.scrollLines(le),c.getViewportY()!==W},ke=()=>{o.rafId!==null&&typeof window<"u"&&window.cancelAnimationFrame(o.rafId),o.rafId=null},k={passive:!1,capture:!1};if(typeof window<"u"&&"PointerEvent"in window){const i=Object.assign(o,{pointerId:null,startX:null,startY:null,moved:!1}),W=6,re=r=>{if(r.pointerType==="touch"){ke(),i.pointerId=r.pointerId,i.startX=r.clientX,i.startY=r.clientY,i.moved=!1,i.lastY=r.clientY,i.lastTime=P(),i.velocity=0;try{e.setPointerCapture(r.pointerId)}catch{}}},le=r=>{if(r.pointerType!=="touch"||i.pointerId!==r.pointerId)return;if(i.startX!==null&&i.startY!==null&&!i.moved){const ut=r.clientX-i.startX,He=r.clientY-i.startY;Math.hypot(ut,He)>=W&&(i.moved=!0)}if(i.lastY===null){i.lastY=r.clientY,i.lastTime=P();return}const g=i.lastY,m=i.lastTime??P(),x=P();i.lastY=r.clientY,i.lastTime=x;const y=g-r.clientY;if(Math.abs(y)<1)return;const G=Math.max(x-m,8),Ee=a+Math.min(w,Math.abs(y)/s),we=y*Ee,Re=we/G;i.velocity=i.velocity*(1-T)+Re*T,i.velocity>h?i.velocity=h:i.velocity<-h&&(i.velocity=-h),i.moved&&(r.cancelable&&r.preventDefault(),r.stopPropagation()),$(we)},ue=r=>{if(r.pointerType!=="touch"||i.pointerId!==r.pointerId)return;const g=!i.moved;i.pointerId=null,i.startX=null,i.startY=null,i.moved=!1,i.lastY=null,i.lastTime=null;try{e.releasePointerCapture(r.pointerId)}catch{}if(g){b?S(r.clientX,r.clientY):R&&!L.current&&(L.current=!0,B.current?.focus());return}if(typeof window>"u")return;if(Math.abs(i.velocity)<te){i.velocity=0;return}let m=P();const x=()=>{const y=P(),G=Math.max(y-m,8);m=y;const Ee=$(i.velocity*G)??!1,we=Math.sign(i.velocity),Re=Math.max(0,Math.abs(i.velocity)-M*G);if(i.velocity=Re*we,!Ee||Re<=te){ke(),i.velocity=0;return}i.rafId=window.requestAnimationFrame(x)};i.rafId=window.requestAnimationFrame(x)};e.addEventListener("pointerdown",re,k),e.addEventListener("pointermove",le,k),e.addEventListener("pointerup",ue,k),e.addEventListener("pointercancel",ue,k);const t=e.style.touchAction,p=e.style.overscrollBehaviorY;e.style.touchAction="none",e.style.overscrollBehaviorY="contain",ie.current=()=>{ke(),Q.current!==null&&typeof window<"u"&&(window.clearTimeout(Q.current),Q.current=null),Ie.current=0,e.removeEventListener("pointerdown",re,k),e.removeEventListener("pointermove",le,k),e.removeEventListener("pointerup",ue,k),e.removeEventListener("pointercancel",ue,k),e.style.touchAction=t,e.style.overscrollBehaviorY=p};return}const C=6,Ce=i=>{i.touches.length===1&&(ke(),o.lastY=i.touches[0].clientY,o.lastTime=P(),o.velocity=0,o.startX=i.touches[0].clientX,o.startY=i.touches[0].clientY,o.didMove=!1)},Be=i=>{if(i.touches.length!==1){o.lastY=null,o.lastTime=null,o.velocity=0,o.startX=null,o.startY=null,o.didMove=!1,ke();return}const W=i.touches[0].clientX,re=i.touches[0].clientY;if(o.startX!==null&&o.startY!==null&&!o.didMove){const y=W-o.startX,G=re-o.startY;Math.hypot(y,G)>=C&&(o.didMove=!0)}if(o.lastY===null){o.lastY=re,o.lastTime=P();return}const le=o.lastY,ue=o.lastTime??P(),t=P();o.lastY=re,o.lastTime=t;const p=le-re;if(Math.abs(p)<1)return;const r=Math.max(t-ue,8),g=a+Math.min(w,Math.abs(p)/s),m=p*g,x=m/r;o.velocity=o.velocity*(1-T)+x*T,o.velocity>h?o.velocity=h:o.velocity<-h&&(o.velocity=-h),o.didMove&&(i.preventDefault(),i.stopPropagation()),$(m)},H=i=>{const W=!o.didMove;o.lastY=null,o.lastTime=null;const re=o.velocity;if(o.startX=null,o.startY=null,o.didMove=!1,W){const t=i.changedTouches?.[0];b?S(t?.clientX,t?.clientY):R&&!L.current&&(L.current=!0,B.current?.focus());return}if(typeof window>"u")return;if(Math.abs(re)<te){o.velocity=0;return}let le=P();const ue=()=>{const t=P(),p=Math.max(t-le,8);le=t;const r=$(o.velocity*p)??!1,g=Math.sign(o.velocity),m=Math.max(0,Math.abs(o.velocity)-M*p);if(o.velocity=m*g,!r||m<=te){ke(),o.velocity=0;return}o.rafId=window.requestAnimationFrame(ue)};o.rafId=window.requestAnimationFrame(ue)};e.addEventListener("touchstart",Ce,k),e.addEventListener("touchmove",Be,k),e.addEventListener("touchend",H,k),e.addEventListener("touchcancel",H,k);const Qe=e.style.touchAction,j=e.style.overscrollBehaviorY;e.style.touchAction="none",e.style.overscrollBehaviorY="contain",ie.current=()=>{ke(),Q.current!==null&&typeof window<"u"&&(window.clearTimeout(Q.current),Q.current=null),Ie.current=0,e.removeEventListener("touchstart",Ce,k),e.removeEventListener("touchmove",Be,k),e.removeEventListener("touchend",H,k),e.removeEventListener("touchcancel",H,k),e.style.touchAction=Qe,e.style.overscrollBehaviorY=j}},[R,b,S,_]);n.useEffect(()=>{let e=!1,c=null,a=null,w=null,s=[],T=null,h=null,te=null;const M=O.current;if(!M)return;if(M.tabIndex=b?-1:0,b){const k=M.focus.bind(M),ne=M;ne.focus=((...C)=>{S()}),te=()=>{ne.focus=k}}const o=()=>{A(!0)},P=()=>{A(!1)},Y=k=>{const ne=k.target;if(ne&&M.contains(ne)){if(b&&ne instanceof HTMLElement){window.setTimeout(()=>{ne.blur(),S()},0),A(!1);return}if(R&&!L.current&&ne instanceof HTMLElement){window.setTimeout(()=>{ne.blur()},0),A(!1);return}A(!0);return}A(!1)},me=()=>{A(!1)};let $=null;return(async()=>{try{const k=await Lt();if(e)return;const ne=Vt(X,_,E,k,!1),C=new xt(ne);be.current=!0,b&&(C.focus=()=>{});const Ce=C;if(typeof Ce.scrollToBottom=="function"){const j=Ce.scrollToBottom.bind(Ce);Ce.scrollToBottom=()=>{be.current&&j()},T=()=>{Ce.scrollToBottom=j}}const Be=new Tt;if(c=C,B.current=C,tt.current=Be,C.loadAddon(Be),C.open(M),R){const j=C.renderer;if(j&&typeof j.renderScrollbar=="function"){const i=j.renderScrollbar.bind(j);j.renderScrollbar=()=>{},h=()=>{j.renderScrollbar=i}}}ct(),Fe.current=!1,$=C.textarea??M.querySelector("textarea"),$&&($.addEventListener("focus",o),$.addEventListener("blur",P)),ot(),typeof MutationObserver<"u"&&(w=new MutationObserver(()=>{ot()}),w.observe(M,{childList:!0,subtree:!0,attributes:!0,attributeFilter:["contenteditable","role","tabindex","aria-label"]}));const H=Ft(M);H?(H.classList.add("overlay-scrollbar-target","overlay-scrollbar-container"),je.current=H,dt()):je.current=null,z();const Qe=Be;typeof Qe.observeResize=="function"&&Qe.observeResize(),De(),s=[C.onData(j=>{K.current(j)}),C.onScroll(j=>{if(typeof j=="number"&&Number.isFinite(j)){const i=typeof C.hasSelection=="function"&&C.hasSelection();be.current=!i&&j<=.5}}),C.onSelectionChange(()=>{if(typeof C.hasSelection=="function"&&C.hasSelection()){be.current=!1;return}const i=typeof C.getViewportY=="function"?C.getViewportY():0;be.current=i<=.5})],a=new ResizeObserver(()=>{z()}),a.observe(M),typeof window<"u"&&window.setTimeout(()=>{z()},0)}catch{}})(),document.addEventListener("focusin",Y,!0),window.addEventListener("blur",me),()=>{e=!0,ie.current?.(),ie.current=null,document.removeEventListener("focusin",Y,!0),window.removeEventListener("blur",me),s.forEach(k=>k.dispose()),T?.(),T=null,h?.(),h=null,$&&($.removeEventListener("focus",o),$.removeEventListener("blur",P)),a?.disconnect(),w?.disconnect(),te?.(),c?.dispose(),B.current=null,tt.current=null,je.current=null,v.current=null,Fe.current=null,ce()}},[ot,R,z,S,X,_,De,E,ce,A,b]),n.useEffect(()=>{const e=B.current;e&&(e.reset(),ce(),v.current=null,z())},[u,rt,z,ce]),n.useEffect(()=>{!at||!B.current||F()},[at,F,u,rt]),n.useEffect(()=>(De(),()=>{ie.current?.(),ie.current=null}),[De,u]),n.useEffect(()=>{const e=B.current;if(!e)return;if(l.length===0){Le.current!==null&&(e.reset(),ce(),z());return}const c=Le.current;let a;if(c===null)a=l;else{const w=l.findIndex(s=>s.id===c);a=w>=0?l.slice(w+1):l}a.length>0&&$e(a.map(w=>w.data).join("")),Le.current=l[l.length-1].id},[l,rt,$e,z,ce]),n.useImperativeHandle(Se,()=>({focus:()=>{F()},clear:()=>{const e=B.current;e&&(e.reset(),ce(),z())},fit:()=>{z()}}),[F,z,ce]);const Ge=n.useCallback(e=>{if(!b)return;const c=O.current,a=e.relatedTarget,w=a?.tagName,s=w==="INPUT"||w==="TEXTAREA"||a?.isContentEditable,T=a?.getAttribute("data-terminal-hidden-input")==="true",h=!!(a&&c?.contains(a))},[b]),Oe=n.useCallback(e=>{const c=e.nativeEvent,a=c?.inputType??"",w=typeof c?.data=="string"?c.data:"";if(V.current=typeof performance<"u"?performance.now():Date.now(),a==="insertCompositionText"){de.current=!0;return}if(!a&&w){if(de.current)return;e.preventDefault(),K.current(w),ge.current={type:"insertText",at:typeof performance<"u"?performance.now():Date.now()},ee.current=!0;return}if(a==="insertText"&&w){if(de.current)return;e.preventDefault(),K.current(w),ge.current={type:a,at:typeof performance<"u"?performance.now():Date.now()},ee.current=!0;return}if(a==="insertLineBreak"){if(de.current)return;e.preventDefault(),K.current("\r"),ge.current={type:a,at:typeof performance<"u"?performance.now():Date.now()},ee.current=!0;return}if(a==="deleteContentBackward"){if(de.current)return;e.preventDefault(),K.current(""),ge.current={type:a,at:typeof performance<"u"?performance.now():Date.now()},ee.current=!0}},[]),st=n.useCallback(e=>{const c=e.currentTarget;if(V.current=typeof performance<"u"?performance.now():Date.now(),de.current)return;if(ee.current){const s=ge.current,T=typeof performance<"u"?performance.now():Date.now();if(s&&T-s.at<50){ee.current=!1,U(c);return}ee.current=!1}const a=N(c);if(!a)return;he.current=a;const w=a.replace(/\r\n|\r|\n/g,"\r");K.current(w),U(c),he.current=""},[U,N]),_e=n.useCallback(e=>{e.stopPropagation();const c=e.key.toLowerCase(),a=e.metaKey&&!e.ctrlKey&&!e.altKey&&c==="c",w=e.ctrlKey&&e.shiftKey&&!e.metaKey&&!e.altKey&&c==="c";if((a||w)&&Z()){e.preventDefault(),ve();return}if(e.ctrlKey&&!e.metaKey&&!e.altKey&&e.key.length===1){const Y=e.key.toUpperCase();if(Y>="A"&&Y<="Z"){e.preventDefault(),e.nativeEvent?.stopImmediatePropagation(),K.current(String.fromCharCode(Y.charCodeAt(0)-64)),U(e.currentTarget);return}}if(e.altKey&&!e.ctrlKey&&!e.metaKey&&e.key.length===1){e.preventDefault(),e.nativeEvent?.stopImmediatePropagation(),K.current("\x1B"+e.key),U(e.currentTarget);return}const T={ArrowUp:"\x1B[A",ArrowDown:"\x1B[B",ArrowRight:"\x1B[C",ArrowLeft:"\x1B[D",Escape:"\x1B",Tab:" ",Delete:"\x1B[3~",Home:"\x1B[H",End:"\x1B[F"}[e.key];if(T){e.preventDefault(),K.current(T),U(e.currentTarget);return}const h=e.currentTarget;if(e.nativeEvent?.isComposing)return;const M=ge.current,o=typeof performance<"u"?performance.now():Date.now(),P=!!(M&&o-M.at<50);if(e.key==="Enter"){if(P&&M?.type==="insertLineBreak")return;e.preventDefault(),K.current("\r"),U(h);return}if(e.key==="Backspace"){if(e.preventDefault(),P&&M?.type==="deleteContentBackward")return;N(h)||K.current("")}if(e.key.length===1&&!e.ctrlKey&&!e.metaKey&&!e.altKey){const Y=V.current;Y&&o-Y<50||(e.preventDefault(),K.current(e.key),ee.current=!0,ge.current={type:"keydown-text",at:o})}Ye(h)},[U,ve,Z,N,Ye]),Ke=n.useCallback(e=>{e.stopPropagation();const c=e.currentTarget;e.nativeEvent?.isComposing||Ye(c)},[Ye]),Je=n.useCallback(e=>{const c=e.currentTarget;de.current=!1;const a=e.data||N(c);if(V.current=typeof performance<"u"?performance.now():Date.now(),!a)return;const w=a.replace(/\r\n|\r|\n/g,"\r");K.current(w),U(c),ge.current={type:"compositionend",at:typeof performance<"u"?performance.now():Date.now()},ee.current=!0},[U,N]),ze=n.useCallback(e=>{e.stopPropagation();const c=e.clipboardData?.getData("text")??"";if(!c)return;e.preventDefault();const w=B.current?.hasBracketedPaste?.()?`\x1B[200~${c}\x1B[201~`:c;K.current(w)},[]),Te={position:"fixed",left:0,top:0,width:1,height:1,opacity:0,zIndex:-1,background:"transparent",color:"transparent",WebkitTextFillColor:"transparent",caretColor:"transparent",textShadow:"none",WebkitAppearance:"none",appearance:"none",resize:"none",overflow:"hidden",whiteSpace:"nowrap",border:"0",padding:0,margin:0,outline:"none",outlineOffset:0,fontSize:16,fontWeight:400,pointerEvents:"none",WebkitUserSelect:"none",userSelect:"none"};return f.jsxs("div",{ref:O,className:et("relative h-full w-full terminal-viewport-container",ye),"data-hidden-input-overlay-active":b?"true":void 0,style:{backgroundColor:E.background},onTouchStart:e=>{if(!(!b||R)&&!Z()){const c=e.touches?.[0];S(c?.clientX,c?.clientY)}},onClick:e=>{if(!R)if(b){if(Z())return;S(e.clientX,e.clientY)}else B.current?.focus()},onMouseUp:()=>{!R&&Z()&&ve()},onTouchEnd:()=>{!R&&Z()&&ve()},children:[b&&typeof document<"u"?vt.createPortal(f.jsxs(f.Fragment,{children:[f.jsx("input",{ref:We,type:"text",inputMode:"text",autoCapitalize:"off",autoComplete:"off",autoCorrect:"off",spellCheck:!1,tabIndex:-1,enterKeyHint:"send","data-terminal-hidden-input":"true","aria-label":J("terminalView.viewport.inputAria"),"aria-hidden":"true",style:{...Te,display:pe?"block":"none"},onBlur:Ge,onBeforeInput:Oe,onCompositionStart:()=>{de.current=!0},onInput:st,onKeyDown:_e,onKeyUp:Ke,onCompositionEnd:Je,onPaste:ze}),f.jsx("textarea",{ref:ae,inputMode:"text",autoCapitalize:"off",autoComplete:"off",autoCorrect:"off",spellCheck:!1,tabIndex:-1,enterKeyHint:"send","data-terminal-hidden-input":"true","aria-label":J("terminalView.viewport.inputAria"),"aria-hidden":"true",style:{...Te,display:pe?"none":"block"},onBlur:Ge,onBeforeInput:Oe,onCompositionStart:()=>{de.current=!0},onInput:st,onKeyDown:_e,onKeyUp:Ke,onCompositionEnd:Je,onPaste:ze})]}),document.body):null,je.current&&!R?f.jsx(Et,{containerRef:je,disableHorizontal:!0,className:"overlay-scrollbar--flush overlay-scrollbar--dense overlay-scrollbar--zero"}):null]})});gt.displayName="TerminalViewport";const Yt="\x1B",Ot=new RegExp(`${Yt}\\[[0-9;?]*[ -/]*[@-~]`,"g"),Kt=/(https?:\/\/(?:localhost|127\.0\.0\.1|0\.0\.0\.0|\[(?:::1|::)\])(?::\d{2,5})?(?:\/[^\s<>'"`]*)?)/gi,zt=/(?:➜\s*(?:Local|Network):)|\b(?:local|network|loopback|serving|listening|available|ready|started|running|server|vite|webpack|next\.js|astro|sveltekit|nuxt)\b/i,Ht=/Serving HTTP on .*? port (\d{2,5})/i,Ut=new Set([".",",",";",":","!","?"]),Wt=u=>{let l=u;for(;l.length>0;){const d=l[l.length-1];if(d===")"||d==="]"||d==="}"||d===">"){const I=d===")"?"(":d==="]"?"[":d==="}"?"{":"<",E=l.slice(0,-1),X=(E.match(new RegExp(`\\${I}`,"g"))||[]).length,_=(E.match(new RegExp(`\\${d}`,"g"))||[]).length;if(X>_)break;l=E;continue}if(Ut.has(d)){l=l.slice(0,-1);continue}break}return l},yt=u=>{let l=Wt(u);return l=l.replace("0.0.0.0","127.0.0.1"),l=l.replace("[::1]","127.0.0.1"),l=l.replace("[::]","127.0.0.1"),l},qt=u=>{if(!u)return null;const l=u.replace(Ot,""),d=l.match(Ht);if(d?.[1]){const E=Number.parseInt(d[1],10);if(Number.isFinite(E)&&E>0&&E<=65535)return`http://127.0.0.1:${E}/`}const I=l.split(`
2
- `);for(const E of I){if(!zt.test(E))continue;const X=Array.from(E.matchAll(Kt));if(X.length===0)continue;const _=X.find(ye=>{try{return!!new URL(yt(ye[1])).port}catch{return!1}});return yt((_??X[0])[1])}return null},Xt=async(u,l=1500)=>{if(!u||typeof window>"u")return!1;let d;try{d=new URL(u)}catch{return!1}if(d.protocol!=="http:"&&d.protocol!=="https:")return!1;const I=d.hostname.toLowerCase();if(I!=="localhost"&&I!=="127.0.0.1"&&I!=="0.0.0.0"&&I!=="::1"&&I!=="::")return!1;const E=new AbortController,X=window.setTimeout(()=>E.abort(),l);try{const _=await Mt("/api/system/probe-url",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({url:d.toString()}),cache:"no-store",signal:E.signal});return _.ok?(await _.json().catch(()=>null))?.ok===!0:!1}catch{return!1}finally{window.clearTimeout(X)}},$t={esc:"\x1B",tab:" ",enter:"\r","arrow-up":"\x1B[A","arrow-down":"\x1B[B","arrow-left":"\x1B[D","arrow-right":"\x1B[C"},ft={ctrl:"5",cmd:"3"},wt={retry:{maxRetries:3,initialDelayMs:500,maxDelayMs:8e3},connectionTimeoutMs:1e4},Gt={retry:{...wt.retry,initialDelayMs:200,maxDelayMs:500},connectionTimeoutMs:1500},_t=(u,l)=>{if(l)switch(u){case"arrow-up":return`\x1B[1;${ft[l]}A`;case"arrow-down":return`\x1B[1;${ft[l]}B`;case"arrow-right":return`\x1B[1;${ft[l]}C`;case"arrow-left":return`\x1B[1;${ft[l]}D`}return $t[u]??null},sn=()=>{const{t:u}=bt(),{terminal:l,runtime:d}=Rt(),{currentTheme:I}=St(),{monoFont:E}=At(),X=Me(t=>t.terminalFontSize),_=Me(t=>t.bottomTerminalHeight),ye=Me(t=>t.isBottomTerminalExpanded),{isMobile:R,isTablet:at,hasTouchOnlyPointer:Ne}=It(),Se=R||at,J=(Se||Ne)&&d.platform==="web",O=d.platform!=="vscode",je=Me(t=>t.showTerminalQuickKeysOnDesktop),B=Se||je,tt=ht(t=>t.currentSessionId),K=ht(t=>t.newSessionDraft),Ue=tt!==null||K?.open===!0,v=Bt()??null,Ae=q(t=>t.sessions),se=q(t=>t.hasHydrated),Ve=q(t=>t.ensureDirectory),Le=q(t=>t.createTab),be=q(t=>t.setActiveTab),ie=q(t=>t.closeTab),Q=q(t=>t.setTabSessionId),Ie=q(t=>t.setTabLifecycle),ae=q(t=>t.setConnecting),We=q(t=>t.appendToBuffer),de=q(t=>t.setTabPreviewUrl),ee=q(t=>t.clearBuffer),ge=Me(t=>t.openContextPreview),V=n.useMemo(()=>{if(v)return Ae.get(v)},[Ae,v]),D=n.useMemo(()=>V?O?V.activeTabId??V.tabs[0]?.id??null:V.tabs[0]?.id??null:null,[V,O]),he=n.useMemo(()=>{if(V)return D?V.tabs.find(t=>t.id===D)??V.tabs[0]:V.tabs[0]},[V,D]),Fe=n.useMemo(()=>(V?.tabs??[]).map(t=>({icon:(()=>{const p=t.iconKey?Dt[t.iconKey]??"terminal":"terminal";return f.jsx(fe,{name:p,className:"h-4 w-4"})})(),id:t.id,label:t.label,title:t.label,closeLabel:u("terminalView.tabs.closeTabTitle")})),[V?.tabs,u]),L=he?.terminalSessionId??null,nt=he?.lifecycle??"idle",dt=he?.bufferChunks??[],rt=he?.isConnecting??!1,ct=he?.previewUrl??null,[lt,b]=n.useState(null),[ot,A]=n.useState(!1),[pe,S]=n.useState(!1),[F,N]=n.useState(null),[U,Ye]=n.useState(!1),[qe,Xe]=n.useState(0),ve=n.useRef(null),Z=n.useRef(null),ce=n.useRef(D),z=n.useRef(L),xe=n.useRef(v),Pe=n.useRef(null),$e=n.useRef(null),De=n.useRef(!1),Ge=n.useRef(null),Oe=n.useRef(new Set),st=n.useRef(!1),_e=n.useRef(""),Ke=n.useRef(new Set),Je=n.useRef(0),ze=n.useCallback(()=>{_e.current="",Ke.current.clear(),Je.current+=1},[]),Te=n.useCallback(()=>{J||typeof document<"u"&&!document.hasFocus()||Pe.current?.focus()},[J]),e=n.useCallback(()=>{J||Pe.current?.focus()},[J]);n.useEffect(()=>{if(!se||st.current)return;st.current=!0;const t=new Set;for(const[,p]of q.getState().sessions.entries())for(const r of p.tabs)r.terminalSessionId&&t.add(r.terminalSessionId);Oe.current=t},[se]);const c=Me(t=>t.activeMainTab),a=Me(t=>t.isBottomTerminalOpen),w=Me(t=>t.setBottomTerminalOpen),s=Me(t=>t.setBottomTerminalExpanded),T=c==="terminal",h=T||a,[te,M]=n.useState(h);n.useEffect(()=>{!h||d.platform==="vscode"||Pt()},[h,d.platform]),n.useEffect(()=>{h&&M(!0)},[h]),n.useEffect(()=>{De.current=h},[h]),n.useEffect(()=>{z.current=L},[L]),n.useEffect(()=>{ce.current=D,ze()},[D,ze]),n.useEffect(()=>{xe.current=v},[v]),n.useEffect(()=>{!B&&F!==null&&N(null)},[B,F,N]),n.useEffect(()=>{!L&&F!==null&&N(null)},[L,F,N]);const o=n.useCallback(()=>{ve.current?.(),ve.current=null,Z.current=null,S(!1)},[]);n.useEffect(()=>()=>{o(),z.current=null},[o]);const P=n.useCallback((t,p,r)=>{if(!r)return;const g=`${_e.current}${r}`.replace(/\r\n|\r/g,`
1
+ import{R as n,j as f,k as vt,cm as xt,cn as Tt,co as kt}from"./vendor-.bun-BE757pvp.js";import{c as bt,y as Ct,eO as Et,v as et,d as Rt,z as St,W as At,aH as It,G as ht,e as Bt,I as fe,B as oe}from"./useAppFontEffects-9LFMCKYW.js";import{u as q}from"./main-B0CxEOP5.js";import{h as Mt,u as Me,cf as Pt,ce as it,at as pt}from"./index-BklNmnVr.js";import{y as Dt,z as Nt}from"./runtimeEndpointReset-B0P2DED1.js";import"./SessionSwitcherDropdown-DHO5Je6v.js";import"./mobileLayoutPreference-Dio-WWkI.js";function jt(u){const{colors:l}=u,d=l.syntax.base;return{background:l.surface.background,foreground:d.foreground,cursor:l.interactive.cursor,cursorAccent:l.surface.background,selectionBackground:l.interactive.selection,selectionForeground:l.interactive.selectionForeground,selectionInactiveBackground:l.interactive.selection+"50",black:l.surface.muted,red:l.status.error,green:l.status.success,yellow:l.status.warning,blue:d.function,magenta:d.keyword,cyan:d.type,white:d.foreground,brightBlack:d.comment,brightRed:l.status.error,brightGreen:l.status.success,brightYellow:l.status.warning,brightBlue:d.function,brightMagenta:d.keyword,brightCyan:d.type,brightWhite:l.surface.elevatedForeground}}function Vt(u,l,d,I,E=!1){const _=`${u}, "JetBrainsMonoNL Nerd Font", "FiraCode Nerd Font", "Cascadia Code PL", "Fira Code", "JetBrains Mono", "SFMono-Regular", Menlo, Consolas, "Liberation Mono", "Courier New", monospace`;return{cursorBlink:!1,cursorStyle:"bar",fontSize:l,lineHeight:1.15,fontFamily:_,allowTransparency:!1,theme:{background:d.background,foreground:d.foreground,cursor:d.cursor,cursorAccent:d.cursorAccent,selectionBackground:d.selectionBackground,selectionForeground:d.selectionForeground,black:d.black,red:d.red,green:d.green,yellow:d.yellow,blue:d.blue,magenta:d.magenta,cyan:d.cyan,white:d.white,brightBlack:d.brightBlack,brightRed:d.brightRed,brightGreen:d.brightGreen,brightYellow:d.brightYellow,brightBlue:d.brightBlue,brightMagenta:d.brightMagenta,brightCyan:d.brightCyan,brightWhite:d.brightWhite},scrollback:1e4,ghostty:I,disableStdin:E}}let mt=null;function Lt(){return mt||(mt=kt.load()),mt}function Ft(u){if(typeof window>"u")return null;const l=[u,...Array.from(u.querySelectorAll("*"))];let d=null;for(const I of l){const X=window.getComputedStyle(I).overflowY;if(!(X!=="auto"&&X!=="scroll")){if(I.scrollHeight-I.clientHeight>2)return I;d||(d=I)}}return d}const gt=n.forwardRef(({sessionKey:u,chunks:l,onInput:d,onResize:I,theme:E,fontFamily:X,fontSize:_,className:ye,enableTouchScroll:R,autoFocus:at=!0,isVisible:Ne=!0},Se)=>{const{t:J}=bt(),O=n.useRef(null),je=n.useRef(null),B=n.useRef(null),tt=n.useRef(null),K=n.useRef(d),Ue=n.useRef(I),v=n.useRef(null),Ae=n.useRef(""),se=n.useRef(null),Ve=n.useRef(!1),Le=n.useRef(null),be=n.useRef(!0),ie=n.useRef(null),Q=n.useRef(null),Ie=n.useRef(0),ae=n.useRef(null),We=n.useRef(null),de=n.useRef(!1),ee=n.useRef(!1),ge=n.useRef(null),V=n.useRef(null),D=n.useRef(null),he=n.useRef(""),Fe=n.useRef(null),L=n.useRef(!R),nt=n.useRef(Ne),[,dt]=n.useReducer(e=>e+1,0),[rt,ct]=n.useReducer(e=>e+1,0);K.current=d,Ue.current=I;const lt=typeof navigator<"u"&&(/Android/i.test(navigator.userAgent)||navigator.userAgentData?.platform==="Android"),b=!!R;n.useEffect(()=>{if(!R){L.current=!0,nt.current=Ne;return}!nt.current&&Ne&&(L.current=!1),nt.current=Ne},[R,Ne]);const ot=n.useCallback(()=>{if(!b)return;const e=O.current,c=ae.current;if(!e)return;const a=[];e.getAttribute("contenteditable")==="true"&&a.push(e),a.push(...Array.from(e.querySelectorAll('[contenteditable="true"]'))),a.forEach(s=>{s.setAttribute("data-terminal-disabled-contenteditable","true"),s.setAttribute("contenteditable","false"),s.setAttribute("aria-hidden","true"),s.tabIndex=-1,s.style.setProperty("caret-color","transparent"),s.style.color="transparent",s.style.setProperty("-webkit-text-fill-color","transparent"),s.style.background="transparent",s.style.outline="none",s.style.boxShadow="none",s.style.textShadow="none",s.style.setProperty("user-select","none"),s.style.setProperty("-webkit-user-select","none")}),e.tabIndex=-1,e.removeAttribute("role"),e.removeAttribute("aria-multiline"),Array.from(e.querySelectorAll("textarea")).forEach(s=>{s.style.setProperty("caret-color","transparent"),s.style.color="transparent",s.style.setProperty("-webkit-text-fill-color","transparent"),s.style.background="transparent",s.style.border="0",s.style.outline="none",s.style.boxShadow="none",s.style.textShadow="none",s.style.fontSize="0",s.style.lineHeight="0",s!==c&&s.getAttribute("data-terminal-disabled-input")!=="true"&&(s.setAttribute("data-terminal-disabled-input","true"),s.setAttribute("aria-hidden","true"),s.tabIndex=-1,s.disabled=!0,s.style.position="absolute",s.style.opacity="0",s.style.width="0px",s.style.height="0px",s.style.pointerEvents="none",s.style.zIndex="-1")})},[b]),A=n.useCallback(e=>{if(Fe.current===e)return;const c=B.current;if(c)try{if(typeof c.setOption=="function"){c.setOption("cursorBlink",e),Fe.current=e;return}c.options&&(c.options.cursorBlink=e,Fe.current=e)}catch{}},[]),pe=b&&lt,S=n.useCallback((e,c)=>{const a=pe?We.current:ae.current,w=O.current;if(!a||!w)return;const s=w.getBoundingClientRect(),T=typeof window<"u"?window.innerWidth:s.width,h=typeof window<"u"?window.innerHeight:s.height,te=s.left+s.width/2,M=s.top+s.height-12,o=typeof e=="number"?e:te,P=typeof c=="number"?c:M,Y=8,me=Math.max(Y,Math.min(T-Y,o)),$=Math.max(Y,Math.min(h-Y,P));a.style.left=`${me}px`,a.style.top=`${$}px`,a.style.bottom="",(a instanceof HTMLTextAreaElement||a instanceof HTMLInputElement)&&(a.disabled=!1,a.readOnly=!1,a.removeAttribute("disabled"),a.removeAttribute("readonly"));try{a.focus({preventScroll:!0})}catch{try{a.focus()}catch{}}},[pe]),F=n.useCallback(()=>{if(b){S(),A(!0);return}B.current?.focus(),A(!0)},[S,A,b]),N=n.useCallback(e=>e instanceof HTMLTextAreaElement||e instanceof HTMLInputElement?e.value:e.textContent??"",[]),U=n.useCallback(e=>{if(e instanceof HTMLTextAreaElement||e instanceof HTMLInputElement){e.value="";return}e.textContent=""},[]),Ye=n.useCallback(e=>{if(typeof window>"u"||pe)return;D.current!==null&&(window.clearTimeout(D.current),D.current=null);let c=0;const a=3,w=()=>{D.current=window.setTimeout(()=>{D.current=null;const s=N(e);if(!s){if(c+=1,c<a){w();return}return}const T=he.current;he.current=s;const h=s.startsWith(T)?s.slice(T.length):s;h&&K.current(h.replace(/\r\n|\r|\n/g,"\r")),U(e),he.current=""},c===0?0:24)};w()},[U,N,pe]);n.useEffect(()=>{const e=O.current;if(!b||!e||R)return;const c=a=>{const w=a.target;if(w&&w.getAttribute("data-terminal-hidden-input")!=="true"&&e.contains(w)){try{w.blur()}catch{}S()}};return e.addEventListener("focusin",c,!0),()=>{e.removeEventListener("focusin",c,!0)}},[R,b,S]);const qe=n.useCallback(()=>{const e=B.current;if(!e||typeof e.getSelection!="function")return"";const c=e.getSelection();return typeof c=="string"?c:""},[]),Xe=n.useCallback(()=>{if(typeof window>"u")return"";const e=window.getSelection();if(!e)return"";const c=e.toString();if(!c.trim())return"";const a=O.current;if(!a)return"";const w=e.anchorNode,s=e.focusNode;return w&&!a.contains(w)||s&&!a.contains(s)?"":c},[]),ve=n.useCallback(async()=>{if(typeof document>"u")return;const e=qe()||Xe();e.trim()&&await Ct(e)},[Xe,qe]),Z=n.useCallback(()=>qe().trim()?!0:!!Xe().trim(),[Xe,qe]);n.useEffect(()=>{if(typeof window>"u")return;const e=c=>{Z()&&(c.preventDefault(),ve())};return window.addEventListener("openchamber:copy",e),()=>{window.removeEventListener("openchamber:copy",e)}},[ve,Z]);const ce=n.useCallback(()=>{Ae.current="",se.current!==null&&typeof window<"u"&&window.cancelAnimationFrame(se.current),se.current=null,Ve.current=!1,Le.current=null},[]),H=n.useCallback(()=>{const e=tt.current,c=B.current,a=O.current;if(!e||!c||!a)return;const w=a.getBoundingClientRect();if(!(w.width<24||w.height<24))try{e.fit();const s={cols:c.cols,rows:c.rows},T=v.current;(!T||T.cols!==s.cols||T.rows!==s.rows)&&(v.current=s,Ue.current(s.cols,s.rows))}catch{}},[]),xe=n.useCallback(()=>{if(Ve.current)return;const e=B.current;if(!e){ce();return}if(!Ae.current)return;const c=Ae.current;Ae.current="",Ve.current=!0,e.write(c,()=>{Ve.current=!1,Ae.current&&(typeof window<"u"?se.current=window.requestAnimationFrame(()=>{se.current=null,xe()}):xe())})},[ce]),Pe=n.useCallback(()=>{se.current===null&&(typeof window<"u"?se.current=window.requestAnimationFrame(()=>{se.current=null,xe()}):xe())},[xe]),$e=n.useCallback(e=>{e&&(Ae.current+=e,Pe())},[Pe]),De=n.useCallback(()=>{if(ie.current?.(),ie.current=null,Q.current!==null&&typeof window<"u"&&(window.clearTimeout(Q.current),Q.current=null),!R){Ie.current=0;return}const e=O.current;if(!e)return;const c=B.current;if(!c)return;Ie.current=0;const a=2.2,w=2.8,s=25,T=.25,h=8,te=.05,M=.015,o={lastY:null,lastTime:null,velocity:0,rafId:null,startX:null,startY:null,didMove:!1},P=()=>typeof performance<"u"?performance.now():Date.now(),Y=Math.max(12,Math.round(_*1.35));let me=0;const $=i=>{if(!i)return!1;const W=c.getViewportY(),re=me+i,le=Math.trunc(re/Y);return me=re-le*Y,le!==0&&c.scrollLines(le),c.getViewportY()!==W},ke=()=>{o.rafId!==null&&typeof window<"u"&&window.cancelAnimationFrame(o.rafId),o.rafId=null},k={passive:!1,capture:!1};if(typeof window<"u"&&"PointerEvent"in window){const i=Object.assign(o,{pointerId:null,startX:null,startY:null,moved:!1}),W=6,re=r=>{if(r.pointerType==="touch"){ke(),i.pointerId=r.pointerId,i.startX=r.clientX,i.startY=r.clientY,i.moved=!1,i.lastY=r.clientY,i.lastTime=P(),i.velocity=0;try{e.setPointerCapture(r.pointerId)}catch{}}},le=r=>{if(r.pointerType!=="touch"||i.pointerId!==r.pointerId)return;if(i.startX!==null&&i.startY!==null&&!i.moved){const ut=r.clientX-i.startX,ze=r.clientY-i.startY;Math.hypot(ut,ze)>=W&&(i.moved=!0)}if(i.lastY===null){i.lastY=r.clientY,i.lastTime=P();return}const g=i.lastY,m=i.lastTime??P(),x=P();i.lastY=r.clientY,i.lastTime=x;const y=g-r.clientY;if(Math.abs(y)<1)return;const G=Math.max(x-m,8),Ee=a+Math.min(w,Math.abs(y)/s),we=y*Ee,Re=we/G;i.velocity=i.velocity*(1-T)+Re*T,i.velocity>h?i.velocity=h:i.velocity<-h&&(i.velocity=-h),i.moved&&(r.cancelable&&r.preventDefault(),r.stopPropagation()),$(we)},ue=r=>{if(r.pointerType!=="touch"||i.pointerId!==r.pointerId)return;const g=!i.moved;i.pointerId=null,i.startX=null,i.startY=null,i.moved=!1,i.lastY=null,i.lastTime=null;try{e.releasePointerCapture(r.pointerId)}catch{}if(g){b?S(r.clientX,r.clientY):R&&!L.current&&(L.current=!0,B.current?.focus());return}if(typeof window>"u")return;if(Math.abs(i.velocity)<te){i.velocity=0;return}let m=P();const x=()=>{const y=P(),G=Math.max(y-m,8);m=y;const Ee=$(i.velocity*G)??!1,we=Math.sign(i.velocity),Re=Math.max(0,Math.abs(i.velocity)-M*G);if(i.velocity=Re*we,!Ee||Re<=te){ke(),i.velocity=0;return}i.rafId=window.requestAnimationFrame(x)};i.rafId=window.requestAnimationFrame(x)};e.addEventListener("pointerdown",re,k),e.addEventListener("pointermove",le,k),e.addEventListener("pointerup",ue,k),e.addEventListener("pointercancel",ue,k);const t=e.style.touchAction,p=e.style.overscrollBehaviorY;e.style.touchAction="none",e.style.overscrollBehaviorY="contain",ie.current=()=>{ke(),Q.current!==null&&typeof window<"u"&&(window.clearTimeout(Q.current),Q.current=null),Ie.current=0,e.removeEventListener("pointerdown",re,k),e.removeEventListener("pointermove",le,k),e.removeEventListener("pointerup",ue,k),e.removeEventListener("pointercancel",ue,k),e.style.touchAction=t,e.style.overscrollBehaviorY=p};return}const C=6,Ce=i=>{i.touches.length===1&&(ke(),o.lastY=i.touches[0].clientY,o.lastTime=P(),o.velocity=0,o.startX=i.touches[0].clientX,o.startY=i.touches[0].clientY,o.didMove=!1)},Be=i=>{if(i.touches.length!==1){o.lastY=null,o.lastTime=null,o.velocity=0,o.startX=null,o.startY=null,o.didMove=!1,ke();return}const W=i.touches[0].clientX,re=i.touches[0].clientY;if(o.startX!==null&&o.startY!==null&&!o.didMove){const y=W-o.startX,G=re-o.startY;Math.hypot(y,G)>=C&&(o.didMove=!0)}if(o.lastY===null){o.lastY=re,o.lastTime=P();return}const le=o.lastY,ue=o.lastTime??P(),t=P();o.lastY=re,o.lastTime=t;const p=le-re;if(Math.abs(p)<1)return;const r=Math.max(t-ue,8),g=a+Math.min(w,Math.abs(p)/s),m=p*g,x=m/r;o.velocity=o.velocity*(1-T)+x*T,o.velocity>h?o.velocity=h:o.velocity<-h&&(o.velocity=-h),o.didMove&&(i.preventDefault(),i.stopPropagation()),$(m)},z=i=>{const W=!o.didMove;o.lastY=null,o.lastTime=null;const re=o.velocity;if(o.startX=null,o.startY=null,o.didMove=!1,W){const t=i.changedTouches?.[0];b?S(t?.clientX,t?.clientY):R&&!L.current&&(L.current=!0,B.current?.focus());return}if(typeof window>"u")return;if(Math.abs(re)<te){o.velocity=0;return}let le=P();const ue=()=>{const t=P(),p=Math.max(t-le,8);le=t;const r=$(o.velocity*p)??!1,g=Math.sign(o.velocity),m=Math.max(0,Math.abs(o.velocity)-M*p);if(o.velocity=m*g,!r||m<=te){ke(),o.velocity=0;return}o.rafId=window.requestAnimationFrame(ue)};o.rafId=window.requestAnimationFrame(ue)};e.addEventListener("touchstart",Ce,k),e.addEventListener("touchmove",Be,k),e.addEventListener("touchend",z,k),e.addEventListener("touchcancel",z,k);const Qe=e.style.touchAction,j=e.style.overscrollBehaviorY;e.style.touchAction="none",e.style.overscrollBehaviorY="contain",ie.current=()=>{ke(),Q.current!==null&&typeof window<"u"&&(window.clearTimeout(Q.current),Q.current=null),Ie.current=0,e.removeEventListener("touchstart",Ce,k),e.removeEventListener("touchmove",Be,k),e.removeEventListener("touchend",z,k),e.removeEventListener("touchcancel",z,k),e.style.touchAction=Qe,e.style.overscrollBehaviorY=j}},[R,b,S,_]);n.useEffect(()=>{let e=!1,c=null,a=null,w=null,s=[],T=null,h=null,te=null;const M=O.current;if(!M)return;if(M.tabIndex=b?-1:0,b){const k=M.focus.bind(M),ne=M;ne.focus=((...C)=>{S()}),te=()=>{ne.focus=k}}const o=()=>{A(!0)},P=()=>{A(!1)},Y=k=>{const ne=k.target;if(ne&&M.contains(ne)){if(b&&ne instanceof HTMLElement){window.setTimeout(()=>{ne.blur(),S()},0),A(!1);return}if(R&&!L.current&&ne instanceof HTMLElement){window.setTimeout(()=>{ne.blur()},0),A(!1);return}A(!0);return}A(!1)},me=()=>{A(!1)};let $=null;return(async()=>{try{const k=await Lt();if(e)return;const ne=Vt(X,_,E,k,!1),C=new xt(ne);be.current=!0,b&&(C.focus=()=>{});const Ce=C;if(typeof Ce.scrollToBottom=="function"){const j=Ce.scrollToBottom.bind(Ce);Ce.scrollToBottom=()=>{be.current&&j()},T=()=>{Ce.scrollToBottom=j}}const Be=new Tt;if(c=C,B.current=C,tt.current=Be,C.loadAddon(Be),C.open(M),R){const j=C.renderer;if(j&&typeof j.renderScrollbar=="function"){const i=j.renderScrollbar.bind(j);j.renderScrollbar=()=>{},h=()=>{j.renderScrollbar=i}}}ct(),Fe.current=!1,$=C.textarea??M.querySelector("textarea"),$&&($.addEventListener("focus",o),$.addEventListener("blur",P)),ot(),typeof MutationObserver<"u"&&(w=new MutationObserver(()=>{ot()}),w.observe(M,{childList:!0,subtree:!0,attributes:!0,attributeFilter:["contenteditable","role","tabindex","aria-label"]}));const z=Ft(M);z?(z.classList.add("overlay-scrollbar-target","overlay-scrollbar-container"),je.current=z,dt()):je.current=null,H();const Qe=Be;typeof Qe.observeResize=="function"&&Qe.observeResize(),De(),s=[C.onData(j=>{K.current(j)}),C.onScroll(j=>{if(typeof j=="number"&&Number.isFinite(j)){const i=typeof C.hasSelection=="function"&&C.hasSelection();be.current=!i&&j<=.5}}),C.onSelectionChange(()=>{if(typeof C.hasSelection=="function"&&C.hasSelection()){be.current=!1;return}const i=typeof C.getViewportY=="function"?C.getViewportY():0;be.current=i<=.5})],a=new ResizeObserver(()=>{H()}),a.observe(M),typeof window<"u"&&window.setTimeout(()=>{H()},0)}catch{}})(),document.addEventListener("focusin",Y,!0),window.addEventListener("blur",me),()=>{e=!0,ie.current?.(),ie.current=null,document.removeEventListener("focusin",Y,!0),window.removeEventListener("blur",me),s.forEach(k=>k.dispose()),T?.(),T=null,h?.(),h=null,$&&($.removeEventListener("focus",o),$.removeEventListener("blur",P)),a?.disconnect(),w?.disconnect(),te?.(),c?.dispose(),B.current=null,tt.current=null,je.current=null,v.current=null,Fe.current=null,ce()}},[ot,R,H,S,X,_,De,E,ce,A,b]),n.useEffect(()=>{const e=B.current;e&&(e.reset(),ce(),v.current=null,H())},[u,rt,H,ce]),n.useEffect(()=>{!at||!B.current||F()},[at,F,u,rt]),n.useEffect(()=>(De(),()=>{ie.current?.(),ie.current=null}),[De,u]),n.useEffect(()=>{const e=B.current;if(!e)return;if(l.length===0){Le.current!==null&&(e.reset(),ce(),H());return}const c=Le.current;let a;if(c===null)a=l;else{const w=l.findIndex(s=>s.id===c);a=w>=0?l.slice(w+1):l}a.length>0&&$e(a.map(w=>w.data).join("")),Le.current=l[l.length-1].id},[l,rt,$e,H,ce]),n.useImperativeHandle(Se,()=>({focus:()=>{F()},clear:()=>{const e=B.current;e&&(e.reset(),ce(),H())},fit:()=>{H()}}),[F,H,ce]);const Ge=n.useCallback(e=>{if(!b)return;const c=O.current,a=e.relatedTarget,w=a?.tagName,s=w==="INPUT"||w==="TEXTAREA"||a?.isContentEditable,T=a?.getAttribute("data-terminal-hidden-input")==="true",h=!!(a&&c?.contains(a))},[b]),Oe=n.useCallback(e=>{const c=e.nativeEvent,a=c?.inputType??"",w=typeof c?.data=="string"?c.data:"";if(V.current=typeof performance<"u"?performance.now():Date.now(),a==="insertCompositionText"){de.current=!0;return}if(!a&&w){if(de.current)return;e.preventDefault(),K.current(w),ge.current={type:"insertText",at:typeof performance<"u"?performance.now():Date.now()},ee.current=!0;return}if(a==="insertText"&&w){if(de.current)return;e.preventDefault(),K.current(w),ge.current={type:a,at:typeof performance<"u"?performance.now():Date.now()},ee.current=!0;return}if(a==="insertLineBreak"){if(de.current)return;e.preventDefault(),K.current("\r"),ge.current={type:a,at:typeof performance<"u"?performance.now():Date.now()},ee.current=!0;return}if(a==="deleteContentBackward"){if(de.current)return;e.preventDefault(),K.current(""),ge.current={type:a,at:typeof performance<"u"?performance.now():Date.now()},ee.current=!0}},[]),st=n.useCallback(e=>{const c=e.currentTarget;if(V.current=typeof performance<"u"?performance.now():Date.now(),de.current)return;if(ee.current){const s=ge.current,T=typeof performance<"u"?performance.now():Date.now();if(s&&T-s.at<50){ee.current=!1,U(c);return}ee.current=!1}const a=N(c);if(!a)return;he.current=a;const w=a.replace(/\r\n|\r|\n/g,"\r");K.current(w),U(c),he.current=""},[U,N]),_e=n.useCallback(e=>{e.stopPropagation();const c=e.key.toLowerCase(),a=e.metaKey&&!e.ctrlKey&&!e.altKey&&c==="c",w=e.ctrlKey&&e.shiftKey&&!e.metaKey&&!e.altKey&&c==="c";if((a||w)&&Z()){e.preventDefault(),ve();return}if(e.ctrlKey&&!e.metaKey&&!e.altKey&&e.key.length===1){const Y=e.key.toUpperCase();if(Y>="A"&&Y<="Z"){e.preventDefault(),e.nativeEvent?.stopImmediatePropagation(),K.current(String.fromCharCode(Y.charCodeAt(0)-64)),U(e.currentTarget);return}}if(e.altKey&&!e.ctrlKey&&!e.metaKey&&e.key.length===1){e.preventDefault(),e.nativeEvent?.stopImmediatePropagation(),K.current("\x1B"+e.key),U(e.currentTarget);return}const T={ArrowUp:"\x1B[A",ArrowDown:"\x1B[B",ArrowRight:"\x1B[C",ArrowLeft:"\x1B[D",Escape:"\x1B",Tab:" ",Delete:"\x1B[3~",Home:"\x1B[H",End:"\x1B[F"}[e.key];if(T){e.preventDefault(),K.current(T),U(e.currentTarget);return}const h=e.currentTarget;if(e.nativeEvent?.isComposing)return;const M=ge.current,o=typeof performance<"u"?performance.now():Date.now(),P=!!(M&&o-M.at<50);if(e.key==="Enter"){if(P&&M?.type==="insertLineBreak")return;e.preventDefault(),K.current("\r"),U(h);return}if(e.key==="Backspace"){if(e.preventDefault(),P&&M?.type==="deleteContentBackward")return;N(h)||K.current("")}if(e.key.length===1&&!e.ctrlKey&&!e.metaKey&&!e.altKey){const Y=V.current;Y&&o-Y<50||(e.preventDefault(),K.current(e.key),ee.current=!0,ge.current={type:"keydown-text",at:o})}Ye(h)},[U,ve,Z,N,Ye]),Ke=n.useCallback(e=>{e.stopPropagation();const c=e.currentTarget;e.nativeEvent?.isComposing||Ye(c)},[Ye]),Je=n.useCallback(e=>{const c=e.currentTarget;de.current=!1;const a=e.data||N(c);if(V.current=typeof performance<"u"?performance.now():Date.now(),!a)return;const w=a.replace(/\r\n|\r|\n/g,"\r");K.current(w),U(c),ge.current={type:"compositionend",at:typeof performance<"u"?performance.now():Date.now()},ee.current=!0},[U,N]),He=n.useCallback(e=>{e.stopPropagation();const c=e.clipboardData?.getData("text")??"";if(!c)return;e.preventDefault();const w=B.current?.hasBracketedPaste?.()?`\x1B[200~${c}\x1B[201~`:c;K.current(w)},[]),Te={position:"fixed",left:0,top:0,width:1,height:1,opacity:0,zIndex:-1,background:"transparent",color:"transparent",WebkitTextFillColor:"transparent",caretColor:"transparent",textShadow:"none",WebkitAppearance:"none",appearance:"none",resize:"none",overflow:"hidden",whiteSpace:"nowrap",border:"0",padding:0,margin:0,outline:"none",outlineOffset:0,fontSize:16,fontWeight:400,pointerEvents:"none",WebkitUserSelect:"none",userSelect:"none"};return f.jsxs("div",{ref:O,className:et("relative h-full w-full terminal-viewport-container",ye),"data-hidden-input-overlay-active":b?"true":void 0,style:{backgroundColor:E.background},onTouchStart:e=>{if(!(!b||R)&&!Z()){const c=e.touches?.[0];S(c?.clientX,c?.clientY)}},onClick:e=>{if(!R)if(b){if(Z())return;S(e.clientX,e.clientY)}else B.current?.focus()},onMouseUp:()=>{!R&&Z()&&ve()},onTouchEnd:()=>{!R&&Z()&&ve()},children:[b&&typeof document<"u"?vt.createPortal(f.jsxs(f.Fragment,{children:[f.jsx("input",{ref:We,type:"text",inputMode:"text",autoCapitalize:"off",autoComplete:"off",autoCorrect:"off",spellCheck:!1,tabIndex:-1,enterKeyHint:"send","data-terminal-hidden-input":"true","aria-label":J("terminalView.viewport.inputAria"),"aria-hidden":"true",style:{...Te,display:pe?"block":"none"},onBlur:Ge,onBeforeInput:Oe,onCompositionStart:()=>{de.current=!0},onInput:st,onKeyDown:_e,onKeyUp:Ke,onCompositionEnd:Je,onPaste:He}),f.jsx("textarea",{ref:ae,inputMode:"text",autoCapitalize:"off",autoComplete:"off",autoCorrect:"off",spellCheck:!1,tabIndex:-1,enterKeyHint:"send","data-terminal-hidden-input":"true","aria-label":J("terminalView.viewport.inputAria"),"aria-hidden":"true",style:{...Te,display:pe?"none":"block"},onBlur:Ge,onBeforeInput:Oe,onCompositionStart:()=>{de.current=!0},onInput:st,onKeyDown:_e,onKeyUp:Ke,onCompositionEnd:Je,onPaste:He})]}),document.body):null,je.current&&!R?f.jsx(Et,{containerRef:je,disableHorizontal:!0,className:"overlay-scrollbar--flush overlay-scrollbar--dense overlay-scrollbar--zero"}):null]})});gt.displayName="TerminalViewport";const Yt="\x1B",Ot=new RegExp(`${Yt}\\[[0-9;?]*[ -/]*[@-~]`,"g"),Kt=/(https?:\/\/(?:localhost|127\.0\.0\.1|0\.0\.0\.0|\[(?:::1|::)\])(?::\d{2,5})?(?:\/[^\s<>'"`]*)?)/gi,Ht=/(?:➜\s*(?:Local|Network):)|\b(?:local|network|loopback|serving|listening|available|ready|started|running|server|vite|webpack|next\.js|astro|sveltekit|nuxt)\b/i,zt=/Serving HTTP on .*? port (\d{2,5})/i,Ut=new Set([".",",",";",":","!","?"]),Wt=u=>{let l=u;for(;l.length>0;){const d=l[l.length-1];if(d===")"||d==="]"||d==="}"||d===">"){const I=d===")"?"(":d==="]"?"[":d==="}"?"{":"<",E=l.slice(0,-1),X=(E.match(new RegExp(`\\${I}`,"g"))||[]).length,_=(E.match(new RegExp(`\\${d}`,"g"))||[]).length;if(X>_)break;l=E;continue}if(Ut.has(d)){l=l.slice(0,-1);continue}break}return l},yt=u=>{let l=Wt(u);return l=l.replace("0.0.0.0","127.0.0.1"),l=l.replace("[::1]","127.0.0.1"),l=l.replace("[::]","127.0.0.1"),l},qt=u=>{if(!u)return null;const l=u.replace(Ot,""),d=l.match(zt);if(d?.[1]){const E=Number.parseInt(d[1],10);if(Number.isFinite(E)&&E>0&&E<=65535)return`http://127.0.0.1:${E}/`}const I=l.split(`
2
+ `);for(const E of I){if(!Ht.test(E))continue;const X=Array.from(E.matchAll(Kt));if(X.length===0)continue;const _=X.find(ye=>{try{return!!new URL(yt(ye[1])).port}catch{return!1}});return yt((_??X[0])[1])}return null},Xt=async(u,l=1500)=>{if(!u||typeof window>"u")return!1;let d;try{d=new URL(u)}catch{return!1}if(d.protocol!=="http:"&&d.protocol!=="https:")return!1;const I=d.hostname.toLowerCase();if(I!=="localhost"&&I!=="127.0.0.1"&&I!=="0.0.0.0"&&I!=="::1"&&I!=="::")return!1;const E=new AbortController,X=window.setTimeout(()=>E.abort(),l);try{const _=await Mt("/api/system/probe-url",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({url:d.toString()}),cache:"no-store",signal:E.signal});return _.ok?(await _.json().catch(()=>null))?.ok===!0:!1}catch{return!1}finally{window.clearTimeout(X)}},$t={esc:"\x1B",tab:" ",enter:"\r","arrow-up":"\x1B[A","arrow-down":"\x1B[B","arrow-left":"\x1B[D","arrow-right":"\x1B[C"},ft={ctrl:"5",cmd:"3"},wt={retry:{maxRetries:3,initialDelayMs:500,maxDelayMs:8e3},connectionTimeoutMs:1e4},Gt={retry:{...wt.retry,initialDelayMs:200,maxDelayMs:500},connectionTimeoutMs:1500},_t=(u,l)=>{if(l)switch(u){case"arrow-up":return`\x1B[1;${ft[l]}A`;case"arrow-down":return`\x1B[1;${ft[l]}B`;case"arrow-right":return`\x1B[1;${ft[l]}C`;case"arrow-left":return`\x1B[1;${ft[l]}D`}return $t[u]??null},sn=()=>{const{t:u}=bt(),{terminal:l,runtime:d}=Rt(),{currentTheme:I}=St(),{monoFont:E}=At(),X=Me(t=>t.terminalFontSize),_=Me(t=>t.bottomTerminalHeight),ye=Me(t=>t.isBottomTerminalExpanded),{isMobile:R,isTablet:at,hasTouchOnlyPointer:Ne}=It(),Se=R||at,J=(Se||Ne)&&d.platform==="web",O=d.platform!=="vscode",je=Me(t=>t.showTerminalQuickKeysOnDesktop),B=Se||je,tt=ht(t=>t.currentSessionId),K=ht(t=>t.newSessionDraft),Ue=tt!==null||K?.open===!0,v=Bt()??null,Ae=q(t=>t.sessions),se=q(t=>t.hasHydrated),Ve=q(t=>t.ensureDirectory),Le=q(t=>t.createTab),be=q(t=>t.setActiveTab),ie=q(t=>t.closeTab),Q=q(t=>t.setTabSessionId),Ie=q(t=>t.setTabLifecycle),ae=q(t=>t.setConnecting),We=q(t=>t.appendToBuffer),de=q(t=>t.setTabPreviewUrl),ee=q(t=>t.clearBuffer),ge=Me(t=>t.openContextPreview),V=n.useMemo(()=>{if(v)return Ae.get(v)},[Ae,v]),D=n.useMemo(()=>V?O?V.activeTabId??V.tabs[0]?.id??null:V.tabs[0]?.id??null:null,[V,O]),he=n.useMemo(()=>{if(V)return D?V.tabs.find(t=>t.id===D)??V.tabs[0]:V.tabs[0]},[V,D]),Fe=n.useMemo(()=>(V?.tabs??[]).map(t=>({icon:(()=>{const p=t.iconKey?Dt[t.iconKey]??"terminal":"terminal";return f.jsx(fe,{name:p,className:"h-4 w-4"})})(),id:t.id,label:t.label,title:t.label,closeLabel:u("terminalView.tabs.closeTabTitle")})),[V?.tabs,u]),L=he?.terminalSessionId??null,nt=he?.lifecycle??"idle",dt=he?.bufferChunks??[],rt=he?.isConnecting??!1,ct=he?.previewUrl??null,[lt,b]=n.useState(null),[ot,A]=n.useState(!1),[pe,S]=n.useState(!1),[F,N]=n.useState(null),[U,Ye]=n.useState(!1),[qe,Xe]=n.useState(0),ve=n.useRef(null),Z=n.useRef(null),ce=n.useRef(D),H=n.useRef(L),xe=n.useRef(v),Pe=n.useRef(null),$e=n.useRef(null),De=n.useRef(!1),Ge=n.useRef(null),Oe=n.useRef(new Set),st=n.useRef(!1),_e=n.useRef(""),Ke=n.useRef(new Set),Je=n.useRef(0),He=n.useCallback(()=>{_e.current="",Ke.current.clear(),Je.current+=1},[]),Te=n.useCallback(()=>{J||typeof document<"u"&&!document.hasFocus()||Pe.current?.focus()},[J]),e=n.useCallback(()=>{J||Pe.current?.focus()},[J]);n.useEffect(()=>{if(!se||st.current)return;st.current=!0;const t=new Set;for(const[,p]of q.getState().sessions.entries())for(const r of p.tabs)r.terminalSessionId&&t.add(r.terminalSessionId);Oe.current=t},[se]);const c=Me(t=>t.activeMainTab),a=Me(t=>t.isBottomTerminalOpen),w=Me(t=>t.setBottomTerminalOpen),s=Me(t=>t.setBottomTerminalExpanded),T=c==="terminal",h=T||a,[te,M]=n.useState(h);n.useEffect(()=>{!h||d.platform==="vscode"||Pt()},[h,d.platform]),n.useEffect(()=>{h&&M(!0)},[h]),n.useEffect(()=>{De.current=h},[h]),n.useEffect(()=>{H.current=L},[L]),n.useEffect(()=>{ce.current=D,He()},[D,He]),n.useEffect(()=>{xe.current=v},[v]),n.useEffect(()=>{!B&&F!==null&&N(null)},[B,F,N]),n.useEffect(()=>{!L&&F!==null&&N(null)},[L,F,N]);const o=n.useCallback(()=>{ve.current?.(),ve.current=null,Z.current=null,S(!1)},[]);n.useEffect(()=>()=>{o(),H.current=null},[o]);const P=n.useCallback((t,p,r)=>{if(!r)return;const g=`${_e.current}${r}`.replace(/\r\n|\r/g,`
3
3
  `),m=g.split(`
4
4
  `),x=g.endsWith(`
5
5
  `)?m.join(`
6
6
  `):m.slice(0,-1).join(`
7
7
  `);if(_e.current=g.endsWith(`
8
- `)?"":(m[m.length-1]??"").slice(-1024),!x)return;const y=qt(x);if(!y||Ke.current.has(y))return;const G=Je.current;Ke.current.add(y),Xt(y).then(Ee=>{if(Ke.current.delete(y),!Ee||Je.current!==G)return;const we=q.getState().getDirectoryState(t)?.tabs.find(Re=>Re.id===p);!we||we.previewUrlLocked||we.previewUrl===y||de(t,p,y,{locked:!1,autoOpened:!1})})},[de]),Y=n.useCallback((t,p,r,g=wt)=>{if(Z.current===r)return;o(),Z.current=r;const m=l.connect(r,{onEvent:x=>{if(Z.current===r)switch(x.type){case"connected":{ae(t,p,!1),b(null),A(!1),S(!1),Te(),Ge.current===r&&(Ge.current=null,l.sendInput(r,"\r").catch(()=>{}));break}case"reconnecting":{b(null),A(!1),S(!0);break}case"data":{x.data&&(We(t,p,x.data),P(t,p,x.data));break}case"exit":{const y=typeof x.exitCode=="number"?x.exitCode:null,G=typeof x.signal=="number"?x.signal:null,we=!!q.getState().getDirectoryState(t)?.tabs.find(Re=>Re.id===p)?.label?.startsWith("Action:");We(t,p,u("terminalView.stream.processExitedMessage",{exitCodeSegment:y!==null?u("terminalView.stream.processExitedWithCode",{exitCode:y}):"",signalSegment:G!==null?u("terminalView.stream.processExitedWithSignal",{signal:G}):""})),Ie(t,p,"exited"),Q(t,p,null),ae(t,p,!1),b(we?null:u("terminalView.error.sessionEnded")),A(!1),S(!1),o();break}}},onError:(x,y)=>{if(Z.current===r){if(!y){b(null),A(!1);return}S(!1),b(u("terminalView.error.connectionFailed",{message:x.message})),A(!0),ae(t,p,!1),ee(t,p),Ie(t,p,"exited"),Q(t,p,null),o()}}},g);ve.current=()=>{m.close(),Z.current=null}},[We,ee,o,Te,P,ae,Ie,Q,u,l]);n.useEffect(()=>{let t=!1;if(!se||!te)return;if(!v){b(u(Ue?"terminalView.empty.noWorkingDirectory":"terminalView.empty.selectSession")),o();return}return(async()=>{const r=v;if(!xe.current||xe.current!==r)return;Ve(r);const g=q.getState().getDirectoryState(r);if(!g||g.tabs.length===0)return;const m=O?g.activeTabId??g.tabs[0]?.id??null:g.tabs[0]?.id??null;if(!m)return;const x=g.tabs.find(He=>He.id===m)??g.tabs[0];let y=x?.terminalSessionId??null;const G=x?.lifecycle??"idle",Ee=!!x?.label?.startsWith("Action:"),we=(x?.bufferLength??0)>0||(x?.bufferChunks?.length??0)>0,Re=!!y&&Oe.current.has(y)&&(x?.bufferLength??0)===0&&(x?.bufferChunks?.length??0)===0,ut=!!y&&Oe.current.has(y);if(!y){if(G==="exited"){ae(r,m,!1);return}if(Ee&&we){ae(r,m,!1);return}const He=$e.current;if(!He&&De.current)return;b(null),A(!1),S(!1),ae(r,m,!0);try{const Ze=await l.createSession({cwd:r,cols:He?.cols,rows:He?.rows});if(!(!t&&xe.current===r&&ce.current===m)){try{await l.close(Ze.sessionId)}catch{}return}Q(r,m,Ze.sessionId),y=Ze.sessionId}catch(Ze){t||(b(Ze instanceof Error?Ze.message:u("terminalView.error.startSessionFailed")),A(!0),S(!1),ae(r,m,!1));return}}!y||t||(z.current=y,ut&&Oe.current.delete(y),Re&&(Ge.current=y),Y(r,m,y,ut?Gt:wt))})(),()=>{t=!0,z.current=null,o()}},[Ue,v,L,nt,D,te,qe,O,se,Ve,ae,Ie,Q,Y,o,u,l]),n.useEffect(()=>{if(!h||J)return;if(typeof window>"u"){Te();return}const t=window.requestAnimationFrame(()=>{Te()});return()=>{window.cancelAnimationFrame(t)}},[D,Te,h,J]);const me=n.useCallback(async()=>{if(!v||U)return;const t=q.getState().getDirectoryState(v),p=O?D??t?.activeTabId??t?.tabs[0]?.id??null:t?.tabs[0]?.id??null;if(p){Ye(!0),b(null),A(!1),S(!1),o(),ee(v,p),ze();try{await ie(v,p)}catch(r){b(r instanceof Error?r.message:u("terminalView.error.restartFailed")),A(!0),S(!1)}finally{Ye(!1)}}},[D,ee,ie,o,v,O,U,ze,u]),$=n.useCallback(async()=>{await me()},[me]),ke=n.useCallback(()=>{if(!v)return;const t=Le(v);be(v,t),b(null),A(!1),S(!1),o()},[Le,o,v,be]),k=n.useCallback(t=>{v&&(be(v,t),b(null),A(!1),S(!1),o())},[o,v,be]),ne=n.useCallback(t=>{v&&(t===D&&o(),b(null),A(!1),S(!1),ie(v,t))},[D,ie,o,v]),C=n.useCallback(t=>{if(!t||pe)return;let p=t,r=!1;if(F&&t.length>0){const m=t[0];if(m.length===1&&/[a-zA-Z]/.test(m)){const x=m.toUpperCase();(F==="ctrl"||F==="cmd")&&(p=String.fromCharCode(x.charCodeAt(0)&31),r=!0)}r||(r=!0)}const g=z.current;g&&(l.sendInput(g,p).catch(m=>{pe||b(m instanceof Error?m.message:u("terminalView.error.sendInputFailed"))}),r&&(N(null),e()))},[F,e,pe,N,u,l]),Ce=n.useCallback((t,p)=>{const r=$e.current;if((!r||r.cols!==t||r.rows!==p)&&($e.current={cols:t,rows:p},Xe(m=>m+1)),!De.current)return;const g=z.current;g&&l.resize({sessionId:g,cols:t,rows:p}).catch(()=>{})},[l]),Be=n.useCallback(t=>{N(p=>p===t?null:t),e()},[e,N]),H=n.useCallback(t=>{const p=_t(t,F);p&&(C(p),N(null),e())},[F,e,C,N]);n.useEffect(()=>{if(!B||!F||!L)return;const t=p=>{if(p.repeat)return;const r=p.key;if(!r||r==="Control"||r==="Meta"||r==="Alt"||r==="Shift")return;const g=r.length===1?r.toLowerCase():r,m=p.code??"",x=m.startsWith("Key")&&m.length===4?m.slice(3).toUpperCase():null,y=r.length===1&&/[a-zA-Z]/.test(r)?r.toUpperCase():x,G={Tab:"tab",Enter:"enter",ArrowUp:"arrow-up",ArrowDown:"arrow-down",ArrowLeft:"arrow-left",ArrowRight:"arrow-right",Escape:"esc",tab:"tab",enter:"enter",arrowup:"arrow-up",arrowdown:"arrow-down",arrowleft:"arrow-left",arrowright:"arrow-right",escape:"esc"};if(g in G){p.preventDefault(),p.stopPropagation(),H(G[g]);return}if(F==="ctrl"&&y&&y.length===1&&y>="A"&&y<="Z"){const Ee=String.fromCharCode(y.charCodeAt(0)&31);p.preventDefault(),p.stopPropagation(),C(Ee),N(null),e()}};return window.addEventListener("keydown",t),()=>{window.removeEventListener("keydown",t)}},[F,H,C,e,B,N,L]);const Qe=n.useMemo(()=>{const t=it[pt].stack;if(typeof window>"u")return(it[E]??it[pt]).stack;const r=window.getComputedStyle(document.documentElement).getPropertyValue("--font-family-mono");return r&&r.trim().length>0?r.trim():(it[E]??it[pt]).stack??t},[E]),j=n.useMemo(()=>jt(I),[I]),i=n.useMemo(()=>`${v??"no-dir"}::${D??"no-tab"}::${L??"no-terminal"}`,[v,D,L]);if(n.useEffect(()=>{if(!h||J)return;const t=Pe.current;if(!t)return;const p=()=>{t.fit()};if(typeof window<"u"){const r=window.requestAnimationFrame(()=>{p(),Te()}),g=[220,400].map(m=>window.setTimeout(p,m));return()=>{window.cancelAnimationFrame(r),g.forEach(m=>window.clearTimeout(m))}}p()},[Te,h,J,i,L]),n.useEffect(()=>{if(J||!h||!a)return;const t=Pe.current;if(!t)return;const p=()=>{t.fit()};if(typeof window<"u"){const r=window.requestAnimationFrame(()=>{p()}),g=[0,80,180,320].map(m=>window.setTimeout(p,m));return()=>{window.cancelAnimationFrame(r),g.forEach(m=>window.clearTimeout(m))}}p()},[_,ye,a,h,J]),!Ue)return f.jsx("div",{className:"flex h-full items-center justify-center p-4 text-center text-sm text-muted-foreground",children:u("terminalView.empty.selectSession")});if(!v)return f.jsxs("div",{className:"flex h-full flex-col items-center justify-center gap-2 p-4 text-center text-sm text-muted-foreground",children:[f.jsx("p",{children:u("terminalView.empty.noWorkingDirectoryForSession")}),f.jsx("button",{onClick:me,className:"rounded-lg bg-primary px-3 py-1.5 text-xs font-medium text-primary-foreground hover:bg-primary/90",children:u("terminalView.actions.retry")})]});const W=!L||rt||U||pe,re=te,le=!Se&&a&&!T,ue=f.jsxs(f.Fragment,{children:[f.jsx(oe,{type:"button",size:"sm",variant:"outline",className:"h-6 px-2 text-xs",onClick:()=>H("esc"),disabled:W,children:u("terminalView.quickKeys.escape")}),f.jsxs(oe,{type:"button",size:"sm",variant:"outline",className:"h-6 w-9 p-0",onClick:()=>H("tab"),disabled:W,children:[f.jsx(fe,{name:"arrow-right",className:"h-4 w-4"}),f.jsx("span",{className:"sr-only",children:u("terminalView.quickKeys.tabAria")})]}),f.jsxs(oe,{type:"button",size:"sm",variant:"chip","aria-pressed":F==="ctrl",className:"h-6 w-9 p-0",onClick:()=>Be("ctrl"),disabled:W,children:[f.jsx("span",{className:"text-xs font-medium",children:u("terminalView.quickKeys.controlLabel")}),f.jsx("span",{className:"sr-only",children:u("terminalView.quickKeys.controlModifierAria")})]}),f.jsxs(oe,{type:"button",size:"sm",variant:"chip","aria-pressed":F==="cmd",className:"h-6 w-9 p-0",onClick:()=>Be("cmd"),disabled:W,children:[f.jsx(fe,{name:"command"}),f.jsx("span",{className:"sr-only",children:u("terminalView.quickKeys.commandModifierAria")})]}),f.jsxs(oe,{type:"button",size:"sm",variant:"outline",className:"h-6 w-9 p-0",onClick:()=>H("arrow-up"),disabled:W,children:[f.jsx(fe,{name:"arrow-up"}),f.jsx("span",{className:"sr-only",children:u("terminalView.quickKeys.arrowUpAria")})]}),f.jsxs(oe,{type:"button",size:"sm",variant:"outline",className:"h-6 w-9 p-0",onClick:()=>H("arrow-left"),disabled:W,children:[f.jsx(fe,{name:"arrow-left"}),f.jsx("span",{className:"sr-only",children:u("terminalView.quickKeys.arrowLeftAria")})]}),f.jsxs(oe,{type:"button",size:"sm",variant:"outline",className:"h-6 w-9 p-0",onClick:()=>H("arrow-down"),disabled:W,children:[f.jsx(fe,{name:"arrow-down"}),f.jsx("span",{className:"sr-only",children:u("terminalView.quickKeys.arrowDownAria")})]}),f.jsxs(oe,{type:"button",size:"sm",variant:"outline",className:"h-6 w-9 p-0",onClick:()=>H("arrow-right"),disabled:W,children:[f.jsx(fe,{name:"arrow-right"}),f.jsx("span",{className:"sr-only",children:u("terminalView.quickKeys.arrowRightAria")})]}),f.jsxs(oe,{type:"button",size:"sm",variant:"outline",className:"h-6 w-9 p-0",onClick:()=>H("enter"),disabled:W,children:[f.jsx(fe,{name:"arrow-go-back"}),f.jsx("span",{className:"sr-only",children:u("terminalView.quickKeys.enterAria")})]})]});return f.jsxs("div",{className:"flex h-full flex-col overflow-hidden bg-[var(--surface-background)]",children:[f.jsxs("div",{className:et("app-region-no-drag sticky top-0 z-20 shrink-0 bg-[var(--surface-background)] text-xs",Se?"px-3 py-1.5":"pl-3 pr-1.5 py-1"),children:[O&&V?f.jsxs("div",{className:"flex items-center gap-2 pl-1 pr-1",children:[f.jsx("div",{className:et("min-w-0 flex-1",Se?"h-8":"h-7"),children:f.jsx(Nt,{items:Fe,activeId:D,onSelect:k,onClose:ne,layoutMode:"scrollable",variant:"default",className:"h-full bg-transparent"})}),f.jsx(oe,{type:"button",size:"xs",variant:"ghost",className:et("shrink-0",Se?"h-8 w-8 p-0":"h-7 w-7 p-0"),onClick:ke,title:u("terminalView.tabs.newTabTitle"),children:f.jsx(fe,{name:"add",className:`${Se?"h-[18px] w-[18px]":"h-4 w-4"}`})}),f.jsxs("div",{className:"flex shrink-0 items-center gap-1 overflow-visible",children:[ct?f.jsxs(oe,{type:"button",size:"xs",variant:"outline",className:"h-6 shrink-0 gap-1 px-2",onClick:()=>{v&&ge(v,ct)},title:u("terminalView.preview.openTitle"),children:[f.jsx(fe,{name:"global",className:"h-3.5 w-3.5 shrink-0"}),f.jsx("span",{className:"whitespace-nowrap",children:u("terminalView.preview.open")})]}):null,le?f.jsxs(f.Fragment,{children:[f.jsx(oe,{type:"button",size:"xs",variant:"ghost",onClick:()=>s(!ye),className:et("shrink-0 p-0",R?"h-8 w-8":"h-7 w-7"),title:u(ye?"terminalView.bottomDock.restoreTitle":"terminalView.bottomDock.expandTitle"),"aria-label":u(ye?"terminalView.bottomDock.restoreAria":"terminalView.bottomDock.expandAria"),children:ye?f.jsx(fe,{name:"fullscreen-exit",className:"h-4 w-4"}):f.jsx(fe,{name:"fullscreen",className:"h-4 w-4"})}),f.jsx(oe,{type:"button",size:"xs",variant:"ghost",onClick:()=>w(!1),className:et("shrink-0 p-0",R?"h-8 w-8":"h-7 w-7"),title:u("terminalView.bottomDock.closeTitle"),"aria-label":u("terminalView.bottomDock.closeAria"),children:f.jsx(fe,{name:"close",className:"h-4 w-4"})})]}):null]})]}):null,!R&&B&&O&&V?f.jsx("div",{className:"mt-2 flex flex-wrap items-center gap-1 pl-1 pr-1",children:ue}):null,B&&(R||!O||!V)?f.jsx("div",{className:"mt-2 flex flex-wrap items-center gap-1",children:ue}):null]}),f.jsxs("div",{className:"relative flex-1 overflow-hidden",style:{backgroundColor:j.background},children:[f.jsx("div",{className:"h-full w-full box-border pl-4 pr-1.5 pt-3 pb-4",children:re?f.jsx(gt,{ref:t=>{Pe.current=t},sessionKey:i,chunks:dt,onInput:C,onResize:Ce,theme:j,fontFamily:Qe,fontSize:X,enableTouchScroll:J,autoFocus:!J&&h,isVisible:h},i):null}),!pe&&lt&&f.jsxs("div",{className:"absolute inset-x-0 bottom-0 bg-[var(--status-error-background)] px-3 py-2 text-xs text-[var(--status-error-foreground)] flex items-center justify-between gap-2",children:[f.jsx("span",{children:lt}),ot&&R&&f.jsx(oe,{size:"sm",variant:"secondary",className:"h-6 px-2 py-0 text-xs",onClick:$,disabled:U,title:u("terminalView.actions.hardRestartTitle"),type:"button",children:u("terminalView.actions.hardRestart")})]})]})]})};export{sn as TerminalView};
8
+ `)?"":(m[m.length-1]??"").slice(-1024),!x)return;const y=qt(x);if(!y||Ke.current.has(y))return;const G=Je.current;Ke.current.add(y),Xt(y).then(Ee=>{if(Ke.current.delete(y),!Ee||Je.current!==G)return;const we=q.getState().getDirectoryState(t)?.tabs.find(Re=>Re.id===p);!we||we.previewUrlLocked||we.previewUrl===y||de(t,p,y,{locked:!1,autoOpened:!1})})},[de]),Y=n.useCallback((t,p,r,g=wt)=>{if(Z.current===r)return;o(),Z.current=r;const m=l.connect(r,{onEvent:x=>{if(Z.current===r)switch(x.type){case"connected":{ae(t,p,!1),b(null),A(!1),S(!1),Te(),Ge.current===r&&(Ge.current=null,l.sendInput(r,"\r").catch(()=>{}));break}case"reconnecting":{b(null),A(!1),S(!0);break}case"data":{x.data&&(We(t,p,x.data),P(t,p,x.data));break}case"exit":{const y=typeof x.exitCode=="number"?x.exitCode:null,G=typeof x.signal=="number"?x.signal:null,we=!!q.getState().getDirectoryState(t)?.tabs.find(Re=>Re.id===p)?.label?.startsWith("Action:");We(t,p,u("terminalView.stream.processExitedMessage",{exitCodeSegment:y!==null?u("terminalView.stream.processExitedWithCode",{exitCode:y}):"",signalSegment:G!==null?u("terminalView.stream.processExitedWithSignal",{signal:G}):""})),Ie(t,p,"exited"),Q(t,p,null),ae(t,p,!1),b(we?null:u("terminalView.error.sessionEnded")),A(!1),S(!1),o();break}}},onError:(x,y)=>{if(Z.current===r){if(!y){b(null),A(!1);return}S(!1),b(u("terminalView.error.connectionFailed",{message:x.message})),A(!0),ae(t,p,!1),ee(t,p),Ie(t,p,"exited"),Q(t,p,null),o()}}},g);ve.current=()=>{m.close(),Z.current=null}},[We,ee,o,Te,P,ae,Ie,Q,u,l]);n.useEffect(()=>{let t=!1;if(!se||!te)return;if(!v){b(u(Ue?"terminalView.empty.noWorkingDirectory":"terminalView.empty.selectSession")),o();return}return(async()=>{const r=v;if(!xe.current||xe.current!==r)return;Ve(r);const g=q.getState().getDirectoryState(r);if(!g||g.tabs.length===0)return;const m=O?g.activeTabId??g.tabs[0]?.id??null:g.tabs[0]?.id??null;if(!m)return;const x=g.tabs.find(ze=>ze.id===m)??g.tabs[0];let y=x?.terminalSessionId??null;const G=x?.lifecycle??"idle",Ee=!!x?.label?.startsWith("Action:"),we=(x?.bufferLength??0)>0||(x?.bufferChunks?.length??0)>0,Re=!!y&&Oe.current.has(y)&&(x?.bufferLength??0)===0&&(x?.bufferChunks?.length??0)===0,ut=!!y&&Oe.current.has(y);if(!y){if(G==="exited"){ae(r,m,!1);return}if(Ee&&we){ae(r,m,!1);return}const ze=$e.current;if(!ze&&De.current)return;b(null),A(!1),S(!1),ae(r,m,!0);try{const Ze=await l.createSession({cwd:r,cols:ze?.cols,rows:ze?.rows});if(!(!t&&xe.current===r&&ce.current===m)){try{await l.close(Ze.sessionId)}catch{}return}Q(r,m,Ze.sessionId),y=Ze.sessionId}catch(Ze){t||(b(Ze instanceof Error?Ze.message:u("terminalView.error.startSessionFailed")),A(!0),S(!1),ae(r,m,!1));return}}!y||t||(H.current=y,ut&&Oe.current.delete(y),Re&&(Ge.current=y),Y(r,m,y,ut?Gt:wt))})(),()=>{t=!0,H.current=null,o()}},[Ue,v,L,nt,D,te,qe,O,se,Ve,ae,Ie,Q,Y,o,u,l]),n.useEffect(()=>{if(!h||J)return;if(typeof window>"u"){Te();return}const t=window.requestAnimationFrame(()=>{Te()});return()=>{window.cancelAnimationFrame(t)}},[D,Te,h,J]);const me=n.useCallback(async()=>{if(!v||U)return;const t=q.getState().getDirectoryState(v),p=O?D??t?.activeTabId??t?.tabs[0]?.id??null:t?.tabs[0]?.id??null;if(p){Ye(!0),b(null),A(!1),S(!1),o(),ee(v,p),He();try{await ie(v,p)}catch(r){b(r instanceof Error?r.message:u("terminalView.error.restartFailed")),A(!0),S(!1)}finally{Ye(!1)}}},[D,ee,ie,o,v,O,U,He,u]),$=n.useCallback(async()=>{await me()},[me]),ke=n.useCallback(()=>{if(!v)return;const t=Le(v);be(v,t),b(null),A(!1),S(!1),o()},[Le,o,v,be]),k=n.useCallback(t=>{v&&(be(v,t),b(null),A(!1),S(!1),o())},[o,v,be]),ne=n.useCallback(t=>{v&&(t===D&&o(),b(null),A(!1),S(!1),ie(v,t))},[D,ie,o,v]),C=n.useCallback(t=>{if(!t||pe)return;let p=t,r=!1;if(F&&t.length>0){const m=t[0];if(m.length===1&&/[a-zA-Z]/.test(m)){const x=m.toUpperCase();(F==="ctrl"||F==="cmd")&&(p=String.fromCharCode(x.charCodeAt(0)&31),r=!0)}r||(r=!0)}const g=H.current;g&&(l.sendInput(g,p).catch(m=>{pe||b(m instanceof Error?m.message:u("terminalView.error.sendInputFailed"))}),r&&(N(null),e()))},[F,e,pe,N,u,l]),Ce=n.useCallback((t,p)=>{const r=$e.current;if((!r||r.cols!==t||r.rows!==p)&&($e.current={cols:t,rows:p},Xe(m=>m+1)),!De.current)return;const g=H.current;g&&l.resize({sessionId:g,cols:t,rows:p}).catch(()=>{})},[l]),Be=n.useCallback(t=>{N(p=>p===t?null:t),e()},[e,N]),z=n.useCallback(t=>{const p=_t(t,F);p&&(C(p),N(null),e())},[F,e,C,N]);n.useEffect(()=>{if(!B||!F||!L)return;const t=p=>{if(p.repeat)return;const r=p.key;if(!r||r==="Control"||r==="Meta"||r==="Alt"||r==="Shift")return;const g=r.length===1?r.toLowerCase():r,m=p.code??"",x=m.startsWith("Key")&&m.length===4?m.slice(3).toUpperCase():null,y=r.length===1&&/[a-zA-Z]/.test(r)?r.toUpperCase():x,G={Tab:"tab",Enter:"enter",ArrowUp:"arrow-up",ArrowDown:"arrow-down",ArrowLeft:"arrow-left",ArrowRight:"arrow-right",Escape:"esc",tab:"tab",enter:"enter",arrowup:"arrow-up",arrowdown:"arrow-down",arrowleft:"arrow-left",arrowright:"arrow-right",escape:"esc"};if(g in G){p.preventDefault(),p.stopPropagation(),z(G[g]);return}if(F==="ctrl"&&y&&y.length===1&&y>="A"&&y<="Z"){const Ee=String.fromCharCode(y.charCodeAt(0)&31);p.preventDefault(),p.stopPropagation(),C(Ee),N(null),e()}};return window.addEventListener("keydown",t),()=>{window.removeEventListener("keydown",t)}},[F,z,C,e,B,N,L]);const Qe=n.useMemo(()=>{const t=it[pt].stack;if(typeof window>"u")return(it[E]??it[pt]).stack;const r=window.getComputedStyle(document.documentElement).getPropertyValue("--font-family-mono");return r&&r.trim().length>0?r.trim():(it[E]??it[pt]).stack??t},[E]),j=n.useMemo(()=>jt(I),[I]),i=n.useMemo(()=>`${v??"no-dir"}::${D??"no-tab"}::${L??"no-terminal"}`,[v,D,L]);if(n.useEffect(()=>{if(!h||J)return;const t=Pe.current;if(!t)return;const p=()=>{t.fit()};if(typeof window<"u"){const r=window.requestAnimationFrame(()=>{p(),Te()}),g=[220,400].map(m=>window.setTimeout(p,m));return()=>{window.cancelAnimationFrame(r),g.forEach(m=>window.clearTimeout(m))}}p()},[Te,h,J,i,L]),n.useEffect(()=>{if(J||!h||!a)return;const t=Pe.current;if(!t)return;const p=()=>{t.fit()};if(typeof window<"u"){const r=window.requestAnimationFrame(()=>{p()}),g=[0,80,180,320].map(m=>window.setTimeout(p,m));return()=>{window.cancelAnimationFrame(r),g.forEach(m=>window.clearTimeout(m))}}p()},[_,ye,a,h,J]),!Ue)return f.jsx("div",{className:"flex h-full items-center justify-center p-4 text-center text-sm text-muted-foreground",children:u("terminalView.empty.selectSession")});if(!v)return f.jsxs("div",{className:"flex h-full flex-col items-center justify-center gap-2 p-4 text-center text-sm text-muted-foreground",children:[f.jsx("p",{children:u("terminalView.empty.noWorkingDirectoryForSession")}),f.jsx("button",{onClick:me,className:"rounded-lg bg-primary px-3 py-1.5 text-xs font-medium text-primary-foreground hover:bg-primary/90",children:u("terminalView.actions.retry")})]});const W=!L||rt||U||pe,re=te,le=!Se&&a&&!T,ue=f.jsxs(f.Fragment,{children:[f.jsx(oe,{type:"button",size:"sm",variant:"outline",className:"h-6 px-2 text-xs",onClick:()=>z("esc"),disabled:W,children:u("terminalView.quickKeys.escape")}),f.jsxs(oe,{type:"button",size:"sm",variant:"outline",className:"h-6 w-9 p-0",onClick:()=>z("tab"),disabled:W,children:[f.jsx(fe,{name:"arrow-right",className:"h-4 w-4"}),f.jsx("span",{className:"sr-only",children:u("terminalView.quickKeys.tabAria")})]}),f.jsxs(oe,{type:"button",size:"sm",variant:"chip","aria-pressed":F==="ctrl",className:"h-6 w-9 p-0",onClick:()=>Be("ctrl"),disabled:W,children:[f.jsx("span",{className:"text-xs font-medium",children:u("terminalView.quickKeys.controlLabel")}),f.jsx("span",{className:"sr-only",children:u("terminalView.quickKeys.controlModifierAria")})]}),f.jsxs(oe,{type:"button",size:"sm",variant:"chip","aria-pressed":F==="cmd",className:"h-6 w-9 p-0",onClick:()=>Be("cmd"),disabled:W,children:[f.jsx(fe,{name:"command"}),f.jsx("span",{className:"sr-only",children:u("terminalView.quickKeys.commandModifierAria")})]}),f.jsxs(oe,{type:"button",size:"sm",variant:"outline",className:"h-6 w-9 p-0",onClick:()=>z("arrow-up"),disabled:W,children:[f.jsx(fe,{name:"arrow-up"}),f.jsx("span",{className:"sr-only",children:u("terminalView.quickKeys.arrowUpAria")})]}),f.jsxs(oe,{type:"button",size:"sm",variant:"outline",className:"h-6 w-9 p-0",onClick:()=>z("arrow-left"),disabled:W,children:[f.jsx(fe,{name:"arrow-left"}),f.jsx("span",{className:"sr-only",children:u("terminalView.quickKeys.arrowLeftAria")})]}),f.jsxs(oe,{type:"button",size:"sm",variant:"outline",className:"h-6 w-9 p-0",onClick:()=>z("arrow-down"),disabled:W,children:[f.jsx(fe,{name:"arrow-down"}),f.jsx("span",{className:"sr-only",children:u("terminalView.quickKeys.arrowDownAria")})]}),f.jsxs(oe,{type:"button",size:"sm",variant:"outline",className:"h-6 w-9 p-0",onClick:()=>z("arrow-right"),disabled:W,children:[f.jsx(fe,{name:"arrow-right"}),f.jsx("span",{className:"sr-only",children:u("terminalView.quickKeys.arrowRightAria")})]}),f.jsxs(oe,{type:"button",size:"sm",variant:"outline",className:"h-6 w-9 p-0",onClick:()=>z("enter"),disabled:W,children:[f.jsx(fe,{name:"arrow-go-back"}),f.jsx("span",{className:"sr-only",children:u("terminalView.quickKeys.enterAria")})]})]});return f.jsxs("div",{className:"flex h-full flex-col overflow-hidden bg-[var(--surface-background)]",children:[f.jsxs("div",{className:et("app-region-no-drag sticky top-0 z-20 shrink-0 bg-[var(--surface-background)] text-xs",Se?"px-3 py-1.5":"pl-3 pr-1.5 py-1"),children:[O&&V?f.jsxs("div",{className:"flex items-center gap-2 pl-1 pr-1",children:[f.jsx("div",{className:et("min-w-0 flex-1",Se?"h-8":"h-7"),children:f.jsx(Nt,{items:Fe,activeId:D,onSelect:k,onClose:ne,layoutMode:"scrollable",variant:"default",className:"h-full bg-transparent"})}),f.jsx(oe,{type:"button",size:"xs",variant:"ghost",className:et("shrink-0",Se?"h-8 w-8 p-0":"h-7 w-7 p-0"),onClick:ke,title:u("terminalView.tabs.newTabTitle"),children:f.jsx(fe,{name:"add",className:`${Se?"h-[18px] w-[18px]":"h-4 w-4"}`})}),f.jsxs("div",{className:"flex shrink-0 items-center gap-1 overflow-visible",children:[ct?f.jsxs(oe,{type:"button",size:"xs",variant:"outline",className:"h-6 shrink-0 gap-1 px-2",onClick:()=>{v&&ge(v,ct)},title:u("terminalView.preview.openTitle"),children:[f.jsx(fe,{name:"global",className:"h-3.5 w-3.5 shrink-0"}),f.jsx("span",{className:"whitespace-nowrap",children:u("terminalView.preview.open")})]}):null,le?f.jsxs(f.Fragment,{children:[f.jsx(oe,{type:"button",size:"xs",variant:"ghost",onClick:()=>s(!ye),className:et("shrink-0 p-0",R?"h-8 w-8":"h-7 w-7"),title:u(ye?"terminalView.bottomDock.restoreTitle":"terminalView.bottomDock.expandTitle"),"aria-label":u(ye?"terminalView.bottomDock.restoreAria":"terminalView.bottomDock.expandAria"),children:ye?f.jsx(fe,{name:"fullscreen-exit",className:"h-4 w-4"}):f.jsx(fe,{name:"fullscreen",className:"h-4 w-4"})}),f.jsx(oe,{type:"button",size:"xs",variant:"ghost",onClick:()=>w(!1),className:et("shrink-0 p-0",R?"h-8 w-8":"h-7 w-7"),title:u("terminalView.bottomDock.closeTitle"),"aria-label":u("terminalView.bottomDock.closeAria"),children:f.jsx(fe,{name:"close",className:"h-4 w-4"})})]}):null]})]}):null,!R&&B&&O&&V?f.jsx("div",{className:"mt-2 flex flex-wrap items-center gap-1 pl-1 pr-1",children:ue}):null,B&&(R||!O||!V)?f.jsx("div",{className:"mt-2 flex flex-wrap items-center gap-1",children:ue}):null]}),f.jsxs("div",{className:"relative flex-1 overflow-hidden",style:{backgroundColor:j.background},children:[f.jsx("div",{className:"h-full w-full box-border pl-4 pr-1.5 pt-3 pb-4",children:re?f.jsx(gt,{ref:t=>{Pe.current=t},sessionKey:i,chunks:dt,onInput:C,onResize:Ce,theme:j,fontFamily:Qe,fontSize:X,enableTouchScroll:J,autoFocus:!J&&h,isVisible:h},i):null}),!pe&&lt&&f.jsxs("div",{className:"absolute inset-x-0 bottom-0 bg-[var(--status-error-background)] px-3 py-2 text-xs text-[var(--status-error-foreground)] flex items-center justify-between gap-2",children:[f.jsx("span",{children:lt}),ot&&R&&f.jsx(oe,{size:"sm",variant:"secondary",className:"h-6 px-2 py-0 text-xs",onClick:$,disabled:U,title:u("terminalView.actions.hardRestartTitle"),type:"button",children:u("terminalView.actions.hardRestart")})]})]})]})};export{sn as TerminalView};