@mastra/memory 0.0.0-error-handler-fix-20251020202607 → 0.0.0-execa-dynamic-import-20260304221256
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 +2181 -3
- package/LICENSE.md +15 -0
- package/dist/_types/@internal_ai-sdk-v4/dist/index.d.ts +7562 -0
- package/dist/chunk-23EXJLET.cjs +84 -0
- package/dist/chunk-23EXJLET.cjs.map +1 -0
- package/dist/chunk-33ZIVK5L.js +5220 -0
- package/dist/chunk-33ZIVK5L.js.map +1 -0
- package/dist/chunk-BSDWQEU3.js +79 -0
- package/dist/chunk-BSDWQEU3.js.map +1 -0
- package/dist/chunk-DGUM43GV.js +10 -0
- package/dist/chunk-DGUM43GV.js.map +1 -0
- package/dist/chunk-EQ4M72KU.js +439 -0
- package/dist/chunk-EQ4M72KU.js.map +1 -0
- package/dist/chunk-HJYHDIOC.js +250 -0
- package/dist/chunk-HJYHDIOC.js.map +1 -0
- package/dist/chunk-IDRQZVB4.cjs +84 -0
- package/dist/chunk-IDRQZVB4.cjs.map +1 -0
- package/dist/chunk-JEQ2X3Z6.cjs +12 -0
- package/dist/chunk-JEQ2X3Z6.cjs.map +1 -0
- package/dist/chunk-LIBOSOHM.cjs +252 -0
- package/dist/chunk-LIBOSOHM.cjs.map +1 -0
- package/dist/chunk-Q7VDGOMJ.cjs +5240 -0
- package/dist/chunk-Q7VDGOMJ.cjs.map +1 -0
- package/dist/chunk-RC6RZVYE.js +79 -0
- package/dist/chunk-RC6RZVYE.js.map +1 -0
- package/dist/chunk-ZD3BKU5O.cjs +441 -0
- package/dist/chunk-ZD3BKU5O.cjs.map +1 -0
- package/dist/docs/SKILL.md +55 -0
- package/dist/docs/assets/SOURCE_MAP.json +103 -0
- package/dist/docs/references/docs-agents-agent-approval.md +588 -0
- package/dist/docs/references/docs-agents-agent-memory.md +209 -0
- package/dist/docs/references/docs-agents-network-approval.md +275 -0
- package/dist/docs/references/docs-agents-networks.md +299 -0
- package/dist/docs/references/docs-agents-supervisor-agents.md +304 -0
- package/dist/docs/references/docs-memory-memory-processors.md +314 -0
- package/dist/docs/references/docs-memory-message-history.md +260 -0
- package/dist/docs/references/docs-memory-observational-memory.md +255 -0
- package/dist/docs/references/docs-memory-overview.md +45 -0
- package/dist/docs/references/docs-memory-semantic-recall.md +288 -0
- package/dist/docs/references/docs-memory-storage.md +261 -0
- package/dist/docs/references/docs-memory-working-memory.md +400 -0
- package/dist/docs/references/reference-core-getMemory.md +50 -0
- package/dist/docs/references/reference-core-listMemory.md +56 -0
- package/dist/docs/references/reference-memory-clone-utilities.md +199 -0
- package/dist/docs/references/reference-memory-cloneThread.md +142 -0
- package/dist/docs/references/reference-memory-createThread.md +68 -0
- package/dist/docs/references/reference-memory-getThreadById.md +24 -0
- package/dist/docs/references/reference-memory-listThreads.md +145 -0
- package/dist/docs/references/reference-memory-memory-class.md +147 -0
- package/dist/docs/references/reference-memory-observational-memory.md +577 -0
- package/dist/docs/references/reference-processors-token-limiter-processor.md +115 -0
- package/dist/docs/references/reference-storage-dynamodb.md +282 -0
- package/dist/docs/references/reference-storage-libsql.md +135 -0
- package/dist/docs/references/reference-storage-mongodb.md +262 -0
- package/dist/docs/references/reference-storage-postgresql.md +526 -0
- package/dist/docs/references/reference-storage-upstash.md +160 -0
- package/dist/docs/references/reference-vectors-libsql.md +305 -0
- package/dist/docs/references/reference-vectors-mongodb.md +295 -0
- package/dist/docs/references/reference-vectors-pg.md +408 -0
- package/dist/docs/references/reference-vectors-upstash.md +294 -0
- package/dist/index.cjs +16008 -291
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +252 -49
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +15970 -295
- package/dist/index.js.map +1 -1
- package/dist/observational-memory-3NJCJNCQ.cjs +64 -0
- package/dist/observational-memory-3NJCJNCQ.cjs.map +1 -0
- package/dist/observational-memory-SKDDNPFW.js +3 -0
- package/dist/observational-memory-SKDDNPFW.js.map +1 -0
- package/dist/processors/index.cjs +57 -158
- package/dist/processors/index.cjs.map +1 -1
- package/dist/processors/index.d.ts +1 -2
- package/dist/processors/index.d.ts.map +1 -1
- package/dist/processors/index.js +1 -156
- package/dist/processors/index.js.map +1 -1
- package/dist/processors/observational-memory/date-utils.d.ts +35 -0
- package/dist/processors/observational-memory/date-utils.d.ts.map +1 -0
- package/dist/processors/observational-memory/index.d.ts +18 -0
- package/dist/processors/observational-memory/index.d.ts.map +1 -0
- package/dist/processors/observational-memory/markers.d.ts +94 -0
- package/dist/processors/observational-memory/markers.d.ts.map +1 -0
- package/dist/processors/observational-memory/observational-memory.d.ts +842 -0
- package/dist/processors/observational-memory/observational-memory.d.ts.map +1 -0
- package/dist/processors/observational-memory/observer-agent.d.ts +140 -0
- package/dist/processors/observational-memory/observer-agent.d.ts.map +1 -0
- package/dist/processors/observational-memory/operation-registry.d.ts +14 -0
- package/dist/processors/observational-memory/operation-registry.d.ts.map +1 -0
- package/dist/processors/observational-memory/reflector-agent.d.ts +55 -0
- package/dist/processors/observational-memory/reflector-agent.d.ts.map +1 -0
- package/dist/processors/observational-memory/thresholds.d.ts +52 -0
- package/dist/processors/observational-memory/thresholds.d.ts.map +1 -0
- package/dist/processors/observational-memory/token-counter.d.ts +33 -0
- package/dist/processors/observational-memory/token-counter.d.ts.map +1 -0
- package/dist/processors/observational-memory/types.d.ts +539 -0
- package/dist/processors/observational-memory/types.d.ts.map +1 -0
- package/dist/token-6GSAFR2W-ABXTQD64.js +61 -0
- package/dist/token-6GSAFR2W-ABXTQD64.js.map +1 -0
- package/dist/token-6GSAFR2W-TW2P7HCS.cjs +63 -0
- package/dist/token-6GSAFR2W-TW2P7HCS.cjs.map +1 -0
- package/dist/token-APYSY3BW-2DN6RAUY.js +61 -0
- package/dist/token-APYSY3BW-2DN6RAUY.js.map +1 -0
- package/dist/token-APYSY3BW-ZQ7TMBY7.cjs +63 -0
- package/dist/token-APYSY3BW-ZQ7TMBY7.cjs.map +1 -0
- package/dist/token-util-NEHG7TUY-GYFEVMWP.cjs +10 -0
- package/dist/token-util-NEHG7TUY-GYFEVMWP.cjs.map +1 -0
- package/dist/token-util-NEHG7TUY-XQP3QSPX.js +8 -0
- package/dist/token-util-NEHG7TUY-XQP3QSPX.js.map +1 -0
- package/dist/token-util-RMHT2CPJ-6TGPE335.cjs +10 -0
- package/dist/token-util-RMHT2CPJ-6TGPE335.cjs.map +1 -0
- package/dist/token-util-RMHT2CPJ-RJEA3FAN.js +8 -0
- package/dist/token-util-RMHT2CPJ-RJEA3FAN.js.map +1 -0
- package/dist/tools/working-memory.d.ts +17 -24
- package/dist/tools/working-memory.d.ts.map +1 -1
- package/package.json +26 -25
- package/dist/processors/token-limiter.d.ts +0 -32
- package/dist/processors/token-limiter.d.ts.map +0 -1
- package/dist/processors/tool-call-filter.d.ts +0 -20
- package/dist/processors/tool-call-filter.d.ts.map +0 -1
|
@@ -0,0 +1,842 @@
|
|
|
1
|
+
import type { AgentConfig, MastraDBMessage, MessageList } from '@mastra/core/agent';
|
|
2
|
+
import type { Processor, ProcessInputStepArgs, ProcessOutputResultArgs } from '@mastra/core/processors';
|
|
3
|
+
import type { RequestContext } from '@mastra/core/request-context';
|
|
4
|
+
import type { MemoryStorage, ObservationalMemoryRecord } from '@mastra/core/storage';
|
|
5
|
+
import { TokenCounter } from './token-counter.js';
|
|
6
|
+
import type { ObservationConfig, ReflectionConfig, ThresholdRange, ModelSettings, ProviderOptions } from './types.js';
|
|
7
|
+
/**
|
|
8
|
+
* Debug event emitted when observation-related events occur.
|
|
9
|
+
* Useful for understanding what the Observer is doing.
|
|
10
|
+
*/
|
|
11
|
+
export interface ObservationDebugEvent {
|
|
12
|
+
type: 'observation_triggered' | 'observation_complete' | 'reflection_triggered' | 'reflection_complete' | 'tokens_accumulated' | 'step_progress';
|
|
13
|
+
timestamp: Date;
|
|
14
|
+
threadId: string;
|
|
15
|
+
resourceId: string;
|
|
16
|
+
/** Messages that were sent to the Observer */
|
|
17
|
+
messages?: Array<{
|
|
18
|
+
role: string;
|
|
19
|
+
content: string;
|
|
20
|
+
}>;
|
|
21
|
+
/** Token counts */
|
|
22
|
+
pendingTokens?: number;
|
|
23
|
+
sessionTokens?: number;
|
|
24
|
+
totalPendingTokens?: number;
|
|
25
|
+
threshold?: number;
|
|
26
|
+
/** Input token count (for reflection events) */
|
|
27
|
+
inputTokens?: number;
|
|
28
|
+
/** Number of active observations (for reflection events) */
|
|
29
|
+
activeObservationsLength?: number;
|
|
30
|
+
/** Output token count after reflection */
|
|
31
|
+
outputTokens?: number;
|
|
32
|
+
/** The observations that were generated */
|
|
33
|
+
observations?: string;
|
|
34
|
+
/** Previous observations (before this event) */
|
|
35
|
+
previousObservations?: string;
|
|
36
|
+
/** Observer's raw output */
|
|
37
|
+
rawObserverOutput?: string;
|
|
38
|
+
/** LLM usage from Observer/Reflector calls */
|
|
39
|
+
usage?: {
|
|
40
|
+
inputTokens?: number;
|
|
41
|
+
outputTokens?: number;
|
|
42
|
+
totalTokens?: number;
|
|
43
|
+
};
|
|
44
|
+
/** Step progress fields (for step_progress events) */
|
|
45
|
+
stepNumber?: number;
|
|
46
|
+
finishReason?: string;
|
|
47
|
+
thresholdPercent?: number;
|
|
48
|
+
willSave?: boolean;
|
|
49
|
+
willObserve?: boolean;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Configuration for ObservationalMemory
|
|
53
|
+
*/
|
|
54
|
+
export interface ObservationalMemoryConfig {
|
|
55
|
+
/**
|
|
56
|
+
* Storage adapter for persisting observations.
|
|
57
|
+
* Must be a MemoryStorage instance (from MastraStorage.stores.memory).
|
|
58
|
+
*/
|
|
59
|
+
storage: MemoryStorage;
|
|
60
|
+
/**
|
|
61
|
+
* Model for both Observer and Reflector agents.
|
|
62
|
+
* Sets the model for both agents at once. Cannot be used together with
|
|
63
|
+
* `observation.model` or `reflection.model` — an error will be thrown.
|
|
64
|
+
*
|
|
65
|
+
* @default 'google/gemini-2.5-flash'
|
|
66
|
+
*/
|
|
67
|
+
model?: AgentConfig['model'];
|
|
68
|
+
/**
|
|
69
|
+
* Observation step configuration.
|
|
70
|
+
*/
|
|
71
|
+
observation?: ObservationConfig;
|
|
72
|
+
/**
|
|
73
|
+
* Reflection step configuration.
|
|
74
|
+
*/
|
|
75
|
+
reflection?: ReflectionConfig;
|
|
76
|
+
/**
|
|
77
|
+
* Memory scope for observations.
|
|
78
|
+
* - 'resource': Observations span all threads for a resource (cross-thread memory)
|
|
79
|
+
* - 'thread': Observations are per-thread (default)
|
|
80
|
+
*/
|
|
81
|
+
scope?: 'resource' | 'thread';
|
|
82
|
+
/**
|
|
83
|
+
* Debug callback for observation events.
|
|
84
|
+
* Called whenever observation-related events occur.
|
|
85
|
+
* Useful for debugging and understanding the observation flow.
|
|
86
|
+
*/
|
|
87
|
+
onDebugEvent?: (event: ObservationDebugEvent) => void;
|
|
88
|
+
obscureThreadIds?: boolean;
|
|
89
|
+
/**
|
|
90
|
+
* Share the token budget between messages and observations.
|
|
91
|
+
* When true, the total budget = observation.messageTokens + reflection.observationTokens.
|
|
92
|
+
* - Messages can use more space when observations are small
|
|
93
|
+
* - Observations can use more space when messages are small
|
|
94
|
+
*
|
|
95
|
+
* This helps maximize context usage by allowing flexible allocation.
|
|
96
|
+
*
|
|
97
|
+
* @default false
|
|
98
|
+
*/
|
|
99
|
+
shareTokenBudget?: boolean;
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Internal resolved config with all defaults applied.
|
|
103
|
+
* Thresholds are stored as ThresholdRange internally for dynamic calculation,
|
|
104
|
+
* even when user provides a simple number (converted based on shareTokenBudget).
|
|
105
|
+
*/
|
|
106
|
+
interface ResolvedObservationConfig {
|
|
107
|
+
model: AgentConfig['model'];
|
|
108
|
+
/** Internal threshold - always stored as ThresholdRange for dynamic calculation */
|
|
109
|
+
messageTokens: number | ThresholdRange;
|
|
110
|
+
/** Whether shared token budget is enabled */
|
|
111
|
+
shareTokenBudget: boolean;
|
|
112
|
+
/** Model settings - merged with user config and defaults */
|
|
113
|
+
modelSettings: ModelSettings;
|
|
114
|
+
providerOptions: ProviderOptions;
|
|
115
|
+
maxTokensPerBatch: number;
|
|
116
|
+
/** Token interval for async background observation buffering (resolved from config) */
|
|
117
|
+
bufferTokens?: number;
|
|
118
|
+
/** Ratio of buffered observations to activate (0-1 float) */
|
|
119
|
+
bufferActivation?: number;
|
|
120
|
+
/** Token threshold above which synchronous observation is forced */
|
|
121
|
+
blockAfter?: number;
|
|
122
|
+
/** Custom instructions to append to the Observer's system prompt */
|
|
123
|
+
instruction?: string;
|
|
124
|
+
}
|
|
125
|
+
interface ResolvedReflectionConfig {
|
|
126
|
+
model: AgentConfig['model'];
|
|
127
|
+
/** Internal threshold - always stored as ThresholdRange for dynamic calculation */
|
|
128
|
+
observationTokens: number | ThresholdRange;
|
|
129
|
+
/** Whether shared token budget is enabled */
|
|
130
|
+
shareTokenBudget: boolean;
|
|
131
|
+
/** Model settings - merged with user config and defaults */
|
|
132
|
+
modelSettings: ModelSettings;
|
|
133
|
+
providerOptions: ProviderOptions;
|
|
134
|
+
/** Ratio (0-1) controlling when async reflection buffering starts */
|
|
135
|
+
bufferActivation?: number;
|
|
136
|
+
/** Token threshold above which synchronous reflection is forced */
|
|
137
|
+
blockAfter?: number;
|
|
138
|
+
/** Custom instructions to append to the Reflector's system prompt */
|
|
139
|
+
instruction?: string;
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* Default configuration values matching the spec
|
|
143
|
+
*/
|
|
144
|
+
export declare const OBSERVATIONAL_MEMORY_DEFAULTS: {
|
|
145
|
+
readonly observation: {
|
|
146
|
+
readonly model: "google/gemini-2.5-flash";
|
|
147
|
+
readonly messageTokens: 30000;
|
|
148
|
+
readonly modelSettings: {
|
|
149
|
+
readonly temperature: 0.3;
|
|
150
|
+
readonly maxOutputTokens: 100000;
|
|
151
|
+
};
|
|
152
|
+
readonly providerOptions: {
|
|
153
|
+
readonly google: {
|
|
154
|
+
readonly thinkingConfig: {
|
|
155
|
+
readonly thinkingBudget: 215;
|
|
156
|
+
};
|
|
157
|
+
};
|
|
158
|
+
};
|
|
159
|
+
readonly maxTokensPerBatch: 10000;
|
|
160
|
+
readonly bufferTokens: number | undefined;
|
|
161
|
+
readonly bufferActivation: number | undefined;
|
|
162
|
+
};
|
|
163
|
+
readonly reflection: {
|
|
164
|
+
readonly model: "google/gemini-2.5-flash";
|
|
165
|
+
readonly observationTokens: 40000;
|
|
166
|
+
readonly modelSettings: {
|
|
167
|
+
readonly temperature: 0;
|
|
168
|
+
readonly maxOutputTokens: 100000;
|
|
169
|
+
};
|
|
170
|
+
readonly providerOptions: {
|
|
171
|
+
readonly google: {
|
|
172
|
+
readonly thinkingConfig: {
|
|
173
|
+
readonly thinkingBudget: 1024;
|
|
174
|
+
};
|
|
175
|
+
};
|
|
176
|
+
};
|
|
177
|
+
readonly bufferActivation: number | undefined;
|
|
178
|
+
};
|
|
179
|
+
};
|
|
180
|
+
/**
|
|
181
|
+
* Continuation hint injected after observations to guide the model's behavior.
|
|
182
|
+
* Prevents the model from awkwardly acknowledging the memory system or treating
|
|
183
|
+
* the conversation as new after observed messages are removed.
|
|
184
|
+
*/
|
|
185
|
+
export declare const OBSERVATION_CONTINUATION_HINT = "This message is not from the user, the conversation history grew too long and wouldn't fit in context! Thankfully the entire conversation is stored in your memory observations. Please continue from where the observations left off. Do not refer to your \"memory observations\" directly, the user doesn't know about them, they are your memories! Just respond naturally as if you're remembering the conversation (you are!). Do not say \"Hi there!\" or \"based on our previous conversation\" as if the conversation is just starting, this is not a new conversation. This is an ongoing conversation, keep continuity by responding based on your memory. For example do not say \"I understand. I've reviewed my memory observations\", or \"I remember [...]\". Answer naturally following the suggestion from your memory. Note that your memory may contain a suggested first response, which you should follow.\n\nIMPORTANT: this system reminder is NOT from the user. The system placed it here as part of your memory system. This message is part of you remembering your conversation with the user.\n\nNOTE: Any messages following this system reminder are newer than your memories.";
|
|
186
|
+
/**
|
|
187
|
+
* Preamble that introduces the observations block.
|
|
188
|
+
* Use before `<observations>`, with instructions after.
|
|
189
|
+
* Full pattern: `${OBSERVATION_CONTEXT_PROMPT}\n\n<observations>\n${obs}\n</observations>\n\n${OBSERVATION_CONTEXT_INSTRUCTIONS}`
|
|
190
|
+
*/
|
|
191
|
+
export declare const OBSERVATION_CONTEXT_PROMPT = "The following observations block contains your memory of past conversations with this user.";
|
|
192
|
+
/**
|
|
193
|
+
* Instructions that tell the model how to interpret and use observations.
|
|
194
|
+
* Place AFTER the `<observations>` block so the model sees the data before the rules.
|
|
195
|
+
*/
|
|
196
|
+
export declare const OBSERVATION_CONTEXT_INSTRUCTIONS = "IMPORTANT: When responding, reference specific details from these observations. Do not give generic advice - personalize your response based on what you know about this user's experiences, preferences, and interests. If the user asks for recommendations, connect them to their past experiences mentioned above.\n\nKNOWLEDGE UPDATES: When asked about current state (e.g., \"where do I currently...\", \"what is my current...\"), always prefer the MOST RECENT information. Observations include dates - if you see conflicting information, the newer observation supersedes the older one. Look for phrases like \"will start\", \"is switching\", \"changed to\", \"moved to\" as indicators that previous information has been updated.\n\nPLANNED ACTIONS: If the user stated they planned to do something (e.g., \"I'm going to...\", \"I'm looking forward to...\", \"I will...\") and the date they planned to do it is now in the past (check the relative time like \"3 weeks ago\"), assume they completed the action unless there's evidence they didn't. For example, if someone said \"I'll start my new diet on Monday\" and that was 2 weeks ago, assume they started the diet.\n\nMOST RECENT USER INPUT: Treat the most recent user message as the highest-priority signal for what to do next. Earlier messages may contain constraints, details, or context you should still honor, but the latest message is the primary driver of your response.";
|
|
197
|
+
/**
|
|
198
|
+
* ObservationalMemory - A three-agent memory system for long conversations.
|
|
199
|
+
*
|
|
200
|
+
* This processor:
|
|
201
|
+
* 1. On input: Injects observations into context, filters out observed messages
|
|
202
|
+
* 2. On output: Tracks new messages, triggers Observer/Reflector when thresholds hit
|
|
203
|
+
*
|
|
204
|
+
* The Actor (main agent) sees:
|
|
205
|
+
* - Observations (compressed history)
|
|
206
|
+
* - Suggested continuation message
|
|
207
|
+
* - Recent unobserved messages
|
|
208
|
+
*
|
|
209
|
+
* @example
|
|
210
|
+
* ```ts
|
|
211
|
+
* import { ObservationalMemory } from '@mastra/memory/processors';
|
|
212
|
+
*
|
|
213
|
+
* // Minimal configuration
|
|
214
|
+
* const om = new ObservationalMemory({ storage });
|
|
215
|
+
*
|
|
216
|
+
* // Full configuration
|
|
217
|
+
* const om = new ObservationalMemory({
|
|
218
|
+
* storage,
|
|
219
|
+
* model: 'google/gemini-2.5-flash', // shared model for both agents
|
|
220
|
+
* shareTokenBudget: true,
|
|
221
|
+
* observation: {
|
|
222
|
+
* messageTokens: 30_000,
|
|
223
|
+
* modelSettings: { temperature: 0.3 },
|
|
224
|
+
* },
|
|
225
|
+
* reflection: {
|
|
226
|
+
* observationTokens: 40_000,
|
|
227
|
+
* },
|
|
228
|
+
* });
|
|
229
|
+
*
|
|
230
|
+
* const agent = new Agent({
|
|
231
|
+
* inputProcessors: [om],
|
|
232
|
+
* outputProcessors: [om],
|
|
233
|
+
* });
|
|
234
|
+
* ```
|
|
235
|
+
*/
|
|
236
|
+
export interface ObserveHooks {
|
|
237
|
+
onObservationStart?: () => void;
|
|
238
|
+
onObservationEnd?: () => void;
|
|
239
|
+
onReflectionStart?: () => void;
|
|
240
|
+
onReflectionEnd?: () => void;
|
|
241
|
+
}
|
|
242
|
+
export declare class ObservationalMemory implements Processor<'observational-memory'> {
|
|
243
|
+
readonly id: "observational-memory";
|
|
244
|
+
readonly name = "Observational Memory";
|
|
245
|
+
private storage;
|
|
246
|
+
private tokenCounter;
|
|
247
|
+
private scope;
|
|
248
|
+
private observationConfig;
|
|
249
|
+
private reflectionConfig;
|
|
250
|
+
private onDebugEvent?;
|
|
251
|
+
/** Internal Observer agent - created lazily */
|
|
252
|
+
private observerAgent?;
|
|
253
|
+
/** Internal Reflector agent - created lazily */
|
|
254
|
+
private reflectorAgent?;
|
|
255
|
+
private shouldObscureThreadIds;
|
|
256
|
+
private hasher;
|
|
257
|
+
private threadIdCache;
|
|
258
|
+
/**
|
|
259
|
+
* Track message IDs observed during this instance's lifetime.
|
|
260
|
+
* Prevents re-observing messages when per-thread lastObservedAt cursors
|
|
261
|
+
* haven't fully advanced past messages observed in a prior cycle.
|
|
262
|
+
*/
|
|
263
|
+
private observedMessageIds;
|
|
264
|
+
/** Internal MessageHistory for message persistence */
|
|
265
|
+
private messageHistory;
|
|
266
|
+
/**
|
|
267
|
+
* In-memory mutex for serializing observation/reflection cycles per resource/thread.
|
|
268
|
+
* Prevents race conditions where two concurrent cycles could both read isObserving=false
|
|
269
|
+
* before either sets it to true, leading to lost work.
|
|
270
|
+
*
|
|
271
|
+
* Key format: "resource:{resourceId}" or "thread:{threadId}"
|
|
272
|
+
* Value: Promise that resolves when the lock is released
|
|
273
|
+
*
|
|
274
|
+
* NOTE: This mutex only works within a single Node.js process. For distributed
|
|
275
|
+
* deployments, external locking (Redis, database locks) would be needed, or
|
|
276
|
+
* accept eventual consistency (acceptable for v1).
|
|
277
|
+
*/
|
|
278
|
+
private locks;
|
|
279
|
+
/**
|
|
280
|
+
* Track in-flight async buffering operations per resource/thread.
|
|
281
|
+
* STATIC: Shared across all ObservationalMemory instances in this process.
|
|
282
|
+
* This is critical because multiple OM instances are created per agent loop step,
|
|
283
|
+
* and we need them to share knowledge of in-flight operations.
|
|
284
|
+
* Key format: "obs:{lockKey}" or "refl:{lockKey}"
|
|
285
|
+
* Value: Promise that resolves when buffering completes
|
|
286
|
+
*/
|
|
287
|
+
private static asyncBufferingOps;
|
|
288
|
+
/**
|
|
289
|
+
* Track the last token boundary at which we started buffering.
|
|
290
|
+
* STATIC: Shared across all instances so boundary tracking persists across OM recreations.
|
|
291
|
+
* Key format: "obs:{lockKey}" or "refl:{lockKey}"
|
|
292
|
+
*/
|
|
293
|
+
private static lastBufferedBoundary;
|
|
294
|
+
/**
|
|
295
|
+
* Track the timestamp cursor for buffered messages.
|
|
296
|
+
* STATIC: Shared across all instances so each buffer only observes messages
|
|
297
|
+
* newer than the previous buffer's boundary.
|
|
298
|
+
* Key format: "obs:{lockKey}"
|
|
299
|
+
*/
|
|
300
|
+
private static lastBufferedAtTime;
|
|
301
|
+
/**
|
|
302
|
+
* Tracks cycleId for in-flight buffered reflections.
|
|
303
|
+
* STATIC: Shared across instances so we can match cycleId at activation time.
|
|
304
|
+
* Key format: "refl:{lockKey}"
|
|
305
|
+
*/
|
|
306
|
+
private static reflectionBufferCycleIds;
|
|
307
|
+
/**
|
|
308
|
+
* Track message IDs that have been sealed during async buffering.
|
|
309
|
+
* STATIC: Shared across all instances so saveMessagesWithSealedIdTracking
|
|
310
|
+
* generates new IDs when re-saving messages that were sealed in a previous step.
|
|
311
|
+
* Key format: threadId
|
|
312
|
+
* Value: Set of sealed message IDs
|
|
313
|
+
*/
|
|
314
|
+
private static sealedMessageIds;
|
|
315
|
+
/**
|
|
316
|
+
* Check if async buffering is enabled for observations.
|
|
317
|
+
*/
|
|
318
|
+
private isAsyncObservationEnabled;
|
|
319
|
+
/**
|
|
320
|
+
* Check if async buffering is enabled for reflections.
|
|
321
|
+
* Reflection buffering is enabled when bufferActivation is set (triggers at threshold * bufferActivation).
|
|
322
|
+
*/
|
|
323
|
+
private isAsyncReflectionEnabled;
|
|
324
|
+
/**
|
|
325
|
+
* Get the buffer interval boundary key for observations.
|
|
326
|
+
*/
|
|
327
|
+
private getObservationBufferKey;
|
|
328
|
+
/**
|
|
329
|
+
* Get the buffer interval boundary key for reflections.
|
|
330
|
+
*/
|
|
331
|
+
private getReflectionBufferKey;
|
|
332
|
+
/**
|
|
333
|
+
* Clean up static maps for a thread/resource to prevent memory leaks.
|
|
334
|
+
* Called after activation (to remove activated message IDs from sealedMessageIds)
|
|
335
|
+
* and from clear() (to fully remove all static state for a thread).
|
|
336
|
+
*/
|
|
337
|
+
private cleanupStaticMaps;
|
|
338
|
+
/**
|
|
339
|
+
* Await any in-flight async buffering operations for a given thread/resource.
|
|
340
|
+
* Returns once all buffering promises have settled (or after timeout).
|
|
341
|
+
*/
|
|
342
|
+
static awaitBuffering(threadId: string | null | undefined, resourceId: string | null | undefined, scope: 'thread' | 'resource', timeoutMs?: number): Promise<void>;
|
|
343
|
+
/**
|
|
344
|
+
* Safely get bufferedObservationChunks as an array.
|
|
345
|
+
* Handles cases where it might be a JSON string or undefined.
|
|
346
|
+
*/
|
|
347
|
+
private getBufferedChunks;
|
|
348
|
+
/**
|
|
349
|
+
* Check if we've crossed a new bufferTokens interval boundary.
|
|
350
|
+
* Returns true if async buffering should be triggered.
|
|
351
|
+
*
|
|
352
|
+
* When pending tokens are within ~1 bufferTokens of the observation threshold,
|
|
353
|
+
* the buffer interval is halved to produce finer-grained chunks right before
|
|
354
|
+
* activation. This improves chunk boundary selection, reducing overshoot.
|
|
355
|
+
*/
|
|
356
|
+
private shouldTriggerAsyncObservation;
|
|
357
|
+
/**
|
|
358
|
+
* Check if async reflection buffering should be triggered.
|
|
359
|
+
* Triggers once when observation tokens reach `threshold * bufferActivation`.
|
|
360
|
+
* Only allows one buffered reflection at a time.
|
|
361
|
+
*/
|
|
362
|
+
private shouldTriggerAsyncReflection;
|
|
363
|
+
/**
|
|
364
|
+
* Check if an async buffering operation is already in progress.
|
|
365
|
+
*/
|
|
366
|
+
private isAsyncBufferingInProgress;
|
|
367
|
+
/**
|
|
368
|
+
* Acquire a lock for the given key, execute the callback, then release.
|
|
369
|
+
* If a lock is already held, waits for it to be released before acquiring.
|
|
370
|
+
*/
|
|
371
|
+
private withLock;
|
|
372
|
+
/**
|
|
373
|
+
* Get the lock key for the current scope
|
|
374
|
+
*/
|
|
375
|
+
private getLockKey;
|
|
376
|
+
constructor(config: ObservationalMemoryConfig);
|
|
377
|
+
/**
|
|
378
|
+
* Get the current configuration for this OM instance.
|
|
379
|
+
* Used by the server to expose config to the UI when OM is added via processors.
|
|
380
|
+
*/
|
|
381
|
+
get config(): {
|
|
382
|
+
scope: 'resource' | 'thread';
|
|
383
|
+
observation: {
|
|
384
|
+
messageTokens: number | ThresholdRange;
|
|
385
|
+
};
|
|
386
|
+
reflection: {
|
|
387
|
+
observationTokens: number | ThresholdRange;
|
|
388
|
+
};
|
|
389
|
+
};
|
|
390
|
+
/**
|
|
391
|
+
* Wait for any in-flight async buffering operations for the given thread/resource.
|
|
392
|
+
* Used by server endpoints to block until buffering completes so the UI can get final state.
|
|
393
|
+
*/
|
|
394
|
+
waitForBuffering(threadId: string | null | undefined, resourceId: string | null | undefined, timeoutMs?: number): Promise<void>;
|
|
395
|
+
/**
|
|
396
|
+
* Get the full config including resolved model names.
|
|
397
|
+
* This is async because it needs to resolve the model configs.
|
|
398
|
+
*/
|
|
399
|
+
getResolvedConfig(requestContext?: RequestContext): Promise<{
|
|
400
|
+
scope: 'resource' | 'thread';
|
|
401
|
+
observation: {
|
|
402
|
+
messageTokens: number | ThresholdRange;
|
|
403
|
+
model: string;
|
|
404
|
+
};
|
|
405
|
+
reflection: {
|
|
406
|
+
observationTokens: number | ThresholdRange;
|
|
407
|
+
model: string;
|
|
408
|
+
};
|
|
409
|
+
}>;
|
|
410
|
+
/**
|
|
411
|
+
* Emit a debug event if the callback is configured
|
|
412
|
+
*/
|
|
413
|
+
private emitDebugEvent;
|
|
414
|
+
/**
|
|
415
|
+
* Validate buffer configuration on first use.
|
|
416
|
+
* Ensures bufferTokens is less than the threshold and bufferActivation is valid.
|
|
417
|
+
*/
|
|
418
|
+
private validateBufferConfig;
|
|
419
|
+
/**
|
|
420
|
+
* Check whether the unobserved message tokens meet the observation threshold.
|
|
421
|
+
*/
|
|
422
|
+
private meetsObservationThreshold;
|
|
423
|
+
/**
|
|
424
|
+
* Get or create the Observer agent
|
|
425
|
+
*/
|
|
426
|
+
private getObserverAgent;
|
|
427
|
+
/**
|
|
428
|
+
* Get or create the Reflector agent
|
|
429
|
+
*/
|
|
430
|
+
private getReflectorAgent;
|
|
431
|
+
/**
|
|
432
|
+
* Get thread/resource IDs for storage lookup
|
|
433
|
+
*/
|
|
434
|
+
private getStorageIds;
|
|
435
|
+
/**
|
|
436
|
+
* Get or create the observational memory record.
|
|
437
|
+
* Returns the existing record if one exists, otherwise initializes a new one.
|
|
438
|
+
*/
|
|
439
|
+
getOrCreateRecord(threadId: string, resourceId?: string): Promise<ObservationalMemoryRecord>;
|
|
440
|
+
/**
|
|
441
|
+
* Check if we need to trigger reflection.
|
|
442
|
+
*/
|
|
443
|
+
private shouldReflect;
|
|
444
|
+
/**
|
|
445
|
+
* Get current config snapshot for observation markers.
|
|
446
|
+
*/
|
|
447
|
+
private getObservationMarkerConfig;
|
|
448
|
+
/**
|
|
449
|
+
* Persist a data-om-* marker part on the last assistant message in messageList
|
|
450
|
+
* AND save the updated message to the DB so it survives page reload.
|
|
451
|
+
* (data-* parts are filtered out before sending to the LLM, so they don't affect model calls.)
|
|
452
|
+
*/
|
|
453
|
+
private persistMarkerToMessage;
|
|
454
|
+
/**
|
|
455
|
+
* Persist a marker to the last assistant message in storage.
|
|
456
|
+
* Unlike persistMarkerToMessage, this fetches messages directly from the DB
|
|
457
|
+
* so it works even when no MessageList is available (e.g. async buffering ops).
|
|
458
|
+
*/
|
|
459
|
+
private persistMarkerToStorage;
|
|
460
|
+
/**
|
|
461
|
+
* Find the last completed observation boundary in a message's parts.
|
|
462
|
+
* A completed observation is a start marker followed by an end marker.
|
|
463
|
+
*
|
|
464
|
+
* Returns the index of the END marker (which is the observation boundary),
|
|
465
|
+
* or -1 if no completed observation is found.
|
|
466
|
+
*/
|
|
467
|
+
private findLastCompletedObservationBoundary;
|
|
468
|
+
/**
|
|
469
|
+
* Check if a message has an in-progress observation (start without end).
|
|
470
|
+
*/
|
|
471
|
+
private hasInProgressObservation;
|
|
472
|
+
/**
|
|
473
|
+
* Seal messages to prevent new parts from being merged into them.
|
|
474
|
+
* This is used when starting buffering to capture the current content state.
|
|
475
|
+
*
|
|
476
|
+
* Sealing works by:
|
|
477
|
+
* 1. Setting `message.content.metadata.mastra.sealed = true` (message-level flag)
|
|
478
|
+
* 2. Adding `metadata.mastra.sealedAt` to the last part (boundary marker)
|
|
479
|
+
*
|
|
480
|
+
* When MessageList.add() receives a message with the same ID as a sealed message,
|
|
481
|
+
* it creates a new message with only the parts beyond the seal boundary.
|
|
482
|
+
*
|
|
483
|
+
* The messages are mutated in place - since they're references to the same objects
|
|
484
|
+
* in the MessageList, the seal will be recognized immediately.
|
|
485
|
+
*
|
|
486
|
+
* @param messages - Messages to seal (mutated in place)
|
|
487
|
+
*/
|
|
488
|
+
private sealMessagesForBuffering;
|
|
489
|
+
/**
|
|
490
|
+
* Insert an observation marker into a message.
|
|
491
|
+
* The marker is appended directly to the message's parts array (mutating in place).
|
|
492
|
+
* Also persists the change to storage so markers survive page refresh.
|
|
493
|
+
*
|
|
494
|
+
* For end/failed markers, the message is also "sealed" to prevent future content
|
|
495
|
+
* from being merged into it. This ensures observation markers are preserved.
|
|
496
|
+
*/
|
|
497
|
+
/**
|
|
498
|
+
* Insert an observation marker into a message.
|
|
499
|
+
* For start markers, this pushes the part directly.
|
|
500
|
+
* For end/failed markers, this should be called AFTER writer.custom() has added the part,
|
|
501
|
+
* so we just find the part and add sealing metadata.
|
|
502
|
+
*/
|
|
503
|
+
/**
|
|
504
|
+
* Get unobserved parts from a message.
|
|
505
|
+
* If the message has a completed observation (start + end), only return parts after the end.
|
|
506
|
+
* If observation is in progress (start without end), include parts before the start.
|
|
507
|
+
* Otherwise, return all parts.
|
|
508
|
+
*/
|
|
509
|
+
private getUnobservedParts;
|
|
510
|
+
/**
|
|
511
|
+
* Check if a message has any unobserved parts.
|
|
512
|
+
*/
|
|
513
|
+
private hasUnobservedParts;
|
|
514
|
+
/**
|
|
515
|
+
* Create a virtual message containing only the unobserved parts.
|
|
516
|
+
* This is used for token counting and observation.
|
|
517
|
+
*/
|
|
518
|
+
private createUnobservedMessage;
|
|
519
|
+
/**
|
|
520
|
+
* Get unobserved messages with part-level filtering.
|
|
521
|
+
*
|
|
522
|
+
* This method uses data-om-observation-end markers to filter at the part level:
|
|
523
|
+
* 1. For messages WITH a completed observation: only return parts AFTER the end marker
|
|
524
|
+
* 2. For messages WITHOUT completed observation: check timestamp against lastObservedAt
|
|
525
|
+
*
|
|
526
|
+
* This handles the case where a single message accumulates many parts
|
|
527
|
+
* (like tool calls) during an agentic loop - we only observe the new parts.
|
|
528
|
+
*/
|
|
529
|
+
private getUnobservedMessages;
|
|
530
|
+
/**
|
|
531
|
+
* Wrapper for observer/reflector agent.generate() calls that checks for abort.
|
|
532
|
+
* agent.generate() returns an empty result on abort instead of throwing,
|
|
533
|
+
* so we must check the signal before and after the call.
|
|
534
|
+
* Retries are handled by Mastra's built-in p-retry at the model execution layer.
|
|
535
|
+
*/
|
|
536
|
+
private withAbortCheck;
|
|
537
|
+
/**
|
|
538
|
+
* Call the Observer agent to extract observations.
|
|
539
|
+
*/
|
|
540
|
+
private callObserver;
|
|
541
|
+
/**
|
|
542
|
+
* Call the Observer agent for multiple threads in a single batched request.
|
|
543
|
+
* This is more efficient than calling the Observer for each thread individually.
|
|
544
|
+
* Returns per-thread results with observations, currentTask, and suggestedContinuation,
|
|
545
|
+
* plus the total usage for the batch.
|
|
546
|
+
*/
|
|
547
|
+
private callMultiThreadObserver;
|
|
548
|
+
/**
|
|
549
|
+
* Call the Reflector agent to condense observations.
|
|
550
|
+
* Includes compression validation and retry logic.
|
|
551
|
+
*/
|
|
552
|
+
private callReflector;
|
|
553
|
+
/**
|
|
554
|
+
* Format observations for injection into context.
|
|
555
|
+
* Applies token optimization before presenting to the Actor.
|
|
556
|
+
*
|
|
557
|
+
* In resource scope mode, filters continuity messages to only show
|
|
558
|
+
* the message for the current thread.
|
|
559
|
+
*/
|
|
560
|
+
/**
|
|
561
|
+
* Format observations for injection into the Actor's context.
|
|
562
|
+
* @param observations - The observations to inject
|
|
563
|
+
* @param suggestedResponse - Thread-specific suggested response (from thread metadata)
|
|
564
|
+
* @param unobservedContextBlocks - Formatted <unobserved-context> blocks from other threads
|
|
565
|
+
*/
|
|
566
|
+
private formatObservationsForContext;
|
|
567
|
+
/**
|
|
568
|
+
* Get threadId and resourceId from either RequestContext or MessageList
|
|
569
|
+
*/
|
|
570
|
+
private getThreadContext;
|
|
571
|
+
/**
|
|
572
|
+
* Load historical unobserved messages into the message list (step 0 only).
|
|
573
|
+
* In resource scope, loads only current thread's messages.
|
|
574
|
+
* In thread scope, loads all unobserved messages for the thread.
|
|
575
|
+
*/
|
|
576
|
+
private loadHistoricalMessagesIfNeeded;
|
|
577
|
+
/**
|
|
578
|
+
* Calculate all threshold-related values for observation decision making.
|
|
579
|
+
*/
|
|
580
|
+
private calculateObservationThresholds;
|
|
581
|
+
/**
|
|
582
|
+
* Emit debug event and stream progress part for UI feedback.
|
|
583
|
+
*/
|
|
584
|
+
private emitStepProgress;
|
|
585
|
+
/**
|
|
586
|
+
* Handle observation when threshold is reached.
|
|
587
|
+
* Tries async activation first if enabled, then falls back to sync observation.
|
|
588
|
+
* Returns whether observation succeeded.
|
|
589
|
+
*/
|
|
590
|
+
private handleThresholdReached;
|
|
591
|
+
/**
|
|
592
|
+
* Remove observed messages from message list after successful observation.
|
|
593
|
+
* Accepts optional observedMessageIds for activation-based cleanup (when no markers are present).
|
|
594
|
+
*/
|
|
595
|
+
private cleanupAfterObservation;
|
|
596
|
+
/**
|
|
597
|
+
* Handle per-step save when threshold is not reached.
|
|
598
|
+
* Persists messages incrementally to prevent data loss on interruption.
|
|
599
|
+
*/
|
|
600
|
+
private handlePerStepSave;
|
|
601
|
+
/**
|
|
602
|
+
* Inject observations as system message and add continuation reminder.
|
|
603
|
+
*/
|
|
604
|
+
private injectObservationsIntoContext;
|
|
605
|
+
/**
|
|
606
|
+
* Filter out already-observed messages from message list (step 0 only).
|
|
607
|
+
* Historical messages loaded from DB may contain observation markers from previous sessions.
|
|
608
|
+
*/
|
|
609
|
+
private filterAlreadyObservedMessages;
|
|
610
|
+
/**
|
|
611
|
+
* Process input at each step - check threshold, observe if needed, save, inject observations.
|
|
612
|
+
* This is the ONLY processor method - all OM logic happens here.
|
|
613
|
+
*
|
|
614
|
+
* Flow:
|
|
615
|
+
* 1. Load historical messages (step 0 only)
|
|
616
|
+
* 2. Check if observation threshold is reached
|
|
617
|
+
* 3. If threshold reached: observe, save messages with markers
|
|
618
|
+
* 4. Inject observations into context
|
|
619
|
+
* 5. Filter out already-observed messages
|
|
620
|
+
*/
|
|
621
|
+
processInputStep(args: ProcessInputStepArgs): Promise<MessageList | MastraDBMessage[]>;
|
|
622
|
+
/**
|
|
623
|
+
* Save any unsaved messages at the end of the agent turn.
|
|
624
|
+
*
|
|
625
|
+
* This is the "final save" that catches messages that processInputStep didn't save
|
|
626
|
+
* (e.g., when the observation threshold was never reached, or on single-step execution).
|
|
627
|
+
* Without this, messages would be lost because MessageHistory is disabled when OM is active.
|
|
628
|
+
*/
|
|
629
|
+
processOutputResult(args: ProcessOutputResultArgs): Promise<MessageList | MastraDBMessage[]>;
|
|
630
|
+
/**
|
|
631
|
+
* Save messages to storage while preventing duplicate inserts for sealed messages.
|
|
632
|
+
*
|
|
633
|
+
* Sealed messages that do not yet contain a completed observation boundary are
|
|
634
|
+
* skipped because async buffering already persisted them.
|
|
635
|
+
*/
|
|
636
|
+
private saveMessagesWithSealedIdTracking;
|
|
637
|
+
/**
|
|
638
|
+
* Load messages from storage that haven't been observed yet.
|
|
639
|
+
* Uses cursor-based query with lastObservedAt timestamp for efficiency.
|
|
640
|
+
*
|
|
641
|
+
* In resource scope mode, loads messages for the entire resource (all threads).
|
|
642
|
+
* In thread scope mode, loads messages for just the current thread.
|
|
643
|
+
*/
|
|
644
|
+
private loadUnobservedMessages;
|
|
645
|
+
/**
|
|
646
|
+
* Load unobserved messages from other threads (not the current thread) for a resource.
|
|
647
|
+
* Called fresh each step so it reflects the latest lastObservedAt cursors
|
|
648
|
+
* after observations complete.
|
|
649
|
+
*/
|
|
650
|
+
private loadOtherThreadsContext;
|
|
651
|
+
/**
|
|
652
|
+
* Format unobserved messages from other threads as <unobserved-context> blocks.
|
|
653
|
+
* These are injected into the Actor's context so it has awareness of activity
|
|
654
|
+
* in other threads for the same resource.
|
|
655
|
+
*/
|
|
656
|
+
private formatUnobservedContextBlocks;
|
|
657
|
+
private representThreadIDInContext;
|
|
658
|
+
/**
|
|
659
|
+
* Strip any thread tags that the Observer might have added.
|
|
660
|
+
* Thread attribution is handled externally by the system, not by the Observer.
|
|
661
|
+
* This is a defense-in-depth measure.
|
|
662
|
+
*/
|
|
663
|
+
private stripThreadTags;
|
|
664
|
+
/**
|
|
665
|
+
* Get the maximum createdAt timestamp from a list of messages.
|
|
666
|
+
* Used to set lastObservedAt to the most recent message timestamp instead of current time.
|
|
667
|
+
* This ensures historical data (like LongMemEval fixtures) works correctly.
|
|
668
|
+
*/
|
|
669
|
+
private getMaxMessageTimestamp;
|
|
670
|
+
/**
|
|
671
|
+
* Wrap observations in a thread attribution tag.
|
|
672
|
+
* Used in resource scope to track which thread observations came from.
|
|
673
|
+
*/
|
|
674
|
+
private wrapWithThreadTag;
|
|
675
|
+
/**
|
|
676
|
+
* Append or merge new thread sections.
|
|
677
|
+
* If the new section has the same thread ID and date as an existing section,
|
|
678
|
+
* merge the observations into that section to reduce token usage.
|
|
679
|
+
* Otherwise, append as a new section.
|
|
680
|
+
*/
|
|
681
|
+
private replaceOrAppendThreadSection;
|
|
682
|
+
/**
|
|
683
|
+
* Sort threads by their oldest unobserved message.
|
|
684
|
+
* Returns thread IDs in order from oldest to most recent.
|
|
685
|
+
* This ensures no thread's messages get "stuck" unobserved.
|
|
686
|
+
*/
|
|
687
|
+
private sortThreadsByOldestMessage;
|
|
688
|
+
/**
|
|
689
|
+
* Do synchronous observation (fallback when no buffering)
|
|
690
|
+
*/
|
|
691
|
+
private doSynchronousObservation;
|
|
692
|
+
/**
|
|
693
|
+
* Start an async background observation that stores results to bufferedObservations.
|
|
694
|
+
* This is a fire-and-forget operation that runs in the background.
|
|
695
|
+
* The results will be swapped to active when the main threshold is reached.
|
|
696
|
+
*
|
|
697
|
+
* If another buffering operation is already in progress for this scope, this will
|
|
698
|
+
* wait for it to complete before starting a new one (mutex behavior).
|
|
699
|
+
*
|
|
700
|
+
* @param record - Current OM record
|
|
701
|
+
* @param threadId - Thread ID
|
|
702
|
+
* @param unobservedMessages - All unobserved messages (will be filtered for already-buffered)
|
|
703
|
+
* @param lockKey - Lock key for this scope
|
|
704
|
+
* @param writer - Optional stream writer for emitting buffering markers
|
|
705
|
+
*/
|
|
706
|
+
private startAsyncBufferedObservation;
|
|
707
|
+
/**
|
|
708
|
+
* Internal method that waits for existing buffering operation and then runs new buffering.
|
|
709
|
+
* This implements the mutex-wait behavior.
|
|
710
|
+
*/
|
|
711
|
+
private runAsyncBufferedObservation;
|
|
712
|
+
/**
|
|
713
|
+
* Perform async buffered observation - observes messages and stores to bufferedObservations.
|
|
714
|
+
* Does NOT update activeObservations or trigger reflection.
|
|
715
|
+
*
|
|
716
|
+
* The observer sees: active observations + existing buffered observations + message history
|
|
717
|
+
* (excluding already-buffered messages).
|
|
718
|
+
*/
|
|
719
|
+
private doAsyncBufferedObservation;
|
|
720
|
+
/**
|
|
721
|
+
* Combine active and buffered observations for the buffering observer context.
|
|
722
|
+
* The buffering observer needs to see both so it doesn't duplicate content.
|
|
723
|
+
*/
|
|
724
|
+
private combineObservationsForBuffering;
|
|
725
|
+
/**
|
|
726
|
+
* Try to activate buffered observations when threshold is reached.
|
|
727
|
+
* Returns true if activation succeeded, false if no buffered content or activation failed.
|
|
728
|
+
*
|
|
729
|
+
* @param record - Current OM record
|
|
730
|
+
* @param lockKey - Lock key for this scope
|
|
731
|
+
* @param writer - Optional writer for emitting UI markers
|
|
732
|
+
*/
|
|
733
|
+
private tryActivateBufferedObservations;
|
|
734
|
+
/**
|
|
735
|
+
* Start an async background reflection that stores results to bufferedReflection.
|
|
736
|
+
* This is a fire-and-forget operation that runs in the background.
|
|
737
|
+
* The results will be swapped to active when the main reflection threshold is reached.
|
|
738
|
+
*
|
|
739
|
+
* @param record - Current OM record
|
|
740
|
+
* @param observationTokens - Current observation token count
|
|
741
|
+
* @param lockKey - Lock key for this scope
|
|
742
|
+
*/
|
|
743
|
+
private startAsyncBufferedReflection;
|
|
744
|
+
/**
|
|
745
|
+
* Perform async buffered reflection - reflects observations and stores to bufferedReflection.
|
|
746
|
+
* Does NOT create a new generation or update activeObservations.
|
|
747
|
+
*/
|
|
748
|
+
private doAsyncBufferedReflection;
|
|
749
|
+
/**
|
|
750
|
+
* Try to activate buffered reflection when threshold is reached.
|
|
751
|
+
* Returns true if activation succeeded, false if no buffered content or activation failed.
|
|
752
|
+
*
|
|
753
|
+
* @param record - Current OM record
|
|
754
|
+
* @param lockKey - Lock key for this scope
|
|
755
|
+
*/
|
|
756
|
+
private tryActivateBufferedReflection;
|
|
757
|
+
/**
|
|
758
|
+
* Resource-scoped observation: observe ALL threads with unobserved messages.
|
|
759
|
+
* Threads are observed in oldest-first order to ensure no thread's messages
|
|
760
|
+
* get "stuck" unobserved forever.
|
|
761
|
+
*
|
|
762
|
+
* Key differences from thread-scoped observation:
|
|
763
|
+
* 1. Loads messages from ALL threads for the resource
|
|
764
|
+
* 2. Observes threads one-by-one in oldest-first order
|
|
765
|
+
* 3. Only updates lastObservedAt AFTER all threads are observed
|
|
766
|
+
* 4. Only triggers reflection AFTER all threads are observed
|
|
767
|
+
*/
|
|
768
|
+
private doResourceScopedObservation;
|
|
769
|
+
/**
|
|
770
|
+
* Check if async reflection should be triggered or activated.
|
|
771
|
+
* Only handles the async path — will never do synchronous (blocking) reflection.
|
|
772
|
+
* Safe to call after buffered observation activation.
|
|
773
|
+
*/
|
|
774
|
+
private maybeAsyncReflect;
|
|
775
|
+
/**
|
|
776
|
+
* Check if reflection needed and trigger if so.
|
|
777
|
+
* Supports both synchronous reflection and async buffered reflection.
|
|
778
|
+
* When async buffering is enabled via `bufferTokens`, reflection is triggered
|
|
779
|
+
* in the background at intervals, and activated when the threshold is reached.
|
|
780
|
+
*/
|
|
781
|
+
private maybeReflect;
|
|
782
|
+
/**
|
|
783
|
+
* Manually trigger observation.
|
|
784
|
+
*
|
|
785
|
+
* When `messages` is provided, those are used directly (filtered for unobserved)
|
|
786
|
+
* instead of reading from storage. This allows external systems (e.g., opencode)
|
|
787
|
+
* to pass conversation messages without duplicating them into Mastra's DB.
|
|
788
|
+
*/
|
|
789
|
+
observe(opts: {
|
|
790
|
+
threadId: string;
|
|
791
|
+
resourceId?: string;
|
|
792
|
+
messages?: MastraDBMessage[];
|
|
793
|
+
hooks?: ObserveHooks;
|
|
794
|
+
requestContext?: RequestContext;
|
|
795
|
+
}): Promise<void>;
|
|
796
|
+
/**
|
|
797
|
+
* Manually trigger reflection with optional guidance prompt.
|
|
798
|
+
*
|
|
799
|
+
* @example
|
|
800
|
+
* ```ts
|
|
801
|
+
* // Trigger reflection with specific focus
|
|
802
|
+
* await om.reflect(threadId, resourceId,
|
|
803
|
+
* "focus on the authentication implementation, only keep minimal details about UI styling"
|
|
804
|
+
* );
|
|
805
|
+
* ```
|
|
806
|
+
*/
|
|
807
|
+
reflect(threadId: string, resourceId?: string, prompt?: string, requestContext?: RequestContext): Promise<void>;
|
|
808
|
+
/**
|
|
809
|
+
* Get current observations for a thread/resource
|
|
810
|
+
*/
|
|
811
|
+
getObservations(threadId: string, resourceId?: string): Promise<string | undefined>;
|
|
812
|
+
/**
|
|
813
|
+
* Get current record for a thread/resource
|
|
814
|
+
*/
|
|
815
|
+
getRecord(threadId: string, resourceId?: string): Promise<ObservationalMemoryRecord | null>;
|
|
816
|
+
/**
|
|
817
|
+
* Get observation history (previous generations)
|
|
818
|
+
*/
|
|
819
|
+
getHistory(threadId: string, resourceId?: string, limit?: number): Promise<ObservationalMemoryRecord[]>;
|
|
820
|
+
/**
|
|
821
|
+
* Clear all memory for a specific thread/resource
|
|
822
|
+
*/
|
|
823
|
+
clear(threadId: string, resourceId?: string): Promise<void>;
|
|
824
|
+
/**
|
|
825
|
+
* Get the underlying storage adapter
|
|
826
|
+
*/
|
|
827
|
+
getStorage(): MemoryStorage;
|
|
828
|
+
/**
|
|
829
|
+
* Get the token counter
|
|
830
|
+
*/
|
|
831
|
+
getTokenCounter(): TokenCounter;
|
|
832
|
+
/**
|
|
833
|
+
* Get current observation configuration
|
|
834
|
+
*/
|
|
835
|
+
getObservationConfig(): ResolvedObservationConfig;
|
|
836
|
+
/**
|
|
837
|
+
* Get current reflection configuration
|
|
838
|
+
*/
|
|
839
|
+
getReflectionConfig(): ResolvedReflectionConfig;
|
|
840
|
+
}
|
|
841
|
+
export {};
|
|
842
|
+
//# sourceMappingURL=observational-memory.d.ts.map
|