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
+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) {