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

269 lines
16 KiB
Java

package com.moyi.controlplane.gateway;
import com.moyi.controlplane.catalog.CatalogService;
import com.moyi.controlplane.common.ApiException;
import com.moyi.controlplane.provider.OpenAiCompatibleProvider;
import com.moyi.controlplane.wallet.ChatBillingService;
import com.moyi.controlplane.gateway.request.ApiRequestContext;
import com.moyi.controlplane.gateway.request.ApiRequestMetadata;
import com.moyi.controlplane.gateway.request.ApiRequestProtocol;
import com.moyi.controlplane.gateway.request.GatewayRequestExecutionService;
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.Instant;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/v1")
public class GatewayController {
private final CatalogService catalogService;
private final OpenAiCompatibleProvider provider;
private final GatewayGuard guard;
private final GatewayRequestExecutionService requests;
private final ObjectMapper json;
@org.springframework.beans.factory.annotation.Autowired private GatewayFailover failover;
public GatewayController(CatalogService catalogService, OpenAiCompatibleProvider provider, GatewayGuard guard,
GatewayRequestExecutionService requests, ObjectMapper json) {
this.catalogService = catalogService;
this.provider = provider;
this.guard = guard;
this.requests = requests;
this.json = json;
}
@GetMapping("/models")
public Map<String, Object> models(@RequestHeader(value = "Authorization", required = false) String authorization) {
Map<String, Object> key = requireKey(authorization);
guard.checkRate(UUID.fromString(String.valueOf(key.get("id"))), true);
List<Map<String, Object>> visibleModels = GatewayModelIds.authorizedModels(catalogService, key);
List<Map<String, Object>> data = visibleModels.stream()
.map(model -> {
Map<String, Object> item = new LinkedHashMap<>();
String publicId = GatewayModelIds.publicId(model);
item.put("id", publicId);
item.put("name", model.get("publicName"));
item.put("object", "model");
item.put("owned_by", model.get("providerId"));
item.put("moyi_route_id", model.get("id"));
item.put("model_key", model.get("modelKey"));
item.put("public_name", model.get("publicName"));
item.put("vendor_name", model.get("vendorName"));
item.put("source_label", model.get("sourceLabel"));
item.put("upstream_model", model.get("upstreamModel"));
item.put("type", model.get("type"));
item.put("mode", model.get("mode"));
item.put("supported_protocols", model.getOrDefault("supportedProtocols",List.of()));
item.put("execution_ready", model.getOrDefault("executionReady",false));
item.put("billing_ready", model.getOrDefault("billingReady",false));
return item;
}).toList();
catalogService.touchKey(key);
return Map.of("object", "list", "data", data);
}
@PostMapping("/chat/completions")
public ResponseEntity<Map<String, Object>> chatCompletions(
@RequestHeader(value = "Authorization", required = false) String authorization,
@RequestBody Map<String, Object> input,
@RequestHeader(value = "Idempotency-Key", required = false) String idempotencyKey,
HttpServletRequest request,
HttpServletResponse servletResponse
) {
return executeChat(requireKey(authorization), input, idempotencyKey, servletResponse,
ApiRequestMetadata.fromHttp(request.getRemoteAddr()));
}
/** Direct-call compatibility for application tests and non-HTTP adapters. */
public ResponseEntity<Map<String, Object>> chatCompletions(String authorization,
Map<String, Object> input, String idempotencyKey, HttpServletResponse servletResponse) {
return executeChat(requireKey(authorization), input, idempotencyKey, servletResponse, ApiRequestMetadata.internal());
}
// Public API and Playground share the same request execution and risk boundary.
public ResponseEntity<Map<String, Object>> executeChat(Map<String, Object> key,
Map<String, Object> input, String idempotencyKey, HttpServletResponse servletResponse) {
return executeChat(key, input, idempotencyKey, servletResponse, ApiRequestMetadata.internal());
}
public ResponseEntity<Map<String, Object>> executeChat(Map<String, Object> key,
Map<String, Object> input, String idempotencyKey, HttpServletResponse servletResponse, ApiRequestMetadata metadata) {
long startedAt = System.currentTimeMillis();
UUID keyId = UUID.fromString(String.valueOf(key.get("id")));
// Reject request floods before catalog reads and payload normalization, including malformed requests.
guard.checkRate(keyId, false);
String modelId = String.valueOf(input.getOrDefault("model", ""));
Map<String, Object> model = resolveModel(key, modelId);
if (model == null) throw new ApiException("MODEL_NOT_FOUND", "模型不存在或已下线;请使用当前 API Key 的 /v1/models 返回的模型 ID", 404);
if ("async".equals(model.get("mode"))) throw new ApiException("ASYNC_MODEL", "该模型需要使用异步任务接口", 400);
if(!List.of("chat","text","code","multimodal","llm","reasoning","vision").contains(String.valueOf(model.get("type")))) throw new ApiException("WRONG_MODEL_INTERFACE","该模型需要使用模型目录标明的接口",400);
String providerId=String.valueOf(model.get("providerId"));
String routeId=String.valueOf(model.get("id"));
boolean mock="provider-mock".equals(providerId);
if(!mock && !provider.isConfigured(providerId, routeId, String.valueOf(model.get("upstreamModel")))) throw new ApiException("PROVIDER_NOT_CONFIGURED","该供应商尚未配置密钥,不能执行真实调用",503);
ChatMeter.Request request=ChatMeter.request(input,json);
ApiRequestContext context = new ApiRequestContext(
UUID.fromString(String.valueOf(key.get("userId"))), keyId, ApiRequestProtocol.CHAT_COMPLETIONS,
modelId, routeId, Boolean.TRUE.equals(request.body().get("stream")), request.reservedInputTokens(),
request.maxOutputTokens(), metadata, System.nanoTime());
return requests.execute(context, key, model, request, idempotencyKey, () -> {
// A queued request never consumes one scarce slot while waiting for another.
return guard.acquire(model, keyId, guard.queueWaitMs());
}, ticket -> {
servletResponse.setHeader("X-Request-Id",ticket.id().toString());
catalogService.touchKey(key);
if(Boolean.TRUE.equals(request.body().get("stream"))) {
stream(key,model,modelId,routeId,request.body(),ticket,servletResponse,startedAt,mock);
return null;
}
Map<String,Object> body=mock?mockResponse(modelId,input):failover!=null?failover.invoke(key,model,"chat",request.body(),ticket):provider.chat(providerId,String.valueOf(model.get("upstreamModel")),routeId,request.body());
body.put("model",modelId);
requests.observedUsage(ticket,"chat",body.get("usage"));
requireCompletion(body);
var cost=requests.settle(ticket,body,elapsed(startedAt),null);
return ResponseEntity.ok().header("X-Request-Id",ticket.id().toString()).header("X-Moyi-Cost-CNY",cost.toPlainString()).body(body);
}, exception -> exception instanceof ApiException api ? api
: new ApiException("CALL_INTERRUPTED","调用中断,请查看使用记录确认结算状态",502), error -> {
if(servletResponse.isCommitted()) {
try { event(servletResponse,Map.of("error",Map.of("code",error.getCode(),"message",error.getMessage()))); done(servletResponse); } catch(IOException ignored) { }
return null;
}
servletResponse.resetBuffer();
servletResponse.setContentType("application/json");
throw error;
});
}
private void stream(Map<String,Object> key,Map<String,Object> model, String modelId, String routeId, Map<String,Object> input, ChatBillingService.Ticket ticket, HttpServletResponse response, long started, boolean mock) throws IOException {
if(mock) {
var body=mockResponse(modelId,input);
prepareStream(response);
var choices=(List<?>)body.get("choices");
var message=((Map<?,?>)choices.getFirst()).get("message");
event(response,Map.of("id",body.get("id"),"object","chat.completion.chunk","model",modelId,"choices",List.of(Map.of("index",0,"delta",message))));
requests.observedUsage(ticket,"chat",body.get("usage"));
requests.settle(ticket,body,elapsed(started),elapsed(started));
event(response,Map.of("id",body.get("id"),"object","chat.completion.chunk","model",modelId,"choices",List.of(Map.of("index",0,"delta",Map.of(),"finish_reason","stop"))));
event(response,Map.of("choices",List.of(),"usage",body.get("usage"))); done(response); return;
}
// Keep admission, the concurrency slot and the upstream stream alive on this request thread.
// Upstream HTTP errors are inspected before committing a 200/SSE response.
try(var upstream=failover!=null?failover.stream(key,model,"chat",input,ticket):provider.stream(model.get("providerId").toString(),model.get("upstreamModel").toString(),routeId,input)) {
var collector=new ChatStreamCollector(modelId,started);
prepareStream(response);
boolean[] disconnected={false};
try { upstream.events(chunk -> {
chunk.put("model",modelId);
collector.accept(chunk);
// A closed browser must not turn an otherwise completed provider call into free usage.
// Continue within the same hard deadline to obtain the final meter and settle once.
if(!disconnected[0]) try { event(response,chunk); } catch(IOException clientClosed) { disconnected[0]=true; }
}); } finally {requests.observedUsage(ticket,"chat",collector.observedUsage());}
var body=collector.result();
requests.settle(ticket,body,elapsed(started),collector.firstTokenMs());
if(!disconnected[0]) done(response);
}
}
private void prepareStream(HttpServletResponse response) {
response.setContentType("text/event-stream"); response.setCharacterEncoding("UTF-8");
response.setHeader("Cache-Control","no-cache"); response.setHeader("X-Accel-Buffering","no");
}
private void event(HttpServletResponse response,Object data) throws IOException {
response.getOutputStream().write(("data: "+json.writeValueAsString(data)+"\n\n").getBytes(java.nio.charset.StandardCharsets.UTF_8));
response.flushBuffer();
}
private void done(HttpServletResponse response) throws IOException {
response.getOutputStream().write("data: [DONE]\n\n".getBytes(java.nio.charset.StandardCharsets.UTF_8)); response.flushBuffer();
}
private int elapsed(long started) { return (int)Math.min(Integer.MAX_VALUE,System.currentTimeMillis()-started); }
private void requireCompletion(Map<String,Object> body) {
if(body.containsKey("error") || !(body.get("choices") instanceof List<?> choices) || choices.isEmpty()
|| !(choices.getFirst() instanceof Map<?,?> choice) || choice.get("finish_reason")==null || !hasAssistantOutput(choice.get("message")))
throw new ApiException("UPSTREAM_RESPONSE_INVALID","上游未返回完整的聊天结果,本次未扣费",502);
}
/**
* Reasoning models may finish with an empty final content field while
* returning reasoning_content. That is a valid, billable upstream result
* and the UI renders the reasoning separately. A response with neither
* content, reasoning, nor tool calls is a genuine empty response and must
* not be settled as a successful paid call.
*/
private boolean hasAssistantOutput(Object rawMessage) {
if (!(rawMessage instanceof Map<?,?> message)) return false;
if (hasAssistantValue(message.get("content"))) return true;
for (String field : List.of("reasoning_content", "reasoning", "thinking")) {
if (hasText(message.get(field))) return true;
}
if (message.get("tool_calls") instanceof List<?> calls) {
return calls.stream().anyMatch(call -> call instanceof Map<?,?> item && !item.isEmpty());
}
return false;
}
private boolean hasAssistantValue(Object value) {
if (hasText(value)) return true;
if (!(value instanceof List<?> parts)) return false;
return parts.stream().anyMatch(part -> {
if (!(part instanceof Map<?,?> map)) return hasText(part);
for (String field : List.of("text", "content", "thinking", "reasoning", "reasoning_content")) {
if (hasText(map.get(field))) return true;
}
return false;
});
}
private boolean hasText(Object value) {
return value instanceof String text && !text.isBlank();
}
private Map<String,Object> mockResponse(String modelId,Map<String,Object> input) {
String prompt = extractPrompt(input.get("messages"));
String content = "MOYI Java Mock 已收到:" + (prompt.isBlank() ? "(空消息)" : prompt);
Map<String, Object> response = new LinkedHashMap<>();
response.put("id", "chatcmpl-" + UUID.randomUUID().toString().replace("-", "").substring(0, 12));
response.put("object", "chat.completion");
response.put("created", Instant.now().getEpochSecond());
response.put("model", modelId);
response.put("choices", List.of(Map.of("index", 0, "message", Map.of("role", "assistant", "content", content), "finish_reason", "stop")));
response.put("usage", Map.of("prompt_tokens", prompt.length(), "completion_tokens", content.length(), "total_tokens", prompt.length() + content.length()));
return response;
}
private Map<String, Object> requireKey(String authorization) {
if (authorization == null || !authorization.startsWith("Bearer ")) throw new ApiException("UNAUTHENTICATED", "请提供有效的 API Key", 401);
Map<String, Object> key = catalogService.authenticateKey(authorization.substring(7));
if (key == null) throw new ApiException("INVALID_API_KEY", "API Key 无效或已停用", 401);
return key;
}
private String extractPrompt(Object messages) {
if (!(messages instanceof List<?> list)) return "";
return list.stream().filter(item -> item instanceof Map<?, ?>).map(item -> {
Object content = ((Map<?, ?>) item).get("content");
return content == null ? "" : String.valueOf(content);
}).reduce((left, right) -> left + "\n" + right).orElse("");
}
/**
* Resolve a caller supplied model identifier to a route allowed by this
* API key. Route UUIDs remain valid for existing clients; the readable IDs
* returned by /v1/models are preferred for new clients.
*/
Map<String, Object> resolveModel(Map<String, Object> key, String requested) {
return GatewayModelIds.resolve(catalogService, key, requested);
}
}