aicodeman 0.6.4 → 0.6.5

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 (37) hide show
  1. package/dist/web/public/api-client.3adebdc2.js.gz +0 -0
  2. package/dist/web/public/app.6f362fec.js.gz +0 -0
  3. package/dist/web/public/constants.193cefd7.js.gz +0 -0
  4. package/dist/web/public/index.html +1 -1
  5. package/dist/web/public/index.html.br +0 -0
  6. package/dist/web/public/index.html.gz +0 -0
  7. package/dist/web/public/input-cjk.88082175.js.gz +0 -0
  8. package/dist/web/public/keyboard-accessory.29aebd9c.js.gz +0 -0
  9. package/dist/web/public/mobile-handlers.1e2a8ef8.js.gz +0 -0
  10. package/dist/web/public/mobile.37d62c06.css.gz +0 -0
  11. package/dist/web/public/notification-manager.2d5ea8ec.js.gz +0 -0
  12. package/dist/web/public/orchestrator-panel.js.gz +0 -0
  13. package/dist/web/public/panels-ui.07e4dee7.js.gz +0 -0
  14. package/dist/web/public/ralph-panel.61076370.js.gz +0 -0
  15. package/dist/web/public/ralph-wizard.6b0f0be7.js.gz +0 -0
  16. package/dist/web/public/respawn-ui.5377f958.js.gz +0 -0
  17. package/dist/web/public/session-ui.536c2703.js.gz +0 -0
  18. package/dist/web/public/settings-ui.25a18120.js.gz +0 -0
  19. package/dist/web/public/styles.24bfe89a.css.gz +0 -0
  20. package/dist/web/public/subagent-windows.a366a4ad.js.gz +0 -0
  21. package/dist/web/public/sw.js.gz +0 -0
  22. package/dist/web/public/terminal-ui.9ec9bdd7.js +3 -0
  23. package/dist/web/public/terminal-ui.9ec9bdd7.js.br +0 -0
  24. package/dist/web/public/terminal-ui.9ec9bdd7.js.gz +0 -0
  25. package/dist/web/public/upload.html.gz +0 -0
  26. package/dist/web/public/vendor/marked.min.js.gz +0 -0
  27. package/dist/web/public/vendor/xterm-addon-fit.min.js.gz +0 -0
  28. package/dist/web/public/vendor/xterm-addon-unicode11.min.js.gz +0 -0
  29. package/dist/web/public/vendor/xterm-addon-webgl.min.js.gz +0 -0
  30. package/dist/web/public/vendor/xterm-zerolag-input.137ad9f0.js.gz +0 -0
  31. package/dist/web/public/vendor/xterm.css.gz +0 -0
  32. package/dist/web/public/vendor/xterm.min.js.gz +0 -0
  33. package/dist/web/public/voice-input.085e9e73.js.gz +0 -0
  34. package/package.json +1 -1
  35. package/dist/web/public/terminal-ui.f8f6f753.js +0 -3
  36. package/dist/web/public/terminal-ui.f8f6f753.js.br +0 -0
  37. package/dist/web/public/terminal-ui.f8f6f753.js.gz +0 -0
Binary file
@@ -1794,7 +1794,7 @@
1794
1794
  <script defer src="keyboard-accessory.29aebd9c.js"></script>
1795
1795
  <script defer src="input-cjk.88082175.js"></script>
1796
1796
  <script defer src="app.6f362fec.js"></script>
1797
- <script defer src="terminal-ui.f8f6f753.js"></script>
1797
+ <script defer src="terminal-ui.9ec9bdd7.js"></script>
1798
1798
  <script defer src="respawn-ui.5377f958.js"></script>
1799
1799
  <script defer src="ralph-panel.61076370.js"></script>
1800
1800
  <script defer src="orchestrator-panel.js"></script>
