@convex-dev/rag 0.5.4 → 0.6.0

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 (82) hide show
  1. package/README.md +89 -82
  2. package/dist/client/index.d.ts +30 -26
  3. package/dist/client/index.d.ts.map +1 -1
  4. package/dist/client/index.js +2 -2
  5. package/dist/client/index.js.map +1 -1
  6. package/dist/component/_generated/api.d.ts +100 -481
  7. package/dist/component/_generated/api.d.ts.map +1 -1
  8. package/dist/component/_generated/api.js +10 -1
  9. package/dist/component/_generated/api.js.map +1 -1
  10. package/dist/component/_generated/component.d.ts +380 -0
  11. package/dist/component/_generated/component.d.ts.map +1 -0
  12. package/dist/component/_generated/component.js +11 -0
  13. package/dist/component/_generated/component.js.map +1 -0
  14. package/dist/component/_generated/dataModel.d.ts +4 -18
  15. package/dist/component/_generated/dataModel.d.ts.map +1 -0
  16. package/dist/component/_generated/dataModel.js +11 -0
  17. package/dist/component/_generated/dataModel.js.map +1 -0
  18. package/dist/component/_generated/server.d.ts +10 -38
  19. package/dist/component/_generated/server.d.ts.map +1 -1
  20. package/dist/component/_generated/server.js +9 -5
  21. package/dist/component/_generated/server.js.map +1 -1
  22. package/dist/component/chunks.d.ts +5 -5
  23. package/dist/component/chunks.d.ts.map +1 -1
  24. package/dist/component/chunks.js +11 -44
  25. package/dist/component/chunks.js.map +1 -1
  26. package/dist/component/embeddings/tables.d.ts +4 -5
  27. package/dist/component/embeddings/tables.d.ts.map +1 -1
  28. package/dist/component/embeddings/tables.js.map +1 -1
  29. package/dist/component/entries.d.ts +6 -6
  30. package/dist/component/namespaces.d.ts +8 -8
  31. package/dist/component/namespaces.d.ts.map +1 -1
  32. package/dist/component/namespaces.js +2 -2
  33. package/dist/component/namespaces.js.map +1 -1
  34. package/dist/component/schema.d.ts +185 -224
  35. package/dist/component/schema.d.ts.map +1 -1
  36. package/dist/component/search.d.ts +4 -3
  37. package/dist/component/search.d.ts.map +1 -1
  38. package/dist/component/search.js +1 -1
  39. package/dist/component/search.js.map +1 -1
  40. package/dist/shared.d.ts +9 -4
  41. package/dist/shared.d.ts.map +1 -1
  42. package/dist/shared.js +1 -4
  43. package/dist/shared.js.map +1 -1
  44. package/package.json +71 -43
  45. package/src/client/defaultChunker.test.ts +1 -1
  46. package/src/client/defaultChunker.ts +7 -7
  47. package/src/client/fileUtils.ts +3 -3
  48. package/src/client/hybridRank.ts +1 -1
  49. package/src/client/index.test.ts +18 -18
  50. package/src/client/index.ts +104 -84
  51. package/src/client/setup.test.ts +2 -2
  52. package/src/component/_generated/api.ts +152 -0
  53. package/src/component/_generated/component.ts +442 -0
  54. package/src/component/_generated/{server.d.ts → server.ts} +33 -21
  55. package/src/component/chunks.test.ts +14 -14
  56. package/src/component/chunks.ts +49 -82
  57. package/src/component/embeddings/importance.test.ts +4 -4
  58. package/src/component/embeddings/importance.ts +1 -1
  59. package/src/component/embeddings/index.test.ts +3 -4
  60. package/src/component/embeddings/index.ts +6 -6
  61. package/src/component/embeddings/tables.ts +9 -8
  62. package/src/component/entries.test.ts +10 -10
  63. package/src/component/entries.ts +29 -29
  64. package/src/component/filters.ts +8 -8
  65. package/src/component/namespaces.ts +31 -34
  66. package/src/component/schema.ts +2 -2
  67. package/src/component/search.test.ts +5 -5
  68. package/src/component/search.ts +8 -9
  69. package/src/component/setup.test.ts +2 -8
  70. package/src/shared.ts +47 -45
  71. package/src/test.ts +20 -0
  72. package/dist/client/types.d.ts +0 -29
  73. package/dist/client/types.d.ts.map +0 -1
  74. package/dist/client/types.js +0 -2
  75. package/dist/client/types.js.map +0 -1
  76. package/dist/package.json +0 -3
  77. package/src/client/types.ts +0 -69
  78. package/src/component/_generated/api.d.ts +0 -507
  79. package/src/component/_generated/api.js +0 -23
  80. package/src/component/_generated/server.js +0 -90
  81. package/src/vitest.config.ts +0 -7
  82. /package/src/component/_generated/{dataModel.d.ts → dataModel.ts} +0 -0
