@librechat/agents 3.3.11 → 3.3.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (192) hide show
  1. package/dist/cjs/graphs/Graph.cjs +246 -25
  2. package/dist/cjs/graphs/Graph.cjs.map +1 -1
  3. package/dist/cjs/llm/bedrock/index.cjs +13 -2
  4. package/dist/cjs/llm/bedrock/index.cjs.map +1 -1
  5. package/dist/cjs/llm/invoke.cjs +146 -36
  6. package/dist/cjs/llm/invoke.cjs.map +1 -1
  7. package/dist/cjs/llm/openai/index.cjs +2 -2
  8. package/dist/cjs/llm/openai/index.cjs.map +1 -1
  9. package/dist/cjs/llm/streamLimits.cjs +723 -0
  10. package/dist/cjs/llm/streamLimits.cjs.map +1 -0
  11. package/dist/cjs/main.cjs +8 -0
  12. package/dist/cjs/run.cjs +6 -2
  13. package/dist/cjs/run.cjs.map +1 -1
  14. package/dist/cjs/session/AgentSession.cjs +4 -1
  15. package/dist/cjs/session/AgentSession.cjs.map +1 -1
  16. package/dist/cjs/stream.cjs +103 -12
  17. package/dist/cjs/stream.cjs.map +1 -1
  18. package/dist/cjs/summarization/node.cjs +157 -37
  19. package/dist/cjs/summarization/node.cjs.map +1 -1
  20. package/dist/cjs/tools/BashExecutor.cjs +3 -2
  21. package/dist/cjs/tools/BashExecutor.cjs.map +1 -1
  22. package/dist/cjs/tools/CodeExecutor.cjs +4 -3
  23. package/dist/cjs/tools/CodeExecutor.cjs.map +1 -1
  24. package/dist/cjs/tools/ProgrammaticToolCalling.cjs +5 -3
  25. package/dist/cjs/tools/ProgrammaticToolCalling.cjs.map +1 -1
  26. package/dist/cjs/tools/ToolNode.cjs +76 -3
  27. package/dist/cjs/tools/ToolNode.cjs.map +1 -1
  28. package/dist/cjs/tools/ToolSearch.cjs +3 -2
  29. package/dist/cjs/tools/ToolSearch.cjs.map +1 -1
  30. package/dist/cjs/tools/search/crw-scraper.cjs +7 -1
  31. package/dist/cjs/tools/search/crw-scraper.cjs.map +1 -1
  32. package/dist/cjs/tools/search/crw-search.cjs +3 -1
  33. package/dist/cjs/tools/search/crw-search.cjs.map +1 -1
  34. package/dist/cjs/tools/search/firecrawl.cjs +7 -1
  35. package/dist/cjs/tools/search/firecrawl.cjs.map +1 -1
  36. package/dist/cjs/tools/search/keenable-scraper.cjs +7 -1
  37. package/dist/cjs/tools/search/keenable-scraper.cjs.map +1 -1
  38. package/dist/cjs/tools/search/keenable-search.cjs +3 -1
  39. package/dist/cjs/tools/search/keenable-search.cjs.map +1 -1
  40. package/dist/cjs/tools/search/rerankers.cjs +26 -8
  41. package/dist/cjs/tools/search/rerankers.cjs.map +1 -1
  42. package/dist/cjs/tools/search/search.cjs +30 -10
  43. package/dist/cjs/tools/search/search.cjs.map +1 -1
  44. package/dist/cjs/tools/search/serper-scraper.cjs +7 -1
  45. package/dist/cjs/tools/search/serper-scraper.cjs.map +1 -1
  46. package/dist/cjs/tools/search/tavily-scraper.cjs +7 -1
  47. package/dist/cjs/tools/search/tavily-scraper.cjs.map +1 -1
  48. package/dist/cjs/tools/search/tavily-search.cjs +3 -1
  49. package/dist/cjs/tools/search/tavily-search.cjs.map +1 -1
  50. package/dist/cjs/tools/search/tool.cjs +16 -2
  51. package/dist/cjs/tools/search/tool.cjs.map +1 -1
  52. package/dist/cjs/tools/subagent/SubagentExecutor.cjs +54 -3
  53. package/dist/cjs/tools/subagent/SubagentExecutor.cjs.map +1 -1
  54. package/dist/cjs/utils/index.cjs +1 -0
  55. package/dist/cjs/utils/misc.cjs +12 -0
  56. package/dist/cjs/utils/misc.cjs.map +1 -1
  57. package/dist/cjs/utils/proxy.cjs +63 -0
  58. package/dist/cjs/utils/proxy.cjs.map +1 -0
  59. package/dist/esm/graphs/Graph.mjs +245 -24
  60. package/dist/esm/graphs/Graph.mjs.map +1 -1
  61. package/dist/esm/llm/bedrock/index.mjs +13 -2
  62. package/dist/esm/llm/bedrock/index.mjs.map +1 -1
  63. package/dist/esm/llm/invoke.mjs +146 -36
  64. package/dist/esm/llm/invoke.mjs.map +1 -1
  65. package/dist/esm/llm/openai/index.mjs +2 -2
  66. package/dist/esm/llm/openai/index.mjs.map +1 -1
  67. package/dist/esm/llm/streamLimits.mjs +704 -0
  68. package/dist/esm/llm/streamLimits.mjs.map +1 -0
  69. package/dist/esm/main.mjs +4 -2
  70. package/dist/esm/run.mjs +6 -2
  71. package/dist/esm/run.mjs.map +1 -1
  72. package/dist/esm/session/AgentSession.mjs +4 -1
  73. package/dist/esm/session/AgentSession.mjs.map +1 -1
  74. package/dist/esm/stream.mjs +103 -12
  75. package/dist/esm/stream.mjs.map +1 -1
  76. package/dist/esm/summarization/node.mjs +157 -37
  77. package/dist/esm/summarization/node.mjs.map +1 -1
  78. package/dist/esm/tools/BashExecutor.mjs +3 -2
  79. package/dist/esm/tools/BashExecutor.mjs.map +1 -1
  80. package/dist/esm/tools/CodeExecutor.mjs +4 -3
  81. package/dist/esm/tools/CodeExecutor.mjs.map +1 -1
  82. package/dist/esm/tools/ProgrammaticToolCalling.mjs +5 -3
  83. package/dist/esm/tools/ProgrammaticToolCalling.mjs.map +1 -1
  84. package/dist/esm/tools/ToolNode.mjs +76 -3
  85. package/dist/esm/tools/ToolNode.mjs.map +1 -1
  86. package/dist/esm/tools/ToolSearch.mjs +3 -2
  87. package/dist/esm/tools/ToolSearch.mjs.map +1 -1
  88. package/dist/esm/tools/search/crw-scraper.mjs +7 -1
  89. package/dist/esm/tools/search/crw-scraper.mjs.map +1 -1
  90. package/dist/esm/tools/search/crw-search.mjs +3 -1
  91. package/dist/esm/tools/search/crw-search.mjs.map +1 -1
  92. package/dist/esm/tools/search/firecrawl.mjs +7 -1
  93. package/dist/esm/tools/search/firecrawl.mjs.map +1 -1
  94. package/dist/esm/tools/search/keenable-scraper.mjs +7 -1
  95. package/dist/esm/tools/search/keenable-scraper.mjs.map +1 -1
  96. package/dist/esm/tools/search/keenable-search.mjs +3 -1
  97. package/dist/esm/tools/search/keenable-search.mjs.map +1 -1
  98. package/dist/esm/tools/search/rerankers.mjs +26 -8
  99. package/dist/esm/tools/search/rerankers.mjs.map +1 -1
  100. package/dist/esm/tools/search/search.mjs +30 -10
  101. package/dist/esm/tools/search/search.mjs.map +1 -1
  102. package/dist/esm/tools/search/serper-scraper.mjs +7 -1
  103. package/dist/esm/tools/search/serper-scraper.mjs.map +1 -1
  104. package/dist/esm/tools/search/tavily-scraper.mjs +7 -1
  105. package/dist/esm/tools/search/tavily-scraper.mjs.map +1 -1
  106. package/dist/esm/tools/search/tavily-search.mjs +3 -1
  107. package/dist/esm/tools/search/tavily-search.mjs.map +1 -1
  108. package/dist/esm/tools/search/tool.mjs +16 -2
  109. package/dist/esm/tools/search/tool.mjs.map +1 -1
  110. package/dist/esm/tools/subagent/SubagentExecutor.mjs +54 -3
  111. package/dist/esm/tools/subagent/SubagentExecutor.mjs.map +1 -1
  112. package/dist/esm/utils/index.mjs +1 -0
  113. package/dist/esm/utils/misc.mjs +12 -1
  114. package/dist/esm/utils/misc.mjs.map +1 -1
  115. package/dist/esm/utils/proxy.mjs +62 -0
  116. package/dist/esm/utils/proxy.mjs.map +1 -0
  117. package/dist/types/graphs/Graph.d.ts +53 -1
  118. package/dist/types/index.d.ts +2 -0
  119. package/dist/types/llm/invoke.d.ts +25 -4
  120. package/dist/types/llm/openai/index.d.ts +3 -0
  121. package/dist/types/llm/streamLimits.d.ts +314 -0
  122. package/dist/types/run.d.ts +1 -0
  123. package/dist/types/summarization/node.d.ts +27 -2
  124. package/dist/types/tools/BashExecutor.d.ts +2 -2
  125. package/dist/types/tools/CodeExecutor.d.ts +3 -3
  126. package/dist/types/tools/ToolNode.d.ts +11 -1
  127. package/dist/types/tools/search/crw-scraper.d.ts +2 -0
  128. package/dist/types/tools/search/firecrawl.d.ts +2 -0
  129. package/dist/types/tools/search/keenable-scraper.d.ts +2 -0
  130. package/dist/types/tools/search/rerankers.d.ts +9 -5
  131. package/dist/types/tools/search/serper-scraper.d.ts +2 -0
  132. package/dist/types/tools/search/tavily-scraper.d.ts +2 -0
  133. package/dist/types/tools/search/types.d.ts +28 -33
  134. package/dist/types/tools/subagent/SubagentExecutor.d.ts +44 -0
  135. package/dist/types/types/graph.d.ts +7 -1
  136. package/dist/types/types/run.d.ts +46 -1
  137. package/dist/types/types/tools.d.ts +21 -0
  138. package/dist/types/utils/index.d.ts +1 -0
  139. package/dist/types/utils/misc.d.ts +7 -0
  140. package/dist/types/utils/proxy.d.ts +31 -0
  141. package/package.json +2 -1
  142. package/src/__tests__/stream.eagerArgsDivergence.test.ts +158 -0
  143. package/src/__tests__/stream.eagerEventExecution.test.ts +1 -0
  144. package/src/__tests__/stream.streamLimits.test.ts +1982 -0
  145. package/src/graphs/Graph.ts +302 -27
  146. package/src/graphs/__tests__/Graph.breakerLifecycle.test.ts +234 -0
  147. package/src/index.ts +11 -0
  148. package/src/llm/bedrock/index.ts +22 -2
  149. package/src/llm/invoke.streamLimits.test.ts +142 -0
  150. package/src/llm/invoke.test.ts +89 -1
  151. package/src/llm/invoke.ts +197 -20
  152. package/src/llm/openai/cacheWriteTokens.test.ts +112 -0
  153. package/src/llm/openai/index.ts +14 -6
  154. package/src/llm/streamLimits.test.ts +450 -0
  155. package/src/llm/streamLimits.ts +1158 -0
  156. package/src/run.ts +4 -0
  157. package/src/session/AgentSession.ts +5 -0
  158. package/src/stream.ts +141 -6
  159. package/src/summarization/__tests__/node.test.ts +269 -0
  160. package/src/summarization/chunkHandler.test.ts +196 -0
  161. package/src/summarization/node.ts +203 -6
  162. package/src/tools/BashExecutor.ts +4 -3
  163. package/src/tools/CodeExecutor.ts +5 -4
  164. package/src/tools/ProgrammaticToolCalling.ts +7 -5
  165. package/src/tools/ToolNode.ts +109 -6
  166. package/src/tools/ToolSearch.ts +4 -3
  167. package/src/tools/__tests__/BashExecutor.test.ts +2 -2
  168. package/src/tools/__tests__/ProgrammaticToolCalling.test.ts +2 -4
  169. package/src/tools/__tests__/SubagentExecutor.test.ts +126 -0
  170. package/src/tools/__tests__/ToolNode.breakerSignal.test.ts +389 -0
  171. package/src/tools/__tests__/ToolNode.streamLimits.test.ts +69 -0
  172. package/src/tools/search/crw-scraper.ts +6 -0
  173. package/src/tools/search/crw-search.ts +6 -1
  174. package/src/tools/search/firecrawl.ts +6 -0
  175. package/src/tools/search/http-agent.test.ts +133 -0
  176. package/src/tools/search/keenable-scraper.ts +6 -0
  177. package/src/tools/search/keenable-search.ts +6 -1
  178. package/src/tools/search/rerankers.ts +36 -10
  179. package/src/tools/search/search.ts +29 -11
  180. package/src/tools/search/serper-scraper.ts +6 -0
  181. package/src/tools/search/tavily-scraper.ts +6 -0
  182. package/src/tools/search/tavily-search.ts +2 -0
  183. package/src/tools/search/tool.ts +16 -0
  184. package/src/tools/search/types.ts +31 -33
  185. package/src/tools/subagent/SubagentExecutor.ts +96 -3
  186. package/src/types/graph.ts +7 -0
  187. package/src/types/run.ts +49 -1
  188. package/src/types/tools.ts +21 -0
  189. package/src/utils/index.ts +1 -0
  190. package/src/utils/misc.ts +19 -0
  191. package/src/utils/proxy.test.ts +176 -0
  192. package/src/utils/proxy.ts +93 -0
