@mastra/memory 1.5.0 → 1.5.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.
Files changed (50) hide show
  1. package/CHANGELOG.md +30 -0
  2. package/dist/{chunk-DF7NDDSM.js → chunk-6PKWQ3GH.js} +28 -11
  3. package/dist/chunk-6PKWQ3GH.js.map +1 -0
  4. package/dist/{chunk-LLTHE64H.cjs → chunk-6XVTMLW4.cjs} +28 -11
  5. package/dist/chunk-6XVTMLW4.cjs.map +1 -0
  6. package/dist/index.cjs +1 -1
  7. package/dist/index.js +1 -1
  8. package/dist/{observational-memory-ZNTAIUGT.js → observational-memory-AJWSMZVP.js} +3 -3
  9. package/dist/{observational-memory-ZNTAIUGT.js.map → observational-memory-AJWSMZVP.js.map} +1 -1
  10. package/dist/{observational-memory-4PCXEZIS.cjs → observational-memory-Q5TO525O.cjs} +17 -17
  11. package/dist/{observational-memory-4PCXEZIS.cjs.map → observational-memory-Q5TO525O.cjs.map} +1 -1
  12. package/dist/processors/index.cjs +15 -15
  13. package/dist/processors/index.js +1 -1
  14. package/dist/processors/observational-memory/observational-memory.d.ts.map +1 -1
  15. package/dist/processors/observational-memory/token-counter.d.ts.map +1 -1
  16. package/package.json +7 -7
  17. package/dist/chunk-DF7NDDSM.js.map +0 -1
  18. package/dist/chunk-LLTHE64H.cjs.map +0 -1
  19. package/dist/docs/SKILL.md +0 -54
  20. package/dist/docs/assets/SOURCE_MAP.json +0 -103
  21. package/dist/docs/references/docs-agents-agent-approval.md +0 -377
  22. package/dist/docs/references/docs-agents-agent-memory.md +0 -212
  23. package/dist/docs/references/docs-agents-network-approval.md +0 -275
  24. package/dist/docs/references/docs-agents-networks.md +0 -290
  25. package/dist/docs/references/docs-memory-memory-processors.md +0 -316
  26. package/dist/docs/references/docs-memory-message-history.md +0 -260
  27. package/dist/docs/references/docs-memory-observational-memory.md +0 -246
  28. package/dist/docs/references/docs-memory-overview.md +0 -45
  29. package/dist/docs/references/docs-memory-semantic-recall.md +0 -272
  30. package/dist/docs/references/docs-memory-storage.md +0 -261
  31. package/dist/docs/references/docs-memory-working-memory.md +0 -400
  32. package/dist/docs/references/reference-core-getMemory.md +0 -50
  33. package/dist/docs/references/reference-core-listMemory.md +0 -56
  34. package/dist/docs/references/reference-memory-clone-utilities.md +0 -199
  35. package/dist/docs/references/reference-memory-cloneThread.md +0 -130
  36. package/dist/docs/references/reference-memory-createThread.md +0 -68
  37. package/dist/docs/references/reference-memory-getThreadById.md +0 -24
  38. package/dist/docs/references/reference-memory-listThreads.md +0 -145
  39. package/dist/docs/references/reference-memory-memory-class.md +0 -147
  40. package/dist/docs/references/reference-memory-observational-memory.md +0 -565
  41. package/dist/docs/references/reference-processors-token-limiter-processor.md +0 -113
  42. package/dist/docs/references/reference-storage-dynamodb.md +0 -282
  43. package/dist/docs/references/reference-storage-libsql.md +0 -135
  44. package/dist/docs/references/reference-storage-mongodb.md +0 -262
  45. package/dist/docs/references/reference-storage-postgresql.md +0 -529
  46. package/dist/docs/references/reference-storage-upstash.md +0 -160
  47. package/dist/docs/references/reference-vectors-libsql.md +0 -305
  48. package/dist/docs/references/reference-vectors-mongodb.md +0 -295
  49. package/dist/docs/references/reference-vectors-pg.md +0 -408
  50. package/dist/docs/references/reference-vectors-upstash.md +0 -294
