@buoy-gg/agent-core 7.0.40 → 7.0.42

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 (202) hide show
  1. package/lib/commonjs/blocks/receipts.js +1 -761
  2. package/lib/commonjs/blocks/runId.js +1 -31
  3. package/lib/commonjs/blocks/types.js +1 -140
  4. package/lib/commonjs/blocks/uiTool.js +6 -1405
  5. package/lib/commonjs/catalog/catalog.g.js +5 -4207
  6. package/lib/commonjs/catalog/catalog.source.json +656 -12
  7. package/lib/commonjs/catalog/catalog.types.g.js +1 -44
  8. package/lib/commonjs/catalog/normalizeParams.js +2 -332
  9. package/lib/commonjs/catalog/signature.js +1 -68
  10. package/lib/commonjs/catalog/snapshotReads.js +1 -263
  11. package/lib/commonjs/catalog/toProviderTools.js +2 -145
  12. package/lib/commonjs/catalog/validateParams.js +1 -129
  13. package/lib/commonjs/context/buildContextPack.js +1 -489
  14. package/lib/commonjs/effects/digest.js +1 -34
  15. package/lib/commonjs/effects/ledger.js +1 -325
  16. package/lib/commonjs/engine/askGate.js +1 -287
  17. package/lib/commonjs/engine/effectFor.js +1 -176
  18. package/lib/commonjs/engine/evidence.js +3 -111
  19. package/lib/commonjs/engine/historyBudget.js +1 -363
  20. package/lib/commonjs/engine/retrieve.js +1 -214
  21. package/lib/commonjs/engine/runAgentTurn.js +8 -1803
  22. package/lib/commonjs/engine/systemPrompt.js +54 -163
  23. package/lib/commonjs/engine/textToolCalls.js +1 -276
  24. package/lib/commonjs/engine/tokenCalibration.js +1 -81
  25. package/lib/commonjs/engine/verify.js +4 -297
  26. package/lib/commonjs/index.js +1 -384
  27. package/lib/commonjs/policy/labels.js +1 -319
  28. package/lib/commonjs/policy/policy.js +1 -148
  29. package/lib/commonjs/policy/redact.js +1 -172
  30. package/lib/commonjs/providers/anthropic.js +3 -445
  31. package/lib/commonjs/providers/openai.js +4 -324
  32. package/lib/commonjs/providers/problem.js +1 -98
  33. package/lib/commonjs/providers/sse.js +1 -240
  34. package/lib/commonjs/providers/streamTimer.js +1 -123
  35. package/lib/commonjs/providers/transport.js +1 -123
  36. package/lib/commonjs/providers/types.js +1 -6
  37. package/lib/commonjs/providers/xhrStream.js +1 -212
  38. package/lib/commonjs/realNow.js +1 -0
  39. package/lib/commonjs/session.js +1 -393
  40. package/lib/commonjs/types.js +0 -1
  41. package/lib/module/blocks/receipts.js +1 -755
  42. package/lib/module/blocks/runId.js +1 -26
  43. package/lib/module/blocks/types.js +1 -134
  44. package/lib/module/blocks/uiTool.js +6 -1396
  45. package/lib/module/catalog/catalog.g.js +5 -4203
  46. package/lib/module/catalog/catalog.source.json +656 -12
  47. package/lib/module/catalog/catalog.types.g.js +1 -40
  48. package/lib/module/catalog/normalizeParams.js +2 -326
  49. package/lib/module/catalog/signature.js +1 -63
  50. package/lib/module/catalog/snapshotReads.js +1 -259
  51. package/lib/module/catalog/toProviderTools.js +2 -139
  52. package/lib/module/catalog/validateParams.js +1 -124
  53. package/lib/module/context/buildContextPack.js +1 -485
  54. package/lib/module/effects/digest.js +1 -29
  55. package/lib/module/effects/ledger.js +1 -319
  56. package/lib/module/engine/askGate.js +1 -280
  57. package/lib/module/engine/effectFor.js +1 -173
  58. package/lib/module/engine/evidence.js +3 -105
  59. package/lib/module/engine/historyBudget.js +1 -355
  60. package/lib/module/engine/retrieve.js +1 -210
  61. package/lib/module/engine/runAgentTurn.js +8 -1794
  62. package/lib/module/engine/systemPrompt.js +54 -157
  63. package/lib/module/engine/textToolCalls.js +1 -270
  64. package/lib/module/engine/tokenCalibration.js +1 -75
  65. package/lib/module/engine/verify.js +4 -289
  66. package/lib/module/index.js +1 -35
  67. package/lib/module/policy/labels.js +1 -312
  68. package/lib/module/policy/policy.js +1 -142
  69. package/lib/module/policy/redact.js +1 -165
  70. package/lib/module/providers/anthropic.js +3 -441
  71. package/lib/module/providers/openai.js +4 -320
  72. package/lib/module/providers/problem.js +1 -92
  73. package/lib/module/providers/sse.js +1 -231
  74. package/lib/module/providers/streamTimer.js +1 -118
  75. package/lib/module/providers/transport.js +1 -119
  76. package/lib/module/providers/types.js +1 -4
  77. package/lib/module/providers/xhrStream.js +1 -207
  78. package/lib/module/realNow.js +1 -0
  79. package/lib/module/session.js +1 -369
  80. package/lib/module/types.js +0 -1
  81. package/lib/typescript/catalog/catalog.g.d.ts +3 -3
  82. package/lib/typescript/catalog/catalog.types.g.d.ts +7 -4
  83. package/lib/typescript/effects/ledger.d.ts +16 -1
  84. package/lib/typescript/providers/problem.d.ts +0 -20
  85. package/lib/typescript/providers/streamTimer.d.ts +0 -24
  86. package/lib/typescript/realNow.d.ts +18 -0
  87. package/lib/web/index.mjs +152 -0
  88. package/package.json +24 -3
  89. package/lib/commonjs/blocks/receipts.js.map +0 -1
  90. package/lib/commonjs/blocks/runId.js.map +0 -1
  91. package/lib/commonjs/blocks/types.js.map +0 -1
  92. package/lib/commonjs/blocks/uiTool.js.map +0 -1
  93. package/lib/commonjs/catalog/catalog.g.js.map +0 -1
  94. package/lib/commonjs/catalog/catalog.types.g.js.map +0 -1
  95. package/lib/commonjs/catalog/normalizeParams.js.map +0 -1
  96. package/lib/commonjs/catalog/signature.js.map +0 -1
  97. package/lib/commonjs/catalog/snapshotReads.js.map +0 -1
  98. package/lib/commonjs/catalog/toProviderTools.js.map +0 -1
  99. package/lib/commonjs/catalog/validateParams.js.map +0 -1
  100. package/lib/commonjs/context/buildContextPack.js.map +0 -1
  101. package/lib/commonjs/effects/digest.js.map +0 -1
  102. package/lib/commonjs/effects/ledger.js.map +0 -1
  103. package/lib/commonjs/engine/askGate.js.map +0 -1
  104. package/lib/commonjs/engine/effectFor.js.map +0 -1
  105. package/lib/commonjs/engine/evidence.js.map +0 -1
  106. package/lib/commonjs/engine/historyBudget.js.map +0 -1
  107. package/lib/commonjs/engine/retrieve.js.map +0 -1
  108. package/lib/commonjs/engine/runAgentTurn.js.map +0 -1
  109. package/lib/commonjs/engine/systemPrompt.js.map +0 -1
  110. package/lib/commonjs/engine/textToolCalls.js.map +0 -1
  111. package/lib/commonjs/engine/tokenCalibration.js.map +0 -1
  112. package/lib/commonjs/engine/verify.js.map +0 -1
  113. package/lib/commonjs/index.js.map +0 -1
  114. package/lib/commonjs/policy/labels.js.map +0 -1
  115. package/lib/commonjs/policy/policy.js.map +0 -1
  116. package/lib/commonjs/policy/redact.js.map +0 -1
  117. package/lib/commonjs/providers/anthropic.js.map +0 -1
  118. package/lib/commonjs/providers/openai.js.map +0 -1
  119. package/lib/commonjs/providers/problem.js.map +0 -1
  120. package/lib/commonjs/providers/sse.js.map +0 -1
  121. package/lib/commonjs/providers/streamTimer.js.map +0 -1
  122. package/lib/commonjs/providers/transport.js.map +0 -1
  123. package/lib/commonjs/providers/types.js.map +0 -1
  124. package/lib/commonjs/providers/xhrStream.js.map +0 -1
  125. package/lib/commonjs/session.js.map +0 -1
  126. package/lib/commonjs/types.js.map +0 -1
  127. package/lib/module/blocks/receipts.js.map +0 -1
  128. package/lib/module/blocks/runId.js.map +0 -1
  129. package/lib/module/blocks/types.js.map +0 -1
  130. package/lib/module/blocks/uiTool.js.map +0 -1
  131. package/lib/module/catalog/catalog.g.js.map +0 -1
  132. package/lib/module/catalog/catalog.types.g.js.map +0 -1
  133. package/lib/module/catalog/normalizeParams.js.map +0 -1
  134. package/lib/module/catalog/signature.js.map +0 -1
  135. package/lib/module/catalog/snapshotReads.js.map +0 -1
  136. package/lib/module/catalog/toProviderTools.js.map +0 -1
  137. package/lib/module/catalog/validateParams.js.map +0 -1
  138. package/lib/module/context/buildContextPack.js.map +0 -1
  139. package/lib/module/effects/digest.js.map +0 -1
  140. package/lib/module/effects/ledger.js.map +0 -1
  141. package/lib/module/engine/askGate.js.map +0 -1
  142. package/lib/module/engine/effectFor.js.map +0 -1
  143. package/lib/module/engine/evidence.js.map +0 -1
  144. package/lib/module/engine/historyBudget.js.map +0 -1
  145. package/lib/module/engine/retrieve.js.map +0 -1
  146. package/lib/module/engine/runAgentTurn.js.map +0 -1
  147. package/lib/module/engine/systemPrompt.js.map +0 -1
  148. package/lib/module/engine/textToolCalls.js.map +0 -1
  149. package/lib/module/engine/tokenCalibration.js.map +0 -1
  150. package/lib/module/engine/verify.js.map +0 -1
  151. package/lib/module/index.js.map +0 -1
  152. package/lib/module/policy/labels.js.map +0 -1
  153. package/lib/module/policy/policy.js.map +0 -1
  154. package/lib/module/policy/redact.js.map +0 -1
  155. package/lib/module/providers/anthropic.js.map +0 -1
  156. package/lib/module/providers/openai.js.map +0 -1
  157. package/lib/module/providers/problem.js.map +0 -1
  158. package/lib/module/providers/sse.js.map +0 -1
  159. package/lib/module/providers/streamTimer.js.map +0 -1
  160. package/lib/module/providers/transport.js.map +0 -1
  161. package/lib/module/providers/types.js.map +0 -1
  162. package/lib/module/providers/xhrStream.js.map +0 -1
  163. package/lib/module/session.js.map +0 -1
  164. package/lib/module/types.js.map +0 -1
  165. package/lib/typescript/blocks/receipts.d.ts.map +0 -1
  166. package/lib/typescript/blocks/runId.d.ts.map +0 -1
  167. package/lib/typescript/blocks/types.d.ts.map +0 -1
  168. package/lib/typescript/blocks/uiTool.d.ts.map +0 -1
  169. package/lib/typescript/catalog/catalog.g.d.ts.map +0 -1
  170. package/lib/typescript/catalog/catalog.types.g.d.ts.map +0 -1
  171. package/lib/typescript/catalog/normalizeParams.d.ts.map +0 -1
  172. package/lib/typescript/catalog/signature.d.ts.map +0 -1
  173. package/lib/typescript/catalog/snapshotReads.d.ts.map +0 -1
  174. package/lib/typescript/catalog/toProviderTools.d.ts.map +0 -1
  175. package/lib/typescript/catalog/validateParams.d.ts.map +0 -1
  176. package/lib/typescript/context/buildContextPack.d.ts.map +0 -1
  177. package/lib/typescript/effects/digest.d.ts.map +0 -1
  178. package/lib/typescript/effects/ledger.d.ts.map +0 -1
  179. package/lib/typescript/engine/askGate.d.ts.map +0 -1
  180. package/lib/typescript/engine/effectFor.d.ts.map +0 -1
  181. package/lib/typescript/engine/evidence.d.ts.map +0 -1
  182. package/lib/typescript/engine/historyBudget.d.ts.map +0 -1
  183. package/lib/typescript/engine/retrieve.d.ts.map +0 -1
  184. package/lib/typescript/engine/runAgentTurn.d.ts.map +0 -1
  185. package/lib/typescript/engine/systemPrompt.d.ts.map +0 -1
  186. package/lib/typescript/engine/textToolCalls.d.ts.map +0 -1
  187. package/lib/typescript/engine/tokenCalibration.d.ts.map +0 -1
  188. package/lib/typescript/engine/verify.d.ts.map +0 -1
  189. package/lib/typescript/index.d.ts.map +0 -1
  190. package/lib/typescript/policy/labels.d.ts.map +0 -1
  191. package/lib/typescript/policy/policy.d.ts.map +0 -1
  192. package/lib/typescript/policy/redact.d.ts.map +0 -1
  193. package/lib/typescript/providers/anthropic.d.ts.map +0 -1
  194. package/lib/typescript/providers/openai.d.ts.map +0 -1
  195. package/lib/typescript/providers/problem.d.ts.map +0 -1
  196. package/lib/typescript/providers/sse.d.ts.map +0 -1
  197. package/lib/typescript/providers/streamTimer.d.ts.map +0 -1
  198. package/lib/typescript/providers/transport.d.ts.map +0 -1
  199. package/lib/typescript/providers/types.d.ts.map +0 -1
  200. package/lib/typescript/providers/xhrStream.d.ts.map +0 -1
  201. package/lib/typescript/session.d.ts.map +0 -1
  202. package/lib/typescript/types.d.ts.map +0 -1
