@vpxa/aikit 0.1.180 → 0.1.182

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.
@@ -13,7 +13,7 @@ import{mkdir as e,readFile as t,readdir as n,rm as r,stat as i,writeFile as a}fr
13
13
  `),n.push(`| Pattern | Confidence | Count |`),n.push(`|---------|-----------|-------|`);for(let t of e.patterns)n.push(`| ${t.name} | ${t.confidence} | ${t.count} |`)}else t===`normal`&&n.push(`\n**Patterns:** ${e.patterns.map(e=>e.name).join(`, `)}`);return n.join(`
14
14
  `)}const ct=/^[a-zA-Z0-9_./\-~^@{}]+$/;function lt(e){let{from:t,to:n=`HEAD`,format:r=`grouped`,includeBreaking:i=!0,cwd:a=process.cwd()}=e;if(!ct.test(t))throw Error(`Invalid git ref: ${t}`);if(!ct.test(n))throw Error(`Invalid git ref: ${n}`);let o;try{o=C(`git`,[`log`,`${t}..${n}`,`--format=%H%s%b%an%ai`],{cwd:a,encoding:`utf8`,maxBuffer:10*1024*1024,timeout:3e4})}catch{throw Error(`Git log failed. Ensure "${t}" and "${n}" are valid refs.`)}let s=o.split(``).map(e=>e.trim()).filter(Boolean).map(e=>{let[t=``,n=``,r=``,i=``,a=``]=e.split(``),o=n.match(/^(\w+)(?:\(([^)]*)\))?(!)?:\s*(.+)/);return{hash:t.slice(0,8),type:o?.[1]??`other`,scope:o?.[2]??``,subject:o?.[4]??n,body:r.trim(),author:i.trim(),date:a.trim().split(` `)[0],breaking:!!(o?.[3]||/BREAKING[\s-]CHANGE/i.test(r))}}),c={},l=0;for(let e of s)c[e.type]=(c[e.type]??0)+1,e.breaking&&l++;return{entries:s,markdown:ut(s,r,i),stats:{total:s.length,breaking:l,types:c}}}function ut(e,t,n){let r=[`# Changelog`,``];if(n){let t=e.filter(e=>e.breaking);if(t.length>0){r.push(`## Breaking Changes`,``);for(let e of t)r.push(`- ${e.subject} (${e.hash})`);r.push(``)}}if(t===`grouped`){let t={};for(let n of e)t[n.type]||(t[n.type]=[]),t[n.type].push(n);let n=[`feat`,`fix`,`refactor`,`perf`,`test`,`docs`,`chore`],i={feat:`Features`,fix:`Bug Fixes`,refactor:`Refactoring`,perf:`Performance`,test:`Tests`,docs:`Documentation`,chore:`Chores`,other:`Other`};for(let e of[...n,...Object.keys(t).filter(e=>!n.includes(e))])if(t[e]?.length){r.push(`## ${i[e]??e}`,``);for(let n of t[e]){let e=n.scope?`**${n.scope}:** `:``;r.push(`- ${e}${n.subject} (${n.hash})`)}r.push(``)}}else if(t===`chronological`)for(let t of e){let e=t.scope?`(${t.scope}) `:``;r.push(`- \`${t.date}\` ${t.type}: ${e}${t.subject} (${t.hash})`)}else{let t={};for(let n of e){let e=n.scope||`general`;t[e]||(t[e]=[]),t[e].push(n)}for(let[e,n]of Object.entries(t)){r.push(`## ${e}`,``);for(let e of n)r.push(`- ${e.type}: ${e.subject} (${e.hash})`);r.push(``)}}return r.join(`
