577 lines
49 KiB
JavaScript
577 lines
49 KiB
JavaScript
import { unrestricted, ceiling } from '../../shared/contracts/execution-policy.mjs';
|
|
import { randomUUID, createHash } from 'node:crypto';
|
|
import { setTimeout as delay } from 'node:timers/promises';
|
|
import { ModelRequestError } from '../../server/diagnostics.mjs';
|
|
import { CredentialVault } from './credential-vault.mjs';
|
|
import { visionProbe } from './vision-probe.mjs';
|
|
import { modelProtocol, capabilityDeclarations, capabilityView } from '../../shared/contracts/model-capabilities.mjs';
|
|
import { modelRoutingDecision } from '../../shared/contracts/model-routing.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';
|
|
import { assertModelParameterSupport } from '../../shared/contracts/model-parameter-profiles.mjs';
|
|
import { modelIdentitySettings } from '../../shared/contracts/model-identity.mjs';
|
|
import { NetworkDiagnostics } from './network-diagnostics.mjs';
|
|
import { ModelCatalog, credentialScope } from './model-catalog.mjs';
|
|
import { capacitySettings, routedCapacity } from '../../shared/contracts/model-capacity.mjs';
|
|
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();
|
|
const error = (message, status = 400) => Object.assign(new Error(message), { status });
|
|
const bounded = (value, fallback, min, max) => Number.isFinite(Number(value)) ? Math.max(min, Math.min(max, Math.trunc(Number(value)))) : fallback;
|
|
const label = (value, maximum = 100) => String(value ?? '').trim().slice(0, maximum);
|
|
const credentialProfile = connection => JSON.stringify([connection.protocol, connection.baseUrl, connection.model, connection.requiresKey,
|
|
connection.protocol === 'anthropic-messages' ? connection.messagesAuth ?? 'api-key' : null,
|
|
...(requestCredentialIdentity(connection) ? [requestCredentialIdentity(connection)] : [])]);
|
|
function declarationSources(input, previous, declarations) {
|
|
const provided = input.capabilityDeclarationSources;
|
|
if (provided !== undefined && (!provided || typeof provided !== 'object' || Array.isArray(provided)
|
|
|| Object.entries(provided).some(([key, value]) => !Object.hasOwn(declarations, key) || !['user', 'protocol', 'upstream_catalog', 'legacy_connection'].includes(value)))) throw error('能力声明来源格式无效。');
|
|
return Object.fromEntries(Object.keys(declarations).map(key => [key, previous && input.capabilityDeclarations && Object.hasOwn(input.capabilityDeclarations, key)
|
|
&& input.capabilityDeclarations[key] !== previous.capabilityDeclarations?.[key] ? 'user' : provided?.[key] ?? (
|
|
input.capabilityDeclarations && Object.hasOwn(input.capabilityDeclarations, key) && (!previous || input.capabilityDeclarations[key] !== previous.capabilityDeclarations?.[key]) ? 'user'
|
|
: previous?.capabilityDeclarationSources?.[key] ?? (previous?.capabilityDeclarations ? 'legacy_connection' : 'protocol'))]));
|
|
}
|
|
export function modelEndpoint(value) {
|
|
let url;
|
|
try { url = new URL(String(value)); } catch { throw error('请输入有效的模型接口根地址。'); }
|
|
if (!['http:', 'https:'].includes(url.protocol) || url.username || url.password || url.search || url.hash || url.toString().length > 1000) throw error('模型地址只支持不含账号、查询参数的 http/https 地址。');
|
|
return url.toString().replace(/\/+$/, '');
|
|
}
|
|
|
|
export class ModelHub {
|
|
constructor(store, provider) {
|
|
this.store = store; this.provider = provider;
|
|
this.state = store.storage.meta('modelRegistry', { schema: 1, revision: 0, selected: { type: 'legacy', id: legacyId }, connections: [], pools: [], imports: {} });
|
|
this.health = new Map(); this.leases = new Map(); this.cursors = new Map(); this.sticky = new Map(); this.waiting = 0;
|
|
this.ledger = new ModelRequestLedger(store.storage);
|
|
// A process exit leaves a request outcome unknown; never replay it here.
|
|
this.ledger.recoverRunning();
|
|
provider.credentials ??= new Map();
|
|
if (store.storage.meta('modelCredentials', null)) this.vault().restore(this);
|
|
}
|
|
save(config = null) {
|
|
this.state.revision++;
|
|
if (this.editingRegistry) {
|
|
if (config) throw error('批量连接导入不能修改当前运行模式。');
|
|
return;
|
|
}
|
|
const persist = () => this.store.storage.setMeta('modelRegistry', this.state);
|
|
try {
|
|
if (config) this.store.saveConfig(config, { beforeSave: persist });
|
|
else this.store.storage.transaction(persist);
|
|
} catch (cause) {
|
|
// Keep runtime routing consistent with the authoritative committed state.
|
|
this.state = this.store.storage.meta('modelRegistry', { schema: 1, revision: 0,
|
|
selected: { type: 'legacy', id: legacyId }, connections: [], pools: [], imports: {} });
|
|
throw cause;
|
|
}
|
|
}
|
|
editRegistry(action, config = null) {
|
|
if (this.editingRegistry) return action();
|
|
const before = structuredClone(this.state), credentials = new Map(this.provider.credentials);
|
|
const originalKey = this.provider.apiKey, legacyRevision = this.provider.legacyCredentialRevision;
|
|
const vaultIssues = this.credentialVault ? new Map(this.credentialVault.issues) : null;
|
|
this.editingRegistry = true;
|
|
try {
|
|
if (config) {
|
|
let result;
|
|
this.store.saveConfig(config, { beforeSave: () => { result = action(); this.store.storage.setMeta('modelRegistry', this.state); } });
|
|
return result;
|
|
}
|
|
return this.store.storage.transaction(() => {
|
|
const result = action();
|
|
this.store.storage.setMeta('modelRegistry', this.state);
|
|
return result;
|
|
});
|
|
} catch (cause) {
|
|
this.state = before;
|
|
this.provider.credentials.clear();
|
|
for (const [id, value] of credentials) this.provider.credentials.set(id, value);
|
|
this.provider.apiKey = originalKey; this.provider.legacyCredentialRevision = legacyRevision;
|
|
this.credentialVault?.reload();
|
|
if (vaultIssues) this.credentialVault.issues = vaultIssues;
|
|
throw cause;
|
|
} finally { this.editingRegistry = false; }
|
|
}
|
|
get attempts() { return this.compatAttempts ??= this.ledger.all(); }
|
|
set attempts(value) { this.compatAttempts = value; }
|
|
adoptRequestLedger() {
|
|
if (this.ledger) { Object.setPrototypeOf(this.ledger, ModelRequestLedger.prototype); return this.ledger; }
|
|
const retained = Object.getOwnPropertyDescriptor(this, 'attempts');
|
|
if (retained && (!Array.isArray(retained.value) || JSON.stringify(retained.value) !== JSON.stringify(this.store.storage.meta('modelAttempts', [])))) throw error('原内存请求账本与保存来源不同,请先核对,未重放任何请求。', 409);
|
|
const ledger = new ModelRequestLedger(this.store.storage);
|
|
if (retained) { delete this.attempts; this.compatAttempts = retained.value; }
|
|
this.ledger = ledger; return ledger;
|
|
}
|
|
saveAttempts(attempt = null) {
|
|
if (!attempt) return this.ledger.saveAll(this.attempts);
|
|
const sequence = this.ledger.save(attempt);
|
|
if (this.compatAttempts) {
|
|
const positions = this.compatAttempts.map((value, index) => ({ value, index })).filter(({ value }) => value === attempt || this.ledger.positions.get(value) === sequence);
|
|
const sameId = positions.length ? positions : this.compatAttempts.map((value, index) => ({ value, index })).filter(({ value }) => value.id === attempt.id);
|
|
if (sameId.length === 1) this.compatAttempts[sameId[0].index] = attempt;
|
|
}
|
|
return sequence;
|
|
}
|
|
addAttempt(attempt) { this.ledger.save(attempt); this.compatAttempts?.push(attempt); }
|
|
connections() {
|
|
const saved = this.state.legacyConnection;
|
|
return [{ ...saved, id: legacyId, name: saved?.name || this.store.config.model || '未配置模型', brand: saved?.brand || '自定义', protocol: saved?.protocol || 'chat-completions', baseUrl: this.store.config.baseUrl,
|
|
model: this.store.config.model, enabled: saved?.enabled !== false, requiresKey: saved?.requiresKey === true, revision: this.store.config.configRevision ?? 1,
|
|
contextLimit: this.store.config.contextLimit, maxOutputTokens: this.store.config.maxOutputTokens, concurrency: saved?.concurrency ?? 1, source: 'legacy',
|
|
...(this.store.config.parameterProfile ? { parameterProfile: this.store.config.parameterProfile } : {}),
|
|
...(this.store.config.nativeParameters ? { nativeParameters: this.store.config.nativeParameters, outputTokenField: this.store.config.outputTokenField } : {}) }, ...this.state.connections];
|
|
}
|
|
credentialOwner(connection) {
|
|
if (!connection.credentialSource) return connection;
|
|
const source = this.connections().find(value => value.id === connection.credentialSource.connectionId);
|
|
return source && !source.credentialSource && source.enabled && credentialScope(source) === connection.credentialSource.scope
|
|
&& credentialScope(connection) === connection.credentialSource.scope ? source : null;
|
|
}
|
|
credential(connection) {
|
|
const owner = this.credentialOwner(connection);
|
|
return !owner ? undefined : owner.id === legacyId ? { key: this.provider.apiKey, revision: this.provider.legacyCredentialRevision ?? 1 } : this.provider.credentials.get(owner.id);
|
|
}
|
|
vault() { return this.credentialVault ??= new CredentialVault(this.store, this.provider); }
|
|
shouldPersistKey(value) {
|
|
if (value !== undefined && typeof value !== 'boolean') throw error('凭据保存选项必须为开启或关闭。');
|
|
return value ?? process.platform === 'win32';
|
|
}
|
|
writeCredential(connection, apiKey, persistKey, previousRevision = 0) {
|
|
if (typeof apiKey !== 'string' || apiKey.length > 16000) throw error('模型凭据格式无效。');
|
|
const key = apiKey.trim(), persist = this.shouldPersistKey(persistKey);
|
|
const vault = key && persist ? this.vault() : this.credentialVault;
|
|
const revision = Math.max(previousRevision, this.credential(connection)?.revision ?? 0, this.provider.credentials.get(connection.id)?.revision ?? 0,
|
|
vault?.state.entries[connection.id]?.credentialRevision ?? 0) + 1;
|
|
const record = key && persist ? vault.prepare(connection, key, revision) : null;
|
|
if (connection.id === legacyId) { this.provider.setKey(key); this.provider.legacyCredentialRevision = revision; }
|
|
else this.provider.credentials.set(connection.id, { key, revision });
|
|
if (record || vault?.state.entries[connection.id]) vault.stage(connection.id, record);
|
|
}
|
|
configureLegacy(config, input) {
|
|
this.shouldPersistKey(input.persistKey);
|
|
const previous = this.connection(legacyId);
|
|
const connection = { ...previous, name: this.state.legacyConnection?.name || config.model || '未配置模型', baseUrl: config.baseUrl, model: config.model,
|
|
contextLimit: config.contextLimit, maxOutputTokens: config.maxOutputTokens, revision: (this.store.config.configRevision ?? 0) + 1 };
|
|
const hasKey = Object.hasOwn(input, 'apiKey');
|
|
connection.credentialBindingRevision = !hasKey && credentialProfile(previous) === credentialProfile(connection)
|
|
? previous.credentialBindingRevision ?? previous.revision : connection.revision;
|
|
return this.editRegistry(() => {
|
|
this.state.selected = { type: 'legacy', id: legacyId };
|
|
if (this.state.legacyConnection || hasKey || this.credentialVault?.state.entries[legacyId]) this.state.legacyConnection = connection;
|
|
if (hasKey) this.writeCredential(connection, input.apiKey, input.persistKey);
|
|
this.save();
|
|
}, config);
|
|
}
|
|
manageCredential(input) {
|
|
const connection = this.connection(input.id);
|
|
if (connection.credentialSource) throw error('该模型共用上游凭据,请在源连接管理凭据。');
|
|
if (input.revision !== connection.revision) throw error('模型连接已修改,请刷新凭据保存页面。', 409);
|
|
return this.editRegistry(() => {
|
|
const vault = this.vault(), previous = vault.state.entries[connection.id];
|
|
if (input.action === 'save-current') {
|
|
const resident = this.credential(connection);
|
|
if (!resident?.key) throw error('当前连接没有可保存的 Key,请先在模型连接中填写。');
|
|
// This explicit user action stays inside the provider. Preserve the
|
|
// resident object and revision so frozen requests and cache affinity
|
|
// do not change; no credential is sent back to the caller.
|
|
vault.stage(connection.id, vault.prepare(connection, resident.key, resident.revision ?? 1));
|
|
} else if (input.action === 'save') {
|
|
const revision = (this.credential(connection)?.revision ?? 0) + 1, record = vault.prepare(connection, input.apiKey, revision);
|
|
if (connection.id === legacyId) { this.provider.setKey(input.apiKey); this.provider.legacyCredentialRevision = revision; }
|
|
else this.provider.credentials.set(connection.id, { key: input.apiKey.trim(), revision });
|
|
vault.stage(connection.id, record);
|
|
} else if (input.action === 'delete' || input.action === 'forget') {
|
|
vault.stage(connection.id, null);
|
|
if (input.action === 'forget') {
|
|
if (connection.id === legacyId) { this.provider.setKey(''); this.provider.legacyCredentialRevision = (this.provider.legacyCredentialRevision ?? 1) + 1; }
|
|
else this.provider.credentials.set(connection.id, { key: '', revision: (this.credential(connection)?.revision ?? 0) + 1 });
|
|
}
|
|
} else if (input.action === 'disable' || input.action === 'enable') {
|
|
if (!previous) throw error('该连接没有本机保存块。');
|
|
vault.stage(connection.id, { ...previous, enabled: input.action === 'enable' });
|
|
if (input.action === 'enable') vault.restore(this);
|
|
} else throw error('凭据保存操作无效。');
|
|
this.save(); return { credential: vault.status(connection), center: this.view(), config: { ...this.store.config, hasKey: this.provider.hasKey() } };
|
|
});
|
|
}
|
|
identity(connection) { return connection.id + ':' + connection.revision + ':' + (this.credential(connection)?.revision ?? 0)
|
|
+ (connection.credentialSource ? ':' + (this.credentialOwner(connection)?.revision ?? 'unavailable') : ''); }
|
|
status(connection) {
|
|
const credential = this.credential(connection), health = this.health.get(this.identity(connection));
|
|
const capabilityEvidence = this.state.capabilityResults?.[connection.id];
|
|
const ready = connection.enabled && (!connection.credentialSource || !!this.credentialOwner(connection)) && (!connection.requiresKey || !!credential?.key);
|
|
return { ...connection, requestContract: effectiveRequestContract(connection), hasKey: !!credential?.key, credentialRevision: credential?.revision ?? 0, credentialStorage: 'memory',
|
|
...(this.credentialVault ? { savedCredential: this.credentialVault.status(connection) } : {}),
|
|
status: !connection.enabled ? 'disabled' : !ready ? 'needs_key' : health?.blocked ? 'needs_attention' : health?.cooldownUntil > Date.now() ? 'cooling' : 'configured',
|
|
activeRequests: this.leases.get(connection.id) ?? 0, health: health ?? { state: 'unverified' },
|
|
streaming: connection.streaming === true, capabilitySchema: 1, capabilityDeclarations: capabilityDeclarations(undefined, connection.capabilityDeclarations),
|
|
...(capabilityEvidence ? { capabilityEvidence } : {}), modelSpecification: resolveModelSpecification({ ...connection, capabilityEvidence }, credential?.revision ?? 0),
|
|
capabilities: capabilityView({ ...connection, capabilityEvidence }, credential?.revision ?? 0) };
|
|
}
|
|
view() { const page = this.ledger.page({ limit: 100 }); return { revision: this.state.revision, selected: this.state.selected, waitSettings: modelWaitSettings(this.store.config), connections: this.connections().map(value => this.status(value)), pools: this.state.pools,
|
|
imports: Object.entries(this.state.imports).map(([id, value]) => ({ id, ...value })), attempts: page.items, attemptCount: page.retainedTotal, attemptsHasMore: page.hasMore, waiting: this.waiting,
|
|
storage: { credentials: this.credentialVault ? 'memory-or-windows-user-dpapi' : 'memory', credentialPersistenceSupported: process.platform === 'win32', defaultPersistKey: process.platform === 'win32',
|
|
restart: process.platform === 'win32' ? '新填凭据默认本机加密保存并在重启后恢复;只有明确选择仅本次使用的凭据需要重新填写。'
|
|
: '当前宿主不支持本机加密保存,凭据仅在本次宿主内存中。', attemptsRetained: null,
|
|
attemptStorage: 'incremental-sqlite', attemptPreview: 100, originalMetadataPreserved: true } }; }
|
|
selectionView() {
|
|
const selected = this.state.selected;
|
|
const name = selected.type === 'pool' ? this.state.pools.find(value => value.id === selected.id)?.name
|
|
: this.connections().find(value => value.id === selected.id)?.name;
|
|
return { ...selected, name: name ?? '选择模型' };
|
|
}
|
|
networkDiagnostics() { return this.networkChecks ??= new NetworkDiagnostics(this); }
|
|
modelCatalog() { return this.catalog ??= new ModelCatalog(this); }
|
|
connection(id) { const value = this.connections().find(item => item.id === id); if (!value) throw error('模型连接不存在。', 404); return value; }
|
|
async probe(id, scope = 'text') {
|
|
if (!['text', 'tools', 'streaming', 'vision'].includes(scope)) throw error('请选择文本、工具调用、流式或图像检测。');
|
|
if (this.probing) throw error('已有模型能力检测正在执行,请等待结束。', 409);
|
|
this.probing = true;
|
|
try {
|
|
const connection = { ...this.connection(id) }, member = { ...connection, credentialRevision: this.credential(connection)?.revision ?? 0,
|
|
...(connection.credentialSource ? { credentialSourceRevision: this.credentialOwner(connection)?.revision ?? null } : {}) };
|
|
if (!this.available(member)) throw error('该连接不可用,请核对凭据、冷却或停用状态。');
|
|
member.streaming = scope === 'streaming';
|
|
const nonce = randomUUID(), tools = scope === 'tools' ? [{ type: 'function', function: { name: 'workbench_capability_echo',
|
|
description: 'Return the provided nonce in this capability test. No files or commands are available.',
|
|
parameters: { type: 'object', properties: { nonce: { type: 'string' } }, required: ['nonce'], additionalProperties: false } } }] : [];
|
|
// A manual Messages thinking budget cannot fit the ordinary 256-token
|
|
// probe. Keep the chosen parameters and the user's existing output caps.
|
|
const probeOutput = connection.protocol === 'anthropic-messages' && connection.nativeParameters?.thinking?.type === 'enabled'
|
|
? Math.min(this.store.config.maxOutputTokens, connection.maxOutputTokens) : 256;
|
|
const config = { ...this.store.config, mode: 'compatible', model: connection.model, baseUrl: connection.baseUrl,
|
|
protocol: modelProtocol(connection.protocol), maxOutputTokens: probeOutput, capabilityProbe: true, modelRouting: { type: 'connection', id: connection.id, revision: connection.revision,
|
|
strategy: 'round-robin', sticky: false, maxAttempts: 1, queueTimeoutMs: 1000, members: [member] } };
|
|
const visual=scope==='vision'?visionProbe():null;
|
|
if(visual)config.inputImages=[visual.image];
|
|
let attemptId;
|
|
const result = await this.provider.complete(config, [{ role: 'user', content: visual?.prompt ?? (scope === 'tools' ? 'Call workbench_capability_echo with nonce ' + nonce + '. Do not reply with plain text.' : 'Reply OK.') }], null, undefined,
|
|
event => { if (event.modelAttemptId) attemptId = event.modelAttemptId; }, null, tools, { attempts: 1 });
|
|
if (scope === 'tools') {
|
|
const calls = result.message.tool_calls;
|
|
if (calls?.length !== 1 || calls[0].function.name !== 'workbench_capability_echo' || JSON.parse(calls[0].function.arguments).nonce !== nonce) throw error('服务没有返回要求的原工具调用;未标为已验证。');
|
|
} else if (!result.message.content.trim() || result.message.tool_calls?.length) throw error('服务没有返回纯文本响应;未标为已验证。');
|
|
if(visual&&result.message.content.trim().toUpperCase().replace(/\s/g,'')!==visual.expected)throw error('服务未正确识别合成图像的四色顺序,原请求保留,未标为视觉已验证。');
|
|
if (this.connection(id).revision !== connection.revision || (this.credential(connection)?.revision ?? 0) !== member.credentialRevision
|
|
|| connection.credentialSource && this.credentialOwner(connection)?.revision !== member.credentialSourceRevision) throw error('检测期间连接已变化,原结果保留但不更新能力结论。', 409);
|
|
this.editRegistry(() => {
|
|
this.state.capabilityResults ??= {};
|
|
const previous = this.state.capabilityResults[id];
|
|
const results = previous?.connectionRevision === connection.revision && previous.credentialRevision === member.credentialRevision ? { ...previous.results } : {};
|
|
results[scope] = 'verified';
|
|
const attempt = this.ledger.find(attemptId);
|
|
if (!attempt || attempt.connectionId !== id || attempt.status !== 'completed') throw error('检测原请求账本不完整,未更新能力结论。');
|
|
this.state.capabilityResults[id] = { schema: 1, connectionRevision: connection.revision, credentialRevision: member.credentialRevision, results,
|
|
checkedAt: stamp(), protocol: modelProtocol(connection.protocol), attemptId: attempt?.id, source: 'explicit-model-probe',
|
|
scope: 'Only the requested synthetic capability; no project files, commands or real-model engineering benefit.' };
|
|
this.save();
|
|
});
|
|
return { ok: true, usage: result.usage, scope, center: this.view() };
|
|
} finally { this.probing = false; }
|
|
}
|
|
upsert(input) {
|
|
this.shouldPersistKey(input.persistKey);
|
|
const previous = input.id ? this.connection(input.id) : null;
|
|
if (!unrestricted(this.store) && this.state.connections.length >= 100 && !previous) throw error('最多保存100个模型连接。');
|
|
if (previous && input.revision !== previous.revision) throw error('模型连接已修改,请刷新后重试。', 409);
|
|
const protocol = modelProtocol(input.protocol ?? previous?.protocol ?? 'chat-completions');
|
|
const native = Object.hasOwn(input, 'nativeParameters') || Object.hasOwn(input, 'outputTokenField') || Object.hasOwn(input, 'parameterProfile') || previous?.nativeParameters || previous?.parameterProfile
|
|
? nativeParameterSettings(protocol, input.nativeParameters ?? previous?.nativeParameters ?? {}, input.outputTokenField ?? previous?.outputTokenField, input.parameterProfile ?? previous?.parameterProfile, this.store.config) : null;
|
|
const declarations = capabilityDeclarations(input.capabilityDeclarations, previous?.capabilityDeclarations);
|
|
const capabilityDeclarationSources = declarationSources(input, previous, declarations);
|
|
const specificationSettings = {};
|
|
if (Object.hasOwn(input, 'modelOverrides') || previous?.modelOverrides) specificationSettings.modelOverrides = modelOverrideSettings(input.modelOverrides, previous?.modelOverrides);
|
|
for (const key of ['providerId', 'apiVersion']) if (Object.hasOwn(input, key) || previous?.[key]) {
|
|
const value = input[key] ?? previous?.[key];
|
|
if (typeof value !== 'string' || !value || /[\x00-\x20\x7f]/.test(value)) throw error('模型供应商或规格版本标识无效。');
|
|
specificationSettings[key] = value;
|
|
}
|
|
if (input.streaming !== undefined && typeof input.streaming !== 'boolean') throw error('流式选项必须为开启或关闭。');
|
|
const streaming = input.streaming ?? previous?.streaming ?? false;
|
|
if (!unrestricted(this.store) && streaming && declarations.streaming !== 'supported') throw error('请先声明服务支持流式输出,再开启流式请求。');
|
|
const name = label(input.name, ceiling(this.store, 100)), model = label(input.model, ceiling(this.store, 200));
|
|
if (!name || !model) throw error('请填写连接名称和实际模型名称。');
|
|
const connection = { id: previous?.id ?? randomUUID(), name, brand: label(input.brand || '自定义', ceiling(this.store, 40)), protocol, model,
|
|
capabilitySchema: 1, capabilityDeclarations: declarations, capabilityDeclarationSources, ...specificationSettings, streaming, ...(native ?? {}), ...modelIdentitySettings(input, previous ?? {}),
|
|
...messagesAuthSettings(protocol, input.messagesAuth ?? previous?.messagesAuth ?? defaultMessagesAuth({ baseUrl: input.baseUrl, protocol }, { existing: !!previous })),
|
|
baseUrl: validateProtocolEndpoint(protocol, modelEndpoint(input.baseUrl)), enabled: input.enabled !== false, requiresKey: input.requiresKey !== false,
|
|
...capacitySettings(input, previous ?? undefined, this.store.config),
|
|
concurrency: unrestricted(this.store) ? (input.concurrency ?? 0) : bounded(input.concurrency, 1, 1, 8), revision: (previous?.revision ?? 0) + 1,
|
|
source: previous?.source ?? 'manual', ...(previous?.importOrigin ? { importOrigin: previous.importOrigin, importRevision: previous.importRevision } : {}),
|
|
createdAt: previous?.createdAt ?? stamp(), updatedAt: stamp() };
|
|
connection.requestContract = requestContractSettings(input.requestContract, connection, { existing: !!previous, previous: previous ? effectiveRequestContract(previous) : null });
|
|
// Keep an existing encrypted binding for edits that do not change its
|
|
// endpoint/model/auth/key identity. Never decrypt or re-save the old key.
|
|
connection.credentialBindingRevision = previous && !Object.hasOwn(input, 'apiKey') && credentialProfile(previous) === credentialProfile(connection)
|
|
? previous.credentialBindingRevision ?? previous.revision : connection.revision;
|
|
if (previous?.credentialSource && !Object.hasOwn(input, 'apiKey')) {
|
|
if (credentialScope(previous) !== credentialScope(connection)) throw error('该模型共用上游凭据;改变地址或鉴权前,请新建上游或显式填写独立 Key。');
|
|
connection.credentialSource = previous.credentialSource;
|
|
}
|
|
if (previous?.upstreamModel && previous.model === connection.model && credentialScope(previous) === credentialScope(connection)) connection.upstreamModel = previous.upstreamModel;
|
|
if (!unrestricted(this.store)) assertModelParameterSupport(connection, connection.nativeParameters ?? {});
|
|
if (Object.hasOwn(input, 'apiKey')) {
|
|
if (typeof input.apiKey !== 'string' || input.apiKey.length > 16000) throw error('模型凭据格式无效。');
|
|
}
|
|
const legacy = connection.id === legacyId;
|
|
if (!this.editingRegistry) return this.editRegistry(() => this.upsert(input), legacy ? {
|
|
baseUrl: connection.baseUrl, model: connection.model, contextLimit: connection.contextLimit, maxOutputTokens: connection.maxOutputTokens,
|
|
...(native ?? {}),
|
|
} : null);
|
|
if (legacy) this.state.legacyConnection = connection;
|
|
else this.state.connections = [...this.state.connections.filter(item => item.id !== connection.id), connection];
|
|
this.save();
|
|
if (Object.hasOwn(input, 'apiKey')) this.writeCredential(connection, input.apiKey, input.persistKey, this.credential(previous ?? connection)?.revision ?? 0);
|
|
else if (input.persistKey === true) throw error('加密保存需要本次填写 API Key。');
|
|
return this.status(connection);
|
|
}
|
|
setNativeParameters(input) {
|
|
const connection = this.connection(input.id);
|
|
if (connection.revision !== input.revision) throw error('连接已变化,请刷新后核对原生参数。', 409);
|
|
const settings = nativeParameterSettings(connection.protocol, input.nativeParameters, input.outputTokenField, input.parameterProfile ?? connection.parameterProfile, this.store.config);
|
|
if (Object.hasOwn(input, 'modelOverrides')) settings.modelOverrides = modelOverrideSettings(input.modelOverrides, connection.modelOverrides);
|
|
if (connection.id === legacyId && !this.state.legacyConnection && !Object.hasOwn(input, 'modelOverrides')) this.save(settings);
|
|
else this.upsert({ ...connection, ...settings });
|
|
return { center: this.view(), config: { ...this.store.config, hasKey: this.provider.hasKey() } };
|
|
}
|
|
toggle(id, enabled) {
|
|
if (id === legacyId) return this.upsert({ ...this.connection(id), enabled: enabled === true });
|
|
const value = this.connection(id); value.credentialBindingRevision ??= value.revision; value.enabled = enabled === true; value.revision++; value.updatedAt = stamp(); this.save(); return this.status(value);
|
|
}
|
|
pool(input) {
|
|
const previous = input.id ? this.state.pools.find(value => value.id === input.id) : null;
|
|
if (input.id && !previous) throw error('模型池不存在。', 404);
|
|
if (previous && input.revision !== previous.revision) throw error('模型池已修改,请刷新后重试。', 409);
|
|
if (!unrestricted(this.store) && !previous && this.state.pools.length >= 32) throw error('最多保存32个模型池。');
|
|
if (!label(input.name) || !Array.isArray(input.members) || !input.members.length || !unrestricted(this.store) && input.members.length > 32) throw error('模型池需要名称和1–32个成员。');
|
|
const seen = new Set();
|
|
const members = input.members.map(value => {
|
|
this.connection(value.connectionId);
|
|
if (seen.has(value.connectionId)) throw error('同一连接不能重复加入模型池。');
|
|
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,
|
|
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) {
|
|
if (input.type === 'connection') this.connection(input.id);
|
|
else if (input.type === 'pool') { if (!this.state.pools.some(item => item.id === input.id)) throw error('模型池不存在。'); }
|
|
else if (input.type !== 'legacy') throw error('模型选择无效。');
|
|
this.state.selected = { type: input.type, id: input.type === 'legacy' ? legacyId : input.id };
|
|
// Selection and mode must commit together; the original endpoint/key pair
|
|
// stays intact and a Run alone receives its immutable routing snapshot.
|
|
this.save(config ?? (input.type !== 'legacy' ? { mode: 'compatible' } : null));
|
|
return this.view();
|
|
}
|
|
prepare(config, selection = this.state.selected) {
|
|
if (config.mode === 'demo') return config;
|
|
if (selection.type === 'legacy' && !this.state.legacyConnection) {
|
|
const legacy = { ...config, requestContract: effectiveRequestContract(config), modelConnectionId: legacyId,
|
|
modelConnectionRevision: config.configRevision ?? 1, modelCredentialRevision: this.provider.legacyCredentialRevision ?? 1 };
|
|
return { ...legacy, modelSpecification: resolveModelSpecification({ ...legacy, capabilityDeclarationSources: { text: 'protocol', tools: 'protocol' } }), modelSpecificationFrozen: true };
|
|
}
|
|
if (this.catalog?.active.size) throw error('正在获取上游模型目录,请等待完成或取消。', 409);
|
|
const pool = selection.type === 'pool' ? this.state.pools.find(value => value.id === selection.id) : null;
|
|
if (selection.type === 'pool' && !pool) throw error('选中的模型池不存在。');
|
|
const members = (pool?.members ?? [{ connectionId: selection.id, weight: 1 }]).map(value => {
|
|
const connection = this.connection(value.connectionId), credentialRevision = this.credential(connection)?.revision ?? 0;
|
|
const member = { ...connection, requestContract: effectiveRequestContract(connection), capabilityEvidence: this.state.capabilityResults?.[value.connectionId], weight: value.weight, credentialRevision,
|
|
...(connection.credentialSource ? { credentialSourceRevision: this.credentialOwner(connection)?.revision ?? null } : {}) };
|
|
return { ...structuredClone(member), modelSpecification: resolveModelSpecification(member, credentialRevision), modelSpecificationFrozen: true };
|
|
});
|
|
if (!members.some(value => unrestricted(this.store) && value.enabled || this.status(value).status === 'configured' || this.status(value).status === 'cooling')) throw error('没有可用模型连接,请核对启用状态、临时凭据和故障。');
|
|
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,
|
|
requestContract: structuredClone(members[0].requestContract), modelConnectionRevision: members[0].revision,
|
|
nativeParameters: members[0].nativeParameters ?? {}, outputTokenField: members[0].outputTokenField, parameterProfile: members[0].parameterProfile,
|
|
modelSpecification: structuredClone(members[0].modelSpecification), modelSpecificationFrozen: true,
|
|
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, 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);
|
|
if (!current?.enabled || current.revision !== member.revision || (this.credential(current)?.revision ?? 0) !== member.credentialRevision) return false;
|
|
if (current.credentialSource && (!this.credentialOwner(current) || member.credentialSourceRevision !== undefined && this.credentialOwner(current).revision !== member.credentialSourceRevision)) return false;
|
|
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, plan = null } = {}) {
|
|
if (!unrestricted(this.store) && this.waiting >= 100) throw error('模型请求队列已满,请稍后继续任务。');
|
|
this.waiting++;
|
|
try {
|
|
while (true) {
|
|
signal?.throwIfAborted();
|
|
const decision = modelRoutingDecision(routing, requirements, { open: unrestricted(this.store), probe,
|
|
availability: value => excluded.has(value.id) ? 'previous-attempt' : !this.available(value) ? 'connection-unavailable-or-revised' : null });
|
|
const healthy = routing.members.filter(value => decision.candidates.some(candidate => candidate.connectionId === value.id && candidate.disposition === 'eligible'));
|
|
if (!healthy.length) throw Object.assign(error(decision.candidates.some(value => value.disposition === 'excluded')
|
|
? '自动模型池没有适配本次请求的可用成员;可核对能力声明或手动选择连接尝试。'
|
|
: '模型池暂时没有可用成员;已完成操作保留,请核对凭据、停用状态和冷却时间。'), { 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 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);
|
|
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)); } } };
|
|
}
|
|
if (Date.now() >= deadline) throw error('等待模型并发槽位超时,任务已保留。');
|
|
await delay(50, undefined, signal ? { signal } : {});
|
|
}
|
|
} finally { this.waiting--; }
|
|
}
|
|
async complete(config, body, signal, onProgress, plan, execute,beforeDispatch) {
|
|
const operation = this.provider.requestOperation?.(signal);
|
|
try { return await this.completeOwned(config, body, operation?.signal ?? signal, onProgress, plan, execute,beforeDispatch); }
|
|
finally { operation?.finish(); }
|
|
}
|
|
async completeOwned(config, body, signal, onProgress, plan, execute,beforeDispatch) {
|
|
const routing = config.modelRouting, excluded = new Set(), deadline = unrestricted(config) ? Infinity : 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'] : []),
|
|
...(requestImages(body).length ? ['vision'] : [])];
|
|
let lastError;
|
|
for (let count = 1; count <= routing.maxAttempts; count++) {
|
|
signal?.throwIfAborted();
|
|
beforeDispatch?.({previousFailure:lastError?.diagnostic});
|
|
let lease;
|
|
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,
|
|
poolId: routing.type === 'pool' ? routing.id : null, status: 'rejected', reason: cause.message, routingDecision: cause.routingDecision,
|
|
startedAt: stamp(), finishedAt: stamp(), usage: null, usageStatus: 'not_dispatched' });
|
|
}
|
|
throw lastError ?? cause;
|
|
}
|
|
const member = lease.member, credential = this.credential(member);
|
|
const actualBody = actualRequest(body, member);
|
|
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,
|
|
requestUrl: modelRequestUrl(member), ...(member.protocol === 'anthropic-messages' ? messagesAuthSettings(member.protocol, member.messagesAuth) : {}), ordinal: count, status: 'running', startedAt: stamp(),
|
|
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);
|
|
const artifact = this.store.artifactStore.write(session, run, JSON.parse(this.provider.redact(bytes)), { kind: 'model-attempt-request' });
|
|
attempt.requestArtifactId = artifact.id;
|
|
this.store.core.artifact.set(artifact.id, artifact);
|
|
this.store.save(session);
|
|
}
|
|
this.addAttempt(attempt);
|
|
const progress = event => {
|
|
if (event.modelIdentity) attempt.modelIdentity = event.modelIdentity;
|
|
if (event.wireAttemptId) attempt.wireAttemptId = event.wireAttemptId;
|
|
if (event.requestMetadata) { attempt.requestMetadata = event.requestMetadata; this.saveAttempts(attempt); }
|
|
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 });
|
|
this.saveAttempts(attempt);
|
|
return;
|
|
}
|
|
if (event.streamCapture !== undefined || event.responseCapture !== undefined) {
|
|
if (event.stream) attempt.stream = event.stream;
|
|
if (plan?.sessionId) {
|
|
const session = this.store.session(plan.sessionId), run = this.store.owned('run', plan.runId, session.id);
|
|
const raw = event.streamCapture ?? event.responseCapture, decoded = raw.toString('utf8'), redacted = sanitizeModelText(decoded, value => this.provider.redact(value));
|
|
const captured = redacted === decoded ? raw : Buffer.from(redacted, 'utf8');
|
|
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 });
|
|
if (event.upstream?.capture) Object.assign(event.upstream.capture, { retention: 'artifact', storedBytes: artifact.bytes });
|
|
}
|
|
if (event.upstream) attempt.upstream = event.upstream;
|
|
return;
|
|
}
|
|
if (event.streaming) attempt.partial = { sequence: event.streamSequence, bytes: event.streamBytes, textCharacters: event.textCharacters, textPreview: this.provider.redact(event.textPreview ?? '').slice(0, 8000), toolFragments: event.toolFragments };
|
|
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), ...messagesAuthSettings(member.protocol, member.messagesAuth), baseUrl: member.baseUrl, model: member.model, protocol: modelProtocol(member.protocol), streaming: member.streaming === true,
|
|
requestContract: structuredClone(member.requestContract ?? effectiveRequestContract(member)), modelConnectionRevision: member.revision,
|
|
modelConnectionId: member.id, modelCredentialRevision: member.credentialRevision }, 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 });
|
|
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) {
|
|
if (cause.code === 'PROJECT_SELECTION_STALE') {
|
|
attempt.status = 'rejected'; attempt.usageStatus = 'not_dispatched'; attempt.reason = cause.message;
|
|
throw cause;
|
|
}
|
|
const diagnostic = cause.diagnostic;
|
|
if (diagnostic) attempt.diagnostic = diagnostic;
|
|
attempt.status = signal?.aborted ? 'interrupted' : diagnostic?.outcome === 'unknown' ? 'unknown' : 'failed';
|
|
attempt.reason = this.provider.redact(cause.message).slice(0, 600); attempt.httpStatus = diagnostic?.status ?? diagnostic?.httpStatus ?? null;
|
|
const health = this.health.get(this.identity(member)) ?? {};
|
|
if (signal?.aborted) {
|
|
// A user stop is not evidence that the provider is unhealthy. Keep
|
|
// the attempt, release its lease, and leave the connection eligible.
|
|
this.health.set(this.identity(member), { ...health, state: 'interrupted', lastInterruptedAt: stamp() });
|
|
} else if (diagnostic?.reason === 'output_truncated') {
|
|
// The connection responded correctly; the model only overran its output
|
|
// budget. That is not a health failure, so do not cool the member down -
|
|
// the bounded recovery turn must be able to reach the same connection.
|
|
this.health.set(this.identity(member), { ...health, state: 'responded', lastSuccessAt: stamp(), cooldownUntil: 0, failures: 0 });
|
|
} else {
|
|
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 });
|
|
}
|
|
lastError = cause; excluded.add(member.id); this.sticky.delete(runId);
|
|
signal?.throwIfAborted();
|
|
if (config.capacityMode === 'passthrough' || (!diagnostic?.retryable && !diagnostic?.failoverAllowed) || count === routing.maxAttempts) throw cause;
|
|
onProgress?.({ phase: 'retrying', attempt: count, maxAttempts: routing.maxAttempts, connectionId: member.id, modelAttemptId: attempt.id,
|
|
diagnostic, waitMs: 0, switching: true });
|
|
} finally { attempt.finishedAt = stamp(); lease.release(); this.saveAttempts(attempt); }
|
|
}
|
|
throw lastError ?? new ModelRequestError({ message: '模型池请求未完成。', kind: 'connection' });
|
|
}
|
|
}
|