@mastra/vectorize 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/CHANGELOG.md +186 -3
- package/LICENSE.md +15 -0
- package/dist/docs/SKILL.md +27 -0
- package/dist/docs/assets/SOURCE_MAP.json +6 -0
- package/dist/docs/references/guides-rag-retrieval.md +520 -0
- package/dist/docs/references/guides-rag-vector-databases.md +669 -0
- package/dist/docs/references/reference-vectors-vectorize.md +149 -0
- package/dist/index.cjs +377 -374
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +351 -366
- package/dist/index.js.map +1 -1
- package/dist/vector/index.d.ts +5 -2
- package/dist/vector/index.d.ts.map +1 -1
- package/package.json +22 -16
package/dist/index.cjs
CHANGED
|
@@ -1,377 +1,380 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
var
|
|
4
|
-
var
|
|
5
|
-
var
|
|
6
|
-
var
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
var
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
array: ["$in", "$nin"],
|
|
19
|
-
element: [],
|
|
20
|
-
regex: [],
|
|
21
|
-
custom: []
|
|
22
|
-
};
|
|
23
|
-
}
|
|
24
|
-
translate(filter) {
|
|
25
|
-
if (this.isEmpty(filter)) return filter;
|
|
26
|
-
this.validateFilter(filter);
|
|
27
|
-
return this.translateNode(filter);
|
|
28
|
-
}
|
|
29
|
-
translateNode(node, currentPath = "") {
|
|
30
|
-
if (this.isRegex(node)) {
|
|
31
|
-
throw new Error("Regex is not supported in Vectorize");
|
|
32
|
-
}
|
|
33
|
-
if (this.isPrimitive(node)) return { $eq: this.normalizeComparisonValue(node) };
|
|
34
|
-
if (Array.isArray(node)) return { $in: this.normalizeArrayValues(node) };
|
|
35
|
-
const entries = Object.entries(node);
|
|
36
|
-
const firstEntry = entries[0];
|
|
37
|
-
if (entries.length === 1 && firstEntry && this.isOperator(firstEntry[0])) {
|
|
38
|
-
const [operator, value] = firstEntry;
|
|
39
|
-
return { [operator]: this.normalizeComparisonValue(value) };
|
|
40
|
-
}
|
|
41
|
-
const result = {};
|
|
42
|
-
for (const [key, value] of entries) {
|
|
43
|
-
const newPath = currentPath ? `${currentPath}.${key}` : key;
|
|
44
|
-
if (this.isOperator(key)) {
|
|
45
|
-
result[key] = this.normalizeComparisonValue(value);
|
|
46
|
-
continue;
|
|
47
|
-
}
|
|
48
|
-
if (typeof value === "object" && value !== null && !Array.isArray(value)) {
|
|
49
|
-
if (Object.keys(value).length === 0) {
|
|
50
|
-
result[newPath] = this.translateNode(value);
|
|
51
|
-
continue;
|
|
52
|
-
}
|
|
53
|
-
const hasOperators = Object.keys(value).some((k) => this.isOperator(k));
|
|
54
|
-
if (hasOperators) {
|
|
55
|
-
result[newPath] = this.translateNode(value);
|
|
56
|
-
} else {
|
|
57
|
-
Object.assign(result, this.translateNode(value, newPath));
|
|
58
|
-
}
|
|
59
|
-
} else {
|
|
60
|
-
result[newPath] = this.translateNode(value);
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
return result;
|
|
64
|
-
}
|
|
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;
|
|
65
18
|
};
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
config: {
|
|
123
|
-
dimensions: dimension,
|
|
124
|
-
metric: metric === "dotproduct" ? "dot-product" : metric
|
|
125
|
-
},
|
|
126
|
-
name: indexName
|
|
127
|
-
});
|
|
128
|
-
} catch (error$1) {
|
|
129
|
-
const message = error$1?.errors?.[0]?.message || error$1?.message;
|
|
130
|
-
if (error$1.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 error.MastraError(
|
|
135
|
-
{
|
|
136
|
-
id: "STORAGE_VECTORIZE_VECTOR_CREATE_INDEX_FAILED",
|
|
137
|
-
domain: error.ErrorDomain.STORAGE,
|
|
138
|
-
category: error.ErrorCategory.THIRD_PARTY,
|
|
139
|
-
details: { indexName, dimension, metric }
|
|
140
|
-
},
|
|
141
|
-
error$1
|
|
142
|
-
);
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
async query({
|
|
146
|
-
indexName,
|
|
147
|
-
queryVector,
|
|
148
|
-
topK = 10,
|
|
149
|
-
filter,
|
|
150
|
-
includeVector = false
|
|
151
|
-
}) {
|
|
152
|
-
try {
|
|
153
|
-
const translatedFilter = this.transformFilter(filter) ?? {};
|
|
154
|
-
const response = await this.client.vectorize.indexes.query(indexName, {
|
|
155
|
-
account_id: this.accountId,
|
|
156
|
-
vector: queryVector,
|
|
157
|
-
returnValues: includeVector,
|
|
158
|
-
returnMetadata: "all",
|
|
159
|
-
topK,
|
|
160
|
-
filter: translatedFilter
|
|
161
|
-
});
|
|
162
|
-
return response?.matches?.map((match) => {
|
|
163
|
-
return {
|
|
164
|
-
id: match.id,
|
|
165
|
-
metadata: match.metadata,
|
|
166
|
-
score: match.score,
|
|
167
|
-
vector: match.values
|
|
168
|
-
};
|
|
169
|
-
}) || [];
|
|
170
|
-
} catch (error$1) {
|
|
171
|
-
throw new error.MastraError(
|
|
172
|
-
{
|
|
173
|
-
id: "STORAGE_VECTORIZE_VECTOR_QUERY_FAILED",
|
|
174
|
-
domain: error.ErrorDomain.STORAGE,
|
|
175
|
-
category: error.ErrorCategory.THIRD_PARTY,
|
|
176
|
-
details: { indexName, topK }
|
|
177
|
-
},
|
|
178
|
-
error$1
|
|
179
|
-
);
|
|
180
|
-
}
|
|
181
|
-
}
|
|
182
|
-
async listIndexes() {
|
|
183
|
-
try {
|
|
184
|
-
const res = await this.client.vectorize.indexes.list({
|
|
185
|
-
account_id: this.accountId
|
|
186
|
-
});
|
|
187
|
-
return res?.result?.map((index) => index.name) || [];
|
|
188
|
-
} catch (error$1) {
|
|
189
|
-
throw new error.MastraError(
|
|
190
|
-
{
|
|
191
|
-
id: "STORAGE_VECTORIZE_VECTOR_LIST_INDEXES_FAILED",
|
|
192
|
-
domain: error.ErrorDomain.STORAGE,
|
|
193
|
-
category: error.ErrorCategory.THIRD_PARTY
|
|
194
|
-
},
|
|
195
|
-
error$1
|
|
196
|
-
);
|
|
197
|
-
}
|
|
198
|
-
}
|
|
199
|
-
/**
|
|
200
|
-
* Retrieves statistics about a vector index.
|
|
201
|
-
*
|
|
202
|
-
* @param {string} indexName - The name of the index to describe
|
|
203
|
-
* @returns A promise that resolves to the index statistics including dimension, count and metric
|
|
204
|
-
*/
|
|
205
|
-
async describeIndex({ indexName }) {
|
|
206
|
-
try {
|
|
207
|
-
const index = await this.client.vectorize.indexes.get(indexName, {
|
|
208
|
-
account_id: this.accountId
|
|
209
|
-
});
|
|
210
|
-
const described = await this.client.vectorize.indexes.info(indexName, {
|
|
211
|
-
account_id: this.accountId
|
|
212
|
-
});
|
|
213
|
-
return {
|
|
214
|
-
dimension: described?.dimensions,
|
|
215
|
-
// Since vector_count is not available in the response,
|
|
216
|
-
// we might need a separate API call to get the count if needed
|
|
217
|
-
count: described?.vectorCount || 0,
|
|
218
|
-
metric: index?.config?.metric
|
|
219
|
-
};
|
|
220
|
-
} catch (error$1) {
|
|
221
|
-
throw new error.MastraError(
|
|
222
|
-
{
|
|
223
|
-
id: "STORAGE_VECTORIZE_VECTOR_DESCRIBE_INDEX_FAILED",
|
|
224
|
-
domain: error.ErrorDomain.STORAGE,
|
|
225
|
-
category: error.ErrorCategory.THIRD_PARTY,
|
|
226
|
-
details: { indexName }
|
|
227
|
-
},
|
|
228
|
-
error$1
|
|
229
|
-
);
|
|
230
|
-
}
|
|
231
|
-
}
|
|
232
|
-
async deleteIndex({ indexName }) {
|
|
233
|
-
try {
|
|
234
|
-
await this.client.vectorize.indexes.delete(indexName, {
|
|
235
|
-
account_id: this.accountId
|
|
236
|
-
});
|
|
237
|
-
} catch (error$1) {
|
|
238
|
-
throw new error.MastraError(
|
|
239
|
-
{
|
|
240
|
-
id: "STORAGE_VECTORIZE_VECTOR_DELETE_INDEX_FAILED",
|
|
241
|
-
domain: error.ErrorDomain.STORAGE,
|
|
242
|
-
category: error.ErrorCategory.THIRD_PARTY,
|
|
243
|
-
details: { indexName }
|
|
244
|
-
},
|
|
245
|
-
error$1
|
|
246
|
-
);
|
|
247
|
-
}
|
|
248
|
-
}
|
|
249
|
-
async createMetadataIndex(indexName, propertyName, indexType) {
|
|
250
|
-
try {
|
|
251
|
-
await this.client.vectorize.indexes.metadataIndex.create(indexName, {
|
|
252
|
-
account_id: this.accountId,
|
|
253
|
-
propertyName,
|
|
254
|
-
indexType
|
|
255
|
-
});
|
|
256
|
-
} catch (error$1) {
|
|
257
|
-
throw new error.MastraError(
|
|
258
|
-
{
|
|
259
|
-
id: "STORAGE_VECTORIZE_VECTOR_CREATE_METADATA_INDEX_FAILED",
|
|
260
|
-
domain: error.ErrorDomain.STORAGE,
|
|
261
|
-
category: error.ErrorCategory.THIRD_PARTY,
|
|
262
|
-
details: { indexName, propertyName, indexType }
|
|
263
|
-
},
|
|
264
|
-
error$1
|
|
265
|
-
);
|
|
266
|
-
}
|
|
267
|
-
}
|
|
268
|
-
async deleteMetadataIndex(indexName, propertyName) {
|
|
269
|
-
try {
|
|
270
|
-
await this.client.vectorize.indexes.metadataIndex.delete(indexName, {
|
|
271
|
-
account_id: this.accountId,
|
|
272
|
-
propertyName
|
|
273
|
-
});
|
|
274
|
-
} catch (error$1) {
|
|
275
|
-
throw new error.MastraError(
|
|
276
|
-
{
|
|
277
|
-
id: "STORAGE_VECTORIZE_VECTOR_DELETE_METADATA_INDEX_FAILED",
|
|
278
|
-
domain: error.ErrorDomain.STORAGE,
|
|
279
|
-
category: error.ErrorCategory.THIRD_PARTY,
|
|
280
|
-
details: { indexName, propertyName }
|
|
281
|
-
},
|
|
282
|
-
error$1
|
|
283
|
-
);
|
|
284
|
-
}
|
|
285
|
-
}
|
|
286
|
-
async listMetadataIndexes(indexName) {
|
|
287
|
-
try {
|
|
288
|
-
const res = await this.client.vectorize.indexes.metadataIndex.list(indexName, {
|
|
289
|
-
account_id: this.accountId
|
|
290
|
-
});
|
|
291
|
-
return res?.metadataIndexes ?? [];
|
|
292
|
-
} catch (error$1) {
|
|
293
|
-
throw new error.MastraError(
|
|
294
|
-
{
|
|
295
|
-
id: "STORAGE_VECTORIZE_VECTOR_LIST_METADATA_INDEXES_FAILED",
|
|
296
|
-
domain: error.ErrorDomain.STORAGE,
|
|
297
|
-
category: error.ErrorCategory.THIRD_PARTY,
|
|
298
|
-
details: { indexName }
|
|
299
|
-
},
|
|
300
|
-
error$1
|
|
301
|
-
);
|
|
302
|
-
}
|
|
303
|
-
}
|
|
304
|
-
/**
|
|
305
|
-
* Updates a vector by its ID with the provided vector and/or metadata.
|
|
306
|
-
* @param indexName - The name of the index containing the vector.
|
|
307
|
-
* @param id - The ID of the vector to update.
|
|
308
|
-
* @param update - An object containing the vector and/or metadata to update.
|
|
309
|
-
* @param update.vector - An optional array of numbers representing the new vector.
|
|
310
|
-
* @param update.metadata - An optional record containing the new metadata.
|
|
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({ indexName, id, update }) {
|
|
315
|
-
if (!update.vector && !update.metadata) {
|
|
316
|
-
throw new error.MastraError({
|
|
317
|
-
id: "STORAGE_VECTORIZE_VECTOR_UPDATE_VECTOR_INVALID_ARGS",
|
|
318
|
-
domain: error.ErrorDomain.STORAGE,
|
|
319
|
-
category: error.ErrorCategory.USER,
|
|
320
|
-
text: "No update data provided",
|
|
321
|
-
details: { indexName, id }
|
|
322
|
-
});
|
|
323
|
-
}
|
|
324
|
-
try {
|
|
325
|
-
const updatePayload = {
|
|
326
|
-
};
|
|
327
|
-
if (update.vector) {
|
|
328
|
-
updatePayload.vectors = [update.vector];
|
|
329
|
-
}
|
|
330
|
-
if (update.metadata) {
|
|
331
|
-
updatePayload.metadata = [update.metadata];
|
|
332
|
-
}
|
|
333
|
-
await this.upsert({ indexName, vectors: updatePayload.vectors, metadata: updatePayload.metadata });
|
|
334
|
-
} catch (error$1) {
|
|
335
|
-
throw new error.MastraError(
|
|
336
|
-
{
|
|
337
|
-
id: "STORAGE_VECTORIZE_VECTOR_UPDATE_VECTOR_FAILED",
|
|
338
|
-
domain: error.ErrorDomain.STORAGE,
|
|
339
|
-
category: error.ErrorCategory.THIRD_PARTY,
|
|
340
|
-
details: { indexName, id }
|
|
341
|
-
},
|
|
342
|
-
error$1
|
|
343
|
-
);
|
|
344
|
-
}
|
|
345
|
-
}
|
|
346
|
-
/**
|
|
347
|
-
* Deletes a vector by its ID.
|
|
348
|
-
* @param indexName - The name of the index containing the vector.
|
|
349
|
-
* @param id - The ID of the vector to delete.
|
|
350
|
-
* @returns A promise that resolves when the deletion is complete.
|
|
351
|
-
* @throws Will throw an error if the deletion operation fails.
|
|
352
|
-
*/
|
|
353
|
-
async deleteVector({ indexName, id }) {
|
|
354
|
-
try {
|
|
355
|
-
await this.client.vectorize.indexes.deleteByIds(indexName, {
|
|
356
|
-
ids: [id],
|
|
357
|
-
account_id: this.accountId
|
|
358
|
-
});
|
|
359
|
-
} catch (error$1) {
|
|
360
|
-
throw new error.MastraError(
|
|
361
|
-
{
|
|
362
|
-
id: "STORAGE_VECTORIZE_VECTOR_DELETE_VECTOR_FAILED",
|
|
363
|
-
domain: error.ErrorDomain.STORAGE,
|
|
364
|
-
category: error.ErrorCategory.THIRD_PARTY,
|
|
365
|
-
details: { indexName, id }
|
|
366
|
-
},
|
|
367
|
-
error$1
|
|
368
|
-
);
|
|
369
|
-
}
|
|
370
|
-
}
|
|
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
|
+
}
|
|
371
75
|
};
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
var
|
|
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
|
+
try {
|
|
309
|
+
const updatePayload = {
|
|
310
|
+
ids: [id],
|
|
311
|
+
account_id: this.accountId
|
|
312
|
+
};
|
|
313
|
+
if (update.vector) updatePayload.vectors = [update.vector];
|
|
314
|
+
if (update.metadata) updatePayload.metadata = [update.metadata];
|
|
315
|
+
await this.upsert({
|
|
316
|
+
indexName,
|
|
317
|
+
vectors: updatePayload.vectors,
|
|
318
|
+
metadata: updatePayload.metadata
|
|
319
|
+
});
|
|
320
|
+
} catch (error) {
|
|
321
|
+
throw new _mastra_core_error.MastraError({
|
|
322
|
+
id: (0, _mastra_core_storage.createVectorErrorId)("VECTORIZE", "UPDATE_VECTOR", "FAILED"),
|
|
323
|
+
domain: _mastra_core_error.ErrorDomain.STORAGE,
|
|
324
|
+
category: _mastra_core_error.ErrorCategory.THIRD_PARTY,
|
|
325
|
+
details: {
|
|
326
|
+
indexName,
|
|
327
|
+
...id && { id }
|
|
328
|
+
}
|
|
329
|
+
}, error);
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
/**
|
|
333
|
+
* Deletes a vector by its ID.
|
|
334
|
+
* @param indexName - The name of the index containing the vector.
|
|
335
|
+
* @param id - The ID of the vector to delete.
|
|
336
|
+
* @returns A promise that resolves when the deletion is complete.
|
|
337
|
+
* @throws Will throw an error if the deletion operation fails.
|
|
338
|
+
*/
|
|
339
|
+
async deleteVector({ indexName, id }) {
|
|
340
|
+
try {
|
|
341
|
+
await this.client.vectorize.indexes.deleteByIds(indexName, {
|
|
342
|
+
ids: [id],
|
|
343
|
+
account_id: this.accountId
|
|
344
|
+
});
|
|
345
|
+
} catch (error) {
|
|
346
|
+
throw new _mastra_core_error.MastraError({
|
|
347
|
+
id: (0, _mastra_core_storage.createVectorErrorId)("VECTORIZE", "DELETE_VECTOR", "FAILED"),
|
|
348
|
+
domain: _mastra_core_error.ErrorDomain.STORAGE,
|
|
349
|
+
category: _mastra_core_error.ErrorCategory.THIRD_PARTY,
|
|
350
|
+
details: {
|
|
351
|
+
indexName,
|
|
352
|
+
...id && { id }
|
|
353
|
+
}
|
|
354
|
+
}, error);
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
async deleteVectors({ indexName, filter, ids }) {
|
|
358
|
+
throw new _mastra_core_error.MastraError({
|
|
359
|
+
id: (0, _mastra_core_storage.createVectorErrorId)("VECTORIZE", "DELETE_VECTORS", "NOT_SUPPORTED"),
|
|
360
|
+
text: "deleteVectors is not yet implemented for Vectorize vector store",
|
|
361
|
+
domain: _mastra_core_error.ErrorDomain.STORAGE,
|
|
362
|
+
category: _mastra_core_error.ErrorCategory.SYSTEM,
|
|
363
|
+
details: {
|
|
364
|
+
indexName,
|
|
365
|
+
...filter && { filter: JSON.stringify(filter) },
|
|
366
|
+
...ids && { idsCount: ids.length }
|
|
367
|
+
}
|
|
368
|
+
});
|
|
369
|
+
}
|
|
370
|
+
};
|
|
371
|
+
//#endregion
|
|
372
|
+
//#region src/vector/prompt.ts
|
|
373
|
+
/**
|
|
374
|
+
* Vector store specific prompt that details supported operators and examples.
|
|
375
|
+
* This prompt helps users construct valid filters for Vectorize.
|
|
376
|
+
*/
|
|
377
|
+
const VECTORIZE_PROMPT = `When querying Vectorize, you can ONLY use the operators listed below. Any other operators will be rejected.
|
|
375
378
|
Important: Don't explain how to construct the filter - use the specified operators and fields to search the content and return relevant results.
|
|
376
379
|
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.
|
|
377
380
|
|
|
@@ -447,8 +450,8 @@ Example Complex Query:
|
|
|
447
450
|
]}
|
|
448
451
|
]
|
|
449
452
|
}`;
|
|
450
|
-
|
|
453
|
+
//#endregion
|
|
451
454
|
exports.CloudflareVector = CloudflareVector;
|
|
452
455
|
exports.VECTORIZE_PROMPT = VECTORIZE_PROMPT;
|
|
453
|
-
|
|
456
|
+
|
|
454
457
|
//# sourceMappingURL=index.cjs.map
|