@mastra/memory 1.36.0 → 1.37.0-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/dist/docs/SKILL.md +1 -1
- package/dist/docs/assets/SOURCE_MAP.json +1 -1
- package/dist/docs/references/docs-agents-human-in-the-loop.md +5 -3
- package/dist/docs/references/docs-memory-memory-processors.md +2 -5
- package/dist/docs/references/docs-memory-semantic-recall.md +2 -2
- package/dist/docs/references/docs-memory-working-memory.md +16 -0
- package/dist/docs/references/docs-subagents.md +10 -8
- package/dist/docs/references/reference-memory-memory-class.md +2 -2
- package/dist/docs/references/reference-migrations-upgrade-to-v1-memory.md +1 -1
- package/dist/index.cjs +1 -1
- package/dist/index.d.ts +18 -31
- 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/step.d.ts.map +1 -1
- package/dist/processors/observational-memory/processor.d.ts.map +1 -1
- package/dist/{src-yhShj4JX.js → src-C_ZgETf8.js} +105 -15
- package/dist/{src-yhShj4JX.js.map → src-C_ZgETf8.js.map} +1 -1
- package/dist/{src-DwfyRK9Q.cjs → src-DCWwc78Q.cjs} +105 -15
- package/dist/src-DCWwc78Q.cjs.map +1 -0
- package/dist/tools/working-memory.d.ts +4 -0
- package/dist/tools/working-memory.d.ts.map +1 -1
- package/package.json +3 -3
- package/dist/src-DwfyRK9Q.cjs.map +0 -1
|
@@ -34,6 +34,7 @@ let zod = require("zod");
|
|
|
34
34
|
let zod_v4 = require("zod/v4");
|
|
35
35
|
zod_v4 = __toESM$1(zod_v4, 1);
|
|
36
36
|
let _mastra_core_agent = require("@mastra/core/agent");
|
|
37
|
+
let _mastra_core_error = require("@mastra/core/error");
|
|
37
38
|
let _mastra_core_features = require("@mastra/core/features");
|
|
38
39
|
let _mastra_core_memory = require("@mastra/core/memory");
|
|
39
40
|
let _mastra_core_observability = require("@mastra/core/observability");
|
|
@@ -55,7 +56,6 @@ let crypto$1 = require("crypto");
|
|
|
55
56
|
let probe_image_size_sync_js = require("probe-image-size/sync.js");
|
|
56
57
|
probe_image_size_sync_js = __toESM$1(probe_image_size_sync_js, 1);
|
|
57
58
|
let _mastra_core_schema = require("@mastra/core/schema");
|
|
58
|
-
let _mastra_core_error = require("@mastra/core/error");
|
|
59
59
|
let diff = require("diff");
|
|
60
60
|
let util = require("util");
|
|
61
61
|
//#region ../_vendored/ai_v4/dist/dist-Cm5DZgxA.js
|
|
@@ -23619,6 +23619,18 @@ const SET_WORKING_MEMORY_TOOL_NAME = "setWorkingMemory";
|
|
|
23619
23619
|
* - Primitive values are overwritten
|
|
23620
23620
|
* - The returned object is always newly constructed and never aliases `update`
|
|
23621
23621
|
*/
|
|
23622
|
+
/**
|
|
23623
|
+
* Parses stored JSON working memory. Invalid or empty data yields `null` (start fresh).
|
|
23624
|
+
*/
|
|
23625
|
+
function parseWorkingMemoryJson(raw) {
|
|
23626
|
+
if (!raw) return null;
|
|
23627
|
+
if (typeof raw !== "string") return raw;
|
|
23628
|
+
try {
|
|
23629
|
+
return JSON.parse(raw);
|
|
23630
|
+
} catch {
|
|
23631
|
+
return null;
|
|
23632
|
+
}
|
|
23633
|
+
}
|
|
23622
23634
|
function deepMergeWorkingMemory(existing, update) {
|
|
23623
23635
|
if (!update || typeof update !== "object" || Object.keys(update).length === 0) return existing && typeof existing === "object" ? { ...existing } : {};
|
|
23624
23636
|
const result = { ...existing && typeof existing === "object" && !Array.isArray(existing) ? existing : {} };
|
|
@@ -23725,17 +23737,6 @@ const updateWorkingMemoryTool = (memoryConfig) => {
|
|
|
23725
23737
|
}
|
|
23726
23738
|
let workingMemory;
|
|
23727
23739
|
if (usesMergeSemantics) {
|
|
23728
|
-
const existingRaw = await memory.getWorkingMemory({
|
|
23729
|
-
threadId,
|
|
23730
|
-
resourceId,
|
|
23731
|
-
memoryConfig
|
|
23732
|
-
});
|
|
23733
|
-
let existingData = null;
|
|
23734
|
-
if (existingRaw) try {
|
|
23735
|
-
existingData = typeof existingRaw === "string" ? JSON.parse(existingRaw) : existingRaw;
|
|
23736
|
-
} catch {
|
|
23737
|
-
existingData = null;
|
|
23738
|
-
}
|
|
23739
23740
|
const memoryInput = workingMemoryInput.memory;
|
|
23740
23741
|
if (memoryInput === void 0 || memoryInput === null) return {
|
|
23741
23742
|
success: true,
|
|
@@ -23749,7 +23750,20 @@ const updateWorkingMemoryTool = (memoryConfig) => {
|
|
|
23749
23750
|
throw new Error(`Failed to parse working memory input as JSON: ${errorMessage}. Raw input: ${memoryInput.length > 500 ? memoryInput.slice(0, 500) + "..." : memoryInput}`);
|
|
23750
23751
|
}
|
|
23751
23752
|
else newData = memoryInput;
|
|
23752
|
-
|
|
23753
|
+
if (scope === "resource" && typeof memory.supportsAtomicWorkingMemoryMerge === "function" && await memory.supportsAtomicWorkingMemoryMerge()) {
|
|
23754
|
+
await memory.mergeWorkingMemory({
|
|
23755
|
+
threadId,
|
|
23756
|
+
resourceId,
|
|
23757
|
+
workingMemory: newData,
|
|
23758
|
+
memoryConfig
|
|
23759
|
+
});
|
|
23760
|
+
return { success: true };
|
|
23761
|
+
}
|
|
23762
|
+
const mergedData = deepMergeWorkingMemory(parseWorkingMemoryJson(await memory.getWorkingMemory({
|
|
23763
|
+
threadId,
|
|
23764
|
+
resourceId,
|
|
23765
|
+
memoryConfig
|
|
23766
|
+
})), newData);
|
|
23753
23767
|
workingMemory = JSON.stringify(mergedData);
|
|
23754
23768
|
} else {
|
|
23755
23769
|
const memoryInput = workingMemoryInput.memory;
|
|
@@ -28114,8 +28128,14 @@ var ObservationStep = class {
|
|
|
28114
28128
|
const newOutput = messageList.clear.response.db();
|
|
28115
28129
|
const messagesToSave = [...newInput, ...newOutput];
|
|
28116
28130
|
if (messagesToSave.length > 0) {
|
|
28117
|
-
await om.persistMessages(messagesToSave, threadId, resourceId);
|
|
28118
28131
|
for (const msg of messagesToSave) messageList.add(msg, "memory");
|
|
28132
|
+
try {
|
|
28133
|
+
await om.persistMessages(messagesToSave, threadId, resourceId);
|
|
28134
|
+
} catch (error) {
|
|
28135
|
+
restoreDrainedMessages(messageList, newInput, "input");
|
|
28136
|
+
restoreDrainedMessages(messageList, newOutput, "response");
|
|
28137
|
+
throw error;
|
|
28138
|
+
}
|
|
28119
28139
|
}
|
|
28120
28140
|
} else {
|
|
28121
28141
|
const pending = [...messageList.get.input.db(), ...messageList.get.response.db()];
|
|
@@ -28342,6 +28362,18 @@ var ObservationStep = class {
|
|
|
28342
28362
|
};
|
|
28343
28363
|
}
|
|
28344
28364
|
};
|
|
28365
|
+
/**
|
|
28366
|
+
* Move messages drained into the memory bucket back to `source` after a failed save. Messages that
|
|
28367
|
+
* have already left the memory bucket (a background tool result moves its message to `response`)
|
|
28368
|
+
* are left where they are. The list's current copy is re-added so changes made during the save
|
|
28369
|
+
* are kept.
|
|
28370
|
+
*/
|
|
28371
|
+
function restoreDrainedMessages(messageList, drained, source) {
|
|
28372
|
+
if (drained.length === 0) return;
|
|
28373
|
+
const stillQueued = new Set([...messageList.get.input.db(), ...messageList.get.response.db()].map((msg) => msg.id));
|
|
28374
|
+
const ids = drained.map((msg) => msg.id).filter((id) => !stillQueued.has(id));
|
|
28375
|
+
for (const msg of messageList.removeByIds(ids)) messageList.add(msg, source, { merge: false });
|
|
28376
|
+
}
|
|
28345
28377
|
//#endregion
|
|
28346
28378
|
//#region src/processors/observational-memory/observation-turn/turn.ts
|
|
28347
28379
|
/**
|
|
@@ -32846,6 +32878,10 @@ var ObservationalMemoryProcessor = class {
|
|
|
32846
32878
|
} catch (error) {
|
|
32847
32879
|
const err = error instanceof Error ? error : new Error(String(error));
|
|
32848
32880
|
const abortMessage = abortSignal?.aborted ? "Agent execution was aborted" : `Encountered error during memory observation: ${err.message}`;
|
|
32881
|
+
const failedTurn = this.turn;
|
|
32882
|
+
await failedTurn.end().catch(() => {});
|
|
32883
|
+
if (this.turn === failedTurn) this.turn = void 0;
|
|
32884
|
+
state.__omTurn = void 0;
|
|
32849
32885
|
if (typeof abort === "function") abort(abortMessage);
|
|
32850
32886
|
throw err;
|
|
32851
32887
|
}
|
|
@@ -33177,6 +33213,12 @@ const DEFAULT_EMBEDDING_CACHE_MAX_SIZE = 1e3;
|
|
|
33177
33213
|
* @see [Memory documentation](https://mastra.ai/docs/memory/overview)
|
|
33178
33214
|
* if packaged docs are unavailable.
|
|
33179
33215
|
*/
|
|
33216
|
+
const invalidMerge = (text) => new _mastra_core_error.MastraError({
|
|
33217
|
+
id: "MEMORY_WORKING_MEMORY_MERGE_INVALID",
|
|
33218
|
+
domain: _mastra_core_error.ErrorDomain.MASTRA_MEMORY,
|
|
33219
|
+
category: _mastra_core_error.ErrorCategory.USER,
|
|
33220
|
+
text
|
|
33221
|
+
});
|
|
33180
33222
|
var Memory = class Memory extends _mastra_core_memory.MastraMemory {
|
|
33181
33223
|
createMemoryTokenCounter() {
|
|
33182
33224
|
return new TokenCounter();
|
|
@@ -33706,6 +33748,54 @@ var Memory = class Memory extends _mastra_core_memory.MastraMemory {
|
|
|
33706
33748
|
throw error;
|
|
33707
33749
|
}
|
|
33708
33750
|
}
|
|
33751
|
+
/**
|
|
33752
|
+
* Whether the configured storage can merge resource working memory atomically across processes.
|
|
33753
|
+
*/
|
|
33754
|
+
async supportsAtomicWorkingMemoryMerge() {
|
|
33755
|
+
return (await this.getMemoryStore()).supportsAtomicWorkingMemoryMerge === true;
|
|
33756
|
+
}
|
|
33757
|
+
/**
|
|
33758
|
+
* Deep-merges a partial JSON update into resource-scoped working memory as one
|
|
33759
|
+
* atomic storage operation, so concurrent writers (even in other processes) never
|
|
33760
|
+
* lose each other's fields. `null` values delete fields; arrays are replaced.
|
|
33761
|
+
* Requires schema-based working memory, resource scope, and a storage adapter
|
|
33762
|
+
* that supports atomic merges (e.g. PostgreSQL); otherwise throws.
|
|
33763
|
+
*/
|
|
33764
|
+
async mergeWorkingMemory({ threadId, resourceId, workingMemory, memoryConfig, observabilityContext }) {
|
|
33765
|
+
const config = this.getMergedThreadConfig(memoryConfig || {});
|
|
33766
|
+
if (!config.workingMemory?.enabled) throw invalidMerge("Working memory is not enabled for this memory instance");
|
|
33767
|
+
if (!config.workingMemory.schema) throw invalidMerge("Working memory merge requires schema-based (JSON) working memory");
|
|
33768
|
+
if ((config.workingMemory.scope || "resource") !== "resource" || !resourceId) throw invalidMerge("Working memory merge requires resource-scoped working memory and a resourceId");
|
|
33769
|
+
let patch = workingMemory;
|
|
33770
|
+
if (typeof workingMemory === "string") try {
|
|
33771
|
+
patch = JSON.parse(workingMemory);
|
|
33772
|
+
} catch {
|
|
33773
|
+
throw invalidMerge("Working memory merge requires a JSON object");
|
|
33774
|
+
}
|
|
33775
|
+
if (!patch || typeof patch !== "object" || Array.isArray(patch)) throw invalidMerge("Working memory merge requires a JSON object");
|
|
33776
|
+
const memoryStore = await this.getMemoryStore();
|
|
33777
|
+
if (!memoryStore.supportsAtomicWorkingMemoryMerge) throw invalidMerge(`Atomic working memory merge is not supported by this storage adapter (${memoryStore.constructor.name}).`);
|
|
33778
|
+
const span = this.createMemorySpan("update", observabilityContext, {
|
|
33779
|
+
threadId,
|
|
33780
|
+
resourceId
|
|
33781
|
+
}, { workingMemoryEnabled: true });
|
|
33782
|
+
try {
|
|
33783
|
+
await memoryStore.mergeResourceWorkingMemory({
|
|
33784
|
+
resourceId,
|
|
33785
|
+
merge: (existing) => {
|
|
33786
|
+
const parsed = parseWorkingMemoryJson(existing);
|
|
33787
|
+
return JSON.stringify(deepMergeWorkingMemory(parsed && typeof parsed === "object" && !Array.isArray(parsed) ? parsed : null, patch));
|
|
33788
|
+
}
|
|
33789
|
+
});
|
|
33790
|
+
span?.end({ output: { success: true } });
|
|
33791
|
+
} catch (error) {
|
|
33792
|
+
span?.error({
|
|
33793
|
+
error,
|
|
33794
|
+
endSpan: true
|
|
33795
|
+
});
|
|
33796
|
+
throw error;
|
|
33797
|
+
}
|
|
33798
|
+
}
|
|
33709
33799
|
updateWorkingMemoryMutexes = /* @__PURE__ */ new Map();
|
|
33710
33800
|
/**
|
|
33711
33801
|
* @warning experimental! can be removed or changed at any time
|
|
@@ -35700,4 +35790,4 @@ Object.defineProperty(exports, "wrapInObservationGroup", {
|
|
|
35700
35790
|
}
|
|
35701
35791
|
});
|
|
35702
35792
|
|
|
35703
|
-
//# sourceMappingURL=src-
|
|
35793
|
+
//# sourceMappingURL=src-DCWwc78Q.cjs.map
|