import { unrestricted, ceiling } from '../../shared/contracts/execution-policy.mjs'; import { ModelRequestError } from '../../server/diagnostics.mjs'; import { withTokenAccounting } from '../../shared/contracts/model-usage.mjs'; import { bindProviderState } from '../../shared/contracts/model-provider-state.mjs'; export const protocolFailure = (message, kind = 'protocol', extra = null) => new ModelRequestError({ kind, source: 'model_connection', origin: 'local', stage: 'model_request', title: '模型响应未通过完整性核对', message, retryable: false, occurredAt: new Date().toISOString(), ...(extra ? extra : {}) }); // A response stopped at the model's output-token ceiling is never partially executed: // even a syntactically complete leading tool call is dropped, because any later call in // the same turn may be cut mid-argument. The diagnostic must tell the model what happened // and how to proceed, instead of a generic incompleteness message it cannot act on. export const OUTPUT_TRUNCATED_HINT = '上游模型在本次配置的输出额度处停止(finish_reason=length),本次响应整体未执行工具,原响应和用量已保留。请拆分输出:已有文件使用propose_patch;单个长文件用propose_file_chunk分多轮追加较短的完整片段,最后complete=true生成完整提案。已有成功片段使用原draft_id/next_offset/draft_sha256继续,不从头重复。不要在一个响应中堆叠多个大工具参数。原目标和已读依据保留。'; export const outputTruncatedFailure = (kind = 'protocol') => protocolFailure(OUTPUT_TRUNCATED_HINT, kind, { reason: 'output_truncated' }); export { protocolRoute } from '../../shared/contracts/model-endpoint.mjs'; export function reportedUsage(config, value) { return withTokenAccounting(baseReportedUsage(config, value)); } function baseReportedUsage(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, ...(raw.cache_read_input_tokens !== undefined || raw.cache_creation_input_tokens !== undefined ? { prompt_tokens_details: { ...(raw.cache_read_input_tokens !== undefined ? { cached_tokens: cached } : {}), ...(raw.cache_creation_input_tokens !== undefined ? { 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; if (!valid(raw.total_tokens) && !(valid(input) && valid(output))) return null; if (!valid(raw.total_tokens) && !valid(input + output)) return null; const cached = config.protocol === 'responses' ? raw.input_tokens_details?.cached_tokens : raw.prompt_tokens_details?.cached_tokens ?? raw.prompt_cache_hit_tokens; const cacheDetails = valid(cached) && valid(input) && cached <= input ? { cached_tokens: cached } : null; const reasoning = config.protocol === 'responses' ? raw.output_tokens_details?.reasoning_tokens : raw.completion_tokens_details?.reasoning_tokens; return { ...(valid(input) ? { prompt_tokens: input } : {}), ...(valid(output) ? { completion_tokens: output } : {}), total_tokens: valid(raw.total_tokens) ? raw.total_tokens : input + output, ...(cacheDetails ? { prompt_tokens_details: cacheDetails } : {}), ...(valid(reasoning) && valid(output) && reasoning <= output ? { completion_tokens_details: { reasoning_tokens: reasoning } } : {}), raw: structuredClone(raw), protocol: config.protocol ?? 'chat-completions' }; } function acceptsEmptyObject(tools, name) { const tool = tools?.find(tool => (tool.function?.name ?? tool.name) === name); const schema = tool?.function?.parameters ?? tool?.parameters; // Restrict adaptation to schemas whose empty-object validity is evident. return schema?.type === 'object' && !schema.required?.length && Object.keys(schema).every(key => ['type', 'properties', 'required', 'additionalProperties', 'description', 'title', '$schema'].includes(key)); } export function validateModelResponse(value, tools, config = {}) { const choice = value.choices?.[0], message = choice?.message; if (choice?.finish_reason === 'length') throw outputTruncatedFailure(); if (!message || choice.finish_reason === 'content_filter') throw protocolFailure('模型响应缺少完整message或因内容过滤未完整结束,未执行工具。'); const calls = message.tool_calls; if (calls !== undefined && (!Array.isArray(calls) || !unrestricted(config) && 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); if (call.function.arguments.trim() === '' && acceptsEmptyObject(tools, call.function.name)) { (value.compatibility ??= []).push({ kind: 'empty_object_arguments', callId: call.id, tool: call.function.name, originalArguments: call.function.arguments, arguments: '{}' }); call.function.arguments = '{}'; } let args; try { args = JSON.parse(call.function.arguments); } catch { throw protocolFailure('工具 ' + call.function.name + '(' + call.id + ')参数不是完整JSON,未执行这组工具。'); } if (!args || typeof args !== 'object' || Array.isArray(args)) throw protocolFailure('工具 ' + call.function.name + '(' + call.id + ')参数必须为完整对象,未执行这组工具。'); } return value; } export function normalizeProtocolResponse(config, value) { if (config.protocol === 'anthropic-messages') { if (value?.type === 'message' && value.stop_reason === 'max_tokens') throw outputTruncatedFailure(); 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 停止原因与工具内容不一致。'); if (!calls.length && !texts.join('').trim()) throw protocolFailure('Messages 返回为空:没有可用文本或工具调用。原响应及用量已保留,请核对供应商响应。'); return { choices: [{ message: { role: 'assistant', content: texts.join(''), ...(calls.length ? { tool_calls: calls } : {}), providerState: bindProviderState(config, { 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') { const received = value?.choices?.[0]?.message; const normalized = structuredClone(value), target = normalized?.choices?.[0]?.message; if (target) { delete target.providerState; delete target.providerInput; } if (received?.reasoning_content === undefined || received.reasoning_content === null) return { ...normalized, usage: reportedUsage(config, value) }; if (typeof received.reasoning_content !== 'string') throw protocolFailure('Chat reasoning_content必须为字符串,原响应未用于执行工具。'); normalized.usage = reportedUsage(config, value); delete target.reasoning_content; target.providerState = bindProviderState(config, { protocol: 'chat-completions', endpoint: config.baseUrl, model: config.model, connectionId: config.modelConnectionId ?? null, credentialRevision: config.modelCredentialRevision ?? null, reasoningContent: received.reasoning_content }); return normalized; } if (value.status === 'incomplete' && value.incomplete_details?.reason === 'max_output_tokens') throw outputTruncatedFailure(); 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包含当前尚未接入的输出内容类型:' + String(content?.type ?? '未标注').slice(0, 100) + '。'); } } else if (item.type !== 'reasoning') throw protocolFailure('Responses包含当前尚未接入的服务端工具或输出类型:' + String(item.type ?? '未标注').slice(0, 100) + '。'); } const usage = reportedUsage(config, value); return { choices: [{ message: { role: 'assistant', content: texts.join(''), ...(calls.length ? { tool_calls: calls } : {}), providerState: bindProviderState(config, { protocol: 'responses', endpoint: config.baseUrl, model: config.model, responseId: value.id ?? null, items: structuredClone(value.output) }) }, finish_reason: calls.length ? 'tool_calls' : 'stop' }], usage }; }