Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 96 additions & 2 deletions containers/api-proxy/body-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,12 @@ const { translateCodexCustomToolsForCopilot } = require('./codex-compat');
const { endpointForPath, translateCopilotWireApi } = require('./wire-api-compat');
const { stripRedundantModelPrefixInBody } = require('./model-body-rewriter');
const { parseBodyAsObject } = require('./body-utils');
const { recoverRequestModel } = require('./model-recovery');
const { recoverRequestModel, protocolForUrl } = require('./model-recovery');
const { getFallbackUpstreamEndpoint, getRequiredResponseFeatures } = require('./cross-provider-fallback');
const { assessRequestCompatibility } = require('./protocol-compatibility/assessment');
const { getRequestReasoning } = require('./model-reasoning');
const { evaluateModelEligibility } = require('./model-eligibility');
const { getCurrentGuardChecks } = require('./proxy-guards');

/** Maximum request body size: 10 MB to prevent DoS via large payloads. */
const MAX_BODY_SIZE = 10 * 1024 * 1024;
Expand Down Expand Up @@ -300,10 +305,99 @@ function createBodyHandler({ handleRequestError, otel }) {
}
}

// Preserve the agent-facing representation after explicitly authorized
// normalization, before any wire translation. Fallback must assess this
// request rather than a body whose translator has already discarded fields.
if (isWritableMethod) req.awfFallbackSourceBody = Buffer.from(body);

const selectResolverFallback = () => {
if (!isWritableMethod || req.awfResolverFallback?.provider !== provider || req.awfRecoveryAttempts) return null;
if (getCurrentGuardChecks(null, provider).some(guard => guard.isBlocked(guard.block))) return null;
const parsed = parseBodyAsObject(req.awfFallbackSourceBody);
const protocol = value => {
const name = protocolForUrl(value);
return name === 'chat-completions' ? 'chat_completions'
: name === 'messages' ? 'anthropic_messages' : name;
};
const source = protocol(req.url);
const pins = { eligibilityRevision: req.awfEligibility?.revision, catalogueRevision: req.awfCatalogue?.revision };
const requiredResponseFeatures = getRequiredResponseFeatures(req.awfFallbackSourceBody, protocolForUrl(req.url));
const publish = (model, outcome, compatibility, reasoning) => {
try {
req.awfObserveModelDecision?.({
provider, model: outcome === 'exhausted' ? null : model,
endpoint: source === 'anthropic_messages' ? '/v1/messages' : endpointForPath(req.url),
originProvider: provider, originModel: req.awfResolverFallback.fromModel,
outcome, compatibility, reasoning, ...pins,
reason: outcome === 'exhausted' ? 'fallback_exhausted' : `fallback_${compatibility.status}`,
});
} catch {
// Optional observation must not change request admission.
}
};
const ranked = req.awfModelRecoveryDisabled || req.awfScopedAuto ||
req.awfRequestContext?.purpose === 'routing_classification' ? []
: Array.isArray(req.awfModelCandidates) ? req.awfModelCandidates
: req.awfResolverFallback.candidates || [];
const candidates = [...new Set([parsed?.model, ...ranked])]
.filter(model => typeof model === 'string' && model.length > 0);
let lastCompatibility = null;
let lastReasoning = null;
for (const model of candidates) {
let eligibility = null;
try {
eligibility = evaluateModelEligibility(model, provider, req, { inputMode: 'native' });
} catch { /* fail closed */ }
if (eligibility?.admitted !== true) {
lastCompatibility = assessRequestCompatibility({
body: req.awfFallbackSourceBody, source, target: null, candidate: { provider, model: null }, pins,
});
lastReasoning = null;
publish(null, 'skipped', lastCompatibility, null);
continue;
}
const targetUrl = provider === 'copilot' && req.method === 'POST' && endpointForPath(req.url)
? getFallbackUpstreamEndpoint(provider, model, req.url, false, req.awfRuntimePricing) : req.url;
const target = protocol(targetUrl);
const reasoning = getRequestReasoning(model, provider, req.awfRuntimePricing, req, req.awfFallbackTarget || {});
const compatibility = assessRequestCompatibility({
body: req.awfFallbackSourceBody, source, target, candidate: { provider, model }, pins, reasoning,
requiredResponseFeatures, nativePassthrough: ['gemini', 'vertex'].includes(provider) &&
source === null && target === null && targetUrl === req.url &&
/\/models\/[^/:?]+:(?:generateContent|streamGenerateContent)(?:\?|$)/.test(req.url),
});
lastCompatibility = compatibility;
lastReasoning = reasoning;
if (compatibility.status !== 'compatible') {
publish(model, 'skipped', compatibility, reasoning);
continue;
}
if (model !== parsed.model) {
parsed.model = model;
body = Buffer.from(JSON.stringify(parsed), 'utf8');
req.awfFallbackSourceBody = Buffer.from(body);
req.awfSelectedModelEligibility = Object.freeze({ model, provider, decision: eligibility });
if (eligibility.identity) req.awfModelIdentity = eligibility.identity;
}
publish(model, 'accepted', compatibility, reasoning);
return targetUrl;
}
publish(null, 'exhausted', lastCompatibility || assessRequestCompatibility({
body: req.awfFallbackSourceBody, source, target: source, candidate: { provider, model: null }, pins,
}), lastReasoning);
const error = new Error('The selected automatic fallback cannot preserve the assessed request requirements');
error.statusCode = 400;
error.type = 'invalid_request_error';
error.code = 'model_recovery_exhausted';
throw error;
};
const resolverEndpoint = selectResolverFallback();

if (provider === 'copilot' && req.method === 'POST' && endpointForPath(req.url) &&
!req.awfRecoveryAttempts) {
wireApiSourceBody = Buffer.from(body);
const translated = translateCopilotWireApi(body, req.url);
const translated = resolverEndpoint && endpointForPath(resolverEndpoint) === endpointForPath(req.url)
? null : translateCopilotWireApi(body, req.url, resolverEndpoint ? { upstreamEndpoint: endpointForPath(resolverEndpoint) } : {});
if (translated) {
body = translated.body;
wireApiCompatibility = translated.compatibility;
Expand Down
Loading
Loading