@mastra/astra 1.1.1 → 1.1.2-alpha.1
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 +7 -5
- package/dist/docs/references/reference-rag-vector-databases.md +98 -2
- package/dist/docs/references/reference-vectors-astra.md +10 -5
- package/dist/index.cjs +1 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/package.json +9 -10
package/dist/docs/SKILL.md
CHANGED
|
@@ -50,23 +50,25 @@ console.log(results)
|
|
|
50
50
|
|
|
51
51
|
The `topK` parameter specifies the maximum number of most similar results to return from the vector search.
|
|
52
52
|
|
|
53
|
-
|
|
53
|
+
Each result includes an `id`, a similarity `score`, and the stored `metadata`. When you store chunk text in metadata, it's available as `metadata.text`:
|
|
54
54
|
|
|
55
55
|
```ts
|
|
56
56
|
[
|
|
57
57
|
{
|
|
58
|
-
|
|
58
|
+
id: 'chunk-1',
|
|
59
59
|
score: 0.89,
|
|
60
|
-
metadata: { source: 'article1.txt' },
|
|
60
|
+
metadata: { text: 'Climate change poses significant challenges...', source: 'article1.txt' },
|
|
61
61
|
},
|
|
62
62
|
{
|
|
63
|
-
|
|
63
|
+
id: 'chunk-2',
|
|
64
64
|
score: 0.82,
|
|
65
|
-
metadata: { source: 'article1.txt' },
|
|
65
|
+
metadata: { text: 'Rising temperatures affect crop yields...', source: 'article1.txt' },
|
|
66
66
|
},
|
|
67
67
|
]
|
|
68
68
|
```
|
|
69
69
|
|
|
70
|
+
Results can also include `vector` when you set `includeVector: true`, and `document` when the store returns the original document (Chroma only).
|
|
71
|
+
|
|
70
72
|
## Advanced retrieval options
|
|
71
73
|
|
|
72
74
|
### Metadata Filtering
|
|
@@ -54,6 +54,29 @@ const results = await store.hybridQuery({
|
|
|
54
54
|
|
|
55
55
|
See the [MongoDB vector reference](https://mastra.ai/reference/vectors/mongodb) for details on `createSearchIndex()`, `textQuery()`, and `hybridQuery()`.
|
|
56
56
|
|
|
57
|
+
### Automated Embedding
|
|
58
|
+
|
|
59
|
+
MongoDB can generate the embeddings itself, so you don't need an embedding provider in your application. Create the index with `autoEmbed` and a Voyage AI model, write plain text, and search with a query string:
|
|
60
|
+
|
|
61
|
+
```ts
|
|
62
|
+
await store.createIndex({
|
|
63
|
+
indexName: 'myCollection',
|
|
64
|
+
autoEmbed: { model: 'voyage-4' },
|
|
65
|
+
})
|
|
66
|
+
await store.upsert({
|
|
67
|
+
indexName: 'myCollection',
|
|
68
|
+
documents: chunks.map(chunk => chunk.text),
|
|
69
|
+
metadata: chunks.map(chunk => ({ text: chunk.text })),
|
|
70
|
+
})
|
|
71
|
+
const results = await store.query({
|
|
72
|
+
indexName: 'myCollection',
|
|
73
|
+
queryText: 'search terms',
|
|
74
|
+
topK: 10,
|
|
75
|
+
})
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
Automated Embedding is a MongoDB Preview feature and needs a deployment where it's available. See the [MongoDB vector reference](https://mastra.ai/reference/vectors/mongodb) for the supported options and requirements.
|
|
79
|
+
|
|
57
80
|
**PgVector**:
|
|
58
81
|
|
|
59
82
|
```ts
|
|
@@ -207,7 +230,7 @@ await store.upsert({
|
|
|
207
230
|
**libSQL**:
|
|
208
231
|
|
|
209
232
|
```ts
|
|
210
|
-
import { LibSQLVector } from '@mastra/
|
|
233
|
+
import { LibSQLVector } from '@mastra/libsql'
|
|
211
234
|
|
|
212
235
|
const store = new LibSQLVector({
|
|
213
236
|
id: 'libsql-vector',
|
|
@@ -248,6 +271,34 @@ await store.upsert({
|
|
|
248
271
|
})
|
|
249
272
|
```
|
|
250
273
|
|
|
274
|
+
**Weaviate**:
|
|
275
|
+
|
|
276
|
+
```ts
|
|
277
|
+
import { WeaviateVector } from '@mastra/weaviate'
|
|
278
|
+
|
|
279
|
+
const store = new WeaviateVector({
|
|
280
|
+
id: 'weaviate-vector',
|
|
281
|
+
httpHost: process.env.WEAVIATE_HOST,
|
|
282
|
+
httpPort: 443,
|
|
283
|
+
httpSecure: true,
|
|
284
|
+
grpcHost: process.env.WEAVIATE_GRPC_HOST,
|
|
285
|
+
grpcPort: 443,
|
|
286
|
+
grpcSecure: true,
|
|
287
|
+
apiKey: process.env.WEAVIATE_API_KEY,
|
|
288
|
+
})
|
|
289
|
+
|
|
290
|
+
await store.createIndex({
|
|
291
|
+
indexName: 'myCollection',
|
|
292
|
+
dimension: 1536,
|
|
293
|
+
})
|
|
294
|
+
|
|
295
|
+
await store.upsert({
|
|
296
|
+
indexName: 'myCollection',
|
|
297
|
+
vectors: embeddings,
|
|
298
|
+
metadata: chunks.map(chunk => ({ text: chunk.text })),
|
|
299
|
+
})
|
|
300
|
+
```
|
|
301
|
+
|
|
251
302
|
**Cloudflare**:
|
|
252
303
|
|
|
253
304
|
```ts
|
|
@@ -317,6 +368,29 @@ await store.upsert({
|
|
|
317
368
|
|
|
318
369
|
For detailed setup instructions and best practices, see the [official Elasticsearch documentation](https://www.elastic.co/docs/solutions/search/get-started).
|
|
319
370
|
|
|
371
|
+
**Azure AI Search**:
|
|
372
|
+
|
|
373
|
+
```ts
|
|
374
|
+
import { AzureAISearchVector } from '@mastra/azure-ai-search'
|
|
375
|
+
|
|
376
|
+
const store = new AzureAISearchVector({
|
|
377
|
+
id: 'azure-search-vectors',
|
|
378
|
+
endpoint: process.env.AZURE_AI_SEARCH_ENDPOINT!,
|
|
379
|
+
credential: process.env.AZURE_AI_SEARCH_CREDENTIAL!,
|
|
380
|
+
})
|
|
381
|
+
|
|
382
|
+
await store.createIndex({
|
|
383
|
+
indexName: 'my-collection',
|
|
384
|
+
dimension: 1536,
|
|
385
|
+
})
|
|
386
|
+
|
|
387
|
+
await store.upsert({
|
|
388
|
+
indexName: 'my-collection',
|
|
389
|
+
vectors: embeddings,
|
|
390
|
+
metadata: chunks.map(chunk => ({ text: chunk.text })),
|
|
391
|
+
})
|
|
392
|
+
```
|
|
393
|
+
|
|
320
394
|
**Couchbase**:
|
|
321
395
|
|
|
322
396
|
```ts
|
|
@@ -534,6 +608,17 @@ Namespace names must:
|
|
|
534
608
|
|
|
535
609
|
- Example: `_namespace` isn't valid (starts with underscore)
|
|
536
610
|
|
|
611
|
+
**Weaviate**:
|
|
612
|
+
|
|
613
|
+
Index names map to Weaviate collections, which:
|
|
614
|
+
|
|
615
|
+
- Are capitalized by Weaviate (the first letter is upper-cased)
|
|
616
|
+
- Should contain only letters, numbers, and the `_` character
|
|
617
|
+
- Must start with a letter, since only the first character is upper-cased (a leading digit or symbol stays invalid)
|
|
618
|
+
- Preserve the original Mastra index name in the collection description, so `listIndexes()` and `describeIndex()` return the name you supplied
|
|
619
|
+
- Example: `my_collection` is stored as `My_collection` and returned as `my_collection`
|
|
620
|
+
- Example: `123_collection` isn't valid (starts with a digit)
|
|
621
|
+
|
|
537
622
|
**Cloudflare**:
|
|
538
623
|
|
|
539
624
|
Index names must:
|
|
@@ -587,6 +672,17 @@ Index names must:
|
|
|
587
672
|
- Example: `myindex-` isn't valid (ends with hyphen)
|
|
588
673
|
- Example: `MyIndex` isn't valid (contains uppercase letters)
|
|
589
674
|
|
|
675
|
+
**Azure AI Search**:
|
|
676
|
+
|
|
677
|
+
Index names must:
|
|
678
|
+
|
|
679
|
+
- Use only lowercase letters, numbers, dashes (`-`), and underscore (`_`) characters
|
|
680
|
+
- Not start or end with a dash
|
|
681
|
+
- Be between 2 and 128 characters long
|
|
682
|
+
- Example: `my-index-123` and `my_index` are valid
|
|
683
|
+
- Example: `MyIndex` isn't valid (contains uppercase letters)
|
|
684
|
+
- Example: `my-index-` isn't valid (ends with a dash)
|
|
685
|
+
|
|
590
686
|
### Upserting Embeddings
|
|
591
687
|
|
|
592
688
|
After creating an index, you can store embeddings along with their basic metadata:
|
|
@@ -650,7 +746,7 @@ Key metadata considerations:
|
|
|
650
746
|
|
|
651
747
|
## Deleting vectors
|
|
652
748
|
|
|
653
|
-
|
|
749
|
+
Use `deleteVectors` with a metadata filter to remove embeddings associated with a document. This is useful for cleaning up stale vectors after a document is deleted or updated.
|
|
654
750
|
|
|
655
751
|
### Delete by Metadata Filter
|
|
656
752
|
|
|
@@ -60,7 +60,7 @@ Returns:
|
|
|
60
60
|
interface IndexStats {
|
|
61
61
|
dimension: number
|
|
62
62
|
count: number
|
|
63
|
-
metric
|
|
63
|
+
metric?: 'cosine' | 'euclidean' | 'dotproduct'
|
|
64
64
|
}
|
|
65
65
|
```
|
|
66
66
|
|
|
@@ -90,24 +90,29 @@ Query results are returned in this format:
|
|
|
90
90
|
interface QueryResult {
|
|
91
91
|
id: string
|
|
92
92
|
score: number
|
|
93
|
-
metadata
|
|
93
|
+
metadata?: Record<string, any>
|
|
94
94
|
vector?: number[] // Only included if includeVector is true
|
|
95
|
+
document?: string // Only returned by stores that keep document content
|
|
95
96
|
}
|
|
96
97
|
```
|
|
97
98
|
|
|
98
99
|
## Error handling
|
|
99
100
|
|
|
100
|
-
The store throws
|
|
101
|
+
The store throws `MastraError` instances from `@mastra/core/error`:
|
|
101
102
|
|
|
102
103
|
```typescript
|
|
104
|
+
import { MastraError } from '@mastra/core/error'
|
|
105
|
+
|
|
103
106
|
try {
|
|
104
107
|
await store.query({
|
|
105
108
|
indexName: 'index_name',
|
|
106
109
|
queryVector: queryVector,
|
|
107
110
|
})
|
|
108
111
|
} catch (error) {
|
|
109
|
-
if (error instanceof
|
|
110
|
-
console.log(error.
|
|
112
|
+
if (error instanceof MastraError) {
|
|
113
|
+
console.log(error.id) // 'MASTRA_VECTOR_ASTRA_QUERY_DB_ERROR'
|
|
114
|
+
console.log(error.domain) // 'MASTRA_VECTOR'
|
|
115
|
+
console.log(error.category) // 'USER' | 'THIRD_PARTY'
|
|
111
116
|
console.log(error.details) // Additional error context
|
|
112
117
|
}
|
|
113
118
|
}
|
package/dist/index.cjs
CHANGED
|
@@ -151,7 +151,7 @@ var AstraVector = class extends _mastra_core_vector.MastraVector {
|
|
|
151
151
|
sort: { $vector: queryVector },
|
|
152
152
|
limit: topK,
|
|
153
153
|
includeSimilarity: true,
|
|
154
|
-
projection: { $vector: includeVector
|
|
154
|
+
projection: { $vector: !!includeVector }
|
|
155
155
|
}).toArray()).map((result) => ({
|
|
156
156
|
id: result.id,
|
|
157
157
|
score: result.$similarity,
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs","names":["BaseFilterTranslator","MastraVector","#db","DataAPIClient","MastraError","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 ? true : false,\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 }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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,IAAIF,mBAAAA,YAAY;GACpB,KAAA,GAAA,qBAAA,oBAAA,CAAwB,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","#db","DataAPIClient","MastraError","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,yBAAyB,KAAK;EAG5C,IAAI,KAAK,gBAAgB,QAAQ,KAAK,MAAM,QAA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MADmB,KAAKJ,IAAI,WAAW,SACxB,CAAC,CAAC,UAAU,EAAE,GAAG,CAAC;EACnC,SAAS,OAAY;GACnB,IAAI,iBAAiBE,mBAAAA,aAAa,MAAM;GACxC,MAAM,IAAIA,mBAAAA,YACR;IACE,KAAA,GAAA,qBAAA,oBAAA,CAAwB,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,IAAIF,mBAAAA,YAAY;GACpB,KAAA,GAAA,qBAAA,oBAAA,CAAwB,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
|
@@ -150,7 +150,7 @@ var AstraVector = class extends MastraVector {
|
|
|
150
150
|
sort: { $vector: queryVector },
|
|
151
151
|
limit: topK,
|
|
152
152
|
includeSimilarity: true,
|
|
153
|
-
projection: { $vector: includeVector
|
|
153
|
+
projection: { $vector: !!includeVector }
|
|
154
154
|
}).toArray()).map((result) => ({
|
|
155
155
|
id: result.id,
|
|
156
156
|
score: result.$similarity,
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":["#db"],"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 ? true : false,\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,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,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"}
|
|
1
|
+
{"version":3,"file":"index.js","names":["#db"],"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,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,yBAAyB,KAAK;EAG5C,IAAI,KAAK,gBAAgB,QAAQ,KAAK,MAAM,QAAQ,KAAK,GACvD,OAAO,KAAK,qBAAqB,KAAK;EAGxC,OAAO,KAAK,cA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GED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mastra/astra",
|
|
3
|
-
"version": "1.1.1",
|
|
3
|
+
"version": "1.1.2-alpha.1",
|
|
4
4
|
"description": "Astra DB provider for Mastra - includes vector store capabilities",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -27,16 +27,15 @@
|
|
|
27
27
|
},
|
|
28
28
|
"devDependencies": {
|
|
29
29
|
"@types/node": "22.20.1",
|
|
30
|
-
"@vitest/coverage-v8": "4.1.
|
|
31
|
-
"@vitest/ui": "4.1.
|
|
32
|
-
"eslint": "^10.7.0",
|
|
30
|
+
"@vitest/coverage-v8": "4.1.11",
|
|
31
|
+
"@vitest/ui": "4.1.11",
|
|
33
32
|
"tsdown": "0.22.9",
|
|
34
33
|
"tsx": "^4.23.1",
|
|
35
34
|
"typescript": "^7.0.2",
|
|
36
|
-
"vitest": "4.1.
|
|
37
|
-
"@internal/lint": "0.0.
|
|
38
|
-
"@internal/types-builder": "0.0.
|
|
39
|
-
"@mastra/core": "1.
|
|
35
|
+
"vitest": "4.1.11",
|
|
36
|
+
"@internal/lint": "0.0.144",
|
|
37
|
+
"@internal/types-builder": "0.0.119",
|
|
38
|
+
"@mastra/core": "1.79.0-alpha.9"
|
|
40
39
|
},
|
|
41
40
|
"peerDependencies": {
|
|
42
41
|
"@mastra/core": ">=1.0.0-0 <2.0.0-0"
|
|
@@ -57,7 +56,7 @@
|
|
|
57
56
|
"build:lib": "tsdown --silent --config tsdown.config.ts",
|
|
58
57
|
"build:watch": "tsdown --watch --silent --config tsdown.config.ts",
|
|
59
58
|
"test": "vitest run",
|
|
60
|
-
"lint": "oxlint
|
|
61
|
-
"lint:fix": "oxlint --
|
|
59
|
+
"lint": "oxlint --type-aware .",
|
|
60
|
+
"lint:fix": "oxlint --type-aware --fix ."
|
|
62
61
|
}
|
|
63
62
|
}
|