@mastra/memory 1.27.0 → 1.28.0-alpha.3
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 +63 -0
- package/dist/docs/SKILL.md +5 -2
- package/dist/docs/assets/SOURCE_MAP.json +1 -1
- package/dist/docs/references/docs-guides-context-engineering.md +297 -0
- package/dist/docs/references/docs-storage.md +2 -0
- package/dist/docs/references/integrations-databases-elasticsearch.md +156 -0
- package/dist/docs/references/integrations-databases-libsql.md +16 -0
- package/dist/docs/references/integrations-databases-mongodb.md +1 -1
- package/dist/docs/references/integrations-databases-postgresql.md +26 -0
- package/dist/docs/references/integrations-databases-valkey.md +99 -0
- package/dist/docs/references/reference-vectors-mongodb.md +11 -11
- package/dist/docs/references/reference-vectors-pg.md +2 -0
- package/dist/index.cjs +1 -1
- package/dist/index.d.ts +6 -3
- 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/observation-turn/load-memory-context.d.ts +3 -1
- package/dist/processors/observational-memory/observation-turn/load-memory-context.d.ts.map +1 -1
- package/dist/processors/observational-memory/observation-turn/turn.d.ts +2 -1
- package/dist/processors/observational-memory/observation-turn/turn.d.ts.map +1 -1
- package/dist/processors/observational-memory/observational-memory.d.ts +6 -12
- 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/processor.d.ts +2 -0
- package/dist/processors/observational-memory/processor.d.ts.map +1 -1
- package/dist/processors/observational-memory/reflector-runner.d.ts +15 -0
- package/dist/processors/observational-memory/reflector-runner.d.ts.map +1 -1
- package/dist/processors/observational-memory/tracing.d.ts.map +1 -1
- package/dist/processors/observational-memory/types.d.ts +23 -13
- package/dist/processors/observational-memory/types.d.ts.map +1 -1
- package/dist/{src-aTfbkxBl.js → src-BTHCV1ov.js} +314 -164
- package/dist/src-BTHCV1ov.js.map +1 -0
- package/dist/{src-CxkQ-ICG.cjs → src-D8byb8Ge.cjs} +314 -164
- package/dist/src-D8byb8Ge.cjs.map +1 -0
- package/package.json +7 -7
- package/dist/src-CxkQ-ICG.cjs.map +0 -1
- package/dist/src-aTfbkxBl.js.map +0 -1
|
@@ -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-nVfjvSbw.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;
|
|
@@ -4266,6 +4266,7 @@ async function cancelResponseBody$1(response) {
|
|
|
4266
4266
|
var initialGlobalFetch$1 = globalThis.fetch;
|
|
4267
4267
|
isNodeDefaultFetch$1(initialGlobalFetch$1);
|
|
4268
4268
|
function isNodeDefaultFetch$1(fetch) {
|
|
4269
|
+
if (typeof fetch !== "function") return false;
|
|
4269
4270
|
const source = Function.prototype.toString.call(fetch);
|
|
4270
4271
|
return source.includes("internal/deps/undici") || source.includes("lazy loading of undici");
|
|
4271
4272
|
}
|
|
@@ -8601,7 +8602,7 @@ var object$1 = ({ schema: inputSchema }) => {
|
|
|
8601
8602
|
};
|
|
8602
8603
|
};
|
|
8603
8604
|
//#endregion
|
|
8604
|
-
//#region ../_vendored/ai_v6/dist/dist-
|
|
8605
|
+
//#region ../_vendored/ai_v6/dist/dist-Gd4EIxPM.js
|
|
8605
8606
|
var marker$1 = "vercel.ai.error";
|
|
8606
8607
|
var symbol$1$1 = Symbol.for(marker$1);
|
|
8607
8608
|
var _a$1$1;
|
|
@@ -9175,6 +9176,7 @@ var DownloadError = class extends (_b$2 = AISDKError, _a$2 = symbol$2, _b$2) {
|
|
|
9175
9176
|
var initialGlobalFetch = globalThis.fetch;
|
|
9176
9177
|
isNodeDefaultFetch(initialGlobalFetch);
|
|
9177
9178
|
function isNodeDefaultFetch(fetch) {
|
|
9179
|
+
if (typeof fetch !== "function") return false;
|
|
9178
9180
|
const source = Function.prototype.toString.call(fetch);
|
|
9179
9181
|
return source.includes("internal/deps/undici") || source.includes("lazy loading of undici");
|
|
9180
9182
|
}
|
|
@@ -10753,7 +10755,7 @@ function withoutTrailingSlash(url) {
|
|
|
10753
10755
|
return url == null ? void 0 : url.replace(/\/$/, "");
|
|
10754
10756
|
}
|
|
10755
10757
|
//#endregion
|
|
10756
|
-
//#region ../_vendored/ai_v6/dist/dist-
|
|
10758
|
+
//#region ../_vendored/ai_v6/dist/dist-BoeKQsDe.js
|
|
10757
10759
|
function getContext() {
|
|
10758
10760
|
return { headers: {} };
|
|
10759
10761
|
}
|
|
@@ -18905,6 +18907,12 @@ function extractListItemsOnly(content) {
|
|
|
18905
18907
|
*/
|
|
18906
18908
|
const MAX_OBSERVATION_LINE_CHARS = 1e4;
|
|
18907
18909
|
/**
|
|
18910
|
+
* Minimum trimmed line length considered by the duplicate-line degenerate
|
|
18911
|
+
* check. Short lines (blank lines, separators, terse bullets) legitimately
|
|
18912
|
+
* repeat; long identical lines almost never do.
|
|
18913
|
+
*/
|
|
18914
|
+
const MIN_DUPLICATE_LINE_CHARS = 24;
|
|
18915
|
+
/**
|
|
18908
18916
|
* Truncate individual observation lines that exceed the maximum length.
|
|
18909
18917
|
*/
|
|
18910
18918
|
function sanitizeObservationLines(observations) {
|
|
@@ -18942,6 +18950,18 @@ function detectDegenerateRepetition(text) {
|
|
|
18942
18950
|
if (totalWindows > 5 && duplicateWindows / totalWindows > .4) return true;
|
|
18943
18951
|
const lines = text.split("\n");
|
|
18944
18952
|
for (const line of lines) if (line.length > 5e4) return true;
|
|
18953
|
+
const seenLines = /* @__PURE__ */ new Map();
|
|
18954
|
+
let duplicateLines = 0;
|
|
18955
|
+
let totalCountedLines = 0;
|
|
18956
|
+
for (const line of lines) {
|
|
18957
|
+
const trimmed = line.trim();
|
|
18958
|
+
if (trimmed.length < MIN_DUPLICATE_LINE_CHARS) continue;
|
|
18959
|
+
totalCountedLines++;
|
|
18960
|
+
const count = (seenLines.get(trimmed) ?? 0) + 1;
|
|
18961
|
+
seenLines.set(trimmed, count);
|
|
18962
|
+
if (count > 1) duplicateLines++;
|
|
18963
|
+
}
|
|
18964
|
+
if (totalCountedLines >= 20 && duplicateLines / totalCountedLines > .5) return true;
|
|
18945
18965
|
return false;
|
|
18946
18966
|
}
|
|
18947
18967
|
/**
|
|
@@ -18976,12 +18996,26 @@ function describeDegenerateOutput(text, snippetChars = 400) {
|
|
|
18976
18996
|
topWindow = window;
|
|
18977
18997
|
}
|
|
18978
18998
|
let longestLine = 0;
|
|
18979
|
-
|
|
18999
|
+
const seenLines = /* @__PURE__ */ new Map();
|
|
19000
|
+
let duplicateLines = 0;
|
|
19001
|
+
let totalCountedLines = 0;
|
|
19002
|
+
for (const line of text.split("\n")) {
|
|
19003
|
+
if (line.length > longestLine) longestLine = line.length;
|
|
19004
|
+
const trimmed = line.trim();
|
|
19005
|
+
if (trimmed.length < MIN_DUPLICATE_LINE_CHARS) continue;
|
|
19006
|
+
totalCountedLines++;
|
|
19007
|
+
const count = (seenLines.get(trimmed) ?? 0) + 1;
|
|
19008
|
+
seenLines.set(trimmed, count);
|
|
19009
|
+
if (count > 1) duplicateLines++;
|
|
19010
|
+
}
|
|
18980
19011
|
const duplicateRatio = totalWindows > 0 ? (duplicateWindows / totalWindows).toFixed(2) : "n/a";
|
|
19012
|
+
const duplicateLineRatio = totalCountedLines > 0 ? (duplicateLines / totalCountedLines).toFixed(2) : "n/a";
|
|
18981
19013
|
const parts = [
|
|
18982
19014
|
`length=${text.length}`,
|
|
18983
19015
|
`sampledWindows=${totalWindows}`,
|
|
18984
19016
|
`duplicateRatio=${duplicateRatio}`,
|
|
19017
|
+
`duplicateLineRatio=${duplicateLineRatio}`,
|
|
19018
|
+
`countedLines=${totalCountedLines}`,
|
|
18985
19019
|
`longestLine=${longestLine}`,
|
|
18986
19020
|
`topWindowCount=${topCount}`
|
|
18987
19021
|
];
|
|
@@ -19255,7 +19289,19 @@ async function withOmTracingSpan({ phase, model, inputTokens, requestContext, ob
|
|
|
19255
19289
|
});
|
|
19256
19290
|
const childObservabilityContext = createObservabilityContext({ currentSpan: span });
|
|
19257
19291
|
if (!span) return callback(childObservabilityContext);
|
|
19258
|
-
return span.executeInContext(() =>
|
|
19292
|
+
return span.executeInContext(async () => {
|
|
19293
|
+
try {
|
|
19294
|
+
const result = await callback(childObservabilityContext);
|
|
19295
|
+
span.end();
|
|
19296
|
+
return result;
|
|
19297
|
+
} catch (error) {
|
|
19298
|
+
span.error({
|
|
19299
|
+
error,
|
|
19300
|
+
endSpan: true
|
|
19301
|
+
});
|
|
19302
|
+
throw error;
|
|
19303
|
+
}
|
|
19304
|
+
});
|
|
19259
19305
|
}
|
|
19260
19306
|
//#endregion
|
|
19261
19307
|
//#region src/processors/observational-memory/observer-runner.ts
|
|
@@ -24392,10 +24438,11 @@ ObservationStrategy.create = ((om, opts) => {
|
|
|
24392
24438
|
});
|
|
24393
24439
|
//#endregion
|
|
24394
24440
|
//#region src/processors/observational-memory/observation-turn/load-memory-context.ts
|
|
24395
|
-
async function loadMemoryContextMessages({ memory, messageList, threadId, resourceId }) {
|
|
24441
|
+
async function loadMemoryContextMessages({ memory, messageList, threadId, resourceId, runState }) {
|
|
24396
24442
|
const ctx = await memory.getContext({
|
|
24397
24443
|
threadId,
|
|
24398
|
-
resourceId
|
|
24444
|
+
resourceId,
|
|
24445
|
+
runState
|
|
24399
24446
|
});
|
|
24400
24447
|
for (const msg of ctx.messages) if (msg.role !== "system") messageList.add(msg, "memory");
|
|
24401
24448
|
return ctx;
|
|
@@ -24853,10 +24900,11 @@ var ObservationTurn = class {
|
|
|
24853
24900
|
* If a MemoryContextProvider is passed, loads historical messages and adds
|
|
24854
24901
|
* them to the MessageList. Without a provider, only fetches/caches the record.
|
|
24855
24902
|
*/
|
|
24856
|
-
async start(memory) {
|
|
24903
|
+
async start(memory, runState) {
|
|
24857
24904
|
if (this._started) throw new Error("Turn already started");
|
|
24858
24905
|
this._started = true;
|
|
24859
24906
|
this._record = await this.om.getOrCreateRecord(this.threadId, this.resourceId);
|
|
24907
|
+
runState?.set(`observational-memory:record:${this.threadId}:${this.resourceId ?? ""}`, this._record);
|
|
24860
24908
|
this._generationCountAtStart = this._record.generationCount;
|
|
24861
24909
|
this.memory = memory;
|
|
24862
24910
|
if (memory) {
|
|
@@ -24864,7 +24912,8 @@ var ObservationTurn = class {
|
|
|
24864
24912
|
memory,
|
|
24865
24913
|
messageList: this.messageList,
|
|
24866
24914
|
threadId: this.threadId,
|
|
24867
|
-
resourceId: this.resourceId
|
|
24915
|
+
resourceId: this.resourceId,
|
|
24916
|
+
runState
|
|
24868
24917
|
});
|
|
24869
24918
|
this._context = {
|
|
24870
24919
|
messages: ctx.messages,
|
|
@@ -25344,6 +25393,21 @@ var ReflectorRunner = class {
|
|
|
25344
25393
|
memory;
|
|
25345
25394
|
onReflectionCommitted;
|
|
25346
25395
|
mastra;
|
|
25396
|
+
/**
|
|
25397
|
+
* Per-lock-key observation-token count of the last unproductive synchronous
|
|
25398
|
+
* reflection (failed, or committed while still over the reflection
|
|
25399
|
+
* threshold). `call()` already runs the full bounded compression ladder for
|
|
25400
|
+
* a given input, so re-running it against unchanged observations cannot
|
|
25401
|
+
* succeed — threshold-triggered sync reflection is skipped while the count
|
|
25402
|
+
* is unchanged and retried as soon as the input differs.
|
|
25403
|
+
* Keyed by lock key (thread/resource), which is stable across generations —
|
|
25404
|
+
* record ids change every time a generation is created.
|
|
25405
|
+
*
|
|
25406
|
+
* Lifecycle: an entry is deleted when a sync reflection finishes under
|
|
25407
|
+
* threshold and replaced on each unproductive attempt. Entries are one
|
|
25408
|
+
* number per active thread/resource key and live for the runner's lifetime.
|
|
25409
|
+
*/
|
|
25410
|
+
syncReflectionSuppression = /* @__PURE__ */ new Map();
|
|
25347
25411
|
constructor(opts) {
|
|
25348
25412
|
this.reflectionConfig = opts.reflectionConfig;
|
|
25349
25413
|
this.observationConfig = opts.observationConfig;
|
|
@@ -25426,8 +25490,8 @@ var ReflectorRunner = class {
|
|
|
25426
25490
|
};
|
|
25427
25491
|
let reflectedTokens = 0;
|
|
25428
25492
|
let attemptNumber = 0;
|
|
25429
|
-
/**
|
|
25430
|
-
let
|
|
25493
|
+
/** True when the latest attempt returned an empty block for non-empty input. */
|
|
25494
|
+
let emptyOutput = false;
|
|
25431
25495
|
while (currentLevel <= maxLevel) {
|
|
25432
25496
|
attemptNumber++;
|
|
25433
25497
|
const isRetry = attemptNumber > 1;
|
|
@@ -25491,21 +25555,20 @@ var ReflectorRunner = class {
|
|
|
25491
25555
|
}
|
|
25492
25556
|
finalProviderMetadata = result.providerMetadata ?? finalProviderMetadata;
|
|
25493
25557
|
parsed = parseReflectorOutput(result.text, observations, activeExtractors);
|
|
25558
|
+
emptyOutput = !parsed.degenerate && observations.trim().length > 0 && parsed.observations.trim().length === 0;
|
|
25494
25559
|
if (parsed.degenerate) {
|
|
25495
25560
|
omDebug(`[OM:callReflector] attempt #${attemptNumber}: degenerate repetition detected, treating as compression failure. ${describeDegenerateOutput(result.text, 2e3)}`);
|
|
25496
25561
|
reflectedTokens = originalTokens;
|
|
25562
|
+
} else if (emptyOutput) {
|
|
25563
|
+
omDebug(`[OM:callReflector] attempt #${attemptNumber}: empty observations block for non-empty input, treating as compression failure`);
|
|
25564
|
+
reflectedTokens = originalTokens;
|
|
25497
25565
|
} else reflectedTokens = this.tokenCounter.countObservations(parsed.observations);
|
|
25498
25566
|
omDebug(`[OM:callReflector] attempt #${attemptNumber} parsed: reflectedTokens=${reflectedTokens}, targetThreshold=${targetThreshold}, compressionValid=${validateCompression(reflectedTokens, targetThreshold)}, parsedObsLen=${parsed.observations?.length}, degenerate=${parsed.degenerate ?? false}`);
|
|
25499
|
-
if (!parsed.degenerate && (validateCompression(reflectedTokens, targetThreshold) || currentLevel >= maxLevel)) break;
|
|
25500
|
-
if (parsed.degenerate && currentLevel >= maxLevel) {
|
|
25501
|
-
omDebug(`[OM:callReflector] degenerate output persists at maxLevel=${maxLevel}, breaking`);
|
|
25502
|
-
break;
|
|
25503
|
-
}
|
|
25504
|
-
if (!parsed.degenerate && previousObservations !== void 0 && parsed.observations === previousObservations) {
|
|
25505
|
-
omDebug(`[OM:callReflector] attempt #${attemptNumber} returned output identical to the previous attempt; escalating cannot help, stopping the ladder`);
|
|
25567
|
+
if (!parsed.degenerate && !emptyOutput && (validateCompression(reflectedTokens, targetThreshold) || currentLevel >= maxLevel)) break;
|
|
25568
|
+
if ((parsed.degenerate || emptyOutput) && currentLevel >= maxLevel) {
|
|
25569
|
+
omDebug(`[OM:callReflector] ${parsed.degenerate ? "degenerate" : "empty"} output persists at maxLevel=${maxLevel}, breaking`);
|
|
25506
25570
|
break;
|
|
25507
25571
|
}
|
|
25508
|
-
previousObservations = parsed.observations;
|
|
25509
25572
|
if (streamContext?.writer) {
|
|
25510
25573
|
const failedMarker = createObservationFailedMarker({
|
|
25511
25574
|
cycleId: streamContext.cycleId,
|
|
@@ -25541,6 +25604,7 @@ var ReflectorRunner = class {
|
|
|
25541
25604
|
}
|
|
25542
25605
|
currentLevel = Math.min(currentLevel + 1, maxLevel);
|
|
25543
25606
|
}
|
|
25607
|
+
if (observations.trim().length > 0 && parsed.observations.trim().length === 0) throw new Error(`Reflector produced empty output after ${attemptNumber} attempt(s)${parsed.degenerate ? " (degenerate repetition)" : ""} — refusing to commit an empty reflection over ${originalTokens} observation tokens`);
|
|
25544
25608
|
const structuredExtraction = await extractStructuredValues({
|
|
25545
25609
|
agent,
|
|
25546
25610
|
source: "reflector",
|
|
@@ -25589,52 +25653,51 @@ var ReflectorRunner = class {
|
|
|
25589
25653
|
this.storage.setBufferingReflectionFlag(record.id, true).catch((err) => {
|
|
25590
25654
|
omError("[OM] Failed to set buffering reflection flag", err);
|
|
25591
25655
|
});
|
|
25592
|
-
|
|
25593
|
-
|
|
25656
|
+
const asyncOp = (async () => {
|
|
25657
|
+
let outcome;
|
|
25658
|
+
let reflectionError;
|
|
25594
25659
|
try {
|
|
25595
|
-
reflectionHooks?.
|
|
25596
|
-
|
|
25597
|
-
|
|
25598
|
-
|
|
25599
|
-
|
|
25600
|
-
|
|
25601
|
-
|
|
25602
|
-
|
|
25603
|
-
|
|
25604
|
-
|
|
25605
|
-
|
|
25606
|
-
|
|
25607
|
-
|
|
25608
|
-
|
|
25609
|
-
|
|
25610
|
-
|
|
25611
|
-
|
|
25612
|
-
|
|
25613
|
-
|
|
25614
|
-
|
|
25615
|
-
|
|
25616
|
-
}
|
|
25617
|
-
|
|
25618
|
-
|
|
25619
|
-
|
|
25620
|
-
|
|
25621
|
-
|
|
25622
|
-
|
|
25623
|
-
|
|
25624
|
-
|
|
25625
|
-
|
|
25660
|
+
await reflectionHooks?.onReflectionStart?.();
|
|
25661
|
+
outcome = await this.doAsyncBufferedReflection(record, bufferKey, writer, requestContext, observabilityContext, priorExtractedValues, mainAgent, sendSignal);
|
|
25662
|
+
} catch (error) {
|
|
25663
|
+
reflectionError = error instanceof Error ? error : new Error(String(error));
|
|
25664
|
+
if (writer) try {
|
|
25665
|
+
const failedMarker = createBufferingFailedMarker({
|
|
25666
|
+
cycleId: `reflect-buf-${Date.now()}-${Math.random().toString(36).slice(2, 11)}`,
|
|
25667
|
+
operationType: "reflection",
|
|
25668
|
+
startedAt: (/* @__PURE__ */ new Date()).toISOString(),
|
|
25669
|
+
tokensAttempted: observationTokens,
|
|
25670
|
+
error: reflectionError.message,
|
|
25671
|
+
recordId: record.id,
|
|
25672
|
+
threadId: record.threadId ?? ""
|
|
25673
|
+
});
|
|
25674
|
+
writer.custom({
|
|
25675
|
+
...failedMarker,
|
|
25676
|
+
transient: true
|
|
25677
|
+
}).catch(() => {});
|
|
25678
|
+
await this.persistMarkerToStorage(failedMarker, record.threadId ?? "", record.resourceId ?? void 0);
|
|
25679
|
+
} catch (markerError) {
|
|
25680
|
+
omError("[OM] Failed to persist buffering-failed marker after async buffered reflection failure", markerError);
|
|
25681
|
+
}
|
|
25682
|
+
omError("[OM] Async buffered reflection failed", error);
|
|
25683
|
+
BufferingCoordinator.lastBufferedBoundary.delete(bufferKey);
|
|
25684
|
+
} finally {
|
|
25685
|
+
try {
|
|
25686
|
+
await reflectionHooks?.onReflectionEnd?.({
|
|
25687
|
+
usage: outcome?.usage,
|
|
25688
|
+
error: reflectionError,
|
|
25689
|
+
...outcome?.providerMetadata ? { providerMetadata: outcome.providerMetadata } : {}
|
|
25690
|
+
});
|
|
25691
|
+
} catch (hookError) {
|
|
25692
|
+
omError("[OM] onReflectionEnd hook failed after async buffered reflection", hookError);
|
|
25693
|
+
}
|
|
25694
|
+
BufferingCoordinator.asyncBufferingOps.delete(bufferKey);
|
|
25695
|
+
unregisterOp(record.id, "bufferingReflection");
|
|
25696
|
+
this.storage.setBufferingReflectionFlag(record.id, false).catch((err) => {
|
|
25697
|
+
omError("[OM] Failed to clear buffering reflection flag", err);
|
|
25626
25698
|
});
|
|
25627
|
-
} catch (hookError) {
|
|
25628
|
-
omError("[OM] onReflectionEnd hook failed after async buffered reflection failure", hookError);
|
|
25629
25699
|
}
|
|
25630
|
-
|
|
25631
|
-
}).finally(() => {
|
|
25632
|
-
BufferingCoordinator.asyncBufferingOps.delete(bufferKey);
|
|
25633
|
-
unregisterOp(record.id, "bufferingReflection");
|
|
25634
|
-
this.storage.setBufferingReflectionFlag(record.id, false).catch((err) => {
|
|
25635
|
-
omError("[OM] Failed to clear buffering reflection flag", err);
|
|
25636
|
-
});
|
|
25637
|
-
});
|
|
25700
|
+
})();
|
|
25638
25701
|
BufferingCoordinator.asyncBufferingOps.set(bufferKey, asyncOp);
|
|
25639
25702
|
}
|
|
25640
25703
|
/**
|
|
@@ -25895,6 +25958,13 @@ var ReflectorRunner = class {
|
|
|
25895
25958
|
return;
|
|
25896
25959
|
}
|
|
25897
25960
|
}
|
|
25961
|
+
if (activationTriggeredBy === "threshold") {
|
|
25962
|
+
const suppressedAtTokens = this.syncReflectionSuppression.get(lockKey);
|
|
25963
|
+
if (suppressedAtTokens !== void 0 && observationTokens === suppressedAtTokens) {
|
|
25964
|
+
omDebug(`[OM:reflect] skipping sync reflection — observations unchanged at ${observationTokens} tokens since the last unproductive attempt; retrying once they change`);
|
|
25965
|
+
return;
|
|
25966
|
+
}
|
|
25967
|
+
}
|
|
25898
25968
|
await this.storage.setReflectingFlag(record.id, true);
|
|
25899
25969
|
registerOp(record.id, "reflecting");
|
|
25900
25970
|
const cycleId = crypto.randomUUID();
|
|
@@ -25935,8 +26005,15 @@ var ReflectorRunner = class {
|
|
|
25935
26005
|
let reflectionUsage;
|
|
25936
26006
|
let reflectionProviderMetadata;
|
|
25937
26007
|
let reflectionError;
|
|
25938
|
-
|
|
26008
|
+
let lifecycleError;
|
|
25939
26009
|
try {
|
|
26010
|
+
try {
|
|
26011
|
+
await reflectionHooks?.onReflectionStart?.();
|
|
26012
|
+
} catch (error) {
|
|
26013
|
+
lifecycleError = error;
|
|
26014
|
+
reflectionError = error instanceof Error ? error : new Error(String(error));
|
|
26015
|
+
throw error;
|
|
26016
|
+
}
|
|
25940
26017
|
const compressionStartLevel = await this.getCompressionStartLevel(requestContext);
|
|
25941
26018
|
const reflectResult = await this.call(record.activeObservations, void 0, streamContext, reflectThreshold, abortSignal, void 0, compressionStartLevel, requestContext, priorExtractedValues, observabilityContext, void 0, mainAgent, sendSignal);
|
|
25942
26019
|
reflectionUsage = reflectResult.usage;
|
|
@@ -25948,6 +26025,8 @@ var ReflectorRunner = class {
|
|
|
25948
26025
|
reflection: reflectResult.observations,
|
|
25949
26026
|
tokenCount: reflectionTokenCount
|
|
25950
26027
|
});
|
|
26028
|
+
if (reflectionTokenCount >= reflectThreshold) this.syncReflectionSuppression.set(lockKey, reflectionTokenCount);
|
|
26029
|
+
else this.syncReflectionSuppression.delete(lockKey);
|
|
25951
26030
|
await this.notifyReflectionCommitted({
|
|
25952
26031
|
parentThreadId: requestedThreadId ?? record.threadId ?? "",
|
|
25953
26032
|
resourceId: record.resourceId ?? "",
|
|
@@ -26006,18 +26085,28 @@ var ReflectorRunner = class {
|
|
|
26006
26085
|
await this.persistMarkerToStorage(failedMarker, threadId, record.resourceId ?? void 0);
|
|
26007
26086
|
}
|
|
26008
26087
|
reflectionError = error instanceof Error ? error : new Error(String(error));
|
|
26009
|
-
if (abortSignal?.aborted) throw error;
|
|
26088
|
+
if (lifecycleError !== void 0 || abortSignal?.aborted) throw error;
|
|
26089
|
+
this.syncReflectionSuppression.set(lockKey, observationTokens);
|
|
26010
26090
|
omError("[OM] Reflection failed", error);
|
|
26011
26091
|
} finally {
|
|
26012
|
-
await this.storage.setReflectingFlag(record.id, false);
|
|
26013
26092
|
try {
|
|
26014
|
-
|
|
26093
|
+
await this.storage.setReflectingFlag(record.id, false);
|
|
26094
|
+
} finally {
|
|
26095
|
+
unregisterOp(record.id, "reflecting");
|
|
26096
|
+
}
|
|
26097
|
+
let endHookError;
|
|
26098
|
+
try {
|
|
26099
|
+
await reflectionHooks?.onReflectionEnd?.({
|
|
26015
26100
|
usage: reflectionUsage,
|
|
26016
26101
|
error: reflectionError,
|
|
26017
26102
|
...reflectionProviderMetadata ? { providerMetadata: reflectionProviderMetadata } : {}
|
|
26018
26103
|
});
|
|
26019
|
-
}
|
|
26020
|
-
|
|
26104
|
+
} catch (error) {
|
|
26105
|
+
endHookError = error;
|
|
26106
|
+
}
|
|
26107
|
+
if (endHookError !== void 0) {
|
|
26108
|
+
if (lifecycleError === void 0 && !abortSignal?.aborted) throw endHookError;
|
|
26109
|
+
omDebug(`[OM:hooks] onReflectionEnd hook failed after cycle failure: ${endHookError instanceof Error ? endHookError.message : String(endHookError)}`);
|
|
26021
26110
|
}
|
|
26022
26111
|
}
|
|
26023
26112
|
}
|
|
@@ -26478,6 +26567,8 @@ var ObservationalMemory = class ObservationalMemory {
|
|
|
26478
26567
|
onIndexObservations;
|
|
26479
26568
|
/** Config-level lifecycle hooks fired for every observation/reflection cycle. */
|
|
26480
26569
|
hooks;
|
|
26570
|
+
/** Execution policy for config-level hooks on manual and turn-synchronous cycles. */
|
|
26571
|
+
hookExecution;
|
|
26481
26572
|
/** Observer agent runner — handles LLM calls for extracting observations. */
|
|
26482
26573
|
observer;
|
|
26483
26574
|
/** Reflector agent runner — handles LLM calls for compressing observations. */
|
|
@@ -26584,6 +26675,7 @@ var ObservationalMemory = class ObservationalMemory {
|
|
|
26584
26675
|
this.retrievalSearch = typeof config.retrieval === "object" && Boolean(config.retrieval.vector);
|
|
26585
26676
|
this.onIndexObservations = config.onIndexObservations;
|
|
26586
26677
|
this.hooks = config.hooks;
|
|
26678
|
+
this.hookExecution = config.hookExecution ?? "non-blocking";
|
|
26587
26679
|
this.mastra = config.mastra;
|
|
26588
26680
|
this.memory = config.memory;
|
|
26589
26681
|
this.curationCadence = config.curationCadence;
|
|
@@ -28376,57 +28468,54 @@ ${formattedMessages}
|
|
|
28376
28468
|
};
|
|
28377
28469
|
}
|
|
28378
28470
|
/**
|
|
28379
|
-
* Compose
|
|
28380
|
-
*
|
|
28381
|
-
*
|
|
28382
|
-
* never fail an observation/reflection cycle; per-call hooks keep their
|
|
28383
|
-
* existing payloads and propagation semantics. Returns undefined when
|
|
28384
|
-
* neither is configured, so call sites stay zero-cost.
|
|
28385
|
-
*
|
|
28386
|
-
* Used internally by every pipeline path (manual observe/reflect,
|
|
28387
|
-
* turn-engine sync observation, async buffering); public only so the
|
|
28388
|
-
* observation turn engine can thread config-level reflection hooks into
|
|
28389
|
-
* reflector calls.
|
|
28471
|
+
* Compose config-level hooks with optional per-call hooks and bind context to
|
|
28472
|
+
* config-level callbacks. The configured policy applies unless a background
|
|
28473
|
+
* path explicitly requests non-blocking invocation.
|
|
28390
28474
|
*
|
|
28391
28475
|
* @internal
|
|
28392
28476
|
*/
|
|
28393
|
-
composeHooks(callHooks, context) {
|
|
28477
|
+
composeHooks(callHooks, context, execution = "configured") {
|
|
28394
28478
|
const configHooks = this.hooks;
|
|
28395
28479
|
if (!configHooks) return callHooks;
|
|
28396
28480
|
if (!callHooks && !Object.keys(configHooks).length) return void 0;
|
|
28397
|
-
const
|
|
28481
|
+
const shouldAwaitConfig = execution === "configured" && this.hookExecution === "await";
|
|
28482
|
+
const invokeConfigHook = async (name, arg) => {
|
|
28398
28483
|
const hook = configHooks[name];
|
|
28399
28484
|
if (!hook) return;
|
|
28485
|
+
if (shouldAwaitConfig) {
|
|
28486
|
+
await hook(arg);
|
|
28487
|
+
return;
|
|
28488
|
+
}
|
|
28400
28489
|
const logHookFailure = (error) => omDebug(`[OM:hooks] config-level ${name} hook failed: ${error instanceof Error ? error.message : String(error)}`);
|
|
28401
28490
|
try {
|
|
28402
|
-
const
|
|
28403
|
-
if (
|
|
28491
|
+
const result = hook(arg);
|
|
28492
|
+
if (result && typeof result.then === "function") result.then(void 0, logHookFailure);
|
|
28404
28493
|
} catch (error) {
|
|
28405
28494
|
logHookFailure(error);
|
|
28406
28495
|
}
|
|
28407
28496
|
};
|
|
28408
28497
|
return {
|
|
28409
|
-
onObservationStart: () => {
|
|
28410
|
-
|
|
28411
|
-
callHooks?.onObservationStart?.();
|
|
28498
|
+
onObservationStart: async () => {
|
|
28499
|
+
await invokeConfigHook("onObservationStart", context);
|
|
28500
|
+
await callHooks?.onObservationStart?.();
|
|
28412
28501
|
},
|
|
28413
|
-
onObservationEnd: (result) => {
|
|
28414
|
-
|
|
28502
|
+
onObservationEnd: async (result) => {
|
|
28503
|
+
await invokeConfigHook("onObservationEnd", {
|
|
28415
28504
|
...context,
|
|
28416
28505
|
...result
|
|
28417
28506
|
});
|
|
28418
|
-
callHooks?.onObservationEnd?.(result);
|
|
28507
|
+
await callHooks?.onObservationEnd?.(result);
|
|
28419
28508
|
},
|
|
28420
|
-
onReflectionStart: () => {
|
|
28421
|
-
|
|
28422
|
-
callHooks?.onReflectionStart?.();
|
|
28509
|
+
onReflectionStart: async () => {
|
|
28510
|
+
await invokeConfigHook("onReflectionStart", context);
|
|
28511
|
+
await callHooks?.onReflectionStart?.();
|
|
28423
28512
|
},
|
|
28424
|
-
onReflectionEnd: (result) => {
|
|
28425
|
-
|
|
28513
|
+
onReflectionEnd: async (result) => {
|
|
28514
|
+
await invokeConfigHook("onReflectionEnd", {
|
|
28426
28515
|
...context,
|
|
28427
28516
|
...result
|
|
28428
28517
|
});
|
|
28429
|
-
callHooks?.onReflectionEnd?.(result);
|
|
28518
|
+
await callHooks?.onReflectionEnd?.(result);
|
|
28430
28519
|
}
|
|
28431
28520
|
};
|
|
28432
28521
|
}
|
|
@@ -28439,22 +28528,29 @@ ${formattedMessages}
|
|
|
28439
28528
|
* end hook reports `error` for failed cycles even though nothing throws.
|
|
28440
28529
|
*/
|
|
28441
28530
|
async runBufferedObservationCycle(context, run) {
|
|
28442
|
-
const hooks = this.composeHooks(void 0, context);
|
|
28443
|
-
hooks?.onObservationStart?.();
|
|
28531
|
+
const hooks = this.composeHooks(void 0, context, "non-blocking");
|
|
28444
28532
|
let runResult;
|
|
28445
28533
|
let runError;
|
|
28534
|
+
let lifecycleError;
|
|
28446
28535
|
try {
|
|
28536
|
+
await hooks?.onObservationStart?.();
|
|
28447
28537
|
runResult = await run();
|
|
28448
28538
|
return runResult;
|
|
28449
28539
|
} catch (error) {
|
|
28540
|
+
lifecycleError = error;
|
|
28450
28541
|
runError = error instanceof Error ? error : new Error(String(error));
|
|
28451
28542
|
throw error;
|
|
28452
28543
|
} finally {
|
|
28453
|
-
|
|
28454
|
-
|
|
28455
|
-
|
|
28456
|
-
|
|
28457
|
-
|
|
28544
|
+
try {
|
|
28545
|
+
await hooks?.onObservationEnd?.({
|
|
28546
|
+
usage: runResult?.usage,
|
|
28547
|
+
error: runError ?? runResult?.error,
|
|
28548
|
+
...runResult?.providerMetadata ? { providerMetadata: runResult.providerMetadata } : {}
|
|
28549
|
+
});
|
|
28550
|
+
} catch (endHookError) {
|
|
28551
|
+
if (lifecycleError === void 0) throw endHookError;
|
|
28552
|
+
omDebug(`[OM:hooks] async-buffer onObservationEnd hook failed after cycle failure: ${endHookError instanceof Error ? endHookError.message : String(endHookError)}`);
|
|
28553
|
+
}
|
|
28458
28554
|
}
|
|
28459
28555
|
}
|
|
28460
28556
|
/**
|
|
@@ -28482,18 +28578,21 @@ ${formattedMessages}
|
|
|
28482
28578
|
let observed = false;
|
|
28483
28579
|
let observationUsage;
|
|
28484
28580
|
let observationProviderMetadata;
|
|
28581
|
+
let observationError;
|
|
28582
|
+
let lifecycleError;
|
|
28583
|
+
let observationStarted = false;
|
|
28485
28584
|
let generationBefore = -1;
|
|
28486
|
-
|
|
28487
|
-
|
|
28488
|
-
|
|
28489
|
-
|
|
28490
|
-
|
|
28491
|
-
|
|
28492
|
-
|
|
28493
|
-
|
|
28494
|
-
|
|
28495
|
-
|
|
28496
|
-
|
|
28585
|
+
try {
|
|
28586
|
+
await this.withLock(lockKey, async () => {
|
|
28587
|
+
const freshRecord = await this.getOrCreateRecord(threadId, resourceId);
|
|
28588
|
+
generationBefore = freshRecord.generationCount;
|
|
28589
|
+
const unobservedMessages = messages ? this.getUnobservedMessages(messages, freshRecord) : await this.loadMessagesFromStorage(threadId, resourceId, freshRecord.lastObservedAt ? new Date(freshRecord.lastObservedAt) : void 0);
|
|
28590
|
+
if (!this.meetsObservationThreshold({
|
|
28591
|
+
record: freshRecord,
|
|
28592
|
+
unobservedTokens: await this.tokenCounter.countMessagesAsync(unobservedMessages)
|
|
28593
|
+
})) return;
|
|
28594
|
+
observationStarted = true;
|
|
28595
|
+
await hooks?.onObservationStart?.();
|
|
28497
28596
|
const result = await ObservationStrategy.create(this, {
|
|
28498
28597
|
record: freshRecord,
|
|
28499
28598
|
threadId,
|
|
@@ -28511,17 +28610,22 @@ ${formattedMessages}
|
|
|
28511
28610
|
observed = result.observed;
|
|
28512
28611
|
observationUsage = result.usage;
|
|
28513
28612
|
observationProviderMetadata = result.providerMetadata;
|
|
28514
|
-
}
|
|
28515
|
-
|
|
28516
|
-
|
|
28517
|
-
|
|
28518
|
-
|
|
28519
|
-
|
|
28520
|
-
|
|
28521
|
-
|
|
28522
|
-
|
|
28523
|
-
|
|
28524
|
-
|
|
28613
|
+
});
|
|
28614
|
+
} catch (error) {
|
|
28615
|
+
lifecycleError = error;
|
|
28616
|
+
observationError = error instanceof Error ? error : new Error(String(error));
|
|
28617
|
+
}
|
|
28618
|
+
if (observationStarted) try {
|
|
28619
|
+
await hooks?.onObservationEnd?.({
|
|
28620
|
+
usage: observationUsage,
|
|
28621
|
+
error: observationError,
|
|
28622
|
+
...observationProviderMetadata ? { providerMetadata: observationProviderMetadata } : {}
|
|
28623
|
+
});
|
|
28624
|
+
} catch (endHookError) {
|
|
28625
|
+
if (lifecycleError === void 0) throw endHookError;
|
|
28626
|
+
omDebug(`[OM:hooks] onObservationEnd hook failed after cycle failure: ${endHookError instanceof Error ? endHookError.message : String(endHookError)}`);
|
|
28627
|
+
}
|
|
28628
|
+
if (lifecycleError !== void 0) throw lifecycleError;
|
|
28525
28629
|
const record = await this.getOrCreateRecord(threadId, resourceId);
|
|
28526
28630
|
const reflected = record.generationCount > generationBefore && generationBefore >= 0;
|
|
28527
28631
|
if (observed) this.maybeTriggerCadenceCuration(threadId, resourceId, record, requestContext).catch((error) => {
|
|
@@ -28595,11 +28699,18 @@ ${formattedMessages}
|
|
|
28595
28699
|
resourceId,
|
|
28596
28700
|
trigger: "manual"
|
|
28597
28701
|
});
|
|
28598
|
-
hooks?.onReflectionStart?.();
|
|
28599
28702
|
let reflectionUsage;
|
|
28600
28703
|
let reflectionProviderMetadata;
|
|
28601
28704
|
let reflectionError;
|
|
28705
|
+
let lifecycleError;
|
|
28602
28706
|
try {
|
|
28707
|
+
try {
|
|
28708
|
+
await hooks?.onReflectionStart?.();
|
|
28709
|
+
} catch (error) {
|
|
28710
|
+
lifecycleError = error;
|
|
28711
|
+
reflectionError = error instanceof Error ? error : new Error(String(error));
|
|
28712
|
+
throw error;
|
|
28713
|
+
}
|
|
28603
28714
|
const thread = await this.storage.getThreadById({ threadId });
|
|
28604
28715
|
const previousOmMetadata = getThreadOMMetadata(thread?.metadata);
|
|
28605
28716
|
const priorExtractedValues = getPriorExtractedValues(previousOmMetadata, this.reflectionConfig.extractors);
|
|
@@ -28637,6 +28748,7 @@ ${formattedMessages}
|
|
|
28637
28748
|
};
|
|
28638
28749
|
} catch (error) {
|
|
28639
28750
|
reflectionError = error instanceof Error ? error : new Error(String(error));
|
|
28751
|
+
if (lifecycleError !== void 0) throw error;
|
|
28640
28752
|
omError("[OM] reflect() failed", error);
|
|
28641
28753
|
return {
|
|
28642
28754
|
reflected: false,
|
|
@@ -28644,13 +28756,25 @@ ${formattedMessages}
|
|
|
28644
28756
|
usage: void 0
|
|
28645
28757
|
};
|
|
28646
28758
|
} finally {
|
|
28647
|
-
|
|
28648
|
-
|
|
28649
|
-
|
|
28650
|
-
|
|
28651
|
-
}
|
|
28652
|
-
|
|
28653
|
-
|
|
28759
|
+
try {
|
|
28760
|
+
await this.storage.setReflectingFlag(record.id, false);
|
|
28761
|
+
} finally {
|
|
28762
|
+
unregisterOp(record.id, "reflecting");
|
|
28763
|
+
}
|
|
28764
|
+
let endHookError;
|
|
28765
|
+
try {
|
|
28766
|
+
await hooks?.onReflectionEnd?.({
|
|
28767
|
+
usage: reflectionUsage,
|
|
28768
|
+
error: reflectionError,
|
|
28769
|
+
...reflectionProviderMetadata ? { providerMetadata: reflectionProviderMetadata } : {}
|
|
28770
|
+
});
|
|
28771
|
+
} catch (error) {
|
|
28772
|
+
endHookError = error;
|
|
28773
|
+
}
|
|
28774
|
+
if (endHookError !== void 0) {
|
|
28775
|
+
if (lifecycleError === void 0) throw endHookError;
|
|
28776
|
+
omDebug(`[OM:hooks] onReflectionEnd hook failed after cycle failure: ${endHookError instanceof Error ? endHookError.message : String(endHookError)}`);
|
|
28777
|
+
}
|
|
28654
28778
|
}
|
|
28655
28779
|
}
|
|
28656
28780
|
/**
|
|
@@ -28946,7 +29070,9 @@ var ObservationalMemoryProcessor = class {
|
|
|
28946
29070
|
return messageList;
|
|
28947
29071
|
}
|
|
28948
29072
|
const { threadId, resourceId } = context;
|
|
28949
|
-
const
|
|
29073
|
+
const memoryContext = parseMemoryRequestContext(requestContext);
|
|
29074
|
+
const runState = memoryContext?.runState?.();
|
|
29075
|
+
const readOnly = memoryContext?.memoryConfig?.readOnly;
|
|
28950
29076
|
const actorModelContext = model?.modelId ? {
|
|
28951
29077
|
provider: model.provider,
|
|
28952
29078
|
modelId: model.modelId,
|
|
@@ -28963,7 +29089,8 @@ var ObservationalMemoryProcessor = class {
|
|
|
28963
29089
|
memory: this.memory,
|
|
28964
29090
|
messageList,
|
|
28965
29091
|
threadId,
|
|
28966
|
-
resourceId
|
|
29092
|
+
resourceId,
|
|
29093
|
+
runState
|
|
28967
29094
|
});
|
|
28968
29095
|
const systemMessages = ctx.omRecord ? await this.engine.buildContextSystemMessages({
|
|
28969
29096
|
threadId,
|
|
@@ -29004,7 +29131,7 @@ var ObservationalMemoryProcessor = class {
|
|
|
29004
29131
|
this.turn.sendStateSignal = args.sendStateSignal;
|
|
29005
29132
|
this.turn.agent = args.agent;
|
|
29006
29133
|
this.turn.requestContext = requestContext;
|
|
29007
|
-
await this.turn.start(this.memory);
|
|
29134
|
+
await this.turn.start(this.memory, runState);
|
|
29008
29135
|
if (stepNumber === 0 && this.temporalMarkers) await insertTemporalGapMarkers({
|
|
29009
29136
|
messageList,
|
|
29010
29137
|
sendSignal: args.sendSignal
|
|
@@ -30174,14 +30301,18 @@ ${workingMemory}`;
|
|
|
30174
30301
|
if (!this.threadConfig.workingMemory?.enabled) return null;
|
|
30175
30302
|
return extractWorkingMemoryContent(text)?.trim() ?? null;
|
|
30176
30303
|
}
|
|
30177
|
-
async getWorkingMemory({ threadId, resourceId, memoryConfig }) {
|
|
30304
|
+
async getWorkingMemory({ threadId, resourceId, memoryConfig, runState }) {
|
|
30178
30305
|
const config = this.getMergedThreadConfig(memoryConfig || {});
|
|
30179
30306
|
if (!config.workingMemory?.enabled) return null;
|
|
30180
30307
|
const scope = config.workingMemory.scope || "resource";
|
|
30181
30308
|
let workingMemoryData = null;
|
|
30182
30309
|
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'.");
|
|
30183
|
-
if (scope === "resource" && resourceId)
|
|
30184
|
-
|
|
30310
|
+
if (scope === "resource" && resourceId) {
|
|
30311
|
+
const loadWorkingMemory = async () => {
|
|
30312
|
+
return (await (await this.getMemoryStore()).getResourceById({ resourceId }))?.workingMemory || null;
|
|
30313
|
+
};
|
|
30314
|
+
workingMemoryData = runState ? await runState.load(`working-memory:resource:${resourceId}`, loadWorkingMemory) : await loadWorkingMemory();
|
|
30315
|
+
} else workingMemoryData = (runState?.threadLoaded ? runState.thread : await this.getThreadById({ threadId }))?.metadata?.workingMemory;
|
|
30185
30316
|
if (!workingMemoryData) return null;
|
|
30186
30317
|
return workingMemoryData;
|
|
30187
30318
|
}
|
|
@@ -30213,16 +30344,18 @@ ${workingMemory}`;
|
|
|
30213
30344
|
content: (config.workingMemory.template || this.defaultWorkingMemoryTemplate).trim()
|
|
30214
30345
|
};
|
|
30215
30346
|
}
|
|
30216
|
-
async getSystemMessage({ threadId, resourceId, memoryConfig }) {
|
|
30347
|
+
async getSystemMessage({ threadId, resourceId, memoryConfig, runState }) {
|
|
30217
30348
|
const config = this.getMergedThreadConfig(memoryConfig);
|
|
30218
30349
|
this.assertWorkingMemoryStateSignalsCompatibility(config);
|
|
30219
30350
|
if (!config.workingMemory?.enabled) return null;
|
|
30220
30351
|
if (config.workingMemory?.useStateSignals) return null;
|
|
30221
|
-
const
|
|
30352
|
+
const loadTemplate = () => this.getWorkingMemoryTemplate({ memoryConfig });
|
|
30353
|
+
const workingMemoryTemplate = runState ? await runState.load("working-memory:template", loadTemplate) : await loadTemplate();
|
|
30222
30354
|
const workingMemoryData = await this.getWorkingMemory({
|
|
30223
30355
|
threadId,
|
|
30224
30356
|
resourceId,
|
|
30225
|
-
memoryConfig: config
|
|
30357
|
+
memoryConfig: config,
|
|
30358
|
+
runState
|
|
30226
30359
|
});
|
|
30227
30360
|
if (!workingMemoryTemplate) return null;
|
|
30228
30361
|
const workingMemoryConfig = config.workingMemory;
|
|
@@ -30255,7 +30388,7 @@ ${workingMemory}`;
|
|
|
30255
30388
|
* ```
|
|
30256
30389
|
*/
|
|
30257
30390
|
async getContext(opts) {
|
|
30258
|
-
const { threadId, resourceId, memoryConfig } = opts;
|
|
30391
|
+
const { threadId, resourceId, memoryConfig, runState } = opts;
|
|
30259
30392
|
const config = this.getMergedThreadConfig(memoryConfig);
|
|
30260
30393
|
const memoryStore = await this.getMemoryStore();
|
|
30261
30394
|
const systemParts = [];
|
|
@@ -30265,10 +30398,14 @@ ${workingMemory}`;
|
|
|
30265
30398
|
let otherThreadsContext;
|
|
30266
30399
|
const omEngine = await this.omEngine;
|
|
30267
30400
|
if (omEngine) {
|
|
30268
|
-
|
|
30401
|
+
const loadOmRecord = () => omEngine.getRecord(threadId, resourceId);
|
|
30402
|
+
omRecord = runState ? await runState.load(`observational-memory:record:${threadId}:${resourceId ?? ""}`, loadOmRecord) : await loadOmRecord();
|
|
30269
30403
|
if (omRecord?.activeObservations) {
|
|
30270
30404
|
hasObservations = true;
|
|
30271
|
-
if (omEngine.scope === "resource" && resourceId)
|
|
30405
|
+
if (omEngine.scope === "resource" && resourceId) {
|
|
30406
|
+
const loadOtherThreadsContext = () => omEngine.getOtherThreadsContext(resourceId, threadId);
|
|
30407
|
+
otherThreadsContext = runState ? await runState.load(`observational-memory:other-threads:${resourceId}:${threadId}:${omRecord.lastObservedAt ?? ""}`, loadOtherThreadsContext) : await loadOtherThreadsContext();
|
|
30408
|
+
}
|
|
30272
30409
|
const obsSystemMessage = await omEngine.buildContextSystemMessage({
|
|
30273
30410
|
threadId,
|
|
30274
30411
|
resourceId,
|
|
@@ -30296,30 +30433,41 @@ ${workingMemory}`;
|
|
|
30296
30433
|
const workingMemoryMessage = await this.getSystemMessage({
|
|
30297
30434
|
threadId,
|
|
30298
30435
|
resourceId,
|
|
30299
|
-
memoryConfig: config
|
|
30436
|
+
memoryConfig: config,
|
|
30437
|
+
runState
|
|
30300
30438
|
});
|
|
30301
30439
|
if (workingMemoryMessage) systemParts.push(workingMemoryMessage);
|
|
30302
30440
|
let messages;
|
|
30303
30441
|
if (omEngine && omRecord) {
|
|
30304
30442
|
const dateFilter = omRecord.lastObservedAt ? { dateRange: { start: new Date(new Date(omRecord.lastObservedAt).getTime() + 1) } } : void 0;
|
|
30305
|
-
|
|
30306
|
-
|
|
30307
|
-
|
|
30308
|
-
|
|
30309
|
-
|
|
30310
|
-
|
|
30311
|
-
|
|
30312
|
-
|
|
30313
|
-
|
|
30314
|
-
|
|
30315
|
-
|
|
30316
|
-
|
|
30317
|
-
|
|
30318
|
-
|
|
30319
|
-
|
|
30320
|
-
|
|
30321
|
-
|
|
30322
|
-
|
|
30443
|
+
const boundary = omRecord.lastObservedAt ? new Date(omRecord.lastObservedAt).toISOString() : "";
|
|
30444
|
+
if (omEngine.scope === "resource" && resourceId) {
|
|
30445
|
+
const loadMessages = async () => {
|
|
30446
|
+
return (await memoryStore.listMessagesByResourceId({
|
|
30447
|
+
resourceId,
|
|
30448
|
+
orderBy: {
|
|
30449
|
+
field: "createdAt",
|
|
30450
|
+
direction: "ASC"
|
|
30451
|
+
},
|
|
30452
|
+
perPage: false,
|
|
30453
|
+
filter: dateFilter
|
|
30454
|
+
})).messages;
|
|
30455
|
+
};
|
|
30456
|
+
messages = runState ? await runState.load(`observational-memory:messages:resource:${resourceId}:${boundary}`, loadMessages) : await loadMessages();
|
|
30457
|
+
} else {
|
|
30458
|
+
const loadMessages = async () => {
|
|
30459
|
+
return (await memoryStore.listMessages({
|
|
30460
|
+
threadId,
|
|
30461
|
+
orderBy: {
|
|
30462
|
+
field: "createdAt",
|
|
30463
|
+
direction: "ASC"
|
|
30464
|
+
},
|
|
30465
|
+
perPage: false,
|
|
30466
|
+
filter: dateFilter
|
|
30467
|
+
})).messages;
|
|
30468
|
+
};
|
|
30469
|
+
messages = runState ? await runState.load(`observational-memory:messages:thread:${threadId}:${boundary}`, loadMessages) : await loadMessages();
|
|
30470
|
+
}
|
|
30323
30471
|
} else {
|
|
30324
30472
|
const lastMessages = config.lastMessages;
|
|
30325
30473
|
if (lastMessages === false) messages = [];
|
|
@@ -30407,6 +30555,7 @@ ${workingMemory}`;
|
|
|
30407
30555
|
instruction: omConfig.observation.instruction,
|
|
30408
30556
|
threadTitle: omConfig.observation.threadTitle,
|
|
30409
30557
|
observeAttachments: omConfig.observation.observeAttachments,
|
|
30558
|
+
continuationHints: omConfig.observation.continuationHints,
|
|
30410
30559
|
extract: omConfig.observation.extract
|
|
30411
30560
|
} : void 0,
|
|
30412
30561
|
reflection: omConfig.reflection ? {
|
|
@@ -30417,6 +30566,7 @@ ${workingMemory}`;
|
|
|
30417
30566
|
bufferActivation: omConfig.reflection.bufferActivation,
|
|
30418
30567
|
blockAfter: omConfig.reflection.blockAfter,
|
|
30419
30568
|
instruction: omConfig.reflection.instruction,
|
|
30569
|
+
continuationHints: omConfig.reflection.continuationHints,
|
|
30420
30570
|
extract: omConfig.reflection.extract
|
|
30421
30571
|
} : void 0
|
|
30422
30572
|
});
|
|
@@ -31331,4 +31481,4 @@ Notes:
|
|
|
31331
31481
|
//#endregion
|
|
31332
31482
|
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, SUBCONSCIOUS_ACTIVITY_STATE_ID as J, SubconsciousCaptureExtractor as K, OBSERVATIONAL_MEMORY_DEFAULTS as L, optimizeObservationsForContext as M, parseObserverOutput as N, buildObserverSystemPrompt as O, injectAnchorIds as P, Extractor as Q, OBSERVATION_CONTEXT_INSTRUCTIONS as R, WorkingMemoryExtractor as S, TokenCounter as T, StaleKnowledgeSemanticIndexError as U, Subconscious as V, SubconsciousRemindExtractor as W, publishSubconsciousActivity as X, buildSubconsciousActivitySnapshot as Y, renderSubconsciousActivity as Z, 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, hasCurrentTaskSection as j, extractCurrentTask as k, WORKING_MEMORY_STATE_PROCESSOR_ID as l, combineObservationGroupRanges as m, MessageHistory$1 as n, extractWorkingMemoryTags as o, ObservationalMemory as p, subconsciousCaptureSchema as q, SemanticRecall as r, removeWorkingMemoryTags as s, Memory as t, WorkingMemoryStateProcessor as u, renderObservationGroupsForReflection as v, summarizeConversation as w, deepMergeWorkingMemory as x, stripObservationGroups as y, OBSERVATION_CONTEXT_PROMPT as z };
|
|
31333
31483
|
|
|
31334
|
-
//# sourceMappingURL=src-
|
|
31484
|
+
//# sourceMappingURL=src-BTHCV1ov.js.map
|