dsh-lcx-codex 0.5.1-pre.1 → 0.5.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.
package/README.md CHANGED
@@ -37,16 +37,16 @@ LCX Codex 是 [DeepSeek Harness](https://github.com/deepseek-ai/DeepSeek-Harness
37
37
 
38
38
  | 你的 DSH 版本 | 安装的 LCX 版本 | 安装命令中的标签 |
39
39
  | --- | --- | --- |
40
- | `0.2.1-alpha.2` 及以后 | `0.5.1-pre.1`(预发布版) | `@prelatest` |
41
- | `0.2.0` 系列(`0.2.0-rc.2`,早于 `0.2.1-alpha.2`) | `0.5.0`(稳定版) | `@latest` |
40
+ | `0.2.1-alpha.2` 及以后 | 最新预发布版(`0.6.x` 预发布线) | `@prelatest` |
41
+ | `0.2.0` 系列(`0.2.0-rc.2`,早于 `0.2.1-alpha.2`) | `0.5.2`(稳定版) | `@latest` |
42
42
  | `0.1.6` 系列(`0.1.6-alpha.2` 及以后) | `0.4.4-pre.1`(旧预发布版) | `@0.4.4-pre.1` |
43
43
  | `0.1.5` 系列(`0.1.5-rc.1` 及以后) | `0.4.3`(稳定版) | `@0.4.3` |
44
44
 
45
- 请务必按表格选择:几个版本互不兼容,装错版本时安装命令可能只给出依赖警告,但插件可能无法正常工作。`@latest` 是稳定版 `0.5.0`(发布基线 DSH `0.2.0-rc.2`,host / plugin Pi `0.87.1`),DSH `0.1.5` 用户请固定安装 `@0.4.3`。
45
+ 请务必按表格选择:几个版本互不兼容,装错版本时安装命令可能只给出依赖警告,但插件可能无法正常工作。`@latest` 是稳定版 `0.5.2`(发布基线 DSH `0.2.0-rc.2`,host / plugin Pi `0.87.1`;相对 `0.5.0` 只包含缺陷修复),DSH `0.1.5` 用户请固定安装 `@0.4.3`。
46
46
 
47
- `0.5.1-pre.1` 是预发布版,面向 DSH `0.2.1-alpha.2`。它的安装范围**只有下限**(`>=0.2.1-alpha.2`),没有上限:更新的 DSH 版本也能装上,但**没有经过验证**。本次预发布的验证基线只有 DSH `0.2.1-alpha.2`(host Pi `1.1.0`,plugin Pi `0.87.1`),请不要据此认为其他 DSH 版本受支持。
47
+ `@prelatest` 指向 `0.6.x` 预发布线的最新预发布版,面向 DSH `0.2.1-alpha.2` 及以后。它的安装范围**只有下限**(`>=0.2.1-alpha.2`),没有上限:更新的 DSH 版本也能装上,但**没有经过验证**。每个预发布版只验证其发布说明里写明的 DSH 基线(目前为 DSH `0.2.1-alpha.2`),请不要据此认为其他 DSH 版本受支持。
48
48
 
49
- > 注意:稳定版 `0.5.0` 的安装范围写作 `>=0.2.0-rc.2 <0.2.1`,按 DSH 的版本范围规则,`0.2.1` 的预发布版(如 `0.2.1-alpha.2`)也可能被放行安装,但这些版本**不在** `0.5.0` 的支持和验证范围内;DSH `0.2.1-alpha.2` 及以后请使用 `@prelatest`。
49
+ > 注意:稳定版 `0.5.2` 的安装范围写作 `>=0.2.0-rc.2 <0.2.1`,按 DSH 的版本范围规则,`0.2.1` 的预发布版(如 `0.2.1-alpha.2`)也可能被放行安装,但这些版本**不在** `0.5.2` 的支持和验证范围内;DSH `0.2.1-alpha.2` 及以后请使用 `@prelatest`。
50
50
 
51
51
  ### 2. 安装并启动
52
52
 
@@ -124,11 +124,11 @@ dsh web
124
124
 
125
125
  ## 从旧版本升级
126
126
 
127
- - **DSH 升级到 `0.2.1-alpha.2` 或更新版本**:把插件换成 `@prelatest`(`0.5.1-pre.1`),稳定版 `0.5.0` 没有在这些 DSH 版本上验证。
128
- - **DSH 升级到 `0.2.0` 系列**:把插件换成 `@latest`(`0.5.0`),旧版 `0.4.x` 不支持 DSH `0.2`。
127
+ - **DSH 升级到 `0.2.1-alpha.2` 或更新版本**:把插件换成 `@prelatest`(`0.6.x` 预发布线),稳定版 `0.5.2` 没有在这些 DSH 版本上验证。
128
+ - **DSH 升级到 `0.2.0` 系列**:把插件换成 `@latest`(`0.5.2`),旧版 `0.4.x` 不支持 DSH `0.2`。
129
129
  - **DSH `0.1.6` 系列**:继续使用 `@0.4.4-pre.1`。
130
- - **DSH `0.1.5` 系列**:继续使用 `@0.4.3`;`@latest` 现在指向支持 DSH `0.2` 的 `0.5.0`。
131
- - **已装了 `0.5.0-pre.1`**:DSH `0.2.0` 系列重新运行 `@latest` 的 `add` 命令即可升到 `0.5.0`(两者运行时代码一致);DSH `0.2.1-alpha.2` 及以后用 `@prelatest`。
130
+ - **DSH `0.1.5` 系列**:继续使用 `@0.4.3`;`@latest` 现在指向支持 DSH `0.2` 的 `0.5.2`。
131
+ - **已装了 `0.5.0`、`0.5.1` 或 `0.5.0-pre.1`**:DSH `0.2.0` 系列重新运行 `@latest` 的 `add` 命令即可升到 `0.5.2`(修复了缺陷,安装范围不变,详见 CHANGELOG;`0.5.2` 修复了 profile 里没写 `api` 时 `gpt-6-sol` 等模型报 `NO_ADAPTER` 的问题);DSH `0.2.1-alpha.2` 及以后用 `@prelatest`。
132
132
  - **升级后建议新建会话**:旧版本保存的压缩状态不保证兼容。DSH `0.2` 迁移过的旧长会话如果带有旧版 LCX 压缩检查点,LCX 会提示 `LCX_CHECKPOINT_UNSUPPORTED`,新建会话即可;其他模型不受影响。
133
133
 
134
134
  ## 停用与卸载
@@ -149,7 +149,7 @@ dsh web
149
149
  - **Markdown 图片换行**:为避免 DSH `0.2` 中图片后的文字挤到图片右侧,LCX 会让回答里的 Markdown 图片单独占一行,这一点与“搜索媒体预览”开关无关;句中的小图也会因此换行。
150
150
  - **DSH `0.1.5` + LCX `0.4.3`**:在插件设置中启用或停用 LCX 后,已打开的浏览器页面可能需要刷新。
151
151
  - **DSH `0.2.1` 的工具更新**:DSH `0.2.1` 的“只增不改”增量工具更新没有实现,LCX 仍然每次请求都发送完整的当前工具列表;这一变化带来的缓存收益不在声明范围内。
152
- - **验证范围**:`0.5.1-pre.1` 的验证基线只有 DSH `0.2.1-alpha.2`(预发布,host Pi `1.1.0`),安装范围没有上限,更新的 DSH 版本未经验证;该基线上的真实运行验证覆盖 GPT 路由、搜索和压缩等主要场景,Grok 路由、Alpha 搜索和用户图片输入没有在这一轮覆盖。`0.5.0` 的发布基线为 DSH `0.2.0-rc.2`;`0.4.4-pre.1` 在 DSH `0.1.6-alpha.2` 上完整验证;`0.4.3` 的稳定发布基线是 DSH `0.1.5-rc.1`,并已另外验证 DSH `0.1.5-rc.2`。同一安装范围内其他后续版本仍需单独评估,遇到问题欢迎反馈。
152
+ - **验证范围**:`@prelatest`(`0.6.x` 预发布线)只验证其发布说明里写明的 DSH 基线(目前为 DSH `0.2.1-alpha.2`),安装范围没有上限,更新的 DSH 版本未经验证;具体验证覆盖范围见对应预发布版的 CHANGELOG 和发布说明。`0.5.2` 的发布基线为 DSH `0.2.0-rc.2`(host / plugin Pi `0.87.1`);`0.4.4-pre.1` 在 DSH `0.1.6-alpha.2` 上完整验证;`0.4.3` 的稳定发布基线是 DSH `0.1.5-rc.1`,并已另外验证 DSH `0.1.5-rc.2`。同一安装范围内其他后续版本仍需单独评估,遇到问题欢迎反馈。
153
153
 
154
154
  ## 常见问题
155
155
 
@@ -166,7 +166,7 @@ dsh web
166
166
  从较早版本一路升级上来的配置目录里可能残留旧的依赖包。先停止 `dsh web`,把 DSH 主目录(默认 `~/.dsh`,或 `DSH_HOME` 指定的目录)下 `profiles/web/` 里的 `node_modules` 和 `pnpm-lock.yaml` 移走或删除,再重新运行对应的 `dsh plugin --profile web add dsh-lcx-codex@latest`(DSH `0.2.1-alpha.2` 及以后用 `@prelatest`),然后启动 `dsh web`。
167
167
 
168
168
  **刚发布新版时 `@latest` / `@prelatest` 装到了旧版本?**
169
- 包管理器的缓存可能还没刷新,DSH 会因版本不兼容拒绝安装。稍等几分钟重试,或直接指定版本号,例如 `dsh-lcx-codex@0.5.1-pre.1` 或 `dsh-lcx-codex@0.5.0`。
169
+ 包管理器的缓存可能还没刷新,DSH 会因版本不兼容拒绝安装。稍等几分钟重试,或直接指定版本号,例如 `dsh-lcx-codex@0.5.2`。
170
170
 
171
171
  ## 反馈问题
172
172
 
@@ -2,9 +2,9 @@
2
2
 
3
3
  `lib/pi-responses-runtime.js` is generated from public exports of the packages below. The build recipe in `scripts/build-pi-runtime.mjs` verifies that no other third-party package contributes output bytes.
4
4
 
5
- ## @earendil-works/pi-ai 0.85.1
5
+ ## @earendil-works/pi-ai 0.87.1
6
6
 
7
- Upstream source: https://github.com/earendil-works/pi/tree/v0.85.1/packages/ai
7
+ Upstream source: https://github.com/earendil-works/pi/tree/v0.87.1/packages/ai
8
8
 
9
9
  Included public Responses helpers and model catalogs:
10
10
 
@@ -1,5 +1,11 @@
1
1
  import { AsyncLocalStorage } from "node:async_hooks";
2
2
  const calls = new AsyncLocalStorage();
3
+ /**
4
+ * Usage a search already billed before its body threw, keyed by the execution object the registry
5
+ * passes to both the body and the around-dispatch wrappers. DSH builds a failed result from the thrown
6
+ * error alone, so the records can only reach `meta` through `installFailedAuxiliaryUsage`.
7
+ */
8
+ const failedBilling = new WeakMap();
3
9
  const object = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
4
10
  const count = (value) => typeof value === "number" && Number.isSafeInteger(value) && value >= 0;
5
11
  /** Collect only authoritative Responses billing, independently of context size. */
@@ -9,13 +15,17 @@ export function recordHostedUsage(response, requestId, provider, model) {
9
15
  return;
10
16
  const u = response.usage;
11
17
  const input = u.input_tokens, output = u.output_tokens, total = u.total_tokens;
12
- const cached = object(u.input_tokens_details) ? u.input_tokens_details.cached_tokens : undefined;
13
- if (!count(input) || !count(output) || !count(total) || !count(cached)
14
- || cached > input || input + output !== total)
18
+ const details = object(u.input_tokens_details) ? u.input_tokens_details : undefined;
19
+ const cached = details?.cached_tokens;
20
+ // Same disjoint accounting as the main Responses path: input = input_tokens - cached - cache_write.
21
+ // An absent or null cache_write_tokens is zero; any other non-counter value is not authoritative.
22
+ const written = details?.cache_write_tokens ?? 0;
23
+ if (!count(input) || !count(output) || !count(total) || !count(cached) || !count(written)
24
+ || cached + written > input || input + output !== total)
15
25
  return;
16
26
  store.push({ requestId, provider, model, usage: {
17
- inputTokens: input - cached, outputTokens: output, totalTokens: total,
18
- cacheReadTokens: cached, cacheWriteTokens: 0,
27
+ inputTokens: input - cached - written, outputTokens: output, totalTokens: total,
28
+ cacheReadTokens: cached, cacheWriteTokens: written,
19
29
  } });
20
30
  }
21
31
  /** Extend only LCX-owned search output; rendering and request schemas stay intact. */
@@ -51,7 +61,16 @@ export function withAuxiliaryUsage(tool) {
51
61
  async execute(args, exec) {
52
62
  const records = [];
53
63
  return calls.run(records, async () => {
54
- const value = await tool.execute(args, exec);
64
+ let value;
65
+ try {
66
+ value = await tool.execute(args, exec);
67
+ }
68
+ catch (error) {
69
+ // Already-billed requests must not vanish with the failure that followed them.
70
+ if (records.length > 0 && object(exec))
71
+ failedBilling.set(exec, records);
72
+ throw error;
73
+ }
55
74
  if (records.length === 0)
56
75
  return value;
57
76
  if (!object(value))
@@ -61,3 +80,23 @@ export function withAuxiliaryUsage(tool) {
61
80
  },
62
81
  };
63
82
  }
83
+ /**
84
+ * Attach the billing a failed search collected to its error result. The around-dispatch wrapper is the
85
+ * one DSH seam that may author `meta` on a failure; the session search-usage projection then reads it
86
+ * from the persisted `tool/result` exactly as it does for a successful call.
87
+ */
88
+ export const failedAuxiliaryUsageListener = async (exec, next) => {
89
+ const result = await next();
90
+ if (!result.isError)
91
+ return result;
92
+ const records = failedBilling.get(exec);
93
+ if (records === undefined || records.length === 0)
94
+ return result;
95
+ const meta = object(result.meta) ? { ...result.meta } : {};
96
+ meta.auxiliaryUsage = records;
97
+ return { ...result, meta };
98
+ };
99
+ /** Register {@link failedAuxiliaryUsageListener} on one context; returns the disposer. */
100
+ export function installFailedAuxiliaryUsage(ctx) {
101
+ return ctx.on("tools/execute", failedAuxiliaryUsageListener);
102
+ }
package/lib/client.js CHANGED
@@ -108,7 +108,7 @@
108
108
  }
109
109
  if (depth !== 0) continue;
110
110
  const value = destination(text.slice(open + 1, close - 1));
111
- if (value !== null) onDestination(value);
111
+ if (value !== null) onDestination(value, index);
112
112
  for (let offset = index; offset < close; offset += 1) chars[offset] = " ";
113
113
  index = close - 1;
114
114
  }
@@ -246,21 +246,23 @@
246
246
  if (value !== void 0) addNative(value);
247
247
  return " ".repeat(_match.length);
248
248
  });
