feat: extend enterprise gateway policies billing protocols and operations
This commit is contained in:
1 parent
1528b71882
commit
af43947b5d
67 files changed
+1822
-69
No files matched your search
@@ -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());
|
||||
|
||||
@@ -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(
|
||||
|
||||
@@ -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<String,Object> status(HttpServletRequest request) {return totp.status(auth.requireUser(AuthController.sessionToken(request)).id());}
|
||||
@PostMapping("/setup") public Map<String,Object> 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<String,Object> 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<String,Object> 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);
|
||||
}
|
||||
}
|
||||
@@ -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<String,Object> 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<String,Object> 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<String> 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<String>();
|
||||
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<String,Object> 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<String,Object> 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);}}
|
||||
}
|
||||
@@ -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<String>();
|
||||
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
|
||||
|
||||
+71
@@ -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<String,Object> protocols(@PathVariable String modelId,@RequestBody Map<String,Object> 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<String,Object> 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<String,Object> save(@PathVariable String modelId,@RequestBody Map<String,Object> 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<String,Object> 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<String,Object> reconcile(@PathVariable UUID id,@RequestBody Map<String,Object> 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<String,Object>();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);
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
|
||||
@@ -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<String,Object> snapshot() {
|
||||
return Map.of("input",input,"output",output,"cachedInput",cachedInput,"unit","CNY / 1M tokens");
|
||||
public record Rates(BigDecimal input, BigDecimal output, BigDecimal cachedInput, Map<String,Object> 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<String,Object> 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<String,Object> 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 {
|
||||
|
||||
+1
@@ -83,6 +83,7 @@ final class ChatStreamCollector {
|
||||
return "";
|
||||
}
|
||||
Integer firstTokenMs() { return firstToken; }
|
||||
Object observedUsage() {return usage;}
|
||||
Map<String,Object> result() {
|
||||
if(finish==null) throw new ApiException("UPSTREAM_STREAM_INCOMPLETE","上游未完成输出,本次未扣费",502);
|
||||
if(content.toString().isBlank() && reasoning.toString().isBlank() && !hasToolOutput())
|
||||
|
||||
+86
@@ -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<String> 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<ip.length;i++) {int mask=bits>=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<Object>(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);}
|
||||
}
|
||||
+69
@@ -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<String,Object> 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<String,Object> keyPolicy(@PathVariable UUID keyId,@RequestBody Map<String,Object> 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<String,Object> 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<String,Object> userPolicy(@PathVariable UUID userId,@RequestBody Map<String,Object> 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+" 的整数");}}
|
||||
}
|
||||
+12
-6
@@ -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<String, Object> model = resolveModel(key, modelId);
|
||||
if (model == null) throw new ApiException("MODEL_NOT_FOUND", "模型不存在或已下线;请使用当前 API Key 的 /v1/models 返回的模型 ID", 404);
|
||||
if ("async".equals(model.get("mode"))) throw new ApiException("ASYNC_MODEL", "该模型需要使用异步任务接口", 400);
|
||||
if(!List.of("chat","text","code","multimodal","llm","reasoning","vision").contains(String.valueOf(model.get("type")))) throw new ApiException("WRONG_MODEL_INTERFACE","该模型需要使用模型目录标明的接口",400);
|
||||
String providerId=String.valueOf(model.get("providerId"));
|
||||
String routeId=String.valueOf(model.get("id"));
|
||||
boolean mock="provider-mock".equals(providerId);
|
||||
@@ -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<String,Object> body=mock?mockResponse(modelId,input):provider.chat(providerId,String.valueOf(model.get("upstreamModel")),routeId,request.body());
|
||||
Map<String,Object> body=mock?mockResponse(modelId,input):failover!=null?failover.invoke(key,model,"chat",request.body(),ticket):provider.chat(providerId,String.valueOf(model.get("upstreamModel")),routeId,request.body());
|
||||
body.put("model",modelId);
|
||||
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<String,Object> model, String modelId, String routeId, Map<String,Object> input, ChatBillingService.Ticket ticket, HttpServletResponse response, long started, boolean mock) throws IOException {
|
||||
private void stream(Map<String,Object> key,Map<String,Object> model, String modelId, String routeId, Map<String,Object> input, ChatBillingService.Ticket ticket, HttpServletResponse response, long started, boolean mock) throws IOException {
|
||||
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);
|
||||
|
||||
@@ -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<Map<String,Object>> candidates(Map<String,Object> key,Map<String,Object> primary,String protocol) {
|
||||
var routes=new ArrayList<Map<String,Object>>();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<String,Object> invoke(Map<String,Object> key,Map<String,Object> model,String protocol,Map<String,Object> input,ChatBillingService.Ticket ticket) {
|
||||
var routes=candidates(key,model,protocol);
|
||||
for(int i=0;i<routes.size();i++) {
|
||||
var route=routes.get(i);try(var slot=i==0?null:guard.acquire(route,1)) {
|
||||
if(i>0) 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<String,Object> key,Map<String,Object> model,String protocol,Map<String,Object> input,ChatBillingService.Ticket ticket) {
|
||||
var routes=candidates(key,model,protocol);
|
||||
for(int i=0;i<routes.size();i++) {
|
||||
var route=routes.get(i);GatewayGuard.Slot slot=i==0?null:guard.acquire(route,1);
|
||||
try {
|
||||
if(i>0) 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");
|
||||
}
|
||||
}
|
||||
@@ -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<Long> script(String location) {
|
||||
DefaultRedisScript<Long> 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<String, Capacity> 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<String, Object> 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<String,Object> 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();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+92
@@ -0,0 +1,92 @@
|
||||
package com.moyi.controlplane.gateway;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.moyi.controlplane.catalog.CatalogService;
|
||||
import com.moyi.controlplane.common.ApiException;
|
||||
import com.moyi.controlplane.provider.OpenAiCompatibleProvider;
|
||||
import com.moyi.controlplane.wallet.ChatBillingService;
|
||||
import jakarta.servlet.http.*;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.*;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
@RestController
|
||||
public class NativeGatewayController {
|
||||
private final CatalogService catalog; private final GatewayGuard guard;
|
||||
private final ChatBillingService billing; private final OpenAiCompatibleProvider provider; private final ObjectMapper json;
|
||||
@org.springframework.beans.factory.annotation.Autowired private GatewayFailover failover;
|
||||
public NativeGatewayController(CatalogService catalog,GatewayGuard guard,ChatBillingService billing,OpenAiCompatibleProvider provider,ObjectMapper json) {this.catalog=catalog;this.guard=guard;this.billing=billing;this.provider=provider;this.json=json;}
|
||||
@PostMapping({"/v1/embeddings","/v1/messages","/v1/responses"})
|
||||
public ResponseEntity<Map<String,Object>> invoke(@RequestBody Map<String,Object> input,HttpServletRequest request,HttpServletResponse response) {
|
||||
String protocol=request.getRequestURI().substring(request.getRequestURI().lastIndexOf('/')+1);
|
||||
return execute(protocol,input,Boolean.TRUE.equals(input.get("stream")),request,response);
|
||||
}
|
||||
@PostMapping("/v1beta/models/{modelAction}")
|
||||
public ResponseEntity<Map<String,Object>> gemini(@PathVariable String modelAction,@RequestBody Map<String,Object> input,HttpServletRequest request,HttpServletResponse response) {
|
||||
int delimiter=modelAction.lastIndexOf(':');
|
||||
if(delimiter<1) throw new ApiException("INVALID_MODEL","请指定模型和 generateContent 方法",400);
|
||||
String action=modelAction.substring(delimiter+1);
|
||||
if(!Set.of("generateContent","streamGenerateContent").contains(action)) throw new ApiException("UNSUPPORTED_METHOD","方法未接入",404);
|
||||
var body=new LinkedHashMap<String,Object>(input);body.put("model",modelAction.substring(0,delimiter));
|
||||
return execute("gemini",body,"streamGenerateContent".equals(action),request,response);
|
||||
}
|
||||
private ResponseEntity<Map<String,Object>> execute(String protocol,Map<String,Object> input,boolean streaming,HttpServletRequest request,HttpServletResponse response) {
|
||||
String credential=request.getHeader("Authorization");
|
||||
if(credential!=null) credential=credential.startsWith("Bearer ")?credential.substring(7):null;
|
||||
else credential=request.getHeader("gemini".equals(protocol)?"x-goog-api-key":"x-api-key");
|
||||
if(credential==null||credential.isBlank()) throw new ApiException("UNAUTHENTICATED","请提供 MOYI API Key",401);
|
||||
var key=catalog.authenticateKey(credential);if(key==null) throw new ApiException("INVALID_API_KEY","API Key 无效或已停用",401);
|
||||
return executeForKey(protocol,input,streaming,key,request,response);
|
||||
}
|
||||
public ResponseEntity<Map<String,Object>> executeForKey(String protocol,Map<String,Object> input,boolean streaming,Map<String,Object> key,HttpServletRequest request,HttpServletResponse response) {
|
||||
UUID keyId=UUID.fromString(key.get("id").toString());guard.checkRate(keyId,false);
|
||||
String requested=String.valueOf(input.getOrDefault("model",""));
|
||||
var model=GatewayModelIds.resolve(catalog,key,requested);
|
||||
if(model==null) throw new ApiException("MODEL_NOT_FOUND","模型不存在或未授权",404);
|
||||
if(!NativeProtocolMeter.supports(model,protocol)) throw new ApiException("PROTOCOL_NOT_CONFIGURED","该模型路由尚未配置此协议,请使用模型目录标明的接口",400);
|
||||
String providerId=model.get("providerId").toString(),upstream=model.get("upstreamModel").toString(),route=model.get("id").toString();
|
||||
if(!provider.isConfigured(providerId,route,upstream)) throw new ApiException("PROVIDER_NOT_CONFIGURED","渠道凭据未配置",503);
|
||||
var normalized=NativeProtocolMeter.request(protocol,input,json);
|
||||
long start=System.currentTimeMillis();
|
||||
try(var admission=guard.acquire(model,keyId,guard.queueWaitMs())) {
|
||||
var ticket=billing.reserve(key,model,normalized,request.getHeader("Idempotency-Key"));
|
||||
billing.endpoint(ticket.id(),"gemini".equals(protocol)?"/v1beta/models:generateContent":"/v1/"+protocol);
|
||||
response.setHeader("X-Request-Id",ticket.id().toString());
|
||||
try {
|
||||
catalog.touchKey(key);
|
||||
if(!streaming) {
|
||||
var result=failover!=null?failover.invoke(key,model,protocol,normalized.body(),ticket):provider.invoke(providerId,upstream,route,normalized.body(),protocol);
|
||||
if(result.containsKey("model")) result.put("model",requested);
|
||||
billing.observedUsage(ticket,protocol,result.get("gemini".equals(protocol)?"usageMetadata":"usage"));
|
||||
var cost=billing.settle(ticket,NativeProtocolMeter.normalize(protocol,result),elapsed(start),null);
|
||||
return ResponseEntity.ok().header("X-Request-Id",ticket.id().toString()).header("X-Moyi-Cost-CNY",cost.toPlainString()).body(result);
|
||||
}
|
||||
try(var stream=failover!=null?failover.stream(key,model,protocol,normalized.body(),ticket):provider.nativeStream(providerId,upstream,route,normalized.body(),protocol)) {
|
||||
var collector=new NativeProtocolMeter.Collector(protocol);
|
||||
response.setContentType("text/event-stream");response.setCharacterEncoding("UTF-8");response.setHeader("Cache-Control","no-cache");response.setHeader("X-Accel-Buffering","no");
|
||||
boolean[] disconnected={false};Integer[] first={null};
|
||||
try {stream.events(chunk->{
|
||||
if(first[0]==null) first[0]=elapsed(start);
|
||||
collector.accept(chunk);
|
||||
if(!disconnected[0]) try {event(response,chunk);} catch(IOException e) {disconnected[0]=true;}
|
||||
},false);} finally {billing.observedUsage(ticket,protocol,collector.observedUsage());}
|
||||
billing.settle(ticket,collector.result(),elapsed(start),first[0]);
|
||||
}
|
||||
return null;
|
||||
} catch(Exception failure) {
|
||||
var error=failure instanceof ApiException api?api:new ApiException("CALL_INTERRUPTED","调用中断,渠道成本待核对",502);
|
||||
billing.fail(ticket,error.getCode(),error.getMessage(),elapsed(start));
|
||||
if(response.isCommitted()) {try {event(response,Map.of("type","error","error",Map.of("type",error.getCode(),"message",error.getMessage())));} catch(IOException ignored) {}return null;}
|
||||
response.resetBuffer();response.setContentType("application/json");throw error;
|
||||
}
|
||||
}
|
||||
}
|
||||
private int elapsed(long start) {return (int)Math.min(Integer.MAX_VALUE,System.currentTimeMillis()-start);}
|
||||
private void event(HttpServletResponse response,Map<String,Object> chunk) throws IOException {
|
||||
String type=String.valueOf(chunk.getOrDefault("type",""));
|
||||
String prefix=type.matches("[a-zA-Z0-9_.-]{1,100}")?"event: "+type+"\n":"";
|
||||
response.getOutputStream().write((prefix+"data: "+json.writeValueAsString(chunk)+"\n\n").getBytes(StandardCharsets.UTF_8));response.flushBuffer();
|
||||
}
|
||||
}
|
||||
+135
@@ -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<String,Object> 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<String,Object> input,ObjectMapper json) {
|
||||
var body=new LinkedHashMap<String,Object>(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<String,Object> 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<String,Object> map(Object value) {return value instanceof Map<?,?> m?(Map<String,Object>)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<String,Object> map,String field) {return map.get(field)==null?0:count(map.get(field));}
|
||||
public static Map<String,Object> normalize(String protocol,Map<String,Object> response) {
|
||||
if(response.containsKey("error")) throw new ApiException("UPSTREAM_RESPONSE_INVALID","上游返回错误",502);
|
||||
var usage=new LinkedHashMap<String,Object>();
|
||||
Map<String,Object> 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<String,Object>(response);result.put("usage",usage);ChatMeter.tokens(result);return result;
|
||||
}
|
||||
public static final class Collector {
|
||||
private final String protocol;
|
||||
private final Map<String,Object> result=new LinkedHashMap<>(),usage=new LinkedHashMap<>();
|
||||
private final List<Object> content=new ArrayList<>();
|
||||
private boolean complete;
|
||||
public Collector(String protocol) {this.protocol=protocol;}
|
||||
public void accept(Map<String,Object> 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<String,Object> 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);}
|
||||
}
|
||||
+5
@@ -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<Map<String,Object>> embeddings(@PathVariable String keyId,@RequestBody Map<String,Object> input,HttpServletRequest request,HttpServletResponse response) {
|
||||
return nativeGateway.executeForKey("embeddings",input,false,requireKey(request,keyId),request,response);
|
||||
}
|
||||
|
||||
public PlaygroundController(AuthService auth, CatalogService catalog, GatewayController gateway, AsyncTaskService tasks) {
|
||||
this.auth = auth;
|
||||
|
||||
+73
@@ -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<String> 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<String,Object> validate(Map<?,?> raw) {
|
||||
if(!"CNY / 1M tokens".equals(raw.get("unit"))) throw new ApiException("INVALID_PRICE","Token 规则必须使用 CNY / 1M tokens");
|
||||
var copy=new LinkedHashMap<String,Object>(); 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<Map<String,Object>>(); 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<String,Object>(); 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<String,Object> at(Map<String,Object> 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<String,Object> 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<String,Object> schedule,long inputTokens,long maxOutput) {
|
||||
BigDecimal in=BigDecimal.ZERO,out=BigDecimal.ZERO;
|
||||
var all=new ArrayList<Map<?,?>>(); 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);
|
||||
}
|
||||
}
|
||||
+73
-13
@@ -40,6 +40,7 @@ public class OpenAiCompatibleProvider {
|
||||
private final ConcurrentMap<String, AtomicInteger> 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<String,Object> input, boolean streaming) {
|
||||
return openEndpoint(providerId,upstreamModel,routeId,input,streaming,"chat");
|
||||
}
|
||||
|
||||
public Map<String,Object> invoke(String providerId,String model,String routeId,Map<String,Object> 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<String,Object> 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<String,Object> input,String protocol) {
|
||||
return openEndpoint(providerId,model,routeId,input,true,protocol);
|
||||
}
|
||||
private StreamResponse openEndpoint(String providerId,String upstreamModel,String routeId,Map<String,Object> 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<String, Object> 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<String> candidates=provider.routeKeys(routeId,upstreamModel,environment);
|
||||
if(candidates.isEmpty()) candidates=provider.apiKeys();
|
||||
String healthRoute=routeId==null?providerId:routeId;
|
||||
var attempted=new java.util.HashSet<String>();
|
||||
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<InputStream> 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)<timeoutSeconds) {
|
||||
attempted.add(apiKey);String next=health.select(healthRoute,candidates,attempted,0);
|
||||
if(next!=null) {apiKey=next;continue;}
|
||||
}
|
||||
ApiException rejection=classifyUpstreamError(response.statusCode(),details);
|
||||
if(List.of(401,402,403,429).contains(response.statusCode())) throw new UpstreamRejectedException(rejection);
|
||||
throw rejection;
|
||||
}
|
||||
if(streaming && !response.headers().firstValue("Content-Type").orElse("").toLowerCase().startsWith("text/event-stream")) {
|
||||
response.body().close();
|
||||
throw new ApiException("UPSTREAM_STREAM_UNSUPPORTED","上游未返回 SSE 流式响应",502);
|
||||
}
|
||||
return new StreamResponse(response.body(),Math.max(1,timeoutSeconds*1000L-TimeUnit.NANOSECONDS.toMillis(System.nanoTime()-started)));
|
||||
return new StreamResponse(response.body(),Math.max(1,timeoutSeconds*1000L-TimeUnit.NANOSECONDS.toMillis(System.nanoTime()-overallStarted)));
|
||||
} catch (ApiException exception) {
|
||||
throw exception;
|
||||
} catch (HttpTimeoutException | SocketTimeoutException exception) {
|
||||
if(health!=null) health.record(healthRoute,apiKey,504,timeoutSeconds*1000);
|
||||
throw new ApiException("UPSTREAM_PROVIDER_TIMEOUT", "上游模型供应商响应超时", 504);
|
||||
} catch (InterruptedException exception) {
|
||||
Thread.currentThread().interrupt();
|
||||
@@ -178,11 +232,16 @@ public class OpenAiCompatibleProvider {
|
||||
private final InputStream body;
|
||||
private final AtomicBoolean expired=new AtomicBoolean();
|
||||
private final ScheduledFuture<?> 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<String,Object> chunk) throws IOException; }
|
||||
@PreDestroy public void close() { deadlines.shutdownNow(); }
|
||||
|
||||
@@ -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<String> candidates,Set<String> 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);
|
||||
}
|
||||
}
|
||||
+6
@@ -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());}
|
||||
}
|
||||
@@ -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<String, Object> getForKey(Map<String, Object> 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<Map<String, Object>> list(String authorization, int limit) {
|
||||
Map<String, Object> 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<String,Object> 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<String,Object> 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<String,Object> 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<String,Object> 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<String,Object> 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);
|
||||
|
||||
+50
@@ -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_at<now()-make_interval(mins=>cooldown_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<String,Object> 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<String,Object> save(@PathVariable String id,@RequestBody Map<String,Object> 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<String,Object> 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+" 的整数");}}
|
||||
}
|
||||
+29
@@ -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"));}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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()));
|
||||
});
|
||||
|
||||
+30
-6
@@ -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<String,Object> route) {
|
||||
var snapshots=jdbc.queryForList("SELECT procurement_pricing FROM model_price_history WHERE model_id=? ORDER BY id DESC LIMIT 1",route.get("id"));
|
||||
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<String,Object> key, Map<String,Object> 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.<String,Object>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}")
|
||||
|
||||
@@ -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:
|
||||
|
||||
+31
@@ -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');
|
||||
+24
@@ -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);
|
||||
@@ -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)
|
||||
);
|
||||
+34
@@ -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);
|
||||
+2
@@ -0,0 +1,2 @@
|
||||
ALTER TABLE usage_events ADD COLUMN actual_route_id varchar(64);
|
||||
ALTER TABLE usage_events ADD COLUMN observed_usage jsonb;
|
||||
@@ -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);
|
||||
@@ -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
|
||||
Reference in new issue
Block a user