@lunora/ai 1.0.0-alpha.63 → 1.0.0-alpha.65
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/packem_shared/sql-CNy5-Oop.mjs +1 -0
- package/dist/packem_shared/{sqlLexicalStore-DcWChCVZ.mjs → sqlLexicalStore-qkTJHHzM.mjs} +1 -1
- package/dist/packem_shared/{sqliteVectorStore-B4HiD_-t.mjs → sqliteVectorStore-DEqrp-Uc.mjs} +1 -1
- package/dist/rag/index.d.mts +0 -12
- package/dist/rag/index.d.ts +0 -12
- package/dist/rag/index.mjs +1 -1
- package/package.json +2 -2
- package/dist/packem_shared/sql-D6W_98IK.mjs +0 -1
|
@@ -0,0 +1 @@
|
|
|
1
|
+
const l=t=>/^[A-Z_]\w*$/i.test(t),h=t=>Array.from({length:t}).fill("?").join(", "),f=64,d=(t,e=64)=>{const n=Math.max(1,Math.floor(e)),s=[];for(let r=0;r<t.length;r+=n)s.push(t.slice(r,r+n));return s},g=(t,e)=>{if(!l(t))throw new TypeError(`@lunora/ai/rag: ${e} must be a bare SQL identifier (letters, digits, underscore; not starting with a digit) — got "${t}"`);return t},u=(t,e)=>{if(t.length!==e.length||t.length===0)return 0;let n=0,s=0,r=0;for(const[c,i]of t.entries()){const o=e[c];n+=i*o,s+=i*i,r+=o*o}const a=Math.sqrt(s)*Math.sqrt(r);return a===0?0:n/a},p=t=>{if(t!=null){if(typeof t=="string")try{return JSON.parse(t)}catch{return}return t}};export{f as I,g as a,u as c,d as i,h as p,p as r};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import{t as x,c as U,b,a as F}from"./bm25-BT0cd0lY.mjs";import M from"./matchesMetadataFilter-BbIOyA5g.mjs";import{a as q,i as p,p as L,r as C,I as D}from"./sql-
|
|
1
|
+
import{t as x,c as U,b,a as F}from"./bm25-BT0cd0lY.mjs";import M from"./matchesMetadataFilter-BbIOyA5g.mjs";import{a as q,i as p,p as L,r as C,I as D}from"./sql-CNy5-Oop.mjs";const X="lunora_rag_lexical",_=null,I=m=>m??"",S=m=>{const s=typeof m=="number"?m:Number(m);return Number.isFinite(s)?s:0},K=m=>{if(typeof m.exec!="function")throw new TypeError("@lunora/ai/rag: sqlLexicalStore requires an `exec` function");const s=q(m.table??X,"sqlLexicalStore `table`"),f=`${s}_terms`,{exec:r}=m;let w;const h=async()=>{w??=(async()=>{await r(`CREATE TABLE IF NOT EXISTS ${s} (id TEXT NOT NULL, namespace TEXT NOT NULL DEFAULT '', text TEXT NOT NULL, length INTEGER NOT NULL, metadata TEXT, PRIMARY KEY (namespace, id))`,[]),await r(`CREATE TABLE IF NOT EXISTS ${f} (term TEXT NOT NULL, id TEXT NOT NULL, namespace TEXT NOT NULL DEFAULT '', frequency INTEGER NOT NULL, PRIMARY KEY (namespace, term, id))`,[]),await r(`CREATE INDEX IF NOT EXISTS ${f}_lookup ON ${f} (namespace, term)`,[]),await r(`CREATE INDEX IF NOT EXISTS ${s}_namespace ON ${s} (namespace)`,[])})().catch(c=>{throw w=void 0,c}),await w},A=async(c,n)=>{for(const o of p(c)){const i=L(o.length);await r(`DELETE FROM ${f} WHERE namespace = ? AND id IN (${i})`,[n,...o]),await r(`DELETE FROM ${s} WHERE namespace = ? AND id IN (${i})`,[n,...o])}},R=async(c,n,o)=>{const i=`(${L(n)})`;for(const E of p(o,D/n))await r(`${c} VALUES ${Array.from({length:E.length}).fill(i).join(", ")}`,E.flatMap(e=>[...e]))};return{index:async(c,n)=>{await h();const o=I(n.namespace);await A(c.map(e=>e.id),o);const i=[],E=[];for(const e of c){const N=F(e.text);if(N.length===0)continue;const d=new Map;for(const T of N)d.set(T,(d.get(T)??0)+1);i.push([e.id,o,e.text,N.length,e.metadata===void 0?_:JSON.stringify(e.metadata)]);for(const[T,l]of d)E.push([T,e.id,o,l])}await R(`INSERT INTO ${f} (term, id, namespace, frequency)`,4,E),await R(`INSERT INTO ${s} (id, namespace, text, length, metadata)`,5,i)},remove:async(c,n)=>{await h(),await A(c,I(n.namespace))},search:async(c,n)=>{await h();const o=I(n.namespace),i=x(c);if(i.length===0)return[];const[E]=await r(`SELECT COUNT(*) AS document_count, COALESCE(SUM(length), 0) AS total_length FROM ${s} WHERE namespace = ?`,[o]),e=S(E?.document_count);if(e===0)return[];const N=S(E?.total_length)/e,d=[];for(const t of p(i)){const a=await r(`SELECT t.term AS term, t.id AS id, t.frequency AS frequency, d.length AS length, d.metadata AS metadata FROM ${f} t JOIN ${s} d ON d.id = t.id AND d.namespace = t.namespace WHERE t.namespace = ? AND t.term IN (${L(t.length)})`,[o,...t]);d.push(...a)}const T=new Map;for(const t of d){const a=String(t.term);T.set(a,(T.get(a)??0)+1)}const l=new Map;for(const t of d){const a=C(t.metadata);if(!M(a,n.filter))continue;const u=String(t.id),y=U(e,T.get(String(t.term))??1),$=b(y,S(t.frequency),S(t.length),N);l.set(u,(l.get(u)??0)+$)}const g=[...l.entries()].toSorted(([,t],[,a])=>a-t).slice(0,n.topK);if(g.length===0)return[];const O=new Map;for(const t of p(g.map(([a])=>a))){const a=await r(`SELECT id, text FROM ${s} WHERE namespace = ? AND id IN (${L(t.length)})`,[o,...t]);for(const u of a)O.set(String(u.id),String(u.text))}return g.map(([t,a])=>({id:t,score:a,text:O.get(t)??""}))}}};export{K as sqlLexicalStore};
|
package/dist/packem_shared/{sqliteVectorStore-B4HiD_-t.mjs → sqliteVectorStore-DEqrp-Uc.mjs}
RENAMED
|
@@ -1 +1 @@
|
|
|
1
|
-
import g from"./matchesMetadataFilter-BbIOyA5g.mjs";import{a as v,p as f,r as h,c as L,i as S}from"./sql-
|
|
1
|
+
import g from"./matchesMetadataFilter-BbIOyA5g.mjs";import{a as v,p as f,r as h,c as L,i as S}from"./sql-CNy5-Oop.mjs";const N="lunora_rag_vectors",p=5e4,w=100,I=null,d=s=>s??"",R=s=>{if(typeof s.exec!="function")throw new TypeError("@lunora/ai/rag: sqliteVectorStore requires an `exec` function");const n=v(s.table??N,"sqliteVectorStore `table`"),E=s.maxScan??p,{exec:c}=s,y={maxDimensions:s.maxDimensions??!1,maxIdBytes:!1,maxMetadataBytes:!1,maxTopK:w,maxTopKWithMetadata:w};let T;const m=async()=>{T??=(async()=>{await c(`CREATE TABLE IF NOT EXISTS ${n} (id TEXT NOT NULL, namespace TEXT NOT NULL DEFAULT '', vector TEXT NOT NULL, metadata TEXT, PRIMARY KEY (namespace, id))`,[]),await c(`CREATE INDEX IF NOT EXISTS ${n}_namespace ON ${n} (namespace)`,[])})().catch(e=>{throw T=void 0,e}),await T};return{capabilities:y,deleteByIds:async(e,t)=>{if(await m(),e.length!==0)for(const a of S(e))await c(`DELETE FROM ${n} WHERE namespace = ? AND id IN (${f(a.length)})`,[d(t),...a])},getByIds:async(e,t)=>{if(await m(),e.length===0)return[];const a=[];for(const i of S(e)){const l=await c(`SELECT id, metadata FROM ${n} WHERE namespace = ? AND id IN (${f(i.length)})`,[d(t),...i]);for(const r of l){const o=h(r.metadata);a.push({id:String(r.id),...o===void 0?{}:{metadata:o}})}}return a},query:async e=>{await m();let t;if(e.embed&&e.input!==void 0)t=await e.embed(e.input);else throw new TypeError("@lunora/ai/rag: sqliteVectorStore query requires both `input` and `embed`");const a=await c(`SELECT id, vector, metadata FROM ${n} WHERE namespace = ? LIMIT ?`,[d(e.namespace),E+1]);if(a.length>E)throw new RangeError(`@lunora/ai/rag: sqliteVectorStore scanned ${String(a.length)} vectors in namespace "${d(e.namespace)}", over the ${String(E)} limit — search here is brute force and linear, so this namespace has outgrown it. Shard it further, or move this index to Vectorize or a pgvector backend`);const i=[];for(const r of a){const o=h(r.metadata);if(!g(o,e.filter))continue;const u=h(r.vector);u!==void 0&&i.push({id:String(r.id),score:L(t,u),...e.returnMetadata==="none"||o===void 0?{}:{metadata:o}})}const l=i.toSorted((r,o)=>o.score-r.score).slice(0,e.topK??10);return{count:l.length,matches:l}},upsert:async e=>{if(await m(),!e.embed)throw new TypeError("@lunora/ai/rag: sqliteVectorStore requires an `embed` function on upsert");const t=await e.embed(e.input);await c(`INSERT INTO ${n} (id, namespace, vector, metadata) VALUES (${f(4)}) ON CONFLICT(namespace, id) DO UPDATE SET vector = excluded.vector, metadata = excluded.metadata`,[e.id,d(e.namespace),JSON.stringify([...t]),e.metadata===void 0?I:JSON.stringify(e.metadata)])}}};export{R as sqliteVectorStore};
|
package/dist/rag/index.d.mts
CHANGED
|
@@ -1026,18 +1026,6 @@ interface RagSourceSync {
|
|
|
1026
1026
|
* @experimental
|
|
1027
1027
|
*/
|
|
1028
1028
|
declare const defineRagSource: (rag: Rag, options?: RagSourceOptions) => RagSourceSync;
|
|
1029
|
-
/**
|
|
1030
|
-
* The injected SQL seam the durable RAG stores share.
|
|
1031
|
-
*
|
|
1032
|
-
* `@lunora/ai` depends on no database. A store is handed an executor instead,
|
|
1033
|
-
* so the same adapter runs on a Durable Object's SQLite, D1, `node:sqlite`, or
|
|
1034
|
-
* Postgres over Hyperdrive — and this package stays free of every one of them.
|
|
1035
|
-
*
|
|
1036
|
-
* The statements the stores emit are SQLite-shaped (`ON CONFLICT … DO UPDATE`,
|
|
1037
|
-
* `CREATE INDEX IF NOT EXISTS`, `?` placeholders), so the executor must front a
|
|
1038
|
-
* SQLite engine — a Durable Object, D1, or `node:sqlite`.
|
|
1039
|
-
* @experimental
|
|
1040
|
-
*/
|
|
1041
1029
|
/**
|
|
1042
1030
|
* Run one statement and return its rows.
|
|
1043
1031
|
*
|
package/dist/rag/index.d.ts
CHANGED
|
@@ -1026,18 +1026,6 @@ interface RagSourceSync {
|
|
|
1026
1026
|
* @experimental
|
|
1027
1027
|
*/
|
|
1028
1028
|
declare const defineRagSource: (rag: Rag, options?: RagSourceOptions) => RagSourceSync;
|
|
1029
|
-
/**
|
|
1030
|
-
* The injected SQL seam the durable RAG stores share.
|
|
1031
|
-
*
|
|
1032
|
-
* `@lunora/ai` depends on no database. A store is handed an executor instead,
|
|
1033
|
-
* so the same adapter runs on a Durable Object's SQLite, D1, `node:sqlite`, or
|
|
1034
|
-
* Postgres over Hyperdrive — and this package stays free of every one of them.
|
|
1035
|
-
*
|
|
1036
|
-
* The statements the stores emit are SQLite-shaped (`ON CONFLICT … DO UPDATE`,
|
|
1037
|
-
* `CREATE INDEX IF NOT EXISTS`, `?` placeholders), so the executor must front a
|
|
1038
|
-
* SQLite engine — a Durable Object, D1, or `node:sqlite`.
|
|
1039
|
-
* @experimental
|
|
1040
|
-
*/
|
|
1041
1029
|
/**
|
|
1042
1030
|
* Run one statement and return its rows.
|
|
1043
1031
|
*
|
package/dist/rag/index.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{default as o}from"../packem_shared/fixedWindowChunks-C461ahRE.mjs";import{markdownChunker as a,sentenceChunker as f,tokenChunker as n}from"../packem_shared/markdownChunker-CDME5gEs.mjs";import{default as x}from"../packem_shared/defineRag-Dzh-MbCu.mjs";import{contentHash as p,guessMimeTypeFromExtension as i}from"../packem_shared/contentHash-BIn6ECP8.mjs";import{default as c}from"../packem_shared/hybridRank-DejmVw2I.mjs";import{default as l}from"../packem_shared/bm25LexicalStore-CALA7Lhm.mjs";import{default as h}from"../packem_shared/matchesMetadataFilter-BbIOyA5g.mjs";import{batchReranker as g,scoreReranker as C}from"../packem_shared/batchReranker-Bc38FBLH.mjs";import{defineRagSource as E}from"../packem_shared/defineRagSource-Q3f3niU8.mjs";import{sqlLexicalStore as T}from"../packem_shared/sqlLexicalStore-
|
|
1
|
+
import{default as o}from"../packem_shared/fixedWindowChunks-C461ahRE.mjs";import{markdownChunker as a,sentenceChunker as f,tokenChunker as n}from"../packem_shared/markdownChunker-CDME5gEs.mjs";import{default as x}from"../packem_shared/defineRag-Dzh-MbCu.mjs";import{contentHash as p,guessMimeTypeFromExtension as i}from"../packem_shared/contentHash-BIn6ECP8.mjs";import{default as c}from"../packem_shared/hybridRank-DejmVw2I.mjs";import{default as l}from"../packem_shared/bm25LexicalStore-CALA7Lhm.mjs";import{default as h}from"../packem_shared/matchesMetadataFilter-BbIOyA5g.mjs";import{batchReranker as g,scoreReranker as C}from"../packem_shared/batchReranker-Bc38FBLH.mjs";import{defineRagSource as E}from"../packem_shared/defineRagSource-Q3f3niU8.mjs";import{sqlLexicalStore as T}from"../packem_shared/sqlLexicalStore-qkTJHHzM.mjs";import{sqliteVectorStore as y}from"../packem_shared/sqliteVectorStore-DEqrp-Uc.mjs";import{ragSyncTriggers as q}from"../packem_shared/ragSyncTriggers-DPqzBNFw.mjs";import{VECTORIZE_CAPABILITIES as A,vectorizeStore as F}from"../packem_shared/VECTORIZE_CAPABILITIES-CUQDoxis.mjs";export{A as VECTORIZE_CAPABILITIES,g as batchReranker,l as bm25LexicalStore,p as contentHash,x as defineRag,E as defineRagSource,o as fixedWindowChunks,i as guessMimeTypeFromExtension,c as hybridRank,a as markdownChunker,h as matchesMetadataFilter,q as ragSyncTriggers,C as scoreReranker,f as sentenceChunker,T as sqlLexicalStore,y as sqliteVectorStore,n as tokenChunker,F as vectorizeStore};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lunora/ai",
|
|
3
|
-
"version": "1.0.0-alpha.
|
|
3
|
+
"version": "1.0.0-alpha.65",
|
|
4
4
|
"description": "Workers AI helper for Lunora: provider-agnostic AI SDK access from functions, Workers AI by default",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"ai",
|
|
@@ -53,7 +53,7 @@
|
|
|
53
53
|
"access": "public"
|
|
54
54
|
},
|
|
55
55
|
"dependencies": {
|
|
56
|
-
"@lunora/errors": "1.0.0-alpha.
|
|
56
|
+
"@lunora/errors": "1.0.0-alpha.26",
|
|
57
57
|
"ai": "7.0.59",
|
|
58
58
|
"workers-ai-provider": "4.0.0"
|
|
59
59
|
},
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
const h=t=>Array.from({length:t}).fill("?").join(", "),f=64,d=(t,e=64)=>{const n=Math.max(1,Math.floor(e)),s=[];for(let r=0;r<t.length;r+=n)s.push(t.slice(r,r+n));return s},l=/^[A-Z_]\w*$/i,g=(t,e)=>{if(!l.test(t))throw new TypeError(`@lunora/ai/rag: ${e} must be a bare SQL identifier (letters, digits, underscore; not starting with a digit) — got "${t}"`);return t},u=(t,e)=>{if(t.length!==e.length||t.length===0)return 0;let n=0,s=0,r=0;for(const[c,o]of t.entries()){const i=e[c];n+=o*i,s+=o*o,r+=i*i}const a=Math.sqrt(s)*Math.sqrt(r);return a===0?0:n/a},I=t=>{if(t!=null){if(typeof t=="string")try{return JSON.parse(t)}catch{return}return t}};export{f as I,g as a,u as c,d as i,h as p,I as r};
|