15
15
  `)}const dt=/^[a-z0-9][a-z0-9-]*$/,ft=new Map;function pt(e){let t=f(e),n=ft.get(t);if(n!==void 0)return n;try{return C(`git`,[`rev-parse`,`--git-dir`],{cwd:t,timeout:1e4,encoding:`utf8`}),ft.set(t,!0),!0}catch{return ft.set(t,!1),!1}}function mt(e,t,n,r){try{return C(`git`,e,{cwd:t,timeout:1e4,input:n,encoding:`utf8`,stdio:[`pipe`,`pipe`,`pipe`]}).trim()}catch(t){r||console.warn(`Git operation failed (${e.join(` `)}): ${t instanceof Error?t.message:String(t)}`);return}}function ht(e){return e.toLowerCase().replace(/[^a-z0-9]+/g,`-`).replace(/^-|-$/g,``).slice(0,60)||`untitled`}function gt(e,t,n,r,i){let a=mt([`hash-object`,`-w`,`--stdin`],i,n);if(!a)return;let o=mt([`mktree`],i,`100644 blob ${a}\t${t}\n`);if(!o)return;let s=mt([`rev-parse`,e],i,void 0,!0),c=[`commit-tree`,o];s&&c.push(`-p`,s),c.push(`-m`,r);let l=mt(c,i);if(l)return mt([`update-ref`,e,l],i)===void 0?void 0:l}function _t(){ft.clear()}const vt=new Set;function yt(e){let t=ht(e);return t===`untitled`?`checkpoint`:t}function bt(e={}){let t=e.cwd??process.cwd();return f(e.stateDir??V(t),`checkpoints`)}function xt(e){return`${e}.bak`}function St(e){return!!e&&typeof e==`object`&&!Array.isArray(e)}function Ct(e,t,n){if(typeof e!=`string`||e.length<1||e.length>120)throw Error(`Checkpoint label must be 1-120 characters.`);if(!St(t))throw Error(`Checkpoint data must be a JSON-serializable object.`);let r;try{r=JSON.stringify(t)}catch{throw Error(`Checkpoint data must be a JSON-serializable object.`)}if(Buffer.byteLength(r,`utf8`)>5e5)throw Error(`Checkpoint data must be 500KB or less when serialized.`);if(n?.notes!==void 0&&Buffer.byteLength(n.notes,`utf8`)>1e4)throw Error(`Checkpoint notes must be 10KB or less.`);return r}function wt(e,t){try{let t=JSON.parse(e);if(!St(t))throw Error(`Checkpoint data must deserialize to an object.`);return t}catch(e){console.warn(`Corrupt checkpoint payload ${t}: ${e instanceof Error?e.message:String(e)}`);return}}function Tt(e){let t=wt(e.data,e.id);if(t)return{id:e.id,label:e.label,createdAt:e.createdAt,data:t,...e.notes?{notes:e.notes}:{}}}function Et(e){return{id:e.id,label:e.label,createdAt:e.createdAt,...e.notes?{notes:e.notes}:{}}}function Dt(e,t={}){let n=bt(t);if(vt.has(n)||!M(n)){vt.add(n);return}try{let t=F(n).filter(e=>e.endsWith(`.json`));for(let r of t){let t=f(n,r);try{let n=P(t,`utf-8`),i=JSON.parse(n),a=typeof i.id==`string`&&i.id.length>0?i.id:o(r,`.json`);if(typeof i.label!=`string`)throw Error(`Missing checkpoint label.`);if(!St(i.data))throw Error(`Checkpoint data must be a JSON object.`);let s=Ct(i.label,i.data,{notes:i.notes});e.checkpointSave(a,i.label,s,i.notes)}catch(e){console.warn(`Legacy checkpoint import failed for ${t}: ${e instanceof Error?e.message:String(e)}`)}}let r=xt(n);M(r)&&L(r,{recursive:!0,force:!0}),I(n,r),vt.add(n)}catch(e){console.warn(`Legacy checkpoint import failed for ${n}: ${e instanceof Error?e.message:String(e)}`)}}function Ot(e,t,n,r){Dt(e,{cwd:r?.cwd});let i=Ct(t,n,{notes:r?.notes}),a=`${Date.now()}-${yt(t)}`;e.checkpointSave(a,t,i,r?.notes);let o=kt(e,a,{cwd:r?.cwd});if(!o)throw Error(`Failed to load saved checkpoint "${a}".`);return o}function kt(e,t,n={}){Dt(e,n);let r=e.checkpointLoad(t);if(r)return Tt(r)}function At(e,t={}){return Dt(e,t),e.checkpointList(t.label,t.limit).map(Et)}function jt(e,t={}){Dt(e,t);let n=e.checkpointLatest(t.label);if(n)return Tt(n)}function Mt(e,t,n,r={}){Dt(e,r);let i=e.checkpointDiff(t,n);if(!i)return;let a=wt(i.from,t),o=wt(i.to,n);if(!a||!o)return;let s=new Set(Object.keys(a)),c=new Set(Object.keys(o));return{fromId:t,toId:n,added:[...c].filter(e=>!s.has(e)),removed:[...s].filter(e=>!c.has(e)),modified:[...s].filter(e=>c.has(e)).filter(e=>JSON.stringify(a[e])!==JSON.stringify(o[e]))}}function Nt(e,t,n){return Dt(e,{cwd:n?.cwd}),e.checkpointHistory(t,n?.limit??20).map(Et)}function Pt(e,t){Dt(e,{cwd:t?.cwd});let n=t?.keepLast??10,r=t?.dryRun??!0,i=t?.maxAgeDays===void 0?void 0:Date.now()-t.maxAgeDays*864e5,a=t?.label?yt(t.label):void 0,o=t?.label?e.checkpointHistory(t.label):e.checkpointList(),s=new Map;for(let e of o){let t=yt(e.label);if(a&&t!==a)continue;let n=s.get(t);n?n.push(e):s.set(t,[e])}let c=[],l=0;for(let e of s.values())e.forEach((e,t)=>{let r=Date.parse(e.createdAt);if(t<n&&!(i!==void 0&&!Number.isNaN(r)&&r<i)){l+=1;return}c.push(e.id)});if(!r)for(let t of c)e.checkpointDelete(t);return{deleted:c.length,kept:l,labels:[...s.keys()],deletedIds:c}}const Ft=[`.ts`,`.tsx`,`.js`,`.jsx`],It=new Set([`node_modules`,`.git`,`dist`,`build`,`coverage`,`.turbo`,`.cache`,`cdk.out`,B.state]);function Lt(e){return e.replace(/\\/g,`/`)}function Rt(e){return e.replace(/[.+^${}()|[\]\\]/g,`\\$&`)}function zt(e,t){let n=Lt(e),r=Lt(t).trim();if(!r)return!1;let i=Rt(r).replace(/\*\*/g,`::DOUBLE_STAR::`).replace(/\*/g,`[^/]*`).replace(/\?/g,`[^/]`).replace(/::DOUBLE_STAR::/g,`.*`);return RegExp(`^${i}$`).test(n)}function Bt(e,t,n){return t.some(t=>zt(e,t)?!0:n?zt(`${e}/`,t):!1)}async function Vt(e,t,r){let a=[],o=t.map(e=>e.toLowerCase());async function s(t){let l=await n(t);for(let n of l){if(It.has(n))continue;let l=u(t,n),f=await i(l),p=Lt(d(e,l));if(f.isDirectory()){Bt(p,r,!0)||await s(l);continue}Bt(p,r,!1)||o.includes(c(n).toLowerCase())&&a.push(l)}}return await s(e),a.sort((e,t)=>e.localeCompare(t)),a}const Ht=B.restorePoints;function Ut(){let e=u(process.cwd(),Ht);return M(e)||N(e,{recursive:!0}),e}function Wt(e,t,n){let r=Ut(),i=`${Date.now()}-${e}`,a={id:i,timestamp:new Date().toISOString(),operation:e,files:t,description:n};z(u(r,`${i}.json`),`${JSON.stringify(a,null,2)}\n`,`utf-8`);let o=F(r).filter(e=>e.endsWith(`.json`)).sort();for(;o.length>50;){let e=o.shift();if(!e)break;try{re(u(r,e))}catch{}}return i}function Gt(){let e=u(process.cwd(),Ht);return M(e)?F(e).filter(e=>e.endsWith(`.json`)).sort().reverse().map(t=>{try{return JSON.parse(P(u(e,t),`utf-8`))}catch(n){return console.debug(`Skipping corrupt restore point ${u(e,t)}: ${n instanceof Error?n.message:String(n)}`),null}}).filter(e=>e!==null):[]}async function Kt(e){let t=u(u(process.cwd(),Ht),`${e}.json`);if(!M(t))throw Error(`Restore point not found: ${e}`);let n=JSON.parse(P(t,`utf-8`)),r=[];for(let e of n.files){let t=s(e.path);M(t)||N(t,{recursive:!0}),await a(e.path,e.content,`utf-8`),r.push(e.path)}return r}function qt(e){return e.replace(/\\/g,`/`)}async function Jt(e){let{rootPath:n,rules:r,extensions:i=Ft,exclude:o=[],dryRun:s=!1}=e,c=r.map(e=>({...e,regex:new RegExp(e.pattern,`g`)})),l=await Vt(n,i,o),u=[],f=new Set,p=0,m=[];for(let e of l){let r=qt(d(n,e)),i=await t(e,`utf-8`),o=i.split(/\r?\n/),l=!1;for(let[e,t]of c.entries())if(!(t.fileFilter&&!zt(r,t.fileFilter)))for(let n=0;n<o.length;n++){let i=o[n];t.regex.lastIndex=0;let a=i.replace(t.regex,t.replacement);i!==a&&(o[n]=a,l=!0,f.add(e),u.push({rule:t.description,path:r,line:n+1,before:i,after:a}))}l&&(p+=1,s||(m.push({path:e,content:i}),await a(e,o.join(`
