@mastra/pinecone 0.0.0-error-handler-fix-20251020202607 → 0.0.0-esbuild-bundle-worker-20260807182016

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.js CHANGED
@@ -1,370 +1,441 @@
1
- import { MastraError, ErrorCategory, ErrorDomain } from '@mastra/core/error';
2
- import { MastraVector } from '@mastra/core/vector';
3
- import { Pinecone } from '@pinecone-database/pinecone';
4
- import { BaseFilterTranslator } from '@mastra/core/vector/filter';
5
-
6
- // 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
7
7
  var PineconeFilterTranslator = class extends BaseFilterTranslator {
8
- getSupportedOperators() {
9
- return {
10
- ...BaseFilterTranslator.DEFAULT_OPERATORS,
11
- logical: ["$and", "$or"],
12
- array: ["$in", "$all", "$nin"],
13
- element: ["$exists"],
14
- regex: [],
15
- custom: []
16
- };
17
- }
18
- translate(filter) {
19
- if (this.isEmpty(filter)) return filter;
20
- this.validateFilter(filter);
21
- return this.translateNode(filter);
22
- }
23
- translateNode(node, currentPath = "") {
24
- if (this.isRegex(node)) {
25
- throw new Error("Regex is not supported in Pinecone");
26
- }
27
- if (this.isPrimitive(node)) return this.normalizeComparisonValue(node);
28
- if (Array.isArray(node)) return { $in: this.normalizeArrayValues(node) };
29
- const entries = Object.entries(node);
30
- const firstEntry = entries[0];
31
- if (entries.length === 1 && firstEntry && this.isOperator(firstEntry[0])) {
32
- const [operator, value] = firstEntry;
33
- const translated = this.translateOperator(operator, value, currentPath);
34
- return this.isLogicalOperator(operator) ? { [operator]: translated } : translated;
35
- }
36
- const result = {};
37
- for (const [key, value] of entries) {
38
- const newPath = currentPath ? `${currentPath}.${key}` : key;
39
- if (this.isOperator(key)) {
40
- result[key] = this.translateOperator(key, value, currentPath);
41
- continue;
42
- }
43
- if (typeof value === "object" && value !== null && !Array.isArray(value)) {
44
- if (Object.keys(value).length === 1 && "$all" in value) {
45
- const translated = this.translateNode(value, key);
46
- if (translated.$and) {
47
- return translated;
48
- }
49
- }
50
- if (Object.keys(value).length === 0) {
51
- result[newPath] = this.translateNode(value);
52
- } else {
53
- const hasOperators = Object.keys(value).some((k) => this.isOperator(k));
54
- if (hasOperators) {
55
- const normalizedValue = {};
56
- for (const [op, opValue] of Object.entries(value)) {
57
- normalizedValue[op] = this.isOperator(op) ? this.translateOperator(op, opValue) : opValue;
58
- }
59
- result[newPath] = normalizedValue;
60
- } else {
61
- Object.assign(result, this.translateNode(value, newPath));
62
- }
63
- }
64
- } else {
65
- result[newPath] = this.translateNode(value);
66
- }
67
- }
68
- return result;
69
- }
70
- translateOperator(operator, value, currentPath = "") {
71
- if (operator === "$all") {
72
- if (!Array.isArray(value) || value.length === 0) {
73
- throw new Error("A non-empty array is required for the $all operator");
74
- }
75
- return this.simulateAllOperator(currentPath, value);
76
- }
77
- if (this.isLogicalOperator(operator)) {
78
- return Array.isArray(value) ? value.map((item) => this.translateNode(item)) : this.translateNode(value);
79
- }
80
- return this.normalizeComparisonValue(value);
81
- }
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
+ }
82
68
  };
