@librechat/agents 3.9.1 → 3.9.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1 +1 @@
1
- {"version":3,"file":"truncation.mjs","names":[],"sources":["../../../src/utils/truncation.ts"],"mappings":";AAYA,MAAa,6BAA6B;AAU1C,MAAa,kCAAkC;AAU/C,SAAgB,4BACd,qBACQ;CACR,IAAI,uBAAuB,QAAQ,uBAAuB,GACxD,OAAO;CAET,OAAO,KAAK,IACV,KAAK,MAAM,sBAAsB,EAAG,IAAI,GACxC,0BACF;AACF;AAaA,SAAgB,gCACd,eACQ;CACR,IAAI,iBAAiB,QAAQ,iBAAiB,GAC5C,OAAO;CAET,OAAO,KAAK,IAAI,gBAAgB,GAAG,+BAA+B;AACpE;AAYA,SAAgB,kBACd,OACA,UACgD;CAChD,MAAM,aAAa,OAAO,UAAU,WAAW,QAAQ,KAAK,UAAU,KAAK;CAC3E,IAAI,WAAW,UAAU,UACvB,OAAO;EAAE,YAAY;EAAY,gBAAgB,WAAW;CAAO;CAErE,MAAM,YAAY,mBAAmB,WAAW,OAAO,kBAAkB,SAAS;CAClF,MAAM,YAAY,WAAW,UAAU;CAEvC,IAAI,YAAY,KACd,OAAO;EACL,YAAY,WAAW,MAAM,GAAG,QAAQ,IAAI,UAAU,QAAQ;EAC9D,gBAAgB,WAAW;CAC7B;CAGF,MAAM,WAAW,KAAK,KAAK,YAAY,EAAG;CAC1C,MAAM,WAAW,YAAY;CAE7B,OAAO;EACL,YACE,WAAW,MAAM,GAAG,QAAQ,IAC5B,YACA,WAAW,MAAM,WAAW,SAAS,QAAQ;EAC/C,gBAAgB,WAAW;CAC7B;AACF;AAeA,SAAgB,0BACd,SACA,UACQ;CACR,IAAI,QAAQ,UAAU,UACpB,OAAO;CAGT,MAAM,YAAY,qBAAqB,QAAQ,OAAO,kBAAkB,SAAS;CACjF,MAAM,YAAY,WAAW,UAAU;CACvC,IAAI,aAAa,GACf,OA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AS;CACjC,MAAM,cAAc,QAAQ,QAAQ,MAAM,SAAS;CACnD,IAAI,cAAc,KAAK,cAAc,YAAY,KAC/C,YAAY,cAAc;CAG5B,OACE,gCAAgC,SAAS,GAAG,OAAO,IACnD,YACA,gCAAgC,SAAS,SAAS;AAEtD;AAGA,MAAa,iCAAiC;AAG9C,MAAa,uBAAuB;AAMpC,SAAgB,+BACd,kBACQ;CACR,IAAI,oBAAoB,QAAQ,oBAAoB,GAClD,OAAO;CAET,OAAO,KAAK,IAAA,GAEV,KAAK,IACH,KAAK,MAAM,mBAAmB,GAAI,IAAI,GACtC,8BACF,CACF;AACF"}
@@ -16,7 +16,7 @@ type StructuredWorkState = {
16
16
  export type BoundedStructuredSerialization = {
17
17
  /** Provider-facing head/tail preview, bounded by `maxChars`. */
18
18
  content: string;
19
- /** Exact serialized prefix up to the defensive traversal-work ceiling. */
19
+ /** Code-point-aligned serialized prefix up to the traversal-work ceiling. */
20
20
  prefix: string;
21
21
  /** Exact length, or `Number.MAX_SAFE_INTEGER` when traversal was capped. */
22
22
  originalChars: number;
@@ -4,6 +4,8 @@
4
4
  * Prevents oversized tool outputs from entering the message array and
5
5
  * consuming the entire context window.
6
6
  */
7
+ /** Slices by UTF-16 code units, dropping orphaned surrogate halves at either edge. */
8
+ export declare function sliceWithoutSplittingSurrogates(value: string, start: number, end?: number): string;
7
9
  /**
8
10
  * Absolute hard cap on tool result length (characters).
9
11
  * Even if the model has a 1M-token context, a single tool result
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@librechat/agents",
3
- "version": "3.9.1",
3
+ "version": "3.9.2",
4
4
  "reova": {
5
5
  "enabled": true,
6
6
  "endpoint": "https://telemetry.reo.dev/data"
@@ -26,6 +26,7 @@ import {
26
26
  serializeToolContentBounded,
27
27
  } from '@/utils/toolContent';
28
28
  import { resolveContextPruningSettings } from './contextPruningSettings';
29
+ import { sliceWithoutSplittingSurrogates } from '@/utils/truncation';
29
30
 
30
31
  /**
31
32
  * Applies head+tail soft-trim to tool result content.
@@ -36,7 +37,11 @@ function softTrimContent(
36
37
  ): string {
37
38
  const { headChars, tailChars } = settings;
38
39
  const indicator = `\n\n… [soft-trimmed: ${content.length} chars → ${headChars + tailChars} chars, middle removed] …\n\n`;
39
- return content.slice(0, headChars) + indicator + content.slice(-tailChars);
40
+ return (
41
+ sliceWithoutSplittingSurrogates(content, 0, headChars) +
42
+ indicator +
43
+ sliceWithoutSplittingSurrogates(content, -tailChars)
44
+ );
40
45
  }
41
46
 
42
47
  export interface ContextPruningResult {
@@ -140,7 +145,8 @@ export function applyContextPruning(params: {
140
145
  }
141
146
  const content = message.content;
142
147
  const contentLength = getToolContentCharLength(content);
143
- const eligibilityContent = params.canonicalMessages?.[i]?.content ?? content;
148
+ const eligibilityContent =
149
+ params.canonicalMessages?.[i]?.content ?? content;
144
150
  if (
145
151
  getToolContentCharLength(eligibilityContent) <
146
152
  settings.minPrunableToolChars
@@ -19,6 +19,7 @@ import {
19
19
  HARD_MAX_TOOL_CALL_INPUT_CHARS,
20
20
  HARD_MAX_TOOL_RESULT_CHARS,
21
21
  MIN_JSON_VALUE_CHARS,
22
+ sliceWithoutSplittingSurrogates,
22
23
  calculateMaxToolCallInputChars,
23
24
  calculateMaxToolResultChars,
24
25
  } from '@/utils/truncation';
@@ -1591,7 +1592,8 @@ function createBoundedTruncationValue(
1591
1592
  const next = Math.ceil((low + high) / 2);
1592
1593
  const candidate = {
1593
1594
  _truncated:
1594
- TOOL_INPUT_TRUNCATION_MARKER + canonicalPrefix.slice(0, next),
1595
+ TOOL_INPUT_TRUNCATION_MARKER +
1596
+ sliceWithoutSplittingSurrogates(canonicalPrefix, 0, next),
1595
1597
  _originalChars: originalChars,
1596
1598
  };
1597
1599
  if (JSON.stringify(candidate).length <= normalizedMaxChars) {
@@ -1604,7 +1606,8 @@ function createBoundedTruncationValue(
1604
1606
  // Keep the marker separate from a pure canonical prefix so another,
1605
1607
  // slightly smaller cap can be derived without nesting the envelope.
1606
1608
  _truncated:
1607
- TOOL_INPUT_TRUNCATION_MARKER + canonicalPrefix.slice(0, low),
1609
+ TOOL_INPUT_TRUNCATION_MARKER +
1610
+ sliceWithoutSplittingSurrogates(canonicalPrefix, 0, low),
1608
1611
  _originalChars: originalChars,
1609
1612
  };
1610
1613
  }
@@ -1847,8 +1850,12 @@ function projectStringInputWithinLimit(
1847
1850
  return {
1848
1851
  value:
1849
1852
  marker.length >= normalizedMaxChars
1850
- ? prefix.slice(0, normalizedMaxChars)
1851
- : prefix.slice(0, normalizedMaxChars - marker.length) + marker,
1853
+ ? sliceWithoutSplittingSurrogates(prefix, 0, normalizedMaxChars)
1854
+ : sliceWithoutSplittingSurrogates(
1855
+ prefix,
1856
+ 0,
1857
+ normalizedMaxChars - marker.length
1858
+ ) + marker,
1852
1859
  changed: true,
1853
1860
  };
1854
1861
  }
@@ -2366,7 +2373,9 @@ function applyToolCallInputCaps(params: {
2366
2373
  additionalKwargsChanges
2367
2374
  );
2368
2375
  }
2369
- if (capped.response_metadata.output !== canonical.response_metadata.output) {
2376
+ if (
2377
+ capped.response_metadata.output !== canonical.response_metadata.output
2378
+ ) {
2370
2379
  changes.response_metadata = cloneWithProjectedProperties(
2371
2380
  current.response_metadata,
2372
2381
  { output: capped.response_metadata.output }
@@ -2523,11 +2532,7 @@ export function createPruneMessages(factoryParams: PruneMessagesFactoryParams) {
2523
2532
  originalToolContent.clear();
2524
2533
  originalToolContentSize = 0;
2525
2534
  }
2526
- for (
2527
- let i = toolExchangeWidthThrough;
2528
- i < canonicalMessages.length;
2529
- i++
2530
- ) {
2535
+ for (let i = toolExchangeWidthThrough; i < canonicalMessages.length; i++) {
2531
2536
  maxToolExchangeWidth = Math.max(
2532
2537
  maxToolExchangeWidth,
2533
2538
  getToolCallIds(canonicalMessages[i]).size
@@ -3,6 +3,7 @@ import { ToolMessage, type BaseMessage } from '@langchain/core/messages';
3
3
  import {
4
4
  HARD_MAX_TOTAL_TOOL_OUTPUT_SIZE,
5
5
  truncateToolResultContent,
6
+ sliceWithoutSplittingSurrogates,
6
7
  } from './truncation';
7
8
 
8
9
  type ToolContent = BaseMessage['content'];
@@ -421,7 +422,7 @@ type SegmentedStringBuffer = {
421
422
  export type BoundedStructuredSerialization = {
422
423
  /** Provider-facing head/tail preview, bounded by `maxChars`. */
423
424
  content: string;
424
- /** Exact serialized prefix up to the defensive traversal-work ceiling. */
425
+ /** Code-point-aligned serialized prefix up to the traversal-work ceiling. */
425
426
  prefix: string;
426
427
  /** Exact length, or `Number.MAX_SAFE_INTEGER` when traversal was capped. */
427
428
  originalChars: number;
@@ -505,6 +506,10 @@ function materializeSegmentedStringBuffer(
505
506
  return segments.join('');
506
507
  }
507
508
 
509
+ /**
510
+ * Retains exact code-unit prefixes internally. Trimming surrogate edges here
511
+ * would let later chunks fill the gap and corrupt the prefix; trim on exposure.
512
+ */
508
513
  function appendSerializedChunk(
509
514
  collector: StructuredSerializationCollector,
510
515
  chunk: string
@@ -953,7 +958,7 @@ function formatBoundedStructuredContent(
953
958
  suffix: string
954
959
  ): string {
955
960
  if (collector.totalChars <= maxChars) {
956
- return prefix.slice(0, collector.totalChars);
961
+ return sliceWithoutSplittingSurrogates(prefix, 0, collector.totalChars);
957
962
  }
958
963
 
959
964
  const indicator = collector.work.exceeded
@@ -962,10 +967,13 @@ function formatBoundedStructuredContent(
962
967
  `${maxChars} limit] …\n\n`;
963
968
  const available = maxChars - indicator.length;
964
969
  if (available <= 0) {
965
- return prefix.slice(0, maxChars);
970
+ return sliceWithoutSplittingSurrogates(prefix, 0, maxChars);
966
971
  }
967
972
  if (available < 200) {
968
- return prefix.slice(0, available) + indicator.trimEnd();
973
+ return (
974
+ sliceWithoutSplittingSurrogates(prefix, 0, available) +
975
+ indicator.trimEnd()
976
+ );
969
977
  }
970
978
 
971
979
  const headSize = Math.ceil(available * 0.7);
@@ -982,7 +990,11 @@ function formatBoundedStructuredContent(
982
990
  tailStart = tailNewline + 1;
983
991
  }
984
992
 
985
- return prefix.slice(0, headEnd) + indicator + suffix.slice(tailStart);
993
+ return (
994
+ sliceWithoutSplittingSurrogates(prefix, 0, headEnd) +
995
+ indicator +
996
+ sliceWithoutSplittingSurrogates(suffix, tailStart)
997
+ );
986
998
  }
987
999
 
988
1000
  /**
@@ -1036,7 +1048,7 @@ export function serializeStructuredValueBounded(
1036
1048
  prefix,
1037
1049
  suffix
1038
1050
  ),
1039
- prefix: prefix.slice(0, normalizedPrefixChars),
1051
+ prefix: sliceWithoutSplittingSurrogates(prefix, 0, normalizedPrefixChars),
1040
1052
  originalChars,
1041
1053
  truncated:
1042
1054
  collector.work.exceeded || collector.totalChars > normalizedMaxChars,
@@ -1931,7 +1943,11 @@ function serializeDenseTextBlocksWithinLimit(
1931
1943
  }
1932
1944
  preview += textBlocks[i].text.slice(0, available - preview.length);
1933
1945
  }
1934
- return (preview + indicator).slice(0, normalizedMaxChars);
1946
+ return sliceWithoutSplittingSurrogates(
1947
+ sliceWithoutSplittingSurrogates(preview, 0) + indicator,
1948
+ 0,
1949
+ normalizedMaxChars
1950
+ );
1935
1951
  }
1936
1952
 
1937
1953
  export type CompactToolContentResult = {
@@ -5,6 +5,22 @@
5
5
  * consuming the entire context window.
6
6
  */
7
7
 
8
+ /** Slices by UTF-16 code units, dropping orphaned surrogate halves at either edge. */
9
+ export function sliceWithoutSplittingSurrogates(
10
+ value: string,
11
+ start: number,
12
+ end?: number
13
+ ): string {
14
+ const sliced = value.slice(start, end);
15
+ const first = sliced.charCodeAt(0);
16
+ const last = sliced.charCodeAt(sliced.length - 1);
17
+ const trimStart = first >= 0xdc00 && first <= 0xdfff;
18
+ const trimEnd = last >= 0xd800 && last <= 0xdbff;
19
+ return trimStart || trimEnd
20
+ ? sliced.slice(trimStart ? 1 : 0, sliced.length - (trimEnd ? 1 : 0))
21
+ : sliced;
22
+ }
23
+
8
24
  /**
9
25
  * Absolute hard cap on tool result length (characters).
10
26
  * Even if the model has a 1M-token context, a single tool result
@@ -85,7 +101,9 @@ export function truncateToolInput(
85
101
 
86
102
  if (available < 100) {
87
103
  return {
88
- _truncated: serialized.slice(0, maxChars) + indicator.trimEnd(),
104
+ _truncated:
105
+ sliceWithoutSplittingSurrogates(serialized, 0, maxChars) +
106
+ indicator.trimEnd(),
89
107
  _originalChars: serialized.length,
90
108
  };
91
109
  }
@@ -95,9 +113,9 @@ export function truncateToolInput(
95
113
 
96
114
  return {
97
115
  _truncated:
98
- serialized.slice(0, headSize) +
116
+ sliceWithoutSplittingSurrogates(serialized, 0, headSize) +
99
117
  indicator +
100
- serialized.slice(serialized.length - tailSize),
118
+ sliceWithoutSplittingSurrogates(serialized, serialized.length - tailSize),
101
119
  _originalChars: serialized.length,
102
120
  };
103
121
  }
@@ -126,12 +144,15 @@ export function truncateToolResultContent(
126
144
  const indicator = `\n\n… [truncated: ${content.length} chars exceeded ${maxChars} limit] …\n\n`;
127
145
  const available = maxChars - indicator.length;
128
146
  if (available <= 0) {
129
- return content.slice(0, maxChars);
147
+ return sliceWithoutSplittingSurrogates(content, 0, maxChars);
130
148
  }
131
149
 
132
150
  // When budget is too small for a meaningful tail, fall back to head-only
133
151
  if (available < 200) {
134
- return content.slice(0, available) + indicator.trimEnd();
152
+ return (
153
+ sliceWithoutSplittingSurrogates(content, 0, available) +
154
+ indicator.trimEnd()
155
+ );
135
156
  }
136
157
 
137
158
  const headSize = Math.ceil(available * 0.7);
@@ -150,7 +171,11 @@ export function truncateToolResultContent(
150
171
  tailStart = tailNewline + 1;
151
172
  }
152
173
 
153
- return content.slice(0, headEnd) + indicator + content.slice(tailStart);
174
+ return (
175
+ sliceWithoutSplittingSurrogates(content, 0, headEnd) +
176
+ indicator +
177
+ sliceWithoutSplittingSurrogates(content, tailStart)
178
+ );
154
179
  }
155
180
 
156
181
  /** Absolute hard cap on a single tool-call input (characters). */