@mastra/libsql 0.0.0-error-handler-fix-20251020202607 → 0.0.0-esbuild-bundle-worker-20260807182016
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 +4789 -3
- package/LICENSE.md +15 -0
- package/README.md +30 -20
- package/dist/docs/SKILL.md +52 -0
- package/dist/docs/assets/SOURCE_MAP.json +6 -0
- package/dist/docs/references/docs-agents-agent-approval.md +664 -0
- package/dist/docs/references/docs-agents-networks.md +184 -0
- package/dist/docs/references/docs-deployment-workers.md +137 -0
- package/dist/docs/references/docs-editor-overview.md +349 -0
- package/dist/docs/references/docs-memory-memory-processors.md +385 -0
- package/dist/docs/references/docs-memory-message-history.md +348 -0
- package/dist/docs/references/docs-memory-multi-user-threads.md +208 -0
- package/dist/docs/references/docs-memory-overview.md +266 -0
- package/dist/docs/references/docs-memory-semantic-recall.md +401 -0
- package/dist/docs/references/docs-memory-working-memory.md +431 -0
- package/dist/docs/references/docs-storage-overview.md +214 -0
- package/dist/docs/references/docs-workflows-snapshots.md +240 -0
- package/dist/docs/references/guides-agent-frameworks-ai-sdk.md +142 -0
- package/dist/docs/references/guides-rag-retrieval.md +520 -0
- package/dist/docs/references/reference-core-getMemory.md +51 -0
- package/dist/docs/references/reference-core-listMemory.md +57 -0
- package/dist/docs/references/reference-core-mastra-class.md +153 -0
- package/dist/docs/references/reference-file-based-agents-memory.md +58 -0
- package/dist/docs/references/reference-file-based-agents-storage.md +30 -0
- package/dist/docs/references/reference-memory-memory-class.md +148 -0
- package/dist/docs/references/reference-storage-composite.md +397 -0
- package/dist/docs/references/reference-storage-dynamodb.md +284 -0
- package/dist/docs/references/reference-storage-libsql.md +143 -0
- package/dist/docs/references/reference-storage-retention.md +248 -0
- package/dist/docs/references/reference-vectors-libsql.md +307 -0
- package/dist/index.cjs +13164 -3390
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +13134 -3383
- package/dist/index.js.map +1 -1
- package/dist/storage/db/index.d.ts +389 -0
- package/dist/storage/db/index.d.ts.map +1 -0
- package/dist/storage/db/utils.d.ts +84 -0
- package/dist/storage/db/utils.d.ts.map +1 -0
- package/dist/storage/db/write-lock.d.ts +8 -0
- package/dist/storage/db/write-lock.d.ts.map +1 -0
- package/dist/storage/domains/agents/index.d.ts +30 -0
- package/dist/storage/domains/agents/index.d.ts.map +1 -0
- package/dist/storage/domains/background-tasks/index.d.ts +27 -0
- package/dist/storage/domains/background-tasks/index.d.ts.map +1 -0
- package/dist/storage/domains/blobs/index.d.ts +17 -0
- package/dist/storage/domains/blobs/index.d.ts.map +1 -0
- package/dist/storage/domains/channels/index.d.ts +20 -0
- package/dist/storage/domains/channels/index.d.ts.map +1 -0
- package/dist/storage/domains/datasets/index.d.ts +43 -0
- package/dist/storage/domains/datasets/index.d.ts.map +1 -0
- package/dist/storage/domains/experiments/index.d.ts +54 -0
- package/dist/storage/domains/experiments/index.d.ts.map +1 -0
- package/dist/storage/domains/favorites/index.d.ts +17 -0
- package/dist/storage/domains/favorites/index.d.ts.map +1 -0
- package/dist/storage/domains/harness/index.d.ts +17 -0
- package/dist/storage/domains/harness/index.d.ts.map +1 -0
- package/dist/storage/domains/mcp-clients/index.d.ts +26 -0
- package/dist/storage/domains/mcp-clients/index.d.ts.map +1 -0
- package/dist/storage/domains/mcp-servers/index.d.ts +26 -0
- package/dist/storage/domains/mcp-servers/index.d.ts.map +1 -0
- package/dist/storage/domains/memory/index.d.ts +58 -54
- package/dist/storage/domains/memory/index.d.ts.map +1 -1
- package/dist/storage/domains/notifications/index.d.ts +23 -0
- package/dist/storage/domains/notifications/index.d.ts.map +1 -0
- package/dist/storage/domains/observability/index.d.ts +49 -29
- package/dist/storage/domains/observability/index.d.ts.map +1 -1
- package/dist/storage/domains/prompt-blocks/index.d.ts +25 -0
- package/dist/storage/domains/prompt-blocks/index.d.ts.map +1 -0
- package/dist/storage/domains/schedules/index.d.ts +27 -0
- package/dist/storage/domains/schedules/index.d.ts.map +1 -0
- package/dist/storage/domains/scorer-definitions/index.d.ts +26 -0
- package/dist/storage/domains/scorer-definitions/index.d.ts.map +1 -0
- package/dist/storage/domains/scores/index.d.ts +29 -31
- package/dist/storage/domains/scores/index.d.ts.map +1 -1
- package/dist/storage/domains/skills/index.d.ts +26 -0
- package/dist/storage/domains/skills/index.d.ts.map +1 -0
- package/dist/storage/domains/thread-state/index.d.ts +38 -0
- package/dist/storage/domains/thread-state/index.d.ts.map +1 -0
- package/dist/storage/domains/tool-provider-connections/index.d.ts +14 -0
- package/dist/storage/domains/tool-provider-connections/index.d.ts.map +1 -0
- package/dist/storage/domains/utils.d.ts +28 -54
- package/dist/storage/domains/utils.d.ts.map +1 -1
- package/dist/storage/domains/workflow-definitions/index.d.ts +14 -0
- package/dist/storage/domains/workflow-definitions/index.d.ts.map +1 -0
- package/dist/storage/domains/workflows/index.d.ts +27 -32
- package/dist/storage/domains/workflows/index.d.ts.map +1 -1
- package/dist/storage/domains/workspaces/index.d.ts +26 -0
- package/dist/storage/domains/workspaces/index.d.ts.map +1 -0
- package/dist/storage/factory-storage.d.ts +28 -0
- package/dist/storage/factory-storage.d.ts.map +1 -0
- package/dist/storage/index.d.ts +128 -255
- package/dist/storage/index.d.ts.map +1 -1
- package/dist/storage/retention.d.ts +77 -0
- package/dist/storage/retention.d.ts.map +1 -0
- package/dist/vector/index.d.ts +31 -4
- package/dist/vector/index.d.ts.map +1 -1
- package/dist/vector/sql-builder.d.ts.map +1 -1
- package/package.json +22 -16
- package/dist/storage/domains/legacy-evals/index.d.ts +0 -18
- package/dist/storage/domains/legacy-evals/index.d.ts.map +0 -1
- package/dist/storage/domains/operations/index.d.ts +0 -110
- package/dist/storage/domains/operations/index.d.ts.map +0 -1
- package/dist/storage/domains/traces/index.d.ts +0 -21
- package/dist/storage/domains/traces/index.d.ts.map +0 -1
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
> Discover all available pages from the documentation index: https://mastra.ai/llms.txt
|
|
2
|
+
|
|
3
|
+
# Mastra class
|
|
4
|
+
|
|
5
|
+
The `Mastra` class is the central orchestrator in any Mastra application, managing agents, workflows, storage, logging, observability, and more. Typically, you create a single instance of `Mastra` to coordinate your application.
|
|
6
|
+
|
|
7
|
+
Think of `Mastra` as a top-level registry where you register agents, workflows, tools, and other components that need to be accessible throughout your application.
|
|
8
|
+
|
|
9
|
+
## Usage example
|
|
10
|
+
|
|
11
|
+
```typescript
|
|
12
|
+
import { Mastra } from '@mastra/core'
|
|
13
|
+
import { PinoLogger } from '@mastra/loggers'
|
|
14
|
+
import { LibSQLStore } from '@mastra/libsql'
|
|
15
|
+
import { weatherWorkflow } from './workflows/weather-workflow'
|
|
16
|
+
import { weatherAgent } from './agents/weather-agent'
|
|
17
|
+
|
|
18
|
+
export const mastra = new Mastra({
|
|
19
|
+
workflows: { weatherWorkflow },
|
|
20
|
+
agents: { weatherAgent },
|
|
21
|
+
storage: new LibSQLStore({
|
|
22
|
+
id: 'mastra-storage',
|
|
23
|
+
url: ':memory:',
|
|
24
|
+
}),
|
|
25
|
+
logger: new PinoLogger({
|
|
26
|
+
name: 'Mastra',
|
|
27
|
+
level: 'info',
|
|
28
|
+
}),
|
|
29
|
+
})
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
Enable scheduled notification dispatch when deferred notification records and notification summaries should be delivered automatically through the workflow scheduler:
|
|
33
|
+
|
|
34
|
+
```typescript
|
|
35
|
+
export const mastra = new Mastra({
|
|
36
|
+
agents: { supportAgent },
|
|
37
|
+
storage,
|
|
38
|
+
notifications: {
|
|
39
|
+
dispatch: {
|
|
40
|
+
enabled: true,
|
|
41
|
+
cron: '*/1 * * * *',
|
|
42
|
+
batchSize: 100,
|
|
43
|
+
},
|
|
44
|
+
},
|
|
45
|
+
})
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
`notifications.dispatch.enabled` allows an internal dispatcher workflow to run with the default cron `*/1 * * * *`. The dispatcher reads due notification records from storage, groups summaries by `agentId`, `resourceId`, and `threadId`, and emits signals through the agent thread runtime. It isn't a user-facing entrypoint. The dispatch schedule (and the workflow scheduler backing it) activates lazily on the first deferred or summarized notification, so apps that never defer notifications don't run a scheduler at all.
|
|
49
|
+
|
|
50
|
+
## Constructor parameters
|
|
51
|
+
|
|
52
|
+
Visit the [Configuration reference](https://mastra.ai/reference/configuration) for detailed documentation on all available configuration options.
|
|
53
|
+
|
|
54
|
+
**agents** (`Record<string, Agent>`): Agent instances to register, keyed by name (Default: `{}`)
|
|
55
|
+
|
|
56
|
+
**tools** (`Record<string, ToolApi>`): Tool instances to register. Keys are registration keys used by \`getTool()\`, and values are tool instances. Use \`getToolById()\` for intrinsic ID lookup and \`listTools()\` to read the registry. (Default: `{}`)
|
|
57
|
+
|
|
58
|
+
**storage** (`MastraCompositeStore`): Storage engine instance for persisting data
|
|
59
|
+
|
|
60
|
+
**vectors** (`Record<string, MastraVector>`): Vector store instance, used for semantic search and vector-based tools (eg Pinecone, PgVector or Qdrant)
|
|
61
|
+
|
|
62
|
+
**logger** (`Logger`): Logger instance created with new PinoLogger() (Default: `Console logger with INFO level`)
|
|
63
|
+
|
|
64
|
+
**idGenerator** (`(context?: IdGeneratorContext) => string`): Custom ID generator function. Used by agents, workflows, memory, and other components to generate unique identifiers. Receives optional context such as idType, source, entityId, and threadId to support context-aware ID formats.
|
|
65
|
+
|
|
66
|
+
**workflows** (`Record<string, Workflow>`): Workflows to register. Structured as a key-value pair, with keys being the workflow name and values being the workflow instance. (Default: `{}`)
|
|
67
|
+
|
|
68
|
+
**tts** (`Record<string, MastraVoice>`): Text-to-speech providers for voice synthesis
|
|
69
|
+
|
|
70
|
+
**observability** (`ObservabilityEntrypoint`): Observability configuration for tracing and monitoring
|
|
71
|
+
|
|
72
|
+
**environment** (`string`): Deployment environment name (e.g. production, staging, development). When set, automatically attached to all observability signals so they can be filtered by environment without passing tracingOptions.metadata.environment on each call. Falls back to process.env.NODE\_ENV when unset; left undefined if neither is set. Per-call tracingOptions.metadata.environment always takes precedence.
|
|
73
|
+
|
|
74
|
+
**deployer** (`MastraDeployer`): An instance of a MastraDeployer for managing deployments.
|
|
75
|
+
|
|
76
|
+
**server** (`ServerConfig`): Server configuration including port, host, timeout, API routes, middleware, CORS settings, and build options for Swagger UI, API request logging, and OpenAPI docs.
|
|
77
|
+
|
|
78
|
+
**mcpServers** (`Record<string, MCPServerBase>`): An object where keys are registry keys (used for getMCPServer()) and values are instances of MCPServer or classes extending MCPServerBase. Each MCPServer must have an id property. Servers can be retrieved by registry key using getMCPServer() or by their intrinsic id using getMCPServerById().
|
|
79
|
+
|
|
80
|
+
**bundler** (`BundlerConfig`): Configuration for the asset bundler with options for externals, sourcemap, transpilePackages, and dynamicPackages. (Default: `{ externals: [], sourcemap: false, transpilePackages: [], dynamicPackages: [] }`)
|
|
81
|
+
|
|
82
|
+
**scorers** (`Record<string, Scorer>`): Scorers for evaluating agent responses and workflow outputs (Default: `{}`)
|
|
83
|
+
|
|
84
|
+
**processors** (`Record<string, Processor>`): Input/output processors for transforming agent inputs and outputs (Default: `{}`)
|
|
85
|
+
|
|
86
|
+
**gateways** (`Record<string, MastraModelGateway>`): Custom model gateways to register for accessing AI models through alternative providers or private deployments. Structured as a key-value pair, with keys being the registry key (used for getGateway()) and values being gateway instances. (Default: `{}`)
|
|
87
|
+
|
|
88
|
+
**memory** (`Record<string, MastraMemory>`): Memory instances to register. These can be referenced by stored agents and resolved at runtime. Structured as a key-value pair, with keys being the registry key and values being memory instances. (Default: `{}`)
|
|
89
|
+
|
|
90
|
+
**notifications** (`object`): Runtime configuration for notification signal dispatch.
|
|
91
|
+
|
|
92
|
+
**notifications.dispatch** (`NotificationDispatchConfig`): Scheduled dispatch configuration for deferred notifications and notification summaries. Dispatch is enabled by default.
|
|
93
|
+
|
|
94
|
+
**notifications.dispatch.enabled** (`boolean`): Set to false to opt out of automatic scheduled notification dispatch.
|
|
95
|
+
|
|
96
|
+
**notifications.dispatch.cron** (`string`): Cron schedule used by the internal notification dispatcher workflow.
|
|
97
|
+
|
|
98
|
+
**notifications.dispatch.batchSize** (`number`): Maximum number of due notification records to process per dispatch run.
|
|
99
|
+
|
|
100
|
+
**versions** (`VersionOverrides`): Global version overrides for sub-agent delegation. When a supervisor agent delegates to a sub-agent, these overrides determine which stored version of that sub-agent to use instead of the code-defined default. Requires the editor package to be configured. See Editor versioning for details.
|
|
101
|
+
|
|
102
|
+
**versions.agents** (`Record<string, VersionSelector>`): A map of agent IDs to their version selectors. Each selector can target a specific version by ID or by publication status.
|
|
103
|
+
|
|
104
|
+
**versions.agents.versionId** (`string`): The ID of a specific version to use.
|
|
105
|
+
|
|
106
|
+
**versions.agents.status** (`'draft' | 'published'`): Select the latest version with this publication status.
|
|
107
|
+
|
|
108
|
+
**workers** (`MastraWorker[] | false`): Configure which workers run in this Mastra instance. When omitted, Mastra auto-creates default workers based on your PubSub and config. Pass false to disable all event processing (useful when running standalone workers separately). Pass a MastraWorker\[] to add custom workers — they are merged with the auto-created defaults, and a custom worker with the same name as a default replaces it.
|
|
109
|
+
|
|
110
|
+
**backgroundTasks** (`BackgroundTaskManagerConfig`): Configure background task execution for agents. See background tasks configuration reference for all options.
|
|
111
|
+
|
|
112
|
+
**backgroundTasks.enabled** (`boolean`): Enable background task dispatch.
|
|
113
|
+
|
|
114
|
+
**backgroundTasks.globalConcurrency** (`number`): Max concurrent tasks across all agents.
|
|
115
|
+
|
|
116
|
+
**backgroundTasks.perAgentConcurrency** (`number`): Max concurrent tasks per agent.
|
|
117
|
+
|
|
118
|
+
**backgroundTasks.backpressure** (`'queue' | 'reject' | 'fallback-sync'`): Behavior when concurrency limit is reached.
|
|
119
|
+
|
|
120
|
+
**backgroundTasks.defaultTimeoutMs** (`number`): Default task timeout in milliseconds.
|
|
121
|
+
|
|
122
|
+
**backgroundTasks.defaultRetries** (`RetryConfig`): Default retry configuration.
|
|
123
|
+
|
|
124
|
+
**scheduler** (`object`): Configure the scheduler worker for cron-driven workflow triggers. Auto-enables when any workflow declares a schedule. See Scheduled workflows.
|
|
125
|
+
|
|
126
|
+
**scheduler.enabled** (`boolean`): Explicitly enable or disable the scheduler.
|
|
127
|
+
|
|
128
|
+
**recovery** (`MastraRecoveryConfig`): Boot-time recovery behavior for orphaned agent and workflow runs. See Crash recovery. (Default: `{ durableAgents: 'off' }`)
|
|
129
|
+
|
|
130
|
+
**recovery.durableAgents** (`'auto' | 'off'`): Set to 'auto' to automatically re-drive orphaned RUNNING durable agent runs on server boot. Recovery re-issues LLM calls and re-executes tool calls, so tools must be idempotent. See Crash recovery.
|
|
131
|
+
|
|
132
|
+
## Methods
|
|
133
|
+
|
|
134
|
+
### `recoverAllDurableAgents()`
|
|
135
|
+
|
|
136
|
+
Re-drives every orphaned `running` durable-agent run across all registered durable agents. Called automatically on boot when `recovery.durableAgents` is `'auto'`. You can also call it directly for manual recovery or from a scheduled task.
|
|
137
|
+
|
|
138
|
+
Requires persistent storage. With an in-memory store, there's nothing to recover after a process restart.
|
|
139
|
+
|
|
140
|
+
```typescript
|
|
141
|
+
const result = await mastra.recoverAllDurableAgents()
|
|
142
|
+
// { agents: 2, recovered: 3, succeeded: 3, failed: 0 }
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
Returns:
|
|
146
|
+
|
|
147
|
+
**agents** (`number`): Number of durable agents scanned.
|
|
148
|
+
|
|
149
|
+
**recovered** (`number`): Total number of runs that were re-driven.
|
|
150
|
+
|
|
151
|
+
**succeeded** (`number`): Runs that restarted successfully.
|
|
152
|
+
|
|
153
|
+
**failed** (`number`): Runs whose restart threw an error.
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
> Discover all available pages from the documentation index: https://mastra.ai/llms.txt
|
|
2
|
+
|
|
3
|
+
# Memory
|
|
4
|
+
|
|
5
|
+
A file-based agent gets [memory](https://mastra.ai/docs/memory/overview) from a `memory.ts` file that default-exports a [`Memory`](https://mastra.ai/reference/memory/memory-class) instance. Use this page for the file-based convention; use the memory docs for message history, semantic recall, storage, and processors.
|
|
6
|
+
|
|
7
|
+
Without `memory.ts` or `config.memory`, the agent has no memory by default. Each `generate()` or `stream()` call starts without remembered conversation state unless you pass the prior context yourself.
|
|
8
|
+
|
|
9
|
+
## Quickstart
|
|
10
|
+
|
|
11
|
+
Create `memory.ts` next to the agent's `config.ts`:
|
|
12
|
+
|
|
13
|
+
```typescript
|
|
14
|
+
import { Memory } from '@mastra/memory'
|
|
15
|
+
|
|
16
|
+
export default new Memory()
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
The exported instance becomes the agent's `memory`. If your app configures a storage provider on the main Mastra instance, memory data is stored there. See [storage](https://mastra.ai/docs/storage/overview) for more information.
|
|
20
|
+
|
|
21
|
+
Use the same `resource` and `thread` values when calling the agent to continue a conversation:
|
|
22
|
+
|
|
23
|
+
```typescript
|
|
24
|
+
const response = await weatherAgent.generate('Remember that I prefer Celsius.', {
|
|
25
|
+
memory: {
|
|
26
|
+
resource: 'user-123',
|
|
27
|
+
thread: 'weather-chat',
|
|
28
|
+
},
|
|
29
|
+
})
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
## Configure memory
|
|
33
|
+
|
|
34
|
+
Pass options to `new Memory()` when the default behavior isn't enough.
|
|
35
|
+
|
|
36
|
+
```typescript
|
|
37
|
+
import { Memory } from '@mastra/memory'
|
|
38
|
+
|
|
39
|
+
export default new Memory({
|
|
40
|
+
options: {
|
|
41
|
+
lastMessages: 20,
|
|
42
|
+
workingMemory: {
|
|
43
|
+
enabled: true,
|
|
44
|
+
scope: 'resource',
|
|
45
|
+
},
|
|
46
|
+
},
|
|
47
|
+
})
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
Visit the [`Memory` reference](https://mastra.ai/reference/memory/memory-class) for constructor options. Use these pages for related memory features:
|
|
51
|
+
|
|
52
|
+
- [Storage](https://mastra.ai/docs/storage/overview): configure persistence for memory data.
|
|
53
|
+
- [Semantic recall](https://mastra.ai/docs/memory/semantic-recall): retrieve relevant past messages by semantic meaning.
|
|
54
|
+
- [Memory processors](https://mastra.ai/docs/memory/memory-processors): filter, trim, or transform messages before memory adds them to model context.
|
|
55
|
+
|
|
56
|
+
## Precedence with config
|
|
57
|
+
|
|
58
|
+
`config.memory` wins over `memory.ts`. If neither is present, the assembled file-based agent has no memory. See [`config.ts` precedence](https://mastra.ai/reference/file-based-agents/config) for the full merge table.
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
> Discover all available pages from the documentation index: https://mastra.ai/llms.txt
|
|
2
|
+
|
|
3
|
+
# Storage
|
|
4
|
+
|
|
5
|
+
Mastra sets the project's default [storage](https://mastra.ai/docs/storage/overview) from a `storage.ts` file directly under `src/mastra/`. The file default-exports a store, which replaces the built-in in-memory store used for memory, workflows, observability, and other storage domains.
|
|
6
|
+
|
|
7
|
+
Use this page for the file-based convention. For backend choice, storage domains, retention, and provider details, see [storage overview](https://mastra.ai/docs/storage/overview).
|
|
8
|
+
|
|
9
|
+
## Quickstart
|
|
10
|
+
|
|
11
|
+
Use [`LibSQLStore`](https://mastra.ai/reference/storage/libsql) for a local file-backed store:
|
|
12
|
+
|
|
13
|
+
```typescript
|
|
14
|
+
import { LibSQLStore } from '@mastra/libsql'
|
|
15
|
+
|
|
16
|
+
export default new LibSQLStore({
|
|
17
|
+
id: 'mastra-storage',
|
|
18
|
+
url: 'file:./mastra.db',
|
|
19
|
+
})
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
Mastra registers the store before file-based agents and workflows, so storage-dependent primitives bind to this store instead of the default in-memory store.
|
|
23
|
+
|
|
24
|
+
## Production backends
|
|
25
|
+
|
|
26
|
+
`storage.ts` can export any Mastra storage adapter, such as LibSQL, PostgreSQL, or MongoDB. For setup patterns, provider support, and schema details, see [storage overview](https://mastra.ai/docs/storage/overview), [observability signal support](https://mastra.ai/docs/observability/overview), and the [storage reference](https://mastra.ai/reference/storage/overview).
|
|
27
|
+
|
|
28
|
+
## Precedence with code
|
|
29
|
+
|
|
30
|
+
Code-registered storage wins over `storage.ts`. Use `storage.ts` when one project-wide store is enough. Use code registration when setup depends on runtime wiring in `src/mastra/index.ts`.
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
> Discover all available pages from the documentation index: https://mastra.ai/llms.txt
|
|
2
|
+
|
|
3
|
+
# Memory class
|
|
4
|
+
|
|
5
|
+
The `Memory` class provides a reliable system for managing conversation history and thread-based message storage in Mastra. It enables persistent storage of conversations, semantic search capabilities, and efficient message retrieval. You must configure a storage provider for conversation history, and if you enable semantic recall you will also need to provide a vector store and embedder.
|
|
6
|
+
|
|
7
|
+
## Usage example
|
|
8
|
+
|
|
9
|
+
```typescript
|
|
10
|
+
import { Memory } from '@mastra/memory'
|
|
11
|
+
import { Agent } from '@mastra/core/agent'
|
|
12
|
+
|
|
13
|
+
export const agent = new Agent({
|
|
14
|
+
id: 'test-agent',
|
|
15
|
+
name: 'test-agent',
|
|
16
|
+
instructions: 'You are an agent with memory.',
|
|
17
|
+
model: 'openai/gpt-5.6-sol',
|
|
18
|
+
memory: new Memory({
|
|
19
|
+
options: {
|
|
20
|
+
workingMemory: {
|
|
21
|
+
enabled: true,
|
|
22
|
+
},
|
|
23
|
+
},
|
|
24
|
+
}),
|
|
25
|
+
})
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
> **Note:** To enable `workingMemory` on an agent, you’ll need a storage provider configured on your main Mastra instance. See [Mastra class](https://mastra.ai/reference/core/mastra-class) for more information.
|
|
29
|
+
|
|
30
|
+
## Constructor parameters
|
|
31
|
+
|
|
32
|
+
**storage** (`MastraCompositeStore`): Storage implementation for persisting memory data. Defaults to new DefaultStorage({ config: { url: "file:memory.db" } }) if not provided.
|
|
33
|
+
|
|
34
|
+
**vector** (`MastraVector | false`): Vector store for semantic search capabilities. Set to false to disable vector operations.
|
|
35
|
+
|
|
36
|
+
**embedder** (`EmbeddingModel<string> | EmbeddingModelV2<string>`): Embedder instance for vector embeddings. Required when semantic recall is enabled.
|
|
37
|
+
|
|
38
|
+
**options** (`MemoryConfig`): Memory configuration options.
|
|
39
|
+
|
|
40
|
+
**options.lastMessages** (`number | false`): Number of most recent messages to include in context. Set to false to disable the message history feature entirely (messages are not loaded into context or saved). Use Number.MAX\_SAFE\_INTEGER to retrieve all messages with no limit. To load messages without saving new ones, use the readOnly option.
|
|
41
|
+
|
|
42
|
+
**options.readOnly** (`boolean`): When true, prevents memory from saving new messages and provides working memory as read-only context (without the updateWorkingMemory tool). Useful for read-only operations like previews, internal routing agents, or sub agents that should reference but not modify memory.
|
|
43
|
+
|
|
44
|
+
**options.semanticRecall** (`boolean | { topK: number; messageRange: number | { before: number; after: number }; scope?: 'thread' | 'resource' }`): Enable semantic search in message history. Can be a boolean or an object with configuration options. When enabled, requires both vector store and embedder to be configured. Default topK is 4, default messageRange is {before: 1, after: 1}.
|
|
45
|
+
|
|
46
|
+
**options.workingMemory** (`WorkingMemory`): Configuration for working memory feature. Can be { enabled: boolean; template?: string; schema?: ZodObject\<any> | JSONSchema7; scope?: 'thread' | 'resource' } or { enabled: boolean } to disable.
|
|
47
|
+
|
|
48
|
+
**options.observationalMemory** (`boolean | ObservationalMemoryOptions`): Enable Observational Memory for long-context agentic memory. Set to true for defaults, or pass a config object to customize token budgets, models, and scope. See Observational Memory reference for configuration details.
|
|
49
|
+
|
|
50
|
+
**options.generateTitle** (`boolean | { model: DynamicArgument<MastraLanguageModel>; instructions?: DynamicArgument<string> }`): Controls automatic thread title generation from the conversation transcript. Can be a boolean or an object with custom model and instructions.
|
|
51
|
+
|
|
52
|
+
## Returns
|
|
53
|
+
|
|
54
|
+
**memory** (`Memory`): A new Memory instance with the specified configuration.
|
|
55
|
+
|
|
56
|
+
## Extended usage example
|
|
57
|
+
|
|
58
|
+
```typescript
|
|
59
|
+
import { Memory } from '@mastra/memory'
|
|
60
|
+
import { Agent } from '@mastra/core/agent'
|
|
61
|
+
import { LibSQLStore, LibSQLVector } from '@mastra/libsql'
|
|
62
|
+
|
|
63
|
+
export const agent = new Agent({
|
|
64
|
+
name: 'test-agent',
|
|
65
|
+
instructions: 'You are an agent with memory.',
|
|
66
|
+
model: 'openai/gpt-5.6-sol',
|
|
67
|
+
memory: new Memory({
|
|
68
|
+
storage: new LibSQLStore({
|
|
69
|
+
id: 'test-agent-storage',
|
|
70
|
+
url: 'file:./working-memory.db',
|
|
71
|
+
}),
|
|
72
|
+
vector: new LibSQLVector({
|
|
73
|
+
id: 'test-agent-vector',
|
|
74
|
+
url: 'file:./vector-memory.db',
|
|
75
|
+
}),
|
|
76
|
+
options: {
|
|
77
|
+
lastMessages: 10,
|
|
78
|
+
semanticRecall: {
|
|
79
|
+
topK: 3,
|
|
80
|
+
messageRange: 2,
|
|
81
|
+
scope: 'resource',
|
|
82
|
+
},
|
|
83
|
+
workingMemory: {
|
|
84
|
+
enabled: true,
|
|
85
|
+
},
|
|
86
|
+
generateTitle: true,
|
|
87
|
+
},
|
|
88
|
+
}),
|
|
89
|
+
})
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
## PostgreSQL with index configuration
|
|
93
|
+
|
|
94
|
+
```typescript
|
|
95
|
+
import { Memory } from '@mastra/memory'
|
|
96
|
+
import { Agent } from '@mastra/core/agent'
|
|
97
|
+
import { ModelRouterEmbeddingModel } from '@mastra/core/llm'
|
|
98
|
+
import { PgStore, PgVector } from '@mastra/pg'
|
|
99
|
+
|
|
100
|
+
export const agent = new Agent({
|
|
101
|
+
name: 'pg-agent',
|
|
102
|
+
instructions: 'You are an agent with optimized PostgreSQL memory.',
|
|
103
|
+
model: 'openai/gpt-5.6-sol',
|
|
104
|
+
memory: new Memory({
|
|
105
|
+
storage: new PgStore({
|
|
106
|
+
id: 'pg-agent-storage',
|
|
107
|
+
connectionString: process.env.DATABASE_URL,
|
|
108
|
+
}),
|
|
109
|
+
vector: new PgVector({
|
|
110
|
+
id: 'pg-agent-vector',
|
|
111
|
+
connectionString: process.env.DATABASE_URL,
|
|
112
|
+
}),
|
|
113
|
+
embedder: new ModelRouterEmbeddingModel('openai/text-embedding-3-small'),
|
|
114
|
+
options: {
|
|
115
|
+
lastMessages: 20,
|
|
116
|
+
semanticRecall: {
|
|
117
|
+
topK: 5,
|
|
118
|
+
messageRange: 3,
|
|
119
|
+
scope: 'resource',
|
|
120
|
+
indexConfig: {
|
|
121
|
+
type: 'hnsw', // Use HNSW for better performance
|
|
122
|
+
metric: 'dotproduct', // Optimal for OpenAI embeddings
|
|
123
|
+
m: 16, // Number of bi-directional links
|
|
124
|
+
efConstruction: 64, // Construction-time candidate list size
|
|
125
|
+
},
|
|
126
|
+
},
|
|
127
|
+
workingMemory: {
|
|
128
|
+
enabled: true,
|
|
129
|
+
},
|
|
130
|
+
},
|
|
131
|
+
}),
|
|
132
|
+
})
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
### Related
|
|
136
|
+
|
|
137
|
+
- [Getting Started with Memory](https://mastra.ai/docs/memory/overview)
|
|
138
|
+
- [Semantic Recall](https://mastra.ai/docs/memory/semantic-recall)
|
|
139
|
+
- [Working Memory](https://mastra.ai/docs/memory/working-memory)
|
|
140
|
+
- [Observational Memory](https://mastra.ai/docs/memory/observational-memory)
|
|
141
|
+
- [Memory Processors](https://mastra.ai/docs/memory/memory-processors)
|
|
142
|
+
- [createThread](https://mastra.ai/reference/memory/createThread)
|
|
143
|
+
- [recall](https://mastra.ai/reference/memory/recall)
|
|
144
|
+
- [getThreadById](https://mastra.ai/reference/memory/getThreadById)
|
|
145
|
+
- [listThreads](https://mastra.ai/reference/memory/listThreads)
|
|
146
|
+
- [deleteMessages](https://mastra.ai/reference/memory/deleteMessages)
|
|
147
|
+
- [cloneThread](https://mastra.ai/reference/memory/cloneThread)
|
|
148
|
+
- [Clone Utility Methods](https://mastra.ai/reference/memory/clone-utilities)
|