249
- const candidates = [];
249
+ const found = [];
250
250
  visible = collectInlineLinkDestinations(
251
251
  visible,
252
- (value) => candidates.push(value)
252
+ (value, position) => found.push({ value, position })
253
253
  );
254
254
  visible.replace(
255
255
  REFERENCE_LINK,
256
- (_match, _prefix, label, explicit) => {
256
+ (_match, prefix, label, explicit, offset) => {
257
257
  const value = definitions.get(referenceLabel(explicit === "" ? label : explicit));
258
- if (value !== void 0) candidates.push(value);
258
+ if (value !== void 0) found.push({ value, position: offset + prefix.length });
259
259
  return _match;
260
260
  }
261
261
  );
262
- visible = visible.replace(REFERENCE_DEFINITION, " ");
263
- for (const match of visible.matchAll(URL_TOKEN)) candidates.push(match[0]);
262
+ visible = visible.replace(REFERENCE_DEFINITION, (definition) => " ".repeat(definition.length));
263
+ for (const match of visible.matchAll(URL_TOKEN))
264
+ found.push({ value: match[0], position: match.index ?? 0 });
265
+ const candidates = found.sort((a, b) => a.position - b.position).map((entry) => entry.value);
264
266
  const result = [];
265
267
  const seen = /* @__PURE__ */ new Set();
266
268
  for (const candidate of candidates) {
@@ -396,18 +398,16 @@ a.lcx-media-chip:hover{text-decoration:underline;text-underline-offset:2px}
396
398
  return { ...group[0], sources: group.map((value) => value.url) };
397
399
  });