@@ -5,7 +5,6 @@ import { numberedFiltersFromNamedFilters, vNamedFilter } from "./filters.js";
5
5
  import { internal } from "./_generated/api.js";
6
6
  import {
7
7
  vEntry,
8
- type Entry,
9
8
  vSearchResult,
10
9
  type SearchResult,
11
10
  type EntryId,
@@ -22,7 +21,7 @@ export const search = action({
22
21
  limit: v.number(),
23
22
  vectorScoreThreshold: v.optional(v.number()),
24
23
  chunkContext: v.optional(
25
- v.object({ before: v.number(), after: v.number() })
24
+ v.object({ before: v.number(), after: v.number() }),
26
25
  ),
27
26
  },
28
27
  returns: v.object({
@@ -31,10 +30,10 @@ export const search = action({
31
30
  }),
32
31
  handler: async (
33
32
  ctx,
34
- args
33
+ args,
35
34
  ): Promise<{
36
35
  results: SearchResult[];
37
- entries: Entry[];
36
+ entries: Infer<typeof vEntry>[];
38
37
  }> => {
39
38
  const { modelId, embedding, filters, limit } = args;
40
39
  const namespace = await ctx.runQuery(
@@ -44,11 +43,11 @@ export const search = action({
44
43
  modelId,
45
44
  dimension: embedding.length,
46
45
  filterNames: filters.map((f) => f.name),
47
- }
46
+ },
48
47
  );
49
48
  if (!namespace) {
50
49
  console.debug(
51
- `No compatible namespace found for ${args.namespace} with model ${args.modelId} and dimension ${embedding.length} and filters ${filters.map((f) => f.name).join(", ")}.`
50
+ `No compatible namespace found for ${args.namespace} with model ${args.modelId} and dimension ${embedding.length} and filters ${filters.map((f) => f.name).join(", ")}.`,
52
51
  );
53
52
  return {
54
53
  results: [],
@@ -71,20 +70,20 @@ export const search = action({
71
70
  {
72
71
  embeddingIds: aboveThreshold.map((r) => r._id),
73
72
  chunkContext,
74
- }
73
+ },
75
74
  );
76
75
  return {
77
76
  results: ranges
78
77
  .map((r, i) => publicSearchResult(r, aboveThreshold[i]._score))
79
78
  .filter((r) => r !== null),
80
- entries,
79
+ entries: entries as Infer<typeof vEntry>[],
81
80
  };
82
81
  },
83
82
  });
84
83
 
85
84
  function publicSearchResult(
86
85
  r: Infer<typeof vRangeResult> | null,
87
- score: number
86
+ score: number,
88
87
  ): SearchResult | null {
89
88
  if (r === null) {
90
89
  return null;
@@ -3,17 +3,11 @@ import { test } from "vitest";
3
3
  import { convexTest } from "convex-test";
4
4
  import schema from "./schema.js";
5
5
  export const modules = import.meta.glob("./**/*.*s");
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
- );
6
+ import workpool from "@convex-dev/workpool/test";
13
7
 
14
8
  export function initConvexTest() {
15
9
  const t = convexTest(schema, modules);
16
- t.registerComponent("workpool", componentSchema, componentModules);
10
+ t.registerComponent("workpool", workpool.schema, workpool.modules);
17
11
  return t;
18
12
  }
19
13
 
package/src/shared.ts CHANGED
@@ -1,5 +1,11 @@
1
1
  import { v } from "convex/values";
2
- import type { Infer, Validator, Value, VObject } from "convex/values";
2
+ import type {
3
+ GenericId,
4
+ Infer,
5
+ Validator,
6
+ Value,
7
+ VObject,
8
+ } from "convex/values";
3
9
  import { vNamedFilter, type NamedFilter } from "./component/filters.js";
4
10
  import { brandedString } from "convex-helpers/validators";
5
11
  import type { FunctionReference } from "convex/server";
@@ -21,7 +27,7 @@ export const vSearchResult = v.object({
21
27
  v.object({
22
28
  text: v.string(),
23
29
  metadata: v.optional(v.record(v.string(), v.any())),
24
- })
30
+ }),
25
31
  ),
26
32
  startOrder: v.number(),
27
33
  score: v.number(),
@@ -32,7 +38,7 @@ export type SearchResult = Infer<typeof vSearchResult>;
32
38
  export const vStatus = v.union(
33
39
  v.literal("pending"),
34
40
  v.literal("ready"),
35
- v.literal("replaced")
41
+ v.literal("replaced"),
36
42
  );
37
43
  export const vActiveStatus = v.union(v.literal("pending"), v.literal("ready"));
38
44
  export type Status = Infer<typeof vStatus>;
@@ -77,10 +83,7 @@ export type VEntry<
77
83
  const _1: Entry = {} as Infer<typeof vEntry> & { status: "pending" | "ready" };
78
84
  const _2: Infer<typeof vEntry> = {} as Entry;
79
85
 
80
- export const vSearchEntry = v.object({
81
- ...vEntry.fields,
82
- text: v.string(),
83
- });
86
+ export const vSearchEntry = v.object({ ...vEntry.fields, text: v.string() });
84
87
 
85
88
  export type VSearchEntry<
86
89
  Filters extends Record<string, Value>,
@@ -95,9 +98,7 @@ export type VSearchEntry<
95
98
  export type SearchEntry<
96
99
  Filters extends Record<string, Value>,
97
100
  Metadata extends Record<string, Value>,
98
- > = Entry<Filters, Metadata> & {
99
- text: string;
100
- };
101
+ > = Entry<Filters, Metadata> & { text: string };
101
102
 
102
103
  export type EntryFilter<
103
104
  Filters extends Record<string, Value> = Record<string, Value>,
@@ -106,9 +107,7 @@ export type EntryFilter<
106
107
  }[keyof Filters & string];
107
108
 
108
109
  export type Entry<
109
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
110
110
  Filters extends Record<string, Value> = any,
111
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
112
111
  Metadata extends Record<string, Value> = any,
113
112
  > = {
114
113
  /** The entry's id, uniquely identifying the key + contents + namespace etc. */
@@ -136,10 +135,7 @@ export type Entry<
136
135
  /** Whether this entry's contents have all been inserted and indexed. */
137
136
  status: "pending" | "ready";
138
137
  }
139
- | {
140
- status: "replaced";
141
- replacedAt: number;
142
- }
138
+ | { status: "replaced"; replacedAt: number }
143
139
  );
144
140
 
145
141
  export const vChunk = v.object({
@@ -173,8 +169,8 @@ export function vPaginationResult<
173
169
  v.union(
174
170
  v.literal("SplitRecommended"),
175
171
  v.literal("SplitRequired"),
176
- v.null()
177
- )
172
+ v.null(),
173
+ ),
178
174
  ),
179
175
  });
