@mastra/vectorize 0.0.0-a2a-20250421213654 → 0.0.0-add-libsql-changeset-20250910154739
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 +2196 -0
- package/LICENSE.md +12 -4
- package/dist/index.cjs +339 -118
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.ts +3 -1
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +337 -117
- package/dist/index.js.map +1 -0
- package/dist/vector/filter.d.ts +13 -0
- package/dist/vector/filter.d.ts.map +1 -0
- package/dist/vector/index.d.ts +51 -0
- package/dist/vector/index.d.ts.map +1 -0
- package/dist/vector/prompt.d.ts +6 -0
- package/dist/vector/prompt.d.ts.map +1 -0
- package/package.json +30 -16
- package/dist/_tsup-dts-rollup.d.cts +0 -49
- package/dist/_tsup-dts-rollup.d.ts +0 -49
- package/dist/index.d.cts +0 -1
package/LICENSE.md
CHANGED
|
@@ -1,7 +1,15 @@
|
|
|
1
|
-
|
|
1
|
+
# Apache License 2.0
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Copyright (c) 2025 Kepler Software, Inc.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
you may not use this file except in compliance with the License.
|
|
7
|
+
You may obtain a copy of the License at
|
|
6
8
|
|
|
7
|
-
|
|
9
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
|
|
11
|
+
Unless required by applicable law or agreed to in writing, software
|
|
12
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
See the License for the specific language governing permissions and
|
|
15
|
+
limitations under the License.
|
package/dist/index.cjs
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
+
var error = require('@mastra/core/error');
|
|
3
4
|
var vector = require('@mastra/core/vector');
|
|
4
5
|
var Cloudflare = require('cloudflare');
|
|
5
6
|
var filter = require('@mastra/core/vector/filter');
|
|
@@ -74,9 +75,10 @@ var CloudflareVector = class extends vector.MastraVector {
|
|
|
74
75
|
apiToken
|
|
75
76
|
});
|
|
76
77
|
}
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
78
|
+
get indexSeparator() {
|
|
79
|
+
return "-";
|
|
80
|
+
}
|
|
81
|
+
async upsert({ indexName, vectors, metadata, ids }) {
|
|
80
82
|
const generatedIds = ids || vectors.map(() => crypto.randomUUID());
|
|
81
83
|
const ndjson = vectors.map(
|
|
82
84
|
(vector, index) => JSON.stringify({
|
|
@@ -85,149 +87,368 @@ var CloudflareVector = class extends vector.MastraVector {
|
|
|
85
87
|
metadata: metadata?.[index]
|
|
86
88
|
})
|
|
87
89
|
).join("\n");
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
90
|
+
try {
|
|
91
|
+
await this.client.vectorize.indexes.upsert(
|
|
92
|
+
indexName,
|
|
93
|
+
{
|
|
94
|
+
account_id: this.accountId,
|
|
95
|
+
body: ndjson
|
|
96
|
+
},
|
|
97
|
+
{
|
|
98
|
+
__binaryRequest: true
|
|
99
|
+
}
|
|
100
|
+
);
|
|
101
|
+
return generatedIds;
|
|
102
|
+
} catch (error$1) {
|
|
103
|
+
throw new error.MastraError(
|
|
104
|
+
{
|
|
105
|
+
id: "STORAGE_VECTORIZE_VECTOR_UPSERT_FAILED",
|
|
106
|
+
domain: error.ErrorDomain.STORAGE,
|
|
107
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
108
|
+
details: { indexName, vectorCount: vectors?.length }
|
|
109
|
+
},
|
|
110
|
+
error$1
|
|
111
|
+
);
|
|
112
|
+
}
|
|
99
113
|
}
|
|
100
114
|
transformFilter(filter) {
|
|
101
115
|
const translator = new VectorizeFilterTranslator();
|
|
102
116
|
return translator.translate(filter);
|
|
103
117
|
}
|
|
104
|
-
async
|
|
118
|
+
async createIndex({ indexName, dimension, metric = "cosine" }) {
|
|
105
119
|
try {
|
|
106
|
-
|
|
107
|
-
account_id: this.accountId
|
|
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
|
|
108
127
|
});
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
`Index "${indexName}" already exists with ${info.dimensions} dimensions, but ${dimension} dimensions were requested`
|
|
115
|
-
);
|
|
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;
|
|
116
133
|
}
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
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
|
+
);
|
|
124
143
|
}
|
|
125
144
|
}
|
|
126
|
-
async
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
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
|
|
133
179
|
);
|
|
134
|
-
return;
|
|
135
180
|
}
|
|
136
|
-
await this.client.vectorize.indexes.create({
|
|
137
|
-
account_id: this.accountId,
|
|
138
|
-
config: {
|
|
139
|
-
dimensions: dimension,
|
|
140
|
-
metric: metric === "dotproduct" ? "dot-product" : metric
|
|
141
|
-
},
|
|
142
|
-
name: indexName
|
|
143
|
-
});
|
|
144
|
-
}
|
|
145
|
-
async query(...args) {
|
|
146
|
-
const params = this.normalizeArgs("query", args);
|
|
147
|
-
const { indexName, queryVector, topK = 10, filter, includeVector = false } = params;
|
|
148
|
-
const translatedFilter = this.transformFilter(filter) ?? {};
|
|
149
|
-
const response = await this.client.vectorize.indexes.query(indexName, {
|
|
150
|
-
account_id: this.accountId,
|
|
151
|
-
vector: queryVector,
|
|
152
|
-
returnValues: includeVector,
|
|
153
|
-
returnMetadata: "all",
|
|
154
|
-
topK,
|
|
155
|
-
filter: translatedFilter
|
|
156
|
-
});
|
|
157
|
-
return response?.matches?.map((match) => {
|
|
158
|
-
return {
|
|
159
|
-
id: match.id,
|
|
160
|
-
metadata: match.metadata,
|
|
161
|
-
score: match.score,
|
|
162
|
-
vector: match.values
|
|
163
|
-
};
|
|
164
|
-
}) || [];
|
|
165
181
|
}
|
|
166
182
|
async listIndexes() {
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
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
|
+
}
|
|
171
198
|
}
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
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
|
+
}
|
|
186
231
|
}
|
|
187
|
-
async deleteIndex(indexName) {
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
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
|
+
}
|
|
191
248
|
}
|
|
192
249
|
async createMetadataIndex(indexName, propertyName, indexType) {
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
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
|
+
}
|
|
198
267
|
}
|
|
199
268
|
async deleteMetadataIndex(indexName, propertyName) {
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
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
|
+
}
|
|
204
285
|
}
|
|
205
286
|
async listMetadataIndexes(indexName) {
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
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
|
+
}
|
|
210
303
|
}
|
|
211
|
-
|
|
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 }) {
|
|
212
315
|
if (!update.vector && !update.metadata) {
|
|
213
|
-
throw new
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
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
|
+
});
|
|
219
323
|
}
|
|
220
|
-
|
|
221
|
-
updatePayload
|
|
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
|
+
);
|
|
222
344
|
}
|
|
223
|
-
await this.upsert({ indexName, vectors: updatePayload.vectors, metadata: updatePayload.metadata });
|
|
224
345
|
}
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
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
|
+
}
|
|
230
370
|
}
|
|
231
371
|
};
|
|
232
372
|
|
|
373
|
+
// src/vector/prompt.ts
|
|
374
|
+
var VECTORIZE_PROMPT = `When querying Vectorize, you can ONLY use the operators listed below. Any other operators will be rejected.
|
|
375
|
+
Important: Don't explain how to construct the filter - use the specified operators and fields to search the content and return relevant results.
|
|
376
|
+
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
|
+
|
|
378
|
+
Basic Comparison Operators:
|
|
379
|
+
- $eq: Exact match (default when using field: value)
|
|
380
|
+
Example: { "category": "electronics" }
|
|
381
|
+
- $ne: Not equal
|
|
382
|
+
Example: { "category": { "$ne": "electronics" } }
|
|
383
|
+
- $gt: Greater than
|
|
384
|
+
Example: { "price": { "$gt": 100 } }
|
|
385
|
+
- $gte: Greater than or equal
|
|
386
|
+
Example: { "price": { "$gte": 100 } }
|
|
387
|
+
- $lt: Less than
|
|
388
|
+
Example: { "price": { "$lt": 100 } }
|
|
389
|
+
- $lte: Less than or equal
|
|
390
|
+
Example: { "price": { "$lte": 100 } }
|
|
391
|
+
|
|
392
|
+
Array Operators:
|
|
393
|
+
- $in: Match any value in array
|
|
394
|
+
Example: { "category": { "$in": ["electronics", "books"] } }
|
|
395
|
+
- $nin: Does not match any value in array
|
|
396
|
+
Example: { "category": { "$nin": ["electronics", "books"] } }
|
|
397
|
+
|
|
398
|
+
Logical Operators:
|
|
399
|
+
- $and: Logical AND (can be implicit or explicit)
|
|
400
|
+
Implicit Example: { "price": { "$gt": 100 }, "category": "electronics" }
|
|
401
|
+
Explicit Example: { "$and": [{ "price": { "$gt": 100 } }, { "category": "electronics" }] }
|
|
402
|
+
- $or: Logical OR
|
|
403
|
+
Example: { "$or": [{ "price": { "$lt": 50 } }, { "category": "books" }] }
|
|
404
|
+
|
|
405
|
+
Element Operators:
|
|
406
|
+
- $exists: Check if field exists
|
|
407
|
+
Example: { "rating": { "$exists": true } }
|
|
408
|
+
- $match: Match text using full-text search
|
|
409
|
+
Example: { "description": { "$match": "gaming laptop" } }
|
|
410
|
+
|
|
411
|
+
Restrictions:
|
|
412
|
+
- Regex patterns are not supported
|
|
413
|
+
- Only $and and $or logical operators are supported at the top level
|
|
414
|
+
- Empty arrays in $in/$nin will return no results
|
|
415
|
+
- Nested fields are supported using dot notation
|
|
416
|
+
- Multiple conditions on the same field are supported with both implicit and explicit $and
|
|
417
|
+
- At least one key-value pair is required in filter object
|
|
418
|
+
- Empty objects and undefined values are treated as no filter
|
|
419
|
+
- Invalid types in comparison operators will throw errors
|
|
420
|
+
- All non-logical operators must be used within a field condition
|
|
421
|
+
Valid: { "field": { "$gt": 100 } }
|
|
422
|
+
Valid: { "$and": [...] }
|
|
423
|
+
Invalid: { "$gt": 100 }
|
|
424
|
+
- Logical operators must contain field conditions, not direct operators
|
|
425
|
+
Valid: { "$and": [{ "field": { "$gt": 100 } }] }
|
|
426
|
+
Invalid: { "$and": [{ "$gt": 100 }] }
|
|
427
|
+
- Logical operators ($and, $or):
|
|
428
|
+
- Can only be used at top level or nested within other logical operators
|
|
429
|
+
- Can not be used on a field level, or be nested inside a field
|
|
430
|
+
- Can not be used inside an operator
|
|
431
|
+
- Valid: { "$and": [{ "field": { "$gt": 100 } }] }
|
|
432
|
+
- Valid: { "$or": [{ "$and": [{ "field": { "$gt": 100 } }] }] }
|
|
433
|
+
- Invalid: { "field": { "$and": [{ "$gt": 100 }] } }
|
|
434
|
+
- Invalid: { "field": { "$or": [{ "$gt": 100 }] } }
|
|
435
|
+
- Invalid: { "field": { "$gt": { "$and": [{...}] } } }
|
|
436
|
+
|
|
437
|
+
Example Complex Query:
|
|
438
|
+
{
|
|
439
|
+
"$and": [
|
|
440
|
+
{ "category": { "$in": ["electronics", "computers"] } },
|
|
441
|
+
{ "price": { "$gte": 100, "$lte": 1000 } },
|
|
442
|
+
{ "description": { "$match": "gaming laptop" } },
|
|
443
|
+
{ "rating": { "$exists": true, "$gt": 4 } },
|
|
444
|
+
{ "$or": [
|
|
445
|
+
{ "stock": { "$gt": 0 } },
|
|
446
|
+
{ "preorder": true }
|
|
447
|
+
]}
|
|
448
|
+
]
|
|
449
|
+
}`;
|
|
450
|
+
|
|
233
451
|
exports.CloudflareVector = CloudflareVector;
|
|
452
|
+
exports.VECTORIZE_PROMPT = VECTORIZE_PROMPT;
|
|
453
|
+
//# sourceMappingURL=index.cjs.map
|
|
454
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/vector/filter.ts","../src/vector/index.ts","../src/vector/prompt.ts"],"names":["BaseFilterTranslator","MastraVector","Cloudflare","error","MastraError","ErrorDomain","ErrorCategory"],"mappings":";;;;;;;;;;;;AAsBO,IAAM,yBAAA,GAAN,cAAwCA,2BAAA,CAA4C;AAAA,EACtE,qBAAA,GAAyC;AAC1D,IAAA,OAAO;AAAA,MACL,GAAGA,2BAAA,CAAqB,iBAAA;AAAA,MACxB,SAAS,EAAC;AAAA,MACV,KAAA,EAAO,CAAC,KAAA,EAAO,MAAM,CAAA;AAAA,MACrB,SAAS,EAAC;AAAA,MACV,OAAO,EAAC;AAAA,MACR,QAAQ;AAAC,KACX;AAAA,EACF;AAAA,EAEA,UAAU,MAAA,EAAuD;AAC/D,IAAA,IAAI,IAAA,CAAK,OAAA,CAAQ,MAAM,CAAA,EAAG,OAAO,MAAA;AACjC,IAAA,IAAA,CAAK,eAAe,MAAM,CAAA;AAC1B,IAAA,OAAO,IAAA,CAAK,cAAc,MAAM,CAAA;AAAA,EAClC;AAAA,EAEQ,aAAA,CAAc,IAAA,EAA6B,WAAA,GAAsB,EAAA,EAAS;AAChF,IAAA,IAAI,IAAA,CAAK,OAAA,CAAQ,IAAI,CAAA,EAAG;AACtB,MAAA,MAAM,IAAI,MAAM,qCAAqC,CAAA;AAAA,IACvD;AACA,IAAA,IAAI,IAAA,CAAK,WAAA,CAAY,IAAI,CAAA,EAAG,OAAO,EAAE,GAAA,EAAK,IAAA,CAAK,wBAAA,CAAyB,IAAI,CAAA,EAAE;AAC9E,IAAA,IAAI,KAAA,CAAM,OAAA,CAAQ,IAAI,CAAA,EAAG,OAAO,EAAE,GAAA,EAAK,IAAA,CAAK,oBAAA,CAAqB,IAAI,CAAA,EAAE;AAEvE,IAAA,MAAM,OAAA,GAAU,MAAA,CAAO,OAAA,CAAQ,IAA2B,CAAA;AAC1D,IAAA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CAAa,EAAE,SAAA,EAAW,IAAG,EAAsC;AACvE,IAAA,IAAI;AACF,MAAA,MAAM,IAAA,CAAK,MAAA,CAAO,SAAA,CAAU,OAAA,CAAQ,YAAY,SAAA,EAAW;AAAA,QACzD,GAAA,EAAK,CAAC,EAAE,CAAA;AAAA,QACR,YAAY,IAAA,CAAK;AAAA,OAClB,CAAA;AAAA,IACH,SAASA,OAAA,EAAO;AACd,MAAA,MAAM,IAAIC,iBAAA;AAAA,QACR;AAAA,UACE,EAAA,EAAI,+CAAA;AAAA,UACJ,QAAQC,iBAAA,CAAY,OAAA;AAAA,UACpB,UAAUC,mBAAA,CAAc,WAAA;AAAA,UACxB,OAAA,EAAS,EAAE,SAAA,EAAW,EAAA;AAAG,SAC3B;AAAA,QACAH;AAAA,OACF;AAAA,IACF;AAAA,EACF;AACF;;;ACxWO,IAAM,gBAAA,GAAmB,CAAA;AAAA;AAAA;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;AAAA;AAAA;AAAA;AAAA;AAAA;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;AAAA;AAAA;AAAA;AAAA;AAAA;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,CAAA","file":"index.cjs","sourcesContent":["import { BaseFilterTranslator } from '@mastra/core/vector/filter';\nimport type {\n VectorFilter,\n OperatorSupport,\n OperatorValueMap,\n LogicalOperatorValueMap,\n BlacklistedRootOperators,\n} from '@mastra/core/vector/filter';\n\ntype VectorizeOperatorValueMap = Omit<OperatorValueMap, '$regex' | '$options' | '$exists' | '$elemMatch' | '$all'>;\n\ntype VectorizeLogicalOperatorValueMap = Omit<LogicalOperatorValueMap, '$nor' | '$not' | '$and' | '$or'>;\n\ntype VectorizeBlacklistedRootOperators = BlacklistedRootOperators | '$nor' | '$not' | '$and' | '$or';\n\nexport type VectorizeVectorFilter = VectorFilter<\n keyof VectorizeOperatorValueMap,\n VectorizeOperatorValueMap,\n VectorizeLogicalOperatorValueMap,\n VectorizeBlacklistedRootOperators\n>;\n\nexport class VectorizeFilterTranslator extends BaseFilterTranslator<VectorizeVectorFilter> {\n protected override getSupportedOperators(): OperatorSupport {\n return {\n ...BaseFilterTranslator.DEFAULT_OPERATORS,\n logical: [],\n array: ['$in', '$nin'],\n element: [],\n regex: [],\n custom: [],\n };\n }\n\n translate(filter?: VectorizeVectorFilter): VectorizeVectorFilter {\n if (this.isEmpty(filter)) return filter;\n this.validateFilter(filter);\n return this.translateNode(filter);\n }\n\n private translateNode(node: VectorizeVectorFilter, currentPath: string = ''): any {\n if (this.isRegex(node)) {\n throw new Error('Regex is not supported in Vectorize');\n }\n if (this.isPrimitive(node)) return { $eq: this.normalizeComparisonValue(node) };\n if (Array.isArray(node)) return { $in: this.normalizeArrayValues(node) };\n\n const entries = Object.entries(node as Record<string, any>);\n const firstEntry = entries[0];\n\n // Handle single operator case\n if (entries.length === 1 && firstEntry && this.isOperator(firstEntry[0])) {\n const [operator, value] = firstEntry;\n return { [operator]: this.normalizeComparisonValue(value) };\n }\n\n // Process each entry\n const result: Record<string, any> = {};\n for (const [key, value] of entries) {\n const newPath = currentPath ? `${currentPath}.${key}` : key;\n\n if (this.isOperator(key)) {\n result[key] = this.normalizeComparisonValue(value);\n continue;\n }\n\n if (typeof value === 'object' && value !== null && !Array.isArray(value)) {\n if (Object.keys(value).length === 0) {\n result[newPath] = this.translateNode(value);\n continue;\n }\n\n // Check if the nested object contains operators\n const hasOperators = Object.keys(value).some(k => this.isOperator(k));\n if (hasOperators) {\n result[newPath] = this.translateNode(value);\n } else {\n // For objects without operators, flatten them\n Object.assign(result, this.translateNode(value, newPath));\n }\n } else {\n result[newPath] = this.translateNode(value);\n }\n }\n\n return result;\n }\n}\n","import { MastraError, ErrorDomain, ErrorCategory } from '@mastra/core/error';\nimport { MastraVector } from '@mastra/core/vector';\nimport type {\n QueryResult,\n CreateIndexParams,\n UpsertVectorParams,\n QueryVectorParams,\n DescribeIndexParams,\n DeleteIndexParams,\n DeleteVectorParams,\n UpdateVectorParams,\n IndexStats,\n} from '@mastra/core/vector';\nimport Cloudflare from 'cloudflare';\n\nimport { VectorizeFilterTranslator } from './filter';\nimport type { VectorizeVectorFilter } from './filter';\n\ntype VectorizeQueryParams = QueryVectorParams<VectorizeVectorFilter>;\n\nexport class CloudflareVector extends MastraVector<VectorizeVectorFilter> {\n client: Cloudflare;\n accountId: string;\n\n constructor({ accountId, apiToken }: { accountId: string; apiToken: string }) {\n super();\n this.accountId = accountId;\n\n this.client = new Cloudflare({\n apiToken: apiToken,\n });\n }\n\n get indexSeparator(): string {\n return '-';\n }\n\n async upsert({ indexName, vectors, metadata, ids }: UpsertVectorParams): Promise<string[]> {\n const generatedIds = ids || vectors.map(() => crypto.randomUUID());\n\n // Create NDJSON string - each line is a JSON object\n const ndjson = vectors\n .map((vector, index) =>\n JSON.stringify({\n id: generatedIds[index]!,\n values: vector,\n metadata: metadata?.[index],\n }),\n )\n .join('\\n');\n\n try {\n // Note: __binaryRequest is required for proper NDJSON handling\n await this.client.vectorize.indexes.upsert(\n indexName,\n {\n account_id: this.accountId,\n body: ndjson,\n },\n {\n __binaryRequest: true,\n },\n );\n\n return generatedIds;\n } catch (error) {\n throw new MastraError(\n {\n id: 'STORAGE_VECTORIZE_VECTOR_UPSERT_FAILED',\n domain: ErrorDomain.STORAGE,\n category: ErrorCategory.THIRD_PARTY,\n details: { indexName, vectorCount: vectors?.length },\n },\n error,\n );\n }\n }\n\n transformFilter(filter?: VectorizeVectorFilter) {\n const translator = new VectorizeFilterTranslator();\n return translator.translate(filter);\n }\n\n async createIndex({ indexName, dimension, metric = 'cosine' }: CreateIndexParams): Promise<void> {\n try {\n await this.client.vectorize.indexes.create({\n account_id: this.accountId,\n config: {\n dimensions: dimension,\n metric: metric === 'dotproduct' ? 'dot-product' : metric,\n },\n name: indexName,\n });\n } catch (error: any) {\n // Check for 'already exists' error\n const message = error?.errors?.[0]?.message || error?.message;\n if (\n error.status === 409 ||\n (typeof message === 'string' &&\n (message.toLowerCase().includes('already exists') || message.toLowerCase().includes('duplicate')))\n ) {\n // Fetch index info and check dimensions\n await this.validateExistingIndex(indexName, dimension, metric);\n return;\n }\n // For any other errors, propagate\n throw new MastraError(\n {\n id: 'STORAGE_VECTORIZE_VECTOR_CREATE_INDEX_FAILED',\n domain: ErrorDomain.STORAGE,\n category: ErrorCategory.THIRD_PARTY,\n details: { indexName, dimension, metric },\n },\n error,\n );\n }\n }\n\n async query({\n indexName,\n queryVector,\n topK = 10,\n filter,\n includeVector = false,\n }: VectorizeQueryParams): Promise<QueryResult[]> {\n try {\n const translatedFilter = this.transformFilter(filter) ?? {};\n const response = await this.client.vectorize.indexes.query(indexName, {\n account_id: this.accountId,\n vector: queryVector,\n returnValues: includeVector,\n returnMetadata: 'all',\n topK,\n filter: translatedFilter,\n });\n\n return (\n response?.matches?.map((match: any) => {\n return {\n id: match.id,\n metadata: match.metadata,\n score: match.score,\n vector: match.values,\n };\n }) || []\n );\n } catch (error) {\n throw new MastraError(\n {\n id: 'STORAGE_VECTORIZE_VECTOR_QUERY_FAILED',\n domain: ErrorDomain.STORAGE,\n category: ErrorCategory.THIRD_PARTY,\n details: { indexName, topK },\n },\n error,\n );\n }\n }\n\n async listIndexes(): Promise<string[]> {\n try {\n const res = await this.client.vectorize.indexes.list({\n account_id: this.accountId,\n });\n\n return res?.result?.map(index => index.name!) || [];\n } catch (error) {\n throw new MastraError(\n {\n id: 'STORAGE_VECTORIZE_VECTOR_LIST_INDEXES_FAILED',\n domain: ErrorDomain.STORAGE,\n category: ErrorCategory.THIRD_PARTY,\n },\n error,\n );\n }\n }\n\n /**\n * Retrieves statistics about a vector index.\n *\n * @param {string} indexName - The name of the index to describe\n * @returns A promise that resolves to the index statistics including dimension, count and metric\n */\n async describeIndex({ indexName }: DescribeIndexParams): Promise<IndexStats> {\n try {\n const index = await this.client.vectorize.indexes.get(indexName, {\n account_id: this.accountId,\n });\n\n const described = await this.client.vectorize.indexes.info(indexName, {\n account_id: this.accountId,\n });\n\n return {\n dimension: described?.dimensions!,\n // Since vector_count is not available in the response,\n // we might need a separate API call to get the count if needed\n count: described?.vectorCount || 0,\n metric: index?.config?.metric as 'cosine' | 'euclidean' | 'dotproduct',\n };\n } catch (error) {\n throw new MastraError(\n {\n id: 'STORAGE_VECTORIZE_VECTOR_DESCRIBE_INDEX_FAILED',\n domain: ErrorDomain.STORAGE,\n category: ErrorCategory.THIRD_PARTY,\n details: { indexName },\n },\n error,\n );\n }\n }\n\n async deleteIndex({ indexName }: DeleteIndexParams): Promise<void> {\n try {\n await this.client.vectorize.indexes.delete(indexName, {\n account_id: this.accountId,\n });\n } catch (error) {\n throw new MastraError(\n {\n id: 'STORAGE_VECTORIZE_VECTOR_DELETE_INDEX_FAILED',\n domain: ErrorDomain.STORAGE,\n category: ErrorCategory.THIRD_PARTY,\n details: { indexName },\n },\n error,\n );\n }\n }\n\n async createMetadataIndex(indexName: string, propertyName: string, indexType: 'string' | 'number' | 'boolean') {\n try {\n await this.client.vectorize.indexes.metadataIndex.create(indexName, {\n account_id: this.accountId,\n propertyName,\n indexType,\n });\n } catch (error) {\n throw new MastraError(\n {\n id: 'STORAGE_VECTORIZE_VECTOR_CREATE_METADATA_INDEX_FAILED',\n domain: ErrorDomain.STORAGE,\n category: ErrorCategory.THIRD_PARTY,\n details: { indexName, propertyName, indexType },\n },\n error,\n );\n }\n }\n\n async deleteMetadataIndex(indexName: string, propertyName: string) {\n try {\n await this.client.vectorize.indexes.metadataIndex.delete(indexName, {\n account_id: this.accountId,\n propertyName,\n });\n } catch (error) {\n throw new MastraError(\n {\n id: 'STORAGE_VECTORIZE_VECTOR_DELETE_METADATA_INDEX_FAILED',\n domain: ErrorDomain.STORAGE,\n category: ErrorCategory.THIRD_PARTY,\n details: { indexName, propertyName },\n },\n error,\n );\n }\n }\n\n async listMetadataIndexes(indexName: string) {\n try {\n const res = await this.client.vectorize.indexes.metadataIndex.list(indexName, {\n account_id: this.accountId,\n });\n\n return res?.metadataIndexes ?? [];\n } catch (error) {\n throw new MastraError(\n {\n id: 'STORAGE_VECTORIZE_VECTOR_LIST_METADATA_INDEXES_FAILED',\n domain: ErrorDomain.STORAGE,\n category: ErrorCategory.THIRD_PARTY,\n details: { indexName },\n },\n error,\n );\n }\n }\n\n /**\n * Updates a vector by its ID with the provided vector and/or metadata.\n * @param indexName - The name of the index containing the vector.\n * @param id - The ID of the vector to update.\n * @param update - An object containing the vector and/or metadata to update.\n * @param update.vector - An optional array of numbers representing the new vector.\n * @param update.metadata - An optional record containing the new metadata.\n * @returns A promise that resolves when the update is complete.\n * @throws Will throw an error if no updates are provided or if the update operation fails.\n */\n async updateVector({ indexName, id, update }: UpdateVectorParams): Promise<void> {\n if (!update.vector && !update.metadata) {\n throw new MastraError({\n id: 'STORAGE_VECTORIZE_VECTOR_UPDATE_VECTOR_INVALID_ARGS',\n domain: ErrorDomain.STORAGE,\n category: ErrorCategory.USER,\n text: 'No update data provided',\n details: { indexName, id },\n });\n }\n\n try {\n const updatePayload: any = {\n ids: [id],\n account_id: this.accountId,\n };\n\n if (update.vector) {\n updatePayload.vectors = [update.vector];\n }\n if (update.metadata) {\n updatePayload.metadata = [update.metadata];\n }\n\n await this.upsert({ indexName: indexName, vectors: updatePayload.vectors, metadata: updatePayload.metadata });\n } catch (error) {\n throw new MastraError(\n {\n id: 'STORAGE_VECTORIZE_VECTOR_UPDATE_VECTOR_FAILED',\n domain: ErrorDomain.STORAGE,\n category: ErrorCategory.THIRD_PARTY,\n details: { indexName, id },\n },\n error,\n );\n }\n }\n\n /**\n * Deletes a vector by its ID.\n * @param indexName - The name of the index containing the vector.\n * @param id - The ID of the vector to delete.\n * @returns A promise that resolves when the deletion is complete.\n * @throws Will throw an error if the deletion operation fails.\n */\n async deleteVector({ indexName, id }: DeleteVectorParams): Promise<void> {\n try {\n await this.client.vectorize.indexes.deleteByIds(indexName, {\n ids: [id],\n account_id: this.accountId,\n });\n } catch (error) {\n throw new MastraError(\n {\n id: 'STORAGE_VECTORIZE_VECTOR_DELETE_VECTOR_FAILED',\n domain: ErrorDomain.STORAGE,\n category: ErrorCategory.THIRD_PARTY,\n details: { indexName, id },\n },\n error,\n );\n }\n }\n}\n","/**\n * Vector store specific prompt that details supported operators and examples.\n * This prompt helps users construct valid filters for Vectorize.\n */\nexport const VECTORIZE_PROMPT = `When querying Vectorize, you can ONLY use the operators listed below. Any other operators will be rejected.\nImportant: Don't explain how to construct the filter - use the specified operators and fields to search the content and return relevant results.\nIf 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.\n\nBasic Comparison Operators:\n- $eq: Exact match (default when using field: value)\n Example: { \"category\": \"electronics\" }\n- $ne: Not equal\n Example: { \"category\": { \"$ne\": \"electronics\" } }\n- $gt: Greater than\n Example: { \"price\": { \"$gt\": 100 } }\n- $gte: Greater than or equal\n Example: { \"price\": { \"$gte\": 100 } }\n- $lt: Less than\n Example: { \"price\": { \"$lt\": 100 } }\n- $lte: Less than or equal\n Example: { \"price\": { \"$lte\": 100 } }\n\nArray Operators:\n- $in: Match any value in array\n Example: { \"category\": { \"$in\": [\"electronics\", \"books\"] } }\n- $nin: Does not match any value in array\n Example: { \"category\": { \"$nin\": [\"electronics\", \"books\"] } }\n\nLogical Operators:\n- $and: Logical AND (can be implicit or explicit)\n Implicit Example: { \"price\": { \"$gt\": 100 }, \"category\": \"electronics\" }\n Explicit Example: { \"$and\": [{ \"price\": { \"$gt\": 100 } }, { \"category\": \"electronics\" }] }\n- $or: Logical OR\n Example: { \"$or\": [{ \"price\": { \"$lt\": 50 } }, { \"category\": \"books\" }] }\n\nElement Operators:\n- $exists: Check if field exists\n Example: { \"rating\": { \"$exists\": true } }\n- $match: Match text using full-text search\n Example: { \"description\": { \"$match\": \"gaming laptop\" } }\n\nRestrictions:\n- Regex patterns are not supported\n- Only $and and $or logical operators are supported at the top level\n- Empty arrays in $in/$nin will return no results\n- Nested fields are supported using dot notation\n- Multiple conditions on the same field are supported with both implicit and explicit $and\n- At least one key-value pair is required in filter object\n- Empty objects and undefined values are treated as no filter\n- Invalid types in comparison operators will throw errors\n- All non-logical operators must be used within a field condition\n Valid: { \"field\": { \"$gt\": 100 } }\n Valid: { \"$and\": [...] }\n Invalid: { \"$gt\": 100 }\n- Logical operators must contain field conditions, not direct operators\n Valid: { \"$and\": [{ \"field\": { \"$gt\": 100 } }] }\n Invalid: { \"$and\": [{ \"$gt\": 100 }] }\n- Logical operators ($and, $or):\n - Can only be used at top level or nested within other logical operators\n - Can not be used on a field level, or be nested inside a field\n - Can not be used inside an operator\n - Valid: { \"$and\": [{ \"field\": { \"$gt\": 100 } }] }\n - Valid: { \"$or\": [{ \"$and\": [{ \"field\": { \"$gt\": 100 } }] }] }\n - Invalid: { \"field\": { \"$and\": [{ \"$gt\": 100 }] } }\n - Invalid: { \"field\": { \"$or\": [{ \"$gt\": 100 }] } }\n - Invalid: { \"field\": { \"$gt\": { \"$and\": [{...}] } } }\n\nExample Complex Query:\n{\n \"$and\": [\n { \"category\": { \"$in\": [\"electronics\", \"computers\"] } },\n { \"price\": { \"$gte\": 100, \"$lte\": 1000 } },\n { \"description\": { \"$match\": \"gaming laptop\" } },\n { \"rating\": { \"$exists\": true, \"$gt\": 4 } },\n { \"$or\": [\n { \"stock\": { \"$gt\": 0 } },\n { \"preorder\": true }\n ]}\n ]\n}`;\n"]}
|
package/dist/index.d.ts
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,UAAU,CAAC;AACzB,OAAO,EAAE,gBAAgB,EAAE,MAAM,iBAAiB,CAAC"}
|