@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
|
@@ -19276,7 +19276,19 @@ async function withOmTracingSpan({ phase, model, inputTokens, requestContext, ob
|
|
|
19276
19276
|
});
|
|
19277
19277
|
const childObservabilityContext = (0, _mastra_core_observability.createObservabilityContext)({ currentSpan: span });
|
|
19278
19278
|
if (!span) return callback(childObservabilityContext);
|
|
19279
|
-
return span.executeInContext(() =>
|
|
19279
|
+
return span.executeInContext(async () => {
|
|
19280
|
+
try {
|
|
19281
|
+
const result = await callback(childObservabilityContext);
|
|
19282
|
+
span.end();
|
|
19283
|
+
return result;
|
|
19284
|
+
} catch (error) {
|
|
19285
|
+
span.error({
|
|
19286
|
+
error,
|
|
19287
|
+
endSpan: true
|
|
19288
|
+
});
|
|
19289
|
+
throw error;
|
|
19290
|
+
}
|
|
19291
|
+
});
|
|
19280
19292
|
}
|
|
19281
19293
|
//#endregion
|
|
19282
19294
|
//#region src/processors/observational-memory/observer-runner.ts
|
|
@@ -24413,10 +24425,11 @@ ObservationStrategy.create = ((om, opts) => {
|
|
|
24413
24425
|
});
|
|
24414
24426
|
//#endregion
|
|
24415
24427
|
//#region src/processors/observational-memory/observation-turn/load-memory-context.ts
|
|
24416
|
-
async function loadMemoryContextMessages({ memory, messageList, threadId, resourceId }) {
|
|
24428
|
+
async function loadMemoryContextMessages({ memory, messageList, threadId, resourceId, runState }) {
|
|
24417
24429
|
const ctx = await memory.getContext({
|
|
24418
24430
|
threadId,
|
|
24419
|
-
resourceId
|
|
24431
|
+
resourceId,
|
|
24432
|
+
runState
|
|
24420
24433
|
});
|
|
24421
24434
|
for (const msg of ctx.messages) if (msg.role !== "system") messageList.add(msg, "memory");
|
|
24422
24435
|
return ctx;
|
|
@@ -24874,10 +24887,11 @@ var ObservationTurn = class {
|
|
|
24874
24887
|
* If a MemoryContextProvider is passed, loads historical messages and adds
|
|
24875
24888
|
* them to the MessageList. Without a provider, only fetches/caches the record.
|
|
24876
24889
|
*/
|
|
24877
|
-
async start(memory) {
|
|
24890
|
+
async start(memory, runState) {
|
|
24878
24891
|
if (this._started) throw new Error("Turn already started");
|
|
24879
24892
|
this._started = true;
|
|
24880
24893
|
this._record = await this.om.getOrCreateRecord(this.threadId, this.resourceId);
|
|
24894
|
+
runState?.set(`observational-memory:record:${this.threadId}:${this.resourceId ?? ""}`, this._record);
|
|
24881
24895
|
this._generationCountAtStart = this._record.generationCount;
|
|
24882
24896
|
this.memory = memory;
|
|
24883
24897
|
if (memory) {
|
|
@@ -24885,7 +24899,8 @@ var ObservationTurn = class {
|
|
|
24885
24899
|
memory,
|
|
24886
24900
|
messageList: this.messageList,
|
|
24887
24901
|
threadId: this.threadId,
|
|
24888
|
-
resourceId: this.resourceId
|
|
24902
|
+
resourceId: this.resourceId,
|
|
24903
|
+
runState
|
|
24889
24904
|
});
|
|
24890
24905
|
this._context = {
|
|
24891
24906
|
messages: ctx.messages,
|
|
@@ -28967,7 +28982,9 @@ var ObservationalMemoryProcessor = class {
|
|
|
28967
28982
|
return messageList;
|
|
28968
28983
|
}
|
|
28969
28984
|
const { threadId, resourceId } = context;
|
|
28970
|
-
const
|
|
28985
|
+
const memoryContext = (0, _mastra_core_memory.parseMemoryRequestContext)(requestContext);
|
|
28986
|
+
const runState = memoryContext?.runState?.();
|
|
28987
|
+
const readOnly = memoryContext?.memoryConfig?.readOnly;
|
|
28971
28988
|
const actorModelContext = model?.modelId ? {
|
|
28972
28989
|
provider: model.provider,
|
|
28973
28990
|
modelId: model.modelId,
|
|
@@ -28984,7 +29001,8 @@ var ObservationalMemoryProcessor = class {
|
|
|
28984
29001
|
memory: this.memory,
|
|
28985
29002
|
messageList,
|
|
28986
29003
|
threadId,
|
|
28987
|
-
resourceId
|
|
29004
|
+
resourceId,
|
|
29005
|
+
runState
|
|
28988
29006
|
});
|
|
28989
29007
|
const systemMessages = ctx.omRecord ? await this.engine.buildContextSystemMessages({
|
|
28990
29008
|
threadId,
|
|
@@ -29025,7 +29043,7 @@ var ObservationalMemoryProcessor = class {
|
|
|
29025
29043
|
this.turn.sendStateSignal = args.sendStateSignal;
|
|
29026
29044
|
this.turn.agent = args.agent;
|
|
29027
29045
|
this.turn.requestContext = requestContext;
|
|
29028
|
-
await this.turn.start(this.memory);
|
|
29046
|
+
await this.turn.start(this.memory, runState);
|
|
29029
29047
|
if (stepNumber === 0 && this.temporalMarkers) await insertTemporalGapMarkers({
|
|
29030
29048
|
messageList,
|
|
29031
29049
|
sendSignal: args.sendSignal
|
|
@@ -30195,14 +30213,18 @@ ${workingMemory}`;
|
|
|
30195
30213
|
if (!this.threadConfig.workingMemory?.enabled) return null;
|
|
30196
30214
|
return extractWorkingMemoryContent(text)?.trim() ?? null;
|
|
30197
30215
|
}
|
|
30198
|
-
async getWorkingMemory({ threadId, resourceId, memoryConfig }) {
|
|
30216
|
+
async getWorkingMemory({ threadId, resourceId, memoryConfig, runState }) {
|
|
30199
30217
|
const config = this.getMergedThreadConfig(memoryConfig || {});
|
|
30200
30218
|
if (!config.workingMemory?.enabled) return null;
|
|
30201
30219
|
const scope = config.workingMemory.scope || "resource";
|
|
30202
30220
|
let workingMemoryData = null;
|
|
30203
30221
|
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'.");
|
|
30204
|
-
if (scope === "resource" && resourceId)
|
|
30205
|
-
|
|
30222
|
+
if (scope === "resource" && resourceId) {
|
|
30223
|
+
const loadWorkingMemory = async () => {
|
|
30224
|
+
return (await (await this.getMemoryStore()).getResourceById({ resourceId }))?.workingMemory || null;
|
|
30225
|
+
};
|
|
30226
|
+
workingMemoryData = runState ? await runState.load(`working-memory:resource:${resourceId}`, loadWorkingMemory) : await loadWorkingMemory();
|
|
30227
|
+
} else workingMemoryData = (runState?.threadLoaded ? runState.thread : await this.getThreadById({ threadId }))?.metadata?.workingMemory;
|
|
30206
30228
|
if (!workingMemoryData) return null;
|
|
30207
30229
|
return workingMemoryData;
|
|
30208
30230
|
}
|
|
@@ -30234,16 +30256,18 @@ ${workingMemory}`;
|
|
|
30234
30256
|
content: (config.workingMemory.template || this.defaultWorkingMemoryTemplate).trim()
|
|
30235
30257
|
};
|
|
30236
30258
|
}
|
|
30237
|
-
async getSystemMessage({ threadId, resourceId, memoryConfig }) {
|
|
30259
|
+
async getSystemMessage({ threadId, resourceId, memoryConfig, runState }) {
|
|
30238
30260
|
const config = this.getMergedThreadConfig(memoryConfig);
|
|
30239
30261
|
this.assertWorkingMemoryStateSignalsCompatibility(config);
|
|
30240
30262
|
if (!config.workingMemory?.enabled) return null;
|
|
30241
30263
|
if (config.workingMemory?.useStateSignals) return null;
|
|
30242
|
-
const
|
|
30264
|
+
const loadTemplate = () => this.getWorkingMemoryTemplate({ memoryConfig });
|
|
30265
|
+
const workingMemoryTemplate = runState ? await runState.load("working-memory:template", loadTemplate) : await loadTemplate();
|
|
30243
30266
|
const workingMemoryData = await this.getWorkingMemory({
|
|
30244
30267
|
threadId,
|
|
30245
30268
|
resourceId,
|
|
30246
|
-
memoryConfig: config
|
|
30269
|
+
memoryConfig: config,
|
|
30270
|
+
runState
|
|
30247
30271
|
});
|
|
30248
30272
|
if (!workingMemoryTemplate) return null;
|
|
30249
30273
|
const workingMemoryConfig = config.workingMemory;
|
|
@@ -30276,7 +30300,7 @@ ${workingMemory}`;
|
|
|
30276
30300
|
* ```
|
|
30277
30301
|
*/
|
|
30278
30302
|
async getContext(opts) {
|
|
30279
|
-
const { threadId, resourceId, memoryConfig } = opts;
|
|
30303
|
+
const { threadId, resourceId, memoryConfig, runState } = opts;
|
|
30280
30304
|
const config = this.getMergedThreadConfig(memoryConfig);
|
|
30281
30305
|
const memoryStore = await this.getMemoryStore();
|
|
30282
30306
|
const systemParts = [];
|
|
@@ -30286,10 +30310,14 @@ ${workingMemory}`;
|
|
|
30286
30310
|
let otherThreadsContext;
|
|
30287
30311
|
const omEngine = await this.omEngine;
|
|
30288
30312
|
if (omEngine) {
|
|
30289
|
-
|
|
30313
|
+
const loadOmRecord = () => omEngine.getRecord(threadId, resourceId);
|
|
30314
|
+
omRecord = runState ? await runState.load(`observational-memory:record:${threadId}:${resourceId ?? ""}`, loadOmRecord) : await loadOmRecord();
|
|
30290
30315
|
if (omRecord?.activeObservations) {
|
|
30291
30316
|
hasObservations = true;
|
|
30292
|
-
if (omEngine.scope === "resource" && resourceId)
|
|
30317
|
+
if (omEngine.scope === "resource" && resourceId) {
|
|
30318
|
+
const loadOtherThreadsContext = () => omEngine.getOtherThreadsContext(resourceId, threadId);
|
|
30319
|
+
otherThreadsContext = runState ? await runState.load(`observational-memory:other-threads:${resourceId}:${threadId}:${omRecord.lastObservedAt ?? ""}`, loadOtherThreadsContext) : await loadOtherThreadsContext();
|
|
30320
|
+
}
|
|
30293
30321
|
const obsSystemMessage = await omEngine.buildContextSystemMessage({
|
|
30294
30322
|
threadId,
|
|
30295
30323
|
resourceId,
|
|
@@ -30317,30 +30345,41 @@ ${workingMemory}`;
|
|
|
30317
30345
|
const workingMemoryMessage = await this.getSystemMessage({
|
|
30318
30346
|
threadId,
|
|
30319
30347
|
resourceId,
|
|
30320
|
-
memoryConfig: config
|
|
30348
|
+
memoryConfig: config,
|
|
30349
|
+
runState
|
|
30321
30350
|
});
|
|
30322
30351
|
if (workingMemoryMessage) systemParts.push(workingMemoryMessage);
|
|
30323
30352
|
let messages;
|
|
30324
30353
|
if (omEngine && omRecord) {
|
|
30325
30354
|
const dateFilter = omRecord.lastObservedAt ? { dateRange: { start: new Date(new Date(omRecord.lastObservedAt).getTime() + 1) } } : void 0;
|
|
30326
|
-
|
|
30327
|
-
|
|
30328
|
-
|
|
30329
|
-
|
|
30330
|
-
|
|
30331
|
-
|
|
30332
|
-
|
|
30333
|
-
|
|
30334
|
-
|
|
30335
|
-
|
|
30336
|
-
|
|
30337
|
-
|
|
30338
|
-
|
|
30339
|
-
|
|
30340
|
-
|
|
30341
|
-
|
|
30342
|
-
|
|
30343
|
-
|
|
30355
|
+
const boundary = omRecord.lastObservedAt ? new Date(omRecord.lastObservedAt).toISOString() : "";
|
|
30356
|
+
if (omEngine.scope === "resource" && resourceId) {
|
|
30357
|
+
const loadMessages = async () => {
|
|
30358
|
+
return (await memoryStore.listMessagesByResourceId({
|
|
30359
|
+
resourceId,
|
|
30360
|
+
orderBy: {
|
|
30361
|
+
field: "createdAt",
|
|
30362
|
+
direction: "ASC"
|
|
30363
|
+
},
|
|
30364
|
+
perPage: false,
|
|
30365
|
+
filter: dateFilter
|
|
30366
|
+
})).messages;
|
|
30367
|
+
};
|
|
30368
|
+
messages = runState ? await runState.load(`observational-memory:messages:resource:${resourceId}:${boundary}`, loadMessages) : await loadMessages();
|
|
30369
|
+
} else {
|
|
30370
|
+
const loadMessages = async () => {
|
|
30371
|
+
return (await memoryStore.listMessages({
|
|
30372
|
+
threadId,
|
|
30373
|
+
orderBy: {
|
|
30374
|
+
field: "createdAt",
|
|
30375
|
+
direction: "ASC"
|
|
30376
|
+
},
|
|
30377
|
+
perPage: false,
|
|
30378
|
+
filter: dateFilter
|
|
30379
|
+
})).messages;
|
|
30380
|
+
};
|
|
30381
|
+
messages = runState ? await runState.load(`observational-memory:messages:thread:${threadId}:${boundary}`, loadMessages) : await loadMessages();
|
|
30382
|
+
}
|
|
30344
30383
|
} else {
|
|
30345
30384
|
const lastMessages = config.lastMessages;
|
|
30346
30385
|
if (lastMessages === false) messages = [];
|
|
@@ -31645,4 +31684,4 @@ Object.defineProperty(exports, "wrapInObservationGroup", {
|
|
|
31645
31684
|
}
|
|
31646
31685
|
});
|
|
31647
31686
|
|
|
31648
|
-
//# sourceMappingURL=src-
|
|
31687
|
+
//# sourceMappingURL=src-bE5dqCVp.cjs.map
|