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