@librechat/agents 3.7.6 → 3.7.8

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.
Files changed (92) hide show
  1. package/dist/cjs/agents/AgentContext.cjs +23 -3
  2. package/dist/cjs/agents/AgentContext.cjs.map +1 -1
  3. package/dist/cjs/common/constants.cjs +10 -0
  4. package/dist/cjs/common/constants.cjs.map +1 -1
  5. package/dist/cjs/eventActor/EventActorExecutor.cjs +305 -11
  6. package/dist/cjs/eventActor/EventActorExecutor.cjs.map +1 -1
  7. package/dist/cjs/graphs/Graph.cjs +16 -5
  8. package/dist/cjs/graphs/Graph.cjs.map +1 -1
  9. package/dist/cjs/langfuseTraceShaping.cjs +75 -0
  10. package/dist/cjs/langfuseTraceShaping.cjs.map +1 -1
  11. package/dist/cjs/llm/bedrock/toolCache.cjs +1 -1
  12. package/dist/cjs/llm/fake.cjs +9 -5
  13. package/dist/cjs/llm/fake.cjs.map +1 -1
  14. package/dist/cjs/llm/invoke.cjs +1 -1
  15. package/dist/cjs/llm/openrouter/toolCache.cjs +1 -1
  16. package/dist/cjs/llm/prepareProviderRequest.cjs +2 -2
  17. package/dist/cjs/llm/providers.cjs +1 -1
  18. package/dist/cjs/llm/truncation.cjs +1 -0
  19. package/dist/cjs/main.cjs +3 -2
  20. package/dist/cjs/messages/format.cjs +298 -3
  21. package/dist/cjs/messages/format.cjs.map +1 -1
  22. package/dist/cjs/messages/prune.cjs +1 -1
  23. package/dist/cjs/run.cjs +25 -15
  24. package/dist/cjs/run.cjs.map +1 -1
  25. package/dist/cjs/stream.cjs +1 -1
  26. package/dist/cjs/summarization/node.cjs +43 -9
  27. package/dist/cjs/summarization/node.cjs.map +1 -1
  28. package/dist/cjs/summarization/semanticIndex.cjs +362 -0
  29. package/dist/cjs/summarization/semanticIndex.cjs.map +1 -0
  30. package/dist/cjs/tools/subagent/childGraphConfig.cjs +2 -1
  31. package/dist/cjs/tools/subagent/childGraphConfig.cjs.map +1 -1
  32. package/dist/cjs/utils/index.cjs +1 -1
  33. package/dist/esm/agents/AgentContext.mjs +23 -3
  34. package/dist/esm/agents/AgentContext.mjs.map +1 -1
  35. package/dist/esm/common/constants.mjs +10 -1
  36. package/dist/esm/common/constants.mjs.map +1 -1
  37. package/dist/esm/eventActor/EventActorExecutor.mjs +305 -11
  38. package/dist/esm/eventActor/EventActorExecutor.mjs.map +1 -1
  39. package/dist/esm/graphs/Graph.mjs +16 -5
  40. package/dist/esm/graphs/Graph.mjs.map +1 -1
  41. package/dist/esm/langfuseTraceShaping.mjs +75 -0
  42. package/dist/esm/langfuseTraceShaping.mjs.map +1 -1
  43. package/dist/esm/llm/bedrock/toolCache.mjs +1 -1
  44. package/dist/esm/llm/fake.mjs +9 -5
  45. package/dist/esm/llm/fake.mjs.map +1 -1
  46. package/dist/esm/llm/invoke.mjs +1 -1
  47. package/dist/esm/llm/openrouter/toolCache.mjs +1 -1
  48. package/dist/esm/llm/prepareProviderRequest.mjs +2 -2
  49. package/dist/esm/llm/providers.mjs +1 -1
  50. package/dist/esm/llm/truncation.mjs +1 -1
  51. package/dist/esm/main.mjs +4 -4
  52. package/dist/esm/messages/format.mjs +298 -3
  53. package/dist/esm/messages/format.mjs.map +1 -1
  54. package/dist/esm/messages/prune.mjs +1 -1
  55. package/dist/esm/run.mjs +25 -15
  56. package/dist/esm/run.mjs.map +1 -1
  57. package/dist/esm/stream.mjs +1 -1
  58. package/dist/esm/summarization/node.mjs +43 -9
  59. package/dist/esm/summarization/node.mjs.map +1 -1
  60. package/dist/esm/summarization/semanticIndex.mjs +360 -0
  61. package/dist/esm/summarization/semanticIndex.mjs.map +1 -0
  62. package/dist/esm/tools/subagent/childGraphConfig.mjs +2 -1
  63. package/dist/esm/tools/subagent/childGraphConfig.mjs.map +1 -1
  64. package/dist/esm/utils/index.mjs +1 -1
  65. package/dist/types/agents/AgentContext.d.ts +27 -1
  66. package/dist/types/common/constants.d.ts +10 -0
  67. package/dist/types/eventActor/EventActorExecutor.d.ts +3 -1
  68. package/dist/types/eventActor/index.d.ts +1 -1
  69. package/dist/types/eventActor/types.d.ts +149 -5
  70. package/dist/types/graphs/Graph.d.ts +15 -0
  71. package/dist/types/llm/fake.d.ts +12 -2
  72. package/dist/types/messages/format.d.ts +10 -1
  73. package/dist/types/run.d.ts +17 -5
  74. package/dist/types/summarization/semanticIndex.d.ts +19 -0
  75. package/dist/types/types/graph.d.ts +9 -1
  76. package/dist/types/types/summarize.d.ts +38 -0
  77. package/package.json +2 -1
  78. package/src/agents/AgentContext.ts +61 -1
  79. package/src/common/constants.ts +11 -0
  80. package/src/eventActor/EventActorExecutor.ts +545 -16
  81. package/src/eventActor/index.ts +17 -0
  82. package/src/eventActor/types.ts +182 -3
  83. package/src/graphs/Graph.ts +41 -8
  84. package/src/langfuseTraceShaping.ts +94 -0
  85. package/src/llm/fake.ts +35 -4
  86. package/src/messages/format.ts +652 -4
  87. package/src/run.ts +36 -10
  88. package/src/summarization/node.ts +90 -33
  89. package/src/summarization/semanticIndex.ts +653 -0
  90. package/src/tools/subagent/childGraphConfig.ts +3 -0
  91. package/src/types/graph.ts +9 -0
  92. package/src/types/summarize.ts +50 -0
