@mastra/memory 1.11.0 → 1.12.0-alpha.1
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 +24 -0
- package/README.md +1 -1
- package/dist/{chunk-D4D6ZFBQ.js → chunk-DDQHE4NV.js} +65 -40
- package/dist/chunk-DDQHE4NV.js.map +1 -0
- package/dist/{chunk-VINRPDYQ.cjs → chunk-HLGFIN4J.cjs} +65 -40
- package/dist/chunk-HLGFIN4J.cjs.map +1 -0
- package/dist/docs/SKILL.md +1 -1
- package/dist/docs/assets/SOURCE_MAP.json +43 -43
- package/dist/docs/references/docs-memory-message-history.md +6 -4
- package/dist/docs/references/docs-memory-observational-memory.md +20 -11
- package/dist/docs/references/docs-memory-overview.md +4 -4
- package/dist/docs/references/docs-memory-semantic-recall.md +28 -19
- package/dist/docs/references/docs-memory-storage.md +4 -4
- package/dist/docs/references/reference-memory-observational-memory.md +1 -1
- package/dist/docs/references/reference-storage-dynamodb.md +1 -1
- package/dist/docs/references/reference-storage-upstash.md +1 -1
- package/dist/index.cjs +137 -22
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +130 -15
- package/dist/index.js.map +1 -1
- package/dist/{observational-memory-FATH657E.cjs → observational-memory-34W4S4I5.cjs} +26 -26
- package/dist/{observational-memory-FATH657E.cjs.map → observational-memory-34W4S4I5.cjs.map} +1 -1
- package/dist/{observational-memory-SN7GKMHZ.js → observational-memory-B25SASRW.js} +3 -3
- package/dist/{observational-memory-SN7GKMHZ.js.map → observational-memory-B25SASRW.js.map} +1 -1
- package/dist/processors/index.cjs +24 -24
- package/dist/processors/index.js +1 -1
- package/dist/processors/observational-memory/observation-groups.d.ts +2 -2
- package/dist/processors/observational-memory/observation-groups.d.ts.map +1 -1
- package/dist/processors/observational-memory/observation-strategies/async-buffer.d.ts +1 -0
- package/dist/processors/observational-memory/observation-strategies/async-buffer.d.ts.map +1 -1
- package/dist/processors/observational-memory/observation-strategies/base.d.ts +7 -2
- package/dist/processors/observational-memory/observation-strategies/base.d.ts.map +1 -1
- package/dist/processors/observational-memory/observation-strategies/resource-scoped.d.ts +1 -0
- package/dist/processors/observational-memory/observation-strategies/resource-scoped.d.ts.map +1 -1
- package/dist/processors/observational-memory/observation-strategies/sync.d.ts +1 -0
- package/dist/processors/observational-memory/observation-strategies/sync.d.ts.map +1 -1
- package/dist/processors/observational-memory/observational-memory.d.ts.map +1 -1
- package/dist/processors/observational-memory/reflector-agent.d.ts.map +1 -1
- package/dist/tools/om-tools.d.ts.map +1 -1
- package/package.json +5 -5
- package/dist/chunk-D4D6ZFBQ.js.map +0 -1
- package/dist/chunk-VINRPDYQ.cjs.map +0 -1
|
@@ -724,10 +724,10 @@ function stripReflectionGroupMetadata(body) {
|
|
|
724
724
|
function generateAnchorId() {
|
|
725
725
|
return crypto$1.randomBytes(8).toString("hex");
|
|
726
726
|
}
|
|
727
|
-
function wrapInObservationGroup(observations, range, id = generateAnchorId(),
|
|
727
|
+
function wrapInObservationGroup(observations, range, id = generateAnchorId(), _sourceGroupIds, kind) {
|
|
728
728
|
const content = observations.trim();
|
|
729
|
-
const
|
|
730
|
-
return `<observation-group id="${id}" range="${range}"${
|
|
729
|
+
const kindAttr = kind ? ` kind="${kind}"` : "";
|
|
730
|
+
return `<observation-group id="${id}" range="${range}"${kindAttr}>
|
|
731
731
|
${content}
|
|
732
732
|
</observation-group>`;
|
|
733
733
|
}
|
|
@@ -747,8 +747,8 @@ function parseObservationGroups(observations) {
|
|
|
747
747
|
groups.push({
|
|
748
748
|
id,
|
|
749
749
|
range,
|
|
750
|
-
|
|
751
|
-
|
|
750
|
+
kind: attributes.kind,
|
|
751
|
+
content: match[2].trim()
|
|
752
752
|
});
|
|
753
753
|
}
|
|
754
754
|
return groups;
|
|
@@ -759,12 +759,22 @@ function stripObservationGroups(observations) {
|
|
|
759
759
|
}
|
|
760
760
|
return observations.replace(OBSERVATION_GROUP_PATTERN, (_match, _attributes, content) => content.trim()).replace(/\n{3,}/g, "\n\n").trim();
|
|
761
761
|
}
|
|
762
|
+
function getRangeSegments(range) {
|
|
763
|
+
return range.split(",").map((segment) => segment.trim()).filter(Boolean);
|
|
764
|
+
}
|
|
762
765
|
function combineObservationGroupRanges(groups) {
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
766
|
+
const segments = groups.flatMap((group) => getRangeSegments(group.range));
|
|
767
|
+
if (segments.length === 0) {
|
|
768
|
+
return "";
|
|
769
|
+
}
|
|
770
|
+
const firstSegment = segments[0];
|
|
771
|
+
const lastSegment = segments[segments.length - 1];
|
|
772
|
+
const firstStart = firstSegment?.split(":")[0]?.trim();
|
|
773
|
+
const lastEnd = lastSegment?.split(":").at(-1)?.trim();
|
|
774
|
+
if (firstStart && lastEnd) {
|
|
775
|
+
return `${firstStart}:${lastEnd}`;
|
|
776
|
+
}
|
|
777
|
+
return Array.from(new Set(segments)).join(",");
|
|
768
778
|
}
|
|
769
779
|
function renderObservationGroupsForReflection(observations) {
|
|
770
780
|
const groups = parseObservationGroups(observations);
|
|
@@ -801,15 +811,12 @@ function deriveObservationGroupProvenance(content, groups) {
|
|
|
801
811
|
});
|
|
802
812
|
const fallbackGroup = groups[Math.min(index, groups.length - 1)];
|
|
803
813
|
const resolvedGroups = matchingGroups.length > 0 ? matchingGroups : fallbackGroup ? [fallbackGroup] : [];
|
|
804
|
-
const sourceGroupIds = Array.from(
|
|
805
|
-
new Set(resolvedGroups.flatMap((group) => [group.id, ...group.sourceGroupIds ?? []]))
|
|
806
|
-
);
|
|
807
814
|
const canonicalGroupId = getCanonicalGroupId(section.heading, index);
|
|
808
815
|
return {
|
|
809
816
|
id: canonicalGroupId,
|
|
810
817
|
range: combineObservationGroupRanges(resolvedGroups),
|
|
811
|
-
|
|
812
|
-
|
|
818
|
+
kind: "reflection",
|
|
819
|
+
content: section.body
|
|
813
820
|
};
|
|
814
821
|
});
|
|
815
822
|
}
|
|
@@ -824,13 +831,14 @@ function reconcileObservationGroupsFromReflection(content, sourceObservations) {
|
|
|
824
831
|
}
|
|
825
832
|
const derivedGroups = deriveObservationGroupProvenance(normalizedContent, sourceGroups);
|
|
826
833
|
if (derivedGroups.length > 0) {
|
|
827
|
-
return derivedGroups.map((group) => wrapInObservationGroup(group.content, group.range, group.id, group.
|
|
834
|
+
return derivedGroups.map((group) => wrapInObservationGroup(group.content, group.range, group.id, void 0, group.kind)).join("\n\n");
|
|
828
835
|
}
|
|
829
836
|
return wrapInObservationGroup(
|
|
830
837
|
normalizedContent,
|
|
831
838
|
combineObservationGroupRanges(sourceGroups),
|
|
832
839
|
generateAnchorId(),
|
|
833
|
-
|
|
840
|
+
void 0,
|
|
841
|
+
"reflection"
|
|
834
842
|
);
|
|
835
843
|
}
|
|
836
844
|
|
|
@@ -942,7 +950,11 @@ var ObservationStrategy = class _ObservationStrategy {
|
|
|
942
950
|
retrieval;
|
|
943
951
|
/** Select the right strategy based on scope and mode. Wired up by index.ts. */
|
|
944
952
|
static create;
|
|
945
|
-
/**
|
|
953
|
+
/**
|
|
954
|
+
* Run the full observation lifecycle.
|
|
955
|
+
* @returns `true` if a full observation cycle completed; `false` if skipped (stale lock) or async-buffer failure was swallowed.
|
|
956
|
+
* @throws On sync/resource-scoped observer failure after failed markers (same as pre–Option-A contract).
|
|
957
|
+
*/
|
|
946
958
|
async run() {
|
|
947
959
|
const { record, threadId, abortSignal, writer, reflectionHooks, requestContext } = this.opts;
|
|
948
960
|
const cycleId = this.generateCycleId();
|
|
@@ -950,7 +962,7 @@ var ObservationStrategy = class _ObservationStrategy {
|
|
|
950
962
|
if (this.needsLock) {
|
|
951
963
|
const fresh = await this.storage.getObservationalMemory(record.threadId, record.resourceId);
|
|
952
964
|
if (fresh?.lastObservedAt && record.lastObservedAt && fresh.lastObservedAt > record.lastObservedAt) {
|
|
953
|
-
return;
|
|
965
|
+
return false;
|
|
954
966
|
}
|
|
955
967
|
}
|
|
956
968
|
const { messages, existingObservations } = await this.prepare();
|
|
@@ -971,23 +983,29 @@ var ObservationStrategy = class _ObservationStrategy {
|
|
|
971
983
|
observabilityContext: this.opts.observabilityContext
|
|
972
984
|
});
|
|
973
985
|
}
|
|
986
|
+
return true;
|
|
974
987
|
} catch (error) {
|
|
975
988
|
await this.emitFailedMarkers(cycleId, error);
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
989
|
+
if (!this.rethrowOnFailure) {
|
|
990
|
+
const failedMarkerForStorage = {
|
|
991
|
+
type: "data-om-observation-failed",
|
|
992
|
+
data: {
|
|
993
|
+
cycleId,
|
|
994
|
+
operationType: "observation",
|
|
995
|
+
startedAt: (/* @__PURE__ */ new Date()).toISOString(),
|
|
996
|
+
error: error instanceof Error ? error.message : String(error),
|
|
997
|
+
recordId: record.id,
|
|
998
|
+
threadId
|
|
999
|
+
}
|
|
1000
|
+
};
|
|
1001
|
+
await this.persistMarkerToStorage(failedMarkerForStorage, threadId, this.opts.resourceId).catch(() => {
|
|
1002
|
+
});
|
|
1003
|
+
if (abortSignal?.aborted) throw error;
|
|
1004
|
+
omError("[OM] Observation failed", error);
|
|
1005
|
+
return false;
|
|
1006
|
+
}
|
|
990
1007
|
omError("[OM] Observation failed", error);
|
|
1008
|
+
throw error;
|
|
991
1009
|
}
|
|
992
1010
|
}
|
|
993
1011
|
// ── Shared helpers ──────────────────────────────────────────
|
|
@@ -1208,6 +1226,9 @@ var SyncObservationStrategy = class extends ObservationStrategy {
|
|
|
1208
1226
|
get needsReflection() {
|
|
1209
1227
|
return true;
|
|
1210
1228
|
}
|
|
1229
|
+
get rethrowOnFailure() {
|
|
1230
|
+
return true;
|
|
1231
|
+
}
|
|
1211
1232
|
async prepare() {
|
|
1212
1233
|
const { record, threadId, messages } = this.opts;
|
|
1213
1234
|
this.deps.emitDebugEvent({
|
|
@@ -1401,6 +1422,9 @@ var AsyncBufferObservationStrategy = class extends ObservationStrategy {
|
|
|
1401
1422
|
get needsReflection() {
|
|
1402
1423
|
return false;
|
|
1403
1424
|
}
|
|
1425
|
+
get rethrowOnFailure() {
|
|
1426
|
+
return false;
|
|
1427
|
+
}
|
|
1404
1428
|
generateCycleId() {
|
|
1405
1429
|
return this.cycleId;
|
|
1406
1430
|
}
|
|
@@ -1537,6 +1561,9 @@ var ResourceScopedObservationStrategy = class extends ObservationStrategy {
|
|
|
1537
1561
|
get needsReflection() {
|
|
1538
1562
|
return true;
|
|
1539
1563
|
}
|
|
1564
|
+
get rethrowOnFailure() {
|
|
1565
|
+
return true;
|
|
1566
|
+
}
|
|
1540
1567
|
async prepare() {
|
|
1541
1568
|
const { record, threadId: currentThreadId, messages: currentThreadMessages } = this.opts;
|
|
1542
1569
|
const { threads: allThreads } = await this.storage.listThreads({ filter: { resourceId: this.resourceId } });
|
|
@@ -3828,11 +3855,10 @@ Aim for a 2/10 detail level. Fewer, more generic observations are better than ma
|
|
|
3828
3855
|
};
|
|
3829
3856
|
function buildReflectorPrompt(observations, manualPrompt, compressionLevel, skipContinuationHints) {
|
|
3830
3857
|
const level = typeof compressionLevel === "number" ? compressionLevel : compressionLevel ? 1 : 0;
|
|
3831
|
-
const reflectionView =
|
|
3832
|
-
const anchoredObservations = injectAnchorIds(reflectionView);
|
|
3858
|
+
const reflectionView = stripObservationGroups(observations);
|
|
3833
3859
|
let prompt = `## OBSERVATIONS TO REFLECT ON
|
|
3834
3860
|
|
|
3835
|
-
${
|
|
3861
|
+
${reflectionView}
|
|
3836
3862
|
|
|
3837
3863
|
---
|
|
3838
3864
|
|
|
@@ -7806,7 +7832,7 @@ ${grouped}` : grouped;
|
|
|
7806
7832
|
}
|
|
7807
7833
|
hooks?.onObservationStart?.();
|
|
7808
7834
|
try {
|
|
7809
|
-
await ObservationStrategy.create(this, {
|
|
7835
|
+
observed = await ObservationStrategy.create(this, {
|
|
7810
7836
|
record: freshRecord,
|
|
7811
7837
|
threadId,
|
|
7812
7838
|
resourceId,
|
|
@@ -7816,7 +7842,6 @@ ${grouped}` : grouped;
|
|
|
7816
7842
|
writer: opts.writer,
|
|
7817
7843
|
observabilityContext: opts.observabilityContext
|
|
7818
7844
|
}).run();
|
|
7819
|
-
observed = true;
|
|
7820
7845
|
} finally {
|
|
7821
7846
|
hooks?.onObservationEnd?.();
|
|
7822
7847
|
}
|
|
@@ -8450,5 +8475,5 @@ exports.stripEphemeralAnchorIds = stripEphemeralAnchorIds;
|
|
|
8450
8475
|
exports.stripObservationGroups = stripObservationGroups;
|
|
8451
8476
|
exports.truncateStringByTokens = truncateStringByTokens;
|
|
8452
8477
|
exports.wrapInObservationGroup = wrapInObservationGroup;
|
|
8453
|
-
//# sourceMappingURL=chunk-
|
|
8454
|
-
//# sourceMappingURL=chunk-
|
|
8478
|
+
//# sourceMappingURL=chunk-HLGFIN4J.cjs.map
|
|
8479
|
+
//# sourceMappingURL=chunk-HLGFIN4J.cjs.map
|