fix: normalize relay request compatibility and classify errors

This commit is contained in:
wlx committed 2026-09-28 08:12:07 +08:00
1 parent 8a2591ef51
commit 4ec47cdb23
3 files changed
+130 -30

No files matched your search

@@ -86,7 +86,9 @@ public class GatewayController {
String providerId=String.valueOf(model.get("providerId"));
boolean mock="provider-mock".equals(providerId);
if(!mock && !provider.isConfigured(providerId)) throw new ApiException("PROVIDER_NOT_CONFIGURED","该供应商尚未配置密钥,不能执行真实调用",503);
ChatMeter.Request request=ChatMeter.request(input,json);
Map<String,Object> normalizedInput = new LinkedHashMap<>(input);
OpenAiCompatibleProvider.prepareRequestForModel(String.valueOf(model.get("upstreamModel")), normalizedInput);
ChatMeter.Request request=ChatMeter.request(normalizedInput,json);
guard.checkRate(UUID.fromString(String.valueOf(key.get("id"))), false);
// Reserve both limits atomically. A queued request never consumes a
// model slot while waiting for the API-Key slot (or vice versa).
@@ -83,6 +83,22 @@ public class OpenAiCompatibleProvider {
return open(providerId,upstreamModel,input,true);
}
/**
* Normalize the small set of provider-specific request constraints that
* are known before the request is reserved and billed. Claude manual
* thinking requires a larger output budget than the common 512-token
* playground default; changing it here keeps the billing reservation and
* the upstream request aligned.
*/
public static void prepareRequestForModel(String upstreamModel, Map<String, Object> body) {
if (upstreamModel == null || body == null || !upstreamModel.toLowerCase(java.util.Locale.ROOT).contains("claude")
|| !upstreamModel.toLowerCase(java.util.Locale.ROOT).contains("thinking")) return;
String field = body.containsKey("max_completion_tokens") ? "max_completion_tokens" : "max_tokens";
if (!body.containsKey(field)) return;
int current = numberValue(body.get(field), 0);
if (current > 0 && current < 2048) body.put(field, 2048);
}
private StreamResponse open(String providerId, String upstreamModel, Map<String,Object> input, boolean streaming) {
ProviderConfig provider = resolve(providerId);
if (provider == null || !provider.hasKey()) {
@@ -94,36 +110,69 @@ public class OpenAiCompatibleProvider {
if(streaming) body.put("stream_options",Map.of("include_usage",true));
else body.remove("stream_options");
String apiKey = provider.nextApiKey();
String json = writeJson(body);
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(normalizeUrl(provider.baseUrl()) + "/chat/completions"))
.timeout(Duration.ofSeconds(timeoutSeconds))
.header("Authorization", "Bearer " + apiKey)
.header("Content-Type", "application/json")
.POST(HttpRequest.BodyPublishers.ofString(json))
.build();
try {
long started=System.nanoTime();
HttpResponse<InputStream> response = httpClient.send(request, HttpResponse.BodyHandlers.ofInputStream());
if (response.statusCode() < 200 || response.statusCode() >= 300) {
String details = readErrorBody(response.body());
throw classifyUpstreamError(response.statusCode(), details);
for (int attempt = 0; attempt < 2; attempt++) {
String json = writeJson(body);
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(normalizeUrl(provider.baseUrl()) + "/chat/completions"))
.timeout(Duration.ofSeconds(timeoutSeconds))
.header("Authorization", "Bearer " + apiKey)
.header("Content-Type", "application/json")
.POST(HttpRequest.BodyPublishers.ofString(json))
.build();
try {
long started=System.nanoTime();
HttpResponse<InputStream> response = httpClient.send(request, HttpResponse.BodyHandlers.ofInputStream());
if (response.statusCode() < 200 || response.statusCode() >= 300) {
String details = readErrorBody(response.body());
// Some OpenAI-compatible relays use the newer spelling for
// reasoning-model budgets, and Claude manual thinking
// rejects small budgets. Retry only these deterministic
// request-shape errors; other failures remain bill-safe.
if (attempt == 0 && prepareCompatibilityRetry(body, details)) continue;
throw classifyUpstreamError(response.statusCode(), details);
}
if(streaming && !response.headers().firstValue("Content-Type").orElse("").toLowerCase().startsWith("text/event-stream")) {
response.body().close();
throw new ApiException("UPSTREAM_STREAM_UNSUPPORTED","上游未返回 SSE 流式响应",502);
}
return new StreamResponse(response.body(),Math.max(1,timeoutSeconds*1000L-TimeUnit.NANOSECONDS.toMillis(System.nanoTime()-started)));
} catch (ApiException exception) {
throw exception;
} catch (HttpTimeoutException | SocketTimeoutException exception) {
throw new ApiException("UPSTREAM_PROVIDER_TIMEOUT", "上游模型供应商响应超时", 504);
} catch (InterruptedException exception) {
Thread.currentThread().interrupt();
throw new ApiException("UPSTREAM_PROVIDER_TIMEOUT", "上游模型供应商请求被中断", 502);
} catch (Exception exception) {
throw new ApiException("UPSTREAM_PROVIDER_ERROR", "上游模型供应商请求失败", 502);
}
if(streaming && !response.headers().firstValue("Content-Type").orElse("").toLowerCase().startsWith("text/event-stream")) {
response.body().close();
throw new ApiException("UPSTREAM_STREAM_UNSUPPORTED","上游未返回 SSE 流式响应",502);
}
return new StreamResponse(response.body(),Math.max(1,timeoutSeconds*1000L-TimeUnit.NANOSECONDS.toMillis(System.nanoTime()-started)));
} catch (ApiException exception) {
throw exception;
} catch (HttpTimeoutException | SocketTimeoutException exception) {
throw new ApiException("UPSTREAM_PROVIDER_TIMEOUT", "上游模型供应商响应超时", 504);
} catch (InterruptedException exception) {
Thread.currentThread().interrupt();
throw new ApiException("UPSTREAM_PROVIDER_TIMEOUT", "上游模型供应商请求被中断", 502);
} catch (Exception exception) {
throw new ApiException("UPSTREAM_PROVIDER_ERROR", "上游模型供应商请求失败", 502);
}
throw new ApiException("UPSTREAM_PROVIDER_ERROR", "上游模型供应商请求失败", 502);
}
/** Return true only for a deterministic request-shape rejection that is safe to retry. */
static boolean prepareCompatibilityRetry(Map<String, Object> body, String details) {
String text = details == null ? "" : details.toLowerCase(java.util.Locale.ROOT);
if (text.matches(".*max_tokens.{0,80}max_completion_tokens.*") && body.containsKey("max_tokens")) {
Object value = body.remove("max_tokens");
body.put("max_completion_tokens", value);
return true;
}
String model = String.valueOf(body.getOrDefault("model", "")).toLowerCase(java.util.Locale.ROOT);
if (text.matches(".*max_tokens must be greater than 1024.*") && body.containsKey("max_tokens")
&& model.contains("claude") && model.contains("thinking")) {
int current = numberValue(body.get("max_tokens"), 0);
if (current < 2048) {
body.put("max_tokens", 2048);
return true;
}
}
return false;
}
private static int numberValue(Object value, int fallback) {
if (value instanceof Number number) return number.intValue();
try { return Integer.parseInt(String.valueOf(value)); } catch (RuntimeException ignored) { return fallback; }
}
/** A hard body deadline is required: HttpRequest.timeout alone does not cover InputStream reads. */
@@ -195,7 +244,10 @@ public class OpenAiCompatibleProvider {
if (status == 429 || details.matches(".*(rate.?limit|too many requests|concurr|并发|频率过高|请求过于频繁|限流).*")) {
return new ApiException("UPSTREAM_RATE_LIMITED", "上游渠道限流或并发已满,请稍后重试", 429);
}
if (status == 404 || details.matches(".*(no available channel|available channel.{0,12}(does not exist|not found)|channel.{0,12}(not found|does not exist|unavailable)|model.{0,16}(not found|does not exist|unavailable)|not_found|unsupported model|没有可用渠道|可用渠道不存在|渠道不存在|模型不存在|模型不可用).*")) {
if (details.matches(".*(resources_tips|free quota.{0,12}(ended|exhausted)|免费额度已结束|免费额度已用尽).*")) {
return new ApiException("UPSTREAM_CREDIT_INSUFFICIENT", "上游渠道余额或额度不足", 502);
}
if (status == 404 || details.matches(".*(no available channel|available channel.{0,12}(does not exist|not found)|channel.{0,12}(not found|does not exist|unavailable)|model.{0,16}(not found|does not exist|unavailable)|not_found|unsupported model|没有可用渠道|可用渠道不存在|渠道不存在|模型不存在|模型不可用|model_price_error|has not been priced|价格尚未由管理员配置|invalidendpoint\\.closedendpoint|endpoint.{0,24}(closed|temporarily unavailable)|端点.*关闭).*")) {
return new ApiException("UPSTREAM_MODEL_UNAVAILABLE", "上游渠道没有可用的模型路由", 502);
}
return new ApiException("UPSTREAM_PROVIDER_ERROR", "上游模型供应商请求失败", 502);