dsh-lcx-codex 0.6.0-pre.1 → 0.6.0

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.
@@ -1,10 +1,11 @@
1
- // @ts-check
2
1
  import { createAssistantMessageEventStream, processResponsesStream, } from "./pi-responses-runtime.js";
3
2
  import { abortError } from "./abort-error.js";
4
- import { IMAGE_OFFLOAD_REQUIRED_CODE, ToolCallId } from "@deepseek-ai/dsh-llm";
5
- import { fetchSseWithRetry } from "./transport.js";
3
+ import { IMAGE_OFFLOAD_REQUIRED_CODE, ToolCallId, isContextWindowExceededError } from "@deepseek-ai/dsh-llm";
4
+ import { fetchSseWithRetry, readSseEvents } from "./transport.js";
6
5
  import { sanitizeContextDetails } from "./search-accounting.js";
7
6
  import { createGrokNativeReplayEnvelope, grokPendingSourcesStart, sanitizeGrokVisibleText, } from "./grok-native-search.js";
7
+ import { codedError, isPositiveFinite, isRecord } from "./util.js";
8
+ import { DEFAULTS } from "./defaults.js";
8
9
  /** Durable LCX-only pressure evidence; canonical usage stays on Pi/DSH billing. */
9
10
  function responseUsageMarker(meta) {
10
11
  return { version: 1, inputTokenScope: meta.inputTokenScope,
@@ -21,23 +22,16 @@ function emptyUsage() {
21
22
  cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
22
23
  };
23
24
  }
24
- /** @param {unknown} value @returns {value is UnknownRecord} */
25
- function isObject(value) {
26
- return value !== null && typeof value === "object" && !Array.isArray(value);
27
- }
28
- /** @param {unknown} raw */
29
25
  function safeStatus(raw) {
30
26
  return Number.isInteger(raw) && Number(raw) >= 100 && Number(raw) <= 599
31
27
  ? Number(raw)
32
28
  : undefined;
33
29
  }
34
- /** @param {unknown} raw */
35
30
  function safeRequestId(raw) {
36
31
  if (typeof raw !== "string" || raw.length === 0 || raw.length > 160)
37
32
  return undefined;
38
33
  return /^[A-Za-z0-9._:\/-]+$/u.test(raw) ? raw : undefined;
39
34
  }
40
- /** @param {unknown} error */
41
35
  function errorFacts(error) {
42
36
  const seen = new Set();
43
37
  const codes = [];
@@ -47,7 +41,7 @@ function errorFacts(error) {
47
41
  let providerRetryAfterMs;
48
42
  let current = error;
49
43
  for (let depth = 0; depth < 8 &&
50
- (current instanceof Error || isObject(current)) &&
44
+ (current instanceof Error || isRecord(current)) &&
51
45
  !seen.has(current); depth += 1) {
52
46
  seen.add(current);
53
47
  const value = current;
@@ -73,7 +67,6 @@ function errorFacts(error) {
73
67
  text: texts.join(" | "),
74
68
  };
75
69
  }
76
- /** @param {unknown} value */
77
70
  function safeMachineField(value) {
78
71
  if (typeof value !== "string" || value.length === 0 || value.length > 96)
79
72
  return undefined;
@@ -93,11 +86,11 @@ function observeUpstreamFailure(event, meta) {
93
86
  if (type === "error") {
94
87
  // OpenAI sends flat `{code, message}`; some gateways nest `{error: {code, type}}`. `type` is only
95
88
  // ever the error type when nested, because the flat `type` is the event name itself.
96
- const nested = isObject(event.error) ? event.error : undefined;
89
+ const nested = isRecord(event.error) ? event.error : undefined;
97
90
  source = { code: nested?.code ?? event.code, type: nested?.type };
98
91
  hasError = true;
99
92
  }
100
- else if (isObject(event.response)) {
93
+ else if (isRecord(event.response)) {
101
94
  const response = event.response;
102
95
  const status = response.status;
103
96
  const failedTerminal = type === "response.failed" ||
@@ -107,13 +100,13 @@ function observeUpstreamFailure(event, meta) {
107
100
  if (!failedTerminal && !incompleteTerminal)
108
101
  return;
109
102
  incomplete = incompleteTerminal;
110
- details = isObject(response.incomplete_details)
103
+ details = isRecord(response.incomplete_details)
111
104
  ? response.incomplete_details.reason
112
105
  : undefined;
113
106
  // max_output_tokens is a length stop that Pi maps to a normal finish, not a failure.
114
107
  if (incompleteTerminal && details === "max_output_tokens")
115
108
  return;
116
- source = isObject(response.error) ? response.error : undefined;
109
+ source = isRecord(response.error) ? response.error : undefined;
117
110
  hasError = source !== undefined;
118
111
  }
119
112
  else
@@ -178,8 +171,6 @@ function classifyUpstreamFailure(upstream) {
178
171
  }
179
172
  /**
180
173
  * Provider bodies/messages are deliberately not surfaced. Only a stable class and safe facts leave the wire boundary.
181
- * @param {unknown} error
182
- * @param {AbortSignal} [signal]
183
174
  */
184
175
  export function managedFailure(error, signal, upstream) {
185
176
  const facts = errorFacts(error);
@@ -210,6 +201,7 @@ export function managedFailure(error, signal, upstream) {
210
201
  else if (sourceCodes.has("LCX_GROK_NATIVE_PROTOCOL_ERROR"))
211
202
  code = "GROK_NATIVE_PROTOCOL_ERROR";
212
203
  else if (sourceCodes.has("LCX_RESPONSES_ROUTE_UNAVAILABLE") ||
204
+ sourceCodes.has("LCX_RESPONSES_API_UNRESOLVED") ||
213
205
  sourceCodes.has("LCX_RESPONSES_MODEL_UNAVAILABLE") ||
214
206
  sourceCodes.has("LCX_SYSTEM_PROMPT_MODE_UNKNOWN"))
215
207
  code = "NO_ADAPTER";
@@ -218,6 +210,7 @@ export function managedFailure(error, signal, upstream) {
218
210
  else if (facts.status === 401 ||
219
211
  facts.status === 403 ||
220
212
  sourceCodes.has("AUTH") ||
213
+ sourceCodes.has("INVALID_CREDENTIAL") ||
221
214
  sourceCodes.has("LCX_CREDENTIAL_UNAVAILABLE"))
222
215
  code = "AUTH";
223
216
  else if (facts.status === 409 || facts.status === 425)
@@ -226,7 +219,10 @@ export function managedFailure(error, signal, upstream) {
226
219
  code = "RATE_LIMIT";
227
220
  else if (facts.status !== undefined && facts.status >= 500)
228
221
  code = "SERVER";
229
- else if (/context (?:window|length)|maximum context|too many tokens/iu.test(text) ||
222
+ else if (
223
+ // DSH's own overflow wording first, then the LCX phrases it does not cover.
224
+ isContextWindowExceededError(text) ||
225
+ /context (?:window|length)|maximum context|too many tokens/iu.test(text) ||
230
226
  [...sourceCodes].some((value) => /context.*(?:window|length|exceed)/iu.test(value)))
231
227
  code = "CONTEXT_WINDOW_EXCEEDED";
232
228
  else if (facts.status === 400 ||
@@ -249,14 +245,16 @@ export function managedFailure(error, signal, upstream) {
249
245
  // Nothing in the text said otherwise: a failure the provider reported without a usable code is its own.
250
246
  if (code === "RESPONSES_ERROR" && upstreamClass !== undefined)
251
247
  code = upstreamClass.code;
252
- const messages = /** @type {Record<string, string>} */ {
248
+ const messages = {
253
249
  ABORTED: "Responses request was aborted",
254
250
  AUTH: "Responses request was rejected by authentication",
255
251
  INSUFFICIENT_QUOTA: "服务商额度不足或未满足请求预留额度,请检查中转额度(并非 API 密钥无效) / Provider quota is insufficient for this request",
256
252
  UNSUPPORTED_OPTION: "LCX Responses does not support this request option",
257
253
  NO_ADAPTER: sourceCodes.has("LCX_SYSTEM_PROMPT_MODE_UNKNOWN")
258
254
  ? "LCX could not read the route's model information, so in-history system prompt updates cannot be sent safely; retry once the route is available"
259
- : "LCX could not resolve the selected Responses route",
255
+ : sourceCodes.has("LCX_RESPONSES_API_UNRESOLVED")
256
+ ? "LCX could not determine the wire protocol of the selected GPT route (the provider profile sets no api and LCX could not infer one for this model); add api: openai-responses to the provider profile or select another model"
257
+ : "LCX could not resolve the selected Responses route",
260
258
  RATE_LIMIT: "Responses provider rate limit was reached",
261
259
  SERVER: "Responses provider returned a server failure",
262
260
  INVALID_REQUEST: sourceCodes.has("LCX_CHECKPOINT_UNSUPPORTED")
@@ -281,7 +279,6 @@ export function managedFailure(error, signal, upstream) {
281
279
  : { providerRetryAfterMs: facts.providerRetryAfterMs }),
282
280
  };
283
281
  }
284
- /** @param {unknown} error @param {AbortSignal} [signal] */
285
282
  /**
286
283
  * The host image-offload contract, when this failure carries it.
287
284
  *
@@ -290,13 +287,11 @@ export function managedFailure(error, signal, upstream) {
290
287
  * code and `offloadImages` count before recording an `image/offload` decision
291
288
  * and retrying. Normalizing it into the closed LCX taxonomy would drop both
292
289
  * fields and turn a recoverable step into a terminal turn failure.
293
- * @param {unknown} error
294
- * @returns {LlmFailure | undefined}
295
290
  */
296
291
  function imageOffloadFailure(error) {
297
- if (!isObject(error))
292
+ if (!isRecord(error))
298
293
  return undefined;
299
- const source = isObject(error.failure) ? error.failure : error;
294
+ const source = isRecord(error.failure) ? error.failure : error;
300
295
  if (source.code !== IMAGE_OFFLOAD_REQUIRED_CODE)
301
296
  return undefined;
302
297
  const offloadImages = source.offloadImages;
@@ -327,150 +322,85 @@ export function managedFailureChunk(error, signal) {
327
322
  : { kind: "error", failure },
328
323
  };
329
324
  }
330
- async function readWithSignal(reader, signal) {
331
- if (!signal)
332
- return reader.read();
333
- if (signal.aborted)
334
- throw abortError(signal);
335
- return new Promise((resolve, reject) => {
336
- const onAbort = () => reject(abortError(signal));
337
- signal.addEventListener("abort", onAbort, { once: true });
338
- reader.read().then(resolve, reject).finally(() => {
339
- signal.removeEventListener("abort", onAbort);
340
- });
341
- });
342
- }
343
- function requestDeadline(parent, timeoutMs) {
325
+ /**
326
+ * An abort controller that follows `parent` and can be closed by the consumer: closing clears the caller's
327
+ * timer, detaches from the parent and aborts with a stable "consumer stopped" reason when nothing else did.
328
+ */
329
+ function linkedAbort(parent, consumerStoppedMessage) {
344
330
  const controller = new AbortController();
345
- let timer;
346
331
  const onParentAbort = () => controller.abort(parent?.reason);
347
332
  if (parent?.aborted)
348
333
  onParentAbort();
349
334
  else
350
335
  parent?.addEventListener("abort", onParentAbort, { once: true });
351
- if (!controller.signal.aborted &&
352
- typeof timeoutMs === "number" &&
353
- Number.isFinite(timeoutMs) &&
354
- timeoutMs > 0)
355
- timer = setTimeout(() => controller.abort(Object.assign(new Error(`Responses request timeout after ${timeoutMs}ms`), { code: "LCX_REQUEST_TIMEOUT" })), timeoutMs);
356
336
  return {
357
337
  signal: controller.signal,
358
- dispose() {
359
- if (timer !== undefined)
360
- clearTimeout(timer);
338
+ abort: (reason) => controller.abort(reason),
339
+ close(clearTimer) {
340
+ clearTimer();
361
341
  parent?.removeEventListener("abort", onParentAbort);
362
342
  if (!controller.signal.aborted)
363
- controller.abort(Object.assign(new Error("Responses request consumer stopped"), {
364
- code: "LCX_ABORTED",
365
- }));
343
+ controller.abort(codedError(consumerStoppedMessage, "LCX_ABORTED"));
366
344
  },
367
345
  };
368
346
  }
347
+ function requestDeadline(parent, timeoutMs) {
348
+ const linked = linkedAbort(parent, "Responses request consumer stopped");
349
+ let timer;
350
+ if (!linked.signal.aborted && isPositiveFinite(timeoutMs))
351
+ timer = setTimeout(() => linked.abort(codedError(`Responses request timeout after ${timeoutMs}ms`, "LCX_REQUEST_TIMEOUT")), timeoutMs);
352
+ return {
353
+ signal: linked.signal,
354
+ dispose: () => linked.close(() => {
355
+ if (timer !== undefined)
356
+ clearTimeout(timer);
357
+ }),
358
+ };
359
+ }
369
360
  function streamIdleWatchdog(parent, idleTimeoutMs) {
370
- const controller = new AbortController();
361
+ const linked = linkedAbort(parent, "Responses stream consumer stopped");
371
362
  let timer;
372
- const onParentAbort = () => controller.abort(parent?.reason);
373
- if (parent?.aborted)
374
- onParentAbort();
375
- else
376
- parent?.addEventListener("abort", onParentAbort, { once: true });
377
363
  const disarm = () => {
378
364
  if (timer !== undefined)
379
365
  clearTimeout(timer);
380
366
  timer = undefined;
381
367
  };
382
368
  const arm = () => {
383
- if (controller.signal.aborted)
369
+ if (linked.signal.aborted)
384
370
  return;
385
371
  disarm();
386
- if (typeof idleTimeoutMs === "number" && Number.isFinite(idleTimeoutMs) && idleTimeoutMs > 0)
387
- timer = setTimeout(() => controller.abort(Object.assign(new Error(`Responses stream idle timeout after ${idleTimeoutMs}ms`), { code: "LCX_STREAM_IDLE_TIMEOUT" })), idleTimeoutMs);
372
+ if (isPositiveFinite(idleTimeoutMs))
373
+ timer = setTimeout(() => linked.abort(codedError(`Responses stream idle timeout after ${idleTimeoutMs}ms`, "LCX_STREAM_IDLE_TIMEOUT")), idleTimeoutMs);
388
374
  };
389
375
  return {
390
- signal: controller.signal,
376
+ signal: linked.signal,
391
377
  arm,
392
378
  disarm,
393
- dispose() {
394
- disarm();
395
- parent?.removeEventListener("abort", onParentAbort);
396
- if (!controller.signal.aborted)
397
- controller.abort(Object.assign(new Error("Responses stream consumer stopped"), {
398
- code: "LCX_ABORTED",
399
- }));
400
- },
379
+ dispose: () => linked.close(disarm),
401
380
  };
402
381
  }
403
382
  /**
404
383
  * Minimal JSON SSE reader. LCX owns the exact HTTP wire; Pi owns event semantics after this boundary.
405
384
  * Stream liveness is measured after Pi emits meaningful DSH progress, not here.
406
- * @param {Response} response
407
- * @param {StreamOptions} [options]
408
385
  */
409
386
  async function* responseEvents(response, options = {}) {
410
- if (!response?.body)
411
- throw Object.assign(new Error("Responses stream returned no body"), {
412
- code: "LCX_INVALID_SSE",
413
- status: response?.status,
414
- });
415
- const reader = response.body.getReader();
416
- const decoder = new TextDecoder();
417
- const maxBytes = options.maxResponseBytes ?? 8 * 1024 * 1024;
418
- let bytes = 0;
419
- let pending = "";
420
- /** @type {string[]} */
421
- let dataLines = [];
422
- const decode = () => {
423
- if (dataLines.length === 0)
424
- return undefined;
425
- const data = dataLines.join("\n");
426
- dataLines = [];
427
- if (data === "[DONE]")
428
- return undefined;
429
- try {
430
- return JSON.parse(data);
431
- }
432
- catch (cause) {
433
- throw Object.assign(new Error("Responses stream contained malformed SSE JSON", { cause }), { code: "LCX_INVALID_SSE" });
434
- }
435
- };
436
- try {
437
- while (true) {
438
- if (options.signal?.aborted)
439
- throw abortError(options.signal);
440
- const { done, value } = await readWithSignal(reader, options.signal);
441
- if (done)
442
- break;
443
- bytes += value.byteLength;
444
- if (bytes > maxBytes)
445
- throw Object.assign(new Error(`Responses SSE exceeds ${maxBytes} bytes`), { code: "LCX_RESPONSE_TOO_LARGE" });
446
- pending += decoder.decode(value, { stream: true });
447
- let newline;
448
- while ((newline = pending.indexOf("\n")) >= 0) {
449
- const line = pending.slice(0, newline).replace(/\r$/u, "");
450
- pending = pending.slice(newline + 1);
451
- if (line === "") {
452
- const event = decode();
453
- if (event !== undefined)
454
- yield event;
455
- }
456
- else if (!line.startsWith(":") && line.startsWith("data:")) {
457
- dataLines.push(line.slice(5).replace(/^ /u, ""));
458
- }
459
- }
460
- }
461
- pending += decoder.decode();
462
- if (pending.startsWith("data:"))
463
- dataLines.push(pending.slice(5).replace(/^ /u, ""));
464
- const event = decode();
465
- if (event !== undefined)
466
- yield event;
467
- }
468
- finally {
469
- await reader.cancel().catch(() => undefined);
470
- }
387
+ yield* readSseEvents(response, {
388
+ signal: options.signal,
389
+ maxBytes: options.maxResponseBytes ?? DEFAULTS.maxResponseBytes,
390
+ errors: {
391
+ noBody: (failed) => Object.assign(new Error("Responses stream returned no body"), {
392
+ code: "LCX_INVALID_SSE",
393
+ status: failed?.status,
394
+ }),
395
+ malformed: (cause) => Object.assign(new Error("Responses stream contained malformed SSE JSON", { cause }), { code: "LCX_INVALID_SSE" }),
396
+ tooLarge: (maxBytes) => Object.assign(new Error(`Responses SSE exceeds ${maxBytes} bytes`), {
397
+ code: "LCX_RESPONSE_TOO_LARGE",
398
+ }),
399
+ },
400
+ });
471
401
  }
472
402
  function isSupportedOutputItem(value) {
473
- if (!isObject(value))
403
+ if (!isRecord(value))
474
404
  return false;
475
405
  if (value.type === "message")
476
406
  return (typeof value.id === "string" &&
@@ -479,7 +409,7 @@ function isSupportedOutputItem(value) {
479
409
  value.status === "completed" ||
480
410
  value.status === "incomplete") &&
481
411
  Array.isArray(value.content) &&
482
- value.content.every((part) => isObject(part) &&
412
+ value.content.every((part) => isRecord(part) &&
483
413
  ((part.type === "output_text" &&
484
414
  typeof part.text === "string" &&
485
415
  Array.isArray(part.annotations)) ||
@@ -487,7 +417,7 @@ function isSupportedOutputItem(value) {
487
417
  if (value.type === "reasoning")
488
418
  return (typeof value.id === "string" &&
489
419
  Array.isArray(value.summary) &&
490
- value.summary.every((part) => isObject(part) && typeof part.text === "string"));
420
+ value.summary.every((part) => isRecord(part) && typeof part.text === "string"));
491
421
  if (value.type === "function_call")
492
422
  return (typeof value.id === "string" &&
493
423
  typeof value.call_id === "string" &&
@@ -500,7 +430,7 @@ function isSupportedOutputItem(value) {
500
430
  typeof value.input === "string");
501
431
  }
502
432
  function isTerminalResponse(value) {
503
- if (!isObject(value) || typeof value.id !== "string")
433
+ if (!isRecord(value) || typeof value.id !== "string")
504
434
  return false;
505
435
  if (!Array.isArray(value.output))
506
436
  return false;
@@ -535,14 +465,14 @@ const PI_RESPONSE_EVENT_TYPES = new Set([
535
465
  * ignoring unrelated Responses events.
536
466
  */
537
467
  function isSupportedResponseStreamEvent(value) {
538
- if (!isObject(value))
468
+ if (!isRecord(value))
539
469
  return false;
540
470
  if (!Number.isSafeInteger(value.sequence_number) ||
541
471
  typeof value.type !== "string" ||
542
472
  !PI_RESPONSE_EVENT_TYPES.has(value.type))
543
473
  return false;
544
474
  if (value.type === "response.created")
545
- return isObject(value.response) && typeof value.response.id === "string";
475
+ return isRecord(value.response) && typeof value.response.id === "string";
546
476
  if (value.type === "response.output_item.added" ||
547
477
  value.type === "response.output_item.done")
548
478
  return Number.isSafeInteger(value.output_index) && isSupportedOutputItem(value.item);
@@ -582,13 +512,13 @@ function isSupportedResponseStreamEvent(value) {
582
512
  value.type === "response.incomplete")
583
513
  return isTerminalResponse(value.response);
584
514
  if (value.type === "response.failed")
585
- return isObject(value.response);
515
+ return isRecord(value.response);
586
516
  return typeof value.code === "string" && typeof value.message === "string";
587
517
  }
588
518
  async function* validatedResponseEvents(source) {
589
519
  let sequenceNumber = 0;
590
520
  for await (const raw of source) {
591
- if (!isObject(raw))
521
+ if (!isRecord(raw))
592
522
  continue;
593
523
  if (!PI_RESPONSE_EVENT_TYPES.has(String(raw.type ?? "")))
594
524
  continue;
@@ -598,7 +528,6 @@ async function* validatedResponseEvents(source) {
598
528
  yield event;
599
529
  }
600
530
  }
601
- /** @param {UnknownRecord} item */
602
531
  function itemKind(item) {
603
532
  if (item.type === "message")
604
533
  return "text";
@@ -608,7 +537,6 @@ function itemKind(item) {
608
537
  return "tool-call";
609
538
  return undefined;
610
539
  }
611
- /** @param {UnknownRecord} item */
612
540
  function itemIdentity(item) {
613
541
  const id = typeof item.id === "string" && item.id
614
542
  ? item.id
@@ -617,11 +545,10 @@ function itemIdentity(item) {
617
545
  : "";
618
546
  return id ? `${String(item.type)}:${id}` : undefined;
619
547
  }
620
- /** @param {UnknownRecord} item */
621
548
  function itemText(item) {
622
549
  if (item.type === "message")
623
550
  return (Array.isArray(item.content) ? item.content : [])
624
- .map((part) => isObject(part) &&
551
+ .map((part) => isRecord(part) &&
625
552
  (part.type === "output_text" || part.type === "refusal")
626
553
  ? String(part.text ?? part.refusal ?? "")
627
554
  : "")
@@ -633,7 +560,7 @@ function itemText(item) {
633
560
  ? item.content
634
561
  : [];
635
562
  return parts
636
- .map((part) => isObject(part) ? String(part.text ?? "") : "")
563
+ .map((part) => isRecord(part) ? String(part.text ?? "") : "")
637
564
  .join("\n\n");
638
565
  }
639
566
  if (item.type === "function_call")
@@ -645,7 +572,7 @@ function itemText(item) {
645
572
  function itemAnnotations(item) {
646
573
  if (item.type !== "message" || !Array.isArray(item.content))
647
574
  return [];
648
- return item.content.flatMap((part) => isObject(part) && Array.isArray(part.annotations) ? part.annotations : []);
575
+ return item.content.flatMap((part) => isRecord(part) && Array.isArray(part.annotations) ? part.annotations : []);
649
576
  }
650
577
  function sanitizeGrokTerminalItem(item, index) {
651
578
  const normalized = normalizedTerminalItem(item, index);
@@ -654,7 +581,7 @@ function sanitizeGrokTerminalItem(item, index) {
654
581
  return {
655
582
  ...normalized,
656
583
  content: normalized.content.map((part) => {
657
- if (!isObject(part) || (part.type !== "output_text" && part.type !== "refusal"))
584
+ if (!isRecord(part) || (part.type !== "output_text" && part.type !== "refusal"))
658
585
  return structuredClone(part);
659
586
  const text = sanitizeGrokVisibleText(String(part.text ?? part.refusal ?? ""), Array.isArray(part.annotations) ? part.annotations : []);
660
587
  return part.type === "output_text"
@@ -663,7 +590,6 @@ function sanitizeGrokTerminalItem(item, index) {
663
590
  }),
664
591
  };
665
592
  }
666
- /** @param {UnknownRecord} item @param {number} index */
667
593
  function normalizedTerminalItem(item, index) {
668
594
  if (item.type === "message")
669
595
  return {
@@ -701,7 +627,6 @@ function normalizedTerminalItem(item, index) {
701
627
  };
702
628
  return structuredClone(item);
703
629
  }
704
- /** @param {UnknownRecord} item @param {number} index */
705
630
  function addedShell(item, index) {
706
631
  if (item.type === "message")
707
632
  return {
@@ -729,11 +654,10 @@ function addedShell(item, index) {
729
654
  input: "",
730
655
  };
731
656
  }
732
- /** @param {UnknownRecord} record @param {UnknownRecord} item */
733
657
  function recordMatches(record, item) {
734
658
  if (record.kind !== itemKind(item))
735
659
  return false;
736
- const recordItem = /** @type {UnknownRecord} */ record.item;
660
+ const recordItem = record.item;
737
661
  if (itemIdentity(recordItem) &&
738
662
  itemIdentity(recordItem) === itemIdentity(item))
739
663
  return true;
@@ -748,8 +672,6 @@ function recordMatches(record, item) {
748
672
  /**
749
673
  * Some compatible gateways omit `response.output_item.added/done`, or terminal output indexes drift
750
674
  * when a reasoning item is inserted. Normalize only the missing framing; Pi remains authoritative for item semantics.
751
- * @param {AsyncIterable<unknown>} source
752
- * @param {{ responseModel?: string }} [meta]
753
675
  */
754
676
  async function* normalizedResponseEvents(source, meta = {}, options = {}) {
755
677
  const open = new Map();
@@ -801,7 +723,7 @@ async function* normalizedResponseEvents(source, meta = {}, options = {}) {
801
723
  const hiddenServerToolIndexes = new Set();
802
724
  const serverToolIds = new Map();
803
725
  const nativeServerSearchEchoCallIds = new Set();
804
- const isServerToolItem = (value) => isObject(value) && (options.serverToolTypes?.has(String(value.type ?? "")) === true ||
726
+ const isServerToolItem = (value) => isRecord(value) && (options.serverToolTypes?.has(String(value.type ?? "")) === true ||
805
727
  options.isServerToolItem?.(value) === true);
806
728
  const observeServerTool = (item) => {
807
729
  meta.inputTokenScope = "aggregate";
@@ -815,7 +737,6 @@ async function* normalizedResponseEvents(source, meta = {}, options = {}) {
815
737
  }
816
738
  ids.add(typeof item.id === "string" && item.id ? item.id : `${type}:${ids.size}`);
817
739
  };
818
- /** @param {number} index @param {UnknownRecord} item */
819
740
  const ensure = function* (index, item) {
820
741
  let record = open.get(index);
821
742
  if (record)
@@ -831,19 +752,19 @@ async function* normalizedResponseEvents(source, meta = {}, options = {}) {
831
752
  return record;
832
753
  };
833
754
  for await (const raw of source) {
834
- if (!isObject(raw))
755
+ if (!isRecord(raw))
835
756
  continue;
836
- const event = /** @type {UnknownRecord} */ raw;
757
+ const event = raw;
837
758
  observeUpstreamFailure(event, meta);
838
759
  if (options.serverToolTypes && ['response.completed', 'response.failed', 'response.incomplete'].includes(String(event.type))) {
839
- meta.contextDetails = sanitizeContextDetails(isObject(event.response) && isObject(event.response.usage)
760
+ meta.contextDetails = sanitizeContextDetails(isRecord(event.response) && isRecord(event.response.usage)
840
761
  ? event.response.usage.context_details : undefined);
841
762
  }
842
763
  // Scope is response-local evidence, including failed/incomplete terminal samples.
843
- if (options.serverToolTypes && isObject(event.response)) {
764
+ if (options.serverToolTypes && isRecord(event.response)) {
844
765
  const response = event.response;
845
- const usage = isObject(response.usage) ? response.usage : {};
846
- const details = isObject(usage.server_side_tool_usage_details) ? usage.server_side_tool_usage_details : {};
766
+ const usage = isRecord(response.usage) ? response.usage : {};
767
+ const details = isRecord(usage.server_side_tool_usage_details) ? usage.server_side_tool_usage_details : {};
847
768
  if (Number(usage.num_server_side_tools_used ?? 0) > 0 ||
848
769
  Number(details.web_search_calls ?? 0) > 0 || Number(details.x_search_calls ?? 0) > 0)
849
770
  meta.inputTokenScope = "aggregate";
@@ -854,7 +775,7 @@ async function* normalizedResponseEvents(source, meta = {}, options = {}) {
854
775
  const index = Number.isInteger(event.output_index)
855
776
  ? Number(event.output_index)
856
777
  : 0;
857
- if (event.type === "response.output_item.done" && isObject(event.item))
778
+ if (event.type === "response.output_item.done" && isRecord(event.item))
858
779
  completedWireItems.set(index, structuredClone(event.item));
859
780
  if ((event.type === "response.output_item.added" ||
860
781
  event.type === "response.output_item.done") &&
@@ -876,10 +797,10 @@ async function* normalizedResponseEvents(source, meta = {}, options = {}) {
876
797
  // Keep them in completedWireItems/nativeOutput, but do not open empty UI blocks.
877
798
  if (options.serverToolTypes &&
878
799
  (event.type === "response.output_item.added" || event.type === "response.output_item.done") &&
879
- isObject(event.item) && event.item.type === "reasoning" &&
800
+ isRecord(event.item) && event.item.type === "reasoning" &&
880
801
  !itemText(event.item).trim() && !open.get(index)?.text.trim())
881
802
  continue;
882
- if (event.type === "response.output_item.added" && isObject(event.item)) {
803
+ if (event.type === "response.output_item.added" && isRecord(event.item)) {
883
804
  const rawItem = normalizedTerminalItem(event.item, index);
884
805
  const item = options.serverToolTypes ? sanitizeGrokTerminalItem(rawItem, index) : rawItem;
885
806
  open.set(index, { kind: itemKind(item), item: rawItem, text: itemText(rawItem) });
@@ -987,7 +908,7 @@ async function* normalizedResponseEvents(source, meta = {}, options = {}) {
987
908
  yield event;
988
909
  continue;
989
910
  }
990
- if (event.type === "response.output_item.done" && isObject(event.item)) {
911
+ if (event.type === "response.output_item.done" && isRecord(event.item)) {
991
912
  const rawItem = normalizedTerminalItem(event.item, index);
992
913
  const item = options.serverToolTypes ? sanitizeGrokTerminalItem(rawItem, index) : rawItem;
993
914
  if (options.serverToolTypes && item.type === "message") {
@@ -1004,8 +925,8 @@ async function* normalizedResponseEvents(source, meta = {}, options = {}) {
1004
925
  }
1005
926
  if ((event.type === "response.completed" ||
1006
927
  event.type === "response.incomplete") &&
1007
- isObject(event.response)) {
1008
- const response = /** @type {UnknownRecord} */ event.response;
928
+ isRecord(event.response)) {
929
+ const response = event.response;
1009
930
  if (typeof response.model === "string" && response.model.length > 0)
1010
931
  meta.responseModel = response.model;
1011
932
  const terminalOutput = Array.isArray(response.output) && response.output.length > 0
@@ -1015,20 +936,20 @@ async function* normalizedResponseEvents(source, meta = {}, options = {}) {
1015
936
  .map(([, item]) => item);
1016
937
  const nativeOutput = terminalOutput.map((item) => structuredClone(item));
1017
938
  for (const item of terminalOutput) {
1018
- if (isServerToolItem(item) && isObject(item) && item.type === "custom_tool_call" &&
939
+ if (isServerToolItem(item) && isRecord(item) && item.type === "custom_tool_call" &&
1019
940
  item.status !== "completed")
1020
941
  throw Object.assign(new Error("Grok native custom search item did not complete on the server"), { code: "LCX_GROK_NATIVE_PROTOCOL_ERROR" });
1021
942
  if (isServerToolItem(item))
1022
943
  observeServerTool(item);
1023
- else if (options.serverToolTypes && isObject(item) && item.type === "custom_tool_call" &&
944
+ else if (options.serverToolTypes && isRecord(item) && item.type === "custom_tool_call" &&
1024
945
  typeof item.name === "string" && options.declaredToolNames?.has(item.name) !== true)
1025
946
  throw Object.assign(new Error(`Grok returned undeclared client custom tool "${item.name}"`), { code: "LCX_GROK_NATIVE_PROTOCOL_ERROR" });
1026
947
  }
1027
948
  const output = terminalOutput
1028
949
  .filter((item) => !isServerToolItem(item))
1029
- .filter((item) => !(options.serverToolTypes && isObject(item) &&
950
+ .filter((item) => !(options.serverToolTypes && isRecord(item) &&
1030
951
  item.type === "reasoning" && !itemText(item).trim()))
1031
- .map((item, terminalIndex) => isObject(item)
952
+ .map((item, terminalIndex) => isRecord(item)
1032
953
  ? (options.serverToolTypes
1033
954
  ? sanitizeGrokTerminalItem(item, terminalIndex)
1034
955
  : normalizedTerminalItem(item, terminalIndex))
@@ -1038,8 +959,8 @@ async function* normalizedResponseEvents(source, meta = {}, options = {}) {
1038
959
  options.serverToolTypes) {
1039
960
  meta.nativeOutput = nativeOutput;
1040
961
  meta.nativeServerSearchEchoCallIds = [...nativeServerSearchEchoCallIds];
1041
- const usage = isObject(response.usage) ? response.usage : {};
1042
- const details = isObject(usage.server_side_tool_usage_details) ? usage.server_side_tool_usage_details : {};
962
+ const usage = isRecord(response.usage) ? response.usage : {};
963
+ const details = isRecord(usage.server_side_tool_usage_details) ? usage.server_side_tool_usage_details : {};
1043
964
  meta.inputTokenScope = meta.inputTokenScope === "aggregate" || serverToolIds.size > 0 || nativeOutput.some(isServerToolItem)
1044
965
  || Number(usage.num_server_side_tools_used ?? 0) > 0
1045
966
  || Number(details.web_search_calls ?? 0) > 0 || Number(details.x_search_calls ?? 0) > 0
@@ -1048,12 +969,12 @@ async function* normalizedResponseEvents(source, meta = {}, options = {}) {
1048
969
  const used = new Set();
1049
970
  for (const [streamIndex, record] of open) {
1050
971
  let terminalIndex = output.findIndex((item, candidateIndex) => !used.has(candidateIndex) &&
1051
- isObject(item) &&
972
+ isRecord(item) &&
1052
973
  candidateIndex === streamIndex &&
1053
974
  recordMatches(record, item));
1054
975
  if (terminalIndex < 0)
1055
976
  terminalIndex = output.findIndex((item, candidateIndex) => !used.has(candidateIndex) &&
1056
- isObject(item) &&
977
+ isRecord(item) &&
1057
978
  recordMatches(record, item));
1058
979
  const item = terminalIndex >= 0
1059
980
  ? output[terminalIndex]
@@ -1077,7 +998,7 @@ async function* normalizedResponseEvents(source, meta = {}, options = {}) {
1077
998
  open.clear();
1078
999
  grokTextProjection.clear();
1079
1000
  for (const [terminalIndex, candidate] of output.entries()) {
1080
- if (!isObject(candidate) ||
1001
+ if (!isRecord(candidate) ||
1081
1002
  !itemKind(candidate) ||
1082
1003
  used.has(terminalIndex))
1083
1004
  continue;
@@ -1136,8 +1057,8 @@ async function* normalizedResponseEvents(source, meta = {}, options = {}) {
1136
1057
  completed.add(identity);
1137
1058
  }
1138
1059
  if (options.onServerToolUsage) {
1139
- const usage = isObject(response.usage) ? response.usage : {};
1140
- const details = isObject(usage.server_side_tool_usage_details)
1060
+ const usage = isRecord(response.usage) ? response.usage : {};
1061
+ const details = isRecord(usage.server_side_tool_usage_details)
1141
1062
  ? usage.server_side_tool_usage_details
1142
1063
  : {};
1143
1064
  const safeCount = (value, fallback) => Number.isSafeInteger(value) && Number(value) >= 0
@@ -1155,9 +1076,8 @@ async function* normalizedResponseEvents(source, meta = {}, options = {}) {
1155
1076
  yield event;
1156
1077
  }
1157
1078
  }
1158
- /** @param {unknown} usage */
1159
1079
  function isManagedFailure(value) {
1160
- return (isObject(value) &&
1080
+ return (isRecord(value) &&
1161
1081
  typeof value.message === "string" &&
1162
1082
  typeof value.code === "string" &&
1163
1083
  (value.status === undefined || typeof value.status === "number") &&
@@ -1166,7 +1086,7 @@ function isManagedFailure(value) {
1166
1086
  typeof value.providerRetryAfterMs === "number"));
1167
1087
  }
1168
1088
  function dshUsage(usage) {
1169
- const value = /** @type {UnknownRecord} */ isObject(usage) ? usage : {};
1089
+ const value = isRecord(usage) ? usage : {};
1170
1090
  const inputTokens = Number(value.input ?? 0);
1171
1091
  const outputTokens = Number(value.output ?? 0);
1172
1092
  const cacheReadTokens = Number(value.cacheRead ?? 0);
@@ -1185,21 +1105,19 @@ function dshUsage(usage) {
1185
1105
  : {}),
1186
1106
  };
1187
1107
  }
1188
- /** @param {unknown} value */
1189
1108
  function rawArguments(value) {
1190
1109
  try {
1191
- return JSON.stringify(isObject(value) ? value : {});
1110
+ return JSON.stringify(isRecord(value) ? value : {});
1192
1111
  }
1193
1112
  catch {
1194
1113
  return "{}";
1195
1114
  }
1196
1115
  }
1197
- /** @param {unknown} message */
1198
1116
  function replayState(message, nativeOutput, nativeReplayRoute, nativeServerSearchEchoCallIds = []) {
1199
- if (!isObject(message))
1117
+ if (!isRecord(message))
1200
1118
  return undefined;
1201
1119
  const content = Array.isArray(message.content)
1202
- ? message.content.filter(isObject)
1120
+ ? message.content.filter(isRecord)
1203
1121
  : [];
1204
1122
  const provider = typeof message.provider === "string" ? message.provider : undefined;
1205
1123
  const model = typeof message.model === "string" ? message.model : undefined;
@@ -1220,6 +1138,9 @@ function replayState(message, nativeOutput, nativeReplayRoute, nativeServerSearc
1220
1138
  ...(typeof message.responseId === "string"
1221
1139
  ? { responseId: message.responseId }
1222
1140
  : {}),
1141
+ ...(typeof message.providerThinkingLevel === "string"
1142
+ ? { providerThinkingLevel: message.providerThinkingLevel }
1143
+ : {}),
1223
1144
  stopReason: message.stopReason,
1224
1145
  },
1225
1146
  blocks: content.map((block) => {
@@ -1253,12 +1174,11 @@ function replayState(message, nativeOutput, nativeReplayRoute, nativeServerSearc
1253
1174
  ...(grokNative === undefined ? {} : { grokNative }),
1254
1175
  };
1255
1176
  }
1256
- /** @param {unknown} message */
1257
1177
  function successfulFinish(message) {
1258
- const value = /** @type {UnknownRecord} */ isObject(message) ? message : {};
1178
+ const value = isRecord(message) ? message : {};
1259
1179
  const stopReason = String(value.stopReason ?? "stop");
1260
- const content = /** @type {UnknownRecord[]} */ Array.isArray(value.content)
1261
- ? value.content.filter(isObject)
1180
+ const content = Array.isArray(value.content)
1181
+ ? value.content.filter(isRecord)
1262
1182
  : [];
1263
1183
  if (stopReason === "length")
1264
1184
  return { kind: "max-tokens" };
@@ -1277,8 +1197,6 @@ function successfulFinish(message) {
1277
1197
  }
1278
1198
  /**
1279
1199
  * Pi event vocabulary -> DSH StreamChunk. This remains thin and provider-neutral.
1280
- * @param {AsyncIterable<AssistantMessageEvent>} events
1281
- * @param {AbortSignal} [signal]
1282
1200
  */
1283
1201
  async function* toDshChunks(events, signal, wireMeta, nativeReplayRoute) {
1284
1202
  const toolIds = new Map();
@@ -1329,12 +1247,12 @@ async function* toDshChunks(events, signal, wireMeta, nativeReplayRoute) {
1329
1247
  };
1330
1248
  break;
1331
1249
  case "toolcall_start": {
1332
- const partial = /** @type {UnknownRecord | undefined} */ isObject(event.partial)
1250
+ const partial = isRecord(event.partial)
1333
1251
  ? event.partial
1334
1252
  : undefined;
1335
1253
  const content = Array.isArray(partial?.content) ? partial.content : [];
1336
1254
  const item = content[Number(event.contentIndex)];
1337
- const block = isObject(item) ? item : undefined;
1255
+ const block = isRecord(item) ? item : undefined;
1338
1256
  toolIds.set(Number(event.contentIndex), {
1339
1257
  id: String(block?.id ?? ""),
1340
1258
  name: String(block?.name ?? ""),
@@ -1361,7 +1279,7 @@ async function* toDshChunks(events, signal, wireMeta, nativeReplayRoute) {
1361
1279
  break;
1362
1280
  }
1363
1281
  case "toolcall_end": {
1364
- const call = isObject(event.toolCall)
1282
+ const call = isRecord(event.toolCall)
1365
1283
  ? event.toolCall
1366
1284
  : {};
1367
1285
  yield {
@@ -1377,7 +1295,7 @@ async function* toDshChunks(events, signal, wireMeta, nativeReplayRoute) {
1377
1295
  break;
1378
1296
  }
1379
1297
  case "done": {
1380
- const message = isObject(event.message)
1298
+ const message = isRecord(event.message)
1381
1299
  ? event.message
1382
1300
  : {};
1383
1301
  yield { type: "usage", usage: dshUsage(message.usage) };
@@ -1396,7 +1314,7 @@ async function* toDshChunks(events, signal, wireMeta, nativeReplayRoute) {
1396
1314
  return;
1397
1315
  }
1398
1316
  case "error": {
1399
- const message = isObject(event.error)
1317
+ const message = isRecord(event.error)
1400
1318
  ? event.error
1401
1319
  : {};
1402
1320
  yield { type: "usage", usage: dshUsage(message.usage) };
@@ -1433,22 +1351,8 @@ async function* toDshChunks(events, signal, wireMeta, nativeReplayRoute) {
1433
1351
  /**
1434
1352
  * Send one LCX-owned OpenAI Responses request. Ordinary and replay use one provider attempt;
1435
1353
  * the DSH agent recovery layer remains the visible retry owner.
1436
- * @param {object} options
1437
- * @param {string} options.baseURL
1438
- * @param {string} options.provider
1439
- * @param {string} options.model
1440
- * @param {UnknownRecord} options.piModel
1441
- * @param {UnknownRecord} options.body
1442
- * @param {Map<string, string>} [options.grammarToolInputProperties]
1443
- * @param {Record<string, string>} [options.headers]
1444
- * @param {AbortSignal} [options.signal]
1445
- * @param {number} [options.timeoutMs]
1446
- * @param {number} [options.maxAttempts]
1447
- * @param {number} [options.maxResponseBytes]
1448
- * @param {ReadonlySet<string>} [options.serverToolTypes]
1449
- * @param {(usage: ServerToolUsage) => void} [options.onServerToolUsage]
1450
1354
  */
1451
- export async function* streamResponsesRequest({ baseURL, provider, model, piModel, body, grammarToolInputProperties, headers, signal, timeoutMs, applyDefaultTimeout = true, streamIdleTimeoutMs, maxAttempts = 1, maxResponseBytes, serverToolTypes, isServerToolItem, declaredToolNames, nativeReplayRoute, onServerToolUsage, onFailure, }) {
1355
+ export async function* streamResponsesRequest({ baseURL, provider, model, piModel, body, grammarToolInputProperties, headers, signal, timeoutMs, applyDefaultTimeout = true, streamIdleTimeoutMs, maxAttempts = 1, maxResponseBytes, serverToolTypes, isServerToolItem, declaredToolNames, nativeReplayRoute, providerThinkingLevel, onServerToolUsage, onFailure, }) {
1452
1356
  let contentEmitted = false;
1453
1357
  let failureReported = false;
1454
1358
  /** One redacted report per request; user aborts are not failures. */
@@ -1475,7 +1379,7 @@ export async function* streamResponsesRequest({ baseURL, provider, model, piMode
1475
1379
  // Diagnostics must never change the stream outcome.
1476
1380
  }
1477
1381
  };
1478
- const effectiveTimeoutMs = timeoutMs ?? (applyDefaultTimeout ? 300_000 : undefined);
1382
+ const effectiveTimeoutMs = timeoutMs ?? (applyDefaultTimeout ? DEFAULTS.requestTimeoutMs : undefined);
1479
1383
  const deadline = requestDeadline(signal, effectiveTimeoutMs);
1480
1384
  const watchdog = streamIdleWatchdog(deadline.signal, streamIdleTimeoutMs);
1481
1385
  try {
@@ -1489,6 +1393,7 @@ export async function* streamResponsesRequest({ baseURL, provider, model, piMode
1489
1393
  api: "openai-responses",
1490
1394
  provider,
1491
1395
  model,
1396
+ ...(providerThinkingLevel === undefined ? {} : { providerThinkingLevel }),
1492
1397
  usage: emptyUsage(),
1493
1398
  stopReason: "pending",
1494
1399
  timestamp: Date.now(),
@@ -1525,7 +1430,7 @@ export async function* streamResponsesRequest({ baseURL, provider, model, piMode
1525
1430
  output.__lcxFailure = managedFailure(error, signal, wireMeta.upstreamFailure);
1526
1431
  piEvents.push({
1527
1432
  type: "error",
1528
- reason: /** @type {'aborted' | 'error'} */ output.stopReason,
1433
+ reason: output.stopReason,
1529
1434
  error: output,
1530
1435
  });
1531
1436
  piEvents.end();