@librechat/agents 3.1.71-dev.0 → 3.1.71
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 +7 -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/tools/BashExecutor.cjs +3 -1
- package/dist/cjs/tools/BashExecutor.cjs.map +1 -1
- package/dist/cjs/tools/ToolNode.cjs +84 -55
- package/dist/cjs/tools/ToolNode.cjs.map +1 -1
- package/dist/cjs/tools/toolOutputReferences.cjs +195 -0
- package/dist/cjs/tools/toolOutputReferences.cjs.map +1 -1
- package/dist/esm/graphs/Graph.mjs +7 -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/tools/BashExecutor.mjs +3 -1
- package/dist/esm/tools/BashExecutor.mjs.map +1 -1
- package/dist/esm/tools/ToolNode.mjs +85 -56
- package/dist/esm/tools/ToolNode.mjs.map +1 -1
- package/dist/esm/tools/toolOutputReferences.mjs +195 -1
- package/dist/esm/tools/toolOutputReferences.mjs.map +1 -1
- package/dist/types/graphs/Graph.d.ts +9 -2
- package/dist/types/llm/invoke.d.ts +29 -3
- package/dist/types/tools/ToolNode.d.ts +11 -13
- package/dist/types/tools/toolOutputReferences.d.ts +31 -0
- package/dist/types/types/index.d.ts +1 -0
- package/dist/types/types/messages.d.ts +26 -0
- package/package.json +1 -1
- package/src/graphs/Graph.ts +8 -1
- package/src/llm/invoke.test.ts +446 -0
- package/src/llm/invoke.ts +45 -5
- package/src/tools/BashExecutor.ts +3 -1
- package/src/tools/ToolNode.ts +94 -81
- package/src/tools/__tests__/BashExecutor.test.ts +13 -0
- package/src/tools/__tests__/ToolNode.outputReferences.test.ts +98 -55
- package/src/tools/__tests__/annotateMessagesForLLM.test.ts +479 -0
- package/src/tools/toolOutputReferences.ts +235 -0
- package/src/types/index.ts +1 -0
- package/src/types/messages.ts +27 -0
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { ToolMessage } from '@langchain/core/messages';
|
|
1
2
|
import { HARD_MAX_TOOL_RESULT_CHARS, HARD_MAX_TOTAL_TOOL_OUTPUT_SIZE, calculateMaxTotalToolOutputSize } from '../utils/truncation.mjs';
|
|
2
3
|
|
|
3
4
|
/**
|
|
@@ -163,6 +164,15 @@ class ToolOutputReferenceRegistry {
|
|
|
163
164
|
get(runId, key) {
|
|
164
165
|
return this.runStates.get(this.keyFor(runId))?.entries.get(key);
|
|
165
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
|
+
}
|
|
166
176
|
/** Total number of registered outputs across every run bucket. */
|
|
167
177
|
get size() {
|
|
168
178
|
let n = 0;
|
|
@@ -463,6 +473,190 @@ function arraysShallowEqual(a, b) {
|
|
|
463
473
|
}
|
|
464
474
|
return true;
|
|
465
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 — including primitives
|
|
515
|
+
* (a malformed serializer might write a string, or `null`).
|
|
516
|
+
* Guard with a runtime object check before the `in` probes
|
|
517
|
+
* because the `in` operator throws `TypeError` on primitives.
|
|
518
|
+
* A single malformed message must never crash the provider call
|
|
519
|
+
* path; skip its annotation/strip and continue.
|
|
520
|
+
*/
|
|
521
|
+
const rawMeta = m.additional_kwargs;
|
|
522
|
+
if (rawMeta == null || typeof rawMeta !== 'object')
|
|
523
|
+
continue;
|
|
524
|
+
const meta = rawMeta;
|
|
525
|
+
const hasRefKey = '_refKey' in meta;
|
|
526
|
+
const hasRefScope = '_refScope' in meta;
|
|
527
|
+
const hasUnresolvedField = '_unresolvedRefs' in meta;
|
|
528
|
+
if (!hasRefKey && !hasRefScope && !hasUnresolvedField)
|
|
529
|
+
continue;
|
|
530
|
+
const refKey = readRefKey(meta);
|
|
531
|
+
const unresolved = readUnresolvedRefs(meta);
|
|
532
|
+
/**
|
|
533
|
+
* Prefer the message-stamped `_refScope` for the registry lookup.
|
|
534
|
+
* For named runs it equals the current `runId`; for anonymous
|
|
535
|
+
* invocations it carries the per-batch synthetic scope minted by
|
|
536
|
+
* ToolNode (`\0anon-<n>`), which `runId` from config cannot
|
|
537
|
+
* recover. Falling back to `runId` keeps backward compatibility
|
|
538
|
+
* with messages stamped before this field existed.
|
|
539
|
+
*/
|
|
540
|
+
const lookupScope = readRefScope(meta) ?? runId;
|
|
541
|
+
const liveRef = refKey != null && registry.has(lookupScope, refKey) ? refKey : undefined;
|
|
542
|
+
const annotates = liveRef != null || unresolved.length > 0;
|
|
543
|
+
const tm = m;
|
|
544
|
+
let nextContent = tm.content;
|
|
545
|
+
if (annotates && typeof tm.content === 'string') {
|
|
546
|
+
nextContent = annotateToolOutputWithReference(tm.content, liveRef, unresolved);
|
|
547
|
+
}
|
|
548
|
+
else if (annotates &&
|
|
549
|
+
Array.isArray(tm.content) &&
|
|
550
|
+
unresolved.length > 0) {
|
|
551
|
+
const warningBlock = {
|
|
552
|
+
type: 'text',
|
|
553
|
+
text: `[unresolved refs: ${unresolved.join(', ')}]`,
|
|
554
|
+
};
|
|
555
|
+
/**
|
|
556
|
+
* `as unknown as ToolMessage['content']` is unavoidable here:
|
|
557
|
+
* LangChain's content union (`MessageContentComplex[] |
|
|
558
|
+
* DataContentBlock[] | string`) does not accept a freshly built
|
|
559
|
+
* mixed array literal even though the structural shape is valid
|
|
560
|
+
* at runtime. The double-cast is structurally safe — we
|
|
561
|
+
* preserve every block from `tm.content` and prepend a single
|
|
562
|
+
* `{ type: 'text', text }` block that all providers accept.
|
|
563
|
+
*/
|
|
564
|
+
nextContent = [
|
|
565
|
+
warningBlock,
|
|
566
|
+
...tm.content,
|
|
567
|
+
];
|
|
568
|
+
}
|
|
569
|
+
/**
|
|
570
|
+
* Project unconditionally: even when no annotation applies (stale
|
|
571
|
+
* `_refKey` or non-annotatable content), `cloneToolMessageWithContent`
|
|
572
|
+
* runs `stripFrameworkRefMetadata` on `additional_kwargs` so the
|
|
573
|
+
* framework-owned keys never reach the wire.
|
|
574
|
+
*/
|
|
575
|
+
out ??= messages.slice();
|
|
576
|
+
out[i] = cloneToolMessageWithContent(tm, nextContent);
|
|
577
|
+
}
|
|
578
|
+
return out ?? messages;
|
|
579
|
+
}
|
|
580
|
+
/**
|
|
581
|
+
* Reads `_refKey` defensively from untyped `additional_kwargs`. Returns
|
|
582
|
+
* undefined for non-string values so a malformed field cannot poison
|
|
583
|
+
* the registry lookup or downstream string operations.
|
|
584
|
+
*/
|
|
585
|
+
function readRefKey(meta) {
|
|
586
|
+
const v = meta?._refKey;
|
|
587
|
+
return typeof v === 'string' ? v : undefined;
|
|
588
|
+
}
|
|
589
|
+
/**
|
|
590
|
+
* Reads `_refScope` defensively from untyped `additional_kwargs`.
|
|
591
|
+
* Mirrors {@link readRefKey} — non-string scopes are dropped (the
|
|
592
|
+
* caller falls back to the run-derived scope) rather than passed into
|
|
593
|
+
* the registry as a malformed key.
|
|
594
|
+
*/
|
|
595
|
+
function readRefScope(meta) {
|
|
596
|
+
const v = meta?._refScope;
|
|
597
|
+
return typeof v === 'string' ? v : undefined;
|
|
598
|
+
}
|
|
599
|
+
/**
|
|
600
|
+
* Reads `_unresolvedRefs` defensively from untyped `additional_kwargs`.
|
|
601
|
+
* Returns an empty array for any non-array value, and filters out
|
|
602
|
+
* non-string entries from a real array. Without this guard, a hydrated
|
|
603
|
+
* ToolMessage carrying e.g. `_unresolvedRefs: 'tool0turn0'` would crash
|
|
604
|
+
* `attemptInvoke` on the eventual `.length` / `.join(...)` call.
|
|
605
|
+
*/
|
|
606
|
+
function readUnresolvedRefs(meta) {
|
|
607
|
+
const v = meta?._unresolvedRefs;
|
|
608
|
+
if (!Array.isArray(v))
|
|
609
|
+
return [];
|
|
610
|
+
const out = [];
|
|
611
|
+
for (const item of v) {
|
|
612
|
+
if (typeof item === 'string')
|
|
613
|
+
out.push(item);
|
|
614
|
+
}
|
|
615
|
+
return out;
|
|
616
|
+
}
|
|
617
|
+
/**
|
|
618
|
+
* Builds a fresh `ToolMessage` that mirrors `tm`'s identity fields with
|
|
619
|
+
* the supplied `content`. Every `ToolMessage` field but `content` is
|
|
620
|
+
* carried over so the projection is structurally identical to the
|
|
621
|
+
* original from a LangChain serializer's perspective.
|
|
622
|
+
*
|
|
623
|
+
* `additional_kwargs` is rebuilt with the framework-owned ref keys
|
|
624
|
+
* stripped. Defensive: LangChain's standard provider serializers do not
|
|
625
|
+
* transmit `additional_kwargs` to provider HTTP APIs, but a custom
|
|
626
|
+
* adapter or future LangChain change could. Stripping keeps the
|
|
627
|
+
* implementation correct under any serializer behavior at the cost of a
|
|
628
|
+
* shallow object spread per annotated message.
|
|
629
|
+
*/
|
|
630
|
+
function cloneToolMessageWithContent(tm, content) {
|
|
631
|
+
return new ToolMessage({
|
|
632
|
+
id: tm.id,
|
|
633
|
+
name: tm.name,
|
|
634
|
+
status: tm.status,
|
|
635
|
+
artifact: tm.artifact,
|
|
636
|
+
tool_call_id: tm.tool_call_id,
|
|
637
|
+
response_metadata: tm.response_metadata,
|
|
638
|
+
additional_kwargs: stripFrameworkRefMetadata(tm.additional_kwargs),
|
|
639
|
+
content,
|
|
640
|
+
});
|
|
641
|
+
}
|
|
642
|
+
/**
|
|
643
|
+
* Returns a copy of `kwargs` with `_refKey`, `_refScope`, and
|
|
644
|
+
* `_unresolvedRefs` removed. Returns the input reference-equal when
|
|
645
|
+
* none of those keys are present so the no-strip path stays cheap;
|
|
646
|
+
* returns `undefined` when stripping leaves the object empty so the
|
|
647
|
+
* caller can drop the field entirely.
|
|
648
|
+
*/
|
|
649
|
+
function stripFrameworkRefMetadata(kwargs) {
|
|
650
|
+
if (kwargs == null)
|
|
651
|
+
return undefined;
|
|
652
|
+
if (!('_refKey' in kwargs) &&
|
|
653
|
+
!('_refScope' in kwargs) &&
|
|
654
|
+
!('_unresolvedRefs' in kwargs)) {
|
|
655
|
+
return kwargs;
|
|
656
|
+
}
|
|
657
|
+
const { _refKey, _refScope, _unresolvedRefs, ...rest } = kwargs;
|
|
658
|
+
return Object.keys(rest).length === 0 ? undefined : rest;
|
|
659
|
+
}
|
|
466
660
|
|
|
467
|
-
export { TOOL_OUTPUT_REF_KEY, TOOL_OUTPUT_UNRESOLVED_KEY, ToolOutputReferenceRegistry, annotateToolOutputWithReference, buildReferenceKey, buildReferencePrefix };
|
|
661
|
+
export { TOOL_OUTPUT_REF_KEY, TOOL_OUTPUT_UNRESOLVED_KEY, ToolOutputReferenceRegistry, annotateMessagesForLLM, annotateToolOutputWithReference, buildReferenceKey, buildReferencePrefix };
|
|
468
662
|
//# sourceMappingURL=toolOutputReferences.mjs.map
|
|
@@ -1 +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 (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AE;AACjB,YAAA,OAAO,KAAK;QACd;IACF;AACA,IAAA,OAAO,IAAI;AACb;;;;"}
|
|
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 { ToolMessage } from '@langchain/core/messages';\nimport type { BaseMessage } from '@langchain/core/messages';\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 /**\n * Returns `true` when `key` is currently stored in `runId`'s bucket.\n * Used by {@link annotateMessagesForLLM} to gate transient annotation\n * on whether the registry still owns the referenced output (a stale\n * `_refKey` from a prior run silently no-ops here).\n */\n has(runId: string | undefined, key: string): boolean {\n return this.runStates.get(this.keyFor(runId))?.entries.has(key) ?? false;\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\n/**\n * Lazy projection that, given a registry and a runId, returns a new\n * `messages` array where each `ToolMessage` carrying ref metadata is\n * projected into a transient copy with annotated content (when the ref\n * is live in the registry) and with the framework-owned `additional_\n * kwargs` keys (`_refKey`, `_refScope`, `_unresolvedRefs`) stripped\n * regardless of whether annotation applied. The original input array\n * and its messages are never mutated.\n *\n * Annotation is gated on registry presence: a stale `_refKey` from a\n * prior run (e.g. one that survived in persisted history) silently\n * no-ops on the *content* side. The strip-metadata side still runs so\n * stale framework keys never leak onto the wire under any custom or\n * future provider serializer that might transmit `additional_kwargs`.\n * `_unresolvedRefs` is always meaningful and is not gated.\n *\n * **Feature-disabled fast path:** when the host hasn't enabled the\n * tool-output-reference feature, the registry is `undefined` and this\n * function returns the input array reference-equal *without iterating\n * a single message*. The loop is exclusive to the feature-enabled\n * code path.\n */\nexport function annotateMessagesForLLM(\n messages: BaseMessage[],\n registry: ToolOutputReferenceRegistry | undefined,\n runId: string | undefined\n): BaseMessage[] {\n if (registry == null) return messages;\n\n /**\n * Lazy-allocate the output array so the common case (no ToolMessage\n * carries framework metadata) returns the input reference-equal with\n * zero allocations beyond the per-message predicate checks.\n */\n let out: BaseMessage[] | undefined;\n for (let i = 0; i < messages.length; i++) {\n const m = messages[i];\n if (m._getType() !== 'tool') continue;\n /**\n * `additional_kwargs` is untyped at the LangChain layer\n * (`Record<string, unknown>`), so persisted or client-supplied\n * ToolMessages can carry arbitrary shapes — including primitives\n * (a malformed serializer might write a string, or `null`).\n * Guard with a runtime object check before the `in` probes\n * because the `in` operator throws `TypeError` on primitives.\n * A single malformed message must never crash the provider call\n * path; skip its annotation/strip and continue.\n */\n const rawMeta = m.additional_kwargs as unknown;\n if (rawMeta == null || typeof rawMeta !== 'object') continue;\n const meta = rawMeta as Record<string, unknown>;\n const hasRefKey = '_refKey' in meta;\n const hasRefScope = '_refScope' in meta;\n const hasUnresolvedField = '_unresolvedRefs' in meta;\n if (!hasRefKey && !hasRefScope && !hasUnresolvedField) continue;\n\n const refKey = readRefKey(meta);\n const unresolved = readUnresolvedRefs(meta);\n\n /**\n * Prefer the message-stamped `_refScope` for the registry lookup.\n * For named runs it equals the current `runId`; for anonymous\n * invocations it carries the per-batch synthetic scope minted by\n * ToolNode (`\\0anon-<n>`), which `runId` from config cannot\n * recover. Falling back to `runId` keeps backward compatibility\n * with messages stamped before this field existed.\n */\n const lookupScope = readRefScope(meta) ?? runId;\n const liveRef =\n refKey != null && registry.has(lookupScope, refKey) ? refKey : undefined;\n const annotates = liveRef != null || unresolved.length > 0;\n\n const tm = m as ToolMessage;\n let nextContent: ToolMessage['content'] = tm.content;\n\n if (annotates && typeof tm.content === 'string') {\n nextContent = annotateToolOutputWithReference(\n tm.content,\n liveRef,\n unresolved\n );\n } else if (\n annotates &&\n Array.isArray(tm.content) &&\n unresolved.length > 0\n ) {\n const warningBlock = {\n type: 'text' as const,\n text: `[unresolved refs: ${unresolved.join(', ')}]`,\n };\n /**\n * `as unknown as ToolMessage['content']` is unavoidable here:\n * LangChain's content union (`MessageContentComplex[] |\n * DataContentBlock[] | string`) does not accept a freshly built\n * mixed array literal even though the structural shape is valid\n * at runtime. The double-cast is structurally safe — we\n * preserve every block from `tm.content` and prepend a single\n * `{ type: 'text', text }` block that all providers accept.\n */\n nextContent = [\n warningBlock,\n ...tm.content,\n ] as unknown as ToolMessage['content'];\n }\n\n /**\n * Project unconditionally: even when no annotation applies (stale\n * `_refKey` or non-annotatable content), `cloneToolMessageWithContent`\n * runs `stripFrameworkRefMetadata` on `additional_kwargs` so the\n * framework-owned keys never reach the wire.\n */\n out ??= messages.slice();\n out[i] = cloneToolMessageWithContent(tm, nextContent);\n }\n\n return out ?? messages;\n}\n\n/**\n * Reads `_refKey` defensively from untyped `additional_kwargs`. Returns\n * undefined for non-string values so a malformed field cannot poison\n * the registry lookup or downstream string operations.\n */\nfunction readRefKey(\n meta: Record<string, unknown> | undefined\n): string | undefined {\n const v = meta?._refKey;\n return typeof v === 'string' ? v : undefined;\n}\n\n/**\n * Reads `_refScope` defensively from untyped `additional_kwargs`.\n * Mirrors {@link readRefKey} — non-string scopes are dropped (the\n * caller falls back to the run-derived scope) rather than passed into\n * the registry as a malformed key.\n */\nfunction readRefScope(\n meta: Record<string, unknown> | undefined\n): string | undefined {\n const v = meta?._refScope;\n return typeof v === 'string' ? v : undefined;\n}\n\n/**\n * Reads `_unresolvedRefs` defensively from untyped `additional_kwargs`.\n * Returns an empty array for any non-array value, and filters out\n * non-string entries from a real array. Without this guard, a hydrated\n * ToolMessage carrying e.g. `_unresolvedRefs: 'tool0turn0'` would crash\n * `attemptInvoke` on the eventual `.length` / `.join(...)` call.\n */\nfunction readUnresolvedRefs(\n meta: Record<string, unknown> | undefined\n): string[] {\n const v = meta?._unresolvedRefs;\n if (!Array.isArray(v)) return [];\n const out: string[] = [];\n for (const item of v) {\n if (typeof item === 'string') out.push(item);\n }\n return out;\n}\n\n/**\n * Builds a fresh `ToolMessage` that mirrors `tm`'s identity fields with\n * the supplied `content`. Every `ToolMessage` field but `content` is\n * carried over so the projection is structurally identical to the\n * original from a LangChain serializer's perspective.\n *\n * `additional_kwargs` is rebuilt with the framework-owned ref keys\n * stripped. Defensive: LangChain's standard provider serializers do not\n * transmit `additional_kwargs` to provider HTTP APIs, but a custom\n * adapter or future LangChain change could. Stripping keeps the\n * implementation correct under any serializer behavior at the cost of a\n * shallow object spread per annotated message.\n */\nfunction cloneToolMessageWithContent(\n tm: ToolMessage,\n content: ToolMessage['content']\n): ToolMessage {\n return new ToolMessage({\n id: tm.id,\n name: tm.name,\n status: tm.status,\n artifact: tm.artifact,\n tool_call_id: tm.tool_call_id,\n response_metadata: tm.response_metadata,\n additional_kwargs: stripFrameworkRefMetadata(tm.additional_kwargs),\n content,\n });\n}\n\n/**\n * Returns a copy of `kwargs` with `_refKey`, `_refScope`, and\n * `_unresolvedRefs` removed. Returns the input reference-equal when\n * none of those keys are present so the no-strip path stays cheap;\n * returns `undefined` when stripping leaves the object empty so the\n * caller can drop the field entirely.\n */\nfunction stripFrameworkRefMetadata(\n kwargs: Record<string, unknown> | undefined\n): Record<string, unknown> | undefined {\n if (kwargs == null) return undefined;\n if (\n !('_refKey' in kwargs) &&\n !('_refScope' in kwargs) &&\n !('_unresolvedRefs' in kwargs)\n ) {\n return kwargs;\n }\n const { _refKey, _refScope, _unresolvedRefs, ...rest } = kwargs as Record<\n string,\n unknown\n > & {\n _refKey?: unknown;\n _refScope?: unknown;\n _unresolvedRefs?: unknown;\n };\n void _refKey;\n void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AAiB,IAAI,MAAM,CAAC,EAC9B;AACA,QAAA,OAAO,MAAM;IACf;AACA,IAAA,MAAM,EAAE,OAAO,EAAE,SAAS,EAAE,eAAe,EAAE,GAAG,IAAI,EAAE,GAAG,MAOxD;AAID,IAAA,OAAO,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,MAAM,KAAK,CAAC,GAAG,SAAS,GAAG,IAAI;AAC1D;;;;"}
|
|
@@ -76,8 +76,15 @@ export declare abstract class Graph<T extends t.BaseGraphState = t.BaseGraphStat
|
|
|
76
76
|
* constructing it on first access. Returns `undefined` when the
|
|
77
77
|
* feature is disabled. All ToolNodes compiled from this graph share
|
|
78
78
|
* this single instance so cross-agent `{{…}}` references resolve.
|
|
79
|
-
|
|
80
|
-
|
|
79
|
+
*
|
|
80
|
+
* @internal Public so `attemptInvoke` can read it through the typed
|
|
81
|
+
* `InvokeContext` and project ToolMessages into LLM-facing annotated
|
|
82
|
+
* copies right before each provider call (see
|
|
83
|
+
* `annotateMessagesForLLM`). Host code should not call this directly
|
|
84
|
+
* — registry mutations outside the ToolNode lifecycle break the
|
|
85
|
+
* partitioning, eviction, and turn-counter invariants.
|
|
86
|
+
*/
|
|
87
|
+
getOrCreateToolOutputRegistry(): ToolOutputReferenceRegistry | undefined;
|
|
81
88
|
}
|
|
82
89
|
export declare class StandardGraph extends Graph<t.BaseGraphState, t.GraphNode> {
|
|
83
90
|
overrideModel?: t.ChatModel;
|
|
@@ -1,14 +1,40 @@
|
|
|
1
1
|
import { AIMessageChunk } from '@langchain/core/messages';
|
|
2
2
|
import type { RunnableConfig } from '@langchain/core/runnables';
|
|
3
3
|
import type { BaseMessage } from '@langchain/core/messages';
|
|
4
|
+
import type { ToolOutputReferenceRegistry } from '@/tools/toolOutputReferences';
|
|
4
5
|
import type * as t from '@/types';
|
|
5
6
|
import { ChatModelStreamHandler } from '@/stream';
|
|
6
7
|
import { Providers } from '@/common';
|
|
7
8
|
/**
|
|
8
|
-
* Context passed to `attemptInvoke
|
|
9
|
-
*
|
|
9
|
+
* Context passed to `attemptInvoke`. Matches the subset of Graph that
|
|
10
|
+
* `ChatModelStreamHandler.handle` needs *plus* the explicit
|
|
11
|
+
* `getOrCreateToolOutputRegistry()` accessor that `attemptInvoke`
|
|
12
|
+
* itself calls to pull the run-scoped tool-output registry off the
|
|
13
|
+
* graph and project each relevant ToolMessage into a transient
|
|
14
|
+
* annotated copy before the provider call.
|
|
15
|
+
*
|
|
16
|
+
* The intersection is intentional: `Parameters<...>[3]` resolves
|
|
17
|
+
* indirectly through the stream handler's signature (which returns
|
|
18
|
+
* `StandardGraph` and already exposes the accessor since #117), but
|
|
19
|
+
* stating it explicitly here surfaces the contract at the call site —
|
|
20
|
+
* a developer reading `attemptInvoke` doesn't have to chase the
|
|
21
|
+
* upstream handler's parameter list to discover that
|
|
22
|
+
* `context?.getOrCreateToolOutputRegistry()` is a real thing. Single
|
|
23
|
+
* optional chain only — the method itself is required on the
|
|
24
|
+
* `StandardGraph` branch of the intersection, so the second `?.` is
|
|
25
|
+
* unnecessary at the call site.
|
|
26
|
+
*
|
|
27
|
+
* `NonNullable<...>` strips `undefined` from the upstream parameter
|
|
28
|
+
* type so the intersection doesn't collapse to `never` on the
|
|
29
|
+
* undefined branch; callers express optionality via `context?:
|
|
30
|
+
* InvokeContext` on the function signature instead.
|
|
31
|
+
*
|
|
32
|
+
* Callers without a registry (e.g. summarization) simply pass no
|
|
33
|
+
* `context` and the transform safely no-ops.
|
|
10
34
|
*/
|
|
11
|
-
export type InvokeContext = Parameters<ChatModelStreamHandler['handle']>[3]
|
|
35
|
+
export type InvokeContext = NonNullable<Parameters<ChatModelStreamHandler['handle']>[3]> & {
|
|
36
|
+
getOrCreateToolOutputRegistry?(): ToolOutputReferenceRegistry | undefined;
|
|
37
|
+
};
|
|
12
38
|
/**
|
|
13
39
|
* Per-chunk callback for custom stream processing.
|
|
14
40
|
* When provided, replaces the default `ChatModelStreamHandler`.
|
|
@@ -97,27 +97,25 @@ export declare class ToolNode<T = any> extends RunnableCallable<T, T> {
|
|
|
97
97
|
*/
|
|
98
98
|
protected runTool(call: ToolCall, config: RunnableConfig, batchContext?: RunToolBatchContext): Promise<BaseMessage | Command>;
|
|
99
99
|
/**
|
|
100
|
-
*
|
|
101
|
-
*
|
|
102
|
-
*
|
|
100
|
+
* Registers the full, raw output under `refKey` (when provided) and
|
|
101
|
+
* builds the per-message ref metadata stamped onto the resulting
|
|
102
|
+
* `ToolMessage.additional_kwargs`. The metadata is read at LLM-
|
|
103
|
+
* request time by `annotateMessagesForLLM` to produce a transient
|
|
104
|
+
* annotated copy of the message — the persisted `content` itself
|
|
105
|
+
* stays clean.
|
|
103
106
|
*
|
|
104
|
-
* @param llmContent The content string the LLM will see. This is
|
|
105
|
-
* the already-truncated, post-hook view; the annotation is
|
|
106
|
-
* applied on top of it.
|
|
107
107
|
* @param registryContent The full, untruncated output to store in
|
|
108
108
|
* the registry so `{{tool<i>turn<n>}}` substitutions deliver the
|
|
109
109
|
* complete payload. Ignored when `refKey` is undefined.
|
|
110
110
|
* @param refKey Precomputed `tool<i>turn<n>` key, or undefined when
|
|
111
111
|
* the output is not to be registered (errors, disabled feature,
|
|
112
112
|
* unavailable batch/turn).
|
|
113
|
-
* @param unresolved Placeholder keys that did not resolve;
|
|
114
|
-
*
|
|
115
|
-
*
|
|
116
|
-
*
|
|
117
|
-
* so parallel `invoke()` calls on the same ToolNode cannot race on
|
|
118
|
-
* the shared turn field.
|
|
113
|
+
* @param unresolved Placeholder keys that did not resolve; surfaced
|
|
114
|
+
* to the LLM lazily so it can self-correct.
|
|
115
|
+
* @returns A `ToolMessageRefMetadata` object when there is anything
|
|
116
|
+
* to stamp, otherwise `undefined`.
|
|
119
117
|
*/
|
|
120
|
-
private
|
|
118
|
+
private recordOutputReference;
|
|
121
119
|
/**
|
|
122
120
|
* Builds code session context for injection into event-driven tool calls.
|
|
123
121
|
* Mirrors the session injection logic in runTool() for direct execution.
|
|
@@ -21,6 +21,7 @@
|
|
|
21
21
|
* registry pristine means downstream bash/jq piping receives the
|
|
22
22
|
* complete, verbatim output with no injected fields.
|
|
23
23
|
*/
|
|
24
|
+
import type { BaseMessage } from '@langchain/core/messages';
|
|
24
25
|
/**
|
|
25
26
|
* Non-global matcher for a single `{{tool<i>turn<n>}}` placeholder.
|
|
26
27
|
* Exported for consumers that want to detect references (e.g., syntax
|
|
@@ -115,6 +116,13 @@ export declare class ToolOutputReferenceRegistry {
|
|
|
115
116
|
set(runId: string | undefined, key: string, value: string): void;
|
|
116
117
|
/** Returns the stored value for `key` in `runId`'s bucket, or `undefined`. */
|
|
117
118
|
get(runId: string | undefined, key: string): string | undefined;
|
|
119
|
+
/**
|
|
120
|
+
* Returns `true` when `key` is currently stored in `runId`'s bucket.
|
|
121
|
+
* Used by {@link annotateMessagesForLLM} to gate transient annotation
|
|
122
|
+
* on whether the registry still owns the referenced output (a stale
|
|
123
|
+
* `_refKey` from a prior run silently no-ops here).
|
|
124
|
+
*/
|
|
125
|
+
has(runId: string | undefined, key: string): boolean;
|
|
118
126
|
/** Total number of registered outputs across every run bucket. */
|
|
119
127
|
get size(): number;
|
|
120
128
|
/** Maximum characters retained per output (post-clip). */
|
|
@@ -203,3 +211,26 @@ export declare class ToolOutputReferenceRegistry {
|
|
|
203
211
|
* self-correct its next tool call.
|
|
204
212
|
*/
|
|
205
213
|
export declare function annotateToolOutputWithReference(content: string, key: string | undefined, unresolved?: string[]): string;
|
|
214
|
+
/**
|
|
215
|
+
* Lazy projection that, given a registry and a runId, returns a new
|
|
216
|
+
* `messages` array where each `ToolMessage` carrying ref metadata is
|
|
217
|
+
* projected into a transient copy with annotated content (when the ref
|
|
218
|
+
* is live in the registry) and with the framework-owned `additional_
|
|
219
|
+
* kwargs` keys (`_refKey`, `_refScope`, `_unresolvedRefs`) stripped
|
|
220
|
+
* regardless of whether annotation applied. The original input array
|
|
221
|
+
* and its messages are never mutated.
|
|
222
|
+
*
|
|
223
|
+
* Annotation is gated on registry presence: a stale `_refKey` from a
|
|
224
|
+
* prior run (e.g. one that survived in persisted history) silently
|
|
225
|
+
* no-ops on the *content* side. The strip-metadata side still runs so
|
|
226
|
+
* stale framework keys never leak onto the wire under any custom or
|
|
227
|
+
* future provider serializer that might transmit `additional_kwargs`.
|
|
228
|
+
* `_unresolvedRefs` is always meaningful and is not gated.
|
|
229
|
+
*
|
|
230
|
+
* **Feature-disabled fast path:** when the host hasn't enabled the
|
|
231
|
+
* tool-output-reference feature, the registry is `undefined` and this
|
|
232
|
+
* function returns the input array reference-equal *without iterating
|
|
233
|
+
* a single message*. The loop is exclusive to the feature-enabled
|
|
234
|
+
* code path.
|
|
235
|
+
*/
|
|
236
|
+
export declare function annotateMessagesForLLM(messages: BaseMessage[], registry: ToolOutputReferenceRegistry | undefined, runId: string | undefined): BaseMessage[];
|
|
@@ -2,3 +2,29 @@ import type Anthropic from '@anthropic-ai/sdk';
|
|
|
2
2
|
import type { BaseMessage } from '@langchain/core/messages';
|
|
3
3
|
export type AnthropicMessages = Array<AnthropicMessage | BaseMessage>;
|
|
4
4
|
export type AnthropicMessage = Anthropic.MessageParam;
|
|
5
|
+
/**
|
|
6
|
+
* Per-message ref metadata stamped onto a `ToolMessage` at execution
|
|
7
|
+
* time. Read by `annotateMessagesForLLM` to apply transient annotation
|
|
8
|
+
* to a copy of the message right before it goes on the wire to the
|
|
9
|
+
* provider. Never read after the run-scoped registry has been cleared.
|
|
10
|
+
*
|
|
11
|
+
* Lives in `ToolMessage.additional_kwargs`. LangChain's provider
|
|
12
|
+
* serializers don't transmit `additional_kwargs` to provider APIs, so
|
|
13
|
+
* the metadata never leaks even if you forget to clean it.
|
|
14
|
+
*/
|
|
15
|
+
export interface ToolMessageRefMetadata {
|
|
16
|
+
/** Key under which this message's untruncated output was registered. */
|
|
17
|
+
_refKey?: string;
|
|
18
|
+
/**
|
|
19
|
+
* Registry bucket scope under which `_refKey` was stored. For named
|
|
20
|
+
* runs this equals `config.configurable.run_id`; for anonymous
|
|
21
|
+
* invocations (no `run_id`) ToolNode mints a per-batch synthetic
|
|
22
|
+
* scope (`\0anon-<n>`) so concurrent batches don't collide. Stamping
|
|
23
|
+
* the scope on the message itself lets `annotateMessagesForLLM`
|
|
24
|
+
* recover it without re-deriving from config — which is impossible
|
|
25
|
+
* for the anonymous case, since the scope is internal to ToolNode.
|
|
26
|
+
*/
|
|
27
|
+
_refScope?: string;
|
|
28
|
+
/** Placeholders the model used that could not be resolved this batch. */
|
|
29
|
+
_unresolvedRefs?: string[];
|
|
30
|
+
}
|
package/package.json
CHANGED
package/src/graphs/Graph.ts
CHANGED
|
@@ -186,8 +186,15 @@ export abstract class Graph<
|
|
|
186
186
|
* constructing it on first access. Returns `undefined` when the
|
|
187
187
|
* feature is disabled. All ToolNodes compiled from this graph share
|
|
188
188
|
* this single instance so cross-agent `{{…}}` references resolve.
|
|
189
|
+
*
|
|
190
|
+
* @internal Public so `attemptInvoke` can read it through the typed
|
|
191
|
+
* `InvokeContext` and project ToolMessages into LLM-facing annotated
|
|
192
|
+
* copies right before each provider call (see
|
|
193
|
+
* `annotateMessagesForLLM`). Host code should not call this directly
|
|
194
|
+
* — registry mutations outside the ToolNode lifecycle break the
|
|
195
|
+
* partitioning, eviction, and turn-counter invariants.
|
|
189
196
|
*/
|
|
190
|
-
|
|
197
|
+
public getOrCreateToolOutputRegistry():
|
|
191
198
|
| ToolOutputReferenceRegistry
|
|
192
199
|
| undefined {
|
|
193
200
|
if (this.toolOutputReferences?.enabled !== true) {
|