@librechat/agents 3.1.70 → 3.1.71-dev.1

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 (66) hide show
  1. package/dist/cjs/graphs/Graph.cjs +52 -0
  2. package/dist/cjs/graphs/Graph.cjs.map +1 -1
  3. package/dist/cjs/llm/invoke.cjs +13 -2
  4. package/dist/cjs/llm/invoke.cjs.map +1 -1
  5. package/dist/cjs/main.cjs +4 -0
  6. package/dist/cjs/main.cjs.map +1 -1
  7. package/dist/cjs/messages/prune.cjs +9 -2
  8. package/dist/cjs/messages/prune.cjs.map +1 -1
  9. package/dist/cjs/run.cjs +4 -0
  10. package/dist/cjs/run.cjs.map +1 -1
  11. package/dist/cjs/tools/BashExecutor.cjs +43 -0
  12. package/dist/cjs/tools/BashExecutor.cjs.map +1 -1
  13. package/dist/cjs/tools/ToolNode.cjs +482 -45
  14. package/dist/cjs/tools/ToolNode.cjs.map +1 -1
  15. package/dist/cjs/tools/toolOutputReferences.cjs +657 -0
  16. package/dist/cjs/tools/toolOutputReferences.cjs.map +1 -0
  17. package/dist/cjs/utils/truncation.cjs +28 -0
  18. package/dist/cjs/utils/truncation.cjs.map +1 -1
  19. package/dist/esm/graphs/Graph.mjs +52 -0
  20. package/dist/esm/graphs/Graph.mjs.map +1 -1
  21. package/dist/esm/llm/invoke.mjs +13 -2
  22. package/dist/esm/llm/invoke.mjs.map +1 -1
  23. package/dist/esm/main.mjs +2 -2
  24. package/dist/esm/messages/prune.mjs +9 -2
  25. package/dist/esm/messages/prune.mjs.map +1 -1
  26. package/dist/esm/run.mjs +4 -0
  27. package/dist/esm/run.mjs.map +1 -1
  28. package/dist/esm/tools/BashExecutor.mjs +42 -1
  29. package/dist/esm/tools/BashExecutor.mjs.map +1 -1
  30. package/dist/esm/tools/ToolNode.mjs +482 -45
  31. package/dist/esm/tools/ToolNode.mjs.map +1 -1
  32. package/dist/esm/tools/toolOutputReferences.mjs +649 -0
  33. package/dist/esm/tools/toolOutputReferences.mjs.map +1 -0
  34. package/dist/esm/utils/truncation.mjs +27 -1
  35. package/dist/esm/utils/truncation.mjs.map +1 -1
  36. package/dist/types/graphs/Graph.d.ts +28 -0
  37. package/dist/types/llm/invoke.d.ts +9 -0
  38. package/dist/types/run.d.ts +1 -0
  39. package/dist/types/tools/BashExecutor.d.ts +31 -0
  40. package/dist/types/tools/ToolNode.d.ts +84 -3
  41. package/dist/types/tools/toolOutputReferences.d.ts +236 -0
  42. package/dist/types/types/index.d.ts +1 -0
  43. package/dist/types/types/messages.d.ts +26 -0
  44. package/dist/types/types/run.d.ts +9 -1
  45. package/dist/types/types/tools.d.ts +70 -0
  46. package/dist/types/utils/truncation.d.ts +21 -0
  47. package/package.json +1 -1
  48. package/src/graphs/Graph.ts +55 -0
  49. package/src/llm/invoke.test.ts +442 -0
  50. package/src/llm/invoke.ts +23 -2
  51. package/src/messages/prune.ts +9 -2
  52. package/src/run.ts +4 -0
  53. package/src/specs/prune.test.ts +413 -0
  54. package/src/tools/BashExecutor.ts +45 -0
  55. package/src/tools/ToolNode.ts +631 -55
  56. package/src/tools/__tests__/BashExecutor.test.ts +36 -0
  57. package/src/tools/__tests__/ToolNode.outputReferences.test.ts +1438 -0
  58. package/src/tools/__tests__/annotateMessagesForLLM.test.ts +419 -0
  59. package/src/tools/__tests__/toolOutputReferences.test.ts +415 -0
  60. package/src/tools/toolOutputReferences.ts +813 -0
  61. package/src/types/index.ts +1 -0
  62. package/src/types/messages.ts +27 -0
  63. package/src/types/run.ts +9 -1
  64. package/src/types/tools.ts +71 -0
  65. package/src/utils/__tests__/truncation.test.ts +66 -0
  66. package/src/utils/truncation.ts +30 -0
