feat: extend enterprise gateway policies billing protocols and operations
This commit is contained in:
1 parent
1528b71882
commit
af43947b5d
67 files changed
+1820
-67
No files matched your search
@@ -43,7 +43,9 @@ public class AuthController {
|
|||||||
this.secureCookie = secureCookie;
|
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 RegisterRequest(String invitationCode, String account, String password, String displayName, String verificationCode) {}
|
||||||
public record CodeRequest(String account, String invitationCode) {}
|
public record CodeRequest(String account, String invitationCode) {}
|
||||||
@@ -87,7 +89,7 @@ public class AuthController {
|
|||||||
HttpServletRequest request,
|
HttpServletRequest request,
|
||||||
HttpServletResponse response
|
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
|
// Keep the browser session cookie ephemeral. The server-side session still
|
||||||
// has its normal TTL, but a closed browser must require a fresh login.
|
// has its normal TTL, but a closed browser must require a fresh login.
|
||||||
response.addHeader(HttpHeaders.SET_COOKIE, sessionCookie(result.token(), null).toString());
|
response.addHeader(HttpHeaders.SET_COOKIE, sessionCookie(result.token(), null).toString());
|
||||||
|
|||||||
@@ -34,6 +34,7 @@ public class AuthService {
|
|||||||
private final ObjectMapper objectMapper;
|
private final ObjectMapper objectMapper;
|
||||||
private final int sessionTtlHours;
|
private final int sessionTtlHours;
|
||||||
private final SecretCipher secretCipher;
|
private final SecretCipher secretCipher;
|
||||||
|
@Autowired private TotpService totp;
|
||||||
|
|
||||||
/** Kept for isolated unit tests that do not exercise encrypted invitation display. */
|
/** Kept for isolated unit tests that do not exercise encrypted invitation display. */
|
||||||
public AuthService(
|
public AuthService(
|
||||||
@@ -119,6 +120,9 @@ public class AuthService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public AuthResult login(String accountInput, String password, RequestMeta meta) {
|
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));
|
UserRow user = findUserByAccount(normalizeAccount(accountInput));
|
||||||
if (user == null || !passwordHasher.matches(password, user.passwordHash())) {
|
if (user == null || !passwordHasher.matches(password, user.passwordHash())) {
|
||||||
throw new ApiException("INVALID_CREDENTIALS", "账号或密码错误", 401);
|
throw new ApiException("INVALID_CREDENTIALS", "账号或密码错误", 401);
|
||||||
@@ -126,11 +130,16 @@ public class AuthService {
|
|||||||
if (!"active".equals(user.status())) {
|
if (!"active".equals(user.status())) {
|
||||||
throw new ApiException("ACCOUNT_DISABLED", "账号已停用", 403);
|
throw new ApiException("ACCOUNT_DISABLED", "账号已停用", 403);
|
||||||
}
|
}
|
||||||
|
if(totp!=null) totp.verifyLogin(user.id(),otp);
|
||||||
OffsetDateTime loginAt = now();
|
OffsetDateTime loginAt = now();
|
||||||
jdbc.update("UPDATE users SET last_login_at = ?, updated_at = ? WHERE id = ?", loginAt, loginAt, user.id());
|
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());
|
audit("user.logged_in", user.id(), meta, Map.of());
|
||||||
return new AuthResult(createSession(user.id(), meta), toView(user, loginAt));
|
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
|
@Transactional
|
||||||
public AuthResult register(
|
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) {
|
static void validatePricing(Object input) {
|
||||||
if (!(input instanceof Map<?,?> prices)) return;
|
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)
|
if (prices.get("listPriceUsd") != null && prices.get("listPriceCny") != null)
|
||||||
throw new ApiException("INVALID_PRICE", "标准价只能选择一种币种:人民币或美元");
|
throw new ApiException("INVALID_PRICE", "标准价只能选择一种币种:人民币或美元");
|
||||||
Object discount = prices.get("discount");
|
Object discount = prices.get("discount");
|
||||||
@@ -540,7 +541,12 @@ public class CatalogService {
|
|||||||
// while unsupported task types remain visibly pending.
|
// while unsupported task types remain visibly pending.
|
||||||
boolean asyncExecutor = asyncExecutorReady(
|
boolean asyncExecutor = asyncExecutorReady(
|
||||||
resultSet.getString("provider_id"), providerProtocol, resultSet.getString("model_type"));
|
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")));
|
model.put("billingReady", billingReady(pricing, resultSet.getString("call_mode"), resultSet.getString("provider_id")));
|
||||||
// Keep the internal route UUID for permission storage and expose a
|
// Keep the internal route UUID for permission storage and expose a
|
||||||
// stable, readable identifier for the user console and third-party
|
// 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("/**")
|
registry.addMapping("/**")
|
||||||
.allowedOrigins(allowedOrigins)
|
.allowedOrigins(allowedOrigins)
|
||||||
.allowedMethods("GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS")
|
.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")
|
.exposedHeaders("X-Request-Id", "X-Moyi-Cost-CNY")
|
||||||
.allowCredentials(true)
|
.allowCredentials(true)
|
||||||
.maxAge(3600);
|
.maxAge(3600);
|
||||||
|
|||||||
@@ -9,18 +9,22 @@ import java.util.*;
|
|||||||
/** Monetary calculations never use floating point or locally estimated completion usage. */
|
/** Monetary calculations never use floating point or locally estimated completion usage. */
|
||||||
public final class ChatMeter {
|
public final class ChatMeter {
|
||||||
private ChatMeter() {}
|
private ChatMeter() {}
|
||||||
public record Rates(BigDecimal input, BigDecimal output, BigDecimal cachedInput) {
|
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() {
|
public Map<String,Object> snapshot() {
|
||||||
return Map.of("input",input,"output",output,"cachedInput",cachedInput,"unit","CNY / 1M tokens");
|
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) {
|
public BigDecimal cost(long prompt, long completion, long cached) {
|
||||||
return input.multiply(BigDecimal.valueOf(prompt-cached))
|
return cost(new Tokens(prompt,completion,cached));
|
||||||
.add(cachedInput.multiply(BigDecimal.valueOf(cached)))
|
|
||||||
.add(output.multiply(BigDecimal.valueOf(completion)))
|
|
||||||
.divide(new BigDecimal("1000000"),8,RoundingMode.HALF_UP);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
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 record Request(Map<String,Object> body, long reservedInputTokens, int maxOutputTokens) {}
|
||||||
|
|
||||||
public static Rates rates(Object value, boolean mock) {
|
public static Rates rates(Object value, boolean mock) {
|
||||||
@@ -31,7 +35,7 @@ public final class ChatMeter {
|
|||||||
throw new ApiException("BILLING_UNIT_UNSUPPORTED","当前聊天接口仅支持 Token 计费;该模型应使用其对应的媒体或按次接口",503);
|
throw new ApiException("BILLING_UNIT_UNSUPPORTED","当前聊天接口仅支持 Token 计费;该模型应使用其对应的媒体或按次接口",503);
|
||||||
BigDecimal input=price(prices.get("input")), output=price(prices.get("output"));
|
BigDecimal input=price(prices.get("input")), output=price(prices.get("output"));
|
||||||
BigDecimal cached=prices.get("cachedInput")==null?input:price(prices.get("cachedInput"));
|
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) {
|
private static BigDecimal price(Object value) {
|
||||||
try {
|
try {
|
||||||
@@ -46,8 +50,14 @@ public final class ChatMeter {
|
|||||||
Object cached=usage.get("prompt_cache_hit_tokens");
|
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");
|
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);
|
long hits=cached==null?0:count(cached);
|
||||||
if(hits>prompt) throw invalidUsage();
|
long writes=usage.get("cache_creation_input_tokens")==null?0:count(usage.get("cache_creation_input_tokens"));
|
||||||
return new Tokens(prompt,completion,hits);
|
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) {
|
private static long count(Object value) {
|
||||||
try {
|
try {
|
||||||
|
|||||||
+1
@@ -83,6 +83,7 @@ final class ChatStreamCollector {
|
|||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
Integer firstTokenMs() { return firstToken; }
|
Integer firstTokenMs() { return firstToken; }
|
||||||
|
Object observedUsage() {return usage;}
|
||||||
Map<String,Object> result() {
|
Map<String,Object> result() {
|
||||||
if(finish==null) throw new ApiException("UPSTREAM_STREAM_INCOMPLETE","上游未完成输出,本次未扣费",502);
|
if(finish==null) throw new ApiException("UPSTREAM_STREAM_INCOMPLETE","上游未完成输出,本次未扣费",502);
|
||||||
if(content.toString().isBlank() && reasoning.toString().isBlank() && !hasToolOutput())
|
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 GatewayGuard guard;
|
||||||
private final ChatBillingService billing;
|
private final ChatBillingService billing;
|
||||||
private final ObjectMapper json;
|
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) {
|
public GatewayController(CatalogService catalogService, OpenAiCompatibleProvider provider, GatewayGuard guard, ChatBillingService billing, ObjectMapper json) {
|
||||||
this.catalogService = catalogService;
|
this.catalogService = catalogService;
|
||||||
@@ -58,6 +59,9 @@ public class GatewayController {
|
|||||||
item.put("upstream_model", model.get("upstreamModel"));
|
item.put("upstream_model", model.get("upstreamModel"));
|
||||||
item.put("type", model.get("type"));
|
item.put("type", model.get("type"));
|
||||||
item.put("mode", model.get("mode"));
|
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;
|
return item;
|
||||||
}).toList();
|
}).toList();
|
||||||
catalogService.touchKey(key);
|
catalogService.touchKey(key);
|
||||||
@@ -82,6 +86,7 @@ public class GatewayController {
|
|||||||
Map<String, Object> model = resolveModel(key, modelId);
|
Map<String, Object> model = resolveModel(key, modelId);
|
||||||
if (model == null) throw new ApiException("MODEL_NOT_FOUND", "模型不存在或已下线;请使用当前 API Key 的 /v1/models 返回的模型 ID", 404);
|
if (model == null) throw new ApiException("MODEL_NOT_FOUND", "模型不存在或已下线;请使用当前 API Key 的 /v1/models 返回的模型 ID", 404);
|
||||||
if ("async".equals(model.get("mode"))) throw new ApiException("ASYNC_MODEL", "该模型需要使用异步任务接口", 400);
|
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 providerId=String.valueOf(model.get("providerId"));
|
||||||
String routeId=String.valueOf(model.get("id"));
|
String routeId=String.valueOf(model.get("id"));
|
||||||
boolean mock="provider-mock".equals(providerId);
|
boolean mock="provider-mock".equals(providerId);
|
||||||
@@ -96,11 +101,12 @@ public class GatewayController {
|
|||||||
try {
|
try {
|
||||||
catalogService.touchKey(key);
|
catalogService.touchKey(key);
|
||||||
if(Boolean.TRUE.equals(request.body().get("stream"))) {
|
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;
|
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);
|
body.put("model",modelId);
|
||||||
|
billing.observedUsage(ticket,"chat",body.get("usage"));
|
||||||
requireCompletion(body);
|
requireCompletion(body);
|
||||||
var cost=billing.settle(ticket,body,elapsed(startedAt),null);
|
var cost=billing.settle(ticket,body,elapsed(startedAt),null);
|
||||||
return ResponseEntity.ok().header("X-Request-Id",ticket.id().toString()).header("X-Moyi-Cost-CNY",cost.toPlainString()).body(body);
|
return ResponseEntity.ok().header("X-Request-Id",ticket.id().toString()).header("X-Moyi-Cost-CNY",cost.toPlainString()).body(body);
|
||||||
@@ -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) {
|
if(mock) {
|
||||||
var body=mockResponse(modelId,input);
|
var body=mockResponse(modelId,input);
|
||||||
prepareStream(response);
|
prepareStream(response);
|
||||||
@@ -131,17 +137,17 @@ public class GatewayController {
|
|||||||
}
|
}
|
||||||
// Keep admission, the concurrency slot and the upstream stream alive on this request thread.
|
// 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.
|
// 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);
|
var collector=new ChatStreamCollector(modelId,started);
|
||||||
prepareStream(response);
|
prepareStream(response);
|
||||||
boolean[] disconnected={false};
|
boolean[] disconnected={false};
|
||||||
upstream.events(chunk -> {
|
try { upstream.events(chunk -> {
|
||||||
chunk.put("model",modelId);
|
chunk.put("model",modelId);
|
||||||
collector.accept(chunk);
|
collector.accept(chunk);
|
||||||
// A closed browser must not turn an otherwise completed provider call into free usage.
|
// A closed browser must not turn an otherwise completed provider call into free usage.
|
||||||
// Continue within the same hard deadline to obtain the final meter and settle once.
|
// Continue within the same hard deadline to obtain the final meter and settle once.
|
||||||
if(!disconnected[0]) try { event(response,chunk); } catch(IOException clientClosed) { disconnected[0]=true; }
|
if(!disconnected[0]) try { event(response,chunk); } catch(IOException clientClosed) { disconnected[0]=true; }
|
||||||
});
|
}); } finally {billing.observedUsage(ticket,"chat",collector.observedUsage());}
|
||||||
var body=collector.result();
|
var body=collector.result();
|
||||||
billing.settle(ticket,body,elapsed(started),collector.firstTokenMs());
|
billing.settle(ticket,body,elapsed(started),collector.firstTokenMs());
|
||||||
if(!disconnected[0]) done(response);
|
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 queueWaitMs;
|
||||||
private final long queuePollMs;
|
private final long queuePollMs;
|
||||||
private final String redisPrefix;
|
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) {
|
private static DefaultRedisScript<Long> script(String location) {
|
||||||
DefaultRedisScript<Long> script = new DefaultRedisScript<>();
|
DefaultRedisScript<Long> script = new DefaultRedisScript<>();
|
||||||
@@ -105,6 +108,11 @@ public class GatewayGuard {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void checkRate(UUID keyId, boolean readOnly) {
|
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;
|
String bucket = redisPrefix + "gateway:rate:" + (readOnly ? "read:" : "invoke:") + keyId;
|
||||||
long retryMs = execute(RATE, bucket, Integer.toString(readOnly ? readsPerMinute : requestsPerMinute), "60000");
|
long retryMs = execute(RATE, bucket, Integer.toString(readOnly ? readsPerMinute : requestsPerMinute), "60000");
|
||||||
if (retryMs > 0) {
|
if (retryMs > 0) {
|
||||||
@@ -150,6 +158,23 @@ public class GatewayGuard {
|
|||||||
|
|
||||||
public int defaultModelConcurrency() { return defaultModelConcurrency; }
|
public int defaultModelConcurrency() { return defaultModelConcurrency; }
|
||||||
public int defaultKeyConcurrency() { return defaultKeyConcurrency; }
|
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 Capacity(long running, long queued) {}
|
||||||
public record CapacitySnapshot(boolean available, Map<String, Capacity> groups) {}
|
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. */
|
/** 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) {
|
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) {
|
if (!modelConcurrencyEnabled) {
|
||||||
// Measurement mode removes only MOYI's per-model/group guard. The
|
// Measurement mode removes only MOYI's per-model/group guard. The
|
||||||
// independent per-platform-Key limit remains active so one client
|
// 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) {
|
private Slot acquireSlot(String bucket, int limit, String code, String message, long requestedWaitMs) {
|
||||||
if (limit <= 0) return new Slot(null, null);
|
if (limit <= 0) return new Slot(null, null);
|
||||||
String token = UUID.randomUUID().toString();
|
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);
|
long deadline = System.nanoTime() + java.util.concurrent.TimeUnit.MILLISECONDS.toNanos(waitMs);
|
||||||
while (true) {
|
while (true) {
|
||||||
long acquired = execute(ACQUIRE, bucket, Integer.toString(limit), Long.toString(SLOT_TTL_MS), token);
|
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 {
|
public final class Admission implements AutoCloseable {
|
||||||
|
private Slot userSlot;
|
||||||
private final Slot modelSlot;
|
private final Slot modelSlot;
|
||||||
private final Slot keySlot;
|
private final Slot keySlot;
|
||||||
private final AtomicBoolean closed = new AtomicBoolean();
|
private final AtomicBoolean closed = new AtomicBoolean();
|
||||||
@@ -299,6 +336,7 @@ public class GatewayGuard {
|
|||||||
if (!closed.compareAndSet(false, true)) return;
|
if (!closed.compareAndSet(false, true)) return;
|
||||||
if (modelSlot != null) modelSlot.close();
|
if (modelSlot != null) modelSlot.close();
|
||||||
if (keySlot != null) keySlot.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 CatalogService catalog;
|
||||||
private final GatewayController gateway;
|
private final GatewayController gateway;
|
||||||
private final AsyncTaskService tasks;
|
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) {
|
public PlaygroundController(AuthService auth, CatalogService catalog, GatewayController gateway, AsyncTaskService tasks) {
|
||||||
this.auth = auth;
|
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 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 ScheduledExecutorService deadlines = Executors.newSingleThreadScheduledExecutor(r -> { var t=new Thread(r,"upstream-deadlines"); t.setDaemon(true); return t; });
|
||||||
private final int timeoutSeconds;
|
private final int timeoutSeconds;
|
||||||
|
@org.springframework.beans.factory.annotation.Autowired private UpstreamHealth health;
|
||||||
|
|
||||||
public OpenAiCompatibleProvider(
|
public OpenAiCompatibleProvider(
|
||||||
ObjectMapper objectMapper,
|
ObjectMapper objectMapper,
|
||||||
@@ -99,47 +100,100 @@ public class OpenAiCompatibleProvider {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private StreamResponse open(String providerId, String upstreamModel, String routeId, Map<String,Object> input, boolean streaming) {
|
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);
|
ProviderConfig provider = resolve(providerId);
|
||||||
if (provider == null || !provider.hasKey(routeId, upstreamModel, environment)) {
|
if (provider == null || !provider.hasKey(routeId, upstreamModel, environment)) {
|
||||||
throw new ApiException("PROVIDER_NOT_CONFIGURED", "该模型的供应商尚未配置服务端 API Key", 503);
|
throw new ApiException("PROVIDER_NOT_CONFIGURED", "该模型的供应商尚未配置服务端 API Key", 503);
|
||||||
}
|
}
|
||||||
Map<String, Object> body = new LinkedHashMap<>(input);
|
Map<String, Object> body = new LinkedHashMap<>(input);
|
||||||
body.put("model", upstreamModel);
|
body.put("model", upstreamModel);
|
||||||
body.put("stream", streaming);
|
if(!List.of("embeddings","gemini").contains(protocol)) body.put("stream", streaming);
|
||||||
if(streaming) body.put("stream_options",Map.of("include_usage",true));
|
if("chat".equals(protocol)&&streaming) body.put("stream_options",Map.of("include_usage",true));
|
||||||
else body.remove("stream_options");
|
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);
|
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);
|
String json = writeJson(body);
|
||||||
HttpRequest request = HttpRequest.newBuilder()
|
HttpRequest.Builder builder = HttpRequest.newBuilder()
|
||||||
.uri(URI.create(normalizeUrl(provider.baseUrl()) + "/chat/completions"))
|
.uri(URI.create(base + path))
|
||||||
.timeout(Duration.ofSeconds(timeoutSeconds))
|
.timeout(Duration.ofMillis(remaining))
|
||||||
.header("Authorization", "Bearer " + apiKey)
|
.header("Authorization", "Bearer " + apiKey)
|
||||||
.header("Content-Type", "application/json")
|
.header("Content-Type", "application/json")
|
||||||
.POST(HttpRequest.BodyPublishers.ofString(json))
|
.POST(HttpRequest.BodyPublishers.ofString(json));
|
||||||
.build();
|
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 {
|
try {
|
||||||
long started=System.nanoTime();
|
long started=System.nanoTime();
|
||||||
HttpResponse<InputStream> response = httpClient.send(request, HttpResponse.BodyHandlers.ofInputStream());
|
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) {
|
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)) {
|
if (requiresLargerOutputBudget(details)) {
|
||||||
throw new ApiException("INVALID_MAX_TOKENS", "上游模型要求 max_tokens 大于 1024,请提高输出 Token 上限后重试", 400);
|
throw new ApiException("INVALID_MAX_TOKENS", "上游模型要求 max_tokens 大于 1024,请提高输出 Token 上限后重试", 400);
|
||||||
}
|
}
|
||||||
// Some OpenAI-compatible relays use the newer spelling for
|
// Some OpenAI-compatible relays use the newer spelling for
|
||||||
// reasoning-model budgets. Retry only this deterministic
|
// reasoning-model budgets. Retry only this deterministic
|
||||||
// GPT request-shape error; other failures remain bill-safe.
|
// GPT request-shape error; other failures remain bill-safe.
|
||||||
if (attempt == 0 && prepareCompatibilityRetry(body, details)) continue;
|
if ("chat".equals(protocol) && !compatibilityRetried && prepareCompatibilityRetry(body, details)) {compatibilityRetried=true;continue;}
|
||||||
throw classifyUpstreamError(response.statusCode(), details);
|
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")) {
|
if(streaming && !response.headers().firstValue("Content-Type").orElse("").toLowerCase().startsWith("text/event-stream")) {
|
||||||
response.body().close();
|
response.body().close();
|
||||||
throw new ApiException("UPSTREAM_STREAM_UNSUPPORTED","上游未返回 SSE 流式响应",502);
|
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) {
|
} catch (ApiException exception) {
|
||||||
throw exception;
|
throw exception;
|
||||||
} catch (HttpTimeoutException | SocketTimeoutException exception) {
|
} catch (HttpTimeoutException | SocketTimeoutException exception) {
|
||||||
|
if(health!=null) health.record(healthRoute,apiKey,504,timeoutSeconds*1000);
|
||||||
throw new ApiException("UPSTREAM_PROVIDER_TIMEOUT", "上游模型供应商响应超时", 504);
|
throw new ApiException("UPSTREAM_PROVIDER_TIMEOUT", "上游模型供应商响应超时", 504);
|
||||||
} catch (InterruptedException exception) {
|
} catch (InterruptedException exception) {
|
||||||
Thread.currentThread().interrupt();
|
Thread.currentThread().interrupt();
|
||||||
@@ -178,11 +232,16 @@ public class OpenAiCompatibleProvider {
|
|||||||
private final InputStream body;
|
private final InputStream body;
|
||||||
private final AtomicBoolean expired=new AtomicBoolean();
|
private final AtomicBoolean expired=new AtomicBoolean();
|
||||||
private final ScheduledFuture<?> deadline;
|
private final ScheduledFuture<?> deadline;
|
||||||
|
private Runnable afterClose=()->{};
|
||||||
|
public StreamResponse onClose(Runnable callback) {afterClose=callback;return this;}
|
||||||
private StreamResponse(InputStream body,long remainingMs) {
|
private StreamResponse(InputStream body,long remainingMs) {
|
||||||
this.body=body;
|
this.body=body;
|
||||||
this.deadline=deadlines.schedule(() -> { expired.set(true); try { body.close(); } catch(IOException ignored) {} },remainingMs,TimeUnit.MILLISECONDS);
|
this.deadline=deadlines.schedule(() -> { expired.set(true); try { body.close(); } catch(IOException ignored) {} },remainingMs,TimeUnit.MILLISECONDS);
|
||||||
}
|
}
|
||||||
public void events(EventConsumer consumer) throws IOException {
|
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));
|
var reader=new BufferedReader(new InputStreamReader(body,StandardCharsets.UTF_8));
|
||||||
StringBuilder event=new StringBuilder(), line=new StringBuilder();
|
StringBuilder event=new StringBuilder(), line=new StringBuilder();
|
||||||
long total=0;
|
long total=0;
|
||||||
@@ -210,9 +269,10 @@ public class OpenAiCompatibleProvider {
|
|||||||
if(expired.get()) throw new ApiException("UPSTREAM_PROVIDER_TIMEOUT","上游响应超时,本次未扣费",504);
|
if(expired.get()) throw new ApiException("UPSTREAM_PROVIDER_TIMEOUT","上游响应超时,本次未扣费",504);
|
||||||
throw exception;
|
throw exception;
|
||||||
}
|
}
|
||||||
|
if(!requireDone && !expired.get() && event.isEmpty() && line.isEmpty()) return;
|
||||||
throw new ApiException(expired.get()?"UPSTREAM_PROVIDER_TIMEOUT":"UPSTREAM_STREAM_INCOMPLETE","上游流式响应中断,本次未扣费",502);
|
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; }
|
@FunctionalInterface public interface EventConsumer { void accept(Map<String,Object> chunk) throws IOException; }
|
||||||
@PreDestroy public void close() { deadlines.shutdownNow(); }
|
@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 UsageAuditService usage;
|
||||||
private final AsyncProviderClient providerClient;
|
private final AsyncProviderClient providerClient;
|
||||||
private final TransactionTemplate transactions;
|
private final TransactionTemplate transactions;
|
||||||
|
private final com.moyi.controlplane.gateway.EnterprisePolicies policies;
|
||||||
|
|
||||||
/** Compatibility constructor for isolated gateway tests that only exercise mock tasks. */
|
/** Compatibility constructor for isolated gateway tests that only exercise mock tasks. */
|
||||||
public AsyncTaskService(JdbcTemplate jdbc, ObjectMapper objectMapper, CatalogService catalogService,
|
public AsyncTaskService(JdbcTemplate jdbc, ObjectMapper objectMapper, CatalogService catalogService,
|
||||||
@@ -92,6 +93,7 @@ public class AsyncTaskService {
|
|||||||
this.usage = usage;
|
this.usage = usage;
|
||||||
this.providerClient = providerClient;
|
this.providerClient = providerClient;
|
||||||
this.transactions = transactionManager == null ? null : new TransactionTemplate(transactionManager);
|
this.transactions = transactionManager == null ? null : new TransactionTemplate(transactionManager);
|
||||||
|
this.policies = new com.moyi.controlplane.gateway.EnterprisePolicies(jdbc,objectMapper);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
@@ -130,6 +132,7 @@ public class AsyncTaskService {
|
|||||||
// tests can complete them without an upstream call.
|
// tests can complete them without an upstream call.
|
||||||
String providerTaskId = "provider-mock".equals(model.get("providerId")) ? "mock" : null;
|
String providerTaskId = "provider-mock".equals(model.get("providerId")) ? "mock" : null;
|
||||||
String id = "task_" + UUID.randomUUID().toString().replace("-", "").substring(0, 24);
|
String id = "task_" + UUID.randomUUID().toString().replace("-", "").substring(0, 24);
|
||||||
|
policies.reserve(id,userId,apiKeyId,reservedAmount);
|
||||||
OffsetDateTime now = now();
|
OffsetDateTime now = now();
|
||||||
try {
|
try {
|
||||||
jdbc.update(
|
jdbc.update(
|
||||||
@@ -150,6 +153,7 @@ public class AsyncTaskService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public Map<String, Object> getForKey(Map<String, Object> key, String taskId) {
|
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"))));
|
UUID userId = catalogService.keyOwnerId(UUID.fromString(String.valueOf(key.get("id"))));
|
||||||
try {
|
try {
|
||||||
return jdbc.queryForObject("SELECT * FROM model_tasks WHERE id = ? AND user_id = ?", this::mapTask, taskId, userId);
|
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) {
|
public List<Map<String, Object>> list(String authorization, int limit) {
|
||||||
Map<String, Object> key = requireKey(authorization);
|
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"))));
|
UUID userId = catalogService.keyOwnerId(UUID.fromString(String.valueOf(key.get("id"))));
|
||||||
int boundedLimit = Math.max(1, Math.min(limit, 100));
|
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);
|
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.
|
// workers reliably.
|
||||||
jdbc.queryForList("SELECT pg_advisory_xact_lock(hashtextextended('moyi:async-dispatch', 0))");
|
jdbc.queryForList("SELECT pg_advisory_xact_lock(hashtextextended('moyi:async-dispatch', 0))");
|
||||||
String sql = """
|
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
|
FROM model_tasks t
|
||||||
JOIN model_catalog m ON m.id=t.model_id
|
JOIN model_catalog m ON m.id=t.model_id
|
||||||
JOIN model_providers p ON p.id=m.provider_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 t.provider_task_id IS NULL
|
||||||
AND m.status='active' AND m.deleted_at IS NULL
|
AND m.status='active' AND m.deleted_at IS NULL
|
||||||
AND p.status='active' AND p.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 (
|
AND (
|
||||||
? <= 0
|
? <= 0
|
||||||
OR COALESCE(m.max_concurrency, ?) <= 0
|
OR COALESCE(m.max_concurrency, ?) <= 0
|
||||||
@@ -264,7 +270,9 @@ public class AsyncTaskService {
|
|||||||
defaultKeyConcurrency, defaultKeyConcurrency);
|
defaultKeyConcurrency, defaultKeyConcurrency);
|
||||||
if (rows.isEmpty()) return null;
|
if (rows.isEmpty()) return null;
|
||||||
Map<String,Object> row = rows.getFirst();
|
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;
|
return row;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -501,7 +509,6 @@ public class AsyncTaskService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void reserveWallet(UUID userId, BigDecimal amount) {
|
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);
|
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);
|
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"));
|
BigDecimal available = ((BigDecimal) wallet.get("balance")).subtract((BigDecimal) wallet.get("reserved_balance"));
|
||||||
@@ -516,6 +523,8 @@ public class AsyncTaskService {
|
|||||||
private void settleTaskInTransaction(String taskId) {
|
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);
|
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");
|
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;
|
if (amount == null || amount.signum() == 0 || task.get("cost_amount") != null) return;
|
||||||
UUID userId = (UUID) task.get("user_id");
|
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);
|
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) {
|
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);
|
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");
|
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) {
|
if (amount != null && amount.signum() > 0 && task.get("cost_amount") == null) {
|
||||||
releaseWallet((UUID) task.get("user_id"), amount);
|
releaseWallet((UUID) task.get("user_id"), amount);
|
||||||
jdbc.update("UPDATE model_tasks SET cost_amount=0 WHERE id=?", taskId);
|
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) {
|
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=?",
|
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);
|
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(
|
public void recordCall(
|
||||||
@@ -393,8 +397,8 @@ public class UsageAuditService {
|
|||||||
ObjectNode result = objectMapper.createObjectNode();
|
ObjectNode result = objectMapper.createObjectNode();
|
||||||
node.fields().forEachRemaining(entry -> {
|
node.fields().forEachRemaining(entry -> {
|
||||||
String key = entry.getKey().toLowerCase();
|
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 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").contains(key) && entry.getValue().isObject();
|
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]");
|
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()));
|
else result.set(entry.getKey(), sanitize(entry.getValue()));
|
||||||
});
|
});
|
||||||
|
|||||||
+30
-6
@@ -18,13 +18,30 @@ public class ChatBillingService {
|
|||||||
private final JdbcTemplate jdbc;
|
private final JdbcTemplate jdbc;
|
||||||
private final ObjectMapper json;
|
private final ObjectMapper json;
|
||||||
private final UsageAuditService usage;
|
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 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
|
@Transactional
|
||||||
public Ticket reserve(Map<String,Object> key, Map<String,Object> model, ChatMeter.Request request, String idempotencyKey) {
|
public Ticket reserve(Map<String,Object> key, Map<String,Object> model, ChatMeter.Request request, String idempotencyKey) {
|
||||||
UUID userId=UUID.fromString(key.get("userId").toString());
|
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 requestHash=hash(encode(new TreeMap<>(request.body())));
|
||||||
String idem=null;
|
String idem=null;
|
||||||
if(idempotencyKey!=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);
|
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);
|
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()))
|
BigDecimal hold=rates.hold(request.reservedInputTokens(),request.maxOutputTokens());
|
||||||
.add(rates.output().multiply(BigDecimal.valueOf(request.maxOutputTokens())))
|
|
||||||
.divide(new BigDecimal("1000000"),8,java.math.RoundingMode.CEILING);
|
|
||||||
jdbc.update("INSERT INTO user_wallets(user_id) VALUES (?) ON CONFLICT DO NOTHING",userId);
|
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);
|
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"));
|
BigDecimal available=((BigDecimal)wallet.get("balance")).subtract((BigDecimal)wallet.get("reserved_balance"));
|
||||||
if(available.compareTo(hold)<0) throw new ApiException("INSUFFICIENT_BALANCE","可用余额不足以预留本次调用费用,请充值或调低输出 Token 上限",402);
|
if(available.compareTo(hold)<0) throw new ApiException("INSUFFICIENT_BALANCE","可用余额不足以预留本次调用费用,请充值或调低输出 Token 上限",402);
|
||||||
UUID id=UUID.randomUUID();
|
UUID id=UUID.randomUUID();
|
||||||
|
policies.reserve(id.toString(),userId,UUID.fromString(key.get("id").toString()),hold);
|
||||||
usage.startChat(id,userId,UUID.fromString(key.get("id").toString()),model,request.body());
|
usage.startChat(id,userId,UUID.fromString(key.get("id").toString()),model,request.body());
|
||||||
// Longer than the upstream hard deadline and the concurrency lease, including cleanup margin.
|
// Longer than the upstream hard deadline and the concurrency lease, including cleanup margin.
|
||||||
jdbc.update("INSERT INTO chat_billing_requests(id,user_id,idempotency_hash,request_hash,reserved_amount,pricing,expires_at) VALUES (?,?,?,?,?,?::jsonb,now()+interval '5 minutes')",id,userId,idem,requestHash,hold,encode(rates.snapshot()));
|
jdbc.update("INSERT INTO chat_billing_requests(id,user_id,idempotency_hash,request_hash,reserved_amount,pricing,expires_at) VALUES (?,?,?,?,?,?::jsonb,now()+interval '5 minutes')",id,userId,idem,requestHash,hold,encode(rates.snapshot()));
|
||||||
|
jdbc.update("UPDATE chat_billing_requests SET procurement_pricing=?::jsonb,price_version=? WHERE id=?",version.get("procurement_pricing")==null?null:version.get("procurement_pricing").toString(),version.get("id"),id);
|
||||||
jdbc.update("UPDATE user_wallets SET reserved_balance=reserved_balance+?,updated_at=now() WHERE user_id=?",hold,userId);
|
jdbc.update("UPDATE user_wallets SET reserved_balance=reserved_balance+?,updated_at=now() WHERE user_id=?",hold,userId);
|
||||||
return new Ticket(id,userId,rates);
|
return new Ticket(id,userId,rates);
|
||||||
}
|
}
|
||||||
@@ -57,13 +74,18 @@ public class ChatBillingService {
|
|||||||
var tokens=ChatMeter.tokens(response);
|
var tokens=ChatMeter.tokens(response);
|
||||||
// Read the persisted snapshot, not editable catalog prices or caller-supplied rates.
|
// Read the persisted snapshot, not editable catalog prices or caller-supplied rates.
|
||||||
ChatMeter.Rates rates=ChatMeter.rates(decode(call.get("pricing").toString()),false);
|
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 before=jdbc.queryForObject("SELECT balance FROM user_wallets WHERE user_id=? FOR UPDATE",BigDecimal.class,ticket.userId());
|
||||||
BigDecimal after=before.subtract(cost);
|
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());
|
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);
|
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);
|
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());
|
jdbc.update("UPDATE chat_billing_requests SET status='succeeded',cost_amount=? WHERE id=?",cost,ticket.id());
|
||||||
|
policies.release(ticket.id().toString());
|
||||||
return cost;
|
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 user_wallets SET reserved_balance=reserved_balance-?,updated_at=now() WHERE user_id=?",call.get("reserved_amount"),call.get("user_id"));
|
||||||
jdbc.update("UPDATE chat_billing_requests SET status='failed',cost_amount=0 WHERE id=?",id);
|
jdbc.update("UPDATE chat_billing_requests SET status='failed',cost_amount=0 WHERE id=?",id);
|
||||||
usage.finishChat(id,"failed",new ChatMeter.Tokens(0,0,0),latency,null,BigDecimal.ZERO,null,code,message);
|
usage.finishChat(id,"failed",new ChatMeter.Tokens(0,0,0),latency,null,BigDecimal.ZERO,null,code,message);
|
||||||
|
jdbc.update("UPDATE usage_events SET cost_status='pending' WHERE id=?",id);
|
||||||
|
policies.release(id.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Scheduled(fixedDelayString="${moyi.billing.recovery-interval-ms:60000}")
|
@Scheduled(fixedDelayString="${moyi.billing.recovery-interval-ms:60000}")
|
||||||
|
|||||||
@@ -27,6 +27,8 @@ server:
|
|||||||
address: ${CONTROL_PLANE_HOST:0.0.0.0}
|
address: ${CONTROL_PLANE_HOST:0.0.0.0}
|
||||||
port: ${CONTROL_PLANE_PORT:8787}
|
port: ${CONTROL_PLANE_PORT:8787}
|
||||||
shutdown: graceful
|
shutdown: graceful
|
||||||
|
# Native Tomcat processing trusts only its configured internal proxies, never arbitrary clients.
|
||||||
|
forward-headers-strategy: ${MOYI_FORWARD_HEADERS_STRATEGY:none}
|
||||||
|
|
||||||
management:
|
management:
|
||||||
endpoints:
|
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
|
return 0
|
||||||
end
|
end
|
||||||
redis.call('ZADD', KEYS[1], now + tonumber(ARGV[2]), ARGV[3])
|
redis.call('ZADD', KEYS[1], now + tonumber(ARGV[2]), ARGV[3])
|
||||||
|
-- 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])
|
redis.call('PEXPIRE', KEYS[1], ARGV[2])
|
||||||
|
end
|
||||||
return 1
|
return 1
|
||||||
+5
-1
@@ -82,6 +82,7 @@ class PlatformAdminPostgresTest {
|
|||||||
context.registerBean(DataSource.class, () -> source);
|
context.registerBean(DataSource.class, () -> source);
|
||||||
context.registerBean(DataSourceTransactionManager.class, () -> new DataSourceTransactionManager(source));
|
context.registerBean(DataSourceTransactionManager.class, () -> new DataSourceTransactionManager(source));
|
||||||
context.registerBean(AuthService.class, () -> new AuthService(jdbc, new PasswordHasher("test-pepper"), json, 168));
|
context.registerBean(AuthService.class, () -> new AuthService(jdbc, new PasswordHasher("test-pepper"), json, 168));
|
||||||
|
context.registerBean(TotpService.class, () -> new TotpService(jdbc,new SecretCipher("test-mfa")));
|
||||||
context.registerBean(CatalogService.class, () -> new CatalogService(jdbc, json, new SecretCipher("test-encryption")));
|
context.registerBean(CatalogService.class, () -> new CatalogService(jdbc, json, new SecretCipher("test-encryption")));
|
||||||
context.registerBean(WalletService.class, () -> new WalletService(jdbc, context.getBean(AuthService.class)));
|
context.registerBean(WalletService.class, () -> new WalletService(jdbc, context.getBean(AuthService.class)));
|
||||||
context.registerBean(UsageAuditService.class, () -> new UsageAuditService(jdbc,json));
|
context.registerBean(UsageAuditService.class, () -> new UsageAuditService(jdbc,json));
|
||||||
@@ -371,6 +372,7 @@ class PlatformAdminPostgresTest {
|
|||||||
ChatFixture chatFixture() {
|
ChatFixture chatFixture() {
|
||||||
var model=new LinkedHashMap<>(catalog.listModels(false).stream().filter(m -> "provider-deepseek".equals(m.get("providerId"))).findFirst().orElseThrow());
|
var model=new LinkedHashMap<>(catalog.listModels(false).stream().filter(m -> "provider-deepseek".equals(m.get("providerId"))).findFirst().orElseThrow());
|
||||||
model.put("pricing",Map.of("input",2,"output",4,"cachedInput",1,"unit","CNY / 1M tokens"));
|
model.put("pricing",Map.of("input",2,"output",4,"cachedInput",1,"unit","CNY / 1M tokens"));
|
||||||
|
jdbc.update("UPDATE model_catalog SET pricing='{"+"\"input\":2,\"output\":4,\"cachedInput\":1,\"unit\":\"CNY / 1M tokens\"}'::jsonb WHERE id=?",model.get("id"));
|
||||||
var created=catalog.createKey(memberToken,Map.of("name","Local billing test","modelIds",List.of(model.get("id"))),auth);
|
var created=catalog.createKey(memberToken,Map.of("name","Local billing test","modelIds",List.of(model.get("id"))),auth);
|
||||||
var key=catalog.authenticateKey(created.get("key").toString());
|
var key=catalog.authenticateKey(created.get("key").toString());
|
||||||
var input=ChatMeter.request(Map.of("model",model.get("id"),"messages",List.of(Map.of("role","user","content","你好")),"max_tokens",16),json);
|
var input=ChatMeter.request(Map.of("model",model.get("id"),"messages",List.of(Map.of("role","user","content","你好")),"max_tokens",16),json);
|
||||||
@@ -444,9 +446,11 @@ class PlatformAdminPostgresTest {
|
|||||||
var f=chatFixture();
|
var f=chatFixture();
|
||||||
assertEquals("INSUFFICIENT_BALANCE",assertThrows(ApiException.class,()->f.reserve(null)).getCode());
|
assertEquals("INSUFFICIENT_BALANCE",assertThrows(ApiException.class,()->f.reserve(null)).getCode());
|
||||||
f.model().remove("pricing");
|
f.model().remove("pricing");
|
||||||
|
jdbc.update("UPDATE model_catalog SET pricing=NULL WHERE id=?",f.model().get("id"));
|
||||||
assertEquals("MODEL_NOT_PRICED",assertThrows(ApiException.class,()->f.reserve(null)).getCode());
|
assertEquals("MODEL_NOT_PRICED",assertThrows(ApiException.class,()->f.reserve(null)).getCode());
|
||||||
f.model().put("pricing",Map.of("input",1,"output",2,"unit","USD / 1M tokens"));
|
f.model().put("pricing",Map.of("input",1,"output",2,"unit","USD / 1M tokens"));
|
||||||
assertEquals("MODEL_NOT_PRICED",assertThrows(ApiException.class,()->f.reserve(null)).getCode());
|
jdbc.update("UPDATE model_catalog SET pricing='{"+"\"input\":1,\"output\":2,\"unit\":\"USD / 1M tokens\"}'::jsonb WHERE id=?",f.model().get("id"));
|
||||||
|
assertEquals("BILLING_UNIT_UNSUPPORTED",assertThrows(ApiException.class,()->f.reserve(null)).getCode());
|
||||||
assertEquals(0,jdbc.queryForObject("SELECT count(*) FROM chat_billing_requests",Integer.class));
|
assertEquals(0,jdbc.queryForObject("SELECT count(*) FROM chat_billing_requests",Integer.class));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -51,6 +51,7 @@ class AuthPostgresTest {
|
|||||||
context.registerBean(DataSource.class, () -> source);
|
context.registerBean(DataSource.class, () -> source);
|
||||||
context.registerBean(DataSourceTransactionManager.class, () -> new DataSourceTransactionManager(source));
|
context.registerBean(DataSourceTransactionManager.class, () -> new DataSourceTransactionManager(source));
|
||||||
context.registerBean(AuthService.class, () -> new AuthService(jdbc, hasher, json, 168));
|
context.registerBean(AuthService.class, () -> new AuthService(jdbc, hasher, json, 168));
|
||||||
|
context.registerBean(TotpService.class, () -> new TotpService(jdbc,new com.moyi.controlplane.security.SecretCipher("test-mfa")));
|
||||||
context.refresh();
|
context.refresh();
|
||||||
AuthService auth = context.getBean(AuthService.class);
|
AuthService auth = context.getBean(AuthService.class);
|
||||||
try (var threads = Executors.newFixedThreadPool(2)) {
|
try (var threads = Executors.newFixedThreadPool(2)) {
|
||||||
|
|||||||
@@ -0,0 +1,13 @@
|
|||||||
|
package com.moyi.controlplane.auth;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
class TotpServiceTest {
|
||||||
|
@Test void rfc6238Sha1VectorsTruncatedToSixDigits() {
|
||||||
|
String secret=TotpService.base32("12345678901234567890".getBytes(StandardCharsets.US_ASCII));
|
||||||
|
assertEquals("287082",TotpService.totp(secret,59/30));
|
||||||
|
assertEquals("081804",TotpService.totp(secret,1111111109L/30));
|
||||||
|
assertEquals("050471",TotpService.totp(secret,1111111111L/30));
|
||||||
|
assertEquals("353130",TotpService.totp(secret,20000000000L/30));
|
||||||
|
}
|
||||||
|
}
|
||||||
+21
@@ -0,0 +1,21 @@
|
|||||||
|
package com.moyi.controlplane.gateway;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
import java.util.List;
|
||||||
|
class EnterprisePoliciesTest {
|
||||||
|
@Test void ipv4AndIpv6CidrAreMatchedWithoutDns() {
|
||||||
|
assertTrue(EnterprisePolicies.matches("192.0.2.23","192.0.2.0/24"));
|
||||||
|
assertFalse(EnterprisePolicies.matches("192.0.3.23","192.0.2.0/24"));
|
||||||
|
assertTrue(EnterprisePolicies.matches("2001:db8::4","2001:db8::/32"));
|
||||||
|
assertFalse(EnterprisePolicies.matches("2001:db9::4","2001:db8::/32"));
|
||||||
|
assertFalse(EnterprisePolicies.matches("127.0.0.1","localhost"));
|
||||||
|
assertFalse(EnterprisePolicies.matches("127.0.0.1","127.0.0.1/33"));
|
||||||
|
assertThrows(RuntimeException.class,()->EnterprisePolicies.validateRanges(List.of("127.1")));
|
||||||
|
}
|
||||||
|
@Test void nullBudgetIsUnlimitedAndZeroBudgetBlocksSpending() {
|
||||||
|
assertNull(EnterprisePolicyController.budget(""));
|
||||||
|
assertEquals(0,EnterprisePolicyController.budget("0").signum());
|
||||||
|
assertThrows(RuntimeException.class,()->EnterprisePolicyController.budget("-1"));
|
||||||
|
assertThrows(RuntimeException.class,()->EnterprisePolicyController.integer("1.5",100));
|
||||||
|
}
|
||||||
|
}
|
||||||
+120
@@ -0,0 +1,120 @@
|
|||||||
|
package com.moyi.controlplane.gateway;
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import com.moyi.controlplane.auth.TotpService;
|
||||||
|
import com.moyi.controlplane.auth.AuthService;
|
||||||
|
import com.moyi.controlplane.catalog.PriceManagementController;
|
||||||
|
import com.moyi.controlplane.usage.OperationsController;
|
||||||
|
import org.springframework.mock.web.MockHttpServletRequest;
|
||||||
|
import com.moyi.controlplane.common.ApiException;
|
||||||
|
import com.moyi.controlplane.security.SecretCipher;
|
||||||
|
import com.moyi.controlplane.usage.UsageAuditService;
|
||||||
|
import com.moyi.controlplane.wallet.ChatBillingService;
|
||||||
|
import com.moyi.controlplane.provider.UpstreamHealth;
|
||||||
|
import java.math.BigDecimal;
|
||||||
|
import java.util.*;
|
||||||
|
import java.util.concurrent.*;
|
||||||
|
import javax.sql.DataSource;
|
||||||
|
import org.flywaydb.core.Flyway;
|
||||||
|
import org.junit.jupiter.api.*;
|
||||||
|
import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable;
|
||||||
|
import org.springframework.context.annotation.*;
|
||||||
|
import org.springframework.jdbc.core.JdbcTemplate;
|
||||||
|
import org.springframework.jdbc.datasource.*;
|
||||||
|
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
||||||
|
import org.springframework.test.util.ReflectionTestUtils;
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
import static org.mockito.Mockito.*;
|
||||||
|
|
||||||
|
@EnabledIfEnvironmentVariable(named="MOYI_AUTH_TEST_JDBC_URL",matches="jdbc:postgresql://127\\.0\\.0\\.1:[0-9]+/.+")
|
||||||
|
class EnterprisePostgresTest {
|
||||||
|
@Configuration @EnableTransactionManagement(proxyTargetClass=true) static class Transactions {}
|
||||||
|
JdbcTemplate jdbc;DriverManagerDataSource base;AnnotationConfigApplicationContext context;String schema,model;
|
||||||
|
UUID user=UUID.randomUUID(),key=UUID.randomUUID();ObjectMapper json=new ObjectMapper();ChatBillingService billing;
|
||||||
|
Map<String,Object> keyMap,modelMap;
|
||||||
|
@BeforeEach void setup() {
|
||||||
|
String url=System.getenv("MOYI_AUTH_TEST_JDBC_URL");schema="enterprise_"+UUID.randomUUID().toString().replace("-","");
|
||||||
|
base=new DriverManagerDataSource(url,"moyi_role_test","");var source=new DriverManagerDataSource(url+"?currentSchema="+schema,"moyi_role_test","");
|
||||||
|
Flyway.configure().dataSource(base).schemas(schema).defaultSchema(schema).load().migrate();jdbc=new JdbcTemplate(source);
|
||||||
|
context=new AnnotationConfigApplicationContext();context.register(Transactions.class);
|
||||||
|
context.registerBean(DataSource.class,()->source);context.registerBean(DataSourceTransactionManager.class,()->new DataSourceTransactionManager(source));
|
||||||
|
context.registerBean(ChatBillingService.class,()->new ChatBillingService(jdbc,json,new UsageAuditService(jdbc,json)));
|
||||||
|
context.registerBean(TotpService.class,()->new TotpService(jdbc,new SecretCipher("isolated-secret")));context.refresh();billing=context.getBean(ChatBillingService.class);
|
||||||
|
jdbc.update("INSERT INTO users(id,account,account_kind,password_hash,display_name) VALUES (?,'enterprise-test','username','not-a-login','test')",user);
|
||||||
|
jdbc.update("INSERT INTO platform_api_keys(id,user_id,name,key_prefix,key_hash) VALUES (?,?,'key','test',?)",key,user,"e".repeat(64));
|
||||||
|
jdbc.update("INSERT INTO user_wallets(user_id,balance) VALUES (?,100)",user);
|
||||||
|
model=jdbc.queryForObject("SELECT id FROM model_catalog WHERE provider_id<>'provider-mock' LIMIT 1",String.class);
|
||||||
|
jdbc.update("UPDATE model_catalog SET pricing=?::jsonb,procurement_pricing=?::jsonb WHERE id=?","{\"unit\":\"CNY / 1M tokens\",\"input\":1,\"output\":2}","{\"unit\":\"CNY / 1M tokens\",\"input\":0.5,\"output\":0.8}",model);
|
||||||
|
keyMap=Map.of("id",key,"userId",user);modelMap=Map.of("id",model,"providerId",jdbc.queryForObject("SELECT provider_id FROM model_catalog WHERE id=?",String.class,model));
|
||||||
|
}
|
||||||
|
@AfterEach void cleanup() {if(context!=null) context.close();if(base!=null) new JdbcTemplate(base).execute("DROP SCHEMA IF EXISTS "+schema+" CASCADE");}
|
||||||
|
ChatMeter.Request request() {return new ChatMeter.Request(Map.of("messages",List.of(Map.of("role","user","content","test"))),100,10);}
|
||||||
|
@Test void concurrentAdmissionsRespectBudgetAndReleaseFailures() throws Exception {
|
||||||
|
jdbc.update("INSERT INTO gateway_user_policies(user_id,monthly_budget) VALUES (?,0.0002)",user);
|
||||||
|
var gate=new CountDownLatch(1);var tickets=new CopyOnWriteArrayList<ChatBillingService.Ticket>();
|
||||||
|
try(var pool=Executors.newFixedThreadPool(2)) {
|
||||||
|
var jobs=new ArrayList<Future<String>>();for(int i=0;i<2;i++) jobs.add(pool.submit(()->{gate.await();try {tickets.add(billing.reserve(keyMap,modelMap,request(),null));return "accepted";} catch(ApiException e) {return e.getCode();}}));
|
||||||
|
gate.countDown();var outcomes=new ArrayList<String>();for(var job:jobs) outcomes.add(job.get(10,TimeUnit.SECONDS));
|
||||||
|
assertEquals(1,Collections.frequency(outcomes,"accepted"));assertEquals(1,Collections.frequency(outcomes,"BUDGET_EXCEEDED"));
|
||||||
|
}
|
||||||
|
billing.fail(tickets.getFirst(),"TEST","interrupted",10);
|
||||||
|
assertEquals(0,jdbc.queryForObject("SELECT count(*) FROM gateway_budget_holds",Integer.class));
|
||||||
|
assertEquals("pending",jdbc.queryForObject("SELECT cost_status FROM usage_events WHERE id=?",String.class,tickets.getFirst().id()));
|
||||||
|
assertNotNull(billing.reserve(keyMap,modelMap,request(),null));
|
||||||
|
}
|
||||||
|
@Test void editsCannotChangeInflightRetailOrProcurementSnapshots() {
|
||||||
|
var ticket=billing.reserve(keyMap,modelMap,request(),"one");
|
||||||
|
jdbc.update("UPDATE model_catalog SET pricing='{"+"\"unit\":\"CNY / 1M tokens\",\"input\":10,\"output\":20}'::jsonb,procurement_pricing=NULL WHERE id=?",model);
|
||||||
|
var response=Map.<String,Object>of("usage",Map.of("prompt_tokens",100,"completion_tokens",10));
|
||||||
|
assertEquals(new BigDecimal("0.00012000"),billing.settle(ticket,response,100,null));
|
||||||
|
assertEquals(new BigDecimal("0.00012000"),billing.settle(ticket,response,100,null));
|
||||||
|
assertEquals(new BigDecimal("0.00005800"),jdbc.queryForObject("SELECT procurement_cost FROM usage_events WHERE id=?",BigDecimal.class,ticket.id()));
|
||||||
|
assertEquals(1,jdbc.queryForObject("SELECT count(*) FROM wallet_entries WHERE request_id=?",Integer.class,ticket.id()));
|
||||||
|
assertThrows(ApiException.class,()->billing.reserve(keyMap,modelMap,request(),"one"));
|
||||||
|
}
|
||||||
|
@Test void mfaRejectsReplayAndRecoveryCodesAreSingleUse() {
|
||||||
|
var mfa=context.getBean(TotpService.class);var setup=mfa.begin(user,"enterprise-test");
|
||||||
|
String secret=setup.get("secret").toString();String code=ReflectionTestUtils.invokeMethod(TotpService.class,"totp",secret,System.currentTimeMillis()/30000);
|
||||||
|
var recovery=mfa.confirm(user,code);assertEquals(10,recovery.size());
|
||||||
|
assertThrows(ApiException.class,()->mfa.verifyLogin(user,code));
|
||||||
|
mfa.verifyLogin(user,recovery.getFirst());assertThrows(ApiException.class,()->mfa.verifyLogin(user,recovery.getFirst()));
|
||||||
|
assertEquals(9,mfa.status(user).get("recoveryRemaining"));
|
||||||
|
assertFalse(jdbc.queryForObject("SELECT secret_ciphertext FROM user_totp WHERE user_id=?",String.class,user).contains(secret));
|
||||||
|
for(int i=0;i<4;i++) assertThrows(ApiException.class,()->mfa.verifyLogin(user,"wrong"));
|
||||||
|
assertEquals("MFA_RATE_LIMITED",assertThrows(ApiException.class,()->mfa.verifyLogin(user,recovery.get(1))).getCode());
|
||||||
|
}
|
||||||
|
@Test void healthCooldownExcludesRejectedAccount() {
|
||||||
|
var health=new UpstreamHealth(jdbc);health.record(model,"upstream-test-a",429,100);
|
||||||
|
assertEquals("upstream-test-b",health.select(model,List.of("upstream-test-a","upstream-test-b"),Set.of(),0));
|
||||||
|
assertNull(health.select(model,List.of("upstream-test-a"),Set.of(),0));
|
||||||
|
health.record(model,"upstream-test-a",200,50);
|
||||||
|
assertEquals("upstream-test-a",health.select(model,List.of("upstream-test-a"),Set.of(),0));
|
||||||
|
}
|
||||||
|
@Test void removedPriceCannotFallBackToPreviouslyReadCatalog() {
|
||||||
|
var stale=new HashMap<>(modelMap);stale.put("pricing",Map.of("unit","CNY / 1M tokens","input",1,"output",2));
|
||||||
|
jdbc.update("UPDATE model_catalog SET pricing=NULL WHERE id=?",model);
|
||||||
|
assertEquals("MODEL_NOT_PRICED",assertThrows(ApiException.class,()->billing.reserve(keyMap,stale,request(),null)).getCode());
|
||||||
|
assertEquals(0,jdbc.queryForObject("SELECT count(*) FROM gateway_budget_holds",Integer.class));
|
||||||
|
}
|
||||||
|
@Test void priceEditsAcceptEquivalentNumbersAndRejectStaleVersions() throws Exception {
|
||||||
|
var auth=mock(AuthService.class);when(auth.requireAdmin(any())).thenReturn(new AuthService.UserView(user,"admin","username","test","super_admin","active",null,null));
|
||||||
|
var controller=new PriceManagementController(jdbc,auth,json);var request=new MockHttpServletRequest();
|
||||||
|
jdbc.update("UPDATE model_catalog SET pricing=NULL,procurement_pricing=NULL WHERE id=?",model);
|
||||||
|
var payload=new HashMap<String,Object>();payload.put("pricing",Map.of("unit","CNY / 1M tokens","input",new BigDecimal("2.50"),"output",new BigDecimal("10.00")));
|
||||||
|
payload.put("expectedPricing",null);payload.put("expectedProcurementPricing",null);
|
||||||
|
controller.save(model,payload,request);
|
||||||
|
assertEquals("PRICE_CHANGED",assertThrows(ApiException.class,()->controller.save(model,payload,request)).getCode());
|
||||||
|
payload.put("expectedPricing",Map.of("unit","CNY / 1M tokens","input",2.5,"output",10));
|
||||||
|
payload.put("pricing",Map.of("unit","CNY / 1M tokens","input",3,"output",10));
|
||||||
|
controller.save(model,payload,request);
|
||||||
|
assertEquals(new BigDecimal("3"),jdbc.queryForObject("SELECT (pricing->>'input')::numeric FROM model_catalog WHERE id=?",BigDecimal.class,model));
|
||||||
|
}
|
||||||
|
@Test void alertCooldownAndRecoveryPersistWithoutSendingMail() {
|
||||||
|
jdbc.update("UPDATE ops_alert_rules SET minimum_requests=1,error_percent=50");
|
||||||
|
var ticket=billing.reserve(keyMap,modelMap,request(),null);billing.fail(ticket,"TEST","isolated failure",1);
|
||||||
|
var ops=new OperationsController(jdbc,mock(AuthService.class));ops.evaluate();ops.evaluate();
|
||||||
|
assertEquals(1,jdbc.queryForObject("SELECT count(*) FROM ops_alert_events",Integer.class));
|
||||||
|
assertEquals("disabled",jdbc.queryForObject("SELECT notification_status FROM ops_alert_events",String.class));
|
||||||
|
jdbc.update("UPDATE usage_events SET updated_at=now()-interval '1 day'");ops.evaluate();
|
||||||
|
assertEquals("resolved",jdbc.queryForObject("SELECT status FROM ops_alert_events",String.class));
|
||||||
|
}
|
||||||
|
}
|
||||||
+17
-4
@@ -20,6 +20,19 @@ import static org.mockito.ArgumentMatchers.*;
|
|||||||
import static org.mockito.Mockito.*;
|
import static org.mockito.Mockito.*;
|
||||||
|
|
||||||
class GatewayDeploymentTest {
|
class GatewayDeploymentTest {
|
||||||
|
@Test void failedTaskClaimReleasesRedisLeaseAfterDatabaseRollback() {
|
||||||
|
var redis=mock(StringRedisTemplate.class);doReturn(1L).when(redis).execute(any(RedisScript.class),anyList(),any(Object[].class));
|
||||||
|
var policies=mock(EnterprisePolicies.class);UUID key=UUID.randomUUID(),user=UUID.randomUUID();
|
||||||
|
when(policies.taskLimit(key)).thenReturn(new EnterprisePolicies.UserLimit(user,120,16));
|
||||||
|
var guard=new GatewayGuard(redis,60,300,4,"moyi:test:");guard.setEnterprisePolicies(policies);
|
||||||
|
org.springframework.transaction.support.TransactionSynchronizationManager.initSynchronization();
|
||||||
|
try {
|
||||||
|
assertTrue(guard.tryTaskUserSlot(key,"isolated-task",3600000));
|
||||||
|
clearInvocations(redis);
|
||||||
|
for(var callback:org.springframework.transaction.support.TransactionSynchronizationManager.getSynchronizations()) callback.afterCompletion(org.springframework.transaction.support.TransactionSynchronization.STATUS_ROLLED_BACK);
|
||||||
|
verify(redis).execute(any(RedisScript.class),eq(List.of("moyi:test:gateway:slots:user:"+user)),eq("isolated-task"));
|
||||||
|
} finally {org.springframework.transaction.support.TransactionSynchronizationManager.clearSynchronization();}
|
||||||
|
}
|
||||||
@Test
|
@Test
|
||||||
void capacitySnapshotReadsBothLiveAndWaitingBucketsAndDoesNotInventZeroOnFailure() {
|
void capacitySnapshotReadsBothLiveAndWaitingBucketsAndDoesNotInventZeroOnFailure() {
|
||||||
StringRedisTemplate redis = mock(StringRedisTemplate.class);
|
StringRedisTemplate redis = mock(StringRedisTemplate.class);
|
||||||
@@ -221,7 +234,7 @@ class GatewayDeploymentTest {
|
|||||||
UsageAuditService usage = mock(UsageAuditService.class);
|
UsageAuditService usage = mock(UsageAuditService.class);
|
||||||
Map<String, Object> key = Map.of("id", UUID.randomUUID().toString(), "userId", UUID.randomUUID().toString());
|
Map<String, Object> key = Map.of("id", UUID.randomUUID().toString(), "userId", UUID.randomUUID().toString());
|
||||||
when(catalog.authenticateKey("test-key")).thenReturn(key);
|
when(catalog.authenticateKey("test-key")).thenReturn(key);
|
||||||
when(catalog.activeModel("model-real")).thenReturn(Map.of("id", "model-real", "providerId", "provider-deepseek", "mode", "sync"));
|
when(catalog.activeModel("model-real")).thenReturn(Map.of("id", "model-real", "providerId", "provider-deepseek", "mode", "sync", "type", "text"));
|
||||||
when(catalog.keyCanInvoke(key, "model-real")).thenReturn(true);
|
when(catalog.keyCanInvoke(key, "model-real")).thenReturn(true);
|
||||||
GatewayController controller = new GatewayController(catalog, provider, guard, mock(ChatBillingService.class),new ObjectMapper());
|
GatewayController controller = new GatewayController(catalog, provider, guard, mock(ChatBillingService.class),new ObjectMapper());
|
||||||
ApiException error = assertThrows(ApiException.class, () -> controller.chatCompletions("Bearer test-key", Map.of("model", "model-real"),null,new MockHttpServletResponse()));
|
ApiException error = assertThrows(ApiException.class, () -> controller.chatCompletions("Bearer test-key", Map.of("model", "model-real"),null,new MockHttpServletResponse()));
|
||||||
@@ -235,7 +248,7 @@ class GatewayDeploymentTest {
|
|||||||
CatalogService catalog = mock(CatalogService.class);
|
CatalogService catalog = mock(CatalogService.class);
|
||||||
Map<String, Object> key = Map.of("id", UUID.randomUUID().toString(), "userId", UUID.randomUUID().toString());
|
Map<String, Object> key = Map.of("id", UUID.randomUUID().toString(), "userId", UUID.randomUUID().toString());
|
||||||
when(catalog.authenticateKey("test-key")).thenReturn(key);
|
when(catalog.authenticateKey("test-key")).thenReturn(key);
|
||||||
when(catalog.activeModel("mock")).thenReturn(Map.of("id", "mock", "providerId", "provider-mock", "mode", "sync"));
|
when(catalog.activeModel("mock")).thenReturn(Map.of("id", "mock", "providerId", "provider-mock", "mode", "sync", "type", "text"));
|
||||||
when(catalog.keyCanInvoke(key, "mock")).thenReturn(true);
|
when(catalog.keyCanInvoke(key, "mock")).thenReturn(true);
|
||||||
var billing=mock(ChatBillingService.class);
|
var billing=mock(ChatBillingService.class);
|
||||||
when(billing.reserve(anyMap(),anyMap(),any(),any())).thenReturn(new ChatBillingService.Ticket(UUID.randomUUID(),UUID.fromString(key.get("userId").toString()),ChatMeter.rates(null,true)));
|
when(billing.reserve(anyMap(),anyMap(),any(),any())).thenReturn(new ChatBillingService.Ticket(UUID.randomUUID(),UUID.fromString(key.get("userId").toString()),ChatMeter.rates(null,true)));
|
||||||
@@ -252,7 +265,7 @@ class GatewayDeploymentTest {
|
|||||||
ChatBillingService billing = mock(ChatBillingService.class);
|
ChatBillingService billing = mock(ChatBillingService.class);
|
||||||
String userId = UUID.randomUUID().toString();
|
String userId = UUID.randomUUID().toString();
|
||||||
Map<String, Object> key = Map.of("id", UUID.randomUUID().toString(), "userId", userId);
|
Map<String, Object> key = Map.of("id", UUID.randomUUID().toString(), "userId", userId);
|
||||||
Map<String, Object> model = Map.of("id", "reasoning-route", "providerId", "provider-relay", "upstreamModel", "reasoning-model", "mode", "sync");
|
Map<String, Object> model = Map.of("id", "reasoning-route", "providerId", "provider-relay", "upstreamModel", "reasoning-model", "mode", "sync", "type", "text");
|
||||||
when(catalog.authenticateKey("test-key")).thenReturn(key);
|
when(catalog.authenticateKey("test-key")).thenReturn(key);
|
||||||
when(catalog.activeModel("reasoning-route")).thenReturn(model);
|
when(catalog.activeModel("reasoning-route")).thenReturn(model);
|
||||||
when(catalog.keyCanInvoke(key, "reasoning-route")).thenReturn(true);
|
when(catalog.keyCanInvoke(key, "reasoning-route")).thenReturn(true);
|
||||||
@@ -284,7 +297,7 @@ class GatewayDeploymentTest {
|
|||||||
ChatBillingService billing = mock(ChatBillingService.class);
|
ChatBillingService billing = mock(ChatBillingService.class);
|
||||||
String userId = UUID.randomUUID().toString();
|
String userId = UUID.randomUUID().toString();
|
||||||
Map<String, Object> key = Map.of("id", UUID.randomUUID().toString(), "userId", userId);
|
Map<String, Object> key = Map.of("id", UUID.randomUUID().toString(), "userId", userId);
|
||||||
Map<String, Object> model = Map.of("id", "empty-route", "providerId", "provider-relay", "upstreamModel", "empty-model", "mode", "sync");
|
Map<String, Object> model = Map.of("id", "empty-route", "providerId", "provider-relay", "upstreamModel", "empty-model", "mode", "sync", "type", "text");
|
||||||
when(catalog.authenticateKey("test-key")).thenReturn(key);
|
when(catalog.authenticateKey("test-key")).thenReturn(key);
|
||||||
when(catalog.activeModel("empty-route")).thenReturn(model);
|
when(catalog.activeModel("empty-route")).thenReturn(model);
|
||||||
when(catalog.keyCanInvoke(key, "empty-route")).thenReturn(true);
|
when(catalog.keyCanInvoke(key, "empty-route")).thenReturn(true);
|
||||||
|
|||||||
+32
@@ -0,0 +1,32 @@
|
|||||||
|
package com.moyi.controlplane.gateway;
|
||||||
|
import com.moyi.controlplane.catalog.CatalogService;
|
||||||
|
import com.moyi.controlplane.common.ApiException;
|
||||||
|
import com.moyi.controlplane.provider.*;
|
||||||
|
import com.moyi.controlplane.wallet.ChatBillingService;
|
||||||
|
import java.util.*;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
import static org.mockito.Mockito.*;
|
||||||
|
import static org.mockito.ArgumentMatchers.*;
|
||||||
|
class GatewayFailoverTest {
|
||||||
|
Map<String,Object> route(String id,String logical) {return Map.of("id",id,"logicalModelId",logical,"providerId","p", "upstreamModel","same-model","type","text","mode","sync","capabilities",Map.of("failover",true));}
|
||||||
|
@Test void onlyAuthorizedEquivalentRoutesAreSelectedAndRetailTicketIsReused() {
|
||||||
|
var catalog=mock(CatalogService.class);var provider=mock(OpenAiCompatibleProvider.class);var guard=mock(GatewayGuard.class);var billing=mock(ChatBillingService.class);
|
||||||
|
Map<String,Object> primary=route("one","logical"),backup=route("two","logical"),forbidden=route("no-access","logical"),different=route("other","other-model");
|
||||||
|
var key=Map.<String,Object>of("id",UUID.randomUUID());when(catalog.listModels(false)).thenReturn(List.of(primary,backup,forbidden,different));
|
||||||
|
when(catalog.keyCanInvoke(eq(key),anyString())).thenAnswer(call->!call.getArgument(1).equals("no-access"));when(provider.isConfigured(anyString(),anyString(),anyString())).thenReturn(true);
|
||||||
|
var service=new GatewayFailover(catalog,guard,billing,provider);assertEquals(List.of(primary,backup),service.candidates(key,primary,"chat"));
|
||||||
|
var ticket=new ChatBillingService.Ticket(UUID.randomUUID(),UUID.randomUUID(),ChatMeter.rates(null,true));
|
||||||
|
when(provider.chat(eq("p"),eq("same-model"),eq("one"),anyMap())).thenThrow(new UpstreamRejectedException(new ApiException("UPSTREAM_RATE_LIMITED","rejected",429)));
|
||||||
|
when(provider.chat(eq("p"),eq("same-model"),eq("two"),anyMap())).thenReturn(Map.of("ok",true));
|
||||||
|
assertEquals(Map.of("ok",true),service.invoke(key,primary,"chat",Map.of(),ticket));verify(billing).actualRoute(ticket,backup);verify(billing,never()).reserve(any(),any(),any(),any());
|
||||||
|
}
|
||||||
|
@Test void ambiguousTimeoutIsNeverRetried() {
|
||||||
|
var catalog=mock(CatalogService.class);var provider=mock(OpenAiCompatibleProvider.class);var guard=mock(GatewayGuard.class);var billing=mock(ChatBillingService.class);
|
||||||
|
Map<String,Object> primary=route("one","logical"),backup=route("two","logical");var key=Map.<String,Object>of("id",UUID.randomUUID());
|
||||||
|
when(catalog.listModels(false)).thenReturn(List.of(primary,backup));when(catalog.keyCanInvoke(anyMap(),anyString())).thenReturn(true);when(provider.isConfigured(anyString(),anyString(),anyString())).thenReturn(true);
|
||||||
|
when(provider.chat(eq("p"),eq("same-model"),eq("one"),anyMap())).thenThrow(new ApiException("UPSTREAM_PROVIDER_TIMEOUT","unknown",504));
|
||||||
|
assertThrows(ApiException.class,()->new GatewayFailover(catalog,guard,billing,provider).invoke(key,primary,"chat",Map.of(),null));
|
||||||
|
verify(provider,never()).chat(eq("p"),eq("same-model"),eq("two"),anyMap());
|
||||||
|
}
|
||||||
|
}
|
||||||
+32
@@ -0,0 +1,32 @@
|
|||||||
|
package com.moyi.controlplane.gateway;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
import java.util.*;
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
class NativeProtocolMeterTest {
|
||||||
|
@Test void claudeCacheTokensAreAddedOnceAndHourlyWritesPreserved() {
|
||||||
|
var body=Map.<String,Object>of("stop_reason","end_turn","usage",Map.of("input_tokens",10,"output_tokens",20,"cache_read_input_tokens",100,"cache_creation_input_tokens",50,"cache_creation",Map.of("ephemeral_1h_input_tokens",30,"ephemeral_5m_input_tokens",20)));
|
||||||
|
var tokens=ChatMeter.tokens(NativeProtocolMeter.normalize("messages",body));
|
||||||
|
assertEquals(new ChatMeter.Tokens(160,20,100,20,30),tokens);
|
||||||
|
}
|
||||||
|
@Test void geminiThoughtTokensAreBillableOutput() {
|
||||||
|
var tokens=ChatMeter.tokens(NativeProtocolMeter.normalize("gemini",Map.of("candidates",List.of(Map.of("finishReason","STOP")),"usageMetadata",Map.of("promptTokenCount",100,"candidatesTokenCount",20,"thoughtsTokenCount",30))));
|
||||||
|
assertEquals(50,tokens.completion());
|
||||||
|
}
|
||||||
|
@Test void messagesStreamRequiresStopAndMergesCumulativeUsage() {
|
||||||
|
var collector=new NativeProtocolMeter.Collector("messages");
|
||||||
|
collector.accept(Map.of("type","message_start","message",Map.of("usage",Map.of("input_tokens",100,"output_tokens",1))));
|
||||||
|
collector.accept(Map.of("type","message_delta","delta",Map.of("stop_reason","tool_use"),"usage",Map.of("output_tokens",25)));
|
||||||
|
assertThrows(RuntimeException.class,collector::result);
|
||||||
|
collector.accept(Map.of("type","message_stop"));
|
||||||
|
assertEquals(25,ChatMeter.tokens(collector.result()).completion());
|
||||||
|
}
|
||||||
|
@Test void responsesStateCannotCrossCustomerBoundary() {
|
||||||
|
assertThrows(RuntimeException.class,()->NativeProtocolMeter.request("responses",Map.of("input","hello","previous_response_id","resp_other_customer"),new ObjectMapper()));
|
||||||
|
assertEquals(false,NativeProtocolMeter.request("responses",Map.of("input","hello"),new ObjectMapper()).body().get("store"));
|
||||||
|
}
|
||||||
|
@Test void embeddingsHaveNoOutputCharge() {
|
||||||
|
assertEquals(0,ChatMeter.tokens(NativeProtocolMeter.normalize("embeddings",Map.of("data",List.of(Map.of("embedding",List.of(0.1))),"usage",Map.of("prompt_tokens",5)))).completion());
|
||||||
|
assertThrows(RuntimeException.class,()->NativeProtocolMeter.normalize("embeddings",Map.of("data",List.of(Map.of("error","no vector")),"usage",Map.of("prompt_tokens",5))));
|
||||||
|
}
|
||||||
|
}
|
||||||
+30
@@ -0,0 +1,30 @@
|
|||||||
|
package com.moyi.controlplane.gateway;
|
||||||
|
import java.math.BigDecimal;
|
||||||
|
import java.util.*;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
class TokenPriceScheduleTest {
|
||||||
|
private Map<String,Object> base() { return new HashMap<>(Map.of("unit","CNY / 1M tokens","input",2,"output",8,"cachedInput",0.2,"cacheWrite5m",2.5,"cacheWrite1h",4)); }
|
||||||
|
@Test void inputIncludesCacheButEachCategoryIsBilledOnlyOnce() {
|
||||||
|
var rates=ChatMeter.rates(base(),false);
|
||||||
|
assertEquals(new BigDecimal("0.00287000"),rates.cost(new ChatMeter.Tokens(1000,100,100,100,100)));
|
||||||
|
}
|
||||||
|
@Test void tierBoundaryIsStrictAndSnapshotIsIndependent() {
|
||||||
|
var raw=base(); raw.put("tiers",List.of(Map.of("aboveInputTokens",1000,"input",4,"output",12)));
|
||||||
|
var rates=ChatMeter.rates(raw,false); raw.put("input",999);
|
||||||
|
assertEquals(new BigDecimal("0.00200000"),rates.cost(1000,0,0));
|
||||||
|
assertEquals(new BigDecimal("0.00400400"),rates.cost(1001,0,0));
|
||||||
|
assertEquals(new BigDecimal("0.01600000"),rates.hold(1000,1000));
|
||||||
|
assertEquals(rates.cost(1001,0,0),ChatMeter.rates(rates.snapshot(),false).cost(1001,0,0));
|
||||||
|
}
|
||||||
|
@Test void rejectsAmbiguousTiersAndInvalidCacheBreakdown() {
|
||||||
|
var raw=base(); raw.put("tiers",List.of(Map.of("aboveInputTokens",1000,"input",3),Map.of("aboveInputTokens",1000,"output",5)));
|
||||||
|
assertThrows(RuntimeException.class,()->ChatMeter.rates(raw,false));
|
||||||
|
assertThrows(RuntimeException.class,()->ChatMeter.tokens(Map.of("usage",Map.of("prompt_tokens",10,"completion_tokens",1,"cache_creation_input_tokens",11))));
|
||||||
|
}
|
||||||
|
@Test void explicitZeroCacheWritePriceRemainsFree() {
|
||||||
|
var raw=base(); raw.put("cacheWrite1h",0);
|
||||||
|
assertEquals(new BigDecimal("0.00000000"),ChatMeter.rates(raw,false).cost(new ChatMeter.Tokens(1000,0,0,0,1000)));
|
||||||
|
}
|
||||||
|
}
|
||||||
+40
@@ -0,0 +1,40 @@
|
|||||||
|
package com.moyi.controlplane.provider;
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import com.moyi.controlplane.security.SecretCipher;
|
||||||
|
import com.sun.net.httpserver.HttpServer;
|
||||||
|
import java.net.InetSocketAddress;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.util.*;
|
||||||
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.springframework.mock.env.MockEnvironment;
|
||||||
|
import org.springframework.jdbc.core.JdbcTemplate;
|
||||||
|
import org.springframework.test.util.ReflectionTestUtils;
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
import static org.mockito.Mockito.*;
|
||||||
|
class NativeProviderTest {
|
||||||
|
@Test void nativeEndpointsKeepToolsHeadersAndSseEvents() throws Exception {
|
||||||
|
var server=HttpServer.create(new InetSocketAddress("127.0.0.1",0),0);var json=new ObjectMapper();var seen=new ArrayList<String>();
|
||||||
|
server.createContext("/",exchange->{
|
||||||
|
seen.add(exchange.getRequestURI().toString());var body=json.readTree(exchange.getRequestBody());
|
||||||
|
assertEquals("Bearer test-upstream",exchange.getRequestHeaders().getFirst("Authorization"));
|
||||||
|
if(exchange.getRequestURI().getPath().endsWith("messages")) {assertEquals("2023-06-01",exchange.getRequestHeaders().getFirst("anthropic-version"));assertEquals("function",body.path("tools").get(0).path("type").asText());}
|
||||||
|
byte[] output=(body.path("stream").asBoolean()?"event: message_stop\ndata: {\"type\":\"message_stop\"}\n\n":"{\"usage\":{\"prompt_tokens\":5},\"data\":[{\"embedding\":[0.1]}]}").getBytes(StandardCharsets.UTF_8);
|
||||||
|
exchange.getResponseHeaders().set("Content-Type",body.path("stream").asBoolean()?"text/event-stream":"application/json");exchange.sendResponseHeaders(200,output.length);exchange.getResponseBody().write(output);exchange.close();
|
||||||
|
});server.start();
|
||||||
|
var env=new MockEnvironment().withProperty("moyi.providers.deepseek.api-key","test-upstream").withProperty("moyi.providers.deepseek.base-url","http://127.0.0.1:"+server.getAddress().getPort()+"/v1");
|
||||||
|
var provider=new OpenAiCompatibleProvider(json,env,mock(JdbcTemplate.class),new SecretCipher("test"));
|
||||||
|
try {provider.invoke("provider-deepseek","embedding","route",Map.of("input","hello"),"embeddings");
|
||||||
|
try(var stream=provider.nativeStream("provider-deepseek","claude","route",Map.of("tools",List.of(Map.of("type","function"))),"messages")) {var chunks=new ArrayList<Map<String,Object>>();stream.events(chunks::add,false);assertEquals("message_stop",chunks.getFirst().get("type"));}
|
||||||
|
provider.invoke("provider-deepseek","gemini-model","route",Map.of("contents",List.of()),"gemini");
|
||||||
|
assertEquals(List.of("/v1/embeddings","/v1/messages","/v1beta/models/gemini-model:generateContent"),seen);
|
||||||
|
} finally {provider.close();server.stop(0);}
|
||||||
|
}
|
||||||
|
@Test void rejectedKeyIsReplacedButSuccessfulRequestIsNotDuplicated() throws Exception {
|
||||||
|
var calls=new AtomicInteger();var server=HttpServer.create(new InetSocketAddress("127.0.0.1",0),0);
|
||||||
|
server.createContext("/v1/chat/completions",exchange->{calls.incrementAndGet();boolean reject=exchange.getRequestHeaders().getFirst("Authorization").equals("Bearer first");byte[] output=(reject?"{\"error\":\"rate limit\"}":"{\"choices\":[]}").getBytes(StandardCharsets.UTF_8);exchange.sendResponseHeaders(reject?429:200,output.length);exchange.getResponseBody().write(output);exchange.close();});server.start();
|
||||||
|
var env=new MockEnvironment().withProperty("moyi.providers.deepseek.api-key","first").withProperty("MOYI_UPSTREAM_API_KEYS_ROUTE_ROUTE_1","first|second").withProperty("moyi.providers.deepseek.base-url","http://127.0.0.1:"+server.getAddress().getPort()+"/v1");var provider=new OpenAiCompatibleProvider(new ObjectMapper(),env,mock(JdbcTemplate.class),new SecretCipher("test"));
|
||||||
|
ReflectionTestUtils.setField(provider,"health",new UpstreamHealth(mock(JdbcTemplate.class)));
|
||||||
|
try {provider.chat("provider-deepseek","model","route-1",Map.of());assertEquals(2,calls.get());} finally {provider.close();server.stop(0);}
|
||||||
|
}
|
||||||
|
}
|
||||||
+12
-9
@@ -46,8 +46,10 @@ class AsyncTaskPostgresTest {
|
|||||||
jdbc = new JdbcTemplate(source);
|
jdbc = new JdbcTemplate(source);
|
||||||
usage = spy(new UsageAuditService(jdbc, json));
|
usage = spy(new UsageAuditService(jdbc, json));
|
||||||
provider = mock(AsyncProviderClient.class);
|
provider = mock(AsyncProviderClient.class);
|
||||||
|
GatewayGuard guard=mock(GatewayGuard.class);
|
||||||
|
when(guard.tryTaskUserSlot(any(),anyString(),anyLong())).thenReturn(true);
|
||||||
tasks = new AsyncTaskService(jdbc, json, mock(CatalogService.class), 100, 4, 4, 120_000,
|
tasks = new AsyncTaskService(jdbc, json, mock(CatalogService.class), 100, 4, 4, 120_000,
|
||||||
mock(GatewayGuard.class), usage, provider, new DataSourceTransactionManager(source));
|
guard, usage, provider, new DataSourceTransactionManager(source));
|
||||||
jdbc.update("INSERT INTO users(id,account,account_kind,password_hash,display_name) VALUES (?,'async-test','username','not-a-password','Test')", userId);
|
jdbc.update("INSERT INTO users(id,account,account_kind,password_hash,display_name) VALUES (?,'async-test','username','not-a-password','Test')", userId);
|
||||||
jdbc.update("INSERT INTO platform_api_keys(id,user_id,name,key_prefix,key_hash) VALUES (?,?,'Test','test-prefix',?)", keyId, userId, "b".repeat(64));
|
jdbc.update("INSERT INTO platform_api_keys(id,user_id,name,key_prefix,key_hash) VALUES (?,?,'Test','test-prefix',?)", keyId, userId, "b".repeat(64));
|
||||||
jdbc.update("INSERT INTO user_wallets(user_id,balance,reserved_balance) VALUES (?,10,1.44)", userId);
|
jdbc.update("INSERT INTO user_wallets(user_id,balance,reserved_balance) VALUES (?,10,1.44)", userId);
|
||||||
@@ -64,8 +66,9 @@ class AsyncTaskPostgresTest {
|
|||||||
ReflectionTestUtils.invokeMethod(tasks,"failTask",taskId,"PROVIDER_NOT_CONFIGURED","异步渠道尚未配置 API Key");
|
ReflectionTestUtils.invokeMethod(tasks,"failTask",taskId,"PROVIDER_NOT_CONFIGURED","异步渠道尚未配置 API Key");
|
||||||
}
|
}
|
||||||
private void poll() {
|
private void poll() {
|
||||||
Map<String,Object> row = jdbc.queryForMap("SELECT id,provider_task_id,provider_deadline_at,created_at FROM model_tasks WHERE id=?", taskId);
|
Map<String,Object> row = jdbc.queryForMap("SELECT id,model_id,provider_task_id,provider_deadline_at,created_at,provider_key_fingerprint FROM model_tasks WHERE id=?", taskId);
|
||||||
row.put("provider_id", "provider-vast");
|
row.put("provider_id", "provider-vast");
|
||||||
|
row.put("upstream_model","v3.1-20260211");
|
||||||
ReflectionTestUtils.invokeMethod(tasks,"pollProviderTask",row);
|
ReflectionTestUtils.invokeMethod(tasks,"pollProviderTask",row);
|
||||||
}
|
}
|
||||||
private BigDecimal wallet(String column) {
|
private BigDecimal wallet(String column) {
|
||||||
@@ -82,7 +85,7 @@ class AsyncTaskPostgresTest {
|
|||||||
@ValueSource(strings={"PROVIDER_NOT_CONFIGURED", "UPSTREAM_AUTH_FAILED"})
|
@ValueSource(strings={"PROVIDER_NOT_CONFIGURED", "UPSTREAM_AUTH_FAILED"})
|
||||||
void submissionFailureReleasesExactlyOnceWithoutDebitingBalance(String code) {
|
void submissionFailureReleasesExactlyOnceWithoutDebitingBalance(String code) {
|
||||||
jdbc.update("UPDATE model_tasks SET status='queued',provider_task_id=NULL WHERE id=?",taskId);
|
jdbc.update("UPDATE model_tasks SET status='queued',provider_task_id=NULL WHERE id=?",taskId);
|
||||||
when(provider.submit(anyString(),anyString(),anyString(),anyString())).thenThrow(new ApiException(code,"上游任务未接受",503));
|
when(provider.submit(anyString(),anyString(),anyString(),anyString(),anyString())).thenThrow(new ApiException(code,"上游任务未接受",503));
|
||||||
tasks.completeMockTasks();
|
tasks.completeMockTasks();
|
||||||
tasks.completeMockTasks();
|
tasks.completeMockTasks();
|
||||||
assertEquals("failed",status());
|
assertEquals("failed",status());
|
||||||
@@ -91,7 +94,7 @@ class AsyncTaskPostgresTest {
|
|||||||
assertEquals(0,cost().signum());
|
assertEquals(0,cost().signum());
|
||||||
assertEquals(0,jdbc.queryForObject("SELECT cost_amount FROM usage_events WHERE provider_task_id=?",BigDecimal.class,taskId).signum());
|
assertEquals(0,jdbc.queryForObject("SELECT cost_amount FROM usage_events WHERE provider_task_id=?",BigDecimal.class,taskId).signum());
|
||||||
assertEquals(0,jdbc.queryForObject("SELECT count(*) FROM wallet_entries WHERE user_id=?",Integer.class,userId));
|
assertEquals(0,jdbc.queryForObject("SELECT count(*) FROM wallet_entries WHERE user_id=?",Integer.class,userId));
|
||||||
verify(provider,times(1)).submit(anyString(),anyString(),anyString(),anyString());
|
verify(provider,times(1)).submit(anyString(),anyString(),anyString(),anyString(),anyString());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test void refundAndTaskStateRollBackTogetherWhenAuditFails() {
|
@Test void refundAndTaskStateRollBackTogetherWhenAuditFails() {
|
||||||
@@ -108,7 +111,7 @@ class AsyncTaskPostgresTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test void successfulSettlementRollsBackForRetryAndCannotBeRefundedByLateFailure() {
|
@Test void successfulSettlementRollsBackForRetryAndCannotBeRefundedByLateFailure() {
|
||||||
when(provider.poll("provider-vast","upstream-task")).thenReturn(Map.of("data",Map.of("status","success","progress",100,"output",Map.of("model_url","https://example.test/model.glb"))));
|
when(provider.poll("provider-vast","model-mock-3d","v3.1-20260211","upstream-task",null)).thenReturn(Map.of("data",Map.of("status","success","progress",100,"output",Map.of("model_url","https://example.test/model.glb"))));
|
||||||
doThrow(new IllegalStateException("simulated database write failure")).when(usage).updateTask(eq(taskId),eq("succeeded"),anyMap(),isNull(),isNull());
|
doThrow(new IllegalStateException("simulated database write failure")).when(usage).updateTask(eq(taskId),eq("succeeded"),anyMap(),isNull(),isNull());
|
||||||
assertThrows(IllegalStateException.class,this::poll);
|
assertThrows(IllegalStateException.class,this::poll);
|
||||||
assertEquals("running",status());
|
assertEquals("running",status());
|
||||||
@@ -128,7 +131,7 @@ class AsyncTaskPostgresTest {
|
|||||||
@ParameterizedTest
|
@ParameterizedTest
|
||||||
@ValueSource(strings={"UPSTREAM_PROVIDER_ERROR", "UPSTREAM_AUTH_FAILED", "UPSTREAM_RATE_LIMITED"})
|
@ValueSource(strings={"UPSTREAM_PROVIDER_ERROR", "UPSTREAM_AUTH_FAILED", "UPSTREAM_RATE_LIMITED"})
|
||||||
void pollingErrorsKeepTheHoldAndClearOnRecovery(String code) {
|
void pollingErrorsKeepTheHoldAndClearOnRecovery(String code) {
|
||||||
when(provider.poll("provider-vast","upstream-task"))
|
when(provider.poll("provider-vast","model-mock-3d","v3.1-20260211","upstream-task",null))
|
||||||
.thenThrow(new ApiException(code,"private upstream details",502))
|
.thenThrow(new ApiException(code,"private upstream details",502))
|
||||||
.thenReturn(Map.of("data",Map.of("status","running","progress",75)));
|
.thenReturn(Map.of("data",Map.of("status","running","progress",75)));
|
||||||
poll();
|
poll();
|
||||||
@@ -160,7 +163,7 @@ class AsyncTaskPostgresTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test void confirmedUpstreamFailureReleasesHoldImmediately() {
|
@Test void confirmedUpstreamFailureReleasesHoldImmediately() {
|
||||||
when(provider.poll("provider-vast","upstream-task")).thenReturn(Map.of("data",Map.of("status","failed")));
|
when(provider.poll("provider-vast","model-mock-3d","v3.1-20260211","upstream-task",null)).thenReturn(Map.of("data",Map.of("status","failed")));
|
||||||
poll();
|
poll();
|
||||||
assertEquals("failed",status());
|
assertEquals("failed",status());
|
||||||
assertEquals(0,wallet("reserved_balance").signum());
|
assertEquals(0,wallet("reserved_balance").signum());
|
||||||
@@ -173,13 +176,13 @@ class AsyncTaskPostgresTest {
|
|||||||
OffsetDateTime migratedDeadline = jdbc.queryForObject("SELECT provider_deadline_at FROM model_tasks WHERE id=?",OffsetDateTime.class,taskId);
|
OffsetDateTime migratedDeadline = jdbc.queryForObject("SELECT provider_deadline_at FROM model_tasks WHERE id=?",OffsetDateTime.class,taskId);
|
||||||
assertEquals(Duration.ofHours(1),Duration.between(createdAt,migratedDeadline));
|
assertEquals(Duration.ofHours(1),Duration.between(createdAt,migratedDeadline));
|
||||||
jdbc.update("UPDATE model_tasks SET status='queued',provider_task_id=NULL,provider_deadline_at=NULL WHERE id=?",taskId);
|
jdbc.update("UPDATE model_tasks SET status='queued',provider_task_id=NULL,provider_deadline_at=NULL WHERE id=?",taskId);
|
||||||
when(provider.submit(anyString(),anyString(),anyString(),anyString())).thenReturn(Map.of("data",Map.of("task_id","upstream-task")));
|
when(provider.submit(anyString(),anyString(),anyString(),anyString(),anyString())).thenReturn(Map.of("data",Map.of("task_id","upstream-task")));
|
||||||
tasks.completeMockTasks();
|
tasks.completeMockTasks();
|
||||||
OffsetDateTime submittedAt = jdbc.queryForObject("SELECT updated_at FROM model_tasks WHERE id=?",OffsetDateTime.class,taskId);
|
OffsetDateTime submittedAt = jdbc.queryForObject("SELECT updated_at FROM model_tasks WHERE id=?",OffsetDateTime.class,taskId);
|
||||||
OffsetDateTime deadline = jdbc.queryForObject("SELECT provider_deadline_at FROM model_tasks WHERE id=?",OffsetDateTime.class,taskId);
|
OffsetDateTime deadline = jdbc.queryForObject("SELECT provider_deadline_at FROM model_tasks WHERE id=?",OffsetDateTime.class,taskId);
|
||||||
assertEquals(Duration.ofMinutes(2),Duration.between(submittedAt,deadline));
|
assertEquals(Duration.ofMinutes(2),Duration.between(submittedAt,deadline));
|
||||||
tasks.completeMockTasks();
|
tasks.completeMockTasks();
|
||||||
verify(provider,times(1)).submit(anyString(),anyString(),anyString(),anyString());
|
verify(provider,times(1)).submit(anyString(),anyString(),anyString(),anyString(),anyString());
|
||||||
verify(provider,never()).poll(anyString(),anyString());
|
verify(provider,never()).poll(anyString(),anyString());
|
||||||
assertEquals("running",status());
|
assertEquals("running",status());
|
||||||
}
|
}
|
||||||
|
|||||||
+1
@@ -51,6 +51,7 @@ class UsageCapacityPostgresTest {
|
|||||||
@Test void liveCapacitySeparatesQueuedWorkAndKeepsEveryGroupAndLongRunningTask() {
|
@Test void liveCapacitySeparatesQueuedWorkAndKeepsEveryGroupAndLongRunningTask() {
|
||||||
GatewayGuard guard = mock(GatewayGuard.class);
|
GatewayGuard guard = mock(GatewayGuard.class);
|
||||||
when(guard.defaultModelConcurrency()).thenReturn(7);
|
when(guard.defaultModelConcurrency()).thenReturn(7);
|
||||||
|
when(guard.modelConcurrencyEnabled()).thenReturn(true);
|
||||||
when(guard.defaultKeyConcurrency()).thenReturn(3);
|
when(guard.defaultKeyConcurrency()).thenReturn(3);
|
||||||
when(guard.queueWaitMs()).thenReturn(4500L);
|
when(guard.queueWaitMs()).thenReturn(4500L);
|
||||||
when(guard.capacitySnapshot(anyList())).thenAnswer(invocation -> {
|
when(guard.capacitySnapshot(anyList())).thenAnswer(invocation -> {
|
||||||
|
|||||||
@@ -0,0 +1,69 @@
|
|||||||
|
# 企业网关增量建设(2026-10-13)
|
||||||
|
|
||||||
|
用户确认:在现有 MOYI 系统补齐 Sub2API 对比中发现的能力,保留 Java/Vue、账户、余额、权限、历史记录及发布体系。当前服务 B/G 端,通过合同对公付款;支付及订阅能力后置。现有管理员入账与审计流程继续使用。
|
||||||
|
|
||||||
|
## 本轮验收范围
|
||||||
|
|
||||||
|
- [x] 可用状态与真实适配器一致;标准 Embeddings 接口及按输入用量结算。
|
||||||
|
- [x] Messages、Responses、Gemini 原生协议入口,保留工具调用及流式事件;不支持的上游协议明确拒绝。
|
||||||
|
- [x] 渠道账号健康、暂时隔离、安全重试、会话绑定;跨渠道切换不得越过模型授权或改变本次用户售价。
|
||||||
|
- [x] 版本化价格快照,缓存写入、上下文阶梯;采购成本独立于用户扣费,未知成本不得当零成本。
|
||||||
|
- [x] 调用中断及用量缺失进入成本待核状态,支持人工对账与审计,避免重复扣费。
|
||||||
|
- [x] 用户总并发及请求限额,Key 到期/IP/总额和周期预算,所有协议共用准入与扣费限制。
|
||||||
|
- [x] 运营健康及错误率告警、持久化状态、冷却与通知配置。
|
||||||
|
- [x] TOTP 双因素认证、重放保护、恢复流程;后台及用户设置可操作。
|
||||||
|
- [x] 新增 V35–V40 迁移;单元与隔离 PostgreSQL、Redis 验证及前端类型检查通过。
|
||||||
|
|
||||||
|
## 后置范围
|
||||||
|
|
||||||
|
自助充值(支付宝/微信/Stripe/易支付)、用户付费套餐与订阅账号接入。本轮不要求用户提供商户或订阅账号凭据。
|
||||||
|
|
||||||
|
## 实施原则
|
||||||
|
|
||||||
|
以当前生产提交 512ca522(维护文档提交 1528b718)为起点,新增迁移,不修改已执行的迁移。所有金额采用人民币 Decimal/BigDecimal,保存调用时的价目快照。故障切换只在已确认未产生输出且允许重试的错误上进行;已经输出的流式响应不重新请求。对外接口和管理入口均保持原有鉴权边界。新增凭据加密存储,不返回给列表、日志或导出。外部源码仅供行为比较,独立实现。
|
||||||
|
|
||||||
|
## 实施记录
|
||||||
|
|
||||||
|
2026-10-13:完成上述增量实现,保留现有合同对公付款后的管理员额度入账与审计。
|
||||||
|
|
||||||
|
## 入口与操作
|
||||||
|
|
||||||
|
| 入口 | 可操作内容 |
|
||||||
|
|---|---|
|
||||||
|
| 管理端 → 企业调用策略 | 用户全部 Key 共用的 RPM、总并发、人民币月预算;默认 120 RPM / 16 并发,预算不限 |
|
||||||
|
| 用户端 → Key 安全与预算 | 到期时间、IPv4/IPv6/CIDR 白名单、总/日/月预算;周期按北京时间 |
|
||||||
|
| 管理端 → 价格版本与对账 | 维护人民币/百万 Token 的普通输入、输出、缓存读、5 分钟/1 小时缓存写入、上下文阶梯;采购成本独立填写;保留历史版本 |
|
||||||
|
| 同一页面 → 本月对账 | 查看用户消费、已知采购成本/利润,按渠道账单填写实际成本和依据;不更改用户扣费 |
|
||||||
|
| 同一页面 → 原生协议 | 按路由启用 Messages、Responses、Gemini,启用已授权的同模型备用渠道;上线默认不自动打开 |
|
||||||
|
| 管理端 → 渠道健康与告警 | 账号指纹、响应延迟、冷却状态、错误率阈值及站内告警;邮件通知默认关闭 |
|
||||||
|
| 账号设置 → 双因素认证 | 密码确认后配置认证器;6 位 TOTP,10 个一次性恢复码只展示一次 |
|
||||||
|
| 用户端 → 接入文档 | 原生协议请求示例、鉴权及使用限制;在线调试可提交 Embeddings |
|
||||||
|
|
||||||
|
定价单位 `1M tokens` 等于 `1,000,000 tokens`(一百万 Token)。手工调整绝对金额后显示“自定义价格”,避免继续显示失效的旧折扣。原有渠道原价乘折扣的批量定价功能保留。本轮迁移不重算现有售价,不把未知采购成本填成 0。
|
||||||
|
|
||||||
|
## 计费与可靠性边界
|
||||||
|
|
||||||
|
- 每次调用冻结售价版本和采购价格;价格编辑使用乐观冲突检查。撤下价格后新请求停止计费准入,已受理请求按原快照结算。
|
||||||
|
- 所有 Key 共用用户预算与并发;异步任务也占用户并发,长任务 Redis 租约不会被短请求缩短。预算按已消费和在途预留检查;上游异常超出预留仍按真实用量记账,可能形成欠款并阻止后续调用。
|
||||||
|
- 原生协议当前覆盖文本与本地函数工具。Responses 为完整上下文、`store=false`;不支持 `previous_response_id`、云端会话和后台任务。媒体、托管工具、外部缓存引用需要独立计费适配器,当前明确拒绝。
|
||||||
|
- 原生接口不会自动假定所有渠道支持。管理员核实渠道能力后按路由启用;本轮使用本地 HTTP 模拟上游验证,没有批量发起收费模型调用。
|
||||||
|
- 账号明确返回 401/403/429 时可尝试账号池中其他 Key;跨路由切换需显式开启,仅限 Key 已授权、同逻辑模型/类型/上游模型名的最多 2 个备用路由。仅明确拒绝受理或账号全部冷却可切换;超时、已开始输出的流不重试。
|
||||||
|
- `X-Moyi-Session` 提供账号亲和性,纳入调用凭据隔离;不绕过健康、授权或限额。
|
||||||
|
- 客户端断开时尽量继续读取最终用量;用量缺失或流不完整不估算用户扣费,释放预留并记录待对账成本。管理员可记录实际渠道损失。
|
||||||
|
- IP 白名单使用可信代理解析后的地址;生产 Nginx 覆盖客户端伪造的转发头。直接运行后端时默认不信任转发头。
|
||||||
|
- 站内告警每分钟评估;邮件仅管理员启用且 SMTP 已配置时发送,最多尝试 3 次。发送可能因进程中断重复,使用告警编号识别。
|
||||||
|
- TOTP 密钥加密,恢复码只存哈希;动态码防重放,错误尝试 5 次后限制 5 分钟。已有用户不会被自动开启 MFA。
|
||||||
|
|
||||||
|
## 验证记录
|
||||||
|
|
||||||
|
- 2026-10-13 18:56:`scripts/test_enterprise_increment.py`,118 个 Java 测试全部通过,0 跳过;使用全新本机 PostgreSQL 17,完成后停止。日志:`.tools/enterprise-test-67yhxfz7/tests.log`。
|
||||||
|
- Redis Lua 实测通过:共享用户并发、拒绝超额、释放、RPM、长短租约共存;仅使用唯一前缀临时键并清理,不触碰业务键。
|
||||||
|
- 部署工具:37 项中 33 项通过;4 项旧凭据维护测试要求单独固定端口 PostgreSQL,未启动该专用环境,明确跳过。真实 Docker Compose 插值和 HTTPS 覆盖测试通过。
|
||||||
|
- Vue 类型检查通过;本地隔离浏览器完成登录、企业预算保存、空价格首次配置/重复保存、告警页面、Key 空状态、MFA 设置展示验证。
|
||||||
|
- 最终发布包另执行 Maven clean verify、18 项前端测试与 Vite 构建;线上验收结果在发布后补充。
|
||||||
|
|
||||||
|
## 部署与回退
|
||||||
|
|
||||||
|
使用 `scripts/package-deployment.ps1` 从干净提交生成 JAR、静态资源和校验清单。`infra/moyi/upgrade_release.py` 先备份现有数据库,再构建并启动同一个 Compose 项目,不执行 `docker compose down`。保留旧镜像和备份。
|
||||||
|
|
||||||
|
V35–V40 为新增表、列、索引和价格历史触发器。紧急回退优先切回旧应用镜像,保留新增数据库结构;除非明确接受备份后数据丢失,不覆盖恢复生产数据库。旧应用不理解新预算/MFA/原生协议,因此回退后应停止依赖这些新增能力。
|
||||||
@@ -30,6 +30,10 @@ def execute(command, capture=False, data=None):
|
|||||||
# shell stdin. This is especially important when deploy.py is invoked from
|
# shell stdin. This is especially important when deploy.py is invoked from
|
||||||
# an SSH heredoc; only an explicit data payload may be sent to a process.
|
# an SSH heredoc; only an explicit data payload may be sent to a process.
|
||||||
options = {"input": data} if data is not None else {"stdin": subprocess.DEVNULL}
|
options = {"input": data} if data is not None else {"stdin": subprocess.DEVNULL}
|
||||||
|
if command[:2] == ["docker", "compose"] and "--env-file" in command:
|
||||||
|
environment = os.environ.copy()
|
||||||
|
environment["MOYI_RUNTIME_ENV_FILE"] = str(Path(command[command.index("--env-file") + 1]).resolve())
|
||||||
|
options["env"] = environment
|
||||||
result = subprocess.run(command, cwd=HERE, capture_output=capture,
|
result = subprocess.run(command, cwd=HERE, capture_output=capture,
|
||||||
text=True, encoding="utf-8", errors="replace", **options)
|
text=True, encoding="utf-8", errors="replace", **options)
|
||||||
if result.returncode:
|
if result.returncode:
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ services:
|
|||||||
# Pass per-route key pools from the deployment secret file without
|
# Pass per-route key pools from the deployment secret file without
|
||||||
# committing secret names or values to the repository.
|
# committing secret names or values to the repository.
|
||||||
env_file:
|
env_file:
|
||||||
- .env.cloud
|
- ${MOYI_RUNTIME_ENV_FILE:-.env.cloud}
|
||||||
environment:
|
environment:
|
||||||
CONTROL_PLANE_HOST: 0.0.0.0
|
CONTROL_PLANE_HOST: 0.0.0.0
|
||||||
CONTROL_PLANE_PORT: 8787
|
CONTROL_PLANE_PORT: 8787
|
||||||
@@ -28,6 +28,7 @@ services:
|
|||||||
MOYI_REDIS_SSL: ${MOYI_REDIS_SSL:-false}
|
MOYI_REDIS_SSL: ${MOYI_REDIS_SSL:-false}
|
||||||
MOYI_REDIS_KEY_PREFIX: ${MOYI_REDIS_KEY_PREFIX:-moyi:test:}
|
MOYI_REDIS_KEY_PREFIX: ${MOYI_REDIS_KEY_PREFIX:-moyi:test:}
|
||||||
FLYWAY_ENABLED: true
|
FLYWAY_ENABLED: true
|
||||||
|
MOYI_FORWARD_HEADERS_STRATEGY: native
|
||||||
FLYWAY_BASELINE_ON_MIGRATE: false
|
FLYWAY_BASELINE_ON_MIGRATE: false
|
||||||
AUTH_SECURE_COOKIE: false
|
AUTH_SECURE_COOKIE: false
|
||||||
AUTH_PASSWORD_PEPPER: ${AUTH_PASSWORD_PEPPER:?Initialize auth secrets}
|
AUTH_PASSWORD_PEPPER: ${AUTH_PASSWORD_PEPPER:?Initialize auth secrets}
|
||||||
|
|||||||
@@ -43,6 +43,7 @@ services:
|
|||||||
SPRING_DATASOURCE_PASSWORD: ${POSTGRES_PASSWORD}
|
SPRING_DATASOURCE_PASSWORD: ${POSTGRES_PASSWORD}
|
||||||
SPRING_DATA_REDIS_URL: redis://:${REDIS_PASSWORD}@redis:6379/0
|
SPRING_DATA_REDIS_URL: redis://:${REDIS_PASSWORD}@redis:6379/0
|
||||||
FLYWAY_ENABLED: true
|
FLYWAY_ENABLED: true
|
||||||
|
MOYI_FORWARD_HEADERS_STRATEGY: native
|
||||||
depends_on:
|
depends_on:
|
||||||
postgres:
|
postgres:
|
||||||
condition: service_healthy
|
condition: service_healthy
|
||||||
|
|||||||
@@ -23,7 +23,7 @@ location /api/ {
|
|||||||
proxy_read_timeout 120s;
|
proxy_read_timeout 120s;
|
||||||
proxy_next_upstream off;
|
proxy_next_upstream off;
|
||||||
}
|
}
|
||||||
location /v1/ {
|
location ~ ^/v1(?:beta)?/ {
|
||||||
proxy_pass http://moyi-api:8787;
|
proxy_pass http://moyi-api:8787;
|
||||||
proxy_http_version 1.1;
|
proxy_http_version 1.1;
|
||||||
proxy_set_header Host $http_host;
|
proxy_set_header Host $http_host;
|
||||||
|
|||||||
@@ -229,7 +229,8 @@ class DeploymentTests(unittest.TestCase):
|
|||||||
self.assertEqual(api["SPRING_DATASOURCE_PASSWORD"], "test-db-$value#literal")
|
self.assertEqual(api["SPRING_DATASOURCE_PASSWORD"], "test-db-$value#literal")
|
||||||
self.assertEqual(api["ADMIN_PASSWORD"], "Qa-Test42!")
|
self.assertEqual(api["ADMIN_PASSWORD"], "Qa-Test42!")
|
||||||
self.assertNotIn("MOYI_REDIS_PASSWORD", cfg["services"]["frontend"].get("environment", {}))
|
self.assertNotIn("MOYI_REDIS_PASSWORD", cfg["services"]["frontend"].get("environment", {}))
|
||||||
self.assertNotIn("OSS_ACCESS_KEY_SECRET", api)
|
self.assertFalse(api.get("OSS_ACCESS_KEY_SECRET"))
|
||||||
|
self.assertNotIn("OSS_ACCESS_KEY_SECRET", cfg["services"]["frontend"].get("environment", {}))
|
||||||
if mode == "https":
|
if mode == "https":
|
||||||
ports = cfg["services"]["frontend"]["ports"]
|
ports = cfg["services"]["frontend"]["ports"]
|
||||||
self.assertEqual({str(p["published"]) for p in ports}, {"80", "443", "8080"})
|
self.assertEqual({str(p["published"]) for p in ports}, {"80", "443", "8080"})
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
import { reactive, ref } from 'vue'
|
import { reactive, ref } from 'vue'
|
||||||
import { authClient, type AuthUser } from '../services/auth'
|
import { authClient, type AuthUser } from '../services/auth'
|
||||||
import UiModal from './UiModal.vue'
|
import UiModal from './UiModal.vue'
|
||||||
|
import MfaSettings from './MfaSettings.vue'
|
||||||
|
|
||||||
const props = defineProps<{ user: AuthUser }>()
|
const props = defineProps<{ user: AuthUser }>()
|
||||||
const emit = defineEmits<{ close: []; saved: [user: AuthUser]; notify: [message: string] }>()
|
const emit = defineEmits<{ close: []; saved: [user: AuthUser]; notify: [message: string] }>()
|
||||||
@@ -47,6 +48,7 @@ async function save() {
|
|||||||
<input id="account-confirm-password" v-model="form.confirmPassword" type="password" class="text-input" maxlength="256" autocomplete="new-password" placeholder="留空表示不修改" />
|
<input id="account-confirm-password" v-model="form.confirmPassword" type="password" class="text-input" maxlength="256" autocomplete="new-password" placeholder="留空表示不修改" />
|
||||||
<p class="form-hint">普通用户新密码至少 8 位;超级管理员按实际设置保存。</p>
|
<p class="form-hint">普通用户新密码至少 8 位;超级管理员按实际设置保存。</p>
|
||||||
</form>
|
</form>
|
||||||
|
<MfaSettings />
|
||||||
<template #footer>
|
<template #footer>
|
||||||
<button class="button secondary" :disabled="saving" @click="emit('close')">取消</button>
|
<button class="button secondary" :disabled="saving" @click="emit('close')">取消</button>
|
||||||
<button class="button primary" type="submit" form="account-settings" :disabled="saving">{{ saving ? '保存中…' : '保存设置' }}</button>
|
<button class="button primary" type="submit" form="account-settings" :disabled="saving">{{ saving ? '保存中…' : '保存设置' }}</button>
|
||||||
|
|||||||
@@ -0,0 +1,53 @@
|
|||||||
|
<script setup lang="ts">
|
||||||
|
import { computed, onMounted, reactive, ref } from 'vue'
|
||||||
|
import { catalogClient, type CatalogModel } from '../services/catalog'
|
||||||
|
import { enterprise } from '../services/enterprise'
|
||||||
|
type Json = Record<string, unknown>
|
||||||
|
const models = ref<CatalogModel[]>([]), selected = ref(''), loaded = ref(false), busy = ref(false), notice = ref(''), history = ref<Json[]>([]), records = ref<Json[]>([]), summary = ref<Json>({}), page = ref(1)
|
||||||
|
const retail = ref<Json>({}), procurement = ref<Json | null>(null), retailFields = reactive<Record<string, string>>({}), costFields = reactive<Record<string, string>>({}), costKnown = ref(false), retailTiers = ref('[]'), costTiers = ref('[]'), protocols = ref<string[]>([])
|
||||||
|
const fields = [{ id: 'input', label: '普通输入' }, { id: 'output', label: '输出' }, { id: 'cachedInput', label: '缓存命中输入' }, { id: 'cacheWrite5m', label: '缓存写入 · 5 分钟' }, { id: 'cacheWrite1h', label: '缓存写入 · 1 小时' }]
|
||||||
|
const reconcileId = ref(''), reconcileCost = ref(''), reconcileNote = ref('')
|
||||||
|
const expectedRetail = ref<Json | null>(null)
|
||||||
|
const failoverEnabled = computed({ get: () => Boolean(models.value.find(m => m.id === selected.value)?.capabilities.failover), set: value => { const model = models.value.find(m => m.id === selected.value); if (model) model.capabilities.failover = value } })
|
||||||
|
const money = (v: unknown) => v == null ? '待核' : `¥ ${Number(v).toLocaleString('zh-CN', { maximumFractionDigits: 8 })}`
|
||||||
|
const state = (s: unknown) => ({ unknown: '成本未知', pending: '待对账', estimated: '按采购价估算', reconciled: '已核对' }[String(s)] || String(s))
|
||||||
|
async function refresh() { const data = await enterprise<{ records: Json[]; summary: Json }>(`/api/admin/finance/reconciliation?page=${page.value}`); records.value = data.records; summary.value = data.summary }
|
||||||
|
onMounted(async () => { try { models.value = (await catalogClient.listAdminModels()).models.filter(m => m.mode !== 'async' && ['text', 'code', 'multimodal', 'embedding'].includes(m.type)); await refresh() } catch (e) { notice.value = String(e) } })
|
||||||
|
async function load() {
|
||||||
|
loaded.value = false; notice.value = ''; if (!selected.value) return
|
||||||
|
busy.value = true
|
||||||
|
try {
|
||||||
|
const data = await enterprise<{ current: Json; history: Json[] }>(`/api/admin/finance/prices/${selected.value}`)
|
||||||
|
expectedRetail.value = data.current.pricing ? JSON.parse(String(data.current.pricing)) : null
|
||||||
|
retail.value = expectedRetail.value || {}
|
||||||
|
procurement.value = data.current.procurement_pricing ? JSON.parse(String(data.current.procurement_pricing)) : null
|
||||||
|
history.value = data.history; costKnown.value = !!procurement.value
|
||||||
|
for (const field of fields) { retailFields[field.id] = String(retail.value[field.id] ?? ''); costFields[field.id] = String(procurement.value?.[field.id] ?? '') }
|
||||||
|
retailTiers.value = JSON.stringify(retail.value.tiers || [], null, 2); costTiers.value = JSON.stringify(procurement.value?.tiers || [], null, 2)
|
||||||
|
protocols.value = [...(models.value.find(m => m.id === selected.value)?.capabilities.protocols as string[] || [])]; loaded.value = true
|
||||||
|
} catch (e) { notice.value = String(e) } finally { busy.value = false }
|
||||||
|
}
|
||||||
|
function schedule(base: Json, values: Record<string, string>, tiers: string) { const out: Json = { ...base, unit: 'CNY / 1M tokens', tiers: JSON.parse(tiers) }; for (const f of fields) { if (values[f.id]?.trim()) out[f.id] = values[f.id]; else delete out[f.id] }; delete out.discount; out.discountLabel = '自定义价格'; return out }
|
||||||
|
async function save() {
|
||||||
|
busy.value = true
|
||||||
|
try { await enterprise(`/api/admin/finance/prices/${selected.value}`, { pricing: schedule(retail.value, retailFields, retailTiers.value), procurementPricing: costKnown.value ? schedule(procurement.value || {}, costFields, costTiers.value) : null, expectedPricing: expectedRetail.value, expectedProcurementPricing: procurement.value }); await load(); notice.value = '价格版本已保存,新请求使用新价格。' } catch (e) { notice.value = String(e) } finally { busy.value = false }
|
||||||
|
}
|
||||||
|
async function saveProtocols() { busy.value = true; try { await enterprise(`/api/admin/finance/protocols/${selected.value}`, { protocols: protocols.value, failover: failoverEnabled.value }); const model = models.value.find(m => m.id === selected.value); if (model) model.capabilities.protocols = [...protocols.value]; notice.value = '协议配置已保存,请使用测试 Key 验证渠道接口。' } catch (e) { notice.value = String(e) } finally { busy.value = false } }
|
||||||
|
async function reconcile() { busy.value = true; try { await enterprise(`/api/admin/finance/reconciliation/${reconcileId.value}`, { procurementCost: reconcileCost.value, note: reconcileNote.value }, 'POST'); reconcileId.value = ''; reconcileCost.value = ''; reconcileNote.value = ''; await refresh(); notice.value = '采购成本已核对,用户扣费不变。' } catch (e) { notice.value = String(e) } finally { busy.value = false } }
|
||||||
|
</script>
|
||||||
|
<template>
|
||||||
|
<section class="page-heading compact"><div><span>FINANCE</span><h1>价格版本与成本对账</h1><p>人民币统一结算。采购成本与用户售价独立维护,未知成本不计入已知利润。</p></div></section>
|
||||||
|
<p role="status">{{ notice }}</p>
|
||||||
|
<section class="panel finance-card"><h3>Token 价格规则</h3><label class="form-label">模型路由<select v-model="selected" class="text-input" @change="load"><option value="">请选择模型</option><option v-for="m in models" :key="m.id" :value="m.id">{{ m.publicName }} · {{ m.sourceLabel || m.id }}</option></select></label>
|
||||||
|
<template v-if="loaded"><form @submit.prevent="save"><div class="finance-grid"><div><h4>用户售价 · 元/百万 Token</h4><label v-for="f in fields" :key="f.id" class="form-label">{{ f.label }}<input v-model="retailFields[f.id]" class="text-input" inputmode="decimal" :required="['input', 'output'].includes(f.id)" placeholder="按普通输入价格" /></label><details><summary>上下文阶梯规则</summary><p>按 aboveInputTokens 从小到大排列,超过阈值时覆盖对应价格。</p><textarea v-model="retailTiers" class="text-input" rows="6" placeholder='[{"aboveInputTokens":200000,"input":4,"output":16}]' /></details></div><div><h4>采购成本 · 元/百万 Token</h4><label><input v-model="costKnown" type="checkbox" /> 已核实采购成本</label><template v-if="costKnown"><label v-for="f in fields" :key="f.id" class="form-label">{{ f.label }}<input v-model="costFields[f.id]" class="text-input" inputmode="decimal" :required="['input', 'output'].includes(f.id)" placeholder="按普通输入价格" /></label><details><summary>采购上下文阶梯规则</summary><textarea v-model="costTiers" class="text-input" rows="6" /></details></template><p v-else>采购成本保留未知状态,后续可根据渠道账单核对。</p></div></div><button class="button primary" :disabled="busy">保存价格新版本</button></form>
|
||||||
|
<details><summary>最近 {{ history.length }} 个版本</summary><div class="history"><div v-for="h in history" :key="String(h.id)"><strong>#{{ h.id }} · {{ h.created_at }}</strong><pre>{{ JSON.stringify(JSON.parse(String(h.pricing || '{}')), null, 2) }}</pre></div></div></details>
|
||||||
|
<h4>原生协议</h4><p>仅为已确认支持原生接口的渠道启用。未勾选的原生协议会明确拒绝请求。标准 Chat/Embeddings 由模型类型决定。</p><div class="protocols"><label v-for="p in ['messages', 'responses', 'gemini']" :key="p"><input v-model="protocols" type="checkbox" :value="p" /> {{ p }}</label><button class="button secondary" :disabled="busy" @click="saveProtocols">保存协议</button></div>
|
||||||
|
<label><input v-model="failoverEnabled" type="checkbox" /> 允许切换到已授权的同模型备用渠道(用户售价不变)</label><p>开关与上方协议配置一同保存。仅在明确拒绝请求时切换;超时和已开始输出的请求不重试。</p>
|
||||||
|
</template>
|
||||||
|
</section>
|
||||||
|
<section class="panel finance-card"><h3>本月对账</h3><div class="finance-grid"><p>用户消费 <strong>{{ money(summary.revenue) }}</strong></p><p>已知采购成本 <strong>{{ money(summary.known_cost) }}</strong></p><p>已知利润 <strong>{{ money(summary.known_margin) }}</strong></p><p>待核记录 <strong>{{ summary.pending || 0 }}</strong></p></div>
|
||||||
|
<form v-if="reconcileId" @submit.prevent="reconcile"><h4>核对 {{ reconcileId }}</h4><label class="form-label">实际采购成本(元)<input v-model="reconcileCost" class="text-input" inputmode="decimal" required /></label><label class="form-label">渠道账单依据<textarea v-model="reconcileNote" class="text-input" minlength="3" maxlength="2000" required /></label><button class="button primary" :disabled="busy">确认成本</button><button type="button" class="button secondary" @click="reconcileId = ''">取消</button></form>
|
||||||
|
<div class="responsive-table"><table><thead><tr><th>时间/模型</th><th>调用</th><th>用户消费</th><th>采购成本</th><th>核对状态</th><th></th></tr></thead><tbody><tr v-for="r in records" :key="String(r.id)"><td>{{ r.created_at }}<br />{{ r.public_name }}</td><td>{{ r.status }}</td><td>{{ money(r.cost_amount) }}</td><td>{{ money(r.procurement_cost) }}</td><td>{{ state(r.cost_status) }}</td><td><button class="button secondary small" @click="reconcileId = String(r.id); reconcileCost = ''; reconcileNote = ''">核对</button></td></tr></tbody></table></div><div class="protocols"><button class="button secondary" :disabled="page <= 1" @click="page--; refresh().catch(e => notice = String(e))">上一页</button><span>第 {{ page }} 页</span><button class="button secondary" :disabled="records.length < 50" @click="page++; refresh().catch(e => notice = String(e))">下一页</button></div>
|
||||||
|
</section>
|
||||||
|
</template>
|
||||||
|
<style scoped>.finance-card{padding:24px;margin-bottom:22px}.finance-card .form-label{flex-direction:column;gap:8px}.finance-card p{font-size:13px;color:#64736d;line-height:1.7}.finance-grid{display:grid;grid-template-columns:1fr 1fr;gap:24px}.finance-card button,.finance-card details{margin-top:16px}.finance-card summary{cursor:pointer}.history{max-height:340px;overflow:auto}.history pre{white-space:pre-wrap;background:#f3f6f4;padding:12px}.protocols{display:flex;align-items:center;gap:18px;flex-wrap:wrap}@media(max-width:700px){.finance-grid{grid-template-columns:1fr}}</style>
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
<script setup lang="ts">
|
||||||
|
import { onMounted, onUnmounted, reactive, ref } from 'vue'
|
||||||
|
import { enterprise } from '../services/enterprise'
|
||||||
|
type Row = Record<string, string | number | boolean | null>
|
||||||
|
const accounts = ref<Row[]>([]), alerts = ref<Row[]>([]), notice = ref(''), busy = ref(false)
|
||||||
|
const rule = reactive({ enabled: true, windowMinutes: 5, minimumRequests: 10, errorPercent: 20, cooldownMinutes: 30, notificationEnabled: false, notificationEmail: '' })
|
||||||
|
async function refresh(initial = false) { try { const data = await enterprise<{ accounts: Row[]; alerts: Row[]; rules: Row[] }>('/api/admin/operations'); accounts.value = data.accounts; alerts.value = data.alerts; if (initial && data.rules[0]) { const r = data.rules[0]; Object.assign(rule, { enabled: r.enabled, windowMinutes: Number(r.window_minutes), minimumRequests: Number(r.minimum_requests), errorPercent: Number(r.error_percent), cooldownMinutes: Number(r.cooldown_minutes), notificationEnabled: !!r.notification_enabled, notificationEmail: String(r.notification_email || '') }) } } catch (e) { notice.value = String(e) } }
|
||||||
|
let timer: ReturnType<typeof setInterval> | undefined
|
||||||
|
onMounted(() => { refresh(true); timer = setInterval(() => refresh(), 30000) }); onUnmounted(() => clearInterval(timer))
|
||||||
|
async function save() { busy.value = true; try { await enterprise('/api/admin/operations/rules/gateway-errors', rule); notice.value = '告警规则已保存' } catch (e) { notice.value = String(e) } finally { busy.value = false } }
|
||||||
|
async function ack(id: unknown) { try { await enterprise(`/api/admin/operations/alerts/${id}/acknowledge`, {}, 'POST'); await refresh() } catch (e) { notice.value = String(e) } }
|
||||||
|
const state = (value: unknown) => ({ open: '待处理', acknowledged: '已确认', resolved: '已恢复', disabled: '通知关闭', pending: '待发送', retry: '待重试', sent: '已发送', failed: '发送失败' }[String(value)] || String(value))
|
||||||
|
</script>
|
||||||
|
<template>
|
||||||
|
<section class="page-heading compact"><div><span>OPERATIONS</span><h1>渠道健康与告警</h1><p>每 30 秒刷新。账号异常会暂时隔离;恢复后可重新参与调度。</p></div><button class="button secondary" @click="refresh()">刷新</button></section><p role="status">{{ notice }}</p>
|
||||||
|
<section class="panel ops-card"><h3>错误率告警</h3><form @submit.prevent="save"><label><input v-model="rule.enabled" type="checkbox" /> 启用站内告警</label><div class="ops-grid"><label class="form-label">统计窗口(分钟)<input v-model.number="rule.windowMinutes" type="number" min="1" max="1440" class="text-input" required /></label><label class="form-label">最小请求数<input v-model.number="rule.minimumRequests" type="number" min="1" class="text-input" required /></label><label class="form-label">错误率阈值(%)<input v-model.number="rule.errorPercent" type="number" min="0" max="100" step="0.01" class="text-input" required /></label><label class="form-label">重复提醒间隔(分钟)<input v-model.number="rule.cooldownMinutes" type="number" min="1" max="1440" class="text-input" required /></label></div><button class="button primary" :disabled="busy">保存规则</button></form></section>
|
||||||
|
<section class="panel ops-card"><h3>邮件通知</h3><form @submit.prevent="save"><label><input v-model="rule.notificationEnabled" type="checkbox" /> 将新告警发送至指定邮箱</label><label class="form-label">告警接收邮箱<input v-model="rule.notificationEmail" type="email" class="text-input" :required="rule.notificationEnabled" /></label><p>使用平台已配置的邮件服务。默认关闭;发送失败最多尝试 3 次,告警编号可用于识别重复通知。</p><button class="button secondary" :disabled="busy">保存通知设置</button></form></section>
|
||||||
|
<section class="panel ops-card"><h3>最近告警</h3><div class="responsive-table"><table><thead><tr><th>时间</th><th>失败/总请求</th><th>错误率</th><th>状态/通知</th><th></th></tr></thead><tbody><tr v-for="a in alerts" :key="String(a.id)"><td>{{ a.created_at }}</td><td>{{ a.failed_requests }} / {{ a.total_requests }}</td><td>{{ a.error_percent }}%</td><td>{{ state(a.status) }} / {{ a.notification_status }}</td><td><button v-if="a.status === 'open'" class="button secondary small" @click="ack(a.id)">确认处理</button></td></tr><tr v-if="!alerts.length"><td colspan="5">暂无告警</td></tr></tbody></table></div></section>
|
||||||
|
<section class="panel ops-card"><h3>渠道账号健康</h3><p>仅显示不可逆的账号指纹,不显示上游密钥。延迟为上游响应头到达时间的平滑值。</p><div class="responsive-table"><table><thead><tr><th>模型/账号</th><th>连续异常</th><th>最近状态码</th><th>延迟</th><th>隔离至</th></tr></thead><tbody><tr v-for="a in accounts" :key="`${a.route_id}-${a.account}`"><td>{{ a.public_name || a.route_id }}<br /><code>{{ a.account }}</code></td><td>{{ a.failures }}</td><td>{{ a.last_status }}</td><td>{{ a.latency_ms }} ms</td><td>{{ a.cooldown_until || '正常' }}</td></tr><tr v-if="!accounts.length"><td colspan="5">调用后显示健康记录</td></tr></tbody></table></div></section>
|
||||||
|
</template>
|
||||||
|
<style scoped>.ops-card{padding:24px;margin-bottom:22px}.ops-card .form-label{flex-direction:column;gap:8px}.ops-card p{color:#64736d;font-size:13px}.ops-grid{display:grid;grid-template-columns:repeat(4,1fr);gap:16px}.ops-card button{margin-top:16px}@media(max-width:850px){.ops-grid{grid-template-columns:1fr 1fr}}</style>
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
<script setup lang="ts">
|
||||||
|
import { onMounted, reactive, ref } from 'vue'
|
||||||
|
import { catalogClient } from '../services/catalog'
|
||||||
|
import { enterprise } from '../services/enterprise'
|
||||||
|
const props = defineProps<{ admin?: boolean }>()
|
||||||
|
const choices = ref<{ id: string; name: string }[]>([]), selected = ref(''), busy = ref(false), notice = ref(''), loaded = ref(false)
|
||||||
|
const form = reactive({ requestsPerMinute: 120, maxConcurrency: 16, monthlyBudget: '', dailyBudget: '', totalBudget: '', expiresAt: '', ipAllowlist: '' })
|
||||||
|
onMounted(async () => { try { choices.value = props.admin ? (await catalogClient.listAdminUsers()).users.map(u => ({ id: u.id, name: `${u.displayName} · ${u.account}` })) : (await catalogClient.listKeys()).keys.map(k => ({ id: k.id, name: k.name })); } catch (e) { notice.value = String(e) } })
|
||||||
|
function path() { return props.admin ? `/api/admin/users/${selected.value}/gateway-policy` : `/api/keys/${selected.value}/policy` }
|
||||||
|
async function load() {
|
||||||
|
loaded.value = false; notice.value = ''; if (!selected.value) return
|
||||||
|
busy.value = true
|
||||||
|
try { const data = await enterprise<Record<string, string | number | null>>(path()); Object.assign(form, { requestsPerMinute: data.requests_per_minute ?? 120, maxConcurrency: data.max_concurrency ?? 16, monthlyBudget: data.monthly_budget ?? '', dailyBudget: data.daily_budget ?? '', totalBudget: data.total_budget ?? '', expiresAt: data.expires_at ? new Date(String(data.expires_at)).toLocaleString('sv-SE').replace(' ', 'T') : '', ipAllowlist: JSON.parse(String(data.ip_allowlist || '[]')).join('\n') }); loaded.value = true } catch (e) { notice.value = String(e) } finally { busy.value = false }
|
||||||
|
}
|
||||||
|
async function save() {
|
||||||
|
busy.value = true; notice.value = ''
|
||||||
|
try { await enterprise(path(), { ...form, expiresAt: form.expiresAt ? new Date(form.expiresAt).toISOString() : null, ipAllowlist: form.ipAllowlist.split(/[\s,]+/).filter(Boolean) }); notice.value = '策略已保存,对新请求生效。' } catch (e) { notice.value = e instanceof Error ? e.message : '保存失败' } finally { busy.value = false }
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
<template>
|
||||||
|
<section class="page-heading compact"><div><span>ACCESS POLICY</span><h1>{{ admin ? '企业用户调用策略' : 'API Key 安全与预算' }}</h1><p>{{ admin ? '同一用户的多个 Key 共用请求限额和人民币月预算。' : '为每个 Key 设置有效期、来源 IP 和人民币预算。' }}</p></div></section>
|
||||||
|
<section class="panel policy-card">
|
||||||
|
<p v-if="!admin && !choices.length">暂无 API Key,请先在「API Keys」页面创建。</p>
|
||||||
|
<label class="form-label">{{ admin ? '用户' : 'API Key' }}<select v-model="selected" class="text-input" :disabled="busy" @change="load"><option value="">请选择</option><option v-for="item in choices" :key="item.id" :value="item.id">{{ item.name }}</option></select></label>
|
||||||
|
<form v-if="loaded" @submit.prevent="save">
|
||||||
|
<div class="policy-grid" v-if="admin"><label class="form-label">每分钟请求数<input v-model.number="form.requestsPerMinute" type="number" min="1" max="100000" class="text-input" required /></label><label class="form-label">最大总并发<input v-model.number="form.maxConcurrency" type="number" min="1" max="10000" class="text-input" required /></label></div>
|
||||||
|
<template v-else><label class="form-label">有效期截止(本地时间)<input v-model="form.expiresAt" type="datetime-local" class="text-input" /></label><label class="form-label">IP 白名单<textarea v-model="form.ipAllowlist" class="text-input" rows="4" placeholder="每行一个 IPv4、IPv6 或 CIDR;留空为不限" /></label><div class="policy-grid"><label class="form-label">总预算(元)<input v-model="form.totalBudget" inputmode="decimal" class="text-input" placeholder="不限" /></label><label class="form-label">日预算(元)<input v-model="form.dailyBudget" inputmode="decimal" class="text-input" placeholder="不限" /></label></div></template>
|
||||||
|
<label class="form-label">月预算(元)<input v-model="form.monthlyBudget" inputmode="decimal" class="text-input" placeholder="不限" /></label><p>预算留空表示不限,0 表示不允许付费调用。日、月周期按北京时间计算,统计包含已结算用量与在途预留。调整预算不会修改钱包余额。</p><button class="button primary" :disabled="busy">{{ busy ? '保存中…' : '保存策略' }}</button>
|
||||||
|
</form><p role="status">{{ notice }}</p>
|
||||||
|
</section>
|
||||||
|
</template>
|
||||||
|
<style scoped>.policy-card{padding:24px;max-width:900px}.policy-card .form-label{flex-direction:column;gap:8px}.policy-card p{font-size:13px;color:#65736e;line-height:1.7}.policy-grid{display:grid;grid-template-columns:1fr 1fr;gap:18px}.policy-card textarea{resize:vertical}.policy-card button{margin-top:16px}@media(max-width:650px){.policy-grid{grid-template-columns:1fr}}</style>
|
||||||
@@ -0,0 +1,28 @@
|
|||||||
|
<script setup lang="ts">
|
||||||
|
import { onMounted, ref } from 'vue'
|
||||||
|
import { enterprise } from '../services/enterprise'
|
||||||
|
const enabled = ref(false), remaining = ref(0), password = ref(''), code = ref(''), secret = ref(''), recovery = ref<string[]>([]), busy = ref(false), notice = ref('')
|
||||||
|
async function load() { const data = await enterprise<{ enabled: boolean; recoveryRemaining: number }>('/api/auth/mfa'); enabled.value = data.enabled; remaining.value = data.recoveryRemaining }
|
||||||
|
onMounted(() => load().catch(e => { notice.value = e.message }))
|
||||||
|
async function action(kind: 'setup' | 'confirm' | 'disable') {
|
||||||
|
busy.value = true; notice.value = ''
|
||||||
|
try {
|
||||||
|
const data = await enterprise<{ secret?: string; recoveryCodes?: string[] }>(`/api/auth/mfa/${kind}`, { password: password.value, code: code.value }, 'POST')
|
||||||
|
if (kind === 'setup') secret.value = data.secret || ''
|
||||||
|
else { secret.value = ''; password.value = ''; code.value = ''; recovery.value = data.recoveryCodes || []; await load(); notice.value = kind === 'confirm' ? '双因素认证已启用,请保存一次性恢复码。' : '双因素认证已关闭' }
|
||||||
|
} catch (e) { notice.value = e instanceof Error ? e.message : '操作失败' } finally { busy.value = false }
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
<template>
|
||||||
|
<section class="mfa-settings">
|
||||||
|
<h3>双因素认证 <small>{{ enabled ? '已启用' : '未启用' }}</small></h3>
|
||||||
|
<p>使用认证器生成验证码。{{ enabled ? `剩余 ${remaining} 个恢复码。` : '启用后,登录需要密码和动态验证码。' }}</p>
|
||||||
|
<label class="form-label">当前密码<input v-model="password" type="password" autocomplete="current-password" class="text-input" /></label>
|
||||||
|
<template v-if="secret"><p>在认证器中添加账号,选择「输入设置密钥」,使用以下密钥(10 分钟内有效):</p><code class="mfa-secret">{{ secret }}</code></template>
|
||||||
|
<label v-if="enabled || secret" class="form-label">{{ enabled ? '验证码或恢复码' : '认证器验证码' }}<input v-model="code" class="text-input" autocomplete="one-time-code" maxlength="24" /></label>
|
||||||
|
<button class="button secondary" :disabled="busy || !password" @click="action(enabled ? 'disable' : secret ? 'confirm' : 'setup')">{{ busy ? '处理中…' : enabled ? '关闭双因素认证' : secret ? '验证并启用' : '开始设置' }}</button>
|
||||||
|
<p role="status">{{ notice }}</p>
|
||||||
|
<template v-if="recovery.length"><p>恢复码只展示这一次,每个只能使用一次,请保存到安全位置:</p><pre class="mfa-secret">{{ recovery.join('\n') }}</pre><button class="button secondary" @click="recovery = []">我已保存</button></template>
|
||||||
|
</section>
|
||||||
|
</template>
|
||||||
|
<style scoped>.mfa-settings{border-top:1px solid #e5e9e8;margin-top:24px;padding-top:16px}.mfa-settings .form-label{flex-direction:column;gap:8px}.mfa-settings p,.mfa-settings small{font-size:13px;color:#64736d}.mfa-secret{display:block;padding:14px;background:#f3f6f4;overflow-wrap:anywhere;user-select:all}.mfa-settings button{margin-top:12px}</style>
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
<script setup lang="ts">
|
||||||
|
import { ref } from 'vue'
|
||||||
|
const selected = ref('embeddings')
|
||||||
|
const examples: Record<string, { path: string; body: string; note: string }> = {
|
||||||
|
embeddings: { path: '/v1/embeddings', body: '{"model":"从 /v1/models 选择向量模型 ID","input":"需要向量化的文字"}', note: '按输入 Token 计费,支持批量文本,无输出 Token 费用。在线调试也支持向量模型。' },
|
||||||
|
messages: { path: '/v1/messages', body: '{"model":"已启用 Messages 的模型 ID","messages":[{"role":"user","content":"你好"}],"max_tokens":4096,"stream":true}', note: '支持文本、工具调用与原生 SSE 事件。可使用 Authorization: Bearer 或 x-api-key 传入 MOYI Key。' },
|
||||||
|
responses: { path: '/v1/responses', body: '{"model":"已启用 Responses 的模型 ID","input":"你好","max_output_tokens":4096,"stream":true}', note: '支持完整上下文的文本与函数调用。当前不保存云端会话;请携带完整上下文,不使用 previous_response_id、conversation 或后台任务。' },
|
||||||
|
gemini: { path: '/v1beta/models/模型ID:generateContent', body: '{"contents":[{"role":"user","parts":[{"text":"你好"}]}],"generationConfig":{"maxOutputTokens":4096}}', note: '流式调用使用 :streamGenerateContent?alt=sse。可使用 Authorization: Bearer 或 x-goog-api-key,Key 放在请求头中。' },
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
<template><section class="panel native-guide"><h2>标准与原生协议接入</h2><p>所有金额均为人民币,1M Token = 1,000,000 Token。所有接口共用用户、Key 预算和调用记录。</p><div class="protocol-tabs"><button v-for="(_, name) in examples" :key="name" class="button" :class="selected === name ? 'primary' : 'secondary'" @click="selected = String(name)">{{ name }}</button></div><h3>{{ examples[selected]?.path }}</h3><pre>{{ examples[selected]?.body }}</pre><p>{{ examples[selected]?.note }}</p><p>Messages、Responses、Gemini 需要管理员确认渠道支持后启用。原生入口当前支持文本与本地工具调用;图片、音频、托管工具和外部缓存需要对应的计费适配器。</p><p>可选请求头:<code>Idempotency-Key</code> 防止重复受理;<code>X-Moyi-Session</code> 将同一会话绑定到健康的渠道账号。流式输出开始后不会自动重新生成。</p></section></template>
|
||||||
|
<style scoped>.native-guide{padding:24px;margin-bottom:22px}.native-guide p{font-size:14px;color:#64736d;line-height:1.8}.protocol-tabs{display:flex;gap:10px;flex-wrap:wrap}.native-guide pre{padding:18px;background:#f3f6f4;white-space:pre-wrap;overflow-wrap:anywhere}.native-guide code{overflow-wrap:anywhere}</style>
|
||||||
@@ -30,6 +30,7 @@ export const userNavigation: NavGroup[] = [
|
|||||||
label: '开发者',
|
label: '开发者',
|
||||||
items: [
|
items: [
|
||||||
{ id: 'keys', label: 'API Keys', icon: KeyRound },
|
{ id: 'keys', label: 'API Keys', icon: KeyRound },
|
||||||
|
{ id: 'key-policies', label: 'Key 安全与预算', icon: ShieldCheck },
|
||||||
{ id: 'tasks', label: '任务中心', icon: FileClock },
|
{ id: 'tasks', label: '任务中心', icon: FileClock },
|
||||||
{ id: 'usage', label: '用量与额度', icon: BarChart3 },
|
{ id: 'usage', label: '用量与额度', icon: BarChart3 },
|
||||||
{ id: 'docs', label: '接入文档', icon: FileText },
|
{ id: 'docs', label: '接入文档', icon: FileText },
|
||||||
@@ -46,10 +47,13 @@ export const adminNavigation: NavGroup[] = [
|
|||||||
label: '资源运营',
|
label: '资源运营',
|
||||||
items: [
|
items: [
|
||||||
{ id: 'admin-users', label: '用户与额度', icon: Users },
|
{ id: 'admin-users', label: '用户与额度', icon: Users },
|
||||||
|
{ id: 'admin-policies', label: '企业调用策略', icon: ShieldCheck },
|
||||||
{ id: 'admin-models', label: '模型与分组', icon: Boxes },
|
{ id: 'admin-models', label: '模型与分组', icon: Boxes },
|
||||||
{ id: 'admin-routing', label: '渠道管理', icon: Network },
|
{ id: 'admin-routing', label: '渠道管理', icon: Network },
|
||||||
{ id: 'admin-capacity', label: '并发与队列', icon: Gauge },
|
{ id: 'admin-capacity', label: '并发与队列', icon: Gauge },
|
||||||
{ id: 'admin-billing', label: '计费配置', icon: CircleDollarSign },
|
{ id: 'admin-billing', label: '计费配置', icon: CircleDollarSign },
|
||||||
|
{ id: 'admin-finance', label: '价格版本与对账', icon: CircleDollarSign },
|
||||||
|
{ id: 'admin-operations', label: '渠道健康与告警', icon: Gauge },
|
||||||
{ id: 'admin-usage', label: '模型调用记录', icon: ScrollText },
|
{ id: 'admin-usage', label: '模型调用记录', icon: ScrollText },
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -22,6 +22,7 @@ export interface RegisterInput {
|
|||||||
export interface LoginInput {
|
export interface LoginInput {
|
||||||
account: string
|
account: string
|
||||||
password: string
|
password: string
|
||||||
|
otp?: string
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface AuthClient {
|
export interface AuthClient {
|
||||||
|
|||||||
@@ -0,0 +1,7 @@
|
|||||||
|
export async function enterprise<T = Record<string, unknown>>(path: string, body?: unknown, method = 'PUT'): Promise<T> {
|
||||||
|
const base = (import.meta.env.VITE_CONTROL_PLANE_URL || '').replace(/\/$/, '')
|
||||||
|
const response = await fetch(`${base}${path}`, { credentials: 'include', method: body === undefined ? 'GET' : method, headers: { 'Content-Type': 'application/json', 'X-Moyi-Source': path.startsWith('/api/playground/') ? 'playground' : 'console' }, body: body === undefined ? undefined : JSON.stringify(body) })
|
||||||
|
const data = await response.json().catch(() => ({}))
|
||||||
|
if (!response.ok) throw new Error(data.error?.message || '操作失败,请稍后重试')
|
||||||
|
return data as T
|
||||||
|
}
|
||||||
@@ -39,6 +39,9 @@ import SlidePanel from '../components/SlidePanel.vue'
|
|||||||
import UiModal from '../components/UiModal.vue'
|
import UiModal from '../components/UiModal.vue'
|
||||||
import CatalogAdmin from '../components/CatalogAdmin.vue'
|
import CatalogAdmin from '../components/CatalogAdmin.vue'
|
||||||
import AccountAdmin from '../components/AccountAdmin.vue'
|
import AccountAdmin from '../components/AccountAdmin.vue'
|
||||||
|
import EnterprisePolicies from '../components/EnterprisePolicies.vue'
|
||||||
|
import EnterpriseFinance from '../components/EnterpriseFinance.vue'
|
||||||
|
import EnterpriseOperations from '../components/EnterpriseOperations.vue'
|
||||||
import { providerCapabilityLabel, summarizeModelCatalog } from '../services/model-catalog'
|
import { providerCapabilityLabel, summarizeModelCatalog } from '../services/model-catalog'
|
||||||
import { usePagination } from '../composables/usePagination'
|
import { usePagination } from '../composables/usePagination'
|
||||||
import ListPagination from '../components/ListPagination.vue'
|
import ListPagination from '../components/ListPagination.vue'
|
||||||
@@ -282,7 +285,10 @@ function mapModels(items: CatalogModel[], providersList: CatalogProvider[]): Mod
|
|||||||
|
|
||||||
<template>
|
<template>
|
||||||
<div class="page-stack admin-pages">
|
<div class="page-stack admin-pages">
|
||||||
<template v-if="view === 'admin-overview'">
|
<EnterprisePolicies v-if="view === 'admin-policies'" admin />
|
||||||
|
<EnterpriseFinance v-else-if="view === 'admin-finance'" />
|
||||||
|
<EnterpriseOperations v-else-if="view === 'admin-operations'" />
|
||||||
|
<template v-else-if="view === 'admin-overview'">
|
||||||
<section class="page-heading compact"><div><span>ADMIN OVERVIEW</span><h1>管理总览</h1><p>掌握平台供给、用户、容量和风险状态。</p></div><div class="heading-actions"><span class="health-badge"><i></i>目录数据已读取</span><button class="button secondary" :disabled="overviewRefreshing" @click="refreshOverviewData(true)"><Activity :size="16" />{{ overviewRefreshing ? '刷新中…' : '刷新数据' }}</button></div></section>
|
<section class="page-heading compact"><div><span>ADMIN OVERVIEW</span><h1>管理总览</h1><p>掌握平台供给、用户、容量和风险状态。</p></div><div class="heading-actions"><span class="health-badge"><i></i>目录数据已读取</span><button class="button secondary" :disabled="overviewRefreshing" @click="refreshOverviewData(true)"><Activity :size="16" />{{ overviewRefreshing ? '刷新中…' : '刷新数据' }}</button></div></section>
|
||||||
<section class="metric-grid"><article class="metric-card"><span class="icon-tile violet"><Users :size="18" /></span><div><small>用户总数</small><strong>{{ users.length }}</strong><em>来自认证服务</em></div></article><article class="metric-card"><span class="icon-tile cyan"><Cloud :size="18" /></span><div><small>已完成接入配置的模型</small><strong>{{ publicModelCount }}</strong><em>模型广场显示 {{ publishedModelCount }} 个 · {{ publicRouteCount }} 个就绪分组 · 待定价 {{ unpricedRouteCount }} 个</em></div></article><article class="metric-card"><span class="icon-tile orange"><Gauge :size="18" /></span><div><small>等待 / 执行任务</small><strong>{{ adminSummary?.waiting ?? '—' }} / {{ adminSummary?.running ?? '—' }}</strong><em>Redis 租约与异步任务实时状态</em></div></article><article class="metric-card"><span class="icon-tile green"><CircleDollarSign :size="18" /></span><div><small>今日额度消耗</small><strong>{{ adminSummary ? `¥${Number(adminSummary.costToday || 0).toFixed(4)}` : '—' }}</strong><em>成功调用实际扣费</em></div></article></section>
|
<section class="metric-grid"><article class="metric-card"><span class="icon-tile violet"><Users :size="18" /></span><div><small>用户总数</small><strong>{{ users.length }}</strong><em>来自认证服务</em></div></article><article class="metric-card"><span class="icon-tile cyan"><Cloud :size="18" /></span><div><small>已完成接入配置的模型</small><strong>{{ publicModelCount }}</strong><em>模型广场显示 {{ publishedModelCount }} 个 · {{ publicRouteCount }} 个就绪分组 · 待定价 {{ unpricedRouteCount }} 个</em></div></article><article class="metric-card"><span class="icon-tile orange"><Gauge :size="18" /></span><div><small>等待 / 执行任务</small><strong>{{ adminSummary?.waiting ?? '—' }} / {{ adminSummary?.running ?? '—' }}</strong><em>Redis 租约与异步任务实时状态</em></div></article><article class="metric-card"><span class="icon-tile green"><CircleDollarSign :size="18" /></span><div><small>今日额度消耗</small><strong>{{ adminSummary ? `¥${Number(adminSummary.costToday || 0).toFixed(4)}` : '—' }}</strong><em>成功调用实际扣费</em></div></article></section>
|
||||||
<section class="two-column equal"><article class="panel chart-panel"><header class="panel-header"><div><span>PLATFORM TRAFFIC</span><h3>平台请求趋势</h3></div><span class="select-button static">最近 7 天</span></header><div v-if="adminSummary" class="traffic-list"><div v-for="point in adminSummary.series" :key="point.date"><span>{{ point.date }}</span><strong>{{ point.count }} 次</strong></div></div><div v-else class="empty-state compact"><BarChart3 :size="22" /><strong>正在加载平台流量</strong><span>刷新后显示最近 7 天调用量</span></div></article><article class="panel health-panel"><header class="panel-header"><div><span>GATEWAY STATUS</span><h3>模型供给状态</h3></div><button class="text-link" @click="emit('navigate', 'admin-routing')">管理渠道<ArrowRight :size="14" /></button></header><div v-if="providers.length" class="health-list"><div v-for="provider in providers.slice(0, 3)" :key="provider.id"><span class="provider-mark">{{ provider.name.slice(0, 1) }}</span><p><strong>{{ provider.name }}</strong><small>{{ provider.capability }} · {{ provider.latency }}</small></p><b>{{ provider.successRate }}</b><span class="status-chip" :class="provider.status === '已启用' ? 'success' : 'warning'"><i></i>{{ provider.status }}</span></div></div><div v-else class="empty-state compact"><Cloud :size="22" /><strong>暂无供应商</strong><span>由管理员登记上游渠道后显示</span></div></article></section>
|
<section class="two-column equal"><article class="panel chart-panel"><header class="panel-header"><div><span>PLATFORM TRAFFIC</span><h3>平台请求趋势</h3></div><span class="select-button static">最近 7 天</span></header><div v-if="adminSummary" class="traffic-list"><div v-for="point in adminSummary.series" :key="point.date"><span>{{ point.date }}</span><strong>{{ point.count }} 次</strong></div></div><div v-else class="empty-state compact"><BarChart3 :size="22" /><strong>正在加载平台流量</strong><span>刷新后显示最近 7 天调用量</span></div></article><article class="panel health-panel"><header class="panel-header"><div><span>GATEWAY STATUS</span><h3>模型供给状态</h3></div><button class="text-link" @click="emit('navigate', 'admin-routing')">管理渠道<ArrowRight :size="14" /></button></header><div v-if="providers.length" class="health-list"><div v-for="provider in providers.slice(0, 3)" :key="provider.id"><span class="provider-mark">{{ provider.name.slice(0, 1) }}</span><p><strong>{{ provider.name }}</strong><small>{{ provider.capability }} · {{ provider.latency }}</small></p><b>{{ provider.successRate }}</b><span class="status-chip" :class="provider.status === '已启用' ? 'success' : 'warning'"><i></i>{{ provider.status }}</span></div></div><div v-else class="empty-state compact"><Cloud :size="22" /><strong>暂无供应商</strong><span>由管理员登记上游渠道后显示</span></div></article></section>
|
||||||
|
|||||||
@@ -28,6 +28,8 @@ const mode = ref<AuthMode>('login')
|
|||||||
const registerChannel = ref<RegisterChannel>('email')
|
const registerChannel = ref<RegisterChannel>('email')
|
||||||
const account = ref('')
|
const account = ref('')
|
||||||
const password = ref('')
|
const password = ref('')
|
||||||
|
const otp = ref('')
|
||||||
|
const mfaRequired = ref(false)
|
||||||
const confirmPassword = ref('')
|
const confirmPassword = ref('')
|
||||||
const invitationCode = ref('')
|
const invitationCode = ref('')
|
||||||
const verificationCode = ref('')
|
const verificationCode = ref('')
|
||||||
@@ -89,10 +91,11 @@ async function submitLogin() {
|
|||||||
if (!account.value.trim() || !password.value) return emit('notify', '请输入账号和密码')
|
if (!account.value.trim() || !password.value) return emit('notify', '请输入账号和密码')
|
||||||
loading.value = true
|
loading.value = true
|
||||||
try {
|
try {
|
||||||
const user = await authClient.login({ account: account.value, password: password.value })
|
const user = await authClient.login({ account: account.value, password: password.value, otp: otp.value || undefined })
|
||||||
loading.value = false
|
loading.value = false
|
||||||
emit('success', user)
|
emit('success', user)
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
if (error instanceof AuthApiError && error.code === 'MFA_REQUIRED') mfaRequired.value = true
|
||||||
loading.value = false
|
loading.value = false
|
||||||
emit('notify', error instanceof AuthApiError ? error.message : '登录失败,请稍后重试')
|
emit('notify', error instanceof AuthApiError ? error.message : '登录失败,请稍后重试')
|
||||||
}
|
}
|
||||||
@@ -200,6 +203,7 @@ onMounted(async () => {
|
|||||||
<form v-if="mode === 'login'" class="auth-form" @submit.prevent="submitLogin">
|
<form v-if="mode === 'login'" class="auth-form" @submit.prevent="submitLogin">
|
||||||
<label><span>账号</span><div class="auth-input"><Mail :size="17" /><input v-model="account" autocomplete="username" placeholder="请输入用户名、邮箱或手机号" /></div></label>
|
<label><span>账号</span><div class="auth-input"><Mail :size="17" /><input v-model="account" autocomplete="username" placeholder="请输入用户名、邮箱或手机号" /></div></label>
|
||||||
<label><span>登录密码</span><div class="auth-input"><LockKeyhole :size="17" /><input v-model="password" :type="showPassword ? 'text' : 'password'" autocomplete="current-password" placeholder="请输入密码" /><button type="button" aria-label="显示或隐藏密码" @click="showPassword = !showPassword"><EyeOff v-if="showPassword" :size="17" /><Eye v-else :size="17" /></button></div></label>
|
<label><span>登录密码</span><div class="auth-input"><LockKeyhole :size="17" /><input v-model="password" :type="showPassword ? 'text' : 'password'" autocomplete="current-password" placeholder="请输入密码" /><button type="button" aria-label="显示或隐藏密码" @click="showPassword = !showPassword"><EyeOff v-if="showPassword" :size="17" /><Eye v-else :size="17" /></button></div></label>
|
||||||
|
<label v-if="mfaRequired"><span>认证器验证码或一次性恢复码</span><div class="auth-input"><ShieldCheck :size="17" /><input v-model="otp" autocomplete="one-time-code" maxlength="24" placeholder="输入验证码或恢复码" /></div></label>
|
||||||
<div class="auth-form-options login-help"><span>账号密码登录</span><button type="button" @click="switchMode('forgot')">忘记密码?</button></div>
|
<div class="auth-form-options login-help"><span>账号密码登录</span><button type="button" @click="switchMode('forgot')">忘记密码?</button></div>
|
||||||
<button class="auth-submit" type="submit" :disabled="loading"><span v-if="loading" class="spinner"></span><template v-else>进入工作台<ArrowRight :size="17" /></template></button>
|
<button class="auth-submit" type="submit" :disabled="loading"><span v-if="loading" class="spinner"></span><template v-else>进入工作台<ArrowRight :size="17" /></template></button>
|
||||||
</form>
|
</form>
|
||||||
|
|||||||
@@ -42,6 +42,9 @@ import { buildModelCatalog } from '../services/model-catalog'
|
|||||||
import { groupDiscountLabel as formatGroupDiscountLabel, modelStandardPriceLabel, platformPriceLabel } from '../services/pricing'
|
import { groupDiscountLabel as formatGroupDiscountLabel, modelStandardPriceLabel, platformPriceLabel } from '../services/pricing'
|
||||||
import { usePagination } from '../composables/usePagination'
|
import { usePagination } from '../composables/usePagination'
|
||||||
import ListPagination from '../components/ListPagination.vue'
|
import ListPagination from '../components/ListPagination.vue'
|
||||||
|
import EnterprisePolicies from '../components/EnterprisePolicies.vue'
|
||||||
|
import NativeApiGuide from '../components/NativeApiGuide.vue'
|
||||||
|
import { enterprise } from '../services/enterprise'
|
||||||
import { copyText, type CopyResult } from '../services/clipboard'
|
import { copyText, type CopyResult } from '../services/clipboard'
|
||||||
import SlidePanel from '../components/SlidePanel.vue'
|
import SlidePanel from '../components/SlidePanel.vue'
|
||||||
import UiModal from '../components/UiModal.vue'
|
import UiModal from '../components/UiModal.vue'
|
||||||
@@ -152,6 +155,7 @@ const activeMediaKind = computed<AsyncMediaType | undefined>(() => {
|
|||||||
return undefined
|
return undefined
|
||||||
})
|
})
|
||||||
const activeInputTypes = computed(() => activeModel.value.inputTypes || [])
|
const activeInputTypes = computed(() => activeModel.value.inputTypes || [])
|
||||||
|
const activeEmbedding = computed(() => catalogRoutes.value.find(m => m.id === activeModel.value.id)?.type === 'embedding')
|
||||||
const activeOutputTypes = computed(() => activeModel.value.outputTypes || [])
|
const activeOutputTypes = computed(() => activeModel.value.outputTypes || [])
|
||||||
const activeAsyncSupported = computed(() => activeModel.value.mode !== '异步' || (!!activeMediaKind.value && activeModel.value.executionReady !== false))
|
const activeAsyncSupported = computed(() => activeModel.value.mode !== '异步' || (!!activeMediaKind.value && activeModel.value.executionReady !== false))
|
||||||
const supportsMediaInput = computed(() => activeInputTypes.value.some(input => /image|audio|video|url|media/i.test(input)))
|
const supportsMediaInput = computed(() => activeInputTypes.value.some(input => /image|audio|video|url|media/i.test(input)))
|
||||||
@@ -427,7 +431,10 @@ async function runModel() {
|
|||||||
finishReason.value = ''
|
finishReason.value = ''
|
||||||
const selectedMode = callMode.value
|
const selectedMode = callMode.value
|
||||||
try {
|
try {
|
||||||
if (callMode.value === '异步') {
|
if (activeEmbedding.value) {
|
||||||
|
const result = await enterprise(`/api/playground/keys/${encodeURIComponent(apiKey)}/embeddings`, { model: gatewayModelId(activeModel.value), input: prompt.value }, 'POST')
|
||||||
|
response.value = JSON.stringify(result, null, 2)
|
||||||
|
} else if (callMode.value === '异步') {
|
||||||
const task = await catalogClient.playgroundCreateMediaTask(apiKey, { model: gatewayModelId(activeModel.value), prompt: prompt.value, mediaUrl: mediaUrl.value.trim() || undefined, mediaType: activeMediaKind.value! })
|
const task = await catalogClient.playgroundCreateMediaTask(apiKey, { model: gatewayModelId(activeModel.value), prompt: prompt.value, mediaUrl: mediaUrl.value.trim() || undefined, mediaType: activeMediaKind.value! })
|
||||||
const taskItem = mapAsyncTask(task, activeModel.value, prompt.value)
|
const taskItem = mapAsyncTask(task, activeModel.value, prompt.value)
|
||||||
tasks.value.unshift(taskItem)
|
tasks.value.unshift(taskItem)
|
||||||
@@ -768,7 +775,9 @@ async function createProject() {
|
|||||||
</script>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
<div class="page-stack">
|
<EnterprisePolicies v-if="view === 'key-policies'" />
|
||||||
|
<NativeApiGuide v-if="view === 'docs'" />
|
||||||
|
<div v-if="view !== 'key-policies'" class="page-stack">
|
||||||
<template v-if="view === 'overview'">
|
<template v-if="view === 'overview'">
|
||||||
<section class="page-heading compact"><div><span>WORKSPACE OVERVIEW</span><h1>你好,{{ displayName || 'MOYI 用户' }}</h1><p>从统一入口开始使用每一种模型能力。</p></div><div class="heading-actions"><span class="health-badge" :class="{ offline: !catalogConnected }"><i></i>{{ catalogConnected ? 'Control Plane 已连接' : 'Control Plane 未连接' }}</span><button class="button secondary" @click="emit('navigate', 'docs')"><BookOpen :size="16" />快速开始</button></div></section>
|
<section class="page-heading compact"><div><span>WORKSPACE OVERVIEW</span><h1>你好,{{ displayName || 'MOYI 用户' }}</h1><p>从统一入口开始使用每一种模型能力。</p></div><div class="heading-actions"><span class="health-badge" :class="{ offline: !catalogConnected }"><i></i>{{ catalogConnected ? 'Control Plane 已连接' : 'Control Plane 未连接' }}</span><button class="button secondary" @click="emit('navigate', 'docs')"><BookOpen :size="16" />快速开始</button></div></section>
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,68 @@
|
|||||||
|
"""Acceptance check on unique expiring Redis keys; no production keys are read or changed."""
|
||||||
|
import json
|
||||||
|
from pathlib import Path
|
||||||
|
import socket
|
||||||
|
import ssl
|
||||||
|
import subprocess
|
||||||
|
import uuid
|
||||||
|
|
||||||
|
container = 'moyi-test-moyi-api-1'
|
||||||
|
config = json.loads(subprocess.check_output(['docker', 'inspect', container]))[0]
|
||||||
|
env = dict(item.split('=', 1) for item in config['Config']['Env'] if '=' in item)
|
||||||
|
sock = socket.create_connection((env['MOYI_REDIS_HOST'], int(env.get('MOYI_REDIS_PORT', 6379))), 10)
|
||||||
|
if env.get('MOYI_REDIS_SSL', '').lower() == 'true':
|
||||||
|
sock = ssl.create_default_context().wrap_socket(sock, server_hostname=env['MOYI_REDIS_HOST'])
|
||||||
|
reader = sock.makefile('rb')
|
||||||
|
|
||||||
|
def response():
|
||||||
|
line = reader.readline()
|
||||||
|
if not line:
|
||||||
|
raise RuntimeError('Redis closed connection')
|
||||||
|
kind, text = line[:1], line[1:-2]
|
||||||
|
if kind == b'-':
|
||||||
|
raise RuntimeError('Redis rejected acceptance command')
|
||||||
|
if kind == b'+':
|
||||||
|
return text.decode()
|
||||||
|
if kind == b':':
|
||||||
|
return int(text)
|
||||||
|
if kind == b'$':
|
||||||
|
size = int(text)
|
||||||
|
if size < 0:
|
||||||
|
return None
|
||||||
|
value = reader.read(size)
|
||||||
|
reader.read(2)
|
||||||
|
return value
|
||||||
|
if kind == b'*':
|
||||||
|
return [response() for _ in range(int(text))]
|
||||||
|
raise RuntimeError('Unexpected Redis response')
|
||||||
|
|
||||||
|
def command(*parts):
|
||||||
|
values = [str(p).encode() for p in parts]
|
||||||
|
sock.sendall(b'*' + str(len(values)).encode() + b'\r\n' + b''.join(b'$' + str(len(v)).encode() + b'\r\n' + v + b'\r\n' for v in values))
|
||||||
|
return response()
|
||||||
|
|
||||||
|
if env.get('MOYI_REDIS_PASSWORD'):
|
||||||
|
args = ['AUTH']
|
||||||
|
if env.get('MOYI_REDIS_USERNAME'):
|
||||||
|
args.append(env['MOYI_REDIS_USERNAME'])
|
||||||
|
command(*args, env['MOYI_REDIS_PASSWORD'])
|
||||||
|
command('SELECT', int(env.get('MOYI_REDIS_DATABASE', 0)))
|
||||||
|
root = Path(__file__).resolve().parent
|
||||||
|
script = (root / 'acquire-slot.lua').read_text()
|
||||||
|
rate = (root / 'rate-limit.lua').read_text()
|
||||||
|
prefix = 'moyi:acceptance:' + uuid.uuid4().hex + ':'
|
||||||
|
keys = [prefix + 'user', prefix + 'rate']
|
||||||
|
try:
|
||||||
|
assert command('EVAL', script, 1, keys[0], 2, 60000, 'long-task') == 1
|
||||||
|
assert command('EVAL', script, 1, keys[0], 2, 1000, 'short-chat') == 1
|
||||||
|
assert command('PTTL', keys[0]) > 50000, 'Short chat shortened async lease'
|
||||||
|
assert command('EVAL', script, 1, keys[0], 2, 1000, 'third') == 0
|
||||||
|
assert command('ZREM', keys[0], 'short-chat') == 1
|
||||||
|
assert command('EVAL', script, 1, keys[0], 2, 1000, 'third') == 1
|
||||||
|
assert command('EVAL', rate, 1, keys[1], 1, 60000) == 0
|
||||||
|
assert command('EVAL', rate, 1, keys[1], 1, 60000) > 0
|
||||||
|
print('PASS: shared user concurrency, async lease lifetime, release and RPM on isolated keys')
|
||||||
|
finally:
|
||||||
|
command('DEL', *keys)
|
||||||
|
reader.close()
|
||||||
|
sock.close()
|
||||||
@@ -30,7 +30,7 @@ New-Item -ItemType Directory -Force -Path $artifactRoot | Out-Null
|
|||||||
$staging = Join-Path $artifactRoot ('moyi-test-' + $releaseId)
|
$staging = Join-Path $artifactRoot ('moyi-test-' + $releaseId)
|
||||||
New-Item -ItemType Directory -Path $staging | Out-Null
|
New-Item -ItemType Directory -Path $staging | Out-Null
|
||||||
# Copy tracked deployment files only, never .env, certificates or .git.
|
# Copy tracked deployment files only, never .env, certificates or .git.
|
||||||
$tracked = @(git ls-files infra/moyi scripts/live-gateway-smoke.py docs/11-server-test-deployment.md docs/13-model-groups-registration-credit.md docs/14-lifecycle-usage-credit-adjustments.md docs/15-relay-onboarding.md docs/16-streaming-chat-billing.md docs/17-catalog-and-key-selection.md docs/18-pricing-currency-and-discounts.md)
|
$tracked = @(git ls-files infra/moyi scripts/live-gateway-smoke.py docs/11-server-test-deployment.md docs/13-model-groups-registration-credit.md docs/14-lifecycle-usage-credit-adjustments.md docs/15-relay-onboarding.md docs/16-streaming-chat-billing.md docs/17-catalog-and-key-selection.md docs/18-pricing-currency-and-discounts.md docs/19-enterprise-gateway-increment.md)
|
||||||
foreach ($relative in $tracked) {
|
foreach ($relative in $tracked) {
|
||||||
if ($relative -match '(^|/)(\.env\.cloud|\.deploy-mode|certs)(/|$)') { throw 'Sensitive path in release allowlist.' }
|
if ($relative -match '(^|/)(\.env\.cloud|\.deploy-mode|certs)(/|$)') { throw 'Sensitive path in release allowlist.' }
|
||||||
$destination = Join-Path $staging $relative
|
$destination = Join-Path $staging $relative
|
||||||
|
|||||||
@@ -0,0 +1,34 @@
|
|||||||
|
"""Run all Java tests against a fresh, local-only PostgreSQL cluster; never production."""
|
||||||
|
import os
|
||||||
|
from pathlib import Path
|
||||||
|
import socket
|
||||||
|
import subprocess
|
||||||
|
import tempfile
|
||||||
|
|
||||||
|
repo = Path(__file__).resolve().parent.parent
|
||||||
|
pg = Path(r'C:\Program Files\PostgreSQL\17\bin')
|
||||||
|
folder = Path(tempfile.mkdtemp(prefix='enterprise-test-', dir=repo / '.tools'))
|
||||||
|
with socket.socket() as sock:
|
||||||
|
sock.bind(('127.0.0.1', 0))
|
||||||
|
port = sock.getsockname()[1]
|
||||||
|
env = os.environ.copy()
|
||||||
|
env['JAVA_HOME'] = str(repo / '.tools/jdk-build-22')
|
||||||
|
env['MOYI_AUTH_TEST_JDBC_URL'] = f'jdbc:postgresql://127.0.0.1:{port}/postgres'
|
||||||
|
|
||||||
|
def run(command, name, timeout=120):
|
||||||
|
with (folder / name).open('wb') as output:
|
||||||
|
result = subprocess.run([str(x) for x in command], cwd=repo, env=env, stdout=output,
|
||||||
|
stderr=subprocess.STDOUT, timeout=timeout,
|
||||||
|
creationflags=getattr(subprocess, 'CREATE_NO_WINDOW', 0))
|
||||||
|
if result.returncode:
|
||||||
|
print((folder / name).read_text(encoding='utf-8', errors='replace')[-14000:])
|
||||||
|
raise RuntimeError(f'{name} failed ({result.returncode})')
|
||||||
|
|
||||||
|
run([pg / 'initdb.exe', '-D', folder / 'data', '-U', 'moyi_role_test', '-A', 'trust', '--encoding=UTF8', '--locale=C'], 'init.log')
|
||||||
|
try:
|
||||||
|
run([pg / 'pg_ctl.exe', '-D', folder / 'data', '-l', folder / 'postgres.log', '-o', f'-p {port} -h 127.0.0.1', '-w', 'start'], 'start.log')
|
||||||
|
print(f'Isolated PostgreSQL ready on 127.0.0.1:{port}; logs: {folder}', flush=True)
|
||||||
|
run([repo / '.tools/apache-maven-3.9.9/bin/mvn.cmd', '-B', '-ntp', '-Dmaven.repo.local=.tools/m2', '-f', 'control-plane-java/pom.xml', 'test'], 'tests.log', 600)
|
||||||
|
print((folder / 'tests.log').read_text(encoding='utf-8', errors='replace')[-3500:])
|
||||||
|
finally:
|
||||||
|
run([pg / 'pg_ctl.exe', '-D', folder / 'data', '-w', 'stop', '-m', 'fast'], 'stop.log')
|
||||||
Reference in new issue
Block a user