@mastra/memory 1.22.2 → 1.23.0-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/CHANGELOG.md +51 -0
- package/dist/{chunk-3JX4RMDN.cjs → chunk-5HE6DK63.cjs} +6504 -6350
- package/dist/chunk-5HE6DK63.cjs.map +1 -0
- package/dist/{chunk-TB6SV7QK.js → chunk-HF2E2T6Z.js} +6504 -6352
- package/dist/chunk-HF2E2T6Z.js.map +1 -0
- package/dist/docs/SKILL.md +5 -3
- package/dist/docs/assets/SOURCE_MAP.json +53 -45
- package/dist/docs/references/docs-agents-agent-approval.md +2 -2
- package/dist/docs/references/docs-agents-networks.md +1 -1
- package/dist/docs/references/docs-long-running-agents-background-tasks.md +2 -2
- package/dist/docs/references/docs-long-running-agents-goals.md +4 -4
- package/dist/docs/references/docs-memory-memory-processors.md +67 -0
- package/dist/docs/references/docs-memory-message-history.md +56 -2
- package/dist/docs/references/docs-memory-observational-memory.md +3 -3
- package/dist/docs/references/docs-memory-overview.md +3 -3
- package/dist/docs/references/docs-memory-semantic-recall.md +35 -1
- package/dist/docs/references/docs-storage-overview.md +214 -0
- package/dist/docs/references/reference-memory-observational-memory.md +1 -1
- package/dist/docs/references/reference-memory-summarizeConversation.md +99 -0
- package/dist/docs/references/reference-memory-summarizeThread.md +93 -0
- package/dist/docs/references/reference-storage-dynamodb.md +1 -1
- package/dist/docs/references/reference-storage-mongodb.md +46 -46
- package/dist/docs/references/reference-vectors-mongodb.md +93 -4
- package/dist/index.cjs +21 -13
- package/dist/index.d.ts +48 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/{observational-memory-TEIGXE56.cjs → observational-memory-LEIZSPQ5.cjs} +32 -28
- package/dist/{observational-memory-TEIGXE56.cjs.map → observational-memory-LEIZSPQ5.cjs.map} +1 -1
- package/dist/{observational-memory-NNGAOKRQ.js → observational-memory-XKEEBG2S.js} +3 -3
- package/dist/{observational-memory-NNGAOKRQ.js.map → observational-memory-XKEEBG2S.js.map} +1 -1
- package/dist/processors/index.cjs +30 -26
- package/dist/processors/index.js +1 -1
- package/dist/processors/observational-memory/index.d.ts +2 -0
- package/dist/processors/observational-memory/index.d.ts.map +1 -1
- package/dist/processors/observational-memory/summarize.d.ts +92 -0
- package/dist/processors/observational-memory/summarize.d.ts.map +1 -0
- package/package.json +5 -5
- package/dist/chunk-3JX4RMDN.cjs.map +0 -1
- package/dist/chunk-TB6SV7QK.js.map +0 -1
- package/dist/docs/references/docs-memory-storage.md +0 -267
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
> Discover all available pages from the documentation index: https://mastra.ai/llms.txt
|
|
2
|
+
|
|
3
|
+
# Storage overview
|
|
4
|
+
|
|
5
|
+
Storage is the persistence layer for the Mastra runtime. It keeps memory, workflow state, observability data, eval results, schedules, and long-running agent state available after a process restarts.
|
|
6
|
+
|
|
7
|
+
Storage powers:
|
|
8
|
+
|
|
9
|
+
- [Memory](https://mastra.ai/docs/memory/overview): Message history, threads, resources, and working memory.
|
|
10
|
+
- [Workflows](https://mastra.ai/docs/workflows/overview): Durable snapshots for suspended and resumed workflow runs.
|
|
11
|
+
- [Observability](https://mastra.ai/docs/observability/overview): Traces, spans, metrics, logs, and feedback.
|
|
12
|
+
- [Evals](https://mastra.ai/docs/evals/overview): Scores, datasets, experiments, and evaluation results.
|
|
13
|
+
- [Long-running agents](https://mastra.ai/docs/long-running-agents/durable-agents): Background tasks, schedules, goals, and thread state.
|
|
14
|
+
|
|
15
|
+
## When to configure storage
|
|
16
|
+
|
|
17
|
+
Configure a persistent storage adapter when state must survive restarts, be shared across processes, or be visible in Studio across sessions. The default in-memory store is useful for tests and short local experiments, but it loses data when the process exits.
|
|
18
|
+
|
|
19
|
+
Use storage when your application needs any of these behaviors:
|
|
20
|
+
|
|
21
|
+
- Agents remember past messages or user facts.
|
|
22
|
+
- Workflows suspend and resume after a restart.
|
|
23
|
+
- Traces, metrics, logs, scores, or feedback stay available for analysis.
|
|
24
|
+
- Schedules and background tasks continue across deployments.
|
|
25
|
+
- Multiple runtime processes read and write the same state.
|
|
26
|
+
|
|
27
|
+
## How storage works
|
|
28
|
+
|
|
29
|
+
Mastra storage is organized into **domains**. A domain owns one type of runtime data, and a storage adapter implements one or more domains.
|
|
30
|
+
|
|
31
|
+
| Domain | What it stores |
|
|
32
|
+
| ----------------- | --------------------------------------------------------------------------- |
|
|
33
|
+
| `memory` | Threads, messages, resources, working memory, and other agent memory state. |
|
|
34
|
+
| `workflows` | Workflow snapshots used to suspend and resume runs. |
|
|
35
|
+
| `observability` | Traces, spans, metrics, logs, and feedback. |
|
|
36
|
+
| `scores` | Eval score records. |
|
|
37
|
+
| `datasets` | Dataset records and dataset items used by evals and experiments. |
|
|
38
|
+
| `experiments` | Experiment runs and per-item experiment results. |
|
|
39
|
+
| `backgroundTasks` | Background task records and execution state. |
|
|
40
|
+
| `schedules` | Schedule definitions and trigger history. |
|
|
41
|
+
| `threadState` | Durable task, goal, and thread state. |
|
|
42
|
+
|
|
43
|
+
Adapter support varies by domain. For the full domain list and built-in schemas, see the [storage overview reference](https://mastra.ai/reference/storage/overview).
|
|
44
|
+
|
|
45
|
+
## Choose a backend by data shape
|
|
46
|
+
|
|
47
|
+
Different domains write and query different kinds of data. Pick a backend based on the domain's access pattern:
|
|
48
|
+
|
|
49
|
+
- `memory`: Reads and writes rows during every remembered agent call. Use a transactional database such as libSQL, PostgreSQL, or MongoDB.
|
|
50
|
+
- `observability`: Writes high-volume telemetry and often queries aggregations. Use a dedicated observability store or an online analytical processing (OLAP) backend such as ClickHouse or DuckDB.
|
|
51
|
+
- `workflows`: Stores durable snapshots that must be available when a run resumes. Use a reliable persistent database.
|
|
52
|
+
- `scores`, `datasets`, and `experiments`: Store lower-frequency evaluation data that's often read later for analysis.
|
|
53
|
+
- `schedules`: Stores schedule definitions and fire history. Use an adapter that implements the schedules domain.
|
|
54
|
+
|
|
55
|
+
When domains have different operational needs, use [composite storage](#composite-storage) to route each domain to the right backend.
|
|
56
|
+
|
|
57
|
+
## Get started locally
|
|
58
|
+
|
|
59
|
+
For local development, use libSQL with a file-backed database. It doesn't require a separate database server and persists state between restarts.
|
|
60
|
+
|
|
61
|
+
```typescript
|
|
62
|
+
import { Mastra } from '@mastra/core'
|
|
63
|
+
import { LibSQLStore } from '@mastra/libsql'
|
|
64
|
+
|
|
65
|
+
export const mastra = new Mastra({
|
|
66
|
+
storage: new LibSQLStore({
|
|
67
|
+
id: 'mastra-storage',
|
|
68
|
+
url: 'file:./mastra.db',
|
|
69
|
+
}),
|
|
70
|
+
})
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
> **Sharing the database with Studio:** When running `mastra dev` alongside your application, use an absolute path so both processes access the same database:
|
|
74
|
+
>
|
|
75
|
+
> ```typescript
|
|
76
|
+
> url: 'file:/absolute/path/to/your/project/mastra.db'
|
|
77
|
+
> ```
|
|
78
|
+
>
|
|
79
|
+
> Relative paths like `file:./mastra.db` resolve based on each process's working directory, which may differ.
|
|
80
|
+
|
|
81
|
+
Mastra initializes the required storage structures on first use.
|
|
82
|
+
|
|
83
|
+
## Configure for production
|
|
84
|
+
|
|
85
|
+
For production, use a persistent managed database. PostgreSQL is a good default for most teams because it works well for transactional runtime state and is widely available as a managed service.
|
|
86
|
+
|
|
87
|
+
Production guidance:
|
|
88
|
+
|
|
89
|
+
- Use a managed database with backups, monitoring, and connection pooling.
|
|
90
|
+
- Keep local file databases such as `file:./mastra.db` out of multi-process production deployments.
|
|
91
|
+
- Route high-volume domains, especially `observability`, to a dedicated backend with [composite storage](#composite-storage).
|
|
92
|
+
- Configure [retention](https://mastra.ai/reference/storage/retention) policies on the storage adapter or composite store, then call `storage.prune()` from a scheduler or maintenance job.
|
|
93
|
+
- Choose providers based on the domains your application uses. For example, schedules require an adapter that implements the `schedules` domain.
|
|
94
|
+
|
|
95
|
+
## Configuration scope
|
|
96
|
+
|
|
97
|
+
Storage can be configured at the Mastra instance level or at the agent level.
|
|
98
|
+
|
|
99
|
+
### Instance-level storage
|
|
100
|
+
|
|
101
|
+
Instance-level storage is shared by agents, workflows, observability, evals, schedules, and other runtime features registered on the same Mastra instance.
|
|
102
|
+
|
|
103
|
+
**PostgreSQL**:
|
|
104
|
+
|
|
105
|
+
```typescript
|
|
106
|
+
import { Mastra } from '@mastra/core'
|
|
107
|
+
import { PostgresStore } from '@mastra/pg'
|
|
108
|
+
|
|
109
|
+
export const mastra = new Mastra({
|
|
110
|
+
storage: new PostgresStore({
|
|
111
|
+
id: 'mastra-storage',
|
|
112
|
+
connectionString: process.env.DATABASE_URL,
|
|
113
|
+
}),
|
|
114
|
+
})
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
**MongoDB**:
|
|
118
|
+
|
|
119
|
+
```typescript
|
|
120
|
+
import { Mastra } from '@mastra/core'
|
|
121
|
+
import { MongoDBStore } from '@mastra/mongodb'
|
|
122
|
+
|
|
123
|
+
export const mastra = new Mastra({
|
|
124
|
+
storage: new MongoDBStore({
|
|
125
|
+
id: 'mastra-storage',
|
|
126
|
+
uri: process.env.MONGODB_URI,
|
|
127
|
+
dbName: process.env.MONGODB_DB_NAME,
|
|
128
|
+
}),
|
|
129
|
+
})
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
Use instance-level storage when most runtime domains can share the same database.
|
|
133
|
+
|
|
134
|
+
### Agent-level storage
|
|
135
|
+
|
|
136
|
+
Agent-level storage is configured on a `Memory` instance. It overrides instance-level storage for that agent's memory data only.
|
|
137
|
+
|
|
138
|
+
```typescript
|
|
139
|
+
import { Agent } from '@mastra/core/agent'
|
|
140
|
+
import { Memory } from '@mastra/memory'
|
|
141
|
+
import { PostgresStore } from '@mastra/pg'
|
|
142
|
+
|
|
143
|
+
export const supportAgent = new Agent({
|
|
144
|
+
id: 'support-agent',
|
|
145
|
+
name: 'Support agent',
|
|
146
|
+
instructions: 'Answer customer support questions.',
|
|
147
|
+
model: 'openai/gpt-5.5',
|
|
148
|
+
memory: new Memory({
|
|
149
|
+
storage: new PostgresStore({
|
|
150
|
+
id: 'support-agent-storage',
|
|
151
|
+
connectionString: process.env.SUPPORT_AGENT_DATABASE_URL,
|
|
152
|
+
}),
|
|
153
|
+
}),
|
|
154
|
+
})
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
Use agent-level storage when an agent needs an isolated memory boundary or a different memory backend.
|
|
158
|
+
|
|
159
|
+
## Composite storage
|
|
160
|
+
|
|
161
|
+
[`MastraCompositeStore`](https://mastra.ai/reference/storage/composite) routes domains to different backends. Use it when one database isn't the right fit for every domain.
|
|
162
|
+
|
|
163
|
+
The following example uses libSQL as the default store and routes workflow state to PostgreSQL:
|
|
164
|
+
|
|
165
|
+
```typescript
|
|
166
|
+
import { Mastra } from '@mastra/core'
|
|
167
|
+
import { MastraCompositeStore } from '@mastra/core/storage'
|
|
168
|
+
import { LibSQLStore } from '@mastra/libsql'
|
|
169
|
+
import { WorkflowsPG } from '@mastra/pg'
|
|
170
|
+
|
|
171
|
+
export const mastra = new Mastra({
|
|
172
|
+
storage: new MastraCompositeStore({
|
|
173
|
+
id: 'composite-storage',
|
|
174
|
+
default: new LibSQLStore({
|
|
175
|
+
id: 'default-storage',
|
|
176
|
+
url: 'file:./mastra.db',
|
|
177
|
+
}),
|
|
178
|
+
domains: {
|
|
179
|
+
workflows: new WorkflowsPG({
|
|
180
|
+
connectionString: process.env.DATABASE_URL,
|
|
181
|
+
}),
|
|
182
|
+
},
|
|
183
|
+
}),
|
|
184
|
+
})
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
You can also route `observability` to a dedicated analytics backend. See [observability storage](https://mastra.ai/docs/observability/storage) for an observability-specific example.
|
|
188
|
+
|
|
189
|
+
## Supported providers
|
|
190
|
+
|
|
191
|
+
Each provider page includes installation instructions, configuration parameters, and usage examples:
|
|
192
|
+
|
|
193
|
+
- [libSQL](https://mastra.ai/reference/storage/libsql)
|
|
194
|
+
- [PostgreSQL](https://mastra.ai/reference/storage/postgresql)
|
|
195
|
+
- [MongoDB](https://mastra.ai/reference/storage/mongodb)
|
|
196
|
+
- [Upstash](https://mastra.ai/reference/storage/upstash)
|
|
197
|
+
- [Redis](https://mastra.ai/reference/storage/redis)
|
|
198
|
+
- [Cloudflare D1](https://mastra.ai/reference/storage/cloudflare-d1)
|
|
199
|
+
- [Cloudflare KV & Durable Objects](https://mastra.ai/reference/storage/cloudflare)
|
|
200
|
+
- [Convex](https://mastra.ai/reference/storage/convex)
|
|
201
|
+
- [DynamoDB](https://mastra.ai/reference/storage/dynamodb)
|
|
202
|
+
- [LanceDB](https://mastra.ai/reference/storage/lance)
|
|
203
|
+
- [Microsoft SQL Server](https://mastra.ai/reference/storage/mssql)
|
|
204
|
+
- [Google Cloud Spanner](https://mastra.ai/reference/storage/spanner)
|
|
205
|
+
|
|
206
|
+
> **Tip:** libSQL is the fastest path for local development because it doesn't require running a separate database server.
|
|
207
|
+
|
|
208
|
+
## Next steps
|
|
209
|
+
|
|
210
|
+
- [Composite storage](https://mastra.ai/reference/storage/composite)
|
|
211
|
+
- [Storage retention](https://mastra.ai/reference/storage/retention)
|
|
212
|
+
- [Storage schemas](https://mastra.ai/reference/storage/overview)
|
|
213
|
+
- [Memory](https://mastra.ai/docs/memory/overview)
|
|
214
|
+
- [Observability storage](https://mastra.ai/docs/observability/storage)
|
|
@@ -171,7 +171,7 @@ const memory = new Memory({
|
|
|
171
171
|
- Dynamic extractor functions receive runtime context, including `source`, `threadId`, `resourceId`, `mainAgent`, `memory`, and `requestContext` when available.
|
|
172
172
|
- `WorkingMemoryExtractor` uses the normal extractor pipeline to update working memory through the active `Memory` instance. It uses structured extraction when working memory has a JSON schema and skips OM metadata persistence, so the working memory payload isn't duplicated under OM extracted metadata.
|
|
173
173
|
- `observationalMemory.observation.manageWorkingMemory` adds `WorkingMemoryExtractor`, defaults `workingMemory.agentManaged` to `false`, and defaults `workingMemory.useStateSignals` to `true` when working memory is enabled.
|
|
174
|
-
- Extraction failures are reported in OM marker data and
|
|
174
|
+
- Extraction failures are reported in OM marker data and don't discard other successful extracted values.
|
|
175
175
|
|
|
176
176
|
## Examples
|
|
177
177
|
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
> Discover all available pages from the documentation index: https://mastra.ai/llms.txt
|
|
2
|
+
|
|
3
|
+
# summarizeConversation()
|
|
4
|
+
|
|
5
|
+
The standalone `summarizeConversation()` function summarizes a conversation in one shot. It distills the messages you pass in with the same Observer plumbing that powers [Observational Memory](https://mastra.ai/reference/memory/observational-memory) — without Observational Memory attached to an agent, and without reading from or writing to storage.
|
|
6
|
+
|
|
7
|
+
Nothing is written back to memory. The summary and extracted values are returned to you (and to each extractor's `onExtracted` hook), so you decide where they go — for example your own database.
|
|
8
|
+
|
|
9
|
+
Use this when you already have the messages in hand and want explicit control over what gets summarized. To summarize a stored thread by ID instead, use [`Memory.summarizeThread()`](https://mastra.ai/reference/memory/summarizeThread), which loads the messages for you.
|
|
10
|
+
|
|
11
|
+
## Usage example
|
|
12
|
+
|
|
13
|
+
```typescript
|
|
14
|
+
import { summarizeConversation } from '@mastra/memory'
|
|
15
|
+
|
|
16
|
+
const result = await summarizeConversation({
|
|
17
|
+
model: 'openai/gpt-5-mini',
|
|
18
|
+
messages,
|
|
19
|
+
instructions: 'Summarize this voicemail call for the business owner.',
|
|
20
|
+
})
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## Parameters
|
|
24
|
+
|
|
25
|
+
**model** (`string | LanguageModel | DynamicModel`): Model that runs the summarization, such as a router string like '\_\_GATEWAY\_OPENAI\_MODEL\_MINI\_\_'.
|
|
26
|
+
|
|
27
|
+
**messages** (`MastraDBMessage[]`): The conversation to summarize. When empty, the function returns an empty result without calling the model.
|
|
28
|
+
|
|
29
|
+
**instructions** (`string`): Extra guidance appended to the summarizer's system prompt, such as what to focus on or who the summary is for.
|
|
30
|
+
|
|
31
|
+
**extract** (`Extractor[]`): Extractors to run over the conversation. Extractors with a Zod schema run as a follow-up structured output call; schema-less extractors are extracted inline. Each extractor's onExtracted hook fires with the extracted value.
|
|
32
|
+
|
|
33
|
+
**threadId** (`string`): Thread the conversation belongs to. Defaults to the first threadId found on messages. Passed to extractor contexts.
|
|
34
|
+
|
|
35
|
+
**resourceId** (`string`): Resource (user) the conversation belongs to. Defaults to the first resourceId found on messages. Passed to extractor contexts.
|
|
36
|
+
|
|
37
|
+
**memory** (`Memory`): Memory instance forwarded to extractor contexts. Set automatically when called through Memory.summarizeThread().
|
|
38
|
+
|
|
39
|
+
**mastra** (`Mastra`): Mastra instance used to resolve custom gateway models. Set automatically when called through Memory.summarizeThread().
|
|
40
|
+
|
|
41
|
+
**requestContext** (`RequestContext`): Request context forwarded to the summarizer model and extractor hooks.
|
|
42
|
+
|
|
43
|
+
**abortSignal** (`AbortSignal`): Signal to cancel the summarization call.
|
|
44
|
+
|
|
45
|
+
## Returns
|
|
46
|
+
|
|
47
|
+
**summary** (`string`): The distilled observations produced from the conversation, in dense bullet form.
|
|
48
|
+
|
|
49
|
+
**extracted** (`Record<string, unknown>`): Values produced by extract extractors, keyed by extractor slug.
|
|
50
|
+
|
|
51
|
+
**extractionFailures** (`{ slug: string; error: string }[]`): Extractors that failed to produce a valid value, with the reason. Only present when at least one extractor failed.
|
|
52
|
+
|
|
53
|
+
**usage** (`{ inputTokens?: number; outputTokens?: number; totalTokens?: number }`): Token usage of the summarization call.
|
|
54
|
+
|
|
55
|
+
## Extended usage example
|
|
56
|
+
|
|
57
|
+
The following example summarizes an in-flight voice session from messages the application already holds, and extracts a structured record:
|
|
58
|
+
|
|
59
|
+
```typescript
|
|
60
|
+
import { summarizeConversation, Extractor } from '@mastra/memory'
|
|
61
|
+
import type { MastraDBMessage } from '@mastra/core/agent'
|
|
62
|
+
import { z } from 'zod'
|
|
63
|
+
import { sessionRecords } from './db'
|
|
64
|
+
|
|
65
|
+
const sessionSummary = new Extractor({
|
|
66
|
+
name: 'session-summary',
|
|
67
|
+
instructions: 'Return a concise summary of the session.',
|
|
68
|
+
schema: z.object({
|
|
69
|
+
summary: z.string(),
|
|
70
|
+
sentiment: z.enum(['positive', 'neutral', 'negative']),
|
|
71
|
+
followUps: z.array(z.string()),
|
|
72
|
+
}),
|
|
73
|
+
metadataKeyPath: false, // don't persist into memory metadata — the hook owns storage
|
|
74
|
+
onExtracted: async ({ current, threadId }) => {
|
|
75
|
+
await sessionRecords.upsert({ sessionId: threadId, record: current })
|
|
76
|
+
},
|
|
77
|
+
})
|
|
78
|
+
|
|
79
|
+
export async function onSessionEnd(messages: MastraDBMessage[]) {
|
|
80
|
+
const { summary, extracted, extractionFailures } = await summarizeConversation({
|
|
81
|
+
model: 'openai/gpt-5-mini',
|
|
82
|
+
messages,
|
|
83
|
+
instructions: 'Summarize this support session for the account manager.',
|
|
84
|
+
extract: [sessionSummary],
|
|
85
|
+
})
|
|
86
|
+
|
|
87
|
+
if (extractionFailures?.length) {
|
|
88
|
+
console.warn('Some extractors failed', extractionFailures)
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
return { summary, extracted }
|
|
92
|
+
}
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
### Related
|
|
96
|
+
|
|
97
|
+
- [.summarizeThread()](https://mastra.ai/reference/memory/summarizeThread)
|
|
98
|
+
- [Memory Class Reference](https://mastra.ai/reference/memory/memory-class)
|
|
99
|
+
- [Observational Memory](https://mastra.ai/reference/memory/observational-memory)
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
> Discover all available pages from the documentation index: https://mastra.ai/llms.txt
|
|
2
|
+
|
|
3
|
+
# Memory.summarizeThread()
|
|
4
|
+
|
|
5
|
+
The `.summarizeThread()` method summarizes a thread's conversation in one shot. It loads the thread's messages from storage and distills them with the same Observer plumbing that powers [Observational Memory](https://mastra.ai/reference/memory/observational-memory) — as a standalone call, without Observational Memory attached to an agent.
|
|
6
|
+
|
|
7
|
+
Messages load page-by-page starting from the newest, bounded by `lastMessages` and `maxInputTokens`, so summarizing a long thread doesn't read its entire history from storage.
|
|
8
|
+
|
|
9
|
+
Nothing is written back to memory. The summary and extracted values are returned to you (and to each extractor's `onExtracted` hook), so you decide where they go — for example your own database.
|
|
10
|
+
|
|
11
|
+
Use this when a session ends and you want a summary or structured extraction of the whole conversation, such as a voice call at hang-up. To summarize messages you already have in hand (without loading them from a thread), use the standalone [`summarizeConversation()`](https://mastra.ai/reference/memory/summarizeConversation) function instead — it takes the same options with `messages` in place of `threadId`.
|
|
12
|
+
|
|
13
|
+
## Usage example
|
|
14
|
+
|
|
15
|
+
```typescript
|
|
16
|
+
const result = await memory.summarizeThread({
|
|
17
|
+
model: 'openai/gpt-5-mini',
|
|
18
|
+
threadId: 'thread-123',
|
|
19
|
+
instructions: 'Summarize this voicemail call for the business owner.',
|
|
20
|
+
})
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## Parameters
|
|
24
|
+
|
|
25
|
+
**threadId** (`string`): The unique identifier of the thread to summarize.
|
|
26
|
+
|
|
27
|
+
**model** (`string | LanguageModel | DynamicModel`): Model that runs the summarization, such as a router string like '\_\_GATEWAY\_OPENAI\_MODEL\_MINI\_\_'.
|
|
28
|
+
|
|
29
|
+
**resourceId** (`string`): ID of the resource that owns the thread. If provided, validates thread ownership. Also passed to extractor contexts.
|
|
30
|
+
|
|
31
|
+
**lastMessages** (`number`): Only summarize the last N messages of the thread. By default the whole thread is loaded, bounded by maxInputTokens.
|
|
32
|
+
|
|
33
|
+
**maxInputTokens** (`number`): Stop loading older messages once the collected messages exceed this estimated token count. The newest message is always included. (Default: `1000000`)
|
|
34
|
+
|
|
35
|
+
**instructions** (`string`): Extra guidance appended to the summarizer's system prompt, such as what to focus on or who the summary is for.
|
|
36
|
+
|
|
37
|
+
**extract** (`Extractor[]`): Extractors to run over the conversation. Extractors with a Zod schema run as a follow-up structured output call; schema-less extractors are extracted inline. Each extractor's onExtracted hook fires with the extracted value.
|
|
38
|
+
|
|
39
|
+
**requestContext** (`RequestContext`): Request context forwarded to the summarizer model and extractor hooks.
|
|
40
|
+
|
|
41
|
+
**abortSignal** (`AbortSignal`): Signal to cancel the summarization call.
|
|
42
|
+
|
|
43
|
+
## Returns
|
|
44
|
+
|
|
45
|
+
**summary** (`string`): The distilled observations produced from the conversation, in dense bullet form.
|
|
46
|
+
|
|
47
|
+
**extracted** (`Record<string, unknown>`): Values produced by extract extractors, keyed by extractor slug.
|
|
48
|
+
|
|
49
|
+
**extractionFailures** (`{ slug: string; error: string }[]`): Extractors that failed to produce a valid value, with the reason. Only present when at least one extractor failed.
|
|
50
|
+
|
|
51
|
+
**usage** (`{ inputTokens?: number; outputTokens?: number; totalTokens?: number }`): Token usage of the summarization call.
|
|
52
|
+
|
|
53
|
+
## Extended usage example
|
|
54
|
+
|
|
55
|
+
The following example summarizes a finished voice call and stores a structured record in the application's own database:
|
|
56
|
+
|
|
57
|
+
```typescript
|
|
58
|
+
import { Extractor } from '@mastra/memory'
|
|
59
|
+
import { z } from 'zod'
|
|
60
|
+
import { memory } from './mastra/memory'
|
|
61
|
+
import { callRecords } from './db'
|
|
62
|
+
|
|
63
|
+
const callSummary = new Extractor({
|
|
64
|
+
name: 'call-summary',
|
|
65
|
+
instructions: 'Return a concise summary of the call.',
|
|
66
|
+
schema: z.object({
|
|
67
|
+
summary: z.string(),
|
|
68
|
+
sentiment: z.enum(['positive', 'neutral', 'negative']),
|
|
69
|
+
requestedServices: z.array(z.string()),
|
|
70
|
+
}),
|
|
71
|
+
metadataKeyPath: false, // don't persist into memory metadata — the hook owns storage
|
|
72
|
+
onExtracted: async ({ current, threadId, resourceId }) => {
|
|
73
|
+
await callRecords.upsert({ callId: threadId, callerId: resourceId, record: current })
|
|
74
|
+
},
|
|
75
|
+
})
|
|
76
|
+
|
|
77
|
+
export async function onCallEnd(threadId: string, resourceId: string) {
|
|
78
|
+
await memory.summarizeThread({
|
|
79
|
+
model: 'openai/gpt-5-mini',
|
|
80
|
+
threadId,
|
|
81
|
+
resourceId,
|
|
82
|
+
instructions: 'Summarize this voicemail call for the business owner.',
|
|
83
|
+
extract: [callSummary],
|
|
84
|
+
})
|
|
85
|
+
}
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
### Related
|
|
89
|
+
|
|
90
|
+
- [summarizeConversation()](https://mastra.ai/reference/memory/summarizeConversation)
|
|
91
|
+
- [Memory Class Reference](https://mastra.ai/reference/memory/memory-class)
|
|
92
|
+
- [Observational Memory](https://mastra.ai/reference/memory/observational-memory)
|
|
93
|
+
- [.recall()](https://mastra.ai/reference/memory/recall)
|
|
@@ -6,7 +6,7 @@ The DynamoDB storage implementation provides a scalable and performant NoSQL dat
|
|
|
6
6
|
|
|
7
7
|
> **Observability Not Supported:** DynamoDB storage **doesn't support the observability domain**. Traces from the `MastraStorageExporter` can't be persisted to DynamoDB, and [Studio's](https://mastra.ai/docs/studio/overview) observability features won't work with DynamoDB as your only storage provider. To enable observability, use [composite storage](https://mastra.ai/reference/storage/composite) to route observability data to a supported provider like ClickHouse.
|
|
8
8
|
|
|
9
|
-
> **Item Size Limit:** DynamoDB enforces a **400 KB maximum item size**. This limit can be exceeded when storing messages with base64-encoded attachments such as images. See [Handling large attachments](https://mastra.ai/docs/memory/
|
|
9
|
+
> **Item Size Limit:** DynamoDB enforces a **400 KB maximum item size**. This limit can be exceeded when storing messages with base64-encoded attachments such as images. See [Handling large attachments](https://mastra.ai/docs/memory/memory-processors) for workarounds including uploading attachments to external storage.
|
|
10
10
|
|
|
11
11
|
## Features
|
|
12
12
|
|
|
@@ -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) or MongoDB Atlas Cloud instance with Atlas Search enabled. MongoDB 7.0+ is recommended.
|
|
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 Atlas Search enabled. MongoDB 7.0+ is recommended.
|
|
36
36
|
|
|
37
37
|
```typescript
|
|
38
38
|
import { MongoDBStore } from '@mastra/mongodb'
|
|
@@ -40,7 +40,7 @@ import { MongoDBStore } from '@mastra/mongodb'
|
|
|
40
40
|
const storage = new MongoDBStore({
|
|
41
41
|
id: 'mongodb-storage',
|
|
42
42
|
uri: process.env.MONGODB_URI,
|
|
43
|
-
dbName: process.env.
|
|
43
|
+
dbName: process.env.MONGODB_DB_NAME,
|
|
44
44
|
})
|
|
45
45
|
```
|
|
46
46
|
|
|
@@ -54,7 +54,15 @@ const storage = new MongoDBStore({
|
|
|
54
54
|
|
|
55
55
|
**dbName** (`string`): The name of the database you want the storage to use.
|
|
56
56
|
|
|
57
|
-
**options** (`MongoClientOptions`): MongoDB client options for advanced configuration (SSL, connection pooling, etc.).
|
|
57
|
+
**options** (`MongoClientOptions`): MongoDB client options for advanced configuration (SSL, connection pooling, etc.). See Connection Options
|
|
58
|
+
|
|
59
|
+
**disableInit** (`boolean`): When true, automatic initialization (collection creation) is disabled. Useful for CI/CD pipelines where you want to run migrations explicitly. You must call storage.init() manually when this is true.
|
|
60
|
+
|
|
61
|
+
**skipDefaultIndexes** (`boolean`): When true, default indexes will not be created during initialization. Useful when managing indexes separately or using only custom indexes.
|
|
62
|
+
|
|
63
|
+
**indexes** (`MongoDBIndexConfig[]`): Custom indexes to create during initialization. Each index must specify a collection, keys, and optional index options. See Indexes
|
|
64
|
+
|
|
65
|
+
**connectorHandler** (`ConnectorHandler`): Custom connection handler for advanced connection management. Alternative to providing uri/dbName directly.
|
|
58
66
|
|
|
59
67
|
> **Deprecation Notice:** The `url` parameter is deprecated but still supported for backward compatibility. Please use `uri` instead in all new code.
|
|
60
68
|
|
|
@@ -84,6 +92,26 @@ const store2 = new MongoDBStore({
|
|
|
84
92
|
socketTimeoutMS: 45000,
|
|
85
93
|
},
|
|
86
94
|
})
|
|
95
|
+
|
|
96
|
+
// With custom indexes
|
|
97
|
+
const store3 = new MongoDBStore({
|
|
98
|
+
id: 'mongodb-storage-03',
|
|
99
|
+
uri: 'mongodb+srv://user:password@cluster.mongodb.net',
|
|
100
|
+
dbName: 'mastra_storage',
|
|
101
|
+
indexes: [
|
|
102
|
+
{ collection: 'mastra_threads', keys: { 'metadata.type': 1 } },
|
|
103
|
+
{ collection: 'mastra_messages', keys: { 'metadata.status': 1 }, options: { sparse: true } },
|
|
104
|
+
],
|
|
105
|
+
})
|
|
106
|
+
|
|
107
|
+
// For CI/CD with explicit initialization
|
|
108
|
+
const store4 = new MongoDBStore({
|
|
109
|
+
id: 'mongodb-storage-04',
|
|
110
|
+
uri: 'mongodb+srv://user:password@cluster.mongodb.net',
|
|
111
|
+
dbName: 'mastra_storage',
|
|
112
|
+
disableInit: true, // Disable auto-init
|
|
113
|
+
})
|
|
114
|
+
await store4.init() // Call init explicitly
|
|
87
115
|
```
|
|
88
116
|
|
|
89
117
|
## Additional notes
|
|
@@ -114,7 +142,7 @@ import { MongoDBStore } from '@mastra/mongodb'
|
|
|
114
142
|
const storage = new MongoDBStore({
|
|
115
143
|
id: 'mongodb-storage',
|
|
116
144
|
uri: process.env.MONGODB_URI,
|
|
117
|
-
dbName: process.env.
|
|
145
|
+
dbName: process.env.MONGODB_DB_NAME,
|
|
118
146
|
})
|
|
119
147
|
|
|
120
148
|
const mastra = new Mastra({
|
|
@@ -130,7 +158,7 @@ import { MongoDBStore } from '@mastra/mongodb'
|
|
|
130
158
|
const storage = new MongoDBStore({
|
|
131
159
|
id: 'mongodb-storage',
|
|
132
160
|
uri: process.env.MONGODB_URI,
|
|
133
|
-
dbName: process.env.
|
|
161
|
+
dbName: process.env.MONGODB_DB_NAME,
|
|
134
162
|
})
|
|
135
163
|
|
|
136
164
|
// Required when using storage directly
|
|
@@ -143,57 +171,28 @@ const thread = await memoryStore?.getThreadById({ threadId: '...' })
|
|
|
143
171
|
|
|
144
172
|
> **Warning:** If `init()` isn't called, collections won't be created and storage operations will fail silently or throw errors.
|
|
145
173
|
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
MongoDB storage includes built-in vector search capabilities for AI applications:
|
|
174
|
+
### Connection Management
|
|
149
175
|
|
|
150
|
-
|
|
176
|
+
The `close()` method closes the MongoDB client connection. Call this when shutting down your application:
|
|
151
177
|
|
|
152
178
|
```typescript
|
|
153
|
-
import {
|
|
179
|
+
import { MongoDBStore } from '@mastra/mongodb'
|
|
154
180
|
|
|
155
|
-
const
|
|
156
|
-
id: 'mongodb-
|
|
181
|
+
const storage = new MongoDBStore({
|
|
182
|
+
id: 'mongodb-storage',
|
|
157
183
|
uri: process.env.MONGODB_URI,
|
|
158
|
-
dbName: process.env.
|
|
184
|
+
dbName: process.env.MONGODB_DB_NAME,
|
|
159
185
|
})
|
|
160
186
|
|
|
161
|
-
//
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
})
|
|
187
|
+
// Use storage...
|
|
188
|
+
|
|
189
|
+
// Clean up on shutdown
|
|
190
|
+
await storage.close()
|
|
166
191
|
```
|
|
167
192
|
|
|
168
|
-
|
|
193
|
+
## Vector search capabilities
|
|
169
194
|
|
|
170
|
-
|
|
171
|
-
// Store vectors with metadata
|
|
172
|
-
await vectorStore.upsert({
|
|
173
|
-
indexName: "document_embeddings",
|
|
174
|
-
vectors: [
|
|
175
|
-
{
|
|
176
|
-
id: "doc-1",
|
|
177
|
-
values: [0.1, 0.2, 0.3, ...], // 1536-dimensional vector
|
|
178
|
-
metadata: {
|
|
179
|
-
title: "Document Title",
|
|
180
|
-
category: "technical",
|
|
181
|
-
source: "api-docs",
|
|
182
|
-
},
|
|
183
|
-
},
|
|
184
|
-
],
|
|
185
|
-
});
|
|
186
|
-
|
|
187
|
-
// Similarity search
|
|
188
|
-
const results = await vectorStore.query({
|
|
189
|
-
indexName: "document_embeddings",
|
|
190
|
-
vector: queryEmbedding,
|
|
191
|
-
topK: 5,
|
|
192
|
-
filter: {
|
|
193
|
-
category: "technical",
|
|
194
|
-
},
|
|
195
|
-
});
|
|
196
|
-
```
|
|
195
|
+
MongoDB storage includes built-in vector search capabilities for AI applications. For detailed vector operations including index creation, upserting embeddings, similarity search, and metadata filtering, see the [MongoDB vector reference](https://mastra.ai/reference/vectors/mongodb).
|
|
197
196
|
|
|
198
197
|
## Usage example
|
|
199
198
|
|
|
@@ -214,6 +213,7 @@ export const mongodbAgent = new Agent({
|
|
|
214
213
|
model: 'openai/gpt-5.5',
|
|
215
214
|
memory: new Memory({
|
|
216
215
|
storage: new MongoDBStore({
|
|
216
|
+
id: 'mongodb-storage',
|
|
217
217
|
uri: process.env.MONGODB_URI!,
|
|
218
218
|
dbName: process.env.MONGODB_DB_NAME!,
|
|
219
219
|
}),
|