@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
@@ -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
  *
@@ -23,9 +25,46 @@ var _sse = require("./sse");
23
25
  * user content array with any text after, and that user message must
24
26
  * immediately follow the assistant turn. "Here are the results:" placed
25
27
  * before the blocks is an instant 400.
28
+ * 5. Every current Claude model thinks by DEFAULT, and a `thinking` block must
29
+ * be replayed VERBATIM (signature and all) at the FRONT of the assistant
30
+ * turn it came from whenever that turn's tool results are sent back. An
31
+ * adapter that keeps only text and tool_use 400s on the second request of
32
+ * every tool-using turn — which is every turn Ask Buoy actually makes.
26
33
  */
27
34
 
28
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
+ }
29
68
  function toWireMessages(messages) {
30
69
  const out = [];
31
70
  for (const m of messages) {
@@ -39,6 +78,8 @@ function toWireMessages(messages) {
39
78
  });
40
79
  } else if (m.role === "assistant") {
41
80
  const content = [];
81
+ // Thinking FIRST — see note 5. Signatures are opaque; never edit them.
82
+ for (const t of m.thinking ?? []) content.push(t);
42
83
  if (m.text) content.push({
43
84
  type: "text",
44
85
  text: m.text
@@ -73,199 +114,324 @@ function toWireMessages(messages) {
73
114
  return out;
74
115
  }
75
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);
76
120
  return {
77
121
  protocol: "anthropic",
78
122
  async *send(req) {
79
- const headers = {
80
- "content-type": "application/json",
81
- "anthropic-version": config.anthropicVersion ?? DEFAULT_VERSION,
82
- ...(await (config.headers?.() ?? {}))
123
+ const timer = (0, _streamTimer.createStreamTimer)(req.signal, config);
124
+ const status = {
125
+ truncated: false
83
126
  };
84
- // Dev-only direct mode. A distributed build uses `headers` against the
85
- // org's own gateway and never carries a provider key at all.
86
- if (config.apiKey && !headers["x-api-key"] && !headers.Authorization) {
87
- headers["x-api-key"] = config.apiKey;
88
- // Without this the POST comes back with no access-control-allow-origin
89
- // and a browser/Electron renderer silently discards an otherwise fine
90
- // response — a 200 in the network log and "Failed to fetch" in JS.
91
- headers["anthropic-dangerous-direct-browser-access"] = "true";
92
- }
93
- const response = await fetch(config.endpoint, {
94
- method: "POST",
95
- headers,
96
- signal: req.signal,
97
- body: JSON.stringify({
98
- model: req.model,
99
- max_tokens: req.maxTokens,
100
- stream: true,
101
- // ONE cache breakpoint, at the end of `system`.
102
- //
103
- // Anthropic caches the prefix in a fixed order — tools, then system,
104
- // then messages — up to and including the block carrying
105
- // `cache_control`. So marking the end of the system prompt caches
106
- // the tool definitions with it, which is the point: the tools are
107
- // ~20k tokens and by far the larger half. A second breakpoint on the
108
- // last tool would buy nothing and spend one of the four available.
109
- //
110
- // Messages stay uncached on purpose. They grow every turn, so they
111
- // would invalidate on each request and pay the write multiplier for
112
- // a prefix that is never read back.
113
- system: config.cache === false ? req.system : [{
114
- type: "text",
115
- text: req.system,
116
- cache_control: {
117
- type: "ephemeral"
118
- }
119
- }],
120
- tools: req.tools.map(t => ({
121
- name: t.name,
122
- description: t.description,
123
- input_schema: t.inputSchema
124
- })),
125
- messages: toWireMessages(req.messages),
126
- ...config.requestOverrides
127
- })
128
- });
129
- if (!response.ok) {
130
- yield {
131
- type: "error",
132
- 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?.() ?? {}))
133
132
  };
134
- return;
135
- }
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
+ });
136
192
 
137
- // Partial tool_use args accumulate here, keyed by content-block index.
138
- const pending = new Map();
139
- let stopReason;
140
- let usage;
141
- for await (const ev of (0, _sse.readSse)(response)) {
142
- let payload;
143
- try {
144
- payload = JSON.parse(ev.data);
145
- } catch {
146
- 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;
147
202
  }
148
- const kind = payload.type ?? ev.event;
149
- switch (kind) {
150
- case "content_block_start":
151
- {
152
- const block = payload.content_block;
153
- if (block?.type === "tool_use") {
154
- // NOT block.input — see note 1.
155
- pending.set(payload.index, {
156
- id: block.id,
157
- name: block.name,
158
- json: ""
159
- });
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;
160
255
  }
161
- break;
162
- }
163
- case "content_block_delta":
164
- {
165
- const delta = payload.delta;
166
- if (delta?.type === "text_delta") {
167
- yield {
168
- type: "text",
169
- delta: delta.text
170
- };
171
- } else if (delta?.type === "input_json_delta") {
172
- const slot = pending.get(payload.index);
173
- if (slot) slot.json += delta.partial_json ?? "";
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;
174
275
  }
175
- break;
176
- }
177
- case "content_block_stop":
178
- {
179
- const slot = pending.get(payload.index);
180
- if (slot) {
181
- pending.delete(payload.index);
182
- let input = {};
183
- try {
184
- input = slot.json ? JSON.parse(slot.json) : {};
185
- } catch {
276
+ case "content_block_stop":
277
+ {
278
+ const thought = thinking.get(payload.index);
279
+ if (thought) {
280
+ thinking.delete(payload.index);
186
281
  yield {
187
- type: "error",
188
- message: `Model sent unparseable arguments for ${slot.name}`
282
+ type: "thinking",
283
+ block: {
284
+ type: "thinking",
285
+ ...thought
286
+ }
189
287
  };
190
- break;
191
288
  }
192
- const call = {
193
- id: slot.id,
194
- name: slot.name,
195
- input
196
- };
197
- yield {
198
- type: "tool-call",
199
- call
200
- };
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;
305
+ }
306
+ const call = {
307
+ id: slot.id,
308
+ name: slot.name,
309
+ input
310
+ };
311
+ yield {
312
+ type: "tool-call",
313
+ call
314
+ };
315
+ }
316
+ break;
201
317
  }
202
- break;
203
- }
204
- case "message_start":
205
- {
206
- // The cache counters arrive HERE, not on message_delta — the
207
- // prefix is accounted for before a single token is generated. A
208
- // reader that only watches message_delta sees cacheRead 0 on
209
- // every request and concludes caching is not working.
210
- const m = payload.message;
211
- const u = m?.usage;
212
- if (u) {
213
- usage = {
214
- input: u.input_tokens ?? 0,
215
- output: u.output_tokens ?? 0,
216
- ...(u.cache_read_input_tokens !== undefined ? {
217
- cacheRead: u.cache_read_input_tokens
218
- } : {}),
219
- ...(u.cache_creation_input_tokens !== undefined ? {
220
- cacheWrite: u.cache_creation_input_tokens
221
- } : {})
222
- };
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;
223
342
  }
224
- break;
225
- }
226
- case "message_delta":
227
- {
228
- const delta = payload.delta;
229
- if (delta?.stop_reason) stopReason = delta.stop_reason;
230
- // Cumulative, not incremental — assign, never add. Output tokens
231
- // are only final here, so keep whatever message_start reported
232
- // about the cache rather than replacing the whole record.
233
- const u = payload.usage;
234
- if (u) {
235
- usage = {
236
- ...usage,
237
- input: u.input_tokens ?? usage?.input ?? 0,
238
- 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"
239
371
  };
372
+ return;
240
373
  }
374
+ case "message_stop":
375
+ // The end. There is no [DONE] — see note 2.
376
+ sawStop = true;
377
+ break;
378
+ default:
241
379
  break;
242
- }
243
- case "error":
244
- {
245
- const err = payload.error;
246
- yield {
247
- type: "error",
248
- message: err?.message ?? "Provider error"
249
- };
250
- return;
251
- }
252
- case "message_stop":
253
- // The end. There is no [DONE] — see note 2.
254
- yield {
255
- type: "done",
256
- stopReason,
257
- usage
258
- };
259
- return;
260
- default:
261
- break;
380
+ }
381
+ // `break` inside the switch leaves the switch, not the loop.
382
+ if (sawStop) break;
262
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;
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();
263
434
  }
264
- yield {
265
- type: "done",
266
- stopReason,
267
- usage
268
- };
269
435
  }
270
436
  };
271
437
  }
@@ -1 +1 @@
1
- {"version":3,"names":["_sse","require","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","errorBody","pending","Map","stopReason","usage","ev","readSse","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":";;;;;;AA4BA,IAAAA,IAAA,GAAAC,OAAA;AA5BA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;A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AAM;cAAE9C,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":["_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;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,G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