@mastra/memory 1.27.0 → 1.28.0-alpha.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +50 -0
- package/dist/docs/SKILL.md +4 -2
- package/dist/docs/assets/SOURCE_MAP.json +1 -1
- package/dist/docs/references/docs-guides-context-engineering.md +297 -0
- package/dist/docs/references/docs-storage.md +1 -0
- package/dist/docs/references/integrations-databases-mongodb.md +1 -1
- package/dist/docs/references/integrations-databases-postgresql.md +2 -0
- package/dist/docs/references/integrations-databases-valkey.md +99 -0
- package/dist/docs/references/reference-vectors-mongodb.md +11 -11
- package/dist/docs/references/reference-vectors-pg.md +2 -0
- package/dist/index.cjs +1 -1
- package/dist/index.d.ts +6 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/processors/index.cjs +1 -1
- package/dist/processors/index.js +1 -1
- package/dist/processors/observational-memory/observation-turn/load-memory-context.d.ts +3 -1
- package/dist/processors/observational-memory/observation-turn/load-memory-context.d.ts.map +1 -1
- package/dist/processors/observational-memory/observation-turn/turn.d.ts +2 -1
- package/dist/processors/observational-memory/observation-turn/turn.d.ts.map +1 -1
- package/dist/processors/observational-memory/observational-memory.d.ts +6 -12
- package/dist/processors/observational-memory/observational-memory.d.ts.map +1 -1
- package/dist/processors/observational-memory/observer-agent.d.ts.map +1 -1
- package/dist/processors/observational-memory/processor.d.ts +2 -0
- package/dist/processors/observational-memory/processor.d.ts.map +1 -1
- package/dist/processors/observational-memory/reflector-runner.d.ts.map +1 -1
- package/dist/processors/observational-memory/tracing.d.ts.map +1 -1
- package/dist/processors/observational-memory/types.d.ts +23 -13
- package/dist/processors/observational-memory/types.d.ts.map +1 -1
- package/dist/{src-CxkQ-ICG.cjs → src-CIBCjg2A.cjs} +272 -158
- package/dist/{src-CxkQ-ICG.cjs.map → src-CIBCjg2A.cjs.map} +1 -1
- package/dist/{src-aTfbkxBl.js → src-kLYHhPqh.js} +272 -158
- package/dist/{src-aTfbkxBl.js.map → src-kLYHhPqh.js.map} +1 -1
- package/package.json +7 -7
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,55 @@
|
|
|
1
1
|
# @mastra/memory
|
|
2
2
|
|
|
3
|
+
## 1.28.0-alpha.2
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- Observational memory now detects observer/reflector output where a multi-line block repeats many times (a model repetition loop). Previously such output could slip past the degenerate-output check and balloon stored observations, causing constant synchronous reflection churn; it is now rejected and retried like other degenerate output. ([#22072](https://github.com/mastra-ai/mastra/pull/22072))
|
|
8
|
+
|
|
9
|
+
- Fixed observational memory reflection to keep retrying with stronger compression when a result remains above the token threshold. ([#22232](https://github.com/mastra-ai/mastra/pull/22232))
|
|
10
|
+
|
|
11
|
+
- Updated dependencies [[`ae8790c`](https://github.com/mastra-ai/mastra/commit/ae8790c4bfaa088d2ab279d1dcc06f326b9fd109), [`04a815f`](https://github.com/mastra-ai/mastra/commit/04a815fc8971d29e97fcdcc5008a1eb472fc00ff), [`cced745`](https://github.com/mastra-ai/mastra/commit/cced745a056ec2225c5bc702e32d848847aa8b65)]:
|
|
12
|
+
- @mastra/core@1.62.0-alpha.7
|
|
13
|
+
|
|
14
|
+
## 1.28.0-alpha.1
|
|
15
|
+
|
|
16
|
+
### Minor Changes
|
|
17
|
+
|
|
18
|
+
- Added opt-in awaited Observational Memory hooks for synchronous cycles. ([#22147](https://github.com/mastra-ai/mastra/pull/22147))
|
|
19
|
+
|
|
20
|
+
Set `hookExecution: "await"` to await lifecycle hooks, stop the observer or reflector when a start hook fails, and receive one paired end callback after cleanup. Async-buffer cycles remain fire-and-forget.
|
|
21
|
+
|
|
22
|
+
```ts
|
|
23
|
+
const memory = new Memory({
|
|
24
|
+
options: {
|
|
25
|
+
observationalMemory: {
|
|
26
|
+
hookExecution: 'await',
|
|
27
|
+
hooks: {
|
|
28
|
+
onObservationStart: async context => {
|
|
29
|
+
await authorizeObservation(context);
|
|
30
|
+
},
|
|
31
|
+
},
|
|
32
|
+
},
|
|
33
|
+
},
|
|
34
|
+
});
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
### Patch Changes
|
|
38
|
+
|
|
39
|
+
- Updated dependencies [[`65edab1`](https://github.com/mastra-ai/mastra/commit/65edab1c233d17b8f163bad12fca410d0e6f16b1), [`ab20a38`](https://github.com/mastra-ai/mastra/commit/ab20a38d0275f8d85e0f3833bd87ef487bcc609f), [`dbbfeb8`](https://github.com/mastra-ai/mastra/commit/dbbfeb85ec949dc9ebc0755e1ad262e4f5eba8db), [`3cc9d00`](https://github.com/mastra-ai/mastra/commit/3cc9d00b2b4333e0377a5e9df5eff92c17ce7630), [`733a537`](https://github.com/mastra-ai/mastra/commit/733a537489a858b5880b2e98809334fba895a221), [`9207dfa`](https://github.com/mastra-ai/mastra/commit/9207dfab8062e5fc68b751684797ff86fe0b4e70), [`12c61d2`](https://github.com/mastra-ai/mastra/commit/12c61d280c8cb208bc3c8dbcbe5dcc60cf9d1cd0), [`9a12ef3`](https://github.com/mastra-ai/mastra/commit/9a12ef3fccf3f4186db0f294f4ee1f02cf4d8db2)]:
|
|
40
|
+
- @mastra/core@1.62.0-alpha.5
|
|
41
|
+
|
|
42
|
+
## 1.27.1-alpha.0
|
|
43
|
+
|
|
44
|
+
### Patch Changes
|
|
45
|
+
|
|
46
|
+
- Fixed Observational Memory tracing spans (`om.observer`, `om.observer.multi-thread`, `om.reflector`) never being ended. Unended spans kept their traces retained in exporters that hold a trace open until every span in it finishes — a memory leak with the Datadog bridge, which retains full LLM Observability payloads — and meant Observational Memory tracing never reached any exporter. The spans now end when the observer/reflector run completes, and record the error before ending when it fails. ([#22117](https://github.com/mastra-ai/mastra/pull/22117))
|
|
47
|
+
|
|
48
|
+
- Improved agent turn latency by reusing run-scoped memory reads ([#22076](https://github.com/mastra-ai/mastra/pull/22076))
|
|
49
|
+
|
|
50
|
+
- Updated dependencies [[`2c85f42`](https://github.com/mastra-ai/mastra/commit/2c85f428e04ccd63ea31a7ec80b5b327afdad555), [`11bbeb9`](https://github.com/mastra-ai/mastra/commit/11bbeb9b108ef2264e05acefc6dafb9cbb342921), [`1a485f3`](https://github.com/mastra-ai/mastra/commit/1a485f3538f5ec64d58bd8b5e1e99de0c695c87b), [`0d37487`](https://github.com/mastra-ai/mastra/commit/0d37487d9f349388a3f1cef6a536cf9dcc4b6273), [`8661d7d`](https://github.com/mastra-ai/mastra/commit/8661d7d7179f0a024456aabdd8679bcecd09ac28), [`575e343`](https://github.com/mastra-ai/mastra/commit/575e343900451021d96110916497d334af7bc252), [`cacb839`](https://github.com/mastra-ai/mastra/commit/cacb8392d9e74189b56d857290b0615f98a2683d), [`b47b26e`](https://github.com/mastra-ai/mastra/commit/b47b26e6fe95cb8a3482be2c5e52de157fe59d0b), [`0d37487`](https://github.com/mastra-ai/mastra/commit/0d37487d9f349388a3f1cef6a536cf9dcc4b6273), [`c46eb09`](https://github.com/mastra-ai/mastra/commit/c46eb09ce4987509af57a0ac582c61241a6dd2f1), [`30ed33e`](https://github.com/mastra-ai/mastra/commit/30ed33ee14084a26019aba15fceadda6d6ddefaf), [`91ad69d`](https://github.com/mastra-ai/mastra/commit/91ad69d64994c89199b0c55399e64ed91c61df2f), [`8dc408d`](https://github.com/mastra-ai/mastra/commit/8dc408d34438f9e13297f792c11a5cfd6cf952e1), [`c92def1`](https://github.com/mastra-ai/mastra/commit/c92def10a13c822972c96f0a4ca6ffc1f4258aed), [`c5eaec5`](https://github.com/mastra-ai/mastra/commit/c5eaec5a860d80d0e3805e67db0414b87ac8cbed), [`e66b2ba`](https://github.com/mastra-ai/mastra/commit/e66b2ba100db63eaeab6e21e1ea34b113f2ec781)]:
|
|
51
|
+
- @mastra/core@1.62.0-alpha.3
|
|
52
|
+
|
|
3
53
|
## 1.27.0
|
|
4
54
|
|
|
5
55
|
### Minor Changes
|
package/dist/docs/SKILL.md
CHANGED
|
@@ -3,7 +3,7 @@ name: mastra-memory
|
|
|
3
3
|
description: Documentation for @mastra/memory. Use when working with @mastra/memory APIs, configuration, or implementation.
|
|
4
4
|
metadata:
|
|
5
5
|
package: "@mastra/memory"
|
|
6
|
-
version: "1.
|
|
6
|
+
version: "1.28.0-alpha.2"
|
|
7
7
|
---
|
|
8
8
|
|
|
9
9
|
## When to use
|
|
@@ -19,6 +19,7 @@ Read the individual reference documents for detailed explanations and code examp
|
|
|
19
19
|
- [Human-in-the-loop](references/docs-agents-human-in-the-loop.md) - Learn how to require approvals and suspend tool execution, plus automatically resume suspended tools while keeping humans in control of agent workflows.
|
|
20
20
|
- [Agent networks](references/docs-agents-networks.md) - Coordinate multiple agents, workflows, and tools using agent networks for complex, non-deterministic task execution.
|
|
21
21
|
- [Evals with memory](references/docs-evals-evals-with-memory.md) - Run scorers against memory-enabled agents, including observational memory in thread scope, using runEvals and dataset experiments.
|
|
22
|
+
- [Context engineering](references/docs-guides-context-engineering.md) - Learn how to choose, retrieve, persist, and control the context a Mastra agent receives.
|
|
22
23
|
- [Background tasks](references/docs-harness-background-tasks.md) - Learn how to dispatch long-running tool calls in the background and keep the stream open until they complete, plus orchestrate subagents asynchronously.
|
|
23
24
|
- [Goals](references/docs-harness-goals.md) - Learn how to set a durable objective on an agent that's judged in the execution loop, so the agent keeps working until the goal is complete or the run budget is exhausted.
|
|
24
25
|
- [Memory processors](references/docs-memory-memory-processors.md) - Learn how to use memory processors in Mastra to filter, trim, and transform messages before they're sent to the language model to manage context window limits.
|
|
@@ -42,6 +43,7 @@ Read the individual reference documents for detailed explanations and code examp
|
|
|
42
43
|
- [PostgreSQL](references/integrations-databases-postgresql.md) - Documentation for the PostgreSQL storage implementation in Mastra.
|
|
43
44
|
- [Redis](references/integrations-databases-redis.md) - Documentation for the Redis storage implementation in Mastra.
|
|
44
45
|
- [Upstash](references/integrations-databases-upstash.md) - Documentation for the Upstash storage implementation in Mastra.
|
|
46
|
+
- [Valkey](references/integrations-databases-valkey.md) - Documentation for the GLIDE-backed Valkey storage implementation in Mastra.
|
|
45
47
|
|
|
46
48
|
### Reference
|
|
47
49
|
|
|
@@ -62,7 +64,7 @@ Read the individual reference documents for detailed explanations and code examp
|
|
|
62
64
|
- [Memory](references/reference-migrations-upgrade-to-v1-memory.md) - Learn how to migrate memory-related changes when upgrading to v1.
|
|
63
65
|
- [Reference: TokenLimiterProcessor](references/reference-processors-token-limiter-processor.md) - Documentation for the TokenLimiterProcessor in Mastra, which limits the number of tokens in messages.
|
|
64
66
|
- [Reference: libSQL vector store](references/reference-vectors-libsql.md) - Documentation for the LibSQLVector class in Mastra, which provides vector search using libSQL with vector extensions.
|
|
65
|
-
- [Reference: MongoDB vector store](references/reference-vectors-mongodb.md) - Documentation for the MongoDBVector class in Mastra, which provides vector search using MongoDB Atlas and
|
|
67
|
+
- [Reference: MongoDB vector store](references/reference-vectors-mongodb.md) - Documentation for the MongoDBVector class in Mastra, which provides vector search using MongoDB Atlas and Vector Search.
|
|
66
68
|
- [Reference: OracleDB vector store](references/reference-vectors-oracledb.md) - Documentation for the Oracle Database vector provider in Mastra.
|
|
67
69
|
- [Reference: PG vector store](references/reference-vectors-pg.md) - Documentation for the PgVector class in Mastra, which provides vector search using PostgreSQL with pgvector extension.
|
|
68
70
|
- [Reference: Upstash vector store](references/reference-vectors-upstash.md) - Documentation for the UpstashVector class in Mastra, which provides vector search using Upstash Vector.
|
|
@@ -0,0 +1,297 @@
|
|
|
1
|
+
> Discover all available pages from the documentation index: https://mastra.ai/llms.txt
|
|
2
|
+
|
|
3
|
+
# Context engineering
|
|
4
|
+
|
|
5
|
+
A model can only work with the information available in its context window. That might include the current conversation, remembered details, application data, tool results, or relevant passages from a knowledge base.
|
|
6
|
+
|
|
7
|
+
Context engineering is the practice of deciding what information the model should see and when. The goal isn't to provide as much as possible, but to keep the context relevant and current. Too little context leaves the model without information it needs; too much can make important details harder to find, increase cost, and reduce the quality of the response long before the model reaches its context limit.
|
|
8
|
+
|
|
9
|
+
Mastra provides different ways to bring information into context, keep it available over time, retrieve it when needed, and reduce or isolate it as a task grows. This guide explains when to use each mechanism and how they fit together.
|
|
10
|
+
|
|
11
|
+
| Need | Start with | What the model sees |
|
|
12
|
+
| ----------------------------------------- | --------------------------------------------- | -------------------------------------------------------- |
|
|
13
|
+
| Stable identity, rules, or constraints | [Instructions](#instructions) | System context on each model call |
|
|
14
|
+
| Data from a database or API | [Tools](#tools) | Tool definitions followed by selected results |
|
|
15
|
+
| Current customer or application data | [Inline context](#inline-context) | Data interpolated into the user message |
|
|
16
|
+
| A large, stable knowledge base | [RAG](#rag) | Semantically relevant chunks from an index |
|
|
17
|
+
| User- or organization-managed documents | [Filesystems](#filesystems) | Files selected through read or search tools |
|
|
18
|
+
| Recent conversation or durable facts | [Memory](#memory) | History, observations, or retrieved memories |
|
|
19
|
+
| A long-running conversation | [Observational Memory](#observational-memory) | Dense observations plus recent unobserved messages |
|
|
20
|
+
| New events or changing state during a run | [Signals](#signals) | User, reactive, notification, or state messages |
|
|
21
|
+
| Instructions needed only for some tasks | [Dynamic skills](#dynamic-skills) | Skill metadata followed by instructions loaded on demand |
|
|
22
|
+
|
|
23
|
+
## Instructions
|
|
24
|
+
|
|
25
|
+
An agent's [`instructions`](https://mastra.ai/reference/agents/agent) define its stable identity, behavior, and constraints. They're system messages and appear before conversation messages in the model request.
|
|
26
|
+
|
|
27
|
+
```typescript
|
|
28
|
+
import { Agent } from '@mastra/core/agent'
|
|
29
|
+
|
|
30
|
+
export const supportAgent = new Agent({
|
|
31
|
+
id: 'support-agent',
|
|
32
|
+
name: 'Support Agent',
|
|
33
|
+
instructions: `You help customers understand their account.
|
|
34
|
+
Today is ${new Date().toDateString()}.
|
|
35
|
+
Use plain language and don't invent account details.`,
|
|
36
|
+
model: 'openai/gpt-5.6-sol',
|
|
37
|
+
})
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
Keep instructions focused on behavior that applies to most calls. Adding current account data, retrieved documents, or task-specific details makes the base prompt larger and harder to reuse.
|
|
41
|
+
|
|
42
|
+
Instructions can also be resolved at runtime from [`RequestContext`](https://mastra.ai/docs/server/request-context):
|
|
43
|
+
|
|
44
|
+
```typescript
|
|
45
|
+
instructions: ({ requestContext }) => {
|
|
46
|
+
const name = requestContext.get('name')
|
|
47
|
+
|
|
48
|
+
return `You help ${name} understand their account.`
|
|
49
|
+
}
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
Use `RequestContext` when instructions depend on data that changes with each request, such as the current user, tenant, locale, role, or feature flags. Values that don't come from the request, such as the current date, can be interpolated directly as shown in the first example.
|
|
53
|
+
|
|
54
|
+
> **Tip:** If the resolved instructions change often, the model provider may not be able to reuse the same prompt cache prefix. Keep the stable part first, and pass frequently changing background through messages or signals instead.
|
|
55
|
+
>
|
|
56
|
+
> Watch [this short video on prompt caching](https://youtu.be/eBB0dBqfvuQ) to learn how cacheable prompt prefixes reduce latency and cost.
|
|
57
|
+
|
|
58
|
+
## Inline context
|
|
59
|
+
|
|
60
|
+
Most applications pass runtime context by interpolating relevant values into the current message. This works well when your code has already loaded customer or application data:
|
|
61
|
+
|
|
62
|
+
```typescript
|
|
63
|
+
const customer = await db.customer.findById(customerId)
|
|
64
|
+
|
|
65
|
+
await supportAgent.generate(`
|
|
66
|
+
Customer: ${customer.name}
|
|
67
|
+
Plan: ${customer.plan}
|
|
68
|
+
Question: ${question}
|
|
69
|
+
`)
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
Select and label the fields the model needs instead of serializing an entire database record. This keeps the prompt smaller and makes the meaning of each value clear.
|
|
73
|
+
|
|
74
|
+
When memory is enabled, Mastra saves the current user message. Don't interpolate sensitive or temporary data that shouldn't appear in conversation history.
|
|
75
|
+
|
|
76
|
+
For the less common case where background should affect one response without being saved as conversation history, pass a [`context`](https://mastra.ai/reference/agents/generate) message:
|
|
77
|
+
|
|
78
|
+
```typescript
|
|
79
|
+
await supportAgent.generate('Recommend the next action.', {
|
|
80
|
+
context: [{ role: 'user', content: 'The customer has an unresolved billing dispute.' }],
|
|
81
|
+
})
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
The model sees this background for the current execution, but Mastra doesn't save it to memory. Use `context` when persisting the background would pollute the conversation or expose temporary application state on later turns.
|
|
85
|
+
|
|
86
|
+
## Tools
|
|
87
|
+
|
|
88
|
+
[Tools](https://mastra.ai/docs/agents/tools) are the recommended way to fetch current data from a database, API, or service. The model decides when it needs the data and supplies the tool arguments, while your application controls the query and returned fields.
|
|
89
|
+
|
|
90
|
+
```typescript
|
|
91
|
+
import { createTool } from '@mastra/core/tools'
|
|
92
|
+
import { z } from 'zod'
|
|
93
|
+
|
|
94
|
+
export const getCustomer = createTool({
|
|
95
|
+
id: 'get-customer',
|
|
96
|
+
description: 'Gets the current profile and plan for a customer',
|
|
97
|
+
inputSchema: z.object({ customerId: z.string() }),
|
|
98
|
+
execute: async ({ customerId }) => {
|
|
99
|
+
const customer = await db.customer.findById(customerId)
|
|
100
|
+
return { name: customer.name, plan: customer.plan, status: customer.status }
|
|
101
|
+
},
|
|
102
|
+
})
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
Use [`toModelOutput`](https://mastra.ai/docs/agents/tools) when application code needs the full result but the model needs a smaller representation.
|
|
106
|
+
|
|
107
|
+
## RAG
|
|
108
|
+
|
|
109
|
+
[Retrieval-Augmented Generation (RAG)](https://mastra.ai/reference/rag/overview) retrieves semantically relevant chunks from an indexed corpus. It still fits large, stable knowledge bases where users ask open-ended questions that don't map cleanly to structured database queries.
|
|
110
|
+
|
|
111
|
+
```typescript
|
|
112
|
+
import { ModelRouterEmbeddingModel } from '@mastra/core/llm'
|
|
113
|
+
import { createVectorQueryTool } from '@mastra/rag'
|
|
114
|
+
|
|
115
|
+
const knowledgeBase = createVectorQueryTool({
|
|
116
|
+
vectorStoreName: 'knowledgeBase',
|
|
117
|
+
indexName: 'support-docs',
|
|
118
|
+
model: new ModelRouterEmbeddingModel('openai/text-embedding-3-small'),
|
|
119
|
+
})
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
Register the vector store referenced by `vectorStoreName` on the same Mastra instance as the agent. Mastra supports [multiple vector databases](https://mastra.ai/reference/rag/vector-databases). RAG is often exposed through a tool, as in this example. The design choice is whether the agent needs semantic retrieval or can query the source directly.
|
|
123
|
+
|
|
124
|
+
Many applications now start with direct, source-specific tools. Models have become better at selecting them, and a direct query is often simpler and cheaper because it doesn't require a chunking, embedding, and vector-index pipeline. Choose RAG when semantic search over unstructured content is the actual requirement, then constrain the returned context with metadata filters, reranking, and a conservative `topK`.
|
|
125
|
+
|
|
126
|
+
## Filesystems
|
|
127
|
+
|
|
128
|
+
A [filesystem](https://mastra.ai/docs/sandbox/filesystem) gives an agent persistent access to documents and other files. Files can live in a local directory or in providers such as Amazon S3, AgentFS, or Google Drive. The agent receives built-in tools to list, read, and search them.
|
|
129
|
+
|
|
130
|
+
```typescript
|
|
131
|
+
import { LocalFilesystem, Workspace } from '@mastra/core/workspace'
|
|
132
|
+
|
|
133
|
+
export const workspace = new Workspace({
|
|
134
|
+
filesystem: new LocalFilesystem({ basePath: './knowledge-base' }),
|
|
135
|
+
bm25: true,
|
|
136
|
+
autoIndexPaths: ['**/*.md'],
|
|
137
|
+
})
|
|
138
|
+
|
|
139
|
+
// Agents receive tools including read_file, list_files, grep,
|
|
140
|
+
// mastra_workspace_search, and mastra_workspace_index.
|
|
141
|
+
await workspace.init()
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
[Workspace search](https://mastra.ai/docs/sandbox/search) supports BM25 keyword search, vector semantic search, or a hybrid of both. Use a filesystem for a personal assistant that works with a user's files or an organization knowledge base that teammates update in a service such as Google Drive. Search runs against the workspace index, so changed files must be indexed before the agent can retrieve their latest contents.
|
|
145
|
+
|
|
146
|
+
> **Tip:** Filesystem search can also use vectors, so it overlaps with RAG. Choose a filesystem when the source of truth is a set of files that the agent may need to list, read, or update. Choose a standalone RAG pipeline when retrieval is the main requirement and the source content doesn't need to behave like files.
|
|
147
|
+
|
|
148
|
+
## Memory
|
|
149
|
+
|
|
150
|
+
[Memory](https://mastra.ai/docs/memory/overview) gives an agent conversational coherence across turns. It brings recent messages and remembered details into context without requiring the application to resend the full transcript on every turn.
|
|
151
|
+
|
|
152
|
+
Memory requires a storage provider. Each call also identifies a `resource` that owns the memory and a `thread` that identifies the conversation. Reuse both values to continue the same conversation:
|
|
153
|
+
|
|
154
|
+
```typescript
|
|
155
|
+
import { Agent } from '@mastra/core/agent'
|
|
156
|
+
import { Memory } from '@mastra/memory'
|
|
157
|
+
|
|
158
|
+
export const assistant = new Agent({
|
|
159
|
+
id: 'assistant',
|
|
160
|
+
name: 'Assistant',
|
|
161
|
+
model: 'openai/gpt-5.6-sol',
|
|
162
|
+
memory: new Memory({
|
|
163
|
+
options: {
|
|
164
|
+
lastMessages: 20,
|
|
165
|
+
},
|
|
166
|
+
}),
|
|
167
|
+
})
|
|
168
|
+
|
|
169
|
+
await assistant.generate('Help me plan the next project milestone.', {
|
|
170
|
+
memory: {
|
|
171
|
+
resource: 'user-123',
|
|
172
|
+
thread: 'project-456',
|
|
173
|
+
},
|
|
174
|
+
})
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
The example assumes storage is configured on the registered Mastra instance or directly on `Memory`. `lastMessages` controls how many recent messages Mastra loads from the thread. The default is 10.
|
|
178
|
+
|
|
179
|
+
Message history works well for shorter conversations where recent turns contain the context the agent needs. For long-running conversations, Mastra recommends [Observational Memory](https://mastra.ai/docs/memory/observational-memory), which keeps recent conversation available and turns older history into a dense observation log.
|
|
180
|
+
|
|
181
|
+
## Observational Memory
|
|
182
|
+
|
|
183
|
+
Conversation history grows with every user message, response, and tool call. Even before it reaches the model's hard context limit, a long transcript can increase cost and make relevant details harder for the model to find. Compression replaces old, verbose history with a smaller representation.
|
|
184
|
+
|
|
185
|
+
[Observational Memory](https://mastra.ai/docs/memory/observational-memory) handles this continuously. An Observer turns older messages and tool interactions into dense observations, while periodic reflection reorganizes and compresses those observations.
|
|
186
|
+
|
|
187
|
+
You don't need to configure `lastMessages` when Observational Memory is enabled. Observational Memory manages history itself, keeping recent unobserved messages in context and replacing older messages with observations.
|
|
188
|
+
|
|
189
|
+
```typescript
|
|
190
|
+
import { Agent } from '@mastra/core/agent'
|
|
191
|
+
import { Memory } from '@mastra/memory'
|
|
192
|
+
|
|
193
|
+
export const assistant = new Agent({
|
|
194
|
+
id: 'assistant',
|
|
195
|
+
name: 'Assistant',
|
|
196
|
+
model: 'openai/gpt-5.6-sol',
|
|
197
|
+
memory: new Memory({
|
|
198
|
+
options: {
|
|
199
|
+
observationalMemory: true,
|
|
200
|
+
},
|
|
201
|
+
}),
|
|
202
|
+
})
|
|
203
|
+
```
|
|
204
|
+
|
|
205
|
+
After messages are observed, the model receives the observation log, recent messages that haven't been observed, and a continuation reminder. The raw messages remain stored but no longer occupy the active model context.
|
|
206
|
+
|
|
207
|
+
Observations are added in stable chunks, which helps providers reuse the existing prompt prefix. Observational Memory can also activate buffered observations after a prompt cache is likely to expire or before the agent changes providers.
|
|
208
|
+
|
|
209
|
+
## Signals
|
|
210
|
+
|
|
211
|
+
> **Beta:** Signals may change without a major version bump until the API is stable.
|
|
212
|
+
|
|
213
|
+
[Signals](https://mastra.ai/docs/harness/signals) add messages or system-generated context to a memory-backed thread. Delivery depends on the thread's state: a signal can wake an idle agent or enter an active loop. It can also wait for the next turn or persist without waking the agent.
|
|
214
|
+
|
|
215
|
+
State signals require memory and an existing thread. Notification inbox signals require a storage adapter with notification support.
|
|
216
|
+
|
|
217
|
+
| API | Use | Context behavior |
|
|
218
|
+
| ------------------- | ------------------------------------------------------------------ | ----------------------------------------------------------------------- |
|
|
219
|
+
| `sendMessage()` | User input that the active agent should see now | Enters the active loop or wakes an idle thread |
|
|
220
|
+
| `queueMessage()` | User input that should wait for the next turn | Starts after the current run finishes |
|
|
221
|
+
| `sendSignal()` | Background results, policy reminders, or external events | Adds reactive or notification context according to its delivery options |
|
|
222
|
+
| `sendStateSignal()` | Browser state, editor state, task state, or another changing value | Maintains a thread-scoped state lane with snapshots and deltas |
|
|
223
|
+
|
|
224
|
+
Use `sendSignal()` for context produced by the system rather than the user:
|
|
225
|
+
|
|
226
|
+
```typescript
|
|
227
|
+
const result = agent.sendSignal(
|
|
228
|
+
{
|
|
229
|
+
type: 'notification',
|
|
230
|
+
contents: 'CI failed on pull request 123: three tests failed.',
|
|
231
|
+
attributes: { source: 'github', pullRequest: 123 },
|
|
232
|
+
},
|
|
233
|
+
{
|
|
234
|
+
resourceId: 'user-123',
|
|
235
|
+
threadId: 'project-456',
|
|
236
|
+
},
|
|
237
|
+
)
|
|
238
|
+
|
|
239
|
+
await result.accepted
|
|
240
|
+
```
|
|
241
|
+
|
|
242
|
+
A processor can send a reactive signal during `processInputStep()`. This is useful for guidance that depends on the current step or a recent tool result. Set `transient: true` when the signal should reach only the current model call. Re-send it when needed instead of storing repeated reminders in conversation history.
|
|
243
|
+
|
|
244
|
+
State signals represent context that changes over time. Mastra tracks snapshots and deltas for each state lane and can reinsert a fresh snapshot after the previous one leaves the active context window. Use `computeStateSignal()` when a processor owns the state. Working memory, browser context, and task lists can use this lane to stay available even after history or Observational Memory removes older messages.
|
|
245
|
+
|
|
246
|
+
Signals append changing context near the current turn instead of rewriting the agent's base instructions. Transient and state signals can therefore preserve a more stable prompt prefix while keeping current guidance and state visible to the model.
|
|
247
|
+
|
|
248
|
+
## Dynamic skills
|
|
249
|
+
|
|
250
|
+
[Agent skills](https://mastra.ai/docs/skills) let an agent load task-specific instructions only when needed instead of carrying every procedure in its base instructions. Use them for specialized guidance that applies to some requests and keep the default context smaller.
|
|
251
|
+
|
|
252
|
+
## Context control
|
|
253
|
+
|
|
254
|
+
Context control limits what the model sees as a task grows. Compaction and processors reduce context within one agent, while subagent boundaries control what moves between agents.
|
|
255
|
+
|
|
256
|
+
### Compaction
|
|
257
|
+
|
|
258
|
+
If you've used Claude Code, you may have seen compaction happen during a long session. The Mastra team likes to joke, "Friends don't let friends do compaction."
|
|
259
|
+
|
|
260
|
+
Compaction waits until a conversation reaches a token threshold. It then summarizes the transcript and replaces earlier messages. It's a blunt fallback. The compaction turn adds latency, and a single summary has to represent everything that came before. Repeated summaries can flatten chronology or lose details that later become important.
|
|
261
|
+
|
|
262
|
+
Prefer [Observational Memory](#observational-memory) for long-running conversations. It can process history asynchronously in the background while preserving temporal context. Reflection revisits accumulated memories and naturally prunes details that no longer matter. Mastra doesn't provide compaction out of the box, though you could implement it with a custom [processor](https://mastra.ai/docs/agents/processors).
|
|
263
|
+
|
|
264
|
+
### Processors
|
|
265
|
+
|
|
266
|
+
[Processors](https://mastra.ai/docs/agents/processors) control what enters model context and can rewrite content before a model call. Use them when information should remain in stored history or application output but doesn't need to be sent back to the model on every step.
|
|
267
|
+
|
|
268
|
+
For example, `ToolCallFilter` removes old tool arguments and results from the next model request without deleting those messages from storage. The model gets a smaller prompt, while your application can still display or inspect the complete interaction.
|
|
269
|
+
|
|
270
|
+
Use `processInput()` or `processInputStep()` to change the active message list. Those changes may later be saved to memory. Use `processLLMRequest()` when a rewrite should apply only to the current provider call and leave memory untouched.
|
|
271
|
+
|
|
272
|
+
Mastra includes several controls for common sources of context bloat:
|
|
273
|
+
|
|
274
|
+
- [`toModelOutput`](https://mastra.ai/docs/agents/tools): Replace a verbose tool result with a smaller model-facing representation.
|
|
275
|
+
- [`ToolCallFilter`](https://mastra.ai/reference/processors/tool-call-filter): Remove old tool calls and results from model input while retaining them in memory and the UI.
|
|
276
|
+
- [`ToolSearchProcessor`](https://mastra.ai/reference/processors/tool-search-processor): Replace a large tool catalog with search and load tools.
|
|
277
|
+
- [`TokenLimiter`](https://mastra.ai/reference/processors/token-limiter-processor): Prune non-system messages until the prompt fits a token budget.
|
|
278
|
+
|
|
279
|
+
Start with [`toModelOutput`](https://mastra.ai/docs/agents/tools) for verbose tool results and `ToolCallFilter` for old tool interactions. Add `TokenLimiter` as a final budget guard rather than relying on the model's maximum context window.
|
|
280
|
+
|
|
281
|
+
### Subagents
|
|
282
|
+
|
|
283
|
+
A common source of context bloat is passing too much information to and from [subagents](https://mastra.ai/docs/subagents). A subagent gets a separate model context for its delegated task, but the boundary still needs deliberate controls.
|
|
284
|
+
|
|
285
|
+
By default, Mastra forwards the parent's conversation to the subagent. Use `messageFilter` to pass only the messages that specialist needs:
|
|
286
|
+
|
|
287
|
+
```typescript
|
|
288
|
+
await supervisor.generate('Investigate the failed deployment.', {
|
|
289
|
+
delegation: {
|
|
290
|
+
messageFilter: ({ messages }) => messages.slice(-10),
|
|
291
|
+
},
|
|
292
|
+
})
|
|
293
|
+
```
|
|
294
|
+
|
|
295
|
+
In the other direction, Mastra returns the subagent's text to the parent model by default while keeping nested tool calls and metadata available to application code. Leave `includeSubAgentToolResultsInModelContext` disabled unless the parent must reason over those details. Use `onDelegationStart` to refine the child prompt and `onDelegationComplete` to reduce or replace the text returned to the parent.
|
|
296
|
+
|
|
297
|
+
See [Subagents](https://mastra.ai/docs/subagents) for delegation hooks, memory isolation, iteration monitoring, and result controls.
|
|
@@ -207,6 +207,7 @@ Each provider page includes installation instructions, configuration parameters,
|
|
|
207
207
|
- [OracleDB](https://mastra.ai/integrations/databases/oracledb)
|
|
208
208
|
- [PostgreSQL](https://mastra.ai/integrations/databases/postgresql)
|
|
209
209
|
- [Redis](https://mastra.ai/integrations/databases/redis)
|
|
210
|
+
- [Valkey](https://mastra.ai/integrations/databases/valkey)
|
|
210
211
|
- [Upstash](https://mastra.ai/integrations/databases/upstash)
|
|
211
212
|
|
|
212
213
|
## Next steps
|
|
@@ -32,7 +32,7 @@ bun add @mastra/mongodb@latest
|
|
|
32
32
|
|
|
33
33
|
## Usage
|
|
34
34
|
|
|
35
|
-
Ensure you have a [MongoDB Atlas Local (via Docker)](https://www.mongodb.com/docs/atlas/cli/current/atlas-cli-deploy-docker/) or [MongoDB Atlas Cloud](https://www.mongodb.com/docs/atlas/cli/current/atlas-cli-getting-started/) instance with
|
|
35
|
+
Ensure you have a [MongoDB Atlas Local (via Docker)](https://www.mongodb.com/docs/atlas/cli/current/atlas-cli-deploy-docker/) or [MongoDB Atlas Cloud](https://www.mongodb.com/docs/atlas/cli/current/atlas-cli-getting-started/) instance with MongoDB Search enabled. MongoDB 7.0+ is recommended.
|
|
36
36
|
|
|
37
37
|
```typescript
|
|
38
38
|
import { MongoDBStore } from '@mastra/mongodb'
|
|
@@ -146,6 +146,8 @@ PostgreSQL supports observability and can handle low trace volumes. Throughput c
|
|
|
146
146
|
- Setting up table partitioning for efficient data retention
|
|
147
147
|
- Migrating observability to [ClickHouse via composite storage](https://mastra.ai/reference/storage/composite) if you need to scale further
|
|
148
148
|
|
|
149
|
+
`PostgresStoreVNext` uses the `event-sourced` [tracing strategy](https://mastra.ai/docs/observability/integrations/exporters/mastra-storage) instead. It writes one row when a span starts and another when it ends, never updating a row in place, and collapses those rows when a trace is read. Writes stay append-only, and traces appear in Studio while the run is still executing.
|
|
150
|
+
|
|
149
151
|
### Initialization
|
|
150
152
|
|
|
151
153
|
When you pass storage to the Mastra class, `init()` is called automatically before any storage operation:
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
> Discover all available pages from the documentation index: https://mastra.ai/llms.txt
|
|
2
|
+
|
|
3
|
+
# Valkey
|
|
4
|
+
|
|
5
|
+
The Valkey storage implementation provides persistent storage and server-side caching through the [Valkey GLIDE](https://github.com/valkey-io/valkey-glide) client. Use it when your deployment runs Valkey or needs GLIDE features such as native Valkey configuration and authentication.
|
|
6
|
+
|
|
7
|
+
Use [`@mastra/redis`](https://mastra.ai/integrations/databases/redis) for Redis deployments that use the official `redis` client. The packages are tested independently against their respective servers.
|
|
8
|
+
|
|
9
|
+
## Installation
|
|
10
|
+
|
|
11
|
+
**npm**:
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
npm install @mastra/valkey
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
**pnpm**:
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
pnpm add @mastra/valkey
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
**Yarn**:
|
|
24
|
+
|
|
25
|
+
```bash
|
|
26
|
+
yarn add @mastra/valkey
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
**Bun**:
|
|
30
|
+
|
|
31
|
+
```bash
|
|
32
|
+
bun add @mastra/valkey
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
## Usage
|
|
36
|
+
|
|
37
|
+
```typescript
|
|
38
|
+
import { Mastra } from '@mastra/core'
|
|
39
|
+
import { ValkeyStore } from '@mastra/valkey'
|
|
40
|
+
|
|
41
|
+
export const mastra = new Mastra({
|
|
42
|
+
storage: new ValkeyStore({
|
|
43
|
+
id: 'valkey-storage',
|
|
44
|
+
host: 'localhost',
|
|
45
|
+
port: 6379,
|
|
46
|
+
password: process.env.VALKEY_PASSWORD,
|
|
47
|
+
}),
|
|
48
|
+
})
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
You can also provide a native GLIDE configuration:
|
|
52
|
+
|
|
53
|
+
```typescript
|
|
54
|
+
import { ValkeyStore } from '@mastra/valkey'
|
|
55
|
+
|
|
56
|
+
const storage = new ValkeyStore({
|
|
57
|
+
id: 'valkey-storage',
|
|
58
|
+
config: {
|
|
59
|
+
addresses: [{ host: 'localhost', port: 6379 }],
|
|
60
|
+
useTLS: true,
|
|
61
|
+
},
|
|
62
|
+
})
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
For an existing `GlideClient`, connect it before passing it to `ValkeyStore`. The caller remains responsible for closing injected clients.
|
|
66
|
+
|
|
67
|
+
## Constructor parameters
|
|
68
|
+
|
|
69
|
+
**id** (`string`): Unique identifier for the storage instance.
|
|
70
|
+
|
|
71
|
+
**host** (`string`): Valkey host address. Use with the direct connection fields.
|
|
72
|
+
|
|
73
|
+
**port** (`number`): Valkey port. (Default: `6379`)
|
|
74
|
+
|
|
75
|
+
**username** (`string`): Valkey authentication username. (Default: `default`)
|
|
76
|
+
|
|
77
|
+
**password** (`string`): Valkey authentication password.
|
|
78
|
+
|
|
79
|
+
**db** (`number`): Valkey database number. (Default: `0`)
|
|
80
|
+
|
|
81
|
+
**useTLS** (`boolean`): Enables TLS for direct connections.
|
|
82
|
+
|
|
83
|
+
**config** (`GlideClientConfiguration`): Native GLIDE standalone client configuration.
|
|
84
|
+
|
|
85
|
+
**client** (`GlideClient`): Preconfigured GLIDE standalone client.
|
|
86
|
+
|
|
87
|
+
**disableInit** (`boolean`): Disables automatic storage initialization.
|
|
88
|
+
|
|
89
|
+
Provide exactly one connection form: `client`, `config`, or `host` with its optional direct connection fields.
|
|
90
|
+
|
|
91
|
+
## Closing connections
|
|
92
|
+
|
|
93
|
+
Close clients created by `ValkeyStore` during graceful shutdown:
|
|
94
|
+
|
|
95
|
+
```typescript
|
|
96
|
+
await storage.close()
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
`close()` doesn't close an injected `GlideClient`.
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
# MongoDB vector store
|
|
4
4
|
|
|
5
|
-
The `MongoDBVector` class provides vector search using [MongoDB
|
|
5
|
+
The `MongoDBVector` class provides vector search using [MongoDB Vector Search](https://www.mongodb.com/docs/atlas/atlas-vector-search/). It enables efficient similarity search and metadata filtering within your MongoDB collections.
|
|
6
6
|
|
|
7
7
|
## Installation
|
|
8
8
|
|
|
@@ -89,11 +89,11 @@ Creates a new vector index (collection) in MongoDB.
|
|
|
89
89
|
|
|
90
90
|
**metric** (`'cosine' | 'euclidean' | 'dotproduct'`): Distance metric for similarity search (Default: `cosine`)
|
|
91
91
|
|
|
92
|
-
**filterFields** (`string[]`): Metadata field names to declare as filter fields in the
|
|
92
|
+
**filterFields** (`string[]`): Metadata field names to declare as filter fields in the MongoDB vectorSearch index (registered as metadata.\<field>). Queries that filter only on declared fields are pushed directly into $vectorSearch instead of pre-filtering candidate \_ids, avoiding the 16 MB BSON limit on large result sets. Filters that reference an undeclared field, or use an operator $vectorSearch does not support, fall back to the pre-filter automatically.
|
|
93
93
|
|
|
94
94
|
**collectionName** (`string`): Store the vectors on an existing (operational) collection instead of a managed collection named after the index. The collection is never created or dropped by this store when set. Defaults to indexName.
|
|
95
95
|
|
|
96
|
-
**searchIndexName** (`string`): Name for the
|
|
96
|
+
**searchIndexName** (`string`): Name for the MongoDB vectorSearch index created on the collection. Defaults to ${indexName}\_vector\_index.
|
|
97
97
|
|
|
98
98
|
**allowWrites** (`boolean`): Opt-in to write operations (upsert, updateVector, deleteVector, deleteVectors) on a bring-your-own collection. By default a BYO index is read-only: the store never modifies or deletes caller-owned operational documents. Ignored for managed collections, which are always writable. The policy is persisted with the index registration and survives restarts. (Default: `false`)
|
|
99
99
|
|
|
@@ -143,7 +143,7 @@ Searches for similar vectors with optional metadata filtering.
|
|
|
143
143
|
|
|
144
144
|
### `createSearchIndex()`
|
|
145
145
|
|
|
146
|
-
Provisions
|
|
146
|
+
Provisions a MongoDB Search (BM25/full-text) index on the collection backing an index and records it as the text-search index that `textQuery()` and `hybridQuery()` will target.
|
|
147
147
|
|
|
148
148
|
**Managed vs. bring-your-own collections:**
|
|
149
149
|
|
|
@@ -159,7 +159,7 @@ Naming:
|
|
|
159
159
|
|
|
160
160
|
**fields** (`string[]`): Field names to index for full-text search. Omit for dynamic mapping (all string fields).
|
|
161
161
|
|
|
162
|
-
**searchIndexName** (`string`): Name for the
|
|
162
|
+
**searchIndexName** (`string`): Name for the MongoDB Search index. When fields is provided and this is omitted, a distinct default name that is unique per logical index is used, so the field mapping is not shadowed by the auto-created dynamic index and two logical indexes on the same collection do not collide. (Default: ``${collectionName}_search_index (or ${collectionName}_${indexName}_search_fields_index when `fields` is given)``)
|
|
163
163
|
|
|
164
164
|
**waitUntilReady** (`boolean`): When true, block until the provisioned full-text index reports READY before resolving. Defaults to false to avoid surprising latency; call waitForSearchIndexReady() explicitly if you prefer to await separately. (Default: `false`)
|
|
165
165
|
|
|
@@ -174,7 +174,7 @@ The field-mapped index name includes the logical `indexName`, so two logical ind
|
|
|
174
174
|
|
|
175
175
|
### `waitForSearchIndexReady()`
|
|
176
176
|
|
|
177
|
-
Waits for the full-text (BM25) search index of an index to become READY. `waitForIndexReady()` polls only the vectorSearch index; `createSearchIndex()` returns while the
|
|
177
|
+
Waits for the full-text (BM25) search index of an index to become READY. `waitForIndexReady()` polls only the vectorSearch index; `createSearchIndex()` returns while the MongoDB Search full-text index is still building, so an immediate `textQuery()`/`hybridQuery()` can intermittently fail. Call this (or pass `waitUntilReady: true` to `createSearchIndex()`) to block until the resolved text index reports READY.
|
|
178
178
|
|
|
179
179
|
**indexName** (`string`): Logical name of the index whose text index to wait for
|
|
180
180
|
|
|
@@ -191,7 +191,7 @@ await store.waitForSearchIndexReady({ indexName: 'precedents' })
|
|
|
191
191
|
|
|
192
192
|
### `textQuery()`
|
|
193
193
|
|
|
194
|
-
Runs a full-text (BM25) search against
|
|
194
|
+
Runs a full-text (BM25) search against a MongoDB Search index. By default it targets the text-search index recorded for this index (set by `createSearchIndex()`, or the dynamic `${collectionName}_search_index` auto-created by `createIndex()`). Pass `searchIndexName` to target a specific index for this call.
|
|
195
195
|
|
|
196
196
|
Metadata filters here (like `hybridQuery()`) are applied via a `$match` stage. For the vector branch of `hybridQuery()`, filters on fields not declared via `filterFields` at index creation are transparently materialised as candidate `_id`s (the same fallback `query()` uses), so undeclared-field filters don't error.
|
|
197
197
|
|
|
@@ -220,7 +220,7 @@ const results = await store.textQuery({
|
|
|
220
220
|
|
|
221
221
|
### `hybridQuery()`
|
|
222
222
|
|
|
223
|
-
Runs a hybrid search that fuses vector similarity and full-text results using MongoDB's server-side `$rankFusion`. It requires MongoDB >= 8.0 and is generally available from 8.1. On 8.0.x, it may require a MongoDB support case for enablement. It runs where enabled, including Atlas 8.0.x. A full-text search index must exist: it's auto-created for managed indexes, but for a bring-your-own collection you must call `createSearchIndex()` first (opt-in).
|
|
223
|
+
Runs a hybrid search that fuses vector similarity and full-text results using MongoDB's server-side `$rankFusion`. It requires MongoDB >= 8.0 and is generally available from 8.1. On 8.0.x, it may require a MongoDB support case for enablement. It runs where enabled, including MongoDB Atlas 8.0.x. A full-text search index must exist: it's auto-created for managed indexes, but for a bring-your-own collection you must call `createSearchIndex()` first (opt-in).
|
|
224
224
|
|
|
225
225
|
**indexName** (`string`): Name of the Mastra index to search
|
|
226
226
|
|
|
@@ -253,7 +253,7 @@ const results = await store.hybridQuery({
|
|
|
253
253
|
})
|
|
254
254
|
```
|
|
255
255
|
|
|
256
|
-
`hybridQuery()` requires MongoDB >= 8.0 for the `$rankFusion` stage. The stage is generally available from 8.1. On 8.0.x, it may need a MongoDB support case to enable and runs where enabled, such as Atlas 8.0.x. If you're running an older version, or `$rankFusion` isn't enabled on your 8.0.x deployment, use `query()` and `textQuery()` separately and merge the results client-side.
|
|
256
|
+
`hybridQuery()` requires MongoDB >= 8.0 for the `$rankFusion` stage. The stage is generally available from 8.1. On 8.0.x, it may need a MongoDB support case to enable and runs where enabled, such as MongoDB Atlas 8.0.x. If you're running an older version, or `$rankFusion` isn't enabled on your 8.0.x deployment, use `query()` and `textQuery()` separately and merge the results client-side.
|
|
257
257
|
|
|
258
258
|
### `describeIndex()`
|
|
259
259
|
|
|
@@ -276,7 +276,7 @@ interface IndexStats {
|
|
|
276
276
|
Deletes a vector index. Behavior depends on how the index was created:
|
|
277
277
|
|
|
278
278
|
- **Managed index** (created without `collectionName`): drops the entire collection and all its data.
|
|
279
|
-
- **Bring-your-own index** (created with `collectionName`): drops the
|
|
279
|
+
- **Bring-your-own index** (created with `collectionName`): drops the MongoDB vectorSearch index. If `createSearchIndex()` provisioned a companion full-text search index, it drops that index too. The caller's operational collection and its documents are preserved. This store never drops a collection it didn't create.
|
|
280
280
|
|
|
281
281
|
The BYO classification is recorded durably when the index is created, so it's applied correctly even by a different process (e.g. an index created by a setup job and later deleted by a long-lived service). Always pass the **logical index name** (the `indexName` used at `createIndex`), not the physical collection name.
|
|
282
282
|
|
|
@@ -429,7 +429,7 @@ await store.createSearchIndex({ indexName: 'precedents', fields: ['note'] })
|
|
|
429
429
|
|
|
430
430
|
Embeddings are numeric vectors used by memory's `semanticRecall` to retrieve related messages by meaning (not keywords).
|
|
431
431
|
|
|
432
|
-
> **Note:** MongoDB
|
|
432
|
+
> **Note:** MongoDB Vector Search is recommended for production use. For self-hosted deployments, Vector Search is available with [local Atlas deployments via the Atlas CLI](https://www.mongodb.com/docs/atlas/cli/current/atlas-cli-deploy-local/).
|
|
433
433
|
|
|
434
434
|
This setup uses FastEmbed, a local embedding model, to generate vector embeddings. To use this, install `@mastra/fastembed`:
|
|
435
435
|
|
|
@@ -173,6 +173,8 @@ interface PGIndexStats {
|
|
|
173
173
|
}
|
|
174
174
|
```
|
|
175
175
|
|
|
176
|
+
`count` is an exact `SELECT COUNT(*)`, which scans the whole table, so avoid calling `describeIndex()` on a hot path for a large index. Reads and writes never pay for it: they only use the index metadata, which comes from the Postgres catalog.
|
|
177
|
+
|
|
176
178
|
### `deleteIndex()`
|
|
177
179
|
|
|
178
180
|
**indexName** (`string`): Name of the index to delete
|