@mastra/pg 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.
Files changed (147) hide show
  1. package/CHANGELOG.md +5579 -3
  2. package/LICENSE.md +15 -0
  3. package/README.md +72 -20
  4. package/dist/docs/SKILL.md +45 -0
  5. package/dist/docs/assets/SOURCE_MAP.json +6 -0
  6. package/dist/docs/references/docs-deployment-workers.md +137 -0
  7. package/dist/docs/references/docs-memory-semantic-recall.md +401 -0
  8. package/dist/docs/references/docs-memory-working-memory.md +431 -0
  9. package/dist/docs/references/docs-storage-overview.md +214 -0
  10. package/dist/docs/references/guides-rag-overview.md +74 -0
  11. package/dist/docs/references/guides-rag-retrieval.md +520 -0
  12. package/dist/docs/references/guides-rag-vector-databases.md +669 -0
  13. package/dist/docs/references/reference-memory-memory-class.md +148 -0
  14. package/dist/docs/references/reference-processors-message-history-processor.md +86 -0
  15. package/dist/docs/references/reference-processors-semantic-recall-processor.md +118 -0
  16. package/dist/docs/references/reference-processors-working-memory-processor.md +151 -0
  17. package/dist/docs/references/reference-rag-metadata-filters.md +218 -0
  18. package/dist/docs/references/reference-storage-composite.md +397 -0
  19. package/dist/docs/references/reference-storage-dynamodb.md +284 -0
  20. package/dist/docs/references/reference-storage-postgresql.md +531 -0
  21. package/dist/docs/references/reference-storage-retention.md +248 -0
  22. package/dist/docs/references/reference-tools-vector-query-tool.md +495 -0
  23. package/dist/docs/references/reference-vectors-pg.md +430 -0
  24. package/dist/index.cjs +20692 -4338
  25. package/dist/index.cjs.map +1 -1
  26. package/dist/index.d.ts +1 -1
  27. package/dist/index.d.ts.map +1 -1
  28. package/dist/index.js +20640 -4310
  29. package/dist/index.js.map +1 -1
  30. package/dist/shared/config.d.ts +129 -31
  31. package/dist/shared/config.d.ts.map +1 -1
  32. package/dist/shared/pool-config.d.ts +32 -0
  33. package/dist/shared/pool-config.d.ts.map +1 -0
  34. package/dist/storage/client.d.ts +165 -0
  35. package/dist/storage/client.d.ts.map +1 -0
  36. package/dist/storage/db/constraint-utils.d.ts +16 -0
  37. package/dist/storage/db/constraint-utils.d.ts.map +1 -0
  38. package/dist/storage/db/index.d.ts +364 -0
  39. package/dist/storage/db/index.d.ts.map +1 -0
  40. package/dist/storage/db/pg-errors.d.ts +24 -0
  41. package/dist/storage/db/pg-errors.d.ts.map +1 -0
  42. package/dist/storage/db/schema-snapshot.d.ts +71 -0
  43. package/dist/storage/db/schema-snapshot.d.ts.map +1 -0
  44. package/dist/storage/domains/agents/index.d.ts +51 -0
  45. package/dist/storage/domains/agents/index.d.ts.map +1 -0
  46. package/dist/storage/domains/background-tasks/index.d.ts +43 -0
  47. package/dist/storage/domains/background-tasks/index.d.ts.map +1 -0
  48. package/dist/storage/domains/blobs/index.d.ts +18 -0
  49. package/dist/storage/domains/blobs/index.d.ts.map +1 -0
  50. package/dist/storage/domains/channels/index.d.ts +25 -0
  51. package/dist/storage/domains/channels/index.d.ts.map +1 -0
  52. package/dist/storage/domains/datasets/index.d.ts +47 -0
  53. package/dist/storage/domains/datasets/index.d.ts.map +1 -0
  54. package/dist/storage/domains/experiments/index.d.ts +70 -0
  55. package/dist/storage/domains/experiments/index.d.ts.map +1 -0
  56. package/dist/storage/domains/favorites/index.d.ts +19 -0
  57. package/dist/storage/domains/favorites/index.d.ts.map +1 -0
  58. package/dist/storage/domains/mcp-clients/index.d.ts +34 -0
  59. package/dist/storage/domains/mcp-clients/index.d.ts.map +1 -0
  60. package/dist/storage/domains/mcp-servers/index.d.ts +34 -0
  61. package/dist/storage/domains/mcp-servers/index.d.ts.map +1 -0
  62. package/dist/storage/domains/memory/index.d.ts +130 -56
  63. package/dist/storage/domains/memory/index.d.ts.map +1 -1
  64. package/dist/storage/domains/notifications/index.d.ts +43 -0
  65. package/dist/storage/domains/notifications/index.d.ts.map +1 -0
  66. package/dist/storage/domains/observability/index.d.ts +87 -39
  67. package/dist/storage/domains/observability/index.d.ts.map +1 -1
  68. package/dist/storage/domains/observability/v-next/ddl.d.ts +55 -0
  69. package/dist/storage/domains/observability/v-next/ddl.d.ts.map +1 -0
  70. package/dist/storage/domains/observability/v-next/discovery.d.ts +52 -0
  71. package/dist/storage/domains/observability/v-next/discovery.d.ts.map +1 -0
  72. package/dist/storage/domains/observability/v-next/feedback.d.ts +17 -0
  73. package/dist/storage/domains/observability/v-next/feedback.d.ts.map +1 -0
  74. package/dist/storage/domains/observability/v-next/filters.d.ts +29 -0
  75. package/dist/storage/domains/observability/v-next/filters.d.ts.map +1 -0
  76. package/dist/storage/domains/observability/v-next/helpers.d.ts +33 -0
  77. package/dist/storage/domains/observability/v-next/helpers.d.ts.map +1 -0
  78. package/dist/storage/domains/observability/v-next/index.d.ts +120 -0
  79. package/dist/storage/domains/observability/v-next/index.d.ts.map +1 -0
  80. package/dist/storage/domains/observability/v-next/listing.d.ts +53 -0
  81. package/dist/storage/domains/observability/v-next/listing.d.ts.map +1 -0
  82. package/dist/storage/domains/observability/v-next/logs.d.ts +8 -0
  83. package/dist/storage/domains/observability/v-next/logs.d.ts.map +1 -0
  84. package/dist/storage/domains/observability/v-next/metrics.d.ts +19 -0
  85. package/dist/storage/domains/observability/v-next/metrics.d.ts.map +1 -0
  86. package/dist/storage/domains/observability/v-next/olap.d.ts +93 -0
  87. package/dist/storage/domains/observability/v-next/olap.d.ts.map +1 -0
  88. package/dist/storage/domains/observability/v-next/partitioning.d.ts +76 -0
  89. package/dist/storage/domains/observability/v-next/partitioning.d.ts.map +1 -0
  90. package/dist/storage/domains/observability/v-next/pg-errors.d.ts +5 -0
  91. package/dist/storage/domains/observability/v-next/pg-errors.d.ts.map +1 -0
  92. package/dist/storage/domains/observability/v-next/polling.d.ts +28 -0
  93. package/dist/storage/domains/observability/v-next/polling.d.ts.map +1 -0
  94. package/dist/storage/domains/observability/v-next/retention.d.ts +64 -0
  95. package/dist/storage/domains/observability/v-next/retention.d.ts.map +1 -0
  96. package/dist/storage/domains/observability/v-next/scores.d.ts +17 -0
  97. package/dist/storage/domains/observability/v-next/scores.d.ts.map +1 -0
  98. package/dist/storage/domains/observability/v-next/signal-schema.d.ts +776 -0
  99. package/dist/storage/domains/observability/v-next/signal-schema.d.ts.map +1 -0
  100. package/dist/storage/domains/observability/v-next/sql.d.ts +31 -0
  101. package/dist/storage/domains/observability/v-next/sql.d.ts.map +1 -0
  102. package/dist/storage/domains/observability/v-next/traces.d.ts +16 -0
  103. package/dist/storage/domains/observability/v-next/traces.d.ts.map +1 -0
  104. package/dist/storage/domains/observability/v-next/tracing.d.ts +19 -0
  105. package/dist/storage/domains/observability/v-next/tracing.d.ts.map +1 -0
  106. package/dist/storage/domains/prompt-blocks/index.d.ts +42 -0
  107. package/dist/storage/domains/prompt-blocks/index.d.ts.map +1 -0
  108. package/dist/storage/domains/schedules/index.d.ts +52 -0
  109. package/dist/storage/domains/schedules/index.d.ts.map +1 -0
  110. package/dist/storage/domains/scorer-definitions/index.d.ts +42 -0
  111. package/dist/storage/domains/scorer-definitions/index.d.ts.map +1 -0
  112. package/dist/storage/domains/scores/index.d.ts +61 -33
  113. package/dist/storage/domains/scores/index.d.ts.map +1 -1
  114. package/dist/storage/domains/skills/index.d.ts +34 -0
  115. package/dist/storage/domains/skills/index.d.ts.map +1 -0
  116. package/dist/storage/domains/tool-provider-connections/index.d.ts +20 -0
  117. package/dist/storage/domains/tool-provider-connections/index.d.ts.map +1 -0
  118. package/dist/storage/domains/utils.d.ts +38 -5
  119. package/dist/storage/domains/utils.d.ts.map +1 -1
  120. package/dist/storage/domains/workflow-definitions/index.d.ts +20 -0
  121. package/dist/storage/domains/workflow-definitions/index.d.ts.map +1 -0
  122. package/dist/storage/domains/workflows/index.d.ts +70 -30
  123. package/dist/storage/domains/workflows/index.d.ts.map +1 -1
  124. package/dist/storage/domains/workspaces/index.d.ts +34 -0
  125. package/dist/storage/domains/workspaces/index.d.ts.map +1 -0
  126. package/dist/storage/factory-storage.d.ts +35 -0
  127. package/dist/storage/factory-storage.d.ts.map +1 -0
  128. package/dist/storage/index.d.ts +159 -253
  129. package/dist/storage/index.d.ts.map +1 -1
  130. package/dist/storage/performance-indexes/performance-test.d.ts +3 -1
  131. package/dist/storage/performance-indexes/performance-test.d.ts.map +1 -1
  132. package/dist/storage/retention.d.ts +76 -0
  133. package/dist/storage/retention.d.ts.map +1 -0
  134. package/dist/storage/test-utils.d.ts.map +1 -1
  135. package/dist/vector/index.d.ts +91 -11
  136. package/dist/vector/index.d.ts.map +1 -1
  137. package/dist/vector/sql-builder.d.ts +4 -0
  138. package/dist/vector/sql-builder.d.ts.map +1 -1
  139. package/dist/vector/types.d.ts +40 -0
  140. package/dist/vector/types.d.ts.map +1 -1
  141. package/package.json +31 -18
  142. package/dist/storage/domains/legacy-evals/index.d.ts +0 -20
  143. package/dist/storage/domains/legacy-evals/index.d.ts.map +0 -1
  144. package/dist/storage/domains/operations/index.d.ts +0 -118
  145. package/dist/storage/domains/operations/index.d.ts.map +0 -1
  146. package/dist/storage/domains/traces/index.d.ts +0 -23
  147. package/dist/storage/domains/traces/index.d.ts.map +0 -1