398
400
  }
399
- var CHAT_FLOW_SLOT = "conversation.chat.flow";
400
401
  var activeDialogByDocument = /* @__PURE__ */ new WeakMap();
401
402
  function inlineMediaAnswer(marker) {
402
403
  const row = marker.closest('[data-chat-flow-kind="lcx-search-media"]');
403
404
  const ElementClass = marker.ownerDocument.defaultView?.HTMLElement;
404
405
  if (!ElementClass || !row || !row.dataset.chatTurn) return null;
405
406
  const group = row.closest("[data-chat-group-key][data-step-process]");
406
- const siblings = group?.parentElement;
407
- const column = siblings?.dataset.slot === CHAT_FLOW_SLOT ? siblings.parentElement : siblings;
408
- if (!group || !(siblings instanceof ElementClass) || !(column instanceof ElementClass) || !column.hasAttribute("data-chat-flow") || group.dataset.chatTurn !== row.dataset.chatTurn) return null;
407
+ const column = group?.parentElement;
408
+ if (!group || !(column instanceof ElementClass) || !column.hasAttribute("data-chat-flow") || group.dataset.chatTurn !== row.dataset.chatTurn) return null;
409
409
  let answer = null;
410
- for (const child of siblings.children) {
410
+ for (const child of column.children) {
411
411
  if (child === group) break;
412
412
  if (child instanceof ElementClass && child.dataset.chatFlowKind === "assistant-step" && child.dataset.chatGroupPart === "response" && child.dataset.chatTurn === row.dataset.chatTurn) {
413
413
  answer = child;
package/lib/dsh-compat.js CHANGED
@@ -75,6 +75,23 @@ export function scopedToolRuntime(agent) {
75
75
  : {}),
76
76
  };
77
77
  }
78
+ /**
79
+ * Listen on the around-dispatch `tools/execute` seam of one Agent only. The listener rides the Agent's
80
+ * own scoped context, so it never observes another Agent's calls and no global wrapper is installed.
81
+ * Returns the disposer, or undefined when the Agent exposes no scoped context.
82
+ */
83
+ export function onScopedToolExecute(agent, listener) {
84
+ const ctx = agentLike(agent)?.ctx;
85
+ if (!mutableService(ctx) || typeof ctx.on !== "function")
86
+ return undefined;
87
+ try {
88
+ const dispose = Reflect.apply(ctx.on, ctx, ["tools/execute", listener]);
89
+ return typeof dispose === "function" ? () => void Reflect.apply(dispose, undefined, []) : undefined;
90
+ }
91
+ catch {
92
+ return undefined;
93
+ }
94
+ }
78
95
  export function tokenMeterTotal(value, session) {
79
96
  if (!mutableService(value) || typeof value.measure !== "function")
80
97
  return undefined;
@@ -1,7 +1,7 @@
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, } from "./pi-responses-runtime.js";
4
+ import { resolveCatalogModel } from "./model-catalog.js";
5
5
  const DEFAULT_MAX_REQUEST_IMAGE_BYTES = 20 * 1024 * 1024;
6
6
  const DEFAULT_REQUEST_IMAGE_PIXEL_BUDGET = 2048 * 2048;
7
7
  const DEFAULT_REQUEST_IMAGE_MAX_BYTES = 1024 * 1024;
@@ -72,15 +72,11 @@ function imageAccess(ctx, attachments, ref) {
72
72
  : () => undefined;
73
73
  return attachments.resolveAccess(mapper, ref);
74
74
  }
75
- /**
76
- * Facts DSH resolves for the exact routed model through `llm.resolveModelInfo`, the same
77
- * adapter-owned metadata the agent loop uses to choose how a system prompt change is recorded.
78
- * Resolution failure reports `resolved: false` with conservative defaults (unknown image support, no in-history mode).
79
- */
75
+ /** One adapter-owned lookup supplies the model facts a route needs: image support and reasoning efforts. */
80
76
  export async function resolveRouteModelFacts(ctx, route, signal) {
81
77
  const llm = ctx?.llm;
82
78
  if (llm === undefined)
83
- return { imageSupport: "unknown", systemPromptUpdate: undefined, resolved: false };
79
+ return { imageSupport: "unknown", reasoning: undefined, contextWindow: undefined, resolved: false };
84
80
  try {
85
81
  const info = await llm.resolveModelInfo(stringValue(route.provider), stringValue(route.model), signal);
86
82
  const modalities = info.inputModalities;
@@ -90,14 +86,15 @@ export async function resolveRouteModelFacts(ctx, route, signal) {
90
86
  : modalities !== undefined
91
87
  ? "unsupported"
92
88
  : "unknown",
93
- systemPromptUpdate: info.systemPromptUpdate === "in-history" ? "in-history" : undefined,
89
+ reasoning: info.reasoning === undefined
90
+ ? undefined
91
+ : { efforts: info.reasoning.efforts.map((effort) => String(effort.id)) },
92
+ contextWindow: positiveSafeInteger(info.context?.contextWindow, 0) || undefined,
94
93
  resolved: true,
95
94
  };
96
95
  }
97
96
  catch {
98
- // `resolved: false` tells callers the mode is unknown rather than known-absent, so they can refuse
99
- // to downgrade history that only an in-history route may legitimately contain.
100
- return { imageSupport: "unknown", systemPromptUpdate: undefined, resolved: false };
97
+ return { imageSupport: "unknown", reasoning: undefined, contextWindow: undefined, resolved: false };
101
98
  }
102
99
  }
103
100
  export async function resolveModelImageSupport(ctx, route, signal) {
@@ -370,15 +367,6 @@ async function prepareRequestImageVersions(messages, ctx, options) {
370
367
  }));
371
368
  return new Map(versions);
372
369
  }
