2026-09-07 16:54:43 +08:00
|
|
|
package com.moyi.controlplane.gateway;
|
|
|
|
|
|
|
|
|
|
import com.moyi.controlplane.catalog.CatalogService;
|
|
|
|
|
import com.moyi.controlplane.common.ApiException;
|
2026-09-07 17:04:23 +08:00
|
|
|
import com.moyi.controlplane.provider.OpenAiCompatibleProvider;
|
2026-09-08 20:43:04 +08:00
|
|
|
import com.moyi.controlplane.usage.UsageAuditService;
|
2026-09-07 16:54:43 +08:00
|
|
|
import java.time.Instant;
|
|
|
|
|
import java.util.LinkedHashMap;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
import java.util.UUID;
|
|
|
|
|
import org.springframework.http.HttpStatus;
|
|
|
|
|
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;
|
2026-09-07 17:04:23 +08:00
|
|
|
private final OpenAiCompatibleProvider provider;
|
2026-09-08 18:02:45 +08:00
|
|
|
private final GatewayGuard guard;
|
2026-09-08 20:43:04 +08:00
|
|
|
private final UsageAuditService usage;
|
2026-09-07 16:54:43 +08:00
|
|
|
|
2026-09-08 20:43:04 +08:00
|
|
|
public GatewayController(CatalogService catalogService, OpenAiCompatibleProvider provider, GatewayGuard guard, UsageAuditService usage) {
|
2026-09-07 16:54:43 +08:00
|
|
|
this.catalogService = catalogService;
|
2026-09-07 17:04:23 +08:00
|
|
|
this.provider = provider;
|
2026-09-08 18:02:45 +08:00
|
|
|
this.guard = guard;
|
2026-09-08 20:43:04 +08:00
|
|
|
this.usage = usage;
|
2026-09-07 16:54:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@GetMapping("/models")
|
|
|
|
|
public Map<String, Object> models(@RequestHeader(value = "Authorization", required = false) String authorization) {
|
|
|
|
|
Map<String, Object> key = requireKey(authorization);
|
2026-09-08 18:02:45 +08:00
|
|
|
guard.checkRate(UUID.fromString(String.valueOf(key.get("id"))), true);
|
2026-09-07 16:54:43 +08:00
|
|
|
List<Map<String, Object>> data = catalogService.listModels(false).stream()
|
|
|
|
|
.filter(model -> catalogService.keyCanInvoke(key, String.valueOf(model.get("id"))))
|
|
|
|
|
.map(model -> {
|
|
|
|
|
Map<String, Object> item = new LinkedHashMap<>();
|
|
|
|
|
item.put("id", model.get("id"));
|
|
|
|
|
item.put("object", "model");
|
|
|
|
|
item.put("owned_by", model.get("providerId"));
|
|
|
|
|
item.put("type", model.get("type"));
|
|
|
|
|
item.put("mode", model.get("mode"));
|
|
|
|
|
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
|
|
|
|
|
) {
|
|
|
|
|
Map<String, Object> key = requireKey(authorization);
|
2026-09-08 20:43:04 +08:00
|
|
|
long startedAt = System.currentTimeMillis();
|
2026-09-07 16:54:43 +08:00
|
|
|
String modelId = String.valueOf(input.getOrDefault("model", ""));
|
|
|
|
|
Map<String, Object> model = catalogService.activeModel(modelId);
|
|
|
|
|
if (model == null) throw new ApiException("MODEL_NOT_FOUND", "模型不存在或已下线", 404);
|
|
|
|
|
if (!catalogService.keyCanInvoke(key, modelId)) throw new ApiException("MODEL_FORBIDDEN", "当前 Key 无权调用该模型", 403);
|
|
|
|
|
if ("async".equals(model.get("mode"))) throw new ApiException("ASYNC_MODEL", "该模型需要使用异步任务接口", 400);
|
2026-09-08 18:02:45 +08:00
|
|
|
if (Boolean.TRUE.equals(input.get("stream"))) throw new ApiException("STREAM_NOT_IMPLEMENTED", "流式转发将在下一节点开放", 501);
|
|
|
|
|
guard.checkRate(UUID.fromString(String.valueOf(key.get("id"))), false);
|
|
|
|
|
try (GatewayGuard.Slot slot = guard.acquire(model)) {
|
2026-09-08 20:43:04 +08:00
|
|
|
ResponseEntity<Map<String, Object>> result = invoke(key, modelId, model, input);
|
|
|
|
|
Map<String, Object> body = result.getBody();
|
|
|
|
|
usage.recordCall(UUID.fromString(String.valueOf(key.get("userId"))), UUID.fromString(String.valueOf(key.get("id"))), modelId, String.valueOf(model.get("providerId")), "succeeded", (int) (System.currentTimeMillis() - startedAt), number(body, "usage.prompt_tokens"), number(body, "usage.completion_tokens"), input, body, null, null);
|
|
|
|
|
return result;
|
|
|
|
|
} catch (ApiException exception) {
|
|
|
|
|
usage.recordCall(UUID.fromString(String.valueOf(key.get("userId"))), UUID.fromString(String.valueOf(key.get("id"))), modelId, String.valueOf(model.get("providerId")), "failed", (int) (System.currentTimeMillis() - startedAt), 0, 0, input, null, exception.getCode(), exception.getMessage());
|
|
|
|
|
throw exception;
|
2026-09-08 18:02:45 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private ResponseEntity<Map<String, Object>> invoke(Map<String, Object> key, String modelId, Map<String, Object> model, Map<String, Object> input) {
|
2026-09-07 17:04:23 +08:00
|
|
|
String providerId = String.valueOf(model.get("providerId"));
|
|
|
|
|
if (provider.isConfigured(providerId)) {
|
|
|
|
|
List<?> messages = input.get("messages") instanceof List<?> values ? values : List.of();
|
|
|
|
|
Map<String, Object> upstream = provider.chat(providerId, String.valueOf(model.get("upstreamModel")), messages);
|
|
|
|
|
upstream.put("model", modelId);
|
|
|
|
|
catalogService.touchKey(key);
|
|
|
|
|
return ResponseEntity.ok(upstream);
|
|
|
|
|
}
|
2026-09-10 18:10:31 +08:00
|
|
|
if (!"provider-mock".equals(providerId)) {
|
|
|
|
|
throw new ApiException("PROVIDER_NOT_CONFIGURED", "该供应商尚未配置密钥,不能执行真实调用", 503);
|
|
|
|
|
}
|
2026-09-07 16:54:43 +08:00
|
|
|
String prompt = extractPrompt(input.get("messages"));
|
|
|
|
|
String content = "MOYI Java Mock 已收到:" + (prompt.isBlank() ? "(空消息)" : prompt);
|
|
|
|
|
catalogService.touchKey(key);
|
|
|
|
|
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 ResponseEntity.status(HttpStatus.OK).body(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("");
|
|
|
|
|
}
|
2026-09-08 20:43:04 +08:00
|
|
|
|
|
|
|
|
private long number(Map<String, Object> body, String path) {
|
|
|
|
|
if (body == null) return 0;
|
|
|
|
|
Object usageValue = body.get("usage");
|
|
|
|
|
if (!(usageValue instanceof Map<?, ?> map)) return 0;
|
|
|
|
|
String key = path.substring(path.indexOf('.') + 1);
|
|
|
|
|
Object value = map.get(key);
|
|
|
|
|
return value instanceof Number number ? number.longValue() : 0;
|
|
|
|
|
}
|
2026-09-07 16:54:43 +08:00
|
|
|
}
|