@buoy-gg/agent-core 7.0.35 → 7.0.36

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 (174) hide show
  1. package/README.md +10 -7
  2. package/lib/commonjs/blocks/receipts.js +57 -1
  3. package/lib/commonjs/blocks/receipts.js.map +1 -1
  4. package/lib/commonjs/blocks/types.js +23 -2
  5. package/lib/commonjs/blocks/types.js.map +1 -1
  6. package/lib/commonjs/blocks/uiTool.js +423 -10
  7. package/lib/commonjs/blocks/uiTool.js.map +1 -1
  8. package/lib/commonjs/catalog/catalog.g.js +121 -43
  9. package/lib/commonjs/catalog/catalog.g.js.map +1 -1
  10. package/lib/commonjs/catalog/catalog.source.json +121 -45
  11. package/lib/commonjs/catalog/catalog.types.g.js +44 -0
  12. package/lib/commonjs/catalog/catalog.types.g.js.map +1 -0
  13. package/lib/commonjs/catalog/normalizeParams.js +333 -0
  14. package/lib/commonjs/catalog/normalizeParams.js.map +1 -0
  15. package/lib/commonjs/catalog/signature.js +68 -0
  16. package/lib/commonjs/catalog/signature.js.map +1 -0
  17. package/lib/commonjs/catalog/snapshotReads.js +13 -6
  18. package/lib/commonjs/catalog/snapshotReads.js.map +1 -1
  19. package/lib/commonjs/catalog/toProviderTools.js +41 -5
  20. package/lib/commonjs/catalog/toProviderTools.js.map +1 -1
  21. package/lib/commonjs/context/buildContextPack.js +367 -19
  22. package/lib/commonjs/context/buildContextPack.js.map +1 -1
  23. package/lib/commonjs/effects/digest.js +34 -0
  24. package/lib/commonjs/effects/digest.js.map +1 -0
  25. package/lib/commonjs/effects/ledger.js +85 -1
  26. package/lib/commonjs/effects/ledger.js.map +1 -1
  27. package/lib/commonjs/engine/askGate.js +287 -0
  28. package/lib/commonjs/engine/askGate.js.map +1 -0
  29. package/lib/commonjs/engine/effectFor.js +82 -1
  30. package/lib/commonjs/engine/effectFor.js.map +1 -1
  31. package/lib/commonjs/engine/historyBudget.js +260 -0
  32. package/lib/commonjs/engine/historyBudget.js.map +1 -0
  33. package/lib/commonjs/engine/runAgentTurn.js +924 -83
  34. package/lib/commonjs/engine/runAgentTurn.js.map +1 -1
  35. package/lib/commonjs/engine/systemPrompt.js +72 -8
  36. package/lib/commonjs/engine/systemPrompt.js.map +1 -1
  37. package/lib/commonjs/engine/textToolCalls.js +276 -0
  38. package/lib/commonjs/engine/textToolCalls.js.map +1 -0
  39. package/lib/commonjs/index.js +58 -0
  40. package/lib/commonjs/index.js.map +1 -1
  41. package/lib/commonjs/policy/labels.js +19 -3
  42. package/lib/commonjs/policy/labels.js.map +1 -1
  43. package/lib/commonjs/policy/policy.js +6 -1
  44. package/lib/commonjs/policy/policy.js.map +1 -1
  45. package/lib/commonjs/policy/redact.js +42 -5
  46. package/lib/commonjs/policy/redact.js.map +1 -1
  47. package/lib/commonjs/providers/anthropic.js +331 -208
  48. package/lib/commonjs/providers/anthropic.js.map +1 -1
  49. package/lib/commonjs/providers/openai.js +229 -103
  50. package/lib/commonjs/providers/openai.js.map +1 -1
  51. package/lib/commonjs/providers/sse.js +164 -29
  52. package/lib/commonjs/providers/sse.js.map +1 -1
  53. package/lib/commonjs/providers/streamTimer.js +123 -0
  54. package/lib/commonjs/providers/streamTimer.js.map +1 -0
  55. package/lib/commonjs/providers/transport.js +107 -0
  56. package/lib/commonjs/providers/transport.js.map +1 -0
  57. package/lib/commonjs/providers/xhrStream.js +209 -0
  58. package/lib/commonjs/providers/xhrStream.js.map +1 -0
  59. package/lib/commonjs/session.js +215 -58
  60. package/lib/commonjs/session.js.map +1 -1
  61. package/lib/module/blocks/receipts.js +57 -1
  62. package/lib/module/blocks/receipts.js.map +1 -1
  63. package/lib/module/blocks/types.js +23 -2
  64. package/lib/module/blocks/types.js.map +1 -1
  65. package/lib/module/blocks/uiTool.js +421 -10
  66. package/lib/module/blocks/uiTool.js.map +1 -1
  67. package/lib/module/catalog/catalog.g.js +121 -43
  68. package/lib/module/catalog/catalog.g.js.map +1 -1
  69. package/lib/module/catalog/catalog.source.json +121 -45
  70. package/lib/module/catalog/catalog.types.g.js +40 -0
  71. package/lib/module/catalog/catalog.types.g.js.map +1 -0
  72. package/lib/module/catalog/normalizeParams.js +327 -0
  73. package/lib/module/catalog/normalizeParams.js.map +1 -0
  74. package/lib/module/catalog/signature.js +63 -0
  75. package/lib/module/catalog/signature.js.map +1 -0
  76. package/lib/module/catalog/snapshotReads.js +13 -6
  77. package/lib/module/catalog/snapshotReads.js.map +1 -1
  78. package/lib/module/catalog/toProviderTools.js +41 -5
  79. package/lib/module/catalog/toProviderTools.js.map +1 -1
  80. package/lib/module/context/buildContextPack.js +366 -19
  81. package/lib/module/context/buildContextPack.js.map +1 -1
  82. package/lib/module/effects/digest.js +29 -0
  83. package/lib/module/effects/digest.js.map +1 -0
  84. package/lib/module/effects/ledger.js +85 -1
  85. package/lib/module/effects/ledger.js.map +1 -1
  86. package/lib/module/engine/askGate.js +280 -0
  87. package/lib/module/engine/askGate.js.map +1 -0
  88. package/lib/module/engine/effectFor.js +83 -1
  89. package/lib/module/engine/effectFor.js.map +1 -1
  90. package/lib/module/engine/historyBudget.js +251 -0
  91. package/lib/module/engine/historyBudget.js.map +1 -0
  92. package/lib/module/engine/runAgentTurn.js +920 -83
  93. package/lib/module/engine/runAgentTurn.js.map +1 -1
  94. package/lib/module/engine/systemPrompt.js +71 -8
  95. package/lib/module/engine/systemPrompt.js.map +1 -1
  96. package/lib/module/engine/textToolCalls.js +270 -0
  97. package/lib/module/engine/textToolCalls.js.map +1 -0
  98. package/lib/module/index.js +5 -3
  99. package/lib/module/index.js.map +1 -1
  100. package/lib/module/policy/labels.js +19 -3
  101. package/lib/module/policy/labels.js.map +1 -1
  102. package/lib/module/policy/policy.js +6 -1
  103. package/lib/module/policy/policy.js.map +1 -1
  104. package/lib/module/policy/redact.js +42 -5
  105. package/lib/module/policy/redact.js.map +1 -1
  106. package/lib/module/providers/anthropic.js +332 -209
  107. package/lib/module/providers/anthropic.js.map +1 -1
  108. package/lib/module/providers/openai.js +230 -104
  109. package/lib/module/providers/openai.js.map +1 -1
  110. package/lib/module/providers/sse.js +160 -29
  111. package/lib/module/providers/sse.js.map +1 -1
  112. package/lib/module/providers/streamTimer.js +118 -0
  113. package/lib/module/providers/streamTimer.js.map +1 -0
  114. package/lib/module/providers/transport.js +103 -0
  115. package/lib/module/providers/transport.js.map +1 -0
  116. package/lib/module/providers/xhrStream.js +204 -0
  117. package/lib/module/providers/xhrStream.js.map +1 -0
  118. package/lib/module/session.js +199 -60
  119. package/lib/module/session.js.map +1 -1
  120. package/lib/typescript/blocks/receipts.d.ts +5 -0
  121. package/lib/typescript/blocks/receipts.d.ts.map +1 -1
  122. package/lib/typescript/blocks/types.d.ts +23 -2
  123. package/lib/typescript/blocks/types.d.ts.map +1 -1
  124. package/lib/typescript/blocks/uiTool.d.ts +11 -2
  125. package/lib/typescript/blocks/uiTool.d.ts.map +1 -1
  126. package/lib/typescript/catalog/catalog.g.d.ts.map +1 -1
  127. package/lib/typescript/catalog/catalog.types.g.d.ts +40 -0
  128. package/lib/typescript/catalog/catalog.types.g.d.ts.map +1 -0
  129. package/lib/typescript/catalog/normalizeParams.d.ts +43 -0
  130. package/lib/typescript/catalog/normalizeParams.d.ts.map +1 -0
  131. package/lib/typescript/catalog/signature.d.ts +29 -0
  132. package/lib/typescript/catalog/signature.d.ts.map +1 -0
  133. package/lib/typescript/catalog/snapshotReads.d.ts.map +1 -1
  134. package/lib/typescript/catalog/toProviderTools.d.ts +28 -15
  135. package/lib/typescript/catalog/toProviderTools.d.ts.map +1 -1
  136. package/lib/typescript/context/buildContextPack.d.ts +24 -2
  137. package/lib/typescript/context/buildContextPack.d.ts.map +1 -1
  138. package/lib/typescript/effects/digest.d.ts +10 -0
  139. package/lib/typescript/effects/digest.d.ts.map +1 -0
  140. package/lib/typescript/effects/ledger.d.ts +55 -0
  141. package/lib/typescript/effects/ledger.d.ts.map +1 -1
  142. package/lib/typescript/engine/askGate.d.ts +29 -0
  143. package/lib/typescript/engine/askGate.d.ts.map +1 -0
  144. package/lib/typescript/engine/effectFor.d.ts +6 -1
  145. package/lib/typescript/engine/effectFor.d.ts.map +1 -1
  146. package/lib/typescript/engine/historyBudget.d.ts +92 -0
  147. package/lib/typescript/engine/historyBudget.d.ts.map +1 -0
  148. package/lib/typescript/engine/runAgentTurn.d.ts +83 -1
  149. package/lib/typescript/engine/runAgentTurn.d.ts.map +1 -1
  150. package/lib/typescript/engine/systemPrompt.d.ts +41 -0
  151. package/lib/typescript/engine/systemPrompt.d.ts.map +1 -1
  152. package/lib/typescript/engine/textToolCalls.d.ts +58 -0
  153. package/lib/typescript/engine/textToolCalls.d.ts.map +1 -0
  154. package/lib/typescript/index.d.ts +5 -3
  155. package/lib/typescript/index.d.ts.map +1 -1
  156. package/lib/typescript/policy/labels.d.ts.map +1 -1
  157. package/lib/typescript/policy/policy.d.ts.map +1 -1
  158. package/lib/typescript/policy/redact.d.ts.map +1 -1
  159. package/lib/typescript/providers/anthropic.d.ts.map +1 -1
  160. package/lib/typescript/providers/openai.d.ts +0 -16
  161. package/lib/typescript/providers/openai.d.ts.map +1 -1
  162. package/lib/typescript/providers/sse.d.ts +82 -1
  163. package/lib/typescript/providers/sse.d.ts.map +1 -1
  164. package/lib/typescript/providers/streamTimer.d.ts +59 -0
  165. package/lib/typescript/providers/streamTimer.d.ts.map +1 -0
  166. package/lib/typescript/providers/transport.d.ts +40 -0
  167. package/lib/typescript/providers/transport.d.ts.map +1 -0
  168. package/lib/typescript/providers/types.d.ts +91 -3
  169. package/lib/typescript/providers/types.d.ts.map +1 -1
  170. package/lib/typescript/providers/xhrStream.d.ts +43 -0
  171. package/lib/typescript/providers/xhrStream.d.ts.map +1 -0
  172. package/lib/typescript/session.d.ts +40 -2
  173. package/lib/typescript/session.d.ts.map +1 -1
  174. package/package.json +2 -2