83
-
84
- // src/vector/index.ts
69
+ //#endregion
70
+ //#region src/vector/index.ts
85
71
  var PineconeVector = class extends MastraVector {
86
- client;
87
- /**
88
- * Creates a new PineconeVector client.
89
- * @param apiKey - The API key for Pinecone.
90
- * @param environment - The environment for Pinecone.
91
- */
92
- constructor({ apiKey, environment }) {
93
- super();
94
- const opts = { apiKey };
95
- if (environment) {
96
- opts["controllerHostUrl"] = environment;
97
- }
98
- const baseClient = new Pinecone(opts);
99
- const telemetry = this.__getTelemetry();
100
- this.client = telemetry?.traceClass(baseClient, {
101
- spanNamePrefix: "pinecone-vector",
102
- attributes: {
103
- "vector.type": "pinecone"
104
- }
105
- }) ?? baseClient;
106
- }
107
- get indexSeparator() {
108
- return "-";
109
- }
110
- async createIndex({ indexName, dimension, metric = "cosine" }) {
111
- try {
112
- if (!Number.isInteger(dimension) || dimension <= 0) {
113
- throw new Error("Dimension must be a positive integer");
114
- }
115
- if (metric && !["cosine", "euclidean", "dotproduct"].includes(metric)) {
116
- throw new Error("Metric must be one of: cosine, euclidean, dotproduct");
117
- }
118
- } catch (validationError) {
119
- throw new MastraError(
120
- {
121
- id: "STORAGE_PINECONE_VECTOR_CREATE_INDEX_INVALID_ARGS",
122
- domain: ErrorDomain.STORAGE,
123
- category: ErrorCategory.USER,
124
- details: { indexName, dimension, metric }
125
- },
126
- validationError
127
- );
128
- }
129
- try {
130
- await this.client.createIndex({
131
- name: indexName,
132
- dimension,
133
- metric,
134
- spec: {
135
- serverless: {
136
- cloud: "aws",
137
- region: "us-east-1"
138
- }
139
- }
140
- });
141
- } catch (error) {
142
- const message = error?.errors?.[0]?.message || error?.message;
143
- if (error.status === 409 || typeof message === "string" && (message.toLowerCase().includes("already exists") || message.toLowerCase().includes("duplicate"))) {
144
- await this.validateExistingIndex(indexName, dimension, metric);
145
- return;
146
- }
147
- throw new MastraError(
148
- {
149
- id: "STORAGE_PINECONE_VECTOR_CREATE_INDEX_FAILED",
150
- domain: ErrorDomain.STORAGE,
151
- category: ErrorCategory.THIRD_PARTY,
152
- details: { indexName, dimension, metric }
153
- },
154
- error
155
- );
156
- }
157
- }
158
- async upsert({
159
- indexName,
160
- vectors,
161
- metadata,
162
- ids,
163
- namespace,
164
- sparseVectors
165
- }) {
166
- const index = this.client.Index(indexName).namespace(namespace || "");
167
- const vectorIds = ids || vectors.map(() => crypto.randomUUID());
168
- const records = vectors.map((vector, i) => ({
169
- id: vectorIds[i],
170
- values: vector,
171
- ...sparseVectors?.[i] && { sparseValues: sparseVectors?.[i] },
172
- metadata: metadata?.[i] || {}
173
- }));
174
- const batchSize = 100;
175
- try {
176
- for (let i = 0; i < records.length; i += batchSize) {
177
- const batch = records.slice(i, i + batchSize);
178
- await index.upsert(batch);
179
- }
180
- return vectorIds;
181
- } catch (error) {
182
- throw new MastraError(
183
- {
184
- id: "STORAGE_PINECONE_VECTOR_UPSERT_FAILED",
185
- domain: ErrorDomain.STORAGE,
186
- category: ErrorCategory.THIRD_PARTY,
187
- details: { indexName, vectorCount: vectors.length }
188
- },
189
- error
190
- );
191
- }
192
- }
193
- transformFilter(filter) {
194
- const translator = new PineconeFilterTranslator();
195
- return translator.translate(filter);
196
- }
197
- async query({
198
- indexName,
199
- queryVector,
200
- topK = 10,
201
- filter,
202
- includeVector = false,
203
- namespace,
204
- sparseVector
205
- }) {
206
- const index = this.client.Index(indexName).namespace(namespace || "");
207
- const translatedFilter = this.transformFilter(filter) ?? void 0;
208
- const queryParams = {
209
- vector: queryVector,
210
- topK,
211
- includeMetadata: true,
212
- includeValues: includeVector,
213
- filter: translatedFilter
214
- };
215
- if (sparseVector) {
216
- queryParams.sparseVector = sparseVector;
217
- }
218
- try {
219
- const results = await index.query(queryParams);
220
- return results.matches.map((match) => ({
221
- id: match.id,
222
- score: match.score || 0,
223
- metadata: match.metadata,
224
- ...includeVector && { vector: match.values || [] }
225
- }));
226
- } catch (error) {
227
- throw new MastraError(
228
- {
229
- id: "STORAGE_PINECONE_VECTOR_QUERY_FAILED",
230
- domain: ErrorDomain.STORAGE,
231
- category: ErrorCategory.THIRD_PARTY,
232
- details: { indexName, topK }
233
- },
234
- error
235
- );
236
- }
237
- }
238
- async listIndexes() {
239
- try {
240
- const indexesResult = await this.client.listIndexes();
241
- return indexesResult?.indexes?.map((index) => index.name) || [];
242
- } catch (error) {
243
- throw new MastraError(
244
- {
245
- id: "STORAGE_PINECONE_VECTOR_LIST_INDEXES_FAILED",
246
- domain: ErrorDomain.STORAGE,
247
- category: ErrorCategory.THIRD_PARTY
248
- },
249
- error
250
- );
251
- }
252
- }
253
- /**
254
- * Retrieves statistics about a vector index.
255
- *
256
- * @param {string} indexName - The name of the index to describe
257
- * @returns A promise that resolves to the index statistics including dimension, count and metric
258
- */
259
- async describeIndex({ indexName }) {
260
- try {
261
- const index = this.client.Index(indexName);
262
- const stats = await index.describeIndexStats();
263
- const description = await this.client.describeIndex(indexName);
264
- return {
265
- dimension: description.dimension,
266
- count: stats.totalRecordCount || 0,
267
- metric: description.metric,
268
- namespaces: stats.namespaces
269
- };
270
- } catch (error) {
271
- throw new MastraError(
272
- {
273
- id: "STORAGE_PINECONE_VECTOR_DESCRIBE_INDEX_FAILED",
274
- domain: ErrorDomain.STORAGE,
275
- category: ErrorCategory.THIRD_PARTY,
276
- details: { indexName }
277
- },
278
- error
279
- );
280
- }
281
- }
282
- async deleteIndex({ indexName }) {
283
- try {
284
- await this.client.deleteIndex(indexName);
285
- } catch (error) {
286
- throw new MastraError(
287
- {
288
- id: "STORAGE_PINECONE_VECTOR_DELETE_INDEX_FAILED",
289
- domain: ErrorDomain.STORAGE,
290
- category: ErrorCategory.THIRD_PARTY,
291
- details: { indexName }
292
- },
293
- error
294
- );
295
- }
296
- }
297
- /**
298
- * Updates a vector by its ID with the provided vector and/or metadata.
299
- * @param indexName - The name of the index containing the vector.
300
- * @param id - The ID of the vector to update.
301
- * @param update - An object containing the vector and/or metadata to update.
302
- * @param update.vector - An optional array of numbers representing the new vector.
303
- * @param update.metadata - An optional record containing the new metadata.
304
- * @param namespace - The namespace of the index (optional).
305
- * @returns A promise that resolves when the update is complete.
306
- * @throws Will throw an error if no updates are provided or if the update operation fails.
307
- */
308
- async updateVector({ indexName, id, update, namespace }) {
309
- if (!update.vector && !update.metadata) {
310
- throw new MastraError({
311
- id: "STORAGE_PINECONE_VECTOR_UPDATE_VECTOR_INVALID_ARGS",
312
- domain: ErrorDomain.STORAGE,
313
- category: ErrorCategory.USER,
314
- text: "No updates provided",
315
- details: { indexName, id }
316
- });
317
- }
318
- try {
319
- const index = this.client.Index(indexName).namespace(namespace || "");
320
- const updateObj = { id };
321
- if (update.vector) {
322
- updateObj.values = update.vector;
323
- }
324
- if (update.metadata) {
325
- updateObj.metadata = update.metadata;
326
- }
327
- await index.update(updateObj);
328
- } catch (error) {
329
- throw new MastraError(
330
- {
331
- id: "STORAGE_PINECONE_VECTOR_UPDATE_VECTOR_FAILED",
332
- domain: ErrorDomain.STORAGE,
333
- category: ErrorCategory.THIRD_PARTY,
334
- details: { indexName, id }
335
- },
336
- error
337
- );
338
- }
339
- }
340
- /**
341
- * Deletes a vector by its ID.
342
- * @param indexName - The name of the index containing the vector.
343
- * @param id - The ID of the vector to delete.
344
- * @param namespace - The namespace of the index (optional).
345
- * @returns A promise that resolves when the deletion is complete.
346
- * @throws Will throw an error if the deletion operation fails.
347
- */
348
- async deleteVector({ indexName, id, namespace }) {
349
- try {
350
- const index = this.client.Index(indexName).namespace(namespace || "");
351
- await index.deleteOne(id);
352
- } catch (error) {
353
- throw new MastraError(
354
- {
355
- id: "STORAGE_PINECONE_VECTOR_DELETE_VECTOR_FAILED",
356
- domain: ErrorDomain.STORAGE,
357
- category: ErrorCategory.THIRD_PARTY,
358
- details: { indexName, id }
359
- },
360
- error
361
- );
362
- }
363
- }
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
+ }
364
431
  };
365
-
366
- // src/vector/prompt.ts
367
- var PINECONE_PROMPT = `When querying Pinecone, you can ONLY use the operators listed below. Any other operators will be rejected.
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.
368
439
  Important: Don't explain how to construct the filter - use the specified operators and fields to search the content and return relevant results.
369
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.
370
441
 
@@ -441,7 +512,7 @@ Example Complex Query:
441
512
  ]}
442
513
  ]
443
514
  }`;
444
-
515
+ //#endregion
445
516
  export { PINECONE_PROMPT, PineconeVector };
446
- //# sourceMappingURL=index.js.map
517
+
447
518
  //# sourceMappingURL=index.js.map