@convex-dev/rag 0.3.0 → 0.3.2

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.
Files changed (42) hide show
  1. package/README.md +463 -121
  2. package/dist/client/defaultChunker.d.ts.map +1 -1
  3. package/dist/client/defaultChunker.js +47 -16
  4. package/dist/client/defaultChunker.js.map +1 -1
  5. package/dist/client/fileUtils.d.ts +4 -2
  6. package/dist/client/fileUtils.d.ts.map +1 -1
  7. package/dist/client/fileUtils.js +5 -3
  8. package/dist/client/fileUtils.js.map +1 -1
  9. package/dist/client/index.d.ts +19 -15
  10. package/dist/client/index.d.ts.map +1 -1
  11. package/dist/client/index.js +15 -11
  12. package/dist/client/index.js.map +1 -1
  13. package/dist/component/_generated/api.d.ts +11 -4
  14. package/dist/component/chunks.d.ts +1 -0
  15. package/dist/component/chunks.d.ts.map +1 -1
  16. package/dist/component/chunks.js +2 -1
  17. package/dist/component/chunks.js.map +1 -1
  18. package/dist/component/entries.d.ts +8 -8
  19. package/dist/component/entries.d.ts.map +1 -1
  20. package/dist/component/entries.js +30 -17
  21. package/dist/component/entries.js.map +1 -1
  22. package/dist/component/namespaces.d.ts +3 -3
  23. package/dist/component/namespaces.js +4 -4
  24. package/dist/component/namespaces.js.map +1 -1
  25. package/dist/component/schema.d.ts +31 -31
  26. package/dist/shared.d.ts +28 -11
  27. package/dist/shared.d.ts.map +1 -1
  28. package/dist/shared.js +2 -1
  29. package/dist/shared.js.map +1 -1
  30. package/package.json +1 -6
  31. package/src/client/defaultChunker.test.ts +1 -1
  32. package/src/client/defaultChunker.ts +73 -17
  33. package/src/client/fileUtils.ts +8 -4
  34. package/src/client/index.test.ts +28 -24
  35. package/src/client/index.ts +30 -24
  36. package/src/component/_generated/api.d.ts +11 -4
  37. package/src/component/chunks.test.ts +2 -0
  38. package/src/component/chunks.ts +2 -1
  39. package/src/component/entries.test.ts +16 -16
  40. package/src/component/entries.ts +31 -19
  41. package/src/component/namespaces.ts +4 -4
  42. package/src/shared.ts +15 -7
@@ -1,7 +1,7 @@
1
1
  import { assert, omit } from "convex-helpers";
2
2
  import { createFunctionHandle, paginationOptsValidator } from "convex/server";
3
3
  import { v, type Value } from "convex/values";
