@struct-ai/sdk 0.2.1 → 0.3.17
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/README.md +48 -8
- package/dist/commonjs/context.d.ts +26 -0
- package/dist/commonjs/context.js +20 -1
- package/dist/commonjs/core.d.ts +2 -0
- package/dist/commonjs/core.js +133 -28
- package/dist/commonjs/events.js +3 -3
- package/dist/commonjs/integrations/anthropic.d.ts +2 -0
- package/dist/commonjs/integrations/anthropic.js +470 -47
- package/dist/commonjs/integrations/langchain-callback.d.ts +179 -27
- package/dist/commonjs/integrations/langchain-callback.js +670 -81
- package/dist/commonjs/integrations/langchain.d.ts +3 -0
- package/dist/commonjs/integrations/langchain.js +353 -7
- package/dist/commonjs/semconv.d.ts +11 -0
- package/dist/commonjs/semconv.js +12 -1
- package/dist/commonjs/version.d.ts +2 -0
- package/dist/commonjs/version.js +6 -0
- package/dist/esm/context.d.ts +26 -0
- package/dist/esm/context.js +18 -1
- package/dist/esm/core.d.ts +2 -0
- package/dist/esm/core.js +135 -30
- package/dist/esm/events.js +3 -3
- package/dist/esm/integrations/anthropic.d.ts +2 -0
- package/dist/esm/integrations/anthropic.js +471 -50
- package/dist/esm/integrations/langchain-callback.d.ts +179 -27
- package/dist/esm/integrations/langchain-callback.js +672 -83
- package/dist/esm/integrations/langchain.d.ts +3 -0
- package/dist/esm/integrations/langchain.js +352 -7
- package/dist/esm/semconv.d.ts +11 -0
- package/dist/esm/semconv.js +11 -0
- package/dist/esm/version.d.ts +2 -0
- package/dist/esm/version.js +3 -0
- package/package.json +13 -11
|
@@ -26,29 +26,37 @@ const langchain_content_js_1 = require("./langchain-content.js");
|
|
|
26
26
|
*
|
|
27
27
|
* - `gen_ai.conversation.id` is the OTel GenAI-spec conversation identifier
|
|
28
28
|
* and Struct's UI grouping unit — one value = one entry in the sessions
|
|
29
|
-
* list.
|
|
30
|
-
*
|
|
29
|
+
* list. It is one id per run: every span belonging to a top-level agent
|
|
30
|
+
* invocation AND any subagents it spawns shares the SAME
|
|
31
|
+
* `gen_ai.conversation.id`, so the whole call tree collapses into one
|
|
32
|
+
* session (OTel's `conversation.id` models "the thread", not "the
|
|
33
|
+
* agent").
|
|
31
34
|
*
|
|
32
|
-
*
|
|
33
|
-
*
|
|
34
|
-
* own
|
|
35
|
-
*
|
|
36
|
-
*
|
|
37
|
-
*
|
|
38
|
-
* spans would collapse into the outer session, burying delegation.
|
|
35
|
+
* SUBAGENTS (an agent invoked from inside another's tool body) therefore
|
|
36
|
+
* INHERIT the parent run's `gen_ai.conversation.id` rather than minting
|
|
37
|
+
* their own. Structural linkage — "this invoke_agent span was spawned by
|
|
38
|
+
* that one" — is carried separately via the `struct.agent.parent_session_id`
|
|
39
|
+
* span attribute (what powers "Spawned by" / subagent navigation in the
|
|
40
|
+
* UI), not by giving the subagent a different session id.
|
|
39
41
|
*
|
|
40
|
-
*
|
|
41
|
-
*
|
|
42
|
-
*
|
|
43
|
-
*
|
|
44
|
-
*
|
|
45
|
-
*
|
|
42
|
+
* A subagent MAY still be started with its own explicit thread_id (e.g. a
|
|
43
|
+
* LangGraph checkpoint key distinct from the parent's). When that happens
|
|
44
|
+
* we preserve it as `struct.agent.thread_id` for observability, but it does
|
|
45
|
+
* NOT split the session — `gen_ai.conversation.id` still follows the
|
|
46
|
+
* parent, because the grouping unit is the run, not the checkpoint.
|
|
47
|
+
*
|
|
48
|
+
* Chat / tool / retriever spans NEVER fabricate a conversation id. If no
|
|
49
|
+
* ancestor run, metadata thread_id, or ambient session supplies one, the
|
|
50
|
+
* span (and any log events it emits) simply omits `gen_ai.conversation.id`
|
|
51
|
+
* — an orphan span with no session is preferable to a fake one that can
|
|
52
|
+
* never be joined to anything else.
|
|
46
53
|
*
|
|
47
54
|
* End-user guidance:
|
|
48
55
|
* - Use thread_id per conversation; multi-turn chats reuse it.
|
|
49
|
-
* -
|
|
50
|
-
*
|
|
51
|
-
*
|
|
56
|
+
* - Subagents inherit the enclosing run's session automatically; you
|
|
57
|
+
* don't need to (and shouldn't) pass a different thread_id purely to
|
|
58
|
+
* separate them — use `struct.agent.parent_session_id` /
|
|
59
|
+
* "Spawned by" navigation in the UI instead.
|
|
52
60
|
*/
|
|
53
61
|
class StructCallbackHandler {
|
|
54
62
|
sdk;
|
|
@@ -73,6 +81,54 @@ class StructCallbackHandler {
|
|
|
73
81
|
raiseError = false;
|
|
74
82
|
runs = new Map();
|
|
75
83
|
internalLogger;
|
|
84
|
+
/**
|
|
85
|
+
* Index of live `execute_tool` spans by their `langgraph_checkpoint_ns`,
|
|
86
|
+
* keyed to an ARRAY of currently-live spans rather than a single span.
|
|
87
|
+
*
|
|
88
|
+
* The re-parenting trick this supports: LangGraph stamps a namespace
|
|
89
|
+
* (`tools:<uuid>`) on a tool-call branch AND the same value on the
|
|
90
|
+
* sub-agent graph that tool triggers, so a sub-agent's chain-start (whose
|
|
91
|
+
* `parentRunId` points at the parent GRAPH — a sibling of the tool in the
|
|
92
|
+
* run tree, not the tool itself) can look itself up by ns and re-parent
|
|
93
|
+
* under its triggering tool call instead of landing as a sibling.
|
|
94
|
+
*
|
|
95
|
+
* That assumption — "ns is unique even across parallel same-named tool
|
|
96
|
+
* calls" — is FALSE on every `@langchain/langgraph` release checked, up
|
|
97
|
+
* to and including the latest published as of this writing (`1.4.7`, and
|
|
98
|
+
* confirmed byte-identical on `main` at HEAD): `ToolNode.run()`
|
|
99
|
+
* (`0.2.x`: `dist/prebuilt/tool_node.js`; `1.x`: moved to
|
|
100
|
+
* `libs/langgraph-core/src/prebuilt/tool_node.ts`) invokes every parallel
|
|
101
|
+
* `tool_call` within one step (`Promise.all(...map(call => this.runTool(
|
|
102
|
+
* call, config, input)))`) with the exact SAME `config` object reference
|
|
103
|
+
* — no per-call config cloning, no call-index-derived namespace — so
|
|
104
|
+
* `config.metadata.langgraph_checkpoint_ns` collides across parallel
|
|
105
|
+
* same-named tool calls in that step on every version, not just 0.2.x.
|
|
106
|
+
* (One opt-in exception: routing tool calls through LangGraph `Send`
|
|
107
|
+
* objects, added ~PR #1498, gets Pregel's normal per-task namespacing —
|
|
108
|
+
* but `createReactAgent`/`ToolNode`'s default path does not use it.) If a
|
|
109
|
+
* future release fixes this, update this comment with the version and
|
|
110
|
+
* consider re-enabling direct pairing unconditionally on it. (Python's
|
|
111
|
+
* `langgraph.prebuilt.tool_node.ToolNode._afunc` does not have this
|
|
112
|
+
* problem: it calls `get_config_list(config, len(tool_calls))` to build a
|
|
113
|
+
* genuinely per-call config before fanning out.)
|
|
114
|
+
*
|
|
115
|
+
* An array-per-ns (instead of one span per ns) makes the collision
|
|
116
|
+
* DETECTABLE instead of silently last-write-wins: at consume time
|
|
117
|
+
* (`handleChainStart`'s sub-agent lookup), exactly one live candidate for
|
|
118
|
+
* the ns is an unambiguous pair (re-parent as before); zero or more than
|
|
119
|
+
* one live candidates means we cannot tell which tool call the sub-agent
|
|
120
|
+
* actually belongs to, so we deliberately do NOT re-parent — the
|
|
121
|
+
* sub-agent falls back to the normal `resolveParent` chain (renders as a
|
|
122
|
+
* graph sibling: degraded, but never wrong). Cross-wiring two unrelated
|
|
123
|
+
* spans is worse than under-wiring one. Entries are evicted (their
|
|
124
|
+
* specific span removed, not the whole ns bucket) in
|
|
125
|
+
* `handleToolEnd`/`handleToolError` so the map never grows unboundedly and
|
|
126
|
+
* so a tool call that finishes frees its slot for ambiguity resolution.
|
|
127
|
+
* Parity: python `_tool_spans_by_ns` / `_checkpoint_ns` (python's
|
|
128
|
+
* `langgraph` >= 1.2.0 does not need this same guard, since its ns values
|
|
129
|
+
* never collide — see above).
|
|
130
|
+
*/
|
|
131
|
+
checkpointNsToolSpans = new Map();
|
|
76
132
|
constructor(sdk, tracer, logger) {
|
|
77
133
|
this.sdk = sdk;
|
|
78
134
|
this.tracer = tracer;
|
|
@@ -85,7 +141,7 @@ class StructCallbackHandler {
|
|
|
85
141
|
// LangGraph nodes): don't create a span, but record the runId so
|
|
86
142
|
// downstream children with parentRunId pointing here can still find
|
|
87
143
|
// their effective parent span.
|
|
88
|
-
if (!isAgentChain(chain, runType, runName)) {
|
|
144
|
+
if (!isAgentChain(chain, runType, runName, metadata)) {
|
|
89
145
|
// Skipped chains inherit session from parent (so chat/tool spans
|
|
90
146
|
// created under them stay tied to the enclosing agent's session).
|
|
91
147
|
const sessionId = this.resolveSessionId(parentRunId, metadata);
|
|
@@ -95,49 +151,171 @@ class StructCallbackHandler {
|
|
|
95
151
|
effectiveParentSpan: parentSpan,
|
|
96
152
|
sessionId,
|
|
97
153
|
nearestAgentSessionId: this.inheritedAgentSessionId(parentRunId),
|
|
154
|
+
nearestAgentSpan: this.inheritedAgentSpan(parentRunId),
|
|
155
|
+
...this.resolveQueueOwnership(parentRunId),
|
|
98
156
|
kind: "skipped-chain",
|
|
99
157
|
});
|
|
100
158
|
return;
|
|
101
159
|
}
|
|
160
|
+
// Manual struct.agent() wins over the framework's chain — twin
|
|
161
|
+
// suppression (ownership: manual > framework > provider).
|
|
162
|
+
//
|
|
163
|
+
// Generalized check (0.3.14; was: only a TOP-LEVEL chain, gated on
|
|
164
|
+
// `if (!parentRunId)`): suppress THIS agent chain as a
|
|
165
|
+
// "suppressed-twin" whenever a manual struct.agent() scope is live
|
|
166
|
+
// (`getManualAgentSpan()`) AND the run's EFFECTIVE parent span —
|
|
167
|
+
// resolved exactly like every other span's parent, via
|
|
168
|
+
// `resolveParent`, which already walks THROUGH intervening
|
|
169
|
+
// skipped-chains (RunnableSequence, ChannelWrite, Branch, a
|
|
170
|
+
// `prompt | graph` composition, ...) — resolves to that manual span
|
|
171
|
+
// itself. That condition means this run is the FIRST real agent chain
|
|
172
|
+
// reachable from the manual root through ONLY skipped chains, with no
|
|
173
|
+
// real framework `invoke_agent` in between.
|
|
174
|
+
//
|
|
175
|
+
// This SUBSUMES the old top-level-only case: a genuine top-level
|
|
176
|
+
// chain's effective parent is `resolveParent`'s no-known-parent
|
|
177
|
+
// fallback, `getAgentSpan()` — which core.ts's `agent()` seeds to the
|
|
178
|
+
// SAME span object as `manualAgentSpan`, so it equals `manual`
|
|
179
|
+
// whenever a manual scope is live. It ALSO now catches what the old
|
|
180
|
+
// `!parentRunId` gate missed: `sdk.agent(() => sequence.invoke(...))`
|
|
181
|
+
// wrapping `RunnableSequence -> CompiledStateGraph` (or any skipped
|
|
182
|
+
// chain wrapping the real graph). The sequence is registered as a
|
|
183
|
+
// `skipped-chain` whose `effectiveParentSpan` already resolves to the
|
|
184
|
+
// manual span; the graph's chain-start DOES carry a `parentRunId` (the
|
|
185
|
+
// sequence's) — which used to bypass this check entirely and fall
|
|
186
|
+
// through to a REAL `invoke_agent`, emitting a duplicate — but now
|
|
187
|
+
// resolves ITS OWN effective parent (via the same `resolveParent` every
|
|
188
|
+
// other span uses) to the manual span too, and gets suppressed exactly
|
|
189
|
+
// like a top-level twin would.
|
|
190
|
+
//
|
|
191
|
+
// A genuine nested sub-agent (agent -> tool -> sub-agent, or a
|
|
192
|
+
// sub-agent spawned under a REAL framework `invoke_agent`) instead
|
|
193
|
+
// resolves its effective parent to that real tool/agent SPAN — never
|
|
194
|
+
// the manual span — so `parentSpan !== manual` and it is correctly NOT
|
|
195
|
+
// suppressed, emitting its own `invoke_agent` as before.
|
|
196
|
+
//
|
|
197
|
+
// Record the run pointing at the manual span so descendants parent
|
|
198
|
+
// under it — but emit NO twin invoke_agent span, and NEVER end the
|
|
199
|
+
// manual span from chain callbacks (span: undefined guards that via
|
|
200
|
+
// the `if (!r || !r.span) return;` early-returns in
|
|
201
|
+
// handleChainEnd/handleChainError).
|
|
202
|
+
//
|
|
203
|
+
// Python parity note: `struct-sdk-python`'s `on_chain_start`
|
|
204
|
+
// (langchain.py:681-698) gates the identical suppression on
|
|
205
|
+
// `parent_key is None` — i.e. Python has this SAME top-level-only
|
|
206
|
+
// limitation today; it is not something this TS fix introduces or
|
|
207
|
+
// widens. This fix intentionally puts TS ahead of Python on the
|
|
208
|
+
// RunnableSequence/skipped-chain-wrapped-graph scenario until a
|
|
209
|
+
// matching Python fix lands — filed as a follow-up rather than silently
|
|
210
|
+
// diverging.
|
|
211
|
+
const manual = (0, context_js_1.getManualAgentSpan)();
|
|
212
|
+
const { parentSpan, parentSpanIsAgent: parentSpanIsAgentChain } = this.resolveParent(parentRunId);
|
|
213
|
+
void parentSpanIsAgentChain;
|
|
214
|
+
const parentAgentSessionId = this.inheritedAgentSessionId(parentRunId);
|
|
215
|
+
if (manual && parentSpan === manual) {
|
|
216
|
+
// Self-audit round 5 (FIX C): `getSessionId()` alone is the AMBIENT
|
|
217
|
+
// session — `undefined` for a SESSION-LESS manual `struct.agent()`
|
|
218
|
+
// (no explicit/enclosing sessionId; core.ts never fabricates one for
|
|
219
|
+
// its OWN span). Python still gives the suppressed-twin's
|
|
220
|
+
// descendants a COHERENT conversation.id in that case: it resolves
|
|
221
|
+
// `session_id` via `_resolve_agent_session_id` (metadata thread_id,
|
|
222
|
+
// then ambient session, then a fresh UUID) BEFORE checking manual
|
|
223
|
+
// ownership, then falls back to it — `_current_session_id.get(None)
|
|
224
|
+
// or session_id` (langchain.py:685-696) — rather than leaving the
|
|
225
|
+
// suppressed-twin subtree session-less just because the manual scope
|
|
226
|
+
// itself chose not to fabricate one. Mirror that fallback exactly:
|
|
227
|
+
// ambient session first, else the same resolution a real invoke_agent
|
|
228
|
+
// for THIS run would use — `resolveAgentSessionId(metadata,
|
|
229
|
+
// parentAgentSessionId)`, identical to the real-agent path below, so
|
|
230
|
+
// a suppressed twin reached through skipped chains resolves exactly
|
|
231
|
+
// like the real invoke_agent it stands in for would (for a genuinely
|
|
232
|
+
// top-level twin, parentAgentSessionId is undefined, same as before).
|
|
233
|
+
const session = (0, context_js_1.getSessionId)() ?? this.resolveAgentSessionId(metadata, parentAgentSessionId);
|
|
234
|
+
this.runs.set(runId, {
|
|
235
|
+
span: undefined,
|
|
236
|
+
effectiveParentSpan: manual,
|
|
237
|
+
sessionId: session,
|
|
238
|
+
nearestAgentSessionId: session,
|
|
239
|
+
nearestAgentSpan: manual,
|
|
240
|
+
// Queue ownership generalizes the same way as everything else here:
|
|
241
|
+
// `resolveQueueOwnership` already inherits verbatim from a
|
|
242
|
+
// registered parent (the skipped chain(s) in between) when the live
|
|
243
|
+
// manual span matches what the parent captured, and only
|
|
244
|
+
// re-captures `getPendingToolCalls()` fresh when there's no
|
|
245
|
+
// resolvable parent (the genuinely-top-level case, matching the old
|
|
246
|
+
// hardcoded direct-capture behavior) or a NEW nested manual scope
|
|
247
|
+
// began (FIX F). No behavior change for the top-level twin; correct
|
|
248
|
+
// inheritance for the new skipped-chain-reached twin.
|
|
249
|
+
...this.resolveQueueOwnership(parentRunId),
|
|
250
|
+
kind: "suppressed-twin",
|
|
251
|
+
});
|
|
252
|
+
return;
|
|
253
|
+
}
|
|
102
254
|
const agentName = runName ??
|
|
103
255
|
extractClassName(chain) ??
|
|
104
256
|
(typeof inputs === "object" && inputs
|
|
105
257
|
? inputs.name
|
|
106
258
|
: undefined) ??
|
|
107
259
|
"agent";
|
|
108
|
-
// Agent-start:
|
|
109
|
-
//
|
|
110
|
-
//
|
|
111
|
-
//
|
|
260
|
+
// Agent-start: every agent invocation shares ONE gen_ai.conversation.id
|
|
261
|
+
// with the run it belongs to — subagents INHERIT the parent agent's
|
|
262
|
+
// session rather than minting their own (OTel: conversation.id models
|
|
263
|
+
// the thread, not the agent). Only when there's no parent session at
|
|
264
|
+
// all do we fall back to metadata.thread_id, the ambient session, or
|
|
265
|
+
// (as a last resort, so agent spans always have a coherent id) a fresh
|
|
266
|
+
// UUID.
|
|
112
267
|
//
|
|
113
268
|
// Convention (documented publicly):
|
|
114
269
|
// * thread_id is a LangGraph checkpoint identifier. Multiple turns
|
|
115
270
|
// of one conversation reuse a thread_id → we group them into one
|
|
116
271
|
// session.
|
|
117
|
-
// * Subagents
|
|
118
|
-
//
|
|
119
|
-
//
|
|
272
|
+
// * Subagents don't need a distinct thread_id to appear distinctly —
|
|
273
|
+
// they surface via ``struct.agent.parent_session_id`` linkage
|
|
274
|
+
// ("Spawned by" navigation) while staying in the same session.
|
|
120
275
|
//
|
|
121
|
-
//
|
|
122
|
-
//
|
|
123
|
-
//
|
|
124
|
-
//
|
|
125
|
-
// inheritance as "no thread_id" and assign a fresh UUID.
|
|
276
|
+
// A subagent MAY still supply its own thread_id (e.g. a distinct
|
|
277
|
+
// LangGraph checkpoint key). We preserve that as ``struct.agent.thread_id``
|
|
278
|
+
// below for observability, but it does not override the inherited
|
|
279
|
+
// conversation.id.
|
|
126
280
|
//
|
|
127
281
|
// For the struct.agent.parent_session_id attribute, we need the
|
|
128
282
|
// NEAREST agent ancestor's session — not just the immediate parent
|
|
129
283
|
// run, which might be a tool span. That's what
|
|
130
|
-
// ``inheritedAgentSessionId`` walks.
|
|
131
|
-
|
|
284
|
+
// ``inheritedAgentSessionId`` walks. Both `parentAgentSessionId` and
|
|
285
|
+
// `parentSpan` were already resolved above (twin-suppression check),
|
|
286
|
+
// and are reused verbatim here — the run tree hasn't changed between
|
|
287
|
+
// the two reads within this single synchronous callback.
|
|
132
288
|
const sessionId = this.resolveAgentSessionId(metadata, parentAgentSessionId);
|
|
133
289
|
if (process.env.STRUCT_SDK_DEBUG === "1") {
|
|
134
290
|
// eslint-disable-next-line no-console
|
|
135
291
|
console.error("[struct-sdk] agent-start", agentName, "metadata.thread_id=", metadata?.thread_id, "parent.agent.session=", parentAgentSessionId, "→ resolved session=", sessionId);
|
|
136
292
|
}
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
293
|
+
// LangChain agent-as-tool: a sub-agent graph runs as a SIBLING of its
|
|
294
|
+
// triggering execute_tool (parentRunId points at the parent graph, not
|
|
295
|
+
// the tool), so normal resolution would emit this invoke_agent as a
|
|
296
|
+
// sibling. LangGraph stamps the tool branch and its sub-agent with the
|
|
297
|
+
// same `langgraph_checkpoint_ns`, so on versions where that ns really is
|
|
298
|
+
// unique per tool call we re-parent under the matching live execute_tool
|
|
299
|
+
// span to nest natively (the UI's direct-tool-child path).
|
|
300
|
+
//
|
|
301
|
+
// But on every `@langchain/langgraph` release checked (0.2.x through the
|
|
302
|
+
// latest published, 1.4.7 — see the doc comment on
|
|
303
|
+
// `checkpointNsToolSpans`) the ns is NOT unique across parallel
|
|
304
|
+
// same-named tool calls in one step — so we only ever act on an
|
|
305
|
+
// UNAMBIGUOUS match: exactly one live tool span currently registered
|
|
306
|
+
// under this ns. Zero candidates (no tool ever registered this ns, or it
|
|
307
|
+
// already ended) or two-or-more candidates (a collision — we cannot tell
|
|
308
|
+
// which tool call this sub-agent belongs to) both fall back to the
|
|
309
|
+
// normal resolved parent instead of guessing. Cross-wiring two unrelated
|
|
310
|
+
// spans is a worse outcome than under-wiring one to a sibling position.
|
|
311
|
+
// Parity: python `delegating_tool_span` in `on_chain_start` (python's
|
|
312
|
+
// `langgraph` does not need this ambiguity guard — see above).
|
|
313
|
+
const ns = checkpointNs(metadata);
|
|
314
|
+
const nsCandidates = ns ? this.checkpointNsToolSpans.get(ns) : undefined;
|
|
315
|
+
const nsToolSpan = nsCandidates?.length === 1 ? nsCandidates[0] : undefined;
|
|
316
|
+
const effectiveParent = nsToolSpan ?? parentSpan;
|
|
317
|
+
const parentCtx = effectiveParent
|
|
318
|
+
? api_1.trace.setSpan(api_1.context.active(), effectiveParent)
|
|
141
319
|
: api_1.context.active();
|
|
142
320
|
// Span creation can fail (custom tracer / broken context). Skip
|
|
143
321
|
// run-state insertion when it does — end/error callbacks already
|
|
@@ -159,7 +337,16 @@ class StructCallbackHandler {
|
|
|
159
337
|
// session. LangChain doesn't surface a stable agent-id to us, so we
|
|
160
338
|
// leave the attribute unset. Callers who use struct.agent() directly
|
|
161
339
|
// can pass their own agentId via the scope options.
|
|
162
|
-
|
|
340
|
+
if (sessionId)
|
|
341
|
+
startedSpan.setAttribute(semconv_js_1.GEN_AI.CONVERSATION_ID, sessionId);
|
|
342
|
+
// A subagent may supply its own local thread_id distinct from the
|
|
343
|
+
// inherited conversation.id (e.g. its own LangGraph checkpoint key).
|
|
344
|
+
// Preserve it for observability WITHOUT letting it split the
|
|
345
|
+
// session — struct.agent.thread_id is non-grouping metadata.
|
|
346
|
+
const localThread = metadataThreadId(metadata);
|
|
347
|
+
if (localThread && localThread !== sessionId) {
|
|
348
|
+
startedSpan.setAttribute(semconv_js_1.STRUCT.AGENT_THREAD_ID, localThread);
|
|
349
|
+
}
|
|
163
350
|
// Link subagents back to the parent agent's session. Use the nearest
|
|
164
351
|
// agent ancestor (NOT the immediate parent run) — a subagent spawned
|
|
165
352
|
// from inside a tool has the tool as its immediate parent, but we want
|
|
@@ -192,6 +379,12 @@ class StructCallbackHandler {
|
|
|
192
379
|
sessionId,
|
|
193
380
|
// This agent IS itself the nearest agent ancestor for everything nested inside.
|
|
194
381
|
nearestAgentSessionId: sessionId,
|
|
382
|
+
nearestAgentSpan: startedSpan,
|
|
383
|
+
// Unlike nearestAgentSessionId/nearestAgentSpan, this INHERITS rather
|
|
384
|
+
// than owns — a nested subagent (bare mode) shares its ancestor's
|
|
385
|
+
// queue so a tool call fired from deep inside a subagent's own
|
|
386
|
+
// tool-call cycle still resolves against the same run-tree queue.
|
|
387
|
+
...this.resolveQueueOwnership(parentRunId),
|
|
195
388
|
kind: "chain",
|
|
196
389
|
});
|
|
197
390
|
};
|
|
@@ -242,7 +435,8 @@ class StructCallbackHandler {
|
|
|
242
435
|
startedSpan.setAttribute(semconv_js_1.GEN_AI.OPERATION_NAME, "chat");
|
|
243
436
|
startedSpan.setAttribute(semconv_js_1.GEN_AI.PROVIDER_NAME, provider);
|
|
244
437
|
startedSpan.setAttribute(semconv_js_1.GEN_AI.REQUEST_MODEL, String(model));
|
|
245
|
-
|
|
438
|
+
if (sessionId)
|
|
439
|
+
startedSpan.setAttribute(semconv_js_1.GEN_AI.CONVERSATION_ID, sessionId);
|
|
246
440
|
setRequestAttrsFromInvocation(startedSpan, extraParams?.invocation_params);
|
|
247
441
|
const flat = messages.flat();
|
|
248
442
|
if (flat.length > 0) {
|
|
@@ -253,8 +447,13 @@ class StructCallbackHandler {
|
|
|
253
447
|
if (this.sdk.emitSpanContent) {
|
|
254
448
|
startedSpan.setAttribute(semconv_js_1.GEN_AI.INPUT_MESSAGES, (0, langchain_content_js_1.langchainToInputMessages)(flat));
|
|
255
449
|
}
|
|
256
|
-
// Propagate the most recent user message onto the nearest
|
|
257
|
-
|
|
450
|
+
// Propagate the most recent user message onto the nearest AGENT
|
|
451
|
+
// span — never a tool/chat span. `resolveParent(...).parentSpan`
|
|
452
|
+
// is the immediate parent (which may be a tool span when the chat
|
|
453
|
+
// fires from inside a tool body), so we must use the cached
|
|
454
|
+
// nearest-agent-ancestor lookup instead. Parity: python
|
|
455
|
+
// `_find_agent_ancestor` used at `on_chat_model_start` (:905-907).
|
|
456
|
+
const ancestorAgentSpan = this.inheritedAgentSpan(parentRunId);
|
|
258
457
|
if (ancestorAgentSpan)
|
|
259
458
|
this.propagateUserPrompt(ancestorAgentSpan, flat);
|
|
260
459
|
}
|
|
@@ -264,6 +463,13 @@ class StructCallbackHandler {
|
|
|
264
463
|
effectiveParentSpan: startedSpan,
|
|
265
464
|
sessionId,
|
|
266
465
|
nearestAgentSessionId: this.inheritedAgentSessionId(parentRunId),
|
|
466
|
+
nearestAgentSpan: this.inheritedAgentSpan(parentRunId),
|
|
467
|
+
// No `parentRunId` here means a graph-less bare chat flow (e.g. a
|
|
468
|
+
// bound-tools chat model invoked directly, no AgentExecutor/graph
|
|
469
|
+
// wrapping it) — this run IS the top level, so it gets a fresh queue
|
|
470
|
+
// when bare, exactly like handleChainStart's top-level case.
|
|
471
|
+
...this.resolveQueueOwnership(parentRunId),
|
|
472
|
+
forcedToolName: extractForcedToolName(extraParams?.invocation_params),
|
|
267
473
|
kind: "llm",
|
|
268
474
|
});
|
|
269
475
|
};
|
|
@@ -298,17 +504,42 @@ class StructCallbackHandler {
|
|
|
298
504
|
if (!Array.isArray(toolCalls) || toolCalls.length === 0)
|
|
299
505
|
return;
|
|
300
506
|
const pairs = [];
|
|
507
|
+
let forcedToolConsumed = false;
|
|
301
508
|
for (const tc of toolCalls) {
|
|
302
509
|
if (tc && typeof tc === "object") {
|
|
303
510
|
const n = tc.name;
|
|
304
511
|
const id = tc.id;
|
|
305
512
|
if (typeof n === "string" && typeof id === "string" && n && id) {
|
|
513
|
+
// A forced tool_choice (structured output extraction) is not a
|
|
514
|
+
// real tool call the agent should autofill via @struct.tool() —
|
|
515
|
+
// exclude it from the pending queue. Parity: python forced-tool
|
|
516
|
+
// exclusion in `on_llm_end` (langchain.py:998-1027).
|
|
517
|
+
if (n === r.forcedToolName) {
|
|
518
|
+
forcedToolConsumed = true;
|
|
519
|
+
continue;
|
|
520
|
+
}
|
|
306
521
|
pairs.push([n, id]);
|
|
307
522
|
}
|
|
308
523
|
}
|
|
309
524
|
}
|
|
310
|
-
if (pairs.length > 0)
|
|
311
|
-
|
|
525
|
+
if (pairs.length > 0 && r.toolCallQueue) {
|
|
526
|
+
// Push directly onto the RunState-PINNED queue OBJECT (decided
|
|
527
|
+
// once at the run-tree root and inherited by reference — see
|
|
528
|
+
// RunState's `toolCallQueue` doc comment), NOT via the
|
|
529
|
+
// ALS-reading `pushPendingToolCalls` helper: this run's callback
|
|
530
|
+
// may fire in a lost/foreign ALS frame, and reading/writing the
|
|
531
|
+
// pinned object directly sidesteps that entirely (in manual mode
|
|
532
|
+
// this object IS the same one context.ts's ALS-scoped helpers
|
|
533
|
+
// read, since it was captured via `getPendingToolCalls()` at the
|
|
534
|
+
// run-tree root — see `resolveQueueOwnership`).
|
|
535
|
+
const q = r.toolCallQueue;
|
|
536
|
+
for (const [name, id] of pairs) {
|
|
537
|
+
(q[name] ??= []).push(id);
|
|
538
|
+
}
|
|
539
|
+
}
|
|
540
|
+
if (forcedToolConsumed) {
|
|
541
|
+
span.setAttribute(semconv_js_1.GEN_AI.OUTPUT_TYPE, "json");
|
|
542
|
+
}
|
|
312
543
|
}, "langchain.handleLLMEnd.record_pending_tool_calls", this.internalLogger);
|
|
313
544
|
(0, core_js_1.safe)(() => span.setStatus({ code: api_1.SpanStatusCode.OK }), "langchain.handleLLMEnd.set_status", this.internalLogger);
|
|
314
545
|
(0, core_js_1.safe)(() => span.end(), "langchain.handleLLMEnd.span_end", this.internalLogger);
|
|
@@ -340,33 +571,85 @@ class StructCallbackHandler {
|
|
|
340
571
|
return;
|
|
341
572
|
const startedSpan = span;
|
|
342
573
|
const sessionId = this.resolveSessionId(parentRunId, metadata);
|
|
574
|
+
const queueOwnership = this.resolveQueueOwnership(parentRunId);
|
|
343
575
|
(0, core_js_1.safe)(() => {
|
|
344
576
|
startedSpan.setAttribute(semconv_js_1.GEN_AI.OPERATION_NAME, "execute_tool");
|
|
345
577
|
startedSpan.setAttribute(semconv_js_1.GEN_AI.PROVIDER_NAME, "langchain");
|
|
346
578
|
startedSpan.setAttribute(semconv_js_1.GEN_AI.TOOL_NAME, String(toolName));
|
|
347
|
-
// Tool call id from metadata (LangChain passes it there for ToolCall
|
|
348
|
-
// or fallback to the pending queue populated by the LLM's
|
|
579
|
+
// Tool call id from metadata (LangChain passes it there for ToolCall
|
|
580
|
+
// inputs) or fallback to the pending queue populated by the LLM's
|
|
581
|
+
// tool_calls — popped directly off THIS run's PINNED queue OBJECT
|
|
582
|
+
// (`queueOwnership.toolCallQueue`, inherited from the run-tree root —
|
|
583
|
+
// see RunState's `toolCallQueue` doc comment), NOT via the
|
|
584
|
+
// ALS-reading `popPendingToolCallId` helper: this callback may fire
|
|
585
|
+
// in a lost/foreign ALS frame, and popping from the pinned object
|
|
586
|
+
// directly sidesteps that entirely (in manual mode this object IS
|
|
587
|
+
// the same one context.ts's ALS-scoped helpers read/write, since it
|
|
588
|
+
// was captured via `getPendingToolCalls()` at the run-tree root).
|
|
349
589
|
const callId = metadata?.tool_call_id ??
|
|
350
590
|
extractToolCallIdFromInput(input) ??
|
|
351
|
-
(
|
|
591
|
+
popFromQueue(queueOwnership.toolCallQueue, String(toolName));
|
|
352
592
|
if (callId)
|
|
353
593
|
startedSpan.setAttribute(semconv_js_1.GEN_AI.TOOL_CALL_ID, callId);
|
|
354
|
-
|
|
594
|
+
if (sessionId)
|
|
595
|
+
startedSpan.setAttribute(semconv_js_1.GEN_AI.CONVERSATION_ID, sessionId);
|
|
355
596
|
if (this.sdk.captureContent && input !== undefined) {
|
|
356
597
|
startedSpan.setAttribute(semconv_js_1.GEN_AI.TOOL_CALL_ARGUMENTS, (0, truncation_js_1.safeJsonStringify)(input).slice(0, 8192));
|
|
357
598
|
}
|
|
358
599
|
}, "langchain.handleToolStart.set_attrs", this.internalLogger);
|
|
600
|
+
const tool_ns = checkpointNs(metadata);
|
|
359
601
|
this.runs.set(runId, {
|
|
360
602
|
span: startedSpan,
|
|
361
603
|
effectiveParentSpan: startedSpan,
|
|
362
604
|
sessionId,
|
|
363
605
|
nearestAgentSessionId: this.inheritedAgentSessionId(parentRunId),
|
|
606
|
+
nearestAgentSpan: this.inheritedAgentSpan(parentRunId),
|
|
607
|
+
// Carried forward (not re-resolved) so a bare sub-agent spawned from
|
|
608
|
+
// inside this tool call inherits the SAME run-tree queue and pinned
|
|
609
|
+
// ownership.
|
|
610
|
+
...queueOwnership,
|
|
611
|
+
checkpointNs: tool_ns,
|
|
364
612
|
kind: "tool",
|
|
365
613
|
});
|
|
614
|
+
// Index by checkpoint ns so a sub-agent graph triggered inside this tool
|
|
615
|
+
// (sharing this exact ns) can re-parent its invoke_agent span under us —
|
|
616
|
+
// PUSH onto the ns's candidate list rather than overwrite it, since
|
|
617
|
+
// `@langchain/langgraph@0.2.x` can hand two parallel same-named tool
|
|
618
|
+
// calls the identical ns (see `checkpointNsToolSpans`'s doc comment).
|
|
619
|
+
// Consume-side (`handleChainStart`) only acts when exactly one candidate
|
|
620
|
+
// is live for the ns; a 2+ list here is exactly the collision case it's
|
|
621
|
+
// built to detect.
|
|
622
|
+
if (tool_ns !== undefined) {
|
|
623
|
+
const bucket = this.checkpointNsToolSpans.get(tool_ns);
|
|
624
|
+
if (bucket)
|
|
625
|
+
bucket.push(startedSpan);
|
|
626
|
+
else
|
|
627
|
+
this.checkpointNsToolSpans.set(tool_ns, [startedSpan]);
|
|
628
|
+
}
|
|
366
629
|
};
|
|
630
|
+
/**
|
|
631
|
+
* Remove one specific span from its checkpoint-ns bucket (not the whole
|
|
632
|
+
* bucket) — a sibling parallel tool call under the same colliding ns may
|
|
633
|
+
* still be live and must keep its own candidate-list membership. Deletes
|
|
634
|
+
* the ns key entirely once its bucket is empty so the map never grows
|
|
635
|
+
* unboundedly.
|
|
636
|
+
*/
|
|
637
|
+
evictCheckpointNsSpan(ns, span) {
|
|
638
|
+
if (ns === undefined || span === undefined)
|
|
639
|
+
return;
|
|
640
|
+
const bucket = this.checkpointNsToolSpans.get(ns);
|
|
641
|
+
if (!bucket)
|
|
642
|
+
return;
|
|
643
|
+
const idx = bucket.indexOf(span);
|
|
644
|
+
if (idx !== -1)
|
|
645
|
+
bucket.splice(idx, 1);
|
|
646
|
+
if (bucket.length === 0)
|
|
647
|
+
this.checkpointNsToolSpans.delete(ns);
|
|
648
|
+
}
|
|
367
649
|
handleToolEnd = (output, runId) => {
|
|
368
650
|
const r = this.runs.get(runId);
|
|
369
651
|
this.runs.delete(runId);
|
|
652
|
+
this.evictCheckpointNsSpan(r?.checkpointNs, r?.span);
|
|
370
653
|
if (!r || !r.span)
|
|
371
654
|
return;
|
|
372
655
|
const span = r.span;
|
|
@@ -381,6 +664,7 @@ class StructCallbackHandler {
|
|
|
381
664
|
handleToolError = (err, runId) => {
|
|
382
665
|
const r = this.runs.get(runId);
|
|
383
666
|
this.runs.delete(runId);
|
|
667
|
+
this.evictCheckpointNsSpan(r?.checkpointNs, r?.span);
|
|
384
668
|
if (!r || !r.span)
|
|
385
669
|
return;
|
|
386
670
|
const span = r.span;
|
|
@@ -409,7 +693,8 @@ class StructCallbackHandler {
|
|
|
409
693
|
startedSpan.setAttribute(semconv_js_1.GEN_AI.OPERATION_NAME, "retrieval");
|
|
410
694
|
startedSpan.setAttribute(semconv_js_1.GEN_AI.PROVIDER_NAME, "langchain");
|
|
411
695
|
startedSpan.setAttribute(semconv_js_1.GEN_AI.DATA_SOURCE_ID, String(name));
|
|
412
|
-
|
|
696
|
+
if (sessionId)
|
|
697
|
+
startedSpan.setAttribute(semconv_js_1.GEN_AI.CONVERSATION_ID, sessionId);
|
|
413
698
|
if (this.sdk.captureContent && typeof query === "string") {
|
|
414
699
|
startedSpan.setAttribute(semconv_js_1.GEN_AI.RETRIEVAL_QUERY_TEXT, query.slice(0, 4096));
|
|
415
700
|
}
|
|
@@ -419,6 +704,8 @@ class StructCallbackHandler {
|
|
|
419
704
|
effectiveParentSpan: startedSpan,
|
|
420
705
|
sessionId,
|
|
421
706
|
nearestAgentSessionId: this.inheritedAgentSessionId(parentRunId),
|
|
707
|
+
nearestAgentSpan: this.inheritedAgentSpan(parentRunId),
|
|
708
|
+
...this.resolveQueueOwnership(parentRunId),
|
|
422
709
|
kind: "retriever",
|
|
423
710
|
});
|
|
424
711
|
};
|
|
@@ -454,7 +741,7 @@ class StructCallbackHandler {
|
|
|
454
741
|
// filtered chains (RunnableSequence, ChannelWrite, ...).
|
|
455
742
|
return {
|
|
456
743
|
parentSpan: p.effectiveParentSpan,
|
|
457
|
-
parentSpanIsAgent: p.kind === "chain",
|
|
744
|
+
parentSpanIsAgent: p.kind === "chain" || p.kind === "suppressed-twin",
|
|
458
745
|
};
|
|
459
746
|
}
|
|
460
747
|
}
|
|
@@ -462,7 +749,11 @@ class StructCallbackHandler {
|
|
|
462
749
|
}
|
|
463
750
|
/**
|
|
464
751
|
* Conversation-id resolution for chat/tool/retriever spans — INHERIT from
|
|
465
|
-
* the parent
|
|
752
|
+
* the parent run so everything rolls up under one gen_ai.conversation.id.
|
|
753
|
+
* Returns `undefined` (never fabricates a UUID) when no ancestor run,
|
|
754
|
+
* metadata thread_id, or ambient session supplies one — an orphan span
|
|
755
|
+
* with no conversation.id is preferable to a fake one that can never be
|
|
756
|
+
* joined to anything else (parity: python `_resolve_session_id`).
|
|
466
757
|
*/
|
|
467
758
|
resolveSessionId(parentRunId, metadata) {
|
|
468
759
|
if (parentRunId) {
|
|
@@ -473,10 +764,7 @@ class StructCallbackHandler {
|
|
|
473
764
|
const threadId = metadataThreadId(metadata);
|
|
474
765
|
if (threadId)
|
|
475
766
|
return threadId;
|
|
476
|
-
|
|
477
|
-
if (ambient)
|
|
478
|
-
return ambient;
|
|
479
|
-
return (0, node_crypto_1.randomUUID)();
|
|
767
|
+
return (0, context_js_1.getSessionId)();
|
|
480
768
|
}
|
|
481
769
|
/**
|
|
482
770
|
* Walk the run tree to find the nearest ``invoke_agent`` ancestor's
|
|
@@ -496,29 +784,129 @@ class StructCallbackHandler {
|
|
|
496
784
|
return this.runs.get(parentRunId)?.nearestAgentSessionId;
|
|
497
785
|
}
|
|
498
786
|
/**
|
|
499
|
-
*
|
|
500
|
-
*
|
|
501
|
-
*
|
|
502
|
-
*
|
|
787
|
+
* Nearest ``invoke_agent`` ancestor SPAN — O(1) lookup, mirrors
|
|
788
|
+
* ``inheritedAgentSessionId`` for the span pointer itself. Cached at every
|
|
789
|
+
* RunState creation so descendants (and prompt propagation) can reach the
|
|
790
|
+
* ancestor without walking a parent chain we don't retain.
|
|
791
|
+
*
|
|
792
|
+
* Unlike ``inheritedAgentSessionId``, a missing ``parentRunId`` falls back
|
|
793
|
+
* to the ambient `getAgentSpan()` (matching `resolveParent`'s top-level
|
|
794
|
+
* fallback) rather than `undefined` — a run with no LangChain parent may
|
|
795
|
+
* still be nested inside a manually-created `struct.agent()` span.
|
|
796
|
+
* Parity: python `_inherited_agent_span`.
|
|
797
|
+
*/
|
|
798
|
+
inheritedAgentSpan(parentRunId) {
|
|
799
|
+
if (!parentRunId)
|
|
800
|
+
return (0, context_js_1.getAgentSpan)();
|
|
801
|
+
return this.runs.get(parentRunId)?.nearestAgentSpan ?? (0, context_js_1.getAgentSpan)();
|
|
802
|
+
}
|
|
803
|
+
/**
|
|
804
|
+
* Resolve the pending-tool-call queue OBJECT a run should push/pop
|
|
805
|
+
* against for `gen_ai.tool.call.id` autofill, per `RunState.toolCallQueue`'s
|
|
806
|
+
* doc comment.
|
|
807
|
+
*
|
|
808
|
+
* PINNED AT THE RUN-TREE ROOT, INHERITED DOWN AS THE SAME OBJECT
|
|
809
|
+
* REFERENCE — this is the crux of the design, and it is the OBJECT that
|
|
810
|
+
* must be pinned, not merely a manual-vs-bare boolean. Whenever a parent
|
|
811
|
+
* run is known (`parentRunId` resolves to a live `RunState`), we copy its
|
|
812
|
+
* `toolCallQueue` reference VERBATIM rather than re-deriving anything from
|
|
813
|
+
* the live ALS frame. Only a run with no resolvable parent (a genuine
|
|
814
|
+
* root, or an orphaned `parentRunId` whose `RunState` was never
|
|
815
|
+
* registered) consults the live `getManualAgentSpan()` signal — and even
|
|
816
|
+
* then, only ONCE, at that run's creation.
|
|
817
|
+
*
|
|
818
|
+
* Why the OBJECT, not just a boolean: a descendant run's OWN callback can
|
|
819
|
+
* fire in a lost or foreign ALS frame — the LangChain FRAMEWORK layer
|
|
820
|
+
* (`langchain.ts`'s `BaseChatModel.generate`/`.stream` patch) opens its
|
|
821
|
+
* own short-lived ALS scope (`runWithContext({ suppressGenAi: true },
|
|
822
|
+
* fn)`) around each chat completion, entirely independent of any customer
|
|
823
|
+
* `struct.agent()` call; more generally, LangGraph's internal scheduling
|
|
824
|
+
* can run a node's callback outside the async context that originally
|
|
825
|
+
* held the manual scope. A design that pins only a manual-vs-bare
|
|
826
|
+
* ownership BOOLEAN but still routes the actual push (`handleLLMEnd`) and
|
|
827
|
+
* pop (`handleToolStart`) through the ALS-reading
|
|
828
|
+
* `pushPendingToolCalls`/`popPendingToolCallId` helpers (context.ts) is
|
|
829
|
+
* still broken: those helpers read whatever `pendingToolCalls` object is
|
|
830
|
+
* on the CURRENT frame, so a push in one lost/foreign frame and a pop in a
|
|
831
|
+
* DIFFERENT lost/foreign frame silently disagree, even though both frames
|
|
832
|
+
* correctly resolved the same boolean. Operating directly on the
|
|
833
|
+
* RunState-pinned `toolCallQueue` object sidesteps the ALS frame entirely
|
|
834
|
+
* for both operations: whatever object the root observed is what the
|
|
835
|
+
* whole run tree pushes into and pops from, regardless of ALS frame state
|
|
836
|
+
* at either callback.
|
|
503
837
|
*
|
|
504
|
-
*
|
|
505
|
-
*
|
|
506
|
-
*
|
|
507
|
-
*
|
|
838
|
+
* Three cases:
|
|
839
|
+
* 1. `parentRunId` resolves to a registered parent `RunState` — inherit
|
|
840
|
+
* `toolCallQueue` from it verbatim (decided once at the tree root) —
|
|
841
|
+
* UNLESS a NEW manual scope began mid-tree (see FIX F below).
|
|
842
|
+
* 2. No parent (root, or an orphaned `parentRunId`) AND a MANUAL
|
|
843
|
+
* `struct.agent()` scope is live right now (`getManualAgentSpan()`
|
|
844
|
+
* set) — capture `getPendingToolCalls()`, the SAME object
|
|
845
|
+
* `struct.agent()` seeded onto the ALS store and that manual
|
|
846
|
+
* `struct.tool()` calls pop from via `popPendingToolCallId`
|
|
847
|
+
* (context.ts). Captured IN-FRAME here (this callback necessarily
|
|
848
|
+
* runs inside the live manual scope), so the RunState-pinned
|
|
849
|
+
* reference and the ALS store's `pendingToolCalls` stay identical.
|
|
850
|
+
* 3. No parent AND no manual scope — this run IS the top of a bare run
|
|
851
|
+
* tree; mint a fresh queue `{}`.
|
|
852
|
+
*
|
|
853
|
+
* Self-audit round 5 (FIX F): case 1's "inherit verbatim" is only correct
|
|
854
|
+
* when the parent's queue was captured under the SAME manual ownership
|
|
855
|
+
* that is live right now. A NESTED manual `struct.agent()` — opened from
|
|
856
|
+
* inside a tool body, whose own LangChain graph LangChain threads as a
|
|
857
|
+
* CHILD of the outer run tree, parented on a REAL tool span rather than
|
|
858
|
+
* reaching the manual span through skipped chains (so it never reaches
|
|
859
|
+
* the suppressed-twin branch in `handleChainStart`) — would otherwise
|
|
860
|
+
* silently inherit the OUTER run's
|
|
861
|
+
* queue object, even though the CURRENT ambient `getManualAgentSpan()` is
|
|
862
|
+
* the INNER agent's own (different) span and the inner agent's own
|
|
863
|
+
* `pendingToolCalls` (a fresh object seeded by that inner `struct.agent()`
|
|
864
|
+
* call) is what its descendants actually push/pop against. Detect this by
|
|
865
|
+
* comparing the live `getManualAgentSpan()` against the PARENT run's
|
|
866
|
+
* `queueManualSpan` (the manual span that owned the parent's queue): if
|
|
867
|
+
* they differ and the live one is truthy, a new manual scope has begun —
|
|
868
|
+
* re-capture `getPendingToolCalls()` fresh, exactly like case 2. When they
|
|
869
|
+
* match (including both `undefined`, i.e. bare on both sides — preserves
|
|
870
|
+
* the F2 frame-independence fix, where a merely lost/foreign ALS frame
|
|
871
|
+
* reads as `undefined` and must NOT trigger a spurious re-capture),
|
|
872
|
+
* inherit verbatim as before.
|
|
873
|
+
*/
|
|
874
|
+
resolveQueueOwnership(parentRunId) {
|
|
875
|
+
const manual = (0, context_js_1.getManualAgentSpan)();
|
|
876
|
+
if (parentRunId) {
|
|
877
|
+
const parent = this.runs.get(parentRunId);
|
|
878
|
+
if (parent) {
|
|
879
|
+
if (manual && manual !== parent.queueManualSpan) {
|
|
880
|
+
return { toolCallQueue: (0, context_js_1.getPendingToolCalls)() ?? {}, queueManualSpan: manual };
|
|
881
|
+
}
|
|
882
|
+
return { toolCallQueue: parent.toolCallQueue, queueManualSpan: parent.queueManualSpan };
|
|
883
|
+
}
|
|
884
|
+
}
|
|
885
|
+
return {
|
|
886
|
+
toolCallQueue: manual ? ((0, context_js_1.getPendingToolCalls)() ?? {}) : {},
|
|
887
|
+
queueManualSpan: manual,
|
|
888
|
+
};
|
|
889
|
+
}
|
|
890
|
+
/**
|
|
891
|
+
* Conversation-id resolution for AGENT spans — INHERIT from the parent
|
|
892
|
+
* run first, so a subagent shares its outer agent's gen_ai.conversation.id
|
|
893
|
+
* (one id per run; parity: python `_resolve_agent_session_id`). Only when
|
|
894
|
+
* there's no parent session do we fall back to metadata.thread_id, the
|
|
895
|
+
* ambient session, or — as a last resort, since agent spans should always
|
|
896
|
+
* have a coherent id — a fresh UUID. A subagent's own divergent thread_id
|
|
897
|
+
* (if any) is preserved separately as `struct.agent.thread_id`; see the
|
|
898
|
+
* caller in `handleChainStart`.
|
|
508
899
|
*/
|
|
509
900
|
resolveAgentSessionId(metadata, parentSessionId) {
|
|
901
|
+
if (parentSessionId)
|
|
902
|
+
return parentSessionId; // inherit — one id per run
|
|
510
903
|
const threadId = metadataThreadId(metadata);
|
|
511
|
-
if (threadId)
|
|
512
|
-
if (parentSessionId && threadId === parentSessionId) {
|
|
513
|
-
// Inherited from parent — treat as unset, assign fresh.
|
|
514
|
-
return (0, node_crypto_1.randomUUID)();
|
|
515
|
-
}
|
|
904
|
+
if (threadId)
|
|
516
905
|
return threadId;
|
|
517
|
-
}
|
|
518
906
|
const ambient = (0, context_js_1.getSessionId)();
|
|
519
907
|
if (ambient)
|
|
520
908
|
return ambient;
|
|
521
|
-
return (0, node_crypto_1.randomUUID)();
|
|
909
|
+
return (0, node_crypto_1.randomUUID)(); // agents always get a coherent id
|
|
522
910
|
}
|
|
523
911
|
propagateUserPrompt(parentSpan, messages) {
|
|
524
912
|
try {
|
|
@@ -554,6 +942,77 @@ const AGENT_CLASSES = new Set([
|
|
|
554
942
|
"Pregel",
|
|
555
943
|
"LangGraph",
|
|
556
944
|
]);
|
|
945
|
+
/**
|
|
946
|
+
* LangChain/LangGraph fires chain-start for every internal Runnable. In
|
|
947
|
+
* Python, ``serialized`` is usually ``None`` for these, so we filter on
|
|
948
|
+
* run_name via a denylist. Matches LangSmith's promotion heuristic.
|
|
949
|
+
* Parity: python `_INTERNAL_RUN_NAMES` (langchain.py:267-306).
|
|
950
|
+
*/
|
|
951
|
+
const INTERNAL_RUN_NAMES = new Set([
|
|
952
|
+
// Runnable wiring/plumbing
|
|
953
|
+
"RunnableSequence",
|
|
954
|
+
"RunnableLambda",
|
|
955
|
+
"RunnablePassthrough",
|
|
956
|
+
"RunnableParallel",
|
|
957
|
+
"RunnableBinding",
|
|
958
|
+
"RunnableMap",
|
|
959
|
+
"RunnableAssign",
|
|
960
|
+
"RunnableBranch",
|
|
961
|
+
"RunnableWithFallbacks",
|
|
962
|
+
"RunnableEach",
|
|
963
|
+
"RunnablePick",
|
|
964
|
+
"RunnableGenerator",
|
|
965
|
+
// Prompt templates
|
|
966
|
+
"Prompt",
|
|
967
|
+
"ChatPromptTemplate",
|
|
968
|
+
"PromptTemplate",
|
|
969
|
+
// langchain.agents (1.x) / legacy create_react_agent internal node names
|
|
970
|
+
"agent",
|
|
971
|
+
"tools",
|
|
972
|
+
"call_model",
|
|
973
|
+
"should_continue",
|
|
974
|
+
"__start__",
|
|
975
|
+
"__end__",
|
|
976
|
+
// Output parsers — invoked as Runnables but not agents. LangChain's
|
|
977
|
+
// ``langchain.agents.create_agent`` with ``ToolStrategy`` (or fallback
|
|
978
|
+
// from ``ProviderStrategy`` on models without native structured output)
|
|
979
|
+
// invokes these as a separate step and they fire on_chain_start.
|
|
980
|
+
"PydanticToolsParser",
|
|
981
|
+
"PydanticOutputParser",
|
|
982
|
+
"JsonOutputParser",
|
|
983
|
+
"JsonOutputToolsParser",
|
|
984
|
+
"JsonOutputKeyToolsParser",
|
|
985
|
+
"StrOutputParser",
|
|
986
|
+
"OutputParser",
|
|
987
|
+
"BaseOutputParser",
|
|
988
|
+
"OpenAIToolsAgentOutputParser",
|
|
989
|
+
"OpenAIFunctionsAgentOutputParser",
|
|
990
|
+
// TS-ecosystem addition beyond the Python `_INTERNAL_RUN_NAMES` list: this
|
|
991
|
+
// class only exists in @langchain/anthropic (JS), not in Python LangChain,
|
|
992
|
+
// so the byte-identical port doesn't cover it. `ChatAnthropic
|
|
993
|
+
// .withStructuredOutput(schema)` (without `include_raw`) internally chains
|
|
994
|
+
// through @langchain/anthropic's AnthropicToolsOutputParser
|
|
995
|
+
// (dist/output_parsers.js), which fires on_chain_start with this run name.
|
|
996
|
+
// Do not remove this during a cross-SDK denylist diff against Python.
|
|
997
|
+
"AnthropicToolsOutputParser",
|
|
998
|
+
// TS-ecosystem addition beyond Python's `_INTERNAL_RUN_NAMES`: needed for
|
|
999
|
+
// @langchain/langgraph 0.2.x internal sub-chains. `createReactAgent`'s
|
|
1000
|
+
// prompt-application step (dist/prebuilt/react_agent_executor.js,
|
|
1001
|
+
// `PROMPT_RUNNABLE_NAME = "prompt"`) is wired via
|
|
1002
|
+
// `RunnableLambda.from(...).withConfig({ runName: "prompt" })` and invoked
|
|
1003
|
+
// as a step INSIDE the "agent" Pregel node — so its own
|
|
1004
|
+
// `metadata.langgraph_node` is stamped with the PARENT node's name (e.g.
|
|
1005
|
+
// "agent"), not "prompt" itself, and the self-match rule (3 above) can't
|
|
1006
|
+
// suppress it. Confirmed live: this fires on_chain_start once per model
|
|
1007
|
+
// turn on installed @langchain/langgraph@0.2.74, producing spurious
|
|
1008
|
+
// `invoke_agent prompt` spans (live-parity REPORT.md). Python's
|
|
1009
|
+
// currently-resolved langgraph (1.2.0, via `create_agent`) doesn't hit
|
|
1010
|
+
// this — a LangGraph major-version confound, not something struct-sdk
|
|
1011
|
+
// controls. Do not remove this during a cross-SDK denylist diff against
|
|
1012
|
+
// Python.
|
|
1013
|
+
"prompt",
|
|
1014
|
+
]);
|
|
1015
|
+
const INTERNAL_RUN_NAME_PREFIXES = ["ChannelWrite<", "Branch<", "RunnableSequence<"];
|
|
557
1016
|
/**
|
|
558
1017
|
* Threading-id metadata keys, in resolution order.
|
|
559
1018
|
*
|
|
@@ -580,11 +1039,67 @@ function metadataThreadId(metadata) {
|
|
|
580
1039
|
}
|
|
581
1040
|
return undefined;
|
|
582
1041
|
}
|
|
583
|
-
|
|
1042
|
+
/**
|
|
1043
|
+
* LangGraph stamps a unique `langgraph_checkpoint_ns` (`tools:<uuid>`) on
|
|
1044
|
+
* each tool-call branch, and the SAME value on the sub-agent graph that the
|
|
1045
|
+
* tool triggers — even across parallel same-named tool calls. Used to
|
|
1046
|
+
* re-parent a sub-agent's `invoke_agent` span under its triggering
|
|
1047
|
+
* `execute_tool` span. Returns the namespace, or `undefined` if absent.
|
|
1048
|
+
* Parity: python `_checkpoint_ns` (langchain.py:325-341).
|
|
1049
|
+
*/
|
|
1050
|
+
function checkpointNs(metadata) {
|
|
1051
|
+
if (!metadata || typeof metadata !== "object")
|
|
1052
|
+
return undefined;
|
|
1053
|
+
const ns = metadata["langgraph_checkpoint_ns"];
|
|
1054
|
+
return typeof ns === "string" && ns.length > 0 ? ns : undefined;
|
|
1055
|
+
}
|
|
1056
|
+
/**
|
|
1057
|
+
* Only promote user-meaningful chains to `invoke_agent` spans.
|
|
1058
|
+
*
|
|
1059
|
+
* Decision order (parity: python `_is_agent_chain`, langchain.py:360-407):
|
|
1060
|
+
*
|
|
1061
|
+
* 1. Explicit `runType === "agent"` (legacy AgentExecutor) → agent.
|
|
1062
|
+
* 2. `chain` class identifies a Pregel/CompiledStateGraph → agent.
|
|
1063
|
+
* 3. `metadata.langgraph_node === runName` → INTERNAL Pregel node (every
|
|
1064
|
+
* internal step of a `create_agent` Pregel fires chain-start with
|
|
1065
|
+
* metadata.langgraph_node set to its node name; for real top-level
|
|
1066
|
+
* agents or sub-agents the names differ or langgraph_node is absent).
|
|
1067
|
+
* 4. Known LangChain plumbing run names (denylist) → not agent.
|
|
1068
|
+
* 5. Otherwise, if there's a runName → agent (user-named chain).
|
|
1069
|
+
*/
|
|
1070
|
+
function isAgentChain(chain, runType, runName, metadata) {
|
|
584
1071
|
if (runType === "agent")
|
|
585
1072
|
return true;
|
|
586
1073
|
const cls = extractClassName(chain);
|
|
587
|
-
|
|
1074
|
+
if (cls && AGENT_CLASSES.has(cls))
|
|
1075
|
+
return true;
|
|
1076
|
+
// Internal Pregel node detection: LangGraph populates metadata with
|
|
1077
|
+
// `langgraph_node` (and `langgraph_step`) on every internal node
|
|
1078
|
+
// callback. The run_name of an internal node matches its langgraph_node;
|
|
1079
|
+
// for the top-level Pregel invocation, langgraph_node is absent; for a
|
|
1080
|
+
// sub-agent invoked from a tool body, langgraph_node may be set BUT
|
|
1081
|
+
// contains the *parent's* node name (e.g. "tools"), which differs from
|
|
1082
|
+
// the sub-agent's own run_name. So equality is the discriminator.
|
|
1083
|
+
if (metadata && runName) {
|
|
1084
|
+
const lgNode = metadata["langgraph_node"];
|
|
1085
|
+
if (typeof lgNode === "string" && lgNode && lgNode === runName)
|
|
1086
|
+
return false;
|
|
1087
|
+
}
|
|
1088
|
+
if (runName) {
|
|
1089
|
+
// LangChain names parametrized runnables `Base<...>` — e.g.
|
|
1090
|
+
// `RunnableParallel<raw>` and `RunnableAssign<parsed,parsing_error>`
|
|
1091
|
+
// from `with_structured_output(include_raw=True)`. Strip the `<...>`
|
|
1092
|
+
// so the base class matches the denylist instead of falling through to
|
|
1093
|
+
// the user-named-chain promotion below (a real production phantom-agent
|
|
1094
|
+
// bug found via a customer's structured-output topology).
|
|
1095
|
+
const baseName = runName.split("<", 1)[0];
|
|
1096
|
+
if (INTERNAL_RUN_NAMES.has(baseName))
|
|
1097
|
+
return false;
|
|
1098
|
+
if (INTERNAL_RUN_NAME_PREFIXES.some((p) => runName.startsWith(p)))
|
|
1099
|
+
return false;
|
|
1100
|
+
return true; // user-named chain → promoted
|
|
1101
|
+
}
|
|
1102
|
+
return false;
|
|
588
1103
|
}
|
|
589
1104
|
function extractClassName(obj) {
|
|
590
1105
|
if (!obj)
|
|
@@ -609,6 +1124,47 @@ function extractParam(obj, key) {
|
|
|
609
1124
|
const v = obj[key];
|
|
610
1125
|
return typeof v === "string" && v.length > 0 ? v : undefined;
|
|
611
1126
|
}
|
|
1127
|
+
/**
|
|
1128
|
+
* Extract the tool name the model was forced to call via `tool_choice`, if
|
|
1129
|
+
* any. Handles both provider shapes LangChain forwards in
|
|
1130
|
+
* `invocation_params.tool_choice`:
|
|
1131
|
+
* - Anthropic: `{type:"tool", name}`
|
|
1132
|
+
* - OpenAI: `{type:"function", function:{name}}`
|
|
1133
|
+
* `invocation` is unknown-typed (LangChain's `extraParams.invocation_params`
|
|
1134
|
+
* is untyped provider passthrough) — every layer is defensively checked.
|
|
1135
|
+
* Parity: python `_forced_tool_name` (langchain.py:410-432).
|
|
1136
|
+
*/
|
|
1137
|
+
function extractForcedToolName(invocation) {
|
|
1138
|
+
if (!invocation || typeof invocation !== "object")
|
|
1139
|
+
return undefined;
|
|
1140
|
+
const tc = invocation["tool_choice"];
|
|
1141
|
+
if (!tc || typeof tc !== "object")
|
|
1142
|
+
return undefined;
|
|
1143
|
+
const t = tc;
|
|
1144
|
+
if (t.type === "tool" && typeof t.name === "string")
|
|
1145
|
+
return t.name;
|
|
1146
|
+
if (t.type === "function") {
|
|
1147
|
+
const fn = t.function;
|
|
1148
|
+
if (fn && typeof fn === "object" && typeof fn.name === "string") {
|
|
1149
|
+
return fn.name;
|
|
1150
|
+
}
|
|
1151
|
+
}
|
|
1152
|
+
return undefined;
|
|
1153
|
+
}
|
|
1154
|
+
/**
|
|
1155
|
+
* `@langchain/anthropic` (JS) keys whose value is a negative sentinel
|
|
1156
|
+
* meaning "not set by the caller" rather than a real request parameter.
|
|
1157
|
+
* `dist/chat_models.js` defaults BOTH `topK` and `topP` to `-1` and always
|
|
1158
|
+
* includes them in `invocationParams()` as `top_k`/`top_p` — unlike
|
|
1159
|
+
* Python's `ChatAnthropic`, which never forwards `top_k`/`top_p` unless the
|
|
1160
|
+
* caller genuinely set them. Confirmed live: TS was stamping
|
|
1161
|
+
* `gen_ai.request.top_k = -1` on every chat span (live-parity REPORT.md).
|
|
1162
|
+
* Scoped tightly to these two keys — do NOT extend blanket-negative
|
|
1163
|
+
* filtering to `frequencyPenalty`/`presencePenalty`, which are legitimately
|
|
1164
|
+
* negative in OpenAI's real range (-2.0..2.0). JS-ecosystem sentinel;
|
|
1165
|
+
* python parity: absent-when-unset.
|
|
1166
|
+
*/
|
|
1167
|
+
const NEGATIVE_SENTINEL_KEYS = new Set(["topK", "top_k", "topP", "top_p"]);
|
|
612
1168
|
function setRequestAttrsFromInvocation(span, invocation) {
|
|
613
1169
|
if (!invocation || typeof invocation !== "object")
|
|
614
1170
|
return;
|
|
@@ -626,8 +1182,11 @@ function setRequestAttrsFromInvocation(span, invocation) {
|
|
|
626
1182
|
];
|
|
627
1183
|
for (const [src, dst] of mapping) {
|
|
628
1184
|
const v = m[src];
|
|
629
|
-
if (typeof v
|
|
630
|
-
|
|
1185
|
+
if (typeof v !== "number")
|
|
1186
|
+
continue;
|
|
1187
|
+
if (NEGATIVE_SENTINEL_KEYS.has(src) && v < 0)
|
|
1188
|
+
continue;
|
|
1189
|
+
span.setAttribute(dst, v);
|
|
631
1190
|
}
|
|
632
1191
|
const stop = m.stop ?? m.stopSequences ?? m.stop_sequences;
|
|
633
1192
|
if (Array.isArray(stop) && stop.length > 0) {
|
|
@@ -664,11 +1223,24 @@ function setLlmResponseAttrs(span, sdk, logger, message, provider, sessionId) {
|
|
|
664
1223
|
const mapped = semconv_js_1.LANGCHAIN_FINISH_REASON_MAP[finish] ?? finish;
|
|
665
1224
|
span.setAttribute(semconv_js_1.GEN_AI.RESPONSE_FINISH_REASONS, [mapped]);
|
|
666
1225
|
}
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
1226
|
+
// Prefer the provider message id (`msg_...` / `chatcmpl-...`) from
|
|
1227
|
+
// response_metadata over LangChain's own run id. ChatAnthropic (and most
|
|
1228
|
+
// LangChain chat model adapters) place the real API-level id in
|
|
1229
|
+
// `response_metadata.id` while `message.id` carries a LangChain-internal
|
|
1230
|
+
// run id. gen_ai.response.id is used as the duplicate-detection
|
|
1231
|
+
// fingerprint downstream, so the provider id must take priority. The
|
|
1232
|
+
// LangChain run id is preserved under `langchain.run.id` (only when it
|
|
1233
|
+
// diverges) so consumers can still join back to LangChain/LangSmith run
|
|
1234
|
+
// data. Parity: python `_set_llm_response_attrs` (langchain.py:1560-1576).
|
|
1235
|
+
const providerId = typeof respMeta.id === "string" ? respMeta.id : undefined;
|
|
1236
|
+
const lcRunId = typeof m.id === "string" ? m.id : undefined;
|
|
1237
|
+
const respId = providerId ?? lcRunId ?? null;
|
|
1238
|
+
if (respId) {
|
|
671
1239
|
span.setAttribute(semconv_js_1.GEN_AI.RESPONSE_ID, respId);
|
|
1240
|
+
if (lcRunId && lcRunId !== respId) {
|
|
1241
|
+
span.setAttribute(semconv_js_1.LANGCHAIN.RUN_ID, lcRunId);
|
|
1242
|
+
}
|
|
1243
|
+
}
|
|
672
1244
|
if (sdk.emitEvents && logger) {
|
|
673
1245
|
emitChoiceEvent(logger, message, provider ?? "langchain", sessionId, span);
|
|
674
1246
|
}
|
|
@@ -699,9 +1271,9 @@ function emitMessageEvents(logger, messages, provider, sessionId, span) {
|
|
|
699
1271
|
attributes: {
|
|
700
1272
|
[semconv_js_1.EVENT_NAME]: eventName,
|
|
701
1273
|
body: payload,
|
|
702
|
-
[semconv_js_1.GEN_AI.
|
|
1274
|
+
[semconv_js_1.GEN_AI.PROVIDER_NAME]: provider,
|
|
703
1275
|
[semconv_js_1.GEN_AI.MESSAGE_INDEX]: i,
|
|
704
|
-
[semconv_js_1.GEN_AI.CONVERSATION_ID]: sessionId,
|
|
1276
|
+
...(sessionId ? { [semconv_js_1.GEN_AI.CONVERSATION_ID]: sessionId } : {}),
|
|
705
1277
|
},
|
|
706
1278
|
});
|
|
707
1279
|
}
|
|
@@ -726,8 +1298,8 @@ function emitChoiceEvent(logger, message, provider, sessionId, span) {
|
|
|
726
1298
|
attributes: {
|
|
727
1299
|
[semconv_js_1.EVENT_NAME]: semconv_js_1.EVENT_NAMES.CHOICE,
|
|
728
1300
|
body: payload,
|
|
729
|
-
[semconv_js_1.GEN_AI.
|
|
730
|
-
[semconv_js_1.GEN_AI.CONVERSATION_ID]: sessionId,
|
|
1301
|
+
[semconv_js_1.GEN_AI.PROVIDER_NAME]: provider,
|
|
1302
|
+
...(sessionId ? { [semconv_js_1.GEN_AI.CONVERSATION_ID]: sessionId } : {}),
|
|
731
1303
|
},
|
|
732
1304
|
});
|
|
733
1305
|
}
|
|
@@ -747,6 +1319,23 @@ function extractToolCallIdFromInput(input) {
|
|
|
747
1319
|
}
|
|
748
1320
|
return undefined;
|
|
749
1321
|
}
|
|
1322
|
+
/**
|
|
1323
|
+
* Pop the first pending tool_use id matching `name` from a RunState-pinned
|
|
1324
|
+
* (run-tree-scoped, not ALS-scoped) queue object. FIFO semantics identical
|
|
1325
|
+
* to `context.ts`'s `popPendingToolCallId` — see `RunState.toolCallQueue`.
|
|
1326
|
+
* Used for BOTH manual and bare ownership: in manual mode the queue object
|
|
1327
|
+
* passed in is the same one `getPendingToolCalls()` (context.ts) reads, so
|
|
1328
|
+
* popping here and popping via that ALS-scoped helper observe the same
|
|
1329
|
+
* state.
|
|
1330
|
+
*/
|
|
1331
|
+
function popFromQueue(queue, name) {
|
|
1332
|
+
if (!queue)
|
|
1333
|
+
return undefined;
|
|
1334
|
+
const ids = queue[name];
|
|
1335
|
+
if (!ids || ids.length === 0)
|
|
1336
|
+
return undefined;
|
|
1337
|
+
return ids.shift();
|
|
1338
|
+
}
|
|
750
1339
|
function recordError(span, err) {
|
|
751
1340
|
const errorType = err instanceof Error ? err.constructor.name : typeof err;
|
|
752
1341
|
const message = err instanceof Error ? err.message : String(err);
|