@memo-code/memo 0.8.10 → 0.8.50
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -0
- package/README.zh.md +1 -0
- package/dist/index.js +88 -79
- package/dist/prompt.md +2 -0
- package/dist/web/server/main.cjs +50 -3
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1,34 +1,43 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
var hl=Object.create;var mr=Object.defineProperty;var yl=Object.getOwnPropertyDescriptor;var _l=Object.getOwnPropertyNames;var Sl=Object.getPrototypeOf,vl=Object.prototype.hasOwnProperty;var Yo=(e,t)=>()=>(e&&(t=e(e=0)),t);var Qo=(e,t)=>()=>(t||e((t={exports:{}}).exports,t),t.exports),Tl=(e,t)=>{for(var n in t)mr(e,n,{get:t[n],enumerable:!0})},wl=(e,t,n,r)=>{if(t&&typeof t=="object"||typeof t=="function")for(let o of _l(t))!vl.call(e,o)&&o!==n&&mr(e,o,{get:()=>t[o],enumerable:!(r=yl(t,o))||r.enumerable});return e};var Zo=(e,t,n)=>(n=e!=null?hl(Sl(e)):{},wl(t||!e||!e.__esModule?mr(n,"default",{value:e,enumerable:!0}):n,e));var xs=Qo((wS,Sn)=>{"use strict";function Ss(e){return Array.isArray(e)?e:[e]}var ku=void 0,Sr="",ys=" ",_r="\\",Au=/^\s+$/,Pu=/(?:[^\\]|^)\\$/,Eu=/^\\!/,Ru=/^\\#/,Iu=/\r?\n/g,Ou=/^\.{0,2}\/|^\.{1,2}$/,Lu=/\/$/,yt="/",vs="node-ignore";typeof Symbol<"u"&&(vs=Symbol.for("node-ignore"));var Ts=vs,_t=(e,t,n)=>(Object.defineProperty(e,t,{value:n}),n),Nu=/([0-z])-([0-z])/g,ws=()=>!1,$u=e=>e.replace(Nu,(t,n,r)=>n.charCodeAt(0)<=r.charCodeAt(0)?t:Sr),Du=e=>{let{length:t}=e;return e.slice(0,t-t%2)},Uu=[[/^\uFEFF/,()=>Sr],[/((?:\\\\)*?)(\\?\s+)$/,(e,t,n)=>t+(n.indexOf("\\")===0?ys:Sr)],[/(\\+?)\s/g,(e,t)=>{let{length:n}=t;return t.slice(0,n-n%2)+ys}],[/[\\$.|*+(){^]/g,e=>`\\${e}`],[/(?!\\)\?/g,()=>"[^/]"],[/^\//,()=>"^"],[/\//g,()=>"\\/"],[/^\^*\\\*\\\*\\\//,()=>"^(?:.*\\/)?"],[/^(?=[^^])/,function(){return/\/(?!$)/.test(this)?"^":"(?:^|\\/)"}],[/\\\/\\\*\\\*(?=\\\/|$)/g,(e,t,n)=>t+6<n.length?"(?:\\/[^\\/]+)*":"\\/.+"],[/(^|[^\\]+)(\\\*)+(?=.+)/g,(e,t,n)=>{let r=n.replace(/\\\*/g,"[^\\/]*");return t+r}],[/\\\\\\(?=[$.|*+(){^])/g,()=>_r],[/\\\\/g,()=>_r],[/(\\)?\[([^\]/]*?)(\\*)($|\])/g,(e,t,n,r,o)=>t===_r?`\\[${n}${Du(r)}${o}`:o==="]"&&r.length%2===0?`[${$u(n)}${r}]`:"[]"],[/(?:[^*])$/,e=>/\/$/.test(e)?`${e}$`:`${e}(?=$|\\/$)`]],Fu=/(^|\\\/)?\\\*$/,Bt="regex",yn="checkRegex",_s="_",Hu={[Bt](e,t){return`${t?`${t}[^/]+`:"[^/]*"}(?=$|\\/$)`},[yn](e,t){return`${t?`${t}[^/]*`:"[^/]*"}(?=$|\\/$)`}},Bu=e=>Uu.reduce((t,[n,r])=>t.replace(n,r.bind(e)),e),_n=e=>typeof e=="string",Wu=e=>e&&_n(e)&&!Au.test(e)&&!Pu.test(e)&&e.indexOf("#")!==0,ju=e=>e.split(Iu).filter(Boolean),vr=class{constructor(t,n,r,o,s,i){this.pattern=t,this.mark=n,this.negative=s,_t(this,"body",r),_t(this,"ignoreCase",o),_t(this,"regexPrefix",i)}get regex(){let t=_s+Bt;return this[t]?this[t]:this._make(Bt,t)}get checkRegex(){let t=_s+yn;return this[t]?this[t]:this._make(yn,t)}_make(t,n){let r=this.regexPrefix.replace(Fu,Hu[t]),o=this.ignoreCase?new RegExp(r,"i"):new RegExp(r);return _t(this,n,o)}},zu=({pattern:e,mark:t},n)=>{let r=!1,o=e;o.indexOf("!")===0&&(r=!0,o=o.substr(1)),o=o.replace(Eu,"!").replace(Ru,"#");let s=Bu(o);return new vr(e,t,o,n,r,s)},Tr=class{constructor(t){this._ignoreCase=t,this._rules=[]}_add(t){if(t&&t[Ts]){this._rules=this._rules.concat(t._rules._rules),this._added=!0;return}if(_n(t)&&(t={pattern:t}),Wu(t.pattern)){let n=zu(t,this._ignoreCase);this._added=!0,this._rules.push(n)}}add(t){return this._added=!1,Ss(_n(t)?ju(t):t).forEach(this._add,this),this._added}test(t,n,r){let o=!1,s=!1,i;this._rules.forEach(a=>{let{negative:l}=a;s===l&&o!==s||l&&!o&&!s&&!n||!a[r].test(t)||(o=!l,s=l,i=l?ku:a)});let c={ignored:o,unignored:s};return i&&(c.rule=i),c}},Gu=(e,t)=>{throw new t(e)},Fe=(e,t,n)=>_n(e)?e?Fe.isNotRelative(e)?n(`path should be a \`path.relative()\`d string, but got "${t}"`,RangeError):!0:n("path must not be empty",TypeError):n(`path must be a string, but got \`${t}\``,TypeError),Cs=e=>Ou.test(e);Fe.isNotRelative=Cs;Fe.convert=e=>e;var wr=class{constructor({ignorecase:t=!0,ignoreCase:n=t,allowRelativePaths:r=!1}={}){_t(this,Ts,!0),this._rules=new Tr(n),this._strictPathCheck=!r,this._initCache()}_initCache(){this._ignoreCache=Object.create(null),this._testCache=Object.create(null)}add(t){return this._rules.add(t)&&this._initCache(),this}addPattern(t){return this.add(t)}_test(t,n,r,o){let s=t&&Fe.convert(t);return Fe(s,t,this._strictPathCheck?Gu:ws),this._t(s,n,r,o)}checkIgnore(t){if(!Lu.test(t))return this.test(t);let n=t.split(yt).filter(Boolean);if(n.pop(),n.length){let r=this._t(n.join(yt)+yt,this._testCache,!0,n);if(r.ignored)return r}return this._rules.test(t,!1,yn)}_t(t,n,r,o){if(t in n)return n[t];if(o||(o=t.split(yt).filter(Boolean)),o.pop(),!o.length)return n[t]=this._rules.test(t,r,Bt);let s=this._t(o.join(yt)+yt,n,r,o);return n[t]=s.ignored?s:this._rules.test(t,r,Bt)}ignores(t){return this._test(t,this._ignoreCache,!1).ignored}createFilter(){return t=>!this.ignores(t)}filter(t){return Ss(t).filter(this.createFilter())}test(t){return this._test(t,this._testCache,!0)}},Cr=e=>new wr(e),Ku=e=>Fe(e&&Fe.convert(e),e,ws),bs=()=>{let e=n=>/^\\\\\?\\/.test(n)||/["<>|\u0000-\u001F]+/u.test(n)?n:n.replace(/\\/g,"/");Fe.convert=e;let t=/^[a-z]:\//i;Fe.isNotRelative=n=>t.test(n)||Cs(n)};typeof process<"u"&&process.platform==="win32"&&bs();Sn.exports=Cr;Cr.default=Cr;Sn.exports.isPathValid=Ku;_t(Sn.exports,Symbol.for("setupWindows"),bs)});var ki=Qo((qv,Mi)=>{"use strict";function Si(e){return Array.isArray(e)?e:[e]}var Jr="",vi=" ",Vr="\\",Wd=/^\s+$/,jd=/(?:[^\\]|^)\\$/,zd=/^\\!/,Gd=/^\\#/,Kd=/\r?\n/g,qd=/^\.*\/|^\.+$/,Xr="/",Ci="node-ignore";typeof Symbol<"u"&&(Ci=Symbol.for("node-ignore"));var Ti=Ci,Vd=(e,t,n)=>Object.defineProperty(e,t,{value:n}),Xd=/([0-z])-([0-z])/g,bi=()=>!1,Jd=e=>e.replace(Xd,(t,n,r)=>n.charCodeAt(0)<=r.charCodeAt(0)?t:Jr),Yd=e=>{let{length:t}=e;return e.slice(0,t-t%2)},Qd=[[/^\uFEFF/,()=>Jr],[/((?:\\\\)*?)(\\?\s+)$/,(e,t,n)=>t+(n.indexOf("\\")===0?vi:Jr)],[/(\\+?)\s/g,(e,t)=>{let{length:n}=t;return t.slice(0,n-n%2)+vi}],[/[\\$.|*+(){^]/g,e=>`\\${e}`],[/(?!\\)\?/g,()=>"[^/]"],[/^\//,()=>"^"],[/\//g,()=>"\\/"],[/^\^*\\\*\\\*\\\//,()=>"^(?:.*\\/)?"],[/^(?=[^^])/,function(){return/\/(?!$)/.test(this)?"^":"(?:^|\\/)"}],[/\\\/\\\*\\\*(?=\\\/|$)/g,(e,t,n)=>t+6<n.length?"(?:\\/[^\\/]+)*":"\\/.+"],[/(^|[^\\]+)(\\\*)+(?=.+)/g,(e,t,n)=>{let r=n.replace(/\\\*/g,"[^\\/]*");return t+r}],[/\\\\\\(?=[$.|*+(){^])/g,()=>Vr],[/\\\\/g,()=>Vr],[/(\\)?\[([^\]/]*?)(\\*)($|\])/g,(e,t,n,r,o)=>t===Vr?`\\[${n}${Yd(r)}${o}`:o==="]"&&r.length%2===0?`[${Jd(n)}${r}]`:"[]"],[/(?:[^*])$/,e=>/\/$/.test(e)?`${e}$`:`${e}(?=$|\\/$)`],[/(\^|\\\/)?\\\*$/,(e,t)=>`${t?`${t}[^/]+`:"[^/]*"}(?=$|\\/$)`]],wi=Object.create(null),Zd=(e,t)=>{let n=wi[e];return n||(n=Qd.reduce((r,[o,s])=>r.replace(o,s.bind(e)),e),wi[e]=n),t?new RegExp(n,"i"):new RegExp(n)},Zr=e=>typeof e=="string",em=e=>e&&Zr(e)&&!Wd.test(e)&&!jd.test(e)&&e.indexOf("#")!==0,tm=e=>e.split(Kd),Yr=class{constructor(t,n,r,o){this.origin=t,this.pattern=n,this.negative=r,this.regex=o}},nm=(e,t)=>{let n=e,r=!1;e.indexOf("!")===0&&(r=!0,e=e.substr(1)),e=e.replace(zd,"!").replace(Gd,"#");let o=Zd(e,t);return new Yr(n,e,r,o)},rm=(e,t)=>{throw new t(e)},je=(e,t,n)=>Zr(e)?e?je.isNotRelative(e)?n(`path should be a \`path.relative()\`d string, but got "${t}"`,RangeError):!0:n("path must not be empty",TypeError):n(`path must be a string, but got \`${t}\``,TypeError),xi=e=>qd.test(e);je.isNotRelative=xi;je.convert=e=>e;var Qr=class{constructor({ignorecase:t=!0,ignoreCase:n=t,allowRelativePaths:r=!1}={}){Vd(this,Ti,!0),this._rules=[],this._ignoreCase=n,this._allowRelativePaths=r,this._initCache()}_initCache(){this._ignoreCache=Object.create(null),this._testCache=Object.create(null)}_addPattern(t){if(t&&t[Ti]){this._rules=this._rules.concat(t._rules),this._added=!0;return}if(em(t)){let n=nm(t,this._ignoreCase);this._added=!0,this._rules.push(n)}}add(t){return this._added=!1,Si(Zr(t)?tm(t):t).forEach(this._addPattern,this),this._added&&this._initCache(),this}addPattern(t){return this.add(t)}_testOne(t,n){let r=!1,o=!1;return this._rules.forEach(s=>{let{negative:i}=s;if(o===i&&r!==o||i&&!r&&!o&&!n)return;s.regex.test(t)&&(r=!i,o=i)}),{ignored:r,unignored:o}}_test(t,n,r,o){let s=t&&je.convert(t);return je(s,t,this._allowRelativePaths?bi:rm),this._t(s,n,r,o)}_t(t,n,r,o){if(t in n)return n[t];if(o||(o=t.split(Xr)),o.pop(),!o.length)return n[t]=this._testOne(t,r);let s=this._t(o.join(Xr)+Xr,n,r,o);return n[t]=s.ignored?s:this._testOne(t,r)}ignores(t){return this._test(t,this._ignoreCache,!1).ignored}createFilter(){return t=>!this.ignores(t)}filter(t){return Si(t).filter(this.createFilter())}test(t){return this._test(t,this._testCache,!0)}},kn=e=>new Qr(e),om=e=>je(e&&je.convert(e),e,bi);kn.isPathValid=om;kn.default=kn;Mi.exports=kn;if(typeof process<"u"&&(process.env&&process.env.IGNORE_TEST_WIN32||process.platform==="win32")){let e=n=>/^\\\\\?\\/.test(n)||/["<>|\u0000-\u001F]+/u.test(n)?n:n.replace(/\\/g,"/");je.convert=e;let t=/^[a-z]:\//i;je.isNotRelative=n=>t.test(n)||xi(n)}});function T_(e){let t=Number.parseInt(e,10);return!Number.isInteger(t)||t<=0||t>65535?null:t}function tl(e){let t={open:!0};for(let n=0;n<e.length;n++){let r=e[n];if(r){if(r==="--open"){t.open=!0;continue}if(r==="--no-open"){t.open=!1;continue}if(r==="--host"){let o=e[n+1];o&&!o.startsWith("-")&&(t.host=o,n+=1);continue}if(r==="--port"){let o=e[n+1];if(o&&!o.startsWith("-")){let s=T_(o);s!==null&&(t.port=s,n+=1)}continue}if(r==="--static-dir"){let o=e[n+1];o&&!o.startsWith("-")&&(t.staticDir=o,n+=1);continue}}}return t}var nl=Yo(()=>{"use strict"});var il={};Tl(il,{runWebCommand:()=>N_});import{spawn as rl}from"child_process";import{existsSync as ol,readFileSync as w_}from"fs";import{createServer as C_}from"net";import{dirname as sn,join as Y,resolve as Ne}from"path";import{fileURLToPath as cr}from"url";function nt(e){let t=Ne(e);for(;;){let n=Y(t,"package.json");if(ol(n))try{if(JSON.parse(w_(n,"utf8")).name==="@memo-code/memo")return t}catch{}let r=sn(t);if(r===t)break;t=r}return null}function lr(e){return ol(e)}function M_(e){let t=[];e&&t.push(Ne(e)),process.env.MEMO_WEB_SERVER_ENTRY&&t.push(Ne(process.env.MEMO_WEB_SERVER_ENTRY));let n=sn(cr(import.meta.url)),r=nt(n)??nt(process.cwd());r&&(t.push(Y(r,"dist/web/server/main.cjs")),t.push(Y(r,"dist/web/server/main.js")),t.push(Y(r,"packages/web-server/dist/main.cjs")),t.push(Y(r,"packages/web-server/dist/main.js"))),t.push(Y(process.cwd(),"dist/web/server/main.cjs")),t.push(Y(process.cwd(),"dist/web/server/main.js")),t.push(Y(process.cwd(),"packages/web-server/dist/main.cjs")),t.push(Y(process.cwd(),"packages/web-server/dist/main.js"));for(let o of t)if(lr(o))return o;return null}function k_(e){let t=[];e&&t.push(Ne(e)),process.env.MEMO_WEB_STATIC_DIR&&t.push(Ne(process.env.MEMO_WEB_STATIC_DIR));let n=sn(cr(import.meta.url)),r=nt(n)??nt(process.cwd());r&&(t.push(Y(r,"dist/web/ui")),t.push(Y(r,"packages/web-ui/dist"))),t.push(Y(process.cwd(),"dist/web/ui")),t.push(Y(process.cwd(),"packages/web-ui/dist"));for(let o of t)if(lr(Y(o,"index.html")))return o;return null}function A_(){let e=sn(cr(import.meta.url)),t=nt(e)??nt(process.cwd()),n=[];t&&(n.push(Y(t,"dist/task-prompts")),n.push(Y(t,"packages/tui/src/task-prompts"))),n.push(Ne(e,"../task-prompts")),n.push(Ne(e,"task-prompts"));for(let r of n)if(lr(Y(r,"init_agents.md")))return r;return n[0]??Ne(e,"../task-prompts")}function P_(){let e=sn(cr(import.meta.url)),t=nt(e)??nt(process.cwd()),n=[];process.env.MEMO_SYSTEM_PROMPT_PATH&&n.push(Ne(process.env.MEMO_SYSTEM_PROMPT_PATH)),t&&(n.push(Y(t,"dist/prompt.md")),n.push(Y(t,"packages/core/src/runtime/prompt.md"))),n.push(Ne(e,"../prompt.md")),n.push(Ne(e,"../../core/src/runtime/prompt.md"));for(let r of n)if(lr(r))return r;return null}async function sl(e,t){return new Promise(n=>{let r=C_();r.unref(),r.once("error",()=>n(!1)),r.listen({host:e,port:t},()=>{r.close(()=>n(!0))})})}async function E_(e,t){for(let n=0;n<30;n++){let r=t+n;if(r>65535)break;if(await sl(e,r))return r}throw new Error(`No available port found from ${t} to ${t+29}`)}function R_(e,t){return t==="darwin"?{command:"open",args:[e]}:t==="win32"?{command:"cmd",args:["/c","start","",e]}:t==="linux"?{command:"xdg-open",args:[e]}:null}function I_(e){let t=R_(e,process.platform);if(!t)return!1;try{return rl(t.command,t.args,{stdio:"ignore",detached:!0}).unref(),!0}catch{return!1}}function O_(e,t){return`http://${e.includes(":")?`[${e}]`:e}:${t}`}async function L_(e,t,n=8e3){let r=Date.now();for(;Date.now()-r<n;){if(!await sl(e,t))return!0;await new Promise(o=>setTimeout(o,120))}return!1}async function N_(e){let t=tl(e),n=t.host??b_,r=t.port??x_,o=await E_(n,r),s=M_();if(!s){console.error("web-server entry not found (main.js missing)."),console.error("Please run `pnpm run web:server:build` or `pnpm run build` first."),process.exitCode=1;return}let i=k_(t.staticDir);if(!i){console.error("web-ui static assets not found (index.html missing)."),console.error("Please run `pnpm run web:ui:build` or `pnpm run build` first."),process.exitCode=1;return}o!==r&&console.log(`[memo web] Port ${r} is busy, using ${o}`);let c=O_(n,o);console.log(`[memo web] Server: ${c}`),console.log(`[memo web] Entry: ${s}`),console.log(`[memo web] Static: ${i}`);let a=A_(),l=P_(),p=rl(process.execPath,[s],{stdio:"inherit",env:{...process.env,MEMO_WEB_HOST:n,MEMO_WEB_PORT:String(o),MEMO_WEB_STATIC_DIR:i,MEMO_CLI_ENTRY:process.argv[1],MEMO_TASK_PROMPTS_DIR:a,...l?{MEMO_SYSTEM_PROMPT_PATH:l}:{}}});t.open&&(!await L_(n,o)||!I_(c))&&console.warn(`[memo web] Failed to auto-open browser. Open manually: ${c}`);let u=d=>{p.killed||p.kill(d)};process.once("SIGINT",()=>{u("SIGINT")}),process.once("SIGTERM",()=>{u("SIGTERM")}),await new Promise(d=>{p.once("exit",(m,g)=>{if(g){process.exitCode=0,d();return}process.exitCode=m??0,d()})})}var b_,x_,al=Yo(()=>{"use strict";nl();b_="127.0.0.1",x_=5494});import{randomUUID as cl}from"crypto";import{readFile as $_}from"fs/promises";import{createInterface as D_}from"readline/promises";import{stdin as U_,stdout as F_}from"process";import{render as H_}from"ink";import ql from"os";import{readFile as as}from"fs/promises";import{existsSync as Vl}from"fs";import{join as Ut,dirname as Xl,resolve as cs}from"path";import{fileURLToPath as Jl}from"url";import{access as Cl,readFile as bl,readdir as xl,stat as Ml}from"fs/promises";import{constants as kl}from"fs";import{homedir as ts}from"os";import{dirname as Al,isAbsolute as Pl,join as ht,resolve as pn}from"path";import El from"fast-glob";var Rl="SKILL.md",Il=6,Ol=200,Ll=64,Nl=1024,$l="- Discovery: The list above is the skills available in this session (name + description + file path). Skill bodies live on disk at the listed paths.\n- Trigger rules: If the user names a skill (with `$SkillName` or plain text) OR the task clearly matches a skill's description shown above, you must use that skill for that turn. Multiple mentions mean use them all. Do not carry skills across turns unless re-mentioned.\n- Missing/blocked: If a named skill isn't in the list or the path can't be read, say so briefly and continue with the best fallback.\n- How to use a skill (progressive disclosure):\n 1) After deciding to use a skill, open its `SKILL.md`. Read only enough to follow the workflow.\n 2) When `SKILL.md` references relative paths (e.g., `scripts/foo.py`), resolve them relative to the skill directory listed above first, and only consider other paths if needed.\n 3) If `SKILL.md` points to extra folders such as `references/`, load only the specific files needed for the request; don't bulk-load everything.\n 4) If `scripts/` exist, prefer running or patching them instead of retyping large code blocks.\n 5) If `assets/` or templates exist, reuse them instead of recreating from scratch.\n- Coordination and sequencing:\n - If multiple skills apply, choose the minimal set that covers the request and state the order you'll use them.\n - Announce which skill(s) you're using and why (one short line). If you skip an obvious skill, say why.\n- Context hygiene:\n - Keep context small: summarize long sections instead of pasting them; only load extra files when needed.\n - Avoid deep reference-chasing: prefer opening only files directly linked from `SKILL.md` unless you're blocked.\n - When variants exist (frameworks, providers, domains), pick only the relevant reference file(s) and note that choice.\n- Safety and fallback: If a skill can't be applied cleanly (missing files, unclear instructions), state the issue, pick the next-best approach, and continue.";function ns(e){return e.trim().split(/\s+/).join(" ")}function Dl(e){let t=e.trim();return t.length>=2&&(t.startsWith('"')&&t.endsWith('"')||t.startsWith("'")&&t.endsWith("'"))?t.slice(1,-1):t}function Ul(e){let t=e.split(/\r?\n/);if(t[0]?.trim()!=="---")return null;let n=[],r=!1;for(let o of t.slice(1)){if(o.trim()==="---"){r=!0;break}n.push(o)}return!r||n.length===0?null:n.join(`
|
|
3
|
-
`)}function Fl(e,t){let n=e.split(/\r?\n/),r=!1,o=[];for(let s of n){if(!r){s.match(new RegExp(`^${t}\\s*:\\s*[|>]\\s*$`))&&(r=!0);continue}if(!/^\s+/.test(s))break;o.push(s.replace(/^\s+/,""))}return o.length===0?null:
|
|
4
|
-
`)}var Yl=/{{\s*([\w.-]+)\s*}}/g
|
|
2
|
+
var yl=Object.create;var fr=Object.defineProperty;var Sl=Object.getOwnPropertyDescriptor;var _l=Object.getOwnPropertyNames;var vl=Object.getPrototypeOf,Tl=Object.prototype.hasOwnProperty;var Zo=(e,t)=>()=>(e&&(t=e(e=0)),t);var es=(e,t)=>()=>(t||e((t={exports:{}}).exports,t),t.exports),wl=(e,t)=>{for(var n in t)fr(e,n,{get:t[n],enumerable:!0})},Cl=(e,t,n,r)=>{if(t&&typeof t=="object"||typeof t=="function")for(let o of _l(t))!Tl.call(e,o)&&o!==n&&fr(e,o,{get:()=>t[o],enumerable:!(r=Sl(t,o))||r.enumerable});return e};var ts=(e,t,n)=>(n=e!=null?yl(vl(e)):{},Cl(t||!e||!e.__esModule?fr(n,"default",{value:e,enumerable:!0}):n,e));var Ms=es((k_,vn)=>{"use strict";function vs(e){return Array.isArray(e)?e:[e]}var Iu=void 0,Tr="",Ss=" ",vr="\\",Ou=/^\s+$/,Lu=/(?:[^\\]|^)\\$/,Nu=/^\\!/,$u=/^\\#/,Du=/\r?\n/g,Uu=/^\.{0,2}\/|^\.{1,2}$/,Hu=/\/$/,St="/",Ts="node-ignore";typeof Symbol<"u"&&(Ts=Symbol.for("node-ignore"));var ws=Ts,_t=(e,t,n)=>(Object.defineProperty(e,t,{value:n}),n),Fu=/([0-z])-([0-z])/g,Cs=()=>!1,Bu=e=>e.replace(Fu,(t,n,r)=>n.charCodeAt(0)<=r.charCodeAt(0)?t:Tr),ju=e=>{let{length:t}=e;return e.slice(0,t-t%2)},Wu=[[/^\uFEFF/,()=>Tr],[/((?:\\\\)*?)(\\?\s+)$/,(e,t,n)=>t+(n.indexOf("\\")===0?Ss:Tr)],[/(\\+?)\s/g,(e,t)=>{let{length:n}=t;return t.slice(0,n-n%2)+Ss}],[/[\\$.|*+(){^]/g,e=>`\\${e}`],[/(?!\\)\?/g,()=>"[^/]"],[/^\//,()=>"^"],[/\//g,()=>"\\/"],[/^\^*\\\*\\\*\\\//,()=>"^(?:.*\\/)?"],[/^(?=[^^])/,function(){return/\/(?!$)/.test(this)?"^":"(?:^|\\/)"}],[/\\\/\\\*\\\*(?=\\\/|$)/g,(e,t,n)=>t+6<n.length?"(?:\\/[^\\/]+)*":"\\/.+"],[/(^|[^\\]+)(\\\*)+(?=.+)/g,(e,t,n)=>{let r=n.replace(/\\\*/g,"[^\\/]*");return t+r}],[/\\\\\\(?=[$.|*+(){^])/g,()=>vr],[/\\\\/g,()=>vr],[/(\\)?\[([^\]/]*?)(\\*)($|\])/g,(e,t,n,r,o)=>t===vr?`\\[${n}${ju(r)}${o}`:o==="]"&&r.length%2===0?`[${Bu(n)}${r}]`:"[]"],[/(?:[^*])$/,e=>/\/$/.test(e)?`${e}$`:`${e}(?=$|\\/$)`]],zu=/(^|\\\/)?\\\*$/,jt="regex",Sn="checkRegex",_s="_",Gu={[jt](e,t){return`${t?`${t}[^/]+`:"[^/]*"}(?=$|\\/$)`},[Sn](e,t){return`${t?`${t}[^/]*`:"[^/]*"}(?=$|\\/$)`}},Ku=e=>Wu.reduce((t,[n,r])=>t.replace(n,r.bind(e)),e),_n=e=>typeof e=="string",qu=e=>e&&_n(e)&&!Ou.test(e)&&!Lu.test(e)&&e.indexOf("#")!==0,Vu=e=>e.split(Du).filter(Boolean),wr=class{constructor(t,n,r,o,s,i){this.pattern=t,this.mark=n,this.negative=s,_t(this,"body",r),_t(this,"ignoreCase",o),_t(this,"regexPrefix",i)}get regex(){let t=_s+jt;return this[t]?this[t]:this._make(jt,t)}get checkRegex(){let t=_s+Sn;return this[t]?this[t]:this._make(Sn,t)}_make(t,n){let r=this.regexPrefix.replace(zu,Gu[t]),o=this.ignoreCase?new RegExp(r,"i"):new RegExp(r);return _t(this,n,o)}},Xu=({pattern:e,mark:t},n)=>{let r=!1,o=e;o.indexOf("!")===0&&(r=!0,o=o.substr(1)),o=o.replace(Nu,"!").replace($u,"#");let s=Ku(o);return new wr(e,t,o,n,r,s)},Cr=class{constructor(t){this._ignoreCase=t,this._rules=[]}_add(t){if(t&&t[ws]){this._rules=this._rules.concat(t._rules._rules),this._added=!0;return}if(_n(t)&&(t={pattern:t}),qu(t.pattern)){let n=Xu(t,this._ignoreCase);this._added=!0,this._rules.push(n)}}add(t){return this._added=!1,vs(_n(t)?Vu(t):t).forEach(this._add,this),this._added}test(t,n,r){let o=!1,s=!1,i;this._rules.forEach(a=>{let{negative:l}=a;s===l&&o!==s||l&&!o&&!s&&!n||!a[r].test(t)||(o=!l,s=l,i=l?Iu:a)});let c={ignored:o,unignored:s};return i&&(c.rule=i),c}},Ju=(e,t)=>{throw new t(e)},He=(e,t,n)=>_n(e)?e?He.isNotRelative(e)?n(`path should be a \`path.relative()\`d string, but got "${t}"`,RangeError):!0:n("path must not be empty",TypeError):n(`path must be a string, but got \`${t}\``,TypeError),bs=e=>Uu.test(e);He.isNotRelative=bs;He.convert=e=>e;var br=class{constructor({ignorecase:t=!0,ignoreCase:n=t,allowRelativePaths:r=!1}={}){_t(this,ws,!0),this._rules=new Cr(n),this._strictPathCheck=!r,this._initCache()}_initCache(){this._ignoreCache=Object.create(null),this._testCache=Object.create(null)}add(t){return this._rules.add(t)&&this._initCache(),this}addPattern(t){return this.add(t)}_test(t,n,r,o){let s=t&&He.convert(t);return He(s,t,this._strictPathCheck?Ju:Cs),this._t(s,n,r,o)}checkIgnore(t){if(!Hu.test(t))return this.test(t);let n=t.split(St).filter(Boolean);if(n.pop(),n.length){let r=this._t(n.join(St)+St,this._testCache,!0,n);if(r.ignored)return r}return this._rules.test(t,!1,Sn)}_t(t,n,r,o){if(t in n)return n[t];if(o||(o=t.split(St).filter(Boolean)),o.pop(),!o.length)return n[t]=this._rules.test(t,r,jt);let s=this._t(o.join(St)+St,n,r,o);return n[t]=s.ignored?s:this._rules.test(t,r,jt)}ignores(t){return this._test(t,this._ignoreCache,!1).ignored}createFilter(){return t=>!this.ignores(t)}filter(t){return vs(t).filter(this.createFilter())}test(t){return this._test(t,this._testCache,!0)}},xr=e=>new br(e),Yu=e=>He(e&&He.convert(e),e,Cs),xs=()=>{let e=n=>/^\\\\\?\\/.test(n)||/["<>|\u0000-\u001F]+/u.test(n)?n:n.replace(/\\/g,"/");He.convert=e;let t=/^[a-z]:\//i;He.isNotRelative=n=>t.test(n)||bs(n)};typeof process<"u"&&process.platform==="win32"&&xs();vn.exports=xr;xr.default=xr;vn.exports.isPathValid=Yu;_t(vn.exports,Symbol.for("setupWindows"),xs)});var Ai=es((Qv,ki)=>{"use strict";function vi(e){return Array.isArray(e)?e:[e]}var Qr="",Ti=" ",Jr="\\",qd=/^\s+$/,Vd=/(?:[^\\]|^)\\$/,Xd=/^\\!/,Jd=/^\\#/,Yd=/\r?\n/g,Qd=/^\.*\/|^\.+$/,Yr="/",bi="node-ignore";typeof Symbol<"u"&&(bi=Symbol.for("node-ignore"));var wi=bi,Zd=(e,t,n)=>Object.defineProperty(e,t,{value:n}),em=/([0-z])-([0-z])/g,xi=()=>!1,tm=e=>e.replace(em,(t,n,r)=>n.charCodeAt(0)<=r.charCodeAt(0)?t:Qr),nm=e=>{let{length:t}=e;return e.slice(0,t-t%2)},rm=[[/^\uFEFF/,()=>Qr],[/((?:\\\\)*?)(\\?\s+)$/,(e,t,n)=>t+(n.indexOf("\\")===0?Ti:Qr)],[/(\\+?)\s/g,(e,t)=>{let{length:n}=t;return t.slice(0,n-n%2)+Ti}],[/[\\$.|*+(){^]/g,e=>`\\${e}`],[/(?!\\)\?/g,()=>"[^/]"],[/^\//,()=>"^"],[/\//g,()=>"\\/"],[/^\^*\\\*\\\*\\\//,()=>"^(?:.*\\/)?"],[/^(?=[^^])/,function(){return/\/(?!$)/.test(this)?"^":"(?:^|\\/)"}],[/\\\/\\\*\\\*(?=\\\/|$)/g,(e,t,n)=>t+6<n.length?"(?:\\/[^\\/]+)*":"\\/.+"],[/(^|[^\\]+)(\\\*)+(?=.+)/g,(e,t,n)=>{let r=n.replace(/\\\*/g,"[^\\/]*");return t+r}],[/\\\\\\(?=[$.|*+(){^])/g,()=>Jr],[/\\\\/g,()=>Jr],[/(\\)?\[([^\]/]*?)(\\*)($|\])/g,(e,t,n,r,o)=>t===Jr?`\\[${n}${nm(r)}${o}`:o==="]"&&r.length%2===0?`[${tm(n)}${r}]`:"[]"],[/(?:[^*])$/,e=>/\/$/.test(e)?`${e}$`:`${e}(?=$|\\/$)`],[/(\^|\\\/)?\\\*$/,(e,t)=>`${t?`${t}[^/]+`:"[^/]*"}(?=$|\\/$)`]],Ci=Object.create(null),om=(e,t)=>{let n=Ci[e];return n||(n=rm.reduce((r,[o,s])=>r.replace(o,s.bind(e)),e),Ci[e]=n),t?new RegExp(n,"i"):new RegExp(n)},to=e=>typeof e=="string",sm=e=>e&&to(e)&&!qd.test(e)&&!Vd.test(e)&&e.indexOf("#")!==0,im=e=>e.split(Yd),Zr=class{constructor(t,n,r,o){this.origin=t,this.pattern=n,this.negative=r,this.regex=o}},am=(e,t)=>{let n=e,r=!1;e.indexOf("!")===0&&(r=!0,e=e.substr(1)),e=e.replace(Xd,"!").replace(Jd,"#");let o=om(e,t);return new Zr(n,e,r,o)},cm=(e,t)=>{throw new t(e)},We=(e,t,n)=>to(e)?e?We.isNotRelative(e)?n(`path should be a \`path.relative()\`d string, but got "${t}"`,RangeError):!0:n("path must not be empty",TypeError):n(`path must be a string, but got \`${t}\``,TypeError),Mi=e=>Qd.test(e);We.isNotRelative=Mi;We.convert=e=>e;var eo=class{constructor({ignorecase:t=!0,ignoreCase:n=t,allowRelativePaths:r=!1}={}){Zd(this,wi,!0),this._rules=[],this._ignoreCase=n,this._allowRelativePaths=r,this._initCache()}_initCache(){this._ignoreCache=Object.create(null),this._testCache=Object.create(null)}_addPattern(t){if(t&&t[wi]){this._rules=this._rules.concat(t._rules),this._added=!0;return}if(sm(t)){let n=am(t,this._ignoreCase);this._added=!0,this._rules.push(n)}}add(t){return this._added=!1,vi(to(t)?im(t):t).forEach(this._addPattern,this),this._added&&this._initCache(),this}addPattern(t){return this.add(t)}_testOne(t,n){let r=!1,o=!1;return this._rules.forEach(s=>{let{negative:i}=s;if(o===i&&r!==o||i&&!r&&!o&&!n)return;s.regex.test(t)&&(r=!i,o=i)}),{ignored:r,unignored:o}}_test(t,n,r,o){let s=t&&We.convert(t);return We(s,t,this._allowRelativePaths?xi:cm),this._t(s,n,r,o)}_t(t,n,r,o){if(t in n)return n[t];if(o||(o=t.split(Yr)),o.pop(),!o.length)return n[t]=this._testOne(t,r);let s=this._t(o.join(Yr)+Yr,n,r,o);return n[t]=s.ignored?s:this._testOne(t,r)}ignores(t){return this._test(t,this._ignoreCache,!1).ignored}createFilter(){return t=>!this.ignores(t)}filter(t){return vi(t).filter(this.createFilter())}test(t){return this._test(t,this._testCache,!0)}},An=e=>new eo(e),lm=e=>We(e&&We.convert(e),e,xi);An.isPathValid=lm;An.default=An;ki.exports=An;if(typeof process<"u"&&(process.env&&process.env.IGNORE_TEST_WIN32||process.platform==="win32")){let e=n=>/^\\\\\?\\/.test(n)||/["<>|\u0000-\u001F]+/u.test(n)?n:n.replace(/\\/g,"/");We.convert=e;let t=/^[a-z]:\//i;We.isNotRelative=n=>t.test(n)||Mi(n)}});function MS(e){let t=Number.parseInt(e,10);return!Number.isInteger(t)||t<=0||t>65535?null:t}function nl(e){let t={open:!0};for(let n=0;n<e.length;n++){let r=e[n];if(r){if(r==="--open"){t.open=!0;continue}if(r==="--no-open"){t.open=!1;continue}if(r==="--host"){let o=e[n+1];o&&!o.startsWith("-")&&(t.host=o,n+=1);continue}if(r==="--port"){let o=e[n+1];if(o&&!o.startsWith("-")){let s=MS(o);s!==null&&(t.port=s,n+=1)}continue}if(r==="--static-dir"){let o=e[n+1];o&&!o.startsWith("-")&&(t.staticDir=o,n+=1);continue}}}return t}var rl=Zo(()=>{"use strict"});var al={};wl(al,{runWebCommand:()=>FS});import{spawn as ol}from"child_process";import{existsSync as sl,readFileSync as kS}from"fs";import{createServer as AS}from"net";import{dirname as an,join as Y,resolve as Ne}from"path";import{fileURLToPath as lr}from"url";function rt(e){let t=Ne(e);for(;;){let n=Y(t,"package.json");if(sl(n))try{if(JSON.parse(kS(n,"utf8")).name==="@memo-code/memo")return t}catch{}let r=an(t);if(r===t)break;t=r}return null}function ur(e){return sl(e)}function RS(e){let t=[];e&&t.push(Ne(e)),process.env.MEMO_WEB_SERVER_ENTRY&&t.push(Ne(process.env.MEMO_WEB_SERVER_ENTRY));let n=an(lr(import.meta.url)),r=rt(n)??rt(process.cwd());r&&(t.push(Y(r,"dist/web/server/main.cjs")),t.push(Y(r,"dist/web/server/main.js")),t.push(Y(r,"packages/web-server/dist/main.cjs")),t.push(Y(r,"packages/web-server/dist/main.js"))),t.push(Y(process.cwd(),"dist/web/server/main.cjs")),t.push(Y(process.cwd(),"dist/web/server/main.js")),t.push(Y(process.cwd(),"packages/web-server/dist/main.cjs")),t.push(Y(process.cwd(),"packages/web-server/dist/main.js"));for(let o of t)if(ur(o))return o;return null}function IS(e){let t=[];e&&t.push(Ne(e)),process.env.MEMO_WEB_STATIC_DIR&&t.push(Ne(process.env.MEMO_WEB_STATIC_DIR));let n=an(lr(import.meta.url)),r=rt(n)??rt(process.cwd());r&&(t.push(Y(r,"dist/web/ui")),t.push(Y(r,"packages/web-ui/dist"))),t.push(Y(process.cwd(),"dist/web/ui")),t.push(Y(process.cwd(),"packages/web-ui/dist"));for(let o of t)if(ur(Y(o,"index.html")))return o;return null}function OS(){let e=an(lr(import.meta.url)),t=rt(e)??rt(process.cwd()),n=[];t&&(n.push(Y(t,"dist/task-prompts")),n.push(Y(t,"packages/tui/src/task-prompts"))),n.push(Ne(e,"../task-prompts")),n.push(Ne(e,"task-prompts"));for(let r of n)if(ur(Y(r,"init_agents.md")))return r;return n[0]??Ne(e,"../task-prompts")}function LS(){let e=an(lr(import.meta.url)),t=rt(e)??rt(process.cwd()),n=[];process.env.MEMO_SYSTEM_PROMPT_PATH&&n.push(Ne(process.env.MEMO_SYSTEM_PROMPT_PATH)),t&&(n.push(Y(t,"dist/prompt.md")),n.push(Y(t,"packages/core/src/runtime/prompt.md"))),n.push(Ne(e,"../prompt.md")),n.push(Ne(e,"../../core/src/runtime/prompt.md"));for(let r of n)if(ur(r))return r;return null}async function il(e,t){return new Promise(n=>{let r=AS();r.unref(),r.once("error",()=>n(!1)),r.listen({host:e,port:t},()=>{r.close(()=>n(!0))})})}async function NS(e,t){for(let n=0;n<30;n++){let r=t+n;if(r>65535)break;if(await il(e,r))return r}throw new Error(`No available port found from ${t} to ${t+29}`)}function $S(e,t){return t==="darwin"?{command:"open",args:[e]}:t==="win32"?{command:"cmd",args:["/c","start","",e]}:t==="linux"?{command:"xdg-open",args:[e]}:null}function DS(e){let t=$S(e,process.platform);if(!t)return!1;try{return ol(t.command,t.args,{stdio:"ignore",detached:!0}).unref(),!0}catch{return!1}}function US(e,t){return`http://${e.includes(":")?`[${e}]`:e}:${t}`}async function HS(e,t,n=8e3){let r=Date.now();for(;Date.now()-r<n;){if(!await il(e,t))return!0;await new Promise(o=>setTimeout(o,120))}return!1}async function FS(e){let t=nl(e),n=t.host??PS,r=t.port??ES,o=await NS(n,r),s=RS();if(!s){console.error("web-server entry not found (main.js missing)."),console.error("Please run `pnpm run web:server:build` or `pnpm run build` first."),process.exitCode=1;return}let i=IS(t.staticDir);if(!i){console.error("web-ui static assets not found (index.html missing)."),console.error("Please run `pnpm run web:ui:build` or `pnpm run build` first."),process.exitCode=1;return}o!==r&&console.log(`[memo web] Port ${r} is busy, using ${o}`);let c=US(n,o);console.log(`[memo web] Server: ${c}`),console.log(`[memo web] Entry: ${s}`),console.log(`[memo web] Static: ${i}`);let a=OS(),l=LS(),p=ol(process.execPath,[s],{stdio:"inherit",env:{...process.env,MEMO_WEB_HOST:n,MEMO_WEB_PORT:String(o),MEMO_WEB_STATIC_DIR:i,MEMO_CLI_ENTRY:process.argv[1],MEMO_TASK_PROMPTS_DIR:a,...l?{MEMO_SYSTEM_PROMPT_PATH:l}:{}}});t.open&&(!await HS(n,o)||!DS(c))&&console.warn(`[memo web] Failed to auto-open browser. Open manually: ${c}`);let u=d=>{p.killed||p.kill(d)};process.once("SIGINT",()=>{u("SIGINT")}),process.once("SIGTERM",()=>{u("SIGTERM")}),await new Promise(d=>{p.once("exit",(m,g)=>{if(g){process.exitCode=0,d();return}process.exitCode=m??0,d()})})}var PS,ES,cl=Zo(()=>{"use strict";rl();PS="127.0.0.1",ES=5494});import{randomUUID as ll}from"crypto";import{readFile as BS}from"fs/promises";import{createInterface as jS}from"readline/promises";import{stdin as WS,stdout as zS}from"process";import{render as GS}from"ink";import ls from"os";import{readFile as gr}from"fs/promises";import{existsSync as Vl}from"fs";import{join as Je,dirname as Xl,resolve as Ht}from"path";import{fileURLToPath as Jl}from"url";import{access as bl,readFile as xl,readdir as Ml,stat as kl}from"fs/promises";import{constants as Al}from"fs";import{homedir as rs}from"os";import{dirname as Pl,isAbsolute as El,join as yt,resolve as dn}from"path";import Rl from"fast-glob";var Il="SKILL.md",Ol=6,Ll=200,Nl=64,$l=1024,Dl="- Discovery: The list above is the skills available in this session (name + description + file path). Skill bodies live on disk at the listed paths.\n- Trigger rules: If the user names a skill (with `$SkillName` or plain text) OR the task clearly matches a skill's description shown above, you must use that skill for that turn. Multiple mentions mean use them all. Do not carry skills across turns unless re-mentioned.\n- Missing/blocked: If a named skill isn't in the list or the path can't be read, say so briefly and continue with the best fallback.\n- How to use a skill (progressive disclosure):\n 1) After deciding to use a skill, open its `SKILL.md`. Read only enough to follow the workflow.\n 2) When `SKILL.md` references relative paths (e.g., `scripts/foo.py`), resolve them relative to the skill directory listed above first, and only consider other paths if needed.\n 3) If `SKILL.md` points to extra folders such as `references/`, load only the specific files needed for the request; don't bulk-load everything.\n 4) If `scripts/` exist, prefer running or patching them instead of retyping large code blocks.\n 5) If `assets/` or templates exist, reuse them instead of recreating from scratch.\n- Coordination and sequencing:\n - If multiple skills apply, choose the minimal set that covers the request and state the order you'll use them.\n - Announce which skill(s) you're using and why (one short line). If you skip an obvious skill, say why.\n- Context hygiene:\n - Keep context small: summarize long sections instead of pasting them; only load extra files when needed.\n - Avoid deep reference-chasing: prefer opening only files directly linked from `SKILL.md` unless you're blocked.\n - When variants exist (frameworks, providers, domains), pick only the relevant reference file(s) and note that choice.\n- Safety and fallback: If a skill can't be applied cleanly (missing files, unclear instructions), state the issue, pick the next-best approach, and continue.";function os(e){return e.trim().split(/\s+/).join(" ")}function Ul(e){let t=e.trim();return t.length>=2&&(t.startsWith('"')&&t.endsWith('"')||t.startsWith("'")&&t.endsWith("'"))?t.slice(1,-1):t}function Hl(e){let t=e.split(/\r?\n/);if(t[0]?.trim()!=="---")return null;let n=[],r=!1;for(let o of t.slice(1)){if(o.trim()==="---"){r=!0;break}n.push(o)}return!r||n.length===0?null:n.join(`
|
|
3
|
+
`)}function Fl(e,t){let n=e.split(/\r?\n/),r=!1,o=[];for(let s of n){if(!r){s.match(new RegExp(`^${t}\\s*:\\s*[|>]\\s*$`))&&(r=!0);continue}if(!/^\s+/.test(s))break;o.push(s.replace(/^\s+/,""))}return o.length===0?null:os(o.join(" "))}function ns(e,t){let n=Fl(e,t);if(n)return n;let r=new RegExp(`^${t}\\s*:\\s*(.+?)\\s*$`,"m"),o=e.match(r);return o?.[1]?os(Ul(o[1])):null}function Bl(e,t){let n=Hl(e);if(!n)return null;let r=ns(n,"name"),o=ns(n,"description");return!r||!o||r.length>Nl||o.length>$l?null:{name:r,description:o,path:t}}function ss(e,t){return e==="~"?t:e.startsWith("~/")?yt(t,e.slice(2)):e}async function jl(e){try{return(await kl(e)).isDirectory()}catch{return!1}}async function Wl(e){try{return await bl(yt(e,".git"),Al.F_OK),!0}catch{return!1}}async function zl(e){let t=dn(e),n=t;for(;;){if(await Wl(n))return n;let r=Pl(n);if(r===n)break;n=r}return t}async function Gl(e){let t=[yt(e,".agents","skills")];try{let r=(await Ml(e,{withFileTypes:!0})).filter(o=>o.isDirectory()&&o.name.startsWith(".")).map(o=>o.name).filter(o=>o!==".git").sort((o,s)=>o.localeCompare(s));for(let o of r)t.push(yt(e,o,"skills"))}catch{return pn(t)}return pn(t)}function pn(e){let t=[],n=new Set;for(let r of e){let o=dn(r);n.has(o)||(n.add(o),t.push(o))}return t}async function Kl(e){let t=e.cwd??process.cwd(),n=e.homeDir??rs(),r=ss(e.memoHome??process.env.MEMO_HOME??yt(n,".memo"),n),o=await zl(t),s=await Gl(o);return s.push(yt(r,"skills")),pn(s)}async function ql(e){if(e.skillRoots&&e.skillRoots.length>0){let t=e.homeDir??rs(),n=e.skillRoots.map(r=>{let o=ss(r,t);return El(o)?o:dn(o)});return pn(n)}return Kl(e)}async function is(e={}){let t=await ql(e),n=Math.max(1,e.maxSkills??Ll),r=[],o=new Set;for(let s of t){if(!await jl(s))continue;let i=await Rl(`**/${Il}`,{cwd:s,absolute:!0,onlyFiles:!0,deep:Ol,caseSensitiveMatch:!1,followSymbolicLinks:!0,suppressErrors:!0,unique:!0,ignore:["**/.git/**","**/node_modules/**"]});i.sort((c,a)=>c.localeCompare(a));for(let c of i){let a=dn(c);if(o.has(a))continue;let l;try{l=await xl(a,"utf-8")}catch{continue}let p=Bl(l,a);if(p&&(r.push(p),o.add(a),r.length>=n))return r}}return r}function as(e){if(e.length===0)return null;let t=[];t.push("## Skills"),t.push("A skill is a set of local instructions to follow that is stored in a `SKILL.md` file. Below is the list of skills that can be used. Each entry includes a name, description, and file path so you can open the source for full instructions when using a specific skill."),t.push("### Available skills");for(let n of e)t.push(`- ${n.name}: ${n.description} (file: ${n.path})`);return t.push("### How to use skills"),t.push(Dl),t.join(`
|
|
4
|
+
`)}var Yl=/{{\s*([\w.-]+)\s*}}/g,Ql=/{{\s*soul_section\s*}}/;function Zl(e,t){return e.replace(Yl,(n,r)=>t[r]??"")}function eu(){try{return ls.userInfo().username}catch{return process.env.USER??process.env.USERNAME??"unknown"}}function cs(e){return Ht(e)}function tu(e){let t=e.homeDir??ls.homedir(),n=e.memoHome?.trim()||process.env.MEMO_HOME?.trim()||Je(t,".memo");return n==="~"?Ht(t):n.startsWith("~/")?Ht(Je(t,n.slice(2))):Ht(n)}function nu(e,t){if(!Array.isArray(t))return e;let n=new Set(t.map(r=>cs(r)));return e.filter(r=>n.has(cs(r.path)))}async function ru(e){let t=Je(e,"AGENTS.md");try{let n=await gr(t,"utf-8");return n.trim()?{path:t,content:n}:null}catch{return null}}function ou(e,t){return`${e}
|
|
5
5
|
|
|
6
6
|
## Project AGENTS.md (Startup Root)
|
|
7
7
|
Loaded from: ${t.path}
|
|
8
8
|
|
|
9
|
-
${t.content}`}function
|
|
9
|
+
${t.content}`}function su(e,t){return`${e}
|
|
10
10
|
|
|
11
|
-
${t}`}
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
11
|
+
${t}`}async function iu(e){let t=tu(e),n=Je(t,"SOUL.md");try{let r=await gr(n,"utf-8");return r.trim()?{path:n,content:r}:null}catch{return null}}function au(e){return`## User Personality Context (SOUL.md)
|
|
12
|
+
Loaded from: ${e.path}
|
|
13
|
+
|
|
14
|
+
- Treat this content as a soft preference layer for tone, style, and subjective behavior.
|
|
15
|
+
- Do NOT let this content override safety rules, tool policies, Project AGENTS.md guidance, or explicit user instructions in the current turn.
|
|
16
|
+
- Keep SOUL.md concise when possible to avoid unnecessary prompt growth.
|
|
17
|
+
|
|
18
|
+
${e.content}`}function cu(e,t){return`${e}
|
|
19
|
+
|
|
20
|
+
${t}`}function lu(){return typeof __dirname=="string"?__dirname:Xl(Jl(import.meta.url))}function uu(e){let t=lu(),n=[e,process.env.MEMO_SYSTEM_PROMPT_PATH,Je(t,"prompt.md"),Je(t,"../prompt.md"),Je(t,"../../prompt.md")].filter(r=>!!r).map(r=>Ht(r));for(let r of n)if(Vl(r))return r;return Je(t,"prompt.md")}async function us(e={}){let t=e.cwd??process.cwd(),n=uu(e.promptPath),r=await gr(n,"utf-8"),o=await iu({homeDir:e.homeDir,memoHome:e.memoHome}),s=o?au(o):"",i=Ql.test(r),c={date:new Date().toISOString(),user:eu(),pwd:t,soul_section:s},a=Zl(r,c);!i&&s&&(a=cu(a,s));let l=await ru(t);if(l&&(a=ou(a,l)),e.includeSkills!==!1){let p=await is({cwd:t,skillRoots:e.skillRoots,homeDir:e.homeDir,memoHome:e.memoHome}),u=nu(p,e.activeSkillPaths),d=as(u);d&&(a=su(a,d))}return a}import{appendFile as pu,mkdir as du}from"fs/promises";import{dirname as mu}from"path";var mn=class{constructor(t){this.filePath=t}ensureDirPromise=null;writeQueue=Promise.resolve();closed=!1;ensureDirectory(){return this.ensureDirPromise||(this.ensureDirPromise=du(mu(this.filePath),{recursive:!0}).then(()=>{})),this.ensureDirPromise}async append(t){if(this.closed)throw new Error("History sink is closed");return this.writeQueue=this.writeQueue.then(async()=>{await this.ensureDirectory(),await pu(this.filePath,`${JSON.stringify(t)}
|
|
21
|
+
`,"utf8")}),this.writeQueue}async flush(){await this.writeQueue}async close(){this.closed||(this.closed=!0,await this.flush())}};function ps(e){return{ts:new Date().toISOString(),sessionId:e.sessionId,turn:e.turn,step:e.step,type:e.type,content:e.content,role:e.role,meta:e.meta}}import{basename as gs}from"path";import{createHash as fu}from"crypto";import{basename as y_,resolve as gu}from"path";function hu(e){return e==="/"?e:e.replace(/\/+$/g,"")}function fn(e){let n=gu(e.trim()).replace(/\\/g,"/");return n==="/"?n:hu(n)}function ds(e){let t=fn(e);return fu("sha256").update(t).digest("hex").slice(0,16)}function ms(e,t){let n=fn(e),r=fn(t);return n===r?!0:n.startsWith(`${r}/`)}function hr(){return{prompt:0,completion:0,total:0}}function yu(){return{total:0,success:0,failed:0,denied:0,cancelled:0}}function de(e){return typeof e!="string"?"":e.trim()}function yr(e){let t=de(e);return t?t.replace(/<\s*(think|thinking)\b[^>]*>[\s\S]*?<\s*\/\s*\1\s*>/gi," ").replace(/<\s*\/?\s*(think|thinking)\b[^>]*>/gi," ").replace(/\s+/g," ").trim():""}function Bt(e){if(typeof e=="number"&&Number.isFinite(e))return e;if(typeof e=="string"){let t=Number.parseFloat(e);if(Number.isFinite(t))return t}return null}function ct(e){return typeof e=="object"&&e!==null&&!Array.isArray(e)}function Su(e,t){for(let s of t){let i=s.meta?.sessionId;if(typeof i=="string"&&i.trim())return i.trim()}let n=t.find(s=>{if(!s.meta)return!1;let i=s.meta.sessionId;return typeof i=="string"&&i.trim().length>0});if(n?.meta&&typeof n.meta.sessionId=="string")return n.meta.sessionId;let r=gs(e,".jsonl"),o=r.lastIndexOf("-");return o<=0||o>=r.length-1?r:r.slice(o+1)}function _u(e,t){if(e){let r=e.replace(/\\/g,"/").split("/").filter(Boolean),o=r[r.length-1];if(o)return o}return gs(t,".jsonl")}function Ft(e,t){let n=e.turnsById.get(t);if(n)return n;let r={turn:t,steps:[],byStep:new Map};return e.turnsById.set(t,r),r}function Sr(e,t){let n=e.byStep.get(t);if(n)return n;let r={step:t};return e.byStep.set(t,r),e.steps=Array.from(e.byStep.values()).sort((o,s)=>o.step-s.step),r}function vu(e,t){if(!e.trim())return null;let n;try{n=JSON.parse(e)}catch{return null}if(!ct(n))return null;let r=de(n.ts),o=de(n.type);if(!r||!o)return null;let s={index:t,ts:r,type:o},i=Bt(n.turn),c=Bt(n.step);i!==null&&(s.turn=Math.floor(i)),c!==null&&(s.step=Math.floor(c));let a=de(n.role);return a&&(s.role=a),typeof n.content=="string"&&(s.content=n.content),ct(n.meta)&&(s.meta=n.meta),s}function gn(e,t){if(!t)return;let n=Bt(t.prompt),r=Bt(t.completion),o=Bt(t.total);n!==null&&(e.prompt+=Math.floor(n)),r!==null&&(e.completion+=Math.floor(r)),o!==null&&(e.total+=Math.floor(o))}function fs(e){if(!e)return"idle";let t=e.trim().toLowerCase();return t?t==="cancelled"?"cancelled":t==="error"||t==="prompt_limit"?"error":t==="running"?"running":"idle":"idle"}function Tu(e){if(!e)return[];let t=[],n=de(e.tool);n&&t.push(n);let r=e.tools;if(Array.isArray(r))for(let s of r){let i=de(s);i&&t.push(i)}let o=e.toolBlocks;if(Array.isArray(o))for(let s of o){if(!ct(s))continue;let i=de(s.name);i&&t.push(i)}return Array.from(new Set(t))}function wu(e,t){if(!t)return;let n=t.trim().toLowerCase();if(n){if(n==="success"){e.toolUsage.success+=1;return}if(n==="approval_denied"){e.toolUsage.denied+=1,e.toolUsage.failed+=1,e.hasCancelled=!0;return}if(n==="cancelled"){e.toolUsage.cancelled+=1,e.hasCancelled=!0;return}e.toolUsage.failed+=1,e.hasError=!0}}function Cu(e){let t=e.split(`
|
|
22
|
+
`),n=[];for(let r=0;r<t.length;r+=1){let o=vu(t[r]??"",r);o&&n.push(o)}return n}function hn(e,t){let n=Cu(e),r=new Date().toISOString(),o={sessionId:"",title:"",project:"",cwd:"",startedAt:n[0]?.ts??r,updatedAt:n[n.length-1]?.ts??n[0]?.ts??r,status:"idle",turnCount:0,tokenUsage:hr(),toolUsage:yu(),turnsById:new Map,summaryParts:[],hasError:!1,hasCancelled:!1};for(let a of n){if(a.ts>o.updatedAt&&(o.updatedAt=a.ts),a.type==="session_start"&&a.meta){let l=de(a.meta.cwd);l&&(o.cwd=l)}if(a.type==="session_title"){let l=yr(a.content);l&&(o.title=l)}if(a.type==="turn_start"){let l=a.turn??o.turnCount+1,p=Ft(o,l);p.input=a.content,p.startedAt=a.ts,o.turnCount=Math.max(o.turnCount,l),a.content&&a.content.trim()&&(o.title||(o.title=yr(a.content)),o.summaryParts.push(`User: ${a.content.trim()}`))}if(a.type==="assistant"&&typeof a.turn=="number"){let l=Ft(o,a.turn),p=Sr(l,a.step??l.steps.length),u=p.assistantText??"";p.assistantText=`${u}${a.content??""}`,a.content?.trim()&&o.summaryParts.push(`Assistant: ${a.content.trim()}`)}if(a.type==="action"&&typeof a.turn=="number"){let l=Ft(o,a.turn),p=Sr(l,a.step??l.steps.length),u=Tu(a.meta);for(let g of u)o.toolUsage.total+=1;let d=de(a.meta?.tool);d&&(p.action={tool:d,input:a.meta?.input});let m=de(a.meta?.thinking);if(m&&(p.thinking=m),Array.isArray(a.meta?.toolBlocks)){let g=a.meta.toolBlocks.filter(ct).map(y=>{let b=de(y.name);return b?{tool:b,input:y.input}:null}).filter(y=>!!y);g.length>1&&(p.parallelActions=g)}}if(a.type==="observation"&&typeof a.turn=="number"){let l=Ft(o,a.turn),p=Sr(l,a.step??l.steps.length);p.observation=a.content;let u=de(a.meta?.status);u&&(p.resultStatus=u),wu(o,u)}if(a.type==="final"&&typeof a.turn=="number"){let l=Ft(o,a.turn);l.finalText=a.content;let p=de(a.meta?.status);if(p){l.status=p;let d=fs(p);d==="error"&&(o.hasError=!0),d==="cancelled"&&(o.hasCancelled=!0)}let u=de(a.meta?.errorMessage);if(u&&(l.errorMessage=u),ct(a.meta?.tokens)){let d=hr();gn(d,a.meta.tokens),l.tokenUsage=d,gn(o.tokenUsage,a.meta.tokens)}}if(a.type==="turn_end"){let l=de(a.meta?.status);if(l){let p=fs(l);p==="error"&&(o.hasError=!0),p==="cancelled"&&(o.hasCancelled=!0)}ct(a.meta?.tokens)&&gn(o.tokenUsage,a.meta.tokens)}a.type==="session_end"&&ct(a.meta?.tokens)&&(o.tokenUsage=hr(),gn(o.tokenUsage,a.meta.tokens))}o.sessionId=Su(t,n),o.project=_u(o.cwd,t),o.title=yr(o.title),o.title||(o.title=o.project||o.sessionId),o.hasError?o.status="error":o.hasCancelled?o.status="cancelled":o.status="idle";let s=o.startedAt.slice(0,10),i=Array.from(o.turnsById.values()).sort((a,l)=>a.turn-l.turn).map(a=>({turn:a.turn,input:a.input,startedAt:a.startedAt,finalText:a.finalText,status:a.status,errorMessage:a.errorMessage,tokenUsage:a.tokenUsage,steps:a.steps}));return{...{id:o.sessionId,sessionId:o.sessionId,filePath:t,title:o.title,project:o.project,workspaceId:o.cwd?ds(o.cwd):"",cwd:o.cwd,date:{day:/^\d{4}-\d{2}-\d{2}$/.test(s)?s:r.slice(0,10),startedAt:o.startedAt,updatedAt:o.updatedAt},status:o.status,turnCount:o.turnCount,tokenUsage:o.tokenUsage,toolUsage:o.toolUsage},summary:o.summaryParts.join(`
|
|
23
|
+
`),turns:i,events:n}}import{readdir as bu,readFile as xu,stat as Mu}from"fs/promises";import{join as ku,resolve as _r}from"path";function Au(e,t){if(typeof e!="number"||!Number.isFinite(e))return t;let n=Math.floor(e);return n>0?n:t}function hs(e,t){if(typeof e!="number"||!Number.isFinite(e))return t;let n=Math.floor(e);return n<=0?t:Math.min(n,100)}function ys(e){if(!e)return null;let t=e.trim();return t&&/^\d{4}-\d{2}-\d{2}$/.test(t)?t:null}async function Pu(e){let t=[],n=async r=>{let o;try{o=await bu(r,{withFileTypes:!0})}catch{return}await Promise.all(o.map(async s=>{if(s.isSymbolicLink())return;let i=ku(r,s.name);if(s.isDirectory()){await n(i);return}if(!(!s.isFile()||!s.name.endsWith(".jsonl")))try{let c=await Mu(i);t.push({filePath:_r(i),mtimeMs:c.mtimeMs,size:c.size})}catch{}}))};return await n(_r(e)),t}function Eu(e,t,n,r){let o=r==="asc"?1:-1;if(n==="project"){let s=e.project.localeCompare(t.project);return s===0?e.date.updatedAt.localeCompare(t.date.updatedAt)*o:s*o}if(n==="title"){let s=e.title.localeCompare(t.title);return s===0?e.date.updatedAt.localeCompare(t.date.updatedAt)*o:s*o}return n==="startedAt"?e.date.startedAt.localeCompare(t.date.startedAt)*o:e.date.updatedAt.localeCompare(t.date.updatedAt)*o}function Ru(e,t){let n=e.project.trim(),r=e.cwd.trim(),o=/^\d{4}-\d{2}-\d{2}T\d{2}[-:]\d{2}[-:]\d{2}(?:\.\d+)?(?:Z)?-[A-Za-z0-9._-]+$/i.test(n)||/^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i.test(n)||n===e.sessionId;if(!n||!r||o||t.workspaceCwd&&!ms(r,t.workspaceCwd))return!1;if(t.project){let a=t.project.trim().toLowerCase();if(a&&n.toLowerCase()!==a)return!1}let s=ys(t.dateFrom),i=ys(t.dateTo);if(s&&e.date.day<s||i&&e.date.day>i)return!1;let c=t.q?.trim().toLowerCase();return!(c&&![e.title,n,r,e.sessionId,e.filePath].join(`
|
|
24
|
+
`).toLowerCase().includes(c))}var yn=class{sessionsDir;cache=new Map;sessionIdToPath=new Map;refreshInFlight=null;constructor(t){this.sessionsDir=_r(t.sessionsDir)}async refresh(){if(this.refreshInFlight){await this.refreshInFlight;return}let t=this.refreshInternal();this.refreshInFlight=t;try{await t}finally{this.refreshInFlight=null}}async list(t={}){await this.refresh();let n=Au(t.page,1),r=hs(t.pageSize,20),o=t.sortBy??"updatedAt",s=t.order??"desc",i=Array.from(this.cache.values()).map(u=>u.summary).filter(u=>Ru(u,t)).sort((u,d)=>Eu(u,d,o,s)),c=i.length,a=c===0?0:Math.ceil(c/r),l=(n-1)*r;return{items:i.slice(l,l+r),page:n,pageSize:r,total:c,totalPages:a}}async getSessionDetail(t){await this.refresh();let n=this.sessionIdToPath.get(t);return n?this.cache.get(n)?.detail??null:null}async getSessionEvents(t,n,r){let o=await this.getSessionDetail(t);if(!o)return null;let s=Number.parseInt(n??"0",10),i=Number.isFinite(s)&&s>=0?s:0,c=hs(r,100),a=o.events.slice(i,i+c),l=i+a.length;return{items:a,nextCursor:l>=o.events.length?null:String(l)}}async getAllSummaries(){return await this.refresh(),Array.from(this.cache.values()).map(t=>t.summary)}async refreshInternal(){let t=await Pu(this.sessionsDir),n=new Set(t.map(r=>r.filePath));for(let r of Array.from(this.cache.keys()))if(!n.has(r)){let o=this.cache.get(r);o&&this.sessionIdToPath.delete(o.summary.sessionId),this.cache.delete(r)}for(let r of t){let o=this.cache.get(r.filePath);if(!(o&&o.mtimeMs===r.mtimeMs&&o.size===r.size))try{let s=await xu(r.filePath,"utf8"),i=hn(s,r.filePath),c=i,a={filePath:r.filePath,mtimeMs:r.mtimeMs,size:r.size,summary:c,detail:i};o&&this.sessionIdToPath.delete(o.summary.sessionId),this.cache.set(r.filePath,a),this.sessionIdToPath.set(c.sessionId,r.filePath)}catch{}}}};var Es=ts(Ms(),1);import{existsSync as ks,statSync as Qu}from"fs";import{readFile as Zu,readdir as ep}from"fs/promises";import{dirname as As,join as Cn,relative as Ps,resolve as tp,sep as np}from"path";var rp=6,op=2500,Rs=25,sp=new Set([".git",".svn",".hg","node_modules","dist","build",".next",".turbo",".cache",".output","coverage","tmp","temp","logs"]),Tn=new Map,Mr=new Map,vt=new Map,ip=256;function wn(e,t){if(vt.size>=ip&&!vt.has(e)){let n=vt.keys().next().value;typeof n=="string"&&vt.delete(n)}vt.set(e,t)}function Is(e){return e.split(np).join("/")}function ap(e){return{maxDepth:typeof e.maxDepth=="number"?Math.max(1,e.maxDepth):rp,maxEntries:typeof e.maxEntries=="number"?Math.max(100,e.maxEntries):op,limit:typeof e.limit=="number"?Math.max(1,e.limit):Rs,respectGitIgnore:e.respectGitIgnore??!0,ignoreGlobs:e.ignoreGlobs?.length?e.ignoreGlobs:[]}}function cp(e,t){let n=e.split("/").filter(Boolean),r=n[n.length-1]??"";return n.some(o=>sp.has(o))||r.endsWith(".log")?!0:t.ignoreGlobs.length?t.ignoreGlobs.some(o=>{let s=o.replace(/\\/g,"/").trim();if(!s)return!1;if(s.endsWith("/**")){let i=s.slice(0,-3);return e.startsWith(i)}if(s.startsWith("*")){let i=s.slice(1);return e.endsWith(i)}return e.includes(s)}):!1}function lp(e){return e.replace(/\\/g,"/")}function up(e){let t=tp(e),n=vt.get(t);if(n)return n;let r=t;try{Qu(r).isFile()&&(r=As(r))}catch{r=process.cwd()}let o=r,s=[];for(;;){if(s.push(r),ks(Cn(r,".gitignore"))||ks(Cn(r,".git"))){for(let c of s)wn(c,r);return wn(t,r),r}let i=As(r);if(i===r){for(let c of s)wn(c,o);return wn(t,o),o}r=i}}async function pp(e){let t=Cn(e,".gitignore");try{return(await Zu(t,"utf8")).split(/\r?\n/).map(r=>r.trim()).filter(r=>r.length>0&&!r.startsWith("#"))}catch{return[]}}function dp(e){let t=(0,Es.default)();return e.length>0&&t.add(e),t}async function mp(e){let t=dp(await pp(e));return{root:e,ignores:n=>{let r=Ps(e,n);return!r||r.startsWith("..")?!1:t.ignores(lp(r))}}}async function fp(e){let t=up(e),n=Mr.get(t);if(n)return n;let r=mp(t).catch(o=>{throw Mr.delete(t),o});return Mr.set(t,r),r}function gp(e){return JSON.stringify({maxDepth:e.maxDepth,maxEntries:e.maxEntries,respectGitIgnore:e.respectGitIgnore,ignoreGlobs:e.ignoreGlobs})}async function hp(e,t){let n=[],r=t.respectGitIgnore?await fp(e):null,o=async(s,i)=>{if(n.length>=t.maxEntries)return;let c;try{c=await ep(s,{withFileTypes:!0})}catch{return}for(let a of c){if(n.length>=t.maxEntries)break;if(a.isSymbolicLink())continue;let l=Cn(s,a.name);if(r?.ignores(l))continue;let p=Ps(e,l);if(!p)continue;let u=Is(p);if(cp(u,t))continue;let d=u.split("/").filter(Boolean),m=a.isDirectory();n.push({path:u,pathLower:u.toLowerCase(),segments:d,segmentsLower:d.map(g=>g.toLowerCase()),depth:i,isDir:m}),m&&i<t.maxDepth&&await o(l,i+1)}};return await o(e,0),n.sort((s,i)=>s.path.localeCompare(i.path)),n}async function yp(e,t){let n=ap(t),r=gp(n),o=Tn.get(e);if(o&&o.signature===r)return o.pending?o.pending:o.entries;let s=hp(e,n).then(i=>(Tn.set(e,{entries:i,signature:r}),i)).catch(i=>{throw Tn.delete(e),i});return Tn.set(e,{entries:[],signature:r,pending:s}),s}function Sp(e,t){if(!t.length)return e.depth+(e.isDir?-.2:.2);let n=e.depth,r=0;for(let o of t){let s=-1;for(let i=r;i<e.segmentsLower.length;i++){let c=e.segmentsLower[i];if(c.startsWith(o)){s=i,n+=(i-r)*1.2,n+=c.length-o.length;break}let a=c.indexOf(o);if(a!==-1){s=i,n+=(i-r)*2+a+2;break}}if(s===-1)return null;r=s+1}return e.isDir&&(n-=.5),n}function _p(e,t,n){let o=t.trim().replace(/\\/g,"/").split("/").filter(Boolean).map(i=>i.toLowerCase()),s=[];for(let i of e){let c=Sp(i,o);c!==null&&s.push({entry:i,score:c})}return s.sort((i,c)=>{let a=i.score-c.score;return a!==0?a:i.entry.path.localeCompare(c.entry.path)}),s.slice(0,n).map(({entry:i})=>({id:i.path,path:i.path,name:i.segments[i.segments.length-1]??i.path,parent:i.segments.length>1?i.segments.slice(0,-1).join("/"):void 0,isDir:i.isDir}))}async function Os(e){let t=await yp(e.cwd,e),n=typeof e.limit=="number"?Math.max(1,e.limit):Rs;return _p(t,e.query,n)}var j={HELP:"help",EXIT:"exit",NEW:"new",RESUME:"resume",REVIEW:"review",MODELS:"models",TOOLS:"tools",COMPACT:"compact",MCP:"mcp",INIT:"init"},me={NONE:"none",ONCE:"once",FULL:"full"};function Wt(e){return`/${e}`}var zt=[{name:j.HELP,description:"Show command and shortcut help"},{name:j.EXIT,description:"Exit current session"},{name:j.NEW,description:"Start a fresh session"},{name:j.RESUME,description:"List and load session history"},{name:j.REVIEW,description:"Review a GitHub pull request and post comments"},{name:j.MODELS,description:"List or switch configured models"},{name:j.TOOLS,description:"Set tool permission mode (none/once/full)"},{name:j.COMPACT,description:"Compact conversation context now"},{name:j.MCP,description:"Show configured MCP servers"},{name:j.INIT,description:"Generate AGENTS.md with agent instructions"}],vp={none:me.NONE,off:me.NONE,disabled:me.NONE,"no-tools":me.NONE,once:me.ONCE,ask:me.ONCE,single:me.ONCE,strict:me.ONCE,full:me.FULL,all:me.FULL,dangerous:me.FULL,"full-access":me.FULL};function Tp(e){if(!e)return null;let t=e.trim().toLowerCase();return t?vp[t]??null:null}function Ls(e){return e===me.NONE?"none (no tools)":e===me.ONCE?"once (approval required)":"full (no approval)"}function Ns(){let e=zt.reduce((n,r)=>Math.max(n,r.name.length),0);return["Available commands:",...zt.map(n=>` ${Wt(n.name).padEnd(e+3)} ${n.description}`)," exit Exit session (without slash)","","Shortcuts:"," Enter Send message"," Shift+Enter New line"," Up/Down Browse local input history"," Tab Accept active suggestion"," Ctrl+L Clear screen and start new session"," Esc Esc Interrupt running turn / clear input"].join(`
|
|
25
|
+
`)}function wp(e){if(!e)return null;let t=e.trim();if(!t)return null;let n=t.match(/^#?(\d+)$/);if(n){let o=Number(n[1]);return Number.isInteger(o)&&o>0?o:null}let r=t.match(/\/pull\/(\d+)(?:[/?#].*)?$/i);if(r){let o=Number(r[1]);return Number.isInteger(o)&&o>0?o:null}return null}function kr(e,t){let[n,...r]=e.trim().slice(1).split(/\s+/);switch((n??"").toLowerCase()){case j.HELP:return{kind:"message",title:"Help",content:Ns()};case j.EXIT:return{kind:"exit"};case j.NEW:return{kind:"new"};case j.RESUME:return{kind:"message",title:"Resume",content:'Type "resume" followed by keywords to load local session history.'};case j.REVIEW:{let s=r.join(" ").trim(),i=wp(s);return i?{kind:"review_pr",prNumber:i}:{kind:"message",title:"Review",content:`Usage: ${Wt(j.REVIEW)} <prNumber>
|
|
26
|
+
Examples: /review 999, /review #999`}}case j.MODELS:{if(!t.providers.length)return{kind:"message",title:"Models",content:`No providers configured. Check ${t.configPath}`};let s=r.join(" ").trim(),i=t.providers.find(l=>l.name===s)??t.providers.find(l=>l.model===s);if(i)return{kind:"switch_model",provider:i};let c=t.providers.map(l=>{let p=l.name===t.providerName&&l.model===t.model?" (current)":"",u=l.base_url?` @ ${l.base_url}`:"";return`- ${l.name}: ${l.model}${u}${p}`});return{kind:"message",title:"Models",content:`${s?`Not found: ${s}
|
|
18
27
|
|
|
19
28
|
`:""}${c.join(`
|
|
20
|
-
`)}`}}case
|
|
21
|
-
Choose one of: ${c}`}:{kind:"message",title:"Tools",content:`Current: ${
|
|
22
|
-
Usage: ${Wt(
|
|
23
|
-
Modes: ${c}`}}case
|
|
24
|
-
`)}}case
|
|
25
|
-
Type ${Wt(
|
|
29
|
+
`)}`}}case j.TOOLS:{let s=r.join(" ").trim(),i=Tp(s),c=["none","once","full"].join(", ");return s?i?i===t.toolPermissionMode?{kind:"message",title:"Tools",content:`Already using ${Ls(i)}.`}:{kind:"set_tool_permission",mode:i}:{kind:"message",title:"Tools",content:`Unsupported mode: ${s}
|
|
30
|
+
Choose one of: ${c}`}:{kind:"message",title:"Tools",content:`Current: ${Ls(t.toolPermissionMode)}
|
|
31
|
+
Usage: ${Wt(j.TOOLS)} <mode>
|
|
32
|
+
Modes: ${c}`}}case j.COMPACT:return{kind:"compact"};case j.MCP:{let s=Object.keys(t.mcpServers);if(!s.length)return{kind:"message",title:"MCP Servers",content:"No MCP servers configured in current config."};let i=[];i.push(`Total: ${s.length}`),i.push("");for(let[c,a]of Object.entries(t.mcpServers))i.push(`- ${c}`),"url"in a?(i.push(` type: ${a.type??"streamable_http"}`),i.push(` url: ${a.url}`),a.bearer_token_env_var&&i.push(` bearer: ${a.bearer_token_env_var}`)):(i.push(` type: ${a.type??"stdio"}`),i.push(` command: ${a.command}`),a.args?.length&&i.push(` args: ${a.args.join(" ")}`)),i.push("");return{kind:"message",title:"MCP Servers",content:i.join(`
|
|
33
|
+
`)}}case j.INIT:return{kind:"init_agents_md"};default:return{kind:"message",title:"Unknown",content:`Unknown command: ${e}
|
|
34
|
+
Type ${Wt(j.HELP)} for available commands.`}}}import{mkdir as Cp,writeFile as bp,readFile as xp,access as Mp}from"fs/promises";import{homedir as Hs}from"os";import{dirname as kp,join as lt,parse as Ap,resolve as Pp}from"path";import{randomUUID as j_}from"crypto";import{parse as Ep}from"toml";var Rp=lt(Hs(),".memo"),Ip="sessions",$s=12e4,Fs=80,Gt={current_provider:"deepseek",mcp_oauth_credentials_store_mode:"auto",auto_compact_threshold_percent:Fs,providers:[{name:"deepseek",env_api_key:"DEEPSEEK_API_KEY",model:"deepseek-chat",base_url:"https://api.deepseek.com"}],mcp_servers:{}};function Bs(e){return typeof e=="number"&&Number.isInteger(e)&&Number.isFinite(e)&&e>=1&&e<=100?e:Fs}function Ar(e){return e.trim().toLowerCase()}function Ds(e){if(typeof e?.context_window=="number"&&Number.isFinite(e.context_window)&&e.context_window>0)return Math.floor(e.context_window)}function Tt(e,t){let n=e.model_profiles;if(!n)return $s;let r=new Map;for(let[c,a]of Object.entries(n))r.set(Ar(c),a);let o=Ar(t.name),s=Ar(t.model),i=`${o}:${s}`;return Ds(r.get(i))??Ds(r.get(s))??$s}function Us(e){return/^[A-Za-z0-9_-]+$/.test(e)?e:JSON.stringify(e)}function Op(e){if(!e||typeof e!="object"||Array.isArray(e))return[];let t=[];for(let[n,r]of Object.entries(e)){if(!r)continue;let o=Array.isArray(r)?r:[r];for(let s of o){if(!s||typeof s!="object")continue;let i={...s};(typeof i.name!="string"||i.name.length===0)&&n&&(i.name=n),t.push(i)}}return t}function Lp(e){if(!Array.isArray(e))return;let t=e.filter(n=>typeof n=="string").map(n=>n.trim()).filter(Boolean);return Array.from(new Set(t))}function Np(e){if(!e||typeof e!="object"||Array.isArray(e))return;let t={};for(let[n,r]of Object.entries(e)){if(!r||typeof r!="object"||Array.isArray(r))continue;let o=r,s={};typeof o.supports_parallel_tool_calls=="boolean"&&(s.supports_parallel_tool_calls=o.supports_parallel_tool_calls),typeof o.supports_reasoning_content=="boolean"&&(s.supports_reasoning_content=o.supports_reasoning_content),typeof o.context_window=="number"&&Number.isFinite(o.context_window)&&o.context_window>0&&(s.context_window=Math.floor(o.context_window)),Object.keys(s).length>0&&(t[n]=s)}return Object.keys(t).length>0?t:void 0}function js(e){return e.startsWith("~")?lt(Hs(),e.slice(1)):e}function $p(e){let t=e.providers.map(c=>{let a=typeof c?.name=="string"?c.name:"";if(!a)return"";let p=[`[[providers.${Us(a)}]]`,`name = ${JSON.stringify(a)}`,`env_api_key = ${JSON.stringify(String(c.env_api_key??""))}`,`model = ${JSON.stringify(String(c.model??""))}`];return c.base_url&&p.push(`base_url = ${JSON.stringify(String(c.base_url))}`),p.join(`
|
|
26
35
|
`)}).filter(Boolean).join(`
|
|
27
36
|
|
|
28
|
-
`),n=e.model_profiles?Object.entries(e.model_profiles).map(([c,a])=>{let l=[],p=
|
|
37
|
+
`),n=e.model_profiles?Object.entries(e.model_profiles).map(([c,a])=>{let l=[],p=Us(c);return l.push(`[model_profiles.${p}]`),typeof a.supports_parallel_tool_calls=="boolean"&&l.push(`supports_parallel_tool_calls = ${a.supports_parallel_tool_calls}`),typeof a.supports_reasoning_content=="boolean"&&l.push(`supports_reasoning_content = ${a.supports_reasoning_content}`),typeof a.context_window=="number"&&Number.isFinite(a.context_window)&&a.context_window>0&&l.push(`context_window = ${Math.floor(a.context_window)}`),l.length>1?l.join(`
|
|
29
38
|
`):""}).filter(Boolean).join(`
|
|
30
39
|
|
|
31
|
-
`):"",r="";e.mcp_servers&&Object.keys(e.mcp_servers).length>0&&(r=Object.entries(e.mcp_servers).map(([c,a])=>{if("url"in a){let y=[`[mcp_servers.${c}]`];y.push(`type = "${a.type??"streamable_http"}"`),y.push(`url = "${a.url}"`),a.bearer_token_env_var&&y.push(`bearer_token_env_var = ${JSON.stringify(a.bearer_token_env_var)}`);let b=a.http_headers??a.headers;if(b&&Object.keys(b).length>0){let U=Object.entries(b).map(([
|
|
40
|
+
`):"",r="";e.mcp_servers&&Object.keys(e.mcp_servers).length>0&&(r=Object.entries(e.mcp_servers).map(([c,a])=>{if("url"in a){let y=[`[mcp_servers.${c}]`];y.push(`type = "${a.type??"streamable_http"}"`),y.push(`url = "${a.url}"`),a.bearer_token_env_var&&y.push(`bearer_token_env_var = ${JSON.stringify(a.bearer_token_env_var)}`);let b=a.http_headers??a.headers;if(b&&Object.keys(b).length>0){let U=Object.entries(b).map(([F,R])=>`${JSON.stringify(F)} = ${JSON.stringify(R)}`).join(", "),T=a.http_headers?"http_headers":"headers";y.push(`${T} = { ${U} }`)}return y.join(`
|
|
32
41
|
`)}let l=a.args?`args = ${JSON.stringify(a.args)}`:"",p=a.type?`type = "${a.type}"
|
|
33
42
|
`:"",u=a.stderr?`stderr = "${a.stderr}"
|
|
34
43
|
`:"",d=`[mcp_servers.${c}]
|
|
@@ -39,46 +48,46 @@ ${u}${l}`.trimEnd(),m=a.env?Object.entries(a.env):[];if(m.length===0)return d;le
|
|
|
39
48
|
[mcp_servers.${c}.env]
|
|
40
49
|
${g}`}).join(`
|
|
41
50
|
|
|
42
|
-
`));let o=[`current_provider = "${e.current_provider}"`];Array.isArray(e.active_mcp_servers)&&o.push(`active_mcp_servers = ${JSON.stringify(e.active_mcp_servers)}`),Array.isArray(e.active_skills)&&o.push(`active_skills = ${JSON.stringify(e.active_skills)}`);let s=e.mcp_oauth_credentials_store_mode;return(s==="auto"||s==="keyring"||s==="file")&&o.push(`mcp_oauth_credentials_store_mode = ${JSON.stringify(s)}`),typeof e.mcp_oauth_callback_port=="number"&&Number.isInteger(e.mcp_oauth_callback_port)&&e.mcp_oauth_callback_port>0&&e.mcp_oauth_callback_port<=65535&&o.push(`mcp_oauth_callback_port = ${e.mcp_oauth_callback_port}`),o.push(`auto_compact_threshold_percent = ${
|
|
51
|
+
`));let o=[`current_provider = "${e.current_provider}"`];Array.isArray(e.active_mcp_servers)&&o.push(`active_mcp_servers = ${JSON.stringify(e.active_mcp_servers)}`),Array.isArray(e.active_skills)&&o.push(`active_skills = ${JSON.stringify(e.active_skills)}`);let s=e.mcp_oauth_credentials_store_mode;return(s==="auto"||s==="keyring"||s==="file")&&o.push(`mcp_oauth_credentials_store_mode = ${JSON.stringify(s)}`),typeof e.mcp_oauth_callback_port=="number"&&Number.isInteger(e.mcp_oauth_callback_port)&&e.mcp_oauth_callback_port>0&&e.mcp_oauth_callback_port<=65535&&o.push(`mcp_oauth_callback_port = ${e.mcp_oauth_callback_port}`),o.push(`auto_compact_threshold_percent = ${Bs(e.auto_compact_threshold_percent)}`),[o.join(`
|
|
43
52
|
`),t,n,r].filter(Boolean).join(`
|
|
44
53
|
|
|
45
|
-
`)}async function we(e,t){await _p(wp(e),{recursive:!0}),await Sp(e,Rp(t),"utf-8")}async function te(){let e=process.env.MEMO_HOME?Bs(process.env.MEMO_HOME):Mp,t=ct(e,"config.toml");try{await Tp(t);let n=await vp(t,"utf-8"),r=xp(n),o=Ap(r.providers),s=Array.isArray(r.active_mcp_servers)?r.active_mcp_servers.filter(m=>typeof m=="string"&&m.trim().length>0):void 0,i=Pp(r.active_skills),c=r.mcp_oauth_credentials_store_mode==="auto"||r.mcp_oauth_credentials_store_mode==="keyring"||r.mcp_oauth_credentials_store_mode==="file"?r.mcp_oauth_credentials_store_mode:zt.mcp_oauth_credentials_store_mode,a=typeof r.mcp_oauth_callback_port=="number"&&Number.isInteger(r.mcp_oauth_callback_port)&&r.mcp_oauth_callback_port>0&&r.mcp_oauth_callback_port<=65535?r.mcp_oauth_callback_port:void 0,l=Hs(r.auto_compact_threshold_percent),p=Ep(r.model_profiles),u={current_provider:r.current_provider??zt.current_provider,mcp_oauth_credentials_store_mode:c,mcp_oauth_callback_port:a,auto_compact_threshold_percent:l,providers:o,model_profiles:p,mcp_servers:r.mcp_servers??{},active_mcp_servers:s,active_skills:i},d=!u.providers.length;return{config:d?zt:u,home:e,configPath:t,needsSetup:d}}catch{return{config:zt,home:e,configPath:t,needsSetup:!0}}}function lt(e,t){let n=t||e.current_provider,r=e.providers.find(o=>o.name===n);return r||(e.providers?.[0]??zt.providers[0])}function Gt(e,t){let n=t.historyDir??ct(e.home,kp),r=Bs(n),o=bp(t.cwd??process.cwd()),s=Cp(o).root,c=o.slice(s.length).split(/[\\/]+/).filter(Boolean);if(process.platform==="win32"){let l=/^([A-Za-z]):/.exec(s)?.[1];l&&c.unshift(l.toUpperCase())}if(c.length===0)return ct(r,"-root");let a=`-${c.map(l=>l.replace(/[^A-Za-z0-9._-]/g,"_")).join("-")}`;return ct(r,a)}function Ws(e,t){let n=new Date,r=String(n.getFullYear()),o=String(n.getMonth()+1).padStart(2,"0"),s=String(n.getDate()).padStart(2,"0"),i=String(n.getHours()).padStart(2,"0"),c=String(n.getMinutes()).padStart(2,"0"),a=String(n.getSeconds()).padStart(2,"0"),l=`${r}-${o}-${s}T${i}-${c}-${a}`,p=t.replace(/[^A-Za-z0-9._-]/g,"_"),u=`${l}-${p}.jsonl`;return ct(e,u)}import{createHash as Ip}from"crypto";import{spawn as Op}from"child_process";import{createServer as Lp}from"http";import{mkdir as Np,readFile as $p,rename as Dp,writeFile as Up}from"fs/promises";import{homedir as Pr}from"os";import{dirname as Fp,join as Ir}from"path";import{auth as js}from"@modelcontextprotocol/sdk/client/auth.js";var Er=1,Hp="mcp-oauth.json",Bp=3e5,Wp=5e3,zs="MCP-Protocol-Version",jp="2024-11-05",Or="memo-code.mcp.oauth",zp=33333;function Gp(e){return{client_name:"Memo Code CLI",redirect_uris:[e],grant_types:["authorization_code","refresh_token"],response_types:["code"],token_endpoint_auth_method:"none"}}function Gs(e){return e instanceof Error?e.message:String(e)}function Lr(e){return new URL(e).toString()}function Nr(e){return e==="file"||e==="keyring"||e==="auto"?e:"auto"}function Ks(e){return e==="~"?Pr():e.startsWith("~/")?Ir(Pr(),e.slice(2)):e}function Kp(e){return e?.memoHome?.trim()?Ks(e.memoHome.trim()):process.env.MEMO_HOME?.trim()?Ks(process.env.MEMO_HOME.trim()):Ir(Pr(),".memo")}function Ys(e){return Ir(Kp(e),"auth",Hp)}function Tt(e){return Ip("sha256").update(Lr(e)).digest("hex")}function qs(){return{version:Er,credentials:{}}}async function $r(e){let t=Ys(e);try{let n=await $p(t,"utf8"),r=JSON.parse(n);return r.version!==Er||!r.credentials?qs():{version:Er,credentials:r.credentials}}catch{return qs()}}async function Qs(e,t){let n=Ys(e),r=`${n}.tmp`;await Np(Fp(n),{recursive:!0}),await Up(r,JSON.stringify(t,null,2),{encoding:"utf8",mode:384}),await Dp(r,n)}var Cn=null;async function Dr(){return Cn||(Cn=(async()=>{try{let t=await new Function("specifier","return import(specifier)")("keytar"),n=t.default??t;return n&&typeof n.getPassword=="function"&&typeof n.setPassword=="function"&&typeof n.deletePassword=="function"?n:null}catch{return null}})(),Cn)}async function kr(e,t){let n=Tt(e);return(await $r(t)).credentials[n]?.credential}async function Ar(e,t,n){let r=Tt(e),o=await $r(n);o.credentials[r]={url:Lr(e),updatedAt:Date.now(),credential:t},await Qs(n,o)}async function Rr(e,t){let n=Tt(e),r=await $r(t);return r.credentials[n]?(delete r.credentials[n],await Qs(t,r),!0):!1}async function Vs(e,t){let n=Tt(t),r=await e.getPassword(Or,n);return r?JSON.parse(r).credential:void 0}async function Xs(e,t,n){let r=Tt(t),o={url:Lr(t),updatedAt:Date.now(),credential:n};await e.setPassword(Or,r,JSON.stringify(o))}async function Js(e,t){let n=Tt(t);return e.deletePassword(Or,n)}function qp(e,t){return new URL(e).origin===t.origin}function Zs(e){return{...e.http_headers??e.headers??{}}}function Vp(e){let t=e.replace(/^\/+|\/+$/g,"");if(!t)return["/.well-known/oauth-authorization-server"];let n=[],r=o=>{n.includes(o)||n.push(o)};return r(`/.well-known/oauth-authorization-server/${t}`),r(`/${t}/.well-known/oauth-authorization-server`),r("/.well-known/oauth-authorization-server"),n}function Xp(e){if(!e||e.length===0)return;let t=e.flatMap(n=>n.split(/[,\s]+/g)).map(n=>n.trim()).filter(Boolean);if(t.length!==0)return t.join(" ")}function ei(e){if(e!==void 0){if(!Number.isInteger(e)||e<=0||e>65535)throw new Error(`Invalid MCP OAuth callback port "${e}". Use an integer between 1 and 65535.`);return e}}function Jp(e){return process.platform==="darwin"?{command:"open",args:[e]}:process.platform==="win32"?{command:"cmd",args:["/c","start","",e]}:{command:"xdg-open",args:[e]}}async function Yp(e){let{command:t,args:n}=Jp(e);await new Promise((r,o)=>{let s=Op(t,n,{detached:!0,stdio:"ignore"}),i=!1,c=p=>{if(!i){if(i=!0,s.off("error",a),s.off("spawn",l),p){o(p);return}r()}},a=p=>{c(p)},l=()=>{c()};s.on("error",a),s.on("spawn",l),s.unref()})}function ti(e,t){return async(n,r)=>{let o=typeof n=="string"||n instanceof URL?new URL(String(n),e):new URL(n.url),s=new Headers(r?.headers??{});if(qp(e,o)){for(let[i,c]of Object.entries(t))s.has(i)||s.set(i,c);s.has(zs)||s.set(zs,jp)}return fetch(o,{...r,headers:s})}}async function ni(e,t){let n=new URL(e),r=ti(e,t),o=Vp(n.pathname);for(let s of o){let i=new URL(n.toString());i.pathname=s,i.search="",i.hash="";let c=new AbortController,a=setTimeout(()=>c.abort(),Wp);try{let l=await r(i,{method:"GET",signal:c.signal});if(!l.ok)continue;let p=await l.json();if(typeof p.authorization_endpoint=="string"&&typeof p.token_endpoint=="string")return!0}catch{continue}finally{clearTimeout(a)}}return!1}var bn=class{constructor(t,n,r,o,s){this.serverUrl=t;this.settings=n;this.redirectUrlValue=r;this.handleRedirect=o;this.clientMetadata=Gp(r),s&&(this.credential=s,this.loaded=!0)}loaded=!1;credential={};verifier=null;clientMetadata;get redirectUrl(){return this.redirectUrlValue}async ensureLoaded(){if(this.loaded)return;let t=await xn(this.serverUrl,this.settings);this.credential=t.credential??{},this.loaded=!0}async persist(){await Qp(this.serverUrl,this.credential,this.settings)}async clientInformation(){return await this.ensureLoaded(),this.credential.clientInformation}async saveClientInformation(t){await this.ensureLoaded(),this.credential={...this.credential,clientInformation:t},await this.persist()}async tokens(){return await this.ensureLoaded(),this.credential.tokens}async saveTokens(t){await this.ensureLoaded(),this.credential={...this.credential,tokens:t},await this.persist()}async redirectToAuthorization(t){await this.handleRedirect(t)}saveCodeVerifier(t){this.verifier=t}codeVerifier(){if(!this.verifier)throw new Error("OAuth code verifier is missing.");return this.verifier}async invalidateCredentials(t){if(await this.ensureLoaded(),t==="all")this.credential={};else if(t==="client")this.credential={...this.credential,clientInformation:void 0};else if(t==="tokens")this.credential={...this.credential,tokens:void 0};else if(t==="verifier"){this.verifier=null;return}await this.persist()}};async function xn(e,t){let n=Nr(t?.storeMode);if(n==="file")return{backend:"file",credential:await kr(e,t)};let r=await Dr();if(n==="keyring"){if(!r)throw new Error('Keyring storage is not available. Set mcp_oauth_credentials_store_mode = "file".');return{backend:"keyring",credential:await Vs(r,e)}}if(!r)return{backend:"file",credential:await kr(e,t)};try{let o=await Vs(r,e);if(o)return{backend:"keyring",credential:o}}catch{}return{backend:"file",credential:await kr(e,t)}}async function Qp(e,t,n){let r=Nr(n?.storeMode);if(r==="file")return await Ar(e,t,n),{backend:"file"};let o=await Dr();if(r==="keyring"){if(!o)throw new Error('Keyring storage is not available. Set mcp_oauth_credentials_store_mode = "file".');return await Xs(o,e,t),{backend:"keyring"}}if(!o)return await Ar(e,t,n),{backend:"file"};try{return await Xs(o,e,t),await Rr(e,n).catch(()=>{}),{backend:"keyring"}}catch{return await Ar(e,t,n),{backend:"file"}}}async function Zp(e,t){let n=Nr(t?.storeMode);if(n==="file")return{backend:"file",removed:await Rr(e,t)};let r=await Dr();if(n==="keyring"){if(!r)throw new Error('Keyring storage is not available. Set mcp_oauth_credentials_store_mode = "file".');return{backend:"keyring",removed:await Js(r,e)}}let o=!1;if(r)try{o=await Js(r,e)}catch{o=!1}let s=await Rr(e,t);return{backend:o?"keyring":"file",removed:o||s}}async function Ur(e,t){return"url"in e?e.bearer_token_env_var?"bearer_token":(await xn(e.url,t)).credential?.tokens?.access_token?"oauth":await ni(e.url,Zs(e))?"not_logged_in":"unsupported":"unsupported"}async function ed(e,t){let n=ei(e),r="127.0.0.1",o=!1,s=null,i=null,c=null,a=new Promise((d,m)=>{i=d,c=m}),l=Lp((d,m)=>{let g=new URL(d.url??"/",`http://${r}`);if(g.pathname!=="/callback"){m.writeHead(404,{"Content-Type":"text/plain"}),m.end("Not found");return}let y=g.searchParams.get("code"),b=g.searchParams.get("error"),U=g.searchParams.get("error_description");if(y){m.writeHead(200,{"Content-Type":"text/html; charset=utf-8"}),m.end("<html><body><h1>Authentication complete.</h1><p>You can close this window.</p></body></html>"),i?.(y);return}let T=U??b??"OAuth callback missing code.";m.writeHead(400,{"Content-Type":"text/html; charset=utf-8"}),m.end(`<html><body><h1>Authentication failed.</h1><p>${T}</p></body></html>`),c?.(new Error(T))});await new Promise((d,m)=>{l.once("error",m),l.listen(n??0,r,()=>{l.off("error",m),d()})});let p=l.address();if(!p||typeof p=="string")throw await new Promise(d=>l.close(()=>d())),new Error("Failed to resolve OAuth callback listener address.");s=setTimeout(()=>{c?.(new Error("Timed out waiting for OAuth callback."))},t),s.unref?.();let u=async()=>{o||(o=!0,s&&(clearTimeout(s),s=null),await new Promise(d=>l.close(()=>d())))};return{redirectUrl:`http://${r}:${p.port}/callback`,waitForCode:async()=>{try{return await a}finally{await u()}},close:u}}async function Fr(e){if(e.config.bearer_token_env_var)throw new Error(`Server "${e.serverName}" is configured with bearer_token_env_var. Remove it to use OAuth login.`);let t=Zs(e.config);if(!await ni(e.config.url,t))throw new Error(`Server "${e.serverName}" does not advertise OAuth support. Configure --bearer-token-env-var instead.`);let r=await ed(e.settings?.callbackPort,e.timeoutMs??Bp),o=ti(e.config.url,t),s=new bn(e.config.url,e.settings,r.redirectUrl,async i=>{let c=i.toString();e.onAuthorizationUrl?.(c);try{await Yp(c)}catch(a){e.onBrowserOpenFailure?.(new Error(Gs(a)),i.toString())}});try{let i=Xp(e.scopes);if(await js(s,{serverUrl:e.config.url,scope:i,fetchFn:o})==="REDIRECT"){let l=await r.waitForCode();if(await js(s,{serverUrl:e.config.url,authorizationCode:l,scope:i,fetchFn:o})!=="AUTHORIZED")throw new Error("OAuth authorization did not complete.")}let a=await xn(e.config.url,e.settings);if(!a.credential?.tokens?.access_token)throw new Error("OAuth login completed but no access token was stored.");return{backend:a.backend}}catch(i){throw new Error(`OAuth login failed: ${Gs(i)}`)}finally{await r.close()}}async function Hr(e){return Zp(e.config.url,e.settings)}async function ri(e){if(e.config.bearer_token_env_var)return null;let t=await xn(e.config.url,e.settings);if(!t.credential?.tokens?.access_token)return null;let r=`http://127.0.0.1:${ei(e.settings?.callbackPort)??zp}/callback`;return new bn(e.config.url,e.settings,r,async()=>{throw new Error(`MCP server "${e.serverName}" requires OAuth login. Run: memo mcp login ${e.serverName}`)},t.credential)}import{access as QS,mkdir as ZS,readFile as ev,readdir as tv,rm as nv,writeFile as rv}from"fs/promises";import{homedir as sv}from"os";import{basename as av,dirname as cv,join as lv,resolve as uv}from"path";import{z as Be}from"zod";function D(e){let{inputSchema:t,execute:n,...r}=e,o=t.toJSONSchema?.(),{$schema:s,...i}=o??{};return{...r,source:"native",inputSchema:i,validateInput:c=>{let a=t.safeParse(c);if(!a.success){let l=a.error.issues[0]?.message??"invalid input";return{ok:!1,error:`${e.name} invalid input: ${l}`}}return{ok:!0,data:a.data}},execute:n}}function h(e,t=!1){return{content:[{type:"text",text:e}],isError:t}}import{spawn as bd}from"child_process";import{EventEmitter as xd}from"events";import{resolve as Md}from"path";import{posix as td}from"path";var oi=220,si=4096,nd=/^\/dev\/(?:sd[a-z]\d*|vd[a-z]\d*|xvd[a-z]\d*|hd[a-z]\d*|nvme\d+n\d+(?:p\d+)?|mmcblk\d+(?:p\d+)?|disk\d+|rdisk\d+)$/i,rd=/(?:^|[\s(])(?:\d?>>?|>>|>\||&>)\s*\/dev\/(?:sd[a-z]\d*|vd[a-z]\d*|xvd[a-z]\d*|hd[a-z]\d*|nvme\d+n\d+(?:p\d+)?|mmcblk\d+(?:p\d+)?|disk\d+|rdisk\d+)(?:\s|$)/i,od=new Set(["-u","--user","-g","--group","-h","--host","-p","--prompt","-C","-T","-r","--role","-t","--type","-D","--chdir"]),sd=new Set(["fdisk","sfdisk","cfdisk","parted","sgdisk","gdisk","wipefs","blkdiscard","shred"]);function Br(e){return e.replace(/&/g,"&").replace(/"/g,""").replace(/</g,"<").replace(/>/g,">")}function id(e){let t=e.replace(/\s+/g," ").trim();return t.length>oi?`${t.slice(0,oi)}\u2026`:t}function ii(e){let t=e.trim().replace(/^['"]|['"]$/g,"");return(t.split(/[\\/]/).at(-1)??t).toLowerCase()}function ad(e){let t=null,n=!1;for(let r=0;r<e.length;r+=1){let o=e[r];if(n){n=!1;continue}if(o==="\\"&&t!=="'"){n=!0;continue}if(t){o===t&&(t=null);continue}if(o==='"'||o==="'"){t=o;continue}if(o==="#")return e.slice(0,r)}return e}function cd(e){let t=[],n="",r=null,o=!1,s=()=>{let i=ad(n).trim();i&&t.push(i),n=""};for(let i=0;i<e.length;i+=1){let c=e[i];if(o){n+=c,o=!1;continue}if(c==="\\"&&r!=="'"){n+=c,o=!0;continue}if(r){n+=c,c===r&&(r=null);continue}if(c==='"'||c==="'"){r=c,n+=c;continue}if(c===";"||c===`
|
|
46
|
-
`){s();continue}if(c==="&"){e[i+1]==="&"&&(i+=1),s();continue}if(c==="|"){e[i+1]==="|"&&(i+=1),s();continue}n+=c}return s(),t}function
|
|
54
|
+
`)}async function we(e,t){await Cp(kp(e),{recursive:!0}),await bp(e,$p(t),"utf-8")}async function te(){let e=process.env.MEMO_HOME?js(process.env.MEMO_HOME):Rp,t=lt(e,"config.toml");try{await Mp(t);let n=await xp(t,"utf-8"),r=Ep(n),o=Op(r.providers),s=Array.isArray(r.active_mcp_servers)?r.active_mcp_servers.filter(m=>typeof m=="string"&&m.trim().length>0):void 0,i=Lp(r.active_skills),c=r.mcp_oauth_credentials_store_mode==="auto"||r.mcp_oauth_credentials_store_mode==="keyring"||r.mcp_oauth_credentials_store_mode==="file"?r.mcp_oauth_credentials_store_mode:Gt.mcp_oauth_credentials_store_mode,a=typeof r.mcp_oauth_callback_port=="number"&&Number.isInteger(r.mcp_oauth_callback_port)&&r.mcp_oauth_callback_port>0&&r.mcp_oauth_callback_port<=65535?r.mcp_oauth_callback_port:void 0,l=Bs(r.auto_compact_threshold_percent),p=Np(r.model_profiles),u={current_provider:r.current_provider??Gt.current_provider,mcp_oauth_credentials_store_mode:c,mcp_oauth_callback_port:a,auto_compact_threshold_percent:l,providers:o,model_profiles:p,mcp_servers:r.mcp_servers??{},active_mcp_servers:s,active_skills:i},d=!u.providers.length;return{config:d?Gt:u,home:e,configPath:t,needsSetup:d}}catch{return{config:Gt,home:e,configPath:t,needsSetup:!0}}}function ut(e,t){let n=t||e.current_provider,r=e.providers.find(o=>o.name===n);return r||(e.providers?.[0]??Gt.providers[0])}function Kt(e,t){let n=t.historyDir??lt(e.home,Ip),r=js(n),o=Pp(t.cwd??process.cwd()),s=Ap(o).root,c=o.slice(s.length).split(/[\\/]+/).filter(Boolean);if(process.platform==="win32"){let l=/^([A-Za-z]):/.exec(s)?.[1];l&&c.unshift(l.toUpperCase())}if(c.length===0)return lt(r,"-root");let a=`-${c.map(l=>l.replace(/[^A-Za-z0-9._-]/g,"_")).join("-")}`;return lt(r,a)}function Ws(e,t){let n=new Date,r=String(n.getFullYear()),o=String(n.getMonth()+1).padStart(2,"0"),s=String(n.getDate()).padStart(2,"0"),i=String(n.getHours()).padStart(2,"0"),c=String(n.getMinutes()).padStart(2,"0"),a=String(n.getSeconds()).padStart(2,"0"),l=`${r}-${o}-${s}T${i}-${c}-${a}`,p=t.replace(/[^A-Za-z0-9._-]/g,"_"),u=`${l}-${p}.jsonl`;return lt(e,u)}import{createHash as Dp}from"crypto";import{spawn as Up}from"child_process";import{createServer as Hp}from"http";import{mkdir as Fp,readFile as Bp,rename as jp,writeFile as Wp}from"fs/promises";import{homedir as Rr}from"os";import{dirname as zp,join as Lr}from"path";import{auth as zs}from"@modelcontextprotocol/sdk/client/auth.js";var Ir=1,Gp="mcp-oauth.json",Kp=3e5,qp=5e3,Gs="MCP-Protocol-Version",Vp="2024-11-05",Nr="memo-code.mcp.oauth",Xp=33333;function Jp(e){return{client_name:"Memo Code CLI",redirect_uris:[e],grant_types:["authorization_code","refresh_token"],response_types:["code"],token_endpoint_auth_method:"none"}}function Ks(e){return e instanceof Error?e.message:String(e)}function $r(e){return new URL(e).toString()}function Dr(e){return e==="file"||e==="keyring"||e==="auto"?e:"auto"}function qs(e){return e==="~"?Rr():e.startsWith("~/")?Lr(Rr(),e.slice(2)):e}function Yp(e){return e?.memoHome?.trim()?qs(e.memoHome.trim()):process.env.MEMO_HOME?.trim()?qs(process.env.MEMO_HOME.trim()):Lr(Rr(),".memo")}function Qs(e){return Lr(Yp(e),"auth",Gp)}function wt(e){return Dp("sha256").update($r(e)).digest("hex")}function Vs(){return{version:Ir,credentials:{}}}async function Ur(e){let t=Qs(e);try{let n=await Bp(t,"utf8"),r=JSON.parse(n);return r.version!==Ir||!r.credentials?Vs():{version:Ir,credentials:r.credentials}}catch{return Vs()}}async function Zs(e,t){let n=Qs(e),r=`${n}.tmp`;await Fp(zp(n),{recursive:!0}),await Wp(r,JSON.stringify(t,null,2),{encoding:"utf8",mode:384}),await jp(r,n)}var bn=null;async function Hr(){return bn||(bn=(async()=>{try{let t=await new Function("specifier","return import(specifier)")("keytar"),n=t.default??t;return n&&typeof n.getPassword=="function"&&typeof n.setPassword=="function"&&typeof n.deletePassword=="function"?n:null}catch{return null}})(),bn)}async function Pr(e,t){let n=wt(e);return(await Ur(t)).credentials[n]?.credential}async function Er(e,t,n){let r=wt(e),o=await Ur(n);o.credentials[r]={url:$r(e),updatedAt:Date.now(),credential:t},await Zs(n,o)}async function Or(e,t){let n=wt(e),r=await Ur(t);return r.credentials[n]?(delete r.credentials[n],await Zs(t,r),!0):!1}async function Xs(e,t){let n=wt(t),r=await e.getPassword(Nr,n);return r?JSON.parse(r).credential:void 0}async function Js(e,t,n){let r=wt(t),o={url:$r(t),updatedAt:Date.now(),credential:n};await e.setPassword(Nr,r,JSON.stringify(o))}async function Ys(e,t){let n=wt(t);return e.deletePassword(Nr,n)}function Qp(e,t){return new URL(e).origin===t.origin}function ei(e){return{...e.http_headers??e.headers??{}}}function Zp(e){let t=e.replace(/^\/+|\/+$/g,"");if(!t)return["/.well-known/oauth-authorization-server"];let n=[],r=o=>{n.includes(o)||n.push(o)};return r(`/.well-known/oauth-authorization-server/${t}`),r(`/${t}/.well-known/oauth-authorization-server`),r("/.well-known/oauth-authorization-server"),n}function ed(e){if(!e||e.length===0)return;let t=e.flatMap(n=>n.split(/[,\s]+/g)).map(n=>n.trim()).filter(Boolean);if(t.length!==0)return t.join(" ")}function ti(e){if(e!==void 0){if(!Number.isInteger(e)||e<=0||e>65535)throw new Error(`Invalid MCP OAuth callback port "${e}". Use an integer between 1 and 65535.`);return e}}function td(e){return process.platform==="darwin"?{command:"open",args:[e]}:process.platform==="win32"?{command:"cmd",args:["/c","start","",e]}:{command:"xdg-open",args:[e]}}async function nd(e){let{command:t,args:n}=td(e);await new Promise((r,o)=>{let s=Up(t,n,{detached:!0,stdio:"ignore"}),i=!1,c=p=>{if(!i){if(i=!0,s.off("error",a),s.off("spawn",l),p){o(p);return}r()}},a=p=>{c(p)},l=()=>{c()};s.on("error",a),s.on("spawn",l),s.unref()})}function ni(e,t){return async(n,r)=>{let o=typeof n=="string"||n instanceof URL?new URL(String(n),e):new URL(n.url),s=new Headers(r?.headers??{});if(Qp(e,o)){for(let[i,c]of Object.entries(t))s.has(i)||s.set(i,c);s.has(Gs)||s.set(Gs,Vp)}return fetch(o,{...r,headers:s})}}async function ri(e,t){let n=new URL(e),r=ni(e,t),o=Zp(n.pathname);for(let s of o){let i=new URL(n.toString());i.pathname=s,i.search="",i.hash="";let c=new AbortController,a=setTimeout(()=>c.abort(),qp);try{let l=await r(i,{method:"GET",signal:c.signal});if(!l.ok)continue;let p=await l.json();if(typeof p.authorization_endpoint=="string"&&typeof p.token_endpoint=="string")return!0}catch{continue}finally{clearTimeout(a)}}return!1}var xn=class{constructor(t,n,r,o,s){this.serverUrl=t;this.settings=n;this.redirectUrlValue=r;this.handleRedirect=o;this.clientMetadata=Jp(r),s&&(this.credential=s,this.loaded=!0)}loaded=!1;credential={};verifier=null;clientMetadata;get redirectUrl(){return this.redirectUrlValue}async ensureLoaded(){if(this.loaded)return;let t=await Mn(this.serverUrl,this.settings);this.credential=t.credential??{},this.loaded=!0}async persist(){await rd(this.serverUrl,this.credential,this.settings)}async clientInformation(){return await this.ensureLoaded(),this.credential.clientInformation}async saveClientInformation(t){await this.ensureLoaded(),this.credential={...this.credential,clientInformation:t},await this.persist()}async tokens(){return await this.ensureLoaded(),this.credential.tokens}async saveTokens(t){await this.ensureLoaded(),this.credential={...this.credential,tokens:t},await this.persist()}async redirectToAuthorization(t){await this.handleRedirect(t)}saveCodeVerifier(t){this.verifier=t}codeVerifier(){if(!this.verifier)throw new Error("OAuth code verifier is missing.");return this.verifier}async invalidateCredentials(t){if(await this.ensureLoaded(),t==="all")this.credential={};else if(t==="client")this.credential={...this.credential,clientInformation:void 0};else if(t==="tokens")this.credential={...this.credential,tokens:void 0};else if(t==="verifier"){this.verifier=null;return}await this.persist()}};async function Mn(e,t){let n=Dr(t?.storeMode);if(n==="file")return{backend:"file",credential:await Pr(e,t)};let r=await Hr();if(n==="keyring"){if(!r)throw new Error('Keyring storage is not available. Set mcp_oauth_credentials_store_mode = "file".');return{backend:"keyring",credential:await Xs(r,e)}}if(!r)return{backend:"file",credential:await Pr(e,t)};try{let o=await Xs(r,e);if(o)return{backend:"keyring",credential:o}}catch{}return{backend:"file",credential:await Pr(e,t)}}async function rd(e,t,n){let r=Dr(n?.storeMode);if(r==="file")return await Er(e,t,n),{backend:"file"};let o=await Hr();if(r==="keyring"){if(!o)throw new Error('Keyring storage is not available. Set mcp_oauth_credentials_store_mode = "file".');return await Js(o,e,t),{backend:"keyring"}}if(!o)return await Er(e,t,n),{backend:"file"};try{return await Js(o,e,t),await Or(e,n).catch(()=>{}),{backend:"keyring"}}catch{return await Er(e,t,n),{backend:"file"}}}async function od(e,t){let n=Dr(t?.storeMode);if(n==="file")return{backend:"file",removed:await Or(e,t)};let r=await Hr();if(n==="keyring"){if(!r)throw new Error('Keyring storage is not available. Set mcp_oauth_credentials_store_mode = "file".');return{backend:"keyring",removed:await Ys(r,e)}}let o=!1;if(r)try{o=await Ys(r,e)}catch{o=!1}let s=await Or(e,t);return{backend:o?"keyring":"file",removed:o||s}}async function Fr(e,t){return"url"in e?e.bearer_token_env_var?"bearer_token":(await Mn(e.url,t)).credential?.tokens?.access_token?"oauth":await ri(e.url,ei(e))?"not_logged_in":"unsupported":"unsupported"}async function sd(e,t){let n=ti(e),r="127.0.0.1",o=!1,s=null,i=null,c=null,a=new Promise((d,m)=>{i=d,c=m}),l=Hp((d,m)=>{let g=new URL(d.url??"/",`http://${r}`);if(g.pathname!=="/callback"){m.writeHead(404,{"Content-Type":"text/plain"}),m.end("Not found");return}let y=g.searchParams.get("code"),b=g.searchParams.get("error"),U=g.searchParams.get("error_description");if(y){m.writeHead(200,{"Content-Type":"text/html; charset=utf-8"}),m.end("<html><body><h1>Authentication complete.</h1><p>You can close this window.</p></body></html>"),i?.(y);return}let T=U??b??"OAuth callback missing code.";m.writeHead(400,{"Content-Type":"text/html; charset=utf-8"}),m.end(`<html><body><h1>Authentication failed.</h1><p>${T}</p></body></html>`),c?.(new Error(T))});await new Promise((d,m)=>{l.once("error",m),l.listen(n??0,r,()=>{l.off("error",m),d()})});let p=l.address();if(!p||typeof p=="string")throw await new Promise(d=>l.close(()=>d())),new Error("Failed to resolve OAuth callback listener address.");s=setTimeout(()=>{c?.(new Error("Timed out waiting for OAuth callback."))},t),s.unref?.();let u=async()=>{o||(o=!0,s&&(clearTimeout(s),s=null),await new Promise(d=>l.close(()=>d())))};return{redirectUrl:`http://${r}:${p.port}/callback`,waitForCode:async()=>{try{return await a}finally{await u()}},close:u}}async function Br(e){if(e.config.bearer_token_env_var)throw new Error(`Server "${e.serverName}" is configured with bearer_token_env_var. Remove it to use OAuth login.`);let t=ei(e.config);if(!await ri(e.config.url,t))throw new Error(`Server "${e.serverName}" does not advertise OAuth support. Configure --bearer-token-env-var instead.`);let r=await sd(e.settings?.callbackPort,e.timeoutMs??Kp),o=ni(e.config.url,t),s=new xn(e.config.url,e.settings,r.redirectUrl,async i=>{let c=i.toString();e.onAuthorizationUrl?.(c);try{await nd(c)}catch(a){e.onBrowserOpenFailure?.(new Error(Ks(a)),i.toString())}});try{let i=ed(e.scopes);if(await zs(s,{serverUrl:e.config.url,scope:i,fetchFn:o})==="REDIRECT"){let l=await r.waitForCode();if(await zs(s,{serverUrl:e.config.url,authorizationCode:l,scope:i,fetchFn:o})!=="AUTHORIZED")throw new Error("OAuth authorization did not complete.")}let a=await Mn(e.config.url,e.settings);if(!a.credential?.tokens?.access_token)throw new Error("OAuth login completed but no access token was stored.");return{backend:a.backend}}catch(i){throw new Error(`OAuth login failed: ${Ks(i)}`)}finally{await r.close()}}async function jr(e){return od(e.config.url,e.settings)}async function oi(e){if(e.config.bearer_token_env_var)return null;let t=await Mn(e.config.url,e.settings);if(!t.credential?.tokens?.access_token)return null;let r=`http://127.0.0.1:${ti(e.settings?.callbackPort)??Xp}/callback`;return new xn(e.config.url,e.settings,r,async()=>{throw new Error(`MCP server "${e.serverName}" requires OAuth login. Run: memo mcp login ${e.serverName}`)},t.credential)}import{access as rv,mkdir as ov,readFile as sv,readdir as iv,rm as av,writeFile as cv}from"fs/promises";import{homedir as uv}from"os";import{basename as dv,dirname as mv,join as fv,resolve as gv}from"path";import{z as Be}from"zod";function D(e){let{inputSchema:t,execute:n,...r}=e,o=t.toJSONSchema?.(),{$schema:s,...i}=o??{};return{...r,source:"native",inputSchema:i,validateInput:c=>{let a=t.safeParse(c);if(!a.success){let l=a.error.issues[0]?.message??"invalid input";return{ok:!1,error:`${e.name} invalid input: ${l}`}}return{ok:!0,data:a.data}},execute:n}}function h(e,t=!1){return{content:[{type:"text",text:e}],isError:t}}import{spawn as Pd}from"child_process";import{EventEmitter as Ed}from"events";import{resolve as Rd}from"path";import{posix as id}from"path";var si=220,ii=4096,ad=/^\/dev\/(?:sd[a-z]\d*|vd[a-z]\d*|xvd[a-z]\d*|hd[a-z]\d*|nvme\d+n\d+(?:p\d+)?|mmcblk\d+(?:p\d+)?|disk\d+|rdisk\d+)$/i,cd=/(?:^|[\s(])(?:\d?>>?|>>|>\||&>)\s*\/dev\/(?:sd[a-z]\d*|vd[a-z]\d*|xvd[a-z]\d*|hd[a-z]\d*|nvme\d+n\d+(?:p\d+)?|mmcblk\d+(?:p\d+)?|disk\d+|rdisk\d+)(?:\s|$)/i,ld=new Set(["-u","--user","-g","--group","-h","--host","-p","--prompt","-C","-T","-r","--role","-t","--type","-D","--chdir"]),ud=new Set(["fdisk","sfdisk","cfdisk","parted","sgdisk","gdisk","wipefs","blkdiscard","shred"]);function Wr(e){return e.replace(/&/g,"&").replace(/"/g,""").replace(/</g,"<").replace(/>/g,">")}function pd(e){let t=e.replace(/\s+/g," ").trim();return t.length>si?`${t.slice(0,si)}\u2026`:t}function ai(e){let t=e.trim().replace(/^['"]|['"]$/g,"");return(t.split(/[\\/]/).at(-1)??t).toLowerCase()}function dd(e){let t=null,n=!1;for(let r=0;r<e.length;r+=1){let o=e[r];if(n){n=!1;continue}if(o==="\\"&&t!=="'"){n=!0;continue}if(t){o===t&&(t=null);continue}if(o==='"'||o==="'"){t=o;continue}if(o==="#")return e.slice(0,r)}return e}function md(e){let t=[],n="",r=null,o=!1,s=()=>{let i=dd(n).trim();i&&t.push(i),n=""};for(let i=0;i<e.length;i+=1){let c=e[i];if(o){n+=c,o=!1;continue}if(c==="\\"&&r!=="'"){n+=c,o=!0;continue}if(r){n+=c,c===r&&(r=null);continue}if(c==='"'||c==="'"){r=c,n+=c;continue}if(c===";"||c===`
|
|
55
|
+
`){s();continue}if(c==="&"){e[i+1]==="&"&&(i+=1),s();continue}if(c==="|"){e[i+1]==="|"&&(i+=1),s();continue}n+=c}return s(),t}function fd(e){let t=[],n="",r=null,o=!1,s=()=>{n&&t.push(n),n=""};for(let i=0;i<e.length;i+=1){let c=e[i];if(o){n+=c,o=!1;continue}if(c==="\\"&&r!=="'"){o=!0;continue}if(r){c===r?r=null:n+=c;continue}if(c==='"'||c==="'"){r=c;continue}if(/\s/.test(c)){s();continue}n+=c}return s(),t}function ci(e){return/^[A-Za-z_][A-Za-z0-9_]*=.*/.test(e)}function gd(e,t){let n=t;for(;n<e.length;){let r=ai(e[n]??"");if(r==="sudo"){for(n+=1;n<e.length;){let o=e[n]??"";if(!o.startsWith("-"))break;n+=1,ld.has(o)&&n<e.length&&(n+=1)}continue}if(r==="env"){for(n+=1;n<e.length;){let o=e[n]??"";if(o.startsWith("-")||ci(o)){n+=1;continue}break}continue}if(r==="command"||r==="nohup"||r==="time"){n+=1;continue}break}return n}function hd(e){let t=fd(e);if(t.length===0)return null;let n=0;for(;n<t.length&&ci(t[n]??"");)n+=1;if(n=gd(t,n),n>=t.length)return null;let r=ai(t[n]??"");return r?{raw:e,commandName:r,args:t.slice(n+1)}:null}function zr(e){let t=e.trim().replace(/^['"]|['"]$/g,"");return ad.test(t)}function yd(e){let t=e.trim().replace(/^['"]|['"]$/g,""),n=t.toLowerCase();return n==="/"||n==="/*"||n==="/.*"||n==="~"||n==="~/"||n==="~/*"||n==="$home"||n==="$home/"||n==="$home/*"||n==="${home}"||n==="${home}/"||n==="${home}/*"?!0:t.startsWith("/")&&!/[*?[\]{}$]/.test(t)?id.normalize(t)==="/":!1}function Sd(e){return e.startsWith("-")&&e!=="-"}function _d(e){if(e.commandName!=="rm")return null;let t=!1,n=0;for(;n<e.args.length;){let o=e.args[n]??"";if(o==="--"){n+=1;break}if(!Sd(o))break;if(o==="--recursive"){t=!0,n+=1;continue}if(o.startsWith("--")){n+=1;continue}let s=o.slice(1);(s.includes("r")||s.includes("R"))&&(t=!0),n+=1}if(!t)return null;let r=e.args.slice(n);for(let o of r)if(yd(o))return{ruleId:"rm_recursive_critical_target",matchedSegment:e.raw};return null}function vd(e){return e.commandName==="mkfs"||e.commandName.startsWith("mkfs.")?{ruleId:"mkfs_filesystem_create",matchedSegment:e.raw}:null}function Td(e){if(e.commandName!=="dd")return null;for(let t=0;t<e.args.length;t+=1){let n=e.args[t]??"",r=n.indexOf("=");if(r<=0)continue;let o=n.slice(0,r).toLowerCase(),s=n.slice(r+1);if(o==="of"&&zr(s))return{ruleId:"dd_write_block_device",matchedSegment:e.raw}}for(let t=0;t<e.args.length-1;t+=1){let n=(e.args[t]??"").toLowerCase(),r=e.args[t+1]??"";if(n==="of"&&zr(r))return{ruleId:"dd_write_block_device",matchedSegment:e.raw}}return null}function wd(e){return!ud.has(e.commandName)||!e.args.some(t=>zr(t))?null:{ruleId:"disk_mutation_block_device",matchedSegment:e.raw}}function Cd(e){return cd.test(e)?{ruleId:"redirect_block_device",matchedSegment:e}:null}function bd(e){for(let t of md(e)){let n=hd(t);if(n){let o=_d(n);if(o)return o;let s=vd(n);if(s)return s;let i=Td(n);if(i)return i;let c=wd(n);if(c)return c}let r=Cd(t);if(r)return r}return null}function xd(e,t){let n=pd(e.command),r=typeof e.sessionId=="number"?` session_id="${e.sessionId}"`:"";return`<system_hint type="tool_call_denied" tool="${Wr(e.toolName)}" reason="dangerous_command" policy="blacklist" rule="${Wr(t.ruleId)}"${r} command="${Wr(n)}">Blocked a high-risk shell command to prevent irreversible data loss. Use a safer and scoped alternative.</system_hint>`}function Gr(e){let t=bd(e.command);return t?{blocked:!0,xml:xd(e,t),match:t}:{blocked:!1}}function li(e){let n=e.replace(/\r\n/g,`
|
|
47
56
|
`).replace(/\r/g,`
|
|
48
57
|
`).split(`
|
|
49
|
-
`),r=n.pop()??"";return{completedLines:n,remainder:r}}function
|
|
50
|
-
`)}function
|
|
51
|
-
[exec error] ${y.message}`,p.eventBus.emit("output")}),l.on("close",y=>{p.exited=!0,p.exitCode=typeof y=="number"?y:-1,p.eventBus.emit("exit")}),this.sessions.set(o,p),this.cleanupSessions();let d=typeof t.execution_timeout_ms=="number"&&t.execution_timeout_ms>0?Math.floor(t.execution_timeout_ms):null,m=
|
|
58
|
+
`),r=n.pop()??"";return{completedLines:n,remainder:r}}function Kr(e){return e.length<=ii?e:e.slice(-ii)}import{AsyncLocalStorage as Md}from"async_hooks";import{resolve as kd}from"path";var ui=new Md;function pi(e){if(!(!e||!e.trim()))return kd(e)}function Ad(){return ui.getStore()??{}}function Fe(e=process.cwd()){return pi(Ad().cwd)??e}async function di(e,t){let n={cwd:pi(e.cwd)};return new Promise((r,o)=>{ui.run(n,()=>{Promise.resolve(t()).then(r,o)})})}var Id=1e4,Od=250,Ld=2e3,kn=64;function Nd(e){return Math.ceil(e.length/4)}function $d(){return Math.random().toString(16).slice(2)||String(Date.now())}function Dd(e){let t=e.login,n=e.shell?.trim();if(process.platform==="win32"){let o=n||"powershell.exe";return o.toLowerCase().includes("powershell")?{file:o,args:["-NoProfile","-Command",e.cmd]}:{file:o,args:[t?"-lc":"-c",e.cmd]}}return{file:n||process.env.SHELL||"/bin/bash",args:[t?"-lc":"-c",e.cmd]}}function Ud(e,t){let r=(typeof t=="number"&&t>0?Math.floor(t):Ld)*4,o=Nd(e);return e.length<=r?{output:e,originalTokenCount:o,deliveredChars:e.length}:{output:e.slice(0,r),originalTokenCount:o,deliveredChars:r}}function Hd(e){let t=[];return t.push(`Chunk ID: ${e.chunkId}`),t.push(`Wall time: ${e.wallTimeSeconds.toFixed(4)} seconds`),e.exitCode!==null?t.push(`Process exited with code ${e.exitCode}`):t.push(`Process running with session ID ${e.sessionId}`),t.push(`Original token count: ${e.originalTokenCount}`),t.push("Output:"),t.push(e.output),t.join(`
|
|
59
|
+
`)}function mi(e,t){return typeof e!="number"||Number.isNaN(e)?t:e<0?0:Math.floor(e)}async function fi(e,t){t<=0||e.exited||await new Promise(n=>{let r=!1,o=()=>{r||(r=!0,clearTimeout(c),i(),n())},s=()=>{o()},i=()=>{e.eventBus.off("exit",s)},c=setTimeout(()=>{o()},t);c.unref?.(),e.eventBus.on("exit",s)})}async function gi(e,t){e.exited||t<=0||await new Promise(n=>{let r=!1,o=()=>{r||(r=!0,clearTimeout(c),i(),n())},s=()=>{o()},i=()=>{e.eventBus.off("exit",s)},c=setTimeout(()=>{o()},t);c.unref?.(),e.eventBus.on("exit",s)})}var qr=class{sessions=new Map;nextId=1;cleanupSessions(){if(this.sessions.size<=kn)return;let t=Array.from(this.sessions.values()).filter(n=>n.exited).sort((n,r)=>n.startedAtMs-r.startedAtMs);for(let n of t){if(this.sessions.size<=kn)break;this.sessions.delete(n.id)}}activeSessionCount(){let t=0;for(let n of this.sessions.values())n.exited||(t+=1);return t}async terminateForTimeout(t){if(!t.exited){try{t.proc.kill("SIGTERM")}catch{}if(await gi(t,200),!t.exited){try{t.proc.kill("SIGKILL")}catch{}await gi(t,200)}}}async start(t){let n=t.cmd.trim();if(!n)throw new Error("cmd must not be empty");if(this.cleanupSessions(),this.activeSessionCount()>=kn)throw new Error(`too many active sessions (max ${kn})`);let r=Gr({toolName:t.source_tool??"exec_command",command:n});if(r.blocked)return r.xml;let o=this.nextId++,s=Date.now(),i=Dd({cmd:n,shell:t.shell,login:t.login!==!1}),c=Fe(),a=t.workdir?.trim()?Rd(c,t.workdir.trim()):c,l=Pd(i.file,i.args,{cwd:a,env:process.env,stdio:["pipe","pipe","pipe"],shell:!1}),p={id:o,output:"",readOffset:0,pendingStdinInput:"",startedAtMs:s,exited:!1,exitCode:null,eventBus:new Ed,proc:l},u=(y,b)=>{let U=typeof b=="string"?b:b.toString("utf8");p.output+=y?`${y}${U}`:U,p.eventBus.emit("output")};l.stdout?.on("data",y=>u("",y)),l.stderr?.on("data",y=>u("",y)),l.on("error",y=>{p.output+=`
|
|
60
|
+
[exec error] ${y.message}`,p.eventBus.emit("output")}),l.on("close",y=>{p.exited=!0,p.exitCode=typeof y=="number"?y:-1,p.eventBus.emit("exit")}),this.sessions.set(o,p),this.cleanupSessions();let d=typeof t.execution_timeout_ms=="number"&&t.execution_timeout_ms>0?Math.floor(t.execution_timeout_ms):null,m=mi(t.yield_time_ms,Id),g=d!==null?Math.min(m,Math.max(0,d)):m;if(await fi(p,g),d!==null&&!p.exited&&Date.now()-s>=d)throw await this.terminateForTimeout(p),this.cleanupSessions(),new Error(`command timed out after ${d}ms`);return this.buildResponseText(p,t.max_output_tokens)}async write(t){let n=this.sessions.get(t.session_id);if(!n)throw new Error(`session_id ${t.session_id} not found`);if(!n.exited&&t.chars&&t.chars.length>0){let o=Kr(`${n.pendingStdinInput}${t.chars}`),{completedLines:s,remainder:i}=li(o);for(let c of s){if(!c.trim())continue;let a=Gr({toolName:t.source_tool??"write_stdin",command:c,sessionId:n.id});if(a.blocked)return n.pendingStdinInput="",a.xml}n.pendingStdinInput=Kr(i);try{let c=n.proc.stdin;c&&!c.destroyed&&!c.writableEnded&&c.write(t.chars)}catch{}}let r=mi(t.yield_time_ms,Od);return await fi(n,r),this.buildResponseText(n,t.max_output_tokens)}buildResponseText(t,n){let r=t.output.slice(t.readOffset),o=Ud(r,n);t.readOffset+=o.deliveredChars;let s={sessionId:t.id,chunkId:$d(),wallTimeSeconds:(Date.now()-t.startedAtMs)/1e3,exitCode:t.exited?t.exitCode:null,output:o.output,originalTokenCount:o.originalTokenCount};return Hd(s)}},hi=new qr;async function Ct(e){return hi.start(e)}async function yi(e){return hi.write(e)}var Fd=Be.object({command:Be.array(Be.string().min(1)).min(1,"command cannot be empty"),workdir:Be.string().optional(),timeout_ms:Be.number().int().positive().optional(),sandbox_permissions:Be.enum(["use_default","require_escalated"]).optional(),justification:Be.string().optional(),prefix_rule:Be.array(Be.string().min(1)).optional()}).strict(),Bd=/^[A-Za-z0-9_./:@%+-]+$/;function jd(e){return e.length===0?"''":Bd.test(e)?e:`'${e.replace(/'/g,`'"'"'`)}'`}function Wd(e){return e.map(t=>jd(t)).join(" ")}var Si=D({name:"shell",description:"Runs a shell command (argv form) and returns output.",inputSchema:Fd,supportsParallelToolCalls:!0,isMutating:!0,execute:async({command:e,workdir:t,timeout_ms:n})=>{try{let r=await Ct({cmd:Wd(e),workdir:t,login:!1,yield_time_ms:n,execution_timeout_ms:n,source_tool:"shell"});return h(r)}catch(r){return h(`shell failed: ${r.message}`,!0)}}});import{z as je}from"zod";var zd=je.object({command:je.string().min(1,"command cannot be empty"),workdir:je.string().optional(),login:je.boolean().optional(),timeout_ms:je.number().int().positive().optional(),sandbox_permissions:je.enum(["use_default","require_escalated"]).optional(),justification:je.string().optional(),prefix_rule:je.array(je.string().min(1)).optional()}).strict(),_i=D({name:"shell_command",description:"Runs a shell command and returns its output. Always set workdir when possible.",inputSchema:zd,supportsParallelToolCalls:!0,isMutating:!0,execute:async({command:e,workdir:t,login:n,timeout_ms:r})=>{try{let o=await Ct({cmd:e,workdir:t,login:n,yield_time_ms:r,execution_timeout_ms:r,source_tool:"shell_command"});return h(o)}catch(o){return h(`shell_command failed: ${o.message}`,!0)}}});import{z as Me}from"zod";var Gd=Me.object({cmd:Me.string().min(1,"cmd cannot be empty"),workdir:Me.string().optional(),shell:Me.string().optional(),login:Me.boolean().optional(),tty:Me.boolean().optional(),yield_time_ms:Me.number().int().nonnegative().optional(),max_output_tokens:Me.number().int().positive().optional(),sandbox_permissions:Me.enum(["use_default","require_escalated"]).optional(),justification:Me.string().optional(),prefix_rule:Me.array(Me.string().min(1)).optional()}).strict(),Vr=D({name:"exec_command",description:"Runs a command in a PTY-like managed session, returning output or a session ID for ongoing interaction.",inputSchema:Gd,supportsParallelToolCalls:!0,isMutating:!0,execute:async e=>{try{let t=await Ct({...e,source_tool:"exec_command"});return h(t)}catch(t){return h(`exec_command failed: ${t.message}`,!0)}}});import{z as qt}from"zod";var Kd=qt.object({session_id:qt.number().int().positive(),chars:qt.string().optional(),yield_time_ms:qt.number().int().nonnegative().optional(),max_output_tokens:qt.number().int().positive().optional()}).strict(),Xr=D({name:"write_stdin",description:"Writes characters to an existing unified exec session and returns recent output.",inputSchema:Kd,supportsParallelToolCalls:!1,isMutating:!0,execute:async e=>{try{let t=await yi({...e,source_tool:"write_stdin"});return h(t)}catch(t){return h(`write_stdin failed: ${t.message}`,!0)}}});import{access as wm,readFile as Cm,realpath as bm,writeFile as xm}from"fs/promises";import{z as he}from"zod";var Sm=ts(Ai(),1);import{normalize as pm,resolve as dm,dirname as nT,join as mm,relative as fm,isAbsolute as gm}from"path";import{homedir as hm}from"os";import{existsSync as sT,statSync as iT,realpathSync as ym}from"fs";import{readFile as cT}from"fs/promises";function um(e){let t=e?.trim();if(!t)return null;let n=Number(t);return!Number.isFinite(n)||n<=0?null:Math.floor(n)}function Pn(){return um(process.env.MEMO_TOOL_RESULT_MAX_CHARS)??2e4}function Pi(){let e=Pn(),t=Math.max(16,Math.floor(e*.25)),n=Math.max(32,e-t),r=Math.max(32,Math.floor(e*.6));return Math.min(12e3,r,n)}function ze(e){return pm(dm(e))}function En(e){let t=ze(e);try{return ze(ym(t))}catch{return t}}function _m(e,t){let n=fm(t,e);return n===""||!n.startsWith("..")&&!gm(n)}function vm(){let e=process.env.MEMO_SANDBOX_WRITABLE_ROOTS?.trim();return e?e.split(",").map(t=>t.trim()).filter(Boolean).map(t=>En(t)):[]}function Ei(){let e=new Set;e.add(En(Fe()));let t=process.env.MEMO_HOME?.trim()||mm(hm(),".memo");e.add(En(t));for(let n of vm())e.add(n);return Array.from(e)}function Tm(e){let t=En(e);return Ei().some(r=>_m(t,r))}function Ri(e){if(Tm(e))return null;let t=Ei();return`sandbox write denied: ${e} is not within allowed directories (${t.join(", ")})`}var Mm=he.object({old_string:he.string().min(1,"old_string cannot be empty"),new_string:he.string(),replace_all:he.boolean().optional()}).strict(),Ii=he.object({file_path:he.string().min(1),old_string:he.string().optional(),new_string:he.string().optional(),replace_all:he.boolean().optional(),edits:he.array(Mm).min(1).optional()}).superRefine((e,t)=>{let n=!!(e.edits&&e.edits.length>0),r=e.old_string!==void 0||e.new_string!==void 0||e.replace_all!==void 0;if(n&&r){t.addIssue({code:he.ZodIssueCode.custom,message:"Use either edits or old_string/new_string fields, not both."});return}if(!n){if(typeof e.old_string!="string"||typeof e.new_string!="string"){t.addIssue({code:he.ZodIssueCode.custom,message:"Provide old_string/new_string, or use edits."});return}e.old_string.trim()||t.addIssue({code:he.ZodIssueCode.custom,message:"old_string cannot be empty",path:["old_string"]});return}for(let o=0;o<(e.edits?.length??0);o+=1){let s=e.edits?.[o];s&&(s.old_string.trim()||t.addIssue({code:he.ZodIssueCode.custom,message:"old_string cannot be empty",path:["edits",o,"old_string"]}))}}).strict();function km(e){let t=Ri(e);if(t)throw new Error(t)}function Am(e){return e.edits&&e.edits.length>0?e.edits:[{old_string:e.old_string??"",new_string:e.new_string??"",replace_all:e.replace_all}]}var Oi=D({name:"apply_patch",description:"Edit a local file by direct string replacement. Supports single replacement fields or batch edits.",inputSchema:Ii,supportsParallelToolCalls:!1,isMutating:!0,execute:async e=>{let t=Ii.safeParse(e);if(!t.success){let o=t.error.issues[0]?.message??"invalid input";return h(`apply_patch invalid input: ${o}`,!0)}let n=t.data,r=ze(n.file_path);try{await wm(r);let o=ze(await bm(r));km(o);let s=await Cm(o,"utf8"),i=Am(e),c=s,a=0;for(let l=0;l<i.length;l+=1){let p=i[l];if(!p)continue;let u=p.old_string,d=p.replace_all??!1;if(!c.includes(u))return i.length===1?h("apply_patch failed: target text not found.",!0):h(`apply_patch failed: target text not found at edit ${l+1}.`,!0);if(d){let m=c.split(u),g=m.length-1;c=m.join(p.new_string),a+=g}else c=c.replace(u,p.new_string),a+=1}return c===s?h("No changes made."):(await xm(o,c,"utf8"),h(`Success. Updated file: ${o}
|
|
52
61
|
Edits: ${i.length}
|
|
53
|
-
Replacements: ${a}`))}catch(o){return o.code==="ENOENT"?h(`apply_patch failed: file does not exist: ${r}`,!0):h(`apply_patch failed: ${o.message}`,!0)}}});import{readFile as
|
|
54
|
-
`)}function
|
|
55
|
-
`))}catch(i){return h(`list_dir failed: ${i.message}`,!0)}}});import{spawn as
|
|
56
|
-
`))}catch(s){return h(`grep_files failed: ${s.message}`,!0)}}});import{z as Ge}from"zod";import{createHash as qm}from"crypto";import{mkdir as Vm,readFile as Xm,rename as Jm,writeFile as Ym}from"fs/promises";import{homedir as no}from"os";import{dirname as Qm,join as so}from"path";var Zm="mcp.json",Rn=2,ef=120,tf=600*1e3,Ui=1440*60*1e3;function eo(){return process.env.MEMO_FORCE_MCP_DISK_CACHE==="1"?!0:!(process.env.MEMO_FORCE_MCP_DISK_CACHE==="0"||process.env.NODE_ENV==="test"||typeof process.env.VITEST<"u"||typeof process.env.VITEST_WORKER_ID<"u")}function nf(e){return e==="~"?no():e.startsWith("~/")?so(no(),e.slice(2)):e}function rf(){let e=process.env.MEMO_HOME?.trim();return e?nf(e):so(no(),".memo")}function Fi(){return so(rf(),"cache",Zm)}function ro(e){if(e===null||typeof e!="object")return JSON.stringify(e);if(Array.isArray(e))return`[${e.map(r=>ro(r)).join(",")}]`;let t=e;return`{${Object.keys(t).sort().map(r=>`${JSON.stringify(r)}:${ro(t[r])}`).join(",")}}`}function Hi(e){return qm("sha256").update(ro(e)).digest("hex")}function of(e){return e instanceof Error?e.message:String(e)}function En(){return{version:Rn,toolsByServer:{},responses:{}}}var oo=class{data=En();loaded=!1;loadPromise=null;persistTimer=null;persistRunning=!1;persistRequested=!1;responseInflight=new Map;pruneExpiredResponses(t=Date.now()){for(let[n,r]of Object.entries(this.data.responses))r.expiresAt<=t&&delete this.data.responses[n]}pruneExpiredTools(t=Date.now()){for(let[n,r]of Object.entries(this.data.toolsByServer))t-r.fetchedAt>Ui&&delete this.data.toolsByServer[n]}async ensureLoaded(){if(!eo()){this.loaded=!0;return}if(!this.loaded){if(this.loadPromise){await this.loadPromise;return}this.loadPromise=(async()=>{let t=Fi();try{let n=await Xm(t,"utf8"),r=JSON.parse(n);if(r.version===Rn){let o=r;this.data={version:Rn,toolsByServer:o.toolsByServer??{},responses:o.responses??{}}}else r.version===1&&r.entries?this.data={version:Rn,toolsByServer:{},responses:r.entries}:this.data=En()}catch{this.data=En()}finally{this.pruneExpiredResponses(),this.pruneExpiredTools(),this.loaded=!0}})(),await this.loadPromise}}async persistToDisk(){if(!eo())return;this.pruneExpiredResponses(),this.pruneExpiredTools();let t=Fi(),n=`${t}.tmp`,r=Qm(t);await Vm(r,{recursive:!0}),await Ym(n,JSON.stringify(this.data,null,2),"utf8"),await Jm(n,t)}async flushPersistQueue(){if(!(!this.persistRequested||this.persistRunning)){this.persistRequested=!1,this.persistRunning=!0;try{await this.persistToDisk()}catch{}finally{this.persistRunning=!1,this.persistRequested&&this.flushPersistQueue()}}}schedulePersist(){eo()&&(this.persistRequested=!0,!this.persistTimer&&(this.persistTimer=setTimeout(()=>{this.persistTimer=null,this.flushPersistQueue()},ef),this.persistTimer.unref?.()))}async getServerTools(t,n){await this.ensureLoaded();let r=this.data.toolsByServer[t];if(!r)return null;if(r.configHash!==Hi(n))return delete this.data.toolsByServer[t],this.schedulePersist(),null;let o=Date.now()-r.fetchedAt;return o>Ui?(delete this.data.toolsByServer[t],this.schedulePersist(),null):{tools:r.tools,stale:o>tf,ageMs:o}}async setServerTools(t,n,r){await this.ensureLoaded(),this.data.toolsByServer[t]={fetchedAt:Date.now(),configHash:Hi(n),tools:r},this.schedulePersist()}async withResponseCache(t,n,r){await this.ensureLoaded();let o=this.data.responses[t];if(o&&o.expiresAt>Date.now())return o.value;o&&o.expiresAt<=Date.now()&&delete this.data.responses[t];let s=this.responseInflight.get(t);if(s)return await s;let i=(async()=>{let c=await r();return this.data.responses[t]={expiresAt:Date.now()+n,value:c},this.schedulePersist(),c})();this.responseInflight.set(t,i);try{return await i}catch(c){throw new Error(of(c))}finally{this.responseInflight.delete(t)}}async flushForTests(){this.persistTimer&&(clearTimeout(this.persistTimer),this.persistTimer=null),this.persistRequested=!0,await this.flushPersistQueue()}resetForTests(){this.data=En(),this.loaded=!1,this.loadPromise=null,this.responseInflight.clear(),this.persistRequested=!1,this.persistRunning=!1,this.persistTimer&&(clearTimeout(this.persistTimer),this.persistTimer=null)}},to=null;function In(){return to||(to=new oo),to}var Bi=null,Wi=null;function io(e){Bi=e}function ji(){return Bi}function ao(e){Wi=e}function zi(){return Wi}var sf=Ge.object({server:Ge.string().optional(),cursor:Ge.string().optional()}).strict(),af=Ge.object({server:Ge.string().optional(),cursor:Ge.string().optional()}).strict(),cf=Ge.object({server:Ge.string().min(1),uri:Ge.string().min(1)}).strict(),On=15e3,lf=6e4;function co(){let e=ji();if(!e)throw new Error("MCP pool is not initialized");return e}function lo(){return zi()??In()}function qi(e){return e instanceof Error?e.message:String(e)}function Gi(e,t){return`list_resources:${e}:${t??""}`}function Ki(e,t){return`list_resource_templates:${e}:${t??""}`}function uf(e,t){return`read_resource:${e}:${t}`}function uo(e,t){return typeof e.hasServer=="function"?e.hasServer(t):typeof e.get=="function"?!!e.get(t):!1}async function po(e,t){if(typeof e.get=="function"){let n=e.get(t);if(n)return n}if(typeof e.connect=="function")return e.connect(t)}async function Vi(e){if(typeof e.getKnownServerNames=="function"&&typeof e.connect=="function"){let n=e.getKnownServerNames().sort((s,i)=>s.localeCompare(i)),r=await Promise.allSettled(n.map(s=>e.connect(s))),o=[];return r.forEach((s,i)=>{if(s.status==="fulfilled"){o.push(s.value);return}let c=n[i];c&&o.push({name:c,client:null,__error:s.reason})}),o.sort((s,i)=>s.name.localeCompare(i.name))}return(typeof e.getAll=="function"?e.getAll():[]).sort((n,r)=>n.name.localeCompare(r.name))}var Xi=D({name:"list_mcp_resources",description:"Lists resources provided by MCP servers. Prefer resources over web search when possible.",inputSchema:sf,supportsParallelToolCalls:!0,isMutating:!1,execute:async({server:e,cursor:t})=>{try{let n=co(),r=lo(),o=e?.trim();if(o){if(!uo(n,o))return h(`MCP server not found: ${e}`,!0);let a=await po(n,o);if(!a)return h(`MCP server not found: ${e}`,!0);let l=await r.withResponseCache(Gi(a.name,t),On,async()=>{let p=await a.client.listResources(t?{cursor:t}:void 0);return{server:a.name,resources:p.resources,nextCursor:p.nextCursor}});return h(JSON.stringify(l,null,2))}if(t)return h("cursor is only supported when server is specified",!0);let s=await Vi(n),i=`all:${s.map(a=>a.name).join(",")}`,c=await r.withResponseCache(Gi(i),On,async()=>{let a=await Promise.allSettled(s.map(async u=>{if(!u.client)throw new Error(`MCP server '${u.name}' is not connected`);return{server:u.name,result:await u.client.listResources()}})),l=[],p=[];return a.forEach((u,d)=>{let m=s[d]?.name??"unknown";if(u.status==="rejected"){p.push({server:m,error:qi(u.reason)});return}for(let g of u.value.result.resources)l.push({server:u.value.server,...g})}),{resources:l,...p.length>0?{errors:p}:{}}});return h(JSON.stringify(c,null,2))}catch(n){return h(`list_mcp_resources failed: ${n.message}`,!0)}}}),Ji=D({name:"list_mcp_resource_templates",description:"Lists resource templates provided by MCP servers. Prefer resource templates over web search when possible.",inputSchema:af,supportsParallelToolCalls:!0,isMutating:!1,execute:async({server:e,cursor:t})=>{try{let n=co(),r=lo(),o=e?.trim();if(o){if(!uo(n,o))return h(`MCP server not found: ${e}`,!0);let a=await po(n,o);if(!a)return h(`MCP server not found: ${e}`,!0);let l=await r.withResponseCache(Ki(a.name,t),On,async()=>{let p=await a.client.listResourceTemplates(t?{cursor:t}:void 0);return{server:a.name,resourceTemplates:p.resourceTemplates,nextCursor:p.nextCursor}});return h(JSON.stringify(l,null,2))}if(t)return h("cursor is only supported when server is specified",!0);let s=await Vi(n),i=`all:${s.map(a=>a.name).join(",")}`,c=await r.withResponseCache(Ki(i),On,async()=>{let a=await Promise.allSettled(s.map(async u=>{if(!u.client)throw new Error(`MCP server '${u.name}' is not connected`);return{server:u.name,result:await u.client.listResourceTemplates()}})),l=[],p=[];return a.forEach((u,d)=>{let m=s[d]?.name??"unknown";if(u.status==="rejected"){p.push({server:m,error:qi(u.reason)});return}for(let g of u.value.result.resourceTemplates)l.push({server:u.value.server,...g})}),{resourceTemplates:l,...p.length>0?{errors:p}:{}}});return h(JSON.stringify(c,null,2))}catch(n){return h(`list_mcp_resource_templates failed: ${n.message}`,!0)}}}),Yi=D({name:"read_mcp_resource",description:"Read a specific resource from an MCP server given the server name and resource URI.",inputSchema:cf,supportsParallelToolCalls:!0,isMutating:!1,execute:async({server:e,uri:t})=>{try{let n=co(),r=lo(),o=e.trim();if(!uo(n,o))return h(`MCP server not found: ${e}`,!0);let s=await po(n,o);if(!s)return h(`MCP server not found: ${e}`,!0);let i=await r.withResponseCache(uf(o,t),lf,async()=>{let c=await s.client.readResource({uri:t});return{server:o,uri:t,...c}});return h(JSON.stringify(i,null,2))}catch(n){return h(`read_mcp_resource failed: ${n.message}`,!0)}}});import{z as Ct}from"zod";var pf=Ct.object({step:Ct.string().min(1),status:Ct.enum(["pending","in_progress","completed"])}).strict(),df=Ct.object({explanation:Ct.string().optional(),plan:Ct.array(pf).min(1)}).strict(),Qi=[],Zi=D({name:"update_plan",description:"Updates the task plan. At most one step can be in_progress at a time.",inputSchema:df,supportsParallelToolCalls:!1,isMutating:!1,execute:async({explanation:e,plan:t})=>{if(t.filter(r=>r.status==="in_progress").length>1)return h("At most one step can be in_progress at a time",!0);if(t.length<4){let r=t[0]?.step??"";return h(`<system_hint tool="update_plan" reason="simple_task">Task "${r}" is simple (${t.length} step${t.length>1?"s":""}) - execute directly without update_plan.</system_hint>`)}return Qi=t,h(JSON.stringify({message:"Plan updated",explanation:e,plan:Qi},null,2))}});import{readFile as mf}from"fs/promises";import{homedir as ff}from"os";import{join as ea}from"path";import{z as ta}from"zod";var gf=ta.object({memory_id:ta.string().min(1)}).strict();function hf(){let e=process.env.MEMO_HOME?.trim()||ea(ff(),".memo");return ea(e,"Agents.md")}var na=D({name:"get_memory",description:"Loads the stored memory payload for a memory_id.",inputSchema:gf,supportsParallelToolCalls:!0,isMutating:!1,execute:async({memory_id:e})=>{try{let t=hf(),n=await mf(t,"utf8");return h(JSON.stringify({memory_id:e,memory_summary:n},null,2))}catch{return h(`memory not found for memory_id=${e}`,!0)}}});import{z as ra}from"zod";var yf=ra.object({url:ra.string().min(1)}).strict(),oa=1e4,Xt=512e3,_f=new Set(["http:","https:"]),Sf=/<\/\s*(p|div|section|article|header|footer|aside|main|h[1-6]|li|tr|table|blockquote)\s*>/gi,vf=/<\s*(br|hr)\s*\/?>/gi,Tf=/<\s*li[^>]*>/gi,wf=/<[^>]+>/g,Cf=/<(script|style)[^>]*>[\s\S]*?<\/\s*\1>/gi,bf=e=>e.replace(/ /gi," ").replace(/</gi,"<").replace(/>/gi,">").replace(/&/gi,"&").replace(/"/gi,'"').replace(/'/g,"'").replace(/&#(x?[0-9a-fA-F]+);/g,(r,o)=>{try{let s=o.startsWith("x")||o.startsWith("X")?parseInt(o.slice(1),16):parseInt(o,10);return Number.isFinite(s)?String.fromCharCode(s):""}catch{return""}}),xf=e=>{let r=e.replace(Cf," ").replace(Tf,"- ").replace(vf,`
|
|
57
|
-
`).replace(
|
|
58
|
-
`).replace(
|
|
62
|
+
Replacements: ${a}`))}catch(o){return o.code==="ENOENT"?h(`apply_patch failed: file does not exist: ${r}`,!0):h(`apply_patch failed: ${o.message}`,!0)}}});import{readFile as Pm}from"fs/promises";import{z as Oe}from"zod";var Li=500,Ni=200,Em=Oe.object({file_path:Oe.string().min(1),offset:Oe.number().int().positive().optional(),limit:Oe.number().int().positive().optional(),mode:Oe.enum(["slice","indentation"]).optional(),indentation:Oe.object({anchor_line:Oe.number().int().positive().optional(),max_levels:Oe.number().int().nonnegative().optional(),include_siblings:Oe.boolean().optional(),include_header:Oe.boolean().optional(),max_lines:Oe.number().int().positive().optional()}).strict().optional()}).strict();function Rm(e){return e.length<=Li?e:e.slice(0,Li)}function Im(e){let t=0;for(let n of e)if(n===" ")t+=1;else if(n===" ")t+=4;else break;return t}function Om(e){return e.split(/\r?\n/).map((n,r)=>({line:r+1,text:Rm(n),indent:Im(n)}))}function Lm(e){return e.map(t=>`L${t.line}: ${t.text}`).join(`
|
|
63
|
+
`)}function Nm(e,t,n){let r=t-1;if(r>=e.length)throw new Error("offset exceeds file length");return e.slice(r,r+n)}function $m(e,t){let n=t.offset??1,r=t.limit??Ni,o=t.indentation,s=o?.anchor_line??n;if(s<=0||s>e.length)throw new Error("anchor_line exceeds file length");let i=e[s-1];if(!i)throw new Error("anchor_line exceeds file length");let c=o?.max_levels??0,a=o?.include_siblings??!0,l=o?.include_header??!0,p=o?.max_lines??r,u=Math.max(1,Math.min(r,p)),d=c===0?0:Math.max(0,i.indent-c*4),m=s-1,g=s-1;for(;m-1>=0;){let y=e[m-1];if(!y)break;let b=/^\s*(#|\/\/|--)/.test(y.text),U=y.text.trim().length===0;if(y.indent<d||!a&&y.indent===d&&!b&&!U||!l&&(b||U)&&y.indent<i.indent||(m-=1,g-m+1>=u))break}for(;g+1<e.length&&g-m+1<u;){let y=e[g+1];if(!y||y.indent<d||!a&&y.indent===d)break;g+=1}return e.slice(m,g+1)}var $i=D({name:"read_file",description:"Reads a local file with 1-indexed line numbers, supporting slice and indentation-aware block modes.",inputSchema:Em,supportsParallelToolCalls:!0,isMutating:!1,execute:async e=>{let t=e.offset??1,n=e.limit??Ni;if(t<=0)return h("offset must be a 1-indexed line number",!0);if(n<=0)return h("limit must be greater than zero",!0);let r=e.file_path.trim();if(!r.startsWith("/"))return h("file_path must be an absolute path",!0);let o=ze(r);try{let s=await Pm(o,"utf8"),i=Om(s);if(i.length===0)return h("");let a=(e.mode??"slice")==="indentation"?$m(i,e):Nm(i,t,n);return h(Lm(a))}catch(s){return h(`read_file failed: ${s.message}`,!0)}}});import{readdir as Dm,lstat as Um}from"fs/promises";import{join as Hm}from"path";import{z as Vt}from"zod";var Fm=1,Bm=25,jm=2,Wm=Vt.object({dir_path:Vt.string().min(1),offset:Vt.number().int().positive().optional(),limit:Vt.number().int().positive().optional(),depth:Vt.number().int().positive().optional()}).strict();function zm(e){let t=" ".repeat(e.displayDepth*2),n="";e.kind==="dir"&&(n="/"),e.kind==="symlink"&&(n="@"),e.kind==="other"&&(n="?");let r=e.path.split("/"),o=r[r.length-1]??e.path;return`${t}${o}${n}`}var Di=D({name:"list_dir",description:"Lists entries in a local directory with 1-indexed entry numbers and simple type labels.",inputSchema:Wm,supportsParallelToolCalls:!0,isMutating:!1,execute:async e=>{let t=e.offset??Fm,n=e.limit??Bm,r=e.depth??jm;if(t<=0)return h("offset must be a 1-indexed entry number",!0);if(n<=0)return h("limit must be greater than zero",!0);if(r<=0)return h("depth must be greater than zero",!0);let o=e.dir_path.trim();if(!o.startsWith("/"))return h("dir_path must be an absolute path",!0);let s=ze(o);try{let i=[{absPath:s,depth:r,displayDepth:0}],c=[];for(;i.length>0;){let u=i.shift();if(!u)continue;let d=await Dm(u.absPath);d.sort((m,g)=>m.localeCompare(g));for(let m of d){let g=Hm(u.absPath,m),y=await Um(g),b=y.isSymbolicLink()?"symlink":y.isDirectory()?"dir":y.isFile()?"file":"other";c.push({path:g,displayDepth:u.displayDepth,kind:b}),b==="dir"&&u.depth>1&&i.push({absPath:g,depth:u.depth-1,displayDepth:u.displayDepth+1})}}if(c.length===0)return h(`Absolute path: ${s}`);let a=t-1;if(a>=c.length)return h("offset exceeds directory entry count",!0);let l=c.slice(a,a+n),p=[`Absolute path: ${s}`,...l.map(zm)];return a+n<c.length&&p.push(`More than ${n} entries found`),h(p.join(`
|
|
64
|
+
`))}catch(i){return h(`list_dir failed: ${i.message}`,!0)}}});import{spawn as Gm}from"child_process";import{resolve as Km}from"path";import{z as Xt}from"zod";var qm=100,Vm=2e3,Xm=3e4,Jm=Xt.object({pattern:Xt.string().min(1),include:Xt.string().optional(),path:Xt.string().optional(),limit:Xt.number().int().positive().optional()}).strict();function Ym(e){return new Promise((t,n)=>{let r=["--files-with-matches","--sortr=modified","--regexp",e.pattern,"--max-count","1","--no-messages"];e.include?.trim()&&r.push("--glob",e.include.trim()),r.push("--",e.searchPath);let o=Gm("rg",r,{cwd:e.cwd,stdio:["ignore","pipe","pipe"]}),s=[],i=[];o.stdout?.setEncoding("utf8"),o.stderr?.setEncoding("utf8"),o.stdout?.on("data",g=>s.push(g)),o.stderr?.on("data",g=>i.push(g));let c=!1,a=null,l=!1,p=()=>{clearTimeout(m),a&&(clearTimeout(a),a=null)},u=g=>{l||(l=!0,p(),n(g))},d=g=>{l||(l=!0,p(),t(g))},m=setTimeout(()=>{c=!0;try{o.kill("SIGTERM")}catch{}a=setTimeout(()=>{if(o.exitCode===null)try{o.kill("SIGKILL")}catch{}},500),a.unref?.()},Xm);m.unref?.(),o.on("error",g=>{u(g)}),o.on("close",g=>{if(c){u(new Error("rg timed out after 30 seconds"));return}d({exitCode:typeof g=="number"?g:-1,stdout:s.join(""),stderr:i.join("")})})})}var Ui=D({name:"grep_files",description:"Finds files whose contents match the pattern and lists them by modification time.",inputSchema:Jm,supportsParallelToolCalls:!0,isMutating:!1,execute:async e=>{let t=e.pattern.trim();if(!t)return h("pattern must not be empty",!0);let n=Math.min(e.limit??qm,Vm),r=Fe(),o=e.path?.trim()?Km(r,e.path.trim()):r;try{let s=await Ym({pattern:t,include:e.include,searchPath:o,cwd:r,limit:n});if(s.exitCode===1)return h("No matches found.");if(s.exitCode!==0)return h(`rg failed: ${s.stderr||s.stdout}`,!0);let i=s.stdout.split(/\r?\n/).map(c=>c.trim()).filter(Boolean).slice(0,n);return i.length===0?h("No matches found."):h(i.join(`
|
|
65
|
+
`))}catch(s){return h(`grep_files failed: ${s.message}`,!0)}}});import{z as Ge}from"zod";import{createHash as Qm}from"crypto";import{mkdir as Zm,readFile as ef,rename as tf,writeFile as nf}from"fs/promises";import{homedir as oo}from"os";import{dirname as rf,join as ao}from"path";var of="mcp.json",In=2,sf=120,af=600*1e3,Hi=1440*60*1e3;function no(){return process.env.MEMO_FORCE_MCP_DISK_CACHE==="1"?!0:!(process.env.MEMO_FORCE_MCP_DISK_CACHE==="0"||process.env.NODE_ENV==="test"||typeof process.env.VITEST<"u"||typeof process.env.VITEST_WORKER_ID<"u")}function cf(e){return e==="~"?oo():e.startsWith("~/")?ao(oo(),e.slice(2)):e}function lf(){let e=process.env.MEMO_HOME?.trim();return e?cf(e):ao(oo(),".memo")}function Fi(){return ao(lf(),"cache",of)}function so(e){if(e===null||typeof e!="object")return JSON.stringify(e);if(Array.isArray(e))return`[${e.map(r=>so(r)).join(",")}]`;let t=e;return`{${Object.keys(t).sort().map(r=>`${JSON.stringify(r)}:${so(t[r])}`).join(",")}}`}function Bi(e){return Qm("sha256").update(so(e)).digest("hex")}function uf(e){return e instanceof Error?e.message:String(e)}function Rn(){return{version:In,toolsByServer:{},responses:{}}}var io=class{data=Rn();loaded=!1;loadPromise=null;persistTimer=null;persistRunning=!1;persistRequested=!1;responseInflight=new Map;pruneExpiredResponses(t=Date.now()){for(let[n,r]of Object.entries(this.data.responses))r.expiresAt<=t&&delete this.data.responses[n]}pruneExpiredTools(t=Date.now()){for(let[n,r]of Object.entries(this.data.toolsByServer))t-r.fetchedAt>Hi&&delete this.data.toolsByServer[n]}async ensureLoaded(){if(!no()){this.loaded=!0;return}if(!this.loaded){if(this.loadPromise){await this.loadPromise;return}this.loadPromise=(async()=>{let t=Fi();try{let n=await ef(t,"utf8"),r=JSON.parse(n);if(r.version===In){let o=r;this.data={version:In,toolsByServer:o.toolsByServer??{},responses:o.responses??{}}}else r.version===1&&r.entries?this.data={version:In,toolsByServer:{},responses:r.entries}:this.data=Rn()}catch{this.data=Rn()}finally{this.pruneExpiredResponses(),this.pruneExpiredTools(),this.loaded=!0}})(),await this.loadPromise}}async persistToDisk(){if(!no())return;this.pruneExpiredResponses(),this.pruneExpiredTools();let t=Fi(),n=`${t}.tmp`,r=rf(t);await Zm(r,{recursive:!0}),await nf(n,JSON.stringify(this.data,null,2),"utf8"),await tf(n,t)}async flushPersistQueue(){if(!(!this.persistRequested||this.persistRunning)){this.persistRequested=!1,this.persistRunning=!0;try{await this.persistToDisk()}catch{}finally{this.persistRunning=!1,this.persistRequested&&this.flushPersistQueue()}}}schedulePersist(){no()&&(this.persistRequested=!0,!this.persistTimer&&(this.persistTimer=setTimeout(()=>{this.persistTimer=null,this.flushPersistQueue()},sf),this.persistTimer.unref?.()))}async getServerTools(t,n){await this.ensureLoaded();let r=this.data.toolsByServer[t];if(!r)return null;if(r.configHash!==Bi(n))return delete this.data.toolsByServer[t],this.schedulePersist(),null;let o=Date.now()-r.fetchedAt;return o>Hi?(delete this.data.toolsByServer[t],this.schedulePersist(),null):{tools:r.tools,stale:o>af,ageMs:o}}async setServerTools(t,n,r){await this.ensureLoaded(),this.data.toolsByServer[t]={fetchedAt:Date.now(),configHash:Bi(n),tools:r},this.schedulePersist()}async withResponseCache(t,n,r){await this.ensureLoaded();let o=this.data.responses[t];if(o&&o.expiresAt>Date.now())return o.value;o&&o.expiresAt<=Date.now()&&delete this.data.responses[t];let s=this.responseInflight.get(t);if(s)return await s;let i=(async()=>{let c=await r();return this.data.responses[t]={expiresAt:Date.now()+n,value:c},this.schedulePersist(),c})();this.responseInflight.set(t,i);try{return await i}catch(c){throw new Error(uf(c))}finally{this.responseInflight.delete(t)}}async flushForTests(){this.persistTimer&&(clearTimeout(this.persistTimer),this.persistTimer=null),this.persistRequested=!0,await this.flushPersistQueue()}resetForTests(){this.data=Rn(),this.loaded=!1,this.loadPromise=null,this.responseInflight.clear(),this.persistRequested=!1,this.persistRunning=!1,this.persistTimer&&(clearTimeout(this.persistTimer),this.persistTimer=null)}},ro=null;function On(){return ro||(ro=new io),ro}var ji=null,Wi=null;function co(e){ji=e}function zi(){return ji}function lo(e){Wi=e}function Gi(){return Wi}var pf=Ge.object({server:Ge.string().optional(),cursor:Ge.string().optional()}).strict(),df=Ge.object({server:Ge.string().optional(),cursor:Ge.string().optional()}).strict(),mf=Ge.object({server:Ge.string().min(1),uri:Ge.string().min(1)}).strict(),Ln=15e3,ff=6e4;function uo(){let e=zi();if(!e)throw new Error("MCP pool is not initialized");return e}function po(){return Gi()??On()}function Vi(e){return e instanceof Error?e.message:String(e)}function Ki(e,t){return`list_resources:${e}:${t??""}`}function qi(e,t){return`list_resource_templates:${e}:${t??""}`}function gf(e,t){return`read_resource:${e}:${t}`}function mo(e,t){return typeof e.hasServer=="function"?e.hasServer(t):typeof e.get=="function"?!!e.get(t):!1}async function fo(e,t){if(typeof e.get=="function"){let n=e.get(t);if(n)return n}if(typeof e.connect=="function")return e.connect(t)}async function Xi(e){if(typeof e.getKnownServerNames=="function"&&typeof e.connect=="function"){let n=e.getKnownServerNames().sort((s,i)=>s.localeCompare(i)),r=await Promise.allSettled(n.map(s=>e.connect(s))),o=[];return r.forEach((s,i)=>{if(s.status==="fulfilled"){o.push(s.value);return}let c=n[i];c&&o.push({name:c,client:null,__error:s.reason})}),o.sort((s,i)=>s.name.localeCompare(i.name))}return(typeof e.getAll=="function"?e.getAll():[]).sort((n,r)=>n.name.localeCompare(r.name))}var Ji=D({name:"list_mcp_resources",description:"Lists resources provided by MCP servers. Prefer resources over web search when possible.",inputSchema:pf,supportsParallelToolCalls:!0,isMutating:!1,execute:async({server:e,cursor:t})=>{try{let n=uo(),r=po(),o=e?.trim();if(o){if(!mo(n,o))return h(`MCP server not found: ${e}`,!0);let a=await fo(n,o);if(!a)return h(`MCP server not found: ${e}`,!0);let l=await r.withResponseCache(Ki(a.name,t),Ln,async()=>{let p=await a.client.listResources(t?{cursor:t}:void 0);return{server:a.name,resources:p.resources,nextCursor:p.nextCursor}});return h(JSON.stringify(l,null,2))}if(t)return h("cursor is only supported when server is specified",!0);let s=await Xi(n),i=`all:${s.map(a=>a.name).join(",")}`,c=await r.withResponseCache(Ki(i),Ln,async()=>{let a=await Promise.allSettled(s.map(async u=>{if(!u.client)throw new Error(`MCP server '${u.name}' is not connected`);return{server:u.name,result:await u.client.listResources()}})),l=[],p=[];return a.forEach((u,d)=>{let m=s[d]?.name??"unknown";if(u.status==="rejected"){p.push({server:m,error:Vi(u.reason)});return}for(let g of u.value.result.resources)l.push({server:u.value.server,...g})}),{resources:l,...p.length>0?{errors:p}:{}}});return h(JSON.stringify(c,null,2))}catch(n){return h(`list_mcp_resources failed: ${n.message}`,!0)}}}),Yi=D({name:"list_mcp_resource_templates",description:"Lists resource templates provided by MCP servers. Prefer resource templates over web search when possible.",inputSchema:df,supportsParallelToolCalls:!0,isMutating:!1,execute:async({server:e,cursor:t})=>{try{let n=uo(),r=po(),o=e?.trim();if(o){if(!mo(n,o))return h(`MCP server not found: ${e}`,!0);let a=await fo(n,o);if(!a)return h(`MCP server not found: ${e}`,!0);let l=await r.withResponseCache(qi(a.name,t),Ln,async()=>{let p=await a.client.listResourceTemplates(t?{cursor:t}:void 0);return{server:a.name,resourceTemplates:p.resourceTemplates,nextCursor:p.nextCursor}});return h(JSON.stringify(l,null,2))}if(t)return h("cursor is only supported when server is specified",!0);let s=await Xi(n),i=`all:${s.map(a=>a.name).join(",")}`,c=await r.withResponseCache(qi(i),Ln,async()=>{let a=await Promise.allSettled(s.map(async u=>{if(!u.client)throw new Error(`MCP server '${u.name}' is not connected`);return{server:u.name,result:await u.client.listResourceTemplates()}})),l=[],p=[];return a.forEach((u,d)=>{let m=s[d]?.name??"unknown";if(u.status==="rejected"){p.push({server:m,error:Vi(u.reason)});return}for(let g of u.value.result.resourceTemplates)l.push({server:u.value.server,...g})}),{resourceTemplates:l,...p.length>0?{errors:p}:{}}});return h(JSON.stringify(c,null,2))}catch(n){return h(`list_mcp_resource_templates failed: ${n.message}`,!0)}}}),Qi=D({name:"read_mcp_resource",description:"Read a specific resource from an MCP server given the server name and resource URI.",inputSchema:mf,supportsParallelToolCalls:!0,isMutating:!1,execute:async({server:e,uri:t})=>{try{let n=uo(),r=po(),o=e.trim();if(!mo(n,o))return h(`MCP server not found: ${e}`,!0);let s=await fo(n,o);if(!s)return h(`MCP server not found: ${e}`,!0);let i=await r.withResponseCache(gf(o,t),ff,async()=>{let c=await s.client.readResource({uri:t});return{server:o,uri:t,...c}});return h(JSON.stringify(i,null,2))}catch(n){return h(`read_mcp_resource failed: ${n.message}`,!0)}}});import{z as bt}from"zod";var hf=bt.object({step:bt.string().min(1),status:bt.enum(["pending","in_progress","completed"])}).strict(),yf=bt.object({explanation:bt.string().optional(),plan:bt.array(hf).min(1)}).strict(),Zi=[],ea=D({name:"update_plan",description:"Updates the task plan. At most one step can be in_progress at a time.",inputSchema:yf,supportsParallelToolCalls:!1,isMutating:!1,execute:async({explanation:e,plan:t})=>{if(t.filter(r=>r.status==="in_progress").length>1)return h("At most one step can be in_progress at a time",!0);if(t.length<4){let r=t[0]?.step??"";return h(`<system_hint tool="update_plan" reason="simple_task">Task "${r}" is simple (${t.length} step${t.length>1?"s":""}) - execute directly without update_plan.</system_hint>`)}return Zi=t,h(JSON.stringify({message:"Plan updated",explanation:e,plan:Zi},null,2))}});import{readFile as Sf}from"fs/promises";import{homedir as _f}from"os";import{join as ta}from"path";import{z as na}from"zod";var vf=na.object({memory_id:na.string().min(1)}).strict();function Tf(){let e=process.env.MEMO_HOME?.trim()||ta(_f(),".memo");return ta(e,"Agents.md")}var ra=D({name:"get_memory",description:"Loads the stored memory payload for a memory_id.",inputSchema:vf,supportsParallelToolCalls:!0,isMutating:!1,execute:async({memory_id:e})=>{try{let t=Tf(),n=await Sf(t,"utf8");return h(JSON.stringify({memory_id:e,memory_summary:n},null,2))}catch{return h(`memory not found for memory_id=${e}`,!0)}}});import{z as oa}from"zod";var wf=oa.object({url:oa.string().min(1)}).strict(),sa=1e4,Jt=512e3,Cf=new Set(["http:","https:"]),bf=/<\/\s*(p|div|section|article|header|footer|aside|main|h[1-6]|li|tr|table|blockquote)\s*>/gi,xf=/<\s*(br|hr)\s*\/?>/gi,Mf=/<\s*li[^>]*>/gi,kf=/<[^>]+>/g,Af=/<(script|style)[^>]*>[\s\S]*?<\/\s*\1>/gi,Pf=e=>e.replace(/ /gi," ").replace(/</gi,"<").replace(/>/gi,">").replace(/&/gi,"&").replace(/"/gi,'"').replace(/'/g,"'").replace(/&#(x?[0-9a-fA-F]+);/g,(r,o)=>{try{let s=o.startsWith("x")||o.startsWith("X")?parseInt(o.slice(1),16):parseInt(o,10);return Number.isFinite(s)?String.fromCharCode(s):""}catch{return""}}),Ef=e=>{let r=e.replace(Af," ").replace(Mf,"- ").replace(xf,`
|
|
66
|
+
`).replace(bf,`
|
|
67
|
+
`).replace(kf," "),s=Pf(r).replace(/\r/g,"").split(`
|
|
59
68
|
`).map(c=>c.trim().replace(/[ \t]{2,}/g," "));return s.filter((c,a)=>c.length>0||a>0&&(s[a-1]?.length??0)>0).join(`
|
|
60
|
-
`).trim()},
|
|
61
|
-
...[truncated]`}function
|
|
62
|
-
${o}`),
|
|
69
|
+
`).trim()},Rf=e=>e.replace(/\s+/g," ").trim(),ia=D({name:"webfetch",description:"HTTP GET request, returns processed plain text body (automatically strips HTML tags)",inputSchema:wf,supportsParallelToolCalls:!0,isMutating:!1,execute:async e=>{let t;try{t=new URL(e.url)}catch{return h(`Invalid URL: ${e.url}`,!0)}if(!Cf.has(t.protocol))return h(`Unsupported protocol: ${t.protocol}`,!0);let n=new AbortController,r=setTimeout(()=>n.abort(),sa);try{let o=await globalThis.fetch(t,{signal:n.signal}),s=o.headers.get("content-length"),i=s?Number(s):void 0;if(i&&i>Jt)return h(`Request rejected: response body too large (${i} bytes)`,!0);let c=0,a="";if(o.body&&o.body.getReader){let U=o.body.getReader(),T=[];for(;;){let{done:I,value:G}=await U.read();if(I)break;if(G){if(c+=G.byteLength,c>Jt)return n.abort(),h(`Request aborted: response body exceeds ${Jt} bytes`,!0);T.push(G)}}let F=new Uint8Array(c),R=0;for(let I of T)F.set(I,R),R+=I.byteLength;a=new TextDecoder().decode(F)}else if(a=await o.text(),c=new TextEncoder().encode(a).byteLength,c>Jt)return h(`Request rejected: response body exceeds ${Jt} bytes`,!0);let l=o.headers.get("content-type")||"",p=/text\/html/i.test(l)||/^\s*<!doctype html/i.test(a)||/^\s*<html[\s>]/i.test(a),u=p?Ef(a):a.trim(),d=Rf(u),m=Pi(),g=d.length>m?`${d.slice(0,m)}...`:d,y=d.length>m?" text_truncated=true":"",b=p?" source=html_stripped":"";return h(`status=${o.status} bytes=${c} text_chars=${d.length} text="${g}"${y}${b}`)}catch(o){return o.name==="AbortError"?h(`Request timeout or aborted (${sa}ms)`,!0):h(`Request failed: ${o.message}`,!0)}finally{clearTimeout(r)}}});import{spawn as If}from"child_process";import{existsSync as Of}from"fs";import{resolve as Lf}from"path";import{z as fe}from"zod";var Nf=3e4,$f=1e4,Df=3e5,aa=4,Uf=1500,ca=2e3,Ye=new Map,Hf=fe.object({message:fe.string().min(1),agent_type:fe.string().optional()}).strict(),Ff=fe.object({id:fe.string().min(1),message:fe.string().min(1),interrupt:fe.boolean().optional()}).strict(),Bf=fe.object({id:fe.string().min(1)}).strict(),jf=fe.object({ids:fe.array(fe.string().min(1)).min(1),timeout_ms:fe.number().int().positive().optional()}).strict(),Wf=fe.object({id:fe.string().min(1)}).strict();function xt(){return new Date().toISOString()}function go(e){return h(`agent not found: ${e}`,!0)}function zf(){let e=process.env.MEMO_SUBAGENT_MAX_AGENTS?.trim();if(!e)return aa;let t=Number(e);return!Number.isFinite(t)||t<=0?aa:Math.floor(t)}function Gf(){let e=0;for(let t of Ye.values())t.running&&(e+=1);return e}function Kf(){let e=process.env.MEMO_SUBAGENT_COMMAND?.trim();if(e)return e;let t=Lf(Fe(),"dist/index.js");return Of(t)?`node ${JSON.stringify(t)} --dangerous`:"memo --dangerous"}function qf(e){return e!=="running"}function Vf(e){return new Promise(t=>{setTimeout(t,e)})}function Xf(e){return e===void 0?Nf:e<=0?null:Math.max($f,Math.min(Df,e))}function Jf(e){return e.length<=ca?e:`${e.slice(0,ca)}
|
|
70
|
+
...[truncated]`}function Yf(e,t){let n=[],r=e.trim(),o=t.trim();return r&&n.push(r),o&&n.push(`stderr:
|
|
71
|
+
${o}`),Jf(n.join(`
|
|
63
72
|
|
|
64
|
-
`))}async function
|
|
65
|
-
`)}catch{}try{s.stdin?.end()}catch{}return r}var
|
|
66
|
-
`)}function
|
|
73
|
+
`))}async function la(e){let t=e.running;if(!t)return;t.interrupted=!0;let n=t.process;n.exitCode!==null||n.killed||await new Promise(r=>{let o=!1,s=()=>{o||(o=!0,clearTimeout(i),n.off("close",s),r())},i=setTimeout(()=>{if(n.exitCode===null)try{n.kill("SIGKILL")}catch{s()}},Uf);n.on("close",s);try{n.kill("SIGTERM")}catch{s()}})}function Qf(e){let t=Ye.get(e);return t?t.status:"not_found"}function Zf(e){let t=Ye.get(e);return t?{status:t.status,last_message:t.lastMessage,last_output:t.lastOutput,last_error:t.lastError,last_submission_id:t.lastSubmissionId,updated_at:t.updatedAt}:{status:"not_found",last_message:null,last_output:null,last_error:null,last_submission_id:null,updated_at:null}}function eg(e){return{agent_id:e.id,status:e.status,created_at:e.createdAt,updated_at:e.updatedAt,last_message:e.lastMessage,last_submission_id:e.lastSubmissionId,has_last_output:!!e.lastOutput,has_last_error:!!e.lastError}}function tg(e){let{record:t,submissionId:n,stdout:r,stderr:o,exitCode:s,interrupted:i}=e;if(!(!t.running||t.running.id!==n)&&(t.running=null,t.updatedAt=xt(),t.lastOutput=Yf(r,o)||null,t.lastError=null,t.status!=="closed")){if(i){t.status="errored",t.lastError="interrupted",t.statusBeforeClose="errored";return}if(s===0){t.status="completed",t.statusBeforeClose="completed";return}t.status="errored",t.lastError=`submission failed with exit code ${s}`,t.statusBeforeClose="errored"}}async function ua(e,t){let n=zf();if(Gf()>=n)throw new Error(`subagent concurrency limit reached (${n})`);let r=crypto.randomUUID(),o=Kf(),s=If(o,{cwd:Fe(),env:{...process.env},shell:!0,stdio:["pipe","pipe","pipe"]}),i=[],c=[];s.stdout?.setEncoding("utf8"),s.stderr?.setEncoding("utf8"),s.stdout?.on("data",a=>i.push(a)),s.stderr?.on("data",a=>c.push(a)),s.on("error",a=>{c.push(`[spawn error] ${a.message}`)}),e.running={id:r,message:t,process:s,startedAt:xt(),interrupted:!1},e.status="running",e.lastMessage=t,e.lastSubmissionId=r,e.updatedAt=xt(),s.on("close",a=>{let l=typeof a=="number"?a:-1,p=!!(e.running?.id===r&&e.running.interrupted);tg({record:e,submissionId:r,stdout:i.join(""),stderr:c.join(""),exitCode:l,interrupted:p})});try{s.stdin?.write(`${t.trim()}
|
|
74
|
+
`)}catch{}try{s.stdin?.end()}catch{}return r}var pa=D({name:"spawn_agent",description:"Spawn a sub-agent for a well-scoped task and return the agent id.",inputSchema:Hf,supportsParallelToolCalls:!1,isMutating:!0,execute:async({message:e})=>{let t=e.trim();if(!t)return h("spawn_agent failed: message must not be empty",!0);let n=crypto.randomUUID(),r=xt(),o={id:n,createdAt:r,updatedAt:r,status:"running",statusBeforeClose:"completed",lastMessage:t,lastSubmissionId:null,lastOutput:null,lastError:null,running:null};Ye.set(n,o);try{let s=await ua(o,t);return h(JSON.stringify({...eg(o),submission_id:s},null,2))}catch(s){return Ye.delete(n),h(`spawn_agent failed: ${s.message}`,!0)}}}),da=D({name:"send_input",description:"Send a message to an existing agent.",inputSchema:Ff,supportsParallelToolCalls:!1,isMutating:!0,execute:async({id:e,message:t,interrupt:n})=>{let r=Ye.get(e);if(!r)return go(e);let o=t.trim();if(!o)return h("send_input failed: message must not be empty",!0);if(r.status==="closed")return h(`send_input failed: agent ${e} is closed; run resume_agent first`,!0);if(r.running){if(!n)return h(`send_input failed: agent ${e} is busy; set interrupt=true to cancel current submission`,!0);await la(r)}try{let s=await ua(r,o);return h(JSON.stringify({agent_id:r.id,status:r.status,submission_id:s},null,2))}catch(s){return h(`send_input failed: ${s.message}`,!0)}}}),ma=D({name:"resume_agent",description:"Resume a previously closed agent by id.",inputSchema:Bf,supportsParallelToolCalls:!1,isMutating:!0,execute:async({id:e})=>{let t=Ye.get(e);return t?(t.status==="closed"&&(t.status=t.statusBeforeClose,t.updatedAt=xt()),h(JSON.stringify({agent_id:e,status:t.status},null,2))):go(e)}}),fa=D({name:"wait",description:"Wait for agent statuses and return current snapshots.",inputSchema:jf,supportsParallelToolCalls:!1,isMutating:!1,execute:async({ids:e,timeout_ms:t})=>{let n=Xf(t);if(n===null)return h("wait failed: timeout_ms must be greater than zero",!0);let r=()=>{let i={},c={};for(let a of e){let l=Qf(a);qf(l)&&(i[a]=l,c[a]=Zf(a))}return{status:i,details:c}},o=r();if(Object.keys(o.status).length>0)return h(JSON.stringify({status:o.status,details:o.details,timed_out:!1},null,2));let s=Date.now()+n;for(;Date.now()<s;)if(await Vf(100),o=r(),Object.keys(o.status).length>0)return h(JSON.stringify({status:o.status,details:o.details,timed_out:!1},null,2));return h(JSON.stringify({status:{},details:{},timed_out:!0},null,2))}}),ga=D({name:"close_agent",description:"Close an agent and return its last known status.",inputSchema:Wf,supportsParallelToolCalls:!1,isMutating:!0,execute:async({id:e})=>{let t=Ye.get(e);return t?t.status==="closed"?h(JSON.stringify({agent_id:e,status:"closed"},null,2)):(t.statusBeforeClose=t.running?"completed":t.status,t.status="closed",t.updatedAt=xt(),await la(t),h(JSON.stringify({agent_id:e,status:"closed"},null,2))):go(e)}});var ho={list_mcp_resources:"read",list_mcp_resource_templates:"read",read_mcp_resource:"read",update_plan:"read",get_memory:"read",webfetch:"read",read_file:"read",list_dir:"read",grep_files:"read",wait:"read",spawn_agent:"read",send_input:"read",resume_agent:"read",close_agent:"read",apply_patch:"write",shell:"execute",shell_command:"execute",exec_command:"execute",write_stdin:"execute"},ha=new Set(["spawn_agent","send_input","resume_agent","wait","close_agent"]),Nn={read:0,write:1,execute:2},ya=["exec","run","shell","command","stdin"],Sa=["write","patch","create","delete","modify","update"],_a=["read","get","fetch","search","list","find"],va=new Set(["write","execute"]);function So(e){let t={...ho,...e?.customLevels};return{getRiskLevel(n){if(n in t)return t[n];let r=n.toLowerCase();return yo(r,ya)?"execute":yo(r,Sa)?"write":yo(r,_a)?"read":"write"},compareRisk(n,r){return Nn[n]-Nn[r]},needsApproval(n,r){return r==="strict"?!0:va.has(n)}}}function yo(e,t){return t.some(n=>e.includes(n))}import{createHash as ng}from"crypto";function $n(e){return e===null||typeof e!="object"?JSON.stringify(e):Array.isArray(e)?"["+e.map(n=>$n(n)).join(",")+"]":`{${Object.entries(e).sort(([n],[r])=>n.localeCompare(r)).map(([n,r])=>`${JSON.stringify(n)}:${$n(r)}`).join(",")}}`}function _o(e,t){let n=$n(t),r=`${e}:${n}`;return ng("sha256").update(r).digest("hex").slice(0,16)}function rg(e){return`Tool "${e}" requires approval.`}function vo(e){let{mode:t="auto",dangerous:n=!1,toolRiskLevels:r}=e||{},o=t==="strict"?"strict":"auto";if(n)return{isDangerousMode:!0,getRiskLevel:()=>"read",check:()=>({needApproval:!1,decision:"auto-execute"}),recordDecision:()=>{},isGranted:()=>!0,clearOnceApprovals:()=>{},dispose:()=>{}};let s=So({customLevels:r}),i={sessionTools:new Set,onceTools:new Set,deniedTools:new Set,toolByFingerprint:new Map};return{get isDangerousMode(){return!1},getRiskLevel(c){return s.getRiskLevel(c)},check(c,a){if(ha.has(c))return{needApproval:!1,decision:"auto-execute"};let l=s.getRiskLevel(c);if(!s.needsApproval(l,o))return{needApproval:!1,decision:"auto-execute"};let p=_o(c,a);return i.toolByFingerprint.set(p,c),i.sessionTools.has(c)||i.onceTools.has(c)?{needApproval:!1,decision:"auto-execute"}:i.deniedTools.has(c)?{needApproval:!0,fingerprint:p,riskLevel:l,reason:"This request was previously denied.",toolName:c,params:a}:{needApproval:!0,fingerprint:p,riskLevel:l,reason:rg(c),toolName:c,params:a}},recordDecision(c,a){let l=i.toolByFingerprint.get(c);if(l)switch(i.sessionTools.delete(l),i.onceTools.delete(l),i.deniedTools.delete(l),a){case"session":i.sessionTools.add(l);break;case"once":i.onceTools.add(l);break;case"deny":i.deniedTools.add(l);break}},isGranted(c){let a=i.toolByFingerprint.get(c);return a?i.sessionTools.has(a)||i.onceTools.has(a):!1},clearOnceApprovals(){i.onceTools.clear()},dispose(){i.sessionTools.clear(),i.onceTools.clear(),i.deniedTools.clear(),i.toolByFingerprint.clear()}}}var Ta=1e5;function og(e){return e.replace(/&/g,"&").replace(/"/g,""").replace(/</g,"<").replace(/>/g,">")}function sg(e){let t=0;for(let n of e.content??[]){if(n.type==="text"){t+=n.text.length;continue}try{t+=JSON.stringify(n).length}catch{t+=100}}return t}function ig(e,t,n){return`<system_hint type="tool_output_omitted" tool="${og(e)}" reason="too_long" actual_chars="${t}" max_chars="${n}">Tool output too long, automatically omitted. Please narrow the scope or add limit parameters and try again.</system_hint>`}function ag(e,t){let n=Pn(),r=sg(t);return r<=n?t:{content:[{type:"text",text:ig(e,r,n)}],isError:!1}}function cg(e){return(e.content?.flatMap(n=>n.type==="text"?[n.text]:[])??[]).join(`
|
|
75
|
+
`)}function wa(e){return typeof e=="object"&&e!==null&&!Array.isArray(e)}function lg(e,t){let n=t;if(typeof t=="string"){if(t.length>Ta)return{ok:!1,error:`${e.name} invalid input: input string too large (max ${Ta} chars)`};let r=t.trim();if(r)try{n=JSON.parse(r)}catch{n=r}else n={}}if(!wa(n))return{ok:!1,error:`${e.name} invalid input: expected object`};if(typeof e.validateInput=="function"){let r=e.validateInput(n);return r.ok?wa(r.data)?{ok:!0,data:r.data}:{ok:!1,error:`${e.name} invalid input: expected object`}:r}return{ok:!0,data:n}}function ug(e){let t=e instanceof Error?e.message.toLowerCase():String(e).toLowerCase();return t.includes("sandbox")||t.includes("permission denied")||t.includes("operation not permitted")||t.includes("eacces")?"sandbox_denied":"execution_failed"}var To=class{constructor(t){this.config=t;this.approvalManager=vo(t.approval)}approvalManager;async executeAction(t,n){let r=Date.now(),o=t.id??`${t.name}:${r}`,s=this.approvalManager.check(t.name,t.input);if(s.needApproval){let c={toolName:s.toolName,params:s.params,fingerprint:s.fingerprint,riskLevel:s.riskLevel,reason:s.reason};await n?.onApprovalRequest?.(c);let a=n?.requestApproval?await n.requestApproval(c):"deny";if(this.approvalManager.recordDecision(s.fingerprint,a),await n?.onApprovalResponse?.({fingerprint:s.fingerprint,decision:a}),a==="deny")return{actionId:o,tool:t.name,status:"approval_denied",errorType:"approval_denied",success:!1,observation:`User denied tool execution: ${t.name}`,durationMs:Date.now()-r,rejected:!0}}let i=this.config.tools[t.name];if(!i)return{actionId:o,tool:t.name,status:"tool_not_found",errorType:"tool_not_found",success:!1,observation:`Unknown tool: ${t.name}`,durationMs:Date.now()-r};try{let c=lg(i,t.input);if(!c.ok)return{actionId:o,tool:t.name,status:"input_invalid",errorType:"input_invalid",success:!1,observation:c.error,durationMs:Date.now()-r};let a=await i.execute(c.data),l=ag(t.name,a);return{actionId:o,tool:t.name,status:"success",success:!0,observation:cg(l)||"(no tool output)",durationMs:Date.now()-r}}catch(c){let a=ug(c);return{actionId:o,tool:t.name,status:a,errorType:a,success:!1,observation:`Tool execution failed: ${c.message}`,durationMs:Date.now()-r}}}async executeActions(t,n={}){let r=n.executionMode??"sequential",o=n.failurePolicy??(n.stopOnRejection===!1?"collect_all":"fail_fast"),s=[];if(r==="parallel"){let a=await Promise.all(t.map(l=>this.executeAction(l,n)));if(o==="fail_fast"){let l=a.findIndex(p=>p.rejected);s=l>=0?a.slice(0,l+1):a}else s=a}else for(let a of t){let l=await this.executeAction(a,n);if(s.push(l),l.rejected&&o==="fail_fast")break}let i=s.some(a=>a.rejected),c=s.map(a=>`[${a.tool}]: ${a.observation}`).join(`
|
|
67
76
|
|
|
68
|
-
`);return{results:s,combinedObservation:c,hasRejection:i,executionMode:r,failurePolicy:o}}clearOnceApprovals(){this.approvalManager.clearOnceApprovals()}dispose(){this.approvalManager.dispose()}};function
|
|
77
|
+
`);return{results:s,combinedObservation:c,hasRejection:i,executionMode:r,failurePolicy:o}}clearOnceApprovals(){this.approvalManager.clearOnceApprovals()}dispose(){this.approvalManager.dispose()}};function Ca(e){return new To(e)}var Dn=class{tools=new Map;register(t){this.tools.set(t.name,t)}registerMany(t){for(let n of t)this.register(n)}get(t){return this.tools.get(t)}getAll(){return Array.from(this.tools.values())}toRegistry(){let t={};for(let[n,r]of this.tools)t[n]=r;return t}has(t){return this.tools.has(t)}get size(){return this.tools.size}};import{Client as pg}from"@modelcontextprotocol/sdk/client/index.js";import{StreamableHTTPClientTransport as dg}from"@modelcontextprotocol/sdk/client/streamableHttp.js";import{StdioClientTransport as mg}from"@modelcontextprotocol/sdk/client/stdio.js";import{UnauthorizedError as fg}from"@modelcontextprotocol/sdk/client/auth.js";import{StreamableHTTPError as gg}from"@modelcontextprotocol/sdk/client/streamableHttp.js";function hg(e){if(!e)return;let t={...process.env,...e},n=Object.entries(t).filter(r=>typeof r[1]=="string");return Object.fromEntries(n)}function ba(){return new pg({name:"memo-code-cli-client",version:"1.0.0"},{capabilities:{}})}function yg(e){if(!(!e||Object.keys(e).length===0))return{headers:e}}function Sg(e){let t={...e.http_headers??e.headers??{}};if(e.bearer_token_env_var){let n=process.env[e.bearer_token_env_var];n&&!t.Authorization&&(t.Authorization=`Bearer ${n}`)}return t}async function _g(e,t,n){let r=new URL(t.url),o=yg(Sg(t)),s=await oi({serverName:e,config:t,settings:n});try{let i=ba(),c=new dg(r,{requestInit:o,...s?{authProvider:s}:{}});return await i.connect(c),{client:i,transport:c}}catch(i){let c=Tg(i)?` Run "memo mcp login ${e}".`:"",a=`Failed to connect via streamable_http (${i.message}).${c}`,l=new Error(a);throw l.cause=i,l}}async function vg(e,t,n){if("url"in t)return _g(e,t,n);let r={command:t.command,args:t.args,env:hg(t.env),stderr:t.stderr??(process.stdout.isTTY&&process.stdin.isTTY?"ignore":void 0)},o=new mg(r),s=ba();return await s.connect(o),{client:s,transport:o}}var Un=class{connections=new Map;pendingConnections=new Map;serverConfigs=new Map;oauthSettings;setServerConfigs(t,n){this.serverConfigs=new Map(Object.entries(t)),this.oauthSettings=n}hasServer(t){return this.connections.has(t)||this.serverConfigs.has(t)}async connect(t,n){n&&this.serverConfigs.set(t,n);let r=this.connections.get(t);if(r)return r;let o=this.pendingConnections.get(t);if(o)return o;let s=n??this.serverConfigs.get(t);if(!s)throw new Error(`MCP server config not found: ${t}`);let i=(async()=>{let{client:c,transport:a}=await vg(t,s,this.oauthSettings);try{let l=await c.listTools(),p={name:t,client:c,transport:a,tools:(l.tools||[]).map(u=>({name:`${t}_${u.name}`,description:u.description||`Tool from ${t}: ${u.name}`,source:"mcp",serverName:t,originalName:u.name,inputSchema:u.inputSchema,execute:async()=>({content:[]})}))};return this.connections.set(t,p),p}catch(l){try{await c.close()}catch{}throw l}})();this.pendingConnections.set(t,i);try{return await i}finally{this.pendingConnections.delete(t)}}get(t){return this.connections.get(t)}getAll(){return Array.from(this.connections.values())}getKnownServerNames(){let t=new Set([...Array.from(this.serverConfigs.keys()),...Array.from(this.connections.keys())]);return Array.from(t.values())}getAllTools(){let t=[];for(let n of this.connections.values())for(let r of n.tools)t.push({name:r.name,description:r.description,serverName:r.serverName,originalName:r.originalName,inputSchema:r.inputSchema,client:n.client});return t}async closeAll(){let t=Array.from(this.connections.values()).map(async n=>{try{await n.client.close()}catch(r){console.error(`[MCP] Error closing client ${n.name}:`,r)}});await Promise.all(t),this.connections.clear(),this.pendingConnections.clear()}get size(){return this.connections.size}};function Tg(e){if(e instanceof fg)return!0;if(e instanceof gg)return e.code===401||e.code===403;let t=e?.message?.toLowerCase()??"";return t.includes("unauthorized")||t.includes("401")||t.includes("403")||t.includes("oauth")}var Hn=class{pool;serverToolNames=new Map;refreshPromises=new Map;tools=new Map;cacheStore=On();shouldLog;constructor(){this.pool=new Un,co(this.pool),lo(this.cacheStore),this.shouldLog=!(process.stdout.isTTY&&process.stdin.isTTY)}buildTool(t,n,r){return{name:`${t}_${r.originalName}`,description:r.description||`Tool from ${t}: ${r.originalName}`,source:"mcp",serverName:t,originalName:r.originalName,inputSchema:r.inputSchema??{},execute:async o=>(await this.pool.connect(t,n)).client.callTool({name:r.originalName,arguments:o})}}replaceServerTools(t,n){let r=this.serverToolNames.get(t);if(r)for(let s of r)this.tools.delete(s);let o=new Set;for(let s of n)this.tools.set(s.name,s),o.add(s.name);this.serverToolNames.set(t,o)}connectionToDescriptors(t,n){return n.tools.map(r=>({originalName:r.originalName,description:r.description||`Tool from ${t}: ${r.originalName}`,inputSchema:r.inputSchema}))}async refreshServer(t,n,r){let o=this.refreshPromises.get(t);if(o){r==="sync"&&await o;return}let s=(async()=>{try{let i=await this.pool.connect(t,n),c=this.connectionToDescriptors(t,i);await this.cacheStore.setServerTools(t,n,c);let a=c.map(l=>this.buildTool(t,n,l));this.replaceServerTools(t,a),this.shouldLog&&r==="background"&&console.log(`[MCP] Refreshed '${t}' tools in background (${a.length})`)}catch(i){this.shouldLog&&console.error(`[MCP] Failed to refresh server '${t}':`,i)}})();this.refreshPromises.set(t,s),s.finally(()=>{this.refreshPromises.delete(t)}),r==="sync"&&await s}removeToolsForMissingServers(t){for(let[n,r]of this.serverToolNames.entries())if(!t.has(n)){for(let o of r)this.tools.delete(o);this.serverToolNames.delete(n)}}async loadServers(t){return this.loadServersWithOptions(t)}async loadServersWithOptions(t,n){if(!t||Object.keys(t).length===0)return 0;let r=Object.entries(t);this.pool.setServerConfigs(t,n),this.removeToolsForMissingServers(new Set(r.map(([s])=>s)));let o=[];for(let[s,i]of r){let c=await this.cacheStore.getServerTools(s,i);if(c){let a=c.tools.map(l=>this.buildTool(s,i,l));this.replaceServerTools(s,a),this.shouldLog&&console.log(`[MCP] Loaded ${a.length} cached tools for '${s}' (${c.stale?"stale":"fresh"})`),c.stale&&this.refreshServer(s,i,"background");continue}o.push(this.refreshServer(s,i,"sync"))}return await Promise.all(o),this.tools.size}get(t){return this.tools.get(t)}getAll(){return Array.from(this.tools.values())}toRegistry(){let t={};for(let[n,r]of this.tools)t[n]=r;return t}has(t){return this.tools.has(t)}get size(){return this.tools.size}async dispose(){await this.pool.closeAll(),this.tools.clear(),this.serverToolNames.clear(),this.refreshPromises.clear(),co(null),lo(null)}getPool(){return this.pool}};var Fn=class{nativeRegistry;mcpRegistry;constructor(){this.nativeRegistry=new Dn,this.mcpRegistry=new Hn}registerNativeTool(t){this.nativeRegistry.register(t)}registerNativeTools(t){for(let n of t)this.registerNativeTool(n)}async loadMcpServers(t,n){return this.mcpRegistry.loadServersWithOptions(t,n)}getTool(t){return this.nativeRegistry.get(t)??this.mcpRegistry.get(t)}getAllTools(){return[...this.nativeRegistry.getAll(),...this.mcpRegistry.getAll()]}toRegistry(){return{...this.nativeRegistry.toRegistry(),...this.mcpRegistry.toRegistry()}}hasTool(t){return this.nativeRegistry.has(t)||this.mcpRegistry.has(t)}getToolCount(){let t=this.nativeRegistry.size,n=this.mcpRegistry.size;return{native:t,mcp:n,total:t+n}}async execute(t,n){let r=this.getTool(t);if(!r)throw new Error(`Tool '${t}' not found`);return r.execute(n)}generateToolDefinitions(){return this.getAllTools().map(t=>({name:t.name,description:t.description,input_schema:t.inputSchema||{type:"object",properties:{}}}))}generateToolDescriptions(){let t=this.getAllTools();if(t.length===0)return"";let n=[];n.push("## Available Tools"),n.push("");let r=t.filter(s=>s.source==="native"),o=t.filter(s=>s.source==="mcp");if(r.length>0){n.push("### Built-in Tools"),n.push("");for(let s of r)n.push(this.formatToolDescription(s));n.push("")}if(o.length>0){n.push("### External MCP Tools"),n.push("");let s=this.groupByServer(o);for(let[i,c]of Object.entries(s)){n.push(`**Server: ${i}**`),n.push("");for(let a of c)n.push(this.formatToolDescription(a));n.push("")}}return n.join(`
|
|
69
78
|
`)}formatToolDescription(t){let n=[];return n.push(`#### ${t.name}`),n.push(`- **Description**: ${t.description}`),t.inputSchema&&Object.keys(t.inputSchema).length>0&&n.push(`- **Input Schema**: ${JSON.stringify(t.inputSchema)}`),n.join(`
|
|
70
|
-
`)}groupByServer(t){let n={};for(let r of t)if(r.source==="mcp"){let o=r.serverName;n[o]||(n[o]=[]),n[o].push(r)}return n}getToolDescriptions(){return this.getAllTools().map(t=>({name:t.name,description:t.description,source:t.source,serverName:t.source==="mcp"?t.serverName:void 0,inputSchema:t.inputSchema}))}async dispose(){await this.mcpRegistry.dispose()}};function
|
|
79
|
+
`)}groupByServer(t){let n={};for(let r of t)if(r.source==="mcp"){let o=r.serverName;n[o]||(n[o]=[]),n[o].push(r)}return n}getToolDescriptions(){return this.getAllTools().map(t=>({name:t.name,description:t.description,source:t.source,serverName:t.source==="mcp"?t.serverName:void 0,inputSchema:t.inputSchema}))}async dispose(){await this.mcpRegistry.dispose()}};function wg(e){let t=process.env[e]?.trim();return t?new Set(t.split(",").map(n=>n.trim()).filter(Boolean)):new Set}function Cg(){let e=[],t=process.env.MEMO_SHELL_TOOL_TYPE?.trim()||"unified_exec",n=wg("MEMO_EXPERIMENTAL_TOOLS"),r=n.size===0,o=process.env.MEMO_ENABLE_COLLAB_TOOLS!=="0",s=process.env.MEMO_ENABLE_MEMORY_TOOL!=="0";return t==="shell"?e.push(Si):t==="shell_command"?e.push(_i):t==="unified_exec"?e.push(Vr,Xr):t!=="disabled"&&e.push(Vr,Xr),e.push(Ji,Yi,Qi),e.push(ea),e.push(Oi),(r||n.has("grep_files"))&&e.push(Ui),(r||n.has("read_file"))&&e.push($i),(r||n.has("list_dir"))&&e.push(Di),s&&e.push(ra),e.push(ia),o&&e.push(pa,da,ma,fa,ga),e}function bg(e){let t={};for(let n of e)t[n.name]=n;return t}var xg=bg(Cg()),Mg=Object.values(xg),xa=Mg;import Ng from"openai";import{encoding_for_model as kg,get_encoding as Ag}from"@dqbd/tiktoken";var Ma="cl100k_base";function Pg(e){let t=e?.trim()||Ma;try{let n=()=>kg(t);return n().free(),{model:t,factory:n}}catch{let n=Ma,r=()=>Ag(n);return r().free(),{model:n,factory:r}}}function Eg(e){if(e.role==="assistant"){let t=e.reasoning_content?`
|
|
71
80
|
${e.reasoning_content}`:"";return e.tool_calls?.length?`${e.content}${t}
|
|
72
81
|
${JSON.stringify(e.tool_calls)}`:`${e.content}${t}`}return e.role==="tool"?`${e.content}
|
|
73
82
|
${e.tool_call_id}
|
|
74
|
-
${e.name??""}`:e.content}function
|
|
83
|
+
${e.name??""}`:e.content}function ka(e){let{model:t,factory:n}=Pg(e),r=n(),o=4,s=2,i=1,c=l=>l?r.encode(l).length:0;return{model:t,countText:c,countMessages:l=>{if(!l.length)return 0;let p=0;for(let u of l)p+=o,p+=c(Eg(u)),u.name&&(p+=i);return p+=s,p},dispose:()=>r.free()}}import"openai";var Rg={supportsParallelToolCalls:!1,supportsReasoningContent:!1};function wo(e){return e.trim().toLowerCase()}function Ig(e,t,n){if(!n)return;let r=new Map;for(let[s,i]of Object.entries(n))r.set(wo(s),i);let o=r.get(`${e}:${t}`);return o||r.get(t)}function Og(e,t){if(!t)return{capabilities:e,usedOverride:!1};let n={...e},r=!1;return typeof t.supports_parallel_tool_calls=="boolean"&&(n.supportsParallelToolCalls=t.supports_parallel_tool_calls,r=!0),typeof t.supports_reasoning_content=="boolean"&&(n.supportsReasoningContent=t.supports_reasoning_content,r=!0),typeof t.context_window=="number"&&Number.isFinite(t.context_window)&&t.context_window>0&&(n.contextWindow=Math.floor(t.context_window),r=!0),{capabilities:n,usedOverride:r}}function Aa(e,t){let n=wo(e.name),r=wo(e.model),{capabilities:o,usedOverride:s}=Og(Rg,Ig(n,r,t));return{profile:{wireApi:"chat_completions",...o,isFallback:!s}}}function Lg(e){if(e.length!==0)return e.map(t=>({type:"function",function:{name:t.name,description:t.description,parameters:t.input_schema}}))}function Pa(e){let t=Lg(e.toolDefinitions),n={model:e.model,messages:e.messages,tools:t,tool_choice:t?"auto":void 0};return t&&e.profile.supportsParallelToolCalls&&(n.parallel_tool_calls=!0),n}function $g(e,t){if(!e||!t)return e;let n=new Set(t.map(o=>o.trim()).filter(Boolean));if(n.size===0)return{};let r={};for(let[o,s]of Object.entries(e))n.has(o)&&(r[o]=s);return r}function Dg(e){try{return{ok:!0,data:JSON.parse(e)}}catch(t){return{ok:!1,raw:e,error:t.message}}}function Ug(e){if(e.role==="assistant"){let t={role:"assistant",content:e.content,tool_calls:e.tool_calls?.map(n=>({id:n.id,type:n.type,function:{name:n.function.name,arguments:n.function.arguments}}))};return e.reasoning_content&&(t.reasoning_content=e.reasoning_content),t}return e.role==="tool"?{role:"tool",content:e.content,tool_call_id:e.tool_call_id}:{role:e.role,content:e.content}}function Hg(e){let t=e?.reasoning_content;if(typeof t!="string")return;let n=t.trim();return n.length>0?n:void 0}function Fg(e){return!e||typeof e!="object"?!1:Array.isArray(e.choices)}async function Ea(e,t,n){let r=await te(),o=r.config,s=new Fn;if(s.registerNativeTools(xa),await s.loadMcpServers($g(o.mcp_servers,t.activeMcpServers),{memoHome:r.home,storeMode:o.mcp_oauth_credentials_store_mode,callbackPort:o.mcp_oauth_callback_port}),e.tools)for(let[d,m]of Object.entries(e.tools))s.registerNativeTool({name:d,description:m.description,source:"native",inputSchema:{type:"object"},execute:m.execute});let i=s.toRegistry(),c=async()=>{let d=e.loadPrompt?await e.loadPrompt():await us({cwd:t.cwd,memoHome:r.home,activeSkillPaths:o.active_skills}),m=s.generateToolDescriptions();return m&&(d+=`
|
|
75
84
|
|
|
76
|
-
${m}`),d},a=s.generateToolDefinitions(),l=
|
|
77
|
-
`),{thinkingParts:n,cleaned:r}
|
|
85
|
+
${m}`),d},a=s.generateToolDefinitions(),l=Kt(r,t),p=Ws(l,n),u=new mn(p);return{tools:i,dispose:async()=>{e.dispose&&await e.dispose(),await s.dispose()},callLLM:e.callLLM??(async(d,m,g)=>{let y=ut(o,t.providerName),b=process.env[y.env_api_key]??process.env.OPENAI_API_KEY??process.env.DEEPSEEK_API_KEY;if(!b)throw new Error(`Missing env var ${y.env_api_key} (or OPENAI_API_KEY/DEEPSEEK_API_KEY)`);let U=new Ng({apiKey:b,baseURL:y.base_url}),T=d.map(Ug),{profile:F}=Aa(y,o.model_profiles),R=g?.tools??a,I=Pa({model:y.model,messages:T,toolDefinitions:R,profile:F}),G=await U.chat.completions.create(I,{signal:g?.signal});if(!Fg(G))throw new Error("Streaming response is not supported in core callLLM");let ie=G.choices?.[0]?.message,Re=Hg(ie);if(ie?.tool_calls&&ie.tool_calls.length>0){let z=[];ie.content&&z.push({type:"text",text:ie.content});for(let P of ie.tool_calls)if(P.type==="function"){let ne=Dg(P.function.arguments);ne.ok?z.push({type:"tool_use",id:P.id,name:P.function.name,input:ne.data}):z.push({type:"text",text:`[tool_use parse error] ${ne.error}; raw: ${ne.raw}`})}let M=z.some(P=>P.type==="tool_use");return{content:z,reasoning_content:Re,stop_reason:M?"tool_use":"end_turn",usage:{prompt:G.usage?.prompt_tokens??void 0,completion:G.usage?.completion_tokens??void 0,total:G.usage?.total_tokens??void 0}}}let ae=ie?.content;if(typeof ae!="string")throw new Error("OpenAI-compatible API returned empty content");return{content:[{type:"text",text:ae}],reasoning_content:Re,stop_reason:"end_turn",usage:{prompt:G.usage?.prompt_tokens??void 0,completion:G.usage?.completion_tokens??void 0,total:G.usage?.total_tokens??void 0}}}),loadPrompt:c,historySinks:e.historySinks??[u],tokenCounter:e.tokenCounter??ka(t.tokenizerModel),historyFilePath:p}}function Bg(e){let t=[],n=/<\s*(think|thinking)\s*>([\s\S]*?)<\/\s*\1\s*>/gi,r=e.replace(n,(o,s,i)=>{let c=(i??"").trim();return c&&t.push(c),c});return{thinkingParts:t,cleaned:r.trim()}}function Ra(e){if(e.length===0)return;let t=e.join(`
|
|
86
|
+
`),{thinkingParts:n,cleaned:r}=Bg(t);return n.length>0?n.join(`
|
|
78
87
|
|
|
79
|
-
`):r||void 0}import{randomUUID as
|
|
80
|
-
`)}function
|
|
81
|
-
`),toolUseBlocks:n.map(r=>({id:r.id,name:r.name,input:r.input})),reasoningContent:typeof e.reasoning_content=="string"&&e.reasoning_content.trim().length>0?e.reasoning_content:void 0,stopReason:e.stop_reason,usage:e.usage}}async function
|
|
88
|
+
`):r||void 0}import{randomUUID as Qg}from"crypto";var Bn="interactive";var bo="success",Mt="Tool usage is disabled in the current permission mode. Switch to /tools once or /tools full to enable tools.";var jg="Skipped tool execution after previous rejection.",Ia="Tool execution skipped: tools are disabled in current permission mode.";function Wg(e){process.stderr.write(`${JSON.stringify(e)}
|
|
89
|
+
`)}function Oa(e){if(e.toolPermissionMode==="none")return{mode:"none",toolsDisabled:!0,dangerous:!1,approvalMode:"auto"};if(e.toolPermissionMode==="once")return{mode:"once",toolsDisabled:!1,dangerous:!1,approvalMode:"auto"};if(e.toolPermissionMode==="full")return{mode:"full",toolsDisabled:!1,dangerous:!0,approvalMode:"auto"};let t=e.dangerous??!1;return{mode:t?"full":"auto",toolsDisabled:!1,dangerous:t,approvalMode:"auto"}}function xo(){return{prompt:0,completion:0,total:0}}function Mo(e,t){if(!t)return;let n=t.prompt??0,r=t.completion??0,o=t.total??n+r;e.prompt+=n,e.completion+=r,e.total+=o}function ko(e){let t=e.content.filter(r=>r.type==="text"),n=e.content.filter(r=>r.type==="tool_use");return{textContent:t.map(r=>r.text).join(`
|
|
90
|
+
`),toolUseBlocks:n.map(r=>({id:r.id,name:r.name,input:r.input})),reasoningContent:typeof e.reasoning_content=="string"&&e.reasoning_content.trim().length>0?e.reasoning_content:void 0,stopReason:e.stop_reason,usage:e.usage}}async function La(e,t){for(let n of t)try{await n.append(e)}catch(r){Wg({level:"error",event:"history_sink_append_failed",sink:n.constructor?.name||"anonymous_sink",message:r.message})}}function Na(e){if(!(e instanceof Error))return!1;if(e.name==="AbortError")return!0;let t=e.message?.toLowerCase?.()??"";return t.includes("request was aborted")||t.includes("operation was aborted")||t.includes("aborted")}function jn(e){return Co(e,new WeakSet,0)}var zg=100;function Co(e,t,n){if(n>zg)return JSON.stringify("[MaxDepthExceeded]");if(typeof e=="bigint")return JSON.stringify(e.toString());if(e===null||typeof e!="object")return JSON.stringify(e)??"null";if(t.has(e))return JSON.stringify("[Circular]");if(t.add(e),Array.isArray(e)){let s=`[${e.map(i=>Co(i,t,n+1)).join(",")}]`;return t.delete(e),s}let o=`{${Object.entries(e).sort(([s],[i])=>s.localeCompare(i)).map(([s,i])=>`${JSON.stringify(s)}:${Co(i,t,n+1)}`).join(",")}}`;return t.delete(e),o}function $a(e){return e.map(t=>({id:t.id,type:"function",function:{name:t.name,arguments:jn(t.input)}}))}function Da(e,t){let n=e.trim();if(!n)return null;let r=[n],o=n.match(/^```(?:json)?\s*([\s\S]*?)\s*```$/i);o?.[1]&&r.push(o[1].trim());for(let s of r)if(!(!s.startsWith("{")||!s.endsWith("}")))try{let i=JSON.parse(s);if(!i||typeof i!="object"||Array.isArray(i))continue;let c=i,a=typeof c.tool=="string"?c.tool.trim():"";if(!a||!Object.prototype.hasOwnProperty.call(t,a))continue;return{tool:a,input:c.input??{}}}catch{}return null}function Gg(e){return e.length<=60?e:`${e.slice(0,57).trimEnd()}...`}function Ua(e){let t=e.replace(/\s+/g," ").trim();if(!t)return"New Session";if(!t.includes(" "))return t.length<=20?t:`${t.slice(0,20).trimEnd()}...`;let r=t.split(" ").filter(Boolean).slice(0,8).join(" ");return Gg(r||t)}function Ao(e){return{role:"tool",content:e.observation,tool_call_id:e.actionId,name:e.tool}}function Ha(e,t,n){let r=new Map(t.map(o=>[o.actionId,o]));return e.map(o=>{let s=r.get(o.id);return s||{actionId:o.id,tool:o.name,status:n?"approval_denied":"execution_failed",errorType:n?"approval_denied":"execution_failed",success:!1,observation:n?`${jg} ${o.name}`:`Tool result missing for ${o.name}; execution aborted before producing output.`,durationMs:0,rejected:n?!0:void 0}})}import{randomUUID as Xg}from"crypto";var Fa=`You are performing a CONTEXT CHECKPOINT COMPACTION. Create a handoff summary for another LLM that will resume the task.
|
|
82
91
|
|
|
83
92
|
Include:
|
|
84
93
|
- Current progress and key decisions made
|
|
@@ -86,38 +95,38 @@ Include:
|
|
|
86
95
|
- What remains to be done (clear next steps)
|
|
87
96
|
- Any critical data, examples, or references needed to continue
|
|
88
97
|
|
|
89
|
-
Be concise, structured, and focused on helping the next LLM seamlessly continue the work.`,
|
|
90
|
-
`).trim();return t.length<=4e3?t:`${t.slice(0,4e3)}...`}function
|
|
91
|
-
${
|
|
92
|
-
${
|
|
93
|
-
${
|
|
94
|
-
`)}function
|
|
98
|
+
Be concise, structured, and focused on helping the next LLM seamlessly continue the work.`,Eo="Another language model started to solve this problem and produced a summary of its thinking process. Use this summary to continue the task without redoing completed work.";function Po(e){let t=e.replace(/\r\n/g,`
|
|
99
|
+
`).trim();return t.length<=4e3?t:`${t.slice(0,4e3)}...`}function Kg(e,t){let n=e.role.toUpperCase();if(e.role==="assistant"&&e.tool_calls?.length){let r=e.tool_calls.map(o=>o.function.name).join(", ");return`[${t}] ${n} (tool_calls: ${r})
|
|
100
|
+
${Po(e.content)}`}if(e.role==="tool"){let r=e.name?` (${e.name})`:"";return`[${t}] ${n}${r}
|
|
101
|
+
${Po(e.content)}`}return`[${t}] ${n}
|
|
102
|
+
${Po(e.content)}`}function Ba(e){return e.role!=="user"?!1:e.content.startsWith(`${Eo}
|
|
103
|
+
`)}function ja(e){return["Conversation history to summarize:",e.length?e.map((n,r)=>Kg(n,r)).join(`
|
|
95
104
|
|
|
96
105
|
`):"(empty)","","Return only the summary body in plain text. Do not add markdown fences."].join(`
|
|
97
|
-
`)}function
|
|
98
|
-
${t}`};return n?[n,...s,i]:[...s,i]}selectCompactionUserMessages(t){if(!t.length)return[];let n=[],r=
|
|
106
|
+
`)}function qg(){return{onTurnStart:[],onContextUsage:[],onContextCompacted:[],onAction:[],onObservation:[],onFinal:[],onApprovalRequest:[],onApprovalResponse:[],onTitleGenerated:[]}}function Wa(e,t){t&&(t.onTurnStart&&e.onTurnStart.push(t.onTurnStart),t.onContextUsage&&e.onContextUsage.push(t.onContextUsage),t.onContextCompacted&&e.onContextCompacted.push(t.onContextCompacted),t.onAction&&e.onAction.push(t.onAction),t.onObservation&&e.onObservation.push(t.onObservation),t.onFinal&&e.onFinal.push(t.onFinal),t.onApprovalRequest&&e.onApprovalRequest.push(t.onApprovalRequest),t.onApprovalResponse&&e.onApprovalResponse.push(t.onApprovalResponse),t.onTitleGenerated&&e.onTitleGenerated.push(t.onTitleGenerated))}function za(e){let t=qg();if(Wa(t,e.hooks),Array.isArray(e.middlewares))for(let n of e.middlewares)Wa(t,n);return t}async function V(e,t,n){let r=e[t];if(r.length)for(let o of r)try{await o(n)}catch(s){console.warn(`Hook ${t} failed: ${s.message}`)}}function kt(e){return e.map(t=>t.role==="assistant"&&t.tool_calls?.length?{...t,tool_calls:t.tool_calls.map(n=>({...n,function:{...n.function}}))}:{...t})}var Jg=80,Yg=2e4,Wn=class{constructor(t,n,r,o,s){this.deps=t;this.options=n;this.id=n.sessionId||Xg(),this.mode=n.mode||Bn,this.history=[{role:"system",content:r}],this.tokenCounter=o,this.sinks=t.historySinks??[],this.hooks=za(t),this.historyFilePath=s;let i=Oa(n);this.toolsDisabled=i.toolsDisabled,this.toolPermissionMode=i.mode,this.toolOrchestrator=Ca({tools:t.tools,approval:{dangerous:i.dangerous,mode:i.approvalMode}})}title;id;mode;history;historyFilePath;turnIndex=0;tokenCounter;sinks;sessionUsage=xo();startedAt=Date.now();hooks;closed=!1;sessionStartEmitted=!1;currentAbortController=null;cancelling=!1;lastActionSignature=null;repeatedActionCount=0;toolOrchestrator;toolsDisabled=!1;toolPermissionMode="auto";async init(){}resetActionRepetition(){this.lastActionSignature=null,this.repeatedActionCount=0}maybeWarnRepeatedAction(t,n){let r=`${t}:${jn(n)}`;if(this.lastActionSignature===r?this.repeatedActionCount+=1:(this.lastActionSignature=r,this.repeatedActionCount=1),this.repeatedActionCount===3){let o=jn(n).slice(0,200),s=`\u7CFB\u7EDF\u63D0\u9192\uFF1A\u4F60\u5DF2\u8FDE\u7EED3\u6B21\u8C03\u7528\u540C\u4E00\u5DE5\u5177\u300C${t}\u300D\u4E14\u53C2\u6570\u76F8\u540C\uFF08${o}${o.length>=200?"\u2026":""}\uFF09\u3002\u8BF7\u786E\u8BA4\u662F\u5426\u9677\u5165\u5FAA\u73AF\uFF0C\u5FC5\u8981\u65F6\u76F4\u63A5\u7ED9\u51FA\u6700\u7EC8\u56DE\u7B54\u6216\u8C03\u6574\u53C2\u6570\u3002`;this.history.push({role:"system",content:s})}}resolveContextWindow(){let t=this.options.contextWindow;return typeof t=="number"&&Number.isFinite(t)&&t>0?Math.floor(t):12e4}resolveSessionCwd(){let t=this.options.cwd?.trim();return t||process.cwd()}resolveAutoCompactThresholdPercent(){let t=this.options.autoCompactThresholdPercent;return typeof t=="number"&&Number.isInteger(t)&&Number.isFinite(t)&&t>=1&&t<=100?t:Jg}resolveThresholdTokens(t){let n=Math.floor(t*this.resolveAutoCompactThresholdPercent()/100);return Math.max(1,n)}calculateUsagePercent(t,n){return t<=0||n<=0?0:Math.round(t/n*1e4)/100}async emitContextUsage(t,n,r,o,s,i){let c=this.calculateUsagePercent(r,o);await this.emitEvent("context_usage",{turn:t,step:n,meta:{phase:i,prompt_tokens:r,context_window:o,threshold_tokens:s,usage_percent:c}}),await V(this.hooks,"onContextUsage",{sessionId:this.id,turn:t,step:n,promptTokens:r,contextWindow:o,thresholdTokens:s,usagePercent:c,phase:i})}async emitContextCompacted(t,n,r){await this.emitEvent("context_compact",{turn:t,step:n,content:r.summary,meta:{reason:r.reason,status:r.status,before_tokens:r.beforeTokens,after_tokens:r.afterTokens,threshold_tokens:r.thresholdTokens,reduction_percent:r.reductionPercent,error_message:r.errorMessage}}),await V(this.hooks,"onContextCompacted",{sessionId:this.id,turn:t,step:n,reason:r.reason,status:r.status,beforeTokens:r.beforeTokens,afterTokens:r.afterTokens,thresholdTokens:r.thresholdTokens,reductionPercent:r.reductionPercent,summary:r.summary,errorMessage:r.errorMessage})}buildCompactedHistory(t){let n=this.history[0]?.role==="system"?this.history[0]:void 0,o=(n?this.history.slice(1):this.history).filter(c=>c.role==="user"&&!Ba(c)).map(c=>c.content),s=this.selectCompactionUserMessages(o).map(c=>({role:"user",content:c})),i={role:"user",content:`${Eo}
|
|
107
|
+
${t}`};return n?[n,...s,i]:[...s,i]}selectCompactionUserMessages(t){if(!t.length)return[];let n=[],r=Yg;for(let o=t.length-1;o>=0;o-=1){let s=t[o];if(!s)continue;let i=this.tokenCounter.countText(s);if(i<=r){if(n.push(s),r=Math.max(0,r-i),r===0)break;continue}r>0&&n.push(s.slice(0,r));break}return n.reverse(),n}normalizeCompactionSummary(t){return(t.replace(/<think>[\s\S]*?<\/think>/gi,"").trim()||t).replace(/\n{3,}/g,`
|
|
99
108
|
|
|
100
|
-
`).trim()}async compactHistoryInternal(t,n,r){let o=this.resolveContextWindow(),s=this.resolveThresholdTokens(o),i=this.tokenCounter.countMessages(this.history),a=(this.history[0]?.role==="system"?this.history[0]:void 0)?this.history.slice(1):this.history.slice();if(!a.length){let l={reason:t,status:"skipped",beforeTokens:i,afterTokens:i,thresholdTokens:s,reductionPercent:0};return await this.emitContextCompacted(n,r,l),l}try{let l=await this.deps.callLLM([{role:"system",content:Fa},{role:"user",content:
|
|
109
|
+
`).trim()}async compactHistoryInternal(t,n,r){let o=this.resolveContextWindow(),s=this.resolveThresholdTokens(o),i=this.tokenCounter.countMessages(this.history),a=(this.history[0]?.role==="system"?this.history[0]:void 0)?this.history.slice(1):this.history.slice();if(!a.length){let l={reason:t,status:"skipped",beforeTokens:i,afterTokens:i,thresholdTokens:s,reductionPercent:0};return await this.emitContextCompacted(n,r,l),l}try{let l=await this.deps.callLLM([{role:"system",content:Fa},{role:"user",content:ja(a)}],void 0,{tools:[]}),p=ko(l),u=this.normalizeCompactionSummary(p.textContent);if(!u)throw new Error("Compaction model returned an empty summary.");let d=this.buildCompactedHistory(u),m=this.tokenCounter.countMessages(d);this.history.splice(0,this.history.length,...d);let g=i>0?Math.max(0,Math.round((i-m)/i*1e4)/100):0,y={reason:t,status:"success",beforeTokens:i,afterTokens:m,thresholdTokens:s,reductionPercent:g,summary:u};return await this.emitContextCompacted(n,r,y),y}catch(l){let p={reason:t,status:"failed",beforeTokens:i,afterTokens:i,thresholdTokens:s,reductionPercent:0,errorMessage:l.message};return await this.emitContextCompacted(n,r,p),p}}buildToolApprovalHooks(t,n){return{onApprovalRequest:async r=>{await V(this.hooks,"onApprovalRequest",{sessionId:this.id,turn:t,step:n,request:r})},requestApproval:async r=>this.deps.requestApproval?this.deps.requestApproval(r):"deny",onApprovalResponse:async({fingerprint:r,decision:o})=>{await V(this.hooks,"onApprovalResponse",{sessionId:this.id,turn:t,step:n,fingerprint:r,decision:o})}}}async executeToolAction(t,n,r,o,s){return this.toolOrchestrator.executeAction({id:t,name:n,input:r},this.buildToolApprovalHooks(o,s))}async maybeGenerateSessionTitle(t,n){if(t!==1||this.title)return;let r=Ua(n);this.title=r,await this.emitEvent("session_title",{turn:t,content:r,meta:{source:"first_prompt",original_prompt:n}}),await V(this.hooks,"onTitleGenerated",{sessionId:this.id,turn:t,title:r,originalPrompt:n})}async runTurn(t){return di({cwd:this.resolveSessionCwd()},async()=>{let n=new AbortController;this.currentAbortController=n,this.cancelling=!1,this.turnIndex+=1;let r=this.turnIndex,o=[],s=xo(),i=Date.now(),c=this.resolveContextWindow(),a=this.resolveThresholdTokens(c),l=this.resolveAutoCompactThresholdPercent(),p=!1;if(!this.sessionStartEmitted){let u=this.history[0]?.role==="system"?this.history[0].content:void 0;await this.emitEvent("session_start",{content:u,role:u?"system":void 0,meta:{mode:this.mode,cwd:this.resolveSessionCwd(),tokenizer:this.tokenCounter.model,warnPromptTokens:this.options.warnPromptTokens,contextWindow:c,autoCompactThresholdPercent:l,toolPermissionMode:this.toolPermissionMode}}),this.sessionStartEmitted=!0}this.history.push({role:"user",content:t});try{let u=this.tokenCounter.countMessages(this.history);await this.emitEvent("turn_start",{turn:r,content:t,meta:{tokens:{prompt:u}}}),await V(this.hooks,"onTurnStart",{sessionId:this.id,turn:r,input:t,promptTokens:u,history:kt(this.history)}),await this.emitContextUsage(r,0,u,c,a,"turn_start"),await this.maybeGenerateSessionTitle(r,t);let d="",m="ok",g,y=0,b=null,U=-1;for(let T=0;;T++){let F=this.tokenCounter.countMessages(this.history);if(await this.emitContextUsage(r,T,F,c,a,"step_start"),!p&&F>=a&&(p=!0,await this.compactHistoryInternal("auto",r,T),F=this.tokenCounter.countMessages(this.history),await this.emitContextUsage(r,T,F,c,a,"post_compact")),F>c){let A=`Context tokens (${F}) exceed the limit. Please shorten the input or restart the session.`;this.history.push({role:"assistant",content:A}),m="prompt_limit",d=A,g=A,await this.emitEvent("final",{turn:r,step:T,content:A,role:"assistant",meta:{tokens:{prompt:F}}}),await V(this.hooks,"onFinal",{sessionId:this.id,turn:r,step:T,finalText:A,status:m,errorMessage:g,turnUsage:{...s},steps:o});break}this.options.warnPromptTokens&&F>this.options.warnPromptTokens&&console.warn(`Prompt tokens are near the limit: ${F}`);let R="",I=[],G,ie,Re,ae=!1;try{let A=await this.deps.callLLM(this.history,re=>{re&&(ae=!0),this.deps.onAssistantStep?.(re,T)},{signal:n.signal}),O=ko(A);R=O.textContent,I=O.toolUseBlocks,ie=O.stopReason,G=O.usage,Re=O.reasoningContent,R.trim().length>0&&(b=R,U=T)}catch(A){if(this.cancelling&&Na(A)){m="cancelled",d="",g="Turn cancelled",await this.emitEvent("final",{turn:r,step:T,content:"",role:"assistant",meta:{cancelled:!0}}),await V(this.hooks,"onFinal",{sessionId:this.id,turn:r,step:T,finalText:d,status:m,errorMessage:g,turnUsage:{...s},steps:o});break}let O=`LLM call failed: ${A.message}`;this.history.push({role:"assistant",content:O}),m="error",d=O,g=O,await this.emitEvent("final",{turn:r,content:O,role:"assistant"}),await V(this.hooks,"onFinal",{sessionId:this.id,turn:r,step:T,finalText:d,status:m,errorMessage:g,turnUsage:{...s},steps:o});break}!ae&&R&&this.deps.onAssistantStep?.(R,T);let z=I.length===0&&R?Da(R,this.deps.tools):null,M,P=null;if(I.length>0){let A=I[0];if(A){let O=R?Ra([R]):void 0;M={action:{tool:A.name,input:A.input},thinking:O},P={role:"assistant",content:R,reasoning_content:Re,tool_calls:$a(I)}}else M={}}else R?(M={final:R},P={role:"assistant",content:R,reasoning_content:Re}):M={};let ne=this.tokenCounter.countText(R),ce=G?.prompt??F,H=G?.completion??ne,le=G?.total??ce+H,J={prompt:ce,completion:H,total:le};if(Mo(s,J),Mo(this.sessionUsage,J),o.push({index:T,assistantText:R,parsed:M,tokenUsage:J}),await this.emitEvent("assistant",{turn:r,step:T,content:R,role:"assistant",meta:{tokens:J,protocol_violation:!!z,protocol_violation_count:z?y+1:y||void 0}}),z){y+=1;let A=`Model protocol error: returned plain-text tool JSON for "${z.tool}" ${y} times. Structured tool calls are required.`;m="error",d=A,g=A,this.history.push({role:"assistant",content:A}),await this.emitEvent("final",{turn:r,step:T,content:A,role:"assistant",meta:{error_type:"model_protocol_error",tool:z.tool,protocol_violation:!0,protocol_violation_count:y,tokens:J}}),await V(this.hooks,"onFinal",{sessionId:this.id,turn:r,step:T,finalText:d,status:m,errorMessage:g,tokenUsage:J,turnUsage:{...s},steps:o});break}if(P&&this.history.push(P),I.length>0&&this.toolsDisabled){for(let A of I)this.history.push({role:"tool",content:Ia,tool_call_id:A.id,name:A.name});m="error",d=Mt,g=Mt,this.history.push({role:"assistant",content:Mt}),await this.emitEvent("final",{turn:r,step:T,content:Mt,role:"assistant",meta:{error_type:"tool_disabled",tool_count:I.length,tools:I.map(A=>A.name).join(","),tokens:J}}),await V(this.hooks,"onFinal",{sessionId:this.id,turn:r,step:T,finalText:Mt,status:m,errorMessage:g,tokenUsage:J,turnUsage:{...s},steps:o});break}if(I.length>1){for(let k of I)this.maybeWarnRepeatedAction(k.name,k.input);await this.emitEvent("action",{turn:r,step:T,meta:{tools:I.map(k=>k.name),action_ids:I.map(k=>k.id),action_id:I[0]?.id,parallel:!0,phase:"dispatch",thinking:M.thinking,toolBlocks:I.map(k=>({id:k.id,name:k.name,input:k.input}))}});let A=I[0];A&&await V(this.hooks,"onAction",{sessionId:this.id,turn:r,step:T,action:{tool:A.name,input:A.input},parallelActions:I.map(k=>({tool:k.name,input:k.input})),thinking:M.thinking,history:kt(this.history)});let O=I.every(k=>!!this.deps.tools[k.name]?.supportsParallelToolCalls),re=I.some(k=>!!this.deps.tools[k.name]?.isMutating),_e=O&&!re?"parallel":"sequential",ge=await this.toolOrchestrator.executeActions(I.map(k=>({id:k.id,name:k.name,input:k.input})),{...this.buildToolApprovalHooks(r,T),executionMode:_e,failurePolicy:"fail_fast"}),qe=Ha(I,ge.results,ge.hasRejection);for(let[k,L]of qe.entries())this.history.push(Ao(L)),await this.emitEvent("observation",{turn:r,step:T,content:L.observation,meta:{tool:L.tool,index:k,action_id:L.actionId,phase:"result",status:L.status,error_type:L.errorType,duration_ms:L.durationMs,execution_mode:_e}});let De=qe.map(k=>`[${k.tool}]: ${k.observation}`).join(`
|
|
101
110
|
|
|
102
|
-
`),
|
|
103
|
-
`):""}).join("")}function
|
|
111
|
+
`),ot=qe.map(k=>k.status),ve=ot.find(k=>k!==bo)??bo,be=o[o.length-1];if(be&&(be.observation=De),await V(this.hooks,"onObservation",{sessionId:this.id,turn:r,step:T,tool:I.map(k=>k.name).join(", "),observation:De,resultStatus:ve,parallelResultStatuses:ot,history:kt(this.history)}),ge.hasRejection){let k=qe.find(L=>L.rejected);m="cancelled",d="\u7528\u6237\u62D2\u7EDD\u4E86\u5DE5\u5177\u6267\u884C\uFF0C\u5DF2\u505C\u6B62\u5F53\u524D\u64CD\u4F5C\u3002",await this.emitEvent("final",{turn:r,step:T,content:d,role:"assistant",meta:{rejected:!0,phase:"result",action_id:k?.actionId,error_type:k?.errorType??"approval_denied",duration_ms:k?.durationMs}}),await V(this.hooks,"onFinal",{sessionId:this.id,turn:r,step:T,finalText:d,status:m,tokenUsage:J,turnUsage:{...s},steps:o});break}continue}else if(M.action){this.maybeWarnRepeatedAction(M.action.tool,M.action.input);let A=I[0]?.id??`${r}:${T}:single:${M.action.tool}`;await this.emitEvent("action",{turn:r,step:T,meta:{tool:M.action.tool,input:M.action.input,action_id:A,phase:"dispatch",thinking:M.thinking}}),await V(this.hooks,"onAction",{sessionId:this.id,turn:r,step:T,action:M.action,thinking:M.thinking,history:kt(this.history)});let O=await this.executeToolAction(A,M.action.tool,M.action.input,r,T);if(O.rejected){this.history.push(Ao({...O,observation:O.observation||`User denied tool execution: ${M.action.tool}`})),m="cancelled",d="\u7528\u6237\u62D2\u7EDD\u4E86\u5DE5\u5177\u6267\u884C\uFF0C\u5DF2\u505C\u6B62\u5F53\u524D\u64CD\u4F5C\u3002",await this.emitEvent("final",{turn:r,step:T,content:d,role:"assistant",meta:{rejected:!0,phase:"result",action_id:O.actionId,error_type:O.errorType??"approval_denied",duration_ms:O.durationMs}}),await V(this.hooks,"onFinal",{sessionId:this.id,turn:r,step:T,finalText:d,status:m,tokenUsage:J,turnUsage:{...s},steps:o});break}let re=O.observation;this.history.push({role:"tool",content:re,tool_call_id:O.actionId,name:M.action.tool});let _e=o[o.length-1];_e&&(_e.observation=re),await this.emitEvent("observation",{turn:r,step:T,content:re,meta:{tool:M.action.tool,action_id:O.actionId,phase:"result",status:O.status,error_type:O.errorType,duration_ms:O.durationMs}}),await V(this.hooks,"onObservation",{sessionId:this.id,turn:r,step:T,tool:M.action.tool,observation:re,resultStatus:O.status,history:kt(this.history)});continue}if(ie==="end_turn"||M.final){this.resetActionRepetition();let A=ie==="end_turn"&&!M.final&&R.trim().length===0&&!!b&&U===T-1;d=A?b??"":M.final||R,M.final&&(M.final=d),await this.emitEvent("final",{turn:r,step:T,content:d,role:"assistant",meta:{tokens:J,fallback_from_previous_text:A||void 0}}),await V(this.hooks,"onFinal",{sessionId:this.id,turn:r,step:T,finalText:d,status:m,tokenUsage:J,turnUsage:{...s},steps:o});break}this.resetActionRepetition();break}return!d&&m!=="cancelled"&&(m==="ok"&&(m="error"),d="Unable to produce a final answer. Please retry or adjust the request.",g=d,this.history.push({role:"assistant",content:d}),await this.emitEvent("final",{turn:r,content:d,role:"assistant"}),await V(this.hooks,"onFinal",{sessionId:this.id,turn:r,finalText:d,status:m,errorMessage:g,turnUsage:{...s},steps:o})),await this.emitEvent("turn_end",{turn:r,meta:{status:m,stepCount:o.length,durationMs:Date.now()-i,tokens:s,protocol_violation_count:y||void 0}}),{finalText:d,steps:o,status:m,errorMessage:g,tokenUsage:s}}finally{this.currentAbortController=null,this.cancelling=!1,this.toolOrchestrator.clearOnceApprovals()}})}cancelCurrentTurn(){this.currentAbortController&&(this.cancelling=!0,this.currentAbortController.abort())}async compactHistory(t="manual"){return this.compactHistoryInternal(t,this.turnIndex,0)}listToolNames(){return Object.keys(this.deps.tools)}async close(){if(this.closed)return;if(this.closed=!0,this.sessionStartEmitted||this.turnIndex>=0){await this.emitEvent("session_end",{meta:{durationMs:Date.now()-this.startedAt,tokens:this.sessionUsage}});for(let n of this.sinks)try{n.close?await n.close():n.flush&&await n.flush()}catch(r){console.error(`History flush failed: ${r.message}`)}}this.tokenCounter.dispose(),this.toolOrchestrator.dispose(),this.deps.dispose&&await this.deps.dispose()}async emitEvent(t,n){if(!this.sinks.length)return;let r=ps({sessionId:this.id,type:t,turn:n.turn,step:n.step,content:n.content,role:n.role,meta:n.meta});await La(r,this.sinks)}};async function zn(e,t={}){let n=t.sessionId||Qg(),r=await Ea(e,{...t,sessionId:n},n),o=await r.loadPrompt(),s=new Wn({...e,...r},{...t,sessionId:n,mode:t.mode??Bn},o,r.tokenCounter,r.historyFilePath);return await s.init(),s}import{randomUUID as Lt}from"crypto";import{readFile as lS}from"fs/promises";import{useCallback as X,useEffect as Nt,useMemo as sn,useReducer as uS,useRef as ar,useState as ee}from"react";import{Box as Xc,Text as pS,useApp as dS}from"ink";import{memo as yh,useMemo as oc,useRef as sc,useEffect as Sh}from"react";import{Box as nc,Static as _h,Text as Gn}from"ink";import{memo as Lo}from"react";import{Box as Qe,Text as se}from"ink";var ke={PENDING:"pending",EXECUTING:"executing",SUCCESS:"success",ERROR:"error"};import At from"path";var Zg="success",Yt=".";function Ro(e){return e?e===Zg?ke.SUCCESS:ke.ERROR:ke.SUCCESS}function Ga(e){if(e?.length)return e.map(t=>Ro(t))}function Ka(e,t){return!t||t<=0||!e||e<=0?0:Math.min(100,e/t*100)}function Qt(e,t=80){return e.length<=t?e:`${e.slice(0,Math.max(0,t-3))}...`}function qa(e){if(typeof e=="string")return e;try{return JSON.stringify(e)??String(e)}catch{return String(e)}}function Io(e,t){let n=e.trim();if(!n)return e;if(n===Yt)return Yt;if(At.isAbsolute(n)){let r=At.relative(At.resolve(t),n);return r?At.normalize(r):Yt}if(n.startsWith("./")||n.startsWith("../")){let r=At.normalize(n);return!r||r==="."||r==="./"?Yt:r}return n}function Va(e){return e?e===Yt||At.isAbsolute(e)?!0:e.startsWith("./")||e.startsWith("../"):!1}import{memo as ih,useMemo as ah}from"react";import{Box as Le,Text as Ce}from"ink";import{marked as eh}from"marked";var th=/(`[^`\n]+`|\[[^\]]+\]\((?:\\.|[^)])+\)|\*\*[^*\n]+\*\*|__[^_\n]+__|\*[^*\n]+\*|_[^_\n]+_)/g;function Zt(e){return e.map(t=>{let n=t;return typeof n.text=="string"?n.text:typeof n.raw=="string"?n.raw:Array.isArray(n.tokens)?Zt(n.tokens):Array.isArray(n.items)?n.items.map(r=>typeof r.text=="string"?r.text:Array.isArray(r.tokens)?Zt(r.tokens):"").filter(Boolean).join(`
|
|
112
|
+
`):""}).join("")}function nh(e){let t=[],n=e.replace(/<think>([\s\S]*?)<\/think>/gi,(r,o)=>{let s=o.trim();return s&&t.push(s),""});return{think:t,cleaned:n}}function rh(e){if(e.startsWith("`")&&e.endsWith("`"))return{type:"inlineCode",content:e.slice(1,-1)};if(e.startsWith("**")&&e.endsWith("**")||e.startsWith("__")&&e.endsWith("__"))return{type:"bold",content:e.slice(2,-2)};if(e.startsWith("*")&&e.endsWith("*")||e.startsWith("_")&&e.endsWith("_"))return{type:"italic",content:e.slice(1,-1)};let t=e.match(/^\[([^\]]+)\]\((.+)\)$/);return t?{type:"link",label:t[1]??"",href:t[2]??""}:{type:"text",content:e}}function Ja(e){if(!e)return[];let t=[],n=0;for(let r of e.matchAll(th)){let o=r[0];if(!o)continue;let s=r.index??0;s>n&&t.push({type:"text",content:e.slice(n,s)}),t.push(rh(o)),n=s+o.length}return n<e.length&&t.push({type:"text",content:e.slice(n)}),t.filter(r=>r.type==="link"?r.label.length>0||r.href.length>0:r.content.length>0)}function oh(e){return typeof e.text=="string"&&e.text.length>0?e.text:Array.isArray(e.tokens)?Zt(e.tokens):""}function sh(e){return typeof e.text=="string"&&e.text.length>0?e.text:Array.isArray(e.tokens)?Zt(e.tokens):typeof e.raw=="string"?e.raw.split(`
|
|
104
113
|
`).map(t=>t.replace(/^>\s?/,"")).join(`
|
|
105
|
-
`).trim():""}function
|
|
106
|
-
|
|
107
|
-
`)});let o=
|
|
108
|
-
`);if(t.length===0)return["Think:"];let n=t.findIndex(r=>r.trim().length>0);return n<0&&(n=0),t.map((r,o)=>o===n?`Think: ${r}`:r)}function
|
|
109
|
-
`)}function
|
|
110
|
-
`).map((n,r)=>
|
|
111
|
-
`,Math.max(0,n-1));return r===-1?0:r+1}function
|
|
112
|
-
`,n);return r===-1?e.length:r}function
|
|
113
|
-
`);return o?{value:`${e.slice(0,r)}${o}${e.slice(r)}`,cursor:r+o.length}:{value:e,cursor:r}}function
|
|
114
|
-
`){c=y,p(),i=u,c=u,a="",l=0;continue}let b=Math.max(0,
|
|
115
|
-
`,this.extendWindow(t),!0):!1}newlineShouldInsertInsteadOfSubmit(t){let n=this.burstWindowUntilMs!==null&&t<=this.burstWindowUntilMs;return this.isActiveInternal()||n}extendWindow(t){this.burstWindowUntilMs=t+this.enterSuppressWindowMs}beginWithRetroGrabbed(t,n){t&&(this.buffer+=t),this.active=!0,this.extendWindow(n)}appendCharToBuffer(t,n){t&&(this.buffer+=t,this.extendWindow(n))}decideBeginBuffer(t,n,r){let o=
|
|
116
|
-
`)&&K(!1)},[$,K]),
|
|
117
|
-
`)){let B
|
|
118
|
-
`)){let B
|
|
119
|
-
`,!0),L.current.extendWindow(
|
|
120
|
-
`,!0);return}let pe=M.current.value.trim();if(!pe)return;if(pe.startsWith("/")){let B=xr(pe,an);switch(B.kind){case"message":ie(B.title,B.content);break;case"new":b();break;case"exit":g();break;case"switch_model":R(B.provider);break;case"set_tool_permission":I(B.mode);break;case"compact":T();break;case"review_pr":G(B.prNumber);break;case"init_agents_md":m(Fh);break;default:{let xe=B}}P.current=null,$({value:"",cursor:0},!0),K(!1);return}m(pe),P.current=null,$({value:"",cursor:0},!0),K(!1);return}if(Ue!=="none"){let x=M.current,E=Ue==="backspace"?gc(x.value,x.cursor):No(x.value,x.cursor);P.current=null,$(E,!0);return}if(w&&$t){let x=Array.from(w);if(x.length!==1){ue(!1),N(w,!0),L.current.clearAfterExplicitPaste();return}let E=x[0]??"";if(!E)return;let pe=it=>{let gt=M.current,cn=gt.value.slice(0,gt.cursor),ln=gt.value.slice(gt.cursor),Dt=L.current.decideBeginBuffer(_,cn,it);return Dt?(P.current=null,$({value:`${cn.slice(0,Dt.start)}${ln}`,cursor:Dt.start},!0),L.current.appendCharToBuffer(E,_),!0):!1};if(!((E.codePointAt(0)??0)<=127)){let it=L.current.onPlainCharNoHold(_);if(it?.type==="buffer_append"){L.current.appendCharToBuffer(E,_);return}if(it?.type==="begin_buffer"&&pe(it.retroChars))return;ue(!1),N(E,!0);return}let xe=L.current.onPlainChar(E,_);if(xe.type==="retain_first_char")return;if(xe.type==="buffer_append"||xe.type==="begin_buffer_from_pending"){L.current.appendCharToBuffer(E,_);return}if(xe.type==="begin_buffer"&&pe(xe.retroChars))return;ue(!1),N(E,!0);return}w&&(N(w,!0),L.current.clearWindowAfterNonChar())});let ft=Re?.columns??process.stdout?.columns??80,Q=Math.max(1,ft-3),Xe=Cc(ae.value,ae.cursor,Q),st=Xe.lines;return Ac(Do,{flexDirection:"column",gap:1,children:[Pt(Do,{flexDirection:"column",paddingY:1,children:st.map((w,S)=>{let _=w.text,Z=!t&&S===Xe.row,oe=Z?_.slice(0,Xe.cursorInRow):_,Ue=Z?_.slice(Xe.cursorInRow):"";return Ac(Do,{children:[Pt(Qn,{color:"gray",children:S===0?"\u203A ":" "}),Pt(Qn,{children:oe}),Z?Pt(Qn,{color:"cyan",children:"\u258A"}):null,Z?Pt(Qn,{children:Ue}):null]},`line-${S}`)})}),J!=="none"?Pt(uc,{items:O.map(({value:w,meta:S,..._})=>_),activeIndex:Se,loading:qe}):null]})});import{memo as Qh}from"react";import{Box as Ec,Text as Ho}from"ink";import{jsx as Bo,jsxs as Rc}from"react/jsx-runtime";var Ic=Qh(function({busy:t,pendingApproval:n=!1,contextPercent:r}){let o=`${r.toFixed(1)}%`;return Rc(Ec,{justifyContent:"space-between",children:[Bo(Ec,{children:t?Bo(Ho,{color:"yellow",children:"Working..."}):Bo(Ho,{color:"gray",children:n?"Approval pending \u2022 Enter confirm \u2022 Esc deny":"Enter send \u2022 Shift+Enter newline \u2022 /help"})}),Rc(Ho,{color:"gray",children:["context: ",o]})]})});import{memo as Zh}from"react";import{Box as Zn,Text as Wo,useInput as ey}from"ink";import{Select as ty,StatusMessage as ny}from"@inkjs/ui";import{jsx as mt,jsxs as Oc}from"react/jsx-runtime";var ry=[{label:"Allow once",decision:"once"},{label:"Allow for this session",decision:"session"},{label:"Deny",decision:"deny"}];function oy(e){if(!e)return"";if(typeof e!="object")return String(e);let t=Object.entries(e);if(!t.length)return"";let[n,r]=t[0]??[];if(!n)return"";let o=typeof r=="string"?r:JSON.stringify(r);return`${n}=${o?.slice(0,60)??""}${o&&o.length>60?"...":""}`}var Lc=Zh(function({request:t,onDecision:n}){ey((o,s)=>{(s.escape||s.ctrl&&o==="c")&&n("deny")});let r=oy(t.params);return Oc(Zn,{flexDirection:"column",borderStyle:"single",borderColor:"yellow",paddingX:1,children:[mt(Wo,{bold:!0,color:"yellow",children:"Tool Approval Required"}),Oc(Wo,{children:[t.toolName,r?` (${r})`:""]}),mt(Zn,{marginTop:1,children:mt(ny,{variant:"warning",children:t.reason})}),mt(Zn,{marginTop:1,flexDirection:"column",children:mt(ty,{options:ry.map(o=>({label:o.label,value:o.decision})),onChange:o=>{n(o)}})}),mt(Zn,{marginTop:1,children:mt(Wo,{color:"gray",children:"Enter confirm \u2022 Esc deny"})})]})});import{memo as sy,useMemo as iy,useState as ay}from"react";import{Box as jo,Text as er,useInput as cy}from"ink";import{MultiSelect as ly}from"@inkjs/ui";import{jsx as rn,jsxs as zo}from"react/jsx-runtime";function uy(e,t){let n=new Set(e);return t.filter(r=>n.has(r))}var Nc=sy(function({serverNames:t,defaultSelected:n,onConfirm:r,onExit:o}){let s=iy(()=>{let l=uy(t,n);return n.length===0?[]:l.length>0?l:[...t]},[n,t]),[i,c]=ay(s),a=i.length===t.length;return cy((l,p)=>{if(p.ctrl&&l==="c"){o();return}p.escape&&r(i,!1)}),zo(jo,{flexDirection:"column",borderStyle:"single",borderColor:"cyan",paddingX:1,children:[rn(er,{bold:!0,color:"cyan",children:"Activate MCP Servers"}),rn(er,{color:"gray",children:"Select servers to load for this run."}),rn(jo,{marginTop:1,flexDirection:"column",children:rn(ly,{options:t.map(l=>({label:l,value:l})),defaultValue:s,onChange:c,onSubmit:l=>{r(l,!0)}})}),zo(jo,{marginTop:1,flexDirection:"column",children:[zo(er,{color:"gray",children:["Selected: ",i.length,"/",t.length,a?" (all)":""]}),rn(er,{color:"gray",children:"Controls: \u2191/\u2193 move, Space toggle, Enter confirm"})]})]})});import{spawn as py}from"child_process";var Go="\x07",dy="Memo: Approval required",my="Memo CLI",fy=2e3;function gy(e){return e.replace(/\s+/g," ").trim()}function hy(e,t){return e.length<=t?e:`${e.slice(0,Math.max(0,t-3))}...`}function yy(e){let t=gy(e.reason),n=`Tool ${e.toolName} is waiting for your approval.`;return t?hy(`${n} ${t}`,220):n}function $c(e){return e.replace(/\\/g,"\\\\").replace(/"/g,'\\"')}function _y(e,t=process.platform){let n=dy,r=yy(e);return t==="darwin"?{command:"osascript",args:["-e",`display notification "${$c(r)}" with title "${$c(n)}"`]}:t==="linux"?{command:"notify-send",args:["--app-name",my,n,r]}:null}async function Sy(e,t){await new Promise((n,r)=>{let o=py(e,t,{stdio:"ignore"}),s=!1,i=a=>{if(!s){if(s=!0,a){r(a);return}n()}},c=setTimeout(()=>{try{o.kill()}catch{}i()},fy);o.once("error",a=>{clearTimeout(c),i(a)}),o.once("exit",a=>{if(clearTimeout(c),a===0||a===null){i();return}i(new Error(`${e} exited with code ${a}`))})})}function vy(e){if(e){e(Go);return}try{if(process.stdout?.isTTY){process.stdout.write(Go);return}process.stderr?.isTTY&&process.stderr.write(Go)}catch{}}async function Dc(e,t={}){vy(t.writeBell);let n=_y(e,t.platform??process.platform);if(!n)return;let r=t.runCommand??Sy;try{await r(n.command,n.args)}catch{}}import{Box as Et,Text as Ke,useInput as Ty}from"ink";import{Spinner as wy,StatusMessage as Cy,TextInput as by}from"@inkjs/ui";import{memo as xy,useCallback as Uc,useMemo as My,useState as tr}from"react";import{jsx as Ee,jsxs as Rt}from"react/jsx-runtime";var et=[{key:"name",label:"Provider name",hint:"Used for /models switching",defaultValue:"deepseek"},{key:"envKey",label:"API key env var",hint:"Read at runtime from environment variables",defaultValue:"DEEPSEEK_API_KEY"},{key:"model",label:"Model name",defaultValue:"deepseek-chat"},{key:"baseUrl",label:"Base URL",defaultValue:"https://api.deepseek.com"}],Fc=xy(function({configPath:t,onComplete:n,onExit:r}){let[o,s]=tr(0),[i,c]=tr({}),[a,l]=tr(!1),[p,u]=tr(null),d=et[o]??et[0],m=Uc(async b=>{l(!0),u(null);try{let U={current_provider:b.name,providers:[{name:b.name,env_api_key:b.envKey,model:b.model,base_url:b.baseUrl||void 0}]};await we(t,U),n()}catch(U){u(U.message),l(!1)}},[t,n]),g=Uc(async b=>{if(!d)return;let U=b.trim()||d.defaultValue,T={...i,[d.key]:U};if(c(T),o<et.length-1){s(o+1);return}let H={name:T.name||et[0].defaultValue,envKey:T.envKey||et[1].defaultValue,model:T.model||et[2].defaultValue,baseUrl:T.baseUrl||et[3].defaultValue};await m(H)},[m,d,o,i]);Ty((b,U)=>{U.ctrl&&b==="c"&&r()});let y=My(()=>`Step ${o+1}/${et.length}`,[o]);return d?Rt(Et,{flexDirection:"column",children:[Ee(Ke,{bold:!0,children:"Memo setup"}),Ee(Ke,{color:"gray",children:"No provider config found. Complete setup to continue."}),Rt(Ke,{color:"gray",children:["Config path: ",t]}),Rt(Et,{marginTop:1,flexDirection:"column",children:[Ee(Ke,{color:"cyan",children:y}),Ee(Ke,{children:d.label}),Rt(Ke,{color:"gray",children:["Default: ",d.defaultValue]}),d.hint?Ee(Ke,{color:"gray",children:d.hint}):null]}),Rt(Et,{marginTop:1,children:[Ee(Ke,{color:"gray",children:"> "}),Ee(by,{isDisabled:a,defaultValue:i[d.key]??"",placeholder:d.defaultValue,onSubmit:b=>{g(b)}},d.key)]}),Ee(Et,{marginTop:1,children:Ee(Ke,{color:"gray",children:"Enter to continue, Ctrl+C to exit"})}),a?Ee(Et,{marginTop:1,children:Ee(wy,{label:"Saving config..."})}):null,p?Ee(Et,{marginTop:1,children:Rt(Cy,{variant:"error",children:["Failed to save config: ",p]})}):null]}):null});function Hc(e){let t=e.finalText?.trim();return t||e.steps.map(n=>n.assistantText??"").join("").trim()}function ky(e){return e==="ok"||e==="error"||e==="cancelled"?e:void 0}function Ay(e){if(e.resultStatus)return e.resultStatus==="success"?ke.SUCCESS:ke.ERROR}function Py(e,t,n){return{index:-(n+1),userInput:e.input??"",steps:(e.steps??[]).map(r=>({index:r.step,assistantText:r.assistantText??"",thinking:r.thinking,action:r.action,parallelActions:r.parallelActions,observation:r.observation,toolStatus:Ay(r)})),status:ky(e.status),errorMessage:e.errorMessage,tokenUsage:e.tokenUsage,finalText:Hc(e),sequence:t}}function nr(e){let t=gn(e,"history.log"),n=[...t.turns].sort((i,c)=>i.turn-c.turn),r=[];for(let i of n){let c=(i.input??"").trim();c&&r.push({role:"user",content:c});let a=Hc(i);a&&r.push({role:"assistant",content:a})}let o=0,s=n.map((i,c)=>(o+=1,Py(i,o,c)));return{summary:t.summary,messages:r,turns:s,maxSequence:o}}function Ko(){return{turns:[],historicalTurns:[],systemMessages:[],sequence:0}}function Ey(e,t){return{index:e,userInput:"",steps:[],sequence:t}}function rr(e,t){if(e.length>t)return e;let n=e.slice();for(;n.length<=t;)n.push({index:n.length,assistantText:""});return n}function It(e,t,n){let r=e.turns.slice(),o=r.findIndex(i=>i.index===t);if(o===-1){let i=e.sequence+1;return r.push(n(Ey(t,i))),{turns:r,sequence:i}}let s=r[o];return s?(r[o]=n(s),{turns:r,sequence:e.sequence}):{turns:r,sequence:e.sequence}}function Ry(e){return{id:`${Date.now()}-${Math.random().toString(16).slice(2)}`,title:e.title,content:e.content,tone:e.tone??"info",sequence:e.sequence}}function Bc(e,t){switch(t.type){case"append_system_message":{let n=e.sequence+1;return{...e,sequence:n,systemMessages:[...e.systemMessages,Ry({title:t.title,content:t.content,tone:t.tone,sequence:n})]}}case"turn_start":{let n=It(e,t.turn,r=>({...r,index:t.turn,userInput:t.input,steps:[],finalText:void 0,status:void 0,errorMessage:void 0,tokenUsage:void 0,startedAt:Date.now(),durationMs:void 0,contextPromptTokens:t.promptTokens??r.contextPromptTokens}));return{...e,turns:n.turns,sequence:n.sequence}}case"context_usage":{let n=It(e,t.turn,r=>{let o=rr(r.steps,t.step),s=o[t.step];return s?(o[t.step]={...s,contextPromptTokens:t.promptTokens},{...r,contextPromptTokens:t.promptTokens,steps:o}):r});return{...e,turns:n.turns,sequence:n.sequence}}case"assistant_chunk":{let n=It(e,t.turn,r=>{let o=rr(r.steps,t.step),s=o[t.step];if(!s)return r;let i=`${s.assistantText}${t.chunk}`;return i===s.assistantText?r:(o[t.step]={...s,assistantText:i},{...r,steps:o})});return{...e,turns:n.turns,sequence:n.sequence}}case"tool_action":{let n=It(e,t.turn,r=>{let o=rr(r.steps,t.step),s=o[t.step];return s?(o[t.step]={...s,action:t.action,thinking:t.thinking,parallelActions:t.parallelActions&&t.parallelActions.length>1?t.parallelActions:void 0,toolStatus:ke.EXECUTING},{...r,steps:o}):r});return{...e,turns:n.turns,sequence:n.sequence}}case"tool_observation":{let n=It(e,t.turn,r=>{let o=rr(r.steps,t.step),s=o[t.step];return s?(o[t.step]={...s,observation:t.observation,toolStatus:t.toolStatus,parallelToolStatuses:t.parallelToolStatuses},{...r,steps:o}):r});return{...e,turns:n.turns,sequence:n.sequence}}case"turn_final":{let n=It(e,t.turn,r=>{let o=r.startedAt??Date.now(),s=Math.max(0,Date.now()-o),i=t.tokenUsage?.prompt??r.contextPromptTokens;return{...r,finalText:t.finalText,status:t.status,errorMessage:t.errorMessage,tokenUsage:t.turnUsage,contextPromptTokens:i,startedAt:o,durationMs:s}});return{...e,turns:n.turns,sequence:n.sequence}}case"replace_history":return{...e,historicalTurns:t.turns,sequence:Math.max(e.sequence,t.maxSequence)};case"clear_current_timeline":return{...e,turns:[],systemMessages:[]};case"reset_all":return Ko();default:return e}}import{dirname as or,join as jc,resolve as Iy}from"path";import{statSync as Oy,existsSync as zc,readFileSync as Ly}from"fs";import{readFile as Ny}from"fs/promises";import{get as $y}from"https";import{fileURLToPath as Dy}from"url";function Wc(e){let t=e.trim().replace(/^v/i,""),[n="",r]=t.split("-",2),o=n.split(".").map(s=>Number(s));return o.length<3||o.some(s=>!Number.isFinite(s))?null:{major:o[0]??0,minor:o[1]??0,patch:o[2]??0,prerelease:r??null}}function Uy(e,t){let n=Wc(e),r=Wc(t);return!n||!r?!1:n.major!==r.major?n.major>r.major:n.minor!==r.minor?n.minor>r.minor:n.patch!==r.patch?n.patch>r.patch:n.prerelease&&!r.prerelease?!1:!n.prerelease&&r.prerelease?!0:n.prerelease&&r.prerelease?n.prerelease>r.prerelease:!1}function Gc(){try{let t=Dy(import.meta.url);return or(t)}catch{}let e=Iy(process.argv[1]??process.cwd());try{return Oy(e).isFile()?or(e):e}catch{return process.cwd()}}async function Fy(e){let t=jc(e,"package.json");if(!zc(t))return null;let n=await Ny(t,"utf8"),r=JSON.parse(n);return!r.name||!r.version?null:{name:r.name,version:r.version}}function Hy(e){let t=jc(e,"package.json");if(!zc(t))return null;try{let n=Ly(t,"utf8"),r=JSON.parse(n);return!r.name||!r.version?null:{name:r.name,version:r.version}}catch{return null}}async function By(){let e=Gc();for(;;){let t=await Fy(e);if(t&&t.name==="@memo-code/memo")return t;let n=or(e);if(n===e)break;e=n}return null}function sr(){let e=Gc();for(;;){let t=Hy(e);if(t&&t.name==="@memo-code/memo")return t;let n=or(e);if(n===e)break;e=n}return null}async function Wy(e,t=1500){let r=`https://registry.npmjs.org/${encodeURIComponent(e)}/latest`;return new Promise(o=>{let s=$y(r,{timeout:t},i=>{if(i.statusCode&&i.statusCode>=400){i.resume(),o(null);return}let c=[];i.on("data",a=>c.push(a)),i.on("end",()=>{try{let a=JSON.parse(Buffer.concat(c).toString("utf8"));o(a.version??null)}catch{o(null)}})});s.on("timeout",()=>{s.destroy(),o(null)}),s.on("error",()=>o(null))})}async function Kc(){let e=await By();if(!e)return null;let t=await Wy(e.name);return!t||!Uy(t,e.version)?null:{current:e.version,latest:t}}import{readFile as jy}from"fs/promises";import{dirname as zy,join as Gy}from"path";import{fileURLToPath as Ky}from"url";var qy=/{{\s*([\w.-]+)\s*}}/g;function Vy(e,t){return e.replace(qy,(n,r)=>t[r]??"")}async function qo(e,t={}){let n=zy(Ky(import.meta.url)),r=Gy(n,"task-prompts",`${e}.md`),o=await jy(r,"utf-8");return Vy(o,t)}import{spawn as Xy}from"child_process";var Jy=12e3,Yy=new Set(["WRITE","MAINTAIN","ADMIN"]),Qy=["pull_request_read","list_pull_requests","search_pull_requests","add_issue_comment","add_comment_to_pending_review","issue_read","get_me"];function Zy(e){return e.replace(/\\/g,"/").toLowerCase()}function e_(e,t){if(e.toLowerCase().includes("github"))return!0;if("command"in t){let o=Zy(t.command),s=(t.args??[]).join(" ").toLowerCase();return!!(o.includes("github")||o.includes("gh-")||s.includes("github")||s.includes("gh-"))}let r=t.url.toLowerCase();return r.includes("github")||r.includes("api.github.com")}function t_(e,t){let n=new Set(t),r=Object.entries(e).filter(([i,c])=>e_(i,c)).map(([i])=>i),o=r.find(i=>n.has(i))??null,s=r.filter(i=>!n.has(i));return{active:o,inactiveCandidates:s}}function n_(e){let t=new Map;for(let n of e)for(let r of Qy){let o=`_${r}`;if(!n.endsWith(o))continue;let s=n.slice(0,-o.length);s&&t.set(s,(t.get(s)??0)+1)}return Array.from(t.entries()).filter(([,n])=>n>=2).sort((n,r)=>r[1]-n[1]).map(([n])=>n)}async function Vo(e,t,n,r=Jy){return new Promise(o=>{let s=Xy(e,t,{cwd:n,env:process.env,stdio:["ignore","pipe","pipe"]}),i="",c="",a=!1,l=u=>{a||(a=!0,clearTimeout(p),o(u))},p=setTimeout(()=>{s.kill("SIGTERM"),l({ok:!1,stdout:i,stderr:c,code:null,errorMessage:`${e} ${t.join(" ")} timed out after ${r}ms`})},r);s.stdout.on("data",u=>{i+=u.toString()}),s.stderr.on("data",u=>{c+=u.toString()}),s.on("error",u=>{l({ok:!1,stdout:i,stderr:c,code:null,errorMessage:u.message})}),s.on("close",u=>{l({ok:u===0,stdout:i,stderr:c,code:u})})})}async function r_(e){if(!(await Vo("gh",["--version"],e)).ok)return{ok:!1,reason:"GitHub MCP not available, and GitHub CLI (gh) is not installed or not executable. Install gh: https://cli.github.com/"};let n=await Vo("gh",["auth","status","-h","github.com"],e);if(!n.ok)return{ok:!1,reason:`GitHub CLI is installed but not authenticated for github.com. Run: gh auth login -h github.com (detail: ${n.stderr.trim()||n.stdout.trim()||n.errorMessage||"unknown error"})`};let r=await Vo("gh",["repo","view","--json","nameWithOwner,viewerPermission"],e);if(!r.ok)return{ok:!1,reason:`GitHub CLI authentication works, but this directory is not a readable GitHub repo for gh (detail: ${r.stderr.trim()||r.stdout.trim()||r.errorMessage||"unknown error"})`};let o;try{o=JSON.parse(r.stdout)}catch{return{ok:!1,reason:"Failed to parse `gh repo view` output. Try upgrading gh and retry."}}let s=o.nameWithOwner?.trim(),i=o.viewerPermission?.trim().toUpperCase();return!s||!i?{ok:!1,reason:"GitHub CLI did not return repository or permission info from `gh repo view`."}:Yy.has(i)?{ok:!0,repository:s,viewerPermission:i}:{ok:!1,reason:`GitHub CLI connected to ${s}, but permission is ${i}. PR review comments require WRITE/MAINTAIN/ADMIN permission.`}}async function qc(e){let t=n_(e.availableToolNames??[]),n=e.activeMcpServerNames.find(l=>t.includes(l))??null,r=l=>{let p=e.availableToolNames;if(!p||p.length===0)return!0;let u=`${l}_`;return p.some(d=>d.startsWith(u))};if(n&&r(n))return{kind:"github_mcp",strategy:"github_mcp",details:`Using active GitHub MCP server \`${n}\` (detected from loaded MCP tool signatures).`,mcpServerPrefix:n};let{active:o,inactiveCandidates:s}=t_(e.mcpServers,e.activeMcpServerNames);if(o&&r(o))return{kind:"github_mcp",strategy:"github_mcp",details:`Using active GitHub MCP server \`${o}\`.`,mcpServerPrefix:o};let i=await r_(e.cwd);if(i.ok){let l=s.length?` GitHub MCP server(s) configured but inactive in this session: ${s.join(", ")}.`:"";return{kind:"gh_cli",strategy:"gh_cli",details:`Using gh CLI on repo \`${i.repository}\` with permission \`${i.viewerPermission}\`.${l}`,repository:i.repository,viewerPermission:i.viewerPermission}}let c=s.length?` Also found configured but inactive GitHub MCP server(s): ${s.join(", ")}. Start a new session and activate one of them, or fix gh CLI access.`:"",a=o&&!r(o)?` Active MCP server \`${o}\` is configured, but no tools with prefix \`${o}_\` are currently loaded in this session.`:"";return{kind:"unavailable",reason:`${i.reason}${a}${c}`}}import{jsx as tt,jsxs as p_}from"react/jsx-runtime";function c_(e,t){if(e.length===0)return[];if(t===void 0)return[...e];if(t.length===0)return[];let n=new Set(e),r=t.filter(o=>n.has(o));return r.length>0?r:[...e]}function l_(e,t){if(e.length===0)return[];if(t.length===0)return[];let n=new Set(e);return t.filter(r=>n.has(r))}function u_(){try{process.stdout?.isTTY&&process.stdout.write("\x1Bc")}catch{}}function Xc({sessionOptions:e,providerName:t,model:n,configPath:r,mcpServers:o,cwd:s,sessionsDir:i,providers:c,modelProfiles:a,dangerous:l=!1,needsSetup:p=!1,initialHistory:u}){let{exit:d}=a_(),m=on(()=>Object.keys(o??{}).sort(),[o]),g=on(()=>c_(m,e.activeMcpServers),[m,e.activeMcpServers]),y=e.toolPermissionMode??(l?_e.FULL:_e.ONCE),[b,U]=s_(Bc,void 0,Ko),[T,H]=ee(t),[R,I]=ee(n),[G,ie]=ee(c),[Re,ae]=ee(a),[z,M]=ee(y),P=X(f=>vt({model_profiles:Re},f),[Re]),[ne,ce]=ee({...e,providerName:t,contextWindow:P({name:t,model:n}),dangerous:y===_e.FULL,toolPermissionMode:y}),[F,le]=ee(!1),[J,A]=ee([]),[O,re]=ee(null),[Se,ge]=ee(null),[qe,De]=ee(P({name:t,model:n})),[rt,ve]=ee(0),[be,k]=ee(p),[L,an]=ee(!p&&m.length>0),[Te,K]=ee(g),[$,Nt]=ee(null),[N,ot]=ee(null),ue=ir(null),Ve=ir(null),ft=ir(null),[Q,Xe]=ee(null),st=ir(null),w=on(()=>sr(),[]),S=X(f=>{U(f)},[]);Lt(()=>{u&&(S({type:"clear_current_timeline"}),S({type:"replace_history",turns:u.turns,maxSequence:u.maxSequence}),ge(u.messages),u.summary.trim()&&S({type:"append_system_message",title:"History",content:u.summary,tone:"info"}))},[S,u]),Lt(()=>{be||(K(g),an(m.length>0))},[be,g,m.length]);let _=X((f,v,C="info")=>{S({type:"append_system_message",title:f,content:v,tone:C})},[S]),Z=on(()=>({onAssistantStep:(f,v)=>{let C=Ve.current;C&&S({type:"assistant_chunk",turn:C,step:v,chunk:f})},requestApproval:z===_e.FULL||z===_e.NONE?void 0:f=>new Promise(v=>{Dc(f),Xe(f),st.current=v}),hooks:{onTurnStart:({turn:f,input:v,promptTokens:C})=>{Ve.current=f;let q=ft.current;q&&(ft.current=null);let Ie=q??v;C&&C>0&&ve(C),S({type:"turn_start",turn:f,input:Ie,promptTokens:C})},onContextUsage:({turn:f,step:v,promptTokens:C,phase:q})=>{ve(C),S({type:"context_usage",turn:f,step:v,promptTokens:C,phase:q})},onContextCompacted:({reason:f,status:v,beforeTokens:C,afterTokens:q,reductionPercent:Ie,errorMessage:pr})=>{v==="success"&&ve(q);let dr=f==="manual"?"manual command":"auto trigger";if(v==="success"){_("Context compacted",`Compacted by ${dr}: ${C} -> ${q} tokens (${Ie.toFixed(2)}% reduced).`);return}if(v==="skipped"){_("Context compacted",`Skipped (${dr}): nothing to compact.`,"warning");return}_("Context compacted",`Failed (${dr}): ${pr??"unknown error"}`,"warning")},onAction:({turn:f,step:v,action:C,thinking:q,parallelActions:Ie})=>{S({type:"tool_action",turn:f,step:v,action:C,thinking:q,parallelActions:Ie})},onObservation:({turn:f,step:v,observation:C,resultStatus:q,parallelResultStatuses:Ie})=>{S({type:"tool_observation",turn:f,step:v,observation:C,toolStatus:Po(q),parallelToolStatuses:za(Ie)})},onFinal:({turn:f,finalText:v,status:C,errorMessage:q,turnUsage:Ie,tokenUsage:pr})=>{S({type:"turn_final",turn:f,finalText:v,status:C,errorMessage:q,turnUsage:Ie,tokenUsage:pr}),le(!1)}}}),[_,S,z]);Lt(()=>{let f=!1;return(async()=>{if(!(be||L))try{let v=ue.current;v&&await v.close();let C=await jn(Z,ne);if(f){await C.close();return}ue.current=C,ot(C),re(C.historyFilePath??null)}catch(v){if(f)return;ue.current=null,ot(null),re(null),le(!1),_("Session",`Failed to create session: ${v.message}`,"error")}})(),()=>{f=!0}},[_,Z,L,ne,be]),Lt(()=>{let f=!1;return(async()=>{let v=await Kc();f||!v||_("Update",`Update available: v${v.latest}. Run: npm install -g @memo-code/memo@latest`)})(),()=>{f=!0}},[_]),Lt(()=>()=>{ue.current&&ue.current.close()},[]);let oe=X(async()=>{let f=st.current;f&&(f("deny"),st.current=null),Q&&Xe(null),ue.current&&await ue.current.close(),Nt("Bye!"),setTimeout(()=>d(),250)},[d,Q]),Ue=X(()=>{if(F){_("Clear","Cancel current run before clearing timeline.","warning");return}if(Q){_("Clear","Resolve current approval request before clearing timeline.","warning");return}S({type:"clear_current_timeline"}),ge(null),ve(0),u_()},[_,F,S,Q]),ur=X(()=>{if(F){_("New Session","Cancel current run before starting a new session.","warning");return}if(Q){_("New Session","Resolve current approval request before starting a new session.","warning");return}S({type:"reset_all"}),ge(null),ve(0),Ve.current=null,ce(f=>({...f,sessionId:Ot()})),_("New Session","Started a fresh session.")},[_,F,S,Q]),$t=X(async f=>{try{let v=await te();await we(v.configPath,{...v.config,current_provider:f})}catch(v){_("Config",`Failed to persist provider: ${v.message}`,"warning")}},[_]),x=X(async f=>{if(F){_("Model switch","Cancel current run before switching models.","warning");return}if(f.name===T&&f.model===R){_("Model switch",`Already using ${f.name} (${f.model}).`);return}S({type:"reset_all"}),ve(0),Ve.current=null;let v=P(f);H(f.name),I(f.model),De(v),ce(C=>({...C,sessionId:Ot(),providerName:f.name,contextWindow:v})),await $t(f.name),_("Model switch",`Switched to ${f.name} (${f.model}).`)},[_,F,R,T,S,$t,P]),E=X(f=>f===_e.NONE?"none (no tools)":f===_e.ONCE?"once (approval required)":"full (no approval)",[]),pe=X(f=>{if(F){_("Tools","Cancel current run before changing tool permission mode.","warning");return}if(Q){_("Tools","Resolve current approval request before changing tool permission mode.","warning");return}if(f===z){_("Tools",`Already using ${E(f)}.`);return}M(f),ce(v=>({...v,sessionId:Ot(),dangerous:f===_e.FULL,toolPermissionMode:f})),_("Tools",`Tool permission set to ${E(f)}.`)},[_,F,Q,E,z]),B=X(async f=>{try{let v=await te();await we(v.configPath,{...v.config,active_mcp_servers:f})}catch(v){_("MCP",`Failed to persist active MCP servers: ${v.message}`,"warning")}},[_]),xe=X((f,v)=>{let C=l_(m,f);K(C),an(!1),ce(q=>({...q,sessionId:Ot(),activeMcpServers:C})),v&&B(C)},[m,B]),it=X(async f=>{if(F){_("History","Cancel current run before loading session history.","warning");return}if(Q){_("History","Resolve current approval request before loading session history.","warning");return}try{let v=await o_(f.sessionFile,"utf8"),C=nr(v);S({type:"clear_current_timeline"}),S({type:"replace_history",turns:C.turns,maxSequence:C.maxSequence}),ge(C.messages),le(!1),ot(null),re(null),ve(0),Ve.current=null,ce(q=>({...q,sessionId:Ot()})),_("History",C.summary||f.input)}catch(v){_("History",`Failed to load ${f.sessionFile}: ${v.message}`,"error")}},[_,F,S,Q]),gt=X(()=>{F&&N?.cancelCurrentTurn?.()},[F,N]),cn=X(async()=>{if(F){_("Compact","Cancel current run before compacting context.","warning");return}if(Q){_("Compact","Resolve current approval request before compacting context.","warning");return}if(N)try{let f=await N.compactHistory("manual");ve(f.afterTokens)}catch(f){_("Compact",`Failed to compact context: ${f.message}`,"error")}},[_,F,Q,N]),ln=X(async()=>{if(!N||F)return;let f=Qe($e.INIT);try{let v=await qo("init_agents");A(C=>[...C,f]),le(!0),ft.current=f,await N.runTurn(v)}catch(v){le(!1),_("Init",`Failed to run init task: ${v.message}`,"error")}},[_,F,N]),Dt=X(async f=>{if(!N||F)return;if(z===_e.NONE){_("Review",'Tool permission mode is "none". Set `/tools once` or `/tools full` before running `/review`.',"warning");return}let v=`${Qe($e.REVIEW)} ${f}`;try{let C=await qc({cwd:s,mcpServers:o,activeMcpServerNames:Te,availableToolNames:N.listToolNames?.()??[]});if(C.kind==="unavailable"){_("Review",C.reason,"error");return}let q=await qo("review_pull_request",{pr_number:String(f),backend_strategy:C.strategy,backend_details:C.details,mcp_server_prefix:C.kind==="github_mcp"?C.mcpServerPrefix:"github"});A(Ie=>[...Ie,v]),_("Review",C.details),le(!0),ft.current=v,await N.runTurn(q)}catch(C){le(!1),_("Review",`Failed to run review task for PR #${f}: ${C.message}`,"error")}},[Te,_,F,s,o,N,z]),pl=X(async f=>{let v=f.trim();if(v){if(v.toLowerCase()===Mc){await oe();return}if(v===Qe($e.INIT)){await ln();return}if(!(!N||F)){A(C=>[...C,v]),le(!0);try{await N.runTurn(v)}catch(C){le(!1),_("Run",`Turn failed: ${C.message}`,"error")}}}},[_,F,oe,ln,N]),dl=X(async()=>{try{let f=await te(),v=lt(f.config),C=vt(f.config,v);ie(f.config.providers),ae(f.config.model_profiles),H(v.name),I(v.model),De(C),ce(q=>({...q,sessionId:Ot(),providerName:v.name,contextWindow:C,autoCompactThresholdPercent:f.config.auto_compact_threshold_percent})),k(!1),_("Setup",`Config saved to ${f.configPath}`)}catch(f){_("Setup",`Failed to reload config: ${f.message}`,"error")}},[_]);Lt(()=>{if(!N||!Se?.length)return;let f=N.history[0];f&&(N.history.splice(0,N.history.length,f,...Se),ge(null))},[Se,N]);let ml=X(f=>{let v=st.current;v&&(v(f),st.current=null),Xe(null)},[]),fl=Ga(rt,qe),gl=on(()=>({providerName:T,model:R,cwd:s,sessionId:ne.sessionId??"unknown",mcpNames:Te,version:w?.version??"unknown"}),[Te,R,T,s,w?.version,ne.sessionId]);return $?tt(Vc,{children:tt(i_,{color:"green",children:$})}):be?tt(Fc,{configPath:r,onComplete:dl,onExit:oe}):L?tt(Nc,{serverNames:m,defaultSelected:g,onConfirm:xe,onExit:()=>{oe()}}):p_(Vc,{flexDirection:"column",children:[tt(sc,{header:gl,systemMessages:b.systemMessages,turns:b.turns,historicalTurns:b.historicalTurns}),tt(Pc,{disabled:!N||!!Q,busy:F,history:J,cwd:s,sessionsDir:i,currentSessionFile:O??void 0,providers:G,configPath:r,providerName:T,model:R,toolPermissionMode:z,mcpServers:o,onSubmit:f=>{pl(f)},onExit:()=>{oe()},onClear:Ue,onNewSession:ur,onCancelRun:gt,onCompact:()=>{cn()},onHistorySelect:f=>{it(f)},onModelSelect:f=>{x(f)},onSetToolPermission:pe,onReviewPullRequest:f=>{Dt(f)},onSystemMessage:_}),Q?tt(Lc,{request:Q,onDecision:ml}):null,tt(Ic,{busy:F,pendingApproval:!!Q,contextPercent:fl})]})}var d_=`
|
|
114
|
+
`).trim():""}function Xa(e){return typeof e.text=="string"?e.text:Array.isArray(e.tokens)?Zt(e.tokens):typeof e.raw=="string"?e.raw:""}function Ya(e){let{think:t,cleaned:n}=nh(e),r=[];t.length>0&&r.push({type:"think",content:t.join(`
|
|
115
|
+
|
|
116
|
+
`)});let o=eh.lexer(n);for(let s of o)switch(s.type){case"html":{r.push({type:"html",content:s.text??s.raw??""});break}case"hr":{r.push({type:"hr"});break}case"heading":{r.push({type:"heading",level:typeof s.depth=="number"?s.depth:1,content:s.text??Xa(s)});break}case"paragraph":case"text":{let i=Xa(s);i.trim().length>0&&r.push({type:"paragraph",content:i});break}case"code":{r.push({type:"code",language:s.lang,content:s.text??""});break}case"blockquote":{let i=sh(s);i.trim().length>0&&r.push({type:"blockquote",content:i});break}case"list":{let i=Array.isArray(s.items)?s.items.map(c=>oh(c)).filter(c=>c.trim().length>0):[];i.length>0&&r.push({type:"list",items:i,ordered:!!s.ordered});break}default:break}return r}import{Fragment as gh,jsx as W,jsxs as Pt}from"react/jsx-runtime";var ch="\u2014\u2014\u2014";function lh(e,t){return e.repeat(Math.max(0,t))}function uh(e){let t=e.split(`
|
|
117
|
+
`);if(t.length===0)return["Think:"];let n=t.findIndex(r=>r.trim().length>0);return n<0&&(n=0),t.map((r,o)=>o===n?`Think: ${r}`:r)}function ph(e){return e.split(`
|
|
118
|
+
`)}function dh({content:e}){let t=ph(e);return W(Le,{flexDirection:"column",marginY:1,children:t.map((n,r)=>W(Ce,{children:n},r))})}function mh({node:e}){switch(e.type){case"bold":return W(Ce,{bold:!0,children:e.content});case"italic":return W(Ce,{italic:!0,children:e.content});case"inlineCode":return W(Ce,{color:"cyan",children:e.content});case"link":return Pt(gh,{children:[W(Ce,{color:"blue",underline:!0,children:e.label}),Pt(Ce,{color:"gray",children:[" (",e.href,")"]})]});case"text":return W(Ce,{children:e.content});default:return null}}function Oo({content:e}){let t=Ja(e);return W(Le,{flexWrap:"wrap",children:t.map((n,r)=>W(mh,{node:n},r))})}function fh(e,t){switch(e.type){case"html":return W(Le,{children:W(Ce,{color:"gray",dimColor:!0,children:e.content})},t);case"hr":return W(Le,{marginY:1,children:W(Ce,{color:"gray",dimColor:!0,children:ch})},t);case"think":return W(Le,{flexDirection:"column",marginY:1,children:uh(e.content).map((n,r)=>W(Ce,{color:"gray",dimColor:!0,children:n},r))},t);case"heading":return W(Le,{children:Pt(Ce,{bold:!0,color:"cyan",children:[lh("#",e.level)," ",e.content]})},t);case"paragraph":return W(Oo,{content:e.content},t);case"code":return W(dh,{content:e.content},t);case"blockquote":return W(Le,{flexDirection:"column",children:e.content.split(`
|
|
119
|
+
`).map((n,r)=>Pt(Le,{children:[W(Ce,{color:"gray",dimColor:!0,children:"> "}),W(Oo,{content:n})]},r))},t);case"list":return W(Le,{flexDirection:"column",children:e.items.map((n,r)=>Pt(Le,{children:[Pt(Ce,{color:"gray",children:[e.ordered?`${r+1}.`:"\u2022"," "]}),W(Oo,{content:n})]},r))},t);default:return null}}var Qa=ih(function({content:t}){let n=ah(()=>Ya(t),[t]);return W(Le,{flexDirection:"column",children:n.map((r,o)=>fh(r,`${r.type}-${o}`))})});import{jsx as ye,jsxs as Ae}from"react/jsx-runtime";function Za(e){return e===ke.ERROR?"red":e===ke.EXECUTING?"yellow":"green"}function ec(e,t){if(e==null)return null;if(typeof e=="string"){let s=Va(e)?Io(e,t):e;return Qt(s,70)}if(typeof e!="object"||Array.isArray(e))return Qt(String(e),70);let n=e,r=["cmd","path","file_path","dir_path","query","pattern","url","content"],o=new Set(["path","file_path","dir_path"]);for(let s of r){let i=n[s];if(i==null||i==="")continue;let c=String(i),a=o.has(s)?Io(c,t):c;return Qt(a,70)}return Qt(qa(n),70)}var tc=Lo(function({message:t}){let n=t.tone==="error"?"red":t.tone==="warning"?"yellow":"cyan";return Ae(Qe,{flexDirection:"column",children:[Ae(se,{color:n,children:["\u25CF ",t.title]}),ye(se,{color:"gray",children:t.content})]})}),hh=Lo(function({step:t,cwd:n}){let r=!!(t.parallelActions&&t.parallelActions.length>1),o=!r&&t.action?ec(t.action.input,n):null;return Ae(Qe,{flexDirection:"column",children:[t.thinking?Ae(Qe,{children:[ye(se,{color:"gray",children:"\u25CF "}),ye(se,{color:"gray",children:t.thinking})]}):null,r?t.parallelActions?.map((s,i)=>{let c=ec(s.input,n);return Ae(Qe,{children:[Ae(se,{color:Za(t.parallelToolStatuses?.[i]??t.toolStatus),children:["\u25CF"," "]}),ye(se,{color:"gray",children:"Used "}),ye(se,{color:"cyan",children:s.tool}),c?Ae(se,{color:"gray",children:[" (",c,")"]}):null]},`${s.tool}-${i}`)}):null,!r&&t.action?Ae(Qe,{children:[ye(se,{color:Za(t.toolStatus),children:"\u25CF "}),ye(se,{color:"gray",children:"Used "}),ye(se,{color:"cyan",children:t.action.tool}),o?Ae(se,{color:"gray",children:[" (",o,")"]}):null]}):null]})}),No=Lo(function({turn:t,cwd:n}){return Ae(Qe,{flexDirection:"column",children:[Ae(Qe,{marginY:.5,children:[ye(se,{color:"gray",children:"\u203A "}),ye(se,{children:t.userInput})]}),t.steps.map(r=>ye(hh,{step:r,cwd:n},`${t.index}-${r.index}`)),t.finalText?ye(Qe,{marginTop:0,children:ye(Qa,{content:t.finalText})}):null,t.status&&t.status!=="ok"?Ae(se,{color:"red",children:["Status: ",t.status]}):null,t.errorMessage?ye(se,{color:"red",children:t.errorMessage}):null]})});import{jsx as en,jsxs as tn}from"react/jsx-runtime";function rc(e){return e.sequence??0}function vh(e){return e.type==="header"}function Th(e){return e.id!==void 0}function wh(e){let t=sc("");return oc(()=>{let n=`${e.sessionId}-${e.providerName}-${e.model}-${e.version}`;return n!==t.current&&(t.current=n),t.current},[e.sessionId,e.providerName,e.model,e.version])}var ic=yh(function({header:t,systemMessages:n,turns:r,historicalTurns:o}){let s=wh(t),i=sc(t.cwd);Sh(()=>{i.current=t.cwd},[t.cwd]);let{completedTurns:c,inProgressTurn:a,staticItems:l}=oc(()=>{let u=[...o,...r],d=u.length>0?u[u.length-1]:void 0,m=d&&!!(d.finalText||d.status&&d.status!=="ok"),g=m?u:u.slice(0,-1),y=m?void 0:d,b={type:"header",data:t},U=[...n,...g];U.sort((F,R)=>rc(F)-rc(R));let T=[b,...U];return{completedTurns:g,inProgressTurn:y,staticItems:T}},[t,o,r,n]),p=i.current;return tn(nc,{flexDirection:"column",children:[en(_h,{items:l,children:u=>vh(u)?tn(nc,{borderStyle:"round",borderColor:"blue",paddingX:1,flexDirection:"column",children:[en(Gn,{bold:!0,children:"Memo Code CLI"}),tn(Gn,{color:"gray",children:[u.data.providerName," / ",u.data.model," \u2022 v",u.data.version]}),tn(Gn,{color:"gray",children:["cwd: ",u.data.cwd]}),tn(Gn,{color:"gray",children:["mcp: ",u.data.mcpNames.join(", ")||"none"]})]},`header-${s}`):Th(u)?en(tc,{message:u},u.id):en(No,{turn:u,cwd:p},`turn-${u.sequence??u.index}`)}),a?en(No,{turn:a,cwd:p}):null]})});import{memo as Fh,useCallback as mt,useEffect as Qn,useMemo as Ac,useRef as rn,useState as et}from"react";import{Box as Ho,Text as Zn,useInput as Bh,useStdout as jh}from"ink";async function ac(e){return Os(e)}import{basename as Ch,resolve as $o}from"path";var cc=new Map;function lc(e){let t=$o(e);return process.platform==="win32"?t.toLowerCase():t}function uc(e){return $o(e)}function bh(e){let t=$o(e),n=cc.get(t);if(n)return n;let r=new yn({sessionsDir:t});return cc.set(t,r),r}function xh(e){let t=Date.parse(e);return Number.isFinite(t)?t:0}function Mh(e){let t=e.title?.trim();return t||Ch(e.filePath).replace(/\.jsonl$/i,"")}async function Kn(e){let t=e.limit??10;if(t<=0)return[];let r=await bh(e.sessionsDir).getAllSummaries(),o=lc(e.cwd),s=e.activeSessionFile?uc(e.activeSessionFile):null,i=e.keyword?.trim().toLowerCase(),c=new Set,a=[],l=[...r].sort((p,u)=>u.date.updatedAt.localeCompare(p.date.updatedAt));for(let p of l){if(a.length>=t)break;if(lc(p.cwd)!==o)continue;let u=uc(p.filePath);if(s&&u===s||c.has(u))continue;let d=Mh(p);i&&!d.toLowerCase().includes(i)||(c.add(u),a.push({id:u,cwd:e.cwd,input:d,ts:xh(p.date.updatedAt),sessionFile:u}))}return a}import{memo as kh}from"react";import{Box as qn,Text as Vn}from"ink";import{jsx as pt,jsxs as Ph}from"react/jsx-runtime";var Ah="#3a3a3a",Xn="#262626",pc=kh(function({items:t,activeIndex:n,loading:r}){return r?pt(qn,{paddingX:1,backgroundColor:Xn,children:pt(Vn,{color:"gray",children:"Loading..."})}):t.length?pt(qn,{flexDirection:"column",backgroundColor:Xn,children:t.map((o,s)=>{let i=s===n;return Ph(qn,{paddingX:1,gap:2,backgroundColor:i?Ah:Xn,children:[pt(Vn,{color:i?"cyan":"white",bold:i,children:o.title}),o.subtitle?pt(Vn,{color:"gray",children:o.subtitle}):null]},o.id)})}):pt(qn,{paddingX:1,backgroundColor:Xn,children:pt(Vn,{color:"gray",children:"No matches"})})});import Eh from"string-width";var Rh=55296,Ih=56319,Oh=56320,Lh=57343;function dc(e){return e>=Rh&&e<=Ih}function mc(e){return e>=Oh&&e<=Lh}function Pe(e,t){if(!Number.isFinite(t)||t<=0)return 0;if(t>=e.length)return e.length;if(e.length===0)return 0;let n=Math.floor(t);if(n<=0)return 0;if(n>=e.length)return e.length;let r=e.charCodeAt(n),o=e.charCodeAt(n-1);return mc(r)&&dc(o)?n-1:n}function fc(e,t){let n=Pe(e,t);if(n>=e.length)return e.length;let r=e.codePointAt(n);return r===void 0?Math.min(e.length,n+1):Math.min(e.length,n+(r>65535?2:1))}function dt(e,t){let n=Pe(e,t);if(n<=0)return 0;let r=n-1;if(r<=0)return r;let o=e.charCodeAt(r),s=e.charCodeAt(r-1);return mc(o)&&dc(s)?r-1:r}function Jn(e,t){let n=Pe(e,t),r=e.lastIndexOf(`
|
|
120
|
+
`,Math.max(0,n-1));return r===-1?0:r+1}function Yn(e,t){let n=Pe(e,t),r=e.indexOf(`
|
|
121
|
+
`,n);return r===-1?e.length:r}function Nh(e){return/[\p{L}\p{N}_]/u.test(e)}function gc(e,t,n){let r=Pe(e,t);if(!n)return{value:e,cursor:r};let o=n.replace(/\r\n?/g,`
|
|
122
|
+
`);return o?{value:`${e.slice(0,r)}${o}${e.slice(r)}`,cursor:r+o.length}:{value:e,cursor:r}}function hc(e,t){let n=Pe(e,t);if(n<=0)return{value:e,cursor:n};let r=dt(e,n);return{value:`${e.slice(0,r)}${e.slice(n)}`,cursor:r}}function Do(e,t){let n=Pe(e,t);if(n>=e.length)return{value:e,cursor:n};let r=fc(e,n);return{value:`${e.slice(0,n)}${e.slice(r)}`,cursor:n}}function yc(e,t){let n=Pe(e,t);if(n<=0)return{value:e,cursor:n};let r=n;for(;r>0;){let s=dt(e,r);if(e.slice(s,r).trim().length>0)break;r=s}for(;r>0;){let s=dt(e,r),i=e.slice(s,r);if(!Nh(i))break;r=s}return r===n&&(r=dt(e,n)),{value:`${e.slice(0,r)}${e.slice(n)}`,cursor:r}}function Sc(e,t){let n=Pe(e,t),r=Jn(e,n);return r>=n?{value:e,cursor:n}:{value:`${e.slice(0,r)}${e.slice(n)}`,cursor:r}}function _c(e,t){let n=Pe(e,t),r=Yn(e,n);return r<=n?{value:e,cursor:n}:{value:`${e.slice(0,n)}${e.slice(r)}`,cursor:n}}function vc(e,t){return dt(e,t)}function Tc(e,t){return fc(e,t)}function wc(e,t){return Jn(e,t)}function Cc(e,t){return Yn(e,t)}function Uo(e,t,n,r){let o=Pe(e,t),s=Jn(e,o),i=Yn(e,o),c=o-s,a=r??c;if(n==="up"){if(s===0)return{cursor:o,preferredColumn:a,changed:!1};let d=s-1,m=Jn(e,d),g=Math.min(m+a,d);return{cursor:g,preferredColumn:a,changed:g!==o}}if(i>=e.length)return{cursor:o,preferredColumn:a,changed:!1};let l=i+1,p=Yn(e,l),u=Math.min(l+a,p);return{cursor:u,preferredColumn:a,changed:u!==o}}function bc(e,t,n){let r=Pe(e,t),o=Number.isFinite(n)?Math.max(1,Math.floor(n)):1,s=[],i=0,c=0,a="",l=0,p=()=>{s.push({text:a,start:i,end:c})},u=0;for(let g of e){let y=u;if(u+=g.length,g===`
|
|
123
|
+
`){c=y,p(),i=u,c=u,a="",l=0;continue}let b=Math.max(0,Eh(g));a.length>0&&l+b>o&&(p(),i=y,c=y,a="",l=0),a+=g,c=u,l+=b}if(p(),s.length===0)return{lines:[{text:"",start:0,end:0}],row:0,cursorInRow:0};let d=Math.max(0,s.length-1),m=(s[d]?.text??"").length;for(let g=0;g<s.length;g+=1){let y=s[g];if(y&&!(r<y.start)&&r<=y.end){d=g,m=r-y.start;break}}return{lines:s,row:d,cursorInRow:m}}function xc(e,t){let n=e==="\b"||e==="\x7F",r=!!t.ctrl&&e.toLowerCase()==="h";return t.backspace||n||r||t.delete&&!(t.ctrl||t.meta)?"backspace":t.delete?"delete":"none"}var $h=3,Mc=8,Dh=120,Uh=process.platform==="win32"?60:8,nn=class{lastPlainCharAtMs=null;consecutivePlainChars=0;burstWindowUntilMs=null;buffer="";active=!1;pendingFirstChar=null;minChars;charIntervalMs;enterSuppressWindowMs;activeIdleTimeoutMs;constructor(t={}){this.minChars=t.minChars??$h,this.charIntervalMs=t.charIntervalMs??Mc,this.enterSuppressWindowMs=t.enterSuppressWindowMs??Dh,this.activeIdleTimeoutMs=t.activeIdleTimeoutMs??Uh}static recommendedFlushDelayMs(){return Mc+1}recommendedActiveFlushDelayMs(){return this.activeIdleTimeoutMs+1}onPlainChar(t,n){let r=Array.from(t)[0]??"";return this.notePlainChar(n),this.active?(this.extendWindow(n),{type:"buffer_append"}):this.pendingFirstChar&&n-this.pendingFirstChar.atMs<=this.charIntervalMs?(this.active=!0,this.buffer+=this.pendingFirstChar.ch,this.pendingFirstChar=null,this.extendWindow(n),{type:"begin_buffer_from_pending"}):this.consecutivePlainChars>=this.minChars?{type:"begin_buffer",retroChars:Math.max(0,this.consecutivePlainChars-1)}:r?(this.pendingFirstChar={ch:r,atMs:n},{type:"retain_first_char"}):{type:"retain_first_char"}}onPlainCharNoHold(t){return this.notePlainChar(t),this.active?(this.extendWindow(t),{type:"buffer_append"}):this.consecutivePlainChars>=this.minChars?{type:"begin_buffer",retroChars:Math.max(0,this.consecutivePlainChars-1)}:null}flushIfDue(t){let n=this.isActiveInternal()?this.activeIdleTimeoutMs:this.charIntervalMs;if(!(this.lastPlainCharAtMs!==null&&t-this.lastPlainCharAtMs>n))return{type:"none"};if(this.isActiveInternal()){this.active=!1;let o=this.buffer;return this.buffer="",{type:"paste",text:o}}if(this.pendingFirstChar){let o=this.pendingFirstChar.ch;return this.pendingFirstChar=null,{type:"typed",text:o}}return{type:"none"}}appendNewlineIfActive(t){return this.isActiveInternal()?(this.buffer+=`
|
|
124
|
+
`,this.extendWindow(t),!0):!1}newlineShouldInsertInsteadOfSubmit(t){let n=this.burstWindowUntilMs!==null&&t<=this.burstWindowUntilMs;return this.isActiveInternal()||n}extendWindow(t){this.burstWindowUntilMs=t+this.enterSuppressWindowMs}beginWithRetroGrabbed(t,n){t&&(this.buffer+=t),this.active=!0,this.extendWindow(n)}appendCharToBuffer(t,n){t&&(this.buffer+=t,this.extendWindow(n))}decideBeginBuffer(t,n,r){let o=Hh(n,r),s=n.slice(o);return/\s/u.test(s)||Array.from(s).length>=16?(this.beginWithRetroGrabbed(s,t),{start:o,grabbed:s}):null}flushBeforeModifiedInput(){if(!this.isActive())return null;this.active=!1;let t=this.buffer;return this.buffer="",this.pendingFirstChar&&(t+=this.pendingFirstChar.ch,this.pendingFirstChar=null),t}clearWindowAfterNonChar(){this.consecutivePlainChars=0,this.lastPlainCharAtMs=null,this.burstWindowUntilMs=null,this.active=!1,this.pendingFirstChar=null}isActive(){return this.isActiveInternal()||this.pendingFirstChar!==null}isBuffering(){return this.isActiveInternal()}hasPendingFirstChar(){return this.pendingFirstChar!==null}clearAfterExplicitPaste(){this.lastPlainCharAtMs=null,this.consecutivePlainChars=0,this.burstWindowUntilMs=null,this.active=!1,this.buffer="",this.pendingFirstChar=null}notePlainChar(t){this.lastPlainCharAtMs!==null&&t-this.lastPlainCharAtMs<=this.charIntervalMs?this.consecutivePlainChars+=1:this.consecutivePlainChars=1,this.lastPlainCharAtMs=t}isActiveInternal(){return this.active||this.buffer.length>0}};function Hh(e,t){if(t<=0)return e.length;let n=e.length,r=t;for(;r>0&&n>0;)n=dt(e,n),r-=1;return n}var $e={HELP:"help",EXIT:"exit",NEW:"new",RESUME:"resume",REVIEW:"review",MODELS:"models",TOOLS:"tools",COMPACT:"compact",MCP:"mcp",INIT:"init"},Se={NONE:"none",ONCE:"once",FULL:"full"};function Ze(e){return`/${e}`}var kc="exit";import{jsx as Et,jsxs as Pc}from"react/jsx-runtime";var Wh=400,Fo=Ze($e.MODELS),Bo=Ze($e.TOOLS),zh=Ze($e.INIT),Gh=[{mode:Se.NONE,description:"Disable all tool calls"},{mode:Se.ONCE,description:"Require approval when needed"},{mode:Se.FULL,description:"Run tools without approval"}];function Kh(e){let t=e.lastIndexOf("@");if(t===-1)return null;if(t>0){let r=e[t-1];if(r&&!/\s/.test(r))return null}let n=e.slice(t+1);return/\s/.test(n)?null:{type:"file",query:n,tokenStart:t+1}}function qh(e){let t=e.trimStart(),n=e.length-t.length;if(!t.length)return null;let r=t;if(r.startsWith("/")&&(r=r.slice(1)),!r.toLowerCase().startsWith($e.RESUME)||e.slice(0,n).trim().length>0)return null;let s=r.slice($e.RESUME.length);return s&&!s.startsWith(" ")?null:{type:"history",keyword:s.trim()}}function Vh(e){let t=e.trimStart();if(!t.startsWith("/"))return null;let n=t.slice(1);return n.includes(" ")?null:n.length?/^[a-zA-Z-]+$/.test(n)?{type:"slash",keyword:n.toLowerCase()}:null:{type:"slash",keyword:""}}function Xh(e){let t=e.trimStart();if(!t.startsWith(Fo))return null;let n=t.slice(Fo.length);return n&&!n.startsWith(" ")?null:{type:"models",keyword:n.trim().toLowerCase()}}function Jh(e){let t=e.trimStart();if(!t.startsWith(Bo))return null;let n=t.slice(Bo.length);return n&&!n.startsWith(" ")?null:{type:"tools"}}function Yh(e){return Jh(e)??Xh(e)??Vh(e)??Kh(e)??qh(e)}function Qh(e){let t=new Date(e);if(Number.isNaN(t.getTime()))return"";let n=String(t.getFullYear()),r=String(t.getMonth()+1).padStart(2,"0"),o=String(t.getDate()).padStart(2,"0"),s=String(t.getHours()).padStart(2,"0"),i=String(t.getMinutes()).padStart(2,"0");return`${n}-${r}-${o} ${s}:${i}`}function Zh(e,t){return{mode:"model",items:e.filter(r=>{if(!t)return!0;let o=r.name.toLowerCase(),s=r.model.toLowerCase();return o.includes(t)||s.includes(t)}).map(r=>({id:r.name,title:`${r.name}: ${r.model}`,subtitle:r.base_url,kind:"model",value:`${Fo} ${r.name}`,meta:{type:"model",provider:r}}))}}function ey(e){return{mode:"tools",items:Gh.map(n=>({id:n.mode,title:n.mode,subtitle:n.mode===e?`Current \xB7 ${n.description}`:n.description,kind:"tools",value:`${Bo} ${n.mode}`,meta:{type:"tools",mode:n.mode}}))}}function ty(e){return{mode:"slash",items:zt.filter(n=>n.name.startsWith(e)).map(n=>({id:n.name,title:`/${n.name}`,subtitle:n.description,kind:"slash",value:`/${n.name}`,meta:{type:"slash"}}))}}async function ny({trigger:e,cwd:t,sessionsDir:n,currentSessionFile:r,providers:o,toolPermissionMode:s}){switch(e.type){case"file":return{mode:"file",items:(await ac({cwd:t,query:e.query,limit:8,respectGitIgnore:!0})).map(a=>({id:a.id,title:a.isDir?`${a.path}/`:a.path,kind:"file",value:a.isDir?`${a.path}/`:a.path,meta:{type:"file",isDir:a.isDir}}))};case"history":return{mode:"history",items:(await Kn({sessionsDir:n,cwd:t,keyword:e.keyword,activeSessionFile:r})).map(a=>({id:a.id,title:a.input,subtitle:Qh(a.ts),kind:"history",value:a.input,meta:{type:"history",entry:a}}))};case"models":return Zh(o,e.keyword);case"tools":return ey(s);case"slash":return ty(e.keyword)}}var Ec=Fh(function({disabled:t,busy:n,history:r,cwd:o,sessionsDir:s,currentSessionFile:i,providers:c,configPath:a,providerName:l,model:p,toolPermissionMode:u,mcpServers:d,onSubmit:m,onExit:g,onClear:y,onNewSession:b,onCancelRun:U,onCompact:T,onHistorySelect:F,onModelSelect:R,onSetToolPermission:I,onReviewPullRequest:G,onSystemMessage:ie}){let{stdout:Re}=jh(),[ae,z]=et({value:"",cursor:0}),M=rn(ae),P=rn(null),[ne,ce]=et(null),[H,le]=et(""),[J,A]=et("none"),[O,re]=et([]),[_e,ge]=et(0),[qe,De]=et(!1),[ot,ve]=et(!1),be=rn(0),k=rn(0),L=rn(new nn);Qn(()=>{M.current=ae,ve(!1)},[ae]);let cn=Ac(()=>({configPath:a,providerName:l,model:p,mcpServers:d,providers:c,toolPermissionMode:u}),[a,l,p,d,c,u]),Te=Ac(()=>t||ot?null:Yh(ae.value),[t,ae.value,ot]),K=mt((w=!0)=>{w&&ve(!0),A("none"),re([]),ge(0),De(!1)},[]),$=mt((w,_=!1)=>{M.current=w,z(w),_&&(ce(null),le(""))},[]),$t=mt(()=>{$({value:"",cursor:0},!0)},[$]),N=mt((w,_=!0)=>{if(!w)return;let S=M.current;P.current=null;let Z=gc(S.value,S.cursor,w);$(Z,_),w.includes(`
|
|
125
|
+
`)&&K(!1)},[$,K]),st=mt(w=>w.type==="none"?!1:w.type==="paste"?(w.text&&N(w.text,!0),!0):(N(w.text,!0),!0),[N]),ue=mt((w=!0)=>{let _=L.current.flushBeforeModifiedInput();return _&&N(_,!0),w&&L.current.clearWindowAfterNonChar(),!!_},[N]);Qn(()=>{let w=setInterval(()=>{let _=L.current.flushIfDue(Date.now());st(_)},nn.recommendedFlushDelayMs());return()=>{clearInterval(w)}},[st]),Qn(()=>{t&&K(!1)},[t,K]),Qn(()=>{if(!Te){K(!1);return}let w=!1,_=++be.current;return De(!0),(async()=>{try{let{mode:S,items:Z}=await ny({trigger:Te,cwd:o,sessionsDir:s,currentSessionFile:i,providers:c,toolPermissionMode:u});if(w||_!==be.current)return;A(S),re(Z),ge(oe=>Z.length?Math.min(oe,Z.length-1):0)}finally{!w&&_===be.current&&De(!1)}})(),()=>{w=!0}},[Te,o,s,i,c,u,K]);let Ve=mt(w=>{if(w){if(J==="file"&&Te?.type==="file"){let _=M.current.value,S=_.slice(0,Te.tokenStart),Z=_.slice(Te.tokenStart+Te.query.length),oe=`${S}${w.value}${Z}`,Ue=S.length+w.value.length;$({value:oe,cursor:Ue},!0),w.meta?.type==="file"&&w.meta.isDir||K();return}switch(w.meta?.type){case"history":F(w.meta.entry),$({value:w.value,cursor:w.value.length},!1),K();return;case"model":R(w.meta.provider),$t(),K();return;case"tools":I(w.meta.mode),$t(),K();return;case"slash":$({value:`${w.value} `,cursor:`${w.value} `.length},!0),K(!1);return;default:$({value:w.value,cursor:w.value.length},!0),K()}}},[$,J,Te,K,$t,F,R,I]);Bh((w,_)=>{if(_.ctrl&&w==="c"){g();return}if(t)return;let S=Date.now();st(L.current.flushIfDue(S));let Z=J!=="none",oe=Z&&O.length>0,Ue=xc(w,_),pr=!!(_.ctrl||_.meta),Dt=!!w&&!pr&&!_.return&&!_.tab&&Ue==="none"&&!_.escape&&!_.upArrow&&!_.downArrow&&!_.leftArrow&&!_.rightArrow;if(!Dt&&!_.return&&ue(!0),_.ctrl&&w==="a"){let x=M.current,E=wc(x.value,x.cursor);P.current=null,$({value:x.value,cursor:E},!1);return}if(_.ctrl&&w==="e"){let x=M.current,E=Cc(x.value,x.cursor);P.current=null,$({value:x.value,cursor:E},!1);return}if(_.ctrl&&w==="u"){let x=M.current,E=Sc(x.value,x.cursor);P.current=null,$(E,!0);return}if(_.ctrl&&w==="k"){let x=M.current,E=_c(x.value,x.cursor);P.current=null,$(E,!0);return}if(_.ctrl&&w==="w"){let x=M.current,E=yc(x.value,x.cursor);P.current=null,$(E,!0);return}if(_.ctrl&&w==="d"){let x=M.current;if(!x.value){g();return}let E=Do(x.value,x.cursor);P.current=null,$(E,!0);return}if(_.ctrl&&w==="l"){$({value:"",cursor:0},!0),K(),y(),b();return}if(_.escape){if(S-k.current<=Wh){k.current=0,n?U():(P.current=null,$({value:"",cursor:0},!0),K());return}k.current=S,Z&&K();return}if(_.upArrow){if(oe){ge(B=>B<=0?O.length-1:B-1);return}let x=M.current;if(x.value.includes(`
|
|
126
|
+
`)){let B=Uo(x.value,x.cursor,"up",P.current??void 0);if(B.changed){P.current=B.preferredColumn,$({value:x.value,cursor:B.cursor},!1);return}}if(!r.length)return;if(ne===null){le(x.value);let B=r.length-1;ce(B);let xe=r[B]??"";P.current=null,$({value:xe,cursor:xe.length},!1);return}let E=Math.max(0,ne-1);ce(E);let pe=r[E]??"";P.current=null,$({value:pe,cursor:pe.length},!1);return}if(_.downArrow){if(oe){ge(B=>(B+1)%O.length);return}let x=M.current;if(x.value.includes(`
|
|
127
|
+
`)){let B=Uo(x.value,x.cursor,"down",P.current??void 0);if(B.changed){P.current=B.preferredColumn,$({value:x.value,cursor:B.cursor},!1);return}}if(ne===null)return;let E=ne+1;if(E>=r.length){ce(null),P.current=null,$({value:H,cursor:H.length},!1),le("");return}ce(E);let pe=r[E]??"";P.current=null,$({value:pe,cursor:pe.length},!1);return}if(_.leftArrow){let x=M.current,E=vc(x.value,x.cursor);P.current=null,$({value:x.value,cursor:E},!1);return}if(_.rightArrow){let x=M.current,E=Tc(x.value,x.cursor);P.current=null,$({value:x.value,cursor:E},!1);return}if(_.tab&&oe){Ve(O[_e]);return}if(_.return){if(oe){Ve(O[_e]);return}L.current.hasPendingFirstChar()&&!L.current.isBuffering()&&ue(!1);let x=M.current.value.trimStart().startsWith("/");if(!_.shift&&!x){if(L.current.appendNewlineIfActive(S))return;if(L.current.newlineShouldInsertInsteadOfSubmit(S)){ue(!1),N(`
|
|
128
|
+
`,!0),L.current.extendWindow(S);return}}if(ue(!0),_.shift){N(`
|
|
129
|
+
`,!0);return}let pe=M.current.value.trim();if(!pe)return;if(pe.startsWith("/")){let B=kr(pe,cn);switch(B.kind){case"message":ie(B.title,B.content);break;case"new":b();break;case"exit":g();break;case"switch_model":R(B.provider);break;case"set_tool_permission":I(B.mode);break;case"compact":T();break;case"review_pr":G(B.prNumber);break;case"init_agents_md":m(zh);break;default:{let xe=B}}P.current=null,$({value:"",cursor:0},!0),K(!1);return}m(pe),P.current=null,$({value:"",cursor:0},!0),K(!1);return}if(Ue!=="none"){let x=M.current,E=Ue==="backspace"?hc(x.value,x.cursor):Do(x.value,x.cursor);P.current=null,$(E,!0);return}if(w&&Dt){let x=Array.from(w);if(x.length!==1){ue(!1),N(w,!0),L.current.clearAfterExplicitPaste();return}let E=x[0]??"";if(!E)return;let pe=at=>{let ht=M.current,ln=ht.value.slice(0,ht.cursor),un=ht.value.slice(ht.cursor),Ut=L.current.decideBeginBuffer(S,ln,at);return Ut?(P.current=null,$({value:`${ln.slice(0,Ut.start)}${un}`,cursor:Ut.start},!0),L.current.appendCharToBuffer(E,S),!0):!1};if(!((E.codePointAt(0)??0)<=127)){let at=L.current.onPlainCharNoHold(S);if(at?.type==="buffer_append"){L.current.appendCharToBuffer(E,S);return}if(at?.type==="begin_buffer"&&pe(at.retroChars))return;ue(!1),N(E,!0);return}let xe=L.current.onPlainChar(E,S);if(xe.type==="retain_first_char")return;if(xe.type==="buffer_append"||xe.type==="begin_buffer_from_pending"){L.current.appendCharToBuffer(E,S);return}if(xe.type==="begin_buffer"&&pe(xe.retroChars))return;ue(!1),N(E,!0);return}w&&(N(w,!0),L.current.clearWindowAfterNonChar())});let gt=Re?.columns??process.stdout?.columns??80,Q=Math.max(1,gt-3),Xe=bc(ae.value,ae.cursor,Q),it=Xe.lines;return Pc(Ho,{flexDirection:"column",gap:1,children:[Et(Ho,{flexDirection:"column",paddingY:1,children:it.map((w,_)=>{let S=w.text,Z=!t&&_===Xe.row,oe=Z?S.slice(0,Xe.cursorInRow):S,Ue=Z?S.slice(Xe.cursorInRow):"";return Pc(Ho,{children:[Et(Zn,{color:"gray",children:_===0?"\u203A ":" "}),Et(Zn,{children:oe}),Z?Et(Zn,{color:"cyan",children:"\u258A"}):null,Z?Et(Zn,{children:Ue}):null]},`line-${_}`)})}),J!=="none"?Et(pc,{items:O.map(({value:w,meta:_,...S})=>S),activeIndex:_e,loading:qe}):null]})});import{memo as ry}from"react";import{Box as Rc,Text as jo}from"ink";import{jsx as Wo,jsxs as Ic}from"react/jsx-runtime";var Oc=ry(function({busy:t,pendingApproval:n=!1,contextPercent:r}){let o=`${r.toFixed(1)}%`;return Ic(Rc,{justifyContent:"space-between",children:[Wo(Rc,{children:t?Wo(jo,{color:"yellow",children:"Working..."}):Wo(jo,{color:"gray",children:n?"Approval pending \u2022 Enter confirm \u2022 Esc deny":"Enter send \u2022 Shift+Enter newline \u2022 /help"})}),Ic(jo,{color:"gray",children:["context: ",o]})]})});import{memo as oy}from"react";import{Box as er,Text as zo,useInput as sy}from"ink";import{Select as iy,StatusMessage as ay}from"@inkjs/ui";import{jsx as ft,jsxs as Lc}from"react/jsx-runtime";var cy=[{label:"Allow once",decision:"once"},{label:"Allow for this session",decision:"session"},{label:"Deny",decision:"deny"}];function ly(e){if(!e)return"";if(typeof e!="object")return String(e);let t=Object.entries(e);if(!t.length)return"";let[n,r]=t[0]??[];if(!n)return"";let o=typeof r=="string"?r:JSON.stringify(r);return`${n}=${o?.slice(0,60)??""}${o&&o.length>60?"...":""}`}var Nc=oy(function({request:t,onDecision:n}){sy((o,s)=>{(s.escape||s.ctrl&&o==="c")&&n("deny")});let r=ly(t.params);return Lc(er,{flexDirection:"column",borderStyle:"single",borderColor:"yellow",paddingX:1,children:[ft(zo,{bold:!0,color:"yellow",children:"Tool Approval Required"}),Lc(zo,{children:[t.toolName,r?` (${r})`:""]}),ft(er,{marginTop:1,children:ft(ay,{variant:"warning",children:t.reason})}),ft(er,{marginTop:1,flexDirection:"column",children:ft(iy,{options:cy.map(o=>({label:o.label,value:o.decision})),onChange:o=>{n(o)}})}),ft(er,{marginTop:1,children:ft(zo,{color:"gray",children:"Enter confirm \u2022 Esc deny"})})]})});import{memo as uy,useMemo as py,useState as dy}from"react";import{Box as Go,Text as tr,useInput as my}from"ink";import{MultiSelect as fy}from"@inkjs/ui";import{jsx as on,jsxs as Ko}from"react/jsx-runtime";function gy(e,t){let n=new Set(e);return t.filter(r=>n.has(r))}var $c=uy(function({serverNames:t,defaultSelected:n,onConfirm:r,onExit:o}){let s=py(()=>{let l=gy(t,n);return n.length===0?[]:l.length>0?l:[...t]},[n,t]),[i,c]=dy(s),a=i.length===t.length;return my((l,p)=>{if(p.ctrl&&l==="c"){o();return}p.escape&&r(i,!1)}),Ko(Go,{flexDirection:"column",borderStyle:"single",borderColor:"cyan",paddingX:1,children:[on(tr,{bold:!0,color:"cyan",children:"Activate MCP Servers"}),on(tr,{color:"gray",children:"Select servers to load for this run."}),on(Go,{marginTop:1,flexDirection:"column",children:on(fy,{options:t.map(l=>({label:l,value:l})),defaultValue:s,onChange:c,onSubmit:l=>{r(l,!0)}})}),Ko(Go,{marginTop:1,flexDirection:"column",children:[Ko(tr,{color:"gray",children:["Selected: ",i.length,"/",t.length,a?" (all)":""]}),on(tr,{color:"gray",children:"Controls: \u2191/\u2193 move, Space toggle, Enter confirm"})]})]})});import{spawn as hy}from"child_process";var qo="\x07",yy="Memo: Approval required",Sy="Memo CLI",_y=2e3;function vy(e){return e.replace(/\s+/g," ").trim()}function Ty(e,t){return e.length<=t?e:`${e.slice(0,Math.max(0,t-3))}...`}function wy(e){let t=vy(e.reason),n=`Tool ${e.toolName} is waiting for your approval.`;return t?Ty(`${n} ${t}`,220):n}function Dc(e){return e.replace(/\\/g,"\\\\").replace(/"/g,'\\"')}function Cy(e,t=process.platform){let n=yy,r=wy(e);return t==="darwin"?{command:"osascript",args:["-e",`display notification "${Dc(r)}" with title "${Dc(n)}"`]}:t==="linux"?{command:"notify-send",args:["--app-name",Sy,n,r]}:null}async function by(e,t){await new Promise((n,r)=>{let o=hy(e,t,{stdio:"ignore"}),s=!1,i=a=>{if(!s){if(s=!0,a){r(a);return}n()}},c=setTimeout(()=>{try{o.kill()}catch{}i()},_y);o.once("error",a=>{clearTimeout(c),i(a)}),o.once("exit",a=>{if(clearTimeout(c),a===0||a===null){i();return}i(new Error(`${e} exited with code ${a}`))})})}function xy(e){if(e){e(qo);return}try{if(process.stdout?.isTTY){process.stdout.write(qo);return}process.stderr?.isTTY&&process.stderr.write(qo)}catch{}}async function Uc(e,t={}){xy(t.writeBell);let n=Cy(e,t.platform??process.platform);if(!n)return;let r=t.runCommand??by;try{await r(n.command,n.args)}catch{}}import{Box as Rt,Text as Ke,useInput as My}from"ink";import{Spinner as ky,StatusMessage as Ay,TextInput as Py}from"@inkjs/ui";import{memo as Ey,useCallback as Hc,useMemo as Ry,useState as nr}from"react";import{jsx as Ee,jsxs as It}from"react/jsx-runtime";var tt=[{key:"name",label:"Provider name",hint:"Used for /models switching",defaultValue:"deepseek"},{key:"envKey",label:"API key env var",hint:"Read at runtime from environment variables",defaultValue:"DEEPSEEK_API_KEY"},{key:"model",label:"Model name",defaultValue:"deepseek-chat"},{key:"baseUrl",label:"Base URL",defaultValue:"https://api.deepseek.com"}],Fc=Ey(function({configPath:t,onComplete:n,onExit:r}){let[o,s]=nr(0),[i,c]=nr({}),[a,l]=nr(!1),[p,u]=nr(null),d=tt[o]??tt[0],m=Hc(async b=>{l(!0),u(null);try{let U={current_provider:b.name,providers:[{name:b.name,env_api_key:b.envKey,model:b.model,base_url:b.baseUrl||void 0}]};await we(t,U),n()}catch(U){u(U.message),l(!1)}},[t,n]),g=Hc(async b=>{if(!d)return;let U=b.trim()||d.defaultValue,T={...i,[d.key]:U};if(c(T),o<tt.length-1){s(o+1);return}let F={name:T.name||tt[0].defaultValue,envKey:T.envKey||tt[1].defaultValue,model:T.model||tt[2].defaultValue,baseUrl:T.baseUrl||tt[3].defaultValue};await m(F)},[m,d,o,i]);My((b,U)=>{U.ctrl&&b==="c"&&r()});let y=Ry(()=>`Step ${o+1}/${tt.length}`,[o]);return d?It(Rt,{flexDirection:"column",children:[Ee(Ke,{bold:!0,children:"Memo setup"}),Ee(Ke,{color:"gray",children:"No provider config found. Complete setup to continue."}),It(Ke,{color:"gray",children:["Config path: ",t]}),It(Rt,{marginTop:1,flexDirection:"column",children:[Ee(Ke,{color:"cyan",children:y}),Ee(Ke,{children:d.label}),It(Ke,{color:"gray",children:["Default: ",d.defaultValue]}),d.hint?Ee(Ke,{color:"gray",children:d.hint}):null]}),It(Rt,{marginTop:1,children:[Ee(Ke,{color:"gray",children:"> "}),Ee(Py,{isDisabled:a,defaultValue:i[d.key]??"",placeholder:d.defaultValue,onSubmit:b=>{g(b)}},d.key)]}),Ee(Rt,{marginTop:1,children:Ee(Ke,{color:"gray",children:"Enter to continue, Ctrl+C to exit"})}),a?Ee(Rt,{marginTop:1,children:Ee(ky,{label:"Saving config..."})}):null,p?Ee(Rt,{marginTop:1,children:It(Ay,{variant:"error",children:["Failed to save config: ",p]})}):null]}):null});function Bc(e){let t=e.finalText?.trim();return t||e.steps.map(n=>n.assistantText??"").join("").trim()}function Iy(e){return e==="ok"||e==="error"||e==="cancelled"?e:void 0}function Oy(e){if(e.resultStatus)return e.resultStatus==="success"?ke.SUCCESS:ke.ERROR}function Ly(e,t,n){return{index:-(n+1),userInput:e.input??"",steps:(e.steps??[]).map(r=>({index:r.step,assistantText:r.assistantText??"",thinking:r.thinking,action:r.action,parallelActions:r.parallelActions,observation:r.observation,toolStatus:Oy(r)})),status:Iy(e.status),errorMessage:e.errorMessage,tokenUsage:e.tokenUsage,finalText:Bc(e),sequence:t}}function rr(e){let t=hn(e,"history.log"),n=[...t.turns].sort((i,c)=>i.turn-c.turn),r=[];for(let i of n){let c=(i.input??"").trim();c&&r.push({role:"user",content:c});let a=Bc(i);a&&r.push({role:"assistant",content:a})}let o=0,s=n.map((i,c)=>(o+=1,Ly(i,o,c)));return{summary:t.summary,messages:r,turns:s,maxSequence:o}}function Vo(){return{turns:[],historicalTurns:[],systemMessages:[],sequence:0}}function Ny(e,t){return{index:e,userInput:"",steps:[],sequence:t}}function or(e,t){if(e.length>t)return e;let n=e.slice();for(;n.length<=t;)n.push({index:n.length,assistantText:""});return n}function Ot(e,t,n){let r=e.turns.slice(),o=r.findIndex(i=>i.index===t);if(o===-1){let i=e.sequence+1;return r.push(n(Ny(t,i))),{turns:r,sequence:i}}let s=r[o];return s?(r[o]=n(s),{turns:r,sequence:e.sequence}):{turns:r,sequence:e.sequence}}function $y(e){return{id:`${Date.now()}-${Math.random().toString(16).slice(2)}`,title:e.title,content:e.content,tone:e.tone??"info",sequence:e.sequence}}function jc(e,t){switch(t.type){case"append_system_message":{let n=e.sequence+1;return{...e,sequence:n,systemMessages:[...e.systemMessages,$y({title:t.title,content:t.content,tone:t.tone,sequence:n})]}}case"turn_start":{let n=Ot(e,t.turn,r=>({...r,index:t.turn,userInput:t.input,steps:[],finalText:void 0,status:void 0,errorMessage:void 0,tokenUsage:void 0,startedAt:Date.now(),durationMs:void 0,contextPromptTokens:t.promptTokens??r.contextPromptTokens}));return{...e,turns:n.turns,sequence:n.sequence}}case"context_usage":{let n=Ot(e,t.turn,r=>{let o=or(r.steps,t.step),s=o[t.step];return s?(o[t.step]={...s,contextPromptTokens:t.promptTokens},{...r,contextPromptTokens:t.promptTokens,steps:o}):r});return{...e,turns:n.turns,sequence:n.sequence}}case"assistant_chunk":{let n=Ot(e,t.turn,r=>{let o=or(r.steps,t.step),s=o[t.step];if(!s)return r;let i=`${s.assistantText}${t.chunk}`;return i===s.assistantText?r:(o[t.step]={...s,assistantText:i},{...r,steps:o})});return{...e,turns:n.turns,sequence:n.sequence}}case"tool_action":{let n=Ot(e,t.turn,r=>{let o=or(r.steps,t.step),s=o[t.step];return s?(o[t.step]={...s,action:t.action,thinking:t.thinking,parallelActions:t.parallelActions&&t.parallelActions.length>1?t.parallelActions:void 0,toolStatus:ke.EXECUTING},{...r,steps:o}):r});return{...e,turns:n.turns,sequence:n.sequence}}case"tool_observation":{let n=Ot(e,t.turn,r=>{let o=or(r.steps,t.step),s=o[t.step];return s?(o[t.step]={...s,observation:t.observation,toolStatus:t.toolStatus,parallelToolStatuses:t.parallelToolStatuses},{...r,steps:o}):r});return{...e,turns:n.turns,sequence:n.sequence}}case"turn_final":{let n=Ot(e,t.turn,r=>{let o=r.startedAt??Date.now(),s=Math.max(0,Date.now()-o),i=t.tokenUsage?.prompt??r.contextPromptTokens;return{...r,finalText:t.finalText,status:t.status,errorMessage:t.errorMessage,tokenUsage:t.turnUsage,contextPromptTokens:i,startedAt:o,durationMs:s}});return{...e,turns:n.turns,sequence:n.sequence}}case"replace_history":return{...e,historicalTurns:t.turns,sequence:Math.max(e.sequence,t.maxSequence)};case"clear_current_timeline":return{...e,turns:[],systemMessages:[]};case"reset_all":return Vo();default:return e}}import{dirname as sr,join as zc,resolve as Dy}from"path";import{statSync as Uy,existsSync as Gc,readFileSync as Hy}from"fs";import{readFile as Fy}from"fs/promises";import{get as By}from"https";import{fileURLToPath as jy}from"url";function Wc(e){let t=e.trim().replace(/^v/i,""),[n="",r]=t.split("-",2),o=n.split(".").map(s=>Number(s));return o.length<3||o.some(s=>!Number.isFinite(s))?null:{major:o[0]??0,minor:o[1]??0,patch:o[2]??0,prerelease:r??null}}function Wy(e,t){let n=Wc(e),r=Wc(t);return!n||!r?!1:n.major!==r.major?n.major>r.major:n.minor!==r.minor?n.minor>r.minor:n.patch!==r.patch?n.patch>r.patch:n.prerelease&&!r.prerelease?!1:!n.prerelease&&r.prerelease?!0:n.prerelease&&r.prerelease?n.prerelease>r.prerelease:!1}function Kc(){try{let t=jy(import.meta.url);return sr(t)}catch{}let e=Dy(process.argv[1]??process.cwd());try{return Uy(e).isFile()?sr(e):e}catch{return process.cwd()}}async function zy(e){let t=zc(e,"package.json");if(!Gc(t))return null;let n=await Fy(t,"utf8"),r=JSON.parse(n);return!r.name||!r.version?null:{name:r.name,version:r.version}}function Gy(e){let t=zc(e,"package.json");if(!Gc(t))return null;try{let n=Hy(t,"utf8"),r=JSON.parse(n);return!r.name||!r.version?null:{name:r.name,version:r.version}}catch{return null}}async function Ky(){let e=Kc();for(;;){let t=await zy(e);if(t&&t.name==="@memo-code/memo")return t;let n=sr(e);if(n===e)break;e=n}return null}function ir(){let e=Kc();for(;;){let t=Gy(e);if(t&&t.name==="@memo-code/memo")return t;let n=sr(e);if(n===e)break;e=n}return null}async function qy(e,t=1500){let r=`https://registry.npmjs.org/${encodeURIComponent(e)}/latest`;return new Promise(o=>{let s=By(r,{timeout:t},i=>{if(i.statusCode&&i.statusCode>=400){i.resume(),o(null);return}let c=[];i.on("data",a=>c.push(a)),i.on("end",()=>{try{let a=JSON.parse(Buffer.concat(c).toString("utf8"));o(a.version??null)}catch{o(null)}})});s.on("timeout",()=>{s.destroy(),o(null)}),s.on("error",()=>o(null))})}async function qc(){let e=await Ky();if(!e)return null;let t=await qy(e.name);return!t||!Wy(t,e.version)?null:{current:e.version,latest:t}}import{readFile as Vy}from"fs/promises";import{dirname as Xy,join as Jy}from"path";import{fileURLToPath as Yy}from"url";var Qy=/{{\s*([\w.-]+)\s*}}/g;function Zy(e,t){return e.replace(Qy,(n,r)=>t[r]??"")}async function Xo(e,t={}){let n=Xy(Yy(import.meta.url)),r=Jy(n,"task-prompts",`${e}.md`),o=await Vy(r,"utf-8");return Zy(o,t)}import{spawn as eS}from"child_process";var tS=12e3,nS=new Set(["WRITE","MAINTAIN","ADMIN"]),rS=["pull_request_read","list_pull_requests","search_pull_requests","add_issue_comment","add_comment_to_pending_review","issue_read","get_me"];function oS(e){return e.replace(/\\/g,"/").toLowerCase()}function sS(e,t){if(e.toLowerCase().includes("github"))return!0;if("command"in t){let o=oS(t.command),s=(t.args??[]).join(" ").toLowerCase();return!!(o.includes("github")||o.includes("gh-")||s.includes("github")||s.includes("gh-"))}let r=t.url.toLowerCase();return r.includes("github")||r.includes("api.github.com")}function iS(e,t){let n=new Set(t),r=Object.entries(e).filter(([i,c])=>sS(i,c)).map(([i])=>i),o=r.find(i=>n.has(i))??null,s=r.filter(i=>!n.has(i));return{active:o,inactiveCandidates:s}}function aS(e){let t=new Map;for(let n of e)for(let r of rS){let o=`_${r}`;if(!n.endsWith(o))continue;let s=n.slice(0,-o.length);s&&t.set(s,(t.get(s)??0)+1)}return Array.from(t.entries()).filter(([,n])=>n>=2).sort((n,r)=>r[1]-n[1]).map(([n])=>n)}async function Jo(e,t,n,r=tS){return new Promise(o=>{let s=eS(e,t,{cwd:n,env:process.env,stdio:["ignore","pipe","pipe"]}),i="",c="",a=!1,l=u=>{a||(a=!0,clearTimeout(p),o(u))},p=setTimeout(()=>{s.kill("SIGTERM"),l({ok:!1,stdout:i,stderr:c,code:null,errorMessage:`${e} ${t.join(" ")} timed out after ${r}ms`})},r);s.stdout.on("data",u=>{i+=u.toString()}),s.stderr.on("data",u=>{c+=u.toString()}),s.on("error",u=>{l({ok:!1,stdout:i,stderr:c,code:null,errorMessage:u.message})}),s.on("close",u=>{l({ok:u===0,stdout:i,stderr:c,code:u})})})}async function cS(e){if(!(await Jo("gh",["--version"],e)).ok)return{ok:!1,reason:"GitHub MCP not available, and GitHub CLI (gh) is not installed or not executable. Install gh: https://cli.github.com/"};let n=await Jo("gh",["auth","status","-h","github.com"],e);if(!n.ok)return{ok:!1,reason:`GitHub CLI is installed but not authenticated for github.com. Run: gh auth login -h github.com (detail: ${n.stderr.trim()||n.stdout.trim()||n.errorMessage||"unknown error"})`};let r=await Jo("gh",["repo","view","--json","nameWithOwner,viewerPermission"],e);if(!r.ok)return{ok:!1,reason:`GitHub CLI authentication works, but this directory is not a readable GitHub repo for gh (detail: ${r.stderr.trim()||r.stdout.trim()||r.errorMessage||"unknown error"})`};let o;try{o=JSON.parse(r.stdout)}catch{return{ok:!1,reason:"Failed to parse `gh repo view` output. Try upgrading gh and retry."}}let s=o.nameWithOwner?.trim(),i=o.viewerPermission?.trim().toUpperCase();return!s||!i?{ok:!1,reason:"GitHub CLI did not return repository or permission info from `gh repo view`."}:nS.has(i)?{ok:!0,repository:s,viewerPermission:i}:{ok:!1,reason:`GitHub CLI connected to ${s}, but permission is ${i}. PR review comments require WRITE/MAINTAIN/ADMIN permission.`}}async function Vc(e){let t=aS(e.availableToolNames??[]),n=e.activeMcpServerNames.find(l=>t.includes(l))??null,r=l=>{let p=e.availableToolNames;if(!p||p.length===0)return!0;let u=`${l}_`;return p.some(d=>d.startsWith(u))};if(n&&r(n))return{kind:"github_mcp",strategy:"github_mcp",details:`Using active GitHub MCP server \`${n}\` (detected from loaded MCP tool signatures).`,mcpServerPrefix:n};let{active:o,inactiveCandidates:s}=iS(e.mcpServers,e.activeMcpServerNames);if(o&&r(o))return{kind:"github_mcp",strategy:"github_mcp",details:`Using active GitHub MCP server \`${o}\`.`,mcpServerPrefix:o};let i=await cS(e.cwd);if(i.ok){let l=s.length?` GitHub MCP server(s) configured but inactive in this session: ${s.join(", ")}.`:"";return{kind:"gh_cli",strategy:"gh_cli",details:`Using gh CLI on repo \`${i.repository}\` with permission \`${i.viewerPermission}\`.${l}`,repository:i.repository,viewerPermission:i.viewerPermission}}let c=s.length?` Also found configured but inactive GitHub MCP server(s): ${s.join(", ")}. Start a new session and activate one of them, or fix gh CLI access.`:"",a=o&&!r(o)?` Active MCP server \`${o}\` is configured, but no tools with prefix \`${o}_\` are currently loaded in this session.`:"";return{kind:"unavailable",reason:`${i.reason}${a}${c}`}}import{jsx as nt,jsxs as hS}from"react/jsx-runtime";function mS(e,t){if(e.length===0)return[];if(t===void 0)return[...e];if(t.length===0)return[];let n=new Set(e),r=t.filter(o=>n.has(o));return r.length>0?r:[...e]}function fS(e,t){if(e.length===0)return[];if(t.length===0)return[];let n=new Set(e);return t.filter(r=>n.has(r))}function gS(){try{process.stdout?.isTTY&&process.stdout.write("\x1Bc")}catch{}}function Jc({sessionOptions:e,providerName:t,model:n,configPath:r,mcpServers:o,cwd:s,sessionsDir:i,providers:c,modelProfiles:a,dangerous:l=!1,needsSetup:p=!1,initialHistory:u}){let{exit:d}=dS(),m=sn(()=>Object.keys(o??{}).sort(),[o]),g=sn(()=>mS(m,e.activeMcpServers),[m,e.activeMcpServers]),y=e.toolPermissionMode??(l?Se.FULL:Se.ONCE),[b,U]=uS(jc,void 0,Vo),[T,F]=ee(t),[R,I]=ee(n),[G,ie]=ee(c),[Re,ae]=ee(a),[z,M]=ee(y),P=X(f=>Tt({model_profiles:Re},f),[Re]),[ne,ce]=ee({...e,providerName:t,contextWindow:P({name:t,model:n}),dangerous:y===Se.FULL,toolPermissionMode:y}),[H,le]=ee(!1),[J,A]=ee([]),[O,re]=ee(null),[_e,ge]=ee(null),[qe,De]=ee(P({name:t,model:n})),[ot,ve]=ee(0),[be,k]=ee(p),[L,cn]=ee(!p&&m.length>0),[Te,K]=ee(g),[$,$t]=ee(null),[N,st]=ee(null),ue=ar(null),Ve=ar(null),gt=ar(null),[Q,Xe]=ee(null),it=ar(null),w=sn(()=>ir(),[]),_=X(f=>{U(f)},[]);Nt(()=>{u&&(_({type:"clear_current_timeline"}),_({type:"replace_history",turns:u.turns,maxSequence:u.maxSequence}),ge(u.messages),u.summary.trim()&&_({type:"append_system_message",title:"History",content:u.summary,tone:"info"}))},[_,u]),Nt(()=>{be||(K(g),cn(m.length>0))},[be,g,m.length]);let S=X((f,v,C="info")=>{_({type:"append_system_message",title:f,content:v,tone:C})},[_]),Z=sn(()=>({onAssistantStep:(f,v)=>{let C=Ve.current;C&&_({type:"assistant_chunk",turn:C,step:v,chunk:f})},requestApproval:z===Se.FULL||z===Se.NONE?void 0:f=>new Promise(v=>{Uc(f),Xe(f),it.current=v}),hooks:{onTurnStart:({turn:f,input:v,promptTokens:C})=>{Ve.current=f;let q=gt.current;q&&(gt.current=null);let Ie=q??v;C&&C>0&&ve(C),_({type:"turn_start",turn:f,input:Ie,promptTokens:C})},onContextUsage:({turn:f,step:v,promptTokens:C,phase:q})=>{ve(C),_({type:"context_usage",turn:f,step:v,promptTokens:C,phase:q})},onContextCompacted:({reason:f,status:v,beforeTokens:C,afterTokens:q,reductionPercent:Ie,errorMessage:dr})=>{v==="success"&&ve(q);let mr=f==="manual"?"manual command":"auto trigger";if(v==="success"){S("Context compacted",`Compacted by ${mr}: ${C} -> ${q} tokens (${Ie.toFixed(2)}% reduced).`);return}if(v==="skipped"){S("Context compacted",`Skipped (${mr}): nothing to compact.`,"warning");return}S("Context compacted",`Failed (${mr}): ${dr??"unknown error"}`,"warning")},onAction:({turn:f,step:v,action:C,thinking:q,parallelActions:Ie})=>{_({type:"tool_action",turn:f,step:v,action:C,thinking:q,parallelActions:Ie})},onObservation:({turn:f,step:v,observation:C,resultStatus:q,parallelResultStatuses:Ie})=>{_({type:"tool_observation",turn:f,step:v,observation:C,toolStatus:Ro(q),parallelToolStatuses:Ga(Ie)})},onFinal:({turn:f,finalText:v,status:C,errorMessage:q,turnUsage:Ie,tokenUsage:dr})=>{_({type:"turn_final",turn:f,finalText:v,status:C,errorMessage:q,turnUsage:Ie,tokenUsage:dr}),le(!1)}}}),[S,_,z]);Nt(()=>{let f=!1;return(async()=>{if(!(be||L))try{let v=ue.current;v&&await v.close();let C=await zn(Z,ne);if(f){await C.close();return}ue.current=C,st(C),re(C.historyFilePath??null)}catch(v){if(f)return;ue.current=null,st(null),re(null),le(!1),S("Session",`Failed to create session: ${v.message}`,"error")}})(),()=>{f=!0}},[S,Z,L,ne,be]),Nt(()=>{let f=!1;return(async()=>{let v=await qc();f||!v||S("Update",`Update available: v${v.latest}. Run: npm install -g @memo-code/memo@latest`)})(),()=>{f=!0}},[S]),Nt(()=>()=>{ue.current&&ue.current.close()},[]);let oe=X(async()=>{let f=it.current;f&&(f("deny"),it.current=null),Q&&Xe(null),ue.current&&await ue.current.close(),$t("Bye!"),setTimeout(()=>d(),250)},[d,Q]),Ue=X(()=>{if(H){S("Clear","Cancel current run before clearing timeline.","warning");return}if(Q){S("Clear","Resolve current approval request before clearing timeline.","warning");return}_({type:"clear_current_timeline"}),ge(null),ve(0),gS()},[S,H,_,Q]),pr=X(()=>{if(H){S("New Session","Cancel current run before starting a new session.","warning");return}if(Q){S("New Session","Resolve current approval request before starting a new session.","warning");return}_({type:"reset_all"}),ge(null),ve(0),Ve.current=null,ce(f=>({...f,sessionId:Lt()})),S("New Session","Started a fresh session.")},[S,H,_,Q]),Dt=X(async f=>{try{let v=await te();await we(v.configPath,{...v.config,current_provider:f})}catch(v){S("Config",`Failed to persist provider: ${v.message}`,"warning")}},[S]),x=X(async f=>{if(H){S("Model switch","Cancel current run before switching models.","warning");return}if(f.name===T&&f.model===R){S("Model switch",`Already using ${f.name} (${f.model}).`);return}_({type:"reset_all"}),ve(0),Ve.current=null;let v=P(f);F(f.name),I(f.model),De(v),ce(C=>({...C,sessionId:Lt(),providerName:f.name,contextWindow:v})),await Dt(f.name),S("Model switch",`Switched to ${f.name} (${f.model}).`)},[S,H,R,T,_,Dt,P]),E=X(f=>f===Se.NONE?"none (no tools)":f===Se.ONCE?"once (approval required)":"full (no approval)",[]),pe=X(f=>{if(H){S("Tools","Cancel current run before changing tool permission mode.","warning");return}if(Q){S("Tools","Resolve current approval request before changing tool permission mode.","warning");return}if(f===z){S("Tools",`Already using ${E(f)}.`);return}M(f),ce(v=>({...v,sessionId:Lt(),dangerous:f===Se.FULL,toolPermissionMode:f})),S("Tools",`Tool permission set to ${E(f)}.`)},[S,H,Q,E,z]),B=X(async f=>{try{let v=await te();await we(v.configPath,{...v.config,active_mcp_servers:f})}catch(v){S("MCP",`Failed to persist active MCP servers: ${v.message}`,"warning")}},[S]),xe=X((f,v)=>{let C=fS(m,f);K(C),cn(!1),ce(q=>({...q,sessionId:Lt(),activeMcpServers:C})),v&&B(C)},[m,B]),at=X(async f=>{if(H){S("History","Cancel current run before loading session history.","warning");return}if(Q){S("History","Resolve current approval request before loading session history.","warning");return}try{let v=await lS(f.sessionFile,"utf8"),C=rr(v);_({type:"clear_current_timeline"}),_({type:"replace_history",turns:C.turns,maxSequence:C.maxSequence}),ge(C.messages),le(!1),st(null),re(null),ve(0),Ve.current=null,ce(q=>({...q,sessionId:Lt()})),S("History",C.summary||f.input)}catch(v){S("History",`Failed to load ${f.sessionFile}: ${v.message}`,"error")}},[S,H,_,Q]),ht=X(()=>{H&&N?.cancelCurrentTurn?.()},[H,N]),ln=X(async()=>{if(H){S("Compact","Cancel current run before compacting context.","warning");return}if(Q){S("Compact","Resolve current approval request before compacting context.","warning");return}if(N)try{let f=await N.compactHistory("manual");ve(f.afterTokens)}catch(f){S("Compact",`Failed to compact context: ${f.message}`,"error")}},[S,H,Q,N]),un=X(async()=>{if(!N||H)return;let f=Ze($e.INIT);try{let v=await Xo("init_agents");A(C=>[...C,f]),le(!0),gt.current=f,await N.runTurn(v)}catch(v){le(!1),S("Init",`Failed to run init task: ${v.message}`,"error")}},[S,H,N]),Ut=X(async f=>{if(!N||H)return;if(z===Se.NONE){S("Review",'Tool permission mode is "none". Set `/tools once` or `/tools full` before running `/review`.',"warning");return}let v=`${Ze($e.REVIEW)} ${f}`;try{let C=await Vc({cwd:s,mcpServers:o,activeMcpServerNames:Te,availableToolNames:N.listToolNames?.()??[]});if(C.kind==="unavailable"){S("Review",C.reason,"error");return}let q=await Xo("review_pull_request",{pr_number:String(f),backend_strategy:C.strategy,backend_details:C.details,mcp_server_prefix:C.kind==="github_mcp"?C.mcpServerPrefix:"github"});A(Ie=>[...Ie,v]),S("Review",C.details),le(!0),gt.current=v,await N.runTurn(q)}catch(C){le(!1),S("Review",`Failed to run review task for PR #${f}: ${C.message}`,"error")}},[Te,S,H,s,o,N,z]),dl=X(async f=>{let v=f.trim();if(v){if(v.toLowerCase()===kc){await oe();return}if(v===Ze($e.INIT)){await un();return}if(!(!N||H)){A(C=>[...C,v]),le(!0);try{await N.runTurn(v)}catch(C){le(!1),S("Run",`Turn failed: ${C.message}`,"error")}}}},[S,H,oe,un,N]),ml=X(async()=>{try{let f=await te(),v=ut(f.config),C=Tt(f.config,v);ie(f.config.providers),ae(f.config.model_profiles),F(v.name),I(v.model),De(C),ce(q=>({...q,sessionId:Lt(),providerName:v.name,contextWindow:C,autoCompactThresholdPercent:f.config.auto_compact_threshold_percent})),k(!1),S("Setup",`Config saved to ${f.configPath}`)}catch(f){S("Setup",`Failed to reload config: ${f.message}`,"error")}},[S]);Nt(()=>{if(!N||!_e?.length)return;let f=N.history[0];f&&(N.history.splice(0,N.history.length,f,..._e),ge(null))},[_e,N]);let fl=X(f=>{let v=it.current;v&&(v(f),it.current=null),Xe(null)},[]),gl=Ka(ot,qe),hl=sn(()=>({providerName:T,model:R,cwd:s,sessionId:ne.sessionId??"unknown",mcpNames:Te,version:w?.version??"unknown"}),[Te,R,T,s,w?.version,ne.sessionId]);return $?nt(Xc,{children:nt(pS,{color:"green",children:$})}):be?nt(Fc,{configPath:r,onComplete:ml,onExit:oe}):L?nt($c,{serverNames:m,defaultSelected:g,onConfirm:xe,onExit:()=>{oe()}}):hS(Xc,{flexDirection:"column",children:[nt(ic,{header:hl,systemMessages:b.systemMessages,turns:b.turns,historicalTurns:b.historicalTurns}),nt(Ec,{disabled:!N||!!Q,busy:H,history:J,cwd:s,sessionsDir:i,currentSessionFile:O??void 0,providers:G,configPath:r,providerName:T,model:R,toolPermissionMode:z,mcpServers:o,onSubmit:f=>{dl(f)},onExit:()=>{oe()},onClear:Ue,onNewSession:pr,onCancelRun:ht,onCompact:()=>{ln()},onHistorySelect:f=>{at(f)},onModelSelect:f=>{x(f)},onSetToolPermission:pe,onReviewPullRequest:f=>{Ut(f)},onSystemMessage:S}),Q?nt(Nc,{request:Q,onDecision:fl}):null,nt(Oc,{busy:H,pendingApproval:!!Q,contextPercent:gl})]})}var yS=`
|
|
121
130
|
Usage:
|
|
122
131
|
memo mcp list [--json]
|
|
123
132
|
memo mcp get <name> [--json]
|
|
@@ -126,14 +135,14 @@ Usage:
|
|
|
126
135
|
memo mcp remove <name>
|
|
127
136
|
memo mcp login <name> [--scopes scope1,scope2]
|
|
128
137
|
memo mcp logout <name>
|
|
129
|
-
`;function
|
|
130
|
-
`)}function
|
|
131
|
-
${a}`)},onBrowserOpenFailure:()=>{console.log("Browser launch failed. Open the URL above manually.")}});console.log(`OAuth login completed for "${o}" (credentials stored in ${c.backend}).`)}catch(c){console.error(
|
|
132
|
-
`),{...t,config:u,needsSetup:!1}}finally{s.close()}}async function
|
|
138
|
+
`;function cr(){console.log(yS.trim())}function Yc(e){return e instanceof Error?e.message:String(e)}function SS(e){let t=e.indexOf("=");if(t<=0)return null;let n=e.slice(0,t).trim(),r=e.slice(t+1);return n?{key:n,value:r}:null}function Qc(e,t,n){let r=[];if(r.push(`${e}`),n&&r.push(` auth_status: ${n}`),"url"in t){r.push(` type: ${t.type??"streamable_http"}`),r.push(` url: ${t.url}`),t.bearer_token_env_var&&r.push(` bearer_token_env_var: ${t.bearer_token_env_var}`);let o=t.http_headers??t.headers;o&&Object.keys(o).length>0&&r.push(` headers: ${Object.entries(o).map(([s,i])=>`${s}=${i}`).join(", ")}`)}else r.push(` type: ${t.type??"stdio"}`),r.push(` command: ${t.command}`),t.args&&t.args.length>0&&r.push(` args: ${t.args.join(" ")}`),t.env&&Object.keys(t.env).length>0&&r.push(` env: ${Object.entries(t.env).map(([o,s])=>`${o}=${s}`).join(", ")}`);return r.join(`
|
|
139
|
+
`)}function _S(e){let t=e.shift();if(!t)return{error:"Missing server name."};let n,r,o={},s=[];for(let i=0;i<e.length;i+=1){let c=e[i];if(c){if(c==="--"){s=e.slice(i+1);break}if(c==="--url"){let a=e[i+1];if(!a)return{error:"Missing value for --url."};n=a,i+=1;continue}if(c==="--bearer-token-env-var"){let a=e[i+1];if(!a)return{error:"Missing value for --bearer-token-env-var."};r=a,i+=1;continue}if(c==="--env"){let a=e[i+1];if(!a)return{error:"Missing value for --env (KEY=VALUE)."};let l=SS(a);if(!l)return{error:"Invalid --env format. Use KEY=VALUE."};o[l.key]=l.value,i+=1;continue}return c==="--help"||c==="-h"?{error:""}:{error:`Unknown option: ${c}`}}}return n?s.length>0?{error:"Use either --url or a stdio command, not both."}:Object.keys(o).length>0?{error:"--env is only supported with stdio servers."}:{options:{name:t,url:n,bearerTokenEnvVar:r}}:r?{error:"--bearer-token-env-var is only supported with HTTP servers."}:s.length===0?{error:"Missing stdio command. Use `-- <command...>`."}:{options:{name:t,command:s[0],args:s.slice(1),env:Object.keys(o).length>0?o:void 0}}}function vS(e){let[t,...n]=e;return!t||t==="--help"||t==="-h"||t==="help"?{command:"help",rest:[]}:{command:t,rest:n}}function Yo(e,t=[]){let n=new Set(t);for(let r=0;r<e.length;r+=1){let o=e[r];if(o){if(o.startsWith("--")){n.has(o)&&(r+=1);continue}return o}}return null}function TS(e){return e.split(/[,\s]+/g).map(t=>t.trim()).filter(Boolean)}function wS(e){let t,n;for(let r=0;r<e.length;r+=1){let o=e[r];if(o){if(o==="--help"||o==="-h")return{showHelp:!0};if(o==="--scopes"){let s=e[r+1];if(!s)return{error:"Missing value for --scopes."};let i=TS(s);if(i.length===0)return{error:"Invalid --scopes value. Use comma-separated scopes."};n=i,r+=1;continue}if(o.startsWith("--"))return{error:`Unknown option: ${o}`};if(!t){t=o;continue}return{error:`Unexpected argument: ${o}`}}}return{name:t,scopes:n}}function Qo(e){return{memoHome:e.home,storeMode:e.config.mcp_oauth_credentials_store_mode,callbackPort:e.config.mcp_oauth_callback_port}}async function Zc(e){let{command:t,rest:n}=vS(e);if(t==="help"){cr();return}if(t==="list"){let r=n.includes("--json"),o=await te(),s=o.config.mcp_servers??{},i=Object.keys(s),c=Qo(o),a=new Map;if(await Promise.all(i.map(async l=>{let p=s[l];if(p)try{let u=await Fr(p,c);a.set(l,u)}catch{a.set(l,"unsupported")}})),r){let l={};for(let p of i){let u=s[p];u&&(l[p]={...u,auth_status:a.get(p)??"unsupported"})}console.log(JSON.stringify(l,null,2));return}if(i.length===0){console.log('No MCP servers configured. Add one with "memo mcp add".');return}console.log(`MCP servers (${i.length}):`);for(let l of i){let p=s[l];p&&console.log(Qc(l,p,a.get(l)??"unsupported"))}return}if(t==="get"){let r=n.includes("--json"),o=Yo(n);if(!o){console.error("Missing server name."),process.exitCode=1;return}let i=(await te()).config.mcp_servers?.[o];if(!i){console.error(`Unknown MCP server "${o}".`),process.exitCode=1;return}if(r){console.log(JSON.stringify(i,null,2));return}console.log(Qc(o,i));return}if(t==="add"){let r=_S(n);if(r.error!==void 0){r.error&&(console.error(r.error),process.exitCode=1),cr();return}let o=r.options;if(!o)return;if(o.url)try{new URL(o.url)}catch{console.error("Invalid URL."),process.exitCode=1;return}let s=await te(),i={...s.config.mcp_servers??{}};if(i[o.name]){console.error(`MCP server "${o.name}" already exists.`),process.exitCode=1;return}let c;o.url?c={type:"streamable_http",url:o.url,...o.bearerTokenEnvVar?{bearer_token_env_var:o.bearerTokenEnvVar}:{}}:c={command:o.command,args:o.args&&o.args.length>0?o.args:void 0,env:o.env},i[o.name]=c,await we(s.configPath,{...s.config,mcp_servers:i}),console.log(`Added MCP server "${o.name}".`);return}if(t==="remove"){let r=Yo(n);if(!r){console.error("Missing server name."),process.exitCode=1;return}let o=await te(),s={...o.config.mcp_servers??{}};if(!s[r]){console.error(`Unknown MCP server "${r}".`),process.exitCode=1;return}delete s[r],await we(o.configPath,{...o.config,mcp_servers:s}),console.log(`Removed MCP server "${r}".`);return}if(t==="login"){let r=wS(n);if(r.showHelp){cr();return}if(r.error){console.error(r.error),process.exitCode=1;return}let o=r.name;if(!o){console.error("Missing server name."),process.exitCode=1;return}let s=await te(),i=s.config.mcp_servers?.[o];if(!i){console.error(`Unknown MCP server "${o}".`),process.exitCode=1;return}if(!("url"in i)){console.error("OAuth login only applies to streamable HTTP servers."),process.exitCode=1;return}console.log(`Starting OAuth login for "${o}"...`);try{let c=await Br({serverName:o,config:i,scopes:r.scopes,settings:Qo(s),onAuthorizationUrl:a=>{console.log(`Open this URL to authorize:
|
|
140
|
+
${a}`)},onBrowserOpenFailure:()=>{console.log("Browser launch failed. Open the URL above manually.")}});console.log(`OAuth login completed for "${o}" (credentials stored in ${c.backend}).`)}catch(c){console.error(Yc(c)),process.exitCode=1}return}if(t==="logout"){let r=Yo(n);if(!r){console.error("Missing server name."),process.exitCode=1;return}let o=await te(),s=o.config.mcp_servers?.[r];if(!s){console.error(`Unknown MCP server "${r}".`),process.exitCode=1;return}if(!("url"in s)){console.error("OAuth logout only applies to streamable HTTP servers."),process.exitCode=1;return}try{(await jr({config:s,settings:Qo(o)})).removed?console.log(`Removed OAuth credentials for "${r}".`):console.log(`No OAuth credentials stored for "${r}".`)}catch(i){console.error(Yc(i)),process.exitCode=1}return}console.error(`Unknown subcommand: ${t}`),cr(),process.exitCode=1}function el(e){let t={dangerous:!1,showVersion:!1,once:!1,prev:!1},n=[];for(let r=0;r<e.length;r++){let o=e[r];if(o!==void 0){if(o==="--version"||o==="-v"){t.showVersion=!0;continue}if(o==="--once"||o==="-once"){t.once=!0;continue}if(o==="--prev"||o==="-prev"){t.prev=!0;continue}if(o==="--dangerous"||o==="-d"){t.dangerous=!0;continue}n.push(o)}}return{question:n.join(" "),options:t}}var CS=["mcp","web"];function bS(e){return e.length===0?{token:null,offset:0}:e[0]==="--"?{token:e[1]??null,offset:2}:{token:e[0]??null,offset:1}}function xS(e){return CS.includes(e)}function tl(e){let{token:t,offset:n}=bS(e);return t&&xS(t)?{kind:"subcommand",name:t,args:e.slice(n)}:{kind:"default",args:e}}import{jsx as YS}from"react/jsx-runtime";async function ul(e){let t=await te();if(!t.needsSetup)return t;let n=t.config.providers[0],o=[n?.env_api_key,"OPENAI_API_KEY","DEEPSEEK_API_KEY"].filter(Boolean).some(c=>!!process.env[c]);if(n&&o)return await we(t.configPath,t.config),console.log(`Detected API key in env. Wrote default provider (${n.name}) to ${t.configPath}`),{...t,needsSetup:!1};if(e==="tui")return t;let s=jS({input:WS,output:zS}),i=async(c,a)=>(await s.question(c)).trim()||a;try{console.log("No provider config found. Please answer the prompts:");let c=await i("Provider name [deepseek]: ","deepseek"),a=await i("API key env var [DEEPSEEK_API_KEY]: ","DEEPSEEK_API_KEY"),l=await i("Model name [deepseek-chat]: ","deepseek-chat"),p=await i("Base URL [https://api.deepseek.com]: ","https://api.deepseek.com"),u={current_provider:c,providers:[{name:c,env_api_key:a,model:l,base_url:p||void 0}]};return await we(t.configPath,u),console.log(`Config written to ${t.configPath}
|
|
141
|
+
`),{...t,config:u,needsSetup:!1}}finally{s.close()}}async function pl(e,t){let r=(await Kn({sessionsDir:e,cwd:t,limit:1}))[0];if(!r)return null;let o=await BS(r.sessionFile,"utf8");return rr(o)}function KS(e,t){if(!t.length)return;let n=e.history[0];n&&e.history.splice(0,e.history.length,n,...t)}async function qS(e){let t=await ul("plain"),n=ut(t.config),r=Tt(t.config,n),s={sessionId:ll(),mode:"interactive",contextWindow:r,autoCompactThresholdPercent:t.config.auto_compact_threshold_percent,activeMcpServers:t.config.active_mcp_servers,dangerous:e.options.dangerous},i=Kt(t,s),c=e.options.prev?await pl(i,process.cwd()):null;if(e.options.prev&&!c){console.error("No previous session found for current directory."),process.exitCode=1;return}e.options.dangerous&&console.log("\u26A0\uFE0F DANGEROUS MODE: All tool approvals are bypassed!");let a={requestApproval:e.options.dangerous?void 0:u=>(console.log(`
|
|
133
142
|
[approval required] ${u.toolName}: ${u.reason}`),console.log("[approval] Run with --dangerous to bypass approval"),Promise.resolve("deny")),hooks:{onAction:({action:u})=>{console.log(`
|
|
134
|
-
[tool] ${u.tool}`),u.input!==void 0&&console.log(`[input] ${JSON.stringify(u.input)}`)},onObservation:()=>{}}},l=await
|
|
143
|
+
[tool] ${u.tool}`),u.input!==void 0&&console.log(`[input] ${JSON.stringify(u.input)}`)},onObservation:()=>{}}},l=await zn(a,s);c&&(KS(l,c.messages),console.log("[session] Continued from previous session context."));let p=e.question;if(!p&&!process.stdin.isTTY&&(p=await JS()),!p){console.error("No input provided. Pass a question or use stdin."),await l.close();return}try{console.log(`User: ${p}
|
|
135
144
|
`);let u=await l.runTurn(p);console.log(`
|
|
136
145
|
${u.finalText}`),console.log(`
|
|
137
146
|
[tokens] prompt=${u.tokenUsage.prompt} completion=${u.tokenUsage.completion} total=${u.tokenUsage.total}`),console.log(`
|
|
138
|
-
provider=${n.name} model=${n.model}`)}catch(u){console.error(`Run failed: ${u.message}`)}finally{await l.close()}}async function
|
|
139
|
-
`)),await
|
|
147
|
+
provider=${n.name} model=${n.model}`)}catch(u){console.error(`Run failed: ${u.message}`)}finally{await l.close()}}async function VS(e){let t=await ul("tui"),n=ut(t.config),r=Tt(t.config,n),s={sessionId:ll(),mode:"interactive",contextWindow:r,autoCompactThresholdPercent:t.config.auto_compact_threshold_percent,activeMcpServers:t.config.active_mcp_servers,dangerous:e.options.dangerous},i=Kt(t,s),c=e.options.prev?await pl(i,process.cwd()):null;if(e.options.prev&&!c){console.error("No previous session found for current directory."),process.exitCode=1;return}e.options.dangerous&&(console.log("\u26A0\uFE0F DANGEROUS MODE: All tool approvals are bypassed!"),console.log(` Use with caution.
|
|
148
|
+
`)),await GS(YS(Jc,{sessionOptions:s,providerName:n.name,model:n.model,configPath:t.configPath,mcpServers:t.config.mcp_servers??{},cwd:process.cwd(),sessionsDir:i,providers:t.config.providers,modelProfiles:t.config.model_profiles,dangerous:e.options.dangerous,needsSetup:t.needsSetup,initialHistory:c??void 0}),{exitOnCtrlC:!1,patchConsole:!1}).waitUntilExit()}async function XS(){let e=process.argv.slice(2),t=tl(e);if(t.kind==="subcommand"){if(t.name==="mcp"){await Zc(t.args);return}if(t.name==="web"){let{runWebCommand:o}=await Promise.resolve().then(()=>(cl(),al));await o(t.args);return}}let n=el(t.args);if(n.options.showVersion){let s=ir()?.version??"unknown";console.log(s);return}let r=process.stdin.isTTY&&process.stdout.isTTY;if(n.options.once||!r){await qS(n);return}await VS(n)}XS();async function JS(){return new Promise(e=>{let t="";process.stdin.setEncoding("utf8"),process.stdin.on("data",n=>{t+=n}),process.stdin.on("end",()=>{e(t.trim())}),process.stdin.resume()})}
|