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,17 +1,15 @@
1
1
  import { IMAGE_OFFLOAD_REQUIRED_CODE, LlmError, offloadedImageText, projectOffloadedImages, requiredImageOffload, requestImageHandleText, resolveImageAttachmentAccess, } from "@deepseek-ai/dsh-llm";
2
2
  import { requestImageDimensions } from "@deepseek-ai/dsh-attachment";
3
- import { convertResponsesMessages, convertResponsesTools, createGrammarToolInputProperties, getBuiltinModels, normalizeContext, } from "./pi-responses-runtime.js";
4
- import { wasPi086ResponsesModel } from "./pi086-responses-allowlist.js";
3
+ import { convertResponsesMessages, convertResponsesTools, createGrammarToolInputProperties, normalizeContext, resolveTranscript, resolveTranscriptTools, getResponsesToolSupport, } from "./pi-responses-runtime.js";
4
+ import { resolveCatalogModel } from "./model-catalog.js";
5
5
  import { withoutDeferLoading } from "./tool-surface.js";
6
+ import { isRecord, codedError, positiveSafeInteger } from "./util.js";
6
7
  const DEFAULT_MAX_REQUEST_IMAGE_BYTES = 20 * 1024 * 1024;
7
8
  const DEFAULT_REQUEST_IMAGE_PIXEL_BUDGET = 2048 * 2048;
8
9
  const DEFAULT_REQUEST_IMAGE_MAX_BYTES = 1024 * 1024;
9
10
  export { DEFAULT_MAX_REQUEST_IMAGE_BYTES, DEFAULT_REQUEST_IMAGE_PIXEL_BUDGET, DEFAULT_REQUEST_IMAGE_MAX_BYTES, };
