@buoy-gg/agent-core 7.0.41 → 7.0.43

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 (201) 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 -4556
  6. package/lib/commonjs/catalog/catalog.source.json +249 -0
  7. package/lib/commonjs/catalog/catalog.types.g.js +1 -46
  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 -345
  16. package/lib/commonjs/engine/askGate.js +1 -287
  17. package/lib/commonjs/engine/effectFor.js +1 -223
  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 -1836
  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 -4552
  46. package/lib/module/catalog/catalog.source.json +249 -0
  47. package/lib/module/catalog/catalog.types.g.js +1 -42
  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 -339
  56. package/lib/module/engine/askGate.js +1 -280
  57. package/lib/module/engine/effectFor.js +1 -220
  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 -1827
  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 +3 -2
  83. package/lib/typescript/providers/problem.d.ts +0 -20
  84. package/lib/typescript/providers/streamTimer.d.ts +0 -24
  85. package/lib/typescript/realNow.d.ts +18 -0
  86. package/lib/web/index.mjs +52 -53
  87. package/package.json +1 -1
  88. package/lib/commonjs/blocks/receipts.js.map +0 -1
  89. package/lib/commonjs/blocks/runId.js.map +0 -1
  90. package/lib/commonjs/blocks/types.js.map +0 -1
  91. package/lib/commonjs/blocks/uiTool.js.map +0 -1
  92. package/lib/commonjs/catalog/catalog.g.js.map +0 -1
  93. package/lib/commonjs/catalog/catalog.types.g.js.map +0 -1
  94. package/lib/commonjs/catalog/normalizeParams.js.map +0 -1
  95. package/lib/commonjs/catalog/signature.js.map +0 -1
  96. package/lib/commonjs/catalog/snapshotReads.js.map +0 -1
  97. package/lib/commonjs/catalog/toProviderTools.js.map +0 -1
  98. package/lib/commonjs/catalog/validateParams.js.map +0 -1
  99. package/lib/commonjs/context/buildContextPack.js.map +0 -1
  100. package/lib/commonjs/effects/digest.js.map +0 -1
  101. package/lib/commonjs/effects/ledger.js.map +0 -1
  102. package/lib/commonjs/engine/askGate.js.map +0 -1
  103. package/lib/commonjs/engine/effectFor.js.map +0 -1
  104. package/lib/commonjs/engine/evidence.js.map +0 -1
  105. package/lib/commonjs/engine/historyBudget.js.map +0 -1
  106. package/lib/commonjs/engine/retrieve.js.map +0 -1
  107. package/lib/commonjs/engine/runAgentTurn.js.map +0 -1
  108. package/lib/commonjs/engine/systemPrompt.js.map +0 -1
  109. package/lib/commonjs/engine/textToolCalls.js.map +0 -1
  110. package/lib/commonjs/engine/tokenCalibration.js.map +0 -1
  111. package/lib/commonjs/engine/verify.js.map +0 -1
  112. package/lib/commonjs/index.js.map +0 -1
  113. package/lib/commonjs/policy/labels.js.map +0 -1
  114. package/lib/commonjs/policy/policy.js.map +0 -1
  115. package/lib/commonjs/policy/redact.js.map +0 -1
  116. package/lib/commonjs/providers/anthropic.js.map +0 -1
  117. package/lib/commonjs/providers/openai.js.map +0 -1
  118. package/lib/commonjs/providers/problem.js.map +0 -1
  119. package/lib/commonjs/providers/sse.js.map +0 -1
  120. package/lib/commonjs/providers/streamTimer.js.map +0 -1
  121. package/lib/commonjs/providers/transport.js.map +0 -1
  122. package/lib/commonjs/providers/types.js.map +0 -1
  123. package/lib/commonjs/providers/xhrStream.js.map +0 -1
  124. package/lib/commonjs/session.js.map +0 -1
  125. package/lib/commonjs/types.js.map +0 -1
  126. package/lib/module/blocks/receipts.js.map +0 -1
  127. package/lib/module/blocks/runId.js.map +0 -1
  128. package/lib/module/blocks/types.js.map +0 -1
  129. package/lib/module/blocks/uiTool.js.map +0 -1
  130. package/lib/module/catalog/catalog.g.js.map +0 -1
  131. package/lib/module/catalog/catalog.types.g.js.map +0 -1
  132. package/lib/module/catalog/normalizeParams.js.map +0 -1
  133. package/lib/module/catalog/signature.js.map +0 -1
  134. package/lib/module/catalog/snapshotReads.js.map +0 -1
  135. package/lib/module/catalog/toProviderTools.js.map +0 -1
  136. package/lib/module/catalog/validateParams.js.map +0 -1
  137. package/lib/module/context/buildContextPack.js.map +0 -1
  138. package/lib/module/effects/digest.js.map +0 -1
  139. package/lib/module/effects/ledger.js.map +0 -1
  140. package/lib/module/engine/askGate.js.map +0 -1
  141. package/lib/module/engine/effectFor.js.map +0 -1
  142. package/lib/module/engine/evidence.js.map +0 -1
  143. package/lib/module/engine/historyBudget.js.map +0 -1
  144. package/lib/module/engine/retrieve.js.map +0 -1
  145. package/lib/module/engine/runAgentTurn.js.map +0 -1
  146. package/lib/module/engine/systemPrompt.js.map +0 -1
  147. package/lib/module/engine/textToolCalls.js.map +0 -1
  148. package/lib/module/engine/tokenCalibration.js.map +0 -1
  149. package/lib/module/engine/verify.js.map +0 -1
  150. package/lib/module/index.js.map +0 -1
  151. package/lib/module/policy/labels.js.map +0 -1
  152. package/lib/module/policy/policy.js.map +0 -1
  153. package/lib/module/policy/redact.js.map +0 -1
  154. package/lib/module/providers/anthropic.js.map +0 -1
  155. package/lib/module/providers/openai.js.map +0 -1
  156. package/lib/module/providers/problem.js.map +0 -1
  157. package/lib/module/providers/sse.js.map +0 -1
  158. package/lib/module/providers/streamTimer.js.map +0 -1
  159. package/lib/module/providers/transport.js.map +0 -1
  160. package/lib/module/providers/types.js.map +0 -1
  161. package/lib/module/providers/xhrStream.js.map +0 -1
  162. package/lib/module/session.js.map +0 -1
  163. package/lib/module/types.js.map +0 -1
  164. package/lib/typescript/blocks/receipts.d.ts.map +0 -1
  165. package/lib/typescript/blocks/runId.d.ts.map +0 -1
  166. package/lib/typescript/blocks/types.d.ts.map +0 -1
  167. package/lib/typescript/blocks/uiTool.d.ts.map +0 -1
  168. package/lib/typescript/catalog/catalog.g.d.ts.map +0 -1
  169. package/lib/typescript/catalog/catalog.types.g.d.ts.map +0 -1
  170. package/lib/typescript/catalog/normalizeParams.d.ts.map +0 -1
  171. package/lib/typescript/catalog/signature.d.ts.map +0 -1
  172. package/lib/typescript/catalog/snapshotReads.d.ts.map +0 -1
  173. package/lib/typescript/catalog/toProviderTools.d.ts.map +0 -1
  174. package/lib/typescript/catalog/validateParams.d.ts.map +0 -1
  175. package/lib/typescript/context/buildContextPack.d.ts.map +0 -1
  176. package/lib/typescript/effects/digest.d.ts.map +0 -1
  177. package/lib/typescript/effects/ledger.d.ts.map +0 -1
  178. package/lib/typescript/engine/askGate.d.ts.map +0 -1
  179. package/lib/typescript/engine/effectFor.d.ts.map +0 -1
  180. package/lib/typescript/engine/evidence.d.ts.map +0 -1
  181. package/lib/typescript/engine/historyBudget.d.ts.map +0 -1
  182. package/lib/typescript/engine/retrieve.d.ts.map +0 -1
  183. package/lib/typescript/engine/runAgentTurn.d.ts.map +0 -1
  184. package/lib/typescript/engine/systemPrompt.d.ts.map +0 -1
  185. package/lib/typescript/engine/textToolCalls.d.ts.map +0 -1
  186. package/lib/typescript/engine/tokenCalibration.d.ts.map +0 -1
  187. package/lib/typescript/engine/verify.d.ts.map +0 -1
  188. package/lib/typescript/index.d.ts.map +0 -1
  189. package/lib/typescript/policy/labels.d.ts.map +0 -1
  190. package/lib/typescript/policy/policy.d.ts.map +0 -1
  191. package/lib/typescript/policy/redact.d.ts.map +0 -1
  192. package/lib/typescript/providers/anthropic.d.ts.map +0 -1
  193. package/lib/typescript/providers/openai.d.ts.map +0 -1
  194. package/lib/typescript/providers/problem.d.ts.map +0 -1
  195. package/lib/typescript/providers/sse.d.ts.map +0 -1
  196. package/lib/typescript/providers/streamTimer.d.ts.map +0 -1
  197. package/lib/typescript/providers/transport.d.ts.map +0 -1
  198. package/lib/typescript/providers/types.d.ts.map +0 -1
  199. package/lib/typescript/providers/xhrStream.d.ts.map +0 -1
  200. package/lib/typescript/session.d.ts.map +0 -1
  201. package/lib/typescript/types.d.ts.map +0 -1