@@ -1,323 +1,7 @@
1
- "use strict";
1
+ "use strict";import{truncatedMessage as S,unparseableMessage as O}from"./sse";import{createStreamTimer as N}from"./streamTimer";import{createTransport as A}from"./transport";import{classifyProblem as J}from"./problem";function M(o,d){const e=[{role:"system",content:o}];for(const t of d){if(t.role==="user"){e.push({role:"user",content:t.text})}else if(t.role==="assistant"){e.push({role:"assistant",content:t.text||null,...t.toolCalls?.length?{tool_calls:t.toolCalls.map(i=>({id:i.id,type:"function",function:{name:i.name,arguments:JSON.stringify(i.input)}}))}:{}})}else{for(const i of t.results){e.push({role:"tool",tool_call_id:i.toolCallId,content:i.content})}}}return e}function T(o){const d=o.inputSchema.properties.action;return{...o.inputSchema,properties:{...o.inputSchema.properties,action:{...d,description:`Which action to run.
2
2
 
3
- /**
4
- * OpenAI chat-completions protocol.
5
- *
6
- * The differences from Anthropic that actually matter to the code:
7
- * - Tool results are their own `{role:"tool", tool_call_id, content}` MESSAGE,
8
- * not blocks inside a user message.
9
- * - Tool arguments arrive as a JSON STRING, streamed in fragments, and are
10
- * keyed by an `index` that is stable across deltas while `id`/`name` only
11
- * appear on the first fragment.
12
- * - The stream DOES have a `data: [DONE]` sentinel, unlike Anthropic's.
13
- *
14
- * This adapter also serves Azure and any OpenAI-compatible org gateway, which
15
- * is the common shape for an internal LLM proxy. Note Anthropic's own
16
- * OpenAI-compatibility endpoint is explicitly not production-ready and silently
17
- * ignores several fields — use the native Anthropic adapter for Anthropic.
18
- *
19
- * THE 1024-CHAR TRAP. Azure caps a function description at 1024 chars, so
20
- * this adapter trims to fit. The catalog renders a tool's summary FIRST and
21
- * its `Actions:` list LAST, and every tool's full description is longer than
22
- * the cap — so trimming `t.description` silently removed every action line
23
- * for 23 of the 24 tools — the 24th kept one line of sixteen — measured
24
- * against the catalog as it is. A model on this protocol (api.openai.com, Azure,
25
- * OpenRouter, fal — every row of the model-floor sweep) saw a summary cut
26
- * mid-sentence and a bare `action` enum: no per-action summary, no
27
- * [DESTRUCTIVE] / [changes state] tag, no "prefer X over Y" guidance, and a
28
- * catalog wording change could not move its score. So the description is
29
- * the SUMMARY only, and the `Actions:` block goes into
30
- * `parameters.properties.action.description`, which nothing caps.
31
- */
3
+ ${o.actionsBlock}`}}}}export function createOpenAIProvider(o){const d=A(o);return{protocol:"openai",async*send(e){const t=N(e.signal,o);const i={truncated:false};try{const l={"content-type":"application/json",...await(o.headers?.()??{})};if(o.apiKey&&!l.Authorization&&!l["api-key"]){l.Authorization=`Bearer ${o.apiKey}`}const c=await d.open({headers:l,signal:t.signal,body:JSON.stringify({model:e.model,max_tokens:e.maxTokens,stream:true,stream_options:{include_usage:true},messages:M(e.systemVolatile?`${e.system}
32
4
 
33
- import { truncatedMessage, unparseableMessage } from "./sse";
34
- import { createStreamTimer } from "./streamTimer";
35
- import { createTransport } from "./transport";
36
- import { classifyProblem } from "./problem";
37
- function toWireMessages(system, messages) {
38
- const out = [{
39
- role: "system",
40
- content: system
41
- }];
42
- for (const m of messages) {
43
- if (m.role === "user") {
44
- out.push({
45
- role: "user",
46
- content: m.text
47
- });
48
- } else if (m.role === "assistant") {
49
- out.push({
50
- role: "assistant",
51
- content: m.text || null,
52
- ...(m.toolCalls?.length ? {
53
- tool_calls: m.toolCalls.map(c => ({
54
- id: c.id,
55
- type: "function",
56
- function: {
57
- name: c.name,
58
- arguments: JSON.stringify(c.input)
59
- }
60
- }))
61
- } : {})
62
- });
63
- } else {
64
- // One message per result, not one message with many.
65
- for (const r of m.results) {
66
- out.push({
67
- role: "tool",
68
- tool_call_id: r.toolCallId,
69
- content: r.content
70
- });
71
- }
72
- }
73
- }
74
- return out;
75
- }
5
+ ---
76
6
 
77
- /**
78
- * The tool's input schema with the `Actions:` block on the `action`
79
- * property — the uncapped home for the per-action lines (header note).
80
- */
81
- function withActionsBlock(t) {
82
- const action = t.inputSchema.properties.action;
83
- return {
84
- ...t.inputSchema,
85
- properties: {
86
- ...t.inputSchema.properties,
87
- action: {
88
- ...action,
89
- description: `Which action to run.\n\n${t.actionsBlock}`
90
- }
91
- }
92
- };
93
- }
94
- export function createOpenAIProvider(config) {
95
- // One per provider, so the `auto` transport decision is made once and not
96
- // re-learned (and re-buffered) at the start of every turn.
97
- const transport = createTransport(config);
98
- return {
99
- protocol: "openai",
100
- async *send(req) {
101
- const timer = createStreamTimer(req.signal, config);
102
- const status = {
103
- truncated: false
104
- };
105
- try {
106
- const headers = {
107
- "content-type": "application/json",
108
- ...(await (config.headers?.() ?? {}))
109
- };
110
- if (config.apiKey && !headers.Authorization && !headers["api-key"]) {
111
- headers.Authorization = `Bearer ${config.apiKey}`;
112
- }
113
- const stream = await transport.open({
114
- headers,
115
- // The timer's signal, not the caller's: it aborts on the user's Stop
116
- // AND on either clock running out. See providers/streamTimer.ts.
117
- signal: timer.signal,
118
- body: JSON.stringify({
119
- model: req.model,
120
- max_tokens: req.maxTokens,
121
- stream: true,
122
- stream_options: {
123
- include_usage: true
124
- },
125
- messages: toWireMessages(req.systemVolatile ? `${req.system}\n\n---\n\n${req.systemVolatile}` : req.system, req.messages),
126
- tools: req.tools.map(t => ({
127
- type: "function",
128
- function: {
129
- name: t.name,
130
- // Azure caps function descriptions at 1024 chars while Anthropic's
131
- // guidance asks for several sentences. Trim rather than 400 — but
132
- // trim the SUMMARY only: the action lines travel on `parameters`
133
- // (withActionsBlock), where nothing is capped. See the header.
134
- description: t.summaryDescription.slice(0, 1024),
135
- parameters: withActionsBlock(t)
136
- }
137
- })),
138
- ...config.requestOverrides
139
- })
140
- });
141
-
142
- // Headers are in: the connect clock stops here and the idle one starts.
143
- timer.connected();
144
- if (stream.problem) {
145
- yield {
146
- type: "error",
147
- message: stream.problem,
148
- kind: "provider",
149
- problem: stream.problemInfo
150
- };
151
- return;
152
- }
153
-
154
- // Keyed by the stream's `index`; id and name arrive only on the first
155
- // fragment, arguments accumulate across all of them.
156
- const pending = new Map();
157
- let stopReason;
158
- let usage;
159
- // The served model id, from the first chunk that names one. A gateway
160
- // resolves `gpt-4.1` to a dated snapshot, and that is the id a cost
161
- // table or a benchmark has to attribute a drift to.
162
- let model;
163
- /** The `[DONE]` sentinel; ends the read. See `sawTerminal` below. */
164
- let sawDone = false;
165
- const flush = function* () {
166
- for (const [, slot] of pending) {
167
- let input = {};
168
- try {
169
- input = slot.args ? JSON.parse(slot.args) : {};
170
- } catch {
171
- yield {
172
- type: "error",
173
- kind: "provider",
174
- // Carry the evidence. Without it this error says only THAT the
175
- // arguments were bad, and diagnosing it needs a live repro of a
176
- // model that may not be reachable any more — a Sonnet bench run
177
- // produced 15 of these and left nothing to read. `length` here
178
- // means the model was cut off mid-argument and `max_tokens` is
179
- // the fix; anything else is a genuinely malformed stream.
180
- message: unparseableMessage(slot.name, slot.args, stopReason)
181
- };
182
- continue;
183
- }
184
- yield {
185
- type: "tool-call",
186
- call: {
187
- id: slot.id,
188
- name: slot.name,
189
- input
190
- }
191
- };
192
- }
193
- pending.clear();
194
- };
195
- for await (const ev of stream.frames({
196
- status,
197
- onFrame: timer.activity,
198
- signal: timer.signal
199
- })) {
200
- if (ev.data === "[DONE]") {
201
- sawDone = true;
202
- break;
203
- }
204
- let payload;
205
- try {
206
- payload = JSON.parse(ev.data);
207
- } catch {
208
- continue;
209
- }
210
- if (payload.error) {
211
- const err = payload.error;
212
- const message = err.message ?? "Provider error";
213
- const type = typeof err.code === "string" ? err.code : typeof err.type === "string" ? err.type : undefined;
214
- yield {
215
- type: "error",
216
- message,
217
- kind: "provider",
218
- problem: classifyProblem({
219
- type,
220
- message
221
- })
222
- };
223
- return;
224
- }
225
- if (!model && typeof payload.model === "string" && payload.model) model = payload.model;
226
- const u = payload.usage;
227
- if (u) {
228
- // `prompt_tokens_details.cached_tokens` is this protocol's cache
229
- // counter. Unlike Anthropic's it is INCLUDED in `prompt_tokens`
230
- // rather than reported beside it; it is still carried as
231
- // `cacheRead` so a cost table can price that slice at the
232
- // discounted rate.
233
- const details = u.prompt_tokens_details;
234
- const cached = details?.cached_tokens;
235
- usage = {
236
- input: typeof u.prompt_tokens === "number" ? u.prompt_tokens : 0,
237
- output: typeof u.completion_tokens === "number" ? u.completion_tokens : 0,
238
- ...(typeof cached === "number" ? {
239
- cacheRead: cached
240
- } : {})
241
- };
242
- }
243
- const choice = payload.choices?.[0];
244
- if (!choice) continue;
245
- if (choice.finish_reason) stopReason = choice.finish_reason;
246
- const delta = choice.delta;
247
- if (typeof delta?.content === "string" && delta.content) {
248
- yield {
249
- type: "text",
250
- delta: delta.content
251
- };
252
- }
253
- const calls = delta?.tool_calls;
254
- for (const c of calls ?? []) {
255
- const index = c.index ?? 0;
256
- const fn = c.function;
257
- const slot = pending.get(index) ?? {
258
- id: "",
259
- name: "",
260
- args: ""
261
- };
262
- if (c.id) slot.id = c.id;
263
- if (fn?.name) slot.name = fn.name;
264
- if (typeof fn?.arguments === "string") slot.args += fn.arguments;
265
- pending.set(index, slot);
266
- }
267
- }
268
-
269
- /**
270
- * Did the provider actually say it was finished?
271
- *
272
- * `[DONE]` is the transport sentinel. `finish_reason` counts too, and is
273
- * the stronger claim: it is the model's own statement of why it stopped,
274
- * while `[DONE]` only says the SSE stream closed. Plenty of
275
- * OpenAI-compatible gateways send one and not the other, and refusing
276
- * either shape would break working endpoints for no safety gain. NEITHER
277
- * arriving is the case that matters — bare EOF, which used to be read as
278
- * success, and which is how a complete-looking `storage.setItem` came out
279
- * of a cut connection and ran.
280
- */
281
- const sawTerminal = sawDone || stopReason !== undefined;
282
- if (!sawTerminal) {
283
- // Nothing is flushed: the accumulated arguments may be a whole JSON
284
- // object and still be half a plan. Text already yielded stays on
285
- // screen; the engine shows it and offers Continue.
286
- yield {
287
- type: "error",
288
- message: truncatedMessage(status.truncated),
289
- kind: "truncated"
290
- };
291
- return;
292
- }
293
- yield* flush();
294
- yield {
295
- type: "done",
296
- outcome: stopReason === "length" ? "output-limited" : "completed",
297
- stopReason,
298
- usage,
299
- model
300
- };
301
- } catch (err) {
302
- // The user's Stop wins the label even if a clock fired in the same
303
- // tick: they know why the turn ended, and "timed out" would be a lie.
304
- if (req.signal?.aborted) throw err;
305
- const message = timer.message();
306
- if (message) {
307
- yield {
308
- type: "error",
309
- message,
310
- kind: "timeout"
311
- };
312
- return;
313
- }
314
- // A genuine transport failure — DNS, refused connection, TLS. It has
315
- // always propagated to the failed-turn card, and still does.
316
- throw err;
317
- } finally {
318
- timer.dispose();
319
- }
320
- }
321
- };
322
- }
323
- //# sourceMappingURL=openai.js.map
7
+ ${e.systemVolatile}`:e.system,e.messages),tools:e.tools.map(n=>({type:"function",function:{name:n.name,description:n.summaryDescription.slice(0,1024),parameters:T(n)}})),...o.requestOverrides})});t.connected();if(c.problem){yield{type:"error",message:c.problem,kind:"provider",problem:c.problemInfo};return}const f=new Map;let u;let _;let k;let b=false;const w=function*(){for(const[,n]of f){let r={};try{r=n.args?JSON.parse(n.args):{}}catch{yield{type:"error",kind:"provider",message:O(n.name,n.args,u)};continue}yield{type:"tool-call",call:{id:n.id,name:n.name,input:r}}}f.clear()};for await(const n of c.frames({status:i,onFrame:t.activity,signal:t.signal})){if(n.data==="[DONE]"){b=true;break}let r;try{r=JSON.parse(n.data)}catch{continue}if(r.error){const s=r.error;const a=s.message??"Provider error";const m=typeof s.code==="string"?s.code:typeof s.type==="string"?s.type:void 0;yield{type:"error",message:a,kind:"provider",problem:J({type:m,message:a})};return}if(!k&&typeof r.model==="string"&&r.model)k=r.model;const p=r.usage;if(p){const s=p.prompt_tokens_details;const a=s?.cached_tokens;_={input:typeof p.prompt_tokens==="number"?p.prompt_tokens:0,output:typeof p.completion_tokens==="number"?p.completion_tokens:0,...typeof a==="number"?{cacheRead:a}:{}}}const y=r.choices?.[0];if(!y)continue;if(y.finish_reason)u=y.finish_reason;const g=y.delta;if(typeof g?.content==="string"&&g.content){yield{type:"text",delta:g.content}}const x=g?.tool_calls;for(const s of x??[]){const a=s.index??0;const m=s.function;const h=f.get(a)??{id:"",name:"",args:""};if(s.id)h.id=s.id;if(m?.name)h.name=m.name;if(typeof m?.arguments==="string")h.args+=m.arguments;f.set(a,h)}}const v=b||u!==void 0;if(!v){yield{type:"error",message:S(i.truncated),kind:"truncated"};return}yield*w();yield{type:"done",outcome:u==="length"?"output-limited":"completed",stopReason:u,usage:_,model:k}}catch(l){if(e.signal?.aborted)throw l;const c=t.message();if(c){yield{type:"error",message:c,kind:"timeout"};return}throw l}finally{t.dispose()}}}}
@@ -1,92 +1 @@
1
- "use strict";
2
-
3
- /**
4
- * What kind of failure a provider reported — decided once, from the status,
5
- * the error type and the message, so the engine can act on the KIND rather
6
- * than grep a string.
7
- *
8
- * Only two kinds are acted on, and both only when nothing has been generated:
9
- *
10
- * - `throttled` — 429 / 529 / 503, a `rate_limit_error` or `overloaded_error`
11
- * frame, or a body that says so. Waiting and asking again is the right move;
12
- * the request never reached a model.
13
- * - `overflow` — the provider refused the request as too big. Trimming older
14
- * history and asking once more is the right move; the proactive budget is
15
- * an estimate (chars ÷ 4) and JSON tokenizes worse than prose.
16
- *
17
- * Everything else — auth, a bad schema, an unknown model, a mid-answer
18
- * `api_error` — is reported and NOT retried. The phrase lists are the union
19
- * observed across provider responses by the Strands SDK (harness-sdk
20
- * `381ab48`, `models/anthropic.ts` + `models/openai/errors.ts`, Apache-2.0),
21
- * matched case-insensitively on the lowered message.
22
- */
23
-
24
- const THROTTLE_STATUSES = new Set([429, 503, 529]);
25
- const AUTH_STATUSES = new Set([401, 403]);
26
-
27
- /** Provider error `type` values (Anthropic frames, OpenAI `error.type` / `code`). */
28
- const THROTTLE_TYPES = new Set(["overloaded_error", "rate_limit_error", "rate_limit_exceeded", "insufficient_quota", "server_overloaded"]);
29
- const AUTH_TYPES = new Set(["authentication_error", "permission_error", "invalid_api_key"]);
30
- const OVERFLOW_TYPES = new Set(["request_too_large", "context_length_exceeded"]);
31
- const THROTTLE_PHRASES = ["rate_limit_exceeded", "rate limit", "too many requests", "overloaded", "capacity", "try again later"];
32
- const OVERFLOW_PHRASES = [
33
- // Anthropic
34
- "prompt is too long", "input too long", "input is too long", "input length exceeds context window", "input and output tokens exceed your context limit",
35
- // OpenAI and compatible gateways
36
- "maximum context length", "context_length_exceeded", "too many tokens", "context length", "input is too long for requested model", "input length and `max_tokens` exceed context limit", "too many total text bytes", "exceed customer model maximum"];
37
- export function classifyProblem(input) {
38
- const lowered = input.message.toLowerCase();
39
- const type = input.type?.toLowerCase();
40
- const retryAfterMs = parseRetryAfter(input.retryAfter);
41
- const withStatus = kind => ({
42
- kind,
43
- ...(input.status !== undefined ? {
44
- status: input.status
45
- } : {}),
46
- ...(retryAfterMs !== undefined ? {
47
- retryAfterMs
48
- } : {})
49
- });
50
- if (input.status !== undefined && AUTH_STATUSES.has(input.status)) return withStatus("auth");
51
- if (type && AUTH_TYPES.has(type)) return withStatus("auth");
52
- if (input.status !== undefined && THROTTLE_STATUSES.has(input.status)) return withStatus("throttled");
53
- if (type && THROTTLE_TYPES.has(type)) return withStatus("throttled");
54
- if (type && OVERFLOW_TYPES.has(type)) return withStatus("overflow");
55
- // Overflow before throttling on phrases: "context length" is more specific
56
- // than "capacity", and a 400 that says both is about the size.
57
- if (OVERFLOW_PHRASES.some(p => lowered.includes(p))) return withStatus("overflow");
58
- if (THROTTLE_PHRASES.some(p => lowered.includes(p))) return withStatus("throttled");
59
- return withStatus("other");
60
- }
61
-
62
- /**
63
- * `Retry-After` is either delay-seconds or an HTTP-date. Anything unparseable
64
- * or in the past is undefined, so the caller falls back to its own backoff.
65
- */
66
- export function parseRetryAfter(value) {
67
- if (!value) return undefined;
68
- const trimmed = value.trim();
69
- if (/^\d+$/.test(trimmed)) return Number(trimmed) * 1000;
70
- const at = Date.parse(trimmed);
71
- if (Number.isNaN(at)) return undefined;
72
- const ms = at - Date.now();
73
- return ms > 0 ? ms : undefined;
74
- }
75
-
76
- /**
77
- * Pull the provider's error `type` out of an error body, for the HTTP path
78
- * where the body is JSON like `{"error":{"type":"overloaded_error",…}}` or
79
- * `{"error":{"code":"context_length_exceeded",…}}`. Returns undefined for
80
- * anything that is not that shape; the message phrases still apply.
81
- */
82
- export function errorTypeOf(body) {
83
- try {
84
- const parsed = JSON.parse(body);
85
- const err = typeof parsed.error === "object" && parsed.error ? parsed.error : undefined;
86
- const type = err?.type ?? err?.code ?? parsed.type;
87
- return typeof type === "string" ? type : undefined;
88
- } catch {
89
- return undefined;
90
- }
91
- }
92
- //# sourceMappingURL=problem.js.map
1
+ "use strict";import{realNow as i}from"../realNow";const d=new Set([429,503,529]);const a=new Set([401,403]);const u=new Set(["overloaded_error","rate_limit_error","rate_limit_exceeded","insufficient_quota","server_overloaded"]);const c=new Set(["authentication_error","permission_error","invalid_api_key"]);const f=new Set(["request_too_large","context_length_exceeded"]);const l=["rate_limit_exceeded","rate limit","too many requests","overloaded","capacity","try again later"];const m=["prompt is too long","input too long","input is too long","input length exceeds context window","input and output tokens exceed your context limit","maximum context length","context_length_exceeded","too many tokens","context length","input is too long for requested model","input length and `max_tokens` exceed context limit","too many total text bytes","exceed customer model maximum"];export function classifyProblem(e){const r=e.message.toLowerCase();const t=e.type?.toLowerCase();const o=parseRetryAfter(e.retryAfter);const n=s=>({kind:s,...e.status!==void 0?{status:e.status}:{},...o!==void 0?{retryAfterMs:o}:{}});if(e.status!==void 0&&a.has(e.status))return n("auth");if(t&&c.has(t))return n("auth");if(e.status!==void 0&&d.has(e.status))return n("throttled");if(t&&u.has(t))return n("throttled");if(t&&f.has(t))return n("overflow");if(m.some(s=>r.includes(s)))return n("overflow");if(l.some(s=>r.includes(s)))return n("throttled");return n("other")}export function parseRetryAfter(e){if(!e)return void 0;const r=e.trim();if(/^\d+$/.test(r))return Number(r)*1e3;const t=Date.parse(r);if(Number.isNaN(t))return void 0;const o=t-i();return o>0?o:void 0}export function errorTypeOf(e){try{const r=JSON.parse(e);const t=typeof r.error==="object"&&r.error?r.error:void 0;const o=t?.type??t?.code??r.type;return typeof o==="string"?o:void 0}catch{return void 0}}
@@ -1,231 +1 @@
1
- "use strict";
2
-
3
- /**
4
- * SSE reading, with the React Native reality baked in.
5
- *
6
- * RN's built-in `fetch` does not implement `response.body`. Under Expo you get
7
- * a real `ReadableStream` because `expo/fetch` is the global — but
8
- * `EXPO_PUBLIC_USE_RN_FETCH=1` reverts that, and the bare-RN, NativeScript and
9
- * TV-CLI lanes never had it. So `readSse` degrades to reading the whole body
10
- * and replaying it as events rather than failing: a non-streaming answer is a
11
- * slower chat, while a hard failure is no chat at all.
12
- *
13
- * THE TWO PATHS MUST AGREE. They did not. The buffered path appended "\n\n" to
14
- * the body before parsing, which turns an unterminated final frame — the exact
15
- * signature of a cut connection — into a well-formed event. The streaming path
16
- * discarded that same frame, as the SSE spec requires. So one lane invented a
17
- * complete answer where the other saw a broken one. Now both discard it and
18
- * both RECORD it (`SseReadStatus.truncated`), because "was this reply whole?"
19
- * is a question the engine has to answer before it runs anything.
20
- */
21
-
22
- function* parseChunk(buffer, onFrame) {
23
- let rest = buffer;
24
- let idx;
25
- // Frames are separated by a blank line. \r\n\r\n is legal too.
26
- while ((idx = rest.search(/\r?\n\r?\n/)) !== -1) {
27
- const raw = rest.slice(0, idx);
28
- rest = rest.slice(idx + (rest[idx] === "\r" ? 4 : 2));
29
- onFrame?.();
30
- let event;
31
- const dataLines = [];
32
- for (const line of raw.split(/\r?\n/)) {
33
- if (line.startsWith(":")) continue; // comment / keepalive
34
- if (line.startsWith("event:")) event = line.slice(6).trim();else if (line.startsWith("data:")) dataLines.push(line.slice(5).replace(/^ /, ""));
35
- }
36
- if (dataLines.length) yield {
37
- event,
38
- data: dataLines.join("\n")
39
- };
40
- }
41
- return rest;
42
- }
43
-
44
- /**
45
- * Drain complete frames out of `buffer`, returning what is left over.
46
- *
47
- * Shared by both paths so a framing fix can only ever be made in one place.
48
- */
49
- function* drain(buffer, out, onFrame) {
50
- const gen = parseChunk(buffer, onFrame);
51
- let next = gen.next();
52
- while (!next.done) {
53
- yield next.value;
54
- next = gen.next();
55
- }
56
- out.rest = next.value;
57
- }
58
-
59
- /**
60
- * Turn a stream of TEXT chunks into SSE frames.
61
- *
62
- * The one place framing happens, whatever produced the bytes: a `fetch` body
63
- * reader, an `XMLHttpRequest` progress slice, or a whole body read at once on
64
- * a runtime that cannot stream. Splitting it out is what lets the XHR
65
- * transport exist without a second parser to keep in step with this one.
66
- */
67
- export async function* framesFrom(chunks, options = {}) {
68
- const {
69
- status,
70
- onFrame
71
- } = options;
72
- const leftover = {
73
- rest: ""
74
- };
75
- let buffer = "";
76
- for await (const chunk of chunks) {
77
- buffer += chunk;
78
- yield* drain(buffer, leftover, onFrame);
79
- buffer = leftover.rest;
80
- }
81
- if (status && leftover.rest.trim()) status.truncated = true;
82
- }
83
- export async function* readSse(response, options = {}) {
84
- const {
85
- status,
86
- onFrame,
87
- signal
88
- } = options;
89
- const body = response.body;
90
- const leftover = {
91
- rest: ""
92
- };
93
- if (!body || typeof body.getReader !== "function") {
94
- // No streaming on this platform — read it all, then replay. Note there is
95
- // no "\n\n" here any more: an unterminated tail is discarded exactly as
96
- // the streaming path discards it (see the header).
97
- const text = await response.text();
98
- yield* drain(text, leftover, onFrame);
99
- if (status && leftover.rest.trim()) status.truncated = true;
100
- return;
101
- }
102
- const reader = body.getReader();
103
- const decoder = new TextDecoder();
104
- let buffer = "";
105
- let finished = false;
106
- /**
107
- * One promise for the whole read, not one per chunk.
108
- *
109
- * It never resolves — only rejects — so racing it is free apart from the
110
- * `Promise.race` allocation, and an answer of a few hundred frames pays far
111
- * more for `JSON.parse` than for this.
112
- */
113
- const aborted = signal ? new Promise((_resolve, reject) => {
114
- const fail = () => {
115
- const err = new Error("Aborted");
116
- err.name = "AbortError";
117
- reject(err);
118
- };
119
- if (signal.aborted) fail();else signal.addEventListener?.("abort", fail, {
120
- once: true
121
- });
122
- }) : undefined;
123
- // Nothing awaits this until the race below, and a rejection with no handler
124
- // is an unhandled rejection on some runtimes. This is the handler.
125
- aborted?.catch(() => {});
126
- try {
127
- for (;;) {
128
- const read = reader.read();
129
- const {
130
- done,
131
- value
132
- } = aborted ? await Promise.race([read, aborted]) : await read;
133
- if (done) break;
134
- buffer += decoder.decode(value, {
135
- stream: true
136
- });
137
- yield* drain(buffer, leftover, onFrame);
138
- buffer = leftover.rest;
139
- }
140
- // Flush the decoder: a multi-byte character split across the last two
141
- // chunks is only completed here, and an INCOMPLETE one at EOF is itself
142
- // evidence the connection was cut.
143
- buffer += decoder.decode();
144
- yield* drain(buffer, leftover, onFrame);
145
- if (status && leftover.rest.trim()) status.truncated = true;
146
- finished = true;
147
- } finally {
148
- if (!finished) {
149
- // The consumer left early — an abort, a timeout, or a `break` in the
150
- // provider. `releaseLock` alone leaves the socket open; only `cancel`
151
- // tears the request down, and it must come first because cancelling a
152
- // locked reader is the only way to reach the underlying stream.
153
- try {
154
- // Not awaited: the consumer is already leaving, and a cancel that
155
- // rejects (an errored stream) must not become an unhandled rejection.
156
- void Promise.resolve(reader.cancel()).catch(() => {});
157
- } catch {
158
- /* already cancelled or errored */
159
- }
160
- }
161
- try {
162
- reader.releaseLock();
163
- } catch {
164
- /* already released */
165
- }
166
- }
167
- }
168
-
169
- /**
170
- * A 200 that is not a stream at all.
171
- *
172
- * Gateways in front of a model do this: an auth proxy that has lost its
173
- * upstream serves an HTML error page, a misrouted path serves an SPA shell,
174
- * and a gateway that ignored `stream: true` serves one JSON object. Every one
175
- * of those parses to zero SSE frames, so the old code read it as a model that
176
- * had nothing to say and ended the turn with a blank bubble.
177
- *
178
- * Returns a message when the body should be treated as an error, and undefined
179
- * when it looks like something the SSE reader can work with. Consumes the body
180
- * only in the failing case.
181
- */
182
- export async function nonStreamProblem(response) {
183
- const type = response.headers?.get?.("content-type") ?? "";
184
- if (!type) return undefined; // No header to judge by — let the parser try.
185
- const lowered = type.toLowerCase();
186
- if (lowered.includes("event-stream")) return undefined;
187
- // A JSON body is either a non-streaming reply or a structured error; both
188
- // are worth reporting with their own text rather than guessing.
189
- const detail = (await response.text().catch(() => "")).slice(0, 600);
190
- if (lowered.includes("json")) {
191
- return `The endpoint answered with JSON instead of a stream — it may not support "stream": true.${detail ? ` — ${detail}` : ""}`;
192
- }
193
- return `The endpoint answered with ${type} instead of an event stream.${detail ? ` — ${detail}` : ""}`;
194
- }
195
-
196
- /**
197
- * Body text for a failed response, trimmed to something a user can read.
198
- * The detail alone — the transport prefixes the status and classifies it.
199
- */
200
- export async function errorBody(response) {
201
- try {
202
- return (await response.text()).slice(0, 600);
203
- } catch {
204
- return ""; // body already consumed or unreadable
205
- }
206
- }
207
-
208
- /**
209
- * The message for a stream that stopped before the provider said it was done.
210
- *
211
- * `truncated` distinguishes the two shapes: cut mid-frame (a dropped
212
- * connection) versus a clean frame boundary with no terminal marker (a gateway
213
- * that closes the socket instead of sending one). The second is a
214
- * compatibility problem worth naming, because retrying it will not help.
215
- */
216
- export function truncatedMessage(truncated) {
217
- return truncated ? "The connection dropped before the answer finished." : "The endpoint closed the stream without saying the answer was finished.";
218
- }
219
-
220
- /**
221
- * The message for arguments that would not parse, with enough in it to act on.
222
- * `stopReason` "length" is the common cause — a reasoning model spends its
223
- * budget thinking and the tool call is truncated mid-JSON — and the tail of
224
- * the raw arguments says immediately whether that is what happened.
225
- */
226
- export function unparseableMessage(name, args, stopReason) {
227
- const truncated = stopReason === "length";
228
- const tail = args.length > 160 ? `…${args.slice(-160)}` : args;
229
- return `Model sent unparseable arguments for ${name}` + (truncated ? " (cut off at max_tokens — raise it or reduce thinking budget)" : "") + ` [stop: ${stopReason ?? "none"}, ${args.length} chars] ${tail}`;
230
- }
231
- //# sourceMappingURL=sse.js.map
1
+ "use strict";function*p(n,r){let e=n;let t;while((t=e.search(/\r?\n\r?\n/))!==-1){const s=e.slice(0,t);e=e.slice(t+(e[t]==="\r"?4:2));r?.();let a;const o=[];for(const i of s.split(/\r?\n/)){if(i.startsWith(":"))continue;if(i.startsWith("event:"))a=i.slice(6).trim();else if(i.startsWith("data:"))o.push(i.slice(5).replace(/^ /,""))}if(o.length)yield{event:a,data:o.join("\n")}}return e}function*l(n,r,e){const t=p(n,e);let s=t.next();while(!s.done){yield s.value;s=t.next()}r.rest=s.value}export async function*framesFrom(n,r={}){const{status:e,onFrame:t}=r;const s={rest:""};let a="";for await(const o of n){a+=o;yield*l(a,s,t);a=s.rest}if(e&&s.rest.trim())e.truncated=true}export async function*readSse(n,r={}){const{status:e,onFrame:t,signal:s}=r;const a=n.body;const o={rest:""};if(!a||typeof a.getReader!=="function"){const d=await n.text();yield*l(d,o,t);if(e&&o.rest.trim())e.truncated=true;return}const i=a.getReader();const m=new TextDecoder;let c="";let y=false;const f=s?new Promise((d,h)=>{const u=()=>{const w=new Error("Aborted");w.name="AbortError";h(w)};if(s.aborted)u();else s.addEventListener?.("abort",u,{once:true})}):void 0;f?.catch(()=>{});try{for(;;){const d=i.read();const{done:h,value:u}=f?await Promise.race([d,f]):await d;if(h)break;c+=m.decode(u,{stream:true});yield*l(c,o,t);c=o.rest}c+=m.decode();yield*l(c,o,t);if(e&&o.rest.trim())e.truncated=true;y=true}finally{if(!y){try{void Promise.resolve(i.cancel()).catch(()=>{})}catch{}}try{i.releaseLock()}catch{}}}export async function nonStreamProblem(n){const r=n.headers?.get?.("content-type")??"";if(!r)return void 0;const e=r.toLowerCase();if(e.includes("event-stream"))return void 0;const t=(await n.text().catch(()=>"")).slice(0,600);if(e.includes("json")){return`The endpoint answered with JSON instead of a stream \u2014 it may not support "stream": true.${t?` \u2014 ${t}`:""}`}return`The endpoint answered with ${r} instead of an event stream.${t?` \u2014 ${t}`:""}`}export async function errorBody(n){try{return(await n.text()).slice(0,600)}catch{return""}}export function truncatedMessage(n){return n?"The connection dropped before the answer finished.":"The endpoint closed the stream without saying the answer was finished."}export function unparseableMessage(n,r,e){const t=e==="length";const s=r.length>160?`\u2026${r.slice(-160)}`:r;return`Model sent unparseable arguments for ${n}`+(t?" (cut off at max_tokens \u2014 raise it or reduce thinking budget)":"")+` [stop: ${e??"none"}, ${r.length} chars] ${s}`}