feat(models): execute Responses and validated streams with capability routing

This commit is contained in:
Agent Workbench Dev
2026-10-31 22:27:53 +08:00
parent 578cf88eca
commit eeb11d6a76
24 changed files with 856 additions and 52 deletions
+27
View File
@@ -0,0 +1,27 @@
# 模型协议与能力边界
1.17新增OpenAI Responses适配,保留既有Chat Completions。品牌、模型名称与声明不能替代检测。现有API代理可按实际协议添加;未知WorkBuddy登录态、Anthropic原生协议及视觉/音频输入仍未接入。
## 实际协议
Chat沿用messages与tool_calls/tool_call_id;Responses使用typed input/output Items,function_call.call_id与function_call_output严格配对,保留原用量字段。请求使用store:false,支持同一接口和模型下携带加密reasoning Items,换接口/模型时重新生成普通历史,不泄露私有Items。工具采用strict:false以保持现有可选参数契约。
混合协议或流式模型池封存每个成员实际请求变体;真正发送的请求另有原字节SHA、长度和Task/Run所属工件。历史纯Chat请求继续按原格式核对。预算按最大变体估算和最小成员容量保守预留;不把多线路变体总和当一次发送量。
SSE只显示有界进展。完整参数和终结核对前不派发工具;Chat须完整finish_reason及[DONE],Responses须completed且身份、参数和文本与原片段一致。收到流后断开、超时或协议失败不自动换成员重发。主动停止关闭原HTTP并释放租约。保存有界原响应工件;已知凭据会脱敏,原流SHA和脱敏后工件SHA分别保留。超过4MB则停止读取并明确标记捕获范围。
## 能力与检测
文本/工具保留旧连接的协议声明,流式/视觉/音频默认待验证。检测只在用户主动调用后发送合成输入,可能产生上游费用;不输入项目文件、不执行检测工具。文本应答不能证明工具能力,工具须返回要求的原调用及nonce,流式须走真实SSE并完整结束。结果绑定连接和凭据修订及原请求账本;变化后不再显示已验证。
选路复用原轮询、权重、粘连、租约和冷却。能力不符者在获取租约前排除,并在原Run记录具体原因;全部不符记为未派发,不产生上游用量。HTTP401/429、超时、中断及结果未知分别保留,不承诺上游永不中断。
## 来源
实际读取并据以实现的OpenAI官方资料:
- [Responses迁移](https://developers.openai.com/api/docs/guides/migrate-to-responses)
- [函数调用](https://developers.openai.com/api/docs/guides/function-calling)
- [Responses流式事件](https://developers.openai.com/api/docs/guides/streaming-responses?api-mode=responses)
以上是协议依据,不能证明任意代理完整兼容。专项使用自有loopback服务;真实模型工程效果和Purge收益仍按用户此前选择单独待验。
+2 -2
View File
@@ -1,6 +1,6 @@
# 1.17 执行草案:模型协议、流式执行与能力路由
# 1.17 执行计划:模型协议、流式执行与能力路由
接续已完成独立发行和正式部署的1.16手机伴随端。此文件只登记后续方案,不表示已实现或已开始产品变更;从已验收的本地Git基线开具体分支。继续保持原Task/Run、工具结果、项目字节、审批和数据,按切片开发、验证、回顾并本地提交。
接续已完成独立发行和正式部署的1.16手机伴随端。当前从299fb14在feat/model-protocols-1.17实施;A–D已实现并通过专项,E操作验收中,F完整发行待执行。完成情况以QA-1.17、task_plan和原证据为准。继续保持原Task/Run、工具结果、项目字节、审批和数据,按切片开发、验证、回顾并本地提交。
用户目标是让接入的多个模型在真实工程中稳定承担合适工作。当前基础是兼容Chat协议的多连接、轮询/权重、Run粘连、并发租约、冷却及请求账本,另有受控DeepSeek引擎桥;这些不证明所有品牌的原生协议、流式、视觉或语音都可用。WorkBuddy未知配置格式、未知登录态导入及真实Purge收益仍须独立核验。
+8
View File
@@ -7,3 +7,11 @@
新增Chat Completions/Responses协议登记和版本化能力声明。文本、工具、流式、视觉与语音分别显示声明、未知、不支持、已验证;后者绑定连接修订及凭据修订,客户端提交不能伪造。旧schema:1记录只读兼容,读取不迁移原记录,原DPAPI绑定仍使用原协议/接口/模型身份。流式开关默认关闭,必须先明确声明支持。
18/18相关检查通过,包含旧模型池/请求身份/导入事务/热升级以及新能力声明、失效、未知筛选和失败回滚。原TAP见[专项结果](model-protocols-a-specialists.txt)。本切片尚不代表Responses请求或流式已实现;它们在B/C独立验收。全阶段发行、UI与工程门槛仍待执行。
## B–D:实际协议、流式与能力选路
Responses的typed Items、原call_id配对、原用量与同线路reasoning续接完成;混合池逐线路封存实际请求。Chat原格式和历史证据继续核对。原429切换后保持Run粘连,原工具不会重放。实际HTTP的Chat/Responses SSE支持分片UTF8、参数增量、完整终结、断开和主动停止;半截参数不执行,断流/超时不自动换线路重发。原响应以有界字节工件保存,凭据脱敏前/后SHA分别保留。
相关恢复检查42/42通过;新增边界29/29、流式/手机投影/上下文38/38通过(这些范围有重叠,不相加)。原日志见[恢复专项](model-protocols-current-specialists.txt)、[边界专项](model-protocols-boundary-specialists.txt)、[投影与上下文](model-protocols-projection-specialists.txt)。新增验证包括原停用对象导致probe引用失效的修复、检测所属attempt精确绑定、原始畸形UTF8捕获、CR事件终结、超时关闭HTTP、自有CCSwitch SQLite/TOML Responses导入,全部使用自有loopback数据。原失败夹具与首次UI定位失败保留;后者为select隐式名称包含option文字,已增加明确aria-label,重新实际验收中。
E界面已构建,正在操作验收;F独立发行与正式部署未执行。协议实现不能代表视觉/音频已接入或真实模型自主工程能力已经通过。具体协议和来源见[协议边界](MODEL-PROTOCOLS.md)。
+6
View File
@@ -3,3 +3,9 @@
## A:能力来源与旧连接
保留原schema:1和默认Chat连接,新增的能力信息放在每个连接上。声明不能当成实测,实测必须跟随实际连接和凭据修订;未知视觉/音频保持未知,不从模型名猜测。原模型池/导入/热升级和能力专项18项通过,未访问正式Key或发送真实模型请求。B/C继续实现实际协议和流式,A通过不能代表它们已交付。
## B–D:原请求与执行边界
统一消息不能取代真正发送的请求。Responses和流式池各封存实际变体,并另记当前物理请求的SHA/字节;保留原工具call_id、reasoning与原用量。流式进展不进入工具执行,直到原终结与参数通过核对;断流/超时保留unknown不自动重放。原始Buffer捕获保持畸形字节,发生已知凭据脱敏时单列工件SHA。
回顾发现probe引用原可变连接时,toggle会同时更新比较两端,导致旧检测结论被错误接纳;改为连接快照并验证修订。原attempt通过progress身份绑定,避免并发任务改变账本末项。29项新增边界及38项关联投影/上下文检查通过。预算仍按所有成员容量保守取最小值,可能降低混合池可用容量;没有损失请求/审批边界,后续可按真实任务需求优化。E实际验收和F独立发行仍按计划执行。
@@ -0,0 +1,39 @@
✔ owned CC Switch SQLite imports the selected TOML Responses wire_api and leaves unknown clients untouched (1434.5284ms)
✔ Responses executes one original tool through Runtime, carries encrypted Items, reports raw usage and seals exact wire bytes (4628.0791ms)
✔ mixed-protocol pool keeps both actual variants and changes route on 429 without re-executing completed tools (4708.4754ms)
▶ Responses incomplete, failed, missing call identity and unsupported hosted outputs never dispatch a tool
✔ Reject {"body":{"id":"resp_owned","status":"incomplete","output":[{"type":"function_call","id":"fc_partial" (2367.0582ms)
✔ Reject {"body":{"id":"resp_owned","status":"failed","output":[{"type":"function_call","id":"fc_failed","cal (2476.4776ms)
✔ Reject {"body":{"id":"resp_owned","status":"completed","output":[{"type":"function_call","id":"fc_bad","cal (2317.129ms)
✔ Reject {"body":{"id":"resp_owned","status":"completed","output":[{"type":"computer_call","id":"not-supporte (2229.2344ms)
✔ Responses incomplete, failed, missing call identity and unsupported hosted outputs never dispatch a tool (9390.7418ms)
✔ wire adapter preserves tool IDs but never passes Responses-private Items to Chat or another endpoint (0.1644ms)
✔ connection JSON import accepts explicit Responses protocol and continues to reject unknown protocols without reading client sessions (1040.2795ms)
✔ capability routing skips text-only members before acquiring a lease and records exact refusal in the original Run (2401.3744ms)
✔ all incompatible members reject with no network request, no lease, preserved Task and explicit non-dispatched ledger (2350.372ms)
✔ explicit synthetic tool probe verifies only actual matching call, never executes it and loses verification on revision changes (1549.1779ms)
✔ plain text probe cannot verify tools; wrong tool nonce and client supplied evidence cannot claim verified support (1817.464ms)
✔ a disabled connection invalidates an in-flight probe even when toggle mutates its original registry object (925.1592ms)
✔ real Chat SSE carries UTF8 and split tool JSON; Runtime executes only after DONE, stores partial progress and original usage (4639.8283ms)
✔ Responses typed SSE validates original item identity, terminal data, raw usage and tool result pair (4264.8592ms)
✔ incomplete network stream retains unknown attempt and never automatically switches pool or dispatches fragment tools (2665.4274ms)
✔ stopping an active HTTP stream closes only original request and keeps partial text without executing tools (1507.8489ms)
✔ explicit streaming probe uses actual SSE despite unknown declaration and binds its own original attempt (840.1404ms)
✔ stream timeout closes the original HTTP response, preserves redacted capture and never retries (1045.3911ms)
✔ SSE accepts CR-only final event boundaries and retains exact invalid UTF8 bytes without dispatch (1.144ms)
▶ stream parser rejects truncation, finish length, malformed JSON, duplicate sequence, changed final item and oversized data
✔ reject 0 (0.2171ms)
✔ reject 1 (0.1405ms)
✔ reject 2 (0.1213ms)
✔ reject 3 (5.2154ms)
✔ reject 4 (0.1261ms)
✔ reject 5 (0.1235ms)
✔ stream parser rejects truncation, finish length, malformed JSON, duplicate sequence, changed final item and oversized data (6.4556ms)
ℹ tests 29
ℹ suites 0
ℹ pass 29
ℹ fail 0
ℹ cancelled 0
ℹ skipped 0
ℹ todo 0
ℹ duration_ms 24266.1287
@@ -0,0 +1,52 @@
✔ schema1 connections remain readable without inventing observed capabilities or migrating original state (892.4845ms)
✔ protocol, unsupported capabilities and stream choice persist; client claims cannot forge verified evidence (966.5389ms)
✔ ability evidence belongs to exact connection and credential revision; unknown and unsupported reject required routing (0.3516ms)
✔ invalid declarations and inconsistent stream settings reject before registry, revision and credentials mutate (681.927ms)
✔ model registry keeps original credentials/endpoint intact, rejects stale edits, and forgets only volatile keys on restart (1259.4085ms)
✔ config import previews exact revisions, keeps secrets volatile, syncs only when enabled and preserves manual edits on conflicts (996.4614ms)
✔ real HTTP pool fails over 429 to another model and never repeats completed tools; attempts seal actual request bytes and Run identity (4172.9802ms)
✔ pool rotation and weights select members on new Runs; disabled and missing credentials are excluded without requests (5369.0176ms)
✔ 401 blocks the credential revision; updating the key restores availability without leaking error echoes (2136.1904ms)
✔ queued request cancellation and in-flight disable release only owned leases and do not dispatch another HTTP request (1285.1659ms)
✔ compiled model-center HTTP API persists public profiles, blocks forged origins, and local operations never invoke a model (2522.6777ms)
✔ model registry disk failures restore routing, pool profiles and credential references without partial updates (1125.4403ms)
✔ model selection and saved configuration commit together and failures preserve the original mode and endpoint (1857.4609ms)
✔ configuration imports roll back the entire batch and retain one usable preview after validation or commit failure (874.6812ms)
✔ Responses executes one original tool through Runtime, carries encrypted Items, reports raw usage and seals exact wire bytes (3872.5619ms)
✔ mixed-protocol pool keeps both actual variants and changes route on 429 without re-executing completed tools (3712.8468ms)
▶ Responses incomplete, failed, missing call identity and unsupported hosted outputs never dispatch a tool
✔ Reject {"body":{"id":"resp_owned","status":"incomplete","output":[{"type":"function_call","id":"fc_partial" (2077.5477ms)
✔ Reject {"body":{"id":"resp_owned","status":"failed","output":[{"type":"function_call","id":"fc_failed","cal (2176.6625ms)
✔ Reject {"body":{"id":"resp_owned","status":"completed","output":[{"type":"function_call","id":"fc_bad","cal (2038.6461ms)
✔ Reject {"body":{"id":"resp_owned","status":"completed","output":[{"type":"computer_call","id":"not-supporte (1629.5944ms)
✔ Responses incomplete, failed, missing call identity and unsupported hosted outputs never dispatch a tool (7923.2614ms)
✔ wire adapter preserves tool IDs but never passes Responses-private Items to Chat or another endpoint (0.1597ms)
✔ connection JSON import accepts explicit Responses protocol and continues to reject unknown protocols without reading client sessions (982.9388ms)
✔ capability routing skips text-only members before acquiring a lease and records exact refusal in the original Run (3118.5909ms)
✔ all incompatible members reject with no network request, no lease, preserved Task and explicit non-dispatched ledger (1579.9588ms)
✔ explicit synthetic tool probe verifies only actual matching call, never executes it and loses verification on revision changes (997.4663ms)
✔ plain text probe cannot verify tools; wrong tool nonce and client supplied evidence cannot claim verified support (1226.9391ms)
✔ real Chat SSE carries UTF8 and split tool JSON; Runtime executes only after DONE, stores partial progress and original usage (4102.3699ms)
✔ Responses typed SSE validates original item identity, terminal data, raw usage and tool result pair (3736.7539ms)
✔ incomplete network stream retains unknown attempt and never automatically switches pool or dispatches fragment tools (2254.8904ms)
✔ stopping an active HTTP stream closes only original request and keeps partial text without executing tools (1331.031ms)
▶ stream parser rejects truncation, finish length, malformed JSON, duplicate sequence, changed final item and oversized data
✔ reject 0 (0.6407ms)
✔ reject 1 (0.1739ms)
✔ reject 2 (0.2062ms)
✔ reject 3 (4.8471ms)
✔ reject 4 (0.1743ms)
✔ reject 5 (0.1468ms)
✔ stream parser rejects truncation, finish length, malformed JSON, duplicate sequence, changed final item and oversized data (6.8946ms)
✔ hot upgrade adopts a retained virtual data root without reopening SQLite or changing rows, Provider or routing (1591.9706ms)
✔ failed adoption restores the original virtual data-root name and original open database (834.5372ms)
✔ hot upgrade refreshes retained model and importer methods while preserving credential and routing objects (1088.6735ms)
✔ hot upgrade refuses active probes and restores retained model methods if installation fails (900.0117ms)
ℹ tests 42
ℹ suites 0
ℹ pass 42
ℹ fail 0
ℹ cancelled 0
ℹ skipped 0
ℹ todo 0
ℹ duration_ms 33273.7612
@@ -0,0 +1,47 @@
✔ pairing is short lived, single-use, generation-bound and validates exact permissions (1107.2974ms)
✔ device transaction failure neither consumes the ticket nor leaves a credential (971.6891ms)
✔ devices have project and action boundaries; rotation, expiration, revocation and host changes reject old authority (1084.1224ms)
✔ persistent registry stores hashes and a bounded audit, never raw credentials (967.9301ms)
✔ real TLS verifies IP SAN, CA chain, encrypted restart and public fingerprint (1195.4954ms)
✔ TLS sealing failure leaves listener closed and no plaintext or successful TLS registration (968.7866ms)
✔ HTTP pairing is concurrent single-use; secure cookie and strict origin/host checks (1062.3071ms)
✔ gateway never exposes management routes, arbitrary assets or unpaired project data (1249.5189ms)
✔ authorized project events paginate from original seq without leaking other project events or payloads (1700.9864ms)
✔ remote task projection excludes raw model history, host configuration, tool input and credentials (1442.3281ms)
✔ permission checks apply to direct remote mutation, not only UI controls (1047.2542ms)
✔ approval fingerprint and exact task/revision are bound; duplicate requests never execute twice (1540.1497ms)
✔ unknown original operation is recorded and never replayed after response failure (1245.2744ms)
✔ revocation and gateway closure reject old access without invoking runtime stop (777.859ms)
✔ rate limits reject pairing brute force and do not persist candidate secrets (750.6093ms)
✔ real Windows CurrentUser DPAPI persists only a sealed CA and reopens it (2582.997ms)
✔ mobile approval executes the original Runtime command exactly once and rejects the later desktop decision (1981.9998ms)
✔ desktop rejection wins a concurrent mobile request and original command remains unexecuted (1623.7516ms)
✔ phone stop waits for the original process exit and prevents later heartbeats and command replay (2266.9916ms)
✔ Run replacement during parent-stop wait is rejected rather than stopping the new Run (631.3227ms)
✔ diff pages preserve source SHA; report reads verify blobs and exclude unrelated artifact kinds (959.4308ms)
✔ hot upgrade refuses an active gateway and restores closed gateway ownership on failure (640.9049ms)
✔ damaged device permissions or expiry fail closed and never overwrite the original registry (699.9645ms)
✔ device revocation destroys its actual authenticated TLS connection (790.7691ms)
✔ real Chat SSE carries UTF8 and split tool JSON; Runtime executes only after DONE, stores partial progress and original usage (4304.0643ms)
✔ Responses typed SSE validates original item identity, terminal data, raw usage and tool result pair (3748.2139ms)
✔ incomplete network stream retains unknown attempt and never automatically switches pool or dispatches fragment tools (2501.0255ms)
✔ stopping an active HTTP stream closes only original request and keeps partial text without executing tools (2356.3092ms)
✔ explicit streaming probe uses actual SSE despite unknown declaration and binds its own original attempt (1192.6798ms)
✔ stream timeout closes the original HTTP response, preserves redacted capture and never retries (1412.0866ms)
✔ SSE accepts CR-only final event boundaries and retains exact invalid UTF8 bytes without dispatch (1.9856ms)
▶ stream parser rejects truncation, finish length, malformed JSON, duplicate sequence, changed final item and oversized data
✔ reject 0 (0.3728ms)
✔ reject 1 (0.2031ms)
✔ reject 2 (0.1649ms)
✔ reject 3 (4.8382ms)
✔ reject 4 (0.1347ms)
✔ reject 5 (0.119ms)
✔ stream parser rejects truncation, finish length, malformed JSON, duplicate sequence, changed final item and oversized data (6.4917ms)
ℹ tests 38
ℹ suites 0
ℹ pass 38
ℹ fail 0
ℹ cancelled 0
ℹ skipped 0
ℹ todo 0
ℹ duration_ms 29527.5584
+3
View File
@@ -711,3 +711,6 @@ D/E新增真实Runtime四项通过:原命令只执行一次且迟到桌面审
## 2026-10-31 1.16 A–F完整本地交付
最终不可变20261031114604110、来源52f0d20;独立源码519/519、24类186组历史UI、实际Electron窗口7项、原生流程9组、三工程16节点及父Node8/8、React9/9、Python10/10通过;源码与便携各7组手机流程、同载荷隔离安装/重启/卸载7组、正式桌面/390px共20页通过。正式PID36552/Provider保持,17项目/30全部会话/2590记录/4653事件、全部表每行/会话导出/工件原字节保持;1455条工件及1366个去重文件核对通过,9229关闭。iPhone 14 Pro Max/iOS 27.2为用户提供;用户确认信任、配对、原审批、差异/报告SHA、主屏幕、锁屏、切网恢复、停止及撤销,原宿主审计/进程/文件另行核对,无手机截图。正式手机网关默认关闭、无设备/证书;QA证书与设备身份没有迁入。验收手机监听已关闭并释放4321,4320管理及原数据保留。真实模型工程和Purge推理收益继续按用户选择单独待验,本轮真实模型请求为0。最终274份原字节证据封存,发行身份/QA/回顾/能力矩阵/执行计划和下一阶段方案已同步。原响应式QA失败和提前启动失败独立保留,成功来自source-retry完整独立整轮。所有378输入/22构建/40基准保持冻结,后续说明修改不重跑已通过的产品检查。下一1.17仍仅方案,未开始产品变更。
## 1.17已观测机制
Responses已采用typed Items、call_id原配对和同线路加密reasoning续接;切换Chat/其它接口不会携带私有Items。实际流式在完整终结前仅给进展,工具不执行。停止/断流有原账本与原响应工件,未知结果不自动重发。能力筛选在租约前发生;未知工具成员可直接拒绝并记录未派发。检测使用合成nonce并绑定原attempt,不凭文本应答判定工具能力。官方来源和具体边界见docs/MODEL-PROTOCOLS.md。42项恢复专项和当前构建已通过;新边界与真实渲染继续验收。
+4 -1
View File
@@ -819,4 +819,7 @@ formal-stage已成功,12:28:12Z→12:38:20Z约10分9秒;真实产品setup/
## 2026-10-31 1.17 启动
基线299fb14与干净工作区已核对,新分支feat/model-protocols-1.17。按A–F实现协议/能力、Responses适配、Chat与Responses流式、能力选池及界面/工程发行。复用原轮询/租约/粘连/冷却、DPAPI与Task/Run,不触碰正式配置。官方OpenAI迁移文档已实际读取;工具调用身份/结果配对、store:false、手工Items与typed SSE终结作为协议约束,真实模型收益单独待验。一次规划补丁因猜测progress标题整批拒绝,未发生部分写入;改为已知task_plan锚点和追加日志。只读检索误猜src/core/runtime目录已确认不存在,实际运行器为src/server/runtime.mjs。
1.17 A能力登记18/18通过(新4项+原模型中心/升级14项),旧schema1无写入迁移、能力声明不能伪造已验证,流式选择先声明;正式未动。误猜upgrade.mjs/build.mjs只读失败后通过rg定位hot-upgrade.mjs/build-server.mjs,后续不再猜路径。A回顾与原TAP保存;B/C将新增Responses实际请求、保留reasoning Items和有界终结SSE,混合协议池按各实际请求封存并保守预算。
1.17 A能力登记18/18通过(新4项+原模型中心/升级14项),旧schema1无写入迁移、能力声明不能伪造已验证,流式选择先声明;正式未动。误猜upgrade.mjs/build.mjs只读失败后通过rg定位hot-upgrade.mjs/build-server.mjs,后续不再猜路径。A回顾与原TAP保存;B/C将新增Responses实际请求、保留reasoning Items和有界终结SSE,混合协议池按各实际请求封存并保守预算。
## 1.17 B–E恢复与边界复核
A已提交578cf88。B/C/D新增协议、请求变体、SSE及能力选路,恢复后42/42相关检查和当前TypeScript/Vite/手机壳/服务构建通过,原日志protocol-current-specialists.tap与protocol-e-current-build.log保留。实际审查发现:probe持有可变连接引用,toggle会令旧修订比较失效;改为快照。检测原账本由progress.modelAttemptId绑定,避免并发任务改变最后一条请求。SSE末尾CR在EOF处补齐边界;原捕获改为Buffer,畸形UTF8也保留原字节,只有发生凭据脱敏才改变工件字节。补原停止/断开工件SHA、实际流式probe、禁用期间检测作废、超时不重发及自有CCSwitch SQLite/TOML协议导入回归。补查尚待本轮专项结果,不预判通过。
只读误猜SessionView/verify-desktop-projects等已通过rg定位,实际桌面工程入口controlled-projects --desktop。一次在Windows使用glob路径产生rg路径错误,未写入;改用实际src/core/storage/artifact-store.mjs及目录检索。递归.qa检索误带旧node_modules导致无用超长输出,后续只读顶层自有执行器。正式PID/Provider未动,真实模型请求0。
+9 -8
View File
@@ -1,6 +1,6 @@
import { createHash, randomUUID } from 'node:crypto';
import { BudgetExceeded, accountedTokens } from '../../shared/contracts/execution.mjs';
import { completionBody, requestMetrics } from '../../shared/contracts/model-request.mjs';
import { completionBody, requestMetrics, requestVariants, requestHistory, outputReservation } from '../../shared/contracts/model-request.mjs';
import { workingCandidates, workingDocument, workingEntry, workingMessage, verifyWorkingDocument } from './working-set.mjs';
import { latestToolDocument, latestToolMessage, verifyLatestToolDocument } from './tool-feedback.mjs';
import { projectMemoryMessage, MEMORY_CONTEXT_LIMITS } from '../memory/project-memory.mjs';
@@ -349,8 +349,9 @@ export function verifyPlanEvidence(store, session, plan, cache = new Map()) {
const request = get(plan.requestArtifactId, 'model-request', plan.requestSha256);
const body = JSON.parse(store.artifactStore.read(request)), metrics = requestMetrics(body);
if (metrics.bytes !== plan.requestBytes || metrics.estimatedInputTokens !== plan.estimatedInputTokens
|| body.max_tokens !== plan.budget.outputReservation || metrics.estimatedInputTokens > plan.budget.inputBudget) throw new Error('请求预算与原正文不匹配。');
historyGroups(body.messages);
|| requestVariants(body).some(value => outputReservation(value) !== plan.budget.outputReservation) || metrics.estimatedInputTokens > plan.budget.inputBudget) throw new Error('请求预算与原正文不匹配。');
historyGroups(requestHistory(body));
for (const variant of requestVariants(body)) historyGroups(requestHistory(variant));
const frozen = JSON.parse(store.artifactStore.read(get(plan.artifactId, 'context-plan')));
if (frozen.id !== plan.id || frozen.requestSha256 !== plan.requestSha256 || frozen.requestArtifactId !== plan.requestArtifactId
|| JSON.stringify(frozen.budget) !== JSON.stringify(plan.budget)) throw new Error('请求计划与发送前的封存记录不匹配。');
@@ -375,7 +376,7 @@ export function verifyPlanEvidence(store, session, plan, cache = new Map()) {
|| document.entries.length > MEMORY_CONTEXT_LIMITS.entries || Buffer.byteLength(JSON.stringify(document), 'utf8') > MEMORY_CONTEXT_LIMITS.bytes
|| document.entries.some(entry => entry.sourceFingerprint !== document.sourceFingerprint)) throw new Error('项目记忆工件归属或范围不匹配。');
const message = projectMemoryMessage(artifact, document);
if (body.messages.filter(value => value.role === 'system' && value.content === message.content).length !== 1) throw new Error('项目记忆与实际发送内容不匹配。');
if (requestHistory(body).filter(value => value.role === 'system' && value.content === message.content).length !== 1) throw new Error('项目记忆与实际发送内容不匹配。');
}
if ((frozen.skillCatalogArtifactId ?? null) !== (plan.skillCatalogArtifactId ?? null)
|| (frozen.skillCatalogSha256 ?? null) !== (plan.skillCatalogSha256 ?? null)
@@ -389,7 +390,7 @@ export function verifyPlanEvidence(store, session, plan, cache = new Map()) {
|| document.entries.length > SKILL_LIMITS.enabled || Buffer.byteLength(JSON.stringify(document), 'utf8') > SKILL_LIMITS.catalogBytes
|| new Set(document.entries.map(entry => entry.id)).size !== document.entries.length) throw new Error('技能目录归属或范围不匹配。');
const message = skillCatalogMessage(artifact, document);
if (body.messages.filter(value => value.role === 'system' && value.content === message.content).length !== 1) throw new Error('技能目录与实际发送内容不匹配。');
if (requestHistory(body).filter(value => value.role === 'system' && value.content === message.content).length !== 1) throw new Error('技能目录与实际发送内容不匹配。');
}
if ((frozen.mcpCatalogArtifactId ?? null) !== (plan.mcpCatalogArtifactId ?? null)
|| (frozen.mcpCatalogSha256 ?? null) !== (plan.mcpCatalogSha256 ?? null)
@@ -402,7 +403,7 @@ export function verifyPlanEvidence(store, session, plan, cache = new Map()) {
|| document.kind !== 'registered-mcp-catalog' || !Array.isArray(document.entries) || document.entries.length !== plan.mcpCatalogEntries
|| document.entries.length > MCP_LIMITS.allowedTools || Buffer.byteLength(JSON.stringify(document), 'utf8') > 24000) throw new Error('MCP目录归属或范围不匹配。');
const message = mcpCatalogMessage(artifact, document);
if (body.messages.filter(value => value.role === 'system' && value.content === message.content).length !== 1) throw new Error('MCP目录与实际发送内容不匹配。');
if (requestHistory(body).filter(value => value.role === 'system' && value.content === message.content).length !== 1) throw new Error('MCP目录与实际发送内容不匹配。');
}
if (plan.latestToolsArtifactId) {
const artifact = get(plan.latestToolsArtifactId, 'context-latest-tools', plan.latestToolsSha256);
@@ -410,7 +411,7 @@ export function verifyPlanEvidence(store, session, plan, cache = new Map()) {
if (artifact.taskId !== plan.taskId) throw new Error('最新工具反馈的工件任务归属不匹配。');
verifyLatestToolDocument(store, session, plan, document, historyGroups(session.providerMessages.slice(0, plan.originalMessages)));
const message = latestToolMessage(artifact, document);
if (body.messages.filter(value => value.role === 'system' && value.content === message.content).length !== 1) throw new Error('最新工具反馈与实际发送内容不匹配。');
if (requestHistory(body).filter(value => value.role === 'system' && value.content === message.content).length !== 1) throw new Error('最新工具反馈与实际发送内容不匹配。');
}
if (plan.workingSetArtifactId) {
const artifact = get(plan.workingSetArtifactId, 'context-working-set', plan.workingSetSha256);
@@ -418,7 +419,7 @@ export function verifyPlanEvidence(store, session, plan, cache = new Map()) {
if (artifact.taskId !== plan.taskId) throw new Error('工作依据工件任务归属不匹配。');
verifyWorkingDocument(store, session, plan, document, historyGroups(session.providerMessages.slice(0, plan.originalMessages)));
const message = workingMessage(artifact, document, plan.workingSetViewVersion ?? 1);
if (body.messages.filter(value => value.role === 'system' && value.content === message.content).length !== 1) throw new Error('工作依据与实际发送内容不匹配。');
if (requestHistory(body).filter(value => value.role === 'system' && value.content === message.content).length !== 1) throw new Error('工作依据与实际发送内容不匹配。');
}
if (!plan.historyIndexArtifactId) return;
const index = JSON.parse(store.artifactStore.read(get(plan.historyIndexArtifactId, 'context-history-index', plan.historyIndexSha256)));
+8 -5
View File
@@ -5,6 +5,7 @@ import { createHash, randomUUID } from 'node:crypto';
import { DatabaseSync } from 'node:sqlite';
import { parse as parseToml } from 'smol-toml';
import { modelEndpoint } from './model-hub.mjs';
import { MODEL_PROTOCOLS } from '../../shared/contracts/model-capabilities.mjs';
const digest = value => createHash('sha256').update(JSON.stringify(value)).digest('hex');
export class ConfigImporter {
@@ -13,7 +14,7 @@ export class ConfigImporter {
const root = os.homedir();
const sourcePath = path.join(root, '.cc-switch', 'cc-switch.db');
return { sources: [{ source: 'ccswitch', path: sourcePath, available: fs.existsSync(sourcePath), supported: true,
detail: '仅导入可验证的Chat Completions配置;Responses、Anthropic和客户端登录态分别标记不兼容。' },
detail: '导入已核对格式的Chat Completions/Responses API配置;Anthropic和客户端登录态当前不支持。' },
{ source: 'workbuddy', path: '', available: false, supported: false, detail: '当前尚未取得固定版本的WorkBuddy模型配置契约。可通过连接JSON导入已知API配置;不读取其登录态。' }] };
}
read(source, filename) {
@@ -25,8 +26,10 @@ export class ConfigImporter {
const value = JSON.parse(fs.readFileSync(filename, 'utf8').replace(/^\uFEFF/, ''));
if (value.schema !== 1 || !Array.isArray(value.connections) || value.connections.length > 100) throw new Error('连接JSON需要schema:1与connections数组(最多100项)。');
for (const [index, item] of value.connections.entries()) {
if (item.protocol && item.protocol !== 'chat-completions') { notices.push('第' + (index + 1) + '项协议暂不支持,已跳过。'); continue; }
rows.push({ sourceId: String(item.id ?? index), name: item.name, model: item.model, baseUrl: item.baseUrl, apiKey: item.apiKey, requiresKey: item.requiresKey !== false, brand: item.brand });
if (item.protocol && !MODEL_PROTOCOLS.includes(item.protocol)) { notices.push('第' + (index + 1) + '项协议暂不支持,已跳过。'); continue; }
rows.push({ sourceId: String(item.id ?? index), name: item.name, model: item.model, protocol: item.protocol ?? 'chat-completions',
streaming: item.streaming === true, capabilityDeclarations: item.capabilityDeclarations,
baseUrl: item.baseUrl, apiKey: item.apiKey, requiresKey: item.requiresKey !== false, brand: item.brand });
}
} else if (source === 'ccswitch') {
const db = new DatabaseSync(filename, { readOnly: true });
@@ -42,9 +45,9 @@ export class ConfigImporter {
let parsed;
try { parsed = typeof config.config === 'string' ? parseToml(config.config) : null; } catch { notices.push(provider.name + '的Codex TOML无法解析。'); continue; }
const selected = parsed?.model_providers?.[parsed.model_provider];
if (!selected?.base_url || selected.wire_api !== 'chat') { notices.push(provider.name + '使用Responses或未知协议,需对应的独立适配器。'); continue; }
if (!selected?.base_url || !['chat', 'responses'].includes(selected.wire_api)) { notices.push(provider.name + '使用未知协议,需对应的独立适配器。'); continue; }
rows.push({ sourceId: 'codex:' + provider.id, name: provider.name, baseUrl: selected.base_url, model: parsed.model,
apiKey: config.auth?.OPENAI_API_KEY, requiresKey: true, brand: 'CCSwitch' });
protocol: selected.wire_api === 'responses' ? 'responses' : 'chat-completions', apiKey: config.auth?.OPENAI_API_KEY, requiresKey: true, brand: 'CCSwitch' });
} else notices.push(provider.name + '(' + provider.app_type + ')不属于已验证的Chat Completions配置,已跳过。');
}
} finally { db.close(); }
+79 -12
View File
@@ -2,7 +2,8 @@ import { randomUUID, createHash } from 'node:crypto';
import { setTimeout as delay } from 'node:timers/promises';
import { ModelRequestError } from '../../server/diagnostics.mjs';
import { CredentialVault } from './credential-vault.mjs';
import { modelProtocol, capabilityDeclarations, capabilityView } from '../../shared/contracts/model-capabilities.mjs';
import { modelProtocol, capabilityDeclarations, capabilityView, capabilityRejections } from '../../shared/contracts/model-capabilities.mjs';
import { actualRequest, requestVariants } from '../../shared/contracts/model-request.mjs';
const legacyId = 'legacy-default';
const stamp = () => new Date().toISOString();
@@ -109,12 +110,50 @@ export class ModelHub {
status: !connection.enabled ? 'disabled' : !ready ? 'needs_key' : health?.blocked ? 'needs_attention' : health?.cooldownUntil > Date.now() ? 'cooling' : 'configured',
activeRequests: this.leases.get(connection.id) ?? 0, health: health ?? { state: 'unverified' },
streaming: connection.streaming === true, capabilitySchema: 1, capabilityDeclarations: capabilityDeclarations(undefined, connection.capabilityDeclarations),
capabilities: capabilityView(connection, credential?.revision ?? 0) };
...(this.state.capabilityResults?.[connection.id] ? { capabilityEvidence: this.state.capabilityResults[connection.id] } : {}),
capabilities: capabilityView({ ...connection, capabilityEvidence: this.state.capabilityResults?.[connection.id] }, credential?.revision ?? 0) };
}
view() { return { revision: this.state.revision, selected: this.state.selected, connections: this.connections().map(value => this.status(value)), pools: this.state.pools,
imports: Object.entries(this.state.imports).map(([id, value]) => ({ id, ...value })), attempts: this.attempts.slice(-100).reverse(), waiting: this.waiting,
storage: { credentials: this.credentialVault ? 'memory-or-windows-user-dpapi' : 'memory', restart: '临时凭据在宿主退出后需要重新填写;已明确本机加密保存的连接按用户与原配置加载。', attemptsRetained: 1000 } }; }
connection(id) { const value = this.connections().find(item => item.id === id); if (!value) throw error('模型连接不存在。', 404); return value; }
async probe(id, scope = 'text') {
if (!['text', 'tools', 'streaming'].includes(scope)) throw error('请选择文本、工具调用或流式检测。');
if (this.probing) throw error('已有模型能力检测正在执行,请等待结束。', 409);
this.probing = true;
try {
const connection = { ...this.connection(id) }, member = { ...connection, credentialRevision: this.credential(connection)?.revision ?? 0 };
if (!this.available(member)) throw error('该连接不可用,请核对凭据、冷却或停用状态。');
member.streaming = scope === 'streaming';
const nonce = randomUUID(), tools = scope === 'tools' ? [{ type: 'function', function: { name: 'workbench_capability_echo',
description: 'Return the provided nonce in this capability test. No files or commands are available.',
parameters: { type: 'object', properties: { nonce: { type: 'string' } }, required: ['nonce'], additionalProperties: false } } }] : [];
const config = { ...this.store.config, mode: 'compatible', model: connection.model, baseUrl: connection.baseUrl,
protocol: modelProtocol(connection.protocol), maxOutputTokens: 256, capabilityProbe: true, modelRouting: { type: 'connection', id: connection.id, revision: connection.revision,
strategy: 'round-robin', sticky: false, maxAttempts: 1, queueTimeoutMs: 1000, members: [member] } };
let attemptId;
const result = await this.provider.complete(config, [{ role: 'user', content: scope === 'tools' ? 'Call workbench_capability_echo with nonce ' + nonce + '. Do not reply with plain text.' : 'Reply OK.' }], null, undefined,
event => { if (event.modelAttemptId) attemptId = event.modelAttemptId; }, null, tools, { attempts: 1 });
if (scope === 'tools') {
const calls = result.message.tool_calls;
if (calls?.length !== 1 || calls[0].function.name !== 'workbench_capability_echo' || JSON.parse(calls[0].function.arguments).nonce !== nonce) throw error('服务没有返回要求的原工具调用;未标为已验证。');
} else if (!result.message.content.trim() || result.message.tool_calls?.length) throw error('服务没有返回纯文本响应;未标为已验证。');
if (this.connection(id).revision !== connection.revision || (this.credential(connection)?.revision ?? 0) !== member.credentialRevision) throw error('检测期间连接已变化,原结果保留但不更新能力结论。', 409);
this.editRegistry(() => {
this.state.capabilityResults ??= {};
const previous = this.state.capabilityResults[id];
const results = previous?.connectionRevision === connection.revision && previous.credentialRevision === member.credentialRevision ? { ...previous.results } : {};
results[scope] = 'verified';
const attempt = this.attempts.find(value => value.id === attemptId);
if (!attempt || attempt.connectionId !== id || attempt.status !== 'completed') throw error('检测原请求账本不完整,未更新能力结论。');
this.state.capabilityResults[id] = { schema: 1, connectionRevision: connection.revision, credentialRevision: member.credentialRevision, results,
checkedAt: stamp(), protocol: modelProtocol(connection.protocol), attemptId: attempt?.id, source: 'explicit-model-probe',
scope: 'Only the requested synthetic capability; no project files, commands or real-model engineering benefit.' };
this.save();
});
return { ok: true, usage: result.usage, scope, center: this.view() };
} finally { this.probing = false; }
}
upsert(input) {
if (!this.editingRegistry) return this.editRegistry(() => this.upsert(input));
if (input.id === legacyId) throw error('原连接请通过模型连接设置修改。');
@@ -183,10 +222,10 @@ export class ModelHub {
const selection = this.state.selected;
const pool = selection.type === 'pool' ? this.state.pools.find(value => value.id === selection.id) : null;
if (selection.type === 'pool' && !pool) throw error('选中的模型池不存在。');
const members = (pool?.members ?? [{ connectionId: selection.id, weight: 1 }]).map(value => ({ ...this.connection(value.connectionId), weight: value.weight,
const members = (pool?.members ?? [{ connectionId: selection.id, weight: 1 }]).map(value => ({ ...this.connection(value.connectionId), capabilityEvidence: this.state.capabilityResults?.[value.connectionId], weight: value.weight,
credentialRevision: this.credential(this.connection(value.connectionId))?.revision ?? 0 }));
if (!members.some(value => this.status(value).status === 'configured' || this.status(value).status === 'cooling')) throw error('没有可用模型连接,请核对启用状态、临时凭据和故障。');
return { ...config, baseUrl: members[0].baseUrl, model: members[0].model,
return { ...config, baseUrl: members[0].baseUrl, model: members[0].model, protocol: modelProtocol(members[0].protocol), streaming: members[0].streaming === true,
contextLimit: Math.min(config.contextLimit, ...members.map(value => value.contextLimit)), maxOutputTokens: Math.min(config.maxOutputTokens, ...members.map(value => value.maxOutputTokens)),
modelRouting: { type: selection.type, id: selection.id, revision: pool?.revision ?? members[0].revision,
strategy: pool?.strategy ?? 'round-robin', sticky: pool?.sticky ?? true, maxAttempts: pool?.maxAttempts ?? 1, queueTimeoutMs: pool?.queueTimeoutMs ?? 15000, members } };
@@ -197,13 +236,15 @@ export class ModelHub {
const health = this.health.get(this.identity(member));
return (!member.requiresKey || !!this.credential(member)?.key) && !health?.blocked && !(health?.cooldownUntil > Date.now());
}
async acquire(routing, runId, excluded, signal, deadline) {
async acquire(routing, runId, excluded, signal, deadline, requirements = []) {
if (this.waiting >= 100) throw error('模型请求队列已满,请稍后继续任务。');
this.waiting++;
try {
while (true) {
signal?.throwIfAborted();
const healthy = routing.members.filter(value => this.available(value) && !excluded.has(value.id));
const eligible = routing.members.filter(value => capabilityRejections(value, requirements, value.credentialRevision).length === 0);
if (!eligible.length) throw Object.assign(error('模型池没有满足本次任务能力的成员;请核对工具调用和流式能力声明。'), { rejections: routing.members.flatMap(value => capabilityRejections(value, requirements, value.credentialRevision)) });
const healthy = eligible.filter(value => this.available(value) && !excluded.has(value.id));
const ready = healthy.filter(value => (this.leases.get(value.id) ?? 0) < value.concurrency);
if (ready.length) {
const remembered = routing.sticky && this.sticky.get(runId);
@@ -231,18 +272,28 @@ export class ModelHub {
async completeOwned(config, body, signal, onProgress, plan, execute) {
const routing = config.modelRouting, excluded = new Set(), deadline = Date.now() + Math.min(routing.queueTimeoutMs, 60000);
const requestId = plan?.id ?? randomUUID(), runId = plan?.runId ?? requestId;
const requirements = config.capabilityProbe ? [] : ['text', ...(requestVariants(body).some(value => value.tools?.length) ? ['tools'] : [])];
const rejections = config.capabilityProbe ? [] : routing.members.flatMap(value => capabilityRejections(value, [...requirements, ...(value.streaming ? ['streaming'] : [])], value.credentialRevision));
const eligibleRouting = { ...routing, members: routing.members.filter(value => !rejections.some(item => item.connectionId === value.id)) };
if (!eligibleRouting.members.length) {
const attempt = { id: randomUUID(), requestId, taskId: plan?.taskId ?? null, runId, sessionId: plan?.sessionId ?? null, projectId: plan?.projectId ?? null,
poolId: routing.type === 'pool' ? routing.id : null, status: 'rejected', reason: '所有成员均不满足本次任务所需能力。', rejections, startedAt: stamp(), finishedAt: stamp(), usage: null, usageStatus: 'not_dispatched' };
this.attempts.push(attempt); this.saveAttempts();
throw error('模型池没有满足本次任务能力的成员;请核对能力声明或选择其它连接。');
}
let lastError;
for (let count = 1; count <= routing.maxAttempts; count++) {
signal?.throwIfAborted();
let lease;
try { lease = await this.acquire(routing, runId, excluded, signal, deadline); }
try { lease = await this.acquire(eligibleRouting, runId, excluded, signal, deadline, requirements); }
catch (cause) { throw lastError ?? cause; }
const member = lease.member, credential = this.credential(member);
const actualBody = { ...body, model: member.model };
const actualBody = actualRequest(body, member);
const bytes = JSON.stringify(actualBody);
const attempt = { id: randomUUID(), requestId, taskId: plan?.taskId ?? null, runId, sessionId: plan?.sessionId ?? null, projectId: plan?.projectId ?? null,
connectionId: member.id, connectionRevision: member.revision, credentialRevision: member.credentialRevision, poolId: routing.type === 'pool' ? routing.id : null,
poolRevision: routing.revision, model: member.model, endpoint: member.baseUrl, ordinal: count, status: 'running', startedAt: stamp(),
poolRevision: routing.revision, model: member.model, protocol: modelProtocol(member.protocol), streaming: member.streaming === true, endpoint: member.baseUrl, ordinal: count, status: 'running', startedAt: stamp(),
requestBytes: Buffer.byteLength(bytes, 'utf8'), requirements, rejections,
requestSha256: createHash('sha256').update(bytes).digest('hex'), usage: null, usageStatus: 'unknown' };
try {
if (plan?.sessionId) {
@@ -253,14 +304,30 @@ export class ModelHub {
this.store.save(session);
}
this.attempts.push(attempt); this.saveAttempts();
const progress = event => onProgress?.({ ...event, attempt: count, maxAttempts: routing.maxAttempts, connectionId: member.id, connectionName: member.name, model: member.model, modelAttemptId: attempt.id });
const result = await execute({ ...config, baseUrl: member.baseUrl, model: member.model }, credential?.key ?? '', actualBody, progress);
const progress = event => {
if (event.streamCapture !== undefined) {
attempt.stream = event.stream;
if (plan?.sessionId) {
const session = this.store.session(plan.sessionId), run = this.store.owned('run', plan.runId, session.id);
const raw = event.streamCapture, decoded = raw.toString('utf8'), redacted = this.provider.redact(decoded);
const captured = redacted === decoded ? raw : Buffer.from(redacted, 'utf8');
const artifact = this.store.artifactStore.write(session, run, captured, { kind: 'model-stream-response', mediaType: 'text/event-stream' });
this.store.core.artifact.set(artifact.id, artifact); attempt.responseArtifactId = artifact.id;
attempt.responseSha256 = artifact.sha256; attempt.responseRedacted = redacted !== decoded; this.store.save(session);
}
return;
}
if (event.streaming) attempt.partial = { sequence: event.streamSequence, bytes: event.streamBytes, textCharacters: event.textCharacters, textPreview: this.provider.redact(event.textPreview ?? '').slice(0, 8000), toolFragments: event.toolFragments };
if (event.stream) attempt.stream = event.stream;
onProgress?.({ ...event, attempt: count, maxAttempts: routing.maxAttempts, connectionId: member.id, connectionName: member.name, protocol: modelProtocol(member.protocol), model: member.model, modelAttemptId: attempt.id });
};
const result = await execute({ ...config, baseUrl: member.baseUrl, model: member.model, protocol: modelProtocol(member.protocol), streaming: member.streaming === true }, credential?.key ?? '', actualBody, progress);
attempt.status = 'completed'; attempt.usage = result.usage ?? null; attempt.usageStatus = result.usage ? 'reported' : 'unknown';
this.health.set(this.identity(member), { state: 'responded', lastSuccessAt: stamp(), cooldownUntil: 0, failures: 0 });
return result;
} catch (cause) {
const diagnostic = cause.diagnostic;
attempt.status = signal?.aborted ? 'interrupted' : 'failed';
attempt.status = signal?.aborted ? 'interrupted' : diagnostic?.outcome === 'unknown' ? 'unknown' : 'failed';
attempt.reason = this.provider.redact(cause.message).slice(0, 600); attempt.httpStatus = diagnostic?.status ?? diagnostic?.httpStatus ?? null;
const health = this.health.get(this.identity(member)) ?? {};
if (signal?.aborted) {
+128
View File
@@ -0,0 +1,128 @@
import { createHash } from 'node:crypto';
import { protocolFailure, normalizeProtocolResponse, validateModelResponse } from './protocols.mjs';
// Only a fully terminated and validated stream produces a dispatchable message.
// Progress is cumulative, bounded and explicitly not a tool invocation.
export async function readModelStream(response, config, signal, onProgress) {
if (!response.headers.get('content-type')?.toLowerCase().includes('text/event-stream') || !response.body) throw protocolFailure('服务未返回所选协议的事件流,请核对流式配置。');
const reader = response.body.getReader(), decoder = new TextDecoder('utf-8', { fatal: true }), hash = createHash('sha256');
const chunks = [];
let buffer = '', data = [], bytes = 0, frames = 0, sequence = -1, text = '', done = false, terminal = null, usage = null, finish = null;
let lastProgress = 0, responseId = null, validated = false;
const calls = new Map(), responseCalls = new Map();
const progress = force => {
if (!force && Date.now() - lastProgress < 100) return;
lastProgress = Date.now();
onProgress?.({ phase: 'running', streaming: true, protocol: config.protocol ?? 'chat-completions', streamSequence: frames,
streamBytes: bytes, textPreview: text.slice(0, 8000), textCharacters: text.length, toolFragments: Math.max(calls.size, responseCalls.size) });
};
const processData = raw => {
if (terminal || done) throw protocolFailure('模型流在终结后仍包含数据,未采用响应。');
frames++;
if (frames > 100000) throw protocolFailure('模型事件流超过事件数量上限。');
if (raw === '[DONE]') { if (config.protocol === 'responses') throw protocolFailure('Responses缺少正式终结事件。'); done = true; return; }
let event;
try { event = JSON.parse(raw); } catch { throw protocolFailure('模型事件流包含无效JSON。'); }
if (event.error || event.type === 'error') throw protocolFailure('模型流返回错误,已收到片段保留且不会自动重发。', 'stream_interrupted');
if (config.protocol === 'responses') {
if (event.sequence_number !== undefined) {
if (!Number.isSafeInteger(event.sequence_number) || event.sequence_number <= sequence) throw protocolFailure('Responses事件序列重复或逆序。');
sequence = event.sequence_number;
}
if (event.type === 'response.created') {
if (!event.response?.id || responseId) throw protocolFailure('Responses流创建身份无效。'); responseId = event.response.id;
} else if (event.type === 'response.output_text.delta') {
if (typeof event.delta !== 'string') throw protocolFailure('Responses文本片段格式无效。'); text += event.delta;
} else if (event.type === 'response.output_item.added' && event.item?.type === 'function_call') {
const item = event.item;
if (!item.id || !item.call_id || !item.name || responseCalls.has(item.id)) throw protocolFailure('Responses流工具身份重复或缺失。');
responseCalls.set(item.id, { id: item.id, call_id: item.call_id, name: item.name, arguments: item.arguments ?? '' });
} else if (event.type === 'response.function_call_arguments.delta') {
const call = responseCalls.get(event.item_id);
if (!call || typeof event.delta !== 'string') throw protocolFailure('Responses工具片段缺少所属原调用。'); call.arguments += event.delta;
} else if (event.type === 'response.function_call_arguments.done') {
const call = responseCalls.get(event.item_id);
if (!call || call.arguments !== event.arguments) throw protocolFailure('Responses终结参数与原片段不一致。');
} else if (event.type === 'response.completed') {
if (!event.response || responseId && event.response.id !== responseId) throw protocolFailure('Responses终结身份与创建身份不一致。');
terminal = event.response;
} else if (['response.failed', 'response.incomplete', 'response.cancelled'].includes(event.type)) throw protocolFailure('Responses流未完整结束,工具未执行。', 'stream_interrupted');
} else {
if (!Array.isArray(event.choices)) throw protocolFailure('Chat流缺少choices数组。');
if (event.usage !== undefined && event.usage !== null) usage = event.usage;
for (const choice of event.choices) {
if (choice.index !== 0) throw protocolFailure('当前只接入单个Chat候选流。');
const delta = choice.delta;
if (!delta || typeof delta !== 'object') throw protocolFailure('Chat流缺少delta。');
if (finish && (delta.content || delta.tool_calls?.length)) throw protocolFailure('Chat流在完成后新增了内容。');
if (delta.content !== undefined && delta.content !== null) {
if (typeof delta.content !== 'string') throw protocolFailure('Chat文本片段格式无效。'); text += delta.content;
}
if (delta.refusal) text += delta.refusal;
if (delta.tool_calls !== undefined && !Array.isArray(delta.tool_calls)) throw protocolFailure('Chat流工具片段格式无效。');
for (const item of delta.tool_calls ?? []) {
if (!Number.isSafeInteger(item.index) || item.index < 0 || item.index >= 12) throw protocolFailure('Chat流工具索引无效。');
const call = calls.get(item.index) ?? { id: '', type: 'function', function: { name: '', arguments: '' } };
if (item.id) { if (call.id && call.id !== item.id) throw protocolFailure('Chat流工具身份发生变化。'); call.id = item.id; }
if (item.type && item.type !== 'function') throw protocolFailure('Chat流工具类型未接入。');
if (item.function?.name) call.function.name += item.function.name;
if (item.function?.arguments !== undefined) {
if (typeof item.function.arguments !== 'string') throw protocolFailure('Chat流工具参数格式无效。'); call.function.arguments += item.function.arguments;
}
calls.set(item.index, call);
}
if (choice.finish_reason) { if (finish && finish !== choice.finish_reason) throw protocolFailure('Chat流终结原因发生变化。'); finish = choice.finish_reason; }
}
}
if (calls.size > 12 || responseCalls.size > 12) throw protocolFailure('模型流工具数量超出上限。');
progress(false);
};
const line = value => {
if (value === '') { if (data.length) { processData(data.join('\n')); data = []; } }
else if (value.startsWith('data:')) data.push(value.slice(5).replace(/^ /, ''));
};
try {
while (true) {
signal?.throwIfAborted();
const chunk = await reader.read();
signal?.throwIfAborted();
if (chunk.done) break;
bytes += chunk.value.length; hash.update(chunk.value);
if (bytes > 4 * 1024 * 1024) throw protocolFailure('模型事件流超过4MB读取上限。');
chunks.push(Buffer.from(chunk.value));
buffer += decoder.decode(chunk.value, { stream: true });
// CRLF, LF and CR line endings, including a CR split across chunks.
while (true) {
const at = buffer.search(/[\r\n]/); if (at < 0 || buffer[at] === '\r' && at === buffer.length - 1) break;
const width = buffer[at] === '\r' && buffer[at + 1] === '\n' ? 2 : 1;
line(buffer.slice(0, at)); buffer = buffer.slice(at + width);
}
}
buffer += decoder.decode();
// A final CR is a complete SSE line ending once EOF rules out CRLF.
if (buffer.endsWith('\r')) { line(buffer.slice(0, -1)); buffer = ''; }
if (buffer || data.length) throw protocolFailure('模型流在完整事件边界前断开,未采用片段。', 'stream_interrupted');
let value;
if (config.protocol === 'responses') {
if (!terminal) throw protocolFailure('Responses流断开或缺少终结事件。', 'stream_interrupted');
value = normalizeProtocolResponse(config, terminal);
const received = value.choices[0].message;
if (text && received.content !== text) throw protocolFailure('Responses终结文本与原片段不一致。');
for (const call of responseCalls.values()) {
const final = terminal.output.find(item => item.id === call.id && item.type === 'function_call');
if (!final || final.call_id !== call.call_id || final.name !== call.name || final.arguments !== call.arguments) throw protocolFailure('Responses终结工具与原片段不一致。');
}
value = terminal;
} else {
if (!done || !['stop', 'tool_calls'].includes(finish)) throw protocolFailure('Chat流未完整终结或达到输出上限,未执行工具。', 'stream_interrupted');
value = { choices: [{ message: { role: 'assistant', content: text, ...(calls.size ? { tool_calls: [...calls.entries()].sort((a, b) => a[0] - b[0]).map(([, item]) => item) } : {}) }, finish_reason: finish }], usage };
}
validateModelResponse(config.protocol === 'responses' ? normalizeProtocolResponse(config, value) : value);
validated = true; progress(true);
return { value, evidence: { bytes, frames, sha256: hash.copy().digest('hex'), terminal: true, protocol: config.protocol ?? 'chat-completions' } };
} finally {
try { await reader.cancel(); } catch {} reader.releaseLock();
onProgress?.({ streamCapture: Buffer.concat(chunks), stream: { bytes, capturedBytes: chunks.reduce((sum, item) => sum + item.length, 0), frames,
sha256: hash.digest('hex'), terminal: validated, terminalSeen: !!terminal || done, protocol: config.protocol ?? 'chat-completions' } });
}
}
+43
View File
@@ -0,0 +1,43 @@
import { ModelRequestError } from '../../server/diagnostics.mjs';
export const protocolFailure = (message, kind = 'protocol') => new ModelRequestError({ kind, source: 'model_connection', stage: 'model_request',
title: '模型响应未通过完整性核对', message, retryable: false, occurredAt: new Date().toISOString() });
export const protocolRoute = config => config.protocol === 'responses' ? '/responses' : '/chat/completions';
export function validateModelResponse(value) {
const choice = value.choices?.[0], message = choice?.message;
if (!message || choice.finish_reason === 'length' || choice.finish_reason === 'content_filter') throw protocolFailure('模型响应缺少完整message或没有完整结束,未执行工具。');
const calls = message.tool_calls;
if (calls !== undefined && (!Array.isArray(calls) || calls.length > 12)) throw protocolFailure('模型工具调用数量或格式无效。');
const ids = new Set();
for (const call of calls ?? []) {
if (!call.id || typeof call.id !== 'string' || ids.has(call.id) || call.type !== 'function' || typeof call.function?.name !== 'string' || !call.function.name || typeof call.function.arguments !== 'string') throw protocolFailure('模型工具身份或参数无效。');
ids.add(call.id);
let args; try { args = JSON.parse(call.function.arguments); } catch { throw protocolFailure('模型工具参数不是完整JSON,未执行工具。'); }
if (!args || typeof args !== 'object' || Array.isArray(args)) throw protocolFailure('模型工具参数必须为完整对象。');
}
return value;
}
export function normalizeProtocolResponse(config, value) {
if (config.protocol !== 'responses') return value;
if (value.status !== 'completed' || !Array.isArray(value.output)) throw protocolFailure('Responses未完整结束,原响应未用于执行工具。');
const calls = [], texts = [], ids = new Set();
for (const item of value.output) {
if (item.type === 'function_call') {
if (item.status && item.status !== 'completed') throw protocolFailure('Responses工具参数未完成。');
if (typeof item.call_id !== 'string' || !item.call_id || ids.has(item.call_id) || typeof item.name !== 'string' || !item.name || typeof item.arguments !== 'string') throw protocolFailure('Responses工具调用身份或参数无效。');
ids.add(item.call_id); calls.push({ id: item.call_id, type: 'function', function: { name: item.name, arguments: item.arguments } });
} else if (item.type === 'message') {
if (item.role !== 'assistant' || item.status && item.status !== 'completed' || !Array.isArray(item.content)) throw protocolFailure('Responses消息尚未完成或格式无效。');
for (const content of item.content) {
if (content.type === 'output_text' && typeof content.text === 'string') texts.push(content.text);
else if (content.type === 'refusal' && typeof content.refusal === 'string') texts.push(content.refusal);
else throw protocolFailure('Responses包含当前尚未接入的输出内容类型。');
}
} else if (item.type !== 'reasoning') throw protocolFailure('Responses包含当前尚未接入的服务端工具或输出类型。');
}
const raw = value.usage, usage = raw ? { prompt_tokens: raw.input_tokens, completion_tokens: raw.output_tokens,
...(raw.total_tokens !== undefined ? { total_tokens: raw.total_tokens } : {}), raw, protocol: 'responses' } : null;
return { choices: [{ message: { role: 'assistant', content: texts.join(''), ...(calls.length ? { tool_calls: calls } : {}),
providerState: { protocol: 'responses', endpoint: config.baseUrl, model: config.model, responseId: value.id ?? null, items: value.output } }, finish_reason: calls.length ? 'tool_calls' : 'stop' }], usage };
}
+1 -1
View File
@@ -22,7 +22,7 @@ const at = () => new Date().toISOString();
const sourceIdentity = source => Object.fromEntries(['root', 'head', 'reference', 'branch', 'gitDir', 'commonDir', 'indexSha256',
'statusSha256', 'sourceFingerprint', 'treeSha256', 'configuration', 'filterDrivers'].map(key => [key, source[key]]));
const configView = config => Object.fromEntries(['mode', 'baseUrl', 'model', 'contextLimit', 'maxOutputTokens', 'configRevision',
'maxSteps', 'maxRunDurationMs', 'maxRunTokens', 'maxTaskTokens', ...(config.modelRouting ? ['modelRouting'] : [])].map(key => [key, structuredClone(config[key])]));
'maxSteps', 'maxRunDurationMs', 'maxRunTokens', 'maxTaskTokens', ...(config.protocol ? ['protocol', 'streaming'] : []), ...(config.modelRouting ? ['modelRouting'] : [])].map(key => [key, structuredClone(config[key])]));
const alive = pid => { if (!pid) return false; try { process.kill(pid, 0); return true; } catch (error) { return error.code === 'EPERM'; } };
export class WorkerManager {
+1 -11
View File
@@ -176,17 +176,7 @@ return async (request, response) => {
if (route === '/api/model-center/import-toggle') return json(response, importer().toggle(input.id, input.enabled));
if (route === '/api/model-center/import-sync') return json(response, { results: importer().sync(), center: hub.view() });
if (route === '/api/model-center/probe') {
const connection = hub.connection(input.id);
if (!hub.available({ ...connection, credentialRevision: hub.credential(connection)?.revision ?? 0 })) throw new Error('该连接不可用,请核对凭据、冷却或停用状态。');
const config = { ...store.config, mode: 'compatible', modelRouting: { type: 'connection', id: connection.id, revision: connection.revision,
strategy: 'round-robin', sticky: false, maxAttempts: 1, queueTimeoutMs: 1000, members: [{ ...connection, credentialRevision: hub.credential(connection)?.revision ?? 0 }] } };
const result = await hub.complete(config, { model: connection.model, stream: false, max_tokens: 16, messages: [{ role: 'user', content: 'Reply OK.' }] }, undefined, undefined, null,
async (selected, key, payload) => {
const value = await provider.request(selected, '/chat/completions', payload, undefined, key, undefined, { attempts: 1 });
if (!value.choices?.[0]?.message) throw new Error('模型响应缺少完整message。');
return value;
});
return json(response, { ok: true, usage: result.usage ?? null, center: hub.view() });
return json(response, await hub.probe(input.id, input.scope ?? 'text'));
}
}
}
+22 -4
View File
@@ -4,6 +4,8 @@ import { setTimeout as delay } from 'node:timers/promises';
import { executionEnvironment, ModelRequestError, connectionFailure, httpFailure } from './diagnostics.mjs';
import { completionBody, assertPlannedRequest } from '../shared/contracts/model-request.mjs';
import { ModelHub } from '../core/models/model-hub.mjs';
import { normalizeProtocolResponse, protocolRoute, validateModelResponse } from '../core/models/protocols.mjs';
import { readModelStream } from '../core/models/model-stream.mjs';
export const engineToolDefinitions = [
{ type: 'function', function: { name: 'inspect_engine_checks', description: '读取当前Task已经保存的外部引擎连接核验:真实安装版本、启动完成信号、SDK会话、进程关闭、原报告工件和能力限制。不会启动引擎或发送prompt;完整原记录通过已有read_artifact读取。', parameters: { type: 'object', properties: {}, additionalProperties: false } } },
@@ -145,9 +147,17 @@ export class Provider {
const requestSignal = signal ? AbortSignal.any([signal, timeout]) : timeout;
const context = { endpoint: base, model: config.model, route, attempt };
onProgress?.({ phase: 'running', attempt, maxAttempts: policy.attempts, endpoint: base, route });
let readingStream = false;
try {
const response = await (this.fetcher ?? fetch)(base + route, { method: body ? 'POST' : 'GET', headers,
body: body ? JSON.stringify(body) : undefined, signal: requestSignal });
if (response.ok && body?.stream === true) {
readingStream = true;
const streamed = await readModelStream(response, config, requestSignal, progress => onProgress?.({ ...progress, attempt, maxAttempts: policy.attempts }));
this.lastRequest = { source: 'model_connection', endpoint: base, route, model: config.model, status: 'completed', attempts: attempt, durationMs: Date.now() - startedAt, stream: streamed.evidence };
onProgress?.({ phase: 'completed', attempt, maxAttempts: policy.attempts, durationMs: Date.now() - startedAt, stream: streamed.evidence });
return streamed.value;
}
const text = await boundedResponse(response);
if (!response.ok) {
let message = text;
@@ -171,7 +181,9 @@ export class Provider {
return result;
} catch (error) {
signal?.throwIfAborted();
const diagnostic = connectionFailure(error, { ...context, timedOut: timeout.aborted, redact: value => this.redact(value, key) });
let diagnostic = connectionFailure(error, { ...context, timedOut: timeout.aborted, redact: value => this.redact(value, key) });
if (readingStream) diagnostic = { ...diagnostic, retryable: false, outcome: 'unknown', kind: timeout.aborted ? 'timeout' : diagnostic.kind === 'protocol' ? 'protocol' : 'stream_interrupted',
message: diagnostic.message + ' 已接收的流片段保留,请求不会自动重发。' };
const remaining = deadline - Date.now();
const waitMs = diagnostic.retryAfterMs ?? policy.delays[Math.min(attempt - 1, policy.delays.length - 1)] ?? 500;
const retry = diagnostic.retryable && attempt < policy.attempts && remaining > waitMs;
@@ -201,10 +213,10 @@ export class Provider {
if (config.mode === 'demo') return this.demo(messages, project);
this.validate(config);
const execute = async (selected, key, input, progress) => {
const value = await this.request(selected, '/chat/completions', input, signal, key, progress, { ...(config.modelRouting ? { attempts: 1 } : {}), ...requestPolicy });
const value = normalizeProtocolResponse(selected, await this.request(selected, protocolRoute(selected), input, signal, key, progress, { ...(config.modelRouting ? { attempts: 1 } : {}), ...requestPolicy }));
if (!value.choices?.[0]?.message || value.choices[0].finish_reason === 'length') throw new ModelRequestError({ kind: 'protocol', source: 'model_connection',
stage: 'model_request', title: '模型响应不完整', message: '模型响应缺少完整message或达到输出上限,未执行工具。', retryable: false, occurredAt: new Date().toISOString() });
return value;
return validateModelResponse(value);
};
const result = config.modelRouting && this.hub ? await this.hub.complete(config, body, signal, onProgress, requestPlan, execute)
: await execute(config, this.apiKey, body, onProgress);
@@ -213,11 +225,17 @@ export class Provider {
if (result.choices[0].finish_reason === 'length') throw new Error('模型输出达到长度上限,未执行未完整的工具请求。请提高输出上限或缩小任务。');
if (received.tool_calls !== undefined && (!Array.isArray(received.tool_calls) || received.tool_calls.length > 12)) throw new Error('模型返回的工具调用数量或格式无效。');
const calls = (received.tool_calls ?? []).map(call => ({
id: typeof call.id === 'string' && call.id ? call.id : 'call_' + randomUUID().replaceAll('-', ''),
id: typeof call.id === 'string' && call.id ? call.id : '',
type: 'function', function: { name: String(call.function?.name ?? ''),
arguments: typeof call.function?.arguments === 'string' ? call.function.arguments : JSON.stringify(call.function?.arguments ?? {}) },
}));
if (calls.some(call => !call.id || !call.function.name)) throw new Error('模型工具调用缺少原身份或名称,未执行工具。');
for (const call of calls) {
let args; try { args = JSON.parse(call.function.arguments); } catch { throw new Error('模型工具参数不是完整JSON,未执行工具。'); }
if (!args || typeof args !== 'object' || Array.isArray(args)) throw new Error('模型工具参数必须是完整对象,未执行工具。');
}
return { message: { role: 'assistant', content: typeof received.content === 'string' ? received.content : '',
...(received.providerState ? { providerState: received.providerState } : {}),
...(calls.length ? { tool_calls: calls } : {}) }, usage: result.usage ?? null, finishReason: result.choices[0].finish_reason ?? null };
}
demo(messages, project) {
+6 -1
View File
@@ -284,12 +284,16 @@ export class Runtime {
planned.record.requestAttempts = Math.max(planned.record.requestAttempts, progress.attempt ?? 0);
stepRecord.modelAttempts = Math.max(stepRecord.modelAttempts, progress.attempt ?? 0);
requestEvent.phase = progress.phase; requestEvent.attempts = progress.attempt;
if (progress.streaming) requestEvent.stream = { status: 'receiving', protocol: progress.protocol, sequence: progress.streamSequence,
bytes: progress.streamBytes, textCharacters: progress.textCharacters, textPreview: this.provider.redact(progress.textPreview ?? '').slice(0, 8000), toolFragments: progress.toolFragments };
if (progress.stream) requestEvent.stream = { ...requestEvent.stream, ...progress.stream, status: progress.stream.terminal ? 'completed' : 'receiving' };
if (progress.phase === 'failed' && requestEvent.stream) requestEvent.stream.status = 'incomplete';
if (progress.connectionId) { requestEvent.connectionId = progress.connectionId; requestEvent.modelAttemptId = progress.modelAttemptId; requestEvent.actualModel = progress.model; }
requestEvent.label = progress.switching ? '正在切换模型池连接' : progress.phase === 'retrying' ? '正在重试模型连接' : progress.phase === 'completed' ? '模型响应已收到' : progress.phase === 'failed' ? '模型连接失败' : '请求模型服务';
if (externalEngine) requestEvent.label = progress.phase === 'completed' ? '引擎原结果已收到' : '受控引擎执行';
requestEvent.output = progress.phase === 'retrying' ? '第 ' + progress.attempt + ' 次请求失败,将自动重试(最多 ' + progress.maxAttempts + ' 次)。'
: progress.phase === 'completed' ? '模型响应已收到;工具请求将在校验后执行。'
: progress.diagnostic?.message ?? '正在等待模型响应。';
: progress.streaming ? '正在接收模型输出,工具参数完整核对前不会执行。' : progress.diagnostic?.message ?? '正在等待模型响应。';
if (externalEngine) requestEvent.output = progress.phase === 'completed' ? '原引擎工具请求或最终输出已返回,后续按所属任务核验。' : '等待所属引擎;实际模型尝试另有记录。';
if (progress.diagnostic) requestEvent.diagnostic = progress.diagnostic;
if (progress.durationMs !== undefined) requestEvent.durationMs = progress.durationMs;
@@ -305,6 +309,7 @@ export class Runtime {
planned.record.workerUsageAccounted = true; this.store.save(session); responseAccounted = true;
}
} catch (error) {
if (requestEvent?.stream && requestEvent.stream.status !== 'completed') { requestEvent.stream.status = signal.aborted ? 'interrupted' : 'incomplete'; requestEvent.phase = signal.aborted ? 'interrupted' : 'failed'; requestEvent.finishedAt = now(); }
this.requestPlanner().finish(session, planned.record, { error: Object.assign(new Error(this.provider.redact(error.message ?? String(error))), { name: signal.aborted ? 'AbortError' : error.name }), attempts: planned.record.requestAttempts });
if (run.worker && requestStarted && !responseAccounted) {
this.store.recordResponse(session, run.record, null, planned.record.estimatedInputTokens + config.maxOutputTokens);
+1
View File
@@ -11,6 +11,7 @@ export function modelSnapshot(config) {
mode: config.mode, baseUrl: config.baseUrl, model: config.model,
contextLimit: config.contextLimit, maxOutputTokens: config.maxOutputTokens,
configRevision: config.configRevision ?? 1,
...(config.protocol ? { protocol: config.protocol, streaming: config.streaming === true } : {}),
...(config.modelRouting ? { routing: structuredClone(config.modelRouting) } : {}),
};
}
+56 -3
View File
@@ -1,15 +1,68 @@
import { createHash } from 'node:crypto';
import { modelProtocol } from './model-capabilities.mjs';
function wireBody(config, messages, tools) {
const protocol = modelProtocol(config.protocol), stream = config.streaming === true;
const cleanMessages = messages.map(({ providerState, ...message }) => message);
if (protocol === 'chat-completions') return { model: config.model, messages: cleanMessages, tools, tool_choice: 'auto', max_tokens: config.maxOutputTokens, stream,
...(stream ? { stream_options: { include_usage: true } } : {}) };
const input = messages.flatMap(message => {
if (message.role === 'tool') return [{ type: 'function_call_output', call_id: message.tool_call_id, output: message.content }];
if (message.role === 'assistant') {
if (message.providerState?.protocol === 'responses' && message.providerState.endpoint === config.baseUrl && message.providerState.model === config.model) return message.providerState.items;
return [...(message.content ? [{ role: 'assistant', content: message.content }] : []), ...(message.tool_calls ?? []).map(call => ({
type: 'function_call', call_id: call.id, name: call.function.name, arguments: call.function.arguments }))];
}
return [{ role: message.role, content: message.content }];
});
return { model: config.model, input, tools: (tools ?? []).map(tool => ({ type: 'function', ...tool.function, strict: false })),
tool_choice: 'auto', max_output_tokens: config.maxOutputTokens, stream, store: false, include: ['reasoning.encrypted_content'] };
}
export function completionBody(config, messages, tools) {
return { model: config.model, messages, tools, tool_choice: 'auto', max_tokens: config.maxOutputTokens, stream: false };
const members = config.modelRouting?.members;
if (members?.some(member => member.protocol === 'responses' || member.streaming === true)) {
return { schema: 1, kind: 'model-route-plan', messages, variants: members.map(member => ({ connectionId: member.id,
protocol: modelProtocol(member.protocol), body: wireBody({ ...config, ...member, maxOutputTokens: config.maxOutputTokens }, messages, tools) })) };
}
return wireBody(config, messages, tools);
}
export function requestVariants(body) {
return body.kind === 'model-route-plan' ? body.variants.map(value => value.body) : [body];
}
export function actualRequest(body, member) {
if (body.kind !== 'model-route-plan') return { ...body, model: member.model };
const variant = body.variants.find(value => value.connectionId === member.id);
if (!variant || variant.protocol !== modelProtocol(member.protocol) || variant.body.model !== member.model) throw new Error('实际连接与已封存协议请求不一致。');
return structuredClone(variant.body);
}
export const outputReservation = body => body.max_output_tokens ?? body.max_tokens;
export function requestHistory(body) {
if (body.messages) return body.messages;
const messages = [];
for (const item of body.input ?? []) {
if (item.type === 'reasoning') continue;
if (item.type === 'function_call_output') messages.push({ role: 'tool', tool_call_id: item.call_id, content: item.output });
else if (item.type === 'function_call') {
if (!messages.at(-1)?.tool_calls) messages.push({ role: 'assistant', content: '', tool_calls: [] });
messages.at(-1).tool_calls.push({ id: item.call_id, type: 'function', function: { name: item.name, arguments: item.arguments } });
} else messages.push({ role: item.role, content: typeof item.content === 'string' ? item.content : JSON.stringify(item.content) });
}
return messages;
}
export function requestMetrics(body) {
const serialized = JSON.stringify(body);
const bytes = Buffer.byteLength(serialized, 'utf8');
const variants = requestVariants(body);
return { bytes, sha256: createHash('sha256').update(serialized, 'utf8').digest('hex'),
estimatedInputTokens: Math.ceil(bytes / 3) + body.messages.length * 8 + (body.tools?.length ?? 0) * 16,
estimateMethod: 'utf8-third-plus-framing-v1' };
estimatedInputTokens: Math.max(...variants.map(value => Math.ceil(Buffer.byteLength(JSON.stringify(value), 'utf8') / 3)
+ (value.messages?.length ?? value.input?.length ?? 0) * 8 + (value.tools?.length ?? 0) * 16)),
estimateMethod: body.kind === 'model-route-plan' ? 'maximum-wire-variant-utf8-v1' : 'utf8-third-plus-framing-v1' };
}
export function assertPlannedRequest(body, plan) {
+4 -4
View File
@@ -5,10 +5,10 @@
用户授权下一阶段全量开发并及时回顾。基线299fb14/正式1.16.0,在feat/model-protocols-1.17按PLAN-1.17推进。正式PID36552/Provider和数据保持,开发使用自有loopback服务/独立数据/端口,不读取真实Key、不发真实模型请求;真实收益验收继续单列。不新建代理/goal/automation/chat。
1. [complete] A:协议/能力声明与修订绑定已实现,旧模型中心/导入/热升级及新增能力18/18专项通过,见QA-1.17;不将声明当实际协议执行。
2. [in_progress] B:Chat Completions与新增Responses的实际请求、消息/工具结果/原用量适配;请求封存绑定实际协议字节,保留旧证据。
3. [pending] C:两协议增量文本与工具片段、有界SSE、终结校验/停止/断开/迟到保护;完整参数与终结前不执行工具,不自动重放。
4. [pending] D:复用租约/轮询/粘连/冷却,按任务所需能力筛选及明确拒绝;401/429/超时/中断分类和有界切换。
5. [pending] E:协议/能力/检测范围、选池与当前流状态的桌面/390px/手机对应流程;保留品牌与真实任务布局,实际渲染与操作验收。
2. [complete] B:Responses实际Items/工具call_id/原用量和私有reasoning续接已实现,混合池按实际线路封存请求;专项与原连接兼容42项通过,旧证据不改。
3. [complete] C:Chat/Responses有界SSE、原工具参数/终结核对、断流不重发、停止原HTTP、片段与原响应工件已通过专项;新增字节/超时边界复核中。
4. [complete] D:租约前按能力排除,原Run账本保留拒绝;文本/工具/流式各自检测及修订失效已实现。补查原对象toggle别名导致检测失效遗漏,已修复并加入回归。
5. [in_progress] E:模型中心协议/能力/范围、请求账本与桌面/手机流状态已实现并构建通过;正在实际操作桌面/390px/手机页面,不将窄屏夹具当实机。
6. [pending] F:三工程/故障验证、完整历史回归、独立源码/便携/安装、保留状态部署、证据/文档封存及本地Git;逐切片回顾。
## 2026-10-31 已交付:1.16 手机伴随端
+108
View File
@@ -0,0 +1,108 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import fs from 'node:fs';
import path from 'node:path';
import { createHash } from 'node:crypto';
import { DatabaseSync } from 'node:sqlite';
import { fixture, compatibleFixture, completion, until, assertToolPairs } from './helpers.mjs';
import { Provider } from '../src/server/provider.mjs';
import { Runtime } from '../src/server/runtime.mjs';
import { verifyPlanEvidence } from '../src/core/context/request-plan.mjs';
import { completionBody, requestMetrics } from '../src/shared/contracts/model-request.mjs';
import { ConfigImporter } from '../src/core/models/config-import.mjs';
test('owned CC Switch SQLite imports the selected TOML Responses wire_api and leaves unknown clients untouched', t => {
const f = fixture(t), file = path.join(f.root, 'owned-switch.db'), db = new DatabaseSync(file);
db.exec('CREATE TABLE providers (id TEXT,app_type TEXT,name TEXT,settings_config TEXT)');
const insert = db.prepare('INSERT INTO providers VALUES(?,?,?,?)');
insert.run('responses', 'codex', 'Owned Responses', JSON.stringify({ config: 'model = "owned"\nmodel_provider = "own"\n[model_providers.own]\nbase_url = "http://127.0.0.1:12345/v1"\nwire_api = "responses"', auth: { OPENAI_API_KEY: 'owned-import-key' } }));
insert.run('unknown', 'workbuddy', 'Unknown format', '{}'); db.close();
const original = fs.readFileSync(file), hub = new Provider().modelHub(f.store), importer = new ConfigImporter(hub);
const preview = importer.preview('ccswitch', file);
assert.equal(preview.entries.length, 1); assert.equal(preview.entries[0].protocol, 'responses');
assert.equal(JSON.stringify(preview).includes('owned-import-key'), false); assert.equal(preview.notices.length, 1);
const result = importer.apply(preview.id); assert.equal(hub.connection(result.imported[0]).protocol, 'responses');
assert.deepEqual(fs.readFileSync(file), original);
});
const response = (output, extra = {}) => ({ body: { id: 'resp_owned', status: 'completed', output, usage: { input_tokens: 137, output_tokens: 29, total_tokens: 166 }, ...extra } });
const message = text => ({ type: 'message', id: 'msg_owned', status: 'completed', role: 'assistant', content: [{ type: 'output_text', text }] });
const fn = (id, name = 'list_files', args = '{"path":"","depth":1}') => ({ type: 'function_call', id: 'fc_' + id, call_id: id, status: 'completed', name, arguments: args });
const connect = (hub, server, protocol = 'responses', extra = {}) => hub.upsert({ name: 'Owned ' + protocol, model: 'owned-model', protocol,
baseUrl: server.baseUrl, requiresKey: false, ...extra });
const runUntilIdle = async (runtime, session) => { runtime.start(session.id, 'Inspect files and finish.'); await until(() => !runtime.isActive(session.id), 15000); };
test('Responses executes one original tool through Runtime, carries encrypted Items, reports raw usage and seals exact wire bytes', async t => {
const f = fixture(t), reasoning = { type: 'reasoning', id: 'rs_owned', summary: [], encrypted_content: 'owned-encrypted-reasoning' };
const server = await compatibleFixture(t, (_url, body) => body.input.some(item => item.type === 'function_call_output')
? response([message('工程读取完成')]) : response([reasoning, fn('owned_read')]));
const provider = new Provider(), hub = provider.modelHub(f.store), connection = connect(hub, server);
hub.select({ type: 'connection', id: connection.id }); const runtime = new Runtime(f.store, provider); t.after(() => runtime.stopAll());
await runUntilIdle(runtime, f.session);
assert.equal(f.session.status, 'completed', JSON.stringify(f.session.events.slice(-3))); assert.equal(server.requests.length, 2); assertToolPairs(f.session.providerMessages);
assert.equal(f.session.events.filter(item => item.tool === 'list_files' && item.phase === 'completed').length, 1);
assert.deepEqual(server.requests[1].input.input.find(item => item.type === 'reasoning'), reasoning);
assert.equal(server.requests[1].input.input.find(item => item.type === 'function_call_output').call_id, 'owned_read');
const usage = f.store.core.run.get(f.session.runId).usage;
assert.equal(usage.inputTokens, 274); assert.equal(usage.outputTokens, 58);
for (const attempt of hub.view().attempts) {
const sent = server.requests.find(item => createHash('sha256').update(JSON.stringify(item.input)).digest('hex') === attempt.requestSha256);
assert.ok(sent); assert.equal(sent.url, '/v1/responses'); assert.equal(sent.input.store, false);
assert.equal(sent.input.tools[0].type, 'function'); assert.equal(sent.input.tools[0].strict, false);
assert.equal(sent.input.tools[0].function, undefined); assert.equal(attempt.protocol, 'responses');
assert.equal(attempt.usage.raw.input_tokens, 137); assert.equal(attempt.runId, f.session.runId);
const artifact = f.store.owned('artifact', attempt.requestArtifactId, f.session.id);
assert.equal(artifact.sha256, attempt.requestSha256); assert.equal(attempt.requestBytes, Buffer.byteLength(JSON.stringify(sent.input)));
}
for (const plan of [...f.store.core.context_plan.values()]) assert.doesNotThrow(() => verifyPlanEvidence(f.store, f.session, plan));
});
test('mixed-protocol pool keeps both actual variants and changes route on 429 without re-executing completed tools', async t => {
const f = fixture(t), limited = await compatibleFixture(t, () => ({ status: 429, body: { error: { message: 'owned rate limit' } } }));
const good = await compatibleFixture(t, (_url, body) => body.input.some(item => item.type === 'function_call_output') ? response([message('Done')]) : response([fn('mixed_read')]));
const provider = new Provider(), hub = provider.modelHub(f.store), first = connect(hub, limited, 'chat-completions'), second = connect(hub, good);
const pool = hub.pool({ name: 'Mixed protocol pool', members: [{ connectionId: first.id }, { connectionId: second.id }] });
hub.select({ type: 'pool', id: pool.id }); const runtime = new Runtime(f.store, provider); t.after(() => runtime.stopAll());
await runUntilIdle(runtime, f.session);
assert.equal(f.session.status, 'completed', JSON.stringify(f.session.events.slice(-3))); assert.equal(limited.requests.length, 1); assert.equal(good.requests.length, 2);
assert.ok(limited.requests[0].input.messages); assert.ok(good.requests[0].input.input);
assert.equal(f.session.events.filter(item => item.tool === 'list_files' && item.phase === 'completed').length, 1);
const plan = [...f.store.core.context_plan.values()][0], body = JSON.parse(f.store.artifactStore.read(f.store.core.artifact.get(plan.requestArtifactId)));
assert.equal(body.kind, 'model-route-plan'); assert.equal(body.variants.length, 2);
assert.equal(plan.estimatedInputTokens, Math.max(...body.variants.map(item => requestMetrics(item.body).estimatedInputTokens)));
assert.doesNotThrow(() => verifyPlanEvidence(f.store, f.session, plan));
});
test('Responses incomplete, failed, missing call identity and unsupported hosted outputs never dispatch a tool', async t => {
for (const value of [response([fn('partial')], { status: 'incomplete' }), response([fn('failed')], { status: 'failed' }),
response([{ ...fn('bad'), call_id: '' }]), response([{ type: 'computer_call', id: 'not-supported' }])]) {
await t.test('Reject ' + JSON.stringify(value).slice(0, 100), async sub => {
const f = fixture(sub), server = await compatibleFixture(sub, () => value), provider = new Provider(), hub = provider.modelHub(f.store);
const connection = connect(hub, server); hub.select({ type: 'connection', id: connection.id });
const runtime = new Runtime(f.store, provider); sub.after(() => runtime.stopAll()); await runUntilIdle(runtime, f.session);
assert.notEqual(f.session.status, 'completed'); assert.equal(server.requests.length, 1);
assert.equal(f.session.events.filter(item => item.kind === 'tool').length, 0);
assert.equal(hub.view().attempts[0].status, 'failed');
});
}
});
test('wire adapter preserves tool IDs but never passes Responses-private Items to Chat or another endpoint', () => {
const messages = [{ role: 'user', content: 'Inspect' }, { role: 'assistant', content: '', tool_calls: [{ id: 'call_own', type: 'function', function: { name: 'read_file', arguments: '{"path":"README.md"}' } }],
providerState: { protocol: 'responses', endpoint: 'https://owned.invalid/v1', model: 'm', items: [{ type: 'reasoning', encrypted_content: 'owned-private' }] } },
{ role: 'tool', tool_call_id: 'call_own', content: '{"result":"owned"}' }];
const config = { model: 'm', baseUrl: 'https://other.invalid/v1', maxOutputTokens: 512 };
const chat = completionBody(config, messages, []); assert.equal(chat.messages[1].providerState, undefined);
const native = completionBody({ ...config, protocol: 'responses' }, messages, []);
assert.equal(JSON.stringify(native).includes('owned-private'), false);
assert.equal(native.input.find(item => item.type === 'function_call').call_id, 'call_own');
assert.equal(native.input.find(item => item.type === 'function_call_output').call_id, 'call_own');
});
test('connection JSON import accepts explicit Responses protocol and continues to reject unknown protocols without reading client sessions', t => {
const f = fixture(t), hub = new Provider().modelHub(f.store), importer = new ConfigImporter(hub), file = path.join(f.root, 'owned-connections.json');
fs.writeFileSync(file, JSON.stringify({ schema: 1, connections: [{ ...connectInput(), protocol: 'responses' }, { ...connectInput(), protocol: 'unknown' }] }));
const preview = importer.preview('workbench-json', file); assert.equal(preview.entries.length, 1); assert.equal(preview.entries[0].protocol, 'responses'); assert.equal(preview.notices.length, 1);
const result = importer.apply(preview.id); assert.equal(hub.connection(result.imported[0]).protocol, 'responses');
});
function connectInput() { return { name: 'Owned import', model: 'owned-model', baseUrl: 'http://127.0.0.1:12345/v1', requiresKey: false }; }
+57
View File
@@ -0,0 +1,57 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import { fixture, compatibleFixture, completion, call, until } from './helpers.mjs';
import { Provider } from '../src/server/provider.mjs';
import { Runtime } from '../src/server/runtime.mjs';
const add = (hub, server, name, declarations) => hub.upsert({ name, model: 'owned-model', baseUrl: server.baseUrl, requiresKey: false, capabilityDeclarations: declarations });
test('capability routing skips text-only members before acquiring a lease and records exact refusal in the original Run', async t => {
const f = fixture(t), text = await compatibleFixture(t, () => completion([], 'Text only')), tools = await compatibleFixture(t, () => completion([], 'Done'));
const provider = new Provider(), hub = provider.modelHub(f.store), a = add(hub, text, 'Text only', { tools: 'unsupported' }), b = add(hub, tools, 'Code tools', { tools: 'supported' });
const pool = hub.pool({ name: 'Ability route', members: [{ connectionId: a.id, weight: 10 }, { connectionId: b.id }] }); hub.select({ type: 'pool', id: pool.id });
const runtime = new Runtime(f.store, provider); t.after(() => runtime.stopAll()); runtime.start(f.session.id, 'Inspect'); await until(() => !runtime.isActive(f.session.id));
assert.equal(f.session.status, 'completed'); assert.equal(text.requests.length, 0); assert.equal(tools.requests.length, 1);
const attempt = hub.view().attempts[0]; assert.equal(attempt.runId, f.session.runId); assert.equal(attempt.rejections[0].connectionId, a.id); assert.equal(attempt.rejections[0].capability, 'tools');
});
test('all incompatible members reject with no network request, no lease, preserved Task and explicit non-dispatched ledger', async t => {
const f = fixture(t), server = await compatibleFixture(t, () => completion());
const provider = new Provider(), hub = provider.modelHub(f.store), connection = add(hub, server, 'Unknown tools', { tools: 'unknown' }); hub.select({ type: 'connection', id: connection.id });
const runtime = new Runtime(f.store, provider); t.after(() => runtime.stopAll()); runtime.start(f.session.id, 'Inspect'); await until(() => !runtime.isActive(f.session.id));
assert.equal(server.requests.length, 0); assert.equal(f.session.status, 'failed');
assert.equal(hub.view().attempts[0].status, 'rejected'); assert.equal(hub.view().attempts[0].usageStatus, 'not_dispatched');
assert.equal(hub.view().attempts[0].runId, f.session.runId); assert.equal([...hub.leases.values()].reduce((a, b) => a + b, 0), 0);
});
test('explicit synthetic tool probe verifies only actual matching call, never executes it and loses verification on revision changes', async t => {
const f = fixture(t), server = await compatibleFixture(t, (_url, body) => {
const nonce = body.messages.at(-1).content.match(/nonce ([a-f0-9-]+)/)?.[1];
return completion([call('probe_owned', 'workbench_capability_echo', { nonce })]);
});
const provider = new Provider(), hub = provider.modelHub(f.store), connection = add(hub, server, 'Probe owned', { tools: 'unknown' });
const result = await hub.probe(connection.id, 'tools'); assert.equal(result.ok, true); assert.equal(server.requests.length, 1);
assert.equal(hub.view().connections.find(value => value.id === connection.id).capabilities.tools, 'verified');
assert.equal(f.session.events.length, 0); assert.equal(f.store.core.tool.size, 0);
const current = hub.connection(connection.id); hub.upsert({ ...current, name: 'Changed owned' });
assert.equal(hub.view().connections.find(value => value.id === connection.id).capabilities.tools, 'unknown');
});
test('plain text probe cannot verify tools; wrong tool nonce and client supplied evidence cannot claim verified support', async t => {
const f = fixture(t), server = await compatibleFixture(t, () => completion([], 'OK'));
const provider = new Provider(), hub = provider.modelHub(f.store), connection = add(hub, server, 'Text probe', { tools: 'unknown' });
await hub.probe(connection.id); assert.equal(hub.view().connections.find(value => value.id === connection.id).capabilities.text, 'verified');
assert.equal(hub.view().connections.find(value => value.id === connection.id).capabilities.tools, 'unknown');
await assert.rejects(hub.probe(connection.id, 'tools'), /未标为已验证/);
assert.equal(hub.view().connections.find(value => value.id === connection.id).capabilities.tools, 'unknown');
});
test('a disabled connection invalidates an in-flight probe even when toggle mutates its original registry object', async t => {
const f = fixture(t); let release;
const server = await compatibleFixture(t, async () => { await new Promise(resolve => { release = resolve; }); return completion([], 'OK'); });
const provider = new Provider(), hub = provider.modelHub(f.store), connection = add(hub, server, 'Changing probe', {});
const pending = hub.probe(connection.id), rejected = assert.rejects(pending, /检测期间连接已变化/);
await until(() => !!release); hub.toggle(connection.id, false); release(); await rejected;
assert.equal(hub.view().connections.find(value => value.id === connection.id).capabilities.text, 'protocol_declared');
assert.equal(hub.state.capabilityResults?.[connection.id], undefined);
assert.equal(hub.view().attempts[0].status, 'completed');
});
+145
View File
@@ -0,0 +1,145 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import http from 'node:http';
import { createHash } from 'node:crypto';
import { setTimeout as delay } from 'node:timers/promises';
import { fixture, until, assertToolPairs } from './helpers.mjs';
import { Provider } from '../src/server/provider.mjs';
import { Runtime } from '../src/server/runtime.mjs';
import { readModelStream } from '../src/core/models/model-stream.mjs';
const sse = event => 'data: ' + (typeof event === 'string' ? event : JSON.stringify(event)) + '\r\n\r\n';
const chat = (delta, finish_reason = null) => sse({ choices: [{ index: 0, delta, finish_reason }] });
const chatEnd = (reason = 'stop') => chat({}, reason) + sse({ choices: [], usage: { prompt_tokens: 20, completion_tokens: 10 } }) + sse('[DONE]');
const native = (type, value, sequence_number) => sse({ type, sequence_number, ...value });
async function streamingServer(t, handler) {
const requests = [], sockets = new Set();
const server = http.createServer(async (request, response) => {
const parts = []; for await (const part of request) parts.push(part);
const body = JSON.parse(Buffer.concat(parts)); requests.push({ body, url: request.url });
response.writeHead(200, { 'Content-Type': 'text/event-stream' });
try { await handler(response, body, requests); } catch { response.destroy(); }
});
server.on('connection', socket => { sockets.add(socket); socket.on('close', () => sockets.delete(socket)); });
await new Promise(resolve => server.listen(0, '127.0.0.1', resolve));
t.after(() => new Promise(resolve => { for (const socket of sockets) socket.destroy(); server.close(resolve); }));
return { requests, baseUrl: 'http://127.0.0.1:' + server.address().port + '/v1' };
}
function configure(f, server, protocol = 'chat-completions') {
const provider = new Provider(), hub = provider.modelHub(f.store);
const connection = hub.upsert({ name: 'Owned stream', baseUrl: server.baseUrl, model: 'owned-stream', protocol, requiresKey: false,
streaming: true, capabilityDeclarations: { streaming: 'supported' } });
hub.select({ type: 'connection', id: connection.id });
return { provider, hub, connection };
}
test('real Chat SSE carries UTF8 and split tool JSON; Runtime executes only after DONE, stores partial progress and original usage', async t => {
const f = fixture(t); let runtime, observedBeforeEnd = false;
const server = await streamingServer(t, async (res, body) => {
if (body.messages.some(item => item.role === 'tool')) { res.end(chat({ content: '读取完成。' }) + chatEnd()); return; }
const bytes = Buffer.from(chat({ content: '先读取项目。' }));
for (let at = 0; at < bytes.length; at += 7) res.write(bytes.subarray(at, at + 7));
res.write(chat({ tool_calls: [{ index: 0, id: 'stream_read', type: 'function', function: { name: 'list_files', arguments: '{"path":' } }] }));
await delay(150); observedBeforeEnd = f.session.events.some(item => item.stream?.textPreview === '先读取项目。') && !f.session.events.some(item => item.kind === 'tool');
res.end(chat({ tool_calls: [{ index: 0, function: { arguments: '"","depth":1}' } }] }) + chatEnd('tool_calls'));
});
const { provider, hub } = configure(f, server); runtime = new Runtime(f.store, provider); t.after(() => runtime.stopAll());
runtime.start(f.session.id, 'Inspect original project'); await until(() => !runtime.isActive(f.session.id), 15000);
assert.equal(f.session.status, 'completed', JSON.stringify(f.session.events.slice(-2))); assert.equal(observedBeforeEnd, true);
assert.equal(server.requests.length, 2); assert.equal(f.session.events.filter(item => item.tool === 'list_files' && item.phase === 'completed').length, 1);
assertToolPairs(f.session.providerMessages); assert.equal(f.store.core.run.get(f.session.runId).usage.inputTokens, 40);
assert.ok(hub.view().attempts.every(item => item.stream.terminal && item.stream.sha256.length === 64));
for (const attempt of hub.view().attempts) {
const artifact = f.store.owned('artifact', attempt.responseArtifactId, f.session.id);
assert.equal(artifact.sha256, attempt.stream.sha256); assert.equal(artifact.bytes, attempt.stream.bytes);
assert.equal(artifact.kind, 'model-stream-response'); assert.equal(attempt.responseRedacted, false);
}
});
test('Responses typed SSE validates original item identity, terminal data, raw usage and tool result pair', async t => {
const f = fixture(t), call = { type: 'function_call', id: 'fc_stream', call_id: 'call_stream', name: 'list_files', arguments: '', status: 'in_progress' };
const server = await streamingServer(t, async (res, body) => {
const done = body.input.some(item => item.type === 'function_call_output');
let seq = 0; res.write(native('response.created', { response: { id: 'resp_stream' } }, seq++));
let output;
if (done) {
res.write(native('response.output_text.delta', { delta: '完成' }, seq++));
output = [{ type: 'message', id: 'msg_stream', status: 'completed', role: 'assistant', content: [{ type: 'output_text', text: '完成' }] }];
} else {
res.write(native('response.output_item.added', { output_index: 0, item: call }, seq++));
res.write(native('response.function_call_arguments.delta', { item_id: call.id, delta: '{"path":"",' }, seq++));
res.write(native('response.function_call_arguments.delta', { item_id: call.id, delta: '"depth":1}' }, seq++));
output = [{ ...call, status: 'completed', arguments: '{"path":"","depth":1}' }];
res.write(native('response.function_call_arguments.done', { item_id: call.id, arguments: output[0].arguments }, seq++));
}
res.end(native('response.completed', { response: { id: 'resp_stream', status: 'completed', output, usage: { input_tokens: 33, output_tokens: 9, total_tokens: 42 } } }, seq++));
});
const { provider, hub } = configure(f, server, 'responses'), runtime = new Runtime(f.store, provider); t.after(() => runtime.stopAll());
runtime.start(f.session.id, 'Inspect'); await until(() => !runtime.isActive(f.session.id), 15000);
assert.equal(f.session.status, 'completed', JSON.stringify(f.session.events.slice(-2))); assertToolPairs(f.session.providerMessages);
assert.equal(server.requests.length, 2); assert.equal(server.requests[1].body.input.find(item => item.type === 'function_call_output').call_id, 'call_stream');
assert.equal(hub.view().attempts[0].usage.raw.total_tokens, 42);
});
test('incomplete network stream retains unknown attempt and never automatically switches pool or dispatches fragment tools', async t => {
const f = fixture(t), server = await streamingServer(t, async res => res.end(chat({ content: 'partial' }) + chat({ tool_calls: [{ index: 0, id: 'partial_tool', function: { name: 'list_files', arguments: '{"path":' } }] })));
const { provider, hub, connection } = configure(f, server), other = hub.upsert({ ...connection, id: undefined, name: 'Other', revision: undefined });
const pool = hub.pool({ name: 'No replay', members: [{ connectionId: connection.id }, { connectionId: other.id }] }); hub.select({ type: 'pool', id: pool.id });
const runtime = new Runtime(f.store, provider); t.after(() => runtime.stopAll()); runtime.start(f.session.id, 'Inspect'); await until(() => !runtime.isActive(f.session.id));
assert.equal(server.requests.length, 1); assert.equal(f.session.events.filter(item => item.kind === 'tool').length, 0);
const attempt = hub.view().attempts[0]; assert.equal(attempt.status, 'unknown'); assert.equal(attempt.partial.textPreview, 'partial'); assert.equal(attempt.usageStatus, 'unknown');
const artifact = f.store.owned('artifact', attempt.responseArtifactId, f.session.id);
assert.equal(artifact.sha256, attempt.stream.sha256); assert.equal(artifact.bytes, attempt.stream.capturedBytes); assert.equal(attempt.stream.terminal, false);
assert.equal(f.session.events.find(item => item.stream)?.stream.status, 'incomplete');
});
test('stopping an active HTTP stream closes only original request and keeps partial text without executing tools', async t => {
const f = fixture(t); let closed = false;
const server = await streamingServer(t, async res => { res.on('close', () => { closed = true; }); res.write(chat({ content: '尚未完成' })); });
const { provider, hub } = configure(f, server), runtime = new Runtime(f.store, provider); t.after(() => runtime.stopAll());
runtime.start(f.session.id, 'Inspect'); await until(() => f.session.events.some(item => item.stream?.textCharacters > 0));
runtime.stop(f.session.id); await until(() => !runtime.isActive(f.session.id) && closed);
assert.equal(server.requests.length, 1); assert.equal(f.session.events.filter(item => item.kind === 'tool').length, 0);
assert.equal(hub.view().attempts[0].status, 'interrupted'); assert.equal(f.session.events.find(item => item.stream)?.stream.status, 'interrupted');
const attempt = hub.view().attempts[0]; assert.equal(f.store.owned('artifact', attempt.responseArtifactId, f.session.id).sha256, attempt.stream.sha256);
assert.equal([...hub.leases.values()].reduce((a, b) => a + b, 0), 0);
});
test('explicit streaming probe uses actual SSE despite unknown declaration and binds its own original attempt', async t => {
const f = fixture(t), server = await streamingServer(t, async res => res.end(chat({ content: 'OK' }) + chatEnd()));
const provider = new Provider(), hub = provider.modelHub(f.store), connection = hub.upsert({ name: 'Owned probe', model: 'm', baseUrl: server.baseUrl, requiresKey: false });
await hub.probe(connection.id, 'streaming');
const view = hub.view().connections.find(item => item.id === connection.id);
assert.equal(view.capabilities.streaming, 'verified'); assert.equal(view.streaming, false); assert.equal(server.requests[0].body.stream, true);
assert.equal(view.capabilityEvidence.attemptId, hub.view().attempts[0].id); assert.equal(f.session.events.length, 0);
});
test('stream timeout closes the original HTTP response, preserves redacted capture and never retries', async t => {
const f = fixture(t); let closed = false;
const server = await streamingServer(t, async res => { res.on('close', () => { closed = true; }); res.write(chat({ content: 'owned-secret partial' })); });
const { provider, hub } = configure(f, server); provider.setKey('owned-secret');
await assert.rejects(provider.complete(provider.prepareConfig(f.store.config, f.store), [{ role: 'user', content: 'Inspect' }], f.project,
undefined, undefined, null, [], { timeoutMs: 100, totalTimeoutMs: 300, attempts: 3 }), error => error.diagnostic.kind === 'timeout' && !error.diagnostic.retryable);
await until(() => closed); assert.equal(server.requests.length, 1); assert.equal(hub.view().attempts[0].status, 'unknown');
assert.equal(hub.view().attempts[0].partial.textPreview.includes('owned-secret'), false);
});
test('SSE accepts CR-only final event boundaries and retains exact invalid UTF8 bytes without dispatch', async () => {
const valid = (chat({ content: 'OK' }) + chatEnd()).replaceAll('\r\n', '\r');
const result = await readModelStream(new Response(valid, { headers: { 'Content-Type': 'text/event-stream' } }), {}, undefined);
assert.equal(result.value.choices[0].message.content, 'OK');
const invalid = Buffer.from([100, 97, 116, 97, 58, 32, 0xff, 10, 10]); let capture;
await assert.rejects(readModelStream(new Response(invalid, { headers: { 'Content-Type': 'text/event-stream' } }), {}, undefined, event => { if (event.streamCapture) capture = event; }));
assert.deepEqual(capture.streamCapture, invalid); assert.equal(capture.stream.sha256, createHash('sha256').update(invalid).digest('hex')); assert.equal(capture.stream.terminal, false);
});
test('stream parser rejects truncation, finish length, malformed JSON, duplicate sequence, changed final item and oversized data', async t => {
const cases = [
['chat-completions', chat({ content: 'partial' })], ['chat-completions', chat({ content: 'partial' }) + chatEnd('length')],
['chat-completions', 'data: {broken}\n\n'], ['chat-completions', 'data: ' + 'x'.repeat(4 * 1024 * 1024) + '\n\n'],
['responses', native('response.created', { response: { id: 'r' } }, 0) + native('response.output_text.delta', { delta: 'x' }, 0)],
['responses', native('response.output_text.delta', { delta: 'x' }, 0) + native('response.completed', { response: { status: 'completed', output: [{ type: 'message', role: 'assistant', content: [{ type: 'output_text', text: 'changed' }] }] } }, 1)],
];
for (const [index, [protocol, body]] of cases.entries()) await t.test('reject ' + index, async () => {
await assert.rejects(readModelStream(new Response(body, { headers: { 'content-type': 'text/event-stream' } }), { protocol }, undefined));
});
});