dsh-lcx-codex 0.7.0-pre.1 → 0.7.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,12 +1,15 @@
1
+ import { createHash } from "node:crypto";
1
2
  import { IMAGE_OFFLOAD_REQUIRED_CODE, LlmError, offloadedImageText, projectOffloadedImages, requiredImageOffload, requestImageHandleText, resolveImageAttachmentAccess, } from "@deepseek-ai/dsh-llm";
2
3
  import { requestImageDimensions } from "@deepseek-ai/dsh-attachment";
3
4
  import { convertResponsesMessages, convertResponsesTools, createGrammarToolInputProperties, normalizeContext, resolveTranscript, resolveTranscriptTools, getResponsesToolSupport, } from "./pi-responses-runtime.js";
4
5
  import { resolveCatalogModel } from "./model-catalog.js";
6
+ import { baseURLFingerprint } from "./route.js";
5
7
  import { withoutDeferLoading } from "./tool-surface.js";
6
8
  import { isRecord, codedError, positiveSafeInteger } from "./util.js";
7
9
  const DEFAULT_MAX_REQUEST_IMAGE_BYTES = 20 * 1024 * 1024;
8
10
  const DEFAULT_REQUEST_IMAGE_PIXEL_BUDGET = 2048 * 2048;
9
11
  const DEFAULT_REQUEST_IMAGE_MAX_BYTES = 1024 * 1024;
12
+ const ALLOWED_TOOL_CALL_PROVIDERS = new Set(["openai", "openai-codex", "opencode"]);
10
13
  export { DEFAULT_MAX_REQUEST_IMAGE_BYTES, DEFAULT_REQUEST_IMAGE_PIXEL_BUDGET, DEFAULT_REQUEST_IMAGE_MAX_BYTES, };
