@buoy-gg/agent-core 7.0.35 → 7.0.38

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 (208) hide show
  1. package/README.md +21 -14
  2. package/lib/commonjs/blocks/receipts.js +57 -1
  3. package/lib/commonjs/blocks/receipts.js.map +1 -1
  4. package/lib/commonjs/blocks/types.js +23 -2
  5. package/lib/commonjs/blocks/types.js.map +1 -1
  6. package/lib/commonjs/blocks/uiTool.js +423 -10
  7. package/lib/commonjs/blocks/uiTool.js.map +1 -1
  8. package/lib/commonjs/catalog/catalog.g.js +213 -48
  9. package/lib/commonjs/catalog/catalog.g.js.map +1 -1
  10. package/lib/commonjs/catalog/catalog.source.json +226 -48
  11. package/lib/commonjs/catalog/catalog.types.g.js +44 -0
  12. package/lib/commonjs/catalog/catalog.types.g.js.map +1 -0
  13. package/lib/commonjs/catalog/normalizeParams.js +333 -0
  14. package/lib/commonjs/catalog/normalizeParams.js.map +1 -0
  15. package/lib/commonjs/catalog/signature.js +68 -0
  16. package/lib/commonjs/catalog/signature.js.map +1 -0
  17. package/lib/commonjs/catalog/snapshotReads.js +13 -6
  18. package/lib/commonjs/catalog/snapshotReads.js.map +1 -1
  19. package/lib/commonjs/catalog/toProviderTools.js +44 -6
  20. package/lib/commonjs/catalog/toProviderTools.js.map +1 -1
  21. package/lib/commonjs/context/buildContextPack.js +367 -19
  22. package/lib/commonjs/context/buildContextPack.js.map +1 -1
  23. package/lib/commonjs/effects/digest.js +34 -0
  24. package/lib/commonjs/effects/digest.js.map +1 -0
  25. package/lib/commonjs/effects/ledger.js +98 -2
  26. package/lib/commonjs/effects/ledger.js.map +1 -1
  27. package/lib/commonjs/engine/askGate.js +287 -0
  28. package/lib/commonjs/engine/askGate.js.map +1 -0
  29. package/lib/commonjs/engine/effectFor.js +82 -1
  30. package/lib/commonjs/engine/effectFor.js.map +1 -1
  31. package/lib/commonjs/engine/evidence.js +113 -0
  32. package/lib/commonjs/engine/evidence.js.map +1 -0
  33. package/lib/commonjs/engine/historyBudget.js +363 -0
  34. package/lib/commonjs/engine/historyBudget.js.map +1 -0
  35. package/lib/commonjs/engine/retrieve.js +214 -0
  36. package/lib/commonjs/engine/retrieve.js.map +1 -0
  37. package/lib/commonjs/engine/runAgentTurn.js +1231 -120
  38. package/lib/commonjs/engine/runAgentTurn.js.map +1 -1
  39. package/lib/commonjs/engine/systemPrompt.js +103 -9
  40. package/lib/commonjs/engine/systemPrompt.js.map +1 -1
  41. package/lib/commonjs/engine/textToolCalls.js +276 -0
  42. package/lib/commonjs/engine/textToolCalls.js.map +1 -0
  43. package/lib/commonjs/engine/tokenCalibration.js +81 -0
  44. package/lib/commonjs/engine/tokenCalibration.js.map +1 -0
  45. package/lib/commonjs/engine/verify.js +300 -0
  46. package/lib/commonjs/engine/verify.js.map +1 -0
  47. package/lib/commonjs/index.js +74 -0
  48. package/lib/commonjs/index.js.map +1 -1
  49. package/lib/commonjs/policy/labels.js +19 -3
  50. package/lib/commonjs/policy/labels.js.map +1 -1
  51. package/lib/commonjs/policy/policy.js +6 -1
  52. package/lib/commonjs/policy/policy.js.map +1 -1
  53. package/lib/commonjs/policy/redact.js +42 -5
  54. package/lib/commonjs/policy/redact.js.map +1 -1
  55. package/lib/commonjs/providers/anthropic.js +340 -208
  56. package/lib/commonjs/providers/anthropic.js.map +1 -1
  57. package/lib/commonjs/providers/openai.js +237 -103
  58. package/lib/commonjs/providers/openai.js.map +1 -1
  59. package/lib/commonjs/providers/problem.js +98 -0
  60. package/lib/commonjs/providers/problem.js.map +1 -0
  61. package/lib/commonjs/providers/sse.js +170 -34
  62. package/lib/commonjs/providers/sse.js.map +1 -1
  63. package/lib/commonjs/providers/streamTimer.js +123 -0
  64. package/lib/commonjs/providers/streamTimer.js.map +1 -0
  65. package/lib/commonjs/providers/transport.js +123 -0
  66. package/lib/commonjs/providers/transport.js.map +1 -0
  67. package/lib/commonjs/providers/xhrStream.js +212 -0
  68. package/lib/commonjs/providers/xhrStream.js.map +1 -0
  69. package/lib/commonjs/session.js +242 -58
  70. package/lib/commonjs/session.js.map +1 -1
  71. package/lib/module/blocks/receipts.js +57 -1
  72. package/lib/module/blocks/receipts.js.map +1 -1
  73. package/lib/module/blocks/types.js +23 -2
  74. package/lib/module/blocks/types.js.map +1 -1
  75. package/lib/module/blocks/uiTool.js +421 -10
  76. package/lib/module/blocks/uiTool.js.map +1 -1
  77. package/lib/module/catalog/catalog.g.js +213 -48
  78. package/lib/module/catalog/catalog.g.js.map +1 -1
  79. package/lib/module/catalog/catalog.source.json +226 -48
  80. package/lib/module/catalog/catalog.types.g.js +40 -0
  81. package/lib/module/catalog/catalog.types.g.js.map +1 -0
  82. package/lib/module/catalog/normalizeParams.js +327 -0
  83. package/lib/module/catalog/normalizeParams.js.map +1 -0
  84. package/lib/module/catalog/signature.js +63 -0
  85. package/lib/module/catalog/signature.js.map +1 -0
  86. package/lib/module/catalog/snapshotReads.js +13 -6
  87. package/lib/module/catalog/snapshotReads.js.map +1 -1
  88. package/lib/module/catalog/toProviderTools.js +44 -6
  89. package/lib/module/catalog/toProviderTools.js.map +1 -1
  90. package/lib/module/context/buildContextPack.js +366 -19
  91. package/lib/module/context/buildContextPack.js.map +1 -1
  92. package/lib/module/effects/digest.js +29 -0
  93. package/lib/module/effects/digest.js.map +1 -0
  94. package/lib/module/effects/ledger.js +98 -2
  95. package/lib/module/effects/ledger.js.map +1 -1
  96. package/lib/module/engine/askGate.js +280 -0
  97. package/lib/module/engine/askGate.js.map +1 -0
  98. package/lib/module/engine/effectFor.js +83 -1
  99. package/lib/module/engine/effectFor.js.map +1 -1
  100. package/lib/module/engine/evidence.js +107 -0
  101. package/lib/module/engine/evidence.js.map +1 -0
  102. package/lib/module/engine/historyBudget.js +355 -0
  103. package/lib/module/engine/historyBudget.js.map +1 -0
  104. package/lib/module/engine/retrieve.js +210 -0
  105. package/lib/module/engine/retrieve.js.map +1 -0
  106. package/lib/module/engine/runAgentTurn.js +1227 -121
  107. package/lib/module/engine/runAgentTurn.js.map +1 -1
  108. package/lib/module/engine/systemPrompt.js +101 -9
  109. package/lib/module/engine/systemPrompt.js.map +1 -1
  110. package/lib/module/engine/textToolCalls.js +270 -0
  111. package/lib/module/engine/textToolCalls.js.map +1 -0
  112. package/lib/module/engine/tokenCalibration.js +75 -0
  113. package/lib/module/engine/tokenCalibration.js.map +1 -0
  114. package/lib/module/engine/verify.js +292 -0
  115. package/lib/module/engine/verify.js.map +1 -0
  116. package/lib/module/index.js +7 -3
  117. package/lib/module/index.js.map +1 -1
  118. package/lib/module/policy/labels.js +19 -3
  119. package/lib/module/policy/labels.js.map +1 -1
  120. package/lib/module/policy/policy.js +6 -1
  121. package/lib/module/policy/policy.js.map +1 -1
  122. package/lib/module/policy/redact.js +42 -5
  123. package/lib/module/policy/redact.js.map +1 -1
  124. package/lib/module/providers/anthropic.js +341 -209
  125. package/lib/module/providers/anthropic.js.map +1 -1
  126. package/lib/module/providers/openai.js +238 -104
  127. package/lib/module/providers/openai.js.map +1 -1
  128. package/lib/module/providers/problem.js +92 -0
  129. package/lib/module/providers/problem.js.map +1 -0
  130. package/lib/module/providers/sse.js +166 -34
  131. package/lib/module/providers/sse.js.map +1 -1
  132. package/lib/module/providers/streamTimer.js +118 -0
  133. package/lib/module/providers/streamTimer.js.map +1 -0
  134. package/lib/module/providers/transport.js +119 -0
  135. package/lib/module/providers/transport.js.map +1 -0
  136. package/lib/module/providers/xhrStream.js +207 -0
  137. package/lib/module/providers/xhrStream.js.map +1 -0
  138. package/lib/module/session.js +226 -60
  139. package/lib/module/session.js.map +1 -1
  140. package/lib/typescript/blocks/receipts.d.ts +5 -0
  141. package/lib/typescript/blocks/receipts.d.ts.map +1 -1
  142. package/lib/typescript/blocks/types.d.ts +23 -2
  143. package/lib/typescript/blocks/types.d.ts.map +1 -1
  144. package/lib/typescript/blocks/uiTool.d.ts +11 -2
  145. package/lib/typescript/blocks/uiTool.d.ts.map +1 -1
  146. package/lib/typescript/catalog/catalog.g.d.ts +2 -2
  147. package/lib/typescript/catalog/catalog.g.d.ts.map +1 -1
  148. package/lib/typescript/catalog/catalog.types.g.d.ts +40 -0
  149. package/lib/typescript/catalog/catalog.types.g.d.ts.map +1 -0
  150. package/lib/typescript/catalog/normalizeParams.d.ts +43 -0
  151. package/lib/typescript/catalog/normalizeParams.d.ts.map +1 -0
  152. package/lib/typescript/catalog/signature.d.ts +29 -0
  153. package/lib/typescript/catalog/signature.d.ts.map +1 -0
  154. package/lib/typescript/catalog/snapshotReads.d.ts.map +1 -1
  155. package/lib/typescript/catalog/toProviderTools.d.ts +28 -15
  156. package/lib/typescript/catalog/toProviderTools.d.ts.map +1 -1
  157. package/lib/typescript/context/buildContextPack.d.ts +24 -2
  158. package/lib/typescript/context/buildContextPack.d.ts.map +1 -1
  159. package/lib/typescript/effects/digest.d.ts +10 -0
  160. package/lib/typescript/effects/digest.d.ts.map +1 -0
  161. package/lib/typescript/effects/ledger.d.ts +73 -0
  162. package/lib/typescript/effects/ledger.d.ts.map +1 -1
  163. package/lib/typescript/engine/askGate.d.ts +29 -0
  164. package/lib/typescript/engine/askGate.d.ts.map +1 -0
  165. package/lib/typescript/engine/effectFor.d.ts +6 -1
  166. package/lib/typescript/engine/effectFor.d.ts.map +1 -1
  167. package/lib/typescript/engine/evidence.d.ts +67 -0
  168. package/lib/typescript/engine/evidence.d.ts.map +1 -0
  169. package/lib/typescript/engine/historyBudget.d.ts +115 -0
  170. package/lib/typescript/engine/historyBudget.d.ts.map +1 -0
  171. package/lib/typescript/engine/retrieve.d.ts +32 -0
  172. package/lib/typescript/engine/retrieve.d.ts.map +1 -0
  173. package/lib/typescript/engine/runAgentTurn.d.ts +179 -3
  174. package/lib/typescript/engine/runAgentTurn.d.ts.map +1 -1
  175. package/lib/typescript/engine/systemPrompt.d.ts +80 -0
  176. package/lib/typescript/engine/systemPrompt.d.ts.map +1 -1
  177. package/lib/typescript/engine/textToolCalls.d.ts +58 -0
  178. package/lib/typescript/engine/textToolCalls.d.ts.map +1 -0
  179. package/lib/typescript/engine/tokenCalibration.d.ts +46 -0
  180. package/lib/typescript/engine/tokenCalibration.d.ts.map +1 -0
  181. package/lib/typescript/engine/verify.d.ts +67 -0
  182. package/lib/typescript/engine/verify.d.ts.map +1 -0
  183. package/lib/typescript/index.d.ts +7 -3
  184. package/lib/typescript/index.d.ts.map +1 -1
  185. package/lib/typescript/policy/labels.d.ts.map +1 -1
  186. package/lib/typescript/policy/policy.d.ts.map +1 -1
  187. package/lib/typescript/policy/redact.d.ts.map +1 -1
  188. package/lib/typescript/providers/anthropic.d.ts.map +1 -1
  189. package/lib/typescript/providers/openai.d.ts +0 -16
  190. package/lib/typescript/providers/openai.d.ts.map +1 -1
  191. package/lib/typescript/providers/problem.d.ts +50 -0
  192. package/lib/typescript/providers/problem.d.ts.map +1 -0
  193. package/lib/typescript/providers/sse.d.ts +86 -2
  194. package/lib/typescript/providers/sse.d.ts.map +1 -1
  195. package/lib/typescript/providers/streamTimer.d.ts +59 -0
  196. package/lib/typescript/providers/streamTimer.d.ts.map +1 -0
  197. package/lib/typescript/providers/transport.d.ts +48 -0
  198. package/lib/typescript/providers/transport.d.ts.map +1 -0
  199. package/lib/typescript/providers/types.d.ts +105 -3
  200. package/lib/typescript/providers/types.d.ts.map +1 -1
  201. package/lib/typescript/providers/xhrStream.d.ts +45 -0
  202. package/lib/typescript/providers/xhrStream.d.ts.map +1 -0
  203. package/lib/typescript/session.d.ts +55 -4
  204. package/lib/typescript/session.d.ts.map +1 -1
  205. package/lib/typescript/types.d.ts +6 -0
  206. package/lib/typescript/types.d.ts.map +1 -1
  207. package/package.json +10 -10
  208. package/LICENSE +0 -58
