@langchain/langgraph-checkpoint 0.1.0 → 1.0.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.
- package/CHANGELOG.md +12 -0
- package/dist/_virtual/rolldown_runtime.cjs +25 -0
- package/dist/base.cjs +74 -88
- package/dist/base.cjs.map +1 -0
- package/dist/base.d.cts +95 -0
- package/dist/base.d.cts.map +1 -0
- package/dist/base.d.ts +79 -73
- package/dist/base.d.ts.map +1 -0
- package/dist/base.js +69 -80
- package/dist/base.js.map +1 -1
- package/dist/cache/base.cjs +17 -21
- package/dist/cache/base.cjs.map +1 -0
- package/dist/cache/base.d.cts +43 -0
- package/dist/cache/base.d.cts.map +1 -0
- package/dist/cache/base.d.ts +41 -36
- package/dist/cache/base.d.ts.map +1 -0
- package/dist/cache/base.js +16 -16
- package/dist/cache/base.js.map +1 -1
- package/dist/cache/index.cjs +2 -19
- package/dist/cache/index.js +4 -3
- package/dist/cache/memory.cjs +53 -58
- package/dist/cache/memory.cjs.map +1 -0
- package/dist/cache/memory.d.cts +19 -0
- package/dist/cache/memory.d.cts.map +1 -0
- package/dist/cache/memory.d.ts +18 -13
- package/dist/cache/memory.d.ts.map +1 -0
- package/dist/cache/memory.js +52 -53
- package/dist/cache/memory.js.map +1 -1
- package/dist/id.cjs +12 -16
- package/dist/id.cjs.map +1 -0
- package/dist/id.d.cts +10 -0
- package/dist/id.d.cts.map +1 -0
- package/dist/id.d.ts +10 -2
- package/dist/id.d.ts.map +1 -0
- package/dist/id.js +10 -12
- package/dist/id.js.map +1 -1
- package/dist/index.cjs +36 -27
- package/dist/index.d.cts +12 -0
- package/dist/index.d.ts +12 -8
- package/dist/index.js +12 -9
- package/dist/memory.cjs +194 -268
- package/dist/memory.cjs.map +1 -0
- package/dist/memory.d.cts +22 -0
- package/dist/memory.d.cts.map +1 -0
- package/dist/memory.d.ts +19 -12
- package/dist/memory.d.ts.map +1 -0
- package/dist/memory.js +193 -263
- package/dist/memory.js.map +1 -1
- package/dist/serde/base.d.cts +10 -0
- package/dist/serde/base.d.cts.map +1 -0
- package/dist/serde/base.d.ts +9 -3
- package/dist/serde/base.d.ts.map +1 -0
- package/dist/serde/jsonplus.cjs +92 -141
- package/dist/serde/jsonplus.cjs.map +1 -0
- package/dist/serde/jsonplus.js +90 -136
- package/dist/serde/jsonplus.js.map +1 -1
- package/dist/serde/types.cjs +15 -9
- package/dist/serde/types.cjs.map +1 -0
- package/dist/serde/types.d.cts +55 -0
- package/dist/serde/types.d.cts.map +1 -0
- package/dist/serde/types.d.ts +53 -47
- package/dist/serde/types.d.ts.map +1 -0
- package/dist/serde/types.js +9 -5
- package/dist/serde/types.js.map +1 -1
- package/dist/serde/utils/fast-safe-stringify/index.cjs +88 -208
- package/dist/serde/utils/fast-safe-stringify/index.cjs.map +1 -0
- package/dist/serde/utils/fast-safe-stringify/index.js +87 -205
- package/dist/serde/utils/fast-safe-stringify/index.js.map +1 -1
- package/dist/store/base.cjs +200 -209
- package/dist/store/base.cjs.map +1 -0
- package/dist/store/base.d.cts +392 -0
- package/dist/store/base.d.cts.map +1 -0
- package/dist/store/base.d.ts +336 -332
- package/dist/store/base.d.ts.map +1 -0
- package/dist/store/base.js +198 -204
- package/dist/store/base.js.map +1 -1
- package/dist/store/batch.cjs +116 -147
- package/dist/store/batch.cjs.map +1 -0
- package/dist/store/batch.d.cts +49 -0
- package/dist/store/batch.d.cts.map +1 -0
- package/dist/store/batch.d.ts +48 -40
- package/dist/store/batch.d.ts.map +1 -0
- package/dist/store/batch.js +116 -143
- package/dist/store/batch.js.map +1 -1
- package/dist/store/memory.cjs +262 -363
- package/dist/store/memory.cjs.map +1 -0
- package/dist/store/memory.d.cts +64 -0
- package/dist/store/memory.d.cts.map +1 -0
- package/dist/store/memory.d.ts +30 -24
- package/dist/store/memory.d.ts.map +1 -0
- package/dist/store/memory.js +261 -358
- package/dist/store/memory.js.map +1 -1
- package/dist/store/utils.cjs +141 -249
- package/dist/store/utils.cjs.map +1 -0
- package/dist/store/utils.js +140 -245
- package/dist/store/utils.js.map +1 -1
- package/dist/types.d.cts +35 -0
- package/dist/types.d.cts.map +1 -0
- package/dist/types.d.ts +29 -28
- package/dist/types.d.ts.map +1 -0
- package/package.json +16 -18
- package/dist/cache/index.d.ts +0 -2
- package/dist/cache/index.js.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/serde/base.cjs +0 -3
- package/dist/serde/base.js +0 -2
- package/dist/serde/base.js.map +0 -1
- package/dist/serde/jsonplus.d.ts +0 -7
- package/dist/serde/utils/fast-safe-stringify/index.d.ts +0 -1
- package/dist/store/index.cjs +0 -20
- package/dist/store/index.d.ts +0 -3
- package/dist/store/index.js +0 -4
- package/dist/store/index.js.map +0 -1
- package/dist/store/utils.d.ts +0 -26
- package/dist/types.cjs +0 -3
- package/dist/types.js +0 -2
- package/dist/types.js.map +0 -1
- package/index.cjs +0 -1
- package/index.d.cts +0 -1
- package/index.d.ts +0 -1
- package/index.js +0 -1
package/dist/store/memory.cjs
CHANGED
|
@@ -1,366 +1,265 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
const utils_js_1 = require("./utils.cjs");
|
|
1
|
+
const require_base = require('./base.cjs');
|
|
2
|
+
const require_utils = require('./utils.cjs');
|
|
3
|
+
|
|
4
|
+
//#region src/store/memory.ts
|
|
6
5
|
/**
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
const key = `${item.namespace.join(":")}:${item.key}`;
|
|
261
|
-
if (!seen.has(key)) {
|
|
262
|
-
seen.add(key);
|
|
263
|
-
kept.push([undefined, item]);
|
|
264
|
-
}
|
|
265
|
-
}
|
|
266
|
-
}
|
|
267
|
-
return kept.map(([score, item]) => ({
|
|
268
|
-
...item,
|
|
269
|
-
score,
|
|
270
|
-
}));
|
|
271
|
-
}
|
|
272
|
-
paginateResults(results, offset, limit) {
|
|
273
|
-
return results.slice(offset, offset + limit);
|
|
274
|
-
}
|
|
275
|
-
extractTexts(ops) {
|
|
276
|
-
if (!ops.length || !this._indexConfig) {
|
|
277
|
-
return {};
|
|
278
|
-
}
|
|
279
|
-
const toEmbed = {};
|
|
280
|
-
for (const op of ops) {
|
|
281
|
-
if (op.value !== null && op.index !== false) {
|
|
282
|
-
const paths = op.index === null || op.index === undefined
|
|
283
|
-
? this._indexConfig.__tokenizedFields ?? []
|
|
284
|
-
: op.index.map((ix) => [ix, (0, utils_js_1.tokenizePath)(ix)]);
|
|
285
|
-
for (const [path, field] of paths) {
|
|
286
|
-
const texts = (0, utils_js_1.getTextAtPath)(op.value, field);
|
|
287
|
-
if (texts.length) {
|
|
288
|
-
if (texts.length > 1) {
|
|
289
|
-
texts.forEach((text, i) => {
|
|
290
|
-
if (!toEmbed[text])
|
|
291
|
-
toEmbed[text] = [];
|
|
292
|
-
toEmbed[text].push([op.namespace, op.key, `${path}.${i}`]);
|
|
293
|
-
});
|
|
294
|
-
}
|
|
295
|
-
else {
|
|
296
|
-
if (!toEmbed[texts[0]])
|
|
297
|
-
toEmbed[texts[0]] = [];
|
|
298
|
-
toEmbed[texts[0]].push([op.namespace, op.key, path]);
|
|
299
|
-
}
|
|
300
|
-
}
|
|
301
|
-
}
|
|
302
|
-
}
|
|
303
|
-
}
|
|
304
|
-
return toEmbed;
|
|
305
|
-
}
|
|
306
|
-
insertVectors(texts, embeddings) {
|
|
307
|
-
for (const [text, metadata] of Object.entries(texts)) {
|
|
308
|
-
const embedding = embeddings.shift();
|
|
309
|
-
if (!embedding) {
|
|
310
|
-
throw new Error(`No embedding found for text: ${text}`);
|
|
311
|
-
}
|
|
312
|
-
for (const [namespace, key, field] of metadata) {
|
|
313
|
-
const namespaceKey = namespace.join(":");
|
|
314
|
-
if (!this.vectors.has(namespaceKey)) {
|
|
315
|
-
this.vectors.set(namespaceKey, new Map());
|
|
316
|
-
}
|
|
317
|
-
const namespaceMap = this.vectors.get(namespaceKey);
|
|
318
|
-
if (!namespaceMap.has(key)) {
|
|
319
|
-
namespaceMap.set(key, new Map());
|
|
320
|
-
}
|
|
321
|
-
const itemMap = namespaceMap.get(key);
|
|
322
|
-
itemMap.set(field, embedding);
|
|
323
|
-
}
|
|
324
|
-
}
|
|
325
|
-
}
|
|
326
|
-
getVectors(item) {
|
|
327
|
-
const namespaceKey = item.namespace.join(":");
|
|
328
|
-
const itemKey = item.key;
|
|
329
|
-
if (!this.vectors.has(namespaceKey)) {
|
|
330
|
-
return [];
|
|
331
|
-
}
|
|
332
|
-
const namespaceMap = this.vectors.get(namespaceKey);
|
|
333
|
-
if (!namespaceMap.has(itemKey)) {
|
|
334
|
-
return [];
|
|
335
|
-
}
|
|
336
|
-
const itemMap = namespaceMap.get(itemKey);
|
|
337
|
-
const vectors = Array.from(itemMap.values());
|
|
338
|
-
if (!vectors.length) {
|
|
339
|
-
return [];
|
|
340
|
-
}
|
|
341
|
-
return vectors;
|
|
342
|
-
}
|
|
343
|
-
cosineSimilarity(X, Y) {
|
|
344
|
-
if (!Y.length)
|
|
345
|
-
return [];
|
|
346
|
-
// Calculate dot products for all vectors at once
|
|
347
|
-
const dotProducts = Y.map((vector) => vector.reduce((acc, val, i) => acc + val * X[i], 0));
|
|
348
|
-
// Calculate magnitudes
|
|
349
|
-
const magnitude1 = Math.sqrt(X.reduce((acc, val) => acc + val * val, 0));
|
|
350
|
-
const magnitudes2 = Y.map((vector) => Math.sqrt(vector.reduce((acc, val) => acc + val * val, 0)));
|
|
351
|
-
// Calculate similarities
|
|
352
|
-
return dotProducts.map((dot, i) => {
|
|
353
|
-
const magnitude2 = magnitudes2[i];
|
|
354
|
-
return magnitude1 && magnitude2 ? dot / (magnitude1 * magnitude2) : 0;
|
|
355
|
-
});
|
|
356
|
-
}
|
|
357
|
-
get indexConfig() {
|
|
358
|
-
return this._indexConfig;
|
|
359
|
-
}
|
|
360
|
-
}
|
|
361
|
-
exports.InMemoryStore = InMemoryStore;
|
|
6
|
+
* In-memory key-value store with optional vector search.
|
|
7
|
+
*
|
|
8
|
+
* A lightweight store implementation using JavaScript Maps. Supports basic
|
|
9
|
+
* key-value operations and vector search when configured with embeddings.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```typescript
|
|
13
|
+
* // Basic key-value storage
|
|
14
|
+
* const store = new InMemoryStore();
|
|
15
|
+
* await store.put(["users", "123"], "prefs", { theme: "dark" });
|
|
16
|
+
* const item = await store.get(["users", "123"], "prefs");
|
|
17
|
+
*
|
|
18
|
+
* // Vector search with embeddings
|
|
19
|
+
* import { OpenAIEmbeddings } from "@langchain/openai";
|
|
20
|
+
* const store = new InMemoryStore({
|
|
21
|
+
* index: {
|
|
22
|
+
* dims: 1536,
|
|
23
|
+
* embeddings: new OpenAIEmbeddings({ modelName: "text-embedding-3-small" }),
|
|
24
|
+
* }
|
|
25
|
+
* });
|
|
26
|
+
*
|
|
27
|
+
* // Store documents
|
|
28
|
+
* await store.put(["docs"], "doc1", { text: "Python tutorial" });
|
|
29
|
+
* await store.put(["docs"], "doc2", { text: "TypeScript guide" });
|
|
30
|
+
*
|
|
31
|
+
* // Search by similarity
|
|
32
|
+
* const results = await store.search(["docs"], { query: "python programming" });
|
|
33
|
+
* ```
|
|
34
|
+
*
|
|
35
|
+
* **Warning**: This store keeps all data in memory. Data is lost when the process exits.
|
|
36
|
+
* For persistence, use a database-backed store.
|
|
37
|
+
*/
|
|
38
|
+
var InMemoryStore = class extends require_base.BaseStore {
|
|
39
|
+
data = /* @__PURE__ */ new Map();
|
|
40
|
+
vectors = /* @__PURE__ */ new Map();
|
|
41
|
+
_indexConfig;
|
|
42
|
+
constructor(options) {
|
|
43
|
+
super();
|
|
44
|
+
if (options?.index) this._indexConfig = {
|
|
45
|
+
...options.index,
|
|
46
|
+
__tokenizedFields: (options.index.fields ?? ["$"]).map((p) => [p, p === "$" ? [p] : require_utils.tokenizePath(p)])
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
async batch(operations) {
|
|
50
|
+
const results = [];
|
|
51
|
+
const putOps = /* @__PURE__ */ new Map();
|
|
52
|
+
const searchOps = /* @__PURE__ */ new Map();
|
|
53
|
+
for (let i = 0; i < operations.length; i += 1) {
|
|
54
|
+
const op = operations[i];
|
|
55
|
+
if ("key" in op && "namespace" in op && !("value" in op)) results.push(this.getOperation(op));
|
|
56
|
+
else if ("namespacePrefix" in op) {
|
|
57
|
+
const candidates = this.filterItems(op);
|
|
58
|
+
searchOps.set(i, [op, candidates]);
|
|
59
|
+
results.push(null);
|
|
60
|
+
} else if ("value" in op) {
|
|
61
|
+
const key = `${op.namespace.join(":")}:${op.key}`;
|
|
62
|
+
putOps.set(key, op);
|
|
63
|
+
results.push(null);
|
|
64
|
+
} else if ("matchConditions" in op) results.push(this.listNamespacesOperation(op));
|
|
65
|
+
}
|
|
66
|
+
if (searchOps.size > 0) if (this._indexConfig?.embeddings) {
|
|
67
|
+
const queries = /* @__PURE__ */ new Set();
|
|
68
|
+
for (const [op] of searchOps.values()) if (op.query) queries.add(op.query);
|
|
69
|
+
const queryEmbeddings = queries.size > 0 ? await Promise.all(Array.from(queries).map((q) => this._indexConfig.embeddings.embedQuery(q))) : [];
|
|
70
|
+
const queryVectors = Object.fromEntries(Array.from(queries).map((q, i) => [q, queryEmbeddings[i]]));
|
|
71
|
+
for (const [i, [op, candidates]] of searchOps.entries()) if (op.query && queryVectors[op.query]) {
|
|
72
|
+
const queryVector = queryVectors[op.query];
|
|
73
|
+
const scoredResults = this.scoreResults(candidates, queryVector, op.offset ?? 0, op.limit ?? 10);
|
|
74
|
+
results[i] = scoredResults;
|
|
75
|
+
} else results[i] = this.paginateResults(candidates.map((item) => ({
|
|
76
|
+
...item,
|
|
77
|
+
score: void 0
|
|
78
|
+
})), op.offset ?? 0, op.limit ?? 10);
|
|
79
|
+
} else for (const [i, [op, candidates]] of searchOps.entries()) results[i] = this.paginateResults(candidates.map((item) => ({
|
|
80
|
+
...item,
|
|
81
|
+
score: void 0
|
|
82
|
+
})), op.offset ?? 0, op.limit ?? 10);
|
|
83
|
+
if (putOps.size > 0 && this._indexConfig?.embeddings) {
|
|
84
|
+
const toEmbed = this.extractTexts(Array.from(putOps.values()));
|
|
85
|
+
if (Object.keys(toEmbed).length > 0) {
|
|
86
|
+
const embeddings = await this._indexConfig.embeddings.embedDocuments(Object.keys(toEmbed));
|
|
87
|
+
this.insertVectors(toEmbed, embeddings);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
for (const op of putOps.values()) this.putOperation(op);
|
|
91
|
+
return results;
|
|
92
|
+
}
|
|
93
|
+
getOperation(op) {
|
|
94
|
+
const namespaceKey = op.namespace.join(":");
|
|
95
|
+
const item = this.data.get(namespaceKey)?.get(op.key);
|
|
96
|
+
return item ?? null;
|
|
97
|
+
}
|
|
98
|
+
putOperation(op) {
|
|
99
|
+
const namespaceKey = op.namespace.join(":");
|
|
100
|
+
if (!this.data.has(namespaceKey)) this.data.set(namespaceKey, /* @__PURE__ */ new Map());
|
|
101
|
+
const namespaceMap = this.data.get(namespaceKey);
|
|
102
|
+
if (op.value === null) namespaceMap.delete(op.key);
|
|
103
|
+
else {
|
|
104
|
+
const now = /* @__PURE__ */ new Date();
|
|
105
|
+
if (namespaceMap.has(op.key)) {
|
|
106
|
+
const item = namespaceMap.get(op.key);
|
|
107
|
+
item.value = op.value;
|
|
108
|
+
item.updatedAt = now;
|
|
109
|
+
} else namespaceMap.set(op.key, {
|
|
110
|
+
value: op.value,
|
|
111
|
+
key: op.key,
|
|
112
|
+
namespace: op.namespace,
|
|
113
|
+
createdAt: now,
|
|
114
|
+
updatedAt: now
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
listNamespacesOperation(op) {
|
|
119
|
+
const allNamespaces = Array.from(this.data.keys()).map((ns) => ns.split(":"));
|
|
120
|
+
let namespaces = allNamespaces;
|
|
121
|
+
if (op.matchConditions && op.matchConditions.length > 0) namespaces = namespaces.filter((ns) => op.matchConditions.every((condition) => this.doesMatch(condition, ns)));
|
|
122
|
+
if (op.maxDepth !== void 0) namespaces = Array.from(new Set(namespaces.map((ns) => ns.slice(0, op.maxDepth).join(":")))).map((ns) => ns.split(":"));
|
|
123
|
+
namespaces.sort((a, b) => a.join(":").localeCompare(b.join(":")));
|
|
124
|
+
return namespaces.slice(op.offset ?? 0, (op.offset ?? 0) + (op.limit ?? namespaces.length));
|
|
125
|
+
}
|
|
126
|
+
doesMatch(matchCondition, key) {
|
|
127
|
+
const { matchType, path } = matchCondition;
|
|
128
|
+
if (matchType === "prefix") {
|
|
129
|
+
if (path.length > key.length) return false;
|
|
130
|
+
return path.every((pElem, index) => {
|
|
131
|
+
const kElem = key[index];
|
|
132
|
+
return pElem === "*" || kElem === pElem;
|
|
133
|
+
});
|
|
134
|
+
} else if (matchType === "suffix") {
|
|
135
|
+
if (path.length > key.length) return false;
|
|
136
|
+
return path.every((pElem, index) => {
|
|
137
|
+
const kElem = key[key.length - path.length + index];
|
|
138
|
+
return pElem === "*" || kElem === pElem;
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
throw new Error(`Unsupported match type: ${matchType}`);
|
|
142
|
+
}
|
|
143
|
+
filterItems(op) {
|
|
144
|
+
const candidates = [];
|
|
145
|
+
for (const [namespace, items] of this.data.entries()) if (namespace.startsWith(op.namespacePrefix.join(":"))) candidates.push(...items.values());
|
|
146
|
+
let filteredCandidates = candidates;
|
|
147
|
+
if (op.filter) filteredCandidates = candidates.filter((item) => Object.entries(op.filter).every(([key, value]) => require_utils.compareValues(item.value[key], value)));
|
|
148
|
+
return filteredCandidates;
|
|
149
|
+
}
|
|
150
|
+
scoreResults(candidates, queryVector, offset = 0, limit = 10) {
|
|
151
|
+
const flatItems = [];
|
|
152
|
+
const flatVectors = [];
|
|
153
|
+
const scoreless = [];
|
|
154
|
+
for (const item of candidates) {
|
|
155
|
+
const vectors = this.getVectors(item);
|
|
156
|
+
if (vectors.length) for (const vector of vectors) {
|
|
157
|
+
flatItems.push(item);
|
|
158
|
+
flatVectors.push(vector);
|
|
159
|
+
}
|
|
160
|
+
else scoreless.push(item);
|
|
161
|
+
}
|
|
162
|
+
const scores = this.cosineSimilarity(queryVector, flatVectors);
|
|
163
|
+
const sortedResults = scores.map((score, i) => [score, flatItems[i]]).sort((a, b) => b[0] - a[0]);
|
|
164
|
+
const seen = /* @__PURE__ */ new Set();
|
|
165
|
+
const kept = [];
|
|
166
|
+
for (const [score, item] of sortedResults) {
|
|
167
|
+
const key = `${item.namespace.join(":")}:${item.key}`;
|
|
168
|
+
if (seen.has(key)) continue;
|
|
169
|
+
const ix = seen.size;
|
|
170
|
+
if (ix >= offset + limit) break;
|
|
171
|
+
if (ix < offset) {
|
|
172
|
+
seen.add(key);
|
|
173
|
+
continue;
|
|
174
|
+
}
|
|
175
|
+
seen.add(key);
|
|
176
|
+
kept.push([score, item]);
|
|
177
|
+
}
|
|
178
|
+
if (scoreless.length && kept.length < limit) for (const item of scoreless.slice(0, limit - kept.length)) {
|
|
179
|
+
const key = `${item.namespace.join(":")}:${item.key}`;
|
|
180
|
+
if (!seen.has(key)) {
|
|
181
|
+
seen.add(key);
|
|
182
|
+
kept.push([void 0, item]);
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
return kept.map(([score, item]) => ({
|
|
186
|
+
...item,
|
|
187
|
+
score
|
|
188
|
+
}));
|
|
189
|
+
}
|
|
190
|
+
paginateResults(results, offset, limit) {
|
|
191
|
+
return results.slice(offset, offset + limit);
|
|
192
|
+
}
|
|
193
|
+
extractTexts(ops) {
|
|
194
|
+
if (!ops.length || !this._indexConfig) return {};
|
|
195
|
+
const toEmbed = {};
|
|
196
|
+
for (const op of ops) if (op.value !== null && op.index !== false) {
|
|
197
|
+
const paths = op.index === null || op.index === void 0 ? this._indexConfig.__tokenizedFields ?? [] : op.index.map((ix) => [ix, require_utils.tokenizePath(ix)]);
|
|
198
|
+
for (const [path, field] of paths) {
|
|
199
|
+
const texts = require_utils.getTextAtPath(op.value, field);
|
|
200
|
+
if (texts.length) if (texts.length > 1) texts.forEach((text, i) => {
|
|
201
|
+
if (!toEmbed[text]) toEmbed[text] = [];
|
|
202
|
+
toEmbed[text].push([
|
|
203
|
+
op.namespace,
|
|
204
|
+
op.key,
|
|
205
|
+
`${path}.${i}`
|
|
206
|
+
]);
|
|
207
|
+
});
|
|
208
|
+
else {
|
|
209
|
+
if (!toEmbed[texts[0]]) toEmbed[texts[0]] = [];
|
|
210
|
+
toEmbed[texts[0]].push([
|
|
211
|
+
op.namespace,
|
|
212
|
+
op.key,
|
|
213
|
+
path
|
|
214
|
+
]);
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
return toEmbed;
|
|
219
|
+
}
|
|
220
|
+
insertVectors(texts, embeddings) {
|
|
221
|
+
for (const [text, metadata] of Object.entries(texts)) {
|
|
222
|
+
const embedding = embeddings.shift();
|
|
223
|
+
if (!embedding) throw new Error(`No embedding found for text: ${text}`);
|
|
224
|
+
for (const [namespace, key, field] of metadata) {
|
|
225
|
+
const namespaceKey = namespace.join(":");
|
|
226
|
+
if (!this.vectors.has(namespaceKey)) this.vectors.set(namespaceKey, /* @__PURE__ */ new Map());
|
|
227
|
+
const namespaceMap = this.vectors.get(namespaceKey);
|
|
228
|
+
if (!namespaceMap.has(key)) namespaceMap.set(key, /* @__PURE__ */ new Map());
|
|
229
|
+
const itemMap = namespaceMap.get(key);
|
|
230
|
+
itemMap.set(field, embedding);
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
getVectors(item) {
|
|
235
|
+
const namespaceKey = item.namespace.join(":");
|
|
236
|
+
const itemKey = item.key;
|
|
237
|
+
if (!this.vectors.has(namespaceKey)) return [];
|
|
238
|
+
const namespaceMap = this.vectors.get(namespaceKey);
|
|
239
|
+
if (!namespaceMap.has(itemKey)) return [];
|
|
240
|
+
const itemMap = namespaceMap.get(itemKey);
|
|
241
|
+
const vectors = Array.from(itemMap.values());
|
|
242
|
+
if (!vectors.length) return [];
|
|
243
|
+
return vectors;
|
|
244
|
+
}
|
|
245
|
+
cosineSimilarity(X, Y) {
|
|
246
|
+
if (!Y.length) return [];
|
|
247
|
+
const dotProducts = Y.map((vector) => vector.reduce((acc, val, i) => acc + val * X[i], 0));
|
|
248
|
+
const magnitude1 = Math.sqrt(X.reduce((acc, val) => acc + val * val, 0));
|
|
249
|
+
const magnitudes2 = Y.map((vector) => Math.sqrt(vector.reduce((acc, val) => acc + val * val, 0)));
|
|
250
|
+
return dotProducts.map((dot, i) => {
|
|
251
|
+
const magnitude2 = magnitudes2[i];
|
|
252
|
+
return magnitude1 && magnitude2 ? dot / (magnitude1 * magnitude2) : 0;
|
|
253
|
+
});
|
|
254
|
+
}
|
|
255
|
+
get indexConfig() {
|
|
256
|
+
return this._indexConfig;
|
|
257
|
+
}
|
|
258
|
+
};
|
|
362
259
|
/** @deprecated Alias for InMemoryStore */
|
|
363
|
-
|
|
364
|
-
|
|
260
|
+
var MemoryStore = class extends InMemoryStore {};
|
|
261
|
+
|
|
262
|
+
//#endregion
|
|
263
|
+
exports.InMemoryStore = InMemoryStore;
|
|
365
264
|
exports.MemoryStore = MemoryStore;
|
|
366
|
-
//# sourceMappingURL=memory.
|
|
265
|
+
//# sourceMappingURL=memory.cjs.map
|