From af43947b5df8e11790f38bad7cd703bf89547b77 Mon Sep 17 00:00:00 2001 From: WangLX <519407855@qq.com> Date: Tue, 13 Oct 2026 19:08:50 +0800 Subject: [PATCH] feat: extend enterprise gateway policies billing protocols and operations --- .../controlplane/auth/AuthController.java | 6 +- .../moyi/controlplane/auth/AuthService.java | 9 ++ .../controlplane/auth/TotpController.java | 23 +++ .../moyi/controlplane/auth/TotpService.java | 79 ++++++++++ .../controlplane/catalog/CatalogService.java | 8 +- .../catalog/PriceManagementController.java | 71 +++++++++ .../moyi/controlplane/config/WebConfig.java | 2 +- .../moyi/controlplane/gateway/ChatMeter.java | 32 +++-- .../gateway/ChatStreamCollector.java | 1 + .../gateway/EnterprisePolicies.java | 86 +++++++++++ .../gateway/EnterprisePolicyController.java | 69 +++++++++ .../gateway/GatewayController.java | 18 ++- .../controlplane/gateway/GatewayFailover.java | 45 ++++++ .../controlplane/gateway/GatewayGuard.java | 40 +++++- .../gateway/NativeGatewayController.java | 92 ++++++++++++ .../gateway/NativeProtocolMeter.java | 135 ++++++++++++++++++ .../gateway/PlaygroundController.java | 5 + .../gateway/TokenPriceSchedule.java | 73 ++++++++++ .../provider/OpenAiCompatibleProvider.java | 86 +++++++++-- .../controlplane/provider/UpstreamHealth.java | 33 +++++ .../provider/UpstreamRejectedException.java | 6 + .../controlplane/task/AsyncTaskService.java | 17 ++- .../usage/OperationsController.java | 50 +++++++ .../usage/OpsNotificationService.java | 29 ++++ .../controlplane/usage/UsageAuditService.java | 8 +- .../wallet/ChatBillingService.java | 36 ++++- .../src/main/resources/application.yml | 2 + ...price_versions_and_cost_reconciliation.sql | 31 ++++ .../V36__enterprise_access_policies.sql | 24 ++++ .../db/migration/V37__totp_authentication.sql | 16 +++ .../V38__upstream_health_and_alerts.sql | 34 +++++ .../V39__actual_route_reconciliation.sql | 2 + .../V40__alert_notification_outbox.sql | 6 + .../src/main/resources/redis/acquire-slot.lua | 6 +- .../PlatformAdminPostgresTest.java | 6 +- .../controlplane/auth/AuthPostgresTest.java | 1 + .../controlplane/auth/TotpServiceTest.java | 13 ++ .../gateway/EnterprisePoliciesTest.java | 21 +++ .../gateway/EnterprisePostgresTest.java | 120 ++++++++++++++++ .../gateway/GatewayDeploymentTest.java | 21 ++- .../gateway/GatewayFailoverTest.java | 32 +++++ .../gateway/NativeProtocolMeterTest.java | 32 +++++ .../gateway/TokenPriceScheduleTest.java | 30 ++++ .../provider/NativeProviderTest.java | 40 ++++++ .../task/AsyncTaskPostgresTest.java | 21 +-- .../usage/UsageCapacityPostgresTest.java | 1 + docs/19-enterprise-gateway-increment.md | 69 +++++++++ infra/moyi/deploy.py | 4 + infra/moyi/docker-compose.cloud.yml | 3 +- infra/moyi/docker-compose.yml | 1 + infra/moyi/nginx/routes.conf | 2 +- infra/moyi/tests/test_deploy.py | 3 +- prototype/src/components/AccountSettings.vue | 2 + .../src/components/EnterpriseFinance.vue | 53 +++++++ .../src/components/EnterpriseOperations.vue | 21 +++ .../src/components/EnterprisePolicies.vue | 32 +++++ prototype/src/components/MfaSettings.vue | 28 ++++ prototype/src/components/NativeApiGuide.vue | 12 ++ prototype/src/data.ts | 4 + prototype/src/services/auth.ts | 1 + prototype/src/services/enterprise.ts | 7 + prototype/src/views/AdminConsole.vue | 8 +- prototype/src/views/AuthView.vue | 6 +- prototype/src/views/UserConsole.vue | 13 +- scripts/check_enterprise_redis.py | 68 +++++++++ scripts/package-deployment.ps1 | 2 +- scripts/test_enterprise_increment.py | 34 +++++ 67 files changed, 1822 insertions(+), 69 deletions(-) create mode 100644 control-plane-java/src/main/java/com/moyi/controlplane/auth/TotpController.java create mode 100644 control-plane-java/src/main/java/com/moyi/controlplane/auth/TotpService.java create mode 100644 control-plane-java/src/main/java/com/moyi/controlplane/catalog/PriceManagementController.java create mode 100644 control-plane-java/src/main/java/com/moyi/controlplane/gateway/EnterprisePolicies.java create mode 100644 control-plane-java/src/main/java/com/moyi/controlplane/gateway/EnterprisePolicyController.java create mode 100644 control-plane-java/src/main/java/com/moyi/controlplane/gateway/GatewayFailover.java create mode 100644 control-plane-java/src/main/java/com/moyi/controlplane/gateway/NativeGatewayController.java create mode 100644 control-plane-java/src/main/java/com/moyi/controlplane/gateway/NativeProtocolMeter.java create mode 100644 control-plane-java/src/main/java/com/moyi/controlplane/gateway/TokenPriceSchedule.java create mode 100644 control-plane-java/src/main/java/com/moyi/controlplane/provider/UpstreamHealth.java create mode 100644 control-plane-java/src/main/java/com/moyi/controlplane/provider/UpstreamRejectedException.java create mode 100644 control-plane-java/src/main/java/com/moyi/controlplane/usage/OperationsController.java create mode 100644 control-plane-java/src/main/java/com/moyi/controlplane/usage/OpsNotificationService.java create mode 100644 control-plane-java/src/main/resources/db/migration/V35__price_versions_and_cost_reconciliation.sql create mode 100644 control-plane-java/src/main/resources/db/migration/V36__enterprise_access_policies.sql create mode 100644 control-plane-java/src/main/resources/db/migration/V37__totp_authentication.sql create mode 100644 control-plane-java/src/main/resources/db/migration/V38__upstream_health_and_alerts.sql create mode 100644 control-plane-java/src/main/resources/db/migration/V39__actual_route_reconciliation.sql create mode 100644 control-plane-java/src/main/resources/db/migration/V40__alert_notification_outbox.sql create mode 100644 control-plane-java/src/test/java/com/moyi/controlplane/auth/TotpServiceTest.java create mode 100644 control-plane-java/src/test/java/com/moyi/controlplane/gateway/EnterprisePoliciesTest.java create mode 100644 control-plane-java/src/test/java/com/moyi/controlplane/gateway/EnterprisePostgresTest.java create mode 100644 control-plane-java/src/test/java/com/moyi/controlplane/gateway/GatewayFailoverTest.java create mode 100644 control-plane-java/src/test/java/com/moyi/controlplane/gateway/NativeProtocolMeterTest.java create mode 100644 control-plane-java/src/test/java/com/moyi/controlplane/gateway/TokenPriceScheduleTest.java create mode 100644 control-plane-java/src/test/java/com/moyi/controlplane/provider/NativeProviderTest.java create mode 100644 docs/19-enterprise-gateway-increment.md create mode 100644 prototype/src/components/EnterpriseFinance.vue create mode 100644 prototype/src/components/EnterpriseOperations.vue create mode 100644 prototype/src/components/EnterprisePolicies.vue create mode 100644 prototype/src/components/MfaSettings.vue create mode 100644 prototype/src/components/NativeApiGuide.vue create mode 100644 prototype/src/services/enterprise.ts create mode 100644 scripts/check_enterprise_redis.py create mode 100644 scripts/test_enterprise_increment.py diff --git a/control-plane-java/src/main/java/com/moyi/controlplane/auth/AuthController.java b/control-plane-java/src/main/java/com/moyi/controlplane/auth/AuthController.java index 285c7e9..2c07148 100644 --- a/control-plane-java/src/main/java/com/moyi/controlplane/auth/AuthController.java +++ b/control-plane-java/src/main/java/com/moyi/controlplane/auth/AuthController.java @@ -43,7 +43,9 @@ public class AuthController { this.secureCookie = secureCookie; } - public record LoginRequest(String account, String password) {} + public record LoginRequest(String account, String password,String otp) { + public LoginRequest(String account,String password) {this(account,password,null);} + } public record RegisterRequest(String invitationCode, String account, String password, String displayName, String verificationCode) {} public record CodeRequest(String account, String invitationCode) {} @@ -87,7 +89,7 @@ public class AuthController { HttpServletRequest request, HttpServletResponse response ) { - AuthService.AuthResult result = authService.login(input.account(), input.password(), meta(request)); + AuthService.AuthResult result = authService.login(input.account(), input.password(), input.otp(), meta(request)); // Keep the browser session cookie ephemeral. The server-side session still // has its normal TTL, but a closed browser must require a fresh login. response.addHeader(HttpHeaders.SET_COOKIE, sessionCookie(result.token(), null).toString()); diff --git a/control-plane-java/src/main/java/com/moyi/controlplane/auth/AuthService.java b/control-plane-java/src/main/java/com/moyi/controlplane/auth/AuthService.java index 3759702..2e09582 100644 --- a/control-plane-java/src/main/java/com/moyi/controlplane/auth/AuthService.java +++ b/control-plane-java/src/main/java/com/moyi/controlplane/auth/AuthService.java @@ -34,6 +34,7 @@ public class AuthService { private final ObjectMapper objectMapper; private final int sessionTtlHours; private final SecretCipher secretCipher; + @Autowired private TotpService totp; /** Kept for isolated unit tests that do not exercise encrypted invitation display. */ public AuthService( @@ -119,6 +120,9 @@ public class AuthService { } public AuthResult login(String accountInput, String password, RequestMeta meta) { + return login(accountInput,password,null,meta); + } + public AuthResult login(String accountInput,String password,String otp,RequestMeta meta) { UserRow user = findUserByAccount(normalizeAccount(accountInput)); if (user == null || !passwordHasher.matches(password, user.passwordHash())) { throw new ApiException("INVALID_CREDENTIALS", "账号或密码错误", 401); @@ -126,11 +130,16 @@ public class AuthService { if (!"active".equals(user.status())) { throw new ApiException("ACCOUNT_DISABLED", "账号已停用", 403); } + if(totp!=null) totp.verifyLogin(user.id(),otp); OffsetDateTime loginAt = now(); jdbc.update("UPDATE users SET last_login_at = ?, updated_at = ? WHERE id = ?", loginAt, loginAt, user.id()); audit("user.logged_in", user.id(), meta, Map.of()); return new AuthResult(createSession(user.id(), meta), toView(user, loginAt)); } + public void verifyPassword(UUID userId,String password) { + String hash=jdbc.queryForObject("SELECT password_hash FROM users WHERE id=? AND status='active'",String.class,userId); + if(password==null||!passwordHasher.matches(password,hash)) throw new ApiException("INVALID_CREDENTIALS","当前密码错误",401); + } @Transactional public AuthResult register( diff --git a/control-plane-java/src/main/java/com/moyi/controlplane/auth/TotpController.java b/control-plane-java/src/main/java/com/moyi/controlplane/auth/TotpController.java new file mode 100644 index 0000000..b17430d --- /dev/null +++ b/control-plane-java/src/main/java/com/moyi/controlplane/auth/TotpController.java @@ -0,0 +1,23 @@ +package com.moyi.controlplane.auth; +import jakarta.servlet.http.HttpServletRequest; +import java.util.Map; +import org.springframework.web.bind.annotation.*; +@RestController @RequestMapping("/api/auth/mfa") +public class TotpController { + private final AuthService auth;private final TotpService totp; + public TotpController(AuthService auth,TotpService totp) {this.auth=auth;this.totp=totp;} + public record Proof(String password,String code) {} + @GetMapping public Map status(HttpServletRequest request) {return totp.status(auth.requireUser(AuthController.sessionToken(request)).id());} + @PostMapping("/setup") public Map setup(@RequestBody Proof proof,HttpServletRequest request) { + var user=auth.requireUser(AuthController.sessionToken(request));auth.verifyPassword(user.id(),proof.password()); + var result=totp.begin(user.id(),user.account());auth.audit("user.mfa_setup_started",user.id(),AuthController.meta(request),Map.of());return result; + } + @PostMapping("/confirm") public Map confirm(@RequestBody Proof proof,HttpServletRequest request) { + var user=auth.requireUser(AuthController.sessionToken(request));auth.verifyPassword(user.id(),proof.password()); + var codes=totp.confirm(user.id(),proof.code());auth.audit("user.mfa_enabled",user.id(),AuthController.meta(request),Map.of());return Map.of("recoveryCodes",codes); + } + @PostMapping("/disable") public Map disable(@RequestBody Proof proof,HttpServletRequest request) { + var user=auth.requireUser(AuthController.sessionToken(request));auth.verifyPassword(user.id(),proof.password());totp.disable(user.id(),proof.code()); + auth.audit("user.mfa_disabled",user.id(),AuthController.meta(request),Map.of());return Map.of("enabled",false); + } +} diff --git a/control-plane-java/src/main/java/com/moyi/controlplane/auth/TotpService.java b/control-plane-java/src/main/java/com/moyi/controlplane/auth/TotpService.java new file mode 100644 index 0000000..68464b8 --- /dev/null +++ b/control-plane-java/src/main/java/com/moyi/controlplane/auth/TotpService.java @@ -0,0 +1,79 @@ +package com.moyi.controlplane.auth; + +import com.moyi.controlplane.common.ApiException; +import com.moyi.controlplane.security.SecretCipher; +import java.nio.ByteBuffer; +import java.nio.charset.StandardCharsets; +import java.security.*; +import java.time.*; +import java.util.*; +import javax.crypto.Mac; +import javax.crypto.spec.SecretKeySpec; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +@Service +public class TotpService { + private final JdbcTemplate jdbc;private final SecretCipher cipher; + private static final SecureRandom RANDOM=new SecureRandom(); + public TotpService(JdbcTemplate jdbc,SecretCipher cipher) {this.jdbc=jdbc;this.cipher=cipher;} + public Map status(UUID user) { + var rows=jdbc.queryForList("SELECT enabled FROM user_totp WHERE user_id=?",user); + return Map.of("enabled",!rows.isEmpty()&&Boolean.TRUE.equals(rows.getFirst().get("enabled")),"recoveryRemaining",jdbc.queryForObject("SELECT count(*) FROM user_totp_recovery WHERE user_id=? AND used_at IS NULL",Integer.class,user)); + } + @Transactional + public Map begin(UUID user,String account) { + jdbc.queryForList("SELECT id FROM users WHERE id=? FOR UPDATE",user); + if(Boolean.TRUE.equals(status(user).get("enabled"))) throw new ApiException("MFA_ALREADY_ENABLED","双因素认证已经启用",409); + byte[] bytes=new byte[20];RANDOM.nextBytes(bytes);String secret=base32(bytes); + if(jdbc.update("INSERT INTO user_totp(user_id,secret_ciphertext,pending_until) VALUES (?,?,now()+interval '10 minutes') ON CONFLICT(user_id) DO UPDATE SET secret_ciphertext=excluded.secret_ciphertext,pending_until=excluded.pending_until,last_step=-1,updated_at=now() WHERE user_totp.enabled=false",user,cipher.encrypt(secret))!=1) throw new ApiException("MFA_ALREADY_ENABLED","双因素认证已经启用",409); + String label=java.net.URLEncoder.encode("MOYI:"+account,StandardCharsets.UTF_8).replace("+","%20"); + return Map.of("secret",secret,"uri","otpauth://totp/"+label+"?secret="+secret+"&issuer=MOYI&algorithm=SHA1&digits=6&period=30","expiresInSeconds",600); + } + @Transactional(noRollbackFor=ApiException.class) + public List confirm(UUID user,String code) { + var row=locked(user); + if(row.isEmpty()||Boolean.TRUE.equals(row.get("enabled"))||row.get("pending_until")==null||!((java.sql.Timestamp)row.get("pending_until")).toInstant().isAfter(Instant.now())) throw new ApiException("MFA_SETUP_EXPIRED","设置已过期,请重新生成",400); + verifyRow(user,row,code,false); + jdbc.update("UPDATE user_totp SET enabled=true,pending_until=NULL,updated_at=now() WHERE user_id=?",user); + jdbc.update("DELETE FROM user_totp_recovery WHERE user_id=?",user); + var codes=new ArrayList(); + for(int i=0;i<10;i++) {byte[] random=new byte[12];RANDOM.nextBytes(random);String recovery=HexFormat.of().formatHex(random);codes.add(recovery);jdbc.update("INSERT INTO user_totp_recovery(user_id,code_hash) VALUES (?,?)",user,hash(recovery));} + return codes; + } + @Transactional(noRollbackFor=ApiException.class) + public void verifyLogin(UUID user,String code) { + var row=locked(user);if(row.isEmpty()||!Boolean.TRUE.equals(row.get("enabled"))) return; + if(code==null||code.isBlank()) throw new ApiException("MFA_REQUIRED","请输入认证器验证码或一次性恢复码",401); + verifyRow(user,row,code,true); + } + @Transactional(noRollbackFor=ApiException.class) + public void disable(UUID user,String code) { + var row=locked(user);if(row.isEmpty()||!Boolean.TRUE.equals(row.get("enabled"))) throw new ApiException("MFA_NOT_ENABLED","双因素认证未启用",400); + verifyRow(user,row,code,true); + jdbc.update("DELETE FROM user_totp_recovery WHERE user_id=?",user);jdbc.update("DELETE FROM user_totp WHERE user_id=?",user); + } + private Map locked(UUID user) {var rows=jdbc.queryForList("SELECT * FROM user_totp WHERE user_id=? FOR UPDATE",user);return rows.isEmpty()?Map.of():rows.getFirst();} + private void verifyRow(UUID user,Map row,String code,boolean recoveryAllowed) { + Instant now=Instant.now();boolean fresh=((java.sql.Timestamp)row.get("attempt_window")).toInstant().isAfter(now.minusSeconds(300)); + int attempts=fresh?((Number)row.get("attempts")).intValue():0; + if(attempts>=5) throw new ApiException("MFA_RATE_LIMITED","验证码尝试次数过多,请 5 分钟后重试",429); + jdbc.update("UPDATE user_totp SET attempts=?,attempt_window=CASE WHEN ? THEN attempt_window ELSE now() END WHERE user_id=?",attempts+1,fresh,user); + String normalized=code==null?"":code.strip(); + if(recoveryAllowed&&normalized.matches("[0-9a-fA-F]{24}")&&jdbc.update("UPDATE user_totp_recovery SET used_at=now() WHERE user_id=? AND code_hash=? AND used_at IS NULL",user,hash(normalized.toLowerCase(Locale.ROOT)))==1) {jdbc.update("UPDATE user_totp SET attempts=0 WHERE user_id=?",user);return;} + long step=now.getEpochSecond()/30,last=((Number)row.get("last_step")).longValue();String secret=cipher.decrypt(row.get("secret_ciphertext").toString()); + for(long candidate=step-1;candidate<=step+1;candidate++) { + if(candidate>last&&MessageDigest.isEqual(totp(secret,candidate).getBytes(StandardCharsets.US_ASCII),normalized.getBytes(StandardCharsets.US_ASCII))) { + jdbc.update("UPDATE user_totp SET last_step=?,attempts=0,updated_at=now() WHERE user_id=?",candidate,user);return; + } + } + throw new ApiException("INVALID_MFA_CODE","验证码无效或已使用",401); + } + static String totp(String secret,long step) { + try {Mac mac=Mac.getInstance("HmacSHA1");mac.init(new SecretKeySpec(decode32(secret),"HmacSHA1"));byte[] value=mac.doFinal(ByteBuffer.allocate(8).putLong(step).array());int offset=value[value.length-1]&15;int number=ByteBuffer.wrap(value,offset,4).getInt()&0x7fffffff;return String.format(Locale.ROOT,"%06d",number%1000000);} catch(Exception e) {throw new IllegalStateException(e);} + } + static String base32(byte[] data) {String alphabet="ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";StringBuilder result=new StringBuilder();int bits=0,buffer=0;for(byte b:data) {buffer=(buffer<<8)|(b&255);bits+=8;while(bits>=5) {bits-=5;result.append(alphabet.charAt((buffer>>bits)&31));}}if(bits>0) result.append(alphabet.charAt((buffer<<(5-bits))&31));return result.toString();} + private static byte[] decode32(String text) {var out=new java.io.ByteArrayOutputStream();int bits=0,buffer=0;for(char c:text.toCharArray()) {int n="ABCDEFGHIJKLMNOPQRSTUVWXYZ234567".indexOf(c);if(n<0) throw new IllegalArgumentException();buffer=(buffer<<5)|n;bits+=5;if(bits>=8) {bits-=8;out.write((buffer>>bits)&255);}}return out.toByteArray();} + private static String hash(String value) {try {return HexFormat.of().formatHex(MessageDigest.getInstance("SHA-256").digest(value.getBytes(StandardCharsets.UTF_8)));} catch(Exception e) {throw new IllegalStateException(e);}} +} diff --git a/control-plane-java/src/main/java/com/moyi/controlplane/catalog/CatalogService.java b/control-plane-java/src/main/java/com/moyi/controlplane/catalog/CatalogService.java index fe7806c..e08e851 100644 --- a/control-plane-java/src/main/java/com/moyi/controlplane/catalog/CatalogService.java +++ b/control-plane-java/src/main/java/com/moyi/controlplane/catalog/CatalogService.java @@ -232,6 +232,7 @@ public class CatalogService { static void validatePricing(Object input) { if (!(input instanceof Map prices)) return; + if("CNY / 1M tokens".equals(prices.get("unit"))) com.moyi.controlplane.gateway.TokenPriceSchedule.validate(prices); if (prices.get("listPriceUsd") != null && prices.get("listPriceCny") != null) throw new ApiException("INVALID_PRICE", "标准价只能选择一种币种:人民币或美元"); Object discount = prices.get("discount"); @@ -540,7 +541,12 @@ public class CatalogService { // while unsupported task types remain visibly pending. boolean asyncExecutor = asyncExecutorReady( resultSet.getString("provider_id"), providerProtocol, resultSet.getString("model_type")); - model.put("executionReady", !"async".equals(resultSet.getString("call_mode")) || asyncExecutor); + boolean syncExecutor=List.of("chat","text","code","multimodal","llm","reasoning","vision","embedding").contains(resultSet.getString("model_type")) && ("openai-compatible".equalsIgnoreCase(providerProtocol)||"provider-mock".equals(resultSet.getString("provider_id"))); + model.put("executionReady", "async".equals(resultSet.getString("call_mode")) ? asyncExecutor : syncExecutor); + var protocols=new java.util.ArrayList(); + if(syncExecutor) protocols.add("embedding".equals(resultSet.getString("model_type"))?"embeddings":"chat"); + for(String protocol:List.of("messages","responses","gemini")) if(com.moyi.controlplane.gateway.NativeProtocolMeter.supports(model,protocol)) protocols.add(protocol); + model.put("supportedProtocols",protocols); model.put("billingReady", billingReady(pricing, resultSet.getString("call_mode"), resultSet.getString("provider_id"))); // Keep the internal route UUID for permission storage and expose a // stable, readable identifier for the user console and third-party diff --git a/control-plane-java/src/main/java/com/moyi/controlplane/catalog/PriceManagementController.java b/control-plane-java/src/main/java/com/moyi/controlplane/catalog/PriceManagementController.java new file mode 100644 index 0000000..403049b --- /dev/null +++ b/control-plane-java/src/main/java/com/moyi/controlplane/catalog/PriceManagementController.java @@ -0,0 +1,71 @@ +package com.moyi.controlplane.catalog; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.moyi.controlplane.auth.*; +import com.moyi.controlplane.common.ApiException; +import com.moyi.controlplane.gateway.TokenPriceSchedule; +import jakarta.servlet.http.HttpServletRequest; +import java.math.BigDecimal; +import java.util.*; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.transaction.annotation.Transactional; +import org.springframework.web.bind.annotation.*; + +@RestController +@RequestMapping("/api/admin/finance") +public class PriceManagementController { + private final JdbcTemplate jdbc; private final AuthService auth; private final ObjectMapper json; + public PriceManagementController(JdbcTemplate jdbc,AuthService auth,ObjectMapper json) {this.jdbc=jdbc;this.auth=auth;this.json=json;} + private AuthService.UserView admin(HttpServletRequest request) {return auth.requireAdmin(AuthController.sessionToken(request));} + @PutMapping("/protocols/{modelId}") @Transactional + public Map protocols(@PathVariable String modelId,@RequestBody Map input,HttpServletRequest request) throws Exception { + var actor=admin(request); + if(!(input.get("protocols") instanceof List protocols)||protocols.stream().anyMatch(p->!List.of("messages","responses","gemini").contains(p))) throw new ApiException("INVALID_PROTOCOL","协议配置无效"); + if(jdbc.update("UPDATE model_catalog SET capabilities=jsonb_set(coalesce(capabilities,'{}'::jsonb),'{protocols}',?::jsonb),updated_at=now() WHERE id=? AND deleted_at IS NULL",json.writeValueAsString(protocols),modelId)!=1) throw new ApiException("MODEL_NOT_FOUND","模型不存在",404); + jdbc.update("UPDATE model_catalog SET capabilities=jsonb_set(capabilities,'{failover}',?::jsonb) WHERE id=?",Boolean.TRUE.equals(input.get("failover"))?"true":"false",modelId); + auth.audit("model.protocols_updated",actor.id(),AuthController.meta(request),Map.of("modelId",modelId,"protocols",protocols,"failover",Boolean.TRUE.equals(input.get("failover"))));return Map.of("saved",true); + } + @GetMapping("/prices/{modelId}") + public Map prices(@PathVariable String modelId,HttpServletRequest request) { + admin(request); + var current=jdbc.queryForList("SELECT id,public_name,pricing::text,procurement_pricing::text FROM model_catalog WHERE id=? AND deleted_at IS NULL",modelId); + if(current.isEmpty()) throw new ApiException("MODEL_NOT_FOUND","模型不存在",404); + return Map.of("current",current.getFirst(),"history",jdbc.queryForList("SELECT id,pricing::text,procurement_pricing::text,created_at FROM model_price_history WHERE model_id=? ORDER BY id DESC LIMIT 50",modelId)); + } + @PutMapping("/prices/{modelId}") @Transactional + public Map save(@PathVariable String modelId,@RequestBody Map input,HttpServletRequest request) throws Exception { + var actor=admin(request); + if(!(input.get("pricing") instanceof Map retail)) throw new ApiException("INVALID_PRICE","请填写人民币售价规则"); + CatalogService.validatePricing(retail); + Object procurement=input.get("procurementPricing"); + if(procurement!=null) { + if(!(procurement instanceof Map costs)) throw new ApiException("INVALID_PRICE","采购成本规则格式无效"); + TokenPriceSchedule.validate(costs); + } + var current=jdbc.queryForList("SELECT pricing::text,procurement_pricing::text FROM model_catalog WHERE id=? AND deleted_at IS NULL FOR UPDATE",modelId); + if(current.isEmpty()) throw new ApiException("MODEL_NOT_FOUND","模型不存在",404); + if(!input.containsKey("expectedPricing") || !input.containsKey("expectedProcurementPricing") || !Boolean.TRUE.equals(jdbc.queryForObject("SELECT pricing IS NOT DISTINCT FROM ?::jsonb AND procurement_pricing IS NOT DISTINCT FROM ?::jsonb FROM model_catalog WHERE id=?",Boolean.class,input.get("expectedPricing")==null?null:json.writeValueAsString(input.get("expectedPricing")),input.get("expectedProcurementPricing")==null?null:json.writeValueAsString(input.get("expectedProcurementPricing")),modelId))) + throw new ApiException("PRICE_CHANGED","价格已被其他管理员修改,请刷新后重试",409); + jdbc.update("UPDATE model_catalog SET pricing=?::jsonb,procurement_pricing=?::jsonb,updated_at=now() WHERE id=?",json.writeValueAsString(retail),procurement==null?null:json.writeValueAsString(procurement),modelId); + auth.audit("model.price_updated",actor.id(),AuthController.meta(request),Map.of("modelId",modelId)); + return prices(modelId,request); + } + @GetMapping("/reconciliation") + public Map reconciliation(HttpServletRequest request,@RequestParam(defaultValue="1") int page) { + admin(request); int offset=(Math.max(1,Math.min(page,100000))-1)*50; + return Map.of("records",jdbc.queryForList("SELECT ue.id,ue.created_at,m.public_name,ue.status,ue.cost_amount,ue.procurement_cost,ue.profit_amount,ue.cost_status,ue.error_code,ue.reconciliation_note,ue.actual_route_id,ue.observed_usage,b.price_version FROM usage_events ue LEFT JOIN model_catalog m ON m.id=ue.model_id LEFT JOIN chat_billing_requests b ON b.id=ue.id WHERE ue.deleted_at IS NULL ORDER BY ue.created_at DESC LIMIT 50 OFFSET ?",offset), + "summary",jdbc.queryForMap("SELECT count(*) FILTER(WHERE cost_status IN ('pending','unknown')) AS pending,count(*) FILTER(WHERE cost_status='reconciled') AS reconciled,coalesce(sum(cost_amount),0) AS revenue, sum(procurement_cost) AS known_cost,sum(profit_amount) AS known_margin FROM usage_events WHERE deleted_at IS NULL AND created_at>=date_trunc('month',now())")); + } + @PostMapping("/reconciliation/{id}") @Transactional + public Map reconcile(@PathVariable UUID id,@RequestBody Map input,HttpServletRequest request) { + var actor=admin(request); BigDecimal cost=TokenPriceSchedule.amount(input.get("procurementCost")); + String note=String.valueOf(input.getOrDefault("note","" )).trim(); + if(note.length()<3||note.length()>2000) throw new ApiException("NOTE_REQUIRED","请填写 3–2000 字的渠道账单依据"); + var rows=jdbc.queryForList("SELECT cost_status,procurement_cost FROM usage_events WHERE id=? FOR UPDATE",id); + if(rows.isEmpty()) throw new ApiException("USAGE_NOT_FOUND","记录不存在",404); + jdbc.update("UPDATE usage_events SET procurement_cost=?,profit_amount=CASE WHEN cost_amount IS NULL THEN NULL ELSE cost_amount-? END,cost_status='reconciled',reconciliation_note=?,reconciled_by=?,reconciled_at=now() WHERE id=?",cost,cost,note,actor.id(),id); + var details=new LinkedHashMap();details.put("usageId",id);details.put("before",rows.getFirst());details.put("procurementCost",cost);details.put("note",note); + auth.audit("usage.cost_reconciled",actor.id(),AuthController.meta(request),details); + return Map.of("reconciled",true); + } +} diff --git a/control-plane-java/src/main/java/com/moyi/controlplane/config/WebConfig.java b/control-plane-java/src/main/java/com/moyi/controlplane/config/WebConfig.java index bdaed03..aa4434d 100644 --- a/control-plane-java/src/main/java/com/moyi/controlplane/config/WebConfig.java +++ b/control-plane-java/src/main/java/com/moyi/controlplane/config/WebConfig.java @@ -22,7 +22,7 @@ public class WebConfig implements WebMvcConfigurer { registry.addMapping("/**") .allowedOrigins(allowedOrigins) .allowedMethods("GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS") - .allowedHeaders("Content-Type", "Authorization", "X-Moyi-Source", "Idempotency-Key") + .allowedHeaders("Content-Type", "Authorization", "X-Moyi-Source", "Idempotency-Key", "x-api-key", "anthropic-version", "x-goog-api-key", "X-Moyi-Session") .exposedHeaders("X-Request-Id", "X-Moyi-Cost-CNY") .allowCredentials(true) .maxAge(3600); diff --git a/control-plane-java/src/main/java/com/moyi/controlplane/gateway/ChatMeter.java b/control-plane-java/src/main/java/com/moyi/controlplane/gateway/ChatMeter.java index dc8a892..0584f1e 100644 --- a/control-plane-java/src/main/java/com/moyi/controlplane/gateway/ChatMeter.java +++ b/control-plane-java/src/main/java/com/moyi/controlplane/gateway/ChatMeter.java @@ -9,18 +9,22 @@ import java.util.*; /** Monetary calculations never use floating point or locally estimated completion usage. */ public final class ChatMeter { private ChatMeter() {} - public record Rates(BigDecimal input, BigDecimal output, BigDecimal cachedInput) { - public Map snapshot() { - return Map.of("input",input,"output",output,"cachedInput",cachedInput,"unit","CNY / 1M tokens"); + public record Rates(BigDecimal input, BigDecimal output, BigDecimal cachedInput, Map schedule) { + public Rates(BigDecimal input, BigDecimal output, BigDecimal cachedInput) { + this(input,output,cachedInput,Map.of("input",input,"output",output,"cachedInput",cachedInput,"unit","CNY / 1M tokens")); } + public Map snapshot() { + return schedule; + } + public BigDecimal cost(Tokens tokens) { return TokenPriceSchedule.cost(schedule,tokens); } + public BigDecimal hold(long inputTokens,long maxOutput) { return TokenPriceSchedule.hold(schedule,inputTokens,maxOutput); } public BigDecimal cost(long prompt, long completion, long cached) { - return input.multiply(BigDecimal.valueOf(prompt-cached)) - .add(cachedInput.multiply(BigDecimal.valueOf(cached))) - .add(output.multiply(BigDecimal.valueOf(completion))) - .divide(new BigDecimal("1000000"),8,RoundingMode.HALF_UP); + return cost(new Tokens(prompt,completion,cached)); } } - public record Tokens(long prompt, long completion, long cached) {} + public record Tokens(long prompt, long completion, long cached, long cacheWrite5m, long cacheWrite1h) { + public Tokens(long prompt,long completion,long cached) { this(prompt,completion,cached,0,0); } + } public record Request(Map body, long reservedInputTokens, int maxOutputTokens) {} public static Rates rates(Object value, boolean mock) { @@ -31,7 +35,7 @@ public final class ChatMeter { throw new ApiException("BILLING_UNIT_UNSUPPORTED","当前聊天接口仅支持 Token 计费;该模型应使用其对应的媒体或按次接口",503); BigDecimal input=price(prices.get("input")), output=price(prices.get("output")); BigDecimal cached=prices.get("cachedInput")==null?input:price(prices.get("cachedInput")); - return new Rates(input,output,cached); + return new Rates(input,output,cached,TokenPriceSchedule.validate(prices)); } private static BigDecimal price(Object value) { try { @@ -46,8 +50,14 @@ public final class ChatMeter { Object cached=usage.get("prompt_cache_hit_tokens"); if(usage.get("prompt_tokens_details") instanceof Map details && details.get("cached_tokens")!=null) cached=details.get("cached_tokens"); long hits=cached==null?0:count(cached); - if(hits>prompt) throw invalidUsage(); - return new Tokens(prompt,completion,hits); + long writes=usage.get("cache_creation_input_tokens")==null?0:count(usage.get("cache_creation_input_tokens")); + long hour=0; + if(usage.get("cache_creation") instanceof Map creation) { + hour=creation.get("ephemeral_1h_input_tokens")==null?0:count(creation.get("ephemeral_1h_input_tokens")); + if(creation.get("ephemeral_5m_input_tokens")!=null && count(creation.get("ephemeral_5m_input_tokens"))+hour!=writes) throw invalidUsage(); + } + if(hour>writes || hits+writes>prompt) throw invalidUsage(); + return new Tokens(prompt,completion,hits,writes-hour,hour); } private static long count(Object value) { try { diff --git a/control-plane-java/src/main/java/com/moyi/controlplane/gateway/ChatStreamCollector.java b/control-plane-java/src/main/java/com/moyi/controlplane/gateway/ChatStreamCollector.java index fee4b76..8ec2d57 100644 --- a/control-plane-java/src/main/java/com/moyi/controlplane/gateway/ChatStreamCollector.java +++ b/control-plane-java/src/main/java/com/moyi/controlplane/gateway/ChatStreamCollector.java @@ -83,6 +83,7 @@ final class ChatStreamCollector { return ""; } Integer firstTokenMs() { return firstToken; } + Object observedUsage() {return usage;} Map result() { if(finish==null) throw new ApiException("UPSTREAM_STREAM_INCOMPLETE","上游未完成输出,本次未扣费",502); if(content.toString().isBlank() && reasoning.toString().isBlank() && !hasToolOutput()) diff --git a/control-plane-java/src/main/java/com/moyi/controlplane/gateway/EnterprisePolicies.java b/control-plane-java/src/main/java/com/moyi/controlplane/gateway/EnterprisePolicies.java new file mode 100644 index 0000000..456c983 --- /dev/null +++ b/control-plane-java/src/main/java/com/moyi/controlplane/gateway/EnterprisePolicies.java @@ -0,0 +1,86 @@ +package com.moyi.controlplane.gateway; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.moyi.controlplane.common.ApiException; +import java.math.BigDecimal; +import java.net.InetAddress; +import java.time.*; +import java.util.*; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.stereotype.Service; +import org.springframework.web.context.request.RequestContextHolder; +import org.springframework.web.context.request.ServletRequestAttributes; + +/** Policy checks fail closed. Budget reservations run inside the caller's wallet transaction. */ +@Service +public class EnterprisePolicies { + private final JdbcTemplate jdbc; + private final ObjectMapper json; + public EnterprisePolicies(JdbcTemplate jdbc,ObjectMapper json) {this.jdbc=jdbc;this.json=json;} + public record UserLimit(UUID userId,int rpm,int concurrency) {} + public UserLimit taskLimit(UUID keyId) { + var row=jdbc.queryForMap("SELECT k.user_id,coalesce(p.requests_per_minute,120) rpm,coalesce(p.max_concurrency,16) concurrency FROM platform_api_keys k LEFT JOIN gateway_user_policies p ON p.user_id=k.user_id WHERE k.id=?",keyId); + return new UserLimit((UUID)row.get("user_id"),((Number)row.get("rpm")).intValue(),((Number)row.get("concurrency")).intValue()); + } + public UserLimit access(UUID keyId) { + var rows=jdbc.queryForList("SELECT k.user_id,p.expires_at,p.ip_allowlist,coalesce(u.requests_per_minute,120) rpm,coalesce(u.max_concurrency,16) concurrency FROM platform_api_keys k LEFT JOIN gateway_key_policies p ON p.key_id=k.id LEFT JOIN gateway_user_policies u ON u.user_id=k.user_id WHERE k.id=? AND k.status='active' AND k.deleted_at IS NULL",keyId); + if(rows.isEmpty()) throw new ApiException("INVALID_API_KEY","API Key 不可用",401); + var row=rows.getFirst(); + if(row.get("expires_at") instanceof java.sql.Timestamp expiry && !expiry.toInstant().isAfter(Instant.now())) throw new ApiException("API_KEY_EXPIRED","API Key 已过期",403); + if(row.get("ip_allowlist")!=null) { + try { + List ranges=json.readValue(row.get("ip_allowlist").toString(),new com.fasterxml.jackson.core.type.TypeReference<>(){}); + if(!ranges.isEmpty()) { + var context=RequestContextHolder.getRequestAttributes(); + String address=context instanceof ServletRequestAttributes servlet?servlet.getRequest().getRemoteAddr():null; + if(address==null||ranges.stream().noneMatch(range->matches(address,range))) throw new ApiException("IP_NOT_ALLOWED","此 IP 不在 API Key 白名单内",403); + } + } catch(ApiException e) {throw e;} catch(Exception e) {throw new ApiException("INVALID_KEY_POLICY","API Key 策略不可用",503);} + } + return new UserLimit((UUID)row.get("user_id"),((Number)row.get("rpm")).intValue(),((Number)row.get("concurrency")).intValue()); + } + public static boolean matches(String address,String cidr) { + try { + String[] parts=cidr.split("/",-1); + if(parts.length>2) return false; + byte[] ip=literal(address),network=literal(parts[0]); + int bits=parts.length==2?Integer.parseInt(parts[1]):network.length*8; + if(bits<0||bits>network.length*8||ip.length!=network.length) return false; + for(int i=0;i=8?255:bits<=0?0:255<<(8-bits);if((ip[i]&mask)!=(network[i]&mask)) return false;bits-=8;} + return true; + } catch(Exception e) {return false;} + } + private static byte[] literal(String address) throws Exception { + if(address==null||!address.matches("[0-9a-fA-F:.]+")||(!address.contains(":")&&!address.matches("[0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+"))) throw new IllegalArgumentException("Literal IP required"); + return InetAddress.getByName(address).getAddress(); + } + public static void validateRanges(List ranges) { + if(ranges.size()>100) throw new ApiException("INVALID_IP_RANGE","最多 100 个 IP / CIDR"); + for(Object item:ranges) if(!(item instanceof String range)||!matches(range.split("/",-1)[0],range)) throw new ApiException("INVALID_IP_RANGE","请输入有效的 IPv4 / IPv6 或 CIDR"); + } + public void reserve(String requestId,UUID userId,UUID keyId,BigDecimal amount) { + // Lock order matches wallet settlement. All keys owned by this user share one budget lock. + jdbc.queryForList("SELECT user_id FROM user_wallets WHERE user_id=? FOR UPDATE",userId); + var user=jdbc.queryForList("SELECT monthly_budget FROM gateway_user_policies WHERE user_id=?",userId); + var key=jdbc.queryForList("SELECT total_budget,daily_budget,monthly_budget FROM gateway_key_policies WHERE key_id=?",keyId); + ZonedDateTime now=ZonedDateTime.now(ZoneId.of("Asia/Shanghai")); + Instant day=now.toLocalDate().atStartOfDay(now.getZone()).toInstant(); + Instant month=now.toLocalDate().withDayOfMonth(1).atStartOfDay(now.getZone()).toInstant(); + if(!user.isEmpty()) check(user.getFirst().get("monthly_budget"),userId,null,month,amount); + if(!key.isEmpty()) { + var p=key.getFirst();check(p.get("total_budget"),userId,keyId,Instant.EPOCH,amount); + check(p.get("daily_budget"),userId,keyId,day,amount);check(p.get("monthly_budget"),userId,keyId,month,amount); + } + jdbc.update("INSERT INTO gateway_budget_holds(request_id,user_id,key_id,amount) VALUES (?,?,?,?)",requestId,userId,keyId,amount); + } + private void check(Object ceiling,UUID userId,UUID keyId,Instant since,BigDecimal amount) { + if(ceiling==null) return; + String keyClause=keyId==null?"":" AND api_key_id=?"; + var args=new ArrayList(List.of(userId,java.sql.Timestamp.from(since)));if(keyId!=null) args.add(keyId); + // Includes archived usage: archival is not a budget reset. + BigDecimal spent=jdbc.queryForObject("SELECT coalesce(sum(cost_amount),0) FROM usage_events WHERE user_id=? AND created_at>=?"+keyClause,BigDecimal.class,args.toArray()); + BigDecimal held=keyId==null?jdbc.queryForObject("SELECT coalesce(sum(amount),0) FROM gateway_budget_holds WHERE user_id=?",BigDecimal.class,userId):jdbc.queryForObject("SELECT coalesce(sum(amount),0) FROM gateway_budget_holds WHERE user_id=? AND key_id=?",BigDecimal.class,userId,keyId); + if(spent.add(held).add(amount).compareTo(new BigDecimal(ceiling.toString()))>0) throw new ApiException("BUDGET_EXCEEDED","本次调用将超过用户或 API Key 的人民币预算,请联系管理员调整额度",402); + } + public void release(String requestId) {jdbc.update("DELETE FROM gateway_budget_holds WHERE request_id=?",requestId);} +} diff --git a/control-plane-java/src/main/java/com/moyi/controlplane/gateway/EnterprisePolicyController.java b/control-plane-java/src/main/java/com/moyi/controlplane/gateway/EnterprisePolicyController.java new file mode 100644 index 0000000..485f3a7 --- /dev/null +++ b/control-plane-java/src/main/java/com/moyi/controlplane/gateway/EnterprisePolicyController.java @@ -0,0 +1,69 @@ +package com.moyi.controlplane.gateway; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.moyi.controlplane.auth.*; +import com.moyi.controlplane.common.ApiException; +import jakarta.servlet.http.HttpServletRequest; +import java.math.BigDecimal; +import java.time.OffsetDateTime; +import java.util.*; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.transaction.annotation.Transactional; +import org.springframework.web.bind.annotation.*; + +@RestController +@RequestMapping("/api") +public class EnterprisePolicyController { + private final JdbcTemplate jdbc; private final AuthService auth; private final ObjectMapper json; + public EnterprisePolicyController(JdbcTemplate jdbc,AuthService auth,ObjectMapper json) {this.jdbc=jdbc;this.auth=auth;this.json=json;} + private AuthService.UserView owner(UUID keyId,HttpServletRequest request) { + var user=auth.requireUser(AuthController.sessionToken(request)); + if(jdbc.queryForObject("SELECT count(*) FROM platform_api_keys WHERE id=? AND user_id=? AND deleted_at IS NULL",Integer.class,keyId,user.id())!=1) throw new ApiException("KEY_NOT_FOUND","API Key 不存在",404); + return user; + } + @GetMapping("/keys/{keyId}/policy") + public Map keyPolicy(@PathVariable UUID keyId,HttpServletRequest request) { + owner(keyId,request); + var rows=jdbc.queryForList("SELECT expires_at,ip_allowlist::text,total_budget,daily_budget,monthly_budget FROM gateway_key_policies WHERE key_id=?",keyId); + return rows.isEmpty()?Map.of("ip_allowlist","[]"):rows.getFirst(); + } + @PutMapping("/keys/{keyId}/policy") @Transactional + public Map keyPolicy(@PathVariable UUID keyId,@RequestBody Map input,HttpServletRequest request) throws Exception { + var user=owner(keyId,request); + var ranges=input.getOrDefault("ipAllowlist",List.of()); + if(!(ranges instanceof List list)) throw new ApiException("INVALID_IP_RANGE","IP 白名单应为列表"); + EnterprisePolicies.validateRanges(list); + OffsetDateTime expiry=null; + if(input.get("expiresAt")!=null&&!input.get("expiresAt").toString().isBlank()) { + try {expiry=OffsetDateTime.parse(input.get("expiresAt").toString());} catch(Exception e) {throw new ApiException("INVALID_EXPIRY","请输入带时区的有效截止时间");} + if(!expiry.isAfter(OffsetDateTime.now())) throw new ApiException("INVALID_EXPIRY","截止时间必须晚于当前时间"); + } + // Serialize updates with admissions; lowering below current spend intentionally blocks new calls. + jdbc.queryForList("SELECT user_id FROM user_wallets WHERE user_id=? FOR UPDATE",user.id()); + jdbc.update("INSERT INTO gateway_key_policies(key_id,expires_at,ip_allowlist,total_budget,daily_budget,monthly_budget) VALUES (?,?,?::jsonb,?,?,?) ON CONFLICT(key_id) DO UPDATE SET expires_at=excluded.expires_at,ip_allowlist=excluded.ip_allowlist,total_budget=excluded.total_budget,daily_budget=excluded.daily_budget,monthly_budget=excluded.monthly_budget,updated_at=now()",keyId,expiry,json.writeValueAsString(list),budget(input.get("totalBudget")),budget(input.get("dailyBudget")),budget(input.get("monthlyBudget"))); + auth.audit("key.policy_updated",user.id(),AuthController.meta(request),Map.of("keyId",keyId,"policy",input)); + return keyPolicy(keyId,request); + } + @GetMapping("/admin/users/{userId}/gateway-policy") + public Map userPolicy(@PathVariable UUID userId,HttpServletRequest request) { + auth.requireAdmin(AuthController.sessionToken(request)); + var rows=jdbc.queryForList("SELECT requests_per_minute,max_concurrency,monthly_budget FROM gateway_user_policies WHERE user_id=?",userId); + return rows.isEmpty()?Map.of("requests_per_minute",120,"max_concurrency",16):rows.getFirst(); + } + @PutMapping("/admin/users/{userId}/gateway-policy") @Transactional + public Map userPolicy(@PathVariable UUID userId,@RequestBody Map input,HttpServletRequest request) { + var actor=auth.requireAdmin(AuthController.sessionToken(request)); + int rpm=integer(input.get("requestsPerMinute"),100000),concurrency=integer(input.get("maxConcurrency"),10000); + if(jdbc.queryForObject("SELECT count(*) FROM users WHERE id=?",Integer.class,userId)!=1) throw new ApiException("USER_NOT_FOUND","用户不存在",404); + jdbc.update("INSERT INTO user_wallets(user_id) VALUES (?) ON CONFLICT DO NOTHING",userId); + jdbc.queryForList("SELECT user_id FROM user_wallets WHERE user_id=? FOR UPDATE",userId); + jdbc.update("INSERT INTO gateway_user_policies(user_id,requests_per_minute,max_concurrency,monthly_budget) VALUES (?,?,?,?) ON CONFLICT(user_id) DO UPDATE SET requests_per_minute=excluded.requests_per_minute,max_concurrency=excluded.max_concurrency,monthly_budget=excluded.monthly_budget,updated_at=now()",userId,rpm,concurrency,budget(input.get("monthlyBudget"))); + auth.audit("user.gateway_policy_updated",actor.id(),AuthController.meta(request),Map.of("userId",userId,"policy",input)); + return userPolicy(userId,request); + } + static BigDecimal budget(Object value) { + if(value==null||value.toString().isBlank()) return null; + try {var n=new BigDecimal(value.toString());if(n.signum()<0||n.scale()>8||n.compareTo(new BigDecimal("1000000000"))>0) throw new NumberFormatException();return n;} catch(Exception e) {throw new ApiException("INVALID_BUDGET","预算须为 0–10 亿人民币,最多 8 位小数;留空为不限");} + } + static int integer(Object value,int max) {try {int n=new BigDecimal(value.toString()).intValueExact();if(n<1||n>max) throw new NumberFormatException();return n;} catch(Exception e) {throw new ApiException("INVALID_LIMIT","限额须为 1–"+max+" 的整数");}} +} diff --git a/control-plane-java/src/main/java/com/moyi/controlplane/gateway/GatewayController.java b/control-plane-java/src/main/java/com/moyi/controlplane/gateway/GatewayController.java index b6561a7..ad0b0e4 100644 --- a/control-plane-java/src/main/java/com/moyi/controlplane/gateway/GatewayController.java +++ b/control-plane-java/src/main/java/com/moyi/controlplane/gateway/GatewayController.java @@ -28,6 +28,7 @@ public class GatewayController { private final GatewayGuard guard; private final ChatBillingService billing; private final ObjectMapper json; + @org.springframework.beans.factory.annotation.Autowired private GatewayFailover failover; public GatewayController(CatalogService catalogService, OpenAiCompatibleProvider provider, GatewayGuard guard, ChatBillingService billing, ObjectMapper json) { this.catalogService = catalogService; @@ -58,6 +59,9 @@ public class GatewayController { item.put("upstream_model", model.get("upstreamModel")); item.put("type", model.get("type")); item.put("mode", model.get("mode")); + item.put("supported_protocols", model.getOrDefault("supportedProtocols",List.of())); + item.put("execution_ready", model.getOrDefault("executionReady",false)); + item.put("billing_ready", model.getOrDefault("billingReady",false)); return item; }).toList(); catalogService.touchKey(key); @@ -82,6 +86,7 @@ public class GatewayController { Map model = resolveModel(key, modelId); if (model == null) throw new ApiException("MODEL_NOT_FOUND", "模型不存在或已下线;请使用当前 API Key 的 /v1/models 返回的模型 ID", 404); if ("async".equals(model.get("mode"))) throw new ApiException("ASYNC_MODEL", "该模型需要使用异步任务接口", 400); + if(!List.of("chat","text","code","multimodal","llm","reasoning","vision").contains(String.valueOf(model.get("type")))) throw new ApiException("WRONG_MODEL_INTERFACE","该模型需要使用模型目录标明的接口",400); String providerId=String.valueOf(model.get("providerId")); String routeId=String.valueOf(model.get("id")); boolean mock="provider-mock".equals(providerId); @@ -96,11 +101,12 @@ public class GatewayController { try { catalogService.touchKey(key); if(Boolean.TRUE.equals(request.body().get("stream"))) { - stream(model,modelId,routeId,request.body(),ticket,servletResponse,startedAt,mock); + stream(key,model,modelId,routeId,request.body(),ticket,servletResponse,startedAt,mock); return null; } - Map body=mock?mockResponse(modelId,input):provider.chat(providerId,String.valueOf(model.get("upstreamModel")),routeId,request.body()); + Map 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); + billing.observedUsage(ticket,"chat",body.get("usage")); requireCompletion(body); 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); @@ -118,7 +124,7 @@ public class GatewayController { } } - private void stream(Map model, String modelId, String routeId, Map input, ChatBillingService.Ticket ticket, HttpServletResponse response, long started, boolean mock) throws IOException { + private void stream(Map key,Map model, String modelId, String routeId, Map input, ChatBillingService.Ticket ticket, HttpServletResponse response, long started, boolean mock) throws IOException { if(mock) { var body=mockResponse(modelId,input); prepareStream(response); @@ -131,17 +137,17 @@ public class GatewayController { } // Keep admission, the concurrency slot and the upstream stream alive on this request thread. // Upstream HTTP errors are inspected before committing a 200/SSE response. - try(var upstream=provider.stream(model.get("providerId").toString(),model.get("upstreamModel").toString(),routeId,input)) { + try(var upstream=failover!=null?failover.stream(key,model,"chat",input,ticket):provider.stream(model.get("providerId").toString(),model.get("upstreamModel").toString(),routeId,input)) { var collector=new ChatStreamCollector(modelId,started); prepareStream(response); boolean[] disconnected={false}; - upstream.events(chunk -> { + try { upstream.events(chunk -> { chunk.put("model",modelId); collector.accept(chunk); // A closed browser must not turn an otherwise completed provider call into free usage. // Continue within the same hard deadline to obtain the final meter and settle once. if(!disconnected[0]) try { event(response,chunk); } catch(IOException clientClosed) { disconnected[0]=true; } - }); + }); } finally {billing.observedUsage(ticket,"chat",collector.observedUsage());} var body=collector.result(); billing.settle(ticket,body,elapsed(started),collector.firstTokenMs()); if(!disconnected[0]) done(response); diff --git a/control-plane-java/src/main/java/com/moyi/controlplane/gateway/GatewayFailover.java b/control-plane-java/src/main/java/com/moyi/controlplane/gateway/GatewayFailover.java new file mode 100644 index 0000000..170fbb3 --- /dev/null +++ b/control-plane-java/src/main/java/com/moyi/controlplane/gateway/GatewayFailover.java @@ -0,0 +1,45 @@ +package com.moyi.controlplane.gateway; +import com.moyi.controlplane.catalog.CatalogService; +import com.moyi.controlplane.provider.*; +import com.moyi.controlplane.wallet.ChatBillingService; +import java.util.*; +import org.springframework.stereotype.Service; + +@Service +public class GatewayFailover { + private final CatalogService catalog;private final GatewayGuard guard;private final ChatBillingService billing;private final OpenAiCompatibleProvider provider; + public GatewayFailover(CatalogService catalog,GatewayGuard guard,ChatBillingService billing,OpenAiCompatibleProvider provider) {this.catalog=catalog;this.guard=guard;this.billing=billing;this.provider=provider;} + public List> candidates(Map key,Map primary,String protocol) { + var routes=new ArrayList>();routes.add(primary); + if(!(primary.get("capabilities") instanceof Map cap)||!Boolean.TRUE.equals(cap.get("failover"))||primary.get("logicalModelId")==null) return routes; + GatewayModelIds.authorizedModels(catalog,key).stream() + .filter(m->!Objects.equals(m.get("id"),primary.get("id"))&&Objects.equals(m.get("logicalModelId"),primary.get("logicalModelId"))&&Objects.equals(m.get("upstreamModel"),primary.get("upstreamModel"))&&Objects.equals(m.get("type"),primary.get("type"))&&!"async".equals(m.get("mode"))) + .filter(m->"chat".equals(protocol)||NativeProtocolMeter.supports(m,protocol)) + .filter(m->provider.isConfigured(m.get("providerId").toString(),m.get("id").toString(),m.get("upstreamModel").toString())) + .sorted(Comparator.comparingInt(m->m.get("priority") instanceof Number n?n.intValue():0)).limit(2).forEach(routes::add); + return routes; + } + public Map invoke(Map key,Map model,String protocol,Map input,ChatBillingService.Ticket ticket) { + var routes=candidates(key,model,protocol); + for(int i=0;i0) billing.actualRoute(ticket,route); + try {return "chat".equals(protocol)?provider.chat(route.get("providerId").toString(),route.get("upstreamModel").toString(),route.get("id").toString(),input):provider.invoke(route.get("providerId").toString(),route.get("upstreamModel").toString(),route.get("id").toString(),input,protocol);} + catch(UpstreamRejectedException e) {if(i==routes.size()-1) throw e;} + } + } + throw new IllegalStateException("No route"); + } + public OpenAiCompatibleProvider.StreamResponse stream(Map key,Map model,String protocol,Map input,ChatBillingService.Ticket ticket) { + var routes=candidates(key,model,protocol); + for(int i=0;i0) billing.actualRoute(ticket,route); + var response="chat".equals(protocol)?provider.stream(route.get("providerId").toString(),route.get("upstreamModel").toString(),route.get("id").toString(),input):provider.nativeStream(route.get("providerId").toString(),route.get("upstreamModel").toString(),route.get("id").toString(),input,protocol); + if(slot!=null) response.onClose(slot::close);return response; + } catch(RuntimeException e) {if(slot!=null) slot.close();if(!(e instanceof UpstreamRejectedException)||i==routes.size()-1) throw e;} + } + throw new IllegalStateException("No route"); + } +} diff --git a/control-plane-java/src/main/java/com/moyi/controlplane/gateway/GatewayGuard.java b/control-plane-java/src/main/java/com/moyi/controlplane/gateway/GatewayGuard.java index 48ff5de..ac5a61e 100644 --- a/control-plane-java/src/main/java/com/moyi/controlplane/gateway/GatewayGuard.java +++ b/control-plane-java/src/main/java/com/moyi/controlplane/gateway/GatewayGuard.java @@ -42,6 +42,9 @@ public class GatewayGuard { private final long queueWaitMs; private final long queuePollMs; private final String redisPrefix; + private EnterprisePolicies policies; + @org.springframework.beans.factory.annotation.Autowired + public void setEnterprisePolicies(EnterprisePolicies policies) { this.policies=policies; } private static DefaultRedisScript script(String location) { DefaultRedisScript script = new DefaultRedisScript<>(); @@ -105,6 +108,11 @@ public class GatewayGuard { } public void checkRate(UUID keyId, boolean readOnly) { + if(policies!=null) { + var policy=policies.access(keyId); + long userRetry=execute(RATE,redisPrefix+"gateway:rate:user:"+(readOnly?"read:":"invoke:")+policy.userId(),Integer.toString(readOnly?readsPerMinute:policy.rpm()),"60000"); + if(userRetry>0) throw new GatewayLimitException("USER_RATE_LIMITED","用户请求总量超过限额,请稍后重试",429,(int)((userRetry+999)/1000)); + } String bucket = redisPrefix + "gateway:rate:" + (readOnly ? "read:" : "invoke:") + keyId; long retryMs = execute(RATE, bucket, Integer.toString(readOnly ? readsPerMinute : requestsPerMinute), "60000"); if (retryMs > 0) { @@ -150,6 +158,23 @@ public class GatewayGuard { public int defaultModelConcurrency() { return defaultModelConcurrency; } public int defaultKeyConcurrency() { return defaultKeyConcurrency; } + public boolean tryTaskUserSlot(UUID keyId,String taskId,long ttlMs) { + if(policies==null) return true; + var policy=policies.taskLimit(keyId); + String bucket=redisPrefix+"gateway:slots:user:"+policy.userId(); + boolean acquired=execute(ACQUIRE,bucket,Integer.toString(policy.concurrency()),Long.toString(Math.max(180000,ttlMs)),taskId)==1; + // Redis is outside the database transaction: roll back the lease if claiming the task fails. + if(acquired&&org.springframework.transaction.support.TransactionSynchronizationManager.isSynchronizationActive()) + org.springframework.transaction.support.TransactionSynchronizationManager.registerSynchronization(new org.springframework.transaction.support.TransactionSynchronization() {@Override public void afterCompletion(int status) {if(status!=STATUS_COMMITTED) releaseQuietly(bucket,taskId);}}); + return acquired; + } + public void releaseTaskUserSlot(UUID userId,String taskId) { + if(policies==null) return; + Runnable release=()->releaseQuietly(redisPrefix+"gateway:slots:user:"+userId,taskId); + if(org.springframework.transaction.support.TransactionSynchronizationManager.isSynchronizationActive()) + org.springframework.transaction.support.TransactionSynchronizationManager.registerSynchronization(new org.springframework.transaction.support.TransactionSynchronization() {@Override public void afterCommit() {release.run();}}); + else release.run(); + } public record Capacity(long running, long queued) {} public record CapacitySnapshot(boolean available, Map groups) {} @@ -179,6 +204,17 @@ public class GatewayGuard { /** Atomically reserves the model and API-Key slots, so queued requests do not hold one scarce slot while waiting for the other. */ public Admission acquire(Map model, UUID keyId, long requestedWaitMs) { + Admission admission=acquireModelAndKey(model,keyId,requestedWaitMs); + try { + if(policies!=null) { + var policy=policies.access(keyId); + admission.userSlot=acquireSlot(redisPrefix+"gateway:slots:user:"+policy.userId(),policy.concurrency(),"USER_BUSY","用户总并发已满,请稍后重试",-1); + } + return admission; + } catch(RuntimeException e) {admission.close();throw e;} + } + + private Admission acquireModelAndKey(Map model,UUID keyId,long requestedWaitMs) { if (!modelConcurrencyEnabled) { // Measurement mode removes only MOYI's per-model/group guard. The // independent per-platform-Key limit remains active so one client @@ -233,7 +269,7 @@ public class GatewayGuard { private Slot acquireSlot(String bucket, int limit, String code, String message, long requestedWaitMs) { if (limit <= 0) return new Slot(null, null); String token = UUID.randomUUID().toString(); - long waitMs = Math.max(0, Math.min(queueWaitMs, requestedWaitMs > 0 ? requestedWaitMs : queueWaitMs)); + long waitMs = requestedWaitMs<0?0:Math.max(0, Math.min(queueWaitMs, requestedWaitMs > 0 ? requestedWaitMs : queueWaitMs)); long deadline = System.nanoTime() + java.util.concurrent.TimeUnit.MILLISECONDS.toNanos(waitMs); while (true) { long acquired = execute(ACQUIRE, bucket, Integer.toString(limit), Long.toString(SLOT_TTL_MS), token); @@ -280,6 +316,7 @@ public class GatewayGuard { } public final class Admission implements AutoCloseable { + private Slot userSlot; private final Slot modelSlot; private final Slot keySlot; private final AtomicBoolean closed = new AtomicBoolean(); @@ -299,6 +336,7 @@ public class GatewayGuard { if (!closed.compareAndSet(false, true)) return; if (modelSlot != null) modelSlot.close(); if (keySlot != null) keySlot.close(); + if (userSlot != null) userSlot.close(); } } diff --git a/control-plane-java/src/main/java/com/moyi/controlplane/gateway/NativeGatewayController.java b/control-plane-java/src/main/java/com/moyi/controlplane/gateway/NativeGatewayController.java new file mode 100644 index 0000000..364e06f --- /dev/null +++ b/control-plane-java/src/main/java/com/moyi/controlplane/gateway/NativeGatewayController.java @@ -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> invoke(@RequestBody Map 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> gemini(@PathVariable String modelAction,@RequestBody Map 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(input);body.put("model",modelAction.substring(0,delimiter)); + return execute("gemini",body,"streamGenerateContent".equals(action),request,response); + } + private ResponseEntity> execute(String protocol,Map 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> executeForKey(String protocol,Map input,boolean streaming,Map 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 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(); + } +} diff --git a/control-plane-java/src/main/java/com/moyi/controlplane/gateway/NativeProtocolMeter.java b/control-plane-java/src/main/java/com/moyi/controlplane/gateway/NativeProtocolMeter.java new file mode 100644 index 0000000..09c098f --- /dev/null +++ b/control-plane-java/src/main/java/com/moyi/controlplane/gateway/NativeProtocolMeter.java @@ -0,0 +1,135 @@ +package com.moyi.controlplane.gateway; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.moyi.controlplane.common.ApiException; +import java.math.BigDecimal; +import java.util.*; + +/** Native payloads remain native; only usage is normalized for the shared CNY ledger. */ +public final class NativeProtocolMeter { + private NativeProtocolMeter() {} + public static boolean supports(Map model,String protocol) { + if("async".equals(model.get("mode"))) return false; + if("embeddings".equals(protocol)) return "embedding".equals(model.get("type")); + if(!(model.get("capabilities") instanceof Map capabilities)) return false; + return capabilities.get("protocols") instanceof List list && list.contains(protocol); + } + public static ChatMeter.Request request(String protocol,Map input,ObjectMapper json) { + var body=new LinkedHashMap(input); + if(body.containsKey("api_key")||body.containsKey("base_url")||body.containsKey("service_tier")) throw bad("不支持凭据、上游地址或服务档位覆盖"); + int max=0; + String maxField=switch(protocol) {case "messages"->"max_tokens";case "responses"->"max_output_tokens";default->"";}; + if("embeddings".equals(protocol)) { + Object value=body.get("input"); + if(!(value instanceof String text&&!text.isBlank()) && !(value instanceof List list&&!list.isEmpty()&&list.size()<=2048)) throw bad("Embeddings input 不能为空,批量最多 2048 项"); + if(Boolean.TRUE.equals(body.get("stream"))) throw bad("Embeddings 不支持流式输出"); + body.remove("stream"); + } else { + if(body.get("tools") instanceof List tools) { + if(tools.size()>64) throw bad("工具定义不能超过 64 个"); + for(Object tool:tools) { + var spec=map(tool);String type=String.valueOf(spec.getOrDefault("type","custom")); + if("gemini".equals(protocol)) {if(spec.isEmpty()||spec.keySet().stream().anyMatch(k->!k.equals("functionDeclarations"))) throw bad("当前仅支持本地函数工具");} + else if(!Set.of("function","custom").contains(type)) throw bad("托管工具需要独立计费配置,当前仅支持本地函数工具"); + } + } + if(body.containsKey("container")) throw bad("当前不支持跨请求复用上游容器"); + if("messages".equals(protocol)&&(!(body.get("messages") instanceof List messages)||messages.isEmpty())) throw bad("messages 不能为空"); + if("responses".equals(protocol)) { + if(body.get("previous_response_id")!=null||body.get("conversation")!=null||Boolean.TRUE.equals(body.get("background"))) throw bad("当前 Responses 接口使用完整上下文请求,不支持云端会话或后台任务"); + if(body.get("input")==null) throw bad("input 不能为空"); + body.put("store",false); + } + Map config=null; + Object requested=body.get(maxField); + if("gemini".equals(protocol)) { + if(!(body.get("contents") instanceof List contents)||contents.isEmpty()) throw bad("contents 不能为空"); + config=new LinkedHashMap<>(map(body.get("generationConfig"))); + requested=config.get("maxOutputTokens"); + if(config.get("candidateCount")!=null) {try {if(new BigDecimal(config.get("candidateCount").toString()).intValueExact()!=1) throw new IllegalArgumentException();} catch(Exception e) {throw bad("当前支持 candidateCount=1");}} + } + try {max=requested==null?4096:new BigDecimal(requested.toString()).intValueExact();if(max<1||max>32768) throw new NumberFormatException();} catch(Exception e) {throw bad("输出 Token 上限须为 1–32768");} + if(config!=null) {config.put("maxOutputTokens",max);body.put("generationConfig",config);} else body.put(maxField,max); + validateContent(body); + } + try {int bytes=json.writeValueAsBytes(body).length;if(bytes>262144) throw new ApiException("REQUEST_TOO_LARGE","请求不能超过 256 KiB",413);return new ChatMeter.Request(body,bytes+65536L,max);} catch(java.io.IOException e) {throw bad("请求无法编码");} + } + private static void validateContent(Object node) { + if(node instanceof Map map) { + String type=String.valueOf(map.get("type")); + if(Set.of("image","input_image","audio","input_audio","document","input_file","item_reference","web_search","web_search_preview","computer_20250124","code_interpreter").contains(type)||map.containsKey("fileData")||map.containsKey("inlineData")||map.containsKey("cachedContent")) throw bad("该入口当前按文本与工具 Token 计费;媒体、托管工具及上游存储引用需单独配置计费适配器"); + for(Object value:map.values()) validateContent(value); + } else if(node instanceof List list) for(Object item:list) validateContent(item); + } + @SuppressWarnings("unchecked") static Map map(Object value) {return value instanceof Map m?(Map)m:Map.of();} + static long count(Object value) {try {long n=new BigDecimal(String.valueOf(value)).longValueExact();if(n<0||n>1_000_000_000) throw new NumberFormatException();return n;} catch(Exception e) {throw new ApiException("UPSTREAM_USAGE_MISSING","上游未返回有效 Token 用量",502);}} + private static long optional(Map map,String field) {return map.get(field)==null?0:count(map.get(field));} + public static Map normalize(String protocol,Map response) { + if(response.containsKey("error")) throw new ApiException("UPSTREAM_RESPONSE_INVALID","上游返回错误",502); + var usage=new LinkedHashMap(); + Map raw=map(response.get("usage")); + switch(protocol) { + case "embeddings" -> { + if(!(response.get("data") instanceof List data)||data.isEmpty()) throw badResponse(); + for(Object item:data) { + Object vector=map(item).get("embedding"); + if(vector instanceof List values) { + if(values.isEmpty()||values.stream().anyMatch(v->!(v instanceof Number n)||!Double.isFinite(n.doubleValue()))) throw badResponse(); + } else if(vector instanceof String encoded) { + try {byte[] bytes=Base64.getDecoder().decode(encoded);if(bytes.length==0||bytes.length%4!=0) throw new IllegalArgumentException();} catch(Exception e) {throw badResponse();} + } else throw badResponse(); + } + usage.put("prompt_tokens",count(raw.get("prompt_tokens")));usage.put("completion_tokens",0); + } + case "messages" -> { + if(response.get("stop_reason")==null) throw badResponse(); + long read=optional(raw,"cache_read_input_tokens"),write=optional(raw,"cache_creation_input_tokens"); + usage.put("prompt_tokens",count(raw.get("input_tokens"))+read+write);usage.put("completion_tokens",count(raw.get("output_tokens"))); + usage.put("prompt_cache_hit_tokens",read);usage.put("cache_creation_input_tokens",write); + if(raw.get("cache_creation")!=null) usage.put("cache_creation",raw.get("cache_creation")); + } + case "responses" -> { + if(!Set.of("completed","incomplete").contains(String.valueOf(response.get("status")))) throw badResponse(); + usage.put("prompt_tokens",count(raw.get("input_tokens")));usage.put("completion_tokens",count(raw.get("output_tokens"))); + usage.put("prompt_tokens_details",map(raw.get("input_tokens_details"))); + } + case "gemini" -> { + if(!(response.get("candidates") instanceof List list)||list.isEmpty()||map(list.getLast()).get("finishReason")==null) throw badResponse(); + raw=map(response.get("usageMetadata"));usage.put("prompt_tokens",count(raw.get("promptTokenCount"))); + usage.put("completion_tokens",optional(raw,"candidatesTokenCount")+optional(raw,"thoughtsTokenCount"));usage.put("prompt_cache_hit_tokens",optional(raw,"cachedContentTokenCount")); + } + default -> throw bad("未知协议"); + } + var result=new LinkedHashMap(response);result.put("usage",usage);ChatMeter.tokens(result);return result; + } + public static final class Collector { + private final String protocol; + private final Map result=new LinkedHashMap<>(),usage=new LinkedHashMap<>(); + private final List content=new ArrayList<>(); + private boolean complete; + public Collector(String protocol) {this.protocol=protocol;} + public void accept(Map chunk) { + String type=String.valueOf(chunk.get("type")); + if("messages".equals(protocol)) { + if("message_start".equals(type)) {result.putAll(map(chunk.get("message")));usage.putAll(map(result.get("usage")));} + if("message_delta".equals(type)) {result.putAll(map(chunk.get("delta")));usage.putAll(map(chunk.get("usage")));} + if(type.startsWith("content_block")) content.add(chunk); + if("message_stop".equals(type)) complete=true; + result.put("usage",usage);result.put("content",content); + } else if("responses".equals(protocol)) { + if(Set.of("response.completed","response.incomplete").contains(type)) {result.putAll(map(chunk.get("response")));complete=true;} + if(Set.of("response.failed","error").contains(type)) throw badResponse(); + } else { + content.add(chunk);result.put("chunks",content); + if(chunk.containsKey("usageMetadata")) result.put("usageMetadata",chunk.get("usageMetadata")); + if(chunk.get("candidates") instanceof List candidates&&!candidates.isEmpty()) { + if(candidates.stream().anyMatch(c->map(c).get("finishReason")!=null)) {result.put("candidates",candidates);complete=true;} + } + } + } + public Map result() {if(!complete) throw badResponse();return normalize(protocol,result);} + public Object observedUsage() {return "gemini".equals(protocol)?result.get("usageMetadata"):result.get("usage");} + } + private static ApiException bad(String message) {return new ApiException("UNSUPPORTED_REQUEST",message,400);} + private static ApiException badResponse() {return new ApiException("UPSTREAM_RESPONSE_INCOMPLETE","上游响应未完成,待核对渠道用量",502);} +} diff --git a/control-plane-java/src/main/java/com/moyi/controlplane/gateway/PlaygroundController.java b/control-plane-java/src/main/java/com/moyi/controlplane/gateway/PlaygroundController.java index c8d384f..7a2e1a1 100644 --- a/control-plane-java/src/main/java/com/moyi/controlplane/gateway/PlaygroundController.java +++ b/control-plane-java/src/main/java/com/moyi/controlplane/gateway/PlaygroundController.java @@ -18,6 +18,11 @@ public class PlaygroundController { private final CatalogService catalog; private final GatewayController gateway; private final AsyncTaskService tasks; + @org.springframework.beans.factory.annotation.Autowired private NativeGatewayController nativeGateway; + @PostMapping(value="/embeddings",consumes="application/json") + public ResponseEntity> embeddings(@PathVariable String keyId,@RequestBody Map input,HttpServletRequest request,HttpServletResponse response) { + return nativeGateway.executeForKey("embeddings",input,false,requireKey(request,keyId),request,response); + } public PlaygroundController(AuthService auth, CatalogService catalog, GatewayController gateway, AsyncTaskService tasks) { this.auth = auth; diff --git a/control-plane-java/src/main/java/com/moyi/controlplane/gateway/TokenPriceSchedule.java b/control-plane-java/src/main/java/com/moyi/controlplane/gateway/TokenPriceSchedule.java new file mode 100644 index 0000000..1368ee3 --- /dev/null +++ b/control-plane-java/src/main/java/com/moyi/controlplane/gateway/TokenPriceSchedule.java @@ -0,0 +1,73 @@ +package com.moyi.controlplane.gateway; + +import com.moyi.controlplane.common.ApiException; +import java.math.BigDecimal; +import java.math.RoundingMode; +import java.util.*; + +/** A price schedule contains CNY per million tokens only, frozen when admitted. */ +public final class TokenPriceSchedule { + private TokenPriceSchedule() {} + public static final List FIELDS=List.of("input","output","cachedInput","cacheWrite5m","cacheWrite1h"); + public static BigDecimal amount(Object value) { + try { + BigDecimal result=new BigDecimal(String.valueOf(value)); + if(result.signum()<0 || result.scale()>8 || result.compareTo(new BigDecimal("1000000"))>0) throw new NumberFormatException(); + return result; + } catch(Exception e) { throw new ApiException("INVALID_PRICE","Token 单价须为非负人民币金额,最多 8 位小数,且不超过 100 万元"); } + } + public static Map validate(Map raw) { + if(!"CNY / 1M tokens".equals(raw.get("unit"))) throw new ApiException("INVALID_PRICE","Token 规则必须使用 CNY / 1M tokens"); + var copy=new LinkedHashMap(); copy.put("unit","CNY / 1M tokens"); + for(String field:FIELDS) if(raw.get(field)!=null) copy.put(field,amount(raw.get(field))); + if(!copy.containsKey("input") || !copy.containsKey("output")) throw new ApiException("INVALID_PRICE","输入和输出单价不能为空"); + if(raw.get("tiers")!=null) { + if(!(raw.get("tiers") instanceof List tiers) || tiers.size()>20) throw new ApiException("INVALID_PRICE","上下文阶梯最多 20 档"); + var frozen=new ArrayList>(); long last=0; + for(Object item:tiers) { + if(!(item instanceof Map tier)) throw new ApiException("INVALID_PRICE","阶梯格式无效"); + long threshold; + try { threshold=new BigDecimal(String.valueOf(tier.get("aboveInputTokens"))).longValueExact(); } + catch(Exception e) { throw new ApiException("INVALID_PRICE","阶梯起点必须是整数 Token 数"); } + if(threshold<=last || threshold>10_000_000) throw new ApiException("INVALID_PRICE","阶梯起点须大于 0、严格递增且不超过 1000 万 Token"); + var next=new LinkedHashMap(); next.put("aboveInputTokens",threshold); + for(String field:FIELDS) if(tier.get(field)!=null) next.put(field,amount(tier.get(field))); + if(next.size()==1) throw new ApiException("INVALID_PRICE","每档至少填写一种单价"); + frozen.add(Collections.unmodifiableMap(next)); last=threshold; + } + copy.put("tiers",List.copyOf(frozen)); + } + return Collections.unmodifiableMap(copy); + } + public static Map at(Map schedule,long prompt) { + var result=new LinkedHashMap<>(schedule); + if(schedule.get("tiers") instanceof List tiers) for(Object raw:tiers) { + var tier=(Map)raw; + if(prompt>((Number)tier.get("aboveInputTokens")).longValue()) for(String field:FIELDS) if(tier.containsKey(field)) result.put(field,tier.get(field)); + } + return result; + } + public static BigDecimal cost(Map schedule,ChatMeter.Tokens usage) { + var rates=at(schedule,usage.prompt()); + long ordinary=usage.prompt()-usage.cached()-usage.cacheWrite5m()-usage.cacheWrite1h(); + if(ordinary<0) throw new ApiException("UPSTREAM_USAGE_INVALID","缓存用量大于输入总用量",502); + BigDecimal input=amount(rates.get("input")); + return input.multiply(BigDecimal.valueOf(ordinary)) + .add(amount(rates.getOrDefault("output",0)).multiply(BigDecimal.valueOf(usage.completion()))) + .add(amount(rates.getOrDefault("cachedInput",input)).multiply(BigDecimal.valueOf(usage.cached()))) + .add(amount(rates.getOrDefault("cacheWrite5m",input)).multiply(BigDecimal.valueOf(usage.cacheWrite5m()))) + .add(amount(rates.getOrDefault("cacheWrite1h",input)).multiply(BigDecimal.valueOf(usage.cacheWrite1h()))) + .divide(new BigDecimal("1000000"),8,RoundingMode.HALF_UP); + } + /** A hold covers the highest tier and cache category; exact rates settle later. */ + public static BigDecimal hold(Map schedule,long inputTokens,long maxOutput) { + BigDecimal in=BigDecimal.ZERO,out=BigDecimal.ZERO; + var all=new ArrayList>(); all.add(schedule); + if(schedule.get("tiers") instanceof List tiers) for(Object tier:tiers) all.add((Map)tier); + for(var row:all) for(String field:FIELDS) if(row.get(field)!=null) { + if(field.equals("output")) out=out.max(amount(row.get(field))); else in=in.max(amount(row.get(field))); + } + return in.multiply(BigDecimal.valueOf(inputTokens)).add(out.multiply(BigDecimal.valueOf(maxOutput))) + .divide(new BigDecimal("1000000"),8,RoundingMode.CEILING); + } +} diff --git a/control-plane-java/src/main/java/com/moyi/controlplane/provider/OpenAiCompatibleProvider.java b/control-plane-java/src/main/java/com/moyi/controlplane/provider/OpenAiCompatibleProvider.java index 7006109..1f45988 100644 --- a/control-plane-java/src/main/java/com/moyi/controlplane/provider/OpenAiCompatibleProvider.java +++ b/control-plane-java/src/main/java/com/moyi/controlplane/provider/OpenAiCompatibleProvider.java @@ -40,6 +40,7 @@ public class OpenAiCompatibleProvider { private final ConcurrentMap keyCursors = new ConcurrentHashMap<>(); private final ScheduledExecutorService deadlines = Executors.newSingleThreadScheduledExecutor(r -> { var t=new Thread(r,"upstream-deadlines"); t.setDaemon(true); return t; }); private final int timeoutSeconds; + @org.springframework.beans.factory.annotation.Autowired private UpstreamHealth health; public OpenAiCompatibleProvider( ObjectMapper objectMapper, @@ -99,47 +100,100 @@ public class OpenAiCompatibleProvider { } private StreamResponse open(String providerId, String upstreamModel, String routeId, Map input, boolean streaming) { + return openEndpoint(providerId,upstreamModel,routeId,input,streaming,"chat"); + } + + public Map invoke(String providerId,String model,String routeId,Map input,String protocol) { + try(var response=openEndpoint(providerId,model,routeId,input,false,protocol)) { + byte[] bytes=response.body.readNBytes(4*1024*1024+1); + if(bytes.length>4*1024*1024) throw new ApiException("UPSTREAM_RESPONSE_TOO_LARGE","上游响应超过限制",502); + Map result=objectMapper.readValue(bytes,new com.fasterxml.jackson.core.type.TypeReference<>(){}); + if(result.containsKey("error")) throw classifyUpstreamError(200,writeJson(result)); + return result; + } catch(ApiException e) {throw e;} catch(Exception e) {throw new ApiException("UPSTREAM_PROVIDER_ERROR","上游响应无法读取",502);} + } + public StreamResponse nativeStream(String providerId,String model,String routeId,Map input,String protocol) { + return openEndpoint(providerId,model,routeId,input,true,protocol); + } + private StreamResponse openEndpoint(String providerId,String upstreamModel,String routeId,Map input,boolean streaming,String protocol) { ProviderConfig provider = resolve(providerId); if (provider == null || !provider.hasKey(routeId, upstreamModel, environment)) { throw new ApiException("PROVIDER_NOT_CONFIGURED", "该模型的供应商尚未配置服务端 API Key", 503); } Map body = new LinkedHashMap<>(input); body.put("model", upstreamModel); - body.put("stream", streaming); - if(streaming) body.put("stream_options",Map.of("include_usage",true)); + if(!List.of("embeddings","gemini").contains(protocol)) body.put("stream", streaming); + if("chat".equals(protocol)&&streaming) body.put("stream_options",Map.of("include_usage",true)); else body.remove("stream_options"); + String base=normalizeUrl(provider.baseUrl()); + String path=switch(protocol) { + case "chat" -> "/chat/completions"; + case "messages" -> "/messages"; + case "responses" -> "/responses"; + case "embeddings" -> "/embeddings"; + case "gemini" -> "/models/"+java.net.URLEncoder.encode(upstreamModel,StandardCharsets.UTF_8).replace("+","%20")+(streaming?":streamGenerateContent?alt=sse":":generateContent"); + default -> throw new ApiException("UNSUPPORTED_PROTOCOL","协议不支持",400); + }; + if("gemini".equals(protocol)) {base=base.replaceFirst("/v1$","");if(!base.endsWith("/v1beta")) base+="/v1beta";body.remove("model");body.remove("stream");} String apiKey = provider.nextApiKey(routeId, upstreamModel, environment, keyCursors); - for (int attempt = 0; attempt < 2; attempt++) { + List candidates=provider.routeKeys(routeId,upstreamModel,environment); + if(candidates.isEmpty()) candidates=provider.apiKeys(); + String healthRoute=routeId==null?providerId:routeId; + var attempted=new java.util.HashSet(); + if(health!=null) { + apiKey=health.select(healthRoute,candidates,attempted,Math.max(0,candidates.indexOf(apiKey))); + if(apiKey==null) throw new UpstreamRejectedException(new ApiException("UPSTREAM_ACCOUNTS_COOLING","渠道账号暂时隔离,请稍后重试",503)); + } + boolean compatibilityRetried=false; + long overallStarted=System.nanoTime(); + if(org.springframework.web.context.request.RequestContextHolder.getRequestAttributes() instanceof org.springframework.web.context.request.ServletRequestAttributes attrs) { + Object previous=attrs.getRequest().getAttribute("moyi.upstreamStarted"); + if(previous instanceof Long value) overallStarted=value;else attrs.getRequest().setAttribute("moyi.upstreamStarted",overallStarted); + } + for (int attempt = 0; attempt < 4; attempt++) { + long remaining=timeoutSeconds*1000L-TimeUnit.NANOSECONDS.toMillis(System.nanoTime()-overallStarted); + if(remaining<=0) throw new ApiException("UPSTREAM_PROVIDER_TIMEOUT","上游请求超时",504); String json = writeJson(body); - HttpRequest request = HttpRequest.newBuilder() - .uri(URI.create(normalizeUrl(provider.baseUrl()) + "/chat/completions")) - .timeout(Duration.ofSeconds(timeoutSeconds)) + HttpRequest.Builder builder = HttpRequest.newBuilder() + .uri(URI.create(base + path)) + .timeout(Duration.ofMillis(remaining)) .header("Authorization", "Bearer " + apiKey) .header("Content-Type", "application/json") - .POST(HttpRequest.BodyPublishers.ofString(json)) - .build(); + .POST(HttpRequest.BodyPublishers.ofString(json)); + if("messages".equals(protocol)) builder.header("x-api-key",apiKey).header("anthropic-version","2023-06-01"); + if("gemini".equals(protocol)) builder.header("x-goog-api-key",apiKey); + HttpRequest request=builder.build(); try { long started=System.nanoTime(); HttpResponse response = httpClient.send(request, HttpResponse.BodyHandlers.ofInputStream()); + if(health!=null) health.record(healthRoute,apiKey,response.statusCode(),(int)TimeUnit.NANOSECONDS.toMillis(System.nanoTime()-started)); if (response.statusCode() < 200 || response.statusCode() >= 300) { - String details = readErrorBody(response.body()); + String details; + try(var bounded=new StreamResponse(response.body(),Math.max(1,timeoutSeconds*1000L-TimeUnit.NANOSECONDS.toMillis(System.nanoTime()-overallStarted)))) {details=readErrorBody(bounded.body);} if (requiresLargerOutputBudget(details)) { throw new ApiException("INVALID_MAX_TOKENS", "上游模型要求 max_tokens 大于 1024,请提高输出 Token 上限后重试", 400); } // Some OpenAI-compatible relays use the newer spelling for // reasoning-model budgets. Retry only this deterministic // GPT request-shape error; other failures remain bill-safe. - if (attempt == 0 && prepareCompatibilityRetry(body, details)) continue; - throw classifyUpstreamError(response.statusCode(), details); + if ("chat".equals(protocol) && !compatibilityRetried && prepareCompatibilityRetry(body, details)) {compatibilityRetried=true;continue;} + if(health!=null && List.of(401,403,429).contains(response.statusCode()) && attempt<3 && TimeUnit.NANOSECONDS.toSeconds(System.nanoTime()-overallStarted) deadline; + private Runnable afterClose=()->{}; + public StreamResponse onClose(Runnable callback) {afterClose=callback;return this;} private StreamResponse(InputStream body,long remainingMs) { this.body=body; this.deadline=deadlines.schedule(() -> { expired.set(true); try { body.close(); } catch(IOException ignored) {} },remainingMs,TimeUnit.MILLISECONDS); } public void events(EventConsumer consumer) throws IOException { + events(consumer,true); + } + public void events(EventConsumer consumer,boolean requireDone) throws IOException { var reader=new BufferedReader(new InputStreamReader(body,StandardCharsets.UTF_8)); StringBuilder event=new StringBuilder(), line=new StringBuilder(); long total=0; @@ -210,9 +269,10 @@ public class OpenAiCompatibleProvider { 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) {} } + @Override public void close() { deadline.cancel(false); try { body.close(); } catch(IOException ignored) {} finally {afterClose.run();afterClose=()->{};} } } @FunctionalInterface public interface EventConsumer { void accept(Map chunk) throws IOException; } @PreDestroy public void close() { deadlines.shutdownNow(); } diff --git a/control-plane-java/src/main/java/com/moyi/controlplane/provider/UpstreamHealth.java b/control-plane-java/src/main/java/com/moyi/controlplane/provider/UpstreamHealth.java new file mode 100644 index 0000000..a0157b7 --- /dev/null +++ b/control-plane-java/src/main/java/com/moyi/controlplane/provider/UpstreamHealth.java @@ -0,0 +1,33 @@ +package com.moyi.controlplane.provider; +import java.nio.charset.StandardCharsets; +import java.security.MessageDigest; +import java.util.*; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.stereotype.Service; +import org.springframework.web.context.request.*; +@Service +public class UpstreamHealth { + private final JdbcTemplate jdbc; + public UpstreamHealth(JdbcTemplate jdbc) {this.jdbc=jdbc;} + public static String fingerprint(String key) {try {return HexFormat.of().formatHex(MessageDigest.getInstance("SHA-256").digest(key.getBytes(StandardCharsets.UTF_8)));} catch(Exception e) {throw new IllegalStateException(e);}} + public String select(String route,List candidates,Set attempted,int cursor) { + var cooling=jdbc.queryForList("SELECT account_fingerprint FROM upstream_account_health WHERE route_id=? AND cooldown_until>now()",String.class,route); + var available=candidates.stream().filter(k->!attempted.contains(k)&&!cooling.contains(fingerprint(k))).toList(); + if(available.isEmpty()) return null; + String affinity=null; + if(RequestContextHolder.getRequestAttributes() instanceof ServletRequestAttributes attrs) { + var request=attrs.getRequest();String session=request.getHeader("X-Moyi-Session"); + if(session!=null&&!session.isBlank()&&session.length()<=128) affinity=String.valueOf(request.getHeader("Authorization"))+"|"+String.valueOf(request.getHeader("x-api-key"))+"|"+String.valueOf(request.getHeader("x-goog-api-key"))+"|"+session; + } + if(affinity!=null) { + String identity=affinity; + // Rendezvous hashing preserves existing bindings when another account is cooled down. + return available.stream().max(Comparator.comparing(k->fingerprint(identity+"|"+route+"|"+fingerprint(k)))).orElseThrow(); + } + return available.get(Math.floorMod(cursor,available.size())); + } + public void record(String route,String key,int status,int latency) { + boolean failed=status>=400;int seconds=status==401||status==403?300:status==429?60:failed?15:0; + jdbc.update("INSERT INTO upstream_account_health(route_id,account_fingerprint,failures,last_status,latency_ms,cooldown_until) VALUES (?,?,?,?,?,CASE WHEN ?>0 THEN now()+make_interval(secs=>?) ELSE NULL END) ON CONFLICT(route_id,account_fingerprint) DO UPDATE SET failures=CASE WHEN ? THEN upstream_account_health.failures+1 ELSE 0 END,last_status=excluded.last_status,latency_ms=CASE WHEN upstream_account_health.latency_ms IS NULL THEN excluded.latency_ms ELSE round(upstream_account_health.latency_ms*0.8+excluded.latency_ms*0.2) END,cooldown_until=excluded.cooldown_until,updated_at=now()",route,fingerprint(key),failed?1:0,status,latency,seconds,seconds,failed); + } +} diff --git a/control-plane-java/src/main/java/com/moyi/controlplane/provider/UpstreamRejectedException.java b/control-plane-java/src/main/java/com/moyi/controlplane/provider/UpstreamRejectedException.java new file mode 100644 index 0000000..62644d9 --- /dev/null +++ b/control-plane-java/src/main/java/com/moyi/controlplane/provider/UpstreamRejectedException.java @@ -0,0 +1,6 @@ +package com.moyi.controlplane.provider; +import com.moyi.controlplane.common.ApiException; +/** Only emitted for HTTP rejections known to occur before generation. */ +public class UpstreamRejectedException extends ApiException { + public UpstreamRejectedException(ApiException cause) {super(cause.getCode(),cause.getMessage(),cause.getStatus());} +} diff --git a/control-plane-java/src/main/java/com/moyi/controlplane/task/AsyncTaskService.java b/control-plane-java/src/main/java/com/moyi/controlplane/task/AsyncTaskService.java index da87906..6f4e888 100644 --- a/control-plane-java/src/main/java/com/moyi/controlplane/task/AsyncTaskService.java +++ b/control-plane-java/src/main/java/com/moyi/controlplane/task/AsyncTaskService.java @@ -42,6 +42,7 @@ public class AsyncTaskService { private final UsageAuditService usage; private final AsyncProviderClient providerClient; private final TransactionTemplate transactions; + private final com.moyi.controlplane.gateway.EnterprisePolicies policies; /** Compatibility constructor for isolated gateway tests that only exercise mock tasks. */ public AsyncTaskService(JdbcTemplate jdbc, ObjectMapper objectMapper, CatalogService catalogService, @@ -92,6 +93,7 @@ public class AsyncTaskService { this.usage = usage; this.providerClient = providerClient; this.transactions = transactionManager == null ? null : new TransactionTemplate(transactionManager); + this.policies = new com.moyi.controlplane.gateway.EnterprisePolicies(jdbc,objectMapper); } @Transactional @@ -130,6 +132,7 @@ public class AsyncTaskService { // tests can complete them without an upstream call. String providerTaskId = "provider-mock".equals(model.get("providerId")) ? "mock" : null; String id = "task_" + UUID.randomUUID().toString().replace("-", "").substring(0, 24); + policies.reserve(id,userId,apiKeyId,reservedAmount); OffsetDateTime now = now(); try { jdbc.update( @@ -150,6 +153,7 @@ public class AsyncTaskService { } public Map getForKey(Map key, String taskId) { + gatewayGuard.checkRate(UUID.fromString(key.get("id").toString()),true); UUID userId = catalogService.keyOwnerId(UUID.fromString(String.valueOf(key.get("id")))); try { return jdbc.queryForObject("SELECT * FROM model_tasks WHERE id = ? AND user_id = ?", this::mapTask, taskId, userId); @@ -160,6 +164,7 @@ public class AsyncTaskService { public List> list(String authorization, int limit) { Map key = requireKey(authorization); + gatewayGuard.checkRate(UUID.fromString(key.get("id").toString()),true); UUID userId = catalogService.keyOwnerId(UUID.fromString(String.valueOf(key.get("id")))); int boundedLimit = Math.max(1, Math.min(limit, 100)); return jdbc.query("SELECT * FROM model_tasks WHERE user_id = ? ORDER BY created_at DESC LIMIT ?", this::mapTask, userId, boundedLimit); @@ -234,7 +239,7 @@ public class AsyncTaskService { // workers reliably. jdbc.queryForList("SELECT pg_advisory_xact_lock(hashtextextended('moyi:async-dispatch', 0))"); String sql = """ - SELECT t.id, t.api_key_id, t.model_id, m.provider_id, m.upstream_model, t.task_type, t.prompt, t.input + SELECT t.id, t.user_id, t.api_key_id, t.model_id, m.provider_id, m.upstream_model, t.task_type, t.prompt, t.input FROM model_tasks t JOIN model_catalog m ON m.id=t.model_id JOIN model_providers p ON p.id=m.provider_id @@ -242,6 +247,7 @@ public class AsyncTaskService { AND t.provider_task_id IS NULL AND m.status='active' AND m.deleted_at IS NULL AND p.status='active' AND p.deleted_at IS NULL + AND (SELECT count(*) FROM model_tasks ru WHERE ru.user_id=t.user_id AND ru.status='running') < coalesce((SELECT max_concurrency FROM gateway_user_policies up WHERE up.user_id=t.user_id),16) AND ( ? <= 0 OR COALESCE(m.max_concurrency, ?) <= 0 @@ -264,7 +270,9 @@ public class AsyncTaskService { defaultKeyConcurrency, defaultKeyConcurrency); if (rows.isEmpty()) return null; Map row = rows.getFirst(); - jdbc.update("UPDATE model_tasks SET status='running', updated_at=now() WHERE id=? AND status='queued'", row.get("id")); + if(!gatewayGuard.tryTaskUserSlot((UUID)row.get("api_key_id"),row.get("id").toString(),providerTimeoutMs+180000)) return null; + try {jdbc.update("UPDATE model_tasks SET status='running', updated_at=now() WHERE id=? AND status='queued'", row.get("id"));} + catch(RuntimeException e) {gatewayGuard.releaseTaskUserSlot((UUID)row.get("user_id"),row.get("id").toString());throw e;} return row; }); } @@ -501,7 +509,6 @@ public class AsyncTaskService { } private void reserveWallet(UUID userId, BigDecimal amount) { - if (amount.signum() == 0) return; jdbc.update("INSERT INTO user_wallets(user_id) VALUES (?) ON CONFLICT DO NOTHING", userId); Map wallet = jdbc.queryForMap("SELECT balance,reserved_balance FROM user_wallets WHERE user_id=? FOR UPDATE", userId); BigDecimal available = ((BigDecimal) wallet.get("balance")).subtract((BigDecimal) wallet.get("reserved_balance")); @@ -516,6 +523,8 @@ public class AsyncTaskService { private void settleTaskInTransaction(String taskId) { Map task = jdbc.queryForMap("SELECT user_id,reserved_amount,cost_amount FROM model_tasks WHERE id=? FOR UPDATE", taskId); BigDecimal amount = (BigDecimal) task.get("reserved_amount"); + policies.release(taskId); + gatewayGuard.releaseTaskUserSlot((UUID)task.get("user_id"),taskId); if (amount == null || amount.signum() == 0 || task.get("cost_amount") != null) return; UUID userId = (UUID) task.get("user_id"); Map wallet = jdbc.queryForMap("SELECT balance,reserved_balance FROM user_wallets WHERE user_id=? FOR UPDATE", userId); @@ -530,6 +539,8 @@ public class AsyncTaskService { private void releaseTaskInTransaction(String taskId) { Map task = jdbc.queryForMap("SELECT user_id,reserved_amount,cost_amount FROM model_tasks WHERE id=? FOR UPDATE", taskId); BigDecimal amount = (BigDecimal) task.get("reserved_amount"); + policies.release(taskId); + gatewayGuard.releaseTaskUserSlot((UUID)task.get("user_id"),taskId); if (amount != null && amount.signum() > 0 && task.get("cost_amount") == null) { releaseWallet((UUID) task.get("user_id"), amount); jdbc.update("UPDATE model_tasks SET cost_amount=0 WHERE id=?", taskId); diff --git a/control-plane-java/src/main/java/com/moyi/controlplane/usage/OperationsController.java b/control-plane-java/src/main/java/com/moyi/controlplane/usage/OperationsController.java new file mode 100644 index 0000000..2ceb9a8 --- /dev/null +++ b/control-plane-java/src/main/java/com/moyi/controlplane/usage/OperationsController.java @@ -0,0 +1,50 @@ +package com.moyi.controlplane.usage; +import com.moyi.controlplane.auth.*; +import com.moyi.controlplane.common.ApiException; +import jakarta.servlet.http.HttpServletRequest; +import java.math.*; +import java.util.*; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.transaction.annotation.Transactional; +import org.springframework.web.bind.annotation.*; + +@RestController @RequestMapping("/api/admin/operations") +public class OperationsController { + private final JdbcTemplate jdbc;private final AuthService auth; + public OperationsController(JdbcTemplate jdbc,AuthService auth) {this.jdbc=jdbc;this.auth=auth;} + @Scheduled(fixedDelayString="${moyi.ops.alert-interval-ms:60000}") @Transactional + public void evaluate() { + var rules=jdbc.queryForList("SELECT * FROM ops_alert_rules WHERE enabled=true FOR UPDATE SKIP LOCKED"); + for(var rule:rules) { + var totals=jdbc.queryForMap("SELECT count(*) total,count(*) FILTER(WHERE status='failed') failed FROM usage_events WHERE status IN ('failed','succeeded') AND updated_at>now()-make_interval(mins=>?)",rule.get("window_minutes")); + long total=((Number)totals.get("total")).longValue(),failed=((Number)totals.get("failed")).longValue(); + BigDecimal ratio=total==0?BigDecimal.ZERO:BigDecimal.valueOf(failed*100).divide(BigDecimal.valueOf(total),2,RoundingMode.HALF_UP); + boolean firing=total>=((Number)rule.get("minimum_requests")).longValue()&&ratio.compareTo((BigDecimal)rule.get("error_percent"))>=0; + if(!firing) {jdbc.update("UPDATE ops_alert_events SET status='resolved' WHERE rule_id=? AND status='open'",rule.get("id"));continue;} + if(jdbc.update("UPDATE ops_alert_rules SET last_fired_at=now() WHERE id=? AND (last_fired_at IS NULL OR last_fired_atcooldown_minutes))",rule.get("id"))==1) + jdbc.update("INSERT INTO ops_alert_events(id,rule_id,total_requests,failed_requests,error_percent,notification_status,notification_email) VALUES (?,?,?,?,?,?,?)",UUID.randomUUID(),rule.get("id"),total,failed,ratio,Boolean.TRUE.equals(rule.get("notification_enabled"))?"pending":"disabled",rule.get("notification_email")); + } + } + @GetMapping public Map overview(HttpServletRequest request) { + auth.requireAdmin(AuthController.sessionToken(request)); + return Map.of("accounts",jdbc.queryForList("SELECT h.route_id,m.public_name,h.failures,h.last_status,h.latency_ms,h.cooldown_until,h.updated_at,substring(h.account_fingerprint,1,12) account FROM upstream_account_health h LEFT JOIN model_catalog m ON m.id=h.route_id ORDER BY h.updated_at DESC LIMIT 200"),"rules",jdbc.queryForList("SELECT * FROM ops_alert_rules ORDER BY id"),"alerts",jdbc.queryForList("SELECT * FROM ops_alert_events ORDER BY created_at DESC LIMIT 100")); + } + @PutMapping("/rules/{id}") @Transactional public Map save(@PathVariable String id,@RequestBody Map input,HttpServletRequest request) { + var user=auth.requireAdmin(AuthController.sessionToken(request)); + String email=String.valueOf(input.getOrDefault("notificationEmail","")).strip(); + boolean notify=Boolean.TRUE.equals(input.get("notificationEnabled")); + if(notify&&(email.length()>254||!email.matches("^[^\\s@,;<>]+@[^\\s@,;<>]+\\.[^\\s@,;<>]+$"))) throw new ApiException("INVALID_EMAIL","请输入有效的告警接收邮箱"); + int window=integer(input.get("windowMinutes"),1440),min=integer(input.get("minimumRequests"),1000000),cooldown=integer(input.get("cooldownMinutes"),1440); + BigDecimal ratio;try {ratio=new BigDecimal(input.get("errorPercent").toString());if(ratio.signum()<0||ratio.compareTo(BigDecimal.valueOf(100))>0) throw new IllegalArgumentException();} catch(Exception e) {throw new ApiException("INVALID_RULE","错误率须为 0–100");} + if(jdbc.update("UPDATE ops_alert_rules SET enabled=?,window_minutes=?,minimum_requests=?,error_percent=?,cooldown_minutes=?,updated_at=now() WHERE id=?",Boolean.TRUE.equals(input.get("enabled")),window,min,ratio,cooldown,id)!=1) throw new ApiException("RULE_NOT_FOUND","规则不存在",404); + jdbc.update("UPDATE ops_alert_rules SET notification_enabled=?,notification_email=? WHERE id=?",notify,notify?email:null,id); + if(!notify) jdbc.update("UPDATE ops_alert_events SET notification_status='disabled' WHERE rule_id=? AND notification_status IN ('pending','retry')",id); + auth.audit("ops.alert_rule_updated",user.id(),AuthController.meta(request),Map.of("ruleId",id,"settings",input));return Map.of("saved",true); + } + @PostMapping("/alerts/{id}/acknowledge") public Map acknowledge(@PathVariable UUID id,HttpServletRequest request) { + var user=auth.requireAdmin(AuthController.sessionToken(request));jdbc.update("UPDATE ops_alert_events SET status='acknowledged',acknowledged_by=?,acknowledged_at=now() WHERE id=? AND status='open'",user.id(),id); + auth.audit("ops.alert_acknowledged",user.id(),AuthController.meta(request),Map.of("alertId",id));return Map.of("saved",true); + } + private int integer(Object value,int max) {try {int n=new BigDecimal(value.toString()).intValueExact();if(n<1||n>max) throw new NumberFormatException();return n;} catch(Exception e) {throw new ApiException("INVALID_RULE","规则值须为 1–"+max+" 的整数");}} +} diff --git a/control-plane-java/src/main/java/com/moyi/controlplane/usage/OpsNotificationService.java b/control-plane-java/src/main/java/com/moyi/controlplane/usage/OpsNotificationService.java new file mode 100644 index 0000000..725ef2d --- /dev/null +++ b/control-plane-java/src/main/java/com/moyi/controlplane/usage/OpsNotificationService.java @@ -0,0 +1,29 @@ +package com.moyi.controlplane.usage; +import org.springframework.core.env.Environment; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.mail.SimpleMailMessage; +import org.springframework.mail.javamail.JavaMailSenderImpl; +import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; +@Service +public class OpsNotificationService { + private final JdbcTemplate jdbc;private final Environment env; + public OpsNotificationService(JdbcTemplate jdbc,Environment env) {this.jdbc=jdbc;this.env=env;} + private String value(String name) {return env.getProperty(name,"").strip();} + public boolean available() {return !value("MOYI_SMTP_HOST").isEmpty()&&!value("MOYI_SMTP_FROM").isEmpty();} + @Scheduled(fixedDelayString="${moyi.ops.notification-interval-ms:60000}") @Transactional + public void deliver() { + if(!available()) return; + var rows=jdbc.queryForList("SELECT * FROM ops_alert_events WHERE notification_status IN ('pending','retry') AND notification_attempts<3 AND (notification_next_at IS NULL OR notification_next_at<=now()) ORDER BY created_at LIMIT 5 FOR UPDATE SKIP LOCKED"); + for(var row:rows) { + try { + var mail=new JavaMailSenderImpl();mail.setHost(value("MOYI_SMTP_HOST"));mail.setPort(env.getProperty("MOYI_SMTP_PORT",Integer.class,465));mail.setUsername(value("MOYI_SMTP_USERNAME"));mail.setPassword(env.getProperty("MOYI_SMTP_PASSWORD",""));mail.setDefaultEncoding("UTF-8"); + var p=mail.getJavaMailProperties();p.setProperty("mail.smtp.auth",String.valueOf(!value("MOYI_SMTP_USERNAME").isEmpty()));p.setProperty("mail.smtp.ssl.enable",env.getProperty("MOYI_SMTP_SSL","true"));p.setProperty("mail.smtp.starttls.enable",env.getProperty("MOYI_SMTP_STARTTLS","false"));p.setProperty("mail.smtp.starttls.required",env.getProperty("MOYI_SMTP_STARTTLS","false"));p.setProperty("mail.smtp.ssl.checkserveridentity","true"); + for(String name:new String[]{"connectiontimeout","timeout","writetimeout"}) p.setProperty("mail.smtp."+name,"5000"); + var message=new SimpleMailMessage();message.setFrom(value("MOYI_SMTP_FROM"));message.setTo(row.get("notification_email").toString());message.setSubject("MOYI 网关错误率告警");message.setText("告警编号:"+row.get("id")+"\n失败请求:"+row.get("failed_requests")+" / "+row.get("total_requests")+"\n错误率:"+row.get("error_percent")+"%\n请登录管理端的渠道健康与告警查看详情。");mail.send(message); + jdbc.update("UPDATE ops_alert_events SET notification_status='sent',notification_attempts=notification_attempts+1 WHERE id=?",row.get("id")); + } catch(Exception ignored) {jdbc.update("UPDATE ops_alert_events SET notification_status=CASE WHEN notification_attempts>=2 THEN 'failed' ELSE 'retry' END,notification_attempts=notification_attempts+1,notification_next_at=now()+interval '10 minutes' WHERE id=?",row.get("id"));} + } + } +} diff --git a/control-plane-java/src/main/java/com/moyi/controlplane/usage/UsageAuditService.java b/control-plane-java/src/main/java/com/moyi/controlplane/usage/UsageAuditService.java index e09ff9f..d212826 100644 --- a/control-plane-java/src/main/java/com/moyi/controlplane/usage/UsageAuditService.java +++ b/control-plane-java/src/main/java/com/moyi/controlplane/usage/UsageAuditService.java @@ -65,6 +65,10 @@ public class UsageAuditService { int latency, Integer firstTokenMs, java.math.BigDecimal cost, Object output, String code, String message) { jdbc.update("UPDATE usage_events SET status=?,input_units=?,output_units=?,cached_input_units=?,latency_ms=?,first_token_ms=?,cost_amount=?,output_payload=?::jsonb,error_code=?,error_message=?,updated_at=now() WHERE id=?", status,tokens.prompt(),tokens.completion(),tokens.cached(),latency,firstTokenMs,cost,json(output),code,message,id); + jdbc.update("UPDATE usage_events SET cache_write_units=? WHERE id=?",tokens.cacheWrite5m()+tokens.cacheWrite1h(),id); + } + public void observeUsage(UUID id,String protocol,Object observed) { + jdbc.update("UPDATE usage_events SET observed_usage=?::jsonb WHERE id=?",json(Map.of("protocol",protocol,"usage",observed)),id); } public void recordCall( @@ -393,8 +397,8 @@ public class UsageAuditService { ObjectNode result = objectMapper.createObjectNode(); node.fields().forEachRemaining(entry -> { String key = entry.getKey().toLowerCase(); - boolean usageField = java.util.Set.of("prompt_tokens", "completion_tokens", "total_tokens", "cached_tokens", "reasoning_tokens", "audio_tokens", "accepted_prediction_tokens", "rejected_prediction_tokens", "prompt_cache_hit_tokens", "prompt_cache_miss_tokens", "max_tokens", "max_completion_tokens").contains(key) && entry.getValue().isNumber(); - boolean usageDetails = java.util.Set.of("prompt_tokens_details", "completion_tokens_details").contains(key) && entry.getValue().isObject(); + boolean usageField = java.util.Set.of("prompt_tokens", "completion_tokens", "total_tokens", "cached_tokens", "reasoning_tokens", "audio_tokens", "accepted_prediction_tokens", "rejected_prediction_tokens", "prompt_cache_hit_tokens", "prompt_cache_miss_tokens", "max_tokens", "max_completion_tokens", "input_tokens", "output_tokens", "cache_read_input_tokens", "cache_creation_input_tokens", "ephemeral_5m_input_tokens", "ephemeral_1h_input_tokens", "prompttokencount", "candidatestokencount", "thoughtstokencount", "cachedcontenttokencount").contains(key) && entry.getValue().isNumber(); + boolean usageDetails = java.util.Set.of("prompt_tokens_details", "completion_tokens_details", "input_tokens_details", "output_tokens_details").contains(key) && entry.getValue().isObject(); if (key.contains("authorization") || key.contains("api_key") || key.contains("apikey") || (key.contains("token") && !usageField && !usageDetails) || key.contains("secret") || key.contains("password")) result.put(entry.getKey(), "[REDACTED]"); else result.set(entry.getKey(), sanitize(entry.getValue())); }); diff --git a/control-plane-java/src/main/java/com/moyi/controlplane/wallet/ChatBillingService.java b/control-plane-java/src/main/java/com/moyi/controlplane/wallet/ChatBillingService.java index 85e30fa..1a89f21 100644 --- a/control-plane-java/src/main/java/com/moyi/controlplane/wallet/ChatBillingService.java +++ b/control-plane-java/src/main/java/com/moyi/controlplane/wallet/ChatBillingService.java @@ -18,13 +18,30 @@ public class ChatBillingService { private final JdbcTemplate jdbc; private final ObjectMapper json; private final UsageAuditService usage; - public ChatBillingService(JdbcTemplate jdbc, ObjectMapper json, UsageAuditService usage) { this.jdbc=jdbc; this.json=json; this.usage=usage; } + 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); } 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 observedUsage(Ticket ticket,String protocol,Object observed) { + if(observed==null) return; + usage.observeUsage(ticket.id(),protocol,observed); + } + @Transactional public void actualRoute(Ticket ticket,Map route) { + var snapshots=jdbc.queryForList("SELECT procurement_pricing FROM model_price_history WHERE model_id=? ORDER BY id DESC LIMIT 1",route.get("id")); + Object cost=snapshots.isEmpty()?null:snapshots.getFirst().get("procurement_pricing"); + if(cost!=null) ChatMeter.rates(decode(cost.toString()),false); + jdbc.update("UPDATE chat_billing_requests SET procurement_pricing=?::jsonb WHERE id=? AND status='pending'",cost==null?null:cost.toString(),ticket.id()); + jdbc.update("UPDATE usage_events SET actual_route_id=? WHERE id=?",route.get("id"),ticket.id()); + } @Transactional public Ticket reserve(Map key, Map model, ChatMeter.Request request, String idempotencyKey) { UUID userId=UUID.fromString(key.get("userId").toString()); - ChatMeter.Rates rates=ChatMeter.rates(model.get("pricing"),"provider-mock".equals(model.get("providerId"))); + 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.of():versions.getFirst(); + Object retail=versions.isEmpty()?model.get("pricing"):(version.get("pricing")==null?null:decode(version.get("pricing").toString())); + ChatMeter.Rates rates=ChatMeter.rates(retail,"provider-mock".equals(model.get("providerId"))); + if(version.get("procurement_pricing")!=null) ChatMeter.rates(decode(version.get("procurement_pricing").toString()),false); String requestHash=hash(encode(new TreeMap<>(request.body()))); String idem=null; if(idempotencyKey!=null) { @@ -34,17 +51,17 @@ public class ChatBillingService { var previous=jdbc.queryForList("SELECT id, request_hash FROM chat_billing_requests WHERE user_id=? AND idempotency_hash=?",userId,idem); if(!previous.isEmpty()) throw new ApiException(requestHash.equals(previous.getFirst().get("request_hash"))?"DUPLICATE_REQUEST":"IDEMPOTENCY_CONFLICT","该请求编号已受理,请在使用记录中查看结果;不会重复调用或扣费",409); } - BigDecimal hold=rates.input().max(rates.cachedInput()).multiply(BigDecimal.valueOf(request.reservedInputTokens())) - .add(rates.output().multiply(BigDecimal.valueOf(request.maxOutputTokens()))) - .divide(new BigDecimal("1000000"),8,java.math.RoundingMode.CEILING); + BigDecimal hold=rates.hold(request.reservedInputTokens(),request.maxOutputTokens()); jdbc.update("INSERT INTO user_wallets(user_id) VALUES (?) ON CONFLICT DO NOTHING",userId); var wallet=jdbc.queryForMap("SELECT balance,reserved_balance FROM user_wallets WHERE user_id=? FOR UPDATE",userId); BigDecimal available=((BigDecimal)wallet.get("balance")).subtract((BigDecimal)wallet.get("reserved_balance")); if(available.compareTo(hold)<0) throw new ApiException("INSUFFICIENT_BALANCE","可用余额不足以预留本次调用费用,请充值或调低输出 Token 上限",402); UUID id=UUID.randomUUID(); + 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()); // 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("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 user_wallets SET reserved_balance=reserved_balance+?,updated_at=now() WHERE user_id=?",hold,userId); return new Ticket(id,userId,rates); } @@ -57,13 +74,18 @@ public class ChatBillingService { var tokens=ChatMeter.tokens(response); // Read the persisted snapshot, not editable catalog prices or caller-supplied rates. ChatMeter.Rates rates=ChatMeter.rates(decode(call.get("pricing").toString()),false); - BigDecimal cost=rates.cost(tokens.prompt(),tokens.completion(),tokens.cached()); + BigDecimal cost=rates.cost(tokens); BigDecimal before=jdbc.queryForObject("SELECT balance FROM user_wallets WHERE user_id=? FOR UPDATE",BigDecimal.class,ticket.userId()); BigDecimal after=before.subtract(cost); jdbc.update("UPDATE user_wallets SET balance=?,reserved_balance=reserved_balance-?,updated_at=now() WHERE user_id=?",after,call.get("reserved_amount"),ticket.userId()); if(cost.signum()>0) jdbc.update("INSERT INTO wallet_entries(id,user_id,request_id,amount,balance_before,balance_after,reason,operation,requested_amount) VALUES (?,?,?,?,?,?,?,'usage',?)",UUID.randomUUID(),ticket.userId(),ticket.id(),cost.negate(),before,after,"模型调用 · "+ticket.id(),cost); usage.finishChat(ticket.id(),"succeeded",tokens,latency,firstTokenMs,cost,response,null,null); + if(call.get("procurement_pricing")!=null) { + 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 chat_billing_requests SET status='succeeded',cost_amount=? WHERE id=?",cost,ticket.id()); + policies.release(ticket.id().toString()); return cost; } @@ -76,6 +98,8 @@ public class ChatBillingService { 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); 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); + policies.release(id.toString()); } @Scheduled(fixedDelayString="${moyi.billing.recovery-interval-ms:60000}") diff --git a/control-plane-java/src/main/resources/application.yml b/control-plane-java/src/main/resources/application.yml index 867db6f..047895f 100644 --- a/control-plane-java/src/main/resources/application.yml +++ b/control-plane-java/src/main/resources/application.yml @@ -27,6 +27,8 @@ server: address: ${CONTROL_PLANE_HOST:0.0.0.0} port: ${CONTROL_PLANE_PORT:8787} shutdown: graceful + # Native Tomcat processing trusts only its configured internal proxies, never arbitrary clients. + forward-headers-strategy: ${MOYI_FORWARD_HEADERS_STRATEGY:none} management: endpoints: diff --git a/control-plane-java/src/main/resources/db/migration/V35__price_versions_and_cost_reconciliation.sql b/control-plane-java/src/main/resources/db/migration/V35__price_versions_and_cost_reconciliation.sql new file mode 100644 index 0000000..69ce884 --- /dev/null +++ b/control-plane-java/src/main/resources/db/migration/V35__price_versions_and_cost_reconciliation.sql @@ -0,0 +1,31 @@ +ALTER TABLE model_catalog ADD COLUMN procurement_pricing jsonb; +CREATE TABLE model_price_history ( + id bigserial PRIMARY KEY, + model_id varchar(64) NOT NULL, + pricing jsonb, + procurement_pricing jsonb, + created_at timestamptz NOT NULL DEFAULT now() +); +CREATE INDEX model_price_history_model_idx ON model_price_history(model_id,id DESC); +INSERT INTO model_price_history(model_id,pricing) SELECT id,pricing FROM model_catalog; +CREATE FUNCTION record_model_price_change() RETURNS trigger LANGUAGE plpgsql AS $$ +BEGIN + IF TG_OP='INSERT' THEN + INSERT INTO model_price_history(model_id,pricing,procurement_pricing) VALUES(NEW.id,NEW.pricing,NEW.procurement_pricing); + ELSIF NEW.pricing IS DISTINCT FROM OLD.pricing OR NEW.procurement_pricing IS DISTINCT FROM OLD.procurement_pricing THEN + INSERT INTO model_price_history(model_id,pricing,procurement_pricing) VALUES(NEW.id,NEW.pricing,NEW.procurement_pricing); + END IF; + RETURN NEW; +END $$; +CREATE TRIGGER model_price_version AFTER INSERT OR UPDATE ON model_catalog FOR EACH ROW EXECUTE FUNCTION record_model_price_change(); + +ALTER TABLE chat_billing_requests ADD COLUMN procurement_pricing jsonb; +ALTER TABLE chat_billing_requests ADD COLUMN price_version bigint REFERENCES model_price_history(id); +ALTER TABLE usage_events ADD COLUMN procurement_cost numeric(20,8) CHECK(procurement_cost>=0); +ALTER TABLE usage_events ADD COLUMN profit_amount numeric(20,8); +ALTER TABLE usage_events ADD COLUMN cost_status varchar(24) NOT NULL DEFAULT 'unknown' + CHECK(cost_status IN ('unknown','estimated','pending','reconciled')); +ALTER TABLE usage_events ADD COLUMN reconciliation_note text; +ALTER TABLE usage_events ADD COLUMN reconciled_by uuid REFERENCES users(id); +ALTER TABLE usage_events ADD COLUMN reconciled_at timestamptz; +CREATE INDEX usage_cost_pending_idx ON usage_events(created_at) WHERE cost_status IN ('pending','unknown'); diff --git a/control-plane-java/src/main/resources/db/migration/V36__enterprise_access_policies.sql b/control-plane-java/src/main/resources/db/migration/V36__enterprise_access_policies.sql new file mode 100644 index 0000000..64071e8 --- /dev/null +++ b/control-plane-java/src/main/resources/db/migration/V36__enterprise_access_policies.sql @@ -0,0 +1,24 @@ +CREATE TABLE gateway_user_policies ( + user_id uuid PRIMARY KEY REFERENCES users(id), + requests_per_minute integer NOT NULL DEFAULT 120 CHECK(requests_per_minute BETWEEN 1 AND 100000), + max_concurrency integer NOT NULL DEFAULT 16 CHECK(max_concurrency BETWEEN 1 AND 10000), + monthly_budget numeric(20,8) CHECK(monthly_budget >= 0), + updated_at timestamptz NOT NULL DEFAULT now() +); +CREATE TABLE gateway_key_policies ( + key_id uuid PRIMARY KEY REFERENCES platform_api_keys(id), + expires_at timestamptz, + ip_allowlist jsonb NOT NULL DEFAULT '[]' CHECK(jsonb_typeof(ip_allowlist)='array'), + total_budget numeric(20,8) CHECK(total_budget >= 0), + daily_budget numeric(20,8) CHECK(daily_budget >= 0), + monthly_budget numeric(20,8) CHECK(monthly_budget >= 0), + updated_at timestamptz NOT NULL DEFAULT now() +); +CREATE TABLE gateway_budget_holds ( + request_id varchar(100) PRIMARY KEY, + user_id uuid NOT NULL REFERENCES users(id), + key_id uuid NOT NULL REFERENCES platform_api_keys(id), + amount numeric(20,8) NOT NULL CHECK(amount >= 0), + created_at timestamptz NOT NULL DEFAULT now() +); +CREATE INDEX gateway_budget_holds_user ON gateway_budget_holds(user_id); diff --git a/control-plane-java/src/main/resources/db/migration/V37__totp_authentication.sql b/control-plane-java/src/main/resources/db/migration/V37__totp_authentication.sql new file mode 100644 index 0000000..52dea32 --- /dev/null +++ b/control-plane-java/src/main/resources/db/migration/V37__totp_authentication.sql @@ -0,0 +1,16 @@ +CREATE TABLE user_totp ( + user_id uuid PRIMARY KEY REFERENCES users(id), + secret_ciphertext text NOT NULL, + enabled boolean NOT NULL DEFAULT false, + pending_until timestamptz, + last_step bigint NOT NULL DEFAULT -1, + attempts integer NOT NULL DEFAULT 0, + attempt_window timestamptz NOT NULL DEFAULT now(), + updated_at timestamptz NOT NULL DEFAULT now() +); +CREATE TABLE user_totp_recovery ( + user_id uuid NOT NULL REFERENCES users(id), + code_hash varchar(64) NOT NULL, + used_at timestamptz, + PRIMARY KEY(user_id,code_hash) +); diff --git a/control-plane-java/src/main/resources/db/migration/V38__upstream_health_and_alerts.sql b/control-plane-java/src/main/resources/db/migration/V38__upstream_health_and_alerts.sql new file mode 100644 index 0000000..0e7b87a --- /dev/null +++ b/control-plane-java/src/main/resources/db/migration/V38__upstream_health_and_alerts.sql @@ -0,0 +1,34 @@ +CREATE TABLE upstream_account_health ( + route_id varchar(64) NOT NULL, + account_fingerprint varchar(64) NOT NULL, + failures integer NOT NULL DEFAULT 0, + last_status integer, + latency_ms integer, + cooldown_until timestamptz, + updated_at timestamptz NOT NULL DEFAULT now(), + PRIMARY KEY(route_id,account_fingerprint) +); +CREATE TABLE ops_alert_rules ( + id varchar(64) PRIMARY KEY, + name varchar(120) NOT NULL, + enabled boolean NOT NULL DEFAULT true, + window_minutes integer NOT NULL DEFAULT 5 CHECK(window_minutes BETWEEN 1 AND 1440), + minimum_requests integer NOT NULL DEFAULT 10 CHECK(minimum_requests BETWEEN 1 AND 1000000), + error_percent numeric(5,2) NOT NULL DEFAULT 20 CHECK(error_percent BETWEEN 0 AND 100), + cooldown_minutes integer NOT NULL DEFAULT 30 CHECK(cooldown_minutes BETWEEN 1 AND 1440), + last_fired_at timestamptz, + updated_at timestamptz NOT NULL DEFAULT now() +); +INSERT INTO ops_alert_rules(id,name) VALUES ('gateway-errors','网关错误率'); +CREATE TABLE ops_alert_events ( + id uuid PRIMARY KEY, + rule_id varchar(64) NOT NULL REFERENCES ops_alert_rules(id), + total_requests bigint NOT NULL, + failed_requests bigint NOT NULL, + error_percent numeric(5,2) NOT NULL, + status varchar(16) NOT NULL DEFAULT 'open' CHECK(status IN ('open','acknowledged','resolved')), + created_at timestamptz NOT NULL DEFAULT now(), + acknowledged_by uuid REFERENCES users(id), + acknowledged_at timestamptz +); +CREATE INDEX ops_alert_events_recent ON ops_alert_events(created_at DESC); diff --git a/control-plane-java/src/main/resources/db/migration/V39__actual_route_reconciliation.sql b/control-plane-java/src/main/resources/db/migration/V39__actual_route_reconciliation.sql new file mode 100644 index 0000000..5f556ff --- /dev/null +++ b/control-plane-java/src/main/resources/db/migration/V39__actual_route_reconciliation.sql @@ -0,0 +1,2 @@ +ALTER TABLE usage_events ADD COLUMN actual_route_id varchar(64); +ALTER TABLE usage_events ADD COLUMN observed_usage jsonb; diff --git a/control-plane-java/src/main/resources/db/migration/V40__alert_notification_outbox.sql b/control-plane-java/src/main/resources/db/migration/V40__alert_notification_outbox.sql new file mode 100644 index 0000000..3743b0e --- /dev/null +++ b/control-plane-java/src/main/resources/db/migration/V40__alert_notification_outbox.sql @@ -0,0 +1,6 @@ +ALTER TABLE ops_alert_rules ADD COLUMN notification_email varchar(254); +ALTER TABLE ops_alert_rules ADD COLUMN notification_enabled boolean NOT NULL DEFAULT false; +ALTER TABLE ops_alert_events ADD COLUMN notification_status varchar(24) NOT NULL DEFAULT 'disabled'; +ALTER TABLE ops_alert_events ADD COLUMN notification_attempts integer NOT NULL DEFAULT 0; +ALTER TABLE ops_alert_events ADD COLUMN notification_next_at timestamptz; +ALTER TABLE ops_alert_events ADD COLUMN notification_email varchar(254); diff --git a/control-plane-java/src/main/resources/redis/acquire-slot.lua b/control-plane-java/src/main/resources/redis/acquire-slot.lua index 970b64b..52ac1c3 100644 --- a/control-plane-java/src/main/resources/redis/acquire-slot.lua +++ b/control-plane-java/src/main/resources/redis/acquire-slot.lua @@ -5,5 +5,9 @@ if redis.call('ZCARD', KEYS[1]) >= tonumber(ARGV[1]) then return 0 end redis.call('ZADD', KEYS[1], now + tonumber(ARGV[2]), ARGV[3]) -redis.call('PEXPIRE', KEYS[1], ARGV[2]) +-- Long async leases and short chat leases share the user bucket. +-- A chat admission must never shorten a running async task's lease container. +if redis.call('PTTL', KEYS[1]) < tonumber(ARGV[2]) then + redis.call('PEXPIRE', KEYS[1], ARGV[2]) +end return 1 diff --git a/control-plane-java/src/test/java/com/moyi/controlplane/PlatformAdminPostgresTest.java b/control-plane-java/src/test/java/com/moyi/controlplane/PlatformAdminPostgresTest.java index 4938f0b..e3c0b5c 100644 --- a/control-plane-java/src/test/java/com/moyi/controlplane/PlatformAdminPostgresTest.java +++ b/control-plane-java/src/test/java/com/moyi/controlplane/PlatformAdminPostgresTest.java @@ -82,6 +82,7 @@ class PlatformAdminPostgresTest { context.registerBean(DataSource.class, () -> source); context.registerBean(DataSourceTransactionManager.class, () -> new DataSourceTransactionManager(source)); context.registerBean(AuthService.class, () -> new AuthService(jdbc, new PasswordHasher("test-pepper"), json, 168)); + context.registerBean(TotpService.class, () -> new TotpService(jdbc,new SecretCipher("test-mfa"))); context.registerBean(CatalogService.class, () -> new CatalogService(jdbc, json, new SecretCipher("test-encryption"))); context.registerBean(WalletService.class, () -> new WalletService(jdbc, context.getBean(AuthService.class))); context.registerBean(UsageAuditService.class, () -> new UsageAuditService(jdbc,json)); @@ -371,6 +372,7 @@ class PlatformAdminPostgresTest { ChatFixture chatFixture() { var model=new LinkedHashMap<>(catalog.listModels(false).stream().filter(m -> "provider-deepseek".equals(m.get("providerId"))).findFirst().orElseThrow()); model.put("pricing",Map.of("input",2,"output",4,"cachedInput",1,"unit","CNY / 1M tokens")); + jdbc.update("UPDATE model_catalog SET pricing='{"+"\"input\":2,\"output\":4,\"cachedInput\":1,\"unit\":\"CNY / 1M tokens\"}'::jsonb WHERE id=?",model.get("id")); var created=catalog.createKey(memberToken,Map.of("name","Local billing test","modelIds",List.of(model.get("id"))),auth); var key=catalog.authenticateKey(created.get("key").toString()); var input=ChatMeter.request(Map.of("model",model.get("id"),"messages",List.of(Map.of("role","user","content","你好")),"max_tokens",16),json); @@ -444,9 +446,11 @@ class PlatformAdminPostgresTest { var f=chatFixture(); assertEquals("INSUFFICIENT_BALANCE",assertThrows(ApiException.class,()->f.reserve(null)).getCode()); f.model().remove("pricing"); + jdbc.update("UPDATE model_catalog SET pricing=NULL WHERE id=?",f.model().get("id")); assertEquals("MODEL_NOT_PRICED",assertThrows(ApiException.class,()->f.reserve(null)).getCode()); f.model().put("pricing",Map.of("input",1,"output",2,"unit","USD / 1M tokens")); - assertEquals("MODEL_NOT_PRICED",assertThrows(ApiException.class,()->f.reserve(null)).getCode()); + jdbc.update("UPDATE model_catalog SET pricing='{"+"\"input\":1,\"output\":2,\"unit\":\"USD / 1M tokens\"}'::jsonb WHERE id=?",f.model().get("id")); + assertEquals("BILLING_UNIT_UNSUPPORTED",assertThrows(ApiException.class,()->f.reserve(null)).getCode()); assertEquals(0,jdbc.queryForObject("SELECT count(*) FROM chat_billing_requests",Integer.class)); } diff --git a/control-plane-java/src/test/java/com/moyi/controlplane/auth/AuthPostgresTest.java b/control-plane-java/src/test/java/com/moyi/controlplane/auth/AuthPostgresTest.java index f0d0749..16a1e4c 100644 --- a/control-plane-java/src/test/java/com/moyi/controlplane/auth/AuthPostgresTest.java +++ b/control-plane-java/src/test/java/com/moyi/controlplane/auth/AuthPostgresTest.java @@ -51,6 +51,7 @@ class AuthPostgresTest { context.registerBean(DataSource.class, () -> source); context.registerBean(DataSourceTransactionManager.class, () -> new DataSourceTransactionManager(source)); context.registerBean(AuthService.class, () -> new AuthService(jdbc, hasher, json, 168)); + context.registerBean(TotpService.class, () -> new TotpService(jdbc,new com.moyi.controlplane.security.SecretCipher("test-mfa"))); context.refresh(); AuthService auth = context.getBean(AuthService.class); try (var threads = Executors.newFixedThreadPool(2)) { diff --git a/control-plane-java/src/test/java/com/moyi/controlplane/auth/TotpServiceTest.java b/control-plane-java/src/test/java/com/moyi/controlplane/auth/TotpServiceTest.java new file mode 100644 index 0000000..8d27d1f --- /dev/null +++ b/control-plane-java/src/test/java/com/moyi/controlplane/auth/TotpServiceTest.java @@ -0,0 +1,13 @@ +package com.moyi.controlplane.auth; +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.*; +import java.nio.charset.StandardCharsets; +class TotpServiceTest { + @Test void rfc6238Sha1VectorsTruncatedToSixDigits() { + String secret=TotpService.base32("12345678901234567890".getBytes(StandardCharsets.US_ASCII)); + assertEquals("287082",TotpService.totp(secret,59/30)); + assertEquals("081804",TotpService.totp(secret,1111111109L/30)); + assertEquals("050471",TotpService.totp(secret,1111111111L/30)); + assertEquals("353130",TotpService.totp(secret,20000000000L/30)); + } +} diff --git a/control-plane-java/src/test/java/com/moyi/controlplane/gateway/EnterprisePoliciesTest.java b/control-plane-java/src/test/java/com/moyi/controlplane/gateway/EnterprisePoliciesTest.java new file mode 100644 index 0000000..b91a5dc --- /dev/null +++ b/control-plane-java/src/test/java/com/moyi/controlplane/gateway/EnterprisePoliciesTest.java @@ -0,0 +1,21 @@ +package com.moyi.controlplane.gateway; +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.*; +import java.util.List; +class EnterprisePoliciesTest { + @Test void ipv4AndIpv6CidrAreMatchedWithoutDns() { + assertTrue(EnterprisePolicies.matches("192.0.2.23","192.0.2.0/24")); + assertFalse(EnterprisePolicies.matches("192.0.3.23","192.0.2.0/24")); + assertTrue(EnterprisePolicies.matches("2001:db8::4","2001:db8::/32")); + assertFalse(EnterprisePolicies.matches("2001:db9::4","2001:db8::/32")); + assertFalse(EnterprisePolicies.matches("127.0.0.1","localhost")); + assertFalse(EnterprisePolicies.matches("127.0.0.1","127.0.0.1/33")); + assertThrows(RuntimeException.class,()->EnterprisePolicies.validateRanges(List.of("127.1"))); + } + @Test void nullBudgetIsUnlimitedAndZeroBudgetBlocksSpending() { + assertNull(EnterprisePolicyController.budget("")); + assertEquals(0,EnterprisePolicyController.budget("0").signum()); + assertThrows(RuntimeException.class,()->EnterprisePolicyController.budget("-1")); + assertThrows(RuntimeException.class,()->EnterprisePolicyController.integer("1.5",100)); + } +} diff --git a/control-plane-java/src/test/java/com/moyi/controlplane/gateway/EnterprisePostgresTest.java b/control-plane-java/src/test/java/com/moyi/controlplane/gateway/EnterprisePostgresTest.java new file mode 100644 index 0000000..76f6d7d --- /dev/null +++ b/control-plane-java/src/test/java/com/moyi/controlplane/gateway/EnterprisePostgresTest.java @@ -0,0 +1,120 @@ +package com.moyi.controlplane.gateway; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.moyi.controlplane.auth.TotpService; +import com.moyi.controlplane.auth.AuthService; +import com.moyi.controlplane.catalog.PriceManagementController; +import com.moyi.controlplane.usage.OperationsController; +import org.springframework.mock.web.MockHttpServletRequest; +import com.moyi.controlplane.common.ApiException; +import com.moyi.controlplane.security.SecretCipher; +import com.moyi.controlplane.usage.UsageAuditService; +import com.moyi.controlplane.wallet.ChatBillingService; +import com.moyi.controlplane.provider.UpstreamHealth; +import java.math.BigDecimal; +import java.util.*; +import java.util.concurrent.*; +import javax.sql.DataSource; +import org.flywaydb.core.Flyway; +import org.junit.jupiter.api.*; +import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable; +import org.springframework.context.annotation.*; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.jdbc.datasource.*; +import org.springframework.transaction.annotation.EnableTransactionManagement; +import org.springframework.test.util.ReflectionTestUtils; +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Mockito.*; + +@EnabledIfEnvironmentVariable(named="MOYI_AUTH_TEST_JDBC_URL",matches="jdbc:postgresql://127\\.0\\.0\\.1:[0-9]+/.+") +class EnterprisePostgresTest { + @Configuration @EnableTransactionManagement(proxyTargetClass=true) static class Transactions {} + JdbcTemplate jdbc;DriverManagerDataSource base;AnnotationConfigApplicationContext context;String schema,model; + UUID user=UUID.randomUUID(),key=UUID.randomUUID();ObjectMapper json=new ObjectMapper();ChatBillingService billing; + Map keyMap,modelMap; + @BeforeEach void setup() { + String url=System.getenv("MOYI_AUTH_TEST_JDBC_URL");schema="enterprise_"+UUID.randomUUID().toString().replace("-",""); + base=new DriverManagerDataSource(url,"moyi_role_test","");var source=new DriverManagerDataSource(url+"?currentSchema="+schema,"moyi_role_test",""); + Flyway.configure().dataSource(base).schemas(schema).defaultSchema(schema).load().migrate();jdbc=new JdbcTemplate(source); + context=new AnnotationConfigApplicationContext();context.register(Transactions.class); + context.registerBean(DataSource.class,()->source);context.registerBean(DataSourceTransactionManager.class,()->new DataSourceTransactionManager(source)); + context.registerBean(ChatBillingService.class,()->new ChatBillingService(jdbc,json,new UsageAuditService(jdbc,json))); + context.registerBean(TotpService.class,()->new TotpService(jdbc,new SecretCipher("isolated-secret")));context.refresh();billing=context.getBean(ChatBillingService.class); + jdbc.update("INSERT INTO users(id,account,account_kind,password_hash,display_name) VALUES (?,'enterprise-test','username','not-a-login','test')",user); + jdbc.update("INSERT INTO platform_api_keys(id,user_id,name,key_prefix,key_hash) VALUES (?,?,'key','test',?)",key,user,"e".repeat(64)); + jdbc.update("INSERT INTO user_wallets(user_id,balance) VALUES (?,100)",user); + model=jdbc.queryForObject("SELECT id FROM model_catalog WHERE provider_id<>'provider-mock' LIMIT 1",String.class); + jdbc.update("UPDATE model_catalog SET pricing=?::jsonb,procurement_pricing=?::jsonb WHERE id=?","{\"unit\":\"CNY / 1M tokens\",\"input\":1,\"output\":2}","{\"unit\":\"CNY / 1M tokens\",\"input\":0.5,\"output\":0.8}",model); + keyMap=Map.of("id",key,"userId",user);modelMap=Map.of("id",model,"providerId",jdbc.queryForObject("SELECT provider_id FROM model_catalog WHERE id=?",String.class,model)); + } + @AfterEach void cleanup() {if(context!=null) context.close();if(base!=null) new JdbcTemplate(base).execute("DROP SCHEMA IF EXISTS "+schema+" CASCADE");} + ChatMeter.Request request() {return new ChatMeter.Request(Map.of("messages",List.of(Map.of("role","user","content","test"))),100,10);} + @Test void concurrentAdmissionsRespectBudgetAndReleaseFailures() throws Exception { + jdbc.update("INSERT INTO gateway_user_policies(user_id,monthly_budget) VALUES (?,0.0002)",user); + var gate=new CountDownLatch(1);var tickets=new CopyOnWriteArrayList(); + try(var pool=Executors.newFixedThreadPool(2)) { + var jobs=new ArrayList>();for(int i=0;i<2;i++) jobs.add(pool.submit(()->{gate.await();try {tickets.add(billing.reserve(keyMap,modelMap,request(),null));return "accepted";} catch(ApiException e) {return e.getCode();}})); + gate.countDown();var outcomes=new ArrayList();for(var job:jobs) outcomes.add(job.get(10,TimeUnit.SECONDS)); + assertEquals(1,Collections.frequency(outcomes,"accepted"));assertEquals(1,Collections.frequency(outcomes,"BUDGET_EXCEEDED")); + } + billing.fail(tickets.getFirst(),"TEST","interrupted",10); + assertEquals(0,jdbc.queryForObject("SELECT count(*) FROM gateway_budget_holds",Integer.class)); + assertEquals("pending",jdbc.queryForObject("SELECT cost_status FROM usage_events WHERE id=?",String.class,tickets.getFirst().id())); + assertNotNull(billing.reserve(keyMap,modelMap,request(),null)); + } + @Test void editsCannotChangeInflightRetailOrProcurementSnapshots() { + var ticket=billing.reserve(keyMap,modelMap,request(),"one"); + jdbc.update("UPDATE model_catalog SET pricing='{"+"\"unit\":\"CNY / 1M tokens\",\"input\":10,\"output\":20}'::jsonb,procurement_pricing=NULL WHERE id=?",model); + var response=Map.of("usage",Map.of("prompt_tokens",100,"completion_tokens",10)); + assertEquals(new BigDecimal("0.00012000"),billing.settle(ticket,response,100,null)); + assertEquals(new BigDecimal("0.00012000"),billing.settle(ticket,response,100,null)); + assertEquals(new BigDecimal("0.00005800"),jdbc.queryForObject("SELECT procurement_cost FROM usage_events WHERE id=?",BigDecimal.class,ticket.id())); + assertEquals(1,jdbc.queryForObject("SELECT count(*) FROM wallet_entries WHERE request_id=?",Integer.class,ticket.id())); + assertThrows(ApiException.class,()->billing.reserve(keyMap,modelMap,request(),"one")); + } + @Test void mfaRejectsReplayAndRecoveryCodesAreSingleUse() { + var mfa=context.getBean(TotpService.class);var setup=mfa.begin(user,"enterprise-test"); + String secret=setup.get("secret").toString();String code=ReflectionTestUtils.invokeMethod(TotpService.class,"totp",secret,System.currentTimeMillis()/30000); + var recovery=mfa.confirm(user,code);assertEquals(10,recovery.size()); + assertThrows(ApiException.class,()->mfa.verifyLogin(user,code)); + mfa.verifyLogin(user,recovery.getFirst());assertThrows(ApiException.class,()->mfa.verifyLogin(user,recovery.getFirst())); + assertEquals(9,mfa.status(user).get("recoveryRemaining")); + assertFalse(jdbc.queryForObject("SELECT secret_ciphertext FROM user_totp WHERE user_id=?",String.class,user).contains(secret)); + for(int i=0;i<4;i++) assertThrows(ApiException.class,()->mfa.verifyLogin(user,"wrong")); + assertEquals("MFA_RATE_LIMITED",assertThrows(ApiException.class,()->mfa.verifyLogin(user,recovery.get(1))).getCode()); + } + @Test void healthCooldownExcludesRejectedAccount() { + var health=new UpstreamHealth(jdbc);health.record(model,"upstream-test-a",429,100); + assertEquals("upstream-test-b",health.select(model,List.of("upstream-test-a","upstream-test-b"),Set.of(),0)); + assertNull(health.select(model,List.of("upstream-test-a"),Set.of(),0)); + health.record(model,"upstream-test-a",200,50); + assertEquals("upstream-test-a",health.select(model,List.of("upstream-test-a"),Set.of(),0)); + } + @Test void removedPriceCannotFallBackToPreviouslyReadCatalog() { + var stale=new HashMap<>(modelMap);stale.put("pricing",Map.of("unit","CNY / 1M tokens","input",1,"output",2)); + jdbc.update("UPDATE model_catalog SET pricing=NULL WHERE id=?",model); + assertEquals("MODEL_NOT_PRICED",assertThrows(ApiException.class,()->billing.reserve(keyMap,stale,request(),null)).getCode()); + assertEquals(0,jdbc.queryForObject("SELECT count(*) FROM gateway_budget_holds",Integer.class)); + } + @Test void priceEditsAcceptEquivalentNumbersAndRejectStaleVersions() throws Exception { + var auth=mock(AuthService.class);when(auth.requireAdmin(any())).thenReturn(new AuthService.UserView(user,"admin","username","test","super_admin","active",null,null)); + var controller=new PriceManagementController(jdbc,auth,json);var request=new MockHttpServletRequest(); + jdbc.update("UPDATE model_catalog SET pricing=NULL,procurement_pricing=NULL WHERE id=?",model); + var payload=new HashMap();payload.put("pricing",Map.of("unit","CNY / 1M tokens","input",new BigDecimal("2.50"),"output",new BigDecimal("10.00"))); + payload.put("expectedPricing",null);payload.put("expectedProcurementPricing",null); + controller.save(model,payload,request); + assertEquals("PRICE_CHANGED",assertThrows(ApiException.class,()->controller.save(model,payload,request)).getCode()); + payload.put("expectedPricing",Map.of("unit","CNY / 1M tokens","input",2.5,"output",10)); + payload.put("pricing",Map.of("unit","CNY / 1M tokens","input",3,"output",10)); + controller.save(model,payload,request); + assertEquals(new BigDecimal("3"),jdbc.queryForObject("SELECT (pricing->>'input')::numeric FROM model_catalog WHERE id=?",BigDecimal.class,model)); + } + @Test void alertCooldownAndRecoveryPersistWithoutSendingMail() { + jdbc.update("UPDATE ops_alert_rules SET minimum_requests=1,error_percent=50"); + var ticket=billing.reserve(keyMap,modelMap,request(),null);billing.fail(ticket,"TEST","isolated failure",1); + var ops=new OperationsController(jdbc,mock(AuthService.class));ops.evaluate();ops.evaluate(); + assertEquals(1,jdbc.queryForObject("SELECT count(*) FROM ops_alert_events",Integer.class)); + assertEquals("disabled",jdbc.queryForObject("SELECT notification_status FROM ops_alert_events",String.class)); + jdbc.update("UPDATE usage_events SET updated_at=now()-interval '1 day'");ops.evaluate(); + assertEquals("resolved",jdbc.queryForObject("SELECT status FROM ops_alert_events",String.class)); + } +} diff --git a/control-plane-java/src/test/java/com/moyi/controlplane/gateway/GatewayDeploymentTest.java b/control-plane-java/src/test/java/com/moyi/controlplane/gateway/GatewayDeploymentTest.java index b770a19..b71684b 100644 --- a/control-plane-java/src/test/java/com/moyi/controlplane/gateway/GatewayDeploymentTest.java +++ b/control-plane-java/src/test/java/com/moyi/controlplane/gateway/GatewayDeploymentTest.java @@ -20,6 +20,19 @@ import static org.mockito.ArgumentMatchers.*; import static org.mockito.Mockito.*; class GatewayDeploymentTest { + @Test void failedTaskClaimReleasesRedisLeaseAfterDatabaseRollback() { + 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(); + when(policies.taskLimit(key)).thenReturn(new EnterprisePolicies.UserLimit(user,120,16)); + var guard=new GatewayGuard(redis,60,300,4,"moyi:test:");guard.setEnterprisePolicies(policies); + org.springframework.transaction.support.TransactionSynchronizationManager.initSynchronization(); + try { + assertTrue(guard.tryTaskUserSlot(key,"isolated-task",3600000)); + clearInvocations(redis); + for(var callback:org.springframework.transaction.support.TransactionSynchronizationManager.getSynchronizations()) callback.afterCompletion(org.springframework.transaction.support.TransactionSynchronization.STATUS_ROLLED_BACK); + verify(redis).execute(any(RedisScript.class),eq(List.of("moyi:test:gateway:slots:user:"+user)),eq("isolated-task")); + } finally {org.springframework.transaction.support.TransactionSynchronizationManager.clearSynchronization();} + } @Test void capacitySnapshotReadsBothLiveAndWaitingBucketsAndDoesNotInventZeroOnFailure() { StringRedisTemplate redis = mock(StringRedisTemplate.class); @@ -221,7 +234,7 @@ class GatewayDeploymentTest { UsageAuditService usage = mock(UsageAuditService.class); Map key = Map.of("id", UUID.randomUUID().toString(), "userId", UUID.randomUUID().toString()); when(catalog.authenticateKey("test-key")).thenReturn(key); - when(catalog.activeModel("model-real")).thenReturn(Map.of("id", "model-real", "providerId", "provider-deepseek", "mode", "sync")); + 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); GatewayController controller = new GatewayController(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())); @@ -235,7 +248,7 @@ class GatewayDeploymentTest { CatalogService catalog = mock(CatalogService.class); Map key = Map.of("id", UUID.randomUUID().toString(), "userId", UUID.randomUUID().toString()); when(catalog.authenticateKey("test-key")).thenReturn(key); - when(catalog.activeModel("mock")).thenReturn(Map.of("id", "mock", "providerId", "provider-mock", "mode", "sync")); + when(catalog.activeModel("mock")).thenReturn(Map.of("id", "mock", "providerId", "provider-mock", "mode", "sync", "type", "text")); when(catalog.keyCanInvoke(key, "mock")).thenReturn(true); 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))); @@ -252,7 +265,7 @@ class GatewayDeploymentTest { ChatBillingService billing = mock(ChatBillingService.class); String userId = UUID.randomUUID().toString(); Map key = Map.of("id", UUID.randomUUID().toString(), "userId", userId); - Map model = Map.of("id", "reasoning-route", "providerId", "provider-relay", "upstreamModel", "reasoning-model", "mode", "sync"); + Map model = Map.of("id", "reasoning-route", "providerId", "provider-relay", "upstreamModel", "reasoning-model", "mode", "sync", "type", "text"); when(catalog.authenticateKey("test-key")).thenReturn(key); when(catalog.activeModel("reasoning-route")).thenReturn(model); when(catalog.keyCanInvoke(key, "reasoning-route")).thenReturn(true); @@ -284,7 +297,7 @@ class GatewayDeploymentTest { ChatBillingService billing = mock(ChatBillingService.class); String userId = UUID.randomUUID().toString(); Map key = Map.of("id", UUID.randomUUID().toString(), "userId", userId); - Map model = Map.of("id", "empty-route", "providerId", "provider-relay", "upstreamModel", "empty-model", "mode", "sync"); + Map model = Map.of("id", "empty-route", "providerId", "provider-relay", "upstreamModel", "empty-model", "mode", "sync", "type", "text"); when(catalog.authenticateKey("test-key")).thenReturn(key); when(catalog.activeModel("empty-route")).thenReturn(model); when(catalog.keyCanInvoke(key, "empty-route")).thenReturn(true); diff --git a/control-plane-java/src/test/java/com/moyi/controlplane/gateway/GatewayFailoverTest.java b/control-plane-java/src/test/java/com/moyi/controlplane/gateway/GatewayFailoverTest.java new file mode 100644 index 0000000..b051628 --- /dev/null +++ b/control-plane-java/src/test/java/com/moyi/controlplane/gateway/GatewayFailoverTest.java @@ -0,0 +1,32 @@ +package com.moyi.controlplane.gateway; +import com.moyi.controlplane.catalog.CatalogService; +import com.moyi.controlplane.common.ApiException; +import com.moyi.controlplane.provider.*; +import com.moyi.controlplane.wallet.ChatBillingService; +import java.util.*; +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Mockito.*; +import static org.mockito.ArgumentMatchers.*; +class GatewayFailoverTest { + Map route(String id,String logical) {return Map.of("id",id,"logicalModelId",logical,"providerId","p", "upstreamModel","same-model","type","text","mode","sync","capabilities",Map.of("failover",true));} + @Test void onlyAuthorizedEquivalentRoutesAreSelectedAndRetailTicketIsReused() { + var catalog=mock(CatalogService.class);var provider=mock(OpenAiCompatibleProvider.class);var guard=mock(GatewayGuard.class);var billing=mock(ChatBillingService.class); + Map primary=route("one","logical"),backup=route("two","logical"),forbidden=route("no-access","logical"),different=route("other","other-model"); + var key=Map.of("id",UUID.randomUUID());when(catalog.listModels(false)).thenReturn(List.of(primary,backup,forbidden,different)); + when(catalog.keyCanInvoke(eq(key),anyString())).thenAnswer(call->!call.getArgument(1).equals("no-access"));when(provider.isConfigured(anyString(),anyString(),anyString())).thenReturn(true); + var service=new GatewayFailover(catalog,guard,billing,provider);assertEquals(List.of(primary,backup),service.candidates(key,primary,"chat")); + var ticket=new ChatBillingService.Ticket(UUID.randomUUID(),UUID.randomUUID(),ChatMeter.rates(null,true)); + when(provider.chat(eq("p"),eq("same-model"),eq("one"),anyMap())).thenThrow(new UpstreamRejectedException(new ApiException("UPSTREAM_RATE_LIMITED","rejected",429))); + when(provider.chat(eq("p"),eq("same-model"),eq("two"),anyMap())).thenReturn(Map.of("ok",true)); + assertEquals(Map.of("ok",true),service.invoke(key,primary,"chat",Map.of(),ticket));verify(billing).actualRoute(ticket,backup);verify(billing,never()).reserve(any(),any(),any(),any()); + } + @Test void ambiguousTimeoutIsNeverRetried() { + var catalog=mock(CatalogService.class);var provider=mock(OpenAiCompatibleProvider.class);var guard=mock(GatewayGuard.class);var billing=mock(ChatBillingService.class); + Map primary=route("one","logical"),backup=route("two","logical");var key=Map.of("id",UUID.randomUUID()); + when(catalog.listModels(false)).thenReturn(List.of(primary,backup));when(catalog.keyCanInvoke(anyMap(),anyString())).thenReturn(true);when(provider.isConfigured(anyString(),anyString(),anyString())).thenReturn(true); + when(provider.chat(eq("p"),eq("same-model"),eq("one"),anyMap())).thenThrow(new ApiException("UPSTREAM_PROVIDER_TIMEOUT","unknown",504)); + assertThrows(ApiException.class,()->new GatewayFailover(catalog,guard,billing,provider).invoke(key,primary,"chat",Map.of(),null)); + verify(provider,never()).chat(eq("p"),eq("same-model"),eq("two"),anyMap()); + } +} diff --git a/control-plane-java/src/test/java/com/moyi/controlplane/gateway/NativeProtocolMeterTest.java b/control-plane-java/src/test/java/com/moyi/controlplane/gateway/NativeProtocolMeterTest.java new file mode 100644 index 0000000..e8d1589 --- /dev/null +++ b/control-plane-java/src/test/java/com/moyi/controlplane/gateway/NativeProtocolMeterTest.java @@ -0,0 +1,32 @@ +package com.moyi.controlplane.gateway; +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.*; +import java.util.*; +import com.fasterxml.jackson.databind.ObjectMapper; +class NativeProtocolMeterTest { + @Test void claudeCacheTokensAreAddedOnceAndHourlyWritesPreserved() { + var body=Map.of("stop_reason","end_turn","usage",Map.of("input_tokens",10,"output_tokens",20,"cache_read_input_tokens",100,"cache_creation_input_tokens",50,"cache_creation",Map.of("ephemeral_1h_input_tokens",30,"ephemeral_5m_input_tokens",20))); + var tokens=ChatMeter.tokens(NativeProtocolMeter.normalize("messages",body)); + assertEquals(new ChatMeter.Tokens(160,20,100,20,30),tokens); + } + @Test void geminiThoughtTokensAreBillableOutput() { + var tokens=ChatMeter.tokens(NativeProtocolMeter.normalize("gemini",Map.of("candidates",List.of(Map.of("finishReason","STOP")),"usageMetadata",Map.of("promptTokenCount",100,"candidatesTokenCount",20,"thoughtsTokenCount",30)))); + assertEquals(50,tokens.completion()); + } + @Test void messagesStreamRequiresStopAndMergesCumulativeUsage() { + var collector=new NativeProtocolMeter.Collector("messages"); + collector.accept(Map.of("type","message_start","message",Map.of("usage",Map.of("input_tokens",100,"output_tokens",1)))); + collector.accept(Map.of("type","message_delta","delta",Map.of("stop_reason","tool_use"),"usage",Map.of("output_tokens",25))); + assertThrows(RuntimeException.class,collector::result); + collector.accept(Map.of("type","message_stop")); + assertEquals(25,ChatMeter.tokens(collector.result()).completion()); + } + @Test void responsesStateCannotCrossCustomerBoundary() { + assertThrows(RuntimeException.class,()->NativeProtocolMeter.request("responses",Map.of("input","hello","previous_response_id","resp_other_customer"),new ObjectMapper())); + assertEquals(false,NativeProtocolMeter.request("responses",Map.of("input","hello"),new ObjectMapper()).body().get("store")); + } + @Test void embeddingsHaveNoOutputCharge() { + assertEquals(0,ChatMeter.tokens(NativeProtocolMeter.normalize("embeddings",Map.of("data",List.of(Map.of("embedding",List.of(0.1))),"usage",Map.of("prompt_tokens",5)))).completion()); + assertThrows(RuntimeException.class,()->NativeProtocolMeter.normalize("embeddings",Map.of("data",List.of(Map.of("error","no vector")),"usage",Map.of("prompt_tokens",5)))); + } +} diff --git a/control-plane-java/src/test/java/com/moyi/controlplane/gateway/TokenPriceScheduleTest.java b/control-plane-java/src/test/java/com/moyi/controlplane/gateway/TokenPriceScheduleTest.java new file mode 100644 index 0000000..c369626 --- /dev/null +++ b/control-plane-java/src/test/java/com/moyi/controlplane/gateway/TokenPriceScheduleTest.java @@ -0,0 +1,30 @@ +package com.moyi.controlplane.gateway; +import java.math.BigDecimal; +import java.util.*; +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.*; + +class TokenPriceScheduleTest { + private Map base() { return new HashMap<>(Map.of("unit","CNY / 1M tokens","input",2,"output",8,"cachedInput",0.2,"cacheWrite5m",2.5,"cacheWrite1h",4)); } + @Test void inputIncludesCacheButEachCategoryIsBilledOnlyOnce() { + var rates=ChatMeter.rates(base(),false); + assertEquals(new BigDecimal("0.00287000"),rates.cost(new ChatMeter.Tokens(1000,100,100,100,100))); + } + @Test void tierBoundaryIsStrictAndSnapshotIsIndependent() { + var raw=base(); raw.put("tiers",List.of(Map.of("aboveInputTokens",1000,"input",4,"output",12))); + var rates=ChatMeter.rates(raw,false); raw.put("input",999); + assertEquals(new BigDecimal("0.00200000"),rates.cost(1000,0,0)); + assertEquals(new BigDecimal("0.00400400"),rates.cost(1001,0,0)); + assertEquals(new BigDecimal("0.01600000"),rates.hold(1000,1000)); + assertEquals(rates.cost(1001,0,0),ChatMeter.rates(rates.snapshot(),false).cost(1001,0,0)); + } + @Test void rejectsAmbiguousTiersAndInvalidCacheBreakdown() { + var raw=base(); raw.put("tiers",List.of(Map.of("aboveInputTokens",1000,"input",3),Map.of("aboveInputTokens",1000,"output",5))); + assertThrows(RuntimeException.class,()->ChatMeter.rates(raw,false)); + assertThrows(RuntimeException.class,()->ChatMeter.tokens(Map.of("usage",Map.of("prompt_tokens",10,"completion_tokens",1,"cache_creation_input_tokens",11)))); + } + @Test void explicitZeroCacheWritePriceRemainsFree() { + var raw=base(); raw.put("cacheWrite1h",0); + assertEquals(new BigDecimal("0.00000000"),ChatMeter.rates(raw,false).cost(new ChatMeter.Tokens(1000,0,0,0,1000))); + } +} diff --git a/control-plane-java/src/test/java/com/moyi/controlplane/provider/NativeProviderTest.java b/control-plane-java/src/test/java/com/moyi/controlplane/provider/NativeProviderTest.java new file mode 100644 index 0000000..3947bcf --- /dev/null +++ b/control-plane-java/src/test/java/com/moyi/controlplane/provider/NativeProviderTest.java @@ -0,0 +1,40 @@ +package com.moyi.controlplane.provider; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.moyi.controlplane.security.SecretCipher; +import com.sun.net.httpserver.HttpServer; +import java.net.InetSocketAddress; +import java.nio.charset.StandardCharsets; +import java.util.*; +import java.util.concurrent.atomic.AtomicInteger; +import org.junit.jupiter.api.Test; +import org.springframework.mock.env.MockEnvironment; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.test.util.ReflectionTestUtils; +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Mockito.*; +class NativeProviderTest { + @Test void nativeEndpointsKeepToolsHeadersAndSseEvents() throws Exception { + var server=HttpServer.create(new InetSocketAddress("127.0.0.1",0),0);var json=new ObjectMapper();var seen=new ArrayList(); + server.createContext("/",exchange->{ + seen.add(exchange.getRequestURI().toString());var body=json.readTree(exchange.getRequestBody()); + assertEquals("Bearer test-upstream",exchange.getRequestHeaders().getFirst("Authorization")); + if(exchange.getRequestURI().getPath().endsWith("messages")) {assertEquals("2023-06-01",exchange.getRequestHeaders().getFirst("anthropic-version"));assertEquals("function",body.path("tools").get(0).path("type").asText());} + byte[] output=(body.path("stream").asBoolean()?"event: message_stop\ndata: {\"type\":\"message_stop\"}\n\n":"{\"usage\":{\"prompt_tokens\":5},\"data\":[{\"embedding\":[0.1]}]}").getBytes(StandardCharsets.UTF_8); + exchange.getResponseHeaders().set("Content-Type",body.path("stream").asBoolean()?"text/event-stream":"application/json");exchange.sendResponseHeaders(200,output.length);exchange.getResponseBody().write(output);exchange.close(); + });server.start(); + var env=new MockEnvironment().withProperty("moyi.providers.deepseek.api-key","test-upstream").withProperty("moyi.providers.deepseek.base-url","http://127.0.0.1:"+server.getAddress().getPort()+"/v1"); + var provider=new OpenAiCompatibleProvider(json,env,mock(JdbcTemplate.class),new SecretCipher("test")); + try {provider.invoke("provider-deepseek","embedding","route",Map.of("input","hello"),"embeddings"); + try(var stream=provider.nativeStream("provider-deepseek","claude","route",Map.of("tools",List.of(Map.of("type","function"))),"messages")) {var chunks=new ArrayList>();stream.events(chunks::add,false);assertEquals("message_stop",chunks.getFirst().get("type"));} + provider.invoke("provider-deepseek","gemini-model","route",Map.of("contents",List.of()),"gemini"); + assertEquals(List.of("/v1/embeddings","/v1/messages","/v1beta/models/gemini-model:generateContent"),seen); + } finally {provider.close();server.stop(0);} + } + @Test void rejectedKeyIsReplacedButSuccessfulRequestIsNotDuplicated() throws Exception { + var calls=new AtomicInteger();var server=HttpServer.create(new InetSocketAddress("127.0.0.1",0),0); + server.createContext("/v1/chat/completions",exchange->{calls.incrementAndGet();boolean reject=exchange.getRequestHeaders().getFirst("Authorization").equals("Bearer first");byte[] output=(reject?"{\"error\":\"rate limit\"}":"{\"choices\":[]}").getBytes(StandardCharsets.UTF_8);exchange.sendResponseHeaders(reject?429:200,output.length);exchange.getResponseBody().write(output);exchange.close();});server.start(); + var env=new MockEnvironment().withProperty("moyi.providers.deepseek.api-key","first").withProperty("MOYI_UPSTREAM_API_KEYS_ROUTE_ROUTE_1","first|second").withProperty("moyi.providers.deepseek.base-url","http://127.0.0.1:"+server.getAddress().getPort()+"/v1");var provider=new OpenAiCompatibleProvider(new ObjectMapper(),env,mock(JdbcTemplate.class),new SecretCipher("test")); + ReflectionTestUtils.setField(provider,"health",new UpstreamHealth(mock(JdbcTemplate.class))); + try {provider.chat("provider-deepseek","model","route-1",Map.of());assertEquals(2,calls.get());} finally {provider.close();server.stop(0);} + } +} diff --git a/control-plane-java/src/test/java/com/moyi/controlplane/task/AsyncTaskPostgresTest.java b/control-plane-java/src/test/java/com/moyi/controlplane/task/AsyncTaskPostgresTest.java index 1eced73..252e467 100644 --- a/control-plane-java/src/test/java/com/moyi/controlplane/task/AsyncTaskPostgresTest.java +++ b/control-plane-java/src/test/java/com/moyi/controlplane/task/AsyncTaskPostgresTest.java @@ -46,8 +46,10 @@ class AsyncTaskPostgresTest { jdbc = new JdbcTemplate(source); usage = spy(new UsageAuditService(jdbc, json)); provider = mock(AsyncProviderClient.class); + GatewayGuard guard=mock(GatewayGuard.class); + when(guard.tryTaskUserSlot(any(),anyString(),anyLong())).thenReturn(true); tasks = new AsyncTaskService(jdbc, json, mock(CatalogService.class), 100, 4, 4, 120_000, - mock(GatewayGuard.class), usage, provider, new DataSourceTransactionManager(source)); + guard, usage, provider, new DataSourceTransactionManager(source)); jdbc.update("INSERT INTO users(id,account,account_kind,password_hash,display_name) VALUES (?,'async-test','username','not-a-password','Test')", userId); jdbc.update("INSERT INTO platform_api_keys(id,user_id,name,key_prefix,key_hash) VALUES (?,?,'Test','test-prefix',?)", keyId, userId, "b".repeat(64)); jdbc.update("INSERT INTO user_wallets(user_id,balance,reserved_balance) VALUES (?,10,1.44)", userId); @@ -64,8 +66,9 @@ class AsyncTaskPostgresTest { ReflectionTestUtils.invokeMethod(tasks,"failTask",taskId,"PROVIDER_NOT_CONFIGURED","异步渠道尚未配置 API Key"); } private void poll() { - Map row = jdbc.queryForMap("SELECT id,provider_task_id,provider_deadline_at,created_at FROM model_tasks WHERE id=?", taskId); + Map row = jdbc.queryForMap("SELECT id,model_id,provider_task_id,provider_deadline_at,created_at,provider_key_fingerprint FROM model_tasks WHERE id=?", taskId); row.put("provider_id", "provider-vast"); + row.put("upstream_model","v3.1-20260211"); ReflectionTestUtils.invokeMethod(tasks,"pollProviderTask",row); } private BigDecimal wallet(String column) { @@ -82,7 +85,7 @@ class AsyncTaskPostgresTest { @ValueSource(strings={"PROVIDER_NOT_CONFIGURED", "UPSTREAM_AUTH_FAILED"}) void submissionFailureReleasesExactlyOnceWithoutDebitingBalance(String code) { jdbc.update("UPDATE model_tasks SET status='queued',provider_task_id=NULL WHERE id=?",taskId); - when(provider.submit(anyString(),anyString(),anyString(),anyString())).thenThrow(new ApiException(code,"上游任务未接受",503)); + when(provider.submit(anyString(),anyString(),anyString(),anyString(),anyString())).thenThrow(new ApiException(code,"上游任务未接受",503)); tasks.completeMockTasks(); tasks.completeMockTasks(); assertEquals("failed",status()); @@ -91,7 +94,7 @@ class AsyncTaskPostgresTest { assertEquals(0,cost().signum()); assertEquals(0,jdbc.queryForObject("SELECT cost_amount FROM usage_events WHERE provider_task_id=?",BigDecimal.class,taskId).signum()); assertEquals(0,jdbc.queryForObject("SELECT count(*) FROM wallet_entries WHERE user_id=?",Integer.class,userId)); - verify(provider,times(1)).submit(anyString(),anyString(),anyString(),anyString()); + verify(provider,times(1)).submit(anyString(),anyString(),anyString(),anyString(),anyString()); } @Test void refundAndTaskStateRollBackTogetherWhenAuditFails() { @@ -108,7 +111,7 @@ class AsyncTaskPostgresTest { } @Test void successfulSettlementRollsBackForRetryAndCannotBeRefundedByLateFailure() { - when(provider.poll("provider-vast","upstream-task")).thenReturn(Map.of("data",Map.of("status","success","progress",100,"output",Map.of("model_url","https://example.test/model.glb")))); + when(provider.poll("provider-vast","model-mock-3d","v3.1-20260211","upstream-task",null)).thenReturn(Map.of("data",Map.of("status","success","progress",100,"output",Map.of("model_url","https://example.test/model.glb")))); doThrow(new IllegalStateException("simulated database write failure")).when(usage).updateTask(eq(taskId),eq("succeeded"),anyMap(),isNull(),isNull()); assertThrows(IllegalStateException.class,this::poll); assertEquals("running",status()); @@ -128,7 +131,7 @@ class AsyncTaskPostgresTest { @ParameterizedTest @ValueSource(strings={"UPSTREAM_PROVIDER_ERROR", "UPSTREAM_AUTH_FAILED", "UPSTREAM_RATE_LIMITED"}) void pollingErrorsKeepTheHoldAndClearOnRecovery(String code) { - when(provider.poll("provider-vast","upstream-task")) + when(provider.poll("provider-vast","model-mock-3d","v3.1-20260211","upstream-task",null)) .thenThrow(new ApiException(code,"private upstream details",502)) .thenReturn(Map.of("data",Map.of("status","running","progress",75))); poll(); @@ -160,7 +163,7 @@ class AsyncTaskPostgresTest { } @Test void confirmedUpstreamFailureReleasesHoldImmediately() { - when(provider.poll("provider-vast","upstream-task")).thenReturn(Map.of("data",Map.of("status","failed"))); + when(provider.poll("provider-vast","model-mock-3d","v3.1-20260211","upstream-task",null)).thenReturn(Map.of("data",Map.of("status","failed"))); poll(); assertEquals("failed",status()); assertEquals(0,wallet("reserved_balance").signum()); @@ -173,13 +176,13 @@ class AsyncTaskPostgresTest { OffsetDateTime migratedDeadline = jdbc.queryForObject("SELECT provider_deadline_at FROM model_tasks WHERE id=?",OffsetDateTime.class,taskId); assertEquals(Duration.ofHours(1),Duration.between(createdAt,migratedDeadline)); jdbc.update("UPDATE model_tasks SET status='queued',provider_task_id=NULL,provider_deadline_at=NULL WHERE id=?",taskId); - when(provider.submit(anyString(),anyString(),anyString(),anyString())).thenReturn(Map.of("data",Map.of("task_id","upstream-task"))); + when(provider.submit(anyString(),anyString(),anyString(),anyString(),anyString())).thenReturn(Map.of("data",Map.of("task_id","upstream-task"))); tasks.completeMockTasks(); OffsetDateTime submittedAt = jdbc.queryForObject("SELECT updated_at FROM model_tasks WHERE id=?",OffsetDateTime.class,taskId); OffsetDateTime deadline = jdbc.queryForObject("SELECT provider_deadline_at FROM model_tasks WHERE id=?",OffsetDateTime.class,taskId); assertEquals(Duration.ofMinutes(2),Duration.between(submittedAt,deadline)); tasks.completeMockTasks(); - verify(provider,times(1)).submit(anyString(),anyString(),anyString(),anyString()); + verify(provider,times(1)).submit(anyString(),anyString(),anyString(),anyString(),anyString()); verify(provider,never()).poll(anyString(),anyString()); assertEquals("running",status()); } diff --git a/control-plane-java/src/test/java/com/moyi/controlplane/usage/UsageCapacityPostgresTest.java b/control-plane-java/src/test/java/com/moyi/controlplane/usage/UsageCapacityPostgresTest.java index 7de776b..dc9246f 100644 --- a/control-plane-java/src/test/java/com/moyi/controlplane/usage/UsageCapacityPostgresTest.java +++ b/control-plane-java/src/test/java/com/moyi/controlplane/usage/UsageCapacityPostgresTest.java @@ -51,6 +51,7 @@ class UsageCapacityPostgresTest { @Test void liveCapacitySeparatesQueuedWorkAndKeepsEveryGroupAndLongRunningTask() { GatewayGuard guard = mock(GatewayGuard.class); when(guard.defaultModelConcurrency()).thenReturn(7); + when(guard.modelConcurrencyEnabled()).thenReturn(true); when(guard.defaultKeyConcurrency()).thenReturn(3); when(guard.queueWaitMs()).thenReturn(4500L); when(guard.capacitySnapshot(anyList())).thenAnswer(invocation -> { diff --git a/docs/19-enterprise-gateway-increment.md b/docs/19-enterprise-gateway-increment.md new file mode 100644 index 0000000..245c76f --- /dev/null +++ b/docs/19-enterprise-gateway-increment.md @@ -0,0 +1,69 @@ +# 企业网关增量建设(2026-10-13) + +用户确认:在现有 MOYI 系统补齐 Sub2API 对比中发现的能力,保留 Java/Vue、账户、余额、权限、历史记录及发布体系。当前服务 B/G 端,通过合同对公付款;支付及订阅能力后置。现有管理员入账与审计流程继续使用。 + +## 本轮验收范围 + +- [x] 可用状态与真实适配器一致;标准 Embeddings 接口及按输入用量结算。 +- [x] Messages、Responses、Gemini 原生协议入口,保留工具调用及流式事件;不支持的上游协议明确拒绝。 +- [x] 渠道账号健康、暂时隔离、安全重试、会话绑定;跨渠道切换不得越过模型授权或改变本次用户售价。 +- [x] 版本化价格快照,缓存写入、上下文阶梯;采购成本独立于用户扣费,未知成本不得当零成本。 +- [x] 调用中断及用量缺失进入成本待核状态,支持人工对账与审计,避免重复扣费。 +- [x] 用户总并发及请求限额,Key 到期/IP/总额和周期预算,所有协议共用准入与扣费限制。 +- [x] 运营健康及错误率告警、持久化状态、冷却与通知配置。 +- [x] TOTP 双因素认证、重放保护、恢复流程;后台及用户设置可操作。 +- [x] 新增 V35–V40 迁移;单元与隔离 PostgreSQL、Redis 验证及前端类型检查通过。 + +## 后置范围 + +自助充值(支付宝/微信/Stripe/易支付)、用户付费套餐与订阅账号接入。本轮不要求用户提供商户或订阅账号凭据。 + +## 实施原则 + +以当前生产提交 512ca522(维护文档提交 1528b718)为起点,新增迁移,不修改已执行的迁移。所有金额采用人民币 Decimal/BigDecimal,保存调用时的价目快照。故障切换只在已确认未产生输出且允许重试的错误上进行;已经输出的流式响应不重新请求。对外接口和管理入口均保持原有鉴权边界。新增凭据加密存储,不返回给列表、日志或导出。外部源码仅供行为比较,独立实现。 + +## 实施记录 + +2026-10-13:完成上述增量实现,保留现有合同对公付款后的管理员额度入账与审计。 + +## 入口与操作 + +| 入口 | 可操作内容 | +|---|---| +| 管理端 → 企业调用策略 | 用户全部 Key 共用的 RPM、总并发、人民币月预算;默认 120 RPM / 16 并发,预算不限 | +| 用户端 → Key 安全与预算 | 到期时间、IPv4/IPv6/CIDR 白名单、总/日/月预算;周期按北京时间 | +| 管理端 → 价格版本与对账 | 维护人民币/百万 Token 的普通输入、输出、缓存读、5 分钟/1 小时缓存写入、上下文阶梯;采购成本独立填写;保留历史版本 | +| 同一页面 → 本月对账 | 查看用户消费、已知采购成本/利润,按渠道账单填写实际成本和依据;不更改用户扣费 | +| 同一页面 → 原生协议 | 按路由启用 Messages、Responses、Gemini,启用已授权的同模型备用渠道;上线默认不自动打开 | +| 管理端 → 渠道健康与告警 | 账号指纹、响应延迟、冷却状态、错误率阈值及站内告警;邮件通知默认关闭 | +| 账号设置 → 双因素认证 | 密码确认后配置认证器;6 位 TOTP,10 个一次性恢复码只展示一次 | +| 用户端 → 接入文档 | 原生协议请求示例、鉴权及使用限制;在线调试可提交 Embeddings | + +定价单位 `1M tokens` 等于 `1,000,000 tokens`(一百万 Token)。手工调整绝对金额后显示“自定义价格”,避免继续显示失效的旧折扣。原有渠道原价乘折扣的批量定价功能保留。本轮迁移不重算现有售价,不把未知采购成本填成 0。 + +## 计费与可靠性边界 + +- 每次调用冻结售价版本和采购价格;价格编辑使用乐观冲突检查。撤下价格后新请求停止计费准入,已受理请求按原快照结算。 +- 所有 Key 共用用户预算与并发;异步任务也占用户并发,长任务 Redis 租约不会被短请求缩短。预算按已消费和在途预留检查;上游异常超出预留仍按真实用量记账,可能形成欠款并阻止后续调用。 +- 原生协议当前覆盖文本与本地函数工具。Responses 为完整上下文、`store=false`;不支持 `previous_response_id`、云端会话和后台任务。媒体、托管工具、外部缓存引用需要独立计费适配器,当前明确拒绝。 +- 原生接口不会自动假定所有渠道支持。管理员核实渠道能力后按路由启用;本轮使用本地 HTTP 模拟上游验证,没有批量发起收费模型调用。 +- 账号明确返回 401/403/429 时可尝试账号池中其他 Key;跨路由切换需显式开启,仅限 Key 已授权、同逻辑模型/类型/上游模型名的最多 2 个备用路由。仅明确拒绝受理或账号全部冷却可切换;超时、已开始输出的流不重试。 +- `X-Moyi-Session` 提供账号亲和性,纳入调用凭据隔离;不绕过健康、授权或限额。 +- 客户端断开时尽量继续读取最终用量;用量缺失或流不完整不估算用户扣费,释放预留并记录待对账成本。管理员可记录实际渠道损失。 +- IP 白名单使用可信代理解析后的地址;生产 Nginx 覆盖客户端伪造的转发头。直接运行后端时默认不信任转发头。 +- 站内告警每分钟评估;邮件仅管理员启用且 SMTP 已配置时发送,最多尝试 3 次。发送可能因进程中断重复,使用告警编号识别。 +- TOTP 密钥加密,恢复码只存哈希;动态码防重放,错误尝试 5 次后限制 5 分钟。已有用户不会被自动开启 MFA。 + +## 验证记录 + +- 2026-10-13 18:56:`scripts/test_enterprise_increment.py`,118 个 Java 测试全部通过,0 跳过;使用全新本机 PostgreSQL 17,完成后停止。日志:`.tools/enterprise-test-67yhxfz7/tests.log`。 +- Redis Lua 实测通过:共享用户并发、拒绝超额、释放、RPM、长短租约共存;仅使用唯一前缀临时键并清理,不触碰业务键。 +- 部署工具:37 项中 33 项通过;4 项旧凭据维护测试要求单独固定端口 PostgreSQL,未启动该专用环境,明确跳过。真实 Docker Compose 插值和 HTTPS 覆盖测试通过。 +- Vue 类型检查通过;本地隔离浏览器完成登录、企业预算保存、空价格首次配置/重复保存、告警页面、Key 空状态、MFA 设置展示验证。 +- 最终发布包另执行 Maven clean verify、18 项前端测试与 Vite 构建;线上验收结果在发布后补充。 + +## 部署与回退 + +使用 `scripts/package-deployment.ps1` 从干净提交生成 JAR、静态资源和校验清单。`infra/moyi/upgrade_release.py` 先备份现有数据库,再构建并启动同一个 Compose 项目,不执行 `docker compose down`。保留旧镜像和备份。 + +V35–V40 为新增表、列、索引和价格历史触发器。紧急回退优先切回旧应用镜像,保留新增数据库结构;除非明确接受备份后数据丢失,不覆盖恢复生产数据库。旧应用不理解新预算/MFA/原生协议,因此回退后应停止依赖这些新增能力。 diff --git a/infra/moyi/deploy.py b/infra/moyi/deploy.py index 5d4e797..7dd83a8 100644 --- a/infra/moyi/deploy.py +++ b/infra/moyi/deploy.py @@ -30,6 +30,10 @@ def execute(command, capture=False, data=None): # shell stdin. This is especially important when deploy.py is invoked from # an SSH heredoc; only an explicit data payload may be sent to a process. options = {"input": data} if data is not None else {"stdin": subprocess.DEVNULL} + if command[:2] == ["docker", "compose"] and "--env-file" in command: + environment = os.environ.copy() + environment["MOYI_RUNTIME_ENV_FILE"] = str(Path(command[command.index("--env-file") + 1]).resolve()) + options["env"] = environment result = subprocess.run(command, cwd=HERE, capture_output=capture, text=True, encoding="utf-8", errors="replace", **options) if result.returncode: diff --git a/infra/moyi/docker-compose.cloud.yml b/infra/moyi/docker-compose.cloud.yml index 887a926..c14e547 100644 --- a/infra/moyi/docker-compose.cloud.yml +++ b/infra/moyi/docker-compose.cloud.yml @@ -12,7 +12,7 @@ services: # Pass per-route key pools from the deployment secret file without # committing secret names or values to the repository. env_file: - - .env.cloud + - ${MOYI_RUNTIME_ENV_FILE:-.env.cloud} environment: CONTROL_PLANE_HOST: 0.0.0.0 CONTROL_PLANE_PORT: 8787 @@ -28,6 +28,7 @@ services: MOYI_REDIS_SSL: ${MOYI_REDIS_SSL:-false} MOYI_REDIS_KEY_PREFIX: ${MOYI_REDIS_KEY_PREFIX:-moyi:test:} FLYWAY_ENABLED: true + MOYI_FORWARD_HEADERS_STRATEGY: native FLYWAY_BASELINE_ON_MIGRATE: false AUTH_SECURE_COOKIE: false AUTH_PASSWORD_PEPPER: ${AUTH_PASSWORD_PEPPER:?Initialize auth secrets} diff --git a/infra/moyi/docker-compose.yml b/infra/moyi/docker-compose.yml index e235922..5a82b96 100644 --- a/infra/moyi/docker-compose.yml +++ b/infra/moyi/docker-compose.yml @@ -43,6 +43,7 @@ services: SPRING_DATASOURCE_PASSWORD: ${POSTGRES_PASSWORD} SPRING_DATA_REDIS_URL: redis://:${REDIS_PASSWORD}@redis:6379/0 FLYWAY_ENABLED: true + MOYI_FORWARD_HEADERS_STRATEGY: native depends_on: postgres: condition: service_healthy diff --git a/infra/moyi/nginx/routes.conf b/infra/moyi/nginx/routes.conf index 0beaf8d..e5637d9 100644 --- a/infra/moyi/nginx/routes.conf +++ b/infra/moyi/nginx/routes.conf @@ -23,7 +23,7 @@ location /api/ { proxy_read_timeout 120s; proxy_next_upstream off; } -location /v1/ { +location ~ ^/v1(?:beta)?/ { proxy_pass http://moyi-api:8787; proxy_http_version 1.1; proxy_set_header Host $http_host; diff --git a/infra/moyi/tests/test_deploy.py b/infra/moyi/tests/test_deploy.py index 8a63809..52e99ff 100644 --- a/infra/moyi/tests/test_deploy.py +++ b/infra/moyi/tests/test_deploy.py @@ -229,7 +229,8 @@ class DeploymentTests(unittest.TestCase): self.assertEqual(api["SPRING_DATASOURCE_PASSWORD"], "test-db-$value#literal") self.assertEqual(api["ADMIN_PASSWORD"], "Qa-Test42!") self.assertNotIn("MOYI_REDIS_PASSWORD", cfg["services"]["frontend"].get("environment", {})) - self.assertNotIn("OSS_ACCESS_KEY_SECRET", api) + self.assertFalse(api.get("OSS_ACCESS_KEY_SECRET")) + self.assertNotIn("OSS_ACCESS_KEY_SECRET", cfg["services"]["frontend"].get("environment", {})) if mode == "https": ports = cfg["services"]["frontend"]["ports"] self.assertEqual({str(p["published"]) for p in ports}, {"80", "443", "8080"}) diff --git a/prototype/src/components/AccountSettings.vue b/prototype/src/components/AccountSettings.vue index 600585a..22f9106 100644 --- a/prototype/src/components/AccountSettings.vue +++ b/prototype/src/components/AccountSettings.vue @@ -2,6 +2,7 @@ import { reactive, ref } from 'vue' import { authClient, type AuthUser } from '../services/auth' import UiModal from './UiModal.vue' +import MfaSettings from './MfaSettings.vue' const props = defineProps<{ user: AuthUser }>() const emit = defineEmits<{ close: []; saved: [user: AuthUser]; notify: [message: string] }>() @@ -47,6 +48,7 @@ async function save() {

普通用户新密码至少 8 位;超级管理员按实际设置保存。

+