@@ -1,130 +0,0 @@
1
- # Memory.cloneThread()
2
-
3
- The `.cloneThread()` method creates a copy of an existing conversation thread, including all its messages. This enables creating divergent conversation paths from a specific point in a conversation. When semantic recall is enabled, the method also creates vector embeddings for the cloned messages.
4
-
5
- ## Usage Example
6
-
7
- ```typescript
8
- const { thread, clonedMessages } = await memory.cloneThread({
9
- sourceThreadId: "original-thread-123",
10
- });
11
- ```
12
-
13
- ## Parameters
14
-
15
- **sourceThreadId:** (`string`): The ID of the thread to clone
16
-
17
- **newThreadId?:** (`string`): Optional custom ID for the cloned thread. If not provided, one will be generated.
18
-
19
- **resourceId?:** (`string`): Optional resource ID for the cloned thread. Defaults to the source thread's resourceId.
20
-
21
- **title?:** (`string`): Optional title for the cloned thread. Defaults to '\[source title] (Copy)'.
22
-
23
- **metadata?:** (`Record<string, unknown>`): Optional metadata to merge with the source thread's metadata. Clone metadata is automatically added.
24
-
25
- **options?:** (`CloneOptions`): Optional filtering options for the clone operation.
26
-
27
- ### Options Parameters
28
-
29
- **messageLimit?:** (`number`): Maximum number of messages to clone. When set, clones the most recent N messages.
30
-
31
- **messageFilter?:** (`MessageFilter`): Filter criteria for selecting which messages to clone.
32
-
33
- ### MessageFilter Parameters
34
-
35
- **startDate?:** (`Date`): Only clone messages created on or after this date.
36
-
37
- **endDate?:** (`Date`): Only clone messages created on or before this date.
38
-
39
- **messageIds?:** (`string[]`): Only clone messages with these specific IDs.
40
-
41
- ## Returns
42
-
43
- **thread:** (`StorageThreadType`): The newly created cloned thread with clone metadata.
44
-
45
- **clonedMessages:** (`MastraDBMessage[]`): Array of the cloned messages with new IDs assigned to the new thread.
46
-
47
- ### Clone Metadata
48
-
49
- The cloned thread's metadata includes a `clone` property with:
50
-
51
- **sourceThreadId:** (`string`): The ID of the original thread that was cloned.
52
-
53
- **clonedAt:** (`Date`): Timestamp when the clone was created.
54
-
55
- **lastMessageId?:** (`string`): The ID of the last message in the source thread at the time of cloning.
56
-
57
- ## Extended Usage Example
58
-
59
- ```typescript
60
- import { mastra } from "./mastra";
61
-
62
- const agent = mastra.getAgent("agent");
63
- const memory = await agent.getMemory();
64
-
65
- // Clone a thread with all messages
66
- const { thread: fullClone } = await memory.cloneThread({
67
- sourceThreadId: "original-thread-123",
68
- title: "Alternative Conversation Path",
69
- });
70
-
71
- // Clone with a custom ID
72
- const { thread: customIdClone } = await memory.cloneThread({
73
- sourceThreadId: "original-thread-123",
74
- newThreadId: "my-custom-clone-id",
75
- });
76
-
77
- // Clone only the last 5 messages
78
- const { thread: partialClone, clonedMessages } = await memory.cloneThread({
79
- sourceThreadId: "original-thread-123",
80
- options: {
81
- messageLimit: 5,
82
- },
83
- });
84
-
85
- // Clone messages from a specific date range
86
- const { thread: dateFilteredClone } = await memory.cloneThread({
87
- sourceThreadId: "original-thread-123",
88
- options: {
89
- messageFilter: {
90
- startDate: new Date("2024-01-01"),
91
- endDate: new Date("2024-01-31"),
92
- },
93
- },
94
- });
95
-
96
- // Continue conversation on the cloned thread
97
- const response = await agent.generate("Let's try a different approach", {
98
- threadId: fullClone.id,
99
- resourceId: fullClone.resourceId,
100
- });
101
- ```
102
-
103
- ## Vector Embeddings
104
-
105
- When the Memory instance has semantic recall enabled (with a vector store and embedder configured), `cloneThread()` automatically creates vector embeddings for all cloned messages. This ensures that semantic search works correctly on the cloned thread.
106
-
107
- ```typescript
108
- import { Memory } from "@mastra/memory";
109
- import { LibSQLStore, LibSQLVector } from "@mastra/libsql";
110
-
111
- const memory = new Memory({
112
- storage: new LibSQLStore({ id: 'memory-store', url: "file:./memory.db" }),
113
- vector: new LibSQLVector({ id: 'vector-store', url: "file:./vector.db" }),
114
- embedder: embeddingModel,
115
- options: {
116
- semanticRecall: true,
117
- },
118
- });
119
-
120
- // Clone will also create embeddings for cloned messages
121
- const { thread } = await memory.cloneThread({
122
- sourceThreadId: "original-thread",
123
- });
124
-
125
- // Semantic search works on the cloned thread
126
- const results = await memory.recall({
127
- threadId: thread.id,
128
- vectorSearchString: "search query",
129
- });
130
- ```
@@ -1,68 +0,0 @@
1
- # Memory.createThread()
2
-
3
- The `.createThread()` method creates a new conversation thread in the memory system. Each thread represents a distinct conversation or context and can contain multiple messages.
4
-
5
- ## Usage Example
6
-
7
- ```typescript
8
- await memory?.createThread({ resourceId: "user-123" });
9
- ```
10
-
11
- ## Parameters
12
-
13
- **resourceId:** (`string`): Identifier for the resource this thread belongs to (e.g., user ID, project ID)
14
-
15
- **threadId?:** (`string`): Optional custom ID for the thread. If not provided, one will be generated.
16
-
17
- **title?:** (`string`): Optional title for the thread
18
-
19
- **metadata?:** (`Record<string, unknown>`): Optional metadata to associate with the thread
20
-
21
- ## Returns
22
-
23
- **id:** (`string`): Unique identifier of the created thread
24
-
25
- **resourceId:** (`string`): Resource ID associated with the thread
26
-
27
- **title:** (`string`): Title of the thread (if provided)
28
-
29
- **createdAt:** (`Date`): Timestamp when the thread was created
30
-
31
- **updatedAt:** (`Date`): Timestamp when the thread was last updated
32
-
33
- **metadata:** (`Record<string, unknown>`): Additional metadata associated with the thread
34
-
35
- ## Extended usage example
36
-
37
- ```typescript
38
- import { mastra } from "./mastra";
39
-
40
- const agent = mastra.getAgent("agent");
41
- const memory = await agent.getMemory();
42
-
43
- const thread = await memory?.createThread({
44
- resourceId: "user-123",
45
- title: "Memory Test Thread",
46
- metadata: {
47
- source: "test-script",
48
- purpose: "memory-testing",
49
- },
50
- });
51
-
52
- const response = await agent.generate("message for agent", {
53
- memory: {
54
- thread: thread!.id,
55
- resource: thread!.resourceId,
56
- },
57
- });
58
-
59
- console.log(response.text);
60
- ```
61
-
62
- ### Related
63
-
64
- - [Memory Class Reference](https://mastra.ai/reference/memory/memory-class)
65
- - [Getting Started with Memory](https://mastra.ai/docs/memory/overview) (Covers threads concept)
66
- - [getThreadById](https://mastra.ai/reference/memory/getThreadById)
67
- - [listThreads](https://mastra.ai/reference/memory/listThreads)
68
- - [recall](https://mastra.ai/reference/memory/recall)
@@ -1,24 +0,0 @@
1
- # Memory.getThreadById()
2
-
3
- The `.getThreadById()` method retrieves a specific thread by its ID.
4
-
5
- ## Usage Example
6
-
7
- ```typescript
8
- await memory?.getThreadById({ threadId: "thread-123" });
9
- ```
10
-
11
- ## Parameters
12
-
13
- **threadId:** (`string`): The ID of the thread to be retrieved.
14
-
15
- ## Returns
16
-
17
- **thread:** (`Promise<StorageThreadType | null>`): A promise that resolves to the thread associated with the given ID, or null if not found.
18
-
19
- ### Related
20
-
21
- - [Memory Class Reference](https://mastra.ai/reference/memory/memory-class)
22
- - [Getting Started with Memory](https://mastra.ai/docs/memory/overview) (Covers threads concept)
23
- - [createThread](https://mastra.ai/reference/memory/createThread)
24
- - [listThreads](https://mastra.ai/reference/memory/listThreads)
@@ -1,145 +0,0 @@
1
- # Memory.listThreads()
2
-
3
- The `listThreads()` method retrieves threads with pagination support and optional filtering by `resourceId`, `metadata`, or both.
4
-
5
- ## Usage Examples
6
-
7
- ### List all threads with pagination
8
-
9
- ```typescript
10
- const result = await memory.listThreads({
11
- page: 0,
12
- perPage: 10,
13
- });
14
- ```
15
-
16
- ### Fetch all threads without pagination
17
-
18
- Use `perPage: false` to retrieve all matching threads at once.
19
-
20
- > **Warning:** Generally speaking it's recommended to use pagination, especially for large datasets. Use this option cautiously.
21
-
22
- ```typescript
23
- const result = await memory.listThreads({
24
- filter: { resourceId: "user-123" },
25
- perPage: false,
26
- });
27
- ```
28
-
29
- ### Filter by resourceId
30
-
31
- ```typescript
32
- const result = await memory.listThreads({
33
- filter: { resourceId: "user-123" },
34
- page: 0,
35
- perPage: 10,
36
- });
37
- ```
38
-
39
- ### Filter by metadata
40
-
41
- ```typescript
42
- const result = await memory.listThreads({
43
- filter: { metadata: { category: "support", priority: "high" } },
44
- page: 0,
45
- perPage: 10,
46
- });
47
- ```
48
-
49
- ### Combined filter (resourceId & metadata)
50
-
51
- ```typescript
52
- const result = await memory.listThreads({
53
- filter: {
54
- resourceId: "user-123",
55
- metadata: { status: "active" },
56
- },
57
- page: 0,
58
- perPage: 10,
59
- });
60
- ```
61
-
62
- ## Parameters
63
-
64
- **filter?:** (`{ resourceId?: string; metadata?: Record<string, unknown> }`): Optional filter object. resourceId filters threads by resource ID. metadata filters threads by metadata key-value pairs (AND logic - all must match)
65
-
66
- **page?:** (`number`): Page number (0-indexed) to retrieve
67
-
68
- **perPage?:** (`number | false`): Maximum number of threads to return per page, or false to fetch all
69
-
70
- **orderBy?:** (`{ field: 'createdAt' | 'updatedAt', direction: 'ASC' | 'DESC' }`): Sort configuration with field and direction (defaults to { field: 'createdAt', direction: 'DESC' })
71
-
72
- ## Returns
73
-
74
- **result:** (`Promise<StorageListThreadsOutput>`): A promise that resolves to paginated thread results with metadata
75
-
76
- The return object contains:
77
-
78
- - `threads`: Array of thread objects
79
- - `total`: Total number of threads matching the filter
80
- - `page`: Current page number (same as the input `page` parameter)
81
- - `perPage`: Items per page (same as the input `perPage` parameter)
82
- - `hasMore`: Boolean indicating if more results are available
83
-
84
- ## Extended usage example
85
-
86
- ```typescript
87
- import { mastra } from "./mastra";
88
-
89
- const agent = mastra.getAgent("agent");
90
- const memory = await agent.getMemory();
91
-
92
- let currentPage = 0;
93
- const perPage = 25;
94
- let hasMorePages = true;
95
-
96
- // Fetch all active threads for a user, sorted by creation date
97
- while (hasMorePages) {
98
- const result = await memory?.listThreads({
99
- filter: {
100
- resourceId: "user-123",
101
- metadata: { status: "active" },
102
- },
103
- page: currentPage,
104
- perPage: perPage,
105
- orderBy: { field: "createdAt", direction: "ASC" },
106
- });
107
-
108
- if (!result) {
109
- console.log("No threads");
110
- break;
111
- }
112
-
113
- result.threads.forEach((thread) => {
114
- console.log(`Thread: ${thread.id}, Created: ${thread.createdAt}`);
115
- });
116
-
117
- hasMorePages = result.hasMore;
118
- currentPage++; // Move to next page
119
- }
120
- ```
121
-
122
- ## Metadata Filtering
123
-
124
- The metadata filter uses AND logic - all specified key-value pairs must match for a thread to be included in the results:
125
-
126
- ```typescript
127
- // This will only return threads where BOTH conditions are true:
128
- // - category === 'support'
129
- // - priority === 'high'
130
- await memory.listThreads({
131
- filter: {
132
- metadata: {
133
- category: "support",
134
- priority: "high",
135
- },
136
- },
137
- });
138
- ```
139
-
140
- ## Related
141
-
142
- - [Memory Class Reference](https://mastra.ai/reference/memory/memory-class)
143
- - [Getting Started with Memory](https://mastra.ai/docs/memory/overview)
144
- - [createThread](https://mastra.ai/reference/memory/createThread)
145
- - [getThreadById](https://mastra.ai/reference/memory/getThreadById)
@@ -1,147 +0,0 @@
1
- # Memory Class
2
-
3
- The `Memory` class provides a robust 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.
4
-
5
- ## Usage example
6
-
7
- ```typescript
8
- import { Memory } from "@mastra/memory";
9
- import { Agent } from "@mastra/core/agent";
10
-
11
- export const agent = new Agent({
12
- name: "test-agent",
13
- instructions: "You are an agent with memory.",
14
- model: "openai/gpt-5.1",
15
- memory: new Memory({
16
- options: {
17
- workingMemory: {
18
- enabled: true,
19
- },
20
- },
21
- }),
22
- });
23
- ```
24
-
25
- > 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.
26
-
27
- ## Constructor parameters
28
-
29
- **storage?:** (`MastraCompositeStore`): Storage implementation for persisting memory data. Defaults to \`new DefaultStorage({ config: { url: "file:memory.db" } })\` if not provided.
30
-
31
- **vector?:** (`MastraVector | false`): Vector store for semantic search capabilities. Set to \`false\` to disable vector operations.
32
-
33
- **embedder?:** (`EmbeddingModel<string> | EmbeddingModelV2<string>`): Embedder instance for vector embeddings. Required when semantic recall is enabled.
34
-
35
- **options?:** (`MemoryConfig`): Memory configuration options.
36
-
37
- ### Options parameters
38
-
39
- **lastMessages?:** (`number | false`): Number of most recent messages to include in context. Set to \`false\` to disable loading conversation history into context. Use \`Number.MAX\_SAFE\_INTEGER\` to retrieve all messages with no limit. To prevent saving new messages, use the \`readOnly\` option instead. (Default: `10`)
40
-
41
- **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. (Default: `false`)
42
-
43
- **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}. (Default: `false`)
44
-
45
- **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. (Default: `{ enabled: false, template: '# User Information\n- **First Name**:\n- **Last Name**:\n...' }`)
46
-
47
- **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]\(/reference/memory/observational-memory) for configuration details. (Default: `false`)
48
-
49
- **generateTitle?:** (`boolean | { model: DynamicArgument<MastraLanguageModel>; instructions?: DynamicArgument<string> }`): Controls automatic thread title generation from the user's first message. Can be a boolean or an object with custom model and instructions. (Default: `false`)
50
-
51
- ## Returns
52
-
53
- **memory:** (`Memory`): A new Memory instance with the specified configuration.
54
-
55
- ## Extended usage example
56
-
57
- ```typescript
58
- import { Memory } from "@mastra/memory";
59
- import { Agent } from "@mastra/core/agent";
60
- import { LibSQLStore, LibSQLVector } from "@mastra/libsql";
61
-
62
- export const agent = new Agent({
63
- name: "test-agent",
64
- instructions: "You are an agent with memory.",
65
- model: "openai/gpt-5.1",
66
- memory: new Memory({
67
- storage: new LibSQLStore({
68
- id: 'test-agent-storage',
69
- url: "file:./working-memory.db",
70
- }),
71
- vector: new LibSQLVector({
72
- id: 'test-agent-vector',
73
- url: "file:./vector-memory.db",
74
- }),
75
- options: {
76
- lastMessages: 10,
77
- semanticRecall: {
78
- topK: 3,
79
- messageRange: 2,
80
- scope: "resource",
81
- },
82
- workingMemory: {
83
- enabled: true,
84
- },
85
- generateTitle: true,
86
- },
87
- }),
88
- });
89
- ```
90
-
91
- ## PostgreSQL with index configuration
92
-
93
- ```typescript
94
- import { Memory } from "@mastra/memory";
95
- import { Agent } from "@mastra/core/agent";
96
- import { ModelRouterEmbeddingModel } from "@mastra/core/llm";
97
- import { PgStore, PgVector } from "@mastra/pg";
98
-
99
- export const agent = new Agent({
100
- name: "pg-agent",
101
- instructions: "You are an agent with optimized PostgreSQL memory.",
102
- model: "openai/gpt-5.1",
103
- memory: new Memory({
104
- storage: new PgStore({
105
- id: 'pg-agent-storage',
106
- connectionString: process.env.DATABASE_URL,
107
- }),
108
- vector: new PgVector({
109
- id: 'pg-agent-vector',
110
- connectionString: process.env.DATABASE_URL,
111
- }),
112
- embedder: new ModelRouterEmbeddingModel("openai/text-embedding-3-small"),
113
- options: {
114
- lastMessages: 20,
115
- semanticRecall: {
116
- topK: 5,
117
- messageRange: 3,
118
- scope: "resource",
119
- indexConfig: {
120
- type: "hnsw", // Use HNSW for better performance
121
- metric: "dotproduct", // Optimal for OpenAI embeddings
122
- m: 16, // Number of bi-directional links
123
- efConstruction: 64, // Construction-time candidate list size
124
- },
125
- },
126
- workingMemory: {
127
- enabled: true,
128
- },
129
- },
130
- }),
131
- });
132
- ```
133
-
134
- ### Related
135
-
136
- - [Getting Started with Memory](https://mastra.ai/docs/memory/overview)
137
- - [Semantic Recall](https://mastra.ai/docs/memory/semantic-recall)
138
- - [Working Memory](https://mastra.ai/docs/memory/working-memory)
139
- - [Observational Memory](https://mastra.ai/docs/memory/observational-memory)
140
- - [Memory Processors](https://mastra.ai/docs/memory/memory-processors)
141
- - [createThread](https://mastra.ai/reference/memory/createThread)
142
- - [recall](https://mastra.ai/reference/memory/recall)
143
- - [getThreadById](https://mastra.ai/reference/memory/getThreadById)
144
- - [listThreads](https://mastra.ai/reference/memory/listThreads)
145
- - [deleteMessages](https://mastra.ai/reference/memory/deleteMessages)
146
- - [cloneThread](https://mastra.ai/reference/memory/cloneThread)
147
- - [Clone Utility Methods](https://mastra.ai/reference/memory/clone-utilities)