impactus-swarm 0.0.11 → 0.0.13

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "impactus-swarm",
3
- "version": "0.0.11",
3
+ "version": "0.0.13",
4
4
  "description": "AI-powered terminal swarm orchestration platform",
5
5
  "type": "module",
6
6
  "bin": {
package/server/main.js CHANGED
@@ -40,7 +40,7 @@ set -gx TERM_PROGRAM impactus-swarm
40
40
  `)[0],10);return isNaN(r)?null:r}catch{return null}}static resizeSession(e,t,r){try{O("tmux",["-L",N,"resize-window","-t",e,"-x",String(t),"-y",String(r)],{stdio:"pipe"})}catch{}}static getSessionSize(e){try{let r=O("tmux",["-L",N,"display-message","-t",e,"-p","#{window_width} #{window_height}"],{stdio:"pipe"}).toString().trim().split(" "),s=parseInt(r[0],10),n=parseInt(r[1],10);if(!isNaN(s)&&!isNaN(n))return{cols:s,rows:n}}catch{}return null}static killAllSessions(){let e=this.listSessions();for(let t of e)this.killSession(t)}static killServer(){try{O("tmux",["-L",N,"kill-server"],{stdio:"pipe"})}catch{}}};import{randomUUID as gn}from"node:crypto";var Te=class a{activeBlocks=new Map;lineCounters=new Map;partialBuffers=new Map;static OSC_PROMPT_START=/\x1b\]133;A(?:\x1b\\|\x07)/g;static OSC_COMMAND_EXEC=/\x1b\]133;C(?:\x1b\\|\x07)/g;static OSC_COMMAND_END=/\x1b\]133;D;?(\d*)(?:\x1b\\|\x07)/g;static PARTIAL_OSC=/\x1b(?:\](?:1(?:3(?:3(?:;(?:[A-D](?:;?\d*)?)?)?)?)?)?)?$/;parse(e,t){let r=[],n=(this.partialBuffers.get(t)||"")+e;this.partialBuffers.delete(t);let i=a.PARTIAL_OSC.exec(n),o;i?(this.partialBuffers.set(t,i[0]),o=n.slice(0,i.index)):o=n;let c=(o.match(/\n/g)||[]).length,l=this.lineCounters.get(t)||0,d=[];a.OSC_PROMPT_START.lastIndex=0,a.OSC_COMMAND_EXEC.lastIndex=0,a.OSC_COMMAND_END.lastIndex=0;let u;for(;(u=a.OSC_PROMPT_START.exec(o))!==null;)d.push({type:"A",index:u.index});for(;(u=a.OSC_COMMAND_EXEC.exec(o))!==null;)d.push({type:"C",index:u.index});for(;(u=a.OSC_COMMAND_END.exec(o))!==null;)d.push({type:"D",index:u.index,exitCode:u[1]?parseInt(u[1],10):0});d.sort((p,m)=>p.index-m.index);for(let p of d)switch(p.type){case"A":{let m={id:gn(),terminalId:t,status:"prompting",startTime:new Date().toISOString(),lineStart:l};this.activeBlocks.set(t,m),r.push({type:"block-start",terminalId:t,block:{...m}});break}case"C":{let m=this.activeBlocks.get(t);m&&(m.status="executing",r.push({type:"block-executing",terminalId:t,block:{...m}}));break}case"D":{let m=this.activeBlocks.get(t);m&&(m.status="completed",m.exitCode=p.exitCode,m.endTime=new Date().toISOString(),m.lineEnd=l+c,r.push({type:"block-end",terminalId:t,block:{...m}}),this.activeBlocks.delete(t));break}}return this.lineCounters.set(t,l+c),r}static stripOSC133(e){return e.replace(/\x1b\]133;[A-D];?\d*(?:\x1b\\|\x07)/g,"")}getActiveBlock(e){return this.activeBlocks.get(e)}cleanup(e){this.activeBlocks.delete(e),this.lineCounters.delete(e),this.partialBuffers.delete(e)}};var hn=/\x1b\[[0-9;?]*[a-zA-Z]|\x1b\][^\x07]*(?:\x07|\x1b\\)|\x1b[()][AB012]|\x1b[>=]|\x1b\[[\d;]*m/g;function kr(a){return a.replace(hn,"")}var yr=new Set(["\u280B","\u2819","\u2839","\u2838","\u283C","\u2834","\u2826","\u2827","\u2807","\u280F","\u28FE","\u28FD","\u28FB","\u28BF","\u287F","\u28DF","\u28EF","\u28F7"]),vr=new Set(["\u273B","\u2722","\u26A1","\u25CF","\u25C9","\u25CE","\u25CB","\u25CC","\u23F5"]),fn=/^[\s|/\\\-.*]+$/,wn=/^[\s─━═╌╍┄┅┈┉╴╶╸╺│┃║├┤┬┴┼╋]+$/,En=["\u23F5\u23F5","\u273B ","\u26A1 "];function br(a){let e=kr(a).trim();if(e.length===0)return!0;let t=[...e].filter(r=>r.trim().length>0);if(t.length>0&&t.length<=5&&t.every(s=>yr.has(s)||vr.has(s))||e.length<=10&&fn.test(e)||wn.test(e))return!0;for(let r of En)if(e.startsWith(r))return!0;if(t.length>=1&&t.length<=30){let r=t[0];if(yr.has(r)||vr.has(r))return!0}return!1}function Ir(a){if(a.length===0)return!1;if(a.includes("\r")&&!a.includes(`
41
41
  `))return a.split("\r").filter(s=>s.length>0).every(s=>br(s));let t=a.split(`
42
42
  `).filter(r=>kr(r).trim().length>0);return t.length===0?!0:t.every(r=>br(r))}var Tn=/\x1b\[[0-9;?]*[a-zA-Z]|\x1b\][^\x07]*(?:\x07|\x1b\\)|\x1b[()][AB012]|\x1b[>=]|\x1b\[[\d;]*m/g;function Rr(a){return a.replace(Tn,"")}var Ar=/\/\/|\/\*|^\s*#\s/,Cr=/^[+-][^+-]/,Sn=/\?\s*:|:\s*\?|\?\./,yn=/\w\)\?\s*$|\w\?\s*;?\s*$/,vn=/\?[a-zA-Z_]+=\S/,bn=/^\s+at\s+/,kn=/->|=>|::|!=|==/,In=/^#{1,6}\s/,Lr=/^\s*\d+\s*[|:]\s/,Rn=/\b(?:function|def|fn|func|sub|proc)\s+\w/,Mr=/^(?:\s{4,}|\t)/,Or=/\b(?:import|require|from|use|include)\b.*['"`]/,An=/\([^)]*\)\s*\?\s*$/,Cn=/[+\-*/%]=|<<=|>>=/,Ln=/:\s*\w+\?\s*[;,)=]/;function Nr(a){let e=Rr(a).trim();return!(!e.endsWith("?")||e.length<5||Ar.test(e)||Cr.test(e)||Sn.test(e)||yn.test(e)||vn.test(e)||bn.test(e)||kn.test(e)||In.test(e)||Lr.test(e)||Rn.test(e)||Mr.test(e)||Or.test(e)||An.test(e)||Cn.test(e)||Ln.test(e))}function Dr(a){let e=Rr(a).trim();return Cr.test(e)||Lr.test(e)||Mr.test(e)||Ar.test(e)||Or.test(e)}var Le=class{grid;cursorRow=0;cursorCol=0;rows;cols;savedCursor=null;alternateGrid=null;isAlternate=!1;parseState=0;csiParams="";constructor(e=24,t=80){this.rows=e,this.cols=t,this.grid=this.createGrid(e,t)}createGrid(e,t){let r=[];for(let s=0;s<e;s++)r.push(new Array(t).fill(" "));return r}write(e){for(let t=0;t<e.length;t++){let r=e[t],s=e.charCodeAt(t);switch(this.parseState){case 0:this.handleNormal(r,s);break;case 1:this.handleEscStart(r,s);break;case 2:this.handleCsiParam(r,s);break;case 3:s===7?this.parseState=0:s===27&&t+1<e.length&&e.charCodeAt(t+1)===92&&(t++,this.parseState=0);break}}}handleNormal(e,t){if(t===27){this.parseState=1,this.csiParams="";return}if(t===13){this.cursorCol=0;return}if(t===10){this.lineFeed();return}if(t===8){this.cursorCol>0&&this.cursorCol--;return}if(t===9){this.cursorCol=Math.min(this.cols-1,(Math.floor(this.cursorCol/8)+1)*8);return}t<32||(this.cursorRow>=0&&this.cursorRow<this.rows&&this.cursorCol>=0&&this.cursorCol<this.cols&&(this.grid[this.cursorRow][this.cursorCol]=e),this.cursorCol++,this.cursorCol>=this.cols&&(this.cursorCol=0,this.lineFeed()))}handleEscStart(e,t){if(t===91){this.parseState=2,this.csiParams="";return}if(t===93){this.parseState=3;return}if(t===55){this.savedCursor={row:this.cursorRow,col:this.cursorCol},this.parseState=0;return}if(t===56){this.savedCursor&&(this.cursorRow=this.savedCursor.row,this.cursorCol=this.savedCursor.col),this.parseState=0;return}this.parseState=0}handleCsiParam(e,t){if(t>=48&&t<=63){this.csiParams+=e;return}if(t>=32&&t<=47){this.csiParams+=e;return}if(t>=64&&t<=126){this.executeCsi(e,this.csiParams),this.parseState=0;return}this.parseState=0}executeCsi(e,t){let r=t.replace(/^\?/,"").split(";").map(i=>parseInt(i,10)||0),s=t.startsWith("?"),n=r[0]||1;switch(e){case"A":this.cursorRow=Math.max(0,this.cursorRow-n);break;case"B":this.cursorRow=Math.min(this.rows-1,this.cursorRow+n);break;case"C":this.cursorCol=Math.min(this.cols-1,this.cursorCol+n);break;case"D":this.cursorCol=Math.max(0,this.cursorCol-n);break;case"H":case"f":this.cursorRow=Math.min(this.rows-1,Math.max(0,(r[0]||1)-1)),this.cursorCol=Math.min(this.cols-1,Math.max(0,(r[1]||1)-1));break;case"J":this.eraseDisplay(r[0]||0);break;case"K":this.eraseLine(r[0]||0);break;case"m":break;case"s":this.savedCursor={row:this.cursorRow,col:this.cursorCol};break;case"u":this.savedCursor&&(this.cursorRow=this.savedCursor.row,this.cursorCol=this.savedCursor.col);break;case"h":s&&(r[0]===1049||r[0]===47)&&this.enterAlternateScreen();break;case"l":s&&(r[0]===1049||r[0]===47)&&this.leaveAlternateScreen();break;case"r":break;case"G":this.cursorCol=Math.min(this.cols-1,Math.max(0,n-1));break;case"d":this.cursorRow=Math.min(this.rows-1,Math.max(0,n-1));break;case"E":this.cursorRow=Math.min(this.rows-1,this.cursorRow+n),this.cursorCol=0;break;case"F":this.cursorRow=Math.max(0,this.cursorRow-n),this.cursorCol=0;break;default:break}}lineFeed(){this.cursorRow<this.rows-1?this.cursorRow++:(this.grid.shift(),this.grid.push(new Array(this.cols).fill(" ")))}eraseDisplay(e){switch(e){case 0:this.eraseLine(0);for(let t=this.cursorRow+1;t<this.rows;t++)this.grid[t].fill(" ");break;case 1:this.eraseLine(1);for(let t=0;t<this.cursorRow;t++)this.grid[t].fill(" ");break;case 2:case 3:for(let t=0;t<this.rows;t++)this.grid[t].fill(" ");this.cursorRow=0,this.cursorCol=0;break}}eraseLine(e){if(this.cursorRow<0||this.cursorRow>=this.rows)return;let t=this.grid[this.cursorRow];switch(e){case 0:for(let r=this.cursorCol;r<this.cols;r++)t[r]=" ";break;case 1:for(let r=0;r<=this.cursorCol&&r<this.cols;r++)t[r]=" ";break;case 2:t.fill(" ");break}}enterAlternateScreen(){this.isAlternate||(this.alternateGrid=this.grid,this.grid=this.createGrid(this.rows,this.cols),this.isAlternate=!0,this.savedCursor={row:this.cursorRow,col:this.cursorCol},this.cursorRow=0,this.cursorCol=0)}leaveAlternateScreen(){this.isAlternate&&this.alternateGrid&&(this.grid=this.alternateGrid,this.alternateGrid=null,this.isAlternate=!1,this.savedCursor&&(this.cursorRow=this.savedCursor.row,this.cursorCol=this.savedCursor.col,this.savedCursor=null))}getBottomRows(e){let t=[];for(let r=this.rows-1;r>=0&&t.length<e;r--){let s=this.grid[r].join("").trimEnd();s.length>0&&t.unshift(s)}return t}getScreen(){return this.grid.map(e=>e.join("").trimEnd())}resize(e,t){let r=this.createGrid(e,t),s=Math.min(this.rows,e),n=Math.min(this.cols,t);for(let i=0;i<s;i++)for(let o=0;o<n;o++)r[i][o]=this.grid[i][o];this.grid=r,this.rows=e,this.cols=t,this.cursorRow=Math.min(this.cursorRow,e-1),this.cursorCol=Math.min(this.cursorCol,t-1)}containsInBottom(e,t){return this.getBottomRows(t).join(`
43
- `).includes(e)}};var Me=class a{constructor(e,t){this.terminalId=e;this.onStateChange=t}state="idle";lastRealOutputAt=0;idleCheckHandle=null;debouncedIdleHandle=null;static IDLE_THRESHOLD_MS=500;static DEBOUNCE_MS=2e3;onRealOutput(){this.lastRealOutputAt=Date.now(),this.debouncedIdleHandle!==null&&(clearTimeout(this.debouncedIdleHandle),this.debouncedIdleHandle=null),this.state==="idle"&&(this.state="busy",this.onStateChange({terminalId:this.terminalId,state:"busy"})),this.idleCheckHandle!==null&&clearTimeout(this.idleCheckHandle),this.idleCheckHandle=setTimeout(()=>this.checkIdle(),a.IDLE_THRESHOLD_MS)}onChromeOutput(){}getState(){return this.state}dispose(){this.idleCheckHandle!==null&&(clearTimeout(this.idleCheckHandle),this.idleCheckHandle=null),this.debouncedIdleHandle!==null&&(clearTimeout(this.debouncedIdleHandle),this.debouncedIdleHandle=null)}checkIdle(){this.idleCheckHandle=null,Date.now()-this.lastRealOutputAt>=a.IDLE_THRESHOLD_MS&&this.state==="busy"&&(this.debouncedIdleHandle=setTimeout(()=>{this.debouncedIdleHandle=null,Date.now()-this.lastRealOutputAt>=a.IDLE_THRESHOLD_MS&&(this.state="idle",this.onStateChange({terminalId:this.terminalId,state:"idle"}))},a.DEBOUNCE_MS))}};var Oe=class a{escapeBuffer="";static MAX_ESCAPE_BUFFER=256;process(e){let t=this.escapeBuffer+e;this.escapeBuffer="";let r=this.findIncompleteEscape(t);if(r>=0){if(this.escapeBuffer=t.slice(r),this.escapeBuffer.length>a.MAX_ESCAPE_BUFFER){let s=this.escapeBuffer;return this.escapeBuffer="",t.slice(0,r)+s}return t.slice(0,r)}return t}flush(){let e=this.escapeBuffer;return this.escapeBuffer="",e}hasPending(){return this.escapeBuffer.length>0}findIncompleteEscape(e){let t=e.length;if(t===0)return-1;if(e.charCodeAt(t-1)===27)return t-1;let r=Math.max(0,t-a.MAX_ESCAPE_BUFFER),s=-1;for(let o=t-1;o>=r;o--)if(e.charCodeAt(o)===27){s=o;break}if(s<0)return-1;let n=e.slice(s);if(n.length<2)return s;let i=n.charCodeAt(1);if(i===91){for(let o=2;o<n.length;o++){let c=n.charCodeAt(o);if(c>=64&&c<=126)return-1}return s}if(i===93){for(let o=2;o<n.length;o++)if(n.charCodeAt(o)===7||n.charCodeAt(o)===27&&o+1<n.length&&n.charCodeAt(o+1)===92)return-1;return s}if(i===80){for(let o=2;o<n.length;o++)if(n.charCodeAt(o)===27&&o+1<n.length&&n.charCodeAt(o+1)===92)return-1;return s}return-1}};var Mn=[/(?:rate.?limit|too many requests|429|throttl)/i,/(?:overloaded|capacity|RESOURCE_EXHAUSTED)/i,/(?:quota.?exceeded|billing|usage.?limit)/i,/(?:retry.?after|backoff|slow.?down)/i,/(?:requests? per (?:minute|second|hour)|RPM|RPS)/i],On=/(?:retry.?after|wait|try again in)\s*[:=]?\s*(\d+)\s*(s(?:ec)?|m(?:in)?|h(?:our)?)?/i;function Dt(a){for(let e of Mn)if(e.test(a)){let t=On.exec(a),r;if(t){let s=parseInt(t[1],10),n=t[2]?.toLowerCase()||"s";n.startsWith("m")?r=s*60:n.startsWith("h")?r=s*3600:r=s}return{type:"rate-limit",data:{message:a.trim().slice(0,200),retryAfterSeconds:r}}}return null}var Nn=[/(?:HTTP\s+)?(?:4\d{2}|5\d{2})\b/,/(?:ECONNREFUSED|ENOTFOUND|ETIMEDOUT|ECONNRESET)/,/(?:authentication failed|unauthorized|forbidden|invalid.?(?:api.?)?key)/i,/(?:internal server error|bad gateway|service unavailable|gateway timeout)/i,/(?:APIError|ApiError|API error)/],Dn=/(?:HTTP\s+)?([45]\d{2})/;function Pt(a){for(let e of Nn)if(e.test(a)){let t=Dn.exec(a);return{type:"api-error",data:{message:a.trim().slice(0,300),statusCode:t?parseInt(t[1],10):void 0}}}return null}var Pn=/https?:\/\/(?:github\.com|gitlab\.com|bitbucket\.org)\/[^\s]+\/pull\/(\d+)/gi,xn=/https?:\/\/gitlab\.com\/[^\s]+\/merge_requests\/(\d+)/gi;function xt(a){let e=[];for(let t of[Pn,xn]){t.lastIndex=0;let r;for(;(r=t.exec(a))!==null;)e.push(r[0])}return e.length===0?null:{type:"pr-url",data:{urls:e}}}var _n=/(\d{1,3}(?:\.\d+)?)\s*%/,Un=/(\d+)\s*\/\s*(\d+)/,Fn=/\[([█▓▒░=\->#\s]{4,})\]/;function _t(a){let e=_n.exec(a);if(e){let r=parseFloat(e[1]);if(r>=0&&r<=100)return{type:"progress",data:{percent:r,raw:a.trim().slice(0,100)}}}let t=Un.exec(a);if(t){let r=parseInt(t[1],10),s=parseInt(t[2],10);if(s>0&&r<=s&&s<=1e4)return{type:"progress",data:{percent:r/s*100,current:r,total:s,raw:a.trim().slice(0,100)}}}return Fn.test(a)?{type:"progress",data:{raw:a.trim().slice(0,100)}}:null}var Bn=/(\d+)\s+(?:tool|sub.?(?:task|agent))s?\s+running/i,Pr=/teammate\s+\w+\s+\((?:working|running|busy)\)/gi,Wn=/(?:task|step|agent)\s+(\d+)\s*(?:\/|of)\s*(\d+)/i;function Ut(a){let e=Bn.exec(a);if(e)return{type:"subtask-count",data:{active:parseInt(e[1],10)}};Pr.lastIndex=0;let t=[],r;for(;(r=Pr.exec(a))!==null;)t.push(r[0]);if(t.length>0)return{type:"subtask-count",data:{active:t.length,teammates:t}};let s=Wn.exec(a);return s?{type:"subtask-count",data:{current:parseInt(s[1],10),total:parseInt(s[2],10)}}:null}var $n=[Dt,Pt,xt,_t,Ut];function xr(a){let e=[];for(let t of $n){let r=t(a);r&&e.push(r)}return e}var Hn=/\x1b\[[0-9;?]*[a-zA-Z]|\x1b\][^\x07]*(?:\x07|\x1b\\)|\x1b[()][AB012]|\x1b[>=]|\x1b\[[\d;]*m/g;function qn(a){return a.replace(Hn,"")}var Xn=[/\[Y\/n\]/i,/\[y\/N\]/i,/\[a\/y\/n\]/i,/\(yes\/no\)/i,/\(y\/n\)/i,/Approve\?/i,/Allow\s/i,/Do you want/i,/Press Enter/i,/Enter to select/i,/Select\s/i,/Choose\s/i,/\bconfirm\b/i,/\bproceed\b.*\?/i,/\bcontinue\b.*\?/i,/\boverwrite\b.*\?/i,/\breplace\b.*\?/i,/\bdelete\b.*\?/i,/\bremove\b.*\?/i],jn=1e3,Gn=1e4,Kn=10,Yn=500,Vn=1e3,_r=5e3,Ur=5,Ne=class{terminals=new Map;pushHandler;constructor(e){this.pushHandler=e}initTerminal(e,t=24,r=80){this.cleanupTerminal(e);let s={vtBuffer:new Le(t,r),shellState:new Me(e,n=>{this.pushHandler("terminal.shell-state",n)}),bufferAssembler:new Oe,silenceTimerHandle:setInterval(()=>this.checkSilence(e),jn),lastRealOutputAt:0,pendingQuestionLine:null,pendingQuestionAt:0,questionEmitted:!1,chunksAfterQuestion:0,lastUserInputAt:0,lastResizeAt:0,startedAt:Date.now()};this.terminals.set(e,s)}processOutput(e,t){let r=this.terminals.get(e);if(!r)return t;let s=r.bufferAssembler.process(t);if(s.length===0)return"";r.vtBuffer.write(s),Ir(s)?r.shellState.onChromeOutput():(r.lastRealOutputAt=Date.now(),r.shellState.onRealOutput(),r.pendingQuestionLine!==null&&(r.chunksAfterQuestion++,r.chunksAfterQuestion>=Kn&&(r.pendingQuestionLine=null,r.chunksAfterQuestion=0)),r.questionEmitted&&(r.questionEmitted=!1,this.pushHandler("terminal.awaiting-input-cleared",{terminalId:e})));let i=qn(s);return this.checkInstantQuestions(e,i,r),this.detectQuestionCandidates(i,r),this.runPatternDetection(e,i),s}onUserInput(e){let t=this.terminals.get(e);t&&(t.lastUserInputAt=Date.now())}onResize(e,t,r){let s=this.terminals.get(e);s&&(s.lastResizeAt=Date.now(),s.vtBuffer.resize(t,r))}cleanupTerminal(e){let t=this.terminals.get(e);if(t){clearInterval(t.silenceTimerHandle),t.shellState.dispose();let r=t.bufferAssembler.flush();r.length>0&&t.vtBuffer.write(r),this.terminals.delete(e)}}dispose(){for(let[e]of this.terminals)this.cleanupTerminal(e)}checkInstantQuestions(e,t,r){if(!(Date.now()-r.startedAt<_r))for(let s of t.split(`
43
+ `).includes(e)}};var Me=class a{constructor(e,t){this.terminalId=e;this.onStateChange=t;this.startedAt=Date.now()}state="idle";lastRealOutputAt=0;idleCheckHandle=null;debouncedIdleHandle=null;startedAt;static IDLE_THRESHOLD_MS=500;static DEBOUNCE_MS=2e3;static STARTUP_GRACE_MS=1e4;inStartupGrace(){return Date.now()-this.startedAt<a.STARTUP_GRACE_MS}onRealOutput(){this.lastRealOutputAt=Date.now(),this.debouncedIdleHandle!==null&&(clearTimeout(this.debouncedIdleHandle),this.debouncedIdleHandle=null),this.state==="idle"&&(this.state="busy",this.inStartupGrace()||this.onStateChange({terminalId:this.terminalId,state:"busy"})),this.idleCheckHandle!==null&&clearTimeout(this.idleCheckHandle),this.idleCheckHandle=setTimeout(()=>this.checkIdle(),a.IDLE_THRESHOLD_MS)}onChromeOutput(){}getState(){return this.state}dispose(){this.idleCheckHandle!==null&&(clearTimeout(this.idleCheckHandle),this.idleCheckHandle=null),this.debouncedIdleHandle!==null&&(clearTimeout(this.debouncedIdleHandle),this.debouncedIdleHandle=null)}checkIdle(){this.idleCheckHandle=null,Date.now()-this.lastRealOutputAt>=a.IDLE_THRESHOLD_MS&&this.state==="busy"&&(this.debouncedIdleHandle=setTimeout(()=>{this.debouncedIdleHandle=null,Date.now()-this.lastRealOutputAt>=a.IDLE_THRESHOLD_MS&&(this.state="idle",this.inStartupGrace()||this.onStateChange({terminalId:this.terminalId,state:"idle"}))},a.DEBOUNCE_MS))}};var Oe=class a{escapeBuffer="";static MAX_ESCAPE_BUFFER=256;process(e){let t=this.escapeBuffer+e;this.escapeBuffer="";let r=this.findIncompleteEscape(t);if(r>=0){if(this.escapeBuffer=t.slice(r),this.escapeBuffer.length>a.MAX_ESCAPE_BUFFER){let s=this.escapeBuffer;return this.escapeBuffer="",t.slice(0,r)+s}return t.slice(0,r)}return t}flush(){let e=this.escapeBuffer;return this.escapeBuffer="",e}hasPending(){return this.escapeBuffer.length>0}findIncompleteEscape(e){let t=e.length;if(t===0)return-1;if(e.charCodeAt(t-1)===27)return t-1;let r=Math.max(0,t-a.MAX_ESCAPE_BUFFER),s=-1;for(let o=t-1;o>=r;o--)if(e.charCodeAt(o)===27){s=o;break}if(s<0)return-1;let n=e.slice(s);if(n.length<2)return s;let i=n.charCodeAt(1);if(i===91){for(let o=2;o<n.length;o++){let c=n.charCodeAt(o);if(c>=64&&c<=126)return-1}return s}if(i===93){for(let o=2;o<n.length;o++)if(n.charCodeAt(o)===7||n.charCodeAt(o)===27&&o+1<n.length&&n.charCodeAt(o+1)===92)return-1;return s}if(i===80){for(let o=2;o<n.length;o++)if(n.charCodeAt(o)===27&&o+1<n.length&&n.charCodeAt(o+1)===92)return-1;return s}return-1}};var Mn=[/(?:rate.?limit|too many requests|429|throttl)/i,/(?:overloaded|capacity|RESOURCE_EXHAUSTED)/i,/(?:quota.?exceeded|billing|usage.?limit)/i,/(?:retry.?after|backoff|slow.?down)/i,/(?:requests? per (?:minute|second|hour)|RPM|RPS)/i],On=/(?:retry.?after|wait|try again in)\s*[:=]?\s*(\d+)\s*(s(?:ec)?|m(?:in)?|h(?:our)?)?/i;function Dt(a){for(let e of Mn)if(e.test(a)){let t=On.exec(a),r;if(t){let s=parseInt(t[1],10),n=t[2]?.toLowerCase()||"s";n.startsWith("m")?r=s*60:n.startsWith("h")?r=s*3600:r=s}return{type:"rate-limit",data:{message:a.trim().slice(0,200),retryAfterSeconds:r}}}return null}var Nn=[/(?:HTTP\s+)?(?:4\d{2}|5\d{2})\b/,/(?:ECONNREFUSED|ENOTFOUND|ETIMEDOUT|ECONNRESET)/,/(?:authentication failed|unauthorized|forbidden|invalid.?(?:api.?)?key)/i,/(?:internal server error|bad gateway|service unavailable|gateway timeout)/i,/(?:APIError|ApiError|API error)/],Dn=/(?:HTTP\s+)?([45]\d{2})/;function Pt(a){for(let e of Nn)if(e.test(a)){let t=Dn.exec(a);return{type:"api-error",data:{message:a.trim().slice(0,300),statusCode:t?parseInt(t[1],10):void 0}}}return null}var Pn=/https?:\/\/(?:github\.com|gitlab\.com|bitbucket\.org)\/[^\s]+\/pull\/(\d+)/gi,xn=/https?:\/\/gitlab\.com\/[^\s]+\/merge_requests\/(\d+)/gi;function xt(a){let e=[];for(let t of[Pn,xn]){t.lastIndex=0;let r;for(;(r=t.exec(a))!==null;)e.push(r[0])}return e.length===0?null:{type:"pr-url",data:{urls:e}}}var _n=/(\d{1,3}(?:\.\d+)?)\s*%/,Un=/(\d+)\s*\/\s*(\d+)/,Fn=/\[([█▓▒░=\->#\s]{4,})\]/;function _t(a){let e=_n.exec(a);if(e){let r=parseFloat(e[1]);if(r>=0&&r<=100)return{type:"progress",data:{percent:r,raw:a.trim().slice(0,100)}}}let t=Un.exec(a);if(t){let r=parseInt(t[1],10),s=parseInt(t[2],10);if(s>0&&r<=s&&s<=1e4)return{type:"progress",data:{percent:r/s*100,current:r,total:s,raw:a.trim().slice(0,100)}}}return Fn.test(a)?{type:"progress",data:{raw:a.trim().slice(0,100)}}:null}var Bn=/(\d+)\s+(?:tool|sub.?(?:task|agent))s?\s+running/i,Pr=/teammate\s+\w+\s+\((?:working|running|busy)\)/gi,Wn=/(?:task|step|agent)\s+(\d+)\s*(?:\/|of)\s*(\d+)/i;function Ut(a){let e=Bn.exec(a);if(e)return{type:"subtask-count",data:{active:parseInt(e[1],10)}};Pr.lastIndex=0;let t=[],r;for(;(r=Pr.exec(a))!==null;)t.push(r[0]);if(t.length>0)return{type:"subtask-count",data:{active:t.length,teammates:t}};let s=Wn.exec(a);return s?{type:"subtask-count",data:{current:parseInt(s[1],10),total:parseInt(s[2],10)}}:null}var $n=[Dt,Pt,xt,_t,Ut];function xr(a){let e=[];for(let t of $n){let r=t(a);r&&e.push(r)}return e}var Hn=/\x1b\[[0-9;?]*[a-zA-Z]|\x1b\][^\x07]*(?:\x07|\x1b\\)|\x1b[()][AB012]|\x1b[>=]|\x1b\[[\d;]*m/g;function qn(a){return a.replace(Hn,"")}var Xn=[/\[Y\/n\]/i,/\[y\/N\]/i,/\[a\/y\/n\]/i,/\(yes\/no\)/i,/\(y\/n\)/i,/Approve\?/i,/Allow\s/i,/Do you want/i,/Press Enter/i,/Enter to select/i,/Select\s/i,/Choose\s/i,/\bconfirm\b/i,/\bproceed\b.*\?/i,/\bcontinue\b.*\?/i,/\boverwrite\b.*\?/i,/\breplace\b.*\?/i,/\bdelete\b.*\?/i,/\bremove\b.*\?/i],jn=1e3,Gn=1e4,Kn=10,Yn=500,Vn=1e3,_r=5e3,Ur=5,Ne=class{terminals=new Map;pushHandler;constructor(e){this.pushHandler=e}initTerminal(e,t=24,r=80){this.cleanupTerminal(e);let s={vtBuffer:new Le(t,r),shellState:new Me(e,n=>{this.pushHandler("terminal.shell-state",n)}),bufferAssembler:new Oe,silenceTimerHandle:setInterval(()=>this.checkSilence(e),jn),lastRealOutputAt:0,pendingQuestionLine:null,pendingQuestionAt:0,questionEmitted:!1,chunksAfterQuestion:0,lastUserInputAt:0,lastResizeAt:0,startedAt:Date.now()};this.terminals.set(e,s)}processOutput(e,t){let r=this.terminals.get(e);if(!r)return t;let s=r.bufferAssembler.process(t);if(s.length===0)return"";r.vtBuffer.write(s),Ir(s)?r.shellState.onChromeOutput():(r.lastRealOutputAt=Date.now(),r.shellState.onRealOutput(),r.pendingQuestionLine!==null&&(r.chunksAfterQuestion++,r.chunksAfterQuestion>=Kn&&(r.pendingQuestionLine=null,r.chunksAfterQuestion=0)),r.questionEmitted&&(r.questionEmitted=!1,this.pushHandler("terminal.awaiting-input-cleared",{terminalId:e})));let i=qn(s);return this.checkInstantQuestions(e,i,r),this.detectQuestionCandidates(i,r),this.runPatternDetection(e,i),s}onUserInput(e){let t=this.terminals.get(e);t&&(t.lastUserInputAt=Date.now())}onResize(e,t,r){let s=this.terminals.get(e);s&&(s.lastResizeAt=Date.now(),s.vtBuffer.resize(t,r))}cleanupTerminal(e){let t=this.terminals.get(e);if(t){clearInterval(t.silenceTimerHandle),t.shellState.dispose();let r=t.bufferAssembler.flush();r.length>0&&t.vtBuffer.write(r),this.terminals.delete(e)}}dispose(){for(let[e]of this.terminals)this.cleanupTerminal(e)}checkInstantQuestions(e,t,r){if(!(Date.now()-r.startedAt<_r))for(let s of t.split(`
44
44
  `)){let n=s.trim();if(n.length!==0&&!Dr(s)){for(let i of Xn)if(i.test(n)){r.questionEmitted=!0,r.pendingQuestionLine=null,this.pushHandler("terminal.awaiting-input",{terminalId:e,type:"question",confident:!0,promptText:n.slice(0,200)});return}}}}detectQuestionCandidates(e,t){let r=e.split(`
45
45
  `);for(let s=r.length-1;s>=0;s--){let n=r[s].trim();if(n.endsWith("?")&&n.length>=5){t.pendingQuestionLine=n,t.pendingQuestionAt=Date.now(),t.chunksAfterQuestion=0;return}}}checkSilence(e){let t=this.terminals.get(e);if(!t)return;let r=Date.now();if(r-t.startedAt<_r||r-t.lastResizeAt<Vn||r-t.lastUserInputAt<Yn||t.questionEmitted||t.pendingQuestionLine===null||r-t.lastRealOutputAt<Gn||t.lastRealOutputAt===0)return;let n=t.pendingQuestionLine;if(!Nr(n)){t.pendingQuestionLine=null;return}if(!t.vtBuffer.containsInBottom(n,Ur)){let o=t.vtBuffer.getBottomRows(Ur).join(" "),c=n.split(/\s+/).filter(d=>d.length>3);if(c.filter(d=>o.includes(d)).length<c.length*.5){t.pendingQuestionLine=null;return}}t.questionEmitted=!0,t.pendingQuestionLine=null,this.pushHandler("terminal.awaiting-input",{terminalId:e,type:"question",confident:!1,promptText:n.slice(0,200)})}runPatternDetection(e,t){let r=t.split(`
46
46
  `);for(let s of r){let n=s.trim();if(n.length===0)continue;let i=xr(n);for(let o of i)if(this.pushHandler("terminal.output-event",{terminalId:e,type:o.type,data:o.data}),o.type==="api-error"){let c=this.terminals.get(e);c&&!c.questionEmitted&&(c.questionEmitted=!0,this.pushHandler("terminal.awaiting-input",{terminalId:e,type:"error",confident:!0,promptText:o.data.message||"API Error"}))}}}};var Jn=process.platform==="win32",Ft=new Set(["zsh","bash","sh","fish","dash","ksh","tcsh","csh","ps","grep","rg","cmd.exe","powershell.exe","pwsh.exe","conhost.exe","cmd","powershell","pwsh","conhost"]),Pe=class a{terminals=new Map;pushHandler;commandBlockParser=new Te;outputEventParser;processDetectionInterval;useTmux;constructor(e){this.pushHandler=e,this.outputEventParser=new Ne(e),this.useTmux=R.isAvailable(),this.useTmux?console.log("[terminal] tmux available \u2014 persistent sessions enabled"):console.log("[terminal] tmux not available \u2014 sessions will not persist across restarts"),this.processDetectionInterval=setInterval(()=>{this.detectProcesses()},3e3)}detectProcesses(){for(let[,e]of this.terminals){let t=this.detectProcessForTerminal(e),r=e.lastProcessInfo;(!r||r.foregroundProcess!==t.foregroundProcess||r.isIdle!==t.isIdle)&&(e.lastProcessInfo=t,this.pushHandler("terminal.process",{id:e.id,process:t}))}}getEffectivePid(e){if(e.tmuxSession){let t=R.getShellPid(e.tmuxSession);if(t)return t}return e.pty.pid}detectProcessForTerminal(e){let t=this.getEffectivePid(e),r;try{if(Jn){let n=De(`wmic process where (ParentProcessId=${t}) get Name /format:list`,{stdio:"pipe",timeout:2e3}).toString().trim().split(`