@mastra/pinecone 1.1.0 → 1.1.1-alpha.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/LICENSE.md +6 -4
- package/dist/docs/SKILL.md +5 -6
- package/dist/docs/assets/SOURCE_MAP.json +1 -1
- package/dist/docs/references/docs-memory-memory-processors.md +83 -10
- package/dist/docs/references/{docs-rag-retrieval.md → reference-rag-retrieval.md} +168 -31
- package/dist/docs/references/{docs-rag-vector-databases.md → reference-rag-vector-databases.md} +104 -37
- package/dist/docs/references/reference-vectors-pinecone.md +4 -0
- package/dist/index.cjs +439 -523
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +436 -519
- package/dist/index.js.map +1 -1
- package/dist/vector/filter.d.ts.map +1 -1
- package/dist/vector/index.d.ts.map +1 -1
- package/package.json +18 -18
- package/CHANGELOG.md +0 -2545
- package/dist/docs/references/docs-memory-storage.md +0 -261
package/dist/index.js
CHANGED
|
@@ -1,524 +1,441 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { createVectorErrorId } from
|
|
3
|
-
import { MastraVector } from
|
|
4
|
-
import { Pinecone } from
|
|
5
|
-
import { BaseFilterTranslator } from
|
|
6
|
-
|
|
7
|
-
// src/vector/index.ts
|
|
1
|
+
import { ErrorCategory, ErrorDomain, MastraError } from "@mastra/core/error";
|
|
2
|
+
import { createVectorErrorId } from "@mastra/core/storage";
|
|
3
|
+
import { MastraVector } from "@mastra/core/vector";
|
|
4
|
+
import { Pinecone } from "@pinecone-database/pinecone";
|
|
5
|
+
import { BaseFilterTranslator } from "@mastra/core/vector/filter";
|
|
6
|
+
//#region src/vector/filter.ts
|
|
8
7
|
var PineconeFilterTranslator = class extends BaseFilterTranslator {
|
|
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
|
-
return result;
|
|
70
|
-
}
|
|
71
|
-
translateOperator(operator, value, currentPath = "") {
|
|
72
|
-
if (operator === "$all") {
|
|
73
|
-
if (!Array.isArray(value) || value.length === 0) {
|
|
74
|
-
throw new Error("A non-empty array is required for the $all operator");
|
|
75
|
-
}
|
|
76
|
-
return this.simulateAllOperator(currentPath, value);
|
|
77
|
-
}
|
|
78
|
-
if (this.isLogicalOperator(operator)) {
|
|
79
|
-
return Array.isArray(value) ? value.map((item) => this.translateNode(item)) : this.translateNode(value);
|
|
80
|
-
}
|
|
81
|
-
return this.normalizeComparisonValue(value);
|
|
82
|
-
}
|
|
8
|
+
getSupportedOperators() {
|
|
9
|
+
return {
|
|
10
|
+
...BaseFilterTranslator.DEFAULT_OPERATORS,
|
|
11
|
+
logical: ["$and", "$or"],
|
|
12
|
+
array: [
|
|
13
|
+
"$in",
|
|
14
|
+
"$all",
|
|
15
|
+
"$nin"
|
|
16
|
+
],
|
|
17
|
+
element: ["$exists"],
|
|
18
|
+
regex: [],
|
|
19
|
+
custom: []
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
translate(filter) {
|
|
23
|
+
if (this.isEmpty(filter)) return filter;
|
|
24
|
+
this.validateFilter(filter);
|
|
25
|
+
return this.translateNode(filter);
|
|
26
|
+
}
|
|
27
|
+
translateNode(node, currentPath = "") {
|
|
28
|
+
if (this.isRegex(node)) throw new Error("Regex is not supported in Pinecone");
|
|
29
|
+
if (this.isPrimitive(node)) return this.normalizeComparisonValue(node);
|
|
30
|
+
if (Array.isArray(node)) return { $in: this.normalizeArrayValues(node) };
|
|
31
|
+
const entries = Object.entries(node);
|
|
32
|
+
const firstEntry = entries[0];
|
|
33
|
+
if (entries.length === 1 && firstEntry && this.isOperator(firstEntry[0])) {
|
|
34
|
+
const [operator, value] = firstEntry;
|
|
35
|
+
const translated = this.translateOperator(operator, value, currentPath);
|
|
36
|
+
return this.isLogicalOperator(operator) ? { [operator]: translated } : translated;
|
|
37
|
+
}
|
|
38
|
+
const result = {};
|
|
39
|
+
for (const [key, value] of entries) {
|
|
40
|
+
const newPath = currentPath ? `${currentPath}.${key}` : key;
|
|
41
|
+
if (this.isOperator(key)) {
|
|
42
|
+
result[key] = this.translateOperator(key, value, currentPath);
|
|
43
|
+
continue;
|
|
44
|
+
}
|
|
45
|
+
if (typeof value === "object" && value !== null && !Array.isArray(value)) {
|
|
46
|
+
if (Object.keys(value).length === 1 && "$all" in value) {
|
|
47
|
+
const translated = this.translateNode(value, key);
|
|
48
|
+
if (translated.$and) return translated;
|
|
49
|
+
}
|
|
50
|
+
if (Object.keys(value).length === 0) result[newPath] = this.translateNode(value);
|
|
51
|
+
else if (Object.keys(value).some((k) => this.isOperator(k))) {
|
|
52
|
+
const normalizedValue = {};
|
|
53
|
+
for (const [op, opValue] of Object.entries(value)) normalizedValue[op] = this.isOperator(op) ? this.translateOperator(op, opValue) : opValue;
|
|
54
|
+
result[newPath] = normalizedValue;
|
|
55
|
+
} else Object.assign(result, this.translateNode(value, newPath));
|
|
56
|
+
} else result[newPath] = this.translateNode(value);
|
|
57
|
+
}
|
|
58
|
+
return result;
|
|
59
|
+
}
|
|
60
|
+
translateOperator(operator, value, currentPath = "") {
|
|
61
|
+
if (operator === "$all") {
|
|
62
|
+
if (!Array.isArray(value) || value.length === 0) throw new Error("A non-empty array is required for the $all operator");
|
|
63
|
+
return this.simulateAllOperator(currentPath, value);
|
|
64
|
+
}
|
|
65
|
+
if (this.isLogicalOperator(operator)) return Array.isArray(value) ? value.map((item) => this.translateNode(item)) : this.translateNode(value);
|
|
66
|
+
return this.normalizeComparisonValue(value);
|
|
67
|
+
}
|
|
83
68
|
};
|
|
84
|
-
|
|
85
|
-
|
|
69
|
+
//#endregion
|
|
70
|
+
//#region src/vector/index.ts
|
|
86
71
|
var PineconeVector = class extends MastraVector {
|
|
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
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
text: "Cannot specify both ids and filter - they are mutually exclusive",
|
|
447
|
-
domain: ErrorDomain.STORAGE,
|
|
448
|
-
category: ErrorCategory.USER,
|
|
449
|
-
details: { indexName }
|
|
450
|
-
});
|
|
451
|
-
}
|
|
452
|
-
if (!ids && !filter) {
|
|
453
|
-
throw new MastraError({
|
|
454
|
-
id: createVectorErrorId("PINECONE", "DELETE_VECTORS", "NO_TARGET"),
|
|
455
|
-
text: "Either filter or ids must be provided",
|
|
456
|
-
domain: ErrorDomain.STORAGE,
|
|
457
|
-
category: ErrorCategory.USER,
|
|
458
|
-
details: { indexName }
|
|
459
|
-
});
|
|
460
|
-
}
|
|
461
|
-
if (ids && ids.length === 0) {
|
|
462
|
-
throw new MastraError({
|
|
463
|
-
id: createVectorErrorId("PINECONE", "DELETE_VECTORS", "EMPTY_IDS"),
|
|
464
|
-
text: "Cannot delete with empty ids array",
|
|
465
|
-
domain: ErrorDomain.STORAGE,
|
|
466
|
-
category: ErrorCategory.USER,
|
|
467
|
-
details: { indexName }
|
|
468
|
-
});
|
|
469
|
-
}
|
|
470
|
-
if (filter && Object.keys(filter).length === 0) {
|
|
471
|
-
throw new MastraError({
|
|
472
|
-
id: createVectorErrorId("PINECONE", "DELETE_VECTORS", "EMPTY_FILTER"),
|
|
473
|
-
text: "Cannot delete with empty filter object",
|
|
474
|
-
domain: ErrorDomain.STORAGE,
|
|
475
|
-
category: ErrorCategory.USER,
|
|
476
|
-
details: { indexName }
|
|
477
|
-
});
|
|
478
|
-
}
|
|
479
|
-
try {
|
|
480
|
-
const index = this.client.Index(indexName).namespace(namespace || "");
|
|
481
|
-
if (ids) {
|
|
482
|
-
await index.deleteMany(ids);
|
|
483
|
-
} else if (filter) {
|
|
484
|
-
const translatedFilter = this.transformFilter(filter);
|
|
485
|
-
if (translatedFilter) {
|
|
486
|
-
const stats = await this.describeIndex({ indexName });
|
|
487
|
-
const dummyVector = new Array(stats.dimension).fill(1 / Math.sqrt(stats.dimension));
|
|
488
|
-
const results = await index.query({
|
|
489
|
-
vector: dummyVector,
|
|
490
|
-
topK: 1e4,
|
|
491
|
-
filter: translatedFilter,
|
|
492
|
-
includeMetadata: false,
|
|
493
|
-
includeValues: false
|
|
494
|
-
});
|
|
495
|
-
const idsToDelete = results.matches.map((m) => m.id);
|
|
496
|
-
if (idsToDelete.length > 0) {
|
|
497
|
-
await index.deleteMany(idsToDelete);
|
|
498
|
-
}
|
|
499
|
-
}
|
|
500
|
-
}
|
|
501
|
-
} catch (error) {
|
|
502
|
-
if (error instanceof MastraError) throw error;
|
|
503
|
-
throw new MastraError(
|
|
504
|
-
{
|
|
505
|
-
id: createVectorErrorId("PINECONE", "DELETE_VECTORS", "FAILED"),
|
|
506
|
-
domain: ErrorDomain.STORAGE,
|
|
507
|
-
category: ErrorCategory.THIRD_PARTY,
|
|
508
|
-
details: {
|
|
509
|
-
indexName,
|
|
510
|
-
...filter && { filter: JSON.stringify(filter) },
|
|
511
|
-
...ids && { idsCount: ids.length }
|
|
512
|
-
}
|
|
513
|
-
},
|
|
514
|
-
error
|
|
515
|
-
);
|
|
516
|
-
}
|
|
517
|
-
}
|
|
72
|
+
client;
|
|
73
|
+
cloud;
|
|
74
|
+
region;
|
|
75
|
+
/**
|
|
76
|
+
* Creates a new PineconeVector client.
|
|
77
|
+
*
|
|
78
|
+
* @param config - Configuration options for the Pinecone client.
|
|
79
|
+
* @see {@link PineconeVectorConfig} for all available options.
|
|
80
|
+
*/
|
|
81
|
+
constructor({ id, cloud, region, ...pineconeConfig }) {
|
|
82
|
+
super({ id });
|
|
83
|
+
this.client = new Pinecone(pineconeConfig);
|
|
84
|
+
this.cloud = cloud || "aws";
|
|
85
|
+
this.region = region || "us-east-1";
|
|
86
|
+
}
|
|
87
|
+
get indexSeparator() {
|
|
88
|
+
return "-";
|
|
89
|
+
}
|
|
90
|
+
async createIndex({ indexName, dimension, metric = "cosine" }) {
|
|
91
|
+
try {
|
|
92
|
+
if (!Number.isInteger(dimension) || dimension <= 0) throw new Error("Dimension must be a positive integer");
|
|
93
|
+
if (metric && ![
|
|
94
|
+
"cosine",
|
|
95
|
+
"euclidean",
|
|
96
|
+
"dotproduct"
|
|
97
|
+
].includes(metric)) throw new Error("Metric must be one of: cosine, euclidean, dotproduct");
|
|
98
|
+
} catch (validationError) {
|
|
99
|
+
throw new MastraError({
|
|
100
|
+
id: createVectorErrorId("PINECONE", "CREATE_INDEX", "INVALID_ARGS"),
|
|
101
|
+
domain: ErrorDomain.STORAGE,
|
|
102
|
+
category: ErrorCategory.USER,
|
|
103
|
+
details: {
|
|
104
|
+
indexName,
|
|
105
|
+
dimension,
|
|
106
|
+
metric
|
|
107
|
+
}
|
|
108
|
+
}, validationError);
|
|
109
|
+
}
|
|
110
|
+
try {
|
|
111
|
+
await this.client.createIndex({
|
|
112
|
+
name: indexName,
|
|
113
|
+
dimension,
|
|
114
|
+
metric,
|
|
115
|
+
spec: { serverless: {
|
|
116
|
+
cloud: this.cloud,
|
|
117
|
+
region: this.region
|
|
118
|
+
} }
|
|
119
|
+
});
|
|
120
|
+
} catch (error) {
|
|
121
|
+
const message = error?.errors?.[0]?.message || error?.message;
|
|
122
|
+
if (error.status === 409 || typeof message === "string" && (message.toLowerCase().includes("already exists") || message.toLowerCase().includes("duplicate"))) {
|
|
123
|
+
await this.validateExistingIndex(indexName, dimension, metric);
|
|
124
|
+
return;
|
|
125
|
+
}
|
|
126
|
+
throw new MastraError({
|
|
127
|
+
id: createVectorErrorId("PINECONE", "CREATE_INDEX", "FAILED"),
|
|
128
|
+
domain: ErrorDomain.STORAGE,
|
|
129
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
130
|
+
details: {
|
|
131
|
+
indexName,
|
|
132
|
+
dimension,
|
|
133
|
+
metric
|
|
134
|
+
}
|
|
135
|
+
}, error);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
async upsert({ indexName, vectors, metadata, ids, namespace, sparseVectors }) {
|
|
139
|
+
const index = this.client.Index(indexName).namespace(namespace || "");
|
|
140
|
+
const vectorIds = ids || vectors.map(() => crypto.randomUUID());
|
|
141
|
+
const records = vectors.map((vector, i) => ({
|
|
142
|
+
id: vectorIds[i],
|
|
143
|
+
values: vector,
|
|
144
|
+
...sparseVectors?.[i] && { sparseValues: sparseVectors?.[i] },
|
|
145
|
+
metadata: metadata?.[i] || {}
|
|
146
|
+
}));
|
|
147
|
+
const batchSize = 100;
|
|
148
|
+
try {
|
|
149
|
+
for (let i = 0; i < records.length; i += batchSize) {
|
|
150
|
+
const batch = records.slice(i, i + batchSize);
|
|
151
|
+
await index.upsert(batch);
|
|
152
|
+
}
|
|
153
|
+
return vectorIds;
|
|
154
|
+
} catch (error) {
|
|
155
|
+
throw new MastraError({
|
|
156
|
+
id: createVectorErrorId("PINECONE", "UPSERT", "FAILED"),
|
|
157
|
+
domain: ErrorDomain.STORAGE,
|
|
158
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
159
|
+
details: {
|
|
160
|
+
indexName,
|
|
161
|
+
vectorCount: vectors.length
|
|
162
|
+
}
|
|
163
|
+
}, error);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
transformFilter(filter) {
|
|
167
|
+
return new PineconeFilterTranslator().translate(filter);
|
|
168
|
+
}
|
|
169
|
+
async query({ indexName, queryVector, topK = 10, filter, includeVector = false, namespace, sparseVector }) {
|
|
170
|
+
if (!queryVector) throw new MastraError({
|
|
171
|
+
id: createVectorErrorId("PINECONE", "QUERY", "MISSING_VECTOR"),
|
|
172
|
+
text: "queryVector is required for Pinecone queries. Metadata-only queries are not supported by this vector store.",
|
|
173
|
+
domain: ErrorDomain.STORAGE,
|
|
174
|
+
category: ErrorCategory.USER,
|
|
175
|
+
details: { indexName }
|
|
176
|
+
});
|
|
177
|
+
const index = this.client.Index(indexName).namespace(namespace || "");
|
|
178
|
+
const queryParams = {
|
|
179
|
+
vector: queryVector,
|
|
180
|
+
topK,
|
|
181
|
+
includeMetadata: true,
|
|
182
|
+
includeValues: includeVector,
|
|
183
|
+
filter: this.transformFilter(filter) ?? void 0
|
|
184
|
+
};
|
|
185
|
+
if (sparseVector) queryParams.sparseVector = sparseVector;
|
|
186
|
+
try {
|
|
187
|
+
return (await index.query(queryParams)).matches.map((match) => ({
|
|
188
|
+
id: match.id,
|
|
189
|
+
score: match.score || 0,
|
|
190
|
+
metadata: match.metadata,
|
|
191
|
+
...includeVector && { vector: match.values || [] }
|
|
192
|
+
}));
|
|
193
|
+
} catch (error) {
|
|
194
|
+
throw new MastraError({
|
|
195
|
+
id: createVectorErrorId("PINECONE", "QUERY", "FAILED"),
|
|
196
|
+
domain: ErrorDomain.STORAGE,
|
|
197
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
198
|
+
details: {
|
|
199
|
+
indexName,
|
|
200
|
+
topK
|
|
201
|
+
}
|
|
202
|
+
}, error);
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
async listIndexes() {
|
|
206
|
+
try {
|
|
207
|
+
return (await this.client.listIndexes())?.indexes?.map((index) => index.name) || [];
|
|
208
|
+
} catch (error) {
|
|
209
|
+
throw new MastraError({
|
|
210
|
+
id: createVectorErrorId("PINECONE", "LIST_INDEXES", "FAILED"),
|
|
211
|
+
domain: ErrorDomain.STORAGE,
|
|
212
|
+
category: ErrorCategory.THIRD_PARTY
|
|
213
|
+
}, error);
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
/**
|
|
217
|
+
* Retrieves statistics about a vector index.
|
|
218
|
+
*
|
|
219
|
+
* @param {string} indexName - The name of the index to describe
|
|
220
|
+
* @returns A promise that resolves to the index statistics including dimension, count and metric
|
|
221
|
+
*/
|
|
222
|
+
async describeIndex({ indexName }) {
|
|
223
|
+
try {
|
|
224
|
+
const stats = await this.client.Index(indexName).describeIndexStats();
|
|
225
|
+
const description = await this.client.describeIndex(indexName);
|
|
226
|
+
return {
|
|
227
|
+
dimension: description.dimension,
|
|
228
|
+
count: stats.totalRecordCount || 0,
|
|
229
|
+
metric: description.metric,
|
|
230
|
+
namespaces: stats.namespaces
|
|
231
|
+
};
|
|
232
|
+
} catch (error) {
|
|
233
|
+
throw new MastraError({
|
|
234
|
+
id: createVectorErrorId("PINECONE", "DESCRIBE_INDEX", "FAILED"),
|
|
235
|
+
domain: ErrorDomain.STORAGE,
|
|
236
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
237
|
+
details: { indexName }
|
|
238
|
+
}, error);
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
async deleteIndex({ indexName }) {
|
|
242
|
+
try {
|
|
243
|
+
await this.client.deleteIndex(indexName);
|
|
244
|
+
} catch (error) {
|
|
245
|
+
throw new MastraError({
|
|
246
|
+
id: createVectorErrorId("PINECONE", "DELETE_INDEX", "FAILED"),
|
|
247
|
+
domain: ErrorDomain.STORAGE,
|
|
248
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
249
|
+
details: { indexName }
|
|
250
|
+
}, error);
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
/**
|
|
254
|
+
* Updates a vector by its ID with the provided vector and/or metadata.
|
|
255
|
+
* Note: Pinecone only supports update by ID, not by filter.
|
|
256
|
+
* @param params - Parameters containing the id for targeting the vector to update
|
|
257
|
+
* @param params.indexName - The name of the index containing the vector.
|
|
258
|
+
* @param params.id - The ID of the vector to update.
|
|
259
|
+
* @param params.update - An object containing the vector and/or metadata to update.
|
|
260
|
+
* @param namespace - The namespace of the index (optional, Pinecone-specific).
|
|
261
|
+
* @returns A promise that resolves when the update is complete.
|
|
262
|
+
* @throws Will throw an error if no updates are provided or if the update operation fails.
|
|
263
|
+
*/
|
|
264
|
+
async updateVector(params) {
|
|
265
|
+
const { indexName, update } = params;
|
|
266
|
+
if ("id" in params && params.id && "filter" in params && params.filter) throw new MastraError({
|
|
267
|
+
id: createVectorErrorId("PINECONE", "UPDATE_VECTOR", "MUTUALLY_EXCLUSIVE"),
|
|
268
|
+
text: "Cannot specify both id and filter - they are mutually exclusive",
|
|
269
|
+
domain: ErrorDomain.STORAGE,
|
|
270
|
+
category: ErrorCategory.USER,
|
|
271
|
+
details: { indexName }
|
|
272
|
+
});
|
|
273
|
+
if (!("id" in params && params.id) && !("filter" in params && params.filter)) throw new MastraError({
|
|
274
|
+
id: createVectorErrorId("PINECONE", "UPDATE_VECTOR", "NO_TARGET"),
|
|
275
|
+
text: "Either id or filter must be provided",
|
|
276
|
+
domain: ErrorDomain.STORAGE,
|
|
277
|
+
category: ErrorCategory.USER,
|
|
278
|
+
details: { indexName }
|
|
279
|
+
});
|
|
280
|
+
if (!update.vector && !update.metadata) throw new MastraError({
|
|
281
|
+
id: createVectorErrorId("PINECONE", "UPDATE_VECTOR", "NO_PAYLOAD"),
|
|
282
|
+
domain: ErrorDomain.STORAGE,
|
|
283
|
+
category: ErrorCategory.USER,
|
|
284
|
+
text: "No updates provided",
|
|
285
|
+
details: { indexName }
|
|
286
|
+
});
|
|
287
|
+
const namespace = params.namespace;
|
|
288
|
+
try {
|
|
289
|
+
const index = this.client.Index(indexName).namespace(namespace || "");
|
|
290
|
+
if ("id" in params && params.id) {
|
|
291
|
+
const updateObj = { id: params.id };
|
|
292
|
+
if (update.vector) updateObj.values = update.vector;
|
|
293
|
+
if (update.metadata) updateObj.metadata = update.metadata;
|
|
294
|
+
await index.update(updateObj);
|
|
295
|
+
} else if ("filter" in params && params.filter) {
|
|
296
|
+
if (Object.keys(params.filter).length === 0) throw new MastraError({
|
|
297
|
+
id: createVectorErrorId("PINECONE", "UPDATE_VECTOR", "EMPTY_FILTER"),
|
|
298
|
+
text: "Filter cannot be an empty filter object",
|
|
299
|
+
domain: ErrorDomain.STORAGE,
|
|
300
|
+
category: ErrorCategory.USER,
|
|
301
|
+
details: { indexName }
|
|
302
|
+
});
|
|
303
|
+
const translatedFilter = this.transformFilter(params.filter);
|
|
304
|
+
if (translatedFilter) {
|
|
305
|
+
const stats = await this.describeIndex({ indexName });
|
|
306
|
+
const dummyVector = new Array(stats.dimension).fill(1 / Math.sqrt(stats.dimension));
|
|
307
|
+
const idsToUpdate = (await index.query({
|
|
308
|
+
vector: dummyVector,
|
|
309
|
+
topK: 1e4,
|
|
310
|
+
filter: translatedFilter,
|
|
311
|
+
includeMetadata: false,
|
|
312
|
+
includeValues: false
|
|
313
|
+
})).matches.map((m) => m.id);
|
|
314
|
+
for (const id of idsToUpdate) {
|
|
315
|
+
const updateObj = { id };
|
|
316
|
+
if (update.vector) updateObj.values = update.vector;
|
|
317
|
+
if (update.metadata) updateObj.metadata = update.metadata;
|
|
318
|
+
await index.update(updateObj);
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
} catch (error) {
|
|
323
|
+
if (error instanceof MastraError) throw error;
|
|
324
|
+
throw new MastraError({
|
|
325
|
+
id: createVectorErrorId("PINECONE", "UPDATE_VECTOR", "FAILED"),
|
|
326
|
+
domain: ErrorDomain.STORAGE,
|
|
327
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
328
|
+
details: {
|
|
329
|
+
indexName,
|
|
330
|
+
..."id" in params && params.id && { id: params.id },
|
|
331
|
+
..."filter" in params && params.filter && { filter: JSON.stringify(params.filter) }
|
|
332
|
+
}
|
|
333
|
+
}, error);
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
/**
|
|
337
|
+
* Deletes a vector by its ID.
|
|
338
|
+
* @param indexName - The name of the index containing the vector.
|
|
339
|
+
* @param id - The ID of the vector to delete.
|
|
340
|
+
* @param namespace - The namespace of the index (optional).
|
|
341
|
+
* @returns A promise that resolves when the deletion is complete.
|
|
342
|
+
* @throws Will throw an error if the deletion operation fails.
|
|
343
|
+
*/
|
|
344
|
+
async deleteVector({ indexName, id, namespace }) {
|
|
345
|
+
try {
|
|
346
|
+
await this.client.Index(indexName).namespace(namespace || "").deleteOne(id);
|
|
347
|
+
} catch (error) {
|
|
348
|
+
throw new MastraError({
|
|
349
|
+
id: createVectorErrorId("PINECONE", "DELETE_VECTOR", "FAILED"),
|
|
350
|
+
domain: ErrorDomain.STORAGE,
|
|
351
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
352
|
+
details: {
|
|
353
|
+
indexName,
|
|
354
|
+
...id && { id }
|
|
355
|
+
}
|
|
356
|
+
}, error);
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
/**
|
|
360
|
+
* Deletes multiple vectors by IDs or filter.
|
|
361
|
+
* @param indexName - The name of the index containing the vectors.
|
|
362
|
+
* @param ids - Array of vector IDs to delete (mutually exclusive with filter).
|
|
363
|
+
* @param filter - Filter to match vectors to delete (mutually exclusive with ids).
|
|
364
|
+
* @param namespace - The namespace of the index (optional, Pinecone-specific).
|
|
365
|
+
* @returns A promise that resolves when the deletion is complete.
|
|
366
|
+
* @throws Will throw an error if both ids and filter are provided, or if neither is provided.
|
|
367
|
+
*/
|
|
368
|
+
async deleteVectors(params) {
|
|
369
|
+
const { indexName, filter, ids } = params;
|
|
370
|
+
const namespace = params.namespace;
|
|
371
|
+
if (ids && filter) throw new MastraError({
|
|
372
|
+
id: createVectorErrorId("PINECONE", "DELETE_VECTORS", "MUTUALLY_EXCLUSIVE"),
|
|
373
|
+
text: "Cannot specify both ids and filter - they are mutually exclusive",
|
|
374
|
+
domain: ErrorDomain.STORAGE,
|
|
375
|
+
category: ErrorCategory.USER,
|
|
376
|
+
details: { indexName }
|
|
377
|
+
});
|
|
378
|
+
if (!ids && !filter) throw new MastraError({
|
|
379
|
+
id: createVectorErrorId("PINECONE", "DELETE_VECTORS", "NO_TARGET"),
|
|
380
|
+
text: "Either filter or ids must be provided",
|
|
381
|
+
domain: ErrorDomain.STORAGE,
|
|
382
|
+
category: ErrorCategory.USER,
|
|
383
|
+
details: { indexName }
|
|
384
|
+
});
|
|
385
|
+
if (ids && ids.length === 0) throw new MastraError({
|
|
386
|
+
id: createVectorErrorId("PINECONE", "DELETE_VECTORS", "EMPTY_IDS"),
|
|
387
|
+
text: "Cannot delete with empty ids array",
|
|
388
|
+
domain: ErrorDomain.STORAGE,
|
|
389
|
+
category: ErrorCategory.USER,
|
|
390
|
+
details: { indexName }
|
|
391
|
+
});
|
|
392
|
+
if (filter && Object.keys(filter).length === 0) throw new MastraError({
|
|
393
|
+
id: createVectorErrorId("PINECONE", "DELETE_VECTORS", "EMPTY_FILTER"),
|
|
394
|
+
text: "Cannot delete with empty filter object",
|
|
395
|
+
domain: ErrorDomain.STORAGE,
|
|
396
|
+
category: ErrorCategory.USER,
|
|
397
|
+
details: { indexName }
|
|
398
|
+
});
|
|
399
|
+
try {
|
|
400
|
+
const index = this.client.Index(indexName).namespace(namespace || "");
|
|
401
|
+
if (ids) await index.deleteMany(ids);
|
|
402
|
+
else if (filter) {
|
|
403
|
+
const translatedFilter = this.transformFilter(filter);
|
|
404
|
+
if (translatedFilter) {
|
|
405
|
+
const stats = await this.describeIndex({ indexName });
|
|
406
|
+
const dummyVector = new Array(stats.dimension).fill(1 / Math.sqrt(stats.dimension));
|
|
407
|
+
const idsToDelete = (await index.query({
|
|
408
|
+
vector: dummyVector,
|
|
409
|
+
topK: 1e4,
|
|
410
|
+
filter: translatedFilter,
|
|
411
|
+
includeMetadata: false,
|
|
412
|
+
includeValues: false
|
|
413
|
+
})).matches.map((m) => m.id);
|
|
414
|
+
if (idsToDelete.length > 0) await index.deleteMany(idsToDelete);
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
} catch (error) {
|
|
418
|
+
if (error instanceof MastraError) throw error;
|
|
419
|
+
throw new MastraError({
|
|
420
|
+
id: createVectorErrorId("PINECONE", "DELETE_VECTORS", "FAILED"),
|
|
421
|
+
domain: ErrorDomain.STORAGE,
|
|
422
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
423
|
+
details: {
|
|
424
|
+
indexName,
|
|
425
|
+
...filter && { filter: JSON.stringify(filter) },
|
|
426
|
+
...ids && { idsCount: ids.length }
|
|
427
|
+
}
|
|
428
|
+
}, error);
|
|
429
|
+
}
|
|
430
|
+
}
|
|
518
431
|
};
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
432
|
+
//#endregion
|
|
433
|
+
//#region src/vector/prompt.ts
|
|
434
|
+
/**
|
|
435
|
+
* Vector store specific prompt that details supported operators and examples.
|
|
436
|
+
* This prompt helps users construct valid filters for Pinecone Vector.
|
|
437
|
+
*/
|
|
438
|
+
const PINECONE_PROMPT = `When querying Pinecone, you can ONLY use the operators listed below. Any other operators will be rejected.
|
|
522
439
|
Important: Don't explain how to construct the filter - use the specified operators and fields to search the content and return relevant results.
|
|
523
440
|
If a user tries to give an explicit operator that is not supported, reject the filter entirely and let them know that the operator is not supported.
|
|
524
441
|
|
|
@@ -595,7 +512,7 @@ Example Complex Query:
|
|
|
595
512
|
]}
|
|
596
513
|
]
|
|
597
514
|
}`;
|
|
598
|
-
|
|
515
|
+
//#endregion
|
|
599
516
|
export { PINECONE_PROMPT, PineconeVector };
|
|
600
|
-
|
|
517
|
+
|
|
601
518
|
//# sourceMappingURL=index.js.map
|