dsh-lcx-codex 0.5.0 → 0.5.1
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 +24 -12
- package/lib/auxiliary-usage.js +45 -6
- package/lib/client.js +9 -7
- package/lib/dsh-compat.js +17 -0
- package/lib/dsh-responses.js +69 -8
- package/lib/index.js +82 -6
- package/lib/native-checkpoint.js +15 -10
- package/lib/responses-request.js +4 -1
- package/lib/responses-stream.js +154 -7
- package/lib/route.js +31 -13
- package/lib/search-usage.js +12 -5
- package/lib/transport.js +8 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -37,17 +37,27 @@ LCX Codex 是 [DeepSeek Harness](https://github.com/deepseek-ai/DeepSeek-Harness
|
|
|
37
37
|
|
|
38
38
|
| 你的 DSH 版本 | 安装的 LCX 版本 | 安装命令中的标签 |
|
|
39
39
|
| --- | --- | --- |
|
|
40
|
-
| `0.2.
|
|
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.1`(稳定版) | `@latest` |
|
|
41
42
|
| `0.1.6` 系列(`0.1.6-alpha.2` 及以后) | `0.4.4-pre.1`(旧预发布版) | `@0.4.4-pre.1` |
|
|
42
43
|
| `0.1.5` 系列(`0.1.5-rc.1` 及以后) | `0.4.3`(稳定版) | `@0.4.3` |
|
|
43
44
|
|
|
44
|
-
|
|
45
|
+
请务必按表格选择:几个版本互不兼容,装错版本时安装命令可能只给出依赖警告,但插件可能无法正常工作。`@latest` 是稳定版 `0.5.1`(发布基线 DSH `0.2.0-rc.2`,host / plugin Pi `0.87.1`;相对 `0.5.0` 只包含缺陷修复),DSH `0.1.5` 用户请固定安装 `@0.4.3`。
|
|
45
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 版本受支持。
|
|
48
|
+
|
|
49
|
+
> 注意:稳定版 `0.5.1` 的安装范围写作 `>=0.2.0-rc.2 <0.2.1`,按 DSH 的版本范围规则,`0.2.1` 的预发布版(如 `0.2.1-alpha.2`)也可能被放行安装,但这些版本**不在** `0.5.1` 的支持和验证范围内;DSH `0.2.1-alpha.2` 及以后请使用 `@prelatest`。
|
|
47
50
|
|
|
48
51
|
### 2. 安装并启动
|
|
49
52
|
|
|
50
|
-
DSH `0.2.
|
|
53
|
+
DSH `0.2.1-alpha.2` 及以后(预发布版):
|
|
54
|
+
|
|
55
|
+
```sh
|
|
56
|
+
dsh plugin --profile web add dsh-lcx-codex@prelatest
|
|
57
|
+
dsh web
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
DSH `0.2.0` 系列(稳定版):
|
|
51
61
|
|
|
52
62
|
```sh
|
|
53
63
|
dsh plugin --profile web add dsh-lcx-codex@latest
|
|
@@ -114,10 +124,11 @@ dsh web
|
|
|
114
124
|
|
|
115
125
|
## 从旧版本升级
|
|
116
126
|
|
|
117
|
-
- **DSH 升级到 `0.2.
|
|
127
|
+
- **DSH 升级到 `0.2.1-alpha.2` 或更新版本**:把插件换成 `@prelatest`(`0.5.1-pre.1`),稳定版 `0.5.1` 没有在这些 DSH 版本上验证。
|
|
128
|
+
- **DSH 升级到 `0.2.0` 系列**:把插件换成 `@latest`(`0.5.1`),旧版 `0.4.x` 不支持 DSH `0.2`。
|
|
118
129
|
- **DSH `0.1.6` 系列**:继续使用 `@0.4.4-pre.1`。
|
|
119
|
-
- **DSH `0.1.5` 系列**:继续使用 `@0.4.3`;`@latest` 现在指向支持 DSH `0.2` 的 `0.5.
|
|
120
|
-
- **已装了 `0.5.0-pre.1
|
|
130
|
+
- **DSH `0.1.5` 系列**:继续使用 `@0.4.3`;`@latest` 现在指向支持 DSH `0.2` 的 `0.5.1`。
|
|
131
|
+
- **已装了 `0.5.0` 或 `0.5.0-pre.1`**:DSH `0.2.0` 系列重新运行 `@latest` 的 `add` 命令即可升到 `0.5.1`(修复了若干缺陷,安装范围不变,详见 CHANGELOG);DSH `0.2.1-alpha.2` 及以后用 `@prelatest`。
|
|
121
132
|
- **升级后建议新建会话**:旧版本保存的压缩状态不保证兼容。DSH `0.2` 迁移过的旧长会话如果带有旧版 LCX 压缩检查点,LCX 会提示 `LCX_CHECKPOINT_UNSUPPORTED`,新建会话即可;其他模型不受影响。
|
|
122
133
|
|
|
123
134
|
## 停用与卸载
|
|
@@ -133,11 +144,12 @@ dsh web
|
|
|
133
144
|
|
|
134
145
|
- **网关能力因人而异**:搜索、压缩、Alpha 都依赖对应的服务端能力,不同网关支持程度不同。
|
|
135
146
|
- **Grok**:支持 API Key / API 网关路由,不支持 xAI OAuth / SuperGrok 登录。原生 X Search 目前只在 `grok-4.6` 上验证过。
|
|
136
|
-
- **Grok 长对话压力**:xAI
|
|
147
|
+
- **Grok 长对话压力**:xAI 的智能体调用会把多次内部请求的用量累加计费。LCX 在服务端返回完整的实时上下文数据时,用它来判断何时压缩,缺失时退回估算;计费数字本身保持不变。
|
|
137
148
|
- **Alpha 仍是实验功能**:连续打开/查找页面在已测试的 OAuth 类上游上可用;API Key 类上游账号不在支持范围内。目标页面抓取失败时会提示 `LCX_ALPHA_PAGE_FETCH_FAILED`,模型可以换一个结果继续。
|
|
138
149
|
- **Markdown 图片换行**:为避免 DSH `0.2` 中图片后的文字挤到图片右侧,LCX 会让回答里的 Markdown 图片单独占一行,这一点与“搜索媒体预览”开关无关;句中的小图也会因此换行。
|
|
139
150
|
- **DSH `0.1.5` + LCX `0.4.3`**:在插件设置中启用或停用 LCX 后,已打开的浏览器页面可能需要刷新。
|
|
140
|
-
-
|
|
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.1` 的发布基线为 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`。同一安装范围内其他后续版本仍需单独评估,遇到问题欢迎反馈。
|
|
141
153
|
|
|
142
154
|
## 常见问题
|
|
143
155
|
|
|
@@ -151,10 +163,10 @@ dsh web
|
|
|
151
163
|
通常是 DSH 版本和插件版本不匹配,按[上面的表格](#1-按-dsh-版本选择插件版本)重新选择。
|
|
152
164
|
|
|
153
165
|
**升级到 DSH 0.2 后插件“导入失败”(failed to import)?**
|
|
154
|
-
从较早版本一路升级上来的配置目录里可能残留旧的依赖包。先停止 `dsh web`,把 DSH 主目录(默认 `~/.dsh`,或 `DSH_HOME` 指定的目录)下 `profiles/web/` 里的 `node_modules` 和 `pnpm-lock.yaml`
|
|
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`。
|
|
155
167
|
|
|
156
|
-
**刚发布新版时 `@latest` 装到了旧版本?**
|
|
157
|
-
包管理器的缓存可能还没刷新,DSH 会因版本不兼容拒绝安装。稍等几分钟重试,或直接指定版本号,例如 `dsh-lcx-codex@0.5.0`。
|
|
168
|
+
**刚发布新版时 `@latest` / `@prelatest` 装到了旧版本?**
|
|
169
|
+
包管理器的缓存可能还没刷新,DSH 会因版本不兼容拒绝安装。稍等几分钟重试,或直接指定版本号,例如 `dsh-lcx-codex@0.5.1-pre.1` 或 `dsh-lcx-codex@0.5.1`。
|
|
158
170
|
|
|
159
171
|
## 反馈问题
|
|
160
172
|
|
package/lib/auxiliary-usage.js
CHANGED
|
@@ -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
|
|
13
|
-
|
|
14
|
-
|
|
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:
|
|
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
|
-
|
|
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
|
|
249
|
+
const found = [];
|
|
250
250
|
visible = collectInlineLinkDestinations(
|
|
251
251
|
visible,
|
|
252
|
-
(value) =>
|
|
252
|
+
(value, position) => found.push({ value, position })
|
|
253
253
|
);
|
|
254
254
|
visible.replace(
|
|
255
255
|
REFERENCE_LINK,
|
|
256
|
-
(_match,
|
|
256
|
+
(_match, prefix, label, explicit, offset) => {
|
|
257
257
|
const value = definitions.get(referenceLabel(explicit === "" ? label : explicit));
|
|
258
|
-
if (value !== void 0)
|
|
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))
|
|
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) {
|
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;
|
package/lib/dsh-responses.js
CHANGED
|
@@ -72,23 +72,33 @@ function imageAccess(ctx, attachments, ref) {
|
|
|
72
72
|
: () => undefined;
|
|
73
73
|
return attachments.resolveAccess(mapper, ref);
|
|
74
74
|
}
|
|
75
|
-
|
|
75
|
+
/** One adapter-owned lookup supplies the model facts a route needs: image support and reasoning efforts. */
|
|
76
|
+
export async function resolveRouteModelFacts(ctx, route, signal) {
|
|
76
77
|
const llm = ctx?.llm;
|
|
77
78
|
if (llm === undefined)
|
|
78
|
-
return "unknown";
|
|
79
|
+
return { imageSupport: "unknown", reasoning: undefined, resolved: false };
|
|
79
80
|
try {
|
|
80
81
|
const info = await llm.resolveModelInfo(stringValue(route.provider), stringValue(route.model), signal);
|
|
81
82
|
const modalities = info.inputModalities;
|
|
82
|
-
return
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
83
|
+
return {
|
|
84
|
+
imageSupport: modalities?.includes("image")
|
|
85
|
+
? "supported"
|
|
86
|
+
: modalities !== undefined
|
|
87
|
+
? "unsupported"
|
|
88
|
+
: "unknown",
|
|
89
|
+
reasoning: info.reasoning === undefined
|
|
90
|
+
? undefined
|
|
91
|
+
: { efforts: info.reasoning.efforts.map((effort) => String(effort.id)) },
|
|
92
|
+
resolved: true,
|
|
93
|
+
};
|
|
87
94
|
}
|
|
88
95
|
catch {
|
|
89
|
-
return "unknown";
|
|
96
|
+
return { imageSupport: "unknown", reasoning: undefined, resolved: false };
|
|
90
97
|
}
|
|
91
98
|
}
|
|
99
|
+
export async function resolveModelImageSupport(ctx, route, signal) {
|
|
100
|
+
return (await resolveRouteModelFacts(ctx, route, signal)).imageSupport;
|
|
101
|
+
}
|
|
92
102
|
function attachmentResolver(ctx, options, requestImages) {
|
|
93
103
|
const attachments = attachmentServices(ctx?.attachments);
|
|
94
104
|
return async (block, signal) => {
|
|
@@ -551,6 +561,57 @@ async function hydratePart(part, ctx, options) {
|
|
|
551
561
|
return { type: "input_text", text: "[image omitted because the target model does not support image input]" };
|
|
552
562
|
return imagePart({ attachment }, ctx, options, options.imageMap);
|
|
553
563
|
}
|
|
564
|
+
/** Base64 payload length of every `input_image` data URL in Responses input items, in order. */
|
|
565
|
+
export function inputImageBase64Lengths(input) {
|
|
566
|
+
const lengths = [];
|
|
567
|
+
const visit = (value) => {
|
|
568
|
+
if (Array.isArray(value)) {
|
|
569
|
+
for (const child of value)
|
|
570
|
+
visit(child);
|
|
571
|
+
return;
|
|
572
|
+
}
|
|
573
|
+
if (!isObject(value))
|
|
574
|
+
return;
|
|
575
|
+
if (value.type === "input_image" && typeof value.image_url === "string") {
|
|
576
|
+
const url = value.image_url;
|
|
577
|
+
const comma = url.indexOf(",");
|
|
578
|
+
lengths.push(url.startsWith("data:") && comma >= 0 ? url.length - comma - 1 : 0);
|
|
579
|
+
return;
|
|
580
|
+
}
|
|
581
|
+
for (const child of Object.values(value))
|
|
582
|
+
visit(child);
|
|
583
|
+
};
|
|
584
|
+
visit(input);
|
|
585
|
+
return lengths;
|
|
586
|
+
}
|
|
587
|
+
/**
|
|
588
|
+
* Enforce the request image bound over the whole assembled request. Each serialization batch is
|
|
589
|
+
* checked on its own, but images a native checkpoint hydrates (and portable replay re-renders) are
|
|
590
|
+
* not DSH surface images and arrive in other batches, so only the sum shows the real request size.
|
|
591
|
+
* DSH can offload only surface images, oldest first: when offloading enough of them fits the request
|
|
592
|
+
* the route asks for exactly that many; when even all of them cannot, no retry can help and the
|
|
593
|
+
* checkpoint's own images are the reason.
|
|
594
|
+
* @param options.fixedBytes base64 length of images DSH cannot offload (checkpoint / replay)
|
|
595
|
+
* @param options.surfaceBytes base64 length of each offloadable image, oldest first
|
|
596
|
+
*/
|
|
597
|
+
export function assertAggregateImageBudget(options) {
|
|
598
|
+
const total = options.surfaceBytes.reduce((sum, bytes) => sum + bytes, options.fixedBytes);
|
|
599
|
+
if (total <= options.maxBytes)
|
|
600
|
+
return;
|
|
601
|
+
const excess = total - options.maxBytes;
|
|
602
|
+
let removed = 0;
|
|
603
|
+
let count = 0;
|
|
604
|
+
for (const bytes of options.surfaceBytes) {
|
|
605
|
+
if (removed >= excess)
|
|
606
|
+
break;
|
|
607
|
+
removed += bytes;
|
|
608
|
+
count += 1;
|
|
609
|
+
}
|
|
610
|
+
if (removed >= excess) {
|
|
611
|
+
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 });
|
|
612
|
+
}
|
|
613
|
+
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");
|
|
614
|
+
}
|
|
554
615
|
export async function hydrateNativeImageReferences(output, ctx, options = {}) {
|
|
555
616
|
const normalized = {
|
|
556
617
|
imageSupport: options.imageSupport === "supported" || options.imageSupport === "unsupported" ? options.imageSupport : "unknown",
|
package/lib/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import z from "@deepseek-ai/schemastery";
|
|
2
|
-
import { recordHostedUsage, withAuxiliaryUsage } from "./auxiliary-usage.js";
|
|
2
|
+
import { failedAuxiliaryUsageListener, recordHostedUsage, withAuxiliaryUsage } from "./auxiliary-usage.js";
|
|
3
3
|
import { installSearchUsage } from "./search-usage.js";
|
|
4
4
|
import { projectToolUpdates } from "@deepseek-ai/dsh-llm";
|
|
5
5
|
import { WEB_SEARCH_MAX_QUERIES, applyWebSearchTool, } from "@deepseek-ai/dsh-tool-web";
|
|
@@ -12,10 +12,10 @@ import { join } from "node:path";
|
|
|
12
12
|
import { AsyncLocalStorage } from "node:async_hooks";
|
|
13
13
|
import { getSupportedThinkingLevels } from "./pi-responses-runtime.js";
|
|
14
14
|
import { fetchJsonWithRetry } from "./transport.js";
|
|
15
|
-
import { agentSessionId, agentUsesSession, compactionConfigState, compactionPatchCandidate, installCompactionPatch, patchCompactionConfig, patchToolResultPruner, readAgentRouteState, readSelectedAgentRouteState, recordAgentRouteEvent, resolveAgentService, resolveContextService, resolveScopedService, restoreCompactionConfig, restoreCompactionPatches, restoreToolResultPruner, sessionFor, sessionFromAgent, scopedToolRuntime, tokenMeterTotal, toolResultPrunerState, } from "./dsh-compat.js";
|
|
15
|
+
import { agentSessionId, agentUsesSession, compactionConfigState, compactionPatchCandidate, installCompactionPatch, patchCompactionConfig, patchToolResultPruner, readAgentRouteState, readSelectedAgentRouteState, recordAgentRouteEvent, resolveAgentService, resolveContextService, resolveScopedService, restoreCompactionConfig, restoreCompactionPatches, restoreToolResultPruner, sessionFor, sessionFromAgent, scopedToolRuntime, onScopedToolExecute, tokenMeterTotal, toolResultPrunerState, } from "./dsh-compat.js";
|
|
16
16
|
import { InvocationPolicyScope } from "./invocation-policy-scope.js";
|
|
17
17
|
import { authenticatedGrokHeaders, authenticatedHeaders, currentRoute, generationControlsFromSession, grokPromptCacheSessionId, promptCacheKey, promptCacheRetention, promptCacheSessionId, resolveGrokResponsesRouteConfig, resolveResponsesRouteConfig, routeFingerprint, } from "./route.js";
|
|
18
|
-
import { DEFAULT_MAX_REQUEST_IMAGE_BYTES, DEFAULT_REQUEST_IMAGE_PIXEL_BUDGET, DEFAULT_REQUEST_IMAGE_MAX_BYTES, hydrateNativeImageReferences,
|
|
18
|
+
import { DEFAULT_MAX_REQUEST_IMAGE_BYTES, DEFAULT_REQUEST_IMAGE_PIXEL_BUDGET, DEFAULT_REQUEST_IMAGE_MAX_BYTES, assertAggregateImageBudget, hydrateNativeImageReferences, inputImageBase64Lengths, resolveRouteModelFacts, responseInputItems, serializeDshMessages, } from "./dsh-responses.js";
|
|
19
19
|
import { mergeFeatureHeader, requestNativeCompaction } from "./compact-v2.js";
|
|
20
20
|
import { buildResponsesBody } from "./responses-request.js";
|
|
21
21
|
import { managedFailureChunk, streamResponsesRequest, } from "./responses-stream.js";
|
|
@@ -346,6 +346,11 @@ function syncGptToolsForAgent(ctx, agent, state, getConfig, provider, registrati
|
|
|
346
346
|
}
|
|
347
347
|
if (advanced)
|
|
348
348
|
disposers.push(scopedTools.register(createAdvancedHostedTool(ctx, state, getConfig)));
|
|
349
|
+
// A search that was billed and then failed keeps its usage: only the Agent-scoped around-dispatch
|
|
350
|
+
// seam may attach `meta` to a failed tool result.
|
|
351
|
+
const disposeBilling = onScopedToolExecute(agent, failedAuxiliaryUsageListener);
|
|
352
|
+
if (disposeBilling)
|
|
353
|
+
disposers.push(disposeBilling);
|
|
349
354
|
registrations.set(agent, {
|
|
350
355
|
fingerprint,
|
|
351
356
|
dispose() {
|
|
@@ -544,9 +549,35 @@ function mergeMap(target, source) {
|
|
|
544
549
|
for (const [key, value] of source ?? [])
|
|
545
550
|
target.set(key, value);
|
|
546
551
|
}
|
|
552
|
+
/**
|
|
553
|
+
* The reasoning projection a GPT route serializes and sends with. A profile's declared
|
|
554
|
+
* `reasoningEfforts` wins (wire mapping, or `false` for a non-reasoning model); otherwise the catalog
|
|
555
|
+
* entry stands, and a model with no catalog entry follows what DSH itself resolved for it instead of
|
|
556
|
+
* being assumed to reason.
|
|
557
|
+
*/
|
|
558
|
+
function gptModelOverride(routeConfig, facts) {
|
|
559
|
+
if (routeConfig.modelControls !== undefined)
|
|
560
|
+
return { ...routeConfig.modelControls };
|
|
561
|
+
if (!facts.resolved)
|
|
562
|
+
return undefined;
|
|
563
|
+
return { reasoning: facts.reasoning !== undefined };
|
|
564
|
+
}
|
|
565
|
+
/** Refuse an effort DSH does not offer for this model, the way the DSH adapter and the Grok route do. */
|
|
566
|
+
function assertSupportedReasoningEffort(effort, facts) {
|
|
567
|
+
if (effort === undefined || !facts.resolved)
|
|
568
|
+
return;
|
|
569
|
+
const supported = facts.reasoning === undefined ? ["off"] : facts.reasoning.efforts;
|
|
570
|
+
if (supported.includes(String(effort)))
|
|
571
|
+
return;
|
|
572
|
+
throw Object.assign(new Error(`Responses route does not support reasoning effort "${String(effort)}"`), { code: "LCX_RESPONSES_UNSUPPORTED_OPTION" });
|
|
573
|
+
}
|
|
547
574
|
async function serializeNativeAware(messages, route, routeConfig, ctx, options) {
|
|
548
575
|
const session = sessionFor(ctx, route.sessionId);
|
|
549
|
-
const
|
|
576
|
+
const modelFacts = await resolveRouteModelFacts(ctx, route, options.signal);
|
|
577
|
+
const { imageSupport } = modelFacts;
|
|
578
|
+
const modelOverride = options.grokNativeReplayRoute === undefined
|
|
579
|
+
? gptModelOverride(routeConfig, modelFacts)
|
|
580
|
+
: undefined;
|
|
550
581
|
const input = [];
|
|
551
582
|
const imageMap = new Map();
|
|
552
583
|
let nativeTools;
|
|
@@ -557,6 +588,7 @@ async function serializeNativeAware(messages, route, routeConfig, ctx, options)
|
|
|
557
588
|
route,
|
|
558
589
|
tools: options.tools,
|
|
559
590
|
responsesCompat: routeConfig.responsesCompat,
|
|
591
|
+
...(modelOverride === undefined ? {} : { model: modelOverride }),
|
|
560
592
|
...extra,
|
|
561
593
|
});
|
|
562
594
|
const firstMessage = messages[0];
|
|
@@ -575,12 +607,17 @@ async function serializeNativeAware(messages, route, routeConfig, ctx, options)
|
|
|
575
607
|
nativeTools = prelude.tools;
|
|
576
608
|
nativeModel = prelude.model;
|
|
577
609
|
grammarToolInputProperties = prelude.grammarToolInputProperties;
|
|
610
|
+
/** Base64 length of images DSH cannot offload (hydrated checkpoint, portable replay). */
|
|
611
|
+
let fixedImageBytes = 0;
|
|
612
|
+
/** Base64 length of each offloadable surface image, oldest first, across all batches. */
|
|
613
|
+
const surfaceImageBytes = [];
|
|
578
614
|
const flush = async () => {
|
|
579
615
|
if (!normal.length)
|
|
580
616
|
return;
|
|
581
617
|
const batch = normal;
|
|
582
618
|
normal = [];
|
|
583
619
|
const serialized = await serializeDshMessages(batch, ctx, serializeOptions());
|
|
620
|
+
surfaceImageBytes.push(...inputImageBase64Lengths(serialized.input));
|
|
584
621
|
nativeTools = serialized.tools ?? nativeTools;
|
|
585
622
|
nativeModel = serialized.model ?? nativeModel;
|
|
586
623
|
grammarToolInputProperties =
|
|
@@ -604,12 +641,17 @@ async function serializeNativeAware(messages, route, routeConfig, ctx, options)
|
|
|
604
641
|
const nativeOutput = state.nativeOutput;
|
|
605
642
|
const hydratedMap = new Map();
|
|
606
643
|
const hydrated = await hydrateNativeImageReferences(nativeOutput, ctx, requestImageOptions(routeConfig, imageSupport, options.signal, hydratedMap));
|
|
607
|
-
|
|
644
|
+
const hydratedItems = responseInputItems(hydrated);
|
|
645
|
+
for (const bytes of inputImageBase64Lengths(hydratedItems))
|
|
646
|
+
fixedImageBytes += bytes;
|
|
647
|
+
input.push(...hydratedItems);
|
|
608
648
|
mergeMap(imageMap, hydratedMap);
|
|
609
649
|
}
|
|
610
650
|
else {
|
|
611
651
|
const portable = await portableMessagesForCheckpoint(ctx, session, checkpointId, { maxChars: routeConfig.portableReplayMaxChars });
|
|
612
652
|
const serialized = await serializeDshMessages(portable, ctx, serializeOptions());
|
|
653
|
+
for (const bytes of inputImageBase64Lengths(serialized.input))
|
|
654
|
+
fixedImageBytes += bytes;
|
|
613
655
|
input.push(...serialized.input);
|
|
614
656
|
mergeMap(imageMap, serialized.imageMap);
|
|
615
657
|
}
|
|
@@ -618,6 +660,13 @@ async function serializeNativeAware(messages, route, routeConfig, ctx, options)
|
|
|
618
660
|
normal.push(message);
|
|
619
661
|
}
|
|
620
662
|
await flush();
|
|
663
|
+
assertAggregateImageBudget({
|
|
664
|
+
maxBytes: Number.isSafeInteger(routeConfig.maxRequestImageBytes) && Number(routeConfig.maxRequestImageBytes) > 0
|
|
665
|
+
? Number(routeConfig.maxRequestImageBytes)
|
|
666
|
+
: DEFAULT_MAX_REQUEST_IMAGE_BYTES,
|
|
667
|
+
fixedBytes: fixedImageBytes,
|
|
668
|
+
surfaceBytes: surfaceImageBytes,
|
|
669
|
+
});
|
|
621
670
|
if (!nativeModel)
|
|
622
671
|
throw Object.assign(new Error("LCX could not resolve the Pi Responses model descriptor"), { code: "LCX_RESPONSES_MODEL_UNAVAILABLE" });
|
|
623
672
|
return {
|
|
@@ -628,6 +677,7 @@ async function serializeNativeAware(messages, route, routeConfig, ctx, options)
|
|
|
628
677
|
tools: nativeTools,
|
|
629
678
|
model: nativeModel,
|
|
630
679
|
grammarToolInputProperties,
|
|
680
|
+
modelFacts,
|
|
631
681
|
};
|
|
632
682
|
}
|
|
633
683
|
function fallbackEligible(error, signal) {
|
|
@@ -915,6 +965,28 @@ async function messagesContainNativeCheckpoint(ctx, messages, session) {
|
|
|
915
965
|
function inputHasNativeState(input) {
|
|
916
966
|
return input.some((item) => isRecord(item) && item.type === "compaction");
|
|
917
967
|
}
|
|
968
|
+
/**
|
|
969
|
+
* One redacted line per failed provider request. Only the LCX class and machine-safe provider
|
|
970
|
+
* identifiers are logged: never the provider message, a payload, a header or a credential.
|
|
971
|
+
* `warn` matches the compaction failure line; DSH ships no persistent plugin-logger exporter by default
|
|
972
|
+
* (see ERRORS.md), so the line is visible wherever the host attaches one.
|
|
973
|
+
*/
|
|
974
|
+
function logStreamFailure(ctx, route, info) {
|
|
975
|
+
const parts = [
|
|
976
|
+
`route=${route}`,
|
|
977
|
+
`code=${info.code}`,
|
|
978
|
+
info.status === undefined ? "" : `status=${info.status}`,
|
|
979
|
+
info.requestId === undefined ? "" : `requestId=${info.requestId}`,
|
|
980
|
+
info.event === undefined ? "" : `event=${info.event}`,
|
|
981
|
+
info.providerCode === undefined ? "" : `providerCode=${info.providerCode}`,
|
|
982
|
+
info.providerType === undefined ? "" : `providerType=${info.providerType}`,
|
|
983
|
+
info.incompleteReason === undefined
|
|
984
|
+
? ""
|
|
985
|
+
: `incompleteReason=${info.incompleteReason}`,
|
|
986
|
+
`emitted=${info.contentEmitted}`,
|
|
987
|
+
].filter(Boolean);
|
|
988
|
+
ctx.logger?.warn?.(`[lcx-codex] responses stream failed: ${parts.join(" ")}`);
|
|
989
|
+
}
|
|
918
990
|
async function* managedResponsesStream(options, routeConfig, ctx) {
|
|
919
991
|
const route = currentRoute(options, routeConfig);
|
|
920
992
|
try {
|
|
@@ -922,6 +994,7 @@ async function* managedResponsesStream(options, routeConfig, ctx) {
|
|
|
922
994
|
throw Object.assign(new Error("LCX Responses does not support GenerateOptions.stop"), { code: "LCX_RESPONSES_UNSUPPORTED_OPTION" });
|
|
923
995
|
const projected = projectToolUpdates(options.messages, options.tools, undefined, options.toolHistory);
|
|
924
996
|
const prepared = await serializeNativeAware(projected.messages, route, routeConfig, ctx, { signal: options.signal, tools: projected.tools && [...projected.tools] });
|
|
997
|
+
assertSupportedReasoningEffort(options.reasoningEffort, prepared.modelFacts);
|
|
925
998
|
const cacheSessionId = promptCacheSessionId(route, routeConfig, ctx);
|
|
926
999
|
const headers = await authenticatedHeaders(ctx, routeConfig, cacheSessionId, cacheSessionId === undefined ? null : route.sessionId);
|
|
927
1000
|
const body = buildResponsesBody({
|
|
@@ -953,6 +1026,7 @@ async function* managedResponsesStream(options, routeConfig, ctx) {
|
|
|
953
1026
|
timeoutMs: routeConfig.timeoutMs,
|
|
954
1027
|
maxAttempts: 1,
|
|
955
1028
|
maxResponseBytes: routeConfig.maxResponseBytes,
|
|
1029
|
+
onFailure: (info) => logStreamFailure(ctx, "gpt", info),
|
|
956
1030
|
});
|
|
957
1031
|
}
|
|
958
1032
|
catch (error) {
|
|
@@ -986,7 +1060,8 @@ async function* managedGrokNativeSearchStream(options, routeConfig, nativeSearch
|
|
|
986
1060
|
};
|
|
987
1061
|
const effectiveReasoning = options.reasoningEffort ?? routeConfig.profileReasoning;
|
|
988
1062
|
if (effectiveReasoning !== undefined &&
|
|
989
|
-
!getSupportedThinkingLevels(model).some((level) => level === effectiveReasoning)
|
|
1063
|
+
!getSupportedThinkingLevels(model).some((level) => level === effectiveReasoning) &&
|
|
1064
|
+
!(effectiveReasoning === "off" && routeConfig.modelControls?.offOmitsReasoning === true))
|
|
990
1065
|
throw Object.assign(new Error(`Grok route does not support reasoning effort "${String(effectiveReasoning)}"`), { code: "LCX_RESPONSES_UNSUPPORTED_OPTION" });
|
|
991
1066
|
const body = buildResponsesBody({
|
|
992
1067
|
model,
|
|
@@ -1022,6 +1097,7 @@ async function* managedGrokNativeSearchStream(options, routeConfig, nativeSearch
|
|
|
1022
1097
|
isServerToolItem: (item) => isGrokNativeServerToolItem(item, nativeSearch, declaredToolNames),
|
|
1023
1098
|
declaredToolNames,
|
|
1024
1099
|
nativeReplayRoute,
|
|
1100
|
+
onFailure: (info) => logStreamFailure(ctx, "grok-native", info),
|
|
1025
1101
|
onServerToolUsage(usage) {
|
|
1026
1102
|
ctx.logger?.info?.(`[lcx-codex] Grok native search used ${usage.total} server-side tool call(s) (web=${usage.webSearchCalls}, x=${usage.xSearchCalls})`);
|
|
1027
1103
|
},
|
package/lib/native-checkpoint.js
CHANGED
|
@@ -665,26 +665,31 @@ function groupMessages(messages) {
|
|
|
665
665
|
.map((block) => String(block.id))
|
|
666
666
|
: [];
|
|
667
667
|
if (toolCalls.length === 0) {
|
|
668
|
+
// A tool result no call can claim (its call was not directly in front of it) cannot be replayed:
|
|
669
|
+
// upstream refuses a function_call_output without its call. Results that belong to a call are
|
|
670
|
+
// consumed with that call's group below and never reach this branch.
|
|
671
|
+
if (message.role === "tool")
|
|
672
|
+
continue;
|
|
668
673
|
groups.push([message]);
|
|
669
674
|
continue;
|
|
670
675
|
}
|
|
671
676
|
const group = [message];
|
|
672
677
|
const pending = new Set(toolCalls);
|
|
673
678
|
let cursor = index + 1;
|
|
674
|
-
|
|
679
|
+
// The run of tool messages right behind the call belongs to this group. Only the first result of
|
|
680
|
+
// each call it issued joins the group; a result for any other call, or a repeat, is not replayable
|
|
681
|
+
// (upstream refuses a function_call_output without its call) and is dropped. The whole run is
|
|
682
|
+
// consumed either way, so nothing in it is read again as a group of its own.
|
|
683
|
+
while (cursor < messages.length) {
|
|
675
684
|
const next = messages[cursor];
|
|
676
|
-
if (!next)
|
|
677
|
-
break;
|
|
678
|
-
const results = next.role === "tool" ? [String(next.toolCallId)] : [];
|
|
679
|
-
if (results.length === 0)
|
|
685
|
+
if (!next || next.role !== "tool")
|
|
680
686
|
break;
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
687
|
+
const id = String(next.toolCallId);
|
|
688
|
+
if (pending.delete(id))
|
|
689
|
+
group.push(next);
|
|
684
690
|
cursor += 1;
|
|
685
691
|
}
|
|
686
|
-
|
|
687
|
-
index = cursor - 1;
|
|
692
|
+
index = cursor - 1;
|
|
688
693
|
groups.push(group);
|
|
689
694
|
}
|
|
690
695
|
return groups;
|
package/lib/responses-request.js
CHANGED
|
@@ -38,7 +38,10 @@ export function responsesGenerationEnvelope({ model = "unknown", reasoningEffort
|
|
|
38
38
|
result.include = ["reasoning.encrypted_content"];
|
|
39
39
|
if (reasoningEffort !== undefined && reasoningEffort !== "off") {
|
|
40
40
|
const requested = String(reasoningEffort);
|
|
41
|
-
|
|
41
|
+
// Unmapped means "send the level as is"; an explicit null means the model declares the level
|
|
42
|
+
// unsupported, so nothing is sent for it (the DSH adapter refuses such a level before dispatch).
|
|
43
|
+
const mapped = descriptor.thinkingLevelMap?.[requested];
|
|
44
|
+
const wire = mapped === undefined ? requested : mapped;
|
|
42
45
|
if (wire !== null) {
|
|
43
46
|
result.reasoning = { effort: wire, summary: "auto" };
|
|
44
47
|
result.include = ["reasoning.encrypted_content"];
|
package/lib/responses-stream.js
CHANGED
|
@@ -73,12 +73,115 @@ function errorFacts(error) {
|
|
|
73
73
|
text: texts.join(" | "),
|
|
74
74
|
};
|
|
75
75
|
}
|
|
76
|
+
/** @param {unknown} value */
|
|
77
|
+
function safeMachineField(value) {
|
|
78
|
+
if (typeof value !== "string" || value.length === 0 || value.length > 96)
|
|
79
|
+
return undefined;
|
|
80
|
+
return /^[A-Za-z0-9_.:\[\]-]+$/u.test(value) ? value : undefined;
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Record the structured facts of a failure the provider reports in-stream. Pi collapses every such
|
|
84
|
+
* event into one `Error("<code>: <message>")` and `response.completed` with status `failed` into a
|
|
85
|
+
* bare "stream ended in failure", so the machine fields are captured here, before that happens.
|
|
86
|
+
*/
|
|
87
|
+
function observeUpstreamFailure(event, meta) {
|
|
88
|
+
const type = String(event.type ?? "");
|
|
89
|
+
let source;
|
|
90
|
+
let details;
|
|
91
|
+
let hasError = false;
|
|
92
|
+
let incomplete = false;
|
|
93
|
+
if (type === "error") {
|
|
94
|
+
// OpenAI sends flat `{code, message}`; some gateways nest `{error: {code, type}}`. `type` is only
|
|
95
|
+
// ever the error type when nested, because the flat `type` is the event name itself.
|
|
96
|
+
const nested = isObject(event.error) ? event.error : undefined;
|
|
97
|
+
source = { code: nested?.code ?? event.code, type: nested?.type };
|
|
98
|
+
hasError = true;
|
|
99
|
+
}
|
|
100
|
+
else if (isObject(event.response)) {
|
|
101
|
+
const response = event.response;
|
|
102
|
+
const status = response.status;
|
|
103
|
+
const failedTerminal = type === "response.failed" ||
|
|
104
|
+
(type === "response.completed" && status === "failed");
|
|
105
|
+
const incompleteTerminal = type === "response.incomplete" ||
|
|
106
|
+
(type === "response.completed" && status === "incomplete");
|
|
107
|
+
if (!failedTerminal && !incompleteTerminal)
|
|
108
|
+
return;
|
|
109
|
+
incomplete = incompleteTerminal;
|
|
110
|
+
details = isObject(response.incomplete_details)
|
|
111
|
+
? response.incomplete_details.reason
|
|
112
|
+
: undefined;
|
|
113
|
+
// max_output_tokens is a length stop that Pi maps to a normal finish, not a failure.
|
|
114
|
+
if (incompleteTerminal && details === "max_output_tokens")
|
|
115
|
+
return;
|
|
116
|
+
source = isObject(response.error) ? response.error : undefined;
|
|
117
|
+
hasError = source !== undefined;
|
|
118
|
+
}
|
|
119
|
+
else
|
|
120
|
+
return;
|
|
121
|
+
const code = safeMachineField(source?.code);
|
|
122
|
+
const errorType = safeMachineField(source?.type);
|
|
123
|
+
const incompleteReason = safeMachineField(details);
|
|
124
|
+
meta.upstreamFailure = {
|
|
125
|
+
event: type,
|
|
126
|
+
...(code === undefined ? {} : { code }),
|
|
127
|
+
...(errorType === undefined ? {} : { type: errorType }),
|
|
128
|
+
...(incompleteReason === undefined ? {} : { incompleteReason }),
|
|
129
|
+
hasError,
|
|
130
|
+
incomplete,
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
/** Closed mapping from provider machine identifiers to the DSH failure taxonomy; unknown identifiers return undefined. */
|
|
134
|
+
function classifyProviderIdentifier(value) {
|
|
135
|
+
if (value === undefined)
|
|
136
|
+
return undefined;
|
|
137
|
+
const id = value.toLowerCase();
|
|
138
|
+
if (id === "insufficient_quota" || id === "insufficient_user_quota")
|
|
139
|
+
return "INSUFFICIENT_QUOTA";
|
|
140
|
+
if (/^(?:invalid_api_key|authentication_error|permission_error)$/u.test(id))
|
|
141
|
+
return "AUTH";
|
|
142
|
+
if (/^rate_limit|^too_many_requests$/u.test(id))
|
|
143
|
+
return "RATE_LIMIT";
|
|
144
|
+
if (id === "context_length_exceeded")
|
|
145
|
+
return "CONTEXT_WINDOW_EXCEEDED";
|
|
146
|
+
if (id === "content_filter" ||
|
|
147
|
+
id === "content_policy_violation" ||
|
|
148
|
+
id.startsWith("invalid_"))
|
|
149
|
+
return "INVALID_REQUEST";
|
|
150
|
+
if (/^(?:server_error|internal_error|internal_server_error|api_error|service_unavailable|bad_response_status_code)$/u.test(id) ||
|
|
151
|
+
id.includes("overloaded"))
|
|
152
|
+
return "SERVER";
|
|
153
|
+
return undefined;
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* Failure class for an in-stream provider failure, from its structured fields only.
|
|
157
|
+
* `definite` classes come from a recognised identifier and outrank everything else. A failed response
|
|
158
|
+
* with no usable `error.code` (no error object, `{}`, a message only, an empty code, or only an unknown
|
|
159
|
+
* type) is still a provider-side failure and defaults to SERVER, but not definitely: wording in the
|
|
160
|
+
* message that clearly names another class (rate limit, context window, timeout) keeps that class.
|
|
161
|
+
* An unrecognised non-empty code is left to the text heuristics and the generic fallback.
|
|
162
|
+
*/
|
|
163
|
+
function classifyUpstreamFailure(upstream) {
|
|
164
|
+
if (upstream === undefined)
|
|
165
|
+
return undefined;
|
|
166
|
+
const fromError = classifyProviderIdentifier(upstream.code) ??
|
|
167
|
+
classifyProviderIdentifier(upstream.type);
|
|
168
|
+
if (fromError !== undefined)
|
|
169
|
+
return { code: fromError, definite: true };
|
|
170
|
+
const fromReason = classifyProviderIdentifier(upstream.incompleteReason);
|
|
171
|
+
if (fromReason !== undefined)
|
|
172
|
+
return { code: fromReason, definite: true };
|
|
173
|
+
if (upstream.incomplete)
|
|
174
|
+
return undefined;
|
|
175
|
+
if (upstream.code === undefined)
|
|
176
|
+
return { code: "SERVER", definite: false };
|
|
177
|
+
return undefined;
|
|
178
|
+
}
|
|
76
179
|
/**
|
|
77
180
|
* Provider bodies/messages are deliberately not surfaced. Only a stable class and safe facts leave the wire boundary.
|
|
78
181
|
* @param {unknown} error
|
|
79
182
|
* @param {AbortSignal} [signal]
|
|
80
183
|
*/
|
|
81
|
-
export function managedFailure(error, signal) {
|
|
184
|
+
export function managedFailure(error, signal, upstream) {
|
|
82
185
|
const facts = errorFacts(error);
|
|
83
186
|
const sourceCodes = new Set(facts.codes);
|
|
84
187
|
const text = facts.text;
|
|
@@ -87,8 +190,11 @@ export function managedFailure(error, signal) {
|
|
|
87
190
|
(signalReason?.name === "TimeoutError" ||
|
|
88
191
|
signalReason?.code === "TimeoutError" ||
|
|
89
192
|
/time(?:d)?\s*out|timeout/iu.test(String(signalReason?.message ?? "")));
|
|
193
|
+
const upstreamClass = signal?.aborted ? undefined : classifyUpstreamFailure(upstream);
|
|
90
194
|
let code = "RESPONSES_ERROR";
|
|
91
|
-
if (
|
|
195
|
+
if (upstreamClass?.definite === true)
|
|
196
|
+
code = upstreamClass.code;
|
|
197
|
+
else if (signalTimedOut ||
|
|
92
198
|
facts.status === 408 ||
|
|
93
199
|
sourceCodes.has("TimeoutError") ||
|
|
94
200
|
/time(?:d)?\s*out|timeout/iu.test(text))
|
|
@@ -128,6 +234,7 @@ export function managedFailure(error, signal) {
|
|
|
128
234
|
facts.status === 422 ||
|
|
129
235
|
sourceCodes.has("LCX_RESPONSES_INVALID_INPUT") ||
|
|
130
236
|
sourceCodes.has("LCX_CHECKPOINT_UNSUPPORTED") ||
|
|
237
|
+
sourceCodes.has("LCX_CHECKPOINT_IMAGE_BUDGET") ||
|
|
131
238
|
/invalid.?request|payload too large|length limit exceeded/iu.test(text))
|
|
132
239
|
code = "INVALID_REQUEST";
|
|
133
240
|
else if (sourceCodes.has("LCX_RESPONSE_TOO_LARGE"))
|
|
@@ -138,6 +245,9 @@ export function managedFailure(error, signal) {
|
|
|
138
245
|
else if (error instanceof TypeError ||
|
|
139
246
|
/\bnetwork|connection|socket|fetch|ECONN|EAI_AGAIN|terminated|premature close\b/iu.test(text))
|
|
140
247
|
code = "TRANSPORT";
|
|
248
|
+
// Nothing in the text said otherwise: a failure the provider reported without a usable code is its own.
|
|
249
|
+
if (code === "RESPONSES_ERROR" && upstreamClass !== undefined)
|
|
250
|
+
code = upstreamClass.code;
|
|
141
251
|
const messages = /** @type {Record<string, string>} */ {
|
|
142
252
|
ABORTED: "Responses request was aborted",
|
|
143
253
|
AUTH: "Responses request was rejected by authentication",
|
|
@@ -148,7 +258,9 @@ export function managedFailure(error, signal) {
|
|
|
148
258
|
SERVER: "Responses provider returned a server failure",
|
|
149
259
|
INVALID_REQUEST: sourceCodes.has("LCX_CHECKPOINT_UNSUPPORTED")
|
|
150
260
|
? "Unsupported or invalid LCX checkpoint; start a new session"
|
|
151
|
-
: "
|
|
261
|
+
: sourceCodes.has("LCX_CHECKPOINT_IMAGE_BUDGET")
|
|
262
|
+
? "Images kept in the compaction checkpoint exceed the request image bound; start a new session or raise maxRequestImageBytes"
|
|
263
|
+
: "Responses provider rejected the request",
|
|
152
264
|
CONTEXT_WINDOW_EXCEEDED: "Responses request exceeded the model context window",
|
|
153
265
|
UNSUPPORTED_CONTENT: "LCX cannot serialize a DSH content block for this Responses route",
|
|
154
266
|
GROK_NATIVE_PROTOCOL_ERROR: "Grok returned an unrecognized client tool call while native search was enabled",
|
|
@@ -719,6 +831,7 @@ async function* normalizedResponseEvents(source, meta = {}, options = {}) {
|
|
|
719
831
|
if (!isObject(raw))
|
|
720
832
|
continue;
|
|
721
833
|
const event = /** @type {UnknownRecord} */ raw;
|
|
834
|
+
observeUpstreamFailure(event, meta);
|
|
722
835
|
if (options.serverToolTypes && ['response.completed', 'response.failed', 'response.incomplete'].includes(String(event.type))) {
|
|
723
836
|
meta.contextDetails = sanitizeContextDetails(isObject(event.response) && isObject(event.response.usage)
|
|
724
837
|
? event.response.usage.context_details : undefined);
|
|
@@ -1332,7 +1445,33 @@ async function* toDshChunks(events, signal, wireMeta, nativeReplayRoute) {
|
|
|
1332
1445
|
* @param {ReadonlySet<string>} [options.serverToolTypes]
|
|
1333
1446
|
* @param {(usage: ServerToolUsage) => void} [options.onServerToolUsage]
|
|
1334
1447
|
*/
|
|
1335
|
-
export async function* streamResponsesRequest({ baseURL, provider, model, piModel, body, grammarToolInputProperties, headers, signal, timeoutMs, applyDefaultTimeout = true, streamIdleTimeoutMs, maxAttempts = 1, maxResponseBytes, serverToolTypes, isServerToolItem, declaredToolNames, nativeReplayRoute, onServerToolUsage, }) {
|
|
1448
|
+
export async function* streamResponsesRequest({ baseURL, provider, model, piModel, body, grammarToolInputProperties, headers, signal, timeoutMs, applyDefaultTimeout = true, streamIdleTimeoutMs, maxAttempts = 1, maxResponseBytes, serverToolTypes, isServerToolItem, declaredToolNames, nativeReplayRoute, onServerToolUsage, onFailure, }) {
|
|
1449
|
+
let contentEmitted = false;
|
|
1450
|
+
let failureReported = false;
|
|
1451
|
+
/** One redacted report per request; user aborts are not failures. */
|
|
1452
|
+
const reportFailure = (chunk, managed, upstream) => {
|
|
1453
|
+
if (failureReported || onFailure === undefined)
|
|
1454
|
+
return;
|
|
1455
|
+
if (chunk.type !== "finish" || chunk.reason.kind !== "error")
|
|
1456
|
+
return;
|
|
1457
|
+
failureReported = true;
|
|
1458
|
+
const failure = chunk.reason.failure;
|
|
1459
|
+
try {
|
|
1460
|
+
onFailure({
|
|
1461
|
+
code: String(failure.code),
|
|
1462
|
+
...(typeof failure.status === "number" ? { status: failure.status } : {}),
|
|
1463
|
+
...(managed?.requestId === undefined ? {} : { requestId: managed.requestId }),
|
|
1464
|
+
...(upstream?.code === undefined ? {} : { providerCode: upstream.code }),
|
|
1465
|
+
...(upstream?.type === undefined ? {} : { providerType: upstream.type }),
|
|
1466
|
+
...(upstream?.incompleteReason === undefined ? {} : { incompleteReason: upstream.incompleteReason }),
|
|
1467
|
+
...(upstream === undefined ? {} : { event: upstream.event }),
|
|
1468
|
+
contentEmitted,
|
|
1469
|
+
});
|
|
1470
|
+
}
|
|
1471
|
+
catch {
|
|
1472
|
+
// Diagnostics must never change the stream outcome.
|
|
1473
|
+
}
|
|
1474
|
+
};
|
|
1336
1475
|
const effectiveTimeoutMs = timeoutMs ?? (applyDefaultTimeout ? 300_000 : undefined);
|
|
1337
1476
|
const deadline = requestDeadline(signal, effectiveTimeoutMs);
|
|
1338
1477
|
const watchdog = streamIdleWatchdog(deadline.signal, streamIdleTimeoutMs);
|
|
@@ -1380,7 +1519,7 @@ export async function* streamResponsesRequest({ baseURL, provider, model, piMode
|
|
|
1380
1519
|
output.stopReason = signal?.aborted ? "aborted" : "error";
|
|
1381
1520
|
output.errorMessage =
|
|
1382
1521
|
error instanceof Error ? error.message : "Responses stream failed";
|
|
1383
|
-
output.__lcxFailure = managedFailure(error, signal);
|
|
1522
|
+
output.__lcxFailure = managedFailure(error, signal, wireMeta.upstreamFailure);
|
|
1384
1523
|
piEvents.push({
|
|
1385
1524
|
type: "error",
|
|
1386
1525
|
reason: /** @type {'aborted' | 'error'} */ output.stopReason,
|
|
@@ -1402,7 +1541,13 @@ export async function* streamResponsesRequest({ baseURL, provider, model, piMode
|
|
|
1402
1541
|
break;
|
|
1403
1542
|
}
|
|
1404
1543
|
watchdog.disarm();
|
|
1405
|
-
|
|
1544
|
+
const value = result.value;
|
|
1545
|
+
if (value.type === "text-delta" ||
|
|
1546
|
+
value.type === "reasoning-delta" ||
|
|
1547
|
+
value.type === "tool-call-delta")
|
|
1548
|
+
contentEmitted = true;
|
|
1549
|
+
reportFailure(value, output.__lcxFailure, wireMeta.upstreamFailure);
|
|
1550
|
+
yield value;
|
|
1406
1551
|
watchdog.arm();
|
|
1407
1552
|
}
|
|
1408
1553
|
}
|
|
@@ -1414,7 +1559,9 @@ export async function* streamResponsesRequest({ baseURL, provider, model, piMode
|
|
|
1414
1559
|
await parser;
|
|
1415
1560
|
}
|
|
1416
1561
|
catch (error) {
|
|
1417
|
-
|
|
1562
|
+
const chunk = managedFailureChunk(error, signal);
|
|
1563
|
+
reportFailure(chunk, managedFailure(error, signal), undefined);
|
|
1564
|
+
yield chunk;
|
|
1418
1565
|
}
|
|
1419
1566
|
finally {
|
|
1420
1567
|
watchdog.dispose();
|
package/lib/route.js
CHANGED
|
@@ -169,34 +169,48 @@ const MODEL_THINKING_LEVELS = [
|
|
|
169
169
|
function profileReasoning(value) {
|
|
170
170
|
return MODEL_THINKING_LEVELS.find((level) => level === value);
|
|
171
171
|
}
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
172
|
+
/**
|
|
173
|
+
* The reasoning projection of a model whose profile entry declares `reasoningEfforts` (the mapping
|
|
174
|
+
* from DSH levels to wire spellings, or `false` for a non-reasoning model); undefined when the
|
|
175
|
+
* profile says nothing, so the caller falls back to the catalog or to what DSH resolved.
|
|
176
|
+
*/
|
|
177
|
+
function declaredReasoningControls(profile, modelId) {
|
|
178
|
+
const efforts = configuredModelProfile(profile, modelId)?.reasoningEfforts;
|
|
175
179
|
if (efforts === undefined)
|
|
176
|
-
return
|
|
177
|
-
reasoning: base?.reasoning ?? false,
|
|
178
|
-
...(base?.thinkingLevelMap === undefined
|
|
179
|
-
? {}
|
|
180
|
-
: { thinkingLevelMap: base.thinkingLevelMap }),
|
|
181
|
-
};
|
|
180
|
+
return undefined;
|
|
182
181
|
if (efforts === false)
|
|
183
182
|
return { reasoning: false };
|
|
184
183
|
if (!isRecord(efforts))
|
|
185
184
|
return { reasoning: false };
|
|
186
185
|
const thinkingLevelMap = {};
|
|
186
|
+
let offOmitsReasoning = false;
|
|
187
187
|
for (const level of MODEL_THINKING_LEVELS) {
|
|
188
188
|
const value = efforts[level];
|
|
189
189
|
if (value === undefined)
|
|
190
190
|
thinkingLevelMap[level] = null;
|
|
191
191
|
else if (value === null) {
|
|
192
|
-
// DSH permits only off:null: it means supported-off by omitting the wire option.
|
|
193
|
-
|
|
194
|
-
|
|
192
|
+
// DSH permits only off:null: it means supported-off by omitting the wire option. Pi 1.1.0 would
|
|
193
|
+
// read an absent `off` as "send none", so the omission is made explicit here.
|
|
194
|
+
thinkingLevelMap[level] = null;
|
|
195
|
+
if (level === "off")
|
|
196
|
+
offOmitsReasoning = true;
|
|
195
197
|
}
|
|
196
198
|
else if (typeof value === "string")
|
|
197
199
|
thinkingLevelMap[level] = value;
|
|
198
200
|
}
|
|
199
|
-
return {
|
|
201
|
+
return {
|
|
202
|
+
reasoning: true,
|
|
203
|
+
thinkingLevelMap,
|
|
204
|
+
...(offOmitsReasoning ? { offOmitsReasoning: true } : {}),
|
|
205
|
+
};
|
|
206
|
+
}
|
|
207
|
+
function configuredModelControls(profile, modelId, base) {
|
|
208
|
+
return (declaredReasoningControls(profile, modelId) ?? {
|
|
209
|
+
reasoning: base?.reasoning ?? false,
|
|
210
|
+
...(base?.thinkingLevelMap === undefined
|
|
211
|
+
? {}
|
|
212
|
+
: { thinkingLevelMap: base.thinkingLevelMap }),
|
|
213
|
+
});
|
|
200
214
|
}
|
|
201
215
|
function isLcxCapabilityRoute(provider, model) {
|
|
202
216
|
return provider === "lcx" &&
|
|
@@ -241,6 +255,7 @@ export function resolveResponsesRouteConfig(ctx, options, policy) {
|
|
|
241
255
|
const fallbackRetention = lcxCapabilityRoute ? policy.cacheRetention : undefined;
|
|
242
256
|
const requestedRetention = configuredRetention === "none" || configuredRetention === "short" || configuredRetention === "long" ? configuredRetention : fallbackRetention === "none" || fallbackRetention === "short" || fallbackRetention === "long" ? fallbackRetention : undefined;
|
|
243
257
|
const cacheRetention = requestedRetention === "long" && !supportsLongCacheRetention ? "short" : requestedRetention ?? (supportsLongCacheRetention ? "long" : "short");
|
|
258
|
+
const declaredControls = declaredReasoningControls(configured, model);
|
|
244
259
|
return {
|
|
245
260
|
provider,
|
|
246
261
|
model,
|
|
@@ -251,6 +266,9 @@ export function resolveResponsesRouteConfig(ctx, options, policy) {
|
|
|
251
266
|
cacheRetention,
|
|
252
267
|
supportsLongCacheRetention,
|
|
253
268
|
responsesCompat,
|
|
269
|
+
// Only a declared `reasoningEfforts` is projected here; an undeclared model follows the catalog
|
|
270
|
+
// and what DSH resolved for it (see `gptModelOverride`).
|
|
271
|
+
...(declaredControls === undefined ? {} : { modelControls: declaredControls }),
|
|
254
272
|
// These are explicitly LCX-owned transport and request-capability policies.
|
|
255
273
|
timeoutMs: isPositiveInteger(configured.timeoutMs) ? configured.timeoutMs : policy.timeoutMs,
|
|
256
274
|
maxAttempts: retryAttempts(configured.retryPolicy, policy.maxAttempts),
|
package/lib/search-usage.js
CHANGED
|
@@ -10,15 +10,22 @@ export const searchUsageProjection = {
|
|
|
10
10
|
key: 'lcxSearchUsage', stateVersion: 3, stateSchema: schema.extend({ pending: z.record(z.string(), z.string()), headerKey: z.string().optional(), sampleHeaderKey: z.string().optional() }),
|
|
11
11
|
init: () => ({ auxiliary: zeroBuckets(), aggregateContext: false, pending: {} }),
|
|
12
12
|
apply(state, event) {
|
|
13
|
-
|
|
14
|
-
|
|
13
|
+
// Unit state is checkpointed by the host's projection cache, which refuses any state that is not
|
|
14
|
+
// lossless plain JSON. An optional field is therefore omitted when absent, never set to `undefined`.
|
|
15
|
+
if (event.type === 'request/header') {
|
|
16
|
+
const { headerKey: _previous, ...rest } = state;
|
|
17
|
+
const key = headerKey(event.data.header);
|
|
18
|
+
return key === undefined ? rest : { ...rest, headerKey: key };
|
|
19
|
+
}
|
|
15
20
|
// DSH measure anchors assistant/message only. Attempts must not transplant a
|
|
16
21
|
// newer context sample onto an older successful measurement anchor.
|
|
17
22
|
if (event.type === 'assistant/message' || event.type === 'assistant/attempt') {
|
|
18
23
|
const contextDetails = event.type === 'assistant/message' ? providerContextOf(event) : undefined;
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
24
|
+
// The newest reply replaces the whole provider-context sample: drop the old keys, then add the new ones.
|
|
25
|
+
const { contextDetails: _details, contextProvenance: _provenance, sampleHeaderKey: _sample, ...rest } = state;
|
|
26
|
+
return { ...rest, aggregateContext: aggregateContextOf(event) ?? state.aggregateContext,
|
|
27
|
+
...(contextDetails ? { contextDetails, contextProvenance: 'provider-context-details',
|
|
28
|
+
...(state.headerKey === undefined ? {} : { sampleHeaderKey: state.headerKey }) } : {}) };
|
|
22
29
|
}
|
|
23
30
|
if (event.type === 'tool/call' && isSearchTool(event.data.name))
|
|
24
31
|
return { ...state, pending: { ...state.pending, [event.data.callId]: event.data.name } };
|
package/lib/transport.js
CHANGED
|
@@ -279,10 +279,17 @@ export async function consumeSse(response, onEvent, options = {}) {
|
|
|
279
279
|
}
|
|
280
280
|
onEvent(event);
|
|
281
281
|
};
|
|
282
|
+
// An abort must interrupt a read that never settles, not only be noticed between reads. Cancelling
|
|
283
|
+
// the reader settles the pending read as done, so the abort is re-checked right after every read.
|
|
284
|
+
const onAbort = () => {
|
|
285
|
+
reader.cancel(options.signal?.reason).catch(() => { });
|
|
286
|
+
};
|
|
287
|
+
options.signal?.addEventListener("abort", onAbort, { once: true });
|
|
282
288
|
try {
|
|
283
289
|
while (true) {
|
|
284
290
|
abortIfNeeded(options.signal);
|
|
285
291
|
const { done, value } = await reader.read();
|
|
292
|
+
abortIfNeeded(options.signal);
|
|
286
293
|
if (done)
|
|
287
294
|
break;
|
|
288
295
|
bytes += value.byteLength;
|
|
@@ -305,6 +312,7 @@ export async function consumeSse(response, onEvent, options = {}) {
|
|
|
305
312
|
dispatch();
|
|
306
313
|
}
|
|
307
314
|
finally {
|
|
315
|
+
options.signal?.removeEventListener("abort", onAbort);
|
|
308
316
|
try {
|
|
309
317
|
await reader.cancel();
|
|
310
318
|
}
|
package/package.json
CHANGED