nex-code 0.4.6 → 0.4.8
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/README.md +1 -0
- package/dist/nex-code.js +81 -75
- package/package.json +1 -1
package/dist/nex-code.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
var H=(t,e)=>()=>(e||t((e={exports:{}}).exports,e),e.exports);var Nn=H((wk,Om)=>{Om.exports={name:"nex-code",version:"0.4.
|
|
2
|
+
var H=(t,e)=>()=>(e||t((e={exports:{}}).exports,e),e.exports);var Nn=H((wk,Om)=>{Om.exports={name:"nex-code",version:"0.4.8",description:"The open-source agentic coding CLI. Free with Ollama Cloud \u2014 switch to OpenAI, Anthropic or Gemini anytime. Alternative to Claude Code & Gemini CLI.",bin:{"nex-code":"./dist/nex-code.js"},files:["dist/","README.md","LICENSE"],engines:{node:">=18.0.0"},scripts:{start:"node dist/nex-code.js",build:"esbuild bin/nex-code.js --bundle --platform=node --target=node18 --outfile=dist/nex-code.js --minify --external:axios --external:dotenv --external:playwright",test:"jest --forceExit","test:orchestrator":"jest tests/orchestrator.test.js --forceExit",coverage:"jest --coverage --forceExit","test:watch":"jest --watch",format:"prettier --write .","install-hooks":"ln -sf ../../hooks/pre-push .git/hooks/pre-push && chmod +x .git/hooks/pre-push && ln -sf ../../hooks/post-merge .git/hooks/post-merge && chmod +x .git/hooks/post-merge && echo 'Hooks installed (pre-push, post-merge).'",prepublishOnly:"npm run build && npm test","merge-to-main":"bash scripts/merge-to-main.sh",release:"npm version patch && git push --follow-tags && npm publish"},keywords:["ai","cli","coding","agent","ollama","ollama-cloud","openai","anthropic","claude","gemini","llm","gpt","agentic","terminal","coding-assistant","claude-code-alternative","gemini-cli-alternative","open-source","free","qwen3","devstral","kimi-k2","deepseek","local-llm","mcp","model-context-protocol","multi-provider"],repository:{type:"git",url:"https://github.com/hybridpicker/nex-code.git"},bugs:{url:"https://github.com/hybridpicker/nex-code/issues"},homepage:"https://github.com/hybridpicker/nex-code#readme",license:"MIT",dependencies:{axios:"^1.7.0",dotenv:"^16.4.0"},devDependencies:{esbuild:"^0.27.3",jest:"^29.7.0",prettier:"^3.8.1"},jest:{coverageThreshold:{global:{lines:45,functions:30,branches:35},"./cli/sub-agent.js":{lines:70,functions:60,branches:55}}}}});var fn=H((bk,Ec)=>{"use strict";var bc="\x1B[0m",_c="\x1B[1m",Mn="\x1B[2m";function q(t,e,n){return`\x1B[38;2;${t};${e};${n}m`}function Nm(){if(!process.stdout.isTTY)return null;try{let{execFileSync:t}=require("child_process"),e=["import sys,os,tty,termios,select","f=open('/dev/tty','r+b',buffering=0)","fd=f.fileno()","s=termios.tcgetattr(fd)","try:"," tty.setraw(fd)"," f.write(bytes([0x1b,0x5d,0x31,0x31,0x3b,0x3f,0x1b,0x5c]))"," r=select.select([fd],[],[],0.1)[0]"," d=b''"," if r:"," while True:"," r2=select.select([fd],[],[],0.05)[0]"," if not r2:break"," c=os.read(fd,1)"," d+=c"," if d[-1:]==bytes([0x07]) or d[-2:]==bytes([0x1b,0x5c]):break"," sys.stdout.buffer.write(d)","finally:"," termios.tcsetattr(fd,termios.TCSADRAIN,s)"," f.close()"].join(`
|
|
3
3
|
`),s=t("python3",["-c",e],{encoding:"buffer",timeout:400,stdio:["ignore","pipe","ignore"]}).toString("utf8").match(/rgb:([0-9a-fA-F]+)\/([0-9a-fA-F]+)\/([0-9a-fA-F]+)/);if(s){let r=parseInt(s[1].slice(0,2),16),i=parseInt(s[2].slice(0,2),16),a=parseInt(s[3].slice(0,2),16);return .299*r+.587*i+.114*a<128}}catch{}return null}function xc(){let t=require("os");return require("path").join(t.homedir(),".nex-code",".theme_cache.json")}function Mm(t){try{let n=require("fs").readFileSync(xc(),"utf8"),o=JSON.parse(n);if(o&&typeof o[t]=="boolean")return o[t]}catch{}return null}function Pm(t,e){try{let n=require("fs"),o=require("path"),s=xc(),r=o.dirname(s),i={};try{i=JSON.parse(n.readFileSync(s,"utf8"))}catch{}i[t]=e;let a=Object.keys(i);a.length>50&&a.slice(0,a.length-50).forEach(l=>delete i[l]),n.existsSync(r)||n.mkdirSync(r,{recursive:!0}),n.writeFileSync(s,JSON.stringify(i),"utf8")}catch{}}function Im(){let t=(process.env.NEX_THEME||"").toLowerCase();if(t==="light")return!1;if(t==="dark")return!0;let e=process.env.COLORFGBG;if(e){let i=e.split(";"),a=parseInt(i[i.length-1],10);if(!isNaN(a))return a<8}let n=process.env.TERM_SESSION_ID||"default",o=Mm(n);if(o!==null)return o;let s=Nm(),r=s!==null?s:!0;return Pm(n,r),r}var kc=Im(),vc={reset:bc,bold:_c,dim:Mn,primary:q(80,190,255),secondary:q(60,170,190),success:q(80,210,120),warning:q(245,175,50),error:q(230,80,80),muted:Mn,subtle:q(130,130,145),tool_read:q(80,190,255),tool_write:q(245,165,55),tool_exec:q(185,100,235),tool_search:q(70,185,190),tool_git:q(90,210,100),tool_web:q(100,215,250),tool_sysadmin:q(225,150,75),tool_default:q(100,205,115),syn_keyword:q(185,100,235),syn_string:q(90,210,120),syn_number:q(245,175,50),syn_comment:Mn,syn_key:q(80,190,255),diff_add:q(80,210,120),diff_rem:q(230,80,80),banner_logo:q(80,200,255),banner_name:q(80,200,255),banner_version:Mn,banner_model:Mn,banner_yolo:q(245,175,50),footer_sep:Mn,footer_model:q(80,175,235),footer_branch:q(80,210,100),footer_project:q(130,130,145),footer_divider:q(80,80,95),footer_mode:q(210,150,50),white:"\x1B[37m",red:"\x1B[31m",green:"\x1B[32m",yellow:"\x1B[33m",blue:"\x1B[34m",magenta:"\x1B[35m",cyan:"\x1B[36m",gray:"\x1B[90m",bgRed:"\x1B[41m",bgGreen:"\x1B[42m",diff_add_bg:"\x1B[48;2;10;46;20m",diff_rem_bg:"\x1B[48;2;58;16;16m",brightCyan:"\x1B[96m",brightMagenta:"\x1B[95m",brightBlue:"\x1B[94m"},Sc={reset:bc,bold:_c,dim:q(110,110,120),primary:q(0,110,190),secondary:q(0,125,148),success:q(0,148,62),warning:q(168,92,0),error:q(188,32,32),muted:q(110,110,120),subtle:q(155,155,165),tool_read:q(0,110,190),tool_write:q(168,92,0),tool_exec:q(128,42,188),tool_search:q(0,122,148),tool_git:q(0,138,62),tool_web:q(0,112,178),tool_sysadmin:q(168,82,0),tool_default:q(0,138,62),syn_keyword:q(128,42,188),syn_string:q(0,138,62),syn_number:q(168,92,0),syn_comment:q(135,135,148),syn_key:q(0,110,190),diff_add:q(0,148,62),diff_rem:q(188,32,32),banner_logo:q(0,122,205),banner_name:q(0,122,205),banner_version:q(100,100,118),banner_model:q(100,100,118),banner_yolo:q(168,62,0),footer_sep:q(168,168,178),footer_model:q(0,102,175),footer_branch:q(0,138,62),footer_project:q(135,135,148),footer_divider:q(168,168,178),footer_mode:q(148,88,0),white:q(40,40,52),red:q(188,32,32),green:q(0,148,62),yellow:q(168,92,0),blue:q(0,110,190),magenta:q(128,42,188),cyan:q(0,125,148),gray:q(132,132,142),bgRed:"\x1B[41m",bgGreen:"\x1B[42m",diff_add_bg:"\x1B[48;2;215;245;220m",diff_rem_bg:"\x1B[48;2;255;215;215m",brightCyan:q(0,158,182),brightMagenta:q(158,52,208),brightBlue:q(0,112,208)},Lm=kc?vc:Sc;Ec.exports={T:Lm,isDark:kc,DARK:vc,LIGHT:Sc}});var In=H((_k,Rc)=>{var{T:K}=fn(),po=5,Pn=(()=>{let t=[];for(let e=0;e<po;e++)t.push(e);for(let e=po-2;e>=1;e--)t.push(e);return t})(),Tc=["\u273D","\u2726","\u2727","\u2726"],Kr=class{constructor(e="Thinking..."){this.text=e,this.frame=0,this.interval=null,this.startTime=null}_render(){if(this._stopped)return;let e=Pn[this.frame%Pn.length],n="";for(let s=0;s<po;s++)n+=s===e?`${K.cyan}\u25CF${K.reset}`:" ";let o="";if(this.startTime){let s=Math.floor((Date.now()-this.startTime)/1e3);if(s>=60){let r=Math.floor(s/60),i=s%60;o=` ${K.dim}${r}m ${String(i).padStart(2,"0")}s${K.reset}`}else s>=1&&(o=` ${K.dim}${s}s${K.reset}`)}process.stderr.write(`\x1B[2K\r${n} ${K.dim}${this.text}${K.reset}${o}`),this.frame++}start(){this._stopped=!1,this.startTime=Date.now(),process.stderr.isTTY&&(process.stderr.write("\x1B[?25l"),this._render(),this.interval=setInterval(()=>this._render(),100))}update(e){this.text=e}stop(){this._stopped=!0,this.interval&&(clearInterval(this.interval),this.interval=null),process.stderr.isTTY&&process.stderr.write("\x1B[2K\r\x1B[?25h"),this.startTime=null}},Yr=class{constructor(e){this.labels=e,this.statuses=e.map(()=>"running"),this.frame=0,this.interval=null,this.startTime=null,this.lineCount=e.length}_formatElapsed(){if(!this.startTime)return"";let e=Math.floor((Date.now()-this.startTime)/1e3);if(e<1)return"";let n=Math.floor(e/60),o=e%60;return n>0?`${n}m ${String(o).padStart(2,"0")}s`:`${o}s`}_render(){if(this._stopped)return;let e=Pn[this.frame%Pn.length],n=`${K.cyan}\u25CF${K.reset}`,o=`${K.dim}\u25CB${K.reset}`,s=this._formatElapsed(),r=s?` ${K.dim}${s}${K.reset}`:"",i="";for(let a=0;a<this.labels.length;a++){let l,u;switch(this.statuses[a]){case"done":l=`${K.green}\u2713${K.reset}`,u=K.dim;break;case"error":l=`${K.red}\u2717${K.reset}`,u=K.dim;break;default:l=a===e?n:" ",u=""}let d=a===this.labels.length-1?r:"";i+=`\x1B[2K ${l} ${u}${this.labels[a]}${K.reset}${d}
|
|
4
4
|
`}this.lineCount>0&&(i+=`\x1B[${this.lineCount}A`),process.stderr.write(i),this.frame++}start(){this._stopped=!1,this.startTime=Date.now();let e="\x1B[?25l";for(let n=0;n<this.lineCount;n++)e+=`
|
|
5
5
|
`;this.lineCount>0&&(e+=`\x1B[${this.lineCount}A`),process.stderr.write(e),this._render(),this.interval=setInterval(()=>this._render(),100)}update(e,n){e>=0&&e<this.statuses.length&&(this.statuses[e]=n)}stop(e={}){if(this._stopped=!0,this.interval&&(clearInterval(this.interval),this.interval=null),!e.silent)this._renderFinal();else{let n="";for(let o=0;o<this.lineCount;o++)n+=`\x1B[2K
|
|
@@ -46,13 +46,13 @@ ${h}
|
|
|
46
46
|
`).filter(f=>f.trim()&&!f.startsWith("EXIT ")),d=l?`${A.error}\u2717${A.reset}`:`${A.success}\u2713${A.reset}`;if(u.length===0)r=d;else{if(u.length>2&&u.every(g=>/^\[/.test(g.trim()))){r=`${d} ${u.length} log lines`;break}let m=l?u.slice(-3):u.slice(0,3),h=u.length-3,p=m.map((g,y)=>y===0?`${d} ${A.muted}${g.substring(0,120)}${A.reset}`:` ${A.muted}${g.substring(0,120)}${A.reset}`);if(h>0){let g=l?` ${A.subtle}${h} lines above\u2026${A.reset}`:` ${A.subtle}\u2026 +${h} lines${A.reset}`;l?p.unshift(g):p.push(g)}r=p.join(`
|
|
47
47
|
`)}break}default:{let a=s.split(`
|
|
48
48
|
`).filter(l=>l.trim()&&!l.startsWith("EXIT ")&&!l.startsWith("HINT:"));if(a.length>0){let l=a[0].trim().substring(0,90),u=a.length>1?` ${A.subtle}\u2026 +${a.length-1} lines${A.reset}`:"";r=`${A.muted}${l}${A.reset}${u}`}else r="Done"}}return` ${A.muted}\u2514 ${r}${A.reset}`}function Km(t,e,n,o,s,r){let i=[...n.values()].reduce((d,f)=>d+f,0),a=Math.round(o/1e3),l=a>=60?`${Math.floor(a/60)}m ${a%60}s`:`${a}s`,u=`
|
|
49
|
-
${A.success}\u25C6${ot.reset} ${ot.bold}${t}${ot.reset}`;return u+=`${ot.dim} \xB7 ${e} step${e!==1?"s":""}`,u+=` \xB7 ${i} tool${i!==1?"s":""}`,u+=` \xB7 ${l}`,r.size>0&&(u+=` \xB7 ${r.size} file${r.size!==1?"s":""} modified`),u+=ot.reset,u}Ac.exports={C:ot,formatToolCall:Wm,formatResult:Bm,getToolSpinnerText:Hm,formatToolSummary:Gm,formatSectionHeader:Um,formatMilestone:Km}});var
|
|
49
|
+
${A.success}\u25C6${ot.reset} ${ot.bold}${t}${ot.reset}`;return u+=`${ot.dim} \xB7 ${e} step${e!==1?"s":""}`,u+=` \xB7 ${i} tool${i!==1?"s":""}`,u+=` \xB7 ${l}`,r.size>0&&(u+=` \xB7 ${r.size} file${r.size!==1?"s":""} modified`),u+=ot.reset,u}Ac.exports={C:ot,formatToolCall:Wm,formatResult:Bm,getToolSpinnerText:Hm,formatToolSummary:Gm,formatSectionHeader:Um,formatMilestone:Km}});var Oe=H((kk,Mc)=>{var{T:pn}=fn(),Vr=pn,Nc=["01100110","01111110","01111110","01011010","01111110","00111100"];function Ym(t,e){let n=[];for(let o=0;o<t.length;o+=2){let s="";for(let r=0;r<t[0].length;r++){let i=t[o][r]==="1",a=o+1<t.length&&t[o+1][r]==="1";i&&a?s+=`${e}\u2588\x1B[0m`:i&&!a?s+=`${e}\u2580\x1B[0m`:!i&&a?s+=`${e}\u2584\x1B[0m`:s+=" "}n.push(s)}return n}function zm(t,e,n={}){let o=Vr.bold,s=Vr.reset,r=Ym(Nc,pn.banner_logo),i=n.yolo?` ${o}${pn.banner_yolo}\u26A1 YOLO${s}`:"",a=Nn().version,l=[` ${pn.banner_name}${o}nex-code${s} ${pn.banner_version}v${a}${s}`,` ${pn.banner_model}${t}${s} ${pn.muted}\xB7 /help${s}${i}`,""],u=Math.max(r.length,l.length),d=Math.floor((u-r.length)/2),f=Math.floor((u-l.length)/2),m=Nc[0].length,h=[];for(let p=0;p<u;p++){let g=r[p-d]??" ".repeat(m),y=l[p-f]??"";h.push(g+y)}console.log(`
|
|
50
50
|
`+h.join(`
|
|
51
51
|
`)+`
|
|
52
|
-
`)}var{Spinner:Xm,MultiProgress:Jm,TaskProgress:Vm,ToolProgress:Qm,setActiveTaskProgress:Zm,getActiveTaskProgress:eh,cleanupTerminal:th}=In(),{formatToolCall:nh,formatResult:sh,getToolSpinnerText:oh,formatToolSummary:rh,formatSectionHeader:ih,formatMilestone:ah}=Oc();Mc.exports={C:Vr,banner:zm,Spinner:Xm,MultiProgress:Jm,TaskProgress:Vm,ToolProgress:Qm,setActiveTaskProgress:Zm,getActiveTaskProgress:eh,cleanupTerminal:th,formatToolCall:nh,formatResult:sh,getToolSpinnerText:oh,formatToolSummary:rh,formatSectionHeader:ih,formatMilestone:ah}});var Xe=H((vk,qc)=>{var ch=require("readline"),{C:It}=
|
|
52
|
+
`)}var{Spinner:Xm,MultiProgress:Jm,TaskProgress:Vm,ToolProgress:Qm,setActiveTaskProgress:Zm,getActiveTaskProgress:eh,cleanupTerminal:th}=In(),{formatToolCall:nh,formatResult:sh,getToolSpinnerText:oh,formatToolSummary:rh,formatSectionHeader:ih,formatMilestone:ah}=Oc();Mc.exports={C:Vr,banner:zm,Spinner:Xm,MultiProgress:Jm,TaskProgress:Vm,ToolProgress:Qm,setActiveTaskProgress:Zm,getActiveTaskProgress:eh,cleanupTerminal:th,formatToolCall:nh,formatResult:sh,getToolSpinnerText:oh,formatToolSummary:rh,formatSectionHeader:ih,formatMilestone:ah}});var Xe=H((vk,qc)=>{var ch=require("readline"),{C:It}=Oe(),ei=[/rm\s+-rf\s+\/(?:\s|$)/,/rm\s+-rf\s+~(?:\/|\s|$)/,/rm\s+-rf\s+\.(?:\/|\s|$)/,/rm\s+-rf\s+\*(?:\s|$)/,/mkfs/,/dd\s+if=/,/:\(\)\s*\{/,/>\/dev\/sd/,/curl.*\|\s*(?:ba)?sh/,/wget.*\|\s*(?:ba)?sh/,/cat\s+.*\.env\b/,/cat\s+.*credentials/i,/chmod\s+777/,/chown\s+root/,/passwd/,/userdel/,/useradd/,/\beval\s*\(/,/base64.*\|.*bash/,/\bprintenv\b/,/cat\s+.*\.ssh\/id_/,/cat\s+.*\.ssh\/config/,/\bnc\s+-[el]/,/\bncat\b/,/\bsocat\b/,/python3?\s+-c\s/,/node\s+-e\s/,/perl\s+-e\s/,/ruby\s+-e\s/,/(?:^|[;&|]\s*)history(?:\s|$)/,/curl.*-X\s*POST/,/curl.*--data/,/\bsed\s+-n\s+['"]?\d+,\d+p/],Pc=[...ei,/\bgrep\b.*\.env\b/,/\bawk\b.*\.env\b/,/\bsed\b.*\.env\b/,/\bhead\b.*\.env\b/,/\btail\b.*\.env\b/,/\bless\b.*\.env\b/,/\bmore\b.*\.env\b/,/\bstrings\b.*\.env\b/,/\bgrep\b.*credentials/i,/\bawk\b.*credentials/i,/\bsed\b.*credentials/i,/\bhead\b.*credentials/i,/\btail\b.*credentials/i,/\bcat\b.*private.*key/i,/\bgrep\b.*private_key/i,/\bcat\b.*google.*\.json/i,/\bcat\b.*service.?account/i];function lh(t){for(let e of Pc)if(e.test(t))return e;return null}var Ic=[/\.env\b/,/credentials\b/i,/\.ssh\b/,/\.gnupg\b/,/\.aws\b/,/\.npmrc\b/,/\.docker\/config/,/\.kube\/config/,/venv\b/,/\.venv\b/,/\.sqlite3\b/,/\.git\/(?!hooks)/],uh=/\b(?:rm|rmdir|unlink|truncate|shred|mv|cp)\b/,Qr=[/systemctl\s+(status|is-active|is-enabled|list-units|show)/,/journalctl\b/,/\btail\s/,/\bcat\s/,/\bhead\s/,/\bls\b/,/\bfind\s/,/\bgrep\s/,/\bwc\s/,/\bdf\b/,/\bfree\b/,/\buptime\b/,/\bwho\b/,/\bps\s/,/\bgit\s+(status|log|diff|branch|fetch)\b/,/\bgit\s+pull\b/,/\bss\s+-[tlnp]/,/\bnetstat\s/,/\bdu\s/,/\blscpu\b/,/\bnproc\b/,/\buname\b/,/\bhostname\b/,/\bgetent\b/,/\bid\b/,/psql\s.*-c\s/,/\bmysql\s.*-e\s/,/\bdnf\s+(check-update|list|info|history|repolist|updateinfo)\b/,/\brpm\s+-q/,/\bapt\s+list\b/,/\bopenssl\s+s_client\b/,/\bopenssl\s+x509\b/,/\bcertbot\s+certificates\b/,/\bcurl\s+-[sIkv]|curl\s+--head/,/\bdig\s/,/\bnslookup\s/,/\bping\s/,/\bgetenforce\b/,/\bsesearch\b/,/\bausearch\b/,/\bsealert\b/,/\bcrontab\s+-l\b/,/\btimedatectl\b/,/\bfirewall-cmd\s+--list/,/\bfirewall-cmd\s+--state/];function Lc(t){let e=t.match(/ssh\s+[^"]*"([^"]+)"/)?.[1]||t.match(/ssh\s+[^']*'([^']+)'/)?.[1];if(!e)return!1;let o=e.replace(/\bfor\s[\s\S]*?\bdone\b/g,r=>r.replace(/;/g,"\0")).replace(/\bwhile\s[\s\S]*?\bdone\b/g,r=>r.replace(/;/g,"\0")).split(/\s*(?:&&|;)\s*/).map(r=>r.replace(/\x00/g,";").trim()).filter(Boolean);if(o.length===0)return!1;let s=r=>{let i=r.replace(/^sudo\s+(?:-[ugCD]\s+\S+\s+|-[A-Za-z]+\s+)*/,"");if(/^\s*(?:echo|printf)\s/.test(i))return!0;if(/^\s*for\s/.test(r)||/^\s*while\s/.test(r)){let a=r.match(/\bdo\s+([\s\S]*?)\s*(?:done|$)/)?.[1];return a?a.split(/\s*;\s*/).map(u=>u.trim()).filter(Boolean).every(u=>s(u)):Qr.some(l=>l.test(r))}return/^\w+=\$?\(/.test(i)||/^\w+=["']/.test(i)||/^\w+=\S/.test(i)?!0:Qr.some(a=>a.test(i))};return o.every(s)}var ti=[/rm\s+-rf\s/,/docker\s+system\s+prune/,/kubectl\s+delete/,/sudo\s/,/--no-verify\b/,/git\s+reset\s+--hard\b/,/git\s+clean\s+-[a-z]*f/,/git\s+checkout\s+--\s/,/git\s+push\s+(?:--force\b|-f\b)/],Dc=[/git\s+push/,/npm\s+publish/,/\bHUSKY=0\b/,/\bSKIP_HUSKY=1\b/,/\bSKIP_PREFLIGHT_CHECK=true\b/,/npx\s+.*publish/,/docker\s+rm/,/ssh\s/,/wget\s/,/curl\s.*-o\s/,/pip\s+install/,/npm\s+install\s+-g/],jc=[...ti,...Dc],ni=!1,mn=null,Zr=null;function dh(t){ni=t}function fh(t){Zr=t}function ph(){return ni}function mh(t){mn=t}function hh(t){for(let e of ei)if(e.test(t))return e;return null}function gh(t){if(/ssh\s/.test(t)&&Lc(t))return!1;for(let e of jc)if(e.test(t))return!0;return!1}function $h(t){for(let e of ti)if(e.test(t))return!0;return!1}function yh(t){if(process.env.NEX_UNPROTECT==="1"||!uh.test(t))return null;for(let e of Ic)if(e.test(t))return e;return null}function wh(t,e={}){if(ni)return Promise.resolve(!0);if(Zr)return Zr(t,e);if(!process.stdout.isTTY||!process.stdin.isTTY)return bh(t,e);let n=e.toolName?["Yes","No","Always allow"]:["Yes","No"];return new Promise(o=>{let s=0;mn&&mn.pause();let r=global._nexRawWrite||(f=>process.stdout.write(f)),i=()=>{let f=process.stdout.rows||24;return Math.max(1,f-n.length-2)},a=()=>{let f=i(),m=`\x1B[${f};1H\x1B[2K${It.yellow}${t}${It.reset}`;for(let h=0;h<n.length;h++){let p=h===s,g=p?`${It.yellow}\u276F${It.reset}`:" ",y=p?`${It.yellow}${n[h]}${It.reset}`:n[h];m+=`\x1B[${f+1+h};1H\x1B[2K ${g} ${y}`}r(m)},l=f=>{process.stdin.setRawMode(!1),process.stdin.pause(),process.stdin.removeListener("data",d);let m=global._nexRawWrite||(g=>process.stdout.write(g)),h=i(),p="";for(let g=0;g<n.length+1;g++)p+=`\x1B[${h+g};1H\x1B[2K`;m(p),global._nexFooter&&global._nexFooter.drawFooter(),mn&&mn.resume(),o(f)},u=f=>{if(f===1){l(!1);return}f===2&&e.toolName&&ho(e.toolName),l(!0)},d=f=>{if(f[0]===3){l(!1);return}let m=f.toString();if(m==="\r"||m===`
|
|
53
53
|
`){u(s);return}if(m==="\x1B[A"){s=(s-1+n.length)%n.length,a();return}if(m==="\x1B[B"){s=(s+1)%n.length,a();return}let h=m.toLowerCase().trim();if(h==="y"){l(!0);return}if(h==="n"){l(!1);return}if(h==="a"&&e.toolName){ho(e.toolName),l(!0);return}};a(),process.stdin.setRawMode(!0),process.stdin.resume(),process.stdin.on("data",d)})}function bh(t,e){let n=e.toolName?"[Y/n/a] ":"[Y/n] ";return new Promise(o=>{let s=r=>{let i=r.trim().toLowerCase();i==="a"&&e.toolName?(ho(e.toolName),o(!0)):o(i!=="n")};if(mn)mn.question(`${It.yellow}${t} ${n}${It.reset}`,s);else{let r=ch.createInterface({input:process.stdin,output:process.stdout});r.question(`${It.yellow}${t} ${n}${It.reset}`,i=>{r.close(),s(i)})}})}var ho=()=>{};function _h(t){ho=t}qc.exports={FORBIDDEN_PATTERNS:ei,SSH_FORBIDDEN_PATTERNS:Pc,BASH_PROTECTED_PATHS:Ic,SSH_SAFE_PATTERNS:Qr,isSSHReadOnly:Lc,DANGEROUS_BASH:jc,CRITICAL_BASH:ti,NOTABLE_BASH:Dc,isForbidden:hh,isSSHForbidden:lh,isDangerous:gh,isCritical:$h,isBashPathForbidden:yh,confirm:wh,setAutoConfirm:dh,getAutoConfirm:ph,setConfirmHook:fh,setReadlineInterface:mh,setAllowAlwaysHandler:_h}});var Ln=H((Sk,Fc)=>{async function xh(t,e){if(!t.response?.data)return t.message;let n=t.response.data;if(typeof n=="object"&&n!==null&&typeof n.pipe!="function")return e(n)||t.message;try{let o=await new Promise((r,i)=>{let a=[];n.on("data",l=>a.push(l)),n.on("end",()=>r(Buffer.concat(a).toString("utf8"))),n.on("error",i)}),s=JSON.parse(o);return e(s)||o||t.message}catch{return t.message}}var si=class t{constructor(e={}){if(new.target===t)throw new Error("BaseProvider is abstract \u2014 use a concrete provider");this.name=e.name||"unknown",this.baseUrl=e.baseUrl||"",this.models=e.models||{},this.defaultModel=e.defaultModel||null}isConfigured(){throw new Error(`${this.name}: isConfigured() not implemented`)}getApiKey(){return null}getModels(){return this.models}getModelNames(){return Object.keys(this.models)}getModel(e){return this.models[e]||null}async chat(e,n,o={}){throw new Error(`${this.name}: chat() not implemented`)}async stream(e,n,o={}){throw new Error(`${this.name}: stream() not implemented`)}formatMessages(e){return{messages:e}}formatTools(e){return e}normalizeResponse(e){throw new Error(`${this.name}: normalizeResponse() not implemented`)}};Fc.exports={BaseProvider:si,readStreamErrorBody:xh}});var Bc=H((Ek,Wc)=>{var go=require("axios"),kh=require("http"),vh=require("https"),{BaseProvider:Sh,readStreamErrorBody:Eh}=Ln(),$o=new kh.Agent({keepAlive:!0,maxSockets:6,timeout:6e4}),yo=new vh.Agent({keepAlive:!0,maxSockets:6,timeout:6e4}),Uc={"qwen3-coder:480b":{id:"qwen3-coder:480b",name:"Qwen3 Coder 480B",maxTokens:16384,contextWindow:131072},"qwen3-coder-next":{id:"qwen3-coder-next",name:"Qwen3 Coder Next",maxTokens:16384,contextWindow:131072},"devstral-2:123b":{id:"devstral-2:123b",name:"Devstral 2 123B",maxTokens:16384,contextWindow:131072},"devstral-small-2:24b":{id:"devstral-small-2:24b",name:"Devstral Small 2 24B",maxTokens:16384,contextWindow:131072},"kimi-k2.5":{id:"kimi-k2.5",name:"Kimi K2.5",maxTokens:16384,contextWindow:256e3},"kimi-k2:1t":{id:"kimi-k2:1t",name:"Kimi K2 1T",maxTokens:16384,contextWindow:256e3},"kimi-k2-thinking":{id:"kimi-k2-thinking",name:"Kimi K2 Thinking",maxTokens:16384,contextWindow:256e3},"deepseek-v3.2":{id:"deepseek-v3.2",name:"DeepSeek V3.2",maxTokens:16384,contextWindow:131072},"deepseek-v3.1:671b":{id:"deepseek-v3.1:671b",name:"DeepSeek V3.1 671B",maxTokens:16384,contextWindow:131072},"cogito-2.1:671b":{id:"cogito-2.1:671b",name:"Cogito 2.1 671B",maxTokens:16384,contextWindow:131072},"qwen3.5:397b-cloud":{id:"qwen3.5:397b-cloud",name:"Qwen3.5 397B Cloud",maxTokens:16384,contextWindow:262144},"qwen3.5:397b":{id:"qwen3.5:397b",name:"Qwen3.5 397B",maxTokens:16384,contextWindow:262144},"qwen3.5:122b-a10b":{id:"qwen3.5:122b-a10b",name:"Qwen3.5 122B-A10B",maxTokens:16384,contextWindow:262144},"qwen3.5:35b-a3b":{id:"qwen3.5:35b-a3b",name:"Qwen3.5 35B-A3B",maxTokens:16384,contextWindow:262144},"qwen3.5:27b":{id:"qwen3.5:27b",name:"Qwen3.5 27B",maxTokens:16384,contextWindow:262144},"qwen3-next:80b":{id:"qwen3-next:80b",name:"Qwen3 Next 80B",maxTokens:16384,contextWindow:131072},"mistral-large-3:675b":{id:"mistral-large-3:675b",name:"Mistral Large 3 675B",maxTokens:16384,contextWindow:131072},"gpt-oss:120b":{id:"gpt-oss:120b",name:"GPT-OSS 120B",maxTokens:16384,contextWindow:131072},"minimax-m2.5":{id:"minimax-m2.5",name:"MiniMax M2.5",maxTokens:16384,contextWindow:131072},"glm-5":{id:"glm-5",name:"GLM 5",maxTokens:16384,contextWindow:128e3},"glm-4.7":{id:"glm-4.7",name:"GLM 4.7",maxTokens:16384,contextWindow:128e3},"qwen3.5:9b":{id:"qwen3.5:9b",name:"Qwen3.5 9B",maxTokens:8192,contextWindow:262144},"qwen3.5:4b":{id:"qwen3.5:4b",name:"Qwen3.5 4B",maxTokens:8192,contextWindow:262144},"qwen3.5:2b":{id:"qwen3.5:2b",name:"Qwen3.5 2B",maxTokens:8192,contextWindow:262144},"qwen3.5:0.8b":{id:"qwen3.5:0.8b",name:"Qwen3.5 0.8B",maxTokens:8192,contextWindow:262144},"gemma3:27b":{id:"gemma3:27b",name:"Gemma 3 27B",maxTokens:8192,contextWindow:131072},"gemma3:12b":{id:"gemma3:12b",name:"Gemma 3 12B",maxTokens:8192,contextWindow:131072},"gemma3:4b":{id:"gemma3:4b",name:"Gemma 3 4B",maxTokens:8192,contextWindow:131072},"ministral-3:14b":{id:"ministral-3:14b",name:"Ministral 3 14B",maxTokens:8192,contextWindow:131072},"ministral-3:8b":{id:"ministral-3:8b",name:"Ministral 3 8B",maxTokens:8192,contextWindow:131072},"gemini-3-flash-preview":{id:"gemini-3-flash-preview",name:"Gemini 3 Flash Preview",maxTokens:16384,contextWindow:131072}},oi=class extends Sh{constructor(e={}){super({name:"ollama",baseUrl:e.baseUrl||"https://ollama.com",models:e.models||Uc,defaultModel:e.defaultModel||"qwen3-coder:480b",...e}),this.timeout=e.timeout||18e4,this.temperature=e.temperature??.2,this._discovered=!1}async discoverModels(){if(!this._discovered){if(this._discovered=!0,!process.stdout.isTTY){go.get(`${this.baseUrl}/api/tags`,{timeout:5e3,headers:this._getHeaders(),httpAgent:$o,httpsAgent:yo}).then(e=>{let n=e.data?.models||[];for(let o of n){let s=(o.name||o.model||"").replace(/:latest$/,"");!s||this.models[s]||(this.models[s]={id:s,name:o.name||s,maxTokens:16384,contextWindow:131072})}}).catch(()=>{});return}try{let n=(await go.get(`${this.baseUrl}/api/tags`,{timeout:5e3,headers:this._getHeaders(),httpAgent:$o,httpsAgent:yo})).data?.models||[];for(let o of n){let s=(o.name||o.model||"").replace(/:latest$/,"");!s||this.models[s]||(this.models[s]={id:s,name:o.name||s,maxTokens:16384,contextWindow:131072})}}catch{}}}isConfigured(){return!!this.getApiKey()}getApiKey(){return process.env.OLLAMA_API_KEY||null}_getHeaders(){let e=this.getApiKey();if(!e)throw new Error("OLLAMA_API_KEY not set");return{Authorization:`Bearer ${e}`}}_formatMessages(e){return e.map(n=>{if(n.role==="user"&&Array.isArray(n.content)){let o=[],s=[];for(let i of n.content)i.type==="text"?o.push(i.text??""):i.type==="image"&&i.data&&s.push(i.data);let r={role:"user",content:o.join(`
|
|
54
54
|
`)};return s.length>0&&(r.images=s),r}return n})}async chat(e,n,o={}){await this.discoverModels();let s=o.model||this.defaultModel,r=this.getModel(s),i=o.maxTokens||r?.maxTokens||16384,a;try{a=await go.post(`${this.baseUrl}/api/chat`,{model:s,messages:this._formatMessages(e),tools:n&&n.length>0?n:void 0,stream:!1,options:{temperature:o.temperature??this.temperature,num_predict:i}},{timeout:o.timeout||this.timeout,headers:this._getHeaders(),httpAgent:$o,httpsAgent:yo})}catch(l){if(l.name==="CanceledError"||l.name==="AbortError"||l.code==="ERR_CANCELED")throw l;let u=l.response?.status?` [HTTP ${l.response.status}]`:"",d=l.response?.data?.error||l.message;throw new Error(`API Error${u}: ${d}`)}return this.normalizeResponse(a.data)}async stream(e,n,o={}){await this.discoverModels();let s=o.model||this.defaultModel,r=this.getModel(s),i=o.maxTokens||r?.maxTokens||16384,a=o.onToken||(()=>{}),l=o.onThinkingToken||(()=>{}),u;try{u=await go.post(`${this.baseUrl}/api/chat`,{model:s,messages:this._formatMessages(e),tools:n&&n.length>0?n:void 0,stream:!0,options:{temperature:o.temperature??this.temperature,num_predict:i}},{timeout:o.timeout||this.timeout,headers:this._getHeaders(),responseType:"stream",signal:o.signal,httpAgent:$o,httpsAgent:yo})}catch(d){if(d.name==="CanceledError"||d.name==="AbortError"||d.code==="ERR_CANCELED")throw d;let f=d.response?.status?` [HTTP ${d.response.status}]`:"",m=await Eh(d,h=>h?.error);throw new Error(`API Error${f}: ${m}`)}return new Promise((d,f)=>{let m="",h=[],p="";o.signal&&o.signal.addEventListener("abort",()=>{u.data.destroy(),f(new DOMException("The operation was aborted","AbortError"))},{once:!0}),u.data.on("data",g=>{p+=g.toString();let y=p.split(`
|
|
55
|
-
`);p=y.pop()||"";for(let w of y){if(!w.trim())continue;let k;try{k=JSON.parse(w)}catch{continue}if(k.message?.thinking&&l(k.message.thinking),k.message?.content&&(a(k.message.content),m+=k.message.content),k.message?.tool_calls&&(h=h.concat(k.message.tool_calls)),k.done){d({content:m,tool_calls:this._normalizeToolCalls(h)});return}}}),u.data.on("error",g=>{o.signal?.aborted||f(new Error(`Stream error: ${g.message}`))}),u.data.on("end",()=>{if(p.trim())try{let g=JSON.parse(p);g.message?.thinking&&l(g.message.thinking),g.message?.content&&(a(g.message.content),m+=g.message.content),g.message?.tool_calls&&(h=h.concat(g.message.tool_calls))}catch{}d({content:m,tool_calls:this._normalizeToolCalls(h)})})})}normalizeResponse(e){let n=e.message||{};return{content:n.content||"",tool_calls:this._normalizeToolCalls(n.tool_calls||[])}}_normalizeToolCalls(e){return e.map((n,o)=>({id:n.id||`ollama-${Date.now()}-${o}`,function:{name:n.function?.name||n.name||"unknown",arguments:n.function?.arguments||n.arguments||{}}}))}};Wc.exports={OllamaProvider:oi,OLLAMA_MODELS:Uc}});var Xc=H((Tk,zc)=>{var{callChat:Th}=
|
|
55
|
+
`);p=y.pop()||"";for(let w of y){if(!w.trim())continue;let k;try{k=JSON.parse(w)}catch{continue}if(k.message?.thinking&&l(k.message.thinking),k.message?.content&&(a(k.message.content),m+=k.message.content),k.message?.tool_calls&&(h=h.concat(k.message.tool_calls)),k.done){d({content:m,tool_calls:this._normalizeToolCalls(h)});return}}}),u.data.on("error",g=>{o.signal?.aborted||f(new Error(`Stream error: ${g.message}`))}),u.data.on("end",()=>{if(p.trim())try{let g=JSON.parse(p);g.message?.thinking&&l(g.message.thinking),g.message?.content&&(a(g.message.content),m+=g.message.content),g.message?.tool_calls&&(h=h.concat(g.message.tool_calls))}catch{}d({content:m,tool_calls:this._normalizeToolCalls(h)})})})}normalizeResponse(e){let n=e.message||{};return{content:n.content||"",tool_calls:this._normalizeToolCalls(n.tool_calls||[])}}_normalizeToolCalls(e){return e.map((n,o)=>({id:n.id||`ollama-${Date.now()}-${o}`,function:{name:n.function?.name||n.name||"unknown",arguments:n.function?.arguments||n.arguments||{}}}))}};Wc.exports={OllamaProvider:oi,OLLAMA_MODELS:Uc}});var Xc=H((Tk,zc)=>{var{callChat:Th}=Te(),{estimateTokens:ri}=Qe(),Hc=process.env.NEX_COMPACTION!=="false",Gc=6,Kc=500,Rh=`Summarize this conversation history concisely. Focus on:
|
|
56
56
|
- What files were read, created, or modified
|
|
57
57
|
- Key decisions made and their rationale
|
|
58
58
|
- Current state of the task (what's done, what's pending)
|
|
@@ -61,7 +61,7 @@ Be factual and brief. Use bullet points. Max 300 words.`;async function Ch(t){if
|
|
|
61
61
|
${o}`,_compacted:!0,_originalCount:t.length},tokensRemoved:s-r}}catch{return null}}function Yc(t){return t.map(e=>{let n=e.role==="tool"?"tool_result":e.role,o=(e.content||"").substring(0,500);if(e.tool_calls){let s=e.tool_calls.map(r=>r.function?.name).join(", ");return`[${n}] ${o}
|
|
62
62
|
tools: ${s}`}return`[${n}] ${o}`}).join(`
|
|
63
63
|
|
|
64
|
-
`)}zc.exports={compactMessages:Ch,formatMessagesForSummary:Yc,COMPACTION_ENABLED:Hc,COMPACTION_MIN_MESSAGES:Gc,COMPACTION_SUMMARY_BUDGET:Kc}});var Qe=H((Rk,ol)=>{var Ah=require("path");function Vc(){return
|
|
64
|
+
`)}zc.exports={compactMessages:Ch,formatMessagesForSummary:Yc,COMPACTION_ENABLED:Hc,COMPACTION_MIN_MESSAGES:Gc,COMPACTION_SUMMARY_BUDGET:Kc}});var Qe=H((Rk,ol)=>{var Ah=require("path");function Vc(){return Te().getActiveModel()}var Oh={anthropic:3.5,openai:4,gemini:4,ollama:4,local:4},hn=new Map,Jc=1e3,ii=new WeakMap,us=null;function Nh(){if(us!==null)return us;try{let e=Vc()?.provider||"ollama";return us=Oh[e]||4,us}catch{return 4}}function Mh(){us=null}function gn(t){if(!t)return 0;typeof t!="string"&&(t=JSON.stringify(t));let e=t.length<=80?t:`${t.length}:${t.substring(0,60)}:${t.substring(t.length-20)}`,n=hn.get(e);if(n!==void 0)return hn.delete(e),hn.set(e,n),n;let o=Math.ceil(t.length/Nh());if(hn.size>=Jc){let s=Jc>>1,r=hn.keys();for(let i=0;i<s;i++)hn.delete(r.next().value)}return hn.set(e,o),o}function Ph(t){if(ii.has(t))return ii.get(t);let e=JSON.stringify(t);return ii.set(t,e),e}function jn(t){let n=4;if(t.content&&(n+=gn(t.content)),t.tool_calls)for(let o of t.tool_calls){n+=4,n+=gn(o.function?.name||"");let s=o.function?.arguments;typeof s=="string"?n+=gn(s):s&&(n+=gn(JSON.stringify(s)))}return n}function _t(t){let e=0;for(let n of t)e+=jn(n);return e}function Ih(t,e){if(t&&t.length===e.length){let r=!1;for(let i=0;i<e.length;i++)if(t[i]!==e[i]){r=!0;break}if(!r)return 0}let n=t?t.length:0,o=e.length,s=0;for(let r=n;r<o;r++)s+=jn(e[r]);return s}function wo(t){return!t||t.length===0?0:gn(JSON.stringify(t))}function bo(){return Vc()?.contextWindow||32768}function Lh(t,e){let n=_t(t),o=wo(e),s=n+o,r=bo(),i=r>0?s/r*100:0,a=0,l=0,u=0;for(let d of t){let f=jn(d);d.role==="system"?a+=f:d.role==="tool"?u+=f:l+=f}return{used:s,limit:r,percentage:Math.round(i*10)/10,breakdown:{system:a,conversation:l,toolResults:u,toolDefinitions:o},messageCount:t.length}}var Qc=parseFloat(process.env.NEX_COMPRESSION_THRESHOLD)||.75,Zc=parseFloat(process.env.NEX_SAFETY_MARGIN)||.1,el=parseInt(process.env.NEX_KEEP_RECENT,10)||10,Dh=200,jh=500;function tl(t,e){if(!t||t.length<=e)return t;let o=/^(ERROR|EXIT|BLOCKED|CANCELLED)/i.test(t)?e*3:e;if(t.length<=o)return t;let s=t.split(`
|
|
65
65
|
`);if(s.length<=10){let p=Math.floor(o*.6),g=Math.floor(o*.4),y=t.substring(0,p),w=t.substring(t.length-g);return y+`
|
|
66
66
|
...(${t.length} chars total)...
|
|
67
67
|
`+w}let r=Math.floor(s.length*.4),i=Math.floor(s.length*.4),a=[],l=0,u=Math.floor(o*.4);for(let p=0;p<r&&l<u;p++)if(l+s[p].length+1>u&&s[p].trim().startsWith("```")){a.push(s[p]),l+=s[p].length+1;let g=p+1;for(;g<s.length&&l<u*1.5&&!s[g].trim().startsWith("```");)a.push(s[g]),l+=s[g].length+1,g++;g<s.length&&s[g].trim().startsWith("```")&&(a.push(s[g]),l+=s[g].length+1),p=g}else a.push(s[p]),l+=s[p].length+1;let d=[],f=0,m=Math.floor(o*.4);for(let p=s.length-1;p>=s.length-i&&f<m;p--)if(f+s[p].length+1>m&&s[p].trim().startsWith("```")){d.push(s[p]),f+=s[p].length+1;let g=p-1;for(;g>=0&&f<m*1.5&&!s[g].trim().startsWith("```");)d.push(s[g]),f+=s[g].length+1,g--;g>=0&&s[g].trim().startsWith("```")&&(d.push(s[g]),f+=s[g].length+1),p=g}else d.push(s[p]),f+=s[p].length+1;d.reverse();let h=s.length-a.length-d.length;return a.join(`
|
|
@@ -69,7 +69,7 @@ ${o}`,_compacted:!0,_originalCount:t.length},tokensRemoved:s-r}}catch{return nul
|
|
|
69
69
|
...(${h} lines omitted, ${s.length} total)...
|
|
70
70
|
`+d.join(`
|
|
71
71
|
`)}function Dn(t,e="light"){let n=e==="aggressive"?100:e==="medium"?200:jh,o=e==="aggressive"?50:e==="medium"?100:Dh;if(t.role==="tool"){let s=typeof t.content=="string"?t.content:JSON.stringify(t.content);return s.length>o?{...t,content:tl(s,o)}:t}if(t.role==="assistant"){let s={...t};return s.content&&s.content.length>n&&(s.content=s.content.substring(0,n)+`
|
|
72
|
-
...(truncated)`),s.tool_calls&&e==="aggressive"&&(s.tool_calls=s.tool_calls.map(r=>({...r,function:{name:r.function.name,arguments:typeof r.function.arguments=="string"?r.function.arguments.substring(0,50):r.function.arguments}}))),s}return t}function nl(t,e,n,o){let s=0;if(t.role==="system")return 100;if(t.role==="user")s+=35;else if(t.role==="tool"){let i=typeof t.content=="string"?t.content:JSON.stringify(t.content||"");/^(ERROR|BLOCKED|CANCELLED)/i.test(i)?s+=30:s+=15}else t.role==="assistant"&&(s+=t.tool_calls?20:10);let r=n>1?e/(n-1):1;if(s+=Math.round(r*30),o&&o.size>0){let i=typeof t.content=="string"?t.content:JSON.stringify(t.content||""),a=0;for(let l of o)(i.includes(l)||i.includes(Ah.basename(l)))&&a++;s+=Math.min(30,a*10)}return Math.min(100,s)}function sl(t,e=10){let n=new Set,o=t.slice(-e),s=/(?:\/[\w.-]+)+\.\w+/g;for(let r of o){let a=(typeof r.content=="string"?r.content:JSON.stringify(r.content||"")).match(s);a&&a.forEach(l=>n.add(l))}return n}async function qh(t,e,n={}){let o=n.threshold??Qc,s=n.safetyMargin??Zc,r=n.keepRecent??el,i=bo(),a=wo(e),l=Math.floor(i*(o-s)),u=l-a,d=_t(t),f=d+a;if(f<=l)return{messages:t,compressed:!1,compacted:!1,tokensRemoved:0};let m=d,h=null,p=0;t.length>0&&t[0].role==="system"&&(h=t[0],p=1);let g=Math.max(p,t.length-r),y=t.slice(p,g),w=t.slice(g),k=y.filter(C=>!C._compacted);if(k.length>=6)try{let{compactMessages:C}=Xc(),L=await C(k);if(L){let me=[...y.filter(oe=>oe._compacted),L.message],
|
|
72
|
+
...(truncated)`),s.tool_calls&&e==="aggressive"&&(s.tool_calls=s.tool_calls.map(r=>({...r,function:{name:r.function.name,arguments:typeof r.function.arguments=="string"?r.function.arguments.substring(0,50):r.function.arguments}}))),s}return t}function nl(t,e,n,o){let s=0;if(t.role==="system")return 100;if(t.role==="user")s+=35;else if(t.role==="tool"){let i=typeof t.content=="string"?t.content:JSON.stringify(t.content||"");/^(ERROR|BLOCKED|CANCELLED)/i.test(i)?s+=30:s+=15}else t.role==="assistant"&&(s+=t.tool_calls?20:10);let r=n>1?e/(n-1):1;if(s+=Math.round(r*30),o&&o.size>0){let i=typeof t.content=="string"?t.content:JSON.stringify(t.content||""),a=0;for(let l of o)(i.includes(l)||i.includes(Ah.basename(l)))&&a++;s+=Math.min(30,a*10)}return Math.min(100,s)}function sl(t,e=10){let n=new Set,o=t.slice(-e),s=/(?:\/[\w.-]+)+\.\w+/g;for(let r of o){let a=(typeof r.content=="string"?r.content:JSON.stringify(r.content||"")).match(s);a&&a.forEach(l=>n.add(l))}return n}async function qh(t,e,n={}){let o=n.threshold??Qc,s=n.safetyMargin??Zc,r=n.keepRecent??el,i=bo(),a=wo(e),l=Math.floor(i*(o-s)),u=l-a,d=_t(t),f=d+a;if(f<=l)return{messages:t,compressed:!1,compacted:!1,tokensRemoved:0};let m=d,h=null,p=0;t.length>0&&t[0].role==="system"&&(h=t[0],p=1);let g=Math.max(p,t.length-r),y=t.slice(p,g),w=t.slice(g),k=y.filter(C=>!C._compacted);if(k.length>=6)try{let{compactMessages:C}=Xc(),L=await C(k);if(L){let me=[...y.filter(oe=>oe._compacted),L.message],le=Qt(h,me,w),ie=_t(le);if(ie+a<=l)return{messages:le,compressed:!0,compacted:!0,tokensRemoved:m-ie};y=me}}catch(C){process.env.NEX_DEBUG&&console.error("[context-engine] LLM compacting failed:",C.message)}let R=(f-l)/l,x=y.map(C=>Dn(C,"light")),_=Qt(h,x,w),b=_t(_);if(b+a<=l)return{messages:_,compressed:!0,compacted:!1,tokensRemoved:m-b};if(x=y.map(C=>Dn(C,"medium")),_=Qt(h,x,w),b=_t(_),b+a<=l)return{messages:_,compressed:!0,compacted:!1,tokensRemoved:m-b};if(x=y.map(C=>Dn(C,"aggressive")),_=Qt(h,x,w),b=_t(_),b+a<=l)return{messages:_,compressed:!0,compacted:!1,tokensRemoved:m-b};let O=sl([...x,...w]),N=x.map((C,L)=>({msg:C,score:nl(C,L,x.length,O),tokens:jn(C)}));for(;N.length>0&&b>u;){let C=0;for(let L=1;L<N.length;L++)N[L].score<N[C].score&&(C=L);b-=N[C].tokens,N.splice(C,1)}return x=N.map(C=>C.msg),_=Qt(h,x,w),b=_t(_),{messages:_,compressed:!0,compacted:!1,tokensRemoved:m-b}}function Qt(t,e,n){let o=[];return t&&o.push(t),o.push(...e,...n),o}function Fh(t,e){if(!t)return"";if(gn(t)<=e)return t;let o=e*4,s=t.split(`
|
|
73
73
|
`),r=Math.floor(o*.6),i=Math.floor(o*.4),a="",l=0;for(let h of s){if(a.length+h.length+1>r)break;a+=(a?`
|
|
74
74
|
`:"")+h,l++}let u="",d=0;for(let h=s.length-1;h>=l;h--){let p=s[h]+(u?`
|
|
75
75
|
`:"")+u;if(p.length>i)break;u=p,d++}let m=`
|
|
@@ -85,8 +85,8 @@ ${o}`,_compacted:!0,_originalCount:t.length},tokensRemoved:s-r}}catch{return nul
|
|
|
85
85
|
`)};return s.length>0&&(r.images=s),r}return n})}async chat(e,n,o={}){this._modelsLoaded||await this.loadModels();let s=o.model||this.defaultModel;if(!s)throw new Error("No local model available. Is Ollama running?");let r;try{r=await _o.post(`${this.baseUrl}/api/chat`,{model:s,messages:this._formatMessages(e),tools:n&&n.length>0?n:void 0,stream:!1,options:{temperature:o.temperature??this.temperature,num_predict:o.maxTokens||8192}},{timeout:o.timeout||this.timeout})}catch(i){if(i.name==="CanceledError"||i.name==="AbortError"||i.code==="ERR_CANCELED")throw i;let a=i.response?.status?` [HTTP ${i.response.status}]`:"",l=i.response?.data?.error||i.message;throw new Error(`API Error${a}: ${l}`)}return this.normalizeResponse(r.data)}async stream(e,n,o={}){this._modelsLoaded||await this.loadModels();let s=o.model||this.defaultModel;if(!s)throw new Error("No local model available. Is Ollama running?");let r=o.onToken||(()=>{}),i;try{i=await _o.post(`${this.baseUrl}/api/chat`,{model:s,messages:this._formatMessages(e),tools:n&&n.length>0?n:void 0,stream:!0,options:{temperature:o.temperature??this.temperature,num_predict:o.maxTokens||8192}},{timeout:o.timeout||this.timeout,responseType:"stream",signal:o.signal})}catch(a){if(a.name==="CanceledError"||a.name==="AbortError"||a.code==="ERR_CANCELED")throw a;let l=a.response?.status?` [HTTP ${a.response.status}]`:"",u=await Vh(a,d=>d?.error);throw new Error(`API Error${l}: ${u}`)}return new Promise((a,l)=>{let u="",d=[],f="";o.signal&&o.signal.addEventListener("abort",()=>{i.data.destroy(),l(new DOMException("The operation was aborted","AbortError"))},{once:!0}),i.data.on("data",m=>{f+=m.toString();let h=f.split(`
|
|
86
86
|
`);f=h.pop()||"";for(let p of h){if(!p.trim())continue;let g;try{g=JSON.parse(p)}catch{continue}if(g.message?.content&&(r(g.message.content),u+=g.message.content),g.message?.tool_calls&&(d=d.concat(g.message.tool_calls)),g.done){a({content:u,tool_calls:this._normalizeToolCalls(d)});return}}}),i.data.on("error",m=>{o.signal?.aborted||l(new Error(`Stream error: ${m.message}`))}),i.data.on("end",()=>{if(f.trim())try{let m=JSON.parse(f);m.message?.content&&(r(m.message.content),u+=m.message.content),m.message?.tool_calls&&(d=d.concat(m.message.tool_calls))}catch{}a({content:u,tool_calls:this._normalizeToolCalls(d)})})})}normalizeResponse(e){let n=e.message||{};return{content:n.content||"",tool_calls:this._normalizeToolCalls(n.tool_calls||[])}}_parseContextFromModelfile(e){if(!e)return null;let n=e.match(/PARAMETER\s+num_ctx\s+(\d+)/i);return n?parseInt(n[1],10):null}_normalizeToolCalls(e){return e.map((n,o)=>({id:n.id||`local-${Date.now()}-${o}`,function:{name:n.function?.name||n.name||"unknown",arguments:n.function?.arguments||n.arguments||{}}}))}};yl.exports={LocalProvider:ui,DEFAULT_LOCAL_URL:$l}});var Zt=H((Lk,_l)=>{"use strict";var mt=require("fs"),bl=require("path");function Qh(t){if(!t||isNaN(t))return!1;try{return process.kill(t,0),!0}catch(e){return e.code==="EPERM"}}function Zh(t){try{Atomics.wait(new Int32Array(new SharedArrayBuffer(4)),0,0,t)}catch{let e=Date.now()+t;for(;Date.now()<e;);}}function eg(t,e){let n=bl.dirname(t),o=bl.join(n,`.nex-tmp.${process.pid}.${Date.now()}`);try{mt.existsSync(n)||mt.mkdirSync(n,{recursive:!0}),mt.writeFileSync(o,e,"utf-8"),mt.renameSync(o,t)}catch(s){try{mt.unlinkSync(o)}catch{}throw s}}function tg(t,e,{timeout:n=5e3,retryMs:o=50}={}){let s=t+".lock",r=Date.now()+n;for(;;){let i=-1;try{i=mt.openSync(s,"wx"),mt.writeSync(i,Buffer.from(String(process.pid))),mt.closeSync(i),i=-1;try{return e()}finally{try{mt.unlinkSync(s)}catch{}}}catch(a){if(i!==-1)try{mt.closeSync(i)}catch{}if(a.code!=="EEXIST")throw a;try{let l=mt.readFileSync(s,"utf-8").trim(),u=parseInt(l,10);if(!Qh(u)){try{mt.unlinkSync(s)}catch{}continue}}catch(l){if(l.code&&l.code!=="ENOENT")throw l;continue}if(Date.now()>=r){try{mt.unlinkSync(s)}catch{}return e()}Zh(o)}}}_l.exports={atomicWrite:eg,withFileLockSync:tg}});var Fn=H((Dk,Rl)=>{var qn=require("fs"),di=require("path"),{atomicWrite:ng,withFileLockSync:sg}=Zt(),xl={openai:{"gpt-4o":{input:2.5,output:10},"gpt-4o-mini":{input:.15,output:.6},"gpt-4.1":{input:2,output:8},"gpt-4.1-mini":{input:.4,output:1.6},"gpt-4.1-nano":{input:.1,output:.4},o1:{input:15,output:60},o3:{input:10,output:40},"o3-mini":{input:1.1,output:4.4},"o4-mini":{input:1.1,output:4.4}},anthropic:{"claude-sonnet":{input:3,output:15},"claude-opus":{input:5,output:25},"claude-haiku":{input:.8,output:4},"claude-sonnet-4-5":{input:3,output:15},"claude-sonnet-4":{input:3,output:15}},gemini:{"gemini-2.5-pro":{input:1.25,output:10},"gemini-2.5-flash":{input:.15,output:.6},"gemini-2.0-flash":{input:.1,output:.4},"gemini-2.0-flash-lite":{input:.075,output:.3}},ollama:{"qwen3-coder:480b":{input:0,output:0},"qwen3-coder-next":{input:0,output:0},"devstral-2:123b":{input:0,output:0},"devstral-small-2:24b":{input:0,output:0},"kimi-k2.5":{input:0,output:0},"kimi-k2:1t":{input:0,output:0},"deepseek-v3.2":{input:0,output:0},"minimax-m2.5":{input:0,output:0},"glm-5":{input:0,output:0},"glm-4.7":{input:0,output:0},"gpt-oss:120b":{input:0,output:0}},local:{}},xo=[],en={};function og(t,e,n,o){if(xo.push({provider:t,model:e,input:n,output:o}),en[t]!==void 0){let s=El(t);s.allowed||process.stderr.write(`\x1B[33m\u26A0 Budget limit reached for ${t}: $${s.spent.toFixed(2)} / $${s.limit.toFixed(2)}\x1B[0m
|
|
87
87
|
`)}}function fi(t,e){let n=xl[t];return n?n[e]||{input:0,output:0}:{input:0,output:0}}function kl(t){let e=fi(t.provider,t.model);return(t.input*e.input+t.output*e.output)/1e6}function vl(){let t={};for(let r of xo){let i=`${r.provider}:${r.model}`;t[i]||(t[i]={provider:r.provider,model:r.model,input:0,output:0}),t[i].input+=r.input,t[i].output+=r.output}let e=Object.values(t).map(r=>({...r,cost:kl(r)})),n=e.reduce((r,i)=>r+i.cost,0),o=e.reduce((r,i)=>r+i.input,0),s=e.reduce((r,i)=>r+i.output,0);return{totalCost:n,totalInput:o,totalOutput:s,breakdown:e}}function rg(){let{totalCost:t,totalInput:e,totalOutput:n,breakdown:o}=vl();if(o.length===0)return"No token usage recorded this session.";let s=[];s.push("Session Token Usage:"),s.push("");for(let r of o){let i=r.cost>0?`$${r.cost.toFixed(4)}`:"free";s.push(` ${r.provider}:${r.model}`),s.push(` Input: ${r.input.toLocaleString()} tokens`),s.push(` Output: ${r.output.toLocaleString()} tokens`),s.push(` Cost: ${i}`)}return s.push(""),s.push(` Total: ${e.toLocaleString()} in + ${n.toLocaleString()} out = $${t.toFixed(4)}`),s.join(`
|
|
88
|
-
`)}function ig(t){return!t||typeof t!="string"?0:Math.ceil(t.length/4)}function ag(t,e,n,o){let s=fi(t,e),r=(n*s.input+o*s.output)/1e6;return r<=0?"":`[~$${r.toFixed(4)}]`}function cg(){xo=[]}function lg(t,e){en[t]=e}function ug(t){delete en[t]}function dg(){return{...en}}function Sl(t){let e=0;for(let n of xo)n.provider===t&&(e+=kl(n));return e}function El(t){let e=Sl(t),n=en[t];if(n===void 0)return{allowed:!0,spent:e,limit:null,remaining:null};let o=Math.max(0,n-e);return{allowed:e<n,spent:e,limit:n,remaining:o}}function Tl(){let t=di.join(process.cwd(),".nex","config.json");if(qn.existsSync(t))try{let e=JSON.parse(qn.readFileSync(t,"utf-8"));e.costLimits&&typeof e.costLimits=="object"&&(en={...e.costLimits})}catch{}}function fg(){let t=di.join(process.cwd(),".nex"),e=di.join(t,"config.json");qn.existsSync(t)||qn.mkdirSync(t,{recursive:!0}),sg(e,()=>{let n={};if(qn.existsSync(e))try{n=JSON.parse(qn.readFileSync(e,"utf-8"))}catch{n={}}n.costLimits=en,ng(e,JSON.stringify(n,null,2))})}function pg(){en={}}Tl();Rl.exports={PRICING:xl,trackUsage:og,getSessionCosts:vl,formatCosts:rg,formatCostHint:ag,resetCosts:cg,getPricing:fi,setCostLimit:lg,removeCostLimit:ug,getCostLimits:dg,getProviderSpend:Sl,checkBudget:El,loadCostLimits:Tl,saveCostLimits:fg,resetCostLimits:pg,estimateTokens:ig}});var mi=H((jk,Cl)=>{function pi(){return process.env.NEX_DEBUG==="true"||process.argv.includes("--debug")}var mg=pi();function hg(...t){pi()&&console.log(...t)}function gg(...t){pi()&&console.warn(...t)}Cl.exports={DEBUG:mg,debugLog:hg,warnLog:gg}});var Nl=H((qk,Ol)=>{"use strict";var $g=new Set(["read_file","grep","glob","search_files","list_directory"]),yg=new Set(["write_file","edit_file","patch_file"]),wg=new Set(["bash"]),bg=new Set(["web_search","web_fetch","perplexity_search"]);function Al(t,e){let n=0,o=0,s=0,r=0,i=0;for(let[a,l]of t)i+=l,$g.has(a)&&(n+=l),yg.has(a)&&(o+=l),wg.has(a)&&(s+=l),bg.has(a)&&(r+=l);return i===0?`Phase ${e}`:r/i>.5?"Research":n/i>.5?"Exploration":o/i>.3?"Implementation":s/i>.3&&o/i<.15?"Verification":`Phase ${e}`}var hi=class{constructor(e){this._N=e,this._disabled=e<=0,this._phaseNum=0,this._stepsThisPhase=0,this._phaseCounts=new Map,this._phaseStart=Date.now()}record(e,n,o,s){if(this._disabled)return null;this._stepsThisPhase++;for(let i of n)this._phaseCounts.set(i,(this._phaseCounts.get(i)||0)+1);if(this._stepsThisPhase<this._N)return null;this._phaseNum++;let r={phaseNum:this._phaseNum,phaseName:Al(this._phaseCounts,this._phaseNum),stepCount:this._stepsThisPhase,toolCounts:new Map(this._phaseCounts),elapsed:Date.now()-this._phaseStart,filesRead:new Set(o),filesModified:new Set(s)};return this._stepsThisPhase=0,this._phaseCounts=new Map,this._phaseStart=Date.now(),r}};Ol.exports={MilestoneTracker:hi,_phaseName:Al}});var Un=H((Fk,Ml)=>{var ds=
|
|
89
|
-
`),a}catch(a){throw s.stop(),a}}async function Tg(t,e){return ds.callChat(t,e)}Ml.exports={MODELS:_g,getActiveModel:xg,setActiveModel:kg,getModelNames:vg,callOllamaStream:Eg,callOllama:Tg,parseToolArgs:Sg}});var Ll=H((Wk,Il)=>{var ko=require("path"),{C:P}=
|
|
88
|
+
`)}function ig(t){return!t||typeof t!="string"?0:Math.ceil(t.length/4)}function ag(t,e,n,o){let s=fi(t,e),r=(n*s.input+o*s.output)/1e6;return r<=0?"":`[~$${r.toFixed(4)}]`}function cg(){xo=[]}function lg(t,e){en[t]=e}function ug(t){delete en[t]}function dg(){return{...en}}function Sl(t){let e=0;for(let n of xo)n.provider===t&&(e+=kl(n));return e}function El(t){let e=Sl(t),n=en[t];if(n===void 0)return{allowed:!0,spent:e,limit:null,remaining:null};let o=Math.max(0,n-e);return{allowed:e<n,spent:e,limit:n,remaining:o}}function Tl(){let t=di.join(process.cwd(),".nex","config.json");if(qn.existsSync(t))try{let e=JSON.parse(qn.readFileSync(t,"utf-8"));e.costLimits&&typeof e.costLimits=="object"&&(en={...e.costLimits})}catch{}}function fg(){let t=di.join(process.cwd(),".nex"),e=di.join(t,"config.json");qn.existsSync(t)||qn.mkdirSync(t,{recursive:!0}),sg(e,()=>{let n={};if(qn.existsSync(e))try{n=JSON.parse(qn.readFileSync(e,"utf-8"))}catch{n={}}n.costLimits=en,ng(e,JSON.stringify(n,null,2))})}function pg(){en={}}Tl();Rl.exports={PRICING:xl,trackUsage:og,getSessionCosts:vl,formatCosts:rg,formatCostHint:ag,resetCosts:cg,getPricing:fi,setCostLimit:lg,removeCostLimit:ug,getCostLimits:dg,getProviderSpend:Sl,checkBudget:El,loadCostLimits:Tl,saveCostLimits:fg,resetCostLimits:pg,estimateTokens:ig}});var mi=H((jk,Cl)=>{function pi(){return process.env.NEX_DEBUG==="true"||process.argv.includes("--debug")}var mg=pi();function hg(...t){pi()&&console.log(...t)}function gg(...t){pi()&&console.warn(...t)}Cl.exports={DEBUG:mg,debugLog:hg,warnLog:gg}});var Nl=H((qk,Ol)=>{"use strict";var $g=new Set(["read_file","grep","glob","search_files","list_directory"]),yg=new Set(["write_file","edit_file","patch_file"]),wg=new Set(["bash"]),bg=new Set(["web_search","web_fetch","perplexity_search"]);function Al(t,e){let n=0,o=0,s=0,r=0,i=0;for(let[a,l]of t)i+=l,$g.has(a)&&(n+=l),yg.has(a)&&(o+=l),wg.has(a)&&(s+=l),bg.has(a)&&(r+=l);return i===0?`Phase ${e}`:r/i>.5?"Research":n/i>.5?"Exploration":o/i>.3?"Implementation":s/i>.3&&o/i<.15?"Verification":`Phase ${e}`}var hi=class{constructor(e){this._N=e,this._disabled=e<=0,this._phaseNum=0,this._stepsThisPhase=0,this._phaseCounts=new Map,this._phaseStart=Date.now()}record(e,n,o,s){if(this._disabled)return null;this._stepsThisPhase++;for(let i of n)this._phaseCounts.set(i,(this._phaseCounts.get(i)||0)+1);if(this._stepsThisPhase<this._N)return null;this._phaseNum++;let r={phaseNum:this._phaseNum,phaseName:Al(this._phaseCounts,this._phaseNum),stepCount:this._stepsThisPhase,toolCounts:new Map(this._phaseCounts),elapsed:Date.now()-this._phaseStart,filesRead:new Set(o),filesModified:new Set(s)};return this._stepsThisPhase=0,this._phaseCounts=new Map,this._phaseStart=Date.now(),r}};Ol.exports={MilestoneTracker:hi,_phaseName:Al}});var Un=H((Fk,Ml)=>{var ds=Te(),_g={"kimi-k2.5":{id:"kimi-k2.5",name:"Kimi K2.5",max_tokens:16384},"qwen3-coder:480b":{id:"qwen3-coder:480b",name:"Qwen3 Coder 480B",max_tokens:16384}};function xg(){return ds.getActiveModel()}function kg(t){return ds.setActiveModel(t)}function vg(){return ds.getModelNames()}function Sg(t){if(!t)return null;if(typeof t=="object")return t;try{return JSON.parse(t)}catch{}try{let o=t.replace(/,\s*([}\]])/g,"$1").replace(/'/g,'"');return JSON.parse(o)}catch{}let e=t.match(/\{[\s\S]*\}/);if(e)try{return JSON.parse(e[0])}catch{}try{let o=t.replace(/(\{|,)\s*([a-zA-Z_]\w*)\s*:/g,'$1"$2":');return JSON.parse(o)}catch{}let n=t.match(/```(?:json)?\s*([\s\S]*?)```/);if(n)try{return JSON.parse(n[1].trim())}catch{}return null}async function Eg(t,e){let{C:n}=Oe(),{Spinner:o}=Oe(),s=new o("Thinking...");s.start();let r=!0,i="";try{let a=await ds.callStream(t,e,{onToken:l=>{r&&(s.stop(),process.stdout.write(`${n.blue}`),r=!1),process.stdout.write(l),i+=l}});return r?s.stop():process.stdout.write(`${n.reset}
|
|
89
|
+
`),a}catch(a){throw s.stop(),a}}async function Tg(t,e){return ds.callChat(t,e)}Ml.exports={MODELS:_g,getActiveModel:xg,setActiveModel:kg,getModelNames:vg,callOllamaStream:Eg,callOllama:Tg,parseToolArgs:Sg}});var Ll=H((Wk,Il)=>{var ko=require("path"),{C:P}=Oe(),{T:gi,isDark:Uk}=fn(),{confirm:Rg,getAutoConfirm:Cg}=Xe(),Pl=2e3;function fs(t,e){let n=t.split(`
|
|
90
90
|
`),o=e.split(`
|
|
91
91
|
`),s=[],r=n.length,i=o.length;if(r>Pl||i>Pl){for(let f of n)s.push({type:"remove",line:f});for(let f of o)s.push({type:"add",line:f});return s}let a=Array.from({length:r+1},()=>new Array(i+1).fill(0));for(let f=1;f<=r;f++)for(let m=1;m<=i;m++)n[f-1]===o[m-1]?a[f][m]=a[f-1][m-1]+1:a[f][m]=Math.max(a[f-1][m],a[f][m-1]);let l=r,u=i,d=[];for(;l>0||u>0;)l>0&&u>0&&n[l-1]===o[u-1]?(d.unshift({type:"same",line:n[l-1]}),l--,u--):u>0&&(l===0||a[l][u-1]>=a[l-1][u])?(d.unshift({type:"add",line:o[u-1]}),u--):(d.unshift({type:"remove",line:n[l-1]}),l--);return d}function Ag(t,e,n,o=3){console.log(`
|
|
92
92
|
${P.bold}${P.cyan} Diff: ${t}${P.reset}`);let s=fs(e,n),r=[];if(s.forEach((l,u)=>{l.type!=="same"&&r.push(u)}),r.length===0){console.log(`${P.gray} (no changes)${P.reset}`);return}let i=Math.max(0,r[0]-o),a=Math.min(s.length,r[r.length-1]+o+1);i>0&&console.log(`${P.gray} ...${P.reset}`);for(let l=i;l<a;l++){let u=s[l];switch(u.type){case"remove":console.log(`${P.red} - ${u.line}${P.reset}`);break;case"add":console.log(`${P.green} + ${u.line}${P.reset}`);break;default:console.log(`${P.gray} ${u.line}${P.reset}`)}}a<s.length&&console.log(`${P.gray} ...${P.reset}`),console.log()}function Og(t,e,n){console.log(`
|
|
@@ -96,9 +96,9 @@ ${P.bold}${P.cyan} New file: ${t}${P.reset}`);let n=e.split(`
|
|
|
96
96
|
${P.bold}${P.cyan} Side-by-side: ${t}${P.reset}`),console.log(` ${P.dim}${"\u2500".repeat(r)}\u252C${"\u2500".repeat(r)}${P.reset}`);let i=fs(e,n),a=[],l=0;for(;l<i.length;)if(i[l].type==="same")a.push({left:i[l].line,right:i[l].line,type:"same"}),l++;else if(i[l].type==="remove"){let h=[];for(;l<i.length&&i[l].type==="remove";)h.push(i[l].line),l++;let p=[];for(;l<i.length&&i[l].type==="add";)p.push(i[l].line),l++;let g=Math.max(h.length,p.length);for(let y=0;y<g;y++)a.push({left:y<h.length?h[y]:"",right:y<p.length?p[y]:"",type:"changed"})}else i[l].type==="add"&&(a.push({left:"",right:i[l].line,type:"changed"}),l++);let u=a.map((h,p)=>h.type!=="same"?p:-1).filter(h=>h>=0);if(u.length===0){console.log(` ${P.gray}(no changes)${P.reset}`);return}let d=Math.max(0,u[0]-2),f=Math.min(a.length,u[u.length-1]+3),m=(h,p)=>{let g=h.replace(/\x1b\[[0-9;]*m/g,"");return g.length>=p?h.substring(0,p):h+" ".repeat(p-g.length)};d>0&&console.log(` ${P.dim}${"\xB7".repeat(r)}\u250A${"\xB7".repeat(r)}${P.reset}`);for(let h=d;h<f;h++){let p=a[h];if(p.type==="same")console.log(` ${P.gray}${m(p.left,r)}${P.reset}\u2502${P.gray}${m(p.right,r)}${P.reset}`);else{let g=p.left?`${P.red}${m(p.left,r)}${P.reset}`:`${m("",r)}`,y=p.right?`${P.green}${m(p.right,r)}${P.reset}`:`${m("",r)}`;console.log(` ${g}\u2502${y}`)}}f<a.length&&console.log(` ${P.dim}${"\xB7".repeat(r)}\u250A${"\xB7".repeat(r)}${P.reset}`),console.log(` ${P.dim}${"\u2500".repeat(r)}\u2534${"\u2500".repeat(r)}${P.reset}
|
|
97
97
|
`)}function Ig(t,e,n,o={}){let s=o.label||"Update",r=o.context||3,i=o.annotations||[],a=ko.isAbsolute(t)?ko.relative(process.cwd(),t):t,l=fs(e,n),u=1,d=1;for(let b of l)b.type==="same"?(b.oldLine=u++,b.newLine=d++):b.type==="remove"?(b.oldLine=u++,b.newLine=null):(b.oldLine=null,b.newLine=d++);let f=0,m=0;for(let b of l)b.type==="add"?f++:b.type==="remove"&&m++;if(console.log(`
|
|
98
98
|
${P.green}\u25CF${P.reset} ${P.bold}${s}(${a})${P.reset}`),f===0&&m===0){console.log(` ${P.dim}\u2514 (no changes)${P.reset}
|
|
99
|
-
`);return}let h=[];if(f>0&&h.push(`Added ${f} line${f!==1?"s":""}`),m>0&&h.push(`removed ${m} line${m!==1?"s":""}`),i.length>0){let b=i.filter(L=>L.severity==="error").length,O=i.filter(L=>L.severity==="warn").length,N=i.filter(L=>L.severity==="info").length,C=[];b>0&&C.push(`${P.red}${b} error${b!==1?"s":""}${P.dim}`),O>0&&C.push(`${P.yellow}${O} warning${O!==1?"s":""}${P.dim}`),N>0&&C.push(`${P.cyan}${N} info${N!==1?"s":""}${P.dim}`),h.push(`found ${C.join(", ")}`)}console.log(` ${P.dim}\u2514 ${h.join(", ")}${P.reset}`);let g=[];l.forEach((b,O)=>{b.type!=="same"&&g.push(O)});let y=[],w=null,k=null;for(let b of g){let O=Math.max(0,b-r),N=Math.min(l.length-1,b+r);w===null?(w=O,k=N):(O<=k+1||(y.push([w,k]),w=O),k=N)}w!==null&&y.push([w,k]);let R=" ",x=process.stdout.columns||120;function _(b,O,N){let C=N.replace(/\x1b\[[0-9;]*m/g,""),L=N+" ".repeat(Math.max(0,x-C.length));return`${b}${O}${L}${P.reset}`}for(let b=0;b<y.length;b++){b>0&&console.log(`${R}${P.dim}\xB7\xB7\xB7${P.reset}`);let[O,N]=y[b];for(let C=O;C<=N;C++){let L=l[C],Ee=L.newLine!=null?L.newLine:L.oldLine,me=String(Ee).padStart(4),
|
|
99
|
+
`);return}let h=[];if(f>0&&h.push(`Added ${f} line${f!==1?"s":""}`),m>0&&h.push(`removed ${m} line${m!==1?"s":""}`),i.length>0){let b=i.filter(L=>L.severity==="error").length,O=i.filter(L=>L.severity==="warn").length,N=i.filter(L=>L.severity==="info").length,C=[];b>0&&C.push(`${P.red}${b} error${b!==1?"s":""}${P.dim}`),O>0&&C.push(`${P.yellow}${O} warning${O!==1?"s":""}${P.dim}`),N>0&&C.push(`${P.cyan}${N} info${N!==1?"s":""}${P.dim}`),h.push(`found ${C.join(", ")}`)}console.log(` ${P.dim}\u2514 ${h.join(", ")}${P.reset}`);let g=[];l.forEach((b,O)=>{b.type!=="same"&&g.push(O)});let y=[],w=null,k=null;for(let b of g){let O=Math.max(0,b-r),N=Math.min(l.length-1,b+r);w===null?(w=O,k=N):(O<=k+1||(y.push([w,k]),w=O),k=N)}w!==null&&y.push([w,k]);let R=" ",x=process.stdout.columns||120;function _(b,O,N){let C=N.replace(/\x1b\[[0-9;]*m/g,""),L=N+" ".repeat(Math.max(0,x-C.length));return`${b}${O}${L}${P.reset}`}for(let b=0;b<y.length;b++){b>0&&console.log(`${R}${P.dim}\xB7\xB7\xB7${P.reset}`);let[O,N]=y[b];for(let C=O;C<=N;C++){let L=l[C],Ee=L.newLine!=null?L.newLine:L.oldLine,me=String(Ee).padStart(4),le=L.type!=="remove"?i.filter(ie=>ie.line===L.newLine):[];L.type==="remove"?console.log(_(gi.diff_rem_bg,P.red,`${R}${me} - ${L.line}`)):L.type==="add"?console.log(_(gi.diff_add_bg,P.green,`${R}${me} + ${L.line}`)):console.log(`${R}${P.dim}${me} ${P.reset}${L.line}`);for(let ie of le){let oe=P.cyan,ce="\u2139";ie.severity==="error"?(oe=P.red,ce="\u2716"):ie.severity==="warn"&&(oe=P.yellow,ce="\u26A0"),console.log(`${R} ${oe}${ce} ${ie.message}${P.reset}`)}}}console.log()}function Lg(t,e,n={}){let o=ko.isAbsolute(t)?ko.relative(process.cwd(),t):t,s=e.split(`
|
|
100
100
|
`),r=n.annotations||[];console.log(`
|
|
101
|
-
${P.green}\u25CF${P.reset} ${P.bold}Create(${o})${P.reset}`);let i=[`${s.length} line${s.length!==1?"s":""}`];if(r.length>0){let f=r.filter(g=>g.severity==="error").length,m=r.filter(g=>g.severity==="warn").length,h=r.filter(g=>g.severity==="info").length,p=[];f>0&&p.push(`${P.red}${f} error${f!==1?"s":""}${P.dim}`),m>0&&p.push(`${P.yellow}${m} warning${m!==1?"s":""}${P.dim}`),h>0&&p.push(`${P.cyan}${h} info${h!==1?"s":""}${P.dim}`),i.push(`found ${p.join(", ")}`)}console.log(` ${P.dim}\u2514 ${i.join(", ")}${P.reset}`);let l=" ",u=process.stdout.columns||120,d=Math.min(s.length,20);for(let f=0;f<d;f++){let m=String(f+1).padStart(4),h=f+1,p=r.filter(k=>k.line===h),g=`${l}${m} + ${s[f]}`,y=g.replace(/\x1b\[[0-9;]*m/g,""),w=g+" ".repeat(Math.max(0,u-y.length));console.log(`${gi.diff_add_bg}${P.green}${w}${P.reset}`);for(let k of p){let R=P.cyan,x="\u2139";k.severity==="error"?(R=P.red,x="\u2716"):k.severity==="warn"&&(R=P.yellow,x="\u26A0"),console.log(`${l} ${R}${x} ${k.message}${P.reset}`)}}s.length>20&&console.log(`${l}${P.dim} ...+${s.length-20} more lines${P.reset}`),console.log()}Il.exports={diffLines:fs,showEditDiff:Ag,showWriteDiff:Og,showNewFilePreview:Ng,confirmFileChange:Mg,showSideBySideDiff:Pg,showClaudeDiff:Ig,showClaudeNewFile:Lg}});var tn=H((Bk,ql)=>{var Dl=require("util").promisify(require("child_process").exec),Dg=require("util").promisify(require("child_process").execFile),{C:Me}=
|
|
101
|
+
${P.green}\u25CF${P.reset} ${P.bold}Create(${o})${P.reset}`);let i=[`${s.length} line${s.length!==1?"s":""}`];if(r.length>0){let f=r.filter(g=>g.severity==="error").length,m=r.filter(g=>g.severity==="warn").length,h=r.filter(g=>g.severity==="info").length,p=[];f>0&&p.push(`${P.red}${f} error${f!==1?"s":""}${P.dim}`),m>0&&p.push(`${P.yellow}${m} warning${m!==1?"s":""}${P.dim}`),h>0&&p.push(`${P.cyan}${h} info${h!==1?"s":""}${P.dim}`),i.push(`found ${p.join(", ")}`)}console.log(` ${P.dim}\u2514 ${i.join(", ")}${P.reset}`);let l=" ",u=process.stdout.columns||120,d=Math.min(s.length,20);for(let f=0;f<d;f++){let m=String(f+1).padStart(4),h=f+1,p=r.filter(k=>k.line===h),g=`${l}${m} + ${s[f]}`,y=g.replace(/\x1b\[[0-9;]*m/g,""),w=g+" ".repeat(Math.max(0,u-y.length));console.log(`${gi.diff_add_bg}${P.green}${w}${P.reset}`);for(let k of p){let R=P.cyan,x="\u2139";k.severity==="error"?(R=P.red,x="\u2716"):k.severity==="warn"&&(R=P.yellow,x="\u26A0"),console.log(`${l} ${R}${x} ${k.message}${P.reset}`)}}s.length>20&&console.log(`${l}${P.dim} ...+${s.length-20} more lines${P.reset}`),console.log()}Il.exports={diffLines:fs,showEditDiff:Ag,showWriteDiff:Og,showNewFilePreview:Ng,confirmFileChange:Mg,showSideBySideDiff:Pg,showClaudeDiff:Ig,showClaudeNewFile:Lg}});var tn=H((Bk,ql)=>{var Dl=require("util").promisify(require("child_process").exec),Dg=require("util").promisify(require("child_process").execFile),{C:Me}=Oe();async function $i(t){try{let{stdout:e}=await Dl(t,{cwd:process.cwd(),timeout:3e4});return e.trim()}catch{return null}}async function vo(...t){try{let{stdout:e}=await Dg("git",t,{cwd:process.cwd(),timeout:3e4});return e.trim()}catch{return null}}async function jg(){return await $i("git rev-parse --is-inside-work-tree")==="true"}async function qg(){return await $i("git branch --show-current")}async function yi(){try{let{stdout:t}=await Dl("git status --porcelain",{cwd:process.cwd(),timeout:3e4});return!t||!t.trim()?[]:t.split(`
|
|
102
102
|
`).filter(Boolean).map(e=>{let n=e.substring(0,2).trim(),o=e.substring(3);return{status:n,file:o}})}catch{return[]}}async function So(t=!1){return await $i(`git diff ${t?"--cached":""}`)||""}async function wi(){return(await yi()).map(e=>e.file)}async function jl(){let t=await wi();if(t.length===0)return null;let e=await So(),o=await So(!0)||e,s=0,r=0;if(o){let d=o.split(`
|
|
103
103
|
`);for(let f of d)f.startsWith("+")&&!f.startsWith("+++")&&s++,f.startsWith("-")&&!f.startsWith("---")&&r++}else s=t.length;let i="chore",a=t.join(" ").toLowerCase();a.includes("test")?i="test":a.includes("readme")||a.includes("doc")?i="docs":s>r*2?i="feat":r>s?i="refactor":i="fix";let l=t.slice(0,3).map(d=>d.split("/").pop());return{summary:`${i}: update ${l.join(", ")}${t.length>3?` (+${t.length-3} more)`:""}`,type:i,files:t,stats:{additions:s,deletions:r}}}async function Fg(t){let n=`feat/${t.toLowerCase().replace(/[^a-z0-9\s-]/g,"").replace(/\s+/g,"-").substring(0,50)}`;return await vo("checkout","-b",n)!==null?n:null}async function Ug(t){return await vo("add","-A"),await vo("commit","-m",t)?await vo("rev-parse","--short","HEAD"):null}async function Wg(){let t=await jl();if(!t)return`${Me.dim}No changes${Me.reset}`;let e=[];e.push(`
|
|
104
104
|
${Me.bold}${Me.cyan}Git Diff Summary:${Me.reset}`),e.push(` ${Me.green}+${t.stats.additions}${Me.reset} ${Me.red}-${t.stats.deletions}${Me.reset} in ${t.files.length} file(s)`),e.push(`
|
|
@@ -110,13 +110,13 @@ ${Me.bold}${Me.cyan}Suggested message:${Me.reset}`),e.push(` ${Me.cyan}${t.summ
|
|
|
110
110
|
DIFF:
|
|
111
111
|
${o}`)}return e.join(`
|
|
112
112
|
`)}ql.exports={isGitRepo:jg,getCurrentBranch:qg,getStatus:yi,getDiff:So,getChangedFiles:wi,analyzeDiff:jl,createBranch:Fg,commit:Ug,formatDiffSummary:Wg,getDiffContext:Hg,getMergeConflicts:Bg}});var Wt=H((Gk,Gl)=>{var Ke=require("fs").promises,Hk=require("fs"),xt=require("path"),Gg=require("crypto"),{execSync:ps}=require("child_process"),Kg=100*1024,Wl=50,rt=[],Wn=[];function Yg(t,e,n,o){for(rt.push({tool:t,filePath:e,oldContent:n,newContent:o,timestamp:Date.now()});rt.length>Wl;)rt.shift();Wn.length=0,Bl(rt[rt.length-1]).catch(()=>{})}async function zg(){if(rt.length===0)return null;let t=rt.pop();if(t.oldContent===null)try{await Ke.unlink(t.filePath)}catch{}else await Ke.writeFile(t.filePath,t.oldContent,"utf-8");return Wn.push(t),{tool:t.tool,filePath:t.filePath,wasCreated:t.oldContent===null}}async function Xg(){if(Wn.length===0)return null;let t=Wn.pop();return await Ke.writeFile(t.filePath,t.newContent,"utf-8"),rt.push(t),{tool:t.tool,filePath:t.filePath}}function Jg(t=10){return rt.slice(-t).reverse().map(e=>({tool:e.tool,filePath:e.filePath,timestamp:e.timestamp}))}function Vg(){return rt.length}function Qg(){return Wn.length}function Zg({diskToo:t=!0}={}){if(rt.length=0,Wn.length=0,t){let e=To();Ke.readdir(e).then(n=>{for(let o of n)o.endsWith(".json")&&Ke.unlink(xt.join(e,o)).catch(()=>{})}).catch(()=>{})}}function To(){return xt.join(process.cwd(),".nex","history")}async function Fl(t,e){if(t==null)return{inline:!0,content:t};if(Buffer.byteLength(t,"utf-8")<=Kg)return{inline:!0,content:t};let n=Gg.createHash("sha256").update(t,"utf-8").digest("hex"),o=xt.join(e,"blobs");return await Ke.mkdir(o,{recursive:!0}),await Ke.writeFile(xt.join(o,n),t,"utf-8"),{inline:!1,hash:n}}async function Bl(t){let e=To();await Ke.mkdir(e,{recursive:!0});let n=xt.basename(t.filePath).replace(/[^a-zA-Z0-9]/g,"-"),o=`${t.timestamp}-${n}.json`,s=await Fl(t.oldContent,e),r=await Fl(t.newContent,e),i={tool:t.tool,filePath:t.filePath,timestamp:t.timestamp,oldContent:s.inline?{inline:!0,content:s.content}:{inline:!1,hash:s.hash},newContent:r.inline?{inline:!0,content:r.content}:{inline:!1,hash:r.hash}};await Ke.writeFile(xt.join(e,o),JSON.stringify(i),"utf-8")}async function Ul(t,e){if(!t)return null;if(t.inline)return t.content;let n=xt.join(e,"blobs",t.hash);return Ke.readFile(n,"utf-8")}async function e$(){let t=To(),e;try{e=await Ke.readdir(t)}catch{return 0}let n=e.filter(s=>s.endsWith(".json")).sort(),o=0;for(let s of n)try{let r=await Ke.readFile(xt.join(t,s),"utf-8"),i=JSON.parse(r),a=await Ul(i.oldContent,t),l=await Ul(i.newContent,t);rt.push({tool:i.tool,filePath:i.filePath,timestamp:i.timestamp,oldContent:a,newContent:l}),o++}catch{}for(;rt.length>Wl;)rt.shift();return o}async function t$(t=7){let e=To(),n;try{n=await Ke.readdir(e)}catch{return 0}let o=Date.now()-t*24*60*60*1e3,s=n.filter(u=>u.endsWith(".json")),r=0,i=new Set,a=[];for(let u of s)try{let d=await Ke.readFile(xt.join(e,u),"utf-8"),f=JSON.parse(d);f.timestamp<o?(a.push(u),r++):(f.oldContent&&!f.oldContent.inline&&f.oldContent.hash&&i.add(f.oldContent.hash),f.newContent&&!f.newContent.inline&&f.newContent.hash&&i.add(f.newContent.hash))}catch{}for(let u of a)try{await Ke.unlink(xt.join(e,u))}catch{}let l=xt.join(e,"blobs");try{let u=await Ke.readdir(l);for(let d of u)if(!i.has(d))try{await Ke.unlink(xt.join(l,d))}catch{}}catch{}return r}var Eo="nex-snapshot";function n$(t,e=process.cwd()){let n=t?`${Eo}-${t.replace(/[^a-zA-Z0-9_-]/g,"-")}`:`${Eo}-${Date.now()}`;try{return ps("git status --porcelain",{cwd:e,timeout:1e4}).toString().trim()?(ps(`git stash push -u -m "${n}"`,{cwd:e,timeout:15e3}),ps("git stash pop",{cwd:e,timeout:1e4}),{name:n,label:n,ok:!0}):{name:n,label:n,ok:!1,error:"No changes to snapshot (working tree clean)"}}catch(o){return{name:n,label:n,ok:!1,error:o.message}}}function Hl(t=process.cwd()){try{let e=ps("git stash list",{cwd:t,timeout:1e4}).toString().trim();return e?e.split(`
|
|
113
|
-
`).map(n=>{let o=n.match(/^stash@\{(\d+)\}:\s+(?:WIP on [^:]+:\s+\S+\s+|On \S+:\s+)(.*)/);if(!o)return null;let s=o[2].trim();return s.startsWith(Eo)?{index:parseInt(o[1],10),label:s,shortName:s.replace(`${Eo}-`,""),date:n}:null}).filter(Boolean):[]}catch{return[]}}function s$(t,e=process.cwd()){try{let n=Hl(e);if(n.length===0)return{ok:!1,error:"No snapshots found"};let o;return t===void 0||t==="last"?o=n[0]:typeof t=="number"?o=n.find(s=>s.index===t):o=n.find(s=>s.label===t||s.shortName===t||s.shortName.includes(String(t))),o?(ps(`git stash apply stash@{${o.index}}`,{cwd:e,timeout:15e3}),{ok:!0,label:o.label}):{ok:!1,error:`Snapshot not found: ${t}`}}catch(n){return{ok:!1,error:n.message}}}Gl.exports={recordChange:Yg,undo:zg,redo:Xg,getHistory:Jg,getUndoCount:Vg,getRedoCount:Qg,clearHistory:Zg,persistEntry:Bl,loadPersistedHistory:e$,pruneHistory:t$,createSnapshot:n$,listSnapshots:Hl,restoreSnapshot:s$}});var nn=H((Kk,Jl)=>{var
|
|
113
|
+
`).map(n=>{let o=n.match(/^stash@\{(\d+)\}:\s+(?:WIP on [^:]+:\s+\S+\s+|On \S+:\s+)(.*)/);if(!o)return null;let s=o[2].trim();return s.startsWith(Eo)?{index:parseInt(o[1],10),label:s,shortName:s.replace(`${Eo}-`,""),date:n}:null}).filter(Boolean):[]}catch{return[]}}function s$(t,e=process.cwd()){try{let n=Hl(e);if(n.length===0)return{ok:!1,error:"No snapshots found"};let o;return t===void 0||t==="last"?o=n[0]:typeof t=="number"?o=n.find(s=>s.index===t):o=n.find(s=>s.label===t||s.shortName===t||s.shortName.includes(String(t))),o?(ps(`git stash apply stash@{${o.index}}`,{cwd:e,timeout:15e3}),{ok:!0,label:o.label}):{ok:!1,error:`Snapshot not found: ${t}`}}catch(n){return{ok:!1,error:n.message}}}Gl.exports={recordChange:Yg,undo:zg,redo:Xg,getHistory:Jg,getUndoCount:Vg,getRedoCount:Qg,clearHistory:Zg,persistEntry:Bl,loadPersistedHistory:e$,pruneHistory:t$,createSnapshot:n$,listSnapshots:Hl,restoreSnapshot:s$}});var nn=H((Kk,Jl)=>{var Re=require("fs"),it=require("path"),{atomicWrite:o$,withFileLockSync:r$}=Zt(),at=[];function ms(){return it.join(process.cwd(),".nex","skills")}function Kl(){return it.join(process.cwd(),".nex","config.json")}function Yl(){let t=ms();return Re.existsSync(t)||Re.mkdirSync(t,{recursive:!0}),t}function xi(){let t=Kl();if(!Re.existsSync(t))return[];try{let e=JSON.parse(Re.readFileSync(t,"utf-8"));return e.skills&&Array.isArray(e.skills.disabled)?e.skills.disabled:[]}catch{return[]}}function zl(t){let e=Kl(),n=it.dirname(e);Re.existsSync(n)||Re.mkdirSync(n,{recursive:!0}),r$(e,()=>{let o={};if(Re.existsSync(e))try{o=JSON.parse(Re.readFileSync(e,"utf-8"))}catch{o={}}o.skills||(o.skills={}),o.skills.disabled=t,o$(e,JSON.stringify(o,null,2))})}function Xl(t,e){let n=[];if(typeof t!="object"||t===null)return{valid:!1,errors:["Module must export an object"]};if(t.name!==void 0&&typeof t.name!="string"&&n.push("name must be a string"),t.description!==void 0&&typeof t.description!="string"&&n.push("description must be a string"),t.instructions!==void 0&&typeof t.instructions!="string"&&n.push("instructions must be a string"),t.commands!==void 0)if(!Array.isArray(t.commands))n.push("commands must be an array");else for(let o=0;o<t.commands.length;o++){let s=t.commands[o];(!s.cmd||typeof s.cmd!="string")&&n.push(`commands[${o}].cmd must be a non-empty string`),s.handler!==void 0&&typeof s.handler!="function"&&n.push(`commands[${o}].handler must be a function`)}if(t.tools!==void 0)if(!Array.isArray(t.tools))n.push("tools must be an array");else for(let o=0;o<t.tools.length;o++){let s=t.tools[o];(!s.function||!s.function.name||typeof s.function.name!="string")&&n.push(`tools[${o}].function.name must be a non-empty string`),s.execute!==void 0&&typeof s.execute!="function"&&n.push(`tools[${o}].execute must be a function`)}return{valid:n.length===0,errors:n}}function bi(t){try{let e=Re.readFileSync(t,"utf-8").trim();return e?{name:it.basename(t,".md"),type:"prompt",filePath:t,instructions:e,commands:[],tools:[]}:null}catch{return null}}function _i(t){try{let e=require(t),{valid:n,errors:o}=Xl(e,t);return n?{name:e.name||it.basename(t,".js"),type:"script",filePath:t,description:e.description||"",instructions:e.instructions||"",commands:(e.commands||[]).map(r=>({cmd:r.cmd.startsWith("/")?r.cmd:`/${r.cmd}`,desc:r.desc||r.description||"",handler:r.handler||null})),tools:(e.tools||[]).map(r=>({type:r.type||"function",function:{name:r.function.name,description:r.function.description||"",parameters:r.function.parameters||{type:"object",properties:{}}},execute:r.execute||null}))}:(console.error(`Skill validation failed: ${t}
|
|
114
114
|
${o.join(`
|
|
115
|
-
`)}`),null)}catch(e){return console.error(`Failed to load skill: ${t}: ${e.message}`),null}}function ki(){at=[];let t=xi(),e=ms(),n=[];if(
|
|
115
|
+
`)}`),null)}catch(e){return console.error(`Failed to load skill: ${t}: ${e.message}`),null}}function ki(){at=[];let t=xi(),e=ms(),n=[];if(Re.existsSync(e))try{n=Re.readdirSync(e)}catch{n=[]}for(let s of n){let r=it.join(e,s),i;try{i=Re.statSync(r)}catch{continue}if(!i.isFile())continue;let a=null;s.endsWith(".md")?a=bi(r):s.endsWith(".js")&&(a=_i(r)),a&&(a.enabled=!t.includes(a.name),at.push(a))}let o=it.join(__dirname,"skills");if(!process.env.NEX_SKIP_BUILTIN_SKILLS&&Re.existsSync(o)){let s;try{s=Re.readdirSync(o).filter(r=>r.endsWith(".md")||r.endsWith(".js"))}catch{s=[]}for(let r of s){let i=it.join(o,r),a=it.basename(r,it.extname(r));if(at.some(d=>d.name===a))continue;let l;try{l=Re.statSync(i)}catch{continue}if(!l.isFile())continue;let u=r.endsWith(".md")?bi(i):_i(i);u&&(u._builtin=!0,u.enabled=!t.includes(u.name),at.push(u))}}return at}function i$(){let t=[];for(let e of at)!e.enabled||!e.instructions||t.push(`[Skill: ${e.name}]
|
|
116
116
|
${e.instructions}`);return t.length===0?"":`SKILL INSTRUCTIONS:
|
|
117
117
|
${t.join(`
|
|
118
118
|
|
|
119
|
-
`)}`}function a$(){let t=[];for(let e of at)if(e.enabled)for(let n of e.commands)t.push({cmd:n.cmd,desc:n.desc||`[skill: ${e.name}]`});return t}function c$(){let t=[];for(let e of at)if(e.enabled)for(let n of e.tools)t.push({type:"function",function:{name:`skill_${n.function.name}`,description:`[Skill:${e.name}] ${n.function.description}`,parameters:n.function.parameters}});return t}async function l$(t,e){if(!t.startsWith("skill_"))return null;let n=t.substring(6);for(let o of at)if(o.enabled){for(let s of o.tools)if(s.function.name===n&&s.execute)try{let r=await s.execute(e);return typeof r=="string"?r:JSON.stringify(r)}catch(r){return`ERROR: Skill tool '${n}' failed: ${r.message}`}}return`ERROR: Skill tool '${n}' not found`}function u$(t){let[e,...n]=t.split(/\s+/),o=n.join(" ").trim();for(let s of at)if(s.enabled){for(let r of s.commands)if(r.cmd===e&&r.handler){try{r.handler(o)}catch(i){console.error(`Skill command error (${e}): ${i.message}`)}return!0}}return!1}function d$(){return at.map(t=>({name:t.name,type:t.type,enabled:t.enabled,description:t.description||"",commands:t.commands.length,tools:t.tools.length,filePath:t.filePath}))}function f$(t){let e=at.find(o=>o.name===t);if(!e)return!1;e.enabled=!0;let n=xi().filter(o=>o!==t);return zl(n),!0}function p$(t){let e=at.find(o=>o.name===t);if(!e)return!1;e.enabled=!1;let n=xi();return n.includes(t)||(n.push(t),zl(n)),!0}function m$(){return at}async function h$(t,e={}){let{execSync:n}=require("child_process"),o=Yl(),s=t;/^[\w-]+\/[\w.-]+$/.test(t)&&(s=`https://github.com/${t}.git`);let r=e.name||it.basename(s,".git").replace(/^nex-skill-/,""),i=it.join(o,r);if(
|
|
119
|
+
`)}`}function a$(){let t=[];for(let e of at)if(e.enabled)for(let n of e.commands)t.push({cmd:n.cmd,desc:n.desc||`[skill: ${e.name}]`});return t}function c$(){let t=[];for(let e of at)if(e.enabled)for(let n of e.tools)t.push({type:"function",function:{name:`skill_${n.function.name}`,description:`[Skill:${e.name}] ${n.function.description}`,parameters:n.function.parameters}});return t}async function l$(t,e){if(!t.startsWith("skill_"))return null;let n=t.substring(6);for(let o of at)if(o.enabled){for(let s of o.tools)if(s.function.name===n&&s.execute)try{let r=await s.execute(e);return typeof r=="string"?r:JSON.stringify(r)}catch(r){return`ERROR: Skill tool '${n}' failed: ${r.message}`}}return`ERROR: Skill tool '${n}' not found`}function u$(t){let[e,...n]=t.split(/\s+/),o=n.join(" ").trim();for(let s of at)if(s.enabled){for(let r of s.commands)if(r.cmd===e&&r.handler){try{r.handler(o)}catch(i){console.error(`Skill command error (${e}): ${i.message}`)}return!0}}return!1}function d$(){return at.map(t=>({name:t.name,type:t.type,enabled:t.enabled,description:t.description||"",commands:t.commands.length,tools:t.tools.length,filePath:t.filePath}))}function f$(t){let e=at.find(o=>o.name===t);if(!e)return!1;e.enabled=!0;let n=xi().filter(o=>o!==t);return zl(n),!0}function p$(t){let e=at.find(o=>o.name===t);if(!e)return!1;e.enabled=!1;let n=xi();return n.includes(t)||(n.push(t),zl(n)),!0}function m$(){return at}async function h$(t,e={}){let{execSync:n}=require("child_process"),o=Yl(),s=t;/^[\w-]+\/[\w.-]+$/.test(t)&&(s=`https://github.com/${t}.git`);let r=e.name||it.basename(s,".git").replace(/^nex-skill-/,""),i=it.join(o,r);if(Re.existsSync(i))return{ok:!1,name:r,error:`Skill "${r}" is already installed at ${i}. Remove it first to reinstall.`};try{n(`git clone --depth 1 ${s} ${i}`,{timeout:3e4,stdio:"pipe"})}catch(d){return{ok:!1,name:r,error:`Git clone failed: ${d.stderr?.toString().trim()||d.message}`}}let a=it.join(i,"skill.json"),l=Re.existsSync(a),u=Re.readdirSync(i).some(d=>(d.endsWith(".md")||d.endsWith(".js"))&&!d.startsWith("."));if(!l&&!u){try{Re.rmSync(i,{recursive:!0,force:!0})}catch{}return{ok:!1,name:r,error:"No skill.json manifest or .md/.js skill file found in repository"}}if(l)try{let d=JSON.parse(Re.readFileSync(a,"utf-8"));d.name||(d.name=r)}catch{try{Re.rmSync(i,{recursive:!0,force:!0})}catch{}return{ok:!1,name:r,error:"Invalid skill.json \u2014 not valid JSON"}}return ki(),{ok:!0,name:r}}async function g$(t){let e=require("axios");try{let n=encodeURIComponent(`nex-skill ${t} OR nex-code-skill ${t}`);return((await e.get(`https://api.github.com/search/repositories?q=${n}&sort=stars&per_page=10`,{timeout:1e4,headers:{Accept:"application/vnd.github.v3+json"}})).data.items||[]).map(s=>({name:s.name.replace(/^nex-skill-/,""),description:s.description||"(no description)",url:s.clone_url,stars:s.stargazers_count,owner:s.owner.login}))}catch(n){return[{name:"error",description:`Search failed: ${n.message}`,url:"",stars:0,owner:""}]}}function $$(t){let e=it.join(ms(),t);if(!Re.existsSync(e))return{ok:!1,error:`Skill "${t}" not found in ${ms()}`};try{return Re.rmSync(e,{recursive:!0,force:!0}),ki(),{ok:!0}}catch(n){return{ok:!1,error:n.message}}}Jl.exports={initSkillsDir:Yl,loadAllSkills:ki,getSkillInstructions:i$,getSkillCommands:a$,getSkillToolDefinitions:c$,routeSkillCall:l$,handleSkillCommand:u$,listSkills:d$,enableSkill:f$,disableSkill:p$,getLoadedSkills:m$,installSkill:h$,searchSkills:g$,removeSkill:$$,_getSkillsDir:ms,_validateScriptSkill:Xl,_loadMarkdownSkill:bi,_loadScriptSkill:_i}});var Co=H((Yk,nu)=>{var{spawn:y$}=require("child_process"),w$=require("path"),Vl=require("fs"),Bt=new Map;function b$(){return w$.join(process.cwd(),".nex","config.json")}function vi(){let t=b$();if(!Vl.existsSync(t))return{};try{return JSON.parse(Vl.readFileSync(t,"utf-8")).mcpServers||{}}catch{return{}}}function Ro(t,e,n={},o=1e4){return new Promise((s,r)=>{let i=`${Date.now()}-${Math.random().toString(36).slice(2,6)}`,a=JSON.stringify({jsonrpc:"2.0",id:i,method:e,params:n})+`
|
|
120
120
|
`,l="",u=setTimeout(()=>{f(),r(new Error(`MCP request timeout: ${e}`))},o);function d(m){l+=m.toString();let h=l.split(`
|
|
121
121
|
`);for(let p of h)if(p.trim())try{let g=JSON.parse(p);if(g.id===i){f(),g.error?r(new Error(`MCP error: ${g.error.message||JSON.stringify(g.error)}`)):s(g.result);return}}catch{}l=h[h.length-1]||""}function f(){clearTimeout(u),t.stdout.removeListener("data",d)}t.stdout.on("data",d);try{t.stdin.write(a)}catch(m){f(),r(new Error(`MCP write failed: ${m.message}`))}})}async function Ql(t,e){if(Bt.has(t))return Bt.get(t);let n=["PATH","HOME","USER","SHELL","LANG","TERM","NODE_ENV"],o={};for(let i of n)process.env[i]&&(o[i]=process.env[i]);let s=y$(e.command,e.args||[],{stdio:["pipe","pipe","pipe"],env:{...o,...e.env||{}}}),r={name:t,proc:s,tools:[],config:e};try{await Ro(s,"initialize",{protocolVersion:"2024-11-05",capabilities:{},clientInfo:{name:"nex-code",version:"0.2.0"}});let i=await Ro(s,"tools/list",{});return r.tools=i&&i.tools||[],Bt.set(t,r),r}catch(i){throw s.kill(),new Error(`Failed to connect MCP server '${t}': ${i.message}`)}}function Zl(t){let e=Bt.get(t);if(!e)return!1;try{e.proc.kill()}catch{}return Bt.delete(t),!0}function _$(){for(let[t]of Bt)Zl(t)}async function eu(t,e,n={}){let o=Bt.get(t);if(!o)throw new Error(`MCP server not connected: ${t}`);let s=await Ro(o.proc,"tools/call",{name:e,arguments:n});return s&&Array.isArray(s.content)?s.content.filter(r=>r.type==="text").map(r=>r.text).join(`
|
|
122
122
|
`):JSON.stringify(s)}function tu(){let t=[];for(let[e,n]of Bt)for(let o of n.tools)t.push({server:e,name:o.name,description:o.description||"",inputSchema:o.inputSchema||{type:"object",properties:{}}});return t}function x$(){return tu().map(t=>({type:"function",function:{name:`mcp_${t.server}_${t.name}`,description:`[MCP:${t.server}] ${t.description}`,parameters:t.inputSchema}}))}async function k$(t,e){if(!t.startsWith("mcp_"))return null;let n=t.substring(4).split("_");if(n.length<2)return null;let o=n[0],s=n.slice(1).join("_");return eu(o,s,e)}function v$(){let t=vi();return Object.entries(t).map(([e,n])=>{let o=Bt.get(e);return{name:e,command:n.command,connected:!!o,toolCount:o?o.tools.length:0}})}async function S$(){let t=vi(),e=[];for(let[n,o]of Object.entries(t))try{let s=await Ql(n,o);e.push({name:n,tools:s.tools.length})}catch(s){e.push({name:n,tools:0,error:s.message})}return e}nu.exports={loadMCPConfig:vi,sendRequest:Ro,connectServer:Ql,disconnectServer:Zl,disconnectAll:_$,callTool:eu,getAllTools:tu,getMCPToolDefinitions:x$,routeMCPCall:k$,listServers:v$,connectAll:S$}});var Oo=H((zk,iu)=>{var su=require("fs"),Si=require("path"),Ao=[],hs=[],$n={},gs=["onToolResult","onModelResponse","onSessionStart","onSessionEnd","onFileChange","beforeToolExec","afterToolExec"];function ou(t,e){if(!t||!t.function||!t.function.name)return{ok:!1,error:"Tool definition must have function.name"};if(typeof e!="function")return{ok:!1,error:"Handler must be a function"};let n=t.function.name;return hs.some(o=>o.definition.function.name===n)?{ok:!1,error:`Tool "${n}" is already registered`}:(hs.push({definition:{type:"function",...t},handler:e}),{ok:!0})}function ru(t,e){return gs.includes(t)?typeof e!="function"?{ok:!1,error:"Handler must be a function"}:($n[t]||($n[t]=[]),$n[t].push(e),{ok:!0}):{ok:!1,error:`Unknown event "${t}". Available: ${gs.join(", ")}`}}async function Ei(t,e){let n=$n[t]||[],o=e;for(let s of n)try{let r=await s(o);r!==void 0&&(o=r)}catch(r){process.env.NEX_DEBUG&&console.error(`[plugin] Hook error on ${t}: ${r.message}`)}return o}function E$(){let t=Si.join(process.cwd(),".nex","plugins"),e=[];if(!su.existsSync(t))return{loaded:0,errors:[]};let n=su.readdirSync(t).filter(s=>s.endsWith(".js")),o={registerTool:ou,registerHook:ru,EVENTS:gs};for(let s of n){let r=Si.join(t,s);try{let i=require(r);if(typeof i=="function")i(o);else if(typeof i.setup=="function")i.setup(o);else{e.push(`${s}: Plugin must export a function or { setup: function }`);continue}Ao.push({name:i.name||Si.basename(s,".js"),filePath:r})}catch(i){e.push(`${s}: ${i.message}`)}}return{loaded:Ao.length,errors:e}}function T$(){return hs.map(t=>t.definition)}async function R$(t,e,n={}){let o=hs.find(a=>a.definition.function.name===t);if(!o)return null;let s=await Ei("beforeToolExec",{name:t,args:e,options:n}),r=await o.handler(s.args||e,n);return(await Ei("afterToolExec",{name:t,args:e,result:r})).result||r}function C$(){return[...Ao]}function A$(){let t={};for(let e of gs)t[e]=($n[e]||[]).length;return t}function O$(){Ao.length=0,hs.length=0;for(let t of Object.keys($n))delete $n[t]}iu.exports={registerTool:ou,registerHook:ru,emit:Ei,loadPlugins:E$,getPluginToolDefinitions:T$,executePluginTool:R$,getLoadedPlugins:C$,getHookCounts:A$,clearPlugins:O$,EVENTS:gs}});var Ti=H((Xk,uu)=>{var{getSkillToolDefinitions:N$}=nn(),{getMCPToolDefinitions:M$}=Co(),{getPluginToolDefinitions:P$}=Oo(),No=new Map;function au(){let{TOOL_DEFINITIONS:t}=Et();return[...t,...N$(),...M$(),...P$()]}function cu(t){if(No.has(t))return No.get(t);let n=au().find(s=>s.function.name===t);if(!n)return null;let o=n.function.parameters;return No.set(t,o),o}function I$(){No.clear()}function Mo(t,e){if(!t||e.length===0)return null;let n=null,o=1/0;for(let s of e){let r=lu(t.toLowerCase(),s.toLowerCase());r<o&&(o=r,n=s)}return o<=Math.ceil(t.length/2)?n:null}function lu(t,e){let n=t.length,o=e.length,s=Array.from({length:n+1},()=>Array(o+1).fill(0));for(let r=0;r<=n;r++)s[r][0]=r;for(let r=0;r<=o;r++)s[0][r]=r;for(let r=1;r<=n;r++)for(let i=1;i<=o;i++)s[r][i]=t[r-1]===e[i-1]?s[r-1][i-1]:1+Math.min(s[r-1][i],s[r][i-1],s[r-1][i-1]);return s[n][o]}function L$(t,e){let n=cu(t);if(n===null){let d=au().map(m=>m.function.name),f=Mo(t,d);return{valid:!1,error:`Unknown tool "${t}".${f?` Did you mean "${f}"?`:""}
|
|
@@ -136,7 +136,7 @@ Expected parameters: ${JSON.stringify(n.properties,null,2)}`}:{valid:!0,correcte
|
|
|
136
136
|
`),s=e.split(`
|
|
137
137
|
`).length;return n.length===0||s===0?null:s===1?B$(n,e):G$(n,e,s)}function du(t){return Math.max(1,Math.floor(t/j$))}function fu(t,e){return t<=Math.ceil(e*q$)}function B$(t,e){let n=e.trim(),o=du(t.length),s=null,r=1/0;for(let i=0;i<t.length;i+=o){let a=t[i];if(!a.trim())continue;let l=Po(a.trim(),n);l<r&&(r=l,s={text:a,distance:l,line:i+1})}return s&&o>1&&(s=H$(t,n,s,o)||s,r=s.distance),fu(r,e.length)?s:null}function H$(t,e,n,o){let s=n.line-1,r=Math.max(0,s-o),i=Math.min(t.length-1,s+o),a=n.distance,l=null;for(let u=r;u<=i;u++){let d=t[u];if(!d.trim())continue;let f=Po(d.trim(),e);f<a&&(a=f,l={text:d,distance:f,line:u+1})}return l}function G$(t,e,n){let o=t.length-n+1;if(o<=0)return null;let s=du(o),r=null,i=1/0;for(let a=0;a<o;a+=s){let l=t.slice(a,a+n).join(`
|
|
138
138
|
`),u=Po(l,e);u<i&&(i=u,r={text:l,distance:u,line:a+1})}return r&&s>1&&(r=K$(t,e,r,s,n,o)||r,i=r.distance),fu(i,e.length)?r:null}function K$(t,e,n,o,s,r){let i=n.line-1,a=Math.max(0,i-o),l=Math.min(r-1,i+o),u=n.distance,d=null;for(let f=a;f<=l;f++){let m=t.slice(f,f+s).join(`
|
|
139
|
-
`),h=Po(m,e);h<u&&(u=h,d={text:m,distance:h,line:f+1})}return d}pu.exports={normalizeWhitespace:Ri,fuzzyFindText:U$,findMostSimilar:W$}});var gu=H((Qk,hu)=>{var{C:Vk}=
|
|
139
|
+
`),h=Po(m,e);h<u&&(u=h,d={text:m,distance:h,line:f+1})}return d}pu.exports={normalizeWhitespace:Ri,fuzzyFindText:U$,findMostSimilar:W$}});var gu=H((Qk,hu)=>{var{C:Vk}=Oe(),Y$=[{name:"OpenAI API Key",regex:/sk-[a-zA-Z0-9]{20,}/i},{name:"Anthropic API Key",regex:/sk-ant-api03-[a-zA-Z0-9-]{90,}/i},{name:"Google Gemini API Key",regex:/AIzaSy[a-zA-Z0-9_-]{30,45}/i},{name:"Slack Token",regex:/xox[bpors]-[a-zA-Z0-9-]+/i},{name:"AWS Access Key",regex:/AKIA[A-Z0-9]{16}/i},{name:"GitHub Token",regex:/ghp_[a-zA-Z0-9]{36}/i},{name:"Private Key",regex:/BEGIN (RSA|EC|DSA|OPENSSH|PGP) PRIVATE KEY/i},{name:"Database URL",regex:/\b(postgres|mongodb|mysql|redis):\/\/[^"'\s]+/i},{name:"Hardcoded Secret",regex:/(password|secret|token|api_key|apikey|api_secret|access_token|auth_token|credentials)\s*[:=]\s*['"'][^'"']{8,}/i}],z$=[{name:"TODO",regex:/\bTODO\b/i,severity:"warn"},{name:"FIXME",regex:/\bFIXME\b/i,severity:"warn"},{name:"Debugger",regex:/\bdebugger\b/,severity:"error",ext:[".js",".ts",".jsx",".tsx"]},{name:"eval()",regex:/\beval\s*\(/,severity:"warn",ext:[".js",".ts",".jsx",".tsx"]},{name:"Console Log",regex:/\bconsole\.log\(/,severity:"info",ext:[".js",".ts",".jsx",".tsx"]},{name:"ANSI Code",regex:/\\x1b\[[0-9;]*m/,severity:"warn",message:"Avoid hardcoded ANSI codes; use cli/ui.js instead."}];function X$(t,e){let n=e.split(`
|
|
140
140
|
`),o=[],s=t?`.${t.split(".").pop()}`:"";for(let r=0;r<n.length;r++){let i=n[r],a=r+1;for(let l of Y$)l.regex.test(i)&&o.push({line:a,message:`Potential secret detected: ${l.name}`,severity:"error"});for(let l of z$)l.ext&&!l.ext.includes(s)||l.regex.test(i)&&o.push({line:a,message:l.message||`Found ${l.name}`,severity:l.severity||"warn"})}return n.length>500&&o.push({line:0,message:`Large file detected (${n.length} lines). Consider refactoring.`,severity:"info"}),o}function J$(){let t=process.memoryUsage();return{rss:Math.round(t.rss/1024/1024*100)/100,heapUsed:Math.round(t.heapUsed/1024/1024*100)/100}}hu.exports={runDiagnostics:X$,getMemoryUsage:J$}});var Mi=H((Zk,xu)=>{var Bn=require("fs").promises,Hn=require("path"),{exec:V$}=require("util").promisify(require("child_process").exec),Gn=[],Ni=null,Io=!1,Ai=0,Q$=6e4;function yu(t){return!(Gn.length===0||Ni!==t||Date.now()-Ai>Q$)}async function wu(t){if(!Io&&!yu(t)){Io=!0,Ni=t;try{try{let{stdout:o}=await V$("rg --files",{cwd:t,timeout:5e3});Gn=o.split(`
|
|
141
141
|
`).filter(Boolean),Ai=Date.now(),Io=!1;return}catch{}let e=[],n=async(o,s)=>{let r;try{r=await Bn.readdir(o,{withFileTypes:!0})}catch{return}for(let i of r){if(i.name==="node_modules"||i.name===".git"||i.name.startsWith("."))continue;let a=s?`${s}/${i.name}`:i.name;i.isDirectory()?await n(Hn.join(o,i.name),a):e.push(a)}};await n(t,""),Gn=e,Ai=Date.now()}catch(e){console.error(`Index error: ${e.message}`)}finally{Io=!1}}}function Oi(){return Gn}function Z$(){return Ni}function ey(t){return Gn.filter(e=>Hn.basename(e)===t)}function ty(t){let e=t.toLowerCase();return Gn.filter(n=>n.toLowerCase().includes(e)).slice(0,20)}var Ci=null,$u=0,ny=12e4,sy=new Set([".js",".ts",".jsx",".tsx",".mjs",".cjs",".py",".go",".rs",".java",".rb"]);function bu(t,e){let n=[],o=t.split(`
|
|
142
142
|
`);for(let s=0;s<o.length;s++){let r=o[s],i=s+1;if([".js",".ts",".jsx",".tsx",".mjs",".cjs"].includes(e)){let a=r.match(/(?:export\s+)?(?:async\s+)?function\s+(\w+)/);a&&n.push({type:"function",name:a[1],line:i});let l=r.match(/(?:export\s+)?class\s+(\w+)/);l&&n.push({type:"class",name:l[1],line:i});let u=r.match(/(?:export\s+)?(?:const|let|var)\s+(\w+)\s*=\s*(?:async\s+)?(?:\([^)]*\)|[\w$]+)\s*=>/);u&&n.push({type:"function",name:u[1],line:i});let d=r.match(/module\.exports\s*=\s*\{([^}]+)\}/);if(d){let m=d[1].split(",").map(h=>h.trim().split(":")[0].trim()).filter(Boolean);for(let h of m)/^\w+$/.test(h)&&n.push({type:"export",name:h,line:i})}let f=r.match(/(?:require\(['"]([^'"]+)['"]\)|from\s+['"]([^'"]+)['"])/);if(f){let m=f[1]||f[2];n.push({type:"import",name:m,line:i})}}if(e===".py"){let a=r.match(/^(?:async\s+)?def\s+(\w+)/);a&&n.push({type:"function",name:a[1],line:i});let l=r.match(/^class\s+(\w+)/);l&&n.push({type:"class",name:l[1],line:i});let u=r.match(/^(?:from\s+(\S+)\s+)?import\s+(\S+)/);u&&n.push({type:"import",name:u[1]||u[2],line:i})}if(e===".go"){let a=r.match(/^func\s+(?:\([^)]+\)\s+)?(\w+)/);a&&n.push({type:"function",name:a[1],line:i});let l=r.match(/^type\s+(\w+)\s+struct/);l&&n.push({type:"class",name:l[1],line:i})}}return n}async function _u(t){t=t||process.cwd();let e=Hn.join(t,".nex","index","content-index.json"),n={};if(Ci&&Date.now()-$u<ny)return Ci;try{let i=await Bn.readFile(e,"utf-8");n=JSON.parse(i)}catch{n={files:{}}}Oi().length===0&&await wu(t);let s={files:{}},r=!1;for(let i of Oi()){let a=Hn.extname(i);if(!sy.has(a))continue;let l=Hn.join(t,i);try{let d=(await Bn.stat(l)).mtimeMs;if(n.files[i]&&n.files[i].mtime===d){s.files[i]=n.files[i];continue}let f=await Bn.readFile(l,"utf-8"),m=bu(f,a);s.files[i]={defs:m,mtime:d},r=!0}catch{}}if(r){let i=Hn.join(t,".nex","index");await Bn.mkdir(i,{recursive:!0}),await Bn.writeFile(e,JSON.stringify(s),"utf-8")}return Ci=s,$u=Date.now(),s}async function oy(t,e){let n=await _u(),o=[],s=t.toLowerCase();for(let[r,i]of Object.entries(n.files))for(let a of i.defs)e&&a.type!==e||a.name.toLowerCase().includes(s)&&o.push({file:r,type:a.type,name:a.name,line:a.line});return o.sort((r,i)=>{let a=r.name.toLowerCase()===s?0:1,l=i.name.toLowerCase()===s?0:1;if(a!==l)return a-l;let u=r.name.toLowerCase().startsWith(s)?0:1,d=i.name.toLowerCase().startsWith(s)?0:1;return u-d}),o.slice(0,50)}xu.exports={refreshIndex:wu,getFileIndex:Oi,getIndexedCwd:Z$,findFileInIndex:ey,searchIndex:ty,isIndexValid:yu,buildContentIndex:_u,searchContentIndex:oy,extractDefinitions:bu}});var Kn=H((ev,Eu)=>{var Lo=require("fs"),jo=require("path"),qo=require("os"),{execFile:ry}=require("child_process"),{promisify:iy}=require("util"),Pi=iy(ry),ay=jo.join(qo.homedir(),".nex","servers.json"),Do=jo.join(qo.tmpdir(),"nex-ssh-sockets");function cy(){return jo.join(process.cwd(),".nex","servers.json")}function ku(){let t=o=>{if(!Lo.existsSync(o))return{};try{return JSON.parse(Lo.readFileSync(o,"utf-8"))}catch{return{}}},e=t(ay),n=t(cy());return{...e,...n}}function ly(t){let e=ku();if(e[t])return{...e[t],_name:t};if(/^[\w.-]+@[\w.-]+$/.test(t)||/[\w-]+\.[\w.-]+/.test(t)||t==="localhost"){let[s,r]=t.includes("@")?t.split("@"):[void 0,t];return{host:r,user:s}}let n=Object.keys(e),o=n.length?`Available profiles: ${n.join(", ")}`:"No profiles configured. Create .nex/servers.json (project) or ~/.nex/servers.json (global)";throw new Error(`Unknown server: "${t}". ${o}`)}function uy(){Lo.existsSync(Do)||Lo.mkdirSync(Do,{recursive:!0})}function vu(t){let e=["-o","BatchMode=yes","-o","ConnectTimeout=15","-o","StrictHostKeyChecking=accept-new","-o","ServerAliveInterval=30"];t.key&&e.push("-i",t.key.replace(/^~/,qo.homedir())),t.port&&Number(t.port)!==22&&e.push("-p",String(t.port)),uy();let n=t.user?`${t.user}@${t.host}`:t.host,o=jo.join(Do,n.replace(/[@.:]/g,"_"));return e.push("-o","ControlMaster=auto","-o",`ControlPath=${o}`,"-o","ControlPersist=120"),e.push(n),{args:e,target:n}}function Su(t){let e=["-o","BatchMode=yes","-o","ConnectTimeout=15","-o","StrictHostKeyChecking=accept-new","-r"];return t.key&&e.push("-i",t.key.replace(/^~/,qo.homedir())),t.port&&Number(t.port)!==22&&e.push("-P",String(t.port)),e}async function dy(t,e,{timeout:n=3e4,sudo:o=!1}={}){let{args:s}=vu(t),r=o&&t.sudo?`sudo sh -c ${JSON.stringify(e)}`:e;try{let{stdout:i,stderr:a}=await Pi("ssh",[...s,r],{timeout:n,maxBuffer:4194304});return{stdout:i||"",stderr:a||"",exitCode:0}}catch(i){let a=typeof i.code=="number"?i.code:1,l=(i.stderr||i.message||"").toString();return{stdout:(i.stdout||"").toString(),stderr:l,exitCode:a,error:Fo(l,t)}}}async function fy(t,e,n,{timeout:o=12e4}={}){let s=Su(t),r=t.user?`${t.user}@${t.host}`:t.host;s.push(e,`${r}:${n}`);try{let{stdout:i,stderr:a}=await Pi("scp",s,{timeout:o,maxBuffer:1048576});return i||a||`Uploaded ${e} \u2192 ${r}:${n}`}catch(i){let a=(i.stderr||i.message||"").toString();throw new Error(Fo(a,t)||a)}}async function py(t,e,n,{timeout:o=12e4}={}){let s=Su(t),r=t.user?`${t.user}@${t.host}`:t.host;s.push(`${r}:${e}`,n);try{let{stdout:i,stderr:a}=await Pi("scp",s,{timeout:o,maxBuffer:1048576});return i||a||`Downloaded ${r}:${e} \u2192 ${n}`}catch(i){let a=(i.stderr||i.message||"").toString();throw new Error(Fo(a,t)||a)}}function Fo(t,e){if(!t)return"";if(/connection refused/i.test(t)){let n=e.port||22;return`${t}
|
|
@@ -146,11 +146,11 @@ HINT: Cannot reach ${e.host}. Check: network connection, correct hostname/IP, DN
|
|
|
146
146
|
HINT: Host key changed for ${e.host}. To reset: ssh-keygen -R ${e.host}`:/timed out/i.test(t)?`${t}
|
|
147
147
|
HINT: Connection timed out to ${e.host}. Check firewall rules and network connectivity.`:/too many authentication failures/i.test(t)?`${t}
|
|
148
148
|
HINT: Too many auth attempts. Add "-o IdentitiesOnly=yes -i ${e.key||"~/.ssh/id_rsa"}" or clear your SSH agent keys.`:t}function my(t,e){let n=e.user?`${e.user}@${e.host}`:e.host,o=e.port&&Number(e.port)!==22?`:${e.port}`:"",s=e.os?` [${e.os}]`:"",r=e.key?` key:${e.key}`:"",i=e.sudo?" sudo:yes":"";return`${t}: ${n}${o}${s}${r}${i}`}Eu.exports={loadServerProfiles:ku,resolveProfile:ly,buildSSHArgs:vu,sshExec:dy,scpUpload:fy,scpDownload:py,enrichSSHError:Fo,formatProfile:my,SSH_SOCKET_DIR:Do}});var Di=H((tv,Ru)=>{var $s=require("fs"),Ii=require("path"),hy=Ii.join(".nex","deploy.json");function Li(){return Ii.join(process.cwd(),hy)}function Tu(){let t=Li();if(!$s.existsSync(t))return{};try{return JSON.parse($s.readFileSync(t,"utf-8"))}catch{return{}}}function gy(t){let e=Tu();if(e[t])return{...e[t],_name:t};let n=Object.keys(e),o=n.length?`Available: ${n.join(", ")}`:"No deploy configs found. Create .nex/deploy.json or use explicit params.";throw new Error(`Unknown deploy config: "${t}". ${o}`)}function $y(t){let e=Ii.join(process.cwd(),".nex");$s.existsSync(e)||$s.mkdirSync(e,{recursive:!0}),$s.writeFileSync(Li(),JSON.stringify(t,null,2)+`
|
|
149
|
-
`,"utf-8")}Ru.exports={loadDeployConfigs:Tu,resolveDeployConfig:gy,saveDeployConfigs:$y,getDeployConfigPath:Li}});var Uo=H((nv,Ou)=>{var{getActiveModel:yy,getActiveProviderName:wy}=
|
|
149
|
+
`,"utf-8")}Ru.exports={loadDeployConfigs:Tu,resolveDeployConfig:gy,saveDeployConfigs:$y,getDeployConfigPath:Li}});var Uo=H((nv,Ou)=>{var{getActiveModel:yy,getActiveProviderName:wy}=Te(),ys={essential:["bash","read_file","write_file","edit_file","list_directory"],standard:["bash","read_file","write_file","edit_file","list_directory","search_files","glob","grep","ask_user","git_status","git_diff","git_log","task_list","ssh_exec","service_manage","service_logs","container_list","container_logs","container_exec","container_manage","deploy"],full:null},ws={"qwen3-coder:480b":"full","qwen3-coder-next":"full","kimi-k2.5":"full","kimi-k2:1t":"full","kimi-k2-thinking":"full","deepseek-v3.2":"full","deepseek-v3.1:671b":"full","devstral-2:123b":"full","devstral-small-2:24b":"standard","cogito-2.1:671b":"full","qwen3-next:80b":"full","qwen3.5:397b-cloud":"full","qwen3.5:397b":"full","qwen3.5:122b-a10b":"full","qwen3.5:35b-a3b":"full","qwen3.5:27b":"full","qwen3.5:9b":"standard","qwen3.5:4b":"essential","qwen3.5:2b":"essential","qwen3.5:0.8b":"essential","mistral-large-3:675b":"full","gpt-oss:120b":"full","minimax-m2.5":"full","glm-5":"full","glm-4.7":"standard","gemma3:27b":"standard","gemma3:12b":"essential","gemma3:4b":"essential","ministral-3:14b":"standard","ministral-3:8b":"essential","gpt-4o":"full","gpt-4.1":"full",o1:"full",o3:"full","o4-mini":"full","claude-sonnet":"full","claude-sonnet-4-5":"full","claude-opus":"full","claude-haiku":"standard","claude-sonnet-4":"full","gemini-3.1-pro-preview":"full","gemini-3-flash-preview":"full","gemini-2.5-pro":"full","gemini-2.5-flash":"full","gemini-2.0-flash":"standard","gemini-2.0-flash-lite":"essential"},bs={ollama:"full",openai:"full",anthropic:"full",gemini:"full",local:"essential"},Lt={};function Au(){try{let t=require("fs"),n=require("path").join(process.cwd(),".nex","config.json");t.existsSync(n)&&(Lt=JSON.parse(t.readFileSync(n,"utf-8")).toolTiers||{})}catch{Lt={}}}Au();function ji(){let e=yy()?.id,n=wy();return e&&Lt[e]?Lt[e]:n&&Lt[`${n}:*`]?Lt[`${n}:*`]:e&&ws[e]?ws[e]:n&&bs[n]?bs[n]:"full"}var by=new Set(["claude-sonnet","claude-sonnet-4-5","claude-sonnet-4","claude-opus","claude-haiku","gpt-4o","gpt-4.1","o1","o3","o4-mini","kimi-k2:1t","kimi-k2.5","kimi-k2-thinking","qwen3-coder:480b","qwen3-coder-next","deepseek-v3.2","deepseek-v3.1:671b"]),Cu={anthropic:"strict",openai:"strict",gemini:"strict",ollama:"fuzzy",local:"fuzzy"};function _y(t,e){return t&&(by.has(t)||t.startsWith("claude-"))?"strict":e&&Cu[e]?Cu[e]:"fuzzy"}function xy(t,e){return t&&Lt[t]?Lt[t]:e&&Lt[`${e}:*`]?Lt[`${e}:*`]:t&&ws[t]?ws[t]:e&&bs[e]?bs[e]:"full"}function ky(t,e){let n=e||ji();if(n==="full"||!ys[n])return t;let o=new Set(ys[n]);return t.filter(s=>o.has(s.function.name))}function vy(){let t=ji(),e=ys[t]?ys[t].length:"all";return{tier:t,toolCount:e}}Ou.exports={filterToolsForModel:ky,getActiveTier:ji,getModelTier:xy,getEditMode:_y,getTierInfo:vy,TIERS:ys,MODEL_TIERS:ws,PROVIDER_DEFAULT_TIER:bs,loadConfigOverrides:Au}});var xs=H((sv,Pu)=>{var qi=null,Ht=null,_s=null,Nu=`Playwright is not installed. Install with:
|
|
150
150
|
npm install playwright && npx playwright install chromium
|
|
151
151
|
Then restart nex-code.`;function Mu(){if(_s!==null)return _s;try{require("playwright"),_s=!0}catch{_s=!1}return _s}async function Wo(){if(!Mu())throw new Error(Nu);return qi||(qi=require("playwright")),(!Ht||!Ht.isConnected())&&(Ht=await qi.chromium.launch({headless:!0})),Ht}async function Sy(){if(Ht){try{await Ht.close()}catch{}Ht=null}}process.on("exit",()=>{if(Ht)try{Ht.close()}catch{}});async function Ey(t,{timeout:e=3e4,waitFor:n="domcontentloaded"}={}){let s=await(await Wo()).newPage();try{await s.goto(t,{waitUntil:n,timeout:e});let r=await s.title(),i=await s.evaluate(()=>(document.querySelectorAll("script,style,nav,footer,header,aside,[role=navigation]").forEach(u=>u.remove()),document.body?.innerText||"")),a=await s.evaluate(()=>Array.from(document.querySelectorAll("a[href]")).slice(0,30).map(l=>({text:(l.innerText||l.textContent||"").trim().substring(0,80),href:l.href})).filter(l=>l.text&&l.href&&!l.href.startsWith("javascript:")));return{title:r,url:s.url(),text:i.substring(0,8e3)+(i.length>8e3?`
|
|
152
|
-
...(truncated)`:""),links:a.slice(0,20)}}finally{await s.close()}}async function Ty(t,{width:e=1280,height:n=800,fullPage:o=!1,timeout:s=3e4}={}){let i=await(await Wo()).newPage();try{await i.setViewportSize({width:e,height:n}),await i.goto(t,{waitUntil:"networkidle",timeout:s});let a=await i.screenshot({type:"png",fullPage:o}),l=require("os"),u=require("path"),d=require("fs"),f=u.join(l.tmpdir(),`nex-screenshot-${Date.now()}.png`);return d.writeFileSync(f,a),{path:f,base64:a.toString("base64"),media_type:"image/png",title:await i.title(),url:i.url()}}finally{await i.close()}}async function Ry(t,{selector:e,text:n,timeout:o=3e4}={}){if(!e&&!n)throw new Error("selector or text is required");let r=await(await Wo()).newPage();try{await r.goto(t,{waitUntil:"domcontentloaded",timeout:o}),n?await r.getByText(n,{exact:!1}).first().click({timeout:1e4}):await r.locator(e).first().click({timeout:1e4}),await r.waitForLoadState("domcontentloaded");let i=await r.title();return`Clicked \u2014 now at: ${r.url()} (${i})`}finally{await r.close()}}async function Cy(t,{selector:e,value:n,submit:o=!1,timeout:s=3e4}={}){if(!e||n===void 0)throw new Error("selector and value are required");let i=await(await Wo()).newPage();try{return await i.goto(t,{waitUntil:"domcontentloaded",timeout:s}),await i.fill(e,String(n)),o&&(await i.keyboard.press("Enter"),await i.waitForLoadState("domcontentloaded")),`Filled "${e}" with value. ${o?`Submitted \u2192 ${i.url()}`:"Not submitted."}`}finally{await i.close()}}Pu.exports={isPlaywrightAvailable:Mu,browserNavigate:Ey,browserScreenshot:Ty,browserClick:Ry,browserFill:Cy,closeBrowser:Sy,INSTALL_MSG:Nu}});var Bo=H((ov,Iu)=>{var{C:
|
|
153
|
-
`)}function Dy(){return Ze.length>0&&Ze.some(t=>t.status==="pending"||t.status==="in_progress")}Iu.exports={createTasks:Oy,updateTask:Ny,getTaskList:My,clearTasks:Py,getReadyTasks:Iy,renderTaskList:Ly,setOnChange:Ay,hasActiveTasks:Dy}});var Yo=H((iv,td)=>{var{callStream:jy,getActiveProviderName:Go,getActiveModelId:qy,getConfiguredProviders:Fy,getProvider:Lu,getActiveProvider:Du,parseModelSpec:ju}=
|
|
152
|
+
...(truncated)`:""),links:a.slice(0,20)}}finally{await s.close()}}async function Ty(t,{width:e=1280,height:n=800,fullPage:o=!1,timeout:s=3e4}={}){let i=await(await Wo()).newPage();try{await i.setViewportSize({width:e,height:n}),await i.goto(t,{waitUntil:"networkidle",timeout:s});let a=await i.screenshot({type:"png",fullPage:o}),l=require("os"),u=require("path"),d=require("fs"),f=u.join(l.tmpdir(),`nex-screenshot-${Date.now()}.png`);return d.writeFileSync(f,a),{path:f,base64:a.toString("base64"),media_type:"image/png",title:await i.title(),url:i.url()}}finally{await i.close()}}async function Ry(t,{selector:e,text:n,timeout:o=3e4}={}){if(!e&&!n)throw new Error("selector or text is required");let r=await(await Wo()).newPage();try{await r.goto(t,{waitUntil:"domcontentloaded",timeout:o}),n?await r.getByText(n,{exact:!1}).first().click({timeout:1e4}):await r.locator(e).first().click({timeout:1e4}),await r.waitForLoadState("domcontentloaded");let i=await r.title();return`Clicked \u2014 now at: ${r.url()} (${i})`}finally{await r.close()}}async function Cy(t,{selector:e,value:n,submit:o=!1,timeout:s=3e4}={}){if(!e||n===void 0)throw new Error("selector and value are required");let i=await(await Wo()).newPage();try{return await i.goto(t,{waitUntil:"domcontentloaded",timeout:s}),await i.fill(e,String(n)),o&&(await i.keyboard.press("Enter"),await i.waitForLoadState("domcontentloaded")),`Filled "${e}" with value. ${o?`Submitted \u2192 ${i.url()}`:"Not submitted."}`}finally{await i.close()}}Pu.exports={isPlaywrightAvailable:Mu,browserNavigate:Ey,browserScreenshot:Ty,browserClick:Ry,browserFill:Cy,closeBrowser:Sy,INSTALL_MSG:Nu}});var Bo=H((ov,Iu)=>{var{C:Ce}=Oe(),vs="",Ze=[],ks=0,yn=null;function Ay(t){yn=t}function Oy(t,e){vs=t,Ze=[],ks=0;for(let o of e){ks++;let s=`t${ks}`;Ze.push({id:s,description:o.description||o.title||o.name||o.task||`Task ${ks}`,status:"pending",dependsOn:o.depends_on||[],result:null})}let n=Ze.map(o=>({...o}));return yn&&yn("create",{name:t,tasks:n}),n}function Ny(t,e,n){let o=Ze.find(s=>s.id===t);return o?(o.status=e,n!==void 0&&(o.result=n),yn&&yn("update",{id:t,status:e,result:n}),{...o}):null}function My(){return{name:vs,tasks:Ze.map(t=>({...t}))}}function Py(){vs="",Ze=[],ks=0,yn&&yn("clear",{})}function Iy(){return Ze.filter(t=>t.status!=="pending"?!1:t.dependsOn.length===0?!0:t.dependsOn.every(e=>{let n=Ze.find(o=>o.id===e);return n&&n.status==="done"}))}function Ly(){if(Ze.length===0)return`${Ce.dim}No active tasks${Ce.reset}`;let t=[];vs&&(t.push(` ${Ce.bold}${Ce.cyan}Tasks: ${vs}${Ce.reset}`),t.push(` ${Ce.dim}${"\u2500".repeat(40)}${Ce.reset}`));for(let s of Ze){let r,i;switch(s.status){case"done":r="\u2713",i=Ce.green;break;case"in_progress":r="\u2192",i=Ce.cyan;break;case"failed":r="\u2717",i=Ce.red;break;default:r="\xB7",i=Ce.dim}let a=s.dependsOn.length>0?` ${Ce.dim}(after: ${s.dependsOn.join(", ")})${Ce.reset}`:"",l=`[${s.status}]`,u=s.description.length>50?s.description.substring(0,47)+"...":s.description;if(t.push(` ${i}${r}${Ce.reset} ${Ce.bold}${s.id}${Ce.reset} ${u.padEnd(40)} ${i}${l}${Ce.reset}${a}`),s.result&&s.status==="done"){let d=s.result.length>60?s.result.substring(0,57)+"...":s.result;t.push(` ${Ce.dim}\u2192 ${d}${Ce.reset}`)}}let e=Ze.filter(s=>s.status==="done").length,n=Ze.filter(s=>s.status==="failed").length,o=Ze.length;return t.push(` ${Ce.dim}${"\u2500".repeat(40)}${Ce.reset}`),t.push(` ${Ce.dim}${e}/${o} done${n>0?`, ${n} failed`:""}${Ce.reset}`),t.join(`
|
|
153
|
+
`)}function Dy(){return Ze.length>0&&Ze.some(t=>t.status==="pending"||t.status==="in_progress")}Iu.exports={createTasks:Oy,updateTask:Ny,getTaskList:My,clearTasks:Py,getReadyTasks:Iy,renderTaskList:Ly,setOnChange:Ay,hasActiveTasks:Dy}});var Yo=H((iv,td)=>{var{callStream:jy,getActiveProviderName:Go,getActiveModelId:qy,getConfiguredProviders:Fy,getProvider:Lu,getActiveProvider:Du,parseModelSpec:ju}=Te(),{parseToolArgs:qu}=Un(),{filterToolsForModel:Uy,getModelTier:Ho}=Uo(),{trackUsage:Fu,estimateTokens:Uu}=Fn(),{MultiProgress:Wy,C:rv}=Oe();function Wu(t){return!t||typeof t!="string"?0:typeof Uu=="function"?Uu(t):Math.ceil(t.length/4)}var Gu=15,By=5,Bu=3,Ku=8,Hy=2,Ko=new Map,Yu=600*1e3;function Gy(t,e){let n=Ko.get(t);return n&&n.agentId!==e&&Date.now()-n.timestamp<Yu?!1:(Ko.set(t,{agentId:e,timestamp:Date.now()}),!0)}function Ss(t){Ko.delete(t)}function Fi(){Ko.clear()}function zu(t){let e=t.message||"",n=t.code||"";return!!(e.includes("429")||e.includes("500")||e.includes("502")||e.includes("503")||e.includes("504")||n==="ECONNRESET"||n==="ECONNABORTED"||n==="ETIMEDOUT"||n==="ECONNREFUSED"||e.includes("socket disconnected")||e.includes("TLS")||e.includes("ECONNRESET")||e.includes("fetch failed")||e.includes("ETIMEDOUT")||e.includes("ENOTFOUND"))}async function Xu(t,e,n){let o;for(let s=0;s<=Bu;s++)try{return await jy(t,e,n)}catch(r){if(o=r,s<Bu&&zu(r)){let a=(r.message||"").includes("429")?Math.min(2e3*Math.pow(2,s),15e3):Math.min(500*Math.pow(2,s),4e3);await new Promise(l=>setTimeout(l,a).unref());continue}throw r}throw o}var Hu=new Set(["ask_user","task_list"]);function Ju(t){return t>=2?new Set([...Hu,"spawn_agents"]):Hu}var Ky=new Set(["write_file","edit_file","patch_file"]),Yy=/\b(read|summarize|search|find|list|check|count|inspect|scan)\b/i,zy=/\b(refactor|rewrite|implement|create|architect|design|generate|migrate)\b/i;function Vu(t){return zy.test(t)?"full":Yy.test(t)?"essential":"standard"}function Qu(t){let e=Fy(),n=Go(),o=[...e].sort((s,r)=>(s.name===n?-1:1)-(r.name===n?-1:1));for(let s of o)for(let r of s.models)if(Ho(r.id,s.name)===t)return{provider:s.name,model:r.id};return null}var Xy={essential:process.env.NEX_FAST_MODEL||null,standard:process.env.NEX_STANDARD_MODEL||null,full:process.env.NEX_HEAVY_MODEL||null};function Ui(t){if(t.model){let{provider:s,model:r}=ju(t.model),i=s?Lu(s):Du(),a=s||Go();if(i&&i.isConfigured()&&(i.getModel(r)||a==="local")){let l=Ho(r,a);return{provider:a,model:r,tier:l}}}let e=Vu(t.task),n=Xy[e];if(n){let{provider:s,model:r}=ju(n),i=s?Lu(s):Du(),a=s||Go();if(i&&i.isConfigured()&&(i.getModel(r)||a==="local")){let l=Ho(r,a);return{provider:a,model:r,tier:l}}}let o=Qu(e);if(o){let s=Ho(o.model,o.provider);return{provider:o.provider,model:o.model,tier:s}}return{provider:null,model:null,tier:null}}async function Zu(t,e={},n=0){let o=n===0?Gu:Ku,s=Math.min(t.max_iterations||10,o),r=`sub-${Date.now()}-${Math.random().toString(36).slice(2,6)}`,i=[],a={input:0,output:0},l=new Set,u=new Map,f=[{role:"system",content:t._systemPrompt||`You are a focused sub-agent. Complete this specific task efficiently.
|
|
154
154
|
|
|
155
155
|
TASK: ${t.task}
|
|
156
156
|
${t.context?`
|
|
@@ -173,8 +173,8 @@ ERROR RECOVERY:
|
|
|
173
173
|
- If edit_file fails with "old_text not found": read the file again, compare, and retry with exact text.
|
|
174
174
|
- If bash fails: read the error, fix the root cause, then retry.
|
|
175
175
|
- After 2 failed attempts at the same operation, summarize the issue and stop.`}];f.push({role:"user",content:t.task});let m=Ui(t),h=m.provider,p=m.model,g=m.tier,{TOOL_DEFINITIONS:y,executeTool:w}=Et(),k=Ju(n),R=Uy(y.filter(_=>!k.has(_.function.name)),g);if(p&&!t._skipLog){let _=g?` (${g})`:"";process.stderr.write(` [sub-agent: ${h}:${p}${_}]
|
|
176
|
-
`)}let x={};h&&(x.provider=h),p&&(x.model=p);try{for(let b=0;b<s;b++){let O=await Xu(f,R,x);if(!O||typeof O!="object")throw new Error("Empty or invalid response from provider");{let
|
|
177
|
-
...(truncated)`:U,tool_call_id:
|
|
176
|
+
`)}let x={};h&&(x.provider=h),p&&(x.model=p);try{for(let b=0;b<s;b++){let O=await Xu(f,R,x);if(!O||typeof O!="object")throw new Error("Empty or invalid response from provider");{let le=h||Go(),ie=p||qy();if(O.usage){let oe=O.usage.prompt_tokens||0,ce=O.usage.completion_tokens||0;a.input+=oe,a.output+=ce,Fu(le,ie,oe,ce)}else{let oe=O.content||"",ce=f.map(M=>typeof M.content=="string"?M.content:Array.isArray(M.content)?M.content.map(U=>typeof U=="string"?U:U.text||"").join(""):"").join(" "),Q=Wu(ce),E=Wu(oe);a.input+=Q,a.output+=E,a._estimated=!0,Fu(le,ie,Q,E)}}let N=O.content||"",C=O.tool_calls,L={role:"assistant",content:N||""};if(C&&C.length>0&&(L.tool_calls=C),f.push(L),!C||C.length===0){for(let le of l)Ss(le);return{task:t.task,status:"done",result:N||"(no response)",toolsUsed:i,tokensUsed:a,modelSpec:h&&p?`${h}:${p}`:null}}let Ee=C.map(le=>{let ie=le.function.name,oe=qu(le.function.arguments),ce=le.id||`sub-${Date.now()}-${Math.random().toString(36).slice(2,6)}`;if(!oe)return Promise.resolve({role:"tool",content:`ERROR: Malformed tool arguments for ${ie}`,tool_call_id:ce});let Q=null;if(Ky.has(ie)&&oe.path){let M=require("path"),U=M.isAbsolute(oe.path)?oe.path:M.resolve(process.cwd(),oe.path);if(l.has(U)||!Gy(U,r))return Promise.resolve({role:"tool",content:`ERROR: File '${oe.path}' is locked by another operation. Try a different approach or skip this file.`,tool_call_id:ce});l.add(U),Q=U}return i.push(ie),e.onUpdate&&e.onUpdate({type:"tool_call",tool:ie,agentId:r}),(ie==="spawn_agents"?ed(oe,n+1):w(ie,oe,{autoConfirm:!0,silent:!0})).then(M=>{Q&&(Ss(Q),l.delete(Q));let U=String(M??"");return{role:"tool",content:U.length>2e4?U.substring(0,2e4)+`
|
|
177
|
+
...(truncated)`:U,tool_call_id:ce}}).catch(M=>(Q&&(Ss(Q),l.delete(Q)),{role:"tool",content:`ERROR: ${M.message}`,tool_call_id:ce}))}),me=await Promise.all(Ee);f.push(...me);for(let le=0;le<C.length;le++){let ie=C[le];if(ie.function.name==="bash_exec"){let oe=qu(ie.function.arguments);if((me[le]?.content||"").startsWith("ERROR")&&oe&&oe.command){let Q=oe.command.replace(/\s+/g," ").trim().slice(0,100);u.set(Q,(u.get(Q)||0)+1)}}}e.onUpdate&&e.onUpdate(`step ${b+1}/${s}`)}for(let b of l)Ss(b);let _=[...u.entries()].filter(([,b])=>b>=3).sort((b,O)=>O[1]-b[1]).slice(0,3).map(([b,O])=>`"${b}" (failed ${O}\xD7)`);return{task:t.task,status:"truncated",abortReason:"iteration_limit",repeatedFailures:_,result:f[f.length-1]?.content||"(max iterations reached)",toolsUsed:i,tokensUsed:a,modelSpec:h&&p?`${h}:${p}`:null}}catch(_){for(let b of l)Ss(b);return{task:t.task,status:"failed",result:`Error: ${_.message}`,toolsUsed:i,tokensUsed:a,modelSpec:h&&p?`${h}:${p}`:null}}}async function ed(t,e=0){if(e>=2)return"ERROR: max agent nesting depth (2) reached \u2014 reviewer agents cannot spawn further agents.";let n=e===0?By:Hy,o=e===0?Gu:Ku,s=(t.agents||[]).slice(0,n);if(s.length===0)return"ERROR: No agents specified";let r=e>0?" \u21B3 ":"",i=e>0?38:44,a=s.map(d=>Ui(d)),l=s.map((d,f)=>{let m=a[f],h=m.model?` [${m.model}]`:"",p=d.task.substring(0,i-h.length);return`${r}Agent ${f+1}${h}: ${p}${d.task.length>p.length?"...":""}`}),u=new Wy(l);u.start();try{let d=s.map((g,y)=>{let w=a[y],k=Math.min(g.max_iterations||o,o),R=w.model?{...g,model:`${w.provider}:${w.model}`,_skipLog:!0,max_iterations:k}:{...g,_skipLog:!0,max_iterations:k};return Zu(R,{onUpdate:()=>{}},e).then(x=>(u.update(y,x.status==="failed"?"error":"done"),x)).catch(x=>(u.update(y,"error"),{task:g.task,status:"failed",result:`Error: ${x.message}`,toolsUsed:[],tokensUsed:{input:0,output:0}}))}),f=await Promise.all(d);u.stop(),Fi();let m=["Sub-agent results:",""],h=0,p=0;for(let g=0;g<f.length;g++){let y=f[g],w=y.status==="done"?"\u2713":y.status==="truncated"?"\u26A0":"\u2717",k=y.modelSpec?` [${y.modelSpec}]`:"";m.push(`${w} Agent ${g+1}${k}: ${y.task}`),m.push(` Status: ${y.status}`),m.push(` Tools used: ${y.toolsUsed.length>0?y.toolsUsed.join(", "):"none"}`),m.push(` Result: ${y.result}`),y.repeatedFailures&&y.repeatedFailures.length>0&&m.push(` Repeated failures: ${y.repeatedFailures.join("; ")}`),m.push(""),h+=y.tokensUsed.input,p+=y.tokensUsed.output}return m.push(`Total sub-agent tokens: ${h} input + ${p} output`),m.join(`
|
|
178
178
|
`)}catch(d){return u.stop(),Fi(),`ERROR: Sub-agent execution failed: ${d.message}`}}td.exports={runSubAgent:Zu,executeSpawnAgents:ed,clearAllLocks:Fi,classifyTask:Vu,pickModelForTier:Qu,resolveSubAgentModel:Ui,isRetryableError:zu,callWithRetry:Xu,getExcludedTools:Ju,LOCK_TIMEOUT_MS:Yu}});var Rs=H((av,hd)=>{var Le=require("fs"),Gt=require("path"),{atomicWrite:nd,withFileLockSync:sd}=Zt(),Jy=new Set(["the","a","an","is","are","and","or","but","in","on","at","to","for","of","with","this","that","it","as","be","by","from","was","were","has","have","had","not","do","does","did","so","if","its","my","me","we","you","he","she","they","our","your","their","can","will","would","could","should","may","might","then","than","also","which","when","where","how","what","who","all","any","each","more","most","use","used","using","get","set","new","add","make","der","die","das","den","dem","des","ein","eine","einen","einem","eines","und","oder","aber","von","zu","mit","auf","bei","nach","aus","vor","ist","sind","war","hat","haben","wird","kann","soll","muss","nicht","auch","als","durch"]);function Yn(){let t=Gt.join(process.cwd(),".nex","brain");return Le.existsSync(t)||Le.mkdirSync(t,{recursive:!0}),t}function Xo(){return Gt.join(Yn(),".brain-index.json")}function Hi(){return Gt.join(Yn(),".embeddings.json")}function Es(){let t=Gt.join(process.cwd(),".nex","brain");if(!Le.existsSync(t))return[];try{return Le.readdirSync(t).filter(e=>e.endsWith(".md")&&!e.startsWith(".")).map(e=>{let n=Gt.join(t,e),o=Le.statSync(n);return{name:e.replace(/\.md$/,""),path:n,size:o.size,modified:new Date(o.mtimeMs)}}).sort((e,n)=>n.modified-e.modified)}catch{return[]}}function Ts(t){let e={},n=t,o=t.match(/^---\r?\n([\s\S]*?)\r?\n---\r?\n?([\s\S]*)$/);if(o){let s=o[1].split(`
|
|
179
179
|
`);for(let r of s){let i=r.match(/^(\w+):\s*(.+)$/);if(!i)continue;let a=i[1].trim(),l=i[2].trim();l.startsWith("[")&&l.endsWith("]")?e[a]=l.slice(1,-1).split(",").map(u=>u.trim()).filter(Boolean):e[a]=l}n=o[2]}return{frontmatter:e,body:n}}function od(t){let e=Gt.join(Yn(),`${t}.md`);if(!Le.existsSync(e))return{name:t,content:"",body:"",frontmatter:{}};let n=Le.readFileSync(e,"utf-8"),{frontmatter:o,body:s}=Ts(n);return{name:t,content:n,body:s,frontmatter:o}}function Vy(t,e){let n=Gt.join(Yn(),`${t}.md`);nd(n,e),ew(t,e),Qy()}function Qy(){if(process.env.NEX_BRAIN_EMBEDDINGS==="false")return;let t=Hi();Le.existsSync(t)&&setImmediate(async()=>{try{await dd()}catch{}})}function Zy(t){let e=Gt.join(Yn(),`${t}.md`);return Le.existsSync(e)?(Le.unlinkSync(e),tw(t),!0):!1}function zo(t){return t.toLowerCase().replace(/[^a-z0-9äöüß\s-]/g," ").split(/[\s-]+/).filter(e=>e.length>2&&!Jy.has(e))}function Gi(t){let e={},{frontmatter:n,body:o}=Ts(t),s=Array.isArray(n.tags)?n.tags:[];for(let i of s){let a=i.toLowerCase().replace(/[^a-z0-9-]/g,"");a.length>1&&(e[a]=(e[a]||0)+5)}let r=(o||t).split(`
|
|
180
180
|
`);for(let i of r)if(i.startsWith("#")){let a=i.replace(/^#+\s*/,"");for(let l of zo(a))e[l]=(e[l]||0)+3}for(let i of zo(o||t))e[i]=(e[i]||0)+1;return e}function Ki(){let t=Xo();if(!Le.existsSync(t))return{documents:{}};try{return JSON.parse(Le.readFileSync(t,"utf-8"))}catch{return{documents:{}}}}function Yi(t){nd(Xo(),JSON.stringify(t,null,2))}function ew(t,e){sd(Xo(),()=>{let n=Ki(),{frontmatter:o}=Ts(e),s=Array.isArray(o.tags)?o.tags:[];n.documents[t]={keywords:Gi(e),tags:s,modified:new Date().toISOString()},Yi(n)})}function tw(t){sd(Xo(),()=>{let e=Ki();delete e.documents[t],Yi(e)})}function Bi(){let t=Es(),e={documents:{}};for(let n of t){let o=Le.readFileSync(n.path,"utf-8"),{frontmatter:s}=Ts(o),r=Array.isArray(s.tags)?s.tags:[];e.documents[n.name]={keywords:Gi(o),tags:r,modified:n.modified.toISOString()}}return Yi(e),e}function rd(){let t=Ki(),e=Es();for(let n of e){let o=t.documents[n.name];if(!o||new Date(o.modified)<n.modified)return Bi()}for(let n of Object.keys(t.documents))if(!e.some(o=>o.name===n))return Bi();return t}function id(t,e={}){let{topK:n=3,minScore:o=.1}=e,s=zo(t);if(s.length===0)return[];let r=rd(),i=[];for(let[a,l]of Object.entries(r.documents)){let u=0;for(let d of s){l.keywords[d]&&(u+=l.keywords[d]);for(let[f,m]of Object.entries(l.keywords))f!==d&&f.length>3&&d.length>3&&(f.includes(d)||d.includes(f))&&(u+=m*.3)}u>=o&&i.push({name:a,score:u})}return i.sort((a,l)=>l.score-a.score),i.slice(0,n)}var ad=process.env.NEX_EMBED_MODEL||"nomic-embed-text",Wi=400,nw=50;async function cd(){if(process.env.NEX_BRAIN_EMBEDDINGS==="false")return!1;try{let t=process.env.OLLAMA_HOST||"http://localhost:11434",e=require("http"),n=require("https"),o=new URL(`${t}/api/tags`),s=o.protocol==="https:"?n:e;return((await new Promise((a,l)=>{let u=s.get(o.toString(),{timeout:2e3},d=>{let f="";d.on("data",m=>f+=m),d.on("end",()=>{try{a(JSON.parse(f))}catch{l(new Error("bad json"))}})});u.on("error",l),u.on("timeout",()=>{u.destroy(),l(new Error("timeout"))})})).models||[]).map(a=>a.name).some(a=>a.startsWith(ad.split(":")[0]))}catch{return!1}}async function zi(t){let e=process.env.OLLAMA_HOST||"http://localhost:11434",n=require("http"),o=require("https"),s=new URL(`${e}/api/embeddings`),r=s.protocol==="https:"?o:n,i=JSON.stringify({model:ad,prompt:t});return new Promise((a,l)=>{let u=r.request(s,{method:"POST",headers:{"Content-Type":"application/json","Content-Length":Buffer.byteLength(i)},timeout:3e4},d=>{let f="";d.on("data",m=>f+=m),d.on("end",()=>{try{a(JSON.parse(f).embedding||[])}catch(m){l(m)}})});u.on("error",l),u.on("timeout",()=>{u.destroy(),l(new Error("embedding timeout"))}),u.write(i),u.end()})}function ld(t,e){if(!t||!e||t.length!==e.length)return 0;let n=0,o=0,s=0;for(let i=0;i<t.length;i++)n+=t[i]*e[i],o+=t[i]*t[i],s+=e[i]*e[i];let r=Math.sqrt(o)*Math.sqrt(s);return r===0?0:n/r}function ud(t){let e=t.split(/\s+/),n=[],o=0;for(;o<e.length;){let s=e.slice(o,o+Wi).join(" ");if(n.push({text:s,offset:o}),o+Wi>=e.length)break;o+=Wi-nw}return n}async function dd(){let t=Es(),e={documents:{}},n=Hi();if(Le.existsSync(n))try{e=JSON.parse(Le.readFileSync(n,"utf-8"))}catch{}for(let o of t){let s=e.documents[o.name];if(s&&new Date(s.modified)>=o.modified)continue;let r=Le.readFileSync(o.path,"utf-8"),i=ud(r),a=[];for(let l of i){let u=await zi(l.text);a.push({text:l.text,embedding:u,offset:l.offset})}e.documents[o.name]={chunks:a,modified:o.modified.toISOString()}}for(let o of Object.keys(e.documents))t.some(s=>s.name===o)||delete e.documents[o];return Le.writeFileSync(n,JSON.stringify(e),"utf-8"),e}async function fd(t,e={}){let{topK:n=3,minSimilarity:o=.3}=e,s=Hi();if(!Le.existsSync(s))return[];let r;try{r=JSON.parse(Le.readFileSync(s,"utf-8"))}catch{return[]}let i=await zi(t),a=[];for(let[l,u]of Object.entries(r.documents||{})){let d=0,f="";for(let m of u.chunks||[]){let h=ld(i,m.embedding);h>d&&(d=h,f=m.text)}d>=o&&a.push({name:l,score:d,bestChunk:f})}return a.sort((l,u)=>u.score-l.score),a.slice(0,n)}function pd(t,e,n={}){let{k:o=60,topK:s=3}=n,r={};return t.forEach((i,a)=>{r[i.name]=(r[i.name]||0)+1/(o+a+1)}),e.forEach((i,a)=>{r[i.name]=(r[i.name]||0)+1/(o+a+1)}),Object.entries(r).map(([i,a])=>({name:i,score:a})).sort((i,a)=>a.score-i.score).slice(0,s)}async function md(t,e={}){let{topK:n=3,minScore:o=.1}=e,s=id(t,{topK:n,minScore:o}),r=s;if(process.env.NEX_BRAIN_EMBEDDINGS!=="false")try{if(await cd()){let a=await fd(t,{topK:n});r=pd(s,a,{topK:n})}}catch{}return r.map(i=>{let a=od(i.name),l=(a.body||a.content||"").slice(0,300).replace(/\n+/g," ")+"...";return{name:i.name,score:i.score,content:a.content,excerpt:l}})}async function sw(t){if(!t||!t.trim())return"";let e=Gt.join(process.cwd(),".nex","brain");if(!Le.existsSync(e)||Es().length===0)return"";let o;try{o=await md(t,{topK:3})}catch{return""}if(!o||o.length===0)return"";let s;try{s=Qe().estimateTokens}catch{s=l=>Math.ceil(l.length/4)}let r=25e3,i=[],a=0;for(let l of o){let u=l.content||"",d=s(u);if(a+d>r){let m=r-a;if(m<100)break;let h=m/d;u=u.slice(0,Math.floor(u.length*h))+`
|
|
@@ -185,7 +185,7 @@ ${i.join(`
|
|
|
185
185
|
|
|
186
186
|
`)}`}hd.exports={getBrainDir:Yn,listDocuments:Es,readDocument:od,writeDocument:Vy,removeDocument:Zy,buildIndex:Bi,getIndex:rd,query:md,getBrainContext:sw,isEmbeddingAvailable:cd,generateEmbedding:zi,buildEmbeddingIndex:dd,semanticQuery:fd,cosineSimilarity:ld,_keywordQuery:id,_extractKeywords:Gi,_chunkText:ud,parseFrontmatter:Ts,tokenize:zo,_fuseResults:pd}});var Vi=H((cv,bd)=>{var Cs=require("fs"),Xi=require("path"),Ji=process.env.NEX_AUDIT!=="false",wn=null;function gd(){return wn||(wn=Xi.join(process.cwd(),".nex","audit"),Cs.existsSync(wn)||Cs.mkdirSync(wn,{recursive:!0}),wn)}function $d(){let t=new Date().toISOString().split("T")[0];return Xi.join(gd(),`${t}.jsonl`)}function ow(t){if(Ji)try{let e={timestamp:new Date().toISOString(),tool:t.tool,args:yd(t.args),resultLength:typeof t.result=="string"?t.result.length:0,resultPreview:typeof t.result=="string"?t.result.substring(0,200):"",duration:t.duration||0,success:t.success!==!1,model:t.model||null,provider:t.provider||null},n=JSON.stringify(e)+`
|
|
187
187
|
`;Cs.appendFileSync($d(),n,"utf-8")}catch{}}function yd(t){if(!t||typeof t!="object")return{};let e={};for(let[n,o]of Object.entries(t))/key|token|password|secret|credential/i.test(n)?e[n]="***":typeof o=="string"&&o.length>500?e[n]=o.substring(0,500)+`... (${o.length} chars)`:e[n]=o;return e}function wd(t={}){let e=gd(),n=t.days||1,o=[];for(let s=0;s<n;s++){let r=t.date||new Date(Date.now()-s*864e5).toISOString().split("T")[0],i=Xi.join(e,`${r}.jsonl`);if(!Cs.existsSync(i))continue;let a=Cs.readFileSync(i,"utf-8").split(`
|
|
188
|
-
`).filter(l=>l.trim());for(let l of a)try{let u=JSON.parse(l);if(t.tool&&u.tool!==t.tool)continue;o.push(u)}catch{}if(t.date)break}return o}function rw(t=1){let e=wd({days:t});if(e.length===0)return{totalCalls:0,byTool:{},avgDuration:0,successRate:1};let n={},o=0,s=0;for(let r of e)n[r.tool]=(n[r.tool]||0)+1,o+=r.duration||0,r.success&&s++;return{totalCalls:e.length,byTool:n,avgDuration:Math.round(o/e.length),successRate:s/e.length}}function iw(t){Ji=t}function aw(){return Ji}function cw(){wn=null}bd.exports={logToolExecution:ow,sanitizeArgs:yd,readAuditLog:wd,getAuditSummary:rw,setAuditEnabled:iw,isAuditEnabled:aw,getAuditLogPath:$d,_reset:cw}});var Od=H((dv,Ad)=>{var De=require("fs").promises,_d=require("fs"),ke=require("path"),ve=require("util").promisify(require("child_process").exec),Qi=require("util").promisify(require("child_process").execFile),{spawnSync:lw}=require("child_process"),Zi=require("axios"),{isForbidden:uw,isSSHForbidden:dw,isDangerous:fw,isCritical:xd,isBashPathForbidden:pw,confirm:Tt}=Xe(),{showClaudeDiff:Jo,showClaudeNewFile:mw,showEditDiff:lv,confirmFileChange:As}=Ll(),{C:te,Spinner:hw,getToolSpinnerText:gw}=
|
|
188
|
+
`).filter(l=>l.trim());for(let l of a)try{let u=JSON.parse(l);if(t.tool&&u.tool!==t.tool)continue;o.push(u)}catch{}if(t.date)break}return o}function rw(t=1){let e=wd({days:t});if(e.length===0)return{totalCalls:0,byTool:{},avgDuration:0,successRate:1};let n={},o=0,s=0;for(let r of e)n[r.tool]=(n[r.tool]||0)+1,o+=r.duration||0,r.success&&s++;return{totalCalls:e.length,byTool:n,avgDuration:Math.round(o/e.length),successRate:s/e.length}}function iw(t){Ji=t}function aw(){return Ji}function cw(){wn=null}bd.exports={logToolExecution:ow,sanitizeArgs:yd,readAuditLog:wd,getAuditSummary:rw,setAuditEnabled:iw,isAuditEnabled:aw,getAuditLogPath:$d,_reset:cw}});var Od=H((dv,Ad)=>{var De=require("fs").promises,_d=require("fs"),ke=require("path"),ve=require("util").promisify(require("child_process").exec),Qi=require("util").promisify(require("child_process").execFile),{spawnSync:lw}=require("child_process"),Zi=require("axios"),{isForbidden:uw,isSSHForbidden:dw,isDangerous:fw,isCritical:xd,isBashPathForbidden:pw,confirm:Tt}=Xe(),{showClaudeDiff:Jo,showClaudeNewFile:mw,showEditDiff:lv,confirmFileChange:As}=Ll(),{C:te,Spinner:hw,getToolSpinnerText:gw}=Oe(),{isGitRepo:ea,getCurrentBranch:kd,getStatus:$w,getDiff:yw}=tn(),{recordChange:Vo}=Wt(),{fuzzyFindText:vd,findMostSimilar:Ms}=mu(),{runDiagnostics:Os}=gu(),{findFileInIndex:ww,getFileIndex:uv}=Mi(),{resolveProfile:kt,sshExec:ct,scpUpload:bw,scpDownload:_w}=Kn(),{resolveDeployConfig:xw,loadDeployConfigs:kw}=Di(),{getEditMode:Sd}=Uo(),vw=/^(vim?|nano|emacs|pico|less|more|top|htop|iftop|iotop|glances|telnet\s|screen|tmux|fzf|gum|dialog|whiptail|man\s|node\s*$|python3?\s*$|irb\s*$|rails\s*c|psql\s|mysql\s|redis-cli|mongosh?|sqlite3)\b/,Sw=/^ssh\s/,Ew=/^ssh(?:\s+-\S+)*\s+\S+@?\S+\s+["']?[^-]/;async function ht(t){return De.access(t).then(()=>!0).catch(()=>!1)}async function Qo(t){if(!t)return{fixedPath:null,message:""};let e=t.replace(/\/+/g,"/").replace(/^~\//,`${require("os").homedir()}/`),n=lt(e);if(n&&await ht(n))return{fixedPath:n,message:`(auto-fixed path: ${t} \u2192 ${e})`};let o=[".js",".ts",".jsx",".tsx",".mjs",".cjs",".json"],s=ke.extname(t);if(!s)for(let i of o){let a=lt(t+i);if(a&&await ht(a))return{fixedPath:a,message:`(auto-fixed: added ${i} extension)`}}if(s){let i=t.replace(/\.[^.]+$/,"");for(let a of o){if(a===s)continue;let l=lt(i+a);if(l&&await ht(l))return{fixedPath:l,message:`(auto-fixed: ${s} \u2192 ${a})`}}}let r=ke.basename(t);if(r&&r.length>2)try{let i=ww(r).map(a=>lt(a));if(i.length===1)return{fixedPath:i[0],message:`(auto-fixed: found ${r} at ${ke.relative(process.cwd(),i[0])})`};if(i.length>1&&i.length<=5)return{fixedPath:null,message:`File not found. Did you mean one of:
|
|
189
189
|
${i.map(l=>ke.relative(process.cwd(),l)).map(l=>` - ${l}`).join(`
|
|
190
190
|
`)}`}}catch{}return{fixedPath:null,message:""}}function Tw(t){return/\bprintenv\b/.test(t)?"printenv exposes all secrets. Use `echo $VAR_NAME` for a single variable, or `env | grep PATTERN` for filtered output.":/cat\s+.*\.env\b/.test(t)?'Reading .env directly is blocked. Use `grep -v "KEY=" .env` to inspect non-secret entries, or ask the user to share specific values.':/cat\s+.*credentials/i.test(t)?"Credentials files are blocked. Reference the variable name from the application config instead.":/python3?\s+-c\s/.test(t)?"Inline python -c is blocked. Write a temporary script file and run it with `python3 script.py` instead.":/node\s+-e\s/.test(t)?"Inline node -e is blocked. Write a temporary script file and run it with `node script.js` instead.":/curl.*-X\s*POST|curl.*--data/.test(t)?"curl POST is blocked to prevent data exfiltration. Use the application's own API client or ask the user to run the request.":/base64.*\|.*bash/.test(t)?"Piping base64-decoded content to bash is blocked. Decode the content first, inspect it, then run explicitly.":/\beval\s*\(/.test(t)?"eval is blocked. Execute the command directly without eval.":/(?:^|[;&|]\s*)history(?:\s|$)/.test(t)?"Shell history is blocked. Look at git log or project files for context instead.":/\bsed\s+-n\s+['"]?\d+,\d+p/.test(t)?'sed -n line-range scrolling floods context with irrelevant lines. Use targeted grep instead: grep -n "ERROR\\|pattern" <logfile> | tail -20':""}function Rd(t,e){let n=[];if(/command not found|: not found|not recognized/i.test(t)){let o=e.match(/^(\S+)/),s=o?o[1]:"";/^(npx|npm|node|yarn|pnpm|bun)$/.test(s)?n.push("HINT: Node.js/npm may not be in PATH. Check your Node.js installation."):/^(python|python3|pip|pip3)$/.test(s)?n.push("HINT: Python may not be installed. Try: brew install python3 (macOS) or apt install python3 (Linux)"):n.push(`HINT: "${s}" is not installed. Try installing it with your package manager.`)}if(/Cannot find module|MODULE_NOT_FOUND/i.test(t)){let o=t.match(/Cannot find module '([^']+)'/),s=o?o[1]:"";s&&!s.startsWith(".")&&!s.startsWith("/")?n.push(`HINT: Missing npm package "${s}". Run: npm install ${s}`):n.push("HINT: Module not found. Check the import path or run npm install.")}if(/permission denied|EACCES/i.test(t)&&n.push("HINT: Permission denied. Check file permissions or try a different approach."),/EADDRINUSE|address already in use/i.test(t)){let o=t.match(/port (\d+)|:(\d+)/),s=o?o[1]||o[2]:"";n.push(`HINT: Port ${s||""} is already in use. Kill the process or use a different port.`)}if(/SyntaxError|Unexpected token/i.test(t)&&n.push("HINT: Syntax error in the code. Check the file at the line number shown above."),/TS\d{4}:/i.test(t)&&n.push("HINT: TypeScript compilation error. Fix the type issue at the indicated line."),/Test Suites:.*failed|Tests:.*failed/i.test(t)&&n.push("HINT: Test failures detected. Read the error output above to identify failing tests."),/fatal: not a git repository/i.test(t)&&n.push("HINT: Not inside a git repository. Run git init or cd to a git project."),/^curl\b/.test(e)){let o=t.match(/curl:\s*\((\d+)\)/),s=o?parseInt(o[1],10):null;s===6||/Could not resolve host/i.test(t)?n.push("HINT: Hostname could not be resolved. Check DNS or use an IP address directly."):s===7||/Failed to connect|Connection refused/i.test(t)?n.push("HINT: Service not running or port wrong. Check if the service is up and the port is correct."):s===22||/HTTP error/i.test(t)?n.push("HINT: HTTP 4xx/5xx response. The endpoint exists but returned an error status."):s===28||/timed out/i.test(t)?n.push("HINT: Request timed out. The host may be unreachable or the service is slow."):(s===35||/SSL.*error/i.test(t))&&n.push("HINT: SSL/TLS handshake failed. Try with --insecure to bypass, or check the certificate.")}if(/remote port forwarding failed/i.test(t)){let o=t.match(/port (\d+)/),s=o?o[1]:"";n.push(`HINT: SSH remote port forwarding failed for port ${s}. The port may already be bound on the server. Check with: ssh server "ss -tuln | grep ${s}" and kill any lingering process with that port.`)}return/bind.*Cannot assign requested address|Address already in use/i.test(t)&&n.push("HINT: Port is already in use. Find the process with: ss -tuln | grep <port> and kill it, then retry."),/Connection.*timed out|ssh.*timeout/i.test(t)&&/^ssh\b/.test(e)&&n.push("HINT: SSH connection timed out. Check if the host is reachable: ping <host> and verify the port with: nc -zv <host> 22"),/spawn \/bin\/sh ENOENT|spawn sh ENOENT/i.test(t)&&n.push("HINT: The working directory was deleted during this session \u2014 bash cannot execute commands in a non-existent cwd. Previous rm/delete commands succeeded. Use list_directory or glob to verify the state instead of retrying bash."),/cp.*\$f.*\$f\.bak.*sed.*-i\.bak|sed.*-i\.bak.*cp.*\$f.*\$f\.bak/i.test(e)&&n.push('HINT: Using both cp with .bak and sed -i.bak creates double backups (.bak.bak). Choose one method: either cp "$f" "$f.bak" OR sed -i.bak, not both.'),n.length===0?t:t+`
|
|
191
191
|
|
|
@@ -201,7 +201,7 @@ ${h}`}}case"read_file":{let o=lt(e.path);if(!o)return`ERROR: Access denied \u201
|
|
|
201
201
|
[File truncated: showing lines 1-${m} of ${f} total. Use line_start/line_end to read other sections, e.g. line_start=${m+1} line_end=${Math.min(m*2,f)}]`:!h&&f>m?`
|
|
202
202
|
[Large file (${f} lines total) \u2014 use line_start/line_end for other sections]`:"";return`${R}
|
|
203
203
|
${x}${_}`}case"write_file":{await ta();let o=lt(e.path);if(!o)return`ERROR: Access denied \u2014 path outside project: ${e.path}`;let s=await ht(o),r=null;if(n.autoConfirm)s&&(r=await De.readFile(o,"utf-8"));else if(s){r=await De.readFile(o,"utf-8");let d=await Os(o,e.content);if(Jo(o,r,e.content,{annotations:d}),!await As("Overwrite"))return"CANCELLED: User declined to overwrite file."}else{let d=await Os(o,e.content);if(mw(o,e.content,{annotations:d}),!await As("Create"))return"CANCELLED: User declined to create file."}let i=ke.dirname(o);await ht(i)||await De.mkdir(i,{recursive:!0}),await De.writeFile(o,e.content,"utf-8");let l=/[/\\]\.git[/\\]hooks[/\\]/.test(o)||o.endsWith(".sh")||e.content.startsWith("#!");l&&await De.chmod(o,493),Vo("write_file",o,r,e.content);let u=l?" [chmod +x applied]":"";return`Written: ${o} (${e.content.length} chars)${u}`}case"edit_file":{await ta();let o=lt(e.path);if(!o)return`ERROR: Access denied \u2014 path outside project: ${e.path}`;if(!await ht(o)){let d=await Qo(e.path);if(d.fixedPath)o=d.fixedPath,console.log(`${te.dim} \u2713 auto-fixed path: ${e.path} \u2192 ${ke.relative(process.cwd(),o)}${te.reset}`);else return`ERROR: File not found: ${e.path}${d.message?`
|
|
204
|
-
`+d.message:""}`}let r=await De.readFile(o,"utf-8"),i=e.old_text,a=!1,l=!1;if(!r.includes(e.old_text)){let{getActiveModelId:d,getActiveProviderName:f}=
|
|
204
|
+
`+d.message:""}`}let r=await De.readFile(o,"utf-8"),i=e.old_text,a=!1,l=!1;if(!r.includes(e.old_text)){let{getActiveModelId:d,getActiveProviderName:f}=Te();if(Sd(d(),f())==="strict"){let p=Ms(r,e.old_text);return p?`ERROR: old_text not found in ${o} (strict mode \u2014 exact match required)
|
|
205
205
|
Most similar text (line ${p.line}, distance ${p.distance}):
|
|
206
206
|
${p.text}`:`ERROR: old_text not found in ${o} (strict mode \u2014 exact match required)`}let h=vd(r,e.old_text);if(h)i=h,a=!0,console.log(`${te.dim} \u2713 fuzzy whitespace match applied${te.reset}`);else{let p=Cd(r,e.old_text,e.new_text);if(p){if(!n.autoConfirm){let R=await Os(o,p.content);if(Jo(o,r,p.content,{annotations:R}),!await As(`Apply (auto-fix, line ${p.line}, distance ${p.distance})`))return"CANCELLED: User declined to apply edit."}await De.writeFile(o,p.content,"utf-8"),(/[/\\]\.git[/\\]hooks[/\\]/.test(o)||o.endsWith(".sh")||p.content.startsWith("#!"))&&await De.chmod(o,493),Vo("edit_file",o,r,p.content);let k=p.matchText.length>80?p.matchText.substring(0,77)+"...":p.matchText;return console.log(`${te.dim} \u2713 auto-fixed edit: line ${p.line}, distance ${p.distance}${te.reset}`),`Edited: ${o} (auto-fixed, line ${p.line}, distance ${p.distance}, matched: "${k}")`}let g=Ms(r,e.old_text);if(g){let k=r.split(`
|
|
207
207
|
`),R=Math.max(0,g.line-6),x=Math.min(k.length,g.line+10),_=k.slice(R,x).map((O,N)=>`${R+N+1}: ${O}`).join(`
|
|
@@ -227,7 +227,7 @@ Recovery: use grep -n to locate the text, then re-read that section with line_st
|
|
|
227
227
|
`).filter(m=>m.trim());else{let m=l.split("\0");u=[];for(let h=0;h<m.length;h+=2){let p=m[h],g=m[h+1];if(p&&g){let y=g.split(`
|
|
228
228
|
`).filter(w=>w.trim());for(let w of y)u.push(`${p}:${w}`)}}}let d=e.offset||0,f=e.head_limit||(e.output_mode==="files_with_matches"?200:100);return u=u.slice(d,d+f),a.update({count:u.length,detail:`in ${o}`}),a.stop(),u.join(`
|
|
229
229
|
`).trim()||"(no matches)"}catch(l){return a.stop(),l.code===2?`ERROR: Invalid regex pattern: ${e.pattern}`:"(no matches)"}}case"patch_file":{await ta();let o=lt(e.path);if(!o)return`ERROR: Access denied \u2014 path outside project: ${e.path}`;if(!await ht(o)){let w=await Qo(e.path);if(w.fixedPath)o=w.fixedPath,console.log(`${te.dim} \u2713 auto-fixed path: ${e.path} \u2192 ${ke.relative(process.cwd(),o)}${te.reset}`);else return`ERROR: File not found: ${e.path}${w.message?`
|
|
230
|
-
`+w.message:""}`}let r=e.patches;if(!Array.isArray(r)||r.length===0)return"ERROR: No patches provided";let i=await De.readFile(o,"utf-8"),{getActiveModelId:a,getActiveProviderName:l}=
|
|
230
|
+
`+w.message:""}`}let r=e.patches;if(!Array.isArray(r)||r.length===0)return"ERROR: No patches provided";let i=await De.readFile(o,"utf-8"),{getActiveModelId:a,getActiveProviderName:l}=Te(),u=Sd(a(),l()),d=[],f=!1,m=!1;for(let w=0;w<r.length;w++){let{old_text:k,new_text:R}=r[w];if(i.includes(k))d.push({old_text:k,new_text:R});else if(u==="strict"){let x=Ms(i,k);return x?`ERROR: Patch ${w+1} old_text not found in ${o} (strict mode \u2014 exact match required)
|
|
231
231
|
Most similar text (line ${x.line}, distance ${x.distance}):
|
|
232
232
|
${x.text}`:`ERROR: Patch ${w+1} old_text not found in ${o} (strict mode \u2014 exact match required)`}else{let x=vd(i,k);if(x)d.push({old_text:x,new_text:R}),f=!0;else{let _=Ms(i,k);if(_){let b=Math.max(3,Math.ceil(k.length*.05));if(_.distance<=b)d.push({old_text:_.text,new_text:R}),m=!0;else return`ERROR: Patch ${w+1} old_text not found in ${o}
|
|
233
233
|
Most similar text (line ${_.line}, distance ${_.distance}):
|
|
@@ -257,12 +257,12 @@ ${s.map((l,u)=>` ${u+1}. ${l}`).join(`
|
|
|
257
257
|
`:"";console.log(`
|
|
258
258
|
${te.cyan}${te.bold} ? ${o}${te.reset}${a}`),i.question(`${te.cyan} > ${te.reset}`,l=>{bn=null,i.close(),r(l.trim()||"(no response)")})})}case"git_status":{if(!await ea())return"ERROR: Not a git repository";let o=await kd()||"(detached)",s=await $w();if(s.length===0)return`Branch: ${o}
|
|
259
259
|
Clean working tree (no changes)`;let r=[`Branch: ${o}`,`Changed files (${s.length}):`];for(let i of s){let a=i.status==="M"?"modified":i.status==="A"?"added":i.status==="D"?"deleted":i.status==="??"?"untracked":i.status;r.push(` ${a}: ${i.file}`)}return r.join(`
|
|
260
|
-
`)}case"git_diff":{if(!await ea())return"ERROR: Not a git repository";let o;if(e.file){let s=["diff"];e.staged&&s.push("--cached"),s.push("--",e.file);try{o=execFileSync("git",s,{cwd:process.cwd(),encoding:"utf-8",timeout:15e3,stdio:"pipe"}).trim()}catch{o=""}}else o=await yw(!!e.staged);return o||"(no diff)"}case"git_log":{if(!await ea())return"ERROR: Not a git repository";let s=["log","--oneline",`-${Math.min(e.count||10,50)}`];e.file&&s.push("--",e.file);try{return execFileSync("git",s,{cwd:process.cwd(),encoding:"utf-8",timeout:15e3,stdio:"pipe"}).trim()||"(no commits)"}catch{return"(no commits)"}}case"task_list":{let{createTasks:o,updateTask:s,getTaskList:r,renderTaskList:i,hasActiveTasks:a}=Bo(),{getActiveTaskProgress:l}=
|
|
260
|
+
`)}case"git_diff":{if(!await ea())return"ERROR: Not a git repository";let o;if(e.file){let s=["diff"];e.staged&&s.push("--cached"),s.push("--",e.file);try{o=execFileSync("git",s,{cwd:process.cwd(),encoding:"utf-8",timeout:15e3,stdio:"pipe"}).trim()}catch{o=""}}else o=await yw(!!e.staged);return o||"(no diff)"}case"git_log":{if(!await ea())return"ERROR: Not a git repository";let s=["log","--oneline",`-${Math.min(e.count||10,50)}`];e.file&&s.push("--",e.file);try{return execFileSync("git",s,{cwd:process.cwd(),encoding:"utf-8",timeout:15e3,stdio:"pipe"}).trim()||"(no commits)"}catch{return"(no commits)"}}case"task_list":{let{createTasks:o,updateTask:s,getTaskList:r,renderTaskList:i,hasActiveTasks:a}=Bo(),{getActiveTaskProgress:l}=Oe(),u=l();switch(e.action){case"create":{if(!e.name||!e.tasks)return"ERROR: task_list create requires name and tasks";let d=o(e.name,e.tasks);return u||console.log(`
|
|
261
261
|
`+i()),`Created task list "${e.name}" with ${d.length} tasks:
|
|
262
262
|
`+d.map(f=>` ${f.id}: ${f.description}`).join(`
|
|
263
263
|
`)}case"update":return!e.task_id||!e.status?"ERROR: task_list update requires task_id and status":s(e.task_id,e.status,e.result)?(u||console.log(`
|
|
264
264
|
`+i()),`Updated ${e.task_id}: ${e.status}${e.result?" \u2014 "+e.result:""}`):`ERROR: Task not found: ${e.task_id}`;case"get":{let d=r();return d.tasks.length===0?"No active tasks":(u||console.log(`
|
|
265
|
-
`+i()),JSON.stringify(d,null,2))}default:return`ERROR: Unknown task_list action: ${e.action}. Use: create, update, get`}}case"spawn_agents":{let{executeSpawnAgents:o}=Yo();return o(e)}case"switch_model":{let{setActiveModel:o,getActiveProviderName:s,getActiveModelId:r}=
|
|
265
|
+
`+i()),JSON.stringify(d,null,2))}default:return`ERROR: Unknown task_list action: ${e.action}. Use: create, update, get`}}case"spawn_agents":{let{executeSpawnAgents:o}=Yo();return o(e)}case"switch_model":{let{setActiveModel:o,getActiveProviderName:s,getActiveModelId:r}=Te();return o(e.model)?`Switched to ${s()}:${r()}`:`ERROR: Unknown model: ${e.model}. Use /providers to see available models.`}case"gh_run_list":{let o=Math.min(e.limit||10,30),s=["run","list","--limit",String(o),"--json","databaseId,status,conclusion,name,headBranch,createdAt,updatedAt,event"];e.workflow&&s.push("--workflow",e.workflow),e.branch&&s.push("--branch",e.branch),e.status&&s.push("--status",e.status);try{let{stdout:r}=await ve(`gh ${s.join(" ")}`,{cwd:process.cwd(),timeout:3e4}),i=JSON.parse(r||"[]");return i.length===0?"No workflow runs found.":i.map(l=>{let u=l.conclusion||l.status||"unknown",d=u==="success"?"\u2713":u==="failure"?"\u2717":u==="in_progress"?"\u283F":"\u25CB",f=l.updatedAt?new Date(l.updatedAt).toISOString().slice(0,16).replace("T"," "):"";return`${d} [${l.databaseId}] ${l.name} \xB7 ${l.headBranch} \xB7 ${u} \xB7 ${f}`}).join(`
|
|
266
266
|
`)}catch(r){let i=(r.stderr||r.message||"").toString();return i.includes("not found")||i.includes("not logged")?"ERROR: gh CLI not found or not authenticated. Run: gh auth login":`ERROR: ${i.split(`
|
|
267
267
|
`)[0]}`}}case"gh_run_view":{if(!e.run_id)return"ERROR: run_id is required";try{if(e.log){let{stdout:i}=await ve(`gh run view ${e.run_id} --log`,{cwd:process.cwd(),timeout:6e4,maxBuffer:5242880});return i.substring(0,8e3)+(i.length>8e3?`
|
|
268
268
|
...(truncated)`:"")}let{stdout:o}=await ve(`gh run view ${e.run_id} --json status,conclusion,name,headBranch,createdAt,updatedAt,jobs`,{cwd:process.cwd(),timeout:3e4}),s=JSON.parse(o),r=[`Run: ${s.name} [${e.run_id}]`,`Branch: ${s.headBranch} Status: ${s.conclusion||s.status}`,`Started: ${s.createdAt} Finished: ${s.updatedAt||"\u2014"}`,"","Jobs:"];for(let i of s.jobs||[]){let a=i.conclusion==="success"?"\u2713":i.conclusion==="failure"?"\u2717":"\u25CB";r.push(` ${a} ${i.name} (${i.conclusion||i.status})`);for(let l of i.steps||[]){if(l.conclusion==="failure"||l.conclusion==="skipped")continue;let u=l.conclusion==="success"?" \u2713":l.conclusion==="failure"?" \u2717":" \u25CB";r.push(` ${u} ${l.name}`)}}return r.join(`
|
|
@@ -442,7 +442,7 @@ ${u}
|
|
|
442
442
|
- \u274C Do NOT \`read_file\` on paths like \`logs/\` \u2014 these files do not exist locally
|
|
443
443
|
- \u274C Do NOT \`list_directory\` on server paths \u2014 the local project is the source, not the running instance
|
|
444
444
|
|
|
445
|
-
**When in doubt:** If a path contains \`logs/\`, \`/var/log/\`, or \`/home/<user>/\` \u2014 it is on the server. SSH there.`}Md.exports={getServerContext:Mw,getDeploymentContextBlock:Lw,hasServerOS:Pw,getProfileNames:Iw,OS_HINTS:sa}});var nr=H((mv,Ld)=>{var Rt=require("fs").promises,Pd=require("fs"),et=require("path"),er=require("util").promisify(require("child_process").exec),{C:zn}=
|
|
445
|
+
**When in doubt:** If a path contains \`logs/\`, \`/var/log/\`, or \`/home/<user>/\` \u2014 it is on the server. SSH there.`}Md.exports={getServerContext:Mw,getDeploymentContextBlock:Lw,hasServerOS:Pw,getProfileNames:Iw,OS_HINTS:sa}});var nr=H((mv,Ld)=>{var Rt=require("fs").promises,Pd=require("fs"),et=require("path"),er=require("util").promisify(require("child_process").exec),{C:zn}=Oe(),{getMergeConflicts:Id}=tn(),{getServerContext:Dw}=oa(),jw=new Set(["node_modules",".git",".svn","dist","build","coverage",".nyc_output","__pycache__",".DS_Store",".next",".nuxt",".turbo",".cache","vendor","tmp","temp"]);function qw(t){try{return Pd.readFileSync(t,"utf-8").split(`
|
|
446
446
|
`).map(n=>n.trim()).filter(n=>n&&!n.startsWith("#")&&!n.startsWith("!")).map(n=>n.replace(/\/$/,""))}catch{return[]}}function Fw(t,e){for(let n of e)if(n===t||n.includes("*")&&new RegExp("^"+n.replace(/\./g,"\\.").replace(/\*/g,".*")+"$").test(t))return!0;return!1}function Uw(t,{maxDepth:e=3,maxFiles:n=200,giPatterns:o=[]}={}){let s=et.join(t,".gitignore"),r=[...o,...qw(s)],i=0,a=[et.basename(t)+"/"];function l(u,d,f){if(f>e||i>=n)return;let m;try{m=Pd.readdirSync(u,{withFileTypes:!0})}catch{return}m.sort((p,g)=>p.isDirectory()!==g.isDirectory()?p.isDirectory()?-1:1:p.name.localeCompare(g.name));let h=m.filter(p=>!(jw.has(p.name)||p.name.startsWith(".")&&p.name!==".env.example"||Fw(p.name,r)));for(let p=0;p<h.length;p++){if(i>=n){a.push(`${d}\u2514\u2500\u2500 \u2026 (truncated)`);break}let g=h[p],y=p===h.length-1,w=y?"\u2514\u2500\u2500 ":"\u251C\u2500\u2500 ",k=d+(y?" ":"\u2502 "),R=g.isDirectory()?g.name+"/":g.name;a.push(`${d}${w}${R}`),i++,g.isDirectory()&&l(et.join(u,g.name),k,f+1)}}return l(t,"",1),a.join(`
|
|
447
447
|
`)}var ra=new Map,Xn=new Map,tr=null,Ww=3e4;async function sn(t){try{return await t()}catch{return null}}async function Bw(){if(!tr||Date.now()>tr)return!1;let t=[et.join(process.cwd(),"package.json"),et.join(process.cwd(),"README.md"),et.join(process.cwd(),".gitignore")];for(let e of t)try{let n=await Rt.stat(e),o=Xn.get(e);if(!o||n.mtimeMs!==o)return!1}catch{if(Xn.has(e))return!1}try{let e=et.join(process.cwd(),".git","HEAD"),n=await Rt.stat(e),o=Xn.get(e);if(!o||n.mtimeMs!==o)return!1}catch{}return!0}async function Hw(){let t=[et.join(process.cwd(),"package.json"),et.join(process.cwd(),"README.md"),et.join(process.cwd(),".gitignore"),et.join(process.cwd(),".git","HEAD")];for(let e of t)try{let n=await Rt.stat(e);Xn.set(e,n.mtimeMs)}catch{Xn.delete(e)}}async function Gw(t){let e="fileContext",n=ra.get(e),o=!1;if(n&&await Bw()&&(o=!0),!o){let u=[],d=et.join(t,"package.json");if(await sn(()=>Rt.access(d).then(()=>!0).catch(()=>!1)))try{let y=await Rt.readFile(d,"utf-8"),w=JSON.parse(y),k={name:w.name,version:w.version};w.scripts&&(k.scripts=Object.keys(w.scripts).slice(0,15)),w.dependencies&&(k.deps=Object.keys(w.dependencies).length),w.devDependencies&&(k.devDeps=Object.keys(w.devDependencies).length),u.push(`PACKAGE: ${JSON.stringify(k)}`)}catch{}let m=et.join(t,"README.md");if(await sn(()=>Rt.access(m).then(()=>!0).catch(()=>!1))){let w=(await Rt.readFile(m,"utf-8")).split(`
|
|
448
448
|
`).slice(0,50);u.push(`README (first 50 lines):
|
|
@@ -456,7 +456,7 @@ ${a}`),l&&l.length>0){let u=l.map(d=>` ${d.file}`).join(`
|
|
|
456
456
|
`);s.push(`MERGE CONFLICTS (resolve before editing these files):
|
|
457
457
|
${u}`)}try{let u=Dw();u&&s.push(u)}catch{}return s.join(`
|
|
458
458
|
|
|
459
|
-
`)}async function Kw(t){let e=et.join(t,"package.json"),n="";if(await sn(()=>Rt.access(e).then(()=>!0).catch(()=>!1)))try{let i=await Rt.readFile(e,"utf-8"),a=JSON.parse(i);n=`${a.name||"?"} v${a.version||"?"}`}catch{}let[s,r]=await Promise.all([sn(async()=>{let{stdout:i}=await er("git branch --show-current",{cwd:t,timeout:5e3});return i.trim()}),Id()]);if(r&&r.length>0){console.log(`${zn.red} \u26A0 ${r.length} unresolved merge conflict(s):${zn.reset}`);for(let i of r)console.log(`${zn.red} ${i.file}${zn.reset}`);console.log(`${zn.yellow} \u2192 Resolve conflicts before starting tasks${zn.reset}`)}console.log()}Ld.exports={gatherProjectContext:Gw,printContext:Kw,generateFileTree:Uw,_clearContextCache:()=>{ra.clear(),Xn.clear(),tr=null}}});var At=H((hv,qd)=>{var Ct=require("fs"),sr=require("path"),{atomicWrite:Yw}=Zt();function Is(){return sr.join(process.cwd(),".nex","sessions")}function ia(){let t=Is();Ct.existsSync(t)||Ct.mkdirSync(t,{recursive:!0})}function aa(t){let e=t.replace(/[^a-zA-Z0-9_-]/g,"_").substring(0,100);return sr.join(Is(),`${e}.json`)}function ca(t,e,n={}){ia();let o=aa(t),s={name:t,createdAt:n.createdAt||new Date().toISOString(),updatedAt:new Date().toISOString(),messageCount:e.length,model:n.model||null,provider:n.provider||null,messages:e};return Yw(o,JSON.stringify(s,null,2)),{path:o,name:t}}function Dd(t){let e=aa(t);if(!Ct.existsSync(e))return null;try{return JSON.parse(Ct.readFileSync(e,"utf-8"))}catch{return null}}function jd(){ia();let t=Is(),e=Ct.readdirSync(t).filter(o=>o.endsWith(".json")),n=[];for(let o of e)try{let s=JSON.parse(Ct.readFileSync(sr.join(t,o),"utf-8"));n.push({name:s.name||o.replace(".json",""),createdAt:s.createdAt,updatedAt:s.updatedAt,messageCount:s.messageCount||0,model:s.model,provider:s.provider,score:s.score!=null?s.score:null,scoreGrade:s.scoreGrade||null})}catch{}return n.sort((o,s)=>(s.updatedAt||"").localeCompare(o.updatedAt||""))}function zw(t){let e=aa(t);return Ct.existsSync(e)?(Ct.unlinkSync(e),!0):!1}function Xw(){let t=jd();return t.length===0?null:Dd(t[0].name)}var _n=null,Kt=null,Ps=null;function Jw(t,e={}){t.length!==0&&(_n&&clearTimeout(_n),Kt=t,Ps=e||{},_n=setTimeout(()=>{Kt&&Kt.length>0&&ca("_autosave",Kt,Ps),_n=null,Kt=null,Ps=null},5e3))}function Vw(){_n&&(clearTimeout(_n),_n=null),Kt&&Kt.length>0&&(ca("_autosave",Kt,Ps),Kt=null,Ps=null)}function Qw(){ia();let t=Is(),e=Ct.readdirSync(t).filter(r=>r.endsWith(".json")),n=new Date,o=new Date(n.getTime()-720*60*60*1e3),s=0;for(let r of e){let i=sr.join(t,r);try{let a=Ct.statSync(i);new Date(a.mtime)<o&&(Ct.unlinkSync(i),s++)}catch{}}return s}qd.exports={saveSession:ca,loadSession:Dd,listSessions:jd,deleteSession:zw,getLastSession:Xw,autoSave:Jw,flushAutoSave:Vw,clearOldSessions:Qw,_getSessionsDir:Is}});var or=H((gv,Yd)=>{"use strict";var Ls=require("fs"),Fd=require("path");function Ud(t){let e=[];return t.forEach((n,o)=>{n.role==="assistant"&&(Array.isArray(n.content)&&n.content.forEach(s=>{s&&s.type==="tool_use"&&e.push({name:s.name||"",input:s.input||{},index:o})}),Array.isArray(n.tool_calls)&&n.tool_calls.forEach(s=>{let r=s.function?.name||s.name||"",i={};try{i=typeof s.function?.arguments=="string"?JSON.parse(s.function.arguments):s.function?.arguments||s.input||{}}catch{}e.push({name:r,input:i,index:o})}))}),e}function Wd(t){let e=[];return t.forEach((n,o)=>{if(n.role==="user"&&Array.isArray(n.content)&&n.content.forEach(s=>{if(s&&s.type==="tool_result"){let r=typeof s.content=="string"?s.content:Array.isArray(s.content)?s.content.map(i=>typeof i=="string"?i:i.text||"").join(""):JSON.stringify(s.content||"");e.push({content:r,index:o})}}),n.role==="tool"){let s=typeof n.content=="string"?n.content:JSON.stringify(n.content||"");e.push({content:s,index:o})}}),e}function Bd(t){for(let e=t.length-1;e>=0;e--){let n=t[e];if(n.role==="assistant"){if(typeof n.content=="string")return n.content.trim();if(Array.isArray(n.content)){let o=n.content.filter(s=>s&&(s.type==="text"||typeof s=="string")).map(s=>typeof s=="string"?s:s.text||"").join("").trim();if(o)return o}}}return""}function Hd(t,e){let n=[];for(let o=t.length-1;o>=0&&n.length<e;o--){let s=t[o];if(s.role!=="assistant")continue;let r="";typeof s.content=="string"?r=s.content.trim():Array.isArray(s.content)&&(r=s.content.filter(i=>i&&(i.type==="text"||typeof i=="string")).map(i=>typeof i=="string"?i:i.text||"").join("").trim()),r&&n.push(r)}return n}function Gd(t){let e=new Map;for(let n of t){let o;try{o=JSON.stringify(n.input)}catch{o=String(n.input)}let s=`${n.name}|${o}`;e.set(s,(e.get(s)||0)+1)}return e}function Kd(t){if(!Array.isArray(t)||t.length===0)return{score:0,issues:["Empty or invalid session \u2014 no messages to analyse"],summary:"No messages found"};let e=10,n=[],o=Ud(t),s=Wd(t),r=o.length;t.some(E=>E.role==="user"&&typeof E.content=="string"&&E.content.startsWith("[SYSTEM WARNING]")&&(E.content.includes("edited")||E.content.includes("bash command")||E.content.includes("grep pattern")||E.content.includes("re-read")||E.content.includes("already in your context")))&&(e-=2,n.push("Loop-warning was fired during session (repeated file edits, bash commands, or re-reads)"));let a=o.find(E=>{let M=E.input?.command||E.input?.cmd||"";return/\bsed\s+-n\b/.test(M)});if(a){let E=(a.input?.command||a.input?.cmd||"").slice(0,80);e-=1.5,n.push(`sed -n anti-pattern used: ${E}`)}o.find(E=>{if(E.name!=="grep"&&E.name!=="bash"&&E.name!=="ssh_exec")return!1;let M=E.input?.command||E.input?.cmd||"",U=E.input?.pattern||"",G=`${M} ${U}`;return/(?:-[CAB]|--context|--after|--before)\s*[=\s]?([2-9][1-9]|\d{3,})/.test(G)||/grep.*-[CAB]\s*([2-9][1-9]|\d{3,})/.test(G)})&&(e-=1,n.push("grep used with >20 context lines (context flood risk)"));let u=t.some(E=>E.role==="assistant"),d=Bd(t),m=Hd(t,3).some(E=>E.length>100&&!/^[^.!]{0,40}\?$/.test(E));if(u&&!m&&(d.length<80||/^[^.!]{0,40}\?$/.test(d))){e-=2;let E=d.length>0?`"${d.slice(0,60)}..."`:"(no assistant text found)";n.push(`Session ends without diagnosis \u2014 last response too short or is only a question: ${E}`)}r>40?(e-=1.5,n.push(`Excessive tool calls: ${r} (>40 threshold)`)):r>25&&(e-=.5,n.push(`High tool call count: ${r} (>25 threshold)`)),t.some(E=>{let M=typeof E.content=="string"?E.content:Array.isArray(E.content)?E.content.map(U=>typeof U=="string"?U:U.text||"").join(""):"";return/\[auto-compressed|context compacted|force-compressed/.test(M)})&&(e-=.5,n.push("Auto-compress triggered (context flood indicator)"));let g=Gd(o),y=0,w="";for(let[E,M]of g)M>y&&(y=M,w=E);if(y>=3){let[E]=w.split("|");e-=1,n.push(`Same tool call repeated ${y}\xD7 (tool: ${E})`)}let k=!1;for(let E of s)if(E.content&&E.content.includes('"valid":true')&&o.filter(U=>U.index>E.index).length>0){k=!0;break}k&&(e-=1.5,n.push('Stop-trigger ignored: tool result contained "valid":true but session continued with more tool calls'));let R=o.filter(E=>E.name==="ssh_exec");if(R.length>=8){let E=0,M=1;for(let U=1;U<R.length;U++)R[U].index<=R[U-1].index+2?M++:(E=Math.max(E,M),M=1);E=Math.max(E,M),E>=8&&(e-=.5,n.push(`SSH reconnect storm: ${E} consecutive SSH calls`))}let x=new Map;for(let E of o)if(E.name==="read_file"&&E.input?.path){let M=E.input.path;x.has(M)||x.set(M,{count:0,ranges:[]});let U=x.get(M);if(U.count++,E.input.line_start!=null){let G=E.input.line_start||1,ne=E.input.line_end||G+350;U.ranges.push([G,ne])}}function _(E,M,U){for(let[G,ne]of U){let I=Math.max(E,G),W=Math.min(M,ne);if(W>I){let se=W-I,X=M-E||1;if(se/X>=.7)return!0}}return!1}let b=0,O="";for(let[E,M]of x){if(M.count<3)continue;if(M.ranges.length===M.count){let G=!1,ne=[];for(let[I,W]of M.ranges){if(ne.length>0&&_(I,W,ne)){G=!0;break}ne.push([I,W])}if(!G)continue}M.count>b&&(b=M.count,O=E)}if(b>=3){e-=1;let E=O.split("/").slice(-2).join("/");n.push(`read_file loop: "${E}" read ${b}\xD7 (file already in context)`)}let N=0,C="";for(let[E,M]of x){if(M.ranges.length<4)continue;let U=[],G=!1;for(let[ne,I]of M.ranges){if(U.length>0&&_(ne,I,U)){G=!0;break}U.push([ne,I])}!G&&M.ranges.length>N&&(N=M.ranges.length,C=E)}if(N>=4){e-=.5;let E=C.split("/").slice(-2).join("/");n.push(`File-scroll pattern: "${E}" read in ${N} sequential sections \u2014 use grep instead`)}let L=new Map;for(let E of o)if(E.name==="grep"&&E.input?.path&&E.input?.pattern){let M=E.input.path;L.has(M)||L.set(M,new Set),L.get(M).add(E.input.pattern)}let Ee=0,me="";for(let[E,M]of L)M.size>Ee&&(Ee=M.size,me=E);if(Ee>=3){e-=.75;let E=me.split("/").slice(-2).join("/");n.push(`grep flood on single file: "${E}" searched ${Ee}\xD7 with different patterns (file already in context)`)}{let E=new Set,M=new Set,U=/^(test_|demo_|temp_|tmp_|scratch_)/;for(let ne of o){if(ne.name==="write_file"&&ne.input?.path){let I=ne.input.path.split("/").pop(),W=ne.input.path.includes("/tests/");U.test(I)&&!W&&E.add(ne.input.path)}if((ne.name==="bash"||ne.name==="ssh_exec")&&ne.input?.command){let I=ne.input.command.match(/\brm\s+(?:-\w+\s+)?(\S+)/g);if(I)for(let W of I){let se=W.split(/\s+/),X=se[se.length-1];for(let Q of E)(Q.endsWith(X)||X.endsWith(Q.split("/").pop()))&&M.add(Q)}}}let G=M.size;if(G>=1){let ne=Math.min(G*.25,.5);e-=ne;let I=[...M].map(W=>W.split("/").pop()).join(", ");n.push(`Temp file write-then-delete: ${I} \u2014 write inline logic or use tests/ instead`)}}let ae=s.filter(E=>E.content.startsWith("EXIT")).length;ae>=10?(e-=1,n.push(`Bash exit-error storm: ${ae} tool results started with EXIT (repeated failing commands)`)):ae>=5&&(e-=.5,n.push(`Repeated bash errors: ${ae} tool results with non-zero exit code`));for(let E of t){if(E.role!=="assistant")continue;let M="";if(typeof E.content=="string"?M=E.content:Array.isArray(E.content)&&(M=E.content.filter(X=>X&&(X.type==="text"||typeof X=="string")).map(X=>typeof X=="string"?X:X.text||"").join("")),M.length<=5e3)continue;let U=M.split(/(?<=\. )/).filter(X=>X.trim().length>0);if(U.length<6)continue;let G=new Map;for(let X=0;X<=U.length-3;X++){let Q=U.slice(X,X+3).join("").trim();Q.length>30&&G.set(Q,(G.get(Q)||0)+1)}let ne=0,I="";for(let[X,Q]of G)Q>ne&&(ne=Q,I=X);if(ne<3)continue;let se=I.length*ne/M.length;if(se>=.4||ne>=10){e-=1.5,n.push(`llm output loop: assistant message repeated content detected (${ne}\xD7 same paragraph, ${Math.round(se*100)}% repeated)`);break}}{let E=new Set(["read_file","list_directory","search_files","glob","grep"]),M=t.some(G=>Array.isArray(G.tool_calls)?G.tool_calls.some(ne=>E.has(ne.function?.name)):Array.isArray(G.content)?G.content.some(ne=>ne.type==="tool_use"&&E.has(ne.name)):!1);t.some(G=>G.role==="assistant"&&typeof G.content=="string"&&(G.content.includes("## Steps")||G.content.includes("/plan approve")))&&!M&&(e-=2,n.push("plan written without reading any files \u2014 LLM invented data structures from training knowledge (hallucination risk)"))}let ie=s.filter(E=>E.content.startsWith("BLOCKED:"));if(ie.length>0){let E=Math.min(ie.length*.5,1.5);e-=E,n.push(`${ie.length} tool call${ie.length===1?"":"s"} blocked (agent attempted denied actions)`)}let oe=t.filter(E=>{let M=typeof E.content=="string"?E.content:"";return/\[SYSTEM WARNING\] Context wiped \d+×/.test(M)}).length;if(oe>0){let E=Math.min(oe*1,2);e-=E,n.push(`Super-nuclear context wipe fired ${oe}\xD7 (context collapse \u2014 task too large or read loops)`)}{let E=!1,M=!1,U=!1;for(let ne of o){if(ne.name!=="bash")continue;let I=(ne.input?.command||ne.input?.cmd||"").trim();!(/cat\s*>/.test(I)||/<</.test(I))&&/\bcat\s+\S/.test(I)&&(E=!0),/^\s*ls(\s|$)/.test(I)&&!/npm|yarn|pnpm|make|git\b/.test(I)&&(M=!0),/\bfind\s+\S/.test(I)&&!/git\b|npm\b|-exec\b/.test(I)&&(U=!0)}let G=[E,M,U].filter(Boolean).length;if(G>0){let ne=Math.min(G*.25,.75);e-=ne;let I=[];E&&I.push("cat (use read_file)"),M&&I.push("ls (use list_directory)"),U&&I.push("find (use glob)"),n.push(`bash used instead of dedicated tool: ${I.join(", ")}`)}}e=Math.max(0,Math.min(10,e)),e=Math.round(e*10)/10;let ue=e>=9?"A":e>=8?"B":e>=7?"C":e>=6?"D":"F",Z=n.length===0?`Clean session \u2014 no quality issues detected (${r} tool calls)`:`${n.length} issue${n.length===1?"":"s"} found \u2014 ${r} tool calls`;return{score:e,grade:ue,issues:n,summary:Z}}function Zw(t){try{let{loadSession:e}=At(),n=e(t);return n?Kd(n.messages||[]):null}catch{return null}}function e0(t,e=null){let{score:n,grade:o,issues:s,summary:r}=t,i=e?.dim||"",a=e?.reset||"",l=e?.green||"",u=e?.yellow||"",d=e?.red||"",f=e?.cyan||"",m=e?.bold||"",h=n>=8?l:n>=6?u:d,p=`
|
|
459
|
+
`)}async function Kw(t){let e=et.join(t,"package.json"),n="";if(await sn(()=>Rt.access(e).then(()=>!0).catch(()=>!1)))try{let i=await Rt.readFile(e,"utf-8"),a=JSON.parse(i);n=`${a.name||"?"} v${a.version||"?"}`}catch{}let[s,r]=await Promise.all([sn(async()=>{let{stdout:i}=await er("git branch --show-current",{cwd:t,timeout:5e3});return i.trim()}),Id()]);if(r&&r.length>0){console.log(`${zn.red} \u26A0 ${r.length} unresolved merge conflict(s):${zn.reset}`);for(let i of r)console.log(`${zn.red} ${i.file}${zn.reset}`);console.log(`${zn.yellow} \u2192 Resolve conflicts before starting tasks${zn.reset}`)}console.log()}Ld.exports={gatherProjectContext:Gw,printContext:Kw,generateFileTree:Uw,_clearContextCache:()=>{ra.clear(),Xn.clear(),tr=null}}});var At=H((hv,qd)=>{var Ct=require("fs"),sr=require("path"),{atomicWrite:Yw}=Zt();function Is(){return sr.join(process.cwd(),".nex","sessions")}function ia(){let t=Is();Ct.existsSync(t)||Ct.mkdirSync(t,{recursive:!0})}function aa(t){let e=t.replace(/[^a-zA-Z0-9_-]/g,"_").substring(0,100);return sr.join(Is(),`${e}.json`)}function ca(t,e,n={}){ia();let o=aa(t),s={name:t,createdAt:n.createdAt||new Date().toISOString(),updatedAt:new Date().toISOString(),messageCount:e.length,model:n.model||null,provider:n.provider||null,messages:e};return Yw(o,JSON.stringify(s,null,2)),{path:o,name:t}}function Dd(t){let e=aa(t);if(!Ct.existsSync(e))return null;try{return JSON.parse(Ct.readFileSync(e,"utf-8"))}catch{return null}}function jd(){ia();let t=Is(),e=Ct.readdirSync(t).filter(o=>o.endsWith(".json")),n=[];for(let o of e)try{let s=JSON.parse(Ct.readFileSync(sr.join(t,o),"utf-8"));n.push({name:s.name||o.replace(".json",""),createdAt:s.createdAt,updatedAt:s.updatedAt,messageCount:s.messageCount||0,model:s.model,provider:s.provider,score:s.score!=null?s.score:null,scoreGrade:s.scoreGrade||null})}catch{}return n.sort((o,s)=>(s.updatedAt||"").localeCompare(o.updatedAt||""))}function zw(t){let e=aa(t);return Ct.existsSync(e)?(Ct.unlinkSync(e),!0):!1}function Xw(){let t=jd();return t.length===0?null:Dd(t[0].name)}var _n=null,Kt=null,Ps=null;function Jw(t,e={}){t.length!==0&&(_n&&clearTimeout(_n),Kt=t,Ps=e||{},_n=setTimeout(()=>{Kt&&Kt.length>0&&ca("_autosave",Kt,Ps),_n=null,Kt=null,Ps=null},5e3))}function Vw(){_n&&(clearTimeout(_n),_n=null),Kt&&Kt.length>0&&(ca("_autosave",Kt,Ps),Kt=null,Ps=null)}function Qw(){ia();let t=Is(),e=Ct.readdirSync(t).filter(r=>r.endsWith(".json")),n=new Date,o=new Date(n.getTime()-720*60*60*1e3),s=0;for(let r of e){let i=sr.join(t,r);try{let a=Ct.statSync(i);new Date(a.mtime)<o&&(Ct.unlinkSync(i),s++)}catch{}}return s}qd.exports={saveSession:ca,loadSession:Dd,listSessions:jd,deleteSession:zw,getLastSession:Xw,autoSave:Jw,flushAutoSave:Vw,clearOldSessions:Qw,_getSessionsDir:Is}});var or=H((gv,Yd)=>{"use strict";var Ls=require("fs"),Fd=require("path");function Ud(t){let e=[];return t.forEach((n,o)=>{n.role==="assistant"&&(Array.isArray(n.content)&&n.content.forEach(s=>{s&&s.type==="tool_use"&&e.push({name:s.name||"",input:s.input||{},index:o})}),Array.isArray(n.tool_calls)&&n.tool_calls.forEach(s=>{let r=s.function?.name||s.name||"",i={};try{i=typeof s.function?.arguments=="string"?JSON.parse(s.function.arguments):s.function?.arguments||s.input||{}}catch{}e.push({name:r,input:i,index:o})}))}),e}function Wd(t){let e=[];return t.forEach((n,o)=>{if(n.role==="user"&&Array.isArray(n.content)&&n.content.forEach(s=>{if(s&&s.type==="tool_result"){let r=typeof s.content=="string"?s.content:Array.isArray(s.content)?s.content.map(i=>typeof i=="string"?i:i.text||"").join(""):JSON.stringify(s.content||"");e.push({content:r,index:o})}}),n.role==="tool"){let s=typeof n.content=="string"?n.content:JSON.stringify(n.content||"");e.push({content:s,index:o})}}),e}function Bd(t){for(let e=t.length-1;e>=0;e--){let n=t[e];if(n.role==="assistant"){if(typeof n.content=="string")return n.content.trim();if(Array.isArray(n.content)){let o=n.content.filter(s=>s&&(s.type==="text"||typeof s=="string")).map(s=>typeof s=="string"?s:s.text||"").join("").trim();if(o)return o}}}return""}function Hd(t,e){let n=[];for(let o=t.length-1;o>=0&&n.length<e;o--){let s=t[o];if(s.role!=="assistant")continue;let r="";typeof s.content=="string"?r=s.content.trim():Array.isArray(s.content)&&(r=s.content.filter(i=>i&&(i.type==="text"||typeof i=="string")).map(i=>typeof i=="string"?i:i.text||"").join("").trim()),r&&n.push(r)}return n}function Gd(t){let e=new Map;for(let n of t){let o;try{o=JSON.stringify(n.input)}catch{o=String(n.input)}let s=`${n.name}|${o}`;e.set(s,(e.get(s)||0)+1)}return e}function Kd(t){if(!Array.isArray(t)||t.length===0)return{score:0,issues:["Empty or invalid session \u2014 no messages to analyse"],summary:"No messages found"};let e=10,n=[],o=Ud(t),s=Wd(t),r=o.length;t.some(E=>E.role==="user"&&typeof E.content=="string"&&E.content.startsWith("[SYSTEM WARNING]")&&(E.content.includes("edited")||E.content.includes("bash command")||E.content.includes("grep pattern")||E.content.includes("re-read")||E.content.includes("already in your context")))&&(e-=2,n.push("Loop-warning was fired during session (repeated file edits, bash commands, or re-reads)"));let a=o.find(E=>{let M=E.input?.command||E.input?.cmd||"";return/\bsed\s+-n\b/.test(M)});if(a){let E=(a.input?.command||a.input?.cmd||"").slice(0,80);e-=1.5,n.push(`sed -n anti-pattern used: ${E}`)}o.find(E=>{if(E.name!=="grep"&&E.name!=="bash"&&E.name!=="ssh_exec")return!1;let M=E.input?.command||E.input?.cmd||"",U=E.input?.pattern||"",G=`${M} ${U}`;return/(?:-[CAB]|--context|--after|--before)\s*[=\s]?([2-9][1-9]|\d{3,})/.test(G)||/grep.*-[CAB]\s*([2-9][1-9]|\d{3,})/.test(G)})&&(e-=1,n.push("grep used with >20 context lines (context flood risk)"));let u=t.some(E=>E.role==="assistant"),d=Bd(t),m=Hd(t,3).some(E=>E.length>100&&!/^[^.!]{0,40}\?$/.test(E));if(u&&!m&&(d.length<80||/^[^.!]{0,40}\?$/.test(d))){e-=2;let E=d.length>0?`"${d.slice(0,60)}..."`:"(no assistant text found)";n.push(`Session ends without diagnosis \u2014 last response too short or is only a question: ${E}`)}r>40?(e-=1.5,n.push(`Excessive tool calls: ${r} (>40 threshold)`)):r>25&&(e-=.5,n.push(`High tool call count: ${r} (>25 threshold)`)),t.some(E=>{let M=typeof E.content=="string"?E.content:Array.isArray(E.content)?E.content.map(U=>typeof U=="string"?U:U.text||"").join(""):"";return/\[auto-compressed|context compacted|force-compressed/.test(M)})&&(e-=.5,n.push("Auto-compress triggered (context flood indicator)"));let g=Gd(o),y=0,w="";for(let[E,M]of g)M>y&&(y=M,w=E);if(y>=3){let[E]=w.split("|");e-=1,n.push(`Same tool call repeated ${y}\xD7 (tool: ${E})`)}let k=!1;for(let E of s)if(E.content&&E.content.includes('"valid":true')&&o.filter(U=>U.index>E.index).length>0){k=!0;break}k&&(e-=1.5,n.push('Stop-trigger ignored: tool result contained "valid":true but session continued with more tool calls'));let R=o.filter(E=>E.name==="ssh_exec");if(R.length>=8){let E=0,M=1;for(let U=1;U<R.length;U++)R[U].index<=R[U-1].index+2?M++:(E=Math.max(E,M),M=1);E=Math.max(E,M),E>=8&&(e-=.5,n.push(`SSH reconnect storm: ${E} consecutive SSH calls`))}let x=new Map;for(let E of o)if(E.name==="read_file"&&E.input?.path){let M=E.input.path;x.has(M)||x.set(M,{count:0,ranges:[]});let U=x.get(M);if(U.count++,E.input.line_start!=null){let G=E.input.line_start||1,ne=E.input.line_end||G+350;U.ranges.push([G,ne])}}function _(E,M,U){for(let[G,ne]of U){let I=Math.max(E,G),W=Math.min(M,ne);if(W>I){let se=W-I,X=M-E||1;if(se/X>=.7)return!0}}return!1}let b=0,O="";for(let[E,M]of x){if(M.count<3)continue;if(M.ranges.length===M.count){let G=!1,ne=[];for(let[I,W]of M.ranges){if(ne.length>0&&_(I,W,ne)){G=!0;break}ne.push([I,W])}if(!G)continue}M.count>b&&(b=M.count,O=E)}if(b>=3){e-=1;let E=O.split("/").slice(-2).join("/");n.push(`read_file loop: "${E}" read ${b}\xD7 (file already in context)`)}let N=0,C="";for(let[E,M]of x){if(M.ranges.length<4)continue;let U=[],G=!1;for(let[ne,I]of M.ranges){if(U.length>0&&_(ne,I,U)){G=!0;break}U.push([ne,I])}!G&&M.ranges.length>N&&(N=M.ranges.length,C=E)}if(N>=4){e-=.5;let E=C.split("/").slice(-2).join("/");n.push(`File-scroll pattern: "${E}" read in ${N} sequential sections \u2014 use grep instead`)}let L=new Map;for(let E of o)if(E.name==="grep"&&E.input?.path&&E.input?.pattern){let M=E.input.path;L.has(M)||L.set(M,new Set),L.get(M).add(E.input.pattern)}let Ee=0,me="";for(let[E,M]of L)M.size>Ee&&(Ee=M.size,me=E);if(Ee>=3){e-=.75;let E=me.split("/").slice(-2).join("/");n.push(`grep flood on single file: "${E}" searched ${Ee}\xD7 with different patterns (file already in context)`)}{let E=new Set,M=new Set,U=/^(test_|demo_|temp_|tmp_|scratch_)/;for(let ne of o){if(ne.name==="write_file"&&ne.input?.path){let I=ne.input.path.split("/").pop(),W=ne.input.path.includes("/tests/");U.test(I)&&!W&&E.add(ne.input.path)}if((ne.name==="bash"||ne.name==="ssh_exec")&&ne.input?.command){let I=ne.input.command.match(/\brm\s+(?:-\w+\s+)?(\S+)/g);if(I)for(let W of I){let se=W.split(/\s+/),X=se[se.length-1];for(let Z of E)(Z.endsWith(X)||X.endsWith(Z.split("/").pop()))&&M.add(Z)}}}let G=M.size;if(G>=1){let ne=Math.min(G*.25,.5);e-=ne;let I=[...M].map(W=>W.split("/").pop()).join(", ");n.push(`Temp file write-then-delete: ${I} \u2014 write inline logic or use tests/ instead`)}}let le=s.filter(E=>E.content.startsWith("EXIT")).length;le>=10?(e-=1,n.push(`Bash exit-error storm: ${le} tool results started with EXIT (repeated failing commands)`)):le>=5&&(e-=.5,n.push(`Repeated bash errors: ${le} tool results with non-zero exit code`));for(let E of t){if(E.role!=="assistant")continue;let M="";if(typeof E.content=="string"?M=E.content:Array.isArray(E.content)&&(M=E.content.filter(X=>X&&(X.type==="text"||typeof X=="string")).map(X=>typeof X=="string"?X:X.text||"").join("")),M.length<=5e3)continue;let U=M.split(/(?<=\. )/).filter(X=>X.trim().length>0);if(U.length<6)continue;let G=new Map;for(let X=0;X<=U.length-3;X++){let Z=U.slice(X,X+3).join("").trim();Z.length>30&&G.set(Z,(G.get(Z)||0)+1)}let ne=0,I="";for(let[X,Z]of G)Z>ne&&(ne=Z,I=X);if(ne<3)continue;let se=I.length*ne/M.length;if(se>=.4||ne>=10){e-=1.5,n.push(`llm output loop: assistant message repeated content detected (${ne}\xD7 same paragraph, ${Math.round(se*100)}% repeated)`);break}}{let E=new Set(["read_file","list_directory","search_files","glob","grep"]),M=t.some(G=>Array.isArray(G.tool_calls)?G.tool_calls.some(ne=>E.has(ne.function?.name)):Array.isArray(G.content)?G.content.some(ne=>ne.type==="tool_use"&&E.has(ne.name)):!1);t.some(G=>G.role==="assistant"&&typeof G.content=="string"&&(G.content.includes("## Steps")||G.content.includes("/plan approve")))&&!M&&(e-=2,n.push("plan written without reading any files \u2014 LLM invented data structures from training knowledge (hallucination risk)"))}let ie=s.filter(E=>E.content.startsWith("BLOCKED:"));if(ie.length>0){let E=Math.min(ie.length*.5,1.5);e-=E,n.push(`${ie.length} tool call${ie.length===1?"":"s"} blocked (agent attempted denied actions)`)}let oe=t.filter(E=>{let M=typeof E.content=="string"?E.content:"";return/\[SYSTEM WARNING\] Context wiped \d+×/.test(M)}).length;if(oe>0){let E=Math.min(oe*1,2);e-=E,n.push(`Super-nuclear context wipe fired ${oe}\xD7 (context collapse \u2014 task too large or read loops)`)}{let E=!1,M=!1,U=!1;for(let ne of o){if(ne.name!=="bash")continue;let I=(ne.input?.command||ne.input?.cmd||"").trim();!(/cat\s*>/.test(I)||/<</.test(I))&&/\bcat\s+\S/.test(I)&&(E=!0),/^\s*ls(\s|$)/.test(I)&&!/npm|yarn|pnpm|make|git\b/.test(I)&&(M=!0),/\bfind\s+\S/.test(I)&&!/git\b|npm\b|-exec\b/.test(I)&&(U=!0)}let G=[E,M,U].filter(Boolean).length;if(G>0){let ne=Math.min(G*.25,.75);e-=ne;let I=[];E&&I.push("cat (use read_file)"),M&&I.push("ls (use list_directory)"),U&&I.push("find (use glob)"),n.push(`bash used instead of dedicated tool: ${I.join(", ")}`)}}e=Math.max(0,Math.min(10,e)),e=Math.round(e*10)/10;let ce=e>=9?"A":e>=8?"B":e>=7?"C":e>=6?"D":"F",Q=n.length===0?`Clean session \u2014 no quality issues detected (${r} tool calls)`:`${n.length} issue${n.length===1?"":"s"} found \u2014 ${r} tool calls`;return{score:e,grade:ce,issues:n,summary:Q}}function Zw(t){try{let{loadSession:e}=At(),n=e(t);return n?Kd(n.messages||[]):null}catch{return null}}function e0(t,e=null){let{score:n,grade:o,issues:s,summary:r}=t,i=e?.dim||"",a=e?.reset||"",l=e?.green||"",u=e?.yellow||"",d=e?.red||"",f=e?.cyan||"",m=e?.bold||"",h=n>=8?l:n>=6?u:d,p=`
|
|
460
460
|
${i} Session score: ${a}${m}${h}${n}/10 (${o})${a}`;if(r&&(p+=` ${i}${r}${a}`),s.length>0)for(let g of s)p+=`
|
|
461
461
|
${u}\u26A0${a} ${i}${g}${a}`;return p}function t0(t,e={}){try{let n=Fd.join(process.cwd(),".nex");Ls.existsSync(n)||Ls.mkdirSync(n,{recursive:!0});let o=Fd.join(n,"benchmark-history.json"),s=[];if(Ls.existsSync(o))try{s=JSON.parse(Ls.readFileSync(o,"utf-8"))}catch{s=[]}Array.isArray(s)||(s=[]);let r=t>=9?"A":t>=8?"B":t>=7?"C":t>=6?"D":"F",i={date:new Date().toISOString(),version:e.version||null,model:e.model||null,score:t,grade:r,sessionName:e.sessionName||null,issues:Array.isArray(e.issues)?e.issues:[]};s.push(i),s.length>100&&(s=s.slice(s.length-100)),Ls.writeFileSync(o,JSON.stringify(s,null,2))}catch{}}Yd.exports={scoreMessages:Kd,scoreSession:Zw,formatScore:e0,appendScoreHistory:t0,_extractToolCalls:Ud,_extractToolResults:Wd,_getLastAssistantText:Bd,_getLastNAssistantTexts:Hd,_countDuplicateToolCalls:Gd}});var rn=H(($v,ef)=>{var on=require("fs"),rr=require("path"),n0=require("os"),{atomicWrite:s0,withFileLockSync:zd}=Zt();function la(){return rr.join(process.cwd(),".nex","memory")}function Ds(){return rr.join(la(),"memory.json")}function o0(){return rr.join(process.cwd(),"NEX.md")}function Xd(){return rr.join(n0.homedir(),".nex","NEX.md")}function ua(){let t=la();on.existsSync(t)||on.mkdirSync(t,{recursive:!0})}function ir(){let t=Ds();if(!on.existsSync(t))return{};try{return JSON.parse(on.readFileSync(t,"utf-8"))}catch{return{}}}function Jd(t){ua(),s0(Ds(),JSON.stringify(t,null,2))}function r0(t,e){ua(),zd(Ds(),()=>{let n=ir();n[t]={value:e,updatedAt:new Date().toISOString()},Jd(n)})}function i0(t){let e=ir();return e[t]?e[t].value:null}function a0(t){return ua(),zd(Ds(),()=>{let e=ir();return t in e?(delete e[t],Jd(e),!0):!1})}function Vd(){let t=ir();return Object.entries(t).map(([e,n])=>({key:e,value:n.value,updatedAt:n.updatedAt}))}function Qd(){let t=Xd();if(!on.existsSync(t))return"";try{return on.readFileSync(t,"utf-8").trim()}catch{return""}}function Zd(){let t=o0();if(!on.existsSync(t))return"";try{return on.readFileSync(t,"utf-8").trim()}catch{return""}}function c0(){let t=[],e=Qd();e&&t.push(`GLOBAL INSTRUCTIONS (~/.nex/NEX.md):
|
|
462
462
|
${e}`);let n=Zd();n&&t.push(`PROJECT INSTRUCTIONS (NEX.md):
|
|
@@ -464,7 +464,7 @@ ${n}`);let o=Vd();if(o.length>0){let s=o.map(r=>` ${r.key}: ${r.value}`).join(`
|
|
|
464
464
|
`);t.push(`PROJECT MEMORY:
|
|
465
465
|
${s}`)}return t.join(`
|
|
466
466
|
|
|
467
|
-
`)}ef.exports={remember:r0,recall:i0,forget:a0,listMemories:Vd,loadGlobalInstructions:Qd,loadProjectInstructions:Zd,getMemoryContext:c0,_getMemoryDir:la,_getMemoryFile:Ds,_getGlobalNexMdPath:Xd}});var qs=H((wv,rf)=>{var gt=require("fs"),Jn=require("path"),{C:yv}=
|
|
467
|
+
`)}ef.exports={remember:r0,recall:i0,forget:a0,listMemories:Vd,loadGlobalInstructions:Qd,loadProjectInstructions:Zd,getMemoryContext:c0,_getMemoryDir:la,_getMemoryFile:Ds,_getGlobalNexMdPath:Xd}});var qs=H((wv,rf)=>{var gt=require("fs"),Jn=require("path"),{C:yv}=Oe(),{atomicWrite:l0,withFileLockSync:u0}=Zt(),ar={bash:"ask",read_file:"allow",write_file:"ask",edit_file:"ask",list_directory:"allow",search_files:"allow",glob:"allow",grep:"allow",patch_file:"ask",web_fetch:"allow",web_search:"allow",ask_user:"allow",task_list:"allow",spawn_agents:"ask"},Vn={...ar};function tf(){let t=Jn.join(process.cwd(),".nex","config.json");if(gt.existsSync(t))try{let e=JSON.parse(gt.readFileSync(t,"utf-8"));e.permissions&&(Vn={...ar,...e.permissions})}catch{}}function d0(){let t=Jn.join(process.cwd(),".nex"),e=Jn.join(t,"config.json");gt.existsSync(t)||gt.mkdirSync(t,{recursive:!0}),u0(e,()=>{let n={};if(gt.existsSync(e))try{n=JSON.parse(gt.readFileSync(e,"utf-8"))}catch{n={}}n.permissions=Vn,l0(e,JSON.stringify(n,null,2))})}function nf(t){return Vn[t]||"ask"}function f0(t,e){return["allow","ask","deny"].includes(e)?(Vn[t]=e,!0):!1}function p0(t){return nf(t)}function m0(){return Object.entries(Vn).map(([t,e])=>({tool:t,mode:e}))}function h0(){Vn={...ar}}var js={readonly:{description:"Read-only access \u2014 can search and read but not modify",allowedTools:["read_file","list_directory","search_files","glob","grep","git_status","git_diff","git_log","ask_user","web_fetch","web_search","browser_open","browser_screenshot","task_list"],blockedTools:["bash","write_file","edit_file","patch_file","deploy","ssh_exec","service_manage","container_manage","container_exec","remote_agent"],autoConfirm:!1,allowDangerous:!1},developer:{description:"Standard developer access \u2014 can read, write, and run commands",allowedTools:null,blockedTools:["deploy","service_manage","container_manage","remote_agent"],autoConfirm:!1,allowDangerous:!1},admin:{description:"Full access \u2014 all tools, including deployment and infrastructure",allowedTools:null,blockedTools:[],autoConfirm:!1,allowDangerous:!0}};function sf(){let t=Jn.join(process.cwd(),".nex","config.json");try{return gt.existsSync(t)&&JSON.parse(gt.readFileSync(t,"utf-8")).teamPermissions||null}catch{return null}}function g0(t){let e=Jn.join(process.cwd(),".nex"),n=Jn.join(e,"config.json");gt.existsSync(e)||gt.mkdirSync(e,{recursive:!0});let o={};try{gt.existsSync(n)&&(o=JSON.parse(gt.readFileSync(n,"utf-8")))}catch{o={}}o.teamPermissions=t,gt.writeFileSync(n,JSON.stringify(o,null,2),"utf-8")}function of(){let t=sf();if(!t)return js.admin;let e=t.role||"admin",n=js[e]||js.admin;return{...n,...t.overrides,blockedTools:[...n.blockedTools||[],...t.overrides?.blockedTools||[]]}}function $0(t){let e=of();return e.blockedTools&&e.blockedTools.includes(t)?{allowed:!1,reason:`Tool "${t}" is blocked by permission preset "${e.description||"custom"}"`}:e.allowedTools&&!e.allowedTools.includes(t)?{allowed:!1,reason:`Tool "${t}" is not in the allowed list for this permission level`}:{allowed:!0}}function y0(){return Object.entries(js).map(([t,e])=>({name:t,description:e.description,toolCount:e.allowedTools?`${e.allowedTools.length} allowed`:"all allowed",blockedCount:e.blockedTools.length}))}tf();rf.exports={getPermission:nf,setPermission:f0,checkPermission:p0,listPermissions:m0,loadPermissions:tf,savePermissions:d0,resetPermissions:h0,DEFAULT_PERMISSIONS:ar,PERMISSION_PRESETS:js,loadPresetConfig:sf,savePresetConfig:g0,getEffectivePreset:of,isToolAllowed:$0,listPresets:y0}});var Dt=H((_v,df)=>{var kn=require("fs"),cr=require("path"),bv=require("readline"),{C:xe}=Oe(),_e=null,fa=!1,pa=null,af=new Set(["read_file","list_directory","search_files","glob","grep","web_search","web_fetch","git_status","git_diff","git_log","git_show"]);function lr(){return cr.join(process.cwd(),".nex","plans")}function cf(){let t=lr();kn.existsSync(t)||kn.mkdirSync(t,{recursive:!0})}function w0(t,e=[]){return _e={name:`plan-${Date.now()}-${Math.random().toString(36).slice(2,6)}`,task:t,steps:e.map(n=>({description:n.description||n,files:n.files||[],status:"pending"})),status:"draft",createdAt:new Date().toISOString()},_e}function b0(){return _e}function _0(t){fa=t}function x0(){return fa}function k0(){return!_e||_e.status!=="draft"?!1:(_e.status="approved",_e.updatedAt=new Date().toISOString(),!0)}function v0(){return!_e||_e.status!=="approved"?!1:(_e.status="executing",!0)}function da(t,e){return!_e||t<0||t>=_e.steps.length?!1:(_e.steps[t].status=e,_e.updatedAt=new Date().toISOString(),_e.steps.every(n=>n.status==="done"||n.status==="skipped")&&(_e.status="completed"),!0)}function S0(t){if(!t)return`${xe.dim}No active plan${xe.reset}`;let e={draft:`${xe.yellow}DRAFT${xe.reset}`,approved:`${xe.green}APPROVED${xe.reset}`,executing:`${xe.blue}EXECUTING${xe.reset}`,completed:`${xe.green}COMPLETED${xe.reset}`},n=[];n.push(`
|
|
468
468
|
${xe.bold}${xe.cyan}Plan: ${t.task}${xe.reset}`),n.push(`${xe.dim}Status: ${e[t.status]||t.status}${xe.reset}
|
|
469
469
|
`);for(let o=0;o<t.steps.length;o++){let s=t.steps[o],r;switch(s.status){case"done":r=`${xe.green}\u2713${xe.reset}`;break;case"in_progress":r=`${xe.blue}\u2192${xe.reset}`;break;case"skipped":r=`${xe.dim}\u25CB${xe.reset}`;break;default:r=`${xe.dim} ${xe.reset}`}n.push(` ${r} ${xe.bold}Step ${o+1}:${xe.reset} ${s.description}`),s.files.length>0&&n.push(` ${xe.dim}Files: ${s.files.join(", ")}${xe.reset}`)}return n.push(""),n.join(`
|
|
470
470
|
`)}function E0(t){if(t||(t=_e),!t)return null;cf();let e=cr.join(lr(),`${t.name}.json`);return kn.writeFileSync(e,JSON.stringify(t,null,2),"utf-8"),e}function T0(t){let e=cr.join(lr(),`${t}.json`);if(!kn.existsSync(e))return null;try{let n=JSON.parse(kn.readFileSync(e,"utf-8"));return _e=n,n}catch{return null}}function R0(){cf();let t=lr(),e=kn.readdirSync(t).filter(o=>o.endsWith(".json")),n=[];for(let o of e)try{let s=JSON.parse(kn.readFileSync(cr.join(t,o),"utf-8"));n.push({name:s.name,task:s.task,status:s.status,steps:s.steps?s.steps.length:0,createdAt:s.createdAt})}catch{}return n.sort((o,s)=>(s.createdAt||"").localeCompare(o.createdAt||""))}function C0(t){if(!t)return[];let e=[],n=t.match(/##\s+Steps?\s*\n([\s\S]*?)(?:\n##|\s*$)/i),o=n?n[1]:t,s=/^\s*(\d+)[.)]\s+(.+)/gm,r;for(;(r=s.exec(o))!==null;){let a=r[2].trim().replace(/^\*\*What\*\*:\s*/i,"").replace(/^\*\*\d+\.\*\*\s*/,"").replace(/\*\*/g,"").trim();a.length>3&&e.push({description:a,files:[],status:"pending"})}if(e.length===0){let i=/\*\*Step\s+\d+[:.]\*\*\s*(.+)/gi;for(;(r=i.exec(t))!==null;){let a=r[1].replace(/\*\*/g,"").trim();a.length>3&&e.push({description:a,files:[],status:"pending"})}}if(e.length>0){let i=/\*\*(?:Where|Files?)\*\*:\s*(.+)/gi,a=[...t.matchAll(i)];for(let l=0;l<Math.min(e.length,a.length);l++){let u=a[l][1];e[l].files=u.split(/[,\s]+/).filter(d=>/[./]/.test(d)).slice(0,5)}}return e}function A0(t){pa=t}function O0(){return pa}function N0(){_e=null,fa=!1,pa=null,L0()}function M0(){return`
|
|
@@ -515,7 +515,7 @@ Bullet list of potential issues and mitigations.
|
|
|
515
515
|
- Order steps by dependency (later steps may depend on earlier ones).
|
|
516
516
|
- After presenting the plan, tell the user to type \`/plan approve\` to proceed.
|
|
517
517
|
- Do NOT make any file changes \u2014 your role is analysis and planning only.
|
|
518
|
-
- Do NOT call ask_user. If anything is ambiguous, add an "## Assumptions" section to the plan and state your assumption. The user approves or rejects the whole plan \u2014 that is the only gate.`}var xn=0;function P0(){!_e||_e.status!=="executing"||(xn>0&&da(xn-1,"done"),xn<_e.steps.length&&(da(xn,"in_progress"),xn++))}function I0(){if(!_e||_e.status!=="executing"||_e.steps.length===0)return null;let t=Math.min(xn,_e.steps.length),e=_e.steps.length,n=Math.max(0,t-1),o=_e.steps[n]?.description||"";return{current:t,total:e,description:o}}function L0(){xn=0}var lf=["interactive","semi-auto","autonomous"],uf="interactive";function D0(t){return lf.includes(t)?(uf=t,!0):!1}function j0(){return uf}df.exports={createPlan:w0,getActivePlan:b0,setPlanMode:_0,isPlanMode:x0,approvePlan:k0,startExecution:v0,updateStep:da,formatPlan:S0,savePlan:E0,loadPlan:T0,listPlans:R0,clearPlan:N0,getPlanModePrompt:M0,setPlanContent:A0,getPlanContent:O0,extractStepsFromText:C0,advancePlanStep:P0,getPlanStepInfo:I0,PLAN_MODE_ALLOWED_TOOLS:af,setAutonomyLevel:D0,getAutonomyLevel:j0,AUTONOMY_LEVELS:lf}});var xf=H((xv,_f)=>{var{C:T}=
|
|
518
|
+
- Do NOT call ask_user. If anything is ambiguous, add an "## Assumptions" section to the plan and state your assumption. The user approves or rejects the whole plan \u2014 that is the only gate.`}var xn=0;function P0(){!_e||_e.status!=="executing"||(xn>0&&da(xn-1,"done"),xn<_e.steps.length&&(da(xn,"in_progress"),xn++))}function I0(){if(!_e||_e.status!=="executing"||_e.steps.length===0)return null;let t=Math.min(xn,_e.steps.length),e=_e.steps.length,n=Math.max(0,t-1),o=_e.steps[n]?.description||"";return{current:t,total:e,description:o}}function L0(){xn=0}var lf=["interactive","semi-auto","autonomous"],uf="interactive";function D0(t){return lf.includes(t)?(uf=t,!0):!1}function j0(){return uf}df.exports={createPlan:w0,getActivePlan:b0,setPlanMode:_0,isPlanMode:x0,approvePlan:k0,startExecution:v0,updateStep:da,formatPlan:S0,savePlan:E0,loadPlan:T0,listPlans:R0,clearPlan:N0,getPlanModePrompt:M0,setPlanContent:A0,getPlanContent:O0,extractStepsFromText:C0,advancePlanStep:P0,getPlanStepInfo:I0,PLAN_MODE_ALLOWED_TOOLS:af,setAutonomyLevel:D0,getAutonomyLevel:j0,AUTONOMY_LEVELS:lf}});var xf=H((xv,_f)=>{var{C:T}=Oe();function ff(){return Math.max(10,(process.stdout.columns||80)-2)}function q0(t){if(!t)return"";let e=t.split(`
|
|
519
519
|
`),n=[],o=!1,s="";for(let r of e){let i=ff();if(r.trim().startsWith("```")){if(o)n.push(`${T.dim}${"\u2500".repeat(40)}${T.reset}`),o=!1,s="";else{o=!0,s=r.trim().substring(3).trim();let a=s?` ${s} `:"";n.push(`${T.dim}${"\u2500".repeat(3)}${a}${"\u2500".repeat(Math.max(0,37-a.length))}${T.reset}`)}continue}if(o){n.push(` ${ha(r,s)}`);continue}if(r.startsWith("### ")){n.push(`${T.bold}${T.cyan} ${vn(r.substring(4))}${T.reset}`);continue}if(r.startsWith("## ")){n.push(`${T.bold}${T.cyan} ${vn(r.substring(3))}${T.reset}`);continue}if(r.startsWith("# ")){n.push(`${T.bold}${T.cyan}${vn(r.substring(2))}${T.reset}`);continue}if(/^\s*[-*]\s/.test(r)){let a=r.match(/^(\s*)/)[1],l=r.replace(/^\s*[-*]\s/,""),u=`${a}${T.cyan}\u2022${T.reset} ${Sn(l)}`;n.push(En(u,i,a+" "));continue}if(/^\s*\d+\.\s/.test(r)){let a=r.match(/^(\s*)(\d+)\.\s(.*)/);if(a){let l=a[1],u=a[2],d=a[3],f=`${l}${T.cyan}${u}.${T.reset} ${Sn(d)}`,m=l+" ".repeat(u.length+2);n.push(En(f,i,m));continue}}n.push(En(Sn(r),i))}return n.join(`
|
|
520
520
|
`)}function vn(t){return t.replace(/\*\*([^*]+)\*\*/g,"$1").replace(/\*([^*]+)\*/g,"$1").replace(/__([^_]+)__/g,"$1").replace(/_([^_]+)_/g,"$1").replace(/`([^`]+)`/g,"$1")}function Sn(t){return t?t.replace(/`([^`]+)`/g,`${T.cyan}$1${T.reset}`).replace(/\*\*([^*]+)\*\*/g,`${T.bold}$1${T.reset}`).replace(/\*([^*]+)\*/g,`${T.dim}$1${T.reset}`).replace(/\[([^\]]+)\]\(([^)]+)\)/g,`${T.cyan}$1${T.reset} ${T.dim}($2)${T.reset}`):""}function ha(t,e){return t?["js","javascript","ts","typescript","jsx","tsx"].includes(e)||!e?pf(t):e==="bash"||e==="sh"||e==="shell"||e==="zsh"?mf(t):e==="json"||e==="jsonc"?hf(t):e==="python"||e==="py"?gf(t):e==="go"||e==="golang"?$f(t):e==="rust"||e==="rs"?yf(t):e==="css"||e==="scss"||e==="less"?wf(t):e==="html"||e==="xml"||e==="svg"||e==="htm"?bf(t):t:""}function pf(t){let e=/\b(const|let|var|function|return|if|else|for|while|class|import|export|from|require|async|await|new|this|throw|try|catch|switch|case|break|default|typeof|instanceof)\b/g,n=/(["'`])(?:(?=(\\?))\2.)*?\1/g,o=/(\/\/.*$)/,s=/\b(\d+\.?\d*)\b/g,r=t;return r=r.replace(s,`${T.yellow}$1${T.reset}`),r=r.replace(e,`${T.magenta}$1${T.reset}`),r=r.replace(n,`${T.green}$&${T.reset}`),r=r.replace(o,`${T.dim}$1${T.reset}`),r}function mf(t){let e=/^(\s*)([\w-]+)/,n=/(--?\w[\w-]*)/g,o=/(["'])(?:(?=(\\?))\2.)*?\1/g,s=/(#.*$)/,r=t;return r=r.replace(n,`${T.cyan}$1${T.reset}`),r=r.replace(e,`$1${T.green}$2${T.reset}`),r=r.replace(o,`${T.yellow}$&${T.reset}`),r=r.replace(s,`${T.dim}$1${T.reset}`),r}function hf(t){let e=/("[\w-]+")\s*:/g,n=/:\s*("(?:[^"\\]|\\.)*")/g,o=/:\s*(\d+\.?\d*)/g,s=/:\s*(true|false|null)/g,r=t;return r=r.replace(e,`${T.cyan}$1${T.reset}:`),r=r.replace(n,`: ${T.green}$1${T.reset}`),r=r.replace(o,`: ${T.yellow}$1${T.reset}`),r=r.replace(s,`: ${T.magenta}$1${T.reset}`),r}function gf(t){let e=/\b(def|class|if|elif|else|for|while|return|import|from|as|try|except|finally|raise|with|yield|lambda|pass|break|continue|and|or|not|in|is|None|True|False|self|async|await|nonlocal|global)\b/g,n=/("""[\s\S]*?"""|'''[\s\S]*?'''|"(?:[^"\\]|\\.)*"|'(?:[^'\\]|\\.)*')/g,o=/(#.*$)/,s=/\b(\d+\.?\d*)\b/g,r=/^(\s*@\w+)/,i=t;return i=i.replace(s,`${T.yellow}$1${T.reset}`),i=i.replace(e,`${T.magenta}$1${T.reset}`),i=i.replace(r,`${T.cyan}$1${T.reset}`),i=i.replace(n,`${T.green}$&${T.reset}`),i=i.replace(o,`${T.dim}$1${T.reset}`),i}function $f(t){let e=/\b(func|package|import|var|const|type|struct|interface|map|chan|go|defer|return|if|else|for|range|switch|case|default|break|continue|select|fallthrough|nil|true|false|make|new|len|cap|append|copy|delete|panic|recover)\b/g,n=/\b(string|int|int8|int16|int32|int64|uint|uint8|uint16|uint32|uint64|float32|float64|bool|byte|rune|error|any)\b/g,o=/(["'`])(?:(?=(\\?))\2.)*?\1/g,s=/(\/\/.*$)/,r=/\b(\d+\.?\d*)\b/g,i=t;return i=i.replace(r,`${T.yellow}$1${T.reset}`),i=i.replace(n,`${T.cyan}$1${T.reset}`),i=i.replace(e,`${T.magenta}$1${T.reset}`),i=i.replace(o,`${T.green}$&${T.reset}`),i=i.replace(s,`${T.dim}$1${T.reset}`),i}function yf(t){let e=/\b(fn|let|mut|const|struct|enum|impl|trait|pub|use|mod|crate|self|super|match|if|else|for|while|loop|return|break|continue|where|as|in|ref|move|async|await|unsafe|extern|type|static|dyn|macro_rules)\b/g,n=/\b(i8|i16|i32|i64|i128|u8|u16|u32|u64|u128|f32|f64|bool|char|str|String|Vec|Option|Result|Box|Rc|Arc|Self|Some|None|Ok|Err|true|false)\b/g,o=/(["'])(?:(?=(\\?))\2.)*?\1/g,s=/(\/\/.*$)/,r=/\b(\d+\.?\d*)\b/g,i=/\b(\w+!)/g,a=t;return a=a.replace(r,`${T.yellow}$1${T.reset}`),a=a.replace(n,`${T.cyan}$1${T.reset}`),a=a.replace(e,`${T.magenta}$1${T.reset}`),a=a.replace(i,`${T.yellow}$1${T.reset}`),a=a.replace(o,`${T.green}$&${T.reset}`),a=a.replace(s,`${T.dim}$1${T.reset}`),a}function wf(t){let e=/^(\s*)([\w-]+)\s*:/,n=/:\s*([^;]+)/,o=/^(\s*[.#@][\w-]+)/,s=/\b(\d+\.?\d*(px|em|rem|%|vh|vw|s|ms|deg|fr)?)\b/g,r=/(\/\*.*?\*\/|\/\/.*$)/,i=/(#[0-9a-fA-F]{3,8})\b/g,a=t;return a=a.replace(i,`${T.yellow}$1${T.reset}`),a=a.replace(s,`${T.yellow}$1${T.reset}`),a=a.replace(e,`$1${T.cyan}$2${T.reset}:`),a=a.replace(o,`$1${T.magenta}$&${T.reset}`),a=a.replace(r,`${T.dim}$1${T.reset}`),a}function bf(t){let e=/<\/?(\w[\w-]*)/g,n=/\s([\w-]+)=/g,o=/(["'])(?:(?=(\\?))\2.)*?\1/g,s=/(<!--.*?-->)/g,r=/(&\w+;)/g,i=t;return i=i.replace(s,`${T.dim}$1${T.reset}`),i=i.replace(o,`${T.green}$&${T.reset}`),i=i.replace(e,`<${T.magenta}$1${T.reset}`),i=i.replace(n,` ${T.cyan}$1${T.reset}=`),i=i.replace(r,`${T.yellow}$1${T.reset}`),i}function En(t,e,n=""){if(!e||e<10)return t;let o="",s=0,r=-1,i=0,a=0,l=t.length;for(;i<l;){if(t[i]==="\x1B"){let u=i+1;if(u<l&&t[u]==="["){for(u++;u<l&&!/[a-zA-Z]/.test(t[u]);)u++;u<l&&u++}i=u;continue}if(t[i]===" "&&(r=i),s++,s>e&&r!==-1){o+=t.slice(a,r)+`
|
|
521
521
|
`+n,a=r+1,i=a,s=n.length,r=-1;continue}s>e&&r===-1&&(o+=t.slice(a,i)+`
|
|
@@ -531,7 +531,7 @@ Bullet list of potential issues and mitigations.
|
|
|
531
531
|
`);return}if(/^\s*[-*]\s/.test(e)){let s=e.match(/^(\s*)/)[1],r=e.replace(/^\s*[-*]\s/,""),i=`${s}${T.cyan}\u2022${T.reset} ${Sn(r)}`,a=En(i,n,s+" ");this._safeWrite(`${a}
|
|
532
532
|
`);return}if(/^\s*\d+\.\s/.test(e)){let s=e.match(/^(\s*)(\d+)\.\s(.*)/);if(s){let r=s[1],i=s[2],a=s[3],l=`${r}${T.cyan}${i}.${T.reset} ${Sn(a)}`,u=r+" ".repeat(i.length+2),d=En(l,n,u);this._safeWrite(`${d}
|
|
533
533
|
`);return}}let o=En(Sn(e),n);this._safeWrite(`${o}
|
|
534
|
-
`)}};_f.exports={renderMarkdown:q0,renderInline:Sn,stripHeadingMarkers:vn,highlightCode:ha,highlightJS:pf,highlightBash:mf,highlightJSON:hf,highlightPython:gf,highlightGo:$f,highlightRust:yf,highlightCSS:wf,highlightHTML:bf,renderTable:F0,renderProgress:U0,wrapAnsi:En,StreamRenderer:ma}});var ya=H((kv,Ef)=>{var{execSync:W0}=require("child_process"),ga=require("path"),Fs=require("fs"),$a=["pre-tool","post-tool","pre-commit","post-response","session-start","session-end"];function kf(){return ga.join(process.cwd(),".nex","hooks")}function B0(){return ga.join(process.cwd(),".nex","config.json")}function vf(){let t=B0();if(!Fs.existsSync(t))return{};try{return JSON.parse(Fs.readFileSync(t,"utf-8")).hooks||{}}catch{return{}}}function ur(t){if(!$a.includes(t))return[];let e=[],n=kf(),o=ga.join(n,t);Fs.existsSync(o)&&e.push(o);let s=vf();if(s[t]){let r=Array.isArray(s[t])?s[t]:[s[t]];e.push(...r)}return e}function Sf(t,e={},n=3e4){try{return{success:!0,output:W0(t,{cwd:process.cwd(),encoding:"utf-8",timeout:n,env:{...process.env,...e},stdio:["pipe","pipe","pipe"]}).trim()}}catch(o){return{success:!1,error:o.stderr?o.stderr.trim():o.message}}}function H0(t,e={}){let n=ur(t);if(n.length===0)return[];let o={};for(let[r,i]of Object.entries(e))o[`NEX_${r.toUpperCase()}`]=String(i);let s=[];for(let r of n){let i=Sf(r,o);if(s.push({command:r,...i}),!i.success&&t.startsWith("pre-"))break}return s}function G0(t){return ur(t).length>0}function K0(){let t=[];for(let e of $a){let n=ur(e);n.length>0&&t.push({event:e,commands:n})}return t}function Y0(){let t=kf();return Fs.existsSync(t)||Fs.mkdirSync(t,{recursive:!0}),t}Ef.exports={HOOK_EVENTS:$a,loadHookConfig:vf,getHooksForEvent:ur,executeHook:Sf,runHooks:H0,hasHooks:G0,listHooks:K0,initHooksDir:Y0}});var Us=H((Ev,Df)=>{"use strict";var{callWithRetry:Tf,runSubAgent:z0,clearAllLocks:X0}=Yo(),{parseModelSpec:Rf,getActiveProviderName:vv,getActiveModelId:Sv}=
|
|
534
|
+
`)}};_f.exports={renderMarkdown:q0,renderInline:Sn,stripHeadingMarkers:vn,highlightCode:ha,highlightJS:pf,highlightBash:mf,highlightJSON:hf,highlightPython:gf,highlightGo:$f,highlightRust:yf,highlightCSS:wf,highlightHTML:bf,renderTable:F0,renderProgress:U0,wrapAnsi:En,StreamRenderer:ma}});var ya=H((kv,Ef)=>{var{execSync:W0}=require("child_process"),ga=require("path"),Fs=require("fs"),$a=["pre-tool","post-tool","pre-commit","post-response","session-start","session-end"];function kf(){return ga.join(process.cwd(),".nex","hooks")}function B0(){return ga.join(process.cwd(),".nex","config.json")}function vf(){let t=B0();if(!Fs.existsSync(t))return{};try{return JSON.parse(Fs.readFileSync(t,"utf-8")).hooks||{}}catch{return{}}}function ur(t){if(!$a.includes(t))return[];let e=[],n=kf(),o=ga.join(n,t);Fs.existsSync(o)&&e.push(o);let s=vf();if(s[t]){let r=Array.isArray(s[t])?s[t]:[s[t]];e.push(...r)}return e}function Sf(t,e={},n=3e4){try{return{success:!0,output:W0(t,{cwd:process.cwd(),encoding:"utf-8",timeout:n,env:{...process.env,...e},stdio:["pipe","pipe","pipe"]}).trim()}}catch(o){return{success:!1,error:o.stderr?o.stderr.trim():o.message}}}function H0(t,e={}){let n=ur(t);if(n.length===0)return[];let o={};for(let[r,i]of Object.entries(e))o[`NEX_${r.toUpperCase()}`]=String(i);let s=[];for(let r of n){let i=Sf(r,o);if(s.push({command:r,...i}),!i.success&&t.startsWith("pre-"))break}return s}function G0(t){return ur(t).length>0}function K0(){let t=[];for(let e of $a){let n=ur(e);n.length>0&&t.push({event:e,commands:n})}return t}function Y0(){let t=kf();return Fs.existsSync(t)||Fs.mkdirSync(t,{recursive:!0}),t}Ef.exports={HOOK_EVENTS:$a,loadHookConfig:vf,getHooksForEvent:ur,executeHook:Sf,runHooks:H0,hasHooks:G0,listHooks:K0,initHooksDir:Y0}});var Us=H((Ev,Df)=>{"use strict";var{callWithRetry:Tf,runSubAgent:z0,clearAllLocks:X0}=Yo(),{parseModelSpec:Rf,getActiveProviderName:vv,getActiveModelId:Sv}=Te(),{MultiProgress:J0,C:ae}=Oe(),Cf=3,wa=4,Af="devstral-2:123b",Of="kimi-k2.5",Nf=`You are a task decomposition engine. Given a complex user request, split it into independent, atomic sub-tasks.
|
|
535
535
|
|
|
536
536
|
RULES:
|
|
537
537
|
- Output ONLY a JSON array, no markdown fences, no explanation.
|
|
@@ -567,8 +567,8 @@ Result: ${d.result}
|
|
|
567
567
|
Tools: ${(d.toolsUsed||[]).join(", ")||"none"}`}).join(`
|
|
568
568
|
|
|
569
569
|
`),r=[{role:"system",content:Mf.replace("{prompt}",e).replace("{results}",o)},{role:"user",content:"Synthesize the sub-agent results above."}],i={};if(n){let d=Rf(n);d.provider&&(i.provider=d.provider),d.model&&(i.model=d.model)}let l=(await Tf(r,[],i)).content||"",u=ba(l);return{summary:String(u.summary||""),conflicts:Array.isArray(u.conflicts)?u.conflicts:[],commitMessage:String(u.commitMessage||""),filesChanged:Array.isArray(u.filesChanged)?u.filesChanged:[]}}async function Q0(t,e={}){let n=e.orchestratorModel||process.env.NEX_ORCHESTRATOR_MODEL||Of,o=e.workerModel||Af,s=e.maxParallel||Cf,r=e.maxSubTasks||wa,i=e.onProgress||(()=>{}),a={input:0,output:0};console.log(`
|
|
570
|
-
${
|
|
571
|
-
`),i("decomposing"),console.log(`${
|
|
570
|
+
${ae.bold}Orchestrator${ae.reset} ${ae.dim}model: ${n} | workers: ${o} | max parallel: ${s}${ae.reset}
|
|
571
|
+
`),i("decomposing"),console.log(`${ae.dim}Phase 1: Decomposing prompt into sub-tasks...${ae.reset}`);let l;try{l=await If(t,n,{maxSubTasks:r})}catch(C){return console.log(`${ae.red}Decompose failed: ${C.message}${ae.reset}`),{results:[],synthesis:{summary:`Decompose failed: ${C.message}`,conflicts:[],commitMessage:"",filesChanged:[]},totalTokens:a}}if(l.length===0)return console.log(`${ae.yellow}No sub-tasks generated. Prompt may be too simple for orchestration.${ae.reset}`),{results:[],synthesis:{summary:"No sub-tasks generated.",conflicts:[],commitMessage:"",filesChanged:[]},totalTokens:a};console.log(`${ae.green}Decomposed into ${l.length} sub-tasks:${ae.reset}`);for(let C of l){console.log(` ${ae.dim}${C.id}:${ae.reset} ${C.task}`);let L=C.scope.filter(Boolean);L.length>0&&console.log(` ${ae.dim}scope: ${L.join(", ")}${ae.reset}`)}console.log(""),i("executing"),console.log(`${ae.dim}Phase 2: Running ${l.length} sub-agents (max ${s} parallel)...${ae.reset}
|
|
572
572
|
`);let u=Date.now(),d=Pf(s),f={findings:[],_lock:!1},m=l.map((C,L)=>`Agent ${L+1} [${o}]: ${C.task.substring(0,40)}${C.task.length>40?"...":""}`),h=new J0(m);h.start();let p=`
|
|
573
573
|
You are a focused coding agent executing ONE specific sub-task.
|
|
574
574
|
Your scope is limited to the files listed in your task definition.
|
|
@@ -587,20 +587,21 @@ RULES:
|
|
|
587
587
|
- If your task says "add X to README" \u2014 add it, don't check if it exists first.
|
|
588
588
|
- Max 10 tool calls. If you need more, you are doing too much \u2014 narrow your scope.
|
|
589
589
|
- When done: stop calling tools and write a one-line summary of what you changed.
|
|
590
|
-
`,g=l.map(async(C,L)=>{let Ee=await d();try{let me=f.findings.filter(
|
|
591
|
-
`),
|
|
590
|
+
`,g=l.map(async(C,L)=>{let Ee=await d();try{let me=f.findings.filter(ce=>ce.agentId!==C.id).map(ce=>`Agent ${ce.agentId} found: ${ce.summary}`).join(`
|
|
591
|
+
`),le=[me?`Prior agent findings:
|
|
592
592
|
${me}
|
|
593
|
-
`:"",C.scope.length>0?`Focus on files: ${C.scope.join(", ")}`:""].filter(Boolean),ie=await z0({task:C.task,context:
|
|
594
|
-
`):void 0,max_iterations:Math.min(C.estimatedCalls||10,15),model:o,_skipLog:!0,_systemPrompt:p},{onUpdate:
|
|
595
|
-
`),
|
|
596
|
-
|
|
597
|
-
|
|
593
|
+
`:"",C.scope.length>0?`Focus on files: ${C.scope.join(", ")}`:""].filter(Boolean),ie=await z0({task:C.task,context:le.length>0?le.join(`
|
|
594
|
+
`):void 0,max_iterations:Math.min(C.estimatedCalls||10,15),model:o,_skipLog:!0,_systemPrompt:p},{onUpdate:ce=>{if(ce&&ce.type==="tool_call"&&process.stderr.isTTY){let Q=` ${ae.dim}[Agent ${L+1}] ${ce.tool}${ae.reset}`;process.stderr.write(Q+`
|
|
595
|
+
`)}}}),oe=typeof ie.result=="string"?ie.result.slice(0,200):String(ie.result||"").slice(0,200);return f.findings.push({agentId:C.id,summary:oe,files:Array.isArray(C.scope)?C.scope:[]}),h.update(L,ie.status==="failed"?"error":"done"),a.input+=ie.tokensUsed?.input||0,a.output+=ie.tokensUsed?.output||0,ie.tokensUsed?._estimated&&(a._estimated=!0),{...ie,_scope:C.scope,_idx:L}}catch(me){return h.update(L,"error"),{task:C.task,status:"failed",result:`Error: ${me.message}`,toolsUsed:[],tokensUsed:{input:0,output:0}}}finally{Ee()}}),y;try{y=await Promise.all(g)}finally{h.stop({silent:!0}),X0()}console.log("");let w=Math.round((Date.now()-u)/1e3),k=w>=60?`${Math.floor(w/60)}m ${w%60}s`:`${w}s`;for(let C=0;C<y.length;C++){let L=y[C],me=L.status==="done"||L.status==="truncated"&&L.result&&!L.result.startsWith("Error")?`${ae.green}\u2713${ae.reset}`:`${ae.red}\u2717${ae.reset}`,le=L._scope&&L._scope.length>0?L._scope.map(ce=>ce.replace(/^.*\//,"").replace(/\/$/,"")).filter(Boolean):[],ie=le.length>0?le.join(", "):L.task.substring(0,35)+(L.task.length>35?"...":""),oe=C===y.length-1;console.log(` ${me} Agent ${C+1} ${ae.dim}${ie}${ae.reset}${oe?` ${ae.dim}${k}${ae.reset}`:""}`)}console.log(""),i("synthesizing"),console.log(`${ae.dim}Phase 3: Synthesizing results...${ae.reset}`);let R;try{R=await Lf(y,t,n)}catch(C){console.log(`${ae.yellow}Synthesize failed: ${C.message} \u2014 using raw results.${ae.reset}`),R={summary:y.map(L=>L.result).join(`
|
|
596
|
+
`),conflicts:[],commitMessage:"",filesChanged:[]}}let x=f.findings.flatMap(C=>C.files),_=new Map;for(let C of x)_.set(C,(_.get(C)||0)+1);let b=[..._.values()].some(C=>C>1),O=f.findings.length>1&&b?` ${ae.dim}(agents shared context)${ae.reset}`:"";if(console.log(`
|
|
597
|
+
${ae.bold}Summary:${ae.reset} ${R.summary}${O}`),R.conflicts.length>0){console.log(`${ae.yellow}Conflicts:${ae.reset}`);for(let C of R.conflicts)console.log(` - ${C}`)}R.commitMessage&&console.log(`${ae.dim}Suggested commit: ${R.commitMessage}${ae.reset}`);let N=a.input===0&&a.output===0?"n/a (provider does not report token counts)":a._estimated?`~${a.input} input / ~${a.output} output (est.)`:`${a.input} input + ${a.output} output`;return console.log(`${ae.dim}Tokens: ${N}${ae.reset}
|
|
598
|
+
`),{results:y,synthesis:R,totalTokens:a}}Df.exports={runOrchestrated:Q0,decompose:If,synthesize:Lf,detectComplexPrompt:V0,extractJSON:ba,createSemaphore:Pf,DECOMPOSE_PROMPT:Nf,SYNTHESIZE_PROMPT:Mf,DEFAULT_ORCHESTRATOR_MODEL:Of,DEFAULT_WORKER_MODEL:Af,DEFAULT_MAX_PARALLEL:Cf,DEFAULT_MAX_SUBTASKS:wa}});var Se=H((Nv,op)=>{var{C:$,Spinner:Zn,TaskProgress:Z0,formatToolCall:eb,formatToolSummary:zf,formatSectionHeader:_a,formatMilestone:tb,setActiveTaskProgress:Tv}=Oe(),{debugLog:re,warnLog:Rv}=mi(),{MilestoneTracker:nb}=Nl(),{callStream:sb}=Te(),{parseToolArgs:ob}=Un(),{executeTool:rb}=Et(),{gatherProjectContext:ib}=nr(),{fitToContext:ab,forceCompress:jt,getUsage:Ot,estimateTokens:cb}=Qe(),{autoSave:lb,flushAutoSave:ub}=At(),{scoreMessages:db,formatScore:fb,appendScoreHistory:pb}=or();function Ue(t){lb(t),ub()}function jf(t){try{if(!t.some(o=>o.role!=="assistant"?!1:!!(Array.isArray(o.content)&&o.content.some(s=>s&&s.type==="tool_use")||Array.isArray(o.tool_calls)&&o.tool_calls.length>0)))return;let n=db(t);if(!n)return;console.log(fb(n,$));try{let{_getSessionsDir:o}=At(),s=require("fs"),r=require("path").join(o(),"_autosave.json");if(s.existsSync(r)){let i=JSON.parse(s.readFileSync(r,"utf-8"));i.score=n.score,i.scoreGrade=n.grade,i.scoreIssues=n.issues,s.writeFileSync(r,JSON.stringify(i,null,2))}}catch{}try{let{getActiveModel:o}=Un(),s=Nn();pb(n.score,{version:s.version,model:o?o():null,sessionName:"_autosave",issues:n.issues})}catch{}}catch{}}var{getMemoryContext:mb}=rn(),{getDeploymentContextBlock:hb}=oa(),{checkPermission:gb,setPermission:$b,savePermissions:yb}=qs(),{confirm:Xf,setAllowAlwaysHandler:wb,getAutoConfirm:bb}=Xe(),{isPlanMode:Gs,getPlanModePrompt:_b,PLAN_MODE_ALLOWED_TOOLS:Jf,setPlanContent:xb,extractStepsFromText:kb,createPlan:vb,getActivePlan:Cv,startExecution:Av,advancePlanStep:Sb,getPlanStepInfo:Eb}=Dt(),{StreamRenderer:Tb}=xf(),{runHooks:qf}=ya(),{routeMCPCall:Rb,getMCPToolDefinitions:Cb}=Co(),{getSkillInstructions:Ab,getSkillToolDefinitions:Ob,routeSkillCall:Nb}=nn(),{trackUsage:Ff,estimateTokens:Uf}=Fn();function Wf(t){return!t||typeof t!="string"?0:typeof Uf=="function"?Uf(t):Math.ceil(t.length/4)}var{validateToolArgs:Mb}=Ti(),{filterToolsForModel:Bf,getModelTier:Pb,PROVIDER_DEFAULT_TIER:Ov}=Uo(),{getConfiguredProviders:Ib,getActiveProviderName:es,getActiveModelId:Tn,setActiveModel:Hf,MODEL_EQUIVALENTS:yr}=Te(),Ia=require("fs"),La=require("path"),Lb=(()=>{let t=parseInt(process.env.NEX_MILESTONE_STEPS??"5",10);return Number.isFinite(t)&&t>=0?t:5})();function Db(t){let e=tb(t.phaseName,t.stepCount,t.toolCounts,t.elapsed,t.filesRead,t.filesModified);process.stdout.write(`${e}
|
|
598
599
|
`)}var Gf=/(?:^|\s)((?:~|\.{1,2})?(?:\/[\w.\-@() ]+)+\.(?:png|jpe?g|gif|webp|bmp|tiff?))(?:\s|$)/gi;function jb(t){let e=[],n;for(Gf.lastIndex=0;(n=Gf.exec(t))!==null;){let o=n[1].trim(),s=o.startsWith("~")?o.replace("~",process.env.HOME||""):La.resolve(o);Ia.existsSync(s)&&e.push({raw:o,abs:s})}return e}function qb(t){let e=Ia.readFileSync(t),n=La.extname(t).toLowerCase().replace(".",""),o=n==="jpg"||n==="jpeg"?"image/jpeg":n==="png"?"image/png":n==="gif"?"image/gif":n==="webp"?"image/webp":"image/png";return{data:e.toString("base64"),media_type:o}}function Vf(t){let e=jb(t);if(e.length===0)return t;let n=[{type:"text",text:t}];for(let o of e)try{let{data:s,media_type:r}=qb(o.abs);n.push({type:"image",media_type:r,data:s})}catch{}return n.length>1?n:t}function Fb(t){if(!t||t.length<200)return{text:t,truncated:!1,repeatCount:0};let n=t.split(/(?<=\. )/).filter(l=>l.trim().length>0);if(n.length<6)return{text:t,truncated:!1,repeatCount:0};let o=new Map;for(let l=0;l<=n.length-3;l++){let u=n.slice(l,l+3).join("").trim();u.length>30&&o.set(u,(o.get(u)||0)+1)}let s=0,r="";for(let[l,u]of o)u>s&&(s=u,r=l);if(s<3)return{text:t,truncated:!1,repeatCount:s};let i=`
|
|
599
600
|
|
|
600
601
|
[SYSTEM: Output repetition detected \u2014 response truncated (${s}\xD7 repeated paragraph)]`,a;if(t.length>8e3)a=t.slice(0,3e3)+i;else{let l=0,u=-1,d=0;for(;l<2;){let f=t.indexOf(r,d);if(f===-1)break;l++,u=f+r.length,d=f+1}a=u>0?t.slice(0,u)+i:t.slice(0,3e3)+i}return{text:a,truncated:!0,repeatCount:s}}function Ra(t,e=5){if(!t||t.length<40)return{text:t,truncated:!1,repeatCount:0};let n=t.split(`
|
|
601
602
|
`),o=new Map;for(let f of n){let m=f.trim();m.length>=20&&o.set(m,(o.get(m)||0)+1)}let s=0,r="";for(let[f,m]of o)m>s&&(s=m,r=f);if(s<=e)return{text:t,truncated:!1,repeatCount:s};let i=`
|
|
602
603
|
|
|
603
|
-
\u26A0 [Response truncated: repeated paragraph detected (${s}\xD7)]`,a=0,l=-1,u=0;for(;a<e;){let f=t.indexOf(r,u);if(f===-1)break;a++,l=f+r.length,u=f+1}return{text:l>0?t.slice(0,l)+i:t.slice(0,2e3)+i,truncated:!0,repeatCount:s}}var xa=null,ka=null,va=null;function Je(){if(xa===null){let{TOOL_DEFINITIONS:t}=Et();xa=t}return ka===null&&(ka=Ob()),va===null&&(va=Cb()),[...xa,...ka,...va]}var Ca=50;function Ub(t){Number.isFinite(t)&&t>0&&(Ca=t)}var fr=()=>null;function Wb(t){fr=t}var Ws=null,Aa=null,ts=null,pr=new Map,Bb=1e4,Hb=6e3,Gb=/\b((?:API|ACCESS|AUTH|BEARER|CLIENT|GITHUB|GITLAB|SLACK|STRIPE|TWILIO|SENDGRID|AWS|GCP|AZURE|OPENAI|ANTHROPIC|GEMINI|OLLAMA)[_A-Z0-9]*(?:KEY|TOKEN|SECRET|PASS(?:WORD)?|CREDENTIAL)[_A-Z0-9]*)\s*=\s*["']?([A-Za-z0-9\-_.+/=]{10,})["']?/g;function Kb(t){return!t||typeof t!="string"?t:t.replace(Gb,(e,n)=>`${n}=***REDACTED***`)}var Yb=7e3,zb=4e3;function Xb(t,e=null){let n=Kb(t),o=cb(n),s=e==="read_file"?Yb:Bb,r=e==="read_file"?zb:Hb;if(o>s)try{let{compressToolResult:i}=Qe();return i(n,r)}catch{return n}return n}function Qf(t){try{let{getActiveModel:e}=
|
|
604
|
+
\u26A0 [Response truncated: repeated paragraph detected (${s}\xD7)]`,a=0,l=-1,u=0;for(;a<e;){let f=t.indexOf(r,u);if(f===-1)break;a++,l=f+r.length,u=f+1}return{text:l>0?t.slice(0,l)+i:t.slice(0,2e3)+i,truncated:!0,repeatCount:s}}var xa=null,ka=null,va=null;function Je(){if(xa===null){let{TOOL_DEFINITIONS:t}=Et();xa=t}return ka===null&&(ka=Ob()),va===null&&(va=Cb()),[...xa,...ka,...va]}var Ca=50;function Ub(t){Number.isFinite(t)&&t>0&&(Ca=t)}var fr=()=>null;function Wb(t){fr=t}var Ws=null,Aa=null,ts=null,pr=new Map,Bb=1e4,Hb=6e3,Gb=/\b((?:API|ACCESS|AUTH|BEARER|CLIENT|GITHUB|GITLAB|SLACK|STRIPE|TWILIO|SENDGRID|AWS|GCP|AZURE|OPENAI|ANTHROPIC|GEMINI|OLLAMA)[_A-Z0-9]*(?:KEY|TOKEN|SECRET|PASS(?:WORD)?|CREDENTIAL)[_A-Z0-9]*)\s*=\s*["']?([A-Za-z0-9\-_.+/=]{10,})["']?/g;function Kb(t){return!t||typeof t!="string"?t:t.replace(Gb,(e,n)=>`${n}=***REDACTED***`)}var Yb=7e3,zb=4e3;function Xb(t,e=null){let n=Kb(t),o=cb(n),s=e==="read_file"?Yb:Bb,r=e==="read_file"?zb:Hb;if(o>s)try{let{compressToolResult:i}=Qe();return i(n,r)}catch{return n}return n}function Qf(t){try{let{getActiveModel:e}=Te(),n=e(),o=n?`${n.provider}:${n.id}`:"default";if(pr.has(o))return pr.get(o);let s=Bf(t);return pr.set(o,s),s}catch{return Bf(t)}}function Jb(){pr.clear()}async function Zf(){try{let t=require("fs").promises,e=require("path"),n=[e.join(process.cwd(),"package.json"),e.join(process.cwd(),".git","HEAD"),e.join(process.cwd(),"README.md"),e.join(process.cwd(),"NEX.md")],s=(await Promise.allSettled(n.map(r=>t.stat(r).then(i=>`${r}:${i.mtimeMs}`)))).filter(r=>r.status==="fulfilled").map(r=>r.value);try{let{getMemoryContextHash:r}=rn(),i=r();i&&s.push(`memory:${i}`)}catch{}try{let r=e.join(process.cwd(),".nex","brain");if(Ia.existsSync(r)){let i=await t.stat(r);s.push(`brain:${i.mtimeMs}`)}}catch{}return s.join("|")}catch{return`fallback:${Date.now()}`}}function Oa(){Ws=null,Aa=null,ts=null}var Vb=new Set(["spawn_agents"]),Sa=5,Ea=3,dr=2,Qb=parseInt(process.env.NEX_STALE_WARN_MS||"60000",10),Kf=parseInt(process.env.NEX_STALE_ABORT_MS||"120000",10),Zb=process.env.NEX_STALE_AUTO_SWITCH!=="0";function e_(t){try{let e=require("fs"),n=require("path"),o=n.join(process.cwd(),".nex","plans");e.existsSync(o)||e.mkdirSync(o,{recursive:!0});let s=n.join(o,"current-plan.md");e.writeFileSync(s,t,"utf-8")}catch{}}wb(t=>{$b(t,"allow"),yb(),console.log(`${$.green} \u2713 ${t}: always allow${$.reset}`)});async function t_(t){let e=t.function.name,n=ob(t.function.arguments),o=t.id||`cli-${Date.now()}-${Math.random().toString(36).slice(2,6)}`;if(!n){let l=Je().find(d=>d.function.name===e),u=l?JSON.stringify(l.function.parameters,null,2):"unknown";return re(`${$.yellow} \u26A0 ${e}: malformed arguments, sending schema hint${$.reset}`),{callId:o,fnName:e,args:null,canExecute:!1,errorResult:{role:"tool",content:`ERROR: Malformed tool arguments. Could not parse your arguments as JSON.
|
|
604
605
|
Raw input: ${typeof t.function.arguments=="string"?t.function.arguments.substring(0,200):"N/A"}
|
|
605
606
|
|
|
606
607
|
Expected JSON schema for "${e}":
|
|
@@ -919,7 +920,7 @@ You have access to a persistent knowledge base in .nex/brain/.
|
|
|
919
920
|
${$.dim} ${a}${$.reset}`),l>=30&&process.stdout.isTTY){let u=n.size>0?`Done \u2014 ${n.size} ${n.size===1?"file":"files"} modified in ${l}s`:`Done \u2014 ${t} ${t===1?"step":"steps"} in ${l}s`;h_(u)}n.size>0?console.log(`${$.dim} \u{1F4A1} /diff \xB7 /commit \xB7 /undo${$.reset}`):!r&&o.size>=5&&n.size===0&&t>=3?console.log(`${$.dim} \u{1F4A1} Found issues? Say "fix 1" or "apply all fixes"${$.reset}`):o.size>0&&t>=2&&console.log(`${$.dim} \u{1F4A1} /save \xB7 /clear${$.reset}`)}async function g_(){if(!process.stdout.isTTY)return{action:"quit"};let t=es(),e=Tn(),n=yr.fast?.[t],o=yr.strong?.[t],s=n&&n!==e,r=o&&o!==e&&o!==n,i=[];i.push({key:"r",label:`Retry with current model ${$.dim}(${e})${$.reset}`}),s&&i.push({key:"f",label:`Switch to ${$.bold}${n}${$.reset} ${$.dim}\u2014 fast, low latency${$.reset}`,model:n}),r&&i.push({key:"s",label:`Switch to ${$.bold}${o}${$.reset} ${$.dim}\u2014 reliable tool-calling, medium speed${$.reset}`,model:o}),i.push({key:"q",label:`${$.dim}Quit${$.reset}`}),console.log(),console.log(`${$.yellow} Stream stale \u2014 all retries exhausted.${$.reset} What would you like to do?`);for(let a of i)console.log(` ${$.cyan}[${a.key}]${$.reset} ${a.label}`);return process.stdout.write(` ${$.yellow}> ${$.reset}`),new Promise(a=>{let l=process.stdin,u=l.isRaw;l.setRawMode(!0),l.resume(),l.setEncoding("utf8");let d=!1,f=m=>{if(d)return;d=!0,l.removeListener("data",f),l.setRawMode(u||!1),l.pause();let h=m.toLowerCase().trim();if(process.stdout.write(`${h}
|
|
920
921
|
`),m==="")return a({action:"quit"});let p=i.find(g=>g.key===h);!p||p.key==="q"||!p.model&&p.key!=="r"?a({action:"quit"}):p.key==="r"?a({action:"retry"}):a({action:"switch",model:p.model,provider:t})};l.on("data",f)})}var qt=null;async function $_(t,e=null,n={}){qt=e;let o=Vf(t);V.push({role:"user",content:o}),u_();let s=n.autoOrchestrate||process.env.NEX_AUTO_ORCHESTRATE==="true",r=parseInt(process.env.NEX_ORCHESTRATE_THRESHOLD||"3",10);try{let{detectComplexPrompt:qe,runOrchestrated:Ne}=Us(),Fe=qe(typeof t=="string"?t:"");if(s&&Fe.isComplex&&Fe.estimatedGoals>=r)return console.log(`${$.yellow}\u26A1 Auto-orchestrate: ${Fe.estimatedGoals} goals \u2192 parallel agents${$.reset}`),await Ne(t,{orchestratorModel:n.orchestratorModel||process.env.NEX_ORCHESTRATOR_MODEL,workerModel:n.model});Fe.isComplex&&console.log(`${$.dim}Hint: ~${Fe.estimatedGoals} goals. Try --auto-orchestrate for parallel execution.${$.reset}`)}catch{}let{setOnChange:i}=Bo(),a=null,l=0;i((qe,Ne)=>{qe==="create"?(a&&a.stop(),a=new Z0(Ne.name,Ne.tasks),a.setStats({tokens:l}),a.start()):qe==="update"&&a?a.updateTask(Ne.id,Ne.status):qe==="clear"&&a&&(a.stop(),a=null)});let u=await sp(),d=u;try{let{getBrainContext:qe}=Rs(),Ne=await qe(t);Ne&&(d=u+`
|
|
921
922
|
`+Ne+`
|
|
922
|
-
`)}catch(qe){process.env.NEX_DEBUG&&console.error("[agent] brain context failed:",qe.message)}let f=[{role:"system",content:d},...V],m=new Zn("Thinking...");m.start();let p=V.length===1?m_().catch(()=>null):Promise.resolve(null),g=Je(),[{messages:y,compressed:w,compacted:k,tokensRemoved:R},x]=await Promise.all([ab(f,g),p]),_=Ot(f,g);if(m.stop(),x&&console.log(`${$.dim}${x}${$.reset}`),k)console.log(`${$.dim} [context compacted \u2014 summary (~${R} tokens freed)]${$.reset}`);else if(w){let qe=_.limit>0?Math.round(R/_.limit*100):0;re(`${$.dim} [context compressed \u2014 ~${R} tokens freed (${qe}%)]${$.reset}`)}_.percentage>85&&re(`${$.yellow} \u26A0 Context ${Math.round(_.percentage)}% used (${Math.round(100-_.percentage)}% remaining) \u2014 consider /clear or /save + start fresh${$.reset}`);let b=y;if(Ot(b,Je()).percentage>=65){let{messages:Ne,tokensRemoved:Fe}=jt(b,Je());Fe>0&&(b=Ne,console.log(`${$.dim} [pre-flight compress \u2014 ${Fe} tokens freed, now ${Math.round(Ot(b,Je()).percentage)}% used]${$.reset}`))}let O=0,N=0,C=0,L=0,Ee=0,me=9,
|
|
923
|
+
`)}catch(qe){process.env.NEX_DEBUG&&console.error("[agent] brain context failed:",qe.message)}let f=[{role:"system",content:d},...V],m=new Zn("Thinking...");m.start();let p=V.length===1?m_().catch(()=>null):Promise.resolve(null),g=Je(),[{messages:y,compressed:w,compacted:k,tokensRemoved:R},x]=await Promise.all([ab(f,g),p]),_=Ot(f,g);if(m.stop(),x&&console.log(`${$.dim}${x}${$.reset}`),k)console.log(`${$.dim} [context compacted \u2014 summary (~${R} tokens freed)]${$.reset}`);else if(w){let qe=_.limit>0?Math.round(R/_.limit*100):0;re(`${$.dim} [context compressed \u2014 ~${R} tokens freed (${qe}%)]${$.reset}`)}_.percentage>85&&re(`${$.yellow} \u26A0 Context ${Math.round(_.percentage)}% used (${Math.round(100-_.percentage)}% remaining) \u2014 consider /clear or /save + start fresh${$.reset}`);let b=y;if(Ot(b,Je()).percentage>=65){let{messages:Ne,tokensRemoved:Fe}=jt(b,Je());Fe>0&&(b=Ne,console.log(`${$.dim} [pre-flight compress \u2014 ${Fe} tokens freed, now ${Math.round(Ot(b,Je()).percentage)}% used]${$.reset}`))}let O=0,N=0,C=0,L=0,Ee=0,me=9,le=0,ie=(()=>{let qe=V.find(Ne=>Ne.role==="user");return typeof qe?.content=="string"?qe.content:""})(),oe=/set_reminder|google.?auth|cron:|api\.log|jarvis.{0,30}(fehler|error|fail|broken|crash|nicht.{0,10}(funktioniert|läuft)|debug)|(?:fehler|error|crash|broken).{0,30}jarvis/i.test(ie),ce=0,Q=0,E=new Map,M=new Set,U=new Set,G=Date.now(),ne=new nb(Lb),I=np,W=2,se=4,X=ep,Z=5,he=8,pe=tp,we=4,be=7,je=Na,Ge=3,Jt=4,On=Ma,Pr=2,km=3,cn=0,vm=6,Sm=10,ln=0,dc=5,un=0,Em=2,Tm=3,Ir=0,Lr=8,Rm=12,dt,oo=Ca,Dr=0,fc=3,jr=!1;e:for(;;){for(jr=!1,dt=0;dt<oo&&!fr()?.aborted;dt++){{let S=Je(),v=Ot(b,S),J=Q===0?65:78;if(v.percentage>=J){let{messages:j,tokensRemoved:z}=jt(b,S,Q===0);z>0&&(b=j,z>50&&console.log(`${$.dim} [auto-compressed \u2014 ~${z} tokens freed, now ${Math.round(Ot(b,S).percentage)}%]${$.reset}`))}}let Ne=!0;Q>0&&Sb();let Fe=null;if(a&&a.isActive())a._paused&&a.resume();else if(!a){let S,v=Eb();if(v&&v.total>1){let J=v.description.length>40?v.description.slice(0,37)+"\u2026":v.description;S=`Plan step ${v.current}/${v.total}: ${J}`}else S=Q>0?`Thinking... (step ${Q+1})`:"Thinking...";Fe=new Zn(S),Fe.start()}let qr=!0,Ft="",Fr=!1,ft=new Tb,Pt,Ur=Date.now(),ro=!1,Wr=new AbortController,pc=setInterval(()=>{let S=Date.now()-Ur;if(S>=Kf)ft._clearCursorLine(),re(`${$.yellow} \u26A0 Stream stale for ${Math.round(S/1e3)}s \u2014 aborting and retrying${$.reset}`),Wr.abort();else if(S>=Qb&&!ro){ro=!0,ft._clearCursorLine();let v=yr.fast?.[es()],J=C>0?` (retry ${C+1}/${dr})`:"",j=Math.round((Kf-S)/1e3);re(`${$.yellow} \u26A0 No tokens received for ${Math.round(S/1e3)}s \u2014 waiting...${J}${$.reset}`),v&&v!==Tn()?console.log(`${$.dim} \u{1F4A1} Will auto-switch to ${v} in ~${j}s if no tokens arrive${$.reset}`):console.log(`${$.dim} \u{1F4A1} Ctrl+C to abort \xB7 auto-abort in ~${j}s${$.reset}`)}},5e3),bt="",Ut=null;try{let S=Qf(Je()),v=Gs()?S.filter(z=>Jf.has(z.function.name)):S,J=fr(),j=new AbortController;J&&J.addEventListener("abort",()=>j.abort(),{once:!0}),Wr.signal.addEventListener("abort",()=>j.abort(),{once:!0}),Pt=await sb(b,v,{signal:j.signal,onThinkingToken:()=>{Ur=Date.now(),ro=!1,qt?.onThinkingToken&&qt.onThinkingToken()},onToken:z=>{if(Ur=Date.now(),ro=!1,qt?.onToken){qt.onToken(z),Ft+=z;return}if(Ft+=z,!Fr&&Ft.length>400&&Ft.length%250<z.length+1){let ue=Ra(Ft,3);ue.truncated&&(Fr=!0,ft._clearCursorLine?.(),re(`${$.yellow} \u26A0 LLM stream loop detected (${ue.repeatCount}\xD7 repeated) \u2014 suppressing display${$.reset}`))}Fr||(bt+=z,process.stdout.isTTY?Ut||(Ut=setTimeout(()=>{bt&&ft&&ft.push(bt),bt="",Ut=null},50)):(ft.push(bt),bt=""),qr&&(a&&!a._paused?a.pause():Fe&&Fe.stop(),Ne||(Ne=!0),ft.startCursor(),qr=!1))}})}catch(S){if(clearInterval(pc),Ut&&(clearTimeout(Ut),Ut=null),bt&&ft&&(ft.push(bt),bt=""),a&&!a._paused&&a.pause(),Fe&&Fe.stop(),ft.stopCursor(),Wr.signal.aborted&&!fr()?.aborted){if(C++,C>dr){if(L<1){L++,Qn("Stale retries exhausted \u2014 last-resort force-compress...",$.yellow);let D=Je(),{messages:B,tokensRemoved:ee}=jt(b,D);b=B,ee>50&&re(`${$.dim} [force-compressed \u2014 ~${ee} tokens freed]${$.reset}`),C=0,dt--;continue}a&&(a.stop(),a=null);let ue=await g_();if(ue.action==="quit"){i(null),We(Q,E,M,U,G),Ue(V);break}ue.action==="switch"&&(Hf(`${ue.provider}:${ue.model}`),console.log(`${$.green} \u2713 Switched to ${ue.provider}:${ue.model}${$.reset}`)),C=0,dt--;continue}let j=C===1?3e3:5e3;if(C>=1&&le<1){le++,Qn(`Stale retry ${C}/${dr} \u2014 force-compressing before retry...`,$.yellow);let ue=Je(),{messages:D,tokensRemoved:B}=jt(b,ue,!0);if(b=D,B>0&&B>50&&re(`${$.dim} [force-compressed \u2014 ~${B} tokens freed]${$.reset}`),Zb){let ee=yr.fast?.[es()];ee&&ee!==Tn()&&(Hf(`${es()}:${ee}`),console.log(`${$.cyan} \u26A1 Auto-switched to ${ee} to avoid further stale timeouts${$.reset}`),console.log(`${$.dim} (disable with NEX_STALE_AUTO_SWITCH=0)${$.reset}`))}}else re(`${$.yellow} \u26A0 Stale retry ${C}/${dr} \u2014 retrying in ${j/1e3}s...${$.reset}`);let z=new Zn(`Waiting ${j/1e3}s before retry...`);z.start(),await new Promise(ue=>setTimeout(ue,j)),z.stop(),dt--;continue}if(S.name==="AbortError"||S.name==="CanceledError"||S.message?.includes("canceled")||S.message?.includes("aborted")){a&&(a.stop(),a=null),i(null),We(Q,E,M,U,G),Ue(V);break}let v=S.message;if(S.code==="ECONNREFUSED"||S.message.includes("ECONNREFUSED"))v="Connection refused \u2014 please check your internet connection or API endpoint";else if(S.code==="ENOTFOUND"||S.message.includes("ENOTFOUND"))v="Network error \u2014 could not reach the API server. Please check your connection";else if(S.code==="ETIMEDOUT"||S.message.includes("timeout"))v="Request timed out \u2014 the API server took too long to respond. Please try again";else if(S.message.includes("401")||S.message.includes("Unauthorized"))v="Authentication failed \u2014 please check your API key in the .env file";else if(S.message.includes("403")||S.message.includes("Forbidden"))v="Access denied \u2014 your API key may not have permission for this model";else if(S.message.includes("404")){v=`Model not found (404): ${Tn?Tn():"unknown"} \u2014 check your .env MODEL setting or run /models to list available models`,console.log(`${$.red} \u2717 ${v}${$.reset}`),a&&(a.stop(),a=null),i(null),We(Q,E,M,U,G),Ue(V);break}else if(S.message.includes("400")){if(L<3&&Ee<me){L++,Ee++;let j=Q===0&&L===1,z=j||L===3||le>0;if(j){L=3;let ee=S.message.replace(/^API Error(\s*\[HTTP \d+\])?:\s*/i,"").slice(0,150);Qn(`Bad request (400) \u2014 ${ee||"system prompt too large"}, compressing...`,$.yellow)}else Qn(z?`Bad request (400) \u2014 nuclear compression (attempt ${L}/3, dropping history)...`:`Bad request (400) \u2014 force-compressing and retrying... (attempt ${L}/3)`,$.yellow);let ue=Je(),{messages:D,tokensRemoved:B}=jt(b,ue,z);b=D,B>50&&re(`${$.dim} [force-compressed \u2014 ~${B} tokens freed]${$.reset}`),dt--;continue}{let j=b.find(de=>de.role==="system"),z=b.find(de=>de.role==="user"&&!String(de.content).startsWith("[SYSTEM")&&!String(de.content).startsWith("BLOCKED:")),ue=[j,z].filter(Boolean),{getUsage:D}=Qe(),B=Qe().estimateMessagesTokens(ue),ee=Qe().estimateMessagesTokens(b);if(B<ee){let de=[],ge=V.filter(fe=>fe.role==="assistant"&&typeof fe.content=="string"&&fe.content.trim().length>30).slice(-3).map(fe=>fe.content.trim().slice(0,120).replace(/\n+/g," "));ge.length>0&&de.push(`Key findings:
|
|
923
924
|
`+ge.map(fe=>`- ${fe}`).join(`
|
|
924
925
|
`));let Ie=V.filter(fe=>fe.role==="tool"&&typeof fe.content=="string"&&!fe.content.startsWith("BLOCKED:")&&fe.content.trim().length>10).slice(-3).map(fe=>fe.content.trim().split(`
|
|
925
926
|
`).slice(0,3).join(`
|
|
@@ -928,30 +929,30 @@ ${$.dim} ${a}${$.reset}`),l>=30&&process.stdout.isTTY){let u=n.size>0?`Done \u2
|
|
|
928
929
|
`)),M.size>0){let fe=[...M].map(pt=>pt.split("/").slice(-2).join("/")).join(", ");de.unshift(`Already modified: ${fe} \u2014 use edit_file to add missing pieces only, DO NOT use write_file on these files.`)}if(de.length>0){let fe={role:"user",content:`[SYSTEM: Findings from investigation before context wipe]
|
|
929
930
|
${de.join(`
|
|
930
931
|
`)}
|
|
931
|
-
Continue implementing the fixes based on these findings.`};
|
|
932
|
-
Files modified so far: ${[...M].map(pt=>pt.split("/").slice(-1)[0]).join(", ")}`:"";re(`${$.red} \u2717 Super-nuclear limit reached (3\xD7) \u2014 aborting to prevent runaway context loop${$.reset}`),console.log(`${$.yellow} \u{1F4A1} Task may exceed model context. Try /clear and break it into smaller steps.${fe?$.dim+fe:""}${$.reset}`),a&&(a.stop(),a=null),i(null),We(
|
|
932
|
+
Continue implementing the fixes based on these findings.`};ue.push(fe)}if(Hs>=3){let fe=M.size>0?`
|
|
933
|
+
Files modified so far: ${[...M].map(pt=>pt.split("/").slice(-1)[0]).join(", ")}`:"";re(`${$.red} \u2717 Super-nuclear limit reached (3\xD7) \u2014 aborting to prevent runaway context loop${$.reset}`),console.log(`${$.yellow} \u{1F4A1} Task may exceed model context. Try /clear and break it into smaller steps.${fe?$.dim+fe:""}${$.reset}`),a&&(a.stop(),a=null),i(null),We(Q,E,M,U,G,{suppressHint:!0}),Ue(V);break}if(b=ue,Hs++,Nt=0,ce=0,Ma.clear(),ns.clear(),ss.clear(),Na.clear(),Qn(`Super-nuclear compression \u2014 dropped all history, keeping original task only (${ee-B} tokens freed)`,$.yellow),Hs>=1){let fe={role:"user",content:`[SYSTEM WARNING] Context wiped ${Hs}\xD7. SKIP investigation \u2014 implement directly using findings above. Max 5 tool calls total, then finish.
|
|
933
934
|
|
|
934
|
-
CRITICAL: If you must re-read a file, use line_start/line_end to read ONLY the section you need (e.g. last 50 lines). Never read a full large file again \u2014 that is what caused the context overflow.`};V.push(fe),b.push(fe)}L=0,dt--;continue}}v="Context too large to compress \u2014 use /clear to start fresh"}else S.message.includes("500")||S.message.includes("502")||S.message.includes("503")||S.message.includes("504")?v="API server error \u2014 the provider is experiencing issues. Please try again in a moment":(S.message.includes("fetch failed")||S.message.includes("fetch"))&&(v="Network request failed \u2014 please check your internet connection");if(console.log(`${$.red} \u2717 ${v}${$.reset}`),S.message.includes("429")){if(O++,O>Sa){console.log(`${$.red} Rate limit: max retries (${Sa}) exceeded. Try again later or use /budget to check your limits.${$.reset}`),a&&(a.stop(),a=null),i(null),We(
|
|
935
|
+
CRITICAL: If you must re-read a file, use line_start/line_end to read ONLY the section you need (e.g. last 50 lines). Never read a full large file again \u2014 that is what caused the context overflow.`};V.push(fe),b.push(fe)}L=0,dt--;continue}}v="Context too large to compress \u2014 use /clear to start fresh"}else S.message.includes("500")||S.message.includes("502")||S.message.includes("503")||S.message.includes("504")?v="API server error \u2014 the provider is experiencing issues. Please try again in a moment":(S.message.includes("fetch failed")||S.message.includes("fetch"))&&(v="Network request failed \u2014 please check your internet connection");if(console.log(`${$.red} \u2717 ${v}${$.reset}`),S.message.includes("429")){if(O++,O>Sa){console.log(`${$.red} Rate limit: max retries (${Sa}) exceeded. Try again later or use /budget to check your limits.${$.reset}`),a&&(a.stop(),a=null),i(null),We(Q,E,M,U,G),Ue(V);break}let j=Math.min(1e4*Math.pow(2,O-1),12e4),z=new Zn(`Rate limit \u2014 waiting ${Math.round(j/1e3)}s (retry ${O}/${Sa})`);z.start(),await new Promise(ue=>setTimeout(ue,j)),z.stop();continue}if(S.message.includes("socket disconnected")||S.message.includes("TLS")||S.message.includes("ECONNRESET")||S.message.includes("ECONNABORTED")||S.message.includes("ETIMEDOUT")||S.code==="ECONNRESET"||S.code==="ECONNABORTED"){if(N++,N>Ea){console.log(`${$.red} Network error: max retries (${Ea}) exceeded. Check your connection and try again.${$.reset}`),a&&(a.stop(),a=null),i(null),We(Q,E,M,U,G),Ue(V);break}let j=Math.min(2e3*Math.pow(2,N-1),3e4),z=new Zn(`Network error \u2014 retrying in ${Math.round(j/1e3)}s (${N}/${Ea})`);z.start(),await new Promise(ue=>setTimeout(ue,j)),z.stop(),dt--;continue}a&&(a.stop(),a=null),i(null),We(Q,E,M,U,G),Ue(V);break}if(clearInterval(pc),O=0,N=0,qr&&(a&&!a._paused&&a.pause(),Fe&&Fe.stop()),Ut&&(clearTimeout(Ut),Ut=null),bt&&ft&&(ft.push(bt),bt=""),Ft&&ft.flush(),N=0,C=0,Pt&&Pt.usage){let S=Pt.usage.prompt_tokens||0,v=Pt.usage.completion_tokens||0;Ff(es(),Tn(),S,v),l+=S+v,a&&a.setStats({tokens:l})}else if(Pt&&!Pt.usage){let S=b.map(j=>typeof j.content=="string"?j.content:Array.isArray(j.content)?j.content.map(z=>typeof z=="string"?z:z.text||"").join(""):"").join(" "),v=Wf(S),J=Wf(Pt.content||Ft||"");Ff(es(),Tn(),v,J),l+=v+J,a&&a.setStats({tokens:l})}let{content:mc,tool_calls:dn}=Pt,io=Ra(mc||""),hc=io.truncated?io.text:mc;io.truncated&&re(`${$.yellow} \u26A0 LLM output loop detected (${io.repeatCount}\xD7 repeated paragraph) \u2014 response truncated${$.reset}`);let ao=Fb(hc||""),co=ao.truncated?ao.text:hc;ao.truncated&&re(`${$.yellow} \u26A0 LLM output loop detected (${ao.repeatCount}\xD7 repeated window) \u2014 response truncated${$.reset}`);let Br={role:"assistant",content:co||""};if(dn&&dn.length>0&&(Br.tool_calls=dn),V.push(Br),b.push(Br),!dn||dn.length===0){let S=(co||"").trim().length>0||Ft.trim().length>0,v=!1;if(os&&S&&(os=!1,Nt=0,v=!0),v&&S){let J=(co||"").trim();if(J.endsWith("?")||/\b(Wo |Bitte |Kannst du|Soll ich)\b/.test(J.slice(-200))){let z={role:"user",content:"[SYSTEM] Continue. Do not ask questions \u2014 implement the fix yourself using SSH. The server is at 94.130.37.43."};b.push(z),V.push(z);continue}}if(!S&&Q>0&&dt<Ca-1){let J={role:"user",content:"[SYSTEM] You ran tools but produced no visible output. The user CANNOT see tool results \u2014 only your text. Please summarize your findings now."};b.push(J),V.push(J);continue}if(Gs()&&S&&Q===0)if(mr++,mr>2)re(`${$.yellow} \u26A0 Plan accepted despite no file reads (rejection loop cap reached)${$.reset}`);else{let J={role:"user",content:`[SYSTEM] You wrote a plan without reading any files. This plan may be based on incorrect assumptions (wrong database type, wrong file structure, etc.).
|
|
935
936
|
|
|
936
|
-
MANDATORY: Use read_file, glob, or grep to investigate the actual codebase first. Read at least the relevant module file and route file before writing the plan.`};V.push(J),b.push(J),re(`${$.yellow} \u26A0 Plan rejected (${mr}/2): no files read \u2014 forcing investigation${$.reset}`);continue}if(Gs()&&S){let J=(co||Ft||"").trim();xb(J),e_(J);let j=kb(J);if(j.length>0){let z=V.find(ee=>ee.role==="user"),
|
|
937
|
+
MANDATORY: Use read_file, glob, or grep to investigate the actual codebase first. Read at least the relevant module file and route file before writing the plan.`};V.push(J),b.push(J),re(`${$.yellow} \u26A0 Plan rejected (${mr}/2): no files read \u2014 forcing investigation${$.reset}`);continue}if(Gs()&&S){let J=(co||Ft||"").trim();xb(J),e_(J);let j=kb(J);if(j.length>0){let z=V.find(ee=>ee.role==="user"),ue=typeof z?.content=="string"?z.content.slice(0,120):"Task";vb(ue,j);let D=j.length===1?"step":"steps",B=!1;if(process.stdout.isTTY&&process.stdin.isTTY){let{approvePlan:ee,startExecution:de,setPlanMode:ge}=Dt();process.stdout.write(`
|
|
937
938
|
${$.cyan}${$.bold}Plan ready${$.reset} ${$.dim}(${j.length} ${D})${$.reset} ${$.green}[A]${$.reset}${$.dim}pprove${$.reset} ${$.yellow}[E]${$.reset}${$.dim}dit${$.reset} ${$.red}[R]${$.reset}${$.dim}eject${$.reset} ${$.dim}[\u21B5 = approve]:${$.reset} `);let Ie=process.stdin.isRaw,fe=await new Promise(pt=>{try{process.stdin.setRawMode(!0)}catch{}process.stdin.resume(),process.stdin.once("data",lo=>{try{process.stdin.setRawMode(Ie||!1)}catch{}let Gr=lo.toString().toLowerCase()[0]||"\r";pt(Gr)})});if(process.stdout.write(`
|
|
938
939
|
`),fe==="r")console.log(`${$.red}Plan rejected.${$.reset} Ask follow-up questions to refine.`);else if(fe==="e")console.log(`${$.yellow}Type /plan edit to open in editor, or give feedback.${$.reset}`);else if(ee()){de(),ge(!1),Oa(),console.log(`${$.green}${$.bold}Approved!${$.reset} Executing ${j.length} ${D}...`);let pt=`[PLAN APPROVED \u2014 EXECUTE NOW]
|
|
939
940
|
|
|
940
941
|
Implement the following plan step by step. All tools are now available.
|
|
941
942
|
|
|
942
943
|
${J}`;V.push({role:"user",content:pt}),b.push({role:"user",content:pt}),B=!0}}else console.log(`
|
|
943
|
-
${$.cyan}${$.bold}Plan ready${$.reset} ${$.dim}(${j.length} ${D} extracted).${$.reset} Type ${$.cyan}/plan approve${$.reset}${$.dim} to execute, or ${$.reset}${$.cyan}/plan edit${$.reset}${$.dim} to review.${$.reset}`);if(B){a&&(a.stop(),a=null),dt--;continue}}else{let z=!1;if(process.stdout.isTTY&&process.stdin.isTTY){let{approvePlan:
|
|
944
|
+
${$.cyan}${$.bold}Plan ready${$.reset} ${$.dim}(${j.length} ${D} extracted).${$.reset} Type ${$.cyan}/plan approve${$.reset}${$.dim} to execute, or ${$.reset}${$.cyan}/plan edit${$.reset}${$.dim} to review.${$.reset}`);if(B){a&&(a.stop(),a=null),dt--;continue}}else{let z=!1;if(process.stdout.isTTY&&process.stdin.isTTY){let{approvePlan:ue,startExecution:D,setPlanMode:B}=Dt();process.stdout.write(`
|
|
944
945
|
${$.cyan}${$.bold}Plan ready.${$.reset} ${$.green}[A]${$.reset}${$.dim}pprove${$.reset} ${$.red}[R]${$.reset}${$.dim}eject${$.reset} ${$.dim}[\u21B5 = approve]:${$.reset} `);let ee=process.stdin.isRaw,de=await new Promise(ge=>{try{process.stdin.setRawMode(!0)}catch{}process.stdin.resume(),process.stdin.once("data",Ie=>{try{process.stdin.setRawMode(ee||!1)}catch{}ge(Ie.toString().toLowerCase()[0]||"\r")})});if(process.stdout.write(`
|
|
945
|
-
`),de==="r")console.log(`${$.red}Plan rejected.${$.reset} Ask follow-up questions to refine.`);else if(
|
|
946
|
+
`),de==="r")console.log(`${$.red}Plan rejected.${$.reset} Ask follow-up questions to refine.`);else if(ue()){D(),B(!1),Oa(),console.log(`${$.green}${$.bold}Approved!${$.reset} Executing...`);let Ie=`[PLAN APPROVED \u2014 EXECUTE NOW]
|
|
946
947
|
|
|
947
948
|
Implement the following plan step by step. All tools are now available.
|
|
948
949
|
|
|
949
950
|
${getPlanContent()||Pt.content}`;V.push({role:"user",content:Ie}),b.push({role:"user",content:Ie}),z=!0}}else console.log(`
|
|
950
|
-
${$.cyan}${$.bold}Plan ready.${$.reset} ${$.dim}Type ${$.reset}${$.cyan}/plan approve${$.reset}${$.dim} to execute, or ask follow-up questions to refine.${$.reset}`);if(z){a&&(a.stop(),a=null),dt--;continue}}}a&&(a.stop(),a=null),i(null),We(
|
|
951
|
-
`)):gc&&(Ne=!0,ls.skipSpinner=!0),a&&a._paused&&a.resume();let{results:$c,summaries:Am}=await o_(Pe,!0,{...ls,skipSummaries:!0});if(Hr&&(Hr=null,process.stdout.write(`\r\x1B[2K${_a(Pe,
|
|
952
|
-
`)),!ls.skipSummaries){let S=Am.filter((j,z)=>!(Pe[z]&&Pe[z].fnName==="ask_user"));for(let j of S)console.log(j);console.log("");let v=Pe.filter(j=>j&&j.fnName!=="ask_user").map(j=>j.fnName),J=ne.record(0,v,U,M);J&&Db(J)}for(let S of Pe){if(S.canExecute||!S.errorResult)continue;let v=typeof S.errorResult.content=="string"?S.errorResult.content:"";if((v.startsWith("BLOCKED:")||v.startsWith("PLAN MODE:"))&&(ln++,ln>=dc)){re(`${$.red} \u2716 Loop abort: ${ln} consecutive blocked calls (pre-execution) \u2014 model not heeding BLOCKED messages${$.reset}`),a&&(a.stop(),a=null),i(null),We(
|
|
953
|
-
`)[0],z=!j.startsWith("ERROR")&&!j.startsWith("CANCELLED")&&!j.startsWith("Command failed")&&!j.startsWith("EXIT");if(!z&&(v.fnName==="edit_file"||v.fnName==="patch_file")&&v.args?.path&&j.includes("old_text not found")&&ns.set(v.args.path,!0),z&&v.fnName==="write_file"&&v.args?.path){let D=v.args.path.split("/").pop(),B=v.args.path.includes("/tests/")||v.args.path.includes("\\tests\\");if(/^(test_|demo_|temp_|tmp_|scratch_)/.test(D)&&!B){re(`${$.yellow} \u26A0 Temp file: "${D}" \u2014 delete with bash rm when done to keep the workspace clean${$.reset}`);let de={role:"user",content:`[HINT] "${v.args.path}" looks like a temporary test/demo file. Delete it with bash("rm ${v.args.path}") as soon as you're done \u2014 orphaned temp files count against session quality.`};V.push(de),b.push(de)}}if(z&&["write_file","edit_file","patch_file"].includes(v.fnName)&&v.args&&v.args.path){ns.delete(v.args.path),M.add(v.args.path);let D=(I.get(v.args.path)||0)+1;I.set(v.args.path,D);let B=v.args.path.split("/").slice(-2).join("/");if(D===W){re(`${$.yellow} \u26A0 Loop warning: "${B}" edited ${D}\xD7 \u2014 possible edit loop${$.reset}`);let ee={role:"user",content:`[SYSTEM WARNING] "${v.args.path}" edited ${D}\xD7. One more edit max, then move on.`};V.push(ee),b.push(ee)}else if(D>=se){re(`${$.red} \u2716 Loop abort: "${B}" edited ${D}\xD7 \u2014 aborting to prevent runaway loop${$.reset}`),a&&(a.stop(),a=null),i(null),We(
|
|
954
|
-
${$.yellow}\u26A0 Max iterations reached.${$.reset}`),await Xf(" Continue for 20 more turns?")){oo=20;continue e}console.log(`${$.dim} Tip: set "maxIterations" in .nex/config.json or use --max-turns${$.reset}`)}break e}}op.exports={processInput:$_,clearConversation:l_,getConversationLength:d_,getConversationMessages:f_,setConversationMessages:p_,setAbortSignalGetter:Wb,setMaxIterations:Ub,invalidateSystemPromptCache:Oa,clearToolFilterCache:Jb,getCachedFilteredTools:Qf,buildSystemPrompt:sp,getProjectContextHash:Zf,buildUserContent:Vf,detectAndTruncateLoop:Ra,injectMidRunNote:r_}});var
|
|
951
|
+
${$.cyan}${$.bold}Plan ready.${$.reset} ${$.dim}Type ${$.reset}${$.cyan}/plan approve${$.reset}${$.dim} to execute, or ask follow-up questions to refine.${$.reset}`);if(z){a&&(a.stop(),a=null),dt--;continue}}}a&&(a.stop(),a=null),i(null),We(Q,E,M,U,G),Ue(V),jf(V);return}Q++,Q>=1&&(Ne=!1);for(let S of dn){let v=S.function.name;E.set(v,(E.get(v)||0)+1)}let Pe=await Promise.all(dn.map(S=>t_(S)));{let S=Ot(b,Je()),v=S.percentage,J=Pe.some(D=>D.canExecute&&D.fnName==="read_file"&&!D.args?.line_end),j=Pe.filter(D=>D.canExecute&&D.fnName==="read_file"&&D.args?.path&&On.get(D.args.path)>=1&&!D.args?.line_start).map(D=>D.args.path.split("/").slice(-2).join("/")),z=j.length>0;if(v>=70&&J&&Ir<70||v>=85&&Ir<85||z){Ir=v;let D=v>=85?"URGENT":"WARNING",B;z?(D="WARNING",B=`Unbounded re-read of ${j.join(", ")} \u2014 already in context. Use line_start/line_end to read specific sections instead.`):J?B=`Unbounded read at ${Math.round(v)}% context \u2014 use line_start/line_end to avoid overflow.`:B=`Context ${Math.round(v)}% used. Avoid large reads, wrap up with what you have.`;let ee={role:"user",content:`[SYSTEM ${D}] Context ${Math.round(v)}% used (${S.used}/${S.limit} tokens). ${B}`};if(V.push(ee),b.push(ee),v>=85){let de=z?` (re-read of: ${j.join(", ")})`:"";re(`${$.yellow} \u26A0 Context ${Math.round(v)}% used \u2014 agent warned to use targeted reads${de}${$.reset}`)}}}for(let S of Pe){if(!S.canExecute||S.fnName!=="read_file")continue;let v=S.args?.path;if(!v)continue;let J=On.get(v)||0,j=S.args?.line_start!=null,z=ns.get(v)===!0,ue=6;if(J>=ue){let D=v.split("/").slice(-2).join("/"),B=(ss.get(v)||0)+1;ss.set(v,B),B===1&&re(`${$.red} \u2716 Blocked: "${D}" read ${J}\xD7 \u2014 hard cap (${ue}) reached${$.reset}`),S.canExecute=!1,S.errorResult={role:"tool",content:`BLOCKED: read_file("${v}") denied \u2014 file already read ${J}\xD7 (hard cap: ${ue}). You have seen enough of this file. Use grep to find specific content or proceed with what you know.`,tool_call_id:S.callId}}else if(J>=1&&j)if(z){let D=v.split("/").slice(-2).join("/");console.log(`${$.cyan} \u21A9 Targeted re-read: "${D}" (line_start=${S.args.line_start}) \u2014 edit recovery${$.reset}`),ns.delete(v)}else{let D=S.args.line_start||1,B=S.args.line_end||D+350,ee=Bs.get(v)||[],de=!1;for(let[ge,Ie]of ee){let fe=Math.max(D,ge),pt=Math.min(B,Ie);if(pt>fe){let lo=pt-fe,Gr=B-D||1;if(lo/Gr>=.7){let wc=v.split("/").slice(-2).join("/");re(`${$.red} \u2716 Blocked duplicate read: "${wc}" lines ${D}-${B} (\u226570% overlap with lines ${ge}-${Ie} already in context)${$.reset}`),S.canExecute=!1,S.errorResult={role:"tool",content:`BLOCKED: read_file("${v}", lines ${D}-${B}) is a duplicate \u2014 lines ${ge}-${Ie} are already in your context (\u226570% overlap). Use grep to find specific content instead of re-reading.`,tool_call_id:S.callId},de=!0;break}}}if(!de){let ge=ee.length,Ie=2;if(ge>=3){let pt=v.split("/").slice(-2).join("/");re(`${$.red} \u2716 Blocked file-scroll: "${pt}" \u2014 ${ge} sections already read. Use grep to find specific content.${$.reset}`),S.canExecute=!1,S.errorResult={role:"tool",content:`BLOCKED: read_file("${v}") denied \u2014 you have already read ${ge} different sections of this file (file-scroll pattern). You have seen most of this file. Use grep_search to find the exact lines you need instead of continuing to scroll.`,tool_call_id:S.callId}}else ge>=Ie&&(S._scrollWarn={sectionCount:ge+1,path:v})}}else if(J>=1){let D=v.split("/").slice(-2).join("/"),B=(ss.get(v)||0)+1;ss.set(v,B),B===1&&re(`${$.red} \u2716 Blocked unbounded re-read: "${D}" \u2014 already in context. Use line_start/line_end for specific sections.${$.reset}`),S.canExecute=!1,S.errorResult={role:"tool",content:`BLOCKED: read_file("${v}") denied \u2014 file already in context (read ${J}\xD7). Use line_start/line_end to read a specific section instead of the full file.`,tool_call_id:S.callId}}}for(let S of Pe)S.canExecute&&(S.fnName!=="ssh_exec"&&S.fnName!=="bash"||/\bsed\s+-n\b/.test(S.args?.command||"")&&(re(`${$.red} \u2716 Blocked sed -n: use grep -n "pattern" <file> | head -30 instead${$.reset}`),S.canExecute=!1,S.errorResult={role:"tool",content:'BLOCKED: sed -n is forbidden \u2014 it floods context with line ranges. Use grep -n "pattern" <file> | head -30 to read a specific section, or cat <file> for the full file.',tool_call_id:S.callId}));for(let S of Pe){if(!S.canExecute||S.fnName!=="write_file")continue;let v=S.args?.path,J=S.args?.content||"";if(v)try{let j=require("fs"),z=require("path").resolve(process.cwd(),v);if(j.existsSync(z)){let ue=j.statSync(z).size,D=Buffer.byteLength(J,"utf8"),B=ue>0?D/ue:1;if(B<.6&&ue>200){let ee=v.split("/").slice(-2).join("/");console.log(`${$.red} \u2716 write_file shrink guard: "${ee}" would shrink to ${Math.round(B*100)}% of original \u2014 likely context loss${$.reset}`),S.canExecute=!1,S.errorResult={role:"tool",content:`BLOCKED: write_file("${v}") denied \u2014 new content is only ${Math.round(B*100)}% of current file size (${ue} \u2192 ${D} bytes). This looks like a partial rewrite after context loss. Use edit_file/patch_file to add only the new code, or read the file first to see full content before replacing.`,tool_call_id:S.callId}}}}catch{}}for(let S of Pe){if(!S.canExecute||S.fnName!=="grep")continue;let v=S.args?.path;if(!v)continue;let J=je.get(v)||0;if(J>=Jt){let j=v.split("/").slice(-2).join("/");re(`${$.red} \u2716 Blocked grep: "${j}" grepped ${J}\xD7 with different patterns \u2014 flood threshold exceeded${$.reset}`),S.canExecute=!1,S.errorResult={role:"tool",content:`BLOCKED: grep("${v}") denied \u2014 ${J} patterns already tried. Use existing results.`,tool_call_id:S.callId}}}if(os){let S=Pe.filter(j=>j.canExecute&&j.fnName==="ssh_exec"),v=Pe.some(j=>j.canExecute&&j.fnName!=="ssh_exec"),J=oe&&ce<3;if(S.length>0&&!v&&J&&gr<1)os=!1,gr++,Nt=Math.max(0,Lr-2),re(`${$.dim} [dual-block deadlock: SSH storm relaxed \u2014 allowing 1 SSH call (relax ${gr}/1)]${$.reset}`);else for(let j of S)j.canExecute=!1,j.errorResult={role:"tool",content:`BLOCKED: ssh_exec denied \u2014 SSH storm (${Lr}+ calls). Synthesize findings now.`,tool_call_id:j.callId}}if(oe&&ce<3){for(let S of Pe)if(S.canExecute&&["bash","read_file","find_files"].includes(S.fnName)){ce++;{let v=Je(),{messages:J}=jt(b,v);b=J}re(`${$.yellow} \u26A0 Jarvis-local guard: blocking local ${S.fnName} \u2014 use ssh_exec on 94.130.37.43${$.reset}`),S.canExecute=!1,S.errorResult={role:"tool",content:`BLOCKED: ${S.fnName} denied \u2014 this is a server issue. Use ssh_exec on 94.130.37.43 instead.`,tool_call_id:S.callId};break}}let ls=a?{skipSpinner:!0,skipSummaries:!0}:{},Cm=Pe.some(S=>S.fnName==="ask_user"),gc=!ls.skipSummaries&&!Ne,Hr=null;gc&&!Cm?(Ne=!0,ls.skipSpinner=!0,process.stdout.isTTY?(process.stdout.write(_a(Pe,Q,!1,"blink")),Hr=!0):qt||process.stdout.write(_a(Pe,Q,!1)+`
|
|
952
|
+
`)):gc&&(Ne=!0,ls.skipSpinner=!0),a&&a._paused&&a.resume();let{results:$c,summaries:Am}=await o_(Pe,!0,{...ls,skipSummaries:!0});if(Hr&&(Hr=null,process.stdout.write(`\r\x1B[2K${_a(Pe,Q,!1)}
|
|
953
|
+
`)),!ls.skipSummaries){let S=Am.filter((j,z)=>!(Pe[z]&&Pe[z].fnName==="ask_user"));for(let j of S)console.log(j);console.log("");let v=Pe.filter(j=>j&&j.fnName!=="ask_user").map(j=>j.fnName),J=ne.record(0,v,U,M);J&&Db(J)}for(let S of Pe){if(S.canExecute||!S.errorResult)continue;let v=typeof S.errorResult.content=="string"?S.errorResult.content:"";if((v.startsWith("BLOCKED:")||v.startsWith("PLAN MODE:"))&&(ln++,ln>=dc)){re(`${$.red} \u2716 Loop abort: ${ln} consecutive blocked calls (pre-execution) \u2014 model not heeding BLOCKED messages${$.reset}`),a&&(a.stop(),a=null),i(null),We(Q,E,M,U,G,{suppressHint:!0}),Ue(V);return}}for(let S=0;S<Pe.length;S++){let v=Pe[S];if(!v.canExecute)continue;let J=$c[S].content,j=J.split(`
|
|
954
|
+
`)[0],z=!j.startsWith("ERROR")&&!j.startsWith("CANCELLED")&&!j.startsWith("Command failed")&&!j.startsWith("EXIT");if(!z&&(v.fnName==="edit_file"||v.fnName==="patch_file")&&v.args?.path&&j.includes("old_text not found")&&ns.set(v.args.path,!0),z&&v.fnName==="write_file"&&v.args?.path){let D=v.args.path.split("/").pop(),B=v.args.path.includes("/tests/")||v.args.path.includes("\\tests\\");if(/^(test_|demo_|temp_|tmp_|scratch_)/.test(D)&&!B){re(`${$.yellow} \u26A0 Temp file: "${D}" \u2014 delete with bash rm when done to keep the workspace clean${$.reset}`);let de={role:"user",content:`[HINT] "${v.args.path}" looks like a temporary test/demo file. Delete it with bash("rm ${v.args.path}") as soon as you're done \u2014 orphaned temp files count against session quality.`};V.push(de),b.push(de)}}if(z&&["write_file","edit_file","patch_file"].includes(v.fnName)&&v.args&&v.args.path){ns.delete(v.args.path),M.add(v.args.path);let D=(I.get(v.args.path)||0)+1;I.set(v.args.path,D);let B=v.args.path.split("/").slice(-2).join("/");if(D===W){re(`${$.yellow} \u26A0 Loop warning: "${B}" edited ${D}\xD7 \u2014 possible edit loop${$.reset}`);let ee={role:"user",content:`[SYSTEM WARNING] "${v.args.path}" edited ${D}\xD7. One more edit max, then move on.`};V.push(ee),b.push(ee)}else if(D>=se){re(`${$.red} \u2716 Loop abort: "${B}" edited ${D}\xD7 \u2014 aborting to prevent runaway loop${$.reset}`),a&&(a.stop(),a=null),i(null),We(Q,E,M,U,G,{suppressHint:!0}),Ue(V);return}}if((v.fnName==="bash"||v.fnName==="ssh_exec")&&v.args&&v.args.command){let D=v.args.command.replace(/\d+/g,"N").replace(/\s+/g," ").trim().slice(0,100),B=(X.get(D)||0)+1;if(X.set(D,B),B===Z){re(`${$.yellow} \u26A0 Loop warning: same bash command run ${B}\xD7 \u2014 possible debug loop${$.reset}`);let ee={role:"user",content:`[SYSTEM WARNING] Same bash command ${B}\xD7. Debug loop detected \u2014 try a different approach.`};V.push(ee),b.push(ee)}else if(B>=he){re(`${$.red} \u2716 Loop abort: same bash command run ${B}\xD7 \u2014 aborting runaway debug loop${$.reset}`),a&&(a.stop(),a=null),i(null),We(Q,E,M,U,G,{suppressHint:!0}),Ue(V);return}}if(v.fnName==="ssh_exec"){if(Nt++,Nt>=Rm){re(`${$.red} \u2716 SSH storm abort: ${Nt} consecutive ssh_exec calls \u2014 aborting${$.reset}`),a&&(a.stop(),a=null),i(null),We(Q,E,M,U,G,{suppressHint:!0}),Ue(V);return}else if(Nt===Lr){{let B=Je(),{messages:ee}=jt(b,B);b=ee}os=!0,re(`${$.yellow} \u26A0 SSH storm warning: ${Nt} consecutive ssh_exec calls \u2014 blocking further SSH${$.reset}`);let D={role:"user",content:`[SYSTEM WARNING] ${Nt} consecutive SSH calls. Synthesize findings now \u2014 no more SSH.`};V.push(D),b.push(D)}}else v.canExecute&&(Nt=0);if(z&&v.fnName==="grep"&&v.args&&v.args.pattern){let D=`${v.args.pattern}|${v.args.path||""}`,B=(pe.get(D)||0)+1;if(pe.set(D,B),B===we){re(`${$.yellow} \u26A0 Loop warning: grep pattern "${v.args.pattern.slice(0,40)}" run ${B}\xD7 \u2014 possible search loop${$.reset}`);let ee={role:"user",content:`[SYSTEM WARNING] Same grep pattern ${B}\xD7. Results unchanged \u2014 use existing data or try different pattern.`};V.push(ee),b.push(ee)}else if(B>=be){re(`${$.red} \u2716 Loop abort: grep pattern run ${B}\xD7 \u2014 aborting runaway search loop${$.reset}`),a&&(a.stop(),a=null),i(null),We(Q,E,M,U,G,{suppressHint:!0}),Ue(V);return}if(v.args.path){let ee=(je.get(v.args.path)||0)+1;if(je.set(v.args.path,ee),ee===Ge){let de=v.args.path.split("/").slice(-2).join("/");re(`${$.yellow} \u26A0 Loop warning: "${de}" grepped ${ee}\xD7 with different patterns \u2014 context flood risk${$.reset}`);let ge={role:"user",content:`[SYSTEM WARNING] "${v.args.path}" grepped ${ee}\xD7 \u2014 file already in context. Use existing data, stop searching.`};V.push(ge),b.push(ge)}}}if(z&&(v.fnName==="bash"||v.fnName==="ssh_exec")&&J.includes('"valid":true')){{let B=Je();if(Ot(b,B).percentage>=60){let{messages:de,tokensRemoved:ge}=jt(b,B);ge>0&&(b=de,console.log(`${$.dim} [pre-stop-compress \u2014 ~${ge} tokens freed before STOP injection, now ${Math.round(Ot(b,B).percentage)}%]${$.reset}`))}}let D={role:"user",content:'[SYSTEM STOP] Tool result contains {"valid":true}. The token/service is valid and reachable. STOP all further investigation immediately. Report to the user that the token is valid, the service is healthy, and no fix is needed. Do NOT read any more log files.'};V.push(D),b.push(D),console.log(`${$.cyan} \u2713 Health-check stop signal detected \u2014 injecting STOP instruction${$.reset}`)}if(J.startsWith("BLOCKED:")){if(ln++,ln>=dc){re(`${$.red} \u2716 Loop abort: ${ln} consecutive blocked calls \u2014 model not heeding BLOCKED messages${$.reset}`),a&&(a.stop(),a=null),i(null),We(Q,E,M,U,G,{suppressHint:!0}),Ue(V);return}}else ln=0;if(z)cn=0,jr=!0;else if(cn++,cn===vm){re(`${$.yellow} \u26A0 Loop warning: ${cn} consecutive tool errors \u2014 possible stuck loop${$.reset}`);let D={role:"user",content:`[SYSTEM WARNING] ${cn} consecutive errors. Stuck loop \u2014 try fundamentally different approach or declare done.`};V.push(D),b.push(D)}else if(cn>=Sm){re(`${$.red} \u2716 Loop abort: ${cn} consecutive errors \u2014 aborting stuck loop${$.reset}`),a&&(a.stop(),a=null),i(null),We(Q,E,M,U,G,{suppressHint:!0}),Ue(V);return}if(z&&v.fnName==="read_file"&&v.args&&v.args.path){U.add(v.args.path);let D=(On.get(v.args.path)||0)+1;if(On.set(v.args.path,D),v.args.line_start!=null){let de=v.args.line_start||1,ge=v.args.line_end||de+350;Bs.has(v.args.path)||Bs.set(v.args.path,[]),Bs.get(v.args.path).push([de,ge])}if(v._scrollWarn){let{sectionCount:de,path:ge}=v._scrollWarn,Ie={role:"user",content:`[SYSTEM WARNING] "${ge}" \u2014 you have now read ${de} different sections of this file. This is a file-scroll pattern. Stop reading sections and use grep_search to find the specific lines you need instead.`};V.push(Ie),b.push(Ie),re(`${$.yellow} \u26A0 Scroll warning: "${ge.split("/").slice(-2).join("/")}" \u2014 ${de} sections read \u2014 use grep instead${$.reset}`)}let B=v.args.path.split("/").slice(-2).join("/"),ee=!v.args?.line_start&&!v.args?.line_end;if(ee&&D===Pr){{let ge=Je();if(Ot(b,ge).percentage>=60){let{messages:fe}=jt(b,ge);b=fe}}re(`${$.yellow} \u26A0 Loop warning: "${B}" read unbounded ${D}\xD7 \u2014 use line_start/line_end${$.reset}`);let de={role:"user",content:`[SYSTEM WARNING] "${v.args.path}" read ${D}\xD7 without line ranges. Use line_start/line_end to read specific sections \u2014 do not re-read the full file.`};V.push(de),b.push(de)}else if(ee&&D>=km){re(`${$.red} \u2716 Loop abort: "${B}" read unbounded ${D}\xD7 \u2014 aborting runaway read loop${$.reset}`),a&&(a.stop(),a=null),i(null),We(Q,E,M,U,G,{suppressHint:!0}),Ue(V);return}}if(v.fnName==="spawn_agents"){let D=(J.match(/\bStatus: done\b/g)||[]).length;if((J.match(/\bStatus: truncated\b/g)||[]).length>0&&D===0){if(un++,un===Em){re(`${$.yellow} \u26A0 Swarm warning: all sub-agents hit iteration limit ${un}\xD7 in a row${$.reset}`);let ee={role:"user",content:`[SYSTEM WARNING] Sub-agents truncated ${un}\xD7 in a row. Stop spawning \u2014 try different approach or report findings.`};V.push(ee),b.push(ee)}else if(un>=Tm){console.log(`${$.red} \u2716 Swarm abort: all sub-agents hit iteration limit ${un}\xD7 \u2014 aborting stuck swarm${$.reset}`),a&&(a.stop(),a=null),i(null),We(Q,E,M,U,G,{suppressHint:!0}),Ue(V);return}}else D>0&&(un=0)}}for(let S of $c)V.push(S),b.push(S);{let S=Je();if(Ot(b,S).percentage>=78){let{messages:J,tokensRemoved:j}=jt(b,S);j>0&&(b=J,console.log(`${$.dim} [auto-compressed \u2014 ~${j} tokens freed, now ${Math.round(Ot(b,S).percentage)}%]${$.reset}`))}}let yc=i_();if(yc){let S={role:"user",content:`[User note mid-run]: ${yc}`};V.push(S),b.push(S),console.log(`${$.cyan} \u270E Context added${$.reset}`)}}if(dt>=oo){a&&(a.stop(),a=null),i(null),We(Q,E,M,U,G),Ue(V),jf(V);let{getActiveProviderName:qe}=Te();if(qe()==="ollama"&&Dr<fc){if(M.size===0&&!jr){console.log(`${$.yellow} \u26A0 Max iterations reached with no progress. Stopping.${$.reset}`);break e}Dr++,oo=20,console.log(`${$.dim} \u2500\u2500 auto-extending (+20 turns, ext ${Dr}/${fc}) \u2500\u2500${$.reset}`);continue e}if(console.log(`
|
|
955
|
+
${$.yellow}\u26A0 Max iterations reached.${$.reset}`),await Xf(" Continue for 20 more turns?")){oo=20;continue e}console.log(`${$.dim} Tip: set "maxIterations" in .nex/config.json or use --max-turns${$.reset}`)}break e}}op.exports={processInput:$_,clearConversation:l_,getConversationLength:d_,getConversationMessages:f_,setConversationMessages:p_,setAbortSignalGetter:Wb,setMaxIterations:Ub,invalidateSystemPromptCache:Oa,clearToolFilterCache:Jb,getCachedFilteredTools:Qf,buildSystemPrompt:sp,getProjectContextHash:Zf,buildUserContent:Vf,detectAndTruncateLoop:Ra,injectMidRunNote:r_}});var Te=H((Mv,lp)=>{var{OllamaProvider:y_}=Bc(),{OpenAIProvider:w_}=cl(),{AnthropicProvider:b_}=fl(),{GeminiProvider:__}=gl(),{LocalProvider:x_}=wl(),{checkBudget:k_}=Fn(),Da={top:{ollama:"kimi-k2:1t",openai:"gpt-4.1",anthropic:"claude-sonnet-4-5",gemini:"gemini-2.5-pro"},strong:{ollama:"qwen3-coder:480b",openai:"gpt-4o",anthropic:"claude-sonnet",gemini:"gemini-2.5-flash"},fast:{ollama:"devstral-small-2:24b",openai:"gpt-4.1-mini",anthropic:"claude-haiku",gemini:"gemini-2.0-flash"}},rp={};for(let[t,e]of Object.entries(Da))for(let n of Object.values(e))rp[n]=t;function ip(t,e){let n=rp[t];return n&&Da[n][e]||t}var Ye={},ut=null,Be=null,Ks=[];function vt(){if(Object.keys(Ye).length>0)return;rs("ollama",new y_),rs("openai",new w_),rs("anthropic",new b_),rs("gemini",new __),rs("local",new x_);let t=process.env.DEFAULT_PROVIDER||"ollama",e=process.env.DEFAULT_MODEL||null;Ye[t]?(ut=t,Be=e||Ye[t].defaultModel):(ut="ollama",Be="kimi-k2.5");let n=process.env.FALLBACK_CHAIN;n&&(Ks=n.split(",").map(o=>o.trim()).filter(Boolean))}function rs(t,e){Ye[t]=e}function v_(t){return vt(),Ye[t]||null}function ja(){return vt(),Ye[ut]||null}function S_(){return vt(),ut}function E_(){return vt(),Be}function T_(){vt();let t=ja();if(!t)return{id:Be,name:Be,provider:ut};let e=t.getModel(Be);return e?{...e,provider:ut}:{id:Be,name:Be,provider:ut}}function ap(t){if(!t)return{provider:null,model:null};let e=t.indexOf(":");if(e>0){let n=t.slice(0,e);if(Ye[n]||["ollama","openai","anthropic","gemini","local"].includes(n))return{provider:n,model:t.slice(e+1)}}return{provider:null,model:t}}function R_(t){vt();let{provider:e,model:n}=ap(t);if(e){let s=Ye[e];return s&&(s.getModel(n)||e==="local"||e==="ollama")?(ut=e,Be=n,wr(),!0):!1}let o=ja();if(o&&o.getModel(n))return Be=n,wr(),!0;for(let[s,r]of Object.entries(Ye))if(r.getModel(n))return ut=s,Be=n,wr(),!0;return!1}function wr(){try{let{invalidateSystemPromptCache:t,clearToolFilterCache:e}=Se();t(),e()}catch{}try{let{invalidateTokenRatioCache:t}=Qe();t()}catch{}}function C_(){vt();let t=new Set;for(let e of Object.values(Ye))for(let n of e.getModelNames())t.add(n);return Array.from(t)}function A_(){return vt(),Object.entries(Ye).map(([t,e])=>({provider:t,configured:e.isConfigured(),models:Object.values(e.getModels()).map(n=>({...n,active:t===ut&&n.id===Be}))}))}function O_(){vt();let t=[];for(let[e,n]of Object.entries(Ye)){let o=n.isConfigured();for(let s of Object.values(n.getModels()))t.push({spec:`${e}:${s.id}`,name:s.name,provider:e,configured:o})}return t}function N_(t){Ks=Array.isArray(t)?t:[]}function M_(){return[...Ks]}function P_(t){let e=t.message||"",n=t.code||"";return!!(e.includes("429")||e.includes("500")||e.includes("502")||e.includes("503")||e.includes("504")||n==="ECONNABORTED"||n==="ETIMEDOUT"||n==="ECONNREFUSED"||n==="ECONNRESET"||n==="EHOSTUNREACH"||n==="ENETUNREACH"||n==="EPIPE"||n==="ERR_SOCKET_CONNECTION_TIMEOUT"||e.includes("socket disconnected")||e.includes("TLS")||e.includes("ECONNRESET")||e.includes("ECONNABORTED")||e.includes("network")||e.includes("ETIMEDOUT"))}async function cp(t){let e=[ut,...Ks.filter(r=>r!==ut)],n,o=0,s=0;for(let r=0;r<e.length;r++){let i=e[r],a=Ye[i];if(!a||!a.isConfigured())continue;s++;let l=k_(i);if(!l.allowed){o++,n=new Error(`Budget limit reached for ${i}: $${l.spent.toFixed(2)} / $${l.limit.toFixed(2)}`);continue}try{let u=r>0,d=u?ip(Be,i):Be;return u&&d!==Be&&process.stderr.write(` [fallback: ${i}:${d}]
|
|
955
956
|
`),await t(a,i,d)}catch(u){if(n=u,P_(u)&&r<e.length-1)continue;throw u}}throw o>0&&o===s?new Error("All providers are over budget. Use /budget to check limits or /budget <provider> off to remove a limit."):s===0?new Error("No configured provider available"):n||new Error("No configured provider available")}async function I_(t,e,n={}){return vt(),cp((o,s,r)=>o.stream(t,e,{model:r,signal:n.signal,...n}))}async function L_(t,e,n={}){if(vt(),n.provider){let o=Ye[n.provider];if(!o||!o.isConfigured())throw new Error(`Provider '${n.provider}' is not available`);let s={model:n.model||Be,...n};try{return await o.chat(t,e,s)}catch(r){if(typeof o.stream=="function")try{return await o.stream(t,e,{...s,onToken:()=>{}})}catch{}throw r}}return cp(async(o,s,r)=>{try{return await o.chat(t,e,{model:r,...n})}catch(i){if(typeof o.stream=="function")try{return await o.stream(t,e,{model:r,...n,onToken:()=>{}})}catch{}throw i}})}function D_(){vt();let t=[];for(let[e,n]of Object.entries(Ye))n.isConfigured()&&t.push({name:e,models:Object.values(n.getModels())});return t}function j_(){for(let t of Object.keys(Ye))delete Ye[t];ut=null,Be=null,Ks=[]}lp.exports={registerProvider:rs,getProvider:v_,getActiveProvider:ja,getActiveProviderName:S_,getActiveModelId:E_,getActiveModel:T_,setActiveModel:R_,getModelNames:C_,parseModelSpec:ap,listProviders:A_,listAllModels:O_,callStream:I_,callChat:L_,getConfiguredProviders:D_,setFallbackChain:N_,getFallbackChain:M_,resolveModelForProvider:ip,MODEL_EQUIVALENTS:Da,_reset:j_}});var Fa=H((Pv,up)=>{"use strict";var Yt=require("fs"),br=require("path"),q_=require("readline"),$e="\x1B[0m",_r="\x1B[1m",Ve="\x1B[2m",xr="\x1B[33m",qa="\x1B[36m",Ys="\x1B[32m";function F_(t){return(e,n="")=>new Promise(o=>{let s=n?` ${Ve}[${n}]${$e}`:"";t.question(` ${qa}${e}${s}${$e}: `,r=>o(r.trim()||n))})}function zs(t,e){return new Promise(n=>{e&&e.pause(),process.stdout.write(` ${qa}${t}${$e}: `);let o=process.stdin,s=o.isRaw,r="";o.setRawMode(!0),o.resume(),o.setEncoding("utf8");let i=a=>{a==="\r"||a===`
|
|
956
957
|
`?(o.setRawMode(s||!1),o.removeListener("data",i),process.stdout.write(`
|
|
957
958
|
`),e&&e.resume(),n(r)):a===""?(process.stdout.write(`
|
|
@@ -975,7 +976,7 @@ ${xr} No key entered \u2014 cancelled.${$e}
|
|
|
975
976
|
`).some(g=>g.trim()===".env")||Yt.appendFileSync(h,`
|
|
976
977
|
.env
|
|
977
978
|
`)),console.log(`
|
|
978
|
-
${Ys} \u2713 Saved to ${u}${$e}`),a.some(p=>p.includes("API_KEY"))&&console.log(`${Ve} (key stored locally \u2014 never committed)${$e}`)}if(process.env.DEFAULT_PROVIDER)try{let{setActiveModel:m}=
|
|
979
|
+
${Ys} \u2713 Saved to ${u}${$e}`),a.some(p=>p.includes("API_KEY"))&&console.log(`${Ve} (key stored locally \u2014 never committed)${$e}`)}if(process.env.DEFAULT_PROVIDER)try{let{setActiveModel:m}=Te(),h=process.env.DEFAULT_MODEL?`${process.env.DEFAULT_PROVIDER}:${process.env.DEFAULT_MODEL}`:process.env.DEFAULT_PROVIDER;m(h),console.log(`${Ys} \u2713 Switched to ${h} for this session${$e}`)}catch{}console.log(`
|
|
979
980
|
${Ys} \u2713 Setup complete!${$e}
|
|
980
981
|
`)}up.exports={runSetupWizard:U_}});var fp=H((Iv,dp)=>{"use strict";var W_=require("readline");function Rn(t){process.stdout.write(JSON.stringify(t)+`
|
|
981
982
|
`)}function B_(){process.env.NEX_SERVER="1";let t=(...a)=>process.stderr.write(a.map(String).join(" ")+`
|
|
@@ -985,10 +986,10 @@ ${Ys} \u2713 Setup complete!${$e}
|
|
|
985
986
|
`)}function mp(t){return t.replace(/\x1b\[[^a-zA-Z]*[a-zA-Z]/g,"").length}var Ua=class{constructor(){this._active=!1,this._rl=null,this._origWrite=null,this._origPrompt=null,this._origSetPr=null,this._origRefreshLine=null,this._origLog=null,this._origError=null,this._origStderrWrite=null,this._drawing=!1,this._offResize=null,this._cursorOnInputRow=!1,this._inRefreshLine=!1,this._lastOutputRow=1,this._prevTermRows=0,this._prevTermCols=0,this._consistencyTimer=null,this._dirty=!1,this._statusModel="",this._statusBranch="",this._statusProject="",this._statusMode=""}setStatusInfo({model:e,branch:n,project:o,mode:s}={}){e!==void 0&&(this._statusModel=e),n!==void 0&&(this._statusBranch=n),o!==void 0&&(this._statusProject=o),s!==void 0&&(this._statusMode=s),this._active&&this.drawFooter()}get _rows(){return process.stdout.rows||24}get _cols(){return process.stdout.columns||80}get _scrollEnd(){return this._rows-2}get _rowStatus(){return this._rows-1}get _rowInput(){return this._rows}_goto(e,n=1){return`\x1B[${e};${n}H`}_statusLine(){let e=this._cols,n=this._statusModel,o=this._statusBranch,s=this._statusProject,r=this._statusMode;if(!n)return Mt.footer_sep+"\u2500".repeat(e)+$t;let i=` ${Mt.footer_divider}\xB7${$t} `,a=[];n&&a.push(`${Mt.footer_model}${n}${$t}`),o&&a.push(`${Mt.footer_branch}${o}${$t}`),s&&a.push(`${Mt.footer_project}${s}${$t}`);let l=a.join(i),u=[n,o,s].filter(Boolean).join(" \xB7 ").length,d="\u2500 ";if(r){let h=r.length,p=Math.max(0,e-d.length-u-1-1-h-3),g="\u2500".repeat(p);return`${Mt.footer_sep}${d}${$t}${l}${Mt.footer_sep} ${g} ${$t}${Mt.footer_mode}${r}${$t}${Mt.footer_sep} \u2500\u2500${$t}`}let f=Math.max(0,e-d.length-u-2),m="\u2500".repeat(f);return`${Mt.footer_sep}${d}${$t}${l}${Mt.footer_sep} ${m}${$t}`}drawFooter(e){!this._origWrite||this._drawing||(this._drawing=!0,this._origWrite("\x1B7"+this._goto(this._rowStatus)+"\x1B[2K"+this._statusLine(e)+"\x1B8"),this._drawing=!1)}_setScrollRegion(){if(G_)return;let e=Math.max(1,this._scrollEnd);this._origWrite(`\x1B[1;${e}r`)}_clearScrollRegion(){this._origWrite&&this._origWrite("\x1B[r")}_eraseStatus(){this._origWrite&&this._origWrite("\x1B7"+this._goto(this._rowStatus)+"\x1B[2K\x1B8")}rawWrite(e){return pp("RAW",e),this._origWrite?this._origWrite(e):process.stdout.write(e)}_relayout(e){if(!this._origWrite)return;let n=this._origWrite,o=this._rows,s=this._cols,r=Math.max(1,o-2);an("RELAYOUT:",e,"rows="+o,"cols="+s,"scrollEnd="+r,"cursorOnInput="+this._cursorOnInputRow),this._prevTermRows=o,this._prevTermCols=s;let i=Math.min(this._lastOutputRow+1,r+1),a="";for(let l=i;l<=o;l++)a+=this._goto(l)+"\x1B[2K";n(a),this._setScrollRegion(),this._lastOutputRow=Math.min(this._lastOutputRow,r),this.drawFooter(),this._cursorOnInputRow&&this._rl&&this._rl.prompt(!0),this._dirty=!1}activate(e){if(!process.stdout.isTTY)return;this._rl=e,this._origWrite=process.stdout.write.bind(process.stdout),this._active=!0,this._prevTermRows=this._rows,this._prevTermCols=this._cols,this._origWrite("\x1B[r"),this._setScrollRegion(),this._lastOutputRow=1,this.drawFooter();let n=this,o=process.stdout.write.bind(process.stdout);this._origWrite=o,process.stdout.write=function(p,...g){if(pp("PATCH",p),!n._active||typeof p!="string")return o(p,...g);if(n._inRefreshLine){let x=p.replace(/\n/g,"");return x?o(x,...g):!0}if(n._cursorOnInputRow)if(p.includes(`
|
|
986
987
|
`)&&p.length>4&&!p.includes("\r"))an("STDOUT: agent output, leaving input row, data="+JSON.stringify(p).slice(0,100)),n._cursorOnInputRow=!1,o(n._goto(Math.min(n._lastOutputRow+1,n._scrollEnd)));else{if(p.length<=4&&!p.includes(`
|
|
987
988
|
`)&&!p.includes("\r")&&!/[\x00-\x1f\x7f]/.test(p))return an("STDOUT: char intercept:",JSON.stringify(p)),n._origRefreshLine&&n._doRefreshLine(),!0;{let x=p.replace(/\n/g,"");return x?o(x,...g):!0}}let y=n._cols||80,w=0,k=p.split(`
|
|
988
|
-
`);for(let x=0;x<k.length;x++){let _=mp(k[x]);_>0&&(w+=Math.floor((_-1)/y))}let R=k.length-1;return R+w>0&&(n._lastOutputRow=Math.min(n._lastOutputRow+R+w,n._scrollEnd)),o(p,...g)},this._cursorOnInputRow=!1,this._origStderrWrite=process.stderr.write.bind(process.stderr),process.stderr.write=function(p,...g){if(!n._active)return n._origStderrWrite(p,...g);if(typeof p=="string"&&p.includes("\r")){let y=Math.min(n._lastOutputRow+1,n._scrollEnd);if(n._cursorOnInputRow){n._origWrite("\x1B7"),n._origWrite(n._goto(y));let w=n._origStderrWrite(p,...g);return n._origWrite("\x1B8"),w}n._origWrite(n._goto(y))}return n._origStderrWrite(p,...g)},this._origLog=console.log,this._origError=console.error;function s(...p){if(!n._active){n._origLog(...p);return}n._origWrite(n._goto(Math.min(n._lastOutputRow+1,n._scrollEnd))),n._cursorOnInputRow=!1,n._origLog(...p),n.drawFooter(),o(n._goto(n._rowInput)+"\x1B[2K"),n._cursorOnInputRow=!0}function r(...p){if(!n._active){n._origError(...p);return}n._origWrite(n._goto(Math.min(n._lastOutputRow+1,n._scrollEnd))),n._cursorOnInputRow=!1,n._origError(...p),n.drawFooter(),o(n._goto(n._rowInput)+"\x1B[2K"),n._cursorOnInputRow=!0}console.log=s,console.error=r,this._origSetPr=e.setPrompt.bind(e),e.setPrompt=function(p){n._origSetPr(p),n._active&&n.drawFooter(p)},this._origPrompt=e.prompt.bind(e),e.prompt=function(p){if(!n._active)return n._origPrompt(p);an("PROMPT: goto rowInput="+n._rowInput),e.prevRows=0,o(n._goto(n._rowInput)+$t+"\x1B[2K"),n._cursorOnInputRow=!0,n._origPrompt(p)};let i=e.question.bind(e);e.question=function(p,g){if(!n._active)return i(p,g);o(n._goto(n._rowInput)+"\x1B[2K"),e.prevRows=0,n._cursorOnInputRow=!0,i(p,y=>{o(n._goto(n._rowInput)+"\x1B[2K"),n._cursorOnInputRow=!1,n.drawFooter(),g(y)})};let a=Object.getPrototypeOf(e),l=Object.getOwnPropertySymbols(a).find(p=>p.toString()==="Symbol(_refreshLine)"),u=l?a[l].bind(e):e._refreshLine?e._refreshLine.bind(e):null;if(this._origRefreshLine=u,this._doRefreshLine=function(){if(!n._active||!u)return;if(n._rows!==n._prevTermRows||n._cols!==n._prevTermCols){n._dirty=!0;return}e.prevRows=0,o(n._goto(n._rowInput)+$t+"\x1B[2K");let p=n._cols,g=e._prompt||"",y=mp(g),w=p-y-1;n._inRefreshLine=!0;try{if(e.line.length<=w){an("REFRESH: short line, len="+e.line.length),u();return}an("REFRESH: long line, len="+e.line.length+", max="+w);let k=e.line,R=e.cursor,x=Math.max(1,w-1),_=Math.max(0,R-x),b=(_>0?"\xAB":"")+k.slice(_,_+x+(_>0?0:1));e.line=b,e.cursor=b.length,u(),e.line=k,e.cursor=R}finally{n._inRefreshLine=!1}},u){let p=this._doRefreshLine;l&&Object.defineProperty(e,l,{value:p,writable:!0,configurable:!0}),e._refreshLine=p}e.on("line",()=>{n._active&&(an("LINE: leaving input row"),n._cursorOnInputRow=!1,o(n._goto(n._rowInput)+"\x1B[2K"),o(n._goto(Math.min(n._lastOutputRow+1,n._scrollEnd))),n.drawFooter())});let d=null,f=null,m=()=>{d=null,n._relayout("resize"),f&&clearTimeout(f),f=setTimeout(()=>{f=null,n._relayout("resize-cleanup")},300)},h=()=>{n._dirty=!0,d&&clearTimeout(d),d=setTimeout(m,80)};process.stdout.on("resize",h),this._offResize=()=>{process.stdout.off("resize",h),d&&(clearTimeout(d),d=null),f&&(clearTimeout(f),f=null)},this._consistencyTimer=setInterval(()=>{if(!n._active)return;let p=n._rows,g=n._cols;(n._dirty||p!==n._prevTermRows||g!==n._prevTermCols)&&(an("CONSISTENCY: dirty="+n._dirty+" rows="+p+"/"+n._prevTermRows+" cols="+g+"/"+n._prevTermCols),n._relayout("consistency"))},800)}deactivate(){if(this._active){if(this._active=!1,this._offResize&&(this._offResize(),this._offResize=null),this._consistencyTimer&&(clearInterval(this._consistencyTimer),this._consistencyTimer=null),this._origStderrWrite&&(process.stderr.write=this._origStderrWrite,this._origStderrWrite=null),this._origLog&&(console.log=this._origLog,this._origLog=null),this._origError&&(console.error=this._origError,this._origError=null),this._origWrite&&(process.stdout.write=this._origWrite),this._rl&&(this._origPrompt&&(this._rl.prompt=this._origPrompt,this._origPrompt=null),this._origSetPr&&(this._rl.setPrompt=this._origSetPr,this._origSetPr=null),this._origRefreshLine)){let e=Object.getOwnPropertySymbols(Object.getPrototypeOf(this._rl)).find(n=>n.toString()==="Symbol(_refreshLine)");e&&delete this._rl[e],delete this._rl._refreshLine,this._origRefreshLine=null}this._eraseStatus(),this._clearScrollRegion(),this._origWrite=null}}};hp.exports={StickyFooter:Ua}});var wp=H((Dv,yp)=>{var{C:
|
|
989
|
-
`);process.stdout.write(`\x1B[${m}A`)}let w=[];w.push(` ${
|
|
989
|
+
`);for(let x=0;x<k.length;x++){let _=mp(k[x]);_>0&&(w+=Math.floor((_-1)/y))}let R=k.length-1;return R+w>0&&(n._lastOutputRow=Math.min(n._lastOutputRow+R+w,n._scrollEnd)),o(p,...g)},this._cursorOnInputRow=!1,this._origStderrWrite=process.stderr.write.bind(process.stderr),process.stderr.write=function(p,...g){if(!n._active)return n._origStderrWrite(p,...g);if(typeof p=="string"&&p.includes("\r")){let y=Math.min(n._lastOutputRow+1,n._scrollEnd);if(n._cursorOnInputRow){n._origWrite("\x1B7"),n._origWrite(n._goto(y));let w=n._origStderrWrite(p,...g);return n._origWrite("\x1B8"),w}n._origWrite(n._goto(y))}return n._origStderrWrite(p,...g)},this._origLog=console.log,this._origError=console.error;function s(...p){if(!n._active){n._origLog(...p);return}n._origWrite(n._goto(Math.min(n._lastOutputRow+1,n._scrollEnd))),n._cursorOnInputRow=!1,n._origLog(...p),n.drawFooter(),o(n._goto(n._rowInput)+"\x1B[2K"),n._cursorOnInputRow=!0}function r(...p){if(!n._active){n._origError(...p);return}n._origWrite(n._goto(Math.min(n._lastOutputRow+1,n._scrollEnd))),n._cursorOnInputRow=!1,n._origError(...p),n.drawFooter(),o(n._goto(n._rowInput)+"\x1B[2K"),n._cursorOnInputRow=!0}console.log=s,console.error=r,this._origSetPr=e.setPrompt.bind(e),e.setPrompt=function(p){n._origSetPr(p),n._active&&n.drawFooter(p)},this._origPrompt=e.prompt.bind(e),e.prompt=function(p){if(!n._active)return n._origPrompt(p);an("PROMPT: goto rowInput="+n._rowInput),e.prevRows=0,o(n._goto(n._rowInput)+$t+"\x1B[2K"),n._cursorOnInputRow=!0,n._origPrompt(p)};let i=e.question.bind(e);e.question=function(p,g){if(!n._active)return i(p,g);o(n._goto(n._rowInput)+"\x1B[2K"),e.prevRows=0,n._cursorOnInputRow=!0,i(p,y=>{o(n._goto(n._rowInput)+"\x1B[2K"),n._cursorOnInputRow=!1,n.drawFooter(),g(y)})};let a=Object.getPrototypeOf(e),l=Object.getOwnPropertySymbols(a).find(p=>p.toString()==="Symbol(_refreshLine)"),u=l?a[l].bind(e):e._refreshLine?e._refreshLine.bind(e):null;if(this._origRefreshLine=u,this._doRefreshLine=function(){if(!n._active||!u)return;if(n._rows!==n._prevTermRows||n._cols!==n._prevTermCols){n._dirty=!0;return}e.prevRows=0,o(n._goto(n._rowInput)+$t+"\x1B[2K");let p=n._cols,g=e._prompt||"",y=mp(g),w=p-y-1;n._inRefreshLine=!0;try{if(e.line.length<=w){an("REFRESH: short line, len="+e.line.length),u();return}an("REFRESH: long line, len="+e.line.length+", max="+w);let k=e.line,R=e.cursor,x=Math.max(1,w-1),_=Math.max(0,R-x),b=(_>0?"\xAB":"")+k.slice(_,_+x+(_>0?0:1));e.line=b,e.cursor=b.length,u(),e.line=k,e.cursor=R}finally{n._inRefreshLine=!1}},u){let p=this._doRefreshLine;l&&Object.defineProperty(e,l,{value:p,writable:!0,configurable:!0}),e._refreshLine=p}e.on("line",()=>{n._active&&(an("LINE: leaving input row"),n._cursorOnInputRow=!1,o(n._goto(n._rowInput)+"\x1B[2K"),o(n._goto(Math.min(n._lastOutputRow+1,n._scrollEnd))),n.drawFooter())});let d=null,f=null,m=()=>{d=null,n._relayout("resize"),f&&clearTimeout(f),f=setTimeout(()=>{f=null,n._relayout("resize-cleanup")},300)},h=()=>{n._dirty=!0,d&&clearTimeout(d),d=setTimeout(m,80)};process.stdout.on("resize",h),this._offResize=()=>{process.stdout.off("resize",h),d&&(clearTimeout(d),d=null),f&&(clearTimeout(f),f=null)},this._consistencyTimer=setInterval(()=>{if(!n._active)return;let p=n._rows,g=n._cols;(n._dirty||p!==n._prevTermRows||g!==n._prevTermCols)&&(an("CONSISTENCY: dirty="+n._dirty+" rows="+p+"/"+n._prevTermRows+" cols="+g+"/"+n._prevTermCols),n._relayout("consistency"))},800)}deactivate(){if(this._active){if(this._active=!1,this._offResize&&(this._offResize(),this._offResize=null),this._consistencyTimer&&(clearInterval(this._consistencyTimer),this._consistencyTimer=null),this._origStderrWrite&&(process.stderr.write=this._origStderrWrite,this._origStderrWrite=null),this._origLog&&(console.log=this._origLog,this._origLog=null),this._origError&&(console.error=this._origError,this._origError=null),this._origWrite&&(process.stdout.write=this._origWrite),this._rl&&(this._origPrompt&&(this._rl.prompt=this._origPrompt,this._origPrompt=null),this._origSetPr&&(this._rl.setPrompt=this._origSetPr,this._origSetPr=null),this._origRefreshLine)){let e=Object.getOwnPropertySymbols(Object.getPrototypeOf(this._rl)).find(n=>n.toString()==="Symbol(_refreshLine)");e&&delete this._rl[e],delete this._rl._refreshLine,this._origRefreshLine=null}this._eraseStatus(),this._clearScrollRegion(),this._origWrite=null}}};hp.exports={StickyFooter:Ua}});var wp=H((Dv,yp)=>{var{C:Ae}=Oe(),{listProviders:z_,getActiveProviderName:X_,getActiveModelId:J_,setActiveModel:V_}=Te();function $p(t,e,n={}){let{title:o="Select",hint:s="\u2191\u2193 navigate \xB7 Enter select \xB7 Esc cancel"}=n;return new Promise(r=>{let i=e.map((w,k)=>w.isHeader?-1:k).filter(w=>w>=0);if(i.length===0){r(null);return}let a=e.findIndex(w=>w.isCurrent),l=a>=0?i.indexOf(a):0;l<0&&(l=0);let u=process.stdout.rows?Math.max(process.stdout.rows-6,5):20,d=0;function f(){let w=i[l];return w<d?d=w:w>=d+u&&(d=w-u+1),{start:d,end:Math.min(e.length,d+u)}}let m=0;function h(){if(m>0){process.stdout.write(`\x1B[${m}A`);for(let _=0;_<m;_++)process.stdout.write(`\x1B[2K
|
|
990
|
+
`);process.stdout.write(`\x1B[${m}A`)}let w=[];w.push(` ${Ae.bold}${Ae.cyan}${o}${Ae.reset}`),w.push(` ${Ae.dim}${s}${Ae.reset}`),w.push("");let{start:k,end:R}=f();k>0&&w.push(` ${Ae.dim}\u2191 more${Ae.reset}`);for(let _=k;_<R;_++){let b=e[_];if(b.isHeader){w.push(` ${Ae.bold}${Ae.dim}${b.label}${Ae.reset}`);continue}let O=i[l]===_,N=O?`${Ae.cyan}> `:" ",C=b.isCurrent?` ${Ae.yellow}<current>${Ae.reset}`:"";O?w.push(`${N}${Ae.bold}${b.label}${Ae.reset}${C}`):w.push(`${N}${Ae.dim}${b.label}${Ae.reset}${C}`)}R<e.length&&w.push(` ${Ae.dim}\u2193 more${Ae.reset}`);let x=w.join(`
|
|
990
991
|
`);process.stdout.write(x+`
|
|
991
|
-
`),m=w.length}t.pause();let p=process.stdin.isRaw;process.stdin.isTTY&&process.stdin.setRawMode(!0),process.stdin.resume();function g(){process.stdin.removeListener("keypress",y),process.stdin.isTTY&&p!==void 0&&process.stdin.setRawMode(p),t.resume()}function y(w,k){if(k){if(k.name==="up"||k.ctrl&&k.name==="p"){l>0&&(l--,h());return}if(k.name==="down"||k.ctrl&&k.name==="n"){l<i.length-1&&(l++,h());return}if(k.name==="return"){let R=e[i[l]];g(),r(R?R.value:null);return}if(k.name==="escape"||k.ctrl&&k.name==="c"){g(),r(null);return}}}process.stdin.on("keypress",y),h()})}async function Q_(t){let e=z_(),n=X_(),o=J_(),s=[];for(let i of e)if(i.models.length!==0){s.push({label:i.provider,value:null,isHeader:!0});for(let a of i.models){let l=i.provider===n&&a.id===o;s.push({label:` ${a.name} (${i.provider}:${a.id})`,value:`${i.provider}:${a.id}`,isCurrent:l})}}let r=await $p(t,s,{title:"Select Model"});return r?(V_(r),console.log(`${
|
|
992
|
+
`),m=w.length}t.pause();let p=process.stdin.isRaw;process.stdin.isTTY&&process.stdin.setRawMode(!0),process.stdin.resume();function g(){process.stdin.removeListener("keypress",y),process.stdin.isTTY&&p!==void 0&&process.stdin.setRawMode(p),t.resume()}function y(w,k){if(k){if(k.name==="up"||k.ctrl&&k.name==="p"){l>0&&(l--,h());return}if(k.name==="down"||k.ctrl&&k.name==="n"){l<i.length-1&&(l++,h());return}if(k.name==="return"){let R=e[i[l]];g(),r(R?R.value:null);return}if(k.name==="escape"||k.ctrl&&k.name==="c"){g(),r(null);return}}}process.stdin.on("keypress",y),h()})}async function Q_(t){let e=z_(),n=X_(),o=J_(),s=[];for(let i of e)if(i.models.length!==0){s.push({label:i.provider,value:null,isHeader:!0});for(let a of i.models){let l=i.provider===n&&a.id===o;s.push({label:` ${a.name} (${i.provider}:${a.id})`,value:`${i.provider}:${a.id}`,isCurrent:l})}}let r=await $p(t,s,{title:"Select Model"});return r?(V_(r),console.log(`${Ae.green}Switched to ${r}${Ae.reset}`),!0):(console.log(`${Ae.dim}Cancelled${Ae.reset}`),!1)}yp.exports={pickFromList:$p,showModelPicker:Q_}});var Ba=H((qv,Tp)=>{var bp=require("fs"),Z_=require("path"),{atomicWrite:ex,withFileLockSync:tx}=Zt(),{callChat:_p}=Te(),{remember:nx,listMemories:jv,recall:sx}=rn(),Wa=4,ox=`You are a memory optimization agent for an AI coding assistant called nex-code.
|
|
992
993
|
Analyze this conversation history and extract actionable learnings the assistant should remember.
|
|
993
994
|
|
|
994
995
|
Return ONLY valid JSON in this exact format:
|
|
@@ -1062,7 +1063,7 @@ ${n}`}];try{let i=((await _p(o,[],{temperature:0,maxTokens:2e3})).content||"").t
|
|
|
1062
1063
|
|
|
1063
1064
|
## Update ${new Date().toISOString().split("T")[0]}
|
|
1064
1065
|
|
|
1065
|
-
${i.content}`;n(a,l.content+d),s.push({name:a,reason:i.reason||"",action:"updated"})}else n(a,i.content),s.push({name:a,reason:i.reason||"",action:"created"})}return{written:s,skipped:r}}Tp.exports={learnFromSession:rx,learnBrainFromSession:ax,reflectOnSession:kp,reflectBrain:Ep,applyMemories:vp,applyNexAdditions:Sp,LEARN_MIN_MESSAGES:Wa}});var Op=H((Uv,Ap)=>{var tt=require("fs"),cs=require("path"),Fv=require("os"),cx=require("readline"),{C:Y}=
|
|
1066
|
+
${i.content}`;n(a,l.content+d),s.push({name:a,reason:i.reason||"",action:"updated"})}else n(a,i.content),s.push({name:a,reason:i.reason||"",action:"created"})}return{written:s,skipped:r}}Tp.exports={learnFromSession:rx,learnBrainFromSession:ax,reflectOnSession:kp,reflectBrain:Ep,applyMemories:vp,applyNexAdditions:Sp,LEARN_MIN_MESSAGES:Wa}});var Op=H((Uv,Ap)=>{var tt=require("fs"),cs=require("path"),Fv=require("os"),cx=require("readline"),{C:Y}=Oe(),Rp=".nex",Ha=null;function lx(t){Ha=t}function nt(t,e=""){let n=e?` ${Y.dim}[${e}]${Y.reset}`:"",o=` ${Y.cyan}${t}${n}${Y.reset}: `;return new Promise(s=>{let r=i=>{let a=i.trim()||e;s(a)};if(Ha)Ha.question(o,r);else{let i=cx.createInterface({input:process.stdin,output:process.stdout});i.question(o,a=>{i.close(),r(a)})}})}function as(t,e=!0){return nt(`${t} (${e?"Y/n":"y/N"})`,e?"y":"n").then(o=>o.toLowerCase()!=="n"&&o.toLowerCase()!=="no")}async function Ga(t,e,n){let o=e.map((a,l)=>`${Y.dim}${l+1})${Y.reset} ${a}`).join(" ");console.log(` ${Y.cyan}${t}${Y.reset}`),console.log(` ${o}`);let s=n?e.indexOf(n)+1:1,r=await nt("Enter number",String(s)),i=parseInt(r,10)-1;return e[Math.max(0,Math.min(i,e.length-1))]}async function ux(){let t=cs.join(process.cwd(),Rp),e=cs.join(t,"servers.json"),n={};if(tt.existsSync(e))try{n=JSON.parse(tt.readFileSync(e,"utf-8"))}catch{}console.log(`
|
|
1066
1067
|
${Y.bold}${Y.cyan}\u2554\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2557${Y.reset}`),console.log(`${Y.bold}${Y.cyan}\u2551 nex-code Server Setup Wizard \u2551${Y.reset}`),console.log(`${Y.bold}${Y.cyan}\u255A\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u255D${Y.reset}
|
|
1067
1068
|
`);let o=Object.keys(n);if(o.length>0&&(console.log(`${Y.dim}Existing profiles: ${o.join(", ")}${Y.reset}`),!await as("Add or update a server profile?",!0))){console.log(`${Y.dim}No changes made.${Y.reset}
|
|
1068
1069
|
`);return}let s={...n},r=!0;for(;r;){console.log(`
|
|
@@ -1103,7 +1104,7 @@ ${u}
|
|
|
1103
1104
|
${f}
|
|
1104
1105
|
`;ze.writeFileSync(n,a),i=!0}return{saved:!0,envUpdated:i,changes:o}}var Rx=/thinking|reasoning|instruct|planner|orchestrat/i;function Cx(t){return Rx.test(t)}function Ax(t){if(!t||t.length===0)return{updated:!1};let e=Vs.join(Tr.homedir(),".nex-code","models.env");if(!ze.existsSync(e))return{updated:!1};let n=ze.readFileSync(e,"utf-8"),o=n.match(/^NEX_ORCHESTRATOR_MODEL=(.+)$/m),s=o?o[1].trim():null,i=[...t].sort((d,f)=>f.overall-d.overall)[0],a=s?t.find(d=>d.model===s):null,l=a?a.overall:0;if(i.model===s)return{updated:!1};if(l>0&&i.overall<l*1.05)return{updated:!1};let u=`NEX_ORCHESTRATOR_MODEL=${i.model}`;return o?n=n.replace(/^NEX_ORCHESTRATOR_MODEL=.+$/m,u):n+=`
|
|
1105
1106
|
${u}
|
|
1106
|
-
`,ze.writeFileSync(e,n),{updated:!0,previousModel:s,newModel:i.model}}jp.exports={findNewModels:xx,fetchCloudModels:Lp,loadKnownModels:Ya,markBenchmarked:_x,updateReadme:Sx,updateModelsEnv:Ex,updateRoutingConfig:Tx,generateBenchmarkBlock:Dp,checkOrchestratorCandidate:Cx,updateOrchestratorModel:Ax,SENTINEL_START:za,SENTINEL_END:Er}});var Cn=H((Hv,Kp)=>{"use strict";var{C:F}=Ae(),Ox=Oe(),{TOOL_DEFINITIONS:qp}=Et(),Qs=[{id:"read-package",category:"file-ops",prompt:"Read the file package.json and show me its contents.",expectedTool:"read_file",validateArgs:t=>typeof t.path=="string"&&t.path.includes("package.json")},{id:"write-file",category:"file-ops",prompt:'Create a file at /tmp/nex-bench-test.txt with the content "benchmark run".',expectedTool:"write_file",validateArgs:t=>typeof t.path=="string"&&typeof t.content=="string"},{id:"edit-file",category:"file-ops",prompt:'In the file src/config.js, replace the string "debug: false" with "debug: true".',expectedTool:["edit_file","patch_file"],validateArgs:t=>!!(t.path&&t.old_string!==void 0&&t.new_string!==void 0||t.path&&Array.isArray(t.patches)&&t.patches.length>0)},{id:"list-directory",category:"file-ops",prompt:"Show me all files and folders in the cli/ directory.",expectedTool:["list_directory","glob"],validateArgs:t=>!!(typeof t.path=="string"&&t.path.includes("cli")||typeof t.pattern=="string"&&t.pattern.includes("cli"))},{id:"glob-js-files",category:"file-ops",prompt:"Find all JavaScript files (*.js) recursively in the cli/ directory.",expectedTool:"glob",validateArgs:t=>typeof t.pattern=="string"&&t.pattern.includes(".js")},{id:"search-constant",category:"search",prompt:'Search for the string "DEFAULT_MODEL" across all files in the project.',expectedTool:["search_files","grep"],validateArgs:t=>(t.pattern||t.query||t.regex||"").includes("DEFAULT_MODEL")},{id:"grep-function-def",category:"search",prompt:'Find where the function "callStream" is defined in the codebase.',expectedTool:["grep","search_files"],validateArgs:t=>(t.pattern||t.query||t.regex||"").includes("callStream")},{id:"search-todos",category:"search",prompt:"Find all TODO comments in the source code.",expectedTool:["grep","search_files","bash"],validateArgs:t=>JSON.stringify(t).toUpperCase().includes("TODO")},{id:"git-branch",category:"shell",prompt:"What git branch am I currently on?",expectedTool:"bash",validateArgs:t=>typeof t.command=="string"&&t.command.includes("git")},{id:"git-status",category:"shell",prompt:"Show me the current git status of the repository.",expectedTool:"bash",validateArgs:t=>typeof t.command=="string"&&t.command.includes("git status")},{id:"npm-install",category:"shell",prompt:"Run npm install to install project dependencies.",expectedTool:"bash",validateArgs:t=>typeof t.command=="string"&&t.command.includes("npm")},{id:"schema-strict",category:"schema",prompt:"Read the file README.md",expectedTool:"read_file",validateArgs:(t,e)=>{let n=e?.function?.parameters||{},o=n.required||[],s=Object.keys(n.properties||{});return o.every(r=>t[r]!==void 0)&&Object.keys(t).every(r=>s.includes(r))}},{id:"multi-step-version",category:"multi-step",prompt:"What is the current version of this project? Check the source files.",expectedTool:"read_file",validateArgs:t=>typeof t.path=="string"&&t.path.includes("package.json")},{id:"multi-step-count",category:"multi-step",prompt:"How many JavaScript files are in the cli/ directory? Count them.",expectedTool:["bash","glob","list_directory"],validateArgs:t=>!!(typeof t.command=="string"&&t.command.includes("cli")||typeof t.pattern=="string"&&t.pattern.includes("cli")||typeof t.path=="string"&&t.path.includes("cli"))},{id:"no-tool-reasoning",category:"reasoning",prompt:'What does the acronym "API" stand for?',expectedTool:null,validateArgs:()=>!0},{id:"frontend-find-hook",category:"frontend",prompt:"Find all files that import useState from React.",expectedTool:["grep","search_files"],validateArgs:t=>(t.pattern||t.query||t.regex||"").includes("useState")},{id:"frontend-create-component",category:"frontend",prompt:"Create a React functional component called Button that accepts a label prop and renders a styled button element. Save it to src/components/Button.jsx.",expectedTool:"write_file",validateArgs:t=>typeof t.path=="string"&&(t.path.includes(".jsx")||t.path.includes(".tsx")||t.path.includes(".js"))&&typeof t.content=="string"},{id:"frontend-edit-css",category:"frontend",prompt:'In the file src/styles.css, change the background-color value from "blue" to "red".',expectedTool:["edit_file","patch_file"],validateArgs:t=>t.path&&t.old_string!==void 0?t.path.includes(".css")||t.old_string.includes("blue")||t.old_string.includes("background"):!!(t.path&&Array.isArray(t.patches))},{id:"frontend-glob-components",category:"frontend",prompt:"Find all JSX and TSX component files in the components/ directory.",expectedTool:"glob",validateArgs:t=>typeof t.pattern=="string"&&(t.pattern.includes(".jsx")||t.pattern.includes(".tsx")||t.pattern.includes("{jsx,tsx}"))},{id:"frontend-list-assets",category:"frontend",prompt:"List all files in the src/assets/ directory.",expectedTool:["list_directory","glob"],validateArgs:t=>!!(typeof t.path=="string"&&t.path.includes("assets")||typeof t.pattern=="string"&&t.pattern.includes("assets"))},{id:"sysadmin-port-check",category:"sysadmin",prompt:"Which process is currently listening on port 3000?",expectedTool:"bash",validateArgs:t=>typeof t.command=="string"&&(t.command.includes("lsof")||t.command.includes("ss")||t.command.includes("netstat")||t.command.includes("3000"))},{id:"sysadmin-nginx-config",category:"sysadmin",prompt:"Create an nginx server block that proxies requests to localhost:3000. Save it to /etc/nginx/sites-available/myapp.",expectedTool:["write_file","bash"],validateArgs:t=>!!(t.path&&(t.path.includes("nginx")||t.path.includes("sites-available"))||t.command&&t.command.includes("nginx"))},{id:"sysadmin-service-status",category:"sysadmin",prompt:"Check the status of the nginx service and show if it is running.",expectedTool:"bash",validateArgs:t=>typeof t.command=="string"&&(t.command.includes("systemctl")||t.command.includes("service")||t.command.includes("nginx"))},{id:"sysadmin-error-log",category:"sysadmin",prompt:"Show the last 100 lines of the nginx error log.",expectedTool:"bash",validateArgs:t=>typeof t.command=="string"&&(t.command.includes("tail")||t.command.includes("journalctl")||t.command.includes("nginx"))},{id:"sysadmin-docker-compose",category:"sysadmin",prompt:"Create a docker-compose.yml file for a Node.js application with a PostgreSQL database.",expectedTool:"write_file",validateArgs:t=>typeof t.path=="string"&&t.path.includes("docker-compose")&&typeof t.content=="string"},{id:"data-sql-query",category:"data",prompt:"Write a SQL query to find all users who have not logged in for more than 30 days. Save it to queries/inactive-users.sql.",expectedTool:"write_file",validateArgs:t=>typeof t.path=="string"&&(t.path.includes(".sql")||t.path.includes("quer"))&&typeof t.content=="string"},{id:"data-find-json-key",category:"data",prompt:'Find all JSON files in the project that contain the key "userId".',expectedTool:["grep","search_files"],validateArgs:t=>(t.pattern||t.query||t.regex||"").includes("userId")},{id:"data-python-csv",category:"data",prompt:'Write a Python script that reads data.csv and calculates the average of the "price" column. Save it to scripts/average_price.py.',expectedTool:"write_file",validateArgs:t=>typeof t.path=="string"&&t.path.includes(".py")&&typeof t.content=="string"},{id:"data-find-migrations",category:"data",prompt:"Find all database migration files in this project.",expectedTool:["glob","search_files","grep"],validateArgs:t=>JSON.stringify(t).toLowerCase().includes("migrat")},{id:"agentic-test-first",category:"agentic",prompt:"Run the full test suite. If any tests fail, identify the failing test file and read it to understand the issue.",expectedTool:"bash",validateArgs:t=>typeof t.command=="string"&&(t.command.includes("test")||t.command.includes("jest")||t.command.includes("npm"))},{id:"agentic-read-then-act",category:"agentic",prompt:"Read the project README.md, find the TODO section, and list which items are completed.",expectedTool:"read_file",validateArgs:t=>typeof t.path=="string"&&t.path.includes("README")},{id:"agentic-build-deploy",category:"agentic",prompt:"Build the project with npm run build, then verify the output exists in the dist/ directory.",expectedTool:"bash",validateArgs:t=>typeof t.command=="string"&&(t.command.includes("build")||t.command.includes("npm"))}],Fp=["minimax-m2.7:cloud","qwen3-coder:480b","kimi-k2:1t","devstral-2:123b","devstral-small-2:24b"],Up=["minimax-m2.7:cloud","qwen3-coder:480b","devstral-2:123b"],Nx=7,Cr={producedToolCall:.2,correctTool:.35,validArgs:.3,schemaCompliant:.15},Mx="You are a coding assistant. Use the provided tools to help with file operations, search, and development tasks. Only call a tool when one is clearly needed to answer the request. Do not call a tool for questions you can answer from general knowledge.";async function Wp(t,e){let n={taskId:t.id,category:t.category,model:e,producedToolCall:!1,correctTool:!1,validArgs:!1,schemaCompliant:!1,toolCalled:null,error:null,latencyMs:0},o=[{role:"system",content:Mx},{role:"user",content:t.prompt}],s=Date.now();try{let r=await Ox.callChat(o,qp,{provider:"ollama",model:e,temperature:0,timeout:9e4});n.latencyMs=Date.now()-s;let i=r.tool_calls||[];if(t.expectedTool===null){let a=i.length===0;n.producedToolCall=a,n.correctTool=a,n.validArgs=!0,n.schemaCompliant=!0}else if(i.length>0){let a=i[0],l=a.function?.name||"unknown",u=a.function?.arguments||{};n.producedToolCall=!0,n.toolCalled=l;let d=Array.isArray(t.expectedTool)?t.expectedTool:[t.expectedTool];if(n.correctTool=d.includes(l),n.correctTool){let f=qp.find(m=>m.function?.name===l);if(n.validArgs=!!t.validateArgs(u,f),f){let m=f.function?.parameters||{},h=m.required||[],p=Object.keys(m.properties||{});n.schemaCompliant=h.every(g=>u[g]!==void 0)&&Object.keys(u).every(g=>p.includes(g))}}}}catch(r){n.latencyMs=Date.now()-s,n.error=r.message.slice(0,120)}return n}function Zs(t){return t.error?0:((t.producedToolCall?Cr.producedToolCall:0)+(t.correctTool?Cr.correctTool:0)+(t.validArgs?Cr.validArgs:0)+(t.schemaCompliant?Cr.schemaCompliant:0))*100}var Px={coding:"coding",search:"coding",shell:"coding",schema:"coding","multi-step":"coding",reasoning:"coding",frontend:"frontend",sysadmin:"sysadmin",data:"data",agentic:"agentic"};function Xa(t){return Object.entries(t).map(([e,n])=>{let o=n.map(Zs),s=o.reduce((a,l)=>a+l,0)/o.length,r=a=>Math.round(n.filter(a).length/n.length*100),i={};for(let a of["coding","frontend","sysadmin","data","agentic"]){let l=n.filter(d=>Px[d.category]===a);if(l.length===0)continue;let u=l.map(Zs).reduce((d,f)=>d+f,0)/l.length;i[a]=Math.round(u*10)/10}return{model:e,score:Math.round(s*10)/10,toolCallRate:r(a=>!a.error&&(a.producedToolCall||a.category==="reasoning")),correctRate:r(a=>a.correctTool),validArgsRate:r(a=>a.validArgs),schemaRate:r(a=>a.schemaCompliant),avgLatency:Math.round(n.reduce((a,l)=>a+l.latencyMs,0)/n.length),errorCount:n.filter(a=>a.error).length,categoryScores:i,results:n}}).sort((e,n)=>n.score-e.score)}function Ix(t){let e={};for(let n of["coding","frontend","sysadmin","data","agentic"]){let o=t.filter(s=>s.categoryScores[n]!==void 0).sort((s,r)=>r.categoryScores[n]-s.categoryScores[n]);o.length>0&&(e[n]={model:o[0].model,score:o[0].categoryScores[n]})}return e}function Bp(t,e){let n=`nex-code Model Benchmark (${e} tasks \xB7 ollama cloud)`,o=[{label:"#",width:3},{label:"Model",width:26},{label:"Score",width:7},{label:"Tool\u2713",width:7},{label:"Name\u2713",width:7},{label:"Args\u2713",width:7},{label:"Schema\u2713",width:8},{label:"Latency",width:8},{label:"Err",width:4}],s=o.reduce((u,d)=>u+d.width+1,0)+1,r="\u2500".repeat(s);console.log(`
|
|
1107
|
+
`,ze.writeFileSync(e,n),{updated:!0,previousModel:s,newModel:i.model}}jp.exports={findNewModels:xx,fetchCloudModels:Lp,loadKnownModels:Ya,markBenchmarked:_x,updateReadme:Sx,updateModelsEnv:Ex,updateRoutingConfig:Tx,generateBenchmarkBlock:Dp,checkOrchestratorCandidate:Cx,updateOrchestratorModel:Ax,SENTINEL_START:za,SENTINEL_END:Er}});var Cn=H((Hv,Kp)=>{"use strict";var{C:F}=Oe(),Ox=Te(),{TOOL_DEFINITIONS:qp}=Et(),Qs=[{id:"read-package",category:"file-ops",prompt:"Read the file package.json and show me its contents.",expectedTool:"read_file",validateArgs:t=>typeof t.path=="string"&&t.path.includes("package.json")},{id:"write-file",category:"file-ops",prompt:'Create a file at /tmp/nex-bench-test.txt with the content "benchmark run".',expectedTool:"write_file",validateArgs:t=>typeof t.path=="string"&&typeof t.content=="string"},{id:"edit-file",category:"file-ops",prompt:'In the file src/config.js, replace the string "debug: false" with "debug: true".',expectedTool:["edit_file","patch_file"],validateArgs:t=>!!(t.path&&t.old_string!==void 0&&t.new_string!==void 0||t.path&&Array.isArray(t.patches)&&t.patches.length>0)},{id:"list-directory",category:"file-ops",prompt:"Show me all files and folders in the cli/ directory.",expectedTool:["list_directory","glob"],validateArgs:t=>!!(typeof t.path=="string"&&t.path.includes("cli")||typeof t.pattern=="string"&&t.pattern.includes("cli"))},{id:"glob-js-files",category:"file-ops",prompt:"Find all JavaScript files (*.js) recursively in the cli/ directory.",expectedTool:"glob",validateArgs:t=>typeof t.pattern=="string"&&t.pattern.includes(".js")},{id:"search-constant",category:"search",prompt:'Search for the string "DEFAULT_MODEL" across all files in the project.',expectedTool:["search_files","grep"],validateArgs:t=>(t.pattern||t.query||t.regex||"").includes("DEFAULT_MODEL")},{id:"grep-function-def",category:"search",prompt:'Find where the function "callStream" is defined in the codebase.',expectedTool:["grep","search_files"],validateArgs:t=>(t.pattern||t.query||t.regex||"").includes("callStream")},{id:"search-todos",category:"search",prompt:"Find all TODO comments in the source code.",expectedTool:["grep","search_files","bash"],validateArgs:t=>JSON.stringify(t).toUpperCase().includes("TODO")},{id:"git-branch",category:"shell",prompt:"What git branch am I currently on?",expectedTool:"bash",validateArgs:t=>typeof t.command=="string"&&t.command.includes("git")},{id:"git-status",category:"shell",prompt:"Show me the current git status of the repository.",expectedTool:"bash",validateArgs:t=>typeof t.command=="string"&&t.command.includes("git status")},{id:"npm-install",category:"shell",prompt:"Run npm install to install project dependencies.",expectedTool:"bash",validateArgs:t=>typeof t.command=="string"&&t.command.includes("npm")},{id:"schema-strict",category:"schema",prompt:"Read the file README.md",expectedTool:"read_file",validateArgs:(t,e)=>{let n=e?.function?.parameters||{},o=n.required||[],s=Object.keys(n.properties||{});return o.every(r=>t[r]!==void 0)&&Object.keys(t).every(r=>s.includes(r))}},{id:"multi-step-version",category:"multi-step",prompt:"What is the current version of this project? Check the source files.",expectedTool:"read_file",validateArgs:t=>typeof t.path=="string"&&t.path.includes("package.json")},{id:"multi-step-count",category:"multi-step",prompt:"How many JavaScript files are in the cli/ directory? Count them.",expectedTool:["bash","glob","list_directory"],validateArgs:t=>!!(typeof t.command=="string"&&t.command.includes("cli")||typeof t.pattern=="string"&&t.pattern.includes("cli")||typeof t.path=="string"&&t.path.includes("cli"))},{id:"no-tool-reasoning",category:"reasoning",prompt:'What does the acronym "API" stand for?',expectedTool:null,validateArgs:()=>!0},{id:"frontend-find-hook",category:"frontend",prompt:"Find all files that import useState from React.",expectedTool:["grep","search_files"],validateArgs:t=>(t.pattern||t.query||t.regex||"").includes("useState")},{id:"frontend-create-component",category:"frontend",prompt:"Create a React functional component called Button that accepts a label prop and renders a styled button element. Save it to src/components/Button.jsx.",expectedTool:"write_file",validateArgs:t=>typeof t.path=="string"&&(t.path.includes(".jsx")||t.path.includes(".tsx")||t.path.includes(".js"))&&typeof t.content=="string"},{id:"frontend-edit-css",category:"frontend",prompt:'In the file src/styles.css, change the background-color value from "blue" to "red".',expectedTool:["edit_file","patch_file"],validateArgs:t=>t.path&&t.old_string!==void 0?t.path.includes(".css")||t.old_string.includes("blue")||t.old_string.includes("background"):!!(t.path&&Array.isArray(t.patches))},{id:"frontend-glob-components",category:"frontend",prompt:"Find all JSX and TSX component files in the components/ directory.",expectedTool:"glob",validateArgs:t=>typeof t.pattern=="string"&&(t.pattern.includes(".jsx")||t.pattern.includes(".tsx")||t.pattern.includes("{jsx,tsx}"))},{id:"frontend-list-assets",category:"frontend",prompt:"List all files in the src/assets/ directory.",expectedTool:["list_directory","glob"],validateArgs:t=>!!(typeof t.path=="string"&&t.path.includes("assets")||typeof t.pattern=="string"&&t.pattern.includes("assets"))},{id:"sysadmin-port-check",category:"sysadmin",prompt:"Which process is currently listening on port 3000?",expectedTool:"bash",validateArgs:t=>typeof t.command=="string"&&(t.command.includes("lsof")||t.command.includes("ss")||t.command.includes("netstat")||t.command.includes("3000"))},{id:"sysadmin-nginx-config",category:"sysadmin",prompt:"Create an nginx server block that proxies requests to localhost:3000. Save it to /etc/nginx/sites-available/myapp.",expectedTool:["write_file","bash"],validateArgs:t=>!!(t.path&&(t.path.includes("nginx")||t.path.includes("sites-available"))||t.command&&t.command.includes("nginx"))},{id:"sysadmin-service-status",category:"sysadmin",prompt:"Check the status of the nginx service and show if it is running.",expectedTool:"bash",validateArgs:t=>typeof t.command=="string"&&(t.command.includes("systemctl")||t.command.includes("service")||t.command.includes("nginx"))},{id:"sysadmin-error-log",category:"sysadmin",prompt:"Show the last 100 lines of the nginx error log.",expectedTool:"bash",validateArgs:t=>typeof t.command=="string"&&(t.command.includes("tail")||t.command.includes("journalctl")||t.command.includes("nginx"))},{id:"sysadmin-docker-compose",category:"sysadmin",prompt:"Create a docker-compose.yml file for a Node.js application with a PostgreSQL database.",expectedTool:"write_file",validateArgs:t=>typeof t.path=="string"&&t.path.includes("docker-compose")&&typeof t.content=="string"},{id:"data-sql-query",category:"data",prompt:"Write a SQL query to find all users who have not logged in for more than 30 days. Save it to queries/inactive-users.sql.",expectedTool:"write_file",validateArgs:t=>typeof t.path=="string"&&(t.path.includes(".sql")||t.path.includes("quer"))&&typeof t.content=="string"},{id:"data-find-json-key",category:"data",prompt:'Find all JSON files in the project that contain the key "userId".',expectedTool:["grep","search_files"],validateArgs:t=>(t.pattern||t.query||t.regex||"").includes("userId")},{id:"data-python-csv",category:"data",prompt:'Write a Python script that reads data.csv and calculates the average of the "price" column. Save it to scripts/average_price.py.',expectedTool:"write_file",validateArgs:t=>typeof t.path=="string"&&t.path.includes(".py")&&typeof t.content=="string"},{id:"data-find-migrations",category:"data",prompt:"Find all database migration files in this project.",expectedTool:["glob","search_files","grep"],validateArgs:t=>JSON.stringify(t).toLowerCase().includes("migrat")},{id:"agentic-test-first",category:"agentic",prompt:"Run the full test suite. If any tests fail, identify the failing test file and read it to understand the issue.",expectedTool:"bash",validateArgs:t=>typeof t.command=="string"&&(t.command.includes("test")||t.command.includes("jest")||t.command.includes("npm"))},{id:"agentic-read-then-act",category:"agentic",prompt:"Read the project README.md, find the TODO section, and list which items are completed.",expectedTool:"read_file",validateArgs:t=>typeof t.path=="string"&&t.path.includes("README")},{id:"agentic-build-deploy",category:"agentic",prompt:"Build the project with npm run build, then verify the output exists in the dist/ directory.",expectedTool:"bash",validateArgs:t=>typeof t.command=="string"&&(t.command.includes("build")||t.command.includes("npm"))}],Fp=["minimax-m2.7:cloud","qwen3-coder:480b","kimi-k2:1t","devstral-2:123b","devstral-small-2:24b"],Up=["minimax-m2.7:cloud","qwen3-coder:480b","devstral-2:123b"],Nx=7,Cr={producedToolCall:.2,correctTool:.35,validArgs:.3,schemaCompliant:.15},Mx="You are a coding assistant. Use the provided tools to help with file operations, search, and development tasks. Only call a tool when one is clearly needed to answer the request. Do not call a tool for questions you can answer from general knowledge.";async function Wp(t,e){let n={taskId:t.id,category:t.category,model:e,producedToolCall:!1,correctTool:!1,validArgs:!1,schemaCompliant:!1,toolCalled:null,error:null,latencyMs:0},o=[{role:"system",content:Mx},{role:"user",content:t.prompt}],s=Date.now();try{let r=await Ox.callChat(o,qp,{provider:"ollama",model:e,temperature:0,timeout:9e4});n.latencyMs=Date.now()-s;let i=r.tool_calls||[];if(t.expectedTool===null){let a=i.length===0;n.producedToolCall=a,n.correctTool=a,n.validArgs=!0,n.schemaCompliant=!0}else if(i.length>0){let a=i[0],l=a.function?.name||"unknown",u=a.function?.arguments||{};n.producedToolCall=!0,n.toolCalled=l;let d=Array.isArray(t.expectedTool)?t.expectedTool:[t.expectedTool];if(n.correctTool=d.includes(l),n.correctTool){let f=qp.find(m=>m.function?.name===l);if(n.validArgs=!!t.validateArgs(u,f),f){let m=f.function?.parameters||{},h=m.required||[],p=Object.keys(m.properties||{});n.schemaCompliant=h.every(g=>u[g]!==void 0)&&Object.keys(u).every(g=>p.includes(g))}}}}catch(r){n.latencyMs=Date.now()-s,n.error=r.message.slice(0,120)}return n}function Zs(t){return t.error?0:((t.producedToolCall?Cr.producedToolCall:0)+(t.correctTool?Cr.correctTool:0)+(t.validArgs?Cr.validArgs:0)+(t.schemaCompliant?Cr.schemaCompliant:0))*100}var Px={coding:"coding",search:"coding",shell:"coding",schema:"coding","multi-step":"coding",reasoning:"coding",frontend:"frontend",sysadmin:"sysadmin",data:"data",agentic:"agentic"};function Xa(t){return Object.entries(t).map(([e,n])=>{let o=n.map(Zs),s=o.reduce((a,l)=>a+l,0)/o.length,r=a=>Math.round(n.filter(a).length/n.length*100),i={};for(let a of["coding","frontend","sysadmin","data","agentic"]){let l=n.filter(d=>Px[d.category]===a);if(l.length===0)continue;let u=l.map(Zs).reduce((d,f)=>d+f,0)/l.length;i[a]=Math.round(u*10)/10}return{model:e,score:Math.round(s*10)/10,toolCallRate:r(a=>!a.error&&(a.producedToolCall||a.category==="reasoning")),correctRate:r(a=>a.correctTool),validArgsRate:r(a=>a.validArgs),schemaRate:r(a=>a.schemaCompliant),avgLatency:Math.round(n.reduce((a,l)=>a+l.latencyMs,0)/n.length),errorCount:n.filter(a=>a.error).length,categoryScores:i,results:n}}).sort((e,n)=>n.score-e.score)}function Ix(t){let e={};for(let n of["coding","frontend","sysadmin","data","agentic"]){let o=t.filter(s=>s.categoryScores[n]!==void 0).sort((s,r)=>r.categoryScores[n]-s.categoryScores[n]);o.length>0&&(e[n]={model:o[0].model,score:o[0].categoryScores[n]})}return e}function Bp(t,e){let n=`nex-code Model Benchmark (${e} tasks \xB7 ollama cloud)`,o=[{label:"#",width:3},{label:"Model",width:26},{label:"Score",width:7},{label:"Tool\u2713",width:7},{label:"Name\u2713",width:7},{label:"Args\u2713",width:7},{label:"Schema\u2713",width:8},{label:"Latency",width:8},{label:"Err",width:4}],s=o.reduce((u,d)=>u+d.width+1,0)+1,r="\u2500".repeat(s);console.log(`
|
|
1107
1108
|
${F.bold}${n}${F.reset}`),console.log(r);let i=o.map(u=>u.label.padEnd(u.width)).join(" ");if(console.log(`${F.dim}${i}${F.reset}`),console.log(r),t.forEach((u,d)=>{let f=String(d+1).padEnd(o[0].width),m=u.model.slice(0,o[1].width).padEnd(o[1].width),h=String(u.score).padEnd(o[2].width),p=`${u.toolCallRate}%`.padEnd(o[3].width),g=`${u.correctRate}%`.padEnd(o[4].width),y=`${u.validArgsRate}%`.padEnd(o[5].width),w=`${u.schemaRate}%`.padEnd(o[6].width),k=`${(u.avgLatency/1e3).toFixed(1)}s`.padEnd(o[7].width),R=u.errorCount>0?`${F.red}${u.errorCount}${F.reset}`:`${F.dim}0${F.reset}`,x=u.score>=80?F.green:u.score>=60?F.yellow:F.red,_=d===0?`${F.yellow}${f}${F.reset}`:`${F.dim}${f}${F.reset}`;console.log(`${_} ${x}${m}${F.reset} ${F.bold}${x}${h}${F.reset} ${p} ${g} ${y} ${w} ${F.dim}${k}${F.reset} ${R}`)}),console.log(r),t.length>0){let u=t[0];if(console.log(`
|
|
1108
1109
|
${F.bold}${F.green}Winner: ${u.model}${F.reset} score ${u.score}/100`),t.length>1){let d=(u.score-t[1].score).toFixed(1);console.log(`${F.dim}+${d} pts over ${t[1].model}${F.reset}`)}}let a=["coding","frontend","sysadmin","data","agentic"];if(t.some(u=>Object.keys(u.categoryScores).length>1)){console.log(`
|
|
1109
1110
|
${F.bold}Best model per task type:${F.reset}`);for(let u of a){let d=t.filter(p=>p.categoryScores[u]!==void 0).sort((p,g)=>g.categoryScores[u]-p.categoryScores[u]);if(d.length===0)continue;let f=d[0],m=f.categoryScores[u],h=m>=80?F.green:m>=60?F.yellow:F.red;console.log(` ${F.dim}${u.padEnd(10)}${F.reset} ${h}${f.model}${F.reset} ${F.dim}${m}/100${F.reset}`)}}console.log()}async function Lx({models:t,quick:e=!1,onProgress:n}={}){let o=e?Qs.slice(0,Nx):Qs,s=t?.length>0?t:e?Up:Fp,r={};for(let a of s){r[a]=[];for(let l of o){n?.({model:a,task:l.id,done:!1});let u=await Wp(l,a);r[a].push(u),n?.({model:a,task:l.id,done:!0,score:Zs(u),error:u.error})}}let i=Xa(r);return Bp(i,o.length),i}async function Dx({newModels:t,existingRanking:e=[],onProgress:n}={}){if(!t||t.length===0)return e;let o={};for(let i of t){o[i]=[];for(let a of Qs){n?.({model:i,task:a.id,done:!1});let l=await Wp(a,i);o[i].push(l),n?.({model:i,task:a.id,done:!0,score:Zs(l),error:l.error})}}let r=[...Xa(o)];for(let i of e)r.find(a=>a.model===i.model)||r.push(i);return r.sort((i,a)=>a.score-i.score),Bp(r,Qs.length),r}var Ar=[{id:"simple_question",name:"Simple Convergence",prompt:"What is 2+2?",maxTurns:3,successCriteria:["4"]}];function Hp(t){let e=require("fs"),o=require("path").join(t||process.cwd(),".nex","benchmark-config.json");if(!e.existsSync(o))return console.log(`${F.yellow}No scenarios configured.${F.reset} Create ${F.dim}.nex/benchmark-config.json${F.reset} to define your benchmark scenarios.
|
|
@@ -1129,7 +1130,7 @@ ${F.bold}Best model per task type:${F.reset}`);for(let u of a){let d=t.filter(p=
|
|
|
1129
1130
|
${F.bold}Score History (last ${r.length} session${r.length===1?"":"s"}):${F.reset}`);for(let i of r){let a=(i.date||"").replace("T"," ").substring(0,16),l=(i.version||"?").padEnd(8),u=(i.model||"?").substring(0,12).padEnd(12),d=`${i.score}/10`.padStart(6),f=i.grade||"?",m=i.issues&&i.issues.length>0?`${F.yellow}\u26A0 ${i.issues.slice(0,2).map(p=>p.substring(0,30)).join(", ")}${F.reset}`:`${F.green}\u2713${F.reset}`,h=i.score>=8?F.green:i.score>=6?F.yellow:F.red;console.log(` ${F.dim}${a}${F.reset} ${F.dim}v${l}${F.reset} ${F.dim}${u}${F.reset} ${h}${d} ${f}${F.reset} ${m}`)}console.log()}async function Ux({dryRun:t=!1,model:e,cwd:n,onProgress:o}={}){let s=Nn(),r=Hp(n);if(t){console.log(`
|
|
1130
1131
|
${F.bold}Jarvis Benchmark \u2014 Scenarios (dry-run):${F.reset}
|
|
1131
1132
|
`);for(let l of r)console.log(` ${F.cyan}${l.id.padEnd(20)}${F.reset} ${F.dim}${l.name}${F.reset} maxTurns=${l.maxTurns}`),console.log(` ${F.dim}${l.prompt.substring(0,80)}${F.reset}`);return console.log(),[]}let i=(()=>{if(e)return e;try{let{getActiveModel:l}=Un();return l&&l()||"unknown"}catch{return"unknown"}})(),a=[];for(let l of r){o?.({id:l.id,name:l.name,done:!1}),console.log(`
|
|
1132
|
-
${F.dim}Running scenario: ${l.name}...${F.reset}`);let{messages:u,timedOut:d}=await jx(l,{model:i,cwd:n}),f;d||u.length===0?f={score:0,grade:"F",issues:[d?"Scenario timed out":"No messages produced"],summary:"No output",bonuses:[]}:f=Gp(u,l);let m={id:l.id,name:l.name,score:f.score,grade:f.grade,issues:f.issues,bonuses:f.bonuses};a.push(m),o?.({id:l.id,name:l.name,done:!0,score:f.score,grade:f.grade});try{let{appendScoreHistory:h}=or();h(f.score,{version:s.version,model:i,sessionName:`bench:${l.id}`,issues:f.issues})}catch{}}return qx(a,s.version,i),a}Kp.exports={runBenchmark:Lx,runDiscoverBenchmark:Dx,buildSummary:Xa,buildCategoryWinners:Ix,TASKS:Qs,scoreResult:Zs,DEFAULT_MODELS:Fp,QUICK_MODELS:Up,runJarvisBenchmark:Ux,showScoreTrend:Fx,loadScenarios:Hp,DEFAULT_SCENARIOS:Ar,scoreJarvisScenario:Gp}});var tm=H((Gv,em)=>{"use strict";var Wx=require("os"),Xp=require("path"),Ja=require("fs"),{callWithRetry:Yp}=Yo(),{parseModelSpec:Bx}=
|
|
1133
|
+
${F.dim}Running scenario: ${l.name}...${F.reset}`);let{messages:u,timedOut:d}=await jx(l,{model:i,cwd:n}),f;d||u.length===0?f={score:0,grade:"F",issues:[d?"Scenario timed out":"No messages produced"],summary:"No output",bonuses:[]}:f=Gp(u,l);let m={id:l.id,name:l.name,score:f.score,grade:f.grade,issues:f.issues,bonuses:f.bonuses};a.push(m),o?.({id:l.id,name:l.name,done:!0,score:f.score,grade:f.grade});try{let{appendScoreHistory:h}=or();h(f.score,{version:s.version,model:i,sessionName:`bench:${l.id}`,issues:f.issues})}catch{}}return qx(a,s.version,i),a}Kp.exports={runBenchmark:Lx,runDiscoverBenchmark:Dx,buildSummary:Xa,buildCategoryWinners:Ix,TASKS:Qs,scoreResult:Zs,DEFAULT_MODELS:Fp,QUICK_MODELS:Up,runJarvisBenchmark:Ux,showScoreTrend:Fx,loadScenarios:Hp,DEFAULT_SCENARIOS:Ar,scoreJarvisScenario:Gp}});var tm=H((Gv,em)=>{"use strict";var Wx=require("os"),Xp=require("path"),Ja=require("fs"),{callWithRetry:Yp}=Yo(),{parseModelSpec:Bx}=Te(),{extractJSON:zp,DECOMPOSE_PROMPT:Hx,SYNTHESIZE_PROMPT:Gx}=Us(),{C:yt}=Oe(),Or=Xp.join(Wx.homedir(),".nex-code","orchestrator-bench.json"),Jp=[{id:"decompose_multi_bug",type:"decompose",prompt:"Fix 4 bugs: (1) 500 error on SmartThings API call, (2) invalid time format on Sunday schedule, (3) Google Auth callback fails with CORS, (4) contact search returns empty",expectedSubTasks:4,maxSubTasks:5},{id:"decompose_feature_mix",type:"decompose",prompt:"Add dark mode toggle to settings page, fix the broken login redirect, improve search performance by adding an index, update API docs for the new endpoints",expectedSubTasks:4,maxSubTasks:5},{id:"decompose_overlapping",type:"decompose",prompt:"Refactor auth.js to use JWT instead of sessions, update all tests that import auth.js, and add rate limiting to the login endpoint in auth.js",expectedSubTasks:3,maxSubTasks:4},{id:"decompose_single",type:"decompose",prompt:"Fix the broken CSS on the login page \u2014 the submit button is not aligned",expectedSubTasks:1,maxSubTasks:4},{id:"synthesize_clean",type:"synthesize",prompt:"Fix login and search bugs",subResults:[{task:"Fix login redirect",status:"done",result:"Changed auth.js line 42: fixed redirect URL to use req.originalUrl instead of hardcoded /",toolsUsed:["read_file","edit_file"]},{task:"Fix search index",status:"done",result:"Added index on users.email column in migration 20260323. Updated search.js to use indexed query.",toolsUsed:["write_file","edit_file"]}],expectedConflicts:0},{id:"synthesize_conflicts",type:"synthesize",prompt:"Fix config loading and add env validation",subResults:[{task:"Fix config loading",status:"done",result:"Modified config.js: changed loadConfig() to handle missing .env gracefully",toolsUsed:["edit_file"]},{task:"Add env validation",status:"done",result:"Modified config.js: added validateEnv() function that throws on missing required vars",toolsUsed:["edit_file"]}],expectedConflicts:1}];function Vp(t,e){if(!Array.isArray(t))return 0;let n=0;n+=1.5;let o=Math.abs(t.length-e.expectedSubTasks);o===0?n+=3:o===1&&(n+=1.5),t.every(l=>l.task&&typeof l.task=="string"&&l.task.length>0)&&(n+=2);let r=t.flatMap(l=>Array.isArray(l.scope)?l.scope:[]),i=new Set(r);return r.length===i.size&&(n+=2),t.every(l=>typeof l.estimatedCalls=="number"||typeof l.estimatedSshCalls=="number")&&(n+=1.5),Math.min(10,Math.round(n*10)/10)}function Qp(t,e){if(!t||typeof t!="object")return 0;let n=0;n+=1.5,t.summary&&t.summary.length>10&&(n+=2),t.commitMessage&&t.commitMessage.length>5&&(n+=2);let o=Array.isArray(t.conflicts)?t.conflicts:[];return e.expectedConflicts===0&&o.length===0||e.expectedConflicts>0&&o.length>0?n+=2.5:e.expectedConflicts>0&&o.length===0&&(n+=0),Array.isArray(t.filesChanged)&&t.filesChanged.length>0&&(n+=2),Math.min(10,Math.round(n*10)/10)}async function Zp(t,e){let n={};if(e){let s=Bx(e);s.provider&&(n.provider=s.provider),s.model&&(n.model=s.model)}let o=Date.now();try{if(t.type==="decompose"){let s=Hx.replace("{maxSubTasks}",String(t.maxSubTasks)).replace("{prompt}",t.prompt),r=await Yp([{role:"system",content:s},{role:"user",content:t.prompt}],[],n),i=zp(r.content||""),a=Date.now()-o;return{score:Vp(i,t),latencyMs:a}}if(t.type==="synthesize"){let s=t.subResults.map((d,f)=>{let m=d.status==="done"?"SUCCESS":"FAILED";return`--- Agent ${f+1} [${m}] ---
|
|
1133
1134
|
Task: ${d.task}
|
|
1134
1135
|
Result: ${d.result}
|
|
1135
1136
|
Tools: ${d.toolsUsed.join(", ")}`}).join(`
|
|
@@ -1138,7 +1139,7 @@ Tools: ${d.toolsUsed.join(", ")}`}).join(`
|
|
|
1138
1139
|
${yt.bold}${yt.cyan}Orchestrator Model Benchmark${yt.reset}
|
|
1139
1140
|
`);let e=` ${"Model".padEnd(25)} ${"Decompose".padEnd(10)} ${"Synthesize".padEnd(11)} ${"Speed".padEnd(8)} ${"Score".padEnd(8)}`;console.log(`${yt.dim}${e}${yt.reset}`),console.log(` ${yt.dim}${"\u2500".repeat(65)}${yt.reset}`);for(let n of t){let o=t.indexOf(n)===0?"\u{1F947}":t.indexOf(n)===1?"\u{1F948}":t.indexOf(n)===2?"\u{1F949}":" ",s=`${(n.avgLatency/1e3).toFixed(1)}s`;console.log(`${o} ${n.model.padEnd(25)} ${(n.decompose+"/10").padEnd(10)} ${(n.synthesize+"/10").padEnd(11)} ${s.padEnd(8)} ${yt.bold}${n.overall}/10${yt.reset}`)}t.length>0&&(console.log(`
|
|
1140
1141
|
${yt.green}Best orchestrator: ${t[0].model} (${t[0].overall}/10)${yt.reset}`),console.log(` ${yt.dim}Saved to ${Or}${yt.reset}
|
|
1141
|
-
`))}em.exports={runOrchestratorBenchmark:Kx,ORCHESTRATOR_SCENARIOS:Jp,scoreDecompose:Vp,scoreSynthesize:Qp,runScenario:Zp,printResults:Yx,RESULTS_PATH:Or}});var om=H((Kv,sm)=>{var zx=require("axios"),eo=require("fs"),nm=require("path"),Xx="nex-code",Va=nm.join(process.cwd(),".nex"),Qa=nm.join(Va,"last-version-check");eo.existsSync(Va)||eo.mkdirSync(Va,{recursive:!0});async function Jx(){try{let t=Qx(),e=Date.now();if(t&&e-t<1440*60*1e3)return{hasNewVersion:!1};Zx(e);let n=Vx(),s=(await zx.get(`https://registry.npmjs.org/${Xx}/latest`,{timeout:5e3})).data.version,r=ek(s,n);return{hasNewVersion:r,latestVersion:r?s:void 0,currentVersion:r?n:void 0}}catch{return{hasNewVersion:!1}}}function Vx(){return Nn().version}function Qx(){try{if(eo.existsSync(Qa)){let t=eo.readFileSync(Qa,"utf8");return parseInt(t,10)}}catch{}return null}function Zx(t){try{eo.writeFileSync(Qa,t.toString())}catch{}}function ek(t,e){try{let n=t.split(".").map(Number),o=e.split(".").map(Number);return n[0]>o[0]?!0:n[0]<o[0]?!1:n[1]>o[1]?!0:n[1]<o[1]?!1:n[2]>o[2]}catch{return!1}}sm.exports={checkForNewVersion:Jx}});var $m=H((zv,gm)=>{var tk=require("readline"),st=require("fs"),He=require("path"),{C:c,banner:nk,cleanupTerminal:rm}=
|
|
1142
|
+
`))}em.exports={runOrchestratorBenchmark:Kx,ORCHESTRATOR_SCENARIOS:Jp,scoreDecompose:Vp,scoreSynthesize:Qp,runScenario:Zp,printResults:Yx,RESULTS_PATH:Or}});var om=H((Kv,sm)=>{var zx=require("axios"),eo=require("fs"),nm=require("path"),Xx="nex-code",Va=nm.join(process.cwd(),".nex"),Qa=nm.join(Va,"last-version-check");eo.existsSync(Va)||eo.mkdirSync(Va,{recursive:!0});async function Jx(){try{let t=Qx(),e=Date.now();if(t&&e-t<1440*60*1e3)return{hasNewVersion:!1};Zx(e);let n=Vx(),s=(await zx.get(`https://registry.npmjs.org/${Xx}/latest`,{timeout:5e3})).data.version,r=ek(s,n);return{hasNewVersion:r,latestVersion:r?s:void 0,currentVersion:r?n:void 0}}catch{return{hasNewVersion:!1}}}function Vx(){return Nn().version}function Qx(){try{if(eo.existsSync(Qa)){let t=eo.readFileSync(Qa,"utf8");return parseInt(t,10)}}catch{}return null}function Zx(t){try{eo.writeFileSync(Qa,t.toString())}catch{}}function ek(t,e){try{let n=t.split(".").map(Number),o=e.split(".").map(Number);return n[0]>o[0]?!0:n[0]<o[0]?!1:n[1]>o[1]?!0:n[1]<o[1]?!1:n[2]>o[2]}catch{return!1}}sm.exports={checkForNewVersion:Jx}});var $m=H((zv,gm)=>{var tk=require("readline"),st=require("fs"),He=require("path"),{C:c,banner:nk,cleanupTerminal:rm}=Oe(),{isDark:sk}=fn(),{listProviders:tc,getActiveProviderName:An,listAllModels:Yv,setFallbackChain:ok,getFallbackChain:rk,getProvider:ik}=Te(),{flushAutoSave:im}=At(),{getActiveModel:Xt,setActiveModel:nc}=Un(),{printContext:am}=nr(),{loadAllSkills:ak,getSkillCommands:Mr,handleSkillCommand:ck}=nn(),{setReadlineInterface:lk,setAutoConfirm:uk,getAutoConfirm:Za,setAllowAlwaysHandler:dk}=Xe(),{StickyFooter:fk}=gp(),St=process.cwd(),zt=null;function cm(){return zt?.signal??null}var so=[{cmd:"/help",desc:"Show full help"},{cmd:"/model",desc:"Show/switch model"},{cmd:"/providers",desc:"List providers and models"},{cmd:"/fallback",desc:"Show/set fallback chain"},{cmd:"/tokens",desc:"Token usage and context budget"},{cmd:"/costs",desc:"Session token costs"},{cmd:"/budget",desc:"Show/set cost limits per provider"},{cmd:"/clear",desc:"Clear conversation"},{cmd:"/context",desc:"Show project context"},{cmd:"/tree [depth]",desc:"Show project file tree (default depth 3)"},{cmd:"/autoconfirm",desc:"Toggle auto-confirm"},{cmd:"/save",desc:"Save session"},{cmd:"/load",desc:"Load a saved session"},{cmd:"/sessions",desc:"List saved sessions"},{cmd:"/resume",desc:"Resume last session"},{cmd:"/remember",desc:"Save a memory"},{cmd:"/forget",desc:"Delete a memory"},{cmd:"/memory",desc:"Show all memories"},{cmd:"/brain",desc:"Manage knowledge base"},{cmd:"/brain add",desc:"Add document: /brain add <name> [content]"},{cmd:"/brain list",desc:"List all brain documents"},{cmd:"/brain search",desc:"Search brain: /brain search <query>"},{cmd:"/brain show",desc:"Show document: /brain show <name>"},{cmd:"/brain remove",desc:"Remove document: /brain remove <name>"},{cmd:"/brain rebuild",desc:"Rebuild keyword index"},{cmd:"/brain embed",desc:"Build/rebuild embedding index"},{cmd:"/brain status",desc:"Show brain status (docs, index, embeddings)"},{cmd:"/brain review",desc:"Review pending brain changes (git diff)"},{cmd:"/brain undo",desc:"Undo last brain write"},{cmd:"/learn",desc:"Reflect on session and update memory"},{cmd:"/optimize",desc:"Show optimization opportunities"},{cmd:"/permissions",desc:"Show tool permissions"},{cmd:"/allow",desc:"Auto-allow a tool"},{cmd:"/deny",desc:"Block a tool"},{cmd:"/plan",desc:"Plan mode (analyze before executing)"},{cmd:"/plan edit",desc:"Open current plan in $EDITOR"},{cmd:"/plans",desc:"List saved plans"},{cmd:"/auto",desc:"Set autonomy level"},{cmd:"/commit",desc:"Smart commit (diff + message)"},{cmd:"/diff",desc:"Show current diff"},{cmd:"/review [--strict] [file]",desc:"Deep code review with score table and diff suggestions (--strict: force \u22653 critical findings)"},{cmd:"/branch",desc:"Create feature branch"},{cmd:"/mcp",desc:"MCP servers and tools"},{cmd:"/hooks",desc:"Show configured hooks"},{cmd:"/skills",desc:"List, enable, disable skills"},{cmd:"/install-skill <url>",desc:"Install a skill from git URL or user/repo"},{cmd:"/search-skill <query>",desc:"Search for skills on GitHub"},{cmd:"/remove-skill <name>",desc:"Remove an installed skill"},{cmd:"/tasks",desc:"Show task list"},{cmd:"/servers",desc:"List server profiles / ping"},{cmd:"/docker",desc:"List containers across all servers"},{cmd:"/deploy",desc:"List deploy configs / run named deploy"},{cmd:"/benchmark [--quick] [--models=a,b]",desc:"Rank Ollama Cloud models on nex-code tool-calling tasks (--history for nightly log)"},{cmd:"/bench [--dry-run] [--model=<spec>]",desc:"Run Jarvis scenario benchmark (5 agentic tasks)"},{cmd:"/trend [n]",desc:"Show score history trend (default: last 10 sessions)"},{cmd:"/init",desc:"Interactive setup wizard (.nex/)"},{cmd:"/setup",desc:"Configure provider and API keys"},{cmd:"/retry [--model <id>]",desc:"Retry last user turn (optionally with a different model)"},{cmd:"/undo",desc:"Undo last file change"},{cmd:"/redo",desc:"Redo last undone change"},{cmd:"/history",desc:"Show file change history"},{cmd:"/snapshot [name]",desc:"Create a named git snapshot of current changes"},{cmd:"/restore [name|last]",desc:"Restore a previously created snapshot"},{cmd:"/orchestrate <prompt>",desc:"Decompose prompt into parallel sub-tasks, run, synthesize"},{cmd:"/bench-orchestrator",desc:"Benchmark models for orchestrator role"},{cmd:"/audit",desc:"Show tool execution audit log"},{cmd:"/k8s",desc:"Kubernetes overview: namespaces and pods"},{cmd:"/exit",desc:"Quit"}];function lm(){let t=Mr(),e=[...so,...t],n=Math.max(...e.map(o=>o.cmd.length));console.log("");for(let{cmd:o,desc:s}of so)console.log(` ${c.cyan}${o.padEnd(n+2)}${c.reset}${c.dim}${s}${c.reset}`);for(let{cmd:o,desc:s}of t)console.log(` ${c.cyan}${o.padEnd(n+2)}${c.reset}${c.dim}${s} ${c.yellow}[skill]${c.reset}`);console.log(`
|
|
1142
1143
|
${c.dim}Type /help for detailed usage${c.reset}
|
|
1143
1144
|
`)}function um(t){try{let e,n;t.endsWith("/")||t.endsWith(He.sep)?(e=t,n=""):(e=He.dirname(t),n=He.basename(t)),e.startsWith("~")&&(e=He.join(require("os").homedir(),e.slice(1)));let o=He.isAbsolute(e)?e:He.resolve(St,e);if(!st.existsSync(o)||!st.statSync(o).isDirectory())return[];let s=st.readdirSync(o,{withFileTypes:!0}),r=[];for(let i of s){if(i.name.startsWith(".")||i.name==="node_modules"||n&&!i.name.startsWith(n))continue;let a=t.endsWith("/")||t.endsWith(He.sep)?t:He.dirname(t)+"/",l=a==="./"&&!t.startsWith("./")?i.name:a+i.name;r.push(i.isDirectory()?l+"/":l)}return r}catch{return[]}}function dm(t){if(t.startsWith("/")){let o=[...so,...Mr()],s=o.map(r=>r.cmd).filter(r=>r.startsWith(t));return[s.length?s:o.map(r=>r.cmd),t]}let e=t.split(/\s+/),n=e[e.length-1]||"";return n&&(n.includes("/")||n.startsWith("./")||n.startsWith("../")||n.startsWith("~"))?[um(n),n]:[[],t]}function fm(){console.log(`
|
|
1144
1145
|
${c.bold}${c.cyan}Commands:${c.reset}
|
|
@@ -1195,6 +1196,10 @@ ${c.bold}${c.cyan}Tasks:${c.reset}
|
|
|
1195
1196
|
${c.cyan}/tasks${c.reset} ${c.dim}Show current task list${c.reset}
|
|
1196
1197
|
${c.cyan}/tasks clear${c.reset} ${c.dim}Clear all tasks${c.reset}
|
|
1197
1198
|
|
|
1199
|
+
${c.bold}${c.cyan}Conversation:${c.reset}
|
|
1200
|
+
${c.cyan}/retry${c.reset} ${c.dim}Retry last user turn with same model${c.reset}
|
|
1201
|
+
${c.cyan}/retry --model <id>${c.reset}${c.dim} Retry with a different model${c.reset}
|
|
1202
|
+
|
|
1198
1203
|
${c.bold}${c.cyan}Undo / Redo:${c.reset}
|
|
1199
1204
|
${c.cyan}/undo${c.reset} ${c.dim}Undo last file change${c.reset}
|
|
1200
1205
|
${c.cyan}/redo${c.reset} ${c.dim}Redo last undone change${c.reset}
|
|
@@ -1214,7 +1219,8 @@ ${s()}
|
|
|
1214
1219
|
${c.bold}${c.cyan}Cost Limits:${c.reset}`);let g=!1;for(let y of p){let w=r(y.provider),k=h[y.provider];if(k!==void 0){g=!0;let R=Math.min(100,Math.round(w/k*100)),x=10,_=Math.round(R/100*x),b=x-_,N=`${R>=100?c.red:R>=80?c.yellow:c.green}${"\u2588".repeat(_)}${c.dim}${"\u2591".repeat(b)}${c.reset}`;console.log(` ${c.bold}${y.provider}:${c.reset} $${w.toFixed(2)} / $${k.toFixed(2)} (${R}%) ${N}`)}else y.provider==="ollama"||y.provider==="local"?console.log(` ${c.bold}${y.provider}:${c.reset} ${c.dim}free (no limit)${c.reset}`):w>0&&console.log(` ${c.bold}${y.provider}:${c.reset} $${w.toFixed(2)} ${c.dim}(no limit)${c.reset}`)}return g||console.log(` ${c.dim}No limits set. Use /budget <provider> <amount> to set one.${c.reset}`),console.log(),!0}let f=o[1];if(!f){let h=i(d);return h.limit!==null?console.log(`${c.bold}${d}:${c.reset} $${h.spent.toFixed(2)} / $${h.limit.toFixed(2)} ($${h.remaining.toFixed(2)} remaining)`):console.log(`${c.bold}${d}:${c.reset} $${h.spent.toFixed(2)} ${c.dim}(no limit)${c.reset}`),!0}if(f==="off"||f==="remove"||f==="clear")return a(d),l(),console.log(`${c.green}Removed cost limit for ${d}${c.reset}`),!0;let m=parseFloat(f);return isNaN(m)||m<=0?(console.log(`${c.red}Invalid amount: ${f}. Use a positive number or 'off'.${c.reset}`),!0):(u(d,m),l(),console.log(`${c.green}Set ${d} budget limit: $${m.toFixed(2)}${c.reset}`),!0)}case"/clear":{let{clearConversation:s,getConversationMessages:r}=Se(),{clearHistory:i}=Wt(),a=r();if(a.filter(d=>d.role==="user").length>=4){process.stdout.write(`${c.dim}Reflecting on session...${c.reset} `);let{learnFromSession:d}=Ba();d(a).then(f=>{if(!f.skipped&&!f.error&&(f.applied.length>0||f.nexAdded.length>0)){let m=f.applied.length+f.nexAdded.length;process.stdout.write(`${c.green}${m} learning(s) saved${c.reset}
|
|
1215
1220
|
`)}else process.stdout.write(`${c.dim}nothing new${c.reset}
|
|
1216
1221
|
`)}).catch(()=>process.stdout.write(`
|
|
1217
|
-
`))}s(),i();let{deleteSession:u}=At();return u("_autosave"),console.log(`${c.green}Conversation cleared${c.reset}`),!0}case"/
|
|
1222
|
+
`))}s(),i();let{deleteSession:u}=At();return u("_autosave"),console.log(`${c.green}Conversation cleared${c.reset}`),!0}case"/retry":{let{getConversationMessages:s,setConversationMessages:r,processInput:i}=Se(),a=s(),l=a.map(p=>p.role).lastIndexOf("user");if(l===-1)return console.log(`${c.yellow}Nothing to retry \u2014 no user message found.${c.reset}`),!0;let u=o.indexOf("--model"),d=u!==-1&&o[u+1]?o[u+1]:null;if(d){let{setActiveModel:p}=Te();if(!p(d))return console.log(`${c.red}Unknown model: ${d}. Use /providers to see available models.${c.reset}`),!0;console.log(`${c.cyan}Switched to model: ${d}${c.reset}`)}let f=a[l],m;typeof f.content=="string"?m=f.content:Array.isArray(f.content)?m=f.content.filter(p=>p&&p.type==="text").map(p=>p.text).join(`
|
|
1223
|
+
`):m=String(f.content),r(a.slice(0,l));let h=d||"current model";return console.log(`${c.cyan}Retrying with ${h}...${c.reset}`),await i(m),!0}case"/context":return await am(St),!0;case"/tree":{let{generateFileTree:s}=nr(),r=parseInt(o.join(" ").trim(),10),i=!isNaN(r)&&r>0?Math.min(r,8):3,a=s(St,{maxDepth:i,maxFiles:300});return console.log(`
|
|
1218
1224
|
${c.bold}${c.cyan}Project tree${c.reset}${c.dim} (depth ${i})${c.reset}
|
|
1219
1225
|
`),console.log(`${c.dim}${a}${c.reset}
|
|
1220
1226
|
`),!0}case"/autoconfirm":{let s=!Za();return uk(s),console.log(`${c.green}Auto-confirm: ${s?"ON":"OFF"}${c.reset}`),s&&console.log(`${c.yellow} \u26A0 File changes will be applied without confirmation${c.reset}`),to(),!0}case"/save":{let{saveSession:s}=At(),{getConversationMessages:r}=Se(),i=o.join(" ").trim()||`session-${Date.now()}`,a=r();if(a.length===0)return console.log(`${c.yellow}No conversation to save${c.reset}`),!0;let l=Xt(),u=An();return s(i,a,{model:l.id,provider:u}),console.log(`${c.green}Session saved: ${i} (${a.length} messages)${c.reset}`),!0}case"/load":{let{loadSession:s}=At(),{setConversationMessages:r}=Se(),i=o.join(" ").trim();if(!i)return console.log(`${c.red}Usage: /load <name>${c.reset}`),!0;let a=s(i);return a?(r(a.messages),console.log(`${c.green}Loaded session: ${a.name} (${a.messageCount} messages)${c.reset}`),!0):(console.log(`${c.red}Session not found: ${i}${c.reset}`),!0)}case"/sessions":{let{listSessions:s}=At(),r=s();if(r.length===0)return console.log(`${c.dim}No saved sessions${c.reset}`),!0;console.log(`
|
|
@@ -1348,9 +1354,9 @@ ${c.bold}${c.cyan}OpenClaw Nightly Results (${k.length}-day trend)${c.reset}
|
|
|
1348
1354
|
${c.bold}Trend:${c.reset} ${O}${c.reset} over ${R.length} days`)}}console.log();break}if(o.includes("--discover")){let{findNewModels:y,markBenchmarked:w,updateReadme:k,updateModelsEnv:R}=Rr(),{runDiscoverBenchmark:x,buildSummary:_}=Cn();console.log(`
|
|
1349
1355
|
${c.bold}Checking Ollama Cloud for new models...${c.reset}`);let b;try{b=await y()}catch(G){console.log(`${c.red}Discovery failed: ${G.message}${c.reset}`);break}let{newModels:O,allCloud:N}=b;if(console.log(`${c.dim}${N.length} models available on cloud${c.reset}`),O.length===0){console.log(`${c.green}No new models since last benchmark run.${c.reset}
|
|
1350
1356
|
`);break}console.log(`${c.cyan}New models to benchmark (${O.length}):${c.reset} ${O.join(", ")}
|
|
1351
|
-
`);let C=require("os"),L=He.join(C.homedir(),".nex-code","benchmark-results.json"),Ee=[];try{st.existsSync(L)&&(Ee=JSON.parse(st.readFileSync(L,"utf-8")))}catch{}let me="",
|
|
1357
|
+
`);let C=require("os"),L=He.join(C.homedir(),".nex-code","benchmark-results.json"),Ee=[];try{st.existsSync(L)&&(Ee=JSON.parse(st.readFileSync(L,"utf-8")))}catch{}let me="",le=await x({newModels:O,existingRanking:Ee,onProgress:({model:G,task:ne,done:I,score:W,error:se})=>{if(!I){G!==me&&(me&&process.stdout.write(`
|
|
1352
1358
|
`),me=G,process.stdout.write(`${c.cyan}${G}${c.reset} `));return}let X=se?`${c.red}\u2717${c.reset}`:W>=80?`${c.green}\xB7${c.reset}`:W>=40?`${c.yellow}\xB7${c.reset}`:`${c.red}\xB7${c.reset}`;process.stdout.write(X)}});me&&process.stdout.write(`
|
|
1353
|
-
`);try{st.writeFileSync(L,JSON.stringify(
|
|
1359
|
+
`);try{st.writeFileSync(L,JSON.stringify(le,null,2))}catch{}w(N);let ie=He.join(process.cwd(),"README.md"),oe=k(le,ie),ce=R(le),{buildCategoryWinners:Q}=Cn(),{updateRoutingConfig:E}=Rr(),M=Q(le),U=E(M);if(oe&&console.log(`${c.green}README.md benchmark table updated${c.reset}`),ce.updated?console.log(`${c.green}DEFAULT_MODEL: ${ce.previousModel} \u2192 ${ce.newModel}${c.reset}`):ce.reason&&console.log(`${c.dim}models.env unchanged: ${ce.reason}${c.reset}`),U.changes.length>0){console.log(`${c.green}Routing updated:${c.reset}`);for(let G of U.changes)console.log(` ${c.dim}${G}${c.reset}`)}return console.log(),!0}let{runBenchmark:s,DEFAULT_MODELS:r,QUICK_MODELS:i}=Cn(),a=o.includes("--quick"),l=o.find(y=>y.startsWith("--models=")),u=l?l.replace("--models=","").split(",").map(y=>y.trim()).filter(Boolean):[],d=a?7:15,f=u.length>0?u:a?i:r;console.log(`
|
|
1354
1360
|
${c.bold}Starting benchmark${c.reset} ${c.dim}${d} tasks \xB7 ${f.length} models \xB7 ollama cloud${c.reset}`),console.log(`${c.dim}Models: ${f.join(", ")}${c.reset}
|
|
1355
1361
|
`);let m="",h=0,p=d*f.length,g=await s({models:f,quick:a,onProgress:({model:y,task:w,done:k,score:R,error:x})=>{if(!k){y!==m&&(m&&process.stdout.write(`
|
|
1356
1362
|
`),m=y,process.stdout.write(`${c.cyan}${y}${c.reset} `));return}h++;let _=x?`${c.red}\u2717${c.reset}`:R>=80?`${c.green}\xB7${c.reset}`:R>=40?`${c.yellow}\xB7${c.reset}`:`${c.red}\xB7${c.reset}`;process.stdout.write(_)}});if(m&&process.stdout.write(`
|
|
@@ -1365,11 +1371,11 @@ ${c.bold}Jarvis Benchmark${c.reset} ${c.dim}5 agentic scenarios \xB7 each run a
|
|
|
1365
1371
|
`)}catch{}}function wt(){return`${c.bold}${c.cyan}>${c.reset} `}function to(){if(!global._nexFooter)return;let{isPlanMode:t,getAutonomyLevel:e}=Dt(),{getAutoConfirm:n}=Xe(),o=[];t()&&o.push("plan");let s=e();s==="semi-auto"&&o.push("semi"),s==="autonomous"&&o.push("auto"),n()&&o.push("always"),global._nexFooter.setStatusInfo({mode:o.join(" \xB7 ")})}var rc="\x1B[200~",ic="\x1B[201~";function pk(t){return typeof t=="string"&&t.includes(rc)}function mk(t){return typeof t=="string"&&t.includes(ic)}function no(t){return typeof t!="string"?t:t.split(rc).join("").split(ic).join("")}async function hk(){if(!ik("local"))return!1;try{let{exec:e}=require("child_process"),{promisify:n}=require("util");return await n(e)("curl -s --max-time 1 http://localhost:11434/api/tags"),nc("local:llama3"),!0}catch{return!1}}async function gk(){let{setAbortSignalGetter:t,getConversationLength:e,processInput:n}=Se();t(cm);let s=tc().some(I=>I.configured),r=(async()=>{ak();let I=Xt(),W=An();return{model:I,providerName:W}})(),i=(async()=>s?!0:await hk()?(console.log(`${c.green}\u2713 Local Ollama detected \u2014 using local models${c.reset}`),console.log(`${c.dim}Tip: Set API keys for cloud providers for more model options (OPENAI_API_KEY, ANTHROPIC_API_KEY, etc.)${c.reset}
|
|
1366
1372
|
`),!0):!1)(),a=(async()=>{if(process.env.NEX_DISABLE_UPDATE_CHECK==="1")return{hasNewVersion:!1};try{let{checkForNewVersion:I}=om();return await I()}catch{return{hasNewVersion:!1}}})(),[l,u,d]=await Promise.all([r,i,a]);!u&&!s&&(console.error(`
|
|
1367
1373
|
${c.red}\u2717 No provider configured and no local Ollama detected.${c.reset}
|
|
1368
|
-
`),process.exit(1));let{loadPersistedHistory:f,pruneHistory:m}=Wt();f().then(I=>{}),m().catch(()=>{});let h=hm(),p=tk.createInterface({input:process.stdin,output:process.stdout,prompt:wt(),completer:dm,history:h,historySize:sc});lk(p);let{setAskUserHandler:g}=Et();g(async(I,W)=>{let se="\x1B[0m",X="\x1B[1m",
|
|
1374
|
+
`),process.exit(1));let{loadPersistedHistory:f,pruneHistory:m}=Wt();f().then(I=>{}),m().catch(()=>{});let h=hm(),p=tk.createInterface({input:process.stdin,output:process.stdout,prompt:wt(),completer:dm,history:h,historySize:sc});lk(p);let{setAskUserHandler:g}=Et();g(async(I,W)=>{let se="\x1B[0m",X="\x1B[1m",Z="\x1B[2m",he="\x1B[36m";return process.stdout.write(`
|
|
1369
1375
|
${X}\x1B[33m\u2753${se} ${X}${I}${se}
|
|
1370
1376
|
|
|
1371
1377
|
`),W.forEach((we,be)=>{process.stdout.write(` ${he}${be+1}${se} ${we}
|
|
1372
|
-
`)}),process.stdout.write(` ${
|
|
1378
|
+
`)}),process.stdout.write(` ${Z}${W.length+1}${se} ${Z}Eigene Antwort\u2026${se}
|
|
1373
1379
|
`),process.stdout.write(`
|
|
1374
1380
|
${he}[1-${W.length+1}]${se} \u203A `),new Promise(we=>{p.resume(),p.once("line",be=>{let je=be.trim(),Ge=parseInt(je);Ge>=1&&Ge<=W.length?(process.stdout.write(`
|
|
1375
1381
|
`),we(W[Ge-1])):Ge===W.length+1||je===""?(process.stdout.write(` ${he}\u203A${se} `),p.once("line",Jt=>{process.stdout.write(`
|
|
@@ -1378,20 +1384,20 @@ ${c.red}\u2717 No provider configured and no local Ollama detected.${c.reset}
|
|
|
1378
1384
|
`),await am(St);let k=!1,R=0,x=!1,_=null;function b(){im(),y.deactivate(),rm(),process.stdin.isTTY&&process.stdout.write("\x1B[?2004l"),process.stdout.write("\x1B[r\x1B[H\x1B[2J\x1B[3J"),process.exit(0)}process.on("SIGTERM",b),process.on("exit",()=>{im()}),p.on("SIGINT",()=>{if(rm(),R++,R>=2){b();return}if(k){zt&&zt.abort();let{cancelPendingAskUser:I}=Et();I(),console.log(`
|
|
1379
1385
|
${c.yellow} Task cancelled. Press Ctrl+C again to exit.${c.reset}`),k=!1,p.setPrompt(wt()),p.prompt()}else console.log(`${c.dim} (Press Ctrl+C again to exit)${c.reset}`),p.setPrompt(wt()),p.prompt(),_&&clearTimeout(_),_=setTimeout(()=>{R=0,_=null},2e3)}),process.on("SIGINT",()=>{process.stdin.isTTY?(R++,R>=2&&b()):b()});let O=!1,N=[],C=0,L={},Ee=!1;function me(){let I=N.join(`
|
|
1380
1386
|
`).replace(/\r/g,"").trim();if(N=[],O=!1,!I)return!0;C++,Ee=!0;let W=C;L[W]=I;let se=I.split(`
|
|
1381
|
-
`).length,X=se>1?`[Pasted content #${W} \u2014 ${se} lines]`:`[Pasted content #${W}]`,
|
|
1382
|
-
`)),me()}if(
|
|
1387
|
+
`).length,X=se>1?`[Pasted content #${W} \u2014 ${se} lines]`:`[Pasted content #${W}]`,Z=p.line||"",he=Z&&!Z.endsWith(" ")?" ":"",pe=Z+he+X;return p.setPrompt(wt()),p.prompt(),p.line=pe,p.cursor=pe.length,p._refreshLine(),!0}function le(I){return I.replace(/\[Pasted content #(\d+)(?:[^\]]*)\]/g,(W,se)=>L[Number(se)]||"")}function ie(){C=0,L={},Ee=!1}if(process.stdin.isTTY){process.stdout.write("\x1B[?2004h");let I=process.stdin.emit.bind(process.stdin);process.stdin.emit=function(W,...se){if(W!=="data")return I.call(process.stdin,W,...se);let X=se[0];if(Buffer.isBuffer(X)&&(X=X.toString("utf8")),typeof X!="string")return I.call(process.stdin,W,...se);let Z=X.includes(rc),he=X.includes(ic);if(Z&&he){let pe=no(X);return pe&&N.push(...pe.split(`
|
|
1388
|
+
`)),me()}if(Z){O=!0,N=[];let pe=no(X);return pe&&N.push(...pe.split(`
|
|
1383
1389
|
`)),!0}if(he){let pe=no(X);return pe&&N.push(...pe.split(`
|
|
1384
1390
|
`)),me()}if(O){let pe=no(X);return pe&&N.push(...pe.split(`
|
|
1385
1391
|
`)),!0}return X.includes(`
|
|
1386
1392
|
`)&&X.length>40&&!O?(N.push(...X.replace(/\r/g,"").split(`
|
|
1387
|
-
`)),me()):I.call(process.stdin,W,...se)}}let oe=0;function
|
|
1388
|
-
${
|
|
1389
|
-
`),p.prompt()}return}if(E!==null){if(E._mode==="triple"){if(I.trim()==='"""'){let
|
|
1390
|
-
`).trim();if(E=null,
|
|
1391
|
-
`)[0]||"An unexpected error occurred";console.log(`${c.red}Error: ${we}${c.reset}`)}}k=!1;let he=se();he>0&&process.stdout.write(`${c.gray}[${he} messages] ${c.reset}`)}p.setPrompt(wt()),p.prompt();return}E.push(I),p.setPrompt(M),p.prompt();return}if(I.endsWith("\\"))E.push(I.slice(0,-1));else{E.push(I);let
|
|
1392
|
-
`).trim();if(E=null,
|
|
1393
|
-
`)[0]||"An unexpected error occurred";console.log(`${c.red}Error: ${be}${c.reset}`)}}let{getConversationLength:he}=Se();k=!1;let pe=he();pe>0&&process.stdout.write(`${c.gray}[${pe} messages] ${c.reset}`)}p.setPrompt(wt()),p.prompt();return}p.setPrompt(M),p.prompt();return}if(I.trim()==='"""'||I.trim().startsWith('"""')){let
|
|
1394
|
-
`).forEach((we,be)=>{let je=be===0?"\x1B[1;36m\u203A\x1B[22;39m":" ",Ge=2+we.length,Jt=" ".repeat(Math.max(0,he-Ge));console.log(`${
|
|
1393
|
+
`)),me()):I.call(process.stdin,W,...se)}}let oe=0;function ce(){if(oe>0){let I=y._scrollEnd,W="\x1B7";for(let se=0;se<oe;se++)W+=`\x1B[${I-oe+1+se};1H\x1B[2K`;W+="\x1B8",y.rawWrite(W),oe=0}}function Q(I){let W=[...so,...Mr()].filter(be=>be.cmd.startsWith(I));if(!W.length||W.length===1&&W[0].cmd===I)return;let se=y._scrollEnd,X=Math.min(10,se-2);if(X<1)return;let Z=W.slice(0,X),he=Math.max(...Z.map(be=>be.cmd.length));oe=Z.length,W.length>X&&oe++;let pe=se-oe+1,we="\x1B7";for(let be=0;be<Z.length;be++){let{cmd:je,desc:Ge}=Z[be],Jt=je.substring(0,I.length),On=je.substring(I.length),Pr=" ".repeat(Math.max(0,he-je.length+2));we+=`\x1B[${pe+be};1H\x1B[2K ${c.cyan}${Jt}${c.reset}${c.dim}${On}${Pr}${Ge}${c.reset}`}W.length>X&&(we+=`\x1B[${pe+Z.length};1H\x1B[2K ${c.dim}\u2026 +${W.length-X} more${c.reset}`),we+="\x1B8",y.rawWrite(we)}process.stdin.isTTY&&process.stdin.on("keypress",(I,W)=>{ce(),!(W&&(W.name==="tab"||W.name==="return"))&&setImmediate(()=>{p.line&&p.line.startsWith("/")&&Q(p.line)})});let E=null,M=`${c.dim}...${c.reset} `;function U(I){return(I.match(/[^\s\d](\d{1,2})\.\s+\S/g)||[]).length<2?I:I.replace(/([^\s\d])(\d{1,2})\.\s+/g,(se,X,Z)=>`${X}
|
|
1394
|
+
${Z}. `).trim()}let{loadSession:G}=At(),{setConversationMessages:ne}=Se();if(e()===0){let I=G("_autosave");if(I&&I.messages&&I.messages.length>0&&Date.now()-new Date(I.updatedAt).getTime()<1440*60*1e3){let{confirm:se}=Xe();if(await se("Previous session found. Resume?")){let he=I.messages,pe=he.length>20?he.slice(-20):he;ne(pe);let{getUsage:we,forceCompress:be}=Qe();if(we(pe,[]).percentage>=30){let{messages:Ge}=be(pe,[]);ne(Ge)}}}}p.setPrompt(wt()),p.prompt(),p.on("line",async I=>{if(ce(),Object.keys(L).length>0&&(I=le(I),ie(),p.setPrompt(wt())),k){let Z=I.trim();if(Z){let{injectMidRunNote:he}=Se();he(Z),process.stdout.write(`${c.cyan} \u270E Queued \u2014 will be applied in the next step${c.reset}
|
|
1395
|
+
`),p.prompt()}return}if(E!==null){if(E._mode==="triple"){if(I.trim()==='"""'){let Z=E.join(`
|
|
1396
|
+
`).trim();if(E=null,Z){Nr(Z.replace(/\n/g,"\\n")),k=!0,p.prompt(),R=0,x=!1,_&&(clearTimeout(_),_=null),zt=new AbortController;try{await n(Z)}catch(pe){if(!zt?.signal?.aborted){let we=pe.message?.split(`
|
|
1397
|
+
`)[0]||"An unexpected error occurred";console.log(`${c.red}Error: ${we}${c.reset}`)}}k=!1;let he=se();he>0&&process.stdout.write(`${c.gray}[${he} messages] ${c.reset}`)}p.setPrompt(wt()),p.prompt();return}E.push(I),p.setPrompt(M),p.prompt();return}if(I.endsWith("\\"))E.push(I.slice(0,-1));else{E.push(I);let Z=E.join(`
|
|
1398
|
+
`).trim();if(E=null,Z){Nr(Z.replace(/\n/g,"\\n")),k=!0,p.prompt(),R=0,x=!1,_&&(clearTimeout(_),_=null),zt=new AbortController;try{await n(Z)}catch(we){if(!zt?.signal?.aborted){let be=we.message?.split(`
|
|
1399
|
+
`)[0]||"An unexpected error occurred";console.log(`${c.red}Error: ${be}${c.reset}`)}}let{getConversationLength:he}=Se();k=!1;let pe=he();pe>0&&process.stdout.write(`${c.gray}[${pe} messages] ${c.reset}`)}p.setPrompt(wt()),p.prompt();return}p.setPrompt(M),p.prompt();return}if(I.trim()==='"""'||I.trim().startsWith('"""')){let Z=I.trim().substring(3);E=Z?[Z]:[],E._mode="triple",p.setPrompt(M),p.prompt();return}if(I.endsWith("\\")){E=[I.slice(0,-1)],E._mode="backslash",p.setPrompt(M),p.prompt();return}let W=U(I.trim());if(!W){p.setPrompt(wt()),p.prompt();return}if(Nr(W),W==="/"){lm(),p.setPrompt(wt()),p.prompt();return}if(W.startsWith("/")){await mm(W,p),p.setPrompt(wt()),p.prompt();return}{let Z=sk?"\x1B[48;5;237m":"\x1B[48;2;220;225;235m",he=process.stdout.columns||80;W.split(`
|
|
1400
|
+
`).forEach((we,be)=>{let je=be===0?"\x1B[1;36m\u203A\x1B[22;39m":" ",Ge=2+we.length,Jt=" ".repeat(Math.max(0,he-Ge));console.log(`${Z}${je} ${we}${Jt}\x1B[0m`)})}if(process.env.NEX_AUTO_PLAN!=="0"&&!Za()){let{isPlanMode:Z,setPlanMode:he}=Dt(),{invalidateSystemPromptCache:pe}=Se(),we=/\b(implement|refactor|migrate|redesign)\b/i,be=/\b(create|build|add|write|introduce|develop|set\s+up)\b/i,je=/^(how|what|why|when|where|which|explain|show|list|tell|describe|can\s+you|could\s+you|do\s+you)\b/i,Ge=/\b(spawn[_\s]?agents?|swarm)\b/i;!je.test(W)&&!Ge.test(W)&&(we.test(W)||be.test(W)&&W.split(/\s+/).length>=5)&&!Z()&&(he(!0),pe(),console.log(`${c.cyan}${c.bold}\u2387 Auto Plan Mode${c.reset}${c.dim} \u2014 implementation task detected \xB7 read-only until /plan approve${c.reset}`))}{let{getConversationLength:Z}=Se();if(Z()===0)try{let{detectCategory:he,getModelForCategory:pe}=Ka(),we=he(W);if(we&&we.id!=="coding"){let be=pe(we.id),je=Xt();if(be&&be!==je?.id&&nc(be)){let Ge=Xt();console.log(`${c.dim}\u21B3 ${we.icon} ${we.label} task \u2014 routing to ${Ge?.name||be}${c.reset}`),global._nexFooter&&global._nexFooter.setStatusInfo({model:Ge?.name||be})}}}catch{}}k=!0,p.prompt(),R=0,x=!1,_&&(clearTimeout(_),_=null),zt=new AbortController;try{await n(W)}catch(Z){if(!zt?.signal?.aborted){let he=Z.message?.split(`
|
|
1395
1401
|
`)[0]||"An unexpected error occurred";console.log(`${c.red}Error: ${he}${c.reset}`)}}k=!1;let{getConversationLength:se}=Se(),X=se();X>0&&process.stdout.write(`${c.gray}[${X} messages] ${c.reset}`),p.setPrompt(wt()),p.prompt()}),p.on("close",()=>{process.stdin.isTTY&&process.stdout.write("\x1B[?2004l"),process.stdout.write("\x1B[r\x1B[H\x1B[2J\x1B[3J"),process.exit(0)})}gm.exports={startREPL:gk,getPrompt:wt,loadHistory:hm,appendHistory:Nr,getHistoryPath:oc,HISTORY_MAX:sc,showCommandList:lm,completer:dm,completeFilePath:um,handleSlashCommand:mm,showProviders:ec,showHelp:fm,renderBar:pm,hasPasteStart:pk,hasPasteEnd:mk,stripPasteSequences:no,getAbortSignal:cm}});var wm=H((Xv,ym)=>{ym.exports=$m()});var uc=require("path");require("dotenv").config({path:uc.join(__dirname,"..",".env")});require("dotenv").config();var ye=process.argv.slice(2);(ye.includes("--help")||ye.includes("-h"))&&(console.log(`Usage: nex-code [options]
|
|
1396
1402
|
|
|
1397
1403
|
Options:
|
|
@@ -1411,4 +1417,4 @@ Options:
|
|
|
1411
1417
|
--json Output result as JSON (for CI parsing)
|
|
1412
1418
|
-h, --help Show this help
|
|
1413
1419
|
-v, --version Show version
|
|
1414
|
-
`),process.exit(0));if(ye.includes("-v")||ye.includes("--version")){let t=Nn();console.log(t.version),process.exit(0)}var xm=ye.includes("--yolo")||ye.includes("-yolo");if(xm){let{setAutoConfirm:t}=Xe();t(!0)}if(!xm)try{let t=require("fs"),e=uc.join(process.cwd(),".nex","config.json");if(t.existsSync(e)&&JSON.parse(t.readFileSync(e,"utf-8")).yolo===!0){let{setAutoConfirm:o}=Xe();o(!0)}}catch{}var ac=ye.indexOf("--model");if(ac!==-1&&ye[ac+1]){let{setActiveModel:t}=
|
|
1420
|
+
`),process.exit(0));if(ye.includes("-v")||ye.includes("--version")){let t=Nn();console.log(t.version),process.exit(0)}var xm=ye.includes("--yolo")||ye.includes("-yolo");if(xm){let{setAutoConfirm:t}=Xe();t(!0)}if(!xm)try{let t=require("fs"),e=uc.join(process.cwd(),".nex","config.json");if(t.existsSync(e)&&JSON.parse(t.readFileSync(e,"utf-8")).yolo===!0){let{setAutoConfirm:o}=Xe();o(!0)}}catch{}var ac=ye.indexOf("--model");if(ac!==-1&&ye[ac+1]){let{setActiveModel:t}=Te();t(ye[ac+1])}var cc=ye.indexOf("--max-turns");if(cc!==-1&&ye[cc+1]){let t=parseInt(ye[cc+1],10);if(t>0){let{setMaxIterations:e}=Se();e(t)}}else try{let t=require("fs"),e=uc.join(process.cwd(),".nex","config.json");if(t.existsSync(e)){let n=JSON.parse(t.readFileSync(e,"utf-8")),o=parseInt(n.maxIterations,10);if(o>0){let{setMaxIterations:s}=Se();s(o)}}}catch{}function lc(){if(process.platform==="darwin")try{let{spawn:t}=require("child_process"),e=t("caffeinate",["-i","-m"],{stdio:"ignore",detached:!1});e.unref();let n=()=>{try{e.kill()}catch{}};process.on("exit",n),process.on("SIGINT",n),process.on("SIGTERM",n)}catch{}}async function $k(){let{runSetupWizard:t}=Fa();await t()}function bm(t){if(ye.includes("--auto")){let{setAutoConfirm:a}=Xe();a(!0)}if(!ye.includes("--model")){let{setActiveModel:a}=Te(),l=process.env.HEADLESS_MODEL||"devstral-small-2:24b";a(l)}let n=ye.includes("--auto-orchestrate")||process.env.NEX_AUTO_ORCHESTRATE==="true",o=ye.indexOf("--orchestrator-model"),s=o!==-1?ye[o+1]:void 0,{processInput:r,getConversationMessages:i}=Se();r(t,null,{autoOrchestrate:n,orchestratorModel:s}).then(()=>{if(ye.includes("--json")){let l=i().filter(u=>u.role==="assistant").pop();console.log(JSON.stringify({success:!0,response:l?.content||""}))}process.exit(0)}).catch(a=>{ye.includes("--json")?console.log(JSON.stringify({success:!1,error:a.message})):console.error(a.message),process.exit(1)})}if(ye.includes("--server")){let{setAutoConfirm:t}=Xe();t(!0),fp().startServerMode();return}var _m=ye.indexOf("--prompt-file");if(_m!==-1){let t=ye[_m+1];(!t||t.startsWith("--"))&&(console.error("--prompt-file requires a file path"),process.exit(1));let e=require("fs"),n;try{n=e.readFileSync(t,"utf-8").trim()}catch(o){console.error(`--prompt-file: cannot read file: ${o.message}`),process.exit(1)}if(n||(console.error("--prompt-file: file is empty"),process.exit(1)),ye.includes("--delete-prompt-file"))try{e.unlinkSync(t)}catch{}lc(),bm(n)}else{let t=ye.indexOf("--task")!==-1?ye.indexOf("--task"):ye.indexOf("--prompt");if(t!==-1){let e=ye[t+1];if((!e||e.startsWith("--"))&&(console.error("--task/--prompt requires a prompt"),process.exit(1)),lc(),ye.includes("--orchestrate")){let n=ye.indexOf("--orchestrator-model"),o=n!==-1?ye[n+1]:void 0,{runOrchestrated:s}=Us();s(e,{orchestratorModel:o}).then(()=>{process.exit(0)}).catch(r=>{console.error(`Orchestrator error: ${r.message}`),process.exit(1)})}else bm(e)}else $k().then(()=>{lc();let{startREPL:e}=wm();e()})}
|