feat(models): retain native cache blocks and bind cross-run provider state

This commit is contained in:
Agent Workbench Dev
2026-11-07 20:00:33 +08:00
parent 71f1805561
commit 9e1feeebb3
11 changed files with 435 additions and 33 deletions
+8 -6
View File
@@ -1,6 +1,7 @@
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 : {}) });
@@ -94,18 +95,19 @@ export function normalizeProtocolResponse(config, value) {
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: { protocol: 'anthropic-messages', endpoint: config.baseUrl, model: config.model, responseId: value.id, content: structuredClone(value.content) } },
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;
if (received?.reasoning_content === undefined || received.reasoning_content === null) return { ...value, usage: reportedUsage(config, value) };
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必须为字符串,原响应未用于执行工具。');
const normalized = structuredClone(value), target = normalized.choices[0].message;
normalized.usage = reportedUsage(config, value);
delete target.reasoning_content;
target.providerState = { protocol: 'chat-completions', endpoint: config.baseUrl, model: config.model,
connectionId: config.modelConnectionId ?? null, credentialRevision: config.modelCredentialRevision ?? null, reasoningContent: received.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();
@@ -127,5 +129,5 @@ export function normalizeProtocolResponse(config, value) {
}
const usage = reportedUsage(config, value);
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 };
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 };
}