@mastra/astra 1.1.2 → 1.1.3-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/dist/docs/SKILL.md +1 -1
- package/dist/docs/assets/SOURCE_MAP.json +1 -1
- package/dist/docs/references/reference-rag-retrieval.md +24 -13
- package/dist/docs/references/reference-vectors-astra.md +1 -1
- package/dist/index.cjs +3 -6
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +3 -6
- package/dist/index.js.map +1 -1
- package/package.json +9 -9
package/dist/docs/SKILL.md
CHANGED
|
@@ -382,11 +382,12 @@ Vector store prompts define query patterns and filtering capabilities for each v
|
|
|
382
382
|
|
|
383
383
|
```ts
|
|
384
384
|
import { MONGODB_PROMPT } from '@mastra/mongodb'
|
|
385
|
+
import { Agent } from '@mastra/core/agent'
|
|
385
386
|
|
|
386
387
|
export const ragAgent = new Agent({
|
|
387
388
|
id: 'rag-agent',
|
|
388
389
|
name: 'RAG Agent',
|
|
389
|
-
model: 'openai/gpt-
|
|
390
|
+
model: 'openai/gpt-6.1-sol',
|
|
390
391
|
instructions: `
|
|
391
392
|
Process queries using the provided context. Structure responses to be concise and relevant.
|
|
392
393
|
${MONGODB_PROMPT}
|
|
@@ -403,7 +404,7 @@ import { PGVECTOR_PROMPT } from '@mastra/pg'
|
|
|
403
404
|
export const ragAgent = new Agent({
|
|
404
405
|
id: 'rag-agent',
|
|
405
406
|
name: 'RAG Agent',
|
|
406
|
-
model: 'openai/gpt-
|
|
407
|
+
model: 'openai/gpt-6.1-sol',
|
|
407
408
|
instructions: `
|
|
408
409
|
Process queries using the provided context. Structure responses to be concise and relevant.
|
|
409
410
|
${PGVECTOR_PROMPT}
|
|
@@ -416,11 +417,12 @@ export const ragAgent = new Agent({
|
|
|
416
417
|
|
|
417
418
|
```ts
|
|
418
419
|
import { PINECONE_PROMPT } from '@mastra/pinecone'
|
|
420
|
+
import { Agent } from '@mastra/core/agent'
|
|
419
421
|
|
|
420
422
|
export const ragAgent = new Agent({
|
|
421
423
|
id: 'rag-agent',
|
|
422
424
|
name: 'RAG Agent',
|
|
423
|
-
model: 'openai/gpt-
|
|
425
|
+
model: 'openai/gpt-6.1-sol',
|
|
424
426
|
instructions: `
|
|
425
427
|
Process queries using the provided context. Structure responses to be concise and relevant.
|
|
426
428
|
${PINECONE_PROMPT}
|
|
@@ -433,11 +435,12 @@ export const ragAgent = new Agent({
|
|
|
433
435
|
|
|
434
436
|
```ts
|
|
435
437
|
import { QDRANT_PROMPT } from '@mastra/qdrant'
|
|
438
|
+
import { Agent } from '@mastra/core/agent'
|
|
436
439
|
|
|
437
440
|
export const ragAgent = new Agent({
|
|
438
441
|
id: 'rag-agent',
|
|
439
442
|
name: 'RAG Agent',
|
|
440
|
-
model: 'openai/gpt-
|
|
443
|
+
model: 'openai/gpt-6.1-sol',
|
|
441
444
|
instructions: `
|
|
442
445
|
Process queries using the provided context. Structure responses to be concise and relevant.
|
|
443
446
|
${QDRANT_PROMPT}
|
|
@@ -450,11 +453,12 @@ export const ragAgent = new Agent({
|
|
|
450
453
|
|
|
451
454
|
```ts
|
|
452
455
|
import { CHROMA_PROMPT } from '@mastra/chroma'
|
|
456
|
+
import { Agent } from '@mastra/core/agent'
|
|
453
457
|
|
|
454
458
|
export const ragAgent = new Agent({
|
|
455
459
|
id: 'rag-agent',
|
|
456
460
|
name: 'RAG Agent',
|
|
457
|
-
model: 'openai/gpt-
|
|
461
|
+
model: 'openai/gpt-6.1-sol',
|
|
458
462
|
instructions: `
|
|
459
463
|
Process queries using the provided context. Structure responses to be concise and relevant.
|
|
460
464
|
${CHROMA_PROMPT}
|
|
@@ -467,11 +471,12 @@ export const ragAgent = new Agent({
|
|
|
467
471
|
|
|
468
472
|
```ts
|
|
469
473
|
import { ASTRA_PROMPT } from '@mastra/astra'
|
|
474
|
+
import { Agent } from '@mastra/core/agent'
|
|
470
475
|
|
|
471
476
|
export const ragAgent = new Agent({
|
|
472
477
|
id: 'rag-agent',
|
|
473
478
|
name: 'RAG Agent',
|
|
474
|
-
model: 'openai/gpt-
|
|
479
|
+
model: 'openai/gpt-6.1-sol',
|
|
475
480
|
instructions: `
|
|
476
481
|
Process queries using the provided context. Structure responses to be concise and relevant.
|
|
477
482
|
${ASTRA_PROMPT}
|
|
@@ -484,11 +489,12 @@ export const ragAgent = new Agent({
|
|
|
484
489
|
|
|
485
490
|
```ts
|
|
486
491
|
import { LIBSQL_PROMPT } from '@mastra/libsql'
|
|
492
|
+
import { Agent } from '@mastra/core/agent'
|
|
487
493
|
|
|
488
494
|
export const ragAgent = new Agent({
|
|
489
495
|
id: 'rag-agent',
|
|
490
496
|
name: 'RAG Agent',
|
|
491
|
-
model: 'openai/gpt-
|
|
497
|
+
model: 'openai/gpt-6.1-sol',
|
|
492
498
|
instructions: `
|
|
493
499
|
Process queries using the provided context. Structure responses to be concise and relevant.
|
|
494
500
|
${LIBSQL_PROMPT}
|
|
@@ -501,11 +507,12 @@ export const ragAgent = new Agent({
|
|
|
501
507
|
|
|
502
508
|
```ts
|
|
503
509
|
import { UPSTASH_PROMPT } from '@mastra/upstash'
|
|
510
|
+
import { Agent } from '@mastra/core/agent'
|
|
504
511
|
|
|
505
512
|
export const ragAgent = new Agent({
|
|
506
513
|
id: 'rag-agent',
|
|
507
514
|
name: 'RAG Agent',
|
|
508
|
-
model: 'openai/gpt-
|
|
515
|
+
model: 'openai/gpt-6.1-sol',
|
|
509
516
|
instructions: `
|
|
510
517
|
Process queries using the provided context. Structure responses to be concise and relevant.
|
|
511
518
|
${UPSTASH_PROMPT}
|
|
@@ -518,11 +525,12 @@ export const ragAgent = new Agent({
|
|
|
518
525
|
|
|
519
526
|
```ts
|
|
520
527
|
import { VECTORIZE_PROMPT } from '@mastra/vectorize'
|
|
528
|
+
import { Agent } from '@mastra/core/agent'
|
|
521
529
|
|
|
522
530
|
export const ragAgent = new Agent({
|
|
523
531
|
id: 'rag-agent',
|
|
524
532
|
name: 'RAG Agent',
|
|
525
|
-
model: 'openai/gpt-
|
|
533
|
+
model: 'openai/gpt-6.1-sol',
|
|
526
534
|
instructions: `
|
|
527
535
|
Process queries using the provided context. Structure responses to be concise and relevant.
|
|
528
536
|
${VECTORIZE_PROMPT}
|
|
@@ -535,11 +543,12 @@ export const ragAgent = new Agent({
|
|
|
535
543
|
|
|
536
544
|
```ts
|
|
537
545
|
import { OPENSEARCH_PROMPT } from '@mastra/opensearch'
|
|
546
|
+
import { Agent } from '@mastra/core/agent'
|
|
538
547
|
|
|
539
548
|
export const ragAgent = new Agent({
|
|
540
549
|
id: 'rag-agent',
|
|
541
550
|
name: 'RAG Agent',
|
|
542
|
-
model: 'openai/gpt-
|
|
551
|
+
model: 'openai/gpt-6.1-sol',
|
|
543
552
|
instructions: `
|
|
544
553
|
Process queries using the provided context. Structure responses to be concise and relevant.
|
|
545
554
|
${OPENSEARCH_PROMPT}
|
|
@@ -552,11 +561,12 @@ export const ragAgent = new Agent({
|
|
|
552
561
|
|
|
553
562
|
```ts
|
|
554
563
|
import { ORACLEDB_PROMPT } from '@mastra/oracledb'
|
|
564
|
+
import { Agent } from '@mastra/core/agent'
|
|
555
565
|
|
|
556
566
|
export const ragAgent = new Agent({
|
|
557
567
|
id: 'rag-agent',
|
|
558
568
|
name: 'RAG Agent',
|
|
559
|
-
model: 'openai/gpt-
|
|
569
|
+
model: 'openai/gpt-6.1-sol',
|
|
560
570
|
instructions: `
|
|
561
571
|
Process queries using the provided context. Structure responses to be concise and relevant.
|
|
562
572
|
${ORACLEDB_PROMPT}
|
|
@@ -569,11 +579,12 @@ export const ragAgent = new Agent({
|
|
|
569
579
|
|
|
570
580
|
```ts
|
|
571
581
|
import { S3VECTORS_PROMPT } from '@mastra/s3vectors'
|
|
582
|
+
import { Agent } from '@mastra/core/agent'
|
|
572
583
|
|
|
573
584
|
export const ragAgent = new Agent({
|
|
574
585
|
id: 'rag-agent',
|
|
575
586
|
name: 'RAG Agent',
|
|
576
|
-
model: 'openai/gpt-
|
|
587
|
+
model: 'openai/gpt-6.1-sol',
|
|
577
588
|
instructions: `
|
|
578
589
|
Process queries using the provided context. Structure responses to be concise and relevant.
|
|
579
590
|
${S3VECTORS_PROMPT}
|
|
@@ -605,7 +616,7 @@ const initialResults = await pgVector.query({
|
|
|
605
616
|
// Create a relevance scorer
|
|
606
617
|
const relevanceProvider = new MastraAgentRelevanceScorer(
|
|
607
618
|
'relevance-scorer',
|
|
608
|
-
'openai/gpt-
|
|
619
|
+
'openai/gpt-6.1-sol',
|
|
609
620
|
)
|
|
610
621
|
|
|
611
622
|
// Re-rank the results
|
|
@@ -22,7 +22,7 @@ The AstraVector class provides vector search using [DataStax Astra DB](https://w
|
|
|
22
22
|
|
|
23
23
|
**dimension** (`number`): Vector dimension (must match your embedding model)
|
|
24
24
|
|
|
25
|
-
**metric** (`'cosine' | 'euclidean' | 'dotproduct'`): Distance metric for similarity search (maps to
|
|
25
|
+
**metric** (`'cosine' | 'euclidean' | 'dotproduct'`): Distance metric for similarity search (maps to dot_product for dotproduct) (Default: `cosine`)
|
|
26
26
|
|
|
27
27
|
### `upsert()`
|
|
28
28
|
|
package/dist/index.cjs
CHANGED
|
@@ -4,7 +4,7 @@ let _mastra_core_error = require("@mastra/core/error");
|
|
|
4
4
|
let _mastra_core_storage = require("@mastra/core/storage");
|
|
5
5
|
let _mastra_core_vector = require("@mastra/core/vector");
|
|
6
6
|
let _mastra_core_vector_filter = require("@mastra/core/vector/filter");
|
|
7
|
-
|
|
7
|
+
// src/vector/filter.ts
|
|
8
8
|
/**
|
|
9
9
|
* Translator for Astra DB filter queries.
|
|
10
10
|
* Maintains MongoDB-compatible syntax while ensuring proper validation
|
|
@@ -48,8 +48,7 @@ var AstraFilterTranslator = class extends _mastra_core_vector_filter.BaseFilterT
|
|
|
48
48
|
return this.translateNode(value);
|
|
49
49
|
}
|
|
50
50
|
};
|
|
51
|
-
|
|
52
|
-
//#region src/vector/index.ts
|
|
51
|
+
// src/vector/index.ts
|
|
53
52
|
const metricMap = {
|
|
54
53
|
cosine: "cosine",
|
|
55
54
|
euclidean: "euclidean",
|
|
@@ -314,8 +313,7 @@ var AstraVector = class extends _mastra_core_vector.MastraVector {
|
|
|
314
313
|
});
|
|
315
314
|
}
|
|
316
315
|
};
|
|
317
|
-
|
|
318
|
-
//#region src/vector/prompt.ts
|
|
316
|
+
// src/vector/prompt.ts
|
|
319
317
|
/**
|
|
320
318
|
* Vector store specific prompt that details supported operators and examples.
|
|
321
319
|
* This prompt helps users construct valid filters for Astra Vector.
|
|
@@ -407,7 +405,6 @@ Example Complex Query:
|
|
|
407
405
|
]}
|
|
408
406
|
]
|
|
409
407
|
}`;
|
|
410
|
-
//#endregion
|
|
411
408
|
exports.ASTRA_PROMPT = ASTRA_PROMPT;
|
|
412
409
|
exports.AstraVector = AstraVector;
|
|
413
410
|
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs","names":["BaseFilterTranslator","MastraVector","DataAPIClient","MastraError","createVectorErrorId","ErrorDomain","ErrorCategory","UUID"],"sources":["../src/vector/filter.ts","../src/vector/index.ts","../src/vector/prompt.ts"],"sourcesContent":["import { BaseFilterTranslator } from '@mastra/core/vector/filter';\nimport type {\n VectorFilter,\n OperatorSupport,\n QueryOperator,\n OperatorValueMap,\n LogicalOperatorValueMap,\n BlacklistedRootOperators,\n} from '@mastra/core/vector/filter';\n\ntype AstraOperatorValueMap = Omit<OperatorValueMap, '$elemMatch' | '$regex' | '$options'> & {\n $size: number; // Astra-specific\n};\n\ntype AstraLogicalOperatorValueMap = Omit<LogicalOperatorValueMap, '$nor'>;\n\ntype AstraBlacklisted = BlacklistedRootOperators | '$nor' | '$size';\n\nexport type AstraVectorFilter = VectorFilter<\n keyof AstraOperatorValueMap,\n AstraOperatorValueMap,\n AstraLogicalOperatorValueMap,\n AstraBlacklisted\n>;\n\n/**\n * Translator for Astra DB filter queries.\n * Maintains MongoDB-compatible syntax while ensuring proper validation\n * and normalization of values.\n */\nexport class AstraFilterTranslator extends BaseFilterTranslator<AstraVectorFilter> {\n protected override getSupportedOperators(): OperatorSupport {\n return {\n ...BaseFilterTranslator.DEFAULT_OPERATORS,\n array: ['$all', '$in', '$nin'],\n logical: ['$and', '$or', '$not'],\n regex: [],\n custom: ['$size'],\n };\n }\n\n translate(filter?: AstraVectorFilter): AstraVectorFilter {\n if (this.isEmpty(filter)) return filter;\n this.validateFilter(filter);\n\n return this.translateNode(filter);\n }\n\n private translateNode(node: AstraVectorFilter): any {\n // Handle primitive values and arrays\n if (this.isRegex(node)) {\n throw new Error('Regex is not supported in Astra DB');\n }\n if (this.isPrimitive(node) || Array.isArray(node)) {\n return node;\n }\n\n const entries = Object.entries(node as Record<string, any>);\n const translatedEntries = entries.map(([key, value]) => {\n // Handle operators\n if (this.isOperator(key)) {\n return [key, this.translateOperatorValue(key, value)];\n }\n\n // Handle nested paths and objects\n return [key, this.translateNode(value)];\n });\n\n return Object.fromEntries(translatedEntries);\n }\n\n private translateOperatorValue(operator: QueryOperator, value: any): any {\n if (this.isBasicOperator(operator) || this.isNumericOperator(operator)) {\n return this.normalizeComparisonValue(value);\n }\n\n if (this.isArrayOperator(operator) && Array.isArray(value)) {\n return this.normalizeArrayValues(value);\n }\n\n return this.translateNode(value);\n }\n}\n","import type { Db } from '@datastax/astra-db-ts';\nimport { DataAPIClient, UUID } from '@datastax/astra-db-ts';\nimport { MastraError, ErrorDomain, ErrorCategory } from '@mastra/core/error';\nimport { createVectorErrorId } from '@mastra/core/storage';\nimport { MastraVector } from '@mastra/core/vector';\nimport type {\n QueryResult,\n IndexStats,\n CreateIndexParams,\n UpsertVectorParams,\n QueryVectorParams,\n DescribeIndexParams,\n DeleteIndexParams,\n DeleteVectorParams,\n UpdateVectorParams,\n DeleteVectorsParams,\n} from '@mastra/core/vector';\nimport type { AstraVectorFilter } from './filter';\nimport { AstraFilterTranslator } from './filter';\n\n// Mastra and Astra DB agree on cosine and euclidean, but Astra DB uses dot_product instead of dotproduct.\nconst metricMap = {\n cosine: 'cosine',\n euclidean: 'euclidean',\n dotproduct: 'dot_product',\n} as const;\n\nexport interface AstraDbOptions {\n token: string;\n endpoint: string;\n keyspace?: string;\n}\n\ntype AstraQueryVectorParams = QueryVectorParams<AstraVectorFilter>;\n\nexport class AstraVector extends MastraVector<AstraVectorFilter> {\n readonly #db: Db;\n\n constructor({ token, endpoint, keyspace, id }: AstraDbOptions & { id: string }) {\n super({ id });\n const client = new DataAPIClient(token);\n this.#db = client.db(endpoint, { keyspace });\n }\n\n /**\n * Creates a new collection with the specified configuration.\n *\n * @param {string} indexName - The name of the collection to create.\n * @param {number} dimension - The dimension of the vectors to be stored in the collection.\n * @param {'cosine' | 'euclidean' | 'dotproduct'} [metric=cosine] - The metric to use to sort vectors in the collection.\n * @returns {Promise<void>} A promise that resolves when the collection is created.\n */\n async createIndex({ indexName, dimension, metric = 'cosine' }: CreateIndexParams): Promise<void> {\n if (!Number.isInteger(dimension) || dimension <= 0) {\n throw new MastraError({\n id: createVectorErrorId('ASTRA', 'CREATE_INDEX', 'INVALID_DIMENSION'),\n text: 'Dimension must be a positive integer',\n domain: ErrorDomain.MASTRA_VECTOR,\n category: ErrorCategory.USER,\n });\n }\n try {\n await this.#db.createCollection(indexName, {\n vector: {\n dimension,\n metric: metricMap[metric],\n },\n checkExists: false,\n });\n } catch (error: any) {\n new MastraError(\n {\n id: createVectorErrorId('ASTRA', 'CREATE_INDEX', 'DB_ERROR'),\n domain: ErrorDomain.MASTRA_VECTOR,\n category: ErrorCategory.THIRD_PARTY,\n details: { indexName },\n },\n error,\n );\n }\n }\n\n /**\n * Inserts or updates vectors in the specified collection.\n *\n * @param {string} indexName - The name of the collection to upsert into.\n * @param {number[][]} vectors - An array of vectors to upsert.\n * @param {Record<string, any>[]} [metadata] - An optional array of metadata objects corresponding to each vector.\n * @param {string[]} [ids] - An optional array of IDs corresponding to each vector. If not provided, new IDs will be generated.\n * @returns {Promise<string[]>} A promise that resolves to an array of IDs of the upserted vectors.\n */\n async upsert({ indexName, vectors, metadata, ids }: UpsertVectorParams): Promise<string[]> {\n const collection = this.#db.collection(indexName);\n\n // Generate IDs if not provided\n const vectorIds = ids || vectors.map(() => UUID.v7().toString());\n\n const records = vectors.map((vector, i) => ({\n id: vectorIds[i],\n $vector: vector,\n metadata: metadata?.[i] || {},\n }));\n\n try {\n await collection.insertMany(records);\n } catch (error: any) {\n throw new MastraError(\n {\n id: createVectorErrorId('ASTRA', 'UPSERT', 'DB_ERROR'),\n domain: ErrorDomain.MASTRA_VECTOR,\n category: ErrorCategory.THIRD_PARTY,\n details: { indexName },\n },\n error,\n );\n }\n return vectorIds;\n }\n\n transformFilter(filter?: AstraVectorFilter) {\n const translator = new AstraFilterTranslator();\n return translator.translate(filter);\n }\n\n /**\n * Queries the specified collection using a vector and optional filter.\n *\n * @param {string} indexName - The name of the collection to query.\n * @param {number[]} queryVector - The vector to query with.\n * @param {number} [topK] - The maximum number of results to return.\n * @param {Record<string, any>} [filter] - An optional filter to apply to the query. For more on filters in Astra DB, see the filtering reference: https://docs.datastax.com/en/astra-db-serverless/api-reference/documents.html#operators\n * @param {boolean} [includeVectors=false] - Whether to include the vectors in the response.\n * @returns {Promise<QueryResult[]>} A promise that resolves to an array of query results.\n */\n async query({\n indexName,\n queryVector,\n topK = 10,\n filter,\n includeVector = false,\n }: AstraQueryVectorParams): Promise<QueryResult[]> {\n if (!queryVector) {\n throw new MastraError({\n id: createVectorErrorId('ASTRA', 'QUERY', 'MISSING_VECTOR'),\n text: 'queryVector is required for Astra queries. Metadata-only queries are not supported by this vector store.',\n domain: ErrorDomain.MASTRA_VECTOR,\n category: ErrorCategory.USER,\n details: { indexName },\n });\n }\n\n const collection = this.#db.collection(indexName);\n\n const translatedFilter = this.transformFilter(filter);\n\n try {\n const cursor = collection.find(translatedFilter ?? {}, {\n sort: { $vector: queryVector },\n limit: topK,\n includeSimilarity: true,\n projection: {\n $vector: !!includeVector,\n },\n });\n\n const results = await cursor.toArray();\n\n return results.map(result => ({\n id: result.id,\n score: result.$similarity,\n metadata: result.metadata,\n ...(includeVector && { vector: result.$vector }),\n }));\n } catch (error: any) {\n throw new MastraError(\n {\n id: createVectorErrorId('ASTRA', 'QUERY', 'DB_ERROR'),\n domain: ErrorDomain.MASTRA_VECTOR,\n category: ErrorCategory.THIRD_PARTY,\n details: { indexName },\n },\n error,\n );\n }\n }\n\n /**\n * Lists all collections in the database.\n *\n * @returns {Promise<string[]>} A promise that resolves to an array of collection names.\n */\n async listIndexes(): Promise<string[]> {\n try {\n return await this.#db.listCollections({ nameOnly: true });\n } catch (error: any) {\n throw new MastraError(\n {\n id: createVectorErrorId('ASTRA', 'LIST_INDEXES', 'DB_ERROR'),\n domain: ErrorDomain.MASTRA_VECTOR,\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 const collection = this.#db.collection(indexName);\n try {\n const optionsPromise = collection.options();\n const countPromise = collection.countDocuments({}, 100);\n const [options, count] = await Promise.all([optionsPromise, countPromise]);\n\n const keys = Object.keys(metricMap) as (keyof typeof metricMap)[];\n const metric = keys.find(key => metricMap[key] === options.vector?.metric);\n return {\n dimension: options.vector?.dimension!,\n metric,\n count: count,\n };\n } catch (error: any) {\n if (error instanceof MastraError) throw error;\n throw new MastraError(\n {\n id: createVectorErrorId('ASTRA', 'DESCRIBE_INDEX', 'DB_ERROR'),\n domain: ErrorDomain.MASTRA_VECTOR,\n category: ErrorCategory.THIRD_PARTY,\n details: { indexName },\n },\n error,\n );\n }\n }\n\n /**\n * Deletes the specified collection.\n *\n * @param {string} indexName - The name of the collection to delete.\n * @returns {Promise<void>} A promise that resolves when the collection is deleted.\n */\n async deleteIndex({ indexName }: DeleteIndexParams): Promise<void> {\n const collection = this.#db.collection(indexName);\n try {\n await collection.drop();\n } catch (error: any) {\n throw new MastraError(\n {\n id: createVectorErrorId('ASTRA', 'DELETE_INDEX', 'DB_ERROR'),\n domain: ErrorDomain.MASTRA_VECTOR,\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 (!id) {\n throw new MastraError({\n id: createVectorErrorId('ASTRA', 'UPDATE_VECTOR', 'NO_ID'),\n text: 'id is required for Astra updateVector',\n domain: ErrorDomain.MASTRA_VECTOR,\n category: ErrorCategory.USER,\n details: { indexName },\n });\n }\n\n if (!update.vector && !update.metadata) {\n throw new MastraError({\n id: createVectorErrorId('ASTRA', 'UPDATE_VECTOR', 'NO_PAYLOAD'),\n text: 'No updates provided for vector',\n domain: ErrorDomain.MASTRA_VECTOR,\n category: ErrorCategory.USER,\n details: {\n indexName,\n id,\n },\n });\n }\n\n try {\n const collection = this.#db.collection(indexName);\n const updateDoc: Record<string, any> = {};\n\n if (update.vector) {\n updateDoc.$vector = update.vector;\n }\n\n if (update.metadata) {\n updateDoc.metadata = update.metadata;\n }\n\n await collection.findOneAndUpdate({ id }, { $set: updateDoc });\n } catch (error: any) {\n if (error instanceof MastraError) throw error;\n throw new MastraError(\n {\n id: createVectorErrorId('ASTRA', 'UPDATE_VECTOR', 'FAILED_UNHANDLED'),\n domain: ErrorDomain.MASTRA_VECTOR,\n category: ErrorCategory.THIRD_PARTY,\n details: {\n indexName,\n ...(id && { id }),\n },\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 const collection = this.#db.collection(indexName);\n await collection.deleteOne({ id });\n } catch (error: any) {\n if (error instanceof MastraError) throw error;\n throw new MastraError(\n {\n id: createVectorErrorId('ASTRA', 'DELETE_VECTOR', 'FAILED'),\n domain: ErrorDomain.MASTRA_VECTOR,\n category: ErrorCategory.THIRD_PARTY,\n details: {\n indexName,\n ...(id && { id }),\n },\n },\n error,\n );\n }\n }\n\n async deleteVectors({ indexName, filter, ids }: DeleteVectorsParams): Promise<void> {\n throw new MastraError({\n id: createVectorErrorId('ASTRA', 'DELETE_VECTORS', 'NOT_SUPPORTED'),\n text: 'deleteVectors is not yet implemented for Astra vector store',\n domain: ErrorDomain.MASTRA_VECTOR,\n category: ErrorCategory.SYSTEM,\n details: {\n indexName,\n ...(filter && { filter: JSON.stringify(filter) }),\n ...(ids && { idsCount: ids.length }),\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 Astra Vector.\n */\nexport const ASTRA_PROMPT = `When querying Astra, 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- $all: Match all values in array\n Example: { \"tags\": { \"$all\": [\"premium\", \"sale\"] } }\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- $not: Logical NOT\n Example: { \"$not\": { \"category\": \"electronics\" } }\n\nElement Operators:\n- $exists: Check if field exists\n Example: { \"rating\": { \"$exists\": true } }\n\nSpecial Operators:\n- $size: Array length check\n Example: { \"tags\": { \"$size\": 2 } }\n\nRestrictions:\n- Regex patterns are not supported\n- Only $and, $or, and $not logical operators are supported\n- Nested fields are supported using dot notation\n- Multiple conditions on the same field are supported with both implicit and explicit $and\n- Empty arrays in $in/$nin will return no results\n- A non-empty array is required for $all operator\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- Other 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 { \"tags\": { \"$all\": [\"premium\"] } },\n { \"rating\": { \"$exists\": true, \"$gt\": 4 } },\n { \"$or\": [\n { \"stock\": { \"$gt\": 0 } },\n { \"preorder\": true }\n ]}\n ]\n}`;\n"],"mappings":";;;;;;;;;;;;AA8BA,IAAa,wBAAb,cAA2CA,2BAAAA,qBAAwC;CACjF,wBAA4D;EAC1D,OAAO;GACL,GAAGA,2BAAAA,qBAAqB;GACxB,OAAO;IAAC;IAAQ;IAAO;GAAM;GAC7B,SAAS;IAAC;IAAQ;IAAO;GAAM;GAC/B,OAAO,CAAC;GACR,QAAQ,CAAC,OAAO;EAClB;CACF;CAEA,UAAU,QAA+C;EACvD,IAAI,KAAK,QAAQ,MAAM,GAAG,OAAO;EACjC,KAAK,eAAe,MAAM;EAE1B,OAAO,KAAK,cAAc,MAAM;CAClC;CAEA,cAAsB,MAA8B;EAElD,IAAI,KAAK,QAAQ,IAAI,GACnB,MAAM,IAAI,MAAM,oCAAoC;EAEtD,IAAI,KAAK,YAAY,IAAI,KAAK,MAAM,QAAQ,IAAI,GAC9C,OAAO;EAIT,MAAM,oBADU,OAAO,QAAQ,IACC,CAAC,CAAC,KAAK,CAAC,KAAK,WAAW;GAEtD,IAAI,KAAK,WAAW,GAAG,GACrB,OAAO,CAAC,KAAK,KAAK,uBAAuB,KAAK,KAAK,CAAC;GAItD,OAAO,CAAC,KAAK,KAAK,cAAc,KAAK,CAAC;EACxC,CAAC;EAED,OAAO,OAAO,YAAY,iBAAiB;CAC7C;CAEA,uBAA+B,UAAyB,OAAiB;EACvE,IAAI,KAAK,gBAAgB,QAAQ,KAAK,KAAK,kBAAkB,QAAQ,GACnE,OAAO,KAAK,yBA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F,GACA,KACF;EACF;CACF;;;;;;;;CASA,MAAM,aAAa,EAAE,WAAW,MAAyC;EACvE,IAAI;GAEF,MADmB,KAAK,IAAI,WAAW,SACxB,CAAC,CAAC,UAAU,EAAE,GAAG,CAAC;EACnC,SAAS,OAAY;GACnB,IAAI,iBAAiBH,mBAAAA,aAAa,MAAM;GACxC,MAAM,IAAIA,mBAAAA,YACR;IACE,KAAA,GAAIC,qBAAAA,oBAAAA,CAAoB,SAAS,iBAAiB,QAAQ;IAC1D,QAAQC,mBAAAA,YAAY;IACpB,UAAUC,mBAAAA,cAAc;IACxB,SAAS;KACP;KACA,GAAI,MAAM,EAAE,GAAG;IACjB;GACF,GACA,KACF;EACF;CACF;CAEA,MAAM,cAAc,EAAE,WAAW,QAAQ,OAA2C;EAClF,MAAM,IAAIH,mBAAAA,YAAY;GACpB,KAAA,GAAIC,qBAAAA,oBAAAA,CAAoB,SAAS,kBAAkB,eAAe;GAClE,MAAM;GACN,QAAQC,mBAAAA,YAAY;GACpB,UAAUC,mBAAAA,cAAc;GACxB,SAAS;IACP;IACA,GAAI,UAAU,EAAE,QAAQ,KAAK,UAAU,MAAM,EAAE;IAC/C,GAAI,OAAO,EAAE,UAAU,IAAI,OAAO;GACpC;EACF,CAAC;CACH;AACF;;;;;;;AC5WA,MAAa,eAAe"}
|
|
1
|
+
{"version":3,"file":"index.cjs","names":["BaseFilterTranslator","MastraVector","DataAPIClient","MastraError","createVectorErrorId","ErrorDomain","ErrorCategory","UUID"],"sources":["../src/vector/filter.ts","../src/vector/index.ts","../src/vector/prompt.ts"],"sourcesContent":["import { BaseFilterTranslator } from '@mastra/core/vector/filter';\nimport type {\n VectorFilter,\n OperatorSupport,\n QueryOperator,\n OperatorValueMap,\n LogicalOperatorValueMap,\n BlacklistedRootOperators,\n} from '@mastra/core/vector/filter';\n\ntype AstraOperatorValueMap = Omit<OperatorValueMap, '$elemMatch' | '$regex' | '$options'> & {\n $size: number; // Astra-specific\n};\n\ntype AstraLogicalOperatorValueMap = Omit<LogicalOperatorValueMap, '$nor'>;\n\ntype AstraBlacklisted = BlacklistedRootOperators | '$nor' | '$size';\n\nexport type AstraVectorFilter = VectorFilter<\n keyof AstraOperatorValueMap,\n AstraOperatorValueMap,\n AstraLogicalOperatorValueMap,\n AstraBlacklisted\n>;\n\n/**\n * Translator for Astra DB filter queries.\n * Maintains MongoDB-compatible syntax while ensuring proper validation\n * and normalization of values.\n */\nexport class AstraFilterTranslator extends BaseFilterTranslator<AstraVectorFilter> {\n protected override getSupportedOperators(): OperatorSupport {\n return {\n ...BaseFilterTranslator.DEFAULT_OPERATORS,\n array: ['$all', '$in', '$nin'],\n logical: ['$and', '$or', '$not'],\n regex: [],\n custom: ['$size'],\n };\n }\n\n translate(filter?: AstraVectorFilter): AstraVectorFilter {\n if (this.isEmpty(filter)) return filter;\n this.validateFilter(filter);\n\n return this.translateNode(filter);\n }\n\n private translateNode(node: AstraVectorFilter): any {\n // Handle primitive values and arrays\n if (this.isRegex(node)) {\n throw new Error('Regex is not supported in Astra DB');\n }\n if (this.isPrimitive(node) || Array.isArray(node)) {\n return node;\n }\n\n const entries = Object.entries(node as Record<string, any>);\n const translatedEntries = entries.map(([key, value]) => {\n // Handle operators\n if (this.isOperator(key)) {\n return [key, this.translateOperatorValue(key, value)];\n }\n\n // Handle nested paths and objects\n return [key, this.translateNode(value)];\n });\n\n return Object.fromEntries(translatedEntries);\n }\n\n private translateOperatorValue(operator: QueryOperator, value: any): any {\n if (this.isBasicOperator(operator) || this.isNumericOperator(operator)) {\n return this.normalizeComparisonValue(value);\n }\n\n if (this.isArrayOperator(operator) && Array.isArray(value)) {\n return this.normalizeArrayValues(value);\n }\n\n return this.translateNode(value);\n }\n}\n","import type { Db } from '@datastax/astra-db-ts';\nimport { DataAPIClient, UUID } from '@datastax/astra-db-ts';\nimport { MastraError, ErrorDomain, ErrorCategory } from '@mastra/core/error';\nimport { createVectorErrorId } from '@mastra/core/storage';\nimport { MastraVector } from '@mastra/core/vector';\nimport type {\n QueryResult,\n IndexStats,\n CreateIndexParams,\n UpsertVectorParams,\n QueryVectorParams,\n DescribeIndexParams,\n DeleteIndexParams,\n DeleteVectorParams,\n UpdateVectorParams,\n DeleteVectorsParams,\n} from '@mastra/core/vector';\nimport type { AstraVectorFilter } from './filter';\nimport { AstraFilterTranslator } from './filter';\n\n// Mastra and Astra DB agree on cosine and euclidean, but Astra DB uses dot_product instead of dotproduct.\nconst metricMap = {\n cosine: 'cosine',\n euclidean: 'euclidean',\n dotproduct: 'dot_product',\n} as const;\n\nexport interface AstraDbOptions {\n token: string;\n endpoint: string;\n keyspace?: string;\n}\n\ntype AstraQueryVectorParams = QueryVectorParams<AstraVectorFilter>;\n\nexport class AstraVector extends MastraVector<AstraVectorFilter> {\n readonly #db: Db;\n\n constructor({ token, endpoint, keyspace, id }: AstraDbOptions & { id: string }) {\n super({ id });\n const client = new DataAPIClient(token);\n this.#db = client.db(endpoint, { keyspace });\n }\n\n /**\n * Creates a new collection with the specified configuration.\n *\n * @param {string} indexName - The name of the collection to create.\n * @param {number} dimension - The dimension of the vectors to be stored in the collection.\n * @param {'cosine' | 'euclidean' | 'dotproduct'} [metric=cosine] - The metric to use to sort vectors in the collection.\n * @returns {Promise<void>} A promise that resolves when the collection is created.\n */\n async createIndex({ indexName, dimension, metric = 'cosine' }: CreateIndexParams): Promise<void> {\n if (!Number.isInteger(dimension) || dimension <= 0) {\n throw new MastraError({\n id: createVectorErrorId('ASTRA', 'CREATE_INDEX', 'INVALID_DIMENSION'),\n text: 'Dimension must be a positive integer',\n domain: ErrorDomain.MASTRA_VECTOR,\n category: ErrorCategory.USER,\n });\n }\n try {\n await this.#db.createCollection(indexName, {\n vector: {\n dimension,\n metric: metricMap[metric],\n },\n checkExists: false,\n });\n } catch (error: any) {\n new MastraError(\n {\n id: createVectorErrorId('ASTRA', 'CREATE_INDEX', 'DB_ERROR'),\n domain: ErrorDomain.MASTRA_VECTOR,\n category: ErrorCategory.THIRD_PARTY,\n details: { indexName },\n },\n error,\n );\n }\n }\n\n /**\n * Inserts or updates vectors in the specified collection.\n *\n * @param {string} indexName - The name of the collection to upsert into.\n * @param {number[][]} vectors - An array of vectors to upsert.\n * @param {Record<string, any>[]} [metadata] - An optional array of metadata objects corresponding to each vector.\n * @param {string[]} [ids] - An optional array of IDs corresponding to each vector. If not provided, new IDs will be generated.\n * @returns {Promise<string[]>} A promise that resolves to an array of IDs of the upserted vectors.\n */\n async upsert({ indexName, vectors, metadata, ids }: UpsertVectorParams): Promise<string[]> {\n const collection = this.#db.collection(indexName);\n\n // Generate IDs if not provided\n const vectorIds = ids || vectors.map(() => UUID.v7().toString());\n\n const records = vectors.map((vector, i) => ({\n id: vectorIds[i],\n $vector: vector,\n metadata: metadata?.[i] || {},\n }));\n\n try {\n await collection.insertMany(records);\n } catch (error: any) {\n throw new MastraError(\n {\n id: createVectorErrorId('ASTRA', 'UPSERT', 'DB_ERROR'),\n domain: ErrorDomain.MASTRA_VECTOR,\n category: ErrorCategory.THIRD_PARTY,\n details: { indexName },\n },\n error,\n );\n }\n return vectorIds;\n }\n\n transformFilter(filter?: AstraVectorFilter) {\n const translator = new AstraFilterTranslator();\n return translator.translate(filter);\n }\n\n /**\n * Queries the specified collection using a vector and optional filter.\n *\n * @param {string} indexName - The name of the collection to query.\n * @param {number[]} queryVector - The vector to query with.\n * @param {number} [topK] - The maximum number of results to return.\n * @param {Record<string, any>} [filter] - An optional filter to apply to the query. For more on filters in Astra DB, see the filtering reference: https://docs.datastax.com/en/astra-db-serverless/api-reference/documents.html#operators\n * @param {boolean} [includeVectors=false] - Whether to include the vectors in the response.\n * @returns {Promise<QueryResult[]>} A promise that resolves to an array of query results.\n */\n async query({\n indexName,\n queryVector,\n topK = 10,\n filter,\n includeVector = false,\n }: AstraQueryVectorParams): Promise<QueryResult[]> {\n if (!queryVector) {\n throw new MastraError({\n id: createVectorErrorId('ASTRA', 'QUERY', 'MISSING_VECTOR'),\n text: 'queryVector is required for Astra queries. Metadata-only queries are not supported by this vector store.',\n domain: ErrorDomain.MASTRA_VECTOR,\n category: ErrorCategory.USER,\n details: { indexName },\n });\n }\n\n const collection = this.#db.collection(indexName);\n\n const translatedFilter = this.transformFilter(filter);\n\n try {\n const cursor = collection.find(translatedFilter ?? {}, {\n sort: { $vector: queryVector },\n limit: topK,\n includeSimilarity: true,\n projection: {\n $vector: !!includeVector,\n },\n });\n\n const results = await cursor.toArray();\n\n return results.map(result => ({\n id: result.id,\n score: result.$similarity,\n metadata: result.metadata,\n ...(includeVector && { vector: result.$vector }),\n }));\n } catch (error: any) {\n throw new MastraError(\n {\n id: createVectorErrorId('ASTRA', 'QUERY', 'DB_ERROR'),\n domain: ErrorDomain.MASTRA_VECTOR,\n category: ErrorCategory.THIRD_PARTY,\n details: { indexName },\n },\n error,\n );\n }\n }\n\n /**\n * Lists all collections in the database.\n *\n * @returns {Promise<string[]>} A promise that resolves to an array of collection names.\n */\n async listIndexes(): Promise<string[]> {\n try {\n return await this.#db.listCollections({ nameOnly: true });\n } catch (error: any) {\n throw new MastraError(\n {\n id: createVectorErrorId('ASTRA', 'LIST_INDEXES', 'DB_ERROR'),\n domain: ErrorDomain.MASTRA_VECTOR,\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 const collection = this.#db.collection(indexName);\n try {\n const optionsPromise = collection.options();\n const countPromise = collection.countDocuments({}, 100);\n const [options, count] = await Promise.all([optionsPromise, countPromise]);\n\n const keys = Object.keys(metricMap) as (keyof typeof metricMap)[];\n const metric = keys.find(key => metricMap[key] === options.vector?.metric);\n return {\n dimension: options.vector?.dimension!,\n metric,\n count: count,\n };\n } catch (error: any) {\n if (error instanceof MastraError) throw error;\n throw new MastraError(\n {\n id: createVectorErrorId('ASTRA', 'DESCRIBE_INDEX', 'DB_ERROR'),\n domain: ErrorDomain.MASTRA_VECTOR,\n category: ErrorCategory.THIRD_PARTY,\n details: { indexName },\n },\n error,\n );\n }\n }\n\n /**\n * Deletes the specified collection.\n *\n * @param {string} indexName - The name of the collection to delete.\n * @returns {Promise<void>} A promise that resolves when the collection is deleted.\n */\n async deleteIndex({ indexName }: DeleteIndexParams): Promise<void> {\n const collection = this.#db.collection(indexName);\n try {\n await collection.drop();\n } catch (error: any) {\n throw new MastraError(\n {\n id: createVectorErrorId('ASTRA', 'DELETE_INDEX', 'DB_ERROR'),\n domain: ErrorDomain.MASTRA_VECTOR,\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 (!id) {\n throw new MastraError({\n id: createVectorErrorId('ASTRA', 'UPDATE_VECTOR', 'NO_ID'),\n text: 'id is required for Astra updateVector',\n domain: ErrorDomain.MASTRA_VECTOR,\n category: ErrorCategory.USER,\n details: { indexName },\n });\n }\n\n if (!update.vector && !update.metadata) {\n throw new MastraError({\n id: createVectorErrorId('ASTRA', 'UPDATE_VECTOR', 'NO_PAYLOAD'),\n text: 'No updates provided for vector',\n domain: ErrorDomain.MASTRA_VECTOR,\n category: ErrorCategory.USER,\n details: {\n indexName,\n id,\n },\n });\n }\n\n try {\n const collection = this.#db.collection(indexName);\n const updateDoc: Record<string, any> = {};\n\n if (update.vector) {\n updateDoc.$vector = update.vector;\n }\n\n if (update.metadata) {\n updateDoc.metadata = update.metadata;\n }\n\n await collection.findOneAndUpdate({ id }, { $set: updateDoc });\n } catch (error: any) {\n if (error instanceof MastraError) throw error;\n throw new MastraError(\n {\n id: createVectorErrorId('ASTRA', 'UPDATE_VECTOR', 'FAILED_UNHANDLED'),\n domain: ErrorDomain.MASTRA_VECTOR,\n category: ErrorCategory.THIRD_PARTY,\n details: {\n indexName,\n ...(id && { id }),\n },\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 const collection = this.#db.collection(indexName);\n await collection.deleteOne({ id });\n } catch (error: any) {\n if (error instanceof MastraError) throw error;\n throw new MastraError(\n {\n id: createVectorErrorId('ASTRA', 'DELETE_VECTOR', 'FAILED'),\n domain: ErrorDomain.MASTRA_VECTOR,\n category: ErrorCategory.THIRD_PARTY,\n details: {\n indexName,\n ...(id && { id }),\n },\n },\n error,\n );\n }\n }\n\n async deleteVectors({ indexName, filter, ids }: DeleteVectorsParams): Promise<void> {\n throw new MastraError({\n id: createVectorErrorId('ASTRA', 'DELETE_VECTORS', 'NOT_SUPPORTED'),\n text: 'deleteVectors is not yet implemented for Astra vector store',\n domain: ErrorDomain.MASTRA_VECTOR,\n category: ErrorCategory.SYSTEM,\n details: {\n indexName,\n ...(filter && { filter: JSON.stringify(filter) }),\n ...(ids && { idsCount: ids.length }),\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 Astra Vector.\n */\nexport const ASTRA_PROMPT = `When querying Astra, 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- $all: Match all values in array\n Example: { \"tags\": { \"$all\": [\"premium\", \"sale\"] } }\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- $not: Logical NOT\n Example: { \"$not\": { \"category\": \"electronics\" } }\n\nElement Operators:\n- $exists: Check if field exists\n Example: { \"rating\": { \"$exists\": true } }\n\nSpecial Operators:\n- $size: Array length check\n Example: { \"tags\": { \"$size\": 2 } }\n\nRestrictions:\n- Regex patterns are not supported\n- Only $and, $or, and $not logical operators are supported\n- Nested fields are supported using dot notation\n- Multiple conditions on the same field are supported with both implicit and explicit $and\n- Empty arrays in $in/$nin will return no results\n- A non-empty array is required for $all operator\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- Other 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 { \"tags\": { \"$all\": [\"premium\"] } },\n { \"rating\": { \"$exists\": true, \"$gt\": 4 } },\n { \"$or\": [\n { \"stock\": { \"$gt\": 0 } },\n { \"preorder\": true }\n ]}\n ]\n}`;\n"],"mappings":";;;;;;;;;;;;AA8BA,IAAa,wBAAb,cAA2CA,2BAAAA,qBAAwC;CACjF,wBAA4D;EAC1D,OAAO;GACL,GAAGA,2BAAAA,qBAAqB;GACxB,OAAO;IAAC;IAAQ;IAAO;GAAM;GAC7B,SAAS;IAAC;IAAQ;IAAO;GAAM;GAC/B,OAAO,CAAC;GACR,QAAQ,CAAC,OAAO;EAClB;CACF;CAEA,UAAU,QAA+C;EACvD,IAAI,KAAK,QAAQ,MAAM,GAAG,OAAO;EACjC,KAAK,eAAe,MAAM;EAE1B,OAAO,KAAK,cAAc,MAAM;CAClC;CAEA,cAAsB,MAA8B;EAElD,IAAI,KAAK,QAAQ,IAAI,GACnB,MAAM,IAAI,MAAM,oCAAoC;EAEtD,IAAI,KAAK,YAAY,IAAI,KAAK,MAAM,QAAQ,IAAI,GAC9C,OAAO;EAIT,MAAM,oBADU,OAAO,QAAQ,IACC,CAAC,CAAC,KAAK,CAAC,KAAK,WAAW;GAEtD,IAAI,KAAK,WAAW,GAAG,GACr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,SAAS,OAAY;GACnB,IAAI,iBAAiBH,mBAAAA,aAAa,MAAM;GACxC,MAAM,IAAIA,mBAAAA,YACR;IACE,KAAA,GAAIC,qBAAAA,oBAAAA,CAAoB,SAAS,iBAAiB,kBAAkB;IACpE,QAAQC,mBAAAA,YAAY;IACpB,UAAUC,mBAAAA,cAAc;IACxB,SAAS;KACP;KACA,GAAI,MAAM,EAAE,GAAG;IACjB;GACF,GACA,KACF;EACF;CACF;;;;;;;;CASA,MAAM,aAAa,EAAE,WAAW,MAAyC;EACvE,IAAI;GAEF,MADmB,KAAK,IAAI,WAAW,SACxB,CAAC,CAAC,UAAU,EAAE,GAAG,CAAC;EACnC,SAAS,OAAY;GACnB,IAAI,iBAAiBH,mBAAAA,aAAa,MAAM;GACxC,MAAM,IAAIA,mBAAAA,YACR;IACE,KAAA,GAAIC,qBAAAA,oBAAAA,CAAoB,SAAS,iBAAiB,QAAQ;IAC1D,QAAQC,mBAAAA,YAAY;IACpB,UAAUC,mBAAAA,cAAc;IACxB,SAAS;KACP;KACA,GAAI,MAAM,EAAE,GAAG;IACjB;GACF,GACA,KACF;EACF;CACF;CAEA,MAAM,cAAc,EAAE,WAAW,QAAQ,OAA2C;EAClF,MAAM,IAAIH,mBAAAA,YAAY;GACpB,KAAA,GAAIC,qBAAAA,oBAAAA,CAAoB,SAAS,kBAAkB,eAAe;GAClE,MAAM;GACN,QAAQC,mBAAAA,YAAY;GACpB,UAAUC,mBAAAA,cAAc;GACxB,SAAS;IACP;IACA,GAAI,UAAU,EAAE,QAAQ,KAAK,UAAU,MAAM,EAAE;IAC/C,GAAI,OAAO,EAAE,UAAU,IAAI,OAAO;GACpC;EACF,CAAC;CACH;AACF;;;;;;AC5WA,MAAa,eAAe"}
|
package/dist/index.js
CHANGED
|
@@ -3,7 +3,7 @@ import { ErrorCategory, ErrorDomain, MastraError } from "@mastra/core/error";
|
|
|
3
3
|
import { createVectorErrorId } from "@mastra/core/storage";
|
|
4
4
|
import { MastraVector } from "@mastra/core/vector";
|
|
5
5
|
import { BaseFilterTranslator } from "@mastra/core/vector/filter";
|
|
6
|
-
|
|
6
|
+
// src/vector/filter.ts
|
|
7
7
|
/**
|
|
8
8
|
* Translator for Astra DB filter queries.
|
|
9
9
|
* Maintains MongoDB-compatible syntax while ensuring proper validation
|
|
@@ -47,8 +47,7 @@ var AstraFilterTranslator = class extends BaseFilterTranslator {
|
|
|
47
47
|
return this.translateNode(value);
|
|
48
48
|
}
|
|
49
49
|
};
|
|
50
|
-
|
|
51
|
-
//#region src/vector/index.ts
|
|
50
|
+
// src/vector/index.ts
|
|
52
51
|
const metricMap = {
|
|
53
52
|
cosine: "cosine",
|
|
54
53
|
euclidean: "euclidean",
|
|
@@ -313,8 +312,7 @@ var AstraVector = class extends MastraVector {
|
|
|
313
312
|
});
|
|
314
313
|
}
|
|
315
314
|
};
|
|
316
|
-
|
|
317
|
-
//#region src/vector/prompt.ts
|
|
315
|
+
// src/vector/prompt.ts
|
|
318
316
|
/**
|
|
319
317
|
* Vector store specific prompt that details supported operators and examples.
|
|
320
318
|
* This prompt helps users construct valid filters for Astra Vector.
|
|
@@ -406,7 +404,6 @@ Example Complex Query:
|
|
|
406
404
|
]}
|
|
407
405
|
]
|
|
408
406
|
}`;
|
|
409
|
-
//#endregion
|
|
410
407
|
export { ASTRA_PROMPT, AstraVector };
|
|
411
408
|
|
|
412
409
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":[],"sources":["../src/vector/filter.ts","../src/vector/index.ts","../src/vector/prompt.ts"],"sourcesContent":["import { BaseFilterTranslator } from '@mastra/core/vector/filter';\nimport type {\n VectorFilter,\n OperatorSupport,\n QueryOperator,\n OperatorValueMap,\n LogicalOperatorValueMap,\n BlacklistedRootOperators,\n} from '@mastra/core/vector/filter';\n\ntype AstraOperatorValueMap = Omit<OperatorValueMap, '$elemMatch' | '$regex' | '$options'> & {\n $size: number; // Astra-specific\n};\n\ntype AstraLogicalOperatorValueMap = Omit<LogicalOperatorValueMap, '$nor'>;\n\ntype AstraBlacklisted = BlacklistedRootOperators | '$nor' | '$size';\n\nexport type AstraVectorFilter = VectorFilter<\n keyof AstraOperatorValueMap,\n AstraOperatorValueMap,\n AstraLogicalOperatorValueMap,\n AstraBlacklisted\n>;\n\n/**\n * Translator for Astra DB filter queries.\n * Maintains MongoDB-compatible syntax while ensuring proper validation\n * and normalization of values.\n */\nexport class AstraFilterTranslator extends BaseFilterTranslator<AstraVectorFilter> {\n protected override getSupportedOperators(): OperatorSupport {\n return {\n ...BaseFilterTranslator.DEFAULT_OPERATORS,\n array: ['$all', '$in', '$nin'],\n logical: ['$and', '$or', '$not'],\n regex: [],\n custom: ['$size'],\n };\n }\n\n translate(filter?: AstraVectorFilter): AstraVectorFilter {\n if (this.isEmpty(filter)) return filter;\n this.validateFilter(filter);\n\n return this.translateNode(filter);\n }\n\n private translateNode(node: AstraVectorFilter): any {\n // Handle primitive values and arrays\n if (this.isRegex(node)) {\n throw new Error('Regex is not supported in Astra DB');\n }\n if (this.isPrimitive(node) || Array.isArray(node)) {\n return node;\n }\n\n const entries = Object.entries(node as Record<string, any>);\n const translatedEntries = entries.map(([key, value]) => {\n // Handle operators\n if (this.isOperator(key)) {\n return [key, this.translateOperatorValue(key, value)];\n }\n\n // Handle nested paths and objects\n return [key, this.translateNode(value)];\n });\n\n return Object.fromEntries(translatedEntries);\n }\n\n private translateOperatorValue(operator: QueryOperator, value: any): any {\n if (this.isBasicOperator(operator) || this.isNumericOperator(operator)) {\n return this.normalizeComparisonValue(value);\n }\n\n if (this.isArrayOperator(operator) && Array.isArray(value)) {\n return this.normalizeArrayValues(value);\n }\n\n return this.translateNode(value);\n }\n}\n","import type { Db } from '@datastax/astra-db-ts';\nimport { DataAPIClient, UUID } from '@datastax/astra-db-ts';\nimport { MastraError, ErrorDomain, ErrorCategory } from '@mastra/core/error';\nimport { createVectorErrorId } from '@mastra/core/storage';\nimport { MastraVector } from '@mastra/core/vector';\nimport type {\n QueryResult,\n IndexStats,\n CreateIndexParams,\n UpsertVectorParams,\n QueryVectorParams,\n DescribeIndexParams,\n DeleteIndexParams,\n DeleteVectorParams,\n UpdateVectorParams,\n DeleteVectorsParams,\n} from '@mastra/core/vector';\nimport type { AstraVectorFilter } from './filter';\nimport { AstraFilterTranslator } from './filter';\n\n// Mastra and Astra DB agree on cosine and euclidean, but Astra DB uses dot_product instead of dotproduct.\nconst metricMap = {\n cosine: 'cosine',\n euclidean: 'euclidean',\n dotproduct: 'dot_product',\n} as const;\n\nexport interface AstraDbOptions {\n token: string;\n endpoint: string;\n keyspace?: string;\n}\n\ntype AstraQueryVectorParams = QueryVectorParams<AstraVectorFilter>;\n\nexport class AstraVector extends MastraVector<AstraVectorFilter> {\n readonly #db: Db;\n\n constructor({ token, endpoint, keyspace, id }: AstraDbOptions & { id: string }) {\n super({ id });\n const client = new DataAPIClient(token);\n this.#db = client.db(endpoint, { keyspace });\n }\n\n /**\n * Creates a new collection with the specified configuration.\n *\n * @param {string} indexName - The name of the collection to create.\n * @param {number} dimension - The dimension of the vectors to be stored in the collection.\n * @param {'cosine' | 'euclidean' | 'dotproduct'} [metric=cosine] - The metric to use to sort vectors in the collection.\n * @returns {Promise<void>} A promise that resolves when the collection is created.\n */\n async createIndex({ indexName, dimension, metric = 'cosine' }: CreateIndexParams): Promise<void> {\n if (!Number.isInteger(dimension) || dimension <= 0) {\n throw new MastraError({\n id: createVectorErrorId('ASTRA', 'CREATE_INDEX', 'INVALID_DIMENSION'),\n text: 'Dimension must be a positive integer',\n domain: ErrorDomain.MASTRA_VECTOR,\n category: ErrorCategory.USER,\n });\n }\n try {\n await this.#db.createCollection(indexName, {\n vector: {\n dimension,\n metric: metricMap[metric],\n },\n checkExists: false,\n });\n } catch (error: any) {\n new MastraError(\n {\n id: createVectorErrorId('ASTRA', 'CREATE_INDEX', 'DB_ERROR'),\n domain: ErrorDomain.MASTRA_VECTOR,\n category: ErrorCategory.THIRD_PARTY,\n details: { indexName },\n },\n error,\n );\n }\n }\n\n /**\n * Inserts or updates vectors in the specified collection.\n *\n * @param {string} indexName - The name of the collection to upsert into.\n * @param {number[][]} vectors - An array of vectors to upsert.\n * @param {Record<string, any>[]} [metadata] - An optional array of metadata objects corresponding to each vector.\n * @param {string[]} [ids] - An optional array of IDs corresponding to each vector. If not provided, new IDs will be generated.\n * @returns {Promise<string[]>} A promise that resolves to an array of IDs of the upserted vectors.\n */\n async upsert({ indexName, vectors, metadata, ids }: UpsertVectorParams): Promise<string[]> {\n const collection = this.#db.collection(indexName);\n\n // Generate IDs if not provided\n const vectorIds = ids || vectors.map(() => UUID.v7().toString());\n\n const records = vectors.map((vector, i) => ({\n id: vectorIds[i],\n $vector: vector,\n metadata: metadata?.[i] || {},\n }));\n\n try {\n await collection.insertMany(records);\n } catch (error: any) {\n throw new MastraError(\n {\n id: createVectorErrorId('ASTRA', 'UPSERT', 'DB_ERROR'),\n domain: ErrorDomain.MASTRA_VECTOR,\n category: ErrorCategory.THIRD_PARTY,\n details: { indexName },\n },\n error,\n );\n }\n return vectorIds;\n }\n\n transformFilter(filter?: AstraVectorFilter) {\n const translator = new AstraFilterTranslator();\n return translator.translate(filter);\n }\n\n /**\n * Queries the specified collection using a vector and optional filter.\n *\n * @param {string} indexName - The name of the collection to query.\n * @param {number[]} queryVector - The vector to query with.\n * @param {number} [topK] - The maximum number of results to return.\n * @param {Record<string, any>} [filter] - An optional filter to apply to the query. For more on filters in Astra DB, see the filtering reference: https://docs.datastax.com/en/astra-db-serverless/api-reference/documents.html#operators\n * @param {boolean} [includeVectors=false] - Whether to include the vectors in the response.\n * @returns {Promise<QueryResult[]>} A promise that resolves to an array of query results.\n */\n async query({\n indexName,\n queryVector,\n topK = 10,\n filter,\n includeVector = false,\n }: AstraQueryVectorParams): Promise<QueryResult[]> {\n if (!queryVector) {\n throw new MastraError({\n id: createVectorErrorId('ASTRA', 'QUERY', 'MISSING_VECTOR'),\n text: 'queryVector is required for Astra queries. Metadata-only queries are not supported by this vector store.',\n domain: ErrorDomain.MASTRA_VECTOR,\n category: ErrorCategory.USER,\n details: { indexName },\n });\n }\n\n const collection = this.#db.collection(indexName);\n\n const translatedFilter = this.transformFilter(filter);\n\n try {\n const cursor = collection.find(translatedFilter ?? {}, {\n sort: { $vector: queryVector },\n limit: topK,\n includeSimilarity: true,\n projection: {\n $vector: !!includeVector,\n },\n });\n\n const results = await cursor.toArray();\n\n return results.map(result => ({\n id: result.id,\n score: result.$similarity,\n metadata: result.metadata,\n ...(includeVector && { vector: result.$vector }),\n }));\n } catch (error: any) {\n throw new MastraError(\n {\n id: createVectorErrorId('ASTRA', 'QUERY', 'DB_ERROR'),\n domain: ErrorDomain.MASTRA_VECTOR,\n category: ErrorCategory.THIRD_PARTY,\n details: { indexName },\n },\n error,\n );\n }\n }\n\n /**\n * Lists all collections in the database.\n *\n * @returns {Promise<string[]>} A promise that resolves to an array of collection names.\n */\n async listIndexes(): Promise<string[]> {\n try {\n return await this.#db.listCollections({ nameOnly: true });\n } catch (error: any) {\n throw new MastraError(\n {\n id: createVectorErrorId('ASTRA', 'LIST_INDEXES', 'DB_ERROR'),\n domain: ErrorDomain.MASTRA_VECTOR,\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 const collection = this.#db.collection(indexName);\n try {\n const optionsPromise = collection.options();\n const countPromise = collection.countDocuments({}, 100);\n const [options, count] = await Promise.all([optionsPromise, countPromise]);\n\n const keys = Object.keys(metricMap) as (keyof typeof metricMap)[];\n const metric = keys.find(key => metricMap[key] === options.vector?.metric);\n return {\n dimension: options.vector?.dimension!,\n metric,\n count: count,\n };\n } catch (error: any) {\n if (error instanceof MastraError) throw error;\n throw new MastraError(\n {\n id: createVectorErrorId('ASTRA', 'DESCRIBE_INDEX', 'DB_ERROR'),\n domain: ErrorDomain.MASTRA_VECTOR,\n category: ErrorCategory.THIRD_PARTY,\n details: { indexName },\n },\n error,\n );\n }\n }\n\n /**\n * Deletes the specified collection.\n *\n * @param {string} indexName - The name of the collection to delete.\n * @returns {Promise<void>} A promise that resolves when the collection is deleted.\n */\n async deleteIndex({ indexName }: DeleteIndexParams): Promise<void> {\n const collection = this.#db.collection(indexName);\n try {\n await collection.drop();\n } catch (error: any) {\n throw new MastraError(\n {\n id: createVectorErrorId('ASTRA', 'DELETE_INDEX', 'DB_ERROR'),\n domain: ErrorDomain.MASTRA_VECTOR,\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 (!id) {\n throw new MastraError({\n id: createVectorErrorId('ASTRA', 'UPDATE_VECTOR', 'NO_ID'),\n text: 'id is required for Astra updateVector',\n domain: ErrorDomain.MASTRA_VECTOR,\n category: ErrorCategory.USER,\n details: { indexName },\n });\n }\n\n if (!update.vector && !update.metadata) {\n throw new MastraError({\n id: createVectorErrorId('ASTRA', 'UPDATE_VECTOR', 'NO_PAYLOAD'),\n text: 'No updates provided for vector',\n domain: ErrorDomain.MASTRA_VECTOR,\n category: ErrorCategory.USER,\n details: {\n indexName,\n id,\n },\n });\n }\n\n try {\n const collection = this.#db.collection(indexName);\n const updateDoc: Record<string, any> = {};\n\n if (update.vector) {\n updateDoc.$vector = update.vector;\n }\n\n if (update.metadata) {\n updateDoc.metadata = update.metadata;\n }\n\n await collection.findOneAndUpdate({ id }, { $set: updateDoc });\n } catch (error: any) {\n if (error instanceof MastraError) throw error;\n throw new MastraError(\n {\n id: createVectorErrorId('ASTRA', 'UPDATE_VECTOR', 'FAILED_UNHANDLED'),\n domain: ErrorDomain.MASTRA_VECTOR,\n category: ErrorCategory.THIRD_PARTY,\n details: {\n indexName,\n ...(id && { id }),\n },\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 const collection = this.#db.collection(indexName);\n await collection.deleteOne({ id });\n } catch (error: any) {\n if (error instanceof MastraError) throw error;\n throw new MastraError(\n {\n id: createVectorErrorId('ASTRA', 'DELETE_VECTOR', 'FAILED'),\n domain: ErrorDomain.MASTRA_VECTOR,\n category: ErrorCategory.THIRD_PARTY,\n details: {\n indexName,\n ...(id && { id }),\n },\n },\n error,\n );\n }\n }\n\n async deleteVectors({ indexName, filter, ids }: DeleteVectorsParams): Promise<void> {\n throw new MastraError({\n id: createVectorErrorId('ASTRA', 'DELETE_VECTORS', 'NOT_SUPPORTED'),\n text: 'deleteVectors is not yet implemented for Astra vector store',\n domain: ErrorDomain.MASTRA_VECTOR,\n category: ErrorCategory.SYSTEM,\n details: {\n indexName,\n ...(filter && { filter: JSON.stringify(filter) }),\n ...(ids && { idsCount: ids.length }),\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 Astra Vector.\n */\nexport const ASTRA_PROMPT = `When querying Astra, 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- $all: Match all values in array\n Example: { \"tags\": { \"$all\": [\"premium\", \"sale\"] } }\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- $not: Logical NOT\n Example: { \"$not\": { \"category\": \"electronics\" } }\n\nElement Operators:\n- $exists: Check if field exists\n Example: { \"rating\": { \"$exists\": true } }\n\nSpecial Operators:\n- $size: Array length check\n Example: { \"tags\": { \"$size\": 2 } }\n\nRestrictions:\n- Regex patterns are not supported\n- Only $and, $or, and $not logical operators are supported\n- Nested fields are supported using dot notation\n- Multiple conditions on the same field are supported with both implicit and explicit $and\n- Empty arrays in $in/$nin will return no results\n- A non-empty array is required for $all operator\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- Other 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 { \"tags\": { \"$all\": [\"premium\"] } },\n { \"rating\": { \"$exists\": true, \"$gt\": 4 } },\n { \"$or\": [\n { \"stock\": { \"$gt\": 0 } },\n { \"preorder\": true }\n ]}\n ]\n}`;\n"],"mappings":";;;;;;;;;;;AA8BA,IAAa,wBAAb,cAA2C,qBAAwC;CACjF,wBAA4D;EAC1D,OAAO;GACL,GAAG,qBAAqB;GACxB,OAAO;IAAC;IAAQ;IAAO;GAAM;GAC7B,SAAS;IAAC;IAAQ;IAAO;GAAM;GAC/B,OAAO,CAAC;GACR,QAAQ,CAAC,OAAO;EAClB;CACF;CAEA,UAAU,QAA+C;EACvD,IAAI,KAAK,QAAQ,MAAM,GAAG,OAAO;EACjC,KAAK,eAAe,MAAM;EAE1B,OAAO,KAAK,cAAc,MAAM;CAClC;CAEA,cAAsB,MAA8B;EAElD,IAAI,KAAK,QAAQ,IAAI,GACnB,MAAM,IAAI,MAAM,oCAAoC;EAEtD,IAAI,KAAK,YAAY,IAAI,KAAK,MAAM,QAAQ,IAAI,GAC9C,OAAO;EAIT,MAAM,oBADU,OAAO,QAAQ,IACC,CAAC,CAAC,KAAK,CAAC,KAAK,WAAW;GAEtD,IAAI,KAAK,WAAW,GA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M,EAAE,GAAG;IACjB;GACF,GACA,KACF;EACF;CACF;CAEA,MAAM,cAAc,EAAE,WAAW,QAAQ,OAA2C;EAClF,MAAM,IAAI,YAAY;GACpB,IAAI,oBAAoB,SAAS,kBAAkB,eAAe;GAClE,MAAM;GACN,QAAQ,YAAY;GACpB,UAAU,cAAc;GACxB,SAAS;IACP;IACA,GAAI,UAAU,EAAE,QAAQ,KAAK,UAAU,MAAM,EAAE;IAC/C,GAAI,OAAO,EAAE,UAAU,IAAI,OAAO;GACpC;EACF,CAAC;CACH;AACF;;;;;;;AC5WA,MAAa,eAAe"}
|
|
1
|
+
{"version":3,"file":"index.js","names":[],"sources":["../src/vector/filter.ts","../src/vector/index.ts","../src/vector/prompt.ts"],"sourcesContent":["import { BaseFilterTranslator } from '@mastra/core/vector/filter';\nimport type {\n VectorFilter,\n OperatorSupport,\n QueryOperator,\n OperatorValueMap,\n LogicalOperatorValueMap,\n BlacklistedRootOperators,\n} from '@mastra/core/vector/filter';\n\ntype AstraOperatorValueMap = Omit<OperatorValueMap, '$elemMatch' | '$regex' | '$options'> & {\n $size: number; // Astra-specific\n};\n\ntype AstraLogicalOperatorValueMap = Omit<LogicalOperatorValueMap, '$nor'>;\n\ntype AstraBlacklisted = BlacklistedRootOperators | '$nor' | '$size';\n\nexport type AstraVectorFilter = VectorFilter<\n keyof AstraOperatorValueMap,\n AstraOperatorValueMap,\n AstraLogicalOperatorValueMap,\n AstraBlacklisted\n>;\n\n/**\n * Translator for Astra DB filter queries.\n * Maintains MongoDB-compatible syntax while ensuring proper validation\n * and normalization of values.\n */\nexport class AstraFilterTranslator extends BaseFilterTranslator<AstraVectorFilter> {\n protected override getSupportedOperators(): OperatorSupport {\n return {\n ...BaseFilterTranslator.DEFAULT_OPERATORS,\n array: ['$all', '$in', '$nin'],\n logical: ['$and', '$or', '$not'],\n regex: [],\n custom: ['$size'],\n };\n }\n\n translate(filter?: AstraVectorFilter): AstraVectorFilter {\n if (this.isEmpty(filter)) return filter;\n this.validateFilter(filter);\n\n return this.translateNode(filter);\n }\n\n private translateNode(node: AstraVectorFilter): any {\n // Handle primitive values and arrays\n if (this.isRegex(node)) {\n throw new Error('Regex is not supported in Astra DB');\n }\n if (this.isPrimitive(node) || Array.isArray(node)) {\n return node;\n }\n\n const entries = Object.entries(node as Record<string, any>);\n const translatedEntries = entries.map(([key, value]) => {\n // Handle operators\n if (this.isOperator(key)) {\n return [key, this.translateOperatorValue(key, value)];\n }\n\n // Handle nested paths and objects\n return [key, this.translateNode(value)];\n });\n\n return Object.fromEntries(translatedEntries);\n }\n\n private translateOperatorValue(operator: QueryOperator, value: any): any {\n if (this.isBasicOperator(operator) || this.isNumericOperator(operator)) {\n return this.normalizeComparisonValue(value);\n }\n\n if (this.isArrayOperator(operator) && Array.isArray(value)) {\n return this.normalizeArrayValues(value);\n }\n\n return this.translateNode(value);\n }\n}\n","import type { Db } from '@datastax/astra-db-ts';\nimport { DataAPIClient, UUID } from '@datastax/astra-db-ts';\nimport { MastraError, ErrorDomain, ErrorCategory } from '@mastra/core/error';\nimport { createVectorErrorId } from '@mastra/core/storage';\nimport { MastraVector } from '@mastra/core/vector';\nimport type {\n QueryResult,\n IndexStats,\n CreateIndexParams,\n UpsertVectorParams,\n QueryVectorParams,\n DescribeIndexParams,\n DeleteIndexParams,\n DeleteVectorParams,\n UpdateVectorParams,\n DeleteVectorsParams,\n} from '@mastra/core/vector';\nimport type { AstraVectorFilter } from './filter';\nimport { AstraFilterTranslator } from './filter';\n\n// Mastra and Astra DB agree on cosine and euclidean, but Astra DB uses dot_product instead of dotproduct.\nconst metricMap = {\n cosine: 'cosine',\n euclidean: 'euclidean',\n dotproduct: 'dot_product',\n} as const;\n\nexport interface AstraDbOptions {\n token: string;\n endpoint: string;\n keyspace?: string;\n}\n\ntype AstraQueryVectorParams = QueryVectorParams<AstraVectorFilter>;\n\nexport class AstraVector extends MastraVector<AstraVectorFilter> {\n readonly #db: Db;\n\n constructor({ token, endpoint, keyspace, id }: AstraDbOptions & { id: string }) {\n super({ id });\n const client = new DataAPIClient(token);\n this.#db = client.db(endpoint, { keyspace });\n }\n\n /**\n * Creates a new collection with the specified configuration.\n *\n * @param {string} indexName - The name of the collection to create.\n * @param {number} dimension - The dimension of the vectors to be stored in the collection.\n * @param {'cosine' | 'euclidean' | 'dotproduct'} [metric=cosine] - The metric to use to sort vectors in the collection.\n * @returns {Promise<void>} A promise that resolves when the collection is created.\n */\n async createIndex({ indexName, dimension, metric = 'cosine' }: CreateIndexParams): Promise<void> {\n if (!Number.isInteger(dimension) || dimension <= 0) {\n throw new MastraError({\n id: createVectorErrorId('ASTRA', 'CREATE_INDEX', 'INVALID_DIMENSION'),\n text: 'Dimension must be a positive integer',\n domain: ErrorDomain.MASTRA_VECTOR,\n category: ErrorCategory.USER,\n });\n }\n try {\n await this.#db.createCollection(indexName, {\n vector: {\n dimension,\n metric: metricMap[metric],\n },\n checkExists: false,\n });\n } catch (error: any) {\n new MastraError(\n {\n id: createVectorErrorId('ASTRA', 'CREATE_INDEX', 'DB_ERROR'),\n domain: ErrorDomain.MASTRA_VECTOR,\n category: ErrorCategory.THIRD_PARTY,\n details: { indexName },\n },\n error,\n );\n }\n }\n\n /**\n * Inserts or updates vectors in the specified collection.\n *\n * @param {string} indexName - The name of the collection to upsert into.\n * @param {number[][]} vectors - An array of vectors to upsert.\n * @param {Record<string, any>[]} [metadata] - An optional array of metadata objects corresponding to each vector.\n * @param {string[]} [ids] - An optional array of IDs corresponding to each vector. If not provided, new IDs will be generated.\n * @returns {Promise<string[]>} A promise that resolves to an array of IDs of the upserted vectors.\n */\n async upsert({ indexName, vectors, metadata, ids }: UpsertVectorParams): Promise<string[]> {\n const collection = this.#db.collection(indexName);\n\n // Generate IDs if not provided\n const vectorIds = ids || vectors.map(() => UUID.v7().toString());\n\n const records = vectors.map((vector, i) => ({\n id: vectorIds[i],\n $vector: vector,\n metadata: metadata?.[i] || {},\n }));\n\n try {\n await collection.insertMany(records);\n } catch (error: any) {\n throw new MastraError(\n {\n id: createVectorErrorId('ASTRA', 'UPSERT', 'DB_ERROR'),\n domain: ErrorDomain.MASTRA_VECTOR,\n category: ErrorCategory.THIRD_PARTY,\n details: { indexName },\n },\n error,\n );\n }\n return vectorIds;\n }\n\n transformFilter(filter?: AstraVectorFilter) {\n const translator = new AstraFilterTranslator();\n return translator.translate(filter);\n }\n\n /**\n * Queries the specified collection using a vector and optional filter.\n *\n * @param {string} indexName - The name of the collection to query.\n * @param {number[]} queryVector - The vector to query with.\n * @param {number} [topK] - The maximum number of results to return.\n * @param {Record<string, any>} [filter] - An optional filter to apply to the query. For more on filters in Astra DB, see the filtering reference: https://docs.datastax.com/en/astra-db-serverless/api-reference/documents.html#operators\n * @param {boolean} [includeVectors=false] - Whether to include the vectors in the response.\n * @returns {Promise<QueryResult[]>} A promise that resolves to an array of query results.\n */\n async query({\n indexName,\n queryVector,\n topK = 10,\n filter,\n includeVector = false,\n }: AstraQueryVectorParams): Promise<QueryResult[]> {\n if (!queryVector) {\n throw new MastraError({\n id: createVectorErrorId('ASTRA', 'QUERY', 'MISSING_VECTOR'),\n text: 'queryVector is required for Astra queries. Metadata-only queries are not supported by this vector store.',\n domain: ErrorDomain.MASTRA_VECTOR,\n category: ErrorCategory.USER,\n details: { indexName },\n });\n }\n\n const collection = this.#db.collection(indexName);\n\n const translatedFilter = this.transformFilter(filter);\n\n try {\n const cursor = collection.find(translatedFilter ?? {}, {\n sort: { $vector: queryVector },\n limit: topK,\n includeSimilarity: true,\n projection: {\n $vector: !!includeVector,\n },\n });\n\n const results = await cursor.toArray();\n\n return results.map(result => ({\n id: result.id,\n score: result.$similarity,\n metadata: result.metadata,\n ...(includeVector && { vector: result.$vector }),\n }));\n } catch (error: any) {\n throw new MastraError(\n {\n id: createVectorErrorId('ASTRA', 'QUERY', 'DB_ERROR'),\n domain: ErrorDomain.MASTRA_VECTOR,\n category: ErrorCategory.THIRD_PARTY,\n details: { indexName },\n },\n error,\n );\n }\n }\n\n /**\n * Lists all collections in the database.\n *\n * @returns {Promise<string[]>} A promise that resolves to an array of collection names.\n */\n async listIndexes(): Promise<string[]> {\n try {\n return await this.#db.listCollections({ nameOnly: true });\n } catch (error: any) {\n throw new MastraError(\n {\n id: createVectorErrorId('ASTRA', 'LIST_INDEXES', 'DB_ERROR'),\n domain: ErrorDomain.MASTRA_VECTOR,\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 const collection = this.#db.collection(indexName);\n try {\n const optionsPromise = collection.options();\n const countPromise = collection.countDocuments({}, 100);\n const [options, count] = await Promise.all([optionsPromise, countPromise]);\n\n const keys = Object.keys(metricMap) as (keyof typeof metricMap)[];\n const metric = keys.find(key => metricMap[key] === options.vector?.metric);\n return {\n dimension: options.vector?.dimension!,\n metric,\n count: count,\n };\n } catch (error: any) {\n if (error instanceof MastraError) throw error;\n throw new MastraError(\n {\n id: createVectorErrorId('ASTRA', 'DESCRIBE_INDEX', 'DB_ERROR'),\n domain: ErrorDomain.MASTRA_VECTOR,\n category: ErrorCategory.THIRD_PARTY,\n details: { indexName },\n },\n error,\n );\n }\n }\n\n /**\n * Deletes the specified collection.\n *\n * @param {string} indexName - The name of the collection to delete.\n * @returns {Promise<void>} A promise that resolves when the collection is deleted.\n */\n async deleteIndex({ indexName }: DeleteIndexParams): Promise<void> {\n const collection = this.#db.collection(indexName);\n try {\n await collection.drop();\n } catch (error: any) {\n throw new MastraError(\n {\n id: createVectorErrorId('ASTRA', 'DELETE_INDEX', 'DB_ERROR'),\n domain: ErrorDomain.MASTRA_VECTOR,\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 (!id) {\n throw new MastraError({\n id: createVectorErrorId('ASTRA', 'UPDATE_VECTOR', 'NO_ID'),\n text: 'id is required for Astra updateVector',\n domain: ErrorDomain.MASTRA_VECTOR,\n category: ErrorCategory.USER,\n details: { indexName },\n });\n }\n\n if (!update.vector && !update.metadata) {\n throw new MastraError({\n id: createVectorErrorId('ASTRA', 'UPDATE_VECTOR', 'NO_PAYLOAD'),\n text: 'No updates provided for vector',\n domain: ErrorDomain.MASTRA_VECTOR,\n category: ErrorCategory.USER,\n details: {\n indexName,\n id,\n },\n });\n }\n\n try {\n const collection = this.#db.collection(indexName);\n const updateDoc: Record<string, any> = {};\n\n if (update.vector) {\n updateDoc.$vector = update.vector;\n }\n\n if (update.metadata) {\n updateDoc.metadata = update.metadata;\n }\n\n await collection.findOneAndUpdate({ id }, { $set: updateDoc });\n } catch (error: any) {\n if (error instanceof MastraError) throw error;\n throw new MastraError(\n {\n id: createVectorErrorId('ASTRA', 'UPDATE_VECTOR', 'FAILED_UNHANDLED'),\n domain: ErrorDomain.MASTRA_VECTOR,\n category: ErrorCategory.THIRD_PARTY,\n details: {\n indexName,\n ...(id && { id }),\n },\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 const collection = this.#db.collection(indexName);\n await collection.deleteOne({ id });\n } catch (error: any) {\n if (error instanceof MastraError) throw error;\n throw new MastraError(\n {\n id: createVectorErrorId('ASTRA', 'DELETE_VECTOR', 'FAILED'),\n domain: ErrorDomain.MASTRA_VECTOR,\n category: ErrorCategory.THIRD_PARTY,\n details: {\n indexName,\n ...(id && { id }),\n },\n },\n error,\n );\n }\n }\n\n async deleteVectors({ indexName, filter, ids }: DeleteVectorsParams): Promise<void> {\n throw new MastraError({\n id: createVectorErrorId('ASTRA', 'DELETE_VECTORS', 'NOT_SUPPORTED'),\n text: 'deleteVectors is not yet implemented for Astra vector store',\n domain: ErrorDomain.MASTRA_VECTOR,\n category: ErrorCategory.SYSTEM,\n details: {\n indexName,\n ...(filter && { filter: JSON.stringify(filter) }),\n ...(ids && { idsCount: ids.length }),\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 Astra Vector.\n */\nexport const ASTRA_PROMPT = `When querying Astra, 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- $all: Match all values in array\n Example: { \"tags\": { \"$all\": [\"premium\", \"sale\"] } }\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- $not: Logical NOT\n Example: { \"$not\": { \"category\": \"electronics\" } }\n\nElement Operators:\n- $exists: Check if field exists\n Example: { \"rating\": { \"$exists\": true } }\n\nSpecial Operators:\n- $size: Array length check\n Example: { \"tags\": { \"$size\": 2 } }\n\nRestrictions:\n- Regex patterns are not supported\n- Only $and, $or, and $not logical operators are supported\n- Nested fields are supported using dot notation\n- Multiple conditions on the same field are supported with both implicit and explicit $and\n- Empty arrays in $in/$nin will return no results\n- A non-empty array is required for $all operator\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- Other 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 { \"tags\": { \"$all\": [\"premium\"] } },\n { \"rating\": { \"$exists\": true, \"$gt\": 4 }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,OAAO,QACT,UAAU,UAAU,OAAO;GAG7B,IAAI,OAAO,UACT,UAAU,WAAW,OAAO;GAG9B,MAAM,WAAW,iBAAiB,EAAE,GAAG,GAAG,EAAE,MAAM,UAAU,CAAC;EAC/D,SAAS,OAAY;GACnB,IAAI,iBAAiB,aAAa,MAAM;GACxC,MAAM,IAAI,YACR;IACE,IAAI,oBAAoB,SAAS,iBAAiB,kBAAkB;IACpE,QAAQ,YAAY;IACpB,UAAU,cAAc;IACxB,SAAS;KACP;KACA,GAAI,MAAM,EAAE,GAAG;IACjB;GACF,GACA,KACF;EACF;CACF;;;;;;;;CASA,MAAM,aAAa,EAAE,WAAW,MAAyC;EACvE,IAAI;GAEF,MADmB,KAAK,IAAI,WAAW,SACxB,CAAC,CAAC,UAAU,EAAE,GAAG,CAAC;EACnC,SAAS,OAAY;GACnB,IAAI,iBAAiB,aAAa,MAAM;GACxC,MAAM,IAAI,YACR;IACE,IAAI,oBAAoB,SAAS,iBAAiB,QAAQ;IAC1D,QAAQ,YAAY;IACpB,UAAU,cAAc;IACxB,SAAS;KACP;KACA,GAAI,MAAM,EAAE,GAAG;IACjB;GACF,GACA,KACF;EACF;CACF;CAEA,MAAM,cAAc,EAAE,WAAW,QAAQ,OAA2C;EAClF,MAAM,IAAI,YAAY;GACpB,IAAI,oBAAoB,SAAS,kBAAkB,eAAe;GAClE,MAAM;GACN,QAAQ,YAAY;GACpB,UAAU,cAAc;GACxB,SAAS;IACP;IACA,GAAI,UAAU,EAAE,QAAQ,KAAK,UAAU,MAAM,EAAE;IAC/C,GAAI,OAAO,EAAE,UAAU,IAAI,OAAO;GACpC;EACF,CAAC;CACH;AACF;;;;;;AC5WA,MAAa,eAAe"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mastra/astra",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.3-alpha.0",
|
|
4
4
|
"description": "Astra DB provider for Mastra - includes vector store capabilities",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -26,16 +26,16 @@
|
|
|
26
26
|
"@datastax/astra-db-ts": "^1.5.0"
|
|
27
27
|
},
|
|
28
28
|
"devDependencies": {
|
|
29
|
-
"@types/node": "22.
|
|
30
|
-
"@vitest/coverage-v8": "4.1.
|
|
31
|
-
"@vitest/ui": "4.1.
|
|
32
|
-
"tsdown": "0.
|
|
33
|
-
"tsx": "^4.23.
|
|
29
|
+
"@types/node": "22.19.15",
|
|
30
|
+
"@vitest/coverage-v8": "4.1.12",
|
|
31
|
+
"@vitest/ui": "4.1.12",
|
|
32
|
+
"tsdown": "0.23.1",
|
|
33
|
+
"tsx": "^4.23.15",
|
|
34
34
|
"typescript": "^7.0.2",
|
|
35
|
-
"vitest": "4.1.
|
|
35
|
+
"vitest": "4.1.12",
|
|
36
36
|
"@internal/lint": "0.0.145",
|
|
37
|
-
"@
|
|
38
|
-
"@
|
|
37
|
+
"@internal/types-builder": "0.0.120",
|
|
38
|
+
"@mastra/core": "1.80.0-alpha.4"
|
|
39
39
|
},
|
|
40
40
|
"peerDependencies": {
|
|
41
41
|
"@mastra/core": ">=1.0.0-0 <2.0.0-0"
|