@@ -0,0 +1,649 @@
1
+ import { ToolMessage } from '@langchain/core/messages';
2
+ import { HARD_MAX_TOOL_RESULT_CHARS, HARD_MAX_TOTAL_TOOL_OUTPUT_SIZE, calculateMaxTotalToolOutputSize } from '../utils/truncation.mjs';
3
+
4
+ /**
5
+ * Tool output reference registry.
6
+ *
7
+ * When enabled via `RunConfig.toolOutputReferences.enabled`, ToolNode
8
+ * stores each successful tool output under a stable key
9
+ * (`tool<idx>turn<turn>`) where `idx` is the tool's position within a
10
+ * ToolNode batch and `turn` is the batch index within the run
11
+ * (incremented once per ToolNode invocation).
12
+ *
13
+ * Subsequent tool calls can pipe a previous output into their args by
14
+ * embedding `{{tool<idx>turn<turn>}}` inside any string argument;
15
+ * {@link ToolOutputReferenceRegistry.resolve} walks the args and
16
+ * substitutes the placeholders immediately before invocation.
17
+ *
18
+ * The registry stores the *raw, untruncated* tool output so a later
19
+ * `{{…}}` substitution pipes the full payload into the next tool —
20
+ * even when the LLM only saw a head+tail-truncated preview in
21
+ * `ToolMessage.content`. Outputs are stored without any annotation
22
+ * (the `_ref` key or the `[ref: ...]` prefix seen by the LLM is
23
+ * strictly a UX signal attached to `ToolMessage.content`). Keeping the
24
+ * registry pristine means downstream bash/jq piping receives the
25
+ * complete, verbatim output with no injected fields.
26
+ */
27
+ /** Object key used when a parsed-object output has `_ref` injected. */
28
+ const TOOL_OUTPUT_REF_KEY = '_ref';
29
+ /**
30
+ * Object key used to carry unresolved reference warnings on a parsed-
31
+ * object output. Using a dedicated field instead of a trailing text
32
+ * line keeps the annotated `ToolMessage.content` parseable as JSON for
33
+ * downstream consumers that rely on the object shape.
34
+ */
35
+ const TOOL_OUTPUT_UNRESOLVED_KEY = '_unresolved_refs';
36
+ /** Single-line prefix prepended to non-object tool outputs so the LLM sees the reference key. */
37
+ function buildReferencePrefix(key) {
38
+ return `[ref: ${key}]`;
39
+ }
40
+ /** Stable registry key for a tool output. */
41
+ function buildReferenceKey(toolIndex, turn) {
42
+ return `tool${toolIndex}turn${turn}`;
43
+ }
44
+ const EMPTY_ENTRIES = new Map();
45
+ /**
46
+ * Per-run state bucket held inside the registry. Each distinct
47
+ * `run_id` gets its own bucket so overlapping concurrent runs on a
48
+ * shared registry cannot leak outputs, turn counters, or warn-memos
49
+ * into one another.
50
+ */
51
+ class RunStateBucket {
52
+ entries = new Map();
53
+ totalSize = 0;
54
+ turnCounter = 0;
55
+ warnedNonStringTools = new Set();
56
+ }
57
+ /**
58
+ * Anonymous (`run_id` absent) bucket key. Anonymous batches are
59
+ * treated as fresh runs on every invocation — see `nextTurn`.
60
+ */
61
+ const ANON_RUN_KEY = '\0anon';
62
+ /**
63
+ * Default upper bound on the number of concurrently-tracked runs per
64
+ * registry. When exceeded, the oldest run's bucket (by insertion
65
+ * order) is evicted. Keeps memory bounded when a ToolNode is reused
66
+ * across many runs without explicit `releaseRun` calls.
67
+ */
68
+ const DEFAULT_MAX_ACTIVE_RUNS = 32;
69
+ /**
70
+ * Ordered map of reference-key → stored output, partitioned by run so
71
+ * concurrent / interleaved runs sharing one registry cannot leak
72
+ * outputs between each other.
73
+ *
74
+ * Each public method takes a `runId` which selects the run's bucket.
75
+ * Hosts typically get one registry per run via `Graph`, in which
76
+ * case only a single bucket is ever populated; the partitioning
77
+ * exists so the registry also behaves correctly when a single
78
+ * instance is reused directly.
79
+ */
80
+ class ToolOutputReferenceRegistry {
81
+ runStates = new Map();
82
+ maxOutputSize;
83
+ maxTotalSize;
84
+ maxActiveRuns;
85
+ /**
86
+ * Local stateful matcher used only by `replaceInString`. Kept
87
+ * off-module so callers of the exported `TOOL_OUTPUT_REF_PATTERN`
88
+ * never see a stale `lastIndex`.
89
+ */
90
+ static PLACEHOLDER_MATCHER = /\{\{(tool\d+turn\d+)\}\}/g;
91
+ constructor(options = {}) {
92
+ /**
93
+ * Per-output default is the same ~400 KB budget as the standard
94
+ * tool-result truncation (`HARD_MAX_TOOL_RESULT_CHARS`). This
95
+ * keeps a single `{{…}}` substitution at a size that is safe to
96
+ * pass through typical shell `ARG_MAX` limits and matches what
97
+ * the LLM would otherwise have seen. Hosts that want larger per-
98
+ * output payloads (API consumers, long JSON streams) can raise
99
+ * the cap explicitly up to the 5 MB total budget.
100
+ */
101
+ const perOutput = options.maxOutputSize != null && options.maxOutputSize > 0
102
+ ? options.maxOutputSize
103
+ : HARD_MAX_TOOL_RESULT_CHARS;
104
+ /**
105
+ * Clamp a caller-supplied `maxTotalSize` to
106
+ * `HARD_MAX_TOTAL_TOOL_OUTPUT_SIZE` (5 MB) so the documented
107
+ * absolute cap is enforced regardless of host config —
108
+ * `calculateMaxTotalToolOutputSize` already applies the same
109
+ * upper bound on its computed default, but the user-provided
110
+ * branch was bypassing it.
111
+ */
112
+ const totalRaw = options.maxTotalSize != null && options.maxTotalSize > 0
113
+ ? Math.min(options.maxTotalSize, HARD_MAX_TOTAL_TOOL_OUTPUT_SIZE)
114
+ : calculateMaxTotalToolOutputSize(perOutput);
115
+ this.maxTotalSize = totalRaw;
116
+ /**
117
+ * The per-output cap can never exceed the per-run aggregate cap:
118
+ * if a single entry were allowed to be larger than `maxTotalSize`,
119
+ * the eviction loop would either blow the cap (to keep the entry)
120
+ * or self-evict a just-stored value. Clamping here turns
121
+ * `maxTotalSize` into a hard upper bound on *any* state the
122
+ * registry retains per run.
123
+ */
124
+ this.maxOutputSize = Math.min(perOutput, totalRaw);
125
+ this.maxActiveRuns =
126
+ options.maxActiveRuns != null && options.maxActiveRuns > 0
127
+ ? options.maxActiveRuns
128
+ : DEFAULT_MAX_ACTIVE_RUNS;
129
+ }
130
+ keyFor(runId) {
131
+ return runId ?? ANON_RUN_KEY;
132
+ }
133
+ getOrCreate(runId) {
134
+ const key = this.keyFor(runId);
135
+ let state = this.runStates.get(key);
136
+ if (state == null) {
137
+ state = new RunStateBucket();
138
+ this.runStates.set(key, state);
139
+ if (this.runStates.size > this.maxActiveRuns) {
140
+ const oldest = this.runStates.keys().next().value;
141
+ if (oldest != null && oldest !== key) {
142
+ this.runStates.delete(oldest);
143
+ }
144
+ }
145
+ }
146
+ return state;
147
+ }
148
+ /** Registers (or replaces) the output stored under `key` for `runId`. */
149
+ set(runId, key, value) {
150
+ const bucket = this.getOrCreate(runId);
151
+ const clipped = value.length > this.maxOutputSize
152
+ ? value.slice(0, this.maxOutputSize)
153
+ : value;
154
+ const existing = bucket.entries.get(key);
155
+ if (existing != null) {
156
+ bucket.totalSize -= existing.length;
157
+ bucket.entries.delete(key);
158
+ }
159
+ bucket.entries.set(key, clipped);
160
+ bucket.totalSize += clipped.length;
161
+ this.evictWithinBucket(bucket);
162
+ }
163
+ /** Returns the stored value for `key` in `runId`'s bucket, or `undefined`. */
164
+ get(runId, key) {
165
+ return this.runStates.get(this.keyFor(runId))?.entries.get(key);
166
+ }
167
+ /**
168
+ * Returns `true` when `key` is currently stored in `runId`'s bucket.
169
+ * Used by {@link annotateMessagesForLLM} to gate transient annotation
170
+ * on whether the registry still owns the referenced output (a stale
171
+ * `_refKey` from a prior run silently no-ops here).
172
+ */
173
+ has(runId, key) {
174
+ return this.runStates.get(this.keyFor(runId))?.entries.has(key) ?? false;
175
+ }
176
+ /** Total number of registered outputs across every run bucket. */
177
+ get size() {
178
+ let n = 0;
179
+ for (const bucket of this.runStates.values()) {
180
+ n += bucket.entries.size;
181
+ }
182
+ return n;
183
+ }
184
+ /** Maximum characters retained per output (post-clip). */
185
+ get perOutputLimit() {
186
+ return this.maxOutputSize;
187
+ }
188
+ /** Maximum total characters retained *per run*. */
189
+ get totalLimit() {
190
+ return this.maxTotalSize;
191
+ }
192
+ /** Drops every run's state. */
193
+ clear() {
194
+ this.runStates.clear();
195
+ }
196
+ /**
197
+ * Explicitly release `runId`'s state. Safe to call when a run has
198
+ * finished. Hosts sharing one registry across runs should call this
199
+ * to reclaim memory deterministically; otherwise LRU eviction kicks
200
+ * in when `maxActiveRuns` runs accumulate.
201
+ */
202
+ releaseRun(runId) {
203
+ this.runStates.delete(this.keyFor(runId));
204
+ }
205
+ /**
206
+ * Claims the next batch turn synchronously from `runId`'s bucket.
207
+ *
208
+ * Must be called once at the start of each ToolNode batch before
209
+ * any `await`, so concurrent invocations within the same run see
210
+ * distinct turn values (reads are effectively atomic by JS's
211
+ * single-threaded execution of the sync prefix).
212
+ *
213
+ * If `runId` is missing the anonymous bucket is dropped and a
214
+ * fresh one created so each anonymous call behaves as its own run.
215
+ */
216
+ nextTurn(runId) {
217
+ if (runId == null) {
218
+ this.runStates.delete(ANON_RUN_KEY);
219
+ }
220
+ const bucket = this.getOrCreate(runId);
221
+ return bucket.turnCounter++;
222
+ }
223
+ /**
224
+ * Records that `toolName` has been warned about in `runId` (returns
225
+ * `true` on the first call per run, `false` after). Used by
226
+ * ToolNode to emit one log line per offending tool per run when a
227
+ * `ToolMessage.content` isn't a string.
228
+ */
229
+ claimWarnOnce(runId, toolName) {
230
+ const bucket = this.getOrCreate(runId);
231
+ if (bucket.warnedNonStringTools.has(toolName)) {
232
+ return false;
233
+ }
234
+ bucket.warnedNonStringTools.add(toolName);
235
+ return true;
236
+ }
237
+ /**
238
+ * Walks `args` and replaces every `{{tool<i>turn<n>}}` placeholder in
239
+ * string values with the stored output *from `runId`'s bucket*. Non-
240
+ * string values and object keys are left untouched. Unresolved
241
+ * references are left in-place and reported so the caller can
242
+ * surface them to the LLM. When no placeholder appears anywhere in
243
+ * the serialized args, the original input is returned without
244
+ * walking the tree.
245
+ */
246
+ resolve(runId, args) {
247
+ if (!hasAnyPlaceholder(args)) {
248
+ return { resolved: args, unresolved: [] };
249
+ }
250
+ const bucket = this.runStates.get(this.keyFor(runId));
251
+ return this.resolveAgainst(bucket?.entries ?? EMPTY_ENTRIES, args);
252
+ }
253
+ /**
254
+ * Captures a frozen snapshot of `runId`'s current entries and
255
+ * returns a view that resolves placeholders against *only* that
256
+ * snapshot. The snapshot is decoupled from the live registry, so
257
+ * subsequent `set()` calls (for example, same-turn direct outputs
258
+ * registering while an event branch is still in flight) are
259
+ * invisible to the snapshot's `resolve`. Used by the mixed
260
+ * direct+event dispatch path to preserve same-turn isolation when
261
+ * a `PreToolUse` hook rewrites event args after directs have
262
+ * completed.
263
+ */
264
+ snapshot(runId) {
265
+ const bucket = this.runStates.get(this.keyFor(runId));
266
+ const entries = bucket
267
+ ? new Map(bucket.entries)
268
+ : EMPTY_ENTRIES;
269
+ return {
270
+ resolve: (args) => this.resolveAgainst(entries, args),
271
+ };
272
+ }
273
+ resolveAgainst(entries, args) {
274
+ if (!hasAnyPlaceholder(args)) {
275
+ return { resolved: args, unresolved: [] };
276
+ }
277
+ const unresolved = new Set();
278
+ const resolved = this.transform(entries, args, unresolved);
279
+ return { resolved, unresolved: Array.from(unresolved) };
280
+ }
281
+ transform(entries, value, unresolved) {
282
+ if (typeof value === 'string') {
283
+ return this.replaceInString(entries, value, unresolved);
284
+ }
285
+ if (Array.isArray(value)) {
286
+ return value.map((item) => this.transform(entries, item, unresolved));
287
+ }
288
+ if (value !== null && typeof value === 'object') {
289
+ const source = value;
290
+ const next = {};
291
+ for (const [key, item] of Object.entries(source)) {
292
+ next[key] = this.transform(entries, item, unresolved);
293
+ }
294
+ return next;
295
+ }
296
+ return value;
297
+ }
298
+ replaceInString(entries, input, unresolved) {
299
+ if (input.indexOf('{{tool') === -1) {
300
+ return input;
301
+ }
302
+ return input.replace(ToolOutputReferenceRegistry.PLACEHOLDER_MATCHER, (match, key) => {
303
+ const stored = entries.get(key);
304
+ if (stored == null) {
305
+ unresolved.add(key);
306
+ return match;
307
+ }
308
+ return stored;
309
+ });
310
+ }
311
+ evictWithinBucket(bucket) {
312
+ if (bucket.totalSize <= this.maxTotalSize) {
313
+ return;
314
+ }
315
+ for (const key of bucket.entries.keys()) {
316
+ if (bucket.totalSize <= this.maxTotalSize) {
317
+ return;
318
+ }
319
+ const entry = bucket.entries.get(key);
320
+ if (entry == null) {
321
+ continue;
322
+ }
323
+ bucket.totalSize -= entry.length;
324
+ bucket.entries.delete(key);
325
+ }
326
+ }
327
+ }
328
+ /**
329
+ * Cheap pre-check: returns true if any string value in `args` contains
330
+ * the `{{tool` substring. Lets `resolve()` skip the deep tree walk (and
331
+ * its object allocations) for the common case of plain args.
332
+ */
333
+ function hasAnyPlaceholder(value) {
334
+ if (typeof value === 'string') {
335
+ return value.indexOf('{{tool') !== -1;
336
+ }
337
+ if (Array.isArray(value)) {
338
+ for (const item of value) {
339
+ if (hasAnyPlaceholder(item)) {
340
+ return true;
341
+ }
342
+ }
343
+ return false;
344
+ }
345
+ if (value !== null && typeof value === 'object') {
346
+ for (const item of Object.values(value)) {
347
+ if (hasAnyPlaceholder(item)) {
348
+ return true;
349
+ }
350
+ }
351
+ return false;
352
+ }
353
+ return false;
354
+ }
355
+ /**
356
+ * Annotates `content` with a reference key and/or unresolved-ref
357
+ * warnings so the LLM sees both alongside the tool output.
358
+ *
359
+ * Behavior:
360
+ * - If `content` parses as a plain (non-array, non-null) JSON object
361
+ * and the object does not already have a conflicting `_ref` key,
362
+ * the reference key and (when present) `_unresolved_refs` array
363
+ * are injected as object fields, preserving JSON validity for
364
+ * downstream consumers that parse the output.
365
+ * - Otherwise (string output, JSON array/primitive, parse failure,
366
+ * or `_ref` collision), a `[ref: <key>]\n` prefix line is
367
+ * prepended and unresolved refs are appended as a trailing
368
+ * `[unresolved refs: …]` line.
369
+ *
370
+ * The annotated string is what the LLM sees as `ToolMessage.content`.
371
+ * The *original* (un-annotated) value is what gets stored in the
372
+ * registry, so downstream piping remains pristine.
373
+ *
374
+ * @param content Raw (post-truncation) tool output.
375
+ * @param key Reference key for this output, or undefined when
376
+ * there is nothing to register (errors etc.).
377
+ * @param unresolved Reference keys that failed to resolve during
378
+ * argument substitution. Surfaced so the LLM can
379
+ * self-correct its next tool call.
380
+ */
381
+ function annotateToolOutputWithReference(content, key, unresolved = []) {
382
+ const hasRefKey = key != null;
383
+ const hasUnresolved = unresolved.length > 0;
384
+ if (!hasRefKey && !hasUnresolved) {
385
+ return content;
386
+ }
387
+ const trimmed = content.trimStart();
388
+ if (trimmed.startsWith('{')) {
389
+ const annotated = tryInjectRefIntoJsonObject(content, key, unresolved);
390
+ if (annotated != null) {
391
+ return annotated;
392
+ }
393
+ }
394
+ const prefix = hasRefKey ? `${buildReferencePrefix(key)}\n` : '';
395
+ const trailer = hasUnresolved
396
+ ? `\n[unresolved refs: ${unresolved.join(', ')}]`
397
+ : '';
398
+ return `${prefix}${content}${trailer}`;
399
+ }
400
+ function tryInjectRefIntoJsonObject(content, key, unresolved) {
401
+ let parsed;
402
+ try {
403
+ parsed = JSON.parse(content);
404
+ }
405
+ catch {
406
+ return null;
407
+ }
408
+ if (parsed === null || typeof parsed !== 'object' || Array.isArray(parsed)) {
409
+ return null;
410
+ }
411
+ const obj = parsed;
412
+ const injectingRef = key != null;
413
+ const injectingUnresolved = unresolved.length > 0;
414
+ /**
415
+ * Reject the JSON-injection path (fall back to prefix form) when
416
+ * either of our keys collides with real payload data:
417
+ * - `_ref` collision: existing value is non-null and differs from
418
+ * the key we're about to inject.
419
+ * - `_unresolved_refs` collision: existing value is non-null and
420
+ * is not a deep-equal match for the array we'd inject.
421
+ * This keeps us from silently overwriting legitimate tool output.
422
+ */
423
+ if (injectingRef &&
424
+ TOOL_OUTPUT_REF_KEY in obj &&
425
+ obj[TOOL_OUTPUT_REF_KEY] !== key &&
426
+ obj[TOOL_OUTPUT_REF_KEY] != null) {
427
+ return null;
428
+ }
429
+ if (injectingUnresolved &&
430
+ TOOL_OUTPUT_UNRESOLVED_KEY in obj &&
431
+ obj[TOOL_OUTPUT_UNRESOLVED_KEY] != null &&
432
+ !arraysShallowEqual(obj[TOOL_OUTPUT_UNRESOLVED_KEY], unresolved)) {
433
+ return null;
434
+ }
435
+ /**
436
+ * Only strip the framework-owned key we're actually injecting —
437
+ * leave everything else (including a pre-existing `_ref` on the
438
+ * unresolved-only path, or a pre-existing `_unresolved_refs` on a
439
+ * plain-annotation path) untouched so we annotate rather than
440
+ * mutate downstream payload data. Our injected keys land first in
441
+ * the serialized JSON so the LLM sees them before the body.
442
+ */
443
+ const omitKeys = new Set();
444
+ if (injectingRef)
445
+ omitKeys.add(TOOL_OUTPUT_REF_KEY);
446
+ if (injectingUnresolved)
447
+ omitKeys.add(TOOL_OUTPUT_UNRESOLVED_KEY);
448
+ const rest = {};
449
+ for (const [k, v] of Object.entries(obj)) {
450
+ if (!omitKeys.has(k)) {
451
+ rest[k] = v;
452
+ }
453
+ }
454
+ const injected = {};
455
+ if (injectingRef) {
456
+ injected[TOOL_OUTPUT_REF_KEY] = key;
457
+ }
458
+ if (injectingUnresolved) {
459
+ injected[TOOL_OUTPUT_UNRESOLVED_KEY] = unresolved;
460
+ }
461
+ Object.assign(injected, rest);
462
+ const pretty = /^\{\s*\n/.test(content);
463
+ return pretty ? JSON.stringify(injected, null, 2) : JSON.stringify(injected);
464
+ }
465
+ function arraysShallowEqual(a, b) {
466
+ if (!Array.isArray(a) || a.length !== b.length) {
467
+ return false;
468
+ }
469
+ for (let i = 0; i < a.length; i++) {
470
+ if (a[i] !== b[i]) {
471
+ return false;
472
+ }
473
+ }
474
+ return true;
475
+ }
476
+ /**
477
+ * Lazy projection that, given a registry and a runId, returns a new
478
+ * `messages` array where each `ToolMessage` carrying ref metadata is
479
+ * projected into a transient copy with annotated content (when the ref
480
+ * is live in the registry) and with the framework-owned `additional_
481
+ * kwargs` keys (`_refKey`, `_refScope`, `_unresolvedRefs`) stripped
482
+ * regardless of whether annotation applied. The original input array
483
+ * and its messages are never mutated.
484
+ *
485
+ * Annotation is gated on registry presence: a stale `_refKey` from a
486
+ * prior run (e.g. one that survived in persisted history) silently
487
+ * no-ops on the *content* side. The strip-metadata side still runs so
488
+ * stale framework keys never leak onto the wire under any custom or
489
+ * future provider serializer that might transmit `additional_kwargs`.
490
+ * `_unresolvedRefs` is always meaningful and is not gated.
491
+ *
492
+ * **Feature-disabled fast path:** when the host hasn't enabled the
493
+ * tool-output-reference feature, the registry is `undefined` and this
494
+ * function returns the input array reference-equal *without iterating
495
+ * a single message*. The loop is exclusive to the feature-enabled
496
+ * code path.
497
+ */
498
+ function annotateMessagesForLLM(messages, registry, runId) {
499
+ if (registry == null)
500
+ return messages;
501
+ /**
502
+ * Lazy-allocate the output array so the common case (no ToolMessage
503
+ * carries framework metadata) returns the input reference-equal with
504
+ * zero allocations beyond the per-message predicate checks.
505
+ */
506
+ let out;
507
+ for (let i = 0; i < messages.length; i++) {
508
+ const m = messages[i];
509
+ if (m._getType() !== 'tool')
510
+ continue;
511
+ /**
512
+ * `additional_kwargs` is untyped at the LangChain layer
513
+ * (`Record<string, unknown>`), so persisted or client-supplied
514
+ * ToolMessages can carry arbitrary shapes under our framework
515
+ * keys. Treat them as untrusted input and coerce defensively
516
+ * before any array operation — a malformed field on a single
517
+ * hydrated message must not crash `attemptInvoke` before the
518
+ * provider call.
519
+ */
520
+ const meta = m.additional_kwargs;
521
+ const hasRefKey = meta != null && '_refKey' in meta;
522
+ const hasRefScope = meta != null && '_refScope' in meta;
523
+ const hasUnresolvedField = meta != null && '_unresolvedRefs' in meta;
524
+ if (!hasRefKey && !hasRefScope && !hasUnresolvedField)
525
+ continue;
526
+ const refKey = readRefKey(meta);
527
+ const unresolved = readUnresolvedRefs(meta);
528
+ /**
529
+ * Prefer the message-stamped `_refScope` for the registry lookup.
530
+ * For named runs it equals the current `runId`; for anonymous
531
+ * invocations it carries the per-batch synthetic scope minted by
532
+ * ToolNode (`\0anon-<n>`), which `runId` from config cannot
533
+ * recover. Falling back to `runId` keeps backward compatibility
534
+ * with messages stamped before this field existed.
535
+ */
536
+ const lookupScope = readRefScope(meta) ?? runId;
537
+ const liveRef = refKey != null && registry.has(lookupScope, refKey) ? refKey : undefined;
538
+ const annotates = liveRef != null || unresolved.length > 0;
539
+ const tm = m;
540
+ let nextContent = tm.content;
541
+ if (annotates && typeof tm.content === 'string') {
542
+ nextContent = annotateToolOutputWithReference(tm.content, liveRef, unresolved);
543
+ }
544
+ else if (annotates &&
545
+ Array.isArray(tm.content) &&
546
+ unresolved.length > 0) {
547
+ const warningBlock = {
548
+ type: 'text',
549
+ text: `[unresolved refs: ${unresolved.join(', ')}]`,
550
+ };
551
+ nextContent = [
552
+ warningBlock,
553
+ ...tm.content,
554
+ ];
555
+ }
556
+ /**
557
+ * Project unconditionally: even when no annotation applies (stale
558
+ * `_refKey` or non-annotatable content), `cloneToolMessageWithContent`
559
+ * runs `stripFrameworkRefMetadata` on `additional_kwargs` so the
560
+ * framework-owned keys never reach the wire.
561
+ */
562
+ out ??= messages.slice();
563
+ out[i] = cloneToolMessageWithContent(tm, nextContent);
564
+ }
565
+ return out ?? messages;
566
+ }
567
+ /**
568
+ * Reads `_refKey` defensively from untyped `additional_kwargs`. Returns
569
+ * undefined for non-string values so a malformed field cannot poison
570
+ * the registry lookup or downstream string operations.
571
+ */
572
+ function readRefKey(meta) {
573
+ const v = meta?._refKey;
574
+ return typeof v === 'string' ? v : undefined;
575
+ }
576
+ /**
577
+ * Reads `_refScope` defensively from untyped `additional_kwargs`.
578
+ * Mirrors {@link readRefKey} — non-string scopes are dropped (the
579
+ * caller falls back to the run-derived scope) rather than passed into
580
+ * the registry as a malformed key.
581
+ */
582
+ function readRefScope(meta) {
583
+ const v = meta?._refScope;
584
+ return typeof v === 'string' ? v : undefined;
585
+ }
586
+ /**
587
+ * Reads `_unresolvedRefs` defensively from untyped `additional_kwargs`.
588
+ * Returns an empty array for any non-array value, and filters out
589
+ * non-string entries from a real array. Without this guard, a hydrated
590
+ * ToolMessage carrying e.g. `_unresolvedRefs: 'tool0turn0'` would crash
591
+ * `attemptInvoke` on the eventual `.length` / `.join(...)` call.
592
+ */
593
+ function readUnresolvedRefs(meta) {
594
+ const v = meta?._unresolvedRefs;
595
+ if (!Array.isArray(v))
596
+ return [];
597
+ const out = [];
598
+ for (const item of v) {
599
+ if (typeof item === 'string')
600
+ out.push(item);
601
+ }
602
+ return out;
603
+ }
604
+ /**
605
+ * Builds a fresh `ToolMessage` that mirrors `tm`'s identity fields with
606
+ * the supplied `content`. Every `ToolMessage` field but `content` is
607
+ * carried over so the projection is structurally identical to the
608
+ * original from a LangChain serializer's perspective.
609
+ *
610
+ * `additional_kwargs` is rebuilt with the framework-owned ref keys
611
+ * stripped. Defensive: LangChain's standard provider serializers do not
612
+ * transmit `additional_kwargs` to provider HTTP APIs, but a custom
613
+ * adapter or future LangChain change could. Stripping keeps the
614
+ * implementation correct under any serializer behavior at the cost of a
615
+ * shallow object spread per annotated message.
616
+ */
617
+ function cloneToolMessageWithContent(tm, content) {
618
+ return new ToolMessage({
619
+ id: tm.id,
620
+ name: tm.name,
621
+ status: tm.status,
622
+ artifact: tm.artifact,
623
+ tool_call_id: tm.tool_call_id,
624
+ response_metadata: tm.response_metadata,
625
+ additional_kwargs: stripFrameworkRefMetadata(tm.additional_kwargs),
626
+ content,
627
+ });
628
+ }
629
+ /**
630
+ * Returns a copy of `kwargs` with `_refKey`, `_refScope`, and
631
+ * `_unresolvedRefs` removed. Returns the input reference-equal when
632
+ * none of those keys are present so the no-strip path stays cheap;
633
+ * returns `undefined` when stripping leaves the object empty so the
634
+ * caller can drop the field entirely.
635
+ */
636
+ function stripFrameworkRefMetadata(kwargs) {
637
+ if (kwargs == null)
638
+ return undefined;
639
+ if (!('_refKey' in kwargs) &&
640
+ !('_refScope' in kwargs) &&
641
+ !('_unresolvedRefs' in kwargs)) {
642
+ return kwargs;
643
+ }
644
+ const { _refKey, _refScope, _unresolvedRefs, ...rest } = kwargs;
645
+ return Object.keys(rest).length === 0 ? undefined : rest;
646
+ }
647
+
648
+ export { TOOL_OUTPUT_REF_KEY, TOOL_OUTPUT_UNRESOLVED_KEY, ToolOutputReferenceRegistry, annotateMessagesForLLM, annotateToolOutputWithReference, buildReferenceKey, buildReferencePrefix };
649
+ //# sourceMappingURL=toolOutputReferences.mjs.map