16
- `),`utf-8`)))}return!s&&m.length>0&&Wt(`codemod`,m,`codemod: ${r.length} rules, ${p} files`),{changes:u,rulesApplied:f.size,filesModified:p,dryRun:s}}const Yt=new se({max:200,ttl:1e3*60*30});function Xt(e){return H(`sha256`).update(e).digest(`hex`).slice(0,16)}function Zt(e,t){let n=Xt(t),r=Yt.get(e);if(Yt.set(e,{hash:n,text:t,timestamp:Date.now()}),!r||r.hash===n)return{text:r?.hash===n?`[No changes since last read]`:t,isDelta:r?.hash===n,hash:n};let i=oe(e,e,r.text,t,`previous`,`current`,{context:3});return i.length>=t.length*.8?{text:t,isDelta:!1,hash:n}:{text:i,isDelta:!0,hash:n}}const Qt=.6;function $t(e){if(!e||e.length===0)return 0;let t=ce(e),n=e.length;return n===0?0:Math.min(t.length/n,Qt)/Qt}function en(e){if(e.length===0)return[];let t=new Map,n=[];for(let r of e){let e=ce(r);n.push(e);for(let n of e)t.set(n,(t.get(n)??0)+1)}let r=[];for(let i=0;i<e.length;i++){let a=e[i],o=n[i];if(!a||o.length===0){r.push(0);continue}let s=Math.min(o.length/a.length,Qt)/Qt,c=0;for(let e of o)(t.get(e)??0)===1&&c++;let l=c/o.length,u=.6*s+.4*l;r.push(Math.min(u,1))}return r}function tn(e){if(!e||e.length===0)return 0;let t=new Map;for(let n of e)t.set(n,(t.get(n)??0)+1);let n=0,r=e.length;for(let e of t.values()){let t=e/r;t>0&&(n-=t*Math.log2(t))}return Math.min(n/6.6,1)}function nn(e){try{return $t(e)}catch{return tn(e)}}function rn(e,t=15){try{let n=F(s(e),{withFileTypes:!0});if(n.length===0)return``;let r=n.slice(0,t).map(e=>e.isDirectory()?`${e.name}/`:e.name),i=n.length>t?`, … (${n.length} total)`:``;return` Available in directory: ${r.join(`, `)}${i}`}catch{return``}}async function an(e,n){let{query:r,maxChars:a=3e3,minScore:o=.3,segmentation:s=`paragraph`}=n,c=n.tokenBudget?n.tokenBudget*4:a,l;if(n.text)l=n.text;else if(n.path){let e;try{e=await i(n.path)}catch(e){let t=e.code;if(t===`ENOENT`){let e=rn(n.path);throw Error(`File not found: ${n.path}. Check the path and try again.${e}`)}throw t===`EACCES`||t===`EPERM`?Error(`Permission denied reading ${n.path}. The file exists but is not accessible.`):e}if(e.isDirectory())throw Error(`Path is a directory: ${n.path}. compact requires a file path, not a directory. Use analyze({ aspect: "structure", path }) or find to explore directories.`);if(e.size>1e7)throw Error(`File too large (${(e.size/1e6).toFixed(1)}MB). compact supports files up to 10MB. Consider splitting or using search instead.`);l=n.cache?(await n.cache.get(n.path)).content:await t(n.path,`utf-8`)}else throw Error(`Either "text" or "path" must be provided`);if(n.mode===`delta`&&n.path){let e=Zt(n.path,l);if(e.isDelta)return{text:e.text,originalChars:l.length,compressedChars:e.text.length,ratio:e.text.length/l.length,segmentsKept:1,segmentsTotal:1}}if(l.length<=c)return{text:l,originalChars:l.length,compressedChars:l.length,ratio:1,segmentsKept:1,segmentsTotal:1};let u=Xe(l,s);if(u.length===0)return{text:``,originalChars:l.length,compressedChars:0,ratio:0,segmentsKept:0,segmentsTotal:0};let d=await e.embed(r);if(d.length===0){let e=l.slice(0,c);return{text:`/* warning: embeddings unavailable — returning unscored text */\n${e}`,originalChars:l.length,compressedChars:e.length,ratio:e.length/l.length,segmentsKept:u.length,segmentsTotal:u.length}}let f=en(u),p=[];for(let t=0;t<u.length;t++){let n=.85*et(d,await e.embed(u[t]))+.15*(f[t]??0);p.push({text:u[t],score:n,index:t})}let m=p.filter(e=>e.score>=o).sort((e,t)=>t.score-e.score),h=[],g=0;for(let e of m){if(g+e.text.length>c){g===0&&(h.push({...e,text:e.text.slice(0,c)}),g=c);break}h.push(e),g+=e.text.length+2}let _=tt(h.sort((e,t)=>t.score-e.score)).map(e=>e.text).join(`
16
+ `),`utf-8`)))}return!s&&m.length>0&&Wt(`codemod`,m,`codemod: ${r.length} rules, ${p} files`),{changes:u,rulesApplied:f.size,filesModified:p,dryRun:s}}const Yt=new se({max:200,ttl:1e3*60*30});function Xt(e){return H(`sha256`).update(e).digest(`hex`).slice(0,16)}function Zt(e,t){let n=Xt(t),r=Yt.get(e);if(Yt.set(e,{hash:n,text:t,timestamp:Date.now()}),!r||r.hash===n)return{text:r?.hash===n?`[No changes since last read]`:t,isDelta:r?.hash===n,hash:n};let i=oe(e,e,r.text,t,`previous`,`current`,{context:3});return i.length>=t.length*.8?{text:t,isDelta:!1,hash:n}:{text:i,isDelta:!0,hash:n}}const Qt=.6;function $t(e){if(!e||e.length===0)return 0;let t=ce(e),n=e.length;return n===0?0:Math.min(t.length/n,Qt)/Qt}function en(e){if(e.length===0)return[];let t=new Map,n=[];for(let r of e){let e=ce(r);n.push(e);for(let n of e)t.set(n,(t.get(n)??0)+1)}let r=[];for(let i=0;i<e.length;i++){let a=e[i],o=n[i];if(!a||o.length===0){r.push(0);continue}let s=Math.min(o.length/a.length,Qt)/Qt,c=0;for(let e of o)(t.get(e)??0)===1&&c++;let l=c/o.length,u=.6*s+.4*l;r.push(Math.min(u,1))}return r}function tn(e){if(!e||e.length===0)return 0;let t=new Map;for(let n of e)t.set(n,(t.get(n)??0)+1);let n=0,r=e.length;for(let e of t.values()){let t=e/r;t>0&&(n-=t*Math.log2(t))}return Math.min(n/6.6,1)}function nn(e){try{return $t(e)}catch{return tn(e)}}function rn(e,t=15){try{let n=F(s(e),{withFileTypes:!0});if(n.length===0)return``;let r=n.slice(0,t).map(e=>e.isDirectory()?`${e.name}/`:e.name),i=n.length>t?`, … (${n.length} total)`:``;return` Available in directory: ${r.join(`, `)}${i}`}catch{return``}}async function an(e,n){let{query:r,maxChars:a=3e3,minScore:o=.3,segmentation:s=`paragraph`}=n,c=n.tokenBudget?n.tokenBudget*4:a,l;if(n.text)l=n.text;else if(n.path){let e;try{e=await i(n.path)}catch(e){let t=e.code;if(t===`ENOENT`){let e=rn(n.path);throw Error(`File not found: ${n.path}. Check the path and try again.${e}`)}throw t===`EACCES`||t===`EPERM`?Error(`Permission denied reading ${n.path}. The file exists but is not accessible.`):e}if(e.isDirectory())throw Error(`Path is a directory: ${n.path}. compact requires a file path, not a directory. Use analyze({ aspect: "structure", path }) or find to explore directories.`);if(e.size>1e7)throw Error(`File too large (${(e.size/1e6).toFixed(1)}MB). compact supports files up to 10MB. Consider splitting or using search instead.`);l=n.cache?(await n.cache.get(n.path)).content:await t(n.path,`utf-8`)}else throw Error(`Either "text" or "path" must be provided`);if(n.mode===`delta`&&n.path){let e=Zt(n.path,l);if(e.isDelta)return{text:e.text,originalChars:l.length,compressedChars:e.text.length,ratio:e.text.length/l.length,segmentsKept:1,segmentsTotal:1}}if(l.length<=c)return{text:l,originalChars:l.length,compressedChars:l.length,ratio:1,segmentsKept:1,segmentsTotal:1};let u=Xe(l,s);if(u.length===0)return{text:``,originalChars:l.length,compressedChars:0,ratio:0,segmentsKept:0,segmentsTotal:0};let d;try{d=await e.embed(r)}catch{d=new Float32Array}if(d.length===0){let e=l.slice(0,c);return{text:`/* warning: embeddings unavailable — returning unscored text */\n${e}`,originalChars:l.length,compressedChars:e.length,ratio:e.length/l.length,segmentsKept:u.length,segmentsTotal:u.length}}let f=en(u),p=[];for(let t=0;t<u.length;t++){let n=await e.embed(u[t]),r=.85*et(d,n)+.15*(f[t]??0);p.push({text:u[t],score:r,index:t})}let m=p.filter(e=>e.score>=o).sort((e,t)=>t.score-e.score),h=[],g=0;for(let e of m){if(g+e.text.length>c){g===0&&(h.push({...e,text:e.text.slice(0,c)}),g=c);break}h.push(e),g+=e.text.length+2}let _=tt(h.sort((e,t)=>t.score-e.score)).map(e=>e.text).join(`
17
17
 
