@mastra/vectorize 1.1.0 → 1.1.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,410 +1,430 @@
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 Cloudflare = require('cloudflare');
7
- var filter = require('@mastra/core/vector/filter');
8
-
9
- function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
10
-
11
- var Cloudflare__default = /*#__PURE__*/_interopDefault(Cloudflare);
12
-
13
- // src/vector/index.ts
14
- var VectorizeFilterTranslator = class extends filter.BaseFilterTranslator {
15
- getSupportedOperators() {
16
- return {
17
- ...filter.BaseFilterTranslator.DEFAULT_OPERATORS,
18
- logical: [],
19
- array: ["$in", "$nin"],
20
- element: [],
21
- regex: [],
22
- custom: []
23
- };
24
- }
25
- translate(filter) {
26
- if (this.isEmpty(filter)) return filter;
27
- this.validateFilter(filter);
28
- return this.translateNode(filter);
29
- }
30
- translateNode(node, currentPath = "") {
31
- if (this.isRegex(node)) {
32
- throw new Error("Regex is not supported in Vectorize");
33
- }
34
- if (this.isPrimitive(node)) return { $eq: this.normalizeComparisonValue(node) };
35
- if (Array.isArray(node)) return { $in: this.normalizeArrayValues(node) };
36
- const entries = Object.entries(node);
37
- const firstEntry = entries[0];
38
- if (entries.length === 1 && firstEntry && this.isOperator(firstEntry[0])) {
39
- const [operator, value] = firstEntry;
40
- return { [operator]: this.normalizeComparisonValue(value) };
41
- }
42
- const result = {};
43
- for (const [key, value] of entries) {
44
- const newPath = currentPath ? `${currentPath}.${key}` : key;
45
- if (this.isOperator(key)) {
46
- result[key] = this.normalizeComparisonValue(value);
47
- continue;
48
- }
49
- if (typeof value === "object" && value !== null && !Array.isArray(value)) {
50
- if (Object.keys(value).length === 0) {
51
- result[newPath] = this.translateNode(value);
52
- continue;
53
- }
54
- const hasOperators = Object.keys(value).some((k) => this.isOperator(k));
55
- if (hasOperators) {
56
- result[newPath] = this.translateNode(value);
57
- } else {
58
- Object.assign(result, this.translateNode(value, newPath));
59
- }
60
- } else {
61
- result[newPath] = this.translateNode(value);
62
- }
63
- }
64
- return result;
65
- }
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
+ //#region \0rolldown/runtime.js
3
+ var __create = Object.create;
4
+ var __defProp = Object.defineProperty;
5
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
6
+ var __getOwnPropNames = Object.getOwnPropertyNames;
7
+ var __getProtoOf = Object.getPrototypeOf;
8
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
11
+ key = keys[i];
12
+ if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
13
+ get: ((k) => from[k]).bind(null, key),
14
+ enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
15
+ });
16
+ }
17
+ return to;
66
18
  };
