@librechat/agents 3.1.70 → 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 (54) hide show
  1. package/dist/cjs/graphs/Graph.cjs +45 -0
  2. package/dist/cjs/graphs/Graph.cjs.map +1 -1
  3. package/dist/cjs/main.cjs +4 -0
  4. package/dist/cjs/main.cjs.map +1 -1
  5. package/dist/cjs/messages/prune.cjs +9 -2
  6. package/dist/cjs/messages/prune.cjs.map +1 -1
  7. package/dist/cjs/run.cjs +4 -0
  8. package/dist/cjs/run.cjs.map +1 -1
  9. package/dist/cjs/tools/BashExecutor.cjs +43 -0
  10. package/dist/cjs/tools/BashExecutor.cjs.map +1 -1
  11. package/dist/cjs/tools/ToolNode.cjs +453 -45
  12. package/dist/cjs/tools/ToolNode.cjs.map +1 -1
  13. package/dist/cjs/tools/toolOutputReferences.cjs +475 -0
  14. package/dist/cjs/tools/toolOutputReferences.cjs.map +1 -0
  15. package/dist/cjs/utils/truncation.cjs +28 -0
  16. package/dist/cjs/utils/truncation.cjs.map +1 -1
  17. package/dist/esm/graphs/Graph.mjs +45 -0
  18. package/dist/esm/graphs/Graph.mjs.map +1 -1
  19. package/dist/esm/main.mjs +2 -2
  20. package/dist/esm/messages/prune.mjs +9 -2
  21. package/dist/esm/messages/prune.mjs.map +1 -1
  22. package/dist/esm/run.mjs +4 -0
  23. package/dist/esm/run.mjs.map +1 -1
  24. package/dist/esm/tools/BashExecutor.mjs +42 -1
  25. package/dist/esm/tools/BashExecutor.mjs.map +1 -1
  26. package/dist/esm/tools/ToolNode.mjs +453 -45
  27. package/dist/esm/tools/ToolNode.mjs.map +1 -1
  28. package/dist/esm/tools/toolOutputReferences.mjs +468 -0
  29. package/dist/esm/tools/toolOutputReferences.mjs.map +1 -0
  30. package/dist/esm/utils/truncation.mjs +27 -1
  31. package/dist/esm/utils/truncation.mjs.map +1 -1
  32. package/dist/types/graphs/Graph.d.ts +21 -0
  33. package/dist/types/run.d.ts +1 -0
  34. package/dist/types/tools/BashExecutor.d.ts +31 -0
  35. package/dist/types/tools/ToolNode.d.ts +86 -3
  36. package/dist/types/tools/toolOutputReferences.d.ts +205 -0
  37. package/dist/types/types/run.d.ts +9 -1
  38. package/dist/types/types/tools.d.ts +70 -0
  39. package/dist/types/utils/truncation.d.ts +21 -0
  40. package/package.json +1 -1
  41. package/src/graphs/Graph.ts +48 -0
  42. package/src/messages/prune.ts +9 -2
  43. package/src/run.ts +4 -0
  44. package/src/specs/prune.test.ts +413 -0
  45. package/src/tools/BashExecutor.ts +45 -0
  46. package/src/tools/ToolNode.ts +618 -55
  47. package/src/tools/__tests__/BashExecutor.test.ts +36 -0
  48. package/src/tools/__tests__/ToolNode.outputReferences.test.ts +1395 -0
  49. package/src/tools/__tests__/toolOutputReferences.test.ts +415 -0
  50. package/src/tools/toolOutputReferences.ts +590 -0
  51. package/src/types/run.ts +9 -1
  52. package/src/types/tools.ts +71 -0
  53. package/src/utils/__tests__/truncation.test.ts +66 -0
  54. package/src/utils/truncation.ts +30 -0
