Files
moyi-model-intelligence-hub/control-plane-java/src/main/java/com/moyi/controlplane/provider/OpenAiCompatibleProvider.java
T

238 lines
14 KiB
Java
Raw Normal View History

2026-09-07 17:04:23 +08:00
package com.moyi.controlplane.provider;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.moyi.controlplane.common.ApiException;
2026-09-07 17:23:18 +08:00
import com.moyi.controlplane.security.SecretCipher;
2026-09-07 17:04:23 +08:00
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.io.*;
import java.nio.charset.StandardCharsets;
2026-09-07 17:04:23 +08:00
import java.time.Duration;
import java.util.LinkedHashMap;
import java.util.List;
2026-09-07 17:04:23 +08:00
import java.util.Map;
import java.util.ArrayList;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.*;
2026-09-27 18:05:35 +08:00
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.atomic.AtomicBoolean;
import jakarta.annotation.PreDestroy;
2026-09-07 17:23:18 +08:00
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
2026-09-07 17:04:23 +08:00
import org.springframework.stereotype.Service;
@Service
public class OpenAiCompatibleProvider {
private final ObjectMapper objectMapper;
private final HttpClient httpClient;
2026-09-07 17:23:18 +08:00
private final JdbcTemplate jdbc;
private final SecretCipher secretCipher;
2026-09-07 17:04:23 +08:00
private final Map<String, ProviderConfig> providers;
private final org.springframework.core.env.Environment environment;
2026-09-27 18:05:35 +08:00
/** Cursor state must survive dynamic provider resolution; otherwise every
* DB-backed channel request starts at key 0 and never rotates. */
private final ConcurrentMap<String, AtomicInteger> 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;
2026-09-07 17:04:23 +08:00
public OpenAiCompatibleProvider(
ObjectMapper objectMapper,
2026-09-07 17:23:18 +08:00
org.springframework.core.env.Environment environment,
JdbcTemplate jdbc,
SecretCipher secretCipher
2026-09-07 17:04:23 +08:00
) {
this.objectMapper = objectMapper;
this.httpClient = HttpClient.newBuilder().connectTimeout(Duration.ofSeconds(10)).build();
2026-09-07 17:23:18 +08:00
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)));
2026-09-07 17:04:23 +08:00
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)
2026-09-07 17:04:23 +08:00
);
}
public boolean isConfigured(String providerId) {
2026-09-07 17:23:18 +08:00
ProviderConfig provider = resolve(providerId);
return provider != null && provider.hasKey();
2026-09-07 17:04:23 +08:00
}
public Map<String, Object> chat(String providerId, String upstreamModel, Map<String,Object> 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);
return objectMapper.readValue(bytes, new com.fasterxml.jackson.core.type.TypeReference<>() {});
} catch(ApiException e) { throw e; }
catch(Exception e) { throw classifyUpstreamError(502, ""); }
}
public StreamResponse stream(String providerId, String upstreamModel, Map<String,Object> input) {
return open(providerId,upstreamModel,input,true);
}
private StreamResponse open(String providerId, String upstreamModel, Map<String,Object> input, boolean streaming) {
2026-09-07 17:23:18 +08:00
ProviderConfig provider = resolve(providerId);
if (provider == null || !provider.hasKey()) {
2026-09-07 17:04:23 +08:00
throw new ApiException("PROVIDER_NOT_CONFIGURED", "该模型的供应商尚未配置服务端 API Key", 503);
}
Map<String, Object> body = new LinkedHashMap<>(input);
2026-09-07 17:04:23 +08:00
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();
2026-09-07 17:04:23 +08:00
String json = writeJson(body);
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(normalizeUrl(provider.baseUrl()) + "/chat/completions"))
.timeout(Duration.ofSeconds(timeoutSeconds))
.header("Authorization", "Bearer " + apiKey)
2026-09-07 17:04:23 +08:00
.header("Content-Type", "application/json")
.POST(HttpRequest.BodyPublishers.ofString(json))
.build();
try {
long started=System.nanoTime();
HttpResponse<InputStream> response = httpClient.send(request, HttpResponse.BodyHandlers.ofInputStream());
2026-09-07 17:04:23 +08:00
if (response.statusCode() < 200 || response.statusCode() >= 300) {
String details = readErrorBody(response.body());
throw classifyUpstreamError(response.statusCode(), details);
2026-09-07 17:04:23 +08:00
}
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)));
2026-09-07 17:04:23 +08:00
} catch (ApiException exception) {
throw exception;
} 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;
// 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<String,Object> 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);
}
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<String,Object> 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 == 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);
}
2026-09-07 17:04:23 +08:00
private String normalizeUrl(String value) {
return value == null ? "" : value.replaceAll("/+$", "");
}
2026-09-07 17:23:18 +08:00
private ProviderConfig resolve(String providerId) {
ProviderConfig fixed = providers.get(providerId);
if (fixed != null && fixed.hasKey()) return fixed;
2026-09-07 17:23:18 +08:00
try {
Map<String, Object> 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"));
2026-09-07 17:23:18 +08:00
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"));
2026-09-27 18:05:35 +08:00
return configured(providerId, providerCode, apiKey, baseUrl, environment,
keyCursors.computeIfAbsent(providerId, ignored -> new AtomicInteger()));
2026-09-07 17:23:18 +08:00
} catch (EmptyResultDataAccessException exception) {
return fixed;
}
}
2026-09-07 17:04:23 +08:00
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) {
2026-09-27 18:05:35 +08:00
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) {
2026-09-27 18:05:35 +08:00
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<String> 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);
2026-09-27 18:05:35 +08:00
return new ProviderConfig(keys.stream().distinct().toList(), baseUrl, cursor);
}
private void addEnvironmentKeys(List<String> 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<String> apiKeys, String baseUrl, AtomicInteger cursor) {
private boolean hasKey() { return !apiKeys.isEmpty(); }
private String nextApiKey() { return apiKeys.get(Math.floorMod(cursor.getAndIncrement(), apiKeys.size())); }
}
2026-09-07 17:04:23 +08:00
}