@openclaw/ai 2026.7.2-beta.4 → 2026.7.2-beta.6

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 (68) hide show
  1. package/dist/{anthropic-CrUDIBpM.mjs → anthropic-CH4UUnZr.mjs} +26 -441
  2. package/dist/{anthropic-BoTnz8cv.d.mts → anthropic-SrGtwsJu.d.mts} +27 -1
  3. package/dist/anthropic-usage-DWU-x8MI.mjs +459 -0
  4. package/dist/{api-registry-BMphkFf1.d.mts → api-registry-DlMgPR39.d.mts} +1 -1
  5. package/dist/{azure-openai-responses-CbrpVeEv.mjs → azure-openai-responses-CImcwB83.mjs} +4 -4
  6. package/dist/cache-retention-0x979a5V.mjs +12 -0
  7. package/dist/deferred-event-buffer-DAvyP7qA.mjs +19 -0
  8. package/dist/error-coercion-DgxlWC0n.mjs +15 -0
  9. package/dist/{event-stream-Douf9dob.d.mts → event-stream-YjaPW20U.d.mts} +1 -1
  10. package/dist/event-stream.d.mts +1 -1
  11. package/dist/{github-copilot-headers-BsH5cqGj.mjs → github-copilot-headers-NCJtz9i0.mjs} +1 -12
  12. package/dist/{google-Cs62KA7t.mjs → google-CtSg0iTS.mjs} +3 -3
  13. package/dist/{google-shared-CMLI-tCZ.mjs → google-shared-DNBz5rcD.mjs} +7 -4
  14. package/dist/{google-vertex-B4SD3U0f.mjs → google-vertex-31f1uS9L.mjs} +3 -3
  15. package/dist/{host-WvWBo4h8.d.mts → host-B9GUmcra.d.mts} +2 -2
  16. package/dist/host-Dog2WQiR.mjs +369 -0
  17. package/dist/index.d.mts +7 -7
  18. package/dist/index.mjs +3 -3
  19. package/dist/internal/anthropic.d.mts +16 -62
  20. package/dist/internal/anthropic.mjs +5 -4
  21. package/dist/internal/openai.d.mts +260 -2
  22. package/dist/internal/openai.mjs +7 -5
  23. package/dist/internal/runtime.d.mts +8 -32
  24. package/dist/internal/runtime.mjs +7 -6
  25. package/dist/internal/shared.d.mts +14 -9
  26. package/dist/internal/shared.mjs +6 -2
  27. package/dist/{llm-request-activity-CehVkZP-.mjs → llm-request-activity-BjtkplhG.mjs} +1 -19
  28. package/dist/{mistral-D5Ps7Led.mjs → mistral-CWmpvWYh.mjs} +7 -4
  29. package/dist/number-coercion-DvG7SNMg.mjs +129 -0
  30. package/dist/{openai-chatgpt-responses-h0o5yV8Y.mjs → openai-chatgpt-responses-B84Ibtrd.mjs} +33 -29
  31. package/dist/{openai-completions-exO8NFQf.mjs → openai-completions-DsOxhOD1.mjs} +28 -482
  32. package/dist/openai-completions-compat-DBWjXoMZ.d.mts +43 -0
  33. package/dist/openai-prompt-cache-2uo_1OR1.d.mts +7 -0
  34. package/dist/openai-prompt-cache-mZTCdRPo.mjs +12 -0
  35. package/dist/openai-reasoning-compat-YgeLncHw.mjs +396 -0
  36. package/dist/{openai-responses-BHpmtUKo.mjs → openai-responses-BT7A3sLu.mjs} +7 -5
  37. package/dist/openai-responses-shared-pXl6Wd8S.mjs +392 -0
  38. package/dist/{openai-responses-shared-CzCurmY1.mjs → openai-responses-stream-internal-Cw5txaGW.mjs} +1609 -1037
  39. package/dist/{openai-tool-projection-ITOU9bG1.mjs → openai-tool-projection-OhX64DoP.mjs} +26 -11
  40. package/dist/prompt-cache-stability-Cwcjv_fx.d.mts +13 -0
  41. package/dist/{provider-error-C4VvV_3t.mjs → provider-error-CAEvRjry.mjs} +8 -2
  42. package/dist/provider-options-D8bB3z9b.d.mts +144 -0
  43. package/dist/providers.d.mts +3 -2
  44. package/dist/providers.mjs +10 -9
  45. package/dist/reasoning-tag-text-partitioner-CGDyLWUR.mjs +12209 -0
  46. package/dist/simple-options-9lhRrN73.mjs +50 -0
  47. package/dist/{src-CXno1H5g.mjs → src-QkygScBs.mjs} +23 -4
  48. package/dist/stream-first-event-timeout-BBys9hSb.mjs +86 -0
  49. package/dist/stream-first-event-timeout-DvDeSucC.d.mts +29 -0
  50. package/dist/tls-certificate-errors-DXSpluKI.mjs +93 -0
  51. package/dist/tool-result-text-CTpIRbYd.mjs +225 -0
  52. package/dist/transform-messages-C8mBqZxF.mjs +2 -0
  53. package/dist/transport-stream-shared-D81p90xq.mjs +297 -0
  54. package/dist/transports.d.mts +115 -132
  55. package/dist/transports.mjs +400 -1991
  56. package/dist/{types-AFwwWium.d.mts → types-bzp5k29J.d.mts} +7 -0
  57. package/dist/types.d.mts +5 -5
  58. package/dist/types.mjs +2 -2
  59. package/dist/{validation-sxvxC8J-.d.mts → validation-B-j7cOYp.d.mts} +1 -1
  60. package/dist/validation.d.mts +1 -1
  61. package/package.json +4 -5
  62. package/dist/host-XYGZcgO8.mjs +0 -98
  63. package/dist/model-utils-1GiZ2_rr.mjs +0 -64
  64. package/dist/openai-CoGicoDt.d.mts +0 -332
  65. package/dist/reasoning-tag-text-partitioner-BlVe67g6.mjs +0 -400
  66. package/dist/stream-first-event-timeout-DP4xEyBY.mjs +0 -150
  67. package/dist/system-prompt-cache-boundary-CbHeV4_l.mjs +0 -526
  68. package/npm-shrinkwrap.json +0 -638
