@mastra/memory 1.9.0-alpha.1 → 1.9.0-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/CHANGELOG.md +20 -0
- package/dist/{chunk-5SMKVGJP.js → chunk-JJBSFPC5.js} +315 -25
- package/dist/chunk-JJBSFPC5.js.map +1 -0
- package/dist/{chunk-AR52LM55.cjs → chunk-LVV2RT42.cjs} +327 -24
- package/dist/chunk-LVV2RT42.cjs.map +1 -0
- package/dist/docs/SKILL.md +5 -7
- package/dist/docs/assets/SOURCE_MAP.json +77 -27
- package/dist/docs/references/docs-agents-agent-approval.md +114 -193
- package/dist/docs/references/docs-agents-networks.md +88 -205
- package/dist/docs/references/docs-agents-supervisor-agents.md +24 -18
- package/dist/docs/references/docs-memory-observational-memory.md +30 -2
- package/dist/docs/references/docs-memory-overview.md +219 -24
- package/dist/docs/references/docs-memory-semantic-recall.md +1 -1
- package/dist/docs/references/docs-memory-storage.md +4 -4
- package/dist/docs/references/docs-memory-working-memory.md +1 -1
- package/dist/docs/references/reference-core-getMemory.md +1 -2
- package/dist/docs/references/reference-core-listMemory.md +1 -2
- package/dist/docs/references/reference-memory-cloneThread.md +1 -1
- package/dist/docs/references/reference-memory-observational-memory.md +39 -1
- package/dist/index.cjs +432 -11
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +432 -10
- package/dist/index.js.map +1 -1
- package/dist/observational-memory-3XFCO6MX.js +3 -0
- package/dist/{observational-memory-5NFPG6M3.js.map → observational-memory-3XFCO6MX.js.map} +1 -1
- package/dist/observational-memory-MJJFU26W.cjs +108 -0
- package/dist/{observational-memory-NH7VDTXM.cjs.map → observational-memory-MJJFU26W.cjs.map} +1 -1
- package/dist/processors/index.cjs +56 -16
- package/dist/processors/index.js +1 -1
- package/dist/processors/observational-memory/anchor-ids.d.ts +4 -0
- package/dist/processors/observational-memory/anchor-ids.d.ts.map +1 -0
- package/dist/processors/observational-memory/index.d.ts +2 -0
- package/dist/processors/observational-memory/index.d.ts.map +1 -1
- package/dist/processors/observational-memory/observation-groups.d.ts +15 -0
- package/dist/processors/observational-memory/observation-groups.d.ts.map +1 -0
- package/dist/processors/observational-memory/observational-memory.d.ts +14 -0
- package/dist/processors/observational-memory/observational-memory.d.ts.map +1 -1
- package/dist/processors/observational-memory/observer-agent.d.ts.map +1 -1
- package/dist/processors/observational-memory/reflector-agent.d.ts +1 -1
- package/dist/processors/observational-memory/reflector-agent.d.ts.map +1 -1
- package/dist/processors/observational-memory/tool-result-helpers.d.ts.map +1 -1
- package/dist/tools/om-tools.d.ts +77 -0
- package/dist/tools/om-tools.d.ts.map +1 -0
- package/package.json +3 -3
- package/dist/chunk-5SMKVGJP.js.map +0 -1
- package/dist/chunk-AR52LM55.cjs.map +0 -1
- package/dist/docs/references/docs-agents-agent-memory.md +0 -209
- package/dist/docs/references/docs-agents-network-approval.md +0 -278
- package/dist/observational-memory-5NFPG6M3.js +0 -3
- package/dist/observational-memory-NH7VDTXM.cjs +0 -68
package/dist/index.cjs
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
var chunkCK4U3AYR_cjs = require('./chunk-CK4U3AYR.cjs');
|
|
4
4
|
var chunkIDRQZVB4_cjs = require('./chunk-IDRQZVB4.cjs');
|
|
5
|
-
var
|
|
5
|
+
var chunkLVV2RT42_cjs = require('./chunk-LVV2RT42.cjs');
|
|
6
6
|
var v3 = require('zod/v3');
|
|
7
7
|
var zod = require('zod');
|
|
8
8
|
var z4 = require('zod/v4');
|
|
@@ -13,8 +13,9 @@ var utils = require('@mastra/core/utils');
|
|
|
13
13
|
var schema = require('@mastra/schema-compat/schema');
|
|
14
14
|
var asyncMutex = require('async-mutex');
|
|
15
15
|
var xxhash = require('xxhash-wasm');
|
|
16
|
-
var schema$1 = require('@mastra/core/schema');
|
|
17
16
|
var tools = require('@mastra/core/tools');
|
|
17
|
+
var tokenx = require('tokenx');
|
|
18
|
+
var schema$1 = require('@mastra/core/schema');
|
|
18
19
|
var processors = require('@mastra/core/processors');
|
|
19
20
|
|
|
20
21
|
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
@@ -15446,6 +15447,425 @@ var DefaultEmbedManyResult3 = class {
|
|
|
15446
15447
|
};
|
|
15447
15448
|
createIdGenerator3({ prefix: "aiobj", size: 24 });
|
|
15448
15449
|
createIdGenerator3({ prefix: "aiobj", size: 24 });
|
|
15450
|
+
function hasVisibleParts(msg) {
|
|
15451
|
+
if (typeof msg.content === "string") return msg.content.length > 0;
|
|
15452
|
+
const parts = msg.content?.parts;
|
|
15453
|
+
if (!parts || !Array.isArray(parts)) return false;
|
|
15454
|
+
return parts.some((p) => !p.type?.startsWith("data-"));
|
|
15455
|
+
}
|
|
15456
|
+
function parseRangeFormat(cursor) {
|
|
15457
|
+
if (cursor.includes(",")) {
|
|
15458
|
+
const parts = cursor.split(",").map((p) => p.trim()).filter(Boolean);
|
|
15459
|
+
if (parts.length >= 1) {
|
|
15460
|
+
const first = parts[0];
|
|
15461
|
+
const last = parts[parts.length - 1];
|
|
15462
|
+
const firstColon = first.indexOf(":");
|
|
15463
|
+
const lastColon = last.indexOf(":");
|
|
15464
|
+
return {
|
|
15465
|
+
startId: firstColon > 0 ? first.slice(0, firstColon) : first,
|
|
15466
|
+
endId: lastColon > 0 ? last.slice(lastColon + 1) : last
|
|
15467
|
+
};
|
|
15468
|
+
}
|
|
15469
|
+
}
|
|
15470
|
+
const colonIndex = cursor.indexOf(":");
|
|
15471
|
+
if (colonIndex > 0 && colonIndex < cursor.length - 1) {
|
|
15472
|
+
return { startId: cursor.slice(0, colonIndex), endId: cursor.slice(colonIndex + 1) };
|
|
15473
|
+
}
|
|
15474
|
+
return null;
|
|
15475
|
+
}
|
|
15476
|
+
async function resolveCursorMessage(memory, cursor) {
|
|
15477
|
+
const normalized = cursor.trim();
|
|
15478
|
+
if (!normalized) {
|
|
15479
|
+
throw new Error("Cursor is required");
|
|
15480
|
+
}
|
|
15481
|
+
const rangeIds = parseRangeFormat(normalized);
|
|
15482
|
+
if (rangeIds) {
|
|
15483
|
+
return {
|
|
15484
|
+
hint: `The cursor "${cursor}" looks like a range. Use one of the individual message IDs as the cursor instead: start="${rangeIds.startId}" or end="${rangeIds.endId}".`,
|
|
15485
|
+
...rangeIds
|
|
15486
|
+
};
|
|
15487
|
+
}
|
|
15488
|
+
const memoryStore = await memory.getMemoryStore();
|
|
15489
|
+
const result = await memoryStore.listMessagesById({ messageIds: [normalized] });
|
|
15490
|
+
const message = result.messages.find((message2) => message2.id === normalized);
|
|
15491
|
+
if (!message) {
|
|
15492
|
+
throw new Error(`Could not resolve cursor message: ${cursor}`);
|
|
15493
|
+
}
|
|
15494
|
+
return message;
|
|
15495
|
+
}
|
|
15496
|
+
var LOW_DETAIL_PART_TOKENS = 30;
|
|
15497
|
+
var AUTO_EXPAND_TEXT_TOKENS = 100;
|
|
15498
|
+
var AUTO_EXPAND_TOOL_TOKENS = 20;
|
|
15499
|
+
var HIGH_DETAIL_TOOL_RESULT_TOKENS = 4e3;
|
|
15500
|
+
var DEFAULT_MAX_RESULT_TOKENS = 2e3;
|
|
15501
|
+
function formatTimestamp(date) {
|
|
15502
|
+
return date.toISOString().replace("T", " ").replace(/\.\d{3}Z$/, "Z");
|
|
15503
|
+
}
|
|
15504
|
+
function truncateByTokens(text4, maxTokens, hint) {
|
|
15505
|
+
if (tokenx.estimateTokenCount(text4) <= maxTokens) return { text: text4, wasTruncated: false };
|
|
15506
|
+
const truncated = chunkLVV2RT42_cjs.truncateStringByTokens(text4, maxTokens);
|
|
15507
|
+
const suffix = hint ? ` [${hint} for more]` : "";
|
|
15508
|
+
return { text: truncated + suffix, wasTruncated: true };
|
|
15509
|
+
}
|
|
15510
|
+
function lowDetailPartLimit(type) {
|
|
15511
|
+
if (type === "text") return AUTO_EXPAND_TEXT_TOKENS;
|
|
15512
|
+
if (type === "tool-result" || type === "tool-call") return AUTO_EXPAND_TOOL_TOKENS;
|
|
15513
|
+
return LOW_DETAIL_PART_TOKENS;
|
|
15514
|
+
}
|
|
15515
|
+
function makePart(msg, partIndex, type, fullText, detail) {
|
|
15516
|
+
if (detail === "high") {
|
|
15517
|
+
return { messageId: msg.id, partIndex, role: msg.role, type, text: fullText, fullText };
|
|
15518
|
+
}
|
|
15519
|
+
const hint = `recall cursor="${msg.id}" partIndex=${partIndex} detail="high"`;
|
|
15520
|
+
const { text: text4 } = truncateByTokens(fullText, lowDetailPartLimit(type), hint);
|
|
15521
|
+
return { messageId: msg.id, partIndex, role: msg.role, type, text: text4, fullText };
|
|
15522
|
+
}
|
|
15523
|
+
function formatMessageParts(msg, detail) {
|
|
15524
|
+
const parts = [];
|
|
15525
|
+
if (typeof msg.content === "string") {
|
|
15526
|
+
parts.push(makePart(msg, 0, "text", msg.content, detail));
|
|
15527
|
+
return parts;
|
|
15528
|
+
}
|
|
15529
|
+
if (msg.content?.parts && Array.isArray(msg.content.parts)) {
|
|
15530
|
+
for (let i = 0; i < msg.content.parts.length; i++) {
|
|
15531
|
+
const part = msg.content.parts[i];
|
|
15532
|
+
const partType = part.type;
|
|
15533
|
+
if (partType === "text") {
|
|
15534
|
+
const text4 = part.text;
|
|
15535
|
+
parts.push(makePart(msg, i, "text", text4, detail));
|
|
15536
|
+
} else if (partType === "tool-invocation") {
|
|
15537
|
+
const inv = part.toolInvocation;
|
|
15538
|
+
if (inv.state === "result") {
|
|
15539
|
+
const { value: resultValue } = chunkLVV2RT42_cjs.resolveToolResultValue(
|
|
15540
|
+
part,
|
|
15541
|
+
inv.result
|
|
15542
|
+
);
|
|
15543
|
+
const resultStr = chunkLVV2RT42_cjs.formatToolResultForObserver(resultValue, { maxTokens: HIGH_DETAIL_TOOL_RESULT_TOKENS });
|
|
15544
|
+
const fullText = `[Tool Result: ${inv.toolName}]
|
|
15545
|
+
${resultStr}`;
|
|
15546
|
+
parts.push(makePart(msg, i, "tool-result", fullText, detail));
|
|
15547
|
+
} else {
|
|
15548
|
+
const argsStr = detail === "low" ? "" : `
|
|
15549
|
+
${JSON.stringify(inv.args, null, 2)}`;
|
|
15550
|
+
const fullText = `[Tool Call: ${inv.toolName}]${argsStr}`;
|
|
15551
|
+
parts.push({ messageId: msg.id, partIndex: i, role: msg.role, type: "tool-call", text: fullText, fullText });
|
|
15552
|
+
}
|
|
15553
|
+
} else if (partType === "reasoning") {
|
|
15554
|
+
const reasoning = part.reasoning;
|
|
15555
|
+
if (reasoning) {
|
|
15556
|
+
parts.push(makePart(msg, i, "reasoning", reasoning, detail));
|
|
15557
|
+
}
|
|
15558
|
+
} else if (partType === "image" || partType === "file") {
|
|
15559
|
+
const filename = part.filename;
|
|
15560
|
+
const label = filename ? `: ${filename}` : "";
|
|
15561
|
+
const fullText = `[${partType === "image" ? "Image" : "File"}${label}]`;
|
|
15562
|
+
parts.push({ messageId: msg.id, partIndex: i, role: msg.role, type: partType, text: fullText, fullText });
|
|
15563
|
+
} else if (partType?.startsWith("data-")) ; else if (partType) {
|
|
15564
|
+
const fullText = `[${partType}]`;
|
|
15565
|
+
parts.push({ messageId: msg.id, partIndex: i, role: msg.role, type: partType, text: fullText, fullText });
|
|
15566
|
+
}
|
|
15567
|
+
}
|
|
15568
|
+
} else if (msg.content?.content) {
|
|
15569
|
+
parts.push(makePart(msg, 0, "text", msg.content.content, detail));
|
|
15570
|
+
}
|
|
15571
|
+
return parts;
|
|
15572
|
+
}
|
|
15573
|
+
function buildRenderedText(parts, timestamps) {
|
|
15574
|
+
let currentMessageId = "";
|
|
15575
|
+
const lines = [];
|
|
15576
|
+
for (const part of parts) {
|
|
15577
|
+
if (part.messageId !== currentMessageId) {
|
|
15578
|
+
currentMessageId = part.messageId;
|
|
15579
|
+
const ts = timestamps.get(part.messageId);
|
|
15580
|
+
const tsStr = ts ? ` (${formatTimestamp(ts)})` : "";
|
|
15581
|
+
if (lines.length > 0) lines.push("");
|
|
15582
|
+
lines.push(`**${part.role}${tsStr}** [${part.messageId}]:`);
|
|
15583
|
+
}
|
|
15584
|
+
const indexLabel = `[p${part.partIndex}]`;
|
|
15585
|
+
lines.push(` ${indexLabel} ${part.text}`);
|
|
15586
|
+
}
|
|
15587
|
+
return lines.join("\n");
|
|
15588
|
+
}
|
|
15589
|
+
var MAX_EXPAND_USER_TEXT_TOKENS = 200;
|
|
15590
|
+
var MAX_EXPAND_OTHER_TOKENS = 50;
|
|
15591
|
+
function expandLimit(part) {
|
|
15592
|
+
if (part.role === "user" && part.type === "text") return MAX_EXPAND_USER_TEXT_TOKENS;
|
|
15593
|
+
return MAX_EXPAND_OTHER_TOKENS;
|
|
15594
|
+
}
|
|
15595
|
+
function expandPriority(part) {
|
|
15596
|
+
if (part.role === "user" && part.type === "text") return 0;
|
|
15597
|
+
if (part.type === "text" || part.type === "reasoning") return 1;
|
|
15598
|
+
if (part.type === "tool-result") return 2;
|
|
15599
|
+
if (part.type === "tool-call") return 3;
|
|
15600
|
+
return 4;
|
|
15601
|
+
}
|
|
15602
|
+
function renderFormattedParts(parts, timestamps, options) {
|
|
15603
|
+
const text4 = buildRenderedText(parts, timestamps);
|
|
15604
|
+
let totalTokens = tokenx.estimateTokenCount(text4);
|
|
15605
|
+
if (totalTokens > options.maxTokens) {
|
|
15606
|
+
const truncated = chunkLVV2RT42_cjs.truncateStringByTokens(text4, options.maxTokens);
|
|
15607
|
+
return { text: truncated, truncated: true, tokenOffset: totalTokens - options.maxTokens };
|
|
15608
|
+
}
|
|
15609
|
+
const truncatedIndices = parts.map((p, i) => ({ part: p, index: i })).filter(({ part }) => part.text !== part.fullText).sort((a, b) => expandPriority(a.part) - expandPriority(b.part));
|
|
15610
|
+
if (truncatedIndices.length === 0) {
|
|
15611
|
+
return { text: text4, truncated: false, tokenOffset: 0 };
|
|
15612
|
+
}
|
|
15613
|
+
let remaining = options.maxTokens - totalTokens;
|
|
15614
|
+
for (const { part, index } of truncatedIndices) {
|
|
15615
|
+
if (remaining <= 0) break;
|
|
15616
|
+
const maxTokens = expandLimit(part);
|
|
15617
|
+
const fullTokens = tokenx.estimateTokenCount(part.fullText);
|
|
15618
|
+
const currentTokens = tokenx.estimateTokenCount(part.text);
|
|
15619
|
+
const targetTokens = Math.min(fullTokens, maxTokens);
|
|
15620
|
+
const delta = targetTokens - currentTokens;
|
|
15621
|
+
if (delta <= 0) continue;
|
|
15622
|
+
if (delta <= remaining && targetTokens >= fullTokens) {
|
|
15623
|
+
parts[index] = { ...part, text: part.fullText };
|
|
15624
|
+
remaining -= delta;
|
|
15625
|
+
} else {
|
|
15626
|
+
const expandedLimit = Math.min(currentTokens + remaining, maxTokens);
|
|
15627
|
+
const hint = `recall cursor="${part.messageId}" partIndex=${part.partIndex} detail="high"`;
|
|
15628
|
+
const { text: expanded2 } = truncateByTokens(part.fullText, expandedLimit, hint);
|
|
15629
|
+
const expandedDelta = tokenx.estimateTokenCount(expanded2) - currentTokens;
|
|
15630
|
+
parts[index] = { ...part, text: expanded2 };
|
|
15631
|
+
remaining -= expandedDelta;
|
|
15632
|
+
}
|
|
15633
|
+
}
|
|
15634
|
+
const expanded = buildRenderedText(parts, timestamps);
|
|
15635
|
+
const expandedTokens = tokenx.estimateTokenCount(expanded);
|
|
15636
|
+
if (expandedTokens <= options.maxTokens) {
|
|
15637
|
+
return { text: expanded, truncated: false, tokenOffset: 0 };
|
|
15638
|
+
}
|
|
15639
|
+
const hardTruncated = chunkLVV2RT42_cjs.truncateStringByTokens(expanded, options.maxTokens);
|
|
15640
|
+
return { text: hardTruncated, truncated: true, tokenOffset: expandedTokens - options.maxTokens };
|
|
15641
|
+
}
|
|
15642
|
+
async function recallPart({
|
|
15643
|
+
memory,
|
|
15644
|
+
threadId,
|
|
15645
|
+
cursor,
|
|
15646
|
+
partIndex,
|
|
15647
|
+
maxTokens = DEFAULT_MAX_RESULT_TOKENS
|
|
15648
|
+
}) {
|
|
15649
|
+
if (!memory || typeof memory.getMemoryStore !== "function") {
|
|
15650
|
+
throw new Error("Memory instance is required for recall");
|
|
15651
|
+
}
|
|
15652
|
+
if (!threadId) {
|
|
15653
|
+
throw new Error("Thread ID is required for recall");
|
|
15654
|
+
}
|
|
15655
|
+
const resolved = await resolveCursorMessage(memory, cursor);
|
|
15656
|
+
if ("hint" in resolved) {
|
|
15657
|
+
throw new Error(resolved.hint);
|
|
15658
|
+
}
|
|
15659
|
+
if (resolved.threadId !== threadId) {
|
|
15660
|
+
throw new Error("The requested cursor does not belong to the current thread");
|
|
15661
|
+
}
|
|
15662
|
+
const allParts = formatMessageParts(resolved, "high");
|
|
15663
|
+
if (allParts.length === 0) {
|
|
15664
|
+
throw new Error(
|
|
15665
|
+
`Message ${cursor} has no visible content (it may be an internal system message). Try a neighboring message ID instead.`
|
|
15666
|
+
);
|
|
15667
|
+
}
|
|
15668
|
+
const target = allParts.find((p) => p.partIndex === partIndex);
|
|
15669
|
+
if (!target) {
|
|
15670
|
+
throw new Error(
|
|
15671
|
+
`Part index ${partIndex} not found in message ${cursor}. Available indices: ${allParts.map((p) => p.partIndex).join(", ")}`
|
|
15672
|
+
);
|
|
15673
|
+
}
|
|
15674
|
+
const truncatedText = chunkLVV2RT42_cjs.truncateStringByTokens(target.text, maxTokens);
|
|
15675
|
+
const wasTruncated = truncatedText !== target.text;
|
|
15676
|
+
return {
|
|
15677
|
+
text: truncatedText,
|
|
15678
|
+
messageId: target.messageId,
|
|
15679
|
+
partIndex: target.partIndex,
|
|
15680
|
+
role: target.role,
|
|
15681
|
+
type: target.type,
|
|
15682
|
+
truncated: wasTruncated
|
|
15683
|
+
};
|
|
15684
|
+
}
|
|
15685
|
+
async function recallMessages({
|
|
15686
|
+
memory,
|
|
15687
|
+
threadId,
|
|
15688
|
+
resourceId,
|
|
15689
|
+
cursor,
|
|
15690
|
+
page = 1,
|
|
15691
|
+
limit = 20,
|
|
15692
|
+
detail = "low",
|
|
15693
|
+
maxTokens = DEFAULT_MAX_RESULT_TOKENS
|
|
15694
|
+
}) {
|
|
15695
|
+
if (!memory) {
|
|
15696
|
+
throw new Error("Memory instance is required for recall");
|
|
15697
|
+
}
|
|
15698
|
+
if (!threadId) {
|
|
15699
|
+
throw new Error("Thread ID is required for recall");
|
|
15700
|
+
}
|
|
15701
|
+
if (typeof memory.getMemoryStore !== "function") {
|
|
15702
|
+
throw new Error("recall requires a Memory instance with storage access");
|
|
15703
|
+
}
|
|
15704
|
+
const MAX_PAGE = 50;
|
|
15705
|
+
const MAX_LIMIT = 20;
|
|
15706
|
+
const rawPage = page === 0 ? 1 : page;
|
|
15707
|
+
const normalizedPage = Math.max(Math.min(rawPage, MAX_PAGE), -MAX_PAGE);
|
|
15708
|
+
const normalizedLimit = Math.min(limit, MAX_LIMIT);
|
|
15709
|
+
const resolved = await resolveCursorMessage(memory, cursor);
|
|
15710
|
+
if ("hint" in resolved) {
|
|
15711
|
+
return {
|
|
15712
|
+
messages: resolved.hint,
|
|
15713
|
+
count: 0,
|
|
15714
|
+
cursor,
|
|
15715
|
+
page: normalizedPage,
|
|
15716
|
+
limit: normalizedLimit,
|
|
15717
|
+
detail,
|
|
15718
|
+
hasNextPage: false,
|
|
15719
|
+
hasPrevPage: false,
|
|
15720
|
+
truncated: false,
|
|
15721
|
+
tokenOffset: 0
|
|
15722
|
+
};
|
|
15723
|
+
}
|
|
15724
|
+
const anchor = resolved;
|
|
15725
|
+
if (anchor.threadId !== threadId) {
|
|
15726
|
+
throw new Error("The requested cursor does not belong to the current thread");
|
|
15727
|
+
}
|
|
15728
|
+
const isForward = normalizedPage > 0;
|
|
15729
|
+
const pageIndex = Math.max(Math.abs(normalizedPage), 1) - 1;
|
|
15730
|
+
const skip = pageIndex * normalizedLimit;
|
|
15731
|
+
const fetchCount = skip + normalizedLimit + 1;
|
|
15732
|
+
const result = await memory.recall({
|
|
15733
|
+
threadId,
|
|
15734
|
+
resourceId,
|
|
15735
|
+
page: 0,
|
|
15736
|
+
perPage: fetchCount,
|
|
15737
|
+
orderBy: { field: "createdAt", direction: isForward ? "ASC" : "DESC" },
|
|
15738
|
+
filter: {
|
|
15739
|
+
dateRange: isForward ? {
|
|
15740
|
+
start: anchor.createdAt,
|
|
15741
|
+
startExclusive: true
|
|
15742
|
+
} : {
|
|
15743
|
+
end: anchor.createdAt,
|
|
15744
|
+
endExclusive: true
|
|
15745
|
+
}
|
|
15746
|
+
}
|
|
15747
|
+
});
|
|
15748
|
+
const visibleMessages = result.messages.filter(hasVisibleParts);
|
|
15749
|
+
const total = visibleMessages.length;
|
|
15750
|
+
const hasMore = total > skip + normalizedLimit;
|
|
15751
|
+
let messages;
|
|
15752
|
+
if (isForward) {
|
|
15753
|
+
messages = visibleMessages.slice(skip, skip + normalizedLimit);
|
|
15754
|
+
} else {
|
|
15755
|
+
const endIdx = Math.max(total - skip, 0);
|
|
15756
|
+
const startIdx = Math.max(endIdx - normalizedLimit, 0);
|
|
15757
|
+
messages = visibleMessages.slice(startIdx, endIdx);
|
|
15758
|
+
}
|
|
15759
|
+
const hasNextPage = isForward ? hasMore : pageIndex > 0;
|
|
15760
|
+
const hasPrevPage = isForward ? pageIndex > 0 : hasMore;
|
|
15761
|
+
const allParts = [];
|
|
15762
|
+
const timestamps = /* @__PURE__ */ new Map();
|
|
15763
|
+
for (const msg of messages) {
|
|
15764
|
+
timestamps.set(msg.id, msg.createdAt);
|
|
15765
|
+
allParts.push(...formatMessageParts(msg, detail));
|
|
15766
|
+
}
|
|
15767
|
+
if (detail === "high" && allParts.length > 0) {
|
|
15768
|
+
const firstPart = allParts[0];
|
|
15769
|
+
const sameMsgParts = allParts.filter((p) => p.messageId === firstPart.messageId);
|
|
15770
|
+
const otherMsgParts = allParts.filter((p) => p.messageId !== firstPart.messageId);
|
|
15771
|
+
const rendered2 = renderFormattedParts([firstPart], timestamps, { maxTokens });
|
|
15772
|
+
let text4 = rendered2.text;
|
|
15773
|
+
const hints = [];
|
|
15774
|
+
if (sameMsgParts.length > 1) {
|
|
15775
|
+
const nextPart = sameMsgParts[1];
|
|
15776
|
+
hints.push(`next part: partIndex=${nextPart.partIndex} on cursor="${firstPart.messageId}"`);
|
|
15777
|
+
}
|
|
15778
|
+
if (otherMsgParts.length > 0) {
|
|
15779
|
+
const next = otherMsgParts[0];
|
|
15780
|
+
hints.push(`next message: partIndex=${next.partIndex} on cursor="${next.messageId}"`);
|
|
15781
|
+
} else if (hasNextPage) {
|
|
15782
|
+
hints.push(`more messages available on page ${normalizedPage + 1}`);
|
|
15783
|
+
}
|
|
15784
|
+
if (hints.length > 0) {
|
|
15785
|
+
text4 += `
|
|
15786
|
+
|
|
15787
|
+
High detail returns 1 part at a time. To continue: ${hints.join(", or ")}.`;
|
|
15788
|
+
}
|
|
15789
|
+
return {
|
|
15790
|
+
messages: text4,
|
|
15791
|
+
count: 1,
|
|
15792
|
+
cursor,
|
|
15793
|
+
page: normalizedPage,
|
|
15794
|
+
limit: normalizedLimit,
|
|
15795
|
+
detail,
|
|
15796
|
+
hasNextPage: otherMsgParts.length > 0 || hasNextPage,
|
|
15797
|
+
hasPrevPage,
|
|
15798
|
+
truncated: rendered2.truncated,
|
|
15799
|
+
tokenOffset: rendered2.tokenOffset
|
|
15800
|
+
};
|
|
15801
|
+
}
|
|
15802
|
+
const rendered = renderFormattedParts(allParts, timestamps, { maxTokens });
|
|
15803
|
+
return {
|
|
15804
|
+
messages: rendered.text,
|
|
15805
|
+
count: messages.length,
|
|
15806
|
+
cursor,
|
|
15807
|
+
page: normalizedPage,
|
|
15808
|
+
limit: normalizedLimit,
|
|
15809
|
+
detail,
|
|
15810
|
+
hasNextPage,
|
|
15811
|
+
hasPrevPage,
|
|
15812
|
+
truncated: rendered.truncated,
|
|
15813
|
+
tokenOffset: rendered.tokenOffset
|
|
15814
|
+
};
|
|
15815
|
+
}
|
|
15816
|
+
var recallTool = (_memoryConfig) => {
|
|
15817
|
+
return tools.createTool({
|
|
15818
|
+
id: "recall",
|
|
15819
|
+
description: 'Retrieve raw message history near an observation group cursor. Observation group ranges use the format startId:endId. Pass either the start or end message ID as the cursor. Use detail="low" (default) for an overview, detail="high" for full content, or provide partIndex to fetch a specific part from the cursor message.',
|
|
15820
|
+
inputSchema: zod.z.object({
|
|
15821
|
+
cursor: zod.z.string().min(1).describe("A single message ID to use as the pagination cursor. Extract it from the start or end of a range."),
|
|
15822
|
+
page: zod.z.number().int().min(-50).max(50).optional().describe(
|
|
15823
|
+
"Pagination offset from the cursor. Positive pages move forward, negative pages move backward, and 0 is treated as 1."
|
|
15824
|
+
),
|
|
15825
|
+
limit: zod.z.number().int().positive().max(20).optional().describe("Maximum number of messages to return. Defaults to 20."),
|
|
15826
|
+
detail: zod.z.enum(["low", "high"]).optional().describe(
|
|
15827
|
+
'Detail level. "low" (default) returns truncated text and tool names. "high" returns full content with tool args/results.'
|
|
15828
|
+
),
|
|
15829
|
+
partIndex: zod.z.number().int().min(0).optional().describe(
|
|
15830
|
+
"Fetch a single part from the cursor message by its positional index. When provided, returns only that part at high detail. Indices are shown as [p0], [p1], etc. in recall results."
|
|
15831
|
+
)
|
|
15832
|
+
}),
|
|
15833
|
+
execute: async ({
|
|
15834
|
+
cursor,
|
|
15835
|
+
page,
|
|
15836
|
+
limit,
|
|
15837
|
+
detail,
|
|
15838
|
+
partIndex
|
|
15839
|
+
}, context2) => {
|
|
15840
|
+
const memory = context2?.memory;
|
|
15841
|
+
const threadId = context2?.agent?.threadId;
|
|
15842
|
+
const resourceId = context2?.agent?.resourceId;
|
|
15843
|
+
if (!memory) {
|
|
15844
|
+
throw new Error("Memory instance is required for recall");
|
|
15845
|
+
}
|
|
15846
|
+
if (!threadId) {
|
|
15847
|
+
throw new Error("Thread ID is required for recall");
|
|
15848
|
+
}
|
|
15849
|
+
if (partIndex !== void 0 && partIndex !== null) {
|
|
15850
|
+
return recallPart({
|
|
15851
|
+
memory,
|
|
15852
|
+
threadId,
|
|
15853
|
+
cursor,
|
|
15854
|
+
partIndex
|
|
15855
|
+
});
|
|
15856
|
+
}
|
|
15857
|
+
return recallMessages({
|
|
15858
|
+
memory,
|
|
15859
|
+
threadId,
|
|
15860
|
+
resourceId,
|
|
15861
|
+
cursor,
|
|
15862
|
+
page,
|
|
15863
|
+
limit,
|
|
15864
|
+
detail: detail ?? "low"
|
|
15865
|
+
});
|
|
15866
|
+
}
|
|
15867
|
+
});
|
|
15868
|
+
};
|
|
15449
15869
|
function deepMergeWorkingMemory(existing, update) {
|
|
15450
15870
|
if (!update || typeof update !== "object" || Object.keys(update).length === 0) {
|
|
15451
15871
|
return existing && typeof existing === "object" ? { ...existing } : {};
|
|
@@ -16425,15 +16845,15 @@ Notes:
|
|
|
16425
16845
|
}
|
|
16426
16846
|
listTools(config) {
|
|
16427
16847
|
const mergedConfig = this.getMergedThreadConfig(config);
|
|
16848
|
+
const tools = {};
|
|
16428
16849
|
if (mergedConfig.workingMemory?.enabled && !mergedConfig.readOnly) {
|
|
16429
|
-
|
|
16430
|
-
updateWorkingMemory: this.isVNextWorkingMemoryConfig(mergedConfig) ? (
|
|
16431
|
-
// use the new experimental tool
|
|
16432
|
-
__experimental_updateWorkingMemoryToolVNext(mergedConfig)
|
|
16433
|
-
) : updateWorkingMemoryTool(mergedConfig)
|
|
16434
|
-
};
|
|
16850
|
+
tools.updateWorkingMemory = this.isVNextWorkingMemoryConfig(mergedConfig) ? __experimental_updateWorkingMemoryToolVNext(mergedConfig) : updateWorkingMemoryTool(mergedConfig);
|
|
16435
16851
|
}
|
|
16436
|
-
|
|
16852
|
+
const omConfig = normalizeObservationalMemoryConfig(mergedConfig.observationalMemory);
|
|
16853
|
+
if (omConfig?.retrieval && (omConfig.scope ?? "thread") === "thread") {
|
|
16854
|
+
tools.recall = recallTool();
|
|
16855
|
+
}
|
|
16856
|
+
return tools;
|
|
16437
16857
|
}
|
|
16438
16858
|
/**
|
|
16439
16859
|
* Updates a list of messages and syncs the vector database for semantic recall.
|
|
@@ -17015,10 +17435,11 @@ Notes:
|
|
|
17015
17435
|
"Observational memory requires @mastra/core support for request-response-id-rotation. Please bump @mastra/core to a newer version."
|
|
17016
17436
|
);
|
|
17017
17437
|
}
|
|
17018
|
-
const { ObservationalMemory } = await import('./observational-memory-
|
|
17438
|
+
const { ObservationalMemory } = await import('./observational-memory-MJJFU26W.cjs');
|
|
17019
17439
|
return new ObservationalMemory({
|
|
17020
17440
|
storage: memoryStore,
|
|
17021
17441
|
scope: omConfig.scope,
|
|
17442
|
+
retrieval: omConfig.retrieval,
|
|
17022
17443
|
shareTokenBudget: omConfig.shareTokenBudget,
|
|
17023
17444
|
model: omConfig.model,
|
|
17024
17445
|
observation: omConfig.observation ? {
|
|
@@ -17049,7 +17470,7 @@ Notes:
|
|
|
17049
17470
|
|
|
17050
17471
|
Object.defineProperty(exports, "getObservationsAsOf", {
|
|
17051
17472
|
enumerable: true,
|
|
17052
|
-
get: function () { return
|
|
17473
|
+
get: function () { return chunkLVV2RT42_cjs.getObservationsAsOf; }
|
|
17053
17474
|
});
|
|
17054
17475
|
Object.defineProperty(exports, "extractWorkingMemoryContent", {
|
|
17055
17476
|
enumerable: true,
|