toolnet-memory 0.3.16 → 0.3.18
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/.env.example +74 -0
- package/.release-target +1 -1
- package/README.md +157 -7
- package/bin/toolnet-memory +12 -0
- package/bundle/agy-hook.js +59 -59
- package/bundle/agy.js +58 -58
- package/bundle/api.js +15 -15
- package/bundle/audit.js +2 -0
- package/bundle/background-refresh.js +7 -7
- package/bundle/code-capabilities.js +2 -0
- package/bundle/codex.js +63 -63
- package/bundle/context-runtime.js +41 -41
- package/bundle/context.js +15 -15
- package/bundle/copilot-hook.js +73 -73
- package/bundle/cursor-hook.js +73 -73
- package/bundle/docker-healthcheck.js +3 -0
- package/bundle/doctor.js +4 -4
- package/bundle/full-index.js +20 -19
- package/bundle/gc.js +5 -3
- package/bundle/graph-cli.js +1 -1
- package/bundle/graph-index.js +5 -4
- package/bundle/graph-ui.js +4 -3
- package/bundle/grok-hook.js +52 -52
- package/bundle/handoff-refresh.js +2 -2
- package/bundle/help-cli.js +4 -4
- package/bundle/impact.js +5 -4
- package/bundle/incremental.js +5 -5
- package/bundle/init.js +35 -35
- package/bundle/kiro-hook.js +74 -74
- package/bundle/learner.js +8 -6
- package/bundle/mcp.js +19 -17
- package/bundle/memory-agent.js +4 -4
- package/bundle/memory-query.js +3 -3
- package/bundle/opencode.js +73 -73
- package/bundle/project-manual.js +7 -7
- package/bundle/public/index.html +1089 -2306
- package/bundle/runtime.js +16 -13
- package/bundle/semantic.js +6 -5
- package/bundle/service.js +11 -6
- package/bundle/snapshot.js +6 -4
- package/bundle/status-cli.js +4 -4
- package/bundle/toolnet-cli.js +49 -49
- package/bundle/work-continuity.js +7 -7
- package/package.json +26 -2
- package/release-manifest.json +57 -1
package/bundle/full-index.js
CHANGED
|
@@ -1,24 +1,25 @@
|
|
|
1
|
-
import{closeSync as
|
|
2
|
-
`).replace(/\\r/g,"\r").replace(/\\t/g," ").replace(/\\"/g,'"').replace(/\\\\/g,"\\")):e.length>=2&&e.startsWith("'")&&e.endsWith("'")?e.slice(1,-1):e}function
|
|
3
|
-
`,{encoding:"utf8",mode:384}),er(s,r)}function A(n,e){return{id:n.id,name:n.name,remote:n.remote,rootPath:e,createdAt:n.createdAt,updatedAt:n.updatedAt,graphVersion:n.graphVersion,memoryVersion:n.memoryVersion,metadata:n.metadata}}function ze(n){return{version:1,scheme:U,canonicalRemote:n.canonicalRemote,fingerprint:n.fingerprint,repositoryName:n.repositoryName}}function or(n){let e=n.metadata?.toolnetIdentity;if(!e||typeof e!="object"||Array.isArray(e))return null;let t=e;return typeof t.fingerprint=="string"?t.fingerprint:null}var ie=class{adopt(e,t){let r=Ge(N(e));if(!t.id.trim())throw new Error("PROJECT_ADOPTION_INVALID_ID");if(!t.name.trim())throw new Error("PROJECT_ADOPTION_INVALID_NAME");if(!t.remote.trim())throw new Error("PROJECT_ADOPTION_INVALID_REMOTE");if(ut(r)){let a=re(O(r));if(a.id!==t.id)throw new Error(["PROJECT_IDENTITY_ALREADY_EXISTS",`existing=${a.id}`,`requested=${t.id}`].join(" "));return A(a,r)}let s=new Date().toISOString(),i={...t.metadata};t.gitIdentity&&(i.toolnetIdentity=ze(t.gitIdentity));let o={version:1,id:t.id.trim(),name:t.name.trim(),remote:t.remote.trim(),rootPath:r,createdAt:t.createdAt??s,updatedAt:s,graphVersion:t.graphVersion??0,memoryVersion:t.memoryVersion??0,metadata:Object.keys(i).length?i:void 0};return ne(r,o),A(o,r)}recordGitIdentity(e,t,r={}){let s=this.requireExisting(e),i=O(s.rootPath),o=re(i),a=or(o);if(a&&a!==t.fingerprint&&!r.allowRebind)throw new Error(["PROJECT_GIT_REMOTE_CHANGED",`existing=${a}`,`current=${t.fingerprint}`,"Use explicit rebind only when this repository identity change is intentional."].join(" "));let u=o.metadata?.toolnetIdentity;return u&&typeof u=="object"&&!Array.isArray(u)&&u.fingerprint===t.fingerprint||(o.metadata={...o.metadata,toolnetIdentity:ze(t)},o.updatedAt=new Date().toISOString(),ne(s.rootPath,o)),A(o,s.rootPath)}findExisting(e=process.cwd()){let t=N(e),r=Ge(t),i=[".git","package.json","pyproject.toml","Cargo.toml","go.mod","composer.json"].some(u=>se(W(r,u))),o=ct(t,i?r:void 0);if(!o)return null;let a=re(O(o));return A(a,o)}requireExisting(e=process.cwd()){let t=this.findExisting(e);if(!t)throw new Error("PROJECT_NOT_INITIALIZED");return t}detect(e=process.cwd()){let t=N(e),r=Ge(t),i=[".git","package.json","pyproject.toml","Cargo.toml","go.mod","composer.json"].some(f=>se(W(r,f))),o=ct(t,i?r:void 0);if(o){let f=O(o),d=re(f);return d.rootPath!==o&&(d.rootPath=o,d.updatedAt=new Date().toISOString(),ne(o,d)),A(d,o)}let a=new Date().toISOString(),u=rr(r),c=at(r),l={version:1,id:c?it(c.canonicalRemote):sr(r),name:u,remote:c?.repositoryName??u,rootPath:r,createdAt:a,updatedAt:a,graphVersion:0,memoryVersion:0,metadata:c?{toolnetIdentity:ze(c)}:void 0};return ne(r,l),A(l,r)}};var ir=[{type:"openai_key",regex:/\bsk-[A-Za-z0-9_-]{20,}\b/g,confidence:"exact"},{type:"huggingface_token",regex:/\bhf_[A-Za-z0-9]{20,}\b/g,confidence:"exact"},{type:"hf_s3_access_key",regex:/\bHFAK[A-Za-z0-9]{8,}\b/g,confidence:"exact"},{type:"aws_access_key",regex:/\b(?:AKIA|ASIA)[A-Z0-9]{16}\b/g,confidence:"exact"},{type:"github_token",regex:/\b(?:gh[pousr]_[A-Za-z0-9]{30,255}|github_pat_[A-Za-z0-9_]{40,255})\b/g,confidence:"exact"},{type:"stripe_secret_key",regex:/\b(?:sk|rk)_(?:live|test)_[A-Za-z0-9]{16,}\b/g,confidence:"exact"},{type:"google_api_key",regex:/\bAIza[A-Za-z0-9_-]{30,}\b/g,confidence:"exact"},{type:"slack_token",regex:/\bxox[baprs]-[A-Za-z0-9-]{16,}\b/g,confidence:"exact"},{type:"npm_token",regex:/\bnpm_[A-Za-z0-9]{20,}\b/g,confidence:"exact"},{type:"bearer_token",regex:/\bBearer\s+[A-Za-z0-9._~+/=-]{16,}\b/gi,confidence:"high"},{type:"jwt",regex:/\beyJ[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,}\b/g,confidence:"exact"},{type:"private_key",regex:/-----BEGIN (?:RSA |EC |DSA |OPENSSH )?PRIVATE KEY-----[\s\S]*?-----END (?:RSA |EC |DSA |OPENSSH )?PRIVATE KEY-----/g,confidence:"exact"},{type:"password_assignment",regex:/\b(?:password|passwd|pwd)\s*[:=]\s*["']?[^"' \t\r\n]{6,}["']?/gi,confidence:"high"},{type:"secret_assignment",regex:/\b(?:secret|api[_-]?key|access[_-]?token|refresh[_-]?token|client[_-]?secret|private[_-]?key)\s*[:=]\s*["']?[^"' \t\r\n]{8,}["']?/gi,confidence:"high"},{type:"cookie",regex:/\b(?:cookie|set-cookie)\s*[:=]\s*[^;\n]{8,}/gi,confidence:"high"},{type:"url_credentials",regex:/\bhttps?:\/\/[^:/@\s]+:[^/@\s]{4,}@[^/\s]+/gi,confidence:"high"}],ar=new Set(["example","example-key","example-token","changeme","change-me","password","secret","your-api-key","your-token","<token>","<secret>","<password>","[redacted]"]);function pt(n){return n.normalize("NFKC").trim().toLowerCase()}function cr(n){if(n.length===0)return 0;let e=new Map;for(let r of n)e.set(r,(e.get(r)??0)+1);let t=0;for(let r of e.values()){let s=r/n.length;t-=s*Math.log2(s)}return t}function lr(n){return/^[a-f0-9]{32}$/iu.test(n)||/^[a-f0-9]{40}$/iu.test(n)||/^[a-f0-9]{64}$/iu.test(n)}function dr(n,e,t){let r=n.slice(Math.max(0,e-48),e),s=n.slice(t,Math.min(n.length,t+16));return/\b(?:token|secret|key|credential|authorization|password|passwd|apikey|api_key|access[_-]?key)\b/iu.test(`${r} ${s}`)}function ur(n,e){return n.start<e.end&&e.start<n.end}function mt(n){return n.sort((e,t)=>e.start!==t.start?e.start-t.start:t.end-t.start-(e.end-e.start))}var ae=class{allowValues=new Set;enableEntropyHeuristic;constructor(e={}){for(let t of e.allowValues??[]){let r=pt(t);r&&this.allowValues.add(r)}this.enableEntropyHeuristic=e.enableEntropyHeuristic??!0}scan(e){let t=[];for(let i of ir){let o=new RegExp(i.regex.source,i.regex.flags);for(let a of e.matchAll(o))a.index===void 0||!a[0]||this.allowed(a[0])||t.push({type:i.type,value:a[0],start:a.index,end:a.index+a[0].length,confidence:i.confidence})}this.enableEntropyHeuristic&&t.push(...this.entropyMatches(e));let r=mt(t),s=[];for(let i of r)s.some(o=>ur(o,i))||s.push(i);return mt(s)}hasSecrets(e){return this.scan(e).length>0}allowed(e){let t=pt(e);return ar.has(t)?!0:this.allowValues.has(t)}entropyMatches(e){let t=[],r=/[A-Za-z0-9_+/=-]{32,160}/g;for(let s of e.matchAll(r)){if(s.index===void 0||!s[0])continue;let i=s[0];this.allowed(i)||lr(i)||!/[A-Za-z]/u.test(i)||!/[0-9]/u.test(i)||dr(e,s.index,s.index+i.length)&&(cr(i)<3.7||t.push({type:"high_entropy_secret",value:i,start:s.index,end:s.index+i.length,confidence:"heuristic"}))}return t}};var _=class{scanner;constructor(e={}){this.scanner=new ae(e)}sanitize(e){let t=this.scanner.scan(e);if(t.length===0)return{text:e,redacted:0,secretTypes:[]};let r=e,s=[...t].sort((o,a)=>a.start-o.start),i=new Set;for(let o of s)i.add(o.type),r=r.slice(0,o.start)+`[REDACTED:${o.type}]`+r.slice(o.end);return{text:r,redacted:t.length,secretTypes:[...i].sort()}}sanitizeValue(e){if(typeof e=="string")return this.sanitize(e).text;if(Array.isArray(e))return e.map(t=>this.sanitizeValue(t));if(e&&typeof e=="object"){let t={};for(let[r,s]of Object.entries(e)){let i=r.normalize("NFKC").toLowerCase().replace(/[^a-z0-9]+/g,"");if(i.includes("password")||i.includes("passwd")||i==="pwd"||i.includes("secret")||i.includes("token")||i.includes("cookie")||i.includes("authorization")||i.includes("apikey")||i.includes("accesskey")||i.includes("privatekey")||i.includes("clientsecret")||i.includes("credential")){t[r]="[REDACTED]";continue}t[r]=this.sanitizeValue(s)}return t}return e}};var vs=new _;import{lstat as gr,readdir as hr}from"node:fs/promises";import{extname as yr,join as Sr,relative as br,resolve as xr}from"node:path";import{basename as pr}from"node:path";var mr=new Set([".env",".env.local",".env.production",".env.development","id_rsa","id_ed25519","credentials","credentials.json","service-account.json"]),fr=new Set([".pem",".key",".p12",".pfx"]);function ft(n){let e=pr(n).toLowerCase();if(mr.has(e))return!0;for(let t of fr)if(e.endsWith(t))return!0;return!1}var Cr=2*1024*1024,vr=new Set([".ts",".tsx",".js",".jsx",".mjs",".cjs",".mts",".cts"]),Er=new Set(["node_modules",".git",".toolnet",".toolnet-memory","dist","build","out","coverage",".next",".nuxt",".cache",".parcel-cache",".turbo","vendor","target"]);function gt(n,e){if(!Number.isSafeInteger(n)||n<1)throw new Error(`${e} must be a positive integer`);return n}function ht(n){if(n?.aborted)throw new Error("Repository scan aborted")}function wr(n){let e=n.trim().toLowerCase();if(!e)throw new Error("Repository scan extension cannot be empty");return e.startsWith(".")?e:`.${e}`}function Pr(n){let e=n.toLowerCase();return/\.min\.(?:js|mjs|cjs)$/u.test(e)||/\.bundle\.(?:js|mjs|cjs)$/u.test(e)}function Rr(n){return n.replaceAll("\\","/")}async function yt(n,e={}){let t=xr(n),r=e.maxFileBytes??Cr;if(!Number.isSafeInteger(r)||r<1)throw new Error("maxFileBytes must be a positive integer");let s=e.maxFiles??Number.MAX_SAFE_INTEGER;gt(s,"maxFiles");let i=e.progressInterval??1e3;gt(i,"progressInterval");let o=new Set(e.extensions?[...e.extensions].map(wr):vr),a=new Set(Er);for(let f of e.extraIgnoredDirectories??[]){let d=f.trim();d&&a.add(d)}let u=[],c={directories:0,entries:0,accepted:0,skippedIgnoredDirectories:0,skippedSymlinks:0,skippedGenerated:0,skippedSensitive:0,skippedOversized:0,skippedUnreadable:0},l=[t];for(;l.length>0;){ht(e.signal);let f=l.pop(),d;try{d=await hr(f,{withFileTypes:!0})}catch{c.skippedUnreadable+=1;continue}c.directories+=1;for(let m of d){ht(e.signal),c.entries+=1,c.entries%i===0&&e.onProgress?.({directories:c.directories,entries:c.entries,accepted:c.accepted});let g=Sr(f,m.name);if(m.isSymbolicLink()){c.skippedSymlinks+=1;continue}if(m.isDirectory()){if(a.has(m.name)){c.skippedIgnoredDirectories+=1;continue}l.push(g);continue}if(!m.isFile())continue;let h=yr(m.name).toLowerCase();if(!o.has(h))continue;if(Pr(m.name)){c.skippedGenerated+=1;continue}if(ft(g)){c.skippedSensitive+=1;continue}let y;try{y=await gr(g)}catch{c.skippedUnreadable+=1;continue}if(y.isSymbolicLink()){c.skippedSymlinks+=1;continue}if(y.isFile()){if(y.size>r){c.skippedOversized+=1;continue}if(u.length>=s)throw new Error(`Repository scan file limit exceeded: ${s}`);u.push(Rr(br(t,g))),c.accepted+=1}}}return u.sort(),e.onProgress?.({directories:c.directories,entries:c.entries,accepted:c.accepted}),{files:u,stats:c}}import{createHash as kr}from"node:crypto";import{dirname as Tr,extname as Mr,normalize as Ir,posix as Ar}from"node:path";var ce=class{symbols=new Map;edges=new Map;addSymbol(e){this.symbols.set(e.id,e)}addEdge(e){this.edges.set(e.id,e)}getSymbol(e){return this.symbols.get(e)}findByName(e,t){return[...this.symbols.values()].filter(r=>r.projectId===e&&(r.name===t||r.qualifiedName===t))}allSymbols(e){return[...this.symbols.values()].filter(t=>t.projectId===e)}allEdges(e){return[...this.edges.values()].filter(t=>t.projectId===e)}import(e,t){for(let r of e)this.addSymbol(r);for(let r of t)this.addEdge(r)}clearProject(e){for(let[t,r]of this.symbols)r.projectId===e&&this.symbols.delete(t);for(let[t,r]of this.edges)r.projectId===e&&this.edges.delete(t)}};function Nr(n,e,t,r){return kr("sha256").update(`${n}:${e}:${t}:${r}`).digest("hex").slice(0,24)}function k(n){return Ir(n).replaceAll("\\","/")}function Lr(n,e,t){if(!e.startsWith("."))return;let r=k(Ar.join(Tr(n),e)),s=Mr(r),i;if(s===".js"){let o=r.slice(0,-3);i=[`${o}.ts`,`${o}.tsx`,`${o}.js`,`${o}.jsx`]}else if(s===".mjs"){let o=r.slice(0,-4);i=[`${o}.mts`,`${o}.ts`,`${o}.mjs`]}else if(s===".cjs"){let o=r.slice(0,-4);i=[`${o}.cts`,`${o}.ts`,`${o}.cjs`]}else s?i=[r]:i=[`${r}.ts`,`${r}.tsx`,`${r}.mts`,`${r}.cts`,`${r}.js`,`${r}.jsx`,`${r}.mjs`,`${r}.cjs`,`${r}/index.ts`,`${r}/index.tsx`,`${r}/index.mts`,`${r}/index.cts`,`${r}/index.js`,`${r}/index.jsx`,`${r}/index.mjs`,`${r}/index.cjs`];return i.find(o=>t.has(k(o)))}function Ke(n,e,t,r){let s=r?.(n,e,t);if(s){let i=k(s);if(t.has(i))return i}return Lr(n,e,t)}var H=class{build(e,t,r={}){let s=new ce;for(let a of r.seedSymbols??[])s.addSymbol(a);for(let a of t)for(let u of a.symbols)s.addSymbol(u);let i=new Map;for(let a of s.allSymbols(e))a.type==="file"&&i.set(k(a.filePath),a);let o=new Set(i.keys());for(let a of t){let u=k(a.filePath),c=i.get(u);if(c){for(let l of a.symbols)l.id!==c.id&&s.addEdge(this.edge(e,c.id,"DEFINES",l.id));for(let l of a.imports){let f=Ke(u,l.source,o,r.resolveModule);if(!f)continue;let d=i.get(f);d&&s.addEdge(this.edge(e,c.id,"IMPORTS",d.id,{source:l.source,resolvedFile:f}))}for(let l of a.heritage){let f=s.findByName(e,l.targetName).filter(d=>d.type==="class"||d.type==="interface");for(let d of f)s.addEdge(this.edge(e,l.fromId,l.type,d.id))}for(let l of a.calls){if(!l.callerId)continue;let f=this.resolveCallTargets(e,a,l.calleeName,l.qualifier,s,o,r.resolveModule);for(let d of f)d.id!==l.callerId&&s.addEdge(this.edge(e,l.callerId,"CALLS",d.id,{line:l.line,qualifier:l.qualifier}))}}}return s}resolveCallTargets(e,t,r,s,i,o,a){let u=k(t.filePath),c=i.findByName(e,r).filter(l=>k(l.filePath)===u);if(!s&&c.length)return c;if(s)for(let l of t.imports){if(!l.bindings.find(m=>m.kind==="namespace"&&m.localName===s))continue;let d=Ke(u,l.source,o,a);if(d)return i.findByName(e,r).filter(m=>k(m.filePath)===d)}for(let l of t.imports){let f=l.bindings.find(h=>h.localName===r);if(!f)continue;let d=Ke(u,l.source,o,a);if(!d)continue;let m=f.importedName==="default"?r:f.importedName,g=i.findByName(e,m).filter(h=>k(h.filePath)===d);if(g.length)return g}return i.findByName(e,r).filter(l=>l.type==="function"||l.type==="method")}edge(e,t,r,s,i){return{id:Nr(e,t,r,s),projectId:e,from:t,to:s,type:r,metadata:i}}};import{readFile as jr}from"node:fs/promises";import{join as Or}from"node:path";import{createHash as _r}from"node:crypto";import*as C from"typescript";function St(n,e){return _r("sha256").update(`${n}:${e}`).digest("hex").slice(0,24)}function Be(n,e){return n.getLineAndCharacterOfPosition(e.getStart(n)).line+1}async function Ue(n,e,t){let r=Or(e,t),s=await jr(r,"utf8"),i=C.createSourceFile(t,s,C.ScriptTarget.Latest,!0,t.endsWith(".tsx")||t.endsWith(".jsx")?C.ScriptKind.TSX:C.ScriptKind.TS),o=[],a=[],u=[],c=[],l={id:St(n,`file:${t}`),projectId:n,name:t,qualifiedName:t,type:"file",filePath:t,startLine:1,endLine:i.getLineAndCharacterOfPosition(i.end).line+1};o.push(l);let f=[],d=[];function m(p,S,x,v){let w=St(n,`${t}:${x}:${v??S}:${p.pos}`);return o.push({id:w,projectId:n,name:S,qualifiedName:v??S,type:x,filePath:t,startLine:Be(i,p),endLine:i.getLineAndCharacterOfPosition(p.end).line+1}),w}function g(p){if(!C.isStringLiteral(p.moduleSpecifier))return;let S=[],x=p.importClause;if(x?.name&&S.push({localName:x.name.text,importedName:"default",kind:"default"}),x?.namedBindings&&C.isNamedImports(x.namedBindings))for(let v of x.namedBindings.elements)S.push({localName:v.name.text,importedName:v.propertyName?.text??v.name.text,kind:"named"});x?.namedBindings&&C.isNamespaceImport(x.namedBindings)&&S.push({localName:x.namedBindings.name.text,importedName:"*",kind:"namespace"}),a.push({source:p.moduleSpecifier.text,bindings:S})}function h(p,S){for(let x of p.heritageClauses??[])for(let v of x.types){let w=v.expression.getText(i).split(".").at(-1)??"";w&&c.push({fromId:S,targetName:w,type:x.token===C.SyntaxKind.ExtendsKeyword?"INHERITS":"IMPLEMENTS"})}}function y(p){let S=!1,x=!1;if(C.isImportDeclaration(p)&&g(p),C.isInterfaceDeclaration(p)&&m(p,p.name.text,"interface",p.name.text),C.isClassDeclaration(p)&&p.name){let v=m(p,p.name.text,"class",p.name.text);h(p,v),d.push({id:v,name:p.name.text}),x=!0}if(C.isFunctionDeclaration(p)&&p.name){let v=m(p,p.name.text,"function",p.name.text);f.push(v),S=!0}if(C.isMethodDeclaration(p)&&p.name){let v=p.name.getText(i),w=d.at(-1)?.name,T=m(p,v,"method",w?`${w}.${v}`:v);f.push(T),S=!0}if(C.isCallExpression(p)&&(C.isIdentifier(p.expression)&&u.push({callerId:f.at(-1),calleeName:p.expression.text,line:Be(i,p)}),C.isPropertyAccessExpression(p.expression))){let v=p.expression;u.push({callerId:f.at(-1),qualifier:v.expression.getText(i),calleeName:v.name.text,line:Be(i,p)})}C.forEachChild(p,y),S&&f.pop(),x&&d.pop()}return y(i),{filePath:t,symbols:o,imports:a,calls:u,heritage:c}}function Dr(n){let e=n??4;if(!Number.isSafeInteger(e)||e<1||e>64)throw new Error("concurrency must be an integer between 1 and 64");return e}function Fr(n){if(n?.aborted)throw new Error("Operation aborted")}async function le(n,e,t={}){if(n.length===0)return[];let r=Math.min(Dr(t.concurrency),n.length),s=new Array(n.length),i=0,o=0,a=async()=>{for(;;){Fr(t.signal);let c=i;if(i+=1,c>=n.length)return;s[c]=await e(n[c],c),o+=1,t.onProgress?.({completed:o,total:n.length,index:c})}},u=Array.from({length:r},()=>a());return await Promise.all(u),s}var de=class{async index(e,t,r={}){let s={...r.scan};r.signal&&(s.signal=r.signal);let i=await yt(t,s),o=i.files;r.onProgress?.({phase:"scan",current:o.length,total:o.length});let a=r.maxWarnings??20;if(!Number.isSafeInteger(a)||a<0)throw new Error("maxWarnings must be a non-negative integer");let u=0,c=await le(o,async d=>{try{return await Ue(e,t,d)}catch(m){return u+=1,u<=a&&console.warn(`[indexer] skipped ${d}:`,m instanceof Error?m.message:m),null}},{concurrency:r.concurrency??4,signal:r.signal,onProgress:({completed:d,total:m,index:g})=>{r.onProgress?.({phase:"parse",current:d,total:m,file:o[g]})}});u>a&&console.warn(`[indexer] ${u-a} additional parse warnings suppressed`);let l=c.filter(d=>d!==null),f=new H().build(e,l);return{files:l.length,symbols:f.allSymbols(e).length,edges:f.allEdges(e).length,parseFailures:u,scan:i.stats,graph:f}}};import{execFile as $r}from"node:child_process";import{promisify as Gr}from"node:util";var jo=Gr($r);var V=class{constructor(e){this.storage=e}storage;key(e){return["projects",e,"graph","current.json"].join("/")}async load(e){let t=await this.storage.getText(this.key(e));return t?JSON.parse(t):null}async save(e){await this.storage.put(this.key(e.projectId),JSON.stringify(e),"application/json")}};import*as M from"typescript";import{createHash as Kr}from"node:crypto";import{readFile as Br}from"node:fs/promises";import{join as Ur}from"node:path";function xt(n){return Kr("sha256").update(n).digest("hex")}var ue=class{sanitizer=new _;async build(e,t,r,s={}){let i=s.maxLines??80,o=s.overlapLines??10,a=new Map;for(let c of r){let l=a.get(c.filePath)??[];l.push(c),a.set(c.filePath,l)}let u=[];for(let[c,l]of a){let f;try{f=await Br(Ur(t,c),"utf8")}catch{continue}let d=f.split(/\r?\n/),m=l.filter(g=>g.type!=="file"&&typeof g.startLine=="number"&&typeof g.endLine=="number");if(m.length>0)for(let g of m)u.push(...this.chunkRange({projectId:e,filePath:c,lines:d,symbol:g,startLine:g.startLine,endLine:g.endLine,maxLines:i,overlapLines:o}));else u.push(...this.chunkRange({projectId:e,filePath:c,lines:d,startLine:1,endLine:Math.max(1,d.length),maxLines:i,overlapLines:o}))}return u}chunkRange(e){let{projectId:t,filePath:r,lines:s,symbol:i,maxLines:o}=e,a=Math.min(e.overlapLines,Math.max(0,o-1)),u=Math.max(1,e.startLine),c=Math.min(s.length,Math.max(u,e.endLine)),l=[],f=u,d=0;for(;f<=c;){let m=Math.min(c,f+o-1),g=s.slice(f-1,m).join(`
|
|
4
|
-
`).trim();if(g){let h=this.sanitizer.sanitize(g).text,y=
|
|
5
|
-
`));return E.parseJsonConfigFileContent(s.config,E.sys,Yr(t))}return{fileNames:E.sys.readDirectory(e,[".ts",".tsx",".js",".jsx",".mts",".cts",".mjs",".cjs"],["node_modules",".git","dist","build","coverage"]),options:{allowJs:!0,checkJs:!1,target:E.ScriptTarget.ES2022,module:E.ModuleKind.NodeNext,moduleResolution:E.ModuleResolutionKind.NodeNext,skipLibCheck:!0,noEmit:!0}}}};import{createHash as Jr}from"node:crypto";import{dirname as Qr,relative as D,resolve as F}from"node:path";import*as b from"typescript";function R(n){return n.replaceAll("\\","/").replace(/^\.\//,"")}function We(...n){return Jr("sha256").update(n.join(":")).digest("hex").slice(0,24)}function Z(n,e){return n.getLineAndCharacterOfPosition(e.getStart(n)).line+1}var Zr=new Set(["get","post","put","patch","delete","options","head"]),Xr=new Set([b.SyntaxKind.EqualsToken,b.SyntaxKind.PlusEqualsToken,b.SyntaxKind.MinusEqualsToken,b.SyntaxKind.AsteriskEqualsToken,b.SyntaxKind.SlashEqualsToken,b.SyntaxKind.PercentEqualsToken,b.SyntaxKind.QuestionQuestionEqualsToken,b.SyntaxKind.AmpersandAmpersandEqualsToken,b.SyntaxKind.BarBarEqualsToken]),ye=class{constructor(e){this.graph=e}graph;enrich(e,t,r,s){let i={routes:0,tests:0,usesType:0,writes:0,callReferences:0,properties:0},o=this.loadConfig(t),a=b.createProgram({rootNames:o.fileNames,options:o.options}),u=a.getTypeChecker(),c=a.getSourceFiles().filter(f=>{if(f.isDeclarationFile)return!1;let d=R(D(t,F(f.fileName)));return!(d.startsWith("../")||d.includes("node_modules/"))}),l=0;for(let f of c){let d=R(D(t,F(f.fileName))),m=this.fileNode(e,d);if(!m)continue;let g=h=>{if(b.isCallExpression(h)&&this.addRoute(e,t,f,m,h,u)&&i.routes++,b.isTypeReferenceNode(h)&&this.addTypeUse(e,t,f,h,u)&&i.usesType++,b.isBinaryExpression(h)&&Xr.has(h.operatorToken.kind)){let y=this.addWrite(e,t,f,h.left,u);y.added&&(i.writes++,y.propertyCreated&&i.properties++)}if((b.isPrefixUnaryExpression(h)||b.isPostfixUnaryExpression(h))&&(h.operator===b.SyntaxKind.PlusPlusToken||h.operator===b.SyntaxKind.MinusMinusToken)){let y=this.addWrite(e,t,f,h.operand,u);y.added&&(i.writes++,y.propertyCreated&&i.properties++)}b.forEachChild(h,g)};g(f),l++,s?.({current:l,total:c.length,detail:d})}return i.tests+=this.addTestEdges(e),r&&(i.callReferences+=this.addResolvedCalls(e,r)),i}addRoute(e,t,r,s,i,o){if(!b.isPropertyAccessExpression(i.expression))return!1;let a=i.expression.name.text.toLowerCase();if(!Zr.has(a))return!1;let u=i.arguments[0];if(!u||!(b.isStringLiteral(u)||b.isNoSubstitutionTemplateLiteral(u)))return!1;let c=u.text;if(!c.startsWith("/"))return!1;let l=Z(r,i),f=We(e,r.fileName,l,a,c);this.graph.getSymbol(f)||(this.graph.addSymbol({id:f,projectId:e,name:`${a.toUpperCase()} ${c}`,qualifiedName:`${a.toUpperCase()} ${c}`,type:"route",filePath:s.filePath,startLine:l,endLine:l}),this.graph.addEdge(this.edge(e,s.id,"DEFINES",f)));let d=i.arguments.at(-1);if(d){let m=this.resolveNodeSymbol(e,t,d,o);m&&this.graph.addEdge(this.edge(e,f,"ROUTE",m.id,{method:a.toUpperCase(),path:c}))}return!0}addTypeUse(e,t,r,s,i){let o=this.resolveNodeSymbol(e,t,s.typeName,i);if(!o)return!1;let a=this.containingSymbol(e,R(D(t,F(r.fileName))),Z(r,s));return!a||a.id===o.id?!1:(this.graph.addEdge(this.edge(e,a.id,"USES_TYPE",o.id)),!0)}addWrite(e,t,r,s,i){let o=this.resolveTsSymbol(s,i),a=o?.valueDeclaration??o?.declarations?.[0];if(!a)return{added:!1,propertyCreated:!1};let u=a.getSourceFile();if(u.isDeclarationFile)return{added:!1,propertyCreated:!1};let c=R(D(t,F(u.fileName)));if(c.startsWith("../")||c.includes("node_modules/"))return{added:!1,propertyCreated:!1};let l=Z(u,a),f=this.findSymbolAt(e,c,l),d=!1;if(!f||f.type==="file"){let h=o?.getName()??s.getText(r),y=We(e,c,"property",h,l);if(f=this.graph.getSymbol(y),!f){f={id:y,projectId:e,name:h,qualifiedName:h,type:"property",filePath:c,startLine:l,endLine:l},this.graph.addSymbol(f);let p=this.fileNode(e,c);p&&this.graph.addEdge(this.edge(e,p.id,"DEFINES",f.id)),d=!0}}if(!f)return{added:!1,propertyCreated:d};let m=R(D(t,F(r.fileName))),g=this.containingSymbol(e,m,Z(r,s));return!g||g.id===f.id?{added:!1,propertyCreated:d}:(this.graph.addEdge(this.edge(e,g.id,"WRITES",f.id,{expression:s.getText(r)})),{added:!0,propertyCreated:d})}addTestEdges(e){let t=this.graph.allEdges(e),r=0;for(let s of t){if(s.type!=="IMPORTS")continue;let i=this.graph.getSymbol(s.from),o=this.graph.getSymbol(s.to);!i||!o||!this.isTestFile(i.filePath)||this.isTestFile(o.filePath)||(this.graph.addEdge(this.edge(e,i.id,"TESTS",o.id)),r++)}return r}addResolvedCalls(e,t){let r=0;for(let s of t.resolutions){if(s.kind!=="CALL"||!s.targetSymbolId)continue;let i=this.graph.getSymbol(s.targetSymbolId);if(!i)continue;let o=this.containingSymbol(e,s.sourceFile,s.sourceLine);!o||o.id===i.id||this.graph.allEdges(e).some(u=>u.type==="CALLS"&&u.from===o.id&&u.to===i.id)||(this.graph.addEdge(this.edge(e,o.id,"CALL_REFERENCE",i.id,{expression:s.expression,confidence:s.confidence,resolver:s.resolver})),r++)}return r}resolveNodeSymbol(e,t,r,s){let i=this.resolveTsSymbol(r,s),o=i?.valueDeclaration??i?.declarations?.[0];if(!o)return;let a=o.getSourceFile();if(a.isDeclarationFile)return;let u=R(D(t,F(a.fileName)));if(!(u.startsWith("../")||u.includes("node_modules/")))return this.findSymbolAt(e,u,Z(a,o),i?.getName())}resolveTsSymbol(e,t){let r=t.getSymbolAtLocation(e);if(r&&r.flags&b.SymbolFlags.Alias)try{r=t.getAliasedSymbol(r)}catch{}return r}containingSymbol(e,t,r){return this.graph.allSymbols(e).filter(s=>R(s.filePath)===R(t)&&typeof s.startLine=="number"&&typeof s.endLine=="number"&&s.startLine<=r&&s.endLine>=r).sort((s,i)=>(s.endLine??0)-(s.startLine??0)-((i.endLine??0)-(i.startLine??0)))[0]}findSymbolAt(e,t,r,s){let i=this.graph.allSymbols(e).filter(o=>R(o.filePath)===R(t)&&typeof o.startLine=="number"&&typeof o.endLine=="number"&&o.startLine<=r&&o.endLine>=r).sort((o,a)=>(o.endLine??0)-(o.startLine??0)-((a.endLine??0)-(a.startLine??0)));if(s){let o=i.find(a=>a.name===s||(a.qualifiedName?.endsWith(`.${s}`)??!1));if(o)return o}return i[0]}fileNode(e,t){return this.graph.allSymbols(e).find(r=>r.type==="file"&&R(r.filePath)===R(t))}isTestFile(e){let t=R(e).toLowerCase();return t.startsWith("tests/")||t.includes("/tests/")||t.includes("__tests__")||/\.(test|spec)\.[^.]+$/.test(t)}edge(e,t,r,s,i){return{id:We(e,t,r,s,JSON.stringify(i??{})),projectId:e,from:t,to:s,type:r,metadata:i}}loadConfig(e){let t=b.findConfigFile(e,b.sys.fileExists,"tsconfig.json");if(t){let r=b.readConfigFile(t,b.sys.readFile);return b.parseJsonConfigFileContent(r.config,b.sys,Qr(t))}return{fileNames:b.sys.readDirectory(e,[".ts",".tsx",".js",".jsx",".mts",".cts",".mjs",".cjs"],["node_modules",".git","dist","build","coverage"]),options:{allowJs:!0,noEmit:!0,skipLibCheck:!0,target:b.ScriptTarget.ES2022,module:b.ModuleKind.NodeNext,moduleResolution:b.ModuleResolutionKind.NodeNext}}}};function en(n){return n.replaceAll("\\","/").toLowerCase()}var $=class{constructor(e){this.graph=e}graph;detect(e){let t=this.graph.allSymbols(e),r=this.graph.allEdges(e),s=[];for(let o of t){if(o.type!=="route"&&o.type!=="file")continue;let a=this.classify(o,r);a&&s.push({symbolId:o.id,filePath:o.filePath,name:o.name,kind:a.kind,score:a.score,reasons:a.reasons})}let i=new Map;for(let o of s){let a=o.kind==="route"?o.symbolId:o.filePath,u=i.get(a);(!u||o.score>u.score)&&i.set(a,o)}return[...i.values()].sort((o,a)=>a.score-o.score||o.filePath.localeCompare(a.filePath))}classify(e,t){let r=en(e.filePath),s=[];if(e.type==="route")return{kind:"route",score:100,reasons:["HTTP route"]};let i=null,o=0;if(r.startsWith("bin/"))i="cli",o=100,s.push("bin executable");else if(/(^|\/)(cli)\.[^.]+$/.test(r))i="cli",o=90,s.push("CLI entry file");else if(/(^|\/)(main|server)\.[^.]+$/.test(r))i="main",o=90,s.push("main/server file");else if(r.includes("/runtime/")&&/(^|\/)(bootstrap|index)\.[^.]+$/.test(r))i="runtime",o=75,s.push("runtime bootstrap");else if(/(^|\/)index\.[^.]+$/.test(r)){let u=t.filter(l=>l.to===e.id&&l.type!=="DEFINES").length,c=t.filter(l=>l.from===e.id&&l.type!=="DEFINES").length;(r.startsWith("packages/")||u===0||c>0)&&(i="package",o=r.startsWith("packages/")?70:55,s.push("package/public index"))}return i?(t.filter(u=>u.to===e.id&&u.type!=="DEFINES").length===0&&(o+=10,s.push("no incoming dependency")),{kind:i,score:o,reasons:s}):null}};var tn={CALL_REFERENCE:6,CALLS:4,IMPORTS:3,USES_TYPE:4,WRITES:5,ROUTE:4,TESTS:1,INHERITS:5,IMPLEMENTS:5,DEFINES:.25},Se=class{constructor(e){this.graph=e}graph;analyze(e){let t=this.graph.allSymbols(e),r=this.graph.allEdges(e),s=new Map(t.map(c=>[c.id,c])),i=new Map,o=c=>{let l=i.get(c);return l||(l={symbols:0,incoming:0,outgoing:0,weightedIncoming:0,weightedOutgoing:0,writes:0,callReferences:0,crossFile:0},i.set(c,l)),l};for(let c of t)o(c.filePath).symbols++;for(let c of r){let l=s.get(c.from),f=s.get(c.to);if(!l||!f)continue;let d=tn[c.type]??1,m=o(l.filePath),g=o(f.filePath);m.outgoing++,g.incoming++,m.weightedOutgoing+=d,g.weightedIncoming+=d,c.type==="WRITES"&&m.writes++,c.type==="CALL_REFERENCE"&&m.callReferences++,l.filePath!==f.filePath&&(m.crossFile++,g.crossFile++)}let a=[...i.entries()].map(([c,l])=>{let f=Number((l.symbols*.4+l.weightedIncoming*1.75+l.weightedOutgoing*.65+l.crossFile*1.5+l.writes*3+l.callReferences*2).toFixed(2)),d=[];return l.incoming>=10&&d.push("high incoming dependency"),l.crossFile>=10&&d.push("high cross-file coupling"),l.callReferences>=5&&d.push("many exact call references"),l.writes>0&&d.push("contains write relationships"),l.symbols>=10&&d.push("high symbol density"),{filePath:c,score:f,symbols:l.symbols,incoming:l.incoming,outgoing:l.outgoing,reasons:d}}).filter(c=>c.score>0).sort((c,l)=>l.score-c.score||c.filePath.localeCompare(l.filePath));if(a.length===0)return[];let u=Math.max(1,Math.min(30,Math.ceil(a.length*.15)));return a.slice(0,u)}};function rn(n){return n.replaceAll("\\","/").toLowerCase()}var nn={CALL_REFERENCE:6,CALLS:4,IMPORTS:4,USES_TYPE:4,WRITES:5,INHERITS:6,IMPLEMENTS:6,ROUTE:3,TESTS:1},be=class{constructor(e){this.graph=e}graph;detect(e){let t=this.graph.allSymbols(e),r=this.graph.allEdges(e),s=new Map(t.map(a=>[a.id,a])),i=[...new Set(t.map(a=>a.filePath))].sort(),o=new Map;for(let a of i)o.set(a,this.classifyPath(a));for(let a=0;a<3;a++){let u=!1;for(let c of i){let l=o.get(c);if(!l||l.layer!=="unknown")continue;let f=new Map;for(let m of r){let g=s.get(m.from),h=s.get(m.to);if(!g||!h||g.filePath===h.filePath)continue;let y=null;if(g.filePath===c?y=h.filePath:h.filePath===c&&(y=g.filePath),!y)continue;let p=o.get(y);if(!p||p.layer==="unknown"||p.layer==="tests")continue;let S=nn[m.type]??1;f.set(p.layer,(f.get(p.layer)??0)+S)}let d=[...f.entries()].sort((m,g)=>g[1]-m[1]||m[0].localeCompare(g[0]))[0];d&&d[1]>=4&&(o.set(c,{filePath:c,layer:d[0],confidence:.65,reasons:[`graph-neighbor vote=${d[1]}`]}),u=!0)}if(!u)break}return i.map(a=>o.get(a))}classifyPath(e){let t=rn(e);return t.startsWith("tests/")||t.includes("/tests/")||t.includes("__tests__")||/\.(test|spec)\.[^.]+$/.test(t)?{filePath:e,layer:"tests",confidence:1,reasons:["test path"]}:t.startsWith("bin/")||t.includes("/mcp/")||t.includes("/cli/")||t.includes("/api/")||t.includes("/routes/")||t.includes("/hooks/")?{filePath:e,layer:"interface",confidence:.9,reasons:["interface boundary"]}:t.includes("/runtime/")||t.includes("/processor/")||t.includes("/retrieval/")||t.includes("/snapshot/")||t.includes("/code-intelligence/")?{filePath:e,layer:"application",confidence:.85,reasons:["application orchestration"]}:t.includes("/core/")||t.includes("/memory/")||/(^|\/)types\.[^.]+$/.test(t)?{filePath:e,layer:"domain",confidence:.9,reasons:["domain/core logic"]}:t.includes("/storage/")||t.includes("/production/")||t.includes("/security/")||t.includes("/config/")||t.includes("/utils/")?{filePath:e,layer:"infrastructure",confidence:.85,reasons:["infrastructure boundary"]}:{filePath:e,layer:"unknown",confidence:.35,reasons:[]}}};import{createHash as sn}from"node:crypto";var on={CALL_REFERENCE:7,CALLS:3,IMPORTS:4,USES_TYPE:4,WRITES:5,INHERITS:7,IMPLEMENTS:7,ROUTE:4,TESTS:1};function an(n){return sn("sha256").update(n).digest("hex").slice(0,12)}function xe(n){return n.replaceAll("\\","/")}function cn(n){let t=xe(n).split("/").filter(Boolean);return t[0]==="src"&&t[1]?`src/${t[1]}`:t[0]==="packages"&&t[1]?`packages/${t[1]}`:t[0]==="tests"?"tests":t[0]==="bin"?"bin":t[0]??"root"}function ln(n){let e=n.replace(/^src\//,"").replace(/^packages\//,"");return{"code-intelligence":"Code Intelligence",memory:"Memory",storage:"Storage",retrieval:"Retrieval",runtime:"Runtime",mcp:"MCP",hooks:"Hooks",security:"Security",snapshot:"Snapshots",production:"Production",capture:"Capture",processor:"Processor",core:"Core",tests:"Tests",bin:"CLI",sdk:"SDK",cli:"CLI"}[e]??e}var Ce=class{constructor(e){this.graph=e}graph;detect(e){let t=this.graph.allSymbols(e),r=this.graph.allEdges(e),s=new Map(t.map(d=>[d.id,d])),i=[...new Set(t.map(d=>xe(d.filePath)))].sort(),o=new Map,a=(d,m)=>{let g=[d,m].sort();return`${g[0]}\0${g[1]}`};for(let d of r){let m=s.get(d.from),g=s.get(d.to);if(!m||!g)continue;let h=xe(m.filePath),y=xe(g.filePath);if(h===y)continue;let p=on[d.type]??0;if(p<=0)continue;let S=a(h,y),x=o.get(S);x?x.weight+=p:o.set(S,{a:[h,y].sort()[0],b:[h,y].sort()[1],weight:p})}let u=new Map;for(let d of i){let m=cn(d),g=u.get(m)??[];g.push(d),u.set(m,g)}let c=[];for(let[d,m]of u){if(m.sort(),m.length<=30){c.push({subsystem:d,files:m});continue}let g=new Set(m),h=new Map;for(let x of m)h.set(x,new Set);for(let x of o.values())!g.has(x.a)||!g.has(x.b)||x.weight>=8&&(h.get(x.a)?.add(x.b),h.get(x.b)?.add(x.a));let y=new Set,p=[];for(let x of m){if(y.has(x))continue;let v=[x],w=[];for(y.add(x);v.length>0;){let T=v.shift();w.push(T);for(let Fe of h.get(T)??[])y.has(Fe)||(y.add(Fe),v.push(Fe))}p.push(w.sort())}let S=[];for(let x of p)x.length>=2?c.push({subsystem:d,files:x}):S.push(...x);S.length>0&&c.push({subsystem:`${d}/residual`,files:S.sort()})}let l=c.map(d=>({subsystem:d.subsystem,files:[...d.files]}));for(let d=0;d<3;d++){let m=!1;e:for(let g=0;g<l.length;g++){let h=l[g];if(!(!h||h.files.length>3))for(let y=0;y<l.length;y++){if(g===y)continue;let p=l[y];if(!p)continue;let S=0,x=new Set(h.files),v=new Set(p.files);for(let w of o.values())(x.has(w.a)&&v.has(w.b)||x.has(w.b)&&v.has(w.a))&&(S+=w.weight);if(S>=20){p.files=[...new Set([...p.files,...h.files])].sort(),l.splice(g,1),m=!0;break e}}}if(!m)break}let f=[];for(let d of l){let m=new Set(d.files),g=0,h=0;for(let v of o.values()){let w=m.has(v.a),T=m.has(v.b);w&&T?g+=v.weight:(w||T)&&(h+=v.weight)}let y=g+h,p=d.subsystem.replace(/\/residual$/,""),S=ln(p),x=d.subsystem.endsWith("/residual")?`${S} Misc`:S;f.push({id:`cluster-${an(`${d.subsystem}:${d.files.join("|")}`)}`,label:x,subsystem:p,files:d.files,size:d.files.length,internalWeight:g,externalWeight:h,cohesion:y>0?Number((g/y).toFixed(4)):0})}return f.sort((d,m)=>m.size-d.size||m.cohesion-d.cohesion||d.label.localeCompare(m.label))}};var ve=class{constructor(e){this.graph=e;this.entryPoints=new $(e),this.hotspots=new Se(e),this.layers=new be(e),this.clusters=new Ce(e)}graph;entryPoints;hotspots;layers;clusters;analyze(e){let t=this.entryPoints.detect(e),r=this.hotspots.analyze(e),s=this.layers.detect(e),i=this.clusters.detect(e),o=new Set(this.graph.allSymbols(e).map(a=>a.filePath));return{version:1,projectId:e,updatedAt:new Date().toISOString(),summary:{files:o.size,entryPoints:t.length,hotspots:r.length,layers:s.length,clusters:i.length},entryPoints:t,hotspots:r,layers:s,clusters:i}}};var wt=new Set(["CALLS","CALL_REFERENCE","IMPORTS","USES_TYPE","WRITES","INHERITS","IMPLEMENTS","ROUTE","TESTS"]);function He(n){return n.replaceAll("\\","/").toLowerCase()}function dn(n){let e=He(n);return e.startsWith("tests/")||e.includes("/tests/")||e.includes("__tests__")||/\.(test|spec)\.[^.]+$/.test(e)}function un(n){let e=He(n.filePath);return e.startsWith("bin/")||/(^|\/)index\.[^.]+$/.test(e)||/(^|\/)(main|server|cli)\.[^.]+$/.test(e)}var Ee=class{constructor(e){this.graph=e}graph;analyze(e){let t=this.graph.allSymbols(e),r=this.graph.allEdges(e),s=new Set(new $(this.graph).detect(e).map(o=>o.symbolId)),i=[];for(let o of t){if(!["function","method","class","interface","property"].includes(o.type)||dn(o.filePath)||s.has(o.id)||un(o)||r.filter(m=>m.to===o.id&&wt.has(m.type)).length>0)continue;let u=r.filter(m=>m.from===o.id&&wt.has(m.type)),c=["no incoming usage edge"],l=50;(o.type==="function"||o.type==="method")&&(l+=20,c.push("callable symbol")),o.type==="property"&&(l-=15),u.length===0&&(l+=10,c.push("no outgoing dependency")),He(o.filePath).includes("/internal/")&&(l+=10,c.push("internal implementation")),o.name.startsWith("_")&&(l+=5);let d=l>=75?"high":l>=55?"medium":"low";i.push({symbolId:o.id,name:o.name,qualifiedName:o.qualifiedName,type:o.type,filePath:o.filePath,startLine:o.startLine,confidence:d,score:l,reasons:c})}return i.sort((o,a)=>a.score-o.score||o.filePath.localeCompare(a.filePath)||o.name.localeCompare(a.name))}};var pn=new Set(["CALLS","CALL_REFERENCE","IMPORTS","USES_TYPE","WRITES","INHERITS","IMPLEMENTS","ROUTE"]),we=class{constructor(e){this.graph=e}graph;analyze(e){let t=this.graph.allSymbols(e),r=this.graph.allEdges(e).filter(o=>pn.has(o.type)),s=new Map(t.map(o=>[o.id,o]));return[...new Set(t.map(o=>o.filePath))].sort().map(o=>{let a=new Set,u=new Set,c=0,l=0;for(let f of r){let d=s.get(f.from),m=s.get(f.to);!d||!m||d.filePath===m.filePath||(d.filePath===o&&(a.add(m.filePath),c++),m.filePath===o&&(u.add(d.filePath),l++))}return{filePath:o,dependencies:[...a].sort(),dependents:[...u].sort(),outgoingEdges:c,incomingEdges:l}})}};var Pe=class{constructor(e){this.graph=e}graph;analyze(e){let t=new Ee(this.graph).analyze(e),r=new we(this.graph).analyze(e);return{version:1,projectId:e,updatedAt:new Date().toISOString(),summary:{deadCodeCandidates:t.length,highConfidenceDeadCode:t.filter(s=>s.confidence==="high").length,files:r.length},deadCode:t,dependencies:r}}};var Re=class{constructor(e){this.graph=e}graph;build(e,t,r,s){let i=this.graph.allSymbols(e),o=this.graph.allEdges(e),a=new Map(t?.layers.map(p=>[p.filePath,p.layer])??[]),u=new Map;for(let p of t?.clusters??[])for(let S of p.files)u.set(S,{id:p.id,label:p.label});let c=new Map(t?.hotspots.map(p=>[p.filePath,p.score])??[]),l=new Map(r?.deadCode.map(p=>[p.symbolId,{confidence:p.confidence,score:p.score}])??[]),f=new Map,d=new Map;for(let p of o)d.set(p.from,(d.get(p.from)??0)+1),f.set(p.to,(f.get(p.to)??0)+1);let m=[];for(let p=0;p<i.length;p++){let S=i[p],x=u.get(S.filePath);m.push({id:S.id,name:S.name,qualifiedName:S.qualifiedName,type:S.type,filePath:S.filePath,layer:a.get(S.filePath),cluster:x?.id,clusterLabel:x?.label,hotspotScore:c.get(S.filePath),deadCode:l.get(S.id),incoming:f.get(S.id)??0,outgoing:d.get(S.id)??0}),((p+1)%100===0||p===i.length-1)&&s?.({current:p+1,total:i.length,phase:"nodes"})}let g=new Set(m.map(p=>p.id)),h=o.filter(p=>g.has(p.from)&&g.has(p.to)),y=[];for(let p=0;p<h.length;p++){let S=h[p];y.push({source:S.from,target:S.to,type:S.type,confidence:typeof S.metadata?.confidence=="string"?S.metadata.confidence:void 0,resolver:typeof S.metadata?.resolver=="string"?S.metadata.resolver:void 0}),((p+1)%100===0||p===h.length-1)&&s?.({current:p+1,total:h.length,phase:"links"})}return{version:1,projectId:e,generatedAt:new Date().toISOString(),summary:{nodes:m.length,links:y.length,files:new Set(m.map(p=>p.filePath)).size,clusters:new Set(m.map(p=>p.cluster).filter(Boolean)).size},nodes:m,links:y}}};import{homedir as On}from"node:os";import{join as _n}from"node:path";import{DeleteObjectCommand as mn,GetObjectCommand as fn,HeadObjectCommand as gn,ListObjectsV2Command as hn,PutObjectCommand as yn,S3Client as Sn}from"@aws-sdk/client-s3";import{getSignedUrl as bn}from"@aws-sdk/s3-request-presigner";var ke=class{name="huggingface";client;bucket;constructor(e){this.bucket=e.bucket,this.client=new Sn({region:"us-east-1",endpoint:`https://s3.hf.co/${e.namespace}`,forcePathStyle:!0,requestChecksumCalculation:"WHEN_REQUIRED",responseChecksumValidation:"WHEN_REQUIRED",credentials:{accessKeyId:e.accessKeyId,secretAccessKey:e.secretAccessKey}})}async put(e,t,r="application/octet-stream"){let s=typeof t=="string"?Buffer.from(t,"utf8"):t;await this.client.send(new yn({Bucket:this.bucket,Key:e,Body:s,ContentType:r}))}async get(e){let t=await bn(this.client,new fn({Bucket:this.bucket,Key:e}),{expiresIn:60}),r=await fetch(t,{redirect:"follow"});if(r.status===404)return null;if(!r.ok)throw new Error(`HF download failed: ${r.status} ${r.statusText}`);return new Uint8Array(await r.arrayBuffer())}async getText(e){let t=await this.get(e);return t?Buffer.from(t).toString("utf8"):null}async exists(e){try{return await this.client.send(new gn({Bucket:this.bucket,Key:e})),!0}catch(t){if(typeof t=="object"&&t!==null&&"$metadata"in t&&t.$metadata?.httpStatusCode===404)return!1;throw t}}async delete(e){await this.client.send(new mn({Bucket:this.bucket,Key:e}))}async list(e=""){let t=[],r;do{let s=await this.client.send(new hn({Bucket:this.bucket,Prefix:e||void 0,ContinuationToken:r}));for(let i of s.Contents??[])i.Key&&t.push({key:i.Key,size:i.Size,updatedAt:i.LastModified?.toISOString()});r=s.IsTruncated?s.NextContinuationToken:void 0}while(r);return t}};import{access as Pt,mkdir as xn,readFile as Cn,readdir as vn,rm as En,stat as Rt,writeFile as wn}from"node:fs/promises";import{dirname as Pn,join as Rn,relative as kt,resolve as kn}from"node:path";var X=class{constructor(e){this.root=e}root;name="local";path(e){let t=e.replace(/^\/+/,"");return kn(this.root,t)}async put(e,t){let r=this.path(e);await xn(Pn(r),{recursive:!0}),await wn(r,t)}async get(e){try{return await Cn(this.path(e))}catch(t){if(typeof t=="object"&&t!==null&&"code"in t&&t.code==="ENOENT")return null;throw t}}async getText(e){let t=await this.get(e);return t?Buffer.from(t).toString("utf8"):null}async exists(e){try{return await Pt(this.path(e)),!0}catch{return!1}}async delete(e){await En(this.path(e),{force:!0})}async list(e=""){let t=this.path(e),r=[];try{await Pt(t)}catch{return r}let s=async o=>{let a=await vn(o,{withFileTypes:!0});for(let u of a){let c=Rn(o,u.name);if(u.isDirectory()){await s(c);continue}let l=await Rt(c);r.push({key:kt(this.root,c),size:l.size,updatedAt:l.mtime.toISOString()})}},i=await Rt(t);return i.isDirectory()?await s(t):r.push({key:kt(this.root,t),size:i.size,updatedAt:i.mtime.toISOString()}),r}};import{DeleteObjectCommand as Tn,GetObjectCommand as Mn,HeadObjectCommand as In,ListObjectsV2Command as An,PutObjectCommand as Nn,S3Client as Ln}from"@aws-sdk/client-s3";import{getSignedUrl as jn}from"@aws-sdk/s3-request-presigner";var ee=class{name;client;bucket;constructor(e){this.name=e.name??"s3",this.bucket=e.bucket,this.client=new Ln({region:e.region??"us-east-1",endpoint:e.endpoint||void 0,forcePathStyle:e.forcePathStyle??!1,requestChecksumCalculation:"WHEN_REQUIRED",responseChecksumValidation:"WHEN_REQUIRED",credentials:{accessKeyId:e.accessKeyId,secretAccessKey:e.secretAccessKey}})}async put(e,t,r="application/octet-stream"){let s=typeof t=="string"?Buffer.from(t,"utf8"):t;await this.client.send(new Nn({Bucket:this.bucket,Key:e,Body:s,ContentType:r}))}async get(e){let t=await jn(this.client,new Mn({Bucket:this.bucket,Key:e}),{expiresIn:60}),r=await fetch(t,{redirect:"follow"});if(r.status===404)return null;if(!r.ok)throw new Error(`${this.name} download failed: ${r.status} ${r.statusText}`);return new Uint8Array(await r.arrayBuffer())}async getText(e){let t=await this.get(e);return t?Buffer.from(t).toString("utf8"):null}async exists(e){try{return await this.client.send(new In({Bucket:this.bucket,Key:e})),!0}catch(t){if(typeof t=="object"&&t!==null&&"$metadata"in t&&t.$metadata?.httpStatusCode===404)return!1;throw t}}async delete(e){await this.client.send(new Tn({Bucket:this.bucket,Key:e}))}async list(e=""){let t=[],r;do{let s=await this.client.send(new An({Bucket:this.bucket,Prefix:e||void 0,ContinuationToken:r}));for(let i of s.Contents??[])i.Key&&t.push({key:i.Key,size:i.Size,updatedAt:i.LastModified?.toISOString()});r=s.IsTruncated?s.NextContinuationToken:void 0}while(r);return t}};function Ve(n,e){return console.warn(e),new X(n)}function Tt(n){let e=n.localRoot??_n(On(),".toolnet-memory","storage");if(n.provider==="r2"){let t=n.r2;return t?.accountId&&t.bucket&&t.accessKeyId&&t.secretAccessKey?new ee({name:"r2",endpoint:`https://${t.accountId}.r2.cloudflarestorage.com`,region:"auto",bucket:t.bucket,forcePathStyle:!0,accessKeyId:t.accessKeyId,secretAccessKey:t.secretAccessKey}):Ve(e,"[storage] Cloudflare R2 credentials missing. Using local fallback.")}if(n.provider==="s3"){let t=n.s3;return t?.bucket&&t.accessKeyId&&t.secretAccessKey?new ee({name:"s3",endpoint:t.endpoint,region:t.region??"us-east-1",bucket:t.bucket,forcePathStyle:t.forcePathStyle??!1,accessKeyId:t.accessKeyId,secretAccessKey:t.secretAccessKey}):Ve(e,"[storage] S3 credentials missing. Using local fallback.")}if(n.provider==="huggingface"){let t=n.huggingface;return t?.namespace&&t.bucket&&t.accessKeyId&&t.secretAccessKey?new ke({namespace:t.namespace,bucket:t.bucket,accessKeyId:t.accessKeyId,secretAccessKey:t.secretAccessKey}):Ve(e,"[storage] Hugging Face credentials missing. Using local fallback.")}return new X(e)}function Dn(n){return new Promise(e=>setTimeout(e,n))}async function Mt(n,e={}){let t=Math.max(1,e.attempts??3),r=e.baseDelayMs??150,s=e.maxDelayMs??2e3,i;for(let o=1;o<=t;o++)try{return await n()}catch(a){if(i=a,o>=t)break;let u=Math.min(s,r*2**(o-1)),c=Math.floor(Math.random()*Math.max(1,u*.2));await Dn(u+c)}throw i}var Fn=new Set(["put","get","getText","delete","list"]);function It(n,e={}){return new Proxy(n,{get(t,r){let s=Reflect.get(t,r,t);return typeof s!="function"?s:Fn.has(r)?(...i)=>Mt(()=>Promise.resolve(s.apply(t,i)),e):s.bind(t)}})}function At(n){let e=n.trim().replace(/\s+/g,"_").replace(/[^A-Za-z0-9._-]/g,"_").replace(/_+/g,"_").replace(/^\.+|\.+$/g,"").slice(0,100);if(!e||e==="."||e==="..")throw new Error("Invalid project storage folder");return e}function Nt(n){let e=n.replace(/^\/+/,"");if(e.startsWith("memories/"))return"memory/records/"+e.slice(9);if(e.startsWith("vectors/"))return"memory/vectors/"+e.slice(8);if(e.startsWith("graph/"))return"code/graph/"+e.slice(6);let t=e.match(/^(snapshots\/[^/]+\/)memories\/(.+)$/);if(t)return`${t[1]}memory/records/${t[2]}`;if(t=e.match(/^(snapshots\/[^/]+\/)vectors\/(.+)$/),t)return`${t[1]}memory/vectors/${t[2]}`;if(t=e.match(/^(snapshots\/[^/]+\/)graph\/(.+)$/),t)return`${t[1]}code/graph/${t[2]}`;let r=e.match(/^(projects\/[^/]+\/snapshots\/[^/]+\/)memories\/(.+)$/);if(r)return`${r[1]}memory/records/${r[2]}`;if(r=e.match(/^(projects\/[^/]+\/snapshots\/[^/]+\/)vectors\/(.+)$/),r)return`${r[1]}memory/vectors/${r[2]}`;if(r=e.match(/^(projects\/[^/]+\/snapshots\/[^/]+\/)graph\/(.+)$/),r)return`${r[1]}code/graph/${r[2]}`;let s=e.match(/^(projects\/[^/]+\/)memories\/(.+)$/);return s?`${s[1]}memory/records/${s[2]}`:(s=e.match(/^(projects\/[^/]+\/)vectors\/(.+)$/),s?`${s[1]}memory/vectors/${s[2]}`:(s=e.match(/^(projects\/[^/]+\/)graph\/(.+)$/),s?`${s[1]}code/graph/${s[2]}`:e))}var Te=class{constructor(e,t,r,s){this.provider=e;this.name=e.name,this.projectId=t,this.projectName=r,this.folder=At(s??r),this.sourcePrefix=`projects/${t}`,this.targetPrefix=`projects/${this.folder}`}provider;name;folder;sourcePrefix;targetPrefix;projectId;projectName;registryPromise;async registerProject(){let e=`${this.targetPrefix}/project.json`,t=new Date().toISOString(),r=t,s=await this.provider.getText(e);if(s){let o;try{o=JSON.parse(s)}catch(a){throw new Error(`Invalid remote ToolNet project manifest at ${e}: ${a instanceof Error?a.message:String(a)}`)}if(typeof o.id=="string"&&o.id!==this.projectId)throw new Error(["ToolNet remote project namespace collision.",`Remote folder: ${this.targetPrefix}`,`Existing project id: ${o.id}`,`Current project id: ${this.projectId}`,"Refusing to mix data from two projects."].join(" "));typeof o.createdAt=="string"&&(r=o.createdAt)}let i={version:1,id:this.projectId,name:this.projectName,remote:this.folder,createdAt:r,updatedAt:t};await this.provider.put(e,JSON.stringify(i,null,2)+`
|
|
6
|
-
`,"application/json")}async ensureRegistered(){return this.registryPromise||(this.registryPromise=this.registerProject()),this.registryPromise}key(e){if(e=Nt(e),e===this.sourcePrefix)return this.targetPrefix;if(e.startsWith(`${this.sourcePrefix}/`))return this.targetPrefix+e.slice(this.sourcePrefix.length);if(e===this.targetPrefix||e.startsWith(`${this.targetPrefix}/`))return e;if(e.startsWith("projects/"))throw new Error(["Cross-project storage access denied.",`Current project: ${this.targetPrefix}`,`Requested key: ${e}`].join(" "));return e}async put(e,t,r){return await this.ensureRegistered(),this.provider.put(this.key(e),t,r)}async get(e){return await this.ensureRegistered(),this.provider.get(this.key(e))}async getText(e){return await this.ensureRegistered(),this.provider.getText(this.key(e))}async delete(e){return await this.ensureRegistered(),this.provider.delete(this.key(e))}async exists(e){return await this.ensureRegistered(),this.provider.exists(this.key(e))}async list(e){return await this.ensureRegistered(),this.provider.list(this.key(e))}};var Me=class{constructor(e){this.storage=e}storage;key(e){return["projects",e,"graph","resolution","current.json"].join("/")}async load(e){let t=await this.storage.getText(this.key(e));return t?JSON.parse(t):null}async save(e){await this.storage.put(this.key(e.projectId),JSON.stringify(e),"application/json")}};var Ie=class{constructor(e){this.storage=e}storage;base(e){return["projects",e,"code","architecture"].join("/")}async load(e){let t=await this.storage.getText(`${this.base(e)}/current.json`);return t?JSON.parse(t):null}async save(e){let t=this.base(e.projectId),r=s=>JSON.stringify({version:e.version,projectId:e.projectId,updatedAt:e.updatedAt,data:s},null,2);await Promise.all([this.storage.put(`${t}/current.json`,JSON.stringify(e,null,2),"application/json"),this.storage.put(`${t}/entry-points.json`,r(e.entryPoints),"application/json"),this.storage.put(`${t}/hotspots.json`,r(e.hotspots),"application/json"),this.storage.put(`${t}/layers.json`,r(e.layers),"application/json"),this.storage.put(`${t}/clusters.json`,r(e.clusters),"application/json")])}};var Ae=class{constructor(e){this.storage=e}storage;base(e){return["projects",e,"code","analysis"].join("/")}async load(e){let t=await this.storage.getText(`${this.base(e)}/current.json`);return t?JSON.parse(t):null}async save(e){let t=this.base(e.projectId),r=s=>JSON.stringify({version:e.version,projectId:e.projectId,updatedAt:e.updatedAt,data:s},null,2);await Promise.all([this.storage.put(`${t}/current.json`,JSON.stringify(e,null,2),"application/json"),this.storage.put(`${t}/dead-code.json`,r(e.deadCode),"application/json"),this.storage.put(`${t}/dependencies.json`,r(e.dependencies),"application/json")])}};var Ne=class{constructor(e){this.storage=e}storage;key(e){return["projects",e,"code","visualization","graph.json"].join("/")}async load(e){let t=await this.storage.getText(this.key(e));return t?JSON.parse(t):null}async save(e){await this.storage.put(this.key(e.projectId),JSON.stringify(e,null,2),"application/json")}};async function Lt(n,e={}){let t=Date.now();async function r(h,y,p){await e.onStage?.({id:h,title:y,state:"start"});let S=Date.now(),x=await p();return await e.onStage?.({id:h,title:y,state:"complete",durationMs:Date.now()-S}),x}let s=tt(),i=It(Tt({provider:s.storage.provider,huggingface:s.storage.huggingface,localRoot:s.storage.localRoot}),{attempts:3}),o=new Te(i,n.id,n.name,n.remote??n.name),a=new V(o),u=await r("source-index","Source Index",async()=>{let h=await new de().index(n.id,n.rootPath,{onProgress:y=>{e.onSourceProgress?.(y)}});return await a.save({version:1,projectId:n.id,updatedAt:new Date().toISOString(),files:h.files,symbols:h.graph.allSymbols(n.id),edges:h.graph.allEdges(n.id)}),h}),c=u.graph,l=await r("type-resolution","Type Resolution",async()=>{let h=await new he(c).resolveProject(n.id,n.rootPath,y=>{e.onStageProgress?.({stage:"type-resolution",current:y.current,total:y.total,phase:y.phase,detail:y.detail})});return await new Me(o).save(h),h});await r("rich-graph","Rich Graph",async()=>{let h=new ye(c).enrich(n.id,n.rootPath,l,S=>{e.onStageProgress?.({stage:"rich-graph",current:S.current,total:S.total,phase:S.phase,detail:S.detail})}),y=c.allSymbols(n.id),p=c.allEdges(n.id);return await a.save({version:1,projectId:n.id,updatedAt:new Date().toISOString(),files:y.filter(S=>S.type==="file").length,symbols:y,edges:p}),h});let f=await r("semantic-index","Local Code Search \u2014 SQLite FTS5/BM25",async()=>new ge({projectId:n.id,rootPath:n.rootPath,storage:o,graph:c}).initialize(y=>{e.onStageProgress?.({stage:"semantic-index",current:y.current,total:y.total,phase:y.phase,detail:y.detail})})),d=await r("architecture","Architecture Intelligence",async()=>{let h=new ve(c).analyze(n.id);return await new Ie(o).save(h),h}),m=await r("analysis","Graph Analysis",async()=>{let h=new Pe(c).analyze(n.id);return await new Ae(o).save(h),h}),g=await r("visualization","3D Visualization Dataset",async()=>{let h=new Re(c).build(n.id,d,m,y=>{e.onStageProgress?.({stage:"visualization",current:y.current,total:y.total,phase:y.phase,detail:y.detail})});return await new Ne(o).save(h),h});return{files:u.files,project:{id:n.id,name:n.name,remote:n.remote??n.name},storage:o.name,durationMs:Date.now()-t,graph:{symbols:c.allSymbols(n.id).length,edges:c.allEdges(n.id).length},resolution:{total:l.total,exact:l.exact,high:l.high,fallback:l.fallback},semantic:f,architecture:d.summary,analysis:m.summary,visualization:g.summary}}import{Worker as Kn}from"worker_threads";import*as je from"path";import{fileURLToPath as Bn}from"node:url";function Le(){if(process.argv.includes("--no-color"))return!1;if(process.argv.includes("--color"))return!0;let n=process.env.NO_COLOR;if(n!==void 0&&n!=="")return!1;let e=process.env.FORCE_COLOR;if(e!==void 0&&e!=="")return e!=="0"&&e.toLowerCase()!=="false";if(process.stdout.isTTY===!0&&process.env.TERM!=="dumb")return!0;let t=process.env.CI;return t!==void 0&&t!==""}var $n={spinner:["\xB7","\u2722","\u2733","\u2736","\u273B","\u273D"],barFilled:"\u2588",barEmpty:"\u2591",phaseDone:"\u25C6",phaseActive:"\u25C7",rail:"\u2502",branch:"\u251C",corner:"\u2514",dash:"\u2014"},Gn={spinner:[".","*","+","x","*","+"],barFilled:"#",barEmpty:"-",phaseDone:"[OK]",phaseActive:"[>>]",rail:"|",branch:"|",corner:"\\",dash:"-"};function zn(){if(process.env.TOOLNET_ASCII==="1")return!1;if(process.env.TOOLNET_UNICODE==="1")return!0;if(process.platform==="win32"){let n=process.env.TERM_PROGRAM,e=process.env.WT_SESSION,t=process.env.VSCODE_PID,r=process.env.ConEmuPID;return!!(e||t||r||n==="vscode")}return process.env.TERM!=="linux"}var Ye;function G(){return Ye||(Ye=zn()?$n:Gn),Ye}var jt={scan:"Scanning files",parse:"Parsing code","type-resolution":"Type Resolution","rich-graph":"Rich Graph","semantic-index":"Semantic Code Index",architecture:"Architecture Intelligence","graph-analysis":"Graph Analysis",visualization:"3D Visualization Dataset"};function Ot(){if(process.stdout.isTTY!==!0)return Un();let n=Le(),e=G(),t=n?"\x1B[2m":"",r=n?"\x1B[32m":"",s=n?"\x1B[0m":"",i="",o="",a=-1,u=0,c=()=>{if(!o)return;let g="";a>=0?g=` ${e.dash} done`:u>0&&(g=` ${e.dash} ${u.toLocaleString()} found`),process.stdout.write(`\r\x1B[K${t}${e.branch}${s} ${r}${e.phaseDone}${s} ${o}${g}
|
|
7
|
-
|
|
8
|
-
`)},
|
|
1
|
+
import{closeSync as Us,mkdirSync as Hs,openSync as Ws,rmSync as Ys,writeFileSync as Vs}from"node:fs";import{join as mt}from"node:path";import{existsSync as fr,readFileSync as gr}from"node:fs";import{homedir as hr}from"node:os";import{join as yr}from"node:path";function Sr(n){let e=n.trim();return e.length>=2&&e.startsWith('"')&&e.endsWith('"')?(e=e.slice(1,-1),e.replace(/\\n/g,`
|
|
2
|
+
`).replace(/\\r/g,"\r").replace(/\\t/g," ").replace(/\\"/g,'"').replace(/\\\\/g,"\\")):e.length>=2&&e.startsWith("'")&&e.endsWith("'")?e.slice(1,-1):e}function br(){let n=process.env.TOOLNET_GLOBAL_ENV??yr(hr(),".config","toolnet-memory",".env");if(!fr(n))return;let e=gr(n,"utf8");for(let t of e.split(/\r?\n/)){let r=t.trim();if(!r||r.startsWith("#"))continue;r.startsWith("export ")&&(r=r.slice(7));let s=r.indexOf("=");if(s<=0)continue;let i=r.slice(0,s).trim();/^[A-Za-z_][A-Za-z0-9_]*$/.test(i)&&process.env[i]===void 0&&(process.env[i]=Sr(r.slice(s+1)))}}br();function W(n,e){return n===void 0?e:["1","true","yes","on"].includes(n.toLowerCase())}function Y(n,e){if(!n)return e;let t=Number(n);return Number.isFinite(t)?t:e}function gt(){return{memory:{autoCapture:W(process.env.MEMORY_AUTO_CAPTURE,!0),autoRetrieve:W(process.env.MEMORY_AUTO_RETRIEVE,!0),autoSummarize:W(process.env.MEMORY_AUTO_SUMMARIZE,!0),autoSync:W(process.env.MEMORY_AUTO_SYNC,!0)},retrieval:{maxCandidates:Y(process.env.MEMORY_MAX_CANDIDATES,50),rerankTop:Y(process.env.MEMORY_RERANK_TOP,10),finalContext:Y(process.env.MEMORY_FINAL_CONTEXT,5),tokenBudget:Y(process.env.MEMORY_TOKEN_BUDGET,2e3)},storage:{provider:process.env.MEMORY_STORAGE_PROVIDER??"huggingface",r2:{accountId:process.env.R2_ACCOUNT_ID,bucket:process.env.R2_BUCKET,accessKeyId:process.env.R2_ACCESS_KEY_ID,secretAccessKey:process.env.R2_SECRET_ACCESS_KEY},s3:{endpoint:process.env.S3_ENDPOINT,region:process.env.S3_REGION,bucket:process.env.S3_BUCKET,accessKeyId:process.env.S3_ACCESS_KEY_ID,secretAccessKey:process.env.S3_SECRET_ACCESS_KEY,forcePathStyle:W(process.env.S3_FORCE_PATH_STYLE,!1)},huggingface:{namespace:process.env.HF_NAMESPACE,bucket:process.env.HF_BUCKET,accessKeyId:process.env.HF_S3_ACCESS_KEY_ID,secretAccessKey:process.env.HF_S3_SECRET_ACCESS_KEY},localRoot:process.env.MEMORY_LOCAL_STORAGE_PATH},cache:{maxMb:Y(process.env.MEMORY_LOCAL_CACHE_MB,200)}}}import{createHash as Rr}from"node:crypto";import{existsSync as pe,mkdirSync as Tr,readFileSync as kr,renameSync as Ir,writeFileSync as Ar}from"node:fs";import{basename as Mr,dirname as me,join as q,parse as vt,resolve as O}from"node:path";import{createHash as St}from"node:crypto";import{spawnSync as xr}from"node:child_process";var V="git-remote-v1",Er=new Set(["github.com","gitlab.com","bitbucket.org"]);function bt(n,e){let t=e.replaceAll("\\","/").replace(/^\/+/u,"").replace(/\/+$/u,"").replace(/\.git$/iu,"").replace(/\/+/gu,"/");return!t||t==="."||t===".."||t.split("/").some(r=>!r||r==="."||r==="..")?null:(Er.has(n)&&(t=t.toLowerCase()),t)}function Cr(n){let e;try{e=new URL(n)}catch{return null}if(!["https:","http:","ssh:","git:"].includes(e.protocol))return null;let t=e.hostname.trim().toLowerCase();if(!t)return null;let r=e.protocol==="https:"&&e.port==="443"||e.protocol==="http:"&&e.port==="80"||e.protocol==="ssh:"&&e.port==="22",s=e.port&&!r?`${t}:${e.port}`:t,i=bt(t,e.pathname);return i?`${s}/${i}`:null}function vr(n){let e=n.match(/^(?:[^@\s/:]+@)?([^:/\s]+):(.+)$/u);if(!e)return null;let t=e[1]?.trim().toLowerCase();if(!t||t.length===1)return null;let r=bt(t,e[2]??"");return r?`${t}/${r}`:null}function ht(n){let e=n.trim();return e?e.includes("://")?Cr(e):vr(e):null}function wr(n){return St("sha256").update(`${V}:${n}`).digest("hex")}function xt(n){return St("sha256").update(`toolnet-project:${V}:${n}`).digest("hex").slice(0,16)}function Pr(n){return n.split("/").filter(Boolean).at(-1)?.trim()||null}function Ye(n,e){let t=xr("git",["-C",n,...e],{encoding:"utf8",windowsHide:!0,stdio:["ignore","pipe","ignore"]});return t.error||t.status!==0?null:t.stdout?.trim()||null}function yt(n,e){let t=Pr(n);return t?{scheme:V,canonicalRemote:n,fingerprint:wr(n),repositoryName:t,source:e}:null}function Et(n){let e=Ye(n,["remote","get-url","origin"]);if(e){let s=ht(e);if(s)return yt(s,"origin")}let t=Ye(n,["remote"]);if(!t)return null;let r=new Set;for(let s of t.split(/\r?\n/u).map(i=>i.trim()).filter(Boolean)){let i=Ye(n,["remote","get-url",s]);if(!i)continue;let o=ht(i);o&&r.add(o)}return r.size!==1?null:yt([...r][0],"unique-remote")}var wt=".toolnet",Nr="project.json";function Lr(n){return Rr("sha256").update(n).digest("hex").slice(0,16)}function F(n){return q(n,wt,Nr)}function Pt(n){return pe(F(n))}function Ct(n,e){let t=O(n),r=vt(t).root;for(;;){if(Pt(t))return t;if(t===r||e&&t===O(e))break;let s=me(t);if(s===t)break;t=s}return null}function Ve(n){let e=O(n),t=vt(e).root,r=[".git","package.json","pyproject.toml","Cargo.toml","go.mod","composer.json"];for(;;){if(r.some(i=>pe(q(e,i))))return e;if(e===t)break;let s=me(e);if(s===e)break;e=s}return O(n)}function de(n){let e;try{e=JSON.parse(kr(n,"utf8"))}catch(s){throw new Error(`Invalid ToolNet project manifest: ${n}: ${s instanceof Error?s.message:String(s)}`)}if(!e||typeof e!="object")throw new Error(`Invalid ToolNet project manifest: ${n}`);let t=e;if(typeof t.id!="string"||!t.id.trim())throw new Error(`ToolNet project manifest is missing id: ${n}`);if(typeof t.name!="string"||!t.name.trim())throw new Error(`ToolNet project manifest is missing name: ${n}`);let r=new Date().toISOString();return{version:1,id:t.id,name:t.name,remote:typeof t.remote=="string"&&t.remote.trim()?t.remote:t.name,rootPath:typeof t.rootPath=="string"?t.rootPath:me(me(n)),createdAt:typeof t.createdAt=="string"?t.createdAt:r,updatedAt:typeof t.updatedAt=="string"?t.updatedAt:r,graphVersion:typeof t.graphVersion=="number"?t.graphVersion:0,memoryVersion:typeof t.memoryVersion=="number"?t.memoryVersion:0,metadata:t.metadata&&typeof t.metadata=="object"?t.metadata:void 0}}function ue(n,e){let t=q(n,wt);Tr(t,{recursive:!0});let r=F(n),s=`${r}.tmp-${process.pid}`;Ar(s,JSON.stringify(e,null,2)+`
|
|
3
|
+
`,{encoding:"utf8",mode:384}),Ir(s,r)}function L(n,e){return{id:n.id,name:n.name,remote:n.remote,rootPath:e,createdAt:n.createdAt,updatedAt:n.updatedAt,graphVersion:n.graphVersion,memoryVersion:n.memoryVersion,metadata:n.metadata}}function qe(n){return{version:1,scheme:V,canonicalRemote:n.canonicalRemote,fingerprint:n.fingerprint,repositoryName:n.repositoryName}}function Or(n){let e=n.metadata?.toolnetIdentity;if(!e||typeof e!="object"||Array.isArray(e))return null;let t=e;return typeof t.fingerprint=="string"?t.fingerprint:null}var fe=class{adopt(e,t){let r=Ve(O(e));if(!t.id.trim())throw new Error("PROJECT_ADOPTION_INVALID_ID");if(!t.name.trim())throw new Error("PROJECT_ADOPTION_INVALID_NAME");if(!t.remote.trim())throw new Error("PROJECT_ADOPTION_INVALID_REMOTE");if(Pt(r)){let a=de(F(r));if(a.id!==t.id)throw new Error(["PROJECT_IDENTITY_ALREADY_EXISTS",`existing=${a.id}`,`requested=${t.id}`].join(" "));return L(a,r)}let s=new Date().toISOString(),i={...t.metadata};t.gitIdentity&&(i.toolnetIdentity=qe(t.gitIdentity));let o={version:1,id:t.id.trim(),name:t.name.trim(),remote:t.remote.trim(),rootPath:r,createdAt:t.createdAt??s,updatedAt:s,graphVersion:t.graphVersion??0,memoryVersion:t.memoryVersion??0,metadata:Object.keys(i).length?i:void 0};return ue(r,o),L(o,r)}recordGitIdentity(e,t,r={}){let s=this.requireExisting(e),i=F(s.rootPath),o=de(i),a=Or(o);if(a&&a!==t.fingerprint&&!r.allowRebind)throw new Error(["PROJECT_GIT_REMOTE_CHANGED",`existing=${a}`,`current=${t.fingerprint}`,"Use explicit rebind only when this repository identity change is intentional."].join(" "));let d=o.metadata?.toolnetIdentity;return d&&typeof d=="object"&&!Array.isArray(d)&&d.fingerprint===t.fingerprint||(o.metadata={...o.metadata,toolnetIdentity:qe(t)},o.updatedAt=new Date().toISOString(),ue(s.rootPath,o)),L(o,s.rootPath)}findExisting(e=process.cwd()){let t=O(e),r=Ve(t),i=[".git","package.json","pyproject.toml","Cargo.toml","go.mod","composer.json"].some(d=>pe(q(r,d))),o=Ct(t,i?r:void 0);if(!o)return null;let a=de(F(o));return L(a,o)}requireExisting(e=process.cwd()){let t=this.findExisting(e);if(!t)throw new Error("PROJECT_NOT_INITIALIZED");return t}detect(e=process.cwd()){let t=O(e),r=Ve(t),i=[".git","package.json","pyproject.toml","Cargo.toml","go.mod","composer.json"].some(m=>pe(q(r,m))),o=Ct(t,i?r:void 0);if(o){let m=F(o),u=de(m);return u.rootPath!==o&&(u.rootPath=o,u.updatedAt=new Date().toISOString(),ue(o,u)),L(u,o)}let a=new Date().toISOString(),d=Mr(r),c=Et(r),l={version:1,id:c?xt(c.canonicalRemote):Lr(r),name:d,remote:c?.repositoryName??d,rootPath:r,createdAt:a,updatedAt:a,graphVersion:0,memoryVersion:0,metadata:c?{toolnetIdentity:qe(c)}:void 0};return ue(r,l),L(l,r)}};var _r=[{type:"openai_key",regex:/\bsk-[A-Za-z0-9_-]{20,}\b/g,confidence:"exact"},{type:"huggingface_token",regex:/\bhf_[A-Za-z0-9]{20,}\b/g,confidence:"exact"},{type:"hf_s3_access_key",regex:/\bHFAK[A-Za-z0-9]{8,}\b/g,confidence:"exact"},{type:"aws_access_key",regex:/\b(?:AKIA|ASIA)[A-Z0-9]{16}\b/g,confidence:"exact"},{type:"github_token",regex:/\b(?:gh[pousr]_[A-Za-z0-9]{30,255}|github_pat_[A-Za-z0-9_]{40,255})\b/g,confidence:"exact"},{type:"stripe_secret_key",regex:/\b(?:sk|rk)_(?:live|test)_[A-Za-z0-9]{16,}\b/g,confidence:"exact"},{type:"google_api_key",regex:/\bAIza[A-Za-z0-9_-]{30,}\b/g,confidence:"exact"},{type:"slack_token",regex:/\bxox[baprs]-[A-Za-z0-9-]{16,}\b/g,confidence:"exact"},{type:"npm_token",regex:/\bnpm_[A-Za-z0-9]{20,}\b/g,confidence:"exact"},{type:"bearer_token",regex:/\bBearer\s+[A-Za-z0-9._~+/=-]{16,}\b/gi,confidence:"high"},{type:"jwt",regex:/\beyJ[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,}\b/g,confidence:"exact"},{type:"private_key",regex:/-----BEGIN (?:RSA |EC |DSA |OPENSSH )?PRIVATE KEY-----[\s\S]*?-----END (?:RSA |EC |DSA |OPENSSH )?PRIVATE KEY-----/g,confidence:"exact"},{type:"password_assignment",regex:/\b(?:password|passwd|pwd)\s*[:=]\s*["']?[^"' \t\r\n]{6,}["']?/gi,confidence:"high"},{type:"secret_assignment",regex:/\b(?:secret|api[_-]?key|access[_-]?token|refresh[_-]?token|client[_-]?secret|private[_-]?key)\s*[:=]\s*["']?[^"' \t\r\n]{8,}["']?/gi,confidence:"high"},{type:"cookie",regex:/\b(?:cookie|set-cookie)\s*[:=]\s*[^;\n]{8,}/gi,confidence:"high"},{type:"url_credentials",regex:/\bhttps?:\/\/[^:/@\s]+:[^/@\s]{4,}@[^/\s]+/gi,confidence:"high"}],jr=new Set(["example","example-key","example-token","changeme","change-me","password","secret","your-api-key","your-token","<token>","<secret>","<password>","[redacted]"]);function Rt(n){return n.normalize("NFKC").trim().toLowerCase()}function Dr(n){if(n.length===0)return 0;let e=new Map;for(let r of n)e.set(r,(e.get(r)??0)+1);let t=0;for(let r of e.values()){let s=r/n.length;t-=s*Math.log2(s)}return t}function Fr(n){return/^[a-f0-9]{32}$/iu.test(n)||/^[a-f0-9]{40}$/iu.test(n)||/^[a-f0-9]{64}$/iu.test(n)}function $r(n,e,t){let r=n.slice(Math.max(0,e-48),e),s=n.slice(t,Math.min(n.length,t+16));return/\b(?:token|secret|key|credential|authorization|password|passwd|apikey|api_key|access[_-]?key)\b/iu.test(`${r} ${s}`)}function Gr(n,e){return n.start<e.end&&e.start<n.end}function Tt(n){return n.sort((e,t)=>e.start!==t.start?e.start-t.start:t.end-t.start-(e.end-e.start))}var ge=class{allowValues=new Set;enableEntropyHeuristic;constructor(e={}){for(let t of e.allowValues??[]){let r=Rt(t);r&&this.allowValues.add(r)}this.enableEntropyHeuristic=e.enableEntropyHeuristic??!0}scan(e){let t=[];for(let i of _r){let o=new RegExp(i.regex.source,i.regex.flags);for(let a of e.matchAll(o))a.index===void 0||!a[0]||this.allowed(a[0])||t.push({type:i.type,value:a[0],start:a.index,end:a.index+a[0].length,confidence:i.confidence})}this.enableEntropyHeuristic&&t.push(...this.entropyMatches(e));let r=Tt(t),s=[];for(let i of r)s.some(o=>Gr(o,i))||s.push(i);return Tt(s)}hasSecrets(e){return this.scan(e).length>0}allowed(e){let t=Rt(e);return jr.has(t)?!0:this.allowValues.has(t)}entropyMatches(e){let t=[],r=/[A-Za-z0-9_+/=-]{32,160}/g;for(let s of e.matchAll(r)){if(s.index===void 0||!s[0])continue;let i=s[0];this.allowed(i)||Fr(i)||!/[A-Za-z]/u.test(i)||!/[0-9]/u.test(i)||$r(e,s.index,s.index+i.length)&&(Dr(i)<3.7||t.push({type:"high_entropy_secret",value:i,start:s.index,end:s.index+i.length,confidence:"heuristic"}))}return t}};var $=class{scanner;constructor(e={}){this.scanner=new ge(e)}sanitize(e){let t=this.scanner.scan(e);if(t.length===0)return{text:e,redacted:0,secretTypes:[]};let r=e,s=[...t].sort((o,a)=>a.start-o.start),i=new Set;for(let o of s)i.add(o.type),r=r.slice(0,o.start)+`[REDACTED:${o.type}]`+r.slice(o.end);return{text:r,redacted:t.length,secretTypes:[...i].sort()}}sanitizeValue(e){if(typeof e=="string")return this.sanitize(e).text;if(Array.isArray(e))return e.map(t=>this.sanitizeValue(t));if(e&&typeof e=="object"){let t={};for(let[r,s]of Object.entries(e)){let i=r.normalize("NFKC").toLowerCase().replace(/[^a-z0-9]+/g,"");if(i.includes("password")||i.includes("passwd")||i==="pwd"||i.includes("secret")||i.includes("token")||i.includes("cookie")||i.includes("authorization")||i.includes("apikey")||i.includes("accesskey")||i.includes("privatekey")||i.includes("clientsecret")||i.includes("credential")){t[r]="[REDACTED]";continue}t[r]=this.sanitizeValue(s)}return t}return e}};var yo=new $;import{lstat as Ur,readdir as Hr}from"node:fs/promises";import{extname as Wr,join as Yr,relative as Vr,resolve as qr}from"node:path";import{basename as Kr}from"node:path";var zr=new Set([".env",".env.local",".env.production",".env.development","id_rsa","id_ed25519","credentials","credentials.json","service-account.json"]),Br=new Set([".pem",".key",".p12",".pfx"]);function kt(n){let e=Kr(n).toLowerCase();if(zr.has(e))return!0;for(let t of Br)if(e.endsWith(t))return!0;return!1}var Jr=2*1024*1024,Qr=new Set([".ts",".tsx",".js",".jsx",".mjs",".cjs",".mts",".cts"]),Zr=new Set(["node_modules",".git",".toolnet",".toolnet-memory","dist","build","out","coverage",".next",".nuxt",".cache",".parcel-cache",".turbo","vendor","target"]);function It(n,e){if(!Number.isSafeInteger(n)||n<1)throw new Error(`${e} must be a positive integer`);return n}function At(n){if(n?.aborted)throw new Error("Repository scan aborted")}function Xr(n){let e=n.trim().toLowerCase();if(!e)throw new Error("Repository scan extension cannot be empty");return e.startsWith(".")?e:`.${e}`}function en(n){let e=n.toLowerCase();return/\.min\.(?:js|mjs|cjs)$/u.test(e)||/\.bundle\.(?:js|mjs|cjs)$/u.test(e)}function tn(n){return n.replaceAll("\\","/")}async function Mt(n,e={}){let t=qr(n),r=e.maxFileBytes??Jr;if(!Number.isSafeInteger(r)||r<1)throw new Error("maxFileBytes must be a positive integer");let s=e.maxFiles??Number.MAX_SAFE_INTEGER;It(s,"maxFiles");let i=e.progressInterval??1e3;It(i,"progressInterval");let o=new Set(e.extensions?[...e.extensions].map(Xr):Qr),a=new Set(Zr);for(let m of e.extraIgnoredDirectories??[]){let u=m.trim();u&&a.add(u)}let d=[],c={directories:0,entries:0,accepted:0,skippedIgnoredDirectories:0,skippedSymlinks:0,skippedGenerated:0,skippedSensitive:0,skippedOversized:0,skippedUnreadable:0},l=[t];for(;l.length>0;){At(e.signal);let m=l.pop(),u;try{u=await Hr(m,{withFileTypes:!0})}catch{c.skippedUnreadable+=1;continue}c.directories+=1;for(let f of u){At(e.signal),c.entries+=1,c.entries%i===0&&e.onProgress?.({directories:c.directories,entries:c.entries,accepted:c.accepted});let g=Yr(m,f.name);if(f.isSymbolicLink()){c.skippedSymlinks+=1;continue}if(f.isDirectory()){if(a.has(f.name)){c.skippedIgnoredDirectories+=1;continue}l.push(g);continue}if(!f.isFile())continue;let h=Wr(f.name).toLowerCase();if(!o.has(h))continue;if(en(f.name)){c.skippedGenerated+=1;continue}if(kt(g)){c.skippedSensitive+=1;continue}let y;try{y=await Ur(g)}catch{c.skippedUnreadable+=1;continue}if(y.isSymbolicLink()){c.skippedSymlinks+=1;continue}if(y.isFile()){if(y.size>r){c.skippedOversized+=1;continue}if(d.length>=s)throw new Error(`Repository scan file limit exceeded: ${s}`);d.push(tn(Vr(t,g))),c.accepted+=1}}}return d.sort(),e.onProgress?.({directories:c.directories,entries:c.entries,accepted:c.accepted}),{files:d,stats:c}}import{createHash as rn}from"node:crypto";import{dirname as nn,extname as sn,normalize as on,posix as an}from"node:path";var he=class{symbols=new Map;edges=new Map;addSymbol(e){this.symbols.set(e.id,e)}addEdge(e){this.edges.set(e.id,e)}getSymbol(e){return this.symbols.get(e)}findByName(e,t){return[...this.symbols.values()].filter(r=>r.projectId===e&&(r.name===t||r.qualifiedName===t))}allSymbols(e){return[...this.symbols.values()].filter(t=>t.projectId===e)}allEdges(e){return[...this.edges.values()].filter(t=>t.projectId===e)}import(e,t){for(let r of e)this.addSymbol(r);for(let r of t)this.addEdge(r)}clearProject(e){for(let[t,r]of this.symbols)r.projectId===e&&this.symbols.delete(t);for(let[t,r]of this.edges)r.projectId===e&&this.edges.delete(t)}};function cn(n,e,t,r){return rn("sha256").update(`${n}:${e}:${t}:${r}`).digest("hex").slice(0,24)}function k(n){return on(n).replaceAll("\\","/")}function ln(n,e,t){if(!e.startsWith("."))return;let r=k(an.join(nn(n),e)),s=sn(r),i;if(s===".js"){let o=r.slice(0,-3);i=[`${o}.ts`,`${o}.tsx`,`${o}.js`,`${o}.jsx`]}else if(s===".mjs"){let o=r.slice(0,-4);i=[`${o}.mts`,`${o}.ts`,`${o}.mjs`]}else if(s===".cjs"){let o=r.slice(0,-4);i=[`${o}.cts`,`${o}.ts`,`${o}.cjs`]}else s?i=[r]:i=[`${r}.ts`,`${r}.tsx`,`${r}.mts`,`${r}.cts`,`${r}.js`,`${r}.jsx`,`${r}.mjs`,`${r}.cjs`,`${r}/index.ts`,`${r}/index.tsx`,`${r}/index.mts`,`${r}/index.cts`,`${r}/index.js`,`${r}/index.jsx`,`${r}/index.mjs`,`${r}/index.cjs`];return i.find(o=>t.has(k(o)))}function Je(n,e,t,r){let s=r?.(n,e,t);if(s){let i=k(s);if(t.has(i))return i}return ln(n,e,t)}var J=class{build(e,t,r={}){let s=new he;for(let a of r.seedSymbols??[])s.addSymbol(a);for(let a of t)for(let d of a.symbols)s.addSymbol(d);let i=new Map;for(let a of s.allSymbols(e))a.type==="file"&&i.set(k(a.filePath),a);let o=new Set(i.keys());for(let a of t){let d=k(a.filePath),c=i.get(d);if(c){for(let l of a.symbols)l.id!==c.id&&s.addEdge(this.edge(e,c.id,"DEFINES",l.id));for(let l of a.imports){let m=Je(d,l.source,o,r.resolveModule);if(!m)continue;let u=i.get(m);u&&s.addEdge(this.edge(e,c.id,"IMPORTS",u.id,{source:l.source,resolvedFile:m}))}for(let l of a.heritage){let m=s.findByName(e,l.targetName).filter(u=>u.type==="class"||u.type==="interface");for(let u of m)s.addEdge(this.edge(e,l.fromId,l.type,u.id))}for(let l of a.calls){if(!l.callerId)continue;let m=this.resolveCallTargets(e,a,l.calleeName,l.qualifier,s,o,r.resolveModule);for(let u of m)u.id!==l.callerId&&s.addEdge(this.edge(e,l.callerId,"CALLS",u.id,{line:l.line,qualifier:l.qualifier}))}}}return s}resolveCallTargets(e,t,r,s,i,o,a){let d=k(t.filePath),c=i.findByName(e,r).filter(l=>k(l.filePath)===d);if(!s&&c.length)return c;if(s)for(let l of t.imports){if(!l.bindings.find(f=>f.kind==="namespace"&&f.localName===s))continue;let u=Je(d,l.source,o,a);if(u)return i.findByName(e,r).filter(f=>k(f.filePath)===u)}for(let l of t.imports){let m=l.bindings.find(h=>h.localName===r);if(!m)continue;let u=Je(d,l.source,o,a);if(!u)continue;let f=m.importedName==="default"?r:m.importedName,g=i.findByName(e,f).filter(h=>k(h.filePath)===u);if(g.length)return g}return i.findByName(e,r).filter(l=>l.type==="function"||l.type==="method")}edge(e,t,r,s,i){return{id:cn(e,t,r,s),projectId:e,from:t,to:s,type:r,metadata:i}}};import{createHash as fn}from"node:crypto";import{readFile as gn}from"node:fs/promises";import{join as hn}from"node:path";import{extname as dn}from"node:path";var A={supported:!0,structural:!0,lexicalSearch:!0,engine:"typescript-compiler-api",lexicalEngine:"file-chunk-fts5-bm25"},Q={supported:!1,structural:!1,lexicalSearch:!0,engine:"unsupported",lexicalEngine:"file-chunk-fts5-bm25"},G=[{language:"typescript",extensions:[".ts"],...A},{language:"tsx",extensions:[".tsx"],...A},{language:"javascript",extensions:[".js"],...A},{language:"jsx",extensions:[".jsx"],...A},{language:"mts",extensions:[".mts"],...A},{language:"cts",extensions:[".cts"],...A},{language:"mjs",extensions:[".mjs"],...A},{language:"cjs",extensions:[".cjs"],...A},{language:"python",extensions:[".py"],...Q,lspServer:"pyright-langserver"},{language:"go",extensions:[".go"],...Q,lspServer:"gopls"},{language:"rust",extensions:[".rs"],...Q,lspServer:"rust-analyzer"},{language:"c",extensions:[".c",".h"],...Q,lspServer:"clangd"},{language:"cpp",extensions:[".cc",".cpp",".cxx",".hpp",".hh"],...Q,lspServer:"clangd"}];function Qe(n){let e=dn(n).toLowerCase();return G.find(t=>t.extensions.includes(e))}function Nt(){return[...new Set(G.filter(n=>n.lexicalSearch).flatMap(n=>n.extensions))]}import{readFile as un}from"node:fs/promises";import{join as pn}from"node:path";import{createHash as mn}from"node:crypto";import*as E from"typescript";function Lt(n,e){return mn("sha256").update(`${n}:${e}`).digest("hex").slice(0,24)}function Ze(n,e){return n.getLineAndCharacterOfPosition(e.getStart(n)).line+1}async function Xe(n,e,t){let r=pn(e,t),s=await un(r,"utf8"),i=E.createSourceFile(t,s,E.ScriptTarget.Latest,!0,t.endsWith(".tsx")||t.endsWith(".jsx")?E.ScriptKind.TSX:E.ScriptKind.TS),o=[],a=[],d=[],c=[],l={id:Lt(n,`file:${t}`),projectId:n,name:t,qualifiedName:t,type:"file",filePath:t,startLine:1,endLine:i.getLineAndCharacterOfPosition(i.end).line+1};o.push(l);let m=[],u=[];function f(p,S,x,C){let w=Lt(n,`${t}:${x}:${C??S}:${p.pos}`);return o.push({id:w,projectId:n,name:S,qualifiedName:C??S,type:x,filePath:t,startLine:Ze(i,p),endLine:i.getLineAndCharacterOfPosition(p.end).line+1}),w}function g(p){if(!E.isStringLiteral(p.moduleSpecifier))return;let S=[],x=p.importClause;if(x?.name&&S.push({localName:x.name.text,importedName:"default",kind:"default"}),x?.namedBindings&&E.isNamedImports(x.namedBindings))for(let C of x.namedBindings.elements)S.push({localName:C.name.text,importedName:C.propertyName?.text??C.name.text,kind:"named"});x?.namedBindings&&E.isNamespaceImport(x.namedBindings)&&S.push({localName:x.namedBindings.name.text,importedName:"*",kind:"namespace"}),a.push({source:p.moduleSpecifier.text,bindings:S})}function h(p,S){for(let x of p.heritageClauses??[])for(let C of x.types){let w=C.expression.getText(i).split(".").at(-1)??"";w&&c.push({fromId:S,targetName:w,type:x.token===E.SyntaxKind.ExtendsKeyword?"INHERITS":"IMPLEMENTS"})}}function y(p){let S=!1,x=!1;if(E.isImportDeclaration(p)&&g(p),E.isInterfaceDeclaration(p)&&f(p,p.name.text,"interface",p.name.text),E.isClassDeclaration(p)&&p.name){let C=f(p,p.name.text,"class",p.name.text);h(p,C),u.push({id:C,name:p.name.text}),x=!0}if(E.isFunctionDeclaration(p)&&p.name){let C=f(p,p.name.text,"function",p.name.text);m.push(C),S=!0}if(E.isMethodDeclaration(p)&&p.name){let C=p.name.getText(i),w=u.at(-1)?.name,I=f(p,C,"method",w?`${w}.${C}`:C);m.push(I),S=!0}if(E.isCallExpression(p)&&(E.isIdentifier(p.expression)&&d.push({callerId:m.at(-1),calleeName:p.expression.text,line:Ze(i,p)}),E.isPropertyAccessExpression(p.expression))){let C=p.expression;d.push({callerId:m.at(-1),qualifier:C.expression.getText(i),calleeName:C.name.text,line:Ze(i,p)})}E.forEachChild(p,y),S&&m.pop(),x&&u.pop()}return y(i),{filePath:t,symbols:o,imports:a,calls:d,heritage:c}}function yn(n,e){return fn("sha256").update(`${n}:${e}`).digest("hex").slice(0,24)}async function Sn(n,e,t){let r=Qe(t);if(!r||!r.lexicalSearch)throw new Error(`Unsupported code file: ${t}`);let s=await gn(hn(e,t),"utf8"),i=Math.max(1,s.split(/\r?\n/u).length),o={id:yn(n,`file:${t}`),projectId:n,name:t,qualifiedName:t,type:"file",filePath:t,startLine:1,endLine:i,metadata:{language:r.language,structuralParser:!1,lexicalSearch:!0,lexicalEngine:r.lexicalEngine,...r.lspServer?{lspServer:r.lspServer}:{}}};return{filePath:t,symbols:[o],imports:[],calls:[],heritage:[]}}async function Ot(n,e,t){let r=Qe(t);if(!r)throw new Error(`No parser capability for: ${t}`);if(r.supported)return Xe(n,e,t);if(r.lexicalSearch)return Sn(n,e,t);throw new Error(`Structural and lexical parsing unsupported for: ${t}`)}function bn(n){let e=n??4;if(!Number.isSafeInteger(e)||e<1||e>64)throw new Error("concurrency must be an integer between 1 and 64");return e}function xn(n){if(n?.aborted)throw new Error("Operation aborted")}async function ye(n,e,t={}){if(n.length===0)return[];let r=Math.min(bn(t.concurrency),n.length),s=new Array(n.length),i=0,o=0,a=async()=>{for(;;){xn(t.signal);let c=i;if(i+=1,c>=n.length)return;s[c]=await e(n[c],c),o+=1,t.onProgress?.({completed:o,total:n.length,index:c})}},d=Array.from({length:r},()=>a());return await Promise.all(d),s}var Se=class{async index(e,t,r={}){let s={...r.scan,extensions:r.scan?.extensions??Nt()};r.signal&&(s.signal=r.signal);let i=await Mt(t,s),o=i.files;r.onProgress?.({phase:"scan",current:o.length,total:o.length});let a=r.maxWarnings??20;if(!Number.isSafeInteger(a)||a<0)throw new Error("maxWarnings must be a non-negative integer");let d=0,c=await ye(o,async u=>{try{return await Ot(e,t,u)}catch(f){return d+=1,d<=a&&console.warn(`[indexer] skipped ${u}:`,f instanceof Error?f.message:f),null}},{concurrency:r.concurrency??4,signal:r.signal,onProgress:({completed:u,total:f,index:g})=>{r.onProgress?.({phase:"parse",current:u,total:f,file:o[g]})}});d>a&&console.warn(`[indexer] ${d-a} additional parse warnings suppressed`);let l=c.filter(u=>u!==null),m=new J().build(e,l);return{files:l.length,symbols:m.allSymbols(e).length,edges:m.allEdges(e).length,parseFailures:d,scan:i.stats,graph:m}}};import{execFile as En}from"node:child_process";import{promisify as Cn}from"node:util";var ji=Cn(En);var Z=class{constructor(e){this.storage=e}storage;key(e){return["projects",e,"graph","current.json"].join("/")}async load(e){let t=await this.storage.getText(this.key(e));return t?JSON.parse(t):null}async save(e){await this.storage.put(this.key(e.projectId),JSON.stringify(e),"application/json")}};import*as M from"typescript";import{createHash as wn}from"node:crypto";import{readFile as Pn}from"node:fs/promises";import{join as Rn}from"node:path";function jt(n){return wn("sha256").update(n).digest("hex")}var be=class{sanitizer=new $;async build(e,t,r,s={}){let i=s.maxLines??80,o=s.overlapLines??10,a=new Map;for(let c of r){let l=a.get(c.filePath)??[];l.push(c),a.set(c.filePath,l)}let d=[];for(let[c,l]of a){let m;try{m=await Pn(Rn(t,c),"utf8")}catch{continue}let u=m.split(/\r?\n/),f=l.filter(g=>g.type!=="file"&&typeof g.startLine=="number"&&typeof g.endLine=="number");if(f.length>0)for(let g of f)d.push(...this.chunkRange({projectId:e,filePath:c,lines:u,symbol:g,startLine:g.startLine,endLine:g.endLine,maxLines:i,overlapLines:o}));else d.push(...this.chunkRange({projectId:e,filePath:c,lines:u,startLine:1,endLine:Math.max(1,u.length),maxLines:i,overlapLines:o}))}return d}chunkRange(e){let{projectId:t,filePath:r,lines:s,symbol:i,maxLines:o}=e,a=Math.min(e.overlapLines,Math.max(0,o-1)),d=Math.max(1,e.startLine),c=Math.min(s.length,Math.max(d,e.endLine)),l=[],m=d,u=0;for(;m<=c;){let f=Math.min(c,m+o-1),g=s.slice(m-1,f).join(`
|
|
4
|
+
`).trim();if(g){let h=this.sanitizer.sanitize(g).text,y=jt(h),p=[t,r,i?.id??"file",String(u),y].join(":");l.push({id:jt(p).slice(0,32),projectId:t,filePath:r,symbolId:i?.id,symbolName:i?.qualifiedName??i?.name,symbolType:i?.type,startLine:m,endLine:f,content:h,contentHash:y})}if(f>=c)break;m=f-a+1,u++}return l}};var xe=class{constructor(e){this.storage=e}storage;key(e){return["projects",e,"code","chunks","current.json"].join("/")}async load(e){let t=await this.storage.getText(this.key(e));return t?JSON.parse(t):null}async save(e){await this.storage.put(this.key(e.projectId),JSON.stringify(e),"application/json")}};import{DatabaseSync as Tn}from"node:sqlite";var Ee=class{constructor(e){this.projectId=e;this.db.exec("CREATE VIRTUAL TABLE code_fts USING fts5(id UNINDEXED, projectId UNINDEXED, filePath, symbol, content, tokenize='unicode61')")}projectId;db=new Tn(":memory:");chunks=new Map;get size(){return this.chunks.size}build(e){if(this.db.exec("DELETE FROM code_fts"),this.chunks.clear(),e.length===0)return 0;let t=this.db.prepare("INSERT INTO code_fts (id, projectId, filePath, symbol, content) VALUES (?, ?, ?, ?, ?)");this.db.exec("BEGIN");try{for(let r of e)this.chunks.set(r.id,r),t.run(r.id,this.projectId,r.filePath,r.symbolName??"",r.content);this.db.exec("COMMIT")}catch(r){throw this.db.exec("ROLLBACK"),r}return e.length}search(e,t=8){let r=this.toMatchQuery(e);if(!r)return[];let s=this.db.prepare("SELECT id, bm25(code_fts) AS rank FROM code_fts WHERE code_fts MATCH ? ORDER BY rank LIMIT ?").all(r,Math.max(t*4,20)),i=[];for(let o of s){let a=this.chunks.get(o.id);if(!a)continue;let d=1/(1+Math.abs(o.rank));i.push({chunk:a,score:d})}return i.slice(0,t)}toMatchQuery(e){let t=e.toLowerCase().normalize("NFKC").split(/[^\p{L}\p{N}]+/u).map(r=>r.trim()).filter(r=>r.length>0);return t.length===0?null:t.length===1?`"${t[0]}"`:`(${t.map(r=>`"${r}"`).join(" OR ")})`}close(){this.db.close()}};function Dt(n,e){let t=n.toLowerCase().replaceAll("\\","/"),r=e.toLowerCase(),s=/\b(test|tests|spec|fixture|mock)\b/.test(r);return(t.startsWith("tests/")||t.includes("/tests/")||t.includes("__tests__")||/\.(test|spec)\.[^.]+$/.test(t))&&!s?.35:t.includes("/generated/")||t.includes(".generated.")?.25:1}var X="sqlite-fts5-bm25",Ce="lexical",kn="Local Code Search \u2014 SQLite FTS5/BM25",Ft={engine:X,mode:Ce,displayName:kn,semantic:!1,embedding:!1,vectorDatabase:!1,queryExpansion:!1,synonymExpansion:!1,networkRequired:!1,indexBackend:"sqlite-fts5",ranking:"bm25",compatibilityAliases:["SemanticCodeEngine","semantic_code_search","toolnet-memory semantic","semantic-index"]};var ve=class{constructor(e){this.options=e;this.fts=new Ee(e.projectId)}options;fts;get searchContract(){return Ft}async initialize(e){let{projectId:t,rootPath:r,storage:s,graph:i}=this.options,o=new xe(s),a=await new be().build(t,r,i.allSymbols(t)),d=await o.load(t),c=!d||d.chunks.length!==a.length||d.chunks.some(m=>!a.some(u=>u.id===m.id));c&&await o.save({version:1,projectId:t,updatedAt:new Date().toISOString(),chunks:a}),e?.({current:a.length,total:a.length,phase:"index",detail:X});let l=this.fts.build(a);return{chunks:a.length,documentsIndexed:l,indexSize:this.fts.size,engine:X,mode:Ce,embedding:!1,vectorDatabase:!1,savedChunks:c,vectorsLoaded:0,vectorsIndexed:l,vectorsRemoved:0,totalVectors:this.fts.size,savedVectors:c}}async search(e,t=8){let r=this.fts.search(e,t),s=[];for(let i of r){let o=i.chunk,a=Dt(o.filePath,e),d=i.score,c=0,l=d*a;s.push({chunk:o,score:l,lexicalScore:d,vectorScore:c,engine:X,mode:Ce})}return s}size(){return this.fts.size}};import{createHash as In}from"node:crypto";import{dirname as An,relative as ee,resolve as te}from"node:path";import*as v from"typescript";function _(n){return n.replaceAll("\\","/").replace(/^\.\//,"")}function $t(n){return In("sha256").update(n).digest("hex").slice(0,24)}function re(n,e){return n.getLineAndCharacterOfPosition(e.getStart(n)).line+1}function Mn(n){let e=n;if(e.name&&v.isIdentifier(e.name))return e.name.text}var we=class{constructor(e){this.graph=e}graph;async resolveProject(e,t,r){let s=this.loadConfig(t),i=v.createProgram({rootNames:s.fileNames,options:s.options}),o=i.getTypeChecker(),a=[],d=i.getSourceFiles().filter(l=>{if(l.isDeclarationFile)return!1;let m=te(l.fileName),u=_(ee(t,m));return!(u.startsWith("../")||u.includes("node_modules/"))}),c=0;for(let l of d){let m=te(l.fileName),u=_(ee(t,m)),f=g=>{if(v.isCallExpression(g)){let h=this.resolveCall({projectId:e,rootPath:t,source:l,node:g,checker:o});h&&a.push(h)}v.isClassDeclaration(g)&&a.push(...this.resolveHeritage({projectId:e,rootPath:t,source:l,node:g,checker:o})),v.forEachChild(g,f)};f(l),c++,r?.({current:c,total:d.length,detail:u})}return{version:1,projectId:e,updatedAt:new Date().toISOString(),total:a.length,exact:a.filter(l=>l.confidence==="exact").length,high:a.filter(l=>l.confidence==="high").length,fallback:a.filter(l=>l.confidence==="fallback").length,resolutions:a}}resolveCall(e){let{projectId:t,rootPath:r,source:s,node:i,checker:o}=e,a;if(a=o.getResolvedSignature(i)?.declaration,!a){let c=o.getSymbolAtLocation(i.expression);a=c?.valueDeclaration??c?.declarations?.[0]}return a?this.fromDeclaration({projectId:t,rootPath:r,source:s,sourceNode:i,declaration:a,expression:i.expression.getText(s),kind:"CALL"}):this.fallbackCall(t,r,s,i)}resolveHeritage(e){let{projectId:t,rootPath:r,source:s,node:i,checker:o}=e,a=[];for(let d of i.heritageClauses??[])for(let c of d.types){let l=o.getSymbolAtLocation(c.expression),m=l?.valueDeclaration??l?.declarations?.[0];if(!m)continue;let u=this.fromDeclaration({projectId:t,rootPath:r,source:s,sourceNode:c,declaration:m,expression:c.getText(s),kind:d.token===v.SyntaxKind.ExtendsKeyword?"EXTENDS":"IMPLEMENTS"});u&&a.push(u)}return a}fromDeclaration(e){let t=e.declaration.getSourceFile();if(t.isDeclarationFile)return null;let r=_(ee(e.rootPath,te(t.fileName)));if(r.startsWith("../")||r.includes("node_modules/"))return null;let s=re(t,e.declaration),i=Mn(e.declaration)??e.expression.split(".").at(-1),o=this.findGraphSymbol(e.projectId,r,s,i),a=_(ee(e.rootPath,te(e.source.fileName)));return{id:$t([e.projectId,a,re(e.source,e.sourceNode),e.kind,r,s].join(":")),projectId:e.projectId,kind:e.kind,sourceFile:a,sourceLine:re(e.source,e.sourceNode),expression:e.expression,targetFile:r,targetLine:s,targetName:i,targetQualifiedName:o?.qualifiedName,targetSymbolId:o?.id,confidence:o?"exact":"high",resolver:"typescript-checker"}}fallbackCall(e,t,r,s){let i=s.expression.getText(r).split(".").at(-1);if(!i)return null;let o=this.graph.findByName(e,i).filter(c=>c.type==="function"||c.type==="method");if(o.length!==1)return null;let a=o[0],d=_(ee(t,te(r.fileName)));return{id:$t(`${e}:${d}:${re(r,s)}:${a.id}`),projectId:e,kind:"CALL",sourceFile:d,sourceLine:re(r,s),expression:s.expression.getText(r),targetFile:a.filePath,targetLine:a.startLine,targetName:a.name,targetQualifiedName:a.qualifiedName,targetSymbolId:a.id,confidence:"fallback",resolver:"graph-fallback"}}findGraphSymbol(e,t,r,s){let o=this.graph.allSymbols(e).filter(a=>_(a.filePath)===_(t)).filter(a=>typeof a.startLine=="number"&&typeof a.endLine=="number"&&a.startLine<=r&&a.endLine>=r);if(s){let a=o.find(d=>d.name===s||(d.qualifiedName?.endsWith(`.${s}`)??!1));if(a)return a}return o.sort((a,d)=>(a.endLine??0)-(a.startLine??0)-((d.endLine??0)-(d.startLine??0)))[0]}loadConfig(e){let t=v.findConfigFile(e,v.sys.fileExists,"tsconfig.json");if(t){let s=v.readConfigFile(t,v.sys.readFile);if(s.error)throw new Error(v.flattenDiagnosticMessageText(s.error.messageText,`
|
|
5
|
+
`));return v.parseJsonConfigFileContent(s.config,v.sys,An(t))}return{fileNames:v.sys.readDirectory(e,[".ts",".tsx",".js",".jsx",".mts",".cts",".mjs",".cjs"],["node_modules",".git","dist","build","coverage"]),options:{allowJs:!0,checkJs:!1,target:v.ScriptTarget.ES2022,module:v.ModuleKind.NodeNext,moduleResolution:v.ModuleResolutionKind.NodeNext,skipLibCheck:!0,noEmit:!0}}}};import{createHash as Nn}from"node:crypto";import{dirname as Ln,relative as K,resolve as z}from"node:path";import*as b from"typescript";function T(n){return n.replaceAll("\\","/").replace(/^\.\//,"")}function et(...n){return Nn("sha256").update(n.join(":")).digest("hex").slice(0,24)}function ne(n,e){return n.getLineAndCharacterOfPosition(e.getStart(n)).line+1}var On=new Set(["get","post","put","patch","delete","options","head"]),_n=new Set([b.SyntaxKind.EqualsToken,b.SyntaxKind.PlusEqualsToken,b.SyntaxKind.MinusEqualsToken,b.SyntaxKind.AsteriskEqualsToken,b.SyntaxKind.SlashEqualsToken,b.SyntaxKind.PercentEqualsToken,b.SyntaxKind.QuestionQuestionEqualsToken,b.SyntaxKind.AmpersandAmpersandEqualsToken,b.SyntaxKind.BarBarEqualsToken]),Pe=class{constructor(e){this.graph=e}graph;enrich(e,t,r,s){let i={routes:0,tests:0,usesType:0,writes:0,callReferences:0,properties:0},o=this.loadConfig(t),a=b.createProgram({rootNames:o.fileNames,options:o.options}),d=a.getTypeChecker(),c=a.getSourceFiles().filter(m=>{if(m.isDeclarationFile)return!1;let u=T(K(t,z(m.fileName)));return!(u.startsWith("../")||u.includes("node_modules/"))}),l=0;for(let m of c){let u=T(K(t,z(m.fileName))),f=this.fileNode(e,u);if(!f)continue;let g=h=>{if(b.isCallExpression(h)&&this.addRoute(e,t,m,f,h,d)&&i.routes++,b.isTypeReferenceNode(h)&&this.addTypeUse(e,t,m,h,d)&&i.usesType++,b.isBinaryExpression(h)&&_n.has(h.operatorToken.kind)){let y=this.addWrite(e,t,m,h.left,d);y.added&&(i.writes++,y.propertyCreated&&i.properties++)}if((b.isPrefixUnaryExpression(h)||b.isPostfixUnaryExpression(h))&&(h.operator===b.SyntaxKind.PlusPlusToken||h.operator===b.SyntaxKind.MinusMinusToken)){let y=this.addWrite(e,t,m,h.operand,d);y.added&&(i.writes++,y.propertyCreated&&i.properties++)}b.forEachChild(h,g)};g(m),l++,s?.({current:l,total:c.length,detail:u})}return i.tests+=this.addTestEdges(e),r&&(i.callReferences+=this.addResolvedCalls(e,r)),i}addRoute(e,t,r,s,i,o){if(!b.isPropertyAccessExpression(i.expression))return!1;let a=i.expression.name.text.toLowerCase();if(!On.has(a))return!1;let d=i.arguments[0];if(!d||!(b.isStringLiteral(d)||b.isNoSubstitutionTemplateLiteral(d)))return!1;let c=d.text;if(!c.startsWith("/"))return!1;let l=ne(r,i),m=et(e,r.fileName,l,a,c);this.graph.getSymbol(m)||(this.graph.addSymbol({id:m,projectId:e,name:`${a.toUpperCase()} ${c}`,qualifiedName:`${a.toUpperCase()} ${c}`,type:"route",filePath:s.filePath,startLine:l,endLine:l}),this.graph.addEdge(this.edge(e,s.id,"DEFINES",m)));let u=i.arguments.at(-1);if(u){let f=this.resolveNodeSymbol(e,t,u,o);f&&this.graph.addEdge(this.edge(e,m,"ROUTE",f.id,{method:a.toUpperCase(),path:c}))}return!0}addTypeUse(e,t,r,s,i){let o=this.resolveNodeSymbol(e,t,s.typeName,i);if(!o)return!1;let a=this.containingSymbol(e,T(K(t,z(r.fileName))),ne(r,s));return!a||a.id===o.id?!1:(this.graph.addEdge(this.edge(e,a.id,"USES_TYPE",o.id)),!0)}addWrite(e,t,r,s,i){let o=this.resolveTsSymbol(s,i),a=o?.valueDeclaration??o?.declarations?.[0];if(!a)return{added:!1,propertyCreated:!1};let d=a.getSourceFile();if(d.isDeclarationFile)return{added:!1,propertyCreated:!1};let c=T(K(t,z(d.fileName)));if(c.startsWith("../")||c.includes("node_modules/"))return{added:!1,propertyCreated:!1};let l=ne(d,a),m=this.findSymbolAt(e,c,l),u=!1;if(!m||m.type==="file"){let h=o?.getName()??s.getText(r),y=et(e,c,"property",h,l);if(m=this.graph.getSymbol(y),!m){m={id:y,projectId:e,name:h,qualifiedName:h,type:"property",filePath:c,startLine:l,endLine:l},this.graph.addSymbol(m);let p=this.fileNode(e,c);p&&this.graph.addEdge(this.edge(e,p.id,"DEFINES",m.id)),u=!0}}if(!m)return{added:!1,propertyCreated:u};let f=T(K(t,z(r.fileName))),g=this.containingSymbol(e,f,ne(r,s));return!g||g.id===m.id?{added:!1,propertyCreated:u}:(this.graph.addEdge(this.edge(e,g.id,"WRITES",m.id,{expression:s.getText(r)})),{added:!0,propertyCreated:u})}addTestEdges(e){let t=this.graph.allEdges(e),r=0;for(let s of t){if(s.type!=="IMPORTS")continue;let i=this.graph.getSymbol(s.from),o=this.graph.getSymbol(s.to);!i||!o||!this.isTestFile(i.filePath)||this.isTestFile(o.filePath)||(this.graph.addEdge(this.edge(e,i.id,"TESTS",o.id)),r++)}return r}addResolvedCalls(e,t){let r=0;for(let s of t.resolutions){if(s.kind!=="CALL"||!s.targetSymbolId)continue;let i=this.graph.getSymbol(s.targetSymbolId);if(!i)continue;let o=this.containingSymbol(e,s.sourceFile,s.sourceLine);!o||o.id===i.id||this.graph.allEdges(e).some(d=>d.type==="CALLS"&&d.from===o.id&&d.to===i.id)||(this.graph.addEdge(this.edge(e,o.id,"CALL_REFERENCE",i.id,{expression:s.expression,confidence:s.confidence,resolver:s.resolver})),r++)}return r}resolveNodeSymbol(e,t,r,s){let i=this.resolveTsSymbol(r,s),o=i?.valueDeclaration??i?.declarations?.[0];if(!o)return;let a=o.getSourceFile();if(a.isDeclarationFile)return;let d=T(K(t,z(a.fileName)));if(!(d.startsWith("../")||d.includes("node_modules/")))return this.findSymbolAt(e,d,ne(a,o),i?.getName())}resolveTsSymbol(e,t){let r=t.getSymbolAtLocation(e);if(r&&r.flags&b.SymbolFlags.Alias)try{r=t.getAliasedSymbol(r)}catch{}return r}containingSymbol(e,t,r){return this.graph.allSymbols(e).filter(s=>T(s.filePath)===T(t)&&typeof s.startLine=="number"&&typeof s.endLine=="number"&&s.startLine<=r&&s.endLine>=r).sort((s,i)=>(s.endLine??0)-(s.startLine??0)-((i.endLine??0)-(i.startLine??0)))[0]}findSymbolAt(e,t,r,s){let i=this.graph.allSymbols(e).filter(o=>T(o.filePath)===T(t)&&typeof o.startLine=="number"&&typeof o.endLine=="number"&&o.startLine<=r&&o.endLine>=r).sort((o,a)=>(o.endLine??0)-(o.startLine??0)-((a.endLine??0)-(a.startLine??0)));if(s){let o=i.find(a=>a.name===s||(a.qualifiedName?.endsWith(`.${s}`)??!1));if(o)return o}return i[0]}fileNode(e,t){return this.graph.allSymbols(e).find(r=>r.type==="file"&&T(r.filePath)===T(t))}isTestFile(e){let t=T(e).toLowerCase();return t.startsWith("tests/")||t.includes("/tests/")||t.includes("__tests__")||/\.(test|spec)\.[^.]+$/.test(t)}edge(e,t,r,s,i){return{id:et(e,t,r,s,JSON.stringify(i??{})),projectId:e,from:t,to:s,type:r,metadata:i}}loadConfig(e){let t=b.findConfigFile(e,b.sys.fileExists,"tsconfig.json");if(t){let r=b.readConfigFile(t,b.sys.readFile);return b.parseJsonConfigFileContent(r.config,b.sys,Ln(t))}return{fileNames:b.sys.readDirectory(e,[".ts",".tsx",".js",".jsx",".mts",".cts",".mjs",".cjs"],["node_modules",".git","dist","build","coverage"]),options:{allowJs:!0,noEmit:!0,skipLibCheck:!0,target:b.ScriptTarget.ES2022,module:b.ModuleKind.NodeNext,moduleResolution:b.ModuleResolutionKind.NodeNext}}}};function jn(n){return n.replaceAll("\\","/").toLowerCase()}var B=class{constructor(e){this.graph=e}graph;detect(e){let t=this.graph.allSymbols(e),r=this.graph.allEdges(e),s=[];for(let o of t){if(o.type!=="route"&&o.type!=="file")continue;let a=this.classify(o,r);a&&s.push({symbolId:o.id,filePath:o.filePath,name:o.name,kind:a.kind,score:a.score,reasons:a.reasons})}let i=new Map;for(let o of s){let a=o.kind==="route"?o.symbolId:o.filePath,d=i.get(a);(!d||o.score>d.score)&&i.set(a,o)}return[...i.values()].sort((o,a)=>a.score-o.score||o.filePath.localeCompare(a.filePath))}classify(e,t){let r=jn(e.filePath),s=[];if(e.type==="route")return{kind:"route",score:100,reasons:["HTTP route"]};let i=null,o=0;if(r.startsWith("bin/"))i="cli",o=100,s.push("bin executable");else if(/(^|\/)(cli)\.[^.]+$/.test(r))i="cli",o=90,s.push("CLI entry file");else if(/(^|\/)(main|server)\.[^.]+$/.test(r))i="main",o=90,s.push("main/server file");else if(r.includes("/runtime/")&&/(^|\/)(bootstrap|index)\.[^.]+$/.test(r))i="runtime",o=75,s.push("runtime bootstrap");else if(/(^|\/)index\.[^.]+$/.test(r)){let d=t.filter(l=>l.to===e.id&&l.type!=="DEFINES").length,c=t.filter(l=>l.from===e.id&&l.type!=="DEFINES").length;(r.startsWith("packages/")||d===0||c>0)&&(i="package",o=r.startsWith("packages/")?70:55,s.push("package/public index"))}return i?(t.filter(d=>d.to===e.id&&d.type!=="DEFINES").length===0&&(o+=10,s.push("no incoming dependency")),{kind:i,score:o,reasons:s}):null}};var Dn={CALL_REFERENCE:6,CALLS:4,IMPORTS:3,USES_TYPE:4,WRITES:5,ROUTE:4,TESTS:1,INHERITS:5,IMPLEMENTS:5,DEFINES:.25},Re=class{constructor(e){this.graph=e}graph;analyze(e){let t=this.graph.allSymbols(e),r=this.graph.allEdges(e),s=new Map(t.map(c=>[c.id,c])),i=new Map,o=c=>{let l=i.get(c);return l||(l={symbols:0,incoming:0,outgoing:0,weightedIncoming:0,weightedOutgoing:0,writes:0,callReferences:0,crossFile:0},i.set(c,l)),l};for(let c of t)o(c.filePath).symbols++;for(let c of r){let l=s.get(c.from),m=s.get(c.to);if(!l||!m)continue;let u=Dn[c.type]??1,f=o(l.filePath),g=o(m.filePath);f.outgoing++,g.incoming++,f.weightedOutgoing+=u,g.weightedIncoming+=u,c.type==="WRITES"&&f.writes++,c.type==="CALL_REFERENCE"&&f.callReferences++,l.filePath!==m.filePath&&(f.crossFile++,g.crossFile++)}let a=[...i.entries()].map(([c,l])=>{let m=Number((l.symbols*.4+l.weightedIncoming*1.75+l.weightedOutgoing*.65+l.crossFile*1.5+l.writes*3+l.callReferences*2).toFixed(2)),u=[];return l.incoming>=10&&u.push("high incoming dependency"),l.crossFile>=10&&u.push("high cross-file coupling"),l.callReferences>=5&&u.push("many exact call references"),l.writes>0&&u.push("contains write relationships"),l.symbols>=10&&u.push("high symbol density"),{filePath:c,score:m,symbols:l.symbols,incoming:l.incoming,outgoing:l.outgoing,reasons:u}}).filter(c=>c.score>0).sort((c,l)=>l.score-c.score||c.filePath.localeCompare(l.filePath));if(a.length===0)return[];let d=Math.max(1,Math.min(30,Math.ceil(a.length*.15)));return a.slice(0,d)}};function Fn(n){return n.replaceAll("\\","/").toLowerCase()}var $n={CALL_REFERENCE:6,CALLS:4,IMPORTS:4,USES_TYPE:4,WRITES:5,INHERITS:6,IMPLEMENTS:6,ROUTE:3,TESTS:1},Te=class{constructor(e){this.graph=e}graph;detect(e){let t=this.graph.allSymbols(e),r=this.graph.allEdges(e),s=new Map(t.map(a=>[a.id,a])),i=[...new Set(t.map(a=>a.filePath))].sort(),o=new Map;for(let a of i)o.set(a,this.classifyPath(a));for(let a=0;a<3;a++){let d=!1;for(let c of i){let l=o.get(c);if(!l||l.layer!=="unknown")continue;let m=new Map;for(let f of r){let g=s.get(f.from),h=s.get(f.to);if(!g||!h||g.filePath===h.filePath)continue;let y=null;if(g.filePath===c?y=h.filePath:h.filePath===c&&(y=g.filePath),!y)continue;let p=o.get(y);if(!p||p.layer==="unknown"||p.layer==="tests")continue;let S=$n[f.type]??1;m.set(p.layer,(m.get(p.layer)??0)+S)}let u=[...m.entries()].sort((f,g)=>g[1]-f[1]||f[0].localeCompare(g[0]))[0];u&&u[1]>=4&&(o.set(c,{filePath:c,layer:u[0],confidence:.65,reasons:[`graph-neighbor vote=${u[1]}`]}),d=!0)}if(!d)break}return i.map(a=>o.get(a))}classifyPath(e){let t=Fn(e);return t.startsWith("tests/")||t.includes("/tests/")||t.includes("__tests__")||/\.(test|spec)\.[^.]+$/.test(t)?{filePath:e,layer:"tests",confidence:1,reasons:["test path"]}:t.startsWith("bin/")||t.includes("/mcp/")||t.includes("/cli/")||t.includes("/api/")||t.includes("/routes/")||t.includes("/hooks/")?{filePath:e,layer:"interface",confidence:.9,reasons:["interface boundary"]}:t.includes("/runtime/")||t.includes("/processor/")||t.includes("/retrieval/")||t.includes("/snapshot/")||t.includes("/code-intelligence/")?{filePath:e,layer:"application",confidence:.85,reasons:["application orchestration"]}:t.includes("/core/")||t.includes("/memory/")||/(^|\/)types\.[^.]+$/.test(t)?{filePath:e,layer:"domain",confidence:.9,reasons:["domain/core logic"]}:t.includes("/storage/")||t.includes("/production/")||t.includes("/security/")||t.includes("/config/")||t.includes("/utils/")?{filePath:e,layer:"infrastructure",confidence:.85,reasons:["infrastructure boundary"]}:{filePath:e,layer:"unknown",confidence:.35,reasons:[]}}};import{createHash as Gn}from"node:crypto";var Kn={CALL_REFERENCE:7,CALLS:3,IMPORTS:4,USES_TYPE:4,WRITES:5,INHERITS:7,IMPLEMENTS:7,ROUTE:4,TESTS:1};function zn(n){return Gn("sha256").update(n).digest("hex").slice(0,12)}function ke(n){return n.replaceAll("\\","/")}function Bn(n){let t=ke(n).split("/").filter(Boolean);return t[0]==="src"&&t[1]?`src/${t[1]}`:t[0]==="packages"&&t[1]?`packages/${t[1]}`:t[0]==="tests"?"tests":t[0]==="bin"?"bin":t[0]??"root"}function Un(n){let e=n.replace(/^src\//,"").replace(/^packages\//,"");return{"code-intelligence":"Code Intelligence",memory:"Memory",storage:"Storage",retrieval:"Retrieval",runtime:"Runtime",mcp:"MCP",hooks:"Hooks",security:"Security",snapshot:"Snapshots",production:"Production",capture:"Capture",processor:"Processor",core:"Core",tests:"Tests",bin:"CLI",sdk:"SDK",cli:"CLI"}[e]??e}var Ie=class{constructor(e){this.graph=e}graph;detect(e){let t=this.graph.allSymbols(e),r=this.graph.allEdges(e),s=new Map(t.map(u=>[u.id,u])),i=[...new Set(t.map(u=>ke(u.filePath)))].sort(),o=new Map,a=(u,f)=>{let g=[u,f].sort();return`${g[0]}\0${g[1]}`};for(let u of r){let f=s.get(u.from),g=s.get(u.to);if(!f||!g)continue;let h=ke(f.filePath),y=ke(g.filePath);if(h===y)continue;let p=Kn[u.type]??0;if(p<=0)continue;let S=a(h,y),x=o.get(S);x?x.weight+=p:o.set(S,{a:[h,y].sort()[0],b:[h,y].sort()[1],weight:p})}let d=new Map;for(let u of i){let f=Bn(u),g=d.get(f)??[];g.push(u),d.set(f,g)}let c=[];for(let[u,f]of d){if(f.sort(),f.length<=30){c.push({subsystem:u,files:f});continue}let g=new Set(f),h=new Map;for(let x of f)h.set(x,new Set);for(let x of o.values())!g.has(x.a)||!g.has(x.b)||x.weight>=8&&(h.get(x.a)?.add(x.b),h.get(x.b)?.add(x.a));let y=new Set,p=[];for(let x of f){if(y.has(x))continue;let C=[x],w=[];for(y.add(x);C.length>0;){let I=C.shift();w.push(I);for(let We of h.get(I)??[])y.has(We)||(y.add(We),C.push(We))}p.push(w.sort())}let S=[];for(let x of p)x.length>=2?c.push({subsystem:u,files:x}):S.push(...x);S.length>0&&c.push({subsystem:`${u}/residual`,files:S.sort()})}let l=c.map(u=>({subsystem:u.subsystem,files:[...u.files]}));for(let u=0;u<3;u++){let f=!1;e:for(let g=0;g<l.length;g++){let h=l[g];if(!(!h||h.files.length>3))for(let y=0;y<l.length;y++){if(g===y)continue;let p=l[y];if(!p)continue;let S=0,x=new Set(h.files),C=new Set(p.files);for(let w of o.values())(x.has(w.a)&&C.has(w.b)||x.has(w.b)&&C.has(w.a))&&(S+=w.weight);if(S>=20){p.files=[...new Set([...p.files,...h.files])].sort(),l.splice(g,1),f=!0;break e}}}if(!f)break}let m=[];for(let u of l){let f=new Set(u.files),g=0,h=0;for(let C of o.values()){let w=f.has(C.a),I=f.has(C.b);w&&I?g+=C.weight:(w||I)&&(h+=C.weight)}let y=g+h,p=u.subsystem.replace(/\/residual$/,""),S=Un(p),x=u.subsystem.endsWith("/residual")?`${S} Misc`:S;m.push({id:`cluster-${zn(`${u.subsystem}:${u.files.join("|")}`)}`,label:x,subsystem:p,files:u.files,size:u.files.length,internalWeight:g,externalWeight:h,cohesion:y>0?Number((g/y).toFixed(4)):0})}return m.sort((u,f)=>f.size-u.size||f.cohesion-u.cohesion||u.label.localeCompare(f.label))}};var Ae=class{constructor(e){this.graph=e;this.entryPoints=new B(e),this.hotspots=new Re(e),this.layers=new Te(e),this.clusters=new Ie(e)}graph;entryPoints;hotspots;layers;clusters;analyze(e){let t=this.entryPoints.detect(e),r=this.hotspots.analyze(e),s=this.layers.detect(e),i=this.clusters.detect(e),o=new Set(this.graph.allSymbols(e).map(a=>a.filePath));return{version:1,projectId:e,updatedAt:new Date().toISOString(),summary:{files:o.size,entryPoints:t.length,hotspots:r.length,layers:s.length,clusters:i.length},entryPoints:t,hotspots:r,layers:s,clusters:i}}};var Gt=new Set(["CALLS","CALL_REFERENCE","IMPORTS","USES_TYPE","WRITES","INHERITS","IMPLEMENTS","ROUTE","TESTS"]);function tt(n){return n.replaceAll("\\","/").toLowerCase()}function Hn(n){let e=tt(n);return e.startsWith("tests/")||e.includes("/tests/")||e.includes("__tests__")||/\.(test|spec)\.[^.]+$/.test(e)}function Wn(n){let e=tt(n.filePath);return e.startsWith("bin/")||/(^|\/)index\.[^.]+$/.test(e)||/(^|\/)(main|server|cli)\.[^.]+$/.test(e)}var Me=class{constructor(e){this.graph=e}graph;analyze(e){let t=this.graph.allSymbols(e),r=this.graph.allEdges(e),s=new Set(new B(this.graph).detect(e).map(o=>o.symbolId)),i=[];for(let o of t){if(!["function","method","class","interface","property"].includes(o.type)||Hn(o.filePath)||s.has(o.id)||Wn(o)||r.filter(f=>f.to===o.id&&Gt.has(f.type)).length>0)continue;let d=r.filter(f=>f.from===o.id&&Gt.has(f.type)),c=["no incoming usage edge"],l=50;(o.type==="function"||o.type==="method")&&(l+=20,c.push("callable symbol")),o.type==="property"&&(l-=15),d.length===0&&(l+=10,c.push("no outgoing dependency")),tt(o.filePath).includes("/internal/")&&(l+=10,c.push("internal implementation")),o.name.startsWith("_")&&(l+=5);let u=l>=75?"high":l>=55?"medium":"low";i.push({symbolId:o.id,name:o.name,qualifiedName:o.qualifiedName,type:o.type,filePath:o.filePath,startLine:o.startLine,confidence:u,score:l,reasons:c})}return i.sort((o,a)=>a.score-o.score||o.filePath.localeCompare(a.filePath)||o.name.localeCompare(a.name))}};var Yn=new Set(["CALLS","CALL_REFERENCE","IMPORTS","USES_TYPE","WRITES","INHERITS","IMPLEMENTS","ROUTE"]),Ne=class{constructor(e){this.graph=e}graph;analyze(e){let t=this.graph.allSymbols(e),r=this.graph.allEdges(e).filter(o=>Yn.has(o.type)),s=new Map(t.map(o=>[o.id,o]));return[...new Set(t.map(o=>o.filePath))].sort().map(o=>{let a=new Set,d=new Set,c=0,l=0;for(let m of r){let u=s.get(m.from),f=s.get(m.to);!u||!f||u.filePath===f.filePath||(u.filePath===o&&(a.add(f.filePath),c++),f.filePath===o&&(d.add(u.filePath),l++))}return{filePath:o,dependencies:[...a].sort(),dependents:[...d].sort(),outgoingEdges:c,incomingEdges:l}})}};var Le=class{constructor(e){this.graph=e}graph;analyze(e){let t=new Me(this.graph).analyze(e),r=new Ne(this.graph).analyze(e);return{version:1,projectId:e,updatedAt:new Date().toISOString(),summary:{deadCodeCandidates:t.length,highConfidenceDeadCode:t.filter(s=>s.confidence==="high").length,files:r.length},deadCode:t,dependencies:r}}};var Oe=class{constructor(e){this.graph=e}graph;build(e,t,r,s){let i=this.graph.allSymbols(e),o=this.graph.allEdges(e),a=new Map(t?.layers.map(p=>[p.filePath,p.layer])??[]),d=new Map;for(let p of t?.clusters??[])for(let S of p.files)d.set(S,{id:p.id,label:p.label});let c=new Map(t?.hotspots.map(p=>[p.filePath,p.score])??[]),l=new Map(r?.deadCode.map(p=>[p.symbolId,{confidence:p.confidence,score:p.score}])??[]),m=new Map,u=new Map;for(let p of o)u.set(p.from,(u.get(p.from)??0)+1),m.set(p.to,(m.get(p.to)??0)+1);let f=[];for(let p=0;p<i.length;p++){let S=i[p],x=d.get(S.filePath);f.push({id:S.id,name:S.name,qualifiedName:S.qualifiedName,type:S.type,filePath:S.filePath,layer:a.get(S.filePath),cluster:x?.id,clusterLabel:x?.label,hotspotScore:c.get(S.filePath),deadCode:l.get(S.id),incoming:m.get(S.id)??0,outgoing:u.get(S.id)??0}),((p+1)%100===0||p===i.length-1)&&s?.({current:p+1,total:i.length,phase:"nodes"})}let g=new Set(f.map(p=>p.id)),h=o.filter(p=>g.has(p.from)&&g.has(p.to)),y=[];for(let p=0;p<h.length;p++){let S=h[p];y.push({source:S.from,target:S.to,type:S.type,confidence:typeof S.metadata?.confidence=="string"?S.metadata.confidence:void 0,resolver:typeof S.metadata?.resolver=="string"?S.metadata.resolver:void 0}),((p+1)%100===0||p===h.length-1)&&s?.({current:p+1,total:h.length,phase:"links"})}return{version:1,projectId:e,generatedAt:new Date().toISOString(),summary:{nodes:f.length,links:y.length,files:new Set(f.map(p=>p.filePath)).size,clusters:new Set(f.map(p=>p.cluster).filter(Boolean)).size},nodes:f,links:y}}};import{spawnSync as Vn}from"node:child_process";var qn=[{language:"python",command:"pyright-langserver"},{language:"go",command:"gopls"},{language:"rust",command:"rust-analyzer"},{language:"c",command:"clangd"},{language:"cpp",command:"clangd"}];function Jn(n){return n.split(/\r?\n/u).map(t=>t.trim()).find(Boolean)||void 0}function Qn(n,e){let t=Vn(e,["--version"],{encoding:"utf8",timeout:2e3,windowsHide:!0}),r=!t.error&&t.status===0,s=[t.stdout??"",t.stderr??""].join(`
|
|
6
|
+
`);return{language:n,command:e,installed:r,...r?{version:Jn(s)}:{},mode:"external-optional",activeInStructuralGraph:!1}}function Kt(){return qn.map(n=>Qn(n.language,n.command))}var Zn=process.argv.includes("--json"),zt=Kt(),Xn={structuralParser:{engine:"typescript-compiler-api",languages:G.filter(n=>n.structural).map(n=>n.language)},lexicalSearch:{engine:"sqlite-fts5-bm25",languages:G.filter(n=>n.lexicalSearch).map(n=>n.language)},lspFoundation:{mode:"detection-only",automaticDownload:!1,structuralGraphIntegration:!1,servers:zt}};if(Zn)console.log(JSON.stringify(Xn,null,2));else{console.log("ToolNet Code Intelligence Capabilities"),console.log(""),console.log("Structural parser: TypeScript Compiler API");for(let n of G)console.log([n.language.padEnd(12),n.structural?"structural=yes":"structural=no",n.lexicalSearch?"lexical=yes":"lexical=no",n.lspServer?`lsp=${n.lspServer}`:""].filter(Boolean).join(" "));console.log(""),console.log("LSP detection:");for(let n of zt)console.log([n.language.padEnd(12),n.command.padEnd(22),n.installed?"available":"not-installed"].join(" "))}import{homedir as As}from"node:os";import{join as Ms}from"node:path";import{DeleteObjectCommand as es,GetObjectCommand as ts,HeadObjectCommand as rs,ListObjectsV2Command as ns,PutObjectCommand as ss,S3Client as os}from"@aws-sdk/client-s3";import{getSignedUrl as is}from"@aws-sdk/s3-request-presigner";var _e=class{name="huggingface";client;bucket;constructor(e){this.bucket=e.bucket,this.client=new os({region:"us-east-1",endpoint:`https://s3.hf.co/${e.namespace}`,forcePathStyle:!0,requestChecksumCalculation:"WHEN_REQUIRED",responseChecksumValidation:"WHEN_REQUIRED",credentials:{accessKeyId:e.accessKeyId,secretAccessKey:e.secretAccessKey}})}async put(e,t,r="application/octet-stream"){let s=typeof t=="string"?Buffer.from(t,"utf8"):t;await this.client.send(new ss({Bucket:this.bucket,Key:e,Body:s,ContentType:r}))}async get(e){let t=await is(this.client,new ts({Bucket:this.bucket,Key:e}),{expiresIn:60}),r=await fetch(t,{redirect:"follow"});if(r.status===404)return null;if(!r.ok)throw new Error(`HF download failed: ${r.status} ${r.statusText}`);return new Uint8Array(await r.arrayBuffer())}async getText(e){let t=await this.get(e);return t?Buffer.from(t).toString("utf8"):null}async exists(e){try{return await this.client.send(new rs({Bucket:this.bucket,Key:e})),!0}catch(t){if(typeof t=="object"&&t!==null&&"$metadata"in t&&t.$metadata?.httpStatusCode===404)return!1;throw t}}async delete(e){await this.client.send(new es({Bucket:this.bucket,Key:e}))}async list(e=""){let t=[],r;do{let s=await this.client.send(new ns({Bucket:this.bucket,Prefix:e||void 0,ContinuationToken:r}));for(let i of s.Contents??[])i.Key&&t.push({key:i.Key,size:i.Size,updatedAt:i.LastModified?.toISOString()});r=s.IsTruncated?s.NextContinuationToken:void 0}while(r);return t}};import{access as Bt,mkdir as as,readFile as cs,readdir as ls,rm as ds,stat as Ut,writeFile as us}from"node:fs/promises";import{dirname as ps,join as ms,relative as Ht,resolve as fs}from"node:path";var se=class{constructor(e){this.root=e}root;name="local";path(e){let t=e.replace(/^\/+/,"");return fs(this.root,t)}async put(e,t){let r=this.path(e);await as(ps(r),{recursive:!0}),await us(r,t)}async get(e){try{return await cs(this.path(e))}catch(t){if(typeof t=="object"&&t!==null&&"code"in t&&t.code==="ENOENT")return null;throw t}}async getText(e){let t=await this.get(e);return t?Buffer.from(t).toString("utf8"):null}async exists(e){try{return await Bt(this.path(e)),!0}catch{return!1}}async delete(e){await ds(this.path(e),{force:!0})}async list(e=""){let t=this.path(e),r=[];try{await Bt(t)}catch{return r}let s=async o=>{let a=await ls(o,{withFileTypes:!0});for(let d of a){let c=ms(o,d.name);if(d.isDirectory()){await s(c);continue}let l=await Ut(c);r.push({key:Ht(this.root,c),size:l.size,updatedAt:l.mtime.toISOString()})}},i=await Ut(t);return i.isDirectory()?await s(t):r.push({key:Ht(this.root,t),size:i.size,updatedAt:i.mtime.toISOString()}),r}};import{DeleteObjectCommand as gs,GetObjectCommand as hs,HeadObjectCommand as ys,ListObjectsV2Command as Ss,PutObjectCommand as bs,S3Client as xs}from"@aws-sdk/client-s3";import{getSignedUrl as Es}from"@aws-sdk/s3-request-presigner";var oe=class{name;client;bucket;constructor(e){this.name=e.name??"s3",this.bucket=e.bucket,this.client=new xs({region:e.region??"us-east-1",endpoint:e.endpoint||void 0,forcePathStyle:e.forcePathStyle??!1,requestChecksumCalculation:"WHEN_REQUIRED",responseChecksumValidation:"WHEN_REQUIRED",credentials:{accessKeyId:e.accessKeyId,secretAccessKey:e.secretAccessKey}})}async put(e,t,r="application/octet-stream"){let s=typeof t=="string"?Buffer.from(t,"utf8"):t;await this.client.send(new bs({Bucket:this.bucket,Key:e,Body:s,ContentType:r}))}async get(e){let t=await Es(this.client,new hs({Bucket:this.bucket,Key:e}),{expiresIn:60}),r=await fetch(t,{redirect:"follow"});if(r.status===404)return null;if(!r.ok)throw new Error(`${this.name} download failed: ${r.status} ${r.statusText}`);return new Uint8Array(await r.arrayBuffer())}async getText(e){let t=await this.get(e);return t?Buffer.from(t).toString("utf8"):null}async exists(e){try{return await this.client.send(new ys({Bucket:this.bucket,Key:e})),!0}catch(t){if(typeof t=="object"&&t!==null&&"$metadata"in t&&t.$metadata?.httpStatusCode===404)return!1;throw t}}async delete(e){await this.client.send(new gs({Bucket:this.bucket,Key:e}))}async list(e=""){let t=[],r;do{let s=await this.client.send(new Ss({Bucket:this.bucket,Prefix:e||void 0,ContinuationToken:r}));for(let i of s.Contents??[])i.Key&&t.push({key:i.Key,size:i.Size,updatedAt:i.LastModified?.toISOString()});r=s.IsTruncated?s.NextContinuationToken:void 0}while(r);return t}};import{createCipheriv as Cs,createDecipheriv as vs,createHash as ws,randomBytes as Ps,timingSafeEqual as Rs}from"node:crypto";import{readFileSync as Ts}from"node:fs";var j=Buffer.from("TNMEME01","ascii"),Yt=1,ie=8,ae=12,rt=16,Vt=j.length+1+ie+ae+rt,ks="toolnet-memory:remote-encryption:v1:",qt="aes-256-gcm",nt=32,P=class extends Error{constructor(t,r){super(r);this.code=t;this.name="RemoteEncryptionError"}code};function Is(n){return n?["1","true","yes","on","enabled"].includes(n.trim().toLowerCase()):!1}function st(n=process.env){return Is(n.TOOLNET_REMOTE_ENCRYPTION)}function Wt(n){let e=n.trim();if(!e)throw new P("REMOTE_ENCRYPTION_KEY_EMPTY","Remote encryption key is empty.");let t;if(e.startsWith("hex:")){let r=e.slice(4);if(!/^[0-9a-f]{64}$/iu.test(r))throw new P("REMOTE_ENCRYPTION_KEY_INVALID","hex: remote encryption key must contain exactly 64 hexadecimal characters.");t=Buffer.from(r,"hex")}else if(/^[0-9a-f]{64}$/iu.test(e))t=Buffer.from(e,"hex");else{let r=e.startsWith("base64:")?e.slice(7):e;if(!/^[A-Za-z0-9+/_-]+={0,2}$/u.test(r))throw new P("REMOTE_ENCRYPTION_KEY_INVALID","Remote encryption key must be 32 raw bytes encoded as hexadecimal or base64.");t=Buffer.from(r,r.includes("-")||r.includes("_")?"base64url":"base64")}if(t.length!==nt)throw new P("REMOTE_ENCRYPTION_KEY_INVALID_LENGTH",`Remote encryption key must decode to exactly ${nt} bytes.`);return t}function Jt(n=process.env){let e=n.TOOLNET_REMOTE_ENCRYPTION_KEY?.trim(),t=n.TOOLNET_REMOTE_ENCRYPTION_KEY_FILE?.trim();if(e&&t)throw new P("REMOTE_ENCRYPTION_KEY_AMBIGUOUS","Configure either TOOLNET_REMOTE_ENCRYPTION_KEY or TOOLNET_REMOTE_ENCRYPTION_KEY_FILE, not both.");if(e)return Wt(e);if(t){let r;try{r=Ts(t,"utf8")}catch(s){throw new P("REMOTE_ENCRYPTION_KEY_FILE_READ_FAILED",[`Unable to read remote encryption key file: ${t}.`,s instanceof Error?s.message:String(s)].join(" "))}return Wt(r)}}function Qt(n){return ws("sha256").update(n).digest().subarray(0,ie)}function Zt(n){return Buffer.from(`${ks}${n}`,"utf8")}function ot(n){return n.byteLength<j.length?!1:Buffer.from(n).subarray(0,j.length).equals(j)}function Xt(n,e,t){if(t.byteLength!==nt)throw new P("REMOTE_ENCRYPTION_KEY_INVALID_LENGTH","AES-256-GCM requires a 32-byte key.");let r=typeof e=="string"?Buffer.from(e,"utf8"):Buffer.from(e),s=Ps(ae),i=Cs(qt,t,s);i.setAAD(Zt(n));let o=Buffer.concat([i.update(r),i.final()]),a=i.getAuthTag(),d=Buffer.alloc(Vt),c=0;return j.copy(d,c),c+=j.length,d.writeUInt8(Yt,c),c+=1,Qt(t).copy(d,c),c+=ie,s.copy(d,c),c+=ae,a.copy(d,c),Buffer.concat([d,o])}function er(n,e,t){let r=Buffer.from(e);if(!ot(r))throw new P("REMOTE_ENCRYPTION_ENVELOPE_REQUIRED","Payload is not a ToolNet encrypted remote object.");if(r.length<Vt)throw new P("REMOTE_ENCRYPTION_ENVELOPE_TRUNCATED","Encrypted remote payload is truncated.");let s=j.length,i=r.readUInt8(s);if(s+=1,i!==Yt)throw new P("REMOTE_ENCRYPTION_VERSION_UNSUPPORTED",`Unsupported remote encryption envelope version: ${i}.`);let o=r.subarray(s,s+ie);s+=ie;let a=Qt(t);if(!Rs(o,a))throw new P("REMOTE_ENCRYPTION_KEY_MISMATCH","Configured remote encryption key does not match this encrypted object.");let d=r.subarray(s,s+ae);s+=ae;let c=r.subarray(s,s+rt);s+=rt;let l=r.subarray(s),m=vs(qt,t,d);m.setAAD(Zt(n)),m.setAuthTag(c);try{return Buffer.concat([m.update(l),m.final()])}catch{throw new P("REMOTE_ENCRYPTION_AUTH_FAILED","Encrypted remote object failed AES-GCM authentication.")}}var it=class{constructor(e,t){this.inner=e;this.options=t;if(this.name=e.name,t.enabled&&!t.key)throw new P("REMOTE_ENCRYPTION_KEY_REQUIRED","Remote client-side encryption is enabled but no encryption key is configured.")}inner;options;name;async put(e,t,r){if(!this.options.enabled){await this.inner.put(e,t,r);return}let s=this.options.key;if(!s)throw new P("REMOTE_ENCRYPTION_KEY_REQUIRED","Remote encryption key is unavailable.");let i=Xt(e,t,s);await this.inner.put(e,i,"application/octet-stream")}async get(e){let t=await this.inner.get(e);if(!t)return null;if(!ot(t))return t;if(!this.options.enabled)throw new P("REMOTE_ENCRYPTION_REQUIRED",["Remote object is client-side encrypted.","Enable TOOLNET_REMOTE_ENCRYPTION and configure the matching key."].join(" "));let r=this.options.key;if(!r)throw new P("REMOTE_ENCRYPTION_KEY_REQUIRED","Remote object is encrypted but no decryption key is configured.");return er(e,t,r)}async getText(e){let t=await this.get(e);return t?Buffer.from(t).toString("utf8"):null}async exists(e){return this.inner.exists(e)}async delete(e){await this.inner.delete(e)}async list(e=""){return this.inner.list(e)}};function tr(n,e=process.env){if(n.name==="local")return st(e)&&console.warn("[storage] Remote encryption requested but active storage provider is local; local data remains unchanged."),n;let t=st(e),r=t?Jt(e):void 0;return new it(n,{enabled:t,key:r})}function ce(n){return tr(n)}function at(n,e){return console.warn(e),ce(new se(n))}function rr(n){let e=n.localRoot??Ms(As(),".toolnet-memory","storage");if(n.provider==="r2"){let t=n.r2;return t?.accountId&&t.bucket&&t.accessKeyId&&t.secretAccessKey?ce(new oe({name:"r2",endpoint:`https://${t.accountId}.r2.cloudflarestorage.com`,region:"auto",bucket:t.bucket,forcePathStyle:!0,accessKeyId:t.accessKeyId,secretAccessKey:t.secretAccessKey})):at(e,"[storage] Cloudflare R2 credentials missing. Using local fallback.")}if(n.provider==="s3"){let t=n.s3;return t?.bucket&&t.accessKeyId&&t.secretAccessKey?ce(new oe({name:"s3",endpoint:t.endpoint,region:t.region??"us-east-1",bucket:t.bucket,forcePathStyle:t.forcePathStyle??!1,accessKeyId:t.accessKeyId,secretAccessKey:t.secretAccessKey})):at(e,"[storage] S3 credentials missing. Using local fallback.")}if(n.provider==="huggingface"){let t=n.huggingface;return t?.namespace&&t.bucket&&t.accessKeyId&&t.secretAccessKey?ce(new _e({namespace:t.namespace,bucket:t.bucket,accessKeyId:t.accessKeyId,secretAccessKey:t.secretAccessKey})):at(e,"[storage] Hugging Face credentials missing. Using local fallback.")}return ce(new se(e))}function Ns(n){return new Promise(e=>setTimeout(e,n))}async function nr(n,e={}){let t=Math.max(1,e.attempts??3),r=e.baseDelayMs??150,s=e.maxDelayMs??2e3,i;for(let o=1;o<=t;o++)try{return await n()}catch(a){if(i=a,o>=t)break;let d=Math.min(s,r*2**(o-1)),c=Math.floor(Math.random()*Math.max(1,d*.2));await Ns(d+c)}throw i}var Ls=new Set(["put","get","getText","delete","list"]);function sr(n,e={}){return new Proxy(n,{get(t,r){let s=Reflect.get(t,r,t);return typeof s!="function"?s:Ls.has(r)?(...i)=>nr(()=>Promise.resolve(s.apply(t,i)),e):s.bind(t)}})}function or(n){let e=n.trim().replace(/\s+/g,"_").replace(/[^A-Za-z0-9._-]/g,"_").replace(/_+/g,"_").replace(/^\.+|\.+$/g,"").slice(0,100);if(!e||e==="."||e==="..")throw new Error("Invalid project storage folder");return e}function ir(n){let e=n.replace(/^\/+/,"");if(e.startsWith("memories/"))return"memory/records/"+e.slice(9);if(e.startsWith("vectors/"))return"memory/vectors/"+e.slice(8);if(e.startsWith("graph/"))return"code/graph/"+e.slice(6);let t=e.match(/^(snapshots\/[^/]+\/)memories\/(.+)$/);if(t)return`${t[1]}memory/records/${t[2]}`;if(t=e.match(/^(snapshots\/[^/]+\/)vectors\/(.+)$/),t)return`${t[1]}memory/vectors/${t[2]}`;if(t=e.match(/^(snapshots\/[^/]+\/)graph\/(.+)$/),t)return`${t[1]}code/graph/${t[2]}`;let r=e.match(/^(projects\/[^/]+\/snapshots\/[^/]+\/)memories\/(.+)$/);if(r)return`${r[1]}memory/records/${r[2]}`;if(r=e.match(/^(projects\/[^/]+\/snapshots\/[^/]+\/)vectors\/(.+)$/),r)return`${r[1]}memory/vectors/${r[2]}`;if(r=e.match(/^(projects\/[^/]+\/snapshots\/[^/]+\/)graph\/(.+)$/),r)return`${r[1]}code/graph/${r[2]}`;let s=e.match(/^(projects\/[^/]+\/)memories\/(.+)$/);return s?`${s[1]}memory/records/${s[2]}`:(s=e.match(/^(projects\/[^/]+\/)vectors\/(.+)$/),s?`${s[1]}memory/vectors/${s[2]}`:(s=e.match(/^(projects\/[^/]+\/)graph\/(.+)$/),s?`${s[1]}code/graph/${s[2]}`:e))}var je=class{constructor(e,t,r,s){this.provider=e;this.name=e.name,this.projectId=t,this.projectName=r,this.folder=or(s??r),this.sourcePrefix=`projects/${t}`,this.targetPrefix=`projects/${this.folder}`}provider;name;folder;sourcePrefix;targetPrefix;projectId;projectName;registryPromise;async registerProject(){let e=`${this.targetPrefix}/project.json`,t=new Date().toISOString(),r=t,s=await this.provider.getText(e);if(s){let o;try{o=JSON.parse(s)}catch(a){throw new Error(`Invalid remote ToolNet project manifest at ${e}: ${a instanceof Error?a.message:String(a)}`)}if(typeof o.id=="string"&&o.id!==this.projectId)throw new Error(["ToolNet remote project namespace collision.",`Remote folder: ${this.targetPrefix}`,`Existing project id: ${o.id}`,`Current project id: ${this.projectId}`,"Refusing to mix data from two projects."].join(" "));typeof o.createdAt=="string"&&(r=o.createdAt)}let i={version:1,id:this.projectId,name:this.projectName,remote:this.folder,createdAt:r,updatedAt:t};await this.provider.put(e,JSON.stringify(i,null,2)+`
|
|
7
|
+
`,"application/json")}async ensureRegistered(){return this.registryPromise||(this.registryPromise=this.registerProject()),this.registryPromise}key(e){if(e=ir(e),e===this.sourcePrefix)return this.targetPrefix;if(e.startsWith(`${this.sourcePrefix}/`))return this.targetPrefix+e.slice(this.sourcePrefix.length);if(e===this.targetPrefix||e.startsWith(`${this.targetPrefix}/`))return e;if(e.startsWith("projects/"))throw new Error(["Cross-project storage access denied.",`Current project: ${this.targetPrefix}`,`Requested key: ${e}`].join(" "));return e}async put(e,t,r){return await this.ensureRegistered(),this.provider.put(this.key(e),t,r)}async get(e){return await this.ensureRegistered(),this.provider.get(this.key(e))}async getText(e){return await this.ensureRegistered(),this.provider.getText(this.key(e))}async delete(e){return await this.ensureRegistered(),this.provider.delete(this.key(e))}async exists(e){return await this.ensureRegistered(),this.provider.exists(this.key(e))}async list(e){return await this.ensureRegistered(),this.provider.list(this.key(e))}};var De=class{constructor(e){this.storage=e}storage;key(e){return["projects",e,"graph","resolution","current.json"].join("/")}async load(e){let t=await this.storage.getText(this.key(e));return t?JSON.parse(t):null}async save(e){await this.storage.put(this.key(e.projectId),JSON.stringify(e),"application/json")}};var Fe=class{constructor(e){this.storage=e}storage;base(e){return["projects",e,"code","architecture"].join("/")}async load(e){let t=await this.storage.getText(`${this.base(e)}/current.json`);return t?JSON.parse(t):null}async save(e){let t=this.base(e.projectId),r=s=>JSON.stringify({version:e.version,projectId:e.projectId,updatedAt:e.updatedAt,data:s},null,2);await Promise.all([this.storage.put(`${t}/current.json`,JSON.stringify(e,null,2),"application/json"),this.storage.put(`${t}/entry-points.json`,r(e.entryPoints),"application/json"),this.storage.put(`${t}/hotspots.json`,r(e.hotspots),"application/json"),this.storage.put(`${t}/layers.json`,r(e.layers),"application/json"),this.storage.put(`${t}/clusters.json`,r(e.clusters),"application/json")])}};var $e=class{constructor(e){this.storage=e}storage;base(e){return["projects",e,"code","analysis"].join("/")}async load(e){let t=await this.storage.getText(`${this.base(e)}/current.json`);return t?JSON.parse(t):null}async save(e){let t=this.base(e.projectId),r=s=>JSON.stringify({version:e.version,projectId:e.projectId,updatedAt:e.updatedAt,data:s},null,2);await Promise.all([this.storage.put(`${t}/current.json`,JSON.stringify(e,null,2),"application/json"),this.storage.put(`${t}/dead-code.json`,r(e.deadCode),"application/json"),this.storage.put(`${t}/dependencies.json`,r(e.dependencies),"application/json")])}};var Ge=class{constructor(e){this.storage=e}storage;key(e){return["projects",e,"code","visualization","graph.json"].join("/")}async load(e){let t=await this.storage.getText(this.key(e));return t?JSON.parse(t):null}async save(e){await this.storage.put(this.key(e.projectId),JSON.stringify(e,null,2),"application/json")}};async function ar(n,e={}){let t=Date.now();async function r(h,y,p){await e.onStage?.({id:h,title:y,state:"start"});let S=Date.now(),x=await p();return await e.onStage?.({id:h,title:y,state:"complete",durationMs:Date.now()-S}),x}let s=gt(),i=sr(rr({provider:s.storage.provider,huggingface:s.storage.huggingface,localRoot:s.storage.localRoot}),{attempts:3}),o=new je(i,n.id,n.name,n.remote??n.name),a=new Z(o),d=await r("source-index","Source Index",async()=>{let h=await new Se().index(n.id,n.rootPath,{onProgress:y=>{e.onSourceProgress?.(y)}});return await a.save({version:1,projectId:n.id,updatedAt:new Date().toISOString(),files:h.files,symbols:h.graph.allSymbols(n.id),edges:h.graph.allEdges(n.id)}),h}),c=d.graph,l=await r("type-resolution","Type Resolution",async()=>{let h=await new we(c).resolveProject(n.id,n.rootPath,y=>{e.onStageProgress?.({stage:"type-resolution",current:y.current,total:y.total,phase:y.phase,detail:y.detail})});return await new De(o).save(h),h});await r("rich-graph","Rich Graph",async()=>{let h=new Pe(c).enrich(n.id,n.rootPath,l,S=>{e.onStageProgress?.({stage:"rich-graph",current:S.current,total:S.total,phase:S.phase,detail:S.detail})}),y=c.allSymbols(n.id),p=c.allEdges(n.id);return await a.save({version:1,projectId:n.id,updatedAt:new Date().toISOString(),files:y.filter(S=>S.type==="file").length,symbols:y,edges:p}),h});let m=await r("semantic-index","Local Code Search \u2014 SQLite FTS5/BM25",async()=>new ve({projectId:n.id,rootPath:n.rootPath,storage:o,graph:c}).initialize(y=>{e.onStageProgress?.({stage:"semantic-index",current:y.current,total:y.total,phase:y.phase,detail:y.detail})})),u=await r("architecture","Architecture Intelligence",async()=>{let h=new Ae(c).analyze(n.id);return await new Fe(o).save(h),h}),f=await r("analysis","Graph Analysis",async()=>{let h=new Le(c).analyze(n.id);return await new $e(o).save(h),h}),g=await r("visualization","3D Visualization Dataset",async()=>{let h=new Oe(c).build(n.id,u,f,y=>{e.onStageProgress?.({stage:"visualization",current:y.current,total:y.total,phase:y.phase,detail:y.detail})});return await new Ge(o).save(h),h});return{files:d.files,project:{id:n.id,name:n.name,remote:n.remote??n.name},storage:o.name,durationMs:Date.now()-t,graph:{symbols:c.allSymbols(n.id).length,edges:c.allEdges(n.id).length},resolution:{total:l.total,exact:l.exact,high:l.high,fallback:l.fallback},semantic:m,architecture:u.summary,analysis:f.summary,visualization:g.summary}}import{Worker as Ds}from"worker_threads";import*as ze from"path";import{fileURLToPath as Fs}from"node:url";function Ke(){if(process.argv.includes("--no-color"))return!1;if(process.argv.includes("--color"))return!0;let n=process.env.NO_COLOR;if(n!==void 0&&n!=="")return!1;let e=process.env.FORCE_COLOR;if(e!==void 0&&e!=="")return e!=="0"&&e.toLowerCase()!=="false";if(process.stdout.isTTY===!0&&process.env.TERM!=="dumb")return!0;let t=process.env.CI;return t!==void 0&&t!==""}var Os={spinner:["\xB7","\u2722","\u2733","\u2736","\u273B","\u273D"],barFilled:"\u2588",barEmpty:"\u2591",phaseDone:"\u25C6",phaseActive:"\u25C7",rail:"\u2502",branch:"\u251C",corner:"\u2514",dash:"\u2014"},_s={spinner:[".","*","+","x","*","+"],barFilled:"#",barEmpty:"-",phaseDone:"[OK]",phaseActive:"[>>]",rail:"|",branch:"|",corner:"\\",dash:"-"};function js(){if(process.env.TOOLNET_ASCII==="1")return!1;if(process.env.TOOLNET_UNICODE==="1")return!0;if(process.platform==="win32"){let n=process.env.TERM_PROGRAM,e=process.env.WT_SESSION,t=process.env.VSCODE_PID,r=process.env.ConEmuPID;return!!(e||t||r||n==="vscode")}return process.env.TERM!=="linux"}var ct;function U(){return ct||(ct=js()?Os:_s),ct}var lr={scan:"Scanning files",parse:"Parsing code","type-resolution":"Type Resolution","rich-graph":"Rich Graph","semantic-index":"Semantic Code Index",architecture:"Architecture Intelligence","graph-analysis":"Graph Analysis",visualization:"3D Visualization Dataset"};function dr(){if(process.env.TOOLNET_STANDALONE==="1"||process.stdout.isTTY!==!0)return cr();let n=Ke(),e=U(),t=n?"\x1B[2m":"",r=n?"\x1B[32m":"",s=n?"\x1B[0m":"",i="",o="",a=-1,d=0,c=()=>{if(!o)return;let g="";a>=0?g=` ${e.dash} done`:d>0&&(g=` ${e.dash} ${d.toLocaleString()} found`),process.stdout.write(`\r\x1B[K${t}${e.branch}${s} ${r}${e.phaseDone}${s} ${o}${g}
|
|
8
|
+
`),o="",a=-1,d=0},l=ze.dirname(Fs(import.meta.url)),m=l.includes("/bundle/")||l.includes("\\bundle\\"),u=ze.join(l,m?"shimmer-worker.js":"shimmer-worker.ts"),f=new Ds(u,{workerData:{startTime:Date.now(),colors:n}});return{onProgress(g){let h=lr[g.stage]||g.stage;g.stage!==i&&i&&c(),i=g.stage,o=h;let y=-1,p=0;if(g.total>0&&g.current>=0){let S=g.current/g.total*100;y=g.current>=g.total?100:Math.min(99,Math.round(S))}else g.current>0&&(p=g.current);a=y,d=p,f.postMessage({type:"update",phase:g.stage,phaseName:h,percent:y,count:p})},stop(){return new Promise(g=>{let h=!1,y=()=>{h||(h=!0,c(),g())},p=setTimeout(()=>{f.terminate().then(y)},2e3);f.on("message",S=>{S.type==="stopped"&&(clearTimeout(p),f.terminate().then(y))}),f.postMessage({type:"stop"})})}}}function cr(){let n="";return{onProgress(e){if(e.stage===n)return;n=e.stage;let t=lr[e.stage]||e.stage;process.stdout.write(`${t}...
|
|
9
|
+
`)},stop(){return Promise.resolve()}}}var R={reset:"\x1B[0m",green:"\x1B[32m",gray:"\x1B[90m",white:"\x1B[97m",cyan:"\x1B[36m",red:"\x1B[31m"};function D(n){return new Intl.NumberFormat("en-US").format(n)}function lt(n){return n<1e3?`${n}ms`:`${(n/1e3).toFixed(1)}s`}function ur(n,e,t){return Math.max(e,Math.min(t,n))}function $s(n,e){return e<=0?"":n.length<=e?n:e<=3?n.slice(0,e):`${n.slice(0,e-1)}\u2026`}var Be=class{stream;interactive;enabled;colorEnabled;rootPath;stages;shimmerProgress;startedAt=Date.now();scanTotal;parseCurrent=0;parseTotal=0;finished=!1;lastPlainParseBucket=-1;constructor(e){this.stream=e.stream??process.stdout,this.enabled=e.enabled??!0;let t=e.interactive??this.stream.isTTY===!0;this.interactive=t&&process.env.TERM!=="dumb",this.colorEnabled=this.interactive&&Ke(),this.rootPath=e.rootPath,this.stages=e.stages.map(r=>({...r,state:"pending"}))}start(){if(!this.enabled)return;if(this.startedAt=Date.now(),!this.interactive){this.stream.write(`\u25C7 Initializing ToolNet Index
|
|
9
10
|
`),this.stream.write(`\u25C7 Initialized in ${this.rootPath}
|
|
10
|
-
`);return}let e=
|
|
11
|
-
`);let t=Math.max(16,this.terminalWidth()-18);this.writeStatic(`${this.color(e.phaseDone,
|
|
11
|
+
`);return}let e=U();this.writeStatic(this.color("\u25C7 Initializing ToolNet Index",R.white)),this.stream.write(`
|
|
12
|
+
`);let t=Math.max(16,this.terminalWidth()-18);this.writeStatic(`${this.color(e.phaseDone,R.green)} Initialized in ${$s(this.rootPath,t)}`),this.writeStatic(this.color(e.rail,R.gray)),this.shimmerProgress=dr()}scanningComplete(e){if(this.scanTotal=e,this.parseTotal=e,!!this.enabled){if(!this.interactive){this.stream.write(`\u25C6 Scanning files \u2014 ${D(e)} found
|
|
12
13
|
`);return}this.shimmerProgress&&this.shimmerProgress.onProgress({stage:"scan",current:e,total:0})}}parsingProgress(e,t){if(this.parseCurrent=e,this.parseTotal=t,!this.enabled)return;let r=t>0?Math.floor(e/t*100):100;if(!this.interactive){let s=r>=100?100:Math.floor(r/25)*25;s!==this.lastPlainParseBucket&&(this.lastPlainParseBucket=s,this.stream.write(`\u2192 Parsing code \u2014 ${r}%
|
|
13
14
|
`));return}this.shimmerProgress&&this.shimmerProgress.onProgress({stage:"parse",current:e,total:t})}startStage(e,t){let r=this.stages.find(o=>o.id===e);if(r&&(r.state="active",r.title=t,r.startedAt=Date.now()),!this.enabled||e==="source-index")return;if(!this.interactive){this.stream.write(`\u2192 ${t}
|
|
14
15
|
`);return}let i={"type-resolution":"type-resolution","rich-graph":"rich-graph","semantic-index":"semantic-index",architecture:"architecture",analysis:"graph-analysis",visualization:"visualization"}[e]||e;this.shimmerProgress&&this.shimmerProgress.onProgress({stage:i,current:0,total:0})}stageProgress(e){!this.enabled||!this.interactive||!this.shimmerProgress||this.shimmerProgress.onProgress({stage:e.stage,current:e.current,total:e.total})}completeStage(e,t,r=0){let s=this.stages.find(o=>o.id===e);if(s&&(s.state="done",s.title=t,s.durationMs=r),!this.enabled)return;if(!this.interactive){e==="source-index"?this.stream.write(`\u2713 Parsing code \u2014 done
|
|
15
|
-
`):this.stream.write(`\u2713 ${t} (${
|
|
16
|
-
`);return}let i=
|
|
17
|
-
`);return}let r=["",`\u25C6 Indexed ${
|
|
18
|
-
`))}fail(e){this.finished=!0,this.enabled&&(this.shimmerProgress&&this.interactive?this.shimmerProgress.stop().then(()=>{this.printFailMessage(e)}):this.printFailMessage(e))}printFailMessage(e){if(this.interactive){this.stream.write(`${this.color("\u2717",
|
|
16
|
+
`):this.stream.write(`\u2713 ${t} (${lt(r)})
|
|
17
|
+
`);return}let i=U();this.writeStatic(`${this.color(i.rail,R.gray)} ${this.color(i.phaseDone,R.green)} ${t} ${this.color(i.dash,R.gray)} done`)}async finish(e){this.finished=!0,this.enabled&&(this.shimmerProgress&&this.interactive&&await this.shimmerProgress.stop(),this.printFinishSummary(e))}printFinishSummary(e){let t=U();if(this.interactive){this.writeStatic(this.color(t.rail,R.gray)),this.writeStatic(`${this.color(t.phaseDone,R.green)} Indexed ${D(e.files)} files`),this.writeStatic(this.color(t.rail,R.gray)),this.writeStatic(`${this.color(t.phaseActive,R.cyan)} ${D(e.symbols)} symbols, ${D(e.edges)} edges in ${lt(e.durationMs)}`),this.writeStatic(`${this.color(t.phaseActive,R.cyan)} Storage: ${e.storage}`),this.writeStatic(this.color(t.rail,R.gray)),this.writeStatic(`${this.color(t.corner,R.gray)} ${this.color(t.phaseDone,R.green)} Done`),this.stream.write(`
|
|
18
|
+
`);return}let r=["",`\u25C6 Indexed ${D(e.files)} files`,"",`\u25C7 ${D(e.symbols)} symbols, ${D(e.edges)} edges in ${lt(e.durationMs)}`,`\u25C7 Storage: ${e.storage}`,"","Done",""];this.stream.write(r.join(`
|
|
19
|
+
`))}fail(e){this.finished=!0,this.enabled&&(this.shimmerProgress&&this.interactive?this.shimmerProgress.stop().then(()=>{this.printFailMessage(e)}):this.printFailMessage(e))}printFailMessage(e){if(this.interactive){this.stream.write(`${this.color("\u2717",R.red)} ${e}
|
|
19
20
|
`);return}this.stream.write(`\u2717 ${e}
|
|
20
|
-
`)}terminalWidth(){let e=this.stream.columns;if(typeof e=="number"&&Number.isFinite(e))return
|
|
21
|
-
`)}};import{createConnection as
|
|
22
|
-
`)}),o.on("data",
|
|
23
|
-
`);if(
|
|
24
|
-
`,{encoding:"utf8",mode:384})}var
|
|
21
|
+
`)}terminalWidth(){let e=this.stream.columns;if(typeof e=="number"&&Number.isFinite(e))return ur(Math.floor(e),32,240);let t=process.stdout.columns;return typeof t=="number"&&Number.isFinite(t)?ur(Math.floor(t),32,240):80}color(e,t){return this.colorEnabled?`${t}${e}${R.reset}`:e}writeStatic(e){this.stream.write(`${e}
|
|
22
|
+
`)}};import{createConnection as zs}from"node:net";import{homedir as Gs}from"node:os";import{join as Ks}from"node:path";function pr(){return process.env.TOOLNET_SERVICE_SOCKET??Ks(Gs(),".toolnet","run","service.sock")}function Bs(n,e={}){let t=e.socketPath??pr(),r=e.timeoutMs??1e3;return new Promise((s,i)=>{let o=zs({path:t}),a="",d=!1,c=m=>{d||(d=!0,clearTimeout(l),o.destroy(),m())},l=setTimeout(()=>{c(()=>{i(new Error(`ToolNet service timeout after ${r}ms`))})},r);o.setEncoding("utf8"),o.once("connect",()=>{o.write(`${JSON.stringify(n)}
|
|
23
|
+
`)}),o.on("data",m=>{a+=m;let u=a.indexOf(`
|
|
24
|
+
`);if(u===-1)return;let f=a.slice(0,u).trim();c(()=>{try{s(JSON.parse(f))}catch(g){i(g instanceof Error?g:new Error(String(g)))}})}),o.once("error",m=>{c(()=>{i(m)})}),o.once("end",()=>{d||c(()=>{i(new Error("ToolNet service closed without response"))})})})}async function mr(n,e={}){try{let t=await Bs({type:"invalidate",project:n},{timeoutMs:250,...e});return t.ok&&t.type==="invalidate"}catch{return!1}}var H=new fe().detect(),ft=mt(H.rootPath,".toolnet"),dt=mt(ft,"index.lock"),qs=mt(ft,"last-index.json"),ut=process.argv.includes("--json"),Js=[{id:"source-index",title:"Source Index"},{id:"type-resolution",title:"Resolving refs"},{id:"rich-graph",title:"Building rich graph"},{id:"semantic-index",title:"Local code search \u2014 FTS5/BM25"},{id:"architecture",title:"Architecture intelligence"},{id:"analysis",title:"Graph analysis"},{id:"visualization",title:"Visualization dataset"}],N=new Be({rootPath:H.rootPath,stages:Js,enabled:!ut});Hs(ft,{recursive:!0});function Ue(n){Vs(qs,JSON.stringify(n,null,2)+`
|
|
25
|
+
`,{encoding:"utf8",mode:384})}var pt;try{pt=Ws(dt,"wx",384)}catch{console.error("\u274C Index already running for this project."),console.error(`Lock: ${dt}`),process.exit(2)}var He=new Date().toISOString(),le=[];try{N.start(),Ue({version:2,state:"running",startedAt:He,completedStages:le,project:H});let n=await ar(H,{onSourceProgress:e=>{if(e.phase==="scan"){N.scanningComplete(e.total);return}N.parsingProgress(e.current,e.total)},onStage:async e=>{e.state==="start"?N.startStage(e.id,e.title):(le.push(e.id),N.completeStage(e.id,e.title,e.durationMs??0)),Ue({version:2,state:"running",startedAt:He,currentStage:e.id,completedStages:[...le],project:H})},onStageProgress:e=>{N.stageProgress({stage:e.stage,current:e.current,total:e.total,phase:e.phase,detail:e.detail})}});Ue({version:2,state:"complete",startedAt:He,finishedAt:new Date().toISOString(),completedStages:le,result:n}),await mr(H),ut?console.log(JSON.stringify(n,null,2)):await N.finish({files:n.files,symbols:n.graph.symbols,edges:n.graph.edges,durationMs:n.durationMs,storage:n.storage})}catch(n){let e=n instanceof Error?n.message:String(n);Ue({version:2,state:"failed",startedAt:He,failedAt:new Date().toISOString(),completedStages:le,error:e}),N.fail(`Full index failed: ${e}`),ut&&console.error(e),process.exitCode=1}finally{pt!==void 0&&Us(pt),Ys(dt,{force:!0})}
|