Binary file
Binary file
Binary file
@@ -0,0 +1,3 @@
1
+ "use strict";Object.assign(CodemanApp.prototype,{initTerminal(){const e=parseInt(localStorage.getItem("codeman-scrollback"))||DEFAULT_SCROLLBACK;if(this.terminal=new Terminal({theme:{background:"#0d0d0d",foreground:"#e0e0e0",cursor:"#e0e0e0",cursorAccent:"#0d0d0d",selection:"rgba(255, 255, 255, 0.3)",black:"#0d0d0d",red:"#ff6b6b",green:"#51cf66",yellow:"#ffd43b",blue:"#339af0",magenta:"#cc5de8",cyan:"#22b8cf",white:"#e0e0e0",brightBlack:"#495057",brightRed:"#ff8787",brightGreen:"#69db7c",brightYellow:"#ffe066",brightBlue:"#5c7cfa",brightMagenta:"#da77f2",brightCyan:"#66d9e8",brightWhite:"#ffffff"},fontFamily:'"Fira Code", "Cascadia Code", "JetBrains Mono", "SF Mono", Monaco, monospace',fontSize:MobileDetection.getDeviceType()==="mobile"?10:14,lineHeight:1.2,cursorBlink:!1,cursorStyle:"block",scrollback:e,allowTransparency:!0,allowProposedApi:!0}),this.fitAddon=new FitAddon.FitAddon,this.terminal.loadAddon(this.fitAddon),typeof Unicode11Addon<"u")try{const t=new Unicode11Addon.Unicode11Addon;this.terminal.loadAddon(t),this.terminal.unicode.activeVersion="11"}catch{}const i=document.getElementById("terminalContainer");this.terminal.open(i),this.terminal.attachCustomKeyEventHandler(t=>{if(t.isComposing||t.keyCode===229||t.altKey&&t.key>="0"&&t.key<="9")return!1;if(t.key==="Enter"&&(t.shiftKey||t.ctrlKey)&&t.type==="keydown"){if(this.activeSessionId)if(this._localEchoEnabled){const l=this._localEchoOverlay?.pendingText||"";this._localEchoOverlay?.clear(),this._localEchoOverlay?.suppressBufferDetection(),this._flushedOffsets?.delete(this.activeSessionId),this._flushedTexts?.delete(this.activeSessionId),l&&(this._pendingInput+=l,r()),setTimeout(()=>{fetch(`/api/sessions/${this.activeSessionId}/send-key`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({key:t.ctrlKey?"C-Enter":"S-Enter"})})},l?80:0)}else fetch(`/api/sessions/${this.activeSessionId}/send-key`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({key:t.ctrlKey?"C-Enter":"S-Enter"})});return!1}return!0});{const t=i.querySelector(".xterm-helper-textarea");if(t&&MobileDetection.isTouchDevice()){let l=!1,h=0;t.addEventListener("compositionstart",()=>{l=!0}),t.addEventListener("compositionend",()=>{l=!1}),t.addEventListener("keydown",c=>{!c.isComposing&&c.keyCode!==229&&(h=Date.now())}),t.addEventListener("input",c=>{if(l||c.isComposing||c.inputType!=="insertText"||!c.data||Date.now()-h<50)return;const u=c.data;Promise.resolve().then(()=>{const a=t.value;!a||a.trim()===""&&u!==" "||(this.terminal._core.coreService.triggerDataEvent(u,!0),t.value="")})})}}if(this._webglAddon=null,!(MobileDetection.getDeviceType()!=="desktop")&&!new URLSearchParams(location.search).has("nowebgl"))if(typeof WebglAddon<"u")this._initWebGL();else{const t=document.createElement("script");t.src="vendor/xterm-addon-webgl.min.js",t.onload=()=>this._initWebGL(),t.onerror=()=>console.warn("[CRASH-DIAG] Failed to load WebGL addon \u2014 using canvas renderer"),document.head.appendChild(t)}this._localEchoOverlay=new LocalEchoOverlay(this.terminal),this._cjkInput=null,typeof CjkInput<"u"&&(this._cjkInput=CjkInput.init({send:t=>{this.activeSessionId&&this._sendInputAsync(this.activeSessionId,t)}})),MobileDetection.getDeviceType()==="mobile"&&document.body.classList.contains("safari-browser")?requestAnimationFrame(()=>{this.fitAddon.fit(),requestAnimationFrame(()=>this.fitAddon.fit())}):this.fitAddon.fit(),this.registerFilePathLinkProvider(),i.addEventListener("wheel",t=>{t.preventDefault();const l=Math.round(t.deltaY/25)||(t.deltaY>0?1:-1);this.terminal.scrollLines(l)},{passive:!1});{const t=()=>this.terminal._core?._renderService?.dimensions?.css?.cell?.height||13;let l=0,h=0,c=0,u=null,a=!1;const m=y=>{const _=c?(y-c)/16.67:1;if(c=y,!a&&Math.abs(h)>.3){const v=Math.round(h/t());v!==0&&this.terminal.scrollLines(v),h*=.92,u=requestAnimationFrame(m)}else a?u=requestAnimationFrame(m):(u=null,h=0)};let p=0,g=!1;i.addEventListener("touchstart",y=>{y.touches.length===1&&(l=y.touches[0].clientY,h=0,p=0,a=!0,g=!1,c=0,u&&(cancelAnimationFrame(u),u=null))},{passive:!0}),i.addEventListener("touchmove",y=>{if(y.touches.length===1&&a){g=!0;const _=y.touches[0].clientY,v=l-_;p+=v,h=v*1.2,l=_;const b=t(),S=Math.trunc(p/b);S!==0&&(this.terminal.scrollLines(S),p-=S*b)}},{passive:!0}),i.addEventListener("touchend",()=>{a=!1,!u&&Math.abs(h)>.3&&(u=requestAnimationFrame(m)),!g&&this.terminal&&this.terminal.focus()},{passive:!0}),i.addEventListener("touchcancel",()=>{a=!1,h=0,p=0},{passive:!0})}this.showWelcome(),this._chunkedWriteGen=0,this._resizeTimeout=null,this._lastResizeDims=null;const d=40,f=10,o=()=>{this._resizeTimeout&&clearTimeout(this._resizeTimeout),this._resizeTimeout=setTimeout(()=>{this._resizeTimeout=null,this.fitAddon&&this.fitAddon.fit(),this.flickerFilterBuffer&&(this.flickerFilterTimeout&&(clearTimeout(this.flickerFilterTimeout),this.flickerFilterTimeout=null),this.flushFlickerBuffer());const t=typeof KeyboardHandler<"u"&&KeyboardHandler.keyboardVisible;if(this.activeSessionId&&!t){const l=this.fitAddon.proposeDimensions(),h=l?Math.max(l.cols,d):d,c=l?Math.max(l.rows,f):f;if(!this._lastResizeDims||h!==this._lastResizeDims.cols||c!==this._lastResizeDims.rows){const u=this.activeSessionId?this.sessions.get(this.activeSessionId):null;u&&u.mode!=="shell"&&!u._ended&&this.terminal&&this.isTerminalAtBottom()&&this.terminal.write("\x1B[3J\x1B[H\x1B[2J"),this._lastResizeDims={cols:h,rows:c},fetch(`/api/sessions/${this.activeSessionId}/resize`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({cols:h,rows:c})}).catch(()=>{})}}this.updateConnectionLines(),this._localEchoOverlay?.hasPending&&this._localEchoOverlay.rerender()},300)};window.addEventListener("resize",o),this.terminalResizeObserver&&this.terminalResizeObserver.disconnect(),this.terminalResizeObserver=new ResizeObserver(o),this.terminalResizeObserver.observe(i),this._pendingInput="",this._inputFlushTimeout=null,this._lastKeystrokeTime=0;const r=()=>{if(this._inputFlushTimeout=null,this._pendingInput&&this.activeSessionId){const t=this._pendingInput,l=this.activeSessionId;this._pendingInput="",this._sendInputAsync(l,t)}};this.terminal.onData(t=>{if(!(window.cjkActive||document.activeElement?.id==="cjkInput")&&this.activeSessionId){if(/^\x1b\[[\?>=]?[\d;]*[cnR]$/.test(t))return;if(this._localEchoEnabled){if(t==="\x7F"){if(this._localEchoOverlay?.removeChar()==="flushed"){const{count:c,text:u}=this._localEchoOverlay.getFlushed();this._flushedOffsets?.has(this.activeSessionId)&&(c===0?(this._flushedOffsets.delete(this.activeSessionId),this._flushedTexts?.delete(this.activeSessionId)):(this._flushedOffsets.set(this.activeSessionId,c),this._flushedTexts?.set(this.activeSessionId,u))),this._pendingInput+=t,r()}return}if(/^[\r\n]+$/.test(t)){const h=this._localEchoOverlay?.pendingText||"";this._localEchoOverlay?.clear(),this._localEchoOverlay?.suppressBufferDetection(),this._flushedOffsets?.delete(this.activeSessionId),this._flushedTexts?.delete(this.activeSessionId),this._inputFlushTimeout&&(clearTimeout(this._inputFlushTimeout),this._inputFlushTimeout=null),h&&(this._pendingInput+=h,r()),setTimeout(()=>{this._pendingInput+="\r",r()},80);return}if(t.length>1&&t.charCodeAt(0)>=32){this._localEchoOverlay?.appendText(t);return}if(t.charCodeAt(0)<32){if(t.length>1&&t.charCodeAt(0)===27){this._pendingInput+=t,r();return}if(this._restoringFlushedState){this._pendingInput+=t,r();return}if(t===" "){const c=this._localEchoOverlay?.pendingText||"";this._localEchoOverlay?.clear(),this._flushedOffsets?.delete(this.activeSessionId),this._flushedTexts?.delete(this.activeSessionId),c&&(this._pendingInput+=c),this._pendingInput+=t,this._inputFlushTimeout&&(clearTimeout(this._inputFlushTimeout),this._inputFlushTimeout=null);let u="";try{const m=this._localEchoOverlay?.findPrompt?.();if(m){const p=this.terminal.buffer.active,g=p.getLine(p.viewportY+m.row);g&&(u=g.translateToString(!0).slice(m.col+2).trimEnd())}}catch{}this._tabCompletionBaseText=u,r(),this._tabCompletionSessionId=this.activeSessionId,this._tabCompletionRetries=0,this._tabCompletionFallback&&clearTimeout(this._tabCompletionFallback);const a=this;this._tabCompletionFallback=setTimeout(()=>{if(a._tabCompletionFallback=null,!a._tabCompletionSessionId||a._tabCompletionSessionId!==a.activeSessionId)return;const m=a._localEchoOverlay;!m||m.pendingText||a.terminal.write("",()=>{if(!a._tabCompletionSessionId)return;m.resetBufferDetection();const p=m.detectBufferText();p&&p!==a._tabCompletionBaseText&&(a._tabCompletionSessionId=null,a._tabCompletionRetries=0,a._tabCompletionBaseText=null,m.rerender())})},300);return}const h=this._localEchoOverlay?.pendingText||"";this._localEchoOverlay?.clear(),this._localEchoOverlay?.suppressBufferDetection(),this._flushedOffsets?.delete(this.activeSessionId),this._flushedTexts?.delete(this.activeSessionId),h&&(this._pendingInput+=h),this._pendingInput+=t,this._inputFlushTimeout&&(clearTimeout(this._inputFlushTimeout),this._inputFlushTimeout=null),r();return}if(t.length===1&&t.charCodeAt(0)>=32){this._localEchoOverlay?.addChar(t);return}}if(this._pendingInput+=t,t.charCodeAt(0)<32||t.length>1){this._inputFlushTimeout&&(clearTimeout(this._inputFlushTimeout),this._inputFlushTimeout=null),r();return}const l=performance.now();l-this._lastKeystrokeTime>50?(this._inputFlushTimeout&&(clearTimeout(this._inputFlushTimeout),this._inputFlushTimeout=null),this._lastKeystrokeTime=l,r()):(this._lastKeystrokeTime=l,this._inputFlushTimeout||(this._inputFlushTimeout=setTimeout(r,0)))}})},registerFilePathLinkProvider(){const e=this;let i=-1;this.terminal.registerLinkProvider({provideLinks(s,n){s!==i&&(i=s,console.debug("[LinkProvider] Checking line:",s));const f=e.terminal.buffer.active.getLine(s-1);if(!f){n(void 0);return}const o=f.translateToString(!0);if(!o||!o.includes("/")){n(void 0);return}const r=[],t=/https?:\/\/[^\s"'<>|;&)\]\x00-\x1f]+/g,l=(p,g)=>{const y=p.replace(/[.,;:!?)]+$/,""),_=o.indexOf(y,g);_!==-1&&(r.some(v=>v.range.start.x===_+1)||r.push({text:y,range:{start:{x:_+1,y:s},end:{x:_+y.length+1,y:s}},decorations:{pointerCursor:!0,underline:!0},activate(v,b){window.open(b,"_blank","noopener,noreferrer")}}))},h=/(tail|cat|head|less|grep|watch|vim|nano)\s+(?:[^\s\/]*\s+)*(\/[^\s"'<>|;&\n\x00-\x1f]+)/g,c=/(\/(?:home|tmp|var|etc|opt)[^\s"'<>|;&\n\x00-\x1f]*\.(?:log|txt|json|md|yaml|yml|csv|xml|sh|py|ts|js))\b/g,u=/Bash\([^)]*?(\/(?:home|tmp|var|etc|opt)[^\s"'<>|;&\)\n\x00-\x1f]+)/g,a=(p,g)=>{const y=o.indexOf(p,g);y!==-1&&(r.some(_=>_.range.start.x===y+1)||r.push({text:p,range:{start:{x:y+1,y:s},end:{x:y+p.length+1,y:s}},decorations:{pointerCursor:!0,underline:!0},activate(_,v){e.openLogViewerWindow(v,e.activeSessionId)}}))};let m;for(t.lastIndex=0;(m=t.exec(o))!==null;)l(m[0],m.index);for(h.lastIndex=0;(m=h.exec(o))!==null;)a(m[2],m.index);for(c.lastIndex=0;(m=c.exec(o))!==null;)a(m[1],m.index);for(u.lastIndex=0;(m=u.exec(o))!==null;)a(m[1],m.index);r.length>0&&console.debug("[LinkProvider] Found links:",r.map(p=>p.text)),n(r.length>0?r:void 0)}}),console.log("[LinkProvider] File path link provider registered")},showWelcome(){const e=document.getElementById("welcomeOverlay");e&&(e.classList.add("visible"),this.loadTunnelStatus(),this.loadHistorySessions())},hideWelcome(){const e=document.getElementById("welcomeOverlay");e&&e.classList.remove("visible");const i=document.getElementById("welcomeQr");i&&(clearTimeout(this._welcomeQrShrinkTimer),i.classList.remove("expanded"))},async _fetchHistorySessions(){const s=(await(await fetch("/api/history/sessions")).json()).sessions||[];if(s.length===0)return[];const n=new Map;for(const f of s){const o=f.projectKey||f.workingDir;n.has(o)||n.set(o,[]),n.get(o).push(f)}const d=[];for(const[,f]of n)d.push(...f.slice(0,3));return d.sort((f,o)=>new Date(o.lastModified)-new Date(f.lastModified)),d},_resolveCaseLabel(e,i){if(!e)return"";let s=null;for(const n of i||[])if(!(!n||!n.path)){if(e===n.path)return`#${n.name}`;if(e.startsWith(n.path+"/")){const d=n.path.length;(!s||d>s.len)&&(s={name:n.name,suffix:e.slice(d),len:d})}}return s?`#${s.name}${s.suffix}`:e.split("/").pop()||e},_shortenHomePath(e){return(e||"").replace(/^\/home\/[^/]+\//,"~/").replace(/^\/Users\/[^/]+\//,"~/")},_buildHistoryItem(e,i){const s=e.sizeBytes<1024?`${e.sizeBytes}B`:e.sizeBytes<1048576?`${(e.sizeBytes/1024).toFixed(0)}K`:`${(e.sizeBytes/1048576).toFixed(1)}M`,n=new Date(e.lastModified),d=n.toLocaleDateString("en",{month:"short",day:"numeric"})+" "+n.toLocaleTimeString("en",{hour:"2-digit",minute:"2-digit",hour12:!1}),f=this._shortenHomePath(e.workingDir),o=this._resolveCaseLabel(e.workingDir,i),r=document.createElement("div");r.className="history-item",r.title=e.workingDir;const t=document.createElement("div");t.className="history-item-main",t.addEventListener("click",()=>this.resumeHistorySession(e.sessionId,e.workingDir));const l=document.createElement("div");l.className="history-item-text";const h=document.createElement("span");h.className="history-item-title",h.textContent=e.firstPrompt||f;const c=document.createElement("span");c.className="history-item-subtitle",o.startsWith("#")&&c.classList.add("is-case"),c.textContent=o,l.append(h,c);const u=document.createElement("span");u.className="history-item-meta",u.textContent=d;const a=document.createElement("button");a.className="history-item-expand",a.type="button",a.setAttribute("aria-label","Show details"),a.setAttribute("aria-expanded","false"),a.textContent="\u22EF",t.append(l,u,a);const m=document.createElement("div");m.className="history-item-detail",m.hidden=!0;const p=document.createElement("div");p.className="history-detail-row";const g=document.createElement("span");g.className="history-detail-label",g.textContent="Prompt";const y=document.createElement("span");y.className="history-detail-value history-detail-prompt",y.textContent=e.firstPrompt||"(no prompt captured)",p.append(g,y);const _=document.createElement("div");_.className="history-detail-row";const v=document.createElement("span");v.className="history-detail-label",v.textContent="Path";const b=document.createElement("span");b.className="history-detail-value history-detail-path",b.textContent=f,_.append(v,b);const S=document.createElement("div");return S.className="history-detail-row history-detail-meta",S.textContent=`${d} \xB7 ${s} \xB7 ${e.sessionId.slice(0,8)}`,m.append(p,_,S),a.addEventListener("click",x=>{x.stopPropagation();const T=r.classList.toggle("expanded");m.hidden=!T,a.setAttribute("aria-expanded",T?"true":"false")}),r.append(t,m),r},_HISTORY_INITIAL_COUNT:4,async loadHistorySessions(){const e=document.getElementById("historySessions"),i=document.getElementById("historyList");if(!(!e||!i))try{const s=Array.isArray(this.cases)&&this.cases.length>0?Promise.resolve(this.cases):fetch("/api/cases").then(o=>o.ok?o.json():[]).catch(()=>[]),[n,d]=await Promise.all([this._fetchHistorySessions(30),s]);if(n.length===0){e.style.display="none";return}i.replaceChildren();const f=this._HISTORY_INITIAL_COUNT;for(let o=0;o<Math.min(f,n.length);o++)i.appendChild(this._buildHistoryItem(n[o],d));if(n.length>f){const o=document.createElement("button");o.className="history-show-more",o.textContent=`Show ${n.length-f} more`,o.addEventListener("click",()=>{for(let r=f;r<n.length;r++)i.insertBefore(this._buildHistoryItem(n[r],d),o);o.remove()}),i.appendChild(o)}e.style.display=""}catch(s){console.error("[loadHistorySessions]",s),e.style.display="none"}},async resumeHistorySession(e,i){document.getElementById("runModeMenu")?.classList.remove("active");try{this.terminal.clear(),this.terminal.writeln(`\x1B[1;32m Resuming conversation ${e.slice(0,8)}...\x1B[0m`);const s=i.split("/").pop()||"session";let n=1;for(const[,c]of this.sessions){const u=c.name&&c.name.match(/^w(\d+)-/);if(u){const a=parseInt(u[1]);a>=n&&(n=a+1)}}const d=`w${n}-${s}`,o=(this.cases||[]).find(c=>c.path===i)?.name||i.split("/").pop()||"",r=this.buildEnvOverrides(this.getCaseSettings(o),this.loadAppSettingsFromStorage()),l=await(await fetch("/api/sessions",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({workingDir:i,name:d,resumeSessionId:e,...Object.keys(r).length>0?{envOverrides:r}:{}})})).json();if(!l.success)throw new Error(l.error);const h=l.session.id;await fetch(`/api/sessions/${h}/interactive`,{method:"POST"}),this.terminal.writeln(`\x1B[90m Session ${d} ready\x1B[0m`),await this.selectSession(h),this.terminal.focus()}catch(s){this.terminal.writeln(`\x1B[1;31m Error: ${s.message}\x1B[0m`)}},isTerminalAtBottom(){if(!this.terminal)return!0;const e=this.terminal.buffer.active;return e.viewportY>=e.baseY-2},batchTerminalWrite(e){if(this._isLoadingBuffer){this._loadBufferQueue&&this._loadBufferQueue.push(e);return}if(this.writeFrameScheduled||(this._wasAtBottomBeforeWrite=this.isTerminalAtBottom()),(this.activeSessionId?this.sessions.get(this.activeSessionId):null)?.flickerFilterEnabled??!1){if(e.includes("\x1B[2J")||e.includes("\x1B[H\x1B[J")||e.includes("\x1B[H")&&e.includes("\x1B[?25l")){this.flickerFilterActive=!0,this.flickerFilterBuffer+=e,this.flickerFilterTimeout&&clearTimeout(this.flickerFilterTimeout),this.flickerFilterTimeout=setTimeout(()=>{this.flickerFilterTimeout=null,this.flushFlickerBuffer()},SYNC_WAIT_TIMEOUT_MS);return}if(this.flickerFilterActive){this.flickerFilterBuffer+=e;return}}this.pendingWrites.push(e),this.writeFrameScheduled||(this.writeFrameScheduled=!0,requestAnimationFrame(()=>{this.flushPendingWrites(),this.writeFrameScheduled=!1}))},flushFlickerBuffer(){this.flickerFilterBuffer&&(this.pendingWrites.push(this.flickerFilterBuffer),this.flickerFilterBuffer="",this.flickerFilterActive=!1,this.writeFrameScheduled||(this.writeFrameScheduled=!0,requestAnimationFrame(()=>{this.flushPendingWrites(),this.writeFrameScheduled=!1})))},_updateLocalEchoState(){const e=this.loadAppSettingsFromStorage(),i=this.activeSessionId?this.sessions.get(this.activeSessionId):null,n=!!((e.localEchoEnabled??MobileDetection.isTouchDevice())&&i);this._localEchoEnabled&&!n&&this._localEchoOverlay?.clear(),this._localEchoEnabled=n,this._localEchoOverlay&&i&&(i.mode==="opencode"?this._localEchoOverlay.setPrompt({type:"custom",offset:3,find:d=>{try{const f=d.buffer.active,o=f.cursorY,r=f.getLine(f.viewportY+o);if(!r)return null;const l=r.translateToString(!0).indexOf("\u2503");return l>=0?{row:o,col:l}:null}catch{return null}}}):i.mode==="shell"?(this._localEchoOverlay.clear(),this._localEchoEnabled=!1):this._localEchoOverlay.setPrompt({type:"character",char:"\u276F",offset:2}))},flushPendingWrites(){if(this.pendingWrites.length===0||!this.terminal)return;const e=performance.now(),i=this.pendingWrites.join("");this.pendingWrites=[];const s=i.length;s>16384&&_crashDiag.log(`FLUSH: ${(s/1024).toFixed(0)}KB`);const n=65536;let d=!1;s<=n?this.terminal.write(i):(this.terminal.write(i.slice(0,n)),this.pendingWrites.push(i.slice(n)),d=!0,this.writeFrameScheduled||(this.writeFrameScheduled=!0,requestAnimationFrame(()=>{this.flushPendingWrites(),this.writeFrameScheduled=!1})));const f=d?n:s,o=performance.now()-e;if((o>100||d)&&console.warn(`[CRASH-DIAG] flushPendingWrites: ${o.toFixed(0)}ms, ${(f/1024).toFixed(0)}KB written${d?", rest deferred":""} (total ${(s/1024).toFixed(0)}KB)`),this._wasAtBottomBeforeWrite&&this.terminal.scrollToBottom(),this._localEchoOverlay?.hasPending&&this._localEchoOverlay.rerender(),this._tabCompletionSessionId&&this._tabCompletionSessionId===this.activeSessionId&&this._localEchoOverlay&&!this._localEchoOverlay.pendingText){const r=this._localEchoOverlay,t=this;this.terminal.write("",()=>{if(!t._tabCompletionSessionId)return;r.resetBufferDetection();const l=r.detectBufferText();l?l===t._tabCompletionBaseText?(r.undoDetection(),t._tabCompletionRetries=(t._tabCompletionRetries||0)+1,t._tabCompletionRetries>60&&(t._tabCompletionSessionId=null,t._tabCompletionRetries=0)):(t._tabCompletionSessionId=null,t._tabCompletionRetries=0,t._tabCompletionBaseText=null,t._tabCompletionFallback&&(clearTimeout(t._tabCompletionFallback),t._tabCompletionFallback=null),r.rerender()):(t._tabCompletionRetries=(t._tabCompletionRetries||0)+1,t._tabCompletionRetries>60&&(t._tabCompletionSessionId=null,t._tabCompletionRetries=0))})}},chunkedTerminalWrite(e,i=TERMINAL_CHUNK_SIZE){const s=++this._chunkedWriteGen;return new Promise(n=>{if(!e||e.length===0){this._finishBufferLoad(),n();return}this._isLoadingBuffer=!0,this._loadBufferQueue=[];const d=e.replace(DEC_SYNC_STRIP_RE,""),f=()=>{this._chunkedWriteGen===s&&this._finishBufferLoad(),n()};if(d.length<=i){this.terminal.write(d),f();return}let o=0;const r=performance.now();let t=0;const l=()=>{if(this._chunkedWriteGen!==s){n();return}if(o>=d.length){const a=performance.now()-r;console.log(`[CRASH-DIAG] chunkedTerminalWrite complete: ${d.length} bytes in ${t} chunks, ${a.toFixed(0)}ms total`),requestAnimationFrame(f);return}const h=performance.now(),c=d.slice(o,o+i);this.terminal.write(c);const u=performance.now()-h;t++,u>50&&console.warn(`[CRASH-DIAG] chunk #${t} write took ${u.toFixed(0)}ms (${c.length} bytes at offset ${o})`),o+=i,requestAnimationFrame(l)};requestAnimationFrame(l)})},_finishBufferLoad(){this._isLoadingBuffer=!1,this._loadBufferQueue=null},clearTerminal(){this.terminal.clear()},async restoreTerminalSize(){if(!this.activeSessionId){this.showToast("No active session","warning");return}const e=this.getTerminalDimensions();if(!e){this.showToast("Could not determine terminal size","error");return}try{await this.sendResize(this.activeSessionId),await fetch(`/api/sessions/${this.activeSessionId}/input`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({input:"\f"})}),this.showToast(`Terminal restored to ${e.cols}x${e.rows}`,"success")}catch(i){console.error("Failed to restore terminal size:",i),this.showToast("Failed to restore terminal size","error")}},sendPendingCtrlL(e){!this.pendingCtrlL||!this.pendingCtrlL.has(e)||(this.pendingCtrlL.delete(e),e===this.activeSessionId&&this.sendResize(e).then(()=>{fetch(`/api/sessions/${e}/input`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({input:"\f"})})}))},async copyTerminal(){try{const e=this.terminal.buffer.active;let i="";for(let s=0;s<e.length;s++){const n=e.getLine(s);n&&(i+=n.translateToString(!0)+`
2
+ `)}await navigator.clipboard.writeText(i.replace(/\n+$/,`
3
+ `)),this.showToast("Copied to clipboard","success")}catch{this.showToast("Failed to copy","error")}},increaseFontSize(){const e=this.terminal.options.fontSize||14;this.setFontSize(Math.min(e+2,24))},decreaseFontSize(){const e=this.terminal.options.fontSize||14;this.setFontSize(Math.max(e-2,10))},setFontSize(e){this.terminal.options.fontSize=e,document.getElementById("fontSizeDisplay").textContent=e,this.fitAddon.fit(),localStorage.setItem("codeman-font-size",e),this._localEchoOverlay?.refreshFont()},loadFontSize(){const e=localStorage.getItem("codeman-font-size");if(e){const i=parseInt(e,10);i>=10&&i<=24&&(this.terminal.options.fontSize=i,document.getElementById("fontSizeDisplay").textContent=i)}},getTerminalDimensions(){const s=this.fitAddon?.proposeDimensions();return s?{cols:Math.max(s.cols,40),rows:Math.max(s.rows,10)}:null},async sendResize(e){this.fitAddon&&this.fitAddon.fit();const i=this.getTerminalDimensions();if(i){if(this._lastResizeDims={cols:i.cols,rows:i.rows},this._wsReady&&this._wsSessionId===e)try{this._ws.send(JSON.stringify({t:"z",c:i.cols,r:i.rows}));return}catch{}await fetch(`/api/sessions/${e}/resize`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(i)})}},async sendInput(e){this.activeSessionId&&await fetch(`/api/sessions/${this.activeSessionId}/input`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({input:e,useMux:!0})})},toggleDirInput(){const e=document.querySelector("#dirDisplay").parentElement,i=document.getElementById("dirInput");i.classList.contains("hidden")&&(i.classList.remove("hidden"),e.style.display="none",i.focus())},hideDirInput(){const e=document.querySelector("#dirDisplay").parentElement,i=document.getElementById("dirInput");setTimeout(()=>{i.classList.add("hidden"),e.style.display="";const s=i.value.trim();document.getElementById("dirDisplay").textContent=s||"No directory"},100)}});
Binary file
Binary file
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "aicodeman",
3
- "version": "0.6.4",
3
+ "version": "0.6.5",
4
4
  "description": "The missing control plane for AI coding agents - run 20 autonomous agents with real-time monitoring and session persistence",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -1,3 +0,0 @@
1
- "use strict";Object.assign(CodemanApp.prototype,{initTerminal(){const e=parseInt(localStorage.getItem("codeman-scrollback"))||DEFAULT_SCROLLBACK;if(this.terminal=new Terminal({theme:{background:"#0d0d0d",foreground:"#e0e0e0",cursor:"#e0e0e0",cursorAccent:"#0d0d0d",selection:"rgba(255, 255, 255, 0.3)",black:"#0d0d0d",red:"#ff6b6b",green:"#51cf66",yellow:"#ffd43b",blue:"#339af0",magenta:"#cc5de8",cyan:"#22b8cf",white:"#e0e0e0",brightBlack:"#495057",brightRed:"#ff8787",brightGreen:"#69db7c",brightYellow:"#ffe066",brightBlue:"#5c7cfa",brightMagenta:"#da77f2",brightCyan:"#66d9e8",brightWhite:"#ffffff"},fontFamily:'"Fira Code", "Cascadia Code", "JetBrains Mono", "SF Mono", Monaco, monospace',fontSize:MobileDetection.getDeviceType()==="mobile"?10:14,lineHeight:1.2,cursorBlink:!1,cursorStyle:"block",scrollback:e,allowTransparency:!0,allowProposedApi:!0}),this.fitAddon=new FitAddon.FitAddon,this.terminal.loadAddon(this.fitAddon),typeof Unicode11Addon<"u")try{const t=new Unicode11Addon.Unicode11Addon;this.terminal.loadAddon(t),this.terminal.unicode.activeVersion="11"}catch{}const i=document.getElementById("terminalContainer");this.terminal.open(i),this.terminal.attachCustomKeyEventHandler(t=>{if(t.isComposing||t.keyCode===229||t.altKey&&t.key>="0"&&t.key<="9")return!1;if(t.key==="Enter"&&(t.shiftKey||t.ctrlKey)&&t.type==="keydown"){if(this.activeSessionId)if(this._localEchoEnabled){const l=this._localEchoOverlay?.pendingText||"";this._localEchoOverlay?.clear(),this._localEchoOverlay?.suppressBufferDetection(),this._flushedOffsets?.delete(this.activeSessionId),this._flushedTexts?.delete(this.activeSessionId),l&&(this._pendingInput+=l,r()),setTimeout(()=>{fetch(`/api/sessions/${this.activeSessionId}/send-key`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({key:t.ctrlKey?"C-Enter":"S-Enter"})})},l?80:0)}else fetch(`/api/sessions/${this.activeSessionId}/send-key`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({key:t.ctrlKey?"C-Enter":"S-Enter"})});return!1}return!0});{const t=i.querySelector(".xterm-helper-textarea");if(t&&MobileDetection.isTouchDevice()){let l=!1,h=0;t.addEventListener("compositionstart",()=>{l=!0}),t.addEventListener("compositionend",()=>{l=!1}),t.addEventListener("keydown",c=>{!c.isComposing&&c.keyCode!==229&&(h=Date.now())}),t.addEventListener("input",c=>{if(l||c.isComposing||c.inputType!=="insertText"||!c.data||Date.now()-h<50)return;const u=c.data;Promise.resolve().then(()=>{const a=t.value;!a||a.trim()===""||(this.terminal._core.coreService.triggerDataEvent(u,!0),t.value="")})})}}if(this._webglAddon=null,!(MobileDetection.getDeviceType()!=="desktop")&&!new URLSearchParams(location.search).has("nowebgl"))if(typeof WebglAddon<"u")this._initWebGL();else{const t=document.createElement("script");t.src="vendor/xterm-addon-webgl.min.js",t.onload=()=>this._initWebGL(),t.onerror=()=>console.warn("[CRASH-DIAG] Failed to load WebGL addon \u2014 using canvas renderer"),document.head.appendChild(t)}this._localEchoOverlay=new LocalEchoOverlay(this.terminal),this._cjkInput=null,typeof CjkInput<"u"&&(this._cjkInput=CjkInput.init({send:t=>{this.activeSessionId&&this._sendInputAsync(this.activeSessionId,t)}})),MobileDetection.getDeviceType()==="mobile"&&document.body.classList.contains("safari-browser")?requestAnimationFrame(()=>{this.fitAddon.fit(),requestAnimationFrame(()=>this.fitAddon.fit())}):this.fitAddon.fit(),this.registerFilePathLinkProvider(),i.addEventListener("wheel",t=>{t.preventDefault();const l=Math.round(t.deltaY/25)||(t.deltaY>0?1:-1);this.terminal.scrollLines(l)},{passive:!1});{const t=()=>this.terminal._core?._renderService?.dimensions?.css?.cell?.height||13;let l=0,h=0,c=0,u=null,a=!1;const m=y=>{const _=c?(y-c)/16.67:1;if(c=y,!a&&Math.abs(h)>.3){const v=Math.round(h/t());v!==0&&this.terminal.scrollLines(v),h*=.92,u=requestAnimationFrame(m)}else a?u=requestAnimationFrame(m):(u=null,h=0)};let p=0,g=!1;i.addEventListener("touchstart",y=>{y.touches.length===1&&(l=y.touches[0].clientY,h=0,p=0,a=!0,g=!1,c=0,u&&(cancelAnimationFrame(u),u=null))},{passive:!0}),i.addEventListener("touchmove",y=>{if(y.touches.length===1&&a){g=!0;const _=y.touches[0].clientY,v=l-_;p+=v,h=v*1.2,l=_;const b=t(),S=Math.trunc(p/b);S!==0&&(this.terminal.scrollLines(S),p-=S*b)}},{passive:!0}),i.addEventListener("touchend",()=>{a=!1,!u&&Math.abs(h)>.3&&(u=requestAnimationFrame(m)),!g&&this.terminal&&this.terminal.focus()},{passive:!0}),i.addEventListener("touchcancel",()=>{a=!1,h=0,p=0},{passive:!0})}this.showWelcome(),this._chunkedWriteGen=0,this._resizeTimeout=null,this._lastResizeDims=null;const d=40,f=10,o=()=>{this._resizeTimeout&&clearTimeout(this._resizeTimeout),this._resizeTimeout=setTimeout(()=>{this._resizeTimeout=null,this.fitAddon&&this.fitAddon.fit(),this.flickerFilterBuffer&&(this.flickerFilterTimeout&&(clearTimeout(this.flickerFilterTimeout),this.flickerFilterTimeout=null),this.flushFlickerBuffer());const t=typeof KeyboardHandler<"u"&&KeyboardHandler.keyboardVisible;if(this.activeSessionId&&!t){const l=this.fitAddon.proposeDimensions(),h=l?Math.max(l.cols,d):d,c=l?Math.max(l.rows,f):f;if(!this._lastResizeDims||h!==this._lastResizeDims.cols||c!==this._lastResizeDims.rows){const u=this.activeSessionId?this.sessions.get(this.activeSessionId):null;u&&u.mode!=="shell"&&!u._ended&&this.terminal&&this.isTerminalAtBottom()&&this.terminal.write("\x1B[3J\x1B[H\x1B[2J"),this._lastResizeDims={cols:h,rows:c},fetch(`/api/sessions/${this.activeSessionId}/resize`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({cols:h,rows:c})}).catch(()=>{})}}this.updateConnectionLines(),this._localEchoOverlay?.hasPending&&this._localEchoOverlay.rerender()},300)};window.addEventListener("resize",o),this.terminalResizeObserver&&this.terminalResizeObserver.disconnect(),this.terminalResizeObserver=new ResizeObserver(o),this.terminalResizeObserver.observe(i),this._pendingInput="",this._inputFlushTimeout=null,this._lastKeystrokeTime=0;const r=()=>{if(this._inputFlushTimeout=null,this._pendingInput&&this.activeSessionId){const t=this._pendingInput,l=this.activeSessionId;this._pendingInput="",this._sendInputAsync(l,t)}};this.terminal.onData(t=>{if(!(window.cjkActive||document.activeElement?.id==="cjkInput")&&this.activeSessionId){if(/^\x1b\[[\?>=]?[\d;]*[cnR]$/.test(t))return;if(this._localEchoEnabled){if(t==="\x7F"){if(this._localEchoOverlay?.removeChar()==="flushed"){const{count:c,text:u}=this._localEchoOverlay.getFlushed();this._flushedOffsets?.has(this.activeSessionId)&&(c===0?(this._flushedOffsets.delete(this.activeSessionId),this._flushedTexts?.delete(this.activeSessionId)):(this._flushedOffsets.set(this.activeSessionId,c),this._flushedTexts?.set(this.activeSessionId,u))),this._pendingInput+=t,r()}return}if(/^[\r\n]+$/.test(t)){const h=this._localEchoOverlay?.pendingText||"";this._localEchoOverlay?.clear(),this._localEchoOverlay?.suppressBufferDetection(),this._flushedOffsets?.delete(this.activeSessionId),this._flushedTexts?.delete(this.activeSessionId),this._inputFlushTimeout&&(clearTimeout(this._inputFlushTimeout),this._inputFlushTimeout=null),h&&(this._pendingInput+=h,r()),setTimeout(()=>{this._pendingInput+="\r",r()},80);return}if(t.length>1&&t.charCodeAt(0)>=32){this._localEchoOverlay?.appendText(t);return}if(t.charCodeAt(0)<32){if(t.length>1&&t.charCodeAt(0)===27){this._pendingInput+=t,r();return}if(this._restoringFlushedState){this._pendingInput+=t,r();return}if(t===" "){const c=this._localEchoOverlay?.pendingText||"";this._localEchoOverlay?.clear(),this._flushedOffsets?.delete(this.activeSessionId),this._flushedTexts?.delete(this.activeSessionId),c&&(this._pendingInput+=c),this._pendingInput+=t,this._inputFlushTimeout&&(clearTimeout(this._inputFlushTimeout),this._inputFlushTimeout=null);let u="";try{const m=this._localEchoOverlay?.findPrompt?.();if(m){const p=this.terminal.buffer.active,g=p.getLine(p.viewportY+m.row);g&&(u=g.translateToString(!0).slice(m.col+2).trimEnd())}}catch{}this._tabCompletionBaseText=u,r(),this._tabCompletionSessionId=this.activeSessionId,this._tabCompletionRetries=0,this._tabCompletionFallback&&clearTimeout(this._tabCompletionFallback);const a=this;this._tabCompletionFallback=setTimeout(()=>{if(a._tabCompletionFallback=null,!a._tabCompletionSessionId||a._tabCompletionSessionId!==a.activeSessionId)return;const m=a._localEchoOverlay;!m||m.pendingText||a.terminal.write("",()=>{if(!a._tabCompletionSessionId)return;m.resetBufferDetection();const p=m.detectBufferText();p&&p!==a._tabCompletionBaseText&&(a._tabCompletionSessionId=null,a._tabCompletionRetries=0,a._tabCompletionBaseText=null,m.rerender())})},300);return}const h=this._localEchoOverlay?.pendingText||"";this._localEchoOverlay?.clear(),this._localEchoOverlay?.suppressBufferDetection(),this._flushedOffsets?.delete(this.activeSessionId),this._flushedTexts?.delete(this.activeSessionId),h&&(this._pendingInput+=h),this._pendingInput+=t,this._inputFlushTimeout&&(clearTimeout(this._inputFlushTimeout),this._inputFlushTimeout=null),r();return}if(t.length===1&&t.charCodeAt(0)>=32){this._localEchoOverlay?.addChar(t);return}}if(this._pendingInput+=t,t.charCodeAt(0)<32||t.length>1){this._inputFlushTimeout&&(clearTimeout(this._inputFlushTimeout),this._inputFlushTimeout=null),r();return}const l=performance.now();l-this._lastKeystrokeTime>50?(this._inputFlushTimeout&&(clearTimeout(this._inputFlushTimeout),this._inputFlushTimeout=null),this._lastKeystrokeTime=l,r()):(this._lastKeystrokeTime=l,this._inputFlushTimeout||(this._inputFlushTimeout=setTimeout(r,0)))}})},registerFilePathLinkProvider(){const e=this;let i=-1;this.terminal.registerLinkProvider({provideLinks(s,n){s!==i&&(i=s,console.debug("[LinkProvider] Checking line:",s));const f=e.terminal.buffer.active.getLine(s-1);if(!f){n(void 0);return}const o=f.translateToString(!0);if(!o||!o.includes("/")){n(void 0);return}const r=[],t=/https?:\/\/[^\s"'<>|;&)\]\x00-\x1f]+/g,l=(p,g)=>{const y=p.replace(/[.,;:!?)]+$/,""),_=o.indexOf(y,g);_!==-1&&(r.some(v=>v.range.start.x===_+1)||r.push({text:y,range:{start:{x:_+1,y:s},end:{x:_+y.length+1,y:s}},decorations:{pointerCursor:!0,underline:!0},activate(v,b){window.open(b,"_blank","noopener,noreferrer")}}))},h=/(tail|cat|head|less|grep|watch|vim|nano)\s+(?:[^\s\/]*\s+)*(\/[^\s"'<>|;&\n\x00-\x1f]+)/g,c=/(\/(?:home|tmp|var|etc|opt)[^\s"'<>|;&\n\x00-\x1f]*\.(?:log|txt|json|md|yaml|yml|csv|xml|sh|py|ts|js))\b/g,u=/Bash\([^)]*?(\/(?:home|tmp|var|etc|opt)[^\s"'<>|;&\)\n\x00-\x1f]+)/g,a=(p,g)=>{const y=o.indexOf(p,g);y!==-1&&(r.some(_=>_.range.start.x===y+1)||r.push({text:p,range:{start:{x:y+1,y:s},end:{x:y+p.length+1,y:s}},decorations:{pointerCursor:!0,underline:!0},activate(_,v){e.openLogViewerWindow(v,e.activeSessionId)}}))};let m;for(t.lastIndex=0;(m=t.exec(o))!==null;)l(m[0],m.index);for(h.lastIndex=0;(m=h.exec(o))!==null;)a(m[2],m.index);for(c.lastIndex=0;(m=c.exec(o))!==null;)a(m[1],m.index);for(u.lastIndex=0;(m=u.exec(o))!==null;)a(m[1],m.index);r.length>0&&console.debug("[LinkProvider] Found links:",r.map(p=>p.text)),n(r.length>0?r:void 0)}}),console.log("[LinkProvider] File path link provider registered")},showWelcome(){const e=document.getElementById("welcomeOverlay");e&&(e.classList.add("visible"),this.loadTunnelStatus(),this.loadHistorySessions())},hideWelcome(){const e=document.getElementById("welcomeOverlay");e&&e.classList.remove("visible");const i=document.getElementById("welcomeQr");i&&(clearTimeout(this._welcomeQrShrinkTimer),i.classList.remove("expanded"))},async _fetchHistorySessions(){const s=(await(await fetch("/api/history/sessions")).json()).sessions||[];if(s.length===0)return[];const n=new Map;for(const f of s){const o=f.projectKey||f.workingDir;n.has(o)||n.set(o,[]),n.get(o).push(f)}const d=[];for(const[,f]of n)d.push(...f.slice(0,3));return d.sort((f,o)=>new Date(o.lastModified)-new Date(f.lastModified)),d},_resolveCaseLabel(e,i){if(!e)return"";let s=null;for(const n of i||[])if(!(!n||!n.path)){if(e===n.path)return`#${n.name}`;if(e.startsWith(n.path+"/")){const d=n.path.length;(!s||d>s.len)&&(s={name:n.name,suffix:e.slice(d),len:d})}}return s?`#${s.name}${s.suffix}`:e.split("/").pop()||e},_shortenHomePath(e){return(e||"").replace(/^\/home\/[^/]+\//,"~/").replace(/^\/Users\/[^/]+\//,"~/")},_buildHistoryItem(e,i){const s=e.sizeBytes<1024?`${e.sizeBytes}B`:e.sizeBytes<1048576?`${(e.sizeBytes/1024).toFixed(0)}K`:`${(e.sizeBytes/1048576).toFixed(1)}M`,n=new Date(e.lastModified),d=n.toLocaleDateString("en",{month:"short",day:"numeric"})+" "+n.toLocaleTimeString("en",{hour:"2-digit",minute:"2-digit",hour12:!1}),f=this._shortenHomePath(e.workingDir),o=this._resolveCaseLabel(e.workingDir,i),r=document.createElement("div");r.className="history-item",r.title=e.workingDir;const t=document.createElement("div");t.className="history-item-main",t.addEventListener("click",()=>this.resumeHistorySession(e.sessionId,e.workingDir));const l=document.createElement("div");l.className="history-item-text";const h=document.createElement("span");h.className="history-item-title",h.textContent=e.firstPrompt||f;const c=document.createElement("span");c.className="history-item-subtitle",o.startsWith("#")&&c.classList.add("is-case"),c.textContent=o,l.append(h,c);const u=document.createElement("span");u.className="history-item-meta",u.textContent=d;const a=document.createElement("button");a.className="history-item-expand",a.type="button",a.setAttribute("aria-label","Show details"),a.setAttribute("aria-expanded","false"),a.textContent="\u22EF",t.append(l,u,a);const m=document.createElement("div");m.className="history-item-detail",m.hidden=!0;const p=document.createElement("div");p.className="history-detail-row";const g=document.createElement("span");g.className="history-detail-label",g.textContent="Prompt";const y=document.createElement("span");y.className="history-detail-value history-detail-prompt",y.textContent=e.firstPrompt||"(no prompt captured)",p.append(g,y);const _=document.createElement("div");_.className="history-detail-row";const v=document.createElement("span");v.className="history-detail-label",v.textContent="Path";const b=document.createElement("span");b.className="history-detail-value history-detail-path",b.textContent=f,_.append(v,b);const S=document.createElement("div");return S.className="history-detail-row history-detail-meta",S.textContent=`${d} \xB7 ${s} \xB7 ${e.sessionId.slice(0,8)}`,m.append(p,_,S),a.addEventListener("click",x=>{x.stopPropagation();const T=r.classList.toggle("expanded");m.hidden=!T,a.setAttribute("aria-expanded",T?"true":"false")}),r.append(t,m),r},_HISTORY_INITIAL_COUNT:4,async loadHistorySessions(){const e=document.getElementById("historySessions"),i=document.getElementById("historyList");if(!(!e||!i))try{const s=Array.isArray(this.cases)&&this.cases.length>0?Promise.resolve(this.cases):fetch("/api/cases").then(o=>o.ok?o.json():[]).catch(()=>[]),[n,d]=await Promise.all([this._fetchHistorySessions(30),s]);if(n.length===0){e.style.display="none";return}i.replaceChildren();const f=this._HISTORY_INITIAL_COUNT;for(let o=0;o<Math.min(f,n.length);o++)i.appendChild(this._buildHistoryItem(n[o],d));if(n.length>f){const o=document.createElement("button");o.className="history-show-more",o.textContent=`Show ${n.length-f} more`,o.addEventListener("click",()=>{for(let r=f;r<n.length;r++)i.insertBefore(this._buildHistoryItem(n[r],d),o);o.remove()}),i.appendChild(o)}e.style.display=""}catch(s){console.error("[loadHistorySessions]",s),e.style.display="none"}},async resumeHistorySession(e,i){document.getElementById("runModeMenu")?.classList.remove("active");try{this.terminal.clear(),this.terminal.writeln(`\x1B[1;32m Resuming conversation ${e.slice(0,8)}...\x1B[0m`);const s=i.split("/").pop()||"session";let n=1;for(const[,c]of this.sessions){const u=c.name&&c.name.match(/^w(\d+)-/);if(u){const a=parseInt(u[1]);a>=n&&(n=a+1)}}const d=`w${n}-${s}`,o=(this.cases||[]).find(c=>c.path===i)?.name||i.split("/").pop()||"",r=this.buildEnvOverrides(this.getCaseSettings(o),this.loadAppSettingsFromStorage()),l=await(await fetch("/api/sessions",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({workingDir:i,name:d,resumeSessionId:e,...Object.keys(r).length>0?{envOverrides:r}:{}})})).json();if(!l.success)throw new Error(l.error);const h=l.session.id;await fetch(`/api/sessions/${h}/interactive`,{method:"POST"}),this.terminal.writeln(`\x1B[90m Session ${d} ready\x1B[0m`),await this.selectSession(h),this.terminal.focus()}catch(s){this.terminal.writeln(`\x1B[1;31m Error: ${s.message}\x1B[0m`)}},isTerminalAtBottom(){if(!this.terminal)return!0;const e=this.terminal.buffer.active;return e.viewportY>=e.baseY-2},batchTerminalWrite(e){if(this._isLoadingBuffer){this._loadBufferQueue&&this._loadBufferQueue.push(e);return}if(this.writeFrameScheduled||(this._wasAtBottomBeforeWrite=this.isTerminalAtBottom()),(this.activeSessionId?this.sessions.get(this.activeSessionId):null)?.flickerFilterEnabled??!1){if(e.includes("\x1B[2J")||e.includes("\x1B[H\x1B[J")||e.includes("\x1B[H")&&e.includes("\x1B[?25l")){this.flickerFilterActive=!0,this.flickerFilterBuffer+=e,this.flickerFilterTimeout&&clearTimeout(this.flickerFilterTimeout),this.flickerFilterTimeout=setTimeout(()=>{this.flickerFilterTimeout=null,this.flushFlickerBuffer()},SYNC_WAIT_TIMEOUT_MS);return}if(this.flickerFilterActive){this.flickerFilterBuffer+=e;return}}this.pendingWrites.push(e),this.writeFrameScheduled||(this.writeFrameScheduled=!0,requestAnimationFrame(()=>{this.flushPendingWrites(),this.writeFrameScheduled=!1}))},flushFlickerBuffer(){this.flickerFilterBuffer&&(this.pendingWrites.push(this.flickerFilterBuffer),this.flickerFilterBuffer="",this.flickerFilterActive=!1,this.writeFrameScheduled||(this.writeFrameScheduled=!0,requestAnimationFrame(()=>{this.flushPendingWrites(),this.writeFrameScheduled=!1})))},_updateLocalEchoState(){const e=this.loadAppSettingsFromStorage(),i=this.activeSessionId?this.sessions.get(this.activeSessionId):null,n=!!((e.localEchoEnabled??MobileDetection.isTouchDevice())&&i);this._localEchoEnabled&&!n&&this._localEchoOverlay?.clear(),this._localEchoEnabled=n,this._localEchoOverlay&&i&&(i.mode==="opencode"?this._localEchoOverlay.setPrompt({type:"custom",offset:3,find:d=>{try{const f=d.buffer.active,o=f.cursorY,r=f.getLine(f.viewportY+o);if(!r)return null;const l=r.translateToString(!0).indexOf("\u2503");return l>=0?{row:o,col:l}:null}catch{return null}}}):i.mode==="shell"?(this._localEchoOverlay.clear(),this._localEchoEnabled=!1):this._localEchoOverlay.setPrompt({type:"character",char:"\u276F",offset:2}))},flushPendingWrites(){if(this.pendingWrites.length===0||!this.terminal)return;const e=performance.now(),i=this.pendingWrites.join("");this.pendingWrites=[];const s=i.length;s>16384&&_crashDiag.log(`FLUSH: ${(s/1024).toFixed(0)}KB`);const n=65536;let d=!1;s<=n?this.terminal.write(i):(this.terminal.write(i.slice(0,n)),this.pendingWrites.push(i.slice(n)),d=!0,this.writeFrameScheduled||(this.writeFrameScheduled=!0,requestAnimationFrame(()=>{this.flushPendingWrites(),this.writeFrameScheduled=!1})));const f=d?n:s,o=performance.now()-e;if((o>100||d)&&console.warn(`[CRASH-DIAG] flushPendingWrites: ${o.toFixed(0)}ms, ${(f/1024).toFixed(0)}KB written${d?", rest deferred":""} (total ${(s/1024).toFixed(0)}KB)`),this._wasAtBottomBeforeWrite&&this.terminal.scrollToBottom(),this._localEchoOverlay?.hasPending&&this._localEchoOverlay.rerender(),this._tabCompletionSessionId&&this._tabCompletionSessionId===this.activeSessionId&&this._localEchoOverlay&&!this._localEchoOverlay.pendingText){const r=this._localEchoOverlay,t=this;this.terminal.write("",()=>{if(!t._tabCompletionSessionId)return;r.resetBufferDetection();const l=r.detectBufferText();l?l===t._tabCompletionBaseText?(r.undoDetection(),t._tabCompletionRetries=(t._tabCompletionRetries||0)+1,t._tabCompletionRetries>60&&(t._tabCompletionSessionId=null,t._tabCompletionRetries=0)):(t._tabCompletionSessionId=null,t._tabCompletionRetries=0,t._tabCompletionBaseText=null,t._tabCompletionFallback&&(clearTimeout(t._tabCompletionFallback),t._tabCompletionFallback=null),r.rerender()):(t._tabCompletionRetries=(t._tabCompletionRetries||0)+1,t._tabCompletionRetries>60&&(t._tabCompletionSessionId=null,t._tabCompletionRetries=0))})}},chunkedTerminalWrite(e,i=TERMINAL_CHUNK_SIZE){const s=++this._chunkedWriteGen;return new Promise(n=>{if(!e||e.length===0){this._finishBufferLoad(),n();return}this._isLoadingBuffer=!0,this._loadBufferQueue=[];const d=e.replace(DEC_SYNC_STRIP_RE,""),f=()=>{this._chunkedWriteGen===s&&this._finishBufferLoad(),n()};if(d.length<=i){this.terminal.write(d),f();return}let o=0;const r=performance.now();let t=0;const l=()=>{if(this._chunkedWriteGen!==s){n();return}if(o>=d.length){const a=performance.now()-r;console.log(`[CRASH-DIAG] chunkedTerminalWrite complete: ${d.length} bytes in ${t} chunks, ${a.toFixed(0)}ms total`),requestAnimationFrame(f);return}const h=performance.now(),c=d.slice(o,o+i);this.terminal.write(c);const u=performance.now()-h;t++,u>50&&console.warn(`[CRASH-DIAG] chunk #${t} write took ${u.toFixed(0)}ms (${c.length} bytes at offset ${o})`),o+=i,requestAnimationFrame(l)};requestAnimationFrame(l)})},_finishBufferLoad(){this._isLoadingBuffer=!1,this._loadBufferQueue=null},clearTerminal(){this.terminal.clear()},async restoreTerminalSize(){if(!this.activeSessionId){this.showToast("No active session","warning");return}const e=this.getTerminalDimensions();if(!e){this.showToast("Could not determine terminal size","error");return}try{await this.sendResize(this.activeSessionId),await fetch(`/api/sessions/${this.activeSessionId}/input`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({input:"\f"})}),this.showToast(`Terminal restored to ${e.cols}x${e.rows}`,"success")}catch(i){console.error("Failed to restore terminal size:",i),this.showToast("Failed to restore terminal size","error")}},sendPendingCtrlL(e){!this.pendingCtrlL||!this.pendingCtrlL.has(e)||(this.pendingCtrlL.delete(e),e===this.activeSessionId&&this.sendResize(e).then(()=>{fetch(`/api/sessions/${e}/input`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({input:"\f"})})}))},async copyTerminal(){try{const e=this.terminal.buffer.active;let i="";for(let s=0;s<e.length;s++){const n=e.getLine(s);n&&(i+=n.translateToString(!0)+`
2
- `)}await navigator.clipboard.writeText(i.replace(/\n+$/,`
3
- `)),this.showToast("Copied to clipboard","success")}catch{this.showToast("Failed to copy","error")}},increaseFontSize(){const e=this.terminal.options.fontSize||14;this.setFontSize(Math.min(e+2,24))},decreaseFontSize(){const e=this.terminal.options.fontSize||14;this.setFontSize(Math.max(e-2,10))},setFontSize(e){this.terminal.options.fontSize=e,document.getElementById("fontSizeDisplay").textContent=e,this.fitAddon.fit(),localStorage.setItem("codeman-font-size",e),this._localEchoOverlay?.refreshFont()},loadFontSize(){const e=localStorage.getItem("codeman-font-size");if(e){const i=parseInt(e,10);i>=10&&i<=24&&(this.terminal.options.fontSize=i,document.getElementById("fontSizeDisplay").textContent=i)}},getTerminalDimensions(){const s=this.fitAddon?.proposeDimensions();return s?{cols:Math.max(s.cols,40),rows:Math.max(s.rows,10)}:null},async sendResize(e){this.fitAddon&&this.fitAddon.fit();const i=this.getTerminalDimensions();if(i){if(this._lastResizeDims={cols:i.cols,rows:i.rows},this._wsReady&&this._wsSessionId===e)try{this._ws.send(JSON.stringify({t:"z",c:i.cols,r:i.rows}));return}catch{}await fetch(`/api/sessions/${e}/resize`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(i)})}},async sendInput(e){this.activeSessionId&&await fetch(`/api/sessions/${this.activeSessionId}/input`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({input:e,useMux:!0})})},toggleDirInput(){const e=document.querySelector("#dirDisplay").parentElement,i=document.getElementById("dirInput");i.classList.contains("hidden")&&(i.classList.remove("hidden"),e.style.display="none",i.focus())},hideDirInput(){const e=document.querySelector("#dirDisplay").parentElement,i=document.getElementById("dirInput");setTimeout(()=>{i.classList.add("hidden"),e.style.display="";const s=i.value.trim();document.getElementById("dirDisplay").textContent=s||"No directory"},100)}});