feat(models): execute Responses and validated streams with capability routing
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import { createHash, randomUUID } from 'node:crypto';
|
||||
import { BudgetExceeded, accountedTokens } from '../../shared/contracts/execution.mjs';
|
||||
import { completionBody, requestMetrics } from '../../shared/contracts/model-request.mjs';
|
||||
import { completionBody, requestMetrics, requestVariants, requestHistory, outputReservation } from '../../shared/contracts/model-request.mjs';
|
||||
import { workingCandidates, workingDocument, workingEntry, workingMessage, verifyWorkingDocument } from './working-set.mjs';
|
||||
import { latestToolDocument, latestToolMessage, verifyLatestToolDocument } from './tool-feedback.mjs';
|
||||
import { projectMemoryMessage, MEMORY_CONTEXT_LIMITS } from '../memory/project-memory.mjs';
|
||||
@@ -349,8 +349,9 @@ export function verifyPlanEvidence(store, session, plan, cache = new Map()) {
|
||||
const request = get(plan.requestArtifactId, 'model-request', plan.requestSha256);
|
||||
const body = JSON.parse(store.artifactStore.read(request)), metrics = requestMetrics(body);
|
||||
if (metrics.bytes !== plan.requestBytes || metrics.estimatedInputTokens !== plan.estimatedInputTokens
|
||||
|| body.max_tokens !== plan.budget.outputReservation || metrics.estimatedInputTokens > plan.budget.inputBudget) throw new Error('请求预算与原正文不匹配。');
|
||||
historyGroups(body.messages);
|
||||
|| requestVariants(body).some(value => outputReservation(value) !== plan.budget.outputReservation) || metrics.estimatedInputTokens > plan.budget.inputBudget) throw new Error('请求预算与原正文不匹配。');
|
||||
historyGroups(requestHistory(body));
|
||||
for (const variant of requestVariants(body)) historyGroups(requestHistory(variant));
|
||||
const frozen = JSON.parse(store.artifactStore.read(get(plan.artifactId, 'context-plan')));
|
||||
if (frozen.id !== plan.id || frozen.requestSha256 !== plan.requestSha256 || frozen.requestArtifactId !== plan.requestArtifactId
|
||||
|| JSON.stringify(frozen.budget) !== JSON.stringify(plan.budget)) throw new Error('请求计划与发送前的封存记录不匹配。');
|
||||
@@ -375,7 +376,7 @@ export function verifyPlanEvidence(store, session, plan, cache = new Map()) {
|
||||
|| document.entries.length > MEMORY_CONTEXT_LIMITS.entries || Buffer.byteLength(JSON.stringify(document), 'utf8') > MEMORY_CONTEXT_LIMITS.bytes
|
||||
|| document.entries.some(entry => entry.sourceFingerprint !== document.sourceFingerprint)) throw new Error('项目记忆工件归属或范围不匹配。');
|
||||
const message = projectMemoryMessage(artifact, document);
|
||||
if (body.messages.filter(value => value.role === 'system' && value.content === message.content).length !== 1) throw new Error('项目记忆与实际发送内容不匹配。');
|
||||
if (requestHistory(body).filter(value => value.role === 'system' && value.content === message.content).length !== 1) throw new Error('项目记忆与实际发送内容不匹配。');
|
||||
}
|
||||
if ((frozen.skillCatalogArtifactId ?? null) !== (plan.skillCatalogArtifactId ?? null)
|
||||
|| (frozen.skillCatalogSha256 ?? null) !== (plan.skillCatalogSha256 ?? null)
|
||||
@@ -389,7 +390,7 @@ export function verifyPlanEvidence(store, session, plan, cache = new Map()) {
|
||||
|| document.entries.length > SKILL_LIMITS.enabled || Buffer.byteLength(JSON.stringify(document), 'utf8') > SKILL_LIMITS.catalogBytes
|
||||
|| new Set(document.entries.map(entry => entry.id)).size !== document.entries.length) throw new Error('技能目录归属或范围不匹配。');
|
||||
const message = skillCatalogMessage(artifact, document);
|
||||
if (body.messages.filter(value => value.role === 'system' && value.content === message.content).length !== 1) throw new Error('技能目录与实际发送内容不匹配。');
|
||||
if (requestHistory(body).filter(value => value.role === 'system' && value.content === message.content).length !== 1) throw new Error('技能目录与实际发送内容不匹配。');
|
||||
}
|
||||
if ((frozen.mcpCatalogArtifactId ?? null) !== (plan.mcpCatalogArtifactId ?? null)
|
||||
|| (frozen.mcpCatalogSha256 ?? null) !== (plan.mcpCatalogSha256 ?? null)
|
||||
@@ -402,7 +403,7 @@ export function verifyPlanEvidence(store, session, plan, cache = new Map()) {
|
||||
|| document.kind !== 'registered-mcp-catalog' || !Array.isArray(document.entries) || document.entries.length !== plan.mcpCatalogEntries
|
||||
|| document.entries.length > MCP_LIMITS.allowedTools || Buffer.byteLength(JSON.stringify(document), 'utf8') > 24000) throw new Error('MCP目录归属或范围不匹配。');
|
||||
const message = mcpCatalogMessage(artifact, document);
|
||||
if (body.messages.filter(value => value.role === 'system' && value.content === message.content).length !== 1) throw new Error('MCP目录与实际发送内容不匹配。');
|
||||
if (requestHistory(body).filter(value => value.role === 'system' && value.content === message.content).length !== 1) throw new Error('MCP目录与实际发送内容不匹配。');
|
||||
}
|
||||
if (plan.latestToolsArtifactId) {
|
||||
const artifact = get(plan.latestToolsArtifactId, 'context-latest-tools', plan.latestToolsSha256);
|
||||
@@ -410,7 +411,7 @@ export function verifyPlanEvidence(store, session, plan, cache = new Map()) {
|
||||
if (artifact.taskId !== plan.taskId) throw new Error('最新工具反馈的工件任务归属不匹配。');
|
||||
verifyLatestToolDocument(store, session, plan, document, historyGroups(session.providerMessages.slice(0, plan.originalMessages)));
|
||||
const message = latestToolMessage(artifact, document);
|
||||
if (body.messages.filter(value => value.role === 'system' && value.content === message.content).length !== 1) throw new Error('最新工具反馈与实际发送内容不匹配。');
|
||||
if (requestHistory(body).filter(value => value.role === 'system' && value.content === message.content).length !== 1) throw new Error('最新工具反馈与实际发送内容不匹配。');
|
||||
}
|
||||
if (plan.workingSetArtifactId) {
|
||||
const artifact = get(plan.workingSetArtifactId, 'context-working-set', plan.workingSetSha256);
|
||||
@@ -418,7 +419,7 @@ export function verifyPlanEvidence(store, session, plan, cache = new Map()) {
|
||||
if (artifact.taskId !== plan.taskId) throw new Error('工作依据工件任务归属不匹配。');
|
||||
verifyWorkingDocument(store, session, plan, document, historyGroups(session.providerMessages.slice(0, plan.originalMessages)));
|
||||
const message = workingMessage(artifact, document, plan.workingSetViewVersion ?? 1);
|
||||
if (body.messages.filter(value => value.role === 'system' && value.content === message.content).length !== 1) throw new Error('工作依据与实际发送内容不匹配。');
|
||||
if (requestHistory(body).filter(value => value.role === 'system' && value.content === message.content).length !== 1) throw new Error('工作依据与实际发送内容不匹配。');
|
||||
}
|
||||
if (!plan.historyIndexArtifactId) return;
|
||||
const index = JSON.parse(store.artifactStore.read(get(plan.historyIndexArtifactId, 'context-history-index', plan.historyIndexSha256)));
|
||||
|
||||
@@ -5,6 +5,7 @@ import { createHash, randomUUID } from 'node:crypto';
|
||||
import { DatabaseSync } from 'node:sqlite';
|
||||
import { parse as parseToml } from 'smol-toml';
|
||||
import { modelEndpoint } from './model-hub.mjs';
|
||||
import { MODEL_PROTOCOLS } from '../../shared/contracts/model-capabilities.mjs';
|
||||
|
||||
const digest = value => createHash('sha256').update(JSON.stringify(value)).digest('hex');
|
||||
export class ConfigImporter {
|
||||
@@ -13,7 +14,7 @@ export class ConfigImporter {
|
||||
const root = os.homedir();
|
||||
const sourcePath = path.join(root, '.cc-switch', 'cc-switch.db');
|
||||
return { sources: [{ source: 'ccswitch', path: sourcePath, available: fs.existsSync(sourcePath), supported: true,
|
||||
detail: '仅导入可验证的Chat Completions配置;Responses、Anthropic和客户端登录态分别标记不兼容。' },
|
||||
detail: '导入已核对格式的Chat Completions/Responses API配置;Anthropic和客户端登录态当前不支持。' },
|
||||
{ source: 'workbuddy', path: '', available: false, supported: false, detail: '当前尚未取得固定版本的WorkBuddy模型配置契约。可通过连接JSON导入已知API配置;不读取其登录态。' }] };
|
||||
}
|
||||
read(source, filename) {
|
||||
@@ -25,8 +26,10 @@ export class ConfigImporter {
|
||||
const value = JSON.parse(fs.readFileSync(filename, 'utf8').replace(/^\uFEFF/, ''));
|
||||
if (value.schema !== 1 || !Array.isArray(value.connections) || value.connections.length > 100) throw new Error('连接JSON需要schema:1与connections数组(最多100项)。');
|
||||
for (const [index, item] of value.connections.entries()) {
|
||||
if (item.protocol && item.protocol !== 'chat-completions') { notices.push('第' + (index + 1) + '项协议暂不支持,已跳过。'); continue; }
|
||||
rows.push({ sourceId: String(item.id ?? index), name: item.name, model: item.model, baseUrl: item.baseUrl, apiKey: item.apiKey, requiresKey: item.requiresKey !== false, brand: item.brand });
|
||||
if (item.protocol && !MODEL_PROTOCOLS.includes(item.protocol)) { notices.push('第' + (index + 1) + '项协议暂不支持,已跳过。'); continue; }
|
||||
rows.push({ sourceId: String(item.id ?? index), name: item.name, model: item.model, protocol: item.protocol ?? 'chat-completions',
|
||||
streaming: item.streaming === true, capabilityDeclarations: item.capabilityDeclarations,
|
||||
baseUrl: item.baseUrl, apiKey: item.apiKey, requiresKey: item.requiresKey !== false, brand: item.brand });
|
||||
}
|
||||
} else if (source === 'ccswitch') {
|
||||
const db = new DatabaseSync(filename, { readOnly: true });
|
||||
@@ -42,9 +45,9 @@ export class ConfigImporter {
|
||||
let parsed;
|
||||
try { parsed = typeof config.config === 'string' ? parseToml(config.config) : null; } catch { notices.push(provider.name + '的Codex TOML无法解析。'); continue; }
|
||||
const selected = parsed?.model_providers?.[parsed.model_provider];
|
||||
if (!selected?.base_url || selected.wire_api !== 'chat') { notices.push(provider.name + '使用Responses或未知协议,需对应的独立适配器。'); continue; }
|
||||
if (!selected?.base_url || !['chat', 'responses'].includes(selected.wire_api)) { notices.push(provider.name + '使用未知协议,需对应的独立适配器。'); continue; }
|
||||
rows.push({ sourceId: 'codex:' + provider.id, name: provider.name, baseUrl: selected.base_url, model: parsed.model,
|
||||
apiKey: config.auth?.OPENAI_API_KEY, requiresKey: true, brand: 'CCSwitch' });
|
||||
protocol: selected.wire_api === 'responses' ? 'responses' : 'chat-completions', apiKey: config.auth?.OPENAI_API_KEY, requiresKey: true, brand: 'CCSwitch' });
|
||||
} else notices.push(provider.name + '(' + provider.app_type + ')不属于已验证的Chat Completions配置,已跳过。');
|
||||
}
|
||||
} finally { db.close(); }
|
||||
|
||||
@@ -2,7 +2,8 @@ import { randomUUID, createHash } from 'node:crypto';
|
||||
import { setTimeout as delay } from 'node:timers/promises';
|
||||
import { ModelRequestError } from '../../server/diagnostics.mjs';
|
||||
import { CredentialVault } from './credential-vault.mjs';
|
||||
import { modelProtocol, capabilityDeclarations, capabilityView } from '../../shared/contracts/model-capabilities.mjs';
|
||||
import { modelProtocol, capabilityDeclarations, capabilityView, capabilityRejections } from '../../shared/contracts/model-capabilities.mjs';
|
||||
import { actualRequest, requestVariants } from '../../shared/contracts/model-request.mjs';
|
||||
|
||||
const legacyId = 'legacy-default';
|
||||
const stamp = () => new Date().toISOString();
|
||||
@@ -109,12 +110,50 @@ export class ModelHub {
|
||||
status: !connection.enabled ? 'disabled' : !ready ? 'needs_key' : health?.blocked ? 'needs_attention' : health?.cooldownUntil > Date.now() ? 'cooling' : 'configured',
|
||||
activeRequests: this.leases.get(connection.id) ?? 0, health: health ?? { state: 'unverified' },
|
||||
streaming: connection.streaming === true, capabilitySchema: 1, capabilityDeclarations: capabilityDeclarations(undefined, connection.capabilityDeclarations),
|
||||
capabilities: capabilityView(connection, credential?.revision ?? 0) };
|
||||
...(this.state.capabilityResults?.[connection.id] ? { capabilityEvidence: this.state.capabilityResults[connection.id] } : {}),
|
||||
capabilities: capabilityView({ ...connection, capabilityEvidence: this.state.capabilityResults?.[connection.id] }, credential?.revision ?? 0) };
|
||||
}
|
||||
view() { return { revision: this.state.revision, selected: this.state.selected, connections: this.connections().map(value => this.status(value)), pools: this.state.pools,
|
||||
imports: Object.entries(this.state.imports).map(([id, value]) => ({ id, ...value })), attempts: this.attempts.slice(-100).reverse(), waiting: this.waiting,
|
||||
storage: { credentials: this.credentialVault ? 'memory-or-windows-user-dpapi' : 'memory', restart: '临时凭据在宿主退出后需要重新填写;已明确本机加密保存的连接按用户与原配置加载。', attemptsRetained: 1000 } }; }
|
||||
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'].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 };
|
||||
if (!this.available(member)) throw error('该连接不可用,请核对凭据、冷却或停用状态。');
|
||||
member.streaming = scope === 'streaming';
|
||||
const nonce = randomUUID(), tools = scope === 'tools' ? [{ type: 'function', function: { name: 'workbench_capability_echo',
|
||||
description: 'Return the provided nonce in this capability test. No files or commands are available.',
|
||||
parameters: { type: 'object', properties: { nonce: { type: 'string' } }, required: ['nonce'], additionalProperties: false } } }] : [];
|
||||
const config = { ...this.store.config, mode: 'compatible', model: connection.model, baseUrl: connection.baseUrl,
|
||||
protocol: modelProtocol(connection.protocol), maxOutputTokens: 256, capabilityProbe: true, modelRouting: { type: 'connection', id: connection.id, revision: connection.revision,
|
||||
strategy: 'round-robin', sticky: false, maxAttempts: 1, queueTimeoutMs: 1000, members: [member] } };
|
||||
let attemptId;
|
||||
const result = await this.provider.complete(config, [{ role: 'user', content: scope === 'tools' ? 'Call workbench_capability_echo with nonce ' + nonce + '. Do not reply with plain text.' : 'Reply OK.' }], null, undefined,
|
||||
event => { if (event.modelAttemptId) attemptId = event.modelAttemptId; }, null, tools, { attempts: 1 });
|
||||
if (scope === 'tools') {
|
||||
const calls = result.message.tool_calls;
|
||||
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 (this.connection(id).revision !== connection.revision || (this.credential(connection)?.revision ?? 0) !== member.credentialRevision) throw error('检测期间连接已变化,原结果保留但不更新能力结论。', 409);
|
||||
this.editRegistry(() => {
|
||||
this.state.capabilityResults ??= {};
|
||||
const previous = this.state.capabilityResults[id];
|
||||
const results = previous?.connectionRevision === connection.revision && previous.credentialRevision === member.credentialRevision ? { ...previous.results } : {};
|
||||
results[scope] = 'verified';
|
||||
const attempt = this.attempts.find(value => value.id === attemptId);
|
||||
if (!attempt || attempt.connectionId !== id || attempt.status !== 'completed') throw error('检测原请求账本不完整,未更新能力结论。');
|
||||
this.state.capabilityResults[id] = { schema: 1, connectionRevision: connection.revision, credentialRevision: member.credentialRevision, results,
|
||||
checkedAt: stamp(), protocol: modelProtocol(connection.protocol), attemptId: attempt?.id, source: 'explicit-model-probe',
|
||||
scope: 'Only the requested synthetic capability; no project files, commands or real-model engineering benefit.' };
|
||||
this.save();
|
||||
});
|
||||
return { ok: true, usage: result.usage, scope, center: this.view() };
|
||||
} finally { this.probing = false; }
|
||||
}
|
||||
upsert(input) {
|
||||
if (!this.editingRegistry) return this.editRegistry(() => this.upsert(input));
|
||||
if (input.id === legacyId) throw error('原连接请通过模型连接设置修改。');
|
||||
@@ -183,10 +222,10 @@ export class ModelHub {
|
||||
const selection = this.state.selected;
|
||||
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), weight: value.weight,
|
||||
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 }));
|
||||
if (!members.some(value => this.status(value).status === 'configured' || this.status(value).status === 'cooling')) throw error('没有可用模型连接,请核对启用状态、临时凭据和故障。');
|
||||
return { ...config, baseUrl: members[0].baseUrl, model: members[0].model,
|
||||
return { ...config, baseUrl: members[0].baseUrl, model: members[0].model, protocol: modelProtocol(members[0].protocol), streaming: members[0].streaming === true,
|
||||
contextLimit: Math.min(config.contextLimit, ...members.map(value => value.contextLimit)), maxOutputTokens: Math.min(config.maxOutputTokens, ...members.map(value => value.maxOutputTokens)),
|
||||
modelRouting: { type: selection.type, id: selection.id, revision: pool?.revision ?? members[0].revision,
|
||||
strategy: pool?.strategy ?? 'round-robin', sticky: pool?.sticky ?? true, maxAttempts: pool?.maxAttempts ?? 1, queueTimeoutMs: pool?.queueTimeoutMs ?? 15000, members } };
|
||||
@@ -197,13 +236,15 @@ export class ModelHub {
|
||||
const health = this.health.get(this.identity(member));
|
||||
return (!member.requiresKey || !!this.credential(member)?.key) && !health?.blocked && !(health?.cooldownUntil > Date.now());
|
||||
}
|
||||
async acquire(routing, runId, excluded, signal, deadline) {
|
||||
async acquire(routing, runId, excluded, signal, deadline, requirements = []) {
|
||||
if (this.waiting >= 100) throw error('模型请求队列已满,请稍后继续任务。');
|
||||
this.waiting++;
|
||||
try {
|
||||
while (true) {
|
||||
signal?.throwIfAborted();
|
||||
const healthy = routing.members.filter(value => this.available(value) && !excluded.has(value.id));
|
||||
const eligible = routing.members.filter(value => capabilityRejections(value, requirements, value.credentialRevision).length === 0);
|
||||
if (!eligible.length) throw Object.assign(error('模型池没有满足本次任务能力的成员;请核对工具调用和流式能力声明。'), { rejections: routing.members.flatMap(value => capabilityRejections(value, requirements, value.credentialRevision)) });
|
||||
const healthy = eligible.filter(value => this.available(value) && !excluded.has(value.id));
|
||||
const ready = healthy.filter(value => (this.leases.get(value.id) ?? 0) < value.concurrency);
|
||||
if (ready.length) {
|
||||
const remembered = routing.sticky && this.sticky.get(runId);
|
||||
@@ -231,18 +272,28 @@ export class ModelHub {
|
||||
async completeOwned(config, body, signal, onProgress, plan, execute) {
|
||||
const routing = config.modelRouting, excluded = new Set(), deadline = Date.now() + Math.min(routing.queueTimeoutMs, 60000);
|
||||
const requestId = plan?.id ?? randomUUID(), runId = plan?.runId ?? requestId;
|
||||
const requirements = config.capabilityProbe ? [] : ['text', ...(requestVariants(body).some(value => value.tools?.length) ? ['tools'] : [])];
|
||||
const rejections = config.capabilityProbe ? [] : routing.members.flatMap(value => capabilityRejections(value, [...requirements, ...(value.streaming ? ['streaming'] : [])], value.credentialRevision));
|
||||
const eligibleRouting = { ...routing, members: routing.members.filter(value => !rejections.some(item => item.connectionId === value.id)) };
|
||||
if (!eligibleRouting.members.length) {
|
||||
const attempt = { id: randomUUID(), requestId, taskId: plan?.taskId ?? null, runId, sessionId: plan?.sessionId ?? null, projectId: plan?.projectId ?? null,
|
||||
poolId: routing.type === 'pool' ? routing.id : null, status: 'rejected', reason: '所有成员均不满足本次任务所需能力。', rejections, startedAt: stamp(), finishedAt: stamp(), usage: null, usageStatus: 'not_dispatched' };
|
||||
this.attempts.push(attempt); this.saveAttempts();
|
||||
throw error('模型池没有满足本次任务能力的成员;请核对能力声明或选择其它连接。');
|
||||
}
|
||||
let lastError;
|
||||
for (let count = 1; count <= routing.maxAttempts; count++) {
|
||||
signal?.throwIfAborted();
|
||||
let lease;
|
||||
try { lease = await this.acquire(routing, runId, excluded, signal, deadline); }
|
||||
try { lease = await this.acquire(eligibleRouting, runId, excluded, signal, deadline, requirements); }
|
||||
catch (cause) { throw lastError ?? cause; }
|
||||
const member = lease.member, credential = this.credential(member);
|
||||
const actualBody = { ...body, model: member.model };
|
||||
const actualBody = actualRequest(body, member);
|
||||
const bytes = JSON.stringify(actualBody);
|
||||
const attempt = { id: randomUUID(), requestId, taskId: plan?.taskId ?? null, runId, sessionId: plan?.sessionId ?? null, projectId: plan?.projectId ?? null,
|
||||
connectionId: member.id, connectionRevision: member.revision, credentialRevision: member.credentialRevision, poolId: routing.type === 'pool' ? routing.id : null,
|
||||
poolRevision: routing.revision, model: member.model, endpoint: member.baseUrl, ordinal: count, status: 'running', startedAt: stamp(),
|
||||
poolRevision: routing.revision, model: member.model, protocol: modelProtocol(member.protocol), streaming: member.streaming === true, endpoint: member.baseUrl, ordinal: count, status: 'running', startedAt: stamp(),
|
||||
requestBytes: Buffer.byteLength(bytes, 'utf8'), requirements, rejections,
|
||||
requestSha256: createHash('sha256').update(bytes).digest('hex'), usage: null, usageStatus: 'unknown' };
|
||||
try {
|
||||
if (plan?.sessionId) {
|
||||
@@ -253,14 +304,30 @@ export class ModelHub {
|
||||
this.store.save(session);
|
||||
}
|
||||
this.attempts.push(attempt); this.saveAttempts();
|
||||
const progress = event => onProgress?.({ ...event, attempt: count, maxAttempts: routing.maxAttempts, connectionId: member.id, connectionName: member.name, model: member.model, modelAttemptId: attempt.id });
|
||||
const result = await execute({ ...config, baseUrl: member.baseUrl, model: member.model }, credential?.key ?? '', actualBody, progress);
|
||||
const progress = event => {
|
||||
if (event.streamCapture !== undefined) {
|
||||
attempt.stream = event.stream;
|
||||
if (plan?.sessionId) {
|
||||
const session = this.store.session(plan.sessionId), run = this.store.owned('run', plan.runId, session.id);
|
||||
const raw = event.streamCapture, decoded = raw.toString('utf8'), redacted = this.provider.redact(decoded);
|
||||
const captured = redacted === decoded ? raw : Buffer.from(redacted, 'utf8');
|
||||
const artifact = this.store.artifactStore.write(session, run, captured, { kind: 'model-stream-response', mediaType: 'text/event-stream' });
|
||||
this.store.core.artifact.set(artifact.id, artifact); attempt.responseArtifactId = artifact.id;
|
||||
attempt.responseSha256 = artifact.sha256; attempt.responseRedacted = redacted !== decoded; this.store.save(session);
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (event.streaming) attempt.partial = { sequence: event.streamSequence, bytes: event.streamBytes, textCharacters: event.textCharacters, textPreview: this.provider.redact(event.textPreview ?? '').slice(0, 8000), toolFragments: event.toolFragments };
|
||||
if (event.stream) attempt.stream = event.stream;
|
||||
onProgress?.({ ...event, attempt: count, maxAttempts: routing.maxAttempts, connectionId: member.id, connectionName: member.name, protocol: modelProtocol(member.protocol), model: member.model, modelAttemptId: attempt.id });
|
||||
};
|
||||
const result = await execute({ ...config, baseUrl: member.baseUrl, model: member.model, protocol: modelProtocol(member.protocol), streaming: member.streaming === true }, credential?.key ?? '', actualBody, progress);
|
||||
attempt.status = 'completed'; attempt.usage = result.usage ?? null; attempt.usageStatus = result.usage ? 'reported' : 'unknown';
|
||||
this.health.set(this.identity(member), { state: 'responded', lastSuccessAt: stamp(), cooldownUntil: 0, failures: 0 });
|
||||
return result;
|
||||
} catch (cause) {
|
||||
const diagnostic = cause.diagnostic;
|
||||
attempt.status = signal?.aborted ? 'interrupted' : 'failed';
|
||||
attempt.status = signal?.aborted ? 'interrupted' : diagnostic?.outcome === 'unknown' ? 'unknown' : 'failed';
|
||||
attempt.reason = this.provider.redact(cause.message).slice(0, 600); attempt.httpStatus = diagnostic?.status ?? diagnostic?.httpStatus ?? null;
|
||||
const health = this.health.get(this.identity(member)) ?? {};
|
||||
if (signal?.aborted) {
|
||||
|
||||
@@ -0,0 +1,128 @@
|
||||
import { createHash } from 'node:crypto';
|
||||
import { protocolFailure, normalizeProtocolResponse, validateModelResponse } from './protocols.mjs';
|
||||
|
||||
// Only a fully terminated and validated stream produces a dispatchable message.
|
||||
// Progress is cumulative, bounded and explicitly not a tool invocation.
|
||||
export async function readModelStream(response, config, signal, onProgress) {
|
||||
if (!response.headers.get('content-type')?.toLowerCase().includes('text/event-stream') || !response.body) throw protocolFailure('服务未返回所选协议的事件流,请核对流式配置。');
|
||||
const reader = response.body.getReader(), decoder = new TextDecoder('utf-8', { fatal: true }), hash = createHash('sha256');
|
||||
const chunks = [];
|
||||
let buffer = '', data = [], bytes = 0, frames = 0, sequence = -1, text = '', done = false, terminal = null, usage = null, finish = null;
|
||||
let lastProgress = 0, responseId = null, validated = false;
|
||||
const calls = new Map(), responseCalls = new Map();
|
||||
const progress = force => {
|
||||
if (!force && Date.now() - lastProgress < 100) return;
|
||||
lastProgress = Date.now();
|
||||
onProgress?.({ phase: 'running', streaming: true, protocol: config.protocol ?? 'chat-completions', streamSequence: frames,
|
||||
streamBytes: bytes, textPreview: text.slice(0, 8000), textCharacters: text.length, toolFragments: Math.max(calls.size, responseCalls.size) });
|
||||
};
|
||||
const processData = raw => {
|
||||
if (terminal || done) throw protocolFailure('模型流在终结后仍包含数据,未采用响应。');
|
||||
frames++;
|
||||
if (frames > 100000) throw protocolFailure('模型事件流超过事件数量上限。');
|
||||
if (raw === '[DONE]') { if (config.protocol === 'responses') throw protocolFailure('Responses缺少正式终结事件。'); done = true; return; }
|
||||
let event;
|
||||
try { event = JSON.parse(raw); } catch { throw protocolFailure('模型事件流包含无效JSON。'); }
|
||||
if (event.error || event.type === 'error') throw protocolFailure('模型流返回错误,已收到片段保留且不会自动重发。', 'stream_interrupted');
|
||||
if (config.protocol === 'responses') {
|
||||
if (event.sequence_number !== undefined) {
|
||||
if (!Number.isSafeInteger(event.sequence_number) || event.sequence_number <= sequence) throw protocolFailure('Responses事件序列重复或逆序。');
|
||||
sequence = event.sequence_number;
|
||||
}
|
||||
if (event.type === 'response.created') {
|
||||
if (!event.response?.id || responseId) throw protocolFailure('Responses流创建身份无效。'); responseId = event.response.id;
|
||||
} else if (event.type === 'response.output_text.delta') {
|
||||
if (typeof event.delta !== 'string') throw protocolFailure('Responses文本片段格式无效。'); text += event.delta;
|
||||
} else if (event.type === 'response.output_item.added' && event.item?.type === 'function_call') {
|
||||
const item = event.item;
|
||||
if (!item.id || !item.call_id || !item.name || responseCalls.has(item.id)) throw protocolFailure('Responses流工具身份重复或缺失。');
|
||||
responseCalls.set(item.id, { id: item.id, call_id: item.call_id, name: item.name, arguments: item.arguments ?? '' });
|
||||
} else if (event.type === 'response.function_call_arguments.delta') {
|
||||
const call = responseCalls.get(event.item_id);
|
||||
if (!call || typeof event.delta !== 'string') throw protocolFailure('Responses工具片段缺少所属原调用。'); call.arguments += event.delta;
|
||||
} else if (event.type === 'response.function_call_arguments.done') {
|
||||
const call = responseCalls.get(event.item_id);
|
||||
if (!call || call.arguments !== event.arguments) throw protocolFailure('Responses终结参数与原片段不一致。');
|
||||
} else if (event.type === 'response.completed') {
|
||||
if (!event.response || responseId && event.response.id !== responseId) throw protocolFailure('Responses终结身份与创建身份不一致。');
|
||||
terminal = event.response;
|
||||
} else if (['response.failed', 'response.incomplete', 'response.cancelled'].includes(event.type)) throw protocolFailure('Responses流未完整结束,工具未执行。', 'stream_interrupted');
|
||||
} else {
|
||||
if (!Array.isArray(event.choices)) throw protocolFailure('Chat流缺少choices数组。');
|
||||
if (event.usage !== undefined && event.usage !== null) usage = event.usage;
|
||||
for (const choice of event.choices) {
|
||||
if (choice.index !== 0) throw protocolFailure('当前只接入单个Chat候选流。');
|
||||
const delta = choice.delta;
|
||||
if (!delta || typeof delta !== 'object') throw protocolFailure('Chat流缺少delta。');
|
||||
if (finish && (delta.content || delta.tool_calls?.length)) throw protocolFailure('Chat流在完成后新增了内容。');
|
||||
if (delta.content !== undefined && delta.content !== null) {
|
||||
if (typeof delta.content !== 'string') throw protocolFailure('Chat文本片段格式无效。'); text += delta.content;
|
||||
}
|
||||
if (delta.refusal) text += delta.refusal;
|
||||
if (delta.tool_calls !== undefined && !Array.isArray(delta.tool_calls)) throw protocolFailure('Chat流工具片段格式无效。');
|
||||
for (const item of delta.tool_calls ?? []) {
|
||||
if (!Number.isSafeInteger(item.index) || item.index < 0 || item.index >= 12) throw protocolFailure('Chat流工具索引无效。');
|
||||
const call = calls.get(item.index) ?? { id: '', type: 'function', function: { name: '', arguments: '' } };
|
||||
if (item.id) { if (call.id && call.id !== item.id) throw protocolFailure('Chat流工具身份发生变化。'); call.id = item.id; }
|
||||
if (item.type && item.type !== 'function') throw protocolFailure('Chat流工具类型未接入。');
|
||||
if (item.function?.name) call.function.name += item.function.name;
|
||||
if (item.function?.arguments !== undefined) {
|
||||
if (typeof item.function.arguments !== 'string') throw protocolFailure('Chat流工具参数格式无效。'); call.function.arguments += item.function.arguments;
|
||||
}
|
||||
calls.set(item.index, call);
|
||||
}
|
||||
if (choice.finish_reason) { if (finish && finish !== choice.finish_reason) throw protocolFailure('Chat流终结原因发生变化。'); finish = choice.finish_reason; }
|
||||
}
|
||||
}
|
||||
if (calls.size > 12 || responseCalls.size > 12) throw protocolFailure('模型流工具数量超出上限。');
|
||||
progress(false);
|
||||
};
|
||||
const line = value => {
|
||||
if (value === '') { if (data.length) { processData(data.join('\n')); data = []; } }
|
||||
else if (value.startsWith('data:')) data.push(value.slice(5).replace(/^ /, ''));
|
||||
};
|
||||
try {
|
||||
while (true) {
|
||||
signal?.throwIfAborted();
|
||||
const chunk = await reader.read();
|
||||
signal?.throwIfAborted();
|
||||
if (chunk.done) break;
|
||||
bytes += chunk.value.length; hash.update(chunk.value);
|
||||
if (bytes > 4 * 1024 * 1024) throw protocolFailure('模型事件流超过4MB读取上限。');
|
||||
chunks.push(Buffer.from(chunk.value));
|
||||
buffer += decoder.decode(chunk.value, { stream: true });
|
||||
// CRLF, LF and CR line endings, including a CR split across chunks.
|
||||
while (true) {
|
||||
const at = buffer.search(/[\r\n]/); if (at < 0 || buffer[at] === '\r' && at === buffer.length - 1) break;
|
||||
const width = buffer[at] === '\r' && buffer[at + 1] === '\n' ? 2 : 1;
|
||||
line(buffer.slice(0, at)); buffer = buffer.slice(at + width);
|
||||
}
|
||||
}
|
||||
buffer += decoder.decode();
|
||||
// A final CR is a complete SSE line ending once EOF rules out CRLF.
|
||||
if (buffer.endsWith('\r')) { line(buffer.slice(0, -1)); buffer = ''; }
|
||||
if (buffer || data.length) throw protocolFailure('模型流在完整事件边界前断开,未采用片段。', 'stream_interrupted');
|
||||
let value;
|
||||
if (config.protocol === 'responses') {
|
||||
if (!terminal) throw protocolFailure('Responses流断开或缺少终结事件。', 'stream_interrupted');
|
||||
value = normalizeProtocolResponse(config, terminal);
|
||||
const received = value.choices[0].message;
|
||||
if (text && received.content !== text) throw protocolFailure('Responses终结文本与原片段不一致。');
|
||||
for (const call of responseCalls.values()) {
|
||||
const final = terminal.output.find(item => item.id === call.id && item.type === 'function_call');
|
||||
if (!final || final.call_id !== call.call_id || final.name !== call.name || final.arguments !== call.arguments) throw protocolFailure('Responses终结工具与原片段不一致。');
|
||||
}
|
||||
value = terminal;
|
||||
} else {
|
||||
if (!done || !['stop', 'tool_calls'].includes(finish)) throw protocolFailure('Chat流未完整终结或达到输出上限,未执行工具。', 'stream_interrupted');
|
||||
value = { choices: [{ message: { role: 'assistant', content: text, ...(calls.size ? { tool_calls: [...calls.entries()].sort((a, b) => a[0] - b[0]).map(([, item]) => item) } : {}) }, finish_reason: finish }], usage };
|
||||
}
|
||||
validateModelResponse(config.protocol === 'responses' ? normalizeProtocolResponse(config, value) : value);
|
||||
validated = true; progress(true);
|
||||
return { value, evidence: { bytes, frames, sha256: hash.copy().digest('hex'), terminal: true, protocol: config.protocol ?? 'chat-completions' } };
|
||||
} finally {
|
||||
try { await reader.cancel(); } catch {} reader.releaseLock();
|
||||
onProgress?.({ streamCapture: Buffer.concat(chunks), stream: { bytes, capturedBytes: chunks.reduce((sum, item) => sum + item.length, 0), frames,
|
||||
sha256: hash.digest('hex'), terminal: validated, terminalSeen: !!terminal || done, protocol: config.protocol ?? 'chat-completions' } });
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
import { ModelRequestError } from '../../server/diagnostics.mjs';
|
||||
export const protocolFailure = (message, kind = 'protocol') => new ModelRequestError({ kind, source: 'model_connection', stage: 'model_request',
|
||||
title: '模型响应未通过完整性核对', message, retryable: false, occurredAt: new Date().toISOString() });
|
||||
export const protocolRoute = config => config.protocol === 'responses' ? '/responses' : '/chat/completions';
|
||||
|
||||
export function validateModelResponse(value) {
|
||||
const choice = value.choices?.[0], message = choice?.message;
|
||||
if (!message || choice.finish_reason === 'length' || choice.finish_reason === 'content_filter') throw protocolFailure('模型响应缺少完整message或没有完整结束,未执行工具。');
|
||||
const calls = message.tool_calls;
|
||||
if (calls !== undefined && (!Array.isArray(calls) || calls.length > 12)) throw protocolFailure('模型工具调用数量或格式无效。');
|
||||
const ids = new Set();
|
||||
for (const call of calls ?? []) {
|
||||
if (!call.id || typeof call.id !== 'string' || ids.has(call.id) || call.type !== 'function' || typeof call.function?.name !== 'string' || !call.function.name || typeof call.function.arguments !== 'string') throw protocolFailure('模型工具身份或参数无效。');
|
||||
ids.add(call.id);
|
||||
let args; try { args = JSON.parse(call.function.arguments); } catch { throw protocolFailure('模型工具参数不是完整JSON,未执行工具。'); }
|
||||
if (!args || typeof args !== 'object' || Array.isArray(args)) throw protocolFailure('模型工具参数必须为完整对象。');
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
export function normalizeProtocolResponse(config, value) {
|
||||
if (config.protocol !== 'responses') return value;
|
||||
if (value.status !== 'completed' || !Array.isArray(value.output)) throw protocolFailure('Responses未完整结束,原响应未用于执行工具。');
|
||||
const calls = [], texts = [], ids = new Set();
|
||||
for (const item of value.output) {
|
||||
if (item.type === 'function_call') {
|
||||
if (item.status && item.status !== 'completed') throw protocolFailure('Responses工具参数未完成。');
|
||||
if (typeof item.call_id !== 'string' || !item.call_id || ids.has(item.call_id) || typeof item.name !== 'string' || !item.name || typeof item.arguments !== 'string') throw protocolFailure('Responses工具调用身份或参数无效。');
|
||||
ids.add(item.call_id); calls.push({ id: item.call_id, type: 'function', function: { name: item.name, arguments: item.arguments } });
|
||||
} else if (item.type === 'message') {
|
||||
if (item.role !== 'assistant' || item.status && item.status !== 'completed' || !Array.isArray(item.content)) throw protocolFailure('Responses消息尚未完成或格式无效。');
|
||||
for (const content of item.content) {
|
||||
if (content.type === 'output_text' && typeof content.text === 'string') texts.push(content.text);
|
||||
else if (content.type === 'refusal' && typeof content.refusal === 'string') texts.push(content.refusal);
|
||||
else throw protocolFailure('Responses包含当前尚未接入的输出内容类型。');
|
||||
}
|
||||
} else if (item.type !== 'reasoning') throw protocolFailure('Responses包含当前尚未接入的服务端工具或输出类型。');
|
||||
}
|
||||
const raw = value.usage, usage = raw ? { prompt_tokens: raw.input_tokens, completion_tokens: raw.output_tokens,
|
||||
...(raw.total_tokens !== undefined ? { total_tokens: raw.total_tokens } : {}), raw, protocol: 'responses' } : null;
|
||||
return { choices: [{ message: { role: 'assistant', content: texts.join(''), ...(calls.length ? { tool_calls: calls } : {}),
|
||||
providerState: { protocol: 'responses', endpoint: config.baseUrl, model: config.model, responseId: value.id ?? null, items: value.output } }, finish_reason: calls.length ? 'tool_calls' : 'stop' }], usage };
|
||||
}
|
||||
@@ -22,7 +22,7 @@ const at = () => new Date().toISOString();
|
||||
const sourceIdentity = source => Object.fromEntries(['root', 'head', 'reference', 'branch', 'gitDir', 'commonDir', 'indexSha256',
|
||||
'statusSha256', 'sourceFingerprint', 'treeSha256', 'configuration', 'filterDrivers'].map(key => [key, source[key]]));
|
||||
const configView = config => Object.fromEntries(['mode', 'baseUrl', 'model', 'contextLimit', 'maxOutputTokens', 'configRevision',
|
||||
'maxSteps', 'maxRunDurationMs', 'maxRunTokens', 'maxTaskTokens', ...(config.modelRouting ? ['modelRouting'] : [])].map(key => [key, structuredClone(config[key])]));
|
||||
'maxSteps', 'maxRunDurationMs', 'maxRunTokens', 'maxTaskTokens', ...(config.protocol ? ['protocol', 'streaming'] : []), ...(config.modelRouting ? ['modelRouting'] : [])].map(key => [key, structuredClone(config[key])]));
|
||||
const alive = pid => { if (!pid) return false; try { process.kill(pid, 0); return true; } catch (error) { return error.code === 'EPERM'; } };
|
||||
|
||||
export class WorkerManager {
|
||||
|
||||
+1
-11
@@ -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
@@ -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) {
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -11,6 +11,7 @@ export function modelSnapshot(config) {
|
||||
mode: config.mode, baseUrl: config.baseUrl, model: config.model,
|
||||
contextLimit: config.contextLimit, maxOutputTokens: config.maxOutputTokens,
|
||||
configRevision: config.configRevision ?? 1,
|
||||
...(config.protocol ? { protocol: config.protocol, streaming: config.streaming === true } : {}),
|
||||
...(config.modelRouting ? { routing: structuredClone(config.modelRouting) } : {}),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -1,15 +1,68 @@
|
||||
import { createHash } from 'node:crypto';
|
||||
import { modelProtocol } from './model-capabilities.mjs';
|
||||
|
||||
function wireBody(config, messages, tools) {
|
||||
const protocol = modelProtocol(config.protocol), stream = config.streaming === true;
|
||||
const cleanMessages = messages.map(({ providerState, ...message }) => message);
|
||||
if (protocol === 'chat-completions') return { model: config.model, messages: cleanMessages, tools, tool_choice: 'auto', max_tokens: config.maxOutputTokens, stream,
|
||||
...(stream ? { stream_options: { include_usage: true } } : {}) };
|
||||
const input = messages.flatMap(message => {
|
||||
if (message.role === 'tool') return [{ type: 'function_call_output', call_id: message.tool_call_id, output: message.content }];
|
||||
if (message.role === 'assistant') {
|
||||
if (message.providerState?.protocol === 'responses' && message.providerState.endpoint === config.baseUrl && message.providerState.model === config.model) return message.providerState.items;
|
||||
return [...(message.content ? [{ role: 'assistant', content: message.content }] : []), ...(message.tool_calls ?? []).map(call => ({
|
||||
type: 'function_call', call_id: call.id, name: call.function.name, arguments: call.function.arguments }))];
|
||||
}
|
||||
return [{ role: message.role, content: message.content }];
|
||||
});
|
||||
return { model: config.model, input, tools: (tools ?? []).map(tool => ({ type: 'function', ...tool.function, strict: false })),
|
||||
tool_choice: 'auto', max_output_tokens: config.maxOutputTokens, stream, store: false, include: ['reasoning.encrypted_content'] };
|
||||
}
|
||||
|
||||
export function completionBody(config, messages, tools) {
|
||||
return { model: config.model, messages, tools, tool_choice: 'auto', max_tokens: config.maxOutputTokens, stream: false };
|
||||
const members = config.modelRouting?.members;
|
||||
if (members?.some(member => member.protocol === 'responses' || member.streaming === true)) {
|
||||
return { schema: 1, kind: 'model-route-plan', messages, variants: members.map(member => ({ connectionId: member.id,
|
||||
protocol: modelProtocol(member.protocol), body: wireBody({ ...config, ...member, maxOutputTokens: config.maxOutputTokens }, messages, tools) })) };
|
||||
}
|
||||
return wireBody(config, messages, tools);
|
||||
}
|
||||
|
||||
export function requestVariants(body) {
|
||||
return body.kind === 'model-route-plan' ? body.variants.map(value => value.body) : [body];
|
||||
}
|
||||
|
||||
export function actualRequest(body, member) {
|
||||
if (body.kind !== 'model-route-plan') return { ...body, model: member.model };
|
||||
const variant = body.variants.find(value => value.connectionId === member.id);
|
||||
if (!variant || variant.protocol !== modelProtocol(member.protocol) || variant.body.model !== member.model) throw new Error('实际连接与已封存协议请求不一致。');
|
||||
return structuredClone(variant.body);
|
||||
}
|
||||
|
||||
export const outputReservation = body => body.max_output_tokens ?? body.max_tokens;
|
||||
|
||||
export function requestHistory(body) {
|
||||
if (body.messages) return body.messages;
|
||||
const messages = [];
|
||||
for (const item of body.input ?? []) {
|
||||
if (item.type === 'reasoning') continue;
|
||||
if (item.type === 'function_call_output') messages.push({ role: 'tool', tool_call_id: item.call_id, content: item.output });
|
||||
else if (item.type === 'function_call') {
|
||||
if (!messages.at(-1)?.tool_calls) messages.push({ role: 'assistant', content: '', tool_calls: [] });
|
||||
messages.at(-1).tool_calls.push({ id: item.call_id, type: 'function', function: { name: item.name, arguments: item.arguments } });
|
||||
} else messages.push({ role: item.role, content: typeof item.content === 'string' ? item.content : JSON.stringify(item.content) });
|
||||
}
|
||||
return messages;
|
||||
}
|
||||
|
||||
export function requestMetrics(body) {
|
||||
const serialized = JSON.stringify(body);
|
||||
const bytes = Buffer.byteLength(serialized, 'utf8');
|
||||
const variants = requestVariants(body);
|
||||
return { bytes, sha256: createHash('sha256').update(serialized, 'utf8').digest('hex'),
|
||||
estimatedInputTokens: Math.ceil(bytes / 3) + body.messages.length * 8 + (body.tools?.length ?? 0) * 16,
|
||||
estimateMethod: 'utf8-third-plus-framing-v1' };
|
||||
estimatedInputTokens: Math.max(...variants.map(value => Math.ceil(Buffer.byteLength(JSON.stringify(value), 'utf8') / 3)
|
||||
+ (value.messages?.length ?? value.input?.length ?? 0) * 8 + (value.tools?.length ?? 0) * 16)),
|
||||
estimateMethod: body.kind === 'model-route-plan' ? 'maximum-wire-variant-utf8-v1' : 'utf8-third-plus-framing-v1' };
|
||||
}
|
||||
|
||||
export function assertPlannedRequest(body, plan) {
|
||||
|
||||
Reference in New Issue
Block a user