import{s as e}from"./DrKWWDpL.js";import{n as t,t as n}from"./DsQIgu-4.js";import{t as r}from"./DYP3EJMY.js";var i=e(r(),1);t();var a=`all-MiniLM-L6-v2`,o=[{name:`model_quantized.onnx`,url:`https://huggingface.co/Xenova/all-MiniLM-L6-v2/resolve/main/onnx/model_quantized.onnx`,sha256:`afdb6f1a0e45b715d0bb9b11772f032c399babd23bfc31fed1c170afc848bdb1`,sizeBytes:22905032}],s=[{name:`tokenizer.json`,url:`https://huggingface.co/Xenova/all-MiniLM-L6-v2/resolve/main/tokenizer.json`,sha256:null},{name:`tokenizer_config.json`,url:`https://huggingface.co/Xenova/all-MiniLM-L6-v2/resolve/main/tokenizer_config.json`,sha256:null},{name:`vocab.txt`,url:`https://huggingface.co/Xenova/all-MiniLM-L6-v2/resolve/main/vocab.txt`,sha256:null}];function c(){let e={}.LLMTXT_MODEL_CACHE_DIR;return e?(0,i.resolve)(e):(0,i.join)((0,i.homedir)(),`.llmtxt`,`models`,a)}async function l(e,t,n){console.log(`[llmtxt/embeddings] Downloading ${e} → ${t}`);let r=await fetch(e,{headers:{"User-Agent":`llmtxt-embedding-downloader/1.0`}});if(!r.ok)throw Error(`Failed to download ${e}: HTTP ${r.status} ${r.statusText}`);let a=await r.arrayBuffer(),o=Buffer.from(a);if(n){let t=(0,i.createHash)(`sha256`).update(o).digest(`hex`);if(t!==n)throw Error(`Checksum mismatch for ${e}:\n  expected: ${n}\n  actual:   ${t}`)}(0,i.writeFileSync)(t,o)}var u=null,d=null,f=null;async function p(){if(!(u&&d))return f||(f=m(),f)}async function m(){let e=c();(0,i.mkdirSync)(e,{recursive:!0});for(let t of o){let n=(0,i.join)(e,t.name);(0,i.existsSync)(n)||await l(t.url,n,t.sha256??void 0)}let t=(0,i.join)(e,`tokenizer`);(0,i.mkdirSync)(t,{recursive:!0});for(let e of s){let n=(0,i.join)(t,e.name);(0,i.existsSync)(n)||await l(e.url,n,e.sha256??void 0)}let r=(0,i.join)(e,o[0].name),f=[`onnxruntime`,`node`].join(`-`);u=await(await n(()=>import(f),[],import.meta.url)).InferenceSession.create(r,{executionProviders:[`cpu`],graphOptimizationLevel:`all`});let p=(0,i.join)(t,`tokenizer.json`);d=new h(JSON.parse((0,i.readFileSync)(p,`utf-8`))),console.log(`[llmtxt/embeddings] Model loaded: ${a} from ${e}`)}var h=class{vocab;ids;clsId;sepId;padId;unkId;maxSeqLen;constructor(e){this.vocab=new Map,this.ids=new Map,this.maxSeqLen=512;let t=e.model;if(t&&t.vocab&&typeof t.vocab==`object`)for(let[e,n]of Object.entries(t.vocab))this.vocab.set(e,n),this.ids.set(n,e);this.clsId=this.vocab.get(`[CLS]`)??101,this.sepId=this.vocab.get(`[SEP]`)??102,this.padId=this.vocab.get(`[PAD]`)??0,this.unkId=this.vocab.get(`[UNK]`)??100}encode(e,t=128){let n=e.toLowerCase().replace(/[^a-z0-9\s']/g,` `).split(/\s+/).filter(Boolean),r=[this.clsId];for(let e of n){let n=this.wordpieceTokenize(e);for(let e of n)r.push(this.vocab.get(e)??this.unkId);if(r.length>=t-1)break}r.push(this.sepId);let i=r.slice(0,t),a=i.length,o=new BigInt64Array(t).fill(BigInt(this.padId)),s=new BigInt64Array(t).fill(BigInt(0)),c=new BigInt64Array(t).fill(BigInt(0));for(let e=0;e<a;e++)o[e]=BigInt(i[e]),s[e]=BigInt(1);return{inputIds:o,attentionMask:s,tokenTypeIds:c}}wordpieceTokenize(e){if(this.vocab.has(e))return[e];let t=[],n=e,r=!0;for(;n.length>0;){let e=!1;for(let i=n.length;i>0;i--){let a=r?n.slice(0,i):`##`+n.slice(0,i);if(this.vocab.has(a)){t.push(a),n=n.slice(i),r=!1,e=!0;break}}if(!e){t.push(`[UNK]`);break}}return t.length>0?t:[`[UNK]`]}};function g(e,t,n,r){let i=new Float32Array(r),a=0;for(let o=0;o<n;o++){let n=Number(t[o]);a+=n;for(let t=0;t<r;t++)i[t]+=e[o*r+t]*n}if(a>0)for(let e=0;e<r;e++)i[e]/=a;return i}function _(e){let t=0;for(let n=0;n<e.length;n++)t+=e[n]*e[n];if(t=Math.sqrt(t),t>0)for(let n=0;n<e.length;n++)e[n]/=t;return e}async function v(e){return await p(),(await y([e]))[0]}async function y(e){if(e.length===0)return[];await p();let t=[];for(let n=0;n<e.length;n+=32){let r=await x(e.slice(n,n+32));t.push(...r)}return t}var b=128;async function x(e){let t=e.length,r=e.map(e=>d.encode(e,b)),i=new BigInt64Array(t*b),a=new BigInt64Array(t*b),o=new BigInt64Array(t*b);for(let e=0;e<t;e++){let t=r[e];i.set(t.inputIds,e*b),a.set(t.attentionMask,e*b),o.set(t.tokenTypeIds,e*b)}let s=[`onnxruntime`,`node`].join(`-`),{Tensor:c}=await n(()=>import(s),[],import.meta.url),l={input_ids:new c(`int64`,i,[t,b]),attention_mask:new c(`int64`,a,[t,b]),token_type_ids:new c(`int64`,o,[t,b])},f=await u.run(l),p=(f.last_hidden_state??f.token_embeddings).data,m=[];for(let e=0;e<t;e++){let t=g(p.slice(e*b*384,(e+1)*b*384),r[e].attentionMask,b,384);_(t),m.push(t)}return m}export{v as embed};