feat(models): add native Anthropic Messages and observed endpoint diagnostics
This commit is contained in:
@@ -6,6 +6,7 @@ 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';
|
||||
import { messagesAuthSettings, validateProtocolEndpoint } from '../../shared/contracts/model-endpoint.mjs';
|
||||
|
||||
const digest = value => createHash('sha256').update(JSON.stringify(value)).digest('hex');
|
||||
export class ConfigImporter {
|
||||
@@ -29,6 +30,7 @@ export class ConfigImporter {
|
||||
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,
|
||||
...(item.protocol === 'anthropic-messages' ? { messagesAuth: item.messagesAuth ?? 'api-key' } : {}),
|
||||
baseUrl: item.baseUrl, apiKey: item.apiKey, requiresKey: item.requiresKey !== false, brand: item.brand });
|
||||
}
|
||||
} else if (source === 'ccswitch') {
|
||||
@@ -48,7 +50,7 @@ export class ConfigImporter {
|
||||
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,
|
||||
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配置,已跳过。');
|
||||
} else notices.push(provider.name + '(' + provider.app_type + ')不属于已验证的导入格式,已跳过。');
|
||||
}
|
||||
} finally { db.close(); }
|
||||
} else throw new Error('不支持该来源格式。');
|
||||
@@ -57,7 +59,7 @@ export class ConfigImporter {
|
||||
try {
|
||||
if (!String(row.model ?? '').trim() || !String(row.name ?? '').trim()) throw new Error('缺少名称或模型');
|
||||
if (row.apiKey !== undefined && typeof row.apiKey !== 'string') throw new Error('凭据不是字符串');
|
||||
entries.push({ ...row, name: String(row.name).slice(0, 100), model: String(row.model).slice(0, 200), baseUrl: modelEndpoint(row.baseUrl),
|
||||
entries.push({ ...row, ...messagesAuthSettings(row.protocol, row.messagesAuth), name: String(row.name).slice(0, 100), model: String(row.model).slice(0, 200), baseUrl: validateProtocolEndpoint(row.protocol, modelEndpoint(row.baseUrl)),
|
||||
origin: digest({ source, path: path.resolve(filename), id: row.sourceId }), apiKey: row.apiKey?.trim() ?? '' });
|
||||
} catch { notices.push(String(row.name ?? '某项配置').slice(0, 100) + '缺少有效的接口或模型,已跳过。'); }
|
||||
}
|
||||
|
||||
@@ -23,7 +23,8 @@ export class CredentialVault {
|
||||
binding(connection) {
|
||||
return sha(JSON.stringify(canonicalHost({ app: 'agent-workbench', scope: 'Windows.CurrentUser', userSid: this.userSid,
|
||||
dataIdentity: this.dataIdentity, connectionId: connection.id, connectionRevision: connection.revision,
|
||||
protocol: connection.protocol, baseUrl: connection.baseUrl, model: connection.model, requiresKey: connection.requiresKey })));
|
||||
protocol: connection.protocol, baseUrl: connection.baseUrl, model: connection.model, requiresKey: connection.requiresKey,
|
||||
...(connection.protocol === 'anthropic-messages' ? { messagesAuth: connection.messagesAuth ?? 'api-key' } : {}) })));
|
||||
}
|
||||
status(connection) {
|
||||
if (this.blocked) return { status: 'unavailable', storage: 'windows-user-dpapi', message: '本机保存登记格式损坏或来自新版本,未覆盖原记录。' };
|
||||
|
||||
@@ -0,0 +1,74 @@
|
||||
import { protocolFailure } from './protocols.mjs';
|
||||
|
||||
const object = value => value && typeof value === 'object' && !Array.isArray(value);
|
||||
const fail = message => { throw protocolFailure('Messages 流:' + message); };
|
||||
|
||||
// Each block and the whole message must close before any tool becomes dispatchable.
|
||||
export function messagesStream() {
|
||||
let message = null, active = null, deltaSeen = false, stopped = false, text = '', toolCount = 0;
|
||||
const blocks = [];
|
||||
return {
|
||||
consume(event) {
|
||||
if (event.type === 'ping') return;
|
||||
if (stopped) fail('终结后新增了事件。');
|
||||
if (event.type === 'message_start') {
|
||||
const start = event.message;
|
||||
if (message || start?.type !== 'message' || start.role !== 'assistant' || typeof start.id !== 'string' || !start.id
|
||||
|| !Array.isArray(start.content) || start.content.length || start.stop_reason != null) fail('创建事件重复或消息身份/内容无效。');
|
||||
message = structuredClone(start);
|
||||
} else if (!message) fail('事件缺少原 message_start。');
|
||||
else if (event.type === 'content_block_start') {
|
||||
if (deltaSeen || active !== null || !Number.isSafeInteger(event.index) || event.index !== blocks.length || blocks.length >= 128) fail('内容块索引重复、逆序或上个块未完成。');
|
||||
const block = event.content_block;
|
||||
if (!object(block) || !['text', 'tool_use', 'thinking', 'redacted_thinking'].includes(block.type)) fail('未接入的内容块类型。');
|
||||
if (block.type === 'text' && typeof block.text !== 'string' || block.type === 'tool_use' && (!object(block.input) || typeof block.id !== 'string' || !block.id || typeof block.name !== 'string' || !block.name)
|
||||
|| block.type === 'thinking' && (typeof block.thinking !== 'string' || typeof block.signature !== 'string') || block.type === 'redacted_thinking' && typeof block.data !== 'string') fail('初始内容块格式无效。');
|
||||
if (block.type === 'tool_use' && (++toolCount > 12 || blocks.some(item => item.block.id === block.id))) fail('工具数量超限或身份重复。');
|
||||
blocks.push({ block: structuredClone(block), json: '', jsonSeen: false, closed: false }); active = event.index;
|
||||
if (block.type === 'text') text += block.text;
|
||||
} else if (event.type === 'content_block_delta') {
|
||||
if (active === null || event.index !== active || deltaSeen || !object(event.delta)) fail('内容片段缺少所属未完成块。');
|
||||
const entry = blocks[active], block = entry.block, delta = event.delta;
|
||||
if (block.type === 'text' && delta.type === 'text_delta' && typeof delta.text === 'string') { block.text += delta.text; text += delta.text; }
|
||||
else if (block.type === 'text' && delta.type === 'citations_delta' && object(delta.citation)) (block.citations ??= []).push(structuredClone(delta.citation));
|
||||
else if (block.type === 'tool_use' && delta.type === 'input_json_delta' && typeof delta.partial_json === 'string') {
|
||||
if (Object.keys(block.input).length) fail('工具初始 input 与增量参数同时存在。');
|
||||
entry.jsonSeen = true; entry.json += delta.partial_json;
|
||||
} else if (block.type === 'thinking' && delta.type === 'thinking_delta' && typeof delta.thinking === 'string') block.thinking += delta.thinking;
|
||||
else if (block.type === 'thinking' && delta.type === 'signature_delta' && typeof delta.signature === 'string') block.signature += delta.signature;
|
||||
else fail('增量类型与原内容块不一致或尚未接入。');
|
||||
} else if (event.type === 'content_block_stop') {
|
||||
if (active === null || event.index !== active || deltaSeen) fail('块终结缺少所属原内容。');
|
||||
const entry = blocks[active];
|
||||
if (entry.jsonSeen) {
|
||||
try { entry.block.input = JSON.parse(entry.json); } catch { fail('工具参数不是完整 JSON。'); }
|
||||
if (!object(entry.block.input)) fail('工具参数必须是对象。');
|
||||
}
|
||||
entry.closed = true; active = null;
|
||||
} else if (event.type === 'message_delta') {
|
||||
if (active !== null || !object(event.delta) || !object(event.usage)) fail('消息终结前内容块未完成或 delta/usage 无效。');
|
||||
const reason = event.delta.stop_reason;
|
||||
if (deltaSeen && reason != null && reason !== message.stop_reason) fail('消息停止原因发生变化。');
|
||||
if (reason != null) { message.stop_reason = reason; deltaSeen = true; }
|
||||
message.stop_sequence = event.delta.stop_sequence ?? message.stop_sequence;
|
||||
message.usage ??= {};
|
||||
for (const [key, value] of Object.entries(event.usage)) if (value !== null && value !== undefined) {
|
||||
if (['input_tokens', 'output_tokens', 'cache_creation_input_tokens', 'cache_read_input_tokens'].includes(key)
|
||||
&& (!Number.isSafeInteger(value) || value < 0 || typeof message.usage[key] === 'number' && value < message.usage[key])) fail('累计用量无效或倒退。');
|
||||
message.usage[key] = structuredClone(value);
|
||||
}
|
||||
} else if (event.type === 'message_stop') {
|
||||
if (!deltaSeen || active !== null || blocks.some(entry => !entry.closed)) fail('缺少完整块或停止原因。');
|
||||
stopped = true; message.content = blocks.map(entry => entry.block);
|
||||
} else fail('未接入的事件类型 ' + String(event.type).slice(0, 100) + '。');
|
||||
},
|
||||
get text() { return text; },
|
||||
get tools() { return toolCount; },
|
||||
get usage() { return message?.usage ?? null; },
|
||||
get stopped() { return stopped; },
|
||||
finish() {
|
||||
if (!stopped) throw protocolFailure('Messages 流断开或缺少 message_stop,工具未执行。', 'stream_interrupted');
|
||||
return message;
|
||||
},
|
||||
};
|
||||
}
|
||||
@@ -4,7 +4,8 @@ import { ModelRequestError } from '../../server/diagnostics.mjs';
|
||||
import { CredentialVault } from './credential-vault.mjs';
|
||||
import { visionProbe } from './vision-probe.mjs';
|
||||
import { modelProtocol, capabilityDeclarations, capabilityView, capabilityRejections } from '../../shared/contracts/model-capabilities.mjs';
|
||||
import { actualRequest, requestVariants } from '../../shared/contracts/model-request.mjs';
|
||||
import { actualRequest, requestVariants, requestImages } from '../../shared/contracts/model-request.mjs';
|
||||
import { messagesAuthSettings, validateProtocolEndpoint, modelRequestUrl } from '../../shared/contracts/model-endpoint.mjs';
|
||||
import { modelWaitSettings } from './request-deadline.mjs';
|
||||
import { sanitizeModelText } from './upstream-error.mjs';
|
||||
import { nativeParameterSettings } from '../../shared/contracts/native-parameters.mjs';
|
||||
@@ -186,7 +187,8 @@ export class ModelHub {
|
||||
if (!name || !model) throw error('请填写连接名称和实际模型名称。');
|
||||
const connection = { id: previous?.id ?? randomUUID(), name, brand: label(input.brand || '自定义', 40), protocol, model,
|
||||
capabilitySchema: 1, capabilityDeclarations: declarations, streaming, ...(native ?? {}), ...modelIdentitySettings(input, previous ?? {}),
|
||||
baseUrl: modelEndpoint(input.baseUrl), enabled: input.enabled !== false, requiresKey: input.requiresKey !== false,
|
||||
...messagesAuthSettings(protocol, input.messagesAuth ?? previous?.messagesAuth),
|
||||
baseUrl: validateProtocolEndpoint(protocol, modelEndpoint(input.baseUrl)), enabled: input.enabled !== false, requiresKey: input.requiresKey !== false,
|
||||
contextLimit: bounded(input.contextLimit, 16000, 1000, 200000), maxOutputTokens: bounded(input.maxOutputTokens, 2048, 256, 16000),
|
||||
concurrency: bounded(input.concurrency, 1, 1, 8), revision: (previous?.revision ?? 0) + 1,
|
||||
source: previous?.source ?? 'manual', ...(previous?.importOrigin ? { importOrigin: previous.importOrigin, importRevision: previous.importRevision } : {}),
|
||||
@@ -249,7 +251,7 @@ export class ModelHub {
|
||||
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, protocol: modelProtocol(members[0].protocol), streaming: members[0].streaming === true,
|
||||
return { ...config, ...messagesAuthSettings(members[0].protocol, members[0].messagesAuth), baseUrl: members[0].baseUrl, model: members[0].model, protocol: modelProtocol(members[0].protocol), streaming: members[0].streaming === true,
|
||||
nativeParameters: members[0].nativeParameters ?? {}, outputTokenField: members[0].outputTokenField,
|
||||
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,
|
||||
@@ -298,7 +300,7 @@ export class ModelHub {
|
||||
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'] : []),
|
||||
...(requestVariants(body).some(value => (value.messages??value.input??[]).some(message=>Array.isArray(message.content)&&message.content.some(part=>['image_url','input_image'].includes(part.type))))?['vision']:[])];
|
||||
...(requestImages(body).length ? ['vision'] : [])];
|
||||
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) {
|
||||
@@ -319,7 +321,8 @@ export class ModelHub {
|
||||
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, protocol: modelProtocol(member.protocol), streaming: member.streaming === true, 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,
|
||||
requestUrl: modelRequestUrl(member), ...(member.protocol === 'anthropic-messages' ? messagesAuthSettings(member.protocol, member.messagesAuth) : {}), 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 {
|
||||
@@ -366,7 +369,7 @@ export class ModelHub {
|
||||
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, ...modelIdentitySettings(member), baseUrl: member.baseUrl, model: member.model, protocol: modelProtocol(member.protocol), streaming: member.streaming === true }, credential?.key ?? '', actualBody, progress);
|
||||
const result = await execute({ ...config, ...modelIdentitySettings(member), ...messagesAuthSettings(member.protocol, member.messagesAuth), 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 ?? attempt.usage; attempt.usageStatus = attempt.usage ? 'reported' : 'unknown';
|
||||
if (result.compatibility?.length) attempt.compatibility = result.compatibility;
|
||||
this.health.set(this.identity(member), { state: 'responded', lastSuccessAt: stamp(), cooldownUntil: 0, failures: 0 });
|
||||
|
||||
@@ -2,6 +2,7 @@ import { createHash } from 'node:crypto';
|
||||
import { protocolFailure, normalizeProtocolResponse, validateModelResponse, reportedUsage } from './protocols.mjs';
|
||||
import { httpFailure } from '../../server/diagnostics.mjs';
|
||||
import { upstreamCapture } from './upstream-error.mjs';
|
||||
import { messagesStream } from './messages-stream.mjs';
|
||||
|
||||
// Only a fully terminated and validated stream produces a dispatchable message.
|
||||
// Progress is cumulative, bounded and explicitly not a tool invocation.
|
||||
@@ -13,6 +14,8 @@ export async function readModelStream(response, config, signal, onProgress, tool
|
||||
let lastProgress = 0, responseId = null, validated = false, progressTimer = null, progressFailure = null;
|
||||
let upstreamFailure = null;
|
||||
const calls = new Map(), responseCalls = new Map();
|
||||
const messages = config.protocol === 'anthropic-messages' ? messagesStream() : null;
|
||||
let eventName = '';
|
||||
const progress = force => {
|
||||
if (!force && Date.now() - lastProgress < 100) {
|
||||
progressTimer ??= setTimeout(() => {
|
||||
@@ -24,17 +27,19 @@ export async function readModelStream(response, config, signal, onProgress, tool
|
||||
if (progressTimer) { clearTimeout(progressTimer); progressTimer = null; }
|
||||
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) });
|
||||
streamBytes: bytes, textPreview: text.slice(0, 8000), textCharacters: text.length, toolFragments: Math.max(calls.size, responseCalls.size, messages?.tools ?? 0) });
|
||||
};
|
||||
const processData = raw => {
|
||||
if (terminal || done) throw protocolFailure('模型流在终结后仍包含数据,未采用响应。');
|
||||
if (terminal || done || messages?.stopped) throw protocolFailure('模型流在终结后仍包含数据,未采用响应。');
|
||||
frames++;
|
||||
if (frames > 100000) throw protocolFailure('模型事件流超过事件数量上限。');
|
||||
if (raw === '[DONE]') { if (config.protocol === 'responses') throw protocolFailure('Responses缺少正式终结事件。'); done = true; return; }
|
||||
if (raw === '[DONE]') { if (config.protocol !== 'chat-completions' && config.protocol) throw protocolFailure('所选协议缺少正式终结事件。'); done = true; return; }
|
||||
let event;
|
||||
try { event = JSON.parse(raw); } catch { throw protocolFailure('模型事件流包含无效JSON。'); }
|
||||
observeIdentity?.(event);
|
||||
if (event.usage) usage = event.usage;
|
||||
if (!event || typeof event !== 'object' || Array.isArray(event)) throw protocolFailure('模型事件格式无效。');
|
||||
if (messages && eventName && eventName !== event.type) throw protocolFailure('Messages SSE 事件名称与数据类型不一致。');
|
||||
if (event.usage && !messages) usage = event.usage;
|
||||
const failUpstream = () => {
|
||||
usage = event.response?.usage ?? usage;
|
||||
const { details } = upstreamCapture({ raw: Buffer.from(raw, 'utf8'), event, scope: 'sse-event', status: response.status, headers: response.headers }, redact);
|
||||
@@ -46,7 +51,9 @@ export async function readModelStream(response, config, signal, onProgress, tool
|
||||
throw upstreamFailure;
|
||||
};
|
||||
if (event.error || event.type === 'error') failUpstream();
|
||||
if (config.protocol === 'responses') {
|
||||
if (messages) {
|
||||
messages.consume(event); text = messages.text; usage = messages.usage;
|
||||
} else 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;
|
||||
@@ -102,8 +109,9 @@ export async function readModelStream(response, config, signal, onProgress, tool
|
||||
progress(false);
|
||||
};
|
||||
const line = value => {
|
||||
if (value === '') { if (data.length) { processData(data.join('\n')); data = []; } }
|
||||
if (value === '') { if (data.length) { processData(data.join('\n')); data = []; } eventName = ''; }
|
||||
else if (value.startsWith('data:')) data.push(value.slice(5).replace(/^ /, ''));
|
||||
else if (value.startsWith('event:')) eventName = value.slice(6).trim();
|
||||
};
|
||||
try {
|
||||
while (true) {
|
||||
@@ -129,7 +137,9 @@ export async function readModelStream(response, config, signal, onProgress, tool
|
||||
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 (messages) {
|
||||
value = messages.finish(); terminal = value;
|
||||
} else if (config.protocol === 'responses') {
|
||||
if (!terminal) throw protocolFailure('Responses流断开或缺少终结事件。', 'stream_interrupted');
|
||||
value = normalizeProtocolResponse(config, terminal);
|
||||
const received = value.choices[0].message;
|
||||
@@ -143,14 +153,14 @@ export async function readModelStream(response, config, signal, onProgress, tool
|
||||
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, tools);
|
||||
validateModelResponse(config.protocol !== 'chat-completions' && config.protocol ? normalizeProtocolResponse(config, value) : value, tools);
|
||||
validated = true; progress(true);
|
||||
return { value, evidence: { bytes, frames, sha256: hash.copy().digest('hex'), terminal: true, protocol: config.protocol ?? 'chat-completions' } };
|
||||
} finally {
|
||||
if (progressTimer) clearTimeout(progressTimer);
|
||||
try { await reader.cancel(); } catch {} reader.releaseLock();
|
||||
onProgress?.({ streamCapture: Buffer.concat(chunks), ...(upstreamFailure ? { upstream: upstreamFailure.diagnostic.upstream } : {}),
|
||||
reportedUsage: reportedUsage(config, { usage: terminal?.usage ?? usage }), stream: { bytes, capturedBytes: chunks.reduce((sum, item) => sum + item.length, 0), frames,
|
||||
sha256: hash.digest('hex'), terminal: validated, terminalSeen: !!terminal || done || !!upstreamFailure, protocol: config.protocol ?? 'chat-completions' } });
|
||||
reportedUsage: reportedUsage(config, { usage: terminal?.usage ?? messages?.usage ?? usage }), stream: { bytes, capturedBytes: chunks.reduce((sum, item) => sum + item.length, 0), frames,
|
||||
sha256: hash.digest('hex'), terminal: validated, terminalSeen: !!terminal || done || messages?.stopped || !!upstreamFailure, protocol: config.protocol ?? 'chat-completions' } });
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,11 +1,20 @@
|
||||
import { ModelRequestError } from '../../server/diagnostics.mjs';
|
||||
export const protocolFailure = (message, kind = 'protocol') => new ModelRequestError({ kind, source: 'model_connection', origin: 'local', stage: 'model_request',
|
||||
title: '模型响应未通过完整性核对', message, retryable: false, occurredAt: new Date().toISOString() });
|
||||
export const protocolRoute = config => config.protocol === 'responses' ? '/responses' : '/chat/completions';
|
||||
export { protocolRoute } from '../../shared/contracts/model-endpoint.mjs';
|
||||
|
||||
export function reportedUsage(config, value) {
|
||||
const raw = value?.usage;
|
||||
if (!raw || typeof raw !== 'object') return null;
|
||||
if (config.protocol === 'anthropic-messages') {
|
||||
const valid = number => Number.isSafeInteger(number) && number >= 0;
|
||||
const cached = raw.cache_read_input_tokens ?? 0, created = raw.cache_creation_input_tokens ?? 0;
|
||||
if (![raw.input_tokens, raw.output_tokens, cached, created].every(valid)) return null;
|
||||
const input = raw.input_tokens + cached + created, total = input + raw.output_tokens;
|
||||
if (!Number.isSafeInteger(total)) return null;
|
||||
return { prompt_tokens: input, completion_tokens: raw.output_tokens, total_tokens: total,
|
||||
prompt_tokens_details: { cached_tokens: cached, cache_creation_tokens: created }, raw: structuredClone(raw), protocol: 'anthropic-messages' };
|
||||
}
|
||||
const input = config.protocol === 'responses' ? raw.input_tokens : raw.prompt_tokens;
|
||||
const output = config.protocol === 'responses' ? raw.output_tokens : raw.completion_tokens;
|
||||
const valid = number => Number.isSafeInteger(number) && number >= 0;
|
||||
@@ -42,6 +51,27 @@ export function validateModelResponse(value, tools) {
|
||||
}
|
||||
|
||||
export function normalizeProtocolResponse(config, value) {
|
||||
if (config.protocol === 'anthropic-messages') {
|
||||
if (value?.type !== 'message' || typeof value.id !== 'string' || !value.id || value.role !== 'assistant' || !Array.isArray(value.content)
|
||||
|| !['end_turn', 'tool_use', 'stop_sequence', 'refusal'].includes(value.stop_reason)) throw protocolFailure('Messages 未完整结束或停止原因为输出/上下文上限、暂停或未知;原响应未用于执行工具。');
|
||||
const calls = [], texts = [];
|
||||
for (const block of value.content) {
|
||||
if (!block || typeof block !== 'object' || Array.isArray(block)) throw protocolFailure('Messages 内容块格式无效。');
|
||||
if (block.type === 'text' && typeof block.text === 'string') texts.push(block.text);
|
||||
else if (block.type === 'tool_use') {
|
||||
if (typeof block.id !== 'string' || !block.id || typeof block.name !== 'string' || !block.name || !block.input || typeof block.input !== 'object' || Array.isArray(block.input)) throw protocolFailure('Messages 工具身份或 input 对象无效。');
|
||||
calls.push({ id: block.id, type: 'function', function: { name: block.name, arguments: JSON.stringify(block.input) } });
|
||||
} else if (block.type === 'thinking' && typeof block.thinking === 'string' && typeof block.signature === 'string' && block.signature) {
|
||||
// Opaque provider history is retained for this endpoint/model, not exposed as assistant text.
|
||||
} else if (block.type === 'redacted_thinking' && typeof block.data === 'string' && block.data) {
|
||||
// Keep the upstream opaque block unchanged for a subsequent tool round.
|
||||
} else throw protocolFailure('Messages 包含未接入或不完整的内容块:' + String(block?.type ?? '未知').slice(0, 100) + '。');
|
||||
}
|
||||
if ((value.stop_reason === 'tool_use') !== (calls.length > 0)) throw protocolFailure('Messages 停止原因与工具内容不一致。');
|
||||
return { choices: [{ message: { role: 'assistant', content: texts.join(''), ...(calls.length ? { tool_calls: calls } : {}),
|
||||
providerState: { protocol: 'anthropic-messages', endpoint: config.baseUrl, model: config.model, responseId: value.id, content: structuredClone(value.content) } },
|
||||
finish_reason: calls.length ? 'tool_calls' : 'stop' }], usage: reportedUsage(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();
|
||||
|
||||
Reference in New Issue
Block a user