@vpxa/aikit 0.1.237 → 0.1.239

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vpxa/aikit",
3
- "version": "0.1.237",
3
+ "version": "0.1.239",
4
4
  "type": "module",
5
5
  "description": "Local-first AI developer toolkit — knowledge base, code analysis, context management, and developer tools for LLM agents",
6
6
  "license": "MIT",
@@ -1,4 +1,4 @@
1
- import{createRequire as e}from"node:module";import{dirname as t,extname as n,join as r}from"node:path";import{CHUNK_SIZES as i,createLogger as a}from"../../core/dist/index.js";import{copyFileSync as o,existsSync as s,mkdirSync as c,readFileSync as l,readdirSync as u,realpathSync as d,renameSync as f}from"node:fs";import{homedir as p}from"node:os";import{fileURLToPath as m,pathToFileURL as ee}from"node:url";function h(){return{mode:`unknown`,reason:``,pathsChecked:[],os:process.platform,arch:process.arch,nodeVersion:process.version,webTreeSitterImportable:!1,healAttempted:!1,healSuccess:!1,healError:null,initError:null,wasmDir:null,grammarCount:0}}let g=h();const _={get(){return{...g,pathsChecked:g.pathsChecked.map(e=>({...e}))}},update(e){Object.assign(g,e)},reset(){g=h()}},v=t(m(import.meta.url));function y(e){try{return d(e)}catch{return e}}const b={".ts":`tree-sitter-typescript.wasm`,".tsx":`tree-sitter-typescript.wasm`,".mts":`tree-sitter-typescript.wasm`,".cts":`tree-sitter-typescript.wasm`,".js":`tree-sitter-javascript.wasm`,".jsx":`tree-sitter-javascript.wasm`,".mjs":`tree-sitter-javascript.wasm`,".cjs":`tree-sitter-javascript.wasm`,".py":`tree-sitter-python.wasm`,".go":`tree-sitter-go.wasm`,".rs":`tree-sitter-rust.wasm`,".java":`tree-sitter-java.wasm`,".kt":`tree-sitter-kotlin.wasm`,".kts":`tree-sitter-kotlin.wasm`,".scala":`tree-sitter-scala.wasm`,".sc":`tree-sitter-scala.wasm`,".c":`tree-sitter-c.wasm`,".h":`tree-sitter-c.wasm`,".cpp":`tree-sitter-cpp.wasm`,".cc":`tree-sitter-cpp.wasm`,".cxx":`tree-sitter-cpp.wasm`,".hpp":`tree-sitter-cpp.wasm`,".hxx":`tree-sitter-cpp.wasm`,".cs":`tree-sitter-c_sharp.wasm`,".swift":`tree-sitter-swift.wasm`,".rb":`tree-sitter-ruby.wasm`,".php":`tree-sitter-php.wasm`},x=new Set(Object.keys(b));let S;function C(e){return[...new Set(Object.values(b))].filter(t=>s(r(e,t))).length}function w(){let e=y(v),t=[r(e,`..`,`..`,`wasm`),r(e,`..`,`wasm`)];for(let e of t){let t=y(e);if(s(r(t,`tree-sitter.wasm`)))return t}return null}function T(){let e=y(v),t=[r(p(),`.aikit`,`cache`,`wasm`),r(e,`..`,`..`,`wasm`),r(e,`..`,`wasm`),r(process.cwd(),`node_modules`,`@aikit`,`chunker`,`wasm`)],n=[];for(let e of t){let t=y(e),i=s(r(t,`tree-sitter.wasm`));if(n.push({path:t,exists:i}),i)return _.update({mode:`wasm`,reason:`Resolved WASM directory`,pathsChecked:n,wasmDir:t,grammarCount:C(t),initError:null}),t}return _.update({mode:`unknown`,reason:`No WASM directory containing tree-sitter.wasm was found`,pathsChecked:n,wasmDir:null,grammarCount:0}),null}function E(){return S===void 0&&(S=T()),S}function D(e){S=e===null?null:y(e),_.update({mode:S?`wasm`:`unknown`,reason:S?`WASM directory overridden at runtime`:`WASM directory override cleared`,wasmDir:S,grammarCount:S?C(S):0})}function O(e){let t=b[e];if(!t)return null;let n=E();if(!n)return null;let i=r(n,t);return s(i)?i:null}function k(){let e=E();if(!e)return null;let t=r(e,`tree-sitter.wasm`);return s(t)?t:null}const te=[`CI`,`GITHUB_ACTIONS`,`JENKINS_URL`,`GITLAB_CI`,`TF_BUILD`];function ne(){return te.some(e=>!!process.env[e])}function A(e,t){let n=`${t}.tmp-${process.pid}`;o(e,n),f(n,t)}function j(e){let t=r(p(),`.aikit`,`cache`,`wasm`);try{c(t,{recursive:!0});let n=u(e).filter(e=>e.endsWith(`.wasm`)),i=0;for(let a of n){let n=r(e,a),o=r(t,a);s(o)||(A(n,o),i++)}if(s(r(t,`tree-sitter.wasm`))){let e=u(t).filter(e=>e.endsWith(`.wasm`)&&e!==`tree-sitter.wasm`).length;return _.update({healSuccess:!0,healError:null,wasmDir:t,grammarCount:e,reason:`Populated user WASM cache from bundled dir (${i} files copied)`}),t}return _.update({healSuccess:!1,healError:`tree-sitter.wasm not present in cache after copy`}),null}catch(e){return _.update({healSuccess:!1,healError:String(e),reason:`Failed to populate user WASM cache`}),null}}function M(){if(ne())return _.update({healAttempted:!1,healSuccess:!1,healError:null,reason:`CI environment detected - skipping auto-heal`}),null;_.update({healAttempted:!0,healSuccess:!1,healError:null,reason:`Attempting to heal WASM cache`});let n=w();if(n){let e=j(n);if(e)return e}try{let i=e(import.meta.url),a=r(p(),`.aikit`,`cache`,`wasm`),o;try{o=t(i.resolve(`web-tree-sitter/package.json`))}catch{return _.update({healSuccess:!1,healError:n?`Bundled copy failed and web-tree-sitter not in node_modules`:`Bundled WASM dir not found and web-tree-sitter not in node_modules`,reason:`Cannot heal WASM cache - no source available`}),null}let l;try{l=t(i.resolve(`tree-sitter-wasms/package.json`))}catch{c(a,{recursive:!0});let e=r(o,`tree-sitter.wasm`);return s(e)&&(A(e,r(a,`tree-sitter.wasm`)),s(r(a,`tree-sitter.wasm`)))?(_.update({healSuccess:!0,healError:null,wasmDir:a,grammarCount:0,reason:`Healed parser WASM from web-tree-sitter (no grammar wasms available)`}),a):(_.update({healSuccess:!1,healError:`tree-sitter-wasms not found (devDependency only)`,reason:`Cannot heal grammar WASM cache without tree-sitter-wasms package`}),null)}c(a,{recursive:!0});let d=r(o,`tree-sitter.wasm`);s(d)&&A(d,r(a,`tree-sitter.wasm`));let f=r(l,`out`),m=0;if(s(f)){let e=u(f).filter(e=>e.endsWith(`.wasm`));m=e.length;for(let t of e)A(r(f,t),r(a,t))}return s(r(a,`tree-sitter.wasm`))?(_.update({healSuccess:!0,healError:null,wasmDir:a,grammarCount:m,reason:`Healed WASM cache from node_modules (dev environment)`}),a):(_.update({healSuccess:!1,healError:`tree-sitter.wasm not found after copy`,reason:`WASM cache heal completed without parser runtime`}),null)}catch(e){return _.update({healSuccess:!1,healError:String(e),reason:`WASM cache heal failed`}),null}}const N=a(`wasm-runtime`);async function re(){try{return await import(`web-tree-sitter`)}catch{}try{return e(import.meta.url)(`web-tree-sitter`)}catch{}let n=t(m(import.meta.url)),i=[r(n,`..`,`node_modules`,`web-tree-sitter`,`tree-sitter.js`),r(n,`..`,`..`,`..`,`..`,`web-tree-sitter`,`tree-sitter.js`),r(n,`..`,`..`,`..`,`..`,`..`,`node_modules`,`web-tree-sitter`,`tree-sitter.js`)];for(let e of i)if(s(e))try{return await import(ee(e).href)}catch{}throw Error(`web-tree-sitter module not found. Tried: ESM import, createRequire, and path-based resolution from ${n}`)}async function P(){let e=await re(),t=typeof e.default?.init==`function`?e.default:e.Parser;if(!t||typeof t.init!=`function`)throw Error(`Unsupported web-tree-sitter export shape: ${Object.keys(e).join(`, `)}`);return t}var F=class e{static instance=null;parser=null;languages=new Map;loadedGrammars=new Map;recoveryAttempts=0;static MAX_RECOVERIES=3;static MAX_PARSE_SIZE=1e6;static async initialize(){if(e.instance)return e.instance;let t=new e;return await t.init()?(e.instance=t,t):null}static get(){return e.instance}static async ensure(){let t=e.get();if(t)return t;let n=await e.initialize();if(!n)throw Error(`Failed to initialize WASM tree-sitter runtime`);return n}static dispose(){e.instance&&=(e.instance.cleanupParser(),null)}cleanupParser(){this.parser?.delete(),this.parser=null,this.languages.clear(),this.loadedGrammars.clear()}disableWasmParsing(){this.cleanupParser(),this.recoveryAttempts=e.MAX_RECOVERIES}async init(){try{let e;try{e=await P(),_.update({webTreeSitterImportable:!0})}catch(e){return _.update({webTreeSitterImportable:!1,mode:`regex`,reason:`web-tree-sitter import failed: ${String(e)}`,initError:String(e)}),N.warn(`web-tree-sitter module not importable`,{error:String(e)}),!1}let t=k();if(!t){N.info(`Parser WASM not found, attempting auto-heal...`);let e=M();e&&(D(e),t=k(),N.info(`Auto-heal succeeded`,{dir:e}))}if(!t)return _.update({mode:`regex`,reason:`Parser WASM file not found after heal attempt`}),N.warn(`Parser WASM file not found (auto-heal failed or skipped)`),!1;let n=t;return await e.init({locateFile:()=>n}),this.parser=new e,this.autoPopulateUserCache(),_.update({mode:`wasm`,reason:`WASM parser initialized successfully`,initError:null}),N.info(`WASM tree-sitter parser initialized`),!0}catch(e){return _.update({mode:`regex`,reason:`WASM initialization failed: ${String(e)}`,initError:String(e)}),N.warn(`Failed to initialize WASM tree-sitter`,{error:String(e)}),!1}}async loadLanguage(e){let t=this.languages.get(e);if(t)return t;let n=O(e);if(!n)return null;let r=this.loadedGrammars.get(n);if(r)return this.languages.set(e,r),r;try{let t=await(await P()).Language.load(n);return this.loadedGrammars.set(n,t),this.languages.set(e,t),N.debug(`Loaded grammar`,{ext:e}),t}catch(t){return N.warn(`Failed to load grammar`,{ext:e,error:String(t)}),null}}async parse(t,n){if(!this.parser)return null;if(t.length>e.MAX_PARSE_SIZE)return N.warn(`File too large for WASM parser, skipping`,{ext:n,size:t.length}),null;let r=await this.loadLanguage(n);if(!r)return null;try{return this.parser.setLanguage(r),this.parser.parse(t)}catch(r){if(N.warn(`WASM parser crashed, attempting recovery...`,{ext:n,error:String(r)}),this.recoveryAttempts>=e.MAX_RECOVERIES)return N.error(`WASM parser recovery limit reached, falling back to null`,{attempts:this.recoveryAttempts}),this.disableWasmParsing(),null;if(!await this.recoverParser(n))return N.error(`WASM parser recovery failed, falling back to null`),null;try{return this.parser?.parse(t)??null}catch(e){return N.error(`WASM parser crashed again after recovery, falling back to null`,{error:String(e)}),null}}}async recoverParser(e){this.recoveryAttempts++;try{this.cleanupParser();let t=await P(),n=k();if(!n)return N.error(`Cannot recover: parser WASM file not found`),!1;let r=n;await t.init({locateFile:()=>r}),this.parser=new t;let i=await this.loadLanguage(e);return i?(this.parser.setLanguage(i),N.info(`WASM parser recovered successfully`,{attempt:this.recoveryAttempts}),!0):(N.error(`Cannot recover: failed to reload language`,{ext:e}),!1)}catch(e){return N.error(`WASM parser recovery threw`,{error:String(e)}),!1}}autoPopulateUserCache(){try{if(s(r(r(p(),`.aikit`,`cache`,`wasm`),`tree-sitter.wasm`)))return;let e=w();if(!e)return;N.info(`Auto-populating user WASM cache for future reliability`,{from:e}),j(e)}catch(e){N.debug(`Auto-populate user cache failed (non-fatal)`,{error:String(e)})}}hasLanguage(e){return x.has(e)}isLanguageLoaded(e){return this.languages.has(e)}getParser(){return this.parser}getLanguage(e){return this.languages.get(e)??null}};async function I(){return await F.initialize()!==null}const ie=a(`call-graph`),ae=new Set([`function_declaration`,`method_definition`,`arrow_function`,`function_definition`,`function_declaration`,`method_declaration`,`function_item`,`method_declaration`,`constructor_declaration`]),oe=new Set([`call_expression`,`new_expression`,`call`]);function se(e,t){let r=n(t).toLowerCase(),i=F.get();if(i?.isLanguageLoaded(r)){let n=i.getParser(),a=i.getLanguage(r);if(n&&a){n.setLanguage(a);let r;try{r=n.parse(e)}catch(e){return ie.warn(`WASM parser.parse() crashed in call-graph extraction`,{file:t,error:String(e)}),null}if(r){let e=[];return L(r.rootNode,t,`<module>`,e),e}}}return null}function L(e,t,n,r){if(!e)return;let i=n;if(ae.has(e.type)&&(i=ce(e)??n),oe.has(e.type)){let n=le(e);n&&r.push({callerFile:t,callerName:i,calleeName:n,line:(e.startPosition?.row??0)+1})}for(let n=0;n<(e.childCount??0);n++){let a=e.child(n);a&&L(a,t,i,r)}}function ce(e){for(let t=0;t<(e.childCount??0);t++){let n=e.child(t);if(n&&(n.type===`identifier`||n.type===`property_identifier`||n.type===`name`))return n.text??null}return null}function le(e){let t=e.childForFieldName?.(`function`)??e.child(0);return t?t.type===`identifier`||t.type===`name`?t.text??null:t.type===`member_expression`||t.type===`attribute`?(t.childForFieldName?.(`property`)??t.childForFieldName?.(`attribute`))?.text??null:e.type===`new_expression`?e.child(1)?.text??null:null:null}var R=class{maxChunkSize;constructor(e){this.maxChunkSize=e?.maxChunkSize??i.code.max}chunk(e,t){let n=this.findDeclarationBoundaries(e);if(n.length===0)return this.fallbackChunk(e,t);let r=[];for(let i=0;i<n.length;i++){let a=n[i].offset,o=i+1<n.length?n[i+1].offset:e.length,s=e.slice(a,o).trim();if(s=`// File: ${t.sourcePath}\n`+s,s.length>this.maxChunkSize){let n=this.splitByLines(s,this.maxChunkSize),i=this.getLineNumber(e,a);for(let e of n){let n=e.split(`
1
+ import{createRequire as e}from"node:module";import{dirname as t,extname as n,join as r}from"node:path";import{CHUNK_SIZES as i,createLogger as a}from"../../core/dist/index.js";import{copyFileSync as o,existsSync as s,mkdirSync as c,readFileSync as l,readdirSync as u,realpathSync as d,renameSync as f}from"node:fs";import{homedir as p}from"node:os";import{fileURLToPath as m,pathToFileURL as ee}from"node:url";function h(){return{mode:`unknown`,reason:``,pathsChecked:[],os:process.platform,arch:process.arch,nodeVersion:process.version,webTreeSitterImportable:!1,healAttempted:!1,healSuccess:!1,healError:null,initError:null,wasmDir:null,grammarCount:0}}let g=h();const _={get(){return{...g,pathsChecked:g.pathsChecked.map(e=>({...e}))}},update(e){Object.assign(g,e)},reset(){g=h()}},v=t(m(import.meta.url));function y(e){try{return d(e)}catch{return e}}const b={".ts":`tree-sitter-typescript.wasm`,".tsx":`tree-sitter-typescript.wasm`,".mts":`tree-sitter-typescript.wasm`,".cts":`tree-sitter-typescript.wasm`,".js":`tree-sitter-javascript.wasm`,".jsx":`tree-sitter-javascript.wasm`,".mjs":`tree-sitter-javascript.wasm`,".cjs":`tree-sitter-javascript.wasm`,".py":`tree-sitter-python.wasm`,".go":`tree-sitter-go.wasm`,".rs":`tree-sitter-rust.wasm`,".java":`tree-sitter-java.wasm`,".kt":`tree-sitter-kotlin.wasm`,".kts":`tree-sitter-kotlin.wasm`,".scala":`tree-sitter-scala.wasm`,".sc":`tree-sitter-scala.wasm`,".c":`tree-sitter-c.wasm`,".h":`tree-sitter-c.wasm`,".cpp":`tree-sitter-cpp.wasm`,".cc":`tree-sitter-cpp.wasm`,".cxx":`tree-sitter-cpp.wasm`,".hpp":`tree-sitter-cpp.wasm`,".hxx":`tree-sitter-cpp.wasm`,".cs":`tree-sitter-c_sharp.wasm`,".swift":`tree-sitter-swift.wasm`,".rb":`tree-sitter-ruby.wasm`,".php":`tree-sitter-php.wasm`},x=new Set(Object.keys(b));let S;function C(e){return[...new Set(Object.values(b))].filter(t=>s(r(e,t))).length}function w(){let e=y(v),t=[r(e,`..`,`..`,`wasm`),r(e,`..`,`wasm`)];for(let e of t){let t=y(e);if(s(r(t,`tree-sitter.wasm`)))return t}return null}function T(){let e=y(v),t=[r(p(),`.aikit`,`cache`,`wasm`),r(e,`..`,`..`,`wasm`),r(e,`..`,`wasm`),r(process.cwd(),`node_modules`,`@aikit`,`chunker`,`wasm`)],n=[];for(let e of t){let t=y(e),i=s(r(t,`tree-sitter.wasm`));if(n.push({path:t,exists:i}),i)return _.update({mode:`wasm`,reason:`Resolved WASM directory`,pathsChecked:n,wasmDir:t,grammarCount:C(t),initError:null}),t}return _.update({mode:`unknown`,reason:`No WASM directory containing tree-sitter.wasm was found`,pathsChecked:n,wasmDir:null,grammarCount:0}),null}function E(){return S===void 0&&(S=T()),S}function D(e){S=e===null?null:y(e),_.update({mode:S?`wasm`:`unknown`,reason:S?`WASM directory overridden at runtime`:`WASM directory override cleared`,wasmDir:S,grammarCount:S?C(S):0})}function O(e){let t=b[e];if(!t)return null;let n=E();if(!n)return null;let i=r(n,t);return s(i)?i:null}function k(){let e=E();if(!e)return null;let t=r(e,`tree-sitter.wasm`);return s(t)?t:null}const te=[`CI`,`GITHUB_ACTIONS`,`JENKINS_URL`,`GITLAB_CI`,`TF_BUILD`];function ne(){return te.some(e=>!!process.env[e])}function A(e,t){let n=`${t}.tmp-${process.pid}`;o(e,n),f(n,t)}function j(e){let t=r(p(),`.aikit`,`cache`,`wasm`);try{c(t,{recursive:!0});let n=u(e).filter(e=>e.endsWith(`.wasm`)),i=0;for(let a of n){let n=r(e,a),o=r(t,a);s(o)||(A(n,o),i++)}if(s(r(t,`tree-sitter.wasm`))){let e=u(t).filter(e=>e.endsWith(`.wasm`)&&e!==`tree-sitter.wasm`).length;return _.update({healSuccess:!0,healError:null,wasmDir:t,grammarCount:e,reason:`Populated user WASM cache from bundled dir (${i} files copied)`}),t}return _.update({healSuccess:!1,healError:`tree-sitter.wasm not present in cache after copy`}),null}catch(e){return _.update({healSuccess:!1,healError:String(e),reason:`Failed to populate user WASM cache`}),null}}function M(){if(ne())return _.update({healAttempted:!1,healSuccess:!1,healError:null,reason:`CI environment detected - skipping auto-heal`}),null;_.update({healAttempted:!0,healSuccess:!1,healError:null,reason:`Attempting to heal WASM cache`});let n=w();if(n){let e=j(n);if(e)return e}try{let i=e(import.meta.url),a=r(p(),`.aikit`,`cache`,`wasm`),o;try{o=t(i.resolve(`web-tree-sitter/package.json`))}catch{return _.update({healSuccess:!1,healError:n?`Bundled copy failed and web-tree-sitter not in node_modules`:`Bundled WASM dir not found and web-tree-sitter not in node_modules`,reason:`Cannot heal WASM cache - no source available`}),null}let l;try{l=t(i.resolve(`tree-sitter-wasms/package.json`))}catch{c(a,{recursive:!0});let e=r(o,`tree-sitter.wasm`);return s(e)&&(A(e,r(a,`tree-sitter.wasm`)),s(r(a,`tree-sitter.wasm`)))?(_.update({healSuccess:!0,healError:null,wasmDir:a,grammarCount:0,reason:`Healed parser WASM from web-tree-sitter (no grammar wasms available)`}),a):(_.update({healSuccess:!1,healError:`tree-sitter-wasms not found (devDependency only)`,reason:`Cannot heal grammar WASM cache without tree-sitter-wasms package`}),null)}c(a,{recursive:!0});let d=r(o,`tree-sitter.wasm`);s(d)&&A(d,r(a,`tree-sitter.wasm`));let f=r(l,`out`),m=0;if(s(f)){let e=u(f).filter(e=>e.endsWith(`.wasm`));m=e.length;for(let t of e)A(r(f,t),r(a,t))}return s(r(a,`tree-sitter.wasm`))?(_.update({healSuccess:!0,healError:null,wasmDir:a,grammarCount:m,reason:`Healed WASM cache from node_modules (dev environment)`}),a):(_.update({healSuccess:!1,healError:`tree-sitter.wasm not found after copy`,reason:`WASM cache heal completed without parser runtime`}),null)}catch(e){return _.update({healSuccess:!1,healError:String(e),reason:`WASM cache heal failed`}),null}}const N=a(`wasm-runtime`);async function re(){try{return await import(`web-tree-sitter`)}catch{}try{return e(import.meta.url)(`web-tree-sitter`)}catch{}let n=t(m(import.meta.url)),i=[r(n,`..`,`node_modules`,`web-tree-sitter`,`tree-sitter.js`),r(n,`..`,`..`,`..`,`..`,`web-tree-sitter`,`tree-sitter.js`),r(n,`..`,`..`,`..`,`..`,`..`,`node_modules`,`web-tree-sitter`,`tree-sitter.js`)];for(let e of i)if(s(e))try{return await import(ee(e).href)}catch{}throw Error(`web-tree-sitter module not found. Tried: ESM import, createRequire, and path-based resolution from ${n}`)}async function P(){let e=await re(),t=typeof e.default?.init==`function`?e.default:e.Parser;if(!t||typeof t.init!=`function`)throw Error(`Unsupported web-tree-sitter export shape: ${Object.keys(e).join(`, `)}`);return t}var F=class e{static instance=null;parser=null;languages=new Map;loadedGrammars=new Map;recoveryAttempts=0;static MAX_RECOVERIES=3;static MAX_PARSE_SIZE=1e6;static async initialize(){if(e.instance)return e.instance;let t=new e;return await t.init()?(e.instance=t,t):null}static get(){return e.instance}static async ensure(){let t=e.get();if(t)return t;let n=await e.initialize();if(!n)throw Error(`Failed to initialize WASM tree-sitter runtime`);return n}static dispose(){e.instance&&=(e.instance.cleanupParser(),null)}cleanupParser(){this.parser?.delete(),this.parser=null,this.languages.clear(),this.loadedGrammars.clear()}disableWasmParsing(){this.cleanupParser(),this.recoveryAttempts=e.MAX_RECOVERIES}async init(){try{let e;try{e=await P(),_.update({webTreeSitterImportable:!0})}catch(e){return _.update({webTreeSitterImportable:!1,mode:`regex`,reason:`web-tree-sitter import failed: ${String(e)}`,initError:String(e)}),N.warn(`web-tree-sitter module not importable`,{error:String(e)}),!1}let t=k();if(!t){N.info(`Parser WASM not found, attempting auto-heal...`);let e=M();e&&(D(e),t=k(),N.info(`Auto-heal succeeded`,{dir:e}))}if(!t)return _.update({mode:`regex`,reason:`Parser WASM file not found after heal attempt`}),N.warn(`Parser WASM file not found (auto-heal failed or skipped)`),!1;let n=t;return await e.init({locateFile:()=>n}),this.parser=new e,this.autoPopulateUserCache(),_.update({mode:`wasm`,reason:`WASM parser initialized successfully`,initError:null}),N.debug(`WASM tree-sitter parser initialized`),!0}catch(e){return _.update({mode:`regex`,reason:`WASM initialization failed: ${String(e)}`,initError:String(e)}),N.warn(`Failed to initialize WASM tree-sitter`,{error:String(e)}),!1}}async loadLanguage(e){let t=this.languages.get(e);if(t)return t;let n=O(e);if(!n)return null;let r=this.loadedGrammars.get(n);if(r)return this.languages.set(e,r),r;try{let t=await(await P()).Language.load(n);return this.loadedGrammars.set(n,t),this.languages.set(e,t),N.debug(`Loaded grammar`,{ext:e}),t}catch(t){return N.warn(`Failed to load grammar`,{ext:e,error:String(t)}),null}}async parse(t,n){if(!this.parser)return null;if(t.length>e.MAX_PARSE_SIZE)return N.warn(`File too large for WASM parser, skipping`,{ext:n,size:t.length}),null;let r=await this.loadLanguage(n);if(!r)return null;try{return this.parser.setLanguage(r),this.parser.parse(t)}catch(r){if(N.warn(`WASM parser crashed, attempting recovery...`,{ext:n,error:String(r)}),this.recoveryAttempts>=e.MAX_RECOVERIES)return N.error(`WASM parser recovery limit reached, falling back to null`,{attempts:this.recoveryAttempts}),this.disableWasmParsing(),null;if(!await this.recoverParser(n))return N.error(`WASM parser recovery failed, falling back to null`),null;try{return this.parser?.parse(t)??null}catch(e){return N.error(`WASM parser crashed again after recovery, falling back to null`,{error:String(e)}),null}}}async recoverParser(e){this.recoveryAttempts++;try{this.cleanupParser();let t=await P(),n=k();if(!n)return N.error(`Cannot recover: parser WASM file not found`),!1;let r=n;await t.init({locateFile:()=>r}),this.parser=new t;let i=await this.loadLanguage(e);return i?(this.parser.setLanguage(i),N.info(`WASM parser recovered successfully`,{attempt:this.recoveryAttempts}),!0):(N.error(`Cannot recover: failed to reload language`,{ext:e}),!1)}catch(e){return N.error(`WASM parser recovery threw`,{error:String(e)}),!1}}autoPopulateUserCache(){try{if(s(r(r(p(),`.aikit`,`cache`,`wasm`),`tree-sitter.wasm`)))return;let e=w();if(!e)return;N.info(`Auto-populating user WASM cache for future reliability`,{from:e}),j(e)}catch(e){N.debug(`Auto-populate user cache failed (non-fatal)`,{error:String(e)})}}hasLanguage(e){return x.has(e)}isLanguageLoaded(e){return this.languages.has(e)}getParser(){return this.parser}getLanguage(e){return this.languages.get(e)??null}};async function I(){return await F.initialize()!==null}const ie=a(`call-graph`),ae=new Set([`function_declaration`,`method_definition`,`arrow_function`,`function_definition`,`function_declaration`,`method_declaration`,`function_item`,`method_declaration`,`constructor_declaration`]),oe=new Set([`call_expression`,`new_expression`,`call`]);function se(e,t){let r=n(t).toLowerCase(),i=F.get();if(i?.isLanguageLoaded(r)){let n=i.getParser(),a=i.getLanguage(r);if(n&&a){n.setLanguage(a);let r;try{r=n.parse(e)}catch(e){return ie.warn(`WASM parser.parse() crashed in call-graph extraction`,{file:t,error:String(e)}),null}if(r){let e=[];return L(r.rootNode,t,`<module>`,e),e}}}return null}function L(e,t,n,r){if(!e)return;let i=n;if(ae.has(e.type)&&(i=ce(e)??n),oe.has(e.type)){let n=le(e);n&&r.push({callerFile:t,callerName:i,calleeName:n,line:(e.startPosition?.row??0)+1})}for(let n=0;n<(e.childCount??0);n++){let a=e.child(n);a&&L(a,t,i,r)}}function ce(e){for(let t=0;t<(e.childCount??0);t++){let n=e.child(t);if(n&&(n.type===`identifier`||n.type===`property_identifier`||n.type===`name`))return n.text??null}return null}function le(e){let t=e.childForFieldName?.(`function`)??e.child(0);return t?t.type===`identifier`||t.type===`name`?t.text??null:t.type===`member_expression`||t.type===`attribute`?(t.childForFieldName?.(`property`)??t.childForFieldName?.(`attribute`))?.text??null:e.type===`new_expression`?e.child(1)?.text??null:null:null}var R=class{maxChunkSize;constructor(e){this.maxChunkSize=e?.maxChunkSize??i.code.max}chunk(e,t){let n=this.findDeclarationBoundaries(e);if(n.length===0)return this.fallbackChunk(e,t);let r=[];for(let i=0;i<n.length;i++){let a=n[i].offset,o=i+1<n.length?n[i+1].offset:e.length,s=e.slice(a,o).trim();if(s=`// File: ${t.sourcePath}\n`+s,s.length>this.maxChunkSize){let n=this.splitByLines(s,this.maxChunkSize),i=this.getLineNumber(e,a);for(let e of n){let n=e.split(`
2
2
  `).length;r.push({text:e,sourcePath:t.sourcePath,contentType:t.contentType,chunkIndex:r.length,totalChunks:0,startLine:i,endLine:i+n-1}),i+=n}}else{let n=this.getLineNumber(e,a);r.push({text:s,sourcePath:t.sourcePath,contentType:t.contentType,chunkIndex:r.length,totalChunks:0,startLine:n,endLine:n+s.split(`
3
3
  `).length-1})}}if(n[0].offset>0){let i=e.slice(0,n[0].offset).trim();i.length>0&&r.unshift({text:`// File: ${t.sourcePath}\n${i}`,sourcePath:t.sourcePath,contentType:t.contentType,chunkIndex:0,totalChunks:0,startLine:1,endLine:this.getLineNumber(e,n[0].offset)-1})}return r.map((e,t)=>({...e,chunkIndex:t,totalChunks:r.length}))}findDeclarationBoundaries(e){let t=/^(?:export\s+)?(?:default\s+)?(?:async\s+)?(?:function|class|interface|type|const|enum|abstract\s+class)\s+(\w+)/gm,n=[],r;for(;(r=t.exec(e))!==null;){let t=e.lastIndexOf(`
4
4
  `,r.index-1)+1,i=t,a=e.slice(0,t).split(`