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