@mastra/memory 1.41.3-alpha.1 → 1.41.3-alpha.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/dist/docs/SKILL.md +1 -1
- package/dist/docs/assets/SOURCE_MAP.json +71 -71
- package/dist/docs/references/docs-memory-observational-memory.md +3 -3
- package/dist/docs/references/docs-memory-working-memory.md +33 -0
- package/dist/hooks.cjs +0 -2
- package/dist/hooks.js +0 -2
- package/dist/index.cjs +1 -1
- package/dist/index.d.ts +26 -3
- package/dist/index.js +1 -1
- package/dist/processors/index.cjs +1 -1
- package/dist/processors/index.js +1 -1
- package/dist/processors/working-memory-instruction-compat.d.ts +54 -0
- package/dist/{src-CKA5LbDL.cjs → src-BC-wnTHt.cjs} +233 -41
- package/dist/{src-BvZXenIm.js → src-CRmfe-8x.js} +233 -41
- package/package.json +6 -5
- package/dist/hooks.cjs.map +0 -1
- package/dist/hooks.d.ts.map +0 -1
- package/dist/hooks.js.map +0 -1
- package/dist/index.d.ts.map +0 -1
- package/dist/persistence-projection.test-utils.d.ts.map +0 -1
- package/dist/processors/index.d.ts.map +0 -1
- package/dist/processors/observational-memory/activation-ttl.d.ts.map +0 -1
- package/dist/processors/observational-memory/anchor-ids.d.ts.map +0 -1
- package/dist/processors/observational-memory/auto-model.d.ts.map +0 -1
- package/dist/processors/observational-memory/buffering-coordinator.d.ts.map +0 -1
- package/dist/processors/observational-memory/built-in-extractors.d.ts.map +0 -1
- package/dist/processors/observational-memory/commit-queue.d.ts.map +0 -1
- package/dist/processors/observational-memory/constants.d.ts.map +0 -1
- package/dist/processors/observational-memory/date-utils.d.ts.map +0 -1
- package/dist/processors/observational-memory/debug.d.ts.map +0 -1
- package/dist/processors/observational-memory/error.d.ts.map +0 -1
- package/dist/processors/observational-memory/extracted-values.d.ts.map +0 -1
- package/dist/processors/observational-memory/extraction-runner.d.ts.map +0 -1
- package/dist/processors/observational-memory/extractor.d.ts.map +0 -1
- package/dist/processors/observational-memory/hooks.d.ts.map +0 -1
- package/dist/processors/observational-memory/index.d.ts.map +0 -1
- package/dist/processors/observational-memory/internal-request-context.d.ts.map +0 -1
- package/dist/processors/observational-memory/markers.d.ts.map +0 -1
- package/dist/processors/observational-memory/measure-image-buffer.d.ts.map +0 -1
- package/dist/processors/observational-memory/message-utils.d.ts.map +0 -1
- package/dist/processors/observational-memory/model-by-input-tokens.d.ts.map +0 -1
- package/dist/processors/observational-memory/model-context.d.ts.map +0 -1
- package/dist/processors/observational-memory/observation-groups.d.ts.map +0 -1
- package/dist/processors/observational-memory/observation-strategies/async-buffer.d.ts.map +0 -1
- package/dist/processors/observational-memory/observation-strategies/base.d.ts.map +0 -1
- package/dist/processors/observational-memory/observation-strategies/index.d.ts.map +0 -1
- package/dist/processors/observational-memory/observation-strategies/resource-scoped.d.ts.map +0 -1
- package/dist/processors/observational-memory/observation-strategies/sync.d.ts.map +0 -1
- package/dist/processors/observational-memory/observation-strategies/thread-title.d.ts.map +0 -1
- package/dist/processors/observational-memory/observation-strategies/types.d.ts.map +0 -1
- package/dist/processors/observational-memory/observation-turn/index.d.ts.map +0 -1
- package/dist/processors/observational-memory/observation-turn/load-memory-context.d.ts.map +0 -1
- package/dist/processors/observational-memory/observation-turn/safe-buffer-prefix.d.ts.map +0 -1
- package/dist/processors/observational-memory/observation-turn/step.d.ts.map +0 -1
- package/dist/processors/observational-memory/observation-turn/turn.d.ts.map +0 -1
- package/dist/processors/observational-memory/observation-turn/types.d.ts.map +0 -1
- package/dist/processors/observational-memory/observation-utils.d.ts.map +0 -1
- package/dist/processors/observational-memory/observational-memory.d.ts.map +0 -1
- package/dist/processors/observational-memory/observer-agent.d.ts.map +0 -1
- package/dist/processors/observational-memory/observer-runner.d.ts.map +0 -1
- package/dist/processors/observational-memory/operation-registry.d.ts.map +0 -1
- package/dist/processors/observational-memory/processor.d.ts.map +0 -1
- package/dist/processors/observational-memory/record-lineage.d.ts.map +0 -1
- package/dist/processors/observational-memory/reflector-agent.d.ts.map +0 -1
- package/dist/processors/observational-memory/reflector-runner.d.ts.map +0 -1
- package/dist/processors/observational-memory/repro-capture.d.ts.map +0 -1
- package/dist/processors/observational-memory/retry.d.ts.map +0 -1
- package/dist/processors/observational-memory/string-utils.d.ts.map +0 -1
- package/dist/processors/observational-memory/summarize.d.ts.map +0 -1
- package/dist/processors/observational-memory/temporal-markers.d.ts.map +0 -1
- package/dist/processors/observational-memory/temporary-memory.d.ts.map +0 -1
- package/dist/processors/observational-memory/thresholds.d.ts.map +0 -1
- package/dist/processors/observational-memory/token-counter.d.ts.map +0 -1
- package/dist/processors/observational-memory/tool-argument-helpers.d.ts.map +0 -1
- package/dist/processors/observational-memory/tool-result-helpers.d.ts.map +0 -1
- package/dist/processors/observational-memory/tracing.d.ts.map +0 -1
- package/dist/processors/observational-memory/transform-hooks.d.ts.map +0 -1
- package/dist/processors/observational-memory/types.d.ts.map +0 -1
- package/dist/processors/observational-memory/wait-with-abort.d.ts.map +0 -1
- package/dist/processors/observational-memory/working-memory-extractor.d.ts.map +0 -1
- package/dist/processors/working-memory-state/index.d.ts.map +0 -1
- package/dist/processors/working-memory-state/processor.d.ts.map +0 -1
- package/dist/removed-subconscious.d.ts.map +0 -1
- package/dist/src-BvZXenIm.js.map +0 -1
- package/dist/src-CKA5LbDL.cjs.map +0 -1
- package/dist/system-reminders.d.ts.map +0 -1
- package/dist/tools/om-observations.d.ts.map +0 -1
- package/dist/tools/om-search-context.d.ts.map +0 -1
- package/dist/tools/om-tools.d.ts.map +0 -1
- package/dist/tools/working-memory.d.ts.map +0 -1
|
@@ -30381,6 +30381,65 @@ var WorkingMemoryExtractor = class extends Extractor {
|
|
|
30381
30381
|
}, false, true);
|
|
30382
30382
|
}
|
|
30383
30383
|
};
|
|
30384
|
+
// src/processors/working-memory-instruction-compat.ts
|
|
30385
|
+
/**
|
|
30386
|
+
* Stand-in for core's `WorkingMemory` input processor on `@mastra/core` versions whose processor renders its own
|
|
30387
|
+
* built-in instruction text and has no instruction provider (every core before `getWorkingMemoryInstruction`
|
|
30388
|
+
* was added to `MastraMemory`, including 1.86.0-alpha.1 through 1.86.0-alpha.5). `Memory.getInputProcessors()`
|
|
30389
|
+
* swaps it in for the auto-created processor on those cores, so `Memory` subclass instruction overrides reach
|
|
30390
|
+
* the model there too (#29752).
|
|
30391
|
+
*
|
|
30392
|
+
* Loading and caching match core's `WorkingMemory.processInput` exactly; only the instruction comes from the
|
|
30393
|
+
* memory instance. Keep it in sync with packages/core/src/processors/memory/working-memory.ts.
|
|
30394
|
+
*/
|
|
30395
|
+
var WorkingMemoryInstructionCompat = class {
|
|
30396
|
+
options;
|
|
30397
|
+
id = "working-memory";
|
|
30398
|
+
name = "WorkingMemory";
|
|
30399
|
+
constructor(options) {
|
|
30400
|
+
this.options = options;
|
|
30401
|
+
}
|
|
30402
|
+
async processInput(args) {
|
|
30403
|
+
const { messageList, requestContext } = args;
|
|
30404
|
+
const memoryContext = (0, _mastra_core_memory.parseMemoryRequestContext)(requestContext);
|
|
30405
|
+
const threadId = memoryContext?.thread?.id;
|
|
30406
|
+
const resourceId = memoryContext?.resourceId;
|
|
30407
|
+
if (!memoryContext || !threadId && !resourceId) return messageList;
|
|
30408
|
+
const scope = this.options.scope || "resource";
|
|
30409
|
+
const memoryRunState = memoryContext.runState?.();
|
|
30410
|
+
let workingMemoryData = null;
|
|
30411
|
+
if (scope === "thread" && threadId) {
|
|
30412
|
+
const stored = (memoryRunState?.thread ?? await this.options.storage.getThreadById({ threadId }))?.metadata?.workingMemory;
|
|
30413
|
+
workingMemoryData = typeof stored === "string" && stored ? stored : null;
|
|
30414
|
+
} else if (scope === "resource" && resourceId) {
|
|
30415
|
+
const loadWorkingMemory = async () => {
|
|
30416
|
+
const resource = await this.options.storage.getResourceById({ resourceId });
|
|
30417
|
+
return typeof resource?.workingMemory === "string" && resource.workingMemory ? resource.workingMemory : null;
|
|
30418
|
+
};
|
|
30419
|
+
workingMemoryData = memoryRunState ? await memoryRunState.load(`working-memory:resource:${resourceId}`, loadWorkingMemory) : await loadWorkingMemory();
|
|
30420
|
+
}
|
|
30421
|
+
const loadTemplate = () => this.options.getWorkingMemoryTemplate({ memoryConfig: memoryContext.memoryConfig });
|
|
30422
|
+
const template = (memoryRunState ? await memoryRunState.load("working-memory:template", loadTemplate) : await loadTemplate()) || this.options.template || {
|
|
30423
|
+
format: "markdown",
|
|
30424
|
+
content: this.options.defaultTemplate
|
|
30425
|
+
};
|
|
30426
|
+
const workingMemoryConfig = memoryContext.memoryConfig?.workingMemory;
|
|
30427
|
+
const isConfiguredReadOnly = Boolean(this.options.readOnly || memoryContext.memoryConfig?.readOnly);
|
|
30428
|
+
const isAppManaged = this.options.agentManaged === false || typeof workingMemoryConfig === "object" && workingMemoryConfig.agentManaged === false;
|
|
30429
|
+
const instruction = this.options.renderInstruction(isConfiguredReadOnly || isAppManaged ? {
|
|
30430
|
+
kind: "read-only",
|
|
30431
|
+
template,
|
|
30432
|
+
data: workingMemoryData,
|
|
30433
|
+
autoUpdated: !isConfiguredReadOnly
|
|
30434
|
+
} : {
|
|
30435
|
+
kind: this.options.useVNext ? "vnext" : "tool",
|
|
30436
|
+
template,
|
|
30437
|
+
data: workingMemoryData
|
|
30438
|
+
});
|
|
30439
|
+
if (instruction) messageList.addSystem(instruction, "memory");
|
|
30440
|
+
return messageList;
|
|
30441
|
+
}
|
|
30442
|
+
};
|
|
30384
30443
|
// src/removed-subconscious.ts
|
|
30385
30444
|
/**
|
|
30386
30445
|
* Subconscious was removed. These stubs keep code written against older `@mastra/memory`
|
|
@@ -30514,13 +30573,57 @@ function getUnobservedPartsPreservingToolCallPairs(message) {
|
|
|
30514
30573
|
if (id) unobservedResultIds.add(id);
|
|
30515
30574
|
}
|
|
30516
30575
|
if (unobservedResultIds.size === 0) return unobservedParts;
|
|
30517
|
-
const
|
|
30576
|
+
const preserved = [];
|
|
30577
|
+
let pendingReasoning = [];
|
|
30578
|
+
let toolSinceReasoning = false;
|
|
30579
|
+
let keptAnything = false;
|
|
30580
|
+
for (let i = 0; i < endMarkerIndex; i++) {
|
|
30581
|
+
const part = parts[i];
|
|
30518
30582
|
const id = getToolInvocationId(part, "call");
|
|
30519
|
-
|
|
30520
|
-
|
|
30521
|
-
|
|
30583
|
+
if (id !== void 0 && unobservedResultIds.has(id)) {
|
|
30584
|
+
for (const index of pendingReasoning) preserved.push(parts[index]);
|
|
30585
|
+
pendingReasoning = [];
|
|
30586
|
+
toolSinceReasoning = true;
|
|
30587
|
+
preserved.push(part);
|
|
30588
|
+
keptAnything = true;
|
|
30589
|
+
continue;
|
|
30590
|
+
}
|
|
30591
|
+
if (part?.type === "reasoning" && hasReplayableReasoningMetadata(part)) {
|
|
30592
|
+
if (toolSinceReasoning) pendingReasoning = [];
|
|
30593
|
+
toolSinceReasoning = false;
|
|
30594
|
+
pendingReasoning.push(i);
|
|
30595
|
+
continue;
|
|
30596
|
+
}
|
|
30597
|
+
if (part?.type === "tool-invocation") {
|
|
30598
|
+
toolSinceReasoning = true;
|
|
30599
|
+
continue;
|
|
30600
|
+
}
|
|
30601
|
+
if (part?.type === "step-start") {
|
|
30602
|
+
if (toolSinceReasoning) pendingReasoning = [];
|
|
30603
|
+
continue;
|
|
30604
|
+
}
|
|
30605
|
+
if (part?.type === "text") continue;
|
|
30606
|
+
pendingReasoning = [];
|
|
30607
|
+
}
|
|
30608
|
+
return keptAnything ? [...preserved, ...unobservedParts] : unobservedParts;
|
|
30522
30609
|
}
|
|
30523
30610
|
/**
|
|
30611
|
+
* Reasoning part metadata a provider can replay (a signature, redacted payload, item id).
|
|
30612
|
+
* Mirrors `hasReplayableReasoningMetadata` in `@mastra/core`'s message-list utils.
|
|
30613
|
+
*/
|
|
30614
|
+
function hasReplayableReasoningMetadata(part) {
|
|
30615
|
+
const metadata = part.providerMetadata;
|
|
30616
|
+
if (!metadata || typeof metadata !== "object") return false;
|
|
30617
|
+
return Object.values(metadata).some((value) => !!value && typeof value === "object" && REPLAYABLE_REASONING_KEYS.some((key) => key in value));
|
|
30618
|
+
}
|
|
30619
|
+
const REPLAYABLE_REASONING_KEYS = [
|
|
30620
|
+
"signature",
|
|
30621
|
+
"redactedData",
|
|
30622
|
+
"itemId",
|
|
30623
|
+
"reasoningEncryptedContent",
|
|
30624
|
+
"thoughtSignature"
|
|
30625
|
+
];
|
|
30626
|
+
/**
|
|
30524
30627
|
* Get the messages Observational Memory is allowed to work with.
|
|
30525
30628
|
*
|
|
30526
30629
|
* Messages supplied through the `context` option and the synthetic `om-continuation`
|
|
@@ -40777,6 +40880,26 @@ function isTransientSignalMessage(message) {
|
|
|
40777
40880
|
const metadata = message.content.metadata;
|
|
40778
40881
|
return isRecord(metadata) && isRecord(metadata.signal) && !Array.isArray(metadata.signal) && metadata.signal.transient === true;
|
|
40779
40882
|
}
|
|
40883
|
+
const DEFAULT_WORKING_MEMORY_TEMPLATE = `
|
|
40884
|
+
# User Information
|
|
40885
|
+
- **First Name**:
|
|
40886
|
+
- **Last Name**:
|
|
40887
|
+
- **Location**:
|
|
40888
|
+
- **Occupation**:
|
|
40889
|
+
- **Interests**:
|
|
40890
|
+
- **Goals**:
|
|
40891
|
+
- **Events**:
|
|
40892
|
+
- **Facts**:
|
|
40893
|
+
- **Projects**:
|
|
40894
|
+
`;
|
|
40895
|
+
/**
|
|
40896
|
+
* True when the installed `@mastra/core` builds its WorkingMemory processor with the memory instance as the
|
|
40897
|
+
* instruction provider. Cores from before `MastraMemory.getWorkingMemoryInstruction` existed (up to and
|
|
40898
|
+
* including 1.86.0-alpha.5) don't, and render their own built-in text. Checked per call, not at import.
|
|
40899
|
+
*/
|
|
40900
|
+
function coreRendersWorkingMemoryInstructionThroughMemory() {
|
|
40901
|
+
return "getWorkingMemoryInstruction" in _mastra_core_memory.MastraMemory.prototype;
|
|
40902
|
+
}
|
|
40780
40903
|
function normalizeObservationalMemoryConfig(config) {
|
|
40781
40904
|
if (config === true) return { model: "auto" };
|
|
40782
40905
|
if (config === false || config === void 0) return void 0;
|
|
@@ -41708,10 +41831,14 @@ ${workingMemory}`;
|
|
|
41708
41831
|
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'.");
|
|
41709
41832
|
if (scope === "resource" && resourceId) {
|
|
41710
41833
|
const loadWorkingMemory = async () => {
|
|
41711
|
-
|
|
41834
|
+
const resource = await (await this.getMemoryStore()).getResourceById({ resourceId });
|
|
41835
|
+
return typeof resource?.workingMemory === "string" && resource.workingMemory ? resource.workingMemory : null;
|
|
41712
41836
|
};
|
|
41713
41837
|
workingMemoryData = runState ? await runState.load(`working-memory:resource:${resourceId}`, loadWorkingMemory) : await loadWorkingMemory();
|
|
41714
|
-
} else
|
|
41838
|
+
} else {
|
|
41839
|
+
const stored = (runState?.threadLoaded ? runState.thread : await this.getThreadById({ threadId }))?.metadata?.workingMemory;
|
|
41840
|
+
workingMemoryData = typeof stored === "string" && stored ? stored : null;
|
|
41841
|
+
}
|
|
41715
41842
|
if (!workingMemoryData) return null;
|
|
41716
41843
|
return workingMemoryData;
|
|
41717
41844
|
}
|
|
@@ -41758,18 +41885,17 @@ ${workingMemory}`;
|
|
|
41758
41885
|
});
|
|
41759
41886
|
if (!workingMemoryTemplate) return null;
|
|
41760
41887
|
const workingMemoryConfig = config.workingMemory;
|
|
41761
|
-
|
|
41888
|
+
const args = config?.readOnly || workingMemoryConfig.agentManaged === false ? {
|
|
41889
|
+
kind: "read-only",
|
|
41762
41890
|
template: workingMemoryTemplate,
|
|
41763
41891
|
data: workingMemoryData,
|
|
41764
41892
|
autoUpdated: !config?.readOnly
|
|
41765
|
-
}
|
|
41766
|
-
|
|
41767
|
-
template: workingMemoryTemplate,
|
|
41768
|
-
data: workingMemoryData
|
|
41769
|
-
}) : this.getWorkingMemoryToolInstruction({
|
|
41893
|
+
} : {
|
|
41894
|
+
kind: "version" in workingMemoryConfig && workingMemoryConfig.version === "vnext" ? "vnext" : "tool",
|
|
41770
41895
|
template: workingMemoryTemplate,
|
|
41771
41896
|
data: workingMemoryData
|
|
41772
|
-
}
|
|
41897
|
+
};
|
|
41898
|
+
return (this.getWorkingMemoryInstruction(args) ?? this.renderBuiltInWorkingMemoryInstruction(args)) || null;
|
|
41773
41899
|
}
|
|
41774
41900
|
/** Fetch current stored copies of messages in a resumed turn, bypassing run-state history caches. */
|
|
41775
41901
|
async getPersistedMessages(threadId, ids) {
|
|
@@ -42016,19 +42142,8 @@ ${workingMemory}`;
|
|
|
42016
42142
|
} : void 0
|
|
42017
42143
|
});
|
|
42018
42144
|
}
|
|
42019
|
-
defaultWorkingMemoryTemplate =
|
|
42020
|
-
|
|
42021
|
-
- **First Name**:
|
|
42022
|
-
- **Last Name**:
|
|
42023
|
-
- **Location**:
|
|
42024
|
-
- **Occupation**:
|
|
42025
|
-
- **Interests**:
|
|
42026
|
-
- **Goals**:
|
|
42027
|
-
- **Events**:
|
|
42028
|
-
- **Facts**:
|
|
42029
|
-
- **Projects**:
|
|
42030
|
-
`;
|
|
42031
|
-
getWorkingMemoryToolInstruction({ template, data }) {
|
|
42145
|
+
defaultWorkingMemoryTemplate = DEFAULT_WORKING_MEMORY_TEMPLATE;
|
|
42146
|
+
builtInWorkingMemoryToolInstruction({ template, data }) {
|
|
42032
42147
|
const emptyWorkingMemoryTemplateObject = template.format === "json" ? (0, _mastra_core_utils.generateEmptyFromSchema)(template.content) : null;
|
|
42033
42148
|
const hasEmptyWorkingMemoryTemplateObject = emptyWorkingMemoryTemplateObject && Object.keys(emptyWorkingMemoryTemplateObject).length > 0;
|
|
42034
42149
|
return `WORKING_MEMORY_SYSTEM_INSTRUCTION:
|
|
@@ -42064,7 +42179,8 @@ Notes:
|
|
|
42064
42179
|
- IMPORTANT: You MUST call updateWorkingMemory in every response to a prompt where you received relevant information.
|
|
42065
42180
|
- IMPORTANT: Preserve the ${template.format === "json" ? "JSON" : "Markdown"} formatting structure above while updating the content.`;
|
|
42066
42181
|
}
|
|
42067
|
-
|
|
42182
|
+
builtInWorkingMemoryToolInstructionVNext({ template, data }) {
|
|
42183
|
+
const templateContent = typeof template.content === "string" ? template.content : JSON.stringify(template.content);
|
|
42068
42184
|
return `WORKING_MEMORY_SYSTEM_INSTRUCTION:
|
|
42069
42185
|
Store and update any conversation-relevant information by calling the updateWorkingMemory tool.
|
|
42070
42186
|
|
|
@@ -42077,7 +42193,7 @@ Guidelines:
|
|
|
42077
42193
|
6. Information not being relevant to the current conversation is not a valid reason to replace or remove working memory information. Your working memory spans across multiple conversations and may be needed again later, even if it's not currently relevant.
|
|
42078
42194
|
|
|
42079
42195
|
<working_memory_template>
|
|
42080
|
-
${
|
|
42196
|
+
${templateContent}
|
|
42081
42197
|
</working_memory_template>
|
|
42082
42198
|
|
|
42083
42199
|
<working_memory_data>
|
|
@@ -42086,7 +42202,7 @@ ${data || "No working memory data available."}
|
|
|
42086
42202
|
|
|
42087
42203
|
Notes:
|
|
42088
42204
|
- Update memory whenever referenced information changes
|
|
42089
|
-
${
|
|
42205
|
+
${templateContent !== DEFAULT_WORKING_MEMORY_TEMPLATE ? `- Only store information if it's in the working memory template, do not store other information unless the user asks you to remember it, as that non-template information may be irrelevant` : `- If you're unsure whether to store something, store it (eg if the user tells you information about themselves, call updateWorkingMemory immediately to update it)
|
|
42090
42206
|
`}
|
|
42091
42207
|
- This system is here so that you can maintain the conversation when your context window is very short. Update your working memory because you may need it to maintain the conversation without the full conversation history
|
|
42092
42208
|
- REMEMBER: the way you update your working memory is by calling the updateWorkingMemory tool with the ${template.format === "json" ? "JSON" : "Markdown"} content. The system will store it for you. The user will not see it.
|
|
@@ -42094,12 +42210,7 @@ ${template.content !== this.defaultWorkingMemoryTemplate ? `- Only store informa
|
|
|
42094
42210
|
- IMPORTANT: Preserve the ${template.format === "json" ? "JSON" : "Markdown"} formatting structure above while updating the content.
|
|
42095
42211
|
`;
|
|
42096
42212
|
}
|
|
42097
|
-
|
|
42098
|
-
* Generate read-only working memory instructions.
|
|
42099
|
-
* This provides the working memory context without any tool update instructions.
|
|
42100
|
-
* Used when memory is in readOnly mode, or with `autoUpdated` when the app updates it (agentManaged: false).
|
|
42101
|
-
*/
|
|
42102
|
-
getReadOnlyWorkingMemoryInstruction({ data, autoUpdated = false }) {
|
|
42213
|
+
builtInReadOnlyWorkingMemoryInstruction({ data, autoUpdated = false }) {
|
|
42103
42214
|
return `WORKING_MEMORY_SYSTEM_INSTRUCTION (${autoUpdated ? "AUTO-UPDATED" : "READ-ONLY"}):
|
|
42104
42215
|
The following is your working memory - persistent information about the user and conversation collected over previous interactions. This data is provided for context to help you maintain continuity.
|
|
42105
42216
|
|
|
@@ -42116,6 +42227,46 @@ Notes:
|
|
|
42116
42227
|
- This system is here so that you can maintain the conversation when your context window is very short
|
|
42117
42228
|
- The user will not see the working memory data directly`;
|
|
42118
42229
|
}
|
|
42230
|
+
/** Instruction for agents that update working memory with the updateWorkingMemory tool. Override to customize it. */
|
|
42231
|
+
getWorkingMemoryToolInstruction(args) {
|
|
42232
|
+
return this.builtInWorkingMemoryToolInstruction(args);
|
|
42233
|
+
}
|
|
42234
|
+
/** Instruction for `version: 'vnext'` template working memory. Override to customize it. */
|
|
42235
|
+
__experimental_getWorkingMemoryToolInstructionVNext(args) {
|
|
42236
|
+
return this.builtInWorkingMemoryToolInstructionVNext(args);
|
|
42237
|
+
}
|
|
42238
|
+
/**
|
|
42239
|
+
* Generate read-only working memory instructions.
|
|
42240
|
+
* This provides the working memory context without any tool update instructions.
|
|
42241
|
+
* Used when memory is in readOnly mode, or with `autoUpdated` when the app updates it (agentManaged: false).
|
|
42242
|
+
* Override to customize it.
|
|
42243
|
+
*/
|
|
42244
|
+
getReadOnlyWorkingMemoryInstruction(args) {
|
|
42245
|
+
return this.builtInReadOnlyWorkingMemoryInstruction(args);
|
|
42246
|
+
}
|
|
42247
|
+
/**
|
|
42248
|
+
* Renders the working memory instruction an agent receives, using the instruction methods above,
|
|
42249
|
+
* so a subclass that overrides them changes what the model sees. Return `undefined` to use the
|
|
42250
|
+
* built-in instruction, or an empty string to send none.
|
|
42251
|
+
*/
|
|
42252
|
+
getWorkingMemoryInstruction(args) {
|
|
42253
|
+
switch (args.kind) {
|
|
42254
|
+
case "read-only": return this.getReadOnlyWorkingMemoryInstruction(args);
|
|
42255
|
+
case "vnext": return this.__experimental_getWorkingMemoryToolInstructionVNext(args);
|
|
42256
|
+
case "tool": return this.getWorkingMemoryToolInstruction(args);
|
|
42257
|
+
}
|
|
42258
|
+
}
|
|
42259
|
+
/**
|
|
42260
|
+
* The built-in text, ignoring subclass overrides of the instruction methods. Used when
|
|
42261
|
+
* getWorkingMemoryInstruction returns `undefined`, matching the agent processor's own fallback.
|
|
42262
|
+
*/
|
|
42263
|
+
renderBuiltInWorkingMemoryInstruction(args) {
|
|
42264
|
+
switch (args.kind) {
|
|
42265
|
+
case "read-only": return this.builtInReadOnlyWorkingMemoryInstruction(args);
|
|
42266
|
+
case "vnext": return this.builtInWorkingMemoryToolInstructionVNext(args);
|
|
42267
|
+
case "tool": return this.builtInWorkingMemoryToolInstruction(args);
|
|
42268
|
+
}
|
|
42269
|
+
}
|
|
42119
42270
|
isVNextWorkingMemoryConfig(config) {
|
|
42120
42271
|
if (!config?.workingMemory) return false;
|
|
42121
42272
|
const isMDWorkingMemory = !(`schema` in config.workingMemory) && (typeof config.workingMemory.template === `string` || config.workingMemory.template) && config.workingMemory;
|
|
@@ -42815,8 +42966,11 @@ Notes:
|
|
|
42815
42966
|
};
|
|
42816
42967
|
}
|
|
42817
42968
|
const moved = /* @__PURE__ */ new Set();
|
|
42818
|
-
|
|
42819
|
-
|
|
42969
|
+
const deadline = Date.now() + 12e4;
|
|
42970
|
+
let retryDelayMs = 1e3;
|
|
42971
|
+
let complete = false;
|
|
42972
|
+
migration: while (Date.now() < deadline) {
|
|
42973
|
+
const stale = await vector.query({
|
|
42820
42974
|
indexName,
|
|
42821
42975
|
...readParams,
|
|
42822
42976
|
topK: 20,
|
|
@@ -42824,9 +42978,21 @@ Notes:
|
|
|
42824
42978
|
thread_id: threadId,
|
|
42825
42979
|
resource_id: { $ne: resourceId }
|
|
42826
42980
|
}
|
|
42827
|
-
})
|
|
42828
|
-
if (
|
|
42981
|
+
});
|
|
42982
|
+
if (stale.length === 0) {
|
|
42983
|
+
complete = true;
|
|
42984
|
+
break;
|
|
42985
|
+
}
|
|
42986
|
+
const pending = stale.filter((result) => !moved.has(result.id));
|
|
42987
|
+
if (pending.length === 0) {
|
|
42988
|
+
const delayMs = Math.min(retryDelayMs, Math.max(0, deadline - Date.now()));
|
|
42989
|
+
await new Promise((resolve) => setTimeout(resolve, delayMs));
|
|
42990
|
+
retryDelayMs = Math.min(retryDelayMs * 2, 3e4);
|
|
42991
|
+
continue;
|
|
42992
|
+
}
|
|
42993
|
+
retryDelayMs = 1e3;
|
|
42829
42994
|
for (const result of pending) {
|
|
42995
|
+
if (Date.now() >= deadline) break migration;
|
|
42830
42996
|
moved.add(result.id);
|
|
42831
42997
|
if (!result.metadata) {
|
|
42832
42998
|
this.logger.warn("Skipping observation vector without metadata during thread transfer", {
|
|
@@ -42848,6 +43014,7 @@ Notes:
|
|
|
42848
43014
|
});
|
|
42849
43015
|
}
|
|
42850
43016
|
}
|
|
43017
|
+
if (!complete) throw new Error(`Observation vector migration in index "${indexName}" did not complete within 120 seconds (${moved.size} vector IDs processed). The store may still be returning stale results; retry the transfer.`);
|
|
42851
43018
|
}
|
|
42852
43019
|
}
|
|
42853
43020
|
/**
|
|
@@ -43048,6 +43215,10 @@ Notes:
|
|
|
43048
43215
|
*/
|
|
43049
43216
|
async getInputProcessors(configuredProcessors = [], context) {
|
|
43050
43217
|
const processors = await super.getInputProcessors(configuredProcessors, context);
|
|
43218
|
+
if (!coreRendersWorkingMemoryInstructionThroughMemory()) {
|
|
43219
|
+
const index = processors.findIndex((p) => p.id === "working-memory" && !configuredProcessors.includes(p));
|
|
43220
|
+
if (index !== -1) processors[index] = await this.createWorkingMemoryInstructionCompatProcessor(context);
|
|
43221
|
+
}
|
|
43051
43222
|
const om = await this.createOMProcessor(configuredProcessors, context);
|
|
43052
43223
|
if (om) processors.push(om);
|
|
43053
43224
|
const wm = await this.createWorkingMemoryStateProcessor(configuredProcessors, context);
|
|
@@ -43096,6 +43267,29 @@ Notes:
|
|
|
43096
43267
|
return new ObservationalMemoryProcessor(engine, this, { temporalMarkers: effectiveConfig.temporalMarkers });
|
|
43097
43268
|
}
|
|
43098
43269
|
/**
|
|
43270
|
+
* Builds the WorkingMemory input processor for cores without an instruction provider, from the same
|
|
43271
|
+
* config core's `MastraMemory.getInputProcessors()` uses for the processor it replaces.
|
|
43272
|
+
*/
|
|
43273
|
+
async createWorkingMemoryInstructionCompatProcessor(context) {
|
|
43274
|
+
const runtimeMemory = context?.get("MastraMemory");
|
|
43275
|
+
const effectiveConfig = runtimeMemory?.memoryConfig ? this.getMergedThreadConfig(runtimeMemory.memoryConfig) : this.threadConfig;
|
|
43276
|
+
const workingMemory = typeof effectiveConfig.workingMemory === "object" ? effectiveConfig.workingMemory : void 0;
|
|
43277
|
+
return new WorkingMemoryInstructionCompat({
|
|
43278
|
+
storage: await this.getMemoryStore(),
|
|
43279
|
+
template: workingMemory?.template ? {
|
|
43280
|
+
format: "markdown",
|
|
43281
|
+
content: workingMemory.template
|
|
43282
|
+
} : void 0,
|
|
43283
|
+
scope: workingMemory?.scope,
|
|
43284
|
+
useVNext: workingMemory !== void 0 && "version" in workingMemory && workingMemory.version === "vnext",
|
|
43285
|
+
readOnly: effectiveConfig.readOnly,
|
|
43286
|
+
agentManaged: workingMemory?.agentManaged,
|
|
43287
|
+
defaultTemplate: DEFAULT_WORKING_MEMORY_TEMPLATE,
|
|
43288
|
+
getWorkingMemoryTemplate: (args) => this.getWorkingMemoryTemplate(args),
|
|
43289
|
+
renderInstruction: (args) => this.getWorkingMemoryInstruction(args) ?? this.renderBuiltInWorkingMemoryInstruction(args)
|
|
43290
|
+
});
|
|
43291
|
+
}
|
|
43292
|
+
/**
|
|
43099
43293
|
* Creates a WorkingMemoryStateProcessor when working memory is enabled and the
|
|
43100
43294
|
* `useStateSignals` opt-in is set. Returns null otherwise or if the processor
|
|
43101
43295
|
* is already present in the user's configured processors.
|
|
@@ -43387,5 +43581,3 @@ Object.defineProperty(exports, "wrapInObservationGroup", {
|
|
|
43387
43581
|
return wrapInObservationGroup;
|
|
43388
43582
|
}
|
|
43389
43583
|
});
|
|
43390
|
-
|
|
43391
|
-
//# sourceMappingURL=src-CKA5LbDL.cjs.map
|