@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/dist/index.cjs CHANGED
@@ -1,526 +1,442 @@
1
- 'use strict';
2
-
3
- var error = require('@mastra/core/error');
4
- var storage = require('@mastra/core/storage');
5
- var vector = require('@mastra/core/vector');
6
- var pinecone = require('@pinecone-database/pinecone');
7
- var filter = require('@mastra/core/vector/filter');
8
-
9
- // src/vector/index.ts
10
- var PineconeFilterTranslator = class extends filter.BaseFilterTranslator {
11
- getSupportedOperators() {
12
- return {
13
- ...filter.BaseFilterTranslator.DEFAULT_OPERATORS,
14
- logical: ["$and", "$or"],
15
- array: ["$in", "$all", "$nin"],
16
- element: ["$exists"],
17
- regex: [],
18
- custom: []
19
- };
20
- }
21
- translate(filter) {
22
- if (this.isEmpty(filter)) return filter;
23
- this.validateFilter(filter);
24
- return this.translateNode(filter);
25
- }
26
- translateNode(node, currentPath = "") {
27
- if (this.isRegex(node)) {
28
- throw new Error("Regex is not supported in Pinecone");
29
- }
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) {
50
- return translated;
51
- }
52
- }
53
- if (Object.keys(value).length === 0) {
54
- result[newPath] = this.translateNode(value);
55
- } else {
56
- const hasOperators = Object.keys(value).some((k) => this.isOperator(k));
57
- if (hasOperators) {
58
- const normalizedValue = {};
59
- for (const [op, opValue] of Object.entries(value)) {
60
- normalizedValue[op] = this.isOperator(op) ? this.translateOperator(op, opValue) : opValue;
61
- }
62
- result[newPath] = normalizedValue;
63
- } else {
64
- Object.assign(result, this.translateNode(value, newPath));
65
- }
66
- }
67
- } else {
68
- result[newPath] = this.translateNode(value);
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
- // src/vector/index.ts
88
- var PineconeVector = class extends vector.MastraVector {
89
- client;
90
- cloud;
91
- region;
92
- /**
93
- * Creates a new PineconeVector client.
94
- *
95
- * @param config - Configuration options for the Pinecone client.
96
- * @see {@link PineconeVectorConfig} for all available options.
97
- */
98
- constructor({ id, cloud, region, ...pineconeConfig }) {
99
- super({ id });
100
- this.client = new pinecone.Pinecone(pineconeConfig);
101
- this.cloud = cloud || "aws";
102
- this.region = region || "us-east-1";
103
- }
104
- get indexSeparator() {
105
- return "-";
106
- }
107
- async createIndex({ indexName, dimension, metric = "cosine" }) {
108
- try {
109
- if (!Number.isInteger(dimension) || dimension <= 0) {
110
- throw new Error("Dimension must be a positive integer");
111
- }
112
- if (metric && !["cosine", "euclidean", "dotproduct"].includes(metric)) {
113
- throw new Error("Metric must be one of: cosine, euclidean, dotproduct");
114
- }
115
- } catch (validationError) {
116
- throw new error.MastraError(
117
- {
118
- id: storage.createVectorErrorId("PINECONE", "CREATE_INDEX", "INVALID_ARGS"),
119
- domain: error.ErrorDomain.STORAGE,
120
- category: error.ErrorCategory.USER,
121
- details: { indexName, dimension, metric }
122
- },
123
- validationError
124
- );
125
- }
126
- try {
127
- await this.client.createIndex({
128
- name: indexName,
129
- dimension,
130
- metric,
131
- spec: {
132
- serverless: {
133
- cloud: this.cloud,
134
- region: this.region
135
- }
136
- }
137
- });
138
- } catch (error$1) {
139
- const message = error$1?.errors?.[0]?.message || error$1?.message;
140
- if (error$1.status === 409 || typeof message === "string" && (message.toLowerCase().includes("already exists") || message.toLowerCase().includes("duplicate"))) {
141
- await this.validateExistingIndex(indexName, dimension, metric);
142
- return;
143
- }
144
- throw new error.MastraError(
145
- {
146
- id: storage.createVectorErrorId("PINECONE", "CREATE_INDEX", "FAILED"),
147
- domain: error.ErrorDomain.STORAGE,
148
- category: error.ErrorCategory.THIRD_PARTY,
149
- details: { indexName, dimension, metric }
150
- },
151
- error$1
152
- );
153
- }
154
- }
155
- async upsert({
156
- indexName,
157
- vectors,
158
- metadata,
159
- ids,
160
- namespace,
161
- sparseVectors
162
- }) {
163
- const index = this.client.Index(indexName).namespace(namespace || "");
164
- const vectorIds = ids || vectors.map(() => crypto.randomUUID());
165
- const records = vectors.map((vector, i) => ({
166
- id: vectorIds[i],
167
- values: vector,
168
- ...sparseVectors?.[i] && { sparseValues: sparseVectors?.[i] },
169
- metadata: metadata?.[i] || {}
170
- }));
171
- const batchSize = 100;
172
- try {
173
- for (let i = 0; i < records.length; i += batchSize) {
174
- const batch = records.slice(i, i + batchSize);
175
- await index.upsert(batch);
176
- }
177
- return vectorIds;
178
- } catch (error$1) {
179
- throw new error.MastraError(
180
- {
181
- id: storage.createVectorErrorId("PINECONE", "UPSERT", "FAILED"),
182
- domain: error.ErrorDomain.STORAGE,
183
- category: error.ErrorCategory.THIRD_PARTY,
184
- details: { indexName, vectorCount: vectors.length }
185
- },
186
- error$1
187
- );
188
- }
189
- }
190
- transformFilter(filter) {
191
- const translator = new PineconeFilterTranslator();
192
- return translator.translate(filter);
193
- }
194
- async query({
195
- indexName,
196
- queryVector,
197
- topK = 10,
198
- filter,
199
- includeVector = false,
200
- namespace,
201
- sparseVector
202
- }) {
203
- if (!queryVector) {
204
- throw new error.MastraError({
205
- id: storage.createVectorErrorId("PINECONE", "QUERY", "MISSING_VECTOR"),
206
- text: "queryVector is required for Pinecone queries. Metadata-only queries are not supported by this vector store.",
207
- domain: error.ErrorDomain.STORAGE,
208
- category: error.ErrorCategory.USER,
209
- details: { indexName }
210
- });
211
- }
212
- const index = this.client.Index(indexName).namespace(namespace || "");
213
- const translatedFilter = this.transformFilter(filter) ?? void 0;
214
- const queryParams = {
215
- vector: queryVector,
216
- topK,
217
- includeMetadata: true,
218
- includeValues: includeVector,
219
- filter: translatedFilter
220
- };
221
- if (sparseVector) {
222
- queryParams.sparseVector = sparseVector;
223
- }
224
- try {
225
- const results = await index.query(queryParams);
226
- return results.matches.map((match) => ({
227
- id: match.id,
228
- score: match.score || 0,
229
- metadata: match.metadata,
230
- ...includeVector && { vector: match.values || [] }
231
- }));
232
- } catch (error$1) {
233
- throw new error.MastraError(
234
- {
235
- id: storage.createVectorErrorId("PINECONE", "QUERY", "FAILED"),
236
- domain: error.ErrorDomain.STORAGE,
237
- category: error.ErrorCategory.THIRD_PARTY,
238
- details: { indexName, topK }
239
- },
240
- error$1
241
- );
242
- }
243
- }
244
- async listIndexes() {
245
- try {
246
- const indexesResult = await this.client.listIndexes();
247
- return indexesResult?.indexes?.map((index) => index.name) || [];
248
- } catch (error$1) {
249
- throw new error.MastraError(
250
- {
251
- id: storage.createVectorErrorId("PINECONE", "LIST_INDEXES", "FAILED"),
252
- domain: error.ErrorDomain.STORAGE,
253
- category: error.ErrorCategory.THIRD_PARTY
254
- },
255
- error$1
256
- );
257
- }
258
- }
259
- /**
260
- * Retrieves statistics about a vector index.
261
- *
262
- * @param {string} indexName - The name of the index to describe
263
- * @returns A promise that resolves to the index statistics including dimension, count and metric
264
- */
265
- async describeIndex({ indexName }) {
266
- try {
267
- const index = this.client.Index(indexName);
268
- const stats = await index.describeIndexStats();
269
- const description = await this.client.describeIndex(indexName);
270
- return {
271
- dimension: description.dimension,
272
- count: stats.totalRecordCount || 0,
273
- metric: description.metric,
274
- namespaces: stats.namespaces
275
- };
276
- } catch (error$1) {
277
- throw new error.MastraError(
278
- {
279
- id: storage.createVectorErrorId("PINECONE", "DESCRIBE_INDEX", "FAILED"),
280
- domain: error.ErrorDomain.STORAGE,
281
- category: error.ErrorCategory.THIRD_PARTY,
282
- details: { indexName }
283
- },
284
- error$1
285
- );
286
- }
287
- }
288
- async deleteIndex({ indexName }) {
289
- try {
290
- await this.client.deleteIndex(indexName);
291
- } catch (error$1) {
292
- throw new error.MastraError(
293
- {
294
- id: storage.createVectorErrorId("PINECONE", "DELETE_INDEX", "FAILED"),
295
- domain: error.ErrorDomain.STORAGE,
296
- category: error.ErrorCategory.THIRD_PARTY,
297
- details: { indexName }
298
- },
299
- error$1
300
- );
301
- }
302
- }
303
- /**
304
- * Updates a vector by its ID with the provided vector and/or metadata.
305
- * Note: Pinecone only supports update by ID, not by filter.
306
- * @param params - Parameters containing the id for targeting the vector to update
307
- * @param params.indexName - The name of the index containing the vector.
308
- * @param params.id - The ID of the vector to update.
309
- * @param params.update - An object containing the vector and/or metadata to update.
310
- * @param namespace - The namespace of the index (optional, Pinecone-specific).
311
- * @returns A promise that resolves when the update is complete.
312
- * @throws Will throw an error if no updates are provided or if the update operation fails.
313
- */
314
- async updateVector(params) {
315
- const { indexName, update } = params;
316
- if ("id" in params && params.id && "filter" in params && params.filter) {
317
- throw new error.MastraError({
318
- id: storage.createVectorErrorId("PINECONE", "UPDATE_VECTOR", "MUTUALLY_EXCLUSIVE"),
319
- text: "Cannot specify both id and filter - they are mutually exclusive",
320
- domain: error.ErrorDomain.STORAGE,
321
- category: error.ErrorCategory.USER,
322
- details: { indexName }
323
- });
324
- }
325
- if (!("id" in params && params.id) && !("filter" in params && params.filter)) {
326
- throw new error.MastraError({
327
- id: storage.createVectorErrorId("PINECONE", "UPDATE_VECTOR", "NO_TARGET"),
328
- text: "Either id or filter must be provided",
329
- domain: error.ErrorDomain.STORAGE,
330
- category: error.ErrorCategory.USER,
331
- details: { indexName }
332
- });
333
- }
334
- if (!update.vector && !update.metadata) {
335
- throw new error.MastraError({
336
- id: storage.createVectorErrorId("PINECONE", "UPDATE_VECTOR", "NO_PAYLOAD"),
337
- domain: error.ErrorDomain.STORAGE,
338
- category: error.ErrorCategory.USER,
339
- text: "No updates provided",
340
- details: { indexName }
341
- });
342
- }
343
- const namespace = params.namespace;
344
- try {
345
- const index = this.client.Index(indexName).namespace(namespace || "");
346
- if ("id" in params && params.id) {
347
- const updateObj = { id: params.id };
348
- if (update.vector) {
349
- updateObj.values = update.vector;
350
- }
351
- if (update.metadata) {
352
- updateObj.metadata = update.metadata;
353
- }
354
- await index.update(updateObj);
355
- } else if ("filter" in params && params.filter) {
356
- if (Object.keys(params.filter).length === 0) {
357
- throw new error.MastraError({
358
- id: storage.createVectorErrorId("PINECONE", "UPDATE_VECTOR", "EMPTY_FILTER"),
359
- text: "Filter cannot be an empty filter object",
360
- domain: error.ErrorDomain.STORAGE,
361
- category: error.ErrorCategory.USER,
362
- details: { indexName }
363
- });
364
- }
365
- const translatedFilter = this.transformFilter(params.filter);
366
- if (translatedFilter) {
367
- const stats = await this.describeIndex({ indexName });
368
- const dummyVector = new Array(stats.dimension).fill(1 / Math.sqrt(stats.dimension));
369
- const results = await index.query({
370
- vector: dummyVector,
371
- topK: 1e4,
372
- filter: translatedFilter,
373
- includeMetadata: false,
374
- includeValues: false
375
- });
376
- const idsToUpdate = results.matches.map((m) => m.id);
377
- for (const id of idsToUpdate) {
378
- const updateObj = { id };
379
- if (update.vector) {
380
- updateObj.values = update.vector;
381
- }
382
- if (update.metadata) {
383
- updateObj.metadata = update.metadata;
384
- }
385
- await index.update(updateObj);
386
- }
387
- }
388
- }
389
- } catch (error$1) {
390
- if (error$1 instanceof error.MastraError) throw error$1;
391
- throw new error.MastraError(
392
- {
393
- id: storage.createVectorErrorId("PINECONE", "UPDATE_VECTOR", "FAILED"),
394
- domain: error.ErrorDomain.STORAGE,
395
- category: error.ErrorCategory.THIRD_PARTY,
396
- details: {
397
- indexName,
398
- ..."id" in params && params.id && { id: params.id },
399
- ..."filter" in params && params.filter && { filter: JSON.stringify(params.filter) }
400
- }
401
- },
402
- error$1
403
- );
404
- }
405
- }
406
- /**
407
- * Deletes a vector by its ID.
408
- * @param indexName - The name of the index containing the vector.
409
- * @param id - The ID of the vector to delete.
410
- * @param namespace - The namespace of the index (optional).
411
- * @returns A promise that resolves when the deletion is complete.
412
- * @throws Will throw an error if the deletion operation fails.
413
- */
414
- async deleteVector({ indexName, id, namespace }) {
415
- try {
416
- const index = this.client.Index(indexName).namespace(namespace || "");
417
- await index.deleteOne(id);
418
- } catch (error$1) {
419
- throw new error.MastraError(
420
- {
421
- id: storage.createVectorErrorId("PINECONE", "DELETE_VECTOR", "FAILED"),
422
- domain: error.ErrorDomain.STORAGE,
423
- category: error.ErrorCategory.THIRD_PARTY,
424
- details: {
425
- indexName,
426
- ...id && { id }
427
- }
428
- },
429
- error$1
430
- );
431
- }
432
- }
433
- /**
434
- * Deletes multiple vectors by IDs or filter.
435
- * @param indexName - The name of the index containing the vectors.
436
- * @param ids - Array of vector IDs to delete (mutually exclusive with filter).
437
- * @param filter - Filter to match vectors to delete (mutually exclusive with ids).
438
- * @param namespace - The namespace of the index (optional, Pinecone-specific).
439
- * @returns A promise that resolves when the deletion is complete.
440
- * @throws Will throw an error if both ids and filter are provided, or if neither is provided.
441
- */
442
- async deleteVectors(params) {
443
- const { indexName, filter, ids } = params;
444
- const namespace = params.namespace;
445
- if (ids && filter) {
446
- throw new error.MastraError({
447
- id: storage.createVectorErrorId("PINECONE", "DELETE_VECTORS", "MUTUALLY_EXCLUSIVE"),
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
- // src/vector/prompt.ts
523
- var PINECONE_PROMPT = `When querying Pinecone, you can ONLY use the operators listed below. Any other operators will be rejected.
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
- //# sourceMappingURL=index.cjs.map
519
+
604
520
  //# sourceMappingURL=index.cjs.map