@@ -1,5 +1,5 @@
1
- const require_lazyRequire = require("../../lazyRequire.cjs");
2
1
  const require_cache = require("../../messages/cache.cjs");
2
+ const require_lazyRequire = require("../../lazyRequire.cjs");
3
3
  //#region src/llm/bedrock/toolCache.ts
4
4
  const convertToOpenAITool = (...args) => require_lazyRequire.requireInternalModule("llm/openai/index")._convertToOpenAITool(...args);
5
5
  const BEDROCK_TOOL_CACHE_MARKER = "__lc_bedrock_cache_point_after";
@@ -6,10 +6,11 @@ var FakeChatModel = class extends _langchain_core_utils_testing.FakeListChatMode
6
6
  splitStrategy;
7
7
  toolCalls = [];
8
8
  addedToolCalls = false;
9
+ finalChunkGenerationInfo;
9
10
  constructor({ responses, sleep, emitCustomEvent, splitStrategy = {
10
11
  type: "regex",
11
12
  value: /(?<=\s+)|(?=\s+)/
12
- }, toolCalls = [] }) {
13
+ }, toolCalls = [], finalChunkGenerationInfo }) {
13
14
  super({
14
15
  responses,
15
16
  sleep,
@@ -17,6 +18,7 @@ var FakeChatModel = class extends _langchain_core_utils_testing.FakeListChatMode
17
18
  });
18
19
  this.splitStrategy = splitStrategy;
19
20
  this.toolCalls = toolCalls;
21
+ this.finalChunkGenerationInfo = finalChunkGenerationInfo;
20
22
  }
21
23
  splitText(text) {
22
24
  if (this.splitStrategy.type === "regex") return text.split(this.splitStrategy.value);
@@ -27,13 +29,14 @@ var FakeChatModel = class extends _langchain_core_utils_testing.FakeListChatMode
27
29
  return chunks;
28
30
  }
29
31
  }
