@librechat/agents 3.7.7 → 3.7.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/agents/AgentContext.cjs +26 -5
- package/dist/cjs/agents/AgentContext.cjs.map +1 -1
- package/dist/cjs/common/constants.cjs +12 -0
- package/dist/cjs/common/constants.cjs.map +1 -1
- package/dist/cjs/graphs/Graph.cjs +56 -11
- package/dist/cjs/graphs/Graph.cjs.map +1 -1
- package/dist/cjs/graphs/MultiAgentGraph.cjs +1 -1
- package/dist/cjs/hooks/HookRegistry.cjs.map +1 -1
- package/dist/cjs/hooks/executeHooks.cjs +8 -0
- package/dist/cjs/hooks/executeHooks.cjs.map +1 -1
- package/dist/cjs/hooks/index.cjs +2 -0
- package/dist/cjs/hooks/index.cjs.map +1 -1
- package/dist/cjs/hooks/types.cjs +1 -0
- package/dist/cjs/hooks/types.cjs.map +1 -1
- package/dist/cjs/langfuse.cjs +43 -3
- package/dist/cjs/langfuse.cjs.map +1 -1
- package/dist/cjs/langfuseTraceShaping.cjs +75 -0
- package/dist/cjs/langfuseTraceShaping.cjs.map +1 -1
- package/dist/cjs/llm/bedrock/toolCache.cjs +1 -1
- package/dist/cjs/llm/fake.cjs +9 -5
- package/dist/cjs/llm/fake.cjs.map +1 -1
- package/dist/cjs/llm/invoke.cjs +3 -7
- package/dist/cjs/llm/invoke.cjs.map +1 -1
- package/dist/cjs/llm/openrouter/toolCache.cjs +1 -1
- package/dist/cjs/llm/prepareProviderRequest.cjs +2 -2
- package/dist/cjs/llm/providers.cjs +1 -1
- package/dist/cjs/llm/request.cjs +6 -0
- package/dist/cjs/llm/request.cjs.map +1 -1
- package/dist/cjs/llm/truncation.cjs +1 -0
- package/dist/cjs/main.cjs +22 -13
- package/dist/cjs/messages/format.cjs +375 -3
- package/dist/cjs/messages/format.cjs.map +1 -1
- package/dist/cjs/messages/index.cjs +1 -1
- package/dist/cjs/messages/prune.cjs +1 -1
- package/dist/cjs/run.cjs +217 -52
- package/dist/cjs/run.cjs.map +1 -1
- package/dist/cjs/session/AgentSession.cjs +1 -1
- package/dist/cjs/session/JsonlSessionStore.cjs +1 -1
- package/dist/cjs/stream.cjs +1 -1
- package/dist/cjs/summarization/index.cjs +1 -0
- package/dist/cjs/summarization/index.cjs.map +1 -1
- package/dist/cjs/summarization/node.cjs +58 -85
- package/dist/cjs/summarization/node.cjs.map +1 -1
- package/dist/cjs/summarization/semanticIndex.cjs +366 -0
- package/dist/cjs/summarization/semanticIndex.cjs.map +1 -0
- package/dist/cjs/summarization/shared.cjs +83 -0
- package/dist/cjs/summarization/shared.cjs.map +1 -0
- package/dist/cjs/tools/ToolNode.cjs +5 -5
- package/dist/cjs/tools/local/CompileCheckTool.cjs +1 -1
- package/dist/cjs/tools/local/LocalCodingTools.cjs +1 -1
- package/dist/cjs/tools/local/LocalExecutionEngine.cjs +1 -1
- package/dist/cjs/tools/runStepResume.cjs +1 -1
- package/dist/cjs/tools/runStepResume.cjs.map +1 -1
- package/dist/cjs/tools/subagent/SubagentExecutor.cjs +8 -12
- package/dist/cjs/tools/subagent/SubagentExecutor.cjs.map +1 -1
- package/dist/cjs/tools/subagent/childGraphConfig.cjs +2 -1
- package/dist/cjs/tools/subagent/childGraphConfig.cjs.map +1 -1
- package/dist/cjs/utils/index.cjs +1 -1
- package/dist/cjs/utils/tokens.cjs +8 -1
- package/dist/cjs/utils/tokens.cjs.map +1 -1
- package/dist/esm/agents/AgentContext.mjs +26 -5
- package/dist/esm/agents/AgentContext.mjs.map +1 -1
- package/dist/esm/common/constants.mjs +11 -1
- package/dist/esm/common/constants.mjs.map +1 -1
- package/dist/esm/graphs/Graph.mjs +56 -11
- package/dist/esm/graphs/Graph.mjs.map +1 -1
- package/dist/esm/graphs/MultiAgentGraph.mjs +1 -1
- package/dist/esm/hooks/HookRegistry.mjs.map +1 -1
- package/dist/esm/hooks/executeHooks.mjs +8 -1
- package/dist/esm/hooks/executeHooks.mjs.map +1 -1
- package/dist/esm/hooks/index.mjs +2 -1
- package/dist/esm/hooks/index.mjs.map +1 -1
- package/dist/esm/hooks/types.mjs +1 -0
- package/dist/esm/hooks/types.mjs.map +1 -1
- package/dist/esm/langfuse.mjs +43 -3
- package/dist/esm/langfuse.mjs.map +1 -1
- package/dist/esm/langfuseTraceShaping.mjs +75 -0
- package/dist/esm/langfuseTraceShaping.mjs.map +1 -1
- package/dist/esm/llm/bedrock/toolCache.mjs +1 -1
- package/dist/esm/llm/fake.mjs +9 -5
- package/dist/esm/llm/fake.mjs.map +1 -1
- package/dist/esm/llm/invoke.mjs +3 -7
- package/dist/esm/llm/invoke.mjs.map +1 -1
- package/dist/esm/llm/openrouter/toolCache.mjs +1 -1
- package/dist/esm/llm/prepareProviderRequest.mjs +2 -2
- package/dist/esm/llm/providers.mjs +1 -1
- package/dist/esm/llm/request.mjs +6 -1
- package/dist/esm/llm/request.mjs.map +1 -1
- package/dist/esm/llm/truncation.mjs +1 -1
- package/dist/esm/main.mjs +15 -14
- package/dist/esm/messages/format.mjs +375 -3
- package/dist/esm/messages/format.mjs.map +1 -1
- package/dist/esm/messages/index.mjs +1 -1
- package/dist/esm/messages/prune.mjs +1 -1
- package/dist/esm/run.mjs +217 -52
- package/dist/esm/run.mjs.map +1 -1
- package/dist/esm/session/AgentSession.mjs +1 -1
- package/dist/esm/session/JsonlSessionStore.mjs +1 -1
- package/dist/esm/stream.mjs +1 -1
- package/dist/esm/summarization/index.mjs +1 -0
- package/dist/esm/summarization/index.mjs.map +1 -1
- package/dist/esm/summarization/node.mjs +57 -84
- package/dist/esm/summarization/node.mjs.map +1 -1
- package/dist/esm/summarization/semanticIndex.mjs +363 -0
- package/dist/esm/summarization/semanticIndex.mjs.map +1 -0
- package/dist/esm/summarization/shared.mjs +79 -0
- package/dist/esm/summarization/shared.mjs.map +1 -0
- package/dist/esm/tools/ToolNode.mjs +5 -5
- package/dist/esm/tools/local/CompileCheckTool.mjs +1 -1
- package/dist/esm/tools/local/LocalCodingTools.mjs +1 -1
- package/dist/esm/tools/local/LocalExecutionEngine.mjs +1 -1
- package/dist/esm/tools/runStepResume.mjs +1 -1
- package/dist/esm/tools/runStepResume.mjs.map +1 -1
- package/dist/esm/tools/subagent/SubagentExecutor.mjs +8 -12
- package/dist/esm/tools/subagent/SubagentExecutor.mjs.map +1 -1
- package/dist/esm/tools/subagent/childGraphConfig.mjs +2 -1
- package/dist/esm/tools/subagent/childGraphConfig.mjs.map +1 -1
- package/dist/esm/utils/index.mjs +1 -1
- package/dist/esm/utils/tokens.mjs +8 -2
- package/dist/esm/utils/tokens.mjs.map +1 -1
- package/dist/types/agents/AgentContext.d.ts +27 -1
- package/dist/types/common/constants.d.ts +17 -0
- package/dist/types/graphs/Graph.d.ts +24 -0
- package/dist/types/hooks/executeHooks.d.ts +5 -2
- package/dist/types/hooks/index.d.ts +7 -2
- package/dist/types/hooks/types.d.ts +45 -7
- package/dist/types/langfuse.d.ts +3 -1
- package/dist/types/llm/fake.d.ts +12 -2
- package/dist/types/llm/request.d.ts +10 -0
- package/dist/types/messages/format.d.ts +17 -2
- package/dist/types/run.d.ts +19 -6
- package/dist/types/summarization/index.d.ts +6 -0
- package/dist/types/summarization/node.d.ts +0 -4
- package/dist/types/summarization/semanticIndex.d.ts +21 -0
- package/dist/types/summarization/shared.d.ts +25 -0
- package/dist/types/types/graph.d.ts +9 -1
- package/dist/types/types/run.d.ts +8 -0
- package/dist/types/types/stream.d.ts +6 -0
- package/dist/types/types/summarize.d.ts +44 -0
- package/dist/types/utils/tokens.d.ts +9 -0
- package/package.json +2 -1
- package/src/agents/AgentContext.ts +63 -6
- package/src/common/constants.ts +19 -0
- package/src/graphs/Graph.ts +86 -8
- package/src/hooks/HookRegistry.ts +3 -1
- package/src/hooks/executeHooks.ts +20 -2
- package/src/hooks/index.ts +12 -1
- package/src/hooks/types.ts +47 -4
- package/src/langfuse.ts +70 -0
- package/src/langfuseTraceShaping.ts +94 -0
- package/src/llm/fake.ts +35 -4
- package/src/llm/invoke.ts +13 -29
- package/src/llm/request.ts +24 -0
- package/src/messages/format.ts +839 -5
- package/src/run.ts +393 -187
- package/src/summarization/index.ts +11 -0
- package/src/summarization/node.ts +202 -158
- package/src/summarization/semanticIndex.ts +662 -0
- package/src/summarization/shared.ts +130 -0
- package/src/tools/runStepResume.ts +9 -0
- package/src/tools/subagent/SubagentExecutor.ts +4 -27
- package/src/tools/subagent/childGraphConfig.ts +3 -0
- package/src/types/graph.ts +9 -0
- package/src/types/run.ts +8 -0
- package/src/types/stream.ts +6 -0
- package/src/types/summarize.ts +57 -0
- package/src/utils/tokens.ts +31 -8
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
const require_constants = require("../common/constants.cjs");
|
|
1
2
|
require("../common/enum.cjs");
|
|
2
3
|
require("../common/index.cjs");
|
|
3
4
|
const require_truncation = require("../utils/truncation.cjs");
|
|
@@ -5,6 +6,7 @@ const require_toolContent = require("../utils/toolContent.cjs");
|
|
|
5
6
|
const require_provenance = require("./provenance.cjs");
|
|
6
7
|
const require_langchain = require("./langchain.cjs");
|
|
7
8
|
const require_toolResultTypes = require("./toolResultTypes.cjs");
|
|
9
|
+
const require_semanticIndex = require("../summarization/semanticIndex.cjs");
|
|
8
10
|
const require_message_inputs = require("../llm/anthropic/utils/message_inputs.cjs");
|
|
9
11
|
const require_content = require("./content.cjs");
|
|
10
12
|
const require_events = require("../utils/events.cjs");
|
|
@@ -140,6 +142,356 @@ function getToolUseId(part) {
|
|
|
140
142
|
if (!("tool_use_id" in part) || typeof part.tool_use_id !== "string") return;
|
|
141
143
|
return part.tool_use_id;
|
|
142
144
|
}
|
|
145
|
+
const DERIVED_SEMANTIC_HEAD_LIMIT = require_constants.COMPACTION_SEMANTIC_INDEX_LIMITS.maxInputEntries / 4;
|
|
146
|
+
const DERIVED_SEMANTIC_TAIL_LIMIT = require_constants.COMPACTION_SEMANTIC_INDEX_LIMITS.maxInputEntries / 2;
|
|
147
|
+
const DERIVED_SEMANTIC_TYPE_EDGE_LIMIT = require_constants.COMPACTION_SEMANTIC_INDEX_LIMITS.maxInputEntries / 32;
|
|
148
|
+
const DERIVED_SEMANTIC_TYPE_HASH = Object.freeze({
|
|
149
|
+
tool_intent: 1,
|
|
150
|
+
tool_outcome: 2,
|
|
151
|
+
activity_phase: 3,
|
|
152
|
+
reasoning_label: 4
|
|
153
|
+
});
|
|
154
|
+
function createDerivedCompactionSemanticIndexCollector(baseSnapshot) {
|
|
155
|
+
const collector = {
|
|
156
|
+
entries: [],
|
|
157
|
+
entryCount: 0,
|
|
158
|
+
baseEntryCount: 0,
|
|
159
|
+
omittedBaseEntryCount: 0
|
|
160
|
+
};
|
|
161
|
+
let baseEntries;
|
|
162
|
+
let serializedProvidedEntryCount;
|
|
163
|
+
try {
|
|
164
|
+
baseEntries = baseSnapshot?.entries;
|
|
165
|
+
serializedProvidedEntryCount = baseSnapshot?.providedEntryCount;
|
|
166
|
+
} catch {
|
|
167
|
+
return collector;
|
|
168
|
+
}
|
|
169
|
+
const snapshot = require_semanticIndex.snapshotCompactionSemanticIndex(baseEntries);
|
|
170
|
+
if (snapshot == null) return collector;
|
|
171
|
+
const snapshotProvidedEntryCount = require_semanticIndex.getCompactionSemanticIndexProvidedEntryCount(snapshot);
|
|
172
|
+
collector.omittedBaseEntryCount = Math.max(0, (typeof serializedProvidedEntryCount === "number" && Number.isSafeInteger(serializedProvidedEntryCount) && serializedProvidedEntryCount >= snapshotProvidedEntryCount && serializedProvidedEntryCount != null ? serializedProvidedEntryCount : snapshotProvidedEntryCount) - snapshot.length);
|
|
173
|
+
for (let index = 0; index < snapshot.length; index++) appendDerivedCompactionSemanticEntry(collector, snapshot[index], true);
|
|
174
|
+
collector.baseEntryCount = collector.entryCount;
|
|
175
|
+
return collector;
|
|
176
|
+
}
|
|
177
|
+
function createCompactionSemanticCoverageState() {
|
|
178
|
+
return {
|
|
179
|
+
head: [],
|
|
180
|
+
tail: {
|
|
181
|
+
entries: [],
|
|
182
|
+
cursor: 0
|
|
183
|
+
},
|
|
184
|
+
latestByIdentityHash: /* @__PURE__ */ new Map(),
|
|
185
|
+
typeCoverage: /* @__PURE__ */ new Map()
|
|
186
|
+
};
|
|
187
|
+
}
|
|
188
|
+
function getDerivedCompactionSemanticLocalId(entry) {
|
|
189
|
+
if (entry.type === "reasoning_label") return entry.reasoningStepId.trim();
|
|
190
|
+
if (entry.type !== "activity_phase") return entry.toolCallId.trim();
|
|
191
|
+
return "";
|
|
192
|
+
}
|
|
193
|
+
function hashDerivedCompactionSemanticIdentityString(initialHash, value) {
|
|
194
|
+
let hash = initialHash;
|
|
195
|
+
for (let index = 0; index < value.length; index++) {
|
|
196
|
+
hash ^= value.charCodeAt(index);
|
|
197
|
+
hash = Math.imul(hash, 16777619);
|
|
198
|
+
}
|
|
199
|
+
return hash;
|
|
200
|
+
}
|
|
201
|
+
function hashDerivedCompactionSemanticIdentity(entry) {
|
|
202
|
+
let hash = 2166136261 ^ DERIVED_SEMANTIC_TYPE_HASH[entry.type];
|
|
203
|
+
hash = Math.imul(hash, 16777619);
|
|
204
|
+
hash = hashDerivedCompactionSemanticIdentityString(hash, entry.sourceMessageId.trim());
|
|
205
|
+
hash ^= entry.sourceContentIndex;
|
|
206
|
+
hash = Math.imul(hash, 16777619);
|
|
207
|
+
hash = hashDerivedCompactionSemanticIdentityString(hash, getDerivedCompactionSemanticLocalId(entry));
|
|
208
|
+
return hash >>> 0;
|
|
209
|
+
}
|
|
210
|
+
function entriesShareDerivedCompactionSemanticIdentity(left, right) {
|
|
211
|
+
return left.type === right.type && left.sourceMessageId.trim() === right.sourceMessageId.trim() && left.sourceContentIndex === right.sourceContentIndex && getDerivedCompactionSemanticLocalId(left) === getDerivedCompactionSemanticLocalId(right);
|
|
212
|
+
}
|
|
213
|
+
function entriesHaveConflictingSemanticState(left, right) {
|
|
214
|
+
return left.status !== right.status || left.redacted === true !== (right.redacted === true) || left.text.replace(/\s+/g, " ").trim() !== right.text.replace(/\s+/g, " ").trim();
|
|
215
|
+
}
|
|
216
|
+
function findCompactionSemanticRevisionFloor(collector, entry, identityHash) {
|
|
217
|
+
return collector.baseRevisionFloors?.get(identityHash)?.find((floor) => entriesShareDerivedCompactionSemanticIdentity(floor.entry, entry));
|
|
218
|
+
}
|
|
219
|
+
function seedCompactionSemanticRevisionFloor(collector, entry, order) {
|
|
220
|
+
collector.baseRevisionFloors ??= /* @__PURE__ */ new Map();
|
|
221
|
+
const identityHash = hashDerivedCompactionSemanticIdentity(entry);
|
|
222
|
+
const existing = findCompactionSemanticRevisionFloor(collector, entry, identityHash);
|
|
223
|
+
if (existing != null) {
|
|
224
|
+
if (entry.revision > existing.entry.revision) {
|
|
225
|
+
existing.entry = entry;
|
|
226
|
+
existing.order = order;
|
|
227
|
+
}
|
|
228
|
+
return;
|
|
229
|
+
}
|
|
230
|
+
const floor = {
|
|
231
|
+
entry,
|
|
232
|
+
order
|
|
233
|
+
};
|
|
234
|
+
const bucket = collector.baseRevisionFloors.get(identityHash);
|
|
235
|
+
if (bucket == null) {
|
|
236
|
+
collector.baseRevisionFloors.set(identityHash, [floor]);
|
|
237
|
+
return;
|
|
238
|
+
}
|
|
239
|
+
bucket.push(floor);
|
|
240
|
+
}
|
|
241
|
+
function updateCompactionSemanticRevisionFloor(collector, orderedEntry) {
|
|
242
|
+
const floor = findCompactionSemanticRevisionFloor(collector, orderedEntry.entry, orderedEntry.identityHash);
|
|
243
|
+
if (floor == null || orderedEntry.entry.revision <= floor.entry.revision) return;
|
|
244
|
+
floor.entry = orderedEntry.entry;
|
|
245
|
+
floor.order = orderedEntry.order;
|
|
246
|
+
}
|
|
247
|
+
function shouldRejectCompactionSemanticEntryBelowBaseFloor(collector, orderedEntry) {
|
|
248
|
+
const floor = findCompactionSemanticRevisionFloor(collector, orderedEntry.entry, orderedEntry.identityHash);
|
|
249
|
+
if (floor == null || orderedEntry.order === floor.order) return false;
|
|
250
|
+
if ((collector.coverage?.latestByIdentityHash.get(orderedEntry.identityHash))?.find(({ entry }) => entriesShareDerivedCompactionSemanticIdentity(entry, orderedEntry.entry)) != null) return false;
|
|
251
|
+
return orderedEntry.entry.revision <= floor.entry.revision;
|
|
252
|
+
}
|
|
253
|
+
function retainCompactionSemanticEntry(entry) {
|
|
254
|
+
entry.retentionCount++;
|
|
255
|
+
}
|
|
256
|
+
function releaseCompactionSemanticEntry(coverage, entry) {
|
|
257
|
+
entry.retentionCount--;
|
|
258
|
+
if (entry.retentionCount === 0 && coverage.latestByIdentityHash.has(entry.identityHash)) {
|
|
259
|
+
const bucket = coverage.latestByIdentityHash.get(entry.identityHash);
|
|
260
|
+
if (bucket == null) return;
|
|
261
|
+
const entryIndex = bucket.indexOf(entry);
|
|
262
|
+
if (entryIndex >= 0) bucket.splice(entryIndex, 1);
|
|
263
|
+
if (bucket.length === 0) coverage.latestByIdentityHash.delete(entry.identityHash);
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
function appendCompactionSemanticEntryRing(coverage, ring, entry, limit) {
|
|
267
|
+
if (ring.entries.length < limit) {
|
|
268
|
+
ring.entries.push(entry);
|
|
269
|
+
retainCompactionSemanticEntry(entry);
|
|
270
|
+
return;
|
|
271
|
+
}
|
|
272
|
+
releaseCompactionSemanticEntry(coverage, ring.entries[ring.cursor]);
|
|
273
|
+
ring.entries[ring.cursor] = entry;
|
|
274
|
+
retainCompactionSemanticEntry(entry);
|
|
275
|
+
ring.cursor = (ring.cursor + 1) % limit;
|
|
276
|
+
}
|
|
277
|
+
function renewCompactionSemanticEntryRing(coverage, ring, entry, limit) {
|
|
278
|
+
const existingIndex = ring.entries.indexOf(entry);
|
|
279
|
+
if (existingIndex < 0) {
|
|
280
|
+
appendCompactionSemanticEntryRing(coverage, ring, entry, limit);
|
|
281
|
+
return;
|
|
282
|
+
}
|
|
283
|
+
if (ring.entries.length < limit) {
|
|
284
|
+
ring.entries.splice(existingIndex, 1);
|
|
285
|
+
ring.entries.push(entry);
|
|
286
|
+
return;
|
|
287
|
+
}
|
|
288
|
+
const newestIndex = (ring.cursor + ring.entries.length - 1) % ring.entries.length;
|
|
289
|
+
let currentIndex = existingIndex;
|
|
290
|
+
for (let offset = 0; currentIndex !== newestIndex && offset < ring.entries.length; offset++) {
|
|
291
|
+
const nextIndex = (currentIndex + 1) % ring.entries.length;
|
|
292
|
+
ring.entries[currentIndex] = ring.entries[nextIndex];
|
|
293
|
+
currentIndex = nextIndex;
|
|
294
|
+
}
|
|
295
|
+
ring.entries[newestIndex] = entry;
|
|
296
|
+
}
|
|
297
|
+
function renewCoverageBalancedCompactionSemanticEntry(coverage, entry) {
|
|
298
|
+
renewCompactionSemanticEntryRing(coverage, coverage.tail, entry, DERIVED_SEMANTIC_TAIL_LIMIT);
|
|
299
|
+
const typeCoverage = coverage.typeCoverage.get(entry.entry.type);
|
|
300
|
+
if (typeCoverage == null) return;
|
|
301
|
+
renewCompactionSemanticEntryRing(coverage, typeCoverage.tail, entry, DERIVED_SEMANTIC_TYPE_EDGE_LIMIT);
|
|
302
|
+
}
|
|
303
|
+
function appendDerivedCompactionSemanticEntry(collector, entry, baseEntry = false) {
|
|
304
|
+
let boundedEntry = entry;
|
|
305
|
+
if (entry.status === "pending") boundedEntry = {
|
|
306
|
+
...entry,
|
|
307
|
+
text: ""
|
|
308
|
+
};
|
|
309
|
+
else if (entry.text.length > require_constants.COMPACTION_SEMANTIC_INDEX_LIMITS.maxInputTextChars) boundedEntry = {
|
|
310
|
+
...entry,
|
|
311
|
+
text: "",
|
|
312
|
+
redacted: true
|
|
313
|
+
};
|
|
314
|
+
const order = collector.entryCount;
|
|
315
|
+
collector.entryCount++;
|
|
316
|
+
if (collector.coverage == null && collector.entries.length < require_constants.COMPACTION_SEMANTIC_INDEX_LIMITS.maxInputEntries) {
|
|
317
|
+
collector.entries.push(boundedEntry);
|
|
318
|
+
if (!baseEntry && collector.baseRevisionFloors != null) updateCompactionSemanticRevisionFloor(collector, {
|
|
319
|
+
entry: boundedEntry,
|
|
320
|
+
identityHash: hashDerivedCompactionSemanticIdentity(boundedEntry),
|
|
321
|
+
order,
|
|
322
|
+
retentionCount: 0
|
|
323
|
+
});
|
|
324
|
+
return;
|
|
325
|
+
}
|
|
326
|
+
const orderedEntry = {
|
|
327
|
+
entry: boundedEntry,
|
|
328
|
+
identityHash: hashDerivedCompactionSemanticIdentity(boundedEntry),
|
|
329
|
+
order,
|
|
330
|
+
retentionCount: 0
|
|
331
|
+
};
|
|
332
|
+
if (collector.coverage == null) {
|
|
333
|
+
for (let order = 0; order < collector.baseEntryCount; order++) seedCompactionSemanticRevisionFloor(collector, collector.entries[order], order);
|
|
334
|
+
collector.coverage = createCompactionSemanticCoverageState();
|
|
335
|
+
for (let order = 0; order < collector.entries.length; order++) {
|
|
336
|
+
const bufferedEntry = {
|
|
337
|
+
entry: collector.entries[order],
|
|
338
|
+
identityHash: hashDerivedCompactionSemanticIdentity(collector.entries[order]),
|
|
339
|
+
order,
|
|
340
|
+
retentionCount: 0
|
|
341
|
+
};
|
|
342
|
+
if (shouldRejectCompactionSemanticEntryBelowBaseFloor(collector, bufferedEntry)) continue;
|
|
343
|
+
appendCoverageBalancedCompactionSemanticEntry(collector.coverage, bufferedEntry);
|
|
344
|
+
if (order >= collector.baseEntryCount) updateCompactionSemanticRevisionFloor(collector, bufferedEntry);
|
|
345
|
+
}
|
|
346
|
+
collector.entries = [];
|
|
347
|
+
}
|
|
348
|
+
if (!baseEntry && shouldRejectCompactionSemanticEntryBelowBaseFloor(collector, orderedEntry)) return;
|
|
349
|
+
appendCoverageBalancedCompactionSemanticEntry(collector.coverage, orderedEntry);
|
|
350
|
+
if (!baseEntry) updateCompactionSemanticRevisionFloor(collector, orderedEntry);
|
|
351
|
+
}
|
|
352
|
+
function appendCoverageBalancedCompactionSemanticEntry(coverage, orderedEntry) {
|
|
353
|
+
const identityHash = orderedEntry.identityHash;
|
|
354
|
+
const identityBucket = coverage.latestByIdentityHash.get(identityHash);
|
|
355
|
+
const current = identityBucket?.find(({ entry }) => entriesShareDerivedCompactionSemanticIdentity(entry, orderedEntry.entry));
|
|
356
|
+
if (current != null) {
|
|
357
|
+
if (orderedEntry.entry.revision < current.entry.revision) return;
|
|
358
|
+
if (orderedEntry.entry.revision === current.entry.revision) {
|
|
359
|
+
if (entriesHaveConflictingSemanticState(current.entry, orderedEntry.entry)) {
|
|
360
|
+
current.entry = {
|
|
361
|
+
...current.entry,
|
|
362
|
+
text: "",
|
|
363
|
+
redacted: true
|
|
364
|
+
};
|
|
365
|
+
current.order = orderedEntry.order;
|
|
366
|
+
renewCoverageBalancedCompactionSemanticEntry(coverage, current);
|
|
367
|
+
}
|
|
368
|
+
return;
|
|
369
|
+
}
|
|
370
|
+
current.entry = orderedEntry.entry;
|
|
371
|
+
current.order = orderedEntry.order;
|
|
372
|
+
renewCoverageBalancedCompactionSemanticEntry(coverage, current);
|
|
373
|
+
return;
|
|
374
|
+
}
|
|
375
|
+
if (identityBucket == null) coverage.latestByIdentityHash.set(identityHash, [orderedEntry]);
|
|
376
|
+
else identityBucket.push(orderedEntry);
|
|
377
|
+
if (coverage.head.length < DERIVED_SEMANTIC_HEAD_LIMIT) {
|
|
378
|
+
coverage.head.push(orderedEntry);
|
|
379
|
+
retainCompactionSemanticEntry(orderedEntry);
|
|
380
|
+
}
|
|
381
|
+
appendCompactionSemanticEntryRing(coverage, coverage.tail, orderedEntry, DERIVED_SEMANTIC_TAIL_LIMIT);
|
|
382
|
+
let typeCoverage = coverage.typeCoverage.get(orderedEntry.entry.type);
|
|
383
|
+
if (typeCoverage == null) {
|
|
384
|
+
typeCoverage = {
|
|
385
|
+
head: [],
|
|
386
|
+
tail: {
|
|
387
|
+
entries: [],
|
|
388
|
+
cursor: 0
|
|
389
|
+
}
|
|
390
|
+
};
|
|
391
|
+
coverage.typeCoverage.set(orderedEntry.entry.type, typeCoverage);
|
|
392
|
+
}
|
|
393
|
+
if (typeCoverage.head.length < DERIVED_SEMANTIC_TYPE_EDGE_LIMIT) {
|
|
394
|
+
typeCoverage.head.push(orderedEntry);
|
|
395
|
+
retainCompactionSemanticEntry(orderedEntry);
|
|
396
|
+
}
|
|
397
|
+
appendCompactionSemanticEntryRing(coverage, typeCoverage.tail, orderedEntry, DERIVED_SEMANTIC_TYPE_EDGE_LIMIT);
|
|
398
|
+
}
|
|
399
|
+
function finalizeDerivedCompactionSemanticIndexSnapshot(collector) {
|
|
400
|
+
if (collector == null || collector.entryCount === 0 && collector.omittedBaseEntryCount === 0) return;
|
|
401
|
+
const providedEntryCount = Math.min(Number.MAX_SAFE_INTEGER, collector.entryCount + collector.omittedBaseEntryCount);
|
|
402
|
+
if (collector.coverage == null) {
|
|
403
|
+
require_semanticIndex.setCompactionSemanticIndexProvidedEntryCount(collector.entries, providedEntryCount);
|
|
404
|
+
return {
|
|
405
|
+
entries: collector.entries,
|
|
406
|
+
providedEntryCount
|
|
407
|
+
};
|
|
408
|
+
}
|
|
409
|
+
const retained = /* @__PURE__ */ new Map();
|
|
410
|
+
const retain = (entries) => {
|
|
411
|
+
for (const { entry, order } of entries) retained.set(order, entry);
|
|
412
|
+
};
|
|
413
|
+
retain(collector.coverage.head);
|
|
414
|
+
retain(collector.coverage.tail.entries);
|
|
415
|
+
for (const typeCoverage of collector.coverage.typeCoverage.values()) {
|
|
416
|
+
retain(typeCoverage.head);
|
|
417
|
+
retain(typeCoverage.tail.entries);
|
|
418
|
+
}
|
|
419
|
+
const result = [...retained.entries()].sort(([left], [right]) => left - right).map(([, entry]) => entry);
|
|
420
|
+
require_semanticIndex.setCompactionSemanticIndexProvidedEntryCount(result, providedEntryCount);
|
|
421
|
+
return {
|
|
422
|
+
entries: result,
|
|
423
|
+
providedEntryCount
|
|
424
|
+
};
|
|
425
|
+
}
|
|
426
|
+
function collectCompactionSemanticEntriesFromPart(collector, part, sourceMessageId, sourceContentIndex, retainedSourceContentStart, retainedSourceContentEnd, intentToolNames, parsedToolInput) {
|
|
427
|
+
if (sourceMessageId.length > require_constants.COMPACTION_SEMANTIC_INDEX_LIMITS.maxIdentityChars) return;
|
|
428
|
+
if (part.type === "tool_call") {
|
|
429
|
+
if (sourceContentIndex < 0 || sourceContentIndex > require_constants.COMPACTION_SEMANTIC_INDEX_LIMITS.maxSourceContentIndex) return;
|
|
430
|
+
const toolCall = part.tool_call;
|
|
431
|
+
const toolCallId = toolCall?.id;
|
|
432
|
+
if (typeof toolCallId !== "string" || toolCallId === "" || toolCallId.length > require_constants.COMPACTION_SEMANTIC_INDEX_LIMITS.maxIdentityChars) return;
|
|
433
|
+
if (typeof toolCall.name === "string" && intentToolNames?.has(toolCall.name) === true) {
|
|
434
|
+
const intent = parsedToolInput != null && typeof parsedToolInput === "object" && !Array.isArray(parsedToolInput) ? parsedToolInput.intent : void 0;
|
|
435
|
+
if (typeof intent === "string" && intent !== "") appendDerivedCompactionSemanticEntry(collector, {
|
|
436
|
+
type: "tool_intent",
|
|
437
|
+
sourceMessageId,
|
|
438
|
+
sourceContentIndex,
|
|
439
|
+
revision: 0,
|
|
440
|
+
status: "committed",
|
|
441
|
+
text: intent,
|
|
442
|
+
toolCallId
|
|
443
|
+
});
|
|
444
|
+
}
|
|
445
|
+
if (typeof toolCall.outcome === "string" && toolCall.outcome !== "") appendDerivedCompactionSemanticEntry(collector, {
|
|
446
|
+
type: "tool_outcome",
|
|
447
|
+
sourceMessageId,
|
|
448
|
+
sourceContentIndex,
|
|
449
|
+
revision: 0,
|
|
450
|
+
status: "committed",
|
|
451
|
+
text: toolCall.outcome,
|
|
452
|
+
toolCallId
|
|
453
|
+
});
|
|
454
|
+
return;
|
|
455
|
+
}
|
|
456
|
+
if (part.type === "think") {
|
|
457
|
+
if (sourceContentIndex < 0 || sourceContentIndex > require_constants.COMPACTION_SEMANTIC_INDEX_LIMITS.maxSourceContentIndex) return;
|
|
458
|
+
const reasoningStepId = part.reasoning_label_step_id;
|
|
459
|
+
const revision = part.reasoning_label_revision;
|
|
460
|
+
const status = part.reasoning_label_status;
|
|
461
|
+
const text = part.reasoning_label;
|
|
462
|
+
if (typeof reasoningStepId !== "string" || reasoningStepId === "" || reasoningStepId.length > require_constants.COMPACTION_SEMANTIC_INDEX_LIMITS.maxIdentityChars || typeof revision !== "number" || !Number.isSafeInteger(revision) || revision < 0) return;
|
|
463
|
+
const malformedState = status !== "complete" && status !== "streaming" || typeof text !== "string";
|
|
464
|
+
appendDerivedCompactionSemanticEntry(collector, {
|
|
465
|
+
type: "reasoning_label",
|
|
466
|
+
sourceMessageId,
|
|
467
|
+
sourceContentIndex,
|
|
468
|
+
revision,
|
|
469
|
+
status: status === "streaming" || malformedState ? "pending" : "committed",
|
|
470
|
+
text: typeof text === "string" ? text : "",
|
|
471
|
+
reasoningStepId,
|
|
472
|
+
...malformedState ? { redacted: true } : {}
|
|
473
|
+
});
|
|
474
|
+
return;
|
|
475
|
+
}
|
|
476
|
+
if (part.type !== "activity_label" || part.activity_label_type !== "phase") return;
|
|
477
|
+
const activitySourceContentIndex = part.activity_start_index;
|
|
478
|
+
if (typeof activitySourceContentIndex !== "number" || !Number.isSafeInteger(activitySourceContentIndex) || activitySourceContentIndex < retainedSourceContentStart || activitySourceContentIndex >= retainedSourceContentEnd || activitySourceContentIndex > require_constants.COMPACTION_SEMANTIC_INDEX_LIMITS.maxSourceContentIndex) return;
|
|
479
|
+
const revision = part.activity_label_revision;
|
|
480
|
+
if (revision !== void 0 && (typeof revision !== "number" || !Number.isSafeInteger(revision) || revision < 0)) return;
|
|
481
|
+
const malformedPending = part.pending !== void 0 && typeof part.pending !== "boolean";
|
|
482
|
+
const malformedText = typeof part.activity_label !== "string";
|
|
483
|
+
const malformedState = malformedPending || malformedText;
|
|
484
|
+
const text = typeof part.activity_label === "string" ? part.activity_label : "";
|
|
485
|
+
appendDerivedCompactionSemanticEntry(collector, {
|
|
486
|
+
type: "activity_phase",
|
|
487
|
+
sourceMessageId,
|
|
488
|
+
sourceContentIndex: activitySourceContentIndex,
|
|
489
|
+
revision: revision ?? 0,
|
|
490
|
+
status: part.pending === true || malformedPending ? "pending" : "committed",
|
|
491
|
+
text,
|
|
492
|
+
...malformedState ? { redacted: true } : {}
|
|
493
|
+
});
|
|
494
|
+
}
|
|
143
495
|
function collectTrustedToolResultSourceContentPartIndices(content, sourceContentPartOffset) {
|
|
144
496
|
if (content == null) return;
|
|
145
497
|
let calls;
|
|
@@ -295,6 +647,10 @@ function formatAssistantMessage(message, options) {
|
|
|
295
647
|
const emittedServerToolUseIds = /* @__PURE__ */ new Set();
|
|
296
648
|
const pendingServerToolUses = /* @__PURE__ */ new Map();
|
|
297
649
|
const shouldPreserveReasoningContent = options?.preserveReasoningContent === true;
|
|
650
|
+
const compactionSemanticIndex = options?.compactionSemanticIndex;
|
|
651
|
+
const semanticSourceMessageId = compactionSemanticIndex != null && options?.sourceMessageId != null && options.sourceMessageId.length <= require_constants.COMPACTION_SEMANTIC_INDEX_LIMITS.maxIdentityChars ? options.sourceMessageId : void 0;
|
|
652
|
+
const retainedSourceContentStart = options?.sourceContentPartOffset ?? 0;
|
|
653
|
+
const retainedSourceContentEnd = options?.retainedSourceContentEnd ?? retainedSourceContentStart;
|
|
298
654
|
const serverToolResultIds = /* @__PURE__ */ new Set();
|
|
299
655
|
const preferredToolCallParts = /* @__PURE__ */ new Map();
|
|
300
656
|
const takePendingReasoningContent = () => {
|
|
@@ -353,6 +709,7 @@ function formatAssistantMessage(message, options) {
|
|
|
353
709
|
for (let partIndex = 0; partIndex < contentParts.length; partIndex++) {
|
|
354
710
|
const part = contentParts[partIndex];
|
|
355
711
|
if (part == null) continue;
|
|
712
|
+
if (part.type === "activity_label") continue;
|
|
356
713
|
const isTrustedResult = isTrustedServerToolResult(part, getSourcePartIndices(partIndex), options?.toolSourceContentPartIndices);
|
|
357
714
|
if (isTrustedResult) {
|
|
358
715
|
trustedServerToolResultPartIndices ??= /* @__PURE__ */ new Set();
|
|
@@ -374,6 +731,10 @@ function formatAssistantMessage(message, options) {
|
|
|
374
731
|
const part = contentParts[partIndex];
|
|
375
732
|
if (part == null) continue;
|
|
376
733
|
const sourcePartIndices = getSourcePartIndices(partIndex);
|
|
734
|
+
if (part.type === "activity_label") {
|
|
735
|
+
if (compactionSemanticIndex != null && semanticSourceMessageId != null && typeof sourcePartIndices === "number") collectCompactionSemanticEntriesFromPart(compactionSemanticIndex, part, semanticSourceMessageId, sourcePartIndices, retainedSourceContentStart, retainedSourceContentEnd, options?.intentToolNames);
|
|
736
|
+
continue;
|
|
737
|
+
}
|
|
377
738
|
const toolUseId = getToolUseId(part);
|
|
378
739
|
if (toolUseId != null) {
|
|
379
740
|
const isServerToolResult = trustedServerToolResultPartIndices?.has(partIndex) === true;
|
|
@@ -418,12 +779,14 @@ function formatAssistantMessage(message, options) {
|
|
|
418
779
|
if (options?.provider === "anthropic" && typeof _tool_call.id === "string" && _tool_call.id.startsWith("srvtoolu_")) {
|
|
419
780
|
if (!serverToolResultIds.has(_tool_call.id) && options.preserveUnpairedServerToolUses !== true) continue;
|
|
420
781
|
if (emittedServerToolUseIds.has(_tool_call.id) || pendingServerToolUses.has(_tool_call.id)) continue;
|
|
782
|
+
const serverToolInput = parseServerToolInput(_args);
|
|
783
|
+
if (compactionSemanticIndex != null && semanticSourceMessageId != null && typeof sourcePartIndices === "number") collectCompactionSemanticEntriesFromPart(compactionSemanticIndex, part, semanticSourceMessageId, sourcePartIndices, retainedSourceContentStart, retainedSourceContentEnd, options.intentToolNames, serverToolInput);
|
|
421
784
|
pendingServerToolUses.set(_tool_call.id, {
|
|
422
785
|
content: {
|
|
423
786
|
type: "server_tool_use",
|
|
424
787
|
id: _tool_call.id,
|
|
425
788
|
name: _tool_call.name,
|
|
426
|
-
input:
|
|
789
|
+
input: serverToolInput
|
|
427
790
|
},
|
|
428
791
|
sourceContentPartIndices: sourcePartIndices
|
|
429
792
|
});
|
|
@@ -442,6 +805,7 @@ function formatAssistantMessage(message, options) {
|
|
|
442
805
|
if (typeof _args === "string") args = { input: _args };
|
|
443
806
|
}
|
|
444
807
|
tool_call.args = args;
|
|
808
|
+
if (compactionSemanticIndex != null && semanticSourceMessageId != null && typeof sourcePartIndices === "number") collectCompactionSemanticEntriesFromPart(compactionSemanticIndex, part, semanticSourceMessageId, sourcePartIndices, retainedSourceContentStart, retainedSourceContentEnd, options?.intentToolNames, args);
|
|
445
809
|
if (options?.provider === "anthropic" && Array.isArray(lastAIMessage.content)) {
|
|
446
810
|
const content = lastAIMessage.content;
|
|
447
811
|
content.push({
|
|
@@ -461,6 +825,7 @@ function formatAssistantMessage(message, options) {
|
|
|
461
825
|
content: formatToolCallOutput(output)
|
|
462
826
|
}), "tool"), "tool", sourcePartIndices);
|
|
463
827
|
} else if (part.type === "think" || part.type === "thinking" || part.type === "reasoning" || part.type === "reasoning_content" || part.type === "redacted_thinking") {
|
|
828
|
+
if (part.type === "think" && compactionSemanticIndex != null && semanticSourceMessageId != null && typeof sourcePartIndices === "number") collectCompactionSemanticEntriesFromPart(compactionSemanticIndex, part, semanticSourceMessageId, sourcePartIndices, retainedSourceContentStart, retainedSourceContentEnd, options?.intentToolNames);
|
|
464
829
|
hasReasoning = true;
|
|
465
830
|
pendingReasoningContent += extractReasoningContent(part);
|
|
466
831
|
appendSourceContentPartIndices(pendingReasoningSourcePartIndices, sourcePartIndices);
|
|
@@ -487,7 +852,7 @@ function formatAssistantMessage(message, options) {
|
|
|
487
852
|
hasReasoning = false;
|
|
488
853
|
pendingReasoningContent = "";
|
|
489
854
|
pendingReasoningSourcePartIndices = [];
|
|
490
|
-
} else if (part.type === "error" || part.type === "agent_update" || part.type === "summary"
|
|
855
|
+
} else if (part.type === "error" || part.type === "agent_update" || part.type === "summary") continue;
|
|
491
856
|
else {
|
|
492
857
|
if (part.type === "text" && !getTextContent(part).trim()) continue;
|
|
493
858
|
currentContent.push(part);
|
|
@@ -764,6 +1129,7 @@ function extractSkillName(args) {
|
|
|
764
1129
|
const formatAgentMessages = (payload, indexTokenCountMap, tools, skills, options) => {
|
|
765
1130
|
const messages = [];
|
|
766
1131
|
const legacyContentEnabled = options?.legacyContent === true;
|
|
1132
|
+
const compactionSemanticIndexCollector = options?.compactionSemanticIndex != null ? createDerivedCompactionSemanticIndexCollector(options.compactionSemanticIndex.baseSnapshot) : void 0;
|
|
767
1133
|
const emitFormattedMessage = (message) => {
|
|
768
1134
|
if (legacyContentEnabled && require_content.isLegacyConvertible(message)) {
|
|
769
1135
|
const flattened = require_content.flattenLegacyContent(message.content);
|
|
@@ -914,9 +1280,12 @@ const formatAgentMessages = (payload, indexTokenCountMap, tools, skills, options
|
|
|
914
1280
|
}
|
|
915
1281
|
}
|
|
916
1282
|
const formattedMessages = formatAssistantMessage(processedMessage, {
|
|
1283
|
+
compactionSemanticIndex: compactionSemanticIndexCollector,
|
|
1284
|
+
intentToolNames: options?.compactionSemanticIndex?.intentToolNames,
|
|
917
1285
|
preserveUnpairedServerToolUses: i === payload.length - 1,
|
|
918
1286
|
preserveReasoningContent: options?.preserveReasoningContent ?? options?.provider === "deepseek",
|
|
919
1287
|
provider: options?.provider,
|
|
1288
|
+
retainedSourceContentEnd: sourceContentPartOffset + (Array.isArray(message.content) ? message.content.length : 0),
|
|
920
1289
|
sourceMessageId,
|
|
921
1290
|
sourceContentPartOffset,
|
|
922
1291
|
sourceContentPartIndices: processedSourceContentPartIndices,
|
|
@@ -1036,6 +1405,7 @@ const formatAgentMessages = (payload, indexTokenCountMap, tools, skills, options
|
|
|
1036
1405
|
updatedIndexTokenCountMap[resultIndices[lastIdx]] = tokenCount - distributed;
|
|
1037
1406
|
}
|
|
1038
1407
|
}
|
|
1408
|
+
const compactionSemanticIndexSnapshot = finalizeDerivedCompactionSemanticIndexSnapshot(compactionSemanticIndexCollector);
|
|
1039
1409
|
return {
|
|
1040
1410
|
messages,
|
|
1041
1411
|
indexTokenCountMap: indexTokenCountMap ? updatedIndexTokenCountMap : void 0,
|
|
@@ -1043,7 +1413,9 @@ const formatAgentMessages = (payload, indexTokenCountMap, tools, skills, options
|
|
|
1043
1413
|
text: summaryBoundary.text,
|
|
1044
1414
|
tokenCount: summaryBoundary.tokenCount
|
|
1045
1415
|
} : void 0,
|
|
1046
|
-
boundaryTokenAdjustment
|
|
1416
|
+
boundaryTokenAdjustment,
|
|
1417
|
+
compactionSemanticIndex: compactionSemanticIndexSnapshot?.entries,
|
|
1418
|
+
compactionSemanticIndexSnapshot
|
|
1047
1419
|
};
|
|
1048
1420
|
};
|
|
1049
1421
|
function shiftIndexTokenCountMap(indexTokenCountMap, instructionsTokenCount) {
|