fix(models): retain upstream identity and pause strict mismatches

This commit is contained in:
Agent Workbench Dev
2026-11-03 11:53:07 +08:00
parent 410b347894
commit 3659f5e858
25 changed files with 427 additions and 35 deletions
+8 -3
View File
@@ -8,6 +8,7 @@ import { actualRequest, requestVariants } from '../../shared/contracts/model-req
import { modelWaitSettings } from './request-deadline.mjs';
import { sanitizeModelText } from './upstream-error.mjs';
import { nativeParameterSettings } from '../../shared/contracts/native-parameters.mjs';
import { modelIdentitySettings } from '../../shared/contracts/model-identity.mjs';
const legacyId = 'legacy-default';
const stamp = () => new Date().toISOString();
@@ -178,7 +179,7 @@ export class ModelHub {
const name = label(input.name), model = label(input.model, 200);
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 ?? {}),
capabilitySchema: 1, capabilityDeclarations: declarations, streaming, ...(native ?? {}), ...modelIdentitySettings(input, previous ?? {}),
baseUrl: 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,
@@ -325,9 +326,11 @@ export class ModelHub {
}
this.attempts.push(attempt); this.saveAttempts();
const progress = event => {
if (event.modelIdentity) attempt.modelIdentity = event.modelIdentity;
if (event.wireAttemptId) attempt.wireAttemptId = event.wireAttemptId;
if (event.upstreamResponse) attempt.upstreamResponse = event.upstreamResponse;
if (event.consumption) {
if (event.consumption.modelIdentity) attempt.modelIdentity = event.consumption.modelIdentity;
attempt.usage = event.consumption.usage; attempt.usageStatus = event.consumption.usageStatus;
onProgress?.({ consumption: { ...event.consumption, wireAttemptId: event.consumption.attemptId, attemptId: attempt.id,
connectionId: member.id, connectionName: member.name }, connectionId: member.id, model: member.model, modelAttemptId: attempt.id });
@@ -343,6 +346,8 @@ export class ModelHub {
const artifact = this.store.artifactStore.write(session, run, captured, { kind: event.streamCapture !== undefined ? 'model-stream-response' : 'model-http-response',
mediaType: event.streamCapture !== undefined ? 'text/event-stream' : 'text/plain' });
this.store.core.artifact.set(artifact.id, artifact); attempt.responseArtifactId = artifact.id;
if (attempt.modelIdentity) attempt.modelIdentity.responseSource = { artifactId: artifact.id, sha256: artifact.sha256, sessionId: session.id,
scope: event.streamCapture !== undefined ? 'sse-stream' : 'http-body' };
attempt.responseSha256 = artifact.sha256; attempt.responseRedacted = redacted !== decoded || event.upstream?.capture?.redacted === true; this.store.save(session);
if (event.upstream) Object.assign(event.upstream, { artifactId: artifact.id, artifactSha256: artifact.sha256,
artifactScope: event.streamCapture !== undefined ? 'sse-stream' : 'http-body', sessionId: session.id });
@@ -355,7 +360,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, 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), 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 });
@@ -371,7 +376,7 @@ export class ModelHub {
// the attempt, release its lease, and leave the connection eligible.
this.health.set(this.identity(member), { ...health, state: 'interrupted', lastInterruptedAt: stamp() });
} else {
const blocked = [401, 403].includes(attempt.httpStatus) || diagnostic?.kind === 'quota';
const blocked = [401, 403].includes(attempt.httpStatus) || ['quota', 'model_identity'].includes(diagnostic?.kind);
const failedAt = Date.now();
this.health.set(this.identity(member), { state: blocked ? 'needs_attention' : 'failed', blocked, lastFailureAt: new Date(failedAt).toISOString(),
failures: (health.failures ?? 0) + 1, cooldownUntil: failedAt + (diagnostic?.retryAfterMs ?? Math.min(60000, 1000 * 2 ** Math.min(health.failures ?? 0, 5))), reason: attempt.reason });
+2 -1
View File
@@ -5,7 +5,7 @@ import { upstreamCapture } from './upstream-error.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, tools, onBytes, redact = String) {
export async function readModelStream(response, config, signal, onProgress, tools, onBytes, redact = String, observeIdentity) {
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 = [];
@@ -33,6 +33,7 @@ export async function readModelStream(response, config, signal, onProgress, tool
if (raw === '[DONE]') { if (config.protocol === 'responses') throw protocolFailure('Responses缺少正式终结事件。'); done = true; return; }
let event;
try { event = JSON.parse(raw); } catch { throw protocolFailure('模型事件流包含无效JSON。'); }
observeIdentity?.(event);
if (event.usage) usage = event.usage;
const failUpstream = () => {
usage = event.response?.usage ?? usage;