feat: modularize gateway requests and recover settlements
This commit is contained in:
1 parent
7785cda6a6
commit
5533a483d9
40 files changed
+1598
-232
No files matched your search
@@ -1,14 +1,26 @@
|
|||||||
# MOYI Java Control Plane
|
# MOYI Java Control Plane
|
||||||
|
|
||||||
这是 MOYI Control Plane 的 Java 版本,目标运行时为 Java 21,框架为 Spring Boot。当前已提供健康检查、Flyway 数据库迁移、邀请码认证、用户会话、供应商/模型目录、项目、API Key、模型权限和最小 OpenAI 兼容接口;真实供应商适配、异步任务队列和计费仍按后续节点迁移。
|
这是 MOYI Control Plane 的 Java 版本,目标运行时为 Java 21,框架为 Spring Boot。当前包括健康检查、Flyway 数据库迁移、邀请码认证、用户会话、供应商/模型目录、项目、API Key、模型权限、同步与原生协议网关、异步任务、计费和用量审计。
|
||||||
|
|
||||||
目标依赖边界:
|
目标依赖边界:
|
||||||
|
|
||||||
- PostgreSQL:用户、工作区、供应商、模型目录、Key、计费和审计等持久化数据。
|
- PostgreSQL:用户、会话、供应商、模型目录、Key、任务队列、幂等、上游账号健康、计费和审计等持久化数据。
|
||||||
- Redis:会话、限流、并发计数、幂等键和异步任务队列。
|
- Redis:请求频率、并发准入租约;当前会话、调用幂等和异步任务队列以 PostgreSQL 持久化为准。
|
||||||
- OSS:3D、图片、视频等任务结果和用户上传文件;只保存对象元数据,应用不保存大文件本体。
|
- OSS:3D、图片、视频等任务结果和用户上传文件;只保存对象元数据,应用不保存大文件本体。
|
||||||
- Provider Adapter:服务端调用 DeepSeek、Kimi、GLM、Seedance、Tripo/VAST 和后续中转渠道。
|
- Provider Adapter:服务端调用 DeepSeek、Kimi、GLM、Seedance、Tripo/VAST 和后续中转渠道。
|
||||||
|
|
||||||
|
## API 请求模块边界
|
||||||
|
|
||||||
|
`gateway.request` 是统一的模型 API 请求应用边界。HTTP/Playground 控制器负责认证、协议解析和规范化;请求模块接收不含凭据、提示词和原始请求体的 `ApiRequestContext`,先执行风险策略,再通过准入租约包住钱包预留、Provider 调用和最终结算。Chat、原生协议和异步任务提交共用该风险策略。
|
||||||
|
|
||||||
|
协议字段映射留在各自 Meter/Adapter;`ApiRequestRiskRule` 只能读取最小化上下文并返回决策,不能调用 Provider 或改账。新增协议时应复用准入、风控和账务边界,而不是在 Controller 中另写一套钱包/重试流程。默认 `GATEWAY_RISK_MODE=shadow` 仅记录规则 ID,不改变调用;只有显式部署规则并设置 `enforce` 才会拒绝或限速。请求来源由服务端路由确定,不能信任 `X-Moyi-Source` 作为风控分类。
|
||||||
|
|
||||||
|
账务不变量:风险拒绝必须先于并发准入和钱包预留;预留使用价格快照;失败释放和成功结算沿用现有事务处理;不完整流或无效用量不得结算。上游提交结果未知时的异步核账状态机与异步创建幂等将在后续阶段单独处理。
|
||||||
|
|
||||||
|
同步和原生调用在结算前单独提交精简用量快照(Flyway `V44`)。快照已持久化而账本事务失败时,接口返回 `SETTLEMENT_PENDING`,冻结额度保留,由后台恢复任务重试结算。该快照不保存提示词、生成内容或密钥;不能将其当作响应重放存储。若数据库在快照写入前已不可用,则尚不能保证结果的持久恢复,必须结合上游记录核对。
|
||||||
|
|
||||||
|
`provider.credentials.UpstreamCredentialResolver` 共享密钥池解析与轮转;`provider.transport.SseEventReader` 专门负责 SSE 解帧、UTF-8 校验和有界读取,不承担风控、路由或计费。完整模块边界、验证命令和后续风险清单见 `../docs/21-api-request-modularization.md`。
|
||||||
|
|
||||||
Flyway 脚本位于 `src/main/resources/db/migration/`,与现有 TypeScript 服务使用同一套基础表命名,后续迁移数据时可以按表逐步切换。
|
Flyway 脚本位于 `src/main/resources/db/migration/`,与现有 TypeScript 服务使用同一套基础表命名,后续迁移数据时可以按表逐步切换。
|
||||||
|
|
||||||
本目录的镜像由 `infra/moyi/docker-compose.yml` 构建。切换生产流量前仍需完成真实供应商适配、Redis 限流/队列和完整集成测试;TypeScript Control Plane 继续作为迁移期间的参考实现。
|
本目录的镜像由 `infra/moyi/docker-compose.yml` 构建。生产发布前应验证目标渠道能力、价格快照、Redis 准入和 PostgreSQL 集成测试;本地通过不代表线上部署验收。TypeScript Control Plane 保留为历史参考实现,不与 Java 后端同时承担同一钱包的写入。
|
||||||
+53
-30
@@ -4,7 +4,12 @@ import com.moyi.controlplane.catalog.CatalogService;
|
|||||||
import com.moyi.controlplane.common.ApiException;
|
import com.moyi.controlplane.common.ApiException;
|
||||||
import com.moyi.controlplane.provider.OpenAiCompatibleProvider;
|
import com.moyi.controlplane.provider.OpenAiCompatibleProvider;
|
||||||
import com.moyi.controlplane.wallet.ChatBillingService;
|
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 com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
import jakarta.servlet.http.HttpServletResponse;
|
import jakarta.servlet.http.HttpServletResponse;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.time.Instant;
|
import java.time.Instant;
|
||||||
@@ -26,15 +31,16 @@ public class GatewayController {
|
|||||||
private final CatalogService catalogService;
|
private final CatalogService catalogService;
|
||||||
private final OpenAiCompatibleProvider provider;
|
private final OpenAiCompatibleProvider provider;
|
||||||
private final GatewayGuard guard;
|
private final GatewayGuard guard;
|
||||||
private final ChatBillingService billing;
|
private final GatewayRequestExecutionService requests;
|
||||||
private final ObjectMapper json;
|
private final ObjectMapper json;
|
||||||
@org.springframework.beans.factory.annotation.Autowired private GatewayFailover failover;
|
@org.springframework.beans.factory.annotation.Autowired private GatewayFailover failover;
|
||||||
|
|
||||||
public GatewayController(CatalogService catalogService, OpenAiCompatibleProvider provider, GatewayGuard guard, ChatBillingService billing, ObjectMapper json) {
|
public GatewayController(CatalogService catalogService, OpenAiCompatibleProvider provider, GatewayGuard guard,
|
||||||
|
GatewayRequestExecutionService requests, ObjectMapper json) {
|
||||||
this.catalogService = catalogService;
|
this.catalogService = catalogService;
|
||||||
this.provider = provider;
|
this.provider = provider;
|
||||||
this.guard = guard;
|
this.guard = guard;
|
||||||
this.billing = billing;
|
this.requests = requests;
|
||||||
this.json = json;
|
this.json = json;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -73,15 +79,31 @@ public class GatewayController {
|
|||||||
@RequestHeader(value = "Authorization", required = false) String authorization,
|
@RequestHeader(value = "Authorization", required = false) String authorization,
|
||||||
@RequestBody Map<String, Object> input,
|
@RequestBody Map<String, Object> input,
|
||||||
@RequestHeader(value = "Idempotency-Key", required = false) String idempotencyKey,
|
@RequestHeader(value = "Idempotency-Key", required = false) String idempotencyKey,
|
||||||
|
HttpServletRequest request,
|
||||||
HttpServletResponse servletResponse
|
HttpServletResponse servletResponse
|
||||||
) {
|
) {
|
||||||
return executeChat(requireKey(authorization), input, idempotencyKey, servletResponse);
|
return executeChat(requireKey(authorization), input, idempotencyKey, servletResponse,
|
||||||
|
ApiRequestMetadata.fromHttp(request.getRemoteAddr()));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Both the public gateway and the authenticated playground share admission and billing.
|
/** 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,
|
public ResponseEntity<Map<String, Object>> executeChat(Map<String, Object> key,
|
||||||
Map<String, Object> input, String idempotencyKey, HttpServletResponse servletResponse) {
|
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();
|
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", ""));
|
String modelId = String.valueOf(input.getOrDefault("model", ""));
|
||||||
Map<String, Object> model = resolveModel(key, modelId);
|
Map<String, Object> model = resolveModel(key, modelId);
|
||||||
if (model == null) throw new ApiException("MODEL_NOT_FOUND", "模型不存在或已下线;请使用当前 API Key 的 /v1/models 返回的模型 ID", 404);
|
if (model == null) throw new ApiException("MODEL_NOT_FOUND", "模型不存在或已下线;请使用当前 API Key 的 /v1/models 返回的模型 ID", 404);
|
||||||
@@ -92,27 +114,28 @@ public class GatewayController {
|
|||||||
boolean mock="provider-mock".equals(providerId);
|
boolean mock="provider-mock".equals(providerId);
|
||||||
if(!mock && !provider.isConfigured(providerId, routeId, String.valueOf(model.get("upstreamModel")))) throw new ApiException("PROVIDER_NOT_CONFIGURED","该供应商尚未配置密钥,不能执行真实调用",503);
|
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);
|
ChatMeter.Request request=ChatMeter.request(input,json);
|
||||||
guard.checkRate(UUID.fromString(String.valueOf(key.get("id"))), false);
|
ApiRequestContext context = new ApiRequestContext(
|
||||||
// Reserve both limits atomically. A queued request never consumes a
|
UUID.fromString(String.valueOf(key.get("userId"))), keyId, ApiRequestProtocol.CHAT_COMPLETIONS,
|
||||||
// model slot while waiting for the API-Key slot (or vice versa).
|
modelId, routeId, Boolean.TRUE.equals(request.body().get("stream")), request.reservedInputTokens(),
|
||||||
try (GatewayGuard.Admission admission = guard.acquire(model, UUID.fromString(String.valueOf(key.get("id"))), guard.queueWaitMs())) {
|
request.maxOutputTokens(), metadata, System.nanoTime());
|
||||||
var ticket=billing.reserve(key,model,request,idempotencyKey);
|
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());
|
servletResponse.setHeader("X-Request-Id",ticket.id().toString());
|
||||||
try {
|
catalogService.touchKey(key);
|
||||||
catalogService.touchKey(key);
|
if(Boolean.TRUE.equals(request.body().get("stream"))) {
|
||||||
if(Boolean.TRUE.equals(request.body().get("stream"))) {
|
stream(key,model,modelId,routeId,request.body(),ticket,servletResponse,startedAt,mock);
|
||||||
stream(key,model,modelId,routeId,request.body(),ticket,servletResponse,startedAt,mock);
|
return null;
|
||||||
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());
|
||||||
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);
|
||||||
body.put("model",modelId);
|
requests.observedUsage(ticket,"chat",body.get("usage"));
|
||||||
billing.observedUsage(ticket,"chat",body.get("usage"));
|
requireCompletion(body);
|
||||||
requireCompletion(body);
|
var cost=requests.settle(ticket,body,elapsed(startedAt),null);
|
||||||
var cost=billing.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);
|
||||||
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
|
||||||
} catch(Exception exception) {
|
: new ApiException("CALL_INTERRUPTED","调用中断,请查看使用记录确认结算状态",502), error -> {
|
||||||
ApiException error=exception instanceof ApiException e?e:new ApiException("CALL_INTERRUPTED","调用中断,请查看使用记录确认结算状态",502);
|
|
||||||
billing.fail(ticket,error.getCode(),error.getMessage(),elapsed(startedAt));
|
|
||||||
if(servletResponse.isCommitted()) {
|
if(servletResponse.isCommitted()) {
|
||||||
try { event(servletResponse,Map.of("error",Map.of("code",error.getCode(),"message",error.getMessage()))); done(servletResponse); } catch(IOException ignored) { }
|
try { event(servletResponse,Map.of("error",Map.of("code",error.getCode(),"message",error.getMessage()))); done(servletResponse); } catch(IOException ignored) { }
|
||||||
return null;
|
return null;
|
||||||
@@ -120,8 +143,7 @@ public class GatewayController {
|
|||||||
servletResponse.resetBuffer();
|
servletResponse.resetBuffer();
|
||||||
servletResponse.setContentType("application/json");
|
servletResponse.setContentType("application/json");
|
||||||
throw error;
|
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 {
|
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 {
|
||||||
@@ -131,7 +153,8 @@ public class GatewayController {
|
|||||||
var choices=(List<?>)body.get("choices");
|
var choices=(List<?>)body.get("choices");
|
||||||
var message=((Map<?,?>)choices.getFirst()).get("message");
|
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))));
|
event(response,Map.of("id",body.get("id"),"object","chat.completion.chunk","model",modelId,"choices",List.of(Map.of("index",0,"delta",message))));
|
||||||
billing.settle(ticket,body,elapsed(started),elapsed(started));
|
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("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;
|
event(response,Map.of("choices",List.of(),"usage",body.get("usage"))); done(response); return;
|
||||||
}
|
}
|
||||||
@@ -147,9 +170,9 @@ public class GatewayController {
|
|||||||
// A closed browser must not turn an otherwise completed provider call into free usage.
|
// 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.
|
// 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; }
|
if(!disconnected[0]) try { event(response,chunk); } catch(IOException clientClosed) { disconnected[0]=true; }
|
||||||
}); } finally {billing.observedUsage(ticket,"chat",collector.observedUsage());}
|
}); } finally {requests.observedUsage(ticket,"chat",collector.observedUsage());}
|
||||||
var body=collector.result();
|
var body=collector.result();
|
||||||
billing.settle(ticket,body,elapsed(started),collector.firstTokenMs());
|
requests.settle(ticket,body,elapsed(started),collector.firstTokenMs());
|
||||||
if(!disconnected[0]) done(response);
|
if(!disconnected[0]) done(response);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import java.util.LinkedHashMap;
|
|||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
import java.util.concurrent.atomic.AtomicBoolean;
|
import java.util.concurrent.atomic.AtomicBoolean;
|
||||||
|
import com.moyi.controlplane.gateway.request.ApiRequestAdmission;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
@@ -315,7 +316,7 @@ public class GatewayGuard {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public final class Admission implements AutoCloseable {
|
public final class Admission implements ApiRequestAdmission {
|
||||||
private Slot userSlot;
|
private Slot userSlot;
|
||||||
private final Slot modelSlot;
|
private final Slot modelSlot;
|
||||||
private final Slot keySlot;
|
private final Slot keySlot;
|
||||||
|
|||||||
+42
-33
@@ -4,7 +4,10 @@ import com.fasterxml.jackson.databind.ObjectMapper;
|
|||||||
import com.moyi.controlplane.catalog.CatalogService;
|
import com.moyi.controlplane.catalog.CatalogService;
|
||||||
import com.moyi.controlplane.common.ApiException;
|
import com.moyi.controlplane.common.ApiException;
|
||||||
import com.moyi.controlplane.provider.OpenAiCompatibleProvider;
|
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 jakarta.servlet.http.*;
|
import jakarta.servlet.http.*;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
@@ -15,9 +18,9 @@ import org.springframework.web.bind.annotation.*;
|
|||||||
@RestController
|
@RestController
|
||||||
public class NativeGatewayController {
|
public class NativeGatewayController {
|
||||||
private final CatalogService catalog; private final GatewayGuard guard;
|
private final CatalogService catalog; private final GatewayGuard guard;
|
||||||
private final ChatBillingService billing; private final OpenAiCompatibleProvider provider; private final ObjectMapper json;
|
private final GatewayRequestExecutionService requests; private final OpenAiCompatibleProvider provider; private final ObjectMapper json;
|
||||||
@org.springframework.beans.factory.annotation.Autowired private GatewayFailover failover;
|
@org.springframework.beans.factory.annotation.Autowired private GatewayFailover failover;
|
||||||
public NativeGatewayController(CatalogService catalog,GatewayGuard guard,ChatBillingService billing,OpenAiCompatibleProvider provider,ObjectMapper json) {this.catalog=catalog;this.guard=guard;this.billing=billing;this.provider=provider;this.json=json;}
|
public NativeGatewayController(CatalogService catalog,GatewayGuard guard,GatewayRequestExecutionService requests,OpenAiCompatibleProvider provider,ObjectMapper json) {this.catalog=catalog;this.guard=guard;this.requests=requests;this.provider=provider;this.json=json;}
|
||||||
@PostMapping({"/v1/embeddings","/v1/messages","/v1/responses"})
|
@PostMapping({"/v1/embeddings","/v1/messages","/v1/responses"})
|
||||||
public ResponseEntity<Map<String,Object>> invoke(@RequestBody Map<String,Object> input,HttpServletRequest request,HttpServletResponse response) {
|
public ResponseEntity<Map<String,Object>> invoke(@RequestBody Map<String,Object> input,HttpServletRequest request,HttpServletResponse response) {
|
||||||
String protocol=request.getRequestURI().substring(request.getRequestURI().lastIndexOf('/')+1);
|
String protocol=request.getRequestURI().substring(request.getRequestURI().lastIndexOf('/')+1);
|
||||||
@@ -41,7 +44,11 @@ public class NativeGatewayController {
|
|||||||
return executeForKey(protocol,input,streaming,key,request,response);
|
return executeForKey(protocol,input,streaming,key,request,response);
|
||||||
}
|
}
|
||||||
public ResponseEntity<Map<String,Object>> executeForKey(String protocol,Map<String,Object> input,boolean streaming,Map<String,Object> key,HttpServletRequest request,HttpServletResponse response) {
|
public ResponseEntity<Map<String,Object>> executeForKey(String protocol,Map<String,Object> input,boolean streaming,Map<String,Object> key,HttpServletRequest request,HttpServletResponse response) {
|
||||||
UUID keyId=UUID.fromString(key.get("id").toString());guard.checkRate(keyId,false);
|
return executeForKey(protocol,input,streaming,key,request,response,ApiRequestMetadata.fromHttp(request.getRemoteAddr()));
|
||||||
|
}
|
||||||
|
public ResponseEntity<Map<String,Object>> executeForKey(String protocol,Map<String,Object> input,boolean streaming,Map<String,Object> key,HttpServletRequest request,HttpServletResponse response,ApiRequestMetadata metadata) {
|
||||||
|
UUID keyId=UUID.fromString(key.get("id").toString());
|
||||||
|
guard.checkRate(keyId,false);
|
||||||
String requested=String.valueOf(input.getOrDefault("model",""));
|
String requested=String.valueOf(input.getOrDefault("model",""));
|
||||||
var model=GatewayModelIds.resolve(catalog,key,requested);
|
var model=GatewayModelIds.resolve(catalog,key,requested);
|
||||||
if(model==null) throw new ApiException("MODEL_NOT_FOUND","模型不存在或未授权",404);
|
if(model==null) throw new ApiException("MODEL_NOT_FOUND","模型不存在或未授权",404);
|
||||||
@@ -50,38 +57,40 @@ public class NativeGatewayController {
|
|||||||
if(!provider.isConfigured(providerId,route,upstream)) throw new ApiException("PROVIDER_NOT_CONFIGURED","渠道凭据未配置",503);
|
if(!provider.isConfigured(providerId,route,upstream)) throw new ApiException("PROVIDER_NOT_CONFIGURED","渠道凭据未配置",503);
|
||||||
var normalized=NativeProtocolMeter.request(protocol,input,json);
|
var normalized=NativeProtocolMeter.request(protocol,input,json);
|
||||||
long start=System.currentTimeMillis();
|
long start=System.currentTimeMillis();
|
||||||
try(var admission=guard.acquire(model,keyId,guard.queueWaitMs())) {
|
ApiRequestProtocol requestProtocol = ApiRequestProtocol.from(protocol);
|
||||||
var ticket=billing.reserve(key,model,normalized,request.getHeader("Idempotency-Key"));
|
ApiRequestContext context = new ApiRequestContext(UUID.fromString(key.get("userId").toString()), keyId,
|
||||||
billing.endpoint(ticket.id(),"gemini".equals(protocol)?"/v1beta/models:generateContent":"/v1/"+protocol);
|
requestProtocol, requested, route, streaming, normalized.reservedInputTokens(), normalized.maxOutputTokens(),
|
||||||
|
metadata, System.nanoTime());
|
||||||
|
return requests.execute(context, key, model, normalized, request.getHeader("Idempotency-Key"), () -> {
|
||||||
|
return guard.acquire(model,keyId,guard.queueWaitMs());
|
||||||
|
}, ticket -> {
|
||||||
|
requests.endpoint(ticket,"gemini".equals(protocol)?"/v1beta/models:generateContent":"/v1/"+protocol);
|
||||||
response.setHeader("X-Request-Id",ticket.id().toString());
|
response.setHeader("X-Request-Id",ticket.id().toString());
|
||||||
try {
|
catalog.touchKey(key);
|
||||||
catalog.touchKey(key);
|
if(!streaming) {
|
||||||
if(!streaming) {
|
var result=failover!=null?failover.invoke(key,model,protocol,normalized.body(),ticket):provider.invoke(providerId,upstream,route,normalized.body(),protocol);
|
||||||
var result=failover!=null?failover.invoke(key,model,protocol,normalized.body(),ticket):provider.invoke(providerId,upstream,route,normalized.body(),protocol);
|
if(result.containsKey("model")) result.put("model",requested);
|
||||||
if(result.containsKey("model")) result.put("model",requested);
|
requests.observedUsage(ticket,protocol,result.get("gemini".equals(protocol)?"usageMetadata":"usage"));
|
||||||
billing.observedUsage(ticket,protocol,result.get("gemini".equals(protocol)?"usageMetadata":"usage"));
|
var cost=requests.settle(ticket,NativeProtocolMeter.normalize(protocol,result),elapsed(start),null);
|
||||||
var cost=billing.settle(ticket,NativeProtocolMeter.normalize(protocol,result),elapsed(start),null);
|
return ResponseEntity.ok().header("X-Request-Id",ticket.id().toString()).header("X-Moyi-Cost-CNY",cost.toPlainString()).body(result);
|
||||||
return ResponseEntity.ok().header("X-Request-Id",ticket.id().toString()).header("X-Moyi-Cost-CNY",cost.toPlainString()).body(result);
|
}
|
||||||
}
|
try(var stream=failover!=null?failover.stream(key,model,protocol,normalized.body(),ticket):provider.nativeStream(providerId,upstream,route,normalized.body(),protocol)) {
|
||||||
try(var stream=failover!=null?failover.stream(key,model,protocol,normalized.body(),ticket):provider.nativeStream(providerId,upstream,route,normalized.body(),protocol)) {
|
var collector=new NativeProtocolMeter.Collector(protocol);
|
||||||
var collector=new NativeProtocolMeter.Collector(protocol);
|
response.setContentType("text/event-stream");response.setCharacterEncoding("UTF-8");response.setHeader("Cache-Control","no-cache");response.setHeader("X-Accel-Buffering","no");
|
||||||
response.setContentType("text/event-stream");response.setCharacterEncoding("UTF-8");response.setHeader("Cache-Control","no-cache");response.setHeader("X-Accel-Buffering","no");
|
boolean[] disconnected={false};Integer[] first={null};
|
||||||
boolean[] disconnected={false};Integer[] first={null};
|
try {stream.events(chunk->{
|
||||||
try {stream.events(chunk->{
|
if(first[0]==null) first[0]=elapsed(start);
|
||||||
if(first[0]==null) first[0]=elapsed(start);
|
collector.accept(chunk);
|
||||||
collector.accept(chunk);
|
if(!disconnected[0]) try {event(response,chunk);} catch(IOException e) {disconnected[0]=true;}
|
||||||
if(!disconnected[0]) try {event(response,chunk);} catch(IOException e) {disconnected[0]=true;}
|
},false);} finally {requests.observedUsage(ticket,protocol,collector.observedUsage());}
|
||||||
},false);} finally {billing.observedUsage(ticket,protocol,collector.observedUsage());}
|
requests.settle(ticket,collector.result(),elapsed(start),first[0]);
|
||||||
billing.settle(ticket,collector.result(),elapsed(start),first[0]);
|
}
|
||||||
}
|
return null;
|
||||||
return null;
|
}, failure -> failure instanceof ApiException api ? api
|
||||||
} catch(Exception failure) {
|
: new ApiException("CALL_INTERRUPTED","调用中断,渠道成本待核对",502), error -> {
|
||||||
var error=failure instanceof ApiException api?api:new ApiException("CALL_INTERRUPTED","调用中断,渠道成本待核对",502);
|
|
||||||
billing.fail(ticket,error.getCode(),error.getMessage(),elapsed(start));
|
|
||||||
if(response.isCommitted()) {try {event(response,Map.of("type","error","error",Map.of("type",error.getCode(),"message",error.getMessage())));} catch(IOException ignored) {}return null;}
|
if(response.isCommitted()) {try {event(response,Map.of("type","error","error",Map.of("type",error.getCode(),"message",error.getMessage())));} catch(IOException ignored) {}return null;}
|
||||||
response.resetBuffer();response.setContentType("application/json");throw error;
|
response.resetBuffer();response.setContentType("application/json");throw error;
|
||||||
}
|
});
|
||||||
}
|
|
||||||
}
|
}
|
||||||
private int elapsed(long start) {return (int)Math.min(Integer.MAX_VALUE,System.currentTimeMillis()-start);}
|
private int elapsed(long start) {return (int)Math.min(Integer.MAX_VALUE,System.currentTimeMillis()-start);}
|
||||||
private void event(HttpServletResponse response,Map<String,Object> chunk) throws IOException {
|
private void event(HttpServletResponse response,Map<String,Object> chunk) throws IOException {
|
||||||
|
|||||||
+6
-3
@@ -21,7 +21,8 @@ public class PlaygroundController {
|
|||||||
@org.springframework.beans.factory.annotation.Autowired private NativeGatewayController nativeGateway;
|
@org.springframework.beans.factory.annotation.Autowired private NativeGatewayController nativeGateway;
|
||||||
@PostMapping(value="/embeddings",consumes="application/json")
|
@PostMapping(value="/embeddings",consumes="application/json")
|
||||||
public ResponseEntity<Map<String,Object>> embeddings(@PathVariable String keyId,@RequestBody Map<String,Object> input,HttpServletRequest request,HttpServletResponse response) {
|
public ResponseEntity<Map<String,Object>> embeddings(@PathVariable String keyId,@RequestBody Map<String,Object> input,HttpServletRequest request,HttpServletResponse response) {
|
||||||
return nativeGateway.executeForKey("embeddings",input,false,requireKey(request,keyId),request,response);
|
return nativeGateway.executeForKey("embeddings",input,false,requireKey(request,keyId),request,response,
|
||||||
|
com.moyi.controlplane.gateway.request.ApiRequestMetadata.playground(request.getRemoteAddr()));
|
||||||
}
|
}
|
||||||
|
|
||||||
public PlaygroundController(AuthService auth, CatalogService catalog, GatewayController gateway, AsyncTaskService tasks) {
|
public PlaygroundController(AuthService auth, CatalogService catalog, GatewayController gateway, AsyncTaskService tasks) {
|
||||||
@@ -36,13 +37,15 @@ public class PlaygroundController {
|
|||||||
@RequestBody Map<String, Object> input,
|
@RequestBody Map<String, Object> input,
|
||||||
@RequestHeader(value="Idempotency-Key", required=false) String idempotencyKey,
|
@RequestHeader(value="Idempotency-Key", required=false) String idempotencyKey,
|
||||||
HttpServletRequest request, HttpServletResponse response) {
|
HttpServletRequest request, HttpServletResponse response) {
|
||||||
return gateway.executeChat(requireKey(request, keyId), input, idempotencyKey, response);
|
return gateway.executeChat(requireKey(request, keyId), input, idempotencyKey, response,
|
||||||
|
com.moyi.controlplane.gateway.request.ApiRequestMetadata.playground(request.getRemoteAddr()));
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping(value="/{type:3d|video|image|audio}/tasks", consumes="application/json")
|
@PostMapping(value="/{type:3d|video|image|audio}/tasks", consumes="application/json")
|
||||||
public ResponseEntity<Map<String, Object>> createTask(@PathVariable String keyId, @PathVariable String type,
|
public ResponseEntity<Map<String, Object>> createTask(@PathVariable String keyId, @PathVariable String type,
|
||||||
@RequestBody Map<String, Object> input, HttpServletRequest request) {
|
@RequestBody Map<String, Object> input, HttpServletRequest request) {
|
||||||
return ResponseEntity.accepted().body(Map.of("task", tasks.createForKey(requireKey(request, keyId), type, input)));
|
return ResponseEntity.accepted().body(Map.of("task", tasks.createForKey(requireKey(request, keyId), type, input,
|
||||||
|
com.moyi.controlplane.gateway.request.ApiRequestMetadata.playground(request.getRemoteAddr()))));
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/tasks/{taskId}")
|
@GetMapping("/tasks/{taskId}")
|
||||||
|
|||||||
+7
@@ -0,0 +1,7 @@
|
|||||||
|
package com.moyi.controlplane.gateway.request;
|
||||||
|
|
||||||
|
/** One request's releasable admission lease. */
|
||||||
|
public interface ApiRequestAdmission extends AutoCloseable {
|
||||||
|
@Override
|
||||||
|
void close();
|
||||||
|
}
|
||||||
+42
@@ -0,0 +1,42 @@
|
|||||||
|
package com.moyi.controlplane.gateway.request;
|
||||||
|
|
||||||
|
import java.util.Objects;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
/** Immutable, payload-free facts supplied to admission and risk policies. */
|
||||||
|
public record ApiRequestContext(
|
||||||
|
UUID userId,
|
||||||
|
UUID apiKeyId,
|
||||||
|
ApiRequestProtocol protocol,
|
||||||
|
String requestedModelId,
|
||||||
|
String routeId,
|
||||||
|
boolean streaming,
|
||||||
|
long estimatedInputUnits,
|
||||||
|
int maxOutputUnits,
|
||||||
|
ApiRequestMetadata metadata,
|
||||||
|
long startedAtNanos
|
||||||
|
) {
|
||||||
|
public ApiRequestContext {
|
||||||
|
Objects.requireNonNull(userId, "userId");
|
||||||
|
Objects.requireNonNull(apiKeyId, "apiKeyId");
|
||||||
|
Objects.requireNonNull(protocol, "protocol");
|
||||||
|
requestedModelId = Objects.requireNonNullElse(requestedModelId, "");
|
||||||
|
routeId = Objects.requireNonNullElse(routeId, "");
|
||||||
|
metadata = metadata == null ? ApiRequestMetadata.internal() : metadata;
|
||||||
|
if (estimatedInputUnits < 0 || maxOutputUnits < 0) {
|
||||||
|
throw new IllegalArgumentException("Request estimates cannot be negative");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public int elapsedMillis() {
|
||||||
|
return (int) Math.min(Integer.MAX_VALUE,
|
||||||
|
Math.max(0, java.util.concurrent.TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startedAtNanos)));
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Deliberately omit client metadata so incidental logging cannot expose it. */
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "ApiRequestContext[protocol=" + protocol + ", userId=" + userId + ", apiKeyId=" + apiKeyId
|
||||||
|
+ ", requestedModelId=" + requestedModelId + ", routeId=" + routeId + ", streaming=" + streaming + "]";
|
||||||
|
}
|
||||||
|
}
|
||||||
+34
@@ -0,0 +1,34 @@
|
|||||||
|
package com.moyi.controlplane.gateway.request;
|
||||||
|
|
||||||
|
/** Trusted transport metadata captured by the HTTP adapter; never includes credentials or payloads. */
|
||||||
|
public record ApiRequestMetadata(String clientIp, Source source) {
|
||||||
|
public enum Source {
|
||||||
|
API,
|
||||||
|
PLAYGROUND,
|
||||||
|
INTERNAL
|
||||||
|
}
|
||||||
|
|
||||||
|
public ApiRequestMetadata {
|
||||||
|
source = source == null ? Source.INTERNAL : source;
|
||||||
|
if (clientIp != null && clientIp.length() > 64) clientIp = clientIp.substring(0, 64);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Public HTTP source is determined by the mapped server endpoint, never a caller-controlled header. */
|
||||||
|
public static ApiRequestMetadata fromHttp(String clientIp) {
|
||||||
|
return new ApiRequestMetadata(clientIp, Source.API);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Use only after the session-backed Playground controller has authorized its key. */
|
||||||
|
public static ApiRequestMetadata playground(String clientIp) {
|
||||||
|
return new ApiRequestMetadata(clientIp, Source.PLAYGROUND);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static ApiRequestMetadata internal() {
|
||||||
|
return new ApiRequestMetadata(null, Source.INTERNAL);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "ApiRequestMetadata[source=" + source + "]";
|
||||||
|
}
|
||||||
|
}
|
||||||
+19
@@ -0,0 +1,19 @@
|
|||||||
|
package com.moyi.controlplane.gateway.request;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
||||||
|
@Configuration(proxyBeanMethods = false)
|
||||||
|
public class ApiRequestModuleConfiguration {
|
||||||
|
@Bean
|
||||||
|
@ConditionalOnMissingBean(ApiRequestRiskEngine.class)
|
||||||
|
ApiRequestRiskEngine apiRequestRiskEngine(
|
||||||
|
List<ApiRequestRiskRule> rules,
|
||||||
|
@Value("${moyi.gateway.risk-mode:shadow}") String mode
|
||||||
|
) {
|
||||||
|
return new CompositeApiRequestRiskEngine(rules, CompositeApiRequestRiskEngine.Mode.parse(mode));
|
||||||
|
}
|
||||||
|
}
|
||||||
+24
@@ -0,0 +1,24 @@
|
|||||||
|
package com.moyi.controlplane.gateway.request;
|
||||||
|
|
||||||
|
import com.moyi.controlplane.common.ApiException;
|
||||||
|
|
||||||
|
public enum ApiRequestProtocol {
|
||||||
|
CHAT_COMPLETIONS,
|
||||||
|
MESSAGES,
|
||||||
|
RESPONSES,
|
||||||
|
GEMINI,
|
||||||
|
EMBEDDINGS,
|
||||||
|
ASYNC_TASK;
|
||||||
|
|
||||||
|
public static ApiRequestProtocol from(String value) {
|
||||||
|
return switch (value) {
|
||||||
|
case "chat", "chat.completions" -> CHAT_COMPLETIONS;
|
||||||
|
case "messages" -> MESSAGES;
|
||||||
|
case "responses" -> RESPONSES;
|
||||||
|
case "gemini" -> GEMINI;
|
||||||
|
case "embeddings" -> EMBEDDINGS;
|
||||||
|
case "async", "3d", "image", "audio", "video" -> ASYNC_TASK;
|
||||||
|
default -> throw new ApiException("UNSUPPORTED_PROTOCOL", "请求协议未接入", 400);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
+63
@@ -0,0 +1,63 @@
|
|||||||
|
package com.moyi.controlplane.gateway.request;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
|
public record ApiRequestRiskDecision(
|
||||||
|
Action action,
|
||||||
|
List<String> ruleIds,
|
||||||
|
String code,
|
||||||
|
String publicMessage,
|
||||||
|
int retryAfterSeconds
|
||||||
|
) {
|
||||||
|
public enum Action {
|
||||||
|
ALLOW,
|
||||||
|
SHADOW,
|
||||||
|
DENY,
|
||||||
|
THROTTLE,
|
||||||
|
UNAVAILABLE
|
||||||
|
}
|
||||||
|
|
||||||
|
public ApiRequestRiskDecision {
|
||||||
|
Objects.requireNonNull(action, "action");
|
||||||
|
ruleIds = ruleIds == null ? List.of() : List.copyOf(ruleIds);
|
||||||
|
for (String ruleId : ruleIds) {
|
||||||
|
if (ruleId == null || !ruleId.matches("[a-z][a-z0-9._-]{0,63}")) {
|
||||||
|
throw new IllegalArgumentException("Risk decision contains an invalid rule ID");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (action == Action.DENY || action == Action.THROTTLE) {
|
||||||
|
if (code == null || !code.matches("[A-Z][A-Z0-9_]{1,63}")) {
|
||||||
|
throw new IllegalArgumentException("Blocking risk decision requires a safe error code");
|
||||||
|
}
|
||||||
|
if (publicMessage == null || publicMessage.isBlank() || publicMessage.length() > 240) {
|
||||||
|
throw new IllegalArgumentException("Blocking risk decision requires a short public message");
|
||||||
|
}
|
||||||
|
if (action == Action.THROTTLE ? retryAfterSeconds < 1 : retryAfterSeconds != 0) {
|
||||||
|
throw new IllegalArgumentException("Risk retry delay does not match its action");
|
||||||
|
}
|
||||||
|
} else if (retryAfterSeconds != 0) {
|
||||||
|
throw new IllegalArgumentException("Non-blocking risk decisions cannot include a retry delay");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static ApiRequestRiskDecision allow() {
|
||||||
|
return new ApiRequestRiskDecision(Action.ALLOW, List.of(), null, null, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static ApiRequestRiskDecision shadow(List<String> ruleIds) {
|
||||||
|
return new ApiRequestRiskDecision(Action.SHADOW, ruleIds, null, null, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static ApiRequestRiskDecision deny(ApiRequestRiskSignal signal) {
|
||||||
|
return new ApiRequestRiskDecision(Action.DENY, List.of(signal.ruleId()), signal.code(), signal.publicMessage(), 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static ApiRequestRiskDecision throttle(ApiRequestRiskSignal signal) {
|
||||||
|
return new ApiRequestRiskDecision(Action.THROTTLE, List.of(signal.ruleId()), signal.code(), signal.publicMessage(), signal.retryAfterSeconds());
|
||||||
|
}
|
||||||
|
|
||||||
|
public static ApiRequestRiskDecision unavailable(String ruleId) {
|
||||||
|
return new ApiRequestRiskDecision(Action.UNAVAILABLE, List.of(ruleId), null, null, 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
+10
@@ -0,0 +1,10 @@
|
|||||||
|
package com.moyi.controlplane.gateway.request;
|
||||||
|
|
||||||
|
@FunctionalInterface
|
||||||
|
public interface ApiRequestRiskEngine {
|
||||||
|
ApiRequestRiskDecision evaluate(ApiRequestContext context);
|
||||||
|
|
||||||
|
static ApiRequestRiskEngine allowAll() {
|
||||||
|
return ignored -> ApiRequestRiskDecision.allow();
|
||||||
|
}
|
||||||
|
}
|
||||||
+50
@@ -0,0 +1,50 @@
|
|||||||
|
package com.moyi.controlplane.gateway.request;
|
||||||
|
|
||||||
|
import com.moyi.controlplane.common.ApiException;
|
||||||
|
import com.moyi.controlplane.gateway.GatewayLimitException;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
/** Applies risk decisions before admission or wallet reservation. */
|
||||||
|
@Service
|
||||||
|
public class ApiRequestRiskPolicy {
|
||||||
|
private static final Logger log = LoggerFactory.getLogger(ApiRequestRiskPolicy.class);
|
||||||
|
private final ApiRequestRiskEngine engine;
|
||||||
|
|
||||||
|
public ApiRequestRiskPolicy(ApiRequestRiskEngine engine) {
|
||||||
|
this.engine = java.util.Objects.requireNonNull(engine, "engine");
|
||||||
|
}
|
||||||
|
|
||||||
|
public void enforce(ApiRequestContext context) {
|
||||||
|
final ApiRequestRiskDecision decision;
|
||||||
|
try {
|
||||||
|
decision = engine.evaluate(context);
|
||||||
|
} catch (RuntimeException exception) {
|
||||||
|
log.warn("API request risk engine failed ({})", exception.getClass().getSimpleName());
|
||||||
|
throw unavailable();
|
||||||
|
}
|
||||||
|
if (decision == null) throw unavailable();
|
||||||
|
|
||||||
|
switch (decision.action()) {
|
||||||
|
case ALLOW -> { }
|
||||||
|
case SHADOW -> log.info("API request risk shadow signal: rules={} protocol={} user={} key={} route={}",
|
||||||
|
decision.ruleIds(), context.protocol(), context.userId(), context.apiKeyId(), context.routeId());
|
||||||
|
case DENY -> {
|
||||||
|
log.warn("API request blocked by risk policy: rules={} protocol={} user={} key={} route={}",
|
||||||
|
decision.ruleIds(), context.protocol(), context.userId(), context.apiKeyId(), context.routeId());
|
||||||
|
throw new ApiException(decision.code(), decision.publicMessage(), 403);
|
||||||
|
}
|
||||||
|
case THROTTLE -> {
|
||||||
|
log.warn("API request throttled by risk policy: rules={} protocol={} user={} key={} route={}",
|
||||||
|
decision.ruleIds(), context.protocol(), context.userId(), context.apiKeyId(), context.routeId());
|
||||||
|
throw new GatewayLimitException(decision.code(), decision.publicMessage(), 429, decision.retryAfterSeconds());
|
||||||
|
}
|
||||||
|
case UNAVAILABLE -> throw unavailable();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private ApiException unavailable() {
|
||||||
|
return new ApiException("RISK_ENGINE_UNAVAILABLE", "请求安全检查暂不可用,请稍后重试", 503);
|
||||||
|
}
|
||||||
|
}
|
||||||
+10
@@ -0,0 +1,10 @@
|
|||||||
|
package com.moyi.controlplane.gateway.request;
|
||||||
|
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
/** A deterministic policy over request metadata only. Rules never call providers or mutate billing state. */
|
||||||
|
public interface ApiRequestRiskRule {
|
||||||
|
String id();
|
||||||
|
|
||||||
|
Optional<ApiRequestRiskSignal> evaluate(ApiRequestContext context);
|
||||||
|
}
|
||||||
+30
@@ -0,0 +1,30 @@
|
|||||||
|
package com.moyi.controlplane.gateway.request;
|
||||||
|
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
|
/** A rule's stable, public-safe decision; rule implementations must not put prompts or secrets in the message. */
|
||||||
|
public record ApiRequestRiskSignal(Action action, String ruleId, String code, String publicMessage, int retryAfterSeconds) {
|
||||||
|
public enum Action {
|
||||||
|
DENY,
|
||||||
|
THROTTLE
|
||||||
|
}
|
||||||
|
|
||||||
|
public ApiRequestRiskSignal {
|
||||||
|
Objects.requireNonNull(action, "action");
|
||||||
|
if (ruleId == null || !ruleId.matches("[a-z][a-z0-9._-]{0,63}")) {
|
||||||
|
throw new IllegalArgumentException("Risk rule ID is invalid");
|
||||||
|
}
|
||||||
|
if (code == null || !code.matches("[A-Z][A-Z0-9_]{1,63}")) {
|
||||||
|
throw new IllegalArgumentException("Risk error code is invalid");
|
||||||
|
}
|
||||||
|
if (publicMessage == null || publicMessage.isBlank() || publicMessage.length() > 240) {
|
||||||
|
throw new IllegalArgumentException("Risk public message must contain 1 to 240 characters");
|
||||||
|
}
|
||||||
|
if (action == Action.THROTTLE && retryAfterSeconds < 1) {
|
||||||
|
throw new IllegalArgumentException("A throttle decision must include a positive retry delay");
|
||||||
|
}
|
||||||
|
if (action == Action.DENY && retryAfterSeconds != 0) {
|
||||||
|
throw new IllegalArgumentException("A deny decision cannot include a retry delay");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+69
@@ -0,0 +1,69 @@
|
|||||||
|
package com.moyi.controlplane.gateway.request;
|
||||||
|
|
||||||
|
import java.util.Comparator;
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
/** Composes independently testable rules without giving them access to billing or provider ports. */
|
||||||
|
public final class CompositeApiRequestRiskEngine implements ApiRequestRiskEngine {
|
||||||
|
public enum Mode {
|
||||||
|
SHADOW,
|
||||||
|
ENFORCE;
|
||||||
|
|
||||||
|
public static Mode parse(String value) {
|
||||||
|
if (value == null) return SHADOW;
|
||||||
|
return switch (value.trim().toLowerCase(java.util.Locale.ROOT)) {
|
||||||
|
case "shadow" -> SHADOW;
|
||||||
|
case "enforce" -> ENFORCE;
|
||||||
|
default -> throw new IllegalArgumentException("GATEWAY_RISK_MODE must be 'shadow' or 'enforce'");
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private final List<ApiRequestRiskRule> rules;
|
||||||
|
private final Mode mode;
|
||||||
|
|
||||||
|
public CompositeApiRequestRiskEngine(List<ApiRequestRiskRule> rules, Mode mode) {
|
||||||
|
this.rules = rules == null ? List.of() : rules.stream()
|
||||||
|
.sorted(Comparator.comparing(ApiRequestRiskRule::id))
|
||||||
|
.toList();
|
||||||
|
this.mode = mode == null ? Mode.SHADOW : mode;
|
||||||
|
var ids = new HashSet<String>();
|
||||||
|
for (ApiRequestRiskRule rule : this.rules) {
|
||||||
|
if (rule.id() == null || !rule.id().matches("[a-z][a-z0-9._-]{0,63}") || !ids.add(rule.id())) {
|
||||||
|
throw new IllegalArgumentException("Risk rule IDs must be unique lowercase identifiers");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ApiRequestRiskDecision evaluate(ApiRequestContext context) {
|
||||||
|
var signals = new java.util.ArrayList<ApiRequestRiskSignal>();
|
||||||
|
for (ApiRequestRiskRule rule : rules) {
|
||||||
|
try {
|
||||||
|
Optional<ApiRequestRiskSignal> signal = rule.evaluate(context);
|
||||||
|
if (signal.isPresent()) {
|
||||||
|
if (!rule.id().equals(signal.get().ruleId())) {
|
||||||
|
throw new IllegalStateException("Risk rule returned another rule's ID");
|
||||||
|
}
|
||||||
|
signals.add(signal.get());
|
||||||
|
}
|
||||||
|
} catch (RuntimeException exception) {
|
||||||
|
if (mode == Mode.ENFORCE) return ApiRequestRiskDecision.unavailable(rule.id());
|
||||||
|
signals.add(new ApiRequestRiskSignal(ApiRequestRiskSignal.Action.DENY,
|
||||||
|
rule.id(), "RISK_RULE_UNAVAILABLE", "Risk rule evaluation is unavailable", 0));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (signals.isEmpty()) return ApiRequestRiskDecision.allow();
|
||||||
|
if (mode == Mode.SHADOW) return ApiRequestRiskDecision.shadow(signals.stream().map(ApiRequestRiskSignal::ruleId).distinct().toList());
|
||||||
|
|
||||||
|
return signals.stream()
|
||||||
|
.max(Comparator.comparingInt((ApiRequestRiskSignal signal) -> signal.action() == ApiRequestRiskSignal.Action.DENY ? 2 : 1)
|
||||||
|
.thenComparingInt(ApiRequestRiskSignal::retryAfterSeconds))
|
||||||
|
.map(signal -> signal.action() == ApiRequestRiskSignal.Action.DENY
|
||||||
|
? ApiRequestRiskDecision.deny(signal)
|
||||||
|
: ApiRequestRiskDecision.throttle(signal))
|
||||||
|
.orElseGet(ApiRequestRiskDecision::allow);
|
||||||
|
}
|
||||||
|
}
|
||||||
+95
@@ -0,0 +1,95 @@
|
|||||||
|
package com.moyi.controlplane.gateway.request;
|
||||||
|
|
||||||
|
import com.moyi.controlplane.common.ApiException;
|
||||||
|
import com.moyi.controlplane.gateway.ChatMeter;
|
||||||
|
import com.moyi.controlplane.wallet.ChatBillingService;
|
||||||
|
import java.math.BigDecimal;
|
||||||
|
import java.util.LinkedHashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.function.Function;
|
||||||
|
import java.util.function.Supplier;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
/** Shared application boundary for synchronous and native-protocol model requests. */
|
||||||
|
@Service
|
||||||
|
public class GatewayRequestExecutionService {
|
||||||
|
private final ChatBillingService billing;
|
||||||
|
private final ApiRequestRiskPolicy risk;
|
||||||
|
|
||||||
|
public GatewayRequestExecutionService(ChatBillingService billing, ApiRequestRiskPolicy risk) {
|
||||||
|
this.billing = java.util.Objects.requireNonNull(billing, "billing");
|
||||||
|
this.risk = java.util.Objects.requireNonNull(risk, "risk");
|
||||||
|
}
|
||||||
|
|
||||||
|
public <T> T execute(
|
||||||
|
ApiRequestContext context,
|
||||||
|
Map<String, Object> key,
|
||||||
|
Map<String, Object> model,
|
||||||
|
ChatMeter.Request normalizedRequest,
|
||||||
|
String idempotencyKey,
|
||||||
|
Supplier<? extends ApiRequestAdmission> admission,
|
||||||
|
RequestWork<T> work,
|
||||||
|
Function<Exception, ApiException> classifyFailure,
|
||||||
|
Function<ApiException, T> deliverFailure
|
||||||
|
) {
|
||||||
|
risk.enforce(context);
|
||||||
|
try (ApiRequestAdmission ignored = admission.get()) {
|
||||||
|
ChatBillingService.Ticket ticket = billing.reserve(key, model, normalizedRequest, idempotencyKey, context.metadata());
|
||||||
|
try {
|
||||||
|
return work.execute(ticket);
|
||||||
|
} catch (Exception failure) {
|
||||||
|
if (failure instanceof SettlementPendingException pending) {
|
||||||
|
// The provider completed but persistence failed. Do not classify that as a provider failure.
|
||||||
|
// Recovery can retry automatically if prepareSettlement committed its normalized meter.
|
||||||
|
return deliverFailure.apply(pending);
|
||||||
|
}
|
||||||
|
ApiException error = classifyFailure.apply(failure);
|
||||||
|
billing.fail(ticket, error.getCode(), error.getMessage(), context.elapsedMillis());
|
||||||
|
return deliverFailure.apply(error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public BigDecimal settle(ChatBillingService.Ticket ticket, Map<String, Object> result, int latencyMs, Integer firstTokenMs) {
|
||||||
|
// Validate and reduce to the only data required to price the call. The response body may contain
|
||||||
|
// prompts, generated content, tool arguments or other sensitive data and is never recovery state.
|
||||||
|
ChatMeter.Tokens tokens = ChatMeter.tokens(result);
|
||||||
|
Map<String,Object> usage = new LinkedHashMap<>();
|
||||||
|
usage.put("prompt_tokens", tokens.prompt());
|
||||||
|
usage.put("completion_tokens", tokens.completion());
|
||||||
|
usage.put("prompt_cache_hit_tokens", tokens.cached());
|
||||||
|
long cacheWrite = tokens.cacheWrite5m() + tokens.cacheWrite1h();
|
||||||
|
if (cacheWrite > 0) {
|
||||||
|
usage.put("cache_creation_input_tokens", cacheWrite);
|
||||||
|
usage.put("cache_creation", Map.of(
|
||||||
|
"ephemeral_5m_input_tokens", tokens.cacheWrite5m(),
|
||||||
|
"ephemeral_1h_input_tokens", tokens.cacheWrite1h()));
|
||||||
|
}
|
||||||
|
Map<String,Object> meter = Map.of("usage", usage);
|
||||||
|
try {
|
||||||
|
billing.prepareSettlement(ticket, meter, latencyMs, firstTokenMs);
|
||||||
|
return billing.settle(ticket, result, latencyMs, firstTokenMs);
|
||||||
|
} catch (org.springframework.dao.DataAccessException | org.springframework.transaction.TransactionException failure) {
|
||||||
|
throw new SettlementPendingException();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void endpoint(ChatBillingService.Ticket ticket, String endpoint) {
|
||||||
|
billing.endpoint(ticket.id(), endpoint);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void observedUsage(ChatBillingService.Ticket ticket, String protocol, Object usage) {
|
||||||
|
billing.observedUsage(ticket, protocol, usage);
|
||||||
|
}
|
||||||
|
|
||||||
|
@FunctionalInterface
|
||||||
|
public interface RequestWork<T> {
|
||||||
|
T execute(ChatBillingService.Ticket ticket) throws Exception;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static final class SettlementPendingException extends ApiException {
|
||||||
|
private SettlementPendingException() {
|
||||||
|
super("SETTLEMENT_PENDING", "调用已完成,账务正在核对;请稍后查看使用记录", 503);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+12
-65
@@ -4,6 +4,8 @@ import com.fasterxml.jackson.core.JsonProcessingException;
|
|||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
import com.moyi.controlplane.common.ApiException;
|
import com.moyi.controlplane.common.ApiException;
|
||||||
import com.moyi.controlplane.security.SecretCipher;
|
import com.moyi.controlplane.security.SecretCipher;
|
||||||
|
import com.moyi.controlplane.provider.credentials.UpstreamCredentialResolver;
|
||||||
|
import com.moyi.controlplane.provider.transport.SseEventReader;
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
import java.net.SocketTimeoutException;
|
import java.net.SocketTimeoutException;
|
||||||
import java.net.http.HttpClient;
|
import java.net.http.HttpClient;
|
||||||
@@ -16,7 +18,6 @@ import java.time.Duration;
|
|||||||
import java.util.LinkedHashMap;
|
import java.util.LinkedHashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.concurrent.atomic.AtomicInteger;
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
import java.util.concurrent.*;
|
import java.util.concurrent.*;
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
@@ -242,35 +243,10 @@ public class OpenAiCompatibleProvider {
|
|||||||
events(consumer,true);
|
events(consumer,true);
|
||||||
}
|
}
|
||||||
public void events(EventConsumer consumer,boolean requireDone) throws IOException {
|
public void events(EventConsumer consumer,boolean requireDone) throws IOException {
|
||||||
var reader=new BufferedReader(new InputStreamReader(body,StandardCharsets.UTF_8));
|
new SseEventReader(objectMapper).read(body, expired::get, requireDone, chunk -> {
|
||||||
StringBuilder event=new StringBuilder(), line=new StringBuilder();
|
if (chunk.containsKey("error")) throw classifyUpstreamError(502, writeJson(chunk));
|
||||||
long total=0;
|
consumer.accept(chunk);
|
||||||
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<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);
|
|
||||||
}
|
|
||||||
} 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=()->{};} }
|
@Override public void close() { deadline.cancel(false); try { body.close(); } catch(IOException ignored) {} finally {afterClose.run();afterClose=()->{};} }
|
||||||
}
|
}
|
||||||
@@ -351,25 +327,17 @@ public class OpenAiCompatibleProvider {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private ProviderConfig configured(String providerId, String providerCode, String fallbackKey, String baseUrl, org.springframework.core.env.Environment environment, AtomicInteger cursor) {
|
private ProviderConfig configured(String providerId, String providerCode, String fallbackKey, String baseUrl, org.springframework.core.env.Environment environment, AtomicInteger cursor) {
|
||||||
List<String> keys = new ArrayList<>();
|
List<String> keys = new UpstreamCredentialResolver(environment).providerKeys(fallbackKey, providerId, providerCode);
|
||||||
if (fallbackKey != null && !fallbackKey.isBlank()) keys.add(fallbackKey.trim());
|
return new ProviderConfig(providerId, providerCode, keys, baseUrl, cursor);
|
||||||
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<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(String alias, String providerCode, List<String> apiKeys, String baseUrl, AtomicInteger cursor) {
|
private record ProviderConfig(String alias, String providerCode, List<String> apiKeys, String baseUrl, AtomicInteger cursor) {
|
||||||
|
@Override public String toString() { return "ProviderConfig[alias=" + alias + ", credentialCount=" + apiKeys.size() + "]"; }
|
||||||
private boolean hasKey() { return !apiKeys.isEmpty(); }
|
private boolean hasKey() { return !apiKeys.isEmpty(); }
|
||||||
private boolean hasKey(String routeId, String upstreamModel, org.springframework.core.env.Environment environment) {
|
private boolean hasKey(String routeId, String upstreamModel, org.springframework.core.env.Environment environment) {
|
||||||
return !apiKeys.isEmpty() || !routeKeys(routeId, upstreamModel, environment).isEmpty();
|
return !apiKeys.isEmpty() || !routeKeys(routeId, upstreamModel, environment).isEmpty();
|
||||||
}
|
}
|
||||||
private String nextApiKey() { return apiKeys.get(Math.floorMod(cursor.getAndIncrement(), apiKeys.size())); }
|
private String nextApiKey() { return UpstreamCredentialResolver.nextKey(apiKeys, cursor); }
|
||||||
|
|
||||||
private String nextApiKey(String routeId, String upstreamModel, org.springframework.core.env.Environment environment,
|
private String nextApiKey(String routeId, String upstreamModel, org.springframework.core.env.Environment environment,
|
||||||
ConcurrentMap<String, AtomicInteger> cursors) {
|
ConcurrentMap<String, AtomicInteger> cursors) {
|
||||||
@@ -377,32 +345,11 @@ public class OpenAiCompatibleProvider {
|
|||||||
if (routeKeys.isEmpty()) return nextApiKey();
|
if (routeKeys.isEmpty()) return nextApiKey();
|
||||||
String cursorKey = "route:" + (routeId == null ? "" : routeId);
|
String cursorKey = "route:" + (routeId == null ? "" : routeId);
|
||||||
AtomicInteger routeCursor = cursors.computeIfAbsent(cursorKey, ignored -> new AtomicInteger());
|
AtomicInteger routeCursor = cursors.computeIfAbsent(cursorKey, ignored -> new AtomicInteger());
|
||||||
return routeKeys.get(Math.floorMod(routeCursor.getAndIncrement(), routeKeys.size()));
|
return UpstreamCredentialResolver.nextKey(routeKeys, routeCursor);
|
||||||
}
|
}
|
||||||
|
|
||||||
private List<String> routeKeys(String routeId, String upstreamModel, org.springframework.core.env.Environment environment) {
|
private List<String> routeKeys(String routeId, String upstreamModel, org.springframework.core.env.Environment environment) {
|
||||||
if (environment == null) return List.of();
|
return new UpstreamCredentialResolver(environment).scopedKeys(providerCode, routeId, upstreamModel);
|
||||||
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<String> parseKeys(String value) {
|
|
||||||
List<String> 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]", "_");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
+68
@@ -0,0 +1,68 @@
|
|||||||
|
package com.moyi.controlplane.provider.credentials;
|
||||||
|
|
||||||
|
import com.moyi.controlplane.common.ApiException;
|
||||||
|
import java.util.LinkedHashSet;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Locale;
|
||||||
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
|
import org.springframework.core.env.Environment;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Shared, side-effect-free credential pool resolution for sync and async providers.
|
||||||
|
* Selection state belongs to the caller so resolving a dynamic DB channel never resets rotation.
|
||||||
|
* This component neither logs credentials nor decides retries, health cooldowns or task affinity.
|
||||||
|
*/
|
||||||
|
public final class UpstreamCredentialResolver {
|
||||||
|
private final Environment environment;
|
||||||
|
|
||||||
|
public UpstreamCredentialResolver(Environment environment) {
|
||||||
|
this.environment = environment;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Preserve the supplied alias ordering; the encrypted/single configured key remains first. */
|
||||||
|
public List<String> providerKeys(String fallbackKey, String... aliases) {
|
||||||
|
var keys = new LinkedHashSet<String>();
|
||||||
|
if (fallbackKey != null && !fallbackKey.isBlank()) keys.add(fallbackKey.trim());
|
||||||
|
for (String alias : aliases) {
|
||||||
|
if (alias == null || alias.isBlank()) continue;
|
||||||
|
// Keep the existing provider alias naming convention for deployed environment variables.
|
||||||
|
keys.addAll(environmentKeys("MOYI_UPSTREAM_API_KEYS_" + alias.toUpperCase(Locale.ROOT).replace('-', '_')));
|
||||||
|
}
|
||||||
|
return List.copyOf(keys);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Explicit property names are used only for provider-specific official-key fallbacks. */
|
||||||
|
public List<String> environmentKeys(String propertyName) {
|
||||||
|
return parseKeys(environment == null ? null : environment.getProperty(propertyName));
|
||||||
|
}
|
||||||
|
|
||||||
|
/** A route override wins over a provider+model override. Neither is combined with channel keys. */
|
||||||
|
public List<String> scopedKeys(String providerCode, String routeId, String upstreamModel) {
|
||||||
|
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()) {
|
||||||
|
return environmentKeys("MOYI_UPSTREAM_API_KEYS_MODEL_" + normalize(providerCode) + "_" + normalize(upstreamModel));
|
||||||
|
}
|
||||||
|
return List.of();
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Stable order, no blank entries or duplicates; immutable so callers cannot corrupt a pool. */
|
||||||
|
public static List<String> parseKeys(String value) {
|
||||||
|
if (value == null || value.isBlank()) return List.of();
|
||||||
|
var keys = new LinkedHashSet<String>();
|
||||||
|
for (String key : value.split("\\|")) if (!key.isBlank()) keys.add(key.trim());
|
||||||
|
return List.copyOf(keys);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String nextKey(List<String> keys, AtomicInteger cursor) {
|
||||||
|
if (keys.isEmpty()) throw new ApiException("UPSTREAM_KEY_NOT_AVAILABLE", "渠道没有可用的上游凭据", 503);
|
||||||
|
return keys.get(Math.floorMod(cursor.getAndIncrement(), keys.size()));
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String normalize(String value) {
|
||||||
|
return (value == null ? "" : value).toUpperCase(Locale.ROOT).replaceAll("[^A-Z0-9]", "_");
|
||||||
|
}
|
||||||
|
}
|
||||||
+101
@@ -0,0 +1,101 @@
|
|||||||
|
package com.moyi.controlplane.provider.transport;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.core.type.TypeReference;
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import com.moyi.controlplane.common.ApiException;
|
||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.FilterInputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.io.InputStreamReader;
|
||||||
|
import java.nio.charset.CodingErrorAction;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.function.BooleanSupplier;
|
||||||
|
|
||||||
|
/** Bounded SSE/JSON framing only; no routing, retry, provider credentials, metering or billing. */
|
||||||
|
public final class SseEventReader {
|
||||||
|
private static final int MAX_RESPONSE_BYTES = 4 * 1024 * 1024;
|
||||||
|
private static final int MAX_FRAME_CHARS = 262_144;
|
||||||
|
private final ObjectMapper json;
|
||||||
|
|
||||||
|
public SseEventReader(ObjectMapper json) {
|
||||||
|
this.json = json;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** The transport owns and closes the stream; the supplied deadline covers blocked reads. */
|
||||||
|
public void read(InputStream body, BooleanSupplier expired, boolean requireDone, EventConsumer consumer) throws IOException {
|
||||||
|
var decoder = StandardCharsets.UTF_8.newDecoder()
|
||||||
|
.onMalformedInput(CodingErrorAction.REPORT).onUnmappableCharacter(CodingErrorAction.REPORT);
|
||||||
|
var reader = new BufferedReader(new InputStreamReader(new BoundedBody(body), decoder));
|
||||||
|
var event = new StringBuilder();
|
||||||
|
var line = new StringBuilder();
|
||||||
|
try {
|
||||||
|
int c;
|
||||||
|
while ((c = reader.read()) != -1) {
|
||||||
|
if (expired.getAsBoolean()) throw timeout();
|
||||||
|
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 = json.readValue(data, new TypeReference<>() {});
|
||||||
|
if (chunk == null) throw new ApiException("UPSTREAM_STREAM_INVALID", "上游流式事件格式无效", 502);
|
||||||
|
consumer.accept(chunk);
|
||||||
|
}
|
||||||
|
} else if (text.startsWith("data:")) {
|
||||||
|
if ((long) event.length() + text.length() - 5 + 1 > MAX_FRAME_CHARS) throw tooLarge();
|
||||||
|
event.append(text.substring(5).stripLeading()).append('\n');
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (line.length() >= MAX_FRAME_CHARS) throw tooLarge();
|
||||||
|
line.append((char) c);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (IOException exception) {
|
||||||
|
if (expired.getAsBoolean()) throw timeout();
|
||||||
|
throw exception;
|
||||||
|
}
|
||||||
|
if (expired.getAsBoolean()) throw timeout();
|
||||||
|
if (!requireDone && event.isEmpty() && line.isEmpty()) return;
|
||||||
|
throw new ApiException("UPSTREAM_STREAM_INCOMPLETE", "上游流式响应中断,本次未扣费", 502);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static ApiException tooLarge() {
|
||||||
|
return new ApiException("UPSTREAM_RESPONSE_TOO_LARGE", "上游响应超过限制", 502);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static ApiException timeout() {
|
||||||
|
return new ApiException("UPSTREAM_PROVIDER_TIMEOUT", "上游响应超时,本次未扣费", 504);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static final class BoundedBody extends FilterInputStream {
|
||||||
|
private long bytes;
|
||||||
|
|
||||||
|
private BoundedBody(InputStream body) { super(body); }
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int read() throws IOException {
|
||||||
|
int value = in.read();
|
||||||
|
if (value != -1 && ++bytes > MAX_RESPONSE_BYTES) throw tooLarge();
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int read(byte[] buffer, int offset, int length) throws IOException {
|
||||||
|
if (length == 0) return 0;
|
||||||
|
int count = in.read(buffer, offset, (int) Math.min(length, MAX_RESPONSE_BYTES - bytes + 1));
|
||||||
|
if (count > 0 && (bytes += count) > MAX_RESPONSE_BYTES) throw tooLarge();
|
||||||
|
return count;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@FunctionalInterface
|
||||||
|
public interface EventConsumer {
|
||||||
|
void accept(Map<String,Object> event) throws IOException;
|
||||||
|
}
|
||||||
|
}
|
||||||
+10
-52
@@ -5,6 +5,7 @@ import com.fasterxml.jackson.core.type.TypeReference;
|
|||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
import com.moyi.controlplane.common.ApiException;
|
import com.moyi.controlplane.common.ApiException;
|
||||||
import com.moyi.controlplane.security.SecretCipher;
|
import com.moyi.controlplane.security.SecretCipher;
|
||||||
|
import com.moyi.controlplane.provider.credentials.UpstreamCredentialResolver;
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
import java.net.InetAddress;
|
import java.net.InetAddress;
|
||||||
import java.net.http.HttpClient;
|
import java.net.http.HttpClient;
|
||||||
@@ -450,17 +451,15 @@ public class AsyncProviderClient {
|
|||||||
String ciphertext = row.get("credential_ciphertext") == null ? "" : String.valueOf(row.get("credential_ciphertext"));
|
String ciphertext = row.get("credential_ciphertext") == null ? "" : String.valueOf(row.get("credential_ciphertext"));
|
||||||
String key = ciphertext.isBlank() ? "" : cipher.decrypt(ciphertext);
|
String key = ciphertext.isBlank() ? "" : cipher.decrypt(ciphertext);
|
||||||
String code = String.valueOf(row.get("code"));
|
String code = String.valueOf(row.get("code"));
|
||||||
List<String> keys = new ArrayList<>();
|
var resolver = new UpstreamCredentialResolver(environment);
|
||||||
if (!key.isBlank()) keys.add(key);
|
List<String> keys = new ArrayList<>(resolver.providerKeys(key, code, id));
|
||||||
addEnvironmentKeys(keys, code);
|
|
||||||
addEnvironmentKeys(keys, id);
|
|
||||||
// Tripo's official API key may be supplied through the server secret
|
// Tripo's official API key may be supplied through the server secret
|
||||||
// environment so it never has to be committed or exposed in SQL.
|
// environment so it never has to be committed or exposed in SQL.
|
||||||
if (key.isBlank() && isTripo(code, String.valueOf(row.get("base_url")))) {
|
if (key.isBlank() && isTripo(code, String.valueOf(row.get("base_url")))) {
|
||||||
addEnvironmentKeys(keys, "TRIPO_API_KEY");
|
keys.addAll(resolver.environmentKeys("TRIPO_API_KEY"));
|
||||||
addEnvironmentKeys(keys, "MOYI_TRIPO_API_KEY");
|
keys.addAll(resolver.environmentKeys("MOYI_TRIPO_API_KEY"));
|
||||||
}
|
}
|
||||||
if (keys.isEmpty() && routeKeys(code, id, routeId, upstreamModel).isEmpty()) throw new ApiException("PROVIDER_NOT_CONFIGURED", "异步渠道尚未配置 API Key", 503);
|
if (keys.isEmpty() && resolver.scopedKeys(code, routeId, upstreamModel).isEmpty()) throw new ApiException("PROVIDER_NOT_CONFIGURED", "异步渠道尚未配置 API Key", 503);
|
||||||
return new Provider(code, String.valueOf(row.get("protocol")), String.valueOf(row.get("base_url")), keys.stream().distinct().toList(), keyCursors.computeIfAbsent(id, ignored -> new AtomicInteger()));
|
return new Provider(code, String.valueOf(row.get("protocol")), String.valueOf(row.get("base_url")), keys.stream().distinct().toList(), keyCursors.computeIfAbsent(id, ignored -> new AtomicInteger()));
|
||||||
} catch (EmptyResultDataAccessException exception) { throw new ApiException("PROVIDER_NOT_CONFIGURED", "异步渠道不存在或已停用", 503); }
|
} catch (EmptyResultDataAccessException exception) { throw new ApiException("PROVIDER_NOT_CONFIGURED", "异步渠道不存在或已停用", 503); }
|
||||||
}
|
}
|
||||||
@@ -477,32 +476,6 @@ public class AsyncProviderClient {
|
|||||||
private String trim(String value) { return value == null ? "" : value.replaceAll("/+$", ""); }
|
private String trim(String value) { return value == null ? "" : value.replaceAll("/+$", ""); }
|
||||||
private String encode(String value) { return java.net.URLEncoder.encode(value, java.nio.charset.StandardCharsets.UTF_8); }
|
private String encode(String value) { return java.net.URLEncoder.encode(value, java.nio.charset.StandardCharsets.UTF_8); }
|
||||||
private String write(Object value) throws JsonProcessingException { return mapper.writeValueAsString(value == null ? Map.of() : value); }
|
private String write(Object value) throws JsonProcessingException { return mapper.writeValueAsString(value == null ? Map.of() : value); }
|
||||||
private void addEnvironmentKeys(List<String> keys, String alias) {
|
|
||||||
String name = alias.endsWith("_API_KEY") ? alias : "MOYI_UPSTREAM_API_KEYS_" + alias.toUpperCase(java.util.Locale.ROOT).replace('-', '_');
|
|
||||||
String configured = environment.getProperty(name);
|
|
||||||
if (configured != null && !configured.isBlank()) for (String value : configured.split("\\|")) if (!value.isBlank()) keys.add(value.trim());
|
|
||||||
}
|
|
||||||
|
|
||||||
private List<String> routeKeys(String providerCode, String providerId, String routeId, String upstreamModel) {
|
|
||||||
if (routeId != null && !routeId.isBlank()) {
|
|
||||||
String configured = environment.getProperty("MOYI_UPSTREAM_API_KEYS_ROUTE_" + normalize(routeId));
|
|
||||||
if (configured != null && !configured.isBlank()) return parseKeys(configured);
|
|
||||||
}
|
|
||||||
if (upstreamModel != null && !upstreamModel.isBlank()) {
|
|
||||||
String scopedName = "MOYI_UPSTREAM_API_KEYS_MODEL_" + normalize(providerCode) + "_" + normalize(upstreamModel);
|
|
||||||
String configured = environment.getProperty(scopedName);
|
|
||||||
if (configured != null && !configured.isBlank()) return parseKeys(configured);
|
|
||||||
}
|
|
||||||
return List.of();
|
|
||||||
}
|
|
||||||
|
|
||||||
private List<String> parseKeys(String configured) {
|
|
||||||
List<String> keys = new ArrayList<>();
|
|
||||||
for (String key : configured.split("\\|")) if (!key.isBlank()) keys.add(key.trim());
|
|
||||||
return keys.stream().distinct().toList();
|
|
||||||
}
|
|
||||||
|
|
||||||
private String normalize(String value) { return String.valueOf(value == null ? "" : value).toUpperCase(java.util.Locale.ROOT).replaceAll("[^A-Z0-9]", "_"); }
|
|
||||||
private Map<String,Object> withKeyFingerprint(Map<String,Object> response, String apiKey) {
|
private Map<String,Object> withKeyFingerprint(Map<String,Object> response, String apiKey) {
|
||||||
Map<String,Object> copy = new LinkedHashMap<>();
|
Map<String,Object> copy = new LinkedHashMap<>();
|
||||||
if (response != null) copy.putAll(response);
|
if (response != null) copy.putAll(response);
|
||||||
@@ -528,15 +501,15 @@ public class AsyncProviderClient {
|
|||||||
return normalizedCode.contains("tripo") || normalizedUrl.contains("tripo3d");
|
return normalizedCode.contains("tripo") || normalizedUrl.contains("tripo3d");
|
||||||
}
|
}
|
||||||
private record Provider(String code, String protocol, String baseUrl, List<String> apiKeys, AtomicInteger cursor) {
|
private record Provider(String code, String protocol, String baseUrl, List<String> apiKeys, AtomicInteger cursor) {
|
||||||
|
@Override public String toString() { return "Provider[code=" + code + ", credentialCount=" + apiKeys.size() + "]"; }
|
||||||
private String nextApiKey() {
|
private String nextApiKey() {
|
||||||
if (apiKeys.isEmpty()) throw new ApiException("UPSTREAM_KEY_NOT_AVAILABLE", "异步渠道没有可用的上游凭据", 503);
|
return UpstreamCredentialResolver.nextKey(apiKeys, cursor);
|
||||||
return apiKeys.get(Math.floorMod(cursor.getAndIncrement(), apiKeys.size()));
|
|
||||||
}
|
}
|
||||||
private String nextApiKey(String routeId, String upstreamModel, Environment environment, ConcurrentMap<String, AtomicInteger> cursors) {
|
private String nextApiKey(String routeId, String upstreamModel, Environment environment, ConcurrentMap<String, AtomicInteger> cursors) {
|
||||||
List<String> routeKeys = routeKeys(routeId, upstreamModel, environment);
|
List<String> routeKeys = routeKeys(routeId, upstreamModel, environment);
|
||||||
if (routeKeys.isEmpty()) return nextApiKey();
|
if (routeKeys.isEmpty()) return nextApiKey();
|
||||||
AtomicInteger routeCursor = cursors.computeIfAbsent("route:" + (routeId == null ? upstreamModel : routeId), ignored -> new AtomicInteger());
|
AtomicInteger routeCursor = cursors.computeIfAbsent("route:" + (routeId == null ? upstreamModel : routeId), ignored -> new AtomicInteger());
|
||||||
return routeKeys.get(Math.floorMod(routeCursor.getAndIncrement(), routeKeys.size()));
|
return UpstreamCredentialResolver.nextKey(routeKeys, routeCursor);
|
||||||
}
|
}
|
||||||
private String keyForFingerprint(String routeId, String upstreamModel, Environment environment,
|
private String keyForFingerprint(String routeId, String upstreamModel, Environment environment,
|
||||||
ConcurrentMap<String, AtomicInteger> cursors, String fingerprint) {
|
ConcurrentMap<String, AtomicInteger> cursors, String fingerprint) {
|
||||||
@@ -548,22 +521,7 @@ public class AsyncProviderClient {
|
|||||||
throw new ApiException("UPSTREAM_KEY_NOT_AVAILABLE", "创建任务时使用的上游凭据已从服务器配置中移除,无法安全轮询", 503);
|
throw new ApiException("UPSTREAM_KEY_NOT_AVAILABLE", "创建任务时使用的上游凭据已从服务器配置中移除,无法安全轮询", 503);
|
||||||
}
|
}
|
||||||
private List<String> routeKeys(String routeId, String upstreamModel, Environment environment) {
|
private List<String> routeKeys(String routeId, String upstreamModel, Environment environment) {
|
||||||
if (environment == null) return List.of();
|
return new UpstreamCredentialResolver(environment).scopedKeys(code, routeId, upstreamModel);
|
||||||
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 value = environment.getProperty("MOYI_UPSTREAM_API_KEYS_MODEL_" + normalize(code()) + "_" + normalize(upstreamModel));
|
|
||||||
if (value != null && !value.isBlank()) return parseKeys(value);
|
|
||||||
}
|
|
||||||
return List.of();
|
|
||||||
}
|
}
|
||||||
private static List<String> parseKeys(String value) {
|
|
||||||
List<String> 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]", "_"); }
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -7,6 +7,11 @@ import com.moyi.controlplane.catalog.CatalogService;
|
|||||||
import com.moyi.controlplane.common.ApiException;
|
import com.moyi.controlplane.common.ApiException;
|
||||||
import com.moyi.controlplane.gateway.GatewayGuard;
|
import com.moyi.controlplane.gateway.GatewayGuard;
|
||||||
import com.moyi.controlplane.gateway.GatewayModelIds;
|
import com.moyi.controlplane.gateway.GatewayModelIds;
|
||||||
|
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.ApiRequestRiskEngine;
|
||||||
|
import com.moyi.controlplane.gateway.request.ApiRequestRiskPolicy;
|
||||||
import com.moyi.controlplane.usage.UsageAuditService;
|
import com.moyi.controlplane.usage.UsageAuditService;
|
||||||
import java.sql.ResultSet;
|
import java.sql.ResultSet;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
@@ -43,11 +48,13 @@ public class AsyncTaskService {
|
|||||||
private final AsyncProviderClient providerClient;
|
private final AsyncProviderClient providerClient;
|
||||||
private final TransactionTemplate transactions;
|
private final TransactionTemplate transactions;
|
||||||
private final com.moyi.controlplane.gateway.EnterprisePolicies policies;
|
private final com.moyi.controlplane.gateway.EnterprisePolicies policies;
|
||||||
|
private final ApiRequestRiskPolicy requestRisk;
|
||||||
|
|
||||||
/** Compatibility constructor for isolated gateway tests that only exercise mock tasks. */
|
/** Compatibility constructor for isolated gateway tests that only exercise mock tasks. */
|
||||||
public AsyncTaskService(JdbcTemplate jdbc, ObjectMapper objectMapper, CatalogService catalogService,
|
public AsyncTaskService(JdbcTemplate jdbc, ObjectMapper objectMapper, CatalogService catalogService,
|
||||||
int maxQueuedPerModel, GatewayGuard gatewayGuard, UsageAuditService usage) {
|
int maxQueuedPerModel, GatewayGuard gatewayGuard, UsageAuditService usage) {
|
||||||
this(jdbc, objectMapper, catalogService, maxQueuedPerModel, 4, 4, 3_600_000, true, gatewayGuard, usage, null, null);
|
this(jdbc, objectMapper, catalogService, maxQueuedPerModel, 4, 4, 3_600_000, true,
|
||||||
|
gatewayGuard, usage, null, null, new ApiRequestRiskPolicy(ApiRequestRiskEngine.allowAll()));
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Compatibility constructor for isolated tests using explicit limits. */
|
/** Compatibility constructor for isolated tests using explicit limits. */
|
||||||
@@ -58,7 +65,8 @@ public class AsyncTaskService {
|
|||||||
AsyncProviderClient providerClient, PlatformTransactionManager transactionManager
|
AsyncProviderClient providerClient, PlatformTransactionManager transactionManager
|
||||||
) {
|
) {
|
||||||
this(jdbc, objectMapper, catalogService, maxQueuedPerModel, defaultModelConcurrency,
|
this(jdbc, objectMapper, catalogService, maxQueuedPerModel, defaultModelConcurrency,
|
||||||
defaultKeyConcurrency, providerTimeoutMs, true, gatewayGuard, usage, providerClient, transactionManager);
|
defaultKeyConcurrency, providerTimeoutMs, true, gatewayGuard, usage, providerClient,
|
||||||
|
transactionManager, new ApiRequestRiskPolicy(ApiRequestRiskEngine.allowAll()));
|
||||||
}
|
}
|
||||||
|
|
||||||
@org.springframework.beans.factory.annotation.Autowired
|
@org.springframework.beans.factory.annotation.Autowired
|
||||||
@@ -74,7 +82,8 @@ public class AsyncTaskService {
|
|||||||
GatewayGuard gatewayGuard,
|
GatewayGuard gatewayGuard,
|
||||||
UsageAuditService usage,
|
UsageAuditService usage,
|
||||||
AsyncProviderClient providerClient,
|
AsyncProviderClient providerClient,
|
||||||
PlatformTransactionManager transactionManager
|
PlatformTransactionManager transactionManager,
|
||||||
|
ApiRequestRiskPolicy requestRisk
|
||||||
) {
|
) {
|
||||||
// A model concurrency of 0 means no MOYI-side model-group cap. The
|
// A model concurrency of 0 means no MOYI-side model-group cap. The
|
||||||
// upstream provider still enforces its own quota, while API-Key
|
// upstream provider still enforces its own quota, while API-Key
|
||||||
@@ -94,16 +103,28 @@ public class AsyncTaskService {
|
|||||||
this.providerClient = providerClient;
|
this.providerClient = providerClient;
|
||||||
this.transactions = transactionManager == null ? null : new TransactionTemplate(transactionManager);
|
this.transactions = transactionManager == null ? null : new TransactionTemplate(transactionManager);
|
||||||
this.policies = new com.moyi.controlplane.gateway.EnterprisePolicies(jdbc,objectMapper);
|
this.policies = new com.moyi.controlplane.gateway.EnterprisePolicies(jdbc,objectMapper);
|
||||||
|
this.requestRisk = java.util.Objects.requireNonNull(requestRisk, "requestRisk");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
public Map<String, Object> create(String authorization, String taskType, Map<String, Object> input) {
|
public Map<String, Object> create(String authorization, String taskType, Map<String, Object> input) {
|
||||||
return createForKey(requireKey(authorization), taskType, input);
|
return create(authorization, taskType, input, ApiRequestMetadata.internal());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public Map<String, Object> create(String authorization, String taskType, Map<String, Object> input, ApiRequestMetadata metadata) {
|
||||||
|
return createForKey(requireKey(authorization), taskType, input, metadata);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
public Map<String, Object> createForKey(Map<String, Object> key, String taskType, Map<String, Object> input) {
|
public Map<String, Object> createForKey(Map<String, Object> key, String taskType, Map<String, Object> input) {
|
||||||
gatewayGuard.checkRate(UUID.fromString(String.valueOf(key.get("id"))), false);
|
return createForKey(key, taskType, input, ApiRequestMetadata.internal());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public Map<String, Object> createForKey(Map<String, Object> key, String taskType, Map<String, Object> input, ApiRequestMetadata metadata) {
|
||||||
|
UUID apiKeyId = UUID.fromString(String.valueOf(key.get("id")));
|
||||||
|
gatewayGuard.checkRate(apiKeyId, false);
|
||||||
String requestedModelId = String.valueOf(input.getOrDefault("model", ""));
|
String requestedModelId = String.valueOf(input.getOrDefault("model", ""));
|
||||||
Map<String, Object> model = GatewayModelIds.resolve(catalogService, key, requestedModelId);
|
Map<String, Object> model = GatewayModelIds.resolve(catalogService, key, requestedModelId);
|
||||||
if (model == null) throw new ApiException("MODEL_NOT_FOUND", "模型不存在或已下线", 404);
|
if (model == null) throw new ApiException("MODEL_NOT_FOUND", "模型不存在或已下线", 404);
|
||||||
@@ -111,19 +132,20 @@ public class AsyncTaskService {
|
|||||||
if (!taskType.equals(model.get("type")) || !"async".equals(model.get("mode"))) throw new ApiException("NOT_ASYNC_MEDIA_MODEL", "该模型不是对应的异步媒体模型", 400);
|
if (!taskType.equals(model.get("type")) || !"async".equals(model.get("mode"))) throw new ApiException("NOT_ASYNC_MEDIA_MODEL", "该模型不是对应的异步媒体模型", 400);
|
||||||
if (Boolean.FALSE.equals(model.get("executionReady"))) throw new ApiException("ASYNC_PROVIDER_NOT_IMPLEMENTED", "该模型的专用执行接口尚未接入", 503);
|
if (Boolean.FALSE.equals(model.get("executionReady"))) throw new ApiException("ASYNC_PROVIDER_NOT_IMPLEMENTED", "该模型的专用执行接口尚未接入", 503);
|
||||||
if (!"provider-mock".equals(model.get("providerId")) && providerClient == null) throw new ApiException("ASYNC_PROVIDER_NOT_IMPLEMENTED", "异步执行器未配置", 501);
|
if (!"provider-mock".equals(model.get("providerId")) && providerClient == null) throw new ApiException("ASYNC_PROVIDER_NOT_IMPLEMENTED", "异步执行器未配置", 501);
|
||||||
// Serialize the queue-capacity check with task insertion for this
|
|
||||||
// model. Without the transaction advisory lock concurrent creates
|
|
||||||
// could all observe the same count and exceed maxQueuedPerModel.
|
|
||||||
jdbc.queryForList("SELECT pg_advisory_xact_lock(hashtextextended('moyi:async-queue:' || ?, 0))", modelId);
|
|
||||||
Integer queued = jdbc.queryForObject("SELECT COUNT(*) FROM model_tasks WHERE model_id = ? AND status IN ('queued', 'running')", Integer.class, modelId);
|
|
||||||
if (queued != null && queued >= maxQueuedPerModel) throw new ApiException("TASK_QUEUE_FULL", "该模型当前排队已满,请稍后重试", 429);
|
|
||||||
String prompt = String.valueOf(input.getOrDefault("prompt", "")).trim();
|
String prompt = String.valueOf(input.getOrDefault("prompt", "")).trim();
|
||||||
Object mediaInput = input.get("media_url");
|
Object mediaInput = input.get("media_url");
|
||||||
if (prompt.isBlank() && (mediaInput == null || String.valueOf(mediaInput).isBlank())) {
|
if (prompt.isBlank() && (mediaInput == null || String.valueOf(mediaInput).isBlank())) {
|
||||||
throw new ApiException("PROMPT_REQUIRED", "生成提示词或媒体输入不能为空");
|
throw new ApiException("PROMPT_REQUIRED", "生成提示词或媒体输入不能为空");
|
||||||
}
|
}
|
||||||
UUID apiKeyId = UUID.fromString(String.valueOf(key.get("id")));
|
|
||||||
UUID userId = catalogService.keyOwnerId(apiKeyId);
|
UUID userId = catalogService.keyOwnerId(apiKeyId);
|
||||||
|
requestRisk.enforce(new ApiRequestContext(userId, apiKeyId, ApiRequestProtocol.ASYNC_TASK,
|
||||||
|
requestedModelId, modelId, false, estimateInputUnits(input), 0, metadata, System.nanoTime()));
|
||||||
|
// Serialize the queue-capacity check with task insertion for this
|
||||||
|
// model. Without the transaction advisory lock, concurrent creates
|
||||||
|
// could all observe the same count and exceed maxQueuedPerModel.
|
||||||
|
jdbc.queryForList("SELECT pg_advisory_xact_lock(hashtextextended('moyi:async-queue:' || ?, 0))", modelId);
|
||||||
|
Integer queued = jdbc.queryForObject("SELECT COUNT(*) FROM model_tasks WHERE model_id = ? AND status IN ('queued', 'running')", Integer.class, modelId);
|
||||||
|
if (queued != null && queued >= maxQueuedPerModel) throw new ApiException("TASK_QUEUE_FULL", "该模型当前排队已满,请稍后重试", 429);
|
||||||
@SuppressWarnings("unchecked") Map<String,Object> frozenPricing=(Map<String,Object>) model.get("pricing");
|
@SuppressWarnings("unchecked") Map<String,Object> frozenPricing=(Map<String,Object>) model.get("pricing");
|
||||||
if(AsyncPriceSchedule.tokens(frozenPricing) && !"image".equals(taskType)) throw new ApiException("ASYNC_BILLING_UNSUPPORTED","该媒体类型的 Token 用量结算尚未接入",503);
|
if(AsyncPriceSchedule.tokens(frozenPricing) && !"image".equals(taskType)) throw new ApiException("ASYNC_BILLING_UNSUPPORTED","该媒体类型的 Token 用量结算尚未接入",503);
|
||||||
BigDecimal reservedAmount = "provider-mock".equals(model.get("providerId")) ? BigDecimal.ZERO : AsyncPriceSchedule.reserve(frozenPricing,input);
|
BigDecimal reservedAmount = "provider-mock".equals(model.get("providerId")) ? BigDecimal.ZERO : AsyncPriceSchedule.reserve(frozenPricing,input);
|
||||||
@@ -146,7 +168,7 @@ public class AsyncTaskService {
|
|||||||
releaseWallet(userId, reservedAmount);
|
releaseWallet(userId, reservedAmount);
|
||||||
throw exception;
|
throw exception;
|
||||||
}
|
}
|
||||||
usage.recordTask(userId, apiKeyId, modelId, String.valueOf(model.get("providerId")), id, "queued", input, null, null, null);
|
usage.recordTask(userId, apiKeyId, modelId, String.valueOf(model.get("providerId")), id, "queued", input, null, null, null, metadata);
|
||||||
if (!"provider-mock".equals(model.get("providerId"))) jdbc.update("UPDATE model_tasks SET pricing_snapshot=CAST(? AS jsonb),price_version=(SELECT max(id) FROM model_price_history WHERE model_id=?) WHERE id=?",writeJson(frozenPricing),modelId,id);
|
if (!"provider-mock".equals(model.get("providerId"))) jdbc.update("UPDATE model_tasks SET pricing_snapshot=CAST(? AS jsonb),price_version=(SELECT max(id) FROM model_price_history WHERE model_id=?) WHERE id=?",writeJson(frozenPricing),modelId,id);
|
||||||
catalogService.touchKey(key);
|
catalogService.touchKey(key);
|
||||||
return taskView(id);
|
return taskView(id);
|
||||||
@@ -513,6 +535,11 @@ public class AsyncTaskService {
|
|||||||
|
|
||||||
private String writeJson(Object value) { try { return objectMapper.writeValueAsString(value); } catch (JsonProcessingException exception) { return "{}"; } }
|
private String writeJson(Object value) { try { return objectMapper.writeValueAsString(value); } catch (JsonProcessingException exception) { return "{}"; } }
|
||||||
|
|
||||||
|
private long estimateInputUnits(Map<String,Object> input) {
|
||||||
|
try { return objectMapper.writeValueAsBytes(input == null ? Map.of() : input).length; }
|
||||||
|
catch (JsonProcessingException exception) { throw new ApiException("INVALID_REQUEST", "任务请求无法编码"); }
|
||||||
|
}
|
||||||
|
|
||||||
private Map<String,Object> usageOnly(Map<String,Object> result) {
|
private Map<String,Object> usageOnly(Map<String,Object> result) {
|
||||||
var selected=new LinkedHashMap<String,Object>();
|
var selected=new LinkedHashMap<String,Object>();
|
||||||
if(result!=null) for(String field:List.of("usage","usageMetadata")) if(result.get(field) instanceof Map<?,?> value) selected.put(field,value);
|
if(result!=null) for(String field:List.of("usage","usageMetadata")) if(result.get(field) instanceof Map<?,?> value) selected.put(field,value);
|
||||||
|
|||||||
@@ -11,6 +11,8 @@ import org.springframework.web.bind.annotation.RequestBody;
|
|||||||
import org.springframework.web.bind.annotation.RequestHeader;
|
import org.springframework.web.bind.annotation.RequestHeader;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
|
import com.moyi.controlplane.gateway.request.ApiRequestMetadata;
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
@RequestMapping("/v1")
|
@RequestMapping("/v1")
|
||||||
@@ -25,19 +27,23 @@ public class TaskController {
|
|||||||
public ResponseEntity<Map<String, Object>> create3d(
|
public ResponseEntity<Map<String, Object>> create3d(
|
||||||
@org.springframework.web.bind.annotation.PathVariable(required = false) String type,
|
@org.springframework.web.bind.annotation.PathVariable(required = false) String type,
|
||||||
@RequestHeader(value = "Authorization", required = false) String authorization,
|
@RequestHeader(value = "Authorization", required = false) String authorization,
|
||||||
@RequestBody Map<String, Object> input
|
@RequestBody Map<String, Object> input,
|
||||||
|
HttpServletRequest request
|
||||||
) {
|
) {
|
||||||
String taskType = type == null ? String.valueOf(input == null ? "3d" : input.getOrDefault("taskType", "3d")) : type;
|
String taskType = type == null ? String.valueOf(input == null ? "3d" : input.getOrDefault("taskType", "3d")) : type;
|
||||||
return ResponseEntity.status(HttpStatus.ACCEPTED).body(Map.of("task", tasks.create(authorization, taskType, input)));
|
return ResponseEntity.status(HttpStatus.ACCEPTED).body(Map.of("task", tasks.create(authorization, taskType, input,
|
||||||
|
ApiRequestMetadata.fromHttp(request.getRemoteAddr()))));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@PostMapping("/video/tasks")
|
@PostMapping("/video/tasks")
|
||||||
public ResponseEntity<Map<String, Object>> createVideo(
|
public ResponseEntity<Map<String, Object>> createVideo(
|
||||||
@RequestHeader(value = "Authorization", required = false) String authorization,
|
@RequestHeader(value = "Authorization", required = false) String authorization,
|
||||||
@RequestBody Map<String, Object> input
|
@RequestBody Map<String, Object> input,
|
||||||
|
HttpServletRequest request
|
||||||
) {
|
) {
|
||||||
return ResponseEntity.status(HttpStatus.ACCEPTED).body(Map.of("task", tasks.create(authorization, "video", input)));
|
return ResponseEntity.status(HttpStatus.ACCEPTED).body(Map.of("task", tasks.create(authorization, "video", input,
|
||||||
|
ApiRequestMetadata.fromHttp(request.getRemoteAddr()))));
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/tasks/{id}")
|
@GetMapping("/tasks/{id}")
|
||||||
|
|||||||
+40
-7
@@ -8,6 +8,7 @@ import com.fasterxml.jackson.databind.node.ObjectNode;
|
|||||||
import com.moyi.controlplane.auth.AuthService;
|
import com.moyi.controlplane.auth.AuthService;
|
||||||
import com.moyi.controlplane.common.ApiException;
|
import com.moyi.controlplane.common.ApiException;
|
||||||
import com.moyi.controlplane.gateway.GatewayGuard;
|
import com.moyi.controlplane.gateway.GatewayGuard;
|
||||||
|
import com.moyi.controlplane.gateway.request.ApiRequestMetadata;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.sql.ResultSet;
|
import java.sql.ResultSet;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
@@ -50,14 +51,22 @@ public class UsageAuditService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void startChat(UUID id, UUID userId, UUID keyId, Map<String,Object> model, Map<String,Object> input) {
|
public void startChat(UUID id, UUID userId, UUID keyId, Map<String,Object> model, Map<String,Object> input) {
|
||||||
String agent=null, source="api";
|
startChat(id,userId,keyId,model,input,ApiRequestMetadata.internal());
|
||||||
|
}
|
||||||
|
|
||||||
|
public void startChat(UUID id, UUID userId, UUID keyId, Map<String,Object> model, Map<String,Object> input,
|
||||||
|
ApiRequestMetadata metadata) {
|
||||||
|
String agent=null, source=source(metadata);
|
||||||
|
String ip=metadata == null ? null : metadata.clientIp();
|
||||||
var attributes=org.springframework.web.context.request.RequestContextHolder.getRequestAttributes();
|
var attributes=org.springframework.web.context.request.RequestContextHolder.getRequestAttributes();
|
||||||
if(attributes instanceof org.springframework.web.context.request.ServletRequestAttributes servlet) {
|
if(attributes instanceof org.springframework.web.context.request.ServletRequestAttributes servlet) {
|
||||||
agent=servlet.getRequest().getHeader("User-Agent");
|
var request=servlet.getRequest();
|
||||||
if("playground".equals(servlet.getRequest().getHeader("X-Moyi-Source"))) source="playground";
|
agent=request.getHeader("User-Agent");
|
||||||
|
if(ip==null) ip=request.getRemoteAddr();
|
||||||
|
if(metadata==null || metadata.source()==ApiRequestMetadata.Source.INTERNAL) source=isPlaygroundRequest(request)?"playground":"api";
|
||||||
}
|
}
|
||||||
jdbc.update("INSERT INTO usage_events(id,request_id,user_id,api_key_id,model_id,provider_id,event_kind,status,input_payload,endpoint,call_mode,request_source,user_agent,reasoning_effort) VALUES (?,?,?,?,?,?,'call','running',?::jsonb,'/v1/chat/completions',?,?,?,?)",
|
jdbc.update("INSERT INTO usage_events(id,request_id,user_id,api_key_id,model_id,provider_id,event_kind,status,input_payload,endpoint,call_mode,request_source,source_ip,user_agent,reasoning_effort) VALUES (?,?,?,?,?,?,'call','running',?::jsonb,'/v1/chat/completions',?,?,CAST(? AS inet),?,?)",
|
||||||
id,id.toString(),userId,keyId,model.get("id"),model.get("providerId"),json(input),Boolean.TRUE.equals(input.get("stream"))?"stream":"sync",source,
|
id,id.toString(),userId,keyId,model.get("id"),model.get("providerId"),json(input),Boolean.TRUE.equals(input.get("stream"))?"stream":"sync",source,blankToNull(ip),
|
||||||
agent==null?null:agent.substring(0,Math.min(1000,agent.length())),input.get("reasoning_effort")==null?null:String.valueOf(input.get("reasoning_effort")).substring(0,Math.min(32,String.valueOf(input.get("reasoning_effort")).length())));
|
agent==null?null:agent.substring(0,Math.min(1000,agent.length())),input.get("reasoning_effort")==null?null:String.valueOf(input.get("reasoning_effort")).substring(0,Math.min(32,String.valueOf(input.get("reasoning_effort")).length())));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -83,9 +92,18 @@ public class UsageAuditService {
|
|||||||
public void recordTask(
|
public void recordTask(
|
||||||
UUID userId, UUID apiKeyId, String modelId, String providerId, String taskId,
|
UUID userId, UUID apiKeyId, String modelId, String providerId, String taskId,
|
||||||
String status, Object input, Object output, String errorCode, String errorMessage
|
String status, Object input, Object output, String errorCode, String errorMessage
|
||||||
|
) {
|
||||||
|
recordTask(userId,apiKeyId,modelId,providerId,taskId,status,input,output,errorCode,errorMessage,ApiRequestMetadata.internal());
|
||||||
|
}
|
||||||
|
|
||||||
|
public void recordTask(
|
||||||
|
UUID userId, UUID apiKeyId, String modelId, String providerId, String taskId,
|
||||||
|
String status, Object input, Object output, String errorCode, String errorMessage,
|
||||||
|
ApiRequestMetadata metadata
|
||||||
) {
|
) {
|
||||||
record(userId, apiKeyId, modelId, providerId, "task", status, null, 0, 0,
|
record(userId, apiKeyId, modelId, providerId, "task", status, null, 0, 0,
|
||||||
input, output, taskId, errorCode, errorMessage, null, null);
|
input, output, taskId, errorCode, errorMessage, metadata == null ? null : metadata.clientIp(), null,
|
||||||
|
metadata == null ? null : source(metadata));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void updateTask(String taskId, String status, Object output, String errorCode, String errorMessage) {
|
public void updateTask(String taskId, String status, Object output, String errorCode, String errorMessage) {
|
||||||
@@ -345,14 +363,20 @@ public class UsageAuditService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void record(UUID userId, UUID keyId, String modelId, String providerId, String kind, String status, Integer latency, long inputUnits, long outputUnits, Object input, Object output, String taskId, String errorCode, String errorMessage, String ip, String userAgent) {
|
private void record(UUID userId, UUID keyId, String modelId, String providerId, String kind, String status, Integer latency, long inputUnits, long outputUnits, Object input, Object output, String taskId, String errorCode, String errorMessage, String ip, String userAgent) {
|
||||||
|
record(userId,keyId,modelId,providerId,kind,status,latency,inputUnits,outputUnits,input,output,taskId,errorCode,errorMessage,ip,userAgent,null);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void record(UUID userId, UUID keyId, String modelId, String providerId, String kind, String status, Integer latency, long inputUnits, long outputUnits, Object input, Object output, String taskId, String errorCode, String errorMessage, String ip, String userAgent, String explicitSource) {
|
||||||
String endpoint=null, source=null;
|
String endpoint=null, source=null;
|
||||||
var attributes=org.springframework.web.context.request.RequestContextHolder.getRequestAttributes();
|
var attributes=org.springframework.web.context.request.RequestContextHolder.getRequestAttributes();
|
||||||
if(attributes instanceof org.springframework.web.context.request.ServletRequestAttributes servlet) {
|
if(attributes instanceof org.springframework.web.context.request.ServletRequestAttributes servlet) {
|
||||||
var request=servlet.getRequest();
|
var request=servlet.getRequest();
|
||||||
endpoint=request.getRequestURI();
|
endpoint=request.getRequestURI();
|
||||||
userAgent=request.getHeader("User-Agent");
|
userAgent=request.getHeader("User-Agent");
|
||||||
source="playground".equals(request.getHeader("X-Moyi-Source")) ? "playground" : "api";
|
if(ip==null) ip=request.getRemoteAddr();
|
||||||
|
source=explicitSource == null ? (isPlaygroundRequest(request) ? "playground" : "api") : explicitSource;
|
||||||
}
|
}
|
||||||
|
if(source==null) source=explicitSource;
|
||||||
JsonNode payload=objectMapper.valueToTree(output);
|
JsonNode payload=objectMapper.valueToTree(output);
|
||||||
Long cached=usageNumber(payload,"/usage/prompt_tokens_details/cached_tokens");
|
Long cached=usageNumber(payload,"/usage/prompt_tokens_details/cached_tokens");
|
||||||
if(cached==null) cached=usageNumber(payload,"/usage/prompt_cache_hit_tokens");
|
if(cached==null) cached=usageNumber(payload,"/usage/prompt_cache_hit_tokens");
|
||||||
@@ -370,6 +394,15 @@ public class UsageAuditService {
|
|||||||
return value.isIntegralNumber() && value.canConvertToLong() && value.longValue()>=0 ? value.longValue() : null;
|
return value.isIntegralNumber() && value.canConvertToLong() && value.longValue()>=0 ? value.longValue() : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Source attribution is based on the matched server route, not a spoofable client header. */
|
||||||
|
private boolean isPlaygroundRequest(jakarta.servlet.http.HttpServletRequest request) {
|
||||||
|
return request.getRequestURI() != null && request.getRequestURI().startsWith("/api/playground/keys/");
|
||||||
|
}
|
||||||
|
|
||||||
|
private String source(ApiRequestMetadata metadata) {
|
||||||
|
return metadata != null && metadata.source()==ApiRequestMetadata.Source.PLAYGROUND ? "playground" : "api";
|
||||||
|
}
|
||||||
|
|
||||||
private String json(Object value) {
|
private String json(Object value) {
|
||||||
if (value == null) return null;
|
if (value == null) return null;
|
||||||
try {
|
try {
|
||||||
|
|||||||
+58
-8
@@ -3,6 +3,7 @@ package com.moyi.controlplane.wallet;
|
|||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
import com.moyi.controlplane.common.ApiException;
|
import com.moyi.controlplane.common.ApiException;
|
||||||
import com.moyi.controlplane.gateway.ChatMeter;
|
import com.moyi.controlplane.gateway.ChatMeter;
|
||||||
|
import com.moyi.controlplane.gateway.request.ApiRequestMetadata;
|
||||||
import com.moyi.controlplane.usage.UsageAuditService;
|
import com.moyi.controlplane.usage.UsageAuditService;
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
@@ -12,19 +13,40 @@ import org.springframework.jdbc.core.JdbcTemplate;
|
|||||||
import org.springframework.scheduling.annotation.Scheduled;
|
import org.springframework.scheduling.annotation.Scheduled;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
import org.springframework.transaction.support.TransactionTemplate;
|
||||||
|
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class ChatBillingService {
|
public class ChatBillingService {
|
||||||
|
private static final Logger log = LoggerFactory.getLogger(ChatBillingService.class);
|
||||||
private final JdbcTemplate jdbc;
|
private final JdbcTemplate jdbc;
|
||||||
private final ObjectMapper json;
|
private final ObjectMapper json;
|
||||||
private final UsageAuditService usage;
|
private final UsageAuditService usage;
|
||||||
private final com.moyi.controlplane.gateway.EnterprisePolicies policies;
|
private final com.moyi.controlplane.gateway.EnterprisePolicies policies;
|
||||||
public ChatBillingService(JdbcTemplate jdbc, ObjectMapper json, UsageAuditService usage) { this.jdbc=jdbc; this.json=json; this.usage=usage; this.policies=new com.moyi.controlplane.gateway.EnterprisePolicies(jdbc,json); }
|
private final TransactionTemplate recoveryTransactions;
|
||||||
|
public ChatBillingService(JdbcTemplate jdbc, ObjectMapper json, UsageAuditService usage) {
|
||||||
|
this.jdbc=jdbc; this.json=json; this.usage=usage; this.policies=new com.moyi.controlplane.gateway.EnterprisePolicies(jdbc,json);
|
||||||
|
this.recoveryTransactions = jdbc.getDataSource() == null ? null
|
||||||
|
: new TransactionTemplate(new DataSourceTransactionManager(jdbc.getDataSource()));
|
||||||
|
}
|
||||||
public record Ticket(UUID id, UUID userId, ChatMeter.Rates rates) {}
|
public record Ticket(UUID id, UUID userId, ChatMeter.Rates rates) {}
|
||||||
public void endpoint(UUID id,String endpoint) {jdbc.update("UPDATE usage_events SET endpoint=? WHERE id=?",endpoint,id);}
|
public void endpoint(UUID id,String endpoint) {jdbc.update("UPDATE usage_events SET endpoint=? WHERE id=?",endpoint,id);}
|
||||||
public void observedUsage(Ticket ticket,String protocol,Object observed) {
|
public void observedUsage(Ticket ticket,String protocol,Object observed) {
|
||||||
if(observed==null) return;
|
if(observed==null) return;
|
||||||
usage.observeUsage(ticket.id(),protocol,observed);
|
try { usage.observeUsage(ticket.id(),protocol,observed); }
|
||||||
|
catch (RuntimeException exception) {
|
||||||
|
// Usage diagnostics are best-effort; canonical normalized usage is persisted before settlement.
|
||||||
|
log.warn("Observed provider usage could not be persisted ({})", exception.getClass().getSimpleName());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Persist a minimal, normalized meter before attempting the atomic ledger transaction. */
|
||||||
|
@Transactional
|
||||||
|
public void prepareSettlement(Ticket ticket, Map<String,Object> normalizedUsage, int latencyMs, Integer firstTokenMs) {
|
||||||
|
jdbc.update("UPDATE chat_billing_requests SET settlement_usage=?::jsonb, settlement_latency_ms=?, settlement_first_token_ms=?, expires_at=GREATEST(expires_at,now()+interval '5 minutes') WHERE id=? AND status='pending'",
|
||||||
|
encode(normalizedUsage), Math.max(0,latencyMs), firstTokenMs, ticket.id());
|
||||||
}
|
}
|
||||||
@Transactional public void actualRoute(Ticket ticket,Map<String,Object> route) {
|
@Transactional public void actualRoute(Ticket ticket,Map<String,Object> route) {
|
||||||
var snapshots=jdbc.queryForList("SELECT procurement_pricing FROM model_price_history WHERE model_id=? ORDER BY id DESC LIMIT 1",route.get("id"));
|
var snapshots=jdbc.queryForList("SELECT procurement_pricing FROM model_price_history WHERE model_id=? ORDER BY id DESC LIMIT 1",route.get("id"));
|
||||||
@@ -36,6 +58,12 @@ public class ChatBillingService {
|
|||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
public Ticket reserve(Map<String,Object> key, Map<String,Object> model, ChatMeter.Request request, String idempotencyKey) {
|
public Ticket reserve(Map<String,Object> key, Map<String,Object> model, ChatMeter.Request request, String idempotencyKey) {
|
||||||
|
return reserve(key,model,request,idempotencyKey,ApiRequestMetadata.internal());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public Ticket reserve(Map<String,Object> key, Map<String,Object> model, ChatMeter.Request request,
|
||||||
|
String idempotencyKey, ApiRequestMetadata metadata) {
|
||||||
UUID userId=UUID.fromString(key.get("userId").toString());
|
UUID userId=UUID.fromString(key.get("userId").toString());
|
||||||
var versions=jdbc.queryForList("SELECT id,pricing,procurement_pricing FROM model_price_history WHERE model_id=? ORDER BY id DESC LIMIT 1",model.get("id"));
|
var versions=jdbc.queryForList("SELECT id,pricing,procurement_pricing FROM model_price_history WHERE model_id=? ORDER BY id DESC LIMIT 1",model.get("id"));
|
||||||
var version=versions.isEmpty()?Map.<String,Object>of():versions.getFirst();
|
var version=versions.isEmpty()?Map.<String,Object>of():versions.getFirst();
|
||||||
@@ -58,7 +86,7 @@ public class ChatBillingService {
|
|||||||
if(available.compareTo(hold)<0) throw new ApiException("INSUFFICIENT_BALANCE","可用余额不足以预留本次调用费用,请充值或调低输出 Token 上限",402);
|
if(available.compareTo(hold)<0) throw new ApiException("INSUFFICIENT_BALANCE","可用余额不足以预留本次调用费用,请充值或调低输出 Token 上限",402);
|
||||||
UUID id=UUID.randomUUID();
|
UUID id=UUID.randomUUID();
|
||||||
policies.reserve(id.toString(),userId,UUID.fromString(key.get("id").toString()),hold);
|
policies.reserve(id.toString(),userId,UUID.fromString(key.get("id").toString()),hold);
|
||||||
usage.startChat(id,userId,UUID.fromString(key.get("id").toString()),model,request.body());
|
usage.startChat(id,userId,UUID.fromString(key.get("id").toString()),model,request.body(),metadata);
|
||||||
// Longer than the upstream hard deadline and the concurrency lease, including cleanup margin.
|
// Longer than the upstream hard deadline and the concurrency lease, including cleanup margin.
|
||||||
jdbc.update("INSERT INTO chat_billing_requests(id,user_id,idempotency_hash,request_hash,reserved_amount,pricing,expires_at) VALUES (?,?,?,?,?,?::jsonb,now()+interval '5 minutes')",id,userId,idem,requestHash,hold,encode(rates.snapshot()));
|
jdbc.update("INSERT INTO chat_billing_requests(id,user_id,idempotency_hash,request_hash,reserved_amount,pricing,expires_at) VALUES (?,?,?,?,?,?::jsonb,now()+interval '5 minutes')",id,userId,idem,requestHash,hold,encode(rates.snapshot()));
|
||||||
jdbc.update("UPDATE chat_billing_requests SET procurement_pricing=?::jsonb,price_version=? WHERE id=?",version.get("procurement_pricing")==null?null:version.get("procurement_pricing").toString(),version.get("id"),id);
|
jdbc.update("UPDATE chat_billing_requests SET procurement_pricing=?::jsonb,price_version=? WHERE id=?",version.get("procurement_pricing")==null?null:version.get("procurement_pricing").toString(),version.get("id"),id);
|
||||||
@@ -84,7 +112,7 @@ public class ChatBillingService {
|
|||||||
BigDecimal procurement=ChatMeter.rates(decode(call.get("procurement_pricing").toString()),false).cost(tokens);
|
BigDecimal procurement=ChatMeter.rates(decode(call.get("procurement_pricing").toString()),false).cost(tokens);
|
||||||
jdbc.update("UPDATE usage_events SET procurement_cost=?,profit_amount=?,cost_status='estimated' WHERE id=?",procurement,cost.subtract(procurement),ticket.id());
|
jdbc.update("UPDATE usage_events SET procurement_cost=?,profit_amount=?,cost_status='estimated' WHERE id=?",procurement,cost.subtract(procurement),ticket.id());
|
||||||
}
|
}
|
||||||
jdbc.update("UPDATE chat_billing_requests SET status='succeeded',cost_amount=? WHERE id=?",cost,ticket.id());
|
jdbc.update("UPDATE chat_billing_requests SET status='succeeded',cost_amount=?,settlement_usage=NULL,settlement_latency_ms=NULL,settlement_first_token_ms=NULL WHERE id=?",cost,ticket.id());
|
||||||
policies.release(ticket.id().toString());
|
policies.release(ticket.id().toString());
|
||||||
return cost;
|
return cost;
|
||||||
}
|
}
|
||||||
@@ -96,17 +124,39 @@ public class ChatBillingService {
|
|||||||
var call=jdbc.queryForMap("SELECT * FROM chat_billing_requests WHERE id=? FOR UPDATE",id);
|
var call=jdbc.queryForMap("SELECT * FROM chat_billing_requests WHERE id=? FOR UPDATE",id);
|
||||||
if(!"pending".equals(call.get("status"))) return;
|
if(!"pending".equals(call.get("status"))) return;
|
||||||
jdbc.update("UPDATE user_wallets SET reserved_balance=reserved_balance-?,updated_at=now() WHERE user_id=?",call.get("reserved_amount"),call.get("user_id"));
|
jdbc.update("UPDATE user_wallets SET reserved_balance=reserved_balance-?,updated_at=now() WHERE user_id=?",call.get("reserved_amount"),call.get("user_id"));
|
||||||
jdbc.update("UPDATE chat_billing_requests SET status='failed',cost_amount=0 WHERE id=?",id);
|
jdbc.update("UPDATE chat_billing_requests SET status='failed',cost_amount=0,settlement_usage=NULL,settlement_latency_ms=NULL,settlement_first_token_ms=NULL WHERE id=?",id);
|
||||||
usage.finishChat(id,"failed",new ChatMeter.Tokens(0,0,0),latency,null,BigDecimal.ZERO,null,code,message);
|
usage.finishChat(id,"failed",new ChatMeter.Tokens(0,0,0),latency,null,BigDecimal.ZERO,null,code,message);
|
||||||
jdbc.update("UPDATE usage_events SET cost_status='pending' WHERE id=?",id);
|
jdbc.update("UPDATE usage_events SET cost_status='pending' WHERE id=?",id);
|
||||||
policies.release(id.toString());
|
policies.release(id.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Scheduled(fixedDelayString="${moyi.billing.recovery-interval-ms:60000}")
|
@Scheduled(fixedDelayString="${moyi.billing.recovery-interval-ms:60000}")
|
||||||
@Transactional
|
|
||||||
public void recoverExpired() {
|
public void recoverExpired() {
|
||||||
var ids=jdbc.queryForList("SELECT id FROM chat_billing_requests WHERE status='pending' AND expires_at<now() ORDER BY expires_at LIMIT 100 FOR UPDATE SKIP LOCKED",UUID.class);
|
var ids=jdbc.queryForList("SELECT id FROM chat_billing_requests WHERE status='pending' AND expires_at<now() ORDER BY expires_at LIMIT 100",UUID.class);
|
||||||
for(UUID id:ids) release(id,"CALL_INTERRUPTED","调用中断,未取得可结算用量,预留额度已释放",0);
|
for(UUID id:ids) {
|
||||||
|
try {
|
||||||
|
if (recoveryTransactions == null) recoverOne(id);
|
||||||
|
else recoveryTransactions.executeWithoutResult(status -> recoverOne(id));
|
||||||
|
} catch (RuntimeException exception) {
|
||||||
|
// Keep both the pending ticket and its wallet hold. A later scheduler tick retries the durable meter.
|
||||||
|
log.warn("Pending chat settlement recovery failed ({})", exception.getClass().getSimpleName());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void recoverOne(UUID id) {
|
||||||
|
var rows=jdbc.queryForList("SELECT user_id,settlement_usage::text AS settlement_usage,settlement_latency_ms,settlement_first_token_ms FROM chat_billing_requests WHERE id=? AND status='pending' AND expires_at<now() FOR UPDATE SKIP LOCKED",id);
|
||||||
|
if(rows.isEmpty()) return;
|
||||||
|
var row=rows.getFirst();
|
||||||
|
Object usageSnapshot=row.get("settlement_usage");
|
||||||
|
if(usageSnapshot!=null) {
|
||||||
|
Object firstToken=row.get("settlement_first_token_ms");
|
||||||
|
settle(new Ticket(id,(UUID)row.get("user_id"),null),decode(usageSnapshot.toString()),
|
||||||
|
row.get("settlement_latency_ms")==null?0:((Number)row.get("settlement_latency_ms")).intValue(),
|
||||||
|
firstToken==null?null:((Number)firstToken).intValue());
|
||||||
|
} else {
|
||||||
|
release(id,"CALL_INTERRUPTED","调用中断,未取得可结算用量,预留额度已释放",0);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
private String encode(Object value) { try { return json.writeValueAsString(value); } catch(Exception e) { throw new ApiException("INVALID_REQUEST","请求无法编码"); } }
|
private String encode(Object value) { try { return json.writeValueAsString(value); } catch(Exception e) { throw new ApiException("INVALID_REQUEST","请求无法编码"); } }
|
||||||
private Map<String,Object> decode(String value) { try { return json.readValue(value,new com.fasterxml.jackson.core.type.TypeReference<>(){}); } catch(Exception e) { throw new IllegalStateException("Invalid billing snapshot",e); } }
|
private Map<String,Object> decode(String value) { try { return json.readValue(value,new com.fasterxml.jackson.core.type.TypeReference<>(){}); } catch(Exception e) { throw new IllegalStateException("Invalid billing snapshot",e); } }
|
||||||
|
|||||||
@@ -43,6 +43,7 @@ management:
|
|||||||
moyi:
|
moyi:
|
||||||
gateway:
|
gateway:
|
||||||
redis-prefix: ${MOYI_REDIS_KEY_PREFIX:moyi:}
|
redis-prefix: ${MOYI_REDIS_KEY_PREFIX:moyi:}
|
||||||
|
risk-mode: ${GATEWAY_RISK_MODE:shadow}
|
||||||
requests-per-minute: ${GATEWAY_REQUESTS_PER_MINUTE:60}
|
requests-per-minute: ${GATEWAY_REQUESTS_PER_MINUTE:60}
|
||||||
reads-per-minute: ${GATEWAY_READS_PER_MINUTE:300}
|
reads-per-minute: ${GATEWAY_READS_PER_MINUTE:300}
|
||||||
# 0 disables the MOYI-side model-group cap so channel concurrency can be
|
# 0 disables the MOYI-side model-group cap so channel concurrency can be
|
||||||
|
|||||||
+5
@@ -0,0 +1,5 @@
|
|||||||
|
-- Persist only the normalized meter needed to retry settlement after a transient ledger failure.
|
||||||
|
-- Prompts, generated content and provider credentials are deliberately excluded.
|
||||||
|
ALTER TABLE chat_billing_requests ADD COLUMN settlement_usage jsonb;
|
||||||
|
ALTER TABLE chat_billing_requests ADD COLUMN settlement_latency_ms integer;
|
||||||
|
ALTER TABLE chat_billing_requests ADD COLUMN settlement_first_token_ms integer;
|
||||||
+67
-3
@@ -308,7 +308,7 @@ class PlatformAdminPostgresTest {
|
|||||||
} finally { org.springframework.web.context.request.RequestContextHolder.resetRequestAttributes(); }
|
} finally { org.springframework.web.context.request.RequestContextHolder.resetRequestAttributes(); }
|
||||||
var saved=jdbc.queryForMap("SELECT * FROM usage_events WHERE user_id=?",memberId);
|
var saved=jdbc.queryForMap("SELECT * FROM usage_events WHERE user_id=?",memberId);
|
||||||
assertEquals("/v1/chat/completions",saved.get("endpoint"));
|
assertEquals("/v1/chat/completions",saved.get("endpoint"));
|
||||||
assertEquals("playground",saved.get("request_source"));
|
assertEquals("api",saved.get("request_source"),"a caller-supplied source header must not reclassify a public API request");
|
||||||
assertEquals("Local test browser",saved.get("user_agent"));
|
assertEquals("Local test browser",saved.get("user_agent"));
|
||||||
assertEquals(80L,saved.get("cached_input_units"));
|
assertEquals(80L,saved.get("cached_input_units"));
|
||||||
assertEquals(5L,saved.get("cache_write_units"));
|
assertEquals(5L,saved.get("cache_write_units"));
|
||||||
@@ -482,7 +482,13 @@ class PlatformAdminPostgresTest {
|
|||||||
.withProperty("moyi.providers.deepseek.base-url","http://127.0.0.1:"+upstream.getAddress().getPort()+"/v1");
|
.withProperty("moyi.providers.deepseek.base-url","http://127.0.0.1:"+upstream.getAddress().getPort()+"/v1");
|
||||||
var provider=new com.moyi.controlplane.provider.OpenAiCompatibleProvider(json,environment,jdbc,new SecretCipher("local-test"));
|
var provider=new com.moyi.controlplane.provider.OpenAiCompatibleProvider(json,environment,jdbc,new SecretCipher("local-test"));
|
||||||
try {
|
try {
|
||||||
var controller=new com.moyi.controlplane.gateway.GatewayController(catalog,provider,org.mockito.Mockito.mock(com.moyi.controlplane.gateway.GatewayGuard.class),f.billing(),json);
|
var guard=org.mockito.Mockito.mock(com.moyi.controlplane.gateway.GatewayGuard.class);
|
||||||
|
org.mockito.Mockito.doReturn(org.mockito.Mockito.mock(com.moyi.controlplane.gateway.GatewayGuard.Admission.class))
|
||||||
|
.when(guard).acquire(org.mockito.ArgumentMatchers.anyMap(),org.mockito.ArgumentMatchers.any(java.util.UUID.class),org.mockito.ArgumentMatchers.anyLong());
|
||||||
|
var requestExecution=new com.moyi.controlplane.gateway.request.GatewayRequestExecutionService(
|
||||||
|
f.billing(),new com.moyi.controlplane.gateway.request.ApiRequestRiskPolicy(
|
||||||
|
com.moyi.controlplane.gateway.request.ApiRequestRiskEngine.allowAll()));
|
||||||
|
var controller=new com.moyi.controlplane.gateway.GatewayController(catalog,provider,guard,requestExecution,json);
|
||||||
var http=MockMvcBuilders.standaloneSetup(controller).setControllerAdvice(new ApiExceptionHandler()).build();
|
var http=MockMvcBuilders.standaloneSetup(controller).setControllerAdvice(new ApiExceptionHandler()).build();
|
||||||
var input=new LinkedHashMap<>(f.request().body());
|
var input=new LinkedHashMap<>(f.request().body());
|
||||||
http.perform(post("/v1/chat/completions").header("Authorization","Bearer "+key).header("Idempotency-Key","retry-test").contentType("application/json").content(json.writeValueAsBytes(input)))
|
http.perform(post("/v1/chat/completions").header("Authorization","Bearer "+key).header("Idempotency-Key","retry-test").contentType("application/json").content(json.writeValueAsBytes(input)))
|
||||||
@@ -494,7 +500,7 @@ class PlatformAdminPostgresTest {
|
|||||||
.andExpect(status().isOk()).andExpect(content().contentTypeCompatibleWith("text/event-stream")).andReturn().getResponse().getContentAsString();
|
.andExpect(status().isOk()).andExpect(content().contentTypeCompatibleWith("text/event-stream")).andReturn().getResponse().getContentAsString();
|
||||||
assertTrue(streamed.endsWith("data: [DONE]\n\n")); assertFalse(streamed.contains("private-alias"));
|
assertTrue(streamed.endsWith("data: [DONE]\n\n")); assertFalse(streamed.contains("private-alias"));
|
||||||
assertEquals(new BigDecimal("0.99932000"),jdbc.queryForObject("SELECT balance FROM user_wallets WHERE user_id=?",BigDecimal.class,memberId));
|
assertEquals(new BigDecimal("0.99932000"),jdbc.queryForObject("SELECT balance FROM user_wallets WHERE user_id=?",BigDecimal.class,memberId));
|
||||||
assertEquals(1,jdbc.queryForObject("SELECT count(*) FROM usage_events WHERE call_mode='stream' AND request_source='playground' AND cost_amount=0.00034 AND first_token_ms IS NOT NULL",Integer.class));
|
assertEquals(1,jdbc.queryForObject("SELECT count(*) FROM usage_events WHERE call_mode='stream' AND request_source='api' AND cost_amount=0.00034 AND first_token_ms IS NOT NULL",Integer.class));
|
||||||
mode.set("no-usage");
|
mode.set("no-usage");
|
||||||
var failed=http.perform(post("/v1/chat/completions").header("Authorization","Bearer "+key).contentType("application/json").content(json.writeValueAsBytes(input))).andReturn().getResponse().getContentAsString();
|
var failed=http.perform(post("/v1/chat/completions").header("Authorization","Bearer "+key).contentType("application/json").content(json.writeValueAsBytes(input))).andReturn().getResponse().getContentAsString();
|
||||||
assertTrue(failed.contains("UPSTREAM_USAGE_MISSING"));
|
assertTrue(failed.contains("UPSTREAM_USAGE_MISSING"));
|
||||||
@@ -530,4 +536,62 @@ class PlatformAdminPostgresTest {
|
|||||||
f.billing().fail(ticket,"SETTLEMENT_FAILED","Test rollback",5);
|
f.billing().fail(ticket,"SETTLEMENT_FAILED","Test rollback",5);
|
||||||
assertEquals(0,jdbc.queryForObject("SELECT reserved_balance FROM user_wallets WHERE user_id=?",BigDecimal.class,memberId).signum());
|
assertEquals(0,jdbc.queryForObject("SELECT reserved_balance FROM user_wallets WHERE user_id=?",BigDecimal.class,memberId).signum());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test void explicitRequestMetadataPersistsWithoutThreadLocalHttpContext() {
|
||||||
|
var f=chatFixture(); wallet.grant(ownerToken,memberId,UUID.randomUUID(),"1","metadata test",meta);
|
||||||
|
var metadata=com.moyi.controlplane.gateway.request.ApiRequestMetadata.playground("198.51.100.7");
|
||||||
|
var ticket=f.billing().reserve(f.key(),f.model(),f.request(),"metadata",metadata);
|
||||||
|
var saved=jdbc.queryForMap("SELECT request_source,host(source_ip) AS client_ip FROM usage_events WHERE id=?",ticket.id());
|
||||||
|
assertEquals("playground",saved.get("request_source"));
|
||||||
|
assertEquals("198.51.100.7",saved.get("client_ip"));
|
||||||
|
f.billing().fail(ticket,"TEST_COMPLETE","metadata fixture",1);
|
||||||
|
|
||||||
|
var audit=context.getBean(UsageAuditService.class);
|
||||||
|
audit.recordTask(memberId,UUID.fromString(String.valueOf(f.key().get("id"))),String.valueOf(f.model().get("id")),
|
||||||
|
String.valueOf(f.model().get("providerId")),"metadata-task","queued",Map.of(),null,null,null,
|
||||||
|
com.moyi.controlplane.gateway.request.ApiRequestMetadata.fromHttp("203.0.113.8"));
|
||||||
|
saved=jdbc.queryForMap("SELECT request_source,host(source_ip) AS client_ip FROM usage_events WHERE provider_task_id='metadata-task'");
|
||||||
|
assertEquals("api",saved.get("request_source"));
|
||||||
|
assertEquals("203.0.113.8",saved.get("client_ip"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test void gatewaySettlementFailureKeepsDurableMeterAndRecoversExactlyOnce() {
|
||||||
|
var f=chatFixture(); wallet.grant(ownerToken,memberId,UUID.randomUUID(),"1","recoverable settlement test",meta);
|
||||||
|
var billing=f.billing();
|
||||||
|
var execution=new com.moyi.controlplane.gateway.request.GatewayRequestExecutionService(billing,
|
||||||
|
new com.moyi.controlplane.gateway.request.ApiRequestRiskPolicy(
|
||||||
|
com.moyi.controlplane.gateway.request.ApiRequestRiskEngine.allowAll()));
|
||||||
|
var requestContext=new com.moyi.controlplane.gateway.request.ApiRequestContext(memberId,
|
||||||
|
UUID.fromString(String.valueOf(f.key().get("id"))),
|
||||||
|
com.moyi.controlplane.gateway.request.ApiRequestProtocol.CHAT_COMPLETIONS,
|
||||||
|
String.valueOf(f.model().get("id")),String.valueOf(f.model().get("id")),false,
|
||||||
|
f.request().reservedInputTokens(),f.request().maxOutputTokens(),
|
||||||
|
com.moyi.controlplane.gateway.request.ApiRequestMetadata.internal(),System.nanoTime());
|
||||||
|
jdbc.execute("ALTER TABLE usage_events ADD CONSTRAINT test_reject_recoverable_cost CHECK (cost_amount IS NULL OR cost_amount=0)");
|
||||||
|
try {
|
||||||
|
String result=execution.execute(requestContext,f.key(),f.model(),f.request(),null,
|
||||||
|
() -> (com.moyi.controlplane.gateway.request.ApiRequestAdmission)()->{},
|
||||||
|
ticket -> { execution.settle(ticket,meteredResult(),5,null); return "unexpected"; },
|
||||||
|
failure -> failure instanceof ApiException api ? api : new ApiException("CALL_INTERRUPTED","interrupted",502),
|
||||||
|
ApiException::getCode);
|
||||||
|
assertEquals("SETTLEMENT_PENDING",result);
|
||||||
|
UUID ticketId=jdbc.queryForObject("SELECT id FROM chat_billing_requests WHERE user_id=? ORDER BY created_at DESC LIMIT 1",UUID.class,memberId);
|
||||||
|
assertEquals("pending",jdbc.queryForObject("SELECT status FROM chat_billing_requests WHERE id=?",String.class,ticketId));
|
||||||
|
assertNotNull(jdbc.queryForObject("SELECT settlement_usage FROM chat_billing_requests WHERE id=?",Object.class,ticketId));
|
||||||
|
assertTrue(jdbc.queryForObject("SELECT reserved_balance FROM user_wallets WHERE user_id=?",BigDecimal.class,memberId).signum()>0);
|
||||||
|
assertEquals(0,jdbc.queryForObject("SELECT count(*) FROM wallet_entries WHERE request_id=?",Integer.class,ticketId));
|
||||||
|
|
||||||
|
jdbc.execute("ALTER TABLE usage_events DROP CONSTRAINT test_reject_recoverable_cost");
|
||||||
|
jdbc.update("UPDATE chat_billing_requests SET expires_at=now()-interval '1 second' WHERE id=?",ticketId);
|
||||||
|
billing.recoverExpired();
|
||||||
|
billing.recoverExpired();
|
||||||
|
|
||||||
|
assertEquals("succeeded",jdbc.queryForObject("SELECT status FROM chat_billing_requests WHERE id=?",String.class,ticketId));
|
||||||
|
assertEquals(new BigDecimal("0.99966000"),jdbc.queryForObject("SELECT balance FROM user_wallets WHERE user_id=?",BigDecimal.class,memberId));
|
||||||
|
assertEquals(0,jdbc.queryForObject("SELECT reserved_balance FROM user_wallets WHERE user_id=?",BigDecimal.class,memberId).signum());
|
||||||
|
assertEquals(1,jdbc.queryForObject("SELECT count(*) FROM wallet_entries WHERE request_id=?",Integer.class,ticketId));
|
||||||
|
} finally {
|
||||||
|
jdbc.execute("ALTER TABLE usage_events DROP CONSTRAINT IF EXISTS test_reject_recoverable_cost");
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
+19
-9
@@ -7,6 +7,9 @@ import com.moyi.controlplane.provider.OpenAiCompatibleProvider;
|
|||||||
import com.moyi.controlplane.task.AsyncTaskService;
|
import com.moyi.controlplane.task.AsyncTaskService;
|
||||||
import com.moyi.controlplane.usage.UsageAuditService;
|
import com.moyi.controlplane.usage.UsageAuditService;
|
||||||
import com.moyi.controlplane.wallet.ChatBillingService;
|
import com.moyi.controlplane.wallet.ChatBillingService;
|
||||||
|
import com.moyi.controlplane.gateway.request.ApiRequestRiskEngine;
|
||||||
|
import com.moyi.controlplane.gateway.request.ApiRequestRiskPolicy;
|
||||||
|
import com.moyi.controlplane.gateway.request.GatewayRequestExecutionService;
|
||||||
import org.springframework.mock.web.MockHttpServletResponse;
|
import org.springframework.mock.web.MockHttpServletResponse;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
@@ -20,6 +23,13 @@ import static org.mockito.ArgumentMatchers.*;
|
|||||||
import static org.mockito.Mockito.*;
|
import static org.mockito.Mockito.*;
|
||||||
|
|
||||||
class GatewayDeploymentTest {
|
class GatewayDeploymentTest {
|
||||||
|
private static GatewayController controller(CatalogService catalog, OpenAiCompatibleProvider provider,
|
||||||
|
GatewayGuard guard, ChatBillingService billing, ObjectMapper json) {
|
||||||
|
doReturn(mock(GatewayGuard.Admission.class)).when(guard).acquire(anyMap(), any(UUID.class), anyLong());
|
||||||
|
var requests = new GatewayRequestExecutionService(billing,
|
||||||
|
new ApiRequestRiskPolicy(ApiRequestRiskEngine.allowAll()));
|
||||||
|
return new GatewayController(catalog, provider, guard, requests, json);
|
||||||
|
}
|
||||||
@Test void failedTaskClaimReleasesRedisLeaseAfterDatabaseRollback() {
|
@Test void failedTaskClaimReleasesRedisLeaseAfterDatabaseRollback() {
|
||||||
var redis=mock(StringRedisTemplate.class);doReturn(1L).when(redis).execute(any(RedisScript.class),anyList(),any(Object[].class));
|
var redis=mock(StringRedisTemplate.class);doReturn(1L).when(redis).execute(any(RedisScript.class),anyList(),any(Object[].class));
|
||||||
var policies=mock(EnterprisePolicies.class);UUID key=UUID.randomUUID(),user=UUID.randomUUID();
|
var policies=mock(EnterprisePolicies.class);UUID key=UUID.randomUUID(),user=UUID.randomUUID();
|
||||||
@@ -124,7 +134,7 @@ class GatewayDeploymentTest {
|
|||||||
when(catalog.authenticateKey("test-key")).thenReturn(key);
|
when(catalog.authenticateKey("test-key")).thenReturn(key);
|
||||||
when(catalog.listModels(false)).thenReturn(List.of(route));
|
when(catalog.listModels(false)).thenReturn(List.of(route));
|
||||||
when(catalog.keyCanInvoke(key, "route-1")).thenReturn(true);
|
when(catalog.keyCanInvoke(key, "route-1")).thenReturn(true);
|
||||||
GatewayController controller = new GatewayController(catalog, mock(OpenAiCompatibleProvider.class), guard, mock(ChatBillingService.class), new ObjectMapper());
|
GatewayController controller = controller(catalog, mock(OpenAiCompatibleProvider.class), guard, mock(ChatBillingService.class), new ObjectMapper());
|
||||||
|
|
||||||
Map<String, Object> response = controller.models("Bearer test-key");
|
Map<String, Object> response = controller.models("Bearer test-key");
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
@@ -163,7 +173,7 @@ class GatewayDeploymentTest {
|
|||||||
when(catalog.activeModel("glm-5--apihub")).thenReturn(null);
|
when(catalog.activeModel("glm-5--apihub")).thenReturn(null);
|
||||||
when(catalog.listModels(false)).thenReturn(List.of(route));
|
when(catalog.listModels(false)).thenReturn(List.of(route));
|
||||||
when(catalog.keyCanInvoke(key, "route-readable")).thenReturn(true);
|
when(catalog.keyCanInvoke(key, "route-readable")).thenReturn(true);
|
||||||
GatewayController controller = new GatewayController(catalog, mock(OpenAiCompatibleProvider.class), guard, mock(ChatBillingService.class), new ObjectMapper());
|
GatewayController controller = controller(catalog, mock(OpenAiCompatibleProvider.class), guard, mock(ChatBillingService.class), new ObjectMapper());
|
||||||
assertSame(route, controller.resolveModel(key, "glm-5--apihub--" + tag("route-readable")));
|
assertSame(route, controller.resolveModel(key, "glm-5--apihub--" + tag("route-readable")));
|
||||||
verify(catalog).keyCanInvoke(key, "route-readable");
|
verify(catalog).keyCanInvoke(key, "route-readable");
|
||||||
}
|
}
|
||||||
@@ -236,7 +246,7 @@ class GatewayDeploymentTest {
|
|||||||
when(catalog.authenticateKey("test-key")).thenReturn(key);
|
when(catalog.authenticateKey("test-key")).thenReturn(key);
|
||||||
when(catalog.activeModel("model-real")).thenReturn(Map.of("id", "model-real", "providerId", "provider-deepseek", "mode", "sync", "type", "text"));
|
when(catalog.activeModel("model-real")).thenReturn(Map.of("id", "model-real", "providerId", "provider-deepseek", "mode", "sync", "type", "text"));
|
||||||
when(catalog.keyCanInvoke(key, "model-real")).thenReturn(true);
|
when(catalog.keyCanInvoke(key, "model-real")).thenReturn(true);
|
||||||
GatewayController controller = new GatewayController(catalog, provider, guard, mock(ChatBillingService.class),new ObjectMapper());
|
GatewayController controller = controller(catalog, provider, guard, mock(ChatBillingService.class),new ObjectMapper());
|
||||||
ApiException error = assertThrows(ApiException.class, () -> controller.chatCompletions("Bearer test-key", Map.of("model", "model-real"),null,new MockHttpServletResponse()));
|
ApiException error = assertThrows(ApiException.class, () -> controller.chatCompletions("Bearer test-key", Map.of("model", "model-real"),null,new MockHttpServletResponse()));
|
||||||
assertEquals("PROVIDER_NOT_CONFIGURED", error.getCode());
|
assertEquals("PROVIDER_NOT_CONFIGURED", error.getCode());
|
||||||
assertEquals(503, error.getStatus());
|
assertEquals(503, error.getStatus());
|
||||||
@@ -251,9 +261,9 @@ class GatewayDeploymentTest {
|
|||||||
when(catalog.activeModel("mock")).thenReturn(Map.of("id", "mock", "providerId", "provider-mock", "mode", "sync", "type", "text"));
|
when(catalog.activeModel("mock")).thenReturn(Map.of("id", "mock", "providerId", "provider-mock", "mode", "sync", "type", "text"));
|
||||||
when(catalog.keyCanInvoke(key, "mock")).thenReturn(true);
|
when(catalog.keyCanInvoke(key, "mock")).thenReturn(true);
|
||||||
var billing=mock(ChatBillingService.class);
|
var billing=mock(ChatBillingService.class);
|
||||||
when(billing.reserve(anyMap(),anyMap(),any(),any())).thenReturn(new ChatBillingService.Ticket(UUID.randomUUID(),UUID.fromString(key.get("userId").toString()),ChatMeter.rates(null,true)));
|
when(billing.reserve(anyMap(),anyMap(),any(),any(),any())).thenReturn(new ChatBillingService.Ticket(UUID.randomUUID(),UUID.fromString(key.get("userId").toString()),ChatMeter.rates(null,true)));
|
||||||
when(billing.settle(any(),anyMap(),anyInt(),any())).thenReturn(java.math.BigDecimal.ZERO);
|
when(billing.settle(any(),anyMap(),anyInt(),any())).thenReturn(java.math.BigDecimal.ZERO);
|
||||||
GatewayController controller = new GatewayController(catalog, mock(OpenAiCompatibleProvider.class), mock(GatewayGuard.class), billing,new ObjectMapper());
|
GatewayController controller = controller(catalog, mock(OpenAiCompatibleProvider.class), mock(GatewayGuard.class), billing,new ObjectMapper());
|
||||||
assertEquals(200, controller.chatCompletions("Bearer test-key", Map.of("model", "mock","messages",List.of(Map.of("role","user","content","test"))),null,new MockHttpServletResponse()).getStatusCode().value());
|
assertEquals(200, controller.chatCompletions("Bearer test-key", Map.of("model", "mock","messages",List.of(Map.of("role","user","content","test"))),null,new MockHttpServletResponse()).getStatusCode().value());
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -276,10 +286,10 @@ class GatewayDeploymentTest {
|
|||||||
"usage", Map.of("prompt_tokens", 2, "completion_tokens", 16, "total_tokens", 18)
|
"usage", Map.of("prompt_tokens", 2, "completion_tokens", 16, "total_tokens", 18)
|
||||||
));
|
));
|
||||||
when(provider.chat(eq("provider-relay"), eq("reasoning-model"), eq("reasoning-route"), anyMap())).thenReturn(upstreamResponse);
|
when(provider.chat(eq("provider-relay"), eq("reasoning-model"), eq("reasoning-route"), anyMap())).thenReturn(upstreamResponse);
|
||||||
when(billing.reserve(anyMap(), anyMap(), any(), isNull())).thenReturn(new ChatBillingService.Ticket(UUID.randomUUID(), UUID.fromString(userId), ChatMeter.rates(null, true)));
|
when(billing.reserve(anyMap(), anyMap(), any(), isNull(), any())).thenReturn(new ChatBillingService.Ticket(UUID.randomUUID(), UUID.fromString(userId), ChatMeter.rates(null, true)));
|
||||||
when(billing.settle(any(), anyMap(), anyInt(), isNull())).thenReturn(java.math.BigDecimal.ZERO);
|
when(billing.settle(any(), anyMap(), anyInt(), isNull())).thenReturn(java.math.BigDecimal.ZERO);
|
||||||
|
|
||||||
GatewayController controller = new GatewayController(catalog, provider, guard, billing, new ObjectMapper());
|
GatewayController controller = controller(catalog, provider, guard, billing, new ObjectMapper());
|
||||||
var response = controller.chatCompletions("Bearer test-key", Map.of("model", "reasoning-route", "messages", List.of(Map.of("role", "user", "content", "hello"))), null, new MockHttpServletResponse());
|
var response = controller.chatCompletions("Bearer test-key", Map.of("model", "reasoning-route", "messages", List.of(Map.of("role", "user", "content", "hello"))), null, new MockHttpServletResponse());
|
||||||
assertEquals(200, response.getStatusCode().value());
|
assertEquals(200, response.getStatusCode().value());
|
||||||
Map<?, ?> choice = (Map<?, ?>)((List<?>)response.getBody().get("choices")).getFirst();
|
Map<?, ?> choice = (Map<?, ?>)((List<?>)response.getBody().get("choices")).getFirst();
|
||||||
@@ -307,8 +317,8 @@ class GatewayDeploymentTest {
|
|||||||
"choices", List.of(Map.of("index", 0, "message", Map.of("role", "assistant", "content", ""), "finish_reason", "stop"))
|
"choices", List.of(Map.of("index", 0, "message", Map.of("role", "assistant", "content", ""), "finish_reason", "stop"))
|
||||||
));
|
));
|
||||||
when(provider.chat(eq("provider-relay"), eq("empty-model"), eq("empty-route"), anyMap())).thenReturn(upstreamResponse);
|
when(provider.chat(eq("provider-relay"), eq("empty-model"), eq("empty-route"), anyMap())).thenReturn(upstreamResponse);
|
||||||
when(billing.reserve(anyMap(), anyMap(), any(), isNull())).thenReturn(new ChatBillingService.Ticket(UUID.randomUUID(), UUID.fromString(userId), ChatMeter.rates(null, true)));
|
when(billing.reserve(anyMap(), anyMap(), any(), isNull(), any())).thenReturn(new ChatBillingService.Ticket(UUID.randomUUID(), UUID.fromString(userId), ChatMeter.rates(null, true)));
|
||||||
GatewayController controller = new GatewayController(catalog, provider, guard, billing, new ObjectMapper());
|
GatewayController controller = controller(catalog, provider, guard, billing, new ObjectMapper());
|
||||||
|
|
||||||
ApiException error = assertThrows(ApiException.class, () -> controller.chatCompletions("Bearer test-key", Map.of("model", "empty-route", "messages", List.of(Map.of("role", "user", "content", "hello"))), null, new MockHttpServletResponse()));
|
ApiException error = assertThrows(ApiException.class, () -> controller.chatCompletions("Bearer test-key", Map.of("model", "empty-route", "messages", List.of(Map.of("role", "user", "content", "hello"))), null, new MockHttpServletResponse()));
|
||||||
assertEquals("UPSTREAM_RESPONSE_INVALID", error.getCode());
|
assertEquals("UPSTREAM_RESPONSE_INVALID", error.getCode());
|
||||||
|
|||||||
+96
@@ -0,0 +1,96 @@
|
|||||||
|
package com.moyi.controlplane.gateway.request;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import com.moyi.controlplane.catalog.CatalogService;
|
||||||
|
import com.moyi.controlplane.common.ApiException;
|
||||||
|
import com.moyi.controlplane.gateway.GatewayController;
|
||||||
|
import com.moyi.controlplane.gateway.GatewayGuard;
|
||||||
|
import com.moyi.controlplane.gateway.NativeGatewayController;
|
||||||
|
import com.moyi.controlplane.provider.OpenAiCompatibleProvider;
|
||||||
|
import com.moyi.controlplane.wallet.ChatBillingService;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.UUID;
|
||||||
|
import java.util.concurrent.atomic.AtomicReference;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.springframework.mock.web.MockHttpServletRequest;
|
||||||
|
import org.springframework.mock.web.MockHttpServletResponse;
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
import static org.mockito.ArgumentMatchers.*;
|
||||||
|
import static org.mockito.Mockito.*;
|
||||||
|
|
||||||
|
class ApiRequestEntryPointsTest {
|
||||||
|
private final UUID userId=UUID.randomUUID(), keyId=UUID.randomUUID();
|
||||||
|
private final Map<String,Object> key=Map.of("id",keyId.toString(),"userId",userId.toString());
|
||||||
|
private final CatalogService catalog=mock(CatalogService.class);
|
||||||
|
private final GatewayGuard guard=mock(GatewayGuard.class);
|
||||||
|
private final ChatBillingService billing=mock(ChatBillingService.class);
|
||||||
|
private final OpenAiCompatibleProvider provider=mock(OpenAiCompatibleProvider.class);
|
||||||
|
private final AtomicReference<ApiRequestContext> observed=new AtomicReference<>();
|
||||||
|
private final GatewayRequestExecutionService requests=new GatewayRequestExecutionService(billing,
|
||||||
|
new ApiRequestRiskPolicy(context -> {
|
||||||
|
observed.set(context);
|
||||||
|
return ApiRequestRiskDecision.deny(new ApiRequestRiskSignal(ApiRequestRiskSignal.Action.DENY,
|
||||||
|
"test.entry-block","RISK_BLOCKED","Blocked",0));
|
||||||
|
}));
|
||||||
|
|
||||||
|
@Test void publicChatIgnoresSpoofedPlaygroundHeaderAndDenialNeverAcquiresOrReserves() {
|
||||||
|
Map<String,Object> model=Map.of("id","chat-route","providerId","provider-mock","type","text","mode","sync");
|
||||||
|
when(catalog.authenticateKey("test-key")).thenReturn(key);
|
||||||
|
when(catalog.activeModel("chat-route")).thenReturn(model);
|
||||||
|
when(catalog.keyCanInvoke(key,"chat-route")).thenReturn(true);
|
||||||
|
var controller=new GatewayController(catalog,provider,guard,requests,new ObjectMapper());
|
||||||
|
var request=http("/v1/chat/completions");
|
||||||
|
|
||||||
|
assertEquals("RISK_BLOCKED",assertThrows(ApiException.class,() -> controller.chatCompletions("Bearer test-key",
|
||||||
|
Map.of("model","chat-route","messages",List.of(Map.of("role","user","content","hello"))),null,
|
||||||
|
request,new MockHttpServletResponse())).getCode());
|
||||||
|
|
||||||
|
assertEquals(ApiRequestMetadata.Source.API,observed.get().metadata().source());
|
||||||
|
assertEquals("198.51.100.7",observed.get().metadata().clientIp());
|
||||||
|
verify(guard).checkRate(keyId,false);
|
||||||
|
verify(guard,never()).acquire(anyMap(),any(UUID.class),anyLong());
|
||||||
|
verifyNoInteractions(billing,provider);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test void nativePublicAndPlaygroundBothEnterRiskBoundaryWithEndpointOwnedSource() {
|
||||||
|
Map<String,Object> model=Map.of("id","embedding-route","providerId","provider-a","upstreamModel","embedding-model","type","embedding","mode","sync");
|
||||||
|
when(catalog.authenticateKey("test-key")).thenReturn(key);
|
||||||
|
when(catalog.activeModel("embedding-route")).thenReturn(model);
|
||||||
|
when(catalog.keyCanInvoke(key,"embedding-route")).thenReturn(true);
|
||||||
|
when(provider.isConfigured("provider-a","embedding-route","embedding-model")).thenReturn(true);
|
||||||
|
var controller=new NativeGatewayController(catalog,guard,requests,provider,new ObjectMapper());
|
||||||
|
var request=http("/v1/embeddings");
|
||||||
|
request.addHeader("Authorization","Bearer test-key");
|
||||||
|
Map<String,Object> input=Map.of("model","embedding-route","input","hello");
|
||||||
|
|
||||||
|
assertEquals("RISK_BLOCKED",assertThrows(ApiException.class,
|
||||||
|
() -> controller.invoke(input,request,new MockHttpServletResponse())).getCode());
|
||||||
|
assertEquals(ApiRequestProtocol.EMBEDDINGS,observed.get().protocol());
|
||||||
|
assertEquals(ApiRequestMetadata.Source.API,observed.get().metadata().source());
|
||||||
|
|
||||||
|
assertEquals("RISK_BLOCKED",assertThrows(ApiException.class,
|
||||||
|
() -> controller.executeForKey("embeddings",input,false,key,request,new MockHttpServletResponse(),
|
||||||
|
ApiRequestMetadata.playground(request.getRemoteAddr()))).getCode());
|
||||||
|
assertEquals(ApiRequestMetadata.Source.PLAYGROUND,observed.get().metadata().source());
|
||||||
|
verify(guard,never()).acquire(anyMap(),any(UUID.class),anyLong());
|
||||||
|
verify(provider,never()).invoke(anyString(),anyString(),anyString(),anyMap(),anyString());
|
||||||
|
verifyNoInteractions(billing);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test void malformedRequestsStillConsumeTheirEarlyRateBudget() {
|
||||||
|
var controller=new NativeGatewayController(catalog,guard,requests,provider,new ObjectMapper());
|
||||||
|
assertThrows(ApiException.class,() -> controller.executeForKey("embeddings",Map.of(),false,key,
|
||||||
|
http("/v1/embeddings"),new MockHttpServletResponse()));
|
||||||
|
verify(guard).checkRate(keyId,false);
|
||||||
|
verifyNoInteractions(billing);
|
||||||
|
}
|
||||||
|
|
||||||
|
private MockHttpServletRequest http(String path) {
|
||||||
|
var request=new MockHttpServletRequest("POST",path);
|
||||||
|
request.setRemoteAddr("198.51.100.7");
|
||||||
|
request.addHeader("X-Moyi-Source","playground");
|
||||||
|
request.addHeader("X-Forwarded-For","203.0.113.99"); // Adapter never parses this untrusted header itself.
|
||||||
|
return request;
|
||||||
|
}
|
||||||
|
}
|
||||||
+59
@@ -0,0 +1,59 @@
|
|||||||
|
package com.moyi.controlplane.gateway.request;
|
||||||
|
|
||||||
|
import java.util.UUID;
|
||||||
|
import java.util.Optional;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
class ApiRequestModuleConfigurationTest {
|
||||||
|
private final ApplicationContextRunner runner = new ApplicationContextRunner()
|
||||||
|
.withUserConfiguration(ApiRequestModuleConfiguration.class);
|
||||||
|
private final ApiRequestContext request = new ApiRequestContext(UUID.randomUUID(), UUID.randomUUID(),
|
||||||
|
ApiRequestProtocol.EMBEDDINGS, "public", "route", false, 12, 0,
|
||||||
|
ApiRequestMetadata.fromHttp("198.51.100.7"), System.nanoTime());
|
||||||
|
|
||||||
|
@Test void noRulesStartsSafelyAndDefaultsToShadow() {
|
||||||
|
runner.run(context -> {
|
||||||
|
assertNull(context.getStartupFailure());
|
||||||
|
assertEquals(ApiRequestRiskDecision.Action.ALLOW, context.getBean(ApiRequestRiskEngine.class).evaluate(request).action());
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test void installedRuleIsShadowedByDefaultAndEnforcedOnlyWhenConfigured() {
|
||||||
|
var configured = runner.withBean("blockedRule", ApiRequestRiskRule.class, () -> rule("test.block", 0));
|
||||||
|
configured.run(context -> assertEquals(ApiRequestRiskDecision.Action.SHADOW,
|
||||||
|
context.getBean(ApiRequestRiskEngine.class).evaluate(request).action()));
|
||||||
|
configured.withPropertyValues("moyi.gateway.risk-mode=enforce").run(context -> assertEquals(
|
||||||
|
ApiRequestRiskDecision.Action.DENY, context.getBean(ApiRequestRiskEngine.class).evaluate(request).action()));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test void throttleAggregationHonorsTheLongestRetryAndRuleFailureClosesOnlyInEnforce() {
|
||||||
|
var engine = new CompositeApiRequestRiskEngine(java.util.List.of(rule("test.short", 2), rule("test.long", 30)),
|
||||||
|
CompositeApiRequestRiskEngine.Mode.ENFORCE);
|
||||||
|
assertEquals(30, engine.evaluate(request).retryAfterSeconds());
|
||||||
|
ApiRequestRiskRule broken = new ApiRequestRiskRule() {
|
||||||
|
public String id() { return "test.broken"; }
|
||||||
|
public Optional<ApiRequestRiskSignal> evaluate(ApiRequestContext context) { throw new IllegalStateException(); }
|
||||||
|
};
|
||||||
|
assertEquals(ApiRequestRiskDecision.Action.SHADOW, new CompositeApiRequestRiskEngine(java.util.List.of(broken),
|
||||||
|
CompositeApiRequestRiskEngine.Mode.SHADOW).evaluate(request).action());
|
||||||
|
assertEquals(ApiRequestRiskDecision.Action.UNAVAILABLE, new CompositeApiRequestRiskEngine(java.util.List.of(broken),
|
||||||
|
CompositeApiRequestRiskEngine.Mode.ENFORCE).evaluate(request).action());
|
||||||
|
assertFalse(request.metadata().toString().contains("198.51.100.7"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test void invalidModeDoesNotSilentlyDisableRisk() {
|
||||||
|
runner.withPropertyValues("moyi.gateway.risk-mode=typo").run(context -> assertNotNull(context.getStartupFailure()));
|
||||||
|
}
|
||||||
|
|
||||||
|
private ApiRequestRiskRule rule(String id, int retry) {
|
||||||
|
return new ApiRequestRiskRule() {
|
||||||
|
public String id() { return id; }
|
||||||
|
public Optional<ApiRequestRiskSignal> evaluate(ApiRequestContext context) {
|
||||||
|
return Optional.of(new ApiRequestRiskSignal(retry == 0 ? ApiRequestRiskSignal.Action.DENY : ApiRequestRiskSignal.Action.THROTTLE,
|
||||||
|
id, "RISK_BLOCKED", "Blocked", retry));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
+197
@@ -0,0 +1,197 @@
|
|||||||
|
package com.moyi.controlplane.gateway.request;
|
||||||
|
|
||||||
|
import com.moyi.controlplane.common.ApiException;
|
||||||
|
import com.moyi.controlplane.gateway.ChatMeter;
|
||||||
|
import com.moyi.controlplane.gateway.GatewayGuard;
|
||||||
|
import com.moyi.controlplane.catalog.CatalogService;
|
||||||
|
import com.moyi.controlplane.task.AsyncTaskService;
|
||||||
|
import com.moyi.controlplane.usage.UsageAuditService;
|
||||||
|
import com.moyi.controlplane.wallet.ChatBillingService;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.UUID;
|
||||||
|
import java.util.concurrent.atomic.AtomicBoolean;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
import static org.mockito.ArgumentMatchers.*;
|
||||||
|
import static org.mockito.Mockito.*;
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import org.springframework.jdbc.core.JdbcTemplate;
|
||||||
|
|
||||||
|
class GatewayRequestExecutionServiceTest {
|
||||||
|
private final UUID userId = UUID.randomUUID();
|
||||||
|
private final UUID keyId = UUID.randomUUID();
|
||||||
|
private final ApiRequestContext context = new ApiRequestContext(userId, keyId,
|
||||||
|
ApiRequestProtocol.CHAT_COMPLETIONS, "public-model", "route-1", false,
|
||||||
|
256, 128, ApiRequestMetadata.fromHttp("127.0.0.1"), System.nanoTime());
|
||||||
|
private final Map<String, Object> key = Map.of("id", keyId.toString(), "userId", userId.toString());
|
||||||
|
private final Map<String, Object> model = Map.of("id", "route-1");
|
||||||
|
private final ChatMeter.Request normalized = new ChatMeter.Request(Map.of("model", "public-model"), 256, 128);
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void denyHappensBeforeAdmissionAndWalletReservation() {
|
||||||
|
ChatBillingService billing = mock(ChatBillingService.class);
|
||||||
|
var service = new GatewayRequestExecutionService(billing,
|
||||||
|
new ApiRequestRiskPolicy(ignored -> ApiRequestRiskDecision.deny(
|
||||||
|
new ApiRequestRiskSignal(ApiRequestRiskSignal.Action.DENY, "test.block", "RISK_BLOCKED", "Blocked", 0))));
|
||||||
|
AtomicBoolean admitted = new AtomicBoolean();
|
||||||
|
|
||||||
|
ApiException error = assertThrows(ApiException.class, () -> service.execute(context, key, model, normalized,
|
||||||
|
null, () -> { admitted.set(true); return mock(GatewayGuard.Admission.class); },
|
||||||
|
ticket -> "unreachable", failure -> new ApiException("CALL_FAILED", "failed", 502),
|
||||||
|
ApiException::getCode));
|
||||||
|
|
||||||
|
assertEquals(403, error.getStatus());
|
||||||
|
assertFalse(admitted.get());
|
||||||
|
verifyNoInteractions(billing);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void shadowSignalDoesNotChangeRequestExecution() {
|
||||||
|
ChatBillingService billing = mock(ChatBillingService.class);
|
||||||
|
var ticket = new ChatBillingService.Ticket(UUID.randomUUID(), userId, null);
|
||||||
|
when(billing.reserve(key, model, normalized, null, context.metadata())).thenReturn(ticket);
|
||||||
|
var service = new GatewayRequestExecutionService(billing,
|
||||||
|
new ApiRequestRiskPolicy(ignored -> ApiRequestRiskDecision.shadow(List.of("test.observe"))));
|
||||||
|
ApiRequestAdmission admission = mock(ApiRequestAdmission.class);
|
||||||
|
|
||||||
|
String result = service.execute(context, key, model, normalized, null, () -> admission,
|
||||||
|
ignored -> "executed", failure -> new ApiException("CALL_FAILED", "failed", 502), ApiException::getCode);
|
||||||
|
|
||||||
|
assertEquals("executed", result);
|
||||||
|
verify(billing).reserve(key, model, normalized, null, context.metadata());
|
||||||
|
verify(admission).close();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void enforceModeFailsClosedWhenRiskEngineThrowsBeforeAdmission() {
|
||||||
|
ChatBillingService billing = mock(ChatBillingService.class);
|
||||||
|
var service = new GatewayRequestExecutionService(billing,
|
||||||
|
new ApiRequestRiskPolicy(ignored -> { throw new IllegalStateException("private detail"); }));
|
||||||
|
AtomicBoolean admitted = new AtomicBoolean();
|
||||||
|
|
||||||
|
ApiException error = assertThrows(ApiException.class, () -> service.execute(context, key, model, normalized,
|
||||||
|
null, () -> { admitted.set(true); return mock(GatewayGuard.Admission.class); },
|
||||||
|
ticket -> "unreachable", failure -> new ApiException("CALL_FAILED", "failed", 502),
|
||||||
|
ApiException::getCode));
|
||||||
|
|
||||||
|
assertEquals("RISK_ENGINE_UNAVAILABLE", error.getCode());
|
||||||
|
assertEquals(503, error.getStatus());
|
||||||
|
assertFalse(admitted.get());
|
||||||
|
assertFalse(error.getMessage().contains("private detail"));
|
||||||
|
verifyNoInteractions(billing);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void admissionLeaseClosesWhenReservationFails() {
|
||||||
|
ChatBillingService billing = mock(ChatBillingService.class);
|
||||||
|
when(billing.reserve(key, model, normalized, null, context.metadata())).thenThrow(new ApiException("INSUFFICIENT_BALANCE", "low", 402));
|
||||||
|
var service = new GatewayRequestExecutionService(billing, new ApiRequestRiskPolicy(ApiRequestRiskEngine.allowAll()));
|
||||||
|
ApiRequestAdmission admission = mock(ApiRequestAdmission.class);
|
||||||
|
|
||||||
|
assertEquals("INSUFFICIENT_BALANCE", assertThrows(ApiException.class, () -> service.execute(context, key, model,
|
||||||
|
normalized, null, () -> admission, ticket -> "unreachable",
|
||||||
|
failure -> new ApiException("CALL_FAILED", "failed", 502), ApiException::getCode)).getCode());
|
||||||
|
|
||||||
|
verify(admission).close();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void workFailureIsRecordedAndAdmissionLeaseCloses() {
|
||||||
|
ChatBillingService billing = mock(ChatBillingService.class);
|
||||||
|
var ticket = new ChatBillingService.Ticket(UUID.randomUUID(), userId, null);
|
||||||
|
when(billing.reserve(key, model, normalized, null, context.metadata())).thenReturn(ticket);
|
||||||
|
var service = new GatewayRequestExecutionService(billing, new ApiRequestRiskPolicy(ApiRequestRiskEngine.allowAll()));
|
||||||
|
ApiRequestAdmission admission = mock(ApiRequestAdmission.class);
|
||||||
|
|
||||||
|
assertEquals("CALL_INTERRUPTED", service.execute(context, key, model, normalized, null, () -> admission,
|
||||||
|
ignored -> { throw new IllegalStateException("failure"); },
|
||||||
|
failure -> new ApiException("CALL_INTERRUPTED", "failed", 502), ApiException::getCode));
|
||||||
|
|
||||||
|
verify(billing).fail(eq(ticket), eq("CALL_INTERRUPTED"), eq("failed"), anyInt());
|
||||||
|
verify(admission).close();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void asyncTaskRiskDenialPrecedesQueueAndWalletDatabaseWork() {
|
||||||
|
JdbcTemplate jdbc = mock(JdbcTemplate.class);
|
||||||
|
CatalogService catalog = mock(CatalogService.class);
|
||||||
|
GatewayGuard guard = mock(GatewayGuard.class);
|
||||||
|
UsageAuditService usage = mock(UsageAuditService.class);
|
||||||
|
var route = new java.util.LinkedHashMap<String,Object>();
|
||||||
|
route.put("id", "async-route");
|
||||||
|
route.put("providerId", "provider-mock");
|
||||||
|
route.put("type", "3d");
|
||||||
|
route.put("mode", "async");
|
||||||
|
route.put("executionReady", true);
|
||||||
|
var apiKey = Map.<String,Object>of("id", keyId.toString(), "userId", userId.toString());
|
||||||
|
when(catalog.listModels(false)).thenReturn(List.<Map<String,Object>>of(route));
|
||||||
|
when(catalog.activeModel("async-route")).thenReturn(route);
|
||||||
|
when(catalog.keyCanInvoke(apiKey, "async-route")).thenReturn(true);
|
||||||
|
when(catalog.keyOwnerId(keyId)).thenReturn(userId);
|
||||||
|
var risk = new ApiRequestRiskPolicy(ignored -> ApiRequestRiskDecision.deny(
|
||||||
|
new ApiRequestRiskSignal(ApiRequestRiskSignal.Action.DENY, "test.async-block", "RISK_BLOCKED", "Blocked", 0)));
|
||||||
|
var tasks = new AsyncTaskService(jdbc, new ObjectMapper(), catalog, 100, 4, 4, 3_600_000,
|
||||||
|
false, guard, usage, null, null, risk);
|
||||||
|
|
||||||
|
ApiException error = assertThrows(ApiException.class,
|
||||||
|
() -> tasks.createForKey(apiKey, "3d", Map.of("model", "async-route", "prompt", "generate"), ApiRequestMetadata.playground("127.0.0.1")));
|
||||||
|
|
||||||
|
assertEquals("RISK_BLOCKED", error.getCode());
|
||||||
|
verify(guard).checkRate(keyId,false);
|
||||||
|
verifyNoMoreInteractions(guard);
|
||||||
|
verifyNoInteractions(jdbc, usage);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void publicMetadataCannotBePromotedToPlaygroundByRequestHeaderAndContextStringOmitsIp() {
|
||||||
|
var publicMetadata = ApiRequestMetadata.fromHttp("198.51.100.7");
|
||||||
|
var playgroundMetadata = ApiRequestMetadata.playground("198.51.100.7");
|
||||||
|
|
||||||
|
assertEquals(ApiRequestMetadata.Source.API, publicMetadata.source());
|
||||||
|
assertEquals(ApiRequestMetadata.Source.PLAYGROUND, playgroundMetadata.source());
|
||||||
|
assertFalse(context.toString().contains("127.0.0.1"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void compositeRiskModeReportsSignalsInShadowAndAppliesThemOnlyInEnforce() {
|
||||||
|
ApiRequestRiskRule rule = new ApiRequestRiskRule() {
|
||||||
|
@Override public String id() { return "test.rule"; }
|
||||||
|
@Override public java.util.Optional<ApiRequestRiskSignal> evaluate(ApiRequestContext ignored) {
|
||||||
|
return java.util.Optional.of(new ApiRequestRiskSignal(ApiRequestRiskSignal.Action.DENY,
|
||||||
|
id(), "RISK_BLOCKED", "Blocked", 0));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
var shadow = new CompositeApiRequestRiskEngine(List.of(rule), CompositeApiRequestRiskEngine.Mode.SHADOW)
|
||||||
|
.evaluate(context);
|
||||||
|
var enforce = new CompositeApiRequestRiskEngine(List.of(rule), CompositeApiRequestRiskEngine.Mode.ENFORCE)
|
||||||
|
.evaluate(context);
|
||||||
|
|
||||||
|
assertEquals(ApiRequestRiskDecision.Action.SHADOW, shadow.action());
|
||||||
|
assertEquals(List.of("test.rule"), shadow.ruleIds());
|
||||||
|
assertEquals(ApiRequestRiskDecision.Action.DENY, enforce.action());
|
||||||
|
assertEquals("RISK_BLOCKED", enforce.code());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void settlementPersistenceFailureDoesNotMarkCompletedProviderWorkAsFailed() {
|
||||||
|
ChatBillingService billing = mock(ChatBillingService.class);
|
||||||
|
var ticket = new ChatBillingService.Ticket(UUID.randomUUID(), userId, null);
|
||||||
|
when(billing.reserve(key, model, normalized, null, context.metadata())).thenReturn(ticket);
|
||||||
|
doThrow(new org.springframework.dao.DataIntegrityViolationException("ledger unavailable"))
|
||||||
|
.when(billing).settle(eq(ticket), anyMap(), anyInt(), any());
|
||||||
|
var service = new GatewayRequestExecutionService(billing, new ApiRequestRiskPolicy(ApiRequestRiskEngine.allowAll()));
|
||||||
|
ApiRequestAdmission admission = mock(ApiRequestAdmission.class);
|
||||||
|
|
||||||
|
String result = service.execute(context, key, model, normalized, null, () -> admission,
|
||||||
|
current -> {
|
||||||
|
service.settle(current, Map.of("usage", Map.of("prompt_tokens", 1, "completion_tokens", 1)), 2, null);
|
||||||
|
return "unexpected";
|
||||||
|
}, failure -> new ApiException("CALL_INTERRUPTED", "failed", 502), ApiException::getCode);
|
||||||
|
|
||||||
|
assertEquals("SETTLEMENT_PENDING", result);
|
||||||
|
verify(billing, never()).fail(any(), anyString(), anyString(), anyInt());
|
||||||
|
verify(admission).close();
|
||||||
|
}
|
||||||
|
}
|
||||||
+25
@@ -112,6 +112,31 @@ class OpenAiCompatibleProviderHttpTest {
|
|||||||
assertEquals(List.of("Bearer route-key", "Bearer model-key"), authorization);
|
assertEquals(List.of("Bearer route-key", "Bearer model-key"), authorization);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void dynamicDatabaseChannelKeepsRotationAcrossRepeatedResolution() throws Exception {
|
||||||
|
var authorization = new ArrayList<String>();
|
||||||
|
server.createContext("/v1/chat/completions", exchange -> {
|
||||||
|
authorization.add(exchange.getRequestHeaders().getFirst("Authorization"));
|
||||||
|
exchange.getRequestBody().readAllBytes();
|
||||||
|
write(exchange, 200, "{\"usage\":{\"prompt_tokens\":1,\"completion_tokens\":1}}");
|
||||||
|
});
|
||||||
|
server.start();
|
||||||
|
var jdbc = org.mockito.Mockito.mock(JdbcTemplate.class);
|
||||||
|
var cipher = new SecretCipher("local-test-only");
|
||||||
|
org.mockito.Mockito.when(jdbc.queryForMap(org.mockito.ArgumentMatchers.anyString(), org.mockito.ArgumentMatchers.eq("dynamic-provider")))
|
||||||
|
.thenReturn(Map.of("code", "dynamic-code", "base_url", "http://127.0.0.1:" + server.getAddress().getPort() + "/v1",
|
||||||
|
"credential_ciphertext", cipher.encrypt("first-key")));
|
||||||
|
environment.withProperty("MOYI_UPSTREAM_API_KEYS_DYNAMIC_CODE", "second-key");
|
||||||
|
var dynamic = new OpenAiCompatibleProvider(json, environment, jdbc, cipher);
|
||||||
|
try {
|
||||||
|
dynamic.chat("dynamic-provider", "test-model", "route-no-override", textInput(128));
|
||||||
|
dynamic.chat("dynamic-provider", "test-model", "route-no-override", textInput(128));
|
||||||
|
dynamic.chat("dynamic-provider", "test-model", "route-no-override", textInput(128));
|
||||||
|
} finally { dynamic.close(); }
|
||||||
|
|
||||||
|
assertEquals(List.of("Bearer first-key", "Bearer second-key", "Bearer first-key"), authorization);
|
||||||
|
}
|
||||||
|
|
||||||
private Map<String, Object> textInput(int maxTokens) {
|
private Map<String, Object> textInput(int maxTokens) {
|
||||||
var input = new LinkedHashMap<String, Object>();
|
var input = new LinkedHashMap<String, Object>();
|
||||||
input.put("messages", List.of(Map.of("role", "user", "content", "Reply with OK.")));
|
input.put("messages", List.of(Map.of("role", "user", "content", "Reply with OK.")));
|
||||||
|
|||||||
+64
@@ -0,0 +1,64 @@
|
|||||||
|
package com.moyi.controlplane.provider.credentials;
|
||||||
|
|
||||||
|
import com.moyi.controlplane.common.ApiException;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.springframework.mock.env.MockEnvironment;
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
class UpstreamCredentialResolverTest {
|
||||||
|
@Test
|
||||||
|
void scopedPoolUsesRouteThenProviderModelAndNeverGlobalModelKeys() {
|
||||||
|
var env = new MockEnvironment()
|
||||||
|
.withProperty("MOYI_UPSTREAM_API_KEYS_ROUTE_ROUTE_A", "route-first | route-second | route-first")
|
||||||
|
.withProperty("MOYI_UPSTREAM_API_KEYS_MODEL_RELAY_CODE_MODEL_1", "model-first|model-second")
|
||||||
|
.withProperty("MOYI_UPSTREAM_API_KEYS_MODEL_MODEL_1", "unscoped-key");
|
||||||
|
var resolver = new UpstreamCredentialResolver(env);
|
||||||
|
|
||||||
|
assertEquals(List.of("route-first", "route-second"), resolver.scopedKeys("relay-code", "route-a", "model-1"));
|
||||||
|
assertEquals(List.of("model-first", "model-second"), resolver.scopedKeys("relay-code", "route-b", "model-1"));
|
||||||
|
assertTrue(resolver.scopedKeys("other-provider", "route-b", "model-1").isEmpty());
|
||||||
|
assertTrue(resolver.scopedKeys("relay-code", null, null).isEmpty());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void channelPoolPreservesFallbackAndExplicitAliasOrderAndDeduplicates() {
|
||||||
|
var resolver = new UpstreamCredentialResolver(new MockEnvironment()
|
||||||
|
.withProperty("MOYI_UPSTREAM_API_KEYS_PROVIDER_A", "a|shared")
|
||||||
|
.withProperty("MOYI_UPSTREAM_API_KEYS_CHANNEL_CODE", "b|shared"));
|
||||||
|
|
||||||
|
assertEquals(List.of("fallback", "a", "shared", "b"),
|
||||||
|
resolver.providerKeys(" fallback ", "provider-a", "channel-code"));
|
||||||
|
assertEquals(List.of("fallback", "b", "shared", "a"),
|
||||||
|
resolver.providerKeys("fallback", "channel-code", "provider-a"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void parsingHandlesBlankEntriesAndReturnsImmutablePools() {
|
||||||
|
assertEquals(List.of("a", "b"), UpstreamCredentialResolver.parseKeys(" a | |b|a| "));
|
||||||
|
assertTrue(UpstreamCredentialResolver.parseKeys(null).isEmpty());
|
||||||
|
assertTrue(UpstreamCredentialResolver.parseKeys(" || ").isEmpty());
|
||||||
|
assertThrows(UnsupportedOperationException.class, () -> UpstreamCredentialResolver.parseKeys("a").add("b"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void rotationUsesPersistentCallerCursorAndSurvivesIntegerOverflow() {
|
||||||
|
var cursor = new AtomicInteger(Integer.MAX_VALUE);
|
||||||
|
var keys = List.of("a", "b", "c");
|
||||||
|
|
||||||
|
assertEquals(keys.get(Math.floorMod(Integer.MAX_VALUE, keys.size())), UpstreamCredentialResolver.nextKey(keys, cursor));
|
||||||
|
assertEquals(keys.get(Math.floorMod(Integer.MIN_VALUE, keys.size())), UpstreamCredentialResolver.nextKey(keys, cursor));
|
||||||
|
assertEquals(Integer.MIN_VALUE + 1, cursor.get());
|
||||||
|
assertEquals("UPSTREAM_KEY_NOT_AVAILABLE", assertThrows(ApiException.class,
|
||||||
|
() -> UpstreamCredentialResolver.nextKey(List.of(), cursor)).getCode());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void officialFallbackKeysAreReadOnlyWhenRequestedExplicitly() {
|
||||||
|
var resolver = new UpstreamCredentialResolver(new MockEnvironment().withProperty("TRIPO_API_KEY", "tripo-a|tripo-b"));
|
||||||
|
|
||||||
|
assertTrue(resolver.providerKeys("", "provider-tripo").isEmpty());
|
||||||
|
assertEquals(List.of("tripo-a", "tripo-b"), resolver.environmentKeys("TRIPO_API_KEY"));
|
||||||
|
}
|
||||||
|
}
|
||||||
+80
@@ -0,0 +1,80 @@
|
|||||||
|
package com.moyi.controlplane.provider.transport;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import com.moyi.controlplane.common.ApiException;
|
||||||
|
import java.io.ByteArrayInputStream;
|
||||||
|
import java.io.FilterInputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.concurrent.atomic.AtomicBoolean;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
class SseEventReaderTest {
|
||||||
|
private final SseEventReader reader = new SseEventReader(new ObjectMapper());
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void fragmentedUtf8CrLfMultilineDataAndCommentsHaveOneEvent() throws Exception {
|
||||||
|
String data = ": heartbeat\r\nevent: result\r\ndata: {\"content\":\r\ndata: \"你好\"}\r\n\r\ndata: [DONE]\r\n\r\n";
|
||||||
|
var input = new FilterInputStream(bytes(data)) {
|
||||||
|
@Override public int read(byte[] value, int offset, int length) throws IOException {
|
||||||
|
return super.read(value, offset, Math.min(1, length));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
var events = new ArrayList<Map<String,Object>>();
|
||||||
|
|
||||||
|
reader.read(input, () -> false, true, events::add);
|
||||||
|
|
||||||
|
assertEquals(1, events.size());
|
||||||
|
assertEquals("你好", events.getFirst().get("content"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void chatRequiresDoneAndNativeRequiresWholeFramesEvenWithoutDone() throws Exception {
|
||||||
|
String event = "data: {\"type\":\"message_stop\"}\n\n";
|
||||||
|
|
||||||
|
assertEquals("UPSTREAM_STREAM_INCOMPLETE", assertThrows(ApiException.class,
|
||||||
|
() -> reader.read(bytes(event), () -> false, true, ignored -> {})).getCode());
|
||||||
|
assertDoesNotThrow(() -> reader.read(bytes(event), () -> false, false, ignored -> {}));
|
||||||
|
assertEquals("UPSTREAM_STREAM_INCOMPLETE", assertThrows(ApiException.class,
|
||||||
|
() -> reader.read(bytes("data: {\"type\":\"message_stop\"}"), () -> false, false, ignored -> {})).getCode());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void malformedJsonAndUtf8NeverReachConsumer() {
|
||||||
|
var delivered = new AtomicBoolean();
|
||||||
|
assertThrows(IOException.class, () -> reader.read(bytes("data: {not json}\n\n"), () -> false, true,
|
||||||
|
ignored -> delivered.set(true)));
|
||||||
|
assertThrows(IOException.class, () -> reader.read(new ByteArrayInputStream(new byte[]{(byte) 0xc3, 0x28}),
|
||||||
|
() -> false, true, ignored -> delivered.set(true)));
|
||||||
|
assertFalse(delivered.get());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void frameAndActualUtf8ByteBudgetsAreBothBounded() {
|
||||||
|
assertEquals("UPSTREAM_RESPONSE_TOO_LARGE", assertThrows(ApiException.class,
|
||||||
|
() -> reader.read(bytes("data: " + "x".repeat(262_145)), () -> false, true, ignored -> {})).getCode());
|
||||||
|
// Fewer than 2M characters, but more than 4MiB on the wire; comments cannot bypass the budget.
|
||||||
|
String multibyte = (": " + "中".repeat(1024) + "\n\n").repeat(1400);
|
||||||
|
assertEquals("UPSTREAM_RESPONSE_TOO_LARGE", assertThrows(ApiException.class,
|
||||||
|
() -> reader.read(bytes(multibyte), () -> false, true, ignored -> {})).getCode());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void transportDeadlineMapsReadFailureAndTransportRetainsStreamOwnership() throws Exception {
|
||||||
|
var closed = new AtomicBoolean();
|
||||||
|
var input = new ByteArrayInputStream("data: [DONE]\n\n".getBytes(StandardCharsets.UTF_8)) {
|
||||||
|
@Override public void close() { closed.set(true); }
|
||||||
|
};
|
||||||
|
reader.read(input, () -> false, true, ignored -> {});
|
||||||
|
assertFalse(closed.get());
|
||||||
|
assertEquals(504, assertThrows(ApiException.class,
|
||||||
|
() -> reader.read(bytes("data: {}\n\n"), () -> true, true, ignored -> {})).getStatus());
|
||||||
|
}
|
||||||
|
|
||||||
|
private ByteArrayInputStream bytes(String text) {
|
||||||
|
return new ByteArrayInputStream(text.getBytes(StandardCharsets.UTF_8));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,70 @@
|
|||||||
|
# API 请求模块化与风控扩展边界
|
||||||
|
|
||||||
|
更新日期:2026-10-21。本文对应 Java 后端的第一阶段模块化改造,不代表全部安全审计问题已解决或已上线。
|
||||||
|
|
||||||
|
## 模块分工
|
||||||
|
|
||||||
|
采用同一 Spring Boot 应用内的模块化单体,暂不拆微服务或 Maven 子工程,以保留钱包事务的一致性。
|
||||||
|
|
||||||
|
| 模块 | 负责 | 不负责 |
|
||||||
|
| --- | --- | --- |
|
||||||
|
| Gateway / Native / Playground / Task HTTP 入口 | 身份认证、Key 所属权检查、来源标记、早期 RPM 限流、协议规范化 | 风控规则的业务实现、单独复制账务流程 |
|
||||||
|
| `gateway.request` | 不可变请求上下文、风控规则与决策、同步请求执行编排、准入租约、结算持久化异常分类 | 上游凭据、HTTP/SSE 解析、异步轮询状态机 |
|
||||||
|
| `gateway.ChatMeter` / `NativeProtocolMeter` | 请求合法性、用量规范化、协议完成条件 | 选择账号、重试、修改余额 |
|
||||||
|
| `provider.credentials` | 渠道/路由/模型密钥池解析、去重、原子轮转 | 账号冷却、是否安全重试、任务凭据指纹绑定 |
|
||||||
|
| `provider.transport.SseEventReader` | SSE 解帧、严格 UTF-8、单行/事件字符上限和总响应字节上限 | 扣费、HTTP 响应写出、路由切换 |
|
||||||
|
| Provider / Failover / UpstreamHealth | 上游协议适配、HTTP 生命周期、Key 健康、授权约束下的安全换路 | 绕过风险策略、复制钱包事务 |
|
||||||
|
| Wallet / Usage | 价格快照、预留、结算、释放、持久化审计与恢复 | 估算用量后扣费、透明重放上游生成 |
|
||||||
|
| AsyncTaskService | 通过共享风险门禁后排队,worker 提交、轮询和终态事务 | 复用同步请求的短生命周期并发租约 |
|
||||||
|
|
||||||
|
同步调用顺序:认证 → 基础 RPM → 路由与协议校验 → 风控 → 并发准入 → 钱包预留 → 上游调用 → 精简用量快照 → 原子结算 → 释放准入。
|
||||||
|
|
||||||
|
异步创建顺序:认证 → 基础 RPM → 路由与输入校验 → 风控 → 数据库队列准入及价格快照/预留 → worker。异步的长任务租约继续由原有任务调度管理,不能使用同步 Controller 的生命周期。
|
||||||
|
|
||||||
|
## 风控规则接口
|
||||||
|
|
||||||
|
- 通过 Spring 注册 `ApiRequestRiskRule`,每个规则需要唯一、稳定的小写 ID。规则只接收 `ApiRequestContext`,不能读取原始 API Key、Authorization 或请求正文。
|
||||||
|
- `GATEWAY_RISK_MODE=shadow` 为默认值:规则命中或规则异常只形成影子信号,不阻断请求;`enforce` 下才会拒绝、限速或因策略异常返回 503。
|
||||||
|
- 未注册规则时不会凭空产生反欺诈能力。部署配置中写 `enforce` 也不等于已实现有效风控。
|
||||||
|
- 多规则时拒绝优先于限速,限速采用命中规则中的最长等待时间。非法模式在启动时失败,不能默默关闭安全检查。
|
||||||
|
- 原有基础频率与用户/Key/模型并发策略继续生效。不能因为增加高级风控而让非法请求绕过基础 RPM。
|
||||||
|
|
||||||
|
## 来源与隐私
|
||||||
|
|
||||||
|
- 公网入口固定标记为 `API`;只有已经验证会话和 Key 所属权的 Playground 入口使用 `PLAYGROUND`。`X-Moyi-Source` 仅保留为 Playground 的跨站请求约束之一,不作为可信来源证明。
|
||||||
|
- IP 由 Servlet 的 `getRemoteAddr()` 获取。不直接读取客户端 `X-Forwarded-For`;需要真实 IP 时,应由明确可信的反向代理/Tomcat 配置提供。
|
||||||
|
- 风控日志不写 IP、User-Agent、密钥或原始正文;上下文/元数据的默认字符串表示也不含 IP。已有用量审计可保存经脱敏的请求响应,保留策略与访问权限需要独立评估。
|
||||||
|
- 来源/IP 显式传入钱包及任务用量记录,避免仅依赖线程局部 HTTP 上下文。数据库中的 IP 属于敏感审计数据,不应出现在普通风控日志中。
|
||||||
|
|
||||||
|
## 结算与恢复
|
||||||
|
|
||||||
|
- `V44__recoverable_chat_settlement.sql` 新增归一化用量与耗时快照。快照独立于最终扣款事务提交,不保存提示词、工具参数、生成内容或凭据。
|
||||||
|
- 正常路径仍用原始响应完成审计;账本写入失败后使用精简用量恢复,不能保证重放原始生成内容。
|
||||||
|
- 已有有效快照时,账本异常返回 `SETTLEMENT_PENDING`,不能直接调用 `fail()` 释放成功调用的冻结额度。过期恢复任务逐笔事务重试,并利用行锁与原有终态检查确保只扣一次。
|
||||||
|
- 无完整结果或有效用量时仍不能估算扣费;缺少快照的过期孤儿请求沿用释放策略。
|
||||||
|
- **剩余窗口:**数据库在快照提交前不可用、或进程在收到上游结果后而快照提交前崩溃,仍可能没有可自动恢复的用量。需配合上游 request ID、对账记录和后续持久执行状态设计,不能宣称端到端 exactly-once 已实现。
|
||||||
|
- `SETTLEMENT_PENDING` 后不能用新的幂等键自动重新生成。同步既有幂等语义保持为相同用户、相同幂等键不重复调用;结果查询/重放接口尚未实现。
|
||||||
|
|
||||||
|
## 保持不变的上游约束
|
||||||
|
|
||||||
|
密钥顺序保留为路由池 → provider+model 池 → 渠道池;渠道池内保留原实现各自的 fallback/alias 顺序。轮转状态保存在客户端实例中,不随动态 DB 渠道读取重置。异步轮询继续绑定提交时 Key 的指纹,禁止随意换账号查询已有任务。
|
||||||
|
|
||||||
|
超时和发送结果不明时不透明重放;SSE 已取得后不换路。确定的请求参数错误兼容重试、明确未受理的账号拒绝及备用路由授权约束仍由原 Provider/Failover 决定。HTTP 生命周期、整体截止时间、健康冷却目前仍在 Provider 门面内,后续可继续提取为独立 Transport。
|
||||||
|
|
||||||
|
## 验证
|
||||||
|
|
||||||
|
本轮在 Docker 内使用 Java 21、Maven 3.9.9 和独立 PostgreSQL 15.18 执行 `clean verify`:157 项测试,0 失败、0 错误、0 跳过;生成 Spring Boot JAR 成功。临时数据库已清理,未执行线上发布或业务库迁移。该结果不等于真实上游、生产数据库或真实 Redis 并发的完整验收。
|
||||||
|
|
||||||
|
运行 Java 21 / Maven:`mvn -B -ntp -f control-plane-java/pom.xml verify`。
|
||||||
|
|
||||||
|
PostgreSQL 集成测试由 `MOYI_AUTH_TEST_JDBC_URL` 启用,当前测试限制为本机回环地址,并使用专用测试账号 `moyi_role_test`。必须使用独立临时数据库;测试会创建/删除随机 schema,不能指向业务或生产数据库。Docker 内运行时,`127.0.0.1` 指向容器自身;应使用与测试数据库容器共享的 network namespace,而非将宿主机回环地址直接传入普通 Maven 容器。
|
||||||
|
|
||||||
|
回归范围包括:风控 shadow/enforce/异常、所有主要调用入口拒绝时无准入和预留、早期 RPM、来源伪造、IP 显式审计、结算失败与恢复一次扣费、密钥优先级/轮转/任务指纹、SSE 分片/多行/字节上限/完成标记/超时。
|
||||||
|
|
||||||
|
## 下一阶段优先级
|
||||||
|
|
||||||
|
1. 异步创建 `Idempotency-Key`、请求哈希和数据库唯一性/锁语义,保证客户端重试不重复排队或预留。
|
||||||
|
2. 异步上游提交结果未知的持久状态与核账入口:区分明确未受理、已受理可轮询、结果未知,禁止统一退款后自动重试。
|
||||||
|
3. 执行中元数据和计量快照的崩溃窗口、重试退避、人工对账与待结算可视化。
|
||||||
|
4. HTTP Transport 生命周期继续拆分;真实 Redis 并发、租约回收与队列公平性回归。
|
||||||
|
5. 再进入用户/角色/会话、管理操作与密钥生命周期的后续安全优化;本轮模块改造不替代完整审计。
|
||||||
@@ -47,6 +47,7 @@ GATEWAY_READS_PER_MINUTE=300
|
|||||||
GATEWAY_DEFAULT_MODEL_CONCURRENCY=0
|
GATEWAY_DEFAULT_MODEL_CONCURRENCY=0
|
||||||
GATEWAY_DEFAULT_KEY_CONCURRENCY=4
|
GATEWAY_DEFAULT_KEY_CONCURRENCY=4
|
||||||
GATEWAY_DISABLE_MODEL_CONCURRENCY=false
|
GATEWAY_DISABLE_MODEL_CONCURRENCY=false
|
||||||
|
GATEWAY_RISK_MODE=shadow
|
||||||
GATEWAY_QUEUE_WAIT_MS=30000
|
GATEWAY_QUEUE_WAIT_MS=30000
|
||||||
GATEWAY_QUEUE_POLL_MS=100
|
GATEWAY_QUEUE_POLL_MS=100
|
||||||
TASK_MAX_QUEUED_PER_MODEL=100
|
TASK_MAX_QUEUED_PER_MODEL=100
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ GATEWAY_REQUESTS_PER_MINUTE=60
|
|||||||
GATEWAY_READS_PER_MINUTE=300
|
GATEWAY_READS_PER_MINUTE=300
|
||||||
GATEWAY_DEFAULT_MODEL_CONCURRENCY=4
|
GATEWAY_DEFAULT_MODEL_CONCURRENCY=4
|
||||||
GATEWAY_DEFAULT_KEY_CONCURRENCY=4
|
GATEWAY_DEFAULT_KEY_CONCURRENCY=4
|
||||||
|
GATEWAY_RISK_MODE=shadow
|
||||||
TASK_MAX_QUEUED_PER_MODEL=100
|
TASK_MAX_QUEUED_PER_MODEL=100
|
||||||
MOYI_TASK_WORKER_INTERVAL_MS=1000
|
MOYI_TASK_WORKER_INTERVAL_MS=1000
|
||||||
|
|
||||||
|
|||||||
@@ -58,6 +58,7 @@ services:
|
|||||||
# Set true for an upstream concurrency measurement run. This bypasses
|
# Set true for an upstream concurrency measurement run. This bypasses
|
||||||
# all MOYI model/group leases while retaining per-Key protection.
|
# all MOYI model/group leases while retaining per-Key protection.
|
||||||
GATEWAY_DISABLE_MODEL_CONCURRENCY: ${GATEWAY_DISABLE_MODEL_CONCURRENCY:-false}
|
GATEWAY_DISABLE_MODEL_CONCURRENCY: ${GATEWAY_DISABLE_MODEL_CONCURRENCY:-false}
|
||||||
|
GATEWAY_RISK_MODE: ${GATEWAY_RISK_MODE:-shadow}
|
||||||
GATEWAY_QUEUE_WAIT_MS: ${GATEWAY_QUEUE_WAIT_MS:-30000}
|
GATEWAY_QUEUE_WAIT_MS: ${GATEWAY_QUEUE_WAIT_MS:-30000}
|
||||||
GATEWAY_QUEUE_POLL_MS: ${GATEWAY_QUEUE_POLL_MS:-100}
|
GATEWAY_QUEUE_POLL_MS: ${GATEWAY_QUEUE_POLL_MS:-100}
|
||||||
TASK_MAX_QUEUED_PER_MODEL: ${TASK_MAX_QUEUED_PER_MODEL:-100}
|
TASK_MAX_QUEUED_PER_MODEL: ${TASK_MAX_QUEUED_PER_MODEL:-100}
|
||||||
|
|||||||
@@ -44,6 +44,7 @@ services:
|
|||||||
SPRING_DATA_REDIS_URL: redis://:${REDIS_PASSWORD}@redis:6379/0
|
SPRING_DATA_REDIS_URL: redis://:${REDIS_PASSWORD}@redis:6379/0
|
||||||
FLYWAY_ENABLED: true
|
FLYWAY_ENABLED: true
|
||||||
MOYI_FORWARD_HEADERS_STRATEGY: native
|
MOYI_FORWARD_HEADERS_STRATEGY: native
|
||||||
|
GATEWAY_RISK_MODE: ${GATEWAY_RISK_MODE:-shadow}
|
||||||
depends_on:
|
depends_on:
|
||||||
postgres:
|
postgres:
|
||||||
condition: service_healthy
|
condition: service_healthy
|
||||||
|
|||||||
Reference in new issue
Block a user