@@ -5,6 +5,8 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.createAnthropicProvider = createAnthropicProvider;
7
7
  var _sse = require("./sse");
8
+ var _streamTimer = require("./streamTimer");
9
+ var _transport = require("./transport");
8
10
  /**
9
11
  * Anthropic Messages protocol.
10
12
  *
@@ -31,6 +33,38 @@ var _sse = require("./sse");
31
33
  */
32
34
 
33
35
  const DEFAULT_VERSION = "2023-06-01";
36
+ /**
37
+ * Mark the last content block of the last message as a cache breakpoint.
38
+ *
39
+ * WHY THERE IS A SECOND BREAKPOINT AT ALL. The first one, at the end of
40
+ * `system`, caches the tools and the system prompt — the big fixed prefix. It
41
+ * does nothing for the messages, which were left uncached on the reasoning
42
+ * that they grow every turn and so would pay the write multiplier for a prefix
43
+ * never read back. That is true of a turn with ONE request. It is false of the
44
+ * turns this tool actually makes: a turn runs 3 to 8 model requests, each
45
+ * re-sending the whole conversation plus everything the previous steps read,
46
+ * and steps 2..N are exactly a re-read of what step 1 already sent.
47
+ *
48
+ * So the breakpoint MOVES: each request marks its own last block, and the next
49
+ * request hits it (Anthropic looks back ~20 blocks for a match). Step 1 writes
50
+ * the messages at 1.25x, every later step reads them at 0.1x and writes only
51
+ * its own delta.
52
+ *
53
+ * What it does NOT buy is cross-TURN caching, because the volatile "right now"
54
+ * block sits at the end of `system` and changes every turn, invalidating
55
+ * anything after it. Within a turn is where the multiplier lives, and that is
56
+ * what this collects.
57
+ */
58
+ function markLastBlock(out) {
59
+ const lastMessage = out[out.length - 1];
60
+ const lastBlock = lastMessage?.content[lastMessage.content.length - 1];
61
+ if (lastBlock) {
62
+ lastBlock.cache_control = {
63
+ type: "ephemeral"
64
+ };
65
+ }
66
+ return out;
67
+ }
34
68
  function toWireMessages(messages) {
35
69
  const out = [];
36
70
  for (const m of messages) {
@@ -80,235 +114,324 @@ function toWireMessages(messages) {
80
114
  return out;
81
115
  }
82
116
  function createAnthropicProvider(config) {
117
+ // One per provider, so the `auto` transport decision is made once and not
118
+ // re-learned (and re-buffered) at the start of every turn.
119
+ const transport = (0, _transport.createTransport)(config);
83
120
  return {
84
121
  protocol: "anthropic",
85
122
  async *send(req) {
86
- const headers = {
87
- "content-type": "application/json",
88
- "anthropic-version": config.anthropicVersion ?? DEFAULT_VERSION,
89
- ...(await (config.headers?.() ?? {}))
123
+ const timer = (0, _streamTimer.createStreamTimer)(req.signal, config);
124
+ const status = {
125
+ truncated: false
90
126
  };
91
- // Dev-only direct mode. A distributed build uses `headers` against the
92
- // org's own gateway and never carries a provider key at all.
93
- if (config.apiKey && !headers["x-api-key"] && !headers.Authorization) {
94
- headers["x-api-key"] = config.apiKey;
95
- // Without this the POST comes back with no access-control-allow-origin
96
- // and a browser/Electron renderer silently discards an otherwise fine
97
- // response — a 200 in the network log and "Failed to fetch" in JS.
98
- headers["anthropic-dangerous-direct-browser-access"] = "true";
99
- }
100
- const response = await fetch(config.endpoint, {
101
- method: "POST",
102
- headers,
103
- signal: req.signal,
104
- body: JSON.stringify({
105
- model: req.model,
106
- max_tokens: req.maxTokens,
107
- stream: true,
108
- // ONE cache breakpoint, at the end of `system`.
109
- //
110
- // Anthropic caches the prefix in a fixed order — tools, then system,
111
- // then messages — up to and including the block carrying
112
- // `cache_control`. So marking the end of the system prompt caches
113
- // the tool definitions with it, which is the point: the tools are
114
- // ~20k tokens and by far the larger half. A second breakpoint on the
115
- // last tool would buy nothing and spend one of the four available.
116
- //
117
- // Messages stay uncached on purpose. They grow every turn, so they
118
- // would invalidate on each request and pay the write multiplier for
119
- // a prefix that is never read back.
120
- system: config.cache === false ? req.system : [{
121
- type: "text",
122
- text: req.system,
123
- cache_control: {
124
- type: "ephemeral"
125
- }
126
- }],
127
- tools: req.tools.map(t => ({
128
- name: t.name,
129
- description: t.description,
130
- input_schema: t.inputSchema
131
- })),
132
- messages: toWireMessages(req.messages),
133
- ...config.requestOverrides
134
- })
135
- });
136
- if (!response.ok) {
137
- yield {
138
- type: "error",
139
- message: await (0, _sse.errorBody)(response)
127
+ try {
128
+ const headers = {
129
+ "content-type": "application/json",
130
+ "anthropic-version": config.anthropicVersion ?? DEFAULT_VERSION,
131
+ ...(await (config.headers?.() ?? {}))
140
132
  };
141
- return;
142
- }
133
+ // Dev-only direct mode. A distributed build uses `headers` against the
134
+ // org's own gateway and never carries a provider key at all.
135
+ if (config.apiKey && !headers["x-api-key"] && !headers.Authorization) {
136
+ headers["x-api-key"] = config.apiKey;
137
+ // Without this the POST comes back with no access-control-allow-origin
138
+ // and a browser/Electron renderer silently discards an otherwise fine
139
+ // response — a 200 in the network log and "Failed to fetch" in JS.
140
+ headers["anthropic-dangerous-direct-browser-access"] = "true";
141
+ }
142
+ const stream = await transport.open({
143
+ headers,
144
+ // The timer's signal, not the caller's: it aborts on the user's Stop
145
+ // AND on either clock running out. See providers/streamTimer.ts.
146
+ signal: timer.signal,
147
+ body: JSON.stringify({
148
+ model: req.model,
149
+ max_tokens: req.maxTokens,
150
+ stream: true,
151
+ // ONE cache breakpoint, at the end of `system`.
152
+ //
153
+ // Anthropic caches the prefix in a fixed order — tools, then system,
154
+ // then messages — up to and including the block carrying
155
+ // `cache_control`. So marking the end of the system prompt caches
156
+ // the tool definitions with it, which is the point: the tools are
157
+ // ~20k tokens and by far the larger half. A second breakpoint on the
158
+ // last tool would buy nothing and spend one of the four available.
159
+ //
160
+ // Messages stay uncached on purpose. They grow every turn, so they
161
+ // would invalidate on each request and pay the write multiplier for
162
+ // a prefix that is never read back.
163
+ //
164
+ // The volatile block (what is on screen right now) goes in a SECOND
165
+ // text block after the breakpoint. It changes every turn; inside the
166
+ // cached block it would invalidate the whole prefix each time.
167
+ system: config.cache === false ? req.systemVolatile ? `${req.system}\n\n---\n\n${req.systemVolatile}` : req.system : [{
168
+ type: "text",
169
+ text: req.system,
170
+ cache_control: {
171
+ type: "ephemeral"
172
+ }
173
+ }, ...(req.systemVolatile ? [{
174
+ type: "text",
175
+ text: req.systemVolatile
176
+ }] : [])],
177
+ tools: req.tools.map(t => ({
178
+ name: t.name,
179
+ description: t.description,
180
+ input_schema: t.inputSchema,
181
+ // Anthropic-only. Each example must validate against input_schema
182
+ // or the API 400s, so these are generated from the catalog rather
183
+ // than written by hand.
184
+ ...(t.inputExamples?.length ? {
185
+ input_examples: t.inputExamples
186
+ } : {})
187
+ })),
188
+ messages: config.cache === false ? toWireMessages(req.messages) : markLastBlock(toWireMessages(req.messages)),
189
+ ...config.requestOverrides
190
+ })
191
+ });
143
192
 
144
- // Partial tool_use args accumulate here, keyed by content-block index.
145
- const pending = new Map();
146
- // Partial thinking blocks, same keying. `thinking_delta` carries the
147
- // text and `signature_delta` the signature, and the signature usually
148
- // arrives in ONE delta right at the end — a reader that stops at
149
- // thinking_delta replays an unsigned block, which the API rejects.
150
- const thinking = new Map();
151
- let stopReason;
152
- let usage;
153
- for await (const ev of (0, _sse.readSse)(response)) {
154
- let payload;
155
- try {
156
- payload = JSON.parse(ev.data);
157
- } catch {
158
- continue;
193
+ // Headers are in: the connect clock stops here and the idle one starts.
194
+ timer.connected();
195
+ if (stream.problem) {
196
+ yield {
197
+ type: "error",
198
+ message: stream.problem,
199
+ kind: "provider"
200
+ };
201
+ return;
159
202
  }
160
- const kind = payload.type ?? ev.event;
161
- switch (kind) {
162
- case "content_block_start":
163
- {
164
- const block = payload.content_block;
165
- if (block?.type === "thinking") {
166
- thinking.set(payload.index, {
167
- thinking: block.thinking ?? "",
168
- signature: block.signature ?? ""
169
- });
170
- } else if (block?.type === "redacted_thinking") {
171
- // No deltas for these — the whole (encrypted) payload is here.
172
- yield {
173
- type: "thinking",
174
- block: {
175
- type: "redacted_thinking",
176
- data: block.data
177
- }
178
- };
179
- } else if (block?.type === "tool_use") {
180
- // NOT block.input — see note 1.
181
- pending.set(payload.index, {
182
- id: block.id,
183
- name: block.name,
184
- json: ""
185
- });
203
+
204
+ // Partial tool_use args accumulate here, keyed by content-block index.
205
+ const pending = new Map();
206
+ // Partial thinking blocks, same keying. `thinking_delta` carries the
207
+ // text and `signature_delta` the signature, and the signature usually
208
+ // arrives in ONE delta right at the end — a reader that stops at
209
+ // thinking_delta replays an unsigned block, which the API rejects.
210
+ const thinking = new Map();
211
+ let stopReason;
212
+ let usage;
213
+ let model;
214
+ /** The protocol's end marker; ends the read. See `sawTerminal` below. */
215
+ let sawStop = false;
216
+ for await (const ev of stream.frames({
217
+ status,
218
+ onFrame: timer.activity,
219
+ signal: timer.signal
220
+ })) {
221
+ let payload;
222
+ try {
223
+ payload = JSON.parse(ev.data);
224
+ } catch {
225
+ continue;
226
+ }
227
+ const kind = payload.type ?? ev.event;
228
+ switch (kind) {
229
+ case "content_block_start":
230
+ {
231
+ const block = payload.content_block;
232
+ if (block?.type === "thinking") {
233
+ thinking.set(payload.index, {
234
+ thinking: block.thinking ?? "",
235
+ signature: block.signature ?? ""
236
+ });
237
+ } else if (block?.type === "redacted_thinking") {
238
+ // No deltas for these — the whole (encrypted) payload is here.
239
+ yield {
240
+ type: "thinking",
241
+ block: {
242
+ type: "redacted_thinking",
243
+ data: block.data
244
+ }
245
+ };
246
+ } else if (block?.type === "tool_use") {
247
+ // NOT block.input — see note 1.
248
+ pending.set(payload.index, {
249
+ id: block.id,
250
+ name: block.name,
251
+ json: ""
252
+ });
253
+ }
254
+ break;
186
255
  }
187
- break;
188
- }
189
- case "content_block_delta":
190
- {
191
- const delta = payload.delta;
192
- if (delta?.type === "text_delta") {
193
- yield {
194
- type: "text",
195
- delta: delta.text
196
- };
197
- } else if (delta?.type === "input_json_delta") {
198
- const slot = pending.get(payload.index);
199
- if (slot) slot.json += delta.partial_json ?? "";
200
- } else if (delta?.type === "thinking_delta") {
201
- const slot = thinking.get(payload.index);
202
- if (slot) slot.thinking += delta.thinking ?? "";
203
- } else if (delta?.type === "signature_delta") {
204
- const slot = thinking.get(payload.index);
205
- if (slot) slot.signature += delta.signature ?? "";
256
+ case "content_block_delta":
257
+ {
258
+ const delta = payload.delta;
259
+ if (delta?.type === "text_delta") {
260
+ yield {
261
+ type: "text",
262
+ delta: delta.text
263
+ };
264
+ } else if (delta?.type === "input_json_delta") {
265
+ const slot = pending.get(payload.index);
266
+ if (slot) slot.json += delta.partial_json ?? "";
267
+ } else if (delta?.type === "thinking_delta") {
268
+ const slot = thinking.get(payload.index);
269
+ if (slot) slot.thinking += delta.thinking ?? "";
270
+ } else if (delta?.type === "signature_delta") {
271
+ const slot = thinking.get(payload.index);
272
+ if (slot) slot.signature += delta.signature ?? "";
273
+ }
274
+ break;
206
275
  }
207
- break;
208
- }
209
- case "content_block_stop":
210
- {
211
- const thought = thinking.get(payload.index);
212
- if (thought) {
213
- thinking.delete(payload.index);
214
- yield {
215
- type: "thinking",
216
- block: {
276
+ case "content_block_stop":
277
+ {
278
+ const thought = thinking.get(payload.index);
279
+ if (thought) {
280
+ thinking.delete(payload.index);
281
+ yield {
217
282
  type: "thinking",
218
- ...thought
283
+ block: {
284
+ type: "thinking",
285
+ ...thought
286
+ }
287
+ };
288
+ }
289
+ const slot = pending.get(payload.index);
290
+ if (slot) {
291
+ pending.delete(payload.index);
292
+ let input = {};
293
+ try {
294
+ input = slot.json ? JSON.parse(slot.json) : {};
295
+ } catch {
296
+ yield {
297
+ type: "error",
298
+ kind: "provider",
299
+ // Same evidence as the OpenAI path. The stop reason is not
300
+ // known yet here — it rides on `message_delta`, which comes
301
+ // after this block closes — so only the raw tail is carried.
302
+ message: (0, _sse.unparseableMessage)(slot.name, slot.json)
303
+ };
304
+ break;
219
305
  }
220
- };
221
- }
222
- const slot = pending.get(payload.index);
223
- if (slot) {
224
- pending.delete(payload.index);
225
- let input = {};
226
- try {
227
- input = slot.json ? JSON.parse(slot.json) : {};
228
- } catch {
306
+ const call = {
307
+ id: slot.id,
308
+ name: slot.name,
309
+ input
310
+ };
229
311
  yield {
230
- type: "error",
231
- message: `Model sent unparseable arguments for ${slot.name}`
312
+ type: "tool-call",
313
+ call
232
314
  };
233
- break;
234
315
  }
235
- const call = {
236
- id: slot.id,
237
- name: slot.name,
238
- input
239
- };
240
- yield {
241
- type: "tool-call",
242
- call
243
- };
316
+ break;
244
317
  }
245
- break;
246
- }
247
- case "message_start":
248
- {
249
- // The cache counters arrive HERE, not on message_delta — the
250
- // prefix is accounted for before a single token is generated. A
251
- // reader that only watches message_delta sees cacheRead 0 on
252
- // every request and concludes caching is not working.
253
- const m = payload.message;
254
- const u = m?.usage;
255
- if (u) {
256
- usage = {
257
- input: u.input_tokens ?? 0,
258
- output: u.output_tokens ?? 0,
259
- ...(u.cache_read_input_tokens !== undefined ? {
260
- cacheRead: u.cache_read_input_tokens
261
- } : {}),
262
- ...(u.cache_creation_input_tokens !== undefined ? {
263
- cacheWrite: u.cache_creation_input_tokens
264
- } : {})
265
- };
318
+ case "message_start":
319
+ {
320
+ // The cache counters arrive HERE, not on message_delta — the
321
+ // prefix is accounted for before a single token is generated. A
322
+ // reader that only watches message_delta sees cacheRead 0 on
323
+ // every request and concludes caching is not working.
324
+ const m = payload.message;
325
+ // The served model id is named here and nowhere else — a gateway
326
+ // resolves an alias to a dated snapshot, and this is that id.
327
+ if (typeof m?.model === "string" && m.model) model = m.model;
328
+ const u = m?.usage;
329
+ if (u) {
330
+ usage = {
331
+ input: u.input_tokens ?? 0,
332
+ output: u.output_tokens ?? 0,
333
+ ...(u.cache_read_input_tokens !== undefined ? {
334
+ cacheRead: u.cache_read_input_tokens
335
+ } : {}),
336
+ ...(u.cache_creation_input_tokens !== undefined ? {
337
+ cacheWrite: u.cache_creation_input_tokens
338
+ } : {})
339
+ };
340
+ }
341
+ break;
266
342
  }
267
- break;
268
- }
269
- case "message_delta":
270
- {
271
- const delta = payload.delta;
272
- if (delta?.stop_reason) stopReason = delta.stop_reason;
273
- // Cumulative, not incremental — assign, never add. Output tokens
274
- // are only final here, so keep whatever message_start reported
275
- // about the cache rather than replacing the whole record.
276
- const u = payload.usage;
277
- if (u) {
278
- usage = {
279
- ...usage,
280
- input: u.input_tokens ?? usage?.input ?? 0,
281
- output: u.output_tokens ?? 0
343
+ case "message_delta":
344
+ {
345
+ const delta = payload.delta;
346
+ // `stop_reason` is the model's own statement that it finished,
347
+ // and counts as terminal on its own (see `sawTerminal`). It does
348
+ // NOT end the read: `message_stop` is one frame away and there is
349
+ // no reason to cancel a stream that is about to close itself.
350
+ if (delta?.stop_reason) stopReason = delta.stop_reason;
351
+ // Cumulative, not incremental — assign, never add. Output tokens
352
+ // are only final here, so keep whatever message_start reported
353
+ // about the cache rather than replacing the whole record.
354
+ const u = payload.usage;
355
+ if (u) {
356
+ usage = {
357
+ ...usage,
358
+ input: u.input_tokens ?? usage?.input ?? 0,
359
+ output: u.output_tokens ?? 0
360
+ };
361
+ }
362
+ break;
363
+ }
364
+ case "error":
365
+ {
366
+ const err = payload.error;
367
+ yield {
368
+ type: "error",
369
+ message: err?.message ?? "Provider error",
370
+ kind: "provider"
282
371
  };
372
+ return;
283
373
  }
374
+ case "message_stop":
375
+ // The end. There is no [DONE] — see note 2.
376
+ sawStop = true;
284
377
  break;
285
- }
286
- case "error":
287
- {
288
- const err = payload.error;
289
- yield {
290
- type: "error",
291
- message: err?.message ?? "Provider error"
292
- };
293
- return;
294
- }
295
- case "message_stop":
296
- // The end. There is no [DONE] — see note 2.
297
- yield {
298
- type: "done",
299
- stopReason,
300
- usage
301
- };
302
- return;
303
- default:
304
- break;
378
+ default:
379
+ break;
380
+ }
381
+ // `break` inside the switch leaves the switch, not the loop.
382
+ if (sawStop) break;
383
+ }
384
+
385
+ /**
386
+ * Did the provider actually say it was finished?
387
+ *
388
+ * `message_stop` is the protocol's end marker. `stop_reason` counts
389
+ * too: it is the model's own statement of why it stopped, which is the
390
+ * stronger claim — `message_stop` only says the SSE stream closed. A
391
+ * proxy that forwards the delta and drops the final frame has still
392
+ * delivered a whole answer, and refusing it would break working
393
+ * gateways for no safety gain. NEITHER arriving is the case that
394
+ * matters: bare EOF, which used to be read as success.
395
+ */
396
+ const sawTerminal = sawStop || stopReason !== undefined;
397
+ if (!sawTerminal) {
398
+ // Bare EOF. Whatever text arrived is real and the engine will show it,
399
+ // but the plan behind it is half-written: a connection cut after
400
+ // `{"key":"cart","value":[]}` leaves valid JSON that reads exactly like
401
+ // a finished thought. Nothing here is admitted for execution.
402
+ yield {
403
+ type: "error",
404
+ message: (0, _sse.truncatedMessage)(status.truncated),
405
+ kind: "truncated"
406
+ };
407
+ return;
305
408
  }
409
+ yield {
410
+ type: "done",
411
+ outcome: stopReason === "max_tokens" ? "output-limited" : "completed",
412
+ stopReason,
413
+ usage,
414
+ model
415
+ };
416
+ } catch (err) {
417
+ // The user's Stop wins the label even if a clock fired in the same
418
+ // tick: they know why the turn ended, and "timed out" would be a lie.
419
+ if (req.signal?.aborted) throw err;
420
+ const message = timer.message();
421
+ if (message) {
422
+ yield {
423
+ type: "error",
424
+ message,
425
+ kind: "timeout"
426
+ };
427
+ return;
428
+ }
429
+ // A genuine transport failure — DNS, refused connection, TLS. It has
430
+ // always propagated to the failed-turn card, and still does.
431
+ throw err;
432
+ } finally {
433
+ timer.dispose();
306
434
  }
307
- yield {
308
- type: "done",
309
- stopReason,
310
- usage
311
- };
312
435
  }
313
436
  };
314
437
  }
@@ -1 +1 @@
1
- {"version":3,"names":["_sse","require","DEFAULT_VERSION","toWireMessages","messages","out","m","role","push","content","type","text","t","thinking","c","toolCalls","id","name","input","length","results","map","r","tool_use_id","toolCallId","isError","is_error","createAnthropicProvider","config","protocol","send","req","headers","anthropicVersion","apiKey","Authorization","response","fetch","endpoint","method","signal","body","JSON","stringify","model","max_tokens","maxTokens","stream","system","cache","cache_control","tools","description","input_schema","inputSchema","requestOverrides","ok","message","errorBody","pending","Map","stopReason","usage","ev","readSse","payload","parse","data","kind","event","block","content_block","set","index","signature","json","delta","slot","get","partial_json","thought","delete","call","u","input_tokens","output","output_tokens","cache_read_input_tokens","undefined","cacheRead","cache_creation_input_tokens","cacheWrite","stop_reason","err","error"],"sourceRoot":"../../../src","sources":["providers/anthropic.ts"],"mappings":";;;;;;AAkCA,IAAAA,IAAA,GAAAC,OAAA;AAlCA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAa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AAO,CAAC6B,KAA4C;cAChE,MAAM;gBACJpF,IAAI,EAAE,OAAO;gBACb+C,OAAO,EAAGoC,GAAG,EAAEpC,OAAO,IAAe;cACvC,CAAC;cACD;YACF;UACA,KAAK,cAAc;YACjB;YACA,MAAM;cAAE/C,IAAI,EAAE,MAAM;cAAEmD,UAAU;cAAEC;YAAM,CAAC;YACzC;UACF;YACE;QACJ;MACF;MAEA,MAAM;QAAEpD,IAAI,EAAE,MAAM;QAAEmD,UAAU;QAAEC;MAAM,CAAC;IAC3C;EACF,CAAC;AACH","ignoreList":[]}
1
+ {"version":3,"names":["_sse","require","_streamTimer","_transport","DEFAULT_VERSION","markLastBlock","out","lastMessage","length","lastBlock","content","cache_control","type","toWireMessages","messages","m","role","push","text","t","thinking","c","toolCalls","id","name","input","results","map","r","tool_use_id","toolCallId","isError","is_error","createAnthropicProvider","config","transport","createTransport","protocol","send","req","timer","createStreamTimer","signal","status","truncated","headers","anthropicVersion","apiKey","Authorization","stream","open","body","JSON","stringify","model","max_tokens","maxTokens","system","cache","systemVolatile","tools","description","input_schema","inputSchema","inputExamples","input_examples","requestOverrides","connected","problem","message","kind","pending","Map","stopReason","usage","sawStop","ev","frames","onFrame","activity","payload","parse","data","event","block","content_block","set","index","signature","json","delta","slot","get","partial_json","thought","delete","unparseableMessage","call","u","input_tokens","output","output_tokens","cache_read_input_tokens","undefined","cacheRead","cache_creation_input_tokens","cacheWrite","stop_reason","err","error","sawTerminal","truncatedMessage","outcome","aborted","dispose"],"sourceRoot":"../../../src","sources":["providers/anthropic.ts"],"mappings":";;;;;;AAkCA,IAAAA,IAAA,GAAAC,OAAA;AACA,IAAAC,YAAA,GAAAD,OAAA;AACA,IAAAE,UAAA,GAAAF,OAAA;AApCA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAeA,MAAMG,eAAe,GAAG,YAAY;AAQpC;A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