67
-
68
- // src/vector/index.ts
69
- var CloudflareVector = class extends vector.MastraVector {
70
- client;
71
- accountId;
72
- constructor({ accountId, apiToken, id }) {
73
- super({ id });
74
- this.accountId = accountId;
75
- this.client = new Cloudflare__default.default({
76
- apiToken
77
- });
78
- }
79
- get indexSeparator() {
80
- return "-";
81
- }
82
- async upsert({ indexName, vectors, metadata, ids }) {
83
- const generatedIds = ids || vectors.map(() => crypto.randomUUID());
84
- const ndjson = vectors.map(
85
- (vector, index) => JSON.stringify({
86
- id: generatedIds[index],
87
- values: vector,
88
- metadata: metadata?.[index]
89
- })
90
- ).join("\n");
91
- try {
92
- const body = new File([ndjson], `${indexName}.ndjson`, { type: "application/x-ndjson" });
93
- await this.client.vectorize.indexes.upsert(indexName, {
94
- account_id: this.accountId,
95
- body
96
- });
97
- return generatedIds;
98
- } catch (error$1) {
99
- throw new error.MastraError(
100
- {
101
- id: storage.createVectorErrorId("VECTORIZE", "UPSERT", "FAILED"),
102
- domain: error.ErrorDomain.STORAGE,
103
- category: error.ErrorCategory.THIRD_PARTY,
104
- details: { indexName, vectorCount: vectors?.length }
105
- },
106
- error$1
107
- );
108
- }
109
- }
110
- transformFilter(filter) {
111
- const translator = new VectorizeFilterTranslator();
112
- return translator.translate(filter);
113
- }
114
- async createIndex({ indexName, dimension, metric = "cosine" }) {
115
- try {
116
- await this.client.vectorize.indexes.create({
117
- account_id: this.accountId,
118
- config: {
119
- dimensions: dimension,
120
- metric: metric === "dotproduct" ? "dot-product" : metric
121
- },
122
- name: indexName
123
- });
124
- } catch (error$1) {
125
- const message = error$1?.errors?.[0]?.message || error$1?.message;
126
- if (error$1.status === 409 || typeof message === "string" && (message.toLowerCase().includes("already exists") || message.toLowerCase().includes("duplicate"))) {
127
- await this.validateExistingIndex(indexName, dimension, metric);
128
- return;
129
- }
130
- throw new error.MastraError(
131
- {
132
- id: storage.createVectorErrorId("VECTORIZE", "CREATE_INDEX", "FAILED"),
133
- domain: error.ErrorDomain.STORAGE,
134
- category: error.ErrorCategory.THIRD_PARTY,
135
- details: { indexName, dimension, metric }
136
- },
137
- error$1
138
- );
139
- }
140
- }
141
- async query({
142
- indexName,
143
- queryVector,
144
- topK = 10,
145
- filter,
146
- includeVector = false
147
- }) {
148
- if (!queryVector) {
149
- throw new error.MastraError({
150
- id: storage.createVectorErrorId("VECTORIZE", "QUERY", "MISSING_VECTOR"),
151
- text: "queryVector is required for Vectorize queries. Metadata-only queries are not supported by this vector store.",
152
- domain: error.ErrorDomain.STORAGE,
153
- category: error.ErrorCategory.USER,
154
- details: { indexName }
155
- });
156
- }
157
- try {
158
- const translatedFilter = this.transformFilter(filter) ?? {};
159
- const response = await this.client.vectorize.indexes.query(indexName, {
160
- account_id: this.accountId,
161
- vector: queryVector,
162
- returnValues: includeVector,
163
- returnMetadata: "all",
164
- topK,
165
- filter: translatedFilter
166
- });
167
- return response?.matches?.map((match) => {
168
- return {
169
- id: match.id,
170
- metadata: match.metadata,
171
- score: match.score,
172
- vector: match.values
173
- };
174
- }) || [];
175
- } catch (error$1) {
176
- throw new error.MastraError(
177
- {
178
- id: storage.createVectorErrorId("VECTORIZE", "QUERY", "FAILED"),
179
- domain: error.ErrorDomain.STORAGE,
180
- category: error.ErrorCategory.THIRD_PARTY,
181
- details: { indexName, topK }
182
- },
183
- error$1
184
- );
185
- }
186
- }
187
- async listIndexes() {
188
- try {
189
- const res = await this.client.vectorize.indexes.list({
190
- account_id: this.accountId
191
- });
192
- return res?.result?.map((index) => index.name) || [];
193
- } catch (error$1) {
194
- throw new error.MastraError(
195
- {
196
- id: storage.createVectorErrorId("VECTORIZE", "LIST_INDEXES", "FAILED"),
197
- domain: error.ErrorDomain.STORAGE,
198
- category: error.ErrorCategory.THIRD_PARTY
199
- },
200
- error$1
201
- );
202
- }
203
- }
204
- /**
205
- * Retrieves statistics about a vector index.
206
- *
207
- * @param {string} indexName - The name of the index to describe
208
- * @returns A promise that resolves to the index statistics including dimension, count and metric
209
- */
210
- async describeIndex({ indexName }) {
211
- try {
212
- const index = await this.client.vectorize.indexes.get(indexName, {
213
- account_id: this.accountId
214
- });
215
- const described = await this.client.vectorize.indexes.info(indexName, {
216
- account_id: this.accountId
217
- });
218
- return {
219
- dimension: described?.dimensions,
220
- // Since vector_count is not available in the response,
221
- // we might need a separate API call to get the count if needed
222
- count: described?.vectorCount || 0,
223
- metric: index?.config?.metric
224
- };
225
- } catch (error$1) {
226
- throw new error.MastraError(
227
- {
228
- id: storage.createVectorErrorId("VECTORIZE", "DESCRIBE_INDEX", "FAILED"),
229
- domain: error.ErrorDomain.STORAGE,
230
- category: error.ErrorCategory.THIRD_PARTY,
231
- details: { indexName }
232
- },
233
- error$1
234
- );
235
- }
236
- }
237
- async deleteIndex({ indexName }) {
238
- try {
239
- await this.client.vectorize.indexes.delete(indexName, {
240
- account_id: this.accountId
241
- });
242
- } catch (error$1) {
243
- throw new error.MastraError(
244
- {
245
- id: storage.createVectorErrorId("VECTORIZE", "DELETE_INDEX", "FAILED"),
246
- domain: error.ErrorDomain.STORAGE,
247
- category: error.ErrorCategory.THIRD_PARTY,
248
- details: { indexName }
249
- },
250
- error$1
251
- );
252
- }
253
- }
254
- async createMetadataIndex(indexName, propertyName, indexType) {
255
- try {
256
- await this.client.vectorize.indexes.metadataIndex.create(indexName, {
257
- account_id: this.accountId,
258
- propertyName,
259
- indexType
260
- });
261
- } catch (error$1) {
262
- throw new error.MastraError(
263
- {
264
- id: storage.createVectorErrorId("VECTORIZE", "CREATE_METADATA_INDEX", "FAILED"),
265
- domain: error.ErrorDomain.STORAGE,
266
- category: error.ErrorCategory.THIRD_PARTY,
267
- details: { indexName, propertyName, indexType }
268
- },
269
- error$1
270
- );
271
- }
272
- }
273
- async deleteMetadataIndex(indexName, propertyName) {
274
- try {
275
- await this.client.vectorize.indexes.metadataIndex.delete(indexName, {
276
- account_id: this.accountId,
277
- propertyName
278
- });
279
- } catch (error$1) {
280
- throw new error.MastraError(
281
- {
282
- id: storage.createVectorErrorId("VECTORIZE", "DELETE_METADATA_INDEX", "FAILED"),
283
- domain: error.ErrorDomain.STORAGE,
284
- category: error.ErrorCategory.THIRD_PARTY,
285
- details: { indexName, propertyName }
286
- },
287
- error$1
288
- );
289
- }
290
- }
291
- async listMetadataIndexes(indexName) {
292
- try {
293
- const res = await this.client.vectorize.indexes.metadataIndex.list(indexName, {
294
- account_id: this.accountId
295
- });
296
- return res?.metadataIndexes ?? [];
297
- } catch (error$1) {
298
- throw new error.MastraError(
299
- {
300
- id: storage.createVectorErrorId("VECTORIZE", "LIST_METADATA_INDEXES", "FAILED"),
301
- domain: error.ErrorDomain.STORAGE,
302
- category: error.ErrorCategory.THIRD_PARTY,
303
- details: { indexName }
304
- },
305
- error$1
306
- );
307
- }
308
- }
309
- /**
310
- * Updates a vector by its ID with the provided vector and/or metadata.
311
- * @param indexName - The name of the index containing the vector.
312
- * @param id - The ID of the vector to update.
313
- * @param update - An object containing the vector and/or metadata to update.
314
- * @param update.vector - An optional array of numbers representing the new vector.
315
- * @param update.metadata - An optional record containing the new metadata.
316
- * @returns A promise that resolves when the update is complete.
317
- * @throws Will throw an error if no updates are provided or if the update operation fails.
318
- */
319
- async updateVector({ indexName, id, update }) {
320
- if (!id) {
321
- throw new error.MastraError({
322
- id: storage.createVectorErrorId("VECTORIZE", "UPDATE_VECTOR", "INVALID_ARGS"),
323
- domain: error.ErrorDomain.STORAGE,
324
- category: error.ErrorCategory.USER,
325
- text: "id is required for Vectorize updateVector",
326
- details: { indexName }
327
- });
328
- }
329
- if (!update.vector && !update.metadata) {
330
- throw new error.MastraError({
331
- id: storage.createVectorErrorId("VECTORIZE", "UPDATE_VECTOR", "NO_PAYLOAD"),
332
- domain: error.ErrorDomain.STORAGE,
333
- category: error.ErrorCategory.USER,
334
- text: "No update data provided",
335
- details: { indexName, id }
336
- });
337
- }
338
- try {
339
- const updatePayload = {
340
- };
341
- if (update.vector) {
342
- updatePayload.vectors = [update.vector];
343
- }
344
- if (update.metadata) {
345
- updatePayload.metadata = [update.metadata];
346
- }
347
- await this.upsert({ indexName, vectors: updatePayload.vectors, metadata: updatePayload.metadata });
348
- } catch (error$1) {
349
- throw new error.MastraError(
350
- {
351
- id: storage.createVectorErrorId("VECTORIZE", "UPDATE_VECTOR", "FAILED"),
352
- domain: error.ErrorDomain.STORAGE,
353
- category: error.ErrorCategory.THIRD_PARTY,
354
- details: {
355
- indexName,
356
- ...id && { id }
357
- }
358
- },
359
- error$1
360
- );
361
- }
362
- }
363
- /**
364
- * Deletes a vector by its ID.
365
- * @param indexName - The name of the index containing the vector.
366
- * @param id - The ID of the vector to delete.
367
- * @returns A promise that resolves when the deletion is complete.
368
- * @throws Will throw an error if the deletion operation fails.
369
- */
370
- async deleteVector({ indexName, id }) {
371
- try {
372
- await this.client.vectorize.indexes.deleteByIds(indexName, {
373
- ids: [id],
374
- account_id: this.accountId
375
- });
376
- } catch (error$1) {
377
- throw new error.MastraError(
378
- {
379
- id: storage.createVectorErrorId("VECTORIZE", "DELETE_VECTOR", "FAILED"),
380
- domain: error.ErrorDomain.STORAGE,
381
- category: error.ErrorCategory.THIRD_PARTY,
382
- details: {
383
- indexName,
384
- ...id && { id }
385
- }
386
- },
387
- error$1
388
- );
389
- }
390
- }
391
- async deleteVectors({ indexName, filter, ids }) {
392
- throw new error.MastraError({
393
- id: storage.createVectorErrorId("VECTORIZE", "DELETE_VECTORS", "NOT_SUPPORTED"),
394
- text: "deleteVectors is not yet implemented for Vectorize vector store",
395
- domain: error.ErrorDomain.STORAGE,
396
- category: error.ErrorCategory.SYSTEM,
397
- details: {
398
- indexName,
399
- ...filter && { filter: JSON.stringify(filter) },
400
- ...ids && { idsCount: ids.length }
401
- }
402
- });
403
- }
19
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
20
+ value: mod,
21
+ enumerable: true
22
+ }) : target, mod));
23
+ //#endregion
24
+ let _mastra_core_error = require("@mastra/core/error");
25
+ let _mastra_core_storage = require("@mastra/core/storage");
26
+ let _mastra_core_vector = require("@mastra/core/vector");
27
+ let cloudflare = require("cloudflare");
28
+ cloudflare = __toESM(cloudflare, 1);
29
+ let _mastra_core_vector_filter = require("@mastra/core/vector/filter");
30
+ //#region src/vector/filter.ts
31
+ var VectorizeFilterTranslator = class extends _mastra_core_vector_filter.BaseFilterTranslator {
32
+ getSupportedOperators() {
33
+ return {
34
+ ..._mastra_core_vector_filter.BaseFilterTranslator.DEFAULT_OPERATORS,
35
+ logical: [],
36
+ array: ["$in", "$nin"],
37
+ element: [],
38
+ regex: [],
39
+ custom: []
40
+ };
41
+ }
42
+ translate(filter) {
43
+ if (this.isEmpty(filter)) return filter;
44
+ this.validateFilter(filter);
45
+ return this.translateNode(filter);
46
+ }
47
+ translateNode(node, currentPath = "") {
48
+ if (this.isRegex(node)) throw new Error("Regex is not supported in Vectorize");
49
+ if (this.isPrimitive(node)) return { $eq: this.normalizeComparisonValue(node) };
50
+ if (Array.isArray(node)) return { $in: this.normalizeArrayValues(node) };
51
+ const entries = Object.entries(node);
52
+ const firstEntry = entries[0];
53
+ if (entries.length === 1 && firstEntry && this.isOperator(firstEntry[0])) {
54
+ const [operator, value] = firstEntry;
55
+ return { [operator]: this.normalizeComparisonValue(value) };
56
+ }
57
+ const result = {};
58
+ for (const [key, value] of entries) {
59
+ const newPath = currentPath ? `${currentPath}.${key}` : key;
60
+ if (this.isOperator(key)) {
61
+ result[key] = this.normalizeComparisonValue(value);
62
+ continue;
63
+ }
64
+ if (typeof value === "object" && value !== null && !Array.isArray(value)) {
65
+ if (Object.keys(value).length === 0) {
66
+ result[newPath] = this.translateNode(value);
67
+ continue;
68
+ }
69
+ if (Object.keys(value).some((k) => this.isOperator(k))) result[newPath] = this.translateNode(value);
70
+ else Object.assign(result, this.translateNode(value, newPath));
71
+ } else result[newPath] = this.translateNode(value);
72
+ }
73
+ return result;
74
+ }
404
75
  };
