@mastra/memory 1.27.0 → 1.28.0-alpha.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +50 -0
- package/dist/docs/SKILL.md +4 -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 +1 -0
- package/dist/docs/references/integrations-databases-mongodb.md +1 -1
- package/dist/docs/references/integrations-databases-postgresql.md +2 -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.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-CxkQ-ICG.cjs → src-CIBCjg2A.cjs} +272 -158
- package/dist/{src-CxkQ-ICG.cjs.map → src-CIBCjg2A.cjs.map} +1 -1
- package/dist/{src-aTfbkxBl.js → src-kLYHhPqh.js} +272 -158
- package/dist/{src-aTfbkxBl.js.map → src-kLYHhPqh.js.map} +1 -1
- package/package.json +7 -7
|
@@ -18905,6 +18905,12 @@ function extractListItemsOnly(content) {
|
|
|
18905
18905
|
*/
|
|
18906
18906
|
const MAX_OBSERVATION_LINE_CHARS = 1e4;
|
|
18907
18907
|
/**
|
|
18908
|
+
* Minimum trimmed line length considered by the duplicate-line degenerate
|
|
18909
|
+
* check. Short lines (blank lines, separators, terse bullets) legitimately
|
|
18910
|
+
* repeat; long identical lines almost never do.
|
|
18911
|
+
*/
|
|
18912
|
+
const MIN_DUPLICATE_LINE_CHARS = 24;
|
|
18913
|
+
/**
|
|
18908
18914
|
* Truncate individual observation lines that exceed the maximum length.
|
|
18909
18915
|
*/
|
|
18910
18916
|
function sanitizeObservationLines(observations) {
|
|
@@ -18942,6 +18948,18 @@ function detectDegenerateRepetition(text) {
|
|
|
18942
18948
|
if (totalWindows > 5 && duplicateWindows / totalWindows > .4) return true;
|
|
18943
18949
|
const lines = text.split("\n");
|
|
18944
18950
|
for (const line of lines) if (line.length > 5e4) return true;
|
|
18951
|
+
const seenLines = /* @__PURE__ */ new Map();
|
|
18952
|
+
let duplicateLines = 0;
|
|
18953
|
+
let totalCountedLines = 0;
|
|
18954
|
+
for (const line of lines) {
|
|
18955
|
+
const trimmed = line.trim();
|
|
18956
|
+
if (trimmed.length < MIN_DUPLICATE_LINE_CHARS) continue;
|
|
18957
|
+
totalCountedLines++;
|
|
18958
|
+
const count = (seenLines.get(trimmed) ?? 0) + 1;
|
|
18959
|
+
seenLines.set(trimmed, count);
|
|
18960
|
+
if (count > 1) duplicateLines++;
|
|
18961
|
+
}
|
|
18962
|
+
if (totalCountedLines >= 20 && duplicateLines / totalCountedLines > .5) return true;
|
|
18945
18963
|
return false;
|
|
18946
18964
|
}
|
|
18947
18965
|
/**
|
|
@@ -18976,12 +18994,26 @@ function describeDegenerateOutput(text, snippetChars = 400) {
|
|
|
18976
18994
|
topWindow = window;
|
|
18977
18995
|
}
|
|
18978
18996
|
let longestLine = 0;
|
|
18979
|
-
|
|
18997
|
+
const seenLines = /* @__PURE__ */ new Map();
|
|
18998
|
+
let duplicateLines = 0;
|
|
18999
|
+
let totalCountedLines = 0;
|
|
19000
|
+
for (const line of text.split("\n")) {
|
|
19001
|
+
if (line.length > longestLine) longestLine = line.length;
|
|
19002
|
+
const trimmed = line.trim();
|
|
19003
|
+
if (trimmed.length < MIN_DUPLICATE_LINE_CHARS) continue;
|
|
19004
|
+
totalCountedLines++;
|
|
19005
|
+
const count = (seenLines.get(trimmed) ?? 0) + 1;
|
|
19006
|
+
seenLines.set(trimmed, count);
|
|
19007
|
+
if (count > 1) duplicateLines++;
|
|
19008
|
+
}
|
|
18980
19009
|
const duplicateRatio = totalWindows > 0 ? (duplicateWindows / totalWindows).toFixed(2) : "n/a";
|
|
19010
|
+
const duplicateLineRatio = totalCountedLines > 0 ? (duplicateLines / totalCountedLines).toFixed(2) : "n/a";
|
|
18981
19011
|
const parts = [
|
|
18982
19012
|
`length=${text.length}`,
|
|
18983
19013
|
`sampledWindows=${totalWindows}`,
|
|
18984
19014
|
`duplicateRatio=${duplicateRatio}`,
|
|
19015
|
+
`duplicateLineRatio=${duplicateLineRatio}`,
|
|
19016
|
+
`countedLines=${totalCountedLines}`,
|
|
18985
19017
|
`longestLine=${longestLine}`,
|
|
18986
19018
|
`topWindowCount=${topCount}`
|
|
18987
19019
|
];
|
|
@@ -19255,7 +19287,19 @@ async function withOmTracingSpan({ phase, model, inputTokens, requestContext, ob
|
|
|
19255
19287
|
});
|
|
19256
19288
|
const childObservabilityContext = createObservabilityContext({ currentSpan: span });
|
|
19257
19289
|
if (!span) return callback(childObservabilityContext);
|
|
19258
|
-
return span.executeInContext(() =>
|
|
19290
|
+
return span.executeInContext(async () => {
|
|
19291
|
+
try {
|
|
19292
|
+
const result = await callback(childObservabilityContext);
|
|
19293
|
+
span.end();
|
|
19294
|
+
return result;
|
|
19295
|
+
} catch (error) {
|
|
19296
|
+
span.error({
|
|
19297
|
+
error,
|
|
19298
|
+
endSpan: true
|
|
19299
|
+
});
|
|
19300
|
+
throw error;
|
|
19301
|
+
}
|
|
19302
|
+
});
|
|
19259
19303
|
}
|
|
19260
19304
|
//#endregion
|
|
19261
19305
|
//#region src/processors/observational-memory/observer-runner.ts
|
|
@@ -24392,10 +24436,11 @@ ObservationStrategy.create = ((om, opts) => {
|
|
|
24392
24436
|
});
|
|
24393
24437
|
//#endregion
|
|
24394
24438
|
//#region src/processors/observational-memory/observation-turn/load-memory-context.ts
|
|
24395
|
-
async function loadMemoryContextMessages({ memory, messageList, threadId, resourceId }) {
|
|
24439
|
+
async function loadMemoryContextMessages({ memory, messageList, threadId, resourceId, runState }) {
|
|
24396
24440
|
const ctx = await memory.getContext({
|
|
24397
24441
|
threadId,
|
|
24398
|
-
resourceId
|
|
24442
|
+
resourceId,
|
|
24443
|
+
runState
|
|
24399
24444
|
});
|
|
24400
24445
|
for (const msg of ctx.messages) if (msg.role !== "system") messageList.add(msg, "memory");
|
|
24401
24446
|
return ctx;
|
|
@@ -24853,10 +24898,11 @@ var ObservationTurn = class {
|
|
|
24853
24898
|
* If a MemoryContextProvider is passed, loads historical messages and adds
|
|
24854
24899
|
* them to the MessageList. Without a provider, only fetches/caches the record.
|
|
24855
24900
|
*/
|
|
24856
|
-
async start(memory) {
|
|
24901
|
+
async start(memory, runState) {
|
|
24857
24902
|
if (this._started) throw new Error("Turn already started");
|
|
24858
24903
|
this._started = true;
|
|
24859
24904
|
this._record = await this.om.getOrCreateRecord(this.threadId, this.resourceId);
|
|
24905
|
+
runState?.set(`observational-memory:record:${this.threadId}:${this.resourceId ?? ""}`, this._record);
|
|
24860
24906
|
this._generationCountAtStart = this._record.generationCount;
|
|
24861
24907
|
this.memory = memory;
|
|
24862
24908
|
if (memory) {
|
|
@@ -24864,7 +24910,8 @@ var ObservationTurn = class {
|
|
|
24864
24910
|
memory,
|
|
24865
24911
|
messageList: this.messageList,
|
|
24866
24912
|
threadId: this.threadId,
|
|
24867
|
-
resourceId: this.resourceId
|
|
24913
|
+
resourceId: this.resourceId,
|
|
24914
|
+
runState
|
|
24868
24915
|
});
|
|
24869
24916
|
this._context = {
|
|
24870
24917
|
messages: ctx.messages,
|
|
@@ -25426,8 +25473,6 @@ var ReflectorRunner = class {
|
|
|
25426
25473
|
};
|
|
25427
25474
|
let reflectedTokens = 0;
|
|
25428
25475
|
let attemptNumber = 0;
|
|
25429
|
-
/** Observations from the previous attempt, used to detect a no-progress ladder. */
|
|
25430
|
-
let previousObservations;
|
|
25431
25476
|
while (currentLevel <= maxLevel) {
|
|
25432
25477
|
attemptNumber++;
|
|
25433
25478
|
const isRetry = attemptNumber > 1;
|
|
@@ -25501,11 +25546,6 @@ var ReflectorRunner = class {
|
|
|
25501
25546
|
omDebug(`[OM:callReflector] degenerate output persists at maxLevel=${maxLevel}, breaking`);
|
|
25502
25547
|
break;
|
|
25503
25548
|
}
|
|
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`);
|
|
25506
|
-
break;
|
|
25507
|
-
}
|
|
25508
|
-
previousObservations = parsed.observations;
|
|
25509
25549
|
if (streamContext?.writer) {
|
|
25510
25550
|
const failedMarker = createObservationFailedMarker({
|
|
25511
25551
|
cycleId: streamContext.cycleId,
|
|
@@ -25589,52 +25629,51 @@ var ReflectorRunner = class {
|
|
|
25589
25629
|
this.storage.setBufferingReflectionFlag(record.id, true).catch((err) => {
|
|
25590
25630
|
omError("[OM] Failed to set buffering reflection flag", err);
|
|
25591
25631
|
});
|
|
25592
|
-
|
|
25593
|
-
|
|
25632
|
+
const asyncOp = (async () => {
|
|
25633
|
+
let outcome;
|
|
25634
|
+
let reflectionError;
|
|
25594
25635
|
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
|
-
|
|
25636
|
+
await reflectionHooks?.onReflectionStart?.();
|
|
25637
|
+
outcome = await this.doAsyncBufferedReflection(record, bufferKey, writer, requestContext, observabilityContext, priorExtractedValues, mainAgent, sendSignal);
|
|
25638
|
+
} catch (error) {
|
|
25639
|
+
reflectionError = error instanceof Error ? error : new Error(String(error));
|
|
25640
|
+
if (writer) try {
|
|
25641
|
+
const failedMarker = createBufferingFailedMarker({
|
|
25642
|
+
cycleId: `reflect-buf-${Date.now()}-${Math.random().toString(36).slice(2, 11)}`,
|
|
25643
|
+
operationType: "reflection",
|
|
25644
|
+
startedAt: (/* @__PURE__ */ new Date()).toISOString(),
|
|
25645
|
+
tokensAttempted: observationTokens,
|
|
25646
|
+
error: reflectionError.message,
|
|
25647
|
+
recordId: record.id,
|
|
25648
|
+
threadId: record.threadId ?? ""
|
|
25649
|
+
});
|
|
25650
|
+
writer.custom({
|
|
25651
|
+
...failedMarker,
|
|
25652
|
+
transient: true
|
|
25653
|
+
}).catch(() => {});
|
|
25654
|
+
await this.persistMarkerToStorage(failedMarker, record.threadId ?? "", record.resourceId ?? void 0);
|
|
25655
|
+
} catch (markerError) {
|
|
25656
|
+
omError("[OM] Failed to persist buffering-failed marker after async buffered reflection failure", markerError);
|
|
25657
|
+
}
|
|
25658
|
+
omError("[OM] Async buffered reflection failed", error);
|
|
25659
|
+
BufferingCoordinator.lastBufferedBoundary.delete(bufferKey);
|
|
25660
|
+
} finally {
|
|
25661
|
+
try {
|
|
25662
|
+
await reflectionHooks?.onReflectionEnd?.({
|
|
25663
|
+
usage: outcome?.usage,
|
|
25664
|
+
error: reflectionError,
|
|
25665
|
+
...outcome?.providerMetadata ? { providerMetadata: outcome.providerMetadata } : {}
|
|
25666
|
+
});
|
|
25667
|
+
} catch (hookError) {
|
|
25668
|
+
omError("[OM] onReflectionEnd hook failed after async buffered reflection", hookError);
|
|
25669
|
+
}
|
|
25670
|
+
BufferingCoordinator.asyncBufferingOps.delete(bufferKey);
|
|
25671
|
+
unregisterOp(record.id, "bufferingReflection");
|
|
25672
|
+
this.storage.setBufferingReflectionFlag(record.id, false).catch((err) => {
|
|
25673
|
+
omError("[OM] Failed to clear buffering reflection flag", err);
|
|
25626
25674
|
});
|
|
25627
|
-
} catch (hookError) {
|
|
25628
|
-
omError("[OM] onReflectionEnd hook failed after async buffered reflection failure", hookError);
|
|
25629
25675
|
}
|
|
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
|
-
});
|
|
25676
|
+
})();
|
|
25638
25677
|
BufferingCoordinator.asyncBufferingOps.set(bufferKey, asyncOp);
|
|
25639
25678
|
}
|
|
25640
25679
|
/**
|
|
@@ -25935,8 +25974,15 @@ var ReflectorRunner = class {
|
|
|
25935
25974
|
let reflectionUsage;
|
|
25936
25975
|
let reflectionProviderMetadata;
|
|
25937
25976
|
let reflectionError;
|
|
25938
|
-
|
|
25977
|
+
let lifecycleError;
|
|
25939
25978
|
try {
|
|
25979
|
+
try {
|
|
25980
|
+
await reflectionHooks?.onReflectionStart?.();
|
|
25981
|
+
} catch (error) {
|
|
25982
|
+
lifecycleError = error;
|
|
25983
|
+
reflectionError = error instanceof Error ? error : new Error(String(error));
|
|
25984
|
+
throw error;
|
|
25985
|
+
}
|
|
25940
25986
|
const compressionStartLevel = await this.getCompressionStartLevel(requestContext);
|
|
25941
25987
|
const reflectResult = await this.call(record.activeObservations, void 0, streamContext, reflectThreshold, abortSignal, void 0, compressionStartLevel, requestContext, priorExtractedValues, observabilityContext, void 0, mainAgent, sendSignal);
|
|
25942
25988
|
reflectionUsage = reflectResult.usage;
|
|
@@ -26006,18 +26052,27 @@ var ReflectorRunner = class {
|
|
|
26006
26052
|
await this.persistMarkerToStorage(failedMarker, threadId, record.resourceId ?? void 0);
|
|
26007
26053
|
}
|
|
26008
26054
|
reflectionError = error instanceof Error ? error : new Error(String(error));
|
|
26009
|
-
if (abortSignal?.aborted) throw error;
|
|
26055
|
+
if (lifecycleError !== void 0 || abortSignal?.aborted) throw error;
|
|
26010
26056
|
omError("[OM] Reflection failed", error);
|
|
26011
26057
|
} finally {
|
|
26012
|
-
await this.storage.setReflectingFlag(record.id, false);
|
|
26013
26058
|
try {
|
|
26014
|
-
|
|
26059
|
+
await this.storage.setReflectingFlag(record.id, false);
|
|
26060
|
+
} finally {
|
|
26061
|
+
unregisterOp(record.id, "reflecting");
|
|
26062
|
+
}
|
|
26063
|
+
let endHookError;
|
|
26064
|
+
try {
|
|
26065
|
+
await reflectionHooks?.onReflectionEnd?.({
|
|
26015
26066
|
usage: reflectionUsage,
|
|
26016
26067
|
error: reflectionError,
|
|
26017
26068
|
...reflectionProviderMetadata ? { providerMetadata: reflectionProviderMetadata } : {}
|
|
26018
26069
|
});
|
|
26019
|
-
}
|
|
26020
|
-
|
|
26070
|
+
} catch (error) {
|
|
26071
|
+
endHookError = error;
|
|
26072
|
+
}
|
|
26073
|
+
if (endHookError !== void 0) {
|
|
26074
|
+
if (lifecycleError === void 0 && !abortSignal?.aborted) throw endHookError;
|
|
26075
|
+
omDebug(`[OM:hooks] onReflectionEnd hook failed after cycle failure: ${endHookError instanceof Error ? endHookError.message : String(endHookError)}`);
|
|
26021
26076
|
}
|
|
26022
26077
|
}
|
|
26023
26078
|
}
|
|
@@ -26478,6 +26533,8 @@ var ObservationalMemory = class ObservationalMemory {
|
|
|
26478
26533
|
onIndexObservations;
|
|
26479
26534
|
/** Config-level lifecycle hooks fired for every observation/reflection cycle. */
|
|
26480
26535
|
hooks;
|
|
26536
|
+
/** Execution policy for config-level hooks on manual and turn-synchronous cycles. */
|
|
26537
|
+
hookExecution;
|
|
26481
26538
|
/** Observer agent runner — handles LLM calls for extracting observations. */
|
|
26482
26539
|
observer;
|
|
26483
26540
|
/** Reflector agent runner — handles LLM calls for compressing observations. */
|
|
@@ -26584,6 +26641,7 @@ var ObservationalMemory = class ObservationalMemory {
|
|
|
26584
26641
|
this.retrievalSearch = typeof config.retrieval === "object" && Boolean(config.retrieval.vector);
|
|
26585
26642
|
this.onIndexObservations = config.onIndexObservations;
|
|
26586
26643
|
this.hooks = config.hooks;
|
|
26644
|
+
this.hookExecution = config.hookExecution ?? "non-blocking";
|
|
26587
26645
|
this.mastra = config.mastra;
|
|
26588
26646
|
this.memory = config.memory;
|
|
26589
26647
|
this.curationCadence = config.curationCadence;
|
|
@@ -28376,57 +28434,54 @@ ${formattedMessages}
|
|
|
28376
28434
|
};
|
|
28377
28435
|
}
|
|
28378
28436
|
/**
|
|
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.
|
|
28437
|
+
* Compose config-level hooks with optional per-call hooks and bind context to
|
|
28438
|
+
* config-level callbacks. The configured policy applies unless a background
|
|
28439
|
+
* path explicitly requests non-blocking invocation.
|
|
28390
28440
|
*
|
|
28391
28441
|
* @internal
|
|
28392
28442
|
*/
|
|
28393
|
-
composeHooks(callHooks, context) {
|
|
28443
|
+
composeHooks(callHooks, context, execution = "configured") {
|
|
28394
28444
|
const configHooks = this.hooks;
|
|
28395
28445
|
if (!configHooks) return callHooks;
|
|
28396
28446
|
if (!callHooks && !Object.keys(configHooks).length) return void 0;
|
|
28397
|
-
const
|
|
28447
|
+
const shouldAwaitConfig = execution === "configured" && this.hookExecution === "await";
|
|
28448
|
+
const invokeConfigHook = async (name, arg) => {
|
|
28398
28449
|
const hook = configHooks[name];
|
|
28399
28450
|
if (!hook) return;
|
|
28451
|
+
if (shouldAwaitConfig) {
|
|
28452
|
+
await hook(arg);
|
|
28453
|
+
return;
|
|
28454
|
+
}
|
|
28400
28455
|
const logHookFailure = (error) => omDebug(`[OM:hooks] config-level ${name} hook failed: ${error instanceof Error ? error.message : String(error)}`);
|
|
28401
28456
|
try {
|
|
28402
|
-
const
|
|
28403
|
-
if (
|
|
28457
|
+
const result = hook(arg);
|
|
28458
|
+
if (result && typeof result.then === "function") result.then(void 0, logHookFailure);
|
|
28404
28459
|
} catch (error) {
|
|
28405
28460
|
logHookFailure(error);
|
|
28406
28461
|
}
|
|
28407
28462
|
};
|
|
28408
28463
|
return {
|
|
28409
|
-
onObservationStart: () => {
|
|
28410
|
-
|
|
28411
|
-
callHooks?.onObservationStart?.();
|
|
28464
|
+
onObservationStart: async () => {
|
|
28465
|
+
await invokeConfigHook("onObservationStart", context);
|
|
28466
|
+
await callHooks?.onObservationStart?.();
|
|
28412
28467
|
},
|
|
28413
|
-
onObservationEnd: (result) => {
|
|
28414
|
-
|
|
28468
|
+
onObservationEnd: async (result) => {
|
|
28469
|
+
await invokeConfigHook("onObservationEnd", {
|
|
28415
28470
|
...context,
|
|
28416
28471
|
...result
|
|
28417
28472
|
});
|
|
28418
|
-
callHooks?.onObservationEnd?.(result);
|
|
28473
|
+
await callHooks?.onObservationEnd?.(result);
|
|
28419
28474
|
},
|
|
28420
|
-
onReflectionStart: () => {
|
|
28421
|
-
|
|
28422
|
-
callHooks?.onReflectionStart?.();
|
|
28475
|
+
onReflectionStart: async () => {
|
|
28476
|
+
await invokeConfigHook("onReflectionStart", context);
|
|
28477
|
+
await callHooks?.onReflectionStart?.();
|
|
28423
28478
|
},
|
|
28424
|
-
onReflectionEnd: (result) => {
|
|
28425
|
-
|
|
28479
|
+
onReflectionEnd: async (result) => {
|
|
28480
|
+
await invokeConfigHook("onReflectionEnd", {
|
|
28426
28481
|
...context,
|
|
28427
28482
|
...result
|
|
28428
28483
|
});
|
|
28429
|
-
callHooks?.onReflectionEnd?.(result);
|
|
28484
|
+
await callHooks?.onReflectionEnd?.(result);
|
|
28430
28485
|
}
|
|
28431
28486
|
};
|
|
28432
28487
|
}
|
|
@@ -28439,22 +28494,29 @@ ${formattedMessages}
|
|
|
28439
28494
|
* end hook reports `error` for failed cycles even though nothing throws.
|
|
28440
28495
|
*/
|
|
28441
28496
|
async runBufferedObservationCycle(context, run) {
|
|
28442
|
-
const hooks = this.composeHooks(void 0, context);
|
|
28443
|
-
hooks?.onObservationStart?.();
|
|
28497
|
+
const hooks = this.composeHooks(void 0, context, "non-blocking");
|
|
28444
28498
|
let runResult;
|
|
28445
28499
|
let runError;
|
|
28500
|
+
let lifecycleError;
|
|
28446
28501
|
try {
|
|
28502
|
+
await hooks?.onObservationStart?.();
|
|
28447
28503
|
runResult = await run();
|
|
28448
28504
|
return runResult;
|
|
28449
28505
|
} catch (error) {
|
|
28506
|
+
lifecycleError = error;
|
|
28450
28507
|
runError = error instanceof Error ? error : new Error(String(error));
|
|
28451
28508
|
throw error;
|
|
28452
28509
|
} finally {
|
|
28453
|
-
|
|
28454
|
-
|
|
28455
|
-
|
|
28456
|
-
|
|
28457
|
-
|
|
28510
|
+
try {
|
|
28511
|
+
await hooks?.onObservationEnd?.({
|
|
28512
|
+
usage: runResult?.usage,
|
|
28513
|
+
error: runError ?? runResult?.error,
|
|
28514
|
+
...runResult?.providerMetadata ? { providerMetadata: runResult.providerMetadata } : {}
|
|
28515
|
+
});
|
|
28516
|
+
} catch (endHookError) {
|
|
28517
|
+
if (lifecycleError === void 0) throw endHookError;
|
|
28518
|
+
omDebug(`[OM:hooks] async-buffer onObservationEnd hook failed after cycle failure: ${endHookError instanceof Error ? endHookError.message : String(endHookError)}`);
|
|
28519
|
+
}
|
|
28458
28520
|
}
|
|
28459
28521
|
}
|
|
28460
28522
|
/**
|
|
@@ -28482,18 +28544,21 @@ ${formattedMessages}
|
|
|
28482
28544
|
let observed = false;
|
|
28483
28545
|
let observationUsage;
|
|
28484
28546
|
let observationProviderMetadata;
|
|
28547
|
+
let observationError;
|
|
28548
|
+
let lifecycleError;
|
|
28549
|
+
let observationStarted = false;
|
|
28485
28550
|
let generationBefore = -1;
|
|
28486
|
-
|
|
28487
|
-
|
|
28488
|
-
|
|
28489
|
-
|
|
28490
|
-
|
|
28491
|
-
|
|
28492
|
-
|
|
28493
|
-
|
|
28494
|
-
|
|
28495
|
-
|
|
28496
|
-
|
|
28551
|
+
try {
|
|
28552
|
+
await this.withLock(lockKey, async () => {
|
|
28553
|
+
const freshRecord = await this.getOrCreateRecord(threadId, resourceId);
|
|
28554
|
+
generationBefore = freshRecord.generationCount;
|
|
28555
|
+
const unobservedMessages = messages ? this.getUnobservedMessages(messages, freshRecord) : await this.loadMessagesFromStorage(threadId, resourceId, freshRecord.lastObservedAt ? new Date(freshRecord.lastObservedAt) : void 0);
|
|
28556
|
+
if (!this.meetsObservationThreshold({
|
|
28557
|
+
record: freshRecord,
|
|
28558
|
+
unobservedTokens: await this.tokenCounter.countMessagesAsync(unobservedMessages)
|
|
28559
|
+
})) return;
|
|
28560
|
+
observationStarted = true;
|
|
28561
|
+
await hooks?.onObservationStart?.();
|
|
28497
28562
|
const result = await ObservationStrategy.create(this, {
|
|
28498
28563
|
record: freshRecord,
|
|
28499
28564
|
threadId,
|
|
@@ -28511,17 +28576,22 @@ ${formattedMessages}
|
|
|
28511
28576
|
observed = result.observed;
|
|
28512
28577
|
observationUsage = result.usage;
|
|
28513
28578
|
observationProviderMetadata = result.providerMetadata;
|
|
28514
|
-
}
|
|
28515
|
-
|
|
28516
|
-
|
|
28517
|
-
|
|
28518
|
-
|
|
28519
|
-
|
|
28520
|
-
|
|
28521
|
-
|
|
28522
|
-
|
|
28523
|
-
|
|
28524
|
-
|
|
28579
|
+
});
|
|
28580
|
+
} catch (error) {
|
|
28581
|
+
lifecycleError = error;
|
|
28582
|
+
observationError = error instanceof Error ? error : new Error(String(error));
|
|
28583
|
+
}
|
|
28584
|
+
if (observationStarted) try {
|
|
28585
|
+
await hooks?.onObservationEnd?.({
|
|
28586
|
+
usage: observationUsage,
|
|
28587
|
+
error: observationError,
|
|
28588
|
+
...observationProviderMetadata ? { providerMetadata: observationProviderMetadata } : {}
|
|
28589
|
+
});
|
|
28590
|
+
} catch (endHookError) {
|
|
28591
|
+
if (lifecycleError === void 0) throw endHookError;
|
|
28592
|
+
omDebug(`[OM:hooks] onObservationEnd hook failed after cycle failure: ${endHookError instanceof Error ? endHookError.message : String(endHookError)}`);
|
|
28593
|
+
}
|
|
28594
|
+
if (lifecycleError !== void 0) throw lifecycleError;
|
|
28525
28595
|
const record = await this.getOrCreateRecord(threadId, resourceId);
|
|
28526
28596
|
const reflected = record.generationCount > generationBefore && generationBefore >= 0;
|
|
28527
28597
|
if (observed) this.maybeTriggerCadenceCuration(threadId, resourceId, record, requestContext).catch((error) => {
|
|
@@ -28595,11 +28665,18 @@ ${formattedMessages}
|
|
|
28595
28665
|
resourceId,
|
|
28596
28666
|
trigger: "manual"
|
|
28597
28667
|
});
|
|
28598
|
-
hooks?.onReflectionStart?.();
|
|
28599
28668
|
let reflectionUsage;
|
|
28600
28669
|
let reflectionProviderMetadata;
|
|
28601
28670
|
let reflectionError;
|
|
28671
|
+
let lifecycleError;
|
|
28602
28672
|
try {
|
|
28673
|
+
try {
|
|
28674
|
+
await hooks?.onReflectionStart?.();
|
|
28675
|
+
} catch (error) {
|
|
28676
|
+
lifecycleError = error;
|
|
28677
|
+
reflectionError = error instanceof Error ? error : new Error(String(error));
|
|
28678
|
+
throw error;
|
|
28679
|
+
}
|
|
28603
28680
|
const thread = await this.storage.getThreadById({ threadId });
|
|
28604
28681
|
const previousOmMetadata = getThreadOMMetadata(thread?.metadata);
|
|
28605
28682
|
const priorExtractedValues = getPriorExtractedValues(previousOmMetadata, this.reflectionConfig.extractors);
|
|
@@ -28637,6 +28714,7 @@ ${formattedMessages}
|
|
|
28637
28714
|
};
|
|
28638
28715
|
} catch (error) {
|
|
28639
28716
|
reflectionError = error instanceof Error ? error : new Error(String(error));
|
|
28717
|
+
if (lifecycleError !== void 0) throw error;
|
|
28640
28718
|
omError("[OM] reflect() failed", error);
|
|
28641
28719
|
return {
|
|
28642
28720
|
reflected: false,
|
|
@@ -28644,13 +28722,25 @@ ${formattedMessages}
|
|
|
28644
28722
|
usage: void 0
|
|
28645
28723
|
};
|
|
28646
28724
|
} finally {
|
|
28647
|
-
|
|
28648
|
-
|
|
28649
|
-
|
|
28650
|
-
|
|
28651
|
-
}
|
|
28652
|
-
|
|
28653
|
-
|
|
28725
|
+
try {
|
|
28726
|
+
await this.storage.setReflectingFlag(record.id, false);
|
|
28727
|
+
} finally {
|
|
28728
|
+
unregisterOp(record.id, "reflecting");
|
|
28729
|
+
}
|
|
28730
|
+
let endHookError;
|
|
28731
|
+
try {
|
|
28732
|
+
await hooks?.onReflectionEnd?.({
|
|
28733
|
+
usage: reflectionUsage,
|
|
28734
|
+
error: reflectionError,
|
|
28735
|
+
...reflectionProviderMetadata ? { providerMetadata: reflectionProviderMetadata } : {}
|
|
28736
|
+
});
|
|
28737
|
+
} catch (error) {
|
|
28738
|
+
endHookError = error;
|
|
28739
|
+
}
|
|
28740
|
+
if (endHookError !== void 0) {
|
|
28741
|
+
if (lifecycleError === void 0) throw endHookError;
|
|
28742
|
+
omDebug(`[OM:hooks] onReflectionEnd hook failed after cycle failure: ${endHookError instanceof Error ? endHookError.message : String(endHookError)}`);
|
|
28743
|
+
}
|
|
28654
28744
|
}
|
|
28655
28745
|
}
|
|
28656
28746
|
/**
|
|
@@ -28946,7 +29036,9 @@ var ObservationalMemoryProcessor = class {
|
|
|
28946
29036
|
return messageList;
|
|
28947
29037
|
}
|
|
28948
29038
|
const { threadId, resourceId } = context;
|
|
28949
|
-
const
|
|
29039
|
+
const memoryContext = parseMemoryRequestContext(requestContext);
|
|
29040
|
+
const runState = memoryContext?.runState?.();
|
|
29041
|
+
const readOnly = memoryContext?.memoryConfig?.readOnly;
|
|
28950
29042
|
const actorModelContext = model?.modelId ? {
|
|
28951
29043
|
provider: model.provider,
|
|
28952
29044
|
modelId: model.modelId,
|
|
@@ -28963,7 +29055,8 @@ var ObservationalMemoryProcessor = class {
|
|
|
28963
29055
|
memory: this.memory,
|
|
28964
29056
|
messageList,
|
|
28965
29057
|
threadId,
|
|
28966
|
-
resourceId
|
|
29058
|
+
resourceId,
|
|
29059
|
+
runState
|
|
28967
29060
|
});
|
|
28968
29061
|
const systemMessages = ctx.omRecord ? await this.engine.buildContextSystemMessages({
|
|
28969
29062
|
threadId,
|
|
@@ -29004,7 +29097,7 @@ var ObservationalMemoryProcessor = class {
|
|
|
29004
29097
|
this.turn.sendStateSignal = args.sendStateSignal;
|
|
29005
29098
|
this.turn.agent = args.agent;
|
|
29006
29099
|
this.turn.requestContext = requestContext;
|
|
29007
|
-
await this.turn.start(this.memory);
|
|
29100
|
+
await this.turn.start(this.memory, runState);
|
|
29008
29101
|
if (stepNumber === 0 && this.temporalMarkers) await insertTemporalGapMarkers({
|
|
29009
29102
|
messageList,
|
|
29010
29103
|
sendSignal: args.sendSignal
|
|
@@ -30174,14 +30267,18 @@ ${workingMemory}`;
|
|
|
30174
30267
|
if (!this.threadConfig.workingMemory?.enabled) return null;
|
|
30175
30268
|
return extractWorkingMemoryContent(text)?.trim() ?? null;
|
|
30176
30269
|
}
|
|
30177
|
-
async getWorkingMemory({ threadId, resourceId, memoryConfig }) {
|
|
30270
|
+
async getWorkingMemory({ threadId, resourceId, memoryConfig, runState }) {
|
|
30178
30271
|
const config = this.getMergedThreadConfig(memoryConfig || {});
|
|
30179
30272
|
if (!config.workingMemory?.enabled) return null;
|
|
30180
30273
|
const scope = config.workingMemory.scope || "resource";
|
|
30181
30274
|
let workingMemoryData = null;
|
|
30182
30275
|
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
|
-
|
|
30276
|
+
if (scope === "resource" && resourceId) {
|
|
30277
|
+
const loadWorkingMemory = async () => {
|
|
30278
|
+
return (await (await this.getMemoryStore()).getResourceById({ resourceId }))?.workingMemory || null;
|
|
30279
|
+
};
|
|
30280
|
+
workingMemoryData = runState ? await runState.load(`working-memory:resource:${resourceId}`, loadWorkingMemory) : await loadWorkingMemory();
|
|
30281
|
+
} else workingMemoryData = (runState?.threadLoaded ? runState.thread : await this.getThreadById({ threadId }))?.metadata?.workingMemory;
|
|
30185
30282
|
if (!workingMemoryData) return null;
|
|
30186
30283
|
return workingMemoryData;
|
|
30187
30284
|
}
|
|
@@ -30213,16 +30310,18 @@ ${workingMemory}`;
|
|
|
30213
30310
|
content: (config.workingMemory.template || this.defaultWorkingMemoryTemplate).trim()
|
|
30214
30311
|
};
|
|
30215
30312
|
}
|
|
30216
|
-
async getSystemMessage({ threadId, resourceId, memoryConfig }) {
|
|
30313
|
+
async getSystemMessage({ threadId, resourceId, memoryConfig, runState }) {
|
|
30217
30314
|
const config = this.getMergedThreadConfig(memoryConfig);
|
|
30218
30315
|
this.assertWorkingMemoryStateSignalsCompatibility(config);
|
|
30219
30316
|
if (!config.workingMemory?.enabled) return null;
|
|
30220
30317
|
if (config.workingMemory?.useStateSignals) return null;
|
|
30221
|
-
const
|
|
30318
|
+
const loadTemplate = () => this.getWorkingMemoryTemplate({ memoryConfig });
|
|
30319
|
+
const workingMemoryTemplate = runState ? await runState.load("working-memory:template", loadTemplate) : await loadTemplate();
|
|
30222
30320
|
const workingMemoryData = await this.getWorkingMemory({
|
|
30223
30321
|
threadId,
|
|
30224
30322
|
resourceId,
|
|
30225
|
-
memoryConfig: config
|
|
30323
|
+
memoryConfig: config,
|
|
30324
|
+
runState
|
|
30226
30325
|
});
|
|
30227
30326
|
if (!workingMemoryTemplate) return null;
|
|
30228
30327
|
const workingMemoryConfig = config.workingMemory;
|
|
@@ -30255,7 +30354,7 @@ ${workingMemory}`;
|
|
|
30255
30354
|
* ```
|
|
30256
30355
|
*/
|
|
30257
30356
|
async getContext(opts) {
|
|
30258
|
-
const { threadId, resourceId, memoryConfig } = opts;
|
|
30357
|
+
const { threadId, resourceId, memoryConfig, runState } = opts;
|
|
30259
30358
|
const config = this.getMergedThreadConfig(memoryConfig);
|
|
30260
30359
|
const memoryStore = await this.getMemoryStore();
|
|
30261
30360
|
const systemParts = [];
|
|
@@ -30265,10 +30364,14 @@ ${workingMemory}`;
|
|
|
30265
30364
|
let otherThreadsContext;
|
|
30266
30365
|
const omEngine = await this.omEngine;
|
|
30267
30366
|
if (omEngine) {
|
|
30268
|
-
|
|
30367
|
+
const loadOmRecord = () => omEngine.getRecord(threadId, resourceId);
|
|
30368
|
+
omRecord = runState ? await runState.load(`observational-memory:record:${threadId}:${resourceId ?? ""}`, loadOmRecord) : await loadOmRecord();
|
|
30269
30369
|
if (omRecord?.activeObservations) {
|
|
30270
30370
|
hasObservations = true;
|
|
30271
|
-
if (omEngine.scope === "resource" && resourceId)
|
|
30371
|
+
if (omEngine.scope === "resource" && resourceId) {
|
|
30372
|
+
const loadOtherThreadsContext = () => omEngine.getOtherThreadsContext(resourceId, threadId);
|
|
30373
|
+
otherThreadsContext = runState ? await runState.load(`observational-memory:other-threads:${resourceId}:${threadId}:${omRecord.lastObservedAt ?? ""}`, loadOtherThreadsContext) : await loadOtherThreadsContext();
|
|
30374
|
+
}
|
|
30272
30375
|
const obsSystemMessage = await omEngine.buildContextSystemMessage({
|
|
30273
30376
|
threadId,
|
|
30274
30377
|
resourceId,
|
|
@@ -30296,30 +30399,41 @@ ${workingMemory}`;
|
|
|
30296
30399
|
const workingMemoryMessage = await this.getSystemMessage({
|
|
30297
30400
|
threadId,
|
|
30298
30401
|
resourceId,
|
|
30299
|
-
memoryConfig: config
|
|
30402
|
+
memoryConfig: config,
|
|
30403
|
+
runState
|
|
30300
30404
|
});
|
|
30301
30405
|
if (workingMemoryMessage) systemParts.push(workingMemoryMessage);
|
|
30302
30406
|
let messages;
|
|
30303
30407
|
if (omEngine && omRecord) {
|
|
30304
30408
|
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
|
-
|
|
30409
|
+
const boundary = omRecord.lastObservedAt ? new Date(omRecord.lastObservedAt).toISOString() : "";
|
|
30410
|
+
if (omEngine.scope === "resource" && resourceId) {
|
|
30411
|
+
const loadMessages = async () => {
|
|
30412
|
+
return (await memoryStore.listMessagesByResourceId({
|
|
30413
|
+
resourceId,
|
|
30414
|
+
orderBy: {
|
|
30415
|
+
field: "createdAt",
|
|
30416
|
+
direction: "ASC"
|
|
30417
|
+
},
|
|
30418
|
+
perPage: false,
|
|
30419
|
+
filter: dateFilter
|
|
30420
|
+
})).messages;
|
|
30421
|
+
};
|
|
30422
|
+
messages = runState ? await runState.load(`observational-memory:messages:resource:${resourceId}:${boundary}`, loadMessages) : await loadMessages();
|
|
30423
|
+
} else {
|
|
30424
|
+
const loadMessages = async () => {
|
|
30425
|
+
return (await memoryStore.listMessages({
|
|
30426
|
+
threadId,
|
|
30427
|
+
orderBy: {
|
|
30428
|
+
field: "createdAt",
|
|
30429
|
+
direction: "ASC"
|
|
30430
|
+
},
|
|
30431
|
+
perPage: false,
|
|
30432
|
+
filter: dateFilter
|
|
30433
|
+
})).messages;
|
|
30434
|
+
};
|
|
30435
|
+
messages = runState ? await runState.load(`observational-memory:messages:thread:${threadId}:${boundary}`, loadMessages) : await loadMessages();
|
|
30436
|
+
}
|
|
30323
30437
|
} else {
|
|
30324
30438
|
const lastMessages = config.lastMessages;
|
|
30325
30439
|
if (lastMessages === false) messages = [];
|
|
@@ -31331,4 +31445,4 @@ Notes:
|
|
|
31331
31445
|
//#endregion
|
|
31332
31446
|
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
31447
|
|
|
31334
|
-
//# sourceMappingURL=src-
|
|
31448
|
+
//# sourceMappingURL=src-kLYHhPqh.js.map
|