test gateway readiness and classify upstream failures
This commit is contained in:
1 parent
e86a7b1e79
commit
5652a52998
8 files changed
+226
-12
No files matched your search
@@ -482,6 +482,9 @@ public class CatalogService {
|
||||
putIfNotNull(model, "priority", resultSet.getObject("priority"));
|
||||
putIfNotNull(model, "maxConcurrency", resultSet.getObject("max_concurrency"));
|
||||
model.put("status", resultSet.getString("status"));
|
||||
// Do not advertise real asynchronous routes before their worker exists.
|
||||
model.put("executionReady", !"async".equals(resultSet.getString("call_mode"))
|
||||
|| "provider-mock".equals(resultSet.getString("provider_id")));
|
||||
model.put("createdAt", resultSet.getObject("created_at", OffsetDateTime.class));
|
||||
return model;
|
||||
}
|
||||
|
||||
+29
-5
@@ -59,7 +59,7 @@ public class OpenAiCompatibleProvider {
|
||||
if(bytes.length>4*1024*1024) throw new ApiException("UPSTREAM_RESPONSE_TOO_LARGE","上游响应超过限制",502);
|
||||
return objectMapper.readValue(bytes, new com.fasterxml.jackson.core.type.TypeReference<>() {});
|
||||
} catch(ApiException e) { throw e; }
|
||||
catch(Exception e) { throw upstreamError(); }
|
||||
catch(Exception e) { throw classifyUpstreamError(502, ""); }
|
||||
}
|
||||
|
||||
public StreamResponse stream(String providerId, String upstreamModel, Map<String,Object> input) {
|
||||
@@ -88,8 +88,8 @@ public class OpenAiCompatibleProvider {
|
||||
long started=System.nanoTime();
|
||||
HttpResponse<InputStream> response = httpClient.send(request, HttpResponse.BodyHandlers.ofInputStream());
|
||||
if (response.statusCode() < 200 || response.statusCode() >= 300) {
|
||||
response.body().close();
|
||||
throw upstreamError();
|
||||
String details = readErrorBody(response.body());
|
||||
throw classifyUpstreamError(response.statusCode(), details);
|
||||
}
|
||||
if(streaming && !response.headers().firstValue("Content-Type").orElse("").toLowerCase().startsWith("text/event-stream")) {
|
||||
response.body().close();
|
||||
@@ -132,7 +132,7 @@ public class OpenAiCompatibleProvider {
|
||||
String data=event.substring(0,event.length()-1); event.setLength(0);
|
||||
if("[DONE]".equals(data)) return;
|
||||
Map<String,Object> chunk=objectMapper.readValue(data,new com.fasterxml.jackson.core.type.TypeReference<>(){});
|
||||
if(chunk.containsKey("error")) throw upstreamError();
|
||||
if(chunk.containsKey("error")) throw classifyUpstreamError(502, writeJson(chunk));
|
||||
consumer.accept(chunk);
|
||||
}
|
||||
} else if(text.startsWith("data:")) event.append(text.substring(5).stripLeading()).append('\n');
|
||||
@@ -144,7 +144,31 @@ public class OpenAiCompatibleProvider {
|
||||
}
|
||||
@FunctionalInterface public interface EventConsumer { void accept(Map<String,Object> chunk) throws IOException; }
|
||||
@PreDestroy public void close() { deadlines.shutdownNow(); }
|
||||
private ApiException upstreamError() { return new ApiException("UPSTREAM_PROVIDER_ERROR","上游模型供应商请求失败",502); }
|
||||
private String readErrorBody(InputStream body) {
|
||||
try (body) {
|
||||
return new String(body.readNBytes(8192), StandardCharsets.UTF_8);
|
||||
} catch (IOException ignored) {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Keep provider diagnostics useful without returning the upstream response,
|
||||
* which may contain account details or other sensitive data.
|
||||
*/
|
||||
static ApiException classifyUpstreamError(int status, String body) {
|
||||
String details = body == null ? "" : body.toLowerCase(java.util.Locale.ROOT);
|
||||
if (status == 401 || status == 403 || details.matches(".*(invalid.{0,8}(api[ _-]?key|token)|unauthori[sz]ed|forbidden|无效.*(key|凭据)|无权限|认证失败).*")) {
|
||||
return new ApiException("UPSTREAM_AUTH_FAILED", "上游渠道凭据无效或无权限", 502);
|
||||
}
|
||||
if (status == 402 || details.matches(".*(insufficient|余额不足|额度不足|quota.{0,12}(exceed|insufficient|已用尽)|credit.{0,12}(exhaust|insufficient)|billing|payment required|充值).*")) {
|
||||
return new ApiException("UPSTREAM_CREDIT_INSUFFICIENT", "上游渠道余额或额度不足", 502);
|
||||
}
|
||||
if (status == 429 || details.matches(".*(rate.?limit|too many requests|concurr|并发|频率过高|请求过于频繁|限流).*")) {
|
||||
return new ApiException("UPSTREAM_RATE_LIMITED", "上游渠道限流或并发已满,请稍后重试", 429);
|
||||
}
|
||||
return new ApiException("UPSTREAM_PROVIDER_ERROR", "上游模型供应商请求失败", 502);
|
||||
}
|
||||
|
||||
private String normalizeUrl(String value) {
|
||||
return value == null ? "" : value.replaceAll("/+$", "");
|
||||
|
||||
Reference in new issue
Block a user