405
-
406
- // src/vector/prompt.ts
407
- var VECTORIZE_PROMPT = `When querying Vectorize, you can ONLY use the operators listed below. Any other operators will be rejected.
76
+ //#endregion
77
+ //#region src/vector/index.ts
78
+ var CloudflareVector = class extends _mastra_core_vector.MastraVector {
79
+ client;
80
+ accountId;
81
+ constructor({ accountId, apiToken, id }) {
82
+ super({ id });
83
+ this.accountId = accountId;
84
+ this.client = new cloudflare.default({ apiToken });
85
+ }
86
+ get indexSeparator() {
87
+ return "-";
88
+ }
89
+ async upsert({ indexName, vectors, metadata, ids }) {
90
+ const generatedIds = ids || vectors.map(() => crypto.randomUUID());
91
+ const ndjson = vectors.map((vector, index) => JSON.stringify({
92
+ id: generatedIds[index],
93
+ values: vector,
94
+ metadata: metadata?.[index]
95
+ })).join("\n");
96
+ try {
97
+ const body = new File([ndjson], `${indexName}.ndjson`, { type: "application/x-ndjson" });
98
+ await this.client.vectorize.indexes.upsert(indexName, {
99
+ account_id: this.accountId,
100
+ body
101
+ });
102
+ return generatedIds;
103
+ } catch (error) {
104
+ throw new _mastra_core_error.MastraError({
105
+ id: (0, _mastra_core_storage.createVectorErrorId)("VECTORIZE", "UPSERT", "FAILED"),
106
+ domain: _mastra_core_error.ErrorDomain.STORAGE,
107
+ category: _mastra_core_error.ErrorCategory.THIRD_PARTY,
108
+ details: {
109
+ indexName,
110
+ vectorCount: vectors?.length
111
+ }
112
+ }, error);
113
+ }
114
+ }
115
+ transformFilter(filter) {
116
+ return new VectorizeFilterTranslator().translate(filter);
117
+ }
118
+ async createIndex({ indexName, dimension, metric = "cosine" }) {
119
+ try {
120
+ await this.client.vectorize.indexes.create({
121
+ account_id: this.accountId,
122
+ config: {
123
+ dimensions: dimension,
124
+ metric: metric === "dotproduct" ? "dot-product" : metric
125
+ },
126
+ name: indexName
127
+ });
128
+ } catch (error) {
129
+ const message = error?.errors?.[0]?.message || error?.message;
130
+ if (error.status === 409 || typeof message === "string" && (message.toLowerCase().includes("already exists") || message.toLowerCase().includes("duplicate"))) {
131
+ await this.validateExistingIndex(indexName, dimension, metric);
132
+ return;
133
+ }
134
+ throw new _mastra_core_error.MastraError({
135
+ id: (0, _mastra_core_storage.createVectorErrorId)("VECTORIZE", "CREATE_INDEX", "FAILED"),
136
+ domain: _mastra_core_error.ErrorDomain.STORAGE,
137
+ category: _mastra_core_error.ErrorCategory.THIRD_PARTY,
138
+ details: {
139
+ indexName,
140
+ dimension,
141
+ metric
142
+ }
143
+ }, error);
144
+ }
145
+ }
146
+ async query({ indexName, queryVector, topK = 10, filter, includeVector = false }) {
147
+ if (!queryVector) throw new _mastra_core_error.MastraError({
148
+ id: (0, _mastra_core_storage.createVectorErrorId)("VECTORIZE", "QUERY", "MISSING_VECTOR"),
149
+ text: "queryVector is required for Vectorize queries. Metadata-only queries are not supported by this vector store.",
150
+ domain: _mastra_core_error.ErrorDomain.STORAGE,
151
+ category: _mastra_core_error.ErrorCategory.USER,
152
+ details: { indexName }
153
+ });
154
+ try {
155
+ const translatedFilter = this.transformFilter(filter) ?? {};
156
+ return (await this.client.vectorize.indexes.query(indexName, {
157
+ account_id: this.accountId,
158
+ vector: queryVector,
159
+ returnValues: includeVector,
160
+ returnMetadata: "all",
161
+ topK,
162
+ filter: translatedFilter
163
+ }))?.matches?.map((match) => {
164
+ return {
165
+ id: match.id,
166
+ metadata: match.metadata,
167
+ score: match.score,
168
+ vector: match.values
169
+ };
170
+ }) || [];
171
+ } catch (error) {
172
+ throw new _mastra_core_error.MastraError({
173
+ id: (0, _mastra_core_storage.createVectorErrorId)("VECTORIZE", "QUERY", "FAILED"),
174
+ domain: _mastra_core_error.ErrorDomain.STORAGE,
175
+ category: _mastra_core_error.ErrorCategory.THIRD_PARTY,
176
+ details: {
177
+ indexName,
178
+ topK
179
+ }
180
+ }, error);
181
+ }
182
+ }
183
+ async listIndexes() {
184
+ try {
185
+ return (await this.client.vectorize.indexes.list({ account_id: this.accountId }))?.result?.map((index) => index.name) || [];
186
+ } catch (error) {
187
+ throw new _mastra_core_error.MastraError({
188
+ id: (0, _mastra_core_storage.createVectorErrorId)("VECTORIZE", "LIST_INDEXES", "FAILED"),
189
+ domain: _mastra_core_error.ErrorDomain.STORAGE,
190
+ category: _mastra_core_error.ErrorCategory.THIRD_PARTY
191
+ }, error);
192
+ }
193
+ }
194
+ /**
195
+ * Retrieves statistics about a vector index.
196
+ *
197
+ * @param {string} indexName - The name of the index to describe
198
+ * @returns A promise that resolves to the index statistics including dimension, count and metric
199
+ */
200
+ async describeIndex({ indexName }) {
201
+ try {
202
+ const index = await this.client.vectorize.indexes.get(indexName, { account_id: this.accountId });
203
+ const described = await this.client.vectorize.indexes.info(indexName, { account_id: this.accountId });
204
+ return {
205
+ dimension: described?.dimensions,
206
+ count: described?.vectorCount || 0,
207
+ metric: index?.config?.metric
208
+ };
209
+ } catch (error) {
210
+ throw new _mastra_core_error.MastraError({
211
+ id: (0, _mastra_core_storage.createVectorErrorId)("VECTORIZE", "DESCRIBE_INDEX", "FAILED"),
212
+ domain: _mastra_core_error.ErrorDomain.STORAGE,
213
+ category: _mastra_core_error.ErrorCategory.THIRD_PARTY,
214
+ details: { indexName }
215
+ }, error);
216
+ }
217
+ }
218
+ async deleteIndex({ indexName }) {
219
+ try {
220
+ await this.client.vectorize.indexes.delete(indexName, { account_id: this.accountId });
221
+ } catch (error) {
222
+ throw new _mastra_core_error.MastraError({
223
+ id: (0, _mastra_core_storage.createVectorErrorId)("VECTORIZE", "DELETE_INDEX", "FAILED"),
224
+ domain: _mastra_core_error.ErrorDomain.STORAGE,
225
+ category: _mastra_core_error.ErrorCategory.THIRD_PARTY,
226
+ details: { indexName }
227
+ }, error);
228
+ }
229
+ }
230
+ async createMetadataIndex(indexName, propertyName, indexType) {
231
+ try {
232
+ await this.client.vectorize.indexes.metadataIndex.create(indexName, {
233
+ account_id: this.accountId,
234
+ propertyName,
235
+ indexType
236
+ });
237
+ } catch (error) {
238
+ throw new _mastra_core_error.MastraError({
239
+ id: (0, _mastra_core_storage.createVectorErrorId)("VECTORIZE", "CREATE_METADATA_INDEX", "FAILED"),
240
+ domain: _mastra_core_error.ErrorDomain.STORAGE,
241
+ category: _mastra_core_error.ErrorCategory.THIRD_PARTY,
242
+ details: {
243
+ indexName,
244
+ propertyName,
245
+ indexType
246
+ }
247
+ }, error);
248
+ }
249
+ }
250
+ async deleteMetadataIndex(indexName, propertyName) {
251
+ try {
252
+ await this.client.vectorize.indexes.metadataIndex.delete(indexName, {
253
+ account_id: this.accountId,
254
+ propertyName
255
+ });
256
+ } catch (error) {
257
+ throw new _mastra_core_error.MastraError({
258
+ id: (0, _mastra_core_storage.createVectorErrorId)("VECTORIZE", "DELETE_METADATA_INDEX", "FAILED"),
259
+ domain: _mastra_core_error.ErrorDomain.STORAGE,
260
+ category: _mastra_core_error.ErrorCategory.THIRD_PARTY,
261
+ details: {
262
+ indexName,
263
+ propertyName
264
+ }
265
+ }, error);
266
+ }
267
+ }
268
+ async listMetadataIndexes(indexName) {
269
+ try {
270
+ return (await this.client.vectorize.indexes.metadataIndex.list(indexName, { account_id: this.accountId }))?.metadataIndexes ?? [];
271
+ } catch (error) {
272
+ throw new _mastra_core_error.MastraError({
273
+ id: (0, _mastra_core_storage.createVectorErrorId)("VECTORIZE", "LIST_METADATA_INDEXES", "FAILED"),
274
+ domain: _mastra_core_error.ErrorDomain.STORAGE,
275
+ category: _mastra_core_error.ErrorCategory.THIRD_PARTY,
276
+ details: { indexName }
277
+ }, error);
278
+ }
279
+ }
280
+ /**
281
+ * Updates a vector by its ID with the provided vector and/or metadata.
282
+ * @param indexName - The name of the index containing the vector.
283
+ * @param id - The ID of the vector to update.
284
+ * @param update - An object containing the vector and/or metadata to update.
285
+ * @param update.vector - An optional array of numbers representing the new vector.
286
+ * @param update.metadata - An optional record containing the new metadata.
287
+ * @returns A promise that resolves when the update is complete.
288
+ * @throws Will throw an error if no updates are provided or if the update operation fails.
289
+ */
290
+ async updateVector({ indexName, id, update }) {
291
+ if (!id) throw new _mastra_core_error.MastraError({
292
+ id: (0, _mastra_core_storage.createVectorErrorId)("VECTORIZE", "UPDATE_VECTOR", "INVALID_ARGS"),
293
+ domain: _mastra_core_error.ErrorDomain.STORAGE,
294
+ category: _mastra_core_error.ErrorCategory.USER,
295
+ text: "id is required for Vectorize updateVector",
296
+ details: { indexName }
297
+ });
298
+ if (!update.vector && !update.metadata) throw new _mastra_core_error.MastraError({
299
+ id: (0, _mastra_core_storage.createVectorErrorId)("VECTORIZE", "UPDATE_VECTOR", "NO_PAYLOAD"),
300
+ domain: _mastra_core_error.ErrorDomain.STORAGE,
301
+ category: _mastra_core_error.ErrorCategory.USER,
302
+ text: "No update data provided",
303
+ details: {
304
+ indexName,
305
+ id
306
+ }
307
+ });
308
+ if (!update.vector) throw new _mastra_core_error.MastraError({
309
+ id: (0, _mastra_core_storage.createVectorErrorId)("VECTORIZE", "UPDATE_VECTOR", "MISSING_VECTOR"),
310
+ domain: _mastra_core_error.ErrorDomain.STORAGE,
311
+ category: _mastra_core_error.ErrorCategory.USER,
312
+ text: "Vectorize requires vector values when updating; metadata-only updates are not supported. Provide update.vector alongside update.metadata.",
313
+ details: {
314
+ indexName,
315
+ id
316
+ }
317
+ });
318
+ try {
319
+ await this.upsert({
320
+ indexName,
321
+ ids: [id],
322
+ vectors: [update.vector],
323
+ ...update.metadata && { metadata: [update.metadata] }
324
+ });
325
+ } catch (error) {
326
+ throw new _mastra_core_error.MastraError({
327
+ id: (0, _mastra_core_storage.createVectorErrorId)("VECTORIZE", "UPDATE_VECTOR", "FAILED"),
328
+ domain: _mastra_core_error.ErrorDomain.STORAGE,
329
+ category: _mastra_core_error.ErrorCategory.THIRD_PARTY,
330
+ details: {
331
+ indexName,
332
+ ...id && { id }
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
+ * @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 }) {
345
+ try {
346
+ await this.client.vectorize.indexes.deleteByIds(indexName, {
347
+ ids: [id],
348
+ account_id: this.accountId
349
+ });
350
+ } catch (error) {
351
+ throw new _mastra_core_error.MastraError({
352
+ id: (0, _mastra_core_storage.createVectorErrorId)("VECTORIZE", "DELETE_VECTOR", "FAILED"),
353
+ domain: _mastra_core_error.ErrorDomain.STORAGE,
354
+ category: _mastra_core_error.ErrorCategory.THIRD_PARTY,
355
+ details: {
356
+ indexName,
357
+ ...id && { id }
358
+ }
359
+ }, error);
360
+ }
361
+ }
362
+ /**
363
+ * Deletes multiple vectors by their IDs.
364
+ *
365
+ * Vectorize has no filtered-delete primitive, so metadata-filter deletion is rejected.
366
+ *
367
+ * @param indexName - The name of the index containing the vectors.
368
+ * @param ids - The IDs of the vectors to delete. Mutually exclusive with `filter`.
369
+ * @throws Will throw an error if the arguments are invalid or the deletion operation fails.
370
+ */
371
+ async deleteVectors({ indexName, filter, ids }) {
372
+ if (ids && filter) throw new _mastra_core_error.MastraError({
373
+ id: (0, _mastra_core_storage.createVectorErrorId)("VECTORIZE", "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 (filter) throw new _mastra_core_error.MastraError({
380
+ id: (0, _mastra_core_storage.createVectorErrorId)("VECTORIZE", "DELETE_VECTORS", "UNSUPPORTED_FILTER"),
381
+ text: "Deleting by metadata filter is not supported for Vectorize vector store - delete by ids instead",
382
+ domain: _mastra_core_error.ErrorDomain.STORAGE,
383
+ category: _mastra_core_error.ErrorCategory.SYSTEM,
384
+ details: {
385
+ indexName,
386
+ filter: JSON.stringify(filter)
387
+ }
388
+ });
389
+ if (!ids) throw new _mastra_core_error.MastraError({
390
+ id: (0, _mastra_core_storage.createVectorErrorId)("VECTORIZE", "DELETE_VECTORS", "NO_TARGET"),
391
+ text: "Either filter or ids must be provided",
392
+ domain: _mastra_core_error.ErrorDomain.STORAGE,
393
+ category: _mastra_core_error.ErrorCategory.USER,
394
+ details: { indexName }
395
+ });
396
+ if (ids.length === 0) throw new _mastra_core_error.MastraError({
397
+ id: (0, _mastra_core_storage.createVectorErrorId)("VECTORIZE", "DELETE_VECTORS", "EMPTY_IDS"),
398
+ text: "Cannot delete with empty ids array",
399
+ domain: _mastra_core_error.ErrorDomain.STORAGE,
400
+ category: _mastra_core_error.ErrorCategory.USER,
401
+ details: { indexName }
402
+ });
403
+ try {
404
+ await this.client.vectorize.indexes.deleteByIds(indexName, {
405
+ ids,
406
+ account_id: this.accountId
407
+ });
408
+ } catch (error) {
409
+ throw new _mastra_core_error.MastraError({
410
+ id: (0, _mastra_core_storage.createVectorErrorId)("VECTORIZE", "DELETE_VECTORS", "FAILED"),
411
+ domain: _mastra_core_error.ErrorDomain.STORAGE,
412
+ category: _mastra_core_error.ErrorCategory.THIRD_PARTY,
413
+ details: {
414
+ indexName,
415
+ idsCount: ids.length
416
+ }
417
+ }, error);
418
+ }
419
+ }
420
+ };
421
+ //#endregion
422
+ //#region src/vector/prompt.ts
423
+ /**
424
+ * Vector store specific prompt that details supported operators and examples.
425
+ * This prompt helps users construct valid filters for Vectorize.
426
+ */
427
+ const VECTORIZE_PROMPT = `When querying Vectorize, you can ONLY use the operators listed below. Any other operators will be rejected.
408
428
  Important: Don't explain how to construct the filter - use the specified operators and fields to search the content and return relevant results.
409
429
  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.
410
430
 
@@ -480,8 +500,8 @@ Example Complex Query:
480
500
  ]}
481
501
  ]
482
502
  }`;
483
-
503
+ //#endregion
484
504
  exports.CloudflareVector = CloudflareVector;
485
505
  exports.VECTORIZE_PROMPT = VECTORIZE_PROMPT;
486
- //# sourceMappingURL=index.cjs.map
506
+
487
507
  //# sourceMappingURL=index.cjs.map