4
- import type { ChunkerAction, EntryFilterValues, EntryId } from "../shared.js";
4
+ import type { ChunkerAction, EntryFilter, EntryId } from "../shared.js";
5
5
  import {
6
6
  statuses,
7
7
  vActiveStatus,
@@ -115,7 +115,7 @@ function workpoolName(
115
115
  key: string | undefined,
116
116
  entryId: Id<"entries">
117
117
  ) {
118
- return `async-chunker-${namespace}-${key ? key + "-" + entryId : entryId}`;
118
+ return `rag-async-${namespace}-${key ? key + "-" + entryId : entryId}`;
119
119
  }
120
120
 
121
121
  export const addAsyncOnComplete = internalMutation({
@@ -197,7 +197,7 @@ export const add = mutation({
197
197
  entryId: v.id("entries"),
198
198
  status: vStatus,
199
199
  created: v.boolean(),
200
- replacedVersion: v.union(vEntry, v.null()),
200
+ replacedEntry: v.union(vEntry, v.null()),
201
201
  }),
202
202
  handler: async (ctx, args) => {
203
203
  const { namespaceId, key } = args.entry;
@@ -213,7 +213,7 @@ export const add = mutation({
213
213
  entryId: existing._id,
214
214
  status: existing.status.kind,
215
215
  created: false,
216
- replacedVersion: null,
216
+ replacedEntry: null,
217
217
  };
218
218
  }
219
219
  const version = existing ? existing.version + 1 : 0;
@@ -228,21 +228,21 @@ export const add = mutation({
228
228
  startOrder: 0,
229
229
  chunks: args.allChunks,
230
230
  });
231
- const { replacedVersion } = await promoteToReadyHandler(ctx, {
231
+ const { replacedEntry } = await promoteToReadyHandler(ctx, {
232
232
  entryId,
233
233
  });
234
234
  return {
235
235
  entryId,
236
236
  status: "ready" as const,
237
237
  created: true,
238
- replacedVersion,
238
+ replacedEntry,
239
239
  };
240
240
  }
241
241
  return {
242
242
  entryId,
243
243
  status: "pending" as const,
244
244
  created: true,
245
- replacedVersion: null,
245
+ replacedEntry: null,
246
246
  };
247
247
  },
248
248
  });
@@ -293,19 +293,20 @@ function entryIsSame(existing: Doc<"entries">, newEntry: AddEntryArgs) {
293
293
  */
294
294
  export const list = query({
295
295
  args: {
296
- namespaceId: v.id("namespaces"),
296
+ namespaceId: v.optional(v.id("namespaces")),
297
297
  order: v.optional(v.union(v.literal("desc"), v.literal("asc"))),
298
298
  status: vStatus,
299
299
  paginationOpts: paginationOptsValidator,
300
300
  },
301
301
  returns: vPaginationResult(vEntry),
302
302
  handler: async (ctx, args) => {
303
+ const { namespaceId } = args;
303
304
  const results = await stream(ctx.db, schema)
304
305
  .query("entries")
305
306
  .withIndex("status_namespaceId", (q) =>
306
- q
307
- .eq("status.kind", args.status ?? "ready")
308
- .eq("namespaceId", args.namespaceId)
307
+ namespaceId
308
+ ? q.eq("status.kind", args.status).eq("namespaceId", namespaceId)
309
+ : q.eq("status.kind", args.status)
309
310
  )
310
311
  .order(args.order ?? "asc")
311
312
  .paginate(args.paginationOpts);
@@ -389,14 +390,14 @@ export const findByContentHash = query({
389
390
  * Note: this will not replace the chunks automatically, so you should first
390
391
  * call `replaceChunksPage` on all its chunks.
391
392
  * Edge case: if the entry has already been replaced, it will return the
392
- * same entry (replacedVersion.entryId === args.entryId).
393
+ * same entry (replacedEntry.entryId === args.entryId).
393
394
  */
394
395
  export const promoteToReady = mutation({
395
396
  args: v.object({
396
397
  entryId: v.id("entries"),
397
398
  }),
398
399
  returns: v.object({
399
- replacedVersion: v.union(vEntry, v.null()),
400
+ replacedEntry: v.union(vEntry, v.null()),
400
401
  }),
401
402
  handler: promoteToReadyHandler,
402
403
  });
@@ -411,12 +412,12 @@ async function promoteToReadyHandler(
411
412
  assert(namespace, `Namespace for ${entry.namespaceId} not found`);
412
413
  if (entry.status.kind === "ready") {
413
414
  console.debug(`Entry ${args.entryId} is already ready, skipping...`);
414
- return { replacedVersion: null };
415
+ return { replacedEntry: null };
415
416
  } else if (entry.status.kind === "replaced") {
416
417
  console.debug(
417
418
  `Entry ${args.entryId} is already replaced, returning the current version...`
418
419
  );
419
- return { replacedVersion: publicEntry(entry) };
420
+ return { replacedEntry: publicEntry(entry) };
420
421
  }
421
422
  const previousEntry = await getPreviousEntry(ctx, entry);
422
423
  // First mark the previous entry as replaced,
@@ -471,7 +472,7 @@ async function promoteToReadyHandler(
471
472
  );
472
473
  }
473
474
  return {
474
- replacedVersion: previousEntry ? publicEntry(previousEntry) : null,
475
+ replacedEntry: previousEntry ? publicEntry(previousEntry) : null,
475
476
  };
476
477
  }
477
478
 
@@ -496,7 +497,7 @@ export function publicEntry(entry: {
496
497
  _id: Id<"entries">;
497
498
  key?: string | undefined;
498
499
  importance: number;
499
- filterValues: EntryFilterValues[];
500
+ filterValues: EntryFilter[];
500
501
  contentHash?: string | undefined;
501
502
  title?: string | undefined;
502
503
  metadata?: Record<string, Value> | undefined;
@@ -504,7 +505,7 @@ export function publicEntry(entry: {
504
505
  }): Entry {
505
506
  const { key, importance, filterValues, contentHash, title, metadata } = entry;
506
507
 
507
- return {
508
+ const fields = {
508
509
  entryId: entry._id as unknown as EntryId,
509
510
  key,
510
511
  title,
@@ -512,8 +513,19 @@ export function publicEntry(entry: {
512
513
  importance,
513
514
  filterValues,
514
515
  contentHash,
515
- status: entry.status.kind,
516
516
  };
517
+ if (entry.status.kind === "replaced") {
518
+ return {
519
+ ...fields,
520
+ status: "replaced" as const,
521
+ replacedAt: entry.status.replacedAt,
522
+ };
523
+ } else {
524
+ return {
525
+ ...fields,
526
+ status: entry.status.kind,
527
+ };
528
+ }
517
529
  }
518
530
 
519
531
  export const deleteAsync = mutation({
@@ -188,7 +188,7 @@ export const promoteToReady = mutation({
188
188
  namespaceId: v.id("namespaces"),
189
189
  },
190
190
  returns: v.object({
191
- replacedVersion: v.union(v.null(), vNamespace),
191
+ replacedNamespace: v.union(v.null(), vNamespace),
192
192
  }),
193
193
  handler: promoteToReadyHandler,
194
194
  });
@@ -203,12 +203,12 @@ async function promoteToReadyHandler(
203
203
  console.debug(
204
204
  `Namespace ${args.namespaceId} is already ready, not promoting`
205
205
  );
206
- return { replacedVersion: null };
206
+ return { replacedNamespace: null };
207
207
  } else if (namespace.status.kind === "replaced") {
208
208
  console.debug(
209
209
  `Namespace ${args.namespaceId} is already replaced, not promoting and returning itself`
210
210
  );
211
- return { replacedVersion: publicNamespace(namespace) };
211
+ return { replacedNamespace: publicNamespace(namespace) };
212
212
  }
213
213
  const previousNamespace = await ctx.db
214
214
  .query("namespaces")
@@ -259,7 +259,7 @@ async function promoteToReadyHandler(
259
259
  })
260
260
  );
261
261
  return {
262
- replacedVersion: previousNamespace
262
+ replacedNamespace: previousNamespace
263
263
  ? publicNamespace(previousNamespace)
264
264
  : null,
265
265
  };
package/src/shared.ts CHANGED
@@ -60,6 +60,7 @@ export const vEntry = v.object({
60
60
  filterValues: v.array(vNamedFilter),
61
61
  contentHash: v.optional(v.string()),
62
62
  status: vStatus,
63
+ replacedAt: v.optional(v.number()),
63
64
  });
64
65
 
65
66
  export type VEntry<
@@ -72,8 +73,8 @@ export type VEntry<
72
73
  typeof vEntry.fieldPaths
73
74
  >;
74
75
 
75
- // Type assertion to keep us honest
76
- const _1: Entry = {} as Infer<typeof vEntry>;
76
+ // Type assertion to keep us honest (modulo the replacedAt field)
77
+ const _1: Entry = {} as Infer<typeof vEntry> & { status: "pending" | "ready" };
77
78
  const _2: Infer<typeof vEntry> = {} as Entry;
78
79
 
79
80
  export const vSearchEntry = v.object({
@@ -98,7 +99,7 @@ export type SearchEntry<
98
99
  text: string;
99
100
  };
100
101
 
101
- export type EntryFilterValues<
102
+ export type EntryFilter<
102
103
  Filters extends Record<string, Value> = Record<string, Value>,
103
104
  > = {
104
105
  [K in keyof Filters & string]: NamedFilter<K, Filters[K]>;
@@ -125,14 +126,21 @@ export type Entry<
125
126
  /** Filters that can be used to search for this entry.
126
127
  * Up to 4 filters are supported, of any type.
127
128
  */
128
- filterValues: EntryFilterValues<Filters>[];
129
+ filterValues: EntryFilter<Filters>[];
129
130
  /** Hash of the entry contents.
130
131
  * If supplied, it will avoid adding if the hash is the same.
131
132
  */
132
133
  contentHash?: string | undefined;
133
- /** Whether this entry's contents have all been inserted and indexed. */
134
- status: Status;
135
- };
134
+ } & (
135
+ | {
136
+ /** Whether this entry's contents have all been inserted and indexed. */
137
+ status: "pending" | "ready";
138
+ }
139
+ | {
140
+ status: "replaced";
141
+ replacedAt: number;
142
+ }
143
+ );
136
144
 
137
145
  export const vChunk = v.object({
138
146
  order: v.number(),