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