@mastra/libsql 0.0.0-error-handler-fix-20251020202607 → 0.0.0-esbuild-bundle-worker-20260807173433

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.
Files changed (104) hide show
  1. package/CHANGELOG.md +4789 -3
  2. package/LICENSE.md +15 -0
  3. package/README.md +30 -20
  4. package/dist/docs/SKILL.md +52 -0
  5. package/dist/docs/assets/SOURCE_MAP.json +6 -0
  6. package/dist/docs/references/docs-agents-agent-approval.md +664 -0
  7. package/dist/docs/references/docs-agents-networks.md +184 -0
  8. package/dist/docs/references/docs-deployment-workers.md +137 -0
  9. package/dist/docs/references/docs-editor-overview.md +349 -0
  10. package/dist/docs/references/docs-memory-memory-processors.md +385 -0
  11. package/dist/docs/references/docs-memory-message-history.md +348 -0
  12. package/dist/docs/references/docs-memory-multi-user-threads.md +208 -0
  13. package/dist/docs/references/docs-memory-overview.md +266 -0
  14. package/dist/docs/references/docs-memory-semantic-recall.md +401 -0
  15. package/dist/docs/references/docs-memory-working-memory.md +431 -0
  16. package/dist/docs/references/docs-storage-overview.md +214 -0
  17. package/dist/docs/references/docs-workflows-snapshots.md +240 -0
  18. package/dist/docs/references/guides-agent-frameworks-ai-sdk.md +142 -0
  19. package/dist/docs/references/guides-rag-retrieval.md +520 -0
  20. package/dist/docs/references/reference-core-getMemory.md +51 -0
  21. package/dist/docs/references/reference-core-listMemory.md +57 -0
  22. package/dist/docs/references/reference-core-mastra-class.md +153 -0
  23. package/dist/docs/references/reference-file-based-agents-memory.md +58 -0
  24. package/dist/docs/references/reference-file-based-agents-storage.md +30 -0
  25. package/dist/docs/references/reference-memory-memory-class.md +148 -0
  26. package/dist/docs/references/reference-storage-composite.md +339 -0
  27. package/dist/docs/references/reference-storage-dynamodb.md +284 -0
  28. package/dist/docs/references/reference-storage-libsql.md +143 -0
  29. package/dist/docs/references/reference-storage-retention.md +248 -0
  30. package/dist/docs/references/reference-vectors-libsql.md +307 -0
  31. package/dist/index.cjs +13164 -3390
  32. package/dist/index.cjs.map +1 -1
  33. package/dist/index.js +13134 -3383
  34. package/dist/index.js.map +1 -1
  35. package/dist/storage/db/index.d.ts +389 -0
  36. package/dist/storage/db/index.d.ts.map +1 -0
  37. package/dist/storage/db/utils.d.ts +84 -0
  38. package/dist/storage/db/utils.d.ts.map +1 -0
  39. package/dist/storage/db/write-lock.d.ts +8 -0
  40. package/dist/storage/db/write-lock.d.ts.map +1 -0
  41. package/dist/storage/domains/agents/index.d.ts +30 -0
  42. package/dist/storage/domains/agents/index.d.ts.map +1 -0
  43. package/dist/storage/domains/background-tasks/index.d.ts +27 -0
  44. package/dist/storage/domains/background-tasks/index.d.ts.map +1 -0
  45. package/dist/storage/domains/blobs/index.d.ts +17 -0
  46. package/dist/storage/domains/blobs/index.d.ts.map +1 -0
  47. package/dist/storage/domains/channels/index.d.ts +20 -0
  48. package/dist/storage/domains/channels/index.d.ts.map +1 -0
  49. package/dist/storage/domains/datasets/index.d.ts +43 -0
  50. package/dist/storage/domains/datasets/index.d.ts.map +1 -0
  51. package/dist/storage/domains/experiments/index.d.ts +54 -0
  52. package/dist/storage/domains/experiments/index.d.ts.map +1 -0
  53. package/dist/storage/domains/favorites/index.d.ts +17 -0
  54. package/dist/storage/domains/favorites/index.d.ts.map +1 -0
  55. package/dist/storage/domains/harness/index.d.ts +17 -0
  56. package/dist/storage/domains/harness/index.d.ts.map +1 -0
  57. package/dist/storage/domains/mcp-clients/index.d.ts +26 -0
  58. package/dist/storage/domains/mcp-clients/index.d.ts.map +1 -0
  59. package/dist/storage/domains/mcp-servers/index.d.ts +26 -0
  60. package/dist/storage/domains/mcp-servers/index.d.ts.map +1 -0
  61. package/dist/storage/domains/memory/index.d.ts +58 -54
  62. package/dist/storage/domains/memory/index.d.ts.map +1 -1
  63. package/dist/storage/domains/notifications/index.d.ts +23 -0
  64. package/dist/storage/domains/notifications/index.d.ts.map +1 -0
  65. package/dist/storage/domains/observability/index.d.ts +49 -29
  66. package/dist/storage/domains/observability/index.d.ts.map +1 -1
  67. package/dist/storage/domains/prompt-blocks/index.d.ts +25 -0
  68. package/dist/storage/domains/prompt-blocks/index.d.ts.map +1 -0
  69. package/dist/storage/domains/schedules/index.d.ts +27 -0
  70. package/dist/storage/domains/schedules/index.d.ts.map +1 -0
  71. package/dist/storage/domains/scorer-definitions/index.d.ts +26 -0
  72. package/dist/storage/domains/scorer-definitions/index.d.ts.map +1 -0
  73. package/dist/storage/domains/scores/index.d.ts +29 -31
  74. package/dist/storage/domains/scores/index.d.ts.map +1 -1
  75. package/dist/storage/domains/skills/index.d.ts +26 -0
  76. package/dist/storage/domains/skills/index.d.ts.map +1 -0
  77. package/dist/storage/domains/thread-state/index.d.ts +38 -0
  78. package/dist/storage/domains/thread-state/index.d.ts.map +1 -0
  79. package/dist/storage/domains/tool-provider-connections/index.d.ts +14 -0
  80. package/dist/storage/domains/tool-provider-connections/index.d.ts.map +1 -0
  81. package/dist/storage/domains/utils.d.ts +28 -54
  82. package/dist/storage/domains/utils.d.ts.map +1 -1
  83. package/dist/storage/domains/workflow-definitions/index.d.ts +14 -0
  84. package/dist/storage/domains/workflow-definitions/index.d.ts.map +1 -0
  85. package/dist/storage/domains/workflows/index.d.ts +27 -32
  86. package/dist/storage/domains/workflows/index.d.ts.map +1 -1
  87. package/dist/storage/domains/workspaces/index.d.ts +26 -0
  88. package/dist/storage/domains/workspaces/index.d.ts.map +1 -0
  89. package/dist/storage/factory-storage.d.ts +28 -0
  90. package/dist/storage/factory-storage.d.ts.map +1 -0
  91. package/dist/storage/index.d.ts +128 -255
  92. package/dist/storage/index.d.ts.map +1 -1
  93. package/dist/storage/retention.d.ts +77 -0
  94. package/dist/storage/retention.d.ts.map +1 -0
  95. package/dist/vector/index.d.ts +31 -4
  96. package/dist/vector/index.d.ts.map +1 -1
  97. package/dist/vector/sql-builder.d.ts.map +1 -1
  98. package/package.json +22 -16
  99. package/dist/storage/domains/legacy-evals/index.d.ts +0 -18
  100. package/dist/storage/domains/legacy-evals/index.d.ts.map +0 -1
  101. package/dist/storage/domains/operations/index.d.ts +0 -110
  102. package/dist/storage/domains/operations/index.d.ts.map +0 -1
  103. package/dist/storage/domains/traces/index.d.ts +0 -21
  104. package/dist/storage/domains/traces/index.d.ts.map +0 -1
