@librechat/agents 3.1.68 → 3.1.71-dev.0

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 (192) 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/enum.cjs +16 -1
  4. package/dist/cjs/common/enum.cjs.map +1 -1
  5. package/dist/cjs/graphs/Graph.cjs +136 -0
  6. package/dist/cjs/graphs/Graph.cjs.map +1 -1
  7. package/dist/cjs/hooks/HookRegistry.cjs +162 -0
  8. package/dist/cjs/hooks/HookRegistry.cjs.map +1 -0
  9. package/dist/cjs/hooks/executeHooks.cjs +276 -0
  10. package/dist/cjs/hooks/executeHooks.cjs.map +1 -0
  11. package/dist/cjs/hooks/matchers.cjs +256 -0
  12. package/dist/cjs/hooks/matchers.cjs.map +1 -0
  13. package/dist/cjs/hooks/types.cjs +27 -0
  14. package/dist/cjs/hooks/types.cjs.map +1 -0
  15. package/dist/cjs/main.cjs +57 -0
  16. package/dist/cjs/main.cjs.map +1 -1
  17. package/dist/cjs/messages/format.cjs +74 -12
  18. package/dist/cjs/messages/format.cjs.map +1 -1
  19. package/dist/cjs/messages/prune.cjs +9 -2
  20. package/dist/cjs/messages/prune.cjs.map +1 -1
  21. package/dist/cjs/run.cjs +115 -0
  22. package/dist/cjs/run.cjs.map +1 -1
  23. package/dist/cjs/summarization/node.cjs +44 -0
  24. package/dist/cjs/summarization/node.cjs.map +1 -1
  25. package/dist/cjs/tools/BashExecutor.cjs +208 -0
  26. package/dist/cjs/tools/BashExecutor.cjs.map +1 -0
  27. package/dist/cjs/tools/BashProgrammaticToolCalling.cjs +287 -0
  28. package/dist/cjs/tools/BashProgrammaticToolCalling.cjs.map +1 -0
  29. package/dist/cjs/tools/CodeExecutor.cjs +0 -9
  30. package/dist/cjs/tools/CodeExecutor.cjs.map +1 -1
  31. package/dist/cjs/tools/ProgrammaticToolCalling.cjs +7 -23
  32. package/dist/cjs/tools/ProgrammaticToolCalling.cjs.map +1 -1
  33. package/dist/cjs/tools/ReadFile.cjs +43 -0
  34. package/dist/cjs/tools/ReadFile.cjs.map +1 -0
  35. package/dist/cjs/tools/SkillTool.cjs +50 -0
  36. package/dist/cjs/tools/SkillTool.cjs.map +1 -0
  37. package/dist/cjs/tools/SubagentTool.cjs +92 -0
  38. package/dist/cjs/tools/SubagentTool.cjs.map +1 -0
  39. package/dist/cjs/tools/ToolNode.cjs +746 -174
  40. package/dist/cjs/tools/ToolNode.cjs.map +1 -1
  41. package/dist/cjs/tools/ToolSearch.cjs +2 -13
  42. package/dist/cjs/tools/ToolSearch.cjs.map +1 -1
  43. package/dist/cjs/tools/skillCatalog.cjs +84 -0
  44. package/dist/cjs/tools/skillCatalog.cjs.map +1 -0
  45. package/dist/cjs/tools/subagent/SubagentExecutor.cjs +511 -0
  46. package/dist/cjs/tools/subagent/SubagentExecutor.cjs.map +1 -0
  47. package/dist/cjs/tools/toolOutputReferences.cjs +475 -0
  48. package/dist/cjs/tools/toolOutputReferences.cjs.map +1 -0
  49. package/dist/cjs/utils/truncation.cjs +28 -0
  50. package/dist/cjs/utils/truncation.cjs.map +1 -1
  51. package/dist/esm/agents/AgentContext.mjs +23 -3
  52. package/dist/esm/agents/AgentContext.mjs.map +1 -1
  53. package/dist/esm/common/enum.mjs +15 -2
  54. package/dist/esm/common/enum.mjs.map +1 -1
  55. package/dist/esm/graphs/Graph.mjs +136 -0
  56. package/dist/esm/graphs/Graph.mjs.map +1 -1
  57. package/dist/esm/hooks/HookRegistry.mjs +160 -0
  58. package/dist/esm/hooks/HookRegistry.mjs.map +1 -0
  59. package/dist/esm/hooks/executeHooks.mjs +273 -0
  60. package/dist/esm/hooks/executeHooks.mjs.map +1 -0
  61. package/dist/esm/hooks/matchers.mjs +251 -0
  62. package/dist/esm/hooks/matchers.mjs.map +1 -0
  63. package/dist/esm/hooks/types.mjs +25 -0
  64. package/dist/esm/hooks/types.mjs.map +1 -0
  65. package/dist/esm/main.mjs +13 -2
  66. package/dist/esm/main.mjs.map +1 -1
  67. package/dist/esm/messages/format.mjs +66 -4
  68. package/dist/esm/messages/format.mjs.map +1 -1
  69. package/dist/esm/messages/prune.mjs +9 -2
  70. package/dist/esm/messages/prune.mjs.map +1 -1
  71. package/dist/esm/run.mjs +115 -0
  72. package/dist/esm/run.mjs.map +1 -1
  73. package/dist/esm/summarization/node.mjs +44 -0
  74. package/dist/esm/summarization/node.mjs.map +1 -1
  75. package/dist/esm/tools/BashExecutor.mjs +200 -0
  76. package/dist/esm/tools/BashExecutor.mjs.map +1 -0
  77. package/dist/esm/tools/BashProgrammaticToolCalling.mjs +278 -0
  78. package/dist/esm/tools/BashProgrammaticToolCalling.mjs.map +1 -0
  79. package/dist/esm/tools/CodeExecutor.mjs +0 -9
  80. package/dist/esm/tools/CodeExecutor.mjs.map +1 -1
  81. package/dist/esm/tools/ProgrammaticToolCalling.mjs +8 -24
  82. package/dist/esm/tools/ProgrammaticToolCalling.mjs.map +1 -1
  83. package/dist/esm/tools/ReadFile.mjs +38 -0
  84. package/dist/esm/tools/ReadFile.mjs.map +1 -0
  85. package/dist/esm/tools/SkillTool.mjs +45 -0
  86. package/dist/esm/tools/SkillTool.mjs.map +1 -0
  87. package/dist/esm/tools/SubagentTool.mjs +85 -0
  88. package/dist/esm/tools/SubagentTool.mjs.map +1 -0
  89. package/dist/esm/tools/ToolNode.mjs +748 -176
  90. package/dist/esm/tools/ToolNode.mjs.map +1 -1
  91. package/dist/esm/tools/ToolSearch.mjs +3 -14
  92. package/dist/esm/tools/ToolSearch.mjs.map +1 -1
  93. package/dist/esm/tools/skillCatalog.mjs +82 -0
  94. package/dist/esm/tools/skillCatalog.mjs.map +1 -0
  95. package/dist/esm/tools/subagent/SubagentExecutor.mjs +505 -0
  96. package/dist/esm/tools/subagent/SubagentExecutor.mjs.map +1 -0
  97. package/dist/esm/tools/toolOutputReferences.mjs +468 -0
  98. package/dist/esm/tools/toolOutputReferences.mjs.map +1 -0
  99. package/dist/esm/utils/truncation.mjs +27 -1
  100. package/dist/esm/utils/truncation.mjs.map +1 -1
  101. package/dist/types/agents/AgentContext.d.ts +6 -0
  102. package/dist/types/common/enum.d.ts +10 -2
  103. package/dist/types/graphs/Graph.d.ts +23 -0
  104. package/dist/types/hooks/HookRegistry.d.ts +56 -0
  105. package/dist/types/hooks/executeHooks.d.ts +79 -0
  106. package/dist/types/hooks/index.d.ts +6 -0
  107. package/dist/types/hooks/matchers.d.ts +95 -0
  108. package/dist/types/hooks/types.d.ts +320 -0
  109. package/dist/types/index.d.ts +8 -0
  110. package/dist/types/messages/format.d.ts +2 -1
  111. package/dist/types/run.d.ts +2 -0
  112. package/dist/types/summarization/node.d.ts +2 -0
  113. package/dist/types/tools/BashExecutor.d.ts +76 -0
  114. package/dist/types/tools/BashProgrammaticToolCalling.d.ts +72 -0
  115. package/dist/types/tools/ProgrammaticToolCalling.d.ts +4 -9
  116. package/dist/types/tools/ReadFile.d.ts +28 -0
  117. package/dist/types/tools/SkillTool.d.ts +40 -0
  118. package/dist/types/tools/SubagentTool.d.ts +36 -0
  119. package/dist/types/tools/ToolNode.d.ts +109 -4
  120. package/dist/types/tools/ToolSearch.d.ts +2 -2
  121. package/dist/types/tools/skillCatalog.d.ts +19 -0
  122. package/dist/types/tools/subagent/SubagentExecutor.d.ts +137 -0
  123. package/dist/types/tools/subagent/index.d.ts +2 -0
  124. package/dist/types/tools/toolOutputReferences.d.ts +205 -0
  125. package/dist/types/types/graph.d.ts +61 -2
  126. package/dist/types/types/index.d.ts +1 -0
  127. package/dist/types/types/run.d.ts +28 -0
  128. package/dist/types/types/skill.d.ts +9 -0
  129. package/dist/types/types/tools.d.ts +108 -10
  130. package/dist/types/utils/truncation.d.ts +21 -0
  131. package/package.json +5 -1
  132. package/src/agents/AgentContext.ts +26 -2
  133. package/src/common/enum.ts +15 -1
  134. package/src/graphs/Graph.ts +161 -0
  135. package/src/hooks/HookRegistry.ts +208 -0
  136. package/src/hooks/__tests__/HookRegistry.test.ts +190 -0
  137. package/src/hooks/__tests__/compactHooks.test.ts +214 -0
  138. package/src/hooks/__tests__/executeHooks.test.ts +1013 -0
  139. package/src/hooks/__tests__/integration.test.ts +337 -0
  140. package/src/hooks/__tests__/matchers.test.ts +238 -0
  141. package/src/hooks/__tests__/toolHooks.test.ts +669 -0
  142. package/src/hooks/executeHooks.ts +375 -0
  143. package/src/hooks/index.ts +57 -0
  144. package/src/hooks/matchers.ts +280 -0
  145. package/src/hooks/types.ts +404 -0
  146. package/src/index.ts +10 -0
  147. package/src/messages/format.ts +74 -4
  148. package/src/messages/formatAgentMessages.skills.test.ts +334 -0
  149. package/src/messages/prune.ts +9 -2
  150. package/src/run.ts +130 -0
  151. package/src/scripts/multi-agent-subagent.ts +246 -0
  152. package/src/scripts/programmatic_exec.ts +1 -10
  153. package/src/scripts/subagent-event-driven-debug.ts +190 -0
  154. package/src/scripts/subagent-tools-debug.ts +160 -0
  155. package/src/scripts/test_code_api.ts +0 -7
  156. package/src/scripts/tool_search.ts +1 -10
  157. package/src/specs/prune.test.ts +413 -0
  158. package/src/specs/subagent.test.ts +305 -0
  159. package/src/summarization/node.ts +53 -0
  160. package/src/tools/BashExecutor.ts +238 -0
  161. package/src/tools/BashProgrammaticToolCalling.ts +381 -0
  162. package/src/tools/CodeExecutor.ts +0 -11
  163. package/src/tools/ProgrammaticToolCalling.ts +4 -29
  164. package/src/tools/ReadFile.ts +39 -0
  165. package/src/tools/SkillTool.ts +46 -0
  166. package/src/tools/SubagentTool.ts +100 -0
  167. package/src/tools/ToolNode.ts +999 -214
  168. package/src/tools/ToolSearch.ts +3 -19
  169. package/src/tools/__tests__/BashExecutor.test.ts +36 -0
  170. package/src/tools/__tests__/ProgrammaticToolCalling.integration.test.ts +7 -8
  171. package/src/tools/__tests__/ProgrammaticToolCalling.test.ts +0 -1
  172. package/src/tools/__tests__/ReadFile.test.ts +44 -0
  173. package/src/tools/__tests__/SkillTool.test.ts +442 -0
  174. package/src/tools/__tests__/SubagentExecutor.test.ts +1148 -0
  175. package/src/tools/__tests__/SubagentTool.test.ts +149 -0
  176. package/src/tools/__tests__/ToolNode.outputReferences.test.ts +1395 -0
  177. package/src/tools/__tests__/ToolNode.session.test.ts +12 -12
  178. package/src/tools/__tests__/ToolSearch.integration.test.ts +7 -8
  179. package/src/tools/__tests__/skillCatalog.test.ts +161 -0
  180. package/src/tools/__tests__/subagentHooks.test.ts +215 -0
  181. package/src/tools/__tests__/toolOutputReferences.test.ts +415 -0
  182. package/src/tools/skillCatalog.ts +126 -0
  183. package/src/tools/subagent/SubagentExecutor.ts +676 -0
  184. package/src/tools/subagent/index.ts +13 -0
  185. package/src/tools/toolOutputReferences.ts +590 -0
  186. package/src/types/graph.ts +80 -1
  187. package/src/types/index.ts +1 -0
  188. package/src/types/run.ts +28 -0
  189. package/src/types/skill.ts +11 -0
  190. package/src/types/tools.ts +112 -10
  191. package/src/utils/__tests__/truncation.test.ts +66 -0
  192. package/src/utils/truncation.ts +30 -0