180
176
  }
@@ -182,10 +178,7 @@ export function vPaginationResult<
182
178
  export type OnCompleteNamespace = FunctionReference<
183
179
  "mutation",
184
180
  "internal",
185
- {
186
- namespace: Namespace;
187
- replacedNamespace: Namespace | null;
188
- },
181
+ { namespace: Namespace; replacedNamespace: Namespace | null },
189
182
  null,
190
183
  string
191
184
  >;
@@ -197,32 +190,32 @@ export const vOnCompleteArgs = v.object({
197
190
  error: v.optional(v.string()),
198
191
  });
199
192
 
200
- export type OnComplete<
201
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
193
+ export type OnCompleteArgs<
202
194
  Filters extends Record<string, Value> = any,
203
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
204
195
  EntryMetadata extends Record<string, Value> = any,
205
- > = FunctionReference<
196
+ > = {
197
+ /**
198
+ * The namespace that the entry belongs to.
199
+ */
200
+ namespace: Namespace;
201
+ /**
202
+ * The entry that was added.
203
+ */
204
+ entry: Entry<Filters, EntryMetadata>;
205
+ /**
206
+ * The previous "ready" entry with the same key that was replaced.
207
+ */
208
+ replacedEntry: Entry<Filters, EntryMetadata> | undefined;
209
+ /**
210
+ * If async generation failed, this is the error.
211
+ */
212
+ error: string | undefined;
213
+ };
214
+
215
+ export type OnComplete = FunctionReference<
206
216
  "mutation",
