@mastra/memory 1.30.0 → 1.31.0-alpha.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/docs/SKILL.md +1 -1
- package/dist/docs/assets/SOURCE_MAP.json +1 -1
- package/dist/docs/references/docs-memory-message-history.md +21 -0
- package/dist/docs/references/docs-subagents.md +38 -7
- package/dist/docs/references/reference-memory-memory-class.md +2 -0
- package/dist/index.cjs +1 -1
- package/dist/index.d.ts +3 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/processors/index.cjs +1 -1
- package/dist/processors/index.js +1 -1
- package/dist/processors/observational-memory/observer-agent.d.ts.map +1 -1
- package/dist/processors/observational-memory/observer-runner.d.ts.map +1 -1
- package/dist/processors/observational-memory/reflector-runner.d.ts.map +1 -1
- package/dist/processors/observational-memory/token-counter.d.ts +6 -1
- package/dist/processors/observational-memory/token-counter.d.ts.map +1 -1
- package/dist/processors/observational-memory/tool-argument-helpers.d.ts +10 -0
- package/dist/processors/observational-memory/tool-argument-helpers.d.ts.map +1 -0
- package/dist/processors/observational-memory/tool-result-helpers.d.ts +1 -1
- package/dist/processors/observational-memory/tool-result-helpers.d.ts.map +1 -1
- package/dist/{src-oL_dDHN9.cjs → src-Da7AKXPR.cjs} +579 -64
- package/dist/src-Da7AKXPR.cjs.map +1 -0
- package/dist/{src-Dt8oPiQN.js → src-DqLGJfSb.js} +580 -65
- package/dist/src-DqLGJfSb.js.map +1 -0
- package/package.json +9 -9
- package/dist/src-Dt8oPiQN.js.map +0 -1
- package/dist/src-oL_dDHN9.cjs.map +0 -1
|
@@ -5,8 +5,9 @@ import * as z4 from "zod/v4";
|
|
|
5
5
|
import { z as z$1 } from "zod/v4";
|
|
6
6
|
import { Agent, MessageList } from "@mastra/core/agent";
|
|
7
7
|
import { coreFeatures } from "@mastra/core/features";
|
|
8
|
-
import { MastraMemory, getThreadOMMetadata, parseMemoryRequestContext, setThreadOMMetadata } from "@mastra/core/memory";
|
|
8
|
+
import { MastraMemory, getMemoryTokenBoundary, getThreadOMMetadata, isAfterMemoryTokenBoundary, loadMessageHistory, normalizeMessageHistoryConfig, parseMemoryRequestContext, setThreadOMMetadata } from "@mastra/core/memory";
|
|
9
9
|
import { EntityType, SpanType, createObservabilityContext, getOrCreateSpan } from "@mastra/core/observability";
|
|
10
|
+
import { MessageHistory, MessageHistory as MessageHistory$1, SemanticRecall, TokenLimiterProcessor, WorkingMemory } from "@mastra/core/processors";
|
|
10
11
|
import { generateEmptyFromSchema } from "@mastra/core/utils";
|
|
11
12
|
import { isStandardSchemaWithJSON, standardSchemaToJSONSchema, toStandardSchema } from "@mastra/schema-compat/schema";
|
|
12
13
|
import { Mutex } from "async-mutex";
|
|
@@ -23,7 +24,6 @@ import { AsyncLocalStorage } from "async_hooks";
|
|
|
23
24
|
import probeImageSizeSync from "probe-image-size/sync.js";
|
|
24
25
|
import { isStandardSchemaWithJSON as isStandardSchemaWithJSON$1, toStandardSchema as toStandardSchema$1 } from "@mastra/core/schema";
|
|
25
26
|
import { ErrorCategory, ErrorDomain, MastraError } from "@mastra/core/error";
|
|
26
|
-
import { MessageHistory, MessageHistory as MessageHistory$1, SemanticRecall, WorkingMemory } from "@mastra/core/processors";
|
|
27
27
|
import { structuredPatch } from "diff";
|
|
28
28
|
import { inspect } from "util";
|
|
29
29
|
//#region \0rolldown/runtime.js
|
|
@@ -3713,7 +3713,7 @@ function trimStartOfStream() {
|
|
|
3713
3713
|
};
|
|
3714
3714
|
}
|
|
3715
3715
|
//#endregion
|
|
3716
|
-
//#region ../_vendored/ai_v5/dist/dist-
|
|
3716
|
+
//#region ../_vendored/ai_v5/dist/dist-DCHMJHX3.js
|
|
3717
3717
|
var marker$1$4 = "vercel.ai.error";
|
|
3718
3718
|
var symbol$1$3 = Symbol.for(marker$1$4);
|
|
3719
3719
|
var _a$1$3;
|
|
@@ -18304,6 +18304,358 @@ function safeSlice(str, end) {
|
|
|
18304
18304
|
return str.slice(0, safeEnd);
|
|
18305
18305
|
}
|
|
18306
18306
|
//#endregion
|
|
18307
|
+
//#region src/processors/observational-memory/tool-argument-helpers.ts
|
|
18308
|
+
const DEFAULT_OBSERVER_TOOL_ARGUMENT_MAX_TOKENS = 2e3;
|
|
18309
|
+
const OBSERVER_TOOL_ARGUMENT_INLINE_STRING_MAX_CHARS = 160;
|
|
18310
|
+
const OBSERVER_TOOL_ARGUMENT_MAX_DEPTH = 5;
|
|
18311
|
+
const OBSERVER_TOOL_ARGUMENT_MAX_ENTRIES_PER_CONTAINER = 20;
|
|
18312
|
+
const OBSERVER_TOOL_ARGUMENT_MAX_OUTLINE_ENTRIES = 100;
|
|
18313
|
+
const OBSERVER_TOOL_ARGUMENT_MAX_PATH_CHARS = 160;
|
|
18314
|
+
function isObjectLike$1(value) {
|
|
18315
|
+
return typeof value === "object" && value !== null;
|
|
18316
|
+
}
|
|
18317
|
+
function sanitizeSurrogates(value) {
|
|
18318
|
+
let sanitized = "";
|
|
18319
|
+
for (let index = 0; index < value.length; index++) {
|
|
18320
|
+
const code = value.charCodeAt(index);
|
|
18321
|
+
if (code >= 55296 && code <= 56319) {
|
|
18322
|
+
const next = value.charCodeAt(index + 1);
|
|
18323
|
+
if (next >= 56320 && next <= 57343) {
|
|
18324
|
+
sanitized += value.charAt(index) + value.charAt(index + 1);
|
|
18325
|
+
index++;
|
|
18326
|
+
} else sanitized += "�";
|
|
18327
|
+
continue;
|
|
18328
|
+
}
|
|
18329
|
+
sanitized += code >= 56320 && code <= 57343 ? "�" : value[index];
|
|
18330
|
+
}
|
|
18331
|
+
return sanitized;
|
|
18332
|
+
}
|
|
18333
|
+
function stablePathDigest(value) {
|
|
18334
|
+
let hash = 2166136261;
|
|
18335
|
+
for (let index = 0; index < value.length; index++) hash = Math.imul(hash ^ value.charCodeAt(index), 16777619);
|
|
18336
|
+
return (hash >>> 0).toString(16).padStart(8, "0");
|
|
18337
|
+
}
|
|
18338
|
+
function safeTail(value, length) {
|
|
18339
|
+
let start = Math.max(0, value.length - length);
|
|
18340
|
+
if (start > 0) {
|
|
18341
|
+
const code = value.charCodeAt(start);
|
|
18342
|
+
if (code >= 56320 && code <= 57343) start++;
|
|
18343
|
+
}
|
|
18344
|
+
return value.slice(start);
|
|
18345
|
+
}
|
|
18346
|
+
function truncatePath(path) {
|
|
18347
|
+
if (path.length <= OBSERVER_TOOL_ARGUMENT_MAX_PATH_CHARS) return path;
|
|
18348
|
+
const headLength = 50;
|
|
18349
|
+
const tailLength = 60;
|
|
18350
|
+
const omitted = path.length - headLength - tailLength;
|
|
18351
|
+
return `${safeSlice(path, headLength)}…[${omitted} chars; ${stablePathDigest(path)}]…${safeTail(path, tailLength)}`;
|
|
18352
|
+
}
|
|
18353
|
+
function formatPath(parent, key, isArray) {
|
|
18354
|
+
if (isArray) return truncatePath(`${parent}[${key}]`);
|
|
18355
|
+
const stringKey = String(key);
|
|
18356
|
+
const segment = /^[A-Za-z_$][A-Za-z0-9_$]*$/.test(stringKey) ? stringKey : `[${JSON.stringify(stringKey)}]`;
|
|
18357
|
+
return truncatePath(parent ? `${parent}.${segment}` : segment);
|
|
18358
|
+
}
|
|
18359
|
+
function formatPrimitive(value) {
|
|
18360
|
+
if (typeof value === "string") return JSON.stringify(value);
|
|
18361
|
+
if (typeof value === "bigint") return `<bigint> ${value}`;
|
|
18362
|
+
if (typeof value === "symbol" || typeof value === "function") return JSON.stringify(String(value));
|
|
18363
|
+
if (value === void 0) return "<undefined>";
|
|
18364
|
+
if (typeof value === "number" && Object.is(value, -0)) return "-0";
|
|
18365
|
+
return String(value);
|
|
18366
|
+
}
|
|
18367
|
+
function containerSummary(value) {
|
|
18368
|
+
if (!Array.isArray(value)) return "<object>";
|
|
18369
|
+
try {
|
|
18370
|
+
return `<array, ${value.length} ${value.length === 1 ? "item" : "items"}>`;
|
|
18371
|
+
} catch {
|
|
18372
|
+
return "<array, unknown length>";
|
|
18373
|
+
}
|
|
18374
|
+
}
|
|
18375
|
+
function readEntries(value) {
|
|
18376
|
+
const entries = [];
|
|
18377
|
+
try {
|
|
18378
|
+
if (Array.isArray(value)) {
|
|
18379
|
+
const length = value.length;
|
|
18380
|
+
const visibleLength = Math.min(length, OBSERVER_TOOL_ARGUMENT_MAX_ENTRIES_PER_CONTAINER);
|
|
18381
|
+
for (let index = 0; index < visibleLength; index++) {
|
|
18382
|
+
if (!(index in value)) {
|
|
18383
|
+
entries.push({
|
|
18384
|
+
key: index,
|
|
18385
|
+
unavailable: "hole"
|
|
18386
|
+
});
|
|
18387
|
+
continue;
|
|
18388
|
+
}
|
|
18389
|
+
try {
|
|
18390
|
+
entries.push({
|
|
18391
|
+
key: index,
|
|
18392
|
+
value: value[index]
|
|
18393
|
+
});
|
|
18394
|
+
} catch {
|
|
18395
|
+
entries.push({
|
|
18396
|
+
key: index,
|
|
18397
|
+
unavailable: "unreadable"
|
|
18398
|
+
});
|
|
18399
|
+
}
|
|
18400
|
+
}
|
|
18401
|
+
return {
|
|
18402
|
+
entries,
|
|
18403
|
+
hasMore: length > visibleLength,
|
|
18404
|
+
unavailable: false
|
|
18405
|
+
};
|
|
18406
|
+
}
|
|
18407
|
+
let hasMore = false;
|
|
18408
|
+
for (const key in value) {
|
|
18409
|
+
if (!Object.prototype.hasOwnProperty.call(value, key)) continue;
|
|
18410
|
+
if (entries.length >= OBSERVER_TOOL_ARGUMENT_MAX_ENTRIES_PER_CONTAINER) {
|
|
18411
|
+
hasMore = true;
|
|
18412
|
+
break;
|
|
18413
|
+
}
|
|
18414
|
+
try {
|
|
18415
|
+
entries.push({
|
|
18416
|
+
key,
|
|
18417
|
+
value: value[key]
|
|
18418
|
+
});
|
|
18419
|
+
} catch {
|
|
18420
|
+
entries.push({
|
|
18421
|
+
key,
|
|
18422
|
+
unavailable: "unreadable"
|
|
18423
|
+
});
|
|
18424
|
+
}
|
|
18425
|
+
}
|
|
18426
|
+
return {
|
|
18427
|
+
entries,
|
|
18428
|
+
hasMore,
|
|
18429
|
+
unavailable: false
|
|
18430
|
+
};
|
|
18431
|
+
} catch {
|
|
18432
|
+
return {
|
|
18433
|
+
entries,
|
|
18434
|
+
hasMore: false,
|
|
18435
|
+
unavailable: true
|
|
18436
|
+
};
|
|
18437
|
+
}
|
|
18438
|
+
}
|
|
18439
|
+
function fits(text, limits) {
|
|
18440
|
+
return text.length <= limits.maxCharacters && estimateTokenCount(text) <= limits.maxTokens;
|
|
18441
|
+
}
|
|
18442
|
+
function joinEntries(entries, marker) {
|
|
18443
|
+
const lines = [...entries].sort((a, b) => a.order - b.order).map((entry) => entry.text);
|
|
18444
|
+
if (marker) lines.push(marker);
|
|
18445
|
+
return lines.join("\n");
|
|
18446
|
+
}
|
|
18447
|
+
function selectOutlineEntries(entries, limits) {
|
|
18448
|
+
const all = joinEntries(entries);
|
|
18449
|
+
if (fits(all, limits)) return all;
|
|
18450
|
+
const marker = "... [additional argument fields omitted by size limit]";
|
|
18451
|
+
const selected = [];
|
|
18452
|
+
const candidates = [...entries].sort((a, b) => {
|
|
18453
|
+
if (a.depth !== b.depth) return a.depth - b.depth;
|
|
18454
|
+
return estimateTokenCount(a.text) - estimateTokenCount(b.text) || a.text.length - b.text.length || a.order - b.order;
|
|
18455
|
+
});
|
|
18456
|
+
for (const candidate of candidates) if (fits(joinEntries([...selected, candidate], marker), limits)) selected.push(candidate);
|
|
18457
|
+
const rendered = joinEntries(selected, marker);
|
|
18458
|
+
if (fits(rendered, limits)) return rendered;
|
|
18459
|
+
if (fits(marker, limits)) return marker;
|
|
18460
|
+
return fits("…", limits) ? "…" : "";
|
|
18461
|
+
}
|
|
18462
|
+
function indentPreview(value) {
|
|
18463
|
+
return value.split("\n").map((line) => ` | ${line}`).join("\n");
|
|
18464
|
+
}
|
|
18465
|
+
function formatPreview(path, value, limits, prefix = "") {
|
|
18466
|
+
const sanitizedValue = sanitizeSurrogates(value);
|
|
18467
|
+
const marker = (visible) => {
|
|
18468
|
+
const omitted = sanitizedValue.length - visible.length;
|
|
18469
|
+
return `${path}:\n${indentPreview(visible)}${omitted > 0 ? `\n | ... [${omitted} characters omitted]` : ""}`;
|
|
18470
|
+
};
|
|
18471
|
+
let low = 0;
|
|
18472
|
+
let high = sanitizedValue.length;
|
|
18473
|
+
let best = "";
|
|
18474
|
+
while (low <= high) {
|
|
18475
|
+
const mid = Math.floor((low + high) / 2);
|
|
18476
|
+
const candidate = marker(safeSlice(sanitizedValue, mid));
|
|
18477
|
+
if (fits(prefix ? `${prefix}\n${candidate}` : candidate, limits)) {
|
|
18478
|
+
best = candidate;
|
|
18479
|
+
low = mid + 1;
|
|
18480
|
+
} else high = mid - 1;
|
|
18481
|
+
}
|
|
18482
|
+
return best;
|
|
18483
|
+
}
|
|
18484
|
+
function appendWithinBudget(base, addition, limits) {
|
|
18485
|
+
if (!addition) return;
|
|
18486
|
+
const candidate = base ? `${base}\n${addition}` : addition;
|
|
18487
|
+
return fits(candidate, limits) ? candidate : void 0;
|
|
18488
|
+
}
|
|
18489
|
+
/**
|
|
18490
|
+
* Formats tool arguments for the Observer without allowing a large early value
|
|
18491
|
+
* to hide concise sibling fields. A breadth-first structural outline is selected
|
|
18492
|
+
* before bounded previews of large strings are added.
|
|
18493
|
+
*/
|
|
18494
|
+
function formatToolArgumentsForObserver(value, options) {
|
|
18495
|
+
const limits = {
|
|
18496
|
+
maxTokens: options?.maxTokens ?? DEFAULT_OBSERVER_TOOL_ARGUMENT_MAX_TOKENS,
|
|
18497
|
+
maxCharacters: options?.maxCharacters ?? Number.POSITIVE_INFINITY
|
|
18498
|
+
};
|
|
18499
|
+
if (limits.maxTokens <= 0 || limits.maxCharacters <= 0) return "";
|
|
18500
|
+
if (!isObjectLike$1(value)) {
|
|
18501
|
+
if (typeof value !== "string" || value.length <= OBSERVER_TOOL_ARGUMENT_INLINE_STRING_MAX_CHARS) {
|
|
18502
|
+
const primitive = formatPrimitive(value);
|
|
18503
|
+
return fits(primitive, limits) ? primitive : fits("…", limits) ? "…" : "";
|
|
18504
|
+
}
|
|
18505
|
+
const summary = `<string, ${value.length} characters; preview size-limited>`;
|
|
18506
|
+
const rendered = appendWithinBudget(summary, formatPreview("preview", value, limits, summary), limits);
|
|
18507
|
+
if (rendered) return rendered;
|
|
18508
|
+
if (fits(summary, limits)) return summary;
|
|
18509
|
+
return fits("…", limits) ? "…" : "";
|
|
18510
|
+
}
|
|
18511
|
+
const outline = [];
|
|
18512
|
+
const previews = [];
|
|
18513
|
+
const pending = [{
|
|
18514
|
+
path: "",
|
|
18515
|
+
value,
|
|
18516
|
+
depth: 0,
|
|
18517
|
+
ancestors: /* @__PURE__ */ new Set([value])
|
|
18518
|
+
}];
|
|
18519
|
+
let outlineEntries = 0;
|
|
18520
|
+
let order = 0;
|
|
18521
|
+
let structuralLimitReached = false;
|
|
18522
|
+
while (pending.length > 0 && outlineEntries < OBSERVER_TOOL_ARGUMENT_MAX_OUTLINE_ENTRIES) {
|
|
18523
|
+
const current = pending.shift();
|
|
18524
|
+
const read = readEntries(current.value);
|
|
18525
|
+
if (read.unavailable) {
|
|
18526
|
+
outline.push({
|
|
18527
|
+
text: `${current.path || "arguments"}: [unavailable container]`,
|
|
18528
|
+
depth: current.depth,
|
|
18529
|
+
order: order++
|
|
18530
|
+
});
|
|
18531
|
+
outlineEntries++;
|
|
18532
|
+
continue;
|
|
18533
|
+
}
|
|
18534
|
+
if (read.entries.length === 0 && !read.hasMore) {
|
|
18535
|
+
outline.push({
|
|
18536
|
+
text: `${current.path || "arguments"}: [empty ${Array.isArray(current.value) ? "array" : "object"}]`,
|
|
18537
|
+
depth: current.depth,
|
|
18538
|
+
order: order++
|
|
18539
|
+
});
|
|
18540
|
+
outlineEntries++;
|
|
18541
|
+
continue;
|
|
18542
|
+
}
|
|
18543
|
+
if (read.hasMore) {
|
|
18544
|
+
outline.push({
|
|
18545
|
+
text: `${current.path || (Array.isArray(current.value) ? "items" : "fields")}: ... [additional entries omitted]`,
|
|
18546
|
+
depth: current.depth,
|
|
18547
|
+
order: order++
|
|
18548
|
+
});
|
|
18549
|
+
outlineEntries++;
|
|
18550
|
+
}
|
|
18551
|
+
for (const entry of read.entries) {
|
|
18552
|
+
if (outlineEntries >= OBSERVER_TOOL_ARGUMENT_MAX_OUTLINE_ENTRIES) {
|
|
18553
|
+
structuralLimitReached = true;
|
|
18554
|
+
break;
|
|
18555
|
+
}
|
|
18556
|
+
const path = formatPath(current.path, entry.key, Array.isArray(current.value));
|
|
18557
|
+
outlineEntries++;
|
|
18558
|
+
if (entry.unavailable) {
|
|
18559
|
+
outline.push({
|
|
18560
|
+
text: `${path}: [${entry.unavailable}]`,
|
|
18561
|
+
depth: current.depth,
|
|
18562
|
+
order: order++
|
|
18563
|
+
});
|
|
18564
|
+
continue;
|
|
18565
|
+
}
|
|
18566
|
+
const entryValue = entry.value;
|
|
18567
|
+
if (typeof entryValue === "string" && entryValue.length > OBSERVER_TOOL_ARGUMENT_INLINE_STRING_MAX_CHARS) {
|
|
18568
|
+
outline.push({
|
|
18569
|
+
text: `${path}: <string, ${entryValue.length} characters; preview size-limited>`,
|
|
18570
|
+
depth: current.depth,
|
|
18571
|
+
order: order++
|
|
18572
|
+
});
|
|
18573
|
+
previews.push({
|
|
18574
|
+
path,
|
|
18575
|
+
value: entryValue
|
|
18576
|
+
});
|
|
18577
|
+
continue;
|
|
18578
|
+
}
|
|
18579
|
+
if (!isObjectLike$1(entryValue)) {
|
|
18580
|
+
outline.push({
|
|
18581
|
+
text: `${path}: ${formatPrimitive(entryValue)}`,
|
|
18582
|
+
depth: current.depth,
|
|
18583
|
+
order: order++
|
|
18584
|
+
});
|
|
18585
|
+
continue;
|
|
18586
|
+
}
|
|
18587
|
+
if (current.ancestors.has(entryValue)) {
|
|
18588
|
+
outline.push({
|
|
18589
|
+
text: `${path}: [circular]`,
|
|
18590
|
+
depth: current.depth,
|
|
18591
|
+
order: order++
|
|
18592
|
+
});
|
|
18593
|
+
continue;
|
|
18594
|
+
}
|
|
18595
|
+
const summary = containerSummary(entryValue);
|
|
18596
|
+
if (current.depth >= OBSERVER_TOOL_ARGUMENT_MAX_DEPTH) {
|
|
18597
|
+
outline.push({
|
|
18598
|
+
text: `${path}: ${summary} [max depth reached]`,
|
|
18599
|
+
depth: current.depth,
|
|
18600
|
+
order: order++
|
|
18601
|
+
});
|
|
18602
|
+
continue;
|
|
18603
|
+
}
|
|
18604
|
+
outline.push({
|
|
18605
|
+
text: `${path}: ${summary}`,
|
|
18606
|
+
depth: current.depth,
|
|
18607
|
+
order: order++
|
|
18608
|
+
});
|
|
18609
|
+
pending.push({
|
|
18610
|
+
path,
|
|
18611
|
+
value: entryValue,
|
|
18612
|
+
depth: current.depth + 1,
|
|
18613
|
+
ancestors: /* @__PURE__ */ new Set([...current.ancestors, entryValue])
|
|
18614
|
+
});
|
|
18615
|
+
}
|
|
18616
|
+
}
|
|
18617
|
+
if (pending.length > 0 || structuralLimitReached) outline.push({
|
|
18618
|
+
text: "... [additional nested entries omitted by structure limit]",
|
|
18619
|
+
depth: Number.POSITIVE_INFINITY,
|
|
18620
|
+
order: order++
|
|
18621
|
+
});
|
|
18622
|
+
if (previews.length === 0) return selectOutlineEntries(outline, limits);
|
|
18623
|
+
const previewHeader = "Large string previews (size-limited):";
|
|
18624
|
+
const renderedOutline = selectOutlineEntries(outline, limits);
|
|
18625
|
+
const withPreviewHeader = appendWithinBudget(renderedOutline, previewHeader, limits);
|
|
18626
|
+
if (!withPreviewHeader) return renderedOutline;
|
|
18627
|
+
let rendered = withPreviewHeader;
|
|
18628
|
+
let renderedPreviews = 0;
|
|
18629
|
+
let omittedPreviews = 0;
|
|
18630
|
+
for (let index = 0; index < previews.length; index++) {
|
|
18631
|
+
const remainingCount = previews.length - index;
|
|
18632
|
+
const remainingTokens = Math.max(0, limits.maxTokens - estimateTokenCount(rendered));
|
|
18633
|
+
const remainingCharacters = Math.max(0, limits.maxCharacters - rendered.length - 1);
|
|
18634
|
+
const previewLimits = {
|
|
18635
|
+
maxTokens: estimateTokenCount(rendered) + Math.floor(remainingTokens / remainingCount),
|
|
18636
|
+
maxCharacters: rendered.length + 1 + Math.floor(remainingCharacters / remainingCount)
|
|
18637
|
+
};
|
|
18638
|
+
const preview = formatPreview(previews[index].path, previews[index].value, previewLimits, rendered);
|
|
18639
|
+
if (!preview) {
|
|
18640
|
+
omittedPreviews++;
|
|
18641
|
+
continue;
|
|
18642
|
+
}
|
|
18643
|
+
const withPreview = appendWithinBudget(rendered, preview, limits);
|
|
18644
|
+
if (!withPreview) {
|
|
18645
|
+
omittedPreviews++;
|
|
18646
|
+
continue;
|
|
18647
|
+
}
|
|
18648
|
+
rendered = withPreview;
|
|
18649
|
+
renderedPreviews++;
|
|
18650
|
+
}
|
|
18651
|
+
if (renderedPreviews === 0) return renderedOutline;
|
|
18652
|
+
if (omittedPreviews > 0) {
|
|
18653
|
+
const marker = `... [${omittedPreviews} large string ${omittedPreviews === 1 ? "preview" : "previews"} omitted]`;
|
|
18654
|
+
rendered = appendWithinBudget(rendered, marker, limits) ?? rendered;
|
|
18655
|
+
}
|
|
18656
|
+
return rendered;
|
|
18657
|
+
}
|
|
18658
|
+
//#endregion
|
|
18307
18659
|
//#region src/processors/observational-memory/tool-result-helpers.ts
|
|
18308
18660
|
const ENCRYPTED_CONTENT_KEY = "encryptedContent";
|
|
18309
18661
|
const ENCRYPTED_CONTENT_REDACTION_THRESHOLD = 256;
|
|
@@ -18382,7 +18734,7 @@ function truncateStringByTokens(text, maxTokens) {
|
|
|
18382
18734
|
return best;
|
|
18383
18735
|
}
|
|
18384
18736
|
function formatToolResultForObserver(value, options) {
|
|
18385
|
-
return truncateStringByTokens(stringifyToolResult(value), options?.maxTokens ??
|
|
18737
|
+
return truncateStringByTokens(stringifyToolResult(value), options?.maxTokens ?? 5e3);
|
|
18386
18738
|
}
|
|
18387
18739
|
//#endregion
|
|
18388
18740
|
//#region src/processors/observational-memory/observer-agent.ts
|
|
@@ -19069,9 +19421,52 @@ function getTemporalGapMarkerText(msg) {
|
|
|
19069
19421
|
if (metadata?.reminderType === "temporal-gap" && typeof metadata.gapText === "string") return metadata.gapText;
|
|
19070
19422
|
if (typeof metadata?.systemReminder === "object" && metadata.systemReminder && "type" in metadata.systemReminder && metadata.systemReminder.type === "temporal-gap" && "gapText" in metadata.systemReminder && typeof metadata.systemReminder.gapText === "string") return metadata.systemReminder.gapText;
|
|
19071
19423
|
}
|
|
19072
|
-
function
|
|
19424
|
+
function isCallLikeToolInvocation(invocation) {
|
|
19425
|
+
return invocation.state === "call" || invocation.state === "partial-call" || invocation.state === "approval-requested" || invocation.state === "approval-responded";
|
|
19426
|
+
}
|
|
19427
|
+
function isTerminalToolInvocation(invocation) {
|
|
19428
|
+
return invocation.state === "result" || invocation.state === "output-error" || invocation.state === "output-denied";
|
|
19429
|
+
}
|
|
19430
|
+
function createObserverToolFormattingState(messages) {
|
|
19431
|
+
const exchanges = /* @__PURE__ */ new Map();
|
|
19432
|
+
const occurrences = /* @__PURE__ */ new Map();
|
|
19433
|
+
let occurrence = 0;
|
|
19434
|
+
for (const message of messages) {
|
|
19435
|
+
if (typeof message.content === "string" || !Array.isArray(message.content?.parts)) continue;
|
|
19436
|
+
for (const part of message.content.parts) {
|
|
19437
|
+
if (part.type !== "tool-invocation") continue;
|
|
19438
|
+
const invocation = part.toolInvocation;
|
|
19439
|
+
occurrences.set(part, occurrence);
|
|
19440
|
+
if (!isCallLikeToolInvocation(invocation) && !isTerminalToolInvocation(invocation)) {
|
|
19441
|
+
occurrence++;
|
|
19442
|
+
continue;
|
|
19443
|
+
}
|
|
19444
|
+
const exchange = exchanges.get(invocation.toolCallId) ?? { firstOccurrence: occurrence };
|
|
19445
|
+
if (isCallLikeToolInvocation(invocation) && (invocation.args !== void 0 || exchange.signature === void 0)) exchange.signature = invocation;
|
|
19446
|
+
if (isTerminalToolInvocation(invocation)) exchange.terminal = {
|
|
19447
|
+
...invocation,
|
|
19448
|
+
occurrence
|
|
19449
|
+
};
|
|
19450
|
+
exchanges.set(invocation.toolCallId, exchange);
|
|
19451
|
+
occurrence++;
|
|
19452
|
+
}
|
|
19453
|
+
}
|
|
19454
|
+
for (const exchange of exchanges.values()) if (exchange.terminal?.args !== void 0) exchange.signature = exchange.terminal;
|
|
19455
|
+
else if (!exchange.signature && exchange.terminal) exchange.signature = exchange.terminal;
|
|
19456
|
+
return {
|
|
19457
|
+
exchanges,
|
|
19458
|
+
occurrences,
|
|
19459
|
+
emittedCalls: /* @__PURE__ */ new Set(),
|
|
19460
|
+
emittedOutcomes: /* @__PURE__ */ new Set()
|
|
19461
|
+
};
|
|
19462
|
+
}
|
|
19463
|
+
function formatObserverToolArguments(args, maxCharacters) {
|
|
19464
|
+
if (args === void 0) return maybeTruncate("[arguments unavailable]", maxCharacters);
|
|
19465
|
+
return formatToolArgumentsForObserver(args, { maxCharacters: maxCharacters || void 0 });
|
|
19466
|
+
}
|
|
19467
|
+
function formatObserverMessage(msg, counter, toolFormatting, options) {
|
|
19073
19468
|
const maxLen = options?.maxPartLength;
|
|
19074
|
-
const maxToolResultTokens = options?.maxToolResultTokens ??
|
|
19469
|
+
const maxToolResultTokens = options?.maxToolResultTokens ?? 5e3;
|
|
19075
19470
|
const attachmentFilter = options?.attachmentFilter;
|
|
19076
19471
|
const role = msg.role.charAt(0).toUpperCase() + msg.role.slice(1);
|
|
19077
19472
|
const attachments = [];
|
|
@@ -19098,14 +19493,31 @@ function formatObserverMessage(msg, counter, options) {
|
|
|
19098
19493
|
}
|
|
19099
19494
|
if (part.type === "tool-invocation") {
|
|
19100
19495
|
const inv = part.toolInvocation;
|
|
19496
|
+
const occurrence = toolFormatting.occurrences.get(part);
|
|
19497
|
+
const exchange = toolFormatting.exchanges.get(inv.toolCallId);
|
|
19498
|
+
if (exchange && occurrence === exchange.firstOccurrence && exchange.signature && !toolFormatting.emittedCalls.has(inv.toolCallId)) {
|
|
19499
|
+
toolFormatting.emittedCalls.add(inv.toolCallId);
|
|
19500
|
+
pushLine(`Tool Call ${exchange.signature.toolName}`, formatObserverToolArguments(exchange.signature.args, maxLen), partCreatedAt);
|
|
19501
|
+
}
|
|
19101
19502
|
if (inv.state === "result") {
|
|
19102
19503
|
const { value: resultForObserver } = resolveToolResultValue(part, inv.result);
|
|
19103
19504
|
const { resultWithoutAttachments, attachments: extractedAttachments } = extractToolResultAttachments(resultForObserver, counter, attachmentFilter);
|
|
19104
19505
|
if (extractedAttachments.length > 0) attachments.push(...extractedAttachments);
|
|
19105
|
-
|
|
19506
|
+
if (exchange?.terminal?.occurrence === occurrence && !toolFormatting.emittedOutcomes.has(inv.toolCallId)) {
|
|
19507
|
+
toolFormatting.emittedOutcomes.add(inv.toolCallId);
|
|
19508
|
+
const hasResult = resultWithoutAttachments !== void 0 && (typeof resultWithoutAttachments !== "string" || resultWithoutAttachments.trim().length > 0);
|
|
19509
|
+
const body = inv.isError ? hasResult ? formatToolResultForObserver(resultWithoutAttachments, { maxTokens: maxToolResultTokens }) : inv.errorText?.trim() ? inv.errorText : "Tool execution failed" : hasResult ? formatToolResultForObserver(resultWithoutAttachments, { maxTokens: maxToolResultTokens }) : "[empty result]";
|
|
19510
|
+
pushLine(`${inv.isError ? "Tool Error" : "Tool Result"} ${inv.toolName}`, maybeTruncate(body, maxLen), partCreatedAt);
|
|
19511
|
+
}
|
|
19512
|
+
return;
|
|
19513
|
+
}
|
|
19514
|
+
if (exchange?.terminal?.occurrence === occurrence && !toolFormatting.emittedOutcomes.has(inv.toolCallId)) {
|
|
19515
|
+
toolFormatting.emittedOutcomes.add(inv.toolCallId);
|
|
19516
|
+
if (inv.state === "output-error") pushLine(`Tool Error ${inv.toolName}`, maybeTruncate(inv.errorText?.trim() ? inv.errorText : "Tool execution failed", maxLen), partCreatedAt);
|
|
19517
|
+
else if (inv.state === "output-denied") pushLine(`Tool Denied ${inv.toolName}`, maybeTruncate(inv.approval?.reason?.trim() ? inv.approval.reason : "Tool call was not approved by the user", maxLen), partCreatedAt);
|
|
19106
19518
|
return;
|
|
19107
19519
|
}
|
|
19108
|
-
pushLine(`Tool Call ${inv.toolName}`,
|
|
19520
|
+
if (!exchange) pushLine(`Tool Call ${inv.toolName}`, formatObserverToolArguments(inv.args, maxLen), partCreatedAt);
|
|
19109
19521
|
return;
|
|
19110
19522
|
}
|
|
19111
19523
|
const partType = part.type;
|
|
@@ -19139,10 +19551,11 @@ function formatMessagesForObserver(messages, options) {
|
|
|
19139
19551
|
nextImageId: 1,
|
|
19140
19552
|
nextFileId: 1
|
|
19141
19553
|
};
|
|
19554
|
+
const toolFormatting = createObserverToolFormattingState(messages);
|
|
19142
19555
|
const sections = [];
|
|
19143
19556
|
let context = {};
|
|
19144
19557
|
for (const message of messages) {
|
|
19145
|
-
const formatted = formatObserverMessage(message, counter, options);
|
|
19558
|
+
const formatted = formatObserverMessage(message, counter, toolFormatting, options);
|
|
19146
19559
|
if (formatted.lines.length === 0) continue;
|
|
19147
19560
|
const rendered = formatObserverLines(formatted.lines, context);
|
|
19148
19561
|
if (!rendered.text) continue;
|
|
@@ -19165,13 +19578,14 @@ function buildObserverHistoryMessage(messages, options) {
|
|
|
19165
19578
|
nextImageId: 1,
|
|
19166
19579
|
nextFileId: 1
|
|
19167
19580
|
};
|
|
19581
|
+
const toolFormatting = createObserverToolFormattingState(messages);
|
|
19168
19582
|
const content = [{
|
|
19169
19583
|
type: "text",
|
|
19170
19584
|
text: "## New Message History to Observe\n\n"
|
|
19171
19585
|
}];
|
|
19172
19586
|
let context = {};
|
|
19173
19587
|
messages.forEach((message) => {
|
|
19174
|
-
const formatted = formatObserverMessage(message, counter, options);
|
|
19588
|
+
const formatted = formatObserverMessage(message, counter, toolFormatting, options);
|
|
19175
19589
|
if (formatted.lines.length === 0 && formatted.attachments.length === 0) return;
|
|
19176
19590
|
context = appendFormattedObserverMessage(content, formatted, context);
|
|
19177
19591
|
});
|
|
@@ -19199,10 +19613,11 @@ function buildMultiThreadObserverHistoryMessage(messagesByThread, threadOrder, o
|
|
|
19199
19613
|
const messages = messagesByThread.get(threadId);
|
|
19200
19614
|
if (!messages || messages.length === 0) return;
|
|
19201
19615
|
const threadContent = [];
|
|
19616
|
+
const toolFormatting = createObserverToolFormattingState(messages);
|
|
19202
19617
|
let context = {};
|
|
19203
19618
|
let hasVisibleContent = false;
|
|
19204
19619
|
messages.forEach((message) => {
|
|
19205
|
-
const formatted = formatObserverMessage(message, counter, options);
|
|
19620
|
+
const formatted = formatObserverMessage(message, counter, toolFormatting, options);
|
|
19206
19621
|
if (formatted.lines.length === 0 && formatted.attachments.length === 0) return;
|
|
19207
19622
|
context = appendFormattedObserverMessage(threadContent, formatted, context);
|
|
19208
19623
|
hasVisibleContent = true;
|
|
@@ -19898,15 +20313,14 @@ var ObserverRunner = class {
|
|
|
19898
20313
|
this.mastra = mastra;
|
|
19899
20314
|
}
|
|
19900
20315
|
createAgent(model, isMultiThread = false, memory, extractors = this.observationConfig.extractors ?? []) {
|
|
19901
|
-
|
|
20316
|
+
return new Agent({
|
|
19902
20317
|
id: isMultiThread ? "multi-thread-observer" : "observational-memory-observer",
|
|
19903
20318
|
name: isMultiThread ? "multi-thread-observer" : "Observer",
|
|
19904
20319
|
instructions: buildObserverSystemPrompt(isMultiThread, this.observationConfig.instruction, this.observationConfig.threadTitle, extractors),
|
|
19905
20320
|
model,
|
|
19906
|
-
...memory ? { memory } : {}
|
|
20321
|
+
...memory ? { memory } : {},
|
|
20322
|
+
...this.mastra ? { mastra: this.mastra } : {}
|
|
19907
20323
|
});
|
|
19908
|
-
if (this.mastra) agent.__registerMastra(this.mastra);
|
|
19909
|
-
return agent;
|
|
19910
20324
|
}
|
|
19911
20325
|
/**
|
|
19912
20326
|
* Extract a router-style model ID (`provider/model`) from a model config.
|
|
@@ -21552,7 +21966,7 @@ var TokenCounter = class TokenCounter {
|
|
|
21552
21966
|
let tokens = 0;
|
|
21553
21967
|
let overheadDelta = 0;
|
|
21554
21968
|
if (invocation.toolName) tokens += this.readOrPersistPartEstimate(part, "tool-call-name", invocation.toolName);
|
|
21555
|
-
if (invocation.args) if (typeof invocation.args === "string") tokens += this.readOrPersistPartEstimate(part, "tool-call-args", invocation.args);
|
|
21969
|
+
if (invocation.args !== void 0) if (typeof invocation.args === "string") tokens += this.readOrPersistPartEstimate(part, "tool-call-args", invocation.args);
|
|
21556
21970
|
else {
|
|
21557
21971
|
const argsJson = JSON.stringify(invocation.args);
|
|
21558
21972
|
tokens += this.readOrPersistPartEstimate(part, "tool-call-args-json", argsJson);
|
|
@@ -21598,8 +22012,11 @@ var TokenCounter = class TokenCounter {
|
|
|
21598
22012
|
}
|
|
21599
22013
|
if (state === "result") {
|
|
21600
22014
|
extraMessageDelta++;
|
|
22015
|
+
const signature = this.countToolCallSignature(part, invocation);
|
|
22016
|
+
tokens += signature.tokens;
|
|
22017
|
+
overheadDelta += signature.overheadDelta;
|
|
21601
22018
|
const { value: resultForCounting, usingStoredModelOutput } = this.resolveToolResultForTokenCounting(part, invocation.result);
|
|
21602
|
-
if (resultForCounting !== void 0) {
|
|
22019
|
+
if (resultForCounting !== void 0 && (typeof resultForCounting !== "string" || resultForCounting.trim().length > 0)) {
|
|
21603
22020
|
const contentTokens = this.countMultimodalToolResultContent(part, resultForCounting);
|
|
21604
22021
|
if (contentTokens !== void 0) tokens += contentTokens;
|
|
21605
22022
|
else {
|
|
@@ -21607,6 +22024,9 @@ var TokenCounter = class TokenCounter {
|
|
|
21607
22024
|
tokens += this.readOrPersistPartEstimate(part, usingStoredModelOutput ? "tool-result-model-output-json" : "tool-result-json", formattedResult);
|
|
21608
22025
|
}
|
|
21609
22026
|
if (typeof resultForCounting !== "string") overheadDelta -= 12;
|
|
22027
|
+
} else {
|
|
22028
|
+
const fallback = invocation.isError ? invocation.errorText?.trim() ? invocation.errorText : "Tool execution failed" : "[empty result]";
|
|
22029
|
+
tokens += this.readOrPersistPartEstimate(part, invocation.isError ? "tool-result-error" : "tool-result-json", fallback);
|
|
21610
22030
|
}
|
|
21611
22031
|
return {
|
|
21612
22032
|
tokens,
|
|
@@ -21616,7 +22036,10 @@ var TokenCounter = class TokenCounter {
|
|
|
21616
22036
|
}
|
|
21617
22037
|
if (state === "output-denied") {
|
|
21618
22038
|
extraMessageDelta++;
|
|
21619
|
-
const
|
|
22039
|
+
const signature = this.countToolCallSignature(part, invocation);
|
|
22040
|
+
tokens += signature.tokens;
|
|
22041
|
+
overheadDelta += signature.overheadDelta;
|
|
22042
|
+
const reason = invocation.approval?.reason?.trim() ? invocation.approval.reason : "Tool call was not approved by the user";
|
|
21620
22043
|
tokens += this.readOrPersistPartEstimate(part, "tool-result-denied", reason);
|
|
21621
22044
|
return {
|
|
21622
22045
|
tokens,
|
|
@@ -21626,7 +22049,10 @@ var TokenCounter = class TokenCounter {
|
|
|
21626
22049
|
}
|
|
21627
22050
|
if (state === "output-error") {
|
|
21628
22051
|
extraMessageDelta++;
|
|
21629
|
-
const
|
|
22052
|
+
const signature = this.countToolCallSignature(part, invocation);
|
|
22053
|
+
tokens += signature.tokens;
|
|
22054
|
+
overheadDelta += signature.overheadDelta;
|
|
22055
|
+
const errorMessage = invocation.errorText?.trim() ? invocation.errorText : "Tool execution failed";
|
|
21630
22056
|
tokens += this.readOrPersistPartEstimate(part, "tool-result-error", errorMessage);
|
|
21631
22057
|
return {
|
|
21632
22058
|
tokens,
|
|
@@ -21653,7 +22079,12 @@ var TokenCounter = class TokenCounter {
|
|
|
21653
22079
|
};
|
|
21654
22080
|
}
|
|
21655
22081
|
/**
|
|
21656
|
-
* Count tokens in a single message
|
|
22082
|
+
* Count tokens in a single message.
|
|
22083
|
+
*
|
|
22084
|
+
* Canonical terminal invocations carry their call signature and outcome in one part. Legacy or
|
|
22085
|
+
* foreign histories may instead repeat the same signature across separate call and result
|
|
22086
|
+
* messages; those signatures are intentionally counted once per message because this API has no
|
|
22087
|
+
* sequence context. The conservative overcount can activate observation earlier, never later.
|
|
21657
22088
|
*/
|
|
21658
22089
|
countMessage(message) {
|
|
21659
22090
|
let payloadTokens = this.countString(message.role);
|
|
@@ -26309,15 +26740,14 @@ var ReflectorRunner = class {
|
|
|
26309
26740
|
this.mastra = mastra;
|
|
26310
26741
|
}
|
|
26311
26742
|
createAgent(model, memory, extractors = this.reflectionConfig.extractors) {
|
|
26312
|
-
|
|
26743
|
+
return new Agent({
|
|
26313
26744
|
id: "observational-memory-reflector",
|
|
26314
26745
|
name: "Reflector",
|
|
26315
26746
|
instructions: buildReflectorSystemPrompt(this.reflectionConfig.instruction, extractors),
|
|
26316
26747
|
model,
|
|
26317
|
-
...memory ? { memory } : {}
|
|
26748
|
+
...memory ? { memory } : {},
|
|
26749
|
+
...this.mastra ? { mastra: this.mastra } : {}
|
|
26318
26750
|
});
|
|
26319
|
-
if (this.mastra) agent.__registerMastra(this.mastra);
|
|
26320
|
-
return agent;
|
|
26321
26751
|
}
|
|
26322
26752
|
getObservationMarkerConfig(record) {
|
|
26323
26753
|
return {
|
|
@@ -30368,6 +30798,9 @@ const DEFAULT_EMBEDDING_CACHE_MAX_SIZE = 1e3;
|
|
|
30368
30798
|
* if packaged docs are unavailable.
|
|
30369
30799
|
*/
|
|
30370
30800
|
var Memory = class Memory extends MastraMemory {
|
|
30801
|
+
createMemoryTokenCounter() {
|
|
30802
|
+
return new TokenCounter();
|
|
30803
|
+
}
|
|
30371
30804
|
_omEngine;
|
|
30372
30805
|
_omEngineInstance;
|
|
30373
30806
|
_mastraInstance;
|
|
@@ -30538,6 +30971,7 @@ var Memory = class Memory extends MastraMemory {
|
|
|
30538
30971
|
const thread = await this.getThreadById({ threadId });
|
|
30539
30972
|
if (!thread && !resourceScope) throw new Error(`No thread found with id ${threadId}`);
|
|
30540
30973
|
if (thread && thread.resourceId !== resourceId) throw new Error(`Thread with id ${threadId} is for resource with id ${thread.resourceId} but resource ${resourceId} was queried.`);
|
|
30974
|
+
return thread;
|
|
30541
30975
|
}
|
|
30542
30976
|
createMemorySpan(operationType, observabilityContext, input, attributes) {
|
|
30543
30977
|
const currentSpan = observabilityContext?.tracingContext?.currentSpan;
|
|
@@ -30558,6 +30992,9 @@ var Memory = class Memory extends MastraMemory {
|
|
|
30558
30992
|
const { threadId, resourceId, perPage: perPageArg, page, orderBy, threadConfig, vectorSearchString, includeSystemReminders, hideSignals, filter, includeTotal } = args;
|
|
30559
30993
|
const config = this.getMergedThreadConfig(threadConfig || {});
|
|
30560
30994
|
const semanticRecallEnabled = Boolean(config.semanticRecall);
|
|
30995
|
+
const history = normalizeMessageHistoryConfig(config.lastMessages, config.messageHistory);
|
|
30996
|
+
const historyDisabledByConfig = !history.enabled && perPageArg === void 0;
|
|
30997
|
+
const shouldUseTokenLoader = perPageArg === void 0 && history.enabled && history.maxTokens !== void 0;
|
|
30561
30998
|
const span = this.createMemorySpan("recall", args.observabilityContext, {
|
|
30562
30999
|
threadId,
|
|
30563
31000
|
resourceId,
|
|
@@ -30567,9 +31004,23 @@ var Memory = class Memory extends MastraMemory {
|
|
|
30567
31004
|
lastMessages: config.lastMessages
|
|
30568
31005
|
});
|
|
30569
31006
|
try {
|
|
30570
|
-
if (
|
|
30571
|
-
|
|
30572
|
-
|
|
31007
|
+
if (historyDisabledByConfig && (!config.semanticRecall || !vectorSearchString || !this.vector)) {
|
|
31008
|
+
const result = {
|
|
31009
|
+
messages: [],
|
|
31010
|
+
usage: void 0,
|
|
31011
|
+
total: 0,
|
|
31012
|
+
page: page ?? 0,
|
|
31013
|
+
perPage: 0,
|
|
31014
|
+
hasMore: false
|
|
31015
|
+
};
|
|
31016
|
+
span?.end({
|
|
31017
|
+
output: { success: true },
|
|
31018
|
+
attributes: { messageCount: 0 }
|
|
31019
|
+
});
|
|
31020
|
+
return result;
|
|
31021
|
+
}
|
|
31022
|
+
const validatedThread = resourceId ? await this.validateThreadIsOwnedByResource(threadId, resourceId, config) : void 0;
|
|
31023
|
+
const perPage = perPageArg !== void 0 ? perPageArg : history.maxMessages ?? 0;
|
|
30573
31024
|
const shouldGetNewestAndReverse = !orderBy && perPage !== false;
|
|
30574
31025
|
const effectiveOrderBy = shouldGetNewestAndReverse ? {
|
|
30575
31026
|
field: "createdAt",
|
|
@@ -30598,21 +31049,6 @@ var Memory = class Memory extends MastraMemory {
|
|
|
30598
31049
|
const resourceScope = typeof config?.semanticRecall === "object" && config?.semanticRecall?.scope !== `thread` || config.semanticRecall === true;
|
|
30599
31050
|
if (resourceScope && !resourceId && config?.semanticRecall && vectorSearchString) throw new Error("Memory error: Resource-scoped semantic recall is enabled but no resourceId was provided. Either provide a resourceId or explicitly set semanticRecall.scope to 'thread'.");
|
|
30600
31051
|
let usage;
|
|
30601
|
-
if (historyDisabledByConfig && (!config.semanticRecall || !vectorSearchString || !this.vector)) {
|
|
30602
|
-
const result = {
|
|
30603
|
-
messages: [],
|
|
30604
|
-
usage: void 0,
|
|
30605
|
-
total: 0,
|
|
30606
|
-
page: page ?? 0,
|
|
30607
|
-
perPage: 0,
|
|
30608
|
-
hasMore: false
|
|
30609
|
-
};
|
|
30610
|
-
span?.end({
|
|
30611
|
-
output: { success: true },
|
|
30612
|
-
attributes: { messageCount: 0 }
|
|
30613
|
-
});
|
|
30614
|
-
return result;
|
|
30615
|
-
}
|
|
30616
31052
|
if (config?.semanticRecall && vectorSearchString && this.vector) {
|
|
30617
31053
|
const result = await this.embedMessageContent(vectorSearchString);
|
|
30618
31054
|
usage = result.usage;
|
|
@@ -30634,28 +31070,71 @@ var Memory = class Memory extends MastraMemory {
|
|
|
30634
31070
|
const threshold = (typeof config.semanticRecall === "object" ? config.semanticRecall : void 0)?.threshold;
|
|
30635
31071
|
const filteredVectorResults = threshold !== void 0 ? vectorResults.filter((r) => r.score >= threshold) : vectorResults;
|
|
30636
31072
|
const memoryStore = await this.getMemoryStore();
|
|
30637
|
-
const
|
|
30638
|
-
|
|
30639
|
-
threadId,
|
|
30640
|
-
|
|
30641
|
-
|
|
30642
|
-
|
|
30643
|
-
|
|
30644
|
-
|
|
30645
|
-
|
|
30646
|
-
|
|
30647
|
-
|
|
30648
|
-
|
|
30649
|
-
|
|
30650
|
-
|
|
30651
|
-
|
|
30652
|
-
|
|
30653
|
-
|
|
31073
|
+
const include = filteredVectorResults.map((r) => ({
|
|
31074
|
+
id: r.metadata?.message_id,
|
|
31075
|
+
threadId: r.metadata?.thread_id,
|
|
31076
|
+
withNextMessages: typeof vectorConfig.messageRange === "number" ? vectorConfig.messageRange : vectorConfig.messageRange.after,
|
|
31077
|
+
withPreviousMessages: typeof vectorConfig.messageRange === "number" ? vectorConfig.messageRange : vectorConfig.messageRange.before
|
|
31078
|
+
}));
|
|
31079
|
+
let rawMessages;
|
|
31080
|
+
let resultPage;
|
|
31081
|
+
let resultPerPage;
|
|
31082
|
+
let total;
|
|
31083
|
+
let hasMore;
|
|
31084
|
+
if (shouldUseTokenLoader) {
|
|
31085
|
+
const storedBoundary = getMemoryTokenBoundary(validatedThread ?? await memoryStore.getThreadById({
|
|
31086
|
+
threadId,
|
|
31087
|
+
resourceId
|
|
31088
|
+
}));
|
|
31089
|
+
const boundary = storedBoundary !== void 0 && storedBoundary.maxTokens === history.maxTokens && storedBoundary.atMaxRemoveTokens === history.atMaxRemoveTokens ? storedBoundary : void 0;
|
|
31090
|
+
rawMessages = (await loadMessageHistory({
|
|
31091
|
+
storage: memoryStore,
|
|
31092
|
+
threadId,
|
|
31093
|
+
resourceId,
|
|
31094
|
+
boundary,
|
|
31095
|
+
filter,
|
|
31096
|
+
maxMessages: history.maxMessages,
|
|
31097
|
+
maxTokens: history.maxTokens,
|
|
31098
|
+
atMaxRemoveTokens: history.atMaxRemoveTokens,
|
|
31099
|
+
tokenCounter: new TokenCounter(),
|
|
31100
|
+
initialTokens: 24
|
|
31101
|
+
})).messages;
|
|
31102
|
+
if (include.length) {
|
|
31103
|
+
const semanticMessages = await memoryStore.listMessages({
|
|
31104
|
+
threadId,
|
|
31105
|
+
resourceId,
|
|
31106
|
+
perPage: 0,
|
|
31107
|
+
include,
|
|
31108
|
+
includeTotal: false
|
|
31109
|
+
});
|
|
31110
|
+
rawMessages = new MessageList({
|
|
31111
|
+
threadId,
|
|
31112
|
+
resourceId
|
|
31113
|
+
}).add(rawMessages, "memory").add(semanticMessages.messages.filter((message) => !boundary || isAfterMemoryTokenBoundary(message, boundary)), "memory").get.all.db();
|
|
31114
|
+
}
|
|
31115
|
+
resultPage = 0;
|
|
31116
|
+
resultPerPage = history.maxMessages ?? false;
|
|
31117
|
+
total = rawMessages.length;
|
|
31118
|
+
hasMore = false;
|
|
31119
|
+
} else {
|
|
31120
|
+
const effectivePerPage = historyDisabledByConfig ? 0 : perPage;
|
|
31121
|
+
const paginatedResult = await memoryStore.listMessages({
|
|
31122
|
+
threadId,
|
|
31123
|
+
resourceId,
|
|
31124
|
+
perPage: effectivePerPage,
|
|
31125
|
+
page,
|
|
31126
|
+
orderBy: effectiveOrderBy,
|
|
31127
|
+
filter,
|
|
31128
|
+
...includeTotal !== void 0 ? { includeTotal } : {},
|
|
31129
|
+
...include.length ? { include } : {}
|
|
31130
|
+
});
|
|
31131
|
+
rawMessages = shouldGetNewestAndReverse ? paginatedResult.messages.reverse() : paginatedResult.messages;
|
|
31132
|
+
({total, page: resultPage, perPage: resultPerPage, hasMore} = paginatedResult);
|
|
31133
|
+
}
|
|
30654
31134
|
const messages = filterSystemReminderMessages(new MessageList({
|
|
30655
31135
|
threadId,
|
|
30656
31136
|
resourceId
|
|
30657
31137
|
}).add(rawMessages, "memory").get.all.db(), includeSystemReminders, hideSignals);
|
|
30658
|
-
const { total, page: resultPage, perPage: resultPerPage, hasMore } = paginatedResult;
|
|
30659
31138
|
const recallResult = {
|
|
30660
31139
|
messages,
|
|
30661
31140
|
usage,
|
|
@@ -31276,16 +31755,52 @@ ${workingMemory}`;
|
|
|
31276
31755
|
messages = runState ? await runState.load(`observational-memory:messages:thread:${threadId}:${boundary}`, loadMessages) : await loadMessages();
|
|
31277
31756
|
}
|
|
31278
31757
|
} else {
|
|
31279
|
-
const lastMessages = config.lastMessages;
|
|
31280
|
-
if (lastMessages
|
|
31281
|
-
else
|
|
31758
|
+
const lastMessages = normalizeMessageHistoryConfig(config.lastMessages, config.messageHistory);
|
|
31759
|
+
if (!lastMessages.enabled) messages = [];
|
|
31760
|
+
else if (lastMessages.maxTokens !== void 0) {
|
|
31761
|
+
const storedBoundary = getMemoryTokenBoundary(await memoryStore.getThreadById({
|
|
31762
|
+
threadId,
|
|
31763
|
+
resourceId
|
|
31764
|
+
}));
|
|
31765
|
+
const boundary = storedBoundary?.maxTokens === lastMessages.maxTokens && storedBoundary.atMaxRemoveTokens === lastMessages.atMaxRemoveTokens ? storedBoundary : void 0;
|
|
31766
|
+
const tokenCounter = this.createMemoryTokenCounter();
|
|
31767
|
+
const loaded = await loadMessageHistory({
|
|
31768
|
+
storage: memoryStore,
|
|
31769
|
+
threadId,
|
|
31770
|
+
resourceId,
|
|
31771
|
+
boundary,
|
|
31772
|
+
maxMessages: lastMessages.maxMessages,
|
|
31773
|
+
maxTokens: lastMessages.maxTokens,
|
|
31774
|
+
tokenCounter,
|
|
31775
|
+
initialTokens: 24
|
|
31776
|
+
});
|
|
31777
|
+
const messageList = new MessageList();
|
|
31778
|
+
messageList.add(loaded.messages, "memory");
|
|
31779
|
+
if (systemParts.length) messageList.addSystem(systemParts.join("\n\n"));
|
|
31780
|
+
await new TokenLimiterProcessor({
|
|
31781
|
+
limit: lastMessages.maxTokens,
|
|
31782
|
+
atMaxRemoveTokens: lastMessages.atMaxRemoveTokens,
|
|
31783
|
+
trimMode: "memory-only",
|
|
31784
|
+
tokenCounter
|
|
31785
|
+
}).processInput({
|
|
31786
|
+
messageList,
|
|
31787
|
+
messages: messageList.get.all.db(),
|
|
31788
|
+
systemMessages: messageList.getAllSystemMessages(),
|
|
31789
|
+
state: {},
|
|
31790
|
+
retryCount: 0,
|
|
31791
|
+
abort: (reason) => {
|
|
31792
|
+
throw new Error(reason);
|
|
31793
|
+
}
|
|
31794
|
+
});
|
|
31795
|
+
messages = messageList.get.all.db();
|
|
31796
|
+
} else messages = (await memoryStore.listMessages({
|
|
31282
31797
|
threadId,
|
|
31283
31798
|
resourceId,
|
|
31284
31799
|
orderBy: {
|
|
31285
31800
|
field: "createdAt",
|
|
31286
31801
|
direction: "DESC"
|
|
31287
31802
|
},
|
|
31288
|
-
perPage:
|
|
31803
|
+
perPage: lastMessages.maxMessages,
|
|
31289
31804
|
includeTotal: false
|
|
31290
31805
|
})).messages.reverse();
|
|
31291
31806
|
}
|
|
@@ -32427,4 +32942,4 @@ Notes:
|
|
|
32427
32942
|
//#endregion
|
|
32428
32943
|
export { formatMessagesForObserver as A, OBSERVATION_CONTINUATION_HINT as B, SUMMARIZE_THREAD_DEFAULTS as C, buildObserverPrompt as D, OBSERVER_SYSTEM_PROMPT as E, parseAnchorId as F, ModelByInputTokens as G, KnowledgeSemanticIndexCoordinator as H, stripEphemeralAnchorIds as I, publishSubconsciousActivity as J, SUBCONSCIOUS_ACTIVITY_STATE_ID as K, OBSERVATIONAL_MEMORY_DEFAULTS as L, optimizeObservationsForContext as M, parseObserverOutput as N, buildObserverSystemPrompt as O, injectAnchorIds as P, OBSERVATION_CONTEXT_INSTRUCTIONS as R, WorkingMemoryExtractor as S, TokenCounter as T, StaleKnowledgeSemanticIndexError as U, Subconscious as V, SubconsciousRemindExtractor as W, Extractor as X, renderSubconsciousActivity as Y, wrapInObservationGroup as _, extractWorkingMemoryContent as a, WorkingMemoryStateProcessor as b, getObservationsAsOf as c, combineObservationGroupRanges as d, deriveObservationGroupProvenance as f, stripObservationGroups as g, renderObservationGroupsForReflection as h, WorkingMemory as i, hasCurrentTaskSection as j, extractCurrentTask as k, ObservationalMemoryProcessor as l, reconcileObservationGroupsFromReflection as m, MessageHistory$1 as n, extractWorkingMemoryTags as o, parseObservationGroups as p, buildSubconsciousActivitySnapshot as q, SemanticRecall as r, removeWorkingMemoryTags as s, Memory as t, ObservationalMemory as u, WORKING_MEMORY_STATE_ID as v, summarizeConversation as w, deepMergeWorkingMemory as x, WORKING_MEMORY_STATE_PROCESSOR_ID as y, OBSERVATION_CONTEXT_PROMPT as z };
|
|
32429
32944
|
|
|
32430
|
-
//# sourceMappingURL=src-
|
|
32945
|
+
//# sourceMappingURL=src-DqLGJfSb.js.map
|