feat(models): freeze provider request contracts and observe actual headers
This commit is contained in:
+11
-8
@@ -12,7 +12,9 @@ import { modelWaitSettings, requestDeadline } from '../core/models/request-deadl
|
||||
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';
|
||||
import { messagesAuthSettings } from '../shared/contracts/model-endpoint.mjs';
|
||||
import { endpointRequestUrl } from '../shared/contracts/model-endpoint.mjs';
|
||||
import { requestHeaders } from '../shared/contracts/model-request-contract.mjs';
|
||||
import { preparedRequestMetadata } from '../core/models/request-contract-audit.mjs';
|
||||
import { fileDraftToolDefinitions } from '../shared/contracts/file-draft-tools.mjs';
|
||||
|
||||
export const engineToolDefinitions = [
|
||||
@@ -181,13 +183,8 @@ export class Provider {
|
||||
}
|
||||
async request(config, route, body, signal, key = this.apiKey, onProgress, options = {}) {
|
||||
const base = this.validate({ ...config, mode: 'demo' });
|
||||
const headers = { 'Content-Type': 'application/json' };
|
||||
if (config.protocol === 'anthropic-messages') {
|
||||
headers['anthropic-version'] = '2023-06-01';
|
||||
const { messagesAuth } = messagesAuthSettings(config.protocol, config.messagesAuth);
|
||||
if (key) headers[messagesAuth === 'bearer' ? 'Authorization' : 'x-api-key'] = messagesAuth === 'bearer' ? 'Bearer ' + key : key;
|
||||
} else if (key) headers.Authorization = 'Bearer ' + key;
|
||||
const requestUrl = base + route;
|
||||
const headers = requestHeaders(config, key);
|
||||
const requestUrl = endpointRequestUrl(config, route, body ? 'completion' : 'models');
|
||||
const waits = modelWaitSettings(config), timeoutMs = body || unrestricted(config) ? waits.modelResponseTimeoutMs : 20000;
|
||||
const policy = { attempts: 3, delays: [500, 1500], timeoutMs, totalTimeoutMs: timeoutMs,
|
||||
idleTimeoutMs: waits.modelStreamIdleTimeoutMs, ...this.retryPolicy, ...options };
|
||||
@@ -235,8 +232,13 @@ export class Provider {
|
||||
const transport=this.fetcher?null:(this.transport??=new ModelTransport());
|
||||
if(transport)onProgress?.({phase:'running',transport:transport.profile,attempt,maxAttempts:policy.attempts,wireAttemptId:receipt.attemptId});
|
||||
wireBody = body ? JSON.stringify(body) : undefined;
|
||||
const bound = { ...config, modelConnectionId: config.modelConnectionId ?? config.id ?? (body ? 'legacy-default' : null),
|
||||
modelConnectionRevision: config.modelConnectionRevision ?? config.revision ?? (body ? config.configRevision ?? 1 : null),
|
||||
modelCredentialRevision: config.modelCredentialRevision ?? config.credentialRevision ?? (config.id ? this.hub?.credential(config)?.revision ?? 0 : body ? this.legacyCredentialRevision ?? 1 : null) };
|
||||
receipt.requestMetadata = preparedRequestMetadata(bound, body, { headers, requestUrl, wireBody, redact: value => this.redact(value, key) });
|
||||
const submitted = (this.fetcher??transport.fetch.bind(transport))(requestUrl, { method: body ? 'POST' : 'GET', headers,
|
||||
body: wireBody, signal: requestSignal, redirect: 'manual' });
|
||||
try { onProgress?.({ requestMetadata: receipt.requestMetadata, wireAttemptId: receipt.attemptId, attempt, maxAttempts: policy.attempts }); } catch { /* Observe after dispatch without stranding its promise. */ }
|
||||
if (wireBody && options.onSubmitted) {
|
||||
// Optional local observers cannot interrupt an already submitted HTTP
|
||||
// request or leave its promise unobserved.
|
||||
@@ -330,6 +332,7 @@ export class Provider {
|
||||
diagnostic = { ...diagnostic, requestUrl, ...(receipt.responseUrl ? { responseUrl: receipt.responseUrl } : {}),
|
||||
...(receipt.redirectMode ? { redirected: receipt.redirected, redirectMode: receipt.redirectMode } : {}),
|
||||
origin: diagnostic.origin ?? 'local', wireAttemptId: receipt.attemptId, upstreamRequestId: diagnostic.upstreamRequestId ?? receipt.upstreamRequestId,
|
||||
...(receipt.requestMetadata ? { requestMetadata: receipt.requestMetadata } : {}),
|
||||
...(responseDetails && !diagnostic.upstream ? { upstream: responseDetails } : {}) };
|
||||
if (readingStream && diagnostic.kind !== 'model_identity') diagnostic = { ...diagnostic, retryable: false, failoverAllowed: false, outcome: streamTerminalSeen ? 'failed' : 'unknown',
|
||||
kind: diagnostic.origin === 'upstream' ? diagnostic.kind : timeout.timeout ? 'timeout' : streamTerminalSeen || diagnostic.kind === 'protocol' ? 'protocol' : 'stream_interrupted',
|
||||
|
||||
Reference in New Issue
Block a user