@@ -9,15 +9,24 @@
9
9
  * TV-CLI lanes never had it. So `readSse` degrades to reading the whole body
10
10
  * and replaying it as events rather than failing: a non-streaming answer is a
11
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.
12
20
  */
13
21
 
14
- function* parseChunk(buffer) {
22
+ function* parseChunk(buffer, onFrame) {
15
23
  let rest = buffer;
16
24
  let idx;
17
25
  // Frames are separated by a blank line. \r\n\r\n is legal too.
18
26
  while ((idx = rest.search(/\r?\n\r?\n/)) !== -1) {
19
27
  const raw = rest.slice(0, idx);
20
28
  rest = rest.slice(idx + (rest[idx] === "\r" ? 4 : 2));
29
+ onFrame?.();
21
30
  let event;
22
31
  const dataLines = [];
23
32
  for (const line of raw.split(/\r?\n/)) {
@@ -31,53 +40,124 @@ function* parseChunk(buffer) {
31
40
  }
32
41
  return rest;
33
42
  }
34
- export async function* readSse(response) {
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;
35
89
  const body = response.body;
90
+ const leftover = {
91
+ rest: ""
92
+ };
36
93
  if (!body || typeof body.getReader !== "function") {
37
- // No streaming on this platform — read it all, then replay.
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).
38
97
  const text = await response.text();
39
- let rest = "";
40
- const gen = parseChunk(text + "\n\n");
41
- let next = gen.next();
42
- while (!next.done) {
43
- yield next.value;
44
- next = gen.next();
45
- }
46
- rest = next.value;
47
- if (rest.trim()) {
48
- const tail = parseChunk(rest + "\n\n");
49
- let t = tail.next();
50
- while (!t.done) {
51
- yield t.value;
52
- t = tail.next();
53
- }
54
- }
98
+ yield* drain(text, leftover, onFrame);
99
+ if (status && leftover.rest.trim()) status.truncated = true;
55
100
  return;
56
101
  }
57
102
  const reader = body.getReader();
58
103
  const decoder = new TextDecoder();
59
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(() => {});
60
126
  try {
61
127
  for (;;) {
128
+ const read = reader.read();
62
129
  const {
63
130
  done,
64
131
  value
65
- } = await reader.read();
132
+ } = aborted ? await Promise.race([read, aborted]) : await read;
66
133
  if (done) break;
67
134
  buffer += decoder.decode(value, {
68
135
  stream: true
69
136
  });
70
- const gen = parseChunk(buffer);
71
- let next = gen.next();
72
- while (!next.done) {
73
- yield next.value;
74
- next = gen.next();
75
- }
76
- buffer = next.value;
137
+ yield* drain(buffer, leftover, onFrame);
138
+ buffer = leftover.rest;
77
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;
78
147
  } finally {
79
- // Cancelling on abort keeps the socket from lingering after the user
80
- // closes the sheet mid-answer.
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
+ }
81
161
  try {
82
162
  reader.releaseLock();
83
163
  } catch {
@@ -86,14 +166,66 @@ export async function* readSse(response) {
86
166
  }
87
167
  }
88
168
 
89
- /** Body text for a failed response, trimmed to something a user can read. */
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
+ */
90
200
  export async function errorBody(response) {
91
- let detail = "";
92
201
  try {
93
- detail = (await response.text()).slice(0, 600);
202
+ return (await response.text()).slice(0, 600);
94
203
  } catch {
95
- /* body already consumed or unreadable */
204
+ return ""; // body already consumed or unreadable
96
205
  }
97
- return `HTTP ${response.status}${detail ? ` — ${detail}` : ""}`;
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}`;
98
230
  }
99
231
  //# sourceMappingURL=sse.js.map
@@ -1 +1 @@
1
- {"version":3,"names":["parseChunk","buffer","rest","idx","search","raw","slice","event","dataLines","line","split","startsWith","trim","push","replace","length","data","join","readSse","response","body","getReader","text","gen","next","done","value","tail","t","reader","decoder","TextDecoder","read","decode","stream","releaseLock","errorBody","detail","status"],"sourceRoot":"../../../src","sources":["providers/sse.ts"],"mappings":";;AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAOA,UAAUA,UAAUA,CAACC,MAAc,EAAqC;EACtE,IAAIC,IAAI,GAAGD,MAAM;EACjB,IAAIE,GAAW;EACf;EACA,OAAO,CAACA,GAAG,GAAGD,IAAI,CAACE,MAAM,CAAC,YAAY,CAAC,MAAM,CAAC,CAAC,EAAE;IAC/C,MAAMC,GAAG,GAAGH,IAAI,CAACI,KAAK,CAAC,CAAC,EAAEH,GAAG,CAAC;IAC9BD,IAAI,GAAGA,IAAI,CAACI,KAAK,CAACH,GAAG,IAAID,IAAI,CAACC,GAAG,CAAC,KAAK,IAAI,GAAG,CAAC,GAAG,CAAC,CAAC,CAAC;IAErD,IAAII,KAAyB;IAC7B,MAAMC,SAAmB,GAAG,EAAE;IAC9B,KAAK,MAAMC,IAAI,IAAIJ,GAAG,CAACK,KAAK,CAAC,OAAO,CAAC,EAAE;MACrC,IAAID,IAAI,CAACE,UAAU,CAAC,GAAG,CAAC,EAAE,SAAS,CAAC;MACpC,IAAIF,IAAI,CAACE,UAAU,CAAC,QAAQ,CAAC,EAAEJ,KAAK,GAAGE,IAAI,CAACH,KAAK,CAAC,CAAC,CAAC,CAACM,IAAI,CAAC,CAAC,CAAC,KACvD,IAAIH,IAAI,CAACE,UAAU,CAAC,OAAO,CAAC,EAAEH,SAAS,CAACK,IAAI,CAACJ,IAAI,C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M,GAAG,EAAE;EACf,IAAI;IACF,SAAS;MACP,MAAM;QAAEwB,IAAI;QAAEC;MAAM,CAAC,GAAG,MAAMG,MAAM,CAACG,IAAI,CAAC,CAAC;MAC3C,IAAIP,IAAI,EAAE;MACVxB,MAAM,IAAI6B,OAAO,CAACG,MAAM,CAACP,KAAK,EAAE;QAAEQ,MAAM,EAAE;MAAK,CAAC,CAAC;MACjD,MAAMX,GAAG,GAAGvB,UAAU,CAACC,MAAM,CAAC;MAC9B,IAAIuB,IAAI,GAAGD,GAAG,CAACC,IAAI,CAAC,CAAC;MACrB,OAAO,CAACA,IAAI,CAACC,IAAI,EAAE;QACjB,MAAMD,IAAI,CAACE,KAAK;QAChBF,IAAI,GAAGD,GAAG,CAACC,IAAI,CAAC,CAAC;MACnB;MACAvB,MAAM,GAAGuB,IAAI,CAACE,KAAK;IACrB;EACF,CAAC,SAAS;IACR;IACA;IACA,IAAI;MACFG,MAAM,CAACM,WAAW,CAAC,CAAC;IACtB,CAAC,CAAC,MAAM;MACN;IAAA;EAEJ;AACF;;AAEA;AACA,OAAO,eAAeC,SAASA,CAACjB,QAAkB,EAAmB;EACnE,IAAIkB,MAAM,GAAG,EAAE;EACf,IAAI;IACFA,MAAM,GAAG,CAAC,MAAMlB,QAAQ,CAACG,IAAI,CAAC,CAAC,EAAEhB,KAAK,CAAC,CAAC,EAAE,GAAG,CAAC;EAChD,CAAC,CAAC,MAAM;IACN;EAAA;EAEF,OAAO,QAAQa,QAAQ,CAACmB,MAAM,GAAGD,MAAM,GAAG,MAAMA,MAAM,EAAE,GAAG,EAAE,EAAE;AACjE","ignoreList":[]}
1
+ {"version":3,"names":["parseChunk","buffer","onFrame","rest","idx","search","raw","slice","event","dataLines","line","split","startsWith","trim","push","replace","length","data","join","drain","out","gen","next","done","value","framesFrom","chunks","options","status","leftover","chunk","truncated","readSse","response","signal","body","getReader","text","reader","decoder","TextDecoder","finished","aborted","Promise","_resolve","reject","fail","err","Error","name","addEventListener","once","undefined","catch","read","race","decode","stream","resolve","cancel","releaseLock","nonStreamProblem","type","headers","get","lowered","toLowerCase","includes","detail","errorBody","truncatedMessage","unparseableMessage","args","stopReason","tail"],"sourceRoot":"../../../src","sources":["providers/sse.ts"],"mappings":";;AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AA2CA,UAAUA,UAAUA,CAClBC,MAAc,EACdC,OAAoB,EACe;EACnC,IAAIC,IAAI,GAAGF,MAAM;EACjB,IAAIG,GAAW;EACf;EACA,OAAO,CAACA,GAAG,GAAGD,IAAI,CAACE,MAAM,CAAC,YAAY,CAAC,MAAM,CAAC,CAAC,EAAE;IAC/C,MAAMC,GAAG,GAAGH,IAAI,CAACI,KAAK,CAAC,CAAC,EAAEH,GAAG,CAAC;IAC9BD,IAAI,GAAGA,IAAI,CAACI,KAAK,CAACH,GAAG,IAAID,IAAI,CAACC,GAAG,CAAC,KAAK,IAAI,GAAG,CAAC,GAAG,CAAC,CAAC,CAAC;IACrDF,OAAO,GAAG,CAAC;IAEX,IAAIM,KAAyB;IAC7B,MAAMC,SAAmB,GAAG,EAAE;IAC9B,KAAK,MAAMC,IAAI,IAAIJ,GAAG,CAACK,KAAK,CAAC,OAAO,CAAC,EAAE;MACrC,IAAID,IAAI,CAACE,UAAU,CAAC,GAAG,CAAC,EAAE,SAAS,CAAC;MACpC,IAAIF,IAAI,CAACE,UAAU,CAAC,QAAQ,CAAC,EAAEJ,KAAK,GAAGE,IAAI,CAACH,KAAK,CAAC,CAAC,CAAC,CAACM,IAAI,CAAC,CAAC,CAAC,KACvD,IAAIH,IAAI,CAACE,UAAU,CAAC,OAAO,CAAC,EAAEH,SAAS,CAACK,IAAI,CAACJ,IAAI,CAACH,KAAK,CAAC,CAAC,CAAC,CAACQ,OAAO,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC;IACpF;IACA,IAAIN,SAAS,CAACO,MAAM,EAAE,MAAM;MAAER,KAA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,CAACjE,KAAK,CAAC,CAAC,GAAG,CAAC,EAAE,GAAGiE,IAAI;EAC9D,OACE,wCAAwCvB,IAAI,EAAE,IAC7ClB,SAAS,GAAG,+DAA+D,GAAG,EAAE,CAAC,GAClF,WAAW0C,UAAU,IAAI,MAAM,KAAKD,IAAI,CAACxD,MAAM,WAAW0D,IAAI,EAAE;AAEpE","ignoreList":[]}
@@ -0,0 +1,118 @@
1
+ "use strict";
2
+
3
+ /**
4
+ * The clock on a model request.
5
+ *
6
+ * There was none. A gateway that accepted the socket and then said nothing —
7
+ * an overloaded proxy, a dropped VPN, a laptop that slept mid-answer — left
8
+ * the turn running with the thinking strip animating until a person pressed
9
+ * Stop. The 180s turn deadline did not help: it is read at the TOP of a step
10
+ * (`runAgentTurn`), and a step parked inside `await fetch(...)` never reaches
11
+ * the next one.
12
+ *
13
+ * Two clocks, because the two silences mean different things:
14
+ *
15
+ * - CONNECT covers "no headers yet". Nothing has been generated, so nothing is
16
+ * lost by giving up; 30s is already long for a gateway to acknowledge a POST.
17
+ * - IDLE covers "headers arrived, then nothing". This one has to be generous.
18
+ * Anthropic sends `ping` frames, but an OpenAI-protocol reasoning model can
19
+ * legitimately emit nothing for a long stretch while it thinks, and cutting
20
+ * that off would look like a broken product. Any frame at all — a keepalive
21
+ * comment included — resets it, so only total silence trips it.
22
+ *
23
+ * A timeout is NOT a retry. Nothing is re-sent: the request may have reached
24
+ * the model, and a duplicate turn is a duplicate bill and possibly duplicate
25
+ * writes. The turn ends with an honest notice and a Continue button.
26
+ */
27
+
28
+ export const DEFAULT_CONNECT_TIMEOUT_MS = 30_000;
29
+ export const DEFAULT_IDLE_TIMEOUT_MS = 90_000;
30
+ /**
31
+ * A timer over an optional caller signal.
32
+ *
33
+ * When no `AbortController` exists (a runtime old enough to lack it) this
34
+ * degrades to passing the caller's signal straight through with no clocks,
35
+ * rather than failing to send anything at all.
36
+ */
37
+ export function createStreamTimer(callerSignal, options = {}) {
38
+ const connectMs = options.connectTimeoutMs ?? DEFAULT_CONNECT_TIMEOUT_MS;
39
+ const idleMs = options.idleTimeoutMs ?? DEFAULT_IDLE_TIMEOUT_MS;
40
+ const Controller = typeof AbortController === "function" ? AbortController : undefined;
41
+ if (!Controller) {
42
+ return {
43
+ signal: callerSignal,
44
+ connected() {},
45
+ activity() {},
46
+ expired: undefined,
47
+ message: () => undefined,
48
+ dispose() {}
49
+ };
50
+ }
51
+ const controller = new Controller();
52
+ let expired;
53
+ let handle;
54
+ let disposed = false;
55
+ const clear = () => {
56
+ if (handle !== undefined) {
57
+ clearTimeout(handle);
58
+ handle = undefined;
59
+ }
60
+ };
61
+ const arm = (ms, which) => {
62
+ clear();
63
+ if (disposed || !Number.isFinite(ms) || ms <= 0) return;
64
+ handle = setTimeout(() => {
65
+ handle = undefined;
66
+ expired = which;
67
+ // The abort is what actually unblocks `fetch` / `reader.read()`. The
68
+ // reason is tracked here rather than read back off the signal because
69
+ // `AbortSignal.reason` is not reliably present on React Native.
70
+ try {
71
+ controller.abort();
72
+ } catch {
73
+ /* already aborted */
74
+ }
75
+ }, ms);
76
+ };
77
+ const onCallerAbort = () => {
78
+ clear();
79
+ try {
80
+ controller.abort();
81
+ } catch {
82
+ /* already aborted */
83
+ }
84
+ };
85
+ if (callerSignal) {
86
+ if (callerSignal.aborted) onCallerAbort();else callerSignal.addEventListener?.("abort", onCallerAbort);
87
+ }
88
+ arm(connectMs, "connect");
89
+ return {
90
+ signal: controller.signal,
91
+ connected() {
92
+ arm(idleMs, "idle");
93
+ },
94
+ activity() {
95
+ // Only rearm a clock that is already running: a provider that reports
96
+ // activity after `dispose` must not resurrect the timer.
97
+ if (!disposed) arm(idleMs, "idle");
98
+ },
99
+ get expired() {
100
+ return expired;
101
+ },
102
+ message() {
103
+ if (expired === "connect") {
104
+ return `The model endpoint did not respond within ${Math.round(connectMs / 1000)}s. Nothing was generated, and nothing was re-sent.`;
105
+ }
106
+ if (expired === "idle") {
107
+ return `The model endpoint stopped sending after ${Math.round(idleMs / 1000)}s of silence. Whatever arrived is above; nothing was re-sent.`;
108
+ }
109
+ return undefined;
110
+ },
111
+ dispose() {
112
+ disposed = true;
113
+ clear();
114
+ callerSignal?.removeEventListener?.("abort", onCallerAbort);
115
+ }
116
+ };
117
+ }
118
+ //# sourceMappingURL=streamTimer.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":["DEFAULT_CONNECT_TIMEOUT_MS","DEFAULT_IDLE_TIMEOUT_MS","createStreamTimer","callerSignal","options","connectMs","connectTimeoutMs","idleMs","idleTimeoutMs","Controller","AbortController","undefined","signal","connected","activity","expired","message","dispose","controller","handle","disposed","clear","clearTimeout","arm","ms","which","Number","isFinite","setTimeout","abort","onCallerAbort","aborted","addEventListener","Math","round","removeEventListener"],"sourceRoot":"../../../src","sources":["providers/streamTimer.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;;AAEA,OAAO,MAAMA,0BAA0B,GAAG,MAAM;AAChD,OAAO,MAAMC,uBAAuB,GAAG,MAAM;AA4B7C;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASC,iBAAiBA,CAC/BC,YAAqC,EACrCC,OAA2B,GAAG,CAAC,CAAC,EACnB;EACb,MAAMC,SAAS,GAAGD,OAAO,CAACE,gBAAgB,IAAIN,0BAA0B;EACxE,MAAMO,MAAM,GAAGH,OAAO,CAACI,aAAa,IAAIP,uBAAuB;EAE/D,MAAMQ,UAAU,GAAG,OAAOC,eAAe,KAAK,UAAU,GAAGA,eAAe,GAAGC,SAAS;EACtF,IAAI,CAACF,UAAU,EAAE;IACf,OAAO;MACLG,MAAM,EAAET,YAAY;MACpBU,SAASA,CAAA,EAAG,CAAC,CAAC;MACdC,QAAQA,CAAA,EAAG,CAAC,CAAC;MACbC,OAAO,EAAEJ,SAAS;MAClBK,OAAO,EAAEA,CAAA,KAAML,SAAS;MAC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DO,QAAQA,CAAA,EAAG;MACT;MACA;MACA,IAAI,CAACM,QAAQ,EAAEG,GAAG,CAAChB,MAAM,EAAE,MAAM,CAAC;IACpC,CAAC;IACD,IAAIQ,OAAOA,CAAA,EAAG;MACZ,OAAOA,OAAO;IAChB,CAAC;IACDC,OAAOA,CAAA,EAAG;MACR,IAAID,OAAO,KAAK,SAAS,EAAE;QACzB,OAAO,6CAA6CkB,IAAI,CAACC,KAAK,CAAC7B,SAAS,GAAG,IAAI,CAAC,oDAAoD;MACtI;MACA,IAAIU,OAAO,KAAK,MAAM,EAAE;QACtB,OAAO,4CAA4CkB,IAAI,CAACC,KAAK,CAAC3B,MAAM,GAAG,IAAI,CAAC,+DAA+D;MAC7I;MACA,OAAOI,SAAS;IAClB,CAAC;IACDM,OAAOA,CAAA,EAAG;MACRG,QAAQ,GAAG,IAAI;MACfC,KAAK,CAAC,CAAC;MACPlB,YAAY,EAAEgC,mBAAmB,GAAG,OAAO,EAAEL,aAAa,CAAC;IAC7D;EACF,CAAC;AACH","ignoreList":[]}
@@ -0,0 +1,119 @@
1
+ "use strict";
2
+
3
+ /**
4
+ * How a request is SENT, decided before it is sent.
5
+ *
6
+ * The obvious place to add XHR was inside `readSse`, once a missing
7
+ * `response.body` had already proved that `fetch` could not stream. That is
8
+ * the wrong boundary and would have been a real bug: the POST has already
9
+ * happened by then, the reader has neither the URL nor the headers nor the
10
+ * body to repeat it, and issuing a second one would ask the model to generate
11
+ * the same answer twice — billed twice, and for a turn that writes, acted on
12
+ * twice. Transport is chosen up front; a response that has started is consumed
13
+ * exactly once, whatever it turns out to be.
14
+ *
15
+ * `auto` therefore learns rather than probes. The first request goes through
16
+ * `fetch`; if that response has no reader, the answer is buffered (as it
17
+ * always was on that runtime) and the SESSION remembers, so every later
18
+ * request uses XHR and streams. One buffered reply at the start of a bare-RN
19
+ * session, and nothing to configure.
20
+ */
21
+
22
+ import { errorBody, nonStreamProblem, readSse } from "./sse";
23
+ import { xhrAvailable, xhrStream } from "./xhrStream";
24
+ import { classifyProblem, errorTypeOf } from "./problem";
25
+ const EMPTY = async function* () {};
26
+
27
+ /**
28
+ * One transport per provider instance, because the `auto` decision is worth
29
+ * remembering: re-learning it would buffer the first reply of every turn.
30
+ */
31
+ export function createTransport(config) {
32
+ const requested = config.transport ?? "auto";
33
+ /**
34
+ * Set once a `fetch` response has come back with no reader. Only ever
35
+ * flipped one way: a runtime does not gain streaming mid-session.
36
+ */
37
+ let fetchCannotStream = false;
38
+ const useXhr = () => {
39
+ // A host-supplied `fetch` is never bypassed. It is how a harness keeps
40
+ // model traffic off the global (and out of the network interceptor), and
41
+ // routing around it would defeat the reason it was passed in.
42
+ if (config.fetch) return requested === "xhr" && xhrAvailable();
43
+ if (requested === "xhr") return xhrAvailable();
44
+ if (requested === "fetch") return false;
45
+ return fetchCannotStream && xhrAvailable();
46
+ };
47
+ return {
48
+ async open(req) {
49
+ if (useXhr()) {
50
+ const response = await xhrStream({
51
+ url: config.endpoint,
52
+ headers: req.headers,
53
+ body: req.body,
54
+ signal: req.signal
55
+ });
56
+ if (!response.ok) {
57
+ const detail = (await response.text()).slice(0, 600);
58
+ return {
59
+ problem: `HTTP ${response.status}${detail ? ` — ${detail}` : ""}`,
60
+ problemInfo: classifyProblem({
61
+ status: response.status,
62
+ type: errorTypeOf(detail),
63
+ message: detail,
64
+ retryAfter: response.retryAfter
65
+ }),
66
+ frames: EMPTY
67
+ };
68
+ }
69
+ const type = response.contentType.toLowerCase();
70
+ if (type && !type.includes("event-stream")) {
71
+ const detail = (await response.text()).slice(0, 600);
72
+ return {
73
+ problem: type.includes("json") ? `The endpoint answered with JSON instead of a stream — it may not support "stream": true.${detail ? ` — ${detail}` : ""}` : `The endpoint answered with ${response.contentType} instead of an event stream.${detail ? ` — ${detail}` : ""}`,
74
+ frames: EMPTY
75
+ };
76
+ }
77
+ return {
78
+ frames: options => response.frames(options)
79
+ };
80
+ }
81
+ const response = await (config.fetch ?? fetch)(config.endpoint, {
82
+ method: "POST",
83
+ headers: req.headers,
84
+ signal: req.signal,
85
+ body: req.body
86
+ });
87
+ if (!response.ok) {
88
+ const detail = await errorBody(response);
89
+ return {
90
+ problem: `HTTP ${response.status}${detail ? ` — ${detail}` : ""}`,
91
+ problemInfo: classifyProblem({
92
+ status: response.status,
93
+ type: errorTypeOf(detail),
94
+ message: detail,
95
+ retryAfter: response.headers?.get?.("retry-after") ?? null
96
+ }),
97
+ frames: EMPTY
98
+ };
99
+ }
100
+ const problem = await nonStreamProblem(response);
101
+ if (problem) return {
102
+ problem,
103
+ frames: EMPTY
104
+ };
105
+
106
+ // Learn, for the NEXT request. This one is already in flight and is read
107
+ // through the buffered path — re-sending it is what this file exists to
108
+ // avoid.
109
+ const body = response.body;
110
+ if ((!body || typeof body.getReader !== "function") && !config.fetch) {
111
+ fetchCannotStream = true;
112
+ }
113
+ return {
114
+ frames: options => readSse(response, options)
115
+ };
116
+ }
117
+ };
118
+ }
119
+ //# sourceMappingURL=transport.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":["errorBody","nonStreamProblem","readSse","xhrAvailable","xhrStream","classifyProblem","errorTypeOf","EMPTY","createTransport","config","requested","transport","fetchCannotStream","useXhr","fetch","open","req","response","url","endpoint","headers","body","signal","ok","detail","text","slice","problem","status","problemInfo","type","message","retryAfter","frames","contentType","toLowerCase","includes","options","method","get","getReader"],"sourceRoot":"../../../src","sources":["providers/transport.ts"],"mappings":";;AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAEA,SACEA,SAAS,EACTC,gBAAgB,EAChBC,OAAO,QAGF,OAAO;AACd,SAASC,YAAY,EAAEC,SAAS,QAAQ,aAAa;AACrD,SAASC,eAAe,EAAEC,WAAW,QAA4B,WAAW;AA2B5E,MAAMC,KAAK,GAAG,gBAAAA,CAAA,EAAyD,CAAC,CAAC;;AAEzE;AACA;AACA;AACA;AACA,OAAO,SAASC,eAAeA,CAACC,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