@@ -1,118 +1 @@
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
1
+ "use strict";import{realClearTimeout as T,realSetTimeout as m}from"../realNow";export const DEFAULT_CONNECT_TIMEOUT_MS=3e4;export const DEFAULT_IDLE_TIMEOUT_MS=9e4;export function createStreamTimer(e,u={}){const f=u.connectTimeoutMs??DEFAULT_CONNECT_TIMEOUT_MS;const r=u.idleTimeoutMs??DEFAULT_IDLE_TIMEOUT_MS;const l=typeof AbortController==="function"?AbortController:void 0;if(!l){return{signal:e,connected(){},activity(){},expired:void 0,message:()=>void 0,dispose(){}}}const o=new l;let n;let t;let i=false;const d=()=>{if(t!==void 0){T(t);t=void 0}};const s=(a,p)=>{d();if(i||!Number.isFinite(a)||a<=0)return;t=m(()=>{t=void 0;n=p;try{o.abort()}catch{}},a)};const c=()=>{d();try{o.abort()}catch{}};if(e){if(e.aborted)c();else e.addEventListener?.("abort",c)}s(f,"connect");return{signal:o.signal,connected(){s(r,"idle")},activity(){if(!i)s(r,"idle")},get expired(){return n},message(){if(n==="connect"){return`The model endpoint did not respond within ${Math.round(f/1e3)}s. Nothing was generated, and nothing was re-sent.`}if(n==="idle"){return`The model endpoint stopped sending after ${Math.round(r/1e3)}s of silence. Whatever arrived is above; nothing was re-sent.`}return void 0},dispose(){i=true;d();e?.removeEventListener?.("abort",c)}}}
@@ -1,119 +1 @@
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
1
+ "use strict";import{errorBody as y,nonStreamProblem as h,readSse as b}from"./sse";import{xhrAvailable as f,xhrStream as T}from"./xhrStream";import{classifyProblem as l,errorTypeOf as p}from"./problem";const n=async function*(){};export function createTransport(s){const o=s.transport??"auto";let m=false;const c=()=>{if(s.fetch)return o==="xhr"&&f();if(o==="xhr")return f();if(o==="fetch")return false;return m&&f()};return{async open(a){if(c()){const e=await T({url:s.endpoint,headers:a.headers,body:a.body,signal:a.signal});if(!e.ok){const t=(await e.text()).slice(0,600);return{problem:`HTTP ${e.status}${t?` \u2014 ${t}`:""}`,problemInfo:l({status:e.status,type:p(t),message:t,retryAfter:e.retryAfter}),frames:n}}const i=e.contentType.toLowerCase();if(i&&!i.includes("event-stream")){const t=(await e.text()).slice(0,600);return{problem:i.includes("json")?`The endpoint answered with JSON instead of a stream \u2014 it may not support "stream": true.${t?` \u2014 ${t}`:""}`:`The endpoint answered with ${e.contentType} instead of an event stream.${t?` \u2014 ${t}`:""}`,frames:n}}return{frames:t=>e.frames(t)}}const r=await(s.fetch??fetch)(s.endpoint,{method:"POST",headers:a.headers,signal:a.signal,body:a.body});if(!r.ok){const e=await y(r);return{problem:`HTTP ${r.status}${e?` \u2014 ${e}`:""}`,problemInfo:l({status:r.status,type:p(e),message:e,retryAfter:r.headers?.get?.("retry-after")??null}),frames:n}}const u=await h(r);if(u)return{problem:u,frames:n};const d=r.body;if((!d||typeof d.getReader!=="function")&&!s.fetch){m=true}return{frames:e=>b(r,e)}}}}
@@ -1,4 +1 @@
1
- "use strict";
2
-
3
- export {};
4
- //# sourceMappingURL=types.js.map
1
+ "use strict";export{};
@@ -1,207 +1 @@
1
- "use strict";
2
-
3
- /**
4
- * Streaming over `XMLHttpRequest`, for the runtimes where `fetch` cannot.
5
- *
6
- * React Native core still has no `response.body`: its `fetch` is the whatwg
7
- * polyfill over XHR and resolves inside `xhr.onload`, i.e. after the ENTIRE
8
- * body. Under Expo the global is `expo/fetch`, which does stream — but
9
- * `EXPO_PUBLIC_USE_RN_FETCH=1` reverts that, and the bare-RN CLI, TV CLI and
10
- * NativeScript lanes never had it at all. On those, every Ask Buoy answer
11
- * appeared in one lump, however long the model took.
12
- *
13
- * XHR itself has streamed the whole time. `responseText` grows as bytes arrive
14
- * and `onprogress` fires as it does, so slicing from the last offset gives the
15
- * same incremental text a reader would. Two conditions, both easy to miss:
16
- * the progress handler must be installed BEFORE `send()`, and `responseType`
17
- * must stay text — anything else stops RN emitting progress at all.
18
- *
19
- * WHAT THIS IS NOT. It is not a second SSE parser: framing, the truncation
20
- * rule and the terminal-marker contract all live in `sse.ts` and are shared
21
- * verbatim. This file only produces text chunks.
22
- */
23
- import { framesFrom } from "./sse";
24
-
25
- /** Beyond this the answer is not an answer any more; stop reading. */
26
- const MAX_RESPONSE_CHARS = 8_000_000;
27
- export function xhrAvailable() {
28
- return typeof XMLHttpRequest === "function";
29
- }
30
-
31
- /**
32
- * Send the request and resolve once the HEADERS are in, so the caller can
33
- * check the status and content type before deciding to read a stream — the
34
- * same shape `fetch` offers, and the reason this can slot in beside it.
35
- */
36
- export function xhrStream(req) {
37
- return new Promise((resolveHeaders, rejectHeaders) => {
38
- const xhr = new XMLHttpRequest();
39
-
40
- /** Text seen but not yet handed to a consumer. */
41
- const queue = [];
42
- /** Resolvers for a consumer parked on an empty queue. */
43
- const waiters = [];
44
- let offset = 0;
45
- let done = false;
46
- let failure;
47
- let headersSeen = false;
48
- let capped = false;
49
- const wake = () => {
50
- while (waiters.length) waiters.shift()();
51
- };
52
- const pull = () => {
53
- // `responseText` is the WHOLE body so far, so the delta is everything
54
- // past what was already taken. RN keeps the full string either way;
55
- // the offset bounds the work, not the memory.
56
- let text;
57
- try {
58
- text = xhr.responseText ?? "";
59
- } catch {
60
- return; // Not readable yet on some runtimes; the next event will do.
61
- }
62
- if (text.length <= offset) return;
63
- if (text.length > MAX_RESPONSE_CHARS) {
64
- capped = true;
65
- failure = new Error("The endpoint sent more than this tool will read.");
66
- done = true;
67
- try {
68
- xhr.abort();
69
- } catch {
70
- /* already gone */
71
- }
72
- wake();
73
- return;
74
- }
75
- queue.push(text.slice(offset));
76
- offset = text.length;
77
- wake();
78
- };
79
- const settleHeaders = () => {
80
- if (headersSeen) return;
81
- headersSeen = true;
82
- let contentType = "";
83
- let retryAfter = null;
84
- try {
85
- contentType = xhr.getResponseHeader("content-type") ?? "";
86
- retryAfter = xhr.getResponseHeader("retry-after");
87
- } catch {
88
- contentType = "";
89
- }
90
- resolveHeaders({
91
- status: xhr.status,
92
- ok: xhr.status >= 200 && xhr.status < 300,
93
- contentType,
94
- retryAfter,
95
- async text() {
96
- // Whatever has arrived. For a non-2xx that is the error body; the
97
- // request is over by the time anyone asks.
98
- await new Promise(r => {
99
- if (done) return r();
100
- waiters.push(() => r());
101
- });
102
- try {
103
- return xhr.responseText ?? "";
104
- } catch {
105
- return "";
106
- }
107
- },
108
- frames(options) {
109
- return framesFrom(chunks(), options);
110
- }
111
- });
112
- };
113
- async function* chunks() {
114
- try {
115
- for (;;) {
116
- while (queue.length) yield queue.shift();
117
- if (done) break;
118
- await new Promise(r => waiters.push(r));
119
- }
120
- while (queue.length) yield queue.shift();
121
- if (failure) throw failure;
122
- } finally {
123
- if (!done) {
124
- // The consumer left early — a completed turn, a Stop, a timeout.
125
- // Nothing else will read this socket.
126
- try {
127
- xhr.abort();
128
- } catch {
129
- /* already gone */
130
- }
131
- }
132
- }
133
- }
134
- const onAbort = () => {
135
- try {
136
- xhr.abort();
137
- } catch {
138
- /* already gone */
139
- }
140
- };
141
- xhr.onreadystatechange = () => {
142
- // HEADERS_RECEIVED. Status and content type are known here, long before
143
- // the body finishes — which is the whole point of using XHR directly.
144
- if (xhr.readyState === 2) settleHeaders();
145
- };
146
- xhr.onprogress = () => {
147
- settleHeaders();
148
- pull();
149
- };
150
- xhr.onload = () => {
151
- settleHeaders();
152
- if (!capped) pull();
153
- done = true;
154
- wake();
155
- };
156
- xhr.onerror = () => {
157
- failure = new Error("The request failed before it finished.");
158
- done = true;
159
- if (!headersSeen) {
160
- headersSeen = true;
161
- rejectHeaders(failure);
162
- }
163
- wake();
164
- };
165
- xhr.onabort = () => {
166
- const err = new Error("Aborted");
167
- err.name = "AbortError";
168
- failure = err;
169
- done = true;
170
- if (!headersSeen) {
171
- headersSeen = true;
172
- rejectHeaders(err);
173
- }
174
- wake();
175
- };
176
- xhr.ontimeout = () => {
177
- failure = new Error("The request timed out.");
178
- done = true;
179
- if (!headersSeen) {
180
- headersSeen = true;
181
- rejectHeaders(failure);
182
- }
183
- wake();
184
- };
185
- try {
186
- xhr.open("POST", req.url, true);
187
- // Text, and nothing else: RN emits no progress events for any other
188
- // response type, which turns this back into a buffered read in silence.
189
- xhr.responseType = "text";
190
- for (const [k, v] of Object.entries(req.headers)) {
191
- try {
192
- xhr.setRequestHeader(k, v);
193
- } catch {
194
- /* a header the runtime forbids is not worth failing the turn over */
195
- }
196
- }
197
- if (req.signal) {
198
- if (req.signal.aborted) onAbort();else req.signal.addEventListener?.("abort", onAbort);
199
- }
200
- xhr.send(req.body);
201
- } catch (err) {
202
- headersSeen = true;
203
- rejectHeaders(err instanceof Error ? err : new Error(String(err)));
204
- }
205
- });
206
- }
207
- //# sourceMappingURL=xhrStream.js.map
1
+ "use strict";import{framesFrom as m}from"./sse";const x=8e6;export function xhrAvailable(){return typeof XMLHttpRequest==="function"}export function xhrStream(s){return new Promise((b,u)=>{const t=new XMLHttpRequest;const i=[];const l=[];let h=0;let n=false;let o;let r=false;let y=false;const a=()=>{while(l.length)l.shift()()};const d=()=>{let e;try{e=t.responseText??""}catch{return}if(e.length<=h)return;if(e.length>x){y=true;o=new Error("The endpoint sent more than this tool will read.");n=true;try{t.abort()}catch{}a();return}i.push(e.slice(h));h=e.length;a()};const p=()=>{if(r)return;r=true;let e="";let f=null;try{e=t.getResponseHeader("content-type")??"";f=t.getResponseHeader("retry-after")}catch{e=""}b({status:t.status,ok:t.status>=200&&t.status<300,contentType:e,retryAfter:f,async text(){await new Promise(c=>{if(n)return c();l.push(()=>c())});try{return t.responseText??""}catch{return""}},frames(c){return m(g(),c)}})};async function*g(){try{for(;;){while(i.length)yield i.shift();if(n)break;await new Promise(e=>l.push(e))}while(i.length)yield i.shift();if(o)throw o}finally{if(!n){try{t.abort()}catch{}}}}const w=()=>{try{t.abort()}catch{}};t.onreadystatechange=()=>{if(t.readyState===2)p()};t.onprogress=()=>{p();d()};t.onload=()=>{p();if(!y)d();n=true;a()};t.onerror=()=>{o=new Error("The request failed before it finished.");n=true;if(!r){r=true;u(o)}a()};t.onabort=()=>{const e=new Error("Aborted");e.name="AbortError";o=e;n=true;if(!r){r=true;u(e)}a()};t.ontimeout=()=>{o=new Error("The request timed out.");n=true;if(!r){r=true;u(o)}a()};try{t.open("POST",s.url,true);t.responseType="text";for(const[e,f]of Object.entries(s.headers)){try{t.setRequestHeader(e,f)}catch{}}if(s.signal){if(s.signal.aborted)w();else s.signal.addEventListener?.("abort",w)}t.send(s.body)}catch(e){r=true;u(e instanceof Error?e:new Error(String(e)))}})}
@@ -0,0 +1 @@
1
+ "use strict";export function realNow(){const e=globalThis.__buoyRealNow;return typeof e==="function"?e():Date.now()}function t(){const e=globalThis.__buoyRealTimers;return e??globalThis}export function realSetTimeout(e,o){return t().setTimeout(e,o)}export function realClearTimeout(e){t().clearTimeout(e)}