@buoy-gg/agent-core 7.0.34 → 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 (181) hide show
  1. package/README.md +10 -7
  2. package/lib/commonjs/blocks/receipts.js +83 -24
  3. package/lib/commonjs/blocks/receipts.js.map +1 -1
  4. package/lib/commonjs/blocks/runId.js +31 -0
  5. package/lib/commonjs/blocks/runId.js.map +1 -0
  6. package/lib/commonjs/blocks/types.js +23 -2
  7. package/lib/commonjs/blocks/types.js.map +1 -1
  8. package/lib/commonjs/blocks/uiTool.js +426 -12
  9. package/lib/commonjs/blocks/uiTool.js.map +1 -1
  10. package/lib/commonjs/catalog/catalog.g.js +121 -43
  11. package/lib/commonjs/catalog/catalog.g.js.map +1 -1
  12. package/lib/commonjs/catalog/catalog.source.json +121 -45
  13. package/lib/commonjs/catalog/catalog.types.g.js +44 -0
  14. package/lib/commonjs/catalog/catalog.types.g.js.map +1 -0
  15. package/lib/commonjs/catalog/normalizeParams.js +333 -0
  16. package/lib/commonjs/catalog/normalizeParams.js.map +1 -0
  17. package/lib/commonjs/catalog/signature.js +68 -0
  18. package/lib/commonjs/catalog/signature.js.map +1 -0
  19. package/lib/commonjs/catalog/snapshotReads.js +13 -6
  20. package/lib/commonjs/catalog/snapshotReads.js.map +1 -1
  21. package/lib/commonjs/catalog/toProviderTools.js +41 -5
  22. package/lib/commonjs/catalog/toProviderTools.js.map +1 -1
  23. package/lib/commonjs/context/buildContextPack.js +367 -19
  24. package/lib/commonjs/context/buildContextPack.js.map +1 -1
  25. package/lib/commonjs/effects/digest.js +34 -0
  26. package/lib/commonjs/effects/digest.js.map +1 -0
  27. package/lib/commonjs/effects/ledger.js +85 -1
  28. package/lib/commonjs/effects/ledger.js.map +1 -1
  29. package/lib/commonjs/engine/askGate.js +287 -0
  30. package/lib/commonjs/engine/askGate.js.map +1 -0
  31. package/lib/commonjs/engine/effectFor.js +82 -1
  32. package/lib/commonjs/engine/effectFor.js.map +1 -1
  33. package/lib/commonjs/engine/historyBudget.js +260 -0
  34. package/lib/commonjs/engine/historyBudget.js.map +1 -0
  35. package/lib/commonjs/engine/runAgentTurn.js +958 -88
  36. package/lib/commonjs/engine/runAgentTurn.js.map +1 -1
  37. package/lib/commonjs/engine/systemPrompt.js +72 -8
  38. package/lib/commonjs/engine/systemPrompt.js.map +1 -1
  39. package/lib/commonjs/engine/textToolCalls.js +276 -0
  40. package/lib/commonjs/engine/textToolCalls.js.map +1 -0
  41. package/lib/commonjs/index.js +81 -1
  42. package/lib/commonjs/index.js.map +1 -1
  43. package/lib/commonjs/policy/labels.js +19 -3
  44. package/lib/commonjs/policy/labels.js.map +1 -1
  45. package/lib/commonjs/policy/policy.js +6 -1
  46. package/lib/commonjs/policy/policy.js.map +1 -1
  47. package/lib/commonjs/policy/redact.js +42 -5
  48. package/lib/commonjs/policy/redact.js.map +1 -1
  49. package/lib/commonjs/providers/anthropic.js +340 -174
  50. package/lib/commonjs/providers/anthropic.js.map +1 -1
  51. package/lib/commonjs/providers/openai.js +229 -103
  52. package/lib/commonjs/providers/openai.js.map +1 -1
  53. package/lib/commonjs/providers/sse.js +164 -29
  54. package/lib/commonjs/providers/sse.js.map +1 -1
  55. package/lib/commonjs/providers/streamTimer.js +123 -0
  56. package/lib/commonjs/providers/streamTimer.js.map +1 -0
  57. package/lib/commonjs/providers/transport.js +107 -0
  58. package/lib/commonjs/providers/transport.js.map +1 -0
  59. package/lib/commonjs/providers/xhrStream.js +209 -0
  60. package/lib/commonjs/providers/xhrStream.js.map +1 -0
  61. package/lib/commonjs/session.js +245 -52
  62. package/lib/commonjs/session.js.map +1 -1
  63. package/lib/module/blocks/receipts.js +83 -24
  64. package/lib/module/blocks/receipts.js.map +1 -1
  65. package/lib/module/blocks/runId.js +26 -0
  66. package/lib/module/blocks/runId.js.map +1 -0
  67. package/lib/module/blocks/types.js +23 -2
  68. package/lib/module/blocks/types.js.map +1 -1
  69. package/lib/module/blocks/uiTool.js +424 -12
  70. package/lib/module/blocks/uiTool.js.map +1 -1
  71. package/lib/module/catalog/catalog.g.js +121 -43
  72. package/lib/module/catalog/catalog.g.js.map +1 -1
  73. package/lib/module/catalog/catalog.source.json +121 -45
  74. package/lib/module/catalog/catalog.types.g.js +40 -0
  75. package/lib/module/catalog/catalog.types.g.js.map +1 -0
  76. package/lib/module/catalog/normalizeParams.js +327 -0
  77. package/lib/module/catalog/normalizeParams.js.map +1 -0
  78. package/lib/module/catalog/signature.js +63 -0
  79. package/lib/module/catalog/signature.js.map +1 -0
  80. package/lib/module/catalog/snapshotReads.js +13 -6
  81. package/lib/module/catalog/snapshotReads.js.map +1 -1
  82. package/lib/module/catalog/toProviderTools.js +41 -5
  83. package/lib/module/catalog/toProviderTools.js.map +1 -1
  84. package/lib/module/context/buildContextPack.js +366 -19
  85. package/lib/module/context/buildContextPack.js.map +1 -1
  86. package/lib/module/effects/digest.js +29 -0
  87. package/lib/module/effects/digest.js.map +1 -0
  88. package/lib/module/effects/ledger.js +85 -1
  89. package/lib/module/effects/ledger.js.map +1 -1
  90. package/lib/module/engine/askGate.js +280 -0
  91. package/lib/module/engine/askGate.js.map +1 -0
  92. package/lib/module/engine/effectFor.js +83 -1
  93. package/lib/module/engine/effectFor.js.map +1 -1
  94. package/lib/module/engine/historyBudget.js +251 -0
  95. package/lib/module/engine/historyBudget.js.map +1 -0
  96. package/lib/module/engine/runAgentTurn.js +954 -88
  97. package/lib/module/engine/runAgentTurn.js.map +1 -1
  98. package/lib/module/engine/systemPrompt.js +71 -8
  99. package/lib/module/engine/systemPrompt.js.map +1 -1
  100. package/lib/module/engine/textToolCalls.js +270 -0
  101. package/lib/module/engine/textToolCalls.js.map +1 -0
  102. package/lib/module/index.js +7 -4
  103. package/lib/module/index.js.map +1 -1
  104. package/lib/module/policy/labels.js +19 -3
  105. package/lib/module/policy/labels.js.map +1 -1
  106. package/lib/module/policy/policy.js +6 -1
  107. package/lib/module/policy/policy.js.map +1 -1
  108. package/lib/module/policy/redact.js +42 -5
  109. package/lib/module/policy/redact.js.map +1 -1
  110. package/lib/module/providers/anthropic.js +341 -175
  111. package/lib/module/providers/anthropic.js.map +1 -1
  112. package/lib/module/providers/openai.js +230 -104
  113. package/lib/module/providers/openai.js.map +1 -1
  114. package/lib/module/providers/sse.js +160 -29
  115. package/lib/module/providers/sse.js.map +1 -1
  116. package/lib/module/providers/streamTimer.js +118 -0
  117. package/lib/module/providers/streamTimer.js.map +1 -0
  118. package/lib/module/providers/transport.js +103 -0
  119. package/lib/module/providers/transport.js.map +1 -0
  120. package/lib/module/providers/xhrStream.js +204 -0
  121. package/lib/module/providers/xhrStream.js.map +1 -0
  122. package/lib/module/session.js +228 -54
  123. package/lib/module/session.js.map +1 -1
  124. package/lib/typescript/blocks/receipts.d.ts +5 -10
  125. package/lib/typescript/blocks/receipts.d.ts.map +1 -1
  126. package/lib/typescript/blocks/runId.d.ts +20 -0
  127. package/lib/typescript/blocks/runId.d.ts.map +1 -0
  128. package/lib/typescript/blocks/types.d.ts +23 -2
  129. package/lib/typescript/blocks/types.d.ts.map +1 -1
  130. package/lib/typescript/blocks/uiTool.d.ts +11 -2
  131. package/lib/typescript/blocks/uiTool.d.ts.map +1 -1
  132. package/lib/typescript/catalog/catalog.g.d.ts.map +1 -1
  133. package/lib/typescript/catalog/catalog.types.g.d.ts +40 -0
  134. package/lib/typescript/catalog/catalog.types.g.d.ts.map +1 -0
  135. package/lib/typescript/catalog/normalizeParams.d.ts +43 -0
  136. package/lib/typescript/catalog/normalizeParams.d.ts.map +1 -0
  137. package/lib/typescript/catalog/signature.d.ts +29 -0
  138. package/lib/typescript/catalog/signature.d.ts.map +1 -0
  139. package/lib/typescript/catalog/snapshotReads.d.ts.map +1 -1
  140. package/lib/typescript/catalog/toProviderTools.d.ts +28 -15
  141. package/lib/typescript/catalog/toProviderTools.d.ts.map +1 -1
  142. package/lib/typescript/context/buildContextPack.d.ts +24 -2
  143. package/lib/typescript/context/buildContextPack.d.ts.map +1 -1
  144. package/lib/typescript/effects/digest.d.ts +10 -0
  145. package/lib/typescript/effects/digest.d.ts.map +1 -0
  146. package/lib/typescript/effects/ledger.d.ts +55 -0
  147. package/lib/typescript/effects/ledger.d.ts.map +1 -1
  148. package/lib/typescript/engine/askGate.d.ts +29 -0
  149. package/lib/typescript/engine/askGate.d.ts.map +1 -0
  150. package/lib/typescript/engine/effectFor.d.ts +6 -1
  151. package/lib/typescript/engine/effectFor.d.ts.map +1 -1
  152. package/lib/typescript/engine/historyBudget.d.ts +92 -0
  153. package/lib/typescript/engine/historyBudget.d.ts.map +1 -0
  154. package/lib/typescript/engine/runAgentTurn.d.ts +115 -1
  155. package/lib/typescript/engine/runAgentTurn.d.ts.map +1 -1
  156. package/lib/typescript/engine/systemPrompt.d.ts +41 -0
  157. package/lib/typescript/engine/systemPrompt.d.ts.map +1 -1
  158. package/lib/typescript/engine/textToolCalls.d.ts +58 -0
  159. package/lib/typescript/engine/textToolCalls.d.ts.map +1 -0
  160. package/lib/typescript/index.d.ts +8 -5
  161. package/lib/typescript/index.d.ts.map +1 -1
  162. package/lib/typescript/policy/labels.d.ts.map +1 -1
  163. package/lib/typescript/policy/policy.d.ts.map +1 -1
  164. package/lib/typescript/policy/redact.d.ts.map +1 -1
  165. package/lib/typescript/providers/anthropic.d.ts +5 -0
  166. package/lib/typescript/providers/anthropic.d.ts.map +1 -1
  167. package/lib/typescript/providers/openai.d.ts +0 -16
  168. package/lib/typescript/providers/openai.d.ts.map +1 -1
  169. package/lib/typescript/providers/sse.d.ts +82 -1
  170. package/lib/typescript/providers/sse.d.ts.map +1 -1
  171. package/lib/typescript/providers/streamTimer.d.ts +59 -0
  172. package/lib/typescript/providers/streamTimer.d.ts.map +1 -0
  173. package/lib/typescript/providers/transport.d.ts +40 -0
  174. package/lib/typescript/providers/transport.d.ts.map +1 -0
  175. package/lib/typescript/providers/types.d.ts +116 -3
  176. package/lib/typescript/providers/types.d.ts.map +1 -1
  177. package/lib/typescript/providers/xhrStream.d.ts +43 -0
  178. package/lib/typescript/providers/xhrStream.d.ts.map +1 -0
  179. package/lib/typescript/session.d.ts +63 -2
  180. package/lib/typescript/session.d.ts.map +1 -1
  181. package/package.json +2 -2
