feat(models): discover upstream catalogs and select shared-source model pools
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
# 上游模型目录与多模型使用
|
||||
|
||||
用户补充:模型应正常从上游获取,不要求手填;可选择多个型号,使用时手动指定或自动适配。本切片并入1.25.3,沿用模型中心、ModelHub和现有模型池,不另建路由体系。正式交付状态以独立发行记录为准。
|
||||
|
||||
## 实现
|
||||
|
||||
模型中心主要入口为“从上游添加模型”:新上游填写名称、品牌、协议、根地址与凭据即可获取,无需先填型号。也可从已有连接“获取同源模型”,使用原凭据。目录支持搜索、多选与筛选批量选中;保存时可保留当前选择、手动指定默认型号,或建立自动适配池。手动填写保留为未提供目录的兼容入口。
|
||||
|
||||
Chat/Responses和DeepSeek兼容服务使用根地址下的GET /models;Messages按根地址或/v1地址补齐/v1/models,并保留x-api-key/Bearer选择和anthropic-version。has_more/last_id分页有60秒、20页、1000项、4MB界限,达到范围明确显示部分目录;本机100连接、单池32成员的原上限不静默截断。错误保留原上游原因与请求ID,单次目录GET不自动重试,不生成模型推理尝试。取消覆盖请求先后乱序与迟到响应;关闭宿主清除临时预览。
|
||||
|
||||
同上游的多个型号通过credentialSource引用源连接,原内存Key对象和DPAPI块保持;只为新填写且用户明确选择保存的Key创建一个块。不同账号即使端点/型号相同仍独立。源停用或地址/鉴权不符则引用不可用;原Key替换、源代次和连接修订参与旧Run拒绝。子型号显式填写独立Key会脱离引用并增加凭据修订,避免旧opaque历史误回传。新增模型、池、选线和模式在同一SQLite事务中保存,失败整批回退且预览仍可修正。重启只恢复原保存块;临时Key仍不持久化。
|
||||
|
||||
目录同步上游明确提供的型号、显示名称、提供方、容量、生命周期、文本/工具/流式/图像/语音声明、推理模式/强度、支持参数及PDF/服务端工具等功能声明。元数据缺失保持未知,允许用户明确补充未知文本/工具项;负面声明不会被补充覆盖。推理界面与服务端按已提供的档位/模式限制选择。实际配置容量仍取原本机预算与上游上限的较小值,不自动扩预算。目录声明不变成已验证,本地未接入功能仍标未接入。重新选择已有模型保留其本地参数、能力覆盖和命名,结果明确为沿用已有模型;不暗中覆盖用户编辑。
|
||||
|
||||
自动适配按照本次任务能力、健康、并发和原池策略筛选,运行内优先保持型号;这不是已验证的语义任务路由。手动与自动都保留每次实际请求身份、用量和原失败,旧Run保存原目录/连接/凭据/池快照。
|
||||
|
||||
## 验证与回顾
|
||||
|
||||
首批11/11目录核心验证与构建通过;补足编译HTTP、容量、不同账号、停用中的子型号检测和宿主停止后,目录/模型中心/能力路由/推理/凭据/发行与升级相关68/68通过,热升级保留目录对象与方法回退另补4/4通过,完整构建通过。网页/实际Electron最终各8组、各13目录GET、0推理通过,1440/390px原图实际回看。新增目录没有减掉原推理验收,推理网页/桌面各6组再次通过;原模型中心、协议、Messages入口随后复验。独立载荷/安装/正式升级另写发行报告。
|
||||
|
||||
目录品牌补强首轮有一项验收器断言失败:DOM已包含disabled的option,Playwright的toBeDisabled仍返回enabled。原截图和失败保留,改为核对真实HTMLSelectElement选项的disabled布尔属性,原期望/期限不变,最终网页/桌面通过。阶段复核另发现同地址/型号的新账号可能误中旧源匹配,已将去重严格限制到明确源连接,增加真实HTTP两账号隔离;不是将端点或型号当凭据身份。
|
||||
|
||||
证据位于`.qa/reasoning-controls/`的新文件。原109e5124范围的完整源码测试因本次需求扩大,在264个完成项、未观察到失败时主动停止所属进程;未完整通过,不作为新范围验收。原日志与取消回执位于`.qa/reasoning-1.25.3/full-source*`。新范围将重新冻结,旧freeze不覆盖。
|
||||
|
||||
公开契约参考:OpenAI官方GET https://api.openai.com/v1/models的OpenAPI说明;Anthropic官方SDK `https://raw.githubusercontent.com/anthropics/anthropic-sdk-typescript/main/src/resources/models.ts`(原文件已保存于`.qa/reasoning-controls/public-docs/claude-sdk-models.ts`),包括capabilities、effort、thinking.types与分页。兼容代理实际是否提供这些元数据尚未用真实账号核验,本阶段真实模型请求0。
|
||||
|
||||
本地核心/HTTP夹具与真实供应商交付分别判断。Z3空响应、DeepSeek length、全部旧网络异常/重复读取、自主工程交付与Purge AB/BA未验证保持,不续跑旧实验、不推送、不委派、不操作真实Key。
|
||||
@@ -12,6 +12,12 @@
|
||||
|
||||
## 阶段记录
|
||||
|
||||
- 用户新增模型目录要求:1.25.3扩大为上游目录→搜索多选→保存同源型号→手动/自动使用;旧109e5124冻结及其完整源码轮次保持原范围历史证据,不据此打包或升级。新范围完成后重新冻结。
|
||||
- 原范围源码轮次在264个完成项、未观察到失败时因需求扩大停止所属PID56056及其子测试树;原日志/取消回执保持,不报整轮通过。目录实现与阶段回顾见MODEL-CATALOG.md。
|
||||
- 目录设计:复用现有ModelHub与池,保留手动兼容入口;新型号引用单一源连接凭据,源停用、端点/鉴权变化、换Key与重启恢复必须隔离,不能复制或重新保存已有Key。目录从品牌所用协议获取,Messages分页;60秒/20页/1000项/4MB有界、不跟随重定向、不自动重试。目录仅为上游声明,无元数据时保留未知,用户显式补充;不把型号名当已验证能力。
|
||||
- 交互选择:比较另建供应商中心(重复配置体系)、在旧手填表单插入目录(仍强迫先填型号)、现有模型中心独立目录编辑区;采用第三种。新上游不需型号,已有连接可一键获取同源目录,搜索/批量选择/结果/错误/取消都在同区;手动与自动复用现有默认选线和模型池。
|
||||
- 新验收:OpenAI/Responses/DeepSeek兼容GET与Messages两种鉴权/分页;空目录、重复游标、冲突型号、超限、错误/取消/迟到响应;原凭据引用对象/DPAPI块不重存、源变化及重启;未知能力/参数元数据/手动编辑冲突;原Run冻结及手动/自动实际HTTP;网页/实际Electron及390px,然后独立发行、保留状态升级。
|
||||
|
||||
- 起步:读取AGENTS与两份采用计划,确认现有表单只有JSON;Chat尚不接受thinking,Messages已有thinking历史但没有output_config.effort;Chat流式/历史路径待核对。一次猜测不存在的chat-stream.mjs读取失败,无文件变化,后续用rg定位。
|
||||
- 阶段一通过:Messages JSON/SSE真实本机HTTP复现空响应,原6692输入/0输出完整保留,单次protocol错误、0工具/重发。空数组、空白与仅opaque thinking拒绝,合法文字/工具保留;Messages相关28/28通过,日志.qa/reasoning-controls/messages-tests.txt。修复尚未部署。
|
||||
- 官方来源:DeepSeek既有Thinking Mode原HTML保持;OpenAI Docs查阅Chat与reasoning-effort;Claude文档重定向地区不可用页,已保存原失败,随后官方SDK原始类型取得HTTP200,确认output_config.effort支持low/medium/high/xhigh/max。一次node_repl网络等待超时、几次不存在的可选路径读取错误均无产品影响,后续改curl有界及已有实际文件定位。
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
# 汇总发现
|
||||
|
||||
上游模型目录:原Provider.models仅字符串/100条,模型中心手填;新目录入口复用原Hub/池,Chat/Responses/DeepSeek GET及Messages根/v1分页/鉴权。新型号通过源连接凭据引用,原对象与DPAPI块不重存;不同账号即使同地址型号也不去重。目录元数据为声明、未知不伪造、推理档位限制可选项,未接入执行功能仍显明。自动适配是能力/健康/并发加池策略;原Run冻结目录/凭据源代次。68相关+4升级、网页/原生各8组通过,真实推理0。细节docs/MODEL-CATALOG.md;正式和独立发行仍待完成。
|
||||
|
||||
推理设置补充:用户要求可开启并选择强度,不能仅支持thinking.disabled。DeepSeek官方Chat思考开关为thinking.type,强度low/high/max;携带tools的后续请求必须完整回传所有已有reasoning_content。OpenAI官方Chat/Responses强度受型号限制(none/minimal/low/medium/high/xhigh/max),不能在UI宣称所有模型都支持关闭。新增工作见docs/REASONING-CONTROLS-PLAN.md,真实请求暂0。
|
||||
|
||||
Z:Claude请求5.5/返回5-5的型号手误,用户已确认并保存新连接/凭据修订2,首样本1请求176tokens保留。DeepSeek默认high推理;预检146tokens通过,Node5请求47740tokens末轮finish_reason=length,2048输出全部reasoning_tokens,完整SSE也不能当完整答复;identity incomplete、0提案/修改/交付,原九基线失败/build通过。两轮151份82988849字节证据离线通过,原状态/预算/选线/凭据恢复,不续跑旧Task。官方Thinking Mode原HTML117778字节和SHA保存,当前Chat白名单不支持thinking是明确兼容缺口,不能假装已发送该参数或归因网络。
|
||||
|
||||
@@ -1,5 +1,10 @@
|
||||
# 统一执行进度
|
||||
|
||||
## 上游模型目录与多模型使用
|
||||
|
||||
- 用户补充无需手填型号、从上游多选并手动/自动使用;已接到原模型中心/池,来源与边界见docs/MODEL-CATALOG.md。相关68/68、目录热升级补4/4及完整构建通过;网页/真实Electron各8组、13目录GET、0推理,1440/390原图回看,原推理两端各6组再次通过。
|
||||
- 旧109e冻结完整源码轮次因新增范围在264完成项、未观察到失败时主动停止所属树,日志/取消回执保持;不报通过,最终另冻。目录品牌首次UI的option matcher失败及原图保持,按真实disabled属性复验通过。正式仍1.25.2/PID36552,未操作真实Key或续跑旧样本。
|
||||
|
||||
## 推理控制与空响应修复
|
||||
|
||||
- 配置档/可视推理/导入和Run冻结/Chat原生状态回传实现;85/85相关+最终8/8专项,构建通过。网页/实际Electron各6组及旧参数6组/Messages7组通过,1440/390原图回看。首构建声明缺失、重开DB夹具清理超时和所属进程终止保留,修清理顺序后通过。正式仍1.25.2,待独立发行。见docs/QA-REASONING-CONTROLS.md。
|
||||
|
||||
@@ -58,7 +58,7 @@ let result;
|
||||
try {
|
||||
await page.addInitScript(({ projectId, sessionId }) => { localStorage.setItem('awb-project', projectId); localStorage.setItem('awb-session', sessionId); }, { projectId: f.project.id, sessionId: f.session.id });
|
||||
await page.goto(host.url); await page.waitForLoadState('networkidle'); await open();
|
||||
await dialog().getByRole('button', { name: '添加连接', exact: true }).click(); const form = dialog().getByRole('form', { name: '编辑模型连接', exact: true });
|
||||
await dialog().getByRole('button', { name: '手动填写(兼容)', exact: true }).click(); const form = dialog().getByRole('form', { name: '编辑模型连接', exact: true });
|
||||
await form.getByLabel('连接名称', { exact: true }).fill('本机 Messages 验收'); await form.getByLabel('接口协议', { exact: true }).selectOption('anthropic-messages');
|
||||
await form.getByLabel('接口根地址', { exact: true }).fill(baseUrl); await form.getByLabel('请求模型名称', { exact: true }).fill('claude-owned-ui');
|
||||
await form.getByLabel('临时 API Key', { exact: true }).fill('owned-ui-key'); await form.getByLabel('Messages 鉴权', { exact: true }).selectOption('bearer');
|
||||
|
||||
@@ -105,7 +105,7 @@ try:
|
||||
dialog = page.get_by_role('dialog', name='模型中心', exact=True)
|
||||
expect(dialog).to_be_visible()
|
||||
stage = 'real connection form and explicit encrypted save'
|
||||
dialog.get_by_role('button', name='添加连接', exact=True).click()
|
||||
dialog.get_by_role('button', name='手动填写(兼容)', exact=True).click()
|
||||
editor = dialog.get_by_role('form', name='编辑模型连接', exact=True)
|
||||
editor.get_by_label('连接名称', exact=True).fill('Owned Saved Primary')
|
||||
editor.get_by_label('接口根地址', exact=True).fill(endpoint)
|
||||
|
||||
@@ -0,0 +1,151 @@
|
||||
import fs from 'node:fs';
|
||||
import path from 'node:path';
|
||||
import net from 'node:net';
|
||||
import http from 'node:http';
|
||||
import assert from 'node:assert/strict';
|
||||
import { randomUUID, createHash } from 'node:crypto';
|
||||
import { chromium, _electron } from 'playwright';
|
||||
import { expect } from 'playwright/test';
|
||||
import { fixture, launchWorkbench, until } from '../tests/helpers.mjs';
|
||||
import { HostController } from '../src/core/desktop/host-controller.mjs';
|
||||
import { desktopExecutableIdentity } from '../src/core/desktop/app-installation.mjs';
|
||||
import { projectEnvironment } from '../src/core/process-environment.mjs';
|
||||
|
||||
const desktop = process.argv.includes('--desktop'), appRoot = path.resolve(process.env.AGENT_WORKBENCH_QA_APP_ROOT || '.');
|
||||
assert.ok(process.argv.slice(2).every(value => value === '--desktop'));
|
||||
const directory = path.resolve('.qa/reasoning-controls', (desktop ? 'catalog-electron-' : 'catalog-web-') + randomUUID()); fs.mkdirSync(directory, { recursive: true });
|
||||
console.log('EVIDENCE ' + directory);
|
||||
const disposers = [], t = { after: fn => disposers.push(fn) }, f = fixture(t); f.store.close();
|
||||
const requests = [], checks = [], screenshots = [], errors = []; let scenario = 'normal', release;
|
||||
const models = Array.from({ length: 132 }, (_, i) => ({ id: 'owned-model-' + String(i).padStart(3, '0'), owned_by: '自有目录夹具',
|
||||
context_length: 64000, max_output_tokens: 8192, capabilities: { text: true, tools: i !== 1, streaming: true, image_input: { supported: i === 2 },
|
||||
effort: { supported: true, low: { supported: true }, high: { supported: true } }, thinking: { supported: true, types: { adaptive: { supported: true }, disabled: { supported: true }, enabled: { supported: false } } } } }));
|
||||
const server = http.createServer(async (req, res) => {
|
||||
const parts = []; for await (const part of req) parts.push(part);
|
||||
const body = parts.length ? JSON.parse(Buffer.concat(parts)) : null;
|
||||
requests.push({ method: req.method, route: req.url, body, authorizationPresent: !!req.headers.authorization, apiKeyHeaderPresent: !!req.headers['x-api-key'], anthropicVersion: req.headers['anthropic-version'] });
|
||||
if (scenario === 'wait') await new Promise(resolve => { release = resolve; });
|
||||
if (scenario === 'error') { res.writeHead(503, { 'Content-Type': 'application/json', 'x-request-id': 'owned-catalog-503' }); res.end(JSON.stringify({ error: { message: '自有目录暂不可用', code: 'owned_unavailable' } })); return; }
|
||||
const input = new URL(req.url, 'http://owned.test');
|
||||
const value = scenario === 'empty' ? { data: [] } : input.searchParams.has('after_id') ? { data: models.slice(100), has_more: false }
|
||||
: { data: models.slice(0, 100), has_more: true, last_id: models[99].id };
|
||||
res.writeHead(200, { 'Content-Type': 'application/json' }); res.end(JSON.stringify(value));
|
||||
});
|
||||
await new Promise(resolve => server.listen(0, '127.0.0.1', resolve));
|
||||
let browser, application, controller, host, page, window, result;
|
||||
const mark = name => { checks.push(name); console.log('PASS ' + name); };
|
||||
const dialog = () => page.getByRole('dialog', { name: '模型中心', exact: true });
|
||||
const catalog = () => dialog().getByRole('region', { name: '上游模型目录', exact: true });
|
||||
async function resize(width, height) {
|
||||
if (window) { await window.evaluate((win, size) => win.setContentSize(size.width, size.height), { width, height }); await until(() => page.evaluate(w => innerWidth === w, width)); }
|
||||
else await page.setViewportSize({ width, height });
|
||||
}
|
||||
async function shot(name) {
|
||||
const file = path.join(directory, name + '.png'); await page.screenshot({ path: file, scale: 'css' });
|
||||
screenshots.push({ path: file, sha256: createHash('sha256').update(fs.readFileSync(file)).digest('hex') });
|
||||
fs.writeFileSync(path.join(directory, name + '.txt'), await page.locator('body').innerText());
|
||||
}
|
||||
async function open() { if (!await dialog().count()) await page.getByRole('button', { name: '打开模型中心', exact: true }).click(); }
|
||||
async function add() { await open(); await dialog().getByRole('button', { name: '从上游添加模型', exact: true }).click(); await catalog().waitFor(); }
|
||||
async function fetchModels() { await catalog().getByRole('button', { name: /获取上游模型|重新获取模型目录/ }).click(); await expect(catalog().getByLabel('上游模型列表', { exact: true })).toBeVisible(); }
|
||||
try {
|
||||
if (desktop) {
|
||||
const socket = net.createServer(); await new Promise(resolve => socket.listen(0, '127.0.0.1', resolve)); const port = socket.address().port; await new Promise(resolve => socket.close(resolve));
|
||||
assert.notEqual(port, 4318); controller = new HostController({ appRoot, dataRoot: f.data, port, nodePath: process.execPath, timeoutMs: 60000 }); await controller.start();
|
||||
host = { api: (route, input) => controller.api(route, input ? { method: 'POST', input } : {}), bootstrap: await controller.api('/bootstrap') };
|
||||
const executable = desktopExecutableIdentity(appRoot).path;
|
||||
application = await _electron.launch({ executablePath: executable, args: [...(executable.toLowerCase().endsWith('agentworkbench.exe') ? [] : [path.join(appRoot, 'dist-desktop/main.cjs')]),
|
||||
'--app-root', appRoot, '--data-root', f.data, '--node', process.execPath, '--port', String(port)], env: projectEnvironment({ AGENT_WORKBENCH_QA: '1', AGENT_WORKBENCH_NODE: process.execPath }) });
|
||||
page = await application.firstWindow(); window = await application.browserWindow(page); await page.waitForLoadState(); await resize(1440, 1080);
|
||||
} else {
|
||||
host = await launchWorkbench(t, f.data, {}, { appRoot }); browser = await chromium.launch({ channel: 'msedge', headless: true });
|
||||
page = await browser.newPage({ viewport: { width: 1440, height: 1080 } }); await page.goto(host.url);
|
||||
}
|
||||
page.on('pageerror', error => errors.push(error.message)); await page.locator('.connection.connected').waitFor();
|
||||
await add(); await expect(catalog().getByRole('textbox', { name: '请求模型名称', exact: true })).toHaveCount(0);
|
||||
await catalog().getByLabel('上游名称', { exact: true }).fill('自有多模型上游');
|
||||
await catalog().getByLabel('目录接口根地址', { exact: true }).fill('http://127.0.0.1:' + server.address().port + '/v1');
|
||||
await catalog().getByLabel('目录 API Key', { exact: true }).fill('owned-catalog-ui-key'); await fetchModels();
|
||||
assert.equal(requests.length, 2); assert.ok(requests.every(value => value.method === 'GET' && value.body === null));
|
||||
await expect(catalog().getByLabel('上游模型列表', { exact: true }).getByRole('checkbox')).toHaveCount(132);
|
||||
await catalog().getByLabel('搜索上游模型', { exact: true }).fill('owned-model-00');
|
||||
await catalog().getByLabel('选择模型 owned-model-000', { exact: true }).check(); await catalog().getByLabel('选择模型 owned-model-001', { exact: true }).check(); await catalog().getByLabel('选择模型 owned-model-002', { exact: true }).check();
|
||||
await catalog().getByLabel('模型使用方式', { exact: true }).selectOption('auto'); await shot('catalog-multiselect-1440');
|
||||
await catalog().getByRole('button', { name: '保存所选 3 个模型', exact: true }).click(); await expect(catalog().getByRole('status')).toContainText('已新增 3 个模型');
|
||||
let center = await host.api('/model-center'); const members = center.connections.filter(value => value.upstreamModel);
|
||||
assert.equal(members.length, 3); assert.equal(members.filter(value => value.credentialSource).length, 2); assert.equal(center.selected.type, 'pool');
|
||||
assert.equal(center.pools.find(value => value.id === center.selected.id).members.length, 3); assert.equal(requests.length, 2);
|
||||
mark('new upstream needs no manual model; 132 paginated IDs are searchable and selectable; saving creates shared-source members and the existing automatic pool without inference');
|
||||
await page.reload(); await open(); await expect(dialog().getByLabel('新任务默认模型', { exact: true })).toHaveValue('pool:' + center.selected.id);
|
||||
const child = members.find(value => value.model === 'owned-model-002'); await dialog().getByLabel('新任务默认模型', { exact: true }).selectOption('connection:' + child.id);
|
||||
assert.equal((await host.api('/model-center')).selected.id, child.id);
|
||||
const card = () => dialog().locator('.mc-connection').filter({ has: page.getByRole('heading', { name: child.name, exact: true }) });
|
||||
await expect(card()).toContainText('共用'); await card().getByRole('button', { name: '推理与能力', exact: true }).click();
|
||||
const params = () => dialog().getByRole('form', { name: '原生请求参数', exact: true });
|
||||
await expect(params().getByLabel('推理强度', { exact: true }).locator('option')).toHaveCount(3);
|
||||
await params().getByRole('button', { name: '取消', exact: true }).click();
|
||||
mark('reload retains the automatic pool; users can manually choose a member, see its credential source and receive only upstream-declared reasoning strengths');
|
||||
await card().getByRole('button', { name: '获取同源模型', exact: true }).click(); await expect(catalog().getByLabel('目录 API Key', { exact: true })).toHaveCount(0); await fetchModels();
|
||||
await catalog().getByLabel('搜索上游模型', { exact: true }).fill('owned-model-003'); await catalog().getByLabel('选择模型 owned-model-003', { exact: true }).check();
|
||||
await catalog().getByLabel('模型使用方式', { exact: true }).selectOption('manual'); await catalog().getByLabel('目录默认模型', { exact: true }).selectOption('owned-model-003');
|
||||
await catalog().getByRole('button', { name: '保存所选 1 个模型', exact: true }).click(); await expect(catalog().getByRole('status')).toContainText('已新增 1 个模型');
|
||||
center = await host.api('/model-center'); assert.equal(center.connections.find(value => value.id === center.selected.id).model, 'owned-model-003');
|
||||
assert.equal(center.connections.filter(value => value.upstreamModel).length, 4); assert.equal(requests.length, 4);
|
||||
mark('an existing member opens its original upstream without exposing or re-entering a key and adds another model with an explicit manual default');
|
||||
await catalog().getByRole('button', { name: '关闭目录', exact: true }).click(); await card().getByRole('button', { name: '获取同源模型', exact: true }).click();
|
||||
scenario = 'error'; await catalog().getByRole('button', { name: '获取上游模型', exact: true }).click(); await expect(catalog().getByRole('alert')).toContainText('自有目录暂不可用');
|
||||
await catalog().getByText('查看上游原文与请求信息', { exact: true }).click(); await expect(catalog()).toContainText('owned-catalog-503'); await shot('catalog-upstream-error');
|
||||
assert.equal(requests.length, 5); scenario = 'empty'; await fetchModels(); await expect(catalog()).toContainText('上游返回了空目录');
|
||||
assert.equal((await host.api('/model-center')).connections.length, 5);
|
||||
scenario = 'wait'; await catalog().getByRole('button', { name: '重新获取模型目录', exact: true }).click(); await until(() => !!release);
|
||||
await catalog().getByRole('button', { name: '取消获取', exact: true }).click(); release(); await expect(catalog().getByRole('status')).toContainText('已取消获取');
|
||||
await expect(catalog().getByLabel('上游模型列表', { exact: true })).toHaveCount(0); scenario = 'normal'; await fetchModels();
|
||||
await expect(catalog().getByLabel('上游模型列表', { exact: true }).getByRole('checkbox')).toHaveCount(132);
|
||||
mark('upstream error retains request ID and original reason with one GET; empty directory and cancellation preserve members, and a later explicit refresh recovers');
|
||||
await resize(390, 844); await catalog().getByLabel('搜索上游模型', { exact: true }).fill('owned-model-13');
|
||||
await catalog().getByRole('button', { name: '选中当前筛选', exact: true }).click(); await catalog().getByLabel('模型使用方式', { exact: true }).selectOption('keep');
|
||||
await catalog().getByLabel('上游模型列表', { exact: true }).scrollIntoViewIfNeeded(); await shot('catalog-list-390');
|
||||
assert.equal(await page.evaluate(() => document.documentElement.scrollWidth <= innerWidth + 1), true);
|
||||
const save = catalog().getByRole('button', { name: '保存所选 2 个模型', exact: true }); await save.scrollIntoViewIfNeeded(); const bounds = await save.boundingBox(); assert.ok(bounds && bounds.x >= 0 && bounds.x + bounds.width <= 391);
|
||||
await save.click(); await expect(catalog().getByRole('status')).toContainText('已新增 2 个模型'); await shot('catalog-saved-390');
|
||||
assert.equal((await host.api('/model-center')).connections.filter(value => value.upstreamModel).length, 6);
|
||||
mark('390px search, filtered bulk selection, source feedback and save action fit and complete the same real API flow');
|
||||
await resize(1440, 1080); await catalog().getByRole('button', { name: '关闭目录', exact: true }).click(); await add();
|
||||
await catalog().getByLabel('目录服务品牌', { exact: true }).selectOption('Anthropic');
|
||||
await expect(catalog().getByLabel('目录接口协议', { exact: true })).toHaveValue('anthropic-messages');
|
||||
await expect(catalog().getByLabel('目录接口根地址', { exact: true })).toHaveValue('https://api.anthropic.com');
|
||||
await catalog().getByLabel('上游名称', { exact: true }).fill('自有 Claude 上游');
|
||||
await catalog().getByLabel('目录接口根地址', { exact: true }).fill('http://127.0.0.1:' + server.address().port);
|
||||
await catalog().getByLabel('目录 API Key', { exact: true }).fill('owned-messages-catalog'); await fetchModels();
|
||||
assert.equal(requests.at(-1).route, '/v1/models?after_id=owned-model-099&limit=100'); assert.equal(requests.at(-1).apiKeyHeaderPresent, true); assert.equal(requests.at(-1).authorizationPresent, false);
|
||||
await catalog().getByLabel('搜索上游模型', { exact: true }).fill('owned-model-003'); await catalog().getByLabel('选择模型 owned-model-003', { exact: true }).check();
|
||||
await catalog().getByRole('button', { name: '保存所选 1 个模型', exact: true }).click(); await expect(catalog().getByRole('status')).toContainText('已新增 1 个模型');
|
||||
const claude = (await host.api('/model-center')).connections.find(value => value.name === '自有 Claude 上游 · owned-model-003');
|
||||
await catalog().getByRole('button', { name: '关闭目录', exact: true }).click(); await dialog().locator('.mc-connection').filter({ has: page.getByRole('heading', { name: claude.name, exact: true }) }).getByRole('button', { name: '推理与能力', exact: true }).click();
|
||||
await expect(params().getByLabel('推理模式', { exact: true }).locator('option[value="enabled"]')).toHaveJSProperty('disabled', true);
|
||||
await expect(params().getByLabel('推理模式', { exact: true }).locator('option[value="adaptive"]')).toHaveJSProperty('disabled', false);
|
||||
await params().getByRole('button', { name: '取消', exact: true }).click();
|
||||
mark('Anthropic brand pairs its endpoint, protocol and x-api-key pagination; upstream thinking modes restrict the actual controls');
|
||||
await add(); await catalog().getByLabel('目录服务品牌', { exact: true }).selectOption('DeepSeek');
|
||||
await expect(catalog().getByLabel('目录接口协议', { exact: true })).toHaveValue('chat-completions');
|
||||
await expect(catalog().getByLabel('目录接口根地址', { exact: true })).toHaveValue('https://api.deepseek.com');
|
||||
await catalog().getByLabel('上游名称', { exact: true }).fill('自有 DeepSeek 上游');
|
||||
await catalog().getByLabel('目录接口根地址', { exact: true }).fill('http://127.0.0.1:' + server.address().port + '/v1');
|
||||
await catalog().getByLabel('目录 API Key', { exact: true }).fill('owned-deepseek-catalog'); await fetchModels();
|
||||
assert.equal(requests.at(-1).authorizationPresent, true); assert.equal(requests.at(-1).apiKeyHeaderPresent, false);
|
||||
await catalog().getByLabel('搜索上游模型', { exact: true }).fill('owned-model-005'); await catalog().getByLabel('选择模型 owned-model-005', { exact: true }).check();
|
||||
await catalog().getByRole('button', { name: '保存所选 1 个模型', exact: true }).click(); await expect(catalog().getByRole('status')).toContainText('已新增 1 个模型');
|
||||
mark('DeepSeek brand pairs Chat and bearer discovery with a separate explicitly entered source key and no inference');
|
||||
await catalog().getByRole('button', { name: '关闭目录', exact: true }).click(); await dialog().getByRole('button', { name: '手动填写(兼容)', exact: true }).click();
|
||||
await expect(dialog().getByRole('form', { name: '编辑模型连接', exact: true }).getByLabel('请求模型名称', { exact: true })).toBeVisible();
|
||||
assert.deepEqual(errors, []); assert.ok(requests.every(value => value.method === 'GET')); assert.equal((await host.api('/model-center')).attempts.length, 0);
|
||||
mark('manual compatibility entry remains usable; every upstream call was a directory GET with zero model attempts');
|
||||
result = { success: true, version: host.bootstrap.version, appRoot, actualElectron: desktop, checks, screenshots, errors, directoryGets: requests.length, realModelRequests: 0, officialServiceTouched: false };
|
||||
} catch (error) {
|
||||
if (page) await shot('failure').catch(() => {}); result = { success: false, appRoot, actualElectron: desktop, checks, screenshots, errors, error: error.stack, realModelRequests: 0 };
|
||||
} finally {
|
||||
release?.(); if (application) await application.close(); if (browser) await browser.close(); if (controller && await controller.health()) await controller.stop();
|
||||
await new Promise(resolve => { server.closeAllConnections(); server.close(resolve); }); for (const dispose of disposers.reverse()) await dispose();
|
||||
fs.writeFileSync(path.join(directory, 'requests.json'), JSON.stringify(requests, null, 2)); fs.writeFileSync(path.join(directory, 'result.json'), JSON.stringify(result, null, 2));
|
||||
}
|
||||
console.log(JSON.stringify({ directory, ...result })); if (!result.success) process.exitCode = 1;
|
||||
@@ -26,7 +26,7 @@ try {
|
||||
await page.goto(host.url); await page.waitForLoadState('networkidle');
|
||||
await page.getByRole('button', { name: '打开模型中心', exact: true }).click();
|
||||
const dialog = page.getByRole('dialog', { name: '模型中心', exact: true }); await expect(dialog).toBeVisible();
|
||||
await dialog.getByRole('button', { name: '添加连接', exact: true }).click();
|
||||
await dialog.getByRole('button', { name: '手动填写(兼容)', exact: true }).click();
|
||||
const editor = dialog.getByRole('form', { name: '编辑模型连接', exact: true });
|
||||
await editor.getByLabel('连接名称', { exact: true }).fill('Fixture Primary');
|
||||
await editor.getByLabel('接口根地址', { exact: true }).fill('http://127.0.0.1:9/v1');
|
||||
@@ -38,7 +38,7 @@ try {
|
||||
assert.equal((await host.api('/model-center')).connections.find(value => value.name === 'Fixture Primary').hasKey, true);
|
||||
mark('connection form stores only the volatile credential and renders configured status without exposing it');
|
||||
|
||||
await dialog.getByRole('button', { name: '添加连接', exact: true }).click();
|
||||
await dialog.getByRole('button', { name: '手动填写(兼容)', exact: true }).click();
|
||||
const second = dialog.getByRole('form', { name: '编辑模型连接', exact: true });
|
||||
await second.getByLabel('连接名称', { exact: true }).fill('Fixture Reserve');
|
||||
await second.getByLabel('接口根地址', { exact: true }).fill('http://127.0.0.1:9/v1');
|
||||
|
||||
@@ -59,7 +59,7 @@ try {
|
||||
await page.goto(host.url); await page.waitForLoadState('networkidle');
|
||||
await page.getByRole('button', { name: '打开模型中心', exact: true }).click();
|
||||
const dialog = page.getByRole('dialog', { name: '模型中心', exact: true });
|
||||
await dialog.getByRole('button', { name: '添加连接', exact: true }).click();
|
||||
await dialog.getByRole('button', { name: '手动填写(兼容)', exact: true }).click();
|
||||
const form = dialog.getByRole('form', { name: '编辑模型连接', exact: true });
|
||||
await form.getByLabel('连接名称', { exact: true }).fill('验收 Responses');
|
||||
await form.getByLabel('接口协议', { exact: true }).selectOption('responses');
|
||||
|
||||
@@ -0,0 +1,105 @@
|
||||
import { useEffect, useRef, useState } from 'react';
|
||||
import { api } from './api';
|
||||
import type { CatalogPreview } from '../shared/contracts/model-catalog.mjs';
|
||||
import { ModelErrorDetails } from './ModelErrorDetails';
|
||||
import type { RuntimeFailure } from './types';
|
||||
|
||||
type Source = { id: string; revision: number; name: string; protocol: string; baseUrl: string; brand: string; enabled: boolean; credentialSource?: { connectionId: string } };
|
||||
const initial = { name: '', brand: '自定义', protocol: 'chat-completions', baseUrl: 'https://api.openai.com/v1', messagesAuth: 'api-key', requiresKey: true };
|
||||
const capabilityNames: Record<string, string> = { text: '文本', tools: '工具调用', streaming: '流式输出', vision: '图像输入', audio: '语音' };
|
||||
export function ModelCatalog({ sources, sourceId = '', busy, onSaved, onClose }: {
|
||||
sources: Source[]; sourceId?: string; busy: boolean; onSaved: () => Promise<void>; onClose: () => void;
|
||||
}) {
|
||||
const [source, setSource] = useState(sourceId), [draft, setDraft] = useState(initial);
|
||||
const [key, setKey] = useState(''), [persistKey, setPersistKey] = useState(false);
|
||||
const [preview, setPreview] = useState<CatalogPreview | null>(null), [chosen, setChosen] = useState<string[]>([]), [query, setQuery] = useState('');
|
||||
const [routing, setRouting] = useState('keep'), [defaultModel, setDefaultModel] = useState('');
|
||||
const [declare, setDeclare] = useState(false), [working, setWorking] = useState(false), [saving, setSaving] = useState(false);
|
||||
const [error, setError] = useState(''), [diagnostic, setDiagnostic] = useState<RuntimeFailure | null>(null), [notice, setNotice] = useState('');
|
||||
const live = useRef(true), generation = useRef(0), request = useRef(''), region = useRef<HTMLElement>(null);
|
||||
const current = sources.find(value => value.id === source), locked = busy || saving;
|
||||
function invalidate() {
|
||||
generation.current++; const id = request.current; request.current = '';
|
||||
if (id) void api('/model-center/catalog/cancel', { id }).catch(() => {});
|
||||
setPreview(null); setChosen([]); setWorking(false); setError(''); setDiagnostic(null); setNotice('');
|
||||
}
|
||||
useEffect(() => {
|
||||
live.current = true; region.current?.scrollIntoView({ block: 'start' });
|
||||
return () => { live.current = false; generation.current++; const id = request.current; if (id) void api('/model-center/catalog/cancel', { id }).catch(() => {}); };
|
||||
}, []);
|
||||
function failed(cause: unknown) {
|
||||
setError(cause instanceof Error ? cause.message : '操作未完成,请重新获取。');
|
||||
if (cause && typeof cause === 'object' && 'diagnostic' in cause) setDiagnostic(cause.diagnostic as RuntimeFailure | null);
|
||||
}
|
||||
async function fetchModels() {
|
||||
invalidate(); const marker = ++generation.current, requestId = crypto.randomUUID(); request.current = requestId;
|
||||
setWorking(true);
|
||||
try {
|
||||
const result = await api<CatalogPreview>('/model-center/catalog', { requestId,
|
||||
...(current ? { id: current.id, revision: current.revision } : { draft, ...(key ? { apiKey: key } : {}) }) });
|
||||
if (live.current && generation.current === marker) { setPreview(result); setNotice(`已获取 ${result.entries.length} 个模型。勾选后保存。`); }
|
||||
} catch (cause) { if (live.current && generation.current === marker) failed(cause); }
|
||||
finally { if (live.current && generation.current === marker) setWorking(false); }
|
||||
}
|
||||
async function save() {
|
||||
if (!preview) return; setSaving(true); setError(''); setDiagnostic(null);
|
||||
try {
|
||||
const result = await api<{ added: string[]; existing: string[] }>('/model-center/catalog/apply', {
|
||||
previewId: preview.id, models: chosen, routing, defaultModel, persistKey: !source && persistKey,
|
||||
...(declare ? { declarations: { text: 'supported', tools: 'supported' } } : {}),
|
||||
});
|
||||
if (live.current) {
|
||||
request.current = ''; setKey(''); setPreview(null); setChosen([]);
|
||||
setNotice(`已新增 ${result.added.length} 个模型,沿用 ${result.existing.length} 个已有模型。${routing === 'auto' ? '已设为自动适配。' : routing === 'manual' ? '默认模型已更新。' : '可在上方选择使用。'}已有模型的手动设置保持。`);
|
||||
await onSaved();
|
||||
}
|
||||
} catch (cause) { if (live.current) failed(cause); }
|
||||
finally { if (live.current) setSaving(false); }
|
||||
}
|
||||
const visible = preview?.entries.filter(value => (value.id + ' ' + value.name + ' ' + value.owner).toLowerCase().includes(query.trim().toLowerCase())) ?? [];
|
||||
const selectable = visible.filter(value => value.lifecycle !== 'retired');
|
||||
return <section className="mc-editor mc-catalog" aria-label="上游模型目录" ref={region}>
|
||||
<div className="mc-section-title"><div><h3>从上游选择模型</h3><p>获取目录,勾选要用的型号,再决定手动指定或自动适配。</p></div><button className="button secondary" disabled={saving} onClick={onClose}>关闭目录</button></div>
|
||||
<div className="mc-form-grid">
|
||||
<label className="mc-full">上游连接<select aria-label="目录上游连接" value={source} disabled={locked || working} onChange={event => { invalidate(); setSource(event.target.value); setKey(''); setPersistKey(false); }}>
|
||||
<option value="">添加新的上游</option>{sources.filter(value => value.enabled && !value.credentialSource).map(value => <option key={value.id} value={value.id}>{value.name} · {value.baseUrl}</option>)}
|
||||
</select></label>
|
||||
{!source ? <>
|
||||
<label>上游名称<input aria-label="上游名称" value={draft.name} disabled={locked || working} onChange={event => { invalidate(); setDraft({ ...draft, name: event.target.value }); }} /></label>
|
||||
<label>服务品牌<select aria-label="目录服务品牌" value={draft.brand} disabled={locked || working} onChange={event => {
|
||||
invalidate(); const brand = event.target.value;
|
||||
setDraft({ ...draft, brand, ...(brand === 'OpenAI' ? { baseUrl: 'https://api.openai.com/v1', protocol: 'responses' } : brand === 'Anthropic' ? { baseUrl: 'https://api.anthropic.com', protocol: 'anthropic-messages' } : brand === 'DeepSeek' ? { baseUrl: 'https://api.deepseek.com', protocol: 'chat-completions' } : {}) });
|
||||
}}>{['自定义', 'OpenAI', 'Anthropic', 'DeepSeek', 'Claude 兼容代理', 'Ollama', 'vLLM', '兼容服务'].map(value => <option key={value}>{value}</option>)}</select></label>
|
||||
<label>接口协议<select aria-label="目录接口协议" value={draft.protocol} disabled={locked || working} onChange={event => { invalidate(); setDraft({ ...draft, protocol: event.target.value }); }}><option value="chat-completions">Chat Completions</option><option value="responses">OpenAI Responses</option><option value="anthropic-messages">Anthropic Messages</option></select></label>
|
||||
{draft.protocol === 'anthropic-messages' && <label>Messages 鉴权<select aria-label="目录 Messages 鉴权" value={draft.messagesAuth} disabled={locked || working} onChange={event => { invalidate(); setDraft({ ...draft, messagesAuth: event.target.value }); }}><option value="api-key">x-api-key</option><option value="bearer">Bearer</option></select></label>}
|
||||
<label className="mc-full">接口根地址<input aria-label="目录接口根地址" type="url" value={draft.baseUrl} disabled={locked || working} onChange={event => { invalidate(); setDraft({ ...draft, baseUrl: event.target.value }); }} /></label>
|
||||
<label>API Key<input aria-label="目录 API Key" type="password" autoComplete="new-password" value={key} disabled={locked || working} onChange={event => { invalidate(); setKey(event.target.value); }} /></label>
|
||||
<label className="mc-checkbox"><input type="checkbox" checked={!draft.requiresKey} disabled={locked || working} onChange={event => { invalidate(); setDraft({ ...draft, requiresKey: !event.target.checked }); }} />此上游不需要 Key</label>
|
||||
<label className="mc-checkbox mc-full"><input type="checkbox" checked={persistKey} disabled={locked || working || !key} onChange={event => setPersistKey(event.target.checked)} />保存模型时,将本次新填写的 Key 在本机加密保存</label>
|
||||
</> : <p className="mc-help mc-full">使用该连接当前的凭据获取目录;新增型号引用同一凭据来源。</p>}
|
||||
</div>
|
||||
<div className="mc-row-actions"><button className="button primary" disabled={locked || working || !!source && !current || !source && (!draft.name.trim() || !draft.baseUrl || draft.requiresKey && !key)} onClick={() => void fetchModels()}>{working ? '正在获取模型…' : preview ? '重新获取模型目录' : '获取上游模型'}</button>
|
||||
{working && <button className="button secondary" onClick={() => { invalidate(); setNotice('已取消获取。可修改上游后重新获取。'); }}>取消获取</button>}</div>
|
||||
<p className="mc-help">只访问模型目录,不发起推理请求。兼容服务未开放目录时,可返回“手动填写(兼容)”。</p>
|
||||
{error && <p className="mc-card-warning" role="alert">{error}</p>}{diagnostic && <ModelErrorDetails failure={diagnostic} />}
|
||||
{notice && <p className="mc-help" role="status">{notice}</p>}
|
||||
{preview && <>
|
||||
<div className="mc-catalog-heading"><label>搜索模型<input aria-label="搜索上游模型" value={query} onChange={event => setQuery(event.target.value)} placeholder="型号、显示名称或提供方" /></label><span>显示 {visible.length} / {preview.entries.length} · 已选 {chosen.length}</span></div>
|
||||
{preview.warning && <p className="mc-card-warning">{preview.warning}</p>}
|
||||
<p className="mc-help">以下为上游目录声明,尚未经过模型调用验证。目录最多1000项,本机最多100个模型连接;单个自动池最多32个成员。</p>
|
||||
<div className="mc-row-actions"><button className="button secondary" disabled={locked || !selectable.length || new Set([...chosen, ...selectable.map(value => value.id)]).size > 100} onClick={() => setChosen([...new Set([...chosen, ...selectable.map(value => value.id)])])}>选中当前筛选</button><button className="button secondary" disabled={locked || !chosen.length} onClick={() => setChosen([])}>清空选择</button></div>
|
||||
<div className="mc-catalog-list" aria-label="上游模型列表">
|
||||
{visible.map(value => <label className={'mc-catalog-model' + (chosen.includes(value.id) ? ' is-selected' : '')} key={value.id}>
|
||||
<input type="checkbox" aria-label={'选择模型 ' + value.id} checked={chosen.includes(value.id)} disabled={locked || value.lifecycle === 'retired' || !chosen.includes(value.id) && chosen.length >= 100} onChange={event => setChosen(event.target.checked ? [...chosen, value.id] : chosen.filter(id => id !== value.id))} />
|
||||
<span><strong>{value.id}</strong>{value.name !== value.id && <span>{value.name}</span>}<small>{[value.owner, value.contextLimit && `上下文 ${value.contextLimit.toLocaleString()} tokens`, value.maxOutputTokens && `输出上限 ${value.maxOutputTokens.toLocaleString()}`, value.lifecycle && value.lifecycle !== 'active' && (value.lifecycle === 'retired' ? '已退役' : '待退役')].filter(Boolean).join(' · ') || '上游未提供详细元数据'}</small>
|
||||
<small>{Object.entries(value.capabilityDeclarations).filter(([, state]) => state !== 'unknown').map(([capability, state]) => `${capabilityNames[capability]}${state === 'supported' ? '支持' : '不支持'}`).join(' · ') || '文本、工具、流式等能力待确认'}{value.efforts.length ? ` · 推理强度 ${value.efforts.join(' / ')}` : ''}</small></span>
|
||||
</label>)}{!visible.length && <p className="mc-empty">{preview.entries.length ? '没有匹配的型号。可更改搜索条件。' : '上游返回了空目录。可核对连接或使用手动兼容入口。'}</p>}
|
||||
</div>
|
||||
<label className="mc-checkbox"><input type="checkbox" checked={declare} disabled={locked} onChange={event => setDeclare(event.target.checked)} />我确认所选型号可通过此协议处理文本及工具调用,仅补充目录未声明的项目</label>
|
||||
<div className="mc-form-grid"><label>保存后的使用方式<select aria-label="模型使用方式" value={routing} disabled={locked} onChange={event => setRouting(event.target.value)}><option value="keep">保留当前选择,稍后手动使用</option><option value="manual">手动指定默认模型</option><option value="auto">自动适配所选模型</option></select></label>
|
||||
{routing === 'manual' && <label>默认模型<select aria-label="目录默认模型" value={defaultModel} disabled={locked} onChange={event => setDefaultModel(event.target.value)}><option value="">请选择型号</option>{chosen.map(id => <option key={id}>{id}</option>)}</select></label>}</div>
|
||||
{routing === 'auto' && <p className="mc-help">自动适配复用模型池,按任务所需能力、连接健康和可用并发筛选,再按轮询分配;同一运行优先保持型号。未确认能力的型号不会用于需要该能力的任务。</p>}
|
||||
<div className="mc-actions"><button className="button primary" disabled={locked || !chosen.length || routing === 'manual' && !chosen.includes(defaultModel) || routing === 'auto' && chosen.length > 32} onClick={() => void save()}>{saving ? '正在保存…' : `保存所选 ${chosen.length} 个模型`}</button></div>
|
||||
</>}
|
||||
</section>;
|
||||
}
|
||||
@@ -25,4 +25,6 @@
|
||||
.mc-route-control{margin-bottom:24px;padding:18px 20px;background:#fff;border:1px solid #b8cadb;border-left:3px solid #2560a3;border-radius:6px;min-width:0}.mc-route-heading{display:flex;align-items:center;justify-content:space-between;gap:16px;margin-bottom:14px}.mc-route-heading h3{font-size:15px;margin-bottom:5px}.mc-route-heading p,.mc-route-detail{font-size:12px;color:#4b6078;line-height:1.7;overflow-wrap:anywhere}.mc-route-heading>.button{flex-shrink:0}.mc-route-select{display:flex;gap:12px;align-items:center;font-size:12px;color:#425974}.mc-route-select select{min-width:0;flex:1;width:100%;min-height:42px;border:1px solid #a7bbcf;border-radius:5px;background:#fff;padding:10px;color:#15283f;font-size:13px}.mc-route-detail{margin-top:9px}.mc-run-model{display:flex;flex-wrap:wrap;gap:6px 12px;border-top:1px solid #dce4eb;margin-top:13px;padding-top:12px;font-size:12px;line-height:1.7;overflow-wrap:anywhere}.mc-run-model strong{color:#344c67}.mc-run-model span{color:#405670}.mc-run-model small{width:100%;color:#566b81;font-size:11px}.mc-route-select select:focus-visible{outline:2px solid #2560a3;outline-offset:2px}
|
||||
@media(max-width:620px){.mc-route-control{padding:14px}.mc-route-heading{align-items:flex-start;gap:10px}.mc-route-heading p{font-size:11px}.mc-route-heading>.button{padding:8px;font-size:11px}.mc-route-select{align-items:stretch;flex-direction:column;gap:7px}.mc-route-select select{font-size:12px}.mc-run-model{display:block}.mc-run-model strong,.mc-run-model span,.mc-run-model small{display:block}.mc-run-model span{margin-top:4px}}
|
||||
.mc-request-url { overflow-wrap: anywhere; font-family: var(--font-mono, monospace); font-size: 12px; line-height: 1.6; }
|
||||
.mc-section-title .mc-row-actions,.mc-catalog .mc-row-actions{display:flex;flex-wrap:wrap;gap:8px}.mc-catalog{margin-bottom:24px;border-top:3px solid #2560a3;min-width:0}.mc-catalog-heading{display:flex;align-items:end;flex-wrap:wrap;gap:12px;margin:18px 0 12px}.mc-catalog-heading label{display:grid;gap:7px;flex:1;min-width:180px}.mc-catalog-heading input{padding:10px;border:1px solid #a9bdcf;border-radius:5px;width:100%;min-width:0;background:#fff}.mc-catalog-heading>span{font-size:12px;color:#40566e;padding:10px 0}.mc-catalog-list{margin:12px 0;max-height:440px;overflow:auto;border:1px solid #b8cadb;border-radius:5px;background:#fff;overscroll-behavior:contain}.mc-catalog-model{display:flex;gap:12px;align-items:start;padding:13px 14px;border-bottom:1px solid #d3dde7;cursor:pointer;min-width:0}.mc-catalog-model:last-child{border-bottom:0}.mc-catalog-model.is-selected{background:#eaf2fa}.mc-catalog-model>input{margin-top:4px;flex:0 0 auto;width:18px;height:18px;accent-color:#2560a3}.mc-catalog-model>span{display:grid;gap:5px;min-width:0;overflow-wrap:anywhere;line-height:1.6}.mc-catalog-model strong{font-family:Consolas,"Microsoft YaHei",monospace;font-size:13px}.mc-catalog-model small{font-size:12px;color:#40566e}.mc-catalog input:focus-visible{outline:2px solid #2560a3;outline-offset:2px}.mc-catalog>.mc-checkbox{display:flex;align-items:start;gap:9px;line-height:1.8;margin:16px 0;color:#40566e;font-size:12px}.mc-catalog>.mc-checkbox>input{flex:0 0 auto;margin-top:4px}
|
||||
@media(max-width:620px){.mc-catalog-heading{align-items:stretch}.mc-catalog-heading>span{padding:0}.mc-catalog-model{padding:12px 10px;gap:10px}.mc-catalog-model>input{width:22px;height:22px}.mc-catalog-list{max-height:380px}.mc-catalog .mc-section-title>.button{align-self:flex-end}}
|
||||
.mc-reasoning .mc-form-grid{margin-top:12px}.mc-reasoning .mc-help{color:#40566e;font-size:12px}.mc-feature-scope{font-size:12px;line-height:1.7;margin:12px 0;overflow-wrap:anywhere}.mc-feature-scope summary{cursor:pointer;color:#245b92;padding:8px 0;min-height:40px}.mc-feature-scope table{width:100%;table-layout:fixed;border-collapse:collapse;margin-top:10px}.mc-feature-scope th,.mc-feature-scope td{text-align:left;vertical-align:top;border-bottom:1px solid #d3dde7;padding:9px 7px;font-size:12px;color:#40566e}.mc-feature-scope th{font-weight:600}.mc-reasoning input:focus-visible{outline:2px solid #2560a3;outline-offset:2px}
|
||||
|
||||
@@ -11,10 +11,12 @@ import { ModelErrorDetails } from './ModelErrorDetails';
|
||||
import { ModelBudgetSettings } from './ModelBudgetSettings';
|
||||
import { modelRequestUrl, protocolLabel } from '../shared/contracts/model-endpoint.mjs';
|
||||
import { ModelNetworkDiagnostics } from './ModelNetworkDiagnostics';
|
||||
import { ModelCatalog } from './ModelCatalog';
|
||||
import type { CatalogModel } from '../shared/contracts/model-catalog.mjs';
|
||||
|
||||
const previewUrl = (value: { protocol: string; baseUrl: string }) => { try { return modelRequestUrl(value); } catch { return '请填写有效的接口根地址'; } };
|
||||
|
||||
type Connection = { id: string; name: string; brand: string; protocol: string; parameterProfile?: string; messagesAuth?: string; baseUrl: string; model: string; enabled: boolean; revision: number; hasKey: boolean;
|
||||
type Connection = { id: string; name: string; brand: string; protocol: string; parameterProfile?: string; messagesAuth?: string; baseUrl: string; model: string; enabled: boolean; revision: number; hasKey: boolean; credentialSource?: { connectionId: string }; upstreamModel?: CatalogModel;
|
||||
requiresKey: boolean; modelIdentityPolicy?: string; modelIdentityAliases?: string[]; modelIdentityAliasSource?: string; 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 = { modelIdentity?: ModelIdentity; diagnostic?: RuntimeFailure; requestUrl?: string; upstreamResponse?: { requestId?: string | null; requestUrl?: string; responseUrl?: string | null; redirectMode?: string }; 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 } };
|
||||
@@ -24,7 +26,7 @@ type Operations = { checkedAt: string; version: string; scope: string; activeTas
|
||||
artifacts?: { offset: number; checked: number; total: number; remaining: number; olderOffset: number | null; newerOffset: number | null; results: { id: string; passed: boolean; error?: string }[] } };
|
||||
type SavedCredential = { id: string; name: string; revision: number; status: string; enabled?: boolean; savedAt?: string; message: string };
|
||||
type CredentialView = { supported: boolean; unavailable: boolean; connections: SavedCredential[]; scope: string };
|
||||
const savedStates: Record<string, string> = { not_saved: '仅本次内存', saved: '本机加密保存', disabled: '关闭自动加载', source_changed: '配置已变化', decrypt_failed: '无法解密', user_mismatch: '用户不匹配', directory_mismatch: '目录不匹配', unavailable: '保存登记不可用' };
|
||||
const savedStates: Record<string, string> = { reference: '共用上游凭据', not_saved: '仅本次内存', saved: '本机加密保存', disabled: '关闭自动加载', source_changed: '配置已变化', decrypt_failed: '无法解密', user_mismatch: '用户不匹配', directory_mismatch: '目录不匹配', unavailable: '保存登记不可用' };
|
||||
const states: Record<string, string> = { configured: '已配置', needs_key: '待填凭据', disabled: '已停用', needs_attention: '需要处理', cooling: '冷却中', completed: '收到响应', failed: '失败', running: '请求中', unknown: '结果待核对', interrupted: '已中断', rejected: '能力不匹配', passed: '通过', attention: '待配置' };
|
||||
const initial = { modelIdentityAliasesText: '', modelIdentityPolicy: 'record', modelIdentityAliases: [] as string[], modelIdentityAliasSource: '', name: '', brand: '自定义', protocol: 'chat-completions', streaming: false, capabilityDeclarations: { text: 'supported', tools: 'supported', streaming: 'unknown', vision: 'unknown', audio: 'unknown' } as Record<string, string>, model: '', baseUrl: 'https://api.openai.com/v1', requiresKey: true, enabled: true, contextLimit: 16000, maxOutputTokens: 2048, concurrency: 1 };
|
||||
const capabilityLabels: Record<string, string> = { text: '文本', tools: '工具', streaming: '流式', vision: '视觉', audio: '语音' };
|
||||
@@ -37,6 +39,7 @@ export function ModelCenter({ onConfig, onLegacy, busy, runModel }: { onConfig:
|
||||
const [draft, setDraft] = useState<typeof initial & { id?: string; revision?: number; messagesAuth?: string; parameterProfile?: string; nativeParameters?: Record<string, unknown>; outputTokenField?: string }>(initial);
|
||||
const [editing, setEditing] = useState(false), [key, setKey] = useState('');
|
||||
const [nativeTarget, setNativeTarget] = useState<Connection | null>(null);
|
||||
const [catalogSource, setCatalogSource] = useState<string | 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);
|
||||
@@ -111,14 +114,17 @@ export function ModelCenter({ onConfig, onLegacy, busy, runModel }: { onConfig:
|
||||
if (alive.current) { onConfig(result.config); setNotice('等待时限已保存,从下一次运行生效。当前模型路由保持。'); }
|
||||
})} />}
|
||||
{tab === 'connections' && <>
|
||||
<div className="mc-section-title"><div><h3>模型连接</h3><p>保存多条连接后可在上方切换;添加连接不会替换当前模型。凭据可选择本机加密保存。</p></div><button className="button primary" disabled={locked} onClick={addConnection}><Plus size={16} />添加连接</button></div>
|
||||
<div className="mc-section-title"><div><h3>模型连接</h3><p>从上游获取目录并选择多个型号,可手动使用或加入自动适配模型池。</p></div><div className="mc-row-actions"><button className="button primary" disabled={locked} onClick={() => { setCatalogSource(''); setEditing(false); setNativeTarget(null); }}><Plus size={16} />从上游添加模型</button><button className="button secondary" disabled={locked} onClick={addConnection}>手动填写(兼容)</button></div></div>
|
||||
{catalogSource !== null && <ModelCatalog key={catalogSource} sources={view.connections} sourceId={catalogSource} busy={locked} onSaved={refresh} onClose={() => setCatalogSource(null)} />}
|
||||
<div className="mc-connections">{view.connections.map(value => <article className={'mc-connection ' + (!value.enabled ? 'mc-disabled' : '')} key={value.id}>
|
||||
<div className="mc-card-heading"><span className="mc-service-icon"><Server size={20} /></span><div><h4>{value.name}</h4><span>{value.brand} · {protocolLabel(value.protocol)}{value.streaming ? ' · 流式开启' : ''}</span></div><span className={'mc-state mc-state-' + value.status}>{states[value.status]}</span></div>
|
||||
<dl><dt>模型</dt><dd>{value.model || '尚未填写'}</dd><dt>请求地址</dt><dd>{previewUrl(value)}</dd>{value.protocol === 'anthropic-messages' && <><dt>鉴权</dt><dd>{value.messagesAuth === 'bearer' ? 'Bearer' : 'x-api-key'} · anthropic-version 2023-06-01</dd></>}<dt>凭据</dt><dd>{value.hasKey ? value.savedCredential?.status === 'saved' ? '已设置,可从本机加密保存加载' : '已设置,重启需重新填写' : value.requiresKey ? '尚未填写' : '此服务可不填'}</dd><dt>请求 / 容量</dt><dd>{value.activeRequests} 在途 · 并发 {value.concurrency} · 上下文 {value.contextLimit.toLocaleString()}</dd></dl>
|
||||
<dl><dt>模型</dt><dd>{value.model || '尚未填写'}</dd><dt>请求地址</dt><dd>{previewUrl(value)}</dd>{value.protocol === 'anthropic-messages' && <><dt>鉴权</dt><dd>{value.messagesAuth === 'bearer' ? 'Bearer' : 'x-api-key'} · anthropic-version 2023-06-01</dd></>}<dt>凭据</dt><dd>{value.credentialSource ? '共用 ' + (view.connections.find(source => source.id === value.credentialSource!.connectionId)?.name ?? '源连接') + (value.hasKey ? ' 的上游凭据' : ';请核对源连接是否启用及凭据') : value.hasKey ? value.savedCredential?.status === 'saved' ? '已设置,可从本机加密保存加载' : '已设置,重启需重新填写' : value.requiresKey ? '尚未填写' : '此服务可不填'}</dd><dt>请求 / 容量</dt><dd>{value.activeRequests} 在途 · 并发 {value.concurrency} · 上下文 {value.contextLimit.toLocaleString()}</dd></dl>
|
||||
{value.upstreamModel && <p className="mc-help">型号来自上游目录,能力按目录声明与显式设置保存;可在“推理与能力”查看已接入范围。</p>}
|
||||
<div className="mc-capabilities">{Object.entries(capabilityLabels).map(([key, title]) => <span key={key} data-capability={value.capabilities?.[key] ?? 'unknown'}>{title}:{capabilityStates[value.capabilities?.[key] ?? 'unknown']}</span>)}</div>
|
||||
{value.health.reason && <p className="mc-card-warning">{value.health.reason}{!!value.health.cooldownUntil && value.health.cooldownUntil > Date.now() && ' · 冷却至 ' + new Date(value.health.cooldownUntil).toLocaleTimeString()}</p>}
|
||||
<footer><button className="button secondary" disabled={locked || !value.enabled} onClick={() => void act(async () => { const result = await api<{ config: Config }>('/model-center/select', { type: value.id === 'legacy-default' ? 'legacy' : 'connection', id: value.id }); onConfig(result.config, { type: value.id === 'legacy-default' ? 'legacy' : 'connection', id: value.id, name: value.name }); })}>{selected?.id === value.id ? <Check size={14} /> : <Route size={14} />}{selected?.id === value.id ? '当前使用' : '用于新任务'}</button>
|
||||
<button className="button secondary" disabled={locked} onClick={() => { if (value.id === 'legacy-default') { onLegacy(); return; } setDraft({ ...initial, ...value, modelIdentityAliasesText: (value.modelIdentityAliases ?? []).join(', '), streaming: value.streaming === true, capabilityDeclarations: { ...initial.capabilityDeclarations, ...value.capabilityDeclarations } }); setKey(''); setPersistKey(false); setEditing(true); }}>编辑</button>
|
||||
<button className="button secondary" disabled={locked || !value.enabled} onClick={() => { setCatalogSource(value.credentialSource?.connectionId ?? value.id); setEditing(false); setNativeTarget(null); }}>获取同源模型</button>
|
||||
<button className="button secondary" onClick={() => { setNetworkTarget(value.id); setTab('network'); }}>连接诊断</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>
|
||||
@@ -162,7 +168,7 @@ export function ModelCenter({ onConfig, onLegacy, busy, runModel }: { onConfig:
|
||||
const result = await api<{ config: Config }>('/model-center/credentials', { id: target.id, revision: target.revision, action: 'save', apiKey: credentialKey });
|
||||
if (alive.current) { setCredentialKey(''); setNotice('本机加密凭据已保存;宿主重启后按原连接身份加载。'); onConfig(result.config); }
|
||||
}); }}>
|
||||
<div className="mc-form-grid"><label>选择保存连接<select aria-label="选择保存连接" value={credentialTarget || view.connections[0]?.id || ''} onChange={event => { setCredentialTarget(event.target.value); setCredentialKey(''); }}>{view.connections.map(value => <option key={value.id} value={value.id}>{value.name} · {value.model || '未填写模型'}</option>)}</select></label>
|
||||
<div className="mc-form-grid"><label>选择保存连接<select aria-label="选择保存连接" value={credentialTarget || view.connections[0]?.id || ''} onChange={event => { setCredentialTarget(event.target.value); setCredentialKey(''); }}>{view.connections.filter(value => !value.credentialSource).map(value => <option key={value.id} value={value.id}>{value.name} · {value.model || '未填写模型'}</option>)}</select></label>
|
||||
<label>重新填写 API Key<input type="password" autoComplete="off" value={credentialKey} onChange={event => setCredentialKey(event.target.value)} /></label></div>
|
||||
<p className="mc-help">只保存这次新填的凭据。不同用户、目录或连接配置无法加载原保存块。</p>
|
||||
<div className="mc-actions"><button type="submit" className="button primary" disabled={locked || !credentialView.supported || credentialView.unavailable || !credentialKey.trim()}>加密保存凭据</button></div>
|
||||
@@ -170,11 +176,11 @@ export function ModelCenter({ onConfig, onLegacy, busy, runModel }: { onConfig:
|
||||
<div className="mc-connections">{credentialView.connections.map(value => <article className="mc-connection" key={value.id}>
|
||||
<div className="mc-card-heading"><KeyRound size={20} /><div><h4>{value.name}</h4><span>{savedStates[value.status] || value.status}</span></div></div>
|
||||
<p className="mc-help">{value.message}</p>{value.savedAt && <p className="mc-help">保存于 {new Date(value.savedAt).toLocaleString()}</p>}
|
||||
<div className="mc-actions">{value.status !== 'not_saved' && <>
|
||||
<div className="mc-actions">{value.status !== 'not_saved' && value.status !== 'reference' && <>
|
||||
<button className="button secondary" disabled={locked} onClick={() => void act(async () => { await api('/model-center/credentials', { id: value.id, revision: value.revision, action: value.enabled ? 'disable' : 'enable' }); })}>{value.enabled ? '关闭自动加载' : '启用自动加载'}</button>
|
||||
<button className="button secondary" disabled={locked} onClick={() => void act(async () => { await api('/model-center/credentials', { id: value.id, revision: value.revision, action: 'delete' }); })}>删除本机保存</button>
|
||||
</>}
|
||||
<button className="button secondary" disabled={locked} onClick={() => void act(async () => { const result = await api<{ config: Config }>('/model-center/credentials', { id: value.id, revision: value.revision, action: 'forget' }); if (alive.current) onConfig(result.config); })}>清除该连接凭据</button>
|
||||
{value.status !== 'reference' && <button className="button secondary" disabled={locked} onClick={() => void act(async () => { const result = await api<{ config: Config }>('/model-center/credentials', { id: value.id, revision: value.revision, action: 'forget' }); if (alive.current) onConfig(result.config); })}>清除该连接凭据</button>}
|
||||
</div>
|
||||
</article>)}</div><p className="mc-help">删除本机保存保留本次内存连接;清除该连接凭据会同时移除本机保存与内存凭据。</p>
|
||||
</>}
|
||||
|
||||
@@ -2,8 +2,9 @@ import { useEffect, useRef, useState } from 'react';
|
||||
import { nativeParameterSettings } from '../shared/contracts/native-parameters.mjs';
|
||||
import { changeReasoningSetting, modelParameterProfile, reasoningSetting } from '../shared/contracts/model-parameter-profiles.mjs';
|
||||
import { ReasoningSettings } from './ReasoningSettings';
|
||||
import type { CatalogModel } from '../shared/contracts/model-catalog.mjs';
|
||||
|
||||
export type NativeConnection = { id: string; revision: number; name: string; protocol: string; model?: string; brand?: string; baseUrl?: string; parameterProfile?: string; capabilityDeclarations?: Record<string, string>; capabilities?: Record<string, string>; nativeParameters?: Record<string, unknown>; outputTokenField?: string };
|
||||
export type NativeConnection = { id: string; revision: number; name: string; protocol: string; model?: string; brand?: string; baseUrl?: string; parameterProfile?: string; capabilityDeclarations?: Record<string, string>; capabilities?: Record<string, string>; nativeParameters?: Record<string, unknown>; outputTokenField?: string; upstreamModel?: CatalogModel };
|
||||
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; parameterProfile?: string }) => Promise<void> }) {
|
||||
const [text, setText] = useState(JSON.stringify(connection.nativeParameters ?? {}, null, 2));
|
||||
|
||||
@@ -17,17 +17,18 @@ export function ReasoningSettings({ connection, parameters, profile, busy, onPro
|
||||
{parameterProfiles(connection.protocol).map(value => <option key={value.id} value={value.id}>{value.label}</option>)}
|
||||
</select></label>
|
||||
<label>推理模式<select aria-label="推理模式" value={setting.mode} disabled={disabled} onChange={event => change('mode', event.target.value)}>
|
||||
<option value="default">跟随模型默认</option><option value="disabled">关闭推理</option>
|
||||
<option value="enabled">{connection.protocol === 'anthropic-messages' ? '开启 · 指定推理预算' : '开启推理'}</option>
|
||||
{connection.protocol === 'anthropic-messages' && <option value="adaptive">开启 · 自适应推理</option>}
|
||||
<option value="default">跟随模型默认</option><option value="disabled" disabled={!resolved.modes.includes('disabled')}>关闭推理</option>
|
||||
<option value="enabled" disabled={!resolved.modes.includes('enabled')}>{connection.protocol === 'anthropic-messages' ? '开启 · 指定推理预算' : '开启推理'}</option>
|
||||
{connection.protocol === 'anthropic-messages' && <option value="adaptive" disabled={!resolved.modes.includes('adaptive')}>开启 · 自适应推理</option>}
|
||||
</select></label>
|
||||
<label>推理强度<select aria-label="推理强度" value={setting.effort} disabled={disabled || setting.mode === 'disabled'} onChange={event => change('effort', event.target.value)}>
|
||||
<label>推理强度<select aria-label="推理强度" value={setting.effort} disabled={disabled || setting.mode === 'disabled' || !resolved.efforts.length && !setting.effort} onChange={event => change('effort', event.target.value)}>
|
||||
<option value="">跟随模型默认</option>{efforts.map(value => <option key={value} value={value}>{effortLabels[value] ?? value}({value})</option>)}
|
||||
</select></label>
|
||||
{connection.protocol === 'anthropic-messages' && setting.mode === 'enabled' && <label>推理预算 tokens<input aria-label="推理预算 tokens" type="number" min={1024} step={1} value={setting.budget} disabled={disabled} onChange={event => change('budget', Number(event.target.value))} /></label>}
|
||||
</div>
|
||||
<p className="mc-help">当前格式:{resolved.label}。{resolved.source}。自定义代理可按实际兼容格式选择配置档,保存后用于新任务。</p>
|
||||
<p className="mc-help">强度越高通常需要更多时间和输出 tokens;推理与最终答复共用原输出预算。具体型号支持的开关和档位以上游为准,设置不会自动提高预算。</p>
|
||||
{connection.upstreamModel && <p className="mc-help">已同步该型号的上游目录声明;提供了档位或模式时,按声明限制可选项。目录没有提供的能力仍待确认。</p>}
|
||||
{connection.protocol === 'anthropic-messages' ? <p className="mc-help">指定预算至少 1024 tokens,且须小于本次任务实际输出上限。指定预算的能力检测使用当前输出上限,会消耗模型额度;自适应模式与强度需服务支持。</p>
|
||||
: resolved.id === 'deepseek' && connection.protocol === 'chat-completions' ? <p className="mc-help">DeepSeek 使用 thinking 开关与 low / high / max 强度;工具往返携带对应的原生推理状态。</p>
|
||||
: <p className="mc-help">关闭推理发送 none,仅适用于接受该值的型号;GPT-6.1 Sol 不支持 none / minimal。可选择受支持的强度或保留默认。</p>}
|
||||
|
||||
@@ -27,6 +27,8 @@ export class CredentialVault {
|
||||
...(connection.protocol === 'anthropic-messages' ? { messagesAuth: connection.messagesAuth ?? 'api-key' } : {}) })));
|
||||
}
|
||||
status(connection) {
|
||||
if (connection.credentialSource) return { status: 'reference', storage: 'source-reference', sourceId: connection.credentialSource.connectionId,
|
||||
message: '共用上游源连接的凭据与加载状态,请在源连接管理;没有复制或额外保存 Key。' };
|
||||
if (this.blocked) return { status: 'unavailable', storage: 'windows-user-dpapi', message: '本机保存登记格式损坏或来自新版本,未覆盖原记录。' };
|
||||
const value = this.state.entries[connection.id];
|
||||
if (!value) return { status: 'not_saved', storage: 'memory', message: '当前凭据只在宿主内存中。' };
|
||||
|
||||
@@ -0,0 +1,151 @@
|
||||
import { randomUUID, createHash } from 'node:crypto';
|
||||
import { catalogPage, catalogRoute } from '../../shared/contracts/model-catalog.mjs';
|
||||
import { messagesAuthSettings, validateProtocolEndpoint } from '../../shared/contracts/model-endpoint.mjs';
|
||||
import { modelProtocol, capabilityDeclarations } from '../../shared/contracts/model-capabilities.mjs';
|
||||
|
||||
const fail = (message, status = 400) => Object.assign(new Error(message), { status });
|
||||
const stamp = () => new Date().toISOString();
|
||||
const clean = (value, max) => String(value ?? '').trim().slice(0, max);
|
||||
export const credentialScope = connection => createHash('sha256').update(JSON.stringify([
|
||||
connection.protocol ?? 'chat-completions', connection.baseUrl, connection.requiresKey !== false,
|
||||
connection.protocol === 'anthropic-messages' ? connection.messagesAuth ?? 'api-key' : null,
|
||||
])).digest('hex');
|
||||
|
||||
export class ModelCatalog {
|
||||
constructor(hub) { this.hub = hub; this.active = new Map(); this.previews = new Map(); this.cancelled = new Map(); }
|
||||
prune() {
|
||||
for (const [id, value] of this.previews) if (value.expires <= Date.now()) this.previews.delete(id);
|
||||
for (const [id, expires] of this.cancelled) if (expires <= Date.now()) this.cancelled.delete(id);
|
||||
}
|
||||
cancel(id) {
|
||||
if (typeof id !== 'string' || !/^[a-f0-9-]{36}$/i.test(id)) throw fail('目录请求标识无效。');
|
||||
this.prune(); if (this.cancelled.size >= 128) this.cancelled.delete(this.cancelled.keys().next().value);
|
||||
this.cancelled.set(id, Date.now() + 5 * 60000);
|
||||
this.active.get(id)?.abort(fail('已取消获取上游模型。')); this.previews.delete(id); return { cancelled: true };
|
||||
}
|
||||
stopAll() {
|
||||
for (const controller of this.active.values()) controller.abort(fail('宿主正在停止获取模型目录。', 409));
|
||||
this.previews.clear(); this.cancelled.clear();
|
||||
}
|
||||
binding(connection) {
|
||||
const owner = this.hub.credentialOwner(connection);
|
||||
return { id: connection.id, revision: connection.revision, ownerId: owner?.id, ownerRevision: owner?.revision,
|
||||
credentialRevision: this.hub.credential(connection)?.revision ?? 0 };
|
||||
}
|
||||
current(value) {
|
||||
if (!value.binding) return;
|
||||
const connection = this.hub.connection(value.binding.id);
|
||||
if (JSON.stringify(this.binding(connection)) !== JSON.stringify(value.binding) || !connection.enabled) throw fail('获取期间连接或凭据已变化,请重新获取模型列表。', 409);
|
||||
}
|
||||
async fetch(input) {
|
||||
if (this.hub.provider.requestsClosing) throw fail('执行宿主正在停止,请稍后获取模型目录。', 409);
|
||||
this.prune();
|
||||
const id = input.requestId ?? randomUUID();
|
||||
if (typeof id !== 'string' || !/^[a-f0-9-]{36}$/i.test(id)) throw fail('目录请求标识无效。');
|
||||
if (this.cancelled.has(id)) throw fail('该目录请求已取消,请重新获取。', 409);
|
||||
if (this.active.size || this.hub.probing) throw fail('已有模型目录或能力检测正在执行。', 409);
|
||||
if (this.previews.has(id)) throw fail('该目录预览已存在,请使用新的请求。', 409);
|
||||
let connection, key, binding = null, explicitKey = null;
|
||||
if (input.id) {
|
||||
connection = { ...this.hub.connection(input.id) };
|
||||
if (input.revision !== connection.revision || !connection.enabled) throw fail('连接已修改或停用,请刷新后获取。', 409);
|
||||
binding = this.binding(connection); key = this.hub.credential(connection)?.key ?? '';
|
||||
if (Object.hasOwn(input, 'apiKey') || input.draft) throw fail('已保存连接通过原凭据获取目录,请先保存显式配置变更。');
|
||||
} else {
|
||||
const draft = input.draft ?? {}, protocol = modelProtocol(draft.protocol);
|
||||
const baseUrl = validateProtocolEndpoint(protocol, new URL(this.hub.provider.validate({ baseUrl: draft.baseUrl, mode: 'demo' })).toString().replace(/\/+$/, ''));
|
||||
if (baseUrl.length > 1000 || !clean(draft.name, 100)) throw fail('请先填写连接名称和接口根地址。');
|
||||
connection = { name: clean(draft.name, 100), brand: clean(draft.brand || '自定义', 40), baseUrl, protocol,
|
||||
...messagesAuthSettings(protocol, draft.messagesAuth), requiresKey: draft.requiresKey !== false, enabled: true,
|
||||
contextLimit: draft.contextLimit, maxOutputTokens: draft.maxOutputTokens, concurrency: draft.concurrency };
|
||||
key = input.apiKey ?? '';
|
||||
if (typeof key !== 'string' || key.length > 16000) throw fail('模型凭据格式无效。');
|
||||
key = key.trim(); explicitKey = key || null;
|
||||
}
|
||||
if (connection.requiresKey && !key) throw fail('请先填写该上游的 API Key,再获取模型目录。');
|
||||
const controller = new AbortController(), deadline = Date.now() + 60000;
|
||||
this.active.set(id, controller);
|
||||
try {
|
||||
const entries = new Map(), cursors = new Set(), route = catalogRoute(connection);
|
||||
let cursor = '', complete = true, warning = '', pages = 0, bytes = 0;
|
||||
do {
|
||||
controller.signal.throwIfAborted();
|
||||
this.current({ binding });
|
||||
if (Date.now() >= deadline) throw fail('获取模型目录超过60秒,请稍后重试。');
|
||||
const query = cursor ? '?after_id=' + encodeURIComponent(cursor) + '&limit=100' : connection.protocol === 'anthropic-messages' ? '?limit=100' : '';
|
||||
const raw = await this.hub.provider.request({ ...connection, model: '', mode: 'demo' }, route + query, undefined, controller.signal, key, undefined,
|
||||
{ attempts: 1, timeoutMs: Math.min(20000, deadline - Date.now()), totalTimeoutMs: Math.min(20000, deadline - Date.now()) });
|
||||
const encoded = this.hub.provider.redact(JSON.stringify(raw), key); bytes += Buffer.byteLength(encoded);
|
||||
if (bytes > 4 * 1024 * 1024) throw fail('模型目录超过4MB上限,请使用供应商筛选接口或手动添加。');
|
||||
const page = catalogPage(JSON.parse(encoded)); pages++;
|
||||
if (page.entries.length > 1000) throw fail('单页模型超过1000项,请核对上游分页。');
|
||||
for (const item of page.entries) {
|
||||
if (entries.has(item.id) && JSON.stringify(entries.get(item.id)) !== JSON.stringify(item)) throw fail('上游目录包含相同型号的冲突定义,请核对后重试。');
|
||||
if (entries.size === 1000 && !entries.has(item.id)) { complete = false; warning = '已达到1000项目录上限;当前只显示已获取部分,可使用手动兼容入口。'; break; }
|
||||
entries.set(item.id, item);
|
||||
}
|
||||
if (!page.hasMore || !complete) break;
|
||||
if (cursors.has(page.cursor) || !page.entries.length) throw fail('上游分页未向前推进,已停止获取。');
|
||||
cursors.add(page.cursor); cursor = page.cursor;
|
||||
if (pages === 20 || entries.size === 1000) { complete = false; warning = '目录仍有后续页,已达到本次获取上限;当前只显示部分模型。'; break; }
|
||||
} while (true);
|
||||
controller.signal.throwIfAborted(); this.current({ binding }); this.prune();
|
||||
if (this.previews.size >= 8) this.previews.delete(this.previews.keys().next().value);
|
||||
const fetchedAt = stamp(), expires = Date.now() + 5 * 60000;
|
||||
const preview = { id, entries: [...entries.values()], fetchedAt, expiresAt: new Date(expires).toISOString(), complete, warning,
|
||||
source: { name: connection.name, baseUrl: connection.baseUrl, protocol: connection.protocol }, limits: { connections: 100, poolMembers: 32, catalog: 1000 } };
|
||||
this.previews.set(id, { preview, connection, binding, explicitKey, expires });
|
||||
return preview;
|
||||
} finally { this.active.delete(id); }
|
||||
}
|
||||
apply(input) {
|
||||
this.prune(); const value = this.previews.get(input.previewId);
|
||||
if (!value) throw fail('模型目录预览已取消、过期或宿主已重启,请重新获取。', 409);
|
||||
this.current(value);
|
||||
if (!Array.isArray(input.models) || !input.models.length || input.models.length > 100 || new Set(input.models).size !== input.models.length) throw fail('请选择1–100个不重复的上游模型。');
|
||||
const selected = input.models.map(id => value.preview.entries.find(model => model.id === id));
|
||||
if (selected.some(model => !model)) throw fail('所选模型不属于本次上游目录。');
|
||||
if (selected.some(model => model.lifecycle === 'retired')) throw fail('已退役型号不能新增使用,请选择其它模型。');
|
||||
if (selected.some(model => model.contextLimit && model.contextLimit < 1000 || model.maxOutputTokens && model.maxOutputTokens < 256)) throw fail('所选型号的上游容量低于当前接入下限(上下文1000 / 输出256),请选择其它型号。');
|
||||
const declarations = capabilityDeclarations(input.declarations, { text: 'unknown', tools: 'unknown', streaming: 'unknown', vision: 'unknown', audio: 'unknown' });
|
||||
if (!['keep', 'manual', 'auto'].includes(input.routing ?? 'keep')) throw fail('请选择保留当前选择、手动指定或自动适配。');
|
||||
if (input.routing === 'manual' && !input.models.includes(input.defaultModel)) throw fail('请在勾选的模型中指定默认型号。');
|
||||
if (input.routing === 'auto' && input.models.length > 32) throw fail('单个自动模型池最多32个成员;可减少本次选择或分别建立模型池。');
|
||||
if (input.persistKey === true && !value.explicitKey) throw fail('本机加密保存需要本次新填写的 Key;已有凭据沿用原保存状态。');
|
||||
const hub = this.hub;
|
||||
const result = hub.editRegistry(() => {
|
||||
let owner = value.binding ? hub.credentialOwner(hub.connection(value.binding.id)) : null;
|
||||
if (value.binding && !owner) throw fail('原凭据来源已变化,请核对源连接。', 409);
|
||||
const ids = [], added = [], existing = [];
|
||||
for (const model of selected) {
|
||||
// A catalog refresh never overwrites a user's edited parameters or
|
||||
// capability declarations. Existing members remain explicit conflicts.
|
||||
const prior = owner && hub.state.connections.find(connection => connection.model === model.id && credentialScope(connection) === credentialScope(value.connection)
|
||||
&& (connection.id === owner?.id || connection.credentialSource?.connectionId === owner?.id));
|
||||
if (prior) { ids.push(prior.id); existing.push(prior.id); continue; }
|
||||
const upstreamModel = { ...model, fetchedAt: value.preview.fetchedAt, endpoint: value.connection.baseUrl };
|
||||
const profile = hub.upsert({ name: value.connection.name + ' · ' + model.id, model: model.id,
|
||||
protocol: value.connection.protocol, baseUrl: value.connection.baseUrl, brand: value.connection.brand, messagesAuth: value.connection.messagesAuth,
|
||||
requiresKey: value.connection.requiresKey, concurrency: value.connection.concurrency, parameterProfile: value.connection.parameterProfile ?? 'auto',
|
||||
capabilityDeclarations: Object.fromEntries(Object.entries(model.capabilityDeclarations).map(([key, state]) => [key, state === 'unknown' ? declarations[key] : state])), streaming: false,
|
||||
contextLimit: Math.min(value.connection.contextLimit ?? 16000, model.contextLimit ?? 200000),
|
||||
maxOutputTokens: Math.min(value.connection.maxOutputTokens ?? 2048, model.maxOutputTokens ?? 16000),
|
||||
...(!owner && value.explicitKey ? { apiKey: value.explicitKey, persistKey: input.persistKey === true } : {}) });
|
||||
const connection = hub.connection(profile.id); connection.upstreamModel = upstreamModel; connection.source = 'upstream-catalog';
|
||||
if (!owner) owner = connection;
|
||||
else connection.credentialSource = { connectionId: owner.id, scope: credentialScope(owner) };
|
||||
hub.save(); ids.push(connection.id); added.push(connection.id);
|
||||
}
|
||||
let selection = null;
|
||||
if (input.routing === 'manual') selection = { type: 'connection', id: ids[input.models.indexOf(input.defaultModel)] };
|
||||
if (input.routing === 'auto') {
|
||||
const pool = hub.pool({ name: value.connection.name + ' · 自动适配', members: ids.map(connectionId => ({ connectionId })), strategy: 'round-robin', sticky: true });
|
||||
selection = { type: 'pool', id: pool.id };
|
||||
}
|
||||
if (selection) hub.state.selected = selection;
|
||||
return { added, existing, selected: hub.state.selected };
|
||||
}, input.routing && input.routing !== 'keep' ? { mode: 'compatible' } : null);
|
||||
this.previews.delete(input.previewId);
|
||||
return { ...result, center: hub.view() };
|
||||
}
|
||||
}
|
||||
@@ -9,8 +9,10 @@ import { messagesAuthSettings, validateProtocolEndpoint, modelRequestUrl } from
|
||||
import { modelWaitSettings } from './request-deadline.mjs';
|
||||
import { sanitizeModelText } from './upstream-error.mjs';
|
||||
import { nativeParameterSettings } from '../../shared/contracts/native-parameters.mjs';
|
||||
import { assertModelParameterSupport } from '../../shared/contracts/model-parameter-profiles.mjs';
|
||||
import { modelIdentitySettings } from '../../shared/contracts/model-identity.mjs';
|
||||
import { NetworkDiagnostics } from './network-diagnostics.mjs';
|
||||
import { ModelCatalog, credentialScope } from './model-catalog.mjs';
|
||||
|
||||
const legacyId = 'legacy-default';
|
||||
const stamp = () => new Date().toISOString();
|
||||
@@ -54,13 +56,18 @@ export class ModelHub {
|
||||
throw cause;
|
||||
}
|
||||
}
|
||||
editRegistry(action) {
|
||||
editRegistry(action, config = null) {
|
||||
if (this.editingRegistry) return action();
|
||||
const before = structuredClone(this.state), credentials = new Map(this.provider.credentials);
|
||||
const originalKey = this.provider.apiKey, legacyRevision = this.provider.legacyCredentialRevision;
|
||||
const vaultIssues = this.credentialVault ? new Map(this.credentialVault.issues) : null;
|
||||
this.editingRegistry = true;
|
||||
try {
|
||||
if (config) {
|
||||
let result;
|
||||
this.store.saveConfig(config, { beforeSave: () => { result = action(); this.store.storage.setMeta('modelRegistry', this.state); } });
|
||||
return result;
|
||||
}
|
||||
return this.store.storage.transaction(() => {
|
||||
const result = action();
|
||||
this.store.storage.setMeta('modelRegistry', this.state);
|
||||
@@ -84,10 +91,20 @@ export class ModelHub {
|
||||
...(this.store.config.parameterProfile ? { parameterProfile: this.store.config.parameterProfile } : {}),
|
||||
...(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); }
|
||||
credentialOwner(connection) {
|
||||
if (!connection.credentialSource) return connection;
|
||||
const source = this.connections().find(value => value.id === connection.credentialSource.connectionId);
|
||||
return source && !source.credentialSource && source.enabled && credentialScope(source) === connection.credentialSource.scope
|
||||
&& credentialScope(connection) === connection.credentialSource.scope ? source : null;
|
||||
}
|
||||
credential(connection) {
|
||||
const owner = this.credentialOwner(connection);
|
||||
return !owner ? undefined : owner.id === legacyId ? { key: this.provider.apiKey, revision: this.provider.legacyCredentialRevision ?? 1 } : this.provider.credentials.get(owner.id);
|
||||
}
|
||||
vault() { return this.credentialVault ??= new CredentialVault(this.store, this.provider); }
|
||||
manageCredential(input) {
|
||||
const connection = this.connection(input.id);
|
||||
if (connection.credentialSource) throw error('该模型共用上游凭据,请在源连接管理凭据。');
|
||||
if (input.revision !== connection.revision) throw error('模型连接已修改,请刷新凭据保存页面。', 409);
|
||||
return this.editRegistry(() => {
|
||||
const vault = this.vault(), previous = vault.state.entries[connection.id];
|
||||
@@ -110,10 +127,11 @@ export class ModelHub {
|
||||
this.save(); return { credential: vault.status(connection), center: this.view(), config: { ...this.store.config, hasKey: this.provider.hasKey() } };
|
||||
});
|
||||
}
|
||||
identity(connection) { return connection.id + ':' + connection.revision + ':' + (this.credential(connection)?.revision ?? 0); }
|
||||
identity(connection) { return connection.id + ':' + connection.revision + ':' + (this.credential(connection)?.revision ?? 0)
|
||||
+ (connection.credentialSource ? ':' + (this.credentialOwner(connection)?.revision ?? 'unavailable') : ''); }
|
||||
status(connection) {
|
||||
const credential = this.credential(connection), health = this.health.get(this.identity(connection));
|
||||
const ready = connection.enabled && (!connection.requiresKey || !!credential?.key);
|
||||
const ready = connection.enabled && (!connection.credentialSource || !!this.credentialOwner(connection)) && (!connection.requiresKey || !!credential?.key);
|
||||
return { ...connection, hasKey: !!credential?.key, credentialRevision: credential?.revision ?? 0, credentialStorage: 'memory',
|
||||
...(this.credentialVault ? { savedCredential: this.credentialVault.status(connection) } : {}),
|
||||
status: !connection.enabled ? 'disabled' : !ready ? 'needs_key' : health?.blocked ? 'needs_attention' : health?.cooldownUntil > Date.now() ? 'cooling' : 'configured',
|
||||
@@ -132,13 +150,15 @@ export class ModelHub {
|
||||
return { ...selected, name: name ?? '原模型连接' };
|
||||
}
|
||||
networkDiagnostics() { return this.networkChecks ??= new NetworkDiagnostics(this); }
|
||||
modelCatalog() { return this.catalog ??= new ModelCatalog(this); }
|
||||
connection(id) { const value = this.connections().find(item => item.id === id); if (!value) throw error('模型连接不存在。', 404); return value; }
|
||||
async probe(id, scope = 'text') {
|
||||
if (!['text', 'tools', 'streaming', 'vision'].includes(scope)) throw error('请选择文本、工具调用、流式或图像检测。');
|
||||
if (this.probing) throw error('已有模型能力检测正在执行,请等待结束。', 409);
|
||||
this.probing = true;
|
||||
try {
|
||||
const connection = { ...this.connection(id) }, member = { ...connection, credentialRevision: this.credential(connection)?.revision ?? 0 };
|
||||
const connection = { ...this.connection(id) }, member = { ...connection, credentialRevision: this.credential(connection)?.revision ?? 0,
|
||||
...(connection.credentialSource ? { credentialSourceRevision: this.credentialOwner(connection)?.revision ?? null } : {}) };
|
||||
if (!this.available(member)) throw error('该连接不可用,请核对凭据、冷却或停用状态。');
|
||||
member.streaming = scope === 'streaming';
|
||||
const nonce = randomUUID(), tools = scope === 'tools' ? [{ type: 'function', function: { name: 'workbench_capability_echo',
|
||||
@@ -161,7 +181,8 @@ export class ModelHub {
|
||||
if (calls?.length !== 1 || calls[0].function.name !== 'workbench_capability_echo' || JSON.parse(calls[0].function.arguments).nonce !== nonce) throw error('服务没有返回要求的原工具调用;未标为已验证。');
|
||||
} else if (!result.message.content.trim() || result.message.tool_calls?.length) throw error('服务没有返回纯文本响应;未标为已验证。');
|
||||
if(visual&&result.message.content.trim().toUpperCase().replace(/\s/g,'')!==visual.expected)throw error('服务未正确识别合成图像的四色顺序,原请求保留,未标为视觉已验证。');
|
||||
if (this.connection(id).revision !== connection.revision || (this.credential(connection)?.revision ?? 0) !== member.credentialRevision) throw error('检测期间连接已变化,原结果保留但不更新能力结论。', 409);
|
||||
if (this.connection(id).revision !== connection.revision || (this.credential(connection)?.revision ?? 0) !== member.credentialRevision
|
||||
|| connection.credentialSource && this.credentialOwner(connection)?.revision !== member.credentialSourceRevision) throw error('检测期间连接已变化,原结果保留但不更新能力结论。', 409);
|
||||
this.editRegistry(() => {
|
||||
this.state.capabilityResults ??= {};
|
||||
const previous = this.state.capabilityResults[id];
|
||||
@@ -200,12 +221,19 @@ export class ModelHub {
|
||||
concurrency: bounded(input.concurrency, 1, 1, 8), revision: (previous?.revision ?? 0) + 1,
|
||||
source: previous?.source ?? 'manual', ...(previous?.importOrigin ? { importOrigin: previous.importOrigin, importRevision: previous.importRevision } : {}),
|
||||
createdAt: previous?.createdAt ?? stamp(), updatedAt: stamp() };
|
||||
if (previous?.credentialSource && !Object.hasOwn(input, 'apiKey')) {
|
||||
if (credentialScope(previous) !== credentialScope(connection)) throw error('该模型共用上游凭据;改变地址或鉴权前,请新建上游或显式填写独立 Key。');
|
||||
connection.credentialSource = previous.credentialSource;
|
||||
}
|
||||
if (previous?.upstreamModel && previous.model === connection.model && credentialScope(previous) === credentialScope(connection)) connection.upstreamModel = previous.upstreamModel;
|
||||
assertModelParameterSupport(connection, connection.nativeParameters ?? {});
|
||||
if (Object.hasOwn(input, 'apiKey')) {
|
||||
if (typeof input.apiKey !== 'string' || input.apiKey.length > 16000) throw error('模型凭据格式无效。');
|
||||
}
|
||||
this.state.connections = [...this.state.connections.filter(item => item.id !== connection.id), connection];
|
||||
this.save();
|
||||
if (Object.hasOwn(input, 'apiKey')) this.provider.credentials.set(connection.id, { key: input.apiKey.trim(), revision: (this.provider.credentials.get(connection.id)?.revision ?? 0) + 1 });
|
||||
if (Object.hasOwn(input, 'apiKey')) this.provider.credentials.set(connection.id, { key: input.apiKey.trim(),
|
||||
revision: Math.max(this.credential(previous ?? connection)?.revision ?? 0, this.provider.credentials.get(connection.id)?.revision ?? 0) + 1 });
|
||||
if (input.persistKey === true) {
|
||||
const vault = this.vault(), record = vault.prepare(connection, input.apiKey, this.credential(connection)?.revision ?? 1);
|
||||
vault.stage(connection.id, record);
|
||||
@@ -253,10 +281,12 @@ export class ModelHub {
|
||||
}
|
||||
prepare(config, selection = this.state.selected) {
|
||||
if (config.mode === 'demo' || selection.type === 'legacy') return config;
|
||||
if (this.catalog?.active.size) throw error('正在获取上游模型目录,请等待完成或取消。', 409);
|
||||
const pool = selection.type === 'pool' ? this.state.pools.find(value => value.id === selection.id) : null;
|
||||
if (selection.type === 'pool' && !pool) throw error('选中的模型池不存在。');
|
||||
const members = (pool?.members ?? [{ connectionId: selection.id, weight: 1 }]).map(value => ({ ...this.connection(value.connectionId), capabilityEvidence: this.state.capabilityResults?.[value.connectionId], weight: value.weight,
|
||||
credentialRevision: this.credential(this.connection(value.connectionId))?.revision ?? 0 }));
|
||||
credentialRevision: this.credential(this.connection(value.connectionId))?.revision ?? 0,
|
||||
...(this.connection(value.connectionId).credentialSource ? { credentialSourceRevision: this.credentialOwner(this.connection(value.connectionId))?.revision ?? null } : {}) }));
|
||||
if (!members.some(value => this.status(value).status === 'configured' || this.status(value).status === 'cooling')) throw error('没有可用模型连接,请核对启用状态、临时凭据和故障。');
|
||||
return { ...config, ...messagesAuthSettings(members[0].protocol, members[0].messagesAuth), 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, parameterProfile: members[0].parameterProfile,
|
||||
@@ -268,6 +298,7 @@ export class ModelHub {
|
||||
available(member) {
|
||||
const current = this.connections().find(value => value.id === member.id);
|
||||
if (!current?.enabled || current.revision !== member.revision || (this.credential(current)?.revision ?? 0) !== member.credentialRevision) return false;
|
||||
if (current.credentialSource && (!this.credentialOwner(current) || member.credentialSourceRevision !== undefined && this.credentialOwner(current).revision !== member.credentialSourceRevision)) return false;
|
||||
const health = this.health.get(this.identity(member));
|
||||
return (!member.requiresKey || !!this.credential(member)?.key) && !health?.blocked && !(health?.cooldownUntil > Date.now());
|
||||
}
|
||||
|
||||
@@ -201,6 +201,7 @@ return async (request, response) => {
|
||||
if (method === 'GET' && route === '/api/model-center/imports') return json(response, importer().discover());
|
||||
if (method === 'POST') {
|
||||
const input = await body(request);
|
||||
if (route === '/api/model-center/catalog/cancel') return json(response, hub.modelCatalog().cancel(input.id));
|
||||
if (route === '/api/model-center/toggle') return json(response, hub.toggle(input.id, input.enabled));
|
||||
if (runtime.hasActivity() || hub.networkChecks?.active.size) throw Object.assign(new Error('请等待正在运行的任务或连接诊断结束,再修改或测试模型配置。'), { status: 409 });
|
||||
if (route === '/api/model-center/runtime-budgets') {
|
||||
@@ -219,6 +220,8 @@ return async (request, response) => {
|
||||
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/catalog') return json(response, await hub.modelCatalog().fetch(input));
|
||||
if (route === '/api/model-center/catalog/apply') return json(response, hub.modelCatalog().apply(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, { mode: 'compatible' }), config: { ...store.config, hasKey: provider.hasKey() } });
|
||||
if (route === '/api/model-center/import-preview') return json(response, importer().preview(input.source, String(input.path ?? '')));
|
||||
|
||||
@@ -5,6 +5,7 @@ import { Runtime } from './runtime.mjs';
|
||||
import { Store, atomicJson, readJson } from './store.mjs';
|
||||
import { ArtifactStore } from '../core/storage/artifact-store.mjs';
|
||||
import { ModelHub } from '../core/models/model-hub.mjs';
|
||||
import { ModelCatalog } from '../core/models/model-catalog.mjs';
|
||||
import { ConfigImporter } from '../core/models/config-import.mjs';
|
||||
import { createRequestHandler, createSessionPublisher } from './app.mjs';
|
||||
import { APP_VERSION } from './diagnostics.mjs';
|
||||
@@ -24,6 +25,7 @@ export function installUpgrade(context, { appDirectory, updateServiceInfo = true
|
||||
if (runtime.browsing?.active.size || runtime.previews?.active.size || runtime.evaluations?.active.size || runtime.environments?.active.size || runtime.active.size || runtime.waiters.size || runtime.workers?.active.size || runtime.controlledEngines?.drivers.size || runtime.mcp?.active.size || runtime.checks?.active.size || runtime.deliveries?.active.size || runtime.changeSets?.projects.size || runtime.workspaces?.projects.size || runtime.workspaces?.pending.size || runtime.research?.active.size || runtime.engines?.active.size || runtime.browser?.active.size || runtime.taskPlans?.active.size) throw new Error('Wait for active evaluations, environments, tasks, workers, controlled engines, MCP operations, checks, Git deliveries, file batches, workspaces, research, engines, browsers, task plans and approvals to finish before upgrading.');
|
||||
if (provider.activeRequests || provider.hub?.waiting || [...(provider.hub?.leases.values() ?? [])].some(count => count > 0)) throw new Error('Wait for active model requests and probes before upgrading.');
|
||||
if (provider.hub?.networkChecks?.active.size) throw new Error('Wait for active connection diagnostics before upgrading.');
|
||||
if (provider.hub?.catalog?.active.size || provider.hub?.probing) throw new Error('Wait for active model catalogs and probes before upgrading.');
|
||||
const root = fs.realpathSync.native(path.resolve(appDirectory));
|
||||
if (!fs.existsSync(path.join(root, 'dist-client', 'index.html'))) throw new Error('The new client bundle is missing.');
|
||||
const oldHandlers = server.listeners('request');
|
||||
@@ -34,7 +36,8 @@ export function installUpgrade(context, { appDirectory, updateServiceInfo = true
|
||||
const oldModelPrototypes = { hub: provider.hub ? Object.getPrototypeOf(provider.hub) : null,
|
||||
importer: provider.hub?.importer ? Object.getPrototypeOf(provider.hub.importer) : null,
|
||||
vault: provider.hub?.credentialVault ? Object.getPrototypeOf(provider.hub.credentialVault) : null,
|
||||
network: provider.hub?.networkChecks ? Object.getPrototypeOf(provider.hub.networkChecks) : null };
|
||||
network: provider.hub?.networkChecks ? Object.getPrototypeOf(provider.hub.networkChecks) : null,
|
||||
catalog: provider.hub?.catalog ? Object.getPrototypeOf(provider.hub.catalog) : null };
|
||||
const oldStoreProperties = { ...store };
|
||||
const oldRuntimeProperties = { filePreviews: runtime.filePreviews, browsing: runtime.browsing, previews: runtime.previews, evaluations:runtime.evaluations, environments: runtime.environments, appDirectory: runtime.appDirectory, workers: runtime.workers, controlledEngines: runtime.controlledEngines, checks: runtime.checks, deliveries: runtime.deliveries, codeIndex: runtime.codeIndex, changeSets: runtime.changeSets, contextPlans: runtime.contextPlans, workspaces: runtime.workspaces, research: runtime.research, engines: runtime.engines, browser: runtime.browser, taskPlans: runtime.taskPlans, projectMemories: runtime.projectMemories, taskCheckpoints: runtime.taskCheckpoints, skills: runtime.skills, mcp: runtime.mcp };
|
||||
const oldUpdates = store.listeners('update');
|
||||
@@ -53,6 +56,7 @@ export function installUpgrade(context, { appDirectory, updateServiceInfo = true
|
||||
if (provider.hub.importer) Object.setPrototypeOf(provider.hub.importer, ConfigImporter.prototype);
|
||||
if (provider.hub.credentialVault) Object.setPrototypeOf(provider.hub.credentialVault, CredentialVault.prototype);
|
||||
if (provider.hub.networkChecks) Object.setPrototypeOf(provider.hub.networkChecks, NetworkDiagnostics.prototype);
|
||||
if (provider.hub.catalog) Object.setPrototypeOf(provider.hub.catalog, ModelCatalog.prototype);
|
||||
}
|
||||
runtime.filePreviews = null; runtime.appDirectory = root; runtime.previews = null; runtime.browsing = null; runtime.evaluations = null; runtime.environments = null; runtime.workers = null; runtime.controlledEngines = null; runtime.checks = null; runtime.deliveries = null; runtime.codeIndex = null; runtime.changeSets = null; runtime.contextPlans = null; runtime.workspaces = null; runtime.research = null; runtime.engines = null; runtime.browser = null; runtime.taskPlans = null; runtime.projectMemories = null; runtime.taskCheckpoints = null; runtime.skills = null; runtime.mcp = null;
|
||||
delete server.workbenchCompanion;
|
||||
@@ -98,6 +102,7 @@ export function installUpgrade(context, { appDirectory, updateServiceInfo = true
|
||||
if (oldModelPrototypes.importer) Object.setPrototypeOf(provider.hub.importer, oldModelPrototypes.importer);
|
||||
if (oldModelPrototypes.vault) Object.setPrototypeOf(provider.hub.credentialVault, oldModelPrototypes.vault);
|
||||
if (oldModelPrototypes.network) Object.setPrototypeOf(provider.hub.networkChecks, oldModelPrototypes.network);
|
||||
if (oldModelPrototypes.catalog) Object.setPrototypeOf(provider.hub.catalog, oldModelPrototypes.catalog);
|
||||
store.removeAllListeners('update'); for (const observer of oldUpdates) store.on('update', observer);
|
||||
server.removeAllListeners('request'); for (const previous of oldHandlers) server.on('request', previous);
|
||||
throw error;
|
||||
|
||||
@@ -130,6 +130,7 @@ export class Provider {
|
||||
}
|
||||
async abortRequests() {
|
||||
this.requestsClosing = true;
|
||||
this.hub?.catalog?.stopAll();
|
||||
await this.hub?.networkChecks?.stopAll();
|
||||
const operations = [...(this.requestControllers ?? [])];
|
||||
for (const operation of operations) operation.controller.abort(new Error('用户停止执行宿主。'));
|
||||
|
||||
@@ -137,7 +137,7 @@ export class Runtime {
|
||||
this.active = new Map(); this.waiters = new Map();
|
||||
}
|
||||
isActive(id) { return !!this.browsing?.isActive(id) || !!this.previews?.isActive(id) || !!this.environments?.isActive(id) || !!this.evaluations?.isActive(id) || this.active.has(id) || !!this.workers?.isActive(id) || !!this.controlledEngines?.isActive(id) || !!this.checks?.isActive(id) || !!this.deliveries?.isActive(id) || !!this.changeSets?.isActive(id) || !!this.workspaces?.isActive(id) || !!this.research?.isActive(id) || !!this.engines?.isActive(id) || !!this.browser?.isActive(id) || !!this.taskPlans?.isActive(id); }
|
||||
hasActivity() { return (this.provider.hub?.networkChecks?.active.size ?? 0) > 0 || (this.browsing?.active.size ?? 0) > 0 || (this.previews?.active.size ?? 0) > 0 || (this.environments?.active.size ?? 0) > 0 || (this.evaluations?.active.size ?? 0) > 0 || (this.provider.activeRequests ?? 0) > 0 || (this.provider.hub?.waiting ?? 0) > 0 || this.active.size > 0 || (this.workers?.active.size ?? 0) > 0 || (this.controlledEngines?.drivers.size ?? 0) > 0 || (this.mcp?.active.size ?? 0) > 0 || (this.checks?.active.size ?? 0) > 0 || (this.deliveries?.active.size ?? 0) > 0 || (this.changeSets?.projects.size ?? 0) > 0 || (this.workspaces?.projects.size ?? 0) > 0 || (this.workspaces?.pending.size ?? 0) > 0 || (this.research?.active.size ?? 0) > 0 || (this.engines?.active.size ?? 0) > 0 || (this.browser?.active.size ?? 0) > 0 || (this.taskPlans?.active.size ?? 0) > 0; }
|
||||
hasActivity() { return (this.provider.hub?.catalog?.active.size ?? 0) > 0 || (this.provider.hub?.networkChecks?.active.size ?? 0) > 0 || (this.browsing?.active.size ?? 0) > 0 || (this.previews?.active.size ?? 0) > 0 || (this.environments?.active.size ?? 0) > 0 || (this.evaluations?.active.size ?? 0) > 0 || (this.provider.activeRequests ?? 0) > 0 || (this.provider.hub?.waiting ?? 0) > 0 || this.active.size > 0 || (this.workers?.active.size ?? 0) > 0 || (this.controlledEngines?.drivers.size ?? 0) > 0 || (this.mcp?.active.size ?? 0) > 0 || (this.checks?.active.size ?? 0) > 0 || (this.deliveries?.active.size ?? 0) > 0 || (this.changeSets?.projects.size ?? 0) > 0 || (this.workspaces?.projects.size ?? 0) > 0 || (this.workspaces?.pending.size ?? 0) > 0 || (this.research?.active.size ?? 0) > 0 || (this.engines?.active.size ?? 0) > 0 || (this.browser?.active.size ?? 0) > 0 || (this.taskPlans?.active.size ?? 0) > 0; }
|
||||
evaluationManager() { return this.evaluations ??= new EvaluationManager(this.store, this); }
|
||||
workerManager() { return this.workers ??= new WorkerManager(this.store, this); }
|
||||
projectIndexer() { return this.codeIndex ??= new ProjectIndex({ cacheDirectory: path.join(this.store.directory, 'cache', 'project-index') }); }
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
export interface CatalogModel {
|
||||
id: string; name: string; owner: string; lifecycle: string | null; contextLimit: number | null; maxOutputTokens: number | null;
|
||||
capabilityDeclarations: Record<string, string>; features: Record<string, string>; efforts: string[]; modes: string[]; parameters: string[]; metadataSource: string;
|
||||
}
|
||||
export interface CatalogPreview {
|
||||
id: string; entries: CatalogModel[]; fetchedAt: string; expiresAt: string; complete: boolean; warning: string;
|
||||
source: { name: string; baseUrl: string; protocol: string }; limits: { connections: number; poolMembers: number; catalog: number };
|
||||
}
|
||||
export function catalogRoute(config: { protocol?: string; baseUrl: string }): string;
|
||||
export function catalogModel(item: unknown): CatalogModel;
|
||||
export function catalogPage(response: unknown): { entries: CatalogModel[]; hasMore: boolean; cursor: string };
|
||||
@@ -0,0 +1,54 @@
|
||||
import { modelProtocol } from './model-capabilities.mjs';
|
||||
|
||||
const fail = message => Object.assign(new Error(message), { status: 400 });
|
||||
const text = (value, max = 200) => typeof value === 'string' ? value.trim().slice(0, max) : '';
|
||||
const supported = value => typeof value === 'boolean' ? value ? 'supported' : 'unsupported'
|
||||
: typeof value?.supported === 'boolean' ? value.supported ? 'supported' : 'unsupported'
|
||||
: ['supported', 'unsupported', 'unknown'].includes(value) ? value : 'unknown';
|
||||
const positive = value => Number.isSafeInteger(value) && value > 0 ? value : null;
|
||||
|
||||
export function catalogRoute(config) {
|
||||
const protocol = modelProtocol(config.protocol);
|
||||
return protocol === 'anthropic-messages' && !/\/v1$/i.test(new URL(config.baseUrl).pathname.replace(/\/+$/, ''))
|
||||
? '/v1/models' : '/models';
|
||||
}
|
||||
|
||||
// Metadata is an upstream declaration, never capability-test evidence. Keep
|
||||
// unsupported adapter features visible without passing arbitrary vendor fields.
|
||||
export function catalogModel(item) {
|
||||
if (typeof item === 'string') item = { id: item };
|
||||
if (!item || typeof item !== 'object' || Array.isArray(item)) throw fail('上游模型目录含无效条目。');
|
||||
const id = text(item.id ?? item.name);
|
||||
if (!id || id.length !== String(item.id ?? item.name).trim().length || /[\x00-\x1f\x7f]/.test(id)) throw fail('上游模型名称为空、过长或含控制字符。');
|
||||
const caps = item.capabilities && typeof item.capabilities === 'object' && !Array.isArray(item.capabilities) ? item.capabilities : {};
|
||||
const declarations = Object.fromEntries(['text', 'tools', 'streaming', 'vision', 'audio'].map(key => [key,
|
||||
supported(caps[key] ?? (key === 'vision' ? caps.image_input : key === 'tools' ? caps.tool_calling ?? caps.function_calling : undefined))]));
|
||||
const input = item.input_modalities ?? item.architecture?.input_modalities;
|
||||
const output = item.output_modalities ?? item.architecture?.output_modalities;
|
||||
if (Array.isArray(input)) {
|
||||
if (input.includes('image') && declarations.vision === 'unknown') declarations.vision = 'supported';
|
||||
if (input.includes('audio') && declarations.audio === 'unknown') declarations.audio = 'supported';
|
||||
}
|
||||
if (Array.isArray(output) && output.includes('text') && declarations.text === 'unknown') declarations.text = 'supported';
|
||||
const parameters = Array.isArray(item.supported_parameters) ? [...new Set(item.supported_parameters.filter(value => typeof value === 'string' && value.length <= 80))].slice(0, 80) : [];
|
||||
if (declarations.tools === 'unknown' && parameters.includes('tools')) declarations.tools = 'supported';
|
||||
const efforts = ['minimal', 'low', 'medium', 'high', 'xhigh', 'max'].filter(value => supported(caps.effort?.[value]) === 'supported');
|
||||
const modes = ['disabled', 'enabled', 'adaptive'].filter(value => supported(caps.thinking?.types?.[value]) === 'supported');
|
||||
const features = Object.fromEntries(['thinking', 'effort', 'pdf_input', 'structured_outputs', 'server_tools', 'code_execution', 'batch', 'citations', 'context_management'].map(key => [key, supported(caps[key])]));
|
||||
return { id, name: text(item.display_name ?? item.name ?? id), owner: text(item.owned_by ?? item.provider, 100),
|
||||
lifecycle: ['active', 'deprecated', 'retired'].includes(item.lifecycle) ? item.lifecycle : null,
|
||||
contextLimit: positive(item.context_length ?? item.max_input_tokens ?? item.context_window),
|
||||
maxOutputTokens: positive(item.max_output_tokens ?? item.max_tokens ?? item.top_provider?.max_completion_tokens),
|
||||
capabilityDeclarations: declarations, features, efforts, modes, parameters,
|
||||
metadataSource: 'upstream-model-list' };
|
||||
}
|
||||
|
||||
export function catalogPage(response) {
|
||||
const data = response?.data ?? response?.models;
|
||||
if (!Array.isArray(data)) throw fail('上游未返回 data 或 models 模型列表;可核对接口或使用手动兼容入口。');
|
||||
const entries = data.map(catalogModel);
|
||||
const hasMore = response.has_more === true;
|
||||
const cursor = hasMore ? text(response.last_id ?? entries.at(-1)?.id) : '';
|
||||
if (hasMore && !cursor) throw fail('上游声明还有下一页,却未提供有效游标。');
|
||||
return { entries, hasMore, cursor };
|
||||
}
|
||||
@@ -1,12 +1,14 @@
|
||||
export interface ParameterConnection {
|
||||
protocol?: string; parameterProfile?: string; brand?: string; model?: string; baseUrl?: string;
|
||||
nativeParameters?: Record<string, unknown>; capabilities?: Record<string, string>; capabilityDeclarations?: Record<string, string>;
|
||||
upstreamModel?: import('./model-catalog.mjs').CatalogModel;
|
||||
}
|
||||
export const reasoningEfforts: string[];
|
||||
export const messagesEfforts: string[];
|
||||
export function parameterProfiles(protocol: string): { id: string; label: string }[];
|
||||
export function parameterProfileSettings(protocol: string, value?: string): string;
|
||||
export function modelParameterProfile(connection: ParameterConnection): { id: string; label: string; source: string; efforts: string[]; thinking: boolean };
|
||||
export function modelParameterProfile(connection: ParameterConnection): { id: string; label: string; source: string; efforts: string[]; thinking: boolean; modes: string[] };
|
||||
export function assertModelParameterSupport(connection: ParameterConnection, parameters: Record<string, unknown>): void;
|
||||
export function reasoningSetting(connection: ParameterConnection, parameters: Record<string, unknown>): { mode: string; effort: string; budget: number };
|
||||
export function changeReasoningSetting(connection: ParameterConnection, parameters: Record<string, unknown>, field: string, value: string | number): Record<string, unknown>;
|
||||
export function modelFeatureRows(connection: ParameterConnection): { name: string; upstream: string; local: string }[];
|
||||
|
||||
@@ -20,11 +20,25 @@ export function modelParameterProfile(connection) {
|
||||
const deepseek = connection.brand === 'DeepSeek' || /^deepseek(?:-|$)/i.test(connection.model ?? '')
|
||||
|| /^https:\/\/api\.deepseek\.com(?:\/|$)/i.test(connection.baseUrl ?? '') || !!connection.nativeParameters?.thinking && protocol === 'chat-completions';
|
||||
const id = selected !== 'auto' ? selected : deepseek ? 'deepseek' : protocol === 'anthropic-messages' ? 'anthropic' : 'openai';
|
||||
const metadata = connection.upstreamModel;
|
||||
const choices = id === 'deepseek' ? ['low', 'high', 'max'] : protocol === 'anthropic-messages' ? messagesEfforts : reasoningEfforts.filter(value => value !== 'none');
|
||||
return { id, label: labels[id], source: selected === 'auto' ? '按品牌、型号与协议匹配,可手动更改' : '用户选择的兼容格式',
|
||||
efforts: id === 'deepseek' ? ['low', 'high', 'max'] : protocol === 'anthropic-messages' ? messagesEfforts : reasoningEfforts.filter(value => value !== 'none'),
|
||||
efforts: metadata?.features?.effort === 'unsupported' ? [] : metadata?.efforts?.length ? choices.filter(value => metadata.efforts.includes(value)) : choices,
|
||||
modes: metadata?.features?.thinking === 'unsupported' ? [] : metadata?.modes?.length ? metadata.modes : ['disabled', 'enabled', ...(protocol === 'anthropic-messages' ? ['adaptive'] : [])],
|
||||
thinking: protocol === 'anthropic-messages' || id === 'deepseek' && protocol === 'chat-completions' };
|
||||
}
|
||||
|
||||
export function assertModelParameterSupport(connection, parameters) {
|
||||
const metadata = connection.upstreamModel;
|
||||
if (!metadata) return;
|
||||
const effort = parameters.reasoning_effort ?? parameters.reasoning?.effort ?? parameters.output_config?.effort;
|
||||
const mode = parameters.thinking?.type;
|
||||
if (effort && (metadata.features?.effort === 'unsupported' || effort !== 'none' && metadata.efforts?.length && !metadata.efforts.includes(effort)))
|
||||
throw Object.assign(new Error('上游目录声明该型号不支持所选推理强度,请选择支持的档位或保留默认。'), { status: 400 });
|
||||
if (mode && (metadata.features?.thinking === 'unsupported' || metadata.modes?.length && !metadata.modes.includes(mode)))
|
||||
throw Object.assign(new Error('上游目录声明该型号不支持所选推理模式,请选择支持的模式或保留默认。'), { status: 400 });
|
||||
}
|
||||
|
||||
export function reasoningSetting(connection, parameters) {
|
||||
const profile = modelParameterProfile(connection), protocol = connection.protocol;
|
||||
const effort = protocol === 'anthropic-messages' ? parameters.output_config?.effort : protocol === 'responses' ? parameters.reasoning?.effort : parameters.reasoning_effort;
|
||||
@@ -49,7 +63,7 @@ export function changeReasoningSetting(connection, parameters, field, value) {
|
||||
if (value === 'disabled') setEffort('');
|
||||
} else {
|
||||
delete next.thinking;
|
||||
setEffort(value === 'disabled' ? 'none' : reasoningSetting(connection, parameters).effort || 'medium');
|
||||
setEffort(value === 'disabled' ? 'none' : reasoningSetting(connection, parameters).effort || (profile.efforts.includes('medium') ? 'medium' : profile.efforts[0] ?? ''));
|
||||
}
|
||||
}
|
||||
return next;
|
||||
@@ -62,8 +76,9 @@ export function modelFeatureRows(connection) {
|
||||
{ name: '文本 / 工具调用', upstream: declared('text') + ' / ' + declared('tools'), local: '已接入,可单独检测' },
|
||||
{ name: '流式输出', upstream: declared('streaming'), local: '已接入,按连接开关发送' },
|
||||
{ name: '图像输入', upstream: declared('vision'), local: '已接入 PNG,可单独检测' },
|
||||
{ name: '推理模式 / 强度', upstream: '依型号与兼容服务支持范围', local: '按配置档发送,受原输出预算约束' },
|
||||
{ name: '推理模式 / 强度', upstream: connection.upstreamModel ? `${states[connection.upstreamModel.features?.thinking ?? 'unknown']} / ${connection.upstreamModel.efforts?.join('、') || states[connection.upstreamModel.features?.effort ?? 'unknown']}` : '依型号与兼容服务支持范围', local: '按配置档发送,受原输出预算约束' },
|
||||
{ name: '语音 / 视频 / 服务端工具', upstream: '需供应商提供相应协议', local: '尚未接入' },
|
||||
{ name: '结构化输出约束', upstream: '依型号与兼容服务支持范围', local: '尚未接入,参数保存时明确拒绝' },
|
||||
{ name: '结构化输出约束', upstream: states[connection.upstreamModel?.features?.structured_outputs ?? 'unknown'], local: '尚未接入,参数保存时明确拒绝' },
|
||||
...(connection.upstreamModel ? [{ name: 'PDF / 服务端工具 / 批处理', upstream: ['pdf_input', 'server_tools', 'batch'].map(key => states[connection.upstreamModel.features?.[key] ?? 'unknown']).join(' / '), local: '目录声明已同步,执行能力尚未接入' }] : []),
|
||||
];
|
||||
}
|
||||
|
||||
+2
-1
@@ -12,7 +12,8 @@
|
||||
6. [complete] 用户指定Z两路线完成:Claude手误型号mismatch,DeepSeek预检通过但新Node第5请求2048输出全用于reasoning后length失败,0提案/交付;151份证据离线通过,状态恢复、原失败和回顾保持。
|
||||
7. [complete] 用户更正Claude后Z2返回GPT而停止;用户明确再试后Z3预检matched、Node首请求6692输入/0输出空内容,verification_incomplete暂停。两样本均封存并离线通过,原状态恢复、凭据3保持,不续跑。
|
||||
8. [complete] 推理开关/强度、品牌/自定义兼容配置档及导入同步、DeepSeek推理工具历史与Messages空响应诊断完成,3ab3a6e4/f1242ee8。85/85相关+最终8/8专项、网页/实际桌面各6组和旧参数6/Messages7组通过,回顾见docs/QA-REASONING-CONTROLS.md;真实模型0,旧失败保持。
|
||||
9. [in_progress] 1.25.3独立发行:版本/守卫/安装验收同步,完整源码检查、不可变候选、源码ZIP独立重建、三归档/便携/实际安装、保留原PID36552/Provider正式升级及网页/原生回看、原状态/证据封存/回顾/本地提交。完成前正式仍1.25.2,不声明真实模型交付或Purge已验证。
|
||||
9. [complete] 用户新增上游目录/多模型选择/手动与自动适配已实现,复用原模型池和凭据引用,目录声明同步但不冒充验证。68/68相关、4/4热升级补验、构建通过;网页/实际Electron各8组13目录GET通过,原推理各6组重验。旧109e完整源码因范围扩大在264完成项停止,原日志不覆盖。见docs/MODEL-CATALOG.md。
|
||||
10. [in_progress] 1.25.3扩大范围后独立发行:版本/守卫/安装验收同步,重新冻结完整源码检查、不可变候选、源码ZIP独立重建、三归档/便携/实际安装(原75+推理12+目录16)、保留原PID36552/Provider正式升级及网页/原生回看、原状态/证据封存/回顾/本地提交。完成前正式仍1.25.2,不声明真实模型交付或Purge已验证。
|
||||
|
||||
细节和阶段回顾见docs/RELIABILITY-1.25.2.md。开始时session-catchup无待同步输出;计划文件首次补丁因findings标题上下文不符被拒且无文件变化,已按原标题修正。以下按历史时点理解。
|
||||
|
||||
|
||||
@@ -0,0 +1,250 @@
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { randomUUID } from 'node:crypto';
|
||||
import { fixture, compatibleFixture, completion, until, launchWorkbench } from './helpers.mjs';
|
||||
import { Store } from '../src/server/store.mjs';
|
||||
import { Provider } from '../src/server/provider.mjs';
|
||||
import { catalogModel } from '../src/shared/contracts/model-catalog.mjs';
|
||||
import { modelSnapshot } from '../src/shared/contracts/execution.mjs';
|
||||
import { modelParameterProfile } from '../src/shared/contracts/model-parameter-profiles.mjs';
|
||||
|
||||
const create = f => { const provider = new Provider(); provider.setKey(''); return { provider, hub: provider.modelHub(f.store) }; };
|
||||
const draft = server => ({ name: '自有上游', protocol: 'chat-completions', baseUrl: server.baseUrl, requiresKey: true });
|
||||
const metadata = id => ({ id, capabilities: { text: true, tools: true, streaming: true, image_input: { supported: true },
|
||||
thinking: { supported: true, types: { adaptive: { supported: true }, enabled: { supported: false }, disabled: { supported: true } } },
|
||||
effort: { supported: true, low: { supported: true }, high: { supported: true }, max: { supported: false } }, structured_outputs: { supported: true } }, max_input_tokens: 200000, max_tokens: 8192 });
|
||||
|
||||
test('upstream declarations include brand metadata and remain distinct from adapter support or verification', () => {
|
||||
const value = catalogModel(metadata('claude-owned'));
|
||||
assert.equal(value.capabilityDeclarations.vision, 'supported'); assert.equal(value.capabilityDeclarations.audio, 'unknown');
|
||||
assert.deepEqual(value.efforts, ['low', 'high']); assert.deepEqual(value.modes, ['disabled', 'adaptive']);
|
||||
assert.equal(value.features.structured_outputs, 'supported');
|
||||
assert.equal(catalogModel({ id: 'only-id' }).capabilityDeclarations.tools, 'unknown');
|
||||
assert.equal(catalogModel({ id: 'embed', capabilities: { tools: false }, supported_parameters: ['tools'] }).capabilityDeclarations.tools, 'unsupported');
|
||||
assert.equal(catalogModel({ id: 'multi', architecture: { input_modalities: ['image'], output_modalities: ['text'] } }).capabilityDeclarations.text, 'supported');
|
||||
for (const id of ['', 'x'.repeat(201), 'x\ny']) assert.throws(() => catalogModel({ id }), /名称/);
|
||||
});
|
||||
|
||||
test('new upstream retrieves more than 100 models without a manual model; choosing several stores one key and frozen metadata', async t => {
|
||||
const f = fixture(t), server = await compatibleFixture(t, () => ({ data: Array.from({ length: 180 }, (_, i) => metadata('owned-' + i)) }));
|
||||
const { provider, hub } = create(f), preview = await hub.modelCatalog().fetch({ draft: draft(server), apiKey: 'owned-catalog-secret' });
|
||||
assert.equal(preview.entries.length, 180); assert.equal(preview.complete, true); assert.equal(server.requests.length, 1);
|
||||
assert.equal(server.requests[0].input, null); assert.equal(server.requests[0].url, '/v1/models');
|
||||
assert.equal(server.requests[0].headers.authorization, 'Bearer owned-catalog-secret');
|
||||
assert.equal(hub.state.connections.length, 0); assert.equal(hub.attempts.length, 0);
|
||||
const result = hub.modelCatalog().apply({ previewId: preview.id, models: ['owned-3', 'owned-7', 'owned-150'], routing: 'manual', defaultModel: 'owned-7' });
|
||||
const [source, child] = result.added.map(id => hub.connection(id));
|
||||
assert.equal(provider.credentials.size, 1); assert.equal(hub.credential(source), hub.credential(child));
|
||||
assert.equal(child.credentialSource.connectionId, source.id); assert.equal(child.upstreamModel.id, 'owned-7');
|
||||
assert.equal(child.contextLimit, 16000); assert.equal(child.maxOutputTokens, 2048);
|
||||
assert.equal(hub.status(child).capabilities.vision, 'protocol_declared'); assert.equal(hub.status(child).capabilityEvidence, undefined);
|
||||
assert.equal(f.store.config.mode, 'compatible'); assert.equal(hub.state.selected.id, child.id);
|
||||
const prepared = hub.prepare(f.store.config), snapshot = modelSnapshot(prepared);
|
||||
assert.equal(snapshot.routing.members[0].upstreamModel.id, 'owned-7');
|
||||
assert.equal(JSON.stringify(hub.state).includes('owned-catalog-secret'), false);
|
||||
assert.equal(JSON.stringify(hub.view()).includes('owned-catalog-secret'), false);
|
||||
assert.throws(() => hub.modelCatalog().apply({ previewId: preview.id, models: ['owned-3'] }), /过期/);
|
||||
});
|
||||
|
||||
test('Responses and DeepSeek list use bearer; Messages root and v1 endpoints paginate with selected authentication', async t => {
|
||||
const f = fixture(t), server = await compatibleFixture(t, (url) => url.includes('after_id=') ? { data: [metadata('owned-b')], has_more: false } : { data: [metadata('owned-a')], has_more: true, last_id: 'owned-a' });
|
||||
const { hub } = create(f);
|
||||
for (const auth of ['api-key', 'bearer']) for (const baseUrl of [server.baseUrl, server.baseUrl.replace(/\/v1$/, '')]) {
|
||||
const preview = await hub.modelCatalog().fetch({ draft: { ...draft(server), baseUrl, protocol: 'anthropic-messages', messagesAuth: auth }, apiKey: 'owned-msg' });
|
||||
assert.deepEqual(preview.entries.map(value => value.id), ['owned-a', 'owned-b']);
|
||||
const last = server.requests.at(-1); assert.equal(last.url, '/v1/models?after_id=owned-a&limit=100');
|
||||
assert.equal(last.headers['anthropic-version'], '2023-06-01');
|
||||
assert.equal(last.headers['x-api-key'], auth === 'api-key' ? 'owned-msg' : undefined);
|
||||
assert.equal(last.headers.authorization, auth === 'bearer' ? 'Bearer owned-msg' : undefined);
|
||||
hub.modelCatalog().cancel(preview.id);
|
||||
}
|
||||
});
|
||||
|
||||
test('refresh preserves existing key objects, encrypted block, parameters and user capability overrides without re-encryption', async t => {
|
||||
const f = fixture(t), server = await compatibleFixture(t, () => ({ data: [metadata('source'), metadata('child')] }));
|
||||
const { provider, hub } = create(f), source = hub.upsert({ ...draft(server), model: 'source', apiKey: 'owned-persist', persistKey: true });
|
||||
const credential = provider.credentials.get(source.id), block = structuredClone(f.store.storage.meta('modelCredentials'));
|
||||
const preview = await hub.modelCatalog().fetch({ id: source.id, revision: source.revision });
|
||||
const first = hub.modelCatalog().apply({ previewId: preview.id, models: ['source', 'child'] });
|
||||
assert.deepEqual(first.existing, [source.id]); const child = hub.connection(first.added[0]);
|
||||
hub.setNativeParameters({ id: child.id, revision: child.revision, nativeParameters: { reasoning_effort: 'high', seed: 3 } });
|
||||
const edited = hub.upsert({ ...hub.connection(child.id), name: '用户命名', capabilityDeclarations: { vision: 'unsupported' } });
|
||||
const refresh = await hub.modelCatalog().fetch({ id: source.id, revision: source.revision });
|
||||
const second = hub.modelCatalog().apply({ previewId: refresh.id, models: ['child'], routing: 'manual', defaultModel: 'child' });
|
||||
assert.deepEqual(second.added, []); assert.deepEqual(second.existing, [child.id]);
|
||||
assert.equal(hub.connection(child.id).name, '用户命名'); assert.deepEqual(hub.connection(child.id).nativeParameters, edited.nativeParameters);
|
||||
assert.equal(hub.connection(child.id).capabilityDeclarations.vision, 'unsupported');
|
||||
assert.equal(provider.credentials.get(source.id), credential); assert.equal(hub.credential(child), credential);
|
||||
assert.deepEqual(f.store.storage.meta('modelCredentials'), block); assert.equal(provider.credentials.size, 1);
|
||||
assert.throws(() => hub.manageCredential({ id: child.id, revision: edited.revision, action: 'forget' }), /源连接/);
|
||||
assert.equal(hub.vault().status(child).status, 'reference');
|
||||
assert.throws(() => hub.upsert({ ...edited, baseUrl: 'https://changed.test' }), /共用上游/);
|
||||
});
|
||||
|
||||
test('shared credentials restore from the original saved block; source disable, endpoint changes and key replacement cannot revive frozen runs', async t => {
|
||||
const f = fixture(t), server = await compatibleFixture(t, () => ({ data: [metadata('owned-a'), metadata('owned-b')] }));
|
||||
let { hub, provider } = create(f);
|
||||
const preview = await hub.modelCatalog().fetch({ draft: draft(server), apiKey: 'owned-original' });
|
||||
const result = hub.modelCatalog().apply({ previewId: preview.id, models: ['owned-a', 'owned-b'], persistKey: true, routing: 'manual', defaultModel: 'owned-b' });
|
||||
const [sourceId, childId] = result.added, old = modelSnapshot(hub.prepare(f.store.config));
|
||||
f.store.close(); const reopened = new Store(f.data); f.onCleanup(() => reopened.close()); provider = new Provider(); provider.setKey(''); hub = provider.modelHub(reopened);
|
||||
const source = hub.connection(sourceId), child = hub.connection(childId), credential = hub.credential(source);
|
||||
assert.equal(credential.key, 'owned-original'); assert.equal(hub.credential(child), credential); assert.equal(provider.credentials.size, 1);
|
||||
const member = hub.prepare(reopened.config).modelRouting.members[0]; assert.equal(hub.available(member), true);
|
||||
hub.toggle(sourceId, false); assert.equal(hub.available(member), false); assert.equal(hub.status(child).hasKey, false);
|
||||
hub.toggle(sourceId, true); assert.equal(hub.available(member), false, 'a source generation change invalidates the frozen member');
|
||||
let owner = hub.connection(sourceId); hub.upsert({ ...owner, baseUrl: 'https://elsewhere.test' });
|
||||
assert.equal(hub.credential(child), undefined); assert.equal(hub.available(member), false);
|
||||
owner = hub.connection(sourceId); hub.upsert({ ...owner, baseUrl: server.baseUrl, apiKey: 'owned-new' });
|
||||
assert.equal(hub.credential(child).revision, credential.revision + 1); assert.equal(hub.available(member), false);
|
||||
assert.equal(old.routing.members[0].credentialRevision, credential.revision); assert.equal(old.routing.members[0].upstreamModel.id, 'owned-b');
|
||||
});
|
||||
|
||||
test('automatic selection reuses capability and health filtering, manual selection sends the exact chosen model and shared auth', async t => {
|
||||
const f = fixture(t), server = await compatibleFixture(t, (_url, input) => input ? { ...completion([], 'OK'), model: input.model } : { data: [
|
||||
{ id: 'text-only', capabilities: { text: true, tools: false } }, metadata('tools-and-vision'),
|
||||
] });
|
||||
const { hub, provider } = create(f), preview = await hub.modelCatalog().fetch({ draft: draft(server), apiKey: 'owned-auto' });
|
||||
const result = hub.modelCatalog().apply({ previewId: preview.id, models: ['text-only', 'tools-and-vision'], routing: 'auto' });
|
||||
const config = hub.prepare(f.store.config), before = modelSnapshot(config), pool = hub.state.pools[0];
|
||||
assert.equal(hub.state.selected.type, 'pool'); assert.equal(pool.members.length, 2);
|
||||
const lease = await hub.acquire(config.modelRouting, randomUUID(), new Set(), undefined, Date.now() + 1000, ['tools', 'vision']);
|
||||
assert.equal(lease.member.model, 'tools-and-vision'); lease.release();
|
||||
hub.select({ type: 'connection', id: result.added[1] });
|
||||
await provider.complete(hub.prepare(f.store.config), [{ role: 'user', content: 'owned' }]);
|
||||
assert.equal(server.requests.at(-1).input.model, 'tools-and-vision'); assert.equal(server.requests.at(-1).headers.authorization, 'Bearer owned-auto');
|
||||
assert.equal(before.routing.type, 'pool'); assert.equal(before.routing.members.length, 2); assert.equal(hub.attempts.length, 1);
|
||||
});
|
||||
|
||||
test('unknown catalog capabilities require explicit declarations; upstream negative and retired models cannot be overridden', async t => {
|
||||
const f = fixture(t), server = await compatibleFixture(t, () => ({ data: [{ id: 'unknown' }, { id: 'negative', capabilities: { tools: false } }, { id: 'retired', lifecycle: 'retired' }] }));
|
||||
const { hub } = create(f), preview = await hub.modelCatalog().fetch({ draft: { ...draft(server), requiresKey: false } });
|
||||
assert.throws(() => hub.modelCatalog().apply({ previewId: preview.id, models: ['retired'] }), /退役/);
|
||||
const result = hub.modelCatalog().apply({ previewId: preview.id, models: ['unknown', 'negative'], declarations: { text: 'supported', tools: 'supported' } });
|
||||
assert.equal(hub.status(hub.connection(result.added[0])).capabilities.tools, 'protocol_declared');
|
||||
assert.equal(hub.status(hub.connection(result.added[1])).capabilities.tools, 'unsupported');
|
||||
});
|
||||
|
||||
test('model reasoning controls use declared effort and mode ranges without silently enabling unsupported options', async t => {
|
||||
const f = fixture(t), server = await compatibleFixture(t, () => ({ data: [metadata('claude-owned')] }));
|
||||
const { hub } = create(f), preview = await hub.modelCatalog().fetch({ draft: { ...draft(server), protocol: 'anthropic-messages', requiresKey: false } });
|
||||
const result = hub.modelCatalog().apply({ previewId: preview.id, models: ['claude-owned'] }), member = hub.connection(result.added[0]);
|
||||
const profile = modelParameterProfile(member); assert.deepEqual(profile.efforts, ['low', 'high']); assert.deepEqual(profile.modes, ['disabled', 'adaptive']);
|
||||
assert.throws(() => hub.setNativeParameters({ id: member.id, revision: member.revision, nativeParameters: { thinking: { type: 'enabled', budget_tokens: 1024 } } }), /推理模式/);
|
||||
assert.throws(() => hub.setNativeParameters({ id: member.id, revision: member.revision, nativeParameters: { output_config: { effort: 'max' } } }), /推理强度/);
|
||||
hub.setNativeParameters({ id: member.id, revision: member.revision, nativeParameters: { thinking: { type: 'adaptive' }, output_config: { effort: 'high' } } });
|
||||
});
|
||||
|
||||
test('cancelled, expired, changed-source and failed catalogs cannot save or overwrite a later selection', async t => {
|
||||
const f = fixture(t); let release;
|
||||
const server = await compatibleFixture(t, async () => { await new Promise(resolve => { release = resolve; }); return { data: [metadata('owned')] }; });
|
||||
const { hub } = create(f), source = hub.upsert({ ...draft(server), model: 'source', apiKey: 'owned' }), id = randomUUID();
|
||||
const pending = hub.modelCatalog().fetch({ id: source.id, revision: source.revision, requestId: id });
|
||||
await until(() => !!release); hub.modelCatalog().cancel(id); release(); await assert.rejects(pending);
|
||||
assert.equal(hub.catalog.previews.size, 0); assert.equal(hub.catalog.active.size, 0);
|
||||
assert.throws(() => hub.catalog.apply({ previewId: id, models: ['owned'] }), /取消/);
|
||||
const alreadyCancelled = randomUUID(); hub.catalog.cancel(alreadyCancelled);
|
||||
await assert.rejects(hub.catalog.fetch({ draft: draft(server), apiKey: 'owned', requestId: alreadyCancelled }), /取消/);
|
||||
assert.equal(server.requests.length, 1, 'cancel received before fetch also prevents a late dispatch');
|
||||
const pending2 = hub.catalog.fetch({ id: source.id, revision: source.revision }); await until(() => hub.catalog.active.size > 0);
|
||||
hub.toggle(source.id, false); await until(() => server.requests.length === 2); release(); await assert.rejects(pending2, /变化/);
|
||||
assert.equal(hub.catalog.previews.size, 0); hub.toggle(source.id, true);
|
||||
const pending3 = hub.catalog.fetch({ id: source.id, revision: hub.connection(source.id).revision }); await until(() => server.requests.length === 3); release();
|
||||
const preview = await pending3; hub.catalog.previews.get(preview.id).expires = 0;
|
||||
assert.throws(() => hub.catalog.apply({ previewId: preview.id, models: ['owned'] }), /过期/);
|
||||
});
|
||||
|
||||
test('pagination loops, conflicting models and upstream errors fail once without creating members or model attempts', async t => {
|
||||
const f = fixture(t); let scenario = 'loop';
|
||||
const server = await compatibleFixture(t, (_url, _input, requests) => scenario === 'loop' ? { data: [{ id: 'same' }], has_more: true, last_id: 'same' }
|
||||
: scenario === 'conflict' ? { data: [{ id: 'same' }, { id: 'same', display_name: 'other' }] }
|
||||
: { status: 503, body: { error: { message: 'owned-catalog upstream unavailable' } } });
|
||||
const { hub } = create(f);
|
||||
await assert.rejects(hub.modelCatalog().fetch({ draft: draft(server), apiKey: 'owned' }), /分页/); assert.equal(server.requests.length, 2);
|
||||
scenario = 'conflict'; await assert.rejects(hub.catalog.fetch({ draft: draft(server), apiKey: 'owned' }), /冲突/);
|
||||
scenario = 'error'; await assert.rejects(hub.catalog.fetch({ draft: draft(server), apiKey: 'owned' })); assert.equal(server.requests.length, 4);
|
||||
assert.equal(hub.state.connections.length, 0); assert.equal(hub.attempts.length, 0); assert.equal(hub.catalog.previews.size, 0);
|
||||
});
|
||||
|
||||
test('limits and transaction failures preserve original registry, config, cipher and preview for explicit correction', async t => {
|
||||
const f = fixture(t), server = await compatibleFixture(t, () => ({ data: Array.from({ length: 101 }, (_, i) => ({ id: 'owned-' + i })) }));
|
||||
const { hub, provider } = create(f), preview = await hub.modelCatalog().fetch({ draft: draft(server), apiKey: 'owned-batch' });
|
||||
assert.throws(() => hub.catalog.apply({ previewId: preview.id, models: preview.entries.slice(0, 33).map(value => value.id), routing: 'auto' }), /32/);
|
||||
assert.throws(() => hub.catalog.apply({ previewId: preview.id, models: ['owned-0', 'owned-0'] }), /重复/);
|
||||
assert.throws(() => hub.catalog.apply({ previewId: preview.id, models: ['foreign'] }), /不属于/);
|
||||
const registry = structuredClone(hub.state), config = structuredClone(f.store.config), setMeta = f.store.storage.setMeta.bind(f.store.storage);
|
||||
t.mock.method(f.store.storage, 'setMeta', (key, value) => { if (key === 'modelRegistry') throw new Error('owned commit failure'); return setMeta(key, value); });
|
||||
assert.throws(() => hub.catalog.apply({ previewId: preview.id, models: ['owned-0', 'owned-1'], persistKey: true, routing: 'auto' }), /owned commit failure/);
|
||||
assert.deepEqual(hub.state, registry); assert.deepEqual(f.store.config, config); assert.equal(provider.credentials.size, 0);
|
||||
assert.equal(f.store.storage.meta('modelCredentials', null), null); assert.equal(hub.catalog.previews.has(preview.id), true);
|
||||
});
|
||||
|
||||
test('a model that switches from a shared to an explicit credential receives a new revision for opaque-history isolation', async t => {
|
||||
const f = fixture(t), server = await compatibleFixture(t, () => ({ data: [metadata('a'), metadata('b')] }));
|
||||
const { hub } = create(f), preview = await hub.modelCatalog().fetch({ draft: draft(server), apiKey: 'owned-shared' });
|
||||
const result = hub.catalog.apply({ previewId: preview.id, models: ['a', 'b'] }), child = hub.connection(result.added[1]);
|
||||
const before = hub.credential(child); hub.upsert({ ...child, apiKey: 'owned-independent' });
|
||||
const after = hub.connection(child.id); assert.equal(after.credentialSource, undefined); assert.equal(hub.credential(after).revision, before.revision + 1);
|
||||
assert.equal(hub.credential(after).key, 'owned-independent'); assert.equal(hub.credential(hub.connection(result.added[0])), before);
|
||||
});
|
||||
|
||||
test('compiled HTTP directory flow persists selection and references, gates changes during fetch and permits explicit cancel', async t => {
|
||||
const f = fixture(t); f.store.close(); let wait = false, release;
|
||||
const server = await compatibleFixture(t, async () => { if (wait) await new Promise(resolve => { release = resolve; }); return { data: [metadata('api-a'), metadata('api-b')] }; });
|
||||
const host = await launchWorkbench(t, f.data);
|
||||
const preview = await host.api('/model-center/catalog', { draft: draft(server), apiKey: 'owned-http-key' });
|
||||
const result = await host.api('/model-center/catalog/apply', { previewId: preview.id, models: ['api-a', 'api-b'], routing: 'auto' });
|
||||
assert.equal(result.added.length, 2); assert.equal(result.center.selected.type, 'pool');
|
||||
const selected = await host.api('/model-center/selection'); assert.equal(selected.config.mode, 'compatible'); assert.equal(selected.modelSelection.type, 'pool');
|
||||
const source = result.center.connections.find(value => value.id === result.added[0]); wait = true;
|
||||
const requestId = randomUUID(), pending = host.api('/model-center/catalog', { id: source.id, revision: source.revision, requestId });
|
||||
const observed = pending.then(value => ({ value }), error => ({ error }));
|
||||
await until(() => !!release);
|
||||
await assert.rejects(host.api('/model-center/connections', { ...draft(server), model: 'blocked' }), cause => cause.status === 409);
|
||||
await host.api('/model-center/catalog/cancel', { id: requestId }); release(); assert.ok((await observed).error);
|
||||
await assert.rejects(host.api('/model-center/catalog/apply', { previewId: requestId, models: ['api-a'] }), cause => cause.status === 409);
|
||||
assert.equal((await host.api('/model-center')).attempts.length, 0);
|
||||
await host.close(); const store = new Store(f.data); f.onCleanup(() => store.close());
|
||||
const provider = new Provider(); provider.setKey(''); const hub = provider.modelHub(store);
|
||||
assert.equal(hub.state.selected.type, 'pool'); assert.equal(hub.connection(result.added[1]).credentialSource.connectionId, source.id);
|
||||
assert.equal(hub.status(hub.connection(result.added[1])).status, 'needs_key', 'volatile credentials are not silently persisted');
|
||||
assert.equal(JSON.stringify(hub.view()).includes('owned-http-key'), false);
|
||||
});
|
||||
|
||||
test('large directories declare partial coverage and a batch crossing the local connection limit leaves no partial members', async t => {
|
||||
const f = fixture(t), server = await compatibleFixture(t, () => ({ data: Array.from({ length: 1000 }, (_, i) => ({ id: 'many-' + i })), has_more: true, last_id: 'many-999' }));
|
||||
const { hub } = create(f), preview = await hub.modelCatalog().fetch({ draft: { ...draft(server), requiresKey: false } });
|
||||
assert.equal(preview.complete, false); assert.match(preview.warning, /后续页|部分/); assert.equal(preview.entries.length, 1000); assert.equal(server.requests.length, 1);
|
||||
for (let i = 0; i < 99; i++) hub.upsert({ ...draft(server), name: 'existing-' + i, model: 'existing-' + i, requiresKey: false });
|
||||
const previous = structuredClone(hub.state);
|
||||
assert.throws(() => hub.catalog.apply({ previewId: preview.id, models: ['many-1', 'many-2'] }), /100个/);
|
||||
assert.deepEqual(hub.state, previous); assert.equal(hub.catalog.previews.has(preview.id), true);
|
||||
});
|
||||
|
||||
test('a distinct upstream account at the same endpoint and model never silently reuses another account', async t => {
|
||||
const f = fixture(t), server = await compatibleFixture(t, () => ({ data: [metadata('same-model')] })), { hub } = create(f);
|
||||
const first = hub.upsert({ ...draft(server), model: 'same-model', apiKey: 'owned-account-first' });
|
||||
const preview = await hub.modelCatalog().fetch({ draft: draft(server), apiKey: 'owned-account-second' });
|
||||
const result = hub.catalog.apply({ previewId: preview.id, models: ['same-model'] });
|
||||
assert.equal(result.added.length, 1); assert.deepEqual(result.existing, []);
|
||||
assert.notEqual(result.added[0], first.id); assert.equal(hub.credential(hub.connection(result.added[0])).key, 'owned-account-second');
|
||||
assert.equal(hub.credential(hub.connection(first.id)).key, 'owned-account-first');
|
||||
});
|
||||
|
||||
test('source changes invalidate an in-flight child probe and host shutdown cancels directory work and drops transient previews', async t => {
|
||||
const f = fixture(t); let mode = 'normal', release;
|
||||
const server = await compatibleFixture(t, async (_url, input) => {
|
||||
if (mode === 'wait') await new Promise(resolve => { release = resolve; });
|
||||
return input ? { ...completion([], 'OK'), model: input.model } : { data: [metadata('a'), metadata('b')] };
|
||||
});
|
||||
const { provider, hub } = create(f), preview = await hub.modelCatalog().fetch({ draft: draft(server), apiKey: 'owned-stop' });
|
||||
const result = hub.catalog.apply({ previewId: preview.id, models: ['a', 'b'] }), source = hub.connection(result.added[0]);
|
||||
mode = 'wait'; const probing = hub.probe(result.added[1]).then(value => ({ value }), error => ({ error }));
|
||||
await until(() => !!release); hub.toggle(source.id, false); release(); assert.match((await probing).error.message, /变化/);
|
||||
assert.equal(hub.state.capabilityResults?.[result.added[1]], undefined); hub.toggle(source.id, true); mode = 'normal';
|
||||
await hub.catalog.fetch({ id: source.id, revision: hub.connection(source.id).revision }); assert.equal(hub.catalog.previews.size, 1);
|
||||
release = null; mode = 'wait'; const fetching = hub.catalog.fetch({ id: source.id, revision: hub.connection(source.id).revision }).then(value => ({ value }), error => ({ error }));
|
||||
await until(() => !!release); await provider.abortRequests(); release(); assert.ok((await fetching).error);
|
||||
assert.equal(hub.catalog.previews.size, 0); assert.equal(hub.catalog.active.size, 0);
|
||||
await assert.rejects(hub.catalog.fetch({ draft: draft(server), apiKey: 'owned' }), /停止/);
|
||||
});
|
||||
@@ -7,6 +7,7 @@ import { fixture, workspace } from './helpers.mjs';
|
||||
import { Provider } from '../src/server/provider.mjs';
|
||||
import { Runtime } from '../src/server/runtime.mjs';
|
||||
import { ModelHub } from '../src/core/models/model-hub.mjs';
|
||||
import { ModelCatalog } from '../src/core/models/model-catalog.mjs';
|
||||
import { ConfigImporter } from '../src/core/models/config-import.mjs';
|
||||
import { NetworkDiagnostics } from '../src/core/models/network-diagnostics.mjs';
|
||||
import { installUpgrade } from '../src/server/hot-upgrade.mjs';
|
||||
@@ -53,6 +54,8 @@ test('failed adoption restores the original virtual data-root name and original
|
||||
test('hot upgrade refreshes retained model and importer methods while preserving credential and routing objects', async t => {
|
||||
const f = await setup(t), credentials = f.provider.credentials, state = f.hub.state;
|
||||
const network = f.hub.networkDiagnostics(), networkView = network.view(), oldNetwork = Object.create(NetworkDiagnostics.prototype);
|
||||
const catalog = f.hub.modelCatalog(), oldCatalog = Object.create(ModelCatalog.prototype), previews = catalog.previews;
|
||||
Object.setPrototypeOf(catalog, oldCatalog);
|
||||
Object.setPrototypeOf(network, oldNetwork);
|
||||
const oldHub = Object.create(ModelHub.prototype), oldImporter = Object.create(ConfigImporter.prototype);
|
||||
Object.setPrototypeOf(f.hub, oldHub); Object.setPrototypeOf(f.hub.importer, oldImporter);
|
||||
@@ -66,6 +69,7 @@ test('hot upgrade refreshes retained model and importer methods while preserving
|
||||
assert.equal(Object.getPrototypeOf(f.hub.importer), ConfigImporter.prototype);
|
||||
assert.equal(f.hub.networkChecks, network); assert.equal(Object.getPrototypeOf(network), NetworkDiagnostics.prototype);
|
||||
assert.deepEqual(network.view(), networkView);
|
||||
assert.equal(f.hub.catalog, catalog); assert.equal(catalog.previews, previews); assert.equal(Object.getPrototypeOf(catalog), ModelCatalog.prototype);
|
||||
const response = await fetch('http://127.0.0.1:' + f.server.address().port + '/api/health');
|
||||
assert.equal((await response.json()).ready, true);
|
||||
});
|
||||
@@ -73,6 +77,8 @@ test('hot upgrade refreshes retained model and importer methods while preserving
|
||||
test('hot upgrade refuses active probes and restores retained model methods if installation fails', async t => {
|
||||
const f = await setup(t), handler = f.server.listeners('request')[0];
|
||||
const network = f.hub.networkDiagnostics(), oldNetwork = Object.create(NetworkDiagnostics.prototype);
|
||||
const catalog = f.hub.modelCatalog(), oldCatalog = Object.create(ModelCatalog.prototype);
|
||||
Object.setPrototypeOf(catalog, oldCatalog);
|
||||
Object.setPrototypeOf(network, oldNetwork);
|
||||
for (const [set, clear] of [
|
||||
[() => { f.provider.activeRequests = 1; }, () => { f.provider.activeRequests = 0; }],
|
||||
@@ -82,6 +88,8 @@ test('hot upgrade refuses active probes and restores retained model methods if i
|
||||
set(); assert.throws(() => installUpgrade(f.context, { appDirectory: workspace, updateServiceInfo: false }), /model requests and probes/);
|
||||
assert.equal(f.server.listeners('request')[0], handler); clear();
|
||||
}
|
||||
catalog.active.set('owned-active-catalog', new AbortController());
|
||||
assert.throws(() => installUpgrade(f.context, { appDirectory: workspace, updateServiceInfo: false }), /active model catalogs/); catalog.active.clear();
|
||||
const oldHub = Object.create(ModelHub.prototype), oldImporter = Object.create(ConfigImporter.prototype);
|
||||
Object.setPrototypeOf(f.hub, oldHub); Object.setPrototypeOf(f.hub.importer, oldImporter);
|
||||
f.store.claimRuntime = () => { throw new Error('fixture installation failure'); };
|
||||
@@ -89,5 +97,6 @@ test('hot upgrade refuses active probes and restores retained model methods if i
|
||||
assert.equal(f.server.listeners('request')[0], handler);
|
||||
assert.equal(Object.getPrototypeOf(f.hub), oldHub); assert.equal(Object.getPrototypeOf(f.hub.importer), oldImporter);
|
||||
assert.equal(f.hub.networkChecks, network); assert.equal(Object.getPrototypeOf(network), oldNetwork);
|
||||
assert.equal(Object.getPrototypeOf(catalog), oldCatalog);
|
||||
assert.equal(f.provider.apiKey, 'owned-upgrade-fixture');
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user