feat(models): retain native cache blocks and bind cross-run provider state
This commit is contained in:
@@ -10,6 +10,7 @@ import { modelReservation,taskConsumption,verifyModelReservation } from '../../s
|
||||
import { fileContinuation } from './read-continuation.mjs';
|
||||
import { prefixAudit } from './prefix-audit.mjs';
|
||||
import { requestContractAudit } from '../models/request-contract-audit.mjs';
|
||||
import { providerStateAudit } from '../../shared/contracts/model-provider-state.mjs';
|
||||
import { observeRequest, providerHistory, requestPath } from './request-observation.mjs';
|
||||
import { verifyToolSurface } from './tool-surface.mjs';
|
||||
import { isPassthrough } from '../../shared/contracts/model-capacity.mjs';
|
||||
@@ -425,6 +426,7 @@ export class RequestPlanner {
|
||||
requestArtifactId: requestArtifact.id,
|
||||
prefixAudit: prefix,
|
||||
...(contractAudit ? { requestContractAudit: contractAudit } : {}),
|
||||
...(config.mode === 'compatible' ? { nativeStateAudit: providerStateAudit(config, messages) } : {}),
|
||||
requestObservation: observation,
|
||||
...(progressReminder ? { progressReminder } : {}),
|
||||
...(preserve ? { contextLayout: 'stable-prefix-1', historyPolicy: 'preserve', contextTailArtifactId: tailArtifact?.id ?? null, contextTailSha256: tailArtifact?.sha256 ?? null } : {}),
|
||||
@@ -499,10 +501,17 @@ export function verifyPlanEvidence(store, session, plan, cache = new Map()) {
|
||||
historyGroups(requestHistory(body));
|
||||
for (const variant of requestVariants(body)) historyGroups(requestHistory(variant));
|
||||
const frozen = JSON.parse(store.artifactStore.read(get(plan.artifactId, 'context-plan')));
|
||||
for (const key of ['contextLayout', 'historyPolicy', 'contextTailArtifactId', 'contextTailSha256', 'prefixAudit', 'requestContractAudit', 'requestObservation', 'progressReminder']) {
|
||||
for (const key of ['contextLayout', 'historyPolicy', 'contextTailArtifactId', 'contextTailSha256', 'prefixAudit', 'requestContractAudit', 'requestObservation', 'progressReminder', 'nativeStateAudit']) {
|
||||
if (JSON.stringify(frozen[key] ?? null) !== JSON.stringify(plan[key] ?? null)) throw new Error('前缀或历史策略与发送前封存不匹配。');
|
||||
}
|
||||
verifyReminderMessage(store, session, plan, body);
|
||||
if (plan.nativeStateAudit) {
|
||||
const run = store.owned('run', plan.runId, session.id);
|
||||
const original = providerHistory(session, run).slice(0, plan.originalMessages);
|
||||
const retained = historyGroups(original).filter(group => plan.retainedGroups.includes(group.index)).flatMap(group => group.messages);
|
||||
const expected = providerStateAudit({ ...run.model, modelRouting: run.model.routing }, retained);
|
||||
if (JSON.stringify(expected) !== JSON.stringify(plan.nativeStateAudit)) throw new Error('原生历史状态与冻结来源或供应商身份不匹配。');
|
||||
}
|
||||
if (plan.requestObservation) {
|
||||
const run = store.owned('run', plan.runId, session.id);
|
||||
const expected = observeRequest(store, session, run, providerHistory(session, run).slice(0, plan.originalMessages), plan.requestObservation.toolSurface);
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
import { providerStateBinding } from '../../shared/contracts/model-provider-state.mjs';
|
||||
|
||||
export const affinityScope = routing => routing.sticky === false ? 'none' : routing.affinityScope ?? 'run';
|
||||
const memberScope = member => providerStateBinding({ ...member, modelConnectionId: member.id, modelCredentialRevision: member.credentialRevision });
|
||||
|
||||
export function savedAffinity(store, routing, plan) {
|
||||
const scope = affinityScope(routing);
|
||||
if (!['task', 'session'].includes(scope) || !plan?.sessionId || !plan?.runId) return { scope, record: null };
|
||||
const session = store.session(plan.sessionId), run = store.core.run.get(plan.runId);
|
||||
if (run?.purpose === 'diagnostic' || !run || run.sessionId !== session.id) return { scope: 'run', record: null, reason: 'independent-run-path' };
|
||||
const scopeId = scope === 'task' ? plan.taskId : session.id;
|
||||
const original = session.modelAffinity;
|
||||
if (original != null && (original.schemaVersion !== 1 || !Array.isArray(original.bindings)
|
||||
|| original.bindings.some(value => !value || typeof value !== 'object' || Array.isArray(value))))
|
||||
return { scope, scopeId, session, record: null, reason: 'unknown-affinity-state', retainedUnknown: true };
|
||||
const record = original?.bindings.find(value => value.scope === scope && value.scopeId === scopeId
|
||||
&& value.routingType === routing.type && value.routingId === routing.id) ?? null;
|
||||
return { scope, scopeId, session, record };
|
||||
}
|
||||
|
||||
export function affinityPreference(store, routing, plan, members) {
|
||||
const saved = savedAffinity(store, routing, plan), record = saved.record;
|
||||
if (!record) return { ...saved, preferredId: null, reason: saved.reason ?? (saved.scope === 'none' ? 'disabled' : 'no-saved-affinity') };
|
||||
const member = members.find(value => value.id === record.connectionId);
|
||||
return member && memberScope(member).fingerprint === record.providerScopeFingerprint
|
||||
? { ...saved, preferredId: member.id, reason: 'saved-provider-scope-match' }
|
||||
: { ...saved, preferredId: null, reason: member ? 'saved-provider-scope-changed' : 'saved-member-not-eligible' };
|
||||
}
|
||||
|
||||
export function rememberAffinity(store, routing, member, plan) {
|
||||
const saved = savedAffinity(store, routing, plan);
|
||||
if (!saved.session || !saved.scopeId) return;
|
||||
if (saved.retainedUnknown) return { status: 'retained-unknown', reason: saved.reason };
|
||||
const record = { scope: saved.scope, scopeId: saved.scopeId, routingType: routing.type, routingId: routing.id,
|
||||
connectionId: member.id, model: member.model, protocol: member.protocol ?? 'chat-completions',
|
||||
providerScopeFingerprint: memberScope(member).fingerprint, sourceRunId: plan.runId,
|
||||
lastRequestId: plan.id, updatedAt: new Date().toISOString(), source: 'completed-model-response' };
|
||||
const original = saved.session.modelAffinity ?? { schemaVersion: 1, bindings: [] };
|
||||
saved.session.modelAffinity = { ...original, schemaVersion: 1, bindings: [...original.bindings.filter(value => !(value.scope === record.scope && value.scopeId === record.scopeId
|
||||
&& value.routingType === record.routingType && value.routingId === record.routingId)), record] };
|
||||
store.save(saved.session, { type: 'model.affinity_recorded', detail: { scope: record.scope, sourceRunId: plan.runId, connectionId: member.id,
|
||||
previous: saved.record, current: record } });
|
||||
return { status: 'recorded', scope: record.scope, sourceRunId: plan.runId };
|
||||
}
|
||||
@@ -19,6 +19,7 @@ import { capacitySettings, routedCapacity } from '../../shared/contracts/model-c
|
||||
import { ModelRequestLedger } from './request-ledger.mjs';
|
||||
import { modelOverrideSettings, resolveModelSpecification } from '../../shared/contracts/official-model-registry.mjs';
|
||||
import { defaultMessagesAuth, effectiveRequestContract, requestContractSettings, requestCredentialIdentity } from '../../shared/contracts/model-request-contract.mjs';
|
||||
import { affinityScope, affinityPreference, rememberAffinity } from './model-affinity.mjs';
|
||||
|
||||
const legacyId = 'legacy-default';
|
||||
const stamp = () => new Date().toISOString();
|
||||
@@ -325,8 +326,10 @@ export class ModelHub {
|
||||
seen.add(value.connectionId); return { connectionId: value.connectionId, weight: bounded(value.weight, 1, 1, 10) };
|
||||
});
|
||||
if (!['round-robin', 'weighted'].includes(input.strategy || 'round-robin')) throw error('请选择轮询或权重策略。');
|
||||
const scope = input.affinityScope ?? (previous ? previous.affinityScope ?? 'run' : 'session');
|
||||
if (!['none', 'run', 'task', 'session'].includes(scope)) throw error('请选择关闭、当前运行、当前任务或当前会话的连接保持范围。');
|
||||
const value = { id: previous?.id ?? randomUUID(), name: label(input.name, ceiling(this.store, 100)), members, strategy: input.strategy || 'round-robin', sticky: input.sticky !== false,
|
||||
maxAttempts: bounded(input.maxAttempts, 3, 1, 8), queueTimeoutMs: bounded(input.queueTimeoutMs, 15000, 100, 60000), revision: (previous?.revision ?? 0) + 1 };
|
||||
affinityScope: scope, maxAttempts: bounded(input.maxAttempts, 3, 1, 8), queueTimeoutMs: bounded(input.queueTimeoutMs, 15000, 100, 60000), revision: (previous?.revision ?? 0) + 1 };
|
||||
this.state.pools = [...this.state.pools.filter(item => item.id !== value.id), value]; this.save(); return value;
|
||||
}
|
||||
select(input, config = null) {
|
||||
@@ -363,7 +366,7 @@ export class ModelHub {
|
||||
modelConnectionId: members[0].id, modelCredentialRevision: members[0].credentialRevision,
|
||||
...routedCapacity(config, members),
|
||||
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 } };
|
||||
strategy: pool?.strategy ?? 'round-robin', sticky: pool?.sticky ?? true, affinityScope: pool?.affinityScope ?? 'run', maxAttempts: pool?.maxAttempts ?? 1, queueTimeoutMs: pool?.queueTimeoutMs ?? 15000, members } };
|
||||
}
|
||||
available(member) {
|
||||
const current = this.connections().find(value => value.id === member.id);
|
||||
@@ -372,7 +375,7 @@ export class ModelHub {
|
||||
const health = this.health.get(this.identity(member));
|
||||
return unrestricted(this.store) || (!member.requiresKey || !!this.credential(member)?.key) && !health?.blocked && !(health?.cooldownUntil > Date.now());
|
||||
}
|
||||
async acquire(routing, runId, excluded, signal, deadline, requirements = [], { probe = false } = {}) {
|
||||
async acquire(routing, runId, excluded, signal, deadline, requirements = [], { probe = false, plan = null } = {}) {
|
||||
if (!unrestricted(this.store) && this.waiting >= 100) throw error('模型请求队列已满,请稍后继续任务。');
|
||||
this.waiting++;
|
||||
try {
|
||||
@@ -386,17 +389,21 @@ export class ModelHub {
|
||||
: '模型池暂时没有可用成员;已完成操作保留,请核对凭据、停用状态和冷却时间。'), { routingDecision: decision });
|
||||
const ready = healthy.filter(value => unrestricted(this.store) || (value.concurrency === 0 || (this.leases.get(value.id) ?? 0) < value.concurrency));
|
||||
if (ready.length) {
|
||||
const remembered = routing.sticky && this.sticky.get(runId);
|
||||
const affinity = affinityPreference(this.store, routing, plan, ready);
|
||||
const remembered = affinityScope(routing) !== 'none' && (this.sticky.get(runId) ?? affinity.preferredId);
|
||||
let selected = ready.find(value => value.id === remembered);
|
||||
if (!selected) {
|
||||
const weighted = ready.flatMap(value => Array(routing.strategy === 'weighted' ? value.weight : 1).fill(value));
|
||||
const cursor = this.cursors.get(routing.id) ?? 0; selected = weighted[cursor % weighted.length]; this.cursors.set(routing.id, cursor + 1);
|
||||
}
|
||||
this.leases.set(selected.id, (this.leases.get(selected.id) ?? 0) + 1);
|
||||
this.sticky.set(runId, selected.id); if (this.sticky.size > 1000) this.sticky.delete(this.sticky.keys().next().value);
|
||||
if (affinityScope(routing) !== 'none') this.sticky.set(runId, selected.id);
|
||||
let released = false;
|
||||
decision.selectedConnectionId = selected.id;
|
||||
decision.selectedBy = remembered === selected.id ? 'sticky' : routing.strategy;
|
||||
decision.affinity = { scope: affinity.scope, reason: affinity.reason, previousConnectionId: affinity.record?.connectionId ?? null,
|
||||
sourceRunId: affinity.record?.sourceRunId ?? null, selectedConnectionId: selected.id,
|
||||
continued: remembered === selected.id, source: affinity.preferredId === selected.id ? 'saved-completed-response' : remembered === selected.id ? 'current-run' : 'routing-strategy' };
|
||||
decision.candidates.find(value => value.connectionId === selected.id).disposition = 'selected';
|
||||
return { member: selected, decision, release: () => { if (!released) { released = true; this.leases.set(selected.id, Math.max(0, (this.leases.get(selected.id) ?? 0) - 1)); } } };
|
||||
}
|
||||
@@ -420,7 +427,7 @@ export class ModelHub {
|
||||
signal?.throwIfAborted();
|
||||
beforeDispatch?.({previousFailure:lastError?.diagnostic});
|
||||
let lease;
|
||||
try { lease = await this.acquire(routing, runId, excluded, signal, deadline, requirements, { probe: config.capabilityProbe === true }); }
|
||||
try { lease = await this.acquire(routing, runId, excluded, signal, deadline, requirements, { probe: config.capabilityProbe === true, plan }); }
|
||||
catch (cause) {
|
||||
if (!lastError && cause.routingDecision) {
|
||||
this.addAttempt({ id: randomUUID(), requestId, taskId: plan?.taskId ?? null, runId, sessionId: plan?.sessionId ?? null, projectId: plan?.projectId ?? null,
|
||||
@@ -439,6 +446,8 @@ export class ModelHub {
|
||||
requestBytes: Buffer.byteLength(bytes, 'utf8'), requirements, routingDecision: lease.decision,
|
||||
rejections: lease.decision.candidates.filter(value => value.disposition === 'excluded').flatMap(value => value.reasons.map(reason => ({ connectionId: value.connectionId, connectionName: value.connectionName, ...reason }))),
|
||||
requestSha256: createHash('sha256').update(bytes).digest('hex'), usage: null, usageStatus: 'unknown' };
|
||||
if (plan?.nativeStateAudit) attempt.nativeStateAudit = { source: 'frozen-plan-provider-scope-for-selected-request', requestPlanId: plan.id,
|
||||
...structuredClone(plan.nativeStateAudit.variants.find(value => value.connectionId === member.id) ?? {}) };
|
||||
try {
|
||||
if (plan?.sessionId) {
|
||||
const session = this.store.session(plan.sessionId), run = this.store.owned('run', plan.runId, session.id);
|
||||
@@ -490,6 +499,12 @@ export class ModelHub {
|
||||
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 });
|
||||
if (!config.capabilityProbe) {
|
||||
// Continuity is optional metadata. A persistence failure cannot turn a
|
||||
// completed upstream response into a failed/replayed model request.
|
||||
try { const saved = rememberAffinity(this.store, routing, member, plan); if (saved) attempt.affinityPersistence = saved; }
|
||||
catch (cause) { attempt.affinityPersistence = { status: 'unknown', reason: this.provider.redact(cause.message) }; }
|
||||
}
|
||||
return result;
|
||||
} catch (cause) {
|
||||
const diagnostic = cause.diagnostic;
|
||||
|
||||
@@ -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 };
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { isPassthrough, messagesOutputSetting } from './model-capacity.mjs';
|
||||
import { providerStateCompatibility } from './model-provider-state.mjs';
|
||||
const object = value => value && typeof value === 'object' && !Array.isArray(value);
|
||||
const fail = message => { throw new Error('Messages 请求未发送:' + message); };
|
||||
|
||||
@@ -6,9 +7,12 @@ function contentBlocks(content) {
|
||||
if (typeof content === 'string') return content ? [{ type: 'text', text: content }] : [];
|
||||
if (!Array.isArray(content)) fail('消息内容必须为文本或已接入的内容块。');
|
||||
return content.map(part => {
|
||||
if (['text', 'input_text', 'output_text'].includes(part.type) && typeof part.text === 'string') return { type: 'text', text: part.text };
|
||||
if (['text', 'input_text', 'output_text'].includes(part.type) && typeof part.text === 'string') return { ...structuredClone(part), type: 'text' };
|
||||
const url = part.type === 'image_url' ? part.image_url?.url : part.type === 'input_image' ? part.image_url : null;
|
||||
if (typeof url === 'string' && /^data:image\/png;base64,[A-Za-z0-9+/=]+$/.test(url)) return { type: 'image', source: { type: 'base64', media_type: 'image/png', data: url.slice(22) } };
|
||||
if (typeof url === 'string' && /^data:image\/png;base64,[A-Za-z0-9+/=]+$/.test(url)) {
|
||||
const { image_url, ...metadata } = structuredClone(part);
|
||||
return { ...metadata, type: 'image', source: { type: 'base64', media_type: 'image/png', data: url.slice(22) } };
|
||||
}
|
||||
fail('未接入的输入内容块 ' + String(part?.type ?? '未知') + '。');
|
||||
});
|
||||
}
|
||||
@@ -23,14 +27,23 @@ export function messagesBody(config, messages, tools, native) {
|
||||
else wire.push({ role, content });
|
||||
};
|
||||
for (const message of messages) {
|
||||
if (message.providerInput?.protocol === 'anthropic-messages') {
|
||||
const original = message.providerInput;
|
||||
if (message.role === 'system') system.push(...structuredClone(original.content));
|
||||
else if (message.role === 'tool') append('user', [structuredClone(original.toolResult)]);
|
||||
else append(message.role, structuredClone(original.content));
|
||||
continue;
|
||||
}
|
||||
if (message.role === 'system') { system.push(...contentBlocks(message.content)); continue; }
|
||||
if (message.role === 'tool') {
|
||||
if (typeof message.tool_call_id !== 'string' || !message.tool_call_id || typeof message.content !== 'string') fail('工具结果缺少原调用身份或文本。');
|
||||
append('user', [{ type: 'tool_result', tool_use_id: message.tool_call_id, content: message.content }]);
|
||||
append('user', [{ type: 'tool_result', tool_use_id: message.tool_call_id, content: message.content,
|
||||
...(message.cache_control !== undefined ? { cache_control: structuredClone(message.cache_control) } : {}),
|
||||
...(message.is_error !== undefined ? { is_error: message.is_error } : {}) }]);
|
||||
} else if (message.role === 'user') append('user', contentBlocks(message.content));
|
||||
else if (message.role === 'assistant') {
|
||||
const state = message.providerState;
|
||||
if (state?.protocol === 'anthropic-messages' && state.endpoint === config.baseUrl && state.model === config.model) {
|
||||
if (state?.protocol === 'anthropic-messages' && providerStateCompatibility(config, state).applicable) {
|
||||
if (!Array.isArray(state.content)) fail('原生助手历史格式无效。');
|
||||
append('assistant', structuredClone(state.content));
|
||||
continue;
|
||||
@@ -46,8 +59,10 @@ export function messagesBody(config, messages, tools, native) {
|
||||
}
|
||||
if (system.some(block => block.type !== 'text')) fail('系统消息只支持文本。');
|
||||
const definitions = (tools ?? []).map(tool => {
|
||||
if (typeof tool.name === 'string' && object(tool.input_schema)) return structuredClone(tool);
|
||||
if (tool.type !== 'function' || !tool.function?.name || !object(tool.function.parameters)) fail('工具定义格式无效。');
|
||||
return { name: tool.function.name, ...(tool.function.description ? { description: tool.function.description } : {}), input_schema: tool.function.parameters };
|
||||
const { type, function: definition, ...metadata } = structuredClone(tool), { parameters, ...fields } = definition;
|
||||
return { ...fields, ...metadata, name: definition.name, input_schema: parameters };
|
||||
});
|
||||
return { model: config.model, ...(system.length ? { system } : {}), messages: wire,
|
||||
...(definitions.length ? { tools: definitions, tool_choice: { type: 'auto' } } : {}),
|
||||
@@ -61,14 +76,25 @@ export function messagesBody(config, messages, tools, native) {
|
||||
export function messagesHistory(body) {
|
||||
const history = [];
|
||||
if (typeof body.system === 'string') history.push({ role: 'system', content: body.system });
|
||||
else for (const part of body.system ?? []) history.push({ role: 'system', content: part.text });
|
||||
else if (body.system?.length) history.push({ role: 'system', content: body.system.filter(part => part.type === 'text').map(part => part.text).join(''),
|
||||
providerInput: { protocol: 'anthropic-messages', content: structuredClone(body.system) } });
|
||||
for (const message of body.messages) {
|
||||
if (!Array.isArray(message.content)) { history.push(message); continue; }
|
||||
const blocks = message.content, text = blocks.filter(block => block.type === 'text').map(block => block.text).join('');
|
||||
const calls = blocks.filter(block => block.type === 'tool_use').map(block => ({ id: block.id, type: 'function', function: { name: block.name, arguments: JSON.stringify(block.input) } }));
|
||||
const results = blocks.filter(block => block.type === 'tool_result');
|
||||
for (const result of results) history.push({ role: 'tool', tool_call_id: result.tool_use_id, content: result.content });
|
||||
if (message.role === 'assistant' || text || blocks.some(block => block.type === 'image')) history.push({ role: message.role, content: text, ...(calls.length ? { tool_calls: calls } : {}) });
|
||||
if (!Array.isArray(message.content)) { history.push(structuredClone(message)); continue; }
|
||||
let pending = [];
|
||||
const flush = () => {
|
||||
if (!pending.length) return;
|
||||
const text = pending.filter(block => block.type === 'text').map(block => block.text).join('');
|
||||
const calls = pending.filter(block => block.type === 'tool_use').map(block => ({ id: block.id, type: 'function', function: { name: block.name, arguments: JSON.stringify(block.input) } }));
|
||||
history.push({ role: message.role, content: text, ...(calls.length ? { tool_calls: calls } : {}),
|
||||
providerInput: { protocol: 'anthropic-messages', content: structuredClone(pending) } });
|
||||
pending = [];
|
||||
};
|
||||
for (const block of message.content) {
|
||||
if (block.type !== 'tool_result') { pending.push(block); continue; }
|
||||
flush(); history.push({ role: 'tool', tool_call_id: block.tool_use_id, content: typeof block.content === 'string' ? block.content : JSON.stringify(block.content),
|
||||
providerInput: { protocol: 'anthropic-messages', toolResult: structuredClone(block) } });
|
||||
}
|
||||
flush();
|
||||
}
|
||||
return history;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
import { createHash } from 'node:crypto';
|
||||
import { modelProtocol } from './model-capabilities.mjs';
|
||||
import { modelRequestUrl } from './model-endpoint.mjs';
|
||||
import { requestHeaders } from './model-request-contract.mjs';
|
||||
|
||||
const sha = value => createHash('sha256').update(JSON.stringify(value)).digest('hex');
|
||||
export function providerStateBinding(config) {
|
||||
const protocol = modelProtocol(config.protocol), connection = { ...config, protocol };
|
||||
const identity = { protocol, endpoint: config.baseUrl ?? null, requestUrl: config.baseUrl ? modelRequestUrl(connection) : null,
|
||||
model: config.model ?? null, connectionId: config.modelConnectionId ?? null, credentialRevision: config.modelCredentialRevision ?? null,
|
||||
headers: [...requestHeaders(connection, '{{credential}}')] };
|
||||
return { schemaVersion: 1, ...identity, fingerprint: sha(identity), representation: 'provider-state-scope-credential-template-v1' };
|
||||
}
|
||||
export const bindProviderState = (config, payload) => ({ ...payload, schemaVersion: 2, binding: providerStateBinding(config) });
|
||||
|
||||
export function providerStateCompatibility(config, state) {
|
||||
if (!state) return { applicable: false, reason: 'no-state' };
|
||||
const binding = providerStateBinding(config);
|
||||
if (state.schemaVersion !== undefined && ![1, 2].includes(state.schemaVersion)) return { applicable: false, reason: 'unknown-state-schema', binding };
|
||||
if (state.schemaVersion === 2) return state.binding?.fingerprint === binding.fingerprint
|
||||
? { applicable: true, reason: 'same-provider-scope', binding }
|
||||
: { applicable: false, reason: 'provider-scope-changed', binding, previousFingerprint: state.binding?.fingerprint ?? null };
|
||||
if (state.protocol !== binding.protocol || state.endpoint !== binding.endpoint || state.model !== binding.model)
|
||||
return { applicable: false, reason: 'legacy-provider-changed', binding };
|
||||
if (state.protocol === 'chat-completions') return state.connectionId === binding.connectionId && state.credentialRevision === binding.credentialRevision
|
||||
? { applicable: true, reason: 'legacy-chat-identity-match', binding }
|
||||
: { applicable: false, reason: 'legacy-provider-changed', binding };
|
||||
// Original unbound direct-protocol histories remain readable. A named current
|
||||
// connection cannot infer authentication/version scope from an old bare state.
|
||||
return binding.connectionId === null && binding.credentialRevision === null
|
||||
? { applicable: true, reason: 'legacy-direct-protocol', binding }
|
||||
: { applicable: false, reason: 'legacy-identity-unavailable', binding };
|
||||
}
|
||||
|
||||
export function providerStateAudit(config, messages) {
|
||||
const variants = config.modelRouting?.members ?? [config];
|
||||
return { schemaVersion: 1, source: 'frozen-history-provider-scope', variants: variants.map(member => {
|
||||
const route = config.modelRouting ? { ...config, ...member, modelConnectionId: member.id, modelCredentialRevision: member.credentialRevision } : config;
|
||||
const states = messages.flatMap(message => message.providerState ? [{ protocol: message.providerState.protocol,
|
||||
responseId: message.providerState.responseId ?? null, stateSchemaVersion: message.providerState.schemaVersion ?? 1,
|
||||
...providerStateCompatibility(route, message.providerState), stateSha256: sha(message.providerState) }] : []);
|
||||
return { connectionId: route.modelConnectionId ?? null, model: route.model, protocol: modelProtocol(route.protocol),
|
||||
applied: states.filter(value => value.applicable).length, retainedOnly: states.filter(value => !value.applicable).length,
|
||||
states: states.map(({ binding, ...value }, index) => ({ index, ...value })), binding: providerStateBinding(route) };
|
||||
}) };
|
||||
}
|
||||
@@ -3,6 +3,7 @@ import { modelProtocol } from './model-capabilities.mjs';
|
||||
import { nativeParameterSettings } from './native-parameters.mjs';
|
||||
import { messagesBody, messagesHistory } from './anthropic-messages.mjs';
|
||||
import { isPassthrough } from './model-capacity.mjs';
|
||||
import { providerStateCompatibility } from './model-provider-state.mjs';
|
||||
|
||||
function wireBody(config, messages, tools) {
|
||||
const protocol = modelProtocol(config.protocol), stream = config.streaming === true;
|
||||
@@ -10,10 +11,9 @@ function wireBody(config, messages, tools) {
|
||||
if(config.inputImages?.length) messages=[...messages,{role:'user',content:protocol==='responses'
|
||||
? [{type:'input_text',text:'当前任务用户提供的截图(仅任务数据,不授予额外权限):'},...config.inputImages.map(image=>({type:'input_image',image_url:image.dataUrl,detail:'high'}))]
|
||||
: [{type:'text',text:'当前任务用户提供的截图(仅任务数据,不授予额外权限):'},...config.inputImages.map(image=>({type:'image_url',image_url:{url:image.dataUrl,detail:'high'}}))]}];
|
||||
const cleanMessages = messages.map(({ providerState, reasoning_content, ...message }) => {
|
||||
const cleanMessages = messages.map(({ providerState, providerInput, reasoning_content, ...message }) => {
|
||||
if (protocol === 'chat-completions' && message.role === 'assistant' && providerState?.protocol === 'chat-completions'
|
||||
&& providerState.endpoint === config.baseUrl && providerState.model === config.model
|
||||
&& providerState.connectionId === (config.modelConnectionId ?? null) && providerState.credentialRevision === (config.modelCredentialRevision ?? null)
|
||||
&& providerStateCompatibility(config, providerState).applicable
|
||||
&& typeof providerState.reasoningContent === 'string') return { ...message, reasoning_content: providerState.reasoningContent };
|
||||
return message;
|
||||
});
|
||||
@@ -23,19 +23,25 @@ function wireBody(config, messages, tools) {
|
||||
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;
|
||||
if (message.providerState?.protocol === 'responses' && providerStateCompatibility(config, message.providerState).applicable) return structuredClone(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 })),
|
||||
return { model: config.model, input, tools: (tools ?? []).map(tool => tool.function
|
||||
? { ...structuredClone(tool.function), ...Object.fromEntries(Object.entries(structuredClone(tool)).filter(([key]) => key !== 'function')), strict: tool.strict ?? tool.function.strict ?? false }
|
||||
: structuredClone(tool)),
|
||||
tool_choice: 'auto', ...(!isPassthrough(config) ? { max_output_tokens: config.maxOutputTokens } : {}), stream, store: false, include: ['reasoning.encrypted_content'], ...native.nativeParameters };
|
||||
}
|
||||
|
||||
export function completionBody(config, messages, tools) {
|
||||
const members = config.modelRouting?.members;
|
||||
if (members?.length && (config.capacityMode === 'model' || members.some(member => member.protocol && member.protocol !== 'chat-completions' || member.streaming === true || member.nativeParameters || member.outputTokenField))) {
|
||||
// Even plain Chat members need separate projections when an opaque history
|
||||
// exists. Replacing only body.model would reuse the first member's reasoning
|
||||
// on another connection, credential or model.
|
||||
if (members?.length && (config.capacityMode === 'model' || messages.some(message => message.providerState)
|
||||
|| members.some(member => member.protocol && member.protocol !== 'chat-completions' || member.streaming === true || member.nativeParameters || member.outputTokenField))) {
|
||||
return { schema: 1, kind: 'model-route-plan', messages, variants: members.map(member => ({ connectionId: member.id,
|
||||
protocol: modelProtocol(member.protocol), body: wireBody({ ...config, ...member, nativeParameters: member.nativeParameters ?? {}, outputTokenField: member.outputTokenField,
|
||||
modelConnectionId: member.id, modelCredentialRevision: member.credentialRevision,
|
||||
|
||||
Reference in New Issue
Block a user