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; 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 Map chat(String providerId, String upstreamModel, Map input) { try(var response=open(providerId,upstreamModel,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 open(providerId,upstreamModel,input,true); } private StreamResponse open(String providerId, String upstreamModel, Map input, boolean streaming) { ProviderConfig provider = resolve(providerId); if (provider == null || !provider.hasKey()) { throw new ApiException("PROVIDER_NOT_CONFIGURED", "该模型的供应商尚未配置服务端 API Key", 503); } Map body = new LinkedHashMap<>(input); body.put("model", upstreamModel); body.put("stream", streaming); 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); } 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); } } /** 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 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 { 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; } 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) {} } } @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 (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 (status == 404 || details.matches(".*(no available channel|model.{0,16}(not found|does not exist|unavailable)|not_found|unsupported model|没有可用渠道|模型不存在|模型不可用).*")) { 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(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(List apiKeys, String baseUrl, AtomicInteger cursor) { private boolean hasKey() { return !apiKeys.isEmpty(); } private String nextApiKey() { return apiKeys.get(Math.floorMod(cursor.getAndIncrement(), apiKeys.size())); } } }