@@ -1,400 +0,0 @@
1
- //#region packages/markdown-core/src/fences.ts
2
- /** Scans fenced-code spans incrementally so chunking can carry an open fence forward. */
3
- function scanFenceSpans(buffer, state) {
4
- const spans = [];
5
- const startsAtLineStart = state?.atLineStart ?? true;
6
- let open = state?.open ? {
7
- ...state.open,
8
- start: 0
9
- } : void 0;
10
- let offset = 0;
11
- while (offset <= buffer.length) {
12
- const nextNewline = buffer.indexOf("\n", offset);
13
- const lineEnd = nextNewline === -1 ? buffer.length : nextNewline;
14
- const line = buffer.slice(offset, lineEnd).replace(/\r$/, "");
15
- const match = line.match(/^( {0,3})(`{3,}|~{3,})(.*)$/);
16
- if (match && (offset > 0 || startsAtLineStart)) {
17
- const [, indent, marker, trailing] = match;
18
- if (indent === void 0 || marker === void 0 || trailing === void 0) {
19
- if (nextNewline === -1) break;
20
- offset = nextNewline + 1;
21
- continue;
22
- }
23
- const markerChar = marker.charAt(0);
24
- const markerLen = marker.length;
25
- if (!open) open = {
26
- start: offset,
27
- markerChar,
28
- markerLen,
29
- openLine: line,
30
- marker,
31
- indent
32
- };
33
- else if (open.markerChar === markerChar && markerLen >= open.markerLen && /^[ \t]*$/.test(trailing)) {
34
- const end = lineEnd;
35
- spans.push({
36
- start: open.start,
37
- end,
38
- openLine: open.openLine,
39
- marker: open.marker,
40
- indent: open.indent
41
- });
42
- open = void 0;
43
- }
44
- }
45
- if (nextNewline === -1) break;
46
- offset = nextNewline + 1;
47
- }
48
- if (open) spans.push({
49
- start: open.start,
50
- end: buffer.length,
51
- openLine: open.openLine,
52
- marker: open.marker,
53
- indent: open.indent
54
- });
55
- return {
56
- spans,
57
- state: {
58
- atLineStart: buffer.length === 0 ? startsAtLineStart : buffer.endsWith("\n"),
59
- ...open ? { open: {
60
- markerChar: open.markerChar,
61
- markerLen: open.markerLen,
62
- openLine: open.openLine,
63
- marker: open.marker,
64
- indent: open.indent
65
- } } : {}
66
- }
67
- };
68
- }
69
- //#endregion
70
- //#region packages/markdown-core/src/code-spans.ts
71
- /** Creates the carry-forward state used when scanning inline code across chunks. */
72
- function createInlineCodeState() {
73
- return {
74
- open: false,
75
- ticks: 0
76
- };
77
- }
78
- /** Builds a lookup for fenced and inline code spans while preserving scanner state. */
79
- function buildCodeSpanIndex(text, inlineState, fenceState) {
80
- const { spans: fenceSpans, state: nextFenceState } = scanFenceSpans(text, fenceState);
81
- const { spans: inlineSpans, state: nextInlineState } = parseInlineCodeSpans(text, fenceSpans, inlineState ? {
82
- open: inlineState.open,
83
- ticks: inlineState.ticks
84
- } : createInlineCodeState());
85
- return {
86
- inlineState: nextInlineState,
87
- fenceState: nextFenceState,
88
- isInside: (index) => isInsideFenceSpan(index, fenceSpans) || isInsideInlineSpan(index, inlineSpans)
89
- };
90
- }
91
- function parseInlineCodeSpans(text, fenceSpans, initialState) {
92
- const spans = [];
93
- let open = initialState.open;
94
- let ticks = initialState.ticks;
95
- let openStart = open ? 0 : -1;
96
- let i = 0;
97
- while (i < text.length) {
98
- const fence = findFenceSpanAtInclusive(fenceSpans, i);
99
- if (fence) {
100
- i = fence.end;
101
- continue;
102
- }
103
- if (text[i] !== "`") {
104
- i += 1;
105
- continue;
106
- }
107
- const runStart = i;
108
- let runLength = 0;
109
- while (i < text.length && text[i] === "`") {
110
- runLength += 1;
111
- i += 1;
112
- }
113
- if (!open) {
114
- open = true;
115
- ticks = runLength;
116
- openStart = runStart;
117
- continue;
118
- }
119
- if (runLength === ticks) {
120
- spans.push([openStart, i]);
121
- open = false;
122
- ticks = 0;
123
- openStart = -1;
124
- }
125
- }
126
- if (open) spans.push([openStart, text.length]);
127
- return {
128
- spans,
129
- state: {
130
- open,
131
- ticks
132
- }
133
- };
134
- }
135
- function findFenceSpanAtInclusive(spans, index) {
136
- return spans.find((span) => index >= span.start && index < span.end);
137
- }
138
- function isInsideFenceSpan(index, spans) {
139
- return spans.some((span) => index >= span.start && index < span.end);
140
- }
141
- function isInsideInlineSpan(index, spans) {
142
- return spans.some(([start, end]) => index >= start && index < end);
143
- }
144
- //#endregion
145
- //#region packages/ai/src/utils/reasoning-tag-text-partitioner.ts
146
- const REASONING_TAG_RE = /<\s*(\/?)\s*(?:(?:antml:|mm:)?(?:think(?:ing)?|thought|reasoning)|antthinking)\b[^<>]*>/gi;
147
- const REASONING_TAG_NAMES = [
148
- "think",
149
- "thinking",
150
- "thought",
151
- "reasoning",
152
- "antthinking",
153
- "antml:think",
154
- "antml:thinking",
155
- "antml:thought",
156
- "antml:reasoning",
157
- "mm:think",
158
- "mm:thinking",
159
- "mm:thought",
160
- "mm:reasoning"
161
- ];
162
- function createReasoningTagTextPartitioner() {
163
- let buffer = "";
164
- let reasoningDepth = 0;
165
- let strictMode = false;
166
- let emittedVisibleText = false;
167
- let inlineCodeState = createInlineCodeState();
168
- let fenceState;
169
- let hiddenInlineCodeState = createInlineCodeState();
170
- let hiddenFenceState;
171
- let recoverableOpenTagText;
172
- const consume = (final, recoverFullUnclosed) => {
173
- const output = [];
174
- const emit = (kind, text) => {
175
- if (!text) return;
176
- if (kind === "text" && text.trim().length > 0) emittedVisibleText = true;
177
- if (kind === "text") {
178
- const nextCode = buildCodeSpanIndex(text, inlineCodeState, fenceState);
179
- inlineCodeState = nextCode.inlineState;
180
- fenceState = nextCode.fenceState;
181
- } else {
182
- const nextCode = buildCodeSpanIndex(text, hiddenInlineCodeState, hiddenFenceState);
183
- hiddenInlineCodeState = nextCode.inlineState;
184
- hiddenFenceState = nextCode.fenceState;
185
- }
186
- const previous = output[output.length - 1];
187
- if (previous?.kind === kind) {
188
- previous.text += text;
189
- return;
190
- }
191
- output.push({
192
- kind,
193
- text
194
- });
195
- };
196
- while (buffer) {
197
- const activeInlineCodeState = reasoningDepth === 0 ? inlineCodeState : hiddenInlineCodeState;
198
- const activeFenceState = reasoningDepth === 0 ? fenceState : hiddenFenceState;
199
- const codeSpans = buildCodeSpanIndex(buffer, activeInlineCodeState, activeFenceState);
200
- const hasUnclosedCode = reasoningDepth === 0 && Boolean(codeSpans.inlineState.open || codeSpans.fenceState.open);
201
- const hasRawReasoning = hasRawReasoningTag(buffer);
202
- const tag = findNextReasoningTag(buffer, (index) => final && hasUnclosedCode && hasRawReasoning ? false : codeSpans.isInside(index));
203
- if (!tag) {
204
- if (final) {
205
- const recoverAsText = reasoningDepth > 0 && recoverFullUnclosed && !hasRawReasoningCloseTag(buffer);
206
- const recoveredText = recoverAsText && recoverableOpenTagText ? recoverableOpenTagText + buffer : buffer;
207
- emit(reasoningDepth > 0 && !recoverAsText ? "thinking" : "text", recoveredText);
208
- buffer = "";
209
- reasoningDepth = 0;
210
- recoverableOpenTagText = void 0;
211
- return output;
212
- }
213
- if (reasoningDepth > 0 && recoverFullUnclosed && (!emittedVisibleText || recoverableOpenTagText)) return output;
214
- if (hasUnclosedCode && hasRawReasoning) {
215
- const openCodeIndex = inlineCodeState.open || fenceState?.open ? 0 : findOpenCodeContextStart(buffer);
216
- if (openCodeIndex !== -1) {
217
- emit("text", buffer.slice(0, openCodeIndex));
218
- buffer = buffer.slice(openCodeIndex);
219
- return output;
220
- }
221
- }
222
- const trailingFenceStart = findTrailingFenceFragmentStart(buffer, activeInlineCodeState, activeFenceState);
223
- if (trailingFenceStart !== -1) {
224
- emit(reasoningDepth > 0 ? "thinking" : "text", buffer.slice(0, trailingFenceStart));
225
- buffer = buffer.slice(trailingFenceStart);
226
- return output;
227
- }
228
- const keepFrom = reasoningTagPrefixSuffixIndex(buffer, (index) => codeSpans.isInside(index));
229
- if (keepFrom === -1) {
230
- emit(reasoningDepth > 0 ? "thinking" : "text", buffer);
231
- buffer = "";
232
- return output;
233
- }
234
- if (reasoningDepth === 0 && keepFrom > 0 && buffer.slice(0, keepFrom).trim().length > 0 && isReasoningCloseTagPrefix(buffer.slice(keepFrom))) return output;
235
- if (keepFrom > 0) {
236
- emit(reasoningDepth > 0 ? "thinking" : "text", buffer.slice(0, keepFrom));
237
- buffer = buffer.slice(keepFrom);
238
- }
239
- return output;
240
- }
241
- const beforeTag = buffer.slice(0, tag.index);
242
- const afterTag = buffer.slice(tag.index + tag.text.length);
243
- if (tag.isClose && reasoningDepth === 0) {
244
- if (recoverFullUnclosed && beforeTag.trim().length > 0 && afterTag.trim().length > 0) {
245
- emit("text", beforeTag + tag.text);
246
- buffer = afterTag;
247
- continue;
248
- }
249
- if (beforeTag.trim().length > 0 && afterTag.trim().length === 0 && !final) return output;
250
- if (beforeTag.trim().length === 0 || afterTag.trim().length === 0) emit("text", beforeTag);
251
- buffer = afterTag;
252
- continue;
253
- }
254
- emit(reasoningDepth > 0 ? "thinking" : "text", buffer.slice(0, tag.index));
255
- buffer = afterTag;
256
- if (tag.isClose) {
257
- reasoningDepth = Math.max(0, reasoningDepth - 1);
258
- if (reasoningDepth === 0) {
259
- recoverableOpenTagText = void 0;
260
- hiddenInlineCodeState = createInlineCodeState();
261
- hiddenFenceState = void 0;
262
- }
263
- } else {
264
- if (reasoningDepth === 0) {
265
- recoverableOpenTagText = recoverFullUnclosed && emittedVisibleText ? tag.text : void 0;
266
- hiddenInlineCodeState = createInlineCodeState();
267
- hiddenFenceState = void 0;
268
- }
269
- reasoningDepth += 1;
270
- }
271
- }
272
- return output;
273
- };
274
- return {
275
- markStrict() {
276
- strictMode = true;
277
- },
278
- push(chunk) {
279
- strictMode = true;
280
- buffer += chunk;
281
- return consume(false, false);
282
- },
283
- pushVisible(chunk) {
284
- buffer += chunk;
285
- return consume(false, true);
286
- },
287
- flush() {
288
- return consume(true, !strictMode);
289
- },
290
- hasPending() {
291
- return buffer.length > 0 || reasoningDepth > 0;
292
- },
293
- isInsideReasoning() {
294
- return reasoningDepth > 0;
295
- }
296
- };
297
- }
298
- function hasRawReasoningTag(text) {
299
- REASONING_TAG_RE.lastIndex = 0;
300
- return REASONING_TAG_RE.test(text);
301
- }
302
- function hasRawReasoningCloseTag(text) {
303
- REASONING_TAG_RE.lastIndex = 0;
304
- for (;;) {
305
- const match = REASONING_TAG_RE.exec(text);
306
- if (!match) return false;
307
- if (match[1] === "/") return true;
308
- }
309
- }
310
- function findNextReasoningTag(text, isIndexInsideCode) {
311
- REASONING_TAG_RE.lastIndex = 0;
312
- for (;;) {
313
- const match = REASONING_TAG_RE.exec(text);
314
- if (!match) return null;
315
- if (!isIndexInsideCode(match.index)) return {
316
- index: match.index,
317
- text: match[0],
318
- isClose: match[1] === "/"
319
- };
320
- }
321
- }
322
- function reasoningTagPrefixSuffixIndex(text, isIndexInsideCode) {
323
- for (let index = text.lastIndexOf("<"); index >= 0;) {
324
- if (!isIndexInsideCode(index) && isReasoningTagPrefix(text.slice(index))) return index;
325
- if (index === 0) break;
326
- index = text.lastIndexOf("<", index - 1);
327
- }
328
- return -1;
329
- }
330
- function isReasoningTagPrefix(text) {
331
- const name = normalizeReasoningTagPrefixName(text);
332
- return REASONING_TAG_NAMES.some((tagName) => {
333
- if (tagName.startsWith(name)) return true;
334
- if (!name.startsWith(tagName)) return false;
335
- const rest = name.slice(tagName.length);
336
- return rest.length === 0 || /^[\s/>]/.test(rest);
337
- });
338
- }
339
- function isReasoningCloseTagPrefix(text) {
340
- return text.replace(/^<\s*/, "<").replace(/^<\s*\//, "</").replace(/^<\/\s*/, "</").toLowerCase().startsWith("</") && isReasoningTagPrefix(text);
341
- }
342
- function normalizeReasoningTagPrefixName(text) {
343
- const normalized = text.replace(/^<\s*/, "<").replace(/^<\s*\//, "</").replace(/^<\/\s*/, "</").toLowerCase();
344
- return (normalized.startsWith("</") ? normalized.slice(2) : normalized.slice(1)).trimStart();
345
- }
346
- function findOpenCodeContextStart(text) {
347
- const fence = findOpenFenceStart(text);
348
- const inline = findOpenInlineCodeStart(text);
349
- if (fence === -1) return inline;
350
- if (inline === -1) return fence;
351
- return Math.min(fence, inline);
352
- }
353
- function findOpenInlineCodeStart(text) {
354
- let openStart = -1;
355
- let openTicks = 0;
356
- let index = 0;
357
- while (index < text.length) {
358
- if (text.charAt(index) !== "`") {
359
- index += 1;
360
- continue;
361
- }
362
- const runStart = index;
363
- let runLength = 0;
364
- while (index < text.length && text.charAt(index) === "`") {
365
- runLength += 1;
366
- index += 1;
367
- }
368
- if (openStart === -1) {
369
- openStart = runStart;
370
- openTicks = runLength;
371
- } else if (runLength === openTicks) {
372
- openStart = -1;
373
- openTicks = 0;
374
- }
375
- }
376
- return openStart;
377
- }
378
- function findOpenFenceStart(text) {
379
- const fenceRe = /(^|\n)(```|~~~)[^\n]*(?:\n|$)/g;
380
- let open = null;
381
- for (const match of text.matchAll(fenceRe)) {
382
- const prefix = match.at(1);
383
- const marker = match.at(2);
384
- if (prefix === void 0 || marker === void 0) continue;
385
- const index = (match.index ?? 0) + prefix.length;
386
- if (open !== null && open.marker === marker) open = null;
387
- else if (!open) open = {
388
- marker,
389
- index
390
- };
391
- }
392
- return open?.index ?? -1;
393
- }
394
- function findTrailingFenceFragmentStart(text, inlineState, fenceState) {
395
- if (inlineState.open || fenceState?.open) return -1;
396
- const lineStart = Math.max(text.lastIndexOf("\n") + 1, 0);
397
- return text.slice(lineStart).match(/^( {0,3})(`{1,2}|~{1,2})$/) ? lineStart : -1;
398
- }
399
- //#endregion
400
- export { createReasoningTagTextPartitioner as t };
@@ -1,150 +0,0 @@
1
- //#region packages/normalization-core/src/number-coercion.ts
2
- /** Returns a number only when the input is already finite. */
3
- function asFiniteNumber(value) {
4
- return typeof value === "number" && Number.isFinite(value) ? value : void 0;
5
- }
6
- /** Returns a finite number only when it satisfies the supplied inclusive/exclusive bounds. */
7
- function asFiniteNumberInRange(value, range) {
8
- const number = asFiniteNumber(value);
9
- if (number === void 0) return;
10
- if (range.min !== void 0) {
11
- if (range.minExclusive ? number <= range.min : number < range.min) return;
12
- }
13
- if (range.max !== void 0) {
14
- if (range.maxExclusive ? number >= range.max : number > range.max) return;
15
- }
16
- return number;
17
- }
18
- function normalizeNumericString(value) {
19
- const trimmed = value.trim();
20
- return trimmed ? trimmed : void 0;
21
- }
22
- /** Parses only safe integer numbers or base-10 integer strings. */
23
- function parseStrictInteger(value) {
24
- if (typeof value === "number") return Number.isSafeInteger(value) ? value : void 0;
25
- if (typeof value !== "string") return;
26
- const normalized = normalizeNumericString(value);
27
- if (!normalized || !/^[+-]?\d+$/.test(normalized)) return;
28
- const parsed = Number(normalized);
29
- return Number.isSafeInteger(parsed) ? parsed : void 0;
30
- }
31
- /** Parses only finite decimal/scientific string tokens, rejecting partial numbers. */
32
- function parseStrictFiniteNumber(value) {
33
- if (typeof value === "number") return Number.isFinite(value) ? value : void 0;
34
- if (typeof value !== "string") return;
35
- const normalized = normalizeNumericString(value);
36
- if (!normalized || !/^[+-]?(?:(?:\d+\.?\d*)|(?:\.\d+))(?:e[+-]?\d+)?$/i.test(normalized)) return;
37
- const parsed = Number(normalized);
38
- return Number.isFinite(parsed) ? parsed : void 0;
39
- }
40
- /** Conservative upper bound for Node timer delays. */
41
- const MAX_TIMER_TIMEOUT_MS = 2147e6;
42
- /** Clamps finite millisecond values into the Node-safe timer range. */
43
- function clampTimerTimeoutMs(valueMs, minMs = 1) {
44
- const value = asFiniteNumber(valueMs);
45
- if (value === void 0) return;
46
- return Math.min(Math.max(Math.floor(value), Math.max(1, Math.floor(minMs))), MAX_TIMER_TIMEOUT_MS);
47
- }
48
- /** Resolves arbitrary timeout input with fallback and minimum timer bounds. */
49
- function resolveTimerTimeoutMs(valueMs, fallbackMs, minMs = 1) {
50
- const value = asFiniteNumber(valueMs) ?? asFiniteNumber(fallbackMs);
51
- const min = Math.max(0, Math.floor(minMs));
52
- if (value === void 0) return min;
53
- return Math.min(Math.max(Math.floor(value), min), MAX_TIMER_TIMEOUT_MS);
54
- }
55
- /** Parses strict positive integer values from numbers or strings. */
56
- function parseStrictPositiveInteger(value) {
57
- const parsed = parseStrictInteger(value);
58
- return parsed !== void 0 && parsed > 0 ? parsed : void 0;
59
- }
60
- /** Parses strict non-negative integer values from numbers or strings. */
61
- function parseStrictNonNegativeInteger(value) {
62
- const parsed = parseStrictInteger(value);
63
- return parsed !== void 0 && parsed >= 0 ? parsed : void 0;
64
- }
65
- //#endregion
66
- //#region packages/ai/src/utils/stream-first-event-timeout.ts
67
- function getFirstStreamEventTimeoutMs(options) {
68
- return options?.firstEventTimeoutMs;
69
- }
70
- function getFirstStreamEventTimeoutHandler(options) {
71
- return options?.onFirstEventTimeout;
72
- }
73
- function formatOptionalField(name, value) {
74
- return value ? ` ${name}=${value}` : "";
75
- }
76
- function createFirstStreamEventTimeoutError(context) {
77
- const stage = context.stage ? `${context.stage} ` : "";
78
- const details = [
79
- formatOptionalField("provider", context.provider),
80
- formatOptionalField("api", context.api),
81
- formatOptionalField("model", context.model)
82
- ].join("");
83
- return /* @__PURE__ */ new Error(`${stage}HTTP stream opened but did not deliver a first SSE event within ${context.timeoutMs}ms after streaming headers (first-event timeout).${details}` + (context.hint ? ` ${context.hint}` : ""));
84
- }
85
- function createFirstStreamEventAbortController(parentSignal) {
86
- const controller = new AbortController();
87
- const abortFromParent = () => {
88
- if (!controller.signal.aborted) controller.abort(parentSignal?.reason);
89
- };
90
- if (parentSignal?.aborted) abortFromParent();
91
- else parentSignal?.addEventListener("abort", abortFromParent, { once: true });
92
- return {
93
- signal: controller.signal,
94
- abort(reason) {
95
- if (!controller.signal.aborted) controller.abort(reason);
96
- },
97
- dispose() {
98
- parentSignal?.removeEventListener("abort", abortFromParent);
99
- }
100
- };
101
- }
102
- function withFirstStreamEventTimeout(stream, context) {
103
- const timeoutMs = clampTimerTimeoutMs(context.timeoutMs);
104
- if (timeoutMs === void 0 || context.timeoutMs <= 0) return stream;
105
- const timeoutContext = {
106
- ...context,
107
- timeoutMs
108
- };
109
- return { async *[Symbol.asyncIterator]() {
110
- const iterator = stream[Symbol.asyncIterator]();
111
- let timer;
112
- let completed = false;
113
- const clear = () => {
114
- if (timer) {
115
- clearTimeout(timer);
116
- timer = void 0;
117
- }
118
- };
119
- try {
120
- const first = await new Promise((resolve, reject) => {
121
- timer = setTimeout(() => {
122
- const timeoutError = createFirstStreamEventTimeoutError(timeoutContext);
123
- timeoutContext.onTimeout?.(timeoutError);
124
- timeoutContext.abort?.(timeoutError);
125
- reject(timeoutError);
126
- }, timeoutMs);
127
- timer.unref?.();
128
- iterator.next().then(resolve, reject);
129
- }).finally(clear);
130
- if (first.done) {
131
- completed = true;
132
- return;
133
- }
134
- yield first.value;
135
- for (;;) {
136
- const next = await iterator.next();
137
- if (next.done) {
138
- completed = true;
139
- return;
140
- }
141
- yield next.value;
142
- }
143
- } finally {
144
- clear();
145
- if (!completed) iterator.return?.().catch(() => void 0);
146
- }
147
- } };
148
- }
149
- //#endregion
150
- export { withFirstStreamEventTimeout as a, parseStrictFiniteNumber as c, resolveTimerTimeoutMs as d, getFirstStreamEventTimeoutMs as i, parseStrictNonNegativeInteger as l, createFirstStreamEventTimeoutError as n, asFiniteNumberInRange as o, getFirstStreamEventTimeoutHandler as r, clampTimerTimeoutMs as s, createFirstStreamEventAbortController as t, parseStrictPositiveInteger as u };