@@ -0,0 +1,475 @@
1
+ 'use strict';
2
+
3
+ var truncation = require('../utils/truncation.cjs');
4
+
5
+ /**
6
+ * Tool output reference registry.
7
+ *
8
+ * When enabled via `RunConfig.toolOutputReferences.enabled`, ToolNode
9
+ * stores each successful tool output under a stable key
10
+ * (`tool<idx>turn<turn>`) where `idx` is the tool's position within a
11
+ * ToolNode batch and `turn` is the batch index within the run
12
+ * (incremented once per ToolNode invocation).
13
+ *
14
+ * Subsequent tool calls can pipe a previous output into their args by
15
+ * embedding `{{tool<idx>turn<turn>}}` inside any string argument;
16
+ * {@link ToolOutputReferenceRegistry.resolve} walks the args and
17
+ * substitutes the placeholders immediately before invocation.
18
+ *
19
+ * The registry stores the *raw, untruncated* tool output so a later
20
+ * `{{…}}` substitution pipes the full payload into the next tool —
21
+ * even when the LLM only saw a head+tail-truncated preview in
22
+ * `ToolMessage.content`. Outputs are stored without any annotation
23
+ * (the `_ref` key or the `[ref: ...]` prefix seen by the LLM is
24
+ * strictly a UX signal attached to `ToolMessage.content`). Keeping the
25
+ * registry pristine means downstream bash/jq piping receives the
26
+ * complete, verbatim output with no injected fields.
27
+ */
28
+ /** Object key used when a parsed-object output has `_ref` injected. */
29
+ const TOOL_OUTPUT_REF_KEY = '_ref';
30
+ /**
31
+ * Object key used to carry unresolved reference warnings on a parsed-
32
+ * object output. Using a dedicated field instead of a trailing text
33
+ * line keeps the annotated `ToolMessage.content` parseable as JSON for
34
+ * downstream consumers that rely on the object shape.
35
+ */
36
+ const TOOL_OUTPUT_UNRESOLVED_KEY = '_unresolved_refs';
37
+ /** Single-line prefix prepended to non-object tool outputs so the LLM sees the reference key. */
38
+ function buildReferencePrefix(key) {
39
+ return `[ref: ${key}]`;
40
+ }
41
+ /** Stable registry key for a tool output. */
42
+ function buildReferenceKey(toolIndex, turn) {
43
+ return `tool${toolIndex}turn${turn}`;
44
+ }
45
+ const EMPTY_ENTRIES = new Map();
46
+ /**
47
+ * Per-run state bucket held inside the registry. Each distinct
48
+ * `run_id` gets its own bucket so overlapping concurrent runs on a
49
+ * shared registry cannot leak outputs, turn counters, or warn-memos
50
+ * into one another.
51
+ */
52
+ class RunStateBucket {
53
+ entries = new Map();
54
+ totalSize = 0;
55
+ turnCounter = 0;
56
+ warnedNonStringTools = new Set();
57
+ }
58
+ /**
59
+ * Anonymous (`run_id` absent) bucket key. Anonymous batches are
60
+ * treated as fresh runs on every invocation — see `nextTurn`.
61
+ */
62
+ const ANON_RUN_KEY = '\0anon';
63
+ /**
64
+ * Default upper bound on the number of concurrently-tracked runs per
65
+ * registry. When exceeded, the oldest run's bucket (by insertion
66
+ * order) is evicted. Keeps memory bounded when a ToolNode is reused
67
+ * across many runs without explicit `releaseRun` calls.
68
+ */
69
+ const DEFAULT_MAX_ACTIVE_RUNS = 32;
70
+ /**
71
+ * Ordered map of reference-key → stored output, partitioned by run so
72
+ * concurrent / interleaved runs sharing one registry cannot leak
73
+ * outputs between each other.
74
+ *
75
+ * Each public method takes a `runId` which selects the run's bucket.
76
+ * Hosts typically get one registry per run via `Graph`, in which
77
+ * case only a single bucket is ever populated; the partitioning
78
+ * exists so the registry also behaves correctly when a single
79
+ * instance is reused directly.
80
+ */
81
+ class ToolOutputReferenceRegistry {
82
+ runStates = new Map();
83
+ maxOutputSize;
84
+ maxTotalSize;
85
+ maxActiveRuns;
86
+ /**
87
+ * Local stateful matcher used only by `replaceInString`. Kept
88
+ * off-module so callers of the exported `TOOL_OUTPUT_REF_PATTERN`
89
+ * never see a stale `lastIndex`.
90
+ */
91
+ static PLACEHOLDER_MATCHER = /\{\{(tool\d+turn\d+)\}\}/g;
92
+ constructor(options = {}) {
93
+ /**
94
+ * Per-output default is the same ~400 KB budget as the standard
95
+ * tool-result truncation (`HARD_MAX_TOOL_RESULT_CHARS`). This
96
+ * keeps a single `{{…}}` substitution at a size that is safe to
97
+ * pass through typical shell `ARG_MAX` limits and matches what
98
+ * the LLM would otherwise have seen. Hosts that want larger per-
99
+ * output payloads (API consumers, long JSON streams) can raise
100
+ * the cap explicitly up to the 5 MB total budget.
101
+ */
102
+ const perOutput = options.maxOutputSize != null && options.maxOutputSize > 0
103
+ ? options.maxOutputSize
104
+ : truncation.HARD_MAX_TOOL_RESULT_CHARS;
105
+ /**
106
+ * Clamp a caller-supplied `maxTotalSize` to
107
+ * `HARD_MAX_TOTAL_TOOL_OUTPUT_SIZE` (5 MB) so the documented
108
+ * absolute cap is enforced regardless of host config —
109
+ * `calculateMaxTotalToolOutputSize` already applies the same
110
+ * upper bound on its computed default, but the user-provided
111
+ * branch was bypassing it.
112
+ */
113
+ const totalRaw = options.maxTotalSize != null && options.maxTotalSize > 0
114
+ ? Math.min(options.maxTotalSize, truncation.HARD_MAX_TOTAL_TOOL_OUTPUT_SIZE)
115
+ : truncation.calculateMaxTotalToolOutputSize(perOutput);
116
+ this.maxTotalSize = totalRaw;
117
+ /**
118
+ * The per-output cap can never exceed the per-run aggregate cap:
119
+ * if a single entry were allowed to be larger than `maxTotalSize`,
120
+ * the eviction loop would either blow the cap (to keep the entry)
121
+ * or self-evict a just-stored value. Clamping here turns
122
+ * `maxTotalSize` into a hard upper bound on *any* state the
123
+ * registry retains per run.
124
+ */
125
+ this.maxOutputSize = Math.min(perOutput, totalRaw);
126
+ this.maxActiveRuns =
127
+ options.maxActiveRuns != null && options.maxActiveRuns > 0
128
+ ? options.maxActiveRuns
129
+ : DEFAULT_MAX_ACTIVE_RUNS;
130
+ }
131
+ keyFor(runId) {
132
+ return runId ?? ANON_RUN_KEY;
133
+ }
134
+ getOrCreate(runId) {
135
+ const key = this.keyFor(runId);
136
+ let state = this.runStates.get(key);
137
+ if (state == null) {
138
+ state = new RunStateBucket();
139
+ this.runStates.set(key, state);
140
+ if (this.runStates.size > this.maxActiveRuns) {
141
+ const oldest = this.runStates.keys().next().value;
142
+ if (oldest != null && oldest !== key) {
143
+ this.runStates.delete(oldest);
144
+ }
145
+ }
146
+ }
147
+ return state;
148
+ }
149
+ /** Registers (or replaces) the output stored under `key` for `runId`. */
150
+ set(runId, key, value) {
151
+ const bucket = this.getOrCreate(runId);
152
+ const clipped = value.length > this.maxOutputSize
153
+ ? value.slice(0, this.maxOutputSize)
154
+ : value;
155
+ const existing = bucket.entries.get(key);
156
+ if (existing != null) {
157
+ bucket.totalSize -= existing.length;
158
+ bucket.entries.delete(key);
159
+ }
160
+ bucket.entries.set(key, clipped);
161
+ bucket.totalSize += clipped.length;
162
+ this.evictWithinBucket(bucket);
163
+ }
164
+ /** Returns the stored value for `key` in `runId`'s bucket, or `undefined`. */
165
+ get(runId, key) {
166
+ return this.runStates.get(this.keyFor(runId))?.entries.get(key);
167
+ }
168
+ /** Total number of registered outputs across every run bucket. */
169
+ get size() {
170
+ let n = 0;
171
+ for (const bucket of this.runStates.values()) {
172
+ n += bucket.entries.size;
173
+ }
174
+ return n;
175
+ }
176
+ /** Maximum characters retained per output (post-clip). */
177
+ get perOutputLimit() {
178
+ return this.maxOutputSize;
179
+ }
180
+ /** Maximum total characters retained *per run*. */
181
+ get totalLimit() {
182
+ return this.maxTotalSize;
183
+ }
184
+ /** Drops every run's state. */
185
+ clear() {
186
+ this.runStates.clear();
187
+ }
188
+ /**
189
+ * Explicitly release `runId`'s state. Safe to call when a run has
190
+ * finished. Hosts sharing one registry across runs should call this
191
+ * to reclaim memory deterministically; otherwise LRU eviction kicks
192
+ * in when `maxActiveRuns` runs accumulate.
193
+ */
194
+ releaseRun(runId) {
195
+ this.runStates.delete(this.keyFor(runId));
196
+ }
197
+ /**
198
+ * Claims the next batch turn synchronously from `runId`'s bucket.
199
+ *
200
+ * Must be called once at the start of each ToolNode batch before
201
+ * any `await`, so concurrent invocations within the same run see
202
+ * distinct turn values (reads are effectively atomic by JS's
203
+ * single-threaded execution of the sync prefix).
204
+ *
205
+ * If `runId` is missing the anonymous bucket is dropped and a
206
+ * fresh one created so each anonymous call behaves as its own run.
207
+ */
208
+ nextTurn(runId) {
209
+ if (runId == null) {
210
+ this.runStates.delete(ANON_RUN_KEY);
211
+ }
212
+ const bucket = this.getOrCreate(runId);
213
+ return bucket.turnCounter++;
214
+ }
215
+ /**
216
+ * Records that `toolName` has been warned about in `runId` (returns
217
+ * `true` on the first call per run, `false` after). Used by
218
+ * ToolNode to emit one log line per offending tool per run when a
219
+ * `ToolMessage.content` isn't a string.
220
+ */
221
+ claimWarnOnce(runId, toolName) {
222
+ const bucket = this.getOrCreate(runId);
223
+ if (bucket.warnedNonStringTools.has(toolName)) {
224
+ return false;
225
+ }
226
+ bucket.warnedNonStringTools.add(toolName);
227
+ return true;
228
+ }
229
+ /**
230
+ * Walks `args` and replaces every `{{tool<i>turn<n>}}` placeholder in
231
+ * string values with the stored output *from `runId`'s bucket*. Non-
232
+ * string values and object keys are left untouched. Unresolved
233
+ * references are left in-place and reported so the caller can
234
+ * surface them to the LLM. When no placeholder appears anywhere in
235
+ * the serialized args, the original input is returned without
236
+ * walking the tree.
237
+ */
238
+ resolve(runId, args) {
239
+ if (!hasAnyPlaceholder(args)) {
240
+ return { resolved: args, unresolved: [] };
241
+ }
242
+ const bucket = this.runStates.get(this.keyFor(runId));
243
+ return this.resolveAgainst(bucket?.entries ?? EMPTY_ENTRIES, args);
244
+ }
245
+ /**
246
+ * Captures a frozen snapshot of `runId`'s current entries and
247
+ * returns a view that resolves placeholders against *only* that
248
+ * snapshot. The snapshot is decoupled from the live registry, so
249
+ * subsequent `set()` calls (for example, same-turn direct outputs
250
+ * registering while an event branch is still in flight) are
251
+ * invisible to the snapshot's `resolve`. Used by the mixed
252
+ * direct+event dispatch path to preserve same-turn isolation when
253
+ * a `PreToolUse` hook rewrites event args after directs have
254
+ * completed.
255
+ */
256
+ snapshot(runId) {
257
+ const bucket = this.runStates.get(this.keyFor(runId));
258
+ const entries = bucket
259
+ ? new Map(bucket.entries)
260
+ : EMPTY_ENTRIES;
261
+ return {
262
+ resolve: (args) => this.resolveAgainst(entries, args),
263
+ };
264
+ }
265
+ resolveAgainst(entries, args) {
266
+ if (!hasAnyPlaceholder(args)) {
267
+ return { resolved: args, unresolved: [] };
268
+ }
269
+ const unresolved = new Set();
270
+ const resolved = this.transform(entries, args, unresolved);
271
+ return { resolved, unresolved: Array.from(unresolved) };
272
+ }
273
+ transform(entries, value, unresolved) {
274
+ if (typeof value === 'string') {
275
+ return this.replaceInString(entries, value, unresolved);
276
+ }
277
+ if (Array.isArray(value)) {
278
+ return value.map((item) => this.transform(entries, item, unresolved));
279
+ }
280
+ if (value !== null && typeof value === 'object') {
281
+ const source = value;
282
+ const next = {};
283
+ for (const [key, item] of Object.entries(source)) {
284
+ next[key] = this.transform(entries, item, unresolved);
285
+ }
286
+ return next;
287
+ }
288
+ return value;
289
+ }
290
+ replaceInString(entries, input, unresolved) {
291
+ if (input.indexOf('{{tool') === -1) {
292
+ return input;
293
+ }
294
+ return input.replace(ToolOutputReferenceRegistry.PLACEHOLDER_MATCHER, (match, key) => {
295
+ const stored = entries.get(key);
296
+ if (stored == null) {
297
+ unresolved.add(key);
298
+ return match;
299
+ }
300
+ return stored;
301
+ });
302
+ }
303
+ evictWithinBucket(bucket) {
304
+ if (bucket.totalSize <= this.maxTotalSize) {
305
+ return;
306
+ }
307
+ for (const key of bucket.entries.keys()) {
308
+ if (bucket.totalSize <= this.maxTotalSize) {
309
+ return;
310
+ }
311
+ const entry = bucket.entries.get(key);
312
+ if (entry == null) {
313
+ continue;
314
+ }
315
+ bucket.totalSize -= entry.length;
316
+ bucket.entries.delete(key);
317
+ }
318
+ }
319
+ }
320
+ /**
321
+ * Cheap pre-check: returns true if any string value in `args` contains
322
+ * the `{{tool` substring. Lets `resolve()` skip the deep tree walk (and
323
+ * its object allocations) for the common case of plain args.
324
+ */
325
+ function hasAnyPlaceholder(value) {
326
+ if (typeof value === 'string') {
327
+ return value.indexOf('{{tool') !== -1;
328
+ }
329
+ if (Array.isArray(value)) {
330
+ for (const item of value) {
331
+ if (hasAnyPlaceholder(item)) {
332
+ return true;
333
+ }
334
+ }
335
+ return false;
336
+ }
337
+ if (value !== null && typeof value === 'object') {
338
+ for (const item of Object.values(value)) {
339
+ if (hasAnyPlaceholder(item)) {
340
+ return true;
341
+ }
342
+ }
343
+ return false;
344
+ }
345
+ return false;
346
+ }
347
+ /**
348
+ * Annotates `content` with a reference key and/or unresolved-ref
349
+ * warnings so the LLM sees both alongside the tool output.
350
+ *
351
+ * Behavior:
352
+ * - If `content` parses as a plain (non-array, non-null) JSON object
353
+ * and the object does not already have a conflicting `_ref` key,
354
+ * the reference key and (when present) `_unresolved_refs` array
355
+ * are injected as object fields, preserving JSON validity for
356
+ * downstream consumers that parse the output.
357
+ * - Otherwise (string output, JSON array/primitive, parse failure,
358
+ * or `_ref` collision), a `[ref: <key>]\n` prefix line is
359
+ * prepended and unresolved refs are appended as a trailing
360
+ * `[unresolved refs: …]` line.
361
+ *
362
+ * The annotated string is what the LLM sees as `ToolMessage.content`.
363
+ * The *original* (un-annotated) value is what gets stored in the
364
+ * registry, so downstream piping remains pristine.
365
+ *
366
+ * @param content Raw (post-truncation) tool output.
367
+ * @param key Reference key for this output, or undefined when
368
+ * there is nothing to register (errors etc.).
369
+ * @param unresolved Reference keys that failed to resolve during
370
+ * argument substitution. Surfaced so the LLM can
371
+ * self-correct its next tool call.
372
+ */
373
+ function annotateToolOutputWithReference(content, key, unresolved = []) {
374
+ const hasRefKey = key != null;
375
+ const hasUnresolved = unresolved.length > 0;
376
+ if (!hasRefKey && !hasUnresolved) {
377
+ return content;
378
+ }
379
+ const trimmed = content.trimStart();
380
+ if (trimmed.startsWith('{')) {
381
+ const annotated = tryInjectRefIntoJsonObject(content, key, unresolved);
382
+ if (annotated != null) {
383
+ return annotated;
384
+ }
385
+ }
386
+ const prefix = hasRefKey ? `${buildReferencePrefix(key)}\n` : '';
387
+ const trailer = hasUnresolved
388
+ ? `\n[unresolved refs: ${unresolved.join(', ')}]`
389
+ : '';
390
+ return `${prefix}${content}${trailer}`;
391
+ }
392
+ function tryInjectRefIntoJsonObject(content, key, unresolved) {
393
+ let parsed;
394
+ try {
395
+ parsed = JSON.parse(content);
396
+ }
397
+ catch {
398
+ return null;
399
+ }
400
+ if (parsed === null || typeof parsed !== 'object' || Array.isArray(parsed)) {
401
+ return null;
402
+ }
403
+ const obj = parsed;
404
+ const injectingRef = key != null;
405
+ const injectingUnresolved = unresolved.length > 0;
406
+ /**
407
+ * Reject the JSON-injection path (fall back to prefix form) when
408
+ * either of our keys collides with real payload data:
409
+ * - `_ref` collision: existing value is non-null and differs from
410
+ * the key we're about to inject.
411
+ * - `_unresolved_refs` collision: existing value is non-null and
412
+ * is not a deep-equal match for the array we'd inject.
413
+ * This keeps us from silently overwriting legitimate tool output.
414
+ */
415
+ if (injectingRef &&
416
+ TOOL_OUTPUT_REF_KEY in obj &&
417
+ obj[TOOL_OUTPUT_REF_KEY] !== key &&
418
+ obj[TOOL_OUTPUT_REF_KEY] != null) {
419
+ return null;
420
+ }
421
+ if (injectingUnresolved &&
422
+ TOOL_OUTPUT_UNRESOLVED_KEY in obj &&
423
+ obj[TOOL_OUTPUT_UNRESOLVED_KEY] != null &&
424
+ !arraysShallowEqual(obj[TOOL_OUTPUT_UNRESOLVED_KEY], unresolved)) {
425
+ return null;
426
+ }
427
+ /**
428
+ * Only strip the framework-owned key we're actually injecting —
429
+ * leave everything else (including a pre-existing `_ref` on the
430
+ * unresolved-only path, or a pre-existing `_unresolved_refs` on a
431
+ * plain-annotation path) untouched so we annotate rather than
432
+ * mutate downstream payload data. Our injected keys land first in
433
+ * the serialized JSON so the LLM sees them before the body.
434
+ */
435
+ const omitKeys = new Set();
436
+ if (injectingRef)
437
+ omitKeys.add(TOOL_OUTPUT_REF_KEY);
438
+ if (injectingUnresolved)
439
+ omitKeys.add(TOOL_OUTPUT_UNRESOLVED_KEY);
440
+ const rest = {};
441
+ for (const [k, v] of Object.entries(obj)) {
442
+ if (!omitKeys.has(k)) {
443
+ rest[k] = v;
444
+ }
445
+ }
446
+ const injected = {};
447
+ if (injectingRef) {
448
+ injected[TOOL_OUTPUT_REF_KEY] = key;
449
+ }
450
+ if (injectingUnresolved) {
451
+ injected[TOOL_OUTPUT_UNRESOLVED_KEY] = unresolved;
452
+ }
453
+ Object.assign(injected, rest);
454
+ const pretty = /^\{\s*\n/.test(content);
455
+ return pretty ? JSON.stringify(injected, null, 2) : JSON.stringify(injected);
456
+ }
457
+ function arraysShallowEqual(a, b) {
458
+ if (!Array.isArray(a) || a.length !== b.length) {
459
+ return false;
460
+ }
461
+ for (let i = 0; i < a.length; i++) {
462
+ if (a[i] !== b[i]) {
463
+ return false;
464
+ }
465
+ }
466
+ return true;
467
+ }
468
+
469
+ exports.TOOL_OUTPUT_REF_KEY = TOOL_OUTPUT_REF_KEY;
470
+ exports.TOOL_OUTPUT_UNRESOLVED_KEY = TOOL_OUTPUT_UNRESOLVED_KEY;
471
+ exports.ToolOutputReferenceRegistry = ToolOutputReferenceRegistry;
472
+ exports.annotateToolOutputWithReference = annotateToolOutputWithReference;
473
+ exports.buildReferenceKey = buildReferenceKey;
474
+ exports.buildReferencePrefix = buildReferencePrefix;
475
+ //# sourceMappingURL=toolOutputReferences.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"toolOutputReferences.cjs","sources":["../../../src/tools/toolOutputReferences.ts"],"sourcesContent":["/**\n * Tool output reference registry.\n *\n * When enabled via `RunConfig.toolOutputReferences.enabled`, ToolNode\n * stores each successful tool output under a stable key\n * (`tool<idx>turn<turn>`) where `idx` is the tool's position within a\n * ToolNode batch and `turn` is the batch index within the run\n * (incremented once per ToolNode invocation).\n *\n * Subsequent tool calls can pipe a previous output into their args by\n * embedding `{{tool<idx>turn<turn>}}` inside any string argument;\n * {@link ToolOutputReferenceRegistry.resolve} walks the args and\n * substitutes the placeholders immediately before invocation.\n *\n * The registry stores the *raw, untruncated* tool output so a later\n * `{{…}}` substitution pipes the full payload into the next tool —\n * even when the LLM only saw a head+tail-truncated preview in\n * `ToolMessage.content`. Outputs are stored without any annotation\n * (the `_ref` key or the `[ref: ...]` prefix seen by the LLM is\n * strictly a UX signal attached to `ToolMessage.content`). Keeping the\n * registry pristine means downstream bash/jq piping receives the\n * complete, verbatim output with no injected fields.\n */\n\nimport {\n calculateMaxTotalToolOutputSize,\n HARD_MAX_TOOL_RESULT_CHARS,\n HARD_MAX_TOTAL_TOOL_OUTPUT_SIZE,\n} from '@/utils/truncation';\n\n/**\n * Non-global matcher for a single `{{tool<i>turn<n>}}` placeholder.\n * Exported for consumers that want to detect references (e.g., syntax\n * highlighting, docs). The stateful `g` variant lives inside the\n * registry so nobody trips on `lastIndex`.\n */\nexport const TOOL_OUTPUT_REF_PATTERN = /\\{\\{(tool\\d+turn\\d+)\\}\\}/;\n\n/** Object key used when a parsed-object output has `_ref` injected. */\nexport const TOOL_OUTPUT_REF_KEY = '_ref';\n\n/**\n * Object key used to carry unresolved reference warnings on a parsed-\n * object output. Using a dedicated field instead of a trailing text\n * line keeps the annotated `ToolMessage.content` parseable as JSON for\n * downstream consumers that rely on the object shape.\n */\nexport const TOOL_OUTPUT_UNRESOLVED_KEY = '_unresolved_refs';\n\n/** Single-line prefix prepended to non-object tool outputs so the LLM sees the reference key. */\nexport function buildReferencePrefix(key: string): string {\n return `[ref: ${key}]`;\n}\n\n/** Stable registry key for a tool output. */\nexport function buildReferenceKey(toolIndex: number, turn: number): string {\n return `tool${toolIndex}turn${turn}`;\n}\n\nexport type ToolOutputReferenceRegistryOptions = {\n /** Maximum characters stored per registered output. */\n maxOutputSize?: number;\n /** Maximum total characters retained across all registered outputs. */\n maxTotalSize?: number;\n /**\n * Upper bound on the number of concurrently-tracked runs. When\n * exceeded, the oldest run bucket is evicted (FIFO). Defaults to 32.\n */\n maxActiveRuns?: number;\n};\n\n/**\n * Result of resolving placeholders in tool args.\n */\nexport type ResolveResult<T> = {\n /** Arguments with placeholders replaced. Same shape as the input. */\n resolved: T;\n /** Reference keys that were referenced but had no stored value. */\n unresolved: string[];\n};\n\n/**\n * Read-only view over a frozen registry snapshot. Returned by\n * {@link ToolOutputReferenceRegistry.snapshot} for callers that need\n * to resolve placeholders against the registry state at a specific\n * point in time, ignoring any subsequent registrations.\n */\nexport interface ToolOutputResolveView {\n resolve<T>(args: T): ResolveResult<T>;\n}\n\n/**\n * Pre-resolved arg map keyed by `toolCallId`. Used by the mixed\n * direct+event dispatch path to feed event calls' resolved args\n * (captured pre-batch) into the dispatcher without re-resolving\n * against the now-stale live registry.\n */\nexport type PreResolvedArgsMap = Map<\n string,\n { resolved: Record<string, unknown>; unresolved: string[] }\n>;\n\n/**\n * Per-call sink for resolved args, keyed by `toolCallId`. Threaded\n * as a per-batch local map so concurrent `ToolNode.run()` calls do\n * not race on shared sink state.\n */\nexport type ResolvedArgsByCallId = Map<string, Record<string, unknown>>;\n\nconst EMPTY_ENTRIES: ReadonlyMap<string, string> = new Map<string, string>();\n\n/**\n * Per-run state bucket held inside the registry. Each distinct\n * `run_id` gets its own bucket so overlapping concurrent runs on a\n * shared registry cannot leak outputs, turn counters, or warn-memos\n * into one another.\n */\nclass RunStateBucket {\n entries: Map<string, string> = new Map();\n totalSize: number = 0;\n turnCounter: number = 0;\n warnedNonStringTools: Set<string> = new Set();\n}\n\n/**\n * Anonymous (`run_id` absent) bucket key. Anonymous batches are\n * treated as fresh runs on every invocation — see `nextTurn`.\n */\nconst ANON_RUN_KEY = '\\0anon';\n\n/**\n * Default upper bound on the number of concurrently-tracked runs per\n * registry. When exceeded, the oldest run's bucket (by insertion\n * order) is evicted. Keeps memory bounded when a ToolNode is reused\n * across many runs without explicit `releaseRun` calls.\n */\nconst DEFAULT_MAX_ACTIVE_RUNS = 32;\n\n/**\n * Ordered map of reference-key → stored output, partitioned by run so\n * concurrent / interleaved runs sharing one registry cannot leak\n * outputs between each other.\n *\n * Each public method takes a `runId` which selects the run's bucket.\n * Hosts typically get one registry per run via `Graph`, in which\n * case only a single bucket is ever populated; the partitioning\n * exists so the registry also behaves correctly when a single\n * instance is reused directly.\n */\nexport class ToolOutputReferenceRegistry {\n private runStates: Map<string, RunStateBucket> = new Map();\n private readonly maxOutputSize: number;\n private readonly maxTotalSize: number;\n private readonly maxActiveRuns: number;\n /**\n * Local stateful matcher used only by `replaceInString`. Kept\n * off-module so callers of the exported `TOOL_OUTPUT_REF_PATTERN`\n * never see a stale `lastIndex`.\n */\n private static readonly PLACEHOLDER_MATCHER = /\\{\\{(tool\\d+turn\\d+)\\}\\}/g;\n\n constructor(options: ToolOutputReferenceRegistryOptions = {}) {\n /**\n * Per-output default is the same ~400 KB budget as the standard\n * tool-result truncation (`HARD_MAX_TOOL_RESULT_CHARS`). This\n * keeps a single `{{…}}` substitution at a size that is safe to\n * pass through typical shell `ARG_MAX` limits and matches what\n * the LLM would otherwise have seen. Hosts that want larger per-\n * output payloads (API consumers, long JSON streams) can raise\n * the cap explicitly up to the 5 MB total budget.\n */\n const perOutput =\n options.maxOutputSize != null && options.maxOutputSize > 0\n ? options.maxOutputSize\n : HARD_MAX_TOOL_RESULT_CHARS;\n /**\n * Clamp a caller-supplied `maxTotalSize` to\n * `HARD_MAX_TOTAL_TOOL_OUTPUT_SIZE` (5 MB) so the documented\n * absolute cap is enforced regardless of host config —\n * `calculateMaxTotalToolOutputSize` already applies the same\n * upper bound on its computed default, but the user-provided\n * branch was bypassing it.\n */\n const totalRaw =\n options.maxTotalSize != null && options.maxTotalSize > 0\n ? Math.min(options.maxTotalSize, HARD_MAX_TOTAL_TOOL_OUTPUT_SIZE)\n : calculateMaxTotalToolOutputSize(perOutput);\n this.maxTotalSize = totalRaw;\n /**\n * The per-output cap can never exceed the per-run aggregate cap:\n * if a single entry were allowed to be larger than `maxTotalSize`,\n * the eviction loop would either blow the cap (to keep the entry)\n * or self-evict a just-stored value. Clamping here turns\n * `maxTotalSize` into a hard upper bound on *any* state the\n * registry retains per run.\n */\n this.maxOutputSize = Math.min(perOutput, totalRaw);\n this.maxActiveRuns =\n options.maxActiveRuns != null && options.maxActiveRuns > 0\n ? options.maxActiveRuns\n : DEFAULT_MAX_ACTIVE_RUNS;\n }\n\n private keyFor(runId: string | undefined): string {\n return runId ?? ANON_RUN_KEY;\n }\n\n private getOrCreate(runId: string | undefined): RunStateBucket {\n const key = this.keyFor(runId);\n let state = this.runStates.get(key);\n if (state == null) {\n state = new RunStateBucket();\n this.runStates.set(key, state);\n if (this.runStates.size > this.maxActiveRuns) {\n const oldest = this.runStates.keys().next().value;\n if (oldest != null && oldest !== key) {\n this.runStates.delete(oldest);\n }\n }\n }\n return state;\n }\n\n /** Registers (or replaces) the output stored under `key` for `runId`. */\n set(runId: string | undefined, key: string, value: string): void {\n const bucket = this.getOrCreate(runId);\n const clipped =\n value.length > this.maxOutputSize\n ? value.slice(0, this.maxOutputSize)\n : value;\n const existing = bucket.entries.get(key);\n if (existing != null) {\n bucket.totalSize -= existing.length;\n bucket.entries.delete(key);\n }\n bucket.entries.set(key, clipped);\n bucket.totalSize += clipped.length;\n this.evictWithinBucket(bucket);\n }\n\n /** Returns the stored value for `key` in `runId`'s bucket, or `undefined`. */\n get(runId: string | undefined, key: string): string | undefined {\n return this.runStates.get(this.keyFor(runId))?.entries.get(key);\n }\n\n /** Total number of registered outputs across every run bucket. */\n get size(): number {\n let n = 0;\n for (const bucket of this.runStates.values()) {\n n += bucket.entries.size;\n }\n return n;\n }\n\n /** Maximum characters retained per output (post-clip). */\n get perOutputLimit(): number {\n return this.maxOutputSize;\n }\n\n /** Maximum total characters retained *per run*. */\n get totalLimit(): number {\n return this.maxTotalSize;\n }\n\n /** Drops every run's state. */\n clear(): void {\n this.runStates.clear();\n }\n\n /**\n * Explicitly release `runId`'s state. Safe to call when a run has\n * finished. Hosts sharing one registry across runs should call this\n * to reclaim memory deterministically; otherwise LRU eviction kicks\n * in when `maxActiveRuns` runs accumulate.\n */\n releaseRun(runId: string | undefined): void {\n this.runStates.delete(this.keyFor(runId));\n }\n\n /**\n * Claims the next batch turn synchronously from `runId`'s bucket.\n *\n * Must be called once at the start of each ToolNode batch before\n * any `await`, so concurrent invocations within the same run see\n * distinct turn values (reads are effectively atomic by JS's\n * single-threaded execution of the sync prefix).\n *\n * If `runId` is missing the anonymous bucket is dropped and a\n * fresh one created so each anonymous call behaves as its own run.\n */\n nextTurn(runId: string | undefined): number {\n if (runId == null) {\n this.runStates.delete(ANON_RUN_KEY);\n }\n const bucket = this.getOrCreate(runId);\n return bucket.turnCounter++;\n }\n\n /**\n * Records that `toolName` has been warned about in `runId` (returns\n * `true` on the first call per run, `false` after). Used by\n * ToolNode to emit one log line per offending tool per run when a\n * `ToolMessage.content` isn't a string.\n */\n claimWarnOnce(runId: string | undefined, toolName: string): boolean {\n const bucket = this.getOrCreate(runId);\n if (bucket.warnedNonStringTools.has(toolName)) {\n return false;\n }\n bucket.warnedNonStringTools.add(toolName);\n return true;\n }\n\n /**\n * Walks `args` and replaces every `{{tool<i>turn<n>}}` placeholder in\n * string values with the stored output *from `runId`'s bucket*. Non-\n * string values and object keys are left untouched. Unresolved\n * references are left in-place and reported so the caller can\n * surface them to the LLM. When no placeholder appears anywhere in\n * the serialized args, the original input is returned without\n * walking the tree.\n */\n resolve<T>(runId: string | undefined, args: T): ResolveResult<T> {\n if (!hasAnyPlaceholder(args)) {\n return { resolved: args, unresolved: [] };\n }\n const bucket = this.runStates.get(this.keyFor(runId));\n return this.resolveAgainst(bucket?.entries ?? EMPTY_ENTRIES, args);\n }\n\n /**\n * Captures a frozen snapshot of `runId`'s current entries and\n * returns a view that resolves placeholders against *only* that\n * snapshot. The snapshot is decoupled from the live registry, so\n * subsequent `set()` calls (for example, same-turn direct outputs\n * registering while an event branch is still in flight) are\n * invisible to the snapshot's `resolve`. Used by the mixed\n * direct+event dispatch path to preserve same-turn isolation when\n * a `PreToolUse` hook rewrites event args after directs have\n * completed.\n */\n snapshot(runId: string | undefined): ToolOutputResolveView {\n const bucket = this.runStates.get(this.keyFor(runId));\n const entries: ReadonlyMap<string, string> = bucket\n ? new Map(bucket.entries)\n : EMPTY_ENTRIES;\n return {\n resolve: <T>(args: T): ResolveResult<T> =>\n this.resolveAgainst(entries, args),\n };\n }\n\n private resolveAgainst<T>(\n entries: ReadonlyMap<string, string>,\n args: T\n ): ResolveResult<T> {\n if (!hasAnyPlaceholder(args)) {\n return { resolved: args, unresolved: [] };\n }\n const unresolved = new Set<string>();\n const resolved = this.transform(entries, args, unresolved) as T;\n return { resolved, unresolved: Array.from(unresolved) };\n }\n\n private transform(\n entries: ReadonlyMap<string, string>,\n value: unknown,\n unresolved: Set<string>\n ): unknown {\n if (typeof value === 'string') {\n return this.replaceInString(entries, value, unresolved);\n }\n if (Array.isArray(value)) {\n return value.map((item) => this.transform(entries, item, unresolved));\n }\n if (value !== null && typeof value === 'object') {\n const source = value as Record<string, unknown>;\n const next: Record<string, unknown> = {};\n for (const [key, item] of Object.entries(source)) {\n next[key] = this.transform(entries, item, unresolved);\n }\n return next;\n }\n return value;\n }\n\n private replaceInString(\n entries: ReadonlyMap<string, string>,\n input: string,\n unresolved: Set<string>\n ): string {\n if (input.indexOf('{{tool') === -1) {\n return input;\n }\n return input.replace(\n ToolOutputReferenceRegistry.PLACEHOLDER_MATCHER,\n (match, key: string) => {\n const stored = entries.get(key);\n if (stored == null) {\n unresolved.add(key);\n return match;\n }\n return stored;\n }\n );\n }\n\n private evictWithinBucket(bucket: RunStateBucket): void {\n if (bucket.totalSize <= this.maxTotalSize) {\n return;\n }\n for (const key of bucket.entries.keys()) {\n if (bucket.totalSize <= this.maxTotalSize) {\n return;\n }\n const entry = bucket.entries.get(key);\n if (entry == null) {\n continue;\n }\n bucket.totalSize -= entry.length;\n bucket.entries.delete(key);\n }\n }\n}\n\n/**\n * Cheap pre-check: returns true if any string value in `args` contains\n * the `{{tool` substring. Lets `resolve()` skip the deep tree walk (and\n * its object allocations) for the common case of plain args.\n */\nfunction hasAnyPlaceholder(value: unknown): boolean {\n if (typeof value === 'string') {\n return value.indexOf('{{tool') !== -1;\n }\n if (Array.isArray(value)) {\n for (const item of value) {\n if (hasAnyPlaceholder(item)) {\n return true;\n }\n }\n return false;\n }\n if (value !== null && typeof value === 'object') {\n for (const item of Object.values(value as Record<string, unknown>)) {\n if (hasAnyPlaceholder(item)) {\n return true;\n }\n }\n return false;\n }\n return false;\n}\n\n/**\n * Annotates `content` with a reference key and/or unresolved-ref\n * warnings so the LLM sees both alongside the tool output.\n *\n * Behavior:\n * - If `content` parses as a plain (non-array, non-null) JSON object\n * and the object does not already have a conflicting `_ref` key,\n * the reference key and (when present) `_unresolved_refs` array\n * are injected as object fields, preserving JSON validity for\n * downstream consumers that parse the output.\n * - Otherwise (string output, JSON array/primitive, parse failure,\n * or `_ref` collision), a `[ref: <key>]\\n` prefix line is\n * prepended and unresolved refs are appended as a trailing\n * `[unresolved refs: …]` line.\n *\n * The annotated string is what the LLM sees as `ToolMessage.content`.\n * The *original* (un-annotated) value is what gets stored in the\n * registry, so downstream piping remains pristine.\n *\n * @param content Raw (post-truncation) tool output.\n * @param key Reference key for this output, or undefined when\n * there is nothing to register (errors etc.).\n * @param unresolved Reference keys that failed to resolve during\n * argument substitution. Surfaced so the LLM can\n * self-correct its next tool call.\n */\nexport function annotateToolOutputWithReference(\n content: string,\n key: string | undefined,\n unresolved: string[] = []\n): string {\n const hasRefKey = key != null;\n const hasUnresolved = unresolved.length > 0;\n if (!hasRefKey && !hasUnresolved) {\n return content;\n }\n const trimmed = content.trimStart();\n if (trimmed.startsWith('{')) {\n const annotated = tryInjectRefIntoJsonObject(content, key, unresolved);\n if (annotated != null) {\n return annotated;\n }\n }\n const prefix = hasRefKey ? `${buildReferencePrefix(key!)}\\n` : '';\n const trailer = hasUnresolved\n ? `\\n[unresolved refs: ${unresolved.join(', ')}]`\n : '';\n return `${prefix}${content}${trailer}`;\n}\n\nfunction tryInjectRefIntoJsonObject(\n content: string,\n key: string | undefined,\n unresolved: string[]\n): string | null {\n let parsed: unknown;\n try {\n parsed = JSON.parse(content);\n } catch {\n return null;\n }\n\n if (parsed === null || typeof parsed !== 'object' || Array.isArray(parsed)) {\n return null;\n }\n\n const obj = parsed as Record<string, unknown>;\n const injectingRef = key != null;\n const injectingUnresolved = unresolved.length > 0;\n\n /**\n * Reject the JSON-injection path (fall back to prefix form) when\n * either of our keys collides with real payload data:\n * - `_ref` collision: existing value is non-null and differs from\n * the key we're about to inject.\n * - `_unresolved_refs` collision: existing value is non-null and\n * is not a deep-equal match for the array we'd inject.\n * This keeps us from silently overwriting legitimate tool output.\n */\n if (\n injectingRef &&\n TOOL_OUTPUT_REF_KEY in obj &&\n obj[TOOL_OUTPUT_REF_KEY] !== key &&\n obj[TOOL_OUTPUT_REF_KEY] != null\n ) {\n return null;\n }\n if (\n injectingUnresolved &&\n TOOL_OUTPUT_UNRESOLVED_KEY in obj &&\n obj[TOOL_OUTPUT_UNRESOLVED_KEY] != null &&\n !arraysShallowEqual(obj[TOOL_OUTPUT_UNRESOLVED_KEY], unresolved)\n ) {\n return null;\n }\n\n /**\n * Only strip the framework-owned key we're actually injecting —\n * leave everything else (including a pre-existing `_ref` on the\n * unresolved-only path, or a pre-existing `_unresolved_refs` on a\n * plain-annotation path) untouched so we annotate rather than\n * mutate downstream payload data. Our injected keys land first in\n * the serialized JSON so the LLM sees them before the body.\n */\n const omitKeys = new Set<string>();\n if (injectingRef) omitKeys.add(TOOL_OUTPUT_REF_KEY);\n if (injectingUnresolved) omitKeys.add(TOOL_OUTPUT_UNRESOLVED_KEY);\n const rest: Record<string, unknown> = {};\n for (const [k, v] of Object.entries(obj)) {\n if (!omitKeys.has(k)) {\n rest[k] = v;\n }\n }\n const injected: Record<string, unknown> = {};\n if (injectingRef) {\n injected[TOOL_OUTPUT_REF_KEY] = key;\n }\n if (injectingUnresolved) {\n injected[TOOL_OUTPUT_UNRESOLVED_KEY] = unresolved;\n }\n Object.assign(injected, rest);\n\n const pretty = /^\\{\\s*\\n/.test(content);\n return pretty ? JSON.stringify(injected, null, 2) : JSON.stringify(injected);\n}\n\nfunction arraysShallowEqual(a: unknown, b: readonly string[]): boolean {\n if (!Array.isArray(a) || a.length !== b.length) {\n return false;\n }\n for (let i = 0; i < a.length; i++) {\n if (a[i] !== b[i]) {\n return false;\n }\n }\n return true;\n}\n"],"names":["HARD_MAX_TOOL_RESULT_CHARS","HARD_MAX_TOTAL_TOOL_OUTPUT_SIZE","calculateMaxTotalToolOutputSize"],"mappings":";;;;AAAA;;;;;;;;;;;;;;;;;;;;;;AAsBG;AAgBH;AACO,MAAM,mBAAmB,GAAG;AAEnC;;;;;AAKG;AACI,MAAM,0BAA0B,GAAG;AAE1C;AACM,SAAU,oBAAoB,CAAC,GAAW,EAAA;IAC9C,OAAO,CAAA,MAAA,EAAS,GAAG,CAAA,CAAA,CAAG;AACxB;AAEA;AACM,SAAU,iBAAiB,CAAC,SAAiB,EAAE,IAAY,EAAA;AAC/D,IAAA,OAAO,CAAA,IAAA,EAAO,SAAS,CAAA,IAAA,EAAO,IAAI,EAAE;AACtC;AAoDA,MAAM,aAAa,GAAgC,IAAI,GAAG,EAAkB;AAE5E;;;;;AAKG;AACH,MAAM,cAAc,CAAA;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AmB,CAAC,GAAG,GAAG;IACrC;IACA,IAAI,mBAAmB,EAAE;AACvB,QAAA,QAAQ,CAAC,0BAA0B,CAAC,GAAG,UAAU;IACnD;AACA,IAAA,MAAM,CAAC,MAAM,CAAC,QAAQ,EAAE,IAAI,CAAC;IAE7B,MAAM,MAAM,GAAG,UAAU,CAAC,IAAI,CAAC,OAAO,CAAC;IACvC,OAAO,MAAM,GAAG,IAAI,CAAC,SAAS,CAAC,QAAQ,EAAE,IAAI,EAAE,CAAC,CAAC,GAAG,IAAI,CAAC,SAAS,CAAC,QAAQ,CAAC;AAC9E;AAEA,SAAS,kBAAkB,CAAC,CAAU,EAAE,CAAoB,EAAA;AAC1D,IAAA,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,MAAM,KAAK,CAAC,CAAC,MAAM,EAAE;AAC9C,QAAA,OAAO,KAAK;IACd;AACA,IAAA,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,CAAC,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE;QACjC,IAAI,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,EAAE;AACjB,YAAA,OAAO,KAAK;QACd;IACF;AACA,IAAA,OAAO,IAAI;AACb;;;;;;;;;"}
@@ -12,6 +12,15 @@
12
12
  * larger than this is almost certainly a bug (e.g., dumping a binary file).
13
13
  */
14
14
  const HARD_MAX_TOOL_RESULT_CHARS = 400_000;
15
+ /**
16
+ * Absolute hard cap on the aggregate size (characters) of all registered
17
+ * tool outputs kept for `{{tool<i>turn<n>}}` substitution. Set at 5 MB
18
+ * because the registry stores *raw, untruncated* tool output — full
19
+ * fidelity for piping into downstream bash/jq — so the budget needs
20
+ * enough headroom to keep a handful of large responses without
21
+ * ballooning unbounded.
22
+ */
23
+ const HARD_MAX_TOTAL_TOOL_OUTPUT_SIZE = 5_000_000;
15
24
  /**
16
25
  * Computes the dynamic max tool result size based on the model's context window.
17
26
  * Uses 30% of the context window (in estimated characters, ~4 chars/token)
@@ -26,6 +35,23 @@ function calculateMaxToolResultChars(contextWindowTokens) {
26
35
  }
27
36
  return Math.min(Math.floor(contextWindowTokens * 0.3) * 4, HARD_MAX_TOOL_RESULT_CHARS);
28
37
  }
38
+ /**
39
+ * Computes the default aggregate size (characters) for the tool output
40
+ * reference registry based on the per-output budget. Mirrors
41
+ * `calculateMaxToolResultChars`'s shape: a multiple of the per-output
42
+ * cap, clamped to `HARD_MAX_TOTAL_TOOL_OUTPUT_SIZE`.
43
+ *
44
+ * @param maxOutputSize - Per-output maximum characters (e.g., the
45
+ * ToolNode's `maxToolResultChars`). When omitted or non-positive,
46
+ * falls back to the absolute total cap.
47
+ * @returns Maximum total characters retained across the registry.
48
+ */
49
+ function calculateMaxTotalToolOutputSize(maxOutputSize) {
50
+ if (maxOutputSize == null || maxOutputSize <= 0) {
51
+ return HARD_MAX_TOTAL_TOOL_OUTPUT_SIZE;
52
+ }
53
+ return Math.min(maxOutputSize * 2, HARD_MAX_TOTAL_TOOL_OUTPUT_SIZE);
54
+ }
29
55
  /**
30
56
  * Truncates a tool-call input (the arguments/payload of a tool_use block)
31
57
  * using head+tail strategy. Returns an object with `_truncated` (the
@@ -101,7 +127,9 @@ function truncateToolResultContent(content, maxChars) {
101
127
  }
102
128
 
103
129
  exports.HARD_MAX_TOOL_RESULT_CHARS = HARD_MAX_TOOL_RESULT_CHARS;
130
+ exports.HARD_MAX_TOTAL_TOOL_OUTPUT_SIZE = HARD_MAX_TOTAL_TOOL_OUTPUT_SIZE;
104
131
  exports.calculateMaxToolResultChars = calculateMaxToolResultChars;
132
+ exports.calculateMaxTotalToolOutputSize = calculateMaxTotalToolOutputSize;
105
133
  exports.truncateToolInput = truncateToolInput;
106
134
  exports.truncateToolResultContent = truncateToolResultContent;
107
135
  //# sourceMappingURL=truncation.cjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"truncation.cjs","sources":["../../../src/utils/truncation.ts"],"sourcesContent":["/**\n * Ingestion-time and pre-flight truncation utilities for tool results.\n *\n * Prevents oversized tool outputs from entering the message array and\n * consuming the entire context window.\n */\n\n/**\n * Absolute hard cap on tool result length (characters).\n * Even if the model has a 1M-token context, a single tool result\n * larger than this is almost certainly a bug (e.g., dumping a binary file).\n */\nexport const HARD_MAX_TOOL_RESULT_CHARS = 400_000;\n\n/**\n * Computes the dynamic max tool result size based on the model's context window.\n * Uses 30% of the context window (in estimated characters, ~4 chars/token)\n * capped at HARD_MAX_TOOL_RESULT_CHARS.\n *\n * @param contextWindowTokens - The model's max context tokens (optional).\n * @returns Maximum allowed characters for a single tool result.\n */\nexport function calculateMaxToolResultChars(\n contextWindowTokens?: number\n): number {\n if (contextWindowTokens == null || contextWindowTokens <= 0) {\n return HARD_MAX_TOOL_RESULT_CHARS;\n }\n return Math.min(\n Math.floor(contextWindowTokens * 0.3) * 4,\n HARD_MAX_TOOL_RESULT_CHARS\n );\n}\n\n/**\n * Truncates a tool-call input (the arguments/payload of a tool_use block)\n * using head+tail strategy. Returns an object with `_truncated` (the\n * truncated string) and `_originalChars` (for diagnostics).\n *\n * Accepts any type — objects are JSON-serialized before truncation.\n *\n * @param input - The tool input (string, object, etc.).\n * @param maxChars - Maximum allowed characters.\n */\nexport function truncateToolInput(\n input: unknown,\n maxChars: number\n): { _truncated: string; _originalChars: number } {\n const serialized = typeof input === 'string' ? input : JSON.stringify(input);\n if (serialized.length <= maxChars) {\n return { _truncated: serialized, _originalChars: serialized.length };\n }\n const indicator = `\\n… [truncated: ${serialized.length} chars exceeded ${maxChars} limit] …\\n`;\n const available = maxChars - indicator.length;\n\n if (available < 100) {\n return {\n _truncated: serialized.slice(0, maxChars) + indicator.trimEnd(),\n _originalChars: serialized.length,\n };\n }\n\n const headSize = Math.ceil(available * 0.7);\n const tailSize = available - headSize;\n\n return {\n _truncated:\n serialized.slice(0, headSize) +\n indicator +\n serialized.slice(serialized.length - tailSize),\n _originalChars: serialized.length,\n };\n}\n\n/**\n * Truncates tool result content that exceeds `maxChars` using a head+tail\n * strategy. Keeps the beginning (structure/headers) and end (return value /\n * conclusion) of the content so the model retains both the opening context\n * and the final outcome.\n *\n * Head gets ~70% of the budget, tail gets ~30%. Falls back to head-only\n * when the budget is too small for a meaningful tail.\n *\n * @param content - The tool result string content.\n * @param maxChars - Maximum allowed characters.\n * @returns The (possibly truncated) content string.\n */\nexport function truncateToolResultContent(\n content: string,\n maxChars: number\n): string {\n if (content.length <= maxChars) {\n return content;\n }\n\n const indicator = `\\n\\n… [truncated: ${content.length} chars exceeded ${maxChars} limit] …\\n\\n`;\n const available = maxChars - indicator.length;\n if (available <= 0) {\n return content.slice(0, maxChars);\n }\n\n // When budget is too small for a meaningful tail, fall back to head-only\n if (available < 200) {\n return content.slice(0, available) + indicator.trimEnd();\n }\n\n const headSize = Math.ceil(available * 0.7);\n const tailSize = available - headSize;\n\n // Try to break at newline boundaries for cleaner output\n let headEnd = headSize;\n const headNewline = content.lastIndexOf('\\n', headSize);\n if (headNewline > headSize - 200 && headNewline > 0) {\n headEnd = headNewline;\n }\n\n let tailStart = content.length - tailSize;\n const tailNewline = content.indexOf('\\n', tailStart);\n if (tailNewline > 0 && tailNewline < tailStart + 200) {\n tailStart = tailNewline + 1;\n }\n\n return content.slice(0, headEnd) + indicator + content.slice(tailStart);\n}\n"],"names":[],"mappings":";;AAAA;;;;;AAKG;AAEH;;;;AAIG;AACI,MAAM,0BAA0B,GAAG;AAE1C;;;;;;;AAOG;AACG,SAAU,2BAA2B,CACzC,mBAA4B,EAAA;IAE5B,IAAI,mBAAmB,IAAI,IAAI,IAAI,mBAAmB,IAAI,CAAC,EAAE;AAC3D,QAAA,OAAO,0BAA0B;IACnC;AACA,IAAA,OAAO,IAAI,CAAC,GAAG,CACb,IAAI,CAAC,KAAK,CAAC,mBAAmB,GAAG,GAAG,CAAC,GAAG,CAAC,EACzC,0BAA0B,CAC3B;AACH;AAEA;;;;;;;;;AASG;AACG,SAAU,iBAAiB,CAC/B,K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AAA,aAAA,CAAe;AAC/F,IAAA,MAAM,SAAS,GAAG,QAAQ,GAAG,SAAS,CAAC,MAAM;AAC7C,IAAA,IAAI,SAAS,IAAI,CAAC,EAAE;QAClB,OAAO,OAAO,CAAC,KAAK,CAAC,CAAC,EAAE,QAAQ,CAAC;IACnC;;AAGA,IAAA,IAAI,SAAS,GAAG,GAAG,EAAE;AACnB,QAAA,OAAO,OAAO,CAAC,KAAK,CAAC,CAAC,EAAE,SAAS,CAAC,GAAG,SAAS,CAAC,OAAO,EAAE;IAC1D;IAEA,MAAM,QAAQ,GAAG,IAAI,CAAC,IAAI,CAAC,SAAS,GAAG,GAAG,CAAC;AAC3C,IAAA,MAAM,QAAQ,GAAG,SAAS,GAAG,QAAQ;;IAGrC,IAAI,OAAO,GAAG,QAAQ;IACtB,MAAM,WAAW,GAAG,OAAO,CAAC,WAAW,CAAC,IAAI,EAAE,QAAQ,CAAC;IACvD,IAAI,WAAW,GAAG,QAAQ,GAAG,GAAG,IAAI,WAAW,GAAG,CAAC,EAAE;QACnD,OAAO,GAAG,WAAW;IACvB;AAEA,IAAA,IAAI,SAAS,GAAG,OAAO,CAAC,MAAM,GAAG,QAAQ;IACzC,MAAM,WAAW,GAAG,OAAO,CAAC,OAAO,CAAC,IAAI,EAAE,SAAS,CAAC;IACpD,IAAI,WAAW,GAAG,CAAC,IAAI,WAAW,GAAG,SAAS,GAAG,GAAG,EAAE;AACpD,QAAA,SAAS,GAAG,WAAW,GAAG,CAAC;IAC7B;AAEA,IAAA,OAAO,OAAO,CAAC,KAAK,CAAC,CAAC,EAAE,OAAO,CAAC,GAAG,SAAS,GAAG,OAAO,CAAC,KAAK,CAAC,SAAS,CAAC;AACzE;;;;;;;"}
1
+ {"version":3,"file":"truncation.cjs","sources":["../../../src/utils/truncation.ts"],"sourcesContent":["/**\n * Ingestion-time and pre-flight truncation utilities for tool results.\n *\n * Prevents oversized tool outputs from entering the message array and\n * consuming the entire context window.\n */\n\n/**\n * Absolute hard cap on tool result length (characters).\n * Even if the model has a 1M-token context, a single tool result\n * larger than this is almost certainly a bug (e.g., dumping a binary file).\n */\nexport const HARD_MAX_TOOL_RESULT_CHARS = 400_000;\n\n/**\n * Absolute hard cap on the aggregate size (characters) of all registered\n * tool outputs kept for `{{tool<i>turn<n>}}` substitution. Set at 5 MB\n * because the registry stores *raw, untruncated* tool output — full\n * fidelity for piping into downstream bash/jq — so the budget needs\n * enough headroom to keep a handful of large responses without\n * ballooning unbounded.\n */\nexport const HARD_MAX_TOTAL_TOOL_OUTPUT_SIZE = 5_000_000;\n\n/**\n * Computes the dynamic max tool result size based on the model's context window.\n * Uses 30% of the context window (in estimated characters, ~4 chars/token)\n * capped at HARD_MAX_TOOL_RESULT_CHARS.\n *\n * @param contextWindowTokens - The model's max context tokens (optional).\n * @returns Maximum allowed characters for a single tool result.\n */\nexport function calculateMaxToolResultChars(\n contextWindowTokens?: number\n): number {\n if (contextWindowTokens == null || contextWindowTokens <= 0) {\n return HARD_MAX_TOOL_RESULT_CHARS;\n }\n return Math.min(\n Math.floor(contextWindowTokens * 0.3) * 4,\n HARD_MAX_TOOL_RESULT_CHARS\n );\n}\n\n/**\n * Computes the default aggregate size (characters) for the tool output\n * reference registry based on the per-output budget. Mirrors\n * `calculateMaxToolResultChars`'s shape: a multiple of the per-output\n * cap, clamped to `HARD_MAX_TOTAL_TOOL_OUTPUT_SIZE`.\n *\n * @param maxOutputSize - Per-output maximum characters (e.g., the\n * ToolNode's `maxToolResultChars`). When omitted or non-positive,\n * falls back to the absolute total cap.\n * @returns Maximum total characters retained across the registry.\n */\nexport function calculateMaxTotalToolOutputSize(\n maxOutputSize?: number\n): number {\n if (maxOutputSize == null || maxOutputSize <= 0) {\n return HARD_MAX_TOTAL_TOOL_OUTPUT_SIZE;\n }\n return Math.min(maxOutputSize * 2, HARD_MAX_TOTAL_TOOL_OUTPUT_SIZE);\n}\n\n/**\n * Truncates a tool-call input (the arguments/payload of a tool_use block)\n * using head+tail strategy. Returns an object with `_truncated` (the\n * truncated string) and `_originalChars` (for diagnostics).\n *\n * Accepts any type — objects are JSON-serialized before truncation.\n *\n * @param input - The tool input (string, object, etc.).\n * @param maxChars - Maximum allowed characters.\n */\nexport function truncateToolInput(\n input: unknown,\n maxChars: number\n): { _truncated: string; _originalChars: number } {\n const serialized = typeof input === 'string' ? input : JSON.stringify(input);\n if (serialized.length <= maxChars) {\n return { _truncated: serialized, _originalChars: serialized.length };\n }\n const indicator = `\\n… [truncated: ${serialized.length} chars exceeded ${maxChars} limit] …\\n`;\n const available = maxChars - indicator.length;\n\n if (available < 100) {\n return {\n _truncated: serialized.slice(0, maxChars) + indicator.trimEnd(),\n _originalChars: serialized.length,\n };\n }\n\n const headSize = Math.ceil(available * 0.7);\n const tailSize = available - headSize;\n\n return {\n _truncated:\n serialized.slice(0, headSize) +\n indicator +\n serialized.slice(serialized.length - tailSize),\n _originalChars: serialized.length,\n };\n}\n\n/**\n * Truncates tool result content that exceeds `maxChars` using a head+tail\n * strategy. Keeps the beginning (structure/headers) and end (return value /\n * conclusion) of the content so the model retains both the opening context\n * and the final outcome.\n *\n * Head gets ~70% of the budget, tail gets ~30%. Falls back to head-only\n * when the budget is too small for a meaningful tail.\n *\n * @param content - The tool result string content.\n * @param maxChars - Maximum allowed characters.\n * @returns The (possibly truncated) content string.\n */\nexport function truncateToolResultContent(\n content: string,\n maxChars: number\n): string {\n if (content.length <= maxChars) {\n return content;\n }\n\n const indicator = `\\n\\n… [truncated: ${content.length} chars exceeded ${maxChars} limit] …\\n\\n`;\n const available = maxChars - indicator.length;\n if (available <= 0) {\n return content.slice(0, maxChars);\n }\n\n // When budget is too small for a meaningful tail, fall back to head-only\n if (available < 200) {\n return content.slice(0, available) + indicator.trimEnd();\n }\n\n const headSize = Math.ceil(available * 0.7);\n const tailSize = available - headSize;\n\n // Try to break at newline boundaries for cleaner output\n let headEnd = headSize;\n const headNewline = content.lastIndexOf('\\n', headSize);\n if (headNewline > headSize - 200 && headNewline > 0) {\n headEnd = headNewline;\n }\n\n let tailStart = content.length - tailSize;\n const tailNewline = content.indexOf('\\n', tailStart);\n if (tailNewline > 0 && tailNewline < tailStart + 200) {\n tailStart = tailNewline + 1;\n }\n\n return content.slice(0, headEnd) + indicator + content.slice(tailStart);\n}\n"],"names":[],"mappings":";;AAAA;;;;;AAKG;AAEH;;;;AAIG;AACI,MAAM,0BAA0B,GAAG;AAE1C;;;;;;;AAOG;AACI,MAAM,+BAA+B,GAAG;AAE/C;;;;;;;AAOG;AACG,SAAU,2BAA2B,CACzC,mBAA4B,EAAA;IAE5B,IAAI,mBAAmB,IAAI,IAAI,IAAI,mBAAmB,IAAI,CAAC,EAAE;AAC3D,QAAA,OAAO,0BAA0B;IACnC;AACA,IAAA,OAAO,IAAI,CAAC,GAAG,CACb,IAAI,CAAC,KAAK,CAAC,mBAAmB,GAAG,GAAG,CAAC,GAAG,CAAC,EACzC,0BAA0B,CAC3B;AACH;AAEA;;;;;;;;;;AAUG;AACG,SAAU,+BAA+B,CAC7C,aAAsB,EAAA;IAEtB,IAAI,aAAa,IAAI,IAAI,IAAI,aAAa,IAAI,CAAC,EAAE;AAC/C,QAAA,OAAO,+BAA+B;IACxC;IACA,OAAO,IAAI,CAAC,GAAG,CAAC,aAAa,GAAG,CAAC,EAAE,+BAA+B,CAAC;AACrE;AAEA;;;;;;;;;AASG;AACG,SAAU,iBAAiB,CAC/B,KAAc,EACd,QAAgB,EAAA;AAEhB,IAAA,MAAM,UAAU,GAAG,OAAO,KAAK,KAAK,QAAQ,GAAG,KAAK,GAAG,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC;AAC5E,IAAA,IAAI,UAAU,CAAC,MAAM,IAAI,QAAQ,EAAE;QACjC,OAAO,EAAE,UAAU,EAAE,UAAU,EAAE,cAAc,EAAE,UAAU,CAAC,MAAM,EAAE;IACtE;IACA,MAAM,SAAS,GAAG,CAAA,gBAAA,EAAmB,UAAU,CAAC,MAAM,CAAA,gBAAA,EAAmB,QAAQ,CAAA,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