feat: add model center and pool routing
This commit is contained in:
@@ -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() });
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@ import { Provider } from './provider.mjs';
|
||||
import { Runtime } from './runtime.mjs';
|
||||
import { createRequestHandler, createSessionPublisher } from './app.mjs';
|
||||
import { APP_VERSION } from './diagnostics.mjs';
|
||||
import { ConfigImporter } from '../core/models/config-import.mjs';
|
||||
|
||||
const appDirectory = process.env.AGENT_WORKBENCH_APP_DIR || path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..');
|
||||
const dataDirectory = process.env.AGENT_WORKBENCH_DATA_DIR || path.join(process.env.LOCALAPPDATA || path.join(os.homedir(), '.local', 'share'), 'AgentWorkbench');
|
||||
@@ -23,6 +24,14 @@ const started = Date.now();
|
||||
let actualPort = requestedPort;
|
||||
let closing = false;
|
||||
let exampleProject = null;
|
||||
const modelImportSync = setInterval(() => {
|
||||
try {
|
||||
const hub = provider.modelHub(store);
|
||||
if (!Object.values(hub.state.imports).some(source => source.enabled)) return;
|
||||
(hub.importer ??= new ConfigImporter(hub)).sync();
|
||||
} catch {}
|
||||
}, 60000);
|
||||
modelImportSync.unref();
|
||||
try {
|
||||
const exampleRoot = path.join(appDirectory, 'examples', 'starter-project');
|
||||
if (fs.existsSync(exampleRoot)) exampleProject = store.addProject(exampleRoot);
|
||||
@@ -36,6 +45,7 @@ server.on('request', createRequestHandler({ appDirectory, server, store, provide
|
||||
|
||||
async function shutdown() {
|
||||
if (closing) return; closing = true;
|
||||
clearInterval(modelImportSync);
|
||||
await runtime.stopAll();
|
||||
for (const response of clients) response.end();
|
||||
clients.clear();
|
||||
|
||||
+18
-4
@@ -3,6 +3,7 @@ import { listFiles } from './files.mjs';
|
||||
import { setTimeout as delay } from 'node:timers/promises';
|
||||
import { executionEnvironment, ModelRequestError, connectionFailure, httpFailure } from './diagnostics.mjs';
|
||||
import { completionBody, assertPlannedRequest } from '../shared/contracts/model-request.mjs';
|
||||
import { ModelHub } from '../core/models/model-hub.mjs';
|
||||
|
||||
export const engineToolDefinitions = [
|
||||
{ type: 'function', function: { name: 'inspect_engine_checks', description: '读取当前Task已经保存的外部引擎连接核验:真实安装版本、启动完成信号、SDK会话、进程关闭、原报告工件和能力限制。不会启动引擎或发送prompt;完整原记录通过已有read_artifact读取。', parameters: { type: 'object', properties: {}, additionalProperties: false } } },
|
||||
@@ -97,18 +98,24 @@ export class Provider {
|
||||
constructor(options = {}) { this.apiKey = process.env.AGENT_WORKBENCH_API_KEY || ''; this.fetcher = options.fetch; this.retryPolicy = options.retryPolicy; }
|
||||
hasKey() { return !!this.apiKey; }
|
||||
setKey(value) { this.apiKey = String(value ?? '').trim(); }
|
||||
redact(value, key = this.apiKey) { return key ? String(value).replaceAll(key, '[已隐藏]') : String(value); }
|
||||
modelHub(store) { return this.hub ??= new ModelHub(store, this); }
|
||||
prepareConfig(config, store) { return this.modelHub(store).prepare(config); }
|
||||
redact(value, key = this.apiKey) {
|
||||
let text = String(value);
|
||||
for (const secret of [key, this.apiKey, ...[...(this.credentials?.values() ?? [])].map(value => value.key)].filter(Boolean).sort((a, b) => b.length - a.length)) text = text.replaceAll(secret, '[已隐藏]');
|
||||
return text;
|
||||
}
|
||||
validate(config) {
|
||||
const url = new URL(config.baseUrl);
|
||||
if (!['http:', 'https:'].includes(url.protocol) || url.username || url.password || url.search || url.hash) throw new Error('请输入不含账号、查询参数的 http/https 服务地址。');
|
||||
if (config.mode === 'compatible' && !String(config.model).trim()) throw new Error('请先在模型设置中填写模型名称。');
|
||||
return config.baseUrl.replace(/\/+$/, '');
|
||||
}
|
||||
async request(config, route, body, signal, key = this.apiKey, onProgress) {
|
||||
async request(config, route, body, signal, key = this.apiKey, onProgress, options = {}) {
|
||||
const base = this.validate({ ...config, mode: 'demo' });
|
||||
const headers = { 'Content-Type': 'application/json' };
|
||||
if (key) headers.Authorization = 'Bearer ' + key;
|
||||
const policy = { attempts: 3, delays: [500, 1500], timeoutMs: signal ? 60000 : 20000, totalTimeoutMs: signal ? 90000 : 20000, ...this.retryPolicy };
|
||||
const policy = { attempts: 3, delays: [500, 1500], timeoutMs: signal ? 60000 : 20000, totalTimeoutMs: signal ? 90000 : 20000, ...this.retryPolicy, ...options };
|
||||
const deadline = Date.now() + policy.totalTimeoutMs;
|
||||
const startedAt = Date.now();
|
||||
let attempts = 0;
|
||||
@@ -173,7 +180,14 @@ export class Provider {
|
||||
assertPlannedRequest(body, requestPlan);
|
||||
if (config.mode === 'demo') return this.demo(messages, project);
|
||||
this.validate(config);
|
||||
const result = await this.request(config, '/chat/completions', body, signal, this.apiKey, onProgress);
|
||||
const execute = async (selected, key, input, progress) => {
|
||||
const value = await this.request(selected, '/chat/completions', input, signal, key, progress, config.modelRouting ? { attempts: 1 } : {});
|
||||
if (!value.choices?.[0]?.message || value.choices[0].finish_reason === 'length') throw new ModelRequestError({ kind: 'protocol', source: 'model_connection',
|
||||
stage: 'model_request', title: '模型响应不完整', message: '模型响应缺少完整message或达到输出上限,未执行工具。', retryable: false, occurredAt: new Date().toISOString() });
|
||||
return value;
|
||||
};
|
||||
const result = config.modelRouting && this.hub ? await this.hub.complete(config, body, signal, onProgress, requestPlan, execute)
|
||||
: await execute(config, this.apiKey, body, onProgress);
|
||||
const received = result.choices?.[0]?.message;
|
||||
if (!received) throw new Error('兼容服务响应中缺少 choices[0].message。');
|
||||
if (result.choices[0].finish_reason === 'length') throw new Error('模型输出达到长度上限,未执行未完整的工具请求。请提高输出上限或缩小任务。');
|
||||
|
||||
@@ -141,7 +141,8 @@ export class Runtime {
|
||||
const project = this.store.project(session.projectId);
|
||||
safePath(project);
|
||||
const controller = new AbortController();
|
||||
const config = structuredClone(this.store.config);
|
||||
const initialConfig = structuredClone(this.store.config);
|
||||
const config = this.provider.prepareConfig ? this.provider.prepareConfig(initialConfig, this.store) : initialConfig;
|
||||
if (config.mode !== 'demo') this.provider.validate(config);
|
||||
const record = this.store.beginRun(session, content, resume, config, { instructionSource });
|
||||
const run = { controller, id: record.id, taskId: record.taskId, project, record, config };
|
||||
@@ -232,7 +233,8 @@ export class Runtime {
|
||||
planned.record.requestAttempts = Math.max(planned.record.requestAttempts, progress.attempt ?? 0);
|
||||
stepRecord.modelAttempts = Math.max(stepRecord.modelAttempts, progress.attempt ?? 0);
|
||||
requestEvent.phase = progress.phase; requestEvent.attempts = progress.attempt;
|
||||
requestEvent.label = progress.phase === 'retrying' ? '正在重试模型连接' : progress.phase === 'completed' ? '模型响应已收到' : progress.phase === 'failed' ? '模型连接失败' : '请求模型服务';
|
||||
if (progress.connectionId) { requestEvent.connectionId = progress.connectionId; requestEvent.modelAttemptId = progress.modelAttemptId; requestEvent.actualModel = progress.model; }
|
||||
requestEvent.label = progress.switching ? '正在切换模型池连接' : progress.phase === 'retrying' ? '正在重试模型连接' : progress.phase === 'completed' ? '模型响应已收到' : progress.phase === 'failed' ? '模型连接失败' : '请求模型服务';
|
||||
requestEvent.output = progress.phase === 'retrying' ? '第 ' + progress.attempt + ' 次请求失败,将自动重试(最多 ' + progress.maxAttempts + ' 次)。'
|
||||
: progress.phase === 'completed' ? '模型响应已收到;工具请求将在校验后执行。'
|
||||
: progress.diagnostic?.message ?? '正在等待模型响应。';
|
||||
|
||||
Reference in New Issue
Block a user