11
14
  function isResponseInputItem(value) {
12
15
  if (!isRecord(value))
@@ -69,7 +72,7 @@ function imageAccess(ctx, attachments, ref) {
69
72
  export async function resolveRouteModelFacts(ctx, route, signal) {
70
73
  const llm = ctx?.llm;
71
74
  if (llm === undefined)
72
- return { imageSupport: "unknown", systemPromptUpdate: undefined, toolUpdate: undefined, reasoning: undefined, contextWindow: undefined, resolved: false };
75
+ return { imageSupport: "unknown", systemPromptUpdate: undefined, toolUpdate: undefined, reasoning: undefined, contextWindow: undefined, defaultMaxTokens: undefined, resolved: false };
73
76
  try {
74
77
  const info = await llm.resolveModelInfo(stringValue(route.provider), stringValue(route.model), signal);
75
78
  const modalities = info.inputModalities;
@@ -83,15 +86,24 @@ export async function resolveRouteModelFacts(ctx, route, signal) {
83
86
  toolUpdate: info.toolUpdate === "addition-only" || info.toolUpdate === "in-history" ? info.toolUpdate : undefined,
84
87
  reasoning: info.reasoning === undefined
85
88
  ? undefined
86
- : { efforts: info.reasoning.efforts.map((effort) => String(effort.id)) },
89
+ : (() => {
90
+ const rawDefaultEffort = info.reasoning.defaultEffort;
91
+ return {
92
+ efforts: info.reasoning.efforts.map((effort) => String(effort.id)),
93
+ ...(rawDefaultEffort !== undefined && rawDefaultEffort !== null
94
+ ? { defaultEffort: String(rawDefaultEffort) }
95
+ : {}),
96
+ };
97
+ })(),
87
98
  contextWindow: positiveSafeInteger(info.context?.contextWindow, 0) || undefined,
99
+ defaultMaxTokens: positiveSafeInteger(info.defaultMaxTokens, 0) || undefined,
88
100
  resolved: true,
89
101
  };
90
102
  }
91
103
  catch {
92
104
  // `resolved: false` tells callers the mode is unknown rather than known-absent, so they can refuse
93
105
  // to downgrade history that only an in-history route may legitimately contain.
94
- return { imageSupport: "unknown", systemPromptUpdate: undefined, toolUpdate: undefined, reasoning: undefined, contextWindow: undefined, resolved: false };
106
+ return { imageSupport: "unknown", systemPromptUpdate: undefined, toolUpdate: undefined, reasoning: undefined, contextWindow: undefined, defaultMaxTokens: undefined, resolved: false };
95
107
  }
96
108
  }
97
109
  /**
@@ -104,11 +116,20 @@ export async function resolveRouteModelFacts(ctx, route, signal) {
104
116
  */
105
117
  export function routeModelOverride(routeConfig, facts, grok) {
106
118
  const contextWindow = facts.resolved && facts.contextWindow !== undefined ? { contextWindow: facts.contextWindow } : {};
107
- if (grok)
108
- return Object.keys(contextWindow).length > 0 ? contextWindow : undefined;
109
- if (!facts.resolved)
110
- return routeConfig.modelControls === undefined ? undefined : { ...routeConfig.modelControls };
111
- return { reasoning: facts.reasoning !== undefined, ...contextWindow, ...routeConfig.modelControls };
119
+ const resolvedMaxTokens = facts.resolved
120
+ ? positiveSafeInteger(facts.defaultMaxTokens, 0) || undefined
121
+ : positiveSafeInteger(routeConfig.profileModelMaxTokens, 0) || undefined;
122
+ const maxTokens = resolvedMaxTokens === undefined ? {} : { maxTokens: resolvedMaxTokens };
123
+ if (grok) {
124
+ const grokOverride = { ...contextWindow, ...maxTokens };
125
+ return Object.keys(grokOverride).length > 0 ? grokOverride : undefined;
126
+ }
127
+ if (!facts.resolved) {
128
+ if (routeConfig.modelControls === undefined && resolvedMaxTokens === undefined)
129
+ return undefined;
130
+ return { ...maxTokens, ...(routeConfig.modelControls ?? {}) };
131
+ }
132
+ return { reasoning: facts.reasoning !== undefined, ...contextWindow, ...maxTokens, ...routeConfig.modelControls };
112
133
  }
113
134
  function attachmentResolver(ctx, options, requestImages) {
114
135
  const attachments = attachmentServices(ctx?.attachments);
@@ -215,7 +236,7 @@ function restorePortableAssistantPhases(input, messages, target) {
215
236
  for (const block of message.content) {
216
237
  if (block.type !== "text")
217
238
  continue;
218
- const phase = sameProviderAndApi && !sameModel
239
+ const phase = !sameModel
219
240
  ? replayTextPhase(block.textSignature)
220
241
  : undefined;
221
242
  slots.push({ text: block.text, ...(phase === undefined ? {} : { phase }) });
@@ -285,7 +306,139 @@ function foreignAssistant(message) {
285
306
  }
286
307
  return { role: "assistant", content, api: "dsh-foreign", provider: source?.provider ?? "dsh-foreign", model: source?.model ?? "dsh-foreign", usage: emptyUsage(), stopReason: content.some((block) => block.type === "toolCall") ? "toolUse" : "stop", timestamp: 0 };
287
308
  }
288
- function replayedAssistant(message, source) {
309
+ function defaultAssistantReplayScope(route, explicitModel) {
310
+ const routeObj = isRecord(route) ? route : undefined;
311
+ const modelObj = isRecord(explicitModel) ? explicitModel : undefined;
312
+ const provider = typeof modelObj?.provider === "string" && modelObj.provider.length > 0
313
+ ? modelObj.provider
314
+ : typeof routeObj?.provider === "string" && routeObj.provider.length > 0
315
+ ? routeObj.provider
316
+ : undefined;
317
+ const model = typeof modelObj?.id === "string" && modelObj.id.length > 0
318
+ ? modelObj.id
319
+ : typeof routeObj?.model === "string" && routeObj.model.length > 0
320
+ ? routeObj.model
321
+ : undefined;
322
+ const sessionId = typeof routeObj?.sessionId === "string" && routeObj.sessionId.length > 0
323
+ ? routeObj.sessionId
324
+ : undefined;
325
+ const rawBaseUrl = typeof routeObj?.baseURL === "string" && routeObj.baseURL.trim().length > 0
326
+ ? routeObj.baseURL.trim()
327
+ : undefined;
328
+ const headerAuthorityFingerprint = typeof routeObj?.headerAuthorityFingerprint === "string" &&
329
+ routeObj.headerAuthorityFingerprint.length > 0
330
+ ? routeObj.headerAuthorityFingerprint
331
+ : undefined;
332
+ if (provider === undefined &&
333
+ model === undefined &&
334
+ sessionId === undefined &&
335
+ rawBaseUrl === undefined &&
336
+ headerAuthorityFingerprint === undefined)
337
+ return undefined;
338
+ return {
339
+ ...(provider === undefined ? {} : { provider }),
340
+ ...(model === undefined ? {} : { model }),
341
+ ...(sessionId === undefined ? {} : { sessionId }),
342
+ ...(rawBaseUrl === undefined
343
+ ? {}
344
+ : { baseURLFingerprint: baseURLFingerprint(rawBaseUrl) }),
345
+ ...(headerAuthorityFingerprint === undefined
346
+ ? {}
347
+ : { headerAuthorityFingerprint }),
348
+ };
349
+ }
350
+ function isNativeAssistantReplayCompatible(message, rawReplayState, scope) {
351
+ if (scope?.disallowAllNativeReplay === true)
352
+ return false;
353
+ const source = message.source.kind === "model" ? message.source : undefined;
354
+ if (scope?.provider !== undefined && source?.provider !== scope.provider)
355
+ return false;
356
+ if (scope?.model !== undefined && source?.model !== scope.model)
357
+ return false;
358
+ const msgId = isRecord(message) && typeof message.id === "string"
359
+ ? String(message.id)
360
+ : undefined;
361
+ if (msgId !== undefined && scope?.inheritedMessageIds?.has(msgId) === true)
362
+ return false;
363
+ if (!isRecord(rawReplayState))
364
+ return false;
365
+ const lcxReplay = rawReplayState.lcxReplay;
366
+ if (!isRecord(lcxReplay) ||
367
+ lcxReplay.version !== 1 ||
368
+ typeof lcxReplay.baseURLFingerprint !== "string" ||
369
+ lcxReplay.baseURLFingerprint.length === 0)
370
+ return false;
371
+ if (scope?.baseURLFingerprint !== undefined &&
372
+ lcxReplay.baseURLFingerprint !== scope.baseURLFingerprint)
373
+ return false;
374
+ if (typeof lcxReplay.sourceSessionId !== "string" ||
375
+ lcxReplay.sourceSessionId.length === 0 ||
376
+ typeof scope?.sessionId !== "string" ||
377
+ scope.sessionId.length === 0 ||
378
+ lcxReplay.sourceSessionId !== scope.sessionId)
379
+ return false;
380
+ if (typeof lcxReplay.headerAuthorityFingerprint !== "string" ||
381
+ lcxReplay.headerAuthorityFingerprint.length === 0 ||
382
+ typeof scope?.headerAuthorityFingerprint !== "string" ||
383
+ scope.headerAuthorityFingerprint.length === 0 ||
384
+ lcxReplay.headerAuthorityFingerprint !== scope.headerAuthorityFingerprint)
385
+ return false;
386
+ if (isRecord(rawReplayState.grokNative) ||
387
+ lcxReplay.grokRouteAuthorityFingerprint !== undefined) {
388
+ if (typeof lcxReplay.grokRouteAuthorityFingerprint !== "string" ||
389
+ lcxReplay.grokRouteAuthorityFingerprint.length === 0 ||
390
+ lcxReplay.grokRouteAuthorityFingerprint !== scope?.grokRouteAuthorityFingerprint)
391
+ return false;
392
+ }
393
+ return true;
394
+ }
395
+ function portableAssistantFromReplay(message, source, state) {
396
+ const content = [];
397
+ for (const [index, block] of message.content.entries()) {
398
+ const replay = state.blocks[index];
399
+ if (block.type === "text") {
400
+ const phase = replayTextPhase(replay?.textSignature);
401
+ content.push({
402
+ type: "text",
403
+ text: block.text,
404
+ ...(phase === undefined
405
+ ? {}
406
+ : { textSignature: JSON.stringify({ v: 1, id: "", phase }) }),
407
+ });
408
+ }
409
+ else if (block.type === "reasoning") {
410
+ content.push({ type: "thinking", thinking: block.text });
411
+ }
412
+ else if (block.type === "tool-call") {
413
+ content.push({
414
+ type: "toolCall",
415
+ id: String(block.id),
416
+ name: block.name,
417
+ arguments: parseArguments(block.arguments),
418
+ ...(typeof replay?.namespace === "string" && replay.namespace.length > 0
419
+ ? { namespace: replay.namespace }
420
+ : {}),
421
+ });
422
+ }
423
+ else
424
+ throw unsupportedContent(block.type);
425
+ }
426
+ const preserveSameResponsesApi = state.response.api === "openai-responses";
427
+ return {
428
+ role: "assistant",
429
+ content,
430
+ api: preserveSameResponsesApi ? state.response.api : "dsh-foreign",
431
+ provider: source.provider,
432
+ model: source.model,
433
+ ...(preserveSameResponsesApi && typeof state.response.providerThinkingLevel === "string"
434
+ ? { providerThinkingLevel: state.response.providerThinkingLevel }
435
+ : {}),
436
+ usage: emptyUsage(),
437
+ stopReason: state.response.stopReason,
438
+ timestamp: 0,
439
+ };
440
+ }
441
+ function validateAndBuildReplayedAssistant(message, source) {
289
442
  if (source.replayState === undefined)
290
443
  throw invalidReplay("assistant source has no replay state");
291
444
  const state = readDshPiReplayState(source.replayState);
@@ -307,21 +460,42 @@ function replayedAssistant(message, source) {
307
460
  return { type: "toolCall", id: String(block.id), name: block.name, arguments: parseArguments(block.arguments), ...(typeof replay.thoughtSignature === "string" ? { thoughtSignature: replay.thoughtSignature } : {}), ...(typeof replay.namespace === "string" ? { namespace: replay.namespace } : {}) };
308
461
  throw invalidReplay(`block ${index} has an unsupported type`);
309
462
  });
310
- return { role: "assistant", content, api: state.response.api, provider: state.response.provider, model: state.response.model, ...(typeof state.response.responseModel === "string" ? { responseModel: state.response.responseModel } : {}), ...(typeof state.response.responseId === "string" ? { responseId: state.response.responseId } : {}), ...(typeof state.response.providerThinkingLevel === "string" ? { providerThinkingLevel: state.response.providerThinkingLevel } : {}), usage: emptyUsage(), stopReason: state.response.stopReason, timestamp: 0 };
463
+ return {
464
+ state,
465
+ assistant: { role: "assistant", content, api: state.response.api, provider: state.response.provider, model: state.response.model, ...(typeof state.response.responseModel === "string" ? { responseModel: state.response.responseModel } : {}), ...(typeof state.response.responseId === "string" ? { responseId: state.response.responseId } : {}), ...(typeof state.response.providerThinkingLevel === "string" ? { providerThinkingLevel: state.response.providerThinkingLevel } : {}), usage: emptyUsage(), stopReason: state.response.stopReason, timestamp: 0 },
466
+ };
467
+ }
468
+ function sanitizeNonNativeToolCallId(rawId, keepCompoundItemId) {
469
+ const sep = rawId.indexOf("|");
470
+ if (sep < 0)
471
+ return rawId;
472
+ const callId = rawId.slice(0, sep);
473
+ const itemIdRaw = rawId.slice(sep + 1);
474
+ if (!keepCompoundItemId || itemIdRaw.startsWith("ctc_") || itemIdRaw.length === 0)
475
+ return callId;
476
+ const digest = createHash("sha256").update(itemIdRaw, "utf8").digest("hex").slice(0, 16);
477
+ return `${callId}|fc_${digest}`;
311
478
  }
312
- function toPiAssistant(message, onReplayDegrade) {
479
+ function toPiAssistant(message, onReplayDegrade, replayScope) {
313
480
  const source = message.source;
314
481
  if (source.kind !== "model" || source.replayState === undefined)
315
- return foreignAssistant(message);
482
+ return { assistant: foreignAssistant(message), nativeCompatible: false };
316
483
  try {
317
- return replayedAssistant(message, source);
484
+ const validated = validateAndBuildReplayedAssistant(message, source);
485
+ if (!isNativeAssistantReplayCompatible(message, source.replayState, replayScope)) {
486
+ return {
487
+ assistant: portableAssistantFromReplay(message, source, validated.state),
488
+ nativeCompatible: false,
489
+ };
490
+ }
491
+ return { assistant: validated.assistant, nativeCompatible: true };
318
492
  }
319
493
  catch (cause) {
320
494
  if (errorCode(cause) !== "LCX_COMPACT_INVALID_REPLAY_STATE")
321
495
  throw cause;
322
496
  if (typeof onReplayDegrade === "function")
323
497
  onReplayDegrade(errorMessage(cause));
324
- return foreignAssistant(message);
498
+ return { assistant: foreignAssistant(message), nativeCompatible: false };
325
499
  }
326
500
  }
327
501
  async function piToolContent(blocks, ctx, options, imageMap, requestImages) {
@@ -412,9 +586,10 @@ function flattenMessageText(message) {
412
586
  return block.text;
413
587
  }).join("");
414
588
  }
415
- async function dshToPiMessages(messages, ctx, options, imageMap, requestImages) {
589
+ async function dshToPiMessages(messages, ctx, options, imageMap, requestImages, targetModel) {
416
590
  const result = [];
417
591
  const toolNames = new Map();
592
+ const rewrittenToolCallIds = new Map();
418
593
  for (const message of messages) {
419
594
  if (message.role === "system") {
420
595
  if (options.inHistorySystem === true) {
@@ -459,15 +634,29 @@ async function dshToPiMessages(messages, ctx, options, imageMap, requestImages)
459
634
  continue;
460
635
  }
461
636
  if (message.role === "assistant") {
462
- const assistant = toPiAssistant(message, options.onReplayDegrade);
463
- for (const block of assistant.content)
464
- if (block.type === "toolCall")
637
+ const { assistant, nativeCompatible } = toPiAssistant(message, options.onReplayDegrade, options.assistantReplayScope);
638
+ const sameTargetModel = assistant.provider === targetModel.provider &&
639
+ assistant.api === targetModel.api &&
640
+ assistant.model === targetModel.id;
641
+ for (const block of assistant.content) {
642
+ if (block.type === "toolCall") {
643
+ if (!nativeCompatible || !sameTargetModel) {
644
+ const rawId = String(block.id);
645
+ const sanitizedId = sanitizeNonNativeToolCallId(rawId, sameTargetModel || ALLOWED_TOOL_CALL_PROVIDERS.has(targetModel.provider));
646
+ if (sanitizedId !== rawId) {
647
+ rewrittenToolCallIds.set(rawId, sanitizedId);
648
+ block.id = sanitizedId;
649
+ }
650
+ }
465
651
  toolNames.set(String(block.id), block.name);
652
+ }
653
+ }
466
654
  result.push(assistant);
467
655
  continue;
468
656
  }
469
657
  if (message.role === "tool") {
470
- const toolCallId = String(message.toolCallId);
658
+ const rawToolCallId = String(message.toolCallId);
659
+ const toolCallId = rewrittenToolCallIds.get(rawToolCallId) ?? rawToolCallId;
471
660
  result.push({ role: "toolResult", toolCallId, toolName: toolNames.get(toolCallId) ?? "unknown", content: await piToolContent(message.content, ctx, options, imageMap, requestImages), isError: message.isError === true, timestamp: 0 });
472
661
  continue;
473
662
  }
@@ -503,12 +692,17 @@ export function resolvePiResponsesModel(options) {
503
692
  const explicit = isRecord(options.model) ? options.model : {};
504
693
  const provider = stringValue(explicit.provider, stringValue(route.provider, "dsh-lcx-codex"));
505
694
  const id = stringValue(explicit.id, stringValue(route.model, "unknown"));
506
- const builtin = builtinResponsesModel(provider, id);
695
+ const catalog = resolveCatalogModel(provider, id);
696
+ const builtin = catalog.descriptor ?? builtinResponsesModel(provider, id);
507
697
  const builtinRecord = isRecord(builtin) ? builtin : {};
698
+ const catalogMaxTokens = positiveSafeInteger(catalog.maxTokens, 0) ||
699
+ positiveSafeInteger(route.catalogMaxTokens, 0) ||
700
+ positiveSafeInteger(builtinRecord.maxTokens, 0) ||
701
+ undefined;
508
702
  const compat = { ...(isRecord(builtinRecord.compat) ? builtinRecord.compat : {}), ...(isRecord(options.responsesCompat) ? options.responsesCompat : {}), ...(isRecord(explicit.compat) ? explicit.compat : {}) };
509
703
  const imageSupport = options.imageSupport === "supported" || options.imageSupport === "unsupported" ? options.imageSupport : "unknown";
510
704
  const input = imageSupport === "supported" ? ["text", "image"] : imageSupport === "unsupported" ? ["text"] : Array.isArray(explicit.input) ? explicit.input : Array.isArray(builtinRecord.input) ? builtinRecord.input : ["text"];
511
- return { ...builtinRecord, ...explicit, id, name: stringValue(explicit.name, stringValue(builtinRecord.name, id)), api: "openai-responses", provider, baseUrl: stringValue(explicit.baseUrl, stringValue(route.baseURL, stringValue(builtinRecord.baseUrl))), reasoning: typeof explicit.reasoning === "boolean" ? explicit.reasoning : typeof builtinRecord.reasoning === "boolean" ? builtinRecord.reasoning : true, input, cost: explicit.cost ?? builtinRecord.cost ?? { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 }, contextWindow: positiveSafeInteger(explicit.contextWindow ?? builtinRecord.contextWindow, 262144), maxTokens: positiveSafeInteger(explicit.maxTokens ?? builtinRecord.maxTokens, 32768), ...(Object.keys(compat).length > 0 ? { compat } : {}) };
705
+ return { ...builtinRecord, ...explicit, id, name: stringValue(explicit.name, stringValue(builtinRecord.name, id)), api: "openai-responses", provider, baseUrl: stringValue(explicit.baseUrl, stringValue(route.rawBaseURL, stringValue(route.baseURL, stringValue(builtinRecord.baseUrl)))), reasoning: typeof explicit.reasoning === "boolean" ? explicit.reasoning : typeof builtinRecord.reasoning === "boolean" ? builtinRecord.reasoning : true, input, cost: explicit.cost ?? builtinRecord.cost ?? { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 }, contextWindow: positiveSafeInteger(explicit.contextWindow ?? builtinRecord.contextWindow, 262144), maxTokens: positiveSafeInteger(explicit.maxTokens ?? catalogMaxTokens ?? route.profileDefaultMaxTokens, 32768), ...(Object.keys(compat).length > 0 ? { compat } : {}) };
512
706
  }
513
707
  export async function serializeDshMessages(messages, ctx, options = {}) {
514
708
  const anchored = options.toolUpdate === "addition-only";
@@ -532,12 +726,14 @@ export async function serializeDshMessages(messages, ctx, options = {}) {
532
726
  const immediateDeclared = (Array.isArray(options.tools) ? options.tools : [])
533
727
  .filter((tool) => !("deferLoading" in tool && tool.deferLoading === true))
534
728
  .map((tool) => withoutDeferLoading(tool));
729
+ const gatewayCompatibilityFallback = options.gatewayCompatibilityFallback === true;
535
730
  const normalized = {
536
731
  imageSupport: options.imageSupport === "supported" || options.imageSupport === "unsupported" ? options.imageSupport : "unknown",
537
732
  signal: options.signal instanceof AbortSignal ? options.signal : undefined,
538
733
  route: options.route ?? {}, model: options.model, responsesCompat: options.responsesCompat,
539
734
  systemPrompt: typeof options.systemPrompt === "string" ? options.systemPrompt : undefined,
540
- includeSystemPrompt: options.includeSystemPrompt === true, onReplayDegrade: options.onReplayDegrade,
735
+ onReplayDegrade: options.onReplayDegrade,
736
+ assistantReplayScope: options.assistantReplayScope ?? defaultAssistantReplayScope(options.route, options.model),
541
737
  inHistorySystem: options.systemPromptUpdate === "in-history",
542
738
  snapshotMarker: options.systemPromptUpdate === "in-history" ? `\u0000lcx-prompt-snapshot:${globalThis.crypto.randomUUID()}` : undefined,
543
739
  maxRequestImageBytes: positiveSafeInteger(options.maxRequestImageBytes, DEFAULT_MAX_REQUEST_IMAGE_BYTES),
@@ -568,22 +764,22 @@ export async function serializeDshMessages(messages, ctx, options = {}) {
568
764
  }
569
765
  const projected = projectOffloadedImages(fileProjected, (ref) => offloadedImageText(ref, attachments === undefined ? undefined : imageAccess(ctx, attachments, ref)));
570
766
  const model = resolvePiResponsesModel(normalized);
571
- if (getResponsesToolSupport({ ...(isRecord(model.compat) ? model.compat : {}), supportsMidConvoSystemMessages: normalized.inHistorySystem }) !== "changes")
572
- normalized.toolUpdateMarker = undefined;
573
- const dshContext = { systemPrompt: normalized.systemPrompt, messages: await dshToPiMessages(projected, ctx, normalized, imageMap, requestImages), tools: [...normalized.tools] };
574
767
  const compat = isRecord(model.compat) ? model.compat : {};
575
768
  const supportsStrictMode = compat.supportsStrictMode === true;
576
769
  const supportsOpenAIGrammarTools = compat.supportsOpenAIGrammarTools === true;
577
- const supportsAdditionalTools = compat.supportsAdditionalTools === true;
578
- const supportsToolSearch = compat.supportsToolSearch === true;
770
+ const supportsAdditionalTools = !gatewayCompatibilityFallback && compat.supportsAdditionalTools === true;
771
+ const supportsToolSearch = !gatewayCompatibilityFallback && compat.supportsToolSearch === true;
772
+ if (getResponsesToolSupport({ ...compat, supportsAdditionalTools, supportsToolSearch, supportsMidConvoSystemMessages: normalized.inHistorySystem }) !== "changes")
773
+ normalized.toolUpdateMarker = undefined;
774
+ const dshContext = { systemPrompt: normalized.systemPrompt, messages: await dshToPiMessages(projected, ctx, normalized, imageMap, requestImages, model), tools: [...normalized.tools] };
579
775
  const deferredToolsMode = supportsAdditionalTools ? "additional-tools" : supportsToolSearch ? "tool-search" : undefined;
580
776
  const grammarToolInputProperties = createGrammarToolInputProperties([...(normalized.anchoredTools ?? dshContext.tools)], supportsOpenAIGrammarTools);
581
777
  const toolOptions = { supportsStrictMode, supportsOpenAIGrammarTools, toolSearchResult: false };
582
778
  const normalizedContext = normalizeContext(dshContext);
583
- // Pi treats whichever system message is first in the transcript as the leading prompt: it skips it when
584
- // includeSystemPrompt is false and renders it without the update framing. A serialization batch carrying
585
- // no caller-owned head (no systemPrompt/tools) must never promote a history update to that slot, so an
586
- // empty placeholder holds the leading position and renders nothing.
779
+ // Pi treats whichever system message is first in the transcript as the leading prompt and renders it
780
+ // without the update framing (omitting it only when empty). A serialization batch carrying no caller-owned
781
+ // head (no systemPrompt/tools) must never promote a history update to that slot, so an empty placeholder
782
+ // holds the leading position and renders nothing.
587
783
  let piContext = normalized.inHistorySystem && normalizedContext.messages[0]?.role === "system" && normalizedContext.messages === dshContext.messages
588
784
  ? { ...normalizedContext, messages: [{ role: "system", content: "", timestamp: 0 }, ...normalizedContext.messages] }
589
785
  : normalizedContext;
@@ -598,8 +794,12 @@ export async function serializeDshMessages(messages, ctx, options = {}) {
598
794
  const piModel = model;
599
795
  // Pi collapses every system message into the leading one unless the transport accepts mid-conversation
600
796
  // system messages, which would drop or reorder in-history updates in batched serialization.
601
- const configurationUpdateEffort = typeof options.configurationUpdateEffort === "string" ? options.configurationUpdateEffort : undefined;
602
- const convertedInput = convertResponsesMessages(piModel, piContext, new Set(["openai", "openai-codex", "opencode"]), { includeSystemPrompt: normalized.includeSystemPrompt, grammarToolInputProperties, supportsAdditionalTools, supportsToolSearch, supportsMidConvoSystemMessages: normalized.inHistorySystem, toolOptions, configurationUpdateEffort });
797
+ const configurationUpdateEffort = gatewayCompatibilityFallback
798
+ ? undefined
799
+ : typeof options.configurationUpdateEffort === "string"
800
+ ? options.configurationUpdateEffort
801
+ : undefined;
802
+ const convertedInput = convertResponsesMessages(piModel, piContext, ALLOWED_TOOL_CALL_PROVIDERS, { grammarToolInputProperties, supportsAdditionalTools, supportsToolSearch, supportsMidConvoSystemMessages: normalized.inHistorySystem, toolOptions, configurationUpdateEffort });
603
803
  if (configurationUpdateEffort !== undefined) {
604
804
  const tail = convertedInput.pop();
605
805
  if (!isConfigurationUpdate(tail, configurationUpdateEffort))
@@ -664,7 +864,22 @@ export async function serializeDshMessages(messages, ctx, options = {}) {
664
864
  const toolSupport = getResponsesToolSupport({ supportsAdditionalTools, supportsToolSearch, supportsMidConvoSystemMessages: normalized.inHistorySystem });
665
865
  const { requestTools } = resolveTranscriptTools(resolveTranscript(piContext, normalized.inHistorySystem).messages, toolSupport);
666
866
  const tools = requestTools.length === 0 ? undefined : convertResponsesTools(requestTools, toolOptions);
667
- return { input, preludeItemCount, supersededInstructionItems, imageMap, tools, model: piModel, grammarToolInputProperties, deferredToolsMode };
867
+ const budgetMessages = dshContext.messages.map((message) => {
868
+ if (message.role !== "system" || typeof message.content !== "string")
869
+ return message;
870
+ let content = message.content;
871
+ if (normalized.snapshotMarker !== undefined && content.startsWith(normalized.snapshotMarker)) {
872
+ content = content.slice(normalized.snapshotMarker.length);
873
+ }
874
+ else if (normalized.toolUpdateMarker !== undefined && content === normalized.toolUpdateMarker) {
875
+ content = "";
876
+ }
877
+ return content === message.content ? message : { ...message, content };
878
+ });
879
+ const budgetContext = { messages: budgetMessages, tools: dshContext.tools };
880
+ if (dshContext.systemPrompt !== undefined)
881
+ budgetContext.systemPrompt = dshContext.systemPrompt;
882
+ return { input, preludeItemCount, supersededInstructionItems, imageMap, tools, model: piModel, piContext: budgetContext, grammarToolInputProperties, deferredToolsMode };
668
883
  }
669
884
  export function responsesTools(tools) {
670
885
  if (tools === undefined)
@@ -329,6 +329,22 @@ function replayForMessage(message, route) {
329
329
  const replayState = message.source.replayState;
330
330
  if (!isRecord(replayState))
331
331
  return undefined;
332
+ const lcxReplay = replayState.lcxReplay;
333
+ const expectedRouteFingerprint = grokNativeReplayRouteFingerprint(route);
334
+ if (!isRecord(lcxReplay) ||
335
+ lcxReplay.version !== 1 ||
336
+ typeof lcxReplay.sourceSessionId !== "string" ||
337
+ lcxReplay.sourceSessionId.length === 0 ||
338
+ lcxReplay.sourceSessionId !== route.sessionId ||
339
+ typeof lcxReplay.headerAuthorityFingerprint !== "string" ||
340
+ lcxReplay.headerAuthorityFingerprint.length === 0 ||
341
+ typeof route.headerAuthorityFingerprint !== "string" ||
342
+ route.headerAuthorityFingerprint.length === 0 ||
343
+ lcxReplay.headerAuthorityFingerprint !== route.headerAuthorityFingerprint ||
344
+ typeof lcxReplay.grokRouteAuthorityFingerprint !== "string" ||
345
+ lcxReplay.grokRouteAuthorityFingerprint.length === 0 ||
346
+ lcxReplay.grokRouteAuthorityFingerprint !== expectedRouteFingerprint)
347
+ return undefined;
332
348
  const envelope = readGrokNativeReplayEnvelope(replayState.grokNative, route);
333
349
  if (!envelope)
334
350
  return undefined;
@@ -362,7 +378,7 @@ function replayForMessage(message, route) {
362
378
  return undefined;
363
379
  return { envelope, visible: anchors };
364
380
  }
365
- export function restoreGrokNativeReplay(input, messages, route) {
381
+ export function restoreGrokNativeReplay(input, messages, route, onRestore) {
366
382
  const restored = structuredClone(input);
367
383
  if (!route)
368
384
  return restored;
@@ -383,6 +399,7 @@ export function restoreGrokNativeReplay(input, messages, route) {
383
399
  if (start < 0)
384
400
  continue;
385
401
  restored.splice(start, replacedLength, ...structuredClone(envelope.output));
402
+ onRestore?.(message, envelope.output);
386
403
  cursor = start + envelope.output.length;
387
404
  }
388
405
  return restored;