@convex-dev/rag 0.3.3-alpha.0 → 0.3.3

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.
@@ -14,13 +14,15 @@ import {
14
14
  import { api, internal } from "./_generated/api.js";
15
15
  import type { Doc, Id } from "./_generated/dataModel.js";
16
16
  import {
17
+ action,
17
18
  internalMutation,
19
+ internalQuery,
18
20
  mutation,
19
21
  query,
20
22
  type MutationCtx,
21
23
  type QueryCtx,
22
24
  } from "./_generated/server.js";
23
- import { deleteChunksPage, insertChunks } from "./chunks.js";
25
+ import { deleteChunksPageHandler, insertChunks } from "./chunks.js";
24
26
  import schema, { type StatusWithOnComplete } from "./schema.js";
25
27
  import { mergedStream } from "convex-helpers/server/stream";
26
28
  import { stream } from "convex-helpers/server/stream";
@@ -36,6 +38,7 @@ import {
36
38
  Workpool,
37
39
  } from "@convex-dev/workpool";
38
40
  import { components } from "./_generated/api.js";
41
+ import { doc } from "convex-helpers/validators";
39
42
 
40
43
  const workpool = new Workpool(components.workpool, {
41
44
  retryActionsByDefault: true,
@@ -197,7 +200,6 @@ export const add = mutation({
197
200
  entryId: v.id("entries"),
198
201
  status: vStatus,
199
202
  created: v.boolean(),
200
- replacedEntry: v.union(vEntry, v.null()),
201
203
  }),
202
204
  handler: async (ctx, args) => {
203
205
  const { namespaceId, key } = args.entry;
@@ -213,7 +215,6 @@ export const add = mutation({
213
215
  entryId: existing._id,
214
216
  status: existing.status.kind,
215
217
  created: false,
216
- replacedEntry: null,
217
218
  };
218
219
  }
219
220
  const version = existing ? existing.version + 1 : 0;
@@ -223,26 +224,24 @@ export const add = mutation({
223
224
  status: { kind: "pending", onComplete: args.onComplete },
224
225
  });
225
226
  if (args.allChunks) {
226
- await insertChunks(ctx, {
227
+ const { status } = await insertChunks(ctx, {
227
228
  entryId,
228
229
  startOrder: 0,
229
230
  chunks: args.allChunks,
230
231
  });
231
- const { replacedEntry } = await promoteToReadyHandler(ctx, {
232
- entryId,
233
- });
232
+ if (status === "ready") {
233
+ await promoteToReadyHandler(ctx, { entryId });
234
+ }
234
235
  return {
235
236
  entryId,
236
- status: "ready" as const,
237
+ status,
237
238
  created: true,
238
- replacedEntry,
239
239
  };
240
240
  }
241
241
  return {
242
242
  entryId,
243
243
  status: "pending" as const,
244
244
  created: true,
245
- replacedEntry: null,
246
245
  };
247
246
  },
248
247
  });
@@ -546,7 +545,7 @@ async function deleteAsyncHandler(
546
545
  if (!entry) {
547
546
  throw new Error(`Entry ${entryId} not found`);
548
547
  }
549
- const status = await deleteChunksPage(ctx, { entryId, startOrder });
548
+ const status = await deleteChunksPageHandler(ctx, { entryId, startOrder });
550
549
  if (status.isDone) {
551
550
  await ctx.db.delete(entryId);
552
551
  } else {
@@ -556,3 +555,110 @@ async function deleteAsyncHandler(
556
555
  });
557
556
  }
558
557
  }
558
+
559
+ export const deleteSync = action({
560
+ args: { entryId: v.id("entries") },
561
+ returns: v.null(),
562
+ handler: async (ctx, { entryId }) => {
563
+ let startOrder = 0;
564
+ while (true) {
565
+ const status = await ctx.runMutation(internal.chunks.deleteChunksPage, {
566
+ entryId,
567
+ startOrder,
568
+ });
569
+ if (status.isDone) {
570
+ await ctx.runMutation(internal.entries._del, { entryId });
571
+ break;
572
+ }
573
+ startOrder = status.nextStartOrder;
574
+ }
575
+ },
576
+ });
577
+
578
+ export const _del = internalMutation({
579
+ args: { entryId: v.id("entries") },
580
+ returns: v.null(),
581
+ handler: async (ctx, args) => {
582
+ await ctx.db.delete(args.entryId);
583
+ },
584
+ });
585
+
586
+ export const deleteByKeyAsync = mutation({
587
+ args: v.object({
588
+ namespaceId: v.id("namespaces"),
589
+ key: v.string(),
590
+ beforeVersion: v.optional(v.number()),
591
+ }),
592
+ returns: v.null(),
593
+ handler: async (ctx, args) => {
594
+ const entries = await getEntriesByKey(ctx, args);
595
+ for await (const entry of entries) {
596
+ await workpool.enqueueMutation(ctx, api.entries.deleteAsync, {
597
+ entryId: entry._id,
598
+ startOrder: 0,
599
+ });
600
+ }
601
+ if (entries.length === 100) {
602
+ await workpool.enqueueMutation(ctx, api.entries.deleteByKeyAsync, {
603
+ namespaceId: args.namespaceId,
604
+ key: args.key,
605
+ beforeVersion: entries[entries.length - 1].version,
606
+ });
607
+ }
608
+ },
609
+ });
610
+
611
+ async function getEntriesByKey(
612
+ ctx: QueryCtx,
613
+ args: { namespaceId: Id<"namespaces">; key: string; beforeVersion?: number }
614
+ ): Promise<Doc<"entries">[]> {
615
+ return mergedStream(
616
+ statuses.map((status) =>
617
+ stream(ctx.db, schema)
618
+ .query("entries")
619
+ .withIndex("namespaceId_status_key_version", (q) =>
620
+ q
621
+ .eq("namespaceId", args.namespaceId)
622
+ .eq("status.kind", status)
623
+ .eq("key", args.key)
624
+ .lt("version", args.beforeVersion ?? Infinity)
625
+ )
626
+ .order("desc")
627
+ ),
628
+ ["version"]
629
+ ).take(100);
630
+ }
631
+
632
+ export const getEntriesForNamespaceByKey = internalQuery({
633
+ args: {
634
+ namespaceId: v.id("namespaces"),
635
+ key: v.string(),
636
+ beforeVersion: v.optional(v.number()),
637
+ },
638
+ returns: v.array(doc(schema, "entries")),
639
+ handler: getEntriesByKey,
640
+ });
641
+
642
+ export const deleteByKeySync = action({
643
+ args: {
644
+ namespaceId: v.id("namespaces"),
645
+ key: v.string(),
646
+ },
647
+ returns: v.null(),
648
+ handler: async (ctx, args) => {
649
+ while (true) {
650
+ const entries: Doc<"entries">[] = await ctx.runQuery(
651
+ internal.entries.getEntriesForNamespaceByKey,
652
+ { namespaceId: args.namespaceId, key: args.key }
653
+ );
654
+ for await (const entry of entries) {
655
+ await ctx.runAction(api.entries.deleteSync, {
656
+ entryId: entry._id,
657
+ });
658
+ }
659
+ if (entries.length <= 100) {
660
+ break;
661
+ }
662
+ }
663
+ },
664
+ });
@@ -1,5 +1,20 @@
1
1
  /// <reference types="vite/client" />
2
2
  import { test } from "vitest";
3
+ import { convexTest } from "convex-test";
4
+ import schema from "./schema.js";
3
5
  export const modules = import.meta.glob("./**/*.*s");
4
6
 
7
+ // Sorry about everything
8
+ import componentSchema from "../../node_modules/@convex-dev/workpool/src/component/schema.js";
9
+ export { componentSchema };
10
+ export const componentModules = import.meta.glob(
11
+ "../../node_modules/@convex-dev/workpool/src/component/**/*.ts"
12
+ );
13
+
14
+ export function initConvexTest() {
15
+ const t = convexTest(schema, modules);
16
+ t.registerComponent("workpool", componentSchema, componentModules);
17
+ return t;
18
+ }
19
+
5
20
  test("setup", () => {});
@@ -0,0 +1 @@
1
+ {"version":"3.2.4","results":[[":component/chunks.test.ts",{"duration":0,"failed":false}]]}