dsh-lcx-codex 0.5.2 → 0.6.0-pre.2

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);
@@ -211,14 +202,15 @@ export function managedFailure(error, signal, upstream) {
211
202
  code = "GROK_NATIVE_PROTOCOL_ERROR";
212
203
  else if (sourceCodes.has("LCX_RESPONSES_ROUTE_UNAVAILABLE") ||
213
204
  sourceCodes.has("LCX_RESPONSES_API_UNRESOLVED") ||
214
- sourceCodes.has("LCX_RESPONSES_MODEL_NOT_CONFIGURED") ||
215
- sourceCodes.has("LCX_RESPONSES_MODEL_UNAVAILABLE"))
205
+ sourceCodes.has("LCX_RESPONSES_MODEL_UNAVAILABLE") ||
206
+ sourceCodes.has("LCX_SYSTEM_PROMPT_MODE_UNKNOWN"))
216
207
  code = "NO_ADAPTER";
217
208
  else if (sourceCodes.has("LCX_INSUFFICIENT_QUOTA"))
218
209
  code = "INSUFFICIENT_QUOTA";
219
210
  else if (facts.status === 401 ||
220
211
  facts.status === 403 ||
221
212
  sourceCodes.has("AUTH") ||
213
+ sourceCodes.has("INVALID_CREDENTIAL") ||
222
214
  sourceCodes.has("LCX_CREDENTIAL_UNAVAILABLE"))
223
215
  code = "AUTH";
224
216
  else if (facts.status === 409 || facts.status === 425)
@@ -227,7 +219,10 @@ export function managedFailure(error, signal, upstream) {
227
219
  code = "RATE_LIMIT";
228
220
  else if (facts.status !== undefined && facts.status >= 500)
229
221
  code = "SERVER";
230
- 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) ||
231
226
  [...sourceCodes].some((value) => /context.*(?:window|length|exceed)/iu.test(value)))
232
227
  code = "CONTEXT_WINDOW_EXCEEDED";
