diff --git a/control-plane-java/src/main/java/com/moyi/controlplane/gateway/GatewayController.java b/control-plane-java/src/main/java/com/moyi/controlplane/gateway/GatewayController.java index 498cc3e..878ac67 100644 --- a/control-plane-java/src/main/java/com/moyi/controlplane/gateway/GatewayController.java +++ b/control-plane-java/src/main/java/com/moyi/controlplane/gateway/GatewayController.java @@ -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 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). diff --git a/control-plane-java/src/main/java/com/moyi/controlplane/provider/OpenAiCompatibleProvider.java b/control-plane-java/src/main/java/com/moyi/controlplane/provider/OpenAiCompatibleProvider.java index a0e78e1..a347536 100644 --- a/control-plane-java/src/main/java/com/moyi/controlplane/provider/OpenAiCompatibleProvider.java +++ b/control-plane-java/src/main/java/com/moyi/controlplane/provider/OpenAiCompatibleProvider.java @@ -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 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 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 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 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 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); diff --git a/control-plane-java/src/test/java/com/moyi/controlplane/provider/ProviderErrorClassificationTest.java b/control-plane-java/src/test/java/com/moyi/controlplane/provider/ProviderErrorClassificationTest.java index 7e94755..c8b0a72 100644 --- a/control-plane-java/src/test/java/com/moyi/controlplane/provider/ProviderErrorClassificationTest.java +++ b/control-plane-java/src/test/java/com/moyi/controlplane/provider/ProviderErrorClassificationTest.java @@ -1,6 +1,9 @@ package com.moyi.controlplane.provider; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.util.LinkedHashMap; import org.junit.jupiter.api.Test; @@ -54,4 +57,47 @@ class ProviderErrorClassificationTest { assertEquals("UPSTREAM_PROVIDER_TIMEOUT", error.getCode()); assertEquals(504, error.getStatus()); } + + @Test + void classifiesRelayPricingAndClosedEndpointAsUnavailable() { + assertEquals("UPSTREAM_MODEL_UNAVAILABLE", OpenAiCompatibleProvider + .classifyUpstreamError(400, "model_price_error: model has not been priced").getCode()); + assertEquals("UPSTREAM_MODEL_UNAVAILABLE", OpenAiCompatibleProvider + .classifyUpstreamError(500, "InvalidEndpoint.ClosedEndpoint").getCode()); + } + + @Test + void classifiesRelayFreeQuotaAsCreditFailure() { + assertEquals("UPSTREAM_CREDIT_INSUFFICIENT", OpenAiCompatibleProvider + .classifyUpstreamError(500, "RESOURCES_TIPS: 免费额度已结束").getCode()); + } + + @Test + void retriesNewCompletionBudgetParameter() { + var body = new LinkedHashMap(); + body.put("model", "claude-sonnet-4-5-20250929-thinking"); + body.put("max_tokens", 512); + assertTrue(OpenAiCompatibleProvider.prepareCompatibilityRetry(body, + "Unsupported parameter: max_tokens is not supported. Use max_completion_tokens instead.")); + assertEquals(512, body.get("max_completion_tokens")); + assertTrue(!body.containsKey("max_tokens")); + } + + @Test + void raisesManualClaudeThinkingBudget() { + var body = new LinkedHashMap(); + body.put("model", "claude-sonnet-4-5-20250929-thinking"); + body.put("max_tokens", 512); + assertTrue(OpenAiCompatibleProvider.prepareCompatibilityRetry(body, + "max_tokens must be greater than 1024 for manual Claude thinking")); + assertEquals(2048, body.get("max_tokens")); + } + + @Test + void normalizesClaudeThinkingBudgetBeforeBilling() { + var body = new LinkedHashMap(); + body.put("max_tokens", 512); + OpenAiCompatibleProvider.prepareRequestForModel("claude-sonnet-4-5-20250929-thinking", body); + assertEquals(2048, body.get("max_tokens")); + } }