dsh-lcx-codex 0.6.0-pre.1 → 0.6.0-pre.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +9 -9
- package/THIRD_PARTY_NOTICES.md +3 -2
- package/lib/abort-error.js +12 -1
- package/lib/client.js +73 -39
- package/lib/compact-v2.js +16 -56
- package/lib/defaults.js +15 -0
- package/lib/dsh-compat.js +15 -76
- package/lib/dsh-responses.js +70 -75
- package/lib/grok-native-search.js +1 -3
- package/lib/index.js +132 -55
- package/lib/model-catalog.js +46 -0
- package/lib/native-checkpoint.js +20 -183
- package/lib/pi-responses-runtime.js +142 -48
- package/lib/provider-headers.js +31 -0
- package/lib/responses-request.js +4 -12
- package/lib/responses-stream.js +115 -214
- package/lib/route.js +205 -87
- package/lib/search-accounting.js +1 -16
- package/lib/token-budget.js +6 -41
- package/lib/tool-surface.js +13 -14
- package/lib/transport.js +89 -107
- package/lib/types/client/search-media.d.ts +0 -2
- package/lib/util.js +21 -0
- package/lib/web-run-output.js +0 -3
- package/lib/web-search-alpha.js +22 -27
- package/lib/web-search-capability.js +1 -3
- package/lib/web-search-hosted.js +2 -2
- package/lib/web-search-ref-store.js +1 -3
- package/package.json +3 -3
- package/lib/pi086-responses-allowlist.js +0 -131
package/README.md
CHANGED
|
@@ -37,14 +37,14 @@ 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.6.0-pre.
|
|
41
|
-
| `0.2.0` 系列(`0.2.0-rc.2`,早于 `0.2.1-alpha.2`) | `0.5.
|
|
40
|
+
| `0.2.1-alpha.2` 及以后 | `0.6.0-pre.2`(预发布版) | `@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.
|
|
45
|
+
请务必按表格选择:几个版本互不兼容,装错版本时安装命令可能只给出依赖警告,但插件可能无法正常工作。`@latest` 是稳定版 `0.5.2`(发布基线 DSH `0.2.0-rc.2`,host / plugin Pi `0.87.1`),DSH `0.1.5` 用户请固定安装 `@0.4.3`。
|
|
46
46
|
|
|
47
|
-
`0.6.0-pre.
|
|
47
|
+
`0.6.0-pre.2` 是预发布版,面向 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 `1.1.0`),请不要据此认为其他 DSH 版本受支持。
|
|
48
48
|
|
|
49
49
|
本版本开始适配 DSH `0.2.1` 的插件相关新能力:
|
|
50
50
|
|
|
@@ -131,10 +131,10 @@ dsh web
|
|
|
131
131
|
|
|
132
132
|
## 从旧版本升级
|
|
133
133
|
|
|
134
|
-
- **DSH 升级到 `0.2.1-alpha.2` 或更新版本**:把插件换成 `@prelatest`(`0.6.0-pre.
|
|
135
|
-
- **DSH 升级到 `0.2.0` 系列**:把插件换成 `@latest`(`0.5.
|
|
134
|
+
- **DSH 升级到 `0.2.1-alpha.2` 或更新版本**:把插件换成 `@prelatest`(`0.6.0-pre.2`),稳定版 `0.5.x` 没有在这些 DSH 版本上验证。
|
|
135
|
+
- **DSH 升级到 `0.2.0` 系列**:把插件换成 `@latest`(`0.5.2`),旧版 `0.4.x` 不支持 DSH `0.2`。
|
|
136
136
|
- **DSH `0.1.6` 系列**:继续使用 `@0.4.4-pre.1`。
|
|
137
|
-
- **DSH `0.1.5` 系列**:继续使用 `@0.4.3`;`@latest` 现在指向支持 DSH `0.2` 的 `0.5.
|
|
137
|
+
- **DSH `0.1.5` 系列**:继续使用 `@0.4.3`;`@latest` 现在指向支持 DSH `0.2` 的 `0.5.2`。
|
|
138
138
|
- **已装了 `0.5.0-pre.1`**:DSH `0.2.0` 系列重新运行 `@latest` 的 `add` 命令即可升到稳定版(`0.5.0-pre.1` 与 `0.5.0` 运行时代码一致);DSH `0.2.1-alpha.2` 及以后用 `@prelatest`。
|
|
139
139
|
- **升级后建议新建会话**:旧版本保存的压缩状态不保证兼容。DSH `0.2` 迁移过的旧长会话如果带有旧版 LCX 压缩检查点,LCX 会提示 `LCX_CHECKPOINT_UNSUPPORTED`,新建会话即可;其他模型不受影响。
|
|
140
140
|
|
|
@@ -156,7 +156,7 @@ dsh web
|
|
|
156
156
|
- **Markdown 图片换行**:为避免 DSH `0.2` 中图片后的文字挤到图片右侧,LCX 会让回答里的 Markdown 图片单独占一行,这一点与“搜索媒体预览”开关无关;句中的小图也会因此换行。
|
|
157
157
|
- **DSH `0.1.5` + LCX `0.4.3`**:在插件设置中启用或停用 LCX 后,已打开的浏览器页面可能需要刷新。
|
|
158
158
|
- **DSH `0.2.1` 的工具更新**:增量工具更新只在 DSH 声明“只增不改”的 GPT Responses 路由上生效;当 DSH 的工具历史或模型信息缺失、路由不支持或设置卡片里关闭了开关时,LCX 回到每次发送完整工具列表。删除工具或同名重定义仍会让缓存失效一次。缓存命中受上游影响有波动:在 `openai/gpt-6-sol` 上用同一操作序列对比,开启时新增工具后的首个请求约 80% 命中缓存,关闭时为 0%。LCX 自带的模型目录冻结在 Pi `0.86`,目录里没有的新模型(如 `gpt-6-sol`)按 DSH 的声明处理。
|
|
159
|
-
- **验证范围**:`0.6.0-pre.
|
|
159
|
+
- **验证范围**:`0.6.0-pre.2` 的验证基线只有 DSH `0.2.1-alpha.2`(预发布,host Pi `1.1.0`),安装范围没有上限,更新的 DSH 版本未经验证;该基线上的真实运行验证覆盖 GPT 路由、搜索、压缩、增量工具更新、`run_code` 内的搜索和插件管理页卡片等主要场景;其中 `run_code` 的实机检查在隔离环境里使用了完全访问的权限模式(DSH 的 Windows 文件沙箱在该机器上无法启动 `run_code`),程序只做了搜索调用。Grok 原生搜索和用户图片输入只做了有限的回归检查,Alpha 搜索和自然触发的压缩压力档位没有在这一轮覆盖。稳定版 `0.5.2` 的发布基线为 DSH `0.2.0-rc.2`(`0.5.0` 同);`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`。同一安装范围内其他后续版本仍需单独评估,遇到问题欢迎反馈。
|
|
160
160
|
|
|
161
161
|
## 常见问题
|
|
162
162
|
|
|
@@ -173,7 +173,7 @@ dsh web
|
|
|
173
173
|
从较早版本一路升级上来的配置目录里可能残留旧的依赖包。先停止 `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`。
|
|
174
174
|
|
|
175
175
|
**刚发布新版时 `@latest` / `@prelatest` 装到了旧版本?**
|
|
176
|
-
包管理器的缓存可能还没刷新,DSH 会因版本不兼容拒绝安装。稍等几分钟重试,或直接指定版本号,例如 `dsh-lcx-codex@0.6.0-pre.
|
|
176
|
+
包管理器的缓存可能还没刷新,DSH 会因版本不兼容拒绝安装。稍等几分钟重试,或直接指定版本号,例如 `dsh-lcx-codex@0.6.0-pre.2` 或 `dsh-lcx-codex@0.5.2`。
|
|
177
177
|
|
|
178
178
|
## 反馈问题
|
|
179
179
|
|
package/THIRD_PARTY_NOTICES.md
CHANGED
|
@@ -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
|
|
5
|
+
## @earendil-works/pi-ai 1.1.0
|
|
6
6
|
|
|
7
|
-
Upstream source: https://github.com/earendil-works/pi/tree/
|
|
7
|
+
Upstream source: https://github.com/earendil-works/pi/tree/v1.1.0/packages/ai
|
|
8
8
|
|
|
9
9
|
Included public Responses helpers and model catalogs:
|
|
10
10
|
|
|
@@ -12,6 +12,7 @@ Included public Responses helpers and model catalogs:
|
|
|
12
12
|
- `api/constrained-sampling`
|
|
13
13
|
- `api/openai-prompt-cache`
|
|
14
14
|
- `utils/event-stream`
|
|
15
|
+
- `utils/transcript`
|
|
15
16
|
- model helpers from the package root
|
|
16
17
|
- Responses-bearing catalogs for `cloudflare-ai-gateway`, `github-copilot`, `openai`, `opencode`, `opencode-go`, and `xai`
|
|
17
18
|
|
package/lib/abort-error.js
CHANGED
|
@@ -1,4 +1,15 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* DSH cancels with plain-object `AgentCancelCause` values — `{ kind: "user" }`,
|
|
3
|
+
* `{ kind: "parent" }`, `{ kind: "disposed" }`, `{ kind: "hook", reason }` — and exposes
|
|
4
|
+
* that same object as the runtime `AbortSignal.reason`. Propagating it raw reaches DSH's
|
|
5
|
+
* `errorMessage()`, which falls back to `String(value)`, so a cancelled in-flight call
|
|
6
|
+
* persists `Error: [object Object]` as model-facing tool-result text.
|
|
7
|
+
*
|
|
8
|
+
* Every LCX transport/stream abort site normalizes through here instead. An `Error`
|
|
9
|
+
* reason is preserved exactly — including `TimeoutError`, which failure classification
|
|
10
|
+
* still reads off the signal — and any other reason becomes a stable `AbortError` that
|
|
11
|
+
* keeps the original value as diagnostic `cause`. DSH's cancel cause is never mutated.
|
|
12
|
+
*/
|
|
2
13
|
const ABORT_MESSAGE = "The operation was aborted";
|
|
3
14
|
function abortInstance() {
|
|
4
15
|
if (typeof DOMException === "function")
|
package/lib/client.js
CHANGED
|
@@ -406,7 +406,7 @@ a.lcx-media-chip:hover{text-decoration:underline;text-underline-offset:2px}
|
|
|
406
406
|
if (!ElementClass || !row || !row.dataset.chatTurn) return null;
|
|
407
407
|
const group = row.closest("[data-chat-group-key][data-step-process]");
|
|
408
408
|
const siblings = group?.parentElement;
|
|
409
|
-
const column = siblings?.dataset.slot === CHAT_FLOW_SLOT ? siblings.parentElement :
|
|
409
|
+
const column = siblings?.dataset.slot === CHAT_FLOW_SLOT ? siblings.parentElement : null;
|
|
410
410
|
if (!group || !(siblings instanceof ElementClass) || !(column instanceof ElementClass) || !column.hasAttribute("data-chat-flow") || group.dataset.chatTurn !== row.dataset.chatTurn) return null;
|
|
411
411
|
let answer = null;
|
|
412
412
|
for (const child of siblings.children) {
|
|
@@ -833,7 +833,19 @@ a.lcx-media-chip:hover{text-decoration:underline;text-underline-offset:2px}
|
|
|
833
833
|
// src/client/nested-calls.ts
|
|
834
834
|
function createCallTurns(limit = 4096) {
|
|
835
835
|
const turns = /* @__PURE__ */ new Map();
|
|
836
|
+
let lastSeq = -Infinity;
|
|
836
837
|
return {
|
|
838
|
+
observe(seq) {
|
|
839
|
+
if (typeof seq !== "number" || !Number.isFinite(seq)) return;
|
|
840
|
+
if (seq < lastSeq) turns.clear();
|
|
841
|
+
lastSeq = seq;
|
|
842
|
+
},
|
|
843
|
+
openTurn(turn) {
|
|
844
|
+
for (const [callId, remembered] of turns) if (remembered !== turn) turns.delete(callId);
|
|
845
|
+
},
|
|
846
|
+
closeTurn(turn) {
|
|
847
|
+
for (const [callId, remembered] of turns) if (remembered === turn) turns.delete(callId);
|
|
848
|
+
},
|
|
837
849
|
remember(callId, turn) {
|
|
838
850
|
if (typeof callId !== "string" || !Number.isSafeInteger(turn)) return;
|
|
839
851
|
turns.delete(callId);
|
|
@@ -842,42 +854,51 @@ a.lcx-media-chip:hover{text-decoration:underline;text-underline-offset:2px}
|
|
|
842
854
|
},
|
|
843
855
|
turnOf(callId) {
|
|
844
856
|
return typeof callId === "string" ? turns.get(callId) : void 0;
|
|
857
|
+
},
|
|
858
|
+
clear() {
|
|
859
|
+
turns.clear();
|
|
860
|
+
lastSeq = -Infinity;
|
|
845
861
|
}
|
|
846
862
|
};
|
|
847
863
|
}
|
|
848
864
|
|
|
849
865
|
// src/client/search-usage-ui.ts
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
866
|
+
function createSearchUsageDefinition(callTurns = createCallTurns()) {
|
|
867
|
+
return {
|
|
868
|
+
kind: "lcx-search-usage",
|
|
869
|
+
match(event) {
|
|
870
|
+
if (event.type !== "assistant/live-chunk") callTurns.observe(event.seq);
|
|
871
|
+
if (event.type === "turn/start") callTurns.openTurn(event.data.turn);
|
|
872
|
+
if (event.type === "turn/end") callTurns.closeTurn(event.data.turn);
|
|
873
|
+
if (event.type === "turn/start") return { id: String(event.data.turn), role: "start" };
|
|
874
|
+
if (event.type === "tool/call") callTurns.remember(event.data.callId, event.data.turn);
|
|
875
|
+
if (event.type === "turn/end" || event.type === "tool/result" || event.type === "tool/call") return { id: String(event.data.turn), role: "update" };
|
|
876
|
+
if (event.type === NESTED_SETTLE_EVENT) {
|
|
877
|
+
const turn = callTurns.turnOf(event.data.rootCallId);
|
|
878
|
+
return turn === void 0 ? null : { id: String(turn), role: "update" };
|
|
879
|
+
}
|
|
880
|
+
return null;
|
|
881
|
+
},
|
|
882
|
+
start(_context, match) {
|
|
883
|
+
if (match.event.type !== "turn/start") throw new Error("LCX billing requires a complete turn");
|
|
884
|
+
return { turn: match.event.data.turn, records: [], pending: {}, complete: false };
|
|
885
|
+
},
|
|
886
|
+
update(context, match) {
|
|
887
|
+
const event = match.event, state = context.state, pending = { ...state.pending };
|
|
888
|
+
if (event.type === "tool/call" && isSearchTool(event.data.name)) pending[event.data.callId] = event.data.name;
|
|
889
|
+
const callId = event.type === "tool/result" ? event.data.message.source.callId : void 0;
|
|
890
|
+
const extra = auxiliaryUsageOf(event, callId ? pending[callId] : void 0);
|
|
891
|
+
if (callId) delete pending[callId];
|
|
892
|
+
return { ...state, pending, records: [...state.records, ...extra], complete: event.type === "turn/end" };
|
|
893
|
+
},
|
|
894
|
+
publication: (match) => match.event.type === "turn/end" ? "immediate" : "none",
|
|
895
|
+
buildLocationData(context, scope) {
|
|
896
|
+
const s = context.state;
|
|
897
|
+
return scope === "turn" && s?.complete ? { kind: "turn", turn: s.turn, key: "lcx-search-usage", value: s.records } : null;
|
|
860
898
|
}
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
if (match.event.type !== "turn/start") throw new Error("LCX billing requires a complete turn");
|
|
865
|
-
return { turn: match.event.data.turn, records: [], pending: {}, complete: false };
|
|
866
|
-
},
|
|
867
|
-
update(context, match) {
|
|
868
|
-
const event = match.event, state = context.state, pending = { ...state.pending };
|
|
869
|
-
if (event.type === "tool/call" && isSearchTool(event.data.name)) pending[event.data.callId] = event.data.name;
|
|
870
|
-
const callId = event.type === "tool/result" ? event.data.message.source.callId : void 0;
|
|
871
|
-
const extra = auxiliaryUsageOf(event, callId ? pending[callId] : void 0);
|
|
872
|
-
if (callId) delete pending[callId];
|
|
873
|
-
return { ...state, pending, records: [...state.records, ...extra], complete: event.type === "turn/end" };
|
|
874
|
-
},
|
|
875
|
-
publication: (match) => match.event.type === "turn/end" ? "immediate" : "none",
|
|
876
|
-
buildLocationData(context, scope) {
|
|
877
|
-
const s = context.state;
|
|
878
|
-
return scope === "turn" && s?.complete ? { kind: "turn", turn: s.turn, key: "lcx-search-usage", value: s.records } : null;
|
|
879
|
-
}
|
|
880
|
-
};
|
|
899
|
+
};
|
|
900
|
+
}
|
|
901
|
+
var searchUsageDefinition = createSearchUsageDefinition();
|
|
881
902
|
function tokenCount(value) {
|
|
882
903
|
return String(value);
|
|
883
904
|
}
|
|
@@ -1084,19 +1105,22 @@ a.lcx-media-chip:hover{text-decoration:underline;text-underline-offset:2px}
|
|
|
1084
1105
|
};
|
|
1085
1106
|
}
|
|
1086
1107
|
const hostedMediaTool = (value) => value === "web_search" || value === "websearch_gpt_advanced";
|
|
1087
|
-
const
|
|
1108
|
+
const callTurns = createCallTurns();
|
|
1088
1109
|
const searchMediaDefinition = {
|
|
1089
1110
|
kind: SEARCH_MEDIA_KIND,
|
|
1090
1111
|
target: "chat",
|
|
1091
1112
|
match(event) {
|
|
1113
|
+
if (event.type !== "assistant/live-chunk") callTurns.observe(event.seq);
|
|
1114
|
+
if (event.type === "turn/start") callTurns.openTurn(event.data.turn);
|
|
1115
|
+
if (event.type === "turn/end") callTurns.closeTurn(event.data.turn);
|
|
1092
1116
|
if (event.type === "turn/start")
|
|
1093
1117
|
return { id: String(event.data.turn), role: "start" };
|
|
1094
1118
|
if (event.type === "tool/call")
|
|
1095
|
-
|
|
1119
|
+
callTurns.remember(event.data.callId, event.data.turn);
|
|
1096
1120
|
if (event.type === "tool/result" || event.type === "tool/call")
|
|
1097
1121
|
return { id: String(event.data.turn), role: "update" };
|
|
1098
1122
|
if (event.type === NESTED_SETTLE_EVENT) {
|
|
1099
|
-
const turn =
|
|
1123
|
+
const turn = callTurns.turnOf(event.data.rootCallId);
|
|
1100
1124
|
return turn === void 0 ? null : { id: String(turn), role: "update" };
|
|
1101
1125
|
}
|
|
1102
1126
|
if (event.type !== "assistant/message" || event.surfaceOp !== "append" || event.data.interrupted === true)
|
|
@@ -1505,15 +1529,25 @@ a.lcx-media-chip:hover{text-decoration:underline;text-underline-offset:2px}
|
|
|
1505
1529
|
function apply(ctx) {
|
|
1506
1530
|
const slots = ctx.slots ?? ctx.get("slots"), svc = ctx.configForms ?? ctx.get("configForms"), locale = ctx.locale ?? ctx.get("locale"), uiConversation = ctx.uiConversation ?? ctx.get("uiConversation");
|
|
1507
1531
|
if (!slots || !svc || !locale || !uiConversation) return;
|
|
1508
|
-
ctx.effect(() =>
|
|
1532
|
+
ctx.effect(() => {
|
|
1533
|
+
const usageTurns = createCallTurns();
|
|
1534
|
+
const unregister = uiConversation.events.register(createSearchUsageDefinition(usageTurns));
|
|
1535
|
+
return () => {
|
|
1536
|
+
if (typeof unregister === "function") unregister();
|
|
1537
|
+
usageTurns.clear();
|
|
1538
|
+
};
|
|
1539
|
+
}, "lcx search billing data");
|
|
1509
1540
|
ctx.effect(() => installUsageSlots(slots, React.createElement), "lcx search billing slots");
|
|
1510
1541
|
ctx.effect(mountCss, "lcx-codex styles");
|
|
1511
1542
|
for (const language of ["zh", "en"])
|
|
1512
1543
|
ctx.effect(() => locale.register(NAMESPACE, language, copy[language]), `lcx-codex ${language} dictionary`);
|
|
1513
|
-
ctx.effect(
|
|
1514
|
-
|
|
1515
|
-
|
|
1516
|
-
|
|
1544
|
+
ctx.effect(() => {
|
|
1545
|
+
const unregister = uiConversation.events.register(searchMediaDefinition);
|
|
1546
|
+
return () => {
|
|
1547
|
+
if (typeof unregister === "function") unregister();
|
|
1548
|
+
callTurns.clear();
|
|
1549
|
+
};
|
|
1550
|
+
}, "lcx-codex search media definition");
|
|
1517
1551
|
const installSlot = (name, label, register) => ctx.effect(() => {
|
|
1518
1552
|
const cleanup = slots.inject(name, register);
|
|
1519
1553
|
return () => {
|
package/lib/compact-v2.js
CHANGED
|
@@ -1,33 +1,15 @@
|
|
|
1
1
|
import { consumeSse, fetchSseWithRetry } from "./transport.js";
|
|
2
2
|
import { buildCompactionResponsesBody, responsesGenerationEnvelope as standardGenerationEnvelope, } from "./responses-request.js";
|
|
3
|
+
import { isRecord, codedError } from "./util.js";
|
|
3
4
|
export const REMOTE_COMPACTION_V2_FEATURE = "remote_compaction_v2";
|
|
4
|
-
/**
|
|
5
|
-
* @param {unknown} value
|
|
6
|
-
* @returns {value is UnknownRecord}
|
|
7
|
-
*/
|
|
8
|
-
function isObject(value) {
|
|
9
|
-
return value !== null && typeof value === "object" && !Array.isArray(value);
|
|
10
|
-
}
|
|
11
|
-
/**
|
|
12
|
-
* @param {string} message
|
|
13
|
-
* @param {string} [code]
|
|
14
|
-
* @returns {LcxError}
|
|
15
|
-
*/
|
|
16
5
|
function fail(message, code = "LCX_COMPACT_INVALID_RESPONSE") {
|
|
17
|
-
|
|
18
|
-
e.code = code;
|
|
19
|
-
return e;
|
|
6
|
+
return codedError(message, code);
|
|
20
7
|
}
|
|
21
|
-
/** @param {unknown} value */
|
|
22
8
|
function safeMachineField(value) {
|
|
23
9
|
if (typeof value !== "string" || value.length === 0 || value.length > 96)
|
|
24
10
|
return undefined;
|
|
25
11
|
return /^[A-Za-z0-9_.:\[\]-]+$/u.test(value) ? value : undefined;
|
|
26
12
|
}
|
|
27
|
-
/**
|
|
28
|
-
* @param {HeaderMap} [headers]
|
|
29
|
-
* @returns {HeaderMap}
|
|
30
|
-
*/
|
|
31
13
|
export function mergeFeatureHeader(headers = {}) {
|
|
32
14
|
const result = { ...headers };
|
|
33
15
|
const key = Object.keys(result).find((name) => name.toLowerCase() === "x-codex-beta-features");
|
|
@@ -40,10 +22,6 @@ export function mergeFeatureHeader(headers = {}) {
|
|
|
40
22
|
result[key ?? "x-codex-beta-features"] = values.join(",");
|
|
41
23
|
return result;
|
|
42
24
|
}
|
|
43
|
-
/**
|
|
44
|
-
* @param {GenerationControls} [controls]
|
|
45
|
-
* @returns {GenerationEnvelope}
|
|
46
|
-
*/
|
|
47
25
|
export function responsesGenerationEnvelope(controls = {}) {
|
|
48
26
|
return standardGenerationEnvelope({
|
|
49
27
|
model: "unknown",
|
|
@@ -52,10 +30,6 @@ export function responsesGenerationEnvelope(controls = {}) {
|
|
|
52
30
|
maxTokens: controls.maxTokens,
|
|
53
31
|
});
|
|
54
32
|
}
|
|
55
|
-
/**
|
|
56
|
-
* @param {NativeCompactionBodyOptions} options
|
|
57
|
-
* @returns {NativeCompactionBody}
|
|
58
|
-
*/
|
|
59
33
|
export function buildNativeCompactionBody({ model, modelDescriptor, input, tools, promptCacheKey, promptCacheRetention, cacheRetention, reasoningEffort, temperature, maxTokens, }) {
|
|
60
34
|
return buildCompactionResponsesBody({
|
|
61
35
|
model: modelDescriptor ?? model,
|
|
@@ -72,16 +46,14 @@ export function buildNativeCompactionBody({ model, modelDescriptor, input, tools
|
|
|
72
46
|
}
|
|
73
47
|
/**
|
|
74
48
|
* Provider usage stays unknown until the object guard and numeric normalization.
|
|
75
|
-
* @param {unknown} raw
|
|
76
|
-
* @returns {CanonicalUsage | undefined}
|
|
77
49
|
*/
|
|
78
50
|
function usageFrom(raw) {
|
|
79
|
-
if (!
|
|
51
|
+
if (!isRecord(raw))
|
|
80
52
|
return undefined;
|
|
81
53
|
const inputTotal = Number(raw.input_tokens ?? 0);
|
|
82
54
|
const output = Number(raw.output_tokens ?? 0);
|
|
83
55
|
const detailsValue = raw.input_tokens_details ?? raw.input_token_details ?? raw.prompt_tokens_details;
|
|
84
|
-
const details =
|
|
56
|
+
const details = isRecord(detailsValue) ? detailsValue : {};
|
|
85
57
|
const cacheRead = Number(details.cached_tokens ?? 0);
|
|
86
58
|
const cacheWrite = Number(details.cache_write_tokens ?? 0);
|
|
87
59
|
const result = {
|
|
@@ -96,9 +68,9 @@ function usageFrom(raw) {
|
|
|
96
68
|
result.cacheReadTokens = cacheRead;
|
|
97
69
|
if (Number.isFinite(cacheWrite) && cacheWrite > 0)
|
|
98
70
|
result.cacheWriteTokens = cacheWrite;
|
|
99
|
-
const outputDetails =
|
|
71
|
+
const outputDetails = isRecord(raw.output_tokens_details)
|
|
100
72
|
? raw.output_tokens_details
|
|
101
|
-
:
|
|
73
|
+
: isRecord(raw.output_token_details)
|
|
102
74
|
? raw.output_token_details
|
|
103
75
|
: {};
|
|
104
76
|
const reasoning = Number(outputDetails.reasoning_tokens ?? 0);
|
|
@@ -106,26 +78,20 @@ function usageFrom(raw) {
|
|
|
106
78
|
result.reasoningTokens = reasoning;
|
|
107
79
|
return Object.values(result).some((v) => Number(v) > 0) ? result : undefined;
|
|
108
80
|
}
|
|
109
|
-
/**
|
|
110
|
-
* @param {unknown} output
|
|
111
|
-
* @returns {{ output: ValidatedOutputItem[], compaction: CompactionItem }}
|
|
112
|
-
*/
|
|
113
81
|
function validateOutput(output) {
|
|
114
82
|
if (!Array.isArray(output) || output.length === 0)
|
|
115
83
|
throw fail("native compaction output is empty");
|
|
116
|
-
const compactions = output.filter((item) =>
|
|
84
|
+
const compactions = output.filter((item) => isRecord(item) && item.type === "compaction");
|
|
117
85
|
if (compactions.length !== 1)
|
|
118
86
|
throw fail(`native compaction must return exactly one compaction item, got ${compactions.length}`);
|
|
119
87
|
const compact = compactions[0];
|
|
120
88
|
if (typeof compact.encrypted_content !== "string" ||
|
|
121
89
|
compact.encrypted_content.length === 0)
|
|
122
90
|
throw fail("native compaction item has no encrypted_content");
|
|
123
|
-
/** @type {Set<string>} */
|
|
124
91
|
const calls = new Set();
|
|
125
|
-
/** @type {Set<string>} */
|
|
126
92
|
const results = new Set();
|
|
127
93
|
for (const item of output) {
|
|
128
|
-
if (!
|
|
94
|
+
if (!isRecord(item) || typeof item.type !== "string")
|
|
129
95
|
throw fail("native compaction output contains a malformed item");
|
|
130
96
|
if (item.type === "function_call") {
|
|
131
97
|
if (typeof item.call_id !== "string" ||
|
|
@@ -155,16 +121,13 @@ function validateOutput(output) {
|
|
|
155
121
|
}
|
|
156
122
|
/**
|
|
157
123
|
* Provider SSE events enter as unknown; response.completed remains authoritative.
|
|
158
|
-
* @param {Response} response
|
|
159
|
-
* @param {SseParseOptions} [options]
|
|
160
|
-
* @returns {Promise<NativeCompactionResult>}
|
|
161
124
|
*/
|
|
162
125
|
export async function parseNativeCompactionSse(response, options = {}) {
|
|
163
126
|
const byIndex = new Map();
|
|
164
127
|
const byId = new Map();
|
|
165
128
|
let terminal;
|
|
166
129
|
const mergeItem = (event) => {
|
|
167
|
-
if (!
|
|
130
|
+
if (!isRecord(event.item))
|
|
168
131
|
return;
|
|
169
132
|
const index = Number.isInteger(event.output_index)
|
|
170
133
|
? Number(event.output_index)
|
|
@@ -189,17 +152,17 @@ export async function parseNativeCompactionSse(response, options = {}) {
|
|
|
189
152
|
byId.set(id, record);
|
|
190
153
|
};
|
|
191
154
|
await consumeSse(response, (event) => {
|
|
192
|
-
if (!
|
|
155
|
+
if (!isRecord(event))
|
|
193
156
|
return;
|
|
194
157
|
if (event.type === "error" ||
|
|
195
158
|
event.type === "response.failed" ||
|
|
196
159
|
event.type === "response.incomplete") {
|
|
197
160
|
const upstreamValue = event.type === "response.failed"
|
|
198
|
-
?
|
|
161
|
+
? isRecord(event.response)
|
|
199
162
|
? event.response.error
|
|
200
163
|
: undefined
|
|
201
164
|
: event.error;
|
|
202
|
-
const upstream =
|
|
165
|
+
const upstream = isRecord(upstreamValue) ? upstreamValue : {};
|
|
203
166
|
const providerCode = safeMachineField(upstream.code);
|
|
204
167
|
const providerType = safeMachineField(upstream.type);
|
|
205
168
|
const providerParam = safeMachineField(upstream.param);
|
|
@@ -211,8 +174,8 @@ export async function parseNativeCompactionSse(response, options = {}) {
|
|
|
211
174
|
.filter(Boolean)
|
|
212
175
|
.join(" ");
|
|
213
176
|
const e = fail(`native compaction ended with ${event.type}${diagnostics ? ` ${diagnostics}` : ""}`, "LCX_COMPACT_UPSTREAM_ERROR");
|
|
214
|
-
if (Number.isInteger(upstream.status ?? (
|
|
215
|
-
e.status = Number(upstream.status ?? (
|
|
177
|
+
if (Number.isInteger(upstream.status ?? (isRecord(event.error) ? event.error.status : undefined)))
|
|
178
|
+
e.status = Number(upstream.status ?? (isRecord(event.error) ? event.error.status : undefined));
|
|
216
179
|
if (providerCode)
|
|
217
180
|
e.providerCode = providerCode;
|
|
218
181
|
if (providerType)
|
|
@@ -225,9 +188,9 @@ export async function parseNativeCompactionSse(response, options = {}) {
|
|
|
225
188
|
event.type === "response.output_item.done")
|
|
226
189
|
mergeItem(event);
|
|
227
190
|
if (event.type === "response.completed")
|
|
228
|
-
terminal =
|
|
191
|
+
terminal = isRecord(event.response) ? event.response : undefined;
|
|
229
192
|
}, options);
|
|
230
|
-
if (!
|
|
193
|
+
if (!isRecord(terminal))
|
|
231
194
|
throw fail("native compaction stream ended without response.completed", "LCX_COMPACT_INCOMPLETE_SSE");
|
|
232
195
|
if (terminal.status !== "completed")
|
|
233
196
|
throw fail("native compaction response.completed did not carry completed status", "LCX_COMPACT_INCOMPLETE_SSE");
|
|
@@ -243,9 +206,6 @@ export async function parseNativeCompactionSse(response, options = {}) {
|
|
|
243
206
|
usage: usageFrom(terminal.usage),
|
|
244
207
|
};
|
|
245
208
|
}
|
|
246
|
-
/**
|
|
247
|
-
* @param {NativeCompactionRequestOptions} options
|
|
248
|
-
*/
|
|
249
209
|
export async function requestNativeCompaction({ baseURL, model, modelDescriptor, input, tools, promptCacheKey, promptCacheRetention, cacheRetention, reasoningEffort, temperature, maxTokens, idempotencyKey, headers, signal, timeoutMs, maxAttempts, maxResponseBytes, }) {
|
|
250
210
|
const body = buildNativeCompactionBody({
|
|
251
211
|
model,
|
package/lib/defaults.js
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export const DEFAULTS = Object.freeze({
|
|
2
|
+
requestTimeoutMs: 300_000,
|
|
3
|
+
streamIdleTimeoutMs: 300_000,
|
|
4
|
+
maxResponseBytes: 8 * 1024 * 1024,
|
|
5
|
+
maxAttempts: 3,
|
|
6
|
+
maxAttemptsCap: 6,
|
|
7
|
+
alphaMaxOutputTokens: 2500,
|
|
8
|
+
alphaMaxOutputTokensCap: 32_000,
|
|
9
|
+
webMaxResults: 8,
|
|
10
|
+
webSearchProvider: "lcx-responses",
|
|
11
|
+
portableReplayMaxChars: 80_000,
|
|
12
|
+
nativeRetentionTokenBudget: 64_000,
|
|
13
|
+
assistantRetentionTokenReserve: 24_000,
|
|
14
|
+
assistantRetentionPerMessageTokenCap: 3_000,
|
|
15
|
+
});
|
package/lib/dsh-compat.js
CHANGED
|
@@ -56,8 +56,8 @@ export function readSelectedAgentRouteState(agent) {
|
|
|
56
56
|
return route;
|
|
57
57
|
// Cold/resumed Agents recover pending selection from DSH's public projection.
|
|
58
58
|
const projections = resolveScopedService(agent, "sessionProjections");
|
|
59
|
-
if (
|
|
60
|
-
const selection =
|
|
59
|
+
if (typeof projections?.stateOf === "function") {
|
|
60
|
+
const selection = projections.stateOf(session, "modelSelection");
|
|
61
61
|
if (mutableService(selection))
|
|
62
62
|
return { ...route, requestConfig: selection.pending ?? route.requestConfig };
|
|
63
63
|
}
|
|
@@ -66,13 +66,11 @@ export function readSelectedAgentRouteState(agent) {
|
|
|
66
66
|
}
|
|
67
67
|
export function scopedToolRuntime(agent) {
|
|
68
68
|
const tools = resolveScopedService(agent, "tools");
|
|
69
|
-
if (
|
|
69
|
+
if (typeof tools?.register !== "function")
|
|
70
70
|
return undefined;
|
|
71
71
|
return {
|
|
72
72
|
register: tools.register.bind(tools),
|
|
73
|
-
...(typeof tools.get === "function"
|
|
74
|
-
? { get: tools.get.bind(tools) }
|
|
75
|
-
: {}),
|
|
73
|
+
...(typeof tools.get === "function" ? { get: tools.get.bind(tools) } : {}),
|
|
76
74
|
};
|
|
77
75
|
}
|
|
78
76
|
/**
|
|
@@ -82,31 +80,32 @@ export function scopedToolRuntime(agent) {
|
|
|
82
80
|
*/
|
|
83
81
|
export function onScopedToolExecute(agent, listener) {
|
|
84
82
|
const ctx = agentLike(agent)?.ctx;
|
|
85
|
-
if (
|
|
83
|
+
if (typeof ctx?.on !== "function")
|
|
86
84
|
return undefined;
|
|
87
85
|
try {
|
|
88
|
-
const dispose =
|
|
89
|
-
return typeof dispose === "function" ? () => void
|
|
86
|
+
const dispose = ctx.on("tools/execute", listener);
|
|
87
|
+
return typeof dispose === "function" ? () => void dispose() : undefined;
|
|
90
88
|
}
|
|
91
89
|
catch {
|
|
92
90
|
return undefined;
|
|
93
91
|
}
|
|
94
92
|
}
|
|
95
93
|
export function tokenMeterTotal(value, session) {
|
|
96
|
-
|
|
94
|
+
const meter = value;
|
|
95
|
+
if (typeof meter?.measure !== "function")
|
|
97
96
|
return undefined;
|
|
98
|
-
const measurement =
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
97
|
+
const measurement = meter.measure(session);
|
|
98
|
+
return typeof measurement?.totalTokens === "number" && Number.isFinite(measurement.totalTokens)
|
|
99
|
+
? measurement.totalTokens
|
|
100
|
+
: undefined;
|
|
102
101
|
}
|
|
103
102
|
export function sessionsService(ctx) {
|
|
104
103
|
const service = resolveContextService(ctx, "sessions");
|
|
105
|
-
if (
|
|
104
|
+
if (typeof service?.get !== "function")
|
|
106
105
|
return undefined;
|
|
107
106
|
return {
|
|
108
107
|
get(id) {
|
|
109
|
-
const value =
|
|
108
|
+
const value = service.get?.(id);
|
|
110
109
|
return value instanceof Session ? value : undefined;
|
|
111
110
|
},
|
|
112
111
|
};
|
|
@@ -116,30 +115,6 @@ export function sessionFor(ctx, sessionId) {
|
|
|
116
115
|
? sessionsService(ctx)?.get(SessionId(sessionId))
|
|
117
116
|
: undefined;
|
|
118
117
|
}
|
|
119
|
-
export function readWebSearchProvider(ctx) {
|
|
120
|
-
const web = resolveContextService(ctx, "web");
|
|
121
|
-
if (!mutableService(web))
|
|
122
|
-
return undefined;
|
|
123
|
-
try {
|
|
124
|
-
return Reflect.get(web, "searchProviderId");
|
|
125
|
-
}
|
|
126
|
-
catch {
|
|
127
|
-
return undefined;
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
export function writeWebSearchProvider(ctx, providerId) {
|
|
131
|
-
const web = resolveContextService(ctx, "web");
|
|
132
|
-
if (!mutableService(web))
|
|
133
|
-
return false;
|
|
134
|
-
try {
|
|
135
|
-
if (!Reflect.set(web, "searchProviderId", providerId))
|
|
136
|
-
return false;
|
|
137
|
-
return Reflect.get(web, "searchProviderId") === providerId;
|
|
138
|
-
}
|
|
139
|
-
catch {
|
|
140
|
-
return false;
|
|
141
|
-
}
|
|
142
|
-
}
|
|
143
118
|
export function contextService(ctx, name) {
|
|
144
119
|
if (!mutableService(ctx))
|
|
145
120
|
return undefined;
|
|
@@ -334,39 +309,3 @@ export function restoreCompactionConfig(record) {
|
|
|
334
309
|
catch { }
|
|
335
310
|
}
|
|
336
311
|
}
|
|
337
|
-
export function patchVisibleWebSearchTimeout(agent, getTimeoutMs, patchedDefinitions) {
|
|
338
|
-
const tools = resolveScopedService(agent, "tools");
|
|
339
|
-
if (!mutableService(tools) || typeof tools.get !== "function")
|
|
340
|
-
return;
|
|
341
|
-
const definition = Reflect.apply(tools.get, tools, ["web_search", agent]);
|
|
342
|
-
if (!mutableService(definition))
|
|
343
|
-
return;
|
|
344
|
-
if (!patchedDefinitions.has(definition))
|
|
345
|
-
patchedDefinitions.set(definition, definition.timeoutMs);
|
|
346
|
-
const timeoutMs = getTimeoutMs();
|
|
347
|
-
const target = timeoutMs === undefined ? patchedDefinitions.get(definition) : timeoutMs;
|
|
348
|
-
try {
|
|
349
|
-
definition.timeoutMs = target;
|
|
350
|
-
}
|
|
351
|
-
catch { }
|
|
352
|
-
}
|
|
353
|
-
export function refreshVisibleWebSearchTimeouts(patchedDefinitions, timeoutMs) {
|
|
354
|
-
for (const [definition, original] of patchedDefinitions) {
|
|
355
|
-
try {
|
|
356
|
-
definition.timeoutMs = timeoutMs === undefined ? original : timeoutMs;
|
|
357
|
-
}
|
|
358
|
-
catch { }
|
|
359
|
-
}
|
|
360
|
-
}
|
|
361
|
-
export function restoreVisibleWebSearchTimeouts(patchedDefinitions) {
|
|
362
|
-
for (const [definition, original] of patchedDefinitions) {
|
|
363
|
-
try {
|
|
364
|
-
if (original === undefined)
|
|
365
|
-
delete definition.timeoutMs;
|
|
366
|
-
else
|
|
367
|
-
definition.timeoutMs = original;
|
|
368
|
-
}
|
|
369
|
-
catch { }
|
|
370
|
-
}
|
|
371
|
-
patchedDefinitions.clear();
|
|
372
|
-
}
|