10
- function isObject(value) {
11
- return value !== null && typeof value === "object" && !Array.isArray(value);
12
- }
13
11
  function isResponseInputItem(value) {
14
- if (!isObject(value))
12
+ if (!isRecord(value))
15
13
  return false;
16
14
  if (value.type === "compaction")
17
15
  return typeof value.encrypted_content === "string";
@@ -31,7 +29,7 @@ function isResponseInputItem(value) {
31
29
  }
32
30
  export function responseInputItems(value) {
33
31
  if (!Array.isArray(value) || !value.every(isResponseInputItem))
34
- throw error("native checkpoint contains an unsupported Responses input item", "LCX_COMPACT_INVALID_RESPONSE");
32
+ throw codedError("native checkpoint contains an unsupported Responses input item", "LCX_COMPACT_INVALID_RESPONSE");
35
33
  return value;
36
34
  }
37
35
  function asArray(value) {
@@ -40,18 +38,8 @@ function asArray(value) {
40
38
  function stringValue(value, fallback = "") {
41
39
  return typeof value === "string" ? value : fallback;
42
40
  }
43
- function positiveSafeInteger(value, fallback) {
44
- return Number.isSafeInteger(value) && value > 0
45
- ? value
46
- : fallback;
47
- }
48
- function error(message, code) {
49
- const value = new Error(message);
50
- value.code = code;
51
- return value;
52
- }
53
41
  function errorCode(value) {
54
- return isObject(value) && typeof value.code === "string" ? value.code : undefined;
42
+ return isRecord(value) && typeof value.code === "string" ? value.code : undefined;
55
43
  }
56
44
  function errorMessage(value) {
57
45
  return value instanceof Error ? value.message : String(value);
@@ -65,7 +53,7 @@ function attachmentServices(store) {
65
53
  };
66
54
  }
67
55
  function hasImageAttachment(value) {
68
- return isObject(value) && typeof value.attachmentId === "string" && typeof value.bytes === "number";
56
+ return isRecord(value) && typeof value.attachmentId === "string" && typeof value.bytes === "number";
69
57
  }
70
58
  function imageAccess(ctx, attachments, ref) {
71
59
  const mapper = ctx?.fs
@@ -81,7 +69,7 @@ function imageAccess(ctx, attachments, ref) {
81
69
  export async function resolveRouteModelFacts(ctx, route, signal) {
82
70
  const llm = ctx?.llm;
83
71
  if (llm === undefined)
84
- return { imageSupport: "unknown", systemPromptUpdate: undefined, toolUpdate: undefined, reasoning: undefined, resolved: false };
72
+ return { imageSupport: "unknown", systemPromptUpdate: undefined, toolUpdate: undefined, reasoning: undefined, contextWindow: undefined, resolved: false };
85
73
  try {
86
74
  const info = await llm.resolveModelInfo(stringValue(route.provider), stringValue(route.model), signal);
87
75
  const modalities = info.inputModalities;
@@ -96,34 +84,48 @@ export async function resolveRouteModelFacts(ctx, route, signal) {
96
84
  reasoning: info.reasoning === undefined
97
85
  ? undefined
98
86
  : { efforts: info.reasoning.efforts.map((effort) => String(effort.id)) },
87
+ contextWindow: positiveSafeInteger(info.context?.contextWindow, 0) || undefined,
99
88
  resolved: true,
100
89
  };
101
90
  }
102
91
  catch {
103
92
  // `resolved: false` tells callers the mode is unknown rather than known-absent, so they can refuse
104
93
  // to downgrade history that only an in-history route may legitimately contain.
105
- return { imageSupport: "unknown", systemPromptUpdate: undefined, toolUpdate: undefined, reasoning: undefined, resolved: false };
94
+ return { imageSupport: "unknown", systemPromptUpdate: undefined, toolUpdate: undefined, reasoning: undefined, contextWindow: undefined, resolved: false };
106
95
  }
107
96
  }
108
- export async function resolveModelImageSupport(ctx, route, signal) {
109
- return (await resolveRouteModelFacts(ctx, route, signal)).imageSupport;
97
+ /**
98
+ * The model facts a route serializes and sends with, laid over the bundled catalog descriptor. What DSH
99
+ * reported for the exact routed model (context window; on a GPT route also the reasoning capability) wins
100
+ * over the catalog entry, and on a GPT route a profile's declared `reasoningEfforts` (wire mapping, or `false`
101
+ * for a non-reasoning model) wins over both. A GPT model with no catalog entry follows what DSH itself
102
+ * resolved for it instead of being assumed to reason. A Grok route carries its reasoning projection
103
+ * separately (`modelDefaults` / `modelControls`), so only the context window is applied there.
104
+ */
105
+ export function routeModelOverride(routeConfig, facts, grok) {
106
+ 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 };
110
112
  }
111
113
  function attachmentResolver(ctx, options, requestImages) {
112
114
  const attachments = attachmentServices(ctx?.attachments);
113
115
  return async (block, signal) => {
114
116
  if (attachments === undefined)
115
- throw error("LCX requires the DSH request-image attachment API", "LCX_COMPACT_IMAGE_API_UNAVAILABLE");
117
+ throw codedError("LCX requires the DSH request-image attachment API", "LCX_COMPACT_IMAGE_API_UNAVAILABLE");
116
118
  const request = requestImages === undefined
117
119
  ? await attachments.store.readImageRequest(block.attachment, { ...requestImageDimensions(block.attachment.width, block.attachment.height, options.requestImagePixelBudget), maxBytes: options.requestImageMaxBytes }, signal)
118
120
  : requestImages.get(String(block.attachment.attachmentId));
119
121
  if (request === undefined)
120
- throw error("DSH attachment returned no request-image bytes", "LCX_COMPACT_IMAGE_UNAVAILABLE");
122
+ throw codedError("DSH attachment returned no request-image bytes", "LCX_COMPACT_IMAGE_UNAVAILABLE");
121
123
  const data = request.data;
122
124
  const mediaType = request.mediaType ?? block.attachment.mediaType;
123
125
  if (!(data instanceof Uint8Array) && !Buffer.isBuffer(data))
124
- throw error("DSH attachment returned no request-image bytes", "LCX_COMPACT_IMAGE_UNAVAILABLE");
126
+ throw codedError("DSH attachment returned no request-image bytes", "LCX_COMPACT_IMAGE_UNAVAILABLE");
125
127
  if (typeof mediaType !== "string" || !mediaType.startsWith("image/"))
126
- throw error("DSH attachment returned an invalid request-image media type", "LCX_COMPACT_IMAGE_UNAVAILABLE");
128
+ throw codedError("DSH attachment returned an invalid request-image media type", "LCX_COMPACT_IMAGE_UNAVAILABLE");
127
129
  return {
128
130
  data: Buffer.from(data),
129
131
  mediaType,
@@ -138,7 +140,7 @@ async function imagePart(block, ctx, options, imageMap, requestImages) {
138
140
  return { type: "input_text", text: "[image omitted because the target model does not support image input]" };
139
141
  const image = await attachmentResolver(ctx, options, requestImages)(block, options.signal);
140
142
  if (Math.ceil(image.data.byteLength / 3) * 4 > options.maxRequestImageBytes)
141
- throw error("one image exceeds the configured LCX request image bound", "LCX_COMPACT_IMAGE_TOO_LARGE");
143
+ throw codedError("one image exceeds the configured LCX request image bound", "LCX_COMPACT_IMAGE_TOO_LARGE");
142
144
  const imageUrl = `data:${image.mediaType};base64,${image.data.toString("base64")}`;
143
145
  imageMap.set(imageUrl, structuredClone(image.ref));
144
146
  return { type: "input_image", detail: "auto", image_url: imageUrl };
@@ -148,7 +150,7 @@ async function piImageParts(block, ctx, options, imageMap, requestImages) {
148
150
  return [{ type: "text", text: "[image omitted because the target model does not support image input]" }];
149
151
  const image = await attachmentResolver(ctx, options, requestImages)(block, options.signal);
150
152
  if (Math.ceil(image.data.byteLength / 3) * 4 > options.maxRequestImageBytes)
151
- throw error("one image exceeds the configured LCX request image bound", "LCX_COMPACT_IMAGE_TOO_LARGE");
153
+ throw codedError("one image exceeds the configured LCX request image bound", "LCX_COMPACT_IMAGE_TOO_LARGE");
152
154
  const data = image.data.toString("base64");
153
155
  imageMap.set(`data:${image.mediaType};base64,${data}`, structuredClone(image.ref));
154
156
  return [
@@ -157,13 +159,13 @@ async function piImageParts(block, ctx, options, imageMap, requestImages) {
157
159
  ];
158
160
  }
159
161
  function parseArguments(value) {
160
- if (isObject(value))
162
+ if (isRecord(value))
161
163
  return structuredClone(value);
162
164
  if (typeof value !== "string")
163
165
  return {};
164
166
  try {
165
167
  const parsed = JSON.parse(value);
166
- return isObject(parsed) ? parsed : {};
168
+ return isRecord(parsed) ? parsed : {};
167
169
  }
168
170
  catch {
169
171
  return {};
@@ -173,20 +175,25 @@ function emptyUsage() {
173
175
  return { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, totalTokens: 0, cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 } };
174
176
  }
175
177
  function invalidReplay(message) {
176
- return error(`invalid pi-ai replay state: ${message}`, "LCX_COMPACT_INVALID_REPLAY_STATE");
178
+ return codedError(`invalid pi-ai replay state: ${message}`, "LCX_COMPACT_INVALID_REPLAY_STATE");
177
179
  }
178
180
  function unsupportedContent(type) {
179
- return error(`LCX Compact cannot safely serialize DSH message content type: ${String(type)}`, "LCX_CHECKPOINT_PORTABLE_UNSUPPORTED_CONTENT");
181
+ return codedError(`LCX Compact cannot safely serialize DSH message content type: ${String(type)}`, "LCX_CHECKPOINT_PORTABLE_UNSUPPORTED_CONTENT");
180
182
  }
181
183
  function replayBlockType(type) {
182
184
  return type === "text" || type === "reasoning" || type === "tool-call" ? type : undefined;
183
185
  }
186
+ /** A Responses `configuration_update` input item carrying exactly this reasoning effort. */
187
+ function isConfigurationUpdate(item, effort) {
188
+ return isRecord(item) && item.type === "configuration_update" && isRecord(item.reasoning) &&
189
+ typeof item.reasoning.effort === "string" && (effort === undefined || item.reasoning.effort === effort);
190
+ }
184
191
  function replayTextPhase(signature) {
185
192
  if (typeof signature !== "string" || !signature.startsWith("{"))
186
193
  return undefined;
187
194
  try {
188
195
  const parsed = JSON.parse(signature);
189
- if (!isObject(parsed) || parsed.v !== 1 || typeof parsed.id !== "string")
196
+ if (!isRecord(parsed) || parsed.v !== 1 || typeof parsed.id !== "string")
190
197
  return undefined;
191
198
  return parsed.phase === "commentary" || parsed.phase === "final_answer"
192
199
  ? parsed.phase
@@ -203,29 +210,28 @@ function restorePortableAssistantPhases(input, messages, target) {
203
210
  continue;
204
211
  const sameProviderAndApi = message.provider === target.provider && message.api === target.api;
205
212
  const sameModel = sameProviderAndApi && message.model === target.id;
213
+ // Pi drops thinking from another model (it no longer becomes assistant text), so only text blocks
214
+ // become assistant message items.
206
215
  for (const block of message.content) {
207
- if (block.type === "text") {
208
- const phase = sameProviderAndApi && !sameModel
209
- ? replayTextPhase(block.textSignature)
210
- : undefined;
211
- slots.push({ text: block.text, ...(phase === undefined ? {} : { phase }) });
212
- }
213
- else if (block.type === "thinking" && !sameModel && !block.redacted && block.thinking.trim()) {
214
- slots.push({ text: block.thinking });
215
- }
216
+ if (block.type !== "text")
217
+ continue;
218
+ const phase = sameProviderAndApi && !sameModel
219
+ ? replayTextPhase(block.textSignature)
220
+ : undefined;
221
+ slots.push({ text: block.text, ...(phase === undefined ? {} : { phase }) });
216
222
  }
217
223
  }
218
224
  const output = structuredClone(input);
219
225
  let slotIndex = 0;
220
226
  for (const [index, candidate] of output.entries()) {
221
- if (!isObject(candidate) || candidate.type !== "message" || candidate.role !== "assistant" ||
227
+ if (!isRecord(candidate) || candidate.type !== "message" || candidate.role !== "assistant" ||
222
228
  !Array.isArray(candidate.content))
223
229
  continue;
224
230
  const slot = slots[slotIndex++];
225
231
  if (slot?.phase === undefined)
226
232
  continue;
227
233
  const text = candidate.content
228
- .map((part) => isObject(part) && part.type === "output_text" ? String(part.text ?? "") : "")
234
+ .map((part) => isRecord(part) && part.type === "output_text" ? String(part.text ?? "") : "")
229
235
  .join("");
230
236
  if (text === slot.text)
231
237
  output[index] = { ...candidate, phase: slot.phase };
@@ -233,10 +239,10 @@ function restorePortableAssistantPhases(input, messages, target) {
233
239
  return output;
234
240
  }
235
241
  export function readDshPiReplayState(value) {
236
- if (!isObject(value))
242
+ if (!isRecord(value))
237
243
  throw invalidReplay("expected a replay envelope");
238
244
  const responseValue = value.response;
239
- if (!isObject(responseValue) || responseValue.kind !== "pi-ai" || responseValue.version !== 2)
245
+ if (!isRecord(responseValue) || responseValue.kind !== "pi-ai" || responseValue.version !== 2)
240
246
  throw invalidReplay("expected a pi-ai version 2 response object");
241
247
  const required = ["api", "provider", "model"];
242
248
  for (const key of required)
@@ -248,10 +254,12 @@ export function readDshPiReplayState(value) {
248
254
  throw invalidReplay("responseModel must be a string");
249
255
  if (responseValue.responseId !== undefined && typeof responseValue.responseId !== "string")
250
256
  throw invalidReplay("responseId must be a string");
257
+ if (responseValue.providerThinkingLevel !== undefined && typeof responseValue.providerThinkingLevel !== "string")
258
+ throw invalidReplay("providerThinkingLevel must be a string");
251
259
  if (!Array.isArray(value.blocks))
252
260
  throw invalidReplay("blocks must be an array");
253
261
  const blocks = value.blocks.map((block, index) => {
254
- if (!isObject(block) || replayBlockType(block.type) === undefined)
262
+ if (!isRecord(block) || replayBlockType(block.type) === undefined)
255
263
  throw invalidReplay(`block ${index} has an unknown type`);
256
264
  for (const signature of ["textSignature", "thinkingSignature", "thoughtSignature", "namespace"])
257
265
  if (block[signature] !== undefined && typeof block[signature] !== "string")
@@ -299,7 +307,7 @@ function replayedAssistant(message, source) {
299
307
  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 } : {}) };
300
308
  throw invalidReplay(`block ${index} has an unsupported type`);
301
309
  });
302
- 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 } : {}), usage: emptyUsage(), stopReason: state.response.stopReason, timestamp: 0 };
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 };
303
311
  }
304
312
  function toPiAssistant(message, onReplayDegrade) {
305
313
  const source = message.source;
@@ -379,9 +387,22 @@ async function prepareRequestImageVersions(messages, ctx, options) {
379
387
  export function isPromptSnapshotMessage(message) {
380
388
  return message.role === "system" && message.source?.kind === "system-prompt";
381
389
  }
382
- /** Whether history after the leading head holds a non-empty DSH prompt snapshot (an in-history update). */
390
+ /**
391
+ * Mirrors the host adapter's `splitSystemPrompt`: the request-level `system` text and every leading system
392
+ * message form the initial prompt, joined by a blank line; empty parts drop out, on every route.
393
+ */
394
+ export function splitSystemPrompt(system, messages) {
395
+ let end = 0;
396
+ while (messages[end]?.role === "system")
397
+ end++;
398
+ const text = [system, ...messages.slice(0, end).map((message) => message.content
399
+ .filter((block) => block.type === "text")
400
+ .map((block) => block.text).join(""))];
401
+ return { systemPrompt: text.filter(Boolean).join("\n\n") || undefined, messages: messages.slice(end) };
402
+ }
403
+ /** Whether history after the leading system messages holds a non-empty DSH prompt snapshot (an in-history update). */
383
404
  export function hasMidHistoryPromptSnapshot(messages) {
384
- return messages.slice(1).some((message) => isPromptSnapshotMessage(message) &&
405
+ return splitSystemPrompt(undefined, messages).messages.some((message) => isPromptSnapshotMessage(message) &&
385
406
  message.content.some((block) => block.type === "text" && block.text.length > 0));
386
407
  }
387
408
  function flattenMessageText(message) {
@@ -402,11 +423,9 @@ async function dshToPiMessages(messages, ctx, options, imageMap, requestImages)
402
423
  // role (developer for reasoning models, system otherwise) at this exact position.
403
424
  const text = flattenMessageText(message);
404
425
  if (text.length > 0) {
405
- // A throwaway marker message directly before a DSH prompt snapshot lets the serializer trace the
406
- // rendered item back to its source message without inspecting text; it is removed afterwards.
407
- if (options.snapshotMarker !== undefined && isPromptSnapshotMessage(message))
408
- result.push({ role: "user", content: options.snapshotMarker, timestamp: 0 });
409
- result.push({ role: "system", content: text, timestamp: 0 });
426
+ // A throwaway prefix traces the rendered snapshot without adding a Pi message (message
427
+ // positions seed Pi's synthetic tool-search and assistant IDs). It is removed afterwards.
428
+ result.push({ role: "system", content: options.snapshotMarker !== undefined && isPromptSnapshotMessage(message) ? options.snapshotMarker + text : text, timestamp: 0 });
410
429
  }
411
430
  continue;
412
431
  }
@@ -419,16 +438,22 @@ async function dshToPiMessages(messages, ctx, options, imageMap, requestImages)
419
438
  if (options.anchoredTools === undefined)
420
439
  throw unsupportedContent("developer message");
421
440
  const toolsAdded = [];
441
+ const toolsRemoved = [];
422
442
  for (const block of message.content) {
443
+ if (block.type === "tool-removal") {
444
+ toolsRemoved.push({ name: block.toolName });
445
+ continue;
446
+ }
423
447
  if (block.type !== "tool-addition")
424
448
  throw unsupportedContent(block.type);
425
449
  const tool = options.anchoredTools.find((candidate) => candidate.name === block.toolName);
426
450
  if (tool === undefined)
427
- throw error(`LCX tool update references undeclared tool "${block.toolName}"`, "LCX_RESPONSES_INVALID_INPUT");
451
+ throw codedError(`LCX tool update references undeclared tool "${block.toolName}"`, "LCX_RESPONSES_INVALID_INPUT");
428
452
  toolsAdded.push(tool);
429
453
  }
430
- if (toolsAdded.length > 0)
431
- result.push({ role: "system", content: [], toolsAdded, timestamp: 0 });
454
+ if (toolsAdded.length > 0 || toolsRemoved.length > 0) {
455
+ result.push({ role: "system", content: options.toolUpdateMarker ?? "", toolsAdded, toolsRemoved, timestamp: 0 });
456
+ }
432
457
  continue;
433
458
  }
434
459
  if (message.role === "assistant") {
@@ -468,24 +493,17 @@ async function dshToPiMessages(messages, ctx, options, imageMap, requestImages)
468
493
  return result;
469
494
  }
470
495
  function builtinResponsesModel(provider, modelId) {
471
- // Keep descriptor and wire capabilities within the same frozen catalog scope as route ownership.
472
- if (!wasPi086ResponsesModel(provider, modelId))
473
- return undefined;
474
- try {
475
- return getBuiltinModels(provider).find((model) => model.id === modelId && model.api === "openai-responses");
476
- }
477
- catch {
478
- return undefined;
479
- }
496
+ // The exact bundled catalog entry (see `resolveCatalogModel`): the same descriptor the route resolution reads.
497
+ return resolveCatalogModel(provider, modelId).descriptor;
480
498
  }
481
499
  export function resolvePiResponsesModel(options) {
482
- const route = isObject(options.route) ? options.route : {};
483
- const explicit = isObject(options.model) ? options.model : {};
500
+ const route = isRecord(options.route) ? options.route : {};
501
+ const explicit = isRecord(options.model) ? options.model : {};
484
502
  const provider = stringValue(explicit.provider, stringValue(route.provider, "dsh-lcx-codex"));
485
503
  const id = stringValue(explicit.id, stringValue(route.model, "unknown"));
486
504
  const builtin = builtinResponsesModel(provider, id);
487
- const builtinRecord = isObject(builtin) ? builtin : {};
488
- const compat = { ...(isObject(builtinRecord.compat) ? builtinRecord.compat : {}), ...(isObject(options.responsesCompat) ? options.responsesCompat : {}), ...(isObject(explicit.compat) ? explicit.compat : {}) };
505
+ const builtinRecord = isRecord(builtin) ? builtin : {};
506
+ const compat = { ...(isRecord(builtinRecord.compat) ? builtinRecord.compat : {}), ...(isRecord(options.responsesCompat) ? options.responsesCompat : {}), ...(isRecord(explicit.compat) ? explicit.compat : {}) };
489
507
  const imageSupport = options.imageSupport === "supported" || options.imageSupport === "unsupported" ? options.imageSupport : "unknown";
490
508
  const input = imageSupport === "supported" ? ["text", "image"] : imageSupport === "unsupported" ? ["text"] : Array.isArray(explicit.input) ? explicit.input : Array.isArray(builtinRecord.input) ? builtinRecord.input : ["text"];
491
509
  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 } : {}) };
@@ -493,14 +511,14 @@ export function resolvePiResponsesModel(options) {
493
511
  export async function serializeDshMessages(messages, ctx, options = {}) {
494
512
  const anchored = options.toolUpdate === "addition-only";
495
513
  if (anchored && options.systemPromptUpdate !== "in-history")
496
- throw error("LCX addition-only tool updates require an in-history route", "LCX_RESPONSES_INVALID_INPUT");
514
+ throw codedError("LCX addition-only tool updates require an in-history route", "LCX_RESPONSES_INVALID_INPUT");
497
515
  for (const message of messages) {
498
516
  if (message.role === "developer" && !anchored)
499
517
  throw unsupportedContent("developer message");
500
518
  for (const block of message.content)
501
519
  if (block.type === "tool-addition" || block.type === "tool-removal") {
502
- // Removals never reach a serializer: the host projection drops them on an addition-only route.
503
- if (!anchored || message.role !== "developer" || block.type === "tool-removal")
520
+ // DSH normally folds removals on addition-only routes; direct transcripts still follow Pi.
521
+ if (!anchored || message.role !== "developer")
504
522
  throw unsupportedContent(block.type);
505
523
  }
506
524
  }
@@ -523,6 +541,7 @@ export async function serializeDshMessages(messages, ctx, options = {}) {
523
541
  maxRequestImageBytes: positiveSafeInteger(options.maxRequestImageBytes, DEFAULT_MAX_REQUEST_IMAGE_BYTES),
524
542
  requestImagePixelBudget: positiveSafeInteger(options.requestImagePixelBudget, DEFAULT_REQUEST_IMAGE_PIXEL_BUDGET),
525
543
  requestImageMaxBytes: positiveSafeInteger(options.requestImageMaxBytes, DEFAULT_REQUEST_IMAGE_MAX_BYTES),
544
+ toolUpdateMarker: anchored ? `lcx-tool-update:${globalThis.crypto.randomUUID()}` : undefined,
526
545
  tools: immediateDeclared,
527
546
  ...(anchored ? { anchoredTools: declaredTools } : {}),
528
547
  };
@@ -545,83 +564,122 @@ export async function serializeDshMessages(messages, ctx, options = {}) {
545
564
  }
546
565
  const projected = projectOffloadedImages(fileProjected, (ref) => offloadedImageText(ref, attachments === undefined ? undefined : imageAccess(ctx, attachments, ref)));
547
566
  const model = resolvePiResponsesModel(normalized);
567
+ if (getResponsesToolSupport({ ...(isRecord(model.compat) ? model.compat : {}), supportsMidConvoSystemMessages: normalized.inHistorySystem }) !== "changes")
568
+ normalized.toolUpdateMarker = undefined;
548
569
  const dshContext = { systemPrompt: normalized.systemPrompt, messages: await dshToPiMessages(projected, ctx, normalized, imageMap, requestImages), tools: [...normalized.tools] };
549
- const compat = isObject(model.compat) ? model.compat : {};
570
+ const compat = isRecord(model.compat) ? model.compat : {};
550
571
  const supportsStrictMode = compat.supportsStrictMode === true;
551
572
  const supportsOpenAIGrammarTools = compat.supportsOpenAIGrammarTools === true;
552
573
  const supportsAdditionalTools = compat.supportsAdditionalTools === true;
553
574
  const supportsToolSearch = compat.supportsToolSearch === true;
554
575
  const deferredToolsMode = supportsAdditionalTools ? "additional-tools" : supportsToolSearch ? "tool-search" : undefined;
555
576
  const grammarToolInputProperties = createGrammarToolInputProperties([...(normalized.anchoredTools ?? dshContext.tools)], supportsOpenAIGrammarTools);
556
- // Immediate declarations only: on an anchored surface deferred tools are declared by their history additions.
557
- const immediateTools = new Map();
558
- for (const tool of dshContext.tools)
559
- if (tool.name)
560
- immediateTools.set(tool.name, tool);
561
577
  const toolOptions = { supportsStrictMode, supportsOpenAIGrammarTools, toolSearchResult: false };
562
578
  const normalizedContext = normalizeContext(dshContext);
563
579
  // Pi treats whichever system message is first in the transcript as the leading prompt: it skips it when
564
580
  // includeSystemPrompt is false and renders it without the update framing. A serialization batch carrying
565
581
  // no caller-owned head (no systemPrompt/tools) must never promote a history update to that slot, so an
566
582
  // empty placeholder holds the leading position and renders nothing.
567
- const piContext = normalized.inHistorySystem && normalizedContext.messages[0]?.role === "system" && normalizedContext.messages === dshContext.messages
583
+ let piContext = normalized.inHistorySystem && normalizedContext.messages[0]?.role === "system" && normalizedContext.messages === dshContext.messages
568
584
  ? { ...normalizedContext, messages: [{ role: "system", content: "", timestamp: 0 }, ...normalizedContext.messages] }
569
585
  : normalizedContext;
586
+ const preludeMarker = options.tracePrelude && (normalized.systemPrompt || normalized.tools.length > 0)
587
+ ? `lcx-prelude:${globalThis.crypto.randomUUID()}` : undefined;
588
+ if (preludeMarker !== undefined) {
589
+ const head = piContext.messages[0];
590
+ if (head?.role !== "system" || typeof head.content !== "string")
591
+ throw codedError("LCX could not trace the request prelude", "LCX_RESPONSES_INVALID_INPUT");
592
+ piContext = { ...piContext, messages: [{ ...head, content: preludeMarker + head.content }, ...piContext.messages.slice(1)] };
593
+ }
570
594
  const piModel = model;
571
595
  // Pi collapses every system message into the leading one unless the transport accepts mid-conversation
572
596
  // system messages, which would drop or reorder in-history updates in batched serialization.
573
- const convertedInput = convertResponsesMessages(piModel, piContext, new Set(["openai", "openai-codex", "opencode"]), { includeSystemPrompt: normalized.includeSystemPrompt, grammarToolInputProperties, supportsAdditionalTools, supportsToolSearch, supportsMidConvoSystemMessages: normalized.inHistorySystem, toolOptions });
597
+ const configurationUpdateEffort = typeof options.configurationUpdateEffort === "string" ? options.configurationUpdateEffort : undefined;
598
+ const convertedInput = convertResponsesMessages(piModel, piContext, new Set(["openai", "openai-codex", "opencode"]), { includeSystemPrompt: normalized.includeSystemPrompt, grammarToolInputProperties, supportsAdditionalTools, supportsToolSearch, supportsMidConvoSystemMessages: normalized.inHistorySystem, toolOptions, configurationUpdateEffort });
599
+ if (configurationUpdateEffort !== undefined) {
600
+ const tail = convertedInput.pop();
601
+ if (!isConfigurationUpdate(tail, configurationUpdateEffort))
602
+ throw codedError("LCX could not trace the request's configuration update", "LCX_RESPONSES_INVALID_INPUT");
603
+ }
574
604
  const supersededInstructionItems = [];
575
605
  const unmarkedInput = [];
576
606
  let markersFound = 0;
577
- for (let position = 0; position < convertedInput.length; position += 1) {
578
- const item = convertedInput[position];
579
- if (normalized.snapshotMarker !== undefined && isMarkerItem(item, normalized.snapshotMarker)) {
607
+ let toolUpdatesFound = 0;
608
+ let preludeItemCount = preludeMarker === undefined ? 0 : -1;
609
+ let additionalDeclarationsSeen = 0;
610
+ for (const [position, candidate] of convertedInput.entries()) {
611
+ if (candidate.type === "additional_tools")
612
+ additionalDeclarationsSeen += 1;
613
+ let item = candidate;
614
+ const instruction = isRecord(item) && (item.role === "developer" || item.role === "system") &&
615
+ (item.type === undefined || item.type === "message") && typeof item.content === "string" ? item : undefined;
616
+ if (instruction && preludeMarker !== undefined && String(instruction.content).startsWith(preludeMarker)) {
617
+ if (preludeItemCount !== -1)
618
+ throw codedError("LCX duplicated the request prelude", "LCX_RESPONSES_INVALID_INPUT");
619
+ const content = String(instruction.content).slice(preludeMarker.length);
620
+ if (content)
621
+ unmarkedInput.push({ ...instruction, content });
622
+ preludeItemCount = unmarkedInput.length;
623
+ continue;
624
+ }
625
+ if (instruction && normalized.toolUpdateMarker !== undefined && String(instruction.content).startsWith(normalized.toolUpdateMarker)) {
626
+ toolUpdatesFound += 1;
627
+ const content = String(instruction.content).slice(normalized.toolUpdateMarker.length);
628
+ if (content !== "" && !content.startsWith("\n\n"))
629
+ throw codedError("LCX could not trace Pi's tool removal output", "LCX_RESPONSES_INVALID_INPUT");
630
+ // Pi places its own incremental notice immediately before each later declaration. Track
631
+ // that output by provenance, so it cannot survive as an orphan after checkpoint retention.
632
+ const declaration = convertedInput[position - 1];
633
+ if (isRecord(declaration) && declaration.type === "additional_tools" && additionalDeclarationsSeen > 1) {
634
+ const notice = unmarkedInput.at(-2);
635
+ if (!isRecord(notice) || (notice.role !== "developer" && notice.role !== "system") || notice.type !== undefined)
636
+ throw codedError("LCX could not trace Pi's incremental tools notice", "LCX_RESPONSES_INVALID_INPUT");
637
+ supersededInstructionItems.push(unmarkedInput.length - 2);
638
+ }
639
+ if (content.length > 0) {
640
+ supersededInstructionItems.push(unmarkedInput.length);
641
+ unmarkedInput.push({ ...instruction, content: content.slice(2) });
642
+ }
643
+ continue;
644
+ }
645
+ if (instruction && normalized.snapshotMarker !== undefined && String(instruction.content).startsWith(normalized.snapshotMarker)) {
580
646
  markersFound += 1;
581
- const rendered = convertedInput[position + 1];
582
- if (!isObject(rendered) || (rendered.role !== "developer" && rendered.role !== "system"))
583
- throw error("LCX could not trace a rendered system prompt snapshot", "LCX_RESPONSES_INVALID_INPUT");
584
647
  supersededInstructionItems.push(unmarkedInput.length);
585
- continue;
648
+ item = { ...instruction, content: String(instruction.content).slice(normalized.snapshotMarker.length) };
586
649
  }
587
650
  unmarkedInput.push(item);
588
651
  }
589
- if (normalized.snapshotMarker !== undefined) {
590
- // Markers are inserted only before non-empty snapshots (empty ones render nothing), so each inserted
591
- // marker must be found exactly once and none may survive in the output, even if a future Pi merged
592
- // adjacent user messages.
593
- const inserted = dshContext.messages.filter((message) => message.role === "user" && message.content === normalized.snapshotMarker).length;
594
- const nonce = normalized.snapshotMarker.slice(normalized.snapshotMarker.lastIndexOf(":") + 1);
595
- if (markersFound !== inserted || JSON.stringify(unmarkedInput).includes(nonce))
596
- throw error("LCX could not remove a system prompt snapshot marker", "LCX_RESPONSES_INVALID_INPUT");
652
+ const expectedMarkedSystems = (marker) => dshContext.messages.filter(message => message.role === "system" && typeof message.content === "string" && message.content.startsWith(marker)).length;
653
+ for (const [marker, found] of [[normalized.snapshotMarker, markersFound], [normalized.toolUpdateMarker, toolUpdatesFound]]) {
654
+ if (marker !== undefined && (found !== expectedMarkedSystems(marker) || JSON.stringify(unmarkedInput).includes(marker)))
655
+ throw codedError("LCX could not remove instruction provenance markers", "LCX_RESPONSES_INVALID_INPUT");
597
656
  }
657
+ if (preludeItemCount < 0 || (preludeMarker !== undefined && JSON.stringify(unmarkedInput).includes(preludeMarker)))
658
+ throw codedError("LCX could not remove the prelude marker", "LCX_RESPONSES_INVALID_INPUT");
598
659
  const input = restorePortableAssistantPhases(unmarkedInput, dshContext.messages, { provider: model.provider, id: model.id, api: model.api });
599
- const tools = options.tools === undefined ? undefined : convertResponsesTools([...immediateTools.values()], toolOptions);
600
- return { input, supersededInstructionItems, imageMap, tools, model: piModel, grammarToolInputProperties, deferredToolsMode };
601
- }
602
- function isMarkerItem(item, marker) {
603
- if (!isObject(item) || item.role !== "user" || !Array.isArray(item.content))
604
- return false;
605
- return item.content.length === 1 && isObject(item.content[0]) && item.content[0].text === marker;
660
+ const toolSupport = getResponsesToolSupport({ supportsAdditionalTools, supportsToolSearch, supportsMidConvoSystemMessages: normalized.inHistorySystem });
661
+ const { requestTools } = resolveTranscriptTools(resolveTranscript(piContext, normalized.inHistorySystem).messages, toolSupport);
662
+ const tools = requestTools.length === 0 ? undefined : convertResponsesTools(requestTools, toolOptions);
663
+ return { input, preludeItemCount, supersededInstructionItems, imageMap, tools, model: piModel, grammarToolInputProperties, deferredToolsMode };
606
664
  }
607
665
  export function responsesTools(tools) {
608
666
  if (tools === undefined)
609
667
  return undefined;
610
668
  if (!Array.isArray(tools))
611
- throw error("LCX Responses tools must be an array", "LCX_COMPACT_INVALID_TOOLS");
612
- if (tools.every((tool) => isObject(tool) && typeof tool.type === "string"))
669
+ throw codedError("LCX Responses tools must be an array", "LCX_COMPACT_INVALID_TOOLS");
670
+ if (tools.every((tool) => isRecord(tool) && typeof tool.type === "string"))
613
671
  return structuredClone(tools);
614
672
  for (const tool of tools)
615
- if (!isObject(tool) || typeof tool.name !== "string" || !tool.name)
616
- throw error("LCX Responses tool has no name", "LCX_COMPACT_INVALID_TOOLS");
673
+ if (!isRecord(tool) || typeof tool.name !== "string" || !tool.name)
674
+ throw codedError("LCX Responses tool has no name", "LCX_COMPACT_INVALID_TOOLS");
617
675
  return convertResponsesTools(tools, { supportsStrictMode: false, supportsOpenAIGrammarTools: false });
618
676
  }
619
677
  function isResponsesMessageItem(item) {
620
- return isObject(item) && typeof item.role === "string" && (item.type === undefined || item.type === "message");
678
+ return isRecord(item) && typeof item.role === "string" && (item.type === undefined || item.type === "message");
621
679
  }
622
680
  function mapImageParts(value, mapper) {
623
681
  return value.map((part) => {
624
- if (!isObject(part))
682
+ if (!isRecord(part))
625
683
  return structuredClone(part);
626
684
  if (part.type === "input_image" || part.type === "dsh_image_attachment")
627
685
  return mapper(part);
@@ -632,17 +690,17 @@ function mapImageParts(value, mapper) {
632
690
  }
633
691
  export function persistNativeImageReferences(output, imageMap) {
634
692
  const persist = (part) => {
635
- if (part.type === "dsh_image_attachment" && isObject(part.attachment))
693
+ if (part.type === "dsh_image_attachment" && isRecord(part.attachment))
636
694
  return structuredClone(part);
637
695
  if (part.type !== "input_image" || typeof part.image_url !== "string")
638
- throw error("native compact returned an invalid image item", "LCX_COMPACT_INVALID_RESPONSE");
696
+ throw codedError("native compact returned an invalid image item", "LCX_COMPACT_INVALID_RESPONSE");
639
697
  const ref = imageMap.get(part.image_url);
640
698
  if (ref === undefined)
641
- throw error("native compact returned an untracked image item", "LCX_COMPACT_INVALID_RESPONSE");
699
+ throw codedError("native compact returned an untracked image item", "LCX_COMPACT_INVALID_RESPONSE");
642
700
  return { type: "dsh_image_attachment", attachment: structuredClone(ref) };
643
701
  };
644
702
  return output.map((item) => {
645
- if (!isObject(item))
703
+ if (!isRecord(item))
646
704
  return structuredClone(item);
647
705
  if (isResponsesMessageItem(item) && Array.isArray(item.content))
648
706
  return { ...structuredClone(item), content: mapImageParts(item.content, persist) };
@@ -652,11 +710,11 @@ export function persistNativeImageReferences(output, imageMap) {
652
710
  });
653
711
  }
654
712
  async function hydratePart(part, ctx, options) {
655
- if (!isObject(part) || part.type !== "dsh_image_attachment")
713
+ if (!isRecord(part) || part.type !== "dsh_image_attachment")
656
714
  return structuredClone(part);
657
715
  const attachment = part.attachment;
658
716
  if (!hasImageAttachment(attachment))
659
- throw error("native compact returned an invalid image attachment", "LCX_COMPACT_INVALID_RESPONSE");
717
+ throw codedError("native compact returned an invalid image attachment", "LCX_COMPACT_INVALID_RESPONSE");
660
718
  if (options.imageSupport === "unsupported")
661
719
  return { type: "input_text", text: "[image omitted because the target model does not support image input]" };
662
720
  return imagePart({ attachment }, ctx, options, options.imageMap);
@@ -670,7 +728,7 @@ export function inputImageBase64Lengths(input) {
670
728
  visit(child);
671
729
  return;
672
730
  }
673
- if (!isObject(value))
731
+ if (!isRecord(value))
674
732
  return;
675
733
  if (value.type === "input_image" && typeof value.image_url === "string") {
676
734
  const url = value.image_url;
@@ -710,7 +768,7 @@ export function assertAggregateImageBudget(options) {
710
768
  if (removed >= excess) {
711
769
  throw new LlmError(`LCX request images exceed the configured base64 bound once checkpoint images are counted; ${count} more oldest occurrence(s) must be offloaded.`, IMAGE_OFFLOAD_REQUIRED_CODE, { offloadImages: count });
712
770
  }
713
- throw error(`Images retained in the compaction checkpoint (${options.fixedBytes} base64 bytes) leave no room within the ${options.maxBytes}-byte request image bound, and offloading new images cannot recover it; start a new session or raise maxRequestImageBytes`, "LCX_CHECKPOINT_IMAGE_BUDGET");
771
+ throw codedError(`Images retained in the compaction checkpoint (${options.fixedBytes} base64 bytes) leave no room within the ${options.maxBytes}-byte request image bound, and offloading new images cannot recover it; start a new session or raise maxRequestImageBytes`, "LCX_CHECKPOINT_IMAGE_BUDGET");
714
772
  }
715
773
  export async function hydrateNativeImageReferences(output, ctx, options = {}) {
716
774
  const normalized = {
@@ -723,7 +781,7 @@ export async function hydrateNativeImageReferences(output, ctx, options = {}) {
723
781
  };
724
782
  const result = [];
725
783
  for (const item of asArray(output)) {
726
- if (!isObject(item)) {
784
+ if (!isRecord(item)) {
727
785
  result.push(structuredClone(item));
728
786
  continue;
729
787
  }
@@ -1,4 +1,5 @@
1
1
  import { createHash } from "node:crypto";
2
+ import { isRecord } from "./util.js";
2
3
  export const GROK_NATIVE_SERVER_TOOL_TYPES = new Set([
3
4
  "web_search_call",
4
5
  "x_search_call",
@@ -28,9 +29,6 @@ const GROK_CITATION_ID_MARKER = /([ \t]?)\{render_inline_citation:citation_id=(\
28
29
  const GROK_UI_CITATION_MARKER = /render_inline_citation[^\r\n]*[ \t]*/giu;
29
30
  const GROK_PRIVATE_RENDER_MARKER = /[\uE000-\uF8FF]render_inline_citation[^\uE000-\uF8FF\r\n]*[\uE000-\uF8FF][ \t]*/giu;
30
31
  const GROK_EOS_MARKER = /<\|eos\|>/giu;
31
- function isRecord(value) {
32
- return value !== null && typeof value === "object" && !Array.isArray(value);
33
- }
34
32
  function citationId(annotation) {
35
33
  const value = annotation.citation_id ?? annotation.citationId ?? annotation.id;
36
34
  return typeof value === "string" || typeof value === "number"