207
217
  "internal",
208
- {
209
- /**
210
- * The namespace that the entry belongs to.
211
- */
212
- namespace: Namespace;
213
- /**
214
- * The entry that was added.
215
- */
216
- entry: Entry<Filters, EntryMetadata>;
217
- /**
218
- * The previous "ready" entry with the same key that was replaced.
219
- */
220
- replacedEntry: Entry<Filters, EntryMetadata> | undefined;
221
- /**
222
- * If async generation failed, this is the error.
223
- */
224
- error: string | undefined;
225
- },
218
+ IdsToStrings<Infer<typeof vOnCompleteArgs>>,
226
219
  null
227
220
  >;
228
221
 
@@ -235,7 +228,7 @@ export const vChunkerArgs = v.object({
235
228
  export type ChunkerAction = FunctionReference<
236
229
  "action",
237
230
  "internal",
238
- Infer<typeof vChunkerArgs>,
231
+ IdsToStrings<Infer<typeof vChunkerArgs>>,
239
232
  null
240
233
  >;
241
234
 
@@ -253,3 +246,12 @@ export function filterNamesContain(existing: string[], args: string[]) {
253
246
  }
254
247
  return true;
255
248
  }
249
+
250
+ type IdsToStrings<T> =
251
+ T extends GenericId<string>
252
+ ? string
253
+ : T extends (infer U)[]
254
+ ? IdsToStrings<U>[]
255
+ : T extends Record<string, Value | undefined>
256
+ ? { [K in keyof T]: IdsToStrings<T[K]> }
257
+ : T;
package/src/test.ts ADDED
@@ -0,0 +1,20 @@
1
+ /// <reference types="vite/client" />
2
+ import type { TestConvex } from "convex-test";
3
+ import type { GenericSchema, SchemaDefinition } from "convex/server";
4
+ import workpool from "@convex-dev/workpool/test";
5
+ import schema from "./component/schema.js";
6
+ const modules = import.meta.glob("./component/**/*.ts");
7
+
8
+ /**
9
+ * Register the component with the test convex instance.
10
+ * @param t - The test convex instance, e.g. from calling `convexTest`.
11
+ * @param name - The name of the component, as registered in convex.config.ts.
12
+ */
13
+ export function register(
14
+ t: TestConvex<SchemaDefinition<GenericSchema, boolean>>,
15
+ name: string = "rag",
16
+ ) {
17
+ t.registerComponent(name, schema, modules);
18
+ workpool.register(t, "workpool");
19
+ }
20
+ export default { register, schema, modules };
@@ -1,29 +0,0 @@
1
- import type { Expand, FunctionReference, GenericActionCtx, GenericDataModel, GenericMutationCtx, GenericQueryCtx, StorageActionWriter, StorageReader } from "convex/server";
2
- import { type GenericId } from "convex/values";
3
- import type { Mounts } from "../component/_generated/api.js";
4
- export type RAGComponent = UseApi<Mounts>;
5
- export type RunQueryCtx = {
6
- runQuery: GenericQueryCtx<GenericDataModel>["runQuery"];
7
- };
8
- export type RunMutationCtx = {
9
- runQuery: GenericMutationCtx<GenericDataModel>["runQuery"];
10
- runMutation: GenericMutationCtx<GenericDataModel>["runMutation"];
11
- };
12
- export type RunActionCtx = {
13
- runQuery: GenericActionCtx<GenericDataModel>["runQuery"];
14
- runMutation: GenericActionCtx<GenericDataModel>["runMutation"];
15
- runAction: GenericActionCtx<GenericDataModel>["runAction"];
16
- };
17
- export type ActionCtx = RunActionCtx & {
18
- storage: StorageActionWriter;
19
- };
20
- export type QueryCtx = RunQueryCtx & {
21
- storage: StorageReader;
22
- };
23
- export type OpaqueIds<T> = T extends GenericId<infer _T> ? string : T extends (infer U)[] ? OpaqueIds<U>[] : T extends ArrayBuffer ? ArrayBuffer : T extends object ? {
24
- [K in keyof T]: OpaqueIds<T[K]>;
25
- } : T;
26
- export type UseApi<API> = Expand<{
27
- [mod in keyof API]: API[mod] extends FunctionReference<infer FType, "public", infer FArgs, infer FReturnType, infer FComponentPath> ? FunctionReference<FType, "internal", OpaqueIds<FArgs>, OpaqueIds<FReturnType>, FComponentPath> : UseApi<API[mod]>;
28
- }>;
29
- //# sourceMappingURL=types.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/client/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,MAAM,EACN,iBAAiB,EACjB,gBAAgB,EAChB,gBAAgB,EAChB,kBAAkB,EAClB,eAAe,EACf,mBAAmB,EACnB,aAAa,EACd,MAAM,eAAe,CAAC;AACvB,OAAO,EAAE,KAAK,SAAS,EAAE,MAAM,eAAe,CAAC;AAC/C,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,gCAAgC,CAAC;AAK7D,MAAM,MAAM,YAAY,GAAG,MAAM,CAAC,MAAM,CAAC,CAAC;AAI1C,MAAM,MAAM,WAAW,GAAG;IACxB,QAAQ,EAAE,eAAe,CAAC,gBAAgB,CAAC,CAAC,UAAU,CAAC,CAAC;CACzD,CAAC;AACF,MAAM,MAAM,cAAc,GAAG;IAC3B,QAAQ,EAAE,kBAAkB,CAAC,gBAAgB,CAAC,CAAC,UAAU,CAAC,CAAC;IAC3D,WAAW,EAAE,kBAAkB,CAAC,gBAAgB,CAAC,CAAC,aAAa,CAAC,CAAC;CAClE,CAAC;AACF,MAAM,MAAM,YAAY,GAAG;IACzB,QAAQ,EAAE,gBAAgB,CAAC,gBAAgB,CAAC,CAAC,UAAU,CAAC,CAAC;IACzD,WAAW,EAAE,gBAAgB,CAAC,gBAAgB,CAAC,CAAC,aAAa,CAAC,CAAC;IAC/D,SAAS,EAAE,gBAAgB,CAAC,gBAAgB,CAAC,CAAC,WAAW,CAAC,CAAC;CAC5D,CAAC;AACF,MAAM,MAAM,SAAS,GAAG,YAAY,GAAG;IACrC,OAAO,EAAE,mBAAmB,CAAC;CAC9B,CAAC;AACF,MAAM,MAAM,QAAQ,GAAG,WAAW,GAAG;IACnC,OAAO,EAAE,aAAa,CAAC;CACxB,CAAC;AAEF,MAAM,MAAM,SAAS,CAAC,CAAC,IACrB,CAAC,SAAS,SAAS,CAAC,MAAM,EAAE,CAAC,GACzB,MAAM,GACN,CAAC,SAAS,CAAC,MAAM,CAAC,CAAC,EAAE,GACnB,SAAS,CAAC,CAAC,CAAC,EAAE,GACd,CAAC,SAAS,WAAW,GACnB,WAAW,GACX,CAAC,SAAS,MAAM,GACd;KACG,CAAC,IAAI,MAAM,CAAC,GAAG,SAAS,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;CAChC,GACD,CAAC,CAAC;AAEd,MAAM,MAAM,MAAM,CAAC,GAAG,IAAI,MAAM,CAAC;KAC9B,GAAG,IAAI,MAAM,GAAG,GAAG,GAAG,CAAC,GAAG,CAAC,SAAS,iBAAiB,CACpD,MAAM,KAAK,EACX,QAAQ,EACR,MAAM,KAAK,EACX,MAAM,WAAW,EACjB,MAAM,cAAc,CACrB,GACG,iBAAiB,CACf,KAAK,EACL,UAAU,EACV,SAAS,CAAC,KAAK,CAAC,EAChB,SAAS,CAAC,WAAW,CAAC,EACtB,cAAc,CACf,GACD,MAAM,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;CACrB,CAAC,CAAC"}
@@ -1,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=types.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/client/types.ts"],"names":[],"mappings":""}
package/dist/package.json DELETED
@@ -1,3 +0,0 @@
1
- {
2
- "type": "module"
3
- }
@@ -1,69 +0,0 @@
1
- import type {
2
- Expand,
3
- FunctionReference,
4
- GenericActionCtx,
5
- GenericDataModel,
6
- GenericMutationCtx,
7
- GenericQueryCtx,
8
- StorageActionWriter,
9
- StorageReader,
10
- } from "convex/server";
11
- import { type GenericId } from "convex/values";
12
- import type { Mounts } from "../component/_generated/api.js";
13
-
14
- // UseApi<typeof api> is an alternative that has jump-to-definition but is
15
- // less stable and reliant on types within the component files, which can cause
16
- // issues where passing `components.foo` doesn't match the argument
17
- export type RAGComponent = UseApi<Mounts>;
18
-
19
- // Type utils follow
20
-
21
- export type RunQueryCtx = {
22
- runQuery: GenericQueryCtx<GenericDataModel>["runQuery"];
23
- };
24
- export type RunMutationCtx = {
25
- runQuery: GenericMutationCtx<GenericDataModel>["runQuery"];
26
- runMutation: GenericMutationCtx<GenericDataModel>["runMutation"];
27
- };
28
- export type RunActionCtx = {
29
- runQuery: GenericActionCtx<GenericDataModel>["runQuery"];
30
- runMutation: GenericActionCtx<GenericDataModel>["runMutation"];
31
- runAction: GenericActionCtx<GenericDataModel>["runAction"];
32
- };
33
- export type ActionCtx = RunActionCtx & {
34
- storage: StorageActionWriter;
35
- };
36
- export type QueryCtx = RunQueryCtx & {
37
- storage: StorageReader;
38
- };
39
-
40
- export type OpaqueIds<T> =
41
- T extends GenericId<infer _T>
42
- ? string
43
- : T extends (infer U)[]
44
- ? OpaqueIds<U>[]
45
- : T extends ArrayBuffer
46
- ? ArrayBuffer
47
- : T extends object
48
- ? {
49
- [K in keyof T]: OpaqueIds<T[K]>;
50
- }
51
- : T;
52
-
53
- export type UseApi<API> = Expand<{
54
- [mod in keyof API]: API[mod] extends FunctionReference<
55
- infer FType,
56
- "public",
57
- infer FArgs,
58
- infer FReturnType,
59
- infer FComponentPath
60
- >
61
- ? FunctionReference<
62
- FType,
63
- "internal",
64
- OpaqueIds<FArgs>,
65
- OpaqueIds<FReturnType>,
66
- FComponentPath
67
- >
68
- : UseApi<API[mod]>;
69
- }>;