@@ -0,0 +1,520 @@
1
+ > Discover all available pages from the documentation index: https://mastra.ai/llms.txt
2
+
3
+ # Retrieval in RAG systems
4
+
5
+ After storing embeddings, you need to retrieve relevant chunks to answer user queries.
6
+
7
+ Mastra provides flexible retrieval options with support for semantic search, filtering, and re-ranking.
8
+
9
+ ## How retrieval works
10
+
11
+ 1. The user's query is converted to an embedding using the same model used for document embeddings
12
+ 2. This embedding is compared to stored embeddings using vector similarity
13
+ 3. The most similar chunks are retrieved and can be optionally:
14
+
15
+ - Filtered by metadata
16
+ - Re-ranked for better relevance
17
+ - Processed through a knowledge graph
18
+
19
+ ## Basic retrieval
20
+
21
+ The simplest approach is direct semantic search. This method uses vector similarity to find chunks that are semantically similar to the query:
22
+
23
+ ```ts
24
+ import { embed } from 'ai'
25
+ import { PgVector } from '@mastra/pg'
26
+ import { ModelRouterEmbeddingModel } from '@mastra/core/llm'
27
+
28
+ // Convert query to embedding
29
+ const { embedding } = await embed({
30
+ value: 'What are the main points in the article?',
31
+ model: new ModelRouterEmbeddingModel('openai/text-embedding-3-small'),
32
+ })
33
+
34
+ // Query vector store
35
+ const pgVector = new PgVector({
36
+ id: 'pg-vector',
37
+ connectionString: process.env.POSTGRES_CONNECTION_STRING,
38
+ })
39
+ const results = await pgVector.query({
40
+ indexName: 'embeddings',
41
+ queryVector: embedding,
42
+ topK: 10,
43
+ })
44
+
45
+ // Display results
46
+ console.log(results)
47
+ ```
48
+
49
+ The `topK` parameter specifies the maximum number of most similar results to return from the vector search.
50
+
51
+ Results include both the text content and a similarity score:
52
+
53
+ ```ts
54
+ [
55
+ {
56
+ text: 'Climate change poses significant challenges...',
57
+ score: 0.89,
58
+ metadata: { source: 'article1.txt' },
59
+ },
60
+ {
61
+ text: 'Rising temperatures affect crop yields...',
62
+ score: 0.82,
63
+ metadata: { source: 'article1.txt' },
64
+ },
65
+ ]
66
+ ```
67
+
68
+ ## Advanced retrieval options
69
+
70
+ ### Metadata Filtering
71
+
72
+ Filter results based on metadata fields to narrow down the search space. This approach - combining vector similarity search with metadata filters - is sometimes called hybrid vector search, as it merges semantic search with structured filtering criteria.
73
+
74
+ This is useful when you have documents from different sources, time periods, or with specific attributes. Mastra provides a unified MongoDB-style query syntax that works across all supported vector stores.
75
+
76
+ For detailed information about available operators and syntax, see the [Metadata Filters Reference](https://mastra.ai/reference/rag/metadata-filters).
77
+
78
+ Basic filtering examples:
79
+
80
+ ```ts
81
+ // Simple equality filter
82
+ const results = await pgVector.query({
83
+ indexName: 'embeddings',
84
+ queryVector: embedding,
85
+ topK: 10,
86
+ filter: {
87
+ source: 'article1.txt',
88
+ },
89
+ })
90
+
91
+ // Numeric comparison
92
+ const results = await pgVector.query({
93
+ indexName: 'embeddings',
94
+ queryVector: embedding,
95
+ topK: 10,
96
+ filter: {
97
+ price: { $gt: 100 },
98
+ },
99
+ })
100
+
101
+ // Multiple conditions
102
+ const results = await pgVector.query({
103
+ indexName: 'embeddings',
104
+ queryVector: embedding,
105
+ topK: 10,
106
+ filter: {
107
+ category: 'electronics',
108
+ price: { $lt: 1000 },
109
+ inStock: true,
110
+ },
111
+ })
112
+
113
+ // Array operations
114
+ const results = await pgVector.query({
115
+ indexName: 'embeddings',
116
+ queryVector: embedding,
117
+ topK: 10,
118
+ filter: {
119
+ tags: { $in: ['sale', 'new'] },
120
+ },
121
+ })
122
+
123
+ // Logical operators
124
+ const results = await pgVector.query({
125
+ indexName: 'embeddings',
126
+ queryVector: embedding,
127
+ topK: 10,
128
+ filter: {
129
+ $or: [{ category: 'electronics' }, { category: 'accessories' }],
130
+ $and: [{ price: { $gt: 50 } }, { price: { $lt: 200 } }],
131
+ },
132
+ })
133
+ ```
134
+
135
+ Common use cases for metadata filtering:
136
+
137
+ - Filter by document source or type
138
+ - Filter by date ranges
139
+ - Filter by specific categories or tags
140
+ - Filter by numerical ranges (e.g., price, rating)
141
+ - Combine multiple conditions for precise querying
142
+ - Filter by document attributes (e.g., language, author)
143
+
144
+ ### Vector Query Tool
145
+
146
+ Sometimes you want to give your agent the ability to query a vector database directly. The Vector Query Tool allows your agent to be in charge of retrieval decisions, combining semantic search with optional filtering and reranking based on the agent's understanding of the user's needs.
147
+
148
+ ```ts
149
+ import { createVectorQueryTool } from '@mastra/rag'
150
+ import { ModelRouterEmbeddingModel } from '@mastra/core/llm'
151
+
152
+ const vectorQueryTool = createVectorQueryTool({
153
+ vectorStoreName: 'pgVector',
154
+ indexName: 'embeddings',
155
+ model: new ModelRouterEmbeddingModel('openai/text-embedding-3-small'),
156
+ })
157
+ ```
158
+
159
+ When creating the tool, pay special attention to the tool's name and description - these help the agent understand when and how to use the retrieval capabilities. For example, you might name it "SearchKnowledgeBase" and describe it as "Search through our documentation to find relevant information about X topic."
160
+
161
+ This is particularly useful when:
162
+
163
+ - Your agent needs to decide at runtime what information to retrieve
164
+ - The retrieval process requires complex decision-making
165
+ - You want the agent to combine multiple retrieval strategies based on context
166
+
167
+ #### Database-Specific Configurations
168
+
169
+ The Vector Query Tool supports database-specific configurations that enable you to use unique features and optimizations of different vector stores.
170
+
171
+ > **Note:** These configurations are for **query-time options** like namespaces, performance tuning, and filtering, not for database connection setup.
172
+ >
173
+ > Connection credentials (URLs, auth tokens) are configured when you instantiate the vector store class (e.g., `new LibSQLVector({ url: '...' })`).
174
+
175
+ ```ts
176
+ import { createVectorQueryTool } from '@mastra/rag'
177
+ import { ModelRouterEmbeddingModel } from '@mastra/core/llm'
178
+
179
+ // Pinecone with namespace
180
+ const pineconeQueryTool = createVectorQueryTool({
181
+ vectorStoreName: 'pinecone',
182
+ indexName: 'docs',
183
+ model: new ModelRouterEmbeddingModel('openai/text-embedding-3-small'),
184
+ databaseConfig: {
185
+ pinecone: {
186
+ namespace: 'production', // Isolate data by environment
187
+ },
188
+ },
189
+ })
190
+
191
+ // pgVector with performance tuning
192
+ const pgVectorQueryTool = createVectorQueryTool({
193
+ vectorStoreName: 'postgres',
194
+ indexName: 'embeddings',
195
+ model: new ModelRouterEmbeddingModel('openai/text-embedding-3-small'),
196
+ databaseConfig: {
197
+ pgvector: {
198
+ minScore: 0.7, // Filter low-quality results
199
+ ef: 200, // HNSW search parameter
200
+ probes: 10, // IVFFlat probe parameter
201
+ },
202
+ },
203
+ })
204
+
205
+ // Chroma with advanced filtering
206
+ const chromaQueryTool = createVectorQueryTool({
207
+ vectorStoreName: 'chroma',
208
+ indexName: 'documents',
209
+ model: new ModelRouterEmbeddingModel('openai/text-embedding-3-small'),
210
+ databaseConfig: {
211
+ chroma: {
212
+ where: { category: 'technical' },
213
+ whereDocument: { $contains: 'API' },
214
+ },
215
+ },
216
+ })
217
+
218
+ // LanceDB with table specificity
219
+ const lanceQueryTool = createVectorQueryTool({
220
+ vectorStoreName: 'lance',
221
+ indexName: 'documents',
222
+ model: new ModelRouterEmbeddingModel('openai/text-embedding-3-small'),
223
+ databaseConfig: {
224
+ lance: {
225
+ tableName: 'myVectors', // Specify which table to query
226
+ includeAllColumns: true, // Include all metadata columns in results
227
+ },
228
+ },
229
+ })
230
+ ```
231
+
232
+ **Key Benefits:**
233
+
234
+ - **Pinecone namespaces**: Organize vectors by tenant, environment, or data type
235
+ - **pgVector optimization**: Control search accuracy and speed with ef/probes parameters
236
+ - **Quality filtering**: Set minimum similarity thresholds to improve result relevance
237
+ - **LanceDB tables**: Separate data into tables for better organization and performance
238
+ - **Runtime flexibility**: Override configurations at runtime based on context
239
+
240
+ **Common Use Cases:**
241
+
242
+ - Multi-tenant applications using Pinecone namespaces
243
+ - Performance optimization in high-load scenarios
244
+ - Environment-specific configurations (dev/staging/prod)
245
+ - Quality-gated search results
246
+ - Embedded, file-based vector storage with LanceDB for edge deployment scenarios
247
+
248
+ You can also override these configurations at runtime using the request context:
249
+
250
+ ```ts
251
+ import { RequestContext } from '@mastra/core/request-context'
252
+
253
+ const requestContext = new RequestContext()
254
+ requestContext.set('databaseConfig', {
255
+ pinecone: {
256
+ namespace: 'runtime-namespace',
257
+ },
258
+ })
259
+
260
+ await pineconeQueryTool.execute({ queryText: 'search query' }, { mastra, requestContext })
261
+ ```
262
+
263
+ For detailed configuration options and advanced usage, see the [Vector Query Tool Reference](https://mastra.ai/reference/tools/vector-query-tool).
264
+
265
+ ### Vector Store Prompts
266
+
267
+ Vector store prompts define query patterns and filtering capabilities for each vector database implementation. When implementing filtering, these prompts are required in the agent's instructions to specify valid operators and syntax for each vector store implementation.
268
+
269
+ **pgVector**:
270
+
271
+ ```ts
272
+ import { PGVECTOR_PROMPT } from '@mastra/pg'
273
+
274
+ export const ragAgent = new Agent({
275
+ id: 'rag-agent',
276
+ name: 'RAG Agent',
277
+ model: 'openai/gpt-5.6-sol',
278
+ instructions: `
279
+ Process queries using the provided context. Structure responses to be concise and relevant.
280
+ ${PGVECTOR_PROMPT}
281
+ `,
282
+ tools: { vectorQueryTool },
283
+ })
284
+ ```
285
+
286
+ **Pinecone**:
287
+
288
+ ```ts
289
+ import { PINECONE_PROMPT } from '@mastra/pinecone'
290
+
291
+ export const ragAgent = new Agent({
292
+ id: 'rag-agent',
293
+ name: 'RAG Agent',
294
+ model: 'openai/gpt-5.6-sol',
295
+ instructions: `
296
+ Process queries using the provided context. Structure responses to be concise and relevant.
297
+ ${PINECONE_PROMPT}
298
+ `,
299
+ tools: { vectorQueryTool },
300
+ })
301
+ ```
302
+
303
+ **Qdrant**:
304
+
305
+ ```ts
306
+ import { QDRANT_PROMPT } from '@mastra/qdrant'
307
+
308
+ export const ragAgent = new Agent({
309
+ id: 'rag-agent',
310
+ name: 'RAG Agent',
311
+ model: 'openai/gpt-5.6-sol',
312
+ instructions: `
313
+ Process queries using the provided context. Structure responses to be concise and relevant.
314
+ ${QDRANT_PROMPT}
315
+ `,
316
+ tools: { vectorQueryTool },
317
+ })
318
+ ```
319
+
320
+ **Chroma**:
321
+
322
+ ```ts
323
+ import { CHROMA_PROMPT } from '@mastra/chroma'
324
+
325
+ export const ragAgent = new Agent({
326
+ id: 'rag-agent',
327
+ name: 'RAG Agent',
328
+ model: 'openai/gpt-5.6-sol',
329
+ instructions: `
330
+ Process queries using the provided context. Structure responses to be concise and relevant.
331
+ ${CHROMA_PROMPT}
332
+ `,
333
+ tools: { vectorQueryTool },
334
+ })
335
+ ```
336
+
337
+ **Astra**:
338
+
339
+ ```ts
340
+ import { ASTRA_PROMPT } from '@mastra/astra'
341
+
342
+ export const ragAgent = new Agent({
343
+ id: 'rag-agent',
344
+ name: 'RAG Agent',
345
+ model: 'openai/gpt-5.6-sol',
346
+ instructions: `
347
+ Process queries using the provided context. Structure responses to be concise and relevant.
348
+ ${ASTRA_PROMPT}
349
+ `,
350
+ tools: { vectorQueryTool },
351
+ })
352
+ ```
353
+
354
+ **libSQL**:
355
+
356
+ ```ts
357
+ import { LIBSQL_PROMPT } from '@mastra/libsql'
358
+
359
+ export const ragAgent = new Agent({
360
+ id: 'rag-agent',
361
+ name: 'RAG Agent',
362
+ model: 'openai/gpt-5.6-sol',
363
+ instructions: `
364
+ Process queries using the provided context. Structure responses to be concise and relevant.
365
+ ${LIBSQL_PROMPT}
366
+ `,
367
+ tools: { vectorQueryTool },
368
+ })
369
+ ```
370
+
371
+ **Upstash**:
372
+
373
+ ```ts
374
+ import { UPSTASH_PROMPT } from '@mastra/upstash'
375
+
376
+ export const ragAgent = new Agent({
377
+ id: 'rag-agent',
378
+ name: 'RAG Agent',
379
+ model: 'openai/gpt-5.6-sol',
380
+ instructions: `
381
+ Process queries using the provided context. Structure responses to be concise and relevant.
382
+ ${UPSTASH_PROMPT}
383
+ `,
384
+ tools: { vectorQueryTool },
385
+ })
386
+ ```
387
+
388
+ **Vectorize**:
389
+
390
+ ```ts
391
+ import { VECTORIZE_PROMPT } from '@mastra/vectorize'
392
+
393
+ export const ragAgent = new Agent({
394
+ id: 'rag-agent',
395
+ name: 'RAG Agent',
396
+ model: 'openai/gpt-5.6-sol',
397
+ instructions: `
398
+ Process queries using the provided context. Structure responses to be concise and relevant.
399
+ ${VECTORIZE_PROMPT}
400
+ `,
401
+ tools: { vectorQueryTool },
402
+ })
403
+ ```
404
+
405
+ **MongoDB**:
406
+
407
+ ```ts
408
+ import { MONGODB_PROMPT } from '@mastra/mongodb'
409
+
410
+ export const ragAgent = new Agent({
411
+ id: 'rag-agent',
412
+ name: 'RAG Agent',
413
+ model: 'openai/gpt-5.6-sol',
414
+ instructions: `
415
+ Process queries using the provided context. Structure responses to be concise and relevant.
416
+ ${MONGODB_PROMPT}
417
+ `,
418
+ tools: { vectorQueryTool },
419
+ })
420
+ ```
421
+
422
+ **OpenSearch**:
423
+
424
+ ```ts
425
+ import { OPENSEARCH_PROMPT } from '@mastra/opensearch'
426
+
427
+ export const ragAgent = new Agent({
428
+ id: 'rag-agent',
429
+ name: 'RAG Agent',
430
+ model: 'openai/gpt-5.6-sol',
431
+ instructions: `
432
+ Process queries using the provided context. Structure responses to be concise and relevant.
433
+ ${OPENSEARCH_PROMPT}
434
+ `,
435
+ tools: { vectorQueryTool },
436
+ })
437
+ ```
438
+
439
+ **S3Vectors**:
440
+
441
+ ```ts
442
+ import { S3VECTORS_PROMPT } from '@mastra/s3vectors'
443
+
444
+ export const ragAgent = new Agent({
445
+ id: 'rag-agent',
446
+ name: 'RAG Agent',
447
+ model: 'openai/gpt-5.6-sol',
448
+ instructions: `
449
+ Process queries using the provided context. Structure responses to be concise and relevant.
450
+ ${S3VECTORS_PROMPT}
451
+ `,
452
+ tools: { vectorQueryTool },
453
+ })
454
+ ```
455
+
456
+ ### Re-ranking
457
+
458
+ Initial vector similarity search can sometimes miss detailed relevance. Re-ranking is a more computationally expensive process, but more accurate algorithm that improves results by:
459
+
460
+ - Considering word order and exact matches
461
+ - Applying more advanced relevance scoring
462
+ - Using a method called cross-attention between query and documents
463
+
464
+ Here's how to use re-ranking:
465
+
466
+ ```ts
467
+ import { rerankWithScorer as rerank, MastraAgentRelevanceScorer } from '@mastra/rag'
468
+
469
+ // Get initial results from vector search
470
+ const initialResults = await pgVector.query({
471
+ indexName: 'embeddings',
472
+ queryVector: queryEmbedding,
473
+ topK: 10,
474
+ })
475
+
476
+ // Create a relevance scorer
477
+ const relevanceProvider = new MastraAgentRelevanceScorer(
478
+ 'relevance-scorer',
479
+ 'openai/gpt-5.6-sol',
480
+ )
481
+
482
+ // Re-rank the results
483
+ const rerankedResults = await rerank({
484
+ results: initialResults,
485
+ query,
486
+ scorer: relevanceProvider,
487
+ options: {
488
+ weights: {
489
+ semantic: 0.5, // How well the content matches the query semantically
490
+ vector: 0.3, // Original vector similarity score
491
+ position: 0.2, // Preserves original result ordering
492
+ },
493
+ topK: 10,
494
+ },
495
+ })
496
+ ```
497
+
498
+ The weights control how different factors influence the final ranking:
499
+
500
+ - `semantic`: Higher values prioritize semantic understanding and relevance to the query
501
+ - `vector`: Higher values favor the original vector similarity scores
502
+ - `position`: Higher values help maintain the original ordering of results
503
+
504
+ > **Note:** For semantic scoring to work properly during re-ranking, each result must include the text content in its `metadata.text` field.
505
+
506
+ You can also use other relevance score providers like Cohere or ZeroEntropy:
507
+
508
+ ```ts
509
+ const relevanceProvider = new CohereRelevanceScorer('rerank-v3.5')
510
+ ```
511
+
512
+ ```ts
513
+ const relevanceProvider = new ZeroEntropyRelevanceScorer('zerank-1')
514
+ ```
515
+
516
+ The re-ranked results combine vector similarity with semantic understanding to improve retrieval quality.
517
+
518
+ For more details about re-ranking, see the [rerank()](https://mastra.ai/reference/rag/rerankWithScorer) method.
519
+
520
+ For graph-based retrieval that follows connections between chunks, see the [GraphRAG](https://mastra.ai/guides/rag/graph-rag) documentation.
@@ -0,0 +1,51 @@
1
+ > Discover all available pages from the documentation index: https://mastra.ai/llms.txt
2
+
3
+ # Mastra.getMemory()
4
+
5
+ The `.getMemory()` method retrieves a memory instance from the Mastra registry by its key. Memory instances are registered in the Mastra constructor and can be referenced by stored agents.
6
+
7
+ ## Usage example
8
+
9
+ ```typescript
10
+ const memory = mastra.getMemory('conversationMemory')
11
+
12
+ // Use the memory instance
13
+ const thread = await memory.createThread({
14
+ resourceId: 'user-123',
15
+ title: 'New Conversation',
16
+ })
17
+ ```
18
+
19
+ ## Parameters
20
+
21
+ **key** (`TMemoryKey extends keyof TMemory`): The registry key of the memory instance to retrieve. Must match a key used when registering memory in the Mastra constructor.
22
+
23
+ ## Returns
24
+
25
+ **memory** (`TMemory[TMemoryKey]`): The memory instance with the specified key. Throws an error if the memory is not found.
26
+
27
+ ## Example: Registering and retrieving memory
28
+
29
+ ```typescript
30
+ import { Mastra } from '@mastra/core'
31
+ import { Memory } from '@mastra/memory'
32
+ import { LibSQLStore } from '@mastra/libsql'
33
+
34
+ const conversationMemory = new Memory({
35
+ storage: new LibSQLStore({ id: 'conversation-store', url: ':memory:' }),
36
+ })
37
+
38
+ const mastra = new Mastra({
39
+ memory: {
40
+ conversationMemory,
41
+ },
42
+ })
43
+
44
+ // Later, retrieve the memory instance
45
+ const memory = mastra.getMemory('conversationMemory')
46
+ ```
47
+
48
+ ## Related
49
+
50
+ - [Mastra.listMemory()](https://mastra.ai/reference/core/listMemory)
51
+ - [Memory overview](https://mastra.ai/docs/memory/overview)
@@ -0,0 +1,57 @@
1
+ > Discover all available pages from the documentation index: https://mastra.ai/llms.txt
2
+
3
+ # Mastra.listMemory()
4
+
5
+ The `.listMemory()` method returns all memory instances registered with the Mastra instance.
6
+
7
+ ## Usage example
8
+
9
+ ```typescript
10
+ const memoryInstances = mastra.listMemory()
11
+
12
+ for (const [key, memory] of Object.entries(memoryInstances)) {
13
+ console.log(`Memory "${key}": ${memory.id}`)
14
+ }
15
+ ```
16
+
17
+ ## Parameters
18
+
19
+ This method takes no parameters.
20
+
21
+ ## Returns
22
+
23
+ **memory** (`Record<string, MastraMemory>`): An object containing all registered memory instances, keyed by their registry keys.
24
+
25
+ ## Example: Checking registered memory
26
+
27
+ ```typescript
28
+ import { Mastra } from '@mastra/core'
29
+ import { Memory } from '@mastra/memory'
30
+ import { LibSQLStore } from '@mastra/libsql'
31
+
32
+ const conversationMemory = new Memory({
33
+ id: 'conversation-memory',
34
+ storage: new LibSQLStore({ id: 'conversation-store', url: ':memory:' }),
35
+ })
36
+
37
+ const analyticsMemory = new Memory({
38
+ id: 'analytics-memory',
39
+ storage: new LibSQLStore({ id: 'analytics-store', url: ':memory:' }),
40
+ })
41
+
42
+ const mastra = new Mastra({
43
+ memory: {
44
+ conversationMemory,
45
+ analyticsMemory,
46
+ },
47
+ })
48
+
49
+ // List all registered memory instances
50
+ const allMemory = mastra.listMemory()
51
+ console.log(Object.keys(allMemory)) // ["conversationMemory", "analyticsMemory"]
52
+ ```
53
+
54
+ ## Related
55
+
56
+ - [Mastra.getMemory()](https://mastra.ai/reference/core/getMemory)
57
+ - [Memory overview](https://mastra.ai/docs/memory/overview)