@mastra/memory 1.27.0 → 1.27.1-alpha.0
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 +11 -0
- package/dist/docs/SKILL.md +2 -1
- package/dist/docs/assets/SOURCE_MAP.json +1 -1
- package/dist/docs/references/docs-guides-context-engineering.md +297 -0
- package/dist/index.cjs +1 -1
- package/dist/index.d.ts +6 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/processors/index.cjs +1 -1
- package/dist/processors/index.js +1 -1
- package/dist/processors/observational-memory/observation-turn/load-memory-context.d.ts +3 -1
- package/dist/processors/observational-memory/observation-turn/load-memory-context.d.ts.map +1 -1
- package/dist/processors/observational-memory/observation-turn/turn.d.ts +2 -1
- package/dist/processors/observational-memory/observation-turn/turn.d.ts.map +1 -1
- package/dist/processors/observational-memory/processor.d.ts +2 -0
- package/dist/processors/observational-memory/processor.d.ts.map +1 -1
- package/dist/processors/observational-memory/tracing.d.ts.map +1 -1
- package/dist/{src-aTfbkxBl.js → src-BlQjkJzk.js} +76 -37
- package/dist/{src-aTfbkxBl.js.map → src-BlQjkJzk.js.map} +1 -1
- package/dist/{src-CxkQ-ICG.cjs → src-bE5dqCVp.cjs} +76 -37
- package/dist/{src-CxkQ-ICG.cjs.map → src-bE5dqCVp.cjs.map} +1 -1
- package/package.json +7 -7
|
@@ -19255,7 +19255,19 @@ async function withOmTracingSpan({ phase, model, inputTokens, requestContext, ob
|
|
|
19255
19255
|
});
|
|
19256
19256
|
const childObservabilityContext = createObservabilityContext({ currentSpan: span });
|
|
19257
19257
|
if (!span) return callback(childObservabilityContext);
|
|
19258
|
-
return span.executeInContext(() =>
|
|
19258
|
+
return span.executeInContext(async () => {
|
|
19259
|
+
try {
|
|
19260
|
+
const result = await callback(childObservabilityContext);
|
|
19261
|
+
span.end();
|
|
19262
|
+
return result;
|
|
19263
|
+
} catch (error) {
|
|
19264
|
+
span.error({
|
|
19265
|
+
error,
|
|
19266
|
+
endSpan: true
|
|
19267
|
+
});
|
|
19268
|
+
throw error;
|
|
19269
|
+
}
|
|
19270
|
+
});
|
|
19259
19271
|
}
|
|
19260
19272
|
//#endregion
|
|
19261
19273
|
//#region src/processors/observational-memory/observer-runner.ts
|
|
@@ -24392,10 +24404,11 @@ ObservationStrategy.create = ((om, opts) => {
|
|
|
24392
24404
|
});
|
|
24393
24405
|
//#endregion
|
|
24394
24406
|
//#region src/processors/observational-memory/observation-turn/load-memory-context.ts
|
|
24395
|
-
async function loadMemoryContextMessages({ memory, messageList, threadId, resourceId }) {
|
|
24407
|
+
async function loadMemoryContextMessages({ memory, messageList, threadId, resourceId, runState }) {
|
|
24396
24408
|
const ctx = await memory.getContext({
|
|
24397
24409
|
threadId,
|
|
24398
|
-
resourceId
|
|
24410
|
+
resourceId,
|
|
24411
|
+
runState
|
|
24399
24412
|
});
|
|
24400
24413
|
for (const msg of ctx.messages) if (msg.role !== "system") messageList.add(msg, "memory");
|
|
24401
24414
|
return ctx;
|
|
@@ -24853,10 +24866,11 @@ var ObservationTurn = class {
|
|
|
24853
24866
|
* If a MemoryContextProvider is passed, loads historical messages and adds
|
|
24854
24867
|
* them to the MessageList. Without a provider, only fetches/caches the record.
|
|
24855
24868
|
*/
|
|
24856
|
-
async start(memory) {
|
|
24869
|
+
async start(memory, runState) {
|
|
24857
24870
|
if (this._started) throw new Error("Turn already started");
|
|
24858
24871
|
this._started = true;
|
|
24859
24872
|
this._record = await this.om.getOrCreateRecord(this.threadId, this.resourceId);
|
|
24873
|
+
runState?.set(`observational-memory:record:${this.threadId}:${this.resourceId ?? ""}`, this._record);
|
|
24860
24874
|
this._generationCountAtStart = this._record.generationCount;
|
|
24861
24875
|
this.memory = memory;
|
|
24862
24876
|
if (memory) {
|
|
@@ -24864,7 +24878,8 @@ var ObservationTurn = class {
|
|
|
24864
24878
|
memory,
|
|
24865
24879
|
messageList: this.messageList,
|
|
24866
24880
|
threadId: this.threadId,
|
|
24867
|
-
resourceId: this.resourceId
|
|
24881
|
+
resourceId: this.resourceId,
|
|
24882
|
+
runState
|
|
24868
24883
|
});
|
|
24869
24884
|
this._context = {
|
|
24870
24885
|
messages: ctx.messages,
|
|
@@ -28946,7 +28961,9 @@ var ObservationalMemoryProcessor = class {
|
|
|
28946
28961
|
return messageList;
|
|
28947
28962
|
}
|
|
28948
28963
|
const { threadId, resourceId } = context;
|
|
28949
|
-
const
|
|
28964
|
+
const memoryContext = parseMemoryRequestContext(requestContext);
|
|
28965
|
+
const runState = memoryContext?.runState?.();
|
|
28966
|
+
const readOnly = memoryContext?.memoryConfig?.readOnly;
|
|
28950
28967
|
const actorModelContext = model?.modelId ? {
|
|
28951
28968
|
provider: model.provider,
|
|
28952
28969
|
modelId: model.modelId,
|
|
@@ -28963,7 +28980,8 @@ var ObservationalMemoryProcessor = class {
|
|
|
28963
28980
|
memory: this.memory,
|
|
28964
28981
|
messageList,
|
|
28965
28982
|
threadId,
|
|
28966
|
-
resourceId
|
|
28983
|
+
resourceId,
|
|
28984
|
+
runState
|
|
28967
28985
|
});
|
|
28968
28986
|
const systemMessages = ctx.omRecord ? await this.engine.buildContextSystemMessages({
|
|
28969
28987
|
threadId,
|
|
@@ -29004,7 +29022,7 @@ var ObservationalMemoryProcessor = class {
|
|
|
29004
29022
|
this.turn.sendStateSignal = args.sendStateSignal;
|
|
29005
29023
|
this.turn.agent = args.agent;
|
|
29006
29024
|
this.turn.requestContext = requestContext;
|
|
29007
|
-
await this.turn.start(this.memory);
|
|
29025
|
+
await this.turn.start(this.memory, runState);
|
|
29008
29026
|
if (stepNumber === 0 && this.temporalMarkers) await insertTemporalGapMarkers({
|
|
29009
29027
|
messageList,
|
|
29010
29028
|
sendSignal: args.sendSignal
|
|
@@ -30174,14 +30192,18 @@ ${workingMemory}`;
|
|
|
30174
30192
|
if (!this.threadConfig.workingMemory?.enabled) return null;
|
|
30175
30193
|
return extractWorkingMemoryContent(text)?.trim() ?? null;
|
|
30176
30194
|
}
|
|
30177
|
-
async getWorkingMemory({ threadId, resourceId, memoryConfig }) {
|
|
30195
|
+
async getWorkingMemory({ threadId, resourceId, memoryConfig, runState }) {
|
|
30178
30196
|
const config = this.getMergedThreadConfig(memoryConfig || {});
|
|
30179
30197
|
if (!config.workingMemory?.enabled) return null;
|
|
30180
30198
|
const scope = config.workingMemory.scope || "resource";
|
|
30181
30199
|
let workingMemoryData = null;
|
|
30182
30200
|
if (scope === "resource" && !resourceId) throw new Error("Memory error: Resource-scoped working memory is enabled but no resourceId was provided. Either provide a resourceId or explicitly set workingMemory.scope to 'thread'.");
|
|
30183
|
-
if (scope === "resource" && resourceId)
|
|
30184
|
-
|
|
30201
|
+
if (scope === "resource" && resourceId) {
|
|
30202
|
+
const loadWorkingMemory = async () => {
|
|
30203
|
+
return (await (await this.getMemoryStore()).getResourceById({ resourceId }))?.workingMemory || null;
|
|
30204
|
+
};
|
|
30205
|
+
workingMemoryData = runState ? await runState.load(`working-memory:resource:${resourceId}`, loadWorkingMemory) : await loadWorkingMemory();
|
|
30206
|
+
} else workingMemoryData = (runState?.threadLoaded ? runState.thread : await this.getThreadById({ threadId }))?.metadata?.workingMemory;
|
|
30185
30207
|
if (!workingMemoryData) return null;
|
|
30186
30208
|
return workingMemoryData;
|
|
30187
30209
|
}
|
|
@@ -30213,16 +30235,18 @@ ${workingMemory}`;
|
|
|
30213
30235
|
content: (config.workingMemory.template || this.defaultWorkingMemoryTemplate).trim()
|
|
30214
30236
|
};
|
|
30215
30237
|
}
|
|
30216
|
-
async getSystemMessage({ threadId, resourceId, memoryConfig }) {
|
|
30238
|
+
async getSystemMessage({ threadId, resourceId, memoryConfig, runState }) {
|
|
30217
30239
|
const config = this.getMergedThreadConfig(memoryConfig);
|
|
30218
30240
|
this.assertWorkingMemoryStateSignalsCompatibility(config);
|
|
30219
30241
|
if (!config.workingMemory?.enabled) return null;
|
|
30220
30242
|
if (config.workingMemory?.useStateSignals) return null;
|
|
30221
|
-
const
|
|
30243
|
+
const loadTemplate = () => this.getWorkingMemoryTemplate({ memoryConfig });
|
|
30244
|
+
const workingMemoryTemplate = runState ? await runState.load("working-memory:template", loadTemplate) : await loadTemplate();
|
|
30222
30245
|
const workingMemoryData = await this.getWorkingMemory({
|
|
30223
30246
|
threadId,
|
|
30224
30247
|
resourceId,
|
|
30225
|
-
memoryConfig: config
|
|
30248
|
+
memoryConfig: config,
|
|
30249
|
+
runState
|
|
30226
30250
|
});
|
|
30227
30251
|
if (!workingMemoryTemplate) return null;
|
|
30228
30252
|
const workingMemoryConfig = config.workingMemory;
|
|
@@ -30255,7 +30279,7 @@ ${workingMemory}`;
|
|
|
30255
30279
|
* ```
|
|
30256
30280
|
*/
|
|
30257
30281
|
async getContext(opts) {
|
|
30258
|
-
const { threadId, resourceId, memoryConfig } = opts;
|
|
30282
|
+
const { threadId, resourceId, memoryConfig, runState } = opts;
|
|
30259
30283
|
const config = this.getMergedThreadConfig(memoryConfig);
|
|
30260
30284
|
const memoryStore = await this.getMemoryStore();
|
|
30261
30285
|
const systemParts = [];
|
|
@@ -30265,10 +30289,14 @@ ${workingMemory}`;
|
|
|
30265
30289
|
let otherThreadsContext;
|
|
30266
30290
|
const omEngine = await this.omEngine;
|
|
30267
30291
|
if (omEngine) {
|
|
30268
|
-
|
|
30292
|
+
const loadOmRecord = () => omEngine.getRecord(threadId, resourceId);
|
|
30293
|
+
omRecord = runState ? await runState.load(`observational-memory:record:${threadId}:${resourceId ?? ""}`, loadOmRecord) : await loadOmRecord();
|
|
30269
30294
|
if (omRecord?.activeObservations) {
|
|
30270
30295
|
hasObservations = true;
|
|
30271
|
-
if (omEngine.scope === "resource" && resourceId)
|
|
30296
|
+
if (omEngine.scope === "resource" && resourceId) {
|
|
30297
|
+
const loadOtherThreadsContext = () => omEngine.getOtherThreadsContext(resourceId, threadId);
|
|
30298
|
+
otherThreadsContext = runState ? await runState.load(`observational-memory:other-threads:${resourceId}:${threadId}:${omRecord.lastObservedAt ?? ""}`, loadOtherThreadsContext) : await loadOtherThreadsContext();
|
|
30299
|
+
}
|
|
30272
30300
|
const obsSystemMessage = await omEngine.buildContextSystemMessage({
|
|
30273
30301
|
threadId,
|
|
30274
30302
|
resourceId,
|
|
@@ -30296,30 +30324,41 @@ ${workingMemory}`;
|
|
|
30296
30324
|
const workingMemoryMessage = await this.getSystemMessage({
|
|
30297
30325
|
threadId,
|
|
30298
30326
|
resourceId,
|
|
30299
|
-
memoryConfig: config
|
|
30327
|
+
memoryConfig: config,
|
|
30328
|
+
runState
|
|
30300
30329
|
});
|
|
30301
30330
|
if (workingMemoryMessage) systemParts.push(workingMemoryMessage);
|
|
30302
30331
|
let messages;
|
|
30303
30332
|
if (omEngine && omRecord) {
|
|
30304
30333
|
const dateFilter = omRecord.lastObservedAt ? { dateRange: { start: new Date(new Date(omRecord.lastObservedAt).getTime() + 1) } } : void 0;
|
|
30305
|
-
|
|
30306
|
-
|
|
30307
|
-
|
|
30308
|
-
|
|
30309
|
-
|
|
30310
|
-
|
|
30311
|
-
|
|
30312
|
-
|
|
30313
|
-
|
|
30314
|
-
|
|
30315
|
-
|
|
30316
|
-
|
|
30317
|
-
|
|
30318
|
-
|
|
30319
|
-
|
|
30320
|
-
|
|
30321
|
-
|
|
30322
|
-
|
|
30334
|
+
const boundary = omRecord.lastObservedAt ? new Date(omRecord.lastObservedAt).toISOString() : "";
|
|
30335
|
+
if (omEngine.scope === "resource" && resourceId) {
|
|
30336
|
+
const loadMessages = async () => {
|
|
30337
|
+
return (await memoryStore.listMessagesByResourceId({
|
|
30338
|
+
resourceId,
|
|
30339
|
+
orderBy: {
|
|
30340
|
+
field: "createdAt",
|
|
30341
|
+
direction: "ASC"
|
|
30342
|
+
},
|
|
30343
|
+
perPage: false,
|
|
30344
|
+
filter: dateFilter
|
|
30345
|
+
})).messages;
|
|
30346
|
+
};
|
|
30347
|
+
messages = runState ? await runState.load(`observational-memory:messages:resource:${resourceId}:${boundary}`, loadMessages) : await loadMessages();
|
|
30348
|
+
} else {
|
|
30349
|
+
const loadMessages = async () => {
|
|
30350
|
+
return (await memoryStore.listMessages({
|
|
30351
|
+
threadId,
|
|
30352
|
+
orderBy: {
|
|
30353
|
+
field: "createdAt",
|
|
30354
|
+
direction: "ASC"
|
|
30355
|
+
},
|
|
30356
|
+
perPage: false,
|
|
30357
|
+
filter: dateFilter
|
|
30358
|
+
})).messages;
|
|
30359
|
+
};
|
|
30360
|
+
messages = runState ? await runState.load(`observational-memory:messages:thread:${threadId}:${boundary}`, loadMessages) : await loadMessages();
|
|
30361
|
+
}
|
|
30323
30362
|
} else {
|
|
30324
30363
|
const lastMessages = config.lastMessages;
|
|
30325
30364
|
if (lastMessages === false) messages = [];
|
|
@@ -31331,4 +31370,4 @@ Notes:
|
|
|
31331
31370
|
//#endregion
|
|
31332
31371
|
export { formatMessagesForObserver as A, OBSERVATION_CONTINUATION_HINT as B, SUMMARIZE_THREAD_DEFAULTS as C, buildObserverPrompt as D, OBSERVER_SYSTEM_PROMPT as E, parseAnchorId as F, ModelByInputTokens as G, KnowledgeSemanticIndexCoordinator as H, stripEphemeralAnchorIds as I, SUBCONSCIOUS_ACTIVITY_STATE_ID as J, SubconsciousCaptureExtractor as K, OBSERVATIONAL_MEMORY_DEFAULTS as L, optimizeObservationsForContext as M, parseObserverOutput as N, buildObserverSystemPrompt as O, injectAnchorIds as P, Extractor as Q, OBSERVATION_CONTEXT_INSTRUCTIONS as R, WorkingMemoryExtractor as S, TokenCounter as T, StaleKnowledgeSemanticIndexError as U, Subconscious as V, SubconsciousRemindExtractor as W, publishSubconsciousActivity as X, buildSubconsciousActivitySnapshot as Y, renderSubconsciousActivity as Z, reconcileObservationGroupsFromReflection as _, extractWorkingMemoryContent as a, wrapInObservationGroup as b, WORKING_MEMORY_STATE_ID as c, getObservationsAsOf as d, ObservationalMemoryProcessor as f, parseObservationGroups as g, deriveObservationGroupProvenance as h, WorkingMemory as i, hasCurrentTaskSection as j, extractCurrentTask as k, WORKING_MEMORY_STATE_PROCESSOR_ID as l, combineObservationGroupRanges as m, MessageHistory$1 as n, extractWorkingMemoryTags as o, ObservationalMemory as p, subconsciousCaptureSchema as q, SemanticRecall as r, removeWorkingMemoryTags as s, Memory as t, WorkingMemoryStateProcessor as u, renderObservationGroupsForReflection as v, summarizeConversation as w, deepMergeWorkingMemory as x, stripObservationGroups as y, OBSERVATION_CONTEXT_PROMPT as z };
|
|
31333
31372
|
|
|
31334
|
-
//# sourceMappingURL=src-
|
|
31373
|
+
//# sourceMappingURL=src-BlQjkJzk.js.map
|