@lunora/ai 1.0.0-alpha.56 → 1.0.0-alpha.57
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 @@
|
|
|
1
|
+
const f=t=>{const u=t.delayMs??0,r=(i,d)=>t.id===void 0?d:t.id(i),c=i=>i===void 0?void 0:t.text(i),o=async(i,d)=>{await i.scheduler.runAfter(u,t.action,d)};return{afterDelete:async(i,d)=>{const e=d.previous??d.doc;await o(i,{deleted:!0,id:e===void 0?d.id:r(e,d.id)})},afterInsert:async(i,d)=>{const e=c(d.doc);e===void 0||d.doc===void 0||await o(i,{id:r(d.doc,d.id),text:e})},afterUpdate:async(i,d)=>{if(d.doc===void 0)return;const e=c(d.doc),v=c(d.previous),a=r(d.doc,d.id),s=d.previous===void 0?a:r(d.previous,d.id);if(s!==a)await o(i,{deleted:!0,id:s});else if(e===v)return;await(e===void 0?o(i,{deleted:!0,id:a}):o(i,{id:a,text:e}))}}};export{f as ragSyncTriggers};
|
package/dist/rag/index.d.mts
CHANGED
|
@@ -555,4 +555,107 @@ declare const hybridRank: (vectorResults: ReadonlyArray<RetrievedChunk>, textRes
|
|
|
555
555
|
* @experimental
|
|
556
556
|
*/
|
|
557
557
|
declare const bm25LexicalStore: () => RagLexicalStore;
|
|
558
|
-
|
|
558
|
+
/**
|
|
559
|
+
* Keep a RAG index in step with a table, so the table IS the index.
|
|
560
|
+
*
|
|
561
|
+
* `rag.index(...)` is a manual call, which means an app that edits a document
|
|
562
|
+
* has to remember to re-index it — and a forgotten call is invisible: retrieval
|
|
563
|
+
* keeps answering, just from stale text. The fix is to hang the re-index off the
|
|
564
|
+
* write itself.
|
|
565
|
+
*
|
|
566
|
+
* Embedding is network I/O, so it cannot run inside the mutation that wrote the
|
|
567
|
+
* row. The bridge is the two seams that already exist: a table `.triggers()`
|
|
568
|
+
* handler runs in the write path and can `ctx.scheduler.runAfter(...)`, so the
|
|
569
|
+
* trigger records the intent and an internal ACTION does the embedding a moment
|
|
570
|
+
* later. That is what {@link ragSyncTriggers} wires up.
|
|
571
|
+
*
|
|
572
|
+
* Re-indexing unchanged text is already cheap — `rag.index` short-circuits on a
|
|
573
|
+
* content hash — but this skips scheduling entirely when an update didn't touch
|
|
574
|
+
* the indexed text, so an unrelated column edit costs nothing at all.
|
|
575
|
+
*/
|
|
576
|
+
/**
|
|
577
|
+
* A dispatchable function reference — the `internal.docs.reindex` you pass as
|
|
578
|
+
* `action`. Typed structurally (rather than `unknown`) so passing the wrong
|
|
579
|
+
* thing is a compile error: mis-wiring the action is the mistake this API is
|
|
580
|
+
* most likely to see, and it would otherwise surface as a silent no-op.
|
|
581
|
+
*/
|
|
582
|
+
interface RagSyncActionReference {
|
|
583
|
+
readonly __lunoraRef: string;
|
|
584
|
+
}
|
|
585
|
+
/** Structural slice of `ctx.scheduler` — enough to defer the re-index. */
|
|
586
|
+
interface RagSyncScheduler {
|
|
587
|
+
runAfter: (delayMs: number, target: unknown, args?: Record<string, unknown>) => Promise<string>;
|
|
588
|
+
}
|
|
589
|
+
/** Structural slice of the `TriggerCtx` a `.triggers()` handler receives. */
|
|
590
|
+
interface RagSyncTriggerContext {
|
|
591
|
+
readonly scheduler: RagSyncScheduler;
|
|
592
|
+
}
|
|
593
|
+
/** The trigger events this helper handles, narrowed to what it reads. */
|
|
594
|
+
interface RagSyncEvent {
|
|
595
|
+
readonly doc?: Record<string, unknown>;
|
|
596
|
+
readonly id: string;
|
|
597
|
+
readonly previous?: Record<string, unknown>;
|
|
598
|
+
}
|
|
599
|
+
/** What the scheduled action receives — one document to re-index, or one to drop. */
|
|
600
|
+
interface RagSyncArgs extends Record<string, unknown> {
|
|
601
|
+
/** `true` when the source row was deleted: call `rag.remove({ id })`. */
|
|
602
|
+
deleted?: boolean;
|
|
603
|
+
/** The source id — the same `id` you pass to `rag.index`/`rag.remove`. */
|
|
604
|
+
id: string;
|
|
605
|
+
/** The text to embed. Absent on a delete. */
|
|
606
|
+
text?: string;
|
|
607
|
+
}
|
|
608
|
+
interface RagSyncOptions<Document extends Record<string, unknown> = Record<string, unknown>> {
|
|
609
|
+
/**
|
|
610
|
+
* The internal action to dispatch — it receives {@link RagSyncArgs} and calls
|
|
611
|
+
* `rag.index` / `rag.remove`. An action, not a mutation: embedding is network
|
|
612
|
+
* I/O and never runs in the deterministic write path.
|
|
613
|
+
*/
|
|
614
|
+
action: RagSyncActionReference;
|
|
615
|
+
/**
|
|
616
|
+
* How long to wait before re-indexing. A small delay coalesces nothing by
|
|
617
|
+
* itself, but it keeps the embed off the write's own tail latency. Default
|
|
618
|
+
* `0` — as soon as the mutation commits.
|
|
619
|
+
*/
|
|
620
|
+
delayMs?: number;
|
|
621
|
+
/** The source id to index under. Defaults to the row's own id. */
|
|
622
|
+
id?: (document: Document) => string;
|
|
623
|
+
/** The text to embed. Return `undefined` to skip the row (a draft, an empty body). */
|
|
624
|
+
text: (document: Document) => string | undefined;
|
|
625
|
+
}
|
|
626
|
+
/** One trigger definition, structurally — matches what `.triggers((t) => …)` returns. */
|
|
627
|
+
type RagSyncHandler = (context: RagSyncTriggerContext, event: RagSyncEvent) => Promise<void>;
|
|
628
|
+
/**
|
|
629
|
+
* Build the three write-path handlers that keep a RAG index in step with a
|
|
630
|
+
* table. Wire them into the table's `.triggers()`:
|
|
631
|
+
*
|
|
632
|
+
* ```ts
|
|
633
|
+
* const sync = ragSyncTriggers({ action: internal.docs.reindex, text: (doc) => doc.body });
|
|
634
|
+
*
|
|
635
|
+
* export const schema = defineSchema({
|
|
636
|
+
* docs: defineTable({ body: v.string(), title: v.string() }).triggers((t) => ({
|
|
637
|
+
* ragDelete: t.afterDelete(sync.afterDelete),
|
|
638
|
+
* ragInsert: t.afterInsert(sync.afterInsert),
|
|
639
|
+
* ragUpdate: t.afterUpdate(sync.afterUpdate),
|
|
640
|
+
* })),
|
|
641
|
+
* });
|
|
642
|
+
* ```
|
|
643
|
+
*
|
|
644
|
+
* The action on the other end is three lines:
|
|
645
|
+
*
|
|
646
|
+
* ```ts
|
|
647
|
+
* export const reindex = internalAction.input({ deleted: v.optional(v.boolean()), id: v.string(), text: v.optional(v.string()) }).action(
|
|
648
|
+
* async ({ args, ctx }) => {
|
|
649
|
+
* const rag = docsRag(ctx);
|
|
650
|
+
*
|
|
651
|
+
* await (args.deleted === true || args.text === undefined ? rag.remove({ id: args.id }) : rag.index({ id: args.id, text: args.text }));
|
|
652
|
+
* },
|
|
653
|
+
* );
|
|
654
|
+
* ```
|
|
655
|
+
*/
|
|
656
|
+
declare const ragSyncTriggers: <Document extends Record<string, unknown> = Record<string, unknown>>(options: RagSyncOptions<Document>) => {
|
|
657
|
+
afterDelete: RagSyncHandler;
|
|
658
|
+
afterInsert: RagSyncHandler;
|
|
659
|
+
afterUpdate: RagSyncHandler;
|
|
660
|
+
};
|
|
661
|
+
export { type IndexInput, type IndexResult, type LexicalMatch, type Rag, type RagConfig, type RagContext, type RagEmbedder, type RagLexicalStore, type RagNamedFilter, type RagSource, type RagSyncActionReference, type RagSyncArgs, type RagSyncOptions, type RagTextStore, type RagToolOptions, type RagVectorMatch, type RagVectorMatches, type RagVectorQueryInput, type RagVectorRecord, type RagVectorUpsertInput, type RagVectors, type RemoveInput, type RetrieveOptions, type RetrieveResult, type RetrievedChunk, type StoredRagChunk, bm25LexicalStore, contentHash, defineRag, fixedWindowChunks, guessMimeTypeFromExtension, hybridRank, ragSyncTriggers };
|
package/dist/rag/index.d.ts
CHANGED
|
@@ -555,4 +555,107 @@ declare const hybridRank: (vectorResults: ReadonlyArray<RetrievedChunk>, textRes
|
|
|
555
555
|
* @experimental
|
|
556
556
|
*/
|
|
557
557
|
declare const bm25LexicalStore: () => RagLexicalStore;
|
|
558
|
-
|
|
558
|
+
/**
|
|
559
|
+
* Keep a RAG index in step with a table, so the table IS the index.
|
|
560
|
+
*
|
|
561
|
+
* `rag.index(...)` is a manual call, which means an app that edits a document
|
|
562
|
+
* has to remember to re-index it — and a forgotten call is invisible: retrieval
|
|
563
|
+
* keeps answering, just from stale text. The fix is to hang the re-index off the
|
|
564
|
+
* write itself.
|
|
565
|
+
*
|
|
566
|
+
* Embedding is network I/O, so it cannot run inside the mutation that wrote the
|
|
567
|
+
* row. The bridge is the two seams that already exist: a table `.triggers()`
|
|
568
|
+
* handler runs in the write path and can `ctx.scheduler.runAfter(...)`, so the
|
|
569
|
+
* trigger records the intent and an internal ACTION does the embedding a moment
|
|
570
|
+
* later. That is what {@link ragSyncTriggers} wires up.
|
|
571
|
+
*
|
|
572
|
+
* Re-indexing unchanged text is already cheap — `rag.index` short-circuits on a
|
|
573
|
+
* content hash — but this skips scheduling entirely when an update didn't touch
|
|
574
|
+
* the indexed text, so an unrelated column edit costs nothing at all.
|
|
575
|
+
*/
|
|
576
|
+
/**
|
|
577
|
+
* A dispatchable function reference — the `internal.docs.reindex` you pass as
|
|
578
|
+
* `action`. Typed structurally (rather than `unknown`) so passing the wrong
|
|
579
|
+
* thing is a compile error: mis-wiring the action is the mistake this API is
|
|
580
|
+
* most likely to see, and it would otherwise surface as a silent no-op.
|
|
581
|
+
*/
|
|
582
|
+
interface RagSyncActionReference {
|
|
583
|
+
readonly __lunoraRef: string;
|
|
584
|
+
}
|
|
585
|
+
/** Structural slice of `ctx.scheduler` — enough to defer the re-index. */
|
|
586
|
+
interface RagSyncScheduler {
|
|
587
|
+
runAfter: (delayMs: number, target: unknown, args?: Record<string, unknown>) => Promise<string>;
|
|
588
|
+
}
|
|
589
|
+
/** Structural slice of the `TriggerCtx` a `.triggers()` handler receives. */
|
|
590
|
+
interface RagSyncTriggerContext {
|
|
591
|
+
readonly scheduler: RagSyncScheduler;
|
|
592
|
+
}
|
|
593
|
+
/** The trigger events this helper handles, narrowed to what it reads. */
|
|
594
|
+
interface RagSyncEvent {
|
|
595
|
+
readonly doc?: Record<string, unknown>;
|
|
596
|
+
readonly id: string;
|
|
597
|
+
readonly previous?: Record<string, unknown>;
|
|
598
|
+
}
|
|
599
|
+
/** What the scheduled action receives — one document to re-index, or one to drop. */
|
|
600
|
+
interface RagSyncArgs extends Record<string, unknown> {
|
|
601
|
+
/** `true` when the source row was deleted: call `rag.remove({ id })`. */
|
|
602
|
+
deleted?: boolean;
|
|
603
|
+
/** The source id — the same `id` you pass to `rag.index`/`rag.remove`. */
|
|
604
|
+
id: string;
|
|
605
|
+
/** The text to embed. Absent on a delete. */
|
|
606
|
+
text?: string;
|
|
607
|
+
}
|
|
608
|
+
interface RagSyncOptions<Document extends Record<string, unknown> = Record<string, unknown>> {
|
|
609
|
+
/**
|
|
610
|
+
* The internal action to dispatch — it receives {@link RagSyncArgs} and calls
|
|
611
|
+
* `rag.index` / `rag.remove`. An action, not a mutation: embedding is network
|
|
612
|
+
* I/O and never runs in the deterministic write path.
|
|
613
|
+
*/
|
|
614
|
+
action: RagSyncActionReference;
|
|
615
|
+
/**
|
|
616
|
+
* How long to wait before re-indexing. A small delay coalesces nothing by
|
|
617
|
+
* itself, but it keeps the embed off the write's own tail latency. Default
|
|
618
|
+
* `0` — as soon as the mutation commits.
|
|
619
|
+
*/
|
|
620
|
+
delayMs?: number;
|
|
621
|
+
/** The source id to index under. Defaults to the row's own id. */
|
|
622
|
+
id?: (document: Document) => string;
|
|
623
|
+
/** The text to embed. Return `undefined` to skip the row (a draft, an empty body). */
|
|
624
|
+
text: (document: Document) => string | undefined;
|
|
625
|
+
}
|
|
626
|
+
/** One trigger definition, structurally — matches what `.triggers((t) => …)` returns. */
|
|
627
|
+
type RagSyncHandler = (context: RagSyncTriggerContext, event: RagSyncEvent) => Promise<void>;
|
|
628
|
+
/**
|
|
629
|
+
* Build the three write-path handlers that keep a RAG index in step with a
|
|
630
|
+
* table. Wire them into the table's `.triggers()`:
|
|
631
|
+
*
|
|
632
|
+
* ```ts
|
|
633
|
+
* const sync = ragSyncTriggers({ action: internal.docs.reindex, text: (doc) => doc.body });
|
|
634
|
+
*
|
|
635
|
+
* export const schema = defineSchema({
|
|
636
|
+
* docs: defineTable({ body: v.string(), title: v.string() }).triggers((t) => ({
|
|
637
|
+
* ragDelete: t.afterDelete(sync.afterDelete),
|
|
638
|
+
* ragInsert: t.afterInsert(sync.afterInsert),
|
|
639
|
+
* ragUpdate: t.afterUpdate(sync.afterUpdate),
|
|
640
|
+
* })),
|
|
641
|
+
* });
|
|
642
|
+
* ```
|
|
643
|
+
*
|
|
644
|
+
* The action on the other end is three lines:
|
|
645
|
+
*
|
|
646
|
+
* ```ts
|
|
647
|
+
* export const reindex = internalAction.input({ deleted: v.optional(v.boolean()), id: v.string(), text: v.optional(v.string()) }).action(
|
|
648
|
+
* async ({ args, ctx }) => {
|
|
649
|
+
* const rag = docsRag(ctx);
|
|
650
|
+
*
|
|
651
|
+
* await (args.deleted === true || args.text === undefined ? rag.remove({ id: args.id }) : rag.index({ id: args.id, text: args.text }));
|
|
652
|
+
* },
|
|
653
|
+
* );
|
|
654
|
+
* ```
|
|
655
|
+
*/
|
|
656
|
+
declare const ragSyncTriggers: <Document extends Record<string, unknown> = Record<string, unknown>>(options: RagSyncOptions<Document>) => {
|
|
657
|
+
afterDelete: RagSyncHandler;
|
|
658
|
+
afterInsert: RagSyncHandler;
|
|
659
|
+
afterUpdate: RagSyncHandler;
|
|
660
|
+
};
|
|
661
|
+
export { type IndexInput, type IndexResult, type LexicalMatch, type Rag, type RagConfig, type RagContext, type RagEmbedder, type RagLexicalStore, type RagNamedFilter, type RagSource, type RagSyncActionReference, type RagSyncArgs, type RagSyncOptions, type RagTextStore, type RagToolOptions, type RagVectorMatch, type RagVectorMatches, type RagVectorQueryInput, type RagVectorRecord, type RagVectorUpsertInput, type RagVectors, type RemoveInput, type RetrieveOptions, type RetrieveResult, type RetrievedChunk, type StoredRagChunk, bm25LexicalStore, contentHash, defineRag, fixedWindowChunks, guessMimeTypeFromExtension, hybridRank, ragSyncTriggers };
|
package/dist/rag/index.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{default as
|
|
1
|
+
import{default as o}from"../packem_shared/fixedWindowChunks-XJRXHEoz.mjs";import{default as a}from"../packem_shared/defineRag-Drf-Vn0B.mjs";import{contentHash as s,guessMimeTypeFromExtension as m}from"../packem_shared/contentHash-BIn6ECP8.mjs";import{default as x}from"../packem_shared/hybridRank-U6PmGuz1.mjs";import{default as i}from"../packem_shared/bm25LexicalStore-RA9sesFC.mjs";import{ragSyncTriggers as u}from"../packem_shared/ragSyncTriggers-BY8QgX4d.mjs";export{i as bm25LexicalStore,s as contentHash,a as defineRag,o as fixedWindowChunks,m as guessMimeTypeFromExtension,x as hybridRank,u as ragSyncTriggers};
|
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.57",
|
|
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.21",
|
|
57
57
|
"ai": "7.0.37",
|
|
58
58
|
"workers-ai-provider": "4.0.0"
|
|
59
59
|
},
|