@lunora/bindings 1.0.0-alpha.70 → 1.0.0-alpha.71

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.
@@ -0,0 +1,6 @@
1
+ import{c as w,U as v}from"./concurrent-vRmSvRpF.mjs";const M=(s,r)=>{if(!r.includes("."))return s[r];let i=s;for(const a of r.split(".")){if(i===null||typeof i!="object"||Array.isArray(i))return;i=i[a]}return i},A=(s,r)=>{const i=r?.namespace,a=new Set(r?.shardedIndexNames),d=(t,c)=>{if(c!==void 0)return c;if(a.has(t)){if(i!==void 0)return i;throw new Error(`@lunora/bindings/vectors: index "${t}" belongs to a sharded table, but this DO instance has no shard key (it is the root/default DO) and no explicit namespace was given. A namespace-less operation here would reach every tenant's vectors — Vectorize indexes are account-global. Pass an explicit namespace, or issue this call from the sharded DO instance that owns the tenant.`)}},n=r?.deferAfterCommit,f=async(t,c,u)=>{await s.upsert(t,{embed:c.embed,id:c.id,input:c.input,metadata:c.metadata,namespace:u})},l=async(t,c)=>{await f(t,c,d(t,c.namespace))},o=n===void 0?l:async(t,c)=>{const u=d(t,c.namespace);await n(async()=>f(t,c,u))},e=async(t,c,u)=>{const m=await s.getByIds(t,c);return u===void 0?m:m.filter(h=>h.namespace===u)};return{deleteByIds:async(t,c,u)=>{const m=d(t,u);if(m===void 0){await s.deleteByIds(t,c);return}const h=await e(t,c,m);h.length!==0&&await s.deleteByIds(t,h.map(p=>p.id))},getByIds:async(t,c,u)=>{const m=d(t,u);return(await e(t,c,m)).map(p=>({id:p.id,metadata:p.metadata,namespace:p.namespace,values:[...p.values]}))},query:async(t,c)=>{const u=await s.query(t,{embed:c.embed,filter:c.filter,input:c.input,namespace:d(t,c.namespace),returnMetadata:c.returnMetadata??"indexed",topK:c.topK,vector:c.vector});return{count:u.count,matches:u.matches.map(m=>({id:m.id,metadata:m.metadata,score:m.score}))}},upsert:o,upsertNow:l}},y=new Set,b=s=>{y.has(s)||(y.add(s),console.warn(`[@lunora/bindings/vectors] index "${s}" syncs vectors without a namespace — in a
2
+ multi-tenant/sharded app this exposes one tenant's vectors (and any captured
3
+ metadata) to every other tenant, since Vectorize indexes are account-global.
4
+ Pass \`namespace\` (the shard/tenant key) on both write and query — query-side
5
+ namespace filtering is mandatory for multi-tenant apps. Single-tenant apps that
6
+ legitimately have no tenant key suppress this via { allowSharedNamespace: true }.`))},S=(s,r)=>{const i={};for(const a of r)a in s&&(i[a]=s[a]);return i},g=(s,r)=>{const i=s.tables[r.table],a=i?.vectorIndexes??[],d=Object.entries(s.vectorIndexes).filter(([,e])=>e.table===r.table);if(a.length===0&&d.length===0)return;const n=i?.softDeleteMode?.field,f=n!==void 0&&r.doc?.[n]!==void 0&&r.doc[n]!==null;if(r.op==="delete"||f)return{deletes:[...a.map(e=>e.name),...d.map(([e])=>e)],upserts:[]};const l=r.doc;if(!l)return;const o={deletes:[],upserts:[]};for(const e of a){const t=M(l,e.field);if(t==null)o.deletes.push(e.name);else if(typeof t=="string")o.upserts.push({embed:e.embed,input:t,metadata:e.metadata?S(l,e.metadata):void 0,name:e.name});else throw new TypeError(`@lunora/bindings/vectors: inline index "${e.name}" expects a string source at "${e.field}" on table "${r.table}" (got ${typeof t}); use a standalone defineVectorIndex with a select() to derive text from non-string columns`)}for(const[e,t]of d)o.upserts.push({embed:t.embed,input:t.select(l),metadata:t.metadata?.(l),name:e});return o},D=s=>{const{allowSharedNamespace:r,namespace:i,schema:a,vectors:d}=s;return async n=>{const f=g(a,n);if(!f)return;const l=[...f.deletes.map(o=>async()=>{await d.deleteByIds(o,[n.id])}),...f.upserts.map(o=>async()=>{!r&&i===void 0&&b(o.name),await d.upsertNow(o.name,{embed:o.embed,id:n.id,input:o.input,metadata:o.metadata,namespace:i})})];await w(l,v,async o=>o())}},I=1e3,x=(s,r)=>{const i=[];for(let a=0;a<s.length;a+=r)i.push(s.slice(a,a+r));return i},k=async(s,r)=>{const i=await w(s,v,async n=>{try{return{item:n,ok:!0,value:await r(n)}}catch(f){return{error:f,item:n,ok:!1}}}),a=i.flatMap(n=>n.ok?[{item:n.item,value:n.value}]:[]),d=i.flatMap(n=>n.ok?[]:[{error:n.error,item:n.item}]);if(s.length>=2&&a.length===0)throw d[0]?.error;return{failed:d,ok:a}},B=(s,r,i,a)=>{const d=new Map,n=[];for(const{doc:f,id:l}of i){let o;try{o=g(s,{doc:f,id:l,op:"update",table:r})}catch(e){a.set(l,e);continue}for(const e of o?.deletes??[])d.set(e,[...d.get(e)??[],l]);for(const e of o?.upserts??[])n.push({id:l,upsert:e})}return{deletes:d,pending:n}},C=async(s,r,i,a)=>{const{allowSharedNamespace:d,namespace:n,upsertMany:f,vectors:l}=s;!d&&n===void 0&&b(r);const o=i.map(({id:e,upsert:t,values:c})=>({embed:()=>c,id:e,input:t.input,metadata:t.metadata,namespace:n}));for(const e of x(o,I))try{await f(r,e)}catch{const t=await k(e,async c=>l.upsertNow(r,c));for(const{error:c,item:u}of t.failed)a.set(u.id,c)}},E=s=>async(r,i)=>{const a=new Map,{deletes:d,pending:n}=B(s.schema,r,i,a),f=await k(n,async({upsert:o})=>o.embed(o.input)),l=new Map;for(const{error:o,item:e}of f.failed)a.set(e.id,o);for(const{item:o,value:e}of f.ok)l.set(o.upsert.name,[...l.get(o.upsert.name)??[],{...o,values:e}]);for(const[o,e]of d)for(const t of x(e,I))await s.vectors.deleteByIds(o,t);for(const[o,e]of l)await C(s,o,e,a);return[...a].map(([o,e])=>({error:e,id:o}))},V=s=>{const r=new Map,i=(a,d)=>{r.set(a,[...r.get(a)??[],JSON.stringify(d)])};for(const[a,d]of Object.entries(s.tables))for(const n of d.vectorIndexes??[])i(a,[n.name,n.field,n.dimensions,n.metric,n.metadata??[]]);for(const[a,d]of Object.entries(s.vectorIndexes))i(d.table,[a,"(select)",d.dimensions,d.metric]);return[...r].map(([a,d])=>({profile:d.toSorted((n,f)=>n.localeCompare(f)).join("|"),table:a}))};export{A as createContextVectors,E as createVectorBackfillSync,D as createVectorSyncHook,V as vectorBackfillTargets};
@@ -214,18 +214,26 @@ interface WriteEvent {
214
214
  type WriteHook = (event: WriteEvent) => Promise<void>;
215
215
  /** Inline vector index declared via `.vectorize(field, ...)` (DSL Shape A). */
216
216
  interface TableVectorIndexLike {
217
+ dimensions?: number;
217
218
  embed: VectorEmbedderLike;
218
219
  field: string;
219
220
  metadata?: ReadonlyArray<string>;
221
+ metric?: string;
220
222
  name: string;
221
223
  }
222
224
  interface TableDefinitionLike {
225
+ /** `.softDelete()` marker column. A row whose marker is set is hidden from `ctx.db`, so it must have no vector. */
226
+ softDeleteMode?: {
227
+ field: string;
228
+ };
223
229
  vectorIndexes?: ReadonlyArray<TableVectorIndexLike>;
224
230
  }
225
231
  /** Standalone vector index declared via `defineVectorIndex(...)` (DSL Shape B). */
226
232
  interface VectorIndexDefinitionLike {
233
+ dimensions?: number;
227
234
  embed: VectorEmbedderLike;
228
235
  metadata?: (row: Record<string, unknown>) => Record<string, unknown>;
236
+ metric?: string;
229
237
  select: (row: Record<string, unknown>) => string;
230
238
  table: string;
231
239
  }
@@ -242,7 +250,7 @@ interface SchemaLike {
242
250
  * Build a {@link WriteHook} that keeps Vectorize in sync with row writes. On
243
251
  * insert/update it embeds each matching index's source (Shape A `row[field]`,
244
252
  * Shape B `select(row)`) and upserts; on delete it removes the row's id from
245
- * every index sourced from the table.
253
+ * every index sourced from the table. {@link planRowSync} makes the decision.
246
254
  *
247
255
  * Tenant isolation — IMPORTANT: Vectorize indexes are account-global and shared
248
256
  * by every shard DO. Without a `namespace`, a multi-tenant sharded app has NO
@@ -298,6 +306,67 @@ declare const createVectorSyncHook: (options: {
298
306
  schema: SchemaLike;
299
307
  vectors: VectorSearchLike;
300
308
  }) => WriteHook;
309
+ /** A row the backfill could not index, and why. */
310
+ interface VectorBackfillFailure {
311
+ error: unknown;
312
+ id: string;
313
+ }
314
+ /**
315
+ * Index one page of rows for the shard's vector backfill. Resolves with the rows
316
+ * that failed on their own; REJECTS when the failure is the service's rather than
317
+ * a row's, so the caller holds its cursor and retries the page.
318
+ */
319
+ type VectorBackfillSync = (table: string, rows: ReadonlyArray<{
320
+ doc: Record<string, unknown>;
321
+ id: string;
322
+ }>) => Promise<ReadonlyArray<VectorBackfillFailure>>;
323
+ /**
324
+ * The backfill's counterpart to {@link createVectorSyncHook}: the same
325
+ * {@link planRowSync} decision for a whole page of rows, with the remote calls
326
+ * batched — every row is embedded (bounded concurrency), then each index takes
327
+ * ONE `upsertMany` and ONE `deleteByIds` per 1000 rows instead of a call per row.
328
+ * That is what keeps a page short enough to hold the shard's write-hook chain.
329
+ *
330
+ * Failures are split in two, because they need opposite handling.
331
+ *
332
+ * A ROW failure is deterministic and would fail on every retry: a non-string
333
+ * source, a `select()` that throws, text the model rejects, metadata Vectorize
334
+ * refuses. The row is reported and the page moves on — the live hook only logs
335
+ * these too, and a backfill that stopped on one would never finish.
336
+ *
337
+ * A SERVICE failure is transient: the embedder or Vectorize is unreachable. The
338
+ * call rejects, so the page is retried. It is recognised as every attempt in a
339
+ * group of two or more failing, and as a failed `deleteByIds` (which has no row
340
+ * content to blame). A batch `upsertMany` that fails is retried one row at a
341
+ * time to tell the two apart.
342
+ *
343
+ * `upsertMany` is the raw binding call, so the namespace is passed explicitly —
344
+ * the same `namespace` the live hook scopes by.
345
+ */
346
+ type BackfillSyncOptions = {
347
+ allowSharedNamespace?: boolean;
348
+ namespace?: string;
349
+ schema: SchemaLike;
350
+ upsertMany: LunoraVectors["upsertMany"];
351
+ vectors: VectorSearchLike;
352
+ };
353
+ declare const createVectorBackfillSync: (options: BackfillSyncOptions) => VectorBackfillSync;
354
+ /**
355
+ * Every table with a vector index sourced from it, each with a fingerprint of
356
+ * what its stored vectors were built from — the input to the shard's vector
357
+ * backfill, which re-walks a table whose fingerprint changed.
358
+ *
359
+ * Covers what the schema can see: index names, the inline source field,
360
+ * dimensions, metric and inline metadata fields. A function (`embed`, a Shape B
361
+ * `select`/`metadata`) has no stable identity to fingerprint — its source text
362
+ * changes with unrelated rebuilds of the bundle, which would re-embed whole
363
+ * tables for nothing — so changing one is announced by calling the backfill with
364
+ * `restart: true`.
365
+ */
366
+ declare const vectorBackfillTargets: (schema: SchemaLike) => {
367
+ profile: string;
368
+ table: string;
369
+ }[];
301
370
  /**
302
371
  * One vector index as the generated `LUNORA_VECTOR_INDEXES` registry describes
303
372
  * it — the static schema shape, independent of any live binding. Structurally
@@ -361,4 +430,4 @@ interface VectorAdminIntrospectorOptions {
361
430
  */
362
431
  declare const createVectorAdminIntrospector: (options: VectorAdminIntrospectorOptions) => VectorAdminIntrospector;
363
432
  declare const createVectors: (options: LunoraVectorsOptions) => LunoraVectors;
364
- export { type EmbedFunction, type LunoraVectors, type LunoraVectorsOptions, type QueryInput, type SchemaLike, type TableDefinitionLike, type TableVectorIndexLike, type UpsertInput, type VectorAdminIndexSummary, type VectorAdminIntrospector, type VectorAdminIntrospectorOptions, type VectorAdminQueryMatch, type VectorEmbedderLike, type VectorIndexDefinitionLike, type VectorIndexRegistryEntry, type VectorMatchLike, type VectorMatchesLike, type VectorQueryInputLike, type VectorRecordLike, type VectorSearchLike, type VectorUpsertInputLike, type WriteEvent, type WriteHook, createContextVectors, createVectorAdminIntrospector, createVectorSyncHook, createVectors };
433
+ export { type EmbedFunction, type LunoraVectors, type LunoraVectorsOptions, type QueryInput, type SchemaLike, type TableDefinitionLike, type TableVectorIndexLike, type UpsertInput, type VectorAdminIndexSummary, type VectorAdminIntrospector, type VectorAdminIntrospectorOptions, type VectorAdminQueryMatch, type VectorBackfillFailure, type VectorBackfillSync, type VectorEmbedderLike, type VectorIndexDefinitionLike, type VectorIndexRegistryEntry, type VectorMatchLike, type VectorMatchesLike, type VectorQueryInputLike, type VectorRecordLike, type VectorSearchLike, type VectorUpsertInputLike, type WriteEvent, type WriteHook, createContextVectors, createVectorAdminIntrospector, createVectorBackfillSync, createVectorSyncHook, createVectors, vectorBackfillTargets };
@@ -214,18 +214,26 @@ interface WriteEvent {
214
214
  type WriteHook = (event: WriteEvent) => Promise<void>;
215
215
  /** Inline vector index declared via `.vectorize(field, ...)` (DSL Shape A). */
216
216
  interface TableVectorIndexLike {
217
+ dimensions?: number;
217
218
  embed: VectorEmbedderLike;
218
219
  field: string;
219
220
  metadata?: ReadonlyArray<string>;
221
+ metric?: string;
220
222
  name: string;
221
223
  }
222
224
  interface TableDefinitionLike {
225
+ /** `.softDelete()` marker column. A row whose marker is set is hidden from `ctx.db`, so it must have no vector. */
226
+ softDeleteMode?: {
227
+ field: string;
228
+ };
223
229
  vectorIndexes?: ReadonlyArray<TableVectorIndexLike>;
224
230
  }
225
231
  /** Standalone vector index declared via `defineVectorIndex(...)` (DSL Shape B). */
226
232
  interface VectorIndexDefinitionLike {
233
+ dimensions?: number;
227
234
  embed: VectorEmbedderLike;
228
235
  metadata?: (row: Record<string, unknown>) => Record<string, unknown>;
236
+ metric?: string;
229
237
  select: (row: Record<string, unknown>) => string;
230
238
  table: string;
231
239
  }
@@ -242,7 +250,7 @@ interface SchemaLike {
242
250
  * Build a {@link WriteHook} that keeps Vectorize in sync with row writes. On
243
251
  * insert/update it embeds each matching index's source (Shape A `row[field]`,
244
252
  * Shape B `select(row)`) and upserts; on delete it removes the row's id from
245
- * every index sourced from the table.
253
+ * every index sourced from the table. {@link planRowSync} makes the decision.
246
254
  *
247
255
  * Tenant isolation — IMPORTANT: Vectorize indexes are account-global and shared
248
256
  * by every shard DO. Without a `namespace`, a multi-tenant sharded app has NO
@@ -298,6 +306,67 @@ declare const createVectorSyncHook: (options: {
298
306
  schema: SchemaLike;
299
307
  vectors: VectorSearchLike;
300
308
  }) => WriteHook;
309
+ /** A row the backfill could not index, and why. */
310
+ interface VectorBackfillFailure {
311
+ error: unknown;
312
+ id: string;
313
+ }
314
+ /**
315
+ * Index one page of rows for the shard's vector backfill. Resolves with the rows
316
+ * that failed on their own; REJECTS when the failure is the service's rather than
317
+ * a row's, so the caller holds its cursor and retries the page.
318
+ */
319
+ type VectorBackfillSync = (table: string, rows: ReadonlyArray<{
320
+ doc: Record<string, unknown>;
321
+ id: string;
322
+ }>) => Promise<ReadonlyArray<VectorBackfillFailure>>;
323
+ /**
324
+ * The backfill's counterpart to {@link createVectorSyncHook}: the same
325
+ * {@link planRowSync} decision for a whole page of rows, with the remote calls
326
+ * batched — every row is embedded (bounded concurrency), then each index takes
327
+ * ONE `upsertMany` and ONE `deleteByIds` per 1000 rows instead of a call per row.
328
+ * That is what keeps a page short enough to hold the shard's write-hook chain.
329
+ *
330
+ * Failures are split in two, because they need opposite handling.
331
+ *
332
+ * A ROW failure is deterministic and would fail on every retry: a non-string
333
+ * source, a `select()` that throws, text the model rejects, metadata Vectorize
334
+ * refuses. The row is reported and the page moves on — the live hook only logs
335
+ * these too, and a backfill that stopped on one would never finish.
336
+ *
337
+ * A SERVICE failure is transient: the embedder or Vectorize is unreachable. The
338
+ * call rejects, so the page is retried. It is recognised as every attempt in a
339
+ * group of two or more failing, and as a failed `deleteByIds` (which has no row
340
+ * content to blame). A batch `upsertMany` that fails is retried one row at a
341
+ * time to tell the two apart.
342
+ *
343
+ * `upsertMany` is the raw binding call, so the namespace is passed explicitly —
344
+ * the same `namespace` the live hook scopes by.
345
+ */
346
+ type BackfillSyncOptions = {
347
+ allowSharedNamespace?: boolean;
348
+ namespace?: string;
349
+ schema: SchemaLike;
350
+ upsertMany: LunoraVectors["upsertMany"];
351
+ vectors: VectorSearchLike;
352
+ };
353
+ declare const createVectorBackfillSync: (options: BackfillSyncOptions) => VectorBackfillSync;
354
+ /**
355
+ * Every table with a vector index sourced from it, each with a fingerprint of
356
+ * what its stored vectors were built from — the input to the shard's vector
357
+ * backfill, which re-walks a table whose fingerprint changed.
358
+ *
359
+ * Covers what the schema can see: index names, the inline source field,
360
+ * dimensions, metric and inline metadata fields. A function (`embed`, a Shape B
361
+ * `select`/`metadata`) has no stable identity to fingerprint — its source text
362
+ * changes with unrelated rebuilds of the bundle, which would re-embed whole
363
+ * tables for nothing — so changing one is announced by calling the backfill with
364
+ * `restart: true`.
365
+ */
366
+ declare const vectorBackfillTargets: (schema: SchemaLike) => {
367
+ profile: string;
368
+ table: string;
369
+ }[];
301
370
  /**
302
371
  * One vector index as the generated `LUNORA_VECTOR_INDEXES` registry describes
303
372
  * it — the static schema shape, independent of any live binding. Structurally
@@ -361,4 +430,4 @@ interface VectorAdminIntrospectorOptions {
361
430
  */
362
431
  declare const createVectorAdminIntrospector: (options: VectorAdminIntrospectorOptions) => VectorAdminIntrospector;
363
432
  declare const createVectors: (options: LunoraVectorsOptions) => LunoraVectors;
364
- export { type EmbedFunction, type LunoraVectors, type LunoraVectorsOptions, type QueryInput, type SchemaLike, type TableDefinitionLike, type TableVectorIndexLike, type UpsertInput, type VectorAdminIndexSummary, type VectorAdminIntrospector, type VectorAdminIntrospectorOptions, type VectorAdminQueryMatch, type VectorEmbedderLike, type VectorIndexDefinitionLike, type VectorIndexRegistryEntry, type VectorMatchLike, type VectorMatchesLike, type VectorQueryInputLike, type VectorRecordLike, type VectorSearchLike, type VectorUpsertInputLike, type WriteEvent, type WriteHook, createContextVectors, createVectorAdminIntrospector, createVectorSyncHook, createVectors };
433
+ export { type EmbedFunction, type LunoraVectors, type LunoraVectorsOptions, type QueryInput, type SchemaLike, type TableDefinitionLike, type TableVectorIndexLike, type UpsertInput, type VectorAdminIndexSummary, type VectorAdminIntrospector, type VectorAdminIntrospectorOptions, type VectorAdminQueryMatch, type VectorBackfillFailure, type VectorBackfillSync, type VectorEmbedderLike, type VectorIndexDefinitionLike, type VectorIndexRegistryEntry, type VectorMatchLike, type VectorMatchesLike, type VectorQueryInputLike, type VectorRecordLike, type VectorSearchLike, type VectorUpsertInputLike, type WriteEvent, type WriteHook, createContextVectors, createVectorAdminIntrospector, createVectorBackfillSync, createVectorSyncHook, createVectors, vectorBackfillTargets };
@@ -1 +1 @@
1
- import{createContextVectors as t,createVectorSyncHook as o}from"../packem_shared/createContextVectors-Bjr3VlJp.mjs";import{createVectorAdminIntrospector as a}from"../packem_shared/createVectorAdminIntrospector-Ct8v6PxJ.mjs";import{default as m}from"../packem_shared/createVectors-Dzv0ilKE.mjs";export{t as createContextVectors,a as createVectorAdminIntrospector,o as createVectorSyncHook,m as createVectors};
1
+ import{createContextVectors as t,createVectorBackfillSync as o,createVectorSyncHook as c,vectorBackfillTargets as a}from"../packem_shared/createContextVectors-Bz5Me4s6.mjs";import{createVectorAdminIntrospector as l}from"../packem_shared/createVectorAdminIntrospector-Ct8v6PxJ.mjs";import{default as s}from"../packem_shared/createVectors-Dzv0ilKE.mjs";export{t as createContextVectors,l as createVectorAdminIntrospector,o as createVectorBackfillSync,c as createVectorSyncHook,s as createVectors,a as vectorBackfillTargets};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lunora/bindings",
3
- "version": "1.0.0-alpha.70",
3
+ "version": "1.0.0-alpha.71",
4
4
  "description": "Lightweight Cloudflare binding helpers for Lunora — ctx.kv, ctx.images, ctx.analytics, ctx.pipelines, ctx.vectors, ctx.r2sql — one install, per-binding subpaths",
5
5
  "keywords": [
6
6
  "analytics",
@@ -65,7 +65,7 @@
65
65
  },
66
66
  "dependencies": {
67
67
  "@lunora/errors": "1.0.0-alpha.41",
68
- "@lunora/platform": "1.0.0-alpha.35"
68
+ "@lunora/platform": "1.0.0-alpha.36"
69
69
  },
70
70
  "engines": {
71
71
  "node": "^22.15.0 || >=24.11.0"
@@ -1,6 +0,0 @@
1
- import{c as b,U as g}from"./concurrent-vRmSvRpF.mjs";const x=(s,c)=>{if(!c.includes("."))return s[c];let n=s;for(const d of c.split(".")){if(n===null||typeof n!="object"||Array.isArray(n))return;n=n[d]}return n},S=(s,c)=>{const n=c?.namespace,d=new Set(c?.shardedIndexNames),l=(t,a)=>{if(a!==void 0)return a;if(d.has(t)){if(n!==void 0)return n;throw new Error(`@lunora/bindings/vectors: index "${t}" belongs to a sharded table, but this DO instance has no shard key (it is the root/default DO) and no explicit namespace was given. A namespace-less operation here would reach every tenant's vectors — Vectorize indexes are account-global. Pass an explicit namespace, or issue this call from the sharded DO instance that owns the tenant.`)}},i=c?.deferAfterCommit,v=async(t,a,r)=>{await s.upsert(t,{embed:a.embed,id:a.id,input:a.input,metadata:a.metadata,namespace:r})},u=async(t,a)=>{await v(t,a,l(t,a.namespace))},f=i===void 0?u:async(t,a)=>{const r=l(t,a.namespace);await i(async()=>v(t,a,r))},h=async(t,a,r)=>{const o=await s.getByIds(t,a);return r===void 0?o:o.filter(m=>m.namespace===r)};return{deleteByIds:async(t,a,r)=>{const o=l(t,r);if(o===void 0){await s.deleteByIds(t,a);return}const m=await h(t,a,o);m.length!==0&&await s.deleteByIds(t,m.map(e=>e.id))},getByIds:async(t,a,r)=>{const o=l(t,r);return(await h(t,a,o)).map(e=>({id:e.id,metadata:e.metadata,namespace:e.namespace,values:[...e.values]}))},query:async(t,a)=>{const r=await s.query(t,{embed:a.embed,filter:a.filter,input:a.input,namespace:l(t,a.namespace),returnMetadata:a.returnMetadata??"indexed",topK:a.topK,vector:a.vector});return{count:r.count,matches:r.matches.map(o=>({id:o.id,metadata:o.metadata,score:o.score}))}},upsert:f,upsertNow:u}},w=new Set,y=s=>{w.has(s)||(w.add(s),console.warn(`[@lunora/bindings/vectors] index "${s}" syncs vectors without a namespace — in a
2
- multi-tenant/sharded app this exposes one tenant's vectors (and any captured
3
- metadata) to every other tenant, since Vectorize indexes are account-global.
4
- Pass \`namespace\` (the shard/tenant key) on both write and query — query-side
5
- namespace filtering is mandatory for multi-tenant apps. Single-tenant apps that
6
- legitimately have no tenant key suppress this via { allowSharedNamespace: true }.`))},I=(s,c)=>{const n={};for(const d of c)d in s&&(n[d]=s[d]);return n},C=s=>{const{allowSharedNamespace:c,namespace:n,schema:d,vectors:l}=s;return async i=>{const u=d.tables[i.table]?.vectorIndexes??[],f=Object.entries(d.vectorIndexes).filter(([,e])=>e.table===i.table);if(u.length===0&&f.length===0)return;const h=[...u.map(e=>e.name),...f.map(([e])=>e)];if(i.op==="delete"){await Promise.all(h.map(e=>l.deleteByIds(e,[i.id])));return}const t=i.doc;if(!t)return;const a=u.map(e=>({index:e,value:x(t,e.field)})),r=a.filter(e=>e.value!==void 0&&e.value!==null),o=a.filter(e=>e.value===void 0||e.value===null);for(const{index:e,value:p}of r)if(typeof p!="string")throw new TypeError(`@lunora/bindings/vectors: inline index "${e.name}" expects a string source at "${e.field}" on table "${i.table}" (got ${typeof p}); use a standalone defineVectorIndex with a select() to derive text from non-string columns`);const m=[...o.map(e=>async()=>{await l.deleteByIds(e.index.name,[i.id])}),...r.map(e=>async()=>{!c&&n===void 0&&y(e.index.name),await l.upsertNow(e.index.name,{embed:e.index.embed,id:i.id,input:e.value,metadata:e.index.metadata?I(t,e.index.metadata):void 0,namespace:n})}),...f.map(([e,p])=>async()=>{!c&&n===void 0&&y(e),await l.upsertNow(e,{embed:p.embed,id:i.id,input:p.select(t),metadata:p.metadata?.(t),namespace:n})})];await b(m,g,async e=>e())}};export{S as createContextVectors,C as createVectorSyncHook};