package com.moyi.controlplane.provider; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import com.moyi.controlplane.common.ApiException; import com.moyi.controlplane.security.SecretCipher; import java.net.URI; import java.net.SocketTimeoutException; import java.net.http.HttpClient; import java.net.http.HttpRequest; import java.net.http.HttpResponse; import java.net.http.HttpTimeoutException; import java.io.*; import java.nio.charset.StandardCharsets; import java.time.Duration; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.ArrayList; import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.*; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; import java.util.concurrent.atomic.AtomicBoolean; import jakarta.annotation.PreDestroy; import org.springframework.dao.EmptyResultDataAccessException; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.stereotype.Service; @Service public class OpenAiCompatibleProvider { private final ObjectMapper objectMapper; private final HttpClient httpClient; private final JdbcTemplate jdbc; private final SecretCipher secretCipher; private final Map providers; private final org.springframework.core.env.Environment environment; /** Cursor state must survive dynamic provider resolution; otherwise every * DB-backed channel request starts at key 0 and never rotates. */ private final ConcurrentMap keyCursors = new ConcurrentHashMap<>(); private final ScheduledExecutorService deadlines = Executors.newSingleThreadScheduledExecutor(r -> { var t=new Thread(r,"upstream-deadlines"); t.setDaemon(true); return t; }); private final int timeoutSeconds; @org.springframework.beans.factory.annotation.Autowired private UpstreamHealth health; public OpenAiCompatibleProvider( ObjectMapper objectMapper, org.springframework.core.env.Environment environment, JdbcTemplate jdbc, SecretCipher secretCipher ) { this.objectMapper = objectMapper; this.httpClient = HttpClient.newBuilder().connectTimeout(Duration.ofSeconds(10)).build(); this.jdbc = jdbc; this.secretCipher = secretCipher; this.environment = environment; this.timeoutSeconds = Math.max(1,Math.min(90,environment.getProperty("moyi.gateway.upstream-timeout-seconds",Integer.class,90))); this.providers = Map.of( "provider-deepseek", configured("provider-deepseek", environment.getProperty("moyi.providers.deepseek.api-key", ""), environment.getProperty("moyi.providers.deepseek.base-url", "https://api.deepseek.com"), environment), "provider-kimi", configured("provider-kimi", environment.getProperty("moyi.providers.kimi.api-key", ""), environment.getProperty("moyi.providers.kimi.base-url", "https://api.moonshot.ai/v1"), environment), "provider-zhipu", configured("provider-zhipu", environment.getProperty("moyi.providers.zhipu.api-key", ""), environment.getProperty("moyi.providers.zhipu.base-url", "https://open.bigmodel.cn/api/paas/v4"), environment) ); } public boolean isConfigured(String providerId) { ProviderConfig provider = resolve(providerId); return provider != null && provider.hasKey(); } public boolean isConfigured(String providerId, String routeId, String upstreamModel) { ProviderConfig provider = resolve(providerId); return provider != null && provider.hasKey(routeId, upstreamModel, environment); } public Map chat(String providerId, String upstreamModel, Map input) { return chat(providerId, upstreamModel, null, input); } /** Route-aware overload. A route-specific key pool is preferred when configured. */ public Map chat(String providerId, String upstreamModel, String routeId, Map input) { try(var response=open(providerId,upstreamModel,routeId,input,false)) { byte[] bytes=response.body.readNBytes(4*1024*1024+1); if(bytes.length>4*1024*1024) throw new ApiException("UPSTREAM_RESPONSE_TOO_LARGE","上游响应超过限制",502); Map body=objectMapper.readValue(bytes, new com.fasterxml.jackson.core.type.TypeReference<>() {}); if(body.containsKey("error")) throw classifyUpstreamError(200,writeJson(body)); return body; } catch(ApiException e) { throw e; } catch(HttpTimeoutException | SocketTimeoutException e) { throw new ApiException("UPSTREAM_PROVIDER_TIMEOUT","上游模型供应商响应超时",504); } catch(Exception e) { throw classifyUpstreamError(502, ""); } } public StreamResponse stream(String providerId, String upstreamModel, Map input) { return stream(providerId, upstreamModel, null, input); } /** Route-aware overload. A route-specific key pool is preferred when configured. */ public StreamResponse stream(String providerId, String upstreamModel, String routeId, Map input) { return open(providerId,upstreamModel,routeId,input,true); } private StreamResponse open(String providerId, String upstreamModel, String routeId, Map input, boolean streaming) { return openEndpoint(providerId,upstreamModel,routeId,input,streaming,"chat"); } public Map invoke(String providerId,String model,String routeId,Map input,String protocol) { try(var response=openEndpoint(providerId,model,routeId,input,false,protocol)) { byte[] bytes=response.body.readNBytes(4*1024*1024+1); if(bytes.length>4*1024*1024) throw new ApiException("UPSTREAM_RESPONSE_TOO_LARGE","上游响应超过限制",502); Map result=objectMapper.readValue(bytes,new com.fasterxml.jackson.core.type.TypeReference<>(){}); if(result.containsKey("error")) throw classifyUpstreamError(200,writeJson(result)); return result; } catch(ApiException e) {throw e;} catch(Exception e) {throw new ApiException("UPSTREAM_PROVIDER_ERROR","上游响应无法读取",502);} } public StreamResponse nativeStream(String providerId,String model,String routeId,Map input,String protocol) { return openEndpoint(providerId,model,routeId,input,true,protocol); } private StreamResponse openEndpoint(String providerId,String upstreamModel,String routeId,Map input,boolean streaming,String protocol) { ProviderConfig provider = resolve(providerId); if (provider == null || !provider.hasKey(routeId, upstreamModel, environment)) { throw new ApiException("PROVIDER_NOT_CONFIGURED", "该模型的供应商尚未配置服务端 API Key", 503); } Map body = new LinkedHashMap<>(input); body.put("model", upstreamModel); if(!List.of("embeddings","gemini").contains(protocol)) body.put("stream", streaming); if("chat".equals(protocol)&&streaming) body.put("stream_options",Map.of("include_usage",true)); else body.remove("stream_options"); String base=normalizeUrl(provider.baseUrl()); String path=switch(protocol) { case "chat" -> "/chat/completions"; case "messages" -> "/messages"; case "responses" -> "/responses"; case "embeddings" -> "/embeddings"; case "gemini" -> "/models/"+java.net.URLEncoder.encode(upstreamModel,StandardCharsets.UTF_8).replace("+","%20")+(streaming?":streamGenerateContent?alt=sse":":generateContent"); default -> throw new ApiException("UNSUPPORTED_PROTOCOL","协议不支持",400); }; if("gemini".equals(protocol)) {base=base.replaceFirst("/v1$","");if(!base.endsWith("/v1beta")) base+="/v1beta";body.remove("model");body.remove("stream");} String apiKey = provider.nextApiKey(routeId, upstreamModel, environment, keyCursors); List candidates=provider.routeKeys(routeId,upstreamModel,environment); if(candidates.isEmpty()) candidates=provider.apiKeys(); String healthRoute=routeId==null?providerId:routeId; var attempted=new java.util.HashSet(); if(health!=null) { apiKey=health.select(healthRoute,candidates,attempted,Math.max(0,candidates.indexOf(apiKey))); if(apiKey==null) throw new UpstreamRejectedException(new ApiException("UPSTREAM_ACCOUNTS_COOLING","渠道账号暂时隔离,请稍后重试",503)); } boolean compatibilityRetried=false; long overallStarted=System.nanoTime(); if(org.springframework.web.context.request.RequestContextHolder.getRequestAttributes() instanceof org.springframework.web.context.request.ServletRequestAttributes attrs) { Object previous=attrs.getRequest().getAttribute("moyi.upstreamStarted"); if(previous instanceof Long value) overallStarted=value;else attrs.getRequest().setAttribute("moyi.upstreamStarted",overallStarted); } for (int attempt = 0; attempt < 4; attempt++) { long remaining=timeoutSeconds*1000L-TimeUnit.NANOSECONDS.toMillis(System.nanoTime()-overallStarted); if(remaining<=0) throw new ApiException("UPSTREAM_PROVIDER_TIMEOUT","上游请求超时",504); String json = writeJson(body); HttpRequest.Builder builder = HttpRequest.newBuilder() .uri(URI.create(base + path)) .timeout(Duration.ofMillis(remaining)) .header("Authorization", "Bearer " + apiKey) .header("Content-Type", "application/json") .POST(HttpRequest.BodyPublishers.ofString(json)); if("messages".equals(protocol)) builder.header("x-api-key",apiKey).header("anthropic-version","2023-06-01"); if("gemini".equals(protocol)) builder.header("x-goog-api-key",apiKey); HttpRequest request=builder.build(); try { long started=System.nanoTime(); HttpResponse response = httpClient.send(request, HttpResponse.BodyHandlers.ofInputStream()); if(health!=null) health.record(healthRoute,apiKey,response.statusCode(),(int)TimeUnit.NANOSECONDS.toMillis(System.nanoTime()-started)); if (response.statusCode() < 200 || response.statusCode() >= 300) { String details; try(var bounded=new StreamResponse(response.body(),Math.max(1,timeoutSeconds*1000L-TimeUnit.NANOSECONDS.toMillis(System.nanoTime()-overallStarted)))) {details=readErrorBody(bounded.body);} if (requiresLargerOutputBudget(details)) { throw new ApiException("INVALID_MAX_TOKENS", "上游模型要求 max_tokens 大于 1024,请提高输出 Token 上限后重试", 400); } // Some OpenAI-compatible relays use the newer spelling for // reasoning-model budgets. Retry only this deterministic // GPT request-shape error; other failures remain bill-safe. if ("chat".equals(protocol) && !compatibilityRetried && prepareCompatibilityRetry(body, details)) {compatibilityRetried=true;continue;} if(health!=null && List.of(401,403,429).contains(response.statusCode()) && attempt<3 && TimeUnit.NANOSECONDS.toSeconds(System.nanoTime()-overallStarted) body, String details) { String text = details == null ? "" : details.toLowerCase(java.util.Locale.ROOT); String model = String.valueOf(body.getOrDefault("model", "")).toLowerCase(java.util.Locale.ROOT); if (model.startsWith("gpt-") && 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; } return false; } static boolean requiresLargerOutputBudget(String details) { String text = details == null ? "" : details.toLowerCase(java.util.Locale.ROOT); return text.matches(".*max_tokens.{0,80}(must be greater than|minimum is|at least).{0,20}1024.*"); } 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. */ public final class StreamResponse implements AutoCloseable { private final InputStream body; private final AtomicBoolean expired=new AtomicBoolean(); private final ScheduledFuture deadline; private Runnable afterClose=()->{}; public StreamResponse onClose(Runnable callback) {afterClose=callback;return this;} private StreamResponse(InputStream body,long remainingMs) { this.body=body; this.deadline=deadlines.schedule(() -> { expired.set(true); try { body.close(); } catch(IOException ignored) {} },remainingMs,TimeUnit.MILLISECONDS); } public void events(EventConsumer consumer) throws IOException { events(consumer,true); } public void events(EventConsumer consumer,boolean requireDone) throws IOException { var reader=new BufferedReader(new InputStreamReader(body,StandardCharsets.UTF_8)); StringBuilder event=new StringBuilder(), line=new StringBuilder(); long total=0; int c; try { // Bound both individual frames and the whole response before allocating unbounded strings. while((c=reader.read())!=-1) { if(expired.get()) throw new ApiException("UPSTREAM_PROVIDER_TIMEOUT","上游响应超时,本次未扣费",504); if(++total>4*1024*1024 || line.length()>262144 || event.length()>262144) throw new ApiException("UPSTREAM_RESPONSE_TOO_LARGE","上游响应超过限制",502); if(c=='\n') { String text=line.toString(); line.setLength(0); if(text.endsWith("\r")) text=text.substring(0,text.length()-1); if(text.isEmpty()) { if(!event.isEmpty()) { String data=event.substring(0,event.length()-1); event.setLength(0); if("[DONE]".equals(data)) return; Map chunk=objectMapper.readValue(data,new com.fasterxml.jackson.core.type.TypeReference<>(){}); 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'); } else line.append((char)c); } } catch(IOException exception) { if(expired.get()) throw new ApiException("UPSTREAM_PROVIDER_TIMEOUT","上游响应超时,本次未扣费",504); throw exception; } if(!requireDone && !expired.get() && event.isEmpty() && line.isEmpty()) return; throw new ApiException(expired.get()?"UPSTREAM_PROVIDER_TIMEOUT":"UPSTREAM_STREAM_INCOMPLETE","上游流式响应中断,本次未扣费",502); } @Override public void close() { deadline.cancel(false); try { body.close(); } catch(IOException ignored) {} finally {afterClose.run();afterClose=()->{};} } } @FunctionalInterface public interface EventConsumer { void accept(Map chunk) throws IOException; } @PreDestroy public void close() { deadlines.shutdownNow(); } 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 (requiresLargerOutputBudget(details)) { return new ApiException("INVALID_MAX_TOKENS", "上游模型要求 max_tokens 大于 1024,请提高输出 Token 上限后重试", 400); } if (status == 408 || status == 504 || details.matches(".*(request|gateway|upstream).{0,12}(timed out|timeout|超时).*")) { return new ApiException("UPSTREAM_PROVIDER_TIMEOUT", "上游模型供应商响应超时", 504); } 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); } 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); } private String normalizeUrl(String value) { return value == null ? "" : value.replaceAll("/+$", ""); } private ProviderConfig resolve(String providerId) { ProviderConfig fixed = providers.get(providerId); if (fixed != null && fixed.hasKey()) return fixed; try { Map row = jdbc.queryForMap("SELECT code, base_url, credential_ciphertext FROM model_providers WHERE id = ? AND status = 'active'", providerId); String providerCode = row.get("code") == null ? "" : String.valueOf(row.get("code")); String ciphertext = row.get("credential_ciphertext") == null ? "" : String.valueOf(row.get("credential_ciphertext")); String apiKey = ciphertext.isBlank() ? "" : secretCipher.decrypt(ciphertext); String baseUrl = row.get("base_url") == null ? "" : String.valueOf(row.get("base_url")); return configured(providerId, providerCode, apiKey, baseUrl, environment, keyCursors.computeIfAbsent(providerId, ignored -> new AtomicInteger())); } catch (EmptyResultDataAccessException exception) { return fixed; } } private String writeJson(Object value) { try { return objectMapper.writeValueAsString(value); } catch (JsonProcessingException exception) { throw new ApiException("INVALID_REQUEST", "请求内容无法编码", 400); } } private ProviderConfig configured(String providerId, String fallbackKey, String baseUrl, org.springframework.core.env.Environment environment) { return configured(providerId, providerId, fallbackKey, baseUrl, environment, new AtomicInteger()); } private ProviderConfig configured(String providerId, String providerCode, String fallbackKey, String baseUrl, org.springframework.core.env.Environment environment) { return configured(providerId, providerCode, fallbackKey, baseUrl, environment, new AtomicInteger()); } private ProviderConfig configured(String providerId, String providerCode, String fallbackKey, String baseUrl, org.springframework.core.env.Environment environment, AtomicInteger cursor) { List keys = new ArrayList<>(); if (fallbackKey != null && !fallbackKey.isBlank()) keys.add(fallbackKey.trim()); addEnvironmentKeys(keys, providerId, environment); if (providerCode != null && !providerCode.isBlank() && !providerCode.equals(providerId)) addEnvironmentKeys(keys, providerCode, environment); return new ProviderConfig(providerId, providerCode, keys.stream().distinct().toList(), baseUrl, cursor); } private void addEnvironmentKeys(List keys, String alias, org.springframework.core.env.Environment environment) { String envName = "MOYI_UPSTREAM_API_KEYS_" + alias.toUpperCase(java.util.Locale.ROOT).replace('-', '_'); String configured = environment == null ? null : environment.getProperty(envName); if (configured != null && !configured.isBlank()) for (String key : configured.split("\\|")) if (!key.isBlank()) keys.add(key.trim()); } private record ProviderConfig(String alias, String providerCode, List apiKeys, String baseUrl, AtomicInteger cursor) { private boolean hasKey() { return !apiKeys.isEmpty(); } private boolean hasKey(String routeId, String upstreamModel, org.springframework.core.env.Environment environment) { return !apiKeys.isEmpty() || !routeKeys(routeId, upstreamModel, environment).isEmpty(); } private String nextApiKey() { return apiKeys.get(Math.floorMod(cursor.getAndIncrement(), apiKeys.size())); } private String nextApiKey(String routeId, String upstreamModel, org.springframework.core.env.Environment environment, ConcurrentMap cursors) { List routeKeys = routeKeys(routeId, upstreamModel, environment); if (routeKeys.isEmpty()) return nextApiKey(); String cursorKey = "route:" + (routeId == null ? "" : routeId); AtomicInteger routeCursor = cursors.computeIfAbsent(cursorKey, ignored -> new AtomicInteger()); return routeKeys.get(Math.floorMod(routeCursor.getAndIncrement(), routeKeys.size())); } private List routeKeys(String routeId, String upstreamModel, org.springframework.core.env.Environment environment) { if (environment == null) return List.of(); if (routeId != null && !routeId.isBlank()) { String value = environment.getProperty("MOYI_UPSTREAM_API_KEYS_ROUTE_" + normalize(routeId)); if (value != null && !value.isBlank()) return parseKeys(value); } if (upstreamModel != null && !upstreamModel.isBlank()) { String scopedName = "MOYI_UPSTREAM_API_KEYS_MODEL_" + normalize(providerCode) + "_" + normalize(upstreamModel); String value = environment.getProperty(scopedName); if (value == null || value.isBlank()) return List.of(); return parseKeys(value); } return List.of(); } private static List parseKeys(String value) { List keys = new ArrayList<>(); for (String key : value.split("\\|")) if (!key.isBlank()) keys.add(key.trim()); return keys.stream().distinct().toList(); } private static String normalize(String value) { return String.valueOf(value == null ? "" : value).toUpperCase(java.util.Locale.ROOT).replaceAll("[^A-Z0-9]", "_"); } } }