4 lines
22 KiB
JavaScript
4 lines
22 KiB
JavaScript
/*! 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<h.length;c+=n){let u=h.slice(c,c+n),m=0,d=new Float32Array(i);for(let l=0;l<i;l++){let f=t.slice(l*n,(l+1)*n),b=0;for(let g=0;g<n;g++)b+=u[g]*f[g];d[l]=Math.exp(b/Math.sqrt(n)),m+=d[l]}for(let l=0;l<i;l++){d[l]/=m;let f=e.slice(l*n,(l+1)*n);for(let b=0;b<n;b++)a[c+b]+=d[l]*f[b]}}return a}}function D(){return(h,t,e={})=>{let{curvature:o=-1}=e,n=Math.abs(o),r=new Float32Array(t.length/h.length);for(let s=0;s<r.length;s++){let i=s*h.length,a=0,c=0,u=0;for(let l=0;l<h.length;l++)a+=h[l]*t[i+l],c+=h[l]*h[l],u+=t[i+l]*t[i+l];let m=Math.sqrt(c+u-2*a),d=Math.acosh(1+2*n*m*m);r[s]=1/(1+d)}return r}}function k(){return(h,t={})=>{let{threshold:e=.8,maxClusters:o=10}=t,n=[],r=new Set;for(let s=0;s<h.length;s++){if(r.has(s))continue;let i=[h[s]];r.add(s);for(let c=s+1;c<h.length;c++){if(r.has(c))continue;let u=0,m=0,d=0;for(let f=0;f<h[s].length;f++)u+=h[s][f]*h[c][f],m+=h[s][f]*h[s][f],d+=h[c][f]*h[c][f];u/(Math.sqrt(m*d)||1)>e&&(i.push(h[c]),r.add(c))}let a=new Float32Array(h[s].length);for(let c of i)for(let u=0;u<a.length;u++)a[u]+=c[u]/i.length;if(n.push({memory:a,count:i.size,members:i}),n.length>=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;n<this.config.numSubvectors;n++){let r=n*e,s=r+e,i=t.map(c=>c.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;a<this.config.maxIterations;a++){let c=0;for(let d=0;d<n;d++){let l=1/0,f=0;for(let b=0;b<e;b++){let g=this.squaredDistance(t[d],r[b]);g<l&&(l=g,f=b)}s[d]=f,c+=l}if(Math.abs(i-c)<this.config.convergenceThreshold)break;i=c;let u=new Uint32Array(e),m=Array.from({length:e},()=>new Float32Array(o));for(let d=0;d<n;d++){let l=s[d];u[l]++;for(let f=0;f<o;f++)m[l][f]+=t[d][f]}for(let d=0;d<e;d++)if(u[d]>0)for(let l=0;l<o;l++)r[d][l]=m[d][l]/u[d]}return r}kMeansPlusPlus(t,e){let o=t.length,n=t[0].length,r=[],s=Math.floor(Math.random()*o);r.push(new Float32Array(t[s]));for(let i=1;i<e;i++){let a=new Float32Array(o),c=0;for(let m=0;m<o;m++){let d=1/0;for(let l of r){let f=this.squaredDistance(t[m],l);d=Math.min(d,f)}a[m]=d,c+=d}let u=Math.random()*c;for(let m=0;m<o;m++)if(u-=a[m],u<=0){r.push(new Float32Array(t[m]));break}}return r}compress(t){if(!this.trained||!this.codebook)throw new Error("Codebook must be trained before compression");let e=new Uint8Array(this.config.numSubvectors),o=this.codebook.subvectorDim,n=0;for(let r=0;r<t.length;r++)n+=t[r]*t[r];n=Math.sqrt(n);for(let r=0;r<this.config.numSubvectors;r++){let s=r*o,i=t.slice(s,s+o),a=1/0,c=0,u=r*this.config.numCentroids;for(let m=0;m<this.config.numCentroids;m++){let d=this.codebook.centroids[u+m],l=this.squaredDistance(i,d);l<a&&(a=l,c=m)}e[r]=c}return{codes:e,norm:n}}decompress(t){if(!this.codebook)throw new Error("Codebook not available");let e=new Float32Array(this.config.dimension),o=this.codebook.subvectorDim;for(let n=0;n<this.config.numSubvectors;n++){let r=t.codes[n],s=n*this.config.numCentroids,i=this.codebook.centroids[s+r],a=n*o;for(let c=0;c<o;c++)e[a+c]=i[c]}return e}asymmetricDistance(t,e){if(!this.codebook)throw new Error("Codebook not available");let o=0,n=this.codebook.subvectorDim;for(let r=0;r<this.config.numSubvectors;r++){let s=e.codes[r],i=r*this.config.numCentroids,a=this.codebook.centroids[i+s],c=r*n,u=t.slice(c,c+n);o+=this.squaredDistance(u,a)}return Math.sqrt(o)}batchCompress(t){return t.map(e=>this.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;n<t.length;n++){let r=t[n]-e[n];o+=r*r}return o}getStats(){let t=this.getCompressionRatio(),e=this.config.numSubvectors+4,o=this.codebook?this.config.numSubvectors*this.config.numCentroids*(this.config.dimension/this.config.numSubvectors)*4:0;return{trained:this.trained,compressionRatio:t,memoryPerVector:e,codebookSize:o}}};function B(h){return new y({dimension:h,numSubvectors:8,numCentroids:256,maxIterations:50})}function Q(h){return new y({dimension:h,numSubvectors:16,numCentroids:256,maxIterations:50})}function R(h){return new y({dimension:h,numSubvectors:32,numCentroids:256,maxIterations:50})}var S=class{items=[];push(t,e){this.items.push({item:t,priority:e}),this.bubbleUp(this.items.length-1)}pop(){if(this.items.length===0)return;let t=this.items[0].item,e=this.items.pop();return this.items.length>0&&(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(e<this.items.length&&this.items[e].priority<this.items[n].priority&&(n=e),o<this.items.length&&this.items[o].priority<this.items[n].priority&&(n=o),n===t)break;[this.items[t],this.items[n]]=[this.items[n],this.items[t]],t=n}}},A=class{config;nodes=new Map;entryPoint=null;currentId=0;ml;constructor(t={}){this.config={dimension:t.dimension||384,M:t.M||16,efConstruction:t.efConstruction||200,efSearch:t.efSearch||50,ml:t.ml||1/Math.log(2),maxLayers:t.maxLayers||16,distanceFunction:t.distanceFunction||"cosine"},this.ml=this.config.ml}add(t,e){let o=e!==void 0?e:this.currentId++,n=this.randomLevel(),r={id:o,vector:t,level:n,connections:new Map};for(let a=0;a<=n;a++)r.connections.set(a,[]);if(this.entryPoint===null)return this.entryPoint=o,this.nodes.set(o,r),o;let s=this.entryPoint,i=s;for(let a=this.nodes.get(s).level;a>n;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();(b<g||i.size()<o)&&(s.push(f,b),i.push(f,-b),i.size()>o&&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()<this.ml&&t<this.config.maxLayers-1;)t++;return t}distance(t,e){switch(this.config.distanceFunction){case"cosine":return 1-this.cosineSimilarity(t,e);case"euclidean":return this.euclideanDistance(t,e);case"manhattan":return this.manhattanDistance(t,e);default:return 1-this.cosineSimilarity(t,e)}}cosineSimilarity(t,e){let o=0,n=0,r=0;for(let s=0;s<t.length;s++)o+=t[s]*e[s],n+=t[s]*t[s],r+=e[s]*e[s];return o/(Math.sqrt(n)*Math.sqrt(r))}euclideanDistance(t,e){let o=0;for(let n=0;n<t.length;n++){let r=t[n]-e[n];o+=r*r}return Math.sqrt(o)}manhattanDistance(t,e){let o=0;for(let n=0;n<t.length;n++)o+=Math.abs(t[n]-e[n]);return o}getStats(){if(this.nodes.size===0)return{numNodes:0,numLayers:0,avgConnections:0,entryPointLevel:0,memoryBytes:0};let t=Math.max(...Array.from(this.nodes.values()).map(a=>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;s<this.config.numHeads;s++){let i=0,a=new Float32Array(n);for(let u of o){let m=this.nodes.get(u),d=this.computeAttentionScore(e.features,m.features,s);i+=d;for(let l=0;l<n&&l<m.features.length;l++)a[l]+=d*m.features[l]}if(i>0)for(let u=0;u<n;u++)a[u]/=i;let c=s*n;for(let u=0;u<n;u++)r[c+u]=a[u]}for(let s=0;s<r.length;s++)r[s]=r[s]>0?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;s<r;s++)n+=t[s]*e[s];return Math.exp(n/Math.sqrt(r))}messagePass(){let t=new Map;for(let[e]of this.nodes)t.set(e,this.graphAttention(e));return t}update(t){for(let[e,o]of t){let n=this.nodes.get(e);n&&(n.features=o)}}computeGraphEmbedding(t,e=2){new Map().set(t,this.nodes.get(t).features);for(let n=0;n<e;n++){let r=this.messagePass();this.update(r)}return this.nodes.get(t).features}getStats(){return{numNodes:this.nodes.size,numEdges:this.edges.length,avgDegree:this.edges.length/Math.max(this.nodes.size,1),config:this.config}}},x=class{config;constructor(t={}){this.config={lambda:t.lambda||.7,metric:t.metric||"cosine"}}rerank(t,e,o){if(e.length===0)return[];let n=[],r=new Set(e.map((a,c)=>c)),s=0,i=-1/0;for(let a=0;a<e.length;a++)e[a].score>i&&(i=e[a].score,s=a);for(n.push(e[s].id),r.delete(s);n.length<o&&r.size>0;){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<t.length;s++)o+=t[s]*e[s],n+=t[s]*t[s],r+=e[s]*e[s];return o/(Math.sqrt(n)*Math.sqrt(r))}euclideanDistance(t,e){let o=0;for(let n=0;n<t.length;n++){let r=t[n]-e[n];o+=r*r}return Math.sqrt(o)}setLambda(t){this.config.lambda=Math.max(0,Math.min(1,t))}},F=class{static compress(t,e){if(t.length===0)return[];let o=t[0].length;if(e>=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;d<e;d++){let l=0;for(let f=0;f<o;f++)l+=u[f]*a[d][f];m[d]=l}return m})}static computeMean(t){let e=t.length,o=t[0].length,n=new Array(o).fill(0);for(let r of t)for(let s=0;s<o;s++)n[s]+=r[s];return n.map(r=>r/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;r<o;r++)for(let s=0;s<=r;s++){let i=0;for(let a of t)i+=a[r]*a[s];n[r][s]=n[s][r]=i/e}return n}static powerIteration(t,e,o=100){let n=t.length,r=[];for(let s=0;s<e;s++){let i=new Array(n).fill(0).map(()=>Math.random()-.5);for(let a=0;a<o;a++){let c=new Array(n).fill(0);for(let m=0;m<n;m++)for(let d=0;d<n;d++)c[m]+=t[m][d]*i[d];for(let m of r){let d=0;for(let l=0;l<n;l++)d+=c[l]*m[l];for(let l=0;l<n;l++)c[l]-=d*m[l]}let u=0;for(let m of c)u+=m*m;if(u=Math.sqrt(u),u<1e-10)break;i=c.map(m=>m/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<t.length;r++)n+=t[r]*t[r];n=Math.sqrt(n);for(let r=0;r<e.length;r++){let s=e[r],i=0,a=0;for(let c=0;c<t.length;c++)i+=t[c]*s[c],a+=s[c]*s[c];a=Math.sqrt(a),o[r]=i/(n*a)}return o}static batchNormalize(t){return t.map(e=>{let o=0;for(let r=0;r<e.length;r++)o+=e[r]*e[r];o=Math.sqrt(o);let n=new Float32Array(e.length);for(let r=0;r<e.length;r++)n[r]=e[r]/o;return n})}};var v=class{wasmModule=null;loadingState="idle";loadError=null;config;constructor(t={}){this.config={dimension:384,numHeads:4,blockSize:64,curvature:-1,useWASM:!0,...t}}getLoadingState(){return this.loadingState}getError(){return this.loadError}async initialize(){if(this.loadingState!=="loaded"){if(this.loadingState==="loading"){for(;this.loadingState==="loading";)await new Promise(t=>setTimeout(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;i<t.length;i+=n){let a=t.slice(i,i+n),c=0,u=new Float32Array(r);for(let m=0;m<r;m++){let d=e.slice(m*n,(m+1)*n),l=0;for(let f=0;f<n;f++)l+=a[f]*d[f];u[m]=Math.exp(l/Math.sqrt(n)),c+=u[m]}for(let m=0;m<r;m++){u[m]/=c||1;let d=o.slice(m*n,(m+1)*n);for(let l=0;l<n;l++)s[i+l]+=u[m]*d[l]}}return s}hyperbolicAttentionFallback(t,e){let{curvature:o=-1}=this.config,n=Math.abs(o),r=new Float32Array(e.length/t.length);for(let s=0;s<r.length;s++){let i=s*t.length,a=0,c=0,u=0;for(let l=0;l<t.length;l++)a+=t[l]*e[i+l],c+=t[l]*t[l],u+=e[i+l]*e[i+l];let m=Math.sqrt(c+u-2*a),d=Math.acosh(1+2*n*m*m);r[s]=1/(1+d)}return r}consolidateMemoriesFallback(t,e){let{threshold:o=.8,maxClusters:n=10,minClusterSize:r=1}=e,s=[],i=new Set;for(let a=0;a<t.length;a++){if(i.has(a))continue;let c=[t[a]];i.add(a);for(let u=a+1;u<t.length;u++){if(i.has(u))continue;this.cosineSimilarity(t[a],t[u])>o&&(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;l<u.length;l++)u[l]+=d[l]/c.length;let m=0;for(let d=0;d<u.length;d++)m+=u[d]*u[d];if(m=Math.sqrt(m),m>0)for(let d=0;d<u.length;d++)u[d]/=m;s.push({memory:u,count:c.length,members:c})}if(s.length>=n)break}return s}cosineSimilarity(t,e){let o=0,n=0,r=0;for(let i=0;i<t.length;i++)o+=t[i]*e[i],n+=t[i]*t[i],r+=e[i]*e[i];let s=Math.sqrt(n*r);return s>0?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;r<t;r++){let s=new Float32Array(o);for(let c=0;c<o;c++)s[c]=Math.random()-.5;let i=performance.now();h(s,e);let a=performance.now();n.push(a-i)}return n.sort((r,s)=>r-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
|