fix: preserve upstream model errors and native request parameters
This commit is contained in:
@@ -1,5 +1,14 @@
|
||||
# Agent Workbench 更新记录
|
||||
|
||||
## 1.20.3 · 模型原生参数与错误保留
|
||||
|
||||
- 错误详情与模型请求账本保留上游原文、code/type、请求ID和原Retry-After,并标明本地超时/协议核验与上游失败的来源。
|
||||
- HTTP错误正文有界保存、凭据脱敏、截断范围与工件校验可查;SSE失败保留原事件、已报告用量和原流,不重放未完整工具。
|
||||
- 额度不足不重复请求同线路,模型池可按规则选其他可用成员;长Retry-After不截成90秒,不足等待预算时结束原请求。
|
||||
- 每个连接可配置现有Chat/Responses已接入的推理、采样、工具并行和文本详略参数,以及Chat输出上限字段;实际请求可下载,未知字段明确拒绝。
|
||||
- 1.22内置浏览器的阶段、前置依赖和实际验收已明确;当前1.10静态工程页面验收不冒充通用浏览器。
|
||||
|
||||
|
||||
## 1.20.2 · 模型响应等待修复
|
||||
|
||||
- 修复模型生成被固定60秒截止的问题:响应总等待默认5分钟、流式无数据默认2分钟,模型中心可调整。
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
# 本地 Agent 工作台
|
||||
|
||||
1.20.2修复固定60秒生成截止,新增模型中心「等待时限」;当前正式版本已升级,原连接和历史保持。见[工作台](http://127.0.0.1:4318/?release=1.20.2)、[验收](docs/QA-1.20.2.md)、[回顾](docs/RETROSPECTIVE-1.20.2.md)和[发行身份](docs/model-wait-release-identity.json)。历史失败仍按原记录显示,手动续接的新请求使用新时限,任务总预算仍优先。
|
||||
1.20.3新增上游错误详情/原文下载、原生参数配置和按原因处理429,见[验收记录](docs/QA-1.20.3.md)。正式部署以最终发行身份为准。内置浏览器已明确为[1.22阶段](docs/PLAN-1.22.md),当前已有静态工程页面验收。
|
||||
|
||||
上一维护版1.20.2修复固定60秒生成截止,新增模型中心「等待时限」;当前正式版本已升级,原连接和历史保持。见[工作台](http://127.0.0.1:4318/?release=1.20.2)、[验收](docs/QA-1.20.2.md)、[回顾](docs/RETROSPECTIVE-1.20.2.md)和[发行身份](docs/model-wait-release-identity.json)。历史失败仍按原记录显示,手动续接的新请求使用新时限,任务总预算仍优先。
|
||||
|
||||
1.20.1已修复任务目标丢失、审查后附加测试、完整无参工具报错、失败消费漏记和无进展循环,并修复暂停收尾及Windows退出竞态。独立626项、实际桌面/390px、安装/升级通过,原连接与全部历史保持。见[工作台](http://127.0.0.1:4318/?release=1.20.1)、[验收](docs/QA-1.20.1.md)、[回顾](docs/RETROSPECTIVE-1.20.1.md)和[发行身份](docs/task-control-release-identity.json)。1.21计划保留,真实模型自主工程复验单列。
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
# 模型能力与错误信息保留方案
|
||||
|
||||
用户提出工作台应保留大模型能力,错误也应传到用户面前,并询问Codex是否为原样透传。本记录固定接入原则与已确认缺口;属于后续维护方案,尚未实现,不改变正式1.20.2服务或原会话。
|
||||
用户提出工作台应保留大模型能力,错误也应传到用户面前,并询问Codex是否为原样透传。本记录固定接入原则与已确认缺口。用户随后授权实现上线,1.20.3候选已完成HTTP/SSE错误、重试语义、错误界面与现有Chat/Responses原生参数接入;最终验证/部署见[QA-1.20.3](QA-1.20.3.md),不将候选阶段写为正式上线。更多原生参数与新模态继续逐项开发,原会话不回写。
|
||||
|
||||
## 接入原则
|
||||
|
||||
|
||||
+3
-1
@@ -1,6 +1,8 @@
|
||||
# 后续执行计划
|
||||
|
||||
新增接入要求:按[模型能力与错误信息保留方案](MODEL-FIDELITY-PLAN.md)先补有界HTTP/SSE原错误、上游request ID、原code/type与Retry-After,再逐项验证供应商原生能力和参数。1.20.2目前不是全能力/全错误字段透传;本次只完成源码审查与方案登记,正式服务未改动。原失败不回写,本机ID与上游ID分开,不把中文解释当原文。
|
||||
最新执行顺序:1.20.3实现并本地上线模型错误保留与现有协议原生参数 → [1.21工程依赖与可复现环境](PLAN-1.21.md) → [1.22工作台内置浏览器](PLAN-1.22.md)。1.10已有静态构建的真实Edge页面验收,1.22才是项目开发预览、桌面内嵌标签页与可观察/停止的通用网站操作;Codex右侧浏览器不算本产品内置能力。
|
||||
|
||||
新增接入要求按[模型能力与错误信息保留方案](MODEL-FIDELITY-PLAN.md)执行:1.20.3候选已实现有界HTTP/SSE原错误、上游request ID、原code/type与Retry-After及现有Chat/Responses原生参数;实际验证/部署以[QA-1.20.3](QA-1.20.3.md)为准。更多供应商参数与模态仍逐项验证,不能称为全能力透传。原失败不回写,本机ID与上游ID分开,不把中文解释当原文。
|
||||
|
||||
本轮正式暂存及独立核对实际耗时860秒,最终安装指针激活及原状态核对647秒,发行19560文件。原暂存后因新增模型尝试被拒绝的结果保持失败,耗时不能当成已通过的性能指标。后续维护优先研究运行载荷与历史验收材料分离、同一发行的必要核对复用,以及当前校验阶段/耗时/失败收据的可见反馈;保留路径、字节变化和回滚门槛,另行实现验收。
|
||||
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
# 1.22 后续方案:工作台内置浏览器
|
||||
|
||||
当前是计划,尚未实现。1.20.3先完成模型错误与原生参数维护;1.21完成工程依赖与可复现环境,随后1.22交付内置浏览器。具体版本取决于前置验收,阶段边界以本文件为准。
|
||||
|
||||
## 现有基础与目标
|
||||
|
||||
1.10以来已有封存静态工程构建、独立Edge上下文、填写/选择/点击/断言、DOM/控制台/请求/PNG与Task归属;现有桌面入口也可打开所属成果。它们用于工程页面验收,不等于工作台内已有通用浏览器。Codex右侧浏览器是Codex提供的外部功能,不计入本产品能力。
|
||||
|
||||
让用户在工作台查看和操作所属项目预览,也能给代理一个明确的网站目标,观察页面、连续交互、核对结果;用户与代理共享可核对的标签页状态。桌面内嵌视图与受控浏览服务分层,手机伴随端先观察原结果及审批,不直接取得Windows浏览器进程控制权。
|
||||
|
||||
## 执行切片
|
||||
|
||||
| 切片 | 实际交付 | 验收 |
|
||||
| --- | --- | --- |
|
||||
| A 项目开发预览 | 复用1.21固定环境,所属开发服务的启动/就绪URL/日志/停止;静态和开发服务均可打开 | React实际开发服务、改动后页面更新、服务退出/端口冲突/停止;不自动复用未知进程 |
|
||||
| B 桌面浏览区 | Electron独立浏览视图、标签页、地址/来源、前进后退/刷新/关闭;任务与页面归属可见 | 真正可交互页面、外站无法取得工作台宿主API或Node权限、关闭视图与停止任务行为分别验证 |
|
||||
| C 代理浏览工具 | 打开明确目标、读取可见页面/DOM/截图、点击/输入/滚动/等待;保存原定位、输入、输出和截图工件 | 多步真实网站任务、失败定位、页面变更、弹窗和停止;模型建议只通过所属执行工具生效 |
|
||||
| D 会话与权限 | 独立浏览配置、可选持久登录资料、域名/操作范围、可撤销授权;下载与敏感提交有可核对审批 | 登录态隔离/清除/关闭;下载绑定目录/文件/来源;站点内容不能自行授予新权限或跨任务访问 |
|
||||
| E 验收与交付 | 本地测试站和真实允许目标,桌面/390px观察与手机审批;原任务/浏览进程/证据保留和本地发行 | 页面身份、可恢复状态、无自动重放敏感操作、实际任务结果、更新回退;每切片本地Git与回顾 |
|
||||
|
||||
支付、发消息、上传私有资料等操作按实际用户授权处理;通用操作电脑、任意浏览器登录导入、语音与视频另列后续,不从能打开网页推断全部实现。浏览器里的文字和下载文件属于任务数据。
|
||||
|
||||
先完成项目预览与浏览区,再接网站连续操作;开发和验收均复用现有Task/Run、审批、预算、事件与工件,避免再建一套不受任务内核管理的浏览器。
|
||||
@@ -0,0 +1,32 @@
|
||||
# 1.20.3 模型原生参数与错误保留验收
|
||||
|
||||
本文件随实施更新;正式部署状态以最终发行身份及下方部署记录为准,不根据浏览器release参数判断运行版本。用户授权实现MODEL-FIDELITY-PLAN并本地上线,基线02cefd0,分支fix/model-fidelity-1.20.3。
|
||||
|
||||
## 实现范围
|
||||
|
||||
- Task所属HTTP错误保存前64KiB,界面预览前8000字符;保留code/type/message/param、允许诊断响应头、上游请求/响应ID、原Retry-After与实际模型/接口/物理尝试ID。原文脱敏,截断/中断范围、保存工件及SHA可查,不把中文说明称为原文。
|
||||
- SSE错误保留原失败事件及原流,区分上游失败、本地等待截止和协议拒绝;HTTP 200是事件流的传输状态,不代表响应任务成功。保留已报告用量,未完整工具不执行,失败片段不自动重发或切池。
|
||||
- 已知额度/消费限制不在同线路重复请求,模型池可按现有规则选其它独立可用成员。429未给明确code时原因保持未确定;Retry-After不截成90秒,预算不足或无效等待头不提前重试。
|
||||
- 每个连接(含原连接)有原生参数入口,保存保留当前路由和内存凭据,新Run/实际请求封存参数;旧Run和原失败不回写。Chat输出字段可选max_tokens/max_completion_tokens,Responses沿用max_output_tokens。预算仍由宿主控制。
|
||||
- 模型中心的显式检测没有工程Task/Run时仅保留有界诊断预览,并显示原错误;不把检测记录冒充工程工件。真实上游和任意品牌全能力未在本轮验证。
|
||||
|
||||
| 协议 | 此版实际发送的可选原生字段 | 未接入范围 |
|
||||
| --- | --- | --- |
|
||||
| Chat Completions | temperature、top_p、seed、parallel_tool_calls、reasoning_effort、verbosity;可选择输出上限字段 | 任意额外参数、音视频输出、服务端内置工具 |
|
||||
| Responses | temperature、top_p、parallel_tool_calls、reasoning.effort、text.verbosity;原reasoning.encrypted_content继续项保持 | 任意额外参数、音视频输出、服务端内置工具和额外缓存选项 |
|
||||
|
||||
字段接入代表已核对实际线上的请求构造,不代表所有同协议模型都接受该参数。具体拒绝信息按原上游错误展示,未接入字段在保存/发送前明确拒绝。
|
||||
|
||||
## 已完成的阶段验证
|
||||
|
||||
实施中既有诊断/模型池/SSE相关45项通过,错误/原生参数/预算/协议50项通过。首次新增15项14通过、1失败来自测试夹具HTTP status与Responses status混用,修正后的整组已包含在50项中;原失败保留。
|
||||
|
||||
TypeScript、Vite、手机静态壳及服务端构建通过。Edge两视口六组流程/十截图通过:原生参数保存与刷新、未知字段拒绝、实际HTTP错误及原文SHA下载、请求账本、Responses失败事件/用量/零工具执行,以及显式检测诊断预览。全部请求仅发送到自有本机HTTP/SSE,不使用真实模型或用户工程。
|
||||
|
||||
截图实际回看后修正SSE错误的上游/本地措辞及实际模型/接口合并;新增API错误详情、转义凭据脱敏及原连接检测参数映射。最终冻结源码验证、随包、旧版到新版升级与正式部署待下方补充,不能以中间结果代替。
|
||||
|
||||
补强后相关八文件103项通过;HTTP错误读取分支另外补齐已报告用量,随后最终17项新增专项整轮通过。原生参数、上游/本地来源、失败HTTP/SSE用量、JSON转义凭据、旧数据重开和原连接检测均已核对。更广的源码轮仍执行中;本次103项开始于最后用量补强前,最终17项覆盖该补强,不合并冒充同一冻结源码整轮。
|
||||
|
||||
## 正式部署
|
||||
|
||||
待完成候选与保留状态升级,当前此处不登记上线成功。
|
||||
@@ -1,5 +1,8 @@
|
||||
# 汇总发现
|
||||
|
||||
- 1.20.3已进入实现:HTTP错误原文上限64KiB、界面预览8000字符;保留上游请求ID、code/type/param、允许的诊断头和原Retry-After,额度类错误不重复同线路请求,池内可切换其它可用成员;SSE原事件及原流分开标明,不自动重放片段。原生参数按现有Chat/Responses协议接入,逐连接封存,禁止覆盖消息/工具/预算。实现和部署仍须后续验证,暂不标完成。
|
||||
- 浏览器现状核对:1.10已实现静态工程构建的真实Edge交互/DOM/请求/PNG验收,通用内置浏览器此前只有跨阶段能力项,未排具体版本;本次明确为1.22,位于1.21工程依赖与可复现环境之后。
|
||||
|
||||
- 用户提出能力/错误透传。源码确认HTTP错误仅提取message截600字符,缺原code/type和上游request ID;429只按状态判可重试,Retry-After截90秒;SSE错误原片段有工件但诊断通用化。Chat/Responses已有适配不等于全部供应商原生能力。细节与后续门槛见docs/MODEL-FIDELITY-PLAN.md,正式1.20.2未改变。
|
||||
|
||||
- 最新结论:1.20.2已完成模型响应等待修复与保留状态部署:单次响应默认300秒、流式无数据120秒,模型中心可调整。111项专项/关联回归、真实本机JSON/SSE各65秒、五组等待UI/五组协议UI及五组随包等待UI通过;实际旧版到新版升级及正式Edge/Electron两视口回看通过。正式PID36552/Provider、17项目/31全部会话/3207记录/6138事件和1761个工件文件原字节保持,仅公开模型中心增加计算出的等待默认值;磁盘配置不回写。本轮验收发起真实模型请求0。 发行源码3b650275ffe6984a1147a39ba1c22dd3a6859a61,原始用户导出/SSE和SQLite备份仅保留本机忽略目录,不进Git。用户确认另一次429来自当前Codex对话,和工作台超时分属不同请求链。
|
||||
|
||||
Generated
+2
-2
@@ -1,12 +1,12 @@
|
||||
{
|
||||
"name": "local-agent-workbench",
|
||||
"version": "1.20.2",
|
||||
"version": "1.20.3",
|
||||
"lockfileVersion": 3,
|
||||
"requires": true,
|
||||
"packages": {
|
||||
"": {
|
||||
"name": "local-agent-workbench",
|
||||
"version": "1.20.2",
|
||||
"version": "1.20.3",
|
||||
"dependencies": {
|
||||
"@babel/parser": "7.29.11",
|
||||
"@peculiar/x509": "3.1.0",
|
||||
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "local-agent-workbench",
|
||||
"version": "1.20.2",
|
||||
"version": "1.20.3",
|
||||
"private": true,
|
||||
"type": "module",
|
||||
"description": "A local coding workbench with explicit tool execution, reviewable changes and recoverable sessions.",
|
||||
|
||||
+12
@@ -1,5 +1,17 @@
|
||||
# 统一执行进度
|
||||
|
||||
## 1.20.3 模型错误和原生参数上线(进行中)
|
||||
|
||||
最终新增17项通过;相关八文件103项通过(该轮开始于最后HTTP错误用量补强前,最后17项单列覆盖补强,不冒充冻结同轮)。HTTP/SSE错误已报告用量、转义凭据、原生参数实际发送和原连接检测均核对。真实Edge六组/十截图通过,已回看宽屏原HTTP正文与390px原SSE事件;当前准备冻结源码、本地提交和不可变候选。更广源码轮仍运行,尚不登记完整通过。
|
||||
|
||||
真实Edge五组流程/七截图通过并实际回看。截图审查发现SSE错误说明把上游事件混称为本地响应核验,且undefined诊断字段覆盖了实际模型/地址;已修正来源措辞和上下文合并并补断言。探测界面API已返回diagnostic但前端丢弃,也已补到模型中心提示中;没有Task/Run的检测只保存预览,明确标注不造工程工件。首轮完整源码回归开始于此补强前,不作为最终冻结源码验收;后续须按最终来源验证。
|
||||
|
||||
首轮既有相关回归45/45通过。初次构建因新MJS共享契约缺声明失败,已补native-parameters.d.mts,TypeScript/Vite/手机及服务端构建随后通过。新增15项首轮14通过、1失败:Responses测试夹具把原响应status="completed"误当HTTP状态,修正为显式body后再运行;原失败日志保留。未部署失败候选。
|
||||
|
||||
界面连接脚本在DiagnosticsPanel的宽泛import锚点处发现多处匹配并停止;此前ModelCenter五处修改已保存,后续读取实际import后用精确补丁继续,未重新执行前面修改。
|
||||
|
||||
用户授权实现并本地上线;已切fix/model-fidelity-1.20.3,QA隔离数据和本机HTTP/SSE。已接HTTP/SSE错误捕获/诊断、原Retry-After、额度分类和原生参数协议构造,正在补界面和实际验证。正式服务尚未切换。首次检索误用了不存在的FailureNotice.tsx、config.mjs和PowerShell路径通配符;均为只读失败,随后根据实际components.tsx、legacy-import.mjs和目录过滤定位,没有重复原失败。早期批量输出过长截断,后续改为限定行段读取。
|
||||
|
||||
## 模型能力与错误信息保留讨论
|
||||
|
||||
只读核对官方Harness/错误文档与当前源码,明确模型推理、协议能力和产品执行环境的关系;确认HTTP/SSE错误字段及原生参数覆盖缺口。已写MODEL-FIDELITY-PLAN并更新后续计划,尚未实现;正式服务、原用户任务/配置均未操作,没有模型调用。首轮批量文档补丁因findings标题误猜整体拒绝,无文件写入;读取实际标题后修正。一次rg使用PowerShell不展开的路径通配符导致局部查询报错,已改为已存在目录加-g过滤;其余结果实际读取。
|
||||
|
||||
@@ -0,0 +1,109 @@
|
||||
import fs from 'node:fs';
|
||||
import path from 'node:path';
|
||||
import http from 'node:http';
|
||||
import assert from 'node:assert/strict';
|
||||
import { createHash, randomUUID } from 'node:crypto';
|
||||
import { chromium, expect } from 'playwright/test';
|
||||
import { fixture, launchWorkbench, until } from '../tests/helpers.mjs';
|
||||
import { Provider } from '../src/server/provider.mjs';
|
||||
|
||||
const args = process.argv.slice(2);
|
||||
assert.ok(args.length === 0 || args.length === 2 && args[0] === '--app-root' && path.isAbsolute(args[1]));
|
||||
const appRoot = args[1] ?? process.cwd(), directory = path.resolve('.qa', 'model-fidelity-ui-' + randomUUID());
|
||||
fs.mkdirSync(directory, { recursive: true });
|
||||
const disposers = [], t = { after: action => disposers.push(action) }, f = fixture(t), requests = [], screenshots = [], checks = [], errors = [];
|
||||
let mode = 'quota', browser, page;
|
||||
const model = http.createServer(async (request, response) => {
|
||||
const parts = []; for await (const part of request) parts.push(part); requests.push({ route: request.url, body: JSON.parse(Buffer.concat(parts)) });
|
||||
if (mode === 'quota') {
|
||||
response.writeHead(429, { 'Content-Type': 'application/json', 'X-Request-ID': 'owned-upstream-quota-ui', 'Retry-After': '180' });
|
||||
response.end(JSON.stringify({ error: { code: 'insufficient_quota', type: 'billing_error', param: 'account', message: 'Owned quota error; owned-ui-private-key' } }));
|
||||
} else {
|
||||
response.writeHead(200, { 'Content-Type': 'text/event-stream', 'X-Request-ID': 'owned-upstream-stream-ui' });
|
||||
const event = value => 'data: ' + JSON.stringify(value) + '\n\n';
|
||||
response.end(event({ type: 'response.created', sequence_number: 1, response: { id: 'resp-owned-ui' } }) +
|
||||
event({ type: 'response.output_item.added', sequence_number: 2, item: { id: 'fc-owned-ui', call_id: 'owned-ui-tool', type: 'function_call', name: 'propose_file', arguments: '' } }) +
|
||||
event({ type: 'response.function_call_arguments.delta', sequence_number: 3, item_id: 'fc-owned-ui', delta: '{"path":' }) +
|
||||
event({ type: 'response.failed', sequence_number: 4, response: { id: 'resp-owned-ui', status: 'failed', error: { code: 'server_error', type: 'api_error', message: 'Owned stream failed' }, usage: { input_tokens: 20, output_tokens: 4 } } }));
|
||||
}
|
||||
});
|
||||
await new Promise(resolve => model.listen(0, '127.0.0.1', resolve));
|
||||
const provider = new Provider(), hub = provider.modelHub(f.store);
|
||||
let member = hub.upsert({ name: 'Owned UI 模型', model: 'owned-ui-model', baseUrl: 'http://127.0.0.1:' + model.address().port + '/v1', apiKey: 'owned-ui-private-key', requiresKey: true });
|
||||
hub.select({ type: 'connection', id: member.id }); f.store.close();
|
||||
const host = await launchWorkbench(t, f.data, {}, { appRoot, nodePath: fs.existsSync(path.join(appRoot, 'runtime/node.exe')) ? path.join(appRoot, 'runtime/node.exe') : process.execPath });
|
||||
// The test credential is intentionally volatile; set it in this new test host.
|
||||
member = await host.api('/model-center/connections', { ...member, apiKey: 'owned-ui-private-key' });
|
||||
const mark = text => { checks.push(text); console.log('PASS ' + text); };
|
||||
async function capture(name) { const file = path.join(directory, name + '.png'); await page.screenshot({ path: file }); screenshots.push({ path: file, sha256: createHash('sha256').update(fs.readFileSync(file)).digest('hex') }); }
|
||||
async function openCenter() { await page.getByRole('button', { name: '打开模型中心', exact: true }).click(); return page.getByRole('dialog', { name: '模型中心', exact: true }); }
|
||||
async function run(prompt) {
|
||||
await page.getByLabel('任务内容').fill(prompt); await page.getByRole('button', { name: '发送任务', exact: true }).click();
|
||||
let id; await until(async () => { id = (await host.api('/sessions')).find(value => value.title === prompt)?.id; return id && (await host.api('/sessions/' + id)).status === 'failed'; }, 15000);
|
||||
return host.api('/sessions/' + id);
|
||||
}
|
||||
let result;
|
||||
try {
|
||||
browser = await chromium.launch({ channel: 'msedge', headless: true }); page = await browser.newPage({ viewport: { width: 1440, height: 1000 } });
|
||||
page.on('pageerror', error => errors.push(error.message)); await page.goto(host.url); await page.getByLabel('当前项目').selectOption(f.project.id);
|
||||
let dialog = await openCenter(); const card = dialog.locator('.mc-connection').filter({ has: page.getByRole('heading', { name: 'Owned UI 模型', exact: true }) });
|
||||
await card.getByRole('button', { name: '原生参数', exact: true }).click();
|
||||
await page.getByLabel('原生参数 JSON', { exact: true }).fill('{"reasoning_effort":"high","temperature":0.4,"parallel_tool_calls":false}');
|
||||
await page.getByLabel('输出上限字段', { exact: true }).selectOption('max_completion_tokens');
|
||||
await page.getByRole('button', { name: '保存原生参数', exact: true }).click(); await expect(dialog.getByRole('status')).toContainText('原生参数已保存');
|
||||
assert.equal(requests.length, 0); await page.reload(); dialog = await openCenter();
|
||||
await dialog.locator('.mc-connection').filter({ has: page.getByRole('heading', { name: 'Owned UI 模型', exact: true }) }).getByRole('button', { name: '原生参数', exact: true }).click();
|
||||
await expect(page.getByLabel('原生参数 JSON', { exact: true })).toContainText('reasoning_effort');
|
||||
await capture('native-desktop'); await page.setViewportSize({ width: 390, height: 844 });
|
||||
await page.getByRole('button', { name: '保存原生参数', exact: true }).scrollIntoViewIfNeeded();
|
||||
const bounds = await page.getByRole('button', { name: '保存原生参数', exact: true }).boundingBox(); assert.ok(bounds && bounds.x >= 0 && bounds.x + bounds.width <= 390);
|
||||
assert.ok(await page.evaluate(() => document.documentElement.scrollWidth <= innerWidth)); await capture('native-390'); mark('native parameters persist after reload and the narrow form remains usable without calling a model');
|
||||
await page.getByLabel('原生参数 JSON', { exact: true }).fill('{"tools":[]}'); await page.getByRole('button', { name: '保存原生参数', exact: true }).click();
|
||||
await expect(page.getByRole('form', { name: '原生请求参数' }).getByRole('alert')).toContainText('尚未接入原生参数:tools');
|
||||
assert.equal(requests.length, 0); await page.keyboard.press('Escape'); await page.setViewportSize({ width: 1440, height: 1000 }); mark('unsupported field is rejected visibly before any dispatch');
|
||||
const first = await run('读取当前项目,HTTP错误原文验收'); assert.equal(requests.length, 1); assert.equal(first.changes.length, 0);
|
||||
assert.equal(requests[0].body.reasoning_effort, 'high'); assert.equal(requests[0].body.temperature, 0.4); assert.equal(requests[0].body.parallel_tool_calls, false);
|
||||
assert.ok(requests[0].body.max_completion_tokens > 0); assert.equal(requests[0].body.max_tokens, undefined);
|
||||
const failureCard = page.locator('.failure-card').last(); await failureCard.getByText('查看上游原文与请求信息', { exact: true }).click();
|
||||
await expect(failureCard).toContainText('owned-upstream-quota-ui'); await expect(failureCard).toContainText('insufficient_quota'); await expect(failureCard).toContainText('180 秒');
|
||||
assert.ok(!(await failureCard.innerText()).includes('owned-ui-private-key')); await capture('http-error-desktop');
|
||||
await failureCard.getByLabel('上游错误原文').scrollIntoViewIfNeeded(); await capture('http-raw-desktop');
|
||||
const downloadEvent = page.waitForEvent('download'); await failureCard.getByRole('button', { name: '下载已保存的原文', exact: true }).click();
|
||||
const download = await downloadEvent, downloaded = path.join(directory, 'owned-http-error.txt'); await download.saveAs(downloaded);
|
||||
assert.equal(createHash('sha256').update(fs.readFileSync(downloaded)).digest('hex'), first.failure.upstream.artifactSha256);
|
||||
await page.setViewportSize({ width: 390, height: 844 }); await failureCard.scrollIntoViewIfNeeded(); await capture('http-error-390');
|
||||
assert.ok(await page.evaluate(() => document.documentElement.scrollWidth <= innerWidth)); mark('actual HTTP error shows raw fields and original wait, keeps secrets hidden and downloads SHA-verified original at both widths');
|
||||
dialog = await openCenter(); await dialog.getByRole('button', { name: '请求账本', exact: true }).click();
|
||||
const attempt = dialog.locator('.mc-attempts article').first(); await attempt.getByText('查看上游原文与请求信息', { exact: true }).click();
|
||||
await expect(attempt).toContainText('owned-upstream-quota-ui'); await expect(attempt.getByRole('button', { name: '下载实际请求', exact: true })).toBeVisible();
|
||||
await capture('ledger-390'); await page.keyboard.press('Escape'); mark('physical request ledger preserves the same error origin and offers the sealed actual request');
|
||||
mode = 'stream'; let center = await host.api('/model-center'), current = center.connections.find(value => value.id === member.id);
|
||||
member = await host.api('/model-center/connections', { ...current, protocol: 'responses', streaming: true, capabilityDeclarations: { ...current.capabilityDeclarations, streaming: 'supported' },
|
||||
nativeParameters: { reasoning: { effort: 'medium' }, text: { verbosity: 'high' } }, outputTokenField: 'max_output_tokens' });
|
||||
const secondSession = await host.api('/sessions', { projectId: f.project.id });
|
||||
await page.evaluate(value => { localStorage.setItem('awb-project', value.projectId); localStorage.setItem('awb-session', value.id); }, secondSession);
|
||||
await page.reload(); await page.setViewportSize({ width: 1440, height: 1000 });
|
||||
const second = await run('读取当前项目,Responses流失败验收');
|
||||
assert.equal(requests.length, 2); assert.equal(requests[1].route, '/v1/responses'); assert.deepEqual(requests[1].body.reasoning, { effort: 'medium' });
|
||||
assert.equal(second.execution.run.usage.totalTokens, 24); assert.equal(second.changes.length, 0); assert.equal(second.execution.run.usage.requests, 1);
|
||||
const streamCard = page.locator('.failure-card').last(); await streamCard.getByText('查看上游原文与请求信息', { exact: true }).click();
|
||||
await expect(streamCard).toContainText('response.failed'); await expect(streamCard).toContainText('resp-owned-ui'); await expect(streamCard).toContainText('owned-upstream-stream-ui');
|
||||
await expect(streamCard).toContainText('流式响应已开始'); await capture('sse-error-desktop');
|
||||
await page.setViewportSize({ width: 390, height: 844 }); await streamCard.scrollIntoViewIfNeeded(); await capture('sse-error-390');
|
||||
await streamCard.getByLabel('上游错误原文').scrollIntoViewIfNeeded(); await capture('sse-raw-390');
|
||||
assert.ok(await page.evaluate(() => document.documentElement.scrollWidth <= innerWidth)); assert.deepEqual(errors, []);
|
||||
mark('Responses failed event retains original identity and reported use at both widths; partial tool is never dispatched or retried');
|
||||
mode = 'quota';
|
||||
await until(async () => (await host.api('/model-center')).connections.find(value => value.id === member.id).status === 'configured');
|
||||
dialog = await openCenter();
|
||||
await dialog.locator('.mc-connection').filter({ has: page.getByRole('heading', { name: 'Owned UI 模型', exact: true }) }).getByRole('button', { name: '验证响应(调用模型)', exact: true }).click();
|
||||
await expect(dialog.locator('.mc-notice')).toContainText('额度或消费限制');
|
||||
await dialog.locator('.mc-notice').getByText('查看上游原文与请求信息', { exact: true }).click();
|
||||
await expect(dialog.locator('.mc-notice')).toContainText('owned-upstream-quota-ui');
|
||||
await expect(dialog.locator('.mc-notice')).toContainText('仅保留以上诊断预览');
|
||||
assert.equal(requests.length, 3); assert.deepEqual(errors, []); await capture('probe-error-390');
|
||||
mark('explicit synthetic probe also shows the API diagnostic and labels its preview-only retention without inventing a Task artifact');
|
||||
result = { success: true, appRoot, checks, screenshots, errors, modelRequests: requests.length, realModelRequests: 0 };
|
||||
} catch (error) { if (page) await capture('failure').catch(() => {}); result = { success: false, appRoot, error: error.stack, checks, screenshots, errors, realModelRequests: 0 }; throw error; }
|
||||
finally { fs.writeFileSync(path.join(directory, 'result.json'), JSON.stringify(result, null, 2)); console.log(JSON.stringify({ success: result.success, directory, checks: checks.length, screenshots: screenshots.length }));
|
||||
await browser?.close(); await new Promise(resolve => { model.closeAllConnections(); model.close(resolve); }); for (const action of disposers.reverse()) await action(); }
|
||||
@@ -1,6 +1,7 @@
|
||||
import { Activity, LoaderCircle, RotateCw } from 'lucide-react';
|
||||
import type { RuntimeDiagnostics } from './types';
|
||||
import { Status } from './components';
|
||||
import { ModelErrorDetails } from './ModelErrorDetails';
|
||||
|
||||
export function DiagnosticsPanel({ value, busy, onRefresh }: { value: RuntimeDiagnostics | null; busy: boolean; onRefresh: () => void }) {
|
||||
if (!value) return <div className="diagnostic-loading"><LoaderCircle size={20} className="spin" />{busy ? '正在读取本机执行记录…' : '诊断记录暂时不可用,请刷新重试。'}<button type="button" className="button secondary" disabled={busy} onClick={onRefresh}>刷新</button></div>;
|
||||
@@ -16,7 +17,7 @@ export function DiagnosticsPanel({ value, busy, onRefresh }: { value: RuntimeDia
|
||||
<dt>当前项目上次核对</dt><dd>{value.projectCache.latest ? new Date(value.projectCache.latest.checkedAt).toLocaleString('zh-CN') : '本次进程尚未检索'}</dd></dl>
|
||||
{value.projectCache.latest && <p className="diagnostic-note">{value.projectCache.latest.reusedFiles} 份解析材料复用,{value.projectCache.latest.parsedFiles} 份重新解析;来源{value.projectCache.latest.sourceVerified ? '已核对' : '范围不完整'}。</p>}
|
||||
<p className="diagnostic-note">在工程「索引 → 解析缓存」管理开关与清除。此处仅显示派生缓存状态,不触发工程检索或模型请求。</p>{value.projectCache.error && <p className="diagnostic-note">{value.projectCache.error}</p>}</section>}
|
||||
{failure && <section className="diagnostic-failure"><div className="diagnostic-section-heading"><h3>最近的错误记录</h3><span>{failure.source === 'model_connection' ? '模型服务请求' : '工作台任务'}</span></div><strong>{failure.title}</strong><p>{failure.message}</p>{failure.detail && <p>{failure.detail}</p>}{failure.occurredAt && <small>{new Date(failure.occurredAt).toLocaleString('zh-CN')}</small>}{failure.suggestion && <p className="diagnostic-note">{failure.suggestion}</p>}</section>}
|
||||
{failure && <section className="diagnostic-failure"><div className="diagnostic-section-heading"><h3>最近的错误记录</h3><span>{failure.source === 'model_connection' ? '模型服务请求' : '工作台任务'}</span></div><strong>{failure.title}</strong><p>{failure.message}</p><ModelErrorDetails failure={failure} />{failure.occurredAt && <small>{new Date(failure.occurredAt).toLocaleString('zh-CN')}</small>}{failure.suggestion && <p className="diagnostic-note">{failure.suggestion}</p>}</section>}
|
||||
{!!value.session?.changes.length && <section><h3>文件改动的实际状态</h3><div className="diagnostic-changes">{value.session.changes.map(change => <div key={change.id}><code>{change.path}</code><Status value={change.status} /></div>)}</div></section>}
|
||||
{!!value.session?.recentTools.length && <section><h3>近期工具结果</h3><div className="diagnostic-tools">{value.session.recentTools.slice(-8).map((tool, index) => <details key={index}><summary><code>{tool.tool}</code><Status value={tool.phase} />{tool.exitCode !== null && <span>退出码 {tool.exitCode}</span>}</summary><pre>{tool.outputExcerpt || '没有文本输出。'}</pre></details>)}</div></section>}
|
||||
</div>;
|
||||
|
||||
@@ -20,3 +20,5 @@
|
||||
.mc-stream-receipt .button { margin-top: 8px; }
|
||||
.mc-editor > p { color: #475b73; font-size: 13px; line-height: 1.8; margin: 12px 0; }
|
||||
.mc-form-grid label > small { color: #475b73; font-size: 12px; line-height: 1.7; font-weight: 400; }
|
||||
|
||||
.mc-native>label{display:flex;flex-direction:column;gap:8px;font-size:12px;color:#40576e;margin:14px 0}.mc-native select,.mc-native textarea{width:100%;min-width:0;padding:10px;border:1px solid #a9bdcf;border-radius:5px;background:white;font-size:12px;line-height:1.75}.mc-native textarea{resize:vertical;font-family:Consolas,"Microsoft YaHei",monospace}.mc-native select:focus-visible,.mc-native textarea:focus-visible{outline:2px solid #2560a3;outline-offset:2px}
|
||||
|
||||
@@ -1,14 +1,16 @@
|
||||
import { useEffect, useRef, useState } from 'react';
|
||||
import { Activity, Check, CircleHelp, KeyRound, Layers3, Plus, Power, RefreshCw, Route, Server, Settings2 } from 'lucide-react';
|
||||
import { api, downloadArtifact } from './api';
|
||||
import type { Config, ModelSelection } from './types';
|
||||
import type { Config, ModelSelection, RuntimeFailure } from './types';
|
||||
import './ModelCenter.css';
|
||||
import { ModelWaitSettings, type WaitSettings } from './ModelWaitSettings';
|
||||
import { NativeParameters } from './NativeParameters';
|
||||
import { ModelErrorDetails } from './ModelErrorDetails';
|
||||
|
||||
type Connection = { id: string; name: string; brand: string; protocol: string; baseUrl: string; model: string; enabled: boolean; revision: number; hasKey: boolean;
|
||||
requiresKey: boolean; streaming?: boolean; capabilityDeclarations?: Record<string, string>; capabilities?: Record<string, string>; capabilityEvidence?: { checkedAt: string }; contextLimit: number; maxOutputTokens: number; concurrency: number; status: string; activeRequests: number; savedCredential?: { status: string }; health: { state: string; reason?: string; cooldownUntil?: number } };
|
||||
requiresKey: boolean; nativeParameters?: Record<string, unknown>; outputTokenField?: string; streaming?: boolean; capabilityDeclarations?: Record<string, string>; capabilities?: Record<string, string>; capabilityEvidence?: { checkedAt: string }; contextLimit: number; maxOutputTokens: number; concurrency: number; status: string; activeRequests: number; savedCredential?: { status: string }; health: { state: string; reason?: string; cooldownUntil?: number } };
|
||||
type Pool = { id?: string; name: string; members: { connectionId: string; weight: number }[]; strategy: string; sticky: boolean; maxAttempts: number; revision?: number; queueTimeoutMs: number };
|
||||
type Attempt = { id: string; connectionId: string; poolId: string | null; model: string; protocol?: string; streaming?: boolean; rejections?: { connectionName: string; capability: string; reason: string }[]; status: string; startedAt: string; ordinal: number; usageStatus: string; reason?: string; taskId?: string; sessionId?: string; runId: string; requestArtifactId?: string; responseArtifactId?: string; responseSha256?: string; responseRedacted?: boolean; stream?: { bytes: number; capturedBytes?: number; frames: number; sha256: string; terminal: boolean }; usage?: { prompt_tokens?: number; completion_tokens?: number } };
|
||||
type Attempt = { diagnostic?: RuntimeFailure; upstreamResponse?: { requestId?: string | null }; id: string; connectionId: string; poolId: string | null; model: string; protocol?: string; streaming?: boolean; rejections?: { connectionName: string; capability: string; reason: string }[]; status: string; startedAt: string; ordinal: number; usageStatus: string; reason?: string; taskId?: string; sessionId?: string; runId: string; requestArtifactId?: string; responseArtifactId?: string; responseSha256?: string; responseRedacted?: boolean; stream?: { bytes: number; capturedBytes?: number; frames: number; sha256: string; terminal: boolean }; usage?: { prompt_tokens?: number; completion_tokens?: number } };
|
||||
type ImportRecord = { id: string; source: string; path: string; enabled: boolean; lastCheckedAt?: string; lastSyncedAt?: string; lastError?: string; lastResult?: { imported?: number; conflicts?: string[] } };
|
||||
type Center = { revision: number; selected: { type: string; id: string }; waitSettings: WaitSettings; connections: Connection[]; pools: Pool[]; imports: ImportRecord[]; attempts: Attempt[]; waiting: number };
|
||||
type Operations = { checkedAt: string; version: string; scope: string; activeTasks: number; queue: number; checks: { id: string; title: string; status: string; detail: string }[];
|
||||
@@ -26,10 +28,12 @@ export function ModelCenter({ onConfig, onLegacy, busy }: { onConfig: (value: Co
|
||||
const [tab, setTab] = useState('connections');
|
||||
const [draft, setDraft] = useState<typeof initial & { id?: string; revision?: number }>(initial);
|
||||
const [editing, setEditing] = useState(false), [key, setKey] = useState('');
|
||||
const [nativeTarget, setNativeTarget] = useState<Connection | null>(null);
|
||||
const [persistKey, setPersistKey] = useState(false), [persistImports, setPersistImports] = useState(false);
|
||||
const [credentialView, setCredentialView] = useState<CredentialView | null>(null), [credentialKey, setCredentialKey] = useState(''), [credentialTarget, setCredentialTarget] = useState('');
|
||||
const [pool, setPool] = useState<Pool>(emptyPool), [poolEditing, setPoolEditing] = useState(false);
|
||||
const [working, setWorking] = useState(false), [notice, setNotice] = useState('');
|
||||
const [noticeFailure, setNoticeFailure] = useState<RuntimeFailure | null>(null);
|
||||
const [probeScopes, setProbeScopes] = useState<Record<string, string>>({});
|
||||
const [operations, setOperations] = useState<Operations | null>(null), [autoInspect, setAutoInspect] = useState(false);
|
||||
const [importPath, setImportPath] = useState(''), [importSource, setImportSource] = useState('ccswitch');
|
||||
@@ -39,9 +43,10 @@ export function ModelCenter({ onConfig, onLegacy, busy }: { onConfig: (value: Co
|
||||
const tabsRef = useRef<HTMLElement>(null);
|
||||
async function refresh() { const marker = ++generation.current; const result = await api<Center>('/model-center'); if (alive.current && marker === generation.current) setView(result); }
|
||||
async function act(action: () => Promise<void>) {
|
||||
setWorking(true); setNotice('');
|
||||
setWorking(true); setNotice(''); setNoticeFailure(null);
|
||||
try { await action(); if (alive.current) { await refresh(); if (credentialView) await refreshCredentials(); } }
|
||||
catch (cause) { if (alive.current) setNotice(cause instanceof Error ? cause.message : '操作未完成,请刷新核对。'); }
|
||||
catch (cause) { if (alive.current) { setNotice(cause instanceof Error ? cause.message : '操作未完成,请刷新核对。');
|
||||
if (cause && typeof cause === 'object' && 'diagnostic' in cause && cause.diagnostic) setNoticeFailure(cause.diagnostic as RuntimeFailure); } }
|
||||
finally { if (alive.current) setWorking(false); }
|
||||
}
|
||||
async function inspect(offset?: number) { const value = await api<Operations>('/operations' + (offset === undefined ? '' : '?artifactOffset=' + offset)); if (alive.current) setOperations(value); }
|
||||
@@ -65,7 +70,7 @@ export function ModelCenter({ onConfig, onLegacy, busy }: { onConfig: (value: Co
|
||||
<nav ref={tabsRef} className="mc-tabs" aria-label="模型中心分类">{[['connections', '模型连接', Server], ['pools', '模型池', Layers3], ['waits', '等待时限', Settings2], ['attempts', '请求账本', Route], ['imports', '配置导入', Settings2], ['credentials', '凭据保存', KeyRound], ['operations', '运行巡检', Activity]].map(([id, title, Icon]) => {
|
||||
const Symbol = Icon as typeof Server; return <button key={id as string} type="button" aria-current={tab === id ? 'page' : undefined} onClick={() => { setTab(id as string); setNotice(''); }}><Symbol size={16} />{title as string}</button>;
|
||||
})}<button className="mc-refresh" type="button" disabled={working} onClick={() => void act(refresh)} aria-label="刷新模型中心"><RefreshCw size={16} /></button></nav>
|
||||
{notice && <div className="mc-notice" role="status">{notice}</div>}
|
||||
{notice && <div className="mc-notice" role="status">{notice}{noticeFailure && <ModelErrorDetails failure={noticeFailure} />}</div>}
|
||||
{busy && <p className="mc-notice">有任务正在执行;连接可停用,其他配置请在任务结束后修改。</p>}
|
||||
{!view ? <p className="mc-empty">正在读取实际连接状态…</p> : <div className="mc-content">
|
||||
{tab === 'waits' && <ModelWaitSettings key={JSON.stringify(view.waitSettings)} value={view.waitSettings} disabled={locked} onSave={value => void act(async () => {
|
||||
@@ -83,9 +88,9 @@ export function ModelCenter({ onConfig, onLegacy, busy }: { onConfig: (value: Co
|
||||
<button className="button secondary" disabled={locked} onClick={() => { if (value.id === 'legacy-default') { onLegacy(); return; } setDraft({ ...initial, ...value, streaming: value.streaming === true, capabilityDeclarations: { ...initial.capabilityDeclarations, ...value.capabilityDeclarations } }); setKey(''); setPersistKey(false); setEditing(true); }}>编辑</button>
|
||||
{value.id !== 'legacy-default' && <button className="mc-icon-action" aria-label={(value.enabled ? '停用' : '启用') + value.name} disabled={working} onClick={() => void act(async () => { await api('/model-center/toggle', { id: value.id, enabled: !value.enabled }); })}><Power size={15} />{value.enabled ? '停用' : '启用'}</button>}
|
||||
<label className="mc-probe-scope">检测范围<select aria-label={value.name + '检测范围'} value={probeScopes[value.id] ?? 'text'} disabled={locked} onChange={event => setProbeScopes({ ...probeScopes, [value.id]: event.target.value })}><option value="text">文本响应</option><option value="tools">工具调用</option><option value="streaming">流式文本</option><option value="vision">合成图像识别</option></select></label>
|
||||
<button className="mc-probe" disabled={locked || !value.enabled} onClick={() => void act(async () => { const result = await api<{ scope: string }>('/model-center/probe', { id: value.id, scope: probeScopes[value.id] ?? 'text' }); if (alive.current) setNotice(capabilityLabels[result.scope] + '检测通过;只验证所选能力,原请求与用量已记入账本。'); })}>验证响应(调用模型)</button></footer>
|
||||
<button className="button secondary" disabled={locked} onClick={() => { setNativeTarget(value); setEditing(false); }}>原生参数</button><button className="mc-probe" disabled={locked || !value.enabled} onClick={() => void act(async () => { const result = await api<{ scope: string }>('/model-center/probe', { id: value.id, scope: probeScopes[value.id] ?? 'text' }); if (alive.current) setNotice(capabilityLabels[result.scope] + '检测通过;只验证所选能力,原请求与用量已记入账本。'); })}>验证响应(调用模型)</button></footer>
|
||||
</article>)}</div>
|
||||
{editing && <form className="mc-editor" aria-label="编辑模型连接" onSubmit={event => { event.preventDefault(); void act(async () => { await api('/model-center/connections', { ...draft, ...(key ? { apiKey: key } : {}), ...(persistKey ? { persistKey: true } : {}) }); if (alive.current) { setKey(''); setEditing(false); setNotice(persistKey ? '连接与本机加密凭据已一起保存。' : '连接已保存。'); } }); }}>
|
||||
{nativeTarget && <NativeParameters key={nativeTarget.id + ":" + nativeTarget.revision} connection={nativeTarget} busy={locked} onCancel={() => setNativeTarget(null)} onSave={async input => { await act(async () => { const result = await api<{ config: Config }>("/model-center/native-parameters", input); if (alive.current) { onConfig(result.config); setNativeTarget(null); setNotice("原生参数已保存;新任务按所选协议发送,旧请求保持原记录。"); } }); }} />}{editing && <form className="mc-editor" aria-label="编辑模型连接" onSubmit={event => { event.preventDefault(); void act(async () => { await api('/model-center/connections', { ...draft, ...(key ? { apiKey: key } : {}), ...(persistKey ? { persistKey: true } : {}) }); if (alive.current) { setKey(''); setEditing(false); setNotice(persistKey ? '连接与本机加密凭据已一起保存。' : '连接已保存。'); } }); }}>
|
||||
<h3>{draft.id ? '编辑模型连接' : '添加模型连接'}</h3><div className="mc-form-grid">
|
||||
<label>连接名称<input required value={draft.name} onChange={event => setDraft({ ...draft, name: event.target.value })} /></label>
|
||||
<label>服务品牌<select value={draft.brand} onChange={event => setDraft({ ...draft, brand: event.target.value })}>{['自定义', 'OpenAI', 'Claude 兼容代理', 'DeepSeek', 'Ollama', 'vLLM', '兼容服务'].map(brand => <option key={brand}>{brand}</option>)}</select></label>
|
||||
@@ -141,7 +146,7 @@ export function ModelCenter({ onConfig, onLegacy, busy }: { onConfig: (value: Co
|
||||
<fieldset><legend>模型池成员与权重</legend>{view.connections.map(connection => { const member = pool.members.find(value => value.connectionId === connection.id); return <label key={connection.id} className="mc-member-choice"><input type="checkbox" checked={!!member} onChange={event => setPool({ ...pool, members: event.target.checked ? [...pool.members, { connectionId: connection.id, weight: 1 }] : pool.members.filter(value => value.connectionId !== connection.id) })} /><span>{connection.name}<small>{connection.model || '尚未选择模型'} · {states[connection.status]}</small></span><input type="number" aria-label={connection.name + '权重'} disabled={!member} min="1" max="10" value={member?.weight ?? 1} onChange={event => setPool({ ...pool, members: pool.members.map(value => value.connectionId === connection.id ? { ...value, weight: Number(event.target.value) } : value) })} /></label>; })}</fieldset>
|
||||
<div className="mc-actions"><button className="button secondary" type="button" onClick={() => setPoolEditing(false)}>取消</button><button className="button primary" disabled={locked || !pool.members.length}>保存模型池</button></div></form>}
|
||||
</>}
|
||||
{tab === 'attempts' && <><div className="mc-section-title"><div><h3>请求账本</h3><p>最近100次物理请求;最多保存1000次。未知用量保持未知。</p></div><span>{view.attempts.length} 条记录</span></div>{!view.attempts.length && <p className="mc-empty">当前没有模型中心请求。选择连接或模型池执行任务后,实际尝试会保存在这里。</p>}<div className="mc-attempts">{view.attempts.map(value => <article key={value.id}><div><span className={'mc-state mc-state-' + value.status}>{states[value.status]}</span><strong>{value.model}</strong><time>{new Date(value.startedAt).toLocaleString()}</time></div><p>{view.connections.find(connection => connection.id === value.connectionId)?.name || '未派发'} · {value.protocol === 'responses' ? 'Responses' : value.protocol ? 'Chat Completions' : '能力筛选'}{value.streaming ? ' · 流式' : ''} · {value.ordinal ? '尝试 ' + value.ordinal : '未发送'} · {value.usageStatus === 'reported' ? `${value.usage?.prompt_tokens ?? '?'} 输入 / ${value.usage?.completion_tokens ?? '?'} 输出 tokens` : value.usageStatus === 'not_dispatched' ? '未产生请求用量' : '用量未知'}</p><code>Run {value.runId}</code>{value.stream && <div className="mc-stream-receipt"><p>{value.stream.terminal ? "响应完整终结" : "响应未完整,片段保留待核对"} · 已接收 {value.stream.bytes.toLocaleString()} bytes · {value.stream.frames} 个事件{value.stream.capturedBytes !== undefined && value.stream.capturedBytes < value.stream.bytes ? " · 仅保存上限内片段" : ""}</p><code>原流 SHA256 {value.stream.sha256}</code>{value.responseSha256 && <code>工件 SHA256 {value.responseSha256}{value.responseRedacted ? "(已脱敏)" : ""}</code>}{value.sessionId && value.responseArtifactId && <button className="button secondary" disabled={working} onClick={() => void act(async () => { await downloadArtifact(value.sessionId!, value.responseArtifactId!, "model-stream-" + value.id + ".sse"); })}>下载已保存的响应</button>}</div>}{value.reason && <p className="mc-card-warning">{value.reason}</p>}{!!value.rejections?.length && <p className="mc-route-rejections">{value.rejections.map(item => item.connectionName + ':' + (capabilityLabels[item.capability] ?? item.capability) + ' · ' + item.reason).join(';')}</p>}</article>)}</div></>}
|
||||
{tab === 'attempts' && <><div className="mc-section-title"><div><h3>请求账本</h3><p>最近100次物理请求;最多保存1000次。未知用量保持未知。</p></div><span>{view.attempts.length} 条记录</span></div>{!view.attempts.length && <p className="mc-empty">当前没有模型中心请求。选择连接或模型池执行任务后,实际尝试会保存在这里。</p>}<div className="mc-attempts">{view.attempts.map(value => <article key={value.id}><div><span className={'mc-state mc-state-' + value.status}>{states[value.status]}</span><strong>{value.model}</strong><time>{new Date(value.startedAt).toLocaleString()}</time></div><p>{view.connections.find(connection => connection.id === value.connectionId)?.name || '未派发'} · {value.protocol === 'responses' ? 'Responses' : value.protocol ? 'Chat Completions' : '能力筛选'}{value.streaming ? ' · 流式' : ''} · {value.ordinal ? '尝试 ' + value.ordinal : '未发送'} · {value.usageStatus === 'reported' ? `${value.usage?.prompt_tokens ?? '?'} 输入 / ${value.usage?.completion_tokens ?? '?'} 输出 tokens` : value.usageStatus === 'not_dispatched' ? '未产生请求用量' : '用量未知'}</p><code>Run {value.runId}</code>{value.stream && <div className="mc-stream-receipt"><p>{value.stream.terminal ? "响应完整终结" : "响应未完整,片段保留待核对"} · 已接收 {value.stream.bytes.toLocaleString()} bytes · {value.stream.frames} 个事件{value.stream.capturedBytes !== undefined && value.stream.capturedBytes < value.stream.bytes ? " · 仅保存上限内片段" : ""}</p><code>原流 SHA256 {value.stream.sha256}</code>{value.responseSha256 && <code>工件 SHA256 {value.responseSha256}{value.responseRedacted ? "(已脱敏)" : ""}</code>}{value.sessionId && value.responseArtifactId && <button className="button secondary" disabled={working} onClick={() => void act(async () => { await downloadArtifact(value.sessionId!, value.responseArtifactId!, "model-stream-" + value.id + ".sse"); })}>下载已保存的响应</button>}</div>}{value.reason && <p className="mc-card-warning">{value.reason}</p>}{value.diagnostic && <ModelErrorDetails failure={value.diagnostic} />}{!value.diagnostic && value.upstreamResponse?.requestId && <p className="mc-help">上游请求 ID:{value.upstreamResponse.requestId}</p>}{value.requestArtifactId && value.sessionId && <button className="button secondary" disabled={working} onClick={() => void act(async () => { await downloadArtifact(value.sessionId!, value.requestArtifactId!, "model-request-" + value.id + ".json"); })}>下载实际请求</button>}{!!value.rejections?.length && <p className="mc-route-rejections">{value.rejections.map(item => item.connectionName + ':' + (capabilityLabels[item.capability] ?? item.capability) + ' · ' + item.reason).join(';')}</p>}</article>)}</div></>}
|
||||
{tab === 'imports' && <><div className="mc-section-title"><div><h3>已有配置导入</h3><p>先预览来源和模型,再导入。原客户端配置保持原样。</p></div><button className="button secondary" disabled={working} onClick={() => void act(async () => { const value = await api<{ sources: typeof discovery }>('/model-center/imports'); if (alive.current) setDiscovery(value.sources); })}>识别本机来源</button></div>
|
||||
{view.imports.length > 0 && <><h4>已登记来源</h4>{view.imports.map(value => <article className="mc-import-source" key={value.id}><strong>{value.source}</strong><code>{value.path}</code><span>{value.enabled ? '每分钟检查' : '自动同步已关闭'}</span>{value.lastCheckedAt && <p>最近检查 {new Date(value.lastCheckedAt).toLocaleString()}</p>}{value.lastError && <p className="mc-card-warning">{value.lastError}</p>}{!!value.lastResult?.conflicts?.length && <p className="mc-card-warning">有 {value.lastResult.conflicts.length} 项与手动修改冲突,已保留工作台中的修改。</p>}<button className="button secondary" disabled={locked} onClick={() => void act(async () => { await api('/model-center/import-toggle', { id: value.id, enabled: !value.enabled }); })}>{value.enabled ? '关闭自动同步' : '启用自动同步'}</button></article>)}<button className="button secondary" disabled={locked || !view.imports.some(value => value.enabled)} onClick={() => void act(async () => { const result = await api<{ results: { imported?: string[]; conflicts?: string[]; error?: string }[] }>('/model-center/import-sync', {}); const imported = result.results.reduce((sum, value) => sum + (value.imported?.length ?? 0), 0); const conflicts = result.results.reduce((sum, value) => sum + (value.conflicts?.length ?? 0), 0); if (alive.current) setNotice(`已检查启用来源:更新 ${imported} 项,冲突 ${conflicts} 项。`); })}>立即同步已启用来源</button></>}
|
||||
{discovery.map(value => <article className="mc-import-source" key={value.source}><strong>{value.source}</strong><code>{value.path || '当前版本尚无可发现的配置路径'}</code><span>{value.available ? value.supported ? '可预览' : '当前不支持' : '未发现'}</span>{value.detail && <p>{value.detail}</p>}<button className="button secondary" disabled={!value.available || !value.supported || locked} onClick={() => { setImportSource(value.source); setImportPath(value.path); }}>选择来源</button></article>)}
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
.model-error-details{font-size:12px;line-height:1.7;margin-top:10px;min-width:0;color:#344e40}
|
||||
.model-error-details summary{cursor:pointer;font-weight:600;color:#365d48;padding:6px 0}
|
||||
.model-error-details summary:focus-visible,.model-error-details pre:focus-visible{outline:2px solid #4c795e;outline-offset:3px}
|
||||
.model-error-details dl{display:grid;grid-template-columns:minmax(84px,128px) minmax(0,1fr);gap:6px 14px;margin:12px 0}
|
||||
.model-error-details dt{color:#56695b}.model-error-details dd{margin:0;min-width:0;overflow-wrap:anywhere}
|
||||
.model-error-details pre{max-height:280px;max-width:100%;overflow:auto;white-space:pre-wrap;overflow-wrap:anywhere;padding:12px;background:#f2f4f1;border:1px solid #d5ded5;border-radius:5px;font-size:12px;line-height:1.7;color:#2c4134}
|
||||
.model-error-caption,.model-error-hash{font-size:11px;overflow-wrap:anywhere;color:#526556}.model-error-hash code{font-size:10px}
|
||||
.model-error-details .button{margin:6px 0;max-width:100%;min-height:38px}
|
||||
@media(max-width:560px){.model-error-details dl{grid-template-columns:1fr;gap:2px}.model-error-details dd{margin-bottom:7px}.model-error-details pre{padding:9px}}
|
||||
@@ -0,0 +1,46 @@
|
||||
import { useState } from 'react';
|
||||
import type { RuntimeFailure } from './types';
|
||||
import { downloadArtifact } from './api';
|
||||
import './ModelErrorDetails.css';
|
||||
|
||||
const decisions: Record<string, string> = { retry_after_wait: '按等待时间重试', stream_not_replayed: '流式响应已开始,保留原片段并停止自动重发',
|
||||
not_retryable: '该错误不在同一线路自动重试', attempt_limit: '达到本次尝试上限', wait_budget: '剩余等待预算不足,保持上游建议时间并结束本次请求' };
|
||||
|
||||
export function ModelErrorDetails({ failure }: { failure: RuntimeFailure }) {
|
||||
const upstream = failure.upstream;
|
||||
const requestId = upstream?.requestId ?? failure.upstreamRequestId;
|
||||
const [notice, setNotice] = useState(''), [busy, setBusy] = useState(false);
|
||||
const origin = failure.origin === 'upstream' ? '上游模型服务' : failure.origin === 'local' || failure.source === 'workbench' ? '工作台本地' : '旧记录未保存具体层级';
|
||||
return <details className="model-error-details"><summary>{upstream ? '查看上游原文与请求信息' : '查看错误详情'}</summary>
|
||||
<dl><dt>错误来源</dt><dd>{origin}</dd>
|
||||
{failure.model && <><dt>实际模型</dt><dd>{failure.model}</dd></>}
|
||||
{failure.endpoint && <><dt>实际接口</dt><dd>{failure.endpoint}{failure.route}</dd></>}
|
||||
{requestId && <><dt>上游请求 ID</dt><dd><code>{requestId}</code></dd></>}
|
||||
{upstream?.responseId && <><dt>上游响应 ID</dt><dd><code>{upstream.responseId}</code></dd></>}
|
||||
{failure.wireAttemptId && <><dt>本地物理尝试 ID</dt><dd><code>{failure.wireAttemptId}</code></dd></>}
|
||||
{upstream?.code && <><dt>上游 code</dt><dd><code>{upstream.code}</code></dd></>}
|
||||
{upstream?.type && <><dt>上游 type</dt><dd><code>{upstream.type}</code></dd></>}
|
||||
{upstream?.param && <><dt>上游参数位置</dt><dd>{upstream.param}</dd></>}
|
||||
{upstream?.eventType && <><dt>上游事件</dt><dd>{upstream.eventType}</dd></>}
|
||||
{upstream?.reason && <><dt>上游结束原因</dt><dd>{upstream.reason}</dd></>}
|
||||
{upstream?.retryAfter !== null && upstream?.retryAfter !== undefined && <><dt>原 Retry-After</dt><dd><code>{upstream.retryAfter}</code>{upstream.retryAfterMs !== null && upstream.retryAfterMs !== undefined ? ' · ' + upstream.retryAfterMs / 1000 + ' 秒' : ' · 未能解析,已停止自动重试'}</dd></>}
|
||||
{failure.limitReason === 'unspecified' && <><dt>限额类型</dt><dd>上游未提供明确原因</dd></>}
|
||||
{failure.retryDecision && <><dt>重试处理</dt><dd>{decisions[failure.retryDecision] ?? failure.retryDecision}</dd></>}
|
||||
</dl>
|
||||
{!upstream && failure.detail && <p>{failure.detail}</p>}
|
||||
{upstream?.rawPreview !== undefined && <><p className="model-error-caption">上游原文{upstream.scope === 'sse-event' ? ' · 失败事件' : ' · 响应正文'}{upstream.capture?.redacted ? ' · 已脱敏' : ''}{upstream.previewTruncated ? ' · 此处显示前 8000 字符' : ''}</p>
|
||||
<pre tabIndex={0} aria-label="上游错误原文">{upstream.rawPreview || '(空响应)'}</pre>
|
||||
{upstream.capture && <p className="model-error-caption">已接收 {upstream.capture.receivedBytes.toLocaleString()} 字节,保留前 {upstream.capture.capturedBytes.toLocaleString()} 字节。{upstream.capture.complete ? '该范围完整接收。' : upstream.capture.truncated ? '已达到保存上限,仅保留前缀;完整长度未知。' : '接收中断,原文可能不完整。'}{upstream.artifactScope === 'sse-stream' ? '下载文件包含已保存的整段事件流。' : ''}</p>}
|
||||
</>}
|
||||
{!!upstream?.shortenedFields?.length && <p className="model-error-caption">部分诊断字段已缩短,详见保存的原文。</p>}
|
||||
{upstream?.capture?.retention === 'preview-only' && <p className="model-error-caption">此请求未绑定工程任务,仅保留以上诊断预览;完整原文未归档为任务工件。</p>}
|
||||
{upstream?.artifactSha256 && <p className="model-error-hash">工件 SHA-256 <code>{upstream.artifactSha256}</code></p>}
|
||||
{upstream?.artifactId && upstream.sessionId && <button className="button secondary" type="button" disabled={busy} onClick={async () => {
|
||||
setBusy(true); setNotice('');
|
||||
try { await downloadArtifact(upstream.sessionId!, upstream.artifactId!, 'model-error-' + (failure.wireAttemptId ?? upstream.artifactId) + (upstream.artifactScope === 'sse-stream' ? '.sse' : '.txt')); setNotice('已下载保存的脱敏响应。'); }
|
||||
catch (cause) { setNotice(cause instanceof Error ? cause.message : '下载失败,请重试。'); }
|
||||
finally { setBusy(false); }
|
||||
}}>下载已保存的原文</button>}
|
||||
{notice && <p role="status">{notice}</p>}
|
||||
</details>;
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
import { useEffect, useRef, useState } from 'react';
|
||||
import { nativeParameterSettings } from '../shared/contracts/native-parameters.mjs';
|
||||
|
||||
export type NativeConnection = { id: string; revision: number; name: string; protocol: string; nativeParameters?: Record<string, unknown>; outputTokenField?: string };
|
||||
export function NativeParameters({ connection, busy, onCancel, onSave }: { connection: NativeConnection; busy: boolean; onCancel: () => void;
|
||||
onSave: (input: { id: string; revision: number; nativeParameters: Record<string, unknown>; outputTokenField: string }) => Promise<void> }) {
|
||||
const [text, setText] = useState(JSON.stringify(connection.nativeParameters ?? {}, null, 2));
|
||||
const [field, setField] = useState(connection.outputTokenField ?? (connection.protocol === 'responses' ? 'max_output_tokens' : 'max_tokens'));
|
||||
const [error, setError] = useState('');
|
||||
const form = useRef<HTMLFormElement>(null);
|
||||
useEffect(() => { form.current?.scrollIntoView({ block: 'nearest' }); }, []);
|
||||
return <form ref={form} className="mc-editor mc-native" aria-label="原生请求参数" onSubmit={async event => {
|
||||
event.preventDefault(); setError('');
|
||||
try { const settings = nativeParameterSettings(connection.protocol, JSON.parse(text), field); await onSave({ id: connection.id, revision: connection.revision, ...settings }); }
|
||||
catch (cause) { setError(cause instanceof Error ? cause.message : '参数格式无效。'); }
|
||||
}}>
|
||||
<h3>{connection.name} · 原生请求参数</h3>
|
||||
<p className="mc-help">参数按所选协议原字段发送,并封存在实际请求中。具体模型是否接受这些选项,以服务响应为准;未填写的选项采用上游默认值。</p>
|
||||
<label>输出上限字段<select aria-label="输出上限字段" value={field} disabled={busy} onChange={event => setField(event.target.value)}>{(connection.protocol === 'responses' ? ['max_output_tokens'] : ['max_tokens', 'max_completion_tokens']).map(value => <option key={value}>{value}</option>)}</select></label>
|
||||
<p className="mc-help">数值沿用当前任务和连接的输出预算,不允许原生参数覆盖预算、消息、工具或接口。</p>
|
||||
<label>原生参数 JSON<textarea aria-label="原生参数 JSON" rows={7} maxLength={8192} spellCheck={false} value={text} disabled={busy} onChange={event => setText(event.target.value)} /></label>
|
||||
<p className="mc-help">{connection.protocol === 'responses' ? '已接入 temperature、top_p、parallel_tool_calls、reasoning.effort、text.verbosity。' : '已接入 temperature、top_p、seed、parallel_tool_calls、reasoning_effort、verbosity。'}其他字段保存时会给出具体提示。语音、视频和服务端内置工具尚未接入。</p>
|
||||
{error && <p className="mc-card-warning" role="alert">{error}</p>}
|
||||
<div className="mc-actions"><button className="button secondary" type="button" disabled={busy} onClick={onCancel}>取消</button><button className="button primary" disabled={busy} type="submit">保存原生参数</button></div>
|
||||
</form>;
|
||||
}
|
||||
+1
-1
@@ -7,7 +7,7 @@ export async function api<T>(path: string, input?: unknown,method?:string): Prom
|
||||
...(input === undefined ? {} : { body: JSON.stringify(input) }),
|
||||
});
|
||||
const body = await response.json();
|
||||
if (!response.ok) throw new Error(body.error || '请求失败,请重试。');
|
||||
if (!response.ok) throw Object.assign(new Error(body.error || '请求失败,请重试。'), { status: response.status, diagnostic: body.diagnostic ?? null });
|
||||
return body as T;
|
||||
}
|
||||
export async function exportSession(id: string) {
|
||||
|
||||
@@ -3,6 +3,7 @@ import type { ReactNode, ButtonHTMLAttributes } from 'react';
|
||||
import { createPortal } from 'react-dom';
|
||||
import { AlertTriangle, Check, ChevronRight, Circle, Clock3, FileCode2, LoaderCircle, RotateCw, ShieldQuestion, SquareTerminal, X, XCircle } from 'lucide-react';
|
||||
import type { Change, ToolEvent, RuntimeFailure } from './types';
|
||||
import { ModelErrorDetails } from './ModelErrorDetails';
|
||||
|
||||
const statusNames: Record<string, string> = {
|
||||
idle: '就绪', running: '执行中', awaiting_approval: '等待确认', needs_review: '待审查', completed: '已完成',
|
||||
@@ -109,7 +110,7 @@ export function FailureCard({ failure, onDiagnose, onRetry, busy = false }: { fa
|
||||
<p>{failure.message}</p>
|
||||
{failure.suggestion && <p className="failure-suggestion">{failure.suggestion}</p>}
|
||||
<div className="failure-meta"><span>{failure.stage === 'model_request' ? '发生在请求模型响应时' : '发生在工作台任务执行时'}</span>{failure.attempts && <span>{failure.attempts} 次请求</span>}{failure.httpStatus && <code>HTTP {failure.httpStatus}</code>}{failure.code && <code>{failure.code}</code>}</div>
|
||||
{failure.detail && failure.detail !== failure.message && <details><summary>查看错误详情</summary><p>{failure.detail}</p></details>}
|
||||
<ModelErrorDetails failure={failure} />
|
||||
<div className="failure-actions"><button type="button" className="button secondary" onClick={onDiagnose}>查看运行诊断</button>{onRetry && <button type="button" className="button primary" onClick={onRetry} disabled={busy}><RotateCw size={14} />{failure.source === 'model_connection' ? '重试模型请求' : '继续任务'}</button>}</div>
|
||||
</div>;
|
||||
}
|
||||
|
||||
@@ -53,6 +53,12 @@ export interface RuntimeFailure {
|
||||
source: string; stage: string; kind: string; title: string; message: string; messageId?: string;
|
||||
detail?: string; suggestion?: string; attempts?: number; httpStatus?: number; code?: string | null;
|
||||
occurredAt?: string; retryable?: boolean; historical?: boolean; endpoint?: string; model?: string;
|
||||
origin?: 'upstream' | 'local'; route?: string; type?: string | null; wireAttemptId?: string; upstreamRequestId?: string | null; retryAfterMs?: number; retryDecision?: string; limitReason?: string | null;
|
||||
upstream?: { schema: number; scope: string; httpStatus?: number; code?: string | null; type?: string | null; message?: string | null; param?: string | null;
|
||||
requestId?: string | null; responseId?: string | null; eventType?: string | null; reason?: string | null; headers?: Record<string, string>; retryAfter?: string | null;
|
||||
retryAfterMs?: number | null; retryAfterInvalid?: boolean; rawPreview?: string; previewTruncated?: boolean; shortenedFields?: string[];
|
||||
artifactId?: string; artifactSha256?: string; artifactScope?: string; sessionId?: string;
|
||||
capture?: { scope: string; receivedBytes: number; capturedBytes: number; storedBytes: number; retention?: string; complete: boolean; truncated: boolean; interrupted: boolean; redacted: boolean; sha256: string; capturedRange: number[] } };
|
||||
}
|
||||
export interface ExecutionEnvironment { platform: string; node: string; shell: string; encoding: string; shellSyntax: string }
|
||||
export interface RuntimeDiagnostics {
|
||||
|
||||
@@ -6,6 +6,8 @@ import { visionProbe } from './vision-probe.mjs';
|
||||
import { modelProtocol, capabilityDeclarations, capabilityView, capabilityRejections } from '../../shared/contracts/model-capabilities.mjs';
|
||||
import { actualRequest, requestVariants } from '../../shared/contracts/model-request.mjs';
|
||||
import { modelWaitSettings } from './request-deadline.mjs';
|
||||
import { sanitizeModelText } from './upstream-error.mjs';
|
||||
import { nativeParameterSettings } from '../../shared/contracts/native-parameters.mjs';
|
||||
|
||||
const legacyId = 'legacy-default';
|
||||
const stamp = () => new Date().toISOString();
|
||||
@@ -75,7 +77,8 @@ export class ModelHub {
|
||||
connections() {
|
||||
return [{ id: legacyId, name: '原模型连接', brand: '兼容服务', protocol: 'chat-completions', baseUrl: this.store.config.baseUrl,
|
||||
model: this.store.config.model, enabled: true, requiresKey: false, revision: this.store.config.configRevision ?? 1,
|
||||
contextLimit: this.store.config.contextLimit, maxOutputTokens: this.store.config.maxOutputTokens, concurrency: 1, source: 'legacy' }, ...this.state.connections];
|
||||
contextLimit: this.store.config.contextLimit, maxOutputTokens: this.store.config.maxOutputTokens, concurrency: 1, source: 'legacy',
|
||||
...(this.store.config.nativeParameters ? { nativeParameters: this.store.config.nativeParameters, outputTokenField: this.store.config.outputTokenField } : {}) }, ...this.state.connections];
|
||||
}
|
||||
credential(connection) { return connection.id === legacyId ? { key: this.provider.apiKey, revision: this.provider.legacyCredentialRevision ?? 1 } : this.provider.credentials.get(connection.id); }
|
||||
vault() { return this.credentialVault ??= new CredentialVault(this.store, this.provider); }
|
||||
@@ -166,6 +169,8 @@ export class ModelHub {
|
||||
if (this.state.connections.length >= 100 && !previous) throw error('最多保存100个模型连接。');
|
||||
if (previous && input.revision !== previous.revision) throw error('模型连接已修改,请刷新后重试。', 409);
|
||||
const protocol = modelProtocol(input.protocol ?? previous?.protocol ?? 'chat-completions');
|
||||
const native = Object.hasOwn(input, 'nativeParameters') || Object.hasOwn(input, 'outputTokenField') || previous?.nativeParameters
|
||||
? nativeParameterSettings(protocol, input.nativeParameters ?? previous?.nativeParameters ?? {}, input.outputTokenField ?? previous?.outputTokenField) : null;
|
||||
const declarations = capabilityDeclarations(input.capabilityDeclarations, previous?.capabilityDeclarations);
|
||||
if (input.streaming !== undefined && typeof input.streaming !== 'boolean') throw error('流式选项必须为开启或关闭。');
|
||||
const streaming = input.streaming ?? previous?.streaming ?? false;
|
||||
@@ -173,7 +178,7 @@ export class ModelHub {
|
||||
const name = label(input.name), model = label(input.model, 200);
|
||||
if (!name || !model) throw error('请填写连接名称和实际模型名称。');
|
||||
const connection = { id: previous?.id ?? randomUUID(), name, brand: label(input.brand || '自定义', 40), protocol, model,
|
||||
capabilitySchema: 1, capabilityDeclarations: declarations, streaming,
|
||||
capabilitySchema: 1, capabilityDeclarations: declarations, streaming, ...(native ?? {}),
|
||||
baseUrl: modelEndpoint(input.baseUrl), enabled: input.enabled !== false, requiresKey: input.requiresKey !== false,
|
||||
contextLimit: bounded(input.contextLimit, 16000, 1000, 200000), maxOutputTokens: bounded(input.maxOutputTokens, 2048, 256, 16000),
|
||||
concurrency: bounded(input.concurrency, 1, 1, 8), revision: (previous?.revision ?? 0) + 1,
|
||||
@@ -191,6 +196,14 @@ export class ModelHub {
|
||||
}
|
||||
return this.status(connection);
|
||||
}
|
||||
setNativeParameters(input) {
|
||||
const connection = this.connection(input.id);
|
||||
if (connection.revision !== input.revision) throw error('连接已变化,请刷新后核对原生参数。', 409);
|
||||
const settings = nativeParameterSettings(connection.protocol, input.nativeParameters, input.outputTokenField);
|
||||
if (connection.id === legacyId) this.save(settings);
|
||||
else this.upsert({ ...connection, ...settings });
|
||||
return { center: this.view(), config: { ...this.store.config, hasKey: this.provider.hasKey() } };
|
||||
}
|
||||
toggle(id, enabled) {
|
||||
if (id === legacyId) throw error('原连接不能停用,请选择离线演示或其他连接。');
|
||||
const value = this.connection(id); value.enabled = enabled === true; value.revision++; value.updatedAt = stamp(); this.save(); return this.status(value);
|
||||
@@ -230,6 +243,7 @@ export class ModelHub {
|
||||
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, protocol: modelProtocol(members[0].protocol), streaming: members[0].streaming === true,
|
||||
nativeParameters: members[0].nativeParameters ?? {}, outputTokenField: members[0].outputTokenField,
|
||||
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 } };
|
||||
@@ -310,6 +324,8 @@ export class ModelHub {
|
||||
}
|
||||
this.attempts.push(attempt); this.saveAttempts();
|
||||
const progress = event => {
|
||||
if (event.wireAttemptId) attempt.wireAttemptId = event.wireAttemptId;
|
||||
if (event.upstreamResponse) attempt.upstreamResponse = event.upstreamResponse;
|
||||
if (event.consumption) {
|
||||
attempt.usage = event.consumption.usage; attempt.usageStatus = event.consumption.usageStatus;
|
||||
onProgress?.({ consumption: { ...event.consumption, wireAttemptId: event.consumption.attemptId, attemptId: attempt.id,
|
||||
@@ -317,16 +333,21 @@ export class ModelHub {
|
||||
this.saveAttempts();
|
||||
return;
|
||||
}
|
||||
if (event.streamCapture !== undefined) {
|
||||
attempt.stream = event.stream;
|
||||
if (event.streamCapture !== undefined || event.responseCapture !== undefined) {
|
||||
if (event.stream) 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 raw = event.streamCapture ?? event.responseCapture, decoded = raw.toString('utf8'), redacted = sanitizeModelText(decoded, value => this.provider.redact(value));
|
||||
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' });
|
||||
const artifact = this.store.artifactStore.write(session, run, captured, { kind: event.streamCapture !== undefined ? 'model-stream-response' : 'model-http-response',
|
||||
mediaType: event.streamCapture !== undefined ? 'text/event-stream' : 'text/plain' });
|
||||
this.store.core.artifact.set(artifact.id, artifact); attempt.responseArtifactId = artifact.id;
|
||||
attempt.responseSha256 = artifact.sha256; attempt.responseRedacted = redacted !== decoded; this.store.save(session);
|
||||
attempt.responseSha256 = artifact.sha256; attempt.responseRedacted = redacted !== decoded || event.upstream?.capture?.redacted === true; this.store.save(session);
|
||||
if (event.upstream) Object.assign(event.upstream, { artifactId: artifact.id, artifactSha256: artifact.sha256,
|
||||
artifactScope: event.streamCapture !== undefined ? 'sse-stream' : 'http-body', sessionId: session.id });
|
||||
if (event.upstream?.capture) Object.assign(event.upstream.capture, { retention: 'artifact', storedBytes: artifact.bytes });
|
||||
}
|
||||
if (event.upstream) attempt.upstream = event.upstream;
|
||||
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 };
|
||||
@@ -340,6 +361,7 @@ export class ModelHub {
|
||||
return result;
|
||||
} catch (cause) {
|
||||
const diagnostic = cause.diagnostic;
|
||||
if (diagnostic) attempt.diagnostic = diagnostic;
|
||||
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)) ?? {};
|
||||
@@ -348,14 +370,14 @@ export class ModelHub {
|
||||
// the attempt, release its lease, and leave the connection eligible.
|
||||
this.health.set(this.identity(member), { ...health, state: 'interrupted', lastInterruptedAt: stamp() });
|
||||
} else {
|
||||
const blocked = [401, 403].includes(attempt.httpStatus);
|
||||
const blocked = [401, 403].includes(attempt.httpStatus) || diagnostic?.kind === 'quota';
|
||||
const failedAt = Date.now();
|
||||
this.health.set(this.identity(member), { state: blocked ? 'needs_attention' : 'failed', blocked, lastFailureAt: new Date(failedAt).toISOString(),
|
||||
failures: (health.failures ?? 0) + 1, cooldownUntil: failedAt + (diagnostic?.retryAfterMs ?? Math.min(60000, 1000 * 2 ** Math.min(health.failures ?? 0, 5))), reason: attempt.reason });
|
||||
}
|
||||
lastError = cause; excluded.add(member.id); this.sticky.delete(runId);
|
||||
signal?.throwIfAborted();
|
||||
if (!diagnostic?.retryable || count === routing.maxAttempts) throw cause;
|
||||
if ((!diagnostic?.retryable && !diagnostic?.failoverAllowed) || count === routing.maxAttempts) throw cause;
|
||||
onProgress?.({ phase: 'retrying', attempt: count, maxAttempts: routing.maxAttempts, connectionId: member.id, modelAttemptId: attempt.id,
|
||||
diagnostic, waitMs: 0, switching: true });
|
||||
} finally { attempt.finishedAt = stamp(); lease.release(); this.saveAttempts(); }
|
||||
|
||||
@@ -1,14 +1,17 @@
|
||||
import { createHash } from 'node:crypto';
|
||||
import { protocolFailure, normalizeProtocolResponse, validateModelResponse, reportedUsage } from './protocols.mjs';
|
||||
import { httpFailure } from '../../server/diagnostics.mjs';
|
||||
import { upstreamCapture } from './upstream-error.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, tools, onBytes) {
|
||||
export async function readModelStream(response, config, signal, onProgress, tools, onBytes, redact = String) {
|
||||
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, progressTimer = null, progressFailure = null;
|
||||
let upstreamFailure = null;
|
||||
const calls = new Map(), responseCalls = new Map();
|
||||
const progress = force => {
|
||||
if (!force && Date.now() - lastProgress < 100) {
|
||||
@@ -30,7 +33,18 @@ export async function readModelStream(response, config, signal, onProgress, tool
|
||||
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 (event.usage) usage = event.usage;
|
||||
const failUpstream = () => {
|
||||
usage = event.response?.usage ?? usage;
|
||||
const { details } = upstreamCapture({ raw: Buffer.from(raw, 'utf8'), event, scope: 'sse-event', status: response.status, headers: response.headers }, redact);
|
||||
details.responseId ??= responseId;
|
||||
upstreamFailure = httpFailure(response.status, (details.message ?? details.reason ?? '上游事件 ' + event.type).slice(0, 600), { upstream: details });
|
||||
if (event.type === 'response.incomplete') {
|
||||
upstreamFailure.diagnostic.kind = 'incomplete'; upstreamFailure.diagnostic.title = '上游模型响应未完整结束';
|
||||
}
|
||||
throw upstreamFailure;
|
||||
};
|
||||
if (event.error || event.type === 'error') failUpstream();
|
||||
if (config.protocol === 'responses') {
|
||||
if (event.sequence_number !== undefined) {
|
||||
if (!Number.isSafeInteger(event.sequence_number) || event.sequence_number <= sequence) throw protocolFailure('Responses事件序列重复或逆序。');
|
||||
@@ -54,8 +68,7 @@ export async function readModelStream(response, config, signal, onProgress, tool
|
||||
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)) {
|
||||
usage = event.response?.usage ?? usage;
|
||||
throw protocolFailure('Responses流未完整结束,工具未执行。', 'stream_interrupted');
|
||||
failUpstream();
|
||||
}
|
||||
} else {
|
||||
if (!Array.isArray(event.choices)) throw protocolFailure('Chat流缺少choices数组。');
|
||||
@@ -135,7 +148,8 @@ export async function readModelStream(response, config, signal, onProgress, tool
|
||||
} finally {
|
||||
if (progressTimer) clearTimeout(progressTimer);
|
||||
try { await reader.cancel(); } catch {} reader.releaseLock();
|
||||
onProgress?.({ streamCapture: Buffer.concat(chunks), reportedUsage: reportedUsage(config, { usage: terminal?.usage ?? usage }), 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' } });
|
||||
onProgress?.({ streamCapture: Buffer.concat(chunks), ...(upstreamFailure ? { upstream: upstreamFailure.diagnostic.upstream } : {}),
|
||||
reportedUsage: reportedUsage(config, { usage: terminal?.usage ?? usage }), stream: { bytes, capturedBytes: chunks.reduce((sum, item) => sum + item.length, 0), frames,
|
||||
sha256: hash.digest('hex'), terminal: validated, terminalSeen: !!terminal || done || !!upstreamFailure, protocol: config.protocol ?? 'chat-completions' } });
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { ModelRequestError } from '../../server/diagnostics.mjs';
|
||||
export const protocolFailure = (message, kind = 'protocol') => new ModelRequestError({ kind, source: 'model_connection', stage: 'model_request',
|
||||
export const protocolFailure = (message, kind = 'protocol') => new ModelRequestError({ kind, source: 'model_connection', origin: 'local', stage: 'model_request',
|
||||
title: '模型响应未通过完整性核对', message, retryable: false, occurredAt: new Date().toISOString() });
|
||||
export const protocolRoute = config => config.protocol === 'responses' ? '/responses' : '/chat/completions';
|
||||
|
||||
@@ -55,9 +55,9 @@ export function normalizeProtocolResponse(config, value) {
|
||||
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 throw protocolFailure('Responses包含当前尚未接入的输出内容类型:' + String(content?.type ?? '未标注').slice(0, 100) + '。');
|
||||
}
|
||||
} else if (item.type !== 'reasoning') throw protocolFailure('Responses包含当前尚未接入的服务端工具或输出类型。');
|
||||
} else if (item.type !== 'reasoning') throw protocolFailure('Responses包含当前尚未接入的服务端工具或输出类型:' + String(item.type ?? '未标注').slice(0, 100) + '。');
|
||||
}
|
||||
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;
|
||||
|
||||
@@ -0,0 +1,89 @@
|
||||
import { createHash } from 'node:crypto';
|
||||
|
||||
export const ERROR_CAPTURE_LIMIT = 64 * 1024;
|
||||
const PREVIEW_LIMIT = 8000;
|
||||
const allowedHeaders = ['content-type', 'retry-after', 'date', 'x-request-id', 'request-id', 'requestid', 'x-amzn-requestid',
|
||||
'anthropic-request-id', 'x-ratelimit-limit-requests', 'x-ratelimit-remaining-requests', 'x-ratelimit-reset-requests',
|
||||
'x-ratelimit-limit-tokens', 'x-ratelimit-remaining-tokens', 'x-ratelimit-reset-tokens'];
|
||||
|
||||
export function sanitizeModelText(value, redact = String) {
|
||||
return redact(String(value))
|
||||
.replace(/("(?:api[_-]?key|authorization|access[_-]?token|refresh[_-]?token|password|secret|cookie|set-cookie)"\s*:\s*")((?:\\.|[^"\\])*)(")/gi, '$1[已隐藏]$3')
|
||||
.replace(/\bBearer\s+[A-Za-z0-9._~+/=-]+/gi, 'Bearer [已隐藏]');
|
||||
}
|
||||
|
||||
export function retryAfterMilliseconds(value, now = Date.now()) {
|
||||
if (!value) return null;
|
||||
const milliseconds = /^\d+$/.test(value) ? Number(value) * 1000 : Date.parse(value) - now;
|
||||
return Number.isSafeInteger(milliseconds) ? Math.max(0, milliseconds) : null;
|
||||
}
|
||||
|
||||
// Captures are bounded independently of the generated-response limit. An
|
||||
// interrupted or truncated prefix never claims to be the complete response.
|
||||
export async function readErrorResponse(response, signal) {
|
||||
if (!response.body) return { raw: Buffer.alloc(0), receivedBytes: 0, truncated: false };
|
||||
const reader = response.body.getReader(), parts = [];
|
||||
let receivedBytes = 0, capturedBytes = 0, truncated = false, interrupted = false;
|
||||
try {
|
||||
while (true) {
|
||||
signal?.throwIfAborted();
|
||||
const { done, value } = await reader.read();
|
||||
if (done) break;
|
||||
receivedBytes += value.length;
|
||||
const keep = Math.min(value.length, ERROR_CAPTURE_LIMIT - capturedBytes);
|
||||
if (keep) { parts.push(Buffer.from(value.subarray(0, keep))); capturedBytes += keep; }
|
||||
if (receivedBytes > ERROR_CAPTURE_LIMIT) { truncated = true; break; }
|
||||
}
|
||||
} catch (cause) {
|
||||
interrupted = true;
|
||||
} finally {
|
||||
try { await reader.cancel(); } catch {}
|
||||
reader.releaseLock();
|
||||
}
|
||||
return { raw: Buffer.concat(parts), receivedBytes, truncated, ...(interrupted ? { interrupted: true } : {}) };
|
||||
}
|
||||
|
||||
export function upstreamCapture({ raw, receivedBytes = raw.length, truncated = false, interrupted = false, status,
|
||||
headers, event, scope = 'http-body' }, redact = String) {
|
||||
const bounded = raw.subarray(0, ERROR_CAPTURE_LIMIT), decoded = bounded.toString('utf8');
|
||||
const text = sanitizeModelText(decoded, redact), changed = text !== decoded;
|
||||
const capture = changed ? Buffer.from(text, 'utf8') : Buffer.from(bounded);
|
||||
let parsed = event;
|
||||
if (!parsed) { try { parsed = JSON.parse(decoded); } catch {} }
|
||||
const original = parsed?.response?.error ?? parsed?.error;
|
||||
const fields = original && typeof original === 'object' ? original : parsed;
|
||||
const shortenedFields = [];
|
||||
const field = (name, value, limit = 512) => {
|
||||
if (typeof value !== 'string' && typeof value !== 'number') return null;
|
||||
const clean = sanitizeModelText(value, redact);
|
||||
if (clean.length > limit) shortenedFields.push(name);
|
||||
return clean.slice(0, limit);
|
||||
};
|
||||
const diagnosticHeaders = {};
|
||||
for (const name of allowedHeaders) {
|
||||
const value = headers?.get(name);
|
||||
if (value !== null && value !== undefined) diagnosticHeaders[name] = field('header:' + name, value, 1024);
|
||||
}
|
||||
const retryAfter = diagnosticHeaders['retry-after'] ?? null;
|
||||
const retryAfterMs = retryAfterMilliseconds(retryAfter);
|
||||
const details = {
|
||||
schema: 1, scope, httpStatus: status,
|
||||
code: field('code', fields?.code, 160), type: field('type', fields?.type ?? (parsed?.type === 'error' ? null : parsed?.type), 160),
|
||||
message: field('message', fields?.message ?? (typeof original === 'string' ? original : null), 4096),
|
||||
param: field('param', fields?.param), reason: field('reason', parsed?.response?.incomplete_details?.reason ?? parsed?.incomplete_details?.reason),
|
||||
requestId: diagnosticHeaders['x-request-id'] ?? diagnosticHeaders['request-id'] ?? diagnosticHeaders.requestid
|
||||
?? diagnosticHeaders['anthropic-request-id'] ?? diagnosticHeaders['x-amzn-requestid']
|
||||
?? field('requestId', parsed?.request_id ?? fields?.request_id ?? parsed?.requestId),
|
||||
responseId: field('responseId', parsed?.response?.id ?? (parsed?.object === 'response' ? parsed.id : null)),
|
||||
eventType: scope === 'sse-event' ? field('eventType', parsed?.type) : null,
|
||||
headers: diagnosticHeaders, retryAfter, retryAfterMs,
|
||||
...(retryAfter && retryAfterMs === null ? { retryAfterInvalid: true } : {}),
|
||||
rawPreview: text.slice(0, PREVIEW_LIMIT), previewTruncated: text.length > PREVIEW_LIMIT,
|
||||
capture: { scope, receivedBytes, capturedBytes: bounded.length, storedBytes: Buffer.byteLength(text.slice(0, PREVIEW_LIMIT), 'utf8'), retention: 'preview-only',
|
||||
complete: !truncated && !interrupted && raw.length <= ERROR_CAPTURE_LIMIT,
|
||||
truncated: truncated || raw.length > ERROR_CAPTURE_LIMIT, interrupted, capturedRange: [0, bounded.length],
|
||||
redacted: changed, sha256: createHash('sha256').update(capture).digest('hex') },
|
||||
...(shortenedFields.length ? { shortenedFields } : {}),
|
||||
};
|
||||
return { capture, details };
|
||||
}
|
||||
@@ -12,6 +12,7 @@ export const DEFAULT_CONFIG = {
|
||||
export function cleanConfig(value = {}) {
|
||||
const result = { ...DEFAULT_CONFIG };
|
||||
for (const key of Object.keys(result)) if (Object.hasOwn(value, key)) result[key] = value[key];
|
||||
for (const key of ['nativeParameters', 'outputTokenField']) if (Object.hasOwn(value, key)) result[key] = structuredClone(value[key]);
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
+4
-1
@@ -17,6 +17,7 @@ import { hostAuthority } from '../core/desktop/host-authority.mjs';
|
||||
import { companionGateway } from './companion-gateway.mjs';
|
||||
import { inspectOperations } from './operations.mjs';
|
||||
import { validateModelWaitSettings } from '../core/models/request-deadline.mjs';
|
||||
import { nativeParameterSettings } from '../shared/contracts/native-parameters.mjs';
|
||||
|
||||
export function createSessionPublisher(clients) {
|
||||
return session => {
|
||||
@@ -173,6 +174,7 @@ return async (request, response) => {
|
||||
return json(response, { center: hub.view(), config: { ...store.config, hasKey: provider.hasKey() } });
|
||||
}
|
||||
if (route === '/api/model-center/credentials') return json(response, hub.manageCredential(input));
|
||||
if (route === '/api/model-center/native-parameters') return json(response, hub.setNativeParameters(input));
|
||||
if (route === '/api/model-center/connections') return json(response, hub.upsert(input), 201);
|
||||
if (route === '/api/model-center/pools') return json(response, hub.pool(input), 201);
|
||||
if (route === '/api/model-center/select') return json(response, { center: hub.select(input), config: { ...store.config, hasKey: provider.hasKey() } });
|
||||
@@ -731,7 +733,8 @@ return async (request, response) => {
|
||||
const config = { ...store.config, baseUrl: String(input.baseUrl || store.config.baseUrl), model: String(input.model || store.config.model), mode: 'compatible' };
|
||||
provider.validate(config);
|
||||
const startedAt = Date.now();
|
||||
const result = await provider.request(config, '/chat/completions', { model: config.model, stream: false, max_tokens: 64,
|
||||
const native = nativeParameterSettings('chat-completions', config.nativeParameters ?? {}, config.outputTokenField);
|
||||
const result = await provider.request(config, '/chat/completions', { model: config.model, stream: false, [native.outputTokenField]: 64, ...native.nativeParameters,
|
||||
messages: [{ role: 'user', content: 'Connection check. Reply with OK.' }] }, undefined,
|
||||
Object.hasOwn(input, 'apiKey') ? String(input.apiKey).trim() : provider.apiKey);
|
||||
if (!result.choices?.[0]?.message) throw new Error('模型服务响应中缺少 choices[0].message。');
|
||||
|
||||
+21
-10
@@ -1,6 +1,6 @@
|
||||
import { randomUUID, createHash } from 'node:crypto';
|
||||
|
||||
export const APP_VERSION = '1.20.2';
|
||||
export const APP_VERSION = '1.20.3';
|
||||
|
||||
export function executionEnvironment() {
|
||||
const windows = process.platform === 'win32';
|
||||
@@ -22,7 +22,8 @@ export class ModelRequestError extends Error {
|
||||
}
|
||||
|
||||
export function connectionFailure(error, { endpoint, model, route, attempt, redact = String, timedOut = false, timeout = null }) {
|
||||
if (error instanceof ModelRequestError) return { endpoint, model, route, ...error.diagnostic, attempts: attempt };
|
||||
if (error instanceof ModelRequestError) return { ...error.diagnostic, endpoint: error.diagnostic.endpoint ?? endpoint,
|
||||
model: error.diagnostic.model ?? model, route: error.diagnostic.route ?? route, attempts: attempt };
|
||||
const causes = [error, error?.cause, ...(error?.cause?.errors ?? [])];
|
||||
const code = causes.map(item => item?.code).find(value => typeof value === 'string' && /^[A-Z0-9_]{2,60}$/.test(value)) ?? null;
|
||||
const kind = timedOut || causes.some(item => item?.name === 'TimeoutError') ? 'timeout'
|
||||
@@ -31,7 +32,7 @@ export function connectionFailure(error, { endpoint, model, route, attempt, reda
|
||||
const title = kind === 'timeout' ? '模型服务响应超时' : kind === 'dns' ? '模型服务地址解析失败'
|
||||
: kind === 'certificate' ? '模型服务证书校验失败' : '模型服务连接失败';
|
||||
return {
|
||||
id: randomUUID(), source: 'model_connection', stage: 'model_request', kind, code,
|
||||
id: randomUUID(), source: 'model_connection', origin: 'local', stage: 'model_request', kind, code,
|
||||
endpoint, model, route, attempts: attempt, occurredAt: new Date().toISOString(),
|
||||
retryable: kind !== 'certificate', title,
|
||||
message: title + (code ? '(' + code + ')' : '') + '。' + (timeout ? timeout.kind === 'idle'
|
||||
@@ -45,17 +46,27 @@ export function connectionFailure(error, { endpoint, model, route, attempt, reda
|
||||
};
|
||||
}
|
||||
|
||||
export function httpFailure(status, detail, { endpoint, model, route, attempt }) {
|
||||
const kind = status === 401 || status === 403 ? 'authentication' : status === 429 ? 'rate_limit' : 'service';
|
||||
export function httpFailure(status, detail, { endpoint, model, route, attempt, upstream, attemptId } = {}) {
|
||||
const codes = [upstream?.code, upstream?.type].filter(value => typeof value === 'string').map(value => value.toLowerCase());
|
||||
const quota = codes.some(value => ['insufficient_quota', 'billing_hard_limit_reached', 'billing_limit_reached', 'usage_limit_reached',
|
||||
'credit_balance_too_low', 'insufficient_balance', 'exceeded_quota'].includes(value));
|
||||
const limited = status === 429 || codes.some(value => ['rate_limit_exceeded', 'rate_limit_error', 'requests_limit_exceeded', 'tokens_limit_exceeded'].includes(value));
|
||||
const kind = quota ? 'quota' : status === 401 || status === 403 ? 'authentication' : limited ? 'rate_limit' : 'service';
|
||||
const title = kind === 'authentication' ? '模型服务拒绝了身份验证'
|
||||
: kind === 'rate_limit' ? '模型服务请求达到限额' : '模型服务暂时无法处理请求';
|
||||
: kind === 'quota' ? '模型服务额度或消费限制' : kind === 'rate_limit' ? '模型服务请求达到限额' : '模型服务暂时无法处理请求';
|
||||
return new ModelRequestError({
|
||||
id: randomUUID(), source: 'model_connection', stage: 'model_request', kind, httpStatus: status,
|
||||
id: randomUUID(), source: 'model_connection', origin: 'upstream', stage: 'model_request', kind, httpStatus: status,
|
||||
endpoint, model, route, attempts: attempt, occurredAt: new Date().toISOString(),
|
||||
retryable: [408, 429, 500, 502, 503, 504].includes(status), title,
|
||||
message: title + '(HTTP ' + status + ')。' + (detail ? ' ' + detail : ''), detail,
|
||||
retryable: !quota && !upstream?.retryAfterInvalid && (limited || [408, 500, 502, 503, 504].includes(status)),
|
||||
...(quota ? { failoverAllowed: true } : {}), title,
|
||||
...(upstream ? { upstream, upstreamRequestId: upstream.requestId, code: upstream.code, type: upstream.type,
|
||||
...(upstream.retryAfterMs !== null ? { retryAfterMs: upstream.retryAfterMs } : {}),
|
||||
limitReason: quota ? 'quota' : limited ? codes.some(value => /rate_limit|requests_limit|tokens_limit/.test(value)) ? 'rate' : 'unspecified' : null } : {}),
|
||||
...(attemptId ? { wireAttemptId: attemptId } : {}),
|
||||
message: title + (status >= 400 ? '(HTTP ' + status + ')' : '(上游返回错误内容)') + '。' + (detail ? ' ' + detail : ''), detail,
|
||||
suggestion: kind === 'authentication' ? '在模型连接中核对 API Key、模型权限和接口地址。'
|
||||
: kind === 'rate_limit' ? '等待服务限额恢复后重试;同时检查账户额度。' : '稍后重试,或检查模型服务状态。',
|
||||
: quota ? '核对上游账户余额、消费上限或用量限制。该线路不会自动重复请求;模型池可按规则选择其他可用成员。'
|
||||
: kind === 'rate_limit' ? '依据上游错误码核对限制原因;有 Retry-After 时按原等待时间处理,原因未给出时保持未确定。' : '稍后重试,或检查模型服务状态。',
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
+54
-22
@@ -7,6 +7,7 @@ import { ModelHub } from '../core/models/model-hub.mjs';
|
||||
import { normalizeProtocolResponse, protocolRoute, validateModelResponse, reportedUsage } from '../core/models/protocols.mjs';
|
||||
import { readModelStream } from '../core/models/model-stream.mjs';
|
||||
import { modelWaitSettings, requestDeadline } from '../core/models/request-deadline.mjs';
|
||||
import { readErrorResponse, upstreamCapture } from '../core/models/upstream-error.mjs';
|
||||
|
||||
export const engineToolDefinitions = [
|
||||
{ type: 'function', function: { name: 'inspect_engine_checks', description: '读取当前Task已经保存的外部引擎连接核验:真实安装版本、启动完成信号、SDK会话、进程关闭、原报告工件和能力限制。不会启动引擎或发送prompt;完整原记录通过已有read_artifact读取。', parameters: { type: 'object', properties: {}, additionalProperties: false } } },
|
||||
@@ -121,7 +122,10 @@ export class Provider {
|
||||
prepareConfig(config, store) { return this.modelHub(store).prepare(config); }
|
||||
redact(value, key = this.apiKey) {
|
||||
let text = String(value);
|
||||
for (const secret of [key, this.apiKey, ...[...(this.credentials?.values() ?? [])].map(value => value.key)].filter(Boolean).sort((a, b) => b.length - a.length)) text = text.replaceAll(secret, '[已隐藏]');
|
||||
for (const secret of [key, this.apiKey, ...[...(this.credentials?.values() ?? [])].map(value => value.key)].filter(Boolean).sort((a, b) => b.length - a.length)) {
|
||||
const encoded = JSON.stringify(secret).slice(1, -1);
|
||||
text = text.replaceAll(encoded, '[已隐藏]').replaceAll(secret, '[已隐藏]');
|
||||
}
|
||||
return text;
|
||||
}
|
||||
validate(config) {
|
||||
@@ -148,14 +152,31 @@ export class Provider {
|
||||
attempts = attempt;
|
||||
const timeout = requestDeadline({ signal, timeoutMs: Math.max(1, Math.min(policy.timeoutMs, deadline - Date.now())), idleTimeoutMs: policy.idleTimeoutMs });
|
||||
const requestSignal = timeout.signal;
|
||||
const context = { endpoint: base, model: config.model, route, attempt };
|
||||
onProgress?.({ phase: 'running', attempt, maxAttempts: policy.attempts, endpoint: base, route });
|
||||
let readingStream = false, streamTerminalSeen = false, dispatched = false, usage = null, outcome = 'unknown';
|
||||
const receipt = { attemptId: randomUUID(), endpoint: base, model: config.model, protocol: config.protocol ?? 'chat-completions', startedAt: new Date().toISOString() };
|
||||
const context = { endpoint: base, model: config.model, route, attempt, attemptId: receipt.attemptId };
|
||||
onProgress?.({ phase: 'running', attempt, maxAttempts: policy.attempts, endpoint: base, route, wireAttemptId: receipt.attemptId });
|
||||
let responseDetails = null;
|
||||
const captureResponse = (response, input) => {
|
||||
const captured = upstreamCapture({ ...input, status: response.status, headers: response.headers }, value => this.redact(value, key));
|
||||
responseDetails = captured.details;
|
||||
onProgress?.({ responseCapture: captured.capture, upstream: responseDetails, attempt, maxAttempts: policy.attempts, wireAttemptId: receipt.attemptId });
|
||||
return responseDetails;
|
||||
};
|
||||
try {
|
||||
dispatched = true;
|
||||
const response = await (this.fetcher ?? fetch)(base + route, { method: body ? 'POST' : 'GET', headers,
|
||||
body: body ? JSON.stringify(body) : undefined, signal: requestSignal });
|
||||
const metadata = upstreamCapture({ raw: Buffer.alloc(0), status: response.status, headers: response.headers }, value => this.redact(value, key)).details;
|
||||
receipt.upstreamRequestId = metadata.requestId; receipt.httpStatus = response.status;
|
||||
onProgress?.({ phase: 'running', attempt, maxAttempts: policy.attempts, upstreamResponse: { httpStatus: response.status, requestId: metadata.requestId, headers: metadata.headers } });
|
||||
if (!response.ok) {
|
||||
const received = await readErrorResponse(response, requestSignal);
|
||||
if (!received.truncated && !received.interrupted) { try { usage = reportedUsage(config, JSON.parse(received.raw.toString('utf8'))); } catch {} }
|
||||
const upstream = captureResponse(response, received);
|
||||
requestSignal.throwIfAborted();
|
||||
throw httpFailure(response.status, (upstream.message ?? upstream.rawPreview).slice(0, 600), { ...context, upstream });
|
||||
}
|
||||
if (response.ok && body?.stream === true) {
|
||||
readingStream = true;
|
||||
timeout.beginStream();
|
||||
@@ -163,31 +184,37 @@ export class Provider {
|
||||
if (progress.reportedUsage) usage = progress.reportedUsage;
|
||||
if (progress.stream?.terminalSeen) streamTerminalSeen = true;
|
||||
onProgress?.({ ...progress, attempt, maxAttempts: policy.attempts });
|
||||
}, body.tools, length => timeout.receive(length));
|
||||
}, body.tools, length => timeout.receive(length), value => this.redact(value, key));
|
||||
const value = options.transformResponse ? options.transformResponse(streamed.value) : streamed.value;
|
||||
outcome = 'completed';
|
||||
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;
|
||||
return value;
|
||||
}
|
||||
const text = await boundedResponse(response);
|
||||
try { usage = reportedUsage(config, JSON.parse(text)); } catch {}
|
||||
if (!response.ok) {
|
||||
let message = text;
|
||||
try { message = JSON.parse(text).error?.message ?? text; } catch {}
|
||||
const failure = httpFailure(response.status, this.redact(message, key).slice(0, 600), context);
|
||||
const retryAfter = response.headers.get('retry-after');
|
||||
if (retryAfter) {
|
||||
const milliseconds = /^\d+$/.test(retryAfter) ? Number(retryAfter) * 1000 : Date.parse(retryAfter) - Date.now();
|
||||
if (Number.isFinite(milliseconds)) failure.diagnostic.retryAfterMs = Math.max(0, Math.min(milliseconds, 90000));
|
||||
}
|
||||
throw failure;
|
||||
}
|
||||
let result;
|
||||
try { result = JSON.parse(text); } catch {
|
||||
const upstream = captureResponse(response, { raw: Buffer.from(text, 'utf8') });
|
||||
throw new ModelRequestError({ ...context, id: randomUUID(), source: 'model_connection', stage: 'model_request',
|
||||
origin: 'local', upstream,
|
||||
kind: 'protocol', title: '模型接口响应格式不兼容', message: '模型服务没有返回有效 JSON,请核对兼容接口地址。',
|
||||
attempts: attempt, retryable: false, occurredAt: new Date().toISOString(), suggestion: '填写服务实际提供的 Chat Completions 接口根地址。' });
|
||||
}
|
||||
if (result.error || config.protocol === 'responses' && ['failed', 'incomplete', 'cancelled'].includes(result.status)) {
|
||||
const upstream = captureResponse(response, { raw: Buffer.from(text, 'utf8') });
|
||||
const failure = httpFailure(response.status, (upstream.message ?? upstream.reason ?? upstream.rawPreview).slice(0, 600), { ...context, upstream });
|
||||
if (result.status === 'incomplete') { failure.diagnostic.kind = 'incomplete'; failure.diagnostic.title = '上游模型响应未完整结束'; }
|
||||
throw failure;
|
||||
}
|
||||
if (options.transformResponse) {
|
||||
try { result = options.transformResponse(result); }
|
||||
catch (cause) {
|
||||
const upstream = captureResponse(response, { raw: Buffer.from(text, 'utf8') });
|
||||
if (cause.diagnostic) cause.diagnostic.upstream = upstream;
|
||||
throw cause;
|
||||
}
|
||||
}
|
||||
this.lastRequest = { source: 'model_connection', endpoint: base, route, model: config.model, status: 'completed', attempts: attempt, durationMs: Date.now() - startedAt };
|
||||
outcome = 'completed';
|
||||
onProgress?.({ phase: 'completed', attempt, maxAttempts: policy.attempts, durationMs: Date.now() - startedAt });
|
||||
@@ -196,11 +223,18 @@ export class Provider {
|
||||
outcome = signal?.aborted ? 'interrupted' : readingStream && !streamTerminalSeen ? 'unknown' : 'failed';
|
||||
signal?.throwIfAborted();
|
||||
let diagnostic = connectionFailure(error, { ...context, timedOut: !!timeout.timeout, timeout: timeout.timeout, redact: value => this.redact(value, key) });
|
||||
if (readingStream) diagnostic = { ...diagnostic, retryable: false, outcome: streamTerminalSeen ? 'failed' : 'unknown', kind: timeout.timeout ? 'timeout' : streamTerminalSeen || diagnostic.kind === 'protocol' ? 'protocol' : 'stream_interrupted',
|
||||
message: diagnostic.message + (streamTerminalSeen ? ' 服务流已终结,响应校验未通过;原流保留,不自动重发。' : ' 已接收的流片段保留,请求不会自动重发。') };
|
||||
diagnostic = { ...diagnostic, origin: diagnostic.origin ?? 'local', wireAttemptId: receipt.attemptId, upstreamRequestId: diagnostic.upstreamRequestId ?? receipt.upstreamRequestId,
|
||||
...(responseDetails && !diagnostic.upstream ? { upstream: responseDetails } : {}) };
|
||||
if (readingStream) diagnostic = { ...diagnostic, retryable: false, failoverAllowed: false, outcome: streamTerminalSeen ? 'failed' : 'unknown',
|
||||
kind: diagnostic.origin === 'upstream' ? diagnostic.kind : timeout.timeout ? 'timeout' : streamTerminalSeen || diagnostic.kind === 'protocol' ? 'protocol' : 'stream_interrupted',
|
||||
...(diagnostic.origin === 'upstream' ? { suggestion: '可查看原失败事件、上游请求 ID 和已保存的响应。核对服务状态后手动续接;该流不会自动重发。' } : {}),
|
||||
message: diagnostic.message + (diagnostic.origin === 'upstream' ? ' 已保存上游失败事件与原流,工具未执行,不会自动重发。'
|
||||
: streamTerminalSeen ? ' 服务流已终结,响应校验未通过;原流保留,不自动重发。' : ' 已接收的流片段保留,请求不会自动重发。') };
|
||||
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;
|
||||
diagnostic.retryDecision = retry ? 'retry_after_wait' : readingStream ? 'stream_not_replayed' : !diagnostic.retryable ? 'not_retryable' : attempt >= policy.attempts ? 'attempt_limit' : 'wait_budget';
|
||||
diagnostic.remainingWaitBudgetMs = Math.max(0, remaining);
|
||||
this.lastRequest = { ...diagnostic, status: retry ? 'retrying' : 'failed', durationMs: Date.now() - startedAt };
|
||||
onProgress?.({ phase: retry ? 'retrying' : 'failed', attempt, maxAttempts: policy.attempts,
|
||||
diagnostic, waitMs: retry ? waitMs : 0, durationMs: Date.now() - startedAt });
|
||||
@@ -232,10 +266,8 @@ export class Provider {
|
||||
if (config.mode === 'demo') return this.demo(messages, project);
|
||||
this.validate(config);
|
||||
const execute = async (selected, key, input, progress) => {
|
||||
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 validateModelResponse(value, tools);
|
||||
return this.request(selected, protocolRoute(selected), input, signal, key, progress, { ...(config.modelRouting ? { attempts: 1 } : {}), ...requestPolicy,
|
||||
transformResponse: raw => validateModelResponse(normalizeProtocolResponse(selected, raw), tools) });
|
||||
};
|
||||
const result = config.modelRouting && this.hub ? await this.hub.complete(config, body, signal, onProgress, requestPlan, execute)
|
||||
: await execute(config, this.apiKey, body, onProgress);
|
||||
|
||||
+13
-1
@@ -1,6 +1,7 @@
|
||||
import { spawn, execFile } from 'node:child_process';
|
||||
import path from 'node:path';
|
||||
import { randomUUID } from 'node:crypto';
|
||||
import { sanitizeModelText } from '../core/models/upstream-error.mjs';
|
||||
import { StringDecoder } from 'node:string_decoder';
|
||||
import { setImmediate as yieldExecution } from 'node:timers/promises';
|
||||
import { safePath, listFiles, readLines, searchFiles, proposeFile, proposePatch } from './files.mjs';
|
||||
@@ -317,6 +318,17 @@ export class Runtime {
|
||||
if (run.worker) { planned.record.workerUsageAccounted = true; this.store.save(session); }
|
||||
return;
|
||||
}
|
||||
if (progress.responseCapture !== undefined || progress.streamCapture !== undefined) {
|
||||
const raw = progress.responseCapture ?? progress.streamCapture;
|
||||
const decoded = raw.toString('utf8'), redacted = sanitizeModelText(decoded, value => this.provider.redact(value));
|
||||
const captured = redacted === decoded ? raw : Buffer.from(redacted, 'utf8');
|
||||
const artifact = this.store.artifactStore.write(session, run.record, captured, { kind: progress.streamCapture !== undefined ? 'model-stream-response' : 'model-http-response', mediaType: 'text/plain' });
|
||||
this.store.core.artifact.set(artifact.id, artifact);
|
||||
if (progress.upstream) Object.assign(progress.upstream, { artifactId: artifact.id, artifactSha256: artifact.sha256,
|
||||
artifactScope: progress.streamCapture !== undefined ? 'sse-stream' : 'http-body', sessionId: session.id });
|
||||
if (progress.upstream?.capture) Object.assign(progress.upstream.capture, { retention: 'artifact', storedBytes: artifact.bytes });
|
||||
this.store.save(session); return;
|
||||
}
|
||||
if (!requestEvent) requestEvent = this.store.event(session, { kind: externalEngine ? 'engine' : 'model', runId: run.id,
|
||||
source: externalEngine ? 'controlled_engine' : 'model_connection', label: externalEngine ? '受控引擎执行' : '请求模型服务', phase: 'running', startedAt: now(), contextPlanId: planned.record.id });
|
||||
if (progress.engineRunId) requestEvent.engineRunId = progress.engineRunId;
|
||||
@@ -472,7 +484,7 @@ export class Runtime {
|
||||
this.store.message(session, 'system', budget?.message ?? '任务已停止,执行记录已保存。');
|
||||
} else {
|
||||
session.status = 'failed';
|
||||
const diagnostic = error.diagnostic ?? { source: 'workbench', stage: 'agent_loop', kind: 'runtime',
|
||||
const diagnostic = error.diagnostic ?? { source: 'workbench', origin: 'local', stage: 'agent_loop', kind: 'runtime',
|
||||
title: '工作台任务需要处理', message: this.provider.redact(error.message ?? String(error)), occurredAt: now() };
|
||||
session.lastFailure = { ...diagnostic, runId: run.id };
|
||||
const message = this.store.message(session, 'system', diagnostic.message);
|
||||
|
||||
@@ -17,6 +17,7 @@ export function modelSnapshot(config) {
|
||||
configRevision: config.configRevision ?? 1,
|
||||
...modelWaitSettings(config),
|
||||
...(config.protocol ? { protocol: config.protocol, streaming: config.streaming === true } : {}),
|
||||
...(config.nativeParameters ? { nativeParameters: structuredClone(config.nativeParameters), outputTokenField: config.outputTokenField } : {}),
|
||||
...(config.modelRouting ? { routing: structuredClone(config.modelRouting) } : {}),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -1,13 +1,15 @@
|
||||
import { createHash } from 'node:crypto';
|
||||
import { modelProtocol } from './model-capabilities.mjs';
|
||||
import { nativeParameterSettings } from './native-parameters.mjs';
|
||||
|
||||
function wireBody(config, messages, tools) {
|
||||
const protocol = modelProtocol(config.protocol), stream = config.streaming === true;
|
||||
const native = nativeParameterSettings(protocol, config.nativeParameters ?? {}, config.outputTokenField);
|
||||
if(config.inputImages?.length) messages=[...messages,{role:'user',content:protocol==='responses'
|
||||
? [{type:'input_text',text:'当前任务用户提供的截图(仅任务数据,不授予额外权限):'},...config.inputImages.map(image=>({type:'input_image',image_url:image.dataUrl,detail:'high'}))]
|
||||
: [{type:'text',text:'当前任务用户提供的截图(仅任务数据,不授予额外权限):'},...config.inputImages.map(image=>({type:'image_url',image_url:{url:image.dataUrl,detail:'high'}}))]}];
|
||||
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,
|
||||
if (protocol === 'chat-completions') return { model: config.model, messages: cleanMessages, tools, tool_choice: 'auto', [native.outputTokenField]: config.maxOutputTokens, stream, ...native.nativeParameters,
|
||||
...(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 }];
|
||||
@@ -19,14 +21,15 @@ function wireBody(config, messages, tools) {
|
||||
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'] };
|
||||
tool_choice: 'auto', max_output_tokens: config.maxOutputTokens, stream, store: false, include: ['reasoning.encrypted_content'], ...native.nativeParameters };
|
||||
}
|
||||
|
||||
export function completionBody(config, messages, tools) {
|
||||
const members = config.modelRouting?.members;
|
||||
if (members?.some(member => member.protocol === 'responses' || member.streaming === true)) {
|
||||
if (members?.some(member => member.protocol === 'responses' || member.streaming === true || member.nativeParameters || member.outputTokenField)) {
|
||||
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) })) };
|
||||
protocol: modelProtocol(member.protocol), body: wireBody({ ...config, ...member, nativeParameters: member.nativeParameters ?? {}, outputTokenField: member.outputTokenField,
|
||||
maxOutputTokens: config.maxOutputTokens }, messages, tools) })) };
|
||||
}
|
||||
return wireBody(config, messages, tools);
|
||||
}
|
||||
@@ -42,7 +45,7 @@ export function actualRequest(body, member) {
|
||||
return structuredClone(variant.body);
|
||||
}
|
||||
|
||||
export const outputReservation = body => body.max_output_tokens ?? body.max_tokens;
|
||||
export const outputReservation = body => body.max_output_tokens ?? body.max_completion_tokens ?? body.max_tokens;
|
||||
|
||||
export function requestHistory(body) {
|
||||
if (body.messages) return body.messages;
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
export function nativeParameterSettings(protocol?: string, parameters?: Record<string, unknown>, outputTokenField?: string): {
|
||||
nativeParameters: Record<string, unknown>;
|
||||
outputTokenField: string;
|
||||
};
|
||||
@@ -0,0 +1,24 @@
|
||||
const fail = message => { throw Object.assign(new Error(message), { status: 400 }); };
|
||||
const object = value => value && typeof value === 'object' && !Array.isArray(value);
|
||||
const efforts = ['none', 'minimal', 'low', 'medium', 'high', 'xhigh'];
|
||||
const verbosity = ['low', 'medium', 'high'];
|
||||
|
||||
export function nativeParameterSettings(protocol = 'chat-completions', parameters = {}, outputTokenField) {
|
||||
if (!object(parameters) || JSON.stringify(parameters).length > 8192) fail('原生参数必须是8KB以内的JSON对象。');
|
||||
const allowed = protocol === 'responses' ? ['temperature', 'top_p', 'parallel_tool_calls', 'reasoning', 'text']
|
||||
: ['temperature', 'top_p', 'parallel_tool_calls', 'reasoning_effort', 'verbosity', 'seed'];
|
||||
for (const [key, value] of Object.entries(parameters)) {
|
||||
if (!allowed.includes(key)) fail('所选协议尚未接入原生参数:' + key + '。参数未保存或发送。');
|
||||
if (key === 'temperature' && (typeof value !== 'number' || !Number.isFinite(value) || value < 0 || value > 2)) fail('temperature应为0–2之间的数字。');
|
||||
if (key === 'top_p' && (typeof value !== 'number' || !Number.isFinite(value) || value < 0 || value > 1)) fail('top_p应为0–1之间的数字。');
|
||||
if (key === 'seed' && !Number.isSafeInteger(value)) fail('seed应为安全整数。');
|
||||
if (key === 'parallel_tool_calls' && typeof value !== 'boolean') fail('parallel_tool_calls应为布尔值。');
|
||||
if (key === 'reasoning_effort' && !efforts.includes(value)) fail('reasoning_effort应为none/minimal/low/medium/high/xhigh。');
|
||||
if (key === 'verbosity' && !verbosity.includes(value)) fail('verbosity应为low/medium/high。');
|
||||
if (key === 'reasoning' && (!object(value) || Object.keys(value).length !== 1 || !efforts.includes(value.effort))) fail('Responses reasoning当前接入格式为{"effort":"low|medium|high等"}。');
|
||||
if (key === 'text' && (!object(value) || Object.keys(value).length !== 1 || !verbosity.includes(value.verbosity))) fail('Responses text当前接入格式为{"verbosity":"low|medium|high"}。');
|
||||
}
|
||||
const field = outputTokenField ?? (protocol === 'responses' ? 'max_output_tokens' : 'max_tokens');
|
||||
if (!(protocol === 'responses' ? ['max_output_tokens'] : ['max_tokens', 'max_completion_tokens']).includes(field)) fail('输出上限字段与所选协议不兼容。');
|
||||
return { nativeParameters: structuredClone(parameters), outputTokenField: field };
|
||||
}
|
||||
@@ -1,5 +1,15 @@
|
||||
# Agent Workbench:核心代理能力研究与产品计划
|
||||
|
||||
## 当前任务:1.20.3 模型能力与错误保留上线
|
||||
|
||||
用户授权将MODEL-FIDELITY-PLAN实现并本地上线,同时明确内置浏览器阶段。基线02cefd0;保留正式Provider/凭据、用户项目与全部历史失败,不调用真实模型或重放原任务。QA使用独立HTTP/SSE、数据和端口。
|
||||
|
||||
1. [complete] 核对错误、重试、原生参数和UI链路;1.10静态工程页面验收与未来通用内置浏览器分开,明确1.22浏览器阶段及其依赖。
|
||||
2. [complete] 有界/脱敏HTTP与SSE原错误工件、上游请求身份和实际尝试;429分类、完整Retry-After与本地来源。
|
||||
3. [complete] 逐协议原生参数配置/实际发送与不支持项提示,错误详情UI、旧记录兼容及专项验证;最终新增17项、相关103项和六组/十截图分别通过,原轮来源边界见QA。
|
||||
4. [in_progress] 构建、相关回归、桌面/390px核心流程;不可变发行、保留状态本地部署和正式回看。
|
||||
5. [pending] 证据封存、回顾、后续计划及按范围本地Git提交。
|
||||
|
||||
## 新增要求:模型能力和错误信息保留
|
||||
|
||||
1. [complete] 核对官方Harness概念及源码:HTTP错误截600字符且缺原code/type/request ID,流错误通用化;模型适配未覆盖全部原生参数。
|
||||
|
||||
@@ -0,0 +1,227 @@
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import fs from 'node:fs';
|
||||
import http from 'node:http';
|
||||
import { createHash } from 'node:crypto';
|
||||
import { Provider } from '../src/server/provider.mjs';
|
||||
import { Runtime } from '../src/server/runtime.mjs';
|
||||
import { Store } from '../src/server/store.mjs';
|
||||
import { fixture, compatibleFixture, completion, launchWorkbench, until } from './helpers.mjs';
|
||||
import { completionBody, actualRequest, outputReservation } from '../src/shared/contracts/model-request.mjs';
|
||||
import { nativeParameterSettings } from '../src/shared/contracts/native-parameters.mjs';
|
||||
import { retryAfterMilliseconds, ERROR_CAPTURE_LIMIT } from '../src/core/models/upstream-error.mjs';
|
||||
|
||||
const policy = { attempts: 3, delays: [1, 1], timeoutMs: 1000, totalTimeoutMs: 2000 };
|
||||
const config = { mode: 'compatible', baseUrl: 'http://127.0.0.1:1/v1', model: 'owned-fidelity', maxOutputTokens: 512 };
|
||||
const sha = value => createHash('sha256').update(value).digest('hex');
|
||||
const sse = value => 'data: ' + JSON.stringify(value) + '\n\n';
|
||||
function configure(f, provider, url, extra = {}) {
|
||||
const hub = provider.modelHub(f.store), connection = hub.upsert({ name: 'Owned fidelity', model: 'owned-fidelity', baseUrl: url,
|
||||
requiresKey: false, ...extra });
|
||||
hub.select({ type: 'connection', id: connection.id });
|
||||
return { hub, connection };
|
||||
}
|
||||
async function execute(f, provider) {
|
||||
const runtime = new Runtime(f.store, provider); f.onCleanup(() => runtime.stopAll());
|
||||
runtime.start(f.session.id, '读取当前项目;不要修改或执行检查');
|
||||
await runtime.active.get(f.session.id).promise;
|
||||
return f.session;
|
||||
}
|
||||
async function streamServer(t, bytes, headers = {}) {
|
||||
let requests = 0;
|
||||
const server = http.createServer(async (req, res) => { for await (const _ of req) {} requests++;
|
||||
res.writeHead(200, { 'Content-Type': 'text/event-stream', ...headers }); res.end(bytes); });
|
||||
await new Promise(resolve => server.listen(0, '127.0.0.1', resolve));
|
||||
t.after(() => new Promise(resolve => { server.closeAllConnections(); server.close(resolve); }));
|
||||
return { url: 'http://127.0.0.1:' + server.address().port + '/v1', count: () => requests };
|
||||
}
|
||||
|
||||
test('HTTP quota retains native code/type/param/request id, bounded original and allowlisted headers, never repeats that connection', async t => {
|
||||
const f = fixture(t), original = { error: { code: 'insufficient_quota', type: 'billing_error', param: 'account',
|
||||
message: 'Owned account quota. Echo owned-private-fidelity-key', api_key: 'unregistered-secret-value' }, request_id: 'body-request', usage: { prompt_tokens: 11, completion_tokens: 7 } };
|
||||
const model = await compatibleFixture(t, () => ({ status: 429, headers: { 'X-Request-ID': 'upstream-owned-quota', 'Retry-After': '120',
|
||||
'Set-Cookie': 'private-session=never-keep', 'X-Internal-Secret': 'never-keep' }, body: original }));
|
||||
const provider = new Provider({ retryPolicy: policy }), { hub, connection } = configure(f, provider, model.baseUrl, { apiKey: 'owned-private-fidelity-key' });
|
||||
await execute(f, provider);
|
||||
assert.equal(model.requests.length, 1); assert.equal(f.session.status, 'failed');
|
||||
const failure = f.session.failure, attempt = hub.view().attempts[0];
|
||||
assert.equal(failure.origin, 'upstream'); assert.equal(failure.kind, 'quota'); assert.equal(failure.code, 'insufficient_quota');
|
||||
assert.equal(failure.upstream.type, 'billing_error'); assert.equal(failure.upstream.param, 'account');
|
||||
assert.equal(failure.upstream.requestId, 'upstream-owned-quota'); assert.notEqual(failure.wireAttemptId, failure.upstream.requestId);
|
||||
assert.equal(failure.retryAfterMs, 120000); assert.equal(failure.retryable, false);
|
||||
assert.equal(attempt.diagnostic.upstream.artifactId, attempt.responseArtifactId); assert.equal(attempt.status, 'failed');
|
||||
const raw = f.store.artifactStore.read(f.store.owned('artifact', attempt.responseArtifactId, f.session.id));
|
||||
assert.ok(raw.includes('insufficient_quota')); assert.ok(raw.includes('[已隐藏]'));
|
||||
const publicData = JSON.stringify({ failure, attempt });
|
||||
for (const secret of ['owned-private-fidelity-key', 'unregistered-secret-value', 'never-keep']) assert.ok(!publicData.includes(secret) && !raw.includes(secret));
|
||||
assert.equal(hub.status(hub.connection(connection.id)).health.blocked, true);
|
||||
assert.equal(f.store.core.tool.size, 0); assert.equal(f.store.core.run.get(f.session.runId).usage.unknownRequests, 0);
|
||||
assert.equal(f.store.core.run.get(f.session.runId).usage.totalTokens, 18);
|
||||
f.store.close(); const reopened = new Store(f.data); try { assert.deepEqual(reopened.session(f.session.id).failure, failure); } finally { reopened.close(); }
|
||||
});
|
||||
|
||||
test('Retry-After above 90 seconds remains exact and a short local budget cannot trigger an early retry', async () => {
|
||||
let requests = 0;
|
||||
const provider = new Provider({ retryPolicy: policy, fetch: async () => { requests++; return new Response(JSON.stringify({ error: { code: 'rate_limit_exceeded', message: 'slow down' } }),
|
||||
{ status: 429, headers: { 'Retry-After': '180', 'X-Request-ID': 'owned-long-wait' } }); } });
|
||||
await assert.rejects(provider.complete(config, [], null), error => {
|
||||
assert.equal(error.diagnostic.retryAfterMs, 180000); assert.equal(error.diagnostic.retryDecision, 'wait_budget');
|
||||
assert.equal(error.diagnostic.limitReason, 'rate'); assert.equal(error.diagnostic.upstream.retryAfter, '180'); return true;
|
||||
});
|
||||
assert.equal(requests, 1);
|
||||
});
|
||||
|
||||
test('Retry-After date, expired date, huge value and invalid header retain explicit semantics', async () => {
|
||||
const now = Date.parse('2026-11-01T00:00:00Z');
|
||||
assert.equal(retryAfterMilliseconds('Sun, 01 Nov 2026 00:02:00 GMT', now), 120000);
|
||||
assert.equal(retryAfterMilliseconds('Sat, 31 Oct 2026 00:00:00 GMT', now), 0);
|
||||
for (const value of ['999999999999999999999999999999999', 'not-a-date']) {
|
||||
let requests = 0;
|
||||
const provider = new Provider({ retryPolicy: policy, fetch: async () => { requests++; return new Response('{"error":{"message":"limited"}}', { status: 429, headers: { 'Retry-After': value } }); } });
|
||||
await assert.rejects(provider.complete(config, [], null), error => { assert.equal(error.diagnostic.upstream.retryAfter, value);
|
||||
assert.equal(error.diagnostic.upstream.retryAfterInvalid, true); assert.equal(error.diagnostic.retryable, false); return true; });
|
||||
assert.equal(requests, 1);
|
||||
}
|
||||
});
|
||||
|
||||
test('unclassified 429 remains unspecified and every bounded actual retry retains its own original error', async t => {
|
||||
const f = fixture(t), provider = new Provider({ retryPolicy: { ...policy, attempts: 2 }, fetch: async () => new Response('{"error":{"message":"unspecified limit"}}',
|
||||
{ status: 429, headers: { 'retry-after': '0' } }) });
|
||||
f.store.saveConfig({ ...config }); await execute(f, provider);
|
||||
assert.equal(f.session.failure.limitReason, 'unspecified'); assert.equal(f.session.failure.attempts, 2);
|
||||
const artifacts = [...f.store.core.artifact.values()].filter(value => value.kind === 'model-http-response');
|
||||
assert.equal(artifacts.length, 2); assert.equal(f.store.core.run.get(f.session.runId).usage.requests, 2);
|
||||
});
|
||||
|
||||
test('oversized HTTP error saves exactly the bounded prefix and labels its incomplete range', async t => {
|
||||
const f = fixture(t), original = Buffer.from('owned-error\n' + 'x'.repeat(ERROR_CAPTURE_LIMIT * 3));
|
||||
const provider = new Provider({ retryPolicy: { ...policy, attempts: 1 }, fetch: async () => new Response(original, { status: 502, headers: { 'X-Request-ID': 'owned-large' } }) });
|
||||
const { hub } = configure(f, provider, config.baseUrl); await execute(f, provider);
|
||||
const attempt = hub.view().attempts[0], artifact = f.store.owned('artifact', attempt.responseArtifactId, f.session.id);
|
||||
assert.equal(artifact.bytes, ERROR_CAPTURE_LIMIT); assert.equal(artifact.sha256, sha(original.subarray(0, ERROR_CAPTURE_LIMIT)));
|
||||
assert.equal(attempt.upstream.capture.complete, false); assert.equal(attempt.upstream.capture.truncated, true);
|
||||
assert.deepEqual(attempt.upstream.capture.capturedRange, [0, ERROR_CAPTURE_LIMIT]);
|
||||
assert.equal(attempt.upstream.rawPreview.length, 8000); assert.equal(attempt.upstream.previewTruncated, true);
|
||||
});
|
||||
|
||||
test('Chat SSE error preserves upstream fields, original failed event, partial tool and reported usage without executing or retrying', async t => {
|
||||
const f = fixture(t), original = sse({ choices: [{ index: 0, delta: { tool_calls: [{ index: 0, id: 'partial', type: 'function', function: { name: 'propose_file', arguments: '{"path":' } }] } }] })
|
||||
+ sse({ error: { code: 'rate_limit_exceeded', type: 'rate_limit_error', message: 'Owned error owned-sse-secret' }, usage: { prompt_tokens: 11, completion_tokens: 3 } });
|
||||
const model = await streamServer(t, original, { 'x-request-id': 'owned-sse-id' });
|
||||
const provider = new Provider({ retryPolicy: policy }), { hub } = configure(f, provider, model.url, { apiKey: 'owned-sse-secret', streaming: true, capabilityDeclarations: { streaming: 'supported' } });
|
||||
await execute(f, provider);
|
||||
assert.equal(model.count(), 1); assert.equal(f.store.core.tool.size, 0); assert.equal(f.session.changes.length, 0);
|
||||
assert.equal(f.session.failure.kind, 'rate_limit'); assert.equal(f.session.failure.origin, 'upstream'); assert.equal(f.session.failure.retryDecision, 'stream_not_replayed');
|
||||
assert.equal(f.session.failure.model, 'owned-fidelity'); assert.equal(f.session.failure.endpoint, model.url); assert.equal(f.session.failure.route, '/chat/completions');
|
||||
assert.equal(f.session.failure.upstream.requestId, 'owned-sse-id'); assert.ok(!JSON.stringify(f.session.failure).includes('owned-sse-secret'));
|
||||
const attempt = hub.view().attempts[0]; assert.equal(attempt.status, 'failed'); assert.equal(attempt.stream.terminal, false);
|
||||
assert.equal(f.store.core.run.get(f.session.runId).usage.totalTokens, 14);
|
||||
assert.equal(f.session.failure.upstream.artifactId, attempt.responseArtifactId);
|
||||
assert.equal(f.session.failure.upstream.artifactScope, 'sse-stream');
|
||||
assert.ok(f.store.artifactStore.read(f.store.owned('artifact', attempt.responseArtifactId, f.session.id)).includes('"id":"partial"'));
|
||||
});
|
||||
|
||||
for (const status of ['failed', 'incomplete']) test('Responses ' + status + ' SSE retains original event/response identity and usage', async t => {
|
||||
const f = fixture(t), terminal = { id: 'resp-owned', status, usage: { input_tokens: 12, output_tokens: 5 },
|
||||
...(status === 'failed' ? { error: { code: 'insufficient_quota', type: 'billing_error', message: 'Owned exhausted' } } : { incomplete_details: { reason: 'max_output_tokens' } }) };
|
||||
const model = await streamServer(t, sse({ type: 'response.created', response: { id: 'resp-owned' }, sequence_number: 1 }) + sse({ type: 'response.' + status, response: terminal, sequence_number: 2 }));
|
||||
const provider = new Provider({ retryPolicy: policy }), { hub } = configure(f, provider, model.url, { protocol: 'responses', streaming: true, capabilityDeclarations: { streaming: 'supported' } });
|
||||
await execute(f, provider);
|
||||
assert.equal(model.count(), 1); assert.equal(f.session.failure.origin, 'upstream'); assert.equal(f.session.failure.upstream.responseId, 'resp-owned');
|
||||
assert.equal(f.session.failure.upstream.eventType, 'response.' + status); assert.equal(f.store.core.run.get(f.session.runId).usage.totalTokens, 17);
|
||||
assert.equal(hub.view().attempts[0].status, 'failed'); assert.equal(f.store.core.tool.size, 0);
|
||||
if (status === 'incomplete') assert.equal(f.session.failure.upstream.reason, 'max_output_tokens');
|
||||
});
|
||||
|
||||
test('unsupported successful Responses output retains original JSON and usage with a local protocol origin', async t => {
|
||||
const f = fixture(t), original = { id: 'resp-audio', object: 'response', status: 'completed', output: [{ type: 'audio', data: 'owned-data' }], usage: { input_tokens: 21, output_tokens: 3 } };
|
||||
const model = await compatibleFixture(t, () => ({ body: original })), provider = new Provider({ retryPolicy: policy }), { hub } = configure(f, provider, model.baseUrl, { protocol: 'responses' });
|
||||
await execute(f, provider);
|
||||
assert.equal(f.session.failure.origin, 'local'); assert.equal(f.session.failure.kind, 'protocol'); assert.equal(model.requests.length, 1);
|
||||
const attempt = hub.view().attempts[0]; assert.equal(attempt.status, 'failed');
|
||||
assert.equal(f.store.artifactStore.read(f.store.owned('artifact', attempt.responseArtifactId, f.session.id)), JSON.stringify(original));
|
||||
assert.equal(f.store.core.run.get(f.session.runId).usage.totalTokens, 24); assert.equal(f.store.core.tool.size, 0);
|
||||
});
|
||||
|
||||
test('HTTP 200 native error is still an upstream failure rather than a fabricated successful answer', async () => {
|
||||
const provider = new Provider({ retryPolicy: policy, fetch: async () => new Response('{"error":{"code":"insufficient_quota","message":"owned"}}') });
|
||||
await assert.rejects(provider.complete(config, [], null), error => error.diagnostic.origin === 'upstream' && error.diagnostic.kind === 'quota' && error.diagnostic.httpStatus === 200);
|
||||
});
|
||||
|
||||
test('JSON-escaped credential echoes are redacted in the original preview and capture as well as parsed fields', async () => {
|
||||
const key = 'owned-quote-"-and-backslash-\\-key', progress = [];
|
||||
const provider = new Provider({ retryPolicy: policy, fetch: async () => new Response(JSON.stringify({ error: { message: 'Echo ' + key, code: 'invalid_api_key' } }), { status: 401 }) });
|
||||
provider.setKey(key);
|
||||
await assert.rejects(provider.complete(config, [], null, undefined, value => progress.push(value)), error => {
|
||||
assert.ok(!JSON.stringify(error.diagnostic).includes(JSON.stringify(key).slice(1, -1)));
|
||||
assert.ok(error.diagnostic.upstream.rawPreview.includes('[已隐藏]')); return true;
|
||||
});
|
||||
assert.ok(progress.find(value => value.responseCapture).responseCapture.toString('utf8').includes('[已隐藏]'));
|
||||
});
|
||||
|
||||
test('quota failover uses another independent pool member and never replays a failed stream', async t => {
|
||||
const f = fixture(t), quota = await compatibleFixture(t, () => ({ status: 429, body: { error: { code: 'insufficient_quota', message: 'Owned empty' } } }));
|
||||
const ready = await compatibleFixture(t, () => completion([], 'Owned completed'));
|
||||
const provider = new Provider(), { hub, connection } = configure(f, provider, quota.baseUrl);
|
||||
const other = hub.upsert({ name: 'Owned other', model: 'owned-other', baseUrl: ready.baseUrl, requiresKey: false });
|
||||
const pool = hub.pool({ name: 'Owned failover', members: [{ connectionId: connection.id }, { connectionId: other.id }], maxAttempts: 3 }); hub.select({ type: 'pool', id: pool.id });
|
||||
await execute(f, provider);
|
||||
assert.equal(quota.requests.length, 1); assert.equal(ready.requests.length, 1); assert.equal(hub.view().attempts.length, 2);
|
||||
assert.deepEqual(hub.view().attempts.map(value => value.status), ['completed', 'failed']);
|
||||
assert.equal(f.store.core.run.get(f.session.runId).usage.requests, 2);
|
||||
});
|
||||
|
||||
test('native parameters are isolated by pool member, preserve Responses state and bind the original output reservation', () => {
|
||||
const a = { id: 'a', model: 'a-model', protocol: 'chat-completions', nativeParameters: { reasoning_effort: 'high', temperature: 0.4, seed: 3 }, outputTokenField: 'max_completion_tokens' };
|
||||
const b = { id: 'b', model: 'b-model', protocol: 'responses', nativeParameters: { reasoning: { effort: 'low' }, text: { verbosity: 'high' }, parallel_tool_calls: false } };
|
||||
const c = { id: 'c', model: 'c-model', protocol: 'chat-completions' };
|
||||
const body = completionBody({ ...config, ...a, modelRouting: { members: [a, b, c] } }, [{ role: 'user', content: 'owned' }], []);
|
||||
const chat = actualRequest(body, a), responses = actualRequest(body, b), plain = actualRequest(body, c);
|
||||
assert.equal(chat.max_completion_tokens, 512); assert.equal(chat.max_tokens, undefined); assert.equal(outputReservation(chat), 512);
|
||||
assert.equal(chat.reasoning_effort, 'high'); assert.equal(chat.seed, 3); assert.deepEqual(responses.reasoning, { effort: 'low' });
|
||||
assert.equal(responses.temperature, undefined); assert.equal(responses.seed, undefined); assert.deepEqual(responses.include, ['reasoning.encrypted_content']);
|
||||
assert.equal(responses.store, false); assert.equal(plain.reasoning_effort, undefined); assert.equal(plain.max_tokens, 512);
|
||||
});
|
||||
|
||||
test('unsupported native fields and invalid values reject before dispatch instead of silently dropping or overriding constraints', () => {
|
||||
for (const [protocol, params, field] of [['chat-completions', { model: 'override' }], ['responses', { tools: [] }], ['responses', { reasoning: { effort: 'high', extra: 1 } }],
|
||||
['responses', { text: { format: { type: 'json' } } }], ['chat-completions', { temperature: '1' }], ['chat-completions', { top_p: 2 }], ['chat-completions', { seed: 1.5 }],
|
||||
['chat-completions', { parallel_tool_calls: 'true' }], ['chat-completions', { reasoning_effort: 'invented' }], ['chat-completions', {}, 'max_output_tokens'],
|
||||
['responses', {}, 'max_tokens'], ['responses', []], ['responses', { unknown: 'x'.repeat(8200) }]]) {
|
||||
assert.throws(() => nativeParameterSettings(protocol, params, field), error => error.status === 400);
|
||||
}
|
||||
});
|
||||
|
||||
test('native settings for legacy and registered profiles persist without changing credentials or old records and reject stale edits', async t => {
|
||||
const f = fixture(t), provider = new Provider(); provider.setKey('owned-legacy-key');
|
||||
const { hub, connection } = configure(f, provider, config.baseUrl, { apiKey: 'owned-profile-key' });
|
||||
const oldMessages = JSON.stringify(f.session), credential = provider.credentials.get(connection.id), oldPool = JSON.stringify(hub.state.pools);
|
||||
hub.setNativeParameters({ id: connection.id, revision: connection.revision, nativeParameters: { reasoning_effort: 'medium' }, outputTokenField: 'max_completion_tokens' });
|
||||
assert.equal(provider.credentials.get(connection.id), credential); assert.equal(JSON.stringify(f.session), oldMessages); assert.equal(JSON.stringify(hub.state.pools), oldPool);
|
||||
assert.throws(() => hub.setNativeParameters({ id: connection.id, revision: connection.revision, nativeParameters: {} }), error => error.status === 409);
|
||||
const legacy = hub.connection('legacy-default'); hub.setNativeParameters({ id: legacy.id, revision: legacy.revision, nativeParameters: { temperature: 0.5 } });
|
||||
assert.equal(provider.apiKey, 'owned-legacy-key'); assert.deepEqual(f.store.config.nativeParameters, { temperature: 0.5 });
|
||||
f.store.close(); const reopened = new Store(f.data); try { assert.deepEqual(reopened.config.nativeParameters, { temperature: 0.5 });
|
||||
assert.deepEqual(new Provider().modelHub(reopened).connection(connection.id).nativeParameters, { reasoning_effort: 'medium' }); } finally { reopened.close(); }
|
||||
});
|
||||
|
||||
test('compiled native settings API saves/restores exact parameters and rejects reserved fields without mutating the profile', async t => {
|
||||
const f = fixture(t); f.store.close(); const host = await launchWorkbench(t, f.data);
|
||||
const member = await host.api('/model-center/connections', { name: 'Owned API', model: 'owned-api', protocol: 'responses', baseUrl: config.baseUrl, requiresKey: false });
|
||||
await host.api('/model-center/native-parameters', { id: member.id, revision: member.revision, nativeParameters: { reasoning: { effort: 'high' }, parallel_tool_calls: false } });
|
||||
const saved = (await host.api('/model-center')).connections.find(value => value.id === member.id);
|
||||
assert.deepEqual(saved.nativeParameters, { reasoning: { effort: 'high' }, parallel_tool_calls: false });
|
||||
await assert.rejects(host.api('/model-center/native-parameters', { id: member.id, revision: saved.revision, nativeParameters: { max_output_tokens: 99999 } }), error => error.status === 400);
|
||||
assert.deepEqual((await host.api('/model-center')).connections.find(value => value.id === member.id), saved);
|
||||
});
|
||||
|
||||
test('legacy connection test sends its saved native fields and selected output limit field without changing the configuration', async t => {
|
||||
const f = fixture(t), model = await compatibleFixture(t, () => completion([], 'OK')); f.store.close();
|
||||
const host = await launchWorkbench(t, f.data), original = (await host.api('/model-center')).connections.find(value => value.id === 'legacy-default');
|
||||
await host.api('/model-center/native-parameters', { id: original.id, revision: original.revision, nativeParameters: { reasoning_effort: 'low' }, outputTokenField: 'max_completion_tokens' });
|
||||
const before = await host.api('/model-center');
|
||||
await host.api('/config/test', { baseUrl: model.baseUrl, model: 'owned-probe' });
|
||||
assert.equal(model.requests.length, 1); assert.equal(model.requests[0].input.reasoning_effort, 'low');
|
||||
assert.equal(model.requests[0].input.max_completion_tokens, 64); assert.equal(model.requests[0].input.max_tokens, undefined);
|
||||
assert.deepEqual(await host.api('/model-center'), before);
|
||||
});
|
||||
Reference in New Issue
Block a user