/*! AgentDB Browser Bundle v2.0.0-alpha.3.6 | MIT | https://agentdb.ruv.io */ var L=Object.defineProperty;var z=(h,t)=>()=>(h&&(t=h(h=0)),t);var H=(h,t)=>{for(var e in t)L(h,e,{get:t[e],enumerable:!0})};var I={};H(I,{initWASM:()=>j,wasmLoadError:()=>P,wasmModule:()=>p});async function j(){return p||M||(M=(async()=>{try{if(typeof WebAssembly>"u")throw new Error("WebAssembly not supported in this browser");let h=await _();return console.log(`WASM SIMD support: ${h}`),p={flashAttention:N(),hyperbolicAttention:D(),memoryConsolidation:k(),simdSupported:h},console.log("\u2705 WASM attention module loaded"),p}catch(h){return P=h,console.warn("\u26A0\uFE0F WASM loading failed, using fallback:",h.message),p={flashAttention:N(),hyperbolicAttention:D(),memoryConsolidation:k(),simdSupported:!1},p}finally{M=null}})(),M)}async function _(){try{let h=new Uint8Array([0,97,115,109,1,0,0,0,1,5,1,96,0,1,123,3,2,1,0,10,10,1,8,0,253,12,253,12,253,84,11]);return await WebAssembly.instantiate(h)instanceof WebAssembly.Instance}catch{return!1}}function N(){return(h,t,e,o={})=>{let{dim:n=384,numHeads:r=4,blockSize:s=64}=o,i=t.length/n,a=new Float32Array(h.length);for(let c=0;c{let{curvature:o=-1}=e,n=Math.abs(o),r=new Float32Array(t.length/h.length);for(let s=0;s{let{threshold:e=.8,maxClusters:o=10}=t,n=[],r=new Set;for(let s=0;se&&(i.push(h[c]),r.add(c))}let a=new Float32Array(h[s].length);for(let c of i)for(let u=0;u=o)break}return n}}var p,M,P,W=z(()=>{"use strict";p=null,M=null,P=null});var y=class{config;codebook=null;trained=!1;constructor(t){if(this.config={dimension:t.dimension,numSubvectors:t.numSubvectors,numCentroids:t.numCentroids,maxIterations:t.maxIterations||50,convergenceThreshold:t.convergenceThreshold||1e-4},this.config.dimension%this.config.numSubvectors!==0)throw new Error(`Dimension ${this.config.dimension} must be divisible by numSubvectors ${this.config.numSubvectors}`)}async train(t){if(t.length===0)throw new Error("Training requires at least one vector");let e=this.config.dimension/this.config.numSubvectors,o=[];console.log(`[PQ] Training ${this.config.numSubvectors} subvectors with ${this.config.numCentroids} centroids each...`);for(let n=0;nc.slice(r,s)),a=await this.kMeans(i,this.config.numCentroids);o.push(...a),((n+1)%4===0||n===this.config.numSubvectors-1)&&console.log(`[PQ] Trained ${n+1}/${this.config.numSubvectors} subvectors`)}this.codebook={subvectorDim:e,numSubvectors:this.config.numSubvectors,numCentroids:this.config.numCentroids,centroids:o},this.trained=!0,console.log("[PQ] Training complete")}async kMeans(t,e){let o=t[0].length,n=t.length,r=this.kMeansPlusPlus(t,e),s=new Uint32Array(n),i=1/0;for(let a=0;anew Float32Array(o));for(let d=0;d0)for(let l=0;lthis.compress(e))}getCompressionRatio(){let t=this.config.dimension*4,e=this.config.numSubvectors+4;return t/e}exportCodebook(){if(!this.codebook)throw new Error("No codebook to export");return JSON.stringify({config:this.config,codebook:{subvectorDim:this.codebook.subvectorDim,numSubvectors:this.codebook.numSubvectors,numCentroids:this.codebook.numCentroids,centroids:this.codebook.centroids.map(t=>Array.from(t))}})}importCodebook(t){let e=JSON.parse(t);this.config=e.config,this.codebook={subvectorDim:e.codebook.subvectorDim,numSubvectors:e.codebook.numSubvectors,numCentroids:e.codebook.numCentroids,centroids:e.codebook.centroids.map(o=>new Float32Array(o))},this.trained=!0}squaredDistance(t,e){let o=0;for(let n=0;n0&&(this.items[0]=e,this.bubbleDown(0)),t}peek(){var t;return(t=this.items[0])==null?void 0:t.item}size(){return this.items.length}bubbleUp(t){for(;t>0;){let e=Math.floor((t-1)/2);if(this.items[t].priority>=this.items[e].priority)break;[this.items[t],this.items[e]]=[this.items[e],this.items[t]],t=e}}bubbleDown(t){for(;;){let e=2*t+1,o=2*t+2,n=t;if(en;a--)i=this.searchLayer(t,i,1,a)[0];for(let a=Math.min(n,this.nodes.get(s).level);a>=0;a--){let c=this.searchLayer(t,i,this.config.efConstruction,a),u=a===0?this.config.M*2:this.config.M,m=this.selectNeighbors(t,c,u);for(let d of m){this.connect(o,d,a),this.connect(d,o,a);let l=this.nodes.get(d),f=l.connections.get(a);if(f.length>u){let b=this.selectNeighbors(l.vector,f,u);l.connections.set(a,b)}}i=c[0]}return n>this.nodes.get(this.entryPoint).level&&(this.entryPoint=o),this.nodes.set(o,r),o}search(t,e,o){if(this.entryPoint===null)return[];o=o||Math.max(this.config.efSearch,e);let n=this.entryPoint,r=n;for(let i=this.nodes.get(n).level;i>0;i--)r=this.searchLayer(t,r,1,i)[0];return this.searchLayer(t,r,o,0).slice(0,e).map(i=>({id:i,distance:this.distance(t,this.nodes.get(i).vector),vector:this.nodes.get(i).vector}))}searchLayer(t,e,o,n){let r=new Set,s=new S,i=new S,a=this.distance(t,this.nodes.get(e).vector);for(s.push(e,a),i.push(e,-a),r.add(e);s.size()>0;){let u=s.pop(),m=-i.peek();if(this.distance(t,this.nodes.get(u).vector)>m)break;let l=this.nodes.get(u).connections.get(n)||[];for(let f of l){if(r.has(f))continue;r.add(f);let b=this.distance(t,this.nodes.get(f).vector),g=-i.peek();(bo&&i.pop())}}let c=[];for(;i.size()>0;)c.unshift(i.pop());return c}selectNeighbors(t,e,o){return e.length<=o?e:e.map(r=>({id:r,distance:this.distance(t,this.nodes.get(r).vector)})).sort((r,s)=>r.distance-s.distance).slice(0,o).map(r=>r.id)}connect(t,e,o){let r=this.nodes.get(t).connections.get(o);r.includes(e)||r.push(e)}randomLevel(){let t=0;for(;Math.random()a.level)),e=0;for(let a of this.nodes.values())for(let c of a.connections.values())e+=c.length;let o=e/this.nodes.size,n=this.config.dimension*4,r=o*4,i=this.nodes.size*(n+r+100);return{numNodes:this.nodes.size,numLayers:t+1,avgConnections:o,entryPointLevel:this.entryPoint?this.nodes.get(this.entryPoint).level:0,memoryBytes:i}}export(){let t={config:this.config,entryPoint:this.entryPoint,currentId:this.currentId,nodes:Array.from(this.nodes.entries()).map(([e,o])=>({id:e,vector:Array.from(o.vector),level:o.level,connections:Array.from(o.connections.entries())}))};return JSON.stringify(t)}import(t){let e=JSON.parse(t);this.config=e.config,this.entryPoint=e.entryPoint,this.currentId=e.currentId,this.nodes.clear();for(let o of e.nodes){let n={id:o.id,vector:new Float32Array(o.vector),level:o.level,connections:new Map(o.connections)};this.nodes.set(o.id,n)}}clear(){this.nodes.clear(),this.entryPoint=null,this.currentId=0}size(){return this.nodes.size}};function q(h){return new A({dimension:h,M:16,efConstruction:200,efSearch:50})}function G(h){return new A({dimension:h,M:8,efConstruction:100,efSearch:30})}function O(h){return new A({dimension:h,M:32,efConstruction:400,efSearch:100})}var w=class{config;nodes=new Map;edges=[];attentionWeights=new Map;constructor(t={}){this.config={hiddenDim:t.hiddenDim||64,numHeads:t.numHeads||4,dropout:t.dropout||.1,learningRate:t.learningRate||.01,attentionType:t.attentionType||"gat"}}addNode(t,e){this.nodes.set(t,{id:t,features:e,neighbors:[]})}addEdge(t,e,o=1){this.edges.push({from:t,to:e,weight:o});let n=this.nodes.get(t),r=this.nodes.get(e);n&&!n.neighbors.includes(e)&&n.neighbors.push(e),r&&!r.neighbors.includes(t)&&r.neighbors.push(t)}graphAttention(t){let e=this.nodes.get(t);if(!e)throw new Error(`Node ${t} not found`);let o=e.neighbors;if(o.length===0)return e.features;let n=Math.floor(this.config.hiddenDim/this.config.numHeads),r=new Float32Array(this.config.hiddenDim);for(let s=0;s0)for(let u=0;u0?r[s]:.01*r[s];return r}computeAttentionScore(t,e,o){let n=0,r=Math.min(t.length,e.length);for(let s=0;sc)),s=0,i=-1/0;for(let a=0;ai&&(i=e[a].score,s=a);for(n.push(e[s].id),r.delete(s);n.length0;){let a=-1/0,c=-1;for(let u of r){let m=e[u],d=this.similarity(t,m.vector),l=-1/0;for(let b of n){let g=e.find(E=>E.id===b),T=this.similarity(m.vector,g.vector);l=Math.max(l,T)}let f=this.config.lambda*d-(1-this.config.lambda)*l;f>a&&(a=f,c=u)}if(c!==-1)n.push(e[c].id),r.delete(c);else break}return n}similarity(t,e){return this.config.metric==="cosine"?this.cosineSimilarity(t,e):1/(1+this.euclideanDistance(t,e))}cosineSimilarity(t,e){let o=0,n=0,r=0;for(let s=0;s=o)return t;let n=t.map(u=>Array.from(u)),r=this.computeMean(n),s=n.map(u=>u.map((m,d)=>m-r[d])),i=this.computeCovariance(s),a=this.powerIteration(i,e);return s.map(u=>{let m=new Float32Array(e);for(let d=0;dr/e)}static computeCovariance(t){let e=t.length,o=t[0].length,n=Array.from({length:o},()=>new Array(o).fill(0));for(let r=0;rMath.random()-.5);for(let a=0;am/u)}r.push(i)}return r}},C=class{static batchCosineSimilarity(t,e){let o=new Float32Array(e.length),n=0;for(let r=0;r{let o=0;for(let r=0;rsetTimeout(t,50));return}this.loadingState="loading";try{if(!this.config.useWASM){this.loadingState="loaded";return}let t=await Promise.resolve().then(()=>(W(),I));this.wasmModule=await t.initWASM(),this.loadingState="loaded"}catch(t){this.loadError=t instanceof Error?t:new Error(String(t)),this.loadingState="error",console.warn("WASM initialization failed, using fallback:",this.loadError.message)}}}async flashAttention(t,e,o){var n;if(await this.initialize(),(n=this.wasmModule)!=null&&n.flashAttention)try{return this.wasmModule.flashAttention(t,e,o,this.config)}catch(r){console.warn("WASM flash attention failed, using fallback:",r)}return this.flashAttentionFallback(t,e,o)}async hyperbolicAttention(t,e){var o;if(await this.initialize(),(o=this.wasmModule)!=null&&o.hyperbolicAttention)try{return this.wasmModule.hyperbolicAttention(t,e,this.config)}catch(n){console.warn("WASM hyperbolic attention failed, using fallback:",n)}return this.hyperbolicAttentionFallback(t,e)}async consolidateMemories(t,e={}){var n;await this.initialize();let o={threshold:.8,maxClusters:10,minClusterSize:1,...e};if((n=this.wasmModule)!=null&&n.memoryConsolidation)try{return this.wasmModule.memoryConsolidation(t,o)}catch(r){console.warn("WASM memory consolidation failed, using fallback:",r)}return this.consolidateMemoriesFallback(t,o)}dispose(){this.wasmModule=null,this.loadingState="idle",this.loadError=null}flashAttentionFallback(t,e,o){let{dimension:n=384}=this.config,r=e.length/n,s=new Float32Array(t.length);for(let i=0;io&&(c.push(t[u]),i.add(u))}if(c.length>=r){let u=new Float32Array(t[a].length);for(let d of c)for(let l=0;l0)for(let d=0;d=n)break}return s}cosineSimilarity(t,e){let o=0,n=0,r=0;for(let i=0;i0?o/s:0}};function U(h){return new v(h)}function $(){return new v({dimension:256,numHeads:2,blockSize:32,useWASM:!0})}function V(){return new v({dimension:768,numHeads:8,blockSize:128,useWASM:!0})}function ot(){return{indexedDB:"indexedDB"in globalThis,broadcastChannel:"BroadcastChannel"in globalThis,webWorkers:typeof globalThis.Worker<"u",wasmSIMD:J(),sharedArrayBuffer:typeof SharedArrayBuffer<"u"}}async function J(){try{if(typeof globalThis.WebAssembly>"u")return!1;let h=new Uint8Array([0,97,115,109,1,0,0,0,1,5,1,96,0,1,123,3,2,1,0,10,10,1,8,0,253,12,253,12,253,84,11]),t=globalThis.WebAssembly;return await t.instantiate(h)instanceof t.Instance}catch{return!1}}var Z={pq:{enabled:!1},hnsw:{enabled:!1},gnn:{enabled:!0,numHeads:2},mmr:{enabled:!0,lambda:.7},svd:{enabled:!1}},K={pq:{enabled:!0,subvectors:8},hnsw:{enabled:!0,M:16},gnn:{enabled:!0,numHeads:4},mmr:{enabled:!0,lambda:.7},svd:{enabled:!1}},Y={pq:{enabled:!0,subvectors:16},hnsw:{enabled:!0,M:32},gnn:{enabled:!0,numHeads:4},mmr:{enabled:!0,lambda:.7},svd:{enabled:!0,targetDim:128}},st={pq:{enabled:!0,subvectors:32},hnsw:{enabled:!0,M:8},gnn:{enabled:!1},mmr:{enabled:!1},svd:{enabled:!0,targetDim:64}},it={pq:{enabled:!1},hnsw:{enabled:!0,M:32,efSearch:100},gnn:{enabled:!1},mmr:{enabled:!1},svd:{enabled:!1}},at={pq:{enabled:!1},hnsw:{enabled:!0,M:48,efConstruction:400},gnn:{enabled:!0,numHeads:8},mmr:{enabled:!0,lambda:.8},svd:{enabled:!1}},ct={major:2,minor:0,patch:0,prerelease:"alpha.2",features:"advanced",full:"2.0.0-alpha.2+advanced"};function lt(h,t,e){var s,i,a;let o=h*t*4;if((s=e.pq)!=null&&s.enabled){let c=e.pq.subvectors||8;o=h*(c+4)}if((i=e.svd)!=null&&i.enabled){let c=e.svd.targetDim||t/2;o=h*c*4}let n=0;if((a=e.hnsw)!=null&&a.enabled){let u=(e.hnsw.M||16)*1.5;n=h*u*4}let r=o+n;return{vectors:o,index:n,total:r,totalMB:r/(1024*1024)}}function ut(h,t){return h<1e3?{name:"SMALL_DATASET",config:Z,reason:"Small dataset, linear search is fast enough"}:h<1e4?{name:"MEDIUM_DATASET",config:K,reason:"Medium dataset, HNSW + PQ8 recommended"}:{name:"LARGE_DATASET",config:Y,reason:"Large dataset, aggressive compression + HNSW recommended"}}async function mt(h,t=100,e=10,o=384){let n=[];for(let r=0;rr-s),{avgTimeMs:n.reduce((r,s)=>r+s,0)/n.length,minTimeMs:n[0],maxTimeMs:n[n.length-1],p50Ms:n[Math.floor(n.length*.5)],p95Ms:n[Math.floor(n.length*.95)],p99Ms:n[Math.floor(n.length*.99)]}}export{v as AttentionBrowser,C as BatchProcessor,w as GraphNeuralNetwork,A as HNSWIndex,Y as LARGE_DATASET_CONFIG,K as MEDIUM_DATASET_CONFIG,st as MEMORY_OPTIMIZED_CONFIG,x as MaximalMarginalRelevance,y as ProductQuantization,at as QUALITY_OPTIMIZED_CONFIG,Z as SMALL_DATASET_CONFIG,it as SPEED_OPTIMIZED_CONFIG,F as TensorCompression,ct as VERSION,mt as benchmarkSearch,V as createAccurateAttention,O as createAccurateHNSW,U as createAttention,$ as createFastAttention,G as createFastHNSW,q as createHNSW,Q as createPQ16,R as createPQ32,B as createPQ8,ot as detectFeatures,lt as estimateMemoryUsage,ut as recommendConfig}; //# sourceMappingURL=agentdb.browser.min.js.map