@@ -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)
@@ -0,0 +1,86 @@
1
+ > Discover all available pages from the documentation index: https://mastra.ai/llms.txt
2
+
3
+ # MessageHistory
4
+
5
+ The `MessageHistory` is a **hybrid processor** that handles both retrieval and persistence of message history. On input, it fetches historical messages from storage and prepends them to the conversation. On output, it persists new messages to storage.
6
+
7
+ ## Usage example
8
+
9
+ ```typescript
10
+ import { MessageHistory } from '@mastra/core/processors'
11
+
12
+ const processor = new MessageHistory({
13
+ storage: memoryStorage,
14
+ lastMessages: 50,
15
+ })
16
+ ```
17
+
18
+ ## Constructor parameters
19
+
20
+ **options** (`MessageHistoryOptions`): Configuration options for the message history processor
21
+
22
+ **options.storage** (`MemoryStorage`): Storage instance for retrieving and persisting messages
23
+
24
+ **options.lastMessages** (`number`): Maximum number of historical messages to retrieve. If not specified, retrieves all messages
25
+
26
+ ## Returns
27
+
28
+ **id** (`string`): Processor identifier set to 'message-history'
29
+
30
+ **name** (`string`): Processor display name set to 'MessageHistory'
31
+
32
+ **processInput** (`(args: { messages: MastraDBMessage[]; messageList: MessageList; abort: (reason?: string) => never; tracingContext?: TracingContext; requestContext?: RequestContext }) => Promise<MessageList | MastraDBMessage[]>`): Fetches historical messages from storage and adds them to the message list
33
+
34
+ **processOutputResult** (`(args: { messages: MastraDBMessage[]; messageList: MessageList; abort: (reason?: string) => never; tracingContext?: TracingContext; requestContext?: RequestContext }) => Promise<MessageList>`): Persists new messages (user input and assistant response) to storage, excluding system messages
35
+
36
+ ## Extended usage example
37
+
38
+ ```typescript
39
+ import { Agent } from '@mastra/core/agent'
40
+ import { MessageHistory } from '@mastra/core/processors'
41
+ import { PostgresStorage } from '@mastra/pg'
42
+
43
+ const storage = new PostgresStorage({
44
+ connectionString: process.env.DATABASE_URL,
45
+ })
46
+
47
+ export const agent = new Agent({
48
+ id: 'memory-agent',
49
+ name: 'memory-agent',
50
+ instructions: 'You are a helpful assistant with conversation memory',
51
+ model: 'openai/gpt-5.6-sol',
52
+ inputProcessors: [
53
+ new MessageHistory({
54
+ storage,
55
+ lastMessages: 100,
56
+ }),
57
+ ],
58
+ outputProcessors: [
59
+ new MessageHistory({
60
+ storage,
61
+ }),
62
+ ],
63
+ })
64
+ ```
65
+
66
+ ## Behavior
67
+
68
+ ### Input processing
69
+
70
+ 1. Retrieves `threadId` from the request context
71
+ 2. Fetches historical messages from storage (ordered by creation date, descending)
72
+ 3. Filters out system messages (they shouldn't be stored in the database)
73
+ 4. Merges historical messages with incoming messages, avoiding duplicates by ID
74
+ 5. Adds historical messages with `source: 'memory'` tag
75
+
76
+ ### Output processing
77
+
78
+ 1. Retrieves `threadId` from the request context
79
+ 2. Skips persistence if `readOnly` is set in memory config
80
+ 3. Filters out incomplete tool calls from messages
81
+ 4. Persists new user input and assistant response messages to storage
82
+ 5. Updates the thread's `updatedAt` timestamp
83
+
84
+ ## Related
85
+
86
+ - [Guardrails](https://mastra.ai/docs/agents/guardrails)
@@ -0,0 +1,118 @@
1
+ > Discover all available pages from the documentation index: https://mastra.ai/llms.txt
2
+
3
+ # SemanticRecall
4
+
5
+ The `SemanticRecall` is a **hybrid processor** that enables semantic search over conversation history using vector embeddings. On input, it performs semantic search to find relevant historical messages. On output, it creates embeddings for new messages to enable future semantic retrieval.
6
+
7
+ ## Usage example
8
+
9
+ ```typescript
10
+ import { SemanticRecall } from '@mastra/core/processors'
11
+ import { openai } from '@ai-sdk/openai'
12
+
13
+ const processor = new SemanticRecall({
14
+ storage: memoryStorage,
15
+ vector: vectorStore,
16
+ embedder: openai.embedding('text-embedding-3-small'),
17
+ topK: 5,
18
+ messageRange: 2,
19
+ scope: 'resource',
20
+ })
21
+ ```
22
+
23
+ ## Constructor parameters
24
+
25
+ **options** (`SemanticRecallOptions`): Configuration options for the semantic recall processor
26
+
27
+ **options.storage** (`MemoryStorage`): Storage instance for retrieving messages
28
+
29
+ **options.vector** (`MastraVector`): Vector store for semantic search
30
+
31
+ **options.embedder** (`MastraEmbeddingModel<string>`): Embedder for generating query embeddings
32
+
33
+ **options.topK** (`number`): Number of most similar messages to retrieve
34
+
35
+ **options.messageRange** (`number | { before: number; after: number }`): Number of context messages to include before/after each match. Can be a single number (same for both) or an object with separate values
36
+
37
+ **options.scope** (`'thread' | 'resource'`): Scope of semantic search. 'thread' searches within the current thread only. 'resource' searches across all threads for the resource
38
+
39
+ **options.threshold** (`number`): Minimum similarity score threshold (0-1). Messages below this threshold are filtered out
40
+
41
+ **options.indexName** (`string`): Index name for the vector store. If not provided, auto-generated based on embedder model
42
+
43
+ **options.logger** (`IMastraLogger`): Optional logger instance for structured logging
44
+
45
+ ## Returns
46
+
47
+ **id** (`string`): Processor identifier set to 'semantic-recall'
48
+
49
+ **name** (`string`): Processor display name set to 'SemanticRecall'
50
+
51
+ **processInput** (`(args: { messages: MastraDBMessage[]; messageList: MessageList; abort: (reason?: string) => never; tracingContext?: TracingContext; requestContext?: RequestContext }) => Promise<MessageList | MastraDBMessage[]>`): Performs semantic search on historical messages and adds relevant context to the message list
52
+
53
+ **processOutputResult** (`(args: { messages: MastraDBMessage[]; messageList?: MessageList; abort: (reason?: string) => never; tracingContext?: TracingContext; requestContext?: RequestContext }) => Promise<MessageList | MastraDBMessage[]>`): Creates embeddings for new messages to enable future semantic search
54
+
55
+ ## Extended usage example
56
+
57
+ ```typescript
58
+ import { Agent } from '@mastra/core/agent'
59
+ import { SemanticRecall, MessageHistory } from '@mastra/core/processors'
60
+ import { PostgresStorage } from '@mastra/pg'
61
+ import { PgVector } from '@mastra/pg'
62
+ import { openai } from '@ai-sdk/openai'
63
+
64
+ const storage = new PostgresStorage({
65
+ id: 'pg-storage',
66
+ connectionString: process.env.DATABASE_URL,
67
+ })
68
+
69
+ const vector = new PgVector({
70
+ id: 'pg-vector',
71
+ connectionString: process.env.DATABASE_URL,
72
+ })
73
+
74
+ const semanticRecall = new SemanticRecall({
75
+ storage,
76
+ vector,
77
+ embedder: openai.embedding('text-embedding-3-small'),
78
+ topK: 5,
79
+ messageRange: { before: 2, after: 1 },
80
+ scope: 'resource',
81
+ threshold: 0.7,
82
+ })
83
+
84
+ export const agent = new Agent({
85
+ id: 'semantic-memory-agent',
86
+ name: 'semantic-memory-agent',
87
+ instructions: 'You are a helpful assistant with semantic memory recall',
88
+ model: 'openai/gpt-5.6-sol',
89
+ inputProcessors: [semanticRecall, new MessageHistory({ storage, lastMessages: 50 })],
90
+ outputProcessors: [semanticRecall, new MessageHistory({ storage })],
91
+ })
92
+ ```
93
+
94
+ ## Behavior
95
+
96
+ ### Input processing
97
+
98
+ 1. Extracts the user query from the last user message
99
+ 2. Generates embeddings for the query
100
+ 3. Performs vector search to find semantically similar messages
101
+ 4. Retrieves matched messages along with surrounding context (based on `messageRange`)
102
+ 5. For `scope: 'resource'`, formats cross-thread messages as a system message with timestamps
103
+ 6. Adds recalled messages with `source: 'memory'` tag
104
+
105
+ ### Output processing
106
+
107
+ 1. Extracts text content from new user and assistant messages
108
+ 2. Generates embeddings for each message
109
+ 3. Stores embeddings in the vector store with metadata (message ID, thread ID, resource ID, role, content, timestamp)
110
+ 4. Uses LRU caching for embeddings to avoid redundant API calls
111
+
112
+ ### Cross-thread recall
113
+
114
+ When `scope` is set to `'resource'`, the processor can recall messages from other threads. These cross-thread messages are formatted as a system message with timestamps and conversation labels to provide context about when and where the conversation occurred.
115
+
116
+ ## Related
117
+
118
+ - [Guardrails](https://mastra.ai/docs/agents/guardrails)
@@ -0,0 +1,151 @@
1
+ > Discover all available pages from the documentation index: https://mastra.ai/llms.txt
2
+
3
+ # WorkingMemory
4
+
5
+ The `WorkingMemory` is an **input processor** that injects working memory data as a system message. It retrieves persistent information from storage and formats it as instructions for the LLM, enabling the agent to maintain context about users across conversations.
6
+
7
+ ## Usage example
8
+
9
+ ```typescript
10
+ import { WorkingMemory } from '@mastra/core/processors'
11
+
12
+ const processor = new WorkingMemory({
13
+ storage: memoryStorage,
14
+ scope: 'resource',
15
+ template: {
16
+ format: 'markdown',
17
+ content: `# User Profile
18
+ - **Name**:
19
+ - **Preferences**:
20
+ - **Goals**:
21
+ `,
22
+ },
23
+ })
24
+ ```
25
+
26
+ ## Constructor parameters
27
+
28
+ **options** (`Options`): Configuration options for the working memory processor
29
+
30
+ **options.storage** (`MemoryStorage`): Storage instance for retrieving working memory data
31
+
32
+ **options.template** (`WorkingMemoryTemplate`): Template defining the format and structure of working memory
33
+
34
+ **options.template.format** (`'markdown' | 'json'`): Format of the working memory content
35
+
36
+ **options.template.content** (`string`): Template content defining the structure of working memory data
37
+
38
+ **options.scope** (`'thread' | 'resource'`): Scope of working memory. 'thread' scopes to current thread, 'resource' shares across all threads for the resource
39
+
40
+ **options.useVNext** (`boolean`): Use the next-generation instruction format with improved guidelines
41
+
42
+ **options.readOnly** (`boolean`): When true, working memory is provided as read-only context. The data is injected into the conversation but without the updateWorkingMemory tool or update instructions. Useful for agents that should reference working memory without modifying it.
43
+
44
+ **options.templateProvider** (`{ getWorkingMemoryTemplate(args: { memoryConfig?: MemoryConfig }): Promise<WorkingMemoryTemplate | null> }`): Dynamic template provider for runtime template resolution
45
+
46
+ **options.logger** (`IMastraLogger`): Optional logger instance for structured logging
47
+
48
+ ## Returns
49
+
50
+ **id** (`string`): Processor identifier set to 'working-memory'
51
+
52
+ **name** (`string`): Processor display name set to 'WorkingMemory'
53
+
54
+ **defaultWorkingMemoryTemplate** (`string`): The default markdown template used when no custom template is provided
55
+
56
+ **processInput** (`(args: { messages: MastraDBMessage[]; messageList: MessageList; abort: (reason?: string) => never; requestContext?: RequestContext }) => Promise<MessageList | MastraDBMessage[]>`): Retrieves working memory and adds it as a system message to the message list
57
+
58
+ ## Extended usage example
59
+
60
+ ```typescript
61
+ import { Agent } from '@mastra/core/agent'
62
+ import { WorkingMemory, MessageHistory } from '@mastra/core/processors'
63
+ import { PostgresStorage } from '@mastra/pg'
64
+
65
+ const storage = new PostgresStorage({
66
+ connectionString: process.env.DATABASE_URL,
67
+ })
68
+
69
+ export const agent = new Agent({
70
+ id: 'personalized-agent',
71
+ name: 'personalized-agent',
72
+ instructions: 'You are a helpful assistant that remembers user preferences',
73
+ model: 'openai/gpt-5.6-sol',
74
+ inputProcessors: [
75
+ new WorkingMemory({
76
+ storage,
77
+ scope: 'resource',
78
+ template: {
79
+ format: 'markdown',
80
+ content: `# User Information
81
+ - **Name**:
82
+ - **Location**:
83
+ - **Preferences**:
84
+ - **Communication Style**:
85
+ - **Current Projects**:
86
+ `,
87
+ },
88
+ }),
89
+ new MessageHistory({ storage, lastMessages: 50 }),
90
+ ],
91
+ outputProcessors: [new MessageHistory({ storage })],
92
+ })
93
+ ```
94
+
95
+ ## JSON format example
96
+
97
+ ```typescript
98
+ import { WorkingMemory } from '@mastra/core/processors'
99
+
100
+ const processor = new WorkingMemory({
101
+ storage: memoryStorage,
102
+ scope: 'resource',
103
+ template: {
104
+ format: 'json',
105
+ content: JSON.stringify({
106
+ user: {
107
+ name: { type: 'string' },
108
+ preferences: { type: 'object' },
109
+ goals: { type: 'array' },
110
+ },
111
+ }),
112
+ },
113
+ })
114
+ ```
115
+
116
+ ## Behavior
117
+
118
+ ### Input processing
119
+
120
+ 1. Retrieves `threadId` and `resourceId` from the request context
121
+
122
+ 2. Based on scope, fetches working memory from either:
123
+
124
+ - Thread metadata (`scope: 'thread'`)
125
+ - Resource record (`scope: 'resource'`)
126
+
127
+ 3. Resolves the template (from provider, options, or default)
128
+
129
+ 4. Generates system instructions based on mode:
130
+
131
+ - **Normal mode**: Includes guidelines for storing/updating information, template structure, and current data
132
+ - **Read-only mode** (`readOnly: true`): Includes only the current data as context without update instructions
133
+
134
+ 5. Adds the instruction as a system message with `source: 'memory'` tag
135
+
136
+ ### Working memory updates
137
+
138
+ Working memory updates happen through the `updateWorkingMemory` tool provided by the Memory class, not through this processor. The processor only handles injecting the current working memory state into conversations.
139
+
140
+ ### Default template
141
+
142
+ If no template is provided, the processor uses a default markdown template with fields for:
143
+
144
+ - First Name, Last Name
145
+ - Location, Occupation
146
+ - Interests, Goals
147
+ - Events, Facts, Projects
148
+
149
+ ## Related
150
+
151
+ - [Guardrails](https://mastra.ai/docs/agents/guardrails)