@mastra/memory 1.25.0-alpha.1 → 1.25.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 +81 -0
- package/dist/docs/SKILL.md +1 -1
- package/dist/docs/assets/SOURCE_MAP.json +1 -1
- package/dist/docs/references/docs-agents-supervisor-agents.md +2 -2
- package/dist/docs/references/docs-evals-evals-with-memory.md +1 -1
- package/dist/docs/references/docs-memory-observational-memory.md +23 -0
- package/dist/docs/references/reference-memory-observational-memory.md +3 -1
- package/dist/docs/references/reference-processors-token-limiter-processor.md +15 -1
- package/dist/index.cjs +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/constants.d.ts +13 -1
- package/dist/processors/observational-memory/constants.d.ts.map +1 -1
- package/dist/processors/observational-memory/observational-memory.d.ts +5 -0
- package/dist/processors/observational-memory/observational-memory.d.ts.map +1 -1
- package/dist/processors/observational-memory/processor.d.ts.map +1 -1
- package/dist/processors/observational-memory/types.d.ts +5 -0
- package/dist/processors/observational-memory/types.d.ts.map +1 -1
- package/dist/{src-DoNjfXNm.js → src-BgdYYHLc.js} +62 -9
- package/dist/src-BgdYYHLc.js.map +1 -0
- package/dist/{src-C3u7zaSu.cjs → src-DQO6B1AU.cjs} +62 -9
- package/dist/src-DQO6B1AU.cjs.map +1 -0
- package/package.json +7 -7
- package/dist/src-C3u7zaSu.cjs.map +0 -1
- package/dist/src-DoNjfXNm.js.map +0 -1
|
@@ -14583,7 +14583,7 @@ var constants_exports = /* @__PURE__ */ __exportAll({
|
|
|
14583
14583
|
OBSERVATION_CONTEXT_INSTRUCTIONS: () => OBSERVATION_CONTEXT_INSTRUCTIONS,
|
|
14584
14584
|
OBSERVATION_CONTEXT_PROMPT: () => OBSERVATION_CONTEXT_PROMPT,
|
|
14585
14585
|
OBSERVATION_CONTINUATION_HINT: () => OBSERVATION_CONTINUATION_HINT,
|
|
14586
|
-
|
|
14586
|
+
getRetrievalInstructions: () => getRetrievalInstructions
|
|
14587
14587
|
});
|
|
14588
14588
|
/**
|
|
14589
14589
|
* Default configuration values matching the spec
|
|
@@ -14646,8 +14646,22 @@ SYSTEM REMINDERS: Messages wrapped in <system-reminder>...</system-reminder> con
|
|
|
14646
14646
|
/**
|
|
14647
14647
|
* Instructions for retrieval mode — explains observation-group ranges and the recall tool.
|
|
14648
14648
|
* Appended to context when `retrieval` is enabled.
|
|
14649
|
+
*
|
|
14650
|
+
* The content adapts to the retrieval scope:
|
|
14651
|
+
* - `'resource'`: covers routing between `search`, `threads`, and `messages` across
|
|
14652
|
+
* all of the user's threads, including fallback when search results are unsuitable.
|
|
14653
|
+
* - `'thread'`: covers cursor-based browsing and search within the current thread.
|
|
14654
|
+
*
|
|
14655
|
+
* @param scope - The retrieval scope the recall tool was registered with.
|
|
14656
|
+
* @param customInstructions - Optional application-provided guidance appended after
|
|
14657
|
+
* the native instructions. Never replaces them.
|
|
14658
|
+
* @param searchEnabled - Whether semantic search (`retrieval: { vector: true }`) is
|
|
14659
|
+
* available. When false, the guidance only covers \`threads\`/\`messages\` browsing so
|
|
14660
|
+
* the agent is not steered toward a mode that cannot work.
|
|
14649
14661
|
*/
|
|
14650
|
-
|
|
14662
|
+
function getRetrievalInstructions(scope = "resource", customInstructions, searchEnabled = true) {
|
|
14663
|
+
const isResource = scope === "resource";
|
|
14664
|
+
const base = `## Recall — looking up source messages
|
|
14651
14665
|
|
|
14652
14666
|
Your memory is comprised of observations which are sometimes wrapped in <observation-group> xml tags containing ranges like <observation-group range="startId:endId">. These ranges point back to the raw messages that each observation group was derived from. The original messages are still available — use the **recall** tool to retrieve them.
|
|
14653
14667
|
|
|
@@ -14655,11 +14669,32 @@ Your memory is comprised of observations which are sometimes wrapped in <observa
|
|
|
14655
14669
|
- The user asks you to **repeat, show, or reproduce** something from a past conversation
|
|
14656
14670
|
- The user asks for **exact content** — code, text, quotes, error messages, URLs, file paths, specific numbers
|
|
14657
14671
|
- Your observations mention something but your memory lacks the detail needed to fully answer (e.g. you know a blog post was shared but only have a summary of it)
|
|
14658
|
-
- You want to **verify or expand on** an observation before responding
|
|
14672
|
+
- You want to **verify or expand on** an observation before responding${isResource ? `
|
|
14673
|
+
- The user references another conversation that your observations don't cover — even if you have no observations yet, their other threads may contain it` : ""}
|
|
14659
14674
|
|
|
14660
14675
|
**Default to using recall when the user references specific past content.** Your observations capture the gist, not the details. If there's any doubt whether your memory is complete enough, use recall.
|
|
14661
14676
|
|
|
14662
|
-
|
|
14677
|
+
${isResource ? searchEnabled ? `### Choosing a mode
|
|
14678
|
+
The recall tool works across ALL of this user's conversation threads, not just the current one.
|
|
14679
|
+
|
|
14680
|
+
- Use \`mode: "search"\` with a \`query\` when you don't know which thread contains the answer. Each result includes its thread ID and the raw message IDs it came from, which you can use as a \`cursor\`.
|
|
14681
|
+
- Use \`mode: "messages"\` when you already know the thread — pass \`threadId\` to read another thread, or a \`cursor\` from an observation-group range or a search result.
|
|
14682
|
+
- Use \`mode: "threads"\` to list the user's threads (IDs, titles, dates) when you need to discover where something was discussed. Use \`before\`/\`after\` to narrow by date.
|
|
14683
|
+
|
|
14684
|
+
**If search results look irrelevant, do not give up.** Search only covers content that has been indexed — a short or recent conversation may exist in raw message history before any observation of it was created. When search returns nothing suitable but the user is clearly referring to a past conversation, call \`mode: "threads"\` to find candidate threads (titles and dates are strong clues), then read them with \`mode: "messages"\`. If a search result already gives you a thread ID, go straight to \`mode: "messages"\`.` : `### Choosing a mode
|
|
14685
|
+
The recall tool works across ALL of this user's conversation threads, not just the current one.
|
|
14686
|
+
|
|
14687
|
+
- Use \`mode: "threads"\` to list the user's threads (IDs, titles, dates) when you need to discover where something was discussed. Use \`before\`/\`after\` to narrow by date.
|
|
14688
|
+
- Use \`mode: "messages"\` when you know the thread — pass \`threadId\` to read another thread, or a \`cursor\` from an observation-group range.
|
|
14689
|
+
|
|
14690
|
+
When the user refers to a past conversation you don't have a cursor for, call \`mode: "threads"\` to find candidate threads (titles and dates are strong clues), then read them with \`mode: "messages"\`. Raw history may exist for threads that have no observations yet.` : `### Choosing a mode
|
|
14691
|
+
The recall tool is limited to the current conversation thread.
|
|
14692
|
+
|
|
14693
|
+
- Use \`mode: "messages"\` (default) to page through this thread's message history near a cursor.${searchEnabled ? `
|
|
14694
|
+
- Use \`mode: "search"\` with a \`query\` to find messages by content within this thread.` : ""}
|
|
14695
|
+
- Use \`mode: "threads"\` to get the current thread's ID, title, and dates.`}
|
|
14696
|
+
|
|
14697
|
+
### How to use recall with a cursor
|
|
14663
14698
|
Each range has the format \`startId:endId\` where both are message IDs separated by a colon.
|
|
14664
14699
|
|
|
14665
14700
|
1. Find the observation group relevant to the user's question and extract the start or end ID from its range.
|
|
@@ -14685,9 +14720,16 @@ Low-detail results may include truncation hints like:
|
|
|
14685
14720
|
### When recall is NOT needed
|
|
14686
14721
|
- The user is asking for a high-level summary and your observations already cover it
|
|
14687
14722
|
- The question is about general preferences or facts that don't require source text
|
|
14688
|
-
|
|
14723
|
+
${isResource ? `- No relevant observation range exists AND ${searchEnabled ? "`search`/`threads`" : "`threads`"} turned up nothing — but remember that raw history may exist for threads that have no observations yet, so check before concluding the information is unavailable` : `- There is no relevant range in your observations for the topic`}
|
|
14689
14724
|
|
|
14690
14725
|
Observation groups with range IDs and your recall tool allows you to think back and remember details you're fuzzy on.`;
|
|
14726
|
+
const custom = customInstructions?.trim();
|
|
14727
|
+
if (!custom) return base;
|
|
14728
|
+
return `${base}
|
|
14729
|
+
|
|
14730
|
+
### Additional recall guidance
|
|
14731
|
+
${custom}`;
|
|
14732
|
+
}
|
|
14691
14733
|
//#endregion
|
|
14692
14734
|
//#region src/processors/observational-memory/extractor.ts
|
|
14693
14735
|
const BUILT_IN_SLUGS = /* @__PURE__ */ new Set([
|
|
@@ -23711,6 +23753,11 @@ var ObservationalMemory = class ObservationalMemory {
|
|
|
23711
23753
|
scope;
|
|
23712
23754
|
/** Whether retrieval-mode observation groups are enabled. */
|
|
23713
23755
|
retrieval;
|
|
23756
|
+
/** Scope the recall tool was registered with — controls which retrieval instructions are injected. */
|
|
23757
|
+
retrievalScope;
|
|
23758
|
+
/** Application-provided guidance appended after the native retrieval instructions. */
|
|
23759
|
+
retrievalInstructions;
|
|
23760
|
+
retrievalSearch;
|
|
23714
23761
|
observationConfig;
|
|
23715
23762
|
reflectionConfig;
|
|
23716
23763
|
onDebugEvent;
|
|
@@ -23776,6 +23823,9 @@ var ObservationalMemory = class ObservationalMemory {
|
|
|
23776
23823
|
this.storage = config.storage;
|
|
23777
23824
|
this.scope = config.scope ?? "thread";
|
|
23778
23825
|
this.retrieval = Boolean(config.retrieval);
|
|
23826
|
+
this.retrievalScope = typeof config.retrieval === "object" ? config.retrieval.scope ?? "resource" : "resource";
|
|
23827
|
+
this.retrievalInstructions = typeof config.retrieval === "object" ? config.retrieval.instructions : void 0;
|
|
23828
|
+
this.retrievalSearch = typeof config.retrieval === "object" && Boolean(config.retrieval.vector);
|
|
23779
23829
|
this.onIndexObservations = config.onIndexObservations;
|
|
23780
23830
|
this.hooks = config.hooks;
|
|
23781
23831
|
this.mastra = config.mastra;
|
|
@@ -24424,7 +24474,7 @@ var ObservationalMemory = class ObservationalMemory {
|
|
|
24424
24474
|
formatObservationsForContext(observations, currentTask, suggestedResponse, extractedValues, unobservedContextBlocks, currentDate, retrieval = false) {
|
|
24425
24475
|
let optimized = retrieval ? renderObservationGroupsForReflection(observations) ?? optimizeObservationsForContext(observations) : optimizeObservationsForContext(observations);
|
|
24426
24476
|
if (currentDate) optimized = addRelativeTimeToObservations(optimized, currentDate);
|
|
24427
|
-
const messages = [`${OBSERVATION_CONTEXT_PROMPT}\n\n${OBSERVATION_CONTEXT_INSTRUCTIONS}${retrieval ? `\n\n${
|
|
24477
|
+
const messages = [`${OBSERVATION_CONTEXT_PROMPT}\n\n${OBSERVATION_CONTEXT_INSTRUCTIONS}${retrieval ? `\n\n${getRetrievalInstructions(this.retrievalScope, this.retrievalInstructions, this.retrievalSearch)}` : ""}`];
|
|
24428
24478
|
if (unobservedContextBlocks) messages.push(`The following content is from OTHER conversations different from the current conversation, they're here for reference, but they're not necessarily your focus:\nSTART_OTHER_CONVERSATIONS_BLOCK\n${unobservedContextBlocks}\nEND_OTHER_CONVERSATIONS_BLOCK`);
|
|
24429
24479
|
const observationChunks = this.splitObservationContextChunks(optimized);
|
|
24430
24480
|
if (observationChunks.length > 0) messages.push("<observations>", ...observationChunks);
|
|
@@ -24926,7 +24976,10 @@ ${formattedMessages}
|
|
|
24926
24976
|
async buildContextSystemMessages(opts) {
|
|
24927
24977
|
const { threadId, resourceId, unobservedContextBlocks } = opts;
|
|
24928
24978
|
const record = opts.record ?? await this.getOrCreateRecord(threadId, resourceId);
|
|
24929
|
-
if (!record.activeObservations)
|
|
24979
|
+
if (!record.activeObservations) {
|
|
24980
|
+
if (this.retrieval && this.retrievalScope === "resource") return [getRetrievalInstructions(this.retrievalScope, this.retrievalInstructions, this.retrievalSearch)];
|
|
24981
|
+
return;
|
|
24982
|
+
}
|
|
24930
24983
|
const omMetadata = getThreadOMMetadata((await this.storage.getThreadById({ threadId }))?.metadata);
|
|
24931
24984
|
const currentTask = omMetadata?.currentTask;
|
|
24932
24985
|
const suggestedResponse = omMetadata?.suggestedResponse;
|
|
@@ -26058,7 +26111,7 @@ var ObservationalMemoryProcessor = class {
|
|
|
26058
26111
|
threadId,
|
|
26059
26112
|
resourceId
|
|
26060
26113
|
});
|
|
26061
|
-
const systemMessages = ctx.
|
|
26114
|
+
const systemMessages = ctx.omRecord ? await this.engine.buildContextSystemMessages({
|
|
26062
26115
|
threadId,
|
|
26063
26116
|
resourceId,
|
|
26064
26117
|
record: ctx.omRecord,
|
|
@@ -28253,4 +28306,4 @@ Notes:
|
|
|
28253
28306
|
//#endregion
|
|
28254
28307
|
export { extractCurrentTask as A, OBSERVATION_CONTEXT_INSTRUCTIONS as B, WorkingMemoryExtractor as C, OBSERVER_SYSTEM_PROMPT as D, TokenCounter as E, injectAnchorIds as F, OBSERVATION_CONTINUATION_HINT as H, parseAnchorId as I, stripEphemeralAnchorIds as L, hasCurrentTaskSection as M, optimizeObservationsForContext as N, buildObserverPrompt as O, parseObserverOutput as P, Extractor as R, deepMergeWorkingMemory as S, summarizeConversation as T, OBSERVATION_CONTEXT_PROMPT as V, 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, formatMessagesForObserver as j, buildObserverSystemPrompt as k, WORKING_MEMORY_STATE_PROCESSOR_ID as l, combineObservationGroupRanges as m, MessageHistory$1 as n, extractWorkingMemoryTags as o, ObservationalMemory as p, SemanticRecall as r, removeWorkingMemoryTags as s, Memory as t, WorkingMemoryStateProcessor as u, renderObservationGroupsForReflection as v, SUMMARIZE_THREAD_DEFAULTS as w, ModelByInputTokens as x, stripObservationGroups as y, OBSERVATIONAL_MEMORY_DEFAULTS as z };
|
|
28255
28308
|
|
|
28256
|
-
//# sourceMappingURL=src-
|
|
28309
|
+
//# sourceMappingURL=src-BgdYYHLc.js.map
|