@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,123 +1 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.DEFAULT_IDLE_TIMEOUT_MS = exports.DEFAULT_CONNECT_TIMEOUT_MS = void 0;
7
- exports.createStreamTimer = createStreamTimer;
8
- /**
9
- * The clock on a model request.
10
- *
11
- * There was none. A gateway that accepted the socket and then said nothing —
12
- * an overloaded proxy, a dropped VPN, a laptop that slept mid-answer — left
13
- * the turn running with the thinking strip animating until a person pressed
14
- * Stop. The 180s turn deadline did not help: it is read at the TOP of a step
15
- * (`runAgentTurn`), and a step parked inside `await fetch(...)` never reaches
16
- * the next one.
17
- *
18
- * Two clocks, because the two silences mean different things:
19
- *
20
- * - CONNECT covers "no headers yet". Nothing has been generated, so nothing is
21
- * lost by giving up; 30s is already long for a gateway to acknowledge a POST.
22
- * - IDLE covers "headers arrived, then nothing". This one has to be generous.
23
- * Anthropic sends `ping` frames, but an OpenAI-protocol reasoning model can
24
- * legitimately emit nothing for a long stretch while it thinks, and cutting
25
- * that off would look like a broken product. Any frame at all — a keepalive
26
- * comment included — resets it, so only total silence trips it.
27
- *
28
- * A timeout is NOT a retry. Nothing is re-sent: the request may have reached
29
- * the model, and a duplicate turn is a duplicate bill and possibly duplicate
30
- * writes. The turn ends with an honest notice and a Continue button.
31
- */
32
-
33
- const DEFAULT_CONNECT_TIMEOUT_MS = exports.DEFAULT_CONNECT_TIMEOUT_MS = 30_000;
34
- const DEFAULT_IDLE_TIMEOUT_MS = exports.DEFAULT_IDLE_TIMEOUT_MS = 90_000;
35
- /**
36
- * A timer over an optional caller signal.
37
- *
38
- * When no `AbortController` exists (a runtime old enough to lack it) this
39
- * degrades to passing the caller's signal straight through with no clocks,
40
- * rather than failing to send anything at all.
41
- */
42
- function createStreamTimer(callerSignal, options = {}) {
43
- const connectMs = options.connectTimeoutMs ?? DEFAULT_CONNECT_TIMEOUT_MS;
44
- const idleMs = options.idleTimeoutMs ?? DEFAULT_IDLE_TIMEOUT_MS;
45
- const Controller = typeof AbortController === "function" ? AbortController : undefined;
46
- if (!Controller) {
47
- return {
48
- signal: callerSignal,
49
- connected() {},
50
- activity() {},
51
- expired: undefined,
52
- message: () => undefined,
53
- dispose() {}
54
- };
55
- }
56
- const controller = new Controller();
57
- let expired;
58
- let handle;
59
- let disposed = false;
60
- const clear = () => {
61
- if (handle !== undefined) {
62
- clearTimeout(handle);
63
- handle = undefined;
64
- }
65
- };
66
- const arm = (ms, which) => {
67
- clear();
68
- if (disposed || !Number.isFinite(ms) || ms <= 0) return;
69
- handle = setTimeout(() => {
70
- handle = undefined;
71
- expired = which;
72
- // The abort is what actually unblocks `fetch` / `reader.read()`. The
73
- // reason is tracked here rather than read back off the signal because
74
- // `AbortSignal.reason` is not reliably present on React Native.
75
- try {
76
- controller.abort();
77
- } catch {
78
- /* already aborted */
79
- }
80
- }, ms);
81
- };
82
- const onCallerAbort = () => {
83
- clear();
84
- try {
85
- controller.abort();
86
- } catch {
87
- /* already aborted */
88
- }
89
- };
90
- if (callerSignal) {
91
- if (callerSignal.aborted) onCallerAbort();else callerSignal.addEventListener?.("abort", onCallerAbort);
92
- }
93
- arm(connectMs, "connect");
94
- return {
95
- signal: controller.signal,
96
- connected() {
97
- arm(idleMs, "idle");
98
- },
99
- activity() {
100
- // Only rearm a clock that is already running: a provider that reports
101
- // activity after `dispose` must not resurrect the timer.
102
- if (!disposed) arm(idleMs, "idle");
103
- },
104
- get expired() {
105
- return expired;
106
- },
107
- message() {
108
- if (expired === "connect") {
109
- return `The model endpoint did not respond within ${Math.round(connectMs / 1000)}s. Nothing was generated, and nothing was re-sent.`;
110
- }
111
- if (expired === "idle") {
112
- return `The model endpoint stopped sending after ${Math.round(idleMs / 1000)}s of silence. Whatever arrived is above; nothing was re-sent.`;
113
- }
114
- return undefined;
115
- },
116
- dispose() {
117
- disposed = true;
118
- clear();
119
- callerSignal?.removeEventListener?.("abort", onCallerAbort);
120
- }
121
- };
122
- }
123
- //# sourceMappingURL=streamTimer.js.map
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:true});exports.DEFAULT_IDLE_TIMEOUT_MS=exports.DEFAULT_CONNECT_TIMEOUT_MS=void 0;exports.createStreamTimer=createStreamTimer;var _realNow=require("../realNow");const DEFAULT_CONNECT_TIMEOUT_MS=exports.DEFAULT_CONNECT_TIMEOUT_MS=3e4;const DEFAULT_IDLE_TIMEOUT_MS=exports.DEFAULT_IDLE_TIMEOUT_MS=9e4;function createStreamTimer(e,u={}){const T=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){(0,_realNow.realClearTimeout)(t);t=void 0}};const s=(c,_)=>{d();if(i||!Number.isFinite(c)||c<=0)return;t=(0,_realNow.realSetTimeout)(()=>{t=void 0;n=_;try{o.abort()}catch{}},c)};const a=()=>{d();try{o.abort()}catch{}};if(e){if(e.aborted)a();else e.addEventListener?.("abort",a)}s(T,"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(T/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",a)}}}
@@ -1,123 +1 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.createTransport = createTransport;
7
- var _sse = require("./sse");
8
- var _xhrStream = require("./xhrStream");
9
- var _problem = require("./problem");
10
- /**
11
- * How a request is SENT, decided before it is sent.
12
- *
13
- * The obvious place to add XHR was inside `readSse`, once a missing
14
- * `response.body` had already proved that `fetch` could not stream. That is
15
- * the wrong boundary and would have been a real bug: the POST has already
16
- * happened by then, the reader has neither the URL nor the headers nor the
17
- * body to repeat it, and issuing a second one would ask the model to generate
18
- * the same answer twice — billed twice, and for a turn that writes, acted on
19
- * twice. Transport is chosen up front; a response that has started is consumed
20
- * exactly once, whatever it turns out to be.
21
- *
22
- * `auto` therefore learns rather than probes. The first request goes through
23
- * `fetch`; if that response has no reader, the answer is buffered (as it
24
- * always was on that runtime) and the SESSION remembers, so every later
25
- * request uses XHR and streams. One buffered reply at the start of a bare-RN
26
- * session, and nothing to configure.
27
- */
28
-
29
- const EMPTY = async function* () {};
30
-
31
- /**
32
- * One transport per provider instance, because the `auto` decision is worth
33
- * remembering: re-learning it would buffer the first reply of every turn.
34
- */
35
- function createTransport(config) {
36
- const requested = config.transport ?? "auto";
37
- /**
38
- * Set once a `fetch` response has come back with no reader. Only ever
39
- * flipped one way: a runtime does not gain streaming mid-session.
40
- */
41
- let fetchCannotStream = false;
42
- const useXhr = () => {
43
- // A host-supplied `fetch` is never bypassed. It is how a harness keeps
44
- // model traffic off the global (and out of the network interceptor), and
45
- // routing around it would defeat the reason it was passed in.
46
- if (config.fetch) return requested === "xhr" && (0, _xhrStream.xhrAvailable)();
47
- if (requested === "xhr") return (0, _xhrStream.xhrAvailable)();
48
- if (requested === "fetch") return false;
49
- return fetchCannotStream && (0, _xhrStream.xhrAvailable)();
50
- };
51
- return {
52
- async open(req) {
53
- if (useXhr()) {
54
- const response = await (0, _xhrStream.xhrStream)({
55
- url: config.endpoint,
56
- headers: req.headers,
57
- body: req.body,
58
- signal: req.signal
59
- });
60
- if (!response.ok) {
61
- const detail = (await response.text()).slice(0, 600);
62
- return {
63
- problem: `HTTP ${response.status}${detail ? ` — ${detail}` : ""}`,
64
- problemInfo: (0, _problem.classifyProblem)({
65
- status: response.status,
66
- type: (0, _problem.errorTypeOf)(detail),
67
- message: detail,
68
- retryAfter: response.retryAfter
69
- }),
70
- frames: EMPTY
71
- };
72
- }
73
- const type = response.contentType.toLowerCase();
74
- if (type && !type.includes("event-stream")) {
75
- const detail = (await response.text()).slice(0, 600);
76
- return {
77
- 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}` : ""}`,
78
- frames: EMPTY
79
- };
80
- }
81
- return {
82
- frames: options => response.frames(options)
83
- };
84
- }
85
- const response = await (config.fetch ?? fetch)(config.endpoint, {
86
- method: "POST",
87
- headers: req.headers,
88
- signal: req.signal,
89
- body: req.body
90
- });
91
- if (!response.ok) {
92
- const detail = await (0, _sse.errorBody)(response);
93
- return {
94
- problem: `HTTP ${response.status}${detail ? ` — ${detail}` : ""}`,
95
- problemInfo: (0, _problem.classifyProblem)({
96
- status: response.status,
97
- type: (0, _problem.errorTypeOf)(detail),
98
- message: detail,
99
- retryAfter: response.headers?.get?.("retry-after") ?? null
100
- }),
101
- frames: EMPTY
102
- };
103
- }
104
- const problem = await (0, _sse.nonStreamProblem)(response);
105
- if (problem) return {
106
- problem,
107
- frames: EMPTY
108
- };
109
-
110
- // Learn, for the NEXT request. This one is already in flight and is read
111
- // through the buffered path — re-sending it is what this file exists to
112
- // avoid.
113
- const body = response.body;
114
- if ((!body || typeof body.getReader !== "function") && !config.fetch) {
115
- fetchCannotStream = true;
116
- }
117
- return {
118
- frames: options => (0, _sse.readSse)(response, options)
119
- };
120
- }
121
- };
122
- }
123
- //# sourceMappingURL=transport.js.map
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:true});exports.createTransport=createTransport;var _sse=require("./sse");var _xhrStream=require("./xhrStream");var _problem=require("./problem");const EMPTY=async function*(){};function createTransport(s){const n=s.transport??"auto";let i=false;const f=()=>{if(s.fetch)return n==="xhr"&&(0,_xhrStream.xhrAvailable)();if(n==="xhr")return(0,_xhrStream.xhrAvailable)();if(n==="fetch")return false;return i&&(0,_xhrStream.xhrAvailable)()};return{async open(a){if(f()){const e=await(0,_xhrStream.xhrStream)({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:(0,_problem.classifyProblem)({status:e.status,type:(0,_problem.errorTypeOf)(t),message:t,retryAfter:e.retryAfter}),frames:EMPTY}}const o=e.contentType.toLowerCase();if(o&&!o.includes("event-stream")){const t=(await e.text()).slice(0,600);return{problem:o.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:EMPTY}}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(0,_sse.errorBody)(r);return{problem:`HTTP ${r.status}${e?` \u2014 ${e}`:""}`,problemInfo:(0,_problem.classifyProblem)({status:r.status,type:(0,_problem.errorTypeOf)(e),message:e,retryAfter:r.headers?.get?.("retry-after")??null}),frames:EMPTY}}const l=await(0,_sse.nonStreamProblem)(r);if(l)return{problem:l,frames:EMPTY};const u=r.body;if((!u||typeof u.getReader!=="function")&&!s.fetch){i=true}return{frames:e=>(0,_sse.readSse)(r,e)}}}}
@@ -1,6 +1 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- //# sourceMappingURL=types.js.map
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:true});
@@ -1,212 +1 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.xhrAvailable = xhrAvailable;
7
- exports.xhrStream = xhrStream;
8
- var _sse = require("./sse");
9
- /**
10
- * Streaming over `XMLHttpRequest`, for the runtimes where `fetch` cannot.
11
- *
12
- * React Native core still has no `response.body`: its `fetch` is the whatwg
13
- * polyfill over XHR and resolves inside `xhr.onload`, i.e. after the ENTIRE
14
- * body. Under Expo the global is `expo/fetch`, which does stream — but
15
- * `EXPO_PUBLIC_USE_RN_FETCH=1` reverts that, and the bare-RN CLI, TV CLI and
16
- * NativeScript lanes never had it at all. On those, every Ask Buoy answer
17
- * appeared in one lump, however long the model took.
18
- *
19
- * XHR itself has streamed the whole time. `responseText` grows as bytes arrive
20
- * and `onprogress` fires as it does, so slicing from the last offset gives the
21
- * same incremental text a reader would. Two conditions, both easy to miss:
22
- * the progress handler must be installed BEFORE `send()`, and `responseType`
23
- * must stay text — anything else stops RN emitting progress at all.
24
- *
25
- * WHAT THIS IS NOT. It is not a second SSE parser: framing, the truncation
26
- * rule and the terminal-marker contract all live in `sse.ts` and are shared
27
- * verbatim. This file only produces text chunks.
28
- */
29
-
30
- /** Beyond this the answer is not an answer any more; stop reading. */
31
- const MAX_RESPONSE_CHARS = 8_000_000;
32
- function xhrAvailable() {
33
- return typeof XMLHttpRequest === "function";
34
- }
35
-
36
- /**
37
- * Send the request and resolve once the HEADERS are in, so the caller can
38
- * check the status and content type before deciding to read a stream — the
39
- * same shape `fetch` offers, and the reason this can slot in beside it.
40
- */
41
- function xhrStream(req) {
42
- return new Promise((resolveHeaders, rejectHeaders) => {
43
- const xhr = new XMLHttpRequest();
44
-
45
- /** Text seen but not yet handed to a consumer. */
46
- const queue = [];
47
- /** Resolvers for a consumer parked on an empty queue. */
48
- const waiters = [];
49
- let offset = 0;
50
- let done = false;
51
- let failure;
52
- let headersSeen = false;
53
- let capped = false;
54
- const wake = () => {
55
- while (waiters.length) waiters.shift()();
56
- };
57
- const pull = () => {
58
- // `responseText` is the WHOLE body so far, so the delta is everything
59
- // past what was already taken. RN keeps the full string either way;
60
- // the offset bounds the work, not the memory.
61
- let text;
62
- try {
63
- text = xhr.responseText ?? "";
64
- } catch {
65
- return; // Not readable yet on some runtimes; the next event will do.
66
- }
67
- if (text.length <= offset) return;
68
- if (text.length > MAX_RESPONSE_CHARS) {
69
- capped = true;
70
- failure = new Error("The endpoint sent more than this tool will read.");
71
- done = true;
72
- try {
73
- xhr.abort();
74
- } catch {
75
- /* already gone */
76
- }
77
- wake();
78
- return;
79
- }
80
- queue.push(text.slice(offset));
81
- offset = text.length;
82
- wake();
83
- };
84
- const settleHeaders = () => {
85
- if (headersSeen) return;
86
- headersSeen = true;
87
- let contentType = "";
88
- let retryAfter = null;
89
- try {
90
- contentType = xhr.getResponseHeader("content-type") ?? "";
91
- retryAfter = xhr.getResponseHeader("retry-after");
92
- } catch {
93
- contentType = "";
94
- }
95
- resolveHeaders({
96
- status: xhr.status,
97
- ok: xhr.status >= 200 && xhr.status < 300,
98
- contentType,
99
- retryAfter,
100
- async text() {
101
- // Whatever has arrived. For a non-2xx that is the error body; the
102
- // request is over by the time anyone asks.
103
- await new Promise(r => {
104
- if (done) return r();
105
- waiters.push(() => r());
106
- });
107
- try {
108
- return xhr.responseText ?? "";
109
- } catch {
110
- return "";
111
- }
112
- },
113
- frames(options) {
114
- return (0, _sse.framesFrom)(chunks(), options);
115
- }
116
- });
117
- };
118
- async function* chunks() {
119
- try {
120
- for (;;) {
121
- while (queue.length) yield queue.shift();
122
- if (done) break;
123
- await new Promise(r => waiters.push(r));
124
- }
125
- while (queue.length) yield queue.shift();
126
- if (failure) throw failure;
127
- } finally {
128
- if (!done) {
129
- // The consumer left early — a completed turn, a Stop, a timeout.
130
- // Nothing else will read this socket.
131
- try {
132
- xhr.abort();
133
- } catch {
134
- /* already gone */
135
- }
136
- }
137
- }
138
- }
139
- const onAbort = () => {
140
- try {
141
- xhr.abort();
142
- } catch {
143
- /* already gone */
144
- }
145
- };
146
- xhr.onreadystatechange = () => {
147
- // HEADERS_RECEIVED. Status and content type are known here, long before
148
- // the body finishes — which is the whole point of using XHR directly.
149
- if (xhr.readyState === 2) settleHeaders();
150
- };
151
- xhr.onprogress = () => {
152
- settleHeaders();
153
- pull();
154
- };
155
- xhr.onload = () => {
156
- settleHeaders();
157
- if (!capped) pull();
158
- done = true;
159
- wake();
160
- };
161
- xhr.onerror = () => {
162
- failure = new Error("The request failed before it finished.");
163
- done = true;
164
- if (!headersSeen) {
165
- headersSeen = true;
166
- rejectHeaders(failure);
167
- }
168
- wake();
169
- };
170
- xhr.onabort = () => {
171
- const err = new Error("Aborted");
172
- err.name = "AbortError";
173
- failure = err;
174
- done = true;
175
- if (!headersSeen) {
176
- headersSeen = true;
177
- rejectHeaders(err);
178
- }
179
- wake();
180
- };
181
- xhr.ontimeout = () => {
182
- failure = new Error("The request timed out.");
183
- done = true;
184
- if (!headersSeen) {
185
- headersSeen = true;
186
- rejectHeaders(failure);
187
- }
188
- wake();
189
- };
190
- try {
191
- xhr.open("POST", req.url, true);
192
- // Text, and nothing else: RN emits no progress events for any other
193
- // response type, which turns this back into a buffered read in silence.
194
- xhr.responseType = "text";
195
- for (const [k, v] of Object.entries(req.headers)) {
196
- try {
197
- xhr.setRequestHeader(k, v);
198
- } catch {
199
- /* a header the runtime forbids is not worth failing the turn over */
200
- }
201
- }
202
- if (req.signal) {
203
- if (req.signal.aborted) onAbort();else req.signal.addEventListener?.("abort", onAbort);
204
- }
205
- xhr.send(req.body);
206
- } catch (err) {
207
- headersSeen = true;
208
- rejectHeaders(err instanceof Error ? err : new Error(String(err)));
209
- }
210
- });
211
- }
212
- //# sourceMappingURL=xhrStream.js.map
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:true});exports.xhrAvailable=xhrAvailable;exports.xhrStream=xhrStream;var _sse=require("./sse");const MAX_RESPONSE_CHARS=8e6;function xhrAvailable(){return typeof XMLHttpRequest==="function"}function xhrStream(o){return new Promise((b,u)=>{const e=new XMLHttpRequest;const i=[];const l=[];let h=0;let n=false;let s;let r=false;let y=false;const a=()=>{while(l.length)l.shift()()};const d=()=>{let t;try{t=e.responseText??""}catch{return}if(t.length<=h)return;if(t.length>MAX_RESPONSE_CHARS){y=true;s=new Error("The endpoint sent more than this tool will read.");n=true;try{e.abort()}catch{}a();return}i.push(t.slice(h));h=t.length;a()};const p=()=>{if(r)return;r=true;let t="";let f=null;try{t=e.getResponseHeader("content-type")??"";f=e.getResponseHeader("retry-after")}catch{t=""}b({status:e.status,ok:e.status>=200&&e.status<300,contentType:t,retryAfter:f,async text(){await new Promise(c=>{if(n)return c();l.push(()=>c())});try{return e.responseText??""}catch{return""}},frames(c){return(0,_sse.framesFrom)(g(),c)}})};async function*g(){try{for(;;){while(i.length)yield i.shift();if(n)break;await new Promise(t=>l.push(t))}while(i.length)yield i.shift();if(s)throw s}finally{if(!n){try{e.abort()}catch{}}}}const w=()=>{try{e.abort()}catch{}};e.onreadystatechange=()=>{if(e.readyState===2)p()};e.onprogress=()=>{p();d()};e.onload=()=>{p();if(!y)d();n=true;a()};e.onerror=()=>{s=new Error("The request failed before it finished.");n=true;if(!r){r=true;u(s)}a()};e.onabort=()=>{const t=new Error("Aborted");t.name="AbortError";s=t;n=true;if(!r){r=true;u(t)}a()};e.ontimeout=()=>{s=new Error("The request timed out.");n=true;if(!r){r=true;u(s)}a()};try{e.open("POST",o.url,true);e.responseType="text";for(const[t,f]of Object.entries(o.headers)){try{e.setRequestHeader(t,f)}catch{}}if(o.signal){if(o.signal.aborted)w();else o.signal.addEventListener?.("abort",w)}e.send(o.body)}catch(t){r=true;u(t instanceof Error?t:new Error(String(t)))}})}
@@ -0,0 +1 @@
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:true});exports.realClearTimeout=realClearTimeout;exports.realNow=realNow;exports.realSetTimeout=realSetTimeout;function realNow(){const e=globalThis.__buoyRealNow;return typeof e==="function"?e():Date.now()}function timers(){const e=globalThis.__buoyRealTimers;return e??globalThis}function realSetTimeout(e,t){return timers().setTimeout(e,t)}function realClearTimeout(e){timers().clearTimeout(e)}