@mastra/memory 0.0.0-error-handler-fix-20251020202607 → 0.0.0-esbuild-bundle-worker-20260807182016
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 +4656 -3
- package/LICENSE.md +15 -0
- package/README.md +26 -1
- package/dist/_types/@internal_ai-sdk-v4/dist/index.d.ts +7450 -0
- package/dist/docs/SKILL.md +62 -0
- package/dist/docs/assets/SOURCE_MAP.json +11 -0
- package/dist/docs/references/docs-agents-agent-approval.md +664 -0
- package/dist/docs/references/docs-agents-networks.md +184 -0
- package/dist/docs/references/docs-capabilities-subagents.md +454 -0
- package/dist/docs/references/docs-evals-evals-with-memory.md +146 -0
- package/dist/docs/references/docs-long-running-agents-background-tasks.md +382 -0
- package/dist/docs/references/docs-long-running-agents-goals.md +118 -0
- package/dist/docs/references/docs-memory-memory-processors.md +385 -0
- package/dist/docs/references/docs-memory-message-history.md +348 -0
- package/dist/docs/references/docs-memory-multi-user-threads.md +208 -0
- package/dist/docs/references/docs-memory-observational-memory.md +835 -0
- package/dist/docs/references/docs-memory-overview.md +266 -0
- package/dist/docs/references/docs-memory-semantic-recall.md +401 -0
- package/dist/docs/references/docs-memory-working-memory.md +431 -0
- package/dist/docs/references/docs-storage-overview.md +214 -0
- package/dist/docs/references/reference-core-getMemory.md +51 -0
- package/dist/docs/references/reference-core-listMemory.md +57 -0
- package/dist/docs/references/reference-file-based-agents-memory.md +58 -0
- package/dist/docs/references/reference-memory-clone-utilities.md +203 -0
- package/dist/docs/references/reference-memory-cloneThread.md +173 -0
- package/dist/docs/references/reference-memory-createThread.md +70 -0
- package/dist/docs/references/reference-memory-getThreadById.md +26 -0
- package/dist/docs/references/reference-memory-listThreads.md +147 -0
- package/dist/docs/references/reference-memory-memory-class.md +148 -0
- package/dist/docs/references/reference-memory-observational-memory.md +877 -0
- package/dist/docs/references/reference-memory-summarizeConversation.md +99 -0
- package/dist/docs/references/reference-memory-summarizeThread.md +93 -0
- package/dist/docs/references/reference-processors-token-limiter-processor.md +158 -0
- package/dist/docs/references/reference-storage-dsql.md +430 -0
- package/dist/docs/references/reference-storage-dynamodb.md +284 -0
- package/dist/docs/references/reference-storage-libsql.md +143 -0
- package/dist/docs/references/reference-storage-mongodb.md +267 -0
- package/dist/docs/references/reference-storage-postgresql.md +531 -0
- package/dist/docs/references/reference-storage-redis.md +268 -0
- package/dist/docs/references/reference-storage-upstash.md +162 -0
- package/dist/docs/references/reference-vectors-libsql.md +307 -0
- package/dist/docs/references/reference-vectors-mongodb.md +567 -0
- package/dist/docs/references/reference-vectors-pg.md +430 -0
- package/dist/docs/references/reference-vectors-upstash.md +296 -0
- package/dist/index.cjs +35 -893
- package/dist/index.d.ts +465 -62
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -887
- package/dist/processors/index.cjs +32 -165
- package/dist/processors/index.d.ts +1 -2
- package/dist/processors/index.d.ts.map +1 -1
- package/dist/processors/index.js +2 -158
- package/dist/processors/observational-memory/activation-ttl.d.ts +4 -0
- package/dist/processors/observational-memory/activation-ttl.d.ts.map +1 -0
- package/dist/processors/observational-memory/anchor-ids.d.ts +4 -0
- package/dist/processors/observational-memory/anchor-ids.d.ts.map +1 -0
- package/dist/processors/observational-memory/buffering-coordinator.d.ts +61 -0
- package/dist/processors/observational-memory/buffering-coordinator.d.ts.map +1 -0
- package/dist/processors/observational-memory/built-in-extractors.d.ts +15 -0
- package/dist/processors/observational-memory/built-in-extractors.d.ts.map +1 -0
- package/dist/processors/observational-memory/constants.d.ts +74 -0
- package/dist/processors/observational-memory/constants.d.ts.map +1 -0
- package/dist/processors/observational-memory/date-utils.d.ts +41 -0
- package/dist/processors/observational-memory/date-utils.d.ts.map +1 -0
- package/dist/processors/observational-memory/debug.d.ts +3 -0
- package/dist/processors/observational-memory/debug.d.ts.map +1 -0
- package/dist/processors/observational-memory/extracted-values.d.ts +44 -0
- package/dist/processors/observational-memory/extracted-values.d.ts.map +1 -0
- package/dist/processors/observational-memory/extraction-runner.d.ts +22 -0
- package/dist/processors/observational-memory/extraction-runner.d.ts.map +1 -0
- package/dist/processors/observational-memory/extractor.d.ts +76 -0
- package/dist/processors/observational-memory/extractor.d.ts.map +1 -0
- package/dist/processors/observational-memory/index.d.ts +30 -0
- package/dist/processors/observational-memory/index.d.ts.map +1 -0
- package/dist/processors/observational-memory/internal-request-context.d.ts +17 -0
- package/dist/processors/observational-memory/internal-request-context.d.ts.map +1 -0
- package/dist/processors/observational-memory/markers.d.ts +118 -0
- package/dist/processors/observational-memory/markers.d.ts.map +1 -0
- package/dist/processors/observational-memory/message-utils.d.ts +83 -0
- package/dist/processors/observational-memory/message-utils.d.ts.map +1 -0
- package/dist/processors/observational-memory/model-by-input-tokens.d.ts +14 -0
- package/dist/processors/observational-memory/model-by-input-tokens.d.ts.map +1 -0
- package/dist/processors/observational-memory/model-context.d.ts +2 -0
- package/dist/processors/observational-memory/model-context.d.ts.map +1 -0
- package/dist/processors/observational-memory/observation-groups.d.ts +15 -0
- package/dist/processors/observational-memory/observation-groups.d.ts.map +1 -0
- package/dist/processors/observational-memory/observation-strategies/async-buffer.d.ts +39 -0
- package/dist/processors/observational-memory/observation-strategies/async-buffer.d.ts.map +1 -0
- package/dist/processors/observational-memory/observation-strategies/base.d.ts +122 -0
- package/dist/processors/observational-memory/observation-strategies/base.d.ts.map +1 -0
- package/dist/processors/observational-memory/observation-strategies/index.d.ts +7 -0
- package/dist/processors/observational-memory/observation-strategies/index.d.ts.map +1 -0
- package/dist/processors/observational-memory/observation-strategies/resource-scoped.d.ts +43 -0
- package/dist/processors/observational-memory/observation-strategies/resource-scoped.d.ts.map +1 -0
- package/dist/processors/observational-memory/observation-strategies/sync.d.ts +41 -0
- package/dist/processors/observational-memory/observation-strategies/sync.d.ts.map +1 -0
- package/dist/processors/observational-memory/observation-strategies/types.d.ts +103 -0
- package/dist/processors/observational-memory/observation-strategies/types.d.ts.map +1 -0
- package/dist/processors/observational-memory/observation-turn/index.d.ts +4 -0
- package/dist/processors/observational-memory/observation-turn/index.d.ts.map +1 -0
- package/dist/processors/observational-memory/observation-turn/load-memory-context.d.ts +9 -0
- package/dist/processors/observational-memory/observation-turn/load-memory-context.d.ts.map +1 -0
- package/dist/processors/observational-memory/observation-turn/step.d.ts +53 -0
- package/dist/processors/observational-memory/observation-turn/step.d.ts.map +1 -0
- package/dist/processors/observational-memory/observation-turn/turn.d.ts +139 -0
- package/dist/processors/observational-memory/observation-turn/turn.d.ts.map +1 -0
- package/dist/processors/observational-memory/observation-turn/types.d.ts +46 -0
- package/dist/processors/observational-memory/observation-turn/types.d.ts.map +1 -0
- package/dist/processors/observational-memory/observation-utils.d.ts +16 -0
- package/dist/processors/observational-memory/observation-utils.d.ts.map +1 -0
- package/dist/processors/observational-memory/observational-memory.d.ts +930 -0
- package/dist/processors/observational-memory/observational-memory.d.ts.map +1 -0
- package/dist/processors/observational-memory/observer-agent.d.ts +190 -0
- package/dist/processors/observational-memory/observer-agent.d.ts.map +1 -0
- package/dist/processors/observational-memory/observer-runner.d.ts +141 -0
- package/dist/processors/observational-memory/observer-runner.d.ts.map +1 -0
- package/dist/processors/observational-memory/operation-registry.d.ts +14 -0
- package/dist/processors/observational-memory/operation-registry.d.ts.map +1 -0
- package/dist/processors/observational-memory/processor.d.ts +70 -0
- package/dist/processors/observational-memory/processor.d.ts.map +1 -0
- package/dist/processors/observational-memory/reflector-agent.d.ts +62 -0
- package/dist/processors/observational-memory/reflector-agent.d.ts.map +1 -0
- package/dist/processors/observational-memory/reflector-runner.d.ts +133 -0
- package/dist/processors/observational-memory/reflector-runner.d.ts.map +1 -0
- package/dist/processors/observational-memory/repro-capture.d.ts +33 -0
- package/dist/processors/observational-memory/repro-capture.d.ts.map +1 -0
- package/dist/processors/observational-memory/retry.d.ts +63 -0
- package/dist/processors/observational-memory/retry.d.ts.map +1 -0
- package/dist/processors/observational-memory/string-utils.d.ts +13 -0
- package/dist/processors/observational-memory/string-utils.d.ts.map +1 -0
- package/dist/processors/observational-memory/summarize.d.ts +92 -0
- package/dist/processors/observational-memory/summarize.d.ts.map +1 -0
- package/dist/processors/observational-memory/temporal-markers.d.ts +4 -0
- package/dist/processors/observational-memory/temporal-markers.d.ts.map +1 -0
- package/dist/processors/observational-memory/temporary-memory.d.ts +8 -0
- package/dist/processors/observational-memory/temporary-memory.d.ts.map +1 -0
- package/dist/processors/observational-memory/thresholds.d.ts +52 -0
- package/dist/processors/observational-memory/thresholds.d.ts.map +1 -0
- package/dist/processors/observational-memory/token-counter.d.ts +57 -0
- package/dist/processors/observational-memory/token-counter.d.ts.map +1 -0
- package/dist/processors/observational-memory/tool-result-helpers.d.ts +12 -0
- package/dist/processors/observational-memory/tool-result-helpers.d.ts.map +1 -0
- package/dist/processors/observational-memory/tracing.d.ts +17 -0
- package/dist/processors/observational-memory/tracing.d.ts.map +1 -0
- package/dist/processors/observational-memory/types.d.ts +997 -0
- package/dist/processors/observational-memory/types.d.ts.map +1 -0
- package/dist/processors/observational-memory/working-memory-extractor.d.ts +5 -0
- package/dist/processors/observational-memory/working-memory-extractor.d.ts.map +1 -0
- package/dist/processors/working-memory-state/index.d.ts +2 -0
- package/dist/processors/working-memory-state/index.d.ts.map +1 -0
- package/dist/processors/working-memory-state/processor.d.ts +53 -0
- package/dist/processors/working-memory-state/processor.d.ts.map +1 -0
- package/dist/src-C1tlhGeW.js +28559 -0
- package/dist/src-C1tlhGeW.js.map +1 -0
- package/dist/src-DqoifKIy.cjs +28813 -0
- package/dist/src-DqoifKIy.cjs.map +1 -0
- package/dist/tools/om-tools.d.ts +172 -0
- package/dist/tools/om-tools.d.ts.map +1 -0
- package/dist/tools/working-memory.d.ts +33 -28
- package/dist/tools/working-memory.d.ts.map +1 -1
- package/package.json +36 -28
- package/dist/index.cjs.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/processors/index.cjs.map +0 -1
- package/dist/processors/index.js.map +0 -1
- package/dist/processors/token-limiter.d.ts +0 -32
- package/dist/processors/token-limiter.d.ts.map +0 -1
- package/dist/processors/tool-call-filter.d.ts +0 -20
- package/dist/processors/tool-call-filter.d.ts.map +0 -1
|
@@ -1,165 +1,32 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
} else {
|
|
34
|
-
this.maxTokens = options.limit;
|
|
35
|
-
this.encoder = new lite.Tiktoken(options.encoding || o200k_base__default.default);
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
process(messages, { systemMessage, memorySystemMessage, newMessages } = {}) {
|
|
39
|
-
let totalTokens = 0;
|
|
40
|
-
totalTokens += this.TOKENS_PER_CONVERSATION;
|
|
41
|
-
if (systemMessage) {
|
|
42
|
-
totalTokens += this.countTokens(systemMessage);
|
|
43
|
-
totalTokens += this.TOKENS_PER_MESSAGE;
|
|
44
|
-
}
|
|
45
|
-
if (memorySystemMessage) {
|
|
46
|
-
totalTokens += this.countTokens(memorySystemMessage);
|
|
47
|
-
totalTokens += this.TOKENS_PER_MESSAGE;
|
|
48
|
-
}
|
|
49
|
-
const allMessages = [...messages, ...newMessages || []];
|
|
50
|
-
const result = [];
|
|
51
|
-
for (let i = allMessages.length - 1; i >= 0; i--) {
|
|
52
|
-
const message = allMessages[i];
|
|
53
|
-
if (!message) continue;
|
|
54
|
-
const messageTokens = this.countTokens(message);
|
|
55
|
-
if (totalTokens + messageTokens <= this.maxTokens) {
|
|
56
|
-
if (i < messages.length) {
|
|
57
|
-
result.unshift(message);
|
|
58
|
-
}
|
|
59
|
-
totalTokens += messageTokens;
|
|
60
|
-
} else {
|
|
61
|
-
this.logger.info(
|
|
62
|
-
`filtering ${allMessages.length - result.length}/${allMessages.length} messages, token limit of ${this.maxTokens} exceeded`
|
|
63
|
-
);
|
|
64
|
-
break;
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
return result;
|
|
68
|
-
}
|
|
69
|
-
countTokens(message) {
|
|
70
|
-
if (typeof message === `string`) {
|
|
71
|
-
return this.encoder.encode(message).length;
|
|
72
|
-
}
|
|
73
|
-
let tokenString = message.role;
|
|
74
|
-
let overhead = 0;
|
|
75
|
-
if (typeof message.content === "string" && message.content) {
|
|
76
|
-
tokenString += message.content;
|
|
77
|
-
} else if (Array.isArray(message.content)) {
|
|
78
|
-
for (const part of message.content) {
|
|
79
|
-
if (part.type === "text") {
|
|
80
|
-
tokenString += part.text;
|
|
81
|
-
} else if (part.type === "tool-call" || part.type === `tool-result`) {
|
|
82
|
-
if (`args` in part && part.args && part.type === `tool-call`) {
|
|
83
|
-
tokenString += part.toolName;
|
|
84
|
-
if (typeof part.args === "string") {
|
|
85
|
-
tokenString += part.args;
|
|
86
|
-
} else {
|
|
87
|
-
tokenString += JSON.stringify(part.args);
|
|
88
|
-
overhead -= 12;
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
if (`result` in part && part.result !== void 0 && part.type === `tool-result`) {
|
|
92
|
-
if (typeof part.result === "string") {
|
|
93
|
-
tokenString += part.result;
|
|
94
|
-
} else {
|
|
95
|
-
tokenString += JSON.stringify(part.result);
|
|
96
|
-
overhead -= 12;
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
} else {
|
|
100
|
-
tokenString += JSON.stringify(part);
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
if (typeof message.content === `string` || // if the message included non-tool parts, add our message overhead
|
|
105
|
-
message.content.some((p) => p.type !== `tool-call` && p.type !== `tool-result`)) {
|
|
106
|
-
overhead += this.TOKENS_PER_MESSAGE;
|
|
107
|
-
}
|
|
108
|
-
return this.encoder.encode(tokenString).length + overhead;
|
|
109
|
-
}
|
|
110
|
-
};
|
|
111
|
-
var ToolCallFilter = class extends memory.MemoryProcessor {
|
|
112
|
-
exclude;
|
|
113
|
-
/**
|
|
114
|
-
* Create a filter for tool calls and results.
|
|
115
|
-
* @param options Configuration options
|
|
116
|
-
* @param options.exclude List of specific tool names to exclude. If not provided, all tool calls are excluded.
|
|
117
|
-
*/
|
|
118
|
-
constructor(options = {}) {
|
|
119
|
-
super({ name: "ToolCallFilter" });
|
|
120
|
-
if (!options || !options.exclude) {
|
|
121
|
-
this.exclude = "all";
|
|
122
|
-
} else {
|
|
123
|
-
this.exclude = Array.isArray(options.exclude) ? options.exclude : [];
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
process(messages) {
|
|
127
|
-
if (this.exclude === "all") {
|
|
128
|
-
return messages.filter((message) => {
|
|
129
|
-
if (Array.isArray(message.content)) {
|
|
130
|
-
return !message.content.some((part) => part.type === "tool-call" || part.type === "tool-result");
|
|
131
|
-
}
|
|
132
|
-
return true;
|
|
133
|
-
});
|
|
134
|
-
}
|
|
135
|
-
if (this.exclude.length > 0) {
|
|
136
|
-
const excludedToolCallIds = /* @__PURE__ */ new Set();
|
|
137
|
-
return messages.filter((message) => {
|
|
138
|
-
if (!Array.isArray(message.content)) return true;
|
|
139
|
-
if (message.role === "assistant") {
|
|
140
|
-
let shouldExclude = false;
|
|
141
|
-
for (const part of message.content) {
|
|
142
|
-
if (part.type === "tool-call" && this.exclude.includes(part.toolName)) {
|
|
143
|
-
excludedToolCallIds.add(part.toolCallId);
|
|
144
|
-
shouldExclude = true;
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
|
-
return !shouldExclude;
|
|
148
|
-
}
|
|
149
|
-
if (message.role === "tool") {
|
|
150
|
-
const shouldExclude = message.content.some(
|
|
151
|
-
(part) => part.type === "tool-result" && excludedToolCallIds.has(part.toolCallId)
|
|
152
|
-
);
|
|
153
|
-
return !shouldExclude;
|
|
154
|
-
}
|
|
155
|
-
return true;
|
|
156
|
-
});
|
|
157
|
-
}
|
|
158
|
-
return messages;
|
|
159
|
-
}
|
|
160
|
-
};
|
|
161
|
-
|
|
162
|
-
exports.TokenLimiter = TokenLimiter;
|
|
163
|
-
exports.ToolCallFilter = ToolCallFilter;
|
|
164
|
-
//# sourceMappingURL=index.cjs.map
|
|
165
|
-
//# sourceMappingURL=index.cjs.map
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
const require_src = require("../src-DqoifKIy.cjs");
|
|
3
|
+
exports.Extractor = require_src.Extractor;
|
|
4
|
+
exports.ModelByInputTokens = require_src.ModelByInputTokens;
|
|
5
|
+
exports.OBSERVATIONAL_MEMORY_DEFAULTS = require_src.OBSERVATIONAL_MEMORY_DEFAULTS;
|
|
6
|
+
exports.OBSERVATION_CONTEXT_INSTRUCTIONS = require_src.OBSERVATION_CONTEXT_INSTRUCTIONS;
|
|
7
|
+
exports.OBSERVATION_CONTEXT_PROMPT = require_src.OBSERVATION_CONTEXT_PROMPT;
|
|
8
|
+
exports.OBSERVATION_CONTINUATION_HINT = require_src.OBSERVATION_CONTINUATION_HINT;
|
|
9
|
+
exports.OBSERVER_SYSTEM_PROMPT = require_src.OBSERVER_SYSTEM_PROMPT;
|
|
10
|
+
exports.ObservationalMemory = require_src.ObservationalMemory;
|
|
11
|
+
exports.ObservationalMemoryProcessor = require_src.ObservationalMemoryProcessor;
|
|
12
|
+
exports.TokenCounter = require_src.TokenCounter;
|
|
13
|
+
exports.WorkingMemoryExtractor = require_src.WorkingMemoryExtractor;
|
|
14
|
+
exports.buildObserverPrompt = require_src.buildObserverPrompt;
|
|
15
|
+
exports.buildObserverSystemPrompt = require_src.buildObserverSystemPrompt;
|
|
16
|
+
exports.combineObservationGroupRanges = require_src.combineObservationGroupRanges;
|
|
17
|
+
exports.deriveObservationGroupProvenance = require_src.deriveObservationGroupProvenance;
|
|
18
|
+
exports.extractCurrentTask = require_src.extractCurrentTask;
|
|
19
|
+
exports.formatMessagesForObserver = require_src.formatMessagesForObserver;
|
|
20
|
+
exports.getObservationsAsOf = require_src.getObservationsAsOf;
|
|
21
|
+
exports.hasCurrentTaskSection = require_src.hasCurrentTaskSection;
|
|
22
|
+
exports.injectAnchorIds = require_src.injectAnchorIds;
|
|
23
|
+
exports.optimizeObservationsForContext = require_src.optimizeObservationsForContext;
|
|
24
|
+
exports.parseAnchorId = require_src.parseAnchorId;
|
|
25
|
+
exports.parseObservationGroups = require_src.parseObservationGroups;
|
|
26
|
+
exports.parseObserverOutput = require_src.parseObserverOutput;
|
|
27
|
+
exports.reconcileObservationGroupsFromReflection = require_src.reconcileObservationGroupsFromReflection;
|
|
28
|
+
exports.renderObservationGroupsForReflection = require_src.renderObservationGroupsForReflection;
|
|
29
|
+
exports.stripEphemeralAnchorIds = require_src.stripEphemeralAnchorIds;
|
|
30
|
+
exports.stripObservationGroups = require_src.stripObservationGroups;
|
|
31
|
+
exports.summarizeConversation = require_src.summarizeConversation;
|
|
32
|
+
exports.wrapInObservationGroup = require_src.wrapInObservationGroup;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/processors/index.ts"],"names":[],"mappings":"AAAA,cAAc,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/processors/index.ts"],"names":[],"mappings":"AAAA,cAAc,wBAAwB,CAAC"}
|
package/dist/processors/index.js
CHANGED
|
@@ -1,158 +1,2 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
import o200k_base from 'js-tiktoken/ranks/o200k_base';
|
|
4
|
-
|
|
5
|
-
// src/processors/token-limiter.ts
|
|
6
|
-
var TokenLimiter = class extends MemoryProcessor {
|
|
7
|
-
encoder;
|
|
8
|
-
maxTokens;
|
|
9
|
-
// Token overheads per OpenAI's documentation
|
|
10
|
-
// See: https://cookbook.openai.com/examples/how_to_count_tokens_with_tiktoken#6-counting-tokens-for-chat-completions-api-calls
|
|
11
|
-
// Every message follows <|start|>{role/name}\n{content}<|end|>
|
|
12
|
-
TOKENS_PER_MESSAGE = 3.8;
|
|
13
|
-
// tokens added for each message (start & end tokens)
|
|
14
|
-
TOKENS_PER_CONVERSATION = 24;
|
|
15
|
-
// fixed overhead for the conversation
|
|
16
|
-
/**
|
|
17
|
-
* Create a token limiter for messages.
|
|
18
|
-
* @param options Either a number (token limit) or a configuration object
|
|
19
|
-
*/
|
|
20
|
-
constructor(options) {
|
|
21
|
-
super({
|
|
22
|
-
name: "TokenLimiter"
|
|
23
|
-
});
|
|
24
|
-
if (typeof options === "number") {
|
|
25
|
-
this.maxTokens = options;
|
|
26
|
-
this.encoder = new Tiktoken(o200k_base);
|
|
27
|
-
} else {
|
|
28
|
-
this.maxTokens = options.limit;
|
|
29
|
-
this.encoder = new Tiktoken(options.encoding || o200k_base);
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
process(messages, { systemMessage, memorySystemMessage, newMessages } = {}) {
|
|
33
|
-
let totalTokens = 0;
|
|
34
|
-
totalTokens += this.TOKENS_PER_CONVERSATION;
|
|
35
|
-
if (systemMessage) {
|
|
36
|
-
totalTokens += this.countTokens(systemMessage);
|
|
37
|
-
totalTokens += this.TOKENS_PER_MESSAGE;
|
|
38
|
-
}
|
|
39
|
-
if (memorySystemMessage) {
|
|
40
|
-
totalTokens += this.countTokens(memorySystemMessage);
|
|
41
|
-
totalTokens += this.TOKENS_PER_MESSAGE;
|
|
42
|
-
}
|
|
43
|
-
const allMessages = [...messages, ...newMessages || []];
|
|
44
|
-
const result = [];
|
|
45
|
-
for (let i = allMessages.length - 1; i >= 0; i--) {
|
|
46
|
-
const message = allMessages[i];
|
|
47
|
-
if (!message) continue;
|
|
48
|
-
const messageTokens = this.countTokens(message);
|
|
49
|
-
if (totalTokens + messageTokens <= this.maxTokens) {
|
|
50
|
-
if (i < messages.length) {
|
|
51
|
-
result.unshift(message);
|
|
52
|
-
}
|
|
53
|
-
totalTokens += messageTokens;
|
|
54
|
-
} else {
|
|
55
|
-
this.logger.info(
|
|
56
|
-
`filtering ${allMessages.length - result.length}/${allMessages.length} messages, token limit of ${this.maxTokens} exceeded`
|
|
57
|
-
);
|
|
58
|
-
break;
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
return result;
|
|
62
|
-
}
|
|
63
|
-
countTokens(message) {
|
|
64
|
-
if (typeof message === `string`) {
|
|
65
|
-
return this.encoder.encode(message).length;
|
|
66
|
-
}
|
|
67
|
-
let tokenString = message.role;
|
|
68
|
-
let overhead = 0;
|
|
69
|
-
if (typeof message.content === "string" && message.content) {
|
|
70
|
-
tokenString += message.content;
|
|
71
|
-
} else if (Array.isArray(message.content)) {
|
|
72
|
-
for (const part of message.content) {
|
|
73
|
-
if (part.type === "text") {
|
|
74
|
-
tokenString += part.text;
|
|
75
|
-
} else if (part.type === "tool-call" || part.type === `tool-result`) {
|
|
76
|
-
if (`args` in part && part.args && part.type === `tool-call`) {
|
|
77
|
-
tokenString += part.toolName;
|
|
78
|
-
if (typeof part.args === "string") {
|
|
79
|
-
tokenString += part.args;
|
|
80
|
-
} else {
|
|
81
|
-
tokenString += JSON.stringify(part.args);
|
|
82
|
-
overhead -= 12;
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
if (`result` in part && part.result !== void 0 && part.type === `tool-result`) {
|
|
86
|
-
if (typeof part.result === "string") {
|
|
87
|
-
tokenString += part.result;
|
|
88
|
-
} else {
|
|
89
|
-
tokenString += JSON.stringify(part.result);
|
|
90
|
-
overhead -= 12;
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
} else {
|
|
94
|
-
tokenString += JSON.stringify(part);
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
if (typeof message.content === `string` || // if the message included non-tool parts, add our message overhead
|
|
99
|
-
message.content.some((p) => p.type !== `tool-call` && p.type !== `tool-result`)) {
|
|
100
|
-
overhead += this.TOKENS_PER_MESSAGE;
|
|
101
|
-
}
|
|
102
|
-
return this.encoder.encode(tokenString).length + overhead;
|
|
103
|
-
}
|
|
104
|
-
};
|
|
105
|
-
var ToolCallFilter = class extends MemoryProcessor {
|
|
106
|
-
exclude;
|
|
107
|
-
/**
|
|
108
|
-
* Create a filter for tool calls and results.
|
|
109
|
-
* @param options Configuration options
|
|
110
|
-
* @param options.exclude List of specific tool names to exclude. If not provided, all tool calls are excluded.
|
|
111
|
-
*/
|
|
112
|
-
constructor(options = {}) {
|
|
113
|
-
super({ name: "ToolCallFilter" });
|
|
114
|
-
if (!options || !options.exclude) {
|
|
115
|
-
this.exclude = "all";
|
|
116
|
-
} else {
|
|
117
|
-
this.exclude = Array.isArray(options.exclude) ? options.exclude : [];
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
process(messages) {
|
|
121
|
-
if (this.exclude === "all") {
|
|
122
|
-
return messages.filter((message) => {
|
|
123
|
-
if (Array.isArray(message.content)) {
|
|
124
|
-
return !message.content.some((part) => part.type === "tool-call" || part.type === "tool-result");
|
|
125
|
-
}
|
|
126
|
-
return true;
|
|
127
|
-
});
|
|
128
|
-
}
|
|
129
|
-
if (this.exclude.length > 0) {
|
|
130
|
-
const excludedToolCallIds = /* @__PURE__ */ new Set();
|
|
131
|
-
return messages.filter((message) => {
|
|
132
|
-
if (!Array.isArray(message.content)) return true;
|
|
133
|
-
if (message.role === "assistant") {
|
|
134
|
-
let shouldExclude = false;
|
|
135
|
-
for (const part of message.content) {
|
|
136
|
-
if (part.type === "tool-call" && this.exclude.includes(part.toolName)) {
|
|
137
|
-
excludedToolCallIds.add(part.toolCallId);
|
|
138
|
-
shouldExclude = true;
|
|
139
|
-
}
|
|
140
|
-
}
|
|
141
|
-
return !shouldExclude;
|
|
142
|
-
}
|
|
143
|
-
if (message.role === "tool") {
|
|
144
|
-
const shouldExclude = message.content.some(
|
|
145
|
-
(part) => part.type === "tool-result" && excludedToolCallIds.has(part.toolCallId)
|
|
146
|
-
);
|
|
147
|
-
return !shouldExclude;
|
|
148
|
-
}
|
|
149
|
-
return true;
|
|
150
|
-
});
|
|
151
|
-
}
|
|
152
|
-
return messages;
|
|
153
|
-
}
|
|
154
|
-
};
|
|
155
|
-
|
|
156
|
-
export { TokenLimiter, ToolCallFilter };
|
|
157
|
-
//# sourceMappingURL=index.js.map
|
|
158
|
-
//# sourceMappingURL=index.js.map
|
|
1
|
+
import { A as extractCurrentTask, B as OBSERVATION_CONTEXT_INSTRUCTIONS, C as WorkingMemoryExtractor, D as OBSERVER_SYSTEM_PROMPT, E as TokenCounter, F as injectAnchorIds, H as OBSERVATION_CONTINUATION_HINT, I as parseAnchorId, L as stripEphemeralAnchorIds, M as hasCurrentTaskSection, N as optimizeObservationsForContext, O as buildObserverPrompt, P as parseObserverOutput, R as Extractor, T as summarizeConversation, V as OBSERVATION_CONTEXT_PROMPT, _ as reconcileObservationGroupsFromReflection, b as wrapInObservationGroup, d as getObservationsAsOf, f as ObservationalMemoryProcessor, g as parseObservationGroups, h as deriveObservationGroupProvenance, j as formatMessagesForObserver, k as buildObserverSystemPrompt, m as combineObservationGroupRanges, p as ObservationalMemory, v as renderObservationGroupsForReflection, x as ModelByInputTokens, y as stripObservationGroups, z as OBSERVATIONAL_MEMORY_DEFAULTS } from "../src-C1tlhGeW.js";
|
|
2
|
+
export { Extractor, ModelByInputTokens, OBSERVATIONAL_MEMORY_DEFAULTS, OBSERVATION_CONTEXT_INSTRUCTIONS, OBSERVATION_CONTEXT_PROMPT, OBSERVATION_CONTINUATION_HINT, OBSERVER_SYSTEM_PROMPT, ObservationalMemory, ObservationalMemoryProcessor, TokenCounter, WorkingMemoryExtractor, buildObserverPrompt, buildObserverSystemPrompt, combineObservationGroupRanges, deriveObservationGroupProvenance, extractCurrentTask, formatMessagesForObserver, getObservationsAsOf, hasCurrentTaskSection, injectAnchorIds, optimizeObservationsForContext, parseAnchorId, parseObservationGroups, parseObserverOutput, reconcileObservationGroupsFromReflection, renderObservationGroupsForReflection, stripEphemeralAnchorIds, stripObservationGroups, summarizeConversation, wrapInObservationGroup };
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { ObservationModelContext, ResolvedActivationTTL } from './types.js';
|
|
2
|
+
export declare function resolveActivationTTL(activateAfterIdle: ResolvedActivationTTL | undefined, modelContext?: ObservationModelContext): number | undefined;
|
|
3
|
+
export declare function resolveAutoActivationTTL(modelContext?: ObservationModelContext): number;
|
|
4
|
+
//# sourceMappingURL=activation-ttl.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"activation-ttl.d.ts","sourceRoot":"","sources":["../../../src/processors/observational-memory/activation-ttl.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,uBAAuB,EAAE,qBAAqB,EAAE,MAAM,SAAS,CAAC;AA4B9E,wBAAgB,oBAAoB,CAClC,iBAAiB,EAAE,qBAAqB,GAAG,SAAS,EACpD,YAAY,CAAC,EAAE,uBAAuB,GACrC,MAAM,GAAG,SAAS,CAMpB;AAED,wBAAgB,wBAAwB,CAAC,YAAY,CAAC,EAAE,uBAAuB,GAAG,MAAM,CAmBvF"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"anchor-ids.d.ts","sourceRoot":"","sources":["../../../src/processors/observational-memory/anchor-ids.ts"],"names":[],"mappings":"AAUA,wBAAgB,aAAa,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAGzD;AAiCD,wBAAgB,eAAe,CAAC,YAAY,EAAE,MAAM,GAAG,MAAM,CAmC5D;AAED,wBAAgB,uBAAuB,CAAC,YAAY,EAAE,MAAM,GAAG,MAAM,CAMpE"}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import type { ObservationalMemoryRecord } from '@mastra/core/storage';
|
|
2
|
+
import type { ResolvedObservationConfig, ResolvedReflectionConfig } from './types.js';
|
|
3
|
+
/**
|
|
4
|
+
* Manages the static buffering state machine for async observation and reflection.
|
|
5
|
+
*
|
|
6
|
+
* Static maps are shared across all ObservationalMemory instances in a process.
|
|
7
|
+
* This is critical because multiple OM instances are created per agent loop step,
|
|
8
|
+
* and they need to share knowledge of in-flight operations.
|
|
9
|
+
*/
|
|
10
|
+
export declare class BufferingCoordinator {
|
|
11
|
+
private readonly observationConfig;
|
|
12
|
+
private readonly reflectionConfig;
|
|
13
|
+
private readonly scope;
|
|
14
|
+
/**
|
|
15
|
+
* Track in-flight async buffering operations per resource/thread.
|
|
16
|
+
* Key format: "obs:{lockKey}" or "refl:{lockKey}"
|
|
17
|
+
* Value: Promise that resolves when buffering completes
|
|
18
|
+
*/
|
|
19
|
+
static asyncBufferingOps: Map<string, Promise<void>>;
|
|
20
|
+
/**
|
|
21
|
+
* Track the last token boundary at which we started buffering.
|
|
22
|
+
* Key format: "obs:{lockKey}" or "refl:{lockKey}"
|
|
23
|
+
*/
|
|
24
|
+
static lastBufferedBoundary: Map<string, number>;
|
|
25
|
+
/**
|
|
26
|
+
* Track the timestamp cursor for buffered messages.
|
|
27
|
+
* Key format: "obs:{lockKey}"
|
|
28
|
+
*/
|
|
29
|
+
static lastBufferedAtTime: Map<string, Date>;
|
|
30
|
+
/**
|
|
31
|
+
* Tracks cycleId for in-flight buffered reflections.
|
|
32
|
+
* Key format: "refl:{lockKey}"
|
|
33
|
+
*/
|
|
34
|
+
static reflectionBufferCycleIds: Map<string, string>;
|
|
35
|
+
constructor(opts: {
|
|
36
|
+
observationConfig: ResolvedObservationConfig;
|
|
37
|
+
reflectionConfig: ResolvedReflectionConfig;
|
|
38
|
+
scope: 'thread' | 'resource';
|
|
39
|
+
});
|
|
40
|
+
getLockKey(threadId: string | null | undefined, resourceId: string | null | undefined): string;
|
|
41
|
+
isAsyncObservationEnabled(): boolean;
|
|
42
|
+
isAsyncReflectionEnabled(): boolean;
|
|
43
|
+
getObservationBufferKey(lockKey: string): string;
|
|
44
|
+
getReflectionBufferKey(lockKey: string): string;
|
|
45
|
+
isAsyncBufferingInProgress(bufferKey: string): boolean;
|
|
46
|
+
/**
|
|
47
|
+
* Clean up static maps for a thread/resource to prevent memory leaks.
|
|
48
|
+
*/
|
|
49
|
+
cleanupStaticMaps(threadId: string, resourceId?: string | null, activatedMessageIds?: string[]): void;
|
|
50
|
+
/**
|
|
51
|
+
* Check if we've crossed a new bufferTokens interval boundary for async observation.
|
|
52
|
+
*/
|
|
53
|
+
shouldTriggerAsyncObservation(currentTokens: number, lockKey: string, record: ObservationalMemoryRecord, storage?: {
|
|
54
|
+
setBufferingObservationFlag(id: string, flag: boolean): Promise<void>;
|
|
55
|
+
}, messageTokensThreshold?: number): boolean;
|
|
56
|
+
/**
|
|
57
|
+
* Await any in-flight async buffering operations for a given thread/resource.
|
|
58
|
+
*/
|
|
59
|
+
static awaitBuffering(threadId: string | null | undefined, resourceId: string | null | undefined, scope: 'thread' | 'resource', timeoutMs?: number): Promise<void>;
|
|
60
|
+
}
|
|
61
|
+
//# sourceMappingURL=buffering-coordinator.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"buffering-coordinator.d.ts","sourceRoot":"","sources":["../../../src/processors/observational-memory/buffering-coordinator.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,yBAAyB,EAAE,MAAM,sBAAsB,CAAC;AAItE,OAAO,KAAK,EAAE,yBAAyB,EAAE,wBAAwB,EAAE,MAAM,SAAS,CAAC;AAEnF;;;;;;GAMG;AACH,qBAAa,oBAAoB;IAC/B,OAAO,CAAC,QAAQ,CAAC,iBAAiB,CAA4B;IAC9D,OAAO,CAAC,QAAQ,CAAC,gBAAgB,CAA2B;IAC5D,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAwB;IAE9C;;;;OAIG;IACH,MAAM,CAAC,iBAAiB,6BAAoC;IAE5D;;;OAGG;IACH,MAAM,CAAC,oBAAoB,sBAA6B;IAExD;;;OAGG;IACH,MAAM,CAAC,kBAAkB,oBAA2B;IAEpD;;;OAGG;IACH,MAAM,CAAC,wBAAwB,sBAA6B;gBAEhD,IAAI,EAAE;QAChB,iBAAiB,EAAE,yBAAyB,CAAC;QAC7C,gBAAgB,EAAE,wBAAwB,CAAC;QAC3C,KAAK,EAAE,QAAQ,GAAG,UAAU,CAAC;KAC9B;IAMD,UAAU,CAAC,QAAQ,EAAE,MAAM,GAAG,IAAI,GAAG,SAAS,EAAE,UAAU,EAAE,MAAM,GAAG,IAAI,GAAG,SAAS,GAAG,MAAM;IAO9F,yBAAyB,IAAI,OAAO;IAIpC,wBAAwB,IAAI,OAAO;IAInC,uBAAuB,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM;IAIhD,sBAAsB,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM;IAI/C,0BAA0B,CAAC,SAAS,EAAE,MAAM,GAAG,OAAO;IAItD;;OAEG;IACH,iBAAiB,CAAC,QAAQ,EAAE,MAAM,EAAE,UAAU,CAAC,EAAE,MAAM,GAAG,IAAI,EAAE,mBAAmB,CAAC,EAAE,MAAM,EAAE,GAAG,IAAI;IAqBrG;;OAEG;IACH,6BAA6B,CAC3B,aAAa,EAAE,MAAM,EACrB,OAAO,EAAE,MAAM,EACf,MAAM,EAAE,yBAAyB,EACjC,OAAO,CAAC,EAAE;QAAE,2BAA2B,CAAC,EAAE,EAAE,MAAM,EAAE,IAAI,EAAE,OAAO,GAAG,OAAO,CAAC,IAAI,CAAC,CAAA;KAAE,EACnF,sBAAsB,CAAC,EAAE,MAAM,GAC9B,OAAO;IAgCV;;OAEG;WACU,cAAc,CACzB,QAAQ,EAAE,MAAM,GAAG,IAAI,GAAG,SAAS,EACnC,UAAU,EAAE,MAAM,GAAG,IAAI,GAAG,SAAS,EACrC,KAAK,EAAE,QAAQ,GAAG,UAAU,EAC5B,SAAS,SAAQ,GAChB,OAAO,CAAC,IAAI,CAAC;CA6BjB"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { Extractor } from './extractor.js';
|
|
2
|
+
import type { ObservationConfig, ReflectionConfig, ResolvedObservationConfig } from './types.js';
|
|
3
|
+
export declare function createCurrentTaskExtractor(): Extractor<string>;
|
|
4
|
+
export declare function createSuggestedResponseExtractor(): Extractor<string>;
|
|
5
|
+
export declare function createThreadTitleExtractor(): Extractor<string>;
|
|
6
|
+
interface ComposeExtractorOptions {
|
|
7
|
+
includeContinuationHints?: boolean;
|
|
8
|
+
includeThreadTitle?: boolean;
|
|
9
|
+
userExtractors?: readonly Extractor<any>[];
|
|
10
|
+
}
|
|
11
|
+
export declare function composeExtractors(options: ComposeExtractorOptions): Extractor<any>[];
|
|
12
|
+
export declare function composeObservationExtractors(config: Pick<ResolvedObservationConfig, 'threadTitle'> & Pick<ObservationConfig, 'extract'>): Extractor[];
|
|
13
|
+
export declare function composeReflectionExtractors(config: Pick<ReflectionConfig, 'extract'>): Extractor[];
|
|
14
|
+
export {};
|
|
15
|
+
//# sourceMappingURL=built-in-extractors.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"built-in-extractors.d.ts","sourceRoot":"","sources":["../../../src/processors/observational-memory/built-in-extractors.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,SAAS,EAAyB,MAAM,aAAa,CAAC;AAC/D,OAAO,KAAK,EAAE,iBAAiB,EAAE,gBAAgB,EAAE,yBAAyB,EAAE,MAAM,SAAS,CAAC;AAmB9F,wBAAgB,0BAA0B,IAAI,SAAS,CAAC,MAAM,CAAC,CAU9D;AAED,wBAAgB,gCAAgC,IAAI,SAAS,CAAC,MAAM,CAAC,CAUpE;AAED,wBAAgB,0BAA0B,IAAI,SAAS,CAAC,MAAM,CAAC,CAU9D;AAED,UAAU,uBAAuB;IAC/B,wBAAwB,CAAC,EAAE,OAAO,CAAC;IACnC,kBAAkB,CAAC,EAAE,OAAO,CAAC;IAC7B,cAAc,CAAC,EAAE,SAAS,SAAS,CAAC,GAAG,CAAC,EAAE,CAAC;CAC5C;AAED,wBAAgB,iBAAiB,CAAC,OAAO,EAAE,uBAAuB,GAAG,SAAS,CAAC,GAAG,CAAC,EAAE,CAUpF;AAED,wBAAgB,4BAA4B,CAC1C,MAAM,EAAE,IAAI,CAAC,yBAAyB,EAAE,aAAa,CAAC,GAAG,IAAI,CAAC,iBAAiB,EAAE,SAAS,CAAC,GAC1F,SAAS,EAAE,CAMb;AAED,wBAAgB,2BAA2B,CAAC,MAAM,EAAE,IAAI,CAAC,gBAAgB,EAAE,SAAS,CAAC,GAAG,SAAS,EAAE,CAKlG"}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Default configuration values matching the spec
|
|
3
|
+
*/
|
|
4
|
+
export declare const OBSERVATIONAL_MEMORY_DEFAULTS: {
|
|
5
|
+
readonly observation: {
|
|
6
|
+
readonly model: "google/gemini-2.5-flash";
|
|
7
|
+
readonly messageTokens: 30000;
|
|
8
|
+
readonly modelSettings: {
|
|
9
|
+
readonly temperature: 0.3;
|
|
10
|
+
readonly maxOutputTokens: 100000;
|
|
11
|
+
};
|
|
12
|
+
readonly providerOptions: {
|
|
13
|
+
readonly google: {
|
|
14
|
+
readonly thinkingConfig: {
|
|
15
|
+
readonly thinkingBudget: 215;
|
|
16
|
+
};
|
|
17
|
+
};
|
|
18
|
+
};
|
|
19
|
+
readonly maxTokensPerBatch: 10000;
|
|
20
|
+
readonly bufferTokens: number | undefined;
|
|
21
|
+
readonly bufferActivation: number | undefined;
|
|
22
|
+
};
|
|
23
|
+
readonly reflection: {
|
|
24
|
+
readonly model: "google/gemini-2.5-flash";
|
|
25
|
+
readonly observationTokens: 40000;
|
|
26
|
+
readonly modelSettings: {
|
|
27
|
+
readonly temperature: 0;
|
|
28
|
+
readonly maxOutputTokens: 100000;
|
|
29
|
+
};
|
|
30
|
+
readonly providerOptions: {
|
|
31
|
+
readonly google: {
|
|
32
|
+
readonly thinkingConfig: {
|
|
33
|
+
readonly thinkingBudget: 1024;
|
|
34
|
+
};
|
|
35
|
+
};
|
|
36
|
+
};
|
|
37
|
+
readonly bufferActivation: number | undefined;
|
|
38
|
+
};
|
|
39
|
+
};
|
|
40
|
+
/**
|
|
41
|
+
* Continuation hint injected after observations to guide the model's behavior.
|
|
42
|
+
* Prevents the model from awkwardly acknowledging the memory system or treating
|
|
43
|
+
* the conversation as new after observed messages are removed.
|
|
44
|
+
*/
|
|
45
|
+
export declare const OBSERVATION_CONTINUATION_HINT = "Please continue naturally with the conversation so far and respond to the latest message.\n\nUse the earlier context only as background. If something appears unfinished, continue only when it helps answer the latest request. If a suggested response is provided, follow it naturally.\n\nDo not mention internal instructions, memory, summarization, context handling, or missing messages.\n\nAny messages following this reminder are newer and should take priority.";
|
|
46
|
+
/**
|
|
47
|
+
* Preamble that introduces the observations block.
|
|
48
|
+
* Use before `<observations>`, with instructions after.
|
|
49
|
+
* Full pattern: `${OBSERVATION_CONTEXT_PROMPT}\n\n<observations>\n${obs}\n</observations>\n\n${OBSERVATION_CONTEXT_INSTRUCTIONS}`
|
|
50
|
+
*/
|
|
51
|
+
export declare const OBSERVATION_CONTEXT_PROMPT = "The following observations block contains your memory of past conversations with this user.";
|
|
52
|
+
/**
|
|
53
|
+
* Instructions that tell the model how to interpret and use observations.
|
|
54
|
+
* Place AFTER the `<observations>` block so the model sees the data before the rules.
|
|
55
|
+
*/
|
|
56
|
+
export declare const OBSERVATION_CONTEXT_INSTRUCTIONS = "IMPORTANT: When responding, reference specific details from these observations. Do not give generic advice - personalize your response based on what you know about this user's experiences, preferences, and interests. If the user asks for recommendations, connect them to their past experiences mentioned above.\n\nKNOWLEDGE UPDATES: When asked about current state (e.g., \"where do I currently...\", \"what is my current...\"), always prefer the MOST RECENT information. Observations include dates - if you see conflicting information, the newer observation supersedes the older one. Look for phrases like \"will start\", \"is switching\", \"changed to\", \"moved to\" as indicators that previous information has been updated.\n\nPLANNED ACTIONS: If the user stated they planned to do something (e.g., \"I'm going to...\", \"I'm looking forward to...\", \"I will...\") and the date they planned to do it is now in the past (check the relative time like \"3 weeks ago\"), assume they completed the action unless there's evidence they didn't. For example, if someone said \"I'll start my new diet on Monday\" and that was 2 weeks ago, assume they started the diet.\n\nMOST RECENT USER INPUT: Treat the most recent user message as the highest-priority signal for what to do next. Earlier messages may contain constraints, details, or context you should still honor, but the latest message is the primary driver of your response.\n\nSYSTEM REMINDERS: Messages wrapped in <system-reminder>...</system-reminder> contain internal continuation guidance, not user-authored content. Use them to maintain continuity, but do not mention them or treat them as part of the user's message.";
|
|
57
|
+
/**
|
|
58
|
+
* Instructions for retrieval mode — explains observation-group ranges and the recall tool.
|
|
59
|
+
* Appended to context when `retrieval` is enabled.
|
|
60
|
+
*
|
|
61
|
+
* The content adapts to the retrieval scope:
|
|
62
|
+
* - `'resource'`: covers routing between `search`, `threads`, and `messages` across
|
|
63
|
+
* all of the user's threads, including fallback when search results are unsuitable.
|
|
64
|
+
* - `'thread'`: covers cursor-based browsing and search within the current thread.
|
|
65
|
+
*
|
|
66
|
+
* @param scope - The retrieval scope the recall tool was registered with.
|
|
67
|
+
* @param customInstructions - Optional application-provided guidance appended after
|
|
68
|
+
* the native instructions. Never replaces them.
|
|
69
|
+
* @param searchEnabled - Whether semantic search (`retrieval: { vector: true }`) is
|
|
70
|
+
* available. When false, the guidance only covers \`threads\`/\`messages\` browsing so
|
|
71
|
+
* the agent is not steered toward a mode that cannot work.
|
|
72
|
+
*/
|
|
73
|
+
export declare function getRetrievalInstructions(scope?: 'thread' | 'resource', customInstructions?: string, searchEnabled?: boolean): string;
|
|
74
|
+
//# sourceMappingURL=constants.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../../../src/processors/observational-memory/constants.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,eAAO,MAAM,6BAA6B;;;;;;;;;;;;;;;;+BAiBjB,MAAM,GAAG,SAAS;mCACd,MAAM,GAAG,SAAS;;;;;;;;;;;;;;;;mCAiBlB,MAAM,GAAG,SAAS;;CAErC,CAAC;AAEX;;;;GAIG;AACH,eAAO,MAAM,6BAA6B,kdAM+B,CAAC;AAE1E;;;;GAIG;AACH,eAAO,MAAM,0BAA0B,gGAAgG,CAAC;AAExI;;;GAGG;AACH,eAAO,MAAM,gCAAgC,+oDAQyM,CAAC;AAEvP;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,wBAAwB,CACtC,KAAK,GAAE,QAAQ,GAAG,UAAuB,EACzC,kBAAkB,CAAC,EAAE,MAAM,EAC3B,aAAa,UAAO,GACnB,MAAM,CA6FR"}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import type { MastraDBMessage } from '@mastra/core/agent';
|
|
2
|
+
/**
|
|
3
|
+
* Date/time utility functions for Observational Memory.
|
|
4
|
+
* Pure functions for formatting relative timestamps and annotating observations.
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* Format a relative time string like "5 days ago", "2 weeks ago", "today", etc.
|
|
8
|
+
*/
|
|
9
|
+
export declare function formatRelativeTime(date: Date, currentDate: Date): string;
|
|
10
|
+
/**
|
|
11
|
+
* Format the gap between two dates as a human-readable string.
|
|
12
|
+
* Returns null for consecutive days (no gap marker needed).
|
|
13
|
+
*/
|
|
14
|
+
export declare function formatGapBetweenDates(prevDate: Date, currDate: Date): string | null;
|
|
15
|
+
/**
|
|
16
|
+
* Parses a date string like "May 30, 2023", "May 27-28, 2023", "late April 2023", etc.
|
|
17
|
+
* Returns the parsed Date or null if unparseable.
|
|
18
|
+
*/
|
|
19
|
+
export declare function parseDateFromContent(dateContent: string): Date | null;
|
|
20
|
+
/**
|
|
21
|
+
* Detects if an observation line indicates future intent (will do, plans to, looking forward to, etc.)
|
|
22
|
+
*/
|
|
23
|
+
export declare function isFutureIntentObservation(line: string): boolean;
|
|
24
|
+
/**
|
|
25
|
+
* Expand inline estimated dates with relative time.
|
|
26
|
+
* Matches patterns like "(estimated May 27-28, 2023)" or "(meaning May 30, 2023)"
|
|
27
|
+
* and expands them to "(meaning May 30, 2023 - which was 3 weeks ago)"
|
|
28
|
+
*/
|
|
29
|
+
export declare function expandInlineEstimatedDates(observations: string, currentDate: Date): string;
|
|
30
|
+
/**
|
|
31
|
+
* Add relative time annotations to observations.
|
|
32
|
+
* Transforms "Date: May 15, 2023" headers to "Date: May 15, 2023 (5 days ago)"
|
|
33
|
+
* and expands inline estimated dates with relative time context.
|
|
34
|
+
*/
|
|
35
|
+
export declare function addRelativeTimeToObservations(observations: string, currentDate: Date): string;
|
|
36
|
+
export declare const MIN_TEMPORAL_GAP_MS: number;
|
|
37
|
+
export declare function formatTemporalGap(diffMs: number): string | null;
|
|
38
|
+
export declare function formatTemporalTimestamp(date: Date): string;
|
|
39
|
+
export declare function getMessagePartTimestamp(msg: MastraDBMessage, position: 'first' | 'last'): number;
|
|
40
|
+
export declare function isTemporalGapMarker(msg: MastraDBMessage): boolean;
|
|
41
|
+
//# sourceMappingURL=date-utils.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"date-utils.d.ts","sourceRoot":"","sources":["../../../src/processors/observational-memory/date-utils.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,oBAAoB,CAAC;AAE1D;;;GAGG;AAEH;;GAEG;AACH,wBAAgB,kBAAkB,CAAC,IAAI,EAAE,IAAI,EAAE,WAAW,EAAE,IAAI,GAAG,MAAM,CAwBxE;AAED;;;GAGG;AACH,wBAAgB,qBAAqB,CAAC,QAAQ,EAAE,IAAI,EAAE,QAAQ,EAAE,IAAI,GAAG,MAAM,GAAG,IAAI,CAmBnF;AAED;;;GAGG;AACH,wBAAgB,oBAAoB,CAAC,WAAW,EAAE,MAAM,GAAG,IAAI,GAAG,IAAI,CAuDrE;AAED;;GAEG;AACH,wBAAgB,yBAAyB,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAa/D;AAED;;;;GAIG;AACH,wBAAgB,0BAA0B,CAAC,YAAY,EAAE,MAAM,EAAE,WAAW,EAAE,IAAI,GAAG,MAAM,CAkC1F;AAED;;;;GAIG;AACH,wBAAgB,6BAA6B,CAAC,YAAY,EAAE,MAAM,EAAE,WAAW,EAAE,IAAI,GAAG,MAAM,CA2D7F;AAED,eAAO,MAAM,mBAAmB,QAAiB,CAAC;AAElD,wBAAgB,iBAAiB,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CA+C/D;AAED,wBAAgB,uBAAuB,CAAC,IAAI,EAAE,IAAI,GAAG,MAAM,CAU1D;AAED,wBAAgB,uBAAuB,CAAC,GAAG,EAAE,eAAe,EAAE,QAAQ,EAAE,OAAO,GAAG,MAAM,GAAG,MAAM,CAYhG;AAED,wBAAgB,mBAAmB,CAAC,GAAG,EAAE,eAAe,GAAG,OAAO,CAEjE"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"debug.d.ts","sourceRoot":"","sources":["../../../src/processors/observational-memory/debug.ts"],"names":[],"mappings":"AAKA,wBAAgB,OAAO,CAAC,GAAG,EAAE,MAAM,QAOlC;AAED,wBAAgB,OAAO,CAAC,GAAG,EAAE,MAAM,EAAE,GAAG,CAAC,EAAE,OAAO,QAIjD"}
|