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