fix: bind observed models and calibrate every model dispatch budget
This commit is contained in:
@@ -8,6 +8,7 @@ import { normalizeProtocolResponse, protocolRoute, validateModelResponse, report
|
||||
import { readModelStream } from '../core/models/model-stream.mjs';
|
||||
import { modelWaitSettings, requestDeadline } from '../core/models/request-deadline.mjs';
|
||||
import { ModelTransport } from '../core/models/transport.mjs';
|
||||
import { localModelEstimate } from '../shared/contracts/model-reservation.mjs';
|
||||
import { ERROR_CAPTURE_LIMIT, readErrorResponse, upstreamCapture } from '../core/models/upstream-error.mjs';
|
||||
|
||||
export const engineToolDefinitions = [
|
||||
@@ -156,19 +157,22 @@ export class Provider {
|
||||
const waits = modelWaitSettings(config), timeoutMs = body ? waits.modelResponseTimeoutMs : 20000;
|
||||
const policy = { attempts: 3, delays: [500, 1500], timeoutMs, totalTimeoutMs: timeoutMs,
|
||||
idleTimeoutMs: waits.modelStreamIdleTimeoutMs, ...this.retryPolicy, ...options };
|
||||
policy.attempts=Math.min(policy.attempts,options.maxBudgetAttempts??policy.attempts);
|
||||
const operation = this.requestOperation(signal); signal = operation.signal;
|
||||
const deadline = Date.now() + policy.totalTimeoutMs;
|
||||
const startedAt = Date.now();
|
||||
let attempts = 0;
|
||||
let attempts = 0, previousFailure = null;
|
||||
this.activeRequests = (this.activeRequests ?? 0) + 1;
|
||||
try {
|
||||
for (let attempt = 1; attempt <= policy.attempts; attempt++) {
|
||||
signal?.throwIfAborted();
|
||||
options.beforeDispatch?.({previousFailure});
|
||||
attempts = attempt;
|
||||
const timeout = requestDeadline({ signal, timeoutMs: Math.max(1, Math.min(policy.timeoutMs, deadline - Date.now())), idleTimeoutMs: policy.idleTimeoutMs });
|
||||
const requestSignal = timeout.signal;
|
||||
let readingStream = false, streamTerminalSeen = false, dispatched = false, usage = null, outcome = 'unknown';
|
||||
const receipt = { attemptId: randomUUID(), endpoint: base, model: config.model, protocol: config.protocol ?? 'chat-completions', startedAt: new Date().toISOString() };
|
||||
const receipt = { attemptId: randomUUID(), endpoint: base, model: config.model, protocol: config.protocol ?? 'chat-completions', startedAt: new Date().toISOString(),
|
||||
...(body?{localEstimate:localModelEstimate(body)}:{}) };
|
||||
const context = { endpoint: base, model: config.model, route, attempt, attemptId: receipt.attemptId };
|
||||
onProgress?.({ phase: 'running', attempt, maxAttempts: policy.attempts, endpoint: base, route, wireAttemptId: receipt.attemptId });
|
||||
let responseDetails = null;
|
||||
@@ -255,6 +259,7 @@ export class Provider {
|
||||
const retry = diagnostic.retryable && attempt < policy.attempts && remaining > waitMs;
|
||||
diagnostic.retryDecision = retry ? 'retry_after_wait' : readingStream ? 'stream_not_replayed' : !diagnostic.retryable ? 'not_retryable' : attempt >= policy.attempts ? 'attempt_limit' : 'wait_budget';
|
||||
diagnostic.remainingWaitBudgetMs = Math.max(0, remaining);
|
||||
previousFailure = diagnostic;
|
||||
this.lastRequest = { ...diagnostic, status: retry ? 'retrying' : 'failed', durationMs: Date.now() - startedAt };
|
||||
onProgress?.({ phase: retry ? 'retrying' : 'failed', attempt, maxAttempts: policy.attempts,
|
||||
diagnostic, waitMs: retry ? waitMs : 0, durationMs: Date.now() - startedAt });
|
||||
@@ -289,7 +294,7 @@ export class Provider {
|
||||
return this.request(selected, protocolRoute(selected), input, signal, key, progress, { ...(config.modelRouting ? { attempts: 1 } : {}), ...requestPolicy,
|
||||
transformResponse: raw => validateModelResponse(normalizeProtocolResponse(selected, raw), tools) });
|
||||
};
|
||||
const result = config.modelRouting && this.hub ? await this.hub.complete(config, body, signal, onProgress, requestPlan, execute)
|
||||
const result = config.modelRouting && this.hub ? await this.hub.complete(config, body, signal, onProgress, requestPlan, execute,requestPolicy.beforeDispatch)
|
||||
: await execute(config, this.apiKey, body, onProgress);
|
||||
const received = result.choices?.[0]?.message;
|
||||
if (!received) throw new Error('兼容服务响应中缺少 choices[0].message。');
|
||||
|
||||
Reference in New Issue
Block a user