18
18
  `);return{text:_,originalChars:l.length,compressedChars:_.length,ratio:_.length/l.length,segmentsKept:h.length,segmentsTotal:u.length}}const on=/\b(error|fatal|exception|failed|failure|fail|passed|warn|warning|panic|abort|timeout|critical)\b/i,sn=[];function cn(e){sn.push(e),sn.sort((e,t)=>t.priority-e.priority)}function ln(e){for(let t of e)cn(t)}function un(){return sn}function dn(e){return/^(diff --git|commit [0-9a-f]{7,40}|On branch |Your branch )/m.test(e)?`git`:/^\s*[MADRCU?!]{1,2}\s+\S/m.test(e)&&/^##\s/m.test(e)?`git-status`:/^(npm (warn|ERR!|notice)|added \d+ packages?|up to date)/m.test(e)?`npm`:/^(Packages|Progress):/m.test(e)||/pnpm/.test(e)?`pnpm`:/✓|✗|PASS|FAIL|Tests?\s+\d+\s+(passed|failed)/m.test(e)||/^(PASS|FAIL)\s+\S/m.test(e)?`test-runner`:/^(error TS\d+|warning TS\d+|\S+\.tsx?[(:]\d+)/m.test(e)?`tsc`:/^\S+\.\w+:\d+:\d+\s+(error|warning|info)/m.test(e)||/Found \d+ (error|warning)/m.test(e)?`lint`:/^(CONTAINER ID|IMAGE|REPOSITORY|Step \d+\/\d+|--->)/m.test(e)||/docker|Dockerfile/i.test(e)?`docker`:/^(NAME\s+READY|NAMESPACE\s|kubectl)/m.test(e)?`kubectl`:`unknown`}function fn(e,t,n){return[`\n... [${e} chars / ~${t} tokens omitted]\n`,`\n... [${e} chars omitted]\n`,`
19
19
  ... [omitted]