feat: extend enterprise gateway policies billing protocols and operations

This commit is contained in:
wlx committed 2026-10-13 19:08:50 +08:00
1 parent 1528b71882
commit af43947b5d
67 files changed
+1822 -69

No files matched your search

@@ -0,0 +1,92 @@
package com.moyi.controlplane.gateway;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.moyi.controlplane.catalog.CatalogService;
import com.moyi.controlplane.common.ApiException;
import com.moyi.controlplane.provider.OpenAiCompatibleProvider;
import com.moyi.controlplane.wallet.ChatBillingService;
import jakarta.servlet.http.*;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.*;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
@RestController
public class NativeGatewayController {
private final CatalogService catalog; private final GatewayGuard guard;
private final ChatBillingService billing; private final OpenAiCompatibleProvider provider; private final ObjectMapper json;
@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;}
@PostMapping({"/v1/embeddings","/v1/messages","/v1/responses"})
public ResponseEntity<Map<String,Object>> invoke(@RequestBody Map<String,Object> input,HttpServletRequest request,HttpServletResponse response) {
String protocol=request.getRequestURI().substring(request.getRequestURI().lastIndexOf('/')+1);
return execute(protocol,input,Boolean.TRUE.equals(input.get("stream")),request,response);
}
@PostMapping("/v1beta/models/{modelAction}")
public ResponseEntity<Map<String,Object>> gemini(@PathVariable String modelAction,@RequestBody Map<String,Object> input,HttpServletRequest request,HttpServletResponse response) {
int delimiter=modelAction.lastIndexOf(':');
if(delimiter<1) throw new ApiException("INVALID_MODEL","请指定模型和 generateContent 方法",400);
String action=modelAction.substring(delimiter+1);
if(!Set.of("generateContent","streamGenerateContent").contains(action)) throw new ApiException("UNSUPPORTED_METHOD","方法未接入",404);
var body=new LinkedHashMap<String,Object>(input);body.put("model",modelAction.substring(0,delimiter));
return execute("gemini",body,"streamGenerateContent".equals(action),request,response);
}
private ResponseEntity<Map<String,Object>> execute(String protocol,Map<String,Object> input,boolean streaming,HttpServletRequest request,HttpServletResponse response) {
String credential=request.getHeader("Authorization");
if(credential!=null) credential=credential.startsWith("Bearer ")?credential.substring(7):null;
else credential=request.getHeader("gemini".equals(protocol)?"x-goog-api-key":"x-api-key");
if(credential==null||credential.isBlank()) throw new ApiException("UNAUTHENTICATED","请提供 MOYI API Key",401);
var key=catalog.authenticateKey(credential);if(key==null) throw new ApiException("INVALID_API_KEY","API Key 无效或已停用",401);
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) {
UUID keyId=UUID.fromString(key.get("id").toString());guard.checkRate(keyId,false);
String requested=String.valueOf(input.getOrDefault("model",""));
var model=GatewayModelIds.resolve(catalog,key,requested);
if(model==null) throw new ApiException("MODEL_NOT_FOUND","模型不存在或未授权",404);
if(!NativeProtocolMeter.supports(model,protocol)) throw new ApiException("PROTOCOL_NOT_CONFIGURED","该模型路由尚未配置此协议,请使用模型目录标明的接口",400);
String providerId=model.get("providerId").toString(),upstream=model.get("upstreamModel").toString(),route=model.get("id").toString();
if(!provider.isConfigured(providerId,route,upstream)) throw new ApiException("PROVIDER_NOT_CONFIGURED","渠道凭据未配置",503);
var normalized=NativeProtocolMeter.request(protocol,input,json);
long start=System.currentTimeMillis();
try(var admission=guard.acquire(model,keyId,guard.queueWaitMs())) {
var ticket=billing.reserve(key,model,normalized,request.getHeader("Idempotency-Key"));
billing.endpoint(ticket.id(),"gemini".equals(protocol)?"/v1beta/models:generateContent":"/v1/"+protocol);
response.setHeader("X-Request-Id",ticket.id().toString());
try {
catalog.touchKey(key);
if(!streaming) {
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);
billing.observedUsage(ticket,protocol,result.get("gemini".equals(protocol)?"usageMetadata":"usage"));
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);
}
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);
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};
try {stream.events(chunk->{
if(first[0]==null) first[0]=elapsed(start);
collector.accept(chunk);
if(!disconnected[0]) try {event(response,chunk);} catch(IOException e) {disconnected[0]=true;}
},false);} finally {billing.observedUsage(ticket,protocol,collector.observedUsage());}
billing.settle(ticket,collector.result(),elapsed(start),first[0]);
}
return null;
} catch(Exception failure) {
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;}
response.resetBuffer();response.setContentType("application/json");throw error;
}
}
}
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 {
String type=String.valueOf(chunk.getOrDefault("type",""));
String prefix=type.matches("[a-zA-Z0-9_.-]{1,100}")?"event: "+type+"\n":"";
response.getOutputStream().write((prefix+"data: "+json.writeValueAsString(chunk)+"\n\n").getBytes(StandardCharsets.UTF_8));response.flushBuffer();
}
}