@@ -18,10 +18,49 @@
18
18
  * user content array with any text after, and that user message must
19
19
  * immediately follow the assistant turn. "Here are the results:" placed
20
20
  * before the blocks is an instant 400.
21
+ * 5. Every current Claude model thinks by DEFAULT, and a `thinking` block must
22
+ * be replayed VERBATIM (signature and all) at the FRONT of the assistant
23
+ * turn it came from whenever that turn's tool results are sent back. An
24
+ * adapter that keeps only text and tool_use 400s on the second request of
25
+ * every tool-using turn — which is every turn Ask Buoy actually makes.
21
26
  */
22
27
 
23
- import { errorBody, readSse } from "./sse";
28
+ import { truncatedMessage, unparseableMessage } from "./sse";
29
+ import { createStreamTimer } from "./streamTimer";
30
+ import { createTransport } from "./transport";
24
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
+ }
25
64
  function toWireMessages(messages) {
26
65
  const out = [];
27
66
  for (const m of messages) {
@@ -35,6 +74,8 @@ function toWireMessages(messages) {
35
74
  });
36
75
  } else if (m.role === "assistant") {
37
76
  const content = [];
77
+ // Thinking FIRST — see note 5. Signatures are opaque; never edit them.
78
+ for (const t of m.thinking ?? []) content.push(t);
38
79
  if (m.text) content.push({
39
80
  type: "text",
40
81
  text: m.text
@@ -69,199 +110,324 @@ function toWireMessages(messages) {
69
110
  return out;
70
111
  }
71
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);
72
116
  return {
73
117
  protocol: "anthropic",
74
118
  async *send(req) {
75
- const headers = {
76
- "content-type": "application/json",
77
- "anthropic-version": config.anthropicVersion ?? DEFAULT_VERSION,
78
- ...(await (config.headers?.() ?? {}))
119
+ const timer = createStreamTimer(req.signal, config);
120
+ const status = {
121
+ truncated: false
79
122
  };
80
- // Dev-only direct mode. A distributed build uses `headers` against the
81
- // org's own gateway and never carries a provider key at all.
82
- if (config.apiKey && !headers["x-api-key"] && !headers.Authorization) {
83
- headers["x-api-key"] = config.apiKey;
84
- // Without this the POST comes back with no access-control-allow-origin
85
- // and a browser/Electron renderer silently discards an otherwise fine
86
- // response — a 200 in the network log and "Failed to fetch" in JS.
87
- headers["anthropic-dangerous-direct-browser-access"] = "true";
88
- }
89
- const response = await fetch(config.endpoint, {
90
- method: "POST",
91
- headers,
92
- signal: req.signal,
93
- body: JSON.stringify({
94
- model: req.model,
95
- max_tokens: req.maxTokens,
96
- stream: true,
97
- // ONE cache breakpoint, at the end of `system`.
98
- //
99
- // Anthropic caches the prefix in a fixed order — tools, then system,
100
- // then messages — up to and including the block carrying
101
- // `cache_control`. So marking the end of the system prompt caches
102
- // the tool definitions with it, which is the point: the tools are
103
- // ~20k tokens and by far the larger half. A second breakpoint on the
104
- // last tool would buy nothing and spend one of the four available.
105
- //
106
- // Messages stay uncached on purpose. They grow every turn, so they
107
- // would invalidate on each request and pay the write multiplier for
108
- // a prefix that is never read back.
109
- system: config.cache === false ? req.system : [{
110
- type: "text",
111
- text: req.system,
112
- cache_control: {
113
- type: "ephemeral"
114
- }
115
- }],
116
- tools: req.tools.map(t => ({
117
- name: t.name,
118
- description: t.description,
119
- input_schema: t.inputSchema
120
- })),
121
- messages: toWireMessages(req.messages),
122
- ...config.requestOverrides
123
- })
124
- });
125
- if (!response.ok) {
126
- yield {
127
- type: "error",
128
- 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?.() ?? {}))
129
128
  };
130
- return;
131
- }
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
+ });
132
188
 
133
- // Partial tool_use args accumulate here, keyed by content-block index.
134
- const pending = new Map();
135
- let stopReason;
136
- let usage;
137
- for await (const ev of readSse(response)) {
138
- let payload;
139
- try {
140
- payload = JSON.parse(ev.data);
141
- } catch {
142
- 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;
143
198
  }
144
- const kind = payload.type ?? ev.event;
145
- switch (kind) {
146
- case "content_block_start":
147
- {
148
- const block = payload.content_block;
149
- if (block?.type === "tool_use") {
150
- // NOT block.input — see note 1.
151
- pending.set(payload.index, {
152
- id: block.id,
153
- name: block.name,
154
- json: ""
155
- });
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;
156
251
  }
157
- break;
158
- }
159
- case "content_block_delta":
160
- {
161
- const delta = payload.delta;
162
- if (delta?.type === "text_delta") {
163
- yield {
164
- type: "text",
165
- delta: delta.text
166
- };
167
- } else if (delta?.type === "input_json_delta") {
168
- const slot = pending.get(payload.index);
169
- if (slot) slot.json += delta.partial_json ?? "";
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;
170
271
  }
171
- break;
172
- }
173
- case "content_block_stop":
174
- {
175
- const slot = pending.get(payload.index);
176
- if (slot) {
177
- pending.delete(payload.index);
178
- let input = {};
179
- try {
180
- input = slot.json ? JSON.parse(slot.json) : {};
181
- } catch {
272
+ case "content_block_stop":
273
+ {
274
+ const thought = thinking.get(payload.index);
275
+ if (thought) {
276
+ thinking.delete(payload.index);
182
277
  yield {
183
- type: "error",
184
- message: `Model sent unparseable arguments for ${slot.name}`
278
+ type: "thinking",
279
+ block: {
280
+ type: "thinking",
281
+ ...thought
282
+ }
185
283
  };
186
- break;
187
284
  }
188
- const call = {
189
- id: slot.id,
190
- name: slot.name,
191
- input
192
- };
193
- yield {
194
- type: "tool-call",
195
- call
196
- };
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;
301
+ }
302
+ const call = {
303
+ id: slot.id,
304
+ name: slot.name,
305
+ input
306
+ };
307
+ yield {
308
+ type: "tool-call",
309
+ call
310
+ };
311
+ }
312
+ break;
197
313
  }
198
- break;
199
- }
200
- case "message_start":
201
- {
202
- // The cache counters arrive HERE, not on message_delta — the
203
- // prefix is accounted for before a single token is generated. A
204
- // reader that only watches message_delta sees cacheRead 0 on
205
- // every request and concludes caching is not working.
206
- const m = payload.message;
207
- const u = m?.usage;
208
- if (u) {
209
- usage = {
210
- input: u.input_tokens ?? 0,
211
- output: u.output_tokens ?? 0,
212
- ...(u.cache_read_input_tokens !== undefined ? {
213
- cacheRead: u.cache_read_input_tokens
214
- } : {}),
215
- ...(u.cache_creation_input_tokens !== undefined ? {
216
- cacheWrite: u.cache_creation_input_tokens
217
- } : {})
218
- };
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;
219
338
  }
220
- break;
221
- }
222
- case "message_delta":
223
- {
224
- const delta = payload.delta;
225
- if (delta?.stop_reason) stopReason = delta.stop_reason;
226
- // Cumulative, not incremental — assign, never add. Output tokens
227
- // are only final here, so keep whatever message_start reported
228
- // about the cache rather than replacing the whole record.
229
- const u = payload.usage;
230
- if (u) {
231
- usage = {
232
- ...usage,
233
- input: u.input_tokens ?? usage?.input ?? 0,
234
- 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"
235
367
  };
368
+ return;
236
369
  }
370
+ case "message_stop":
371
+ // The end. There is no [DONE] — see note 2.
372
+ sawStop = true;
373
+ break;
374
+ default:
237
375
  break;
238
- }
239
- case "error":
240
- {
241
- const err = payload.error;
242
- yield {
243
- type: "error",
244
- message: err?.message ?? "Provider error"
245
- };
246
- return;
247
- }
248
- case "message_stop":
249
- // The end. There is no [DONE] — see note 2.
250
- yield {
251
- type: "done",
252
- stopReason,
253
- usage
254
- };
255
- return;
256
- default:
257
- break;
376
+ }
377
+ // `break` inside the switch leaves the switch, not the loop.
378
+ if (sawStop) break;
258
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;
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();
259
430
  }
260
- yield {
261
- type: "done",
262
- stopReason,
263
- usage
264
- };
265
431
  }
266
432
  };
267
433
  }
@@ -1 +1 @@
1
- {"version":3,"names":["errorBody","readSse","DEFAULT_VERSION","toWireMessages","messages","out","m","role","push","content","type","text","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","t","description","input_schema","inputSchema","requestOverrides","ok","message","pending","Map","stopReason","usage","ev","payload","parse","data","kind","event","block","content_block","set","index","json","delta","slot","get","partial_json","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;;AAUA,SAASA,SAAS,EAAEC,OAAO,QAAQ,OAAO;AAE1C,MAAMC,eAAe,GAAG,YAAY;AAOpC,SAASC,cAAcA,CAACC,QAAwB,EAAE;EAChD,MAAMC,GAAuD,GAAG,EAAE;EAClE,KAAK,MAAMC,CAAC,I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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,CAACC,GAAuD,EAAE;EAC9E,MAAMC,WAAW,GAAGD,GAAG,CAACA,GAAG,CAACE,MAAM,GAAG,CAAC,CAAC;EACvC,MAAMC,SAAS,GAAGF,WAAW,EAAEG,OAAO,CAACH,WAAW,CAACG,OAAO,CAACF,MAAM,GAAG,CAAC,CAAC;EACtE,IAAIC,SA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