30
- _createResponseChunk(text, tool_call_chunks) {
32
+ _createResponseChunk(text, tool_call_chunks, responseMetadata) {
31
33
  return new _langchain_core_outputs.ChatGenerationChunk({
32
34
  text,
33
- generationInfo: {},
35
+ generationInfo: responseMetadata ?? {},
34
36
  message: new _langchain_core_messages.AIMessageChunk({
35
37
  content: text,
36
38
  tool_call_chunks,
39
+ response_metadata: responseMetadata,
37
40
  additional_kwargs: tool_call_chunks ? { tool_calls: tool_call_chunks.map((toolCall) => ({
38
41
  index: toolCall.index ?? 0,
39
42
  id: toolCall.id ?? "",
@@ -51,10 +54,11 @@ var FakeChatModel = class extends _langchain_core_utils_testing.FakeListChatMode
51
54
  this._incrementResponse();
52
55
  if (this.emitCustomEvent) await runManager?.handleCustomEvent("some_test_event", { someval: true });
53
56
  const chunks = this.splitText(response);
54
- for await (const chunk of chunks) {
57
+ for (let i = 0; i < chunks.length; i++) {
58
+ const chunk = chunks[i];
55
59
  await this._sleepIfRequested();
56
60
  if (options.thrownErrorString != null && options.thrownErrorString) throw new Error(options.thrownErrorString);
57
- yield super._createResponseChunk(chunk);
61
+ yield i === chunks.length - 1 && this.toolCalls.length === 0 && this.finalChunkGenerationInfo != null ? this._createResponseChunk(chunk, void 0, this.finalChunkGenerationInfo) : super._createResponseChunk(chunk);
58
62
  runManager?.handleLLMNewToken(chunk);
59
63
  }
60
64
  await this._sleepIfRequested();
@@ -1 +1 @@
1
- {"version":3,"file":"fake.cjs","names":["FakeListChatModel","ChatGenerationChunk","AIMessageChunk"],"sources":["../../../src/llm/fake.ts"],"mappings":";;;;AAYA,IAAa,gBAAb,cAAmCA,8BAAAA,kBAAkB;CACnD;CACA,YAAgC,CAAC;CACjC,iBAAkC;CAElC,YAAY,EACV,WACA,OACA,iBACA,gBAAgB;EAAE,MAAM;EAAS,OAAO;CAAmB,GAC3D,YAAY,CAAC,KAOZ;EACD,MAAM;GAAE;GAAW;GAAO;EAAgB,CAAC;EAC3C,KAAK,gBAAgB;EACrB,KAAK,YAAY;CACnB;CAEA,UAAkB,MAAwB;EACxC,IAAI,KAAK,cAAc,SAAS,SAC9B,OAAO,KAAK,MAAM,KAAK,cAAc,KAAe;OAC/C;GACL,MAAM,YAAY,KAAK,cAAc;GACrC,MAAM,SAAmB,CAAC;GAC1B,KAAK,IAAI,IAAI,GAAG,IAAI,KAAK,QAAQ,KAAK,WACpC,OAAO,KAAK,KAAK,MAAM,GAAG,IAAI,SAAS,CAAC;GAE1C,OAAO;EACT;CACF;CACA,qBACE,MACA,kBACqB;EACrB,OAAO,IAAIC,wBAAAA,oBAAoB;GAC7B;GACA,gBAAgB,CAAC;GACjB,SAAS,IAAIC,yBAAAA,eAAe;IAC1B,SAAS;IACT;IACA,mBAAmB,mBACf,EACA,YAAY,iBAAiB,KAAK,cAAc;KAC9C,OAAO,SAAS,SAAS;KACzB,IAAI,SAAS,MAAM;KACnB,MAAM;KACN,UAAU;MACR,MAAM,SAAS,QAAQ;MACvB,WAAW,SAAS,QAAQ;KAC9B;IACF,EAAE,EACJ,IACE,KAAA;GACN,CAAC;EACH,CAAC;CACH;CAEA,OAAO,sBACL,WACA,SACA,YACqC;EACrC,MAAM,WAAW,KAAK,iBAAiB;EACvC,KAAK,mBAAmB;EAExB,IAAI,KAAK,iBACP,MAAM,YAAY,kBAAkB,mBAAmB,EACrD,SAAS,KACX,CAAC;EAGH,MAAM,SAAS,KAAK,UAAU,QAAQ;EACtC,WAAW,MAAM,SAAS,QAAQ;GAChC,MAAM,KAAK,kBAAkB;GAE7B,IAAI,QAAQ,qBAAqB,QAAQ,QAAQ,mBAC/C,MAAM,IAAI,MAAM,QAAQ,iBAAiB;GAI3C,MADsB,MAAM,qBAAqB,KAC/B;GAClB,YAAiB,kBAAkB,KAAK;EAC1C;EAEA,MAAM,KAAK,kBAAkB;EAC7B,IAAI,KAAK,UAAU,SAAS,KAAK,CAAC,KAAK,gBAAgB;GACrD,KAAK,iBAAiB;GACtB,MAAM,iBAAiB,KAAK,UAAU,KAAK,aAAa;IACtD,OAAO;KACL,MAAM,SAAS;KACf,MAAM,KAAK,UAAU,SAAS,IAAI;KAClC,IAAI,SAAS;KACb,MAAM;IACR;GACF,CAAC;GAED,MADsB,KAAK,qBAAqB,IAAI,cAClC;GAClB,YAAiB,kBAAkB,EAAE;EACvC;CACF;AACF;AAEA,SAAgB,uBAAuB,EACrC,WACA,OACA,eACA,aAMgB;CAChB,OAAO,IAAI,cAAc;EACvB;EACA;EACA,iBAAiB;EACjB;EACA;CACF,CAAC;AACH"}
1
+ {"version":3,"file":"fake.cjs","names":["FakeListChatModel","ChatGenerationChunk","AIMessageChunk"],"sources":["../../../src/llm/fake.ts"],"mappings":";;;;AAYA,IAAa,gBAAb,cAAmCA,8BAAAA,kBAAkB;CACnD;CACA,YAAgC,CAAC;CACjC,iBAAkC;CASlC;CAEA,YAAY,EACV,WACA,OACA,iBACA,gBAAgB;EAAE,MAAM;EAAS,OAAO;CAAmB,GAC3D,YAAY,CAAC,GACb,4BAQC;EACD,MAAM;GAAE;GAAW;GAAO;EAAgB,CAAC;EAC3C,KAAK,gBAAgB;EACrB,KAAK,YAAY;EACjB,KAAK,2BAA2B;CAClC;CAEA,UAAkB,MAAwB;EACxC,IAAI,KAAK,cAAc,SAAS,SAC9B,OAAO,KAAK,MAAM,KAAK,cAAc,KAAe;OAC/C;GACL,MAAM,YAAY,KAAK,cAAc;GACrC,MAAM,SAAmB,CAAC;GAC1B,KAAK,IAAI,IAAI,GAAG,IAAI,KAAK,QAAQ,KAAK,WACpC,OAAO,KAAK,KAAK,MAAM,GAAG,IAAI,SAAS,CAAC;GAE1C,OAAO;EACT;CACF;CACA,qBACE,MACA,kBACA,kBACqB;EACrB,OAAO,IAAIC,wBAAAA,oBAAoB;GAC7B;GACA,gBAAgB,oBAAoB,CAAC;GACrC,SAAS,IAAIC,yBAAAA,eAAe;IAC1B,SAAS;IACT;IACA,mBAAmB;IACnB,mBAAmB,mBACf,EACA,YAAY,iBAAiB,KAAK,cAAc;KAC9C,OAAO,SAAS,SAAS;KACzB,IAAI,SAAS,MAAM;KACnB,MAAM;KACN,UAAU;MACR,MAAM,SAAS,QAAQ;MACvB,WAAW,SAAS,QAAQ;KAC9B;IACF,EAAE,EACJ,IACE,KAAA;GACN,CAAC;EACH,CAAC;CACH;CAEA,OAAO,sBACL,WACA,SACA,YACqC;EACrC,MAAM,WAAW,KAAK,iBAAiB;EACvC,KAAK,mBAAmB;EAExB,IAAI,KAAK,iBACP,MAAM,YAAY,kBAAkB,mBAAmB,EACrD,SAAS,KACX,CAAC;EAGH,MAAM,SAAS,KAAK,UAAU,QAAQ;EACtC,KAAK,IAAI,IAAI,GAAG,IAAI,OAAO,QAAQ,KAAK;GACtC,MAAM,QAAQ,OAAO;GACrB,MAAM,KAAK,kBAAkB;GAE7B,IAAI,QAAQ,qBAAqB,QAAQ,QAAQ,mBAC/C,MAAM,IAAI,MAAM,QAAQ,iBAAiB;GAoB3C,MATE,MAAM,OAAO,SAAS,KAAK,KAAK,UAAU,WAAW,KAElC,KAAK,4BAA4B,OAChD,KAAK,qBACL,OACA,KAAA,GACA,KAAK,wBACP,IACE,MAAM,qBAAqB,KAAK;GAEtC,YAAiB,kBAAkB,KAAK;EAC1C;EAEA,MAAM,KAAK,kBAAkB;EAC7B,IAAI,KAAK,UAAU,SAAS,KAAK,CAAC,KAAK,gBAAgB;GACrD,KAAK,iBAAiB;GACtB,MAAM,iBAAiB,KAAK,UAAU,KAAK,aAAa;IACtD,OAAO;KACL,MAAM,SAAS;KACf,MAAM,KAAK,UAAU,SAAS,IAAI;KAClC,IAAI,SAAS;KACb,MAAM;IACR;GACF,CAAC;GAED,MADsB,KAAK,qBAAqB,IAAI,cAClC;GAClB,YAAiB,kBAAkB,EAAE;EACvC;CACF;AACF;AAEA,SAAgB,uBAAuB,EACrC,WACA,OACA,eACA,aAMgB;CAChB,OAAO,IAAI,cAAc;EACvB;EACA;EACA,iBAAiB;EACjB;EACA;CACF,CAAC;AACH"}
@@ -1,8 +1,8 @@
1
1
  require("../common/enum.cjs");
2
2
  require("../common/index.cjs");
3
3
  const require_callbacks = require("../utils/callbacks.cjs");
4
- const require_core = require("../messages/core.cjs");
5
4
  const require_providerRegistry = require("./providerRegistry.cjs");
5
+ const require_core = require("../messages/core.cjs");
6
6
  const require_events = require("../utils/events.cjs");
7
7
  const require_projectionInvariant = require("../messages/projectionInvariant.cjs");
8
8
  require("../messages/index.cjs");
@@ -1,5 +1,5 @@
1
- const require_lazyRequire = require("../../lazyRequire.cjs");
2
1
  const require_cache = require("../../messages/cache.cjs");
2
+ const require_lazyRequire = require("../../lazyRequire.cjs");
3
3
  //#region src/llm/openrouter/toolCache.ts
4
4
  const convertToOpenAITool = (tool) => require_lazyRequire.requireInternalModule("llm/openai/index")._convertToOpenAITool(tool);
5
5
  function getToolName(tool) {
@@ -1,12 +1,12 @@
1
1
  require("../common/enum.cjs");
2
2
  require("../common/index.cjs");
3
+ const require_providerRegistry = require("./providerRegistry.cjs");
4
+ const require_llm = require("../utils/llm.cjs");
3
5
  const require_cache = require("../messages/cache.cjs");
4
6
  const require_core = require("../messages/core.cjs");
5
- const require_providerRegistry = require("./providerRegistry.cjs");
6
7
  const require_alternation = require("../messages/alternation.cjs");
7
8
  const require_handoffCue = require("../messages/handoffCue.cjs");
8
9
  require("../messages/index.cjs");
9
- const require_llm = require("../utils/llm.cjs");
10
10
  const require_toolOutputReferences = require("../tools/toolOutputReferences.cjs");
11
11
  require("./providers.cjs");
12
12
  //#region src/llm/prepareProviderRequest.ts
@@ -1,7 +1,7 @@
1
1
  require("../common/enum.cjs");
2
2
  require("../common/index.cjs");
3
- const require_lazyRequire = require("../lazyRequire.cjs");
4
3
  const require_providerRegistry = require("./providerRegistry.cjs");
4
+ const require_lazyRequire = require("../lazyRequire.cjs");
5
5
  //#region src/llm/providers.ts
6
6
  function initializeBuiltInProvider(provider, loadModel, traits) {
7
7
  require_providerRegistry.registerBuiltInProviderLoader({
@@ -71,5 +71,6 @@ function assertNotTruncatedToolCall(message, provider) {
71
71
  }
72
72
  //#endregion
73
73
  exports.assertNotTruncatedToolCall = assertNotTruncatedToolCall;
74
+ exports.getTruncationStopReason = getTruncationStopReason;
74
75
 
75
76
  //# sourceMappingURL=truncation.cjs.map
package/dist/cjs/main.cjs CHANGED
@@ -7,11 +7,12 @@ const require_truncation = require("./utils/truncation.cjs");
7
7
  const require_tokenCounterCacheCompatibility = require("./llm/tokenCounterCacheCompatibility.cjs");
8
8
  const require_tokens = require("./utils/tokens.cjs");
9
9
  const require_provenance = require("./messages/provenance.cjs");
10
+ const require_providerRegistry = require("./llm/providerRegistry.cjs");
11
+ const require_llm = require("./utils/llm.cjs");
10
12
  const require_cache = require("./messages/cache.cjs");
11
13
  const require_core = require("./messages/core.cjs");
12
14
  const require_ids = require("./messages/ids.cjs");
13
15
  const require_contextPruningSettings = require("./messages/contextPruningSettings.cjs");
14
- const require_providerRegistry = require("./llm/providerRegistry.cjs");
15
16
  const require_contextPruning = require("./messages/contextPruning.cjs");
16
17
  const require_prune = require("./messages/prune.cjs");
17
18
  const require_budget = require("./messages/budget.cjs");
@@ -28,7 +29,6 @@ const require_assistantPhase = require("./messages/assistantPhase.cjs");
28
29
  const require_projectionInvariant = require("./messages/projectionInvariant.cjs");
29
30
  require("./messages/index.cjs");
30
31
  const require_graph = require("./utils/graph.cjs");
31
- const require_llm = require("./utils/llm.cjs");
32
32
  const require_proxy = require("./utils/proxy.cjs");
33
33
  const require_streamLimits = require("./llm/streamLimits.cjs");
34
34
  const require_handlers = require("./tools/handlers.cjs");
@@ -178,6 +178,7 @@ exports.CODE_API_RATE_LIMITED_ERROR_MESSAGE = require_CodeExecutor.CODE_API_RATE
178
178
  exports.CODE_API_UNAVAILABLE_ERROR_MESSAGE = require_CodeExecutor.CODE_API_UNAVAILABLE_ERROR_MESSAGE;
179
179
  exports.CODE_ARTIFACT_PATH_GUIDANCE = require_CodeExecutor.CODE_ARTIFACT_PATH_GUIDANCE;
180
180
  exports.CODE_EXECUTION_TOOLS = require_enum.CODE_EXECUTION_TOOLS;
181
+ exports.COMPACTION_SEMANTIC_INDEX_LIMITS = require_constants.COMPACTION_SEMANTIC_INDEX_LIMITS;
181
182
  exports.Calculator = require_Calculator.Calculator;
182
183
  exports.CalculatorSchema = require_Calculator.CalculatorSchema;
183
184
  exports.CalculatorToolDefinition = require_Calculator.CalculatorToolDefinition;
@@ -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,281 @@ 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() {
155
+ return {
156
+ entries: [],
157
+ entryCount: 0
158
+ };
159
+ }
160
+ function createCompactionSemanticCoverageState() {
161
+ return {
162
+ head: [],
163
+ tail: {
164
+ entries: [],
165
+ cursor: 0
166
+ },
167
+ latestByIdentityHash: /* @__PURE__ */ new Map(),
168
+ typeCoverage: /* @__PURE__ */ new Map()
169
+ };
170
+ }
171
+ function getDerivedCompactionSemanticLocalId(entry) {
172
+ if (entry.type === "reasoning_label") return entry.reasoningStepId.trim();
173
+ if (entry.type !== "activity_phase") return entry.toolCallId.trim();
174
+ return "";
175
+ }
176
+ function hashDerivedCompactionSemanticIdentityString(initialHash, value) {
177
+ let hash = initialHash;
178
+ for (let index = 0; index < value.length; index++) {
179
+ hash ^= value.charCodeAt(index);
180
+ hash = Math.imul(hash, 16777619);
181
+ }
182
+ return hash;
183
+ }
184
+ function hashDerivedCompactionSemanticIdentity(entry) {
185
+ let hash = 2166136261 ^ DERIVED_SEMANTIC_TYPE_HASH[entry.type];
186
+ hash = Math.imul(hash, 16777619);
187
+ hash = hashDerivedCompactionSemanticIdentityString(hash, entry.sourceMessageId.trim());
188
+ hash ^= entry.sourceContentIndex;
189
+ hash = Math.imul(hash, 16777619);
190
+ hash = hashDerivedCompactionSemanticIdentityString(hash, getDerivedCompactionSemanticLocalId(entry));
191
+ return hash >>> 0;
192
+ }
193
+ function entriesShareDerivedCompactionSemanticIdentity(left, right) {
194
+ return left.type === right.type && left.sourceMessageId.trim() === right.sourceMessageId.trim() && left.sourceContentIndex === right.sourceContentIndex && getDerivedCompactionSemanticLocalId(left) === getDerivedCompactionSemanticLocalId(right);
195
+ }
196
+ function entriesHaveConflictingSemanticState(left, right) {
197
+ return left.status !== right.status || left.redacted === true !== (right.redacted === true) || left.text.replace(/\s+/g, " ").trim() !== right.text.replace(/\s+/g, " ").trim();
198
+ }
199
+ function retainCompactionSemanticEntry(entry) {
200
+ entry.retentionCount++;
201
+ }
202
+ function releaseCompactionSemanticEntry(coverage, entry) {
203
+ entry.retentionCount--;
204
+ if (entry.retentionCount === 0 && coverage.latestByIdentityHash.has(entry.identityHash)) {
205
+ const bucket = coverage.latestByIdentityHash.get(entry.identityHash);
206
+ if (bucket == null) return;
207
+ const entryIndex = bucket.indexOf(entry);
208
+ if (entryIndex >= 0) bucket.splice(entryIndex, 1);
209
+ if (bucket.length === 0) coverage.latestByIdentityHash.delete(entry.identityHash);
210
+ }
211
+ }
212
+ function appendCompactionSemanticEntryRing(coverage, ring, entry, limit) {
213
+ if (ring.entries.length < limit) {
214
+ ring.entries.push(entry);
215
+ retainCompactionSemanticEntry(entry);
216
+ return;
217
+ }
218
+ releaseCompactionSemanticEntry(coverage, ring.entries[ring.cursor]);
219
+ ring.entries[ring.cursor] = entry;
220
+ retainCompactionSemanticEntry(entry);
221
+ ring.cursor = (ring.cursor + 1) % limit;
222
+ }
223
+ function renewCompactionSemanticEntryRing(coverage, ring, entry, limit) {
224
+ const existingIndex = ring.entries.indexOf(entry);
225
+ if (existingIndex < 0) {
226
+ appendCompactionSemanticEntryRing(coverage, ring, entry, limit);
227
+ return;
228
+ }
229
+ if (ring.entries.length < limit) {
230
+ ring.entries.splice(existingIndex, 1);
231
+ ring.entries.push(entry);
232
+ return;
233
+ }
234
+ const newestIndex = (ring.cursor + ring.entries.length - 1) % ring.entries.length;
235
+ let currentIndex = existingIndex;
236
+ for (let offset = 0; currentIndex !== newestIndex && offset < ring.entries.length; offset++) {
237
+ const nextIndex = (currentIndex + 1) % ring.entries.length;
238
+ ring.entries[currentIndex] = ring.entries[nextIndex];
239
+ currentIndex = nextIndex;
240
+ }
241
+ ring.entries[newestIndex] = entry;
242
+ }
243
+ function renewCoverageBalancedCompactionSemanticEntry(coverage, entry) {
244
+ renewCompactionSemanticEntryRing(coverage, coverage.tail, entry, DERIVED_SEMANTIC_TAIL_LIMIT);
245
+ const typeCoverage = coverage.typeCoverage.get(entry.entry.type);
246
+ if (typeCoverage == null) return;
247
+ renewCompactionSemanticEntryRing(coverage, typeCoverage.tail, entry, DERIVED_SEMANTIC_TYPE_EDGE_LIMIT);
248
+ }
249
+ function appendDerivedCompactionSemanticEntry(collector, entry) {
250
+ let boundedEntry = entry;
251
+ if (entry.status === "pending") boundedEntry = {
252
+ ...entry,
253
+ text: ""
254
+ };
255
+ else if (entry.text.length > require_constants.COMPACTION_SEMANTIC_INDEX_LIMITS.maxInputTextChars) boundedEntry = {
256
+ ...entry,
257
+ text: "",
258
+ redacted: true
259
+ };
260
+ const order = collector.entryCount;
261
+ collector.entryCount++;
262
+ if (collector.coverage == null && collector.entries.length < require_constants.COMPACTION_SEMANTIC_INDEX_LIMITS.maxInputEntries) {
263
+ collector.entries.push(boundedEntry);
264
+ return;
265
+ }
266
+ const orderedEntry = {
267
+ entry: boundedEntry,
268
+ identityHash: hashDerivedCompactionSemanticIdentity(boundedEntry),
269
+ order,
270
+ retentionCount: 0
271
+ };
272
+ if (collector.coverage == null) {
273
+ collector.coverage = createCompactionSemanticCoverageState();
274
+ for (let order = 0; order < collector.entries.length; order++) appendCoverageBalancedCompactionSemanticEntry(collector.coverage, {
275
+ entry: collector.entries[order],
276
+ identityHash: hashDerivedCompactionSemanticIdentity(collector.entries[order]),
277
+ order,
278
+ retentionCount: 0
279
+ });
280
+ collector.entries = [];
281
+ }
282
+ appendCoverageBalancedCompactionSemanticEntry(collector.coverage, orderedEntry);
283
+ }
284
+ function appendCoverageBalancedCompactionSemanticEntry(coverage, orderedEntry) {
285
+ const identityHash = orderedEntry.identityHash;
286
+ const identityBucket = coverage.latestByIdentityHash.get(identityHash);
287
+ const current = identityBucket?.find(({ entry }) => entriesShareDerivedCompactionSemanticIdentity(entry, orderedEntry.entry));
288
+ if (current != null) {
289
+ if (orderedEntry.entry.revision < current.entry.revision) return;
290
+ if (orderedEntry.entry.revision === current.entry.revision) {
291
+ if (entriesHaveConflictingSemanticState(current.entry, orderedEntry.entry)) {
292
+ current.entry = {
293
+ ...current.entry,
294
+ text: "",
295
+ redacted: true
296
+ };
297
+ current.order = orderedEntry.order;
298
+ renewCoverageBalancedCompactionSemanticEntry(coverage, current);
299
+ }
300
+ return;
301
+ }
302
+ current.entry = orderedEntry.entry;
303
+ current.order = orderedEntry.order;
304
+ renewCoverageBalancedCompactionSemanticEntry(coverage, current);
305
+ return;
306
+ }
307
+ if (identityBucket == null) coverage.latestByIdentityHash.set(identityHash, [orderedEntry]);
308
+ else identityBucket.push(orderedEntry);
309
+ if (coverage.head.length < DERIVED_SEMANTIC_HEAD_LIMIT) {
310
+ coverage.head.push(orderedEntry);
311
+ retainCompactionSemanticEntry(orderedEntry);
312
+ }
313
+ appendCompactionSemanticEntryRing(coverage, coverage.tail, orderedEntry, DERIVED_SEMANTIC_TAIL_LIMIT);
314
+ let typeCoverage = coverage.typeCoverage.get(orderedEntry.entry.type);
315
+ if (typeCoverage == null) {
316
+ typeCoverage = {
317
+ head: [],
318
+ tail: {
319
+ entries: [],
320
+ cursor: 0
321
+ }
322
+ };
323
+ coverage.typeCoverage.set(orderedEntry.entry.type, typeCoverage);
324
+ }
325
+ if (typeCoverage.head.length < DERIVED_SEMANTIC_TYPE_EDGE_LIMIT) {
326
+ typeCoverage.head.push(orderedEntry);
327
+ retainCompactionSemanticEntry(orderedEntry);
328
+ }
329
+ appendCompactionSemanticEntryRing(coverage, typeCoverage.tail, orderedEntry, DERIVED_SEMANTIC_TYPE_EDGE_LIMIT);
330
+ }
331
+ function finalizeDerivedCompactionSemanticIndex(collector) {
332
+ if (collector == null || collector.entryCount === 0) return;
333
+ if (collector.coverage == null) {
334
+ require_semanticIndex.setCompactionSemanticIndexProvidedEntryCount(collector.entries, collector.entryCount);
335
+ return collector.entries;
336
+ }
337
+ const retained = /* @__PURE__ */ new Map();
338
+ const retain = (entries) => {
339
+ for (const { entry, order } of entries) retained.set(order, entry);
340
+ };
341
+ retain(collector.coverage.head);
342
+ retain(collector.coverage.tail.entries);
343
+ for (const typeCoverage of collector.coverage.typeCoverage.values()) {
344
+ retain(typeCoverage.head);
345
+ retain(typeCoverage.tail.entries);
346
+ }
347
+ const result = [...retained.entries()].sort(([left], [right]) => left - right).map(([, entry]) => entry);
348
+ require_semanticIndex.setCompactionSemanticIndexProvidedEntryCount(result, collector.entryCount);
349
+ return result;
350
+ }
351
+ function collectCompactionSemanticEntriesFromPart(collector, part, sourceMessageId, sourceContentIndex, retainedSourceContentStart, retainedSourceContentEnd, intentToolNames, parsedToolInput) {
352
+ if (sourceMessageId.length > require_constants.COMPACTION_SEMANTIC_INDEX_LIMITS.maxIdentityChars) return;
353
+ if (part.type === "tool_call") {
354
+ if (sourceContentIndex < 0 || sourceContentIndex > require_constants.COMPACTION_SEMANTIC_INDEX_LIMITS.maxSourceContentIndex) return;
355
+ const toolCall = part.tool_call;
356
+ const toolCallId = toolCall?.id;
357
+ if (typeof toolCallId !== "string" || toolCallId === "" || toolCallId.length > require_constants.COMPACTION_SEMANTIC_INDEX_LIMITS.maxIdentityChars) return;
358
+ if (typeof toolCall.name === "string" && intentToolNames?.has(toolCall.name) === true) {
359
+ const intent = parsedToolInput != null && typeof parsedToolInput === "object" && !Array.isArray(parsedToolInput) ? parsedToolInput.intent : void 0;
360
+ if (typeof intent === "string" && intent !== "") appendDerivedCompactionSemanticEntry(collector, {
361
+ type: "tool_intent",
362
+ sourceMessageId,
363
+ sourceContentIndex,
364
+ revision: 0,
365
+ status: "committed",
366
+ text: intent,
367
+ toolCallId
368
+ });
369
+ }
370
+ if (typeof toolCall.outcome === "string" && toolCall.outcome !== "") appendDerivedCompactionSemanticEntry(collector, {
371
+ type: "tool_outcome",
372
+ sourceMessageId,
373
+ sourceContentIndex,
374
+ revision: 0,
375
+ status: "committed",
376
+ text: toolCall.outcome,
377
+ toolCallId
378
+ });
379
+ return;
380
+ }
381
+ if (part.type === "think") {
382
+ if (sourceContentIndex < 0 || sourceContentIndex > require_constants.COMPACTION_SEMANTIC_INDEX_LIMITS.maxSourceContentIndex) return;
383
+ const reasoningStepId = part.reasoning_label_step_id;
384
+ const revision = part.reasoning_label_revision;
385
+ const status = part.reasoning_label_status;
386
+ const text = part.reasoning_label;
387
+ if (typeof reasoningStepId !== "string" || reasoningStepId === "" || reasoningStepId.length > require_constants.COMPACTION_SEMANTIC_INDEX_LIMITS.maxIdentityChars || typeof revision !== "number" || !Number.isSafeInteger(revision) || revision < 0) return;
388
+ const malformedState = status !== "complete" && status !== "streaming" || typeof text !== "string";
389
+ appendDerivedCompactionSemanticEntry(collector, {
390
+ type: "reasoning_label",
391
+ sourceMessageId,
392
+ sourceContentIndex,
393
+ revision,
394
+ status: status === "streaming" || malformedState ? "pending" : "committed",
395
+ text: typeof text === "string" ? text : "",
396
+ reasoningStepId,
397
+ ...malformedState ? { redacted: true } : {}
398
+ });
399
+ return;
400
+ }
401
+ if (part.type !== "activity_label" || part.activity_label_type !== "phase") return;
402
+ const activitySourceContentIndex = part.activity_start_index;
403
+ if (typeof activitySourceContentIndex !== "number" || !Number.isSafeInteger(activitySourceContentIndex) || activitySourceContentIndex < retainedSourceContentStart || activitySourceContentIndex >= retainedSourceContentEnd || activitySourceContentIndex > require_constants.COMPACTION_SEMANTIC_INDEX_LIMITS.maxSourceContentIndex) return;
404
+ const revision = part.activity_label_revision;
405
+ if (revision !== void 0 && (typeof revision !== "number" || !Number.isSafeInteger(revision) || revision < 0)) return;
406
+ const malformedPending = part.pending !== void 0 && typeof part.pending !== "boolean";
407
+ const malformedText = typeof part.activity_label !== "string";
408
+ const malformedState = malformedPending || malformedText;
409
+ const text = typeof part.activity_label === "string" ? part.activity_label : "";
410
+ appendDerivedCompactionSemanticEntry(collector, {
411
+ type: "activity_phase",
412
+ sourceMessageId,
413
+ sourceContentIndex: activitySourceContentIndex,
414
+ revision: revision ?? 0,
415
+ status: part.pending === true || malformedPending ? "pending" : "committed",
416
+ text,
417
+ ...malformedState ? { redacted: true } : {}
418
+ });
419
+ }
143
420
  function collectTrustedToolResultSourceContentPartIndices(content, sourceContentPartOffset) {
144
421
  if (content == null) return;
145
422
  let calls;
@@ -295,6 +572,10 @@ function formatAssistantMessage(message, options) {
295
572
  const emittedServerToolUseIds = /* @__PURE__ */ new Set();
296
573
  const pendingServerToolUses = /* @__PURE__ */ new Map();
297
574
  const shouldPreserveReasoningContent = options?.preserveReasoningContent === true;
575
+ const compactionSemanticIndex = options?.compactionSemanticIndex;
576
+ const semanticSourceMessageId = compactionSemanticIndex != null && options?.sourceMessageId != null && options.sourceMessageId.length <= require_constants.COMPACTION_SEMANTIC_INDEX_LIMITS.maxIdentityChars ? options.sourceMessageId : void 0;
577
+ const retainedSourceContentStart = options?.sourceContentPartOffset ?? 0;
578
+ const retainedSourceContentEnd = options?.retainedSourceContentEnd ?? retainedSourceContentStart;
298
579
  const serverToolResultIds = /* @__PURE__ */ new Set();
299
580
  const preferredToolCallParts = /* @__PURE__ */ new Map();
300
581
  const takePendingReasoningContent = () => {
@@ -353,6 +634,7 @@ function formatAssistantMessage(message, options) {
353
634
  for (let partIndex = 0; partIndex < contentParts.length; partIndex++) {
354
635
  const part = contentParts[partIndex];
355
636
  if (part == null) continue;
637
+ if (part.type === "activity_label") continue;
356
638
  const isTrustedResult = isTrustedServerToolResult(part, getSourcePartIndices(partIndex), options?.toolSourceContentPartIndices);
357
639
  if (isTrustedResult) {
358
640
  trustedServerToolResultPartIndices ??= /* @__PURE__ */ new Set();
@@ -374,6 +656,10 @@ function formatAssistantMessage(message, options) {
374
656
  const part = contentParts[partIndex];
375
657
  if (part == null) continue;
376
658
  const sourcePartIndices = getSourcePartIndices(partIndex);
659
+ if (part.type === "activity_label") {
660
+ if (compactionSemanticIndex != null && semanticSourceMessageId != null && typeof sourcePartIndices === "number") collectCompactionSemanticEntriesFromPart(compactionSemanticIndex, part, semanticSourceMessageId, sourcePartIndices, retainedSourceContentStart, retainedSourceContentEnd, options?.intentToolNames);
661
+ continue;
662
+ }
377
663
  const toolUseId = getToolUseId(part);
378
664
  if (toolUseId != null) {
379
665
  const isServerToolResult = trustedServerToolResultPartIndices?.has(partIndex) === true;
@@ -418,12 +704,14 @@ function formatAssistantMessage(message, options) {
418
704
  if (options?.provider === "anthropic" && typeof _tool_call.id === "string" && _tool_call.id.startsWith("srvtoolu_")) {
419
705
  if (!serverToolResultIds.has(_tool_call.id) && options.preserveUnpairedServerToolUses !== true) continue;
420
706
  if (emittedServerToolUseIds.has(_tool_call.id) || pendingServerToolUses.has(_tool_call.id)) continue;
707
+ const serverToolInput = parseServerToolInput(_args);
708
+ if (compactionSemanticIndex != null && semanticSourceMessageId != null && typeof sourcePartIndices === "number") collectCompactionSemanticEntriesFromPart(compactionSemanticIndex, part, semanticSourceMessageId, sourcePartIndices, retainedSourceContentStart, retainedSourceContentEnd, options.intentToolNames, serverToolInput);
421
709
  pendingServerToolUses.set(_tool_call.id, {
422
710
  content: {
423
711
  type: "server_tool_use",
424
712
  id: _tool_call.id,
425
713
  name: _tool_call.name,
426
- input: parseServerToolInput(_args)
714
+ input: serverToolInput
427
715
  },
428
716
  sourceContentPartIndices: sourcePartIndices
429
717
  });
@@ -442,6 +730,7 @@ function formatAssistantMessage(message, options) {
442
730
  if (typeof _args === "string") args = { input: _args };
443
731
  }
444
732
  tool_call.args = args;
733
+ if (compactionSemanticIndex != null && semanticSourceMessageId != null && typeof sourcePartIndices === "number") collectCompactionSemanticEntriesFromPart(compactionSemanticIndex, part, semanticSourceMessageId, sourcePartIndices, retainedSourceContentStart, retainedSourceContentEnd, options?.intentToolNames, args);
445
734
  if (options?.provider === "anthropic" && Array.isArray(lastAIMessage.content)) {
446
735
  const content = lastAIMessage.content;
447
736
  content.push({
@@ -461,6 +750,7 @@ function formatAssistantMessage(message, options) {
461
750
  content: formatToolCallOutput(output)
462
751
  }), "tool"), "tool", sourcePartIndices);
463
752
  } else if (part.type === "think" || part.type === "thinking" || part.type === "reasoning" || part.type === "reasoning_content" || part.type === "redacted_thinking") {
753
+ if (part.type === "think" && compactionSemanticIndex != null && semanticSourceMessageId != null && typeof sourcePartIndices === "number") collectCompactionSemanticEntriesFromPart(compactionSemanticIndex, part, semanticSourceMessageId, sourcePartIndices, retainedSourceContentStart, retainedSourceContentEnd, options?.intentToolNames);
464
754
  hasReasoning = true;
465
755
  pendingReasoningContent += extractReasoningContent(part);
466
756
  appendSourceContentPartIndices(pendingReasoningSourcePartIndices, sourcePartIndices);
@@ -487,7 +777,7 @@ function formatAssistantMessage(message, options) {
487
777
  hasReasoning = false;
488
778
  pendingReasoningContent = "";
489
779
  pendingReasoningSourcePartIndices = [];
490
- } else if (part.type === "error" || part.type === "agent_update" || part.type === "summary" || part.type === "activity_label") continue;
780
+ } else if (part.type === "error" || part.type === "agent_update" || part.type === "summary") continue;
491
781
  else {
492
782
  if (part.type === "text" && !getTextContent(part).trim()) continue;
493
783
  currentContent.push(part);
@@ -764,6 +1054,7 @@ function extractSkillName(args) {
764
1054
  const formatAgentMessages = (payload, indexTokenCountMap, tools, skills, options) => {
765
1055
  const messages = [];
766
1056
  const legacyContentEnabled = options?.legacyContent === true;
1057
+ const compactionSemanticIndexCollector = options?.compactionSemanticIndex != null ? createDerivedCompactionSemanticIndexCollector() : void 0;
767
1058
  const emitFormattedMessage = (message) => {
768
1059
  if (legacyContentEnabled && require_content.isLegacyConvertible(message)) {
769
1060
  const flattened = require_content.flattenLegacyContent(message.content);
@@ -914,9 +1205,12 @@ const formatAgentMessages = (payload, indexTokenCountMap, tools, skills, options
914
1205
  }
915
1206
  }
916
1207
  const formattedMessages = formatAssistantMessage(processedMessage, {
1208
+ compactionSemanticIndex: compactionSemanticIndexCollector,
1209
+ intentToolNames: options?.compactionSemanticIndex?.intentToolNames,
917
1210
  preserveUnpairedServerToolUses: i === payload.length - 1,
918
1211
  preserveReasoningContent: options?.preserveReasoningContent ?? options?.provider === "deepseek",
919
1212
  provider: options?.provider,
1213
+ retainedSourceContentEnd: sourceContentPartOffset + (Array.isArray(message.content) ? message.content.length : 0),
920
1214
  sourceMessageId,
921
1215
  sourceContentPartOffset,
922
1216
  sourceContentPartIndices: processedSourceContentPartIndices,
@@ -1043,7 +1337,8 @@ const formatAgentMessages = (payload, indexTokenCountMap, tools, skills, options
1043
1337
  text: summaryBoundary.text,
1044
1338
  tokenCount: summaryBoundary.tokenCount
1045
1339
  } : void 0,
1046
- boundaryTokenAdjustment
1340
+ boundaryTokenAdjustment,
1341
+ compactionSemanticIndex: finalizeDerivedCompactionSemanticIndex(compactionSemanticIndexCollector)
1047
1342
  };
1048
1343
  };
1049
1344
  function shiftIndexTokenCountMap(indexTokenCountMap, instructionsTokenCount) {