233
228
  else if (facts.status === 400 ||
@@ -250,15 +245,15 @@ export function managedFailure(error, signal, upstream) {
250
245
  // Nothing in the text said otherwise: a failure the provider reported without a usable code is its own.
251
246
  if (code === "RESPONSES_ERROR" && upstreamClass !== undefined)
252
247
  code = upstreamClass.code;
253
- const messages = /** @type {Record<string, string>} */ {
248
+ const messages = {
254
249
  ABORTED: "Responses request was aborted",
255
250
  AUTH: "Responses request was rejected by authentication",
256
251
  INSUFFICIENT_QUOTA: "服务商额度不足或未满足请求预留额度,请检查中转额度(并非 API 密钥无效) / Provider quota is insufficient for this request",
257
252
  UNSUPPORTED_OPTION: "LCX Responses does not support this request option",
258
- NO_ADAPTER: sourceCodes.has("LCX_RESPONSES_MODEL_NOT_CONFIGURED")
259
- ? "LCX is enabled but the selected model is not configured for this provider (the provider profile lists other models); add it to the profile's models list or select a listed model"
253
+ NO_ADAPTER: sourceCodes.has("LCX_SYSTEM_PROMPT_MODE_UNKNOWN")
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"
260
255
  : sourceCodes.has("LCX_RESPONSES_API_UNRESOLVED")
261
- ? "LCX could not determine the wire protocol of the selected GPT route (the provider profile sets no api and the model is unknown to the catalog); add api: openai-responses to the provider profile or select a model DSH recognizes"
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"
262
257
  : "LCX could not resolve the selected Responses route",
263
258
  RATE_LIMIT: "Responses provider rate limit was reached",
264
259
  SERVER: "Responses provider returned a server failure",
@@ -284,7 +279,6 @@ export function managedFailure(error, signal, upstream) {
284
279
  : { providerRetryAfterMs: facts.providerRetryAfterMs }),
285
280
  };
286
281
  }
287
- /** @param {unknown} error @param {AbortSignal} [signal] */
288
282
  /**
289
283
  * The host image-offload contract, when this failure carries it.
290
284
  *
@@ -293,13 +287,11 @@ export function managedFailure(error, signal, upstream) {
293
287
  * code and `offloadImages` count before recording an `image/offload` decision
294
288
  * and retrying. Normalizing it into the closed LCX taxonomy would drop both
295
289
  * fields and turn a recoverable step into a terminal turn failure.
296
- * @param {unknown} error
297
- * @returns {LlmFailure | undefined}
298
290
  */
299
291
  function imageOffloadFailure(error) {
300
- if (!isObject(error))
292
+ if (!isRecord(error))
301
293
  return undefined;
302
- const source = isObject(error.failure) ? error.failure : error;
294
+ const source = isRecord(error.failure) ? error.failure : error;
303
295
  if (source.code !== IMAGE_OFFLOAD_REQUIRED_CODE)
304
296
  return undefined;
305
297
  const offloadImages = source.offloadImages;
@@ -330,150 +322,85 @@ export function managedFailureChunk(error, signal) {
330
322
  : { kind: "error", failure },
331
323
  };
332
324
  }
333
- async function readWithSignal(reader, signal) {
334
- if (!signal)
335
- return reader.read();
336
- if (signal.aborted)
337
- throw abortError(signal);
338
- return new Promise((resolve, reject) => {
339
- const onAbort = () => reject(abortError(signal));
340
- signal.addEventListener("abort", onAbort, { once: true });
341
- reader.read().then(resolve, reject).finally(() => {
342
- signal.removeEventListener("abort", onAbort);
343
- });
344
- });
345
- }
346
- 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) {
347
330
  const controller = new AbortController();
348
- let timer;
349
331
  const onParentAbort = () => controller.abort(parent?.reason);
350
332
  if (parent?.aborted)
351
333
  onParentAbort();
352
334
  else
353
335
  parent?.addEventListener("abort", onParentAbort, { once: true });
354
- if (!controller.signal.aborted &&
355
- typeof timeoutMs === "number" &&
356
- Number.isFinite(timeoutMs) &&
357
- timeoutMs > 0)
358
- timer = setTimeout(() => controller.abort(Object.assign(new Error(`Responses request timeout after ${timeoutMs}ms`), { code: "LCX_REQUEST_TIMEOUT" })), timeoutMs);
359
336
  return {
360
337
  signal: controller.signal,
361
- dispose() {
362
- if (timer !== undefined)
363
- clearTimeout(timer);
338
+ abort: (reason) => controller.abort(reason),
339
+ close(clearTimer) {
340
+ clearTimer();
364
341
  parent?.removeEventListener("abort", onParentAbort);
365
342
  if (!controller.signal.aborted)
366
- controller.abort(Object.assign(new Error("Responses request consumer stopped"), {
367
- code: "LCX_ABORTED",
368
- }));
343
+ controller.abort(codedError(consumerStoppedMessage, "LCX_ABORTED"));
369
344
  },
370
345
  };
371
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
+ }
372
360
  function streamIdleWatchdog(parent, idleTimeoutMs) {
373
- const controller = new AbortController();
361
+ const linked = linkedAbort(parent, "Responses stream consumer stopped");
374
362
  let timer;
375
- const onParentAbort = () => controller.abort(parent?.reason);
376
- if (parent?.aborted)
377
- onParentAbort();
378
- else
379
- parent?.addEventListener("abort", onParentAbort, { once: true });
380
363
  const disarm = () => {
381
364
  if (timer !== undefined)
382
365
  clearTimeout(timer);
383
366
  timer = undefined;
384
367
  };
385
368
  const arm = () => {
386
- if (controller.signal.aborted)
369
+ if (linked.signal.aborted)
387
370
  return;
388
371
  disarm();
389
- if (typeof idleTimeoutMs === "number" && Number.isFinite(idleTimeoutMs) && idleTimeoutMs > 0)
390
- 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);
391
374
  };
392
375
  return {
393
- signal: controller.signal,
376
+ signal: linked.signal,
394
377
  arm,
395
378
  disarm,
396
- dispose() {
397
- disarm();
398
- parent?.removeEventListener("abort", onParentAbort);
399
- if (!controller.signal.aborted)
400
- controller.abort(Object.assign(new Error("Responses stream consumer stopped"), {
401
- code: "LCX_ABORTED",
402
- }));
403
- },
379
+ dispose: () => linked.close(disarm),
404
380
  };
405
381
  }
406
382
  /**
407
383
  * Minimal JSON SSE reader. LCX owns the exact HTTP wire; Pi owns event semantics after this boundary.
408
384
  * Stream liveness is measured after Pi emits meaningful DSH progress, not here.
409
- * @param {Response} response
410
- * @param {StreamOptions} [options]
411
385
  */
412
386
  async function* responseEvents(response, options = {}) {
413
- if (!response?.body)
414
- throw Object.assign(new Error("Responses stream returned no body"), {
415
- code: "LCX_INVALID_SSE",
416
- status: response?.status,
417
- });
418
- const reader = response.body.getReader();
419
- const decoder = new TextDecoder();
420
- const maxBytes = options.maxResponseBytes ?? 8 * 1024 * 1024;
421
- let bytes = 0;
422
- let pending = "";
423
- /** @type {string[]} */
424
- let dataLines = [];
425
- const decode = () => {
426
- if (dataLines.length === 0)
427
- return undefined;
428
- const data = dataLines.join("\n");
429
- dataLines = [];
430
- if (data === "[DONE]")
431
- return undefined;
432
- try {
433
- return JSON.parse(data);
434
- }
435
- catch (cause) {
436
- throw Object.assign(new Error("Responses stream contained malformed SSE JSON", { cause }), { code: "LCX_INVALID_SSE" });
437
- }
438
- };
439
- try {
440
- while (true) {
441
- if (options.signal?.aborted)
442
- throw abortError(options.signal);
443
- const { done, value } = await readWithSignal(reader, options.signal);
444
- if (done)
445
- break;
446
- bytes += value.byteLength;
447
- if (bytes > maxBytes)
448
- throw Object.assign(new Error(`Responses SSE exceeds ${maxBytes} bytes`), { code: "LCX_RESPONSE_TOO_LARGE" });
449
- pending += decoder.decode(value, { stream: true });
450
- let newline;
451
- while ((newline = pending.indexOf("\n")) >= 0) {
452
- const line = pending.slice(0, newline).replace(/\r$/u, "");
453
- pending = pending.slice(newline + 1);
454
- if (line === "") {
455
- const event = decode();
456
- if (event !== undefined)
457
- yield event;
458
- }
459
- else if (!line.startsWith(":") && line.startsWith("data:")) {
460
- dataLines.push(line.slice(5).replace(/^ /u, ""));
461
- }
462
- }
463
- }
464
- pending += decoder.decode();
465
- if (pending.startsWith("data:"))
466
- dataLines.push(pending.slice(5).replace(/^ /u, ""));
467
- const event = decode();
468
- if (event !== undefined)
469
- yield event;
470
- }
471
- finally {
472
- await reader.cancel().catch(() => undefined);
473
- }
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
+ });
474
401
  }