@@ -0,0 +1,468 @@
1
+ import { HARD_MAX_TOOL_RESULT_CHARS, HARD_MAX_TOTAL_TOOL_OUTPUT_SIZE, calculateMaxTotalToolOutputSize } from '../utils/truncation.mjs';
2
+
3
+ /**
4
+ * Tool output reference registry.
5
+ *
6
+ * When enabled via `RunConfig.toolOutputReferences.enabled`, ToolNode
7
+ * stores each successful tool output under a stable key
8
+ * (`tool<idx>turn<turn>`) where `idx` is the tool's position within a
9
+ * ToolNode batch and `turn` is the batch index within the run
10
+ * (incremented once per ToolNode invocation).
11
+ *
12
+ * Subsequent tool calls can pipe a previous output into their args by
13
+ * embedding `{{tool<idx>turn<turn>}}` inside any string argument;
14
+ * {@link ToolOutputReferenceRegistry.resolve} walks the args and
15
+ * substitutes the placeholders immediately before invocation.
16
+ *
17
+ * The registry stores the *raw, untruncated* tool output so a later
18
+ * `{{…}}` substitution pipes the full payload into the next tool —
19
+ * even when the LLM only saw a head+tail-truncated preview in
20
+ * `ToolMessage.content`. Outputs are stored without any annotation
21
+ * (the `_ref` key or the `[ref: ...]` prefix seen by the LLM is
22
+ * strictly a UX signal attached to `ToolMessage.content`). Keeping the
23
+ * registry pristine means downstream bash/jq piping receives the
24
+ * complete, verbatim output with no injected fields.
25
+ */
26
+ /** Object key used when a parsed-object output has `_ref` injected. */
27
+ const TOOL_OUTPUT_REF_KEY = '_ref';
28
+ /**
29
+ * Object key used to carry unresolved reference warnings on a parsed-
30
+ * object output. Using a dedicated field instead of a trailing text
31
+ * line keeps the annotated `ToolMessage.content` parseable as JSON for
32
+ * downstream consumers that rely on the object shape.
33
+ */
34
+ const TOOL_OUTPUT_UNRESOLVED_KEY = '_unresolved_refs';
35
+ /** Single-line prefix prepended to non-object tool outputs so the LLM sees the reference key. */
36
+ function buildReferencePrefix(key) {
37
+ return `[ref: ${key}]`;
38
+ }
39
+ /** Stable registry key for a tool output. */
40
+ function buildReferenceKey(toolIndex, turn) {
41
+ return `tool${toolIndex}turn${turn}`;
42
+ }
43
+ const EMPTY_ENTRIES = new Map();
44
+ /**
45
+ * Per-run state bucket held inside the registry. Each distinct
46
+ * `run_id` gets its own bucket so overlapping concurrent runs on a
47
+ * shared registry cannot leak outputs, turn counters, or warn-memos
48
+ * into one another.
49
+ */
50
+ class RunStateBucket {
51
+ entries = new Map();
52
+ totalSize = 0;
53
+ turnCounter = 0;
54
+ warnedNonStringTools = new Set();
55
+ }
56
+ /**
57
+ * Anonymous (`run_id` absent) bucket key. Anonymous batches are
58
+ * treated as fresh runs on every invocation — see `nextTurn`.
59
+ */
60
+ const ANON_RUN_KEY = '\0anon';
61
+ /**
62
+ * Default upper bound on the number of concurrently-tracked runs per
63
+ * registry. When exceeded, the oldest run's bucket (by insertion
64
+ * order) is evicted. Keeps memory bounded when a ToolNode is reused
65
+ * across many runs without explicit `releaseRun` calls.
66
+ */
67
+ const DEFAULT_MAX_ACTIVE_RUNS = 32;
68
+ /**
69
+ * Ordered map of reference-key → stored output, partitioned by run so
70
+ * concurrent / interleaved runs sharing one registry cannot leak
71
+ * outputs between each other.
72
+ *
73
+ * Each public method takes a `runId` which selects the run's bucket.
74
+ * Hosts typically get one registry per run via `Graph`, in which
75
+ * case only a single bucket is ever populated; the partitioning
76
+ * exists so the registry also behaves correctly when a single
77
+ * instance is reused directly.
78
+ */
79
+ class ToolOutputReferenceRegistry {
80
+ runStates = new Map();
81
+ maxOutputSize;
82
+ maxTotalSize;
83
+ maxActiveRuns;
84
+ /**
85
+ * Local stateful matcher used only by `replaceInString`. Kept
86
+ * off-module so callers of the exported `TOOL_OUTPUT_REF_PATTERN`
87
+ * never see a stale `lastIndex`.
88
+ */
89
+ static PLACEHOLDER_MATCHER = /\{\{(tool\d+turn\d+)\}\}/g;
90
+ constructor(options = {}) {
91
+ /**
92
+ * Per-output default is the same ~400 KB budget as the standard
93
+ * tool-result truncation (`HARD_MAX_TOOL_RESULT_CHARS`). This
94
+ * keeps a single `{{…}}` substitution at a size that is safe to
95
+ * pass through typical shell `ARG_MAX` limits and matches what
96
+ * the LLM would otherwise have seen. Hosts that want larger per-
97
+ * output payloads (API consumers, long JSON streams) can raise
98
+ * the cap explicitly up to the 5 MB total budget.
99
+ */
100
+ const perOutput = options.maxOutputSize != null && options.maxOutputSize > 0
101
+ ? options.maxOutputSize
102
+ : HARD_MAX_TOOL_RESULT_CHARS;
103
+ /**
104
+ * Clamp a caller-supplied `maxTotalSize` to
105
+ * `HARD_MAX_TOTAL_TOOL_OUTPUT_SIZE` (5 MB) so the documented
106
+ * absolute cap is enforced regardless of host config —
107
+ * `calculateMaxTotalToolOutputSize` already applies the same
108
+ * upper bound on its computed default, but the user-provided
109
+ * branch was bypassing it.
110
+ */
111
+ const totalRaw = options.maxTotalSize != null && options.maxTotalSize > 0
112
+ ? Math.min(options.maxTotalSize, HARD_MAX_TOTAL_TOOL_OUTPUT_SIZE)
113
+ : calculateMaxTotalToolOutputSize(perOutput);
114
+ this.maxTotalSize = totalRaw;
115
+ /**
116
+ * The per-output cap can never exceed the per-run aggregate cap:
117
+ * if a single entry were allowed to be larger than `maxTotalSize`,
118
+ * the eviction loop would either blow the cap (to keep the entry)
119
+ * or self-evict a just-stored value. Clamping here turns
120
+ * `maxTotalSize` into a hard upper bound on *any* state the
121
+ * registry retains per run.
122
+ */
123
+ this.maxOutputSize = Math.min(perOutput, totalRaw);
124
+ this.maxActiveRuns =
125
+ options.maxActiveRuns != null && options.maxActiveRuns > 0
126
+ ? options.maxActiveRuns
127
+ : DEFAULT_MAX_ACTIVE_RUNS;
128
+ }
129
+ keyFor(runId) {
130
+ return runId ?? ANON_RUN_KEY;
131
+ }
132
+ getOrCreate(runId) {
133
+ const key = this.keyFor(runId);
134
+ let state = this.runStates.get(key);
135
+ if (state == null) {
136
+ state = new RunStateBucket();
137
+ this.runStates.set(key, state);
138
+ if (this.runStates.size > this.maxActiveRuns) {
139
+ const oldest = this.runStates.keys().next().value;
140
+ if (oldest != null && oldest !== key) {
141
+ this.runStates.delete(oldest);
142
+ }
143
+ }
144
+ }
145
+ return state;
146
+ }
147
+ /** Registers (or replaces) the output stored under `key` for `runId`. */
148
+ set(runId, key, value) {
149
+ const bucket = this.getOrCreate(runId);
150
+ const clipped = value.length > this.maxOutputSize
151
+ ? value.slice(0, this.maxOutputSize)
152
+ : value;
153
+ const existing = bucket.entries.get(key);
154
+ if (existing != null) {
155
+ bucket.totalSize -= existing.length;
156
+ bucket.entries.delete(key);
157
+ }
158
+ bucket.entries.set(key, clipped);
159
+ bucket.totalSize += clipped.length;
160
+ this.evictWithinBucket(bucket);
161
+ }
162
+ /** Returns the stored value for `key` in `runId`'s bucket, or `undefined`. */
163
+ get(runId, key) {
164
+ return this.runStates.get(this.keyFor(runId))?.entries.get(key);
165
+ }
166
+ /** Total number of registered outputs across every run bucket. */
167
+ get size() {
168
+ let n = 0;
169
+ for (const bucket of this.runStates.values()) {
170
+ n += bucket.entries.size;
171
+ }
172
+ return n;
173
+ }
174
+ /** Maximum characters retained per output (post-clip). */
175
+ get perOutputLimit() {
176
+ return this.maxOutputSize;
177
+ }
178
+ /** Maximum total characters retained *per run*. */
179
+ get totalLimit() {
180
+ return this.maxTotalSize;
181
+ }
182
+ /** Drops every run's state. */
183
+ clear() {
184
+ this.runStates.clear();
185
+ }
186
+ /**
187
+ * Explicitly release `runId`'s state. Safe to call when a run has
188
+ * finished. Hosts sharing one registry across runs should call this
189
+ * to reclaim memory deterministically; otherwise LRU eviction kicks
190
+ * in when `maxActiveRuns` runs accumulate.
191
+ */
192
+ releaseRun(runId) {
193
+ this.runStates.delete(this.keyFor(runId));
194
+ }
195
+ /**
196
+ * Claims the next batch turn synchronously from `runId`'s bucket.
197
+ *
198
+ * Must be called once at the start of each ToolNode batch before
199
+ * any `await`, so concurrent invocations within the same run see
200
+ * distinct turn values (reads are effectively atomic by JS's
201
+ * single-threaded execution of the sync prefix).
202
+ *
203
+ * If `runId` is missing the anonymous bucket is dropped and a
204
+ * fresh one created so each anonymous call behaves as its own run.
205
+ */
206
+ nextTurn(runId) {
207
+ if (runId == null) {
208
+ this.runStates.delete(ANON_RUN_KEY);
209
+ }
210
+ const bucket = this.getOrCreate(runId);
211
+ return bucket.turnCounter++;
212
+ }
213
+ /**
214
+ * Records that `toolName` has been warned about in `runId` (returns
215
+ * `true` on the first call per run, `false` after). Used by
216
+ * ToolNode to emit one log line per offending tool per run when a
217
+ * `ToolMessage.content` isn't a string.
218
+ */
219
+ claimWarnOnce(runId, toolName) {
220
+ const bucket = this.getOrCreate(runId);
221
+ if (bucket.warnedNonStringTools.has(toolName)) {
222
+ return false;
223
+ }
224
+ bucket.warnedNonStringTools.add(toolName);
225
+ return true;
226
+ }
227
+ /**
228
+ * Walks `args` and replaces every `{{tool<i>turn<n>}}` placeholder in
229
+ * string values with the stored output *from `runId`'s bucket*. Non-
230
+ * string values and object keys are left untouched. Unresolved
231
+ * references are left in-place and reported so the caller can
232
+ * surface them to the LLM. When no placeholder appears anywhere in
233
+ * the serialized args, the original input is returned without
234
+ * walking the tree.
235
+ */
236
+ resolve(runId, args) {
237
+ if (!hasAnyPlaceholder(args)) {
238
+ return { resolved: args, unresolved: [] };
239
+ }
240
+ const bucket = this.runStates.get(this.keyFor(runId));
241
+ return this.resolveAgainst(bucket?.entries ?? EMPTY_ENTRIES, args);
242
+ }
243
+ /**
244
+ * Captures a frozen snapshot of `runId`'s current entries and
245
+ * returns a view that resolves placeholders against *only* that
246
+ * snapshot. The snapshot is decoupled from the live registry, so
247
+ * subsequent `set()` calls (for example, same-turn direct outputs
248
+ * registering while an event branch is still in flight) are
249
+ * invisible to the snapshot's `resolve`. Used by the mixed
250
+ * direct+event dispatch path to preserve same-turn isolation when
251
+ * a `PreToolUse` hook rewrites event args after directs have
252
+ * completed.
253
+ */
254
+ snapshot(runId) {
255
+ const bucket = this.runStates.get(this.keyFor(runId));
256
+ const entries = bucket
257
+ ? new Map(bucket.entries)
258
+ : EMPTY_ENTRIES;
259
+ return {
260
+ resolve: (args) => this.resolveAgainst(entries, args),
261
+ };
262
+ }
263
+ resolveAgainst(entries, args) {
264
+ if (!hasAnyPlaceholder(args)) {
265
+ return { resolved: args, unresolved: [] };
266
+ }
267
+ const unresolved = new Set();
268
+ const resolved = this.transform(entries, args, unresolved);
269
+ return { resolved, unresolved: Array.from(unresolved) };
270
+ }
271
+ transform(entries, value, unresolved) {
272
+ if (typeof value === 'string') {
273
+ return this.replaceInString(entries, value, unresolved);
274
+ }
275
+ if (Array.isArray(value)) {
276
+ return value.map((item) => this.transform(entries, item, unresolved));
277
+ }
278
+ if (value !== null && typeof value === 'object') {
279
+ const source = value;
280
+ const next = {};
281
+ for (const [key, item] of Object.entries(source)) {
282
+ next[key] = this.transform(entries, item, unresolved);
283
+ }
284
+ return next;
285
+ }
286
+ return value;
287
+ }
288
+ replaceInString(entries, input, unresolved) {
289
+ if (input.indexOf('{{tool') === -1) {
290
+ return input;
291
+ }
292
+ return input.replace(ToolOutputReferenceRegistry.PLACEHOLDER_MATCHER, (match, key) => {
293
+ const stored = entries.get(key);
294
+ if (stored == null) {
295
+ unresolved.add(key);
296
+ return match;
297
+ }
298
+ return stored;
299
+ });
300
+ }
301
+ evictWithinBucket(bucket) {
302
+ if (bucket.totalSize <= this.maxTotalSize) {
303
+ return;
304
+ }
305
+ for (const key of bucket.entries.keys()) {
306
+ if (bucket.totalSize <= this.maxTotalSize) {
307
+ return;
308
+ }
309
+ const entry = bucket.entries.get(key);
310
+ if (entry == null) {
311
+ continue;
312
+ }
313
+ bucket.totalSize -= entry.length;
314
+ bucket.entries.delete(key);
315
+ }
316
+ }
317
+ }
318
+ /**
319
+ * Cheap pre-check: returns true if any string value in `args` contains
320
+ * the `{{tool` substring. Lets `resolve()` skip the deep tree walk (and
321
+ * its object allocations) for the common case of plain args.
322
+ */
323
+ function hasAnyPlaceholder(value) {
324
+ if (typeof value === 'string') {
325
+ return value.indexOf('{{tool') !== -1;
326
+ }
327
+ if (Array.isArray(value)) {
328
+ for (const item of value) {
329
+ if (hasAnyPlaceholder(item)) {
330
+ return true;
331
+ }
332
+ }
333
+ return false;
334
+ }
335
+ if (value !== null && typeof value === 'object') {
336
+ for (const item of Object.values(value)) {
337
+ if (hasAnyPlaceholder(item)) {
338
+ return true;
339
+ }
340
+ }
341
+ return false;
342
+ }
343
+ return false;
344
+ }
345
+ /**
346
+ * Annotates `content` with a reference key and/or unresolved-ref
347
+ * warnings so the LLM sees both alongside the tool output.
348
+ *
349
+ * Behavior:
350
+ * - If `content` parses as a plain (non-array, non-null) JSON object
351
+ * and the object does not already have a conflicting `_ref` key,
352
+ * the reference key and (when present) `_unresolved_refs` array
353
+ * are injected as object fields, preserving JSON validity for
354
+ * downstream consumers that parse the output.
355
+ * - Otherwise (string output, JSON array/primitive, parse failure,
356
+ * or `_ref` collision), a `[ref: <key>]\n` prefix line is
357
+ * prepended and unresolved refs are appended as a trailing
358
+ * `[unresolved refs: …]` line.
359
+ *
360
+ * The annotated string is what the LLM sees as `ToolMessage.content`.
361
+ * The *original* (un-annotated) value is what gets stored in the
362
+ * registry, so downstream piping remains pristine.
363
+ *
364
+ * @param content Raw (post-truncation) tool output.
365
+ * @param key Reference key for this output, or undefined when
366
+ * there is nothing to register (errors etc.).
367
+ * @param unresolved Reference keys that failed to resolve during
368
+ * argument substitution. Surfaced so the LLM can
369
+ * self-correct its next tool call.
370
+ */
371
+ function annotateToolOutputWithReference(content, key, unresolved = []) {
372
+ const hasRefKey = key != null;
373
+ const hasUnresolved = unresolved.length > 0;
374
+ if (!hasRefKey && !hasUnresolved) {
375
+ return content;
376
+ }
377
+ const trimmed = content.trimStart();
378
+ if (trimmed.startsWith('{')) {
379
+ const annotated = tryInjectRefIntoJsonObject(content, key, unresolved);
380
+ if (annotated != null) {
381
+ return annotated;
382
+ }
383
+ }
384
+ const prefix = hasRefKey ? `${buildReferencePrefix(key)}\n` : '';
385
+ const trailer = hasUnresolved
386
+ ? `\n[unresolved refs: ${unresolved.join(', ')}]`
387
+ : '';
388
+ return `${prefix}${content}${trailer}`;
389
+ }
390
+ function tryInjectRefIntoJsonObject(content, key, unresolved) {
391
+ let parsed;
392
+ try {
393
+ parsed = JSON.parse(content);
394
+ }
395
+ catch {
396
+ return null;
397
+ }
398
+ if (parsed === null || typeof parsed !== 'object' || Array.isArray(parsed)) {
399
+ return null;
400
+ }
401
+ const obj = parsed;
402
+ const injectingRef = key != null;
403
+ const injectingUnresolved = unresolved.length > 0;
404
+ /**
405
+ * Reject the JSON-injection path (fall back to prefix form) when
406
+ * either of our keys collides with real payload data:
407
+ * - `_ref` collision: existing value is non-null and differs from
408
+ * the key we're about to inject.
409
+ * - `_unresolved_refs` collision: existing value is non-null and
410
+ * is not a deep-equal match for the array we'd inject.
411
+ * This keeps us from silently overwriting legitimate tool output.
412
+ */
413
+ if (injectingRef &&
414
+ TOOL_OUTPUT_REF_KEY in obj &&
415
+ obj[TOOL_OUTPUT_REF_KEY] !== key &&
416
+ obj[TOOL_OUTPUT_REF_KEY] != null) {
417
+ return null;
418
+ }
419
+ if (injectingUnresolved &&
420
+ TOOL_OUTPUT_UNRESOLVED_KEY in obj &&
421
+ obj[TOOL_OUTPUT_UNRESOLVED_KEY] != null &&
422
+ !arraysShallowEqual(obj[TOOL_OUTPUT_UNRESOLVED_KEY], unresolved)) {
423
+ return null;
424
+ }
425
+ /**
426
+ * Only strip the framework-owned key we're actually injecting —
427
+ * leave everything else (including a pre-existing `_ref` on the
428
+ * unresolved-only path, or a pre-existing `_unresolved_refs` on a
429
+ * plain-annotation path) untouched so we annotate rather than
430
+ * mutate downstream payload data. Our injected keys land first in
431
+ * the serialized JSON so the LLM sees them before the body.
432
+ */
433
+ const omitKeys = new Set();
434
+ if (injectingRef)
435
+ omitKeys.add(TOOL_OUTPUT_REF_KEY);
436
+ if (injectingUnresolved)
437
+ omitKeys.add(TOOL_OUTPUT_UNRESOLVED_KEY);
438
+ const rest = {};
439
+ for (const [k, v] of Object.entries(obj)) {
440
+ if (!omitKeys.has(k)) {
441
+ rest[k] = v;
442
+ }
443
+ }
444
+ const injected = {};
445
+ if (injectingRef) {
446
+ injected[TOOL_OUTPUT_REF_KEY] = key;
447
+ }
448
+ if (injectingUnresolved) {
449
+ injected[TOOL_OUTPUT_UNRESOLVED_KEY] = unresolved;
450
+ }
451
+ Object.assign(injected, rest);
452
+ const pretty = /^\{\s*\n/.test(content);
453
+ return pretty ? JSON.stringify(injected, null, 2) : JSON.stringify(injected);
454
+ }
455
+ function arraysShallowEqual(a, b) {
456
+ if (!Array.isArray(a) || a.length !== b.length) {
457
+ return false;
458
+ }
459
+ for (let i = 0; i < a.length; i++) {
460
+ if (a[i] !== b[i]) {
461
+ return false;
462
+ }
463
+ }
464
+ return true;
465
+ }
466
+
467
+ export { TOOL_OUTPUT_REF_KEY, TOOL_OUTPUT_UNRESOLVED_KEY, ToolOutputReferenceRegistry, annotateToolOutputWithReference, buildReferenceKey, buildReferencePrefix };
468
+ //# sourceMappingURL=toolOutputReferences.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"toolOutputReferences.mjs","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":[],"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;AAClB,IAAA,OAAO,GAAwB,IAAI,GAAG,EAAE;IACxC,SAAS,GAAW,CAAC;IACrB,WAAW,GAAW,CAAC;AACvB,IAAA,oBAAoB,GAAgB,IAAI,GAAG,EAAE;AAC9C;AAED;;;AAGG;AACH,MAAM,YAAY,GAA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AM,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;;;;"}
@@ -10,6 +10,15 @@
10
10
  * larger than this is almost certainly a bug (e.g., dumping a binary file).
11
11
  */
12
12
  const HARD_MAX_TOOL_RESULT_CHARS = 400_000;
13
+ /**
14
+ * Absolute hard cap on the aggregate size (characters) of all registered
15
+ * tool outputs kept for `{{tool<i>turn<n>}}` substitution. Set at 5 MB
16
+ * because the registry stores *raw, untruncated* tool output — full
17
+ * fidelity for piping into downstream bash/jq — so the budget needs
18
+ * enough headroom to keep a handful of large responses without
19
+ * ballooning unbounded.
20
+ */
21
+ const HARD_MAX_TOTAL_TOOL_OUTPUT_SIZE = 5_000_000;
13
22
  /**
14
23
  * Computes the dynamic max tool result size based on the model's context window.
15
24
  * Uses 30% of the context window (in estimated characters, ~4 chars/token)
@@ -24,6 +33,23 @@ function calculateMaxToolResultChars(contextWindowTokens) {
24
33
  }
25
34
  return Math.min(Math.floor(contextWindowTokens * 0.3) * 4, HARD_MAX_TOOL_RESULT_CHARS);
26
35
  }
36
+ /**
37
+ * Computes the default aggregate size (characters) for the tool output
38
+ * reference registry based on the per-output budget. Mirrors
39
+ * `calculateMaxToolResultChars`'s shape: a multiple of the per-output
40
+ * cap, clamped to `HARD_MAX_TOTAL_TOOL_OUTPUT_SIZE`.
41
+ *
42
+ * @param maxOutputSize - Per-output maximum characters (e.g., the
43
+ * ToolNode's `maxToolResultChars`). When omitted or non-positive,
44
+ * falls back to the absolute total cap.
45
+ * @returns Maximum total characters retained across the registry.
46
+ */
47
+ function calculateMaxTotalToolOutputSize(maxOutputSize) {
48
+ if (maxOutputSize == null || maxOutputSize <= 0) {
49
+ return HARD_MAX_TOTAL_TOOL_OUTPUT_SIZE;
50
+ }
51
+ return Math.min(maxOutputSize * 2, HARD_MAX_TOTAL_TOOL_OUTPUT_SIZE);
52
+ }
27
53
  /**
28
54
  * Truncates a tool-call input (the arguments/payload of a tool_use block)
29
55
  * using head+tail strategy. Returns an object with `_truncated` (the
@@ -98,5 +124,5 @@ function truncateToolResultContent(content, maxChars) {
98
124
  return content.slice(0, headEnd) + indicator + content.slice(tailStart);
99
125
  }
100
126
 
101
- export { HARD_MAX_TOOL_RESULT_CHARS, calculateMaxToolResultChars, truncateToolInput, truncateToolResultContent };
127
+ export { HARD_MAX_TOOL_RESULT_CHARS, HARD_MAX_TOTAL_TOOL_OUTPUT_SIZE, calculateMaxToolResultChars, calculateMaxTotalToolOutputSize, truncateToolInput, truncateToolResultContent };
102
128
  //# sourceMappingURL=truncation.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"truncation.mjs","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,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,CA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AAO,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.mjs","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;;;;;;;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AG,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;;;;"}
@@ -4,6 +4,7 @@ import type { UsageMetadata, BaseMessage } from '@langchain/core/messages';
4
4
  import type { ToolCall } from '@langchain/core/messages/tool';
5
5
  import type * as t from '@/types';
6
6
  import { ToolNode as CustomToolNode } from '@/tools/ToolNode';
7
+ import { ToolOutputReferenceRegistry } from '@/tools/toolOutputReferences';
7
8
  import { AgentContext } from '@/agents/AgentContext';
8
9
  import { HandlerRegistry } from '@/events';
9
10
  import type { HookRegistry } from '@/hooks';
@@ -45,6 +46,19 @@ export declare abstract class Graph<T extends t.BaseGraphState = t.BaseGraphStat
45
46
  invokedToolIds?: Set<string>;
46
47
  handlerRegistry: HandlerRegistry | undefined;
47
48
  hookRegistry: HookRegistry | undefined;
49
+ /**
50
+ * Run-scoped config for the tool output reference registry. Threaded
51
+ * from `RunConfig.toolOutputReferences` down into every ToolNode this
52
+ * graph compiles.
53
+ */
54
+ toolOutputReferences: t.ToolOutputReferencesConfig | undefined;
55
+ /**
56
+ * Shared registry instance used by every ToolNode compiled from this
57
+ * graph. Lazily constructed on first access so multi-agent graphs
58
+ * produce one registry per run (not one per agent), letting cross-
59
+ * agent `{{tool<i>turn<n>}}` substitutions resolve.
60
+ */
61
+ private _toolOutputRegistry?;
48
62
  /**
49
63
  * Tool session contexts for automatic state persistence across tool invocations.
50
64
  * Keyed by tool name (e.g., Constants.EXECUTE_CODE).
@@ -57,6 +71,13 @@ export declare abstract class Graph<T extends t.BaseGraphState = t.BaseGraphStat
57
71
  * Call after a run completes and content has been extracted.
58
72
  */
59
73
  clearHeavyState(): void;
74
+ /**
75
+ * Returns the shared `ToolOutputReferenceRegistry` for this run,
76
+ * constructing it on first access. Returns `undefined` when the
77
+ * feature is disabled. All ToolNodes compiled from this graph share
78
+ * this single instance so cross-agent `{{…}}` references resolve.
79
+ */
80
+ protected getOrCreateToolOutputRegistry(): ToolOutputReferenceRegistry | undefined;
60
81
  }
61
82
  export declare class StandardGraph extends Graph<t.BaseGraphState, t.GraphNode> {
62
83
  overrideModel?: t.ChatModel;