373
- /** A full system prompt snapshot written by DSH's own system-prompt projection. */
374
- export function isPromptSnapshotMessage(message) {
375
- return message.role === "system" && message.source?.kind === "system-prompt";
376
- }
377
- /** Whether history after the leading head holds a non-empty DSH prompt snapshot (an in-history update). */
378
- export function hasMidHistoryPromptSnapshot(messages) {
379
- return messages.slice(1).some((message) => isPromptSnapshotMessage(message) &&
380
- message.content.some((block) => block.type === "text" && block.text.length > 0));
381
- }
382
370
  function flattenMessageText(message) {
383
371
  return message.content.map((block) => {
384
372
  if (block.type !== "text")
@@ -391,20 +379,6 @@ async function dshToPiMessages(messages, ctx, options, imageMap, requestImages)
391
379
  const toolNames = new Map();
392
380
  for (const message of messages) {
393
381
  if (message.role === "system") {
394
- if (options.inHistorySystem === true) {
395
- // In-history routes record a changed system prompt as a full later `system` message that keeps
396
- // instruction authority. Pi renders a non-leading system message with the model's instruction
397
- // role (developer for reasoning models, system otherwise) at this exact position.
398
- const text = flattenMessageText(message);
399
- if (text.length > 0) {
400
- // A throwaway marker message directly before a DSH prompt snapshot lets the serializer trace the
401
- // rendered item back to its source message without inspecting text; it is removed afterwards.
402
- if (options.snapshotMarker !== undefined && isPromptSnapshotMessage(message))
403
- result.push({ role: "user", content: options.snapshotMarker, timestamp: 0 });
404
- result.push({ role: "system", content: text, timestamp: 0 });
405
- }
406
- continue;
407
- }
408
382
  result.push({ role: "user", content: flattenMessageText(message), timestamp: 0 });
409
383
  continue;
410
384
  }
@@ -447,15 +421,8 @@ async function dshToPiMessages(messages, ctx, options, imageMap, requestImages)
447
421
  return result;
448
422
  }
449
423
  function builtinResponsesModel(provider, modelId) {
450
- // Keep descriptor and wire capabilities within the same frozen catalog scope as route ownership.
451
- if (!wasPi086ResponsesModel(provider, modelId))
452
- return undefined;
453
- try {
454
- return getBuiltinModels(provider).find((model) => model.id === modelId && model.api === "openai-responses");
455
- }
456
- catch {
457
- return undefined;
458
- }
424
+ // The exact bundled catalog entry (see `resolveCatalogModel`): the same descriptor the route resolution reads.
425
+ return resolveCatalogModel(provider, modelId).descriptor;
459
426
  }
460
427
  export function resolvePiResponsesModel(options) {
461
428
  const route = isObject(options.route) ? options.route : {};
@@ -486,8 +453,6 @@ export async function serializeDshMessages(messages, ctx, options = {}) {
486
453
  route: options.route ?? {}, model: options.model, responsesCompat: options.responsesCompat,
487
454
  systemPrompt: typeof options.systemPrompt === "string" ? options.systemPrompt : undefined,
488
455
  includeSystemPrompt: options.includeSystemPrompt === true, onReplayDegrade: options.onReplayDegrade,
489
- inHistorySystem: options.systemPromptUpdate === "in-history",
490
- snapshotMarker: options.systemPromptUpdate === "in-history" ? `\u0000lcx-prompt-snapshot:${globalThis.crypto.randomUUID()}` : undefined,
491
456
  maxRequestImageBytes: positiveSafeInteger(options.maxRequestImageBytes, DEFAULT_MAX_REQUEST_IMAGE_BYTES),
492
457
  requestImagePixelBudget: positiveSafeInteger(options.requestImagePixelBudget, DEFAULT_REQUEST_IMAGE_PIXEL_BUDGET),
493
458
  requestImageMaxBytes: positiveSafeInteger(options.requestImageMaxBytes, DEFAULT_REQUEST_IMAGE_MAX_BYTES),
@@ -526,50 +491,12 @@ export async function serializeDshMessages(messages, ctx, options = {}) {
526
491
  if (tool.name)
527
492
  immediateTools.set(tool.name, tool);
528
493
  const toolOptions = { supportsStrictMode, supportsOpenAIGrammarTools, toolSearchResult: false };
529
- const normalizedContext = normalizeContext(dshContext);
530
- // Pi treats whichever system message is first in the transcript as the leading prompt: it skips it when
531
- // includeSystemPrompt is false and renders it without the update framing. A serialization batch carrying
532
- // no caller-owned head (no systemPrompt/tools) must never promote a history update to that slot, so an
533
- // empty placeholder holds the leading position and renders nothing.
534
- const piContext = normalized.inHistorySystem && normalizedContext.messages[0]?.role === "system" && normalizedContext.messages === dshContext.messages
535
- ? { ...normalizedContext, messages: [{ role: "system", content: "", timestamp: 0 }, ...normalizedContext.messages] }
536
- : normalizedContext;
494
+ const piContext = normalizeContext(dshContext);
537
495
  const piModel = model;
538
- // Pi collapses every system message into the leading one unless the transport accepts mid-conversation
539
- // system messages, which would drop or reorder in-history updates in batched serialization.
540
- const convertedInput = convertResponsesMessages(piModel, piContext, new Set(["openai", "openai-codex", "opencode"]), { includeSystemPrompt: normalized.includeSystemPrompt, grammarToolInputProperties, supportsAdditionalTools, supportsToolSearch, supportsMidConvoSystemMessages: normalized.inHistorySystem, toolOptions });
541
- const supersededInstructionItems = [];
542
- const unmarkedInput = [];
543
- let markersFound = 0;
544
- for (let position = 0; position < convertedInput.length; position += 1) {
545
- const item = convertedInput[position];
546
- if (normalized.snapshotMarker !== undefined && isMarkerItem(item, normalized.snapshotMarker)) {
547
- markersFound += 1;
548
- const rendered = convertedInput[position + 1];
549
- if (!isObject(rendered) || (rendered.role !== "developer" && rendered.role !== "system"))
550
- throw error("LCX could not trace a rendered system prompt snapshot", "LCX_RESPONSES_INVALID_INPUT");
551
- supersededInstructionItems.push(unmarkedInput.length);
552
- continue;
553
- }
554
- unmarkedInput.push(item);
555
- }
556
- if (normalized.snapshotMarker !== undefined) {
557
- // Markers are inserted only before non-empty snapshots (empty ones render nothing), so each inserted
558
- // marker must be found exactly once and none may survive in the output, even if a future Pi merged
559
- // adjacent user messages.
560
- const inserted = dshContext.messages.filter((message) => message.role === "user" && message.content === normalized.snapshotMarker).length;
561
- const nonce = normalized.snapshotMarker.slice(normalized.snapshotMarker.lastIndexOf(":") + 1);
562
- if (markersFound !== inserted || JSON.stringify(unmarkedInput).includes(nonce))
563
- throw error("LCX could not remove a system prompt snapshot marker", "LCX_RESPONSES_INVALID_INPUT");
564
- }
565
- const input = restorePortableAssistantPhases(unmarkedInput, dshContext.messages, { provider: model.provider, id: model.id, api: model.api });
496
+ const convertedInput = convertResponsesMessages(piModel, piContext, new Set(["openai", "openai-codex", "opencode"]), { includeSystemPrompt: normalized.includeSystemPrompt, grammarToolInputProperties, supportsAdditionalTools, supportsToolSearch, toolOptions });
497
+ const input = restorePortableAssistantPhases(convertedInput, dshContext.messages, { provider: model.provider, id: model.id, api: model.api });
566
498
  const tools = options.tools === undefined ? undefined : convertResponsesTools([...immediateTools.values()], toolOptions);
567
- return { input, supersededInstructionItems, imageMap, tools, model: piModel, grammarToolInputProperties, deferredToolsMode };
568
- }
569
- function isMarkerItem(item, marker) {
570
- if (!isObject(item) || item.role !== "user" || !Array.isArray(item.content))
571
- return false;
572
- return item.content.length === 1 && isObject(item.content[0]) && item.content[0].text === marker;
499
+ return { input, imageMap, tools, model: piModel, grammarToolInputProperties, deferredToolsMode };
573
500
  }
574
501
  export function responsesTools(tools) {
575
502
  if (tools === undefined)
@@ -628,6 +555,57 @@ async function hydratePart(part, ctx, options) {
628
555
  return { type: "input_text", text: "[image omitted because the target model does not support image input]" };
629
556
  return imagePart({ attachment }, ctx, options, options.imageMap);
630
557
  }
558
+ /** Base64 payload length of every `input_image` data URL in Responses input items, in order. */
559
+ export function inputImageBase64Lengths(input) {
560
+ const lengths = [];
561
+ const visit = (value) => {
562
+ if (Array.isArray(value)) {
563
+ for (const child of value)
564
+ visit(child);
565
+ return;
566
+ }
567
+ if (!isObject(value))
568
+ return;
569
+ if (value.type === "input_image" && typeof value.image_url === "string") {
570
+ const url = value.image_url;
571
+ const comma = url.indexOf(",");
572
+ lengths.push(url.startsWith("data:") && comma >= 0 ? url.length - comma - 1 : 0);
573
+ return;
574
+ }
575
+ for (const child of Object.values(value))
576
+ visit(child);
577
+ };
578
+ visit(input);
579
+ return lengths;
580
+ }
581
+ /**
582
+ * Enforce the request image bound over the whole assembled request. Each serialization batch is
583
+ * checked on its own, but images a native checkpoint hydrates (and portable replay re-renders) are
584
+ * not DSH surface images and arrive in other batches, so only the sum shows the real request size.
585
+ * DSH can offload only surface images, oldest first: when offloading enough of them fits the request
586
+ * the route asks for exactly that many; when even all of them cannot, no retry can help and the
587
+ * checkpoint's own images are the reason.
588
+ * @param options.fixedBytes base64 length of images DSH cannot offload (checkpoint / replay)
589
+ * @param options.surfaceBytes base64 length of each offloadable image, oldest first
590
+ */
591
+ export function assertAggregateImageBudget(options) {
592
+ const total = options.surfaceBytes.reduce((sum, bytes) => sum + bytes, options.fixedBytes);
593
+ if (total <= options.maxBytes)
594
+ return;
595
+ const excess = total - options.maxBytes;
596
+ let removed = 0;
597
+ let count = 0;
598
+ for (const bytes of options.surfaceBytes) {
599
+ if (removed >= excess)
600
+ break;
601
+ removed += bytes;
602
+ count += 1;
603
+ }
604
+ if (removed >= excess) {
605
+ 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 });
606
+ }
607
+ 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");
608
+ }
631
609
  export async function hydrateNativeImageReferences(output, ctx, options = {}) {
632
610
  const normalized = {
633
611
  imageSupport: options.imageSupport === "supported" || options.imageSupport === "unsupported" ? options.imageSupport : "unknown",