xuanzhu 0.1.6 → 0.1.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/dist/cli.js +18 -18
  2. package/package.json +1 -1
package/dist/cli.js CHANGED
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env node
2
- "use strict";var fn=Object.create;var At=Object.defineProperty;var gn=Object.getOwnPropertyDescriptor;var $n=Object.getOwnPropertyNames;var yn=Object.getPrototypeOf,xn=Object.prototype.hasOwnProperty;var bn=(r,e,n,s)=>{if(e&&typeof e=="object"||typeof e=="function")for(let i of $n(e))!xn.call(r,i)&&i!==n&&At(r,i,{get:()=>e[i],enumerable:!(s=gn(e,i))||s.enumerable});return r};var x=(r,e,n)=>(n=r!=null?fn(yn(r)):{},bn(e||!r||!r.__esModule?At(n,"default",{value:r,enumerable:!0}):n,r));var $e=x(require("fs")),Qe=x(require("path"));var D=x(require("fs"));var P=x(require("fs")),ye=x(require("os")),z=x(require("path"));function A(){let r=process.env.XZH_HOME?.trim();return r?z.resolve(j(r)):z.join(ye.homedir(),".xzh")}function vn(){return!!process.env.XZH_HOME?.trim()}function wn(){return z.join(ye.homedir(),".xuanzhu")}function E(){return z.join(A(),"config.json")}function It(){return z.join(__dirname,"skills")}function Lt(){return z.join(A(),"skills")}function Ot(){if(vn())return{migrated:!1};let r=wn(),e=A();if(z.resolve(r)===z.resolve(e))return{migrated:!1};try{if(P.existsSync(e)||!P.existsSync(r))return{migrated:!1}}catch{return{migrated:!1}}try{return P.renameSync(r,e),{migrated:!0,from:r,to:e}}catch{}let n=`${e}.migrating-${process.pid}`;try{P.rmSync(n,{recursive:!0,force:!0}),P.cpSync(r,n,{recursive:!0}),P.renameSync(n,e)}catch{try{P.rmSync(n,{recursive:!0,force:!0})}catch{}return{migrated:!1}}try{P.rmSync(r,{recursive:!0,force:!0})}catch{}return{migrated:!0,from:r,to:e}}function xe(r){P.existsSync(r)||P.mkdirSync(r,{recursive:!0})}function j(r){return r&&(r==="~"?ye.homedir():r.startsWith("~/")||r.startsWith("~\\")?z.join(ye.homedir(),r.slice(2)):r)}var T=10,et="{{'\u7528\u6237\u53D1\u9001\u8FC7\u6765\u7684\u6D88\u606F'}}",Ut=`\u8BF7\u5206\u6790\u4E0B\u9762\u6D88\u606F\u7684\u610F\u56FE\uFF0C\u4EE5\u7B2C\u4E00\u4EBA\u79F0\u8F93\u51FA\u4E09\u6BB5\uFF1A\u3010\u6838\u5FC3\u610F\u56FE\u3011\u4E00\u53E5\u8BDD\u603B\u7ED3\uFF1B\u3010\u9700\u6C42\u62C6\u89E3\u3011\u9010\u6761\u5217\u51FA\u660E\u786E\u9700\u6C42\uFF1B\u3010\u9690\u6027\u610F\u56FE\u3011\u6F5C\u5728\u8BC9\u6C42\u3002\u4E0D\u8981\u589E\u52A0\u539F\u6587\u4E0D\u5B58\u5728\u4FE1\u606F\u3002 \u6D88\u606F\uFF1A ${et}`;function Kt(r,e){if(r.includes(et))return r.replace(et,()=>e);let n=/\{\{\s*[^{}]*\s*\}\}/;return n.test(r)?r.replace(n,()=>e):`${r}
2
+ "use strict";var fn=Object.create;var At=Object.defineProperty;var gn=Object.getOwnPropertyDescriptor;var $n=Object.getOwnPropertyNames;var yn=Object.getPrototypeOf,xn=Object.prototype.hasOwnProperty;var bn=(r,e,n,s)=>{if(e&&typeof e=="object"||typeof e=="function")for(let i of $n(e))!xn.call(r,i)&&i!==n&&At(r,i,{get:()=>e[i],enumerable:!(s=gn(e,i))||s.enumerable});return r};var x=(r,e,n)=>(n=r!=null?fn(yn(r)):{},bn(e||!r||!r.__esModule?At(n,"default",{value:r,enumerable:!0}):n,r));var $e=x(require("fs")),Qe=x(require("path"));var D=x(require("fs"));var P=x(require("fs")),ye=x(require("os")),j=x(require("path"));function A(){let r=process.env.XZH_HOME?.trim();return r?j.resolve(z(r)):j.join(ye.homedir(),".xzh")}function vn(){return!!process.env.XZH_HOME?.trim()}function wn(){return j.join(ye.homedir(),".xuanzhu")}function E(){return j.join(A(),"config.json")}function It(){return j.join(__dirname,"skills")}function Lt(){return j.join(A(),"skills")}function Ot(){if(vn())return{migrated:!1};let r=wn(),e=A();if(j.resolve(r)===j.resolve(e))return{migrated:!1};try{if(P.existsSync(e)||!P.existsSync(r))return{migrated:!1}}catch{return{migrated:!1}}try{return P.renameSync(r,e),{migrated:!0,from:r,to:e}}catch{}let n=`${e}.migrating-${process.pid}`;try{P.rmSync(n,{recursive:!0,force:!0}),P.cpSync(r,n,{recursive:!0}),P.renameSync(n,e)}catch{try{P.rmSync(n,{recursive:!0,force:!0})}catch{}return{migrated:!1}}try{P.rmSync(r,{recursive:!0,force:!0})}catch{}return{migrated:!0,from:r,to:e}}function xe(r){P.existsSync(r)||P.mkdirSync(r,{recursive:!0})}function z(r){return r&&(r==="~"?ye.homedir():r.startsWith("~/")||r.startsWith("~\\")?j.join(ye.homedir(),r.slice(2)):r)}var T=10,et="{{'\u7528\u6237\u53D1\u9001\u8FC7\u6765\u7684\u6D88\u606F'}}",Ut=`\u8BF7\u5206\u6790\u4E0B\u9762\u6D88\u606F\u7684\u610F\u56FE\uFF0C\u4EE5\u7B2C\u4E00\u4EBA\u79F0\u8F93\u51FA\u4E09\u6BB5\uFF1A\u3010\u6838\u5FC3\u610F\u56FE\u3011\u4E00\u53E5\u8BDD\u603B\u7ED3\uFF1B\u3010\u9700\u6C42\u62C6\u89E3\u3011\u9010\u6761\u5217\u51FA\u660E\u786E\u9700\u6C42\uFF1B\u3010\u9690\u6027\u610F\u56FE\u3011\u6F5C\u5728\u8BC9\u6C42\u3002\u4E0D\u8981\u589E\u52A0\u539F\u6587\u4E0D\u5B58\u5728\u4FE1\u606F\u3002 \u6D88\u606F\uFF1A ${et}`;function Kt(r,e){if(r.includes(et))return r.replace(et,()=>e);let n=/\{\{\s*[^{}]*\s*\}\}/;return n.test(r)?r.replace(n,()=>e):`${r}
3
3
  \u6D88\u606F\uFF1A${e}`}var Cn=1,H={version:Cn,provider:"deepseek",model:"deepseek-chat",models:[],intent:{enabled:!1},providers:{},maxToolRounds:200,autoApprove:!0,temperature:.2};function O(){let r=E();try{if(!D.existsSync(r))return{...H,provider:"",model:"",models:[]};let e=D.readFileSync(r,"utf8"),n=JSON.parse(e);return Sn(n)}catch(e){throw new Error(`\u8BFB\u53D6\u914D\u7F6E\u5931\u8D25\uFF08${r}\uFF09\uFF1A${e instanceof Error?e.message:String(e)}`)}}function Sn(r){let e=Array.isArray(r.models)?r.models.filter(n=>!!n&&typeof n.provider=="string"&&typeof n.model=="string").map(n=>({...n,id:n.id||ie(n.provider,n.model),weight:typeof n.weight=="number"&&Number.isFinite(n.weight)?n.weight:T})):H.models;return{...H,...r,models:e,intent:{enabled:r.intent?.enabled===!0,...r.intent?.prompt?{prompt:r.intent.prompt}:{}},providers:{...H.providers,...r.providers??{}},maxToolRounds:r.maxToolRounds??H.maxToolRounds,autoApprove:r.autoApprove??H.autoApprove,temperature:r.temperature??H.temperature}}function M(r){xe(A());let e=E(),n=JSON.stringify(r,null,2)+`
4
- `,s=`${e}.${process.pid}.tmp`;D.writeFileSync(s,n,{mode:384});try{D.chmodSync(s,384)}catch{}D.renameSync(s,e);try{D.chmodSync(e,384)}catch{}}function be(){return D.existsSync(E())}function Nt(r){return r?r.length<=8?"****":`${r.slice(0,4)}...${r.slice(-4)}`:"(\u672A\u8BBE\u7F6E)"}function Wt(r){let e={};for(let[n,s]of Object.entries(r.providers))e[n]={...s,apiKey:Nt(s.apiKey)};return{version:r.version,provider:r.provider,model:r.model,models:k(r).map(n=>({id:n.id,weight:n.weight,...n.baseUrl?{baseUrl:n.baseUrl}:{},...n.apiKey?{apiKey:Nt(n.apiKey)}:{}})),intent:{enabled:r.intent?.enabled===!0,...r.intent?.prompt?{prompt:r.intent.prompt}:{}},temperature:r.temperature,maxToolRounds:r.maxToolRounds,autoApprove:r.autoApprove,systemPromptExtra:r.systemPromptExtra??"",providers:e}}function ie(r,e){return`${r}/${e}`}function k(r){if(r.models&&r.models.length>0)return r.models.map(s=>({...s}));let e=r.provider,n=r.providers[e]?.model||r.model;return e&&n?[{id:ie(e,n),provider:e,model:n,weight:T}]:[]}function Tn(r){return k(r).filter(e=>e.weight>0).sort((e,n)=>n.weight-e.weight)}function ee(r,e){let n=new Set(e??[]);return Tn(r).find(s=>!n.has(s.id))??null}function te(r){return(!r.models||r.models.length===0)&&(r.models=k(r)),r.models}function Bt(r,e){let n=te(r),s=n.find(a=>a.id===e);if(!s)return null;s.weight=Math.max(0,s.weight-1),s.decayed=!0;let i=s.weight,o=n.filter(a=>a.decayed===!0);if(o.length>0&&o.every(a=>a.weight<=0)){for(let a of o)a.weight=T,a.decayed=!1;return{id:e,weight:0,reset:!0}}return{id:e,weight:i,reset:!1}}function Se(r){for(let e of te(r))e.weight=T,e.decayed=!1}function Dt(r){return typeof r=="number"&&Number.isFinite(r)&&r>0}function Ft(r,e){let n=te(r),s=ie(e.provider,e.model),i=typeof e.weight=="number"&&Number.isFinite(e.weight),o=n.find(l=>l.id===s);if(o)return i&&(o.weight=Math.max(0,Math.floor(e.weight)),o.decayed=!1),e.apiKey&&(o.apiKey=e.apiKey),e.baseUrl&&(o.baseUrl=e.baseUrl),Dt(e.contextWindow)&&(o.contextWindow=Math.floor(e.contextWindow)),o;let a={id:s,provider:e.provider,model:e.model,weight:i?Math.max(0,Math.floor(e.weight)):T,...e.apiKey?{apiKey:e.apiKey}:{},...e.baseUrl?{baseUrl:e.baseUrl}:{},...Dt(e.contextWindow)?{contextWindow:Math.floor(e.contextWindow)}:{}};return n.push(a),a}function zt(r,e){let n=r.findIndex(o=>o.id===e);if(n>=0)return n;let s=r.findIndex(o=>o.model===e);if(s>=0)return s;let i=Number.parseInt(e,10);return!Number.isNaN(i)&&i>=1&&i<=r.length?i-1:-1}function jt(r,e){let n=te(r),s=zt(n,e);if(s<0)return null;let[i]=n.splice(s,1);return i??null}function qt(r,e,n){let s=te(r),i=zt(s,e);return i<0?null:(s[i].weight=Math.max(0,Math.floor(n)),s[i].decayed=!1,s[i])}var Jt=x(require("@anthropic-ai/sdk"));var Gt=x(require("http")),Xt=x(require("https"));function Zt(r){let e=!/^http:\/\//i.test(r),n={keepAlive:!1};return e?new Xt.Agent(n):new Gt.Agent(n)}var Ht=new Set(["ECONNRESET","ECONNREFUSED","ECONNABORTED","ETIMEDOUT","EPIPE","ENOTFOUND","EAI_AGAIN","UND_ERR_SOCKET","UND_ERR_CONNECT_TIMEOUT","ERR_STREAM_PREMATURE_CLOSE"]),En=["premature close","socket hang up","other side closed","fetch failed","network error","connection error","connection reset","terminated","timeout","timed out"];function kn(r){return r&&r.replace(/\b(Bearer|Basic|Token)\s+[A-Za-z0-9._~+/=-]{6,}/gi,"$1 ***").replace(/\b(sk|pk|rk|api|key|token)[-_][A-Za-z0-9._-]{6,}/gi,"$1-***").replace(/([?&](?:api[-_]?key|key|token|access_token|authorization)=)[^&\s"']+/gi,"$1***").replace(/("(?:api[-_]?key|apikey|token|secret|password|authorization)"\s*:\s*")[^"]*/gi,"$1***")}function R(r){return typeof r=="string"?r:typeof r=="number"||typeof r=="boolean"?String(r):""}function Te(r){if(typeof r=="number"&&Number.isFinite(r))return r;if(typeof r=="string"){let e=Number.parseInt(r,10);if(!Number.isNaN(e))return e}}function _n(r){let e=r,n=R(e?.message);if(n)return n;let s=e?.error,i=R(s?.message);if(i)return i;let o=e?.cause,a=R(o?.message);if(a)return a;if(r instanceof Error&&r.message)return r.message;try{let c=JSON.stringify(r);if(c&&c!=="{}")return c}catch{}let l=String(r);return l==="[object Object]"?"\u672A\u77E5\u9519\u8BEF\uFF08\u65E0\u6CD5\u89E3\u6790\u9519\u8BEF\u8BE6\u60C5\uFF09":l}function oe(r){let e=r,n=Te(e?.status);if(n!==void 0){if(n===408||n===409||n===429||n>=500&&n<=599)return!0;if(n>=400&&n<500)return!1}let s=R(e?.code).toUpperCase();if(s&&Ht.has(s))return!0;let i=e?.cause,o=R(i?.code).toUpperCase();if(o&&Ht.has(o))return!0;let a=(R(e?.message)||R(i?.message)||String(r)).toLowerCase();return En.some(l=>a.includes(l))}function G(r){return kn(Mn(r))}function tt(r){let e=r,n=Te(e?.status);if(n===401||n===403||n===404)return!0;let s=(R(e?.message)||R(e?.error?.message)||R(e?.cause?.message)||String(r)).toLowerCase();return["invalid api key","incorrect api key","api key not valid","authentication","unauthorized","\u672A\u77E5 provider","unknown provider","model not found","unknown model","no such model","invalid base url","\u7F3A\u5C11 api key"].some(i=>s.includes(i))}function Ee(r){let e=r,n=Te(e?.status);if(n!==void 0&&(n<400||n>=500))return!1;let s=R(e?.code)||R(e?.error?.code);if(/context[_ ]?length|context[_ ]?size|max[_ ]?tokens?_exceeded|token[_ ]?limit/i.test(s))return!0;let i=(R(e?.message)||R(e?.error?.message)||R(e?.cause?.message)||String(r)).toLowerCase();return["context length","context_length","maximum context","context window","prompt is too long","exceed context limit","exceeds the maximum number of tokens","reduce the length","too many tokens","context size","context_size","exceeds the available context","input length exceeds","exceeds the context","n_ctx","n_keep","too long"].some(o=>i.includes(o))}function Mn(r){let e=r,n=_n(r),s=n.toLowerCase(),i=R(e?.code).toUpperCase(),o=Te(e?.status);if(Ee(r))return"\u4E0A\u4E0B\u6587\u8D85\u51FA\u6A21\u578B\u7A97\u53E3\uFF1A\u672C\u6B21\u8BF7\u6C42\u7684\u5185\u5BB9\u6BD4\u6A21\u578B\u80FD\u63A5\u53D7\u7684\u957F\u5EA6\u66F4\u957F\u3002\n\u7384\u732A\u5DF2\u81EA\u52A8\u6536\u655B\u5386\u53F2\u540E\u91CD\u8BD5\uFF1B\u82E5\u4ECD\u5931\u8D25\uFF0C\u53EF\u51CF\u5C11\u4E00\u6B21\u6027\u8BFB\u53D6\u7684\u5185\u5BB9\u91CF\uFF0C\u6216\u7528 `xzh model context <id> <\u5927\u5C0F>` \u8C03\u5927\u8BE5\u6A21\u578B\u7684\u4E0A\u4E0B\u6587\u7A97\u53E3\u3002";if(s.includes("premature close"))return`\u8FDE\u63A5\u88AB\u63D0\u524D\u5173\u95ED\uFF08Premature close\uFF09\uFF1A\u7F51\u7EDC\u6216\u4EE3\u7406\u4E2D\u65AD\u4E86\u54CD\u5E94\u6D41\u3002
4
+ `,s=`${e}.${process.pid}.tmp`;D.writeFileSync(s,n,{mode:384});try{D.chmodSync(s,384)}catch{}D.renameSync(s,e);try{D.chmodSync(e,384)}catch{}}function be(){return D.existsSync(E())}function Nt(r){return r?r.length<=8?"****":`${r.slice(0,4)}...${r.slice(-4)}`:"(\u672A\u8BBE\u7F6E)"}function Wt(r){let e={};for(let[n,s]of Object.entries(r.providers))e[n]={...s,apiKey:Nt(s.apiKey)};return{version:r.version,provider:r.provider,model:r.model,models:k(r).map(n=>({id:n.id,weight:n.weight,...n.baseUrl?{baseUrl:n.baseUrl}:{},...n.apiKey?{apiKey:Nt(n.apiKey)}:{}})),intent:{enabled:r.intent?.enabled===!0,...r.intent?.prompt?{prompt:r.intent.prompt}:{}},temperature:r.temperature,maxToolRounds:r.maxToolRounds,autoApprove:r.autoApprove,systemPromptExtra:r.systemPromptExtra??"",providers:e}}function ie(r,e){return`${r}/${e}`}function k(r){if(r.models&&r.models.length>0)return r.models.map(s=>({...s}));let e=r.provider,n=r.providers[e]?.model||r.model;return e&&n?[{id:ie(e,n),provider:e,model:n,weight:T}]:[]}function Tn(r){return k(r).filter(e=>e.weight>0).sort((e,n)=>n.weight-e.weight)}function ee(r,e){let n=new Set(e??[]);return Tn(r).find(s=>!n.has(s.id))??null}function te(r){return(!r.models||r.models.length===0)&&(r.models=k(r)),r.models}function Bt(r,e){let n=te(r),s=n.find(a=>a.id===e);if(!s)return null;s.weight=Math.max(0,s.weight-1),s.decayed=!0;let i=s.weight,o=n.filter(a=>a.decayed===!0);if(o.length>0&&o.every(a=>a.weight<=0)){for(let a of o)a.weight=T,a.decayed=!1;return{id:e,weight:0,reset:!0}}return{id:e,weight:i,reset:!1}}function Se(r){for(let e of te(r))e.weight=T,e.decayed=!1}function Dt(r){return typeof r=="number"&&Number.isFinite(r)&&r>0}function Ft(r,e){let n=te(r),s=ie(e.provider,e.model),i=typeof e.weight=="number"&&Number.isFinite(e.weight),o=n.find(l=>l.id===s);if(o)return i&&(o.weight=Math.max(0,Math.floor(e.weight)),o.decayed=!1),e.apiKey&&(o.apiKey=e.apiKey),e.baseUrl&&(o.baseUrl=e.baseUrl),Dt(e.contextWindow)&&(o.contextWindow=Math.floor(e.contextWindow)),o;let a={id:s,provider:e.provider,model:e.model,weight:i?Math.max(0,Math.floor(e.weight)):T,...e.apiKey?{apiKey:e.apiKey}:{},...e.baseUrl?{baseUrl:e.baseUrl}:{},...Dt(e.contextWindow)?{contextWindow:Math.floor(e.contextWindow)}:{}};return n.push(a),a}function jt(r,e){let n=r.findIndex(o=>o.id===e);if(n>=0)return n;let s=r.findIndex(o=>o.model===e);if(s>=0)return s;let i=Number.parseInt(e,10);return!Number.isNaN(i)&&i>=1&&i<=r.length?i-1:-1}function zt(r,e){let n=te(r),s=jt(n,e);if(s<0)return null;let[i]=n.splice(s,1);return i??null}function qt(r,e,n){let s=te(r),i=jt(s,e);return i<0?null:(s[i].weight=Math.max(0,Math.floor(n)),s[i].decayed=!1,s[i])}var Jt=x(require("@anthropic-ai/sdk"));var Gt=x(require("http")),Xt=x(require("https"));function Zt(r){let e=!/^http:\/\//i.test(r),n={keepAlive:!1};return e?new Xt.Agent(n):new Gt.Agent(n)}var Ht=new Set(["ECONNRESET","ECONNREFUSED","ECONNABORTED","ETIMEDOUT","EPIPE","ENOTFOUND","EAI_AGAIN","UND_ERR_SOCKET","UND_ERR_CONNECT_TIMEOUT","ERR_STREAM_PREMATURE_CLOSE"]),En=["premature close","socket hang up","other side closed","fetch failed","network error","connection error","connection reset","terminated","timeout","timed out"];function kn(r){return r&&r.replace(/\b(Bearer|Basic|Token)\s+[A-Za-z0-9._~+/=-]{6,}/gi,"$1 ***").replace(/\b(sk|pk|rk|api|key|token)[-_][A-Za-z0-9._-]{6,}/gi,"$1-***").replace(/([?&](?:api[-_]?key|key|token|access_token|authorization)=)[^&\s"']+/gi,"$1***").replace(/("(?:api[-_]?key|apikey|token|secret|password|authorization)"\s*:\s*")[^"]*/gi,"$1***")}function R(r){return typeof r=="string"?r:typeof r=="number"||typeof r=="boolean"?String(r):""}function Te(r){if(typeof r=="number"&&Number.isFinite(r))return r;if(typeof r=="string"){let e=Number.parseInt(r,10);if(!Number.isNaN(e))return e}}function _n(r){let e=r,n=R(e?.message);if(n)return n;let s=e?.error,i=R(s?.message);if(i)return i;let o=e?.cause,a=R(o?.message);if(a)return a;if(r instanceof Error&&r.message)return r.message;try{let c=JSON.stringify(r);if(c&&c!=="{}")return c}catch{}let l=String(r);return l==="[object Object]"?"\u672A\u77E5\u9519\u8BEF\uFF08\u65E0\u6CD5\u89E3\u6790\u9519\u8BEF\u8BE6\u60C5\uFF09":l}function oe(r){let e=r,n=Te(e?.status);if(n!==void 0){if(n===408||n===409||n===429||n>=500&&n<=599)return!0;if(n>=400&&n<500)return!1}let s=R(e?.code).toUpperCase();if(s&&Ht.has(s))return!0;let i=e?.cause,o=R(i?.code).toUpperCase();if(o&&Ht.has(o))return!0;let a=(R(e?.message)||R(i?.message)||String(r)).toLowerCase();return En.some(l=>a.includes(l))}function G(r){return kn(Mn(r))}function tt(r){let e=r,n=Te(e?.status);if(n===401||n===403||n===404)return!0;let s=(R(e?.message)||R(e?.error?.message)||R(e?.cause?.message)||String(r)).toLowerCase();return["invalid api key","incorrect api key","api key not valid","authentication","unauthorized","\u672A\u77E5 provider","unknown provider","model not found","unknown model","no such model","invalid base url","\u7F3A\u5C11 api key"].some(i=>s.includes(i))}function Ee(r){let e=r,n=Te(e?.status);if(n!==void 0&&(n<400||n>=500))return!1;let s=R(e?.code)||R(e?.error?.code);if(/context[_ ]?length|context[_ ]?size|max[_ ]?tokens?_exceeded|token[_ ]?limit/i.test(s))return!0;let i=(R(e?.message)||R(e?.error?.message)||R(e?.cause?.message)||String(r)).toLowerCase();return["context length","context_length","maximum context","context window","prompt is too long","exceed context limit","exceeds the maximum number of tokens","reduce the length","too many tokens","context size","context_size","exceeds the available context","input length exceeds","exceeds the context","n_ctx","n_keep","too long"].some(o=>i.includes(o))}function Mn(r){let e=r,n=_n(r),s=n.toLowerCase(),i=R(e?.code).toUpperCase(),o=Te(e?.status);if(Ee(r))return"\u4E0A\u4E0B\u6587\u8D85\u51FA\u6A21\u578B\u7A97\u53E3\uFF1A\u672C\u6B21\u8BF7\u6C42\u7684\u5185\u5BB9\u6BD4\u6A21\u578B\u80FD\u63A5\u53D7\u7684\u957F\u5EA6\u66F4\u957F\u3002\n\u7384\u732A\u5DF2\u81EA\u52A8\u6536\u655B\u5386\u53F2\u540E\u91CD\u8BD5\uFF1B\u82E5\u4ECD\u5931\u8D25\uFF0C\u53EF\u51CF\u5C11\u4E00\u6B21\u6027\u8BFB\u53D6\u7684\u5185\u5BB9\u91CF\uFF0C\u6216\u7528 `xzh model context <id> <\u5927\u5C0F>` \u8C03\u5927\u8BE5\u6A21\u578B\u7684\u4E0A\u4E0B\u6587\u7A97\u53E3\u3002";if(s.includes("premature close"))return`\u8FDE\u63A5\u88AB\u63D0\u524D\u5173\u95ED\uFF08Premature close\uFF09\uFF1A\u7F51\u7EDC\u6216\u4EE3\u7406\u4E2D\u65AD\u4E86\u54CD\u5E94\u6D41\u3002
5
5
  \u7384\u732A\u5DF2\u81EA\u52A8\u91CD\u8BD5\uFF1B\u82E5\u4ECD\u9891\u7E41\u51FA\u73B0\uFF0C\u901A\u5E38\u662F\u4EE3\u7406/\u7F51\u7EDC\u4E0D\u7A33\u5B9A\u5BFC\u81F4\uFF0C\u53EF\u68C0\u67E5\u4EE3\u7406\u6216\u7A0D\u540E\u518D\u8BD5\u3002`;if(i==="ECONNRESET"||s.includes("socket hang up"))return"\u8FDE\u63A5\u88AB\u5BF9\u7AEF\u91CD\u7F6E\uFF08ECONNRESET\uFF09\uFF1A\u7F51\u7EDC\u6216\u4EE3\u7406\u4E2D\u65AD\u4E86\u8FDE\u63A5\uFF0C\u8BF7\u91CD\u8BD5\u3002";if(i==="ETIMEDOUT"||s.includes("timed out"))return"\u8BF7\u6C42\u8D85\u65F6\uFF1A\u7F51\u7EDC\u8F83\u6162\u6216\u6A21\u578B\u670D\u52A1\u672A\u54CD\u5E94\uFF0C\u8BF7\u7A0D\u540E\u91CD\u8BD5\u3002";if(i==="ENOTFOUND"||i==="EAI_AGAIN")return"\u65E0\u6CD5\u89E3\u6790\u670D\u52A1\u5730\u5740\uFF1A\u8BF7\u68C0\u67E5\u7F51\u7EDC\u8FDE\u63A5\u4E0E\u4EE3\u7406\u8BBE\u7F6E\uFF08DNS \u53EF\u80FD\u88AB\u62E6\u622A\uFF09\u3002";if(i==="ECONNREFUSED")return"\u8FDE\u63A5\u88AB\u62D2\u7EDD\uFF1A\u76EE\u6807\u5730\u5740\u6216\u7AEF\u53E3\u4E0D\u53EF\u8FBE\uFF0C\u8BF7\u68C0\u67E5 baseUrl \u4E0E\u4EE3\u7406\u8BBE\u7F6E\u3002";if(o===401||s.includes("unauthorized")||s.includes("invalid api key")||s.includes("authentication"))return"\u8BA4\u8BC1\u5931\u8D25\uFF1AAPI Key \u65E0\u6548\u6216\u5DF2\u8FC7\u671F\uFF0C\u8BF7\u8FD0\u884C `xzh model` \u91CD\u65B0\u914D\u7F6E\u3002";if(o===403)return"\u8BBF\u95EE\u88AB\u62D2\u7EDD\uFF08403\uFF09\uFF1A\u8BF7\u68C0\u67E5 API Key \u6743\u9650\u6216\u670D\u52A1\u5546\u533A\u57DF\u9650\u5236\u3002";if(o===402||s.includes("insufficient")||s.includes("balance"))return"\u8D26\u6237\u989D\u5EA6\u4E0D\u8DB3\uFF1A\u8BF7\u68C0\u67E5\u670D\u52A1\u5546\u4F59\u989D\u6216\u914D\u989D\u3002";if(o===429||s.includes("rate limit"))return"\u8BF7\u6C42\u8FC7\u4E8E\u9891\u7E41\uFF08\u9650\u6D41\uFF09\uFF1A\u8BF7\u7A0D\u540E\u518D\u8BD5\u3002";if(/out\s*of\s*(device\s*)?memory|outofdevicememory|outofmemory|vk::|cuda.*error|hip.*error|cannot allocate|insufficient memory/i.test(n))return`${n}
6
6
  \u4EE5\u4E0A\u662F\u6A21\u578B\u670D\u52A1\u7AEF\uFF08llama.cpp / LM Studio \u7B49\uFF09\u62A5\u51FA\u7684\u8D44\u6E90\u9519\u8BEF\uFF1A\u663E\u5B58\u6216\u5185\u5B58\u4E0D\u8DB3\uFF0C\u4E0E\u7384\u732A\u65E0\u5173\u3002
7
7
  \u53EF\u5C1D\u8BD5\uFF1A\u51CF\u5C0F\u4E0A\u4E0B\u6587\u957F\u5EA6\uFF08llama.cpp \u7684 -c\uFF0C\u4F8B\u5982 -c 8192\uFF09\u3001\u964D\u4F4E KV cache \u7CBE\u5EA6
@@ -17,11 +17,11 @@
17
17
 
18
18
  \u2026\uFF08\u6B64\u5904\u7701\u7565 ${d} \u5B57\u7B26\u7684\u4E2D\u95F4\u5185\u5BB9\uFF09
19
19
 
20
- ${u}`}})}function Pe(r,e){let n=Qt(r),s=n.length,i=0;for(let a=n.length-1;a>=0;a--){let l=Re(n[a]);if(s<n.length&&i+l>e)break;i+=l,s=a}let o=n.slice(s);for(;o.length>0&&o[0].role==="tool";)o.shift();if(o.length===0){for(let a=r.length-1;a>=0;a--)if(r[a].role==="user"){o=[r[a]];break}}if(o.length===1&&Re(o[0])>e){let l=Fn(o[0],e);if(l===null)return[];o[0]=l}return o}function lt(r,e,n){let s=er(r,e),i=re(n)+4,o=s-i;return Math.max(1,Math.min(Math.max(o,1e3),s))}function er(r,e){let n=r&&r>0?r:ue(e);return Math.floor(n*.8)}function ct(r,e,n){let s=er(r,e);return re(n)+4>=s*.5}var ot={"gpt-4o":128e3,"gpt-4o-mini":128e3,"gpt-4-turbo":128e3,"gpt-4.1":1e6,"gpt-4":8192,"gpt-3.5-turbo":16385,o1:2e5,"o1-mini":128e3,"o3-mini":2e5,"claude-sonnet-4-5":2e5,"claude-opus-4-1":2e5,"claude-3-5-haiku-latest":2e5,"claude-3-5-sonnet-latest":2e5,"deepseek-chat":64e3,"deepseek-reasoner":64e3,"gemini-2.0-flash":1e6,"gemini-2.5-pro":1e6,"gemini-2.5-flash":1e6,"glm-4-plus":128e3,"glm-4-air":128e3,"glm-4-flash":128e3,"qwen-max":32e3,"qwen-plus":128e3,"qwen2.5-coder-32b-instruct":32e3,"llama-3.3-70b-versatile":128e3,"qwen2.5-coder":32e3,"llama3.1":128e3,"deepseek-coder-v2":128e3},zn=[[/claude/i,2e5],[/gemini/i,1e6],[/deepseek/i,64e3],[/gpt-4\.1|gpt-4o|gpt-4-turbo/i,128e3],[/\bo[13]\b/i,128e3],[/gpt-4\b/i,8192],[/gpt-3\.5/i,16385],[/qwen|glm|yi-|moonshot|kimi/i,128e3],[/llama|mistral|mixtral|gemma|phi-|command-r/i,128e3]];function ue(r){let e=r.trim();if(!e)return 32e3;let n=e.toLowerCase(),s=ot[n];if(s)return s;let i=n.replace(/[-_]?(latest|preview|stable|beta|exp)$/,"").replace(/[-_]\d{6,8}$/,"").replace(/[:@].*$/,""),o=ot[i]??Object.entries(ot).find(([a])=>i.startsWith(a)||i===a)?.[1];if(o)return o;for(let[a,l]of zn)if(a.test(i))return l;return 32e3}function ne(r){return!Number.isFinite(r)||r<=0?"\u672A\u8BBE\u7F6E":String(Math.floor(r))}var Y=[{id:"deepseek",label:"DeepSeek \u6DF1\u5EA6\u6C42\u7D22",kind:"openai",defaultBaseUrl:"https://api.deepseek.com/v1",defaultModel:"deepseek-chat",models:["deepseek-chat","deepseek-reasoner"],requiresApiKey:!0,envKey:"DEEPSEEK_API_KEY",apiKeyHint:"sk-..."},{id:"anthropic",label:"Anthropic Claude",kind:"anthropic",defaultModel:"claude-sonnet-4-5",models:["claude-sonnet-4-5","claude-opus-4-1","claude-3-5-haiku-latest"],requiresApiKey:!0,envKey:"ANTHROPIC_API_KEY",apiKeyHint:"sk-ant-..."},{id:"openai",label:"OpenAI",kind:"openai",defaultBaseUrl:"https://api.openai.com/v1",defaultModel:"gpt-4o",models:["gpt-4o","gpt-4o-mini","o4-mini"],requiresApiKey:!0,envKey:"OPENAI_API_KEY",apiKeyHint:"sk-..."},{id:"gemini",label:"Google Gemini",kind:"gemini",defaultModel:"gemini-2.0-flash",models:["gemini-2.0-flash","gemini-2.5-pro","gemini-2.5-flash"],requiresApiKey:!0,envKey:"GEMINI_API_KEY",apiKeyHint:"AIza..."},{id:"glm",label:"\u667A\u8C31 GLM",kind:"openai",defaultBaseUrl:"https://open.bigmodel.cn/api/paas/v4",defaultModel:"glm-4-plus",models:["glm-4-plus","glm-4-air","glm-4-flash"],requiresApiKey:!0,envKey:"GLM_API_KEY"},{id:"qwen",label:"\u901A\u4E49\u5343\u95EE Qwen",kind:"openai",defaultBaseUrl:"https://dashscope.aliyuncs.com/compatible-mode/v1",defaultModel:"qwen-max",models:["qwen-max","qwen-plus","qwen2.5-coder-32b-instruct"],requiresApiKey:!0,envKey:"DASHSCOPE_API_KEY"},{id:"groq",label:"Groq",kind:"openai",defaultBaseUrl:"https://api.groq.com/openai/v1",defaultModel:"llama-3.3-70b-versatile",models:["llama-3.3-70b-versatile","qwen-2.5-coder-32b"],requiresApiKey:!0,envKey:"GROQ_API_KEY",apiKeyHint:"gsk_..."},{id:"ollama",label:"Ollama\uFF08\u672C\u5730\uFF09",kind:"openai",defaultBaseUrl:"http://localhost:11434/v1",defaultModel:"qwen2.5-coder",models:["qwen2.5-coder","llama3.1","deepseek-coder-v2"],requiresApiKey:!1,envKey:"OLLAMA_API_KEY"},{id:"custom",label:"\u81EA\u5B9A\u4E49\uFF08OpenAI \u517C\u5BB9\u7AEF\u70B9\uFF09",kind:"openai",defaultModel:"",models:[],requiresApiKey:!1,requiresBaseUrl:!0,envKey:"XUANZHU_API_KEY"}];function J(r){return Y.find(e=>e.id===r)}function Ae(r,e){return e||(r.envKey?process.env[r.envKey]??"":"")}function rr(r,e){return e||r.defaultBaseUrl}function ut(r,e){let n=[],s=J(e.provider);if(!s)return[`\u6A21\u578B ${e.id} \u4F7F\u7528\u4E86\u672A\u77E5 provider\uFF1A${e.provider}`];let i=r.providers[e.provider]??{};return s.requiresApiKey&&!Ae(s,e.apiKey??i.apiKey)&&n.push(`\u6A21\u578B ${e.id} \u7F3A\u5C11 API Key\uFF0C\u8BF7\u8FD0\u884C \`xzh model\` \u6216\u8BBE\u7F6E\u73AF\u5883\u53D8\u91CF ${s.envKey}`),s.requiresBaseUrl&&!rr(s,e.baseUrl??i.baseUrl)&&n.push(`\u6A21\u578B ${e.id} \u9700\u8981\u914D\u7F6E baseUrl`),e.model||n.push(`\u6A21\u578B ${e.id} \u672A\u6307\u5B9A\u6A21\u578B\u540D`),n}function nr(r){let e=k(r);return e.length===0?["\u5C1A\u672A\u914D\u7F6E\u4EFB\u4F55\u6A21\u578B\uFF0C\u8BF7\u8FD0\u884C `xzh model add` \u6DFB\u52A0"]:e.flatMap(n=>ut(r,n))}function Ie(r,e){let n=e?.provider??r.provider,s=J(n);if(!s)throw new Error(`\u672A\u77E5\u7684 provider\uFF1A${n}`);let i=r.providers[n]??{},o=Ae(s,e?.apiKey??i.apiKey),a=rr(s,e?.baseUrl??i.baseUrl),l=e?.model||i.model||r.model||s.defaultModel;if(s.requiresBaseUrl&&!a)throw new Error(`provider "${s.label}" \u9700\u8981\u914D\u7F6E API \u7AEF\u70B9 baseUrl\u3002\u8FD0\u884C \`xzh model\` \u8865\u9F50\u540E\u518D\u8BD5\u3002`);if(s.requiresApiKey&&!o)throw new Error(`provider "${s.label}" \u7F3A\u5C11 API Key\u3002\u8FD0\u884C \`xzh model\` \u8FDB\u884C\u914D\u7F6E\uFF0C\u6216\u8BBE\u7F6E\u73AF\u5883\u53D8\u91CF ${s.envKey}\u3002`);if(!l)throw new Error(`provider "${s.label}" \u672A\u6307\u5B9A\u6A21\u578B\u3002\u8FD0\u884C \`xzh model\` \u9009\u62E9\u6A21\u578B\u3002`);switch(s.kind){case"anthropic":return new ke({id:s.id,apiKey:o,baseUrl:a,model:l});case"gemini":return new _e({id:s.id,apiKey:o,baseUrl:a,model:l});case"openai":default:return new Me({id:s.id,apiKey:o,baseUrl:a,model:l})}}var ge=x(require("fs")),Xe=x(require("path"));var sr=require("child_process"),Le=x(require("path"));var dt=3e4,jn=12e4,qn=6e5,Hn=1500;function Gn(r,e,n,s){return new Promise(i=>{if(s?.aborted){i({stdout:"",stderr:"\u547D\u4EE4\u5DF2\u88AB\u4E2D\u65AD\uFF08\u5728\u6267\u884C\u524D\uFF09\u3002",code:null,timedOut:!1,aborted:!0});return}let o=process.platform==="win32",a=o?"cmd.exe":process.env.SHELL||"bash",c=(0,sr.spawn)(a,o?["/d","/s","/c",r]:["-lc",r],{cwd:e,env:process.env,windowsHide:!0,detached:!o}),u="",d="",h=!1,m=!1,p=!1,f,g=C=>{let F=c.pid;if(F!==void 0)try{process.kill(-F,C)}catch{try{c.kill(C)}catch{}}},$=C=>{p||(p=!0,clearTimeout(_),f&&clearTimeout(f),s?.removeEventListener("abort",y),i({stdout:u,stderr:d,code:C,timedOut:h,aborted:m}))},b=()=>{f&&clearTimeout(f),f=setTimeout(()=>$(null),Hn)},y=()=>{m=!0,g("SIGKILL"),b()},_=setTimeout(()=>{h=!0,g("SIGKILL"),b()},n);s?.addEventListener("abort",y,{once:!0}),c.stdout?.on("data",C=>{u.length<dt&&(u+=C.toString())}),c.stderr?.on("data",C=>{d.length<dt&&(d+=C.toString())}),c.on("error",C=>{d+=String(C),$(-1)}),c.on("exit",C=>{f&&clearTimeout(f),f=setTimeout(()=>$(C),120)}),c.on("close",C=>$(C))})}var ir={name:"bash",description:"\u5728\u7EC8\u7AEF\u4E2D\u6267\u884C shell \u547D\u4EE4\u5E76\u8FD4\u56DE\u8F93\u51FA\u3002\u9002\u7528\u4E8E\u8FD0\u884C\u6D4B\u8BD5\u3001\u6784\u5EFA\u3001\u5B89\u88C5\u4F9D\u8D56\u3001git \u64CD\u4F5C\u7B49\u3002\u547D\u4EE4\u5728\u975E\u4EA4\u4E92\u5F0F shell \u4E2D\u6267\u884C\uFF0C\u957F\u65F6\u95F4\u8FD0\u884C\u7684\u547D\u4EE4\u8BF7\u8BBE\u7F6E timeout\u3002",requiresConfirmation:!0,danger:!0,parameters:{type:"object",properties:{command:{type:"string",description:"\u8981\u6267\u884C\u7684 shell \u547D\u4EE4"},timeout:{type:"integer",description:"\u8D85\u65F6\u65F6\u95F4\uFF08\u6BEB\u79D2\uFF0C\u9ED8\u8BA4 120000\uFF0C\u6700\u5927 600000\uFF09"},cwd:{type:"string",description:"\u547D\u4EE4\u6267\u884C\u76EE\u5F55\uFF08\u53EF\u9009\uFF0C\u9ED8\u8BA4\u5F53\u524D\u5DE5\u4F5C\u76EE\u5F55\uFF09"}},required:["command"]},async execute(r,e){let n=typeof r.command=="string"?r.command:"";if(!n.trim())return{ok:!1,content:"\u9519\u8BEF\uFF1Acommand \u4E0D\u80FD\u4E3A\u7A7A"};let s=e.cwd;if(typeof r.cwd=="string"&&r.cwd){let u=j(r.cwd);s=Le.isAbsolute(u)?u:Le.resolve(e.cwd,u)}let i=typeof r.timeout=="number"?r.timeout:jn,o=Math.min(Math.max(1e3,i),qn),a=await Gn(n,s,o,e.signal),l=[];a.stdout.trim()&&l.push(a.stdout.trimEnd()),a.stderr.trim()&&l.push(`[stderr]
20
+ ${u}`}})}function Pe(r,e){let n=Qt(r),s=n.length,i=0;for(let a=n.length-1;a>=0;a--){let l=Re(n[a]);if(s<n.length&&i+l>e)break;i+=l,s=a}let o=n.slice(s);for(;o.length>0&&o[0].role==="tool";)o.shift();if(o.length===0){for(let a=r.length-1;a>=0;a--)if(r[a].role==="user"){o=[r[a]];break}}if(o.length===1&&Re(o[0])>e){let l=Fn(o[0],e);if(l===null)return[];o[0]=l}return o}function lt(r,e,n){let s=er(r,e),i=re(n)+4,o=s-i;return Math.max(1,Math.min(Math.max(o,1e3),s))}function er(r,e){let n=r&&r>0?r:ue(e);return Math.floor(n*.8)}function ct(r,e,n){let s=er(r,e);return re(n)+4>=s*.5}var ot={"gpt-4o":128e3,"gpt-4o-mini":128e3,"gpt-4-turbo":128e3,"gpt-4.1":1e6,"gpt-4":8192,"gpt-3.5-turbo":16385,o1:2e5,"o1-mini":128e3,"o3-mini":2e5,"claude-sonnet-4-5":2e5,"claude-opus-4-1":2e5,"claude-3-5-haiku-latest":2e5,"claude-3-5-sonnet-latest":2e5,"deepseek-chat":64e3,"deepseek-reasoner":64e3,"gemini-2.0-flash":1e6,"gemini-2.5-pro":1e6,"gemini-2.5-flash":1e6,"glm-4-plus":128e3,"glm-4-air":128e3,"glm-4-flash":128e3,"qwen-max":32e3,"qwen-plus":128e3,"qwen2.5-coder-32b-instruct":32e3,"llama-3.3-70b-versatile":128e3,"qwen2.5-coder":32e3,"llama3.1":128e3,"deepseek-coder-v2":128e3},jn=[[/claude/i,2e5],[/gemini/i,1e6],[/deepseek/i,64e3],[/gpt-4\.1|gpt-4o|gpt-4-turbo/i,128e3],[/\bo[13]\b/i,128e3],[/gpt-4\b/i,8192],[/gpt-3\.5/i,16385],[/qwen|glm|yi-|moonshot|kimi/i,128e3],[/llama|mistral|mixtral|gemma|phi-|command-r/i,128e3]];function ue(r){let e=r.trim();if(!e)return 32e3;let n=e.toLowerCase(),s=ot[n];if(s)return s;let i=n.replace(/[-_]?(latest|preview|stable|beta|exp)$/,"").replace(/[-_]\d{6,8}$/,"").replace(/[:@].*$/,""),o=ot[i]??Object.entries(ot).find(([a])=>i.startsWith(a)||i===a)?.[1];if(o)return o;for(let[a,l]of jn)if(a.test(i))return l;return 32e3}function ne(r){return!Number.isFinite(r)||r<=0?"\u672A\u8BBE\u7F6E":String(Math.floor(r))}var Y=[{id:"deepseek",label:"DeepSeek \u6DF1\u5EA6\u6C42\u7D22",kind:"openai",defaultBaseUrl:"https://api.deepseek.com/v1",defaultModel:"deepseek-chat",models:["deepseek-chat","deepseek-reasoner"],requiresApiKey:!0,envKey:"DEEPSEEK_API_KEY",apiKeyHint:"sk-..."},{id:"anthropic",label:"Anthropic Claude",kind:"anthropic",defaultModel:"claude-sonnet-4-5",models:["claude-sonnet-4-5","claude-opus-4-1","claude-3-5-haiku-latest"],requiresApiKey:!0,envKey:"ANTHROPIC_API_KEY",apiKeyHint:"sk-ant-..."},{id:"openai",label:"OpenAI",kind:"openai",defaultBaseUrl:"https://api.openai.com/v1",defaultModel:"gpt-4o",models:["gpt-4o","gpt-4o-mini","o4-mini"],requiresApiKey:!0,envKey:"OPENAI_API_KEY",apiKeyHint:"sk-..."},{id:"gemini",label:"Google Gemini",kind:"gemini",defaultModel:"gemini-2.0-flash",models:["gemini-2.0-flash","gemini-2.5-pro","gemini-2.5-flash"],requiresApiKey:!0,envKey:"GEMINI_API_KEY",apiKeyHint:"AIza..."},{id:"glm",label:"\u667A\u8C31 GLM",kind:"openai",defaultBaseUrl:"https://open.bigmodel.cn/api/paas/v4",defaultModel:"glm-4-plus",models:["glm-4-plus","glm-4-air","glm-4-flash"],requiresApiKey:!0,envKey:"GLM_API_KEY"},{id:"qwen",label:"\u901A\u4E49\u5343\u95EE Qwen",kind:"openai",defaultBaseUrl:"https://dashscope.aliyuncs.com/compatible-mode/v1",defaultModel:"qwen-max",models:["qwen-max","qwen-plus","qwen2.5-coder-32b-instruct"],requiresApiKey:!0,envKey:"DASHSCOPE_API_KEY"},{id:"groq",label:"Groq",kind:"openai",defaultBaseUrl:"https://api.groq.com/openai/v1",defaultModel:"llama-3.3-70b-versatile",models:["llama-3.3-70b-versatile","qwen-2.5-coder-32b"],requiresApiKey:!0,envKey:"GROQ_API_KEY",apiKeyHint:"gsk_..."},{id:"ollama",label:"Ollama\uFF08\u672C\u5730\uFF09",kind:"openai",defaultBaseUrl:"http://localhost:11434/v1",defaultModel:"qwen2.5-coder",models:["qwen2.5-coder","llama3.1","deepseek-coder-v2"],requiresApiKey:!1,envKey:"OLLAMA_API_KEY"},{id:"custom",label:"\u81EA\u5B9A\u4E49\uFF08OpenAI \u517C\u5BB9\u7AEF\u70B9\uFF09",kind:"openai",defaultModel:"",models:[],requiresApiKey:!1,requiresBaseUrl:!0,envKey:"XUANZHU_API_KEY"}];function J(r){return Y.find(e=>e.id===r)}function Ae(r,e){return e||(r.envKey?process.env[r.envKey]??"":"")}function rr(r,e){return e||r.defaultBaseUrl}function ut(r,e){let n=[],s=J(e.provider);if(!s)return[`\u6A21\u578B ${e.id} \u4F7F\u7528\u4E86\u672A\u77E5 provider\uFF1A${e.provider}`];let i=r.providers[e.provider]??{};return s.requiresApiKey&&!Ae(s,e.apiKey??i.apiKey)&&n.push(`\u6A21\u578B ${e.id} \u7F3A\u5C11 API Key\uFF0C\u8BF7\u8FD0\u884C \`xzh model\` \u6216\u8BBE\u7F6E\u73AF\u5883\u53D8\u91CF ${s.envKey}`),s.requiresBaseUrl&&!rr(s,e.baseUrl??i.baseUrl)&&n.push(`\u6A21\u578B ${e.id} \u9700\u8981\u914D\u7F6E baseUrl`),e.model||n.push(`\u6A21\u578B ${e.id} \u672A\u6307\u5B9A\u6A21\u578B\u540D`),n}function nr(r){let e=k(r);return e.length===0?["\u5C1A\u672A\u914D\u7F6E\u4EFB\u4F55\u6A21\u578B\uFF0C\u8BF7\u8FD0\u884C `xzh model add` \u6DFB\u52A0"]:e.flatMap(n=>ut(r,n))}function Ie(r,e){let n=e?.provider??r.provider,s=J(n);if(!s)throw new Error(`\u672A\u77E5\u7684 provider\uFF1A${n}`);let i=r.providers[n]??{},o=Ae(s,e?.apiKey??i.apiKey),a=rr(s,e?.baseUrl??i.baseUrl),l=e?.model||i.model||r.model||s.defaultModel;if(s.requiresBaseUrl&&!a)throw new Error(`provider "${s.label}" \u9700\u8981\u914D\u7F6E API \u7AEF\u70B9 baseUrl\u3002\u8FD0\u884C \`xzh model\` \u8865\u9F50\u540E\u518D\u8BD5\u3002`);if(s.requiresApiKey&&!o)throw new Error(`provider "${s.label}" \u7F3A\u5C11 API Key\u3002\u8FD0\u884C \`xzh model\` \u8FDB\u884C\u914D\u7F6E\uFF0C\u6216\u8BBE\u7F6E\u73AF\u5883\u53D8\u91CF ${s.envKey}\u3002`);if(!l)throw new Error(`provider "${s.label}" \u672A\u6307\u5B9A\u6A21\u578B\u3002\u8FD0\u884C \`xzh model\` \u9009\u62E9\u6A21\u578B\u3002`);switch(s.kind){case"anthropic":return new ke({id:s.id,apiKey:o,baseUrl:a,model:l});case"gemini":return new _e({id:s.id,apiKey:o,baseUrl:a,model:l});case"openai":default:return new Me({id:s.id,apiKey:o,baseUrl:a,model:l})}}var ge=x(require("fs")),Xe=x(require("path"));var sr=require("child_process"),Le=x(require("path"));var dt=3e4,zn=12e4,qn=6e5,Hn=1500;function Gn(r,e,n,s){return new Promise(i=>{if(s?.aborted){i({stdout:"",stderr:"\u547D\u4EE4\u5DF2\u88AB\u4E2D\u65AD\uFF08\u5728\u6267\u884C\u524D\uFF09\u3002",code:null,timedOut:!1,aborted:!0});return}let o=process.platform==="win32",a=o?"cmd.exe":process.env.SHELL||"bash",c=(0,sr.spawn)(a,o?["/d","/s","/c",r]:["-lc",r],{cwd:e,env:process.env,windowsHide:!0,detached:!o}),u="",d="",h=!1,m=!1,p=!1,f,g=C=>{let F=c.pid;if(F!==void 0)try{process.kill(-F,C)}catch{try{c.kill(C)}catch{}}},$=C=>{p||(p=!0,clearTimeout(_),f&&clearTimeout(f),s?.removeEventListener("abort",y),i({stdout:u,stderr:d,code:C,timedOut:h,aborted:m}))},b=()=>{f&&clearTimeout(f),f=setTimeout(()=>$(null),Hn)},y=()=>{m=!0,g("SIGKILL"),b()},_=setTimeout(()=>{h=!0,g("SIGKILL"),b()},n);s?.addEventListener("abort",y,{once:!0}),c.stdout?.on("data",C=>{u.length<dt&&(u+=C.toString())}),c.stderr?.on("data",C=>{d.length<dt&&(d+=C.toString())}),c.on("error",C=>{d+=String(C),$(-1)}),c.on("exit",C=>{f&&clearTimeout(f),f=setTimeout(()=>$(C),120)}),c.on("close",C=>$(C))})}var ir={name:"bash",description:"\u5728\u7EC8\u7AEF\u4E2D\u6267\u884C shell \u547D\u4EE4\u5E76\u8FD4\u56DE\u8F93\u51FA\u3002\u9002\u7528\u4E8E\u8FD0\u884C\u6D4B\u8BD5\u3001\u6784\u5EFA\u3001\u5B89\u88C5\u4F9D\u8D56\u3001git \u64CD\u4F5C\u7B49\u3002\u547D\u4EE4\u5728\u975E\u4EA4\u4E92\u5F0F shell \u4E2D\u6267\u884C\uFF0C\u957F\u65F6\u95F4\u8FD0\u884C\u7684\u547D\u4EE4\u8BF7\u8BBE\u7F6E timeout\u3002",requiresConfirmation:!0,danger:!0,parameters:{type:"object",properties:{command:{type:"string",description:"\u8981\u6267\u884C\u7684 shell \u547D\u4EE4"},timeout:{type:"integer",description:"\u8D85\u65F6\u65F6\u95F4\uFF08\u6BEB\u79D2\uFF0C\u9ED8\u8BA4 120000\uFF0C\u6700\u5927 600000\uFF09"},cwd:{type:"string",description:"\u547D\u4EE4\u6267\u884C\u76EE\u5F55\uFF08\u53EF\u9009\uFF0C\u9ED8\u8BA4\u5F53\u524D\u5DE5\u4F5C\u76EE\u5F55\uFF09"}},required:["command"]},async execute(r,e){let n=typeof r.command=="string"?r.command:"";if(!n.trim())return{ok:!1,content:"\u9519\u8BEF\uFF1Acommand \u4E0D\u80FD\u4E3A\u7A7A"};let s=e.cwd;if(typeof r.cwd=="string"&&r.cwd){let u=z(r.cwd);s=Le.isAbsolute(u)?u:Le.resolve(e.cwd,u)}let i=typeof r.timeout=="number"?r.timeout:zn,o=Math.min(Math.max(1e3,i),qn),a=await Gn(n,s,o,e.signal),l=[];a.stdout.trim()&&l.push(a.stdout.trimEnd()),a.stderr.trim()&&l.push(`[stderr]
21
21
  ${a.stderr.trimEnd()}`),a.aborted&&l.push("[\u4E2D\u65AD] \u547D\u4EE4\u5DF2\u88AB\u7528\u6237\u4E2D\u65AD\uFF0C\u5176\u8FDB\u7A0B\u7EC4\u5DF2\u7EC8\u6B62\u3002"),a.timedOut&&l.push(`[\u8B66\u544A] \u547D\u4EE4\u5728 ${o}ms \u540E\u8D85\u65F6\u5E76\u88AB\u7EC8\u6B62\u3002`),l.push(`[\u9000\u51FA\u7801] ${a.code??"\u672A\u77E5"}`);let c=l.join(`
22
22
 
23
23
  `);return{ok:a.code===0&&!a.timedOut&&!a.aborted,content:c.slice(0,dt),summary:a.aborted?"\u5DF2\u4E2D\u65AD":a.timedOut?"\u8D85\u65F6":a.code===0?"\u6267\u884C\u6210\u529F":`\u9000\u51FA\u7801 ${a.code}`}}};var v=x(require("fs")),S=x(require("path"));var or=2e3,Xn=2*1024*1024,ht=200,Oe=200,Zn=1024*1024,ar=2e3;function Yn(r){return/\((?:[^()\\]|\\.)*[+*](?:[^()\\]|\\.)*\)\s*[+*{]/.test(r)}var ur=new Set([".git","node_modules","dist","build","out",".next",".nuxt",".cache","coverage",".venv","venv","__pycache__",".idea",".vscode-test"]);function Jn(r){try{let e=S.join(r,".gitignore");return v.existsSync(e)?v.readFileSync(e,"utf8").split(`
24
- `).map(n=>n.trim()).filter(n=>n&&!n.startsWith("#")&&!n.startsWith("!")):[]}catch{return[]}}function Vn(r,e){let n=r.split(S.sep).join("/"),s=S.basename(n);for(let i of e){let o=i.replace(/^\//,"").replace(/\/$/,"");if(o){if(o.includes("*")){try{if(Ne(o).test(n)||Ne(o).test(s))return!0}catch{}continue}if(n===o||n.startsWith(`${o}/`)||s===o)return!0}}return!1}function de(r,e){let n=j(r);return S.isAbsolute(n)?n:S.resolve(e,n)}function U(r,e=""){return typeof r=="string"?r:e}function lr(r,e){return typeof r=="number"&&Number.isFinite(r)?r:e}function dr(r){return r.subarray(0,8e3).includes(0)}var hr={name:"read_file",description:"\u8BFB\u53D6\u6587\u672C\u6587\u4EF6\u5185\u5BB9\uFF0C\u8FD4\u56DE\u5E26\u884C\u53F7\u7684\u6587\u672C\u3002\u652F\u6301\u901A\u8FC7 offset/limit \u8BFB\u53D6\u6307\u5B9A\u884C\u8303\u56F4\u3002\u5728\u4FEE\u6539\u6587\u4EF6\u524D\u5E94\u5148\u8BFB\u53D6\u786E\u8BA4\u5185\u5BB9\u3002",parameters:{type:"object",properties:{path:{type:"string",description:"\u6587\u4EF6\u8DEF\u5F84\uFF08\u7EDD\u5BF9\u6216\u76F8\u5BF9\u5F53\u524D\u76EE\u5F55\uFF09"},offset:{type:"integer",description:"\u8D77\u59CB\u884C\u53F7\uFF08\u4ECE 1 \u5F00\u59CB\uFF0C\u53EF\u9009\uFF09"},limit:{type:"integer",description:`\u6700\u591A\u8BFB\u53D6\u7684\u884C\u6570\uFF08\u9ED8\u8BA4 ${or}\uFF09`}},required:["path"]},async execute(r,e){let n=de(U(r.path),e.cwd);if(!v.existsSync(n))return{ok:!1,content:`\u9519\u8BEF\uFF1A\u6587\u4EF6\u4E0D\u5B58\u5728\uFF1A${n}`};let s=v.statSync(n);if(s.isDirectory())return{ok:!1,content:`\u9519\u8BEF\uFF1A${n} \u662F\u4E00\u4E2A\u76EE\u5F55\uFF0C\u8BF7\u4F7F\u7528 list_dir`};if(!s.isFile())return{ok:!1,content:`\u9519\u8BEF\uFF1A${n} \u4E0D\u662F\u666E\u901A\u6587\u4EF6\uFF08\u8BBE\u5907\u6587\u4EF6 / \u7BA1\u9053 / socket\uFF09\uFF0C\u62D2\u7EDD\u8BFB\u53D6`};if(s.size>Xn)return{ok:!1,content:`\u9519\u8BEF\uFF1A\u6587\u4EF6\u8FC7\u5927\uFF08${(s.size/1024/1024).toFixed(1)}MB\uFF09\uFF0C\u8D85\u51FA 2MB \u9650\u5236`};let i=v.readFileSync(n);if(dr(i))return{ok:!1,content:`\u9519\u8BEF\uFF1A${n} \u662F\u4E8C\u8FDB\u5236\u6587\u4EF6\uFF0C\u65E0\u6CD5\u4F5C\u4E3A\u6587\u672C\u8BFB\u53D6`};let o=i.toString("utf8").split(`
24
+ `).map(n=>n.trim()).filter(n=>n&&!n.startsWith("#")&&!n.startsWith("!")):[]}catch{return[]}}function Vn(r,e){let n=r.split(S.sep).join("/"),s=S.basename(n);for(let i of e){let o=i.replace(/^\//,"").replace(/\/$/,"");if(o){if(o.includes("*")){try{if(Ne(o).test(n)||Ne(o).test(s))return!0}catch{}continue}if(n===o||n.startsWith(`${o}/`)||s===o)return!0}}return!1}function de(r,e){let n=z(r);return S.isAbsolute(n)?n:S.resolve(e,n)}function U(r,e=""){return typeof r=="string"?r:e}function lr(r,e){return typeof r=="number"&&Number.isFinite(r)?r:e}function dr(r){return r.subarray(0,8e3).includes(0)}var hr={name:"read_file",description:"\u8BFB\u53D6\u6587\u672C\u6587\u4EF6\u5185\u5BB9\uFF0C\u8FD4\u56DE\u5E26\u884C\u53F7\u7684\u6587\u672C\u3002\u652F\u6301\u901A\u8FC7 offset/limit \u8BFB\u53D6\u6307\u5B9A\u884C\u8303\u56F4\u3002\u5728\u4FEE\u6539\u6587\u4EF6\u524D\u5E94\u5148\u8BFB\u53D6\u786E\u8BA4\u5185\u5BB9\u3002",parameters:{type:"object",properties:{path:{type:"string",description:"\u6587\u4EF6\u8DEF\u5F84\uFF08\u7EDD\u5BF9\u6216\u76F8\u5BF9\u5F53\u524D\u76EE\u5F55\uFF09"},offset:{type:"integer",description:"\u8D77\u59CB\u884C\u53F7\uFF08\u4ECE 1 \u5F00\u59CB\uFF0C\u53EF\u9009\uFF09"},limit:{type:"integer",description:`\u6700\u591A\u8BFB\u53D6\u7684\u884C\u6570\uFF08\u9ED8\u8BA4 ${or}\uFF09`}},required:["path"]},async execute(r,e){let n=de(U(r.path),e.cwd);if(!v.existsSync(n))return{ok:!1,content:`\u9519\u8BEF\uFF1A\u6587\u4EF6\u4E0D\u5B58\u5728\uFF1A${n}`};let s=v.statSync(n);if(s.isDirectory())return{ok:!1,content:`\u9519\u8BEF\uFF1A${n} \u662F\u4E00\u4E2A\u76EE\u5F55\uFF0C\u8BF7\u4F7F\u7528 list_dir`};if(!s.isFile())return{ok:!1,content:`\u9519\u8BEF\uFF1A${n} \u4E0D\u662F\u666E\u901A\u6587\u4EF6\uFF08\u8BBE\u5907\u6587\u4EF6 / \u7BA1\u9053 / socket\uFF09\uFF0C\u62D2\u7EDD\u8BFB\u53D6`};if(s.size>Xn)return{ok:!1,content:`\u9519\u8BEF\uFF1A\u6587\u4EF6\u8FC7\u5927\uFF08${(s.size/1024/1024).toFixed(1)}MB\uFF09\uFF0C\u8D85\u51FA 2MB \u9650\u5236`};let i=v.readFileSync(n);if(dr(i))return{ok:!1,content:`\u9519\u8BEF\uFF1A${n} \u662F\u4E8C\u8FDB\u5236\u6587\u4EF6\uFF0C\u65E0\u6CD5\u4F5C\u4E3A\u6587\u672C\u8BFB\u53D6`};let o=i.toString("utf8").split(`
25
25
  `),a=Math.max(1,lr(r.offset,1)),l=Math.max(1,lr(r.limit,or)),c=o.slice(a-1,a-1+l),u=c.map((h,m)=>{let p=a+m;return`${String(p).padStart(6," ")}| ${h}`}).join(`
26
26
  `),d=a-1+l<o.length?`
27
27
  \u2026\uFF08\u6587\u4EF6\u5171 ${o.length} \u884C\uFF0C\u5DF2\u622A\u65AD\uFF09`:"";return{ok:!0,content:u+d,summary:`\u8BFB\u53D6 ${c.length} \u884C`}}},mr={name:"write_file",description:"\u5199\u5165\uFF08\u8986\u76D6\uFF09\u6587\u4EF6\u3002\u4F1A\u521B\u5EFA\u7F3A\u5931\u7684\u7236\u76EE\u5F55\u3002\u8BF7\u8C28\u614E\u4F7F\u7528\uFF0C\u4F18\u5148\u4F7F\u7528 edit_file \u505A\u5C40\u90E8\u4FEE\u6539\u3002",requiresConfirmation:!0,parameters:{type:"object",properties:{path:{type:"string",description:"\u6587\u4EF6\u8DEF\u5F84"},content:{type:"string",description:"\u8981\u5199\u5165\u7684\u5B8C\u6574\u6587\u4EF6\u5185\u5BB9"}},required:["path","content"]},async execute(r,e){let n=de(U(r.path),e.cwd),s=U(r.content);try{return v.mkdirSync(S.dirname(n),{recursive:!0}),v.writeFileSync(n,s,"utf8"),{ok:!0,content:`\u5DF2\u5199\u5165 ${n}\uFF08${s.length} \u5B57\u7B26\uFF09`,summary:"\u5199\u5165\u6210\u529F"}}catch(i){return{ok:!1,content:`\u5199\u5165\u5931\u8D25\uFF1A${i instanceof Error?i.message:String(i)}`}}}},pr={name:"edit_file",description:"\u7CBE\u786E\u66FF\u6362\u6587\u4EF6\u4E2D\u7684\u6587\u672C\u3002\u5C06 search \u6307\u5B9A\u7684\u6587\u672C\u66FF\u6362\u4E3A replace\u3002search \u5FC5\u987B\u4E0E\u6587\u4EF6\u5185\u5BB9\u5B8C\u5168\u4E00\u81F4\uFF08\u542B\u7F29\u8FDB\uFF09\uFF1B\u82E5 search \u5339\u914D\u5230\u591A\u5904\u5219\u9ED8\u8BA4\u62D2\u7EDD\u6267\u884C\uFF0C\u9700\u63D0\u4F9B\u66F4\u7CBE\u786E\u7684 search \u6216\u8BBE\u7F6E replace_all \u4E3A true\u3002",requiresConfirmation:!0,parameters:{type:"object",properties:{path:{type:"string",description:"\u6587\u4EF6\u8DEF\u5F84"},search:{type:"string",description:"\u8981\u88AB\u66FF\u6362\u7684\u539F\u6587\uFF08\u9700\u7CBE\u786E\u5339\u914D\uFF09"},replace:{type:"string",description:"\u66FF\u6362\u540E\u7684\u65B0\u6587\u672C"},replace_all:{type:"boolean",description:"\u662F\u5426\u66FF\u6362\u6240\u6709\u5339\u914D\u9879\uFF08\u9ED8\u8BA4 false\uFF09"}},required:["path","search","replace"]},async execute(r,e){let n=de(U(r.path),e.cwd);if(!v.existsSync(n))return{ok:!1,content:`\u9519\u8BEF\uFF1A\u6587\u4EF6\u4E0D\u5B58\u5728\uFF1A${n}`};let s=U(r.search),i=U(r.replace),o=r.replace_all===!0;if(!s)return{ok:!1,content:"\u9519\u8BEF\uFF1Asearch \u4E0D\u80FD\u4E3A\u7A7A"};let a=v.readFileSync(n,"utf8");if(!a.includes(s))return{ok:!1,content:`\u9519\u8BEF\uFF1A\u5728 ${n} \u4E2D\u672A\u627E\u5230\u5339\u914D\u6587\u672C\u3002\u8BF7\u5148\u7528 read_file \u786E\u8BA4\u5185\u5BB9\uFF08\u6CE8\u610F\u7F29\u8FDB\u4E0E\u6362\u884C\u9700\u5B8C\u5168\u4E00\u81F4\uFF09\u3002`};let l=a.split(s).length-1;if(l>1&&!o)return{ok:!1,content:`\u9519\u8BEF\uFF1Asearch \u5728 ${n} \u4E2D\u51FA\u73B0 ${l} \u5904\uFF0C\u4E3A\u907F\u514D\u8BEF\u6539\u672A\u6267\u884C\u3002
@@ -96,7 +96,7 @@ ${i.map(c=>`> ${c}`).join(`
96
96
 
97
97
  `:"",l=i.filter(c=>c.startsWith("\u2713")).length;return{ok:!0,content:`# \u6280\u80FD\uFF1A${s.name}
98
98
 
99
- ${a}${o}`,summary:l>0?`\u5DF2\u52A0\u8F7D ${s.name}\uFF08\u5B89\u88C5 ${l} \u9879\u4F9D\u8D56\uFF09`:`\u5DF2\u52A0\u8F7D\u6280\u80FD ${s.name}`}}};var Kr=[Dr,Ur,Cr,Sr,hr,fr,gr,$r,mr,pr,ir];function xt(r){return Kr.find(e=>e.name===r)}function Wr(){return Kr.map(r=>({name:r.name,description:r.description,parameters:r.parameters}))}async function Br(r,e,n){let s=xt(r);if(!s)return{ok:!1,content:`\u9519\u8BEF\uFF1A\u672A\u77E5\u5DE5\u5177 "${r}"`};let i={};if(e&&e.trim())try{let d=JSON.parse(e);typeof d=="object"&&d!==null&&!Array.isArray(d)&&(i=d)}catch(d){return{ok:!1,content:`\u9519\u8BEF\uFF1A\u5DE5\u5177\u53C2\u6570\u4E0D\u662F\u5408\u6CD5 JSON\uFF1A${d instanceof Error?d.message:String(d)}`}}let o=s.parameters,a=o.properties??{},l=Array.isArray(o.required)?o.required:[],c=Object.keys(i).filter(d=>!(d in a));if(c.length>0){let d=Object.keys(a);return{ok:!1,content:`\u9519\u8BEF\uFF1A${r} \u6536\u5230\u672A\u77E5\u53C2\u6570 ${c.map(h=>`"${h}"`).join("\u3001")}\u3002\u53EF\u7528\u53C2\u6570\uFF1A${d.length>0?d.map(h=>`"${h}"`).join("\u3001"):"\uFF08\u65E0\uFF09"}`}}let u=l.filter(d=>i[d]===void 0||i[d]===null);if(u.length>0)return{ok:!1,content:`\u9519\u8BEF\uFF1A${r} \u7F3A\u5C11\u5FC5\u586B\u53C2\u6570 ${u.map(d=>`"${d}"`).join("\u3001")}\u3002`};try{return await s.execute(i,n)}catch(d){return{ok:!1,content:`\u5DE5\u5177\u6267\u884C\u5F02\u5E38\uFF1A${d instanceof Error?d.message:String(d)}`}}}function zr(r,e,n,s){let o=`\u4F60\u662F\u300C\u7384\u732A\u300D\uFF08XuanZhu\uFF09\uFF0C\u4E00\u6B3E\u8FD0\u884C\u5728\u7EC8\u7AEF\u4E2D\u7684 AI \u7F16\u7801 Agent\u3002
99
+ ${a}${o}`,summary:l>0?`\u5DF2\u52A0\u8F7D ${s.name}\uFF08\u5B89\u88C5 ${l} \u9879\u4F9D\u8D56\uFF09`:`\u5DF2\u52A0\u8F7D\u6280\u80FD ${s.name}`}}};var Kr=[Dr,Ur,Cr,Sr,hr,fr,gr,$r,mr,pr,ir];function xt(r){return Kr.find(e=>e.name===r)}function Wr(){return Kr.map(r=>({name:r.name,description:r.description,parameters:r.parameters}))}async function Br(r,e,n){let s=xt(r);if(!s)return{ok:!1,content:`\u9519\u8BEF\uFF1A\u672A\u77E5\u5DE5\u5177 "${r}"`};let i={};if(e&&e.trim())try{let d=JSON.parse(e);typeof d=="object"&&d!==null&&!Array.isArray(d)&&(i=d)}catch(d){return{ok:!1,content:`\u9519\u8BEF\uFF1A\u5DE5\u5177\u53C2\u6570\u4E0D\u662F\u5408\u6CD5 JSON\uFF1A${d instanceof Error?d.message:String(d)}`}}let o=s.parameters,a=o.properties??{},l=Array.isArray(o.required)?o.required:[],c=Object.keys(i).filter(d=>!(d in a));if(c.length>0){let d=Object.keys(a);return{ok:!1,content:`\u9519\u8BEF\uFF1A${r} \u6536\u5230\u672A\u77E5\u53C2\u6570 ${c.map(h=>`"${h}"`).join("\u3001")}\u3002\u53EF\u7528\u53C2\u6570\uFF1A${d.length>0?d.map(h=>`"${h}"`).join("\u3001"):"\uFF08\u65E0\uFF09"}`}}let u=l.filter(d=>i[d]===void 0||i[d]===null);if(u.length>0)return{ok:!1,content:`\u9519\u8BEF\uFF1A${r} \u7F3A\u5C11\u5FC5\u586B\u53C2\u6570 ${u.map(d=>`"${d}"`).join("\u3001")}\u3002`};try{return await s.execute(i,n)}catch(d){return{ok:!1,content:`\u5DE5\u5177\u6267\u884C\u5F02\u5E38\uFF1A${d instanceof Error?d.message:String(d)}`}}}function jr(r,e,n,s){let o=`\u4F60\u662F\u300C\u7384\u732A\u300D\uFF08XuanZhu\uFF09\uFF0C\u4E00\u6B3E\u8FD0\u884C\u5728\u7EC8\u7AEF\u4E2D\u7684 AI \u7F16\u7801 Agent\u3002
100
100
 
101
101
  \u4F60\u7684\u5DE5\u4F5C\u76EE\u5F55\uFF1A${r}
102
102
 
@@ -136,7 +136,7 @@ ${e.trim()}`),o}var Fr=1500;function hs(r){let e="\n\n## \u9879\u76EE\u8BB0\u5FC
136
136
  \u2026\uFF08\u5DF2\u622A\u65AD\uFF0C\u53EF\u7528 memory_read \u67E5\u770B\u5B8C\u6574\u5185\u5BB9\uFF09`:n;e+=`
137
137
 
138
138
  ### \u73B0\u6709\u957F\u671F\u8BB0\u5FC6\uFF08\u8282\u9009\uFF09
139
- ${s}`}return e}var qr=x(require("path")),ms=new Set(["read_file","list_dir","glob","grep"]),ps=/(^|\/)(\.env(\..+)?|\.npmrc|\.netrc|\.git-credentials|\.ssh|\.aws|\.gnupg|\.kube|\.docker|\.xzh|\.xuanzhu|credentials|secrets?)(\/|$)|\.(pem|key|p12|pfx|jks)$|(^|\/)id_(rsa|dsa|ecdsa|ed25519)(\.|$)/i,jr=400,fs=.5,bt=1e6,gs=4,$s=3,Be=class{provider;config;cwd;events;systemPrompt;warnedTightContext=!1;history=[];activeModel;constructor(e,n,s,i,o){this.provider=e,this.config=n,this.cwd=s,this.events=i,this.activeModel=o??ee(n),this.systemPrompt=this.composeSystemPrompt()}getActiveModel(){return this.activeModel}composeSystemPrompt(){return zr(this.cwd,this.config.systemPromptExtra,br(this.cwd),De(this.cwd))}get messages(){return this.history}reset(){this.history=[]}get messageCount(){return this.history.length}getCwd(){return this.cwd}setCwd(e){this.cwd=e,this.systemPrompt=this.composeSystemPrompt()}async run(e,n){let s=e;if(!this.warnedTightContext&&ct(this.activeModel?.contextWindow,this.activeModel?.model??"",this.systemPrompt)&&(this.warnedTightContext=!0,this.events.onNotice?.("\u5F53\u524D\u6A21\u578B\u53EF\u7528\u4E0A\u4E0B\u6587\u8FC7\u5C0F\uFF1A\u7CFB\u7EDF\u63D0\u793A\u8BCD\u5DF2\u5360\u53BB\u7EDD\u5927\u90E8\u5206\u7A97\u53E3\uFF0C\u5386\u53F2\u4E0E\u5DE5\u5177\u8F93\u51FA\u5C06\u65E0\u6CD5\u53D1\u9001\u3002\u53EF\u7CBE\u7B80\u9879\u76EE\u89C4\u5219\uFF08.xuanzhu/rules.md\uFF09\u6216 systemPromptExtra\uFF0C\u6216\u6539\u7528\u7A97\u53E3\u66F4\u5927\u7684\u6A21\u578B\uFF08xzh model context <id> <\u5927\u5C0F>\uFF09\u3002")),this.config.intent?.enabled){this.events.onStatus("\u5206\u6790\u610F\u56FE\u4E2D");let h=await this.analyzeIntent(e,n);if(n?.aborted){this.events.onStatus("\u5DF2\u4E2D\u65AD");return}h?(s=h,this.events.onIntent?.(h,e)):this.events.onNotice?.("\u610F\u56FE\u5206\u6790\u672A\u8FD4\u56DE\u5185\u5BB9\uFF0C\u5DF2\u6309\u539F\u59CB\u8F93\u5165\u7EE7\u7EED\u3002")}let i=[{role:"user",content:s}],o=Math.max(1,Math.floor(this.config.maxToolRounds)||1),a=()=>{for(let h of i)this.history.push(h);this.trimHistory()},l=()=>{let h=[];for(let m=0;m<i.length;m++){let p=i[m];if(h.push(p),p.role!=="assistant"||!p.toolCalls?.length)continue;let f=p.toolCalls.length,g=0;for(let $=m+1;$<i.length&&g<f;$++)i[$].role==="tool"&&g++;if(g<f){h.pop();break}}if(h.length>1){for(let m of h)this.history.push(m);this.trimHistory()}},c="",u=0,d=o;try{for(let f=0;;f++){if(f>=d){if(n?.aborted){l(),this.events.onStatus("\u5DF2\u4E2D\u65AD");return}if(!(this.events.onRoundLimit?await this.events.onRoundLimit(f):!1))break;d+=o}if(n?.aborted){l(),this.events.onStatus("\u5DF2\u4E2D\u65AD");return}this.trimHistory(),this.events.onStatus("\u601D\u8003\u4E2D",`\u7B2C ${f+1} \u8F6E`);let{text:g,toolCalls:$}=await this.streamAssistant(n,0,new Set,i);if(n?.aborted){l(),this.events.onStatus("\u5DF2\u4E2D\u65AD");return}if(i.push({role:"assistant",content:g,toolCalls:$.length>0?$:void 0}),$.length===0){a(),this.events.onStatus("\u5C31\u7EEA");return}for(let b of $){let y=`${b.name}:${b.arguments}`;if(y===c?u++:(c=y,u=1),u>=gs){a(),this.events.onStatus("\u5DF2\u4E2D\u6B62","\u68C0\u6D4B\u5230\u91CD\u590D\u8C03\u7528"),this.events.onNotice?.(`\u68C0\u6D4B\u5230\u8FDE\u7EED ${u} \u6B21\u5B8C\u5168\u76F8\u540C\u7684\u5DE5\u5177\u8C03\u7528\uFF08${b.name}\uFF09\uFF0C\u5DF2\u5224\u5B9A\u4E3A\u9677\u5165\u5FAA\u73AF\u5E76\u4E2D\u6B62\u672C\u8F6E\u3002
139
+ ${s}`}return e}var qr=x(require("path")),ms=new Set(["read_file","list_dir","glob","grep"]),ps=/(^|\/)(\.env(\..+)?|\.npmrc|\.netrc|\.git-credentials|\.ssh|\.aws|\.gnupg|\.kube|\.docker|\.xzh|\.xuanzhu|credentials|secrets?)(\/|$)|\.(pem|key|p12|pfx|jks)$|(^|\/)id_(rsa|dsa|ecdsa|ed25519)(\.|$)/i,zr=400,fs=.5,bt=1e6,gs=4,$s=3,Be=class{provider;config;cwd;events;systemPrompt;warnedTightContext=!1;history=[];activeModel;constructor(e,n,s,i,o){this.provider=e,this.config=n,this.cwd=s,this.events=i,this.activeModel=o??ee(n),this.systemPrompt=this.composeSystemPrompt()}getActiveModel(){return this.activeModel}composeSystemPrompt(){return jr(this.cwd,this.config.systemPromptExtra,br(this.cwd),De(this.cwd))}get messages(){return this.history}reset(){this.history=[]}get messageCount(){return this.history.length}getCwd(){return this.cwd}setCwd(e){this.cwd=e,this.systemPrompt=this.composeSystemPrompt()}async run(e,n){let s=e;if(!this.warnedTightContext&&ct(this.activeModel?.contextWindow,this.activeModel?.model??"",this.systemPrompt)&&(this.warnedTightContext=!0,this.events.onNotice?.("\u5F53\u524D\u6A21\u578B\u53EF\u7528\u4E0A\u4E0B\u6587\u8FC7\u5C0F\uFF1A\u7CFB\u7EDF\u63D0\u793A\u8BCD\u5DF2\u5360\u53BB\u7EDD\u5927\u90E8\u5206\u7A97\u53E3\uFF0C\u5386\u53F2\u4E0E\u5DE5\u5177\u8F93\u51FA\u5C06\u65E0\u6CD5\u53D1\u9001\u3002\u53EF\u7CBE\u7B80\u9879\u76EE\u89C4\u5219\uFF08.xuanzhu/rules.md\uFF09\u6216 systemPromptExtra\uFF0C\u6216\u6539\u7528\u7A97\u53E3\u66F4\u5927\u7684\u6A21\u578B\uFF08xzh model context <id> <\u5927\u5C0F>\uFF09\u3002")),this.config.intent?.enabled){this.events.onStatus("\u5206\u6790\u610F\u56FE\u4E2D");let h=await this.analyzeIntent(e,n);if(n?.aborted){this.events.onStatus("\u5DF2\u4E2D\u65AD");return}h?(s=h,this.events.onIntent?.(h,e)):this.events.onNotice?.("\u610F\u56FE\u5206\u6790\u672A\u8FD4\u56DE\u5185\u5BB9\uFF0C\u5DF2\u6309\u539F\u59CB\u8F93\u5165\u7EE7\u7EED\u3002")}let i=[{role:"user",content:s}],o=Math.max(1,Math.floor(this.config.maxToolRounds)||1),a=()=>{for(let h of i)this.history.push(h);this.trimHistory()},l=()=>{let h=[];for(let m=0;m<i.length;m++){let p=i[m];if(h.push(p),p.role!=="assistant"||!p.toolCalls?.length)continue;let f=p.toolCalls.length,g=0;for(let $=m+1;$<i.length&&g<f;$++)i[$].role==="tool"&&g++;if(g<f){h.pop();break}}if(h.length>1){for(let m of h)this.history.push(m);this.trimHistory()}},c="",u=0,d=o;try{for(let f=0;;f++){if(f>=d){if(n?.aborted){l(),this.events.onStatus("\u5DF2\u4E2D\u65AD");return}if(!(this.events.onRoundLimit?await this.events.onRoundLimit(f):!1))break;d+=o}if(n?.aborted){l(),this.events.onStatus("\u5DF2\u4E2D\u65AD");return}this.trimHistory(),this.events.onStatus("\u601D\u8003\u4E2D",`\u7B2C ${f+1} \u8F6E`);let{text:g,toolCalls:$}=await this.streamAssistant(n,0,new Set,i);if(n?.aborted){l(),this.events.onStatus("\u5DF2\u4E2D\u65AD");return}if(i.push({role:"assistant",content:g,toolCalls:$.length>0?$:void 0}),$.length===0){a(),this.events.onStatus("\u5C31\u7EEA");return}for(let b of $){let y=`${b.name}:${b.arguments}`;if(y===c?u++:(c=y,u=1),u>=gs){a(),this.events.onStatus("\u5DF2\u4E2D\u6B62","\u68C0\u6D4B\u5230\u91CD\u590D\u8C03\u7528"),this.events.onNotice?.(`\u68C0\u6D4B\u5230\u8FDE\u7EED ${u} \u6B21\u5B8C\u5168\u76F8\u540C\u7684\u5DE5\u5177\u8C03\u7528\uFF08${b.name}\uFF09\uFF0C\u5DF2\u5224\u5B9A\u4E3A\u9677\u5165\u5FAA\u73AF\u5E76\u4E2D\u6B62\u672C\u8F6E\u3002
140
140
  \xB7 \u5E38\u89C1\u539F\u56E0\uFF1A\u6A21\u578B\u770B\u4E0D\u5230\u6B64\u524D\u7684\u5DE5\u5177\u7ED3\u679C\uFF0C\u6216\u4EFB\u52A1\u63CF\u8FF0\u4E0D\u591F\u5177\u4F53
141
141
  \xB7 \u53EF\u4EE5\u8865\u5145\u66F4\u660E\u786E\u7684\u8981\u6C42\uFF0C\u6216\u76F4\u63A5\u544A\u8BC9\u5B83\u300C\u4E0D\u8981\u91CD\u590D\u8C03\u7528 ${b.name}\u300D
142
142
  \xB7 \u82E5\u4E0A\u4E0B\u6587\u8FC7\u5927\u5BFC\u81F4\u5386\u53F2\u88AB\u88C1\u526A\uFF0C\u53EF\u8C03\u5927\u8BE5\u6A21\u578B\u7684\u4E0A\u4E0B\u6587\u7A97\u53E3\uFF1Axzh model context <id> <\u5927\u5C0F>`);return}let _=await this.runTool(b,n);if(i.push({role:"tool",content:_.content,toolCallId:b.id,name:b.name}),this.events.onToolEnd(b,_),n?.aborted){l(),this.events.onStatus("\u5DF2\u4E2D\u65AD");return}}}a(),this.events.onStatus("\u5DF2\u8FBE\u5DE5\u5177\u8C03\u7528\u4E0A\u9650",`\u5171 ${d} \u8F6E`);let h=new Map;for(let f of i)for(let g of f.toolCalls??[])h.set(g.name,(h.get(g.name)??0)+1);let m=[...h.values()].reduce((f,g)=>f+g,0),p=[...h.entries()].sort((f,g)=>g[1]-f[1]).map(([f,g])=>`${f} \xD7${g}`).join("\u3001");this.events.onNotice?.(`\u5DF2\u8FBE\u5DE5\u5177\u8C03\u7528\u4E0A\u9650\uFF08\u5171 ${d} \u8F6E\uFF09\u800C\u4E2D\u6B62\u672C\u8F6E\u3002
@@ -146,27 +146,27 @@ ${s}`}return e}var qr=x(require("path")),ms=new Set(["read_file","list_dir","glo
146
146
  \xB7 \u6216\u628A\u4EFB\u52A1\u62C6\u6210\u66F4\u5C0F\u7684\u6B65\u9AA4\uFF0C\u6BCF\u6B65\u5355\u72EC\u63D0\u4E00\u6B21`)}catch(h){if(l(),n?.aborted){this.events.onStatus("\u5DF2\u4E2D\u65AD");return}throw h}}async analyzeIntent(e,n){let s=this.config.intent?.prompt?.trim()||Ut,i=Kt(s,e),o="";try{let l=this.provider.chat({messages:[{role:"user",content:i}],tools:[],temperature:this.config.temperature,signal:n});for await(let c of l)if(c.type==="text"&&(o+=c.text,o.length>bt))break}catch(l){return n?.aborted||this.events.onNotice?.(`\u610F\u56FE\u5206\u6790\u8BF7\u6C42\u5931\u8D25\uFF08${G(l).split(`
147
147
  `)[0]}\uFF09\uFF0C\u5DF2\u6309\u539F\u59CB\u8F93\u5165\u7EE7\u7EED\u3002`),null}let a=o.trim();return a.length>0?a:null}async streamAssistant(e,n=0,s=new Set,i=[],o=1){this.activeModel&&s.add(this.activeModel.id);let a=[{role:"system",content:this.systemPrompt},...this.buildConversation(i,o)],l="",c=[],u=!1;try{let d=this.provider.chat({messages:a,tools:Wr(),temperature:this.config.temperature,signal:e,onRetry:(h,m,p)=>{this.events.onStatus(`\u7F51\u7EDC\u4E2D\u65AD\uFF0C\u6B63\u5728\u91CD\u8BD5 (${h}/${m})`,G(p).split(`
148
148
  `)[0])}});for await(let h of d){if(e?.aborted)break;switch(u=!0,h.type){case"text":if(l+=h.text,this.events.onText(h.text),l.length>bt)return this.events.onNotice?.(`\u6A21\u578B\u8F93\u51FA\u8D85\u8FC7 ${bt} \u5B57\u7B26\uFF0C\u5DF2\u5F3A\u5236\u4E2D\u65AD \u2014\u2014 \u8FD9\u901A\u5E38\u610F\u5473\u7740\u6A21\u578B\u9677\u5165\u4E86\u91CD\u590D\u8F93\u51FA\uFF0C\u5EFA\u8BAE\u68C0\u67E5\u4E0A\u4E0B\u6587\u6216\u6362\u4E2A\u63D0\u793A\u3002`),{text:l,toolCalls:c};break;case"reasoning":this.events.onReasoning?.(h.text);break;case"tool_call":c.push(h.toolCall);break;case"done":break}}return{text:l,toolCalls:c}}catch(d){if(e?.aborted)throw d;if(!u&&!e?.aborted&&o>=1&&Ee(d))return this.events.onStatus("\u4E0A\u4E0B\u6587\u8D85\u9650","\u5DF2\u6536\u655B\u5386\u53F2\uFF0C\u6B63\u5728\u91CD\u8BD5"),this.streamAssistant(e,n,s,i,fs);if(this.handleModelFailure(d,s,!u)&&n<$s)return this.streamAssistant(e,n+1,s,i,o);throw d}}handleModelFailure(e,n,s=!0){let i=this.activeModel;if(!i)return!1;let o=G(e).split(`
149
- `)[0];if(tt(e))return this.events.onNotice?.(`\u26A0 \u6A21\u578B ${i.id} \u7684\u914D\u7F6E\u53EF\u80FD\u6709\u8BEF\uFF08${o}\uFF09\u3002\u5DF2\u8DF3\u8FC7\u6743\u91CD\u8870\u51CF \u2014\u2014 \u8BF7\u68C0\u67E5 provider\u3001API Key \u4E0E baseUrl\u3002`),!1;let a=Bt(this.config,i.id);if(!a||(this.persistConfig(),a.reset?(this.events.onNotice?.(`\u26A0 \u6A21\u578B ${i.id} \u8C03\u7528\u5931\u8D25\uFF08${o}\uFF09\uFF1B\u6240\u6709\u6A21\u578B\u6743\u91CD\u5747\u5DF2\u8017\u5C3D\uFF0C\u5DF2\u628A\u5168\u90E8\u6743\u91CD\u91CD\u7F6E\u4E3A ${T}\u3002`),this.events.onStatus("\u6743\u91CD\u5DF2\u91CD\u7F6E")):this.events.onNotice?.(`\u26A0 \u6A21\u578B ${i.id} \u8C03\u7528\u5931\u8D25\uFF08${o}\uFF09\uFF0C\u6743\u91CD\u964D\u4E3A ${a.weight}\u3002`),this.events.onModelChange?.({provider:i.provider,model:i.model,weight:a.reset?T:a.weight,reason:"\u8C03\u7528\u5931\u8D25\u964D\u6743"}),!s))return!1;let l=ee(this.config,n);if(!l||l.id===i.id)return l&&(this.activeModel=l),!1;try{return this.provider=Ie(this.config,l),this.activeModel=l,this.events.onNotice?.(`\u21BB \u5DF2\u5207\u6362\u5230\u6A21\u578B ${l.id}\uFF08\u6743\u91CD ${l.weight}\uFF09\u91CD\u8BD5\u3002`),this.events.onModelChange?.({provider:l.provider,model:l.model,weight:l.weight,reason:"\u8C03\u7528\u5931\u8D25\u964D\u7EA7"}),!0}catch(c){return this.events.onNotice?.(`\u5207\u6362\u6A21\u578B\u5931\u8D25\uFF1A${G(c)}`),!1}}persistConfig(){try{M(this.config)}catch{}}touchesSensitivePath(e,n){if(!ms.has(e))return!1;let s="";try{let i=JSON.parse(n);typeof i.path=="string"&&(s=i.path)}catch{return!1}return s?ps.test(qr.resolve(this.cwd,j(s))):!1}async runTool(e,n){let s=xt(e.name);if(!s)return{ok:!1,content:`\u9519\u8BEF\uFF1A\u672A\u77E5\u5DE5\u5177 "${e.name}"`};if(this.touchesSensitivePath(s.name,e.arguments)||!!s.requiresConfirmation&&!this.config.autoApprove){let a=e.arguments;try{a=JSON.stringify(JSON.parse(e.arguments),null,2)}catch{}if(!await this.events.confirm({tool:e.name,title:`\u6267\u884C\u5DE5\u5177\uFF1A${e.name}`,detail:a,danger:!!s.danger}))return{ok:!1,denied:!0,content:"\u7528\u6237\u62D2\u7EDD\u4E86\u6B64\u64CD\u4F5C\u3002"}}if(n?.aborted)return{ok:!1,content:"\u64CD\u4F5C\u5DF2\u88AB\u4E2D\u65AD\u3002"};this.events.onToolStart(e);let o={cwd:this.cwd,autoApprove:this.config.autoApprove,emit:a=>this.events.onNotice?.(a),confirm:a=>this.events.confirm(a),signal:n};return Br(e.name,e.arguments,o)}conversationBudget(){return lt(this.activeModel?.contextWindow,this.activeModel?.model??"",this.systemPrompt)}buildConversation(e,n=1){let s=[...this.history,...e],i=s.length>jr?s.slice(-jr):s,o=Math.max(1,Math.floor(this.conversationBudget()*n));return Pe(i,o)}trimHistory(){let e=Pe(this.history,this.conversationBudget());if(e.length===this.history.length)return 0;let n=this.history.length-e.length;return this.history=e,n}};var me="0.1.6";var t={reset:"\x1B[0m",bold:"\x1B[1m",dim:"\x1B[2m",italic:"\x1B[3m",underline:"\x1B[4m",inverse:"\x1B[7m",black:"\x1B[30m",red:"\x1B[31m",green:"\x1B[32m",yellow:"\x1B[33m",blue:"\x1B[34m",magenta:"\x1B[35m",cyan:"\x1B[36m",white:"\x1B[37m",gray:"\x1B[90m",brightRed:"\x1B[91m",brightGreen:"\x1B[92m",brightYellow:"\x1B[93m",brightBlue:"\x1B[94m",brightMagenta:"\x1B[95m",brightCyan:"\x1B[96m",brightWhite:"\x1B[97m",bgRed:"\x1B[41m",bgGreen:"\x1B[42m",bgYellow:"\x1B[43m",bgBlue:"\x1B[44m",bgMagenta:"\x1B[45m",bgCyan:"\x1B[46m",bgGray:"\x1B[100m"},K={hide:"\x1B[?25l",show:"\x1B[?25h",up:(r=1)=>`\x1B[${r}A`,down:(r=1)=>`\x1B[${r}B`,right:(r=1)=>`\x1B[${r}C`,left:(r=1)=>`\x1B[${r}D`,to:(r,e)=>`\x1B[${r};${e}H`,col:r=>`\x1B[${r}G`,clearLine:"\x1B[2K",clearToEnd:"\x1B[0K",clearScreen:"\x1B[2J"},Fe={enterAlt:"\x1B[?1049h",exitAlt:"\x1B[?1049l",save:"\x1B[s",restore:"\x1B[u"};function ys(r){return r>=4352&&r<=4447||r>=11904&&r<=12350||r>=12353&&r<=13311||r>=13312&&r<=19903||r>=19968&&r<=40959||r>=40960&&r<=42191||r>=44032&&r<=55203||r>=63744&&r<=64255||r>=65072&&r<=65103||r>=65280&&r<=65376||r>=65504&&r<=65510||r>=127744&&r<=129791||r>=131072&&r<=262141}function I(r){let e=0;for(let n=0;n<r.length;n++){let s=r.codePointAt(n);if(s!==void 0){if(s===27){let i=/^\x1b\[[0-9;?]*[ -/]*[@-~]/.exec(r.slice(n));if(i){n+=i[0].length-1;continue}let o=/^\x1b\][^\x07\x1b]*(?:\x07|\x1b\\)?/.exec(r.slice(n));if(o){n+=o[0].length-1;continue}}if(s>65535&&n++,s===9){e+=8-e%8;continue}s<32||s>=768&&s<=879||s>=8400&&s<=8447||s>=65024&&s<=65039||(e+=ys(s)?2:1)}}return e}function we(r,e){let n=[];for(let s of r.split(`
150
- `)){if(s===""){n.push("");continue}let i="",o=0,a=s.match(/\x1b\[[0-9;?]*[A-Za-z]|./gu)??[];for(let l of a){let c=I(l);o+c>e&&i!==""&&(n.push(i),i="",o=0),i+=l,o+=c}n.push(i)}return n}function W(r,e){if(I(r)<=e)return r;let n=r.match(/\x1b\[[0-9;?]*[A-Za-z]|[\s\S]/gu)??[],s="",i=0,o=Math.max(0,e-1);for(let a of n){let l=I(a);if(i+l>o)break;s+=a,i+=l}return`${s}${t.reset}\u2026`}function Hr(r,e){let n=I(r);return r+" ".repeat(Math.max(0,e-n))}function pe(r){return r&&r.replace(/\x1b\][^\x07\x1b]*(?:\x07|\x1b\\)?/g,"").replace(/\x1b\[[0-9;?]*[ -/]*[@-~]/g,"").replace(/\x1b[@-Z\\-_]/g,"").replace(/[\x00-\x08\x0b\x0c\x0e-\x1f\x7f]/g,"")}var ze=class{buffer="";inCodeBlock=!1;codeLang="";push(e){this.buffer+=e;let n=[],s=this.buffer.indexOf(`
149
+ `)[0];if(tt(e))return this.events.onNotice?.(`\u26A0 \u6A21\u578B ${i.id} \u7684\u914D\u7F6E\u53EF\u80FD\u6709\u8BEF\uFF08${o}\uFF09\u3002\u5DF2\u8DF3\u8FC7\u6743\u91CD\u8870\u51CF \u2014\u2014 \u8BF7\u68C0\u67E5 provider\u3001API Key \u4E0E baseUrl\u3002`),!1;let a=Bt(this.config,i.id);if(!a||(this.persistConfig(),a.reset?(this.events.onNotice?.(`\u26A0 \u6A21\u578B ${i.id} \u8C03\u7528\u5931\u8D25\uFF08${o}\uFF09\uFF1B\u6240\u6709\u6A21\u578B\u6743\u91CD\u5747\u5DF2\u8017\u5C3D\uFF0C\u5DF2\u628A\u5168\u90E8\u6743\u91CD\u91CD\u7F6E\u4E3A ${T}\u3002`),this.events.onStatus("\u6743\u91CD\u5DF2\u91CD\u7F6E")):this.events.onNotice?.(`\u26A0 \u6A21\u578B ${i.id} \u8C03\u7528\u5931\u8D25\uFF08${o}\uFF09\uFF0C\u6743\u91CD\u964D\u4E3A ${a.weight}\u3002`),this.events.onModelChange?.({provider:i.provider,model:i.model,weight:a.reset?T:a.weight,reason:"\u8C03\u7528\u5931\u8D25\u964D\u6743"}),!s))return!1;let l=ee(this.config,n);if(!l||l.id===i.id)return l&&(this.activeModel=l),!1;try{return this.provider=Ie(this.config,l),this.activeModel=l,this.events.onNotice?.(`\u21BB \u5DF2\u5207\u6362\u5230\u6A21\u578B ${l.id}\uFF08\u6743\u91CD ${l.weight}\uFF09\u91CD\u8BD5\u3002`),this.events.onModelChange?.({provider:l.provider,model:l.model,weight:l.weight,reason:"\u8C03\u7528\u5931\u8D25\u964D\u7EA7"}),!0}catch(c){return this.events.onNotice?.(`\u5207\u6362\u6A21\u578B\u5931\u8D25\uFF1A${G(c)}`),!1}}persistConfig(){try{M(this.config)}catch{}}touchesSensitivePath(e,n){if(!ms.has(e))return!1;let s="";try{let i=JSON.parse(n);typeof i.path=="string"&&(s=i.path)}catch{return!1}return s?ps.test(qr.resolve(this.cwd,z(s))):!1}async runTool(e,n){let s=xt(e.name);if(!s)return{ok:!1,content:`\u9519\u8BEF\uFF1A\u672A\u77E5\u5DE5\u5177 "${e.name}"`};if(this.touchesSensitivePath(s.name,e.arguments)||!!s.requiresConfirmation&&!this.config.autoApprove){let a=e.arguments;try{a=JSON.stringify(JSON.parse(e.arguments),null,2)}catch{}if(!await this.events.confirm({tool:e.name,title:`\u6267\u884C\u5DE5\u5177\uFF1A${e.name}`,detail:a,danger:!!s.danger}))return{ok:!1,denied:!0,content:"\u7528\u6237\u62D2\u7EDD\u4E86\u6B64\u64CD\u4F5C\u3002"}}if(n?.aborted)return{ok:!1,content:"\u64CD\u4F5C\u5DF2\u88AB\u4E2D\u65AD\u3002"};this.events.onToolStart(e);let o={cwd:this.cwd,autoApprove:this.config.autoApprove,emit:a=>this.events.onNotice?.(a),confirm:a=>this.events.confirm(a),signal:n};return Br(e.name,e.arguments,o)}conversationBudget(){return lt(this.activeModel?.contextWindow,this.activeModel?.model??"",this.systemPrompt)}buildConversation(e,n=1){let s=[...this.history,...e],i=s.length>zr?s.slice(-zr):s,o=Math.max(1,Math.floor(this.conversationBudget()*n));return Pe(i,o)}trimHistory(){let e=Pe(this.history,this.conversationBudget());if(e.length===this.history.length)return 0;let n=this.history.length-e.length;return this.history=e,n}};var me="0.1.7";var t={reset:"\x1B[0m",bold:"\x1B[1m",dim:"\x1B[2m",italic:"\x1B[3m",underline:"\x1B[4m",inverse:"\x1B[7m",black:"\x1B[30m",red:"\x1B[31m",green:"\x1B[32m",yellow:"\x1B[33m",blue:"\x1B[34m",magenta:"\x1B[35m",cyan:"\x1B[36m",white:"\x1B[37m",gray:"\x1B[90m",brightRed:"\x1B[91m",brightGreen:"\x1B[92m",brightYellow:"\x1B[93m",brightBlue:"\x1B[94m",brightMagenta:"\x1B[95m",brightCyan:"\x1B[96m",brightWhite:"\x1B[97m",bgRed:"\x1B[41m",bgGreen:"\x1B[42m",bgYellow:"\x1B[43m",bgBlue:"\x1B[44m",bgMagenta:"\x1B[45m",bgCyan:"\x1B[46m",bgGray:"\x1B[100m"},K={hide:"\x1B[?25l",show:"\x1B[?25h",up:(r=1)=>`\x1B[${r}A`,down:(r=1)=>`\x1B[${r}B`,right:(r=1)=>`\x1B[${r}C`,left:(r=1)=>`\x1B[${r}D`,to:(r,e)=>`\x1B[${r};${e}H`,col:r=>`\x1B[${r}G`,clearLine:"\x1B[2K",clearToEnd:"\x1B[0K",clearScreen:"\x1B[2J"},Fe={enterAlt:"\x1B[?1049h",exitAlt:"\x1B[?1049l",save:"\x1B[s",restore:"\x1B[u"};function ys(r){return r>=4352&&r<=4447||r>=11904&&r<=12350||r>=12353&&r<=13311||r>=13312&&r<=19903||r>=19968&&r<=40959||r>=40960&&r<=42191||r>=44032&&r<=55203||r>=63744&&r<=64255||r>=65072&&r<=65103||r>=65280&&r<=65376||r>=65504&&r<=65510||r>=127744&&r<=129791||r>=131072&&r<=262141}function I(r){let e=0;for(let n=0;n<r.length;n++){let s=r.codePointAt(n);if(s!==void 0){if(s===27){let i=/^\x1b\[[0-9;?]*[ -/]*[@-~]/.exec(r.slice(n));if(i){n+=i[0].length-1;continue}let o=/^\x1b\][^\x07\x1b]*(?:\x07|\x1b\\)?/.exec(r.slice(n));if(o){n+=o[0].length-1;continue}}if(s>65535&&n++,s===9){e+=8-e%8;continue}s<32||s>=768&&s<=879||s>=8400&&s<=8447||s>=65024&&s<=65039||(e+=ys(s)?2:1)}}return e}function we(r,e){let n=[];for(let s of r.split(`
150
+ `)){if(s===""){n.push("");continue}let i="",o=0,a=s.match(/\x1b\[[0-9;?]*[A-Za-z]|./gu)??[];for(let l of a){let c=I(l);o+c>e&&i!==""&&(n.push(i),i="",o=0),i+=l,o+=c}n.push(i)}return n}function W(r,e){if(I(r)<=e)return r;let n=r.match(/\x1b\[[0-9;?]*[A-Za-z]|[\s\S]/gu)??[],s="",i=0,o=Math.max(0,e-1);for(let a of n){let l=I(a);if(i+l>o)break;s+=a,i+=l}return`${s}${t.reset}\u2026`}function Hr(r,e){let n=I(r);return r+" ".repeat(Math.max(0,e-n))}function pe(r){return r&&r.replace(/\x1b\][^\x07\x1b]*(?:\x07|\x1b\\)?/g,"").replace(/\x1b\[[0-9;?]*[ -/]*[@-~]/g,"").replace(/\x1b[@-Z\\-_]/g,"").replace(/[\x00-\x08\x0b\x0c\x0e-\x1f\x7f]/g,"")}var je=class{buffer="";inCodeBlock=!1;codeLang="";push(e){this.buffer+=e;let n=[],s=this.buffer.indexOf(`
151
151
  `);for(;s!==-1;){let i=this.buffer.slice(0,s);this.buffer=this.buffer.slice(s+1),n.push(this.renderLine(i)),s=this.buffer.indexOf(`
152
152
  `)}return n}peek(){return this.buffer?this.inCodeBlock?`${t.cyan}${this.buffer}${t.reset}`:this.buffer:""}flush(){if(this.buffer.length===0)return this.inCodeBlock=!1,this.codeLang="",[];let e=this.buffer;this.buffer="";let n=this.renderLine(e);return this.inCodeBlock=!1,this.codeLang="",[n]}renderLine(e){let n=e.replace(/\r$/,""),s=n.trimStart();if(s.startsWith("```")){if(!this.inCodeBlock){this.inCodeBlock=!0,this.codeLang=s.slice(3).trim();let a=this.codeLang?` ${this.codeLang} `:"";return`${t.gray}\u250C\u2500${a}${"\u2500".repeat(Math.max(0,6-a.length))}${t.reset}`}return this.inCodeBlock=!1,this.codeLang="",`${t.gray}\u2514\u2500\u2500${t.reset}`}if(this.inCodeBlock)return`${t.cyan}${n}${t.reset}`;let i=/^(#{1,6})\s+(.*)$/.exec(s);if(i){let a=i[2];return i[1].length<=2?`${t.bold}${t.brightWhite}${a}${t.reset}`:`${t.bold}${a}${t.reset}`}if(s.startsWith("> "))return`${t.gray}\u2502 ${s.slice(2)}${t.reset}`;let o=/^(\s*)([-*+]|\d+\.)\s+(.*)$/.exec(n);return o?`${o[1]}${t.blue}${o[2]}${t.reset} ${o[3]}`:/^(-{3,}|\*{3,}|_{3,})$/.test(s)?`${t.gray}${"\u2500".repeat(20)}${t.reset}`:this.renderInline(n)}renderInline(e){let n=e;return n=n.replace(/`([^`]+)`/g,(s,i)=>`${t.cyan}${i}${t.reset}`),n=n.replace(/\*\*([^*]+)\*\*/g,(s,i)=>`${t.bold}${i}${t.reset}`),n=n.replace(/(^|\s)\*([^*\s][^*]*)\*(?=\s|$)/g,"$1$2"),n}};var Zr=require("child_process"),fe=x(require("fs")),qe=x(require("os")),B=x(require("path"));var Gr=2e3,xs=8192,bs=1200,He=class{lines=[];partial="";running;killTimer;cwd;onChange;parseState="text";parseSeq="";constructor(e,n){this.cwd=e,this.onChange=n,this.pushLine(`${t.gray}\u7EC8\u7AEF\u5C31\u7EEA \xB7 \u547D\u4EE4\u5728\u5F53\u524D\u9879\u76EE\u76EE\u5F55\u4E0B\u6267\u884C${t.reset}`)}getCwd(){return this.cwd}isRunning(){return this.running!==void 0}setCwd(e){this.cwd=e,this.pushLine(`${t.gray}\u2500\u2500 \u5DE5\u4F5C\u76EE\u5F55\u5DF2\u5207\u6362\uFF1A${e}${t.reset}`),this.onChange()}clear(){this.lines=[],this.partial="",this.onChange()}notice(e){this.pushLine(e),this.onChange()}visibleLines(){return this.partial?[...this.lines,this.partial]:this.lines}run(e){let n=e.trim();if(!n)return;if(this.running){this.pushLine(`${t.yellow}\u26A0 \u4E0A\u4E00\u6761\u547D\u4EE4\u4ECD\u5728\u8FD0\u884C\uFF0C\u8BF7\u5148 Ctrl+C \u4E2D\u65AD${t.reset}`),this.onChange();return}if(n==="clear"||n==="cls"){this.clear();return}if(this.handleCd(n))return;this.flushPartial(),this.pushLine(`${t.brightGreen}\u276F${t.reset} ${t.bold}${n}${t.reset}`);let s=process.env.SHELL||"/bin/bash",i;try{i=(0,Zr.spawn)(s,["-c",n],{cwd:this.cwd,env:{...process.env,TERM:"xterm-256color",CLICOLOR_FORCE:"1",FORCE_COLOR:"1"},stdio:["ignore","pipe","pipe"],detached:!0})}catch(a){this.pushLine(`${t.red}\u2717 \u542F\u52A8\u5931\u8D25\uFF1A${a instanceof Error?a.message:String(a)}${t.reset}`),this.onChange();return}this.running=i;let o=a=>this.feed(a.toString("utf8"));i.stdout?.on("data",o),i.stderr?.on("data",o),i.on("error",a=>{this.flushPartial(),this.pushLine(`${t.red}\u2717 \u6267\u884C\u51FA\u9519\uFF1A${a.message}${t.reset}`),this.running=void 0,this.onChange()}),i.on("close",(a,l)=>{this.flushPartial(),l?this.pushLine(`${t.yellow}\u23F9 \u5DF2\u4E2D\u65AD\uFF08${l}\uFF09${t.reset}`):a===0?this.pushLine(`${t.gray}\u2713 \u5B8C\u6210${t.reset}`):this.pushLine(`${t.red}\u2717 \u9000\u51FA\u7801 ${a}${t.reset}`),this.running=void 0,this.onChange()}),this.onChange()}interrupt(){let e=this.running;if(!e||e.pid===void 0)return;let n=e.pid,s=i=>{try{process.kill(-n,i)}catch{try{e.kill(i)}catch{}}};s("SIGINT"),this.killTimer&&clearTimeout(this.killTimer),this.killTimer=setTimeout(()=>{this.killTimer=void 0,this.running===e&&s("SIGKILL")},bs)}dispose(){this.killTimer&&(clearTimeout(this.killTimer),this.killTimer=void 0);let e=this.running;if(this.running=void 0,!(!e||e.pid===void 0))try{process.kill(-e.pid,"SIGKILL")}catch{try{e.kill("SIGKILL")}catch{}}}handleCd(e){let n=/^cd(?:\s+([\s\S]*))?$/.exec(e);if(!n)return!1;let s=(n[1]??"").trim().replace(/^['"]|['"]$/g,""),i;if(!s||s==="~")i=qe.homedir();else{let o=s.startsWith("~/")?B.join(qe.homedir(),s.slice(2)):s;i=B.isAbsolute(o)?o:B.resolve(this.cwd,o)}try{if(!fe.statSync(i).isDirectory())return this.pushLine(`${t.red}cd: \u4E0D\u662F\u76EE\u5F55\uFF1A${i}${t.reset}`),this.onChange(),!0}catch{return this.pushLine(`${t.red}cd: \u76EE\u5F55\u4E0D\u5B58\u5728\uFF1A${i}${t.reset}`),this.onChange(),!0}return this.cwd=i,this.flushPartial(),this.pushLine(`${t.gray}cd ${this.cwd}${t.reset}`),this.onChange(),!0}feed(e){let n=e.replace(/\r\n/g,`
153
153
  `),s=this.partial,i=this.parseState,o=this.parseSeq;for(let a of n){if(i==="osc"){(a==="\x07"||a==="\\"&&o.endsWith("\x1B"))&&(i="text"),o+=a;continue}if(i==="esc"){if(a==="["){i="csi",o="\x1B[";continue}if(a==="]"){i="osc",o="\x1B]";continue}i="text";continue}if(i==="csi"){o+=a,a>="@"&&a<="~"&&(a==="m"&&(s+=o),i="text");continue}if(a==="\x1B"){i="esc",o="\x1B";continue}if(a==="\r"){s="";continue}if(a===`
154
- `){this.pushLine(s),s="";continue}a==="\x07"||a==="\b"||(s+=a)}s.length>xs&&(this.pushLine(s),s=""),this.parseState=i,this.parseSeq=o,this.partial=s,this.onChange()}pushLine(e){this.lines.push(e),this.lines.length>Gr&&this.lines.splice(0,this.lines.length-Gr)}flushPartial(){this.partial&&(this.pushLine(this.partial),this.partial="")}},vs=["alias","cd","clear","cls","echo","exit","export","false","history","pwd","set","source","true","type","unalias","unset","which"],je=null;function ws(){if(je)return je;let r=new Set(vs);for(let e of(process.env.PATH??"").split(B.delimiter)){if(!e)continue;let n;try{n=fe.readdirSync(e)}catch{continue}for(let s of n)r.add(s)}return je=[...r].sort(),je}function Cs(r){if(r.length===0)return"";let e=r[0];for(let n of r.slice(1)){let s=0;for(;s<e.length&&s<n.length&&e[s]===n[s];)s++;if(e=e.slice(0,s),!e)break}return e}function Ss(r,e){return e?e==="~"||e.startsWith("~/")?B.join(qe.homedir(),e.slice(1)):B.isAbsolute(e)?e:B.resolve(r,e):r}function Xr(r,e){if(r.isDirectory())return!0;if(!r.isSymbolicLink())return!1;try{return fe.statSync(B.join(e,r.name)).isDirectory()}catch{return!1}}function Ts(r,e,n){let s=e.lastIndexOf("/"),i=s>=0?e.slice(0,s+1):"",o=e.slice(s+1),a=Ss(r,i),l;try{l=fe.readdirSync(a,{withFileTypes:!0})}catch{return{dirPart:i,base:o,names:[]}}let c=l.filter(u=>n&&!Xr(u,a)||u.name.startsWith(".")&&!o.startsWith(".")?!1:u.name.startsWith(o)).map(u=>u.name+(Xr(u,a)?"/":"")).sort();return{dirPart:i,base:o,names:c}}function Yr(r,e,n){let i=e.slice(0,n).replace(/\s+$/,""),o=i.lastIndexOf(" ")+1,a=i.slice(o),l=i.slice(0,o),c="",u=a.lastIndexOf("=");u>=0&&/^[A-Za-z_][A-Za-z0-9_]*$/.test(a.slice(0,u))&&(c=a.slice(0,u+1),a=a.slice(u+1));let d="";(a.startsWith('"')||a.startsWith("'"))&&(d=a[0],a=a.slice(1));let h=a.startsWith("./")||a.startsWith("../")||a.startsWith("/")||a.startsWith("~"),m=l.trim().length===0&&!h&&c.length===0,p=y=>`${c}${d}${y}`,f="",g;if(m)g=ws().filter(y=>y.startsWith(a));else{let y=l.trim().split(/\s+/)[0]??"",_=Ts(r,a,y==="cd");f=_.dirPart,g=_.names}if(g.length===0)return null;if(g.length===1){let y=f+g[0],_=y.endsWith("/"),C=d&&!_?d:"",F=_?"":" ",le=l+p(y)+C+F;return{line:le+e.slice(n),cursor:le.length,candidates:[]}}let $=m?a:a.slice(f.length),b=Cs(g);if(b.length>$.length){let y=l+p(f+b);return{line:y+e.slice(n),cursor:y.length,candidates:[]}}return{line:e,cursor:n,candidates:g.map(y=>p(f+y))}}function Ze(){try{process.stdout.write("\x1B[<u\x1B[>4m\x1B[?2004l\x1B[?1000l\x1B[?1006l\x1B[?7h"+t.reset+K.show+Fe.exitAlt)}catch{}try{process.stdin.isTTY&&process.stdin.isRaw&&process.stdin.setRawMode(!1)}catch{}}var Ge=class r{options;agent;markdown=new ze;cwd;modelProviderLabel;modelLabel;modelWeight;lines=[];input="";inputCursor=0;scrollOffset=0;statusText="\u5C31\u7EEA";statusDetail="";busy=!1;exited=!1;autoApprove;intentEnabled;history=[];historyIndex=-1;pendingConfirm;abortController;keyBuffer="";escTimer;projectDirCreated;needsRender=!1;mouseCapture=!1;lastAnswerStart=0;streamingPreview="";termVisible=!0;termFocus=!1;termScroll=0;lastLeftWidth=0;lastPhysicalCount=0;lastTermPhysicalCount=0;termInput="";termInputCursor=0;termHistory=[];termHistoryIndex=-1;term;onData=e=>this.handleData(e);onResize=()=>{this.lastPhysicalCount=-1,this.lastTermPhysicalCount=-1,this.scheduleRender()};constructor(e){this.options=e,this.cwd=e.cwd,this.autoApprove=e.autoApprove,this.intentEnabled=e.intentEnabled??e.config.intent?.enabled===!0,e.config.intent={...e.config.intent??{enabled:!1},enabled:this.intentEnabled},this.modelProviderLabel=e.providerLabel,this.modelLabel=e.model,this.modelWeight=e.modelWeight,this.term=new He(e.cwd,()=>this.scheduleRender());let n=pt(e.cwd);n.created&&(this.projectDirCreated=n.paths.dir);let s={onText:i=>this.handleTextDelta(i),onReasoning:()=>{},onToolStart:i=>this.handleToolStart(i),onToolEnd:(i,o)=>this.handleToolEnd(i,o),onStatus:(i,o)=>{this.statusText=i,this.statusDetail=o??"",this.scheduleRender()},onNotice:i=>this.handleNotice(i),onIntent:(i,o)=>this.handleIntent(i,o),onModelChange:i=>this.handleModelChange(i),confirm:i=>this.requestConfirm(i),onRoundLimit:i=>this.requestConfirm({tool:"round_limit",title:`\u5DF2\u8C03\u7528 ${i} \u8F6E\u5DE5\u5177\uFF0C\u662F\u5426\u7EE7\u7EED\uFF1F`,detail:`\u672C\u8F6E\u5BF9\u8BDD\u5DF2\u6267\u884C ${i} \u8F6E\u5DE5\u5177\u8C03\u7528\uFF08\u8FBE\u5230\u5F53\u524D\u4E0A\u9650\uFF09\uFF0C\u4EFB\u52A1\u53EF\u80FD\u5C1A\u672A\u5B8C\u6210\u3002
154
+ `){this.pushLine(s),s="";continue}a==="\x07"||a==="\b"||(s+=a)}s.length>xs&&(this.pushLine(s),s=""),this.parseState=i,this.parseSeq=o,this.partial=s,this.onChange()}pushLine(e){this.lines.push(e),this.lines.length>Gr&&this.lines.splice(0,this.lines.length-Gr)}flushPartial(){this.partial&&(this.pushLine(this.partial),this.partial="")}},vs=["alias","cd","clear","cls","echo","exit","export","false","history","pwd","set","source","true","type","unalias","unset","which"],ze=null;function ws(){if(ze)return ze;let r=new Set(vs);for(let e of(process.env.PATH??"").split(B.delimiter)){if(!e)continue;let n;try{n=fe.readdirSync(e)}catch{continue}for(let s of n)r.add(s)}return ze=[...r].sort(),ze}function Cs(r){if(r.length===0)return"";let e=r[0];for(let n of r.slice(1)){let s=0;for(;s<e.length&&s<n.length&&e[s]===n[s];)s++;if(e=e.slice(0,s),!e)break}return e}function Ss(r,e){return e?e==="~"||e.startsWith("~/")?B.join(qe.homedir(),e.slice(1)):B.isAbsolute(e)?e:B.resolve(r,e):r}function Xr(r,e){if(r.isDirectory())return!0;if(!r.isSymbolicLink())return!1;try{return fe.statSync(B.join(e,r.name)).isDirectory()}catch{return!1}}function Ts(r,e,n){let s=e.lastIndexOf("/"),i=s>=0?e.slice(0,s+1):"",o=e.slice(s+1),a=Ss(r,i),l;try{l=fe.readdirSync(a,{withFileTypes:!0})}catch{return{dirPart:i,base:o,names:[]}}let c=l.filter(u=>n&&!Xr(u,a)||u.name.startsWith(".")&&!o.startsWith(".")?!1:u.name.startsWith(o)).map(u=>u.name+(Xr(u,a)?"/":"")).sort();return{dirPart:i,base:o,names:c}}function Yr(r,e,n){let i=e.slice(0,n).replace(/\s+$/,""),o=i.lastIndexOf(" ")+1,a=i.slice(o),l=i.slice(0,o),c="",u=a.lastIndexOf("=");u>=0&&/^[A-Za-z_][A-Za-z0-9_]*$/.test(a.slice(0,u))&&(c=a.slice(0,u+1),a=a.slice(u+1));let d="";(a.startsWith('"')||a.startsWith("'"))&&(d=a[0],a=a.slice(1));let h=a.startsWith("./")||a.startsWith("../")||a.startsWith("/")||a.startsWith("~"),m=l.trim().length===0&&!h&&c.length===0,p=y=>`${c}${d}${y}`,f="",g;if(m)g=ws().filter(y=>y.startsWith(a));else{let y=l.trim().split(/\s+/)[0]??"",_=Ts(r,a,y==="cd");f=_.dirPart,g=_.names}if(g.length===0)return null;if(g.length===1){let y=f+g[0],_=y.endsWith("/"),C=d&&!_?d:"",F=_?"":" ",le=l+p(y)+C+F;return{line:le+e.slice(n),cursor:le.length,candidates:[]}}let $=m?a:a.slice(f.length),b=Cs(g);if(b.length>$.length){let y=l+p(f+b);return{line:y+e.slice(n),cursor:y.length,candidates:[]}}return{line:e,cursor:n,candidates:g.map(y=>p(f+y))}}function Ze(){try{process.stdout.write("\x1B[?2004l\x1B[>4m\x1B[?1000l\x1B[?1006l\x1B[?7h"+t.reset+K.show+Fe.exitAlt)}catch{}try{process.stdin.isTTY&&process.stdin.isRaw&&process.stdin.setRawMode(!1)}catch{}}var Ge=class r{options;agent;markdown=new je;cwd;modelProviderLabel;modelLabel;modelWeight;lines=[];input="";inputCursor=0;scrollOffset=0;statusText="\u5C31\u7EEA";statusDetail="";busy=!1;exited=!1;autoApprove;intentEnabled;history=[];historyIndex=-1;pendingConfirm;abortController;keyBuffer="";escTimer;projectDirCreated;needsRender=!1;mouseCapture=!1;lastAnswerStart=0;streamingPreview="";termVisible=!0;termFocus=!1;termScroll=0;lastLeftWidth=0;lastPhysicalCount=0;lastTermPhysicalCount=0;termInput="";termInputCursor=0;termHistory=[];termHistoryIndex=-1;term;onData=e=>this.handleData(e);onResize=()=>{this.lastPhysicalCount=-1,this.lastTermPhysicalCount=-1,this.scheduleRender()};constructor(e){this.options=e,this.cwd=e.cwd,this.autoApprove=e.autoApprove,this.intentEnabled=e.intentEnabled??e.config.intent?.enabled===!0,e.config.intent={...e.config.intent??{enabled:!1},enabled:this.intentEnabled},this.modelProviderLabel=e.providerLabel,this.modelLabel=e.model,this.modelWeight=e.modelWeight,this.term=new He(e.cwd,()=>this.scheduleRender());let n=pt(e.cwd);n.created&&(this.projectDirCreated=n.paths.dir);let s={onText:i=>this.handleTextDelta(i),onReasoning:()=>{},onToolStart:i=>this.handleToolStart(i),onToolEnd:(i,o)=>this.handleToolEnd(i,o),onStatus:(i,o)=>{this.statusText=i,this.statusDetail=o??"",this.scheduleRender()},onNotice:i=>this.handleNotice(i),onIntent:(i,o)=>this.handleIntent(i,o),onModelChange:i=>this.handleModelChange(i),confirm:i=>this.requestConfirm(i),onRoundLimit:i=>this.requestConfirm({tool:"round_limit",title:`\u5DF2\u8C03\u7528 ${i} \u8F6E\u5DE5\u5177\uFF0C\u662F\u5426\u7EE7\u7EED\uFF1F`,detail:`\u672C\u8F6E\u5BF9\u8BDD\u5DF2\u6267\u884C ${i} \u8F6E\u5DE5\u5177\u8C03\u7528\uFF08\u8FBE\u5230\u5F53\u524D\u4E0A\u9650\uFF09\uFF0C\u4EFB\u52A1\u53EF\u80FD\u5C1A\u672A\u5B8C\u6210\u3002
155
155
 
156
156
  \xB7 \u6309 y \u7EE7\u7EED\uFF1A\u518D\u8FFD\u52A0 ${this.options.config.maxToolRounds} \u8F6E
157
157
  \xB7 \u6309 n \u7ED3\u675F\uFF1A\u4FDD\u7559\u5DF2\u6709\u8FDB\u5C55\uFF0C\u672C\u8F6E\u5230\u6B64\u4E3A\u6B62
158
158
 
159
159
  \u60F3\u6C38\u4E45\u8C03\u6574\u4E0A\u9650\uFF1A\u7F16\u8F91\u914D\u7F6E\u6587\u4EF6\u7684 maxToolRounds \u5B57\u6BB5\u3002`,danger:!1})};this.agent=new Be(e.provider,e.config,e.cwd,s,e.activeModel)}async start(){if(this.enterTerminal(),this.options.startupNotice){for(let e of this.options.startupNotice.split(`
160
- `))this.lines.push(e);this.lines.push("")}this.projectDirCreated&&(this.lines.push(`${t.green}\u2713 \u5DF2\u521D\u59CB\u5316\u9879\u76EE\u76EE\u5F55 ${this.projectDirCreated}${t.reset}`),this.lines.push(`${t.gray} \xB7 memory/ \u9879\u76EE\u8BB0\u5FC6\uFF08\u7384\u732A\u5199\u5165\uFF0C\u8DE8\u4F1A\u8BDD\u590D\u7528\uFF09${t.reset}`),this.lines.push(`${t.gray} \xB7 rules.md \u9879\u76EE\u89C4\u5219\uFF08\u5185\u5BB9\u6CE8\u5165\u7CFB\u7EDF\u63D0\u793A\u8BCD\uFF09${t.reset}`),this.lines.push("")),this.scheduleRender(),await new Promise(e=>{this.resolveExit=e})}resolveExit;enterTerminal(){process.stdout.write(Fe.enterAlt+K.hide+"\x1B[?7l"+(this.mouseCapture?"\x1B[?1000h\x1B[?1006h":"\x1B[?1000l\x1B[?1006l")+"\x1B[?2004h"+this.extendedKeyRequest()),process.stdin.isTTY&&process.stdin.setRawMode(!0),process.stdin.resume(),process.stdin.setEncoding("utf8"),process.stdin.on("data",this.onData),process.stdout.on("resize",this.onResize)}flushStreamingPreview(){this.streamingPreview&&(this.lines.push(this.streamingPreview),this.streamingPreview="")}static MAX_OUTPUT_LINES=4e3;trimOutputLines(){let e=r.MAX_OUTPUT_LINES;this.lines.length>e&&this.lines.splice(0,this.lines.length-e)}exitTerminal(){this.escTimer&&(clearTimeout(this.escTimer),this.escTimer=void 0),process.stdin.off("data",this.onData),process.stdout.off("resize",this.onResize),process.stdin.isTTY&&process.stdin.setRawMode(!1),process.stdin.pause(),process.stdout.write("\x1B[?2004l\x1B[<u\x1B[>4m\x1B[?1000l\x1B[?1006l\x1B[?7h"+t.reset+K.show+Fe.exitAlt)}extendedKeyRequest(){let e=process.env.TERM??"",n=process.env.TERM_PROGRAM??"",s=!!process.env.KITTY_WINDOW_ID||/kitty|ghostty/i.test(e)||/kitty|ghostty/i.test(n),i="\x1B[>1u";return s||(i+="\x1B[>4;2m"),i}shutdown(){this.exited||(this.exited=!0,this.term.dispose(),this.exitTerminal(),this.resolveExit?.())}handleTextDelta(e){let n=this.markdown.push(pe(e));n.length>0&&this.lines.push(...n),this.streamingPreview=this.markdown.peek(),this.scheduleRender()}handleToolStart(e){this.flushStreamingPreview();let n=Rs(e);this.lines.push(`${t.magenta}\u23FA${t.reset} ${t.bold}${e.name}${t.reset} ${t.gray}${n}${t.reset}`),this.statusText=`\u6267\u884C ${e.name}`,this.scheduleRender()}handleToolEnd(e,n){this.flushStreamingPreview();let s=pe(n.summary??(n.ok?"\u5B8C\u6210":"\u5931\u8D25")).slice(0,60),i=n.denied?`${t.yellow}\u23BF \u5DF2\u62D2\u7EDD${t.reset}`:n.ok?`${t.green}\u23BF ${s}${t.reset}`:`${t.red}\u23BF ${s}${t.reset}`;this.lines.push(` ${i}`),this.statusText="\u601D\u8003\u4E2D",this.scheduleRender()}handleNotice(e){this.flushStreamingPreview();let n=pe(e);for(let s of n.split(`
160
+ `))this.lines.push(e);this.lines.push("")}this.projectDirCreated&&(this.lines.push(`${t.green}\u2713 \u5DF2\u521D\u59CB\u5316\u9879\u76EE\u76EE\u5F55 ${this.projectDirCreated}${t.reset}`),this.lines.push(`${t.gray} \xB7 memory/ \u9879\u76EE\u8BB0\u5FC6\uFF08\u7384\u732A\u5199\u5165\uFF0C\u8DE8\u4F1A\u8BDD\u590D\u7528\uFF09${t.reset}`),this.lines.push(`${t.gray} \xB7 rules.md \u9879\u76EE\u89C4\u5219\uFF08\u5185\u5BB9\u6CE8\u5165\u7CFB\u7EDF\u63D0\u793A\u8BCD\uFF09${t.reset}`),this.lines.push("")),this.scheduleRender(),await new Promise(e=>{this.resolveExit=e})}resolveExit;enterTerminal(){process.stdout.write(Fe.enterAlt+K.hide+"\x1B[?7l"+(this.mouseCapture?"\x1B[?1000h\x1B[?1006h":"\x1B[?1000l\x1B[?1006l")+"\x1B[?2004h"+this.extendedKeyRequest()),process.stdin.isTTY&&process.stdin.setRawMode(!0),process.stdin.resume(),process.stdin.setEncoding("utf8"),process.stdin.on("data",this.onData),process.stdout.on("resize",this.onResize)}flushStreamingPreview(){this.streamingPreview&&(this.lines.push(this.streamingPreview),this.streamingPreview="")}static MAX_OUTPUT_LINES=4e3;trimOutputLines(){let e=r.MAX_OUTPUT_LINES;this.lines.length>e&&this.lines.splice(0,this.lines.length-e)}exitTerminal(){this.escTimer&&(clearTimeout(this.escTimer),this.escTimer=void 0),process.stdin.off("data",this.onData),process.stdout.off("resize",this.onResize),process.stdin.isTTY&&process.stdin.setRawMode(!1),process.stdin.pause(),process.stdout.write("\x1B[?2004l\x1B[>4m\x1B[?1000l\x1B[?1006l\x1B[?7h"+t.reset+K.show+Fe.exitAlt)}extendedKeyRequest(){let e=process.env.TERM??"",n=process.env.TERM_PROGRAM??"";return process.env.KITTY_WINDOW_ID||/kitty|ghostty/i.test(e)||/kitty|ghostty/i.test(n)?"":"\x1B[>4;2m"}shutdown(){this.exited||(this.exited=!0,this.term.dispose(),this.exitTerminal(),this.resolveExit?.())}handleTextDelta(e){let n=this.markdown.push(pe(e));n.length>0&&this.lines.push(...n),this.streamingPreview=this.markdown.peek(),this.scheduleRender()}handleToolStart(e){this.flushStreamingPreview();let n=Rs(e);this.lines.push(`${t.magenta}\u23FA${t.reset} ${t.bold}${e.name}${t.reset} ${t.gray}${n}${t.reset}`),this.statusText=`\u6267\u884C ${e.name}`,this.scheduleRender()}handleToolEnd(e,n){this.flushStreamingPreview();let s=pe(n.summary??(n.ok?"\u5B8C\u6210":"\u5931\u8D25")).slice(0,60),i=n.denied?`${t.yellow}\u23BF \u5DF2\u62D2\u7EDD${t.reset}`:n.ok?`${t.green}\u23BF ${s}${t.reset}`:`${t.red}\u23BF ${s}${t.reset}`;this.lines.push(` ${i}`),this.statusText="\u601D\u8003\u4E2D",this.scheduleRender()}handleNotice(e){this.flushStreamingPreview();let n=pe(e);for(let s of n.split(`
161
161
  `))s.trim().length>0&&this.lines.push(`${t.gray} ${s}${t.reset}`);this.scheduleRender()}handleIntent(e,n){let o=pe(e).split(`
162
162
  `).map(a=>a.trim()).filter(a=>a.length>0);this.lines.push(`${t.gray}\u2301 \u610F\u56FE\u5206\u6790\u7ED3\u679C\uFF08\u5C06\u4F5C\u4E3A\u672C\u6B21\u8F93\u5165\uFF09${t.reset}`);for(let a of o.slice(0,14))this.lines.push(`${t.gray} ${a}${t.reset}`);o.length>14&&this.lines.push(`${t.gray} \u2026\uFF08\u5171 ${o.length} \u884C\uFF0C\u5B8C\u6574\u5185\u5BB9\u5DF2\u4F5C\u4E3A\u8F93\u5165\u53D1\u9001\uFF09${t.reset}`),this.lines.push(`${t.gray} \u539F\u59CB\u6D88\u606F\uFF1A${W(n.replace(/\s+/g," "),60)}${t.reset}`),this.scheduleRender()}handleModelChange(e){this.modelProviderLabel=J(e.provider)?.label??e.provider,this.modelLabel=e.model,this.modelWeight=e.weight,e.reason!=="\u8C03\u7528\u5931\u8D25\u964D\u6743"&&this.lines.push(`${t.yellow}\u21C4 \u6A21\u578B\u5DF2\u5207\u6362\u4E3A ${e.provider}/${e.model}\uFF08\u6743\u91CD ${e.weight}\uFF09${t.reset}`),this.scheduleRender()}requestConfirm(e){return new Promise(n=>{this.pendingConfirm={request:e,resolve:n,selected:!0},this.scheduleRender()})}handleData(e){this.keyBuffer+=e,this.drainKeys()}drainKeys(){for(;;){let e=this.readKey();if(e===null){this.keyBuffer.startsWith("\x1B")&&(this.escTimer&&clearTimeout(this.escTimer),this.escTimer=setTimeout(()=>{this.escTimer=void 0;let n=this.keyBuffer;this.keyBuffer="",n==="\x1B"&&this.handleKey("\x1B")},40));return}if(this.escTimer&&(clearTimeout(this.escTimer),this.escTimer=void 0),this.handleKey(e),this.exited)return}}readKey(){let e=this.keyBuffer;if(e.length===0)return null;if(e.startsWith("\x1B[200~")){let i=e.indexOf("\x1B[201~",6);return i===-1?null:(this.keyBuffer=e.slice(i+6),e.slice(0,i+6))}if(e[0]==="\x1B"){let i=/^\x1b\[[0-9:;<=>?]*[@-~]/.exec(e);if(i)return this.keyBuffer=e.slice(i[0].length),i[0];let o=/^\x1bO[A-Za-z]/.exec(e);return o?(this.keyBuffer=e.slice(o[0].length),o[0]):/^\x1b\[?[0-9:;<=>?]*$/.test(e)?null:e.length>=2?(this.keyBuffer=e.slice(2),e.slice(0,2)):null}let n=e.codePointAt(0);if(n===void 0)return null;let s=String.fromCodePoint(n);return this.keyBuffer=e.slice(s.length),s}handleKey(e){if(e.startsWith("\x1B[200~")&&e.endsWith("\x1B[201~")){let i=e.slice(6,e.length-6).replace(/\r\n?/g,`
163
163
  `).replace(/[\x00-\x08\x0b\x0c\x0e-\x1f\x7f]/g,"");i&&this.insertText(i);return}let n=Ms(e);if(this.pendingConfirm){this.handleConfirmKey(n);return}let s=/^\x1b\[<(\d+);(\d+);(\d+)([Mm])$/.exec(n);if(s){this.handleMouse(s);return}if(n==="\x1B[Z"){this.toggleFocus();return}if(this.termFocus){this.handleTermKey(n);return}switch(n){case"":this.busy?(this.abortController?.abort(),this.statusText="\u4E2D\u65AD\u4E2D\u2026",this.scheduleRender()):this.input.length>0?(this.input="",this.inputCursor=0,this.scheduleRender()):this.shutdown();return;case"":this.busy&&(this.abortController?.abort(),this.statusText="\u4E2D\u65AD\u4E2D\u2026",this.scheduleRender());return;case"\r":this.submit();return;case`
164
164
  `:this.insertText(`
165
165
  `);return;case"\x7F":case"\b":this.backspace();return;case"":this.inputCursor=0,this.scheduleRender();return;case"":this.inputCursor=this.input.length,this.scheduleRender();return;case"\f":this.lines=[],this.scrollOffset=0,this.scheduleRender();return;case"\x1B[A":this.navigateHistory(-1);return;case"\x1B[B":this.navigateHistory(1);return;case"\x1B[C":this.moveCursor(1);return;case"\x1B[D":this.moveCursor(-1);return;case"\x1B[5~":this.scrollBy(5);return;case"\x1B[6~":this.scrollBy(-5);return;case"\x1B[1;2A":this.scrollBy(1);return;case"\x1B[1;2B":this.scrollBy(-1);return;case"\x1B[H":case"\x1B[1~":case"\x1B[7~":this.scrollToTop();return;case"\x1B[F":case"\x1B[4~":case"\x1B[8~":this.scrollToBottom();return;default:n.charCodeAt(0)>=32&&this.insertText(n)}}handleMouse(e){let n=Number(e[1]),s=Number(e[2]),i=e[4]==="M",o=this.resolveTermWidth(process.stdout.columns||80),a=this.lastLeftWidth>0&&o>0&&s>this.lastLeftWidth+1;if(i&&(n&64)===64){let c=(n&1)===0?3:-3;a?this.scrollTermBy(c):this.scrollBy(c);return}i&&(n&3)===0&&(n&32)===0&&a!==this.termFocus&&(this.termFocus=a,this.scheduleRender())}toggleFocus(){if(!this.termVisible){this.lines.push(`${t.gray}\u7EC8\u7AEF\u9762\u677F\u5DF2\u9690\u85CF\uFF0C\u8F93\u5165 /term \u53EF\u91CD\u65B0\u663E\u793A\u3002${t.reset}`),this.scheduleRender();return}this.termFocus=!this.termFocus,this.scheduleRender()}handleTermKey(e){switch(e){case"":this.term.isRunning()&&(this.term.interrupt(),this.scheduleRender());return;case"":this.term.isRunning()?this.term.interrupt():this.termInput.length>0?(this.termInput="",this.termInputCursor=0):this.termFocus=!1,this.scheduleRender();return;case"\r":this.submitTermCommand();return;case" ":this.applyTermCompletion();return;case"\x7F":case"\b":this.termBackspace();return;case"":this.termInputCursor=0,this.scheduleRender();return;case"":this.termInputCursor=this.termInput.length,this.scheduleRender();return;case"\f":this.term.clear(),this.scrollTermToBottom();return;case"\x1B[A":this.navigateTermHistory(-1);return;case"\x1B[B":this.navigateTermHistory(1);return;case"\x1B[C":this.termInputCursor=Math.min(this.termInput.length,this.termInputCursor+1),this.scheduleRender();return;case"\x1B[D":this.termInputCursor=Math.max(0,this.termInputCursor-1),this.scheduleRender();return;case"\x1B[5~":this.scrollTermBy(5);return;case"\x1B[6~":this.scrollTermBy(-5);return;case"\x1B[1;2A":this.scrollTermBy(1);return;case"\x1B[1;2B":this.scrollTermBy(-1);return;case"\x1B[H":case"\x1B[1~":case"\x1B[7~":this.scrollTermToTop();return;case"\x1B[F":case"\x1B[4~":case"\x1B[8~":this.scrollTermToBottom();return;default:e.charCodeAt(0)>=32&&(this.termInput=this.termInput.slice(0,this.termInputCursor)+e+this.termInput.slice(this.termInputCursor),this.termInputCursor+=e.length,this.termScroll=0,this.scheduleRender())}}termBackspace(){if(this.termInputCursor<=0)return;let e=this.termInputCursor-1,n=this.termInput.charCodeAt(e);n>=56320&&n<=57343&&e>0&&(e-=1),this.termInput=this.termInput.slice(0,e)+this.termInput.slice(this.termInputCursor),this.termInputCursor=e,this.scheduleRender()}navigateTermHistory(e){this.termHistory.length!==0&&(this.termHistoryIndex===-1&&(this.termHistoryIndex=this.termHistory.length),this.termHistoryIndex+=e,this.termHistoryIndex<0&&(this.termHistoryIndex=0),this.termHistoryIndex>=this.termHistory.length?(this.termHistoryIndex=-1,this.termInput="",this.termInputCursor=0):(this.termInput=this.termHistory[this.termHistoryIndex],this.termInputCursor=this.termInput.length),this.scheduleRender())}submitTermCommand(){let e=this.termInput.trim();e&&(this.termInput="",this.termInputCursor=0,this.termScroll=0,this.termHistory.push(e),this.termHistoryIndex=-1,this.term.run(e),this.scheduleRender())}applyTermCompletion(){let e=Yr(this.term.getCwd(),this.termInput,this.termInputCursor);if(e){if(e.candidates.length>0){let s=e.candidates.slice(0,24),i=e.candidates.length>s.length?` \u2026\uFF08\u5171 ${e.candidates.length} \u9879\uFF09`:"";this.term.notice(`${t.gray}${s.join(" ")}${i}${t.reset}`)}else this.termInput=e.line,this.termInputCursor=e.cursor,this.termScroll=0;this.scheduleRender()}}handleConfirmKey(e){let n=this.pendingConfirm;if(n)switch(e){case"y":case"Y":case"\r":this.resolveConfirm(!0);return;case"":this.abortController?.abort(),this.resolveConfirm(!1);return;case"n":case"N":case"\x1B":case"":this.resolveConfirm(!1);return;case"\x1B[C":case"\x1B[D":case" ":n.selected=!n.selected,this.scheduleRender();return;default:return}}resolveConfirm(e){let n=this.pendingConfirm;if(!n)return;this.pendingConfirm=void 0;let s=e?"\u5DF2\u5141\u8BB8":"\u5DF2\u62D2\u7EDD";this.lines.push(` ${e?t.green:t.yellow}${s}${t.reset}`),n.resolve(e),this.scheduleRender()}insertText(e){this.input=this.input.slice(0,this.inputCursor)+e+this.input.slice(this.inputCursor),this.inputCursor+=e.length,this.scrollOffset=0,this.scheduleRender()}backspace(){if(this.inputCursor<=0)return;let e=this.inputCursor-1,n=this.input.charCodeAt(e);n>=56320&&n<=57343&&e>0&&(e-=1),this.input=this.input.slice(0,e)+this.input.slice(this.inputCursor),this.inputCursor=e,this.scheduleRender()}moveCursor(e){let n=this.inputCursor+e;this.inputCursor=Math.max(0,Math.min(this.input.length,n)),this.scheduleRender()}navigateHistory(e){this.history.length!==0&&(this.historyIndex===-1&&(this.historyIndex=this.history.length),this.historyIndex+=e,this.historyIndex<0&&(this.historyIndex=0),this.historyIndex>=this.history.length?(this.historyIndex=-1,this.input="",this.inputCursor=0):(this.input=this.history[this.historyIndex],this.inputCursor=this.input.length),this.scheduleRender())}scrollBy(e){this.setScrollOffset(this.scrollOffset+e)}setScrollOffset(e){this.scrollOffset=Math.max(0,Math.round(e)),this.scheduleRender()}scrollToTop(){this.setScrollOffset(Number.MAX_SAFE_INTEGER)}scrollToBottom(){this.setScrollOffset(0)}scrollTermBy(e){this.termScroll=Math.max(0,Math.round(this.termScroll+e)),this.scheduleRender()}scrollTermToTop(){this.termScroll=Number.MAX_SAFE_INTEGER,this.scheduleRender()}scrollTermToBottom(){this.termScroll=0,this.scheduleRender()}async submit(){if(this.busy)return;let e=this.input.trim();if(!e)return;if(this.input="",this.inputCursor=0,this.scrollOffset=0,this.history.push(e),this.historyIndex=-1,e.startsWith("/")){this.handleCommand(e);return}this.lastAnswerStart=this.lines.length;let n=`${t.bgGreen}${t.black}${t.bold} \u276F ${t.reset}`,[s,...i]=e.split(`
166
166
  `);this.lines.push(`${n} ${t.brightGreen}${t.bold}${s}${t.reset}`);for(let a of i)this.lines.push(`${t.brightGreen}${t.bold}\u2502${t.reset} ${t.brightGreen}${a}${t.reset}`);this.lines.push(""),this.busy=!0,this.statusText="\u601D\u8003\u4E2D",this.statusDetail="",this.abortController=new AbortController,this.scheduleRender();let o={label:this.modelLabel,weight:this.modelWeight};try{await this.agent.run(e,this.abortController.signal)}catch(a){let[l,...c]=G(a).split(`
167
- `);this.lines.push(`${t.red}\u2717 ${l}${t.reset}`);for(let d of c)this.lines.push(`${t.gray} ${d}${t.reset}`);let u=o.weight!==void 0?`\uFF08\u6743\u91CD ${o.weight}\uFF09`:"";this.lines.push(`${t.gray} \u672C\u6B21\u8BF7\u6C42\u4F7F\u7528\u7684\u6A21\u578B\uFF1A${this.modelProviderLabel} / ${o.label}${u}${t.reset}`)}finally{let a=this.markdown.flush();a.length>0&&this.lines.push(...a),this.streamingPreview="",this.lines.push(""),this.busy=!1,this.abortController=void 0,this.statusText="\u5C31\u7EEA",this.scheduleRender()}}handleCommand(e){let[n,...s]=e.slice(1).split(/\s+/),i=s.join(" ");switch(n){case"help":case"h":this.printHelp();break;case"clear":this.lines=[];break;case"copy":this.copyOutput(i);break;case"mouse":this.toggleMouseCapture();break;case"reset":this.agent.reset(),this.lines.push(`${t.gray}\u4F1A\u8BDD\u5DF2\u91CD\u7F6E\u3002${t.reset}`);break;case"exit":case"quit":case"q":this.shutdown();return;case"cwd":this.lines.push(`${t.gray}${this.cwd}${t.reset}`);break;case"switch":{i?this.switchDirectory(i):(this.lines.push(`${t.gray}\u5F53\u524D\u76EE\u5F55\uFF1A${this.cwd}${t.reset}`),this.lines.push(`${t.gray}\u7528\u6CD5\uFF1A/switch <\u76EE\u5F55\u8DEF\u5F84>${t.reset}`));break}case"term":{i==="clear"?this.term.clear():i==="focus"?(this.termVisible=!0,this.termFocus=!0):(this.termVisible=!this.termVisible,this.termVisible||(this.termFocus=!1),this.lines.push(`${t.gray}\u7EC8\u7AEF\u9762\u677F\uFF1A${this.termVisible?"\u663E\u793A":"\u9690\u85CF"}\uFF08Shift+Tab \u6216\u9F20\u6807\u70B9\u51FB\u5207\u6362\u7126\u70B9\uFF09${t.reset}`));break}case"auto":{let o=i==="on"?!0:i==="off"?!1:!this.autoApprove;this.autoApprove=o,this.options.config.autoApprove=o;try{M(this.options.config)}catch(a){this.lines.push(`${t.yellow}\u26A0 \u914D\u7F6E\u4FDD\u5B58\u5931\u8D25\uFF1A${a instanceof Error?a.message:String(a)}${t.reset}`)}this.lines.push(o?`${t.gray}\u81EA\u52A8\u6279\u51C6\uFF1A${t.brightYellow}\u5F00${t.reset}${t.gray} \u2014\u2014 \u6240\u6709\u5DE5\u5177\uFF08\u542B\u5199\u6587\u4EF6\u3001\u6267\u884C\u547D\u4EE4\uFF09\u5C06\u76F4\u63A5\u6267\u884C\uFF0C\u4E0D\u518D\u8BE2\u95EE\uFF1B\u5DF2\u5199\u5165\u914D\u7F6E\u3002${t.reset}`:`${t.gray}\u81EA\u52A8\u6279\u51C6\uFF1A\u5173 \u2014\u2014 \u5199\u6587\u4EF6 / \u7F16\u8F91\u6587\u4EF6 / \u6267\u884C\u547D\u4EE4\u524D\u4F1A\u8BF7\u6C42\u786E\u8BA4\uFF1B\u5DF2\u5199\u5165\u914D\u7F6E\u3002${t.reset}`);break}case"intent":{let o=i==="on"?!0:i==="off"?!1:!this.intentEnabled;this.intentEnabled=o,this.options.config.intent={...this.options.config.intent??{enabled:!1},enabled:o};try{M(this.options.config)}catch(a){this.lines.push(`${t.yellow}\u26A0 \u914D\u7F6E\u4FDD\u5B58\u5931\u8D25\uFF1A${a instanceof Error?a.message:String(a)}${t.reset}`)}this.lines.push(o?`${t.gray}\u610F\u56FE\u5206\u6790\uFF1A${t.brightGreen}\u5F00${t.reset}${t.gray} \u2014\u2014 \u6BCF\u6761\u6D88\u606F\u4F1A\u5148\u7528\u6A21\u677F\u5305\u88F9\u5E76\u8BF7\u6C42\u6A21\u578B\uFF0C\u8FD4\u56DE\u7684\u5206\u6790\u7ED3\u679C\u4F5C\u4E3A\u672C\u6B21\u8F93\u5165\uFF1B\u5DF2\u5199\u5165\u914D\u7F6E\u3002${t.reset}`:`${t.gray}\u610F\u56FE\u5206\u6790\uFF1A\u5173 \u2014\u2014 \u76F4\u63A5\u4F7F\u7528\u4F60\u8F93\u5165\u7684\u5185\u5BB9\uFF1B\u5DF2\u5199\u5165\u914D\u7F6E\u3002${t.reset}`);break}case"model":{let o=k(this.options.config).sort((l,c)=>c.weight-l.weight),a=this.modelWeight!==void 0?`\uFF08\u6743\u91CD ${this.modelWeight}\uFF09`:"";if(this.lines.push(`${t.gray}\u5F53\u524D\u6A21\u578B\uFF1A${t.reset}${this.modelProviderLabel} / ${this.modelLabel}${t.gray}${a}${t.reset}`),o.length>0){this.lines.push(`${t.gray}\u5DF2\u914D\u7F6E\u6A21\u578B\uFF08\u6309\u6743\u91CD\u964D\u5E8F\uFF09\uFF1A${t.reset}`);for(let l of o){let c=l.model===this.modelLabel?" \u2190 \u5F53\u524D":"";this.lines.push(`${t.gray} \xB7 ${l.id}\u3000\u6743\u91CD ${l.weight}${c}${t.reset}`)}}this.lines.push(`${t.gray}\u7BA1\u7406\u6A21\u578B\uFF08\u65B0\u589E / \u5220\u9664 / \u8C03\u6574\u6743\u91CD\uFF09\uFF1A${t.reset}xzh model${t.reset}`);break}default:this.lines.push(`${t.yellow}\u672A\u77E5\u547D\u4EE4\uFF1A/${n}\uFF0C\u8F93\u5165 /help \u67E5\u770B\u5E2E\u52A9\u3002${t.reset}`)}this.scheduleRender()}switchDirectory(e){let n=j(e.trim()),s=Xe.isAbsolute(n)?n:Xe.resolve(this.cwd,n);if(!ge.existsSync(s)){this.lines.push(`${t.red}\u2717 \u76EE\u5F55\u4E0D\u5B58\u5728\uFF1A${s}${t.reset}`),this.scheduleRender();return}if(!ge.statSync(s).isDirectory()){this.lines.push(`${t.red}\u2717 \u4E0D\u662F\u76EE\u5F55\uFF1A${s}${t.reset}`),this.scheduleRender();return}let i=s;try{i=ge.realpathSync(s)}catch{}this.cwd=i;let o=pt(i);this.agent.setCwd(i),this.term.setCwd(i),this.termScroll=0,this.lines.push(`${t.green}\u2713 \u5DF2\u5207\u6362\u9879\u76EE\u76EE\u5F55\uFF1A${i}${t.reset}`),o.created&&this.lines.push(`${t.gray} \u5DF2\u521D\u59CB\u5316\u9879\u76EE\u76EE\u5F55 ${o.paths.dir}\uFF08memory/ \u8BB0\u5FC6 \xB7 rules.md \u89C4\u5219\uFF09${t.reset}`),this.lines.push(`${t.gray} \u540E\u7EED\u6587\u4EF6\u4E0E\u547D\u4EE4\u64CD\u4F5C\u5C06\u57FA\u4E8E\u8BE5\u76EE\u5F55\uFF1B\u5BF9\u8BDD\u4E0A\u4E0B\u6587\u5DF2\u4FDD\u7559\uFF08\u53EF\u7528 /reset \u6E05\u7A7A\uFF09\u3002${t.reset}`),this.scheduleRender()}toggleMouseCapture(){this.mouseCapture=!this.mouseCapture,process.stdout.write(this.mouseCapture?"\x1B[?1000h\x1B[?1006h":"\x1B[?1000l\x1B[?1006l"),this.lines.push(this.mouseCapture?`${t.gray}\u9F20\u6807\u6355\u83B7\uFF1A${t.reset}\u5F00 ${t.gray}\u2014 \u6EDA\u8F6E\u53EF\u6EDA\u52A8\u8F93\u51FA\u533A\u3002\u4F46\u53F3\u952E\u83DC\u5355\u4E0E\u539F\u751F\u6846\u9009\u4F1A\u88AB\u63A5\u7BA1\uFF0C\u8981\u9009\u6587\u672C\u8BF7\u6309\u4F4F ${t.reset}Shift${t.gray} \u62D6\u62FD\u3002\u518D\u8F93\u5165 ${t.reset}/mouse${t.gray} \u5173\u95ED\u3002${t.reset}`:`${t.gray}\u9F20\u6807\u6355\u83B7\uFF1A${t.reset}${t.brightGreen}\u5173${t.reset}${t.gray} \u2014 \u9F20\u6807\u5168\u90E8\u529F\u80FD\u53EF\u7528\uFF08\u53F3\u952E\u83DC\u5355\u3001\u6846\u9009\u590D\u5236\u3001\u6EDA\u8F6E\u7531\u7EC8\u7AEF\u5904\u7406\uFF09\u3002\u6EDA\u52A8\u8F93\u51FA\u533A\u8BF7\u7528 ${t.reset}PgUp/PgDn${t.gray}\uFF0C\u6216\u7528 ${t.reset}/mouse${t.gray} \u6362\u56DE\u6EDA\u8F6E\u3002${t.reset}`)}copyOutput(e){let n=e.trim().toLowerCase(),s=this.lines.length,i,o;if(n==="all")i=this.lines.slice(),o="\u5168\u90E8";else if(n==="last")i=this.lines.slice(Math.min(this.lastAnswerStart,s)),o="\u6700\u8FD1\u4E00\u6B21\u5BF9\u8BDD";else if(/^\d+-\d+$/.test(n)){let[u,d]=n.split("-"),h=Math.max(1,Number(u)),m=Math.min(s,Number(d));if(h>m){this.lines.push(`${t.yellow}\u884C\u53F7\u8303\u56F4\u65E0\u6548\uFF1A${n}${t.reset}${t.gray}\uFF08\u8F93\u51FA\u533A\u5F53\u524D ${s} \u884C\uFF09${t.reset}`);return}i=this.lines.slice(h-1,m),o=`\u7B2C ${h}-${m} \u884C`}else{let u=/^\d+$/.test(n)?Number(n):30;i=this.lines.slice(-Math.max(1,u)),o=`\u6700\u8FD1 ${Math.min(Math.max(1,u),s)} \u884C`}let a=pe(i.join(`
167
+ `);this.lines.push(`${t.red}\u2717 ${l}${t.reset}`);for(let d of c)this.lines.push(`${t.gray} ${d}${t.reset}`);let u=o.weight!==void 0?`\uFF08\u6743\u91CD ${o.weight}\uFF09`:"";this.lines.push(`${t.gray} \u672C\u6B21\u8BF7\u6C42\u4F7F\u7528\u7684\u6A21\u578B\uFF1A${this.modelProviderLabel} / ${o.label}${u}${t.reset}`)}finally{let a=this.markdown.flush();a.length>0&&this.lines.push(...a),this.streamingPreview="",this.lines.push(""),this.busy=!1,this.abortController=void 0,this.statusText="\u5C31\u7EEA",this.scheduleRender()}}handleCommand(e){let[n,...s]=e.slice(1).split(/\s+/),i=s.join(" ");switch(n){case"help":case"h":this.printHelp();break;case"clear":this.lines=[];break;case"copy":this.copyOutput(i);break;case"mouse":this.toggleMouseCapture();break;case"reset":this.agent.reset(),this.lines.push(`${t.gray}\u4F1A\u8BDD\u5DF2\u91CD\u7F6E\u3002${t.reset}`);break;case"exit":case"quit":case"q":this.shutdown();return;case"cwd":this.lines.push(`${t.gray}${this.cwd}${t.reset}`);break;case"switch":{i?this.switchDirectory(i):(this.lines.push(`${t.gray}\u5F53\u524D\u76EE\u5F55\uFF1A${this.cwd}${t.reset}`),this.lines.push(`${t.gray}\u7528\u6CD5\uFF1A/switch <\u76EE\u5F55\u8DEF\u5F84>${t.reset}`));break}case"term":{i==="clear"?this.term.clear():i==="focus"?(this.termVisible=!0,this.termFocus=!0):(this.termVisible=!this.termVisible,this.termVisible||(this.termFocus=!1),this.lines.push(`${t.gray}\u7EC8\u7AEF\u9762\u677F\uFF1A${this.termVisible?"\u663E\u793A":"\u9690\u85CF"}\uFF08Shift+Tab \u6216\u9F20\u6807\u70B9\u51FB\u5207\u6362\u7126\u70B9\uFF09${t.reset}`));break}case"auto":{let o=i==="on"?!0:i==="off"?!1:!this.autoApprove;this.autoApprove=o,this.options.config.autoApprove=o;try{M(this.options.config)}catch(a){this.lines.push(`${t.yellow}\u26A0 \u914D\u7F6E\u4FDD\u5B58\u5931\u8D25\uFF1A${a instanceof Error?a.message:String(a)}${t.reset}`)}this.lines.push(o?`${t.gray}\u81EA\u52A8\u6279\u51C6\uFF1A${t.brightYellow}\u5F00${t.reset}${t.gray} \u2014\u2014 \u6240\u6709\u5DE5\u5177\uFF08\u542B\u5199\u6587\u4EF6\u3001\u6267\u884C\u547D\u4EE4\uFF09\u5C06\u76F4\u63A5\u6267\u884C\uFF0C\u4E0D\u518D\u8BE2\u95EE\uFF1B\u5DF2\u5199\u5165\u914D\u7F6E\u3002${t.reset}`:`${t.gray}\u81EA\u52A8\u6279\u51C6\uFF1A\u5173 \u2014\u2014 \u5199\u6587\u4EF6 / \u7F16\u8F91\u6587\u4EF6 / \u6267\u884C\u547D\u4EE4\u524D\u4F1A\u8BF7\u6C42\u786E\u8BA4\uFF1B\u5DF2\u5199\u5165\u914D\u7F6E\u3002${t.reset}`);break}case"intent":{let o=i==="on"?!0:i==="off"?!1:!this.intentEnabled;this.intentEnabled=o,this.options.config.intent={...this.options.config.intent??{enabled:!1},enabled:o};try{M(this.options.config)}catch(a){this.lines.push(`${t.yellow}\u26A0 \u914D\u7F6E\u4FDD\u5B58\u5931\u8D25\uFF1A${a instanceof Error?a.message:String(a)}${t.reset}`)}this.lines.push(o?`${t.gray}\u610F\u56FE\u5206\u6790\uFF1A${t.brightGreen}\u5F00${t.reset}${t.gray} \u2014\u2014 \u6BCF\u6761\u6D88\u606F\u4F1A\u5148\u7528\u6A21\u677F\u5305\u88F9\u5E76\u8BF7\u6C42\u6A21\u578B\uFF0C\u8FD4\u56DE\u7684\u5206\u6790\u7ED3\u679C\u4F5C\u4E3A\u672C\u6B21\u8F93\u5165\uFF1B\u5DF2\u5199\u5165\u914D\u7F6E\u3002${t.reset}`:`${t.gray}\u610F\u56FE\u5206\u6790\uFF1A\u5173 \u2014\u2014 \u76F4\u63A5\u4F7F\u7528\u4F60\u8F93\u5165\u7684\u5185\u5BB9\uFF1B\u5DF2\u5199\u5165\u914D\u7F6E\u3002${t.reset}`);break}case"model":{let o=k(this.options.config).sort((l,c)=>c.weight-l.weight),a=this.modelWeight!==void 0?`\uFF08\u6743\u91CD ${this.modelWeight}\uFF09`:"";if(this.lines.push(`${t.gray}\u5F53\u524D\u6A21\u578B\uFF1A${t.reset}${this.modelProviderLabel} / ${this.modelLabel}${t.gray}${a}${t.reset}`),o.length>0){this.lines.push(`${t.gray}\u5DF2\u914D\u7F6E\u6A21\u578B\uFF08\u6309\u6743\u91CD\u964D\u5E8F\uFF09\uFF1A${t.reset}`);for(let l of o){let c=l.model===this.modelLabel?" \u2190 \u5F53\u524D":"";this.lines.push(`${t.gray} \xB7 ${l.id}\u3000\u6743\u91CD ${l.weight}${c}${t.reset}`)}}this.lines.push(`${t.gray}\u7BA1\u7406\u6A21\u578B\uFF08\u65B0\u589E / \u5220\u9664 / \u8C03\u6574\u6743\u91CD\uFF09\uFF1A${t.reset}xzh model${t.reset}`);break}default:this.lines.push(`${t.yellow}\u672A\u77E5\u547D\u4EE4\uFF1A/${n}\uFF0C\u8F93\u5165 /help \u67E5\u770B\u5E2E\u52A9\u3002${t.reset}`)}this.scheduleRender()}switchDirectory(e){let n=z(e.trim()),s=Xe.isAbsolute(n)?n:Xe.resolve(this.cwd,n);if(!ge.existsSync(s)){this.lines.push(`${t.red}\u2717 \u76EE\u5F55\u4E0D\u5B58\u5728\uFF1A${s}${t.reset}`),this.scheduleRender();return}if(!ge.statSync(s).isDirectory()){this.lines.push(`${t.red}\u2717 \u4E0D\u662F\u76EE\u5F55\uFF1A${s}${t.reset}`),this.scheduleRender();return}let i=s;try{i=ge.realpathSync(s)}catch{}this.cwd=i;let o=pt(i);this.agent.setCwd(i),this.term.setCwd(i),this.termScroll=0,this.lines.push(`${t.green}\u2713 \u5DF2\u5207\u6362\u9879\u76EE\u76EE\u5F55\uFF1A${i}${t.reset}`),o.created&&this.lines.push(`${t.gray} \u5DF2\u521D\u59CB\u5316\u9879\u76EE\u76EE\u5F55 ${o.paths.dir}\uFF08memory/ \u8BB0\u5FC6 \xB7 rules.md \u89C4\u5219\uFF09${t.reset}`),this.lines.push(`${t.gray} \u540E\u7EED\u6587\u4EF6\u4E0E\u547D\u4EE4\u64CD\u4F5C\u5C06\u57FA\u4E8E\u8BE5\u76EE\u5F55\uFF1B\u5BF9\u8BDD\u4E0A\u4E0B\u6587\u5DF2\u4FDD\u7559\uFF08\u53EF\u7528 /reset \u6E05\u7A7A\uFF09\u3002${t.reset}`),this.scheduleRender()}toggleMouseCapture(){this.mouseCapture=!this.mouseCapture,process.stdout.write(this.mouseCapture?"\x1B[?1000h\x1B[?1006h":"\x1B[?1000l\x1B[?1006l"),this.lines.push(this.mouseCapture?`${t.gray}\u9F20\u6807\u6355\u83B7\uFF1A${t.reset}\u5F00 ${t.gray}\u2014 \u6EDA\u8F6E\u53EF\u6EDA\u52A8\u8F93\u51FA\u533A\u3002\u4F46\u53F3\u952E\u83DC\u5355\u4E0E\u539F\u751F\u6846\u9009\u4F1A\u88AB\u63A5\u7BA1\uFF0C\u8981\u9009\u6587\u672C\u8BF7\u6309\u4F4F ${t.reset}Shift${t.gray} \u62D6\u62FD\u3002\u518D\u8F93\u5165 ${t.reset}/mouse${t.gray} \u5173\u95ED\u3002${t.reset}`:`${t.gray}\u9F20\u6807\u6355\u83B7\uFF1A${t.reset}${t.brightGreen}\u5173${t.reset}${t.gray} \u2014 \u9F20\u6807\u5168\u90E8\u529F\u80FD\u53EF\u7528\uFF08\u53F3\u952E\u83DC\u5355\u3001\u6846\u9009\u590D\u5236\u3001\u6EDA\u8F6E\u7531\u7EC8\u7AEF\u5904\u7406\uFF09\u3002\u6EDA\u52A8\u8F93\u51FA\u533A\u8BF7\u7528 ${t.reset}PgUp/PgDn${t.gray}\uFF0C\u6216\u7528 ${t.reset}/mouse${t.gray} \u6362\u56DE\u6EDA\u8F6E\u3002${t.reset}`)}copyOutput(e){let n=e.trim().toLowerCase(),s=this.lines.length,i,o;if(n==="all")i=this.lines.slice(),o="\u5168\u90E8";else if(n==="last")i=this.lines.slice(Math.min(this.lastAnswerStart,s)),o="\u6700\u8FD1\u4E00\u6B21\u5BF9\u8BDD";else if(/^\d+-\d+$/.test(n)){let[u,d]=n.split("-"),h=Math.max(1,Number(u)),m=Math.min(s,Number(d));if(h>m){this.lines.push(`${t.yellow}\u884C\u53F7\u8303\u56F4\u65E0\u6548\uFF1A${n}${t.reset}${t.gray}\uFF08\u8F93\u51FA\u533A\u5F53\u524D ${s} \u884C\uFF09${t.reset}`);return}i=this.lines.slice(h-1,m),o=`\u7B2C ${h}-${m} \u884C`}else{let u=/^\d+$/.test(n)?Number(n):30;i=this.lines.slice(-Math.max(1,u)),o=`\u6700\u8FD1 ${Math.min(Math.max(1,u),s)} \u884C`}let a=pe(i.join(`
168
168
  `)).trim();if(!a){let u=n==="last"&&s===0?"\uFF0C\u5C1A\u672A\u8FDB\u884C\u8FC7\u5BF9\u8BDD":"";this.lines.push(`${t.yellow}\u6CA1\u6709\u53EF\u590D\u5236\u7684\u5185\u5BB9${t.reset}${t.gray}\uFF08\u8F93\u51FA\u533A\u5F53\u524D ${s} \u884C${u}\uFF09\u3002${t.reset}`);return}let l=Buffer.from(a,"utf8").toString("base64");process.stdout.write(`\x1B]52;c;${l}\x07`);let c=a.split(`
169
- `).length;this.lines.push(`${t.green}\u2713 \u5DF2\u590D\u5236${o}\uFF08${c} \u884C\uFF09\u5230\u526A\u8D34\u677F${t.reset}${t.gray}\uFF08OSC 52\uFF09\u3002\u82E5\u7C98\u8D34\u51FA\u6765\u662F\u7A7A\u7684\uFF0C\u8BF4\u660E\u672C\u7EC8\u7AEF\u7981\u7528\u4E86\u8BE5\u5E8F\u5217\uFF0C\u8BF7\u6539\u7528 ${t.reset}Shift${t.gray}+\u9F20\u6807\u62D6\u62FD\u9009\u62E9\u3002${t.reset}`)}printHelp(){let e=[`${t.bold}\u7384\u732A \u5185\u7F6E\u547D\u4EE4${t.reset}`," /help \u663E\u793A\u672C\u5E2E\u52A9"," /clear \u6E05\u7A7A\u5C4F\u5E55"," /copy [N|A-B|last|all] \u590D\u5236\u8F93\u51FA\u533A\u5230\u7CFB\u7EDF\u526A\u8D34\u677F\uFF08\u9ED8\u8BA4\u6700\u8FD1 30 \u884C\uFF09"," /mouse \u5207\u6362\u9F20\u6807\u6355\u83B7\uFF1A\u5173\u95ED\u540E\u53EF\u7528\u9F20\u6807\u76F4\u63A5\u6846\u9009\u590D\u5236"," /reset \u91CD\u7F6E\u5BF9\u8BDD\u4E0A\u4E0B\u6587"," /cwd \u663E\u793A\u5F53\u524D\u5DE5\u4F5C\u76EE\u5F55"," /switch <\u76EE\u5F55> \u5207\u6362\u5F53\u524D\u5BF9\u8BDD\u7684\u9879\u76EE\u76EE\u5F55"," /term \u663E\u793A/\u9690\u85CF\u53F3\u4FA7\u7EC8\u7AEF\u9762\u677F\uFF08/term clear \u6E05\u7A7A\u8F93\u51FA\uFF09"," /model \u663E\u793A\u5F53\u524D\u6A21\u578B\u4E0E\u6743\u91CD\u3001\u5DF2\u914D\u7F6E\u6A21\u578B\u5217\u8868"," /auto [on|off] \u5207\u6362\u81EA\u52A8\u6279\u51C6\uFF08on\uFF1A\u6240\u6709\u5DE5\u5177\u76F4\u63A5\u6267\u884C\uFF0C\u5199\u5165\u914D\u7F6E\uFF09"," /intent [on|off] \u5207\u6362\u610F\u56FE\u5206\u6790\uFF08on\uFF1A\u6BCF\u6761\u6D88\u606F\u5148\u505A\u610F\u56FE\u5206\u6790\u518D\u5904\u7406\uFF0C\u5199\u5165\u914D\u7F6E\uFF09"," /exit \u9000\u51FA\u7384\u732A",`${t.bold}\u5FEB\u6377\u952E${t.reset}`,` Enter \u63D0\u4EA4 \xB7 ${t.bold}Alt+Enter${t.reset} \u6362\u884C\uFF08Ctrl+J \u4EA6\u53EF\uFF1BShift/Ctrl+Enter \u89C6\u7EC8\u7AEF\u800C\u5B9A\uFF09`," \u2191/\u2193 \u5386\u53F2 \xB7 Ctrl+A/Ctrl+E \u884C\u9996/\u884C\u5C3E \xB7 PgUp/PgDn \u7FFB\u9875 \xB7 Shift+\u2191/\u2193 \u9010\u884C"," Home/End \u8DF3\u5230\u6700\u65E9/\u6700\u65B0",` ${t.gray}\u9F20\u6807${t.reset}\uFF1A\u9ED8\u8BA4\u4EA4\u8FD8\u7EC8\u7AEF \u2014\u2014 \u53F3\u952E\u83DC\u5355\u3001\u6846\u9009\u590D\u5236\u3001\u4E2D\u952E\u7C98\u8D34\u90FD\u53EF\u7528\uFF1B\u6EDA\u52A8\u8F93\u51FA\u533A\u7528 ${t.reset}PgUp/PgDn${t.gray}\u3002`,` \u8F93\u5165 ${t.reset}/mouse${t.gray} \u53EF\u6539\u4E3A\u8BA9\u7384\u732A\u63A5\u7BA1\u6EDA\u8F6E\uFF08\u6B64\u65F6\u7528 ${t.reset}Shift+\u62D6\u62FD${t.gray} \u9009\u62E9\u6587\u672C\uFF09\u3002${t.reset}`,` ${t.gray}\u7C98\u8D34${t.reset}\uFF1A\u652F\u6301\u591A\u884C\u6574\u6BB5\u7C98\u8D34\uFF08\u4E0D\u4F1A\u9010\u884C\u63D0\u4EA4\uFF09\uFF1B\u4E5F\u53EF\u7528 ${t.reset}/copy${t.gray} \u590D\u5236\u8F93\u51FA\u533A\u3002${t.reset}`," Shift+Tab \u5207\u6362\u7126\u70B9\uFF08\u5BF9\u8BDD \u21C4 \u53F3\u4FA7\u7EC8\u7AEF\uFF09\xB7 \u9F20\u6807\u70B9\u51FB\u4EA6\u53EF"," Ctrl+C \u4E2D\u65AD\u4EFB\u52A1 / \u6E05\u7A7A\u8F93\u5165 / \u9000\u51FA\uFF08\u7A7A\u95F2\u4E14\u8F93\u5165\u4E3A\u7A7A\u65F6\u9000\u51FA\uFF09"," Ctrl+D \u7ED3\u675F\u672C\u8F6E\u6B63\u5728\u8FDB\u884C\u7684\u5BF9\u8BDD \xB7 \u4EFB\u4F55\u60C5\u51B5\u4E0B\u90FD\u4E0D\u4F1A\u9000\u51FA\u7384\u732A"," Ctrl+L \u6E05\u5C4F"," \u6CE8\uFF1AVS Code / CodeBuddy \u7B49\u5185\u7F6E\u7EC8\u7AEF\u628A Shift/Ctrl+Enter \u7F16\u6210\u4E0E Enter \u76F8\u540C\u7684\u5B57\u8282\uFF0C",` \u8FD9\u7C7B\u73AF\u5883\u8BF7\u7528 ${t.bold}Alt+Enter${t.reset} \u6216 Ctrl+J\uFF1B\u652F\u6301 kitty \u952E\u76D8\u534F\u8BAE\u7684\u7EC8\u7AEF`," \uFF08kitty / Ghostty / WezTerm / \u65B0\u7248 VS Code\uFF09\u53EF\u76F4\u63A5\u7528 Shift+Enter\u3002",`${t.bold}\u53F3\u4FA7\u7EC8\u7AEF${t.reset}`," Enter \u6267\u884C\u547D\u4EE4 \xB7 \u2191/\u2193 \u547D\u4EE4\u5386\u53F2 \xB7 Ctrl+C / Ctrl+D \u4E2D\u65AD\u8FD0\u884C\u4E2D\u7684\u547D\u4EE4 \xB7 Ctrl+L \u6E05\u7A7A"," PgUp/PgDn / Shift+\u2191\u2193 / Home/End \u56DE\u770B\u5386\u53F2\u8F93\u51FA\uFF08\u6EDA\u52A8\u4E0D\u5F71\u54CD\u6B63\u5728\u8FD0\u884C\u7684\u547D\u4EE4\uFF09"," \u547D\u4EE4\u5728\u5F53\u524D\u9879\u76EE\u76EE\u5F55\u4E0B\u6267\u884C\uFF1Bcd \u751F\u6548\uFF08\u9762\u677F\u81EA\u884C\u7EF4\u62A4\u5DE5\u4F5C\u76EE\u5F55\uFF09"];this.lines.push(...e.map(n=>`${t.gray}${n}${t.reset}`)),this.lines.push("")}buildHeaderLines(e){let n=this.modelWeight!==void 0?` \xB7 \u6743\u91CD ${this.modelWeight}`:"",s=W(`${this.modelProviderLabel} / ${this.modelLabel}${n}`,Math.max(10,e-8)),i=W(this.cwd,Math.max(10,e-8)),o=`${t.gray}${"\u2500".repeat(Math.max(0,e))}${t.reset}`;return{info:[`${t.brightMagenta}${t.bold} \u7384\u732A XuanZhu${t.reset} ${t.gray}v${me} \xB7 \u7EC8\u7AEF AI \u7F16\u7801 Agent${t.reset}`,`${t.gray} \u6A21\u578B\uFF1A${t.reset}${s}${t.reset}`,`${t.gray} \u76EE\u5F55\uFF1A${t.reset}${i}${t.reset}`,`${t.gray} \u8F93\u5165 ${t.reset}${t.brightGreen}/help${t.reset}${t.gray} \u67E5\u770B\u547D\u4EE4\u4E0E\u5FEB\u6377\u952E${t.reset}`,`${t.gray} ${t.reset}Ctrl+D${t.gray} \u7ED3\u675F\u672C\u8F6E\u5BF9\u8BDD\uFF08\u4E0D\u9000\u51FA\uFF09\xB7 ${t.reset}Ctrl+C${t.gray} \u4E2D\u65AD/\u6E05\u7A7A/\u9000\u51FA${t.reset}`,`${t.gray} ${t.reset}Shift+Tab${t.gray} \u5207\u7EC8\u7AEF \xB7 ${t.reset}PgUp${t.gray}/\u6EDA\u8F6E\u56DE\u770B\u5386\u53F2${t.reset}`],divider:o}}scheduleRender(){this.needsRender||this.exited||(this.needsRender=!0,setTimeout(()=>{this.needsRender=!1,this.render()},16))}render(){if(this.exited)return;this.trimOutputLines();let e=Math.max(40,process.stdout.columns||80),n=Math.max(8,process.stdout.rows||24),s=this.resolveTermWidth(e),i=s>0?Math.max(20,e-s-1):e;this.lastLeftWidth=i;let o=this.pendingConfirm?this.layoutConfirm(i):this.layoutInput(i),a=o.lines.length,l=1,c=this.buildHeaderLines(i),u=Math.max(1,Math.floor(n/3)),d=Math.min(c.info.length,Math.max(0,u-1)),h=[...c.info.slice(0,d),c.divider],m=h.length,p=Math.max(3,n-m-a-l),f=this.physicalLines(i),g=f.length;this.lastPhysicalCount>=0&&this.scrollOffset>0&&g>this.lastPhysicalCount&&(this.scrollOffset+=g-this.lastPhysicalCount),this.lastPhysicalCount=g;let $=Math.max(0,g-p);this.scrollOffset>$&&(this.scrollOffset=$);let b=g-this.scrollOffset,y=Math.max(0,b-p),_=f.slice(y,b);for(;_.length<p;)_.unshift("");let C=n-1,F=s>0?this.buildTermPanel(s,C):{rows:[],cursorRow:0,cursorCol:0},le=m+p,Q=K.hide;for(let L=0;L<C;L++){let q;L<m?q=h[L]:L<m+p?q=_[L-m]:L<le+a?q=o.lines[L-le]:q="",Q+=K.to(L+1,1)+"\x1B[2K",s>0?Q+=Hr(W(q,i),i)+`${t.gray}\u2502${t.reset}`+W(F.rows[L]??"",s):Q+=q}if(Q+=K.to(n,1)+"\x1B[2K"+this.renderStatus(e),this.pendingConfirm)Q+=K.hide;else if(this.termFocus&&s>0&&F.cursorRow>0){let L=F.cursorRow,q=Math.min(e,i+1+F.cursorCol);Q+=K.to(L,q)+K.show}else{let L=le+o.cursorRow+1,q=Math.min(i,o.cursorCol+1);Q+=K.to(L,q)+K.show}process.stdout.write(Q)}resolveTermWidth(e){return!this.termVisible||e<80?0:Math.min(64,Math.max(32,Math.floor(e*.4)))}buildTermPanel(e,n){if(e<=0||n<=0)return{rows:[],cursorRow:0,cursorCol:0};let s=[],i=this.term.isRunning(),o=this.termFocus?`${t.bgBlue}${t.brightWhite} \u7EC8\u7AEF ${t.reset}`:`${t.gray} \u7EC8\u7AEF ${t.reset}`,a=i?`${t.brightYellow} \u25CF \u8FD0\u884C\u4E2D${t.reset}`:"",l=Math.max(6,e-9-(i?5:0));s.push(`${o}${t.gray}${W(this.term.getCwd(),l)}${t.reset}${a}`);let c=Math.max(1,n-3),u=[];for(let F of this.term.visibleLines())u.push(...we(F,e));this.lastTermPhysicalCount>=0&&this.termScroll>0&&u.length>this.lastTermPhysicalCount&&(this.termScroll+=u.length-this.lastTermPhysicalCount),this.lastTermPhysicalCount=u.length;let d=Math.max(0,u.length-c);this.termScroll>d&&(this.termScroll=d);let h=u.length-this.termScroll,m=Math.max(0,h-c),p=u.slice(m,h);for(;p.length<c;)p.unshift("");s.push(...p),s.push(`${t.gray}${"\u2500".repeat(e)}${t.reset}`);let f=2,g=Math.max(1,e-f),$=this.termInput,b=I($.slice(0,this.termInputCursor));for(;b>g&&$.length>0;)$=$.slice(1),b=I($.slice(0,this.termInputCursor));let y=this.termFocus?`${t.brightCyan}\u276F${t.reset} `:`${t.gray}\u276F${t.reset} `;s.push(W(`${y}${$}`,e));let _=s.length,C=Math.min(e,f+b+1);for(;s.length<n;)s.push("");return{rows:s.slice(0,n),cursorRow:_,cursorCol:C}}physicalLines(e){let n=[];for(let s of this.lines)n.push(...we(s,e));return this.streamingPreview&&n.push(...we(this.streamingPreview,e)),n}renderStatus(e){let n=" \u7384\u732A ",s=this.autoApprove?"\u26A1auto \xB7 ":"",i=this.intentEnabled?"\u2301intent \xB7 ":"",o=this.termVisible?this.termFocus?"\u7EC8\u7AEF\u25B8 ":"\u5BF9\u8BDD\u25B8 ":"",a=this.modelWeight!==void 0?` w${this.modelWeight}`:"",l=this.scrollOffset>0?`${t.brightYellow}\u2191${this.scrollOffset}${t.reset}${t.bgBlue} `:"",c=this.termScroll>0?`${t.brightYellow}\u21E1${this.termScroll}${t.reset}${t.bgBlue} `:"",u=` ${s}${i}${o}${l}${c}${this.modelLabel}${a} \u2502 ${this.statusText}${this.statusDetail?" \xB7 "+this.statusDetail:""} `,d=` ${this.cwd} `,h=this.busy?`${t.brightYellow}\u25CF \u8FD0\u884C\u4E2D${t.reset}${t.bgBlue}`:"",m=I(n)+I(u)+I(h)+6,p=`${t.bgBlue}${t.brightWhite}${n}${t.reset}${t.bgBlue}${t.brightWhite}${h}${t.bgBlue}${t.gray}${W(d,Math.max(0,e-m))}${t.bgBlue}${t.brightWhite}${u}${t.reset}`,f=I(p);return f<e?p+=`${t.bgBlue}${" ".repeat(e-f)}${t.reset}`:f>e&&(p=`${W(p,e)}${t.reset}`),p}layoutInput(e){let i=Math.max(1,Math.floor((process.stdout.rows||24)/2)-1),o=Array.from(this.input),a=0;{let m=0;a=o.length;for(let p=0;p<o.length;p++){if(m>=this.inputCursor){a=p;break}m+=o[p].length}}let l=[],c="\u276F ",u=I("\u276F "),d=0,h=u;for(let m=0;m<o.length;m++){let p=o[m];if(p===`
169
+ `).length;this.lines.push(`${t.green}\u2713 \u5DF2\u590D\u5236${o}\uFF08${c} \u884C\uFF09\u5230\u526A\u8D34\u677F${t.reset}${t.gray}\uFF08OSC 52\uFF09\u3002\u82E5\u7C98\u8D34\u51FA\u6765\u662F\u7A7A\u7684\uFF0C\u8BF4\u660E\u672C\u7EC8\u7AEF\u7981\u7528\u4E86\u8BE5\u5E8F\u5217\uFF0C\u8BF7\u6539\u7528 ${t.reset}Shift${t.gray}+\u9F20\u6807\u62D6\u62FD\u9009\u62E9\u3002${t.reset}`)}printHelp(){let e=[`${t.bold}\u7384\u732A \u5185\u7F6E\u547D\u4EE4${t.reset}`," /help \u663E\u793A\u672C\u5E2E\u52A9"," /clear \u6E05\u7A7A\u5C4F\u5E55"," /copy [N|A-B|last|all] \u590D\u5236\u8F93\u51FA\u533A\u5230\u7CFB\u7EDF\u526A\u8D34\u677F\uFF08\u9ED8\u8BA4\u6700\u8FD1 30 \u884C\uFF09"," /mouse \u5207\u6362\u9F20\u6807\u6355\u83B7\uFF1A\u5173\u95ED\u540E\u53EF\u7528\u9F20\u6807\u76F4\u63A5\u6846\u9009\u590D\u5236"," /reset \u91CD\u7F6E\u5BF9\u8BDD\u4E0A\u4E0B\u6587"," /cwd \u663E\u793A\u5F53\u524D\u5DE5\u4F5C\u76EE\u5F55"," /switch <\u76EE\u5F55> \u5207\u6362\u5F53\u524D\u5BF9\u8BDD\u7684\u9879\u76EE\u76EE\u5F55"," /term \u663E\u793A/\u9690\u85CF\u53F3\u4FA7\u7EC8\u7AEF\u9762\u677F\uFF08/term clear \u6E05\u7A7A\u8F93\u51FA\uFF09"," /model \u663E\u793A\u5F53\u524D\u6A21\u578B\u4E0E\u6743\u91CD\u3001\u5DF2\u914D\u7F6E\u6A21\u578B\u5217\u8868"," /auto [on|off] \u5207\u6362\u81EA\u52A8\u6279\u51C6\uFF08on\uFF1A\u6240\u6709\u5DE5\u5177\u76F4\u63A5\u6267\u884C\uFF0C\u5199\u5165\u914D\u7F6E\uFF09"," /intent [on|off] \u5207\u6362\u610F\u56FE\u5206\u6790\uFF08on\uFF1A\u6BCF\u6761\u6D88\u606F\u5148\u505A\u610F\u56FE\u5206\u6790\u518D\u5904\u7406\uFF0C\u5199\u5165\u914D\u7F6E\uFF09"," /exit \u9000\u51FA\u7384\u732A",`${t.bold}\u5FEB\u6377\u952E${t.reset}`," Enter \u63D0\u4EA4 \xB7 Ctrl+Enter / Shift+Enter \u6362\u884C\uFF08Ctrl+J\u3001Alt+Enter \u4EA6\u53EF\uFF09"," \u2191/\u2193 \u5386\u53F2 \xB7 Ctrl+A/Ctrl+E \u884C\u9996/\u884C\u5C3E \xB7 PgUp/PgDn \u7FFB\u9875 \xB7 Shift+\u2191/\u2193 \u9010\u884C"," Home/End \u8DF3\u5230\u6700\u65E9/\u6700\u65B0",` ${t.gray}\u9F20\u6807${t.reset}\uFF1A\u9ED8\u8BA4\u4EA4\u8FD8\u7EC8\u7AEF \u2014\u2014 \u53F3\u952E\u83DC\u5355\u3001\u6846\u9009\u590D\u5236\u3001\u4E2D\u952E\u7C98\u8D34\u90FD\u53EF\u7528\uFF1B\u6EDA\u52A8\u8F93\u51FA\u533A\u7528 ${t.reset}PgUp/PgDn${t.gray}\u3002`,` \u8F93\u5165 ${t.reset}/mouse${t.gray} \u53EF\u6539\u4E3A\u8BA9\u7384\u732A\u63A5\u7BA1\u6EDA\u8F6E\uFF08\u6B64\u65F6\u7528 ${t.reset}Shift+\u62D6\u62FD${t.gray} \u9009\u62E9\u6587\u672C\uFF09\u3002${t.reset}`,` ${t.gray}\u7C98\u8D34${t.reset}\uFF1A\u652F\u6301\u591A\u884C\u6574\u6BB5\u7C98\u8D34\uFF08\u4E0D\u4F1A\u9010\u884C\u63D0\u4EA4\uFF09\uFF1B\u4E5F\u53EF\u7528 ${t.reset}/copy${t.gray} \u590D\u5236\u8F93\u51FA\u533A\u3002${t.reset}`," Shift+Tab \u5207\u6362\u7126\u70B9\uFF08\u5BF9\u8BDD \u21C4 \u53F3\u4FA7\u7EC8\u7AEF\uFF09\xB7 \u9F20\u6807\u70B9\u51FB\u4EA6\u53EF"," Ctrl+C \u4E2D\u65AD\u4EFB\u52A1 / \u6E05\u7A7A\u8F93\u5165 / \u9000\u51FA\uFF08\u7A7A\u95F2\u4E14\u8F93\u5165\u4E3A\u7A7A\u65F6\u9000\u51FA\uFF09"," Ctrl+D \u7ED3\u675F\u672C\u8F6E\u6B63\u5728\u8FDB\u884C\u7684\u5BF9\u8BDD \xB7 \u4EFB\u4F55\u60C5\u51B5\u4E0B\u90FD\u4E0D\u4F1A\u9000\u51FA\u7384\u732A"," Ctrl+L \u6E05\u5C4F"," \u6CE8\uFF1A\u82E5 Ctrl/Shift+Enter \u65E0\u6548\uFF08\u90E8\u5206 IDE \u5185\u7F6E\u7EC8\u7AEF\u4F1A\u628A\u4E8C\u8005\u7F16\u6210\u4E0E Enter \u76F8\u540C\u7684\u5B57\u8282\uFF09\uFF0C",` \u8BF7\u6539\u7528 ${t.bold}Alt+Enter${t.reset} \u6216 Ctrl+J \u2014\u2014 \u5B83\u4EEC\u8D70\u7EC8\u7AEF\u539F\u751F\u7F16\u7801\uFF0C\u5404\u7EC8\u7AEF\u901A\u7528\u3002`,`${t.bold}\u53F3\u4FA7\u7EC8\u7AEF${t.reset}`," Enter \u6267\u884C\u547D\u4EE4 \xB7 \u2191/\u2193 \u547D\u4EE4\u5386\u53F2 \xB7 Ctrl+C / Ctrl+D \u4E2D\u65AD\u8FD0\u884C\u4E2D\u7684\u547D\u4EE4 \xB7 Ctrl+L \u6E05\u7A7A"," PgUp/PgDn / Shift+\u2191\u2193 / Home/End \u56DE\u770B\u5386\u53F2\u8F93\u51FA\uFF08\u6EDA\u52A8\u4E0D\u5F71\u54CD\u6B63\u5728\u8FD0\u884C\u7684\u547D\u4EE4\uFF09"," \u547D\u4EE4\u5728\u5F53\u524D\u9879\u76EE\u76EE\u5F55\u4E0B\u6267\u884C\uFF1Bcd \u751F\u6548\uFF08\u9762\u677F\u81EA\u884C\u7EF4\u62A4\u5DE5\u4F5C\u76EE\u5F55\uFF09"];this.lines.push(...e.map(n=>`${t.gray}${n}${t.reset}`)),this.lines.push("")}buildHeaderLines(e){let n=this.modelWeight!==void 0?` \xB7 \u6743\u91CD ${this.modelWeight}`:"",s=W(`${this.modelProviderLabel} / ${this.modelLabel}${n}`,Math.max(10,e-8)),i=W(this.cwd,Math.max(10,e-8)),o=`${t.gray}${"\u2500".repeat(Math.max(0,e))}${t.reset}`;return{info:[`${t.brightMagenta}${t.bold} \u7384\u732A XuanZhu${t.reset} ${t.gray}v${me} \xB7 \u7EC8\u7AEF AI \u7F16\u7801 Agent${t.reset}`,`${t.gray} \u6A21\u578B\uFF1A${t.reset}${s}${t.reset}`,`${t.gray} \u76EE\u5F55\uFF1A${t.reset}${i}${t.reset}`,`${t.gray} \u8F93\u5165 ${t.reset}${t.brightGreen}/help${t.reset}${t.gray} \u67E5\u770B\u547D\u4EE4\u4E0E\u5FEB\u6377\u952E${t.reset}`,`${t.gray} ${t.reset}Ctrl+D${t.gray} \u7ED3\u675F\u672C\u8F6E\u5BF9\u8BDD\uFF08\u4E0D\u9000\u51FA\uFF09\xB7 ${t.reset}Ctrl+C${t.gray} \u4E2D\u65AD/\u6E05\u7A7A/\u9000\u51FA${t.reset}`,`${t.gray} ${t.reset}Shift+Tab${t.gray} \u5207\u7EC8\u7AEF \xB7 ${t.reset}PgUp${t.gray}/\u6EDA\u8F6E\u56DE\u770B\u5386\u53F2${t.reset}`],divider:o}}scheduleRender(){this.needsRender||this.exited||(this.needsRender=!0,setTimeout(()=>{this.needsRender=!1,this.render()},16))}render(){if(this.exited)return;this.trimOutputLines();let e=Math.max(40,process.stdout.columns||80),n=Math.max(8,process.stdout.rows||24),s=this.resolveTermWidth(e),i=s>0?Math.max(20,e-s-1):e;this.lastLeftWidth=i;let o=this.pendingConfirm?this.layoutConfirm(i):this.layoutInput(i),a=o.lines.length,l=1,c=this.buildHeaderLines(i),u=Math.max(1,Math.floor(n/3)),d=Math.min(c.info.length,Math.max(0,u-1)),h=[...c.info.slice(0,d),c.divider],m=h.length,p=Math.max(3,n-m-a-l),f=this.physicalLines(i),g=f.length;this.lastPhysicalCount>=0&&this.scrollOffset>0&&g>this.lastPhysicalCount&&(this.scrollOffset+=g-this.lastPhysicalCount),this.lastPhysicalCount=g;let $=Math.max(0,g-p);this.scrollOffset>$&&(this.scrollOffset=$);let b=g-this.scrollOffset,y=Math.max(0,b-p),_=f.slice(y,b);for(;_.length<p;)_.unshift("");let C=n-1,F=s>0?this.buildTermPanel(s,C):{rows:[],cursorRow:0,cursorCol:0},le=m+p,Q=K.hide;for(let L=0;L<C;L++){let q;L<m?q=h[L]:L<m+p?q=_[L-m]:L<le+a?q=o.lines[L-le]:q="",Q+=K.to(L+1,1)+"\x1B[2K",s>0?Q+=Hr(W(q,i),i)+`${t.gray}\u2502${t.reset}`+W(F.rows[L]??"",s):Q+=q}if(Q+=K.to(n,1)+"\x1B[2K"+this.renderStatus(e),this.pendingConfirm)Q+=K.hide;else if(this.termFocus&&s>0&&F.cursorRow>0){let L=F.cursorRow,q=Math.min(e,i+1+F.cursorCol);Q+=K.to(L,q)+K.show}else{let L=le+o.cursorRow+1,q=Math.min(i,o.cursorCol+1);Q+=K.to(L,q)+K.show}process.stdout.write(Q)}resolveTermWidth(e){return!this.termVisible||e<80?0:Math.min(64,Math.max(32,Math.floor(e*.4)))}buildTermPanel(e,n){if(e<=0||n<=0)return{rows:[],cursorRow:0,cursorCol:0};let s=[],i=this.term.isRunning(),o=this.termFocus?`${t.bgBlue}${t.brightWhite} \u7EC8\u7AEF ${t.reset}`:`${t.gray} \u7EC8\u7AEF ${t.reset}`,a=i?`${t.brightYellow} \u25CF \u8FD0\u884C\u4E2D${t.reset}`:"",l=Math.max(6,e-9-(i?5:0));s.push(`${o}${t.gray}${W(this.term.getCwd(),l)}${t.reset}${a}`);let c=Math.max(1,n-3),u=[];for(let F of this.term.visibleLines())u.push(...we(F,e));this.lastTermPhysicalCount>=0&&this.termScroll>0&&u.length>this.lastTermPhysicalCount&&(this.termScroll+=u.length-this.lastTermPhysicalCount),this.lastTermPhysicalCount=u.length;let d=Math.max(0,u.length-c);this.termScroll>d&&(this.termScroll=d);let h=u.length-this.termScroll,m=Math.max(0,h-c),p=u.slice(m,h);for(;p.length<c;)p.unshift("");s.push(...p),s.push(`${t.gray}${"\u2500".repeat(e)}${t.reset}`);let f=2,g=Math.max(1,e-f),$=this.termInput,b=I($.slice(0,this.termInputCursor));for(;b>g&&$.length>0;)$=$.slice(1),b=I($.slice(0,this.termInputCursor));let y=this.termFocus?`${t.brightCyan}\u276F${t.reset} `:`${t.gray}\u276F${t.reset} `;s.push(W(`${y}${$}`,e));let _=s.length,C=Math.min(e,f+b+1);for(;s.length<n;)s.push("");return{rows:s.slice(0,n),cursorRow:_,cursorCol:C}}physicalLines(e){let n=[];for(let s of this.lines)n.push(...we(s,e));return this.streamingPreview&&n.push(...we(this.streamingPreview,e)),n}renderStatus(e){let n=" \u7384\u732A ",s=this.autoApprove?"\u26A1auto \xB7 ":"",i=this.intentEnabled?"\u2301intent \xB7 ":"",o=this.termVisible?this.termFocus?"\u7EC8\u7AEF\u25B8 ":"\u5BF9\u8BDD\u25B8 ":"",a=this.modelWeight!==void 0?` w${this.modelWeight}`:"",l=this.scrollOffset>0?`${t.brightYellow}\u2191${this.scrollOffset}${t.reset}${t.bgBlue} `:"",c=this.termScroll>0?`${t.brightYellow}\u21E1${this.termScroll}${t.reset}${t.bgBlue} `:"",u=` ${s}${i}${o}${l}${c}${this.modelLabel}${a} \u2502 ${this.statusText}${this.statusDetail?" \xB7 "+this.statusDetail:""} `,d=` ${this.cwd} `,h=this.busy?`${t.brightYellow}\u25CF \u8FD0\u884C\u4E2D${t.reset}${t.bgBlue}`:"",m=I(n)+I(u)+I(h)+6,p=`${t.bgBlue}${t.brightWhite}${n}${t.reset}${t.bgBlue}${t.brightWhite}${h}${t.bgBlue}${t.gray}${W(d,Math.max(0,e-m))}${t.bgBlue}${t.brightWhite}${u}${t.reset}`,f=I(p);return f<e?p+=`${t.bgBlue}${" ".repeat(e-f)}${t.reset}`:f>e&&(p=`${W(p,e)}${t.reset}`),p}layoutInput(e){let i=Math.max(1,Math.floor((process.stdout.rows||24)/2)-1),o=Array.from(this.input),a=0;{let m=0;a=o.length;for(let p=0;p<o.length;p++){if(m>=this.inputCursor){a=p;break}m+=o[p].length}}let l=[],c="\u276F ",u=I("\u276F "),d=0,h=u;for(let m=0;m<o.length;m++){let p=o[m];if(p===`
170
170
  `){l.push(c),c=" ",u=I(" "),m===a&&(d=l.length,h=u);continue}let f=I(p);u+f>e&&(l.push(c),c=" ",u=I(" ")),m===a&&(d=l.length,h=u),c+=p,u+=f}if(a>=o.length&&(d=l.length,h=u),l.push(c),l.length>i){let m=Math.max(0,d-i+1);return{lines:l.slice(m,m+i),cursorRow:d-m,cursorCol:h}}return{lines:l,cursorRow:d,cursorCol:h}}layoutConfirm(e){let n=this.pendingConfirm,{request:s}=n,i=[],o=s.danger;i.push(`${o?t.brightRed:t.brightYellow}\u26A0 ${s.title}${t.reset}`);let a=s.detail.split(`
171
171
  `).slice(0,12);for(let u of a)for(let d of we(`${t.gray}${u}${t.reset}`,e-4))i.push(` ${d}`);let l=n.selected?`${t.inverse}${t.green} \u5141\u8BB8 ${t.reset}`:`${t.gray} \u5141\u8BB8 ${t.reset}`,c=n.selected?`${t.gray} \u62D2\u7EDD ${t.reset}`:`${t.inverse}${t.yellow} \u62D2\u7EDD ${t.reset}`;return i.push(` ${l} ${c} ${t.gray}\u2190 \u2192 \u5207\u6362 \xB7 Enter \u786E\u8BA4 \xB7 y/n \u5FEB\u6377${t.reset}`),{lines:i,cursorRow:0,cursorCol:0}}},Es=1,ks=2,vt=4;function _s(r,e){if((e&vt)===0)return null;let n=r>=65&&r<=90?r+32:r;if(n>=97&&n<=122)return String.fromCharCode(n-96);switch(r){case 32:case 64:return"\0";case 91:return"\x1B";case 92:return"";case 93:return"";case 94:return"";case 95:case 47:return"";default:return null}}function Jr(r,e){let n=(e&(Es|vt|ks))!==0;switch(r){case 13:return n?`
172
172
  `:"\r";case 9:return n&&(e&vt)===0?"\x1B[Z":" ";case 8:case 127:return"\x7F";default:return _s(r,e)}}function Ms(r){if(r==="\x1B\r"||r===`\x1B
@@ -199,7 +199,7 @@ ${t.bold}${r}${t.reset}
199
199
  `),e[n].value):e[a].value}finally{s.close()}}async function nn(r,e=!1){let n=Ve.createInterface({input:process.stdin,output:process.stdout});try{let s=e?"[Y/n]":"[y/N]",o=(await Promise.race([n.question(`${r} ${s} `),new Promise(a=>n.once("close",()=>a("")))])).trim().toLowerCase();return o?o==="y"||o==="yes":e}finally{n.close()}}async function sn(r){switch(r[0]){case void 0:return Ds();case"list":case"ls":return Ns();case"all":case"available":case"providers":return un();case"add":return Et(r.slice(1));case"remove":case"rm":case"delete":return on(r.slice(1));case"weight":case"w":return an(r.slice(1));case"context":case"ctx":return ln(r.slice(1));case"reset":return cn();case"help":case"-h":case"--help":return Us();default:return Et(r)}}async function Et(r){let e=O(),n=te(e),s=r[0]??await Ks(),i=J(s);if(!i){process.stdout.write(`${t.red}\u672A\u77E5\u7684 provider\uFF1A${s}${t.reset}
200
200
  ${t.gray}\u53EF\u7528\uFF1A${Y.map(p=>p.id).join(", ")}${t.reset}
201
201
  `),process.exitCode=1;return}let o=r[1]??await Ws(i.id,i.models,e),a=r[2],l;if(a!==void 0){let p=/^\d+$/.test(a.trim())?Number.parseInt(a.trim(),10):Number.NaN;if(Number.isNaN(p)||p<0){process.stdout.write(`${t.red}\u6743\u91CD\u5FC5\u987B\u662F \u2265 0 \u7684\u6574\u6570\uFF1A${a}${t.reset}
202
- `),process.exitCode=1;return}l=p}let c=await Bs(i.id,i.requiresBaseUrl===!0,e),u=await zs(i.id,i.envKey,e),d=await Fs(o,r[3]);e.providers={...e.providers,[i.id]:{...e.providers[i.id]??{},model:o,...c?{baseUrl:c}:{},...u?{apiKey:u}:{}}},e.provider=i.id,e.model=o;let h=n.some(p=>p.id===ie(i.id,o)),m=Ft(e,{provider:i.id,model:o,weight:l,contextWindow:d,...c?{baseUrl:c}:{},...u?{apiKey:u}:{}});M(e),process.stdout.write(`
202
+ `),process.exitCode=1;return}l=p}let c=await Bs(i.id,i.requiresBaseUrl===!0,e),u=await js(i.id,i.envKey,e),d=await Fs(o,r[3]);e.providers={...e.providers,[i.id]:{...e.providers[i.id]??{},model:o,...c?{baseUrl:c}:{},...u?{apiKey:u}:{}}},e.provider=i.id,e.model=o;let h=n.some(p=>p.id===ie(i.id,o)),m=Ft(e,{provider:i.id,model:o,weight:l,contextWindow:d,...c?{baseUrl:c}:{},...u?{apiKey:u}:{}});M(e),process.stdout.write(`
203
203
  ${t.green}\u2713 ${h?"\u5DF2\u66F4\u65B0":"\u5DF2\u65B0\u589E"}\u6A21\u578B${t.reset} ${t.bold}${m.id}${t.reset}
204
204
  ${t.gray}provider\uFF1A${t.reset}${i.label}
205
205
  ${t.gray}model \uFF1A${t.reset}${m.model}
@@ -208,7 +208,7 @@ ${t.green}\u2713 ${h?"\u5DF2\u66F4\u65B0":"\u5DF2\u65B0\u589E"}\u6A21\u578B${t.r
208
208
  `+(c?` ${t.gray}baseUrl \uFF1A${t.reset}${c}
209
209
  `:"")+` ${t.gray}\u914D\u7F6E\u6587\u4EF6\uFF1A${t.reset}${E()}
210
210
  `),_t(e)}async function on(r){let e=O(),n=k(e);if(n.length===0){process.stdout.write(`${t.yellow}\u5C1A\u672A\u914D\u7F6E\u4EFB\u4F55\u6A21\u578B\u3002${t.reset}
211
- `);return}let s=r[0]??await Pt(n,"\u9009\u62E9\u8981\u5220\u9664\u7684\u6A21\u578B\uFF1A"),i=Rt(e,s),o=i?jt(e,i):null;if(!o){process.stdout.write(`${t.red}\u672A\u627E\u5230\u6A21\u578B\uFF1A${s}${t.reset}
211
+ `);return}let s=r[0]??await Pt(n,"\u9009\u62E9\u8981\u5220\u9664\u7684\u6A21\u578B\uFF1A"),i=Rt(e,s),o=i?zt(e,i):null;if(!o){process.stdout.write(`${t.red}\u672A\u627E\u5230\u6A21\u578B\uFF1A${s}${t.reset}
212
212
  ${t.gray}\u63D0\u793A\uFF1A\u53EF\u4F7F\u7528 ${t.reset}xzh model list${t.gray} \u67E5\u770B id \u4E0E\u5E8F\u53F7\u3002${t.reset}
213
213
  `),process.exitCode=1;return}Mt(e),M(e),process.stdout.write(`
214
214
  ${t.green}\u2713 \u5DF2\u5220\u9664\u6A21\u578B${t.reset} ${o.id}
@@ -271,7 +271,7 @@ ${t.gray}\u53EF\u7528 provider\uFF1A${Y.map(r=>r.id).join(", ")}${t.reset}
271
271
  `)}function Mt(r){let e=r.models??[],n=e.length>0?e.slice().sort((s,i)=>i.weight-s.weight||s.id.localeCompare(i.id))[0]:null;r.provider=n?n.provider:"",r.model=n?n.model:""}async function Ks(){return Ce("\u9009\u62E9 AI \u670D\u52A1\u5546\uFF1A",Y.map(r=>({label:r.label,value:r.id,hint:r.id})),0)}async function Ws(r,e,n){let s=n.providers[r]?.model;if(e.length>0){let o=[...e.map(c=>({label:c,value:c})),{label:"\u624B\u52A8\u8F93\u5165\u6A21\u578B\u540D",value:"__custom__"}],a=s?Math.max(0,e.indexOf(s)):0,l=await Ce(`\u9009\u62E9\u6A21\u578B\uFF08${r}\uFF09\uFF1A`,o,a);if(l!=="__custom__")return l}return await ae("\u8BF7\u8F93\u5165\u6A21\u578B\u540D\u79F0",s)}async function Bs(r,e,n){let s=n.providers[r]?.baseUrl;return e?await ae("\u8BF7\u8F93\u5165 API \u7AEF\u70B9 baseUrl",s):s??""}function kt(r){let e=r.trim().toLowerCase().replace(/[_,\s]/g,"");if(!e)return;let n=/^(\d+(?:\.\d+)?)(k|m|万)?$/.exec(e);if(!n)return;let s=Number(n[1]);if(!Number.isFinite(s)||s<=0)return;let i=n[2],a=Math.floor(s*(i==="k"?1e3:i==="m"?1e6:i==="\u4E07"?1e4:1));return a>0?a:void 0}async function Fs(r,e){let n=ue(r);if(e!==void 0&&e.trim()!==""){let o=kt(e);return o===void 0?(process.stdout.write(`${t.yellow}\u65E0\u6CD5\u8BC6\u522B\u4E0A\u4E0B\u6587\u7A97\u53E3\u300C${e}\u300D\uFF0C\u5DF2\u4F7F\u7528\u63A8\u65AD\u503C ${ne(n)}\u3002${t.reset}
272
272
  `),n):o}process.stdout.write(`${t.gray} \u4E0A\u4E0B\u6587\u7A97\u53E3\uFF1A\u6309\u6A21\u578B\u540D\u63A8\u65AD\u4E3A ${ne(n)} token\u3002${t.reset}
273
273
  `);let s=await ae("\u8BF7\u8F93\u5165\u4E0A\u4E0B\u6587\u7A97\u53E3\u5927\u5C0F\uFF08token\uFF0C\u5982 128000\uFF1B\u56DE\u8F66\u4F7F\u7528\u63A8\u65AD\u503C\uFF09",String(n)),i=kt(s);return i===void 0?(process.stdout.write(`${t.yellow} \u65E0\u6CD5\u8BC6\u522B\u300C${s}\u300D\uFF0C\u5DF2\u4F7F\u7528\u63A8\u65AD\u503C ${ne(n)}\u3002${t.reset}
274
- `),n):i}async function zs(r,e,n){let s=J(r),i=n.providers[r]?.apiKey;return s?.requiresApiKey?(Ae(s,i)&&process.stdout.write(`${t.gray}\u5DF2\u68C0\u6D4B\u5230 API Key\uFF08${e} \u6216\u914D\u7F6E\u4E2D\uFF09\uFF0C\u76F4\u63A5\u56DE\u8F66\u4FDD\u7559\u3002${t.reset}
274
+ `),n):i}async function js(r,e,n){let s=J(r),i=n.providers[r]?.apiKey;return s?.requiresApiKey?(Ae(s,i)&&process.stdout.write(`${t.gray}\u5DF2\u68C0\u6D4B\u5230 API Key\uFF08${e} \u6216\u914D\u7F6E\u4E2D\uFF09\uFF0C\u76F4\u63A5\u56DE\u8F66\u4FDD\u7559\u3002${t.reset}
275
275
  `),await ae("\u8BF7\u8F93\u5165 API Key",i??"",{mask:!0})):i??""}function Rt(r,e){let n=k(r),s=n.find(o=>o.id===e);if(s)return s.id;let i=n.find(o=>o.model===e);if(i)return i.id;if(/^\d+$/.test(e.trim())){let o=n.slice().sort((l,c)=>c.weight-l.weight),a=Number.parseInt(e,10);if(a>=1&&a<=o.length)return o[a-1].id}return null}async function Pt(r,e){let n=r.slice().sort((s,i)=>i.weight-s.weight);return Ce(e,n.map(s=>({label:`${s.id} \u6743\u91CD ${s.weight}`,value:s.id})),0)}async function hn(){let r=ve();process.stdout.write(`
276
276
  ${t.bold}\u7384\u732A\u6280\u80FD\u4F9D\u8D56\u68C0\u67E5${t.reset} ${t.gray}(\u5E73\u53F0\uFF1A${r})${t.reset}
277
277
 
@@ -298,12 +298,12 @@ ${t.bold}\u7ED3\u679C${t.reset}
298
298
  `),process.exit(1)});process.on("unhandledRejection",r=>{St("\u672A\u5904\u7406\u7684 Promise \u62D2\u7EDD",r),Ze(),process.stderr.write(`
299
299
  \u672A\u5904\u7406\u7684 Promise \u62D2\u7EDD\uFF1A${String(r)}
300
300
  `),process.stderr.write(`\u8BE6\u60C5\u5DF2\u5199\u5165 ${Je()}
301
- `),process.exit(1)});async function js(){let r=Ot();r.migrated&&process.stdout.write(`${t.gray}\u5DF2\u5C06\u5168\u5C40\u76EE\u5F55\u8FC1\u79FB\u81F3 ${A()}${r.from?`\uFF08\u539F ${r.from}\uFF09`:""}${t.reset}
301
+ `),process.exit(1)});async function zs(){let r=Ot();r.migrated&&process.stdout.write(`${t.gray}\u5DF2\u5C06\u5168\u5C40\u76EE\u5F55\u8FC1\u79FB\u81F3 ${A()}${r.from?`\uFF08\u539F ${r.from}\uFF09`:""}${t.reset}
302
302
  `);let e=process.argv.slice(2),n;e.includes("--intent")?n=!0:e.includes("--no-intent")&&(n=!1);let s=e.filter(o=>o!=="--intent"&&o!=="--no-intent"),i=s[0];switch(i){case void 0:await mn(void 0,n);return;case"model":await sn(s.slice(1));return;case"config":await tn(s.slice(1));return;case"setup":await hn();return;case"help":case"-h":case"--help":pn();return;case"version":case"-v":case"--version":process.stdout.write(`\u7384\u732A xzh v${me}
303
303
  `);return;default:{if(i.startsWith("-")){pn();return}let o=Hs(i);if(o){await mn(o,n);return}if(qs(i)){process.stdout.write(`${t.red}\u76EE\u5F55\u4E0D\u5B58\u5728\u6216\u4E0D\u662F\u76EE\u5F55\uFF1A${i}${t.reset}
304
304
  `),process.exitCode=1;return}process.stdout.write(`${t.red}\u672A\u77E5\u547D\u4EE4\uFF1A${i}${t.reset}
305
305
  ${t.gray}\u8FD0\u884C ${t.reset}xzh --help${t.gray} \u67E5\u770B\u7528\u6CD5\uFF0C\u6216\u8FD0\u884C ${t.reset}xzh <\u76EE\u5F55>${t.gray} \u6307\u5B9A\u9879\u76EE\u76EE\u5F55\u3002${t.reset}
306
- `),process.exitCode=1}}}function qs(r){return r.startsWith("/")||r.startsWith("~")||r.startsWith("./")||r.startsWith("../")||r==="."||r===".."}function Hs(r){let e=j(r),n=Qe.isAbsolute(e)?e:Qe.resolve(process.cwd(),e);try{if($e.existsSync(n)&&$e.statSync(n).isDirectory())return $e.realpathSync(n)}catch{}return null}async function mn(r,e){if(!process.stdin.isTTY||!process.stdout.isTTY){process.stdout.write(`${t.red}\u7384\u732A\u9700\u8981\u4EA4\u4E92\u5F0F\u7EC8\u7AEF\uFF08TTY\uFF09\u3002${t.reset}
306
+ `),process.exitCode=1}}}function qs(r){return r.startsWith("/")||r.startsWith("~")||r.startsWith("./")||r.startsWith("../")||r==="."||r===".."}function Hs(r){let e=z(r),n=Qe.isAbsolute(e)?e:Qe.resolve(process.cwd(),e);try{if($e.existsSync(n)&&$e.statSync(n).isDirectory())return $e.realpathSync(n)}catch{}return null}async function mn(r,e){if(!process.stdin.isTTY||!process.stdout.isTTY){process.stdout.write(`${t.red}\u7384\u732A\u9700\u8981\u4EA4\u4E92\u5F0F\u7EC8\u7AEF\uFF08TTY\uFF09\u3002${t.reset}
307
307
  ${t.gray}\u8BF7\u5728\u771F\u5B9E\u7EC8\u7AEF\u4E2D\u8FD0\u884C xzh\u3002${t.reset}
308
308
  `),process.exitCode=1;return}let n=O(),s=k(n);s.length>0&&s.every(d=>d.weight<=0&&d.decayed===!0)&&(Se(n),M(n),process.stdout.write(`${t.yellow}\u26A0 \u6240\u6709\u6A21\u578B\u6743\u91CD\u5747\u4E3A 0\uFF0C\u5DF2\u91CD\u7F6E\u4E3A ${T}\u3002${t.reset}
309
309
  `));let i=ee(n);if(!i){process.stdout.write(s.length===0?`${t.yellow}\u5C1A\u672A\u914D\u7F6E\u4EFB\u4F55\u6A21\u578B\u3002${t.reset}
@@ -364,6 +364,6 @@ ${t.bold}\u754C\u9762\u5185\u5FEB\u6377\u952E${t.reset}
364
364
  Enter \u63D0\u4EA4 \xB7 Ctrl+J \u6362\u884C \xB7 \u2191/\u2193 \u5386\u53F2 \xB7 PgUp/PgDn \u6EDA\u52A8
365
365
  Ctrl+C \u4E2D\u65AD\u4EFB\u52A1/\u9000\u51FA \xB7 Ctrl+L \u6E05\u5C4F \xB7 \u8F93\u5165 /help \u67E5\u770B\u5185\u7F6E\u547D\u4EE4
366
366
  \u5207\u6362\u9879\u76EE\u76EE\u5F55\uFF1A\u5728\u754C\u9762\u4E2D\u8F93\u5165 ${t.green}/switch <\u76EE\u5F55>${t.reset}
367
- `;process.stdout.write(r)}js().catch(r=>{process.stdout.write(`
367
+ `;process.stdout.write(r)}zs().catch(r=>{process.stdout.write(`
368
368
  ${t.red}\u7384\u732A\u8FD0\u884C\u51FA\u9519\uFF1A${G(r)}${t.reset}
369
369
  `),process.exit(1)});
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "xuanzhu",
3
- "version": "0.1.6",
3
+ "version": "0.1.7",
4
4
  "description": "玄猪(XuanZhu)— 专为终端打造的 AI 编码 Agent",
5
5
  "keywords": [
6
6
  "ai",