475
402
  function isSupportedOutputItem(value) {
476
- if (!isObject(value))
403
+ if (!isRecord(value))
477
404
  return false;
478
405
  if (value.type === "message")
479
406
  return (typeof value.id === "string" &&
@@ -482,7 +409,7 @@ function isSupportedOutputItem(value) {
482
409
  value.status === "completed" ||
483
410
  value.status === "incomplete") &&
484
411
  Array.isArray(value.content) &&
485
- value.content.every((part) => isObject(part) &&
412
+ value.content.every((part) => isRecord(part) &&
486
413
  ((part.type === "output_text" &&
487
414
  typeof part.text === "string" &&
488
415
  Array.isArray(part.annotations)) ||
@@ -490,7 +417,7 @@ function isSupportedOutputItem(value) {
490
417
  if (value.type === "reasoning")
491
418
  return (typeof value.id === "string" &&
492
419
  Array.isArray(value.summary) &&
493
- value.summary.every((part) => isObject(part) && typeof part.text === "string"));
420
+ value.summary.every((part) => isRecord(part) && typeof part.text === "string"));
494
421
  if (value.type === "function_call")
495
422
  return (typeof value.id === "string" &&
496
423
  typeof value.call_id === "string" &&
@@ -503,7 +430,7 @@ function isSupportedOutputItem(value) {
503
430
  typeof value.input === "string");
504
431
  }
505
432
  function isTerminalResponse(value) {
506
- if (!isObject(value) || typeof value.id !== "string")
433
+ if (!isRecord(value) || typeof value.id !== "string")
507
434
  return false;
508
435
  if (!Array.isArray(value.output))
509
436
  return false;
@@ -538,14 +465,14 @@ const PI_RESPONSE_EVENT_TYPES = new Set([
538
465
  * ignoring unrelated Responses events.
539
466
  */
540
467
  function isSupportedResponseStreamEvent(value) {
541
- if (!isObject(value))
468
+ if (!isRecord(value))
542
469
  return false;
543
470
  if (!Number.isSafeInteger(value.sequence_number) ||
544
471
  typeof value.type !== "string" ||
545
472
  !PI_RESPONSE_EVENT_TYPES.has(value.type))
546
473
  return false;
547
474
  if (value.type === "response.created")
548
- return isObject(value.response) && typeof value.response.id === "string";
475
+ return isRecord(value.response) && typeof value.response.id === "string";
549
476
  if (value.type === "response.output_item.added" ||
550
477
  value.type === "response.output_item.done")
551
478
  return Number.isSafeInteger(value.output_index) && isSupportedOutputItem(value.item);
@@ -585,13 +512,13 @@ function isSupportedResponseStreamEvent(value) {
585
512
  value.type === "response.incomplete")
586
513
  return isTerminalResponse(value.response);
587
514
  if (value.type === "response.failed")
588
- return isObject(value.response);
515
+ return isRecord(value.response);
589
516
  return typeof value.code === "string" && typeof value.message === "string";
590
517
  }
591
518
  async function* validatedResponseEvents(source) {
592
519
  let sequenceNumber = 0;
593
520
  for await (const raw of source) {
594
- if (!isObject(raw))
521
+ if (!isRecord(raw))
595
522
  continue;
596
523
  if (!PI_RESPONSE_EVENT_TYPES.has(String(raw.type ?? "")))
597
524
  continue;
@@ -601,7 +528,6 @@ async function* validatedResponseEvents(source) {
601
528
  yield event;
602
529
  }
603
530
  }
604
- /** @param {UnknownRecord} item */
605
531
  function itemKind(item) {
606
532
  if (item.type === "message")
607
533
  return "text";
@@ -611,7 +537,6 @@ function itemKind(item) {
611
537
  return "tool-call";
612
538
  return undefined;
613
539
  }
614
- /** @param {UnknownRecord} item */
615
540
  function itemIdentity(item) {
616
541
  const id = typeof item.id === "string" && item.id
617
542
  ? item.id
@@ -620,11 +545,10 @@ function itemIdentity(item) {
620
545
  : "";
621
546
  return id ? `${String(item.type)}:${id}` : undefined;
622
547
  }
623
- /** @param {UnknownRecord} item */
624
548
  function itemText(item) {
625
549
  if (item.type === "message")
626
550
  return (Array.isArray(item.content) ? item.content : [])
627
- .map((part) => isObject(part) &&
551
+ .map((part) => isRecord(part) &&
628
552
  (part.type === "output_text" || part.type === "refusal")
629
553
  ? String(part.text ?? part.refusal ?? "")
630
554
  : "")
@@ -636,7 +560,7 @@ function itemText(item) {
636
560
  ? item.content
637
561
  : [];
638
562
  return parts
639
- .map((part) => isObject(part) ? String(part.text ?? "") : "")
563
+ .map((part) => isRecord(part) ? String(part.text ?? "") : "")
640
564
  .join("\n\n");
641
565
  }
642
566
  if (item.type === "function_call")
@@ -648,7 +572,7 @@ function itemText(item) {
648
572
  function itemAnnotations(item) {
649
573
  if (item.type !== "message" || !Array.isArray(item.content))
650
574
  return [];
651
- 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 : []);
652
576
  }
653
577
  function sanitizeGrokTerminalItem(item, index) {
654
578
  const normalized = normalizedTerminalItem(item, index);
@@ -657,7 +581,7 @@ function sanitizeGrokTerminalItem(item, index) {
657
581
  return {
658
582
  ...normalized,
659
583
  content: normalized.content.map((part) => {
660
- if (!isObject(part) || (part.type !== "output_text" && part.type !== "refusal"))
584
+ if (!isRecord(part) || (part.type !== "output_text" && part.type !== "refusal"))
661
585
  return structuredClone(part);
662
586
  const text = sanitizeGrokVisibleText(String(part.text ?? part.refusal ?? ""), Array.isArray(part.annotations) ? part.annotations : []);
663
587
  return part.type === "output_text"
@@ -666,7 +590,6 @@ function sanitizeGrokTerminalItem(item, index) {
666
590
  }),
667
591
  };
668
592
  }
669
- /** @param {UnknownRecord} item @param {number} index */
670
593
  function normalizedTerminalItem(item, index) {
671
594
  if (item.type === "message")
672
595
  return {
@@ -704,7 +627,6 @@ function normalizedTerminalItem(item, index) {
704
627
  };
705
628
  return structuredClone(item);
706
629
  }
707
- /** @param {UnknownRecord} item @param {number} index */
708
630
  function addedShell(item, index) {
709
631
  if (item.type === "message")
710
632
  return {
@@ -732,11 +654,10 @@ function addedShell(item, index) {
732
654
  input: "",
733
655
  };
734
656
  }
735
- /** @param {UnknownRecord} record @param {UnknownRecord} item */
736
657
  function recordMatches(record, item) {
737
658
  if (record.kind !== itemKind(item))
738
659
  return false;
739
- const recordItem = /** @type {UnknownRecord} */ record.item;
660
+ const recordItem = record.item;
740
661
  if (itemIdentity(recordItem) &&
741
662
  itemIdentity(recordItem) === itemIdentity(item))
742
663
  return true;
@@ -751,8 +672,6 @@ function recordMatches(record, item) {
751
672
  /**
752
673
  * Some compatible gateways omit `response.output_item.added/done`, or terminal output indexes drift
753
674
  * when a reasoning item is inserted. Normalize only the missing framing; Pi remains authoritative for item semantics.
754
- * @param {AsyncIterable<unknown>} source
755
- * @param {{ responseModel?: string }} [meta]
756
675
  */
757
676
  async function* normalizedResponseEvents(source, meta = {}, options = {}) {
758
677
  const open = new Map();
@@ -804,7 +723,7 @@ async function* normalizedResponseEvents(source, meta = {}, options = {}) {
804
723
  const hiddenServerToolIndexes = new Set();
805
724
  const serverToolIds = new Map();
806
725
  const nativeServerSearchEchoCallIds = new Set();
807
- 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 ||
808
727
  options.isServerToolItem?.(value) === true);
809
728
  const observeServerTool = (item) => {
810
729
  meta.inputTokenScope = "aggregate";
@@ -818,7 +737,6 @@ async function* normalizedResponseEvents(source, meta = {}, options = {}) {
818
737
  }
819
738
  ids.add(typeof item.id === "string" && item.id ? item.id : `${type}:${ids.size}`);
820
739
  };
821
- /** @param {number} index @param {UnknownRecord} item */
822
740
  const ensure = function* (index, item) {
823
741
  let record = open.get(index);
824
742
  if (record)
@@ -834,19 +752,19 @@ async function* normalizedResponseEvents(source, meta = {}, options = {}) {
834
752
  return record;
835
753
  };
836
754
  for await (const raw of source) {
837
- if (!isObject(raw))
755
+ if (!isRecord(raw))
838
756
  continue;
839
- const event = /** @type {UnknownRecord} */ raw;
757
+ const event = raw;
840
758
  observeUpstreamFailure(event, meta);
841
759
  if (options.serverToolTypes && ['response.completed', 'response.failed', 'response.incomplete'].includes(String(event.type))) {
842
- meta.contextDetails = sanitizeContextDetails(isObject(event.response) && isObject(event.response.usage)
760
+ meta.contextDetails = sanitizeContextDetails(isRecord(event.response) && isRecord(event.response.usage)
843
761
  ? event.response.usage.context_details : undefined);
844
762
  }
845
763
  // Scope is response-local evidence, including failed/incomplete terminal samples.
846
- if (options.serverToolTypes && isObject(event.response)) {
764
+ if (options.serverToolTypes && isRecord(event.response)) {
847
765
  const response = event.response;
848
- const usage = isObject(response.usage) ? response.usage : {};
849
- 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 : {};
850
768
  if (Number(usage.num_server_side_tools_used ?? 0) > 0 ||
851
769
  Number(details.web_search_calls ?? 0) > 0 || Number(details.x_search_calls ?? 0) > 0)
852
770
  meta.inputTokenScope = "aggregate";
@@ -857,7 +775,7 @@ async function* normalizedResponseEvents(source, meta = {}, options = {}) {
857
775
  const index = Number.isInteger(event.output_index)
858
776
  ? Number(event.output_index)
859
777
  : 0;
860
- if (event.type === "response.output_item.done" && isObject(event.item))
778
+ if (event.type === "response.output_item.done" && isRecord(event.item))
861
779
  completedWireItems.set(index, structuredClone(event.item));
862
780
  if ((event.type === "response.output_item.added" ||
863
781
  event.type === "response.output_item.done") &&
@@ -879,10 +797,10 @@ async function* normalizedResponseEvents(source, meta = {}, options = {}) {
879
797
  // Keep them in completedWireItems/nativeOutput, but do not open empty UI blocks.
880
798
  if (options.serverToolTypes &&
881
799
  (event.type === "response.output_item.added" || event.type === "response.output_item.done") &&
882
- isObject(event.item) && event.item.type === "reasoning" &&
800
+ isRecord(event.item) && event.item.type === "reasoning" &&
883
801
  !itemText(event.item).trim() && !open.get(index)?.text.trim())
884
802
  continue;
885
- if (event.type === "response.output_item.added" && isObject(event.item)) {
803
+ if (event.type === "response.output_item.added" && isRecord(event.item)) {
886
804
  const rawItem = normalizedTerminalItem(event.item, index);
887
805
  const item = options.serverToolTypes ? sanitizeGrokTerminalItem(rawItem, index) : rawItem;
888
806
  open.set(index, { kind: itemKind(item), item: rawItem, text: itemText(rawItem) });
@@ -990,7 +908,7 @@ async function* normalizedResponseEvents(source, meta = {}, options = {}) {
990
908
  yield event;
991
909
  continue;
992
910
  }
993
- if (event.type === "response.output_item.done" && isObject(event.item)) {
911
+ if (event.type === "response.output_item.done" && isRecord(event.item)) {
994
912
  const rawItem = normalizedTerminalItem(event.item, index);
995
913
  const item = options.serverToolTypes ? sanitizeGrokTerminalItem(rawItem, index) : rawItem;
996
914
  if (options.serverToolTypes && item.type === "message") {
@@ -1007,8 +925,8 @@ async function* normalizedResponseEvents(source, meta = {}, options = {}) {
1007
925
  }
1008
926
  if ((event.type === "response.completed" ||
1009
927
  event.type === "response.incomplete") &&
1010
- isObject(event.response)) {
1011
- const response = /** @type {UnknownRecord} */ event.response;
928
+ isRecord(event.response)) {
929
+ const response = event.response;
1012
930
  if (typeof response.model === "string" && response.model.length > 0)
1013
931
  meta.responseModel = response.model;
1014
932
  const terminalOutput = Array.isArray(response.output) && response.output.length > 0
@@ -1018,20 +936,20 @@ async function* normalizedResponseEvents(source, meta = {}, options = {}) {
1018
936
  .map(([, item]) => item);
1019
937
  const nativeOutput = terminalOutput.map((item) => structuredClone(item));
1020
938
  for (const item of terminalOutput) {
1021
- if (isServerToolItem(item) && isObject(item) && item.type === "custom_tool_call" &&
939
+ if (isServerToolItem(item) && isRecord(item) && item.type === "custom_tool_call" &&
1022
940
  item.status !== "completed")
1023
941
  throw Object.assign(new Error("Grok native custom search item did not complete on the server"), { code: "LCX_GROK_NATIVE_PROTOCOL_ERROR" });
1024
942
  if (isServerToolItem(item))
1025
943
  observeServerTool(item);
1026
- else if (options.serverToolTypes && isObject(item) && item.type === "custom_tool_call" &&
944
+ else if (options.serverToolTypes && isRecord(item) && item.type === "custom_tool_call" &&
1027
945
  typeof item.name === "string" && options.declaredToolNames?.has(item.name) !== true)
1028
946
  throw Object.assign(new Error(`Grok returned undeclared client custom tool "${item.name}"`), { code: "LCX_GROK_NATIVE_PROTOCOL_ERROR" });
1029
947
  }
1030
948
  const output = terminalOutput
1031
949
  .filter((item) => !isServerToolItem(item))
1032
- .filter((item) => !(options.serverToolTypes && isObject(item) &&
950
+ .filter((item) => !(options.serverToolTypes && isRecord(item) &&
1033
951
  item.type === "reasoning" && !itemText(item).trim()))
1034
- .map((item, terminalIndex) => isObject(item)
952
+ .map((item, terminalIndex) => isRecord(item)
1035
953
  ? (options.serverToolTypes
1036
954
  ? sanitizeGrokTerminalItem(item, terminalIndex)
1037
955
  : normalizedTerminalItem(item, terminalIndex))
@@ -1041,8 +959,8 @@ async function* normalizedResponseEvents(source, meta = {}, options = {}) {
1041
959
  options.serverToolTypes) {
1042
960
  meta.nativeOutput = nativeOutput;
1043
961
  meta.nativeServerSearchEchoCallIds = [...nativeServerSearchEchoCallIds];
1044
- const usage = isObject(response.usage) ? response.usage : {};
1045
- 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 : {};
1046
964
  meta.inputTokenScope = meta.inputTokenScope === "aggregate" || serverToolIds.size > 0 || nativeOutput.some(isServerToolItem)
1047
965
  || Number(usage.num_server_side_tools_used ?? 0) > 0
1048
966
  || Number(details.web_search_calls ?? 0) > 0 || Number(details.x_search_calls ?? 0) > 0
@@ -1051,12 +969,12 @@ async function* normalizedResponseEvents(source, meta = {}, options = {}) {
1051
969
  const used = new Set();
1052
970
  for (const [streamIndex, record] of open) {
1053
971
  let terminalIndex = output.findIndex((item, candidateIndex) => !used.has(candidateIndex) &&
1054
- isObject(item) &&
972
+ isRecord(item) &&
1055
973
  candidateIndex === streamIndex &&
1056
974
  recordMatches(record, item));
1057
975
  if (terminalIndex < 0)
1058
976
  terminalIndex = output.findIndex((item, candidateIndex) => !used.has(candidateIndex) &&
1059
- isObject(item) &&
977
+ isRecord(item) &&
1060
978
  recordMatches(record, item));
1061
979
  const item = terminalIndex >= 0
1062
980
  ? output[terminalIndex]
@@ -1080,7 +998,7 @@ async function* normalizedResponseEvents(source, meta = {}, options = {}) {
1080
998
  open.clear();
1081
999
  grokTextProjection.clear();
1082
1000
  for (const [terminalIndex, candidate] of output.entries()) {
1083
- if (!isObject(candidate) ||
1001
+ if (!isRecord(candidate) ||
1084
1002
  !itemKind(candidate) ||
1085
1003
  used.has(terminalIndex))
1086
1004
  continue;
@@ -1139,8 +1057,8 @@ async function* normalizedResponseEvents(source, meta = {}, options = {}) {
1139
1057
  completed.add(identity);
1140
1058
  }
1141
1059
  if (options.onServerToolUsage) {
1142
- const usage = isObject(response.usage) ? response.usage : {};
1143
- 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)
1144
1062
  ? usage.server_side_tool_usage_details
1145
1063
  : {};
1146
1064
  const safeCount = (value, fallback) => Number.isSafeInteger(value) && Number(value) >= 0
@@ -1158,9 +1076,8 @@ async function* normalizedResponseEvents(source, meta = {}, options = {}) {
1158
1076
  yield event;
1159
1077
  }
1160
1078
  }
1161
- /** @param {unknown} usage */
1162
1079
  function isManagedFailure(value) {
1163
- return (isObject(value) &&
1080
+ return (isRecord(value) &&
1164
1081
  typeof value.message === "string" &&
1165
1082
  typeof value.code === "string" &&
1166
1083
  (value.status === undefined || typeof value.status === "number") &&
@@ -1169,7 +1086,7 @@ function isManagedFailure(value) {
1169
1086
  typeof value.providerRetryAfterMs === "number"));
1170
1087
  }
1171
1088
  function dshUsage(usage) {
1172
- const value = /** @type {UnknownRecord} */ isObject(usage) ? usage : {};
1089
+ const value = isRecord(usage) ? usage : {};
1173
1090
  const inputTokens = Number(value.input ?? 0);
1174
1091
  const outputTokens = Number(value.output ?? 0);
1175
1092
  const cacheReadTokens = Number(value.cacheRead ?? 0);
@@ -1188,21 +1105,19 @@ function dshUsage(usage) {
1188
1105
  : {}),
1189
1106
  };
1190
1107
  }
1191
- /** @param {unknown} value */
1192
1108
  function rawArguments(value) {
1193
1109
  try {
1194
- return JSON.stringify(isObject(value) ? value : {});
1110
+ return JSON.stringify(isRecord(value) ? value : {});
1195
1111
  }
1196
1112
  catch {
1197
1113
  return "{}";
1198
1114
  }
1199
1115
  }
1200
- /** @param {unknown} message */
1201
1116
  function replayState(message, nativeOutput, nativeReplayRoute, nativeServerSearchEchoCallIds = []) {
1202
- if (!isObject(message))
1117
+ if (!isRecord(message))
1203
1118
  return undefined;
1204
1119
  const content = Array.isArray(message.content)
1205
- ? message.content.filter(isObject)
1120
+ ? message.content.filter(isRecord)
1206
1121
  : [];
1207
1122
  const provider = typeof message.provider === "string" ? message.provider : undefined;
1208
1123
  const model = typeof message.model === "string" ? message.model : undefined;
@@ -1256,12 +1171,11 @@ function replayState(message, nativeOutput, nativeReplayRoute, nativeServerSearc
1256
1171
  ...(grokNative === undefined ? {} : { grokNative }),
1257
1172
  };
1258
1173
  }
1259
- /** @param {unknown} message */
1260
1174
  function successfulFinish(message) {
1261
- const value = /** @type {UnknownRecord} */ isObject(message) ? message : {};
1175
+ const value = isRecord(message) ? message : {};
1262
1176
  const stopReason = String(value.stopReason ?? "stop");
1263
- const content = /** @type {UnknownRecord[]} */ Array.isArray(value.content)
1264
- ? value.content.filter(isObject)
1177
+ const content = Array.isArray(value.content)
1178
+ ? value.content.filter(isRecord)
1265
1179
  : [];
1266
1180
  if (stopReason === "length")
1267
1181
  return { kind: "max-tokens" };
@@ -1280,8 +1194,6 @@ function successfulFinish(message) {
1280
1194
  }
1281
1195
  /**
1282
1196
  * Pi event vocabulary -> DSH StreamChunk. This remains thin and provider-neutral.
1283
- * @param {AsyncIterable<AssistantMessageEvent>} events
1284
- * @param {AbortSignal} [signal]
1285
1197
  */
1286
1198
  async function* toDshChunks(events, signal, wireMeta, nativeReplayRoute) {
1287
1199
  const toolIds = new Map();
@@ -1332,12 +1244,12 @@ async function* toDshChunks(events, signal, wireMeta, nativeReplayRoute) {
1332
1244
  };
1333
1245
  break;
1334
1246
  case "toolcall_start": {
1335
- const partial = /** @type {UnknownRecord | undefined} */ isObject(event.partial)
1247
+ const partial = isRecord(event.partial)
1336
1248
  ? event.partial
1337
1249
  : undefined;
1338
1250
  const content = Array.isArray(partial?.content) ? partial.content : [];
1339
1251
  const item = content[Number(event.contentIndex)];
1340
- const block = isObject(item) ? item : undefined;
1252
+ const block = isRecord(item) ? item : undefined;
1341
1253
  toolIds.set(Number(event.contentIndex), {
1342
1254
  id: String(block?.id ?? ""),
1343
1255
  name: String(block?.name ?? ""),
@@ -1364,7 +1276,7 @@ async function* toDshChunks(events, signal, wireMeta, nativeReplayRoute) {
1364
1276
  break;
1365
1277
  }
1366
1278
  case "toolcall_end": {
1367
- const call = isObject(event.toolCall)
1279
+ const call = isRecord(event.toolCall)
1368
1280
  ? event.toolCall
1369
1281
  : {};
1370
1282
  yield {
@@ -1380,7 +1292,7 @@ async function* toDshChunks(events, signal, wireMeta, nativeReplayRoute) {
1380
1292
  break;
1381
1293
  }
1382
1294
  case "done": {
1383
- const message = isObject(event.message)
1295
+ const message = isRecord(event.message)
1384
1296
  ? event.message
1385
1297
  : {};
1386
1298
  yield { type: "usage", usage: dshUsage(message.usage) };
@@ -1399,7 +1311,7 @@ async function* toDshChunks(events, signal, wireMeta, nativeReplayRoute) {
1399
1311
  return;
1400
1312
  }
1401
1313
  case "error": {
1402
- const message = isObject(event.error)
1314
+ const message = isRecord(event.error)
1403
1315
  ? event.error
1404
1316
  : {};
1405
1317
  yield { type: "usage", usage: dshUsage(message.usage) };
@@ -1436,20 +1348,6 @@ async function* toDshChunks(events, signal, wireMeta, nativeReplayRoute) {
1436
1348
  /**
1437
1349
  * Send one LCX-owned OpenAI Responses request. Ordinary and replay use one provider attempt;
1438
1350
  * the DSH agent recovery layer remains the visible retry owner.
1439
- * @param {object} options
1440
- * @param {string} options.baseURL
1441
- * @param {string} options.provider
1442
- * @param {string} options.model
1443
- * @param {UnknownRecord} options.piModel
1444
- * @param {UnknownRecord} options.body
1445
- * @param {Map<string, string>} [options.grammarToolInputProperties]
1446
- * @param {Record<string, string>} [options.headers]
1447
- * @param {AbortSignal} [options.signal]
1448
- * @param {number} [options.timeoutMs]
1449
- * @param {number} [options.maxAttempts]
1450
- * @param {number} [options.maxResponseBytes]
1451
- * @param {ReadonlySet<string>} [options.serverToolTypes]
1452
- * @param {(usage: ServerToolUsage) => void} [options.onServerToolUsage]
1453
1351
  */
1454
1352
  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, }) {
1455
1353
  let contentEmitted = false;
@@ -1478,7 +1376,7 @@ export async function* streamResponsesRequest({ baseURL, provider, model, piMode
1478
1376
  // Diagnostics must never change the stream outcome.
1479
1377
  }
1480
1378
  };
1481
- const effectiveTimeoutMs = timeoutMs ?? (applyDefaultTimeout ? 300_000 : undefined);
1379
+ const effectiveTimeoutMs = timeoutMs ?? (applyDefaultTimeout ? DEFAULTS.requestTimeoutMs : undefined);
1482
1380
  const deadline = requestDeadline(signal, effectiveTimeoutMs);
1483
1381
  const watchdog = streamIdleWatchdog(deadline.signal, streamIdleTimeoutMs);
1484
1382
  try {
@@ -1528,7 +1426,7 @@ export async function* streamResponsesRequest({ baseURL, provider, model, piMode
1528
1426
  output.__lcxFailure = managedFailure(error, signal, wireMeta.upstreamFailure);
1529
1427
  piEvents.push({
1530
1428
  type: "error",
1531
- reason: /** @type {'aborted' | 'error'} */ output.stopReason,
1429
+ reason: output.stopReason,
1532
1430
  error: output,
1533
1431
  });
1534
1432
  piEvents.end();