fix: normalize relay request compatibility and classify errors
This commit is contained in:
1 parent
8a2591ef51
commit
4ec47cdb23
3 files changed
+102
-2
No files matched your search
+3
-1
@@ -86,7 +86,9 @@ public class GatewayController {
|
|||||||
String providerId=String.valueOf(model.get("providerId"));
|
String providerId=String.valueOf(model.get("providerId"));
|
||||||
boolean mock="provider-mock".equals(providerId);
|
boolean mock="provider-mock".equals(providerId);
|
||||||
if(!mock && !provider.isConfigured(providerId)) throw new ApiException("PROVIDER_NOT_CONFIGURED","该供应商尚未配置密钥,不能执行真实调用",503);
|
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);
|
guard.checkRate(UUID.fromString(String.valueOf(key.get("id"))), false);
|
||||||
// Reserve both limits atomically. A queued request never consumes a
|
// Reserve both limits atomically. A queued request never consumes a
|
||||||
// model slot while waiting for the API-Key slot (or vice versa).
|
// model slot while waiting for the API-Key slot (or vice versa).
|
||||||
|
|||||||
+53
-1
@@ -83,6 +83,22 @@ public class OpenAiCompatibleProvider {
|
|||||||
return open(providerId,upstreamModel,input,true);
|
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) {
|
private StreamResponse open(String providerId, String upstreamModel, Map<String,Object> input, boolean streaming) {
|
||||||
ProviderConfig provider = resolve(providerId);
|
ProviderConfig provider = resolve(providerId);
|
||||||
if (provider == null || !provider.hasKey()) {
|
if (provider == null || !provider.hasKey()) {
|
||||||
@@ -94,6 +110,7 @@ public class OpenAiCompatibleProvider {
|
|||||||
if(streaming) body.put("stream_options",Map.of("include_usage",true));
|
if(streaming) body.put("stream_options",Map.of("include_usage",true));
|
||||||
else body.remove("stream_options");
|
else body.remove("stream_options");
|
||||||
String apiKey = provider.nextApiKey();
|
String apiKey = provider.nextApiKey();
|
||||||
|
for (int attempt = 0; attempt < 2; attempt++) {
|
||||||
String json = writeJson(body);
|
String json = writeJson(body);
|
||||||
HttpRequest request = HttpRequest.newBuilder()
|
HttpRequest request = HttpRequest.newBuilder()
|
||||||
.uri(URI.create(normalizeUrl(provider.baseUrl()) + "/chat/completions"))
|
.uri(URI.create(normalizeUrl(provider.baseUrl()) + "/chat/completions"))
|
||||||
@@ -107,6 +124,11 @@ public class OpenAiCompatibleProvider {
|
|||||||
HttpResponse<InputStream> response = httpClient.send(request, HttpResponse.BodyHandlers.ofInputStream());
|
HttpResponse<InputStream> response = httpClient.send(request, HttpResponse.BodyHandlers.ofInputStream());
|
||||||
if (response.statusCode() < 200 || response.statusCode() >= 300) {
|
if (response.statusCode() < 200 || response.statusCode() >= 300) {
|
||||||
String details = readErrorBody(response.body());
|
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);
|
throw classifyUpstreamError(response.statusCode(), details);
|
||||||
}
|
}
|
||||||
if(streaming && !response.headers().firstValue("Content-Type").orElse("").toLowerCase().startsWith("text/event-stream")) {
|
if(streaming && !response.headers().firstValue("Content-Type").orElse("").toLowerCase().startsWith("text/event-stream")) {
|
||||||
@@ -125,6 +147,33 @@ public class OpenAiCompatibleProvider {
|
|||||||
throw new ApiException("UPSTREAM_PROVIDER_ERROR", "上游模型供应商请求失败", 502);
|
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. */
|
/** A hard body deadline is required: HttpRequest.timeout alone does not cover InputStream reads. */
|
||||||
public final class StreamResponse implements AutoCloseable {
|
public final class StreamResponse implements AutoCloseable {
|
||||||
@@ -195,7 +244,10 @@ public class OpenAiCompatibleProvider {
|
|||||||
if (status == 429 || details.matches(".*(rate.?limit|too many requests|concurr|并发|频率过高|请求过于频繁|限流).*")) {
|
if (status == 429 || details.matches(".*(rate.?limit|too many requests|concurr|并发|频率过高|请求过于频繁|限流).*")) {
|
||||||
return new ApiException("UPSTREAM_RATE_LIMITED", "上游渠道限流或并发已满,请稍后重试", 429);
|
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_MODEL_UNAVAILABLE", "上游渠道没有可用的模型路由", 502);
|
||||||
}
|
}
|
||||||
return new ApiException("UPSTREAM_PROVIDER_ERROR", "上游模型供应商请求失败", 502);
|
return new ApiException("UPSTREAM_PROVIDER_ERROR", "上游模型供应商请求失败", 502);
|
||||||
|
|||||||
+46
@@ -1,6 +1,9 @@
|
|||||||
package com.moyi.controlplane.provider;
|
package com.moyi.controlplane.provider;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
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;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
@@ -54,4 +57,47 @@ class ProviderErrorClassificationTest {
|
|||||||
assertEquals("UPSTREAM_PROVIDER_TIMEOUT", error.getCode());
|
assertEquals("UPSTREAM_PROVIDER_TIMEOUT", error.getCode());
|
||||||
assertEquals(504, error.getStatus());
|
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<String, Object>();
|
||||||
|
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<String, Object>();
|
||||||
|
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<String, Object>();
|
||||||
|
body.put("max_tokens", 512);
|
||||||
|
OpenAiCompatibleProvider.prepareRequestForModel("claude-sonnet-4-5-20250929-thinking", body);
|
||||||
|
assertEquals(2048, body.get("max_tokens"));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user