@@ -0,0 +1,704 @@
1
+ import { getStreamedToolCallSeal } from "../tools/streamedToolCallSeals.mjs";
2
+ //#region src/llm/streamLimits.ts
3
+ /**
4
+ * Circuit breakers for pathological model streams.
5
+ *
6
+ * A malformed generation can stream a single tool call's arguments for many
7
+ * minutes at the provider's full token rate while the arguments never become
8
+ * executable (observed live: one 149,923-char SQL argument streamed for 26
9
+ * minutes before the 64k output-token ceiling finally ended the run). These
10
+ * guards fail fast instead: when a limit trips, the stream handler throws
11
+ * `StreamLimitExceededError` out of the run's `streamEvents` loop, which
12
+ * tears down the in-flight provider request where it stands (see the
13
+ * mid-flight halt notes in `Run.processStream`: leaving the loop cancels the
14
+ * reader and langgraph aborts the model call).
15
+ */
16
+ /** Default cap on a single streamed tool call's cumulative argument bytes (64 KiB). */
17
+ const DEFAULT_MAX_TOOL_CALL_ARG_BYTES = 65536;
18
+ function resolveLimit(value, fallback) {
19
+ if (value == null || Number.isNaN(value)) return fallback;
20
+ if (!Number.isFinite(value)) return 0;
21
+ const whole = Math.floor(value);
22
+ return whole > 0 ? whole : 0;
23
+ }
24
+ /**
25
+ * Normalizes host-supplied limits once at graph construction. `undefined`
26
+ * applies the default for each guard (the tool-argument byte cap is ON by
27
+ * default, the per-turn event cap is opt-in), `0` and negative values
28
+ * disable a guard, `Infinity` means "no limit" and also disables, and `NaN`
29
+ * falls back to the default.
30
+ */
31
+ function resolveStreamLimits(limits) {
32
+ const maxToolCallArgBytes = resolveLimit(limits?.maxToolCallArgBytes, DEFAULT_MAX_TOOL_CALL_ARG_BYTES);
33
+ let maxToolCallArgBytesByTool;
34
+ if (limits?.maxToolCallArgBytesByTool != null) for (const [name, value] of Object.entries(limits.maxToolCallArgBytesByTool)) {
35
+ /** An unusable entry (NaN) falls back to the global cap by omission,
36
+ * matching how the global field treats NaN. */
37
+ if (name === "" || Number.isNaN(value)) continue;
38
+ /** Prototype-free: bracket-assigning a `__proto__` tool name into a
39
+ * plain object invokes the prototype setter instead of creating an own
40
+ * property, silently dropping that tool's configured override. */
41
+ maxToolCallArgBytesByTool ??= Object.create(null);
42
+ maxToolCallArgBytesByTool[name] = resolveLimit(value, maxToolCallArgBytes);
43
+ }
44
+ const hasEnforceableToolCallArgLimit = maxToolCallArgBytes > 0 || maxToolCallArgBytesByTool != null && Object.values(maxToolCallArgBytesByTool).some((value) => value > 0);
45
+ return {
46
+ maxToolCallArgBytes,
47
+ ...maxToolCallArgBytesByTool != null && { maxToolCallArgBytesByTool },
48
+ maxDeltaEventsPerTurn: resolveLimit(limits?.maxDeltaEventsPerTurn, 0),
49
+ hasEnforceableToolCallArgLimit
50
+ };
51
+ }
52
+ const DEFAULT_RESOLVED_LIMITS = resolveStreamLimits();
53
+ function buildLimitMessage(kind, limit, toolName) {
54
+ if (kind === "tool_call_args") return `Streamed tool call arguments exceeded the ${limit}-byte safety limit${toolName != null && toolName !== "" ? ` (tool call: ${toolName})` : ""}. The generation was aborted mid-stream: arguments growing this large without completing usually indicate a runaway or malformed tool call. Raise 'maxToolCallArgBytes' (or the tool's 'maxToolCallArgBytesByTool' entry) if your tools legitimately need larger arguments.`;
55
+ return `Model stream exceeded the ${limit}-event safety limit for a single generation turn. The generation was aborted mid-stream: this usually indicates a looping or duplicated provider stream. Raise 'maxDeltaEventsPerTurn' if legitimate generations need more stream events.`;
56
+ }
57
+ /**
58
+ * Raised when a {@link t.StreamLimits} guard trips. Thrown from inside the
59
+ * run's `streamEvents` loop, so the in-flight provider request is torn down
60
+ * and `processStream` rejects with this error.
61
+ */
62
+ var StreamLimitExceededError = class extends Error {
63
+ kind;
64
+ limit;
65
+ observed;
66
+ toolName;
67
+ constructor({ kind, limit, observed, toolName }) {
68
+ super(buildLimitMessage(kind, limit, toolName));
69
+ this.name = "StreamLimitExceededError";
70
+ this.kind = kind;
71
+ this.limit = limit;
72
+ this.observed = observed;
73
+ this.toolName = toolName;
74
+ }
75
+ };
76
+ /**
77
+ * Identity of one model generation, derived from langgraph's node-execution
78
+ * metadata. Deliberately NOT `Graph.getStepKey()`: the step key forks within
79
+ * a single generation on reasoning transitions (`'reasoning'` /
80
+ * `post-reasoning-<n>` suffixes in `getKeyList`) and on mid-turn server-tool
81
+ * results (`invokedToolIds` count), which would hand a fresh budget to each
82
+ * segment. One agent-node execution is one superstep, so
83
+ * `checkpoint_ns + node + step` stays stable for the whole generation and
84
+ * distinguishes parallel agents in the same superstep.
85
+ *
86
+ * The attempt stamp scopes attempts within one node execution:
87
+ * `attemptInvoke` is the single funnel for primary, fallback, and
88
+ * summarization model calls and stamps {@link STREAM_LIMIT_ATTEMPT_KEY}
89
+ * with a unique sequence number into each attempt's callback metadata. A
90
+ * fallback's chunks therefore key separately from the failed primary's,
91
+ * even for two fallbacks configured with the same provider and model name,
92
+ * and even when the decoupled `streamEvents` reader drains a failed
93
+ * attempt's buffered chunks late. Those late chunks land in their own
94
+ * attempt's bucket instead of polluting the next one's.
95
+ */
96
+ function resolveGenerationKey(metadata) {
97
+ if (metadata == null) return "";
98
+ return `${metadata.langgraph_checkpoint_ns ?? ""}|${metadata.langgraph_node ?? ""}|${metadata.langgraph_step ?? ""}|${metadata["lc_stream_limit_attempt"] ?? ""}`;
99
+ }
100
+ /**
101
+ * Metadata key carrying the unique per-model-attempt sequence number that
102
+ * `attemptInvoke` stamps into every attempt's callback metadata. Part of
103
+ * the generation key so budgets never alias across attempts.
104
+ */
105
+ const STREAM_LIMIT_ATTEMPT_KEY = "lc_stream_limit_attempt";
106
+ /**
107
+ * Event-metadata marker for a chunk the SDK re-dispatches inline after
108
+ * transforming it (`attemptInvoke`'s OpenRouter final-reasoning replay). The
109
+ * original wire chunk still reaches the handler through `streamEvents` and
110
+ * is counted there, so the re-dispatch must not consume a second
111
+ * event-budget slot.
112
+ */
113
+ const STREAM_LIMIT_REDISPATCH_KEY = "lc_stream_limit_redispatch";
114
+ /**
115
+ * Metadata key carrying the graph's breaker epoch at the time a model
116
+ * attempt started. The stream handler trips the shared breaker only when
117
+ * the event's epoch matches the live controller's — a straggling chunk from
118
+ * a failed run that outlived `resetValues()` must fail its own (dead) run,
119
+ * not abort the controller now serving the next one. A primitive rather
120
+ * than the controller itself so attempt metadata stays serialization-safe
121
+ * for tracing.
122
+ */
123
+ const STREAM_LIMIT_EPOCH_KEY = "lc_stream_limit_epoch";
124
+ /**
125
+ * Configurable key carrying the tool batch's entry-captured
126
+ * {@link RunBreakerScope} to tools that spawn their own runs (subagents).
127
+ * Captured BEFORE PreToolUse hooks, so a reset during a hook cannot rebind
128
+ * the spawned child to the new run's controller. Stripped from host-facing
129
+ * batch requests and from child-graph configurables.
130
+ */
131
+ const RUN_BREAKER_SCOPE_CONFIG_KEY = "lc_run_breaker_scope";
132
+ /**
133
+ * True when the event's `single` seal marks this chunk's own call as
134
+ * complete. On the OpenAI Responses adapter the sealing chunk RESTATES the
135
+ * full argument string (`response.function_call_arguments.done`), so summing
136
+ * it would double-count every legitimate call; on Bedrock Converse the
137
+ * sealing chunk carries empty args. Either way the call is finished: its
138
+ * tally is replaced by the sealing chunk's own bytes, checked, and released.
139
+ */
140
+ function sealsChunk(seal, chunk) {
141
+ if (seal == null || seal.kind !== "single") return false;
142
+ /** Either supplied identifier agreeing is sufficient (matching the
143
+ * eager-call seal handling in stream.ts): a mismatch on one identifier
144
+ * must not veto a match on the other, or an OpenAI-style full-argument
145
+ * restatement gets ADDED to the tally instead of replacing it, and an
146
+ * empty seal leaves the tally unreleased. */
147
+ if (seal.index != null && chunk.index != null && seal.index === chunk.index) return true;
148
+ const sealId = seal.id != null && seal.id !== "" ? seal.id : void 0;
149
+ return sealId != null && sealId === chunkCallId(chunk);
150
+ }
151
+ function chunkToolName(chunk) {
152
+ return chunk.name != null && chunk.name !== "" ? chunk.name : void 0;
153
+ }
154
+ /** The chunk's id with empty-string placeholders treated as absent —
155
+ * OpenAI-compatible adapters emit `id: ''` on continuation deltas, and a
156
+ * shared placeholder must not merge independent calls onto one identity
157
+ * (mirrors `getEagerToolChunkKey`). */
158
+ function chunkCallId(chunk) {
159
+ return chunk.id != null && chunk.id !== "" ? chunk.id : void 0;
160
+ }
161
+ function isHighSurrogate(code) {
162
+ return code >= 55296 && code <= 56319;
163
+ }
164
+ function isLowSurrogate(code) {
165
+ return code >= 56320 && code <= 57343;
166
+ }
167
+ /**
168
+ * Accumulates the UTF-8 byte size of streamed tool-call argument chunks per
169
+ * in-flight tool call and throws once a single call's cumulative bytes
170
+ * exceed `maxToolCallArgBytes`. Runs once per streamed chunk event, before
171
+ * complete tool calls are dispatched or eagerly executed and before chunks
172
+ * are recorded, so a tripped limit stops the run without dispatching the
173
+ * offending call.
174
+ *
175
+ * Calls are keyed by generation and chunk `index`, falling back to the
176
+ * chunk `id` and then to the chunk's position within the event's batch when
177
+ * a provider identifies chunks by neither (Google emits complete parallel
178
+ * calls with optional ids and no index). A `kind: 'all'` arrival seal marks
179
+ * every chunk in the event as its own complete call, so those are checked
180
+ * standalone and never share a budget; a matching `kind: 'single'` seal
181
+ * replaces the call's tally with the sealing chunk's own bytes (the OpenAI
182
+ * Responses done-chunk restates the full argument string) and releases it.
183
+ */
184
+ function enforceStreamedToolCallArgLimit({ graph, metadata, toolCallChunks, responseMetadata, parsedToolCalls }) {
185
+ const resolved = graph.streamLimits ?? DEFAULT_RESOLVED_LIMITS;
186
+ const globalLimit = resolved.maxToolCallArgBytes;
187
+ const byTool = resolved.maxToolCallArgBytesByTool;
188
+ if (!resolved.hasEnforceableToolCallArgLimit) return;
189
+ /** An inline re-dispatch of a transformed chunk (OpenRouter final-reasoning
190
+ * replay) duplicates tool-call chunks the original `streamEvents` event
191
+ * already charged — the original always survives the content-specific
192
+ * skips when it carries tool calls, so counting the marked copy would
193
+ * double every legitimate argument byte. */
194
+ if (metadata?.["lc_stream_limit_redispatch"] === true) return;
195
+ const tallies = graph.streamedToolCallArgTallies ??= /* @__PURE__ */ new Map();
196
+ const generationKey = resolveGenerationKey(metadata);
197
+ const seal = getStreamedToolCallSeal(responseMetadata);
198
+ /** The complete call in this event sharing the chunk's id — the strongest
199
+ * name evidence: adapters may stream raw names in FRAGMENTS, and an
200
+ * id-correlated complete call states the full name outright. */
201
+ const correlateParsedNameById = (id) => {
202
+ if (parsedToolCalls == null) return;
203
+ for (const parsed of parsedToolCalls) if (parsed.id === id && parsed.name != null && parsed.name !== "") return parsed.name;
204
+ };
205
+ const generationPrefix = `${generationKey}:`;
206
+ const getLiveGenerationTallies = () => {
207
+ const live = /* @__PURE__ */ new Set();
208
+ for (const [tallyKey, tally] of tallies) if (tallyKey.startsWith(generationPrefix)) live.add(tally);
209
+ return [...live];
210
+ };
211
+ const resolveChunkName = (chunk) => {
212
+ const chunkId = chunkCallId(chunk);
213
+ const correlated = chunkId != null ? correlateParsedNameById(chunkId) : void 0;
214
+ if (correlated != null) return correlated;
215
+ const name = chunkToolName(chunk);
216
+ if (name != null || parsedToolCalls == null || chunkId != null) return name;
217
+ /** No id to correlate on; positional association is unambiguous only
218
+ * when the event carries exactly one raw chunk AND one parsed call —
219
+ * with several id-less raw chunks in flight, handing one parsed call's
220
+ * name (and its override) to all of them would let an unrelated
221
+ * still-partial call bypass the global cap. */
222
+ if (parsedToolCalls.length === 1 && toolCallChunks.length === 1) {
223
+ const only = parsedToolCalls[0];
224
+ if (only.name != null && only.name !== "") return only.name;
225
+ }
226
+ };
227
+ const enforceTallyLimit = (target) => {
228
+ const tallyLimit = byTool != null && target.name != null && Object.hasOwn(byTool, target.name) ? byTool[target.name] : globalLimit;
229
+ if (tallyLimit > 0 && target.bytes > tallyLimit) throw new StreamLimitExceededError({
230
+ kind: "tool_call_args",
231
+ limit: tallyLimit,
232
+ observed: target.bytes,
233
+ toolName: target.name
234
+ });
235
+ };
236
+ for (let i = 0; i < toolCallChunks.length; i++) {
237
+ const chunk = toolCallChunks[i];
238
+ const args = chunk.args;
239
+ const hasArgs = typeof args === "string" && args !== "";
240
+ if (seal?.kind === "all") {
241
+ if (!hasArgs) continue;
242
+ const bytes = Buffer.byteLength(args, "utf8");
243
+ const sealedName = resolveChunkName(chunk);
244
+ const limit = byTool != null && sealedName != null && Object.hasOwn(byTool, sealedName) ? byTool[sealedName] : globalLimit;
245
+ if (limit > 0 && bytes > limit) throw new StreamLimitExceededError({
246
+ kind: "tool_call_args",
247
+ limit,
248
+ observed: bytes,
249
+ toolName: sealedName
250
+ });
251
+ continue;
252
+ }
253
+ /** Keys are namespaced by identity kind (`i:` index, `c:` id, `#`
254
+ * batch position) so an index and a string id with the same textual
255
+ * value — index 0 and id "0" — cannot alias distinct calls onto one
256
+ * tally. Empty-string ids are placeholders, not identities. */
257
+ const chunkId = chunkCallId(chunk);
258
+ const sealed = sealsChunk(seal, chunk);
259
+ /** Applies this chunk's name contribution and reports whether the
260
+ * effective name changed. Adapters may stream the name in FRAGMENTS
261
+ * ("create_" then "file"), so unsealed fragments append — matching
262
+ * langchain's own tool-call-chunk merge — while a sealing chunk's name
263
+ * is a full restatement and replaces, mirroring how sealed args replace
264
+ * the tally bytes. An id-correlated complete call in the same event
265
+ * outranks both; positional parsed-call correlation fills in only when
266
+ * the chunk carries no fragment. */
267
+ const applyChunkName = (target) => {
268
+ const correlated = chunkId != null ? correlateParsedNameById(chunkId) : void 0;
269
+ const fragment = chunkToolName(chunk);
270
+ let next = target.name;
271
+ if (correlated != null)
272
+ /** An id-correlated complete call wins over fragment accumulation:
273
+ * a raw name like "create_" must not hide the "create_file"
274
+ * override behind the global cap. */
275
+ next = correlated;
276
+ else if (fragment != null) {
277
+ if (sealed || target.name == null) next = fragment;
278
+ else if (fragment !== target.name)
279
+ /** A fragment identical to the accumulated name is a repeated
280
+ * full-name delta (a common provider shape) and is a no-op; a
281
+ * differing fragment is a continuation and appends. */
282
+ next = target.name + fragment;
283
+ } else if (target.name == null) next = resolveChunkName(chunk);
284
+ if (next === target.name) return false;
285
+ target.name = next;
286
+ return true;
287
+ };
288
+ /** A single identifier-less event while calls are live is a sparse
289
+ * parallel continuation: its position is relative to this event, not to
290
+ * the original batch, so `#0` cannot identify which call it belongs to.
291
+ * With exactly one live call the association is unambiguous — charge
292
+ * that tally, keeping its budget continuous (a fresh `#0` tally here
293
+ * would RESET the sole remaining call's byte budget). With several,
294
+ * charge every live candidate. Each tally is judged under its OWN
295
+ * name-specific limit: comparing only against the global cap would let
296
+ * a call with a lower per-tool override stream past it indefinitely
297
+ * while its chunks stay anonymous. */
298
+ if (chunk.index == null && chunkId == null && toolCallChunks.length === 1) {
299
+ const liveTallies = getLiveGenerationTallies();
300
+ if (liveTallies.length > 0) {
301
+ if (liveTallies.length === 1 && applyChunkName(liveTallies[0]))
302
+ /** A late name can select a lower override than the limit under
303
+ * which prior bytes were accepted, so re-judge before charging
304
+ * this continuation. */
305
+ enforceTallyLimit(liveTallies[0]);
306
+ if (hasArgs) {
307
+ const argBytes = Buffer.byteLength(args, "utf8");
308
+ for (const liveTally of liveTallies) {
309
+ const reconcilesSplitPair = liveTally.pendingHighSurrogate === true && isLowSurrogate(args.charCodeAt(0));
310
+ liveTally.bytes += reconcilesSplitPair ? argBytes - 2 : argBytes;
311
+ liveTally.pendingHighSurrogate = isHighSurrogate(args.charCodeAt(args.length - 1));
312
+ enforceTallyLimit(liveTally);
313
+ }
314
+ }
315
+ continue;
316
+ }
317
+ }
318
+ let key;
319
+ if (chunk.index != null) key = `${generationKey}:i:${chunk.index}`;
320
+ else if (chunkId != null) key = `${generationKey}:c:${chunkId}`;
321
+ else key = `${generationKey}:#${i}`;
322
+ /** Batch position is a stable identity only when the event carries a
323
+ * single chunk: sparse parallel events reuse position 0 for whichever
324
+ * call happens to continue, and a position alias would hand one call's
325
+ * tally — and its per-tool override — to another live call's deltas. */
326
+ const positionAliasSafe = toolCallChunks.length === 1;
327
+ /** Secondary keys this call is also reachable under, so a later delta
328
+ * that drops one or both identifiers still lands on the same tally:
329
+ * id-bearing chunks register the batch-position fallback (single-chunk
330
+ * events only), and chunks carrying both identifiers additionally
331
+ * register the id. */
332
+ const aliasCandidates = [];
333
+ if (chunkId != null) {
334
+ if (positionAliasSafe) aliasCandidates.push(`${generationKey}:#${i}`);
335
+ if (chunk.index != null) aliasCandidates.push(`${generationKey}:c:${chunkId}`);
336
+ }
337
+ let tally = tallies.get(key);
338
+ /** A later delta can also ADD an identifier, changing the primary key;
339
+ * adopt the call's existing tally through a stronger prior identity
340
+ * before allocating. An index maps to that call's original batch
341
+ * position, never this sparse event's position. A newly arrived id can
342
+ * adopt a position only when one anonymous tally is live; with several,
343
+ * positional association is ambiguous. The old identity is recorded as
344
+ * an alias and released together with the rest. */
345
+ if (tally == null) {
346
+ const adoptionCandidates = [];
347
+ if (chunkId != null) adoptionCandidates.push(`${generationKey}:c:${chunkId}`);
348
+ if (chunk.index != null) {
349
+ /** An index is stable across sparse events; the loop position is
350
+ * merely this event's position and may be 0 for call index 1. */
351
+ const indexedPositionKey = `${generationKey}:#${chunk.index}`;
352
+ adoptionCandidates.push(indexedPositionKey);
353
+ /** A single anonymous call may first appear alone at event position
354
+ * #0 and reveal a nonzero provider index only later. Exact indexed
355
+ * position wins above; when it does not exist, one anonymous tally
356
+ * is still an unambiguous fallback. */
357
+ const liveTallies = getLiveGenerationTallies();
358
+ const soleTally = liveTallies.length === 1 ? liveTallies[0] : void 0;
359
+ if (soleTally?.keys?.[0]?.startsWith(`${generationPrefix}#`) === true) {
360
+ const anonymousKey = soleTally.keys[0];
361
+ if (anonymousKey !== indexedPositionKey) adoptionCandidates.push(anonymousKey);
362
+ }
363
+ } else if (chunkId != null) {
364
+ const anonymousTallies = getLiveGenerationTallies().filter((liveTally) => liveTally.keys?.[0]?.startsWith(`${generationPrefix}#`) === true);
365
+ if (anonymousTallies.length === 1) {
366
+ const anonymousKey = anonymousTallies[0].keys?.[0];
367
+ if (anonymousKey != null) adoptionCandidates.push(anonymousKey);
368
+ }
369
+ }
370
+ for (const adoptionKey of adoptionCandidates) {
371
+ if (adoptionKey === key) continue;
372
+ const existing = tallies.get(adoptionKey);
373
+ if (existing == null) continue;
374
+ /** A position entry may only be adopted when the tally was CREATED
375
+ * anonymous (its first key is the position): id-bearing calls also
376
+ * alias their position, and adopting a live parallel call's alias
377
+ * would merge distinct budgets. */
378
+ if (adoptionKey.includes(":#") && existing.keys?.[0] !== adoptionKey) continue;
379
+ tally = existing;
380
+ tallies.set(key, existing);
381
+ if (existing.keys?.includes(adoptionKey) !== true) (existing.keys ??= []).push(adoptionKey);
382
+ if (!existing.keys.includes(key)) existing.keys.push(key);
383
+ break;
384
+ }
385
+ }
386
+ const registerAliasKeys = (target, candidates) => {
387
+ for (const aliasKey of candidates) {
388
+ const currentOwner = tallies.get(aliasKey);
389
+ if (currentOwner === target) continue;
390
+ /** Parallel calls can contend for one batch position, so the newest
391
+ * live call takes the alias over and the previous owner is disowned
392
+ * — its seal must not delete a key it no longer holds. */
393
+ if (currentOwner?.keys != null) {
394
+ const remaining = currentOwner.keys.filter((ownedKey) => ownedKey !== aliasKey);
395
+ currentOwner.keys = remaining.length > 0 ? remaining : void 0;
396
+ }
397
+ tallies.set(aliasKey, target);
398
+ (target.keys ??= []).push(aliasKey);
399
+ }
400
+ };
401
+ const registerAliases = (target) => {
402
+ registerAliasKeys(target, aliasCandidates);
403
+ };
404
+ /** Index-only calls register their batch position ONCE, at creation:
405
+ * later deltas that drop the index then land on the same tally, while
406
+ * the per-delta hot path (indexed id-less deltas) stays free of alias
407
+ * work. */
408
+ const registerCreationPositionAlias = (target) => {
409
+ if (chunkId != null || chunk.index == null || sealed) return;
410
+ if (!positionAliasSafe) return;
411
+ registerAliasKeys(target, [`${generationKey}:#${i}`]);
412
+ };
413
+ const releaseTally = (target) => {
414
+ tallies.delete(key);
415
+ if (target.keys == null) return;
416
+ for (const aliasKey of target.keys) if (tallies.get(aliasKey) === target) tallies.delete(aliasKey);
417
+ };
418
+ if (!hasArgs) {
419
+ if (tally == null) {
420
+ if (!sealed) {
421
+ tally = {
422
+ bytes: 0,
423
+ name: resolveChunkName(chunk),
424
+ keys: [key],
425
+ epoch: graph.breakerEpoch
426
+ };
427
+ tallies.set(key, tally);
428
+ registerAliases(tally);
429
+ registerCreationPositionAlias(tally);
430
+ }
431
+ continue;
432
+ }
433
+ /** A sealing chunk is about to release this tally; taking the position
434
+ * alias here would steal it from a still-live parallel call. */
435
+ if (!sealed) registerAliases(tally);
436
+ if (applyChunkName(tally))
437
+ /** Bytes tallied under the previous (or absent) name were held
438
+ * against that name's limit; a changed name — late arrival or a
439
+ * completed fragment — must re-judge them, including on a sealing
440
+ * chunk about to release the tally. */
441
+ enforceTallyLimit(tally);
442
+ if (sealed) releaseTally(tally);
443
+ continue;
444
+ }
445
+ if (tally == null) {
446
+ tally = {
447
+ bytes: 0,
448
+ keys: [key],
449
+ epoch: graph.breakerEpoch
450
+ };
451
+ tallies.set(key, tally);
452
+ registerCreationPositionAlias(tally);
453
+ }
454
+ if (!sealed) registerAliases(tally);
455
+ applyChunkName(tally);
456
+ const argBytes = Buffer.byteLength(args, "utf8");
457
+ if (sealed) tally.bytes = argBytes;
458
+ else {
459
+ const reconcilesSplitPair = tally.pendingHighSurrogate === true && isLowSurrogate(args.charCodeAt(0));
460
+ tally.bytes += reconcilesSplitPair ? argBytes - 2 : argBytes;
461
+ }
462
+ tally.pendingHighSurrogate = !sealed && isHighSurrogate(args.charCodeAt(args.length - 1));
463
+ const toolName = tally.name ?? resolveChunkName(chunk);
464
+ const limit = byTool != null && toolName != null && Object.hasOwn(byTool, toolName) ? byTool[toolName] : globalLimit;
465
+ if (limit > 0 && tally.bytes > limit) throw new StreamLimitExceededError({
466
+ kind: "tool_call_args",
467
+ limit,
468
+ observed: tally.bytes,
469
+ toolName
470
+ });
471
+ if (sealed) releaseTally(tally);
472
+ }
473
+ }
474
+ /**
475
+ * Standalone byte check for complete parsed tool calls that arrive without a
476
+ * raw chunk representation: a streaming custom or OpenAI-compatible
477
+ * `ChatModel` can yield fully parsed `tool_calls` with empty
478
+ * `tool_call_chunks`, which would otherwise dispatch without consuming any
479
+ * byte budget. Complete calls are self-contained, so each is judged
480
+ * standalone without tallying.
481
+ */
482
+ function enforceCompleteToolCallArgLimit({ graph, metadata, toolCalls }) {
483
+ const resolved = graph.streamLimits ?? DEFAULT_RESOLVED_LIMITS;
484
+ const globalLimit = resolved.maxToolCallArgBytes;
485
+ const byTool = resolved.maxToolCallArgBytesByTool;
486
+ if (!resolved.hasEnforceableToolCallArgLimit) return;
487
+ if (metadata?.["lc_stream_limit_redispatch"] === true) return;
488
+ for (const toolCall of toolCalls) {
489
+ const name = toolCall.name != null && toolCall.name !== "" ? toolCall.name : void 0;
490
+ const limit = byTool != null && name != null && Object.hasOwn(byTool, name) ? byTool[name] : globalLimit;
491
+ if (limit <= 0) continue;
492
+ const args = toolCall.args;
493
+ let serialized;
494
+ if (typeof args === "string") serialized = args;
495
+ else if (args == null) serialized = "";
496
+ else serialized = JSON.stringify(args);
497
+ const bytes = Buffer.byteLength(serialized, "utf8");
498
+ if (bytes > limit) throw new StreamLimitExceededError({
499
+ kind: "tool_call_args",
500
+ limit,
501
+ observed: bytes,
502
+ toolName: name
503
+ });
504
+ }
505
+ }
506
+ /**
507
+ * Whether a chunk needs charge accounting at all. With the event cap off
508
+ * (the default) and no tool payload on the chunk, there is nothing a claim
509
+ * could ever gate — skipping restores the documented zero-cost-disabled
510
+ * behavior by avoiding a WeakMap entry and nested Map per ordinary text
511
+ * delta. Both the producer and consumer paths use this same predicate on
512
+ * the same chunk, so claim pairing is unaffected.
513
+ */
514
+ function requiresStreamLimitAccounting(graph, chunk) {
515
+ const resolved = graph.streamLimits ?? DEFAULT_RESOLVED_LIMITS;
516
+ if (resolved.maxDeltaEventsPerTurn > 0) return true;
517
+ /** With no argument limit able to fire (byte cap disabled and every
518
+ * per-tool entry a zero-valued disable), neither enforcement function can
519
+ * trip — hosts that explicitly disable the guards for legitimate large
520
+ * arguments must not pay per-chunk claim allocations (generation keys,
521
+ * WeakMap entries, nested Maps) for bookkeeping that judges nothing. */
522
+ if (!resolved.hasEnforceableToolCallArgLimit) return false;
523
+ return (chunk.tool_call_chunks?.length ?? 0) > 0 || (chunk.tool_calls?.length ?? 0) > 0 || (chunk.invalid_tool_calls?.length ?? 0) > 0;
524
+ }
525
+ /** True when any stream-limit guard can fire for this graph. The attempt
526
+ * lease and per-chunk claims are both gated on it — fully disabled guards
527
+ * must allocate no bookkeeping at all, per-attempt included. */
528
+ function streamLimitAccountingEnabled(graph) {
529
+ const resolved = graph.streamLimits ?? DEFAULT_RESOLVED_LIMITS;
530
+ return resolved.hasEnforceableToolCallArgLimit || resolved.maxDeltaEventsPerTurn > 0;
531
+ }
532
+ /** Leases a model attempt's generation: entries under it are exempt from
533
+ * the reset sweep until {@link releaseStreamLimitGeneration} runs from the
534
+ * attempt's `finally`. */
535
+ function registerActiveStreamLimitGeneration(graph, generationKey) {
536
+ (graph.activeStreamLimitGenerations ??= /* @__PURE__ */ new Set()).add(generationKey);
537
+ }
538
+ /** Ends an attempt's lease and deletes its accounting entries — the
539
+ * authoritative retirement point for attempt-scoped state. */
540
+ function releaseStreamLimitGeneration(graph, generationKey) {
541
+ graph.activeStreamLimitGenerations?.delete(generationKey);
542
+ deleteGenerationEntries(graph.streamedToolCallArgTallies, generationKey);
543
+ deleteGenerationEntries(graph.streamDeltaEventCounts, generationKey);
544
+ }
545
+ function deleteGenerationEntries(entries, generationKey) {
546
+ if (entries == null) return;
547
+ const prefix = `${generationKey}:`;
548
+ for (const key of entries.keys()) if (key === generationKey || key.startsWith(prefix)) entries.delete(key);
549
+ }
550
+ function isOwnedByActiveGeneration(key, activeGenerations) {
551
+ for (const generationKey of activeGenerations) if (key === generationKey || key.startsWith(`${generationKey}:`)) return true;
552
+ return false;
553
+ }
554
+ /**
555
+ * Deletes accounting entries older than the epoch that is ending, EXCEPT
556
+ * entries leased by a still-active attempt. Called by `resetValues` instead
557
+ * of clearing: producer loops of straggling attempts use the graph's maps
558
+ * directly and are not behind the consumer-only epoch gate, so a clear
559
+ * would hand a cancellation-ignoring provider a fresh allowance at every
560
+ * run start. Leased entries live until their attempt's `finally` releases
561
+ * them; unleased entries (direct callers with no attempt stamp) get one
562
+ * grace reset via their epoch tag.
563
+ */
564
+ function sweepStaleStreamLimitEntries(entries, endingEpoch, activeGenerations) {
565
+ const hasActive = activeGenerations != null && activeGenerations.size > 0;
566
+ for (const [key, value] of entries) {
567
+ if (value.epoch === endingEpoch) continue;
568
+ if (hasActive && isOwnedByActiveGeneration(key, activeGenerations)) continue;
569
+ entries.delete(key);
570
+ }
571
+ }
572
+ /** Links a secondary representation of a wire chunk (e.g. a provider
573
+ * adapter's callback copy) to its canonical emission object, so claim
574
+ * accounting treats both as one emission even though their identities
575
+ * differ. Non-enumerable, so the link stays out of serialization and chunk
576
+ * merges. */
577
+ const STREAM_LIMIT_CANONICAL = Symbol("streamLimitCanonical");
578
+ function linkStreamLimitCanonical(copy, canonical) {
579
+ Object.defineProperty(copy, STREAM_LIMIT_CANONICAL, {
580
+ value: canonical,
581
+ enumerable: false,
582
+ configurable: true
583
+ });
584
+ }
585
+ function canonicalChunk(chunk) {
586
+ const linked = chunk[STREAM_LIMIT_CANONICAL];
587
+ return typeof linked === "object" && linked != null ? linked : chunk;
588
+ }
589
+ /**
590
+ * Claims accounting ownership of one EMISSION of a wire chunk. LangChain can
591
+ * hand the same chunk object to the decoupled `streamEvents` handler
592
+ * (`consumer`) and to the dispatch loop (`producer`) in either order, and a
593
+ * streaming model may mutate and re-yield the same object across emissions —
594
+ * so dedup is a signed credit balance per object rather than a lifetime set.
595
+ * Each producer visit adds a credit, each consumer visit removes one, and a
596
+ * visit charges only when the other side has not already charged that
597
+ * emission (positive balance = producer ahead, negative = consumer ahead).
598
+ * Paths where only one side ever observes the chunk (summarization, local
599
+ * replay-skip) charge every visit, since their balance never crosses zero
600
+ * the other way. Balances are scoped by generation identity so parallel
601
+ * generations sharing one reused chunk object cannot cancel each other's
602
+ * charges. Non-object chunks cannot be identity-tracked and are always
603
+ * claimable.
604
+ */
605
+ function claimStreamLimitCharge(graph, chunk, side, metadata) {
606
+ if (typeof chunk !== "object" || chunk == null) return true;
607
+ const emission = canonicalChunk(chunk);
608
+ const credits = graph.streamLimitChargeCredits ??= /* @__PURE__ */ new WeakMap();
609
+ let byGeneration = credits.get(emission);
610
+ if (byGeneration == null) {
611
+ byGeneration = /* @__PURE__ */ new Map();
612
+ credits.set(emission, byGeneration);
613
+ }
614
+ const generationKey = resolveGenerationKey(metadata);
615
+ const balance = byGeneration.get(generationKey) ?? 0;
616
+ if (side === "producer") {
617
+ byGeneration.set(generationKey, balance + 1);
618
+ return balance >= 0;
619
+ }
620
+ byGeneration.set(generationKey, balance - 1);
621
+ return balance <= 0;
622
+ }
623
+ /**
624
+ * Synchronous producer-side accounting for wire chunks that would otherwise
625
+ * be judged only when the decoupled `streamEvents` reader catches up — or,
626
+ * on replay-skipped and summarization chunks, not at all. A lagging reader
627
+ * would let an oversized complete call return to LangGraph and reach
628
+ * `ToolNode` before the queued handler throws; charging in the producer
629
+ * loop keeps the breaker ahead of graph progression. Claim-based, so
630
+ * whichever of this path and the handler echo sees the chunk object first
631
+ * charges it and the other skips.
632
+ */
633
+ function enforceStreamLimitsForWireChunk({ graph, metadata, chunk, side = "producer" }) {
634
+ if (!requiresStreamLimitAccounting(graph, chunk)) return;
635
+ if (!claimStreamLimitCharge(graph, chunk, side, metadata)) return;
636
+ enforceStreamDeltaEventLimit({
637
+ graph,
638
+ metadata
639
+ });
640
+ /** Combined view computed first so the raw-chunk guard can correlate
641
+ * names from BOTH parsed and invalid calls in the same event; an unnamed
642
+ * raw chunk twinned with a named invalid call must select that tool's
643
+ * override, not the global cap. */
644
+ const completeCalls = combineCompleteToolCalls(chunk);
645
+ if (chunk.tool_call_chunks != null && chunk.tool_call_chunks.length > 0) enforceStreamedToolCallArgLimit({
646
+ graph,
647
+ metadata,
648
+ toolCallChunks: chunk.tool_call_chunks,
649
+ responseMetadata: chunk.response_metadata,
650
+ parsedToolCalls: completeCalls
651
+ });
652
+ /** Judged whenever parsed calls are present, not only when raw chunks are
653
+ * absent: an adapter can pair an empty or partial raw chunk with a
654
+ * complete parsed call, and the standalone check is stateless so the
655
+ * common both-present case is not double-tallied. `invalid_tool_calls`
656
+ * are included — ToolNode processes and promotes them, and a malformed
657
+ * call streaming oversized arguments is the exact pathology this breaker
658
+ * exists to stop. */
659
+ if (completeCalls != null) enforceCompleteToolCallArgLimit({
660
+ graph,
661
+ metadata,
662
+ toolCalls: completeCalls
663
+ });
664
+ }
665
+ /** Combined view of a chunk's parsed and invalid complete calls, avoiding
666
+ * allocation on the common paths where one or both are absent. */
667
+ function combineCompleteToolCalls(chunk) {
668
+ const parsed = chunk.tool_calls;
669
+ const invalid = chunk.invalid_tool_calls;
670
+ const hasParsed = parsed != null && parsed.length > 0;
671
+ const hasInvalid = invalid != null && invalid.length > 0;
672
+ if (hasParsed && hasInvalid) return [...parsed, ...invalid];
673
+ if (hasParsed) return parsed;
674
+ return hasInvalid ? invalid : void 0;
675
+ }
676
+ /**
677
+ * Counts streamed chunk events per model generation and throws once a single
678
+ * generation exceeds `maxDeltaEventsPerTurn`. Opt-in defense in depth for
679
+ * pathologies a byte cap cannot see, such as a provider stream looping on
680
+ * empty chunks. Zero cost while disabled.
681
+ */
682
+ function enforceStreamDeltaEventLimit({ graph, metadata }) {
683
+ const limit = (graph.streamLimits ?? DEFAULT_RESOLVED_LIMITS).maxDeltaEventsPerTurn;
684
+ if (limit <= 0) return;
685
+ if (metadata?.["lc_stream_limit_redispatch"] === true) return;
686
+ const counts = graph.streamDeltaEventCounts ??= /* @__PURE__ */ new Map();
687
+ const key = resolveGenerationKey(metadata);
688
+ const existing = counts.get(key);
689
+ const next = (existing?.count ?? 0) + 1;
690
+ if (next > limit) throw new StreamLimitExceededError({
691
+ kind: "delta_events",
692
+ limit,
693
+ observed: next
694
+ });
695
+ if (existing != null) existing.count = next;
696
+ else counts.set(key, {
697
+ count: next,
698
+ epoch: graph.breakerEpoch
699
+ });
700
+ }
701
+ //#endregion
702
+ export { DEFAULT_MAX_TOOL_CALL_ARG_BYTES, RUN_BREAKER_SCOPE_CONFIG_KEY, STREAM_LIMIT_ATTEMPT_KEY, STREAM_LIMIT_EPOCH_KEY, STREAM_LIMIT_REDISPATCH_KEY, StreamLimitExceededError, claimStreamLimitCharge, combineCompleteToolCalls, enforceCompleteToolCallArgLimit, enforceStreamDeltaEventLimit, enforceStreamLimitsForWireChunk, enforceStreamedToolCallArgLimit, linkStreamLimitCanonical, registerActiveStreamLimitGeneration, releaseStreamLimitGeneration, requiresStreamLimitAccounting, resolveGenerationKey, resolveStreamLimits, streamLimitAccountingEnabled, sweepStaleStreamLimitEntries };
703
+
704
+ //# sourceMappingURL=streamLimits.mjs.map