@convex-dev/rag 0.1.7
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.
- package/LICENSE +201 -0
- package/README.md +371 -0
- package/dist/client/_generated/_ignore.d.ts +1 -0
- package/dist/client/_generated/_ignore.d.ts.map +1 -0
- package/dist/client/_generated/_ignore.js +3 -0
- package/dist/client/_generated/_ignore.js.map +1 -0
- package/dist/client/defaultChunker.d.ts +15 -0
- package/dist/client/defaultChunker.d.ts.map +1 -0
- package/dist/client/defaultChunker.js +148 -0
- package/dist/client/defaultChunker.js.map +1 -0
- package/dist/client/fileUtils.d.ts +24 -0
- package/dist/client/fileUtils.d.ts.map +1 -0
- package/dist/client/fileUtils.js +179 -0
- package/dist/client/fileUtils.js.map +1 -0
- package/dist/client/index.d.ts +442 -0
- package/dist/client/index.d.ts.map +1 -0
- package/dist/client/index.js +597 -0
- package/dist/client/index.js.map +1 -0
- package/dist/client/types.d.ts +29 -0
- package/dist/client/types.d.ts.map +1 -0
- package/dist/client/types.js +2 -0
- package/dist/client/types.js.map +1 -0
- package/dist/component/_generated/api.d.ts +439 -0
- package/dist/component/_generated/api.d.ts.map +1 -0
- package/dist/component/_generated/api.js +22 -0
- package/dist/component/_generated/api.js.map +1 -0
- package/dist/component/_generated/dataModel.d.ts +60 -0
- package/dist/component/_generated/server.d.ts +149 -0
- package/dist/component/_generated/server.d.ts.map +1 -0
- package/dist/component/_generated/server.js +74 -0
- package/dist/component/_generated/server.js.map +1 -0
- package/dist/component/chunks.d.ts +139 -0
- package/dist/component/chunks.d.ts.map +1 -0
- package/dist/component/chunks.js +413 -0
- package/dist/component/chunks.js.map +1 -0
- package/dist/component/convex.config.d.ts +3 -0
- package/dist/component/convex.config.d.ts.map +1 -0
- package/dist/component/convex.config.js +6 -0
- package/dist/component/convex.config.js.map +1 -0
- package/dist/component/embeddings/importance.d.ts +21 -0
- package/dist/component/embeddings/importance.d.ts.map +1 -0
- package/dist/component/embeddings/importance.js +67 -0
- package/dist/component/embeddings/importance.js.map +1 -0
- package/dist/component/embeddings/index.d.ts +23 -0
- package/dist/component/embeddings/index.d.ts.map +1 -0
- package/dist/component/embeddings/index.js +54 -0
- package/dist/component/embeddings/index.js.map +1 -0
- package/dist/component/embeddings/tables.d.ts +39 -0
- package/dist/component/embeddings/tables.d.ts.map +1 -0
- package/dist/component/embeddings/tables.js +53 -0
- package/dist/component/embeddings/tables.js.map +1 -0
- package/dist/component/entries.d.ts +167 -0
- package/dist/component/entries.d.ts.map +1 -0
- package/dist/component/entries.js +409 -0
- package/dist/component/entries.js.map +1 -0
- package/dist/component/filters.d.ts +46 -0
- package/dist/component/filters.d.ts.map +1 -0
- package/dist/component/filters.js +72 -0
- package/dist/component/filters.js.map +1 -0
- package/dist/component/namespaces.d.ts +131 -0
- package/dist/component/namespaces.d.ts.map +1 -0
- package/dist/component/namespaces.js +222 -0
- package/dist/component/namespaces.js.map +1 -0
- package/dist/component/schema.d.ts +1697 -0
- package/dist/component/schema.d.ts.map +1 -0
- package/dist/component/schema.js +88 -0
- package/dist/component/schema.js.map +1 -0
- package/dist/component/search.d.ts +20 -0
- package/dist/component/search.d.ts.map +1 -0
- package/dist/component/search.js +69 -0
- package/dist/component/search.js.map +1 -0
- package/dist/package.json +3 -0
- package/dist/react/index.d.ts +2 -0
- package/dist/react/index.d.ts.map +1 -0
- package/dist/react/index.js +6 -0
- package/dist/react/index.js.map +1 -0
- package/dist/shared.d.ts +479 -0
- package/dist/shared.d.ts.map +1 -0
- package/dist/shared.js +98 -0
- package/dist/shared.js.map +1 -0
- package/package.json +97 -0
- package/src/client/_generated/_ignore.ts +1 -0
- package/src/client/defaultChunker.test.ts +243 -0
- package/src/client/defaultChunker.ts +183 -0
- package/src/client/fileUtils.ts +179 -0
- package/src/client/index.test.ts +475 -0
- package/src/client/index.ts +1125 -0
- package/src/client/setup.test.ts +28 -0
- package/src/client/types.ts +69 -0
- package/src/component/_generated/api.d.ts +439 -0
- package/src/component/_generated/api.js +23 -0
- package/src/component/_generated/dataModel.d.ts +60 -0
- package/src/component/_generated/server.d.ts +149 -0
- package/src/component/_generated/server.js +90 -0
- package/src/component/chunks.test.ts +915 -0
- package/src/component/chunks.ts +555 -0
- package/src/component/convex.config.ts +7 -0
- package/src/component/embeddings/importance.test.ts +249 -0
- package/src/component/embeddings/importance.ts +75 -0
- package/src/component/embeddings/index.test.ts +482 -0
- package/src/component/embeddings/index.ts +99 -0
- package/src/component/embeddings/tables.ts +114 -0
- package/src/component/entries.test.ts +341 -0
- package/src/component/entries.ts +546 -0
- package/src/component/filters.ts +119 -0
- package/src/component/namespaces.ts +299 -0
- package/src/component/schema.ts +106 -0
- package/src/component/search.test.ts +445 -0
- package/src/component/search.ts +97 -0
- package/src/component/setup.test.ts +5 -0
- package/src/react/index.ts +7 -0
- package/src/shared.ts +247 -0
- package/src/vitest.config.ts +7 -0
|
@@ -0,0 +1,341 @@
|
|
|
1
|
+
/// <reference types="vite/client" />
|
|
2
|
+
|
|
3
|
+
import { describe, expect, test } from "vitest";
|
|
4
|
+
import { convexTest, type TestConvex } from "convex-test";
|
|
5
|
+
import schema from "./schema.js";
|
|
6
|
+
import { api } from "./_generated/api.js";
|
|
7
|
+
import { modules } from "./setup.test.js";
|
|
8
|
+
import type { Id } from "./_generated/dataModel.js";
|
|
9
|
+
|
|
10
|
+
type ConvexTest = TestConvex<typeof schema>;
|
|
11
|
+
|
|
12
|
+
describe("entries", () => {
|
|
13
|
+
async function setupTestNamespace(t: ConvexTest, filterNames: string[] = []) {
|
|
14
|
+
const namespace = await t.mutation(api.namespaces.getOrCreate, {
|
|
15
|
+
namespace: "test-namespace",
|
|
16
|
+
status: "ready",
|
|
17
|
+
modelId: "test-model",
|
|
18
|
+
dimension: 128,
|
|
19
|
+
filterNames,
|
|
20
|
+
});
|
|
21
|
+
return namespace.namespaceId;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function testEntryArgs(namespaceId: Id<"namespaces">, key = "test-entry") {
|
|
25
|
+
return {
|
|
26
|
+
namespaceId,
|
|
27
|
+
key,
|
|
28
|
+
importance: 0.5,
|
|
29
|
+
filterValues: [],
|
|
30
|
+
contentHash: "hash123",
|
|
31
|
+
title: "Test Entry",
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
test("add creates a new entry when none exists", async () => {
|
|
36
|
+
const t = convexTest(schema, modules);
|
|
37
|
+
const namespaceId = await setupTestNamespace(t);
|
|
38
|
+
|
|
39
|
+
const entry = testEntryArgs(namespaceId);
|
|
40
|
+
|
|
41
|
+
const result = await t.mutation(api.entries.add, {
|
|
42
|
+
entry,
|
|
43
|
+
allChunks: [],
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
expect(result.created).toBe(true);
|
|
47
|
+
expect(result.status).toBe("ready");
|
|
48
|
+
expect(result.entryId).toBeDefined();
|
|
49
|
+
expect(result.replacedVersion).toBeNull();
|
|
50
|
+
|
|
51
|
+
// Verify the entry was actually created
|
|
52
|
+
const createdDoc = await t.run(async (ctx) => {
|
|
53
|
+
return ctx.db.get(result.entryId);
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
expect(createdDoc).toBeDefined();
|
|
57
|
+
expect(createdDoc!.key).toBe(entry.key);
|
|
58
|
+
expect(createdDoc!.version).toBe(0);
|
|
59
|
+
expect(createdDoc!.status.kind).toBe("ready");
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
test("add returns existing entry when adding identical content", async () => {
|
|
63
|
+
const t = convexTest(schema, modules);
|
|
64
|
+
const namespaceId = await setupTestNamespace(t);
|
|
65
|
+
|
|
66
|
+
const entry = testEntryArgs(namespaceId);
|
|
67
|
+
|
|
68
|
+
// First add
|
|
69
|
+
const firstResult = await t.mutation(api.entries.add, {
|
|
70
|
+
entry,
|
|
71
|
+
allChunks: [],
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
expect(firstResult.created).toBe(true);
|
|
75
|
+
expect(firstResult.status).toBe("ready");
|
|
76
|
+
expect(firstResult.replacedVersion).toBeNull();
|
|
77
|
+
|
|
78
|
+
// Second add with identical content
|
|
79
|
+
const secondResult = await t.mutation(api.entries.add, {
|
|
80
|
+
entry,
|
|
81
|
+
allChunks: [],
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
expect(secondResult.created).toBe(false);
|
|
85
|
+
expect(secondResult.status).toBe("ready");
|
|
86
|
+
expect(secondResult.entryId).toBe(firstResult.entryId);
|
|
87
|
+
expect(secondResult.replacedVersion).toBeNull();
|
|
88
|
+
|
|
89
|
+
// Verify no new entry was created
|
|
90
|
+
const allDocs = await t.run(async (ctx) => {
|
|
91
|
+
return ctx.db
|
|
92
|
+
.query("entries")
|
|
93
|
+
.filter((q) =>
|
|
94
|
+
q.and(
|
|
95
|
+
q.eq(q.field("namespaceId"), namespaceId),
|
|
96
|
+
q.eq(q.field("key"), entry.key)
|
|
97
|
+
)
|
|
98
|
+
)
|
|
99
|
+
.collect();
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
expect(allDocs).toHaveLength(1);
|
|
103
|
+
expect(allDocs[0]._id).toBe(firstResult.entryId);
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
test("add creates new version when content hash changes", async () => {
|
|
107
|
+
const t = convexTest(schema, modules);
|
|
108
|
+
const namespaceId = await setupTestNamespace(t);
|
|
109
|
+
|
|
110
|
+
const entry = testEntryArgs(namespaceId);
|
|
111
|
+
|
|
112
|
+
// First add
|
|
113
|
+
const firstResult = await t.mutation(api.entries.add, {
|
|
114
|
+
entry,
|
|
115
|
+
allChunks: [],
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
expect(firstResult.created).toBe(true);
|
|
119
|
+
expect(firstResult.replacedVersion).toBeNull();
|
|
120
|
+
|
|
121
|
+
// Second add with different content hash
|
|
122
|
+
const modifiedEntry = {
|
|
123
|
+
...entry,
|
|
124
|
+
contentHash: "hash456", // Different hash
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
const secondResult = await t.mutation(api.entries.add, {
|
|
128
|
+
entry: modifiedEntry,
|
|
129
|
+
allChunks: [],
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
expect(secondResult.created).toBe(true);
|
|
133
|
+
expect(secondResult.entryId).not.toBe(firstResult.entryId);
|
|
134
|
+
// When creating a entry as "ready" initially, replacedVersion is null
|
|
135
|
+
// Replacement only happens during pending -> ready transitions
|
|
136
|
+
expect(secondResult.replacedVersion).toMatchObject({
|
|
137
|
+
entryId: firstResult.entryId,
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
// Verify both entries exist with different versions
|
|
141
|
+
const allDocs = await t.run(async (ctx) => {
|
|
142
|
+
return ctx.db
|
|
143
|
+
.query("entries")
|
|
144
|
+
.filter((q) =>
|
|
145
|
+
q.and(
|
|
146
|
+
q.eq(q.field("namespaceId"), namespaceId),
|
|
147
|
+
q.eq(q.field("key"), entry.key)
|
|
148
|
+
)
|
|
149
|
+
)
|
|
150
|
+
.collect();
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
expect(allDocs).toHaveLength(2);
|
|
154
|
+
|
|
155
|
+
const versions = allDocs.map((entry) => entry.version).sort();
|
|
156
|
+
expect(versions).toEqual([0, 1]);
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
test("add creates new version when importance changes", async () => {
|
|
160
|
+
const t = convexTest(schema, modules);
|
|
161
|
+
const namespaceId = await setupTestNamespace(t);
|
|
162
|
+
|
|
163
|
+
const entry = testEntryArgs(namespaceId);
|
|
164
|
+
|
|
165
|
+
// First add
|
|
166
|
+
const firstResult = await t.mutation(api.entries.add, {
|
|
167
|
+
entry,
|
|
168
|
+
allChunks: [],
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
// Second add with different importance
|
|
172
|
+
const modifiedEntry = {
|
|
173
|
+
...entry,
|
|
174
|
+
importance: 0.8, // Changed from 0.5
|
|
175
|
+
};
|
|
176
|
+
|
|
177
|
+
const secondResult = await t.mutation(api.entries.add, {
|
|
178
|
+
entry: modifiedEntry,
|
|
179
|
+
allChunks: [],
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
expect(secondResult.created).toBe(true);
|
|
183
|
+
expect(secondResult.entryId).not.toBe(firstResult.entryId);
|
|
184
|
+
expect(secondResult.replacedVersion).toMatchObject({
|
|
185
|
+
entryId: firstResult.entryId,
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
// Verify new version was created
|
|
189
|
+
const newDoc = await t.run(async (ctx) => {
|
|
190
|
+
return ctx.db.get(secondResult.entryId);
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
expect(newDoc!.version).toBe(1);
|
|
194
|
+
expect(newDoc!.importance).toBe(0.8);
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
test("add creates new version when filter values change", async () => {
|
|
198
|
+
const t = convexTest(schema, modules);
|
|
199
|
+
const namespaceId = await setupTestNamespace(t, ["category"]); // Add filter name
|
|
200
|
+
|
|
201
|
+
const entry = testEntryArgs(namespaceId);
|
|
202
|
+
|
|
203
|
+
// First add
|
|
204
|
+
const firstResult = await t.mutation(api.entries.add, {
|
|
205
|
+
entry,
|
|
206
|
+
allChunks: [],
|
|
207
|
+
});
|
|
208
|
+
|
|
209
|
+
// Second add with different filter values
|
|
210
|
+
const modifiedEntry = {
|
|
211
|
+
...entry,
|
|
212
|
+
filterValues: [{ name: "category", value: "test" }],
|
|
213
|
+
};
|
|
214
|
+
|
|
215
|
+
const secondResult = await t.mutation(api.entries.add, {
|
|
216
|
+
entry: modifiedEntry,
|
|
217
|
+
allChunks: [],
|
|
218
|
+
});
|
|
219
|
+
|
|
220
|
+
expect(secondResult.created).toBe(true);
|
|
221
|
+
expect(secondResult.entryId).not.toBe(firstResult.entryId);
|
|
222
|
+
expect(secondResult.replacedVersion).toMatchObject({
|
|
223
|
+
entryId: firstResult.entryId,
|
|
224
|
+
});
|
|
225
|
+
|
|
226
|
+
// Verify new version was created with correct filter values
|
|
227
|
+
const newDoc = await t.run(async (ctx) => {
|
|
228
|
+
return ctx.db.get(secondResult.entryId);
|
|
229
|
+
});
|
|
230
|
+
|
|
231
|
+
expect(newDoc!.version).toBe(1);
|
|
232
|
+
expect(newDoc!.filterValues).toHaveLength(1);
|
|
233
|
+
expect(newDoc!.filterValues[0].name).toBe("category");
|
|
234
|
+
expect(newDoc!.filterValues[0].value).toBe("test");
|
|
235
|
+
});
|
|
236
|
+
|
|
237
|
+
test("add without allChunks creates pending entry", async () => {
|
|
238
|
+
const t = convexTest(schema, modules);
|
|
239
|
+
const namespaceId = await setupTestNamespace(t);
|
|
240
|
+
|
|
241
|
+
const entry = testEntryArgs(namespaceId);
|
|
242
|
+
|
|
243
|
+
const result = await t.mutation(api.entries.add, {
|
|
244
|
+
entry,
|
|
245
|
+
// No allChunks provided
|
|
246
|
+
});
|
|
247
|
+
|
|
248
|
+
expect(result.created).toBe(true);
|
|
249
|
+
expect(result.status).toBe("pending");
|
|
250
|
+
expect(result.replacedVersion).toBeNull();
|
|
251
|
+
|
|
252
|
+
// Verify the entry was created with pending status
|
|
253
|
+
const createdDoc = await t.run(async (ctx) => {
|
|
254
|
+
return ctx.db.get(result.entryId);
|
|
255
|
+
});
|
|
256
|
+
|
|
257
|
+
expect(createdDoc!.status.kind).toBe("pending");
|
|
258
|
+
});
|
|
259
|
+
|
|
260
|
+
test("multiple entries with different keys can coexist", async () => {
|
|
261
|
+
const t = convexTest(schema, modules);
|
|
262
|
+
const namespaceId = await setupTestNamespace(t);
|
|
263
|
+
|
|
264
|
+
const entry1 = testEntryArgs(namespaceId, "doc1");
|
|
265
|
+
const entry2 = testEntryArgs(namespaceId, "doc2");
|
|
266
|
+
|
|
267
|
+
const result1 = await t.mutation(api.entries.add, {
|
|
268
|
+
entry: entry1,
|
|
269
|
+
allChunks: [],
|
|
270
|
+
});
|
|
271
|
+
|
|
272
|
+
const result2 = await t.mutation(api.entries.add, {
|
|
273
|
+
entry: entry2,
|
|
274
|
+
allChunks: [],
|
|
275
|
+
});
|
|
276
|
+
|
|
277
|
+
expect(result1.created).toBe(true);
|
|
278
|
+
expect(result2.created).toBe(true);
|
|
279
|
+
expect(result1.entryId).not.toBe(result2.entryId);
|
|
280
|
+
expect(result1.replacedVersion).toBeNull();
|
|
281
|
+
expect(result2.replacedVersion).toBeNull();
|
|
282
|
+
|
|
283
|
+
// Verify both entries exist
|
|
284
|
+
const allDocs = await t.run(async (ctx) => {
|
|
285
|
+
return ctx.db
|
|
286
|
+
.query("entries")
|
|
287
|
+
.filter((q) => q.eq(q.field("namespaceId"), namespaceId))
|
|
288
|
+
.collect();
|
|
289
|
+
});
|
|
290
|
+
|
|
291
|
+
expect(allDocs).toHaveLength(2);
|
|
292
|
+
const keys = allDocs.map((entry) => entry.key).sort();
|
|
293
|
+
expect(keys).toEqual(["doc1", "doc2"]);
|
|
294
|
+
});
|
|
295
|
+
|
|
296
|
+
test("pending to ready transition populates replacedVersion", async () => {
|
|
297
|
+
const t = convexTest(schema, modules);
|
|
298
|
+
const namespaceId = await setupTestNamespace(t);
|
|
299
|
+
|
|
300
|
+
const entry = testEntryArgs(namespaceId);
|
|
301
|
+
|
|
302
|
+
// First add - create as ready
|
|
303
|
+
const firstResult = await t.mutation(api.entries.add, {
|
|
304
|
+
entry,
|
|
305
|
+
allChunks: [],
|
|
306
|
+
});
|
|
307
|
+
|
|
308
|
+
expect(firstResult.created).toBe(true);
|
|
309
|
+
expect(firstResult.status).toBe("ready");
|
|
310
|
+
expect(firstResult.replacedVersion).toBeNull();
|
|
311
|
+
|
|
312
|
+
// Second add - create as pending (no allChunks)
|
|
313
|
+
const modifiedEntry = {
|
|
314
|
+
...entry,
|
|
315
|
+
contentHash: "hash456",
|
|
316
|
+
};
|
|
317
|
+
|
|
318
|
+
const pendingResult = await t.mutation(api.entries.add, {
|
|
319
|
+
entry: modifiedEntry,
|
|
320
|
+
// No allChunks - creates pending entry
|
|
321
|
+
});
|
|
322
|
+
|
|
323
|
+
expect(pendingResult.created).toBe(true);
|
|
324
|
+
expect(pendingResult.status).toBe("pending");
|
|
325
|
+
expect(pendingResult.replacedVersion).toBeNull();
|
|
326
|
+
|
|
327
|
+
// Promote to ready - this should replace the first entry
|
|
328
|
+
const promoteResult = await t.mutation(api.entries.promoteToReady, {
|
|
329
|
+
entryId: pendingResult.entryId,
|
|
330
|
+
});
|
|
331
|
+
|
|
332
|
+
expect(promoteResult.replacedVersion).not.toBeNull();
|
|
333
|
+
expect(promoteResult.replacedVersion!.entryId).toBe(firstResult.entryId);
|
|
334
|
+
|
|
335
|
+
// Verify the first entry is now replaced
|
|
336
|
+
const firstDoc = await t.run(async (ctx) => {
|
|
337
|
+
return ctx.db.get(firstResult.entryId);
|
|
338
|
+
});
|
|
339
|
+
expect(firstDoc!.status.kind).toBe("replaced");
|
|
340
|
+
});
|
|
341
|
+
});
|