feat: add model center and pool routing

This commit is contained in:
Agent Workbench Dev
2026-10-30 16:52:05 +08:00
parent 9770a536bd
commit fb58a41e08
18 changed files with 778 additions and 11 deletions
+44
View File
@@ -8,6 +8,7 @@ import { assertWorkspacesResolved } from '../shared/contracts/workspaces.mjs';
import { verifyWorkspaceProject } from '../core/workspaces/git-workspaces.mjs';
import { publicEngineJob } from '../shared/contracts/engines.mjs';
import { verifyBrowserJob } from '../core/browser/browser-manager.mjs';
import { ConfigImporter } from '../core/models/config-import.mjs';
export function createSessionPublisher(clients) {
return session => {
@@ -71,6 +72,48 @@ return async (request, response) => {
const method = request.method;
const route = url.pathname;
if (method === 'GET' && route === '/api/health') return json(response, { app: 'agent-workbench', version: APP_VERSION, pid: process.pid, ready: true, uptimeMs: Date.now() - started });
if (route.startsWith('/api/model-center')) {
const hub = provider.modelHub(store);
const importer = () => hub.importer ??= new ConfigImporter(hub);
if (method === 'GET' && route === '/api/model-center') return json(response, hub.view());
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/toggle') return json(response, hub.toggle(input.id, input.enabled));
if (runtime.hasActivity()) throw Object.assign(new Error('请先停止正在运行的任务,再修改或测试模型配置。'), { status: 409 });
if (route === '/api/model-center/connections') return json(response, hub.upsert(input), 201);
if (route === '/api/model-center/pools') return json(response, hub.pool(input), 201);
if (route === '/api/model-center/select') return json(response, { center: hub.select(input), config: { ...store.config, hasKey: provider.hasKey() } });
if (route === '/api/model-center/import-preview') return json(response, importer().preview(input.source, String(input.path ?? '')));
if (route === '/api/model-center/import-apply') return json(response, importer().apply(input.previewId, input.autoSync));
if (route === '/api/model-center/import-toggle') return json(response, importer().toggle(input.id, input.enabled));
if (route === '/api/model-center/import-sync') return json(response, { results: importer().sync(), center: hub.view() });
if (route === '/api/model-center/probe') {
const connection = hub.connection(input.id);
if (!hub.available({ ...connection, credentialRevision: hub.credential(connection)?.revision ?? 0 })) throw new Error('该连接不可用,请核对凭据、冷却或停用状态。');
const config = { ...store.config, mode: 'compatible', modelRouting: { type: 'connection', id: connection.id, revision: connection.revision,
strategy: 'round-robin', sticky: false, maxAttempts: 1, queueTimeoutMs: 1000, members: [{ ...connection, credentialRevision: hub.credential(connection)?.revision ?? 0 }] } };
const result = await hub.complete(config, { model: connection.model, stream: false, max_tokens: 16, messages: [{ role: 'user', content: 'Reply OK.' }] }, undefined, undefined, null,
async (selected, key, payload) => {
const value = await provider.request(selected, '/chat/completions', payload, undefined, key, undefined, { attempts: 1 });
if (!value.choices?.[0]?.message) throw new Error('模型响应缺少完整message。');
return value;
});
return json(response, { ok: true, usage: result.usage ?? null, center: hub.view() });
}
}
}
if (method === 'GET' && route === '/api/operations') {
const checks = [];
const inspect = (id, title, action) => { try { checks.push({ id, title, status: 'passed', detail: action() }); } catch (cause) { checks.push({ id, title, status: 'failed', detail: provider.redact(cause.message) }); } };
inspect('node', '执行宿主', () => { if (Number(process.versions.node.split('.')[0]) < 24) throw new Error('需要Node.js24或以上。'); return process.version + ' · PID ' + process.pid; });
inspect('database', '任务数据库', () => { const rows = store.storage.database.prepare('PRAGMA quick_check').all(); if (rows.some(row => Object.values(row)[0] !== 'ok')) throw new Error('数据库一致性检查未通过。'); return 'SQLite quick_check通过;' + store.sessions.size + '个会话。'; });
inspect('artifacts', '最近执行工件', () => { const values = [...store.core.artifact.values()].slice(-50); for (const value of values) store.artifactStore.readBytes(value); return '逐字节核对最近' + values.length + '个工件;更早记录未在本次范围内。'; });
const center = provider.modelHub(store).view();
checks.push({ id: 'models', title: '模型连接', status: center.connections.some(value => value.status === 'configured' && value.model) ? 'configured' : 'attention', detail: center.connections.filter(value => value.enabled).length + '个启用连接;配置状态与真实模型响应分别记录。' });
return json(response, { version: APP_VERSION, checkedAt: new Date().toISOString(), checks, activeTasks: runtime.active.size,
queue: center.waiting, modelAttempts: center.attempts.length, modelRequests: 0, scope: '本地只读巡检;不发送模型请求、不执行项目命令。' });
}
if (method === 'GET' && route === '/api/bootstrap') return json(response, {
token, version: APP_VERSION, config: { ...store.config, hasKey: provider.hasKey() },
eventCursor: store.eventCursor, storage: store.storageInfo(), runtimeRoot: appDirectory,
@@ -447,6 +490,7 @@ return async (request, response) => {
};
provider.validate(config);
if (Object.hasOwn(input, 'apiKey')) provider.setKey(input.apiKey);
if (provider.hub) provider.hub.select({ type: 'legacy' });
store.saveConfig(config);
return json(response, { ...store.config, hasKey: provider.hasKey() });
}