@mastra/opensearch 0.0.0-pass-headers-for-create-mastra-client-20250529200245 → 0.0.0-scorers-api-v2-20250801171841
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 +213 -2
- package/LICENSE.md +11 -42
- package/dist/index.cjs +130 -53
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.ts +2 -1
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +122 -45
- package/dist/index.js.map +1 -0
- package/dist/vector/filter.d.ts +47 -0
- package/dist/vector/filter.d.ts.map +1 -0
- package/dist/vector/index.d.ts +101 -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 +13 -13
- package/src/vector/filter.test.ts +55 -54
- package/src/vector/filter.ts +23 -5
- package/src/vector/index.test.ts +4 -5
- package/src/vector/index.ts +134 -55
- package/tsconfig.build.json +9 -0
- package/tsconfig.json +1 -1
- package/tsup.config.ts +22 -0
- package/.turbo/turbo-build.log +0 -23
- package/dist/_tsup-dts-rollup.d.cts +0 -159
- package/dist/_tsup-dts-rollup.d.ts +0 -159
- package/dist/index.d.cts +0 -1
package/src/vector/filter.ts
CHANGED
|
@@ -1,30 +1,48 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type {
|
|
2
|
+
BlacklistedRootOperators,
|
|
3
|
+
LogicalOperatorValueMap,
|
|
4
|
+
OperatorSupport,
|
|
5
|
+
OperatorValueMap,
|
|
6
|
+
QueryOperator,
|
|
7
|
+
VectorFilter,
|
|
8
|
+
} from '@mastra/core/vector/filter';
|
|
2
9
|
import { BaseFilterTranslator } from '@mastra/core/vector/filter';
|
|
3
10
|
|
|
11
|
+
type OpenSearchOperatorValueMap = Omit<OperatorValueMap, '$options' | '$nor' | '$elemMatch'>;
|
|
12
|
+
|
|
13
|
+
type OpenSearchLogicalOperatorValueMap = Omit<LogicalOperatorValueMap, '$nor'>;
|
|
14
|
+
|
|
15
|
+
type OpenSearchBlacklisted = BlacklistedRootOperators | '$nor';
|
|
16
|
+
|
|
17
|
+
export type OpenSearchVectorFilter = VectorFilter<
|
|
18
|
+
keyof OpenSearchOperatorValueMap,
|
|
19
|
+
OpenSearchOperatorValueMap,
|
|
20
|
+
OpenSearchLogicalOperatorValueMap,
|
|
21
|
+
OpenSearchBlacklisted
|
|
22
|
+
>;
|
|
4
23
|
/**
|
|
5
24
|
* Translator for OpenSearch filter queries.
|
|
6
25
|
* Maintains OpenSearch-compatible syntax while ensuring proper validation
|
|
7
26
|
* and normalization of values.
|
|
8
27
|
*/
|
|
9
|
-
export class OpenSearchFilterTranslator extends BaseFilterTranslator {
|
|
28
|
+
export class OpenSearchFilterTranslator extends BaseFilterTranslator<OpenSearchVectorFilter> {
|
|
10
29
|
protected override getSupportedOperators(): OperatorSupport {
|
|
11
30
|
return {
|
|
12
31
|
...BaseFilterTranslator.DEFAULT_OPERATORS,
|
|
13
32
|
logical: ['$and', '$or', '$not'],
|
|
14
33
|
array: ['$in', '$nin', '$all'],
|
|
15
|
-
element: ['$exists'],
|
|
16
34
|
regex: ['$regex'],
|
|
17
35
|
custom: [],
|
|
18
36
|
};
|
|
19
37
|
}
|
|
20
38
|
|
|
21
|
-
translate(filter?:
|
|
39
|
+
translate(filter?: OpenSearchVectorFilter): OpenSearchVectorFilter {
|
|
22
40
|
if (this.isEmpty(filter)) return undefined;
|
|
23
41
|
this.validateFilter(filter);
|
|
24
42
|
return this.translateNode(filter);
|
|
25
43
|
}
|
|
26
44
|
|
|
27
|
-
private translateNode(node:
|
|
45
|
+
private translateNode(node: OpenSearchVectorFilter): any {
|
|
28
46
|
// Handle primitive values and arrays
|
|
29
47
|
if (this.isPrimitive(node) || Array.isArray(node)) {
|
|
30
48
|
return node;
|
package/src/vector/index.test.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
// To setup a Opensearch server, run the docker compose file in the opensearch directory
|
|
2
|
-
import type { QueryResult
|
|
2
|
+
import type { QueryResult } from '@mastra/core';
|
|
3
3
|
import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest';
|
|
4
4
|
|
|
5
5
|
import { OpenSearchVector } from './index';
|
|
@@ -215,12 +215,11 @@ describe('OpenSearchVector', () => {
|
|
|
215
215
|
|
|
216
216
|
await vectorDB.upsert({ indexName: testIndexName, vectors: testVectors, metadata: testMetadata });
|
|
217
217
|
|
|
218
|
-
const
|
|
218
|
+
const results = await vectorDB.query({
|
|
219
219
|
indexName: testIndexName,
|
|
220
220
|
queryVector: [1.0, 0.1, 0.1],
|
|
221
221
|
topK: 3,
|
|
222
|
-
};
|
|
223
|
-
const results = await vectorDB.query(queryParams);
|
|
222
|
+
});
|
|
224
223
|
|
|
225
224
|
expect(results).toHaveLength(3);
|
|
226
225
|
expect(results[0]?.score).toBeGreaterThan(0);
|
|
@@ -1197,7 +1196,7 @@ describe('OpenSearchVector', () => {
|
|
|
1197
1196
|
vectorDB.query({
|
|
1198
1197
|
indexName,
|
|
1199
1198
|
queryVector: [1, 0, 0],
|
|
1200
|
-
filter: { price: { $invalid: 100 } },
|
|
1199
|
+
filter: { price: { $invalid: 100 } as any },
|
|
1201
1200
|
}),
|
|
1202
1201
|
).rejects.toThrow('Unsupported operator: $invalid');
|
|
1203
1202
|
});
|
package/src/vector/index.ts
CHANGED
|
@@ -9,10 +9,11 @@ import type {
|
|
|
9
9
|
UpdateVectorParams,
|
|
10
10
|
UpsertVectorParams,
|
|
11
11
|
} from '@mastra/core';
|
|
12
|
+
import { MastraError, ErrorDomain, ErrorCategory } from '@mastra/core/error';
|
|
12
13
|
import { MastraVector } from '@mastra/core/vector';
|
|
13
|
-
import type { VectorFilter } from '@mastra/core/vector/filter';
|
|
14
14
|
import { Client as OpenSearchClient } from '@opensearch-project/opensearch';
|
|
15
15
|
import { OpenSearchFilterTranslator } from './filter';
|
|
16
|
+
import type { OpenSearchVectorFilter } from './filter';
|
|
16
17
|
|
|
17
18
|
const METRIC_MAPPING = {
|
|
18
19
|
cosine: 'cosinesimil',
|
|
@@ -26,7 +27,9 @@ const REVERSE_METRIC_MAPPING = {
|
|
|
26
27
|
innerproduct: 'dotproduct',
|
|
27
28
|
} as const;
|
|
28
29
|
|
|
29
|
-
|
|
30
|
+
type OpenSearchVectorParams = QueryVectorParams<OpenSearchVectorFilter>;
|
|
31
|
+
|
|
32
|
+
export class OpenSearchVector extends MastraVector<OpenSearchVectorFilter> {
|
|
30
33
|
private client: OpenSearchClient;
|
|
31
34
|
|
|
32
35
|
/**
|
|
@@ -49,7 +52,13 @@ export class OpenSearchVector extends MastraVector {
|
|
|
49
52
|
*/
|
|
50
53
|
async createIndex({ indexName, dimension, metric = 'cosine' }: CreateIndexParams): Promise<void> {
|
|
51
54
|
if (!Number.isInteger(dimension) || dimension <= 0) {
|
|
52
|
-
throw new
|
|
55
|
+
throw new MastraError({
|
|
56
|
+
id: 'STORAGE_OPENSEARCH_VECTOR_CREATE_INDEX_INVALID_ARGS',
|
|
57
|
+
domain: ErrorDomain.STORAGE,
|
|
58
|
+
category: ErrorCategory.USER,
|
|
59
|
+
text: 'Dimension must be a positive integer',
|
|
60
|
+
details: { indexName, dimension },
|
|
61
|
+
});
|
|
53
62
|
}
|
|
54
63
|
|
|
55
64
|
try {
|
|
@@ -82,8 +91,15 @@ export class OpenSearchVector extends MastraVector {
|
|
|
82
91
|
await this.validateExistingIndex(indexName, dimension, metric);
|
|
83
92
|
return;
|
|
84
93
|
}
|
|
85
|
-
|
|
86
|
-
|
|
94
|
+
throw new MastraError(
|
|
95
|
+
{
|
|
96
|
+
id: 'STORAGE_OPENSEARCH_VECTOR_CREATE_INDEX_FAILED',
|
|
97
|
+
domain: ErrorDomain.STORAGE,
|
|
98
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
99
|
+
details: { indexName, dimension, metric },
|
|
100
|
+
},
|
|
101
|
+
error,
|
|
102
|
+
);
|
|
87
103
|
}
|
|
88
104
|
}
|
|
89
105
|
|
|
@@ -100,9 +116,15 @@ export class OpenSearchVector extends MastraVector {
|
|
|
100
116
|
.filter((index: string | undefined) => index !== undefined);
|
|
101
117
|
|
|
102
118
|
return indexes;
|
|
103
|
-
} catch (error
|
|
104
|
-
|
|
105
|
-
|
|
119
|
+
} catch (error) {
|
|
120
|
+
throw new MastraError(
|
|
121
|
+
{
|
|
122
|
+
id: 'STORAGE_OPENSEARCH_VECTOR_LIST_INDEXES_FAILED',
|
|
123
|
+
domain: ErrorDomain.STORAGE,
|
|
124
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
125
|
+
},
|
|
126
|
+
error,
|
|
127
|
+
);
|
|
106
128
|
}
|
|
107
129
|
}
|
|
108
130
|
|
|
@@ -137,7 +159,17 @@ export class OpenSearchVector extends MastraVector {
|
|
|
137
159
|
try {
|
|
138
160
|
await this.client.indices.delete({ index: indexName });
|
|
139
161
|
} catch (error) {
|
|
140
|
-
|
|
162
|
+
const mastraError = new MastraError(
|
|
163
|
+
{
|
|
164
|
+
id: 'STORAGE_OPENSEARCH_VECTOR_DELETE_INDEX_FAILED',
|
|
165
|
+
domain: ErrorDomain.STORAGE,
|
|
166
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
167
|
+
details: { indexName },
|
|
168
|
+
},
|
|
169
|
+
error,
|
|
170
|
+
);
|
|
171
|
+
this.logger?.error(mastraError.toString());
|
|
172
|
+
this.logger?.trackException(mastraError);
|
|
141
173
|
}
|
|
142
174
|
}
|
|
143
175
|
|
|
@@ -154,39 +186,46 @@ export class OpenSearchVector extends MastraVector {
|
|
|
154
186
|
const vectorIds = ids || vectors.map(() => crypto.randomUUID());
|
|
155
187
|
const operations = [];
|
|
156
188
|
|
|
157
|
-
|
|
158
|
-
|
|
189
|
+
try {
|
|
190
|
+
// Get index stats to check dimension
|
|
191
|
+
const indexInfo = await this.describeIndex({ indexName });
|
|
159
192
|
|
|
160
|
-
|
|
161
|
-
|
|
193
|
+
// Validate vector dimensions
|
|
194
|
+
this.validateVectorDimensions(vectors, indexInfo.dimension);
|
|
162
195
|
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
196
|
+
for (let i = 0; i < vectors.length; i++) {
|
|
197
|
+
const operation = {
|
|
198
|
+
index: {
|
|
199
|
+
_index: indexName,
|
|
200
|
+
_id: vectorIds[i],
|
|
201
|
+
},
|
|
202
|
+
};
|
|
170
203
|
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
204
|
+
const document = {
|
|
205
|
+
id: vectorIds[i],
|
|
206
|
+
embedding: vectors[i],
|
|
207
|
+
metadata: metadata[i] || {},
|
|
208
|
+
};
|
|
176
209
|
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
210
|
+
operations.push(operation);
|
|
211
|
+
operations.push(document);
|
|
212
|
+
}
|
|
180
213
|
|
|
181
|
-
try {
|
|
182
214
|
if (operations.length > 0) {
|
|
183
215
|
await this.client.bulk({ body: operations, refresh: true });
|
|
184
216
|
}
|
|
185
217
|
|
|
186
218
|
return vectorIds;
|
|
187
219
|
} catch (error) {
|
|
188
|
-
|
|
189
|
-
|
|
220
|
+
throw new MastraError(
|
|
221
|
+
{
|
|
222
|
+
id: 'STORAGE_OPENSEARCH_VECTOR_UPSERT_FAILED',
|
|
223
|
+
domain: ErrorDomain.STORAGE,
|
|
224
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
225
|
+
details: { indexName, vectorCount: vectors?.length || 0 },
|
|
226
|
+
},
|
|
227
|
+
error,
|
|
228
|
+
);
|
|
190
229
|
}
|
|
191
230
|
}
|
|
192
231
|
|
|
@@ -206,7 +245,7 @@ export class OpenSearchVector extends MastraVector {
|
|
|
206
245
|
filter,
|
|
207
246
|
topK = 10,
|
|
208
247
|
includeVector = false,
|
|
209
|
-
}:
|
|
248
|
+
}: OpenSearchVectorParams): Promise<QueryResult[]> {
|
|
210
249
|
try {
|
|
211
250
|
const translatedFilter = this.transformFilter(filter);
|
|
212
251
|
|
|
@@ -234,9 +273,16 @@ export class OpenSearchVector extends MastraVector {
|
|
|
234
273
|
});
|
|
235
274
|
|
|
236
275
|
return results;
|
|
237
|
-
} catch (error
|
|
238
|
-
|
|
239
|
-
|
|
276
|
+
} catch (error) {
|
|
277
|
+
throw new MastraError(
|
|
278
|
+
{
|
|
279
|
+
id: 'STORAGE_OPENSEARCH_VECTOR_QUERY_FAILED',
|
|
280
|
+
domain: ErrorDomain.STORAGE,
|
|
281
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
282
|
+
details: { indexName, topK },
|
|
283
|
+
},
|
|
284
|
+
error,
|
|
285
|
+
);
|
|
240
286
|
}
|
|
241
287
|
}
|
|
242
288
|
|
|
@@ -256,10 +302,10 @@ export class OpenSearchVector extends MastraVector {
|
|
|
256
302
|
/**
|
|
257
303
|
* Transforms the filter to the OpenSearch DSL.
|
|
258
304
|
*
|
|
259
|
-
* @param {
|
|
305
|
+
* @param {OpenSearchVectorFilter} filter - The filter to transform.
|
|
260
306
|
* @returns {Record<string, any>} The transformed filter.
|
|
261
307
|
*/
|
|
262
|
-
private transformFilter(filter?:
|
|
308
|
+
private transformFilter(filter?: OpenSearchVectorFilter): any {
|
|
263
309
|
const translator = new OpenSearchFilterTranslator();
|
|
264
310
|
return translator.translate(filter);
|
|
265
311
|
}
|
|
@@ -275,13 +321,15 @@ export class OpenSearchVector extends MastraVector {
|
|
|
275
321
|
* @throws Will throw an error if no updates are provided or if the update operation fails.
|
|
276
322
|
*/
|
|
277
323
|
async updateVector({ indexName, id, update }: UpdateVectorParams): Promise<void> {
|
|
278
|
-
|
|
279
|
-
throw new Error('No updates provided');
|
|
280
|
-
}
|
|
281
|
-
|
|
324
|
+
let existingDoc;
|
|
282
325
|
try {
|
|
326
|
+
if (!update.vector && !update.metadata) {
|
|
327
|
+
throw new Error('No updates provided');
|
|
328
|
+
}
|
|
329
|
+
|
|
283
330
|
// First get the current document to merge with updates
|
|
284
|
-
const { body
|
|
331
|
+
const { body } = await this.client
|
|
332
|
+
|
|
285
333
|
.get({
|
|
286
334
|
index: indexName,
|
|
287
335
|
id: id,
|
|
@@ -290,25 +338,40 @@ export class OpenSearchVector extends MastraVector {
|
|
|
290
338
|
throw new Error(`Document with ID ${id} not found in index ${indexName}`);
|
|
291
339
|
});
|
|
292
340
|
|
|
293
|
-
if (!
|
|
341
|
+
if (!body || !body._source) {
|
|
294
342
|
throw new Error(`Document with ID ${id} has no source data in index ${indexName}`);
|
|
295
343
|
}
|
|
344
|
+
existingDoc = body;
|
|
345
|
+
} catch (error) {
|
|
346
|
+
throw new MastraError(
|
|
347
|
+
{
|
|
348
|
+
id: 'STORAGE_OPENSEARCH_VECTOR_UPDATE_VECTOR_FAILED',
|
|
349
|
+
domain: ErrorDomain.STORAGE,
|
|
350
|
+
category: ErrorCategory.USER,
|
|
351
|
+
details: { indexName, id },
|
|
352
|
+
},
|
|
353
|
+
error,
|
|
354
|
+
);
|
|
355
|
+
}
|
|
296
356
|
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
357
|
+
const source = existingDoc._source;
|
|
358
|
+
const updatedDoc: Record<string, any> = {
|
|
359
|
+
id: source?.id || id,
|
|
360
|
+
};
|
|
301
361
|
|
|
362
|
+
try {
|
|
302
363
|
// Update vector if provided
|
|
303
364
|
if (update.vector) {
|
|
304
365
|
// Get index stats to check dimension
|
|
366
|
+
console.log(`1`);
|
|
305
367
|
const indexInfo = await this.describeIndex({ indexName });
|
|
306
368
|
|
|
307
369
|
// Validate vector dimensions
|
|
370
|
+
console.log(`2`);
|
|
308
371
|
this.validateVectorDimensions([update.vector], indexInfo.dimension);
|
|
309
372
|
|
|
310
373
|
updatedDoc.embedding = update.vector;
|
|
311
|
-
} else if (source
|
|
374
|
+
} else if (source?.embedding) {
|
|
312
375
|
updatedDoc.embedding = source.embedding;
|
|
313
376
|
}
|
|
314
377
|
|
|
@@ -316,10 +379,11 @@ export class OpenSearchVector extends MastraVector {
|
|
|
316
379
|
if (update.metadata) {
|
|
317
380
|
updatedDoc.metadata = update.metadata;
|
|
318
381
|
} else {
|
|
319
|
-
updatedDoc.metadata = source
|
|
382
|
+
updatedDoc.metadata = source?.metadata || {};
|
|
320
383
|
}
|
|
321
384
|
|
|
322
385
|
// Update the document
|
|
386
|
+
console.log(`3`);
|
|
323
387
|
await this.client.index({
|
|
324
388
|
index: indexName,
|
|
325
389
|
id: id,
|
|
@@ -327,8 +391,15 @@ export class OpenSearchVector extends MastraVector {
|
|
|
327
391
|
refresh: true,
|
|
328
392
|
});
|
|
329
393
|
} catch (error) {
|
|
330
|
-
|
|
331
|
-
|
|
394
|
+
throw new MastraError(
|
|
395
|
+
{
|
|
396
|
+
id: 'STORAGE_OPENSEARCH_VECTOR_UPDATE_VECTOR_FAILED',
|
|
397
|
+
domain: ErrorDomain.STORAGE,
|
|
398
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
399
|
+
details: { indexName, id },
|
|
400
|
+
},
|
|
401
|
+
error,
|
|
402
|
+
);
|
|
332
403
|
}
|
|
333
404
|
}
|
|
334
405
|
|
|
@@ -347,11 +418,19 @@ export class OpenSearchVector extends MastraVector {
|
|
|
347
418
|
refresh: true,
|
|
348
419
|
});
|
|
349
420
|
} catch (error: unknown) {
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
throw error;
|
|
421
|
+
// Don't throw error if document doesn't exist (404)
|
|
422
|
+
if (error && typeof error === 'object' && 'statusCode' in error && error.statusCode === 404) {
|
|
423
|
+
return;
|
|
354
424
|
}
|
|
425
|
+
throw new MastraError(
|
|
426
|
+
{
|
|
427
|
+
id: 'STORAGE_OPENSEARCH_VECTOR_DELETE_VECTOR_FAILED',
|
|
428
|
+
domain: ErrorDomain.STORAGE,
|
|
429
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
430
|
+
details: { indexName, id },
|
|
431
|
+
},
|
|
432
|
+
error,
|
|
433
|
+
);
|
|
355
434
|
}
|
|
356
435
|
}
|
|
357
436
|
}
|
package/tsconfig.json
CHANGED
package/tsup.config.ts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { spawn } from 'child_process';
|
|
2
|
+
import { promisify } from 'util';
|
|
3
|
+
import { defineConfig } from 'tsup';
|
|
4
|
+
|
|
5
|
+
const exec = promisify(spawn);
|
|
6
|
+
|
|
7
|
+
export default defineConfig({
|
|
8
|
+
entry: ['src/index.ts'],
|
|
9
|
+
format: ['esm', 'cjs'],
|
|
10
|
+
clean: true,
|
|
11
|
+
dts: false,
|
|
12
|
+
splitting: true,
|
|
13
|
+
treeshake: {
|
|
14
|
+
preset: 'smallest',
|
|
15
|
+
},
|
|
16
|
+
sourcemap: true,
|
|
17
|
+
onSuccess: async () => {
|
|
18
|
+
await exec('pnpm', ['tsc', '-p', 'tsconfig.build.json'], {
|
|
19
|
+
stdio: 'inherit',
|
|
20
|
+
});
|
|
21
|
+
},
|
|
22
|
+
});
|
package/.turbo/turbo-build.log
DELETED
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
> @mastra/opensearch@0.10.0 build /home/runner/work/mastra/mastra/stores/opensearch
|
|
3
|
-
> tsup src/index.ts --format esm,cjs --experimental-dts --clean --treeshake=smallest --splitting
|
|
4
|
-
|
|
5
|
-
[34mCLI[39m Building entry: src/index.ts
|
|
6
|
-
[34mCLI[39m Using tsconfig: tsconfig.json
|
|
7
|
-
[34mCLI[39m tsup v8.4.0
|
|
8
|
-
[34mTSC[39m Build start
|
|
9
|
-
[32mTSC[39m ⚡️ Build success in 10775ms
|
|
10
|
-
[34mDTS[39m Build start
|
|
11
|
-
[34mCLI[39m Target: es2022
|
|
12
|
-
Analysis will use the bundled TypeScript version 5.8.3
|
|
13
|
-
[36mWriting package typings: /home/runner/work/mastra/mastra/stores/opensearch/dist/_tsup-dts-rollup.d.ts[39m
|
|
14
|
-
Analysis will use the bundled TypeScript version 5.8.3
|
|
15
|
-
[36mWriting package typings: /home/runner/work/mastra/mastra/stores/opensearch/dist/_tsup-dts-rollup.d.cts[39m
|
|
16
|
-
[32mDTS[39m ⚡️ Build success in 10998ms
|
|
17
|
-
[34mCLI[39m Cleaning output folder
|
|
18
|
-
[34mESM[39m Build start
|
|
19
|
-
[34mCJS[39m Build start
|
|
20
|
-
[32mESM[39m [1mdist/index.js [22m[32m21.72 KB[39m
|
|
21
|
-
[32mESM[39m ⚡️ Build success in 977ms
|
|
22
|
-
[32mCJS[39m [1mdist/index.cjs [22m[32m21.76 KB[39m
|
|
23
|
-
[32mCJS[39m ⚡️ Build success in 977ms
|
|
@@ -1,159 +0,0 @@
|
|
|
1
|
-
import { BaseFilterTranslator } from '@mastra/core/vector/filter';
|
|
2
|
-
import type { CreateIndexParams } from '@mastra/core';
|
|
3
|
-
import type { DeleteIndexParams } from '@mastra/core';
|
|
4
|
-
import type { DeleteVectorParams } from '@mastra/core';
|
|
5
|
-
import type { DescribeIndexParams } from '@mastra/core';
|
|
6
|
-
import type { IndexStats } from '@mastra/core';
|
|
7
|
-
import { MastraVector } from '@mastra/core/vector';
|
|
8
|
-
import type { OperatorSupport } from '@mastra/core/vector/filter';
|
|
9
|
-
import type { QueryResult } from '@mastra/core';
|
|
10
|
-
import type { QueryVectorParams } from '@mastra/core';
|
|
11
|
-
import type { UpdateVectorParams } from '@mastra/core';
|
|
12
|
-
import type { UpsertVectorParams } from '@mastra/core';
|
|
13
|
-
import type { VectorFilter } from '@mastra/core/vector/filter';
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* Vector store prompt for OpenSearch. This prompt details supported filter operators, syntax, and usage examples.
|
|
17
|
-
* Use this as a guide for constructing valid filters for OpenSearch vector queries in Mastra.
|
|
18
|
-
*/
|
|
19
|
-
export declare const OPENSEARCH_PROMPT = "When querying OpenSearch, you can ONLY use the operators listed below. Any other operators will be rejected.\nImportant: Do not explain how to construct the filter\u2014use the specified operators and fields to search the content and return relevant results.\nIf a user tries to use an unsupported operator, reject the filter entirely and let them know that the operator is not supported.\n\nBasic Comparison Operators:\n- $eq: Exact match (default for 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- $all: Match all values in array\n Example: { \"tags\": { \"$all\": [\"premium\", \"sale\"] } }\n\nLogical Operators:\n- $and: Logical AND (implicit when using multiple conditions)\n Example: { \"$and\": [{ \"price\": { \"$gt\": 100 } }, { \"category\": \"electronics\" }] }\n- $or: Logical OR\n Example: { \"$or\": [{ \"price\": { \"$lt\": 50 } }, { \"category\": \"books\" }] }\n- $not: Logical NOT\n Example: { \"$not\": { \"category\": \"electronics\" } }\n\nElement Operators:\n- $exists: Check if field exists\n Example: { \"rating\": { \"$exists\": true } }\n\nRegex Operator:\n- $regex: Match using a regular expression (ECMAScript syntax)\n Example: { \"name\": { \"$regex\": \"^Sam.*son$\" } }\n Note: Regex queries are supported for string fields only. Use valid ECMAScript patterns; invalid patterns will throw an error.\n\nRestrictions:\n- Nested fields are supported using dot notation (e.g., \"address.city\").\n- Multiple conditions on the same field are supported (e.g., { \"price\": { \"$gte\": 100, \"$lte\": 1000 } }).\n- Only logical operators ($and, $or, $not) can be used at the top level.\n- All other 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- $not operator:\n - Must be an object\n - Cannot be empty\n - Can be used at field level or top level\n - Valid: { \"$not\": { \"field\": \"value\" } }\n - Valid: { \"field\": { \"$not\": { \"$eq\": \"value\" } } }\n- Array operators work on array fields only.\n- Empty arrays in conditions are handled gracefully.\n- Regex queries are case-sensitive by default; use patterns accordingly.\n\nExample Complex Query:\n{\n \"$and\": [\n { \"category\": { \"$in\": [\"electronics\", \"computers\"] } },\n { \"price\": { \"$gte\": 100, \"$lte\": 1000 } },\n { \"tags\": { \"$all\": [\"premium\"] } },\n { \"rating\": { \"$exists\": true, \"$gt\": 4 } },\n { \"$or\": [\n { \"stock\": { \"$gt\": 0 } },\n { \"preorder\": true }\n ]},\n { \"name\": { \"$regex\": \"^Sam.*son$\" } }\n ]\n}";
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* Translator for OpenSearch filter queries.
|
|
23
|
-
* Maintains OpenSearch-compatible syntax while ensuring proper validation
|
|
24
|
-
* and normalization of values.
|
|
25
|
-
*/
|
|
26
|
-
export declare class OpenSearchFilterTranslator extends BaseFilterTranslator {
|
|
27
|
-
protected getSupportedOperators(): OperatorSupport;
|
|
28
|
-
translate(filter?: VectorFilter): VectorFilter;
|
|
29
|
-
private translateNode;
|
|
30
|
-
/**
|
|
31
|
-
* Handles translation of nested objects with dot notation fields
|
|
32
|
-
*/
|
|
33
|
-
private translateNestedObject;
|
|
34
|
-
private translateLogicalOperator;
|
|
35
|
-
private translateFieldOperator;
|
|
36
|
-
/**
|
|
37
|
-
* Translates regex patterns to OpenSearch query syntax
|
|
38
|
-
*/
|
|
39
|
-
private translateRegexOperator;
|
|
40
|
-
private addKeywordIfNeeded;
|
|
41
|
-
/**
|
|
42
|
-
* Helper method to handle special cases for the $not operator
|
|
43
|
-
*/
|
|
44
|
-
private handleNotOperatorSpecialCases;
|
|
45
|
-
private translateOperator;
|
|
46
|
-
/**
|
|
47
|
-
* Translates field conditions to OpenSearch query syntax
|
|
48
|
-
* Handles special cases like range queries and multiple operators
|
|
49
|
-
*/
|
|
50
|
-
private translateFieldConditions;
|
|
51
|
-
/**
|
|
52
|
-
* Checks if conditions can be optimized to a range query
|
|
53
|
-
*/
|
|
54
|
-
private canOptimizeToRangeQuery;
|
|
55
|
-
/**
|
|
56
|
-
* Creates a range query from numeric operators
|
|
57
|
-
*/
|
|
58
|
-
private createRangeQuery;
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
declare class OpenSearchVector extends MastraVector {
|
|
62
|
-
private client;
|
|
63
|
-
/**
|
|
64
|
-
* Creates a new OpenSearchVector client.
|
|
65
|
-
*
|
|
66
|
-
* @param {string} url - The url of the OpenSearch node.
|
|
67
|
-
*/
|
|
68
|
-
constructor({ url }: {
|
|
69
|
-
url: string;
|
|
70
|
-
});
|
|
71
|
-
/**
|
|
72
|
-
* Creates a new collection with the specified configuration.
|
|
73
|
-
*
|
|
74
|
-
* @param {string} indexName - The name of the collection to create.
|
|
75
|
-
* @param {number} dimension - The dimension of the vectors to be stored in the collection.
|
|
76
|
-
* @param {'cosine' | 'euclidean' | 'dotproduct'} [metric=cosine] - The metric to use to sort vectors in the collection.
|
|
77
|
-
* @returns {Promise<void>} A promise that resolves when the collection is created.
|
|
78
|
-
*/
|
|
79
|
-
createIndex({ indexName, dimension, metric }: CreateIndexParams): Promise<void>;
|
|
80
|
-
/**
|
|
81
|
-
* Lists all indexes.
|
|
82
|
-
*
|
|
83
|
-
* @returns {Promise<string[]>} A promise that resolves to an array of indexes.
|
|
84
|
-
*/
|
|
85
|
-
listIndexes(): Promise<string[]>;
|
|
86
|
-
/**
|
|
87
|
-
* Retrieves statistics about a vector index.
|
|
88
|
-
*
|
|
89
|
-
* @param {string} indexName - The name of the index to describe
|
|
90
|
-
* @returns A promise that resolves to the index statistics including dimension, count and metric
|
|
91
|
-
*/
|
|
92
|
-
describeIndex({ indexName }: DescribeIndexParams): Promise<IndexStats>;
|
|
93
|
-
/**
|
|
94
|
-
* Deletes the specified index.
|
|
95
|
-
*
|
|
96
|
-
* @param {string} indexName - The name of the index to delete.
|
|
97
|
-
* @returns {Promise<void>} A promise that resolves when the index is deleted.
|
|
98
|
-
*/
|
|
99
|
-
deleteIndex({ indexName }: DeleteIndexParams): Promise<void>;
|
|
100
|
-
/**
|
|
101
|
-
* Inserts or updates vectors in the specified collection.
|
|
102
|
-
*
|
|
103
|
-
* @param {string} indexName - The name of the collection to upsert into.
|
|
104
|
-
* @param {number[][]} vectors - An array of vectors to upsert.
|
|
105
|
-
* @param {Record<string, any>[]} [metadata] - An optional array of metadata objects corresponding to each vector.
|
|
106
|
-
* @param {string[]} [ids] - An optional array of IDs corresponding to each vector. If not provided, new IDs will be generated.
|
|
107
|
-
* @returns {Promise<string[]>} A promise that resolves to an array of IDs of the upserted vectors.
|
|
108
|
-
*/
|
|
109
|
-
upsert({ indexName, vectors, metadata, ids }: UpsertVectorParams): Promise<string[]>;
|
|
110
|
-
/**
|
|
111
|
-
* Queries the specified collection using a vector and optional filter.
|
|
112
|
-
*
|
|
113
|
-
* @param {string} indexName - The name of the collection to query.
|
|
114
|
-
* @param {number[]} queryVector - The vector to query with.
|
|
115
|
-
* @param {number} [topK] - The maximum number of results to return.
|
|
116
|
-
* @param {Record<string, any>} [filter] - An optional filter to apply to the query. For more on filters in OpenSearch, see the filtering reference: https://opensearch.org/docs/latest/query-dsl/
|
|
117
|
-
* @param {boolean} [includeVectors=false] - Whether to include the vectors in the response.
|
|
118
|
-
* @returns {Promise<QueryResult[]>} A promise that resolves to an array of query results.
|
|
119
|
-
*/
|
|
120
|
-
query({ indexName, queryVector, filter, topK, includeVector, }: QueryVectorParams): Promise<QueryResult[]>;
|
|
121
|
-
/**
|
|
122
|
-
* Validates the dimensions of the vectors.
|
|
123
|
-
*
|
|
124
|
-
* @param {number[][]} vectors - The vectors to validate.
|
|
125
|
-
* @param {number} dimension - The dimension of the vectors.
|
|
126
|
-
* @returns {void}
|
|
127
|
-
*/
|
|
128
|
-
private validateVectorDimensions;
|
|
129
|
-
/**
|
|
130
|
-
* Transforms the filter to the OpenSearch DSL.
|
|
131
|
-
*
|
|
132
|
-
* @param {VectorFilter} filter - The filter to transform.
|
|
133
|
-
* @returns {Record<string, any>} The transformed filter.
|
|
134
|
-
*/
|
|
135
|
-
private transformFilter;
|
|
136
|
-
/**
|
|
137
|
-
* Updates a vector by its ID with the provided vector and/or metadata.
|
|
138
|
-
* @param indexName - The name of the index containing the vector.
|
|
139
|
-
* @param id - The ID of the vector to update.
|
|
140
|
-
* @param update - An object containing the vector and/or metadata to update.
|
|
141
|
-
* @param update.vector - An optional array of numbers representing the new vector.
|
|
142
|
-
* @param update.metadata - An optional record containing the new metadata.
|
|
143
|
-
* @returns A promise that resolves when the update is complete.
|
|
144
|
-
* @throws Will throw an error if no updates are provided or if the update operation fails.
|
|
145
|
-
*/
|
|
146
|
-
updateVector({ indexName, id, update }: UpdateVectorParams): Promise<void>;
|
|
147
|
-
/**
|
|
148
|
-
* Deletes a vector by its ID.
|
|
149
|
-
* @param indexName - The name of the index containing the vector.
|
|
150
|
-
* @param id - The ID of the vector to delete.
|
|
151
|
-
* @returns A promise that resolves when the deletion is complete.
|
|
152
|
-
* @throws Will throw an error if the deletion operation fails.
|
|
153
|
-
*/
|
|
154
|
-
deleteVector({ indexName, id }: DeleteVectorParams): Promise<void>;
|
|
155
|
-
}
|
|
156
|
-
export { OpenSearchVector }
|
|
157
|
-
export { OpenSearchVector as OpenSearchVector_alias_1 }
|
|
158
|
-
|
|
159
|
-
export { }
|