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

This commit is contained in:
Agent Workbench Dev
2026-10-31 22:27:53 +08:00
parent 578cf88eca
commit eeb11d6a76
24 changed files with 856 additions and 52 deletions
+1 -11
View File
@@ -176,17 +176,7 @@ return async (request, response) => {
if (route === '/api/model-center/import-toggle') return json(response, importer().toggle(input.id, input.enabled));
if (route === '/api/model-center/import-sync') return json(response, { results: importer().sync(), center: hub.view() });
if (route === '/api/model-center/probe') {
const connection = hub.connection(input.id);
if (!hub.available({ ...connection, credentialRevision: hub.credential(connection)?.revision ?? 0 })) throw new Error('该连接不可用,请核对凭据、冷却或停用状态。');
const config = { ...store.config, mode: 'compatible', modelRouting: { type: 'connection', id: connection.id, revision: connection.revision,
strategy: 'round-robin', sticky: false, maxAttempts: 1, queueTimeoutMs: 1000, members: [{ ...connection, credentialRevision: hub.credential(connection)?.revision ?? 0 }] } };
const result = await hub.complete(config, { model: connection.model, stream: false, max_tokens: 16, messages: [{ role: 'user', content: 'Reply OK.' }] }, undefined, undefined, null,
async (selected, key, payload) => {
const value = await provider.request(selected, '/chat/completions', payload, undefined, key, undefined, { attempts: 1 });
if (!value.choices?.[0]?.message) throw new Error('模型响应缺少完整message。');
return value;
});
return json(response, { ok: true, usage: result.usage ?? null, center: hub.view() });
return json(response, await hub.probe(input.id, input.scope ?? 'text'));
}
}
}
+22 -4
View File
@@ -4,6 +4,8 @@ import { setTimeout as delay } from 'node:timers/promises';
import { executionEnvironment, ModelRequestError, connectionFailure, httpFailure } from './diagnostics.mjs';
import { completionBody, assertPlannedRequest } from '../shared/contracts/model-request.mjs';
import { ModelHub } from '../core/models/model-hub.mjs';
import { normalizeProtocolResponse, protocolRoute, validateModelResponse } from '../core/models/protocols.mjs';
import { readModelStream } from '../core/models/model-stream.mjs';
export const engineToolDefinitions = [
{ type: 'function', function: { name: 'inspect_engine_checks', description: '读取当前Task已经保存的外部引擎连接核验:真实安装版本、启动完成信号、SDK会话、进程关闭、原报告工件和能力限制。不会启动引擎或发送prompt;完整原记录通过已有read_artifact读取。', parameters: { type: 'object', properties: {}, additionalProperties: false } } },
@@ -145,9 +147,17 @@ export class Provider {
const requestSignal = signal ? AbortSignal.any([signal, timeout]) : timeout;
const context = { endpoint: base, model: config.model, route, attempt };
onProgress?.({ phase: 'running', attempt, maxAttempts: policy.attempts, endpoint: base, route });
let readingStream = false;
try {
const response = await (this.fetcher ?? fetch)(base + route, { method: body ? 'POST' : 'GET', headers,
body: body ? JSON.stringify(body) : undefined, signal: requestSignal });
if (response.ok && body?.stream === true) {
readingStream = true;
const streamed = await readModelStream(response, config, requestSignal, progress => onProgress?.({ ...progress, attempt, maxAttempts: policy.attempts }));
this.lastRequest = { source: 'model_connection', endpoint: base, route, model: config.model, status: 'completed', attempts: attempt, durationMs: Date.now() - startedAt, stream: streamed.evidence };
onProgress?.({ phase: 'completed', attempt, maxAttempts: policy.attempts, durationMs: Date.now() - startedAt, stream: streamed.evidence });
return streamed.value;
}
const text = await boundedResponse(response);
if (!response.ok) {
let message = text;
@@ -171,7 +181,9 @@ export class Provider {
return result;
} catch (error) {
signal?.throwIfAborted();
const diagnostic = connectionFailure(error, { ...context, timedOut: timeout.aborted, redact: value => this.redact(value, key) });
let diagnostic = connectionFailure(error, { ...context, timedOut: timeout.aborted, redact: value => this.redact(value, key) });
if (readingStream) diagnostic = { ...diagnostic, retryable: false, outcome: 'unknown', kind: timeout.aborted ? 'timeout' : diagnostic.kind === 'protocol' ? 'protocol' : 'stream_interrupted',
message: diagnostic.message + ' 已接收的流片段保留,请求不会自动重发。' };
const remaining = deadline - Date.now();
const waitMs = diagnostic.retryAfterMs ?? policy.delays[Math.min(attempt - 1, policy.delays.length - 1)] ?? 500;
const retry = diagnostic.retryable && attempt < policy.attempts && remaining > waitMs;
@@ -201,10 +213,10 @@ export class Provider {
if (config.mode === 'demo') return this.demo(messages, project);
this.validate(config);
const execute = async (selected, key, input, progress) => {
const value = await this.request(selected, '/chat/completions', input, signal, key, progress, { ...(config.modelRouting ? { attempts: 1 } : {}), ...requestPolicy });
const value = normalizeProtocolResponse(selected, await this.request(selected, protocolRoute(selected), input, signal, key, progress, { ...(config.modelRouting ? { attempts: 1 } : {}), ...requestPolicy }));
if (!value.choices?.[0]?.message || value.choices[0].finish_reason === 'length') throw new ModelRequestError({ kind: 'protocol', source: 'model_connection',
stage: 'model_request', title: '模型响应不完整', message: '模型响应缺少完整message或达到输出上限,未执行工具。', retryable: false, occurredAt: new Date().toISOString() });
return value;
return validateModelResponse(value);
};
const result = config.modelRouting && this.hub ? await this.hub.complete(config, body, signal, onProgress, requestPlan, execute)
: await execute(config, this.apiKey, body, onProgress);
@@ -213,11 +225,17 @@ export class Provider {
if (result.choices[0].finish_reason === 'length') throw new Error('模型输出达到长度上限,未执行未完整的工具请求。请提高输出上限或缩小任务。');
if (received.tool_calls !== undefined && (!Array.isArray(received.tool_calls) || received.tool_calls.length > 12)) throw new Error('模型返回的工具调用数量或格式无效。');
const calls = (received.tool_calls ?? []).map(call => ({
id: typeof call.id === 'string' && call.id ? call.id : 'call_' + randomUUID().replaceAll('-', ''),
id: typeof call.id === 'string' && call.id ? call.id : '',
type: 'function', function: { name: String(call.function?.name ?? ''),
arguments: typeof call.function?.arguments === 'string' ? call.function.arguments : JSON.stringify(call.function?.arguments ?? {}) },
}));
if (calls.some(call => !call.id || !call.function.name)) throw new Error('模型工具调用缺少原身份或名称,未执行工具。');
for (const call of calls) {
let args; try { args = JSON.parse(call.function.arguments); } catch { throw new Error('模型工具参数不是完整JSON,未执行工具。'); }
if (!args || typeof args !== 'object' || Array.isArray(args)) throw new Error('模型工具参数必须是完整对象,未执行工具。');
}
return { message: { role: 'assistant', content: typeof received.content === 'string' ? received.content : '',
...(received.providerState ? { providerState: received.providerState } : {}),
...(calls.length ? { tool_calls: calls } : {}) }, usage: result.usage ?? null, finishReason: result.choices[0].finish_reason ?? null };
}
demo(messages, project) {
+6 -1
View File
@@ -284,12 +284,16 @@ export class Runtime {
planned.record.requestAttempts = Math.max(planned.record.requestAttempts, progress.attempt ?? 0);
stepRecord.modelAttempts = Math.max(stepRecord.modelAttempts, progress.attempt ?? 0);
requestEvent.phase = progress.phase; requestEvent.attempts = progress.attempt;
if (progress.streaming) requestEvent.stream = { status: 'receiving', protocol: progress.protocol, sequence: progress.streamSequence,
bytes: progress.streamBytes, textCharacters: progress.textCharacters, textPreview: this.provider.redact(progress.textPreview ?? '').slice(0, 8000), toolFragments: progress.toolFragments };
if (progress.stream) requestEvent.stream = { ...requestEvent.stream, ...progress.stream, status: progress.stream.terminal ? 'completed' : 'receiving' };
if (progress.phase === 'failed' && requestEvent.stream) requestEvent.stream.status = 'incomplete';
if (progress.connectionId) { requestEvent.connectionId = progress.connectionId; requestEvent.modelAttemptId = progress.modelAttemptId; requestEvent.actualModel = progress.model; }
requestEvent.label = progress.switching ? '正在切换模型池连接' : progress.phase === 'retrying' ? '正在重试模型连接' : progress.phase === 'completed' ? '模型响应已收到' : progress.phase === 'failed' ? '模型连接失败' : '请求模型服务';
if (externalEngine) requestEvent.label = progress.phase === 'completed' ? '引擎原结果已收到' : '受控引擎执行';
requestEvent.output = progress.phase === 'retrying' ? '第 ' + progress.attempt + ' 次请求失败,将自动重试(最多 ' + progress.maxAttempts + ' 次)。'
: progress.phase === 'completed' ? '模型响应已收到;工具请求将在校验后执行。'
: progress.diagnostic?.message ?? '正在等待模型响应。';
: progress.streaming ? '正在接收模型输出,工具参数完整核对前不会执行。' : progress.diagnostic?.message ?? '正在等待模型响应。';
if (externalEngine) requestEvent.output = progress.phase === 'completed' ? '原引擎工具请求或最终输出已返回,后续按所属任务核验。' : '等待所属引擎;实际模型尝试另有记录。';
if (progress.diagnostic) requestEvent.diagnostic = progress.diagnostic;
if (progress.durationMs !== undefined) requestEvent.durationMs = progress.durationMs;
@@ -305,6 +309,7 @@ export class Runtime {
planned.record.workerUsageAccounted = true; this.store.save(session); responseAccounted = true;
}
} catch (error) {
if (requestEvent?.stream && requestEvent.stream.status !== 'completed') { requestEvent.stream.status = signal.aborted ? 'interrupted' : 'incomplete'; requestEvent.phase = signal.aborted ? 'interrupted' : 'failed'; requestEvent.finishedAt = now(); }
this.requestPlanner().finish(session, planned.record, { error: Object.assign(new Error(this.provider.redact(error.message ?? String(error))), { name: signal.aborted ? 'AbortError' : error.name }), attempts: planned.record.requestAttempts });
if (run.worker && requestStarted && !responseAccounted) {
this.store.recordResponse(session, run.record, null, planned.record.estimatedInputTokens + config.maxOutputTokens);