@mastra/memory 1.20.1 → 1.20.2
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 +48 -0
- package/dist/chunk-43FJOLKM.cjs +108 -0
- package/dist/chunk-43FJOLKM.cjs.map +1 -0
- package/dist/{chunk-3Q3LOVQP.cjs → chunk-D4J4XPGM.cjs} +2 -9
- package/dist/chunk-D4J4XPGM.cjs.map +1 -0
- package/dist/{chunk-VKRZ3O5X.js → chunk-LSJJAJAF.js} +3 -9
- package/dist/chunk-LSJJAJAF.js.map +1 -0
- package/dist/{chunk-P4BYW7GY.cjs → chunk-PTCSCRB6.cjs} +27 -27
- package/dist/{chunk-P4BYW7GY.cjs.map → chunk-PTCSCRB6.cjs.map} +1 -1
- package/dist/chunk-PZ5AY32C.js +9 -0
- package/dist/chunk-PZ5AY32C.js.map +1 -0
- package/dist/chunk-Q7SFCCGT.cjs +11 -0
- package/dist/chunk-Q7SFCCGT.cjs.map +1 -0
- package/dist/{chunk-KMJQDCCZ.js → chunk-SXFOZU7K.js} +3 -3
- package/dist/{chunk-KMJQDCCZ.js.map → chunk-SXFOZU7K.js.map} +1 -1
- package/dist/chunk-WCGXQIEN.js +103 -0
- package/dist/chunk-WCGXQIEN.js.map +1 -0
- package/dist/constants-DVRTNEGY.js +3 -0
- package/dist/{constants-SPS7MR4D.js.map → constants-DVRTNEGY.js.map} +1 -1
- package/dist/{constants-D6W7OWCP.cjs → constants-JS5SS7GS.cjs} +8 -8
- package/dist/{constants-D6W7OWCP.cjs.map → constants-JS5SS7GS.cjs.map} +1 -1
- package/dist/docs/SKILL.md +1 -1
- package/dist/docs/assets/SOURCE_MAP.json +43 -30
- package/dist/docs/references/docs-memory-multi-user-threads.md +1 -1
- package/dist/docs/references/docs-memory-working-memory.md +27 -0
- package/dist/docs/references/reference-storage-libsql.md +6 -0
- package/dist/docs/references/reference-storage-mongodb.md +3 -0
- package/dist/docs/references/reference-storage-postgresql.md +3 -0
- package/dist/index.cjs +222 -98
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +8 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +184 -72
- package/dist/index.js.map +1 -1
- package/dist/{observational-memory-TJFOX3YN.js → observational-memory-G52HNRHE.js} +4 -4
- package/dist/{observational-memory-TJFOX3YN.js.map → observational-memory-G52HNRHE.js.map} +1 -1
- package/dist/{observational-memory-CDLGZL67.cjs → observational-memory-SZF7GKTS.cjs} +31 -31
- package/dist/{observational-memory-CDLGZL67.cjs.map → observational-memory-SZF7GKTS.cjs.map} +1 -1
- package/dist/processors/index.cjs +29 -29
- package/dist/processors/index.js +2 -2
- package/dist/processors/observational-memory/index.d.ts +1 -1
- package/dist/processors/observational-memory/index.d.ts.map +1 -1
- package/dist/processors/working-memory-state/index.d.ts +2 -0
- package/dist/processors/working-memory-state/index.d.ts.map +1 -0
- package/dist/processors/working-memory-state/processor.d.ts +53 -0
- package/dist/processors/working-memory-state/processor.d.ts.map +1 -0
- package/dist/tools/working-memory.d.ts +22 -0
- package/dist/tools/working-memory.d.ts.map +1 -1
- package/dist/working-memory-state-J7ASTNXX.js +3 -0
- package/dist/working-memory-state-J7ASTNXX.js.map +1 -0
- package/dist/working-memory-state-Y2SCKCNU.cjs +24 -0
- package/dist/working-memory-state-Y2SCKCNU.cjs.map +1 -0
- package/package.json +8 -7
- package/dist/chunk-3Q3LOVQP.cjs.map +0 -1
- package/dist/chunk-VKRZ3O5X.js.map +0 -1
- package/dist/constants-SPS7MR4D.js +0 -3
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import { createHash } from 'crypto';
|
|
2
|
+
import { structuredPatch } from 'diff';
|
|
3
|
+
|
|
4
|
+
// src/processors/working-memory-state/processor.ts
|
|
5
|
+
var WORKING_MEMORY_STATE_ID = "working-memory";
|
|
6
|
+
var WORKING_MEMORY_STATE_PROCESSOR_ID = "working-memory-state";
|
|
7
|
+
var WorkingMemoryStateProcessor = class {
|
|
8
|
+
constructor(memory, memoryConfig) {
|
|
9
|
+
this.memory = memory;
|
|
10
|
+
this.memoryConfig = memoryConfig;
|
|
11
|
+
}
|
|
12
|
+
memory;
|
|
13
|
+
memoryConfig;
|
|
14
|
+
id = WORKING_MEMORY_STATE_PROCESSOR_ID;
|
|
15
|
+
stateId = WORKING_MEMORY_STATE_ID;
|
|
16
|
+
async computeStateSignal(args) {
|
|
17
|
+
const template = await this.memory.getWorkingMemoryTemplate({ memoryConfig: this.memoryConfig });
|
|
18
|
+
if (!template) return;
|
|
19
|
+
const data = await this.memory.getWorkingMemory({
|
|
20
|
+
threadId: args.threadId,
|
|
21
|
+
resourceId: args.resourceId,
|
|
22
|
+
memoryConfig: this.memoryConfig
|
|
23
|
+
});
|
|
24
|
+
const contents = data?.trim();
|
|
25
|
+
if (!contents) return;
|
|
26
|
+
const cacheKey = stableWorkingMemoryCacheKey({ format: template.format, data: contents });
|
|
27
|
+
const shouldMakeSnapshot = !args.contextWindow.hasSnapshot;
|
|
28
|
+
if (args.tracking?.currentCacheKey === cacheKey && !shouldMakeSnapshot) return;
|
|
29
|
+
const mergedConfig = this.memory.getMergedThreadConfig(this.memoryConfig);
|
|
30
|
+
const scope = mergedConfig.workingMemory?.scope ?? "resource";
|
|
31
|
+
const deltaCandidate = template.format === "markdown" && !shouldMakeSnapshot ? buildMarkdownDelta({
|
|
32
|
+
lastSnapshot: args.lastSnapshot,
|
|
33
|
+
deltasSinceSnapshot: args.deltasSinceSnapshot,
|
|
34
|
+
nextContents: contents
|
|
35
|
+
}) : void 0;
|
|
36
|
+
if (deltaCandidate) {
|
|
37
|
+
return {
|
|
38
|
+
id: WORKING_MEMORY_STATE_ID,
|
|
39
|
+
mode: "delta",
|
|
40
|
+
cacheKey,
|
|
41
|
+
tagName: "working-memory",
|
|
42
|
+
contents: deltaCandidate.contents,
|
|
43
|
+
delta: deltaCandidate.contents,
|
|
44
|
+
// Stash the full post-edit text on the signal so the next turn can
|
|
45
|
+
// diff against the most recently emitted state instead of the older
|
|
46
|
+
// snapshot. Invisible to the model.
|
|
47
|
+
value: contents,
|
|
48
|
+
attributes: {
|
|
49
|
+
format: template.format,
|
|
50
|
+
scope,
|
|
51
|
+
patch: "unified-diff"
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
return {
|
|
56
|
+
id: WORKING_MEMORY_STATE_ID,
|
|
57
|
+
mode: "snapshot",
|
|
58
|
+
cacheKey,
|
|
59
|
+
tagName: "working-memory",
|
|
60
|
+
contents,
|
|
61
|
+
// Mirror contents in value so the first delta after a snapshot has a
|
|
62
|
+
// typed prior-state to diff against without falling back to contents.
|
|
63
|
+
value: contents,
|
|
64
|
+
attributes: {
|
|
65
|
+
format: template.format,
|
|
66
|
+
scope
|
|
67
|
+
}
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
};
|
|
71
|
+
function stableWorkingMemoryCacheKey(input) {
|
|
72
|
+
const hash = createHash("sha256");
|
|
73
|
+
hash.update(input.format);
|
|
74
|
+
hash.update("\0");
|
|
75
|
+
hash.update(input.data ?? "");
|
|
76
|
+
return `sha256:${hash.digest("hex")}`;
|
|
77
|
+
}
|
|
78
|
+
function buildMarkdownDelta(args) {
|
|
79
|
+
const { lastSnapshot, deltasSinceSnapshot, nextContents } = args;
|
|
80
|
+
const latestDelta = deltasSinceSnapshot.at(-1);
|
|
81
|
+
const prior = pickStringValue(readSignalValue(latestDelta)) ?? pickStringValue(readSignalValue(lastSnapshot)) ?? (typeof lastSnapshot?.contents === "string" ? lastSnapshot.contents : void 0);
|
|
82
|
+
if (!prior) return;
|
|
83
|
+
const patch = renderHunksOnly(prior, nextContents);
|
|
84
|
+
return { contents: patch };
|
|
85
|
+
}
|
|
86
|
+
function pickStringValue(value) {
|
|
87
|
+
return typeof value === "string" ? value : void 0;
|
|
88
|
+
}
|
|
89
|
+
function readSignalValue(signal) {
|
|
90
|
+
return signal?.metadata?.value;
|
|
91
|
+
}
|
|
92
|
+
function renderHunksOnly(prior, next) {
|
|
93
|
+
const { hunks } = structuredPatch("", "", prior, next, "", "", { context: 0 });
|
|
94
|
+
return hunks.map((hunk) => {
|
|
95
|
+
const header = `@@ -${hunk.oldStart},${hunk.oldLines} +${hunk.newStart},${hunk.newLines} @@`;
|
|
96
|
+
const lines = hunk.lines.filter((line) => !line.startsWith("\"));
|
|
97
|
+
return [header, ...lines].join("\n");
|
|
98
|
+
}).join("\n");
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
export { WORKING_MEMORY_STATE_ID, WORKING_MEMORY_STATE_PROCESSOR_ID, WorkingMemoryStateProcessor, stableWorkingMemoryCacheKey };
|
|
102
|
+
//# sourceMappingURL=chunk-WCGXQIEN.js.map
|
|
103
|
+
//# sourceMappingURL=chunk-WCGXQIEN.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/processors/working-memory-state/processor.ts"],"names":[],"mappings":";;;;AA2CO,IAAM,uBAAA,GAA0B;AAChC,IAAM,iCAAA,GAAoC;AAE1C,IAAM,8BAAN,MAAiG;AAAA,EAItG,WAAA,CACmB,QACA,YAAA,EACjB;AAFiB,IAAA,IAAA,CAAA,MAAA,GAAA,MAAA;AACA,IAAA,IAAA,CAAA,YAAA,GAAA,YAAA;AAAA,EAChB;AAAA,EAFgB,MAAA;AAAA,EACA,YAAA;AAAA,EALV,EAAA,GAAK,iCAAA;AAAA,EACL,OAAA,GAAU,uBAAA;AAAA,EAOnB,MAAM,mBAAmB,IAAA,EAAiE;AACxF,IAAA,MAAM,QAAA,GAAW,MAAM,IAAA,CAAK,MAAA,CAAO,yBAAyB,EAAE,YAAA,EAAc,IAAA,CAAK,YAAA,EAAc,CAAA;AAC/F,IAAA,IAAI,CAAC,QAAA,EAAU;AAEf,IAAA,MAAM,IAAA,GAAO,MAAM,IAAA,CAAK,MAAA,CAAO,gBAAA,CAAiB;AAAA,MAC9C,UAAU,IAAA,CAAK,QAAA;AAAA,MACf,YAAY,IAAA,CAA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AAA,EAAyD;AAChF,EAAA,OAAQ,QAAQ,QAAA,EAA8C,KAAA;AAChE;AAWA,SAAS,eAAA,CAAgB,OAAe,IAAA,EAAsB;AAC5D,EAAA,MAAM,EAAE,KAAA,EAAM,GAAI,eAAA,CAAgB,EAAA,EAAI,EAAA,EAAI,KAAA,EAAO,IAAA,EAAM,EAAA,EAAI,EAAA,EAAI,EAAE,OAAA,EAAS,GAAG,CAAA;AAC7E,EAAA,OAAO,KAAA,CACJ,IAAI,CAAA,IAAA,KAAQ;AACX,IAAA,MAAM,MAAA,GAAS,CAAA,IAAA,EAAO,IAAA,CAAK,QAAQ,CAAA,CAAA,EAAI,IAAA,CAAK,QAAQ,CAAA,EAAA,EAAK,IAAA,CAAK,QAAQ,CAAA,CAAA,EAAI,IAAA,CAAK,QAAQ,CAAA,GAAA,CAAA;AACvF,IAAA,MAAM,KAAA,GAAQ,KAAK,KAAA,CAAM,MAAA,CAAO,UAAQ,CAAC,IAAA,CAAK,UAAA,CAAW,8BAA8B,CAAC,CAAA;AACxF,IAAA,OAAO,CAAC,MAAA,EAAQ,GAAG,KAAK,CAAA,CAAE,KAAK,IAAI,CAAA;AAAA,EACrC,CAAC,CAAA,CACA,IAAA,CAAK,IAAI,CAAA;AACd","file":"chunk-WCGXQIEN.js","sourcesContent":["/**\n * WorkingMemoryStateProcessor\n *\n * Experimental: delivers working memory to the model as a state signal instead\n * of folding it into the system message. Storage and the `setWorkingMemory`\n * tool are unchanged — this processor only changes the delivery path.\n *\n * Pattern matches `BrowserContextProcessor` in `@mastra/core/browser`:\n * - `stateId` namespaces the state lane on the thread.\n * - `cacheKey` is derived from the rendered payload so dedup is automatic.\n * - `contextWindow.hasSnapshot` re-injection ensures the model still sees the\n * current snapshot after older messages drop out of the window.\n *\n * Delta emission (markdown mode only): when a prior snapshot exists in the\n * context window, the processor emits a unified-diff delta against that\n * snapshot's contents. Schema mode and the snapshot fallback always emit a\n * full snapshot.\n *\n * @example\n * ```ts\n * new Memory({\n * options: {\n * workingMemory: {\n * enabled: true,\n * template: '...',\n * useStateSignals: true, // auto-attaches this processor\n * },\n * },\n * });\n * ```\n */\n\nimport { createHash } from 'node:crypto';\n\nimport type { MastraMemory, MemoryConfigInternal, WorkingMemoryTemplate } from '@mastra/core/memory';\nimport type {\n ComputeStateSignalArgs,\n ComputeStateSignalResult,\n Processor,\n ProcessorActiveStateSignal,\n} from '@mastra/core/processors';\nimport { structuredPatch } from 'diff';\n\nexport const WORKING_MEMORY_STATE_ID = 'working-memory';\nexport const WORKING_MEMORY_STATE_PROCESSOR_ID = 'working-memory-state';\n\nexport class WorkingMemoryStateProcessor implements Processor<typeof WORKING_MEMORY_STATE_PROCESSOR_ID> {\n readonly id = WORKING_MEMORY_STATE_PROCESSOR_ID;\n readonly stateId = WORKING_MEMORY_STATE_ID;\n\n constructor(\n private readonly memory: MastraMemory,\n private readonly memoryConfig?: MemoryConfigInternal,\n ) {}\n\n async computeStateSignal(args: ComputeStateSignalArgs): Promise<ComputeStateSignalResult> {\n const template = await this.memory.getWorkingMemoryTemplate({ memoryConfig: this.memoryConfig });\n if (!template) return;\n\n const data = await this.memory.getWorkingMemory({\n threadId: args.threadId,\n resourceId: args.resourceId,\n memoryConfig: this.memoryConfig,\n });\n\n // Nothing stored yet — no state to broadcast. The setWorkingMemory tool\n // description tells the model the expected shape; the signal carries state.\n const contents = data?.trim();\n if (!contents) return;\n\n const cacheKey = stableWorkingMemoryCacheKey({ format: template.format, data: contents });\n const shouldMakeSnapshot = !args.contextWindow.hasSnapshot;\n if (args.tracking?.currentCacheKey === cacheKey && !shouldMakeSnapshot) return;\n\n const mergedConfig = this.memory.getMergedThreadConfig(this.memoryConfig);\n const scope = mergedConfig.workingMemory?.scope ?? 'resource';\n\n const deltaCandidate =\n template.format === 'markdown' && !shouldMakeSnapshot\n ? buildMarkdownDelta({\n lastSnapshot: args.lastSnapshot,\n deltasSinceSnapshot: args.deltasSinceSnapshot,\n nextContents: contents,\n })\n : undefined;\n\n if (deltaCandidate) {\n return {\n id: WORKING_MEMORY_STATE_ID,\n mode: 'delta',\n cacheKey,\n tagName: 'working-memory',\n contents: deltaCandidate.contents,\n delta: deltaCandidate.contents,\n // Stash the full post-edit text on the signal so the next turn can\n // diff against the most recently emitted state instead of the older\n // snapshot. Invisible to the model.\n value: contents,\n attributes: {\n format: template.format,\n scope,\n patch: 'unified-diff',\n },\n };\n }\n\n return {\n id: WORKING_MEMORY_STATE_ID,\n mode: 'snapshot',\n cacheKey,\n tagName: 'working-memory',\n contents,\n // Mirror contents in value so the first delta after a snapshot has a\n // typed prior-state to diff against without falling back to contents.\n value: contents,\n attributes: {\n format: template.format,\n scope,\n },\n };\n }\n}\n\n/**\n * Stable cache key for the rendered working memory payload. Returns a SHA-256\n * digest so dedup metadata stays compact regardless of payload size (working\n * memory blobs can grow arbitrarily long).\n */\nexport function stableWorkingMemoryCacheKey(input: {\n format: WorkingMemoryTemplate['format'];\n data: string | null;\n}): string {\n const hash = createHash('sha256');\n hash.update(input.format);\n hash.update('\\0');\n hash.update(input.data ?? '');\n return `sha256:${hash.digest('hex')}`;\n}\n\n/**\n * Build a unified-diff delta against the most recently emitted state. Prefers\n * the latest delta's `value` (the post-edit full text) when available, falling\n * back to the snapshot's `value` and finally the snapshot's `contents`. This\n * keeps deltas incremental (B→C) instead of cumulative against a stale\n * snapshot (A→C), which matters when many small edits land between snapshots.\n *\n * Returns undefined when:\n * - there's no prior state to diff against\n * - the prior state isn't a plain string (multimodal signal)\n *\n * In either case the caller falls back to emitting a full snapshot.\n */\nfunction buildMarkdownDelta(args: {\n lastSnapshot?: ProcessorActiveStateSignal;\n deltasSinceSnapshot: ProcessorActiveStateSignal[];\n nextContents: string;\n}): { contents: string } | undefined {\n const { lastSnapshot, deltasSinceSnapshot, nextContents } = args;\n\n // `value` is stored on the persisted signal's metadata (see applyStateSignal\n // in @mastra/core/agent/state-signals). Read from there to recover the\n // post-edit full text from the most recently emitted state.\n const latestDelta = deltasSinceSnapshot.at(-1);\n const prior =\n pickStringValue(readSignalValue(latestDelta)) ??\n pickStringValue(readSignalValue(lastSnapshot)) ??\n (typeof lastSnapshot?.contents === 'string' ? lastSnapshot.contents : undefined);\n ('');\n if (!prior) return;\n\n const patch = renderHunksOnly(prior, nextContents);\n\n return { contents: patch };\n}\n\nfunction pickStringValue(value: unknown): string | undefined {\n return typeof value === 'string' ? value : undefined;\n}\n\nfunction readSignalValue(signal: ProcessorActiveStateSignal | undefined): unknown {\n return (signal?.metadata as { value?: unknown } | undefined)?.value;\n}\n\n/**\n * Render a unified-diff-style patch body containing only `@@` hunks and their\n * lines — dropping the filename preamble (`Index:` / `===` / `---` / `+++`)\n * that `createPatch` emits and the `\` trailer.\n * The preamble exists for tooling like `patch -p1` to know which file to\n * apply to; we only ever diff a single working-memory blob. The newline\n * trailer is semantically meaningless to the model and adds noise to the\n * state signal.\n */\nfunction renderHunksOnly(prior: string, next: string): string {\n const { hunks } = structuredPatch('', '', prior, next, '', '', { context: 0 });\n return hunks\n .map(hunk => {\n const header = `@@ -${hunk.oldStart},${hunk.oldLines} +${hunk.newStart},${hunk.newLines} @@`;\n const lines = hunk.lines.filter(line => !line.startsWith('\\\'));\n return [header, ...lines].join('\\n');\n })\n .join('\\n');\n}\n"]}
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
export { OBSERVATIONAL_MEMORY_DEFAULTS, OBSERVATION_CONTEXT_INSTRUCTIONS, OBSERVATION_CONTEXT_PROMPT, OBSERVATION_CONTINUATION_HINT, OBSERVATION_RETRIEVAL_INSTRUCTIONS } from './chunk-LSJJAJAF.js';
|
|
2
|
+
//# sourceMappingURL=constants-DVRTNEGY.js.map
|
|
3
|
+
//# sourceMappingURL=constants-DVRTNEGY.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":[],"names":[],"mappings":"","file":"constants-
|
|
1
|
+
{"version":3,"sources":[],"names":[],"mappings":"","file":"constants-DVRTNEGY.js"}
|
|
@@ -1,28 +1,28 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var
|
|
3
|
+
var chunkD4J4XPGM_cjs = require('./chunk-D4J4XPGM.cjs');
|
|
4
4
|
|
|
5
5
|
|
|
6
6
|
|
|
7
7
|
Object.defineProperty(exports, "OBSERVATIONAL_MEMORY_DEFAULTS", {
|
|
8
8
|
enumerable: true,
|
|
9
|
-
get: function () { return
|
|
9
|
+
get: function () { return chunkD4J4XPGM_cjs.OBSERVATIONAL_MEMORY_DEFAULTS; }
|
|
10
10
|
});
|
|
11
11
|
Object.defineProperty(exports, "OBSERVATION_CONTEXT_INSTRUCTIONS", {
|
|
12
12
|
enumerable: true,
|
|
13
|
-
get: function () { return
|
|
13
|
+
get: function () { return chunkD4J4XPGM_cjs.OBSERVATION_CONTEXT_INSTRUCTIONS; }
|
|
14
14
|
});
|
|
15
15
|
Object.defineProperty(exports, "OBSERVATION_CONTEXT_PROMPT", {
|
|
16
16
|
enumerable: true,
|
|
17
|
-
get: function () { return
|
|
17
|
+
get: function () { return chunkD4J4XPGM_cjs.OBSERVATION_CONTEXT_PROMPT; }
|
|
18
18
|
});
|
|
19
19
|
Object.defineProperty(exports, "OBSERVATION_CONTINUATION_HINT", {
|
|
20
20
|
enumerable: true,
|
|
21
|
-
get: function () { return
|
|
21
|
+
get: function () { return chunkD4J4XPGM_cjs.OBSERVATION_CONTINUATION_HINT; }
|
|
22
22
|
});
|
|
23
23
|
Object.defineProperty(exports, "OBSERVATION_RETRIEVAL_INSTRUCTIONS", {
|
|
24
24
|
enumerable: true,
|
|
25
|
-
get: function () { return
|
|
25
|
+
get: function () { return chunkD4J4XPGM_cjs.OBSERVATION_RETRIEVAL_INSTRUCTIONS; }
|
|
26
26
|
});
|
|
27
|
-
//# sourceMappingURL=constants-
|
|
28
|
-
//# sourceMappingURL=constants-
|
|
27
|
+
//# sourceMappingURL=constants-JS5SS7GS.cjs.map
|
|
28
|
+
//# sourceMappingURL=constants-JS5SS7GS.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":[],"names":[],"mappings":"","file":"constants-
|
|
1
|
+
{"version":3,"sources":[],"names":[],"mappings":"","file":"constants-JS5SS7GS.cjs"}
|
package/dist/docs/SKILL.md
CHANGED
|
@@ -1,136 +1,149 @@
|
|
|
1
1
|
{
|
|
2
|
-
"version": "1.20.
|
|
2
|
+
"version": "1.20.2",
|
|
3
3
|
"package": "@mastra/memory",
|
|
4
4
|
"exports": {
|
|
5
5
|
"ModelByInputTokens": {
|
|
6
6
|
"types": "dist/processors/index.d.ts",
|
|
7
|
-
"implementation": "dist/chunk-
|
|
7
|
+
"implementation": "dist/chunk-SXFOZU7K.js",
|
|
8
8
|
"line": 787
|
|
9
9
|
},
|
|
10
10
|
"OBSERVER_SYSTEM_PROMPT": {
|
|
11
11
|
"types": "dist/processors/index.d.ts",
|
|
12
|
-
"implementation": "dist/chunk-
|
|
12
|
+
"implementation": "dist/chunk-SXFOZU7K.js"
|
|
13
13
|
},
|
|
14
14
|
"ObservationalMemory": {
|
|
15
15
|
"types": "dist/processors/index.d.ts",
|
|
16
|
-
"implementation": "dist/chunk-
|
|
16
|
+
"implementation": "dist/chunk-SXFOZU7K.js",
|
|
17
17
|
"line": 7175
|
|
18
18
|
},
|
|
19
19
|
"ObservationalMemoryProcessor": {
|
|
20
20
|
"types": "dist/processors/index.d.ts",
|
|
21
|
-
"implementation": "dist/chunk-
|
|
21
|
+
"implementation": "dist/chunk-SXFOZU7K.js",
|
|
22
22
|
"line": 9747
|
|
23
23
|
},
|
|
24
24
|
"TokenCounter": {
|
|
25
25
|
"types": "dist/processors/index.d.ts",
|
|
26
|
-
"implementation": "dist/chunk-
|
|
26
|
+
"implementation": "dist/chunk-SXFOZU7K.js",
|
|
27
27
|
"line": 6506
|
|
28
28
|
},
|
|
29
29
|
"buildObserverPrompt": {
|
|
30
30
|
"types": "dist/processors/index.d.ts",
|
|
31
|
-
"implementation": "dist/chunk-
|
|
31
|
+
"implementation": "dist/chunk-SXFOZU7K.js",
|
|
32
32
|
"line": 3774
|
|
33
33
|
},
|
|
34
34
|
"buildObserverSystemPrompt": {
|
|
35
35
|
"types": "dist/processors/index.d.ts",
|
|
36
|
-
"implementation": "dist/chunk-
|
|
36
|
+
"implementation": "dist/chunk-SXFOZU7K.js",
|
|
37
37
|
"line": 3045
|
|
38
38
|
},
|
|
39
39
|
"combineObservationGroupRanges": {
|
|
40
40
|
"types": "dist/processors/index.d.ts",
|
|
41
|
-
"implementation": "dist/chunk-
|
|
41
|
+
"implementation": "dist/chunk-SXFOZU7K.js",
|
|
42
42
|
"line": 879
|
|
43
43
|
},
|
|
44
44
|
"deriveObservationGroupProvenance": {
|
|
45
45
|
"types": "dist/processors/index.d.ts",
|
|
46
|
-
"implementation": "dist/chunk-
|
|
46
|
+
"implementation": "dist/chunk-SXFOZU7K.js",
|
|
47
47
|
"line": 913
|
|
48
48
|
},
|
|
49
49
|
"extractCurrentTask": {
|
|
50
50
|
"types": "dist/processors/index.d.ts",
|
|
51
|
-
"implementation": "dist/chunk-
|
|
51
|
+
"implementation": "dist/chunk-SXFOZU7K.js",
|
|
52
52
|
"line": 3888
|
|
53
53
|
},
|
|
54
54
|
"formatMessagesForObserver": {
|
|
55
55
|
"types": "dist/processors/index.d.ts",
|
|
56
|
-
"implementation": "dist/chunk-
|
|
56
|
+
"implementation": "dist/chunk-SXFOZU7K.js",
|
|
57
57
|
"line": 3500
|
|
58
58
|
},
|
|
59
59
|
"getObservationsAsOf": {
|
|
60
60
|
"types": "dist/processors/index.d.ts",
|
|
61
|
-
"implementation": "dist/chunk-
|
|
61
|
+
"implementation": "dist/chunk-SXFOZU7K.js",
|
|
62
62
|
"line": 9960
|
|
63
63
|
},
|
|
64
64
|
"hasCurrentTaskSection": {
|
|
65
65
|
"types": "dist/processors/index.d.ts",
|
|
66
|
-
"implementation": "dist/chunk-
|
|
66
|
+
"implementation": "dist/chunk-SXFOZU7K.js",
|
|
67
67
|
"line": 3876
|
|
68
68
|
},
|
|
69
69
|
"injectAnchorIds": {
|
|
70
70
|
"types": "dist/processors/index.d.ts",
|
|
71
|
-
"implementation": "dist/chunk-
|
|
71
|
+
"implementation": "dist/chunk-SXFOZU7K.js",
|
|
72
72
|
"line": 2593
|
|
73
73
|
},
|
|
74
74
|
"optimizeObservationsForContext": {
|
|
75
75
|
"types": "dist/processors/index.d.ts",
|
|
76
|
-
"implementation": "dist/chunk-
|
|
76
|
+
"implementation": "dist/chunk-SXFOZU7K.js",
|
|
77
77
|
"line": 3899
|
|
78
78
|
},
|
|
79
79
|
"parseAnchorId": {
|
|
80
80
|
"types": "dist/processors/index.d.ts",
|
|
81
|
-
"implementation": "dist/chunk-
|
|
81
|
+
"implementation": "dist/chunk-SXFOZU7K.js",
|
|
82
82
|
"line": 2566
|
|
83
83
|
},
|
|
84
84
|
"parseObservationGroups": {
|
|
85
85
|
"types": "dist/processors/index.d.ts",
|
|
86
|
-
"implementation": "dist/chunk-
|
|
86
|
+
"implementation": "dist/chunk-SXFOZU7K.js",
|
|
87
87
|
"line": 848
|
|
88
88
|
},
|
|
89
89
|
"parseObserverOutput": {
|
|
90
90
|
"types": "dist/processors/index.d.ts",
|
|
91
|
-
"implementation": "dist/chunk-
|
|
91
|
+
"implementation": "dist/chunk-SXFOZU7K.js",
|
|
92
92
|
"line": 3784
|
|
93
93
|
},
|
|
94
94
|
"reconcileObservationGroupsFromReflection": {
|
|
95
95
|
"types": "dist/processors/index.d.ts",
|
|
96
|
-
"implementation": "dist/chunk-
|
|
96
|
+
"implementation": "dist/chunk-SXFOZU7K.js",
|
|
97
97
|
"line": 937
|
|
98
98
|
},
|
|
99
99
|
"renderObservationGroupsForReflection": {
|
|
100
100
|
"types": "dist/processors/index.d.ts",
|
|
101
|
-
"implementation": "dist/chunk-
|
|
101
|
+
"implementation": "dist/chunk-SXFOZU7K.js",
|
|
102
102
|
"line": 893
|
|
103
103
|
},
|
|
104
104
|
"stripEphemeralAnchorIds": {
|
|
105
105
|
"types": "dist/processors/index.d.ts",
|
|
106
|
-
"implementation": "dist/chunk-
|
|
106
|
+
"implementation": "dist/chunk-SXFOZU7K.js",
|
|
107
107
|
"line": 2623
|
|
108
108
|
},
|
|
109
109
|
"stripObservationGroups": {
|
|
110
110
|
"types": "dist/processors/index.d.ts",
|
|
111
|
-
"implementation": "dist/chunk-
|
|
111
|
+
"implementation": "dist/chunk-SXFOZU7K.js",
|
|
112
112
|
"line": 870
|
|
113
113
|
},
|
|
114
114
|
"wrapInObservationGroup": {
|
|
115
115
|
"types": "dist/processors/index.d.ts",
|
|
116
|
-
"implementation": "dist/chunk-
|
|
116
|
+
"implementation": "dist/chunk-SXFOZU7K.js",
|
|
117
117
|
"line": 841
|
|
118
118
|
},
|
|
119
119
|
"OBSERVATIONAL_MEMORY_DEFAULTS": {
|
|
120
120
|
"types": "dist/processors/index.d.ts",
|
|
121
|
-
"implementation": "dist/chunk-
|
|
121
|
+
"implementation": "dist/chunk-LSJJAJAF.js"
|
|
122
122
|
},
|
|
123
123
|
"OBSERVATION_CONTEXT_INSTRUCTIONS": {
|
|
124
124
|
"types": "dist/processors/index.d.ts",
|
|
125
|
-
"implementation": "dist/chunk-
|
|
125
|
+
"implementation": "dist/chunk-LSJJAJAF.js"
|
|
126
126
|
},
|
|
127
127
|
"OBSERVATION_CONTEXT_PROMPT": {
|
|
128
128
|
"types": "dist/processors/index.d.ts",
|
|
129
|
-
"implementation": "dist/chunk-
|
|
129
|
+
"implementation": "dist/chunk-LSJJAJAF.js"
|
|
130
130
|
},
|
|
131
131
|
"OBSERVATION_CONTINUATION_HINT": {
|
|
132
132
|
"types": "dist/processors/index.d.ts",
|
|
133
|
-
"implementation": "dist/chunk-
|
|
133
|
+
"implementation": "dist/chunk-LSJJAJAF.js"
|
|
134
|
+
},
|
|
135
|
+
"WORKING_MEMORY_STATE_ID": {
|
|
136
|
+
"types": "dist/index.d.ts",
|
|
137
|
+
"implementation": "dist/chunk-WCGXQIEN.js"
|
|
138
|
+
},
|
|
139
|
+
"WORKING_MEMORY_STATE_PROCESSOR_ID": {
|
|
140
|
+
"types": "dist/index.d.ts",
|
|
141
|
+
"implementation": "dist/chunk-WCGXQIEN.js"
|
|
142
|
+
},
|
|
143
|
+
"WorkingMemoryStateProcessor": {
|
|
144
|
+
"types": "dist/index.d.ts",
|
|
145
|
+
"implementation": "dist/chunk-WCGXQIEN.js",
|
|
146
|
+
"line": 7
|
|
134
147
|
},
|
|
135
148
|
"MessageHistory": {
|
|
136
149
|
"types": "dist/index.d.ts",
|
|
@@ -149,8 +162,8 @@
|
|
|
149
162
|
"processors": {
|
|
150
163
|
"index": "dist/processors/index.js",
|
|
151
164
|
"chunks": [
|
|
152
|
-
"chunk-
|
|
153
|
-
"chunk-
|
|
165
|
+
"chunk-SXFOZU7K.js",
|
|
166
|
+
"chunk-LSJJAJAF.js"
|
|
154
167
|
]
|
|
155
168
|
}
|
|
156
169
|
}
|
|
@@ -135,7 +135,7 @@ const memory = new Memory({
|
|
|
135
135
|
|
|
136
136
|
The model reads identity from the `<turn>` tag on the current message and from prior tagged messages brought back through `lastMessages`.
|
|
137
137
|
|
|
138
|
-
### With
|
|
138
|
+
### With Observational Memory (recommended)
|
|
139
139
|
|
|
140
140
|
[Observational Memory](https://mastra.ai/docs/memory/observational-memory) (OM) extracts per-user facts into a background log without burning the agent's tool budget. The default Observer model reads `<turn>` tags natively and produces named attribution like `Alice stated her favorite color is teal.` and `Bob asked for QA sign-off before publish.`
|
|
141
141
|
|
|
@@ -393,6 +393,33 @@ const response = await agent.generate('What do you know about me?', {
|
|
|
393
393
|
})
|
|
394
394
|
```
|
|
395
395
|
|
|
396
|
+
## Opt in to state signals (experimental)
|
|
397
|
+
|
|
398
|
+
By default, working memory reaches the model as part of the system message. You can opt into delivering it as a [state signal](https://mastra.ai/docs/agents/signals) instead by setting `useStateSignals: true`:
|
|
399
|
+
|
|
400
|
+
```typescript
|
|
401
|
+
const memory = new Memory({
|
|
402
|
+
storage: new LibSQLStore({ url: 'file:./mastra.db' }),
|
|
403
|
+
options: {
|
|
404
|
+
workingMemory: {
|
|
405
|
+
enabled: true,
|
|
406
|
+
template: '# User\n- name:\n- location:',
|
|
407
|
+
useStateSignals: true, // experimental: deliver as state signal
|
|
408
|
+
},
|
|
409
|
+
},
|
|
410
|
+
})
|
|
411
|
+
```
|
|
412
|
+
|
|
413
|
+
What changes:
|
|
414
|
+
|
|
415
|
+
- **Storage is identical.** The same resource/thread `workingMemory` field is read and written.
|
|
416
|
+
- **The tool is the same shape, exposed under a new name.** Writes still flow through the same underlying tool; on this path it is registered as `setWorkingMemory` (instead of `updateWorkingMemory`). The rename keeps legacy strip filters from removing tool-call parts so they persist as a normal audit trail and the next model step picks the new value up automatically.
|
|
417
|
+
- **Delivery only.** Instead of folding into the system prompt, `Memory` auto-attaches a `WorkingMemoryStateProcessor` that emits the current working memory as a `state` signal with `stateId: 'working-memory'`.
|
|
418
|
+
|
|
419
|
+
You inherit the standard state-signal benefits: thread-scoped tracking metadata, `cacheKey` dedup so identical snapshots are only emitted once, and `contextWindow.hasSnapshot` re-injection when an older snapshot rolls out of the window.
|
|
420
|
+
|
|
421
|
+
The default (`useStateSignals: false`) keeps the existing system-message behavior unchanged. `useStateSignals` is not supported with template working memory `version: 'vnext'`.
|
|
422
|
+
|
|
396
423
|
## Examples
|
|
397
424
|
|
|
398
425
|
- [Working memory with template](https://github.com/mastra-ai/mastra/tree/main/examples/memory-with-template)
|
|
@@ -93,6 +93,12 @@ storage: new LibSQLStore({
|
|
|
93
93
|
|
|
94
94
|
**authToken** (`string`): Authentication token for remote libSQL databases.
|
|
95
95
|
|
|
96
|
+
## Managed tables
|
|
97
|
+
|
|
98
|
+
The storage implementation creates the core storage tables automatically, including `mastra_notifications` for notification inbox records and delivery metadata.
|
|
99
|
+
|
|
100
|
+
`LibSQLStore` exposes notification storage through `getStore('notifications')`.
|
|
101
|
+
|
|
96
102
|
## Initialization
|
|
97
103
|
|
|
98
104
|
When you pass storage to the Mastra class, `init()` is called automatically to create the [core schema](https://mastra.ai/reference/storage/overview):
|
|
@@ -97,6 +97,9 @@ The storage implementation handles collection creation and management automatica
|
|
|
97
97
|
- `mastra_traces`: Stores telemetry and tracing data
|
|
98
98
|
- `mastra_scorers`: Stores scoring and evaluation data
|
|
99
99
|
- `mastra_resources`: Stores resource working memory data
|
|
100
|
+
- `mastra_notifications`: Stores notification inbox records and delivery metadata
|
|
101
|
+
|
|
102
|
+
`MongoDBStore` exposes notification storage through `getStore('notifications')`.
|
|
100
103
|
|
|
101
104
|
### Initialization
|
|
102
105
|
|
|
@@ -132,6 +132,9 @@ The storage implementation handles schema creation and updates automatically. It
|
|
|
132
132
|
- `mastra_traces`: Stores telemetry and tracing data
|
|
133
133
|
- `mastra_scorers`: Stores scoring and evaluation data
|
|
134
134
|
- `mastra_resources`: Stores resource working memory data
|
|
135
|
+
- `mastra_notifications`: Stores notification inbox records and delivery metadata
|
|
136
|
+
|
|
137
|
+
`PostgresStore` exposes notification storage through `getStore('notifications')`.
|
|
135
138
|
|
|
136
139
|
### Observability
|
|
137
140
|
|