toolnet-memory 0.3.12 → 0.3.13

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.
@@ -1,25 +1,24 @@
1
- import{closeSync as Gs,mkdirSync as Ks,openSync as Us,rmSync as Bs,writeFileSync as zs}from"node:fs";import{join as it}from"node:path";import{existsSync as Gt,readFileSync as Kt}from"node:fs";import{homedir as Ut}from"node:os";import{join as Bt}from"node:path";function zt(s){let e=s.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 Ht(){let s=process.env.TOOLNET_GLOBAL_ENV??Bt(Ut(),".config","toolnet-memory",".env");if(!Gt(s))return;let e=Kt(s,"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 n=r.indexOf("=");if(n<=0)continue;let i=r.slice(0,n).trim();/^[A-Za-z_][A-Za-z0-9_]*$/.test(i)&&process.env[i]===void 0&&(process.env[i]=zt(r.slice(n+1)))}}Ht();function H(s,e){return s===void 0?e:["1","true","yes","on"].includes(s.toLowerCase())}function V(s,e){if(!s)return e;let t=Number(s);return Number.isFinite(t)?t:e}function lt(){return{memory:{autoCapture:H(process.env.MEMORY_AUTO_CAPTURE,!0),autoRetrieve:H(process.env.MEMORY_AUTO_RETRIEVE,!0),autoSummarize:H(process.env.MEMORY_AUTO_SUMMARIZE,!0),autoSync:H(process.env.MEMORY_AUTO_SYNC,!0)},retrieval:{maxCandidates:V(process.env.MEMORY_MAX_CANDIDATES,50),rerankTop:V(process.env.MEMORY_RERANK_TOP,10),finalContext:V(process.env.MEMORY_FINAL_CONTEXT,5),tokenBudget:V(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:H(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:V(process.env.MEMORY_LOCAL_CACHE_MB,200)}}}import{createHash as Vt}from"node:crypto";import{existsSync as Be,mkdirSync as Wt,readFileSync as Yt,renameSync as qt,writeFileSync as Jt}from"node:fs";import{basename as Qt,dirname as oe,join as ae,parse as ut,resolve as W}from"node:path";var mt=".toolnet",Zt="project.json";function Xt(s){return Vt("sha256").update(s).digest("hex").slice(0,16)}function ze(s){return ae(s,mt,Zt)}function er(s){return Be(ze(s))}function tr(s,e){let t=W(s),r=ut(t).root;for(;;){if(er(t))return t;if(t===r||e&&t===W(e))break;let n=oe(t);if(n===t)break;t=n}return null}function rr(s){let e=W(s),t=ut(e).root,r=[".git","package.json","pyproject.toml","Cargo.toml","go.mod","composer.json"];for(;;){if(r.some(i=>Be(ae(e,i))))return e;if(e===t)break;let n=oe(e);if(n===e)break;e=n}return W(s)}function sr(s){let e;try{e=JSON.parse(Yt(s,"utf8"))}catch(n){throw new Error(`Invalid ToolNet project manifest: ${s}: ${n instanceof Error?n.message:String(n)}`)}if(!e||typeof e!="object")throw new Error(`Invalid ToolNet project manifest: ${s}`);let t=e;if(typeof t.id!="string"||!t.id.trim())throw new Error(`ToolNet project manifest is missing id: ${s}`);if(typeof t.name!="string"||!t.name.trim())throw new Error(`ToolNet project manifest is missing name: ${s}`);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:oe(oe(s)),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 dt(s,e){let t=ae(s,mt);Wt(t,{recursive:!0});let r=ze(s),n=`${r}.tmp-${process.pid}`;Jt(n,JSON.stringify(e,null,2)+`
3
- `,{encoding:"utf8",mode:384}),qt(n,r)}function pt(s,e){return{id:s.id,name:s.name,remote:s.remote,rootPath:e,createdAt:s.createdAt,updatedAt:s.updatedAt,graphVersion:s.graphVersion,memoryVersion:s.memoryVersion,metadata:s.metadata}}var ie=class{detect(e=process.cwd()){let t=W(e),r=rr(t),i=[".git","package.json","pyproject.toml","Cargo.toml","go.mod","composer.json"].some(l=>Be(ae(r,l))),o=tr(t,i?r:void 0);if(o){let l=ze(o),m=sr(l);return m.rootPath!==o&&(m.rootPath=o,m.updatedAt=new Date().toISOString(),dt(o,m)),pt(m,o)}let a=new Date().toISOString(),d=Qt(r),c={version:1,id:Xt(r),name:d,remote:d,rootPath:r,createdAt:a,updatedAt:a,graphVersion:0,memoryVersion:0};return dt(r,c),pt(c,r)}};var nr=[{type:"openai_key",regex:/\bsk-[A-Za-z0-9_\-]{20,}\b/g},{type:"huggingface_token",regex:/\bhf_[A-Za-z0-9]{20,}\b/g},{type:"hf_s3_access_key",regex:/\bHFAK[A-Za-z0-9]{8,}\b/g},{type:"bearer_token",regex:/\bBearer\s+[A-Za-z0-9._\-]{16,}\b/gi},{type:"jwt",regex:/\beyJ[A-Za-z0-9_\-]{8,}\.[A-Za-z0-9_\-]{8,}\.[A-Za-z0-9_\-]{8,}\b/g},{type:"private_key",regex:/-----BEGIN (?:RSA |EC |OPENSSH )?PRIVATE KEY-----[\s\S]*?-----END (?:RSA |EC |OPENSSH )?PRIVATE KEY-----/g},{type:"password_assignment",regex:/\b(password|passwd|pwd)\s*[:=]\s*["']?[^"'\s]{6,}["']?/gi},{type:"secret_assignment",regex:/\b(secret|api[_-]?key|access[_-]?token|refresh[_-]?token)\s*[:=]\s*["']?[^"'\s]{8,}["']?/gi},{type:"cookie",regex:/\b(cookie|set-cookie)\s*[:=]\s*[^;\n]{8,}/gi}],ce=class{scan(e){let t=[];for(let r of nr){let n=new RegExp(r.regex.source,r.regex.flags);for(let i of e.matchAll(n))t.push({type:r.type,value:i[0]})}return t}hasSecrets(e){return this.scan(e).length>0}};var Y=class{scanner=new ce;sanitize(e){let t=e,r=this.scanner.scan(e),n=new Set;for(let i of r)n.add(i.type),t=t.split(i.value).join(`[REDACTED:${i.type}]`);return{text:t,redacted:r.length,secretTypes:[...n]}}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,n]of Object.entries(e)){let i=r.normalize("NFKC").toLowerCase().replace(/[^a-z0-9]+/g,"");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]":t[r]=this.sanitizeValue(n)}return t}return e}};var ft=[{id:"openai-compatible",label:"OpenAI-compatible",requiresApiKey:!0,requiresBaseUrl:!0,transport:"openai-compatible"},{id:"alibaba",label:"Alibaba / DashScope",requiresApiKey:!0,requiresBaseUrl:!0,transport:"openai-compatible"},{id:"openrouter",label:"OpenRouter",defaultBaseUrl:"https://openrouter.ai/api/v1",requiresApiKey:!0,transport:"openai-compatible"},{id:"groq",label:"Groq",defaultBaseUrl:"https://api.groq.com/openai/v1",requiresApiKey:!0,transport:"openai-compatible"},{id:"deepseek",label:"DeepSeek",defaultBaseUrl:"https://api.deepseek.com",defaultModel:"deepseek-v4-flash",requiresApiKey:!0,transport:"openai-compatible"},{id:"nvidia",label:"NVIDIA NIM",defaultBaseUrl:"https://integrate.api.nvidia.com/v1",defaultModel:"deepseek-ai/deepseek-v4-pro",requiresApiKey:!0,transport:"openai-compatible"},{id:"gemini",label:"Gemini",defaultBaseUrl:"https://generativelanguage.googleapis.com/v1beta",requiresApiKey:!0,transport:"gemini"},{id:"huggingface",label:"Hugging Face",defaultBaseUrl:"https://router.huggingface.co/v1",requiresApiKey:!0,transport:"openai-compatible"},{id:"ollama",label:"Ollama / Local",defaultBaseUrl:"http://127.0.0.1:11434/v1",requiresApiKey:!1,transport:"openai-compatible"},{id:"custom",label:"Custom endpoint",requiresApiKey:!1,requiresBaseUrl:!0,transport:"openai-compatible"},{id:"cloudflare",label:"Cloudflare Workers AI",requiresApiKey:!0,requiresAccountId:!0,transport:"cloudflare"}];function le(s){let e=ft.find(t=>t.id===s);if(!e)throw new Error(`Unsupported AI provider: ${s}`);return e}function de(s){return ft.some(e=>e.id===s)}function y(s){return process.env[s]?.trim()||void 0}function C(...s){return s.find(e=>!!e?.trim())}function gt(){if(y("GROQ_API_KEY"))return"groq";if(y("DEEPSEEK_API_KEY"))return"deepseek";if(y("NVIDIA_API_KEY")||y("NVIDIA_NIM_API_KEY"))return"nvidia";if(y("OPENROUTER_API_KEY"))return"openrouter";if(y("ALIBABA_API_KEY")||y("DASHSCOPE_API_KEY"))return"alibaba";if(y("GEMINI_API_KEY")||y("GOOGLE_API_KEY"))return"gemini";if(y("CLOUDFLARE_API_TOKEN")&&y("CLOUDFLARE_ACCOUNT_ID"))return"cloudflare";if(y("HF_TOKEN"))return"huggingface";if(y("OLLAMA_MODEL")||y("OLLAMA_BASE_URL"))return"ollama"}function or(){let s=y("TOOLNET_LLM_PROVIDER");return s&&de(s)?s:gt()??"openai-compatible"}function He(s){let e=le(s);switch(s){case"alibaba":return{provider:s,apiKey:C(y("ALIBABA_API_KEY"),y("DASHSCOPE_API_KEY")),baseUrl:C(y("ALIBABA_BASE_URL"),y("DASHSCOPE_BASE_URL"),e.defaultBaseUrl),model:C(y("ALIBABA_MODEL"),y("DASHSCOPE_MODEL"))};case"openrouter":return{provider:s,apiKey:y("OPENROUTER_API_KEY"),baseUrl:C(y("OPENROUTER_BASE_URL"),e.defaultBaseUrl),model:y("OPENROUTER_MODEL")};case"groq":return{provider:s,apiKey:y("GROQ_API_KEY"),baseUrl:C(y("GROQ_BASE_URL"),e.defaultBaseUrl),model:y("GROQ_MODEL")};case"deepseek":return{provider:s,apiKey:y("DEEPSEEK_API_KEY"),baseUrl:C(y("DEEPSEEK_BASE_URL"),e.defaultBaseUrl),model:C(y("DEEPSEEK_MODEL"),e.defaultModel)};case"nvidia":return{provider:s,apiKey:C(y("NVIDIA_API_KEY"),y("NVIDIA_NIM_API_KEY")),baseUrl:C(y("NVIDIA_BASE_URL"),y("NVIDIA_NIM_BASE_URL"),e.defaultBaseUrl),model:C(y("NVIDIA_MODEL"),y("NVIDIA_NIM_MODEL"),e.defaultModel)};case"gemini":return{provider:s,apiKey:C(y("GEMINI_API_KEY"),y("GOOGLE_API_KEY")),baseUrl:C(y("GEMINI_BASE_URL"),e.defaultBaseUrl),model:y("GEMINI_MODEL")};case"huggingface":return{provider:s,apiKey:y("HF_TOKEN"),baseUrl:C(y("HF_INFERENCE_BASE_URL"),e.defaultBaseUrl),model:C(y("HF_LLM_MODEL"),y("HF_MODEL"))};case"ollama":return{provider:s,apiKey:y("OLLAMA_API_KEY"),baseUrl:C(y("OLLAMA_BASE_URL"),e.defaultBaseUrl),model:y("OLLAMA_MODEL")};case"cloudflare":return{provider:s,accountId:y("CLOUDFLARE_ACCOUNT_ID"),apiKey:y("CLOUDFLARE_API_TOKEN"),baseUrl:y("CLOUDFLARE_AI_BASE_URL"),model:y("CLOUDFLARE_MODEL")};case"custom":return{provider:s,apiKey:y("CUSTOM_AI_API_KEY"),baseUrl:y("CUSTOM_AI_BASE_URL"),model:y("CUSTOM_AI_MODEL")};default:return{provider:"openai-compatible",apiKey:C(y("OPENAI_API_KEY"),y("MODEL_API_KEY")),baseUrl:C(y("OPENAI_BASE_URL"),y("MODEL_BASE_URL")),model:C(y("OPENAI_MODEL"),y("MODEL_NAME"))}}}function ir(){let s=y("TOOLNET_LLM_PROVIDER"),e=s&&de(s)?s:or(),t=le(e),r=He(e);return{provider:e,apiKey:C(y("TOOLNET_LLM_API_KEY"),r.apiKey),baseUrl:C(y("TOOLNET_LLM_BASE_URL"),r.baseUrl,t.defaultBaseUrl),model:C(y("TOOLNET_LLM_MODEL"),r.model,t.defaultModel),accountId:C(y("TOOLNET_LLM_ACCOUNT_ID"),r.accountId)}}function ar(){let s=y("TOOLNET_EMBEDDING_PROVIDER");return s==="local"?"local":s&&de(s)?s:y("HF_TOKEN")||y("HF_EMBEDDING_MODEL")?"huggingface":"local"}function cr(){let s=ar();if(s==="local")return{provider:"local",model:C(y("TOOLNET_EMBEDDING_MODEL"),y("LOCAL_EMBEDDING_MODEL"))};let e=le(s),t,r,n,i;switch(s){case"huggingface":t=y("HF_TOKEN"),r=y("HF_INFERENCE_BASE_URL"),n=y("HF_EMBEDDING_MODEL");break;case"openai-compatible":t=y("OPENAI_API_KEY"),r=y("OPENAI_BASE_URL"),n=y("OPENAI_EMBEDDING_MODEL");break;case"cloudflare":t=y("CLOUDFLARE_API_TOKEN"),r=y("CLOUDFLARE_AI_BASE_URL"),n=y("CLOUDFLARE_EMBEDDING_MODEL"),i=y("CLOUDFLARE_ACCOUNT_ID");break;default:t=He(s).apiKey,r=He(s).baseUrl,n=y(`${s.toUpperCase().replace(/-/g,"_")}_EMBEDDING_MODEL`)}let o=C(y("TOOLNET_EMBEDDING_ACCOUNT_ID"),i),a=C(y("TOOLNET_EMBEDDING_BASE_URL"),r,e.defaultBaseUrl),d=s==="cloudflare"&&!a&&o?`https://api.cloudflare.com/client/v4/accounts/${o}/ai/v1`:a;return{provider:s,apiKey:C(y("TOOLNET_EMBEDDING_API_KEY"),t),baseUrl:d,model:C(y("TOOLNET_EMBEDDING_MODEL"),n),accountId:o}}function ht(){let s=ir(),e=cr();return{llm:s,embedding:e,legacy:{llm:!y("TOOLNET_LLM_PROVIDER")&&!!gt(),embedding:!y("TOOLNET_EMBEDDING_PROVIDER")&&!!(y("HF_TOKEN")||y("HF_EMBEDDING_MODEL"))}}}var pe=class{constructor(e){this.options=e}options;name="gemini";async embed(e){let t=this.options.model.replace(/^models\//,""),n=`${this.options.baseUrl?.replace(/\/+$/,"")||"https://generativelanguage.googleapis.com/v1beta"}/models/${encodeURIComponent(t)}:embedContent?key=${encodeURIComponent(this.options.apiKey)}`,i=await fetch(n,{method:"POST",headers:{"content-type":"application/json"},body:JSON.stringify({model:`models/${t}`,content:{parts:[{text:e}]}})});if(!i.ok)throw new Error(`Gemini embedding failed: ${i.status} ${await i.text()}`);let a=(await i.json()).embedding?.values;if(!Array.isArray(a)||a.length===0)throw new Error("Gemini embedding returned invalid response");return a}async embedMany(e){let t=[];for(let r of e)t.push(await this.embed(r));return t}};import{createHash as lr}from"node:crypto";var ue=class{name="hash-fallback";dimensions=128;async embed(e){let t=new Array(this.dimensions).fill(0),r=e.toLowerCase().split(/\s+/).filter(Boolean);for(let n of r){let o=lr("sha256").update(n).digest().readUInt32BE(0)%this.dimensions;t[o]+=1}return dr(t)}async embedMany(e){return Promise.all(e.map(t=>this.embed(t)))}};function dr(s){let e=Math.sqrt(s.reduce((t,r)=>t+r*r,0));return e===0?s:s.map(t=>t/e)}var me=class{constructor(e){this.options=e;this.dimensions=e.dimensions}options;name="openai-compatible";dimensions;async embed(e){let[t]=await this.embedMany([e]);return t}async embedMany(e){if(e.length===0)return[];let t=this.options.baseUrl.replace(/\/+$/,""),r=await fetch(`${t}/embeddings`,{method:"POST",headers:{"content-type":"application/json",...this.options.apiKey?{authorization:`Bearer ${this.options.apiKey}`}:{}},body:JSON.stringify({model:this.options.model,input:e})});if(!r.ok)throw new Error(`Embedding failed: ${r.status} ${await r.text()}`);let i=(await r.json()).data?.sort((o,a)=>(o.index??0)-(a.index??0)).map(o=>o.embedding);if(!i||i.length!==e.length||i.some(o=>!Array.isArray(o)))throw new Error("Embedding provider returned invalid response");return i}};var fe=class{constructor(e){this.options=e}options;name="huggingface";dimensions=384;async embed(e){let[t]=await this.embedMany([e]);return t}async embedMany(e){if(e.length===0)return[];let t=`https://router.huggingface.co/hf-inference/models/${this.options.model}/pipeline/feature-extraction`,r=await fetch(t,{method:"POST",headers:{Authorization:`Bearer ${this.options.token}`,"Content-Type":"application/json"},body:JSON.stringify({inputs:e,options:{wait_for_model:!0}})});if(!r.ok)throw new Error(`HF embedding failed: ${r.status} ${await r.text()}`);let n=await r.json();if(!Array.isArray(n))throw new Error("HF embedding returned invalid response");return n.map(i=>pr(i))}};function pr(s){if(!Array.isArray(s))throw new Error("Invalid embedding");if(s.length>0&&typeof s[0]=="number")return s;if(s.length>0&&Array.isArray(s[0])){let e=s;if(e.length===0)return[];let t=e[0].length,r=new Array(t).fill(0);for(let n of e)for(let i=0;i<t;i++)r[i]+=n[i]??0;for(let n=0;n<t;n++)r[n]/=e.length;return r}throw new Error("Unsupported embedding shape")}var Ve=class{constructor(e,t=new ue){this.primary=e;this.fallback=t}primary;fallback;name="resilient";async embed(e){if(this.primary)try{return await this.primary.embed(e)}catch(t){console.warn("[embedding] primary failed, fallback enabled:",t instanceof Error?t.message:t)}return this.fallback.embed(e)}async embedMany(e){if(this.primary)try{return await this.primary.embedMany(e)}catch(t){console.warn("[embedding] primary failed, fallback enabled:",t instanceof Error?t.message:t)}return this.fallback.embedMany(e)}};function ur(s){return`https://api.cloudflare.com/client/v4/accounts/${s}/ai/v1`}function mr(){let s=ht().embedding;if(s.provider==="local")return null;if(s.provider==="huggingface")return!s.apiKey||!s.model?null:new fe({token:s.apiKey,model:s.model});if(s.provider==="gemini")return!s.apiKey||!s.model?null:new pe({apiKey:s.apiKey,model:s.model,baseUrl:s.baseUrl});let e=s.baseUrl;return s.provider==="cloudflare"&&!e&&s.accountId&&(e=ur(s.accountId)),e&&s.model?new me({apiKey:s.apiKey,baseUrl:e,model:s.model}):null}function yt(){return new Ve(mr())}import{readdir as yr}from"node:fs/promises";import{extname as br,join as Sr,relative as Er}from"node:path";import{basename as fr}from"node:path";var gr=new Set([".env",".env.local",".env.production",".env.development","id_rsa","id_ed25519","credentials","credentials.json","service-account.json"]),hr=new Set([".pem",".key",".p12",".pfx"]);function bt(s){let e=fr(s).toLowerCase();if(gr.has(e))return!0;for(let t of hr)if(e.endsWith(t))return!0;return!1}var vr=new Set([".ts",".tsx",".js",".jsx",".mjs",".cjs",".mts",".cts"]),xr=new Set(["node_modules",".git","dist","build","coverage",".next",".cache",".toolnet-memory"]);async function We(s){let e=[];async function t(r){let n=await yr(r,{withFileTypes:!0});for(let i of n){if(xr.has(i.name))continue;let o=Sr(r,i.name);if(i.isDirectory()){await t(o);continue}vr.has(br(i.name))&&(bt(o)||e.push(Er(s,o)))}}return await t(s),e.sort()}import{readFile as wr}from"node:fs/promises";import{join as Pr}from"node:path";import{createHash as Cr}from"node:crypto";import*as w from"typescript";function St(s,e){return Cr("sha256").update(`${s}:${e}`).digest("hex").slice(0,24)}function Ye(s,e){return s.getLineAndCharacterOfPosition(e.getStart(s)).line+1}async function qe(s,e,t){let r=Pr(e,t),n=await wr(r,"utf8"),i=w.createSourceFile(t,n,w.ScriptTarget.Latest,!0,t.endsWith(".tsx")||t.endsWith(".jsx")?w.ScriptKind.TSX:w.ScriptKind.TS),o=[],a=[],d=[],c=[],l={id:St(s,`file:${t}`),projectId:s,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,E,x){let A=St(s,`${t}:${E}:${x??S}:${p.pos}`);return o.push({id:A,projectId:s,name:S,qualifiedName:x??S,type:E,filePath:t,startLine:Ye(i,p),endLine:i.getLineAndCharacterOfPosition(p.end).line+1}),A}function g(p){if(!w.isStringLiteral(p.moduleSpecifier))return;let S=[],E=p.importClause;if(E?.name&&S.push({localName:E.name.text,importedName:"default",kind:"default"}),E?.namedBindings&&w.isNamedImports(E.namedBindings))for(let x of E.namedBindings.elements)S.push({localName:x.name.text,importedName:x.propertyName?.text??x.name.text,kind:"named"});E?.namedBindings&&w.isNamespaceImport(E.namedBindings)&&S.push({localName:E.namedBindings.name.text,importedName:"*",kind:"namespace"}),a.push({source:p.moduleSpecifier.text,bindings:S})}function h(p,S){for(let E of p.heritageClauses??[])for(let x of E.types){let A=x.expression.getText(i).split(".").at(-1)??"";A&&c.push({fromId:S,targetName:A,type:E.token===w.SyntaxKind.ExtendsKeyword?"INHERITS":"IMPLEMENTS"})}}function b(p){let S=!1,E=!1;if(w.isImportDeclaration(p)&&g(p),w.isInterfaceDeclaration(p)&&f(p,p.name.text,"interface",p.name.text),w.isClassDeclaration(p)&&p.name){let x=f(p,p.name.text,"class",p.name.text);h(p,x),u.push({id:x,name:p.name.text}),E=!0}if(w.isFunctionDeclaration(p)&&p.name){let x=f(p,p.name.text,"function",p.name.text);m.push(x),S=!0}if(w.isMethodDeclaration(p)&&p.name){let x=p.name.getText(i),A=u.at(-1)?.name,k=f(p,x,"method",A?`${A}.${x}`:x);m.push(k),S=!0}if(w.isCallExpression(p)&&(w.isIdentifier(p.expression)&&d.push({callerId:m.at(-1),calleeName:p.expression.text,line:Ye(i,p)}),w.isPropertyAccessExpression(p.expression))){let x=p.expression;d.push({callerId:m.at(-1),qualifier:x.expression.getText(i),calleeName:x.name.text,line:Ye(i,p)})}w.forEachChild(p,b),S&&m.pop(),E&&u.pop()}return b(i),{filePath:t,symbols:o,imports:a,calls:d,heritage:c}}import{createHash as Ar}from"node:crypto";import{dirname as Ir,extname as Tr,normalize as Rr,posix as kr}from"node:path";var q=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 Lr(s,e,t,r){return Ar("sha256").update(`${s}:${e}:${t}:${r}`).digest("hex").slice(0,24)}function N(s){return Rr(s).replaceAll("\\","/")}function Je(s,e,t){if(!e.startsWith("."))return;let r=N(kr.join(Ir(s),e)),n=Tr(r),i;if(n===".js"){let o=r.slice(0,-3);i=[`${o}.ts`,`${o}.tsx`,`${o}.js`,`${o}.jsx`]}else if(n===".mjs"){let o=r.slice(0,-4);i=[`${o}.mts`,`${o}.ts`,`${o}.mjs`]}else if(n===".cjs"){let o=r.slice(0,-4);i=[`${o}.cts`,`${o}.ts`,`${o}.cjs`]}else n?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.js`,`${r}/index.jsx`];return i.find(o=>t.has(N(o)))}var J=class{build(e,t){let r=new q,n=new Map;for(let o of t)for(let a of o.symbols)r.addSymbol(a),a.type==="file"&&n.set(N(o.filePath),a);let i=new Set(n.keys());for(let o of t){let a=N(o.filePath),d=n.get(a);if(d){for(let c of o.symbols)c.id!==d.id&&r.addEdge(this.edge(e,d.id,"DEFINES",c.id));for(let c of o.imports){let l=Je(a,c.source,i);if(!l)continue;let m=n.get(l);m&&r.addEdge(this.edge(e,d.id,"IMPORTS",m.id,{source:c.source}))}for(let c of o.heritage){let l=r.findByName(e,c.targetName).filter(m=>m.type==="class"||m.type==="interface");for(let m of l)r.addEdge(this.edge(e,c.fromId,c.type,m.id))}for(let c of o.calls){if(!c.callerId)continue;let l=this.resolveCallTargets(e,o,c.calleeName,c.qualifier,t,r,i);for(let m of l)m.id!==c.callerId&&r.addEdge(this.edge(e,c.callerId,"CALLS",m.id,{line:c.line,qualifier:c.qualifier}))}}}return r}resolveCallTargets(e,t,r,n,i,o,a){let d=N(t.filePath),c=o.findByName(e,r).filter(l=>N(l.filePath)===d);if(!n&&c.length)return c;if(n)for(let l of t.imports){if(!l.bindings.find(f=>f.kind==="namespace"&&f.localName===n))continue;let u=Je(d,l.source,a);if(u)return o.findByName(e,r).filter(f=>N(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,a);if(!u)continue;let f=m.importedName==="default"?r:m.importedName,g=o.findByName(e,f).filter(h=>N(h.filePath)===u);if(g.length)return g}return o.findByName(e,r).filter(l=>l.type==="function"||l.type==="method")}edge(e,t,r,n,i){return{id:Lr(e,t,r,n),projectId:e,from:t,to:n,type:r,metadata:i}}};var ge=class{async index(e,t,r={}){let n=await We(t);r.onProgress?.({phase:"scan",current:n.length,total:n.length});let i=[],o=0;for(let d of n)try{i.push(await qe(e,t,d))}catch(c){console.warn(`[indexer] skipped ${d}:`,c instanceof Error?c.message:c)}finally{o+=1,r.onProgress?.({phase:"parse",current:o,total:n.length,file:d})}let a=new J().build(e,i);return{files:i.length,symbols:a.allSymbols(e).length,edges:a.allEdges(e).length,graph:a}}};import{execFile as Mr}from"node:child_process";import{promisify as Nr}from"node:util";var Ro=Nr(Mr);var Q=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{createHash as _r}from"node:crypto";import{readFile as Or}from"node:fs/promises";import{join as jr}from"node:path";function Et(s){return _r("sha256").update(s).digest("hex")}var he=class{sanitizer=new Y;async build(e,t,r,n={}){let i=n.maxLines??80,o=n.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 Or(jr(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:n,symbol:i,maxLines:o}=e,a=Math.min(e.overlapLines,Math.max(0,o-1)),d=Math.max(1,e.startLine),c=Math.min(n.length,Math.max(d,e.endLine)),l=[],m=d,u=0;for(;m<=c;){let f=Math.min(c,m+o-1),g=n.slice(m-1,f).join(`
4
- `).trim();if(g){let h=this.sanitizer.sanitize(g).text,b=Et(h),p=[t,r,i?.id??"file",String(u),b].join(":");l.push({id:Et(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:b})}if(f>=c)break;m=f-a+1,u++}return l}};var ye=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")}};var be=class{constructor(e){this.storage=e}storage;key(e){return["projects",e,"code","vectors","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 Se=class{records=new Map;upsert(e){this.records.set(e.id,e)}get(e){return this.records.get(e)}has(e){return this.records.has(e)}remove(e){return this.records.delete(e)}clearProject(e){let t=0;for(let[r,n]of this.records)n.projectId===e&&(this.records.delete(r),t++);return t}exportProject(e){return[...this.records.values()].filter(t=>t.projectId===e)}importRecords(e){let t=0;for(let r of e)!r?.id||!r?.projectId||!Array.isArray(r.vector)||(this.records.set(r.id,r),t++);return t}search(e,t,r=10){let n=[];for(let i of this.records.values()){if(i.projectId!==e)continue;let o=Dr(t,i.vector);n.push({id:i.id,score:o,metadata:i.metadata})}return n.sort((i,o)=>o.score-i.score).slice(0,r)}size(){return this.records.size}};function Dr(s,e){if(s.length===0||e.length===0||s.length!==e.length)return 0;let t=0,r=0,n=0;for(let i=0;i<s.length;i++){let o=s[i]??0,a=e[i]??0;t+=o*a,r+=o*o,n+=a*a}return r===0||n===0?0:t/(Math.sqrt(r)*Math.sqrt(n))}function Qe(s){return s.toLowerCase().normalize("NFKC").replace(/[^\p{L}\p{N}_\-./]+/gu," ").split(/\s+/).map(e=>e.trim()).filter(e=>e.length>1)}function vt(s,e){let t=s.toLowerCase().replaceAll("\\","/"),r=e.toLowerCase(),n=/\b(test|tests|spec|fixture|mock)\b/.test(r);return(t.startsWith("tests/")||t.includes("/tests/")||t.includes("__tests__")||/\.(test|spec)\.[^.]+$/.test(t))&&!n?.35:t.includes("/generated/")||t.includes(".generated.")?.25:1}var Ee=class{constructor(e){this.options=e}options;vectors=new Se;chunks=new Map;async initialize(e){let{projectId:t,rootPath:r,storage:n,embeddings:i,graph:o,model:a}=this.options,d=new ye(n),c=new be(n),l=await new he().build(t,r,o.allSymbols(t));this.chunks.clear();for(let I of l)this.chunks.set(I.id,I);let m=await d.load(t),u=!m||m.chunks.length!==l.length||m.chunks.some(I=>!this.chunks.has(I.id));u&&await d.save({version:1,projectId:t,updatedAt:new Date().toISOString(),chunks:l});let f=await c.load(t),g=0;f&&f.model===a&&(g=this.vectors.importRecords(f.records));let h=new Set(l.map(I=>I.id)),b=0;for(let I of this.vectors.exportProject(t))h.has(I.id)||this.vectors.remove(I.id)&&b++;let p=l.filter(I=>{let $=this.vectors.get(I.id);return $?$.metadata?.contentHash!==I.contentHash:!0}),S=0,E=8,x=this.vectors.exportProject(t)[0]?.vector.length,A=Math.ceil(p.length/E),k=0;for(let I=0;I<p.length;I+=E){let $=p.slice(I,I+E),$t=$.map(L=>[`file: ${L.filePath}`,L.symbolName?`symbol: ${L.symbolName}`:"",L.symbolType?`type: ${L.symbolType}`:"",L.content].filter(Boolean).join(`
5
- `)),Ft=await i.embedMany($t);for(let L=0;L<$.length;L++){let M=$[L],F=Ft[L];if(!(!M||!F||F.length===0)){if(x===void 0&&(x=F.length),F.length!==x){console.warn(`[code-semantic] skip dimension mismatch: ${F.length} != ${x}`);continue}this.vectors.upsert({id:M.id,projectId:t,vector:F,metadata:{contentHash:M.contentHash,filePath:M.filePath,symbolName:M.symbolName,symbolType:M.symbolType,startLine:M.startLine,endLine:M.endLine}}),S++}}k++,e?.({current:k,total:A,phase:"embedding"})}let O=this.vectors.exportProject(t),ct=S>0||b>0||!f||f.model!==a;return ct&&(e?.({current:A,total:A,phase:"saving"}),await c.save({version:1,projectId:t,model:a,dimensions:O[0]?.vector.length??x??0,updatedAt:new Date().toISOString(),records:O})),{chunks:l.length,vectorsLoaded:g,vectorsIndexed:S,vectorsRemoved:b,totalVectors:O.length,savedChunks:u,savedVectors:ct}}async search(e,t=8){let r=await this.options.embeddings.embed(e),n=this.vectors.search(this.options.projectId,r,Math.max(t*4,20)),i=new Set(Qe(e)),o=[];for(let a of n){let d=this.chunks.get(a.id);if(!d)continue;let c=new Set(Qe([d.filePath,d.symbolName??"",d.symbolType??"",d.content].join(" "))),l=0;for(let h of i)c.has(h)&&l++;let m=i.size===0?0:l/i.size,u=Math.max(0,a.score),f=vt(d.filePath,e),g=(u*.75+m*.25)*f;o.push({chunk:d,score:g,vectorScore:u,lexicalScore:m})}return o.sort((a,d)=>d.score-a.score).slice(0,t)}size(){return this.chunks.size}};import{createHash as $r}from"node:crypto";import{dirname as Fr,relative as Z,resolve as X}from"node:path";import*as P from"typescript";function j(s){return s.replaceAll("\\","/").replace(/^\.\//,"")}function xt(s){return $r("sha256").update(s).digest("hex").slice(0,24)}function ee(s,e){return s.getLineAndCharacterOfPosition(e.getStart(s)).line+1}function Gr(s){let e=s;if(e.name&&P.isIdentifier(e.name))return e.name.text}var ve=class{constructor(e){this.graph=e}graph;async resolveProject(e,t,r){let n=this.loadConfig(t),i=P.createProgram({rootNames:n.fileNames,options:n.options}),o=i.getTypeChecker(),a=[],d=i.getSourceFiles().filter(l=>{if(l.isDeclarationFile)return!1;let m=X(l.fileName),u=j(Z(t,m));return!(u.startsWith("../")||u.includes("node_modules/"))}),c=0;for(let l of d){let m=X(l.fileName),u=j(Z(t,m)),f=g=>{if(P.isCallExpression(g)){let h=this.resolveCall({projectId:e,rootPath:t,source:l,node:g,checker:o});h&&a.push(h)}P.isClassDeclaration(g)&&a.push(...this.resolveHeritage({projectId:e,rootPath:t,source:l,node:g,checker:o})),P.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:n,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:n,sourceNode:i,declaration:a,expression:i.expression.getText(n),kind:"CALL"}):this.fallbackCall(t,r,n,i)}resolveHeritage(e){let{projectId:t,rootPath:r,source:n,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:n,sourceNode:c,declaration:m,expression:c.getText(n),kind:d.token===P.SyntaxKind.ExtendsKeyword?"EXTENDS":"IMPLEMENTS"});u&&a.push(u)}return a}fromDeclaration(e){let t=e.declaration.getSourceFile();if(t.isDeclarationFile)return null;let r=j(Z(e.rootPath,X(t.fileName)));if(r.startsWith("../")||r.includes("node_modules/"))return null;let n=ee(t,e.declaration),i=Gr(e.declaration)??e.expression.split(".").at(-1),o=this.findGraphSymbol(e.projectId,r,n,i),a=j(Z(e.rootPath,X(e.source.fileName)));return{id:xt([e.projectId,a,ee(e.source,e.sourceNode),e.kind,r,n].join(":")),projectId:e.projectId,kind:e.kind,sourceFile:a,sourceLine:ee(e.source,e.sourceNode),expression:e.expression,targetFile:r,targetLine:n,targetName:i,targetQualifiedName:o?.qualifiedName,targetSymbolId:o?.id,confidence:o?"exact":"high",resolver:"typescript-checker"}}fallbackCall(e,t,r,n){let i=n.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=j(Z(t,X(r.fileName)));return{id:xt(`${e}:${d}:${ee(r,n)}:${a.id}`),projectId:e,kind:"CALL",sourceFile:d,sourceLine:ee(r,n),expression:n.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,n){let o=this.graph.allSymbols(e).filter(a=>j(a.filePath)===j(t)).filter(a=>typeof a.startLine=="number"&&typeof a.endLine=="number"&&a.startLine<=r&&a.endLine>=r);if(n){let a=o.find(d=>d.name===n||(d.qualifiedName?.endsWith(`.${n}`)??!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=P.findConfigFile(e,P.sys.fileExists,"tsconfig.json");if(t){let n=P.readConfigFile(t,P.sys.readFile);if(n.error)throw new Error(P.flattenDiagnosticMessageText(n.error.messageText,`
6
- `));return P.parseJsonConfigFileContent(n.config,P.sys,Fr(t))}return{fileNames:P.sys.readDirectory(e,[".ts",".tsx",".js",".jsx",".mts",".cts",".mjs",".cjs"],["node_modules",".git","dist","build","coverage"]),options:{allowJs:!0,checkJs:!1,target:P.ScriptTarget.ES2022,module:P.ModuleKind.NodeNext,moduleResolution:P.ModuleResolutionKind.NodeNext,skipLibCheck:!0,noEmit:!0}}}};import{createHash as Kr}from"node:crypto";import{dirname as Ur,relative as G,resolve as K}from"node:path";import*as v from"typescript";function R(s){return s.replaceAll("\\","/").replace(/^\.\//,"")}function Ze(...s){return Kr("sha256").update(s.join(":")).digest("hex").slice(0,24)}function te(s,e){return s.getLineAndCharacterOfPosition(e.getStart(s)).line+1}var Br=new Set(["get","post","put","patch","delete","options","head"]),zr=new Set([v.SyntaxKind.EqualsToken,v.SyntaxKind.PlusEqualsToken,v.SyntaxKind.MinusEqualsToken,v.SyntaxKind.AsteriskEqualsToken,v.SyntaxKind.SlashEqualsToken,v.SyntaxKind.PercentEqualsToken,v.SyntaxKind.QuestionQuestionEqualsToken,v.SyntaxKind.AmpersandAmpersandEqualsToken,v.SyntaxKind.BarBarEqualsToken]),xe=class{constructor(e){this.graph=e}graph;enrich(e,t,r,n){let i={routes:0,tests:0,usesType:0,writes:0,callReferences:0,properties:0},o=this.loadConfig(t),a=v.createProgram({rootNames:o.fileNames,options:o.options}),d=a.getTypeChecker(),c=a.getSourceFiles().filter(m=>{if(m.isDeclarationFile)return!1;let u=R(G(t,K(m.fileName)));return!(u.startsWith("../")||u.includes("node_modules/"))}),l=0;for(let m of c){let u=R(G(t,K(m.fileName))),f=this.fileNode(e,u);if(!f)continue;let g=h=>{if(v.isCallExpression(h)&&this.addRoute(e,t,m,f,h,d)&&i.routes++,v.isTypeReferenceNode(h)&&this.addTypeUse(e,t,m,h,d)&&i.usesType++,v.isBinaryExpression(h)&&zr.has(h.operatorToken.kind)){let b=this.addWrite(e,t,m,h.left,d);b.added&&(i.writes++,b.propertyCreated&&i.properties++)}if((v.isPrefixUnaryExpression(h)||v.isPostfixUnaryExpression(h))&&(h.operator===v.SyntaxKind.PlusPlusToken||h.operator===v.SyntaxKind.MinusMinusToken)){let b=this.addWrite(e,t,m,h.operand,d);b.added&&(i.writes++,b.propertyCreated&&i.properties++)}v.forEachChild(h,g)};g(m),l++,n?.({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,n,i,o){if(!v.isPropertyAccessExpression(i.expression))return!1;let a=i.expression.name.text.toLowerCase();if(!Br.has(a))return!1;let d=i.arguments[0];if(!d||!(v.isStringLiteral(d)||v.isNoSubstitutionTemplateLiteral(d)))return!1;let c=d.text;if(!c.startsWith("/"))return!1;let l=te(r,i),m=Ze(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:n.filePath,startLine:l,endLine:l}),this.graph.addEdge(this.edge(e,n.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,n,i){let o=this.resolveNodeSymbol(e,t,n.typeName,i);if(!o)return!1;let a=this.containingSymbol(e,R(G(t,K(r.fileName))),te(r,n));return!a||a.id===o.id?!1:(this.graph.addEdge(this.edge(e,a.id,"USES_TYPE",o.id)),!0)}addWrite(e,t,r,n,i){let o=this.resolveTsSymbol(n,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=R(G(t,K(d.fileName)));if(c.startsWith("../")||c.includes("node_modules/"))return{added:!1,propertyCreated:!1};let l=te(d,a),m=this.findSymbolAt(e,c,l),u=!1;if(!m||m.type==="file"){let h=o?.getName()??n.getText(r),b=Ze(e,c,"property",h,l);if(m=this.graph.getSymbol(b),!m){m={id:b,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=R(G(t,K(r.fileName))),g=this.containingSymbol(e,f,te(r,n));return!g||g.id===m.id?{added:!1,propertyCreated:u}:(this.graph.addEdge(this.edge(e,g.id,"WRITES",m.id,{expression:n.getText(r)})),{added:!0,propertyCreated:u})}addTestEdges(e){let t=this.graph.allEdges(e),r=0;for(let n of t){if(n.type!=="IMPORTS")continue;let i=this.graph.getSymbol(n.from),o=this.graph.getSymbol(n.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 n of t.resolutions){if(n.kind!=="CALL"||!n.targetSymbolId)continue;let i=this.graph.getSymbol(n.targetSymbolId);if(!i)continue;let o=this.containingSymbol(e,n.sourceFile,n.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:n.expression,confidence:n.confidence,resolver:n.resolver})),r++)}return r}resolveNodeSymbol(e,t,r,n){let i=this.resolveTsSymbol(r,n),o=i?.valueDeclaration??i?.declarations?.[0];if(!o)return;let a=o.getSourceFile();if(a.isDeclarationFile)return;let d=R(G(t,K(a.fileName)));if(!(d.startsWith("../")||d.includes("node_modules/")))return this.findSymbolAt(e,d,te(a,o),i?.getName())}resolveTsSymbol(e,t){let r=t.getSymbolAtLocation(e);if(r&&r.flags&v.SymbolFlags.Alias)try{r=t.getAliasedSymbol(r)}catch{}return r}containingSymbol(e,t,r){return this.graph.allSymbols(e).filter(n=>R(n.filePath)===R(t)&&typeof n.startLine=="number"&&typeof n.endLine=="number"&&n.startLine<=r&&n.endLine>=r).sort((n,i)=>(n.endLine??0)-(n.startLine??0)-((i.endLine??0)-(i.startLine??0)))[0]}findSymbolAt(e,t,r,n){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(n){let o=i.find(a=>a.name===n||(a.qualifiedName?.endsWith(`.${n}`)??!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,n,i){return{id:Ze(e,t,r,n,JSON.stringify(i??{})),projectId:e,from:t,to:n,type:r,metadata:i}}loadConfig(e){let t=v.findConfigFile(e,v.sys.fileExists,"tsconfig.json");if(t){let r=v.readConfigFile(t,v.sys.readFile);return v.parseJsonConfigFileContent(r.config,v.sys,Ur(t))}return{fileNames:v.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:v.ScriptTarget.ES2022,module:v.ModuleKind.NodeNext,moduleResolution:v.ModuleResolutionKind.NodeNext}}}};function Hr(s){return s.replaceAll("\\","/").toLowerCase()}var U=class{constructor(e){this.graph=e}graph;detect(e){let t=this.graph.allSymbols(e),r=this.graph.allEdges(e),n=[];for(let o of t){if(o.type!=="route"&&o.type!=="file")continue;let a=this.classify(o,r);a&&n.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 n){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=Hr(e.filePath),n=[];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,n.push("bin executable");else if(/(^|\/)(cli)\.[^.]+$/.test(r))i="cli",o=90,n.push("CLI entry file");else if(/(^|\/)(main|server)\.[^.]+$/.test(r))i="main",o=90,n.push("main/server file");else if(r.includes("/runtime/")&&/(^|\/)(bootstrap|index)\.[^.]+$/.test(r))i="runtime",o=75,n.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,n.push("package/public index"))}return i?(t.filter(d=>d.to===e.id&&d.type!=="DEFINES").length===0&&(o+=10,n.push("no incoming dependency")),{kind:i,score:o,reasons:n}):null}};var Vr={CALL_REFERENCE:6,CALLS:4,IMPORTS:3,USES_TYPE:4,WRITES:5,ROUTE:4,TESTS:1,INHERITS:5,IMPLEMENTS:5,DEFINES:.25},we=class{constructor(e){this.graph=e}graph;analyze(e){let t=this.graph.allSymbols(e),r=this.graph.allEdges(e),n=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=n.get(c.from),m=n.get(c.to);if(!l||!m)continue;let u=Vr[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 Wr(s){return s.replaceAll("\\","/").toLowerCase()}var Yr={CALL_REFERENCE:6,CALLS:4,IMPORTS:4,USES_TYPE:4,WRITES:5,INHERITS:6,IMPLEMENTS:6,ROUTE:3,TESTS:1},Pe=class{constructor(e){this.graph=e}graph;detect(e){let t=this.graph.allSymbols(e),r=this.graph.allEdges(e),n=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=n.get(f.from),h=n.get(f.to);if(!g||!h||g.filePath===h.filePath)continue;let b=null;if(g.filePath===c?b=h.filePath:h.filePath===c&&(b=g.filePath),!b)continue;let p=o.get(b);if(!p||p.layer==="unknown"||p.layer==="tests")continue;let S=Yr[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=Wr(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 qr}from"node:crypto";var Jr={CALL_REFERENCE:7,CALLS:3,IMPORTS:4,USES_TYPE:4,WRITES:5,INHERITS:7,IMPLEMENTS:7,ROUTE:4,TESTS:1};function Qr(s){return qr("sha256").update(s).digest("hex").slice(0,12)}function Ce(s){return s.replaceAll("\\","/")}function Zr(s){let t=Ce(s).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 Xr(s){let e=s.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 Ae=class{constructor(e){this.graph=e}graph;detect(e){let t=this.graph.allSymbols(e),r=this.graph.allEdges(e),n=new Map(t.map(u=>[u.id,u])),i=[...new Set(t.map(u=>Ce(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=n.get(u.from),g=n.get(u.to);if(!f||!g)continue;let h=Ce(f.filePath),b=Ce(g.filePath);if(h===b)continue;let p=Jr[u.type]??0;if(p<=0)continue;let S=a(h,b),E=o.get(S);E?E.weight+=p:o.set(S,{a:[h,b].sort()[0],b:[h,b].sort()[1],weight:p})}let d=new Map;for(let u of i){let f=Zr(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 E of f)h.set(E,new Set);for(let E of o.values())!g.has(E.a)||!g.has(E.b)||E.weight>=8&&(h.get(E.a)?.add(E.b),h.get(E.b)?.add(E.a));let b=new Set,p=[];for(let E of f){if(b.has(E))continue;let x=[E],A=[];for(b.add(E);x.length>0;){let k=x.shift();A.push(k);for(let O of h.get(k)??[])b.has(O)||(b.add(O),x.push(O))}p.push(A.sort())}let S=[];for(let E of p)E.length>=2?c.push({subsystem:u,files:E}):S.push(...E);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 b=0;b<l.length;b++){if(g===b)continue;let p=l[b];if(!p)continue;let S=0,E=new Set(h.files),x=new Set(p.files);for(let A of o.values())(E.has(A.a)&&x.has(A.b)||E.has(A.b)&&x.has(A.a))&&(S+=A.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 x of o.values()){let A=f.has(x.a),k=f.has(x.b);A&&k?g+=x.weight:(A||k)&&(h+=x.weight)}let b=g+h,p=u.subsystem.replace(/\/residual$/,""),S=Xr(p),E=u.subsystem.endsWith("/residual")?`${S} Misc`:S;m.push({id:`cluster-${Qr(`${u.subsystem}:${u.files.join("|")}`)}`,label:E,subsystem:p,files:u.files,size:u.files.length,internalWeight:g,externalWeight:h,cohesion:b>0?Number((g/b).toFixed(4)):0})}return m.sort((u,f)=>f.size-u.size||f.cohesion-u.cohesion||u.label.localeCompare(f.label))}};var Ie=class{constructor(e){this.graph=e;this.entryPoints=new U(e),this.hotspots=new we(e),this.layers=new Pe(e),this.clusters=new Ae(e)}graph;entryPoints;hotspots;layers;clusters;analyze(e){let t=this.entryPoints.detect(e),r=this.hotspots.analyze(e),n=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:n.length,clusters:i.length},entryPoints:t,hotspots:r,layers:n,clusters:i}}};var wt=new Set(["CALLS","CALL_REFERENCE","IMPORTS","USES_TYPE","WRITES","INHERITS","IMPLEMENTS","ROUTE","TESTS"]);function Xe(s){return s.replaceAll("\\","/").toLowerCase()}function es(s){let e=Xe(s);return e.startsWith("tests/")||e.includes("/tests/")||e.includes("__tests__")||/\.(test|spec)\.[^.]+$/.test(e)}function ts(s){let e=Xe(s.filePath);return e.startsWith("bin/")||/(^|\/)index\.[^.]+$/.test(e)||/(^|\/)(main|server|cli)\.[^.]+$/.test(e)}var Te=class{constructor(e){this.graph=e}graph;analyze(e){let t=this.graph.allSymbols(e),r=this.graph.allEdges(e),n=new Set(new U(this.graph).detect(e).map(o=>o.symbolId)),i=[];for(let o of t){if(!["function","method","class","interface","property"].includes(o.type)||es(o.filePath)||n.has(o.id)||ts(o)||r.filter(f=>f.to===o.id&&wt.has(f.type)).length>0)continue;let d=r.filter(f=>f.from===o.id&&wt.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")),Xe(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 rs=new Set(["CALLS","CALL_REFERENCE","IMPORTS","USES_TYPE","WRITES","INHERITS","IMPLEMENTS","ROUTE"]),Re=class{constructor(e){this.graph=e}graph;analyze(e){let t=this.graph.allSymbols(e),r=this.graph.allEdges(e).filter(o=>rs.has(o.type)),n=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=n.get(m.from),f=n.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 ke=class{constructor(e){this.graph=e}graph;analyze(e){let t=new Te(this.graph).analyze(e),r=new Re(this.graph).analyze(e);return{version:1,projectId:e,updatedAt:new Date().toISOString(),summary:{deadCodeCandidates:t.length,highConfidenceDeadCode:t.filter(n=>n.confidence==="high").length,files:r.length},deadCode:t,dependencies:r}}};var Le=class{constructor(e){this.graph=e}graph;build(e,t,r,n){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],E=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:E?.id,clusterLabel:E?.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)&&n?.({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)),b=[];for(let p=0;p<h.length;p++){let S=h[p];b.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)&&n?.({current:p+1,total:h.length,phase:"links"})}return{version:1,projectId:e,generatedAt:new Date().toISOString(),summary:{nodes:f.length,links:b.length,files:new Set(f.map(p=>p.filePath)).size,clusters:new Set(f.map(p=>p.cluster).filter(Boolean)).size},nodes:f,links:b}}};import{homedir as Cs}from"node:os";import{join as As}from"node:path";import{DeleteObjectCommand as ss,GetObjectCommand as ns,HeadObjectCommand as os,ListObjectsV2Command as is,PutObjectCommand as as,S3Client as cs}from"@aws-sdk/client-s3";import{getSignedUrl as ls}from"@aws-sdk/s3-request-presigner";var Me=class{name="huggingface";client;bucket;constructor(e){this.bucket=e.bucket,this.client=new cs({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 n=typeof t=="string"?Buffer.from(t,"utf8"):t;await this.client.send(new as({Bucket:this.bucket,Key:e,Body:n,ContentType:r}))}async get(e){let t=await ls(this.client,new ns({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 os({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 ss({Bucket:this.bucket,Key:e}))}async list(e=""){let t=[],r;do{let n=await this.client.send(new is({Bucket:this.bucket,Prefix:e||void 0,ContinuationToken:r}));for(let i of n.Contents??[])i.Key&&t.push({key:i.Key,size:i.Size,updatedAt:i.LastModified?.toISOString()});r=n.IsTruncated?n.NextContinuationToken:void 0}while(r);return t}};import{access as Pt,mkdir as ds,readFile as ps,readdir as us,rm as ms,stat as Ct,writeFile as fs}from"node:fs/promises";import{dirname as gs,join as hs,relative as At,resolve as ys}from"node:path";var re=class{constructor(e){this.root=e}root;name="local";path(e){let t=e.replace(/^\/+/,"");return ys(this.root,t)}async put(e,t){let r=this.path(e);await ds(gs(r),{recursive:!0}),await fs(r,t)}async get(e){try{return await ps(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 ms(this.path(e),{force:!0})}async list(e=""){let t=this.path(e),r=[];try{await Pt(t)}catch{return r}let n=async o=>{let a=await us(o,{withFileTypes:!0});for(let d of a){let c=hs(o,d.name);if(d.isDirectory()){await n(c);continue}let l=await Ct(c);r.push({key:At(this.root,c),size:l.size,updatedAt:l.mtime.toISOString()})}},i=await Ct(t);return i.isDirectory()?await n(t):r.push({key:At(this.root,t),size:i.size,updatedAt:i.mtime.toISOString()}),r}};import{DeleteObjectCommand as bs,GetObjectCommand as Ss,HeadObjectCommand as Es,ListObjectsV2Command as vs,PutObjectCommand as xs,S3Client as ws}from"@aws-sdk/client-s3";import{getSignedUrl as Ps}from"@aws-sdk/s3-request-presigner";var se=class{name;client;bucket;constructor(e){this.name=e.name??"s3",this.bucket=e.bucket,this.client=new ws({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 n=typeof t=="string"?Buffer.from(t,"utf8"):t;await this.client.send(new xs({Bucket:this.bucket,Key:e,Body:n,ContentType:r}))}async get(e){let t=await Ps(this.client,new Ss({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 Es({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 bs({Bucket:this.bucket,Key:e}))}async list(e=""){let t=[],r;do{let n=await this.client.send(new vs({Bucket:this.bucket,Prefix:e||void 0,ContinuationToken:r}));for(let i of n.Contents??[])i.Key&&t.push({key:i.Key,size:i.Size,updatedAt:i.LastModified?.toISOString()});r=n.IsTruncated?n.NextContinuationToken:void 0}while(r);return t}};function et(s,e){return console.warn(e),new re(s)}function It(s){let e=s.localRoot??As(Cs(),".toolnet-memory","storage");if(s.provider==="r2"){let t=s.r2;return t?.accountId&&t.bucket&&t.accessKeyId&&t.secretAccessKey?new se({name:"r2",endpoint:`https://${t.accountId}.r2.cloudflarestorage.com`,region:"auto",bucket:t.bucket,forcePathStyle:!0,accessKeyId:t.accessKeyId,secretAccessKey:t.secretAccessKey}):et(e,"[storage] Cloudflare R2 credentials missing. Using local fallback.")}if(s.provider==="s3"){let t=s.s3;return t?.bucket&&t.accessKeyId&&t.secretAccessKey?new se({name:"s3",endpoint:t.endpoint,region:t.region??"us-east-1",bucket:t.bucket,forcePathStyle:t.forcePathStyle??!1,accessKeyId:t.accessKeyId,secretAccessKey:t.secretAccessKey}):et(e,"[storage] S3 credentials missing. Using local fallback.")}if(s.provider==="huggingface"){let t=s.huggingface;return t?.namespace&&t.bucket&&t.accessKeyId&&t.secretAccessKey?new Me({namespace:t.namespace,bucket:t.bucket,accessKeyId:t.accessKeyId,secretAccessKey:t.secretAccessKey}):et(e,"[storage] Hugging Face credentials missing. Using local fallback.")}return new re(e)}function Is(s){return new Promise(e=>setTimeout(e,s))}async function Tt(s,e={}){let t=Math.max(1,e.attempts??3),r=e.baseDelayMs??150,n=e.maxDelayMs??2e3,i;for(let o=1;o<=t;o++)try{return await s()}catch(a){if(i=a,o>=t)break;let d=Math.min(n,r*2**(o-1)),c=Math.floor(Math.random()*Math.max(1,d*.2));await Is(d+c)}throw i}var Ts=new Set(["put","get","getText","delete","list"]);function Rt(s,e={}){return new Proxy(s,{get(t,r){let n=Reflect.get(t,r,t);return typeof n!="function"?n:Ts.has(r)?(...i)=>Tt(()=>Promise.resolve(n.apply(t,i)),e):n.bind(t)}})}function kt(s){let e=s.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 Lt(s){let e=s.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 n=e.match(/^(projects\/[^/]+\/)memories\/(.+)$/);return n?`${n[1]}memory/records/${n[2]}`:(n=e.match(/^(projects\/[^/]+\/)vectors\/(.+)$/),n?`${n[1]}memory/vectors/${n[2]}`:(n=e.match(/^(projects\/[^/]+\/)graph\/(.+)$/),n?`${n[1]}code/graph/${n[2]}`:e))}var Ne=class{constructor(e,t,r,n){this.provider=e;this.name=e.name,this.projectId=t,this.projectName=r,this.folder=kt(n??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,n=await this.provider.getText(e);if(n){let o;try{o=JSON.parse(n)}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=Lt(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 _e=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 Oe=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=n=>JSON.stringify({version:e.version,projectId:e.projectId,updatedAt:e.updatedAt,data:n},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 je=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=n=>JSON.stringify({version:e.version,projectId:e.projectId,updatedAt:e.updatedAt,data:n},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 De=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 Mt(s,e={}){let t=Date.now();async function r(h,b,p){await e.onStage?.({id:h,title:b,state:"start"});let S=Date.now(),E=await p();return await e.onStage?.({id:h,title:b,state:"complete",durationMs:Date.now()-S}),E}let n=lt(),i=Rt(It({provider:n.storage.provider,huggingface:n.storage.huggingface,localRoot:n.storage.localRoot}),{attempts:3}),o=new Ne(i,s.id,s.name,s.remote??s.name),a=new Q(o),d=await r("source-index","Source Index",async()=>{let h=await new ge().index(s.id,s.rootPath,{onProgress:b=>{e.onSourceProgress?.(b)}});return await a.save({version:1,projectId:s.id,updatedAt:new Date().toISOString(),files:h.files,symbols:h.graph.allSymbols(s.id),edges:h.graph.allEdges(s.id)}),h}),c=d.graph,l=await r("type-resolution","Type Resolution",async()=>{let h=await new ve(c).resolveProject(s.id,s.rootPath,b=>{e.onStageProgress?.({stage:"type-resolution",current:b.current,total:b.total,phase:b.phase,detail:b.detail})});return await new _e(o).save(h),h});await r("rich-graph","Rich Graph",async()=>{let h=new xe(c).enrich(s.id,s.rootPath,l,S=>{e.onStageProgress?.({stage:"rich-graph",current:S.current,total:S.total,phase:S.phase,detail:S.detail})}),b=c.allSymbols(s.id),p=c.allEdges(s.id);return await a.save({version:1,projectId:s.id,updatedAt:new Date().toISOString(),files:b.filter(S=>S.type==="file").length,symbols:b,edges:p}),h});let m=await r("semantic-index","Semantic Code Index",async()=>new Ee({projectId:s.id,rootPath:s.rootPath,model:process.env.HF_EMBEDDING_MODEL??"sentence-transformers/all-MiniLM-L6-v2",storage:o,embeddings:yt(),graph:c}).initialize(b=>{e.onStageProgress?.({stage:"semantic-index",current:b.current,total:b.total,phase:b.phase,detail:b.detail})})),u=await r("architecture","Architecture Intelligence",async()=>{let h=new Ie(c).analyze(s.id);return await new Oe(o).save(h),h}),f=await r("analysis","Graph Analysis",async()=>{let h=new ke(c).analyze(s.id);return await new je(o).save(h),h}),g=await r("visualization","3D Visualization Dataset",async()=>{let h=new Le(c).build(s.id,u,f,b=>{e.onStageProgress?.({stage:"visualization",current:b.current,total:b.total,phase:b.phase,detail:b.detail})});return await new De(o).save(h),h});return{files:d.files,project:{id:s.id,name:s.name,remote:s.remote??s.name},storage:o.name,durationMs:Date.now()-t,graph:{symbols:c.allSymbols(s.id).length,edges:c.allEdges(s.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 Ms}from"worker_threads";import*as Fe from"path";import{fileURLToPath as Ns}from"node:url";function $e(){if(process.argv.includes("--no-color"))return!1;if(process.argv.includes("--color"))return!0;let s=process.env.NO_COLOR;if(s!==void 0&&s!=="")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 Rs={spinner:["\xB7","\u2722","\u2733","\u2736","\u273B","\u273D"],barFilled:"\u2588",barEmpty:"\u2591",phaseDone:"\u25C6",phaseActive:"\u25C7",rail:"\u2502",branch:"\u251C",corner:"\u2514",dash:"\u2014"},ks={spinner:[".","*","+","x","*","+"],barFilled:"#",barEmpty:"-",phaseDone:"[OK]",phaseActive:"[>>]",rail:"|",branch:"|",corner:"\\",dash:"-"};function Ls(){if(process.env.TOOLNET_ASCII==="1")return!1;if(process.env.TOOLNET_UNICODE==="1")return!0;if(process.platform==="win32"){let s=process.env.TERM_PROGRAM,e=process.env.WT_SESSION,t=process.env.VSCODE_PID,r=process.env.ConEmuPID;return!!(e||t||r||s==="vscode")}return process.env.TERM!=="linux"}var tt;function B(){return tt||(tt=Ls()?Rs:ks),tt}var Nt={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 _t(){if(process.stdout.isTTY!==!0)return _s();let s=$e(),e=B(),t=s?"\x1B[2m":"",r=s?"\x1B[32m":"",n=s?"\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}${n} ${r}${e.phaseDone}${n} ${o}${g}
8
- `),o="",a=-1,d=0},l=Fe.dirname(Ns(import.meta.url)),m=l.includes("/bundle/")||l.includes("\\bundle\\"),u=Fe.join(l,m?"shimmer-worker.js":"shimmer-worker.ts"),f=new Ms(u,{workerData:{startTime:Date.now(),colors:s}});return{onProgress(g){let h=Nt[g.stage]||g.stage;g.stage!==i&&i&&c(),i=g.stage,o=h;let b=-1,p=0;if(g.total>0&&g.current>=0){let S=g.current/g.total*100;b=g.current>=g.total?100:Math.min(99,Math.round(S))}else g.current>0&&(p=g.current);a=b,d=p,f.postMessage({type:"update",phase:g.stage,phaseName:h,percent:b,count:p})},stop(){return new Promise(g=>{let h=!1,b=()=>{h||(h=!0,c(),g())},p=setTimeout(()=>{f.terminate().then(b)},2e3);f.on("message",S=>{S.type==="stopped"&&(clearTimeout(p),f.terminate().then(b))}),f.postMessage({type:"stop"})})}}}function _s(){let s="";return{onProgress(e){if(e.stage===s)return;s=e.stage;let t=Nt[e.stage]||e.stage;process.stdout.write(`${t}...
9
- `)},stop(){return Promise.resolve()}}}var T={reset:"\x1B[0m",green:"\x1B[32m",gray:"\x1B[90m",white:"\x1B[97m",cyan:"\x1B[36m",red:"\x1B[31m"};function D(s){return new Intl.NumberFormat("en-US").format(s)}function rt(s){return s<1e3?`${s}ms`:`${(s/1e3).toFixed(1)}s`}function Ot(s,e,t){return Math.max(e,Math.min(t,s))}function Os(s,e){return e<=0?"":s.length<=e?s:e<=3?s.slice(0,e):`${s.slice(0,e-1)}\u2026`}var Ge=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&&$e(),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
1
+ import{closeSync as as,mkdirSync as cs,openSync as ls,rmSync as ds,writeFileSync as ps}from"node:fs";import{join as Be}from"node:path";import{existsSync as ht,readFileSync as yt}from"node:fs";import{homedir as St}from"node:os";import{join as bt}from"node:path";function xt(o){let e=o.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 wt(){let o=process.env.TOOLNET_GLOBAL_ENV??bt(St(),".config","toolnet-memory",".env");if(!ht(o))return;let e=yt(o,"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 n=r.indexOf("=");if(n<=0)continue;let i=r.slice(0,n).trim();/^[A-Za-z_][A-Za-z0-9_]*$/.test(i)&&process.env[i]===void 0&&(process.env[i]=xt(r.slice(n+1)))}}wt();function _(o,e){return o===void 0?e:["1","true","yes","on"].includes(o.toLowerCase())}function D(o,e){if(!o)return e;let t=Number(o);return Number.isFinite(t)?t:e}function We(){return{memory:{autoCapture:_(process.env.MEMORY_AUTO_CAPTURE,!0),autoRetrieve:_(process.env.MEMORY_AUTO_RETRIEVE,!0),autoSummarize:_(process.env.MEMORY_AUTO_SUMMARIZE,!0),autoSync:_(process.env.MEMORY_AUTO_SYNC,!0)},retrieval:{maxCandidates:D(process.env.MEMORY_MAX_CANDIDATES,50),rerankTop:D(process.env.MEMORY_RERANK_TOP,10),finalContext:D(process.env.MEMORY_FINAL_CONTEXT,5),tokenBudget:D(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:_(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:D(process.env.MEMORY_LOCAL_CACHE_MB,200)}}}import{createHash as Ct}from"node:crypto";import{existsSync as Ie,mkdirSync as vt,readFileSync as Et,renameSync as Pt,writeFileSync as kt}from"node:fs";import{basename as Tt,dirname as Q,join as X,parse as Ye,resolve as F}from"node:path";var Je=".toolnet",Rt="project.json";function It(o){return Ct("sha256").update(o).digest("hex").slice(0,16)}function Me(o){return X(o,Je,Rt)}function Mt(o){return Ie(Me(o))}function At(o,e){let t=F(o),r=Ye(t).root;for(;;){if(Mt(t))return t;if(t===r||e&&t===F(e))break;let n=Q(t);if(n===t)break;t=n}return null}function Nt(o){let e=F(o),t=Ye(e).root,r=[".git","package.json","pyproject.toml","Cargo.toml","go.mod","composer.json"];for(;;){if(r.some(i=>Ie(X(e,i))))return e;if(e===t)break;let n=Q(e);if(n===e)break;e=n}return F(o)}function Lt(o){let e;try{e=JSON.parse(Et(o,"utf8"))}catch(n){throw new Error(`Invalid ToolNet project manifest: ${o}: ${n instanceof Error?n.message:String(n)}`)}if(!e||typeof e!="object")throw new Error(`Invalid ToolNet project manifest: ${o}`);let t=e;if(typeof t.id!="string"||!t.id.trim())throw new Error(`ToolNet project manifest is missing id: ${o}`);if(typeof t.name!="string"||!t.name.trim())throw new Error(`ToolNet project manifest is missing name: ${o}`);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:Q(Q(o)),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 He(o,e){let t=X(o,Je);vt(t,{recursive:!0});let r=Me(o),n=`${r}.tmp-${process.pid}`;kt(n,JSON.stringify(e,null,2)+`
3
+ `,{encoding:"utf8",mode:384}),Pt(n,r)}function Ve(o,e){return{id:o.id,name:o.name,remote:o.remote,rootPath:e,createdAt:o.createdAt,updatedAt:o.updatedAt,graphVersion:o.graphVersion,memoryVersion:o.memoryVersion,metadata:o.metadata}}var Z=class{detect(e=process.cwd()){let t=F(e),r=Nt(t),i=[".git","package.json","pyproject.toml","Cargo.toml","go.mod","composer.json"].some(l=>Ie(X(r,l))),s=At(t,i?r:void 0);if(s){let l=Me(s),m=Lt(l);return m.rootPath!==s&&(m.rootPath=s,m.updatedAt=new Date().toISOString(),He(s,m)),Ve(m,s)}let a=new Date().toISOString(),p=Tt(r),c={version:1,id:It(r),name:p,remote:p,rootPath:r,createdAt:a,updatedAt:a,graphVersion:0,memoryVersion:0};return He(r,c),Ve(c,r)}};var jt=[{type:"openai_key",regex:/\bsk-[A-Za-z0-9_\-]{20,}\b/g},{type:"huggingface_token",regex:/\bhf_[A-Za-z0-9]{20,}\b/g},{type:"hf_s3_access_key",regex:/\bHFAK[A-Za-z0-9]{8,}\b/g},{type:"bearer_token",regex:/\bBearer\s+[A-Za-z0-9._\-]{16,}\b/gi},{type:"jwt",regex:/\beyJ[A-Za-z0-9_\-]{8,}\.[A-Za-z0-9_\-]{8,}\.[A-Za-z0-9_\-]{8,}\b/g},{type:"private_key",regex:/-----BEGIN (?:RSA |EC |OPENSSH )?PRIVATE KEY-----[\s\S]*?-----END (?:RSA |EC |OPENSSH )?PRIVATE KEY-----/g},{type:"password_assignment",regex:/\b(password|passwd|pwd)\s*[:=]\s*["']?[^"'\s]{6,}["']?/gi},{type:"secret_assignment",regex:/\b(secret|api[_-]?key|access[_-]?token|refresh[_-]?token)\s*[:=]\s*["']?[^"'\s]{8,}["']?/gi},{type:"cookie",regex:/\b(cookie|set-cookie)\s*[:=]\s*[^;\n]{8,}/gi}],ee=class{scan(e){let t=[];for(let r of jt){let n=new RegExp(r.regex.source,r.regex.flags);for(let i of e.matchAll(n))t.push({type:r.type,value:i[0]})}return t}hasSecrets(e){return this.scan(e).length>0}};var G=class{scanner=new ee;sanitize(e){let t=e,r=this.scanner.scan(e),n=new Set;for(let i of r)n.add(i.type),t=t.split(i.value).join(`[REDACTED:${i.type}]`);return{text:t,redacted:r.length,secretTypes:[...n]}}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,n]of Object.entries(e)){let i=r.normalize("NFKC").toLowerCase().replace(/[^a-z0-9]+/g,"");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]":t[r]=this.sanitizeValue(n)}return t}return e}};import{readdir as Dt}from"node:fs/promises";import{extname as Ft,join as Gt,relative as zt}from"node:path";import{basename as Ot}from"node:path";var $t=new Set([".env",".env.local",".env.production",".env.development","id_rsa","id_ed25519","credentials","credentials.json","service-account.json"]),_t=new Set([".pem",".key",".p12",".pfx"]);function qe(o){let e=Ot(o).toLowerCase();if($t.has(e))return!0;for(let t of _t)if(e.endsWith(t))return!0;return!1}var Kt=new Set([".ts",".tsx",".js",".jsx",".mjs",".cjs",".mts",".cts"]),Bt=new Set(["node_modules",".git","dist","build","coverage",".next",".cache",".toolnet-memory"]);async function Ae(o){let e=[];async function t(r){let n=await Dt(r,{withFileTypes:!0});for(let i of n){if(Bt.has(i.name))continue;let s=Gt(r,i.name);if(i.isDirectory()){await t(s);continue}Kt.has(Ft(i.name))&&(qe(s)||e.push(zt(o,s)))}}return await t(o),e.sort()}import{readFile as Ut}from"node:fs/promises";import{join as Wt}from"node:path";import{createHash as Ht}from"node:crypto";import*as w from"typescript";function Qe(o,e){return Ht("sha256").update(`${o}:${e}`).digest("hex").slice(0,24)}function Ne(o,e){return o.getLineAndCharacterOfPosition(e.getStart(o)).line+1}async function Le(o,e,t){let r=Wt(e,t),n=await Ut(r,"utf8"),i=w.createSourceFile(t,n,w.ScriptTarget.Latest,!0,t.endsWith(".tsx")||t.endsWith(".jsx")?w.ScriptKind.TSX:w.ScriptKind.TS),s=[],a=[],p=[],c=[],l={id:Qe(o,`file:${t}`),projectId:o,name:t,qualifiedName:t,type:"file",filePath:t,startLine:1,endLine:i.getLineAndCharacterOfPosition(i.end).line+1};s.push(l);let m=[],u=[];function f(d,S,x,C){let E=Qe(o,`${t}:${x}:${C??S}:${d.pos}`);return s.push({id:E,projectId:o,name:S,qualifiedName:C??S,type:x,filePath:t,startLine:Ne(i,d),endLine:i.getLineAndCharacterOfPosition(d.end).line+1}),E}function g(d){if(!w.isStringLiteral(d.moduleSpecifier))return;let S=[],x=d.importClause;if(x?.name&&S.push({localName:x.name.text,importedName:"default",kind:"default"}),x?.namedBindings&&w.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&&w.isNamespaceImport(x.namedBindings)&&S.push({localName:x.namedBindings.name.text,importedName:"*",kind:"namespace"}),a.push({source:d.moduleSpecifier.text,bindings:S})}function h(d,S){for(let x of d.heritageClauses??[])for(let C of x.types){let E=C.expression.getText(i).split(".").at(-1)??"";E&&c.push({fromId:S,targetName:E,type:x.token===w.SyntaxKind.ExtendsKeyword?"INHERITS":"IMPLEMENTS"})}}function y(d){let S=!1,x=!1;if(w.isImportDeclaration(d)&&g(d),w.isInterfaceDeclaration(d)&&f(d,d.name.text,"interface",d.name.text),w.isClassDeclaration(d)&&d.name){let C=f(d,d.name.text,"class",d.name.text);h(d,C),u.push({id:C,name:d.name.text}),x=!0}if(w.isFunctionDeclaration(d)&&d.name){let C=f(d,d.name.text,"function",d.name.text);m.push(C),S=!0}if(w.isMethodDeclaration(d)&&d.name){let C=d.name.getText(i),E=u.at(-1)?.name,T=f(d,C,"method",E?`${E}.${C}`:C);m.push(T),S=!0}if(w.isCallExpression(d)&&(w.isIdentifier(d.expression)&&p.push({callerId:m.at(-1),calleeName:d.expression.text,line:Ne(i,d)}),w.isPropertyAccessExpression(d.expression))){let C=d.expression;p.push({callerId:m.at(-1),qualifier:C.expression.getText(i),calleeName:C.name.text,line:Ne(i,d)})}w.forEachChild(d,y),S&&m.pop(),x&&u.pop()}return y(i),{filePath:t,symbols:s,imports:a,calls:p,heritage:c}}import{createHash as Vt}from"node:crypto";import{dirname as Yt,extname as Jt,normalize as qt,posix as Qt}from"node:path";var z=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 Zt(o,e,t,r){return Vt("sha256").update(`${o}:${e}:${t}:${r}`).digest("hex").slice(0,24)}function R(o){return qt(o).replaceAll("\\","/")}function je(o,e,t){if(!e.startsWith("."))return;let r=R(Qt.join(Yt(o),e)),n=Jt(r),i;if(n===".js"){let s=r.slice(0,-3);i=[`${s}.ts`,`${s}.tsx`,`${s}.js`,`${s}.jsx`]}else if(n===".mjs"){let s=r.slice(0,-4);i=[`${s}.mts`,`${s}.ts`,`${s}.mjs`]}else if(n===".cjs"){let s=r.slice(0,-4);i=[`${s}.cts`,`${s}.ts`,`${s}.cjs`]}else n?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.js`,`${r}/index.jsx`];return i.find(s=>t.has(R(s)))}var K=class{build(e,t){let r=new z,n=new Map;for(let s of t)for(let a of s.symbols)r.addSymbol(a),a.type==="file"&&n.set(R(s.filePath),a);let i=new Set(n.keys());for(let s of t){let a=R(s.filePath),p=n.get(a);if(p){for(let c of s.symbols)c.id!==p.id&&r.addEdge(this.edge(e,p.id,"DEFINES",c.id));for(let c of s.imports){let l=je(a,c.source,i);if(!l)continue;let m=n.get(l);m&&r.addEdge(this.edge(e,p.id,"IMPORTS",m.id,{source:c.source}))}for(let c of s.heritage){let l=r.findByName(e,c.targetName).filter(m=>m.type==="class"||m.type==="interface");for(let m of l)r.addEdge(this.edge(e,c.fromId,c.type,m.id))}for(let c of s.calls){if(!c.callerId)continue;let l=this.resolveCallTargets(e,s,c.calleeName,c.qualifier,t,r,i);for(let m of l)m.id!==c.callerId&&r.addEdge(this.edge(e,c.callerId,"CALLS",m.id,{line:c.line,qualifier:c.qualifier}))}}}return r}resolveCallTargets(e,t,r,n,i,s,a){let p=R(t.filePath),c=s.findByName(e,r).filter(l=>R(l.filePath)===p);if(!n&&c.length)return c;if(n)for(let l of t.imports){if(!l.bindings.find(f=>f.kind==="namespace"&&f.localName===n))continue;let u=je(p,l.source,a);if(u)return s.findByName(e,r).filter(f=>R(f.filePath)===u)}for(let l of t.imports){let m=l.bindings.find(h=>h.localName===r);if(!m)continue;let u=je(p,l.source,a);if(!u)continue;let f=m.importedName==="default"?r:m.importedName,g=s.findByName(e,f).filter(h=>R(h.filePath)===u);if(g.length)return g}return s.findByName(e,r).filter(l=>l.type==="function"||l.type==="method")}edge(e,t,r,n,i){return{id:Zt(e,t,r,n),projectId:e,from:t,to:n,type:r,metadata:i}}};var te=class{async index(e,t,r={}){let n=await Ae(t);r.onProgress?.({phase:"scan",current:n.length,total:n.length});let i=[],s=0;for(let p of n)try{i.push(await Le(e,t,p))}catch(c){console.warn(`[indexer] skipped ${p}:`,c instanceof Error?c.message:c)}finally{s+=1,r.onProgress?.({phase:"parse",current:s,total:n.length,file:p})}let a=new K().build(e,i);return{files:i.length,symbols:a.allSymbols(e).length,edges:a.allEdges(e).length,graph:a}}};import{execFile as Xt}from"node:child_process";import{promisify as er}from"node:util";var Rn=er(Xt);var B=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{createHash as tr}from"node:crypto";import{readFile as rr}from"node:fs/promises";import{join as sr}from"node:path";function Ze(o){return tr("sha256").update(o).digest("hex")}var re=class{sanitizer=new G;async build(e,t,r,n={}){let i=n.maxLines??80,s=n.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 p=[];for(let[c,l]of a){let m;try{m=await rr(sr(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)p.push(...this.chunkRange({projectId:e,filePath:c,lines:u,symbol:g,startLine:g.startLine,endLine:g.endLine,maxLines:i,overlapLines:s}));else p.push(...this.chunkRange({projectId:e,filePath:c,lines:u,startLine:1,endLine:Math.max(1,u.length),maxLines:i,overlapLines:s}))}return p}chunkRange(e){let{projectId:t,filePath:r,lines:n,symbol:i,maxLines:s}=e,a=Math.min(e.overlapLines,Math.max(0,s-1)),p=Math.max(1,e.startLine),c=Math.min(n.length,Math.max(p,e.endLine)),l=[],m=p,u=0;for(;m<=c;){let f=Math.min(c,m+s-1),g=n.slice(m-1,f).join(`
4
+ `).trim();if(g){let h=this.sanitizer.sanitize(g).text,y=Ze(h),d=[t,r,i?.id??"file",String(u),y].join(":");l.push({id:Ze(d).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 se=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 nr}from"node:sqlite";var ne=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 nr(":memory:");chunks=new Map;get size(){return this.chunks.size}build(e){if(e.length===0)return 0;this.db.exec("DELETE FROM code_fts"),this.chunks.clear();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 n=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 s of n){let a=this.chunks.get(s.id);if(!a)continue;let p=1/(1+Math.abs(s.rank));i.push({chunk:a,score:p})}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 Xe(o,e){let t=o.toLowerCase().replaceAll("\\","/"),r=e.toLowerCase(),n=/\b(test|tests|spec|fixture|mock)\b/.test(r);return(t.startsWith("tests/")||t.includes("/tests/")||t.includes("__tests__")||/\.(test|spec)\.[^.]+$/.test(t))&&!n?.35:t.includes("/generated/")||t.includes(".generated.")?.25:1}var oe=class{constructor(e){this.options=e;this.fts=new ne(e.projectId)}options;fts;async initialize(e){let{projectId:t,rootPath:r,storage:n,graph:i}=this.options,s=new se(n),a=await new re().build(t,r,i.allSymbols(t)),p=await s.load(t),c=!p||p.chunks.length!==a.length||p.chunks.some(m=>!a.some(u=>u.id===m.id));c&&await s.save({version:1,projectId:t,updatedAt:new Date().toISOString(),chunks:a}),e?.({current:a.length,total:a.length,phase:"index"});let l=this.fts.build(a);return{chunks:a.length,vectorsLoaded:0,vectorsIndexed:l,vectorsRemoved:0,totalVectors:this.fts.size,savedChunks:c,savedVectors:c}}async search(e,t=8){let r=this.fts.search(e,t),n=[];for(let i of r){let s=i.chunk,a=Xe(s.filePath,e),p=i.score,c=0,l=p*a;n.push({chunk:s,score:l,vectorScore:c,lexicalScore:p})}return n}size(){return this.fts.size}};import{createHash as or}from"node:crypto";import{dirname as ir,relative as U,resolve as W}from"node:path";import*as v from"typescript";function M(o){return o.replaceAll("\\","/").replace(/^\.\//,"")}function et(o){return or("sha256").update(o).digest("hex").slice(0,24)}function H(o,e){return o.getLineAndCharacterOfPosition(e.getStart(o)).line+1}function ar(o){let e=o;if(e.name&&v.isIdentifier(e.name))return e.name.text}var ie=class{constructor(e){this.graph=e}graph;async resolveProject(e,t,r){let n=this.loadConfig(t),i=v.createProgram({rootNames:n.fileNames,options:n.options}),s=i.getTypeChecker(),a=[],p=i.getSourceFiles().filter(l=>{if(l.isDeclarationFile)return!1;let m=W(l.fileName),u=M(U(t,m));return!(u.startsWith("../")||u.includes("node_modules/"))}),c=0;for(let l of p){let m=W(l.fileName),u=M(U(t,m)),f=g=>{if(v.isCallExpression(g)){let h=this.resolveCall({projectId:e,rootPath:t,source:l,node:g,checker:s});h&&a.push(h)}v.isClassDeclaration(g)&&a.push(...this.resolveHeritage({projectId:e,rootPath:t,source:l,node:g,checker:s})),v.forEachChild(g,f)};f(l),c++,r?.({current:c,total:p.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:n,node:i,checker:s}=e,a;if(a=s.getResolvedSignature(i)?.declaration,!a){let c=s.getSymbolAtLocation(i.expression);a=c?.valueDeclaration??c?.declarations?.[0]}return a?this.fromDeclaration({projectId:t,rootPath:r,source:n,sourceNode:i,declaration:a,expression:i.expression.getText(n),kind:"CALL"}):this.fallbackCall(t,r,n,i)}resolveHeritage(e){let{projectId:t,rootPath:r,source:n,node:i,checker:s}=e,a=[];for(let p of i.heritageClauses??[])for(let c of p.types){let l=s.getSymbolAtLocation(c.expression),m=l?.valueDeclaration??l?.declarations?.[0];if(!m)continue;let u=this.fromDeclaration({projectId:t,rootPath:r,source:n,sourceNode:c,declaration:m,expression:c.getText(n),kind:p.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=M(U(e.rootPath,W(t.fileName)));if(r.startsWith("../")||r.includes("node_modules/"))return null;let n=H(t,e.declaration),i=ar(e.declaration)??e.expression.split(".").at(-1),s=this.findGraphSymbol(e.projectId,r,n,i),a=M(U(e.rootPath,W(e.source.fileName)));return{id:et([e.projectId,a,H(e.source,e.sourceNode),e.kind,r,n].join(":")),projectId:e.projectId,kind:e.kind,sourceFile:a,sourceLine:H(e.source,e.sourceNode),expression:e.expression,targetFile:r,targetLine:n,targetName:i,targetQualifiedName:s?.qualifiedName,targetSymbolId:s?.id,confidence:s?"exact":"high",resolver:"typescript-checker"}}fallbackCall(e,t,r,n){let i=n.expression.getText(r).split(".").at(-1);if(!i)return null;let s=this.graph.findByName(e,i).filter(c=>c.type==="function"||c.type==="method");if(s.length!==1)return null;let a=s[0],p=M(U(t,W(r.fileName)));return{id:et(`${e}:${p}:${H(r,n)}:${a.id}`),projectId:e,kind:"CALL",sourceFile:p,sourceLine:H(r,n),expression:n.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,n){let s=this.graph.allSymbols(e).filter(a=>M(a.filePath)===M(t)).filter(a=>typeof a.startLine=="number"&&typeof a.endLine=="number"&&a.startLine<=r&&a.endLine>=r);if(n){let a=s.find(p=>p.name===n||(p.qualifiedName?.endsWith(`.${n}`)??!1));if(a)return a}return s.sort((a,p)=>(a.endLine??0)-(a.startLine??0)-((p.endLine??0)-(p.startLine??0)))[0]}loadConfig(e){let t=v.findConfigFile(e,v.sys.fileExists,"tsconfig.json");if(t){let n=v.readConfigFile(t,v.sys.readFile);if(n.error)throw new Error(v.flattenDiagnosticMessageText(n.error.messageText,`
5
+ `));return v.parseJsonConfigFileContent(n.config,v.sys,ir(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 cr}from"node:crypto";import{dirname as lr,relative as N,resolve as L}from"node:path";import*as b from"typescript";function k(o){return o.replaceAll("\\","/").replace(/^\.\//,"")}function Oe(...o){return cr("sha256").update(o.join(":")).digest("hex").slice(0,24)}function V(o,e){return o.getLineAndCharacterOfPosition(e.getStart(o)).line+1}var dr=new Set(["get","post","put","patch","delete","options","head"]),pr=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]),ae=class{constructor(e){this.graph=e}graph;enrich(e,t,r,n){let i={routes:0,tests:0,usesType:0,writes:0,callReferences:0,properties:0},s=this.loadConfig(t),a=b.createProgram({rootNames:s.fileNames,options:s.options}),p=a.getTypeChecker(),c=a.getSourceFiles().filter(m=>{if(m.isDeclarationFile)return!1;let u=k(N(t,L(m.fileName)));return!(u.startsWith("../")||u.includes("node_modules/"))}),l=0;for(let m of c){let u=k(N(t,L(m.fileName))),f=this.fileNode(e,u);if(!f)continue;let g=h=>{if(b.isCallExpression(h)&&this.addRoute(e,t,m,f,h,p)&&i.routes++,b.isTypeReferenceNode(h)&&this.addTypeUse(e,t,m,h,p)&&i.usesType++,b.isBinaryExpression(h)&&pr.has(h.operatorToken.kind)){let y=this.addWrite(e,t,m,h.left,p);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,p);y.added&&(i.writes++,y.propertyCreated&&i.properties++)}b.forEachChild(h,g)};g(m),l++,n?.({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,n,i,s){if(!b.isPropertyAccessExpression(i.expression))return!1;let a=i.expression.name.text.toLowerCase();if(!dr.has(a))return!1;let p=i.arguments[0];if(!p||!(b.isStringLiteral(p)||b.isNoSubstitutionTemplateLiteral(p)))return!1;let c=p.text;if(!c.startsWith("/"))return!1;let l=V(r,i),m=Oe(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:n.filePath,startLine:l,endLine:l}),this.graph.addEdge(this.edge(e,n.id,"DEFINES",m)));let u=i.arguments.at(-1);if(u){let f=this.resolveNodeSymbol(e,t,u,s);f&&this.graph.addEdge(this.edge(e,m,"ROUTE",f.id,{method:a.toUpperCase(),path:c}))}return!0}addTypeUse(e,t,r,n,i){let s=this.resolveNodeSymbol(e,t,n.typeName,i);if(!s)return!1;let a=this.containingSymbol(e,k(N(t,L(r.fileName))),V(r,n));return!a||a.id===s.id?!1:(this.graph.addEdge(this.edge(e,a.id,"USES_TYPE",s.id)),!0)}addWrite(e,t,r,n,i){let s=this.resolveTsSymbol(n,i),a=s?.valueDeclaration??s?.declarations?.[0];if(!a)return{added:!1,propertyCreated:!1};let p=a.getSourceFile();if(p.isDeclarationFile)return{added:!1,propertyCreated:!1};let c=k(N(t,L(p.fileName)));if(c.startsWith("../")||c.includes("node_modules/"))return{added:!1,propertyCreated:!1};let l=V(p,a),m=this.findSymbolAt(e,c,l),u=!1;if(!m||m.type==="file"){let h=s?.getName()??n.getText(r),y=Oe(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 d=this.fileNode(e,c);d&&this.graph.addEdge(this.edge(e,d.id,"DEFINES",m.id)),u=!0}}if(!m)return{added:!1,propertyCreated:u};let f=k(N(t,L(r.fileName))),g=this.containingSymbol(e,f,V(r,n));return!g||g.id===m.id?{added:!1,propertyCreated:u}:(this.graph.addEdge(this.edge(e,g.id,"WRITES",m.id,{expression:n.getText(r)})),{added:!0,propertyCreated:u})}addTestEdges(e){let t=this.graph.allEdges(e),r=0;for(let n of t){if(n.type!=="IMPORTS")continue;let i=this.graph.getSymbol(n.from),s=this.graph.getSymbol(n.to);!i||!s||!this.isTestFile(i.filePath)||this.isTestFile(s.filePath)||(this.graph.addEdge(this.edge(e,i.id,"TESTS",s.id)),r++)}return r}addResolvedCalls(e,t){let r=0;for(let n of t.resolutions){if(n.kind!=="CALL"||!n.targetSymbolId)continue;let i=this.graph.getSymbol(n.targetSymbolId);if(!i)continue;let s=this.containingSymbol(e,n.sourceFile,n.sourceLine);!s||s.id===i.id||this.graph.allEdges(e).some(p=>p.type==="CALLS"&&p.from===s.id&&p.to===i.id)||(this.graph.addEdge(this.edge(e,s.id,"CALL_REFERENCE",i.id,{expression:n.expression,confidence:n.confidence,resolver:n.resolver})),r++)}return r}resolveNodeSymbol(e,t,r,n){let i=this.resolveTsSymbol(r,n),s=i?.valueDeclaration??i?.declarations?.[0];if(!s)return;let a=s.getSourceFile();if(a.isDeclarationFile)return;let p=k(N(t,L(a.fileName)));if(!(p.startsWith("../")||p.includes("node_modules/")))return this.findSymbolAt(e,p,V(a,s),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(n=>k(n.filePath)===k(t)&&typeof n.startLine=="number"&&typeof n.endLine=="number"&&n.startLine<=r&&n.endLine>=r).sort((n,i)=>(n.endLine??0)-(n.startLine??0)-((i.endLine??0)-(i.startLine??0)))[0]}findSymbolAt(e,t,r,n){let i=this.graph.allSymbols(e).filter(s=>k(s.filePath)===k(t)&&typeof s.startLine=="number"&&typeof s.endLine=="number"&&s.startLine<=r&&s.endLine>=r).sort((s,a)=>(s.endLine??0)-(s.startLine??0)-((a.endLine??0)-(a.startLine??0)));if(n){let s=i.find(a=>a.name===n||(a.qualifiedName?.endsWith(`.${n}`)??!1));if(s)return s}return i[0]}fileNode(e,t){return this.graph.allSymbols(e).find(r=>r.type==="file"&&k(r.filePath)===k(t))}isTestFile(e){let t=k(e).toLowerCase();return t.startsWith("tests/")||t.includes("/tests/")||t.includes("__tests__")||/\.(test|spec)\.[^.]+$/.test(t)}edge(e,t,r,n,i){return{id:Oe(e,t,r,n,JSON.stringify(i??{})),projectId:e,from:t,to:n,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,lr(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 ur(o){return o.replaceAll("\\","/").toLowerCase()}var j=class{constructor(e){this.graph=e}graph;detect(e){let t=this.graph.allSymbols(e),r=this.graph.allEdges(e),n=[];for(let s of t){if(s.type!=="route"&&s.type!=="file")continue;let a=this.classify(s,r);a&&n.push({symbolId:s.id,filePath:s.filePath,name:s.name,kind:a.kind,score:a.score,reasons:a.reasons})}let i=new Map;for(let s of n){let a=s.kind==="route"?s.symbolId:s.filePath,p=i.get(a);(!p||s.score>p.score)&&i.set(a,s)}return[...i.values()].sort((s,a)=>a.score-s.score||s.filePath.localeCompare(a.filePath))}classify(e,t){let r=ur(e.filePath),n=[];if(e.type==="route")return{kind:"route",score:100,reasons:["HTTP route"]};let i=null,s=0;if(r.startsWith("bin/"))i="cli",s=100,n.push("bin executable");else if(/(^|\/)(cli)\.[^.]+$/.test(r))i="cli",s=90,n.push("CLI entry file");else if(/(^|\/)(main|server)\.[^.]+$/.test(r))i="main",s=90,n.push("main/server file");else if(r.includes("/runtime/")&&/(^|\/)(bootstrap|index)\.[^.]+$/.test(r))i="runtime",s=75,n.push("runtime bootstrap");else if(/(^|\/)index\.[^.]+$/.test(r)){let p=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/")||p===0||c>0)&&(i="package",s=r.startsWith("packages/")?70:55,n.push("package/public index"))}return i?(t.filter(p=>p.to===e.id&&p.type!=="DEFINES").length===0&&(s+=10,n.push("no incoming dependency")),{kind:i,score:s,reasons:n}):null}};var mr={CALL_REFERENCE:6,CALLS:4,IMPORTS:3,USES_TYPE:4,WRITES:5,ROUTE:4,TESTS:1,INHERITS:5,IMPLEMENTS:5,DEFINES:.25},ce=class{constructor(e){this.graph=e}graph;analyze(e){let t=this.graph.allSymbols(e),r=this.graph.allEdges(e),n=new Map(t.map(c=>[c.id,c])),i=new Map,s=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)s(c.filePath).symbols++;for(let c of r){let l=n.get(c.from),m=n.get(c.to);if(!l||!m)continue;let u=mr[c.type]??1,f=s(l.filePath),g=s(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 p=Math.max(1,Math.min(30,Math.ceil(a.length*.15)));return a.slice(0,p)}};function fr(o){return o.replaceAll("\\","/").toLowerCase()}var gr={CALL_REFERENCE:6,CALLS:4,IMPORTS:4,USES_TYPE:4,WRITES:5,INHERITS:6,IMPLEMENTS:6,ROUTE:3,TESTS:1},le=class{constructor(e){this.graph=e}graph;detect(e){let t=this.graph.allSymbols(e),r=this.graph.allEdges(e),n=new Map(t.map(a=>[a.id,a])),i=[...new Set(t.map(a=>a.filePath))].sort(),s=new Map;for(let a of i)s.set(a,this.classifyPath(a));for(let a=0;a<3;a++){let p=!1;for(let c of i){let l=s.get(c);if(!l||l.layer!=="unknown")continue;let m=new Map;for(let f of r){let g=n.get(f.from),h=n.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 d=s.get(y);if(!d||d.layer==="unknown"||d.layer==="tests")continue;let S=gr[f.type]??1;m.set(d.layer,(m.get(d.layer)??0)+S)}let u=[...m.entries()].sort((f,g)=>g[1]-f[1]||f[0].localeCompare(g[0]))[0];u&&u[1]>=4&&(s.set(c,{filePath:c,layer:u[0],confidence:.65,reasons:[`graph-neighbor vote=${u[1]}`]}),p=!0)}if(!p)break}return i.map(a=>s.get(a))}classifyPath(e){let t=fr(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 hr}from"node:crypto";var yr={CALL_REFERENCE:7,CALLS:3,IMPORTS:4,USES_TYPE:4,WRITES:5,INHERITS:7,IMPLEMENTS:7,ROUTE:4,TESTS:1};function Sr(o){return hr("sha256").update(o).digest("hex").slice(0,12)}function de(o){return o.replaceAll("\\","/")}function br(o){let t=de(o).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 xr(o){let e=o.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 pe=class{constructor(e){this.graph=e}graph;detect(e){let t=this.graph.allSymbols(e),r=this.graph.allEdges(e),n=new Map(t.map(u=>[u.id,u])),i=[...new Set(t.map(u=>de(u.filePath)))].sort(),s=new Map,a=(u,f)=>{let g=[u,f].sort();return`${g[0]}\0${g[1]}`};for(let u of r){let f=n.get(u.from),g=n.get(u.to);if(!f||!g)continue;let h=de(f.filePath),y=de(g.filePath);if(h===y)continue;let d=yr[u.type]??0;if(d<=0)continue;let S=a(h,y),x=s.get(S);x?x.weight+=d:s.set(S,{a:[h,y].sort()[0],b:[h,y].sort()[1],weight:d})}let p=new Map;for(let u of i){let f=br(u),g=p.get(f)??[];g.push(u),p.set(f,g)}let c=[];for(let[u,f]of p){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 s.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,d=[];for(let x of f){if(y.has(x))continue;let C=[x],E=[];for(y.add(x);C.length>0;){let T=C.shift();E.push(T);for(let Re of h.get(T)??[])y.has(Re)||(y.add(Re),C.push(Re))}d.push(E.sort())}let S=[];for(let x of d)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 d=l[y];if(!d)continue;let S=0,x=new Set(h.files),C=new Set(d.files);for(let E of s.values())(x.has(E.a)&&C.has(E.b)||x.has(E.b)&&C.has(E.a))&&(S+=E.weight);if(S>=20){d.files=[...new Set([...d.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 s.values()){let E=f.has(C.a),T=f.has(C.b);E&&T?g+=C.weight:(E||T)&&(h+=C.weight)}let y=g+h,d=u.subsystem.replace(/\/residual$/,""),S=xr(d),x=u.subsystem.endsWith("/residual")?`${S} Misc`:S;m.push({id:`cluster-${Sr(`${u.subsystem}:${u.files.join("|")}`)}`,label:x,subsystem:d,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 ue=class{constructor(e){this.graph=e;this.entryPoints=new j(e),this.hotspots=new ce(e),this.layers=new le(e),this.clusters=new pe(e)}graph;entryPoints;hotspots;layers;clusters;analyze(e){let t=this.entryPoints.detect(e),r=this.hotspots.analyze(e),n=this.layers.detect(e),i=this.clusters.detect(e),s=new Set(this.graph.allSymbols(e).map(a=>a.filePath));return{version:1,projectId:e,updatedAt:new Date().toISOString(),summary:{files:s.size,entryPoints:t.length,hotspots:r.length,layers:n.length,clusters:i.length},entryPoints:t,hotspots:r,layers:n,clusters:i}}};var tt=new Set(["CALLS","CALL_REFERENCE","IMPORTS","USES_TYPE","WRITES","INHERITS","IMPLEMENTS","ROUTE","TESTS"]);function $e(o){return o.replaceAll("\\","/").toLowerCase()}function wr(o){let e=$e(o);return e.startsWith("tests/")||e.includes("/tests/")||e.includes("__tests__")||/\.(test|spec)\.[^.]+$/.test(e)}function Cr(o){let e=$e(o.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),n=new Set(new j(this.graph).detect(e).map(s=>s.symbolId)),i=[];for(let s of t){if(!["function","method","class","interface","property"].includes(s.type)||wr(s.filePath)||n.has(s.id)||Cr(s)||r.filter(f=>f.to===s.id&&tt.has(f.type)).length>0)continue;let p=r.filter(f=>f.from===s.id&&tt.has(f.type)),c=["no incoming usage edge"],l=50;(s.type==="function"||s.type==="method")&&(l+=20,c.push("callable symbol")),s.type==="property"&&(l-=15),p.length===0&&(l+=10,c.push("no outgoing dependency")),$e(s.filePath).includes("/internal/")&&(l+=10,c.push("internal implementation")),s.name.startsWith("_")&&(l+=5);let u=l>=75?"high":l>=55?"medium":"low";i.push({symbolId:s.id,name:s.name,qualifiedName:s.qualifiedName,type:s.type,filePath:s.filePath,startLine:s.startLine,confidence:u,score:l,reasons:c})}return i.sort((s,a)=>a.score-s.score||s.filePath.localeCompare(a.filePath)||s.name.localeCompare(a.name))}};var vr=new Set(["CALLS","CALL_REFERENCE","IMPORTS","USES_TYPE","WRITES","INHERITS","IMPLEMENTS","ROUTE"]),fe=class{constructor(e){this.graph=e}graph;analyze(e){let t=this.graph.allSymbols(e),r=this.graph.allEdges(e).filter(s=>vr.has(s.type)),n=new Map(t.map(s=>[s.id,s]));return[...new Set(t.map(s=>s.filePath))].sort().map(s=>{let a=new Set,p=new Set,c=0,l=0;for(let m of r){let u=n.get(m.from),f=n.get(m.to);!u||!f||u.filePath===f.filePath||(u.filePath===s&&(a.add(f.filePath),c++),f.filePath===s&&(p.add(u.filePath),l++))}return{filePath:s,dependencies:[...a].sort(),dependents:[...p].sort(),outgoingEdges:c,incomingEdges:l}})}};var ge=class{constructor(e){this.graph=e}graph;analyze(e){let t=new me(this.graph).analyze(e),r=new fe(this.graph).analyze(e);return{version:1,projectId:e,updatedAt:new Date().toISOString(),summary:{deadCodeCandidates:t.length,highConfidenceDeadCode:t.filter(n=>n.confidence==="high").length,files:r.length},deadCode:t,dependencies:r}}};var he=class{constructor(e){this.graph=e}graph;build(e,t,r,n){let i=this.graph.allSymbols(e),s=this.graph.allEdges(e),a=new Map(t?.layers.map(d=>[d.filePath,d.layer])??[]),p=new Map;for(let d of t?.clusters??[])for(let S of d.files)p.set(S,{id:d.id,label:d.label});let c=new Map(t?.hotspots.map(d=>[d.filePath,d.score])??[]),l=new Map(r?.deadCode.map(d=>[d.symbolId,{confidence:d.confidence,score:d.score}])??[]),m=new Map,u=new Map;for(let d of s)u.set(d.from,(u.get(d.from)??0)+1),m.set(d.to,(m.get(d.to)??0)+1);let f=[];for(let d=0;d<i.length;d++){let S=i[d],x=p.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}),((d+1)%100===0||d===i.length-1)&&n?.({current:d+1,total:i.length,phase:"nodes"})}let g=new Set(f.map(d=>d.id)),h=s.filter(d=>g.has(d.from)&&g.has(d.to)),y=[];for(let d=0;d<h.length;d++){let S=h[d];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}),((d+1)%100===0||d===h.length-1)&&n?.({current:d+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(d=>d.filePath)).size,clusters:new Set(f.map(d=>d.cluster).filter(Boolean)).size},nodes:f,links:y}}};import{homedir as Hr}from"node:os";import{join as Vr}from"node:path";import{DeleteObjectCommand as Er,GetObjectCommand as Pr,HeadObjectCommand as kr,ListObjectsV2Command as Tr,PutObjectCommand as Rr,S3Client as Ir}from"@aws-sdk/client-s3";import{getSignedUrl as Mr}from"@aws-sdk/s3-request-presigner";var ye=class{name="huggingface";client;bucket;constructor(e){this.bucket=e.bucket,this.client=new Ir({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 n=typeof t=="string"?Buffer.from(t,"utf8"):t;await this.client.send(new Rr({Bucket:this.bucket,Key:e,Body:n,ContentType:r}))}async get(e){let t=await Mr(this.client,new Pr({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 kr({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 Er({Bucket:this.bucket,Key:e}))}async list(e=""){let t=[],r;do{let n=await this.client.send(new Tr({Bucket:this.bucket,Prefix:e||void 0,ContinuationToken:r}));for(let i of n.Contents??[])i.Key&&t.push({key:i.Key,size:i.Size,updatedAt:i.LastModified?.toISOString()});r=n.IsTruncated?n.NextContinuationToken:void 0}while(r);return t}};import{access as rt,mkdir as Ar,readFile as Nr,readdir as Lr,rm as jr,stat as st,writeFile as Or}from"node:fs/promises";import{dirname as $r,join as _r,relative as nt,resolve as Dr}from"node:path";var Y=class{constructor(e){this.root=e}root;name="local";path(e){let t=e.replace(/^\/+/,"");return Dr(this.root,t)}async put(e,t){let r=this.path(e);await Ar($r(r),{recursive:!0}),await Or(r,t)}async get(e){try{return await Nr(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 rt(this.path(e)),!0}catch{return!1}}async delete(e){await jr(this.path(e),{force:!0})}async list(e=""){let t=this.path(e),r=[];try{await rt(t)}catch{return r}let n=async s=>{let a=await Lr(s,{withFileTypes:!0});for(let p of a){let c=_r(s,p.name);if(p.isDirectory()){await n(c);continue}let l=await st(c);r.push({key:nt(this.root,c),size:l.size,updatedAt:l.mtime.toISOString()})}},i=await st(t);return i.isDirectory()?await n(t):r.push({key:nt(this.root,t),size:i.size,updatedAt:i.mtime.toISOString()}),r}};import{DeleteObjectCommand as Fr,GetObjectCommand as Gr,HeadObjectCommand as zr,ListObjectsV2Command as Kr,PutObjectCommand as Br,S3Client as Ur}from"@aws-sdk/client-s3";import{getSignedUrl as Wr}from"@aws-sdk/s3-request-presigner";var J=class{name;client;bucket;constructor(e){this.name=e.name??"s3",this.bucket=e.bucket,this.client=new Ur({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 n=typeof t=="string"?Buffer.from(t,"utf8"):t;await this.client.send(new Br({Bucket:this.bucket,Key:e,Body:n,ContentType:r}))}async get(e){let t=await Wr(this.client,new Gr({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 zr({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 Fr({Bucket:this.bucket,Key:e}))}async list(e=""){let t=[],r;do{let n=await this.client.send(new Kr({Bucket:this.bucket,Prefix:e||void 0,ContinuationToken:r}));for(let i of n.Contents??[])i.Key&&t.push({key:i.Key,size:i.Size,updatedAt:i.LastModified?.toISOString()});r=n.IsTruncated?n.NextContinuationToken:void 0}while(r);return t}};function _e(o,e){return console.warn(e),new Y(o)}function ot(o){let e=o.localRoot??Vr(Hr(),".toolnet-memory","storage");if(o.provider==="r2"){let t=o.r2;return t?.accountId&&t.bucket&&t.accessKeyId&&t.secretAccessKey?new J({name:"r2",endpoint:`https://${t.accountId}.r2.cloudflarestorage.com`,region:"auto",bucket:t.bucket,forcePathStyle:!0,accessKeyId:t.accessKeyId,secretAccessKey:t.secretAccessKey}):_e(e,"[storage] Cloudflare R2 credentials missing. Using local fallback.")}if(o.provider==="s3"){let t=o.s3;return t?.bucket&&t.accessKeyId&&t.secretAccessKey?new J({name:"s3",endpoint:t.endpoint,region:t.region??"us-east-1",bucket:t.bucket,forcePathStyle:t.forcePathStyle??!1,accessKeyId:t.accessKeyId,secretAccessKey:t.secretAccessKey}):_e(e,"[storage] S3 credentials missing. Using local fallback.")}if(o.provider==="huggingface"){let t=o.huggingface;return t?.namespace&&t.bucket&&t.accessKeyId&&t.secretAccessKey?new ye({namespace:t.namespace,bucket:t.bucket,accessKeyId:t.accessKeyId,secretAccessKey:t.secretAccessKey}):_e(e,"[storage] Hugging Face credentials missing. Using local fallback.")}return new Y(e)}function Yr(o){return new Promise(e=>setTimeout(e,o))}async function it(o,e={}){let t=Math.max(1,e.attempts??3),r=e.baseDelayMs??150,n=e.maxDelayMs??2e3,i;for(let s=1;s<=t;s++)try{return await o()}catch(a){if(i=a,s>=t)break;let p=Math.min(n,r*2**(s-1)),c=Math.floor(Math.random()*Math.max(1,p*.2));await Yr(p+c)}throw i}var Jr=new Set(["put","get","getText","delete","list"]);function at(o,e={}){return new Proxy(o,{get(t,r){let n=Reflect.get(t,r,t);return typeof n!="function"?n:Jr.has(r)?(...i)=>it(()=>Promise.resolve(n.apply(t,i)),e):n.bind(t)}})}function ct(o){let e=o.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 lt(o){let e=o.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 n=e.match(/^(projects\/[^/]+\/)memories\/(.+)$/);return n?`${n[1]}memory/records/${n[2]}`:(n=e.match(/^(projects\/[^/]+\/)vectors\/(.+)$/),n?`${n[1]}memory/vectors/${n[2]}`:(n=e.match(/^(projects\/[^/]+\/)graph\/(.+)$/),n?`${n[1]}code/graph/${n[2]}`:e))}var Se=class{constructor(e,t,r,n){this.provider=e;this.name=e.name,this.projectId=t,this.projectName=r,this.folder=ct(n??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,n=await this.provider.getText(e);if(n){let s;try{s=JSON.parse(n)}catch(a){throw new Error(`Invalid remote ToolNet project manifest at ${e}: ${a instanceof Error?a.message:String(a)}`)}if(typeof s.id=="string"&&s.id!==this.projectId)throw new Error(["ToolNet remote project namespace collision.",`Remote folder: ${this.targetPrefix}`,`Existing project id: ${s.id}`,`Current project id: ${this.projectId}`,"Refusing to mix data from two projects."].join(" "));typeof s.createdAt=="string"&&(r=s.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=lt(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 be=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 xe=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=n=>JSON.stringify({version:e.version,projectId:e.projectId,updatedAt:e.updatedAt,data:n},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 we=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=n=>JSON.stringify({version:e.version,projectId:e.projectId,updatedAt:e.updatedAt,data:n},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 Ce=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 dt(o,e={}){let t=Date.now();async function r(h,y,d){await e.onStage?.({id:h,title:y,state:"start"});let S=Date.now(),x=await d();return await e.onStage?.({id:h,title:y,state:"complete",durationMs:Date.now()-S}),x}let n=We(),i=at(ot({provider:n.storage.provider,huggingface:n.storage.huggingface,localRoot:n.storage.localRoot}),{attempts:3}),s=new Se(i,o.id,o.name,o.remote??o.name),a=new B(s),p=await r("source-index","Source Index",async()=>{let h=await new te().index(o.id,o.rootPath,{onProgress:y=>{e.onSourceProgress?.(y)}});return await a.save({version:1,projectId:o.id,updatedAt:new Date().toISOString(),files:h.files,symbols:h.graph.allSymbols(o.id),edges:h.graph.allEdges(o.id)}),h}),c=p.graph,l=await r("type-resolution","Type Resolution",async()=>{let h=await new ie(c).resolveProject(o.id,o.rootPath,y=>{e.onStageProgress?.({stage:"type-resolution",current:y.current,total:y.total,phase:y.phase,detail:y.detail})});return await new be(s).save(h),h});await r("rich-graph","Rich Graph",async()=>{let h=new ae(c).enrich(o.id,o.rootPath,l,S=>{e.onStageProgress?.({stage:"rich-graph",current:S.current,total:S.total,phase:S.phase,detail:S.detail})}),y=c.allSymbols(o.id),d=c.allEdges(o.id);return await a.save({version:1,projectId:o.id,updatedAt:new Date().toISOString(),files:y.filter(S=>S.type==="file").length,symbols:y,edges:d}),h});let m=await r("semantic-index","Semantic Code Index",async()=>new oe({projectId:o.id,rootPath:o.rootPath,storage:s,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 ue(c).analyze(o.id);return await new xe(s).save(h),h}),f=await r("analysis","Graph Analysis",async()=>{let h=new ge(c).analyze(o.id);return await new we(s).save(h),h}),g=await r("visualization","3D Visualization Dataset",async()=>{let h=new he(c).build(o.id,u,f,y=>{e.onStageProgress?.({stage:"visualization",current:y.current,total:y.total,phase:y.phase,detail:y.detail})});return await new Ce(s).save(h),h});return{files:p.files,project:{id:o.id,name:o.name,remote:o.remote??o.name},storage:s.name,durationMs:Date.now()-t,graph:{symbols:c.allSymbols(o.id).length,edges:c.allEdges(o.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 Xr}from"worker_threads";import*as Ee from"path";import{fileURLToPath as es}from"node:url";function ve(){if(process.argv.includes("--no-color"))return!1;if(process.argv.includes("--color"))return!0;let o=process.env.NO_COLOR;if(o!==void 0&&o!=="")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 qr={spinner:["\xB7","\u2722","\u2733","\u2736","\u273B","\u273D"],barFilled:"\u2588",barEmpty:"\u2591",phaseDone:"\u25C6",phaseActive:"\u25C7",rail:"\u2502",branch:"\u251C",corner:"\u2514",dash:"\u2014"},Qr={spinner:[".","*","+","x","*","+"],barFilled:"#",barEmpty:"-",phaseDone:"[OK]",phaseActive:"[>>]",rail:"|",branch:"|",corner:"\\",dash:"-"};function Zr(){if(process.env.TOOLNET_ASCII==="1")return!1;if(process.env.TOOLNET_UNICODE==="1")return!0;if(process.platform==="win32"){let o=process.env.TERM_PROGRAM,e=process.env.WT_SESSION,t=process.env.VSCODE_PID,r=process.env.ConEmuPID;return!!(e||t||r||o==="vscode")}return process.env.TERM!=="linux"}var De;function O(){return De||(De=Zr()?qr:Qr),De}var pt={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 ut(){if(process.stdout.isTTY!==!0)return ts();let o=ve(),e=O(),t=o?"\x1B[2m":"",r=o?"\x1B[32m":"",n=o?"\x1B[0m":"",i="",s="",a=-1,p=0,c=()=>{if(!s)return;let g="";a>=0?g=` ${e.dash} done`:p>0&&(g=` ${e.dash} ${p.toLocaleString()} found`),process.stdout.write(`\r\x1B[K${t}${e.branch}${n} ${r}${e.phaseDone}${n} ${s}${g}
7
+ `),s="",a=-1,p=0},l=Ee.dirname(es(import.meta.url)),m=l.includes("/bundle/")||l.includes("\\bundle\\"),u=Ee.join(l,m?"shimmer-worker.js":"shimmer-worker.ts"),f=new Xr(u,{workerData:{startTime:Date.now(),colors:o}});return{onProgress(g){let h=pt[g.stage]||g.stage;g.stage!==i&&i&&c(),i=g.stage,s=h;let y=-1,d=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&&(d=g.current);a=y,p=d,f.postMessage({type:"update",phase:g.stage,phaseName:h,percent:y,count:d})},stop(){return new Promise(g=>{let h=!1,y=()=>{h||(h=!0,c(),g())},d=setTimeout(()=>{f.terminate().then(y)},2e3);f.on("message",S=>{S.type==="stopped"&&(clearTimeout(d),f.terminate().then(y))}),f.postMessage({type:"stop"})})}}}function ts(){let o="";return{onProgress(e){if(e.stage===o)return;o=e.stage;let t=pt[e.stage]||e.stage;process.stdout.write(`${t}...
8
+ `)},stop(){return Promise.resolve()}}}var P={reset:"\x1B[0m",green:"\x1B[32m",gray:"\x1B[90m",white:"\x1B[97m",cyan:"\x1B[36m",red:"\x1B[31m"};function A(o){return new Intl.NumberFormat("en-US").format(o)}function Fe(o){return o<1e3?`${o}ms`:`${(o/1e3).toFixed(1)}s`}function mt(o,e,t){return Math.max(e,Math.min(t,o))}function rs(o,e){return e<=0?"":o.length<=e?o:e<=3?o.slice(0,e):`${o.slice(0,e-1)}\u2026`}var Pe=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&&ve(),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
10
9
  `),this.stream.write(`\u25C7 Initialized in ${this.rootPath}
11
- `);return}let e=B();this.writeStatic(this.color("\u25C7 Initializing ToolNet Index",T.white)),this.stream.write(`
12
- `);let t=Math.max(16,this.terminalWidth()-18);this.writeStatic(`${this.color(e.phaseDone,T.green)} Initialized in ${Os(this.rootPath,t)}`),this.writeStatic(this.color(e.rail,T.gray)),this.shimmerProgress=_t()}scanningComplete(e){if(this.scanTotal=e,this.parseTotal=e,!!this.enabled){if(!this.interactive){this.stream.write(`\u25C6 Scanning files \u2014 ${D(e)} found
10
+ `);return}let e=O();this.writeStatic(this.color("\u25C7 Initializing ToolNet Index",P.white)),this.stream.write(`
11
+ `);let t=Math.max(16,this.terminalWidth()-18);this.writeStatic(`${this.color(e.phaseDone,P.green)} Initialized in ${rs(this.rootPath,t)}`),this.writeStatic(this.color(e.rail,P.gray)),this.shimmerProgress=ut()}scanningComplete(e){if(this.scanTotal=e,this.parseTotal=e,!!this.enabled){if(!this.interactive){this.stream.write(`\u25C6 Scanning files \u2014 ${A(e)} found
13
12
  `);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 n=r>=100?100:Math.floor(r/25)*25;n!==this.lastPlainParseBucket&&(this.lastPlainParseBucket=n,this.stream.write(`\u2192 Parsing code \u2014 ${r}%
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}
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 n=this.stages.find(o=>o.id===e);if(n&&(n.state="done",n.title=t,n.durationMs=r),!this.enabled)return;if(!this.interactive){e==="source-index"?this.stream.write(`\u2713 Parsing code \u2014 done
16
- `):this.stream.write(`\u2713 ${t} (${rt(r)})
17
- `);return}let i=B();this.writeStatic(`${this.color(i.rail,T.gray)} ${this.color(i.phaseDone,T.green)} ${t} ${this.color(i.dash,T.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=B();if(this.interactive){this.writeStatic(this.color(t.rail,T.gray)),this.writeStatic(`${this.color(t.phaseDone,T.green)} Indexed ${D(e.files)} files`),this.writeStatic(this.color(t.rail,T.gray)),this.writeStatic(`${this.color(t.phaseActive,T.cyan)} ${D(e.symbols)} symbols, ${D(e.edges)} edges in ${rt(e.durationMs)}`),this.writeStatic(`${this.color(t.phaseActive,T.cyan)} Storage: ${e.storage}`),this.writeStatic(this.color(t.rail,T.gray)),this.writeStatic(`${this.color(t.corner,T.gray)} ${this.color(t.phaseDone,T.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 ${rt(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",T.red)} ${e}
13
+ `));return}this.shimmerProgress&&this.shimmerProgress.onProgress({stage:"parse",current:e,total:t})}startStage(e,t){let r=this.stages.find(s=>s.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
+ `);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 n=this.stages.find(s=>s.id===e);if(n&&(n.state="done",n.title=t,n.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} (${Fe(r)})
16
+ `);return}let i=O();this.writeStatic(`${this.color(i.rail,P.gray)} ${this.color(i.phaseDone,P.green)} ${t} ${this.color(i.dash,P.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=O();if(this.interactive){this.writeStatic(this.color(t.rail,P.gray)),this.writeStatic(`${this.color(t.phaseDone,P.green)} Indexed ${A(e.files)} files`),this.writeStatic(this.color(t.rail,P.gray)),this.writeStatic(`${this.color(t.phaseActive,P.cyan)} ${A(e.symbols)} symbols, ${A(e.edges)} edges in ${Fe(e.durationMs)}`),this.writeStatic(`${this.color(t.phaseActive,P.cyan)} Storage: ${e.storage}`),this.writeStatic(this.color(t.rail,P.gray)),this.writeStatic(`${this.color(t.corner,P.gray)} ${this.color(t.phaseDone,P.green)} Done`),this.stream.write(`
17
+ `);return}let r=["",`\u25C6 Indexed ${A(e.files)} files`,"",`\u25C7 ${A(e.symbols)} symbols, ${A(e.edges)} edges in ${Fe(e.durationMs)}`,`\u25C7 Storage: ${e.storage}`,"","Done",""];this.stream.write(r.join(`
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",P.red)} ${e}
20
19
  `);return}this.stream.write(`\u2717 ${e}
21
- `)}terminalWidth(){let e=this.stream.columns;if(typeof e=="number"&&Number.isFinite(e))return Ot(Math.floor(e),32,240);let t=process.stdout.columns;return typeof t=="number"&&Number.isFinite(t)?Ot(Math.floor(t),32,240):80}color(e,t){return this.colorEnabled?`${t}${e}${T.reset}`:e}writeStatic(e){this.stream.write(`${e}
22
- `)}};import{createConnection as $s}from"node:net";import{homedir as js}from"node:os";import{join as Ds}from"node:path";function jt(){return process.env.TOOLNET_SERVICE_SOCKET??Ds(js(),".toolnet","run","service.sock")}function Fs(s,e={}){let t=e.socketPath??jt(),r=e.timeoutMs??1e3;return new Promise((n,i)=>{let o=$s({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(s)}
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{n(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 Dt(s,e={}){try{let t=await Fs({type:"invalidate",project:s},{timeoutMs:250,...e});return t.ok&&t.type==="invalidate"}catch{return!1}}var z=new ie().detect(),at=it(z.rootPath,".toolnet"),st=it(at,"index.lock"),Hs=it(at,"last-index.json"),nt=process.argv.includes("--json"),Vs=[{id:"source-index",title:"Source Index"},{id:"type-resolution",title:"Resolving refs"},{id:"rich-graph",title:"Building rich graph"},{id:"semantic-index",title:"Semantic index"},{id:"architecture",title:"Architecture intelligence"},{id:"analysis",title:"Graph analysis"},{id:"visualization",title:"Visualization dataset"}],_=new Ge({rootPath:z.rootPath,stages:Vs,enabled:!nt});Ks(at,{recursive:!0});function Ke(s){zs(Hs,JSON.stringify(s,null,2)+`
25
- `,{encoding:"utf8",mode:384})}var ot;try{ot=Us(st,"wx",384)}catch{console.error("\u274C Index already running for this project."),console.error(`Lock: ${st}`),process.exit(2)}var Ue=new Date().toISOString(),ne=[];try{_.start(),Ke({version:2,state:"running",startedAt:Ue,completedStages:ne,project:z});let s=await Mt(z,{onSourceProgress:e=>{if(e.phase==="scan"){_.scanningComplete(e.total);return}_.parsingProgress(e.current,e.total)},onStage:async e=>{e.state==="start"?_.startStage(e.id,e.title):(ne.push(e.id),_.completeStage(e.id,e.title,e.durationMs??0)),Ke({version:2,state:"running",startedAt:Ue,currentStage:e.id,completedStages:[...ne],project:z})},onStageProgress:e=>{_.stageProgress({stage:e.stage,current:e.current,total:e.total,phase:e.phase,detail:e.detail})}});Ke({version:2,state:"complete",startedAt:Ue,finishedAt:new Date().toISOString(),completedStages:ne,result:s}),await Dt(z),nt?console.log(JSON.stringify(s,null,2)):await _.finish({files:s.files,symbols:s.graph.symbols,edges:s.graph.edges,durationMs:s.durationMs,storage:s.storage})}catch(s){let e=s instanceof Error?s.message:String(s);Ke({version:2,state:"failed",startedAt:Ue,failedAt:new Date().toISOString(),completedStages:ne,error:e}),_.fail(`Full index failed: ${e}`),nt&&console.error(e),process.exitCode=1}finally{ot!==void 0&&Gs(ot),Bs(st,{force:!0})}
20
+ `)}terminalWidth(){let e=this.stream.columns;if(typeof e=="number"&&Number.isFinite(e))return mt(Math.floor(e),32,240);let t=process.stdout.columns;return typeof t=="number"&&Number.isFinite(t)?mt(Math.floor(t),32,240):80}color(e,t){return this.colorEnabled?`${t}${e}${P.reset}`:e}writeStatic(e){this.stream.write(`${e}
21
+ `)}};import{createConnection as os}from"node:net";import{homedir as ss}from"node:os";import{join as ns}from"node:path";function ft(){return process.env.TOOLNET_SERVICE_SOCKET??ns(ss(),".toolnet","run","service.sock")}function is(o,e={}){let t=e.socketPath??ft(),r=e.timeoutMs??1e3;return new Promise((n,i)=>{let s=os({path:t}),a="",p=!1,c=m=>{p||(p=!0,clearTimeout(l),s.destroy(),m())},l=setTimeout(()=>{c(()=>{i(new Error(`ToolNet service timeout after ${r}ms`))})},r);s.setEncoding("utf8"),s.once("connect",()=>{s.write(`${JSON.stringify(o)}
22
+ `)}),s.on("data",m=>{a+=m;let u=a.indexOf(`
23
+ `);if(u===-1)return;let f=a.slice(0,u).trim();c(()=>{try{n(JSON.parse(f))}catch(g){i(g instanceof Error?g:new Error(String(g)))}})}),s.once("error",m=>{c(()=>{i(m)})}),s.once("end",()=>{p||c(()=>{i(new Error("ToolNet service closed without response"))})})})}async function gt(o,e={}){try{let t=await is({type:"invalidate",project:o},{timeoutMs:250,...e});return t.ok&&t.type==="invalidate"}catch{return!1}}var $=new Z().detect(),Ue=Be($.rootPath,".toolnet"),Ge=Be(Ue,"index.lock"),us=Be(Ue,"last-index.json"),ze=process.argv.includes("--json"),ms=[{id:"source-index",title:"Source Index"},{id:"type-resolution",title:"Resolving refs"},{id:"rich-graph",title:"Building rich graph"},{id:"semantic-index",title:"Semantic index"},{id:"architecture",title:"Architecture intelligence"},{id:"analysis",title:"Graph analysis"},{id:"visualization",title:"Visualization dataset"}],I=new Pe({rootPath:$.rootPath,stages:ms,enabled:!ze});cs(Ue,{recursive:!0});function ke(o){ps(us,JSON.stringify(o,null,2)+`
24
+ `,{encoding:"utf8",mode:384})}var Ke;try{Ke=ls(Ge,"wx",384)}catch{console.error("\u274C Index already running for this project."),console.error(`Lock: ${Ge}`),process.exit(2)}var Te=new Date().toISOString(),q=[];try{I.start(),ke({version:2,state:"running",startedAt:Te,completedStages:q,project:$});let o=await dt($,{onSourceProgress:e=>{if(e.phase==="scan"){I.scanningComplete(e.total);return}I.parsingProgress(e.current,e.total)},onStage:async e=>{e.state==="start"?I.startStage(e.id,e.title):(q.push(e.id),I.completeStage(e.id,e.title,e.durationMs??0)),ke({version:2,state:"running",startedAt:Te,currentStage:e.id,completedStages:[...q],project:$})},onStageProgress:e=>{I.stageProgress({stage:e.stage,current:e.current,total:e.total,phase:e.phase,detail:e.detail})}});ke({version:2,state:"complete",startedAt:Te,finishedAt:new Date().toISOString(),completedStages:q,result:o}),await gt($),ze?console.log(JSON.stringify(o,null,2)):await I.finish({files:o.files,symbols:o.graph.symbols,edges:o.graph.edges,durationMs:o.durationMs,storage:o.storage})}catch(o){let e=o instanceof Error?o.message:String(o);ke({version:2,state:"failed",startedAt:Te,failedAt:new Date().toISOString(),completedStages:q,error:e}),I.fail(`Full index failed: ${e}`),ze&&console.error(e),process.exitCode=1}finally{Ke!==void 0&&as(Ke),ds(Ge,{force:!0})}
@@ -1,4 +1,4 @@
1
1
  import"dotenv/config";import{existsSync as ce,readFileSync as le}from"node:fs";import{homedir as de}from"node:os";import{join as pe}from"node:path";function ue(s){let e=s.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 me(){let s=process.env.TOOLNET_GLOBAL_ENV??pe(de(),".config","toolnet-memory",".env");if(!ce(s))return;let e=le(s,"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 o=r.indexOf("=");if(o<=0)continue;let n=r.slice(0,o).trim();/^[A-Za-z_][A-Za-z0-9_]*$/.test(n)&&process.env[n]===void 0&&(process.env[n]=ue(r.slice(o+1)))}}me();function v(s,e){return s===void 0?e:["1","true","yes","on"].includes(s.toLowerCase())}function P(s,e){if(!s)return e;let t=Number(s);return Number.isFinite(t)?t:e}function V(){return{memory:{autoCapture:v(process.env.MEMORY_AUTO_CAPTURE,!0),autoRetrieve:v(process.env.MEMORY_AUTO_RETRIEVE,!0),autoSummarize:v(process.env.MEMORY_AUTO_SUMMARIZE,!0),autoSync:v(process.env.MEMORY_AUTO_SYNC,!0)},retrieval:{maxCandidates:P(process.env.MEMORY_MAX_CANDIDATES,50),rerankTop:P(process.env.MEMORY_RERANK_TOP,10),finalContext:P(process.env.MEMORY_FINAL_CONTEXT,5),tokenBudget:P(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:v(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:P(process.env.MEMORY_LOCAL_CACHE_MB,200)}}}import{createHash as fe}from"node:crypto";import{existsSync as D,mkdirSync as ge,readFileSync as he,renameSync as ye,writeFileSync as Se}from"node:fs";import{basename as be,dirname as N,join as O,parse as J,resolve as k}from"node:path";var Q=".toolnet",xe="project.json";function Ce(s){return fe("sha256").update(s).digest("hex").slice(0,16)}function G(s){return O(s,Q,xe)}function we(s){return D(G(s))}function Ee(s,e){let t=k(s),r=J(t).root;for(;;){if(we(t))return t;if(t===r||e&&t===k(e))break;let o=N(t);if(o===t)break;t=o}return null}function ve(s){let e=k(s),t=J(e).root,r=[".git","package.json","pyproject.toml","Cargo.toml","go.mod","composer.json"];for(;;){if(r.some(n=>D(O(e,n))))return e;if(e===t)break;let o=N(e);if(o===e)break;e=o}return k(s)}function Pe(s){let e;try{e=JSON.parse(he(s,"utf8"))}catch(o){throw new Error(`Invalid ToolNet project manifest: ${s}: ${o instanceof Error?o.message:String(o)}`)}if(!e||typeof e!="object")throw new Error(`Invalid ToolNet project manifest: ${s}`);let t=e;if(typeof t.id!="string"||!t.id.trim())throw new Error(`ToolNet project manifest is missing id: ${s}`);if(typeof t.name!="string"||!t.name.trim())throw new Error(`ToolNet project manifest is missing name: ${s}`);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:N(N(s)),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 Y(s,e){let t=O(s,Q);ge(t,{recursive:!0});let r=G(s),o=`${r}.tmp-${process.pid}`;Se(o,JSON.stringify(e,null,2)+`
3
- `,{encoding:"utf8",mode:384}),ye(o,r)}function q(s,e){return{id:s.id,name:s.name,remote:s.remote,rootPath:e,createdAt:s.createdAt,updatedAt:s.updatedAt,graphVersion:s.graphVersion,memoryVersion:s.memoryVersion,metadata:s.metadata}}var I=class{detect(e=process.cwd()){let t=k(e),r=ve(t),n=[".git","package.json","pyproject.toml","Cargo.toml","go.mod","composer.json"].some(m=>D(O(r,m))),i=Ee(t,n?r:void 0);if(i){let m=G(i),u=Pe(m);return u.rootPath!==i&&(u.rootPath=i,u.updatedAt=new Date().toISOString(),Y(i,u)),q(u,i)}let d=new Date().toISOString(),f=be(r),c={version:1,id:Ce(r),name:f,remote:f,rootPath:r,createdAt:d,updatedAt:d,graphVersion:0,memoryVersion:0};return Y(r,c),q(c,r)}};import{readdir as Ae}from"node:fs/promises";import{extname as Me,join as Le,relative as Ne}from"node:path";import{basename as Re}from"node:path";var Te=new Set([".env",".env.local",".env.production",".env.development","id_rsa","id_ed25519","credentials","credentials.json","service-account.json"]),je=new Set([".pem",".key",".p12",".pfx"]);function Z(s){let e=Re(s).toLowerCase();if(Te.has(e))return!0;for(let t of je)if(e.endsWith(t))return!0;return!1}var Ie=new Set([".ts",".tsx",".js",".jsx",".mjs",".cjs",".mts",".cts"]),Oe=new Set(["node_modules",".git","dist","build","coverage",".next",".cache",".toolnet-memory"]);async function K(s){let e=[];async function t(r){let o=await Ae(r,{withFileTypes:!0});for(let n of o){if(Oe.has(n.name))continue;let i=Le(r,n.name);if(n.isDirectory()){await t(i);continue}Ie.has(Me(n.name))&&(Z(i)||e.push(Ne(s,i)))}}return await t(s),e.sort()}import{readFile as _e}from"node:fs/promises";import{join as Fe}from"node:path";import{createHash as $e}from"node:crypto";import*as l from"typescript";function X(s,e){return $e("sha256").update(`${s}:${e}`).digest("hex").slice(0,24)}function z(s,e){return s.getLineAndCharacterOfPosition(e.getStart(s)).line+1}async function B(s,e,t){let r=Fe(e,t),o=await _e(r,"utf8"),n=l.createSourceFile(t,o,l.ScriptTarget.Latest,!0,t.endsWith(".tsx")||t.endsWith(".jsx")?l.ScriptKind.TSX:l.ScriptKind.TS),i=[],d=[],f=[],c=[],m={id:X(s,`file:${t}`),projectId:s,name:t,qualifiedName:t,type:"file",filePath:t,startLine:1,endLine:n.getLineAndCharacterOfPosition(n.end).line+1};i.push(m);let u=[],x=[];function b(a,y,h,g){let C=X(s,`${t}:${h}:${g??y}:${a.pos}`);return i.push({id:C,projectId:s,name:y,qualifiedName:g??y,type:h,filePath:t,startLine:z(n,a),endLine:n.getLineAndCharacterOfPosition(a.end).line+1}),C}function L(a){if(!l.isStringLiteral(a.moduleSpecifier))return;let y=[],h=a.importClause;if(h?.name&&y.push({localName:h.name.text,importedName:"default",kind:"default"}),h?.namedBindings&&l.isNamedImports(h.namedBindings))for(let g of h.namedBindings.elements)y.push({localName:g.name.text,importedName:g.propertyName?.text??g.name.text,kind:"named"});h?.namedBindings&&l.isNamespaceImport(h.namedBindings)&&y.push({localName:h.namedBindings.name.text,importedName:"*",kind:"namespace"}),d.push({source:a.moduleSpecifier.text,bindings:y})}function E(a,y){for(let h of a.heritageClauses??[])for(let g of h.types){let C=g.expression.getText(n).split(".").at(-1)??"";C&&c.push({fromId:y,targetName:C,type:h.token===l.SyntaxKind.ExtendsKeyword?"INHERITS":"IMPLEMENTS"})}}function U(a){let y=!1,h=!1;if(l.isImportDeclaration(a)&&L(a),l.isInterfaceDeclaration(a)&&b(a,a.name.text,"interface",a.name.text),l.isClassDeclaration(a)&&a.name){let g=b(a,a.name.text,"class",a.name.text);E(a,g),x.push({id:g,name:a.name.text}),h=!0}if(l.isFunctionDeclaration(a)&&a.name){let g=b(a,a.name.text,"function",a.name.text);u.push(g),y=!0}if(l.isMethodDeclaration(a)&&a.name){let g=a.name.getText(n),C=x.at(-1)?.name,ae=b(a,g,"method",C?`${C}.${g}`:g);u.push(ae),y=!0}if(l.isCallExpression(a)&&(l.isIdentifier(a.expression)&&f.push({callerId:u.at(-1),calleeName:a.expression.text,line:z(n,a)}),l.isPropertyAccessExpression(a.expression))){let g=a.expression;f.push({callerId:u.at(-1),qualifier:g.expression.getText(n),calleeName:g.name.text,line:z(n,a)})}l.forEachChild(a,U),y&&u.pop(),h&&x.pop()}return U(n),{filePath:t,symbols:i,imports:d,calls:f,heritage:c}}import{createHash as De}from"node:crypto";import{dirname as Ge,extname as Ke,normalize as ze,posix as Be}from"node:path";var R=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 We(s,e,t,r){return De("sha256").update(`${s}:${e}:${t}:${r}`).digest("hex").slice(0,24)}function w(s){return ze(s).replaceAll("\\","/")}function W(s,e,t){if(!e.startsWith("."))return;let r=w(Be.join(Ge(s),e)),o=Ke(r),n;if(o===".js"){let i=r.slice(0,-3);n=[`${i}.ts`,`${i}.tsx`,`${i}.js`,`${i}.jsx`]}else if(o===".mjs"){let i=r.slice(0,-4);n=[`${i}.mts`,`${i}.ts`,`${i}.mjs`]}else if(o===".cjs"){let i=r.slice(0,-4);n=[`${i}.cts`,`${i}.ts`,`${i}.cjs`]}else o?n=[r]:n=[`${r}.ts`,`${r}.tsx`,`${r}.mts`,`${r}.cts`,`${r}.js`,`${r}.jsx`,`${r}.mjs`,`${r}.cjs`,`${r}/index.ts`,`${r}/index.tsx`,`${r}/index.js`,`${r}/index.jsx`];return n.find(i=>t.has(w(i)))}var T=class{build(e,t){let r=new R,o=new Map;for(let i of t)for(let d of i.symbols)r.addSymbol(d),d.type==="file"&&o.set(w(i.filePath),d);let n=new Set(o.keys());for(let i of t){let d=w(i.filePath),f=o.get(d);if(f){for(let c of i.symbols)c.id!==f.id&&r.addEdge(this.edge(e,f.id,"DEFINES",c.id));for(let c of i.imports){let m=W(d,c.source,n);if(!m)continue;let u=o.get(m);u&&r.addEdge(this.edge(e,f.id,"IMPORTS",u.id,{source:c.source}))}for(let c of i.heritage){let m=r.findByName(e,c.targetName).filter(u=>u.type==="class"||u.type==="interface");for(let u of m)r.addEdge(this.edge(e,c.fromId,c.type,u.id))}for(let c of i.calls){if(!c.callerId)continue;let m=this.resolveCallTargets(e,i,c.calleeName,c.qualifier,t,r,n);for(let u of m)u.id!==c.callerId&&r.addEdge(this.edge(e,c.callerId,"CALLS",u.id,{line:c.line,qualifier:c.qualifier}))}}}return r}resolveCallTargets(e,t,r,o,n,i,d){let f=w(t.filePath),c=i.findByName(e,r).filter(m=>w(m.filePath)===f);if(!o&&c.length)return c;if(o)for(let m of t.imports){if(!m.bindings.find(b=>b.kind==="namespace"&&b.localName===o))continue;let x=W(f,m.source,d);if(x)return i.findByName(e,r).filter(b=>w(b.filePath)===x)}for(let m of t.imports){let u=m.bindings.find(E=>E.localName===r);if(!u)continue;let x=W(f,m.source,d);if(!x)continue;let b=u.importedName==="default"?r:u.importedName,L=i.findByName(e,b).filter(E=>w(E.filePath)===x);if(L.length)return L}return i.findByName(e,r).filter(m=>m.type==="function"||m.type==="method")}edge(e,t,r,o,n){return{id:We(e,t,r,o),projectId:e,from:t,to:o,type:r,metadata:n}}};var _=class{async index(e,t,r={}){let o=await K(t);r.onProgress?.({phase:"scan",current:o.length,total:o.length});let n=[],i=0;for(let f of o)try{n.push(await B(e,t,f))}catch(c){console.warn(`[indexer] skipped ${f}:`,c instanceof Error?c.message:c)}finally{i+=1,r.onProgress?.({phase:"parse",current:i,total:o.length,file:f})}let d=new T().build(e,n);return{files:n.length,symbols:d.allSymbols(e).length,edges:d.allEdges(e).length,graph:d}}};function ee(s,e){let t=s.allSymbols(e),r=s.allEdges(e),o=i=>t.filter(d=>d.type===i).length,n=i=>r.filter(d=>d.type===i).length;return{files:o("file"),classes:o("class"),interfaces:o("interface"),functions:o("function"),methods:o("method"),properties:o("property"),routes:o("route"),symbols:t.length,edges:r.length,calls:n("CALLS"),callReferences:n("CALL_REFERENCE"),imports:n("IMPORTS"),inherits:n("INHERITS"),implements:n("IMPLEMENTS"),usesType:n("USES_TYPE"),tests:n("TESTS"),routeEdges:n("ROUTE"),writes:n("WRITES")}}import{execFile as He}from"node:child_process";import{promisify as Ue}from"node:util";var Nr=Ue(He);var j=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 S from"typescript";import*as p from"typescript";var Us=new Set([p.SyntaxKind.EqualsToken,p.SyntaxKind.PlusEqualsToken,p.SyntaxKind.MinusEqualsToken,p.SyntaxKind.AsteriskEqualsToken,p.SyntaxKind.SlashEqualsToken,p.SyntaxKind.PercentEqualsToken,p.SyntaxKind.QuestionQuestionEqualsToken,p.SyntaxKind.AmpersandAmpersandEqualsToken,p.SyntaxKind.BarBarEqualsToken]);import{homedir as ht}from"node:os";import{join as yt}from"node:path";import{DeleteObjectCommand as Ye,GetObjectCommand as qe,HeadObjectCommand as Je,ListObjectsV2Command as Qe,PutObjectCommand as Ze,S3Client as Xe}from"@aws-sdk/client-s3";import{getSignedUrl as et}from"@aws-sdk/s3-request-presigner";var F=class{name="huggingface";client;bucket;constructor(e){this.bucket=e.bucket,this.client=new Xe({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 o=typeof t=="string"?Buffer.from(t,"utf8"):t;await this.client.send(new Ze({Bucket:this.bucket,Key:e,Body:o,ContentType:r}))}async get(e){let t=await et(this.client,new qe({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 Je({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 Ye({Bucket:this.bucket,Key:e}))}async list(e=""){let t=[],r;do{let o=await this.client.send(new Qe({Bucket:this.bucket,Prefix:e||void 0,ContinuationToken:r}));for(let n of o.Contents??[])n.Key&&t.push({key:n.Key,size:n.Size,updatedAt:n.LastModified?.toISOString()});r=o.IsTruncated?o.NextContinuationToken:void 0}while(r);return t}};import{access as te,mkdir as tt,readFile as rt,readdir as st,rm as ot,stat as re,writeFile as nt}from"node:fs/promises";import{dirname as it,join as at,relative as se,resolve as ct}from"node:path";var A=class{constructor(e){this.root=e}root;name="local";path(e){let t=e.replace(/^\/+/,"");return ct(this.root,t)}async put(e,t){let r=this.path(e);await tt(it(r),{recursive:!0}),await nt(r,t)}async get(e){try{return await rt(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 te(this.path(e)),!0}catch{return!1}}async delete(e){await ot(this.path(e),{force:!0})}async list(e=""){let t=this.path(e),r=[];try{await te(t)}catch{return r}let o=async i=>{let d=await st(i,{withFileTypes:!0});for(let f of d){let c=at(i,f.name);if(f.isDirectory()){await o(c);continue}let m=await re(c);r.push({key:se(this.root,c),size:m.size,updatedAt:m.mtime.toISOString()})}},n=await re(t);return n.isDirectory()?await o(t):r.push({key:se(this.root,t),size:n.size,updatedAt:n.mtime.toISOString()}),r}};import{DeleteObjectCommand as lt,GetObjectCommand as dt,HeadObjectCommand as pt,ListObjectsV2Command as ut,PutObjectCommand as mt,S3Client as ft}from"@aws-sdk/client-s3";import{getSignedUrl as gt}from"@aws-sdk/s3-request-presigner";var M=class{name;client;bucket;constructor(e){this.name=e.name??"s3",this.bucket=e.bucket,this.client=new ft({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 o=typeof t=="string"?Buffer.from(t,"utf8"):t;await this.client.send(new mt({Bucket:this.bucket,Key:e,Body:o,ContentType:r}))}async get(e){let t=await gt(this.client,new dt({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 pt({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 lt({Bucket:this.bucket,Key:e}))}async list(e=""){let t=[],r;do{let o=await this.client.send(new ut({Bucket:this.bucket,Prefix:e||void 0,ContinuationToken:r}));for(let n of o.Contents??[])n.Key&&t.push({key:n.Key,size:n.Size,updatedAt:n.LastModified?.toISOString()});r=o.IsTruncated?o.NextContinuationToken:void 0}while(r);return t}};function H(s,e){return console.warn(e),new A(s)}function oe(s){let e=s.localRoot??yt(ht(),".toolnet-memory","storage");if(s.provider==="r2"){let t=s.r2;return t?.accountId&&t.bucket&&t.accessKeyId&&t.secretAccessKey?new M({name:"r2",endpoint:`https://${t.accountId}.r2.cloudflarestorage.com`,region:"auto",bucket:t.bucket,forcePathStyle:!0,accessKeyId:t.accessKeyId,secretAccessKey:t.secretAccessKey}):H(e,"[storage] Cloudflare R2 credentials missing. Using local fallback.")}if(s.provider==="s3"){let t=s.s3;return t?.bucket&&t.accessKeyId&&t.secretAccessKey?new M({name:"s3",endpoint:t.endpoint,region:t.region??"us-east-1",bucket:t.bucket,forcePathStyle:t.forcePathStyle??!1,accessKeyId:t.accessKeyId,secretAccessKey:t.secretAccessKey}):H(e,"[storage] S3 credentials missing. Using local fallback.")}if(s.provider==="huggingface"){let t=s.huggingface;return t?.namespace&&t.bucket&&t.accessKeyId&&t.secretAccessKey?new F({namespace:t.namespace,bucket:t.bucket,accessKeyId:t.accessKeyId,secretAccessKey:t.secretAccessKey}):H(e,"[storage] Hugging Face credentials missing. Using local fallback.")}return new A(e)}function ne(s){let e=s.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 ie(s){let e=s.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 o=e.match(/^(projects\/[^/]+\/)memories\/(.+)$/);return o?`${o[1]}memory/records/${o[2]}`:(o=e.match(/^(projects\/[^/]+\/)vectors\/(.+)$/),o?`${o[1]}memory/vectors/${o[2]}`:(o=e.match(/^(projects\/[^/]+\/)graph\/(.+)$/),o?`${o[1]}code/graph/${o[2]}`:e))}var $=class{constructor(e,t,r,o){this.provider=e;this.name=e.name,this.projectId=t,this.projectName=r,this.folder=ne(o??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,o=await this.provider.getText(e);if(o){let i;try{i=JSON.parse(o)}catch(d){throw new Error(`Invalid remote ToolNet project manifest at ${e}: ${d instanceof Error?d.message:String(d)}`)}if(typeof i.id=="string"&&i.id!==this.projectId)throw new Error(["ToolNet remote project namespace collision.",`Remote folder: ${this.targetPrefix}`,`Existing project id: ${i.id}`,`Current project id: ${this.projectId}`,"Refusing to mix data from two projects."].join(" "));typeof i.createdAt=="string"&&(r=i.createdAt)}let n={version:1,id:this.projectId,name:this.projectName,remote:this.folder,createdAt:r,updatedAt:t};await this.provider.put(e,JSON.stringify(n,null,2)+`
4
- `,"application/json")}async ensureRegistered(){return this.registryPromise||(this.registryPromise=this.registerProject()),this.registryPromise}key(e){if(e=ie(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))}};async function St(){let s=V(),e=new I().detect(),t=oe({provider:s.storage.provider,huggingface:s.storage.huggingface,localRoot:s.storage.localRoot}),r=new $(t,e.id,e.name,e.remote??e.name),o=await new _().index(e.id,e.rootPath),n=ee(o.graph,e.id);await new j(r).save({version:1,projectId:e.id,updatedAt:new Date().toISOString(),files:o.files,symbols:o.graph.allSymbols(e.id),edges:o.graph.allEdges(e.id)}),console.log({ok:!0,storage:r.name,...n})}St().catch(s=>{console.error(s),process.exit(1)});
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 me(){let s=process.env.TOOLNET_GLOBAL_ENV??pe(de(),".config","toolnet-memory",".env");if(!ce(s))return;let e=le(s,"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 o=r.indexOf("=");if(o<=0)continue;let n=r.slice(0,o).trim();/^[A-Za-z_][A-Za-z0-9_]*$/.test(n)&&process.env[n]===void 0&&(process.env[n]=ue(r.slice(o+1)))}}me();function v(s,e){return s===void 0?e:["1","true","yes","on"].includes(s.toLowerCase())}function k(s,e){if(!s)return e;let t=Number(s);return Number.isFinite(t)?t:e}function V(){return{memory:{autoCapture:v(process.env.MEMORY_AUTO_CAPTURE,!0),autoRetrieve:v(process.env.MEMORY_AUTO_RETRIEVE,!0),autoSummarize:v(process.env.MEMORY_AUTO_SUMMARIZE,!0),autoSync:v(process.env.MEMORY_AUTO_SYNC,!0)},retrieval:{maxCandidates:k(process.env.MEMORY_MAX_CANDIDATES,50),rerankTop:k(process.env.MEMORY_RERANK_TOP,10),finalContext:k(process.env.MEMORY_FINAL_CONTEXT,5),tokenBudget:k(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:v(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:k(process.env.MEMORY_LOCAL_CACHE_MB,200)}}}import{createHash as fe}from"node:crypto";import{existsSync as $,mkdirSync as ge,readFileSync as he,renameSync as ye,writeFileSync as Se}from"node:fs";import{basename as be,dirname as N,join as O,parse as q,resolve as P}from"node:path";var Q=".toolnet",Ce="project.json";function xe(s){return fe("sha256").update(s).digest("hex").slice(0,16)}function G(s){return O(s,Q,Ce)}function Ee(s){return $(G(s))}function we(s,e){let t=P(s),r=q(t).root;for(;;){if(Ee(t))return t;if(t===r||e&&t===P(e))break;let o=N(t);if(o===t)break;t=o}return null}function ve(s){let e=P(s),t=q(e).root,r=[".git","package.json","pyproject.toml","Cargo.toml","go.mod","composer.json"];for(;;){if(r.some(n=>$(O(e,n))))return e;if(e===t)break;let o=N(e);if(o===e)break;e=o}return P(s)}function ke(s){let e;try{e=JSON.parse(he(s,"utf8"))}catch(o){throw new Error(`Invalid ToolNet project manifest: ${s}: ${o instanceof Error?o.message:String(o)}`)}if(!e||typeof e!="object")throw new Error(`Invalid ToolNet project manifest: ${s}`);let t=e;if(typeof t.id!="string"||!t.id.trim())throw new Error(`ToolNet project manifest is missing id: ${s}`);if(typeof t.name!="string"||!t.name.trim())throw new Error(`ToolNet project manifest is missing name: ${s}`);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:N(N(s)),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 Y(s,e){let t=O(s,Q);ge(t,{recursive:!0});let r=G(s),o=`${r}.tmp-${process.pid}`;Se(o,JSON.stringify(e,null,2)+`
3
+ `,{encoding:"utf8",mode:384}),ye(o,r)}function J(s,e){return{id:s.id,name:s.name,remote:s.remote,rootPath:e,createdAt:s.createdAt,updatedAt:s.updatedAt,graphVersion:s.graphVersion,memoryVersion:s.memoryVersion,metadata:s.metadata}}var I=class{detect(e=process.cwd()){let t=P(e),r=ve(t),n=[".git","package.json","pyproject.toml","Cargo.toml","go.mod","composer.json"].some(m=>$(O(r,m))),i=we(t,n?r:void 0);if(i){let m=G(i),u=ke(m);return u.rootPath!==i&&(u.rootPath=i,u.updatedAt=new Date().toISOString(),Y(i,u)),J(u,i)}let d=new Date().toISOString(),f=be(r),c={version:1,id:xe(r),name:f,remote:f,rootPath:r,createdAt:d,updatedAt:d,graphVersion:0,memoryVersion:0};return Y(r,c),J(c,r)}};import{readdir as je}from"node:fs/promises";import{extname as Le,join as Me,relative as Ne}from"node:path";import{basename as Re}from"node:path";var Te=new Set([".env",".env.local",".env.production",".env.development","id_rsa","id_ed25519","credentials","credentials.json","service-account.json"]),Ae=new Set([".pem",".key",".p12",".pfx"]);function Z(s){let e=Re(s).toLowerCase();if(Te.has(e))return!0;for(let t of Ae)if(e.endsWith(t))return!0;return!1}var Ie=new Set([".ts",".tsx",".js",".jsx",".mjs",".cjs",".mts",".cts"]),Oe=new Set(["node_modules",".git","dist","build","coverage",".next",".cache",".toolnet-memory"]);async function K(s){let e=[];async function t(r){let o=await je(r,{withFileTypes:!0});for(let n of o){if(Oe.has(n.name))continue;let i=Me(r,n.name);if(n.isDirectory()){await t(i);continue}Ie.has(Le(n.name))&&(Z(i)||e.push(Ne(s,i)))}}return await t(s),e.sort()}import{readFile as _e}from"node:fs/promises";import{join as Fe}from"node:path";import{createHash as De}from"node:crypto";import*as l from"typescript";function X(s,e){return De("sha256").update(`${s}:${e}`).digest("hex").slice(0,24)}function z(s,e){return s.getLineAndCharacterOfPosition(e.getStart(s)).line+1}async function B(s,e,t){let r=Fe(e,t),o=await _e(r,"utf8"),n=l.createSourceFile(t,o,l.ScriptTarget.Latest,!0,t.endsWith(".tsx")||t.endsWith(".jsx")?l.ScriptKind.TSX:l.ScriptKind.TS),i=[],d=[],f=[],c=[],m={id:X(s,`file:${t}`),projectId:s,name:t,qualifiedName:t,type:"file",filePath:t,startLine:1,endLine:n.getLineAndCharacterOfPosition(n.end).line+1};i.push(m);let u=[],C=[];function b(a,y,h,g){let x=X(s,`${t}:${h}:${g??y}:${a.pos}`);return i.push({id:x,projectId:s,name:y,qualifiedName:g??y,type:h,filePath:t,startLine:z(n,a),endLine:n.getLineAndCharacterOfPosition(a.end).line+1}),x}function M(a){if(!l.isStringLiteral(a.moduleSpecifier))return;let y=[],h=a.importClause;if(h?.name&&y.push({localName:h.name.text,importedName:"default",kind:"default"}),h?.namedBindings&&l.isNamedImports(h.namedBindings))for(let g of h.namedBindings.elements)y.push({localName:g.name.text,importedName:g.propertyName?.text??g.name.text,kind:"named"});h?.namedBindings&&l.isNamespaceImport(h.namedBindings)&&y.push({localName:h.namedBindings.name.text,importedName:"*",kind:"namespace"}),d.push({source:a.moduleSpecifier.text,bindings:y})}function w(a,y){for(let h of a.heritageClauses??[])for(let g of h.types){let x=g.expression.getText(n).split(".").at(-1)??"";x&&c.push({fromId:y,targetName:x,type:h.token===l.SyntaxKind.ExtendsKeyword?"INHERITS":"IMPLEMENTS"})}}function H(a){let y=!1,h=!1;if(l.isImportDeclaration(a)&&M(a),l.isInterfaceDeclaration(a)&&b(a,a.name.text,"interface",a.name.text),l.isClassDeclaration(a)&&a.name){let g=b(a,a.name.text,"class",a.name.text);w(a,g),C.push({id:g,name:a.name.text}),h=!0}if(l.isFunctionDeclaration(a)&&a.name){let g=b(a,a.name.text,"function",a.name.text);u.push(g),y=!0}if(l.isMethodDeclaration(a)&&a.name){let g=a.name.getText(n),x=C.at(-1)?.name,ae=b(a,g,"method",x?`${x}.${g}`:g);u.push(ae),y=!0}if(l.isCallExpression(a)&&(l.isIdentifier(a.expression)&&f.push({callerId:u.at(-1),calleeName:a.expression.text,line:z(n,a)}),l.isPropertyAccessExpression(a.expression))){let g=a.expression;f.push({callerId:u.at(-1),qualifier:g.expression.getText(n),calleeName:g.name.text,line:z(n,a)})}l.forEachChild(a,H),y&&u.pop(),h&&C.pop()}return H(n),{filePath:t,symbols:i,imports:d,calls:f,heritage:c}}import{createHash as $e}from"node:crypto";import{dirname as Ge,extname as Ke,normalize as ze,posix as Be}from"node:path";var R=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 We(s,e,t,r){return $e("sha256").update(`${s}:${e}:${t}:${r}`).digest("hex").slice(0,24)}function E(s){return ze(s).replaceAll("\\","/")}function W(s,e,t){if(!e.startsWith("."))return;let r=E(Be.join(Ge(s),e)),o=Ke(r),n;if(o===".js"){let i=r.slice(0,-3);n=[`${i}.ts`,`${i}.tsx`,`${i}.js`,`${i}.jsx`]}else if(o===".mjs"){let i=r.slice(0,-4);n=[`${i}.mts`,`${i}.ts`,`${i}.mjs`]}else if(o===".cjs"){let i=r.slice(0,-4);n=[`${i}.cts`,`${i}.ts`,`${i}.cjs`]}else o?n=[r]:n=[`${r}.ts`,`${r}.tsx`,`${r}.mts`,`${r}.cts`,`${r}.js`,`${r}.jsx`,`${r}.mjs`,`${r}.cjs`,`${r}/index.ts`,`${r}/index.tsx`,`${r}/index.js`,`${r}/index.jsx`];return n.find(i=>t.has(E(i)))}var T=class{build(e,t){let r=new R,o=new Map;for(let i of t)for(let d of i.symbols)r.addSymbol(d),d.type==="file"&&o.set(E(i.filePath),d);let n=new Set(o.keys());for(let i of t){let d=E(i.filePath),f=o.get(d);if(f){for(let c of i.symbols)c.id!==f.id&&r.addEdge(this.edge(e,f.id,"DEFINES",c.id));for(let c of i.imports){let m=W(d,c.source,n);if(!m)continue;let u=o.get(m);u&&r.addEdge(this.edge(e,f.id,"IMPORTS",u.id,{source:c.source}))}for(let c of i.heritage){let m=r.findByName(e,c.targetName).filter(u=>u.type==="class"||u.type==="interface");for(let u of m)r.addEdge(this.edge(e,c.fromId,c.type,u.id))}for(let c of i.calls){if(!c.callerId)continue;let m=this.resolveCallTargets(e,i,c.calleeName,c.qualifier,t,r,n);for(let u of m)u.id!==c.callerId&&r.addEdge(this.edge(e,c.callerId,"CALLS",u.id,{line:c.line,qualifier:c.qualifier}))}}}return r}resolveCallTargets(e,t,r,o,n,i,d){let f=E(t.filePath),c=i.findByName(e,r).filter(m=>E(m.filePath)===f);if(!o&&c.length)return c;if(o)for(let m of t.imports){if(!m.bindings.find(b=>b.kind==="namespace"&&b.localName===o))continue;let C=W(f,m.source,d);if(C)return i.findByName(e,r).filter(b=>E(b.filePath)===C)}for(let m of t.imports){let u=m.bindings.find(w=>w.localName===r);if(!u)continue;let C=W(f,m.source,d);if(!C)continue;let b=u.importedName==="default"?r:u.importedName,M=i.findByName(e,b).filter(w=>E(w.filePath)===C);if(M.length)return M}return i.findByName(e,r).filter(m=>m.type==="function"||m.type==="method")}edge(e,t,r,o,n){return{id:We(e,t,r,o),projectId:e,from:t,to:o,type:r,metadata:n}}};var _=class{async index(e,t,r={}){let o=await K(t);r.onProgress?.({phase:"scan",current:o.length,total:o.length});let n=[],i=0;for(let f of o)try{n.push(await B(e,t,f))}catch(c){console.warn(`[indexer] skipped ${f}:`,c instanceof Error?c.message:c)}finally{i+=1,r.onProgress?.({phase:"parse",current:i,total:o.length,file:f})}let d=new T().build(e,n);return{files:n.length,symbols:d.allSymbols(e).length,edges:d.allEdges(e).length,graph:d}}};function ee(s,e){let t=s.allSymbols(e),r=s.allEdges(e),o=i=>t.filter(d=>d.type===i).length,n=i=>r.filter(d=>d.type===i).length;return{files:o("file"),classes:o("class"),interfaces:o("interface"),functions:o("function"),methods:o("method"),properties:o("property"),routes:o("route"),symbols:t.length,edges:r.length,calls:n("CALLS"),callReferences:n("CALL_REFERENCE"),imports:n("IMPORTS"),inherits:n("INHERITS"),implements:n("IMPLEMENTS"),usesType:n("USES_TYPE"),tests:n("TESTS"),routeEdges:n("ROUTE"),writes:n("WRITES")}}import{execFile as Ue}from"node:child_process";import{promisify as He}from"node:util";var Nr=He(Ue);var A=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{DatabaseSync as xs}from"node:sqlite";import*as S from"typescript";import*as p from"typescript";var zs=new Set([p.SyntaxKind.EqualsToken,p.SyntaxKind.PlusEqualsToken,p.SyntaxKind.MinusEqualsToken,p.SyntaxKind.AsteriskEqualsToken,p.SyntaxKind.SlashEqualsToken,p.SyntaxKind.PercentEqualsToken,p.SyntaxKind.QuestionQuestionEqualsToken,p.SyntaxKind.AmpersandAmpersandEqualsToken,p.SyntaxKind.BarBarEqualsToken]);import{homedir as ht}from"node:os";import{join as yt}from"node:path";import{DeleteObjectCommand as Ye,GetObjectCommand as Je,HeadObjectCommand as qe,ListObjectsV2Command as Qe,PutObjectCommand as Ze,S3Client as Xe}from"@aws-sdk/client-s3";import{getSignedUrl as et}from"@aws-sdk/s3-request-presigner";var F=class{name="huggingface";client;bucket;constructor(e){this.bucket=e.bucket,this.client=new Xe({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 o=typeof t=="string"?Buffer.from(t,"utf8"):t;await this.client.send(new Ze({Bucket:this.bucket,Key:e,Body:o,ContentType:r}))}async get(e){let t=await et(this.client,new Je({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 qe({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 Ye({Bucket:this.bucket,Key:e}))}async list(e=""){let t=[],r;do{let o=await this.client.send(new Qe({Bucket:this.bucket,Prefix:e||void 0,ContinuationToken:r}));for(let n of o.Contents??[])n.Key&&t.push({key:n.Key,size:n.Size,updatedAt:n.LastModified?.toISOString()});r=o.IsTruncated?o.NextContinuationToken:void 0}while(r);return t}};import{access as te,mkdir as tt,readFile as rt,readdir as st,rm as ot,stat as re,writeFile as nt}from"node:fs/promises";import{dirname as it,join as at,relative as se,resolve as ct}from"node:path";var j=class{constructor(e){this.root=e}root;name="local";path(e){let t=e.replace(/^\/+/,"");return ct(this.root,t)}async put(e,t){let r=this.path(e);await tt(it(r),{recursive:!0}),await nt(r,t)}async get(e){try{return await rt(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 te(this.path(e)),!0}catch{return!1}}async delete(e){await ot(this.path(e),{force:!0})}async list(e=""){let t=this.path(e),r=[];try{await te(t)}catch{return r}let o=async i=>{let d=await st(i,{withFileTypes:!0});for(let f of d){let c=at(i,f.name);if(f.isDirectory()){await o(c);continue}let m=await re(c);r.push({key:se(this.root,c),size:m.size,updatedAt:m.mtime.toISOString()})}},n=await re(t);return n.isDirectory()?await o(t):r.push({key:se(this.root,t),size:n.size,updatedAt:n.mtime.toISOString()}),r}};import{DeleteObjectCommand as lt,GetObjectCommand as dt,HeadObjectCommand as pt,ListObjectsV2Command as ut,PutObjectCommand as mt,S3Client as ft}from"@aws-sdk/client-s3";import{getSignedUrl as gt}from"@aws-sdk/s3-request-presigner";var L=class{name;client;bucket;constructor(e){this.name=e.name??"s3",this.bucket=e.bucket,this.client=new ft({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 o=typeof t=="string"?Buffer.from(t,"utf8"):t;await this.client.send(new mt({Bucket:this.bucket,Key:e,Body:o,ContentType:r}))}async get(e){let t=await gt(this.client,new dt({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 pt({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 lt({Bucket:this.bucket,Key:e}))}async list(e=""){let t=[],r;do{let o=await this.client.send(new ut({Bucket:this.bucket,Prefix:e||void 0,ContinuationToken:r}));for(let n of o.Contents??[])n.Key&&t.push({key:n.Key,size:n.Size,updatedAt:n.LastModified?.toISOString()});r=o.IsTruncated?o.NextContinuationToken:void 0}while(r);return t}};function U(s,e){return console.warn(e),new j(s)}function oe(s){let e=s.localRoot??yt(ht(),".toolnet-memory","storage");if(s.provider==="r2"){let t=s.r2;return t?.accountId&&t.bucket&&t.accessKeyId&&t.secretAccessKey?new L({name:"r2",endpoint:`https://${t.accountId}.r2.cloudflarestorage.com`,region:"auto",bucket:t.bucket,forcePathStyle:!0,accessKeyId:t.accessKeyId,secretAccessKey:t.secretAccessKey}):U(e,"[storage] Cloudflare R2 credentials missing. Using local fallback.")}if(s.provider==="s3"){let t=s.s3;return t?.bucket&&t.accessKeyId&&t.secretAccessKey?new L({name:"s3",endpoint:t.endpoint,region:t.region??"us-east-1",bucket:t.bucket,forcePathStyle:t.forcePathStyle??!1,accessKeyId:t.accessKeyId,secretAccessKey:t.secretAccessKey}):U(e,"[storage] S3 credentials missing. Using local fallback.")}if(s.provider==="huggingface"){let t=s.huggingface;return t?.namespace&&t.bucket&&t.accessKeyId&&t.secretAccessKey?new F({namespace:t.namespace,bucket:t.bucket,accessKeyId:t.accessKeyId,secretAccessKey:t.secretAccessKey}):U(e,"[storage] Hugging Face credentials missing. Using local fallback.")}return new j(e)}function ne(s){let e=s.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 ie(s){let e=s.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 o=e.match(/^(projects\/[^/]+\/)memories\/(.+)$/);return o?`${o[1]}memory/records/${o[2]}`:(o=e.match(/^(projects\/[^/]+\/)vectors\/(.+)$/),o?`${o[1]}memory/vectors/${o[2]}`:(o=e.match(/^(projects\/[^/]+\/)graph\/(.+)$/),o?`${o[1]}code/graph/${o[2]}`:e))}var D=class{constructor(e,t,r,o){this.provider=e;this.name=e.name,this.projectId=t,this.projectName=r,this.folder=ne(o??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,o=await this.provider.getText(e);if(o){let i;try{i=JSON.parse(o)}catch(d){throw new Error(`Invalid remote ToolNet project manifest at ${e}: ${d instanceof Error?d.message:String(d)}`)}if(typeof i.id=="string"&&i.id!==this.projectId)throw new Error(["ToolNet remote project namespace collision.",`Remote folder: ${this.targetPrefix}`,`Existing project id: ${i.id}`,`Current project id: ${this.projectId}`,"Refusing to mix data from two projects."].join(" "));typeof i.createdAt=="string"&&(r=i.createdAt)}let n={version:1,id:this.projectId,name:this.projectName,remote:this.folder,createdAt:r,updatedAt:t};await this.provider.put(e,JSON.stringify(n,null,2)+`
4
+ `,"application/json")}async ensureRegistered(){return this.registryPromise||(this.registryPromise=this.registerProject()),this.registryPromise}key(e){if(e=ie(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))}};async function St(){let s=V(),e=new I().detect(),t=oe({provider:s.storage.provider,huggingface:s.storage.huggingface,localRoot:s.storage.localRoot}),r=new D(t,e.id,e.name,e.remote??e.name),o=await new _().index(e.id,e.rootPath),n=ee(o.graph,e.id);await new A(r).save({version:1,projectId:e.id,updatedAt:new Date().toISOString(),files:o.files,symbols:o.graph.allSymbols(e.id),edges:o.graph.allEdges(e.id)}),console.log({ok:!0,storage:r.name,...n})}St().catch(s=>{console.error(s),process.exit(1)});