@mastra/vectorize 1.1.0 → 1.1.1-alpha.0
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 +9 -0
- package/dist/docs/SKILL.md +3 -6
- package/dist/docs/assets/SOURCE_MAP.json +1 -1
- package/dist/docs/references/{docs-rag-retrieval.md → reference-rag-retrieval.md} +38 -19
- package/dist/docs/references/{docs-rag-vector-databases.md → reference-rag-vector-databases.md} +70 -5
- package/dist/docs/references/reference-vectors-vectorize.md +14 -2
- package/dist/index.cjs +427 -407
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +401 -399
- package/dist/index.js.map +1 -1
- package/dist/vector/index.d.ts +9 -0
- package/dist/vector/index.d.ts.map +1 -1
- package/package.json +15 -14
package/dist/index.js
CHANGED
|
@@ -1,404 +1,406 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { createVectorErrorId } from
|
|
3
|
-
import { MastraVector } from
|
|
4
|
-
import Cloudflare from
|
|
5
|
-
import { BaseFilterTranslator } from
|
|
6
|
-
|
|
7
|
-
// 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 Cloudflare from "cloudflare";
|
|
5
|
+
import { BaseFilterTranslator } from "@mastra/core/vector/filter";
|
|
6
|
+
//#region src/vector/filter.ts
|
|
8
7
|
var VectorizeFilterTranslator = class extends BaseFilterTranslator {
|
|
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
|
-
|
|
52
|
-
Object.assign(result, this.translateNode(value, newPath));
|
|
53
|
-
}
|
|
54
|
-
} else {
|
|
55
|
-
result[newPath] = this.translateNode(value);
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
return result;
|
|
59
|
-
}
|
|
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
|
+
}
|
|
60
51
|
};
|
|
61
|
-
|
|
62
|
-
|
|
52
|
+
//#endregion
|
|
53
|
+
//#region src/vector/index.ts
|
|
63
54
|
var CloudflareVector = class extends MastraVector {
|
|
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
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
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
|
+
if (!update.vector) throw new MastraError({
|
|
285
|
+
id: createVectorErrorId("VECTORIZE", "UPDATE_VECTOR", "MISSING_VECTOR"),
|
|
286
|
+
domain: ErrorDomain.STORAGE,
|
|
287
|
+
category: ErrorCategory.USER,
|
|
288
|
+
text: "Vectorize requires vector values when updating; metadata-only updates are not supported. Provide update.vector alongside update.metadata.",
|
|
289
|
+
details: {
|
|
290
|
+
indexName,
|
|
291
|
+
id
|
|
292
|
+
}
|
|
293
|
+
});
|
|
294
|
+
try {
|
|
295
|
+
await this.upsert({
|
|
296
|
+
indexName,
|
|
297
|
+
ids: [id],
|
|
298
|
+
vectors: [update.vector],
|
|
299
|
+
...update.metadata && { metadata: [update.metadata] }
|
|
300
|
+
});
|
|
301
|
+
} catch (error) {
|
|
302
|
+
throw new MastraError({
|
|
303
|
+
id: createVectorErrorId("VECTORIZE", "UPDATE_VECTOR", "FAILED"),
|
|
304
|
+
domain: ErrorDomain.STORAGE,
|
|
305
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
306
|
+
details: {
|
|
307
|
+
indexName,
|
|
308
|
+
...id && { id }
|
|
309
|
+
}
|
|
310
|
+
}, error);
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
/**
|
|
314
|
+
* Deletes a vector by its ID.
|
|
315
|
+
* @param indexName - The name of the index containing the vector.
|
|
316
|
+
* @param id - The ID of the vector to delete.
|
|
317
|
+
* @returns A promise that resolves when the deletion is complete.
|
|
318
|
+
* @throws Will throw an error if the deletion operation fails.
|
|
319
|
+
*/
|
|
320
|
+
async deleteVector({ indexName, id }) {
|
|
321
|
+
try {
|
|
322
|
+
await this.client.vectorize.indexes.deleteByIds(indexName, {
|
|
323
|
+
ids: [id],
|
|
324
|
+
account_id: this.accountId
|
|
325
|
+
});
|
|
326
|
+
} catch (error) {
|
|
327
|
+
throw new MastraError({
|
|
328
|
+
id: createVectorErrorId("VECTORIZE", "DELETE_VECTOR", "FAILED"),
|
|
329
|
+
domain: ErrorDomain.STORAGE,
|
|
330
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
331
|
+
details: {
|
|
332
|
+
indexName,
|
|
333
|
+
...id && { id }
|
|
334
|
+
}
|
|
335
|
+
}, error);
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
/**
|
|
339
|
+
* Deletes multiple vectors by their IDs.
|
|
340
|
+
*
|
|
341
|
+
* Vectorize has no filtered-delete primitive, so metadata-filter deletion is rejected.
|
|
342
|
+
*
|
|
343
|
+
* @param indexName - The name of the index containing the vectors.
|
|
344
|
+
* @param ids - The IDs of the vectors to delete. Mutually exclusive with `filter`.
|
|
345
|
+
* @throws Will throw an error if the arguments are invalid or the deletion operation fails.
|
|
346
|
+
*/
|
|
347
|
+
async deleteVectors({ indexName, filter, ids }) {
|
|
348
|
+
if (ids && filter) throw new MastraError({
|
|
349
|
+
id: createVectorErrorId("VECTORIZE", "DELETE_VECTORS", "MUTUALLY_EXCLUSIVE"),
|
|
350
|
+
text: "Cannot specify both ids and filter - they are mutually exclusive",
|
|
351
|
+
domain: ErrorDomain.STORAGE,
|
|
352
|
+
category: ErrorCategory.USER,
|
|
353
|
+
details: { indexName }
|
|
354
|
+
});
|
|
355
|
+
if (filter) throw new MastraError({
|
|
356
|
+
id: createVectorErrorId("VECTORIZE", "DELETE_VECTORS", "UNSUPPORTED_FILTER"),
|
|
357
|
+
text: "Deleting by metadata filter is not supported for Vectorize vector store - delete by ids instead",
|
|
358
|
+
domain: ErrorDomain.STORAGE,
|
|
359
|
+
category: ErrorCategory.SYSTEM,
|
|
360
|
+
details: {
|
|
361
|
+
indexName,
|
|
362
|
+
filter: JSON.stringify(filter)
|
|
363
|
+
}
|
|
364
|
+
});
|
|
365
|
+
if (!ids) throw new MastraError({
|
|
366
|
+
id: createVectorErrorId("VECTORIZE", "DELETE_VECTORS", "NO_TARGET"),
|
|
367
|
+
text: "Either filter or ids must be provided",
|
|
368
|
+
domain: ErrorDomain.STORAGE,
|
|
369
|
+
category: ErrorCategory.USER,
|
|
370
|
+
details: { indexName }
|
|
371
|
+
});
|
|
372
|
+
if (ids.length === 0) throw new MastraError({
|
|
373
|
+
id: createVectorErrorId("VECTORIZE", "DELETE_VECTORS", "EMPTY_IDS"),
|
|
374
|
+
text: "Cannot delete with empty ids array",
|
|
375
|
+
domain: ErrorDomain.STORAGE,
|
|
376
|
+
category: ErrorCategory.USER,
|
|
377
|
+
details: { indexName }
|
|
378
|
+
});
|
|
379
|
+
try {
|
|
380
|
+
await this.client.vectorize.indexes.deleteByIds(indexName, {
|
|
381
|
+
ids,
|
|
382
|
+
account_id: this.accountId
|
|
383
|
+
});
|
|
384
|
+
} catch (error) {
|
|
385
|
+
throw new MastraError({
|
|
386
|
+
id: createVectorErrorId("VECTORIZE", "DELETE_VECTORS", "FAILED"),
|
|
387
|
+
domain: ErrorDomain.STORAGE,
|
|
388
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
389
|
+
details: {
|
|
390
|
+
indexName,
|
|
391
|
+
idsCount: ids.length
|
|
392
|
+
}
|
|
393
|
+
}, error);
|
|
394
|
+
}
|
|
395
|
+
}
|
|
398
396
|
};
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
397
|
+
//#endregion
|
|
398
|
+
//#region src/vector/prompt.ts
|
|
399
|
+
/**
|
|
400
|
+
* Vector store specific prompt that details supported operators and examples.
|
|
401
|
+
* This prompt helps users construct valid filters for Vectorize.
|
|
402
|
+
*/
|
|
403
|
+
const VECTORIZE_PROMPT = `When querying Vectorize, you can ONLY use the operators listed below. Any other operators will be rejected.
|
|
402
404
|
Important: Don't explain how to construct the filter - use the specified operators and fields to search the content and return relevant results.
|
|
403
405
|
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.
|
|
404
406
|
|
|
@@ -474,7 +476,7 @@ Example Complex Query:
|
|
|
474
476
|
]}
|
|
475
477
|
]
|
|
476
478
|
}`;
|
|
477
|
-
|
|
479
|
+
//#endregion
|
|
478
480
|
export { CloudflareVector, VECTORIZE_PROMPT };
|
|
479
|
-
|
|
481
|
+
|
|
480
482
|
//# sourceMappingURL=index.js.map
|