Compare commits
14
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4c4f5ad68d | ||
|
|
af43947b5d | ||
|
|
1528b71882 | ||
|
|
512ca52262 | ||
|
|
ea9069e332 | ||
|
|
d8e05beb13 | ||
|
|
8336d9ba3b | ||
|
|
3969a50eee | ||
|
|
558c0dcdd5 | ||
|
|
58dc904c98 | ||
|
|
0876dfe08e | ||
|
|
ce0a718686 | ||
|
|
25114ca6b8 | ||
|
|
9b98de3efa |
No files matched your search
@@ -262,7 +262,7 @@ MaaS 模型服务与应用开发
|
||||
│ └── 面向创作者的 AI 生成 3D 产品
|
||||
├── 模亦灵枢 / MOYI Model Intelligence Hub
|
||||
│ └── 面向开发者和企业的多模型智能基础设施
|
||||
└── 模翎技术
|
||||
└── 模灵科技
|
||||
└── 平台底层技术、工程能力和模型研发体系
|
||||
```
|
||||
|
||||
|
||||
@@ -43,7 +43,9 @@ public class AuthController {
|
||||
this.secureCookie = secureCookie;
|
||||
}
|
||||
|
||||
public record LoginRequest(String account, String password) {}
|
||||
public record LoginRequest(String account, String password,String otp) {
|
||||
public LoginRequest(String account,String password) {this(account,password,null);}
|
||||
}
|
||||
|
||||
public record RegisterRequest(String invitationCode, String account, String password, String displayName, String verificationCode) {}
|
||||
public record CodeRequest(String account, String invitationCode) {}
|
||||
@@ -87,7 +89,7 @@ public class AuthController {
|
||||
HttpServletRequest request,
|
||||
HttpServletResponse response
|
||||
) {
|
||||
AuthService.AuthResult result = authService.login(input.account(), input.password(), meta(request));
|
||||
AuthService.AuthResult result = authService.login(input.account(), input.password(), input.otp(), meta(request));
|
||||
// Keep the browser session cookie ephemeral. The server-side session still
|
||||
// has its normal TTL, but a closed browser must require a fresh login.
|
||||
response.addHeader(HttpHeaders.SET_COOKIE, sessionCookie(result.token(), null).toString());
|
||||
|
||||
@@ -34,6 +34,7 @@ public class AuthService {
|
||||
private final ObjectMapper objectMapper;
|
||||
private final int sessionTtlHours;
|
||||
private final SecretCipher secretCipher;
|
||||
@Autowired private TotpService totp;
|
||||
|
||||
/** Kept for isolated unit tests that do not exercise encrypted invitation display. */
|
||||
public AuthService(
|
||||
@@ -119,6 +120,9 @@ public class AuthService {
|
||||
}
|
||||
|
||||
public AuthResult login(String accountInput, String password, RequestMeta meta) {
|
||||
return login(accountInput,password,null,meta);
|
||||
}
|
||||
public AuthResult login(String accountInput,String password,String otp,RequestMeta meta) {
|
||||
UserRow user = findUserByAccount(normalizeAccount(accountInput));
|
||||
if (user == null || !passwordHasher.matches(password, user.passwordHash())) {
|
||||
throw new ApiException("INVALID_CREDENTIALS", "账号或密码错误", 401);
|
||||
@@ -126,11 +130,16 @@ public class AuthService {
|
||||
if (!"active".equals(user.status())) {
|
||||
throw new ApiException("ACCOUNT_DISABLED", "账号已停用", 403);
|
||||
}
|
||||
if(totp!=null) totp.verifyLogin(user.id(),otp);
|
||||
OffsetDateTime loginAt = now();
|
||||
jdbc.update("UPDATE users SET last_login_at = ?, updated_at = ? WHERE id = ?", loginAt, loginAt, user.id());
|
||||
audit("user.logged_in", user.id(), meta, Map.of());
|
||||
return new AuthResult(createSession(user.id(), meta), toView(user, loginAt));
|
||||
}
|
||||
public void verifyPassword(UUID userId,String password) {
|
||||
String hash=jdbc.queryForObject("SELECT password_hash FROM users WHERE id=? AND status='active'",String.class,userId);
|
||||
if(password==null||!passwordHasher.matches(password,hash)) throw new ApiException("INVALID_CREDENTIALS","当前密码错误",401);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public AuthResult register(
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
package com.moyi.controlplane.auth;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import java.util.Map;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
@RestController @RequestMapping("/api/auth/mfa")
|
||||
public class TotpController {
|
||||
private final AuthService auth;private final TotpService totp;
|
||||
public TotpController(AuthService auth,TotpService totp) {this.auth=auth;this.totp=totp;}
|
||||
public record Proof(String password,String code) {}
|
||||
@GetMapping public Map<String,Object> status(HttpServletRequest request) {return totp.status(auth.requireUser(AuthController.sessionToken(request)).id());}
|
||||
@PostMapping("/setup") public Map<String,Object> setup(@RequestBody Proof proof,HttpServletRequest request) {
|
||||
var user=auth.requireUser(AuthController.sessionToken(request));auth.verifyPassword(user.id(),proof.password());
|
||||
var result=totp.begin(user.id(),user.account());auth.audit("user.mfa_setup_started",user.id(),AuthController.meta(request),Map.of());return result;
|
||||
}
|
||||
@PostMapping("/confirm") public Map<String,Object> confirm(@RequestBody Proof proof,HttpServletRequest request) {
|
||||
var user=auth.requireUser(AuthController.sessionToken(request));auth.verifyPassword(user.id(),proof.password());
|
||||
var codes=totp.confirm(user.id(),proof.code());auth.audit("user.mfa_enabled",user.id(),AuthController.meta(request),Map.of());return Map.of("recoveryCodes",codes);
|
||||
}
|
||||
@PostMapping("/disable") public Map<String,Object> disable(@RequestBody Proof proof,HttpServletRequest request) {
|
||||
var user=auth.requireUser(AuthController.sessionToken(request));auth.verifyPassword(user.id(),proof.password());totp.disable(user.id(),proof.code());
|
||||
auth.audit("user.mfa_disabled",user.id(),AuthController.meta(request),Map.of());return Map.of("enabled",false);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
package com.moyi.controlplane.auth;
|
||||
|
||||
import com.moyi.controlplane.common.ApiException;
|
||||
import com.moyi.controlplane.security.SecretCipher;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.*;
|
||||
import java.time.*;
|
||||
import java.util.*;
|
||||
import javax.crypto.Mac;
|
||||
import javax.crypto.spec.SecretKeySpec;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
@Service
|
||||
public class TotpService {
|
||||
private final JdbcTemplate jdbc;private final SecretCipher cipher;
|
||||
private static final SecureRandom RANDOM=new SecureRandom();
|
||||
public TotpService(JdbcTemplate jdbc,SecretCipher cipher) {this.jdbc=jdbc;this.cipher=cipher;}
|
||||
public Map<String,Object> status(UUID user) {
|
||||
var rows=jdbc.queryForList("SELECT enabled FROM user_totp WHERE user_id=?",user);
|
||||
return Map.of("enabled",!rows.isEmpty()&&Boolean.TRUE.equals(rows.getFirst().get("enabled")),"recoveryRemaining",jdbc.queryForObject("SELECT count(*) FROM user_totp_recovery WHERE user_id=? AND used_at IS NULL",Integer.class,user));
|
||||
}
|
||||
@Transactional
|
||||
public Map<String,Object> begin(UUID user,String account) {
|
||||
jdbc.queryForList("SELECT id FROM users WHERE id=? FOR UPDATE",user);
|
||||
if(Boolean.TRUE.equals(status(user).get("enabled"))) throw new ApiException("MFA_ALREADY_ENABLED","双因素认证已经启用",409);
|
||||
byte[] bytes=new byte[20];RANDOM.nextBytes(bytes);String secret=base32(bytes);
|
||||
if(jdbc.update("INSERT INTO user_totp(user_id,secret_ciphertext,pending_until) VALUES (?,?,now()+interval '10 minutes') ON CONFLICT(user_id) DO UPDATE SET secret_ciphertext=excluded.secret_ciphertext,pending_until=excluded.pending_until,last_step=-1,updated_at=now() WHERE user_totp.enabled=false",user,cipher.encrypt(secret))!=1) throw new ApiException("MFA_ALREADY_ENABLED","双因素认证已经启用",409);
|
||||
String label=java.net.URLEncoder.encode("MOYI:"+account,StandardCharsets.UTF_8).replace("+","%20");
|
||||
return Map.of("secret",secret,"uri","otpauth://totp/"+label+"?secret="+secret+"&issuer=MOYI&algorithm=SHA1&digits=6&period=30","expiresInSeconds",600);
|
||||
}
|
||||
@Transactional(noRollbackFor=ApiException.class)
|
||||
public List<String> confirm(UUID user,String code) {
|
||||
var row=locked(user);
|
||||
if(row.isEmpty()||Boolean.TRUE.equals(row.get("enabled"))||row.get("pending_until")==null||!((java.sql.Timestamp)row.get("pending_until")).toInstant().isAfter(Instant.now())) throw new ApiException("MFA_SETUP_EXPIRED","设置已过期,请重新生成",400);
|
||||
verifyRow(user,row,code,false);
|
||||
jdbc.update("UPDATE user_totp SET enabled=true,pending_until=NULL,updated_at=now() WHERE user_id=?",user);
|
||||
jdbc.update("DELETE FROM user_totp_recovery WHERE user_id=?",user);
|
||||
var codes=new ArrayList<String>();
|
||||
for(int i=0;i<10;i++) {byte[] random=new byte[12];RANDOM.nextBytes(random);String recovery=HexFormat.of().formatHex(random);codes.add(recovery);jdbc.update("INSERT INTO user_totp_recovery(user_id,code_hash) VALUES (?,?)",user,hash(recovery));}
|
||||
return codes;
|
||||
}
|
||||
@Transactional(noRollbackFor=ApiException.class)
|
||||
public void verifyLogin(UUID user,String code) {
|
||||
var row=locked(user);if(row.isEmpty()||!Boolean.TRUE.equals(row.get("enabled"))) return;
|
||||
if(code==null||code.isBlank()) throw new ApiException("MFA_REQUIRED","请输入认证器验证码或一次性恢复码",401);
|
||||
verifyRow(user,row,code,true);
|
||||
}
|
||||
@Transactional(noRollbackFor=ApiException.class)
|
||||
public void disable(UUID user,String code) {
|
||||
var row=locked(user);if(row.isEmpty()||!Boolean.TRUE.equals(row.get("enabled"))) throw new ApiException("MFA_NOT_ENABLED","双因素认证未启用",400);
|
||||
verifyRow(user,row,code,true);
|
||||
jdbc.update("DELETE FROM user_totp_recovery WHERE user_id=?",user);jdbc.update("DELETE FROM user_totp WHERE user_id=?",user);
|
||||
}
|
||||
private Map<String,Object> locked(UUID user) {var rows=jdbc.queryForList("SELECT * FROM user_totp WHERE user_id=? FOR UPDATE",user);return rows.isEmpty()?Map.of():rows.getFirst();}
|
||||
private void verifyRow(UUID user,Map<String,Object> row,String code,boolean recoveryAllowed) {
|
||||
Instant now=Instant.now();boolean fresh=((java.sql.Timestamp)row.get("attempt_window")).toInstant().isAfter(now.minusSeconds(300));
|
||||
int attempts=fresh?((Number)row.get("attempts")).intValue():0;
|
||||
if(attempts>=5) throw new ApiException("MFA_RATE_LIMITED","验证码尝试次数过多,请 5 分钟后重试",429);
|
||||
jdbc.update("UPDATE user_totp SET attempts=?,attempt_window=CASE WHEN ? THEN attempt_window ELSE now() END WHERE user_id=?",attempts+1,fresh,user);
|
||||
String normalized=code==null?"":code.strip();
|
||||
if(recoveryAllowed&&normalized.matches("[0-9a-fA-F]{24}")&&jdbc.update("UPDATE user_totp_recovery SET used_at=now() WHERE user_id=? AND code_hash=? AND used_at IS NULL",user,hash(normalized.toLowerCase(Locale.ROOT)))==1) {jdbc.update("UPDATE user_totp SET attempts=0 WHERE user_id=?",user);return;}
|
||||
long step=now.getEpochSecond()/30,last=((Number)row.get("last_step")).longValue();String secret=cipher.decrypt(row.get("secret_ciphertext").toString());
|
||||
for(long candidate=step-1;candidate<=step+1;candidate++) {
|
||||
if(candidate>last&&MessageDigest.isEqual(totp(secret,candidate).getBytes(StandardCharsets.US_ASCII),normalized.getBytes(StandardCharsets.US_ASCII))) {
|
||||
jdbc.update("UPDATE user_totp SET last_step=?,attempts=0,updated_at=now() WHERE user_id=?",candidate,user);return;
|
||||
}
|
||||
}
|
||||
throw new ApiException("INVALID_MFA_CODE","验证码无效或已使用",401);
|
||||
}
|
||||
static String totp(String secret,long step) {
|
||||
try {Mac mac=Mac.getInstance("HmacSHA1");mac.init(new SecretKeySpec(decode32(secret),"HmacSHA1"));byte[] value=mac.doFinal(ByteBuffer.allocate(8).putLong(step).array());int offset=value[value.length-1]&15;int number=ByteBuffer.wrap(value,offset,4).getInt()&0x7fffffff;return String.format(Locale.ROOT,"%06d",number%1000000);} catch(Exception e) {throw new IllegalStateException(e);}
|
||||
}
|
||||
static String base32(byte[] data) {String alphabet="ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";StringBuilder result=new StringBuilder();int bits=0,buffer=0;for(byte b:data) {buffer=(buffer<<8)|(b&255);bits+=8;while(bits>=5) {bits-=5;result.append(alphabet.charAt((buffer>>bits)&31));}}if(bits>0) result.append(alphabet.charAt((buffer<<(5-bits))&31));return result.toString();}
|
||||
private static byte[] decode32(String text) {var out=new java.io.ByteArrayOutputStream();int bits=0,buffer=0;for(char c:text.toCharArray()) {int n="ABCDEFGHIJKLMNOPQRSTUVWXYZ234567".indexOf(c);if(n<0) throw new IllegalArgumentException();buffer=(buffer<<5)|n;bits+=5;if(bits>=8) {bits-=8;out.write((buffer>>bits)&255);}}return out.toByteArray();}
|
||||
private static String hash(String value) {try {return HexFormat.of().formatHex(MessageDigest.getInstance("SHA-256").digest(value.getBytes(StandardCharsets.UTF_8)));} catch(Exception e) {throw new IllegalStateException(e);}}
|
||||
}
|
||||
@@ -199,7 +199,7 @@ public class CatalogService {
|
||||
Integer priority = integerOrNull(input.get("priority"));
|
||||
Integer maxConcurrency = integerOrNull(input.get("maxConcurrency"));
|
||||
if (priority != null && priority < 0) throw new ApiException("INVALID_MODEL_PRIORITY", "模型路由优先级必须大于等于 0");
|
||||
if (maxConcurrency != null && maxConcurrency < 1) throw new ApiException("INVALID_MODEL_CONCURRENCY", "模型并发上限必须大于等于 1");
|
||||
if (maxConcurrency != null && maxConcurrency < 0) throw new ApiException("INVALID_MODEL_CONCURRENCY", "模型并发上限必须大于等于 0;0 表示不设置平台分组上限");
|
||||
jdbc.update(
|
||||
"INSERT INTO model_catalog (id, provider_id, model_key, source_label, public_name, upstream_model, model_type, call_mode, description, input_types, output_types, capabilities, pricing, priority, max_concurrency, logical_model_id, status) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, CAST(? AS jsonb), CAST(? AS jsonb), CAST(? AS jsonb), CAST(? AS jsonb), ?, ?, ?, ?)",
|
||||
id, providerId, modelKey, sourceLabel, publicName, upstreamModel, type, mode, description,
|
||||
@@ -223,15 +223,30 @@ public class CatalogService {
|
||||
if (findProvider(providerId)==null) throw new ApiException("PROVIDER_NOT_FOUND","渠道不存在",404);
|
||||
if(groupName.length()>160 || upstream.length()>160) throw new ApiException("INVALID_MODEL","分组名称或上游标识过长");
|
||||
Integer priority=integerOrNull(value.get("priority")), concurrency=integerOrNull(value.get("maxConcurrency"));
|
||||
if ((priority!=null && priority<0) || (concurrency!=null && concurrency<1)) throw new ApiException("INVALID_MODEL_LIMIT","优先级不能小于 0,并发上限至少为 1");
|
||||
if ((priority!=null && priority<0) || (concurrency!=null && concurrency<0)) throw new ApiException("INVALID_MODEL_LIMIT","优先级和并发上限不能小于 0;并发填 0 表示不设置平台分组上限");
|
||||
validatePricing(value.get("pricing"));
|
||||
jdbc.update("UPDATE model_catalog SET logical_model_id=?, provider_id=?, source_label=?, upstream_model=?, public_name=?, model_key=?, model_type=?, call_mode=?, description=?, input_types=CAST(? AS jsonb), output_types=CAST(? AS jsonb), pricing=CAST(? AS jsonb), priority=?, max_concurrency=?, status=?, updated_at=now() WHERE id=?",logicalId,providerId,groupName,upstream,definition.get("publicName"),definition.get("modelKey"),definition.get("type"),definition.get("mode"),definition.get("description"),jsonArray(definition.get("inputTypes")),jsonArray(definition.get("outputTypes")),jsonObjectOrNull(value.get("pricing")),priority,concurrency,status(value),id);
|
||||
auth.audit("model_group.updated",admin.id(),new AuthService.RequestMeta(null,null),Map.of("groupId",id,"modelId",logicalId,"providerId",providerId));
|
||||
return findModel(id);
|
||||
}
|
||||
|
||||
private void validatePricing(Object input) {
|
||||
static void validatePricing(Object input) {
|
||||
if (!(input instanceof Map<?,?> prices)) return;
|
||||
if("CNY / 1M tokens".equals(prices.get("unit"))) com.moyi.controlplane.gateway.TokenPriceSchedule.validate(prices);
|
||||
if (prices.get("listPriceUsd") != null && prices.get("listPriceCny") != null)
|
||||
throw new ApiException("INVALID_PRICE", "标准价只能选择一种币种:人民币或美元");
|
||||
Object discount = prices.get("discount");
|
||||
if (discount != null) {
|
||||
try {
|
||||
var value = new java.math.BigDecimal(String.valueOf(discount));
|
||||
if (value.signum() < 0 || value.compareTo(java.math.BigDecimal.TEN) > 0) throw new NumberFormatException();
|
||||
} catch (NumberFormatException e) { throw new ApiException("INVALID_PRICE", "折扣须在 0–10 折之间,例如 5.5 表示 5.5 折"); }
|
||||
}
|
||||
if (prices.get("listPriceCny") != null && prices.get("exchangeRate") != null) {
|
||||
try {
|
||||
if (new java.math.BigDecimal(String.valueOf(prices.get("exchangeRate"))).compareTo(java.math.BigDecimal.ONE) != 0) throw new NumberFormatException();
|
||||
} catch (NumberFormatException e) { throw new ApiException("INVALID_PRICE", "人民币原价的换算系数必须为 1"); }
|
||||
}
|
||||
for(String field: List.of("price","input","output","cachedInput")) {
|
||||
Object amount=prices.get(field);
|
||||
if(amount==null) continue;
|
||||
@@ -517,12 +532,21 @@ public class CatalogService {
|
||||
model.put("status", resultSet.getString("status"));
|
||||
String providerCode = resultSet.getString("provider_code");
|
||||
String providerProtocol = resultSet.getString("provider_protocol");
|
||||
// Only adapters that are implemented and verified are published as
|
||||
// executable. Relay catalog rows stay visibly pending until their
|
||||
// provider endpoint has been configured and tested; protocol alone is
|
||||
// not proof that an image/video/audio task API exists.
|
||||
boolean asyncExecutor = List.of("provider-mock", "provider-vast", "provider-bytedance").contains(resultSet.getString("provider_id"));
|
||||
model.put("executionReady", !"async".equals(resultSet.getString("call_mode")) || asyncExecutor);
|
||||
// Keep the catalog state aligned with the real async adapters. The
|
||||
// OpenAI-compatible adapter handles image, video and speech tasks for
|
||||
// relay providers; it does not claim support for arbitrary async
|
||||
// model types. Provider-specific adapters cover Tripo 3D and
|
||||
// Seedance. This prevents a relay route from being shown as
|
||||
// "异步执行器待接入" when the execution path is already implemented,
|
||||
// while unsupported task types remain visibly pending.
|
||||
boolean asyncExecutor = asyncExecutorReady(
|
||||
resultSet.getString("provider_id"), providerProtocol, resultSet.getString("model_type"));
|
||||
boolean syncExecutor=List.of("chat","text","code","multimodal","llm","reasoning","vision","embedding").contains(resultSet.getString("model_type")) && ("openai-compatible".equalsIgnoreCase(providerProtocol)||"provider-mock".equals(resultSet.getString("provider_id")));
|
||||
model.put("executionReady", "async".equals(resultSet.getString("call_mode")) ? asyncExecutor : syncExecutor);
|
||||
var protocols=new java.util.ArrayList<String>();
|
||||
if(syncExecutor) protocols.add("embedding".equals(resultSet.getString("model_type"))?"embeddings":"chat");
|
||||
for(String protocol:List.of("messages","responses","gemini")) if(com.moyi.controlplane.gateway.NativeProtocolMeter.supports(model,protocol)) protocols.add(protocol);
|
||||
model.put("supportedProtocols",protocols);
|
||||
model.put("billingReady", billingReady(pricing, resultSet.getString("call_mode"), resultSet.getString("provider_id")));
|
||||
// Keep the internal route UUID for permission storage and expose a
|
||||
// stable, readable identifier for the user console and third-party
|
||||
@@ -542,6 +566,12 @@ public class CatalogService {
|
||||
return amount != null;
|
||||
}
|
||||
|
||||
static boolean asyncExecutorReady(String providerId, String providerProtocol, String modelType) {
|
||||
if (List.of("provider-mock", "provider-vast", "provider-bytedance").contains(providerId)) return true;
|
||||
if (!"openai-compatible".equalsIgnoreCase(providerProtocol)) return false;
|
||||
return List.of("image", "video", "audio").contains(String.valueOf(modelType).toLowerCase(java.util.Locale.ROOT));
|
||||
}
|
||||
|
||||
private boolean billingReady(Map<String,Object> pricing, String mode, String providerId) {
|
||||
if ("provider-mock".equals(providerId)) return true;
|
||||
if (pricing == null) return false;
|
||||
|
||||
+71
@@ -0,0 +1,71 @@
|
||||
package com.moyi.controlplane.catalog;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.moyi.controlplane.auth.*;
|
||||
import com.moyi.controlplane.common.ApiException;
|
||||
import com.moyi.controlplane.gateway.TokenPriceSchedule;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import java.math.BigDecimal;
|
||||
import java.util.*;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/admin/finance")
|
||||
public class PriceManagementController {
|
||||
private final JdbcTemplate jdbc; private final AuthService auth; private final ObjectMapper json;
|
||||
public PriceManagementController(JdbcTemplate jdbc,AuthService auth,ObjectMapper json) {this.jdbc=jdbc;this.auth=auth;this.json=json;}
|
||||
private AuthService.UserView admin(HttpServletRequest request) {return auth.requireAdmin(AuthController.sessionToken(request));}
|
||||
@PutMapping("/protocols/{modelId}") @Transactional
|
||||
public Map<String,Object> protocols(@PathVariable String modelId,@RequestBody Map<String,Object> input,HttpServletRequest request) throws Exception {
|
||||
var actor=admin(request);
|
||||
if(!(input.get("protocols") instanceof List<?> protocols)||protocols.stream().anyMatch(p->!List.of("messages","responses","gemini").contains(p))) throw new ApiException("INVALID_PROTOCOL","协议配置无效");
|
||||
if(jdbc.update("UPDATE model_catalog SET capabilities=jsonb_set(coalesce(capabilities,'{}'::jsonb),'{protocols}',?::jsonb),updated_at=now() WHERE id=? AND deleted_at IS NULL",json.writeValueAsString(protocols),modelId)!=1) throw new ApiException("MODEL_NOT_FOUND","模型不存在",404);
|
||||
jdbc.update("UPDATE model_catalog SET capabilities=jsonb_set(capabilities,'{failover}',?::jsonb) WHERE id=?",Boolean.TRUE.equals(input.get("failover"))?"true":"false",modelId);
|
||||
auth.audit("model.protocols_updated",actor.id(),AuthController.meta(request),Map.of("modelId",modelId,"protocols",protocols,"failover",Boolean.TRUE.equals(input.get("failover"))));return Map.of("saved",true);
|
||||
}
|
||||
@GetMapping("/prices/{modelId}")
|
||||
public Map<String,Object> prices(@PathVariable String modelId,HttpServletRequest request) {
|
||||
admin(request);
|
||||
var current=jdbc.queryForList("SELECT id,public_name,pricing::text,procurement_pricing::text FROM model_catalog WHERE id=? AND deleted_at IS NULL",modelId);
|
||||
if(current.isEmpty()) throw new ApiException("MODEL_NOT_FOUND","模型不存在",404);
|
||||
return Map.of("current",current.getFirst(),"history",jdbc.queryForList("SELECT id,pricing::text,procurement_pricing::text,created_at FROM model_price_history WHERE model_id=? ORDER BY id DESC LIMIT 50",modelId));
|
||||
}
|
||||
@PutMapping("/prices/{modelId}") @Transactional
|
||||
public Map<String,Object> save(@PathVariable String modelId,@RequestBody Map<String,Object> input,HttpServletRequest request) throws Exception {
|
||||
var actor=admin(request);
|
||||
if(!(input.get("pricing") instanceof Map<?,?> retail)) throw new ApiException("INVALID_PRICE","请填写人民币售价规则");
|
||||
CatalogService.validatePricing(retail);
|
||||
Object procurement=input.get("procurementPricing");
|
||||
if(procurement!=null) {
|
||||
if(!(procurement instanceof Map<?,?> costs)) throw new ApiException("INVALID_PRICE","采购成本规则格式无效");
|
||||
TokenPriceSchedule.validate(costs);
|
||||
}
|
||||
var current=jdbc.queryForList("SELECT pricing::text,procurement_pricing::text FROM model_catalog WHERE id=? AND deleted_at IS NULL FOR UPDATE",modelId);
|
||||
if(current.isEmpty()) throw new ApiException("MODEL_NOT_FOUND","模型不存在",404);
|
||||
if(!input.containsKey("expectedPricing") || !input.containsKey("expectedProcurementPricing") || !Boolean.TRUE.equals(jdbc.queryForObject("SELECT pricing IS NOT DISTINCT FROM ?::jsonb AND procurement_pricing IS NOT DISTINCT FROM ?::jsonb FROM model_catalog WHERE id=?",Boolean.class,input.get("expectedPricing")==null?null:json.writeValueAsString(input.get("expectedPricing")),input.get("expectedProcurementPricing")==null?null:json.writeValueAsString(input.get("expectedProcurementPricing")),modelId)))
|
||||
throw new ApiException("PRICE_CHANGED","价格已被其他管理员修改,请刷新后重试",409);
|
||||
jdbc.update("UPDATE model_catalog SET pricing=?::jsonb,procurement_pricing=?::jsonb,updated_at=now() WHERE id=?",json.writeValueAsString(retail),procurement==null?null:json.writeValueAsString(procurement),modelId);
|
||||
auth.audit("model.price_updated",actor.id(),AuthController.meta(request),Map.of("modelId",modelId));
|
||||
return prices(modelId,request);
|
||||
}
|
||||
@GetMapping("/reconciliation")
|
||||
public Map<String,Object> reconciliation(HttpServletRequest request,@RequestParam(defaultValue="1") int page) {
|
||||
admin(request); int offset=(Math.max(1,Math.min(page,100000))-1)*50;
|
||||
return Map.of("records",jdbc.queryForList("SELECT ue.id,ue.created_at,m.public_name,ue.status,ue.cost_amount,ue.procurement_cost,ue.profit_amount,ue.cost_status,ue.error_code,ue.reconciliation_note,ue.actual_route_id,ue.observed_usage,b.price_version FROM usage_events ue LEFT JOIN model_catalog m ON m.id=ue.model_id LEFT JOIN chat_billing_requests b ON b.id=ue.id WHERE ue.deleted_at IS NULL ORDER BY ue.created_at DESC LIMIT 50 OFFSET ?",offset),
|
||||
"summary",jdbc.queryForMap("SELECT count(*) FILTER(WHERE cost_status IN ('pending','unknown')) AS pending,count(*) FILTER(WHERE cost_status='reconciled') AS reconciled,coalesce(sum(cost_amount),0) AS revenue, sum(procurement_cost) AS known_cost,sum(profit_amount) AS known_margin FROM usage_events WHERE deleted_at IS NULL AND created_at>=date_trunc('month',now())"));
|
||||
}
|
||||
@PostMapping("/reconciliation/{id}") @Transactional
|
||||
public Map<String,Object> reconcile(@PathVariable UUID id,@RequestBody Map<String,Object> input,HttpServletRequest request) {
|
||||
var actor=admin(request); BigDecimal cost=TokenPriceSchedule.amount(input.get("procurementCost"));
|
||||
String note=String.valueOf(input.getOrDefault("note","" )).trim();
|
||||
if(note.length()<3||note.length()>2000) throw new ApiException("NOTE_REQUIRED","请填写 3–2000 字的渠道账单依据");
|
||||
var rows=jdbc.queryForList("SELECT cost_status,procurement_cost FROM usage_events WHERE id=? FOR UPDATE",id);
|
||||
if(rows.isEmpty()) throw new ApiException("USAGE_NOT_FOUND","记录不存在",404);
|
||||
jdbc.update("UPDATE usage_events SET procurement_cost=?,profit_amount=CASE WHEN cost_amount IS NULL THEN NULL ELSE cost_amount-? END,cost_status='reconciled',reconciliation_note=?,reconciled_by=?,reconciled_at=now() WHERE id=?",cost,cost,note,actor.id(),id);
|
||||
var details=new LinkedHashMap<String,Object>();details.put("usageId",id);details.put("before",rows.getFirst());details.put("procurementCost",cost);details.put("note",note);
|
||||
auth.audit("usage.cost_reconciled",actor.id(),AuthController.meta(request),details);
|
||||
return Map.of("reconciled",true);
|
||||
}
|
||||
}
|
||||
@@ -22,7 +22,7 @@ public class WebConfig implements WebMvcConfigurer {
|
||||
registry.addMapping("/**")
|
||||
.allowedOrigins(allowedOrigins)
|
||||
.allowedMethods("GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS")
|
||||
.allowedHeaders("Content-Type", "Authorization", "X-Moyi-Source", "Idempotency-Key")
|
||||
.allowedHeaders("Content-Type", "Authorization", "X-Moyi-Source", "Idempotency-Key", "x-api-key", "anthropic-version", "x-goog-api-key", "X-Moyi-Session")
|
||||
.exposedHeaders("X-Request-Id", "X-Moyi-Cost-CNY")
|
||||
.allowCredentials(true)
|
||||
.maxAge(3600);
|
||||
|
||||
@@ -9,18 +9,22 @@ import java.util.*;
|
||||
/** Monetary calculations never use floating point or locally estimated completion usage. */
|
||||
public final class ChatMeter {
|
||||
private ChatMeter() {}
|
||||
public record Rates(BigDecimal input, BigDecimal output, BigDecimal cachedInput) {
|
||||
public Map<String,Object> snapshot() {
|
||||
return Map.of("input",input,"output",output,"cachedInput",cachedInput,"unit","CNY / 1M tokens");
|
||||
public record Rates(BigDecimal input, BigDecimal output, BigDecimal cachedInput, Map<String,Object> schedule) {
|
||||
public Rates(BigDecimal input, BigDecimal output, BigDecimal cachedInput) {
|
||||
this(input,output,cachedInput,Map.of("input",input,"output",output,"cachedInput",cachedInput,"unit","CNY / 1M tokens"));
|
||||
}
|
||||
public Map<String,Object> snapshot() {
|
||||
return schedule;
|
||||
}
|
||||
public BigDecimal cost(Tokens tokens) { return TokenPriceSchedule.cost(schedule,tokens); }
|
||||
public BigDecimal hold(long inputTokens,long maxOutput) { return TokenPriceSchedule.hold(schedule,inputTokens,maxOutput); }
|
||||
public BigDecimal cost(long prompt, long completion, long cached) {
|
||||
return input.multiply(BigDecimal.valueOf(prompt-cached))
|
||||
.add(cachedInput.multiply(BigDecimal.valueOf(cached)))
|
||||
.add(output.multiply(BigDecimal.valueOf(completion)))
|
||||
.divide(new BigDecimal("1000000"),8,RoundingMode.HALF_UP);
|
||||
return cost(new Tokens(prompt,completion,cached));
|
||||
}
|
||||
}
|
||||
public record Tokens(long prompt, long completion, long cached) {}
|
||||
public record Tokens(long prompt, long completion, long cached, long cacheWrite5m, long cacheWrite1h) {
|
||||
public Tokens(long prompt,long completion,long cached) { this(prompt,completion,cached,0,0); }
|
||||
}
|
||||
public record Request(Map<String,Object> body, long reservedInputTokens, int maxOutputTokens) {}
|
||||
|
||||
public static Rates rates(Object value, boolean mock) {
|
||||
@@ -31,7 +35,7 @@ public final class ChatMeter {
|
||||
throw new ApiException("BILLING_UNIT_UNSUPPORTED","当前聊天接口仅支持 Token 计费;该模型应使用其对应的媒体或按次接口",503);
|
||||
BigDecimal input=price(prices.get("input")), output=price(prices.get("output"));
|
||||
BigDecimal cached=prices.get("cachedInput")==null?input:price(prices.get("cachedInput"));
|
||||
return new Rates(input,output,cached);
|
||||
return new Rates(input,output,cached,TokenPriceSchedule.validate(prices));
|
||||
}
|
||||
private static BigDecimal price(Object value) {
|
||||
try {
|
||||
@@ -46,8 +50,14 @@ public final class ChatMeter {
|
||||
Object cached=usage.get("prompt_cache_hit_tokens");
|
||||
if(usage.get("prompt_tokens_details") instanceof Map<?,?> details && details.get("cached_tokens")!=null) cached=details.get("cached_tokens");
|
||||
long hits=cached==null?0:count(cached);
|
||||
if(hits>prompt) throw invalidUsage();
|
||||
return new Tokens(prompt,completion,hits);
|
||||
long writes=usage.get("cache_creation_input_tokens")==null?0:count(usage.get("cache_creation_input_tokens"));
|
||||
long hour=0;
|
||||
if(usage.get("cache_creation") instanceof Map<?,?> creation) {
|
||||
hour=creation.get("ephemeral_1h_input_tokens")==null?0:count(creation.get("ephemeral_1h_input_tokens"));
|
||||
if(creation.get("ephemeral_5m_input_tokens")!=null && count(creation.get("ephemeral_5m_input_tokens"))+hour!=writes) throw invalidUsage();
|
||||
}
|
||||
if(hour>writes || hits+writes>prompt) throw invalidUsage();
|
||||
return new Tokens(prompt,completion,hits,writes-hour,hour);
|
||||
}
|
||||
private static long count(Object value) {
|
||||
try {
|
||||
|
||||
+1
@@ -83,6 +83,7 @@ final class ChatStreamCollector {
|
||||
return "";
|
||||
}
|
||||
Integer firstTokenMs() { return firstToken; }
|
||||
Object observedUsage() {return usage;}
|
||||
Map<String,Object> result() {
|
||||
if(finish==null) throw new ApiException("UPSTREAM_STREAM_INCOMPLETE","上游未完成输出,本次未扣费",502);
|
||||
if(content.toString().isBlank() && reasoning.toString().isBlank() && !hasToolOutput())
|
||||
|
||||
+86
@@ -0,0 +1,86 @@
|
||||
package com.moyi.controlplane.gateway;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.moyi.controlplane.common.ApiException;
|
||||
import java.math.BigDecimal;
|
||||
import java.net.InetAddress;
|
||||
import java.time.*;
|
||||
import java.util.*;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.context.request.RequestContextHolder;
|
||||
import org.springframework.web.context.request.ServletRequestAttributes;
|
||||
|
||||
/** Policy checks fail closed. Budget reservations run inside the caller's wallet transaction. */
|
||||
@Service
|
||||
public class EnterprisePolicies {
|
||||
private final JdbcTemplate jdbc;
|
||||
private final ObjectMapper json;
|
||||
public EnterprisePolicies(JdbcTemplate jdbc,ObjectMapper json) {this.jdbc=jdbc;this.json=json;}
|
||||
public record UserLimit(UUID userId,int rpm,int concurrency) {}
|
||||
public UserLimit taskLimit(UUID keyId) {
|
||||
var row=jdbc.queryForMap("SELECT k.user_id,coalesce(p.requests_per_minute,120) rpm,coalesce(p.max_concurrency,16) concurrency FROM platform_api_keys k LEFT JOIN gateway_user_policies p ON p.user_id=k.user_id WHERE k.id=?",keyId);
|
||||
return new UserLimit((UUID)row.get("user_id"),((Number)row.get("rpm")).intValue(),((Number)row.get("concurrency")).intValue());
|
||||
}
|
||||
public UserLimit access(UUID keyId) {
|
||||
var rows=jdbc.queryForList("SELECT k.user_id,p.expires_at,p.ip_allowlist,coalesce(u.requests_per_minute,120) rpm,coalesce(u.max_concurrency,16) concurrency FROM platform_api_keys k LEFT JOIN gateway_key_policies p ON p.key_id=k.id LEFT JOIN gateway_user_policies u ON u.user_id=k.user_id WHERE k.id=? AND k.status='active' AND k.deleted_at IS NULL",keyId);
|
||||
if(rows.isEmpty()) throw new ApiException("INVALID_API_KEY","API Key 不可用",401);
|
||||
var row=rows.getFirst();
|
||||
if(row.get("expires_at") instanceof java.sql.Timestamp expiry && !expiry.toInstant().isAfter(Instant.now())) throw new ApiException("API_KEY_EXPIRED","API Key 已过期",403);
|
||||
if(row.get("ip_allowlist")!=null) {
|
||||
try {
|
||||
List<String> ranges=json.readValue(row.get("ip_allowlist").toString(),new com.fasterxml.jackson.core.type.TypeReference<>(){});
|
||||
if(!ranges.isEmpty()) {
|
||||
var context=RequestContextHolder.getRequestAttributes();
|
||||
String address=context instanceof ServletRequestAttributes servlet?servlet.getRequest().getRemoteAddr():null;
|
||||
if(address==null||ranges.stream().noneMatch(range->matches(address,range))) throw new ApiException("IP_NOT_ALLOWED","此 IP 不在 API Key 白名单内",403);
|
||||
}
|
||||
} catch(ApiException e) {throw e;} catch(Exception e) {throw new ApiException("INVALID_KEY_POLICY","API Key 策略不可用",503);}
|
||||
}
|
||||
return new UserLimit((UUID)row.get("user_id"),((Number)row.get("rpm")).intValue(),((Number)row.get("concurrency")).intValue());
|
||||
}
|
||||
public static boolean matches(String address,String cidr) {
|
||||
try {
|
||||
String[] parts=cidr.split("/",-1);
|
||||
if(parts.length>2) return false;
|
||||
byte[] ip=literal(address),network=literal(parts[0]);
|
||||
int bits=parts.length==2?Integer.parseInt(parts[1]):network.length*8;
|
||||
if(bits<0||bits>network.length*8||ip.length!=network.length) return false;
|
||||
for(int i=0;i<ip.length;i++) {int mask=bits>=8?255:bits<=0?0:255<<(8-bits);if((ip[i]&mask)!=(network[i]&mask)) return false;bits-=8;}
|
||||
return true;
|
||||
} catch(Exception e) {return false;}
|
||||
}
|
||||
private static byte[] literal(String address) throws Exception {
|
||||
if(address==null||!address.matches("[0-9a-fA-F:.]+")||(!address.contains(":")&&!address.matches("[0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+"))) throw new IllegalArgumentException("Literal IP required");
|
||||
return InetAddress.getByName(address).getAddress();
|
||||
}
|
||||
public static void validateRanges(List<?> ranges) {
|
||||
if(ranges.size()>100) throw new ApiException("INVALID_IP_RANGE","最多 100 个 IP / CIDR");
|
||||
for(Object item:ranges) if(!(item instanceof String range)||!matches(range.split("/",-1)[0],range)) throw new ApiException("INVALID_IP_RANGE","请输入有效的 IPv4 / IPv6 或 CIDR");
|
||||
}
|
||||
public void reserve(String requestId,UUID userId,UUID keyId,BigDecimal amount) {
|
||||
// Lock order matches wallet settlement. All keys owned by this user share one budget lock.
|
||||
jdbc.queryForList("SELECT user_id FROM user_wallets WHERE user_id=? FOR UPDATE",userId);
|
||||
var user=jdbc.queryForList("SELECT monthly_budget FROM gateway_user_policies WHERE user_id=?",userId);
|
||||
var key=jdbc.queryForList("SELECT total_budget,daily_budget,monthly_budget FROM gateway_key_policies WHERE key_id=?",keyId);
|
||||
ZonedDateTime now=ZonedDateTime.now(ZoneId.of("Asia/Shanghai"));
|
||||
Instant day=now.toLocalDate().atStartOfDay(now.getZone()).toInstant();
|
||||
Instant month=now.toLocalDate().withDayOfMonth(1).atStartOfDay(now.getZone()).toInstant();
|
||||
if(!user.isEmpty()) check(user.getFirst().get("monthly_budget"),userId,null,month,amount);
|
||||
if(!key.isEmpty()) {
|
||||
var p=key.getFirst();check(p.get("total_budget"),userId,keyId,Instant.EPOCH,amount);
|
||||
check(p.get("daily_budget"),userId,keyId,day,amount);check(p.get("monthly_budget"),userId,keyId,month,amount);
|
||||
}
|
||||
jdbc.update("INSERT INTO gateway_budget_holds(request_id,user_id,key_id,amount) VALUES (?,?,?,?)",requestId,userId,keyId,amount);
|
||||
}
|
||||
private void check(Object ceiling,UUID userId,UUID keyId,Instant since,BigDecimal amount) {
|
||||
if(ceiling==null) return;
|
||||
String keyClause=keyId==null?"":" AND api_key_id=?";
|
||||
var args=new ArrayList<Object>(List.of(userId,java.sql.Timestamp.from(since)));if(keyId!=null) args.add(keyId);
|
||||
// Includes archived usage: archival is not a budget reset.
|
||||
BigDecimal spent=jdbc.queryForObject("SELECT coalesce(sum(cost_amount),0) FROM usage_events WHERE user_id=? AND created_at>=?"+keyClause,BigDecimal.class,args.toArray());
|
||||
BigDecimal held=keyId==null?jdbc.queryForObject("SELECT coalesce(sum(amount),0) FROM gateway_budget_holds WHERE user_id=?",BigDecimal.class,userId):jdbc.queryForObject("SELECT coalesce(sum(amount),0) FROM gateway_budget_holds WHERE user_id=? AND key_id=?",BigDecimal.class,userId,keyId);
|
||||
if(spent.add(held).add(amount).compareTo(new BigDecimal(ceiling.toString()))>0) throw new ApiException("BUDGET_EXCEEDED","本次调用将超过用户或 API Key 的人民币预算,请联系管理员调整额度",402);
|
||||
}
|
||||
public void release(String requestId) {jdbc.update("DELETE FROM gateway_budget_holds WHERE request_id=?",requestId);}
|
||||
}
|
||||
+69
@@ -0,0 +1,69 @@
|
||||
package com.moyi.controlplane.gateway;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.moyi.controlplane.auth.*;
|
||||
import com.moyi.controlplane.common.ApiException;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import java.math.BigDecimal;
|
||||
import java.time.OffsetDateTime;
|
||||
import java.util.*;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api")
|
||||
public class EnterprisePolicyController {
|
||||
private final JdbcTemplate jdbc; private final AuthService auth; private final ObjectMapper json;
|
||||
public EnterprisePolicyController(JdbcTemplate jdbc,AuthService auth,ObjectMapper json) {this.jdbc=jdbc;this.auth=auth;this.json=json;}
|
||||
private AuthService.UserView owner(UUID keyId,HttpServletRequest request) {
|
||||
var user=auth.requireUser(AuthController.sessionToken(request));
|
||||
if(jdbc.queryForObject("SELECT count(*) FROM platform_api_keys WHERE id=? AND user_id=? AND deleted_at IS NULL",Integer.class,keyId,user.id())!=1) throw new ApiException("KEY_NOT_FOUND","API Key 不存在",404);
|
||||
return user;
|
||||
}
|
||||
@GetMapping("/keys/{keyId}/policy")
|
||||
public Map<String,Object> keyPolicy(@PathVariable UUID keyId,HttpServletRequest request) {
|
||||
owner(keyId,request);
|
||||
var rows=jdbc.queryForList("SELECT expires_at,ip_allowlist::text,total_budget,daily_budget,monthly_budget FROM gateway_key_policies WHERE key_id=?",keyId);
|
||||
return rows.isEmpty()?Map.of("ip_allowlist","[]"):rows.getFirst();
|
||||
}
|
||||
@PutMapping("/keys/{keyId}/policy") @Transactional
|
||||
public Map<String,Object> keyPolicy(@PathVariable UUID keyId,@RequestBody Map<String,Object> input,HttpServletRequest request) throws Exception {
|
||||
var user=owner(keyId,request);
|
||||
var ranges=input.getOrDefault("ipAllowlist",List.of());
|
||||
if(!(ranges instanceof List<?> list)) throw new ApiException("INVALID_IP_RANGE","IP 白名单应为列表");
|
||||
EnterprisePolicies.validateRanges(list);
|
||||
OffsetDateTime expiry=null;
|
||||
if(input.get("expiresAt")!=null&&!input.get("expiresAt").toString().isBlank()) {
|
||||
try {expiry=OffsetDateTime.parse(input.get("expiresAt").toString());} catch(Exception e) {throw new ApiException("INVALID_EXPIRY","请输入带时区的有效截止时间");}
|
||||
if(!expiry.isAfter(OffsetDateTime.now())) throw new ApiException("INVALID_EXPIRY","截止时间必须晚于当前时间");
|
||||
}
|
||||
// Serialize updates with admissions; lowering below current spend intentionally blocks new calls.
|
||||
jdbc.queryForList("SELECT user_id FROM user_wallets WHERE user_id=? FOR UPDATE",user.id());
|
||||
jdbc.update("INSERT INTO gateway_key_policies(key_id,expires_at,ip_allowlist,total_budget,daily_budget,monthly_budget) VALUES (?,?,?::jsonb,?,?,?) ON CONFLICT(key_id) DO UPDATE SET expires_at=excluded.expires_at,ip_allowlist=excluded.ip_allowlist,total_budget=excluded.total_budget,daily_budget=excluded.daily_budget,monthly_budget=excluded.monthly_budget,updated_at=now()",keyId,expiry,json.writeValueAsString(list),budget(input.get("totalBudget")),budget(input.get("dailyBudget")),budget(input.get("monthlyBudget")));
|
||||
auth.audit("key.policy_updated",user.id(),AuthController.meta(request),Map.of("keyId",keyId,"policy",input));
|
||||
return keyPolicy(keyId,request);
|
||||
}
|
||||
@GetMapping("/admin/users/{userId}/gateway-policy")
|
||||
public Map<String,Object> userPolicy(@PathVariable UUID userId,HttpServletRequest request) {
|
||||
auth.requireAdmin(AuthController.sessionToken(request));
|
||||
var rows=jdbc.queryForList("SELECT requests_per_minute,max_concurrency,monthly_budget FROM gateway_user_policies WHERE user_id=?",userId);
|
||||
return rows.isEmpty()?Map.of("requests_per_minute",120,"max_concurrency",16):rows.getFirst();
|
||||
}
|
||||
@PutMapping("/admin/users/{userId}/gateway-policy") @Transactional
|
||||
public Map<String,Object> userPolicy(@PathVariable UUID userId,@RequestBody Map<String,Object> input,HttpServletRequest request) {
|
||||
var actor=auth.requireAdmin(AuthController.sessionToken(request));
|
||||
int rpm=integer(input.get("requestsPerMinute"),100000),concurrency=integer(input.get("maxConcurrency"),10000);
|
||||
if(jdbc.queryForObject("SELECT count(*) FROM users WHERE id=?",Integer.class,userId)!=1) throw new ApiException("USER_NOT_FOUND","用户不存在",404);
|
||||
jdbc.update("INSERT INTO user_wallets(user_id) VALUES (?) ON CONFLICT DO NOTHING",userId);
|
||||
jdbc.queryForList("SELECT user_id FROM user_wallets WHERE user_id=? FOR UPDATE",userId);
|
||||
jdbc.update("INSERT INTO gateway_user_policies(user_id,requests_per_minute,max_concurrency,monthly_budget) VALUES (?,?,?,?) ON CONFLICT(user_id) DO UPDATE SET requests_per_minute=excluded.requests_per_minute,max_concurrency=excluded.max_concurrency,monthly_budget=excluded.monthly_budget,updated_at=now()",userId,rpm,concurrency,budget(input.get("monthlyBudget")));
|
||||
auth.audit("user.gateway_policy_updated",actor.id(),AuthController.meta(request),Map.of("userId",userId,"policy",input));
|
||||
return userPolicy(userId,request);
|
||||
}
|
||||
static BigDecimal budget(Object value) {
|
||||
if(value==null||value.toString().isBlank()) return null;
|
||||
try {var n=new BigDecimal(value.toString());if(n.signum()<0||n.scale()>8||n.compareTo(new BigDecimal("1000000000"))>0) throw new NumberFormatException();return n;} catch(Exception e) {throw new ApiException("INVALID_BUDGET","预算须为 0–10 亿人民币,最多 8 位小数;留空为不限");}
|
||||
}
|
||||
static int integer(Object value,int max) {try {int n=new BigDecimal(value.toString()).intValueExact();if(n<1||n>max) throw new NumberFormatException();return n;} catch(Exception e) {throw new ApiException("INVALID_LIMIT","限额须为 1–"+max+" 的整数");}}
|
||||
}
|
||||
+14
-7
@@ -28,6 +28,7 @@ public class GatewayController {
|
||||
private final GatewayGuard guard;
|
||||
private final ChatBillingService billing;
|
||||
private final ObjectMapper json;
|
||||
@org.springframework.beans.factory.annotation.Autowired private GatewayFailover failover;
|
||||
|
||||
public GatewayController(CatalogService catalogService, OpenAiCompatibleProvider provider, GatewayGuard guard, ChatBillingService billing, ObjectMapper json) {
|
||||
this.catalogService = catalogService;
|
||||
@@ -58,6 +59,9 @@ public class GatewayController {
|
||||
item.put("upstream_model", model.get("upstreamModel"));
|
||||
item.put("type", model.get("type"));
|
||||
item.put("mode", model.get("mode"));
|
||||
item.put("supported_protocols", model.getOrDefault("supportedProtocols",List.of()));
|
||||
item.put("execution_ready", model.getOrDefault("executionReady",false));
|
||||
item.put("billing_ready", model.getOrDefault("billingReady",false));
|
||||
return item;
|
||||
}).toList();
|
||||
catalogService.touchKey(key);
|
||||
@@ -82,9 +86,11 @@ public class GatewayController {
|
||||
Map<String, Object> model = resolveModel(key, modelId);
|
||||
if (model == null) throw new ApiException("MODEL_NOT_FOUND", "模型不存在或已下线;请使用当前 API Key 的 /v1/models 返回的模型 ID", 404);
|
||||
if ("async".equals(model.get("mode"))) throw new ApiException("ASYNC_MODEL", "该模型需要使用异步任务接口", 400);
|
||||
if(!List.of("chat","text","code","multimodal","llm","reasoning","vision").contains(String.valueOf(model.get("type")))) throw new ApiException("WRONG_MODEL_INTERFACE","该模型需要使用模型目录标明的接口",400);
|
||||
String providerId=String.valueOf(model.get("providerId"));
|
||||
String routeId=String.valueOf(model.get("id"));
|
||||
boolean mock="provider-mock".equals(providerId);
|
||||
if(!mock && !provider.isConfigured(providerId)) throw new ApiException("PROVIDER_NOT_CONFIGURED","该供应商尚未配置密钥,不能执行真实调用",503);
|
||||
if(!mock && !provider.isConfigured(providerId, routeId, String.valueOf(model.get("upstreamModel")))) throw new ApiException("PROVIDER_NOT_CONFIGURED","该供应商尚未配置密钥,不能执行真实调用",503);
|
||||
ChatMeter.Request request=ChatMeter.request(input,json);
|
||||
guard.checkRate(UUID.fromString(String.valueOf(key.get("id"))), false);
|
||||
// Reserve both limits atomically. A queued request never consumes a
|
||||
@@ -95,11 +101,12 @@ public class GatewayController {
|
||||
try {
|
||||
catalogService.touchKey(key);
|
||||
if(Boolean.TRUE.equals(request.body().get("stream"))) {
|
||||
stream(model,modelId,request.body(),ticket,servletResponse,startedAt,mock);
|
||||
stream(key,model,modelId,routeId,request.body(),ticket,servletResponse,startedAt,mock);
|
||||
return null;
|
||||
}
|
||||
Map<String,Object> body=mock?mockResponse(modelId,input):provider.chat(providerId,String.valueOf(model.get("upstreamModel")),request.body());
|
||||
Map<String,Object> body=mock?mockResponse(modelId,input):failover!=null?failover.invoke(key,model,"chat",request.body(),ticket):provider.chat(providerId,String.valueOf(model.get("upstreamModel")),routeId,request.body());
|
||||
body.put("model",modelId);
|
||||
billing.observedUsage(ticket,"chat",body.get("usage"));
|
||||
requireCompletion(body);
|
||||
var cost=billing.settle(ticket,body,elapsed(startedAt),null);
|
||||
return ResponseEntity.ok().header("X-Request-Id",ticket.id().toString()).header("X-Moyi-Cost-CNY",cost.toPlainString()).body(body);
|
||||
@@ -117,7 +124,7 @@ public class GatewayController {
|
||||
}
|
||||
}
|
||||
|
||||
private void stream(Map<String,Object> model, String modelId, Map<String,Object> input, ChatBillingService.Ticket ticket, HttpServletResponse response, long started, boolean mock) throws IOException {
|
||||
private void stream(Map<String,Object> key,Map<String,Object> model, String modelId, String routeId, Map<String,Object> input, ChatBillingService.Ticket ticket, HttpServletResponse response, long started, boolean mock) throws IOException {
|
||||
if(mock) {
|
||||
var body=mockResponse(modelId,input);
|
||||
prepareStream(response);
|
||||
@@ -130,17 +137,17 @@ public class GatewayController {
|
||||
}
|
||||
// Keep admission, the concurrency slot and the upstream stream alive on this request thread.
|
||||
// Upstream HTTP errors are inspected before committing a 200/SSE response.
|
||||
try(var upstream=provider.stream(model.get("providerId").toString(),model.get("upstreamModel").toString(),input)) {
|
||||
try(var upstream=failover!=null?failover.stream(key,model,"chat",input,ticket):provider.stream(model.get("providerId").toString(),model.get("upstreamModel").toString(),routeId,input)) {
|
||||
var collector=new ChatStreamCollector(modelId,started);
|
||||
prepareStream(response);
|
||||
boolean[] disconnected={false};
|
||||
upstream.events(chunk -> {
|
||||
try { upstream.events(chunk -> {
|
||||
chunk.put("model",modelId);
|
||||
collector.accept(chunk);
|
||||
// A closed browser must not turn an otherwise completed provider call into free usage.
|
||||
// Continue within the same hard deadline to obtain the final meter and settle once.
|
||||
if(!disconnected[0]) try { event(response,chunk); } catch(IOException clientClosed) { disconnected[0]=true; }
|
||||
});
|
||||
}); } finally {billing.observedUsage(ticket,"chat",collector.observedUsage());}
|
||||
var body=collector.result();
|
||||
billing.settle(ticket,body,elapsed(started),collector.firstTokenMs());
|
||||
if(!disconnected[0]) done(response);
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
package com.moyi.controlplane.gateway;
|
||||
import com.moyi.controlplane.catalog.CatalogService;
|
||||
import com.moyi.controlplane.provider.*;
|
||||
import com.moyi.controlplane.wallet.ChatBillingService;
|
||||
import java.util.*;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class GatewayFailover {
|
||||
private final CatalogService catalog;private final GatewayGuard guard;private final ChatBillingService billing;private final OpenAiCompatibleProvider provider;
|
||||
public GatewayFailover(CatalogService catalog,GatewayGuard guard,ChatBillingService billing,OpenAiCompatibleProvider provider) {this.catalog=catalog;this.guard=guard;this.billing=billing;this.provider=provider;}
|
||||
public List<Map<String,Object>> candidates(Map<String,Object> key,Map<String,Object> primary,String protocol) {
|
||||
var routes=new ArrayList<Map<String,Object>>();routes.add(primary);
|
||||
if(!(primary.get("capabilities") instanceof Map<?,?> cap)||!Boolean.TRUE.equals(cap.get("failover"))||primary.get("logicalModelId")==null) return routes;
|
||||
GatewayModelIds.authorizedModels(catalog,key).stream()
|
||||
.filter(m->!Objects.equals(m.get("id"),primary.get("id"))&&Objects.equals(m.get("logicalModelId"),primary.get("logicalModelId"))&&Objects.equals(m.get("upstreamModel"),primary.get("upstreamModel"))&&Objects.equals(m.get("type"),primary.get("type"))&&!"async".equals(m.get("mode")))
|
||||
.filter(m->"chat".equals(protocol)||NativeProtocolMeter.supports(m,protocol))
|
||||
.filter(m->provider.isConfigured(m.get("providerId").toString(),m.get("id").toString(),m.get("upstreamModel").toString()))
|
||||
.sorted(Comparator.comparingInt(m->m.get("priority") instanceof Number n?n.intValue():0)).limit(2).forEach(routes::add);
|
||||
return routes;
|
||||
}
|
||||
public Map<String,Object> invoke(Map<String,Object> key,Map<String,Object> model,String protocol,Map<String,Object> input,ChatBillingService.Ticket ticket) {
|
||||
var routes=candidates(key,model,protocol);
|
||||
for(int i=0;i<routes.size();i++) {
|
||||
var route=routes.get(i);try(var slot=i==0?null:guard.acquire(route,1)) {
|
||||
if(i>0) billing.actualRoute(ticket,route);
|
||||
try {return "chat".equals(protocol)?provider.chat(route.get("providerId").toString(),route.get("upstreamModel").toString(),route.get("id").toString(),input):provider.invoke(route.get("providerId").toString(),route.get("upstreamModel").toString(),route.get("id").toString(),input,protocol);}
|
||||
catch(UpstreamRejectedException e) {if(i==routes.size()-1) throw e;}
|
||||
}
|
||||
}
|
||||
throw new IllegalStateException("No route");
|
||||
}
|
||||
public OpenAiCompatibleProvider.StreamResponse stream(Map<String,Object> key,Map<String,Object> model,String protocol,Map<String,Object> input,ChatBillingService.Ticket ticket) {
|
||||
var routes=candidates(key,model,protocol);
|
||||
for(int i=0;i<routes.size();i++) {
|
||||
var route=routes.get(i);GatewayGuard.Slot slot=i==0?null:guard.acquire(route,1);
|
||||
try {
|
||||
if(i>0) billing.actualRoute(ticket,route);
|
||||
var response="chat".equals(protocol)?provider.stream(route.get("providerId").toString(),route.get("upstreamModel").toString(),route.get("id").toString(),input):provider.nativeStream(route.get("providerId").toString(),route.get("upstreamModel").toString(),route.get("id").toString(),input,protocol);
|
||||
if(slot!=null) response.onClose(slot::close);return response;
|
||||
} catch(RuntimeException e) {if(slot!=null) slot.close();if(!(e instanceof UpstreamRejectedException)||i==routes.size()-1) throw e;}
|
||||
}
|
||||
throw new IllegalStateException("No route");
|
||||
}
|
||||
}
|
||||
@@ -38,9 +38,13 @@ public class GatewayGuard {
|
||||
private final int readsPerMinute;
|
||||
private final int defaultModelConcurrency;
|
||||
private final int defaultKeyConcurrency;
|
||||
private final boolean modelConcurrencyEnabled;
|
||||
private final long queueWaitMs;
|
||||
private final long queuePollMs;
|
||||
private final String redisPrefix;
|
||||
private EnterprisePolicies policies;
|
||||
@org.springframework.beans.factory.annotation.Autowired
|
||||
public void setEnterprisePolicies(EnterprisePolicies policies) { this.policies=policies; }
|
||||
|
||||
private static DefaultRedisScript<Long> script(String location) {
|
||||
DefaultRedisScript<Long> script = new DefaultRedisScript<>();
|
||||
@@ -58,9 +62,14 @@ public class GatewayGuard {
|
||||
@Value("${moyi.gateway.default-key-concurrency:4}") int defaultKeyConcurrency,
|
||||
@Value("${moyi.gateway.queue-wait-ms:30000}") long queueWaitMs,
|
||||
@Value("${moyi.gateway.queue-poll-ms:100}") long queuePollMs,
|
||||
@Value("${moyi.gateway.redis-prefix:moyi:}") String redisPrefix
|
||||
@Value("${moyi.gateway.redis-prefix:moyi:}") String redisPrefix,
|
||||
@Value("${moyi.gateway.disable-model-concurrency:false}") boolean disableModelConcurrency
|
||||
) {
|
||||
if (requestsPerMinute < 1 || readsPerMinute < 1 || defaultModelConcurrency < 1 || defaultKeyConcurrency < 1
|
||||
// A model concurrency of 0 means "unlimited at the MOYI layer". It
|
||||
// is useful when measuring the actual upstream/channel limit: the
|
||||
// provider remains responsible for its own quota while each platform
|
||||
// API key can still retain an independent limit.
|
||||
if (requestsPerMinute < 1 || readsPerMinute < 1 || defaultModelConcurrency < 0 || defaultKeyConcurrency < 0
|
||||
|| queueWaitMs < 0 || queuePollMs < 10) {
|
||||
throw new IllegalArgumentException("Gateway limits must be positive");
|
||||
}
|
||||
@@ -69,6 +78,7 @@ public class GatewayGuard {
|
||||
this.readsPerMinute = readsPerMinute;
|
||||
this.defaultModelConcurrency = defaultModelConcurrency;
|
||||
this.defaultKeyConcurrency = defaultKeyConcurrency;
|
||||
this.modelConcurrencyEnabled = !disableModelConcurrency;
|
||||
this.queueWaitMs = queueWaitMs;
|
||||
this.queuePollMs = queuePollMs;
|
||||
if (redisPrefix == null || !redisPrefix.matches("[A-Za-z0-9:_-]{2,80}:")) {
|
||||
@@ -77,6 +87,14 @@ public class GatewayGuard {
|
||||
this.redisPrefix = redisPrefix;
|
||||
}
|
||||
|
||||
/** Compatibility constructor for callers that do not configure the measurement switch. */
|
||||
public GatewayGuard(StringRedisTemplate redis, int requestsPerMinute, int readsPerMinute,
|
||||
int defaultModelConcurrency, int defaultKeyConcurrency, long queueWaitMs, long queuePollMs,
|
||||
String redisPrefix) {
|
||||
this(redis, requestsPerMinute, readsPerMinute, defaultModelConcurrency, defaultKeyConcurrency,
|
||||
queueWaitMs, queuePollMs, redisPrefix, false);
|
||||
}
|
||||
|
||||
/** Compatibility constructor for isolated unit tests and local callers. */
|
||||
public GatewayGuard(StringRedisTemplate redis, int requestsPerMinute, int readsPerMinute,
|
||||
int defaultModelConcurrency, String redisPrefix) {
|
||||
@@ -90,6 +108,11 @@ public class GatewayGuard {
|
||||
}
|
||||
|
||||
public void checkRate(UUID keyId, boolean readOnly) {
|
||||
if(policies!=null) {
|
||||
var policy=policies.access(keyId);
|
||||
long userRetry=execute(RATE,redisPrefix+"gateway:rate:user:"+(readOnly?"read:":"invoke:")+policy.userId(),Integer.toString(readOnly?readsPerMinute:policy.rpm()),"60000");
|
||||
if(userRetry>0) throw new GatewayLimitException("USER_RATE_LIMITED","用户请求总量超过限额,请稍后重试",429,(int)((userRetry+999)/1000));
|
||||
}
|
||||
String bucket = redisPrefix + "gateway:rate:" + (readOnly ? "read:" : "invoke:") + keyId;
|
||||
long retryMs = execute(RATE, bucket, Integer.toString(readOnly ? readsPerMinute : requestsPerMinute), "60000");
|
||||
if (retryMs > 0) {
|
||||
@@ -99,9 +122,12 @@ public class GatewayGuard {
|
||||
|
||||
public int modelConcurrency(Map<String, Object> model) {
|
||||
Object limit = model.get("maxConcurrency");
|
||||
return limit instanceof Number number && number.intValue() > 0 ? number.intValue() : defaultModelConcurrency;
|
||||
return limit instanceof Number number && number.intValue() >= 0 ? number.intValue() : defaultModelConcurrency;
|
||||
}
|
||||
|
||||
/** True when MOYI enforces the model/group concurrency slot. */
|
||||
public boolean modelConcurrencyEnabled() { return modelConcurrencyEnabled; }
|
||||
|
||||
public Slot acquire(Map<String, Object> model) {
|
||||
return acquire(model, 0);
|
||||
}
|
||||
@@ -112,6 +138,7 @@ public class GatewayGuard {
|
||||
* synchronous while preventing an unbounded servlet-thread backlog.
|
||||
*/
|
||||
public Slot acquire(Map<String, Object> model, long requestedWaitMs) {
|
||||
if (!modelConcurrencyEnabled) return new Slot(null, null);
|
||||
String bucket = redisPrefix + "gateway:slots:model:" + model.get("id");
|
||||
return acquireSlot(bucket, modelConcurrency(model), "MODEL_BUSY", "当前模型并发已满,已进入排队,请稍后重试", requestedWaitMs);
|
||||
}
|
||||
@@ -131,12 +158,30 @@ public class GatewayGuard {
|
||||
|
||||
public int defaultModelConcurrency() { return defaultModelConcurrency; }
|
||||
public int defaultKeyConcurrency() { return defaultKeyConcurrency; }
|
||||
public boolean tryTaskUserSlot(UUID keyId,String taskId,long ttlMs) {
|
||||
if(policies==null) return true;
|
||||
var policy=policies.taskLimit(keyId);
|
||||
String bucket=redisPrefix+"gateway:slots:user:"+policy.userId();
|
||||
boolean acquired=execute(ACQUIRE,bucket,Integer.toString(policy.concurrency()),Long.toString(Math.max(180000,ttlMs)),taskId)==1;
|
||||
// Redis is outside the database transaction: roll back the lease if claiming the task fails.
|
||||
if(acquired&&org.springframework.transaction.support.TransactionSynchronizationManager.isSynchronizationActive())
|
||||
org.springframework.transaction.support.TransactionSynchronizationManager.registerSynchronization(new org.springframework.transaction.support.TransactionSynchronization() {@Override public void afterCompletion(int status) {if(status!=STATUS_COMMITTED) releaseQuietly(bucket,taskId);}});
|
||||
return acquired;
|
||||
}
|
||||
public void releaseTaskUserSlot(UUID userId,String taskId) {
|
||||
if(policies==null) return;
|
||||
Runnable release=()->releaseQuietly(redisPrefix+"gateway:slots:user:"+userId,taskId);
|
||||
if(org.springframework.transaction.support.TransactionSynchronizationManager.isSynchronizationActive())
|
||||
org.springframework.transaction.support.TransactionSynchronizationManager.registerSynchronization(new org.springframework.transaction.support.TransactionSynchronization() {@Override public void afterCommit() {release.run();}});
|
||||
else release.run();
|
||||
}
|
||||
|
||||
public record Capacity(long running, long queued) {}
|
||||
public record CapacitySnapshot(boolean available, Map<String, Capacity> groups) {}
|
||||
|
||||
/** Reads live leases rather than historical usage rows. Expired workers do not count as running. */
|
||||
public CapacitySnapshot capacitySnapshot(List<String> modelIds) {
|
||||
if (!modelConcurrencyEnabled) return new CapacitySnapshot(true, Map.of());
|
||||
if (modelIds.isEmpty()) return new CapacitySnapshot(true, Map.of());
|
||||
List<String> buckets = new ArrayList<>();
|
||||
for (String id : modelIds) {
|
||||
@@ -159,6 +204,23 @@ public class GatewayGuard {
|
||||
|
||||
/** Atomically reserves the model and API-Key slots, so queued requests do not hold one scarce slot while waiting for the other. */
|
||||
public Admission acquire(Map<String, Object> model, UUID keyId, long requestedWaitMs) {
|
||||
Admission admission=acquireModelAndKey(model,keyId,requestedWaitMs);
|
||||
try {
|
||||
if(policies!=null) {
|
||||
var policy=policies.access(keyId);
|
||||
admission.userSlot=acquireSlot(redisPrefix+"gateway:slots:user:"+policy.userId(),policy.concurrency(),"USER_BUSY","用户总并发已满,请稍后重试",-1);
|
||||
}
|
||||
return admission;
|
||||
} catch(RuntimeException e) {admission.close();throw e;}
|
||||
}
|
||||
|
||||
private Admission acquireModelAndKey(Map<String,Object> model,UUID keyId,long requestedWaitMs) {
|
||||
if (!modelConcurrencyEnabled) {
|
||||
// Measurement mode removes only MOYI's per-model/group guard. The
|
||||
// independent per-platform-Key limit remains active so one client
|
||||
// cannot accidentally consume the entire gateway indefinitely.
|
||||
return new Admission(new Slot(null, null), acquireKey(keyId, requestedWaitMs));
|
||||
}
|
||||
String modelBucket = redisPrefix + "gateway:slots:model:" + model.get("id");
|
||||
String keyBucket = redisPrefix + "gateway:slots:key:" + keyId;
|
||||
String waitingBucket = redisPrefix + "gateway:waiting:model:" + model.get("id");
|
||||
@@ -167,9 +229,23 @@ public class GatewayGuard {
|
||||
long deadline = System.nanoTime() + java.util.concurrent.TimeUnit.MILLISECONDS.toNanos(waitMs);
|
||||
boolean admitted = false;
|
||||
try {
|
||||
int modelLimit = modelConcurrency(model);
|
||||
// Do not create a Redis model lease when the route (or the
|
||||
// server default) is explicitly unlimited. This removes the
|
||||
// MOYI-side upstream-group cap while preserving API-Key slots.
|
||||
if (modelLimit <= 0) {
|
||||
Slot keySlot = acquireSlot(keyBucket, defaultKeyConcurrency, "KEY_BUSY", "当前 API Key 的并发已满,已进入排队,请稍后重试", requestedWaitMs);
|
||||
admitted = true;
|
||||
return new Admission(null, keySlot);
|
||||
}
|
||||
if (defaultKeyConcurrency <= 0) {
|
||||
Slot modelSlot = acquireSlot(modelBucket, modelLimit, "MODEL_BUSY", "当前模型并发已满,已进入排队,请稍后重试", requestedWaitMs);
|
||||
admitted = true;
|
||||
return new Admission(modelSlot, null);
|
||||
}
|
||||
while (true) {
|
||||
long remaining = Math.max(0, java.util.concurrent.TimeUnit.NANOSECONDS.toMillis(deadline - System.nanoTime()));
|
||||
long acquired = executePair(modelBucket, keyBucket, waitingBucket, Integer.toString(modelConcurrency(model)), Integer.toString(defaultKeyConcurrency), Long.toString(SLOT_TTL_MS), token, Long.toString(remaining), Long.toString(Math.max(SLOT_TTL_MS, waitMs + 1000)));
|
||||
long acquired = executePair(modelBucket, keyBucket, waitingBucket, Integer.toString(modelLimit), Integer.toString(defaultKeyConcurrency), Long.toString(SLOT_TTL_MS), token, Long.toString(remaining), Long.toString(Math.max(SLOT_TTL_MS, waitMs + 1000)));
|
||||
if (acquired == 1) {
|
||||
admitted = true; // The acquisition script also removed the waiting entry.
|
||||
return new Admission(modelBucket, keyBucket, token);
|
||||
@@ -191,8 +267,9 @@ public class GatewayGuard {
|
||||
}
|
||||
|
||||
private Slot acquireSlot(String bucket, int limit, String code, String message, long requestedWaitMs) {
|
||||
if (limit <= 0) return new Slot(null, null);
|
||||
String token = UUID.randomUUID().toString();
|
||||
long waitMs = Math.max(0, Math.min(queueWaitMs, requestedWaitMs > 0 ? requestedWaitMs : queueWaitMs));
|
||||
long waitMs = requestedWaitMs<0?0:Math.max(0, Math.min(queueWaitMs, requestedWaitMs > 0 ? requestedWaitMs : queueWaitMs));
|
||||
long deadline = System.nanoTime() + java.util.concurrent.TimeUnit.MILLISECONDS.toNanos(waitMs);
|
||||
while (true) {
|
||||
long acquired = execute(ACQUIRE, bucket, Integer.toString(limit), Long.toString(SLOT_TTL_MS), token);
|
||||
@@ -239,20 +316,27 @@ public class GatewayGuard {
|
||||
}
|
||||
|
||||
public final class Admission implements AutoCloseable {
|
||||
private Slot userSlot;
|
||||
private final Slot modelSlot;
|
||||
private final Slot keySlot;
|
||||
private final AtomicBoolean closed = new AtomicBoolean();
|
||||
|
||||
private Admission(String modelBucket, String keyBucket, String token) {
|
||||
this.modelSlot = new Slot(modelBucket, token);
|
||||
this.keySlot = new Slot(keyBucket, token);
|
||||
this.modelSlot = modelBucket == null ? null : new Slot(modelBucket, token);
|
||||
this.keySlot = keyBucket == null ? null : new Slot(keyBucket, token);
|
||||
}
|
||||
|
||||
private Admission(Slot modelSlot, Slot keySlot) {
|
||||
this.modelSlot = modelSlot;
|
||||
this.keySlot = keySlot;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
if (!closed.compareAndSet(false, true)) return;
|
||||
modelSlot.close();
|
||||
keySlot.close();
|
||||
if (modelSlot != null) modelSlot.close();
|
||||
if (keySlot != null) keySlot.close();
|
||||
if (userSlot != null) userSlot.close();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -266,6 +350,7 @@ public class GatewayGuard {
|
||||
@Override
|
||||
public void close() {
|
||||
if (!closed.compareAndSet(false, true)) return;
|
||||
if (key == null || token == null) return;
|
||||
// A failed release expires automatically and must not replace the upstream response.
|
||||
releaseQuietly(key, token);
|
||||
}
|
||||
|
||||
+92
@@ -0,0 +1,92 @@
|
||||
package com.moyi.controlplane.gateway;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.moyi.controlplane.catalog.CatalogService;
|
||||
import com.moyi.controlplane.common.ApiException;
|
||||
import com.moyi.controlplane.provider.OpenAiCompatibleProvider;
|
||||
import com.moyi.controlplane.wallet.ChatBillingService;
|
||||
import jakarta.servlet.http.*;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.*;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
@RestController
|
||||
public class NativeGatewayController {
|
||||
private final CatalogService catalog; private final GatewayGuard guard;
|
||||
private final ChatBillingService billing; private final OpenAiCompatibleProvider provider; private final ObjectMapper json;
|
||||
@org.springframework.beans.factory.annotation.Autowired private GatewayFailover failover;
|
||||
public NativeGatewayController(CatalogService catalog,GatewayGuard guard,ChatBillingService billing,OpenAiCompatibleProvider provider,ObjectMapper json) {this.catalog=catalog;this.guard=guard;this.billing=billing;this.provider=provider;this.json=json;}
|
||||
@PostMapping({"/v1/embeddings","/v1/messages","/v1/responses"})
|
||||
public ResponseEntity<Map<String,Object>> invoke(@RequestBody Map<String,Object> input,HttpServletRequest request,HttpServletResponse response) {
|
||||
String protocol=request.getRequestURI().substring(request.getRequestURI().lastIndexOf('/')+1);
|
||||
return execute(protocol,input,Boolean.TRUE.equals(input.get("stream")),request,response);
|
||||
}
|
||||
@PostMapping("/v1beta/models/{modelAction}")
|
||||
public ResponseEntity<Map<String,Object>> gemini(@PathVariable String modelAction,@RequestBody Map<String,Object> input,HttpServletRequest request,HttpServletResponse response) {
|
||||
int delimiter=modelAction.lastIndexOf(':');
|
||||
if(delimiter<1) throw new ApiException("INVALID_MODEL","请指定模型和 generateContent 方法",400);
|
||||
String action=modelAction.substring(delimiter+1);
|
||||
if(!Set.of("generateContent","streamGenerateContent").contains(action)) throw new ApiException("UNSUPPORTED_METHOD","方法未接入",404);
|
||||
var body=new LinkedHashMap<String,Object>(input);body.put("model",modelAction.substring(0,delimiter));
|
||||
return execute("gemini",body,"streamGenerateContent".equals(action),request,response);
|
||||
}
|
||||
private ResponseEntity<Map<String,Object>> execute(String protocol,Map<String,Object> input,boolean streaming,HttpServletRequest request,HttpServletResponse response) {
|
||||
String credential=request.getHeader("Authorization");
|
||||
if(credential!=null) credential=credential.startsWith("Bearer ")?credential.substring(7):null;
|
||||
else credential=request.getHeader("gemini".equals(protocol)?"x-goog-api-key":"x-api-key");
|
||||
if(credential==null||credential.isBlank()) throw new ApiException("UNAUTHENTICATED","请提供 MOYI API Key",401);
|
||||
var key=catalog.authenticateKey(credential);if(key==null) throw new ApiException("INVALID_API_KEY","API Key 无效或已停用",401);
|
||||
return executeForKey(protocol,input,streaming,key,request,response);
|
||||
}
|
||||
public ResponseEntity<Map<String,Object>> executeForKey(String protocol,Map<String,Object> input,boolean streaming,Map<String,Object> key,HttpServletRequest request,HttpServletResponse response) {
|
||||
UUID keyId=UUID.fromString(key.get("id").toString());guard.checkRate(keyId,false);
|
||||
String requested=String.valueOf(input.getOrDefault("model",""));
|
||||
var model=GatewayModelIds.resolve(catalog,key,requested);
|
||||
if(model==null) throw new ApiException("MODEL_NOT_FOUND","模型不存在或未授权",404);
|
||||
if(!NativeProtocolMeter.supports(model,protocol)) throw new ApiException("PROTOCOL_NOT_CONFIGURED","该模型路由尚未配置此协议,请使用模型目录标明的接口",400);
|
||||
String providerId=model.get("providerId").toString(),upstream=model.get("upstreamModel").toString(),route=model.get("id").toString();
|
||||
if(!provider.isConfigured(providerId,route,upstream)) throw new ApiException("PROVIDER_NOT_CONFIGURED","渠道凭据未配置",503);
|
||||
var normalized=NativeProtocolMeter.request(protocol,input,json);
|
||||
long start=System.currentTimeMillis();
|
||||
try(var admission=guard.acquire(model,keyId,guard.queueWaitMs())) {
|
||||
var ticket=billing.reserve(key,model,normalized,request.getHeader("Idempotency-Key"));
|
||||
billing.endpoint(ticket.id(),"gemini".equals(protocol)?"/v1beta/models:generateContent":"/v1/"+protocol);
|
||||
response.setHeader("X-Request-Id",ticket.id().toString());
|
||||
try {
|
||||
catalog.touchKey(key);
|
||||
if(!streaming) {
|
||||
var result=failover!=null?failover.invoke(key,model,protocol,normalized.body(),ticket):provider.invoke(providerId,upstream,route,normalized.body(),protocol);
|
||||
if(result.containsKey("model")) result.put("model",requested);
|
||||
billing.observedUsage(ticket,protocol,result.get("gemini".equals(protocol)?"usageMetadata":"usage"));
|
||||
var cost=billing.settle(ticket,NativeProtocolMeter.normalize(protocol,result),elapsed(start),null);
|
||||
return ResponseEntity.ok().header("X-Request-Id",ticket.id().toString()).header("X-Moyi-Cost-CNY",cost.toPlainString()).body(result);
|
||||
}
|
||||
try(var stream=failover!=null?failover.stream(key,model,protocol,normalized.body(),ticket):provider.nativeStream(providerId,upstream,route,normalized.body(),protocol)) {
|
||||
var collector=new NativeProtocolMeter.Collector(protocol);
|
||||
response.setContentType("text/event-stream");response.setCharacterEncoding("UTF-8");response.setHeader("Cache-Control","no-cache");response.setHeader("X-Accel-Buffering","no");
|
||||
boolean[] disconnected={false};Integer[] first={null};
|
||||
try {stream.events(chunk->{
|
||||
if(first[0]==null) first[0]=elapsed(start);
|
||||
collector.accept(chunk);
|
||||
if(!disconnected[0]) try {event(response,chunk);} catch(IOException e) {disconnected[0]=true;}
|
||||
},false);} finally {billing.observedUsage(ticket,protocol,collector.observedUsage());}
|
||||
billing.settle(ticket,collector.result(),elapsed(start),first[0]);
|
||||
}
|
||||
return null;
|
||||
} catch(Exception failure) {
|
||||
var error=failure instanceof ApiException api?api:new ApiException("CALL_INTERRUPTED","调用中断,渠道成本待核对",502);
|
||||
billing.fail(ticket,error.getCode(),error.getMessage(),elapsed(start));
|
||||
if(response.isCommitted()) {try {event(response,Map.of("type","error","error",Map.of("type",error.getCode(),"message",error.getMessage())));} catch(IOException ignored) {}return null;}
|
||||
response.resetBuffer();response.setContentType("application/json");throw error;
|
||||
}
|
||||
}
|
||||
}
|
||||
private int elapsed(long start) {return (int)Math.min(Integer.MAX_VALUE,System.currentTimeMillis()-start);}
|
||||
private void event(HttpServletResponse response,Map<String,Object> chunk) throws IOException {
|
||||
String type=String.valueOf(chunk.getOrDefault("type",""));
|
||||
String prefix=type.matches("[a-zA-Z0-9_.-]{1,100}")?"event: "+type+"\n":"";
|
||||
response.getOutputStream().write((prefix+"data: "+json.writeValueAsString(chunk)+"\n\n").getBytes(StandardCharsets.UTF_8));response.flushBuffer();
|
||||
}
|
||||
}
|
||||
+135
@@ -0,0 +1,135 @@
|
||||
package com.moyi.controlplane.gateway;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.moyi.controlplane.common.ApiException;
|
||||
import java.math.BigDecimal;
|
||||
import java.util.*;
|
||||
|
||||
/** Native payloads remain native; only usage is normalized for the shared CNY ledger. */
|
||||
public final class NativeProtocolMeter {
|
||||
private NativeProtocolMeter() {}
|
||||
public static boolean supports(Map<String,Object> model,String protocol) {
|
||||
if("async".equals(model.get("mode"))) return false;
|
||||
if("embeddings".equals(protocol)) return "embedding".equals(model.get("type"));
|
||||
if(!(model.get("capabilities") instanceof Map<?,?> capabilities)) return false;
|
||||
return capabilities.get("protocols") instanceof List<?> list && list.contains(protocol);
|
||||
}
|
||||
public static ChatMeter.Request request(String protocol,Map<String,Object> input,ObjectMapper json) {
|
||||
var body=new LinkedHashMap<String,Object>(input);
|
||||
if(body.containsKey("api_key")||body.containsKey("base_url")||body.containsKey("service_tier")) throw bad("不支持凭据、上游地址或服务档位覆盖");
|
||||
int max=0;
|
||||
String maxField=switch(protocol) {case "messages"->"max_tokens";case "responses"->"max_output_tokens";default->"";};
|
||||
if("embeddings".equals(protocol)) {
|
||||
Object value=body.get("input");
|
||||
if(!(value instanceof String text&&!text.isBlank()) && !(value instanceof List<?> list&&!list.isEmpty()&&list.size()<=2048)) throw bad("Embeddings input 不能为空,批量最多 2048 项");
|
||||
if(Boolean.TRUE.equals(body.get("stream"))) throw bad("Embeddings 不支持流式输出");
|
||||
body.remove("stream");
|
||||
} else {
|
||||
if(body.get("tools") instanceof List<?> tools) {
|
||||
if(tools.size()>64) throw bad("工具定义不能超过 64 个");
|
||||
for(Object tool:tools) {
|
||||
var spec=map(tool);String type=String.valueOf(spec.getOrDefault("type","custom"));
|
||||
if("gemini".equals(protocol)) {if(spec.isEmpty()||spec.keySet().stream().anyMatch(k->!k.equals("functionDeclarations"))) throw bad("当前仅支持本地函数工具");}
|
||||
else if(!Set.of("function","custom").contains(type)) throw bad("托管工具需要独立计费配置,当前仅支持本地函数工具");
|
||||
}
|
||||
}
|
||||
if(body.containsKey("container")) throw bad("当前不支持跨请求复用上游容器");
|
||||
if("messages".equals(protocol)&&(!(body.get("messages") instanceof List<?> messages)||messages.isEmpty())) throw bad("messages 不能为空");
|
||||
if("responses".equals(protocol)) {
|
||||
if(body.get("previous_response_id")!=null||body.get("conversation")!=null||Boolean.TRUE.equals(body.get("background"))) throw bad("当前 Responses 接口使用完整上下文请求,不支持云端会话或后台任务");
|
||||
if(body.get("input")==null) throw bad("input 不能为空");
|
||||
body.put("store",false);
|
||||
}
|
||||
Map<String,Object> config=null;
|
||||
Object requested=body.get(maxField);
|
||||
if("gemini".equals(protocol)) {
|
||||
if(!(body.get("contents") instanceof List<?> contents)||contents.isEmpty()) throw bad("contents 不能为空");
|
||||
config=new LinkedHashMap<>(map(body.get("generationConfig")));
|
||||
requested=config.get("maxOutputTokens");
|
||||
if(config.get("candidateCount")!=null) {try {if(new BigDecimal(config.get("candidateCount").toString()).intValueExact()!=1) throw new IllegalArgumentException();} catch(Exception e) {throw bad("当前支持 candidateCount=1");}}
|
||||
}
|
||||
try {max=requested==null?4096:new BigDecimal(requested.toString()).intValueExact();if(max<1||max>32768) throw new NumberFormatException();} catch(Exception e) {throw bad("输出 Token 上限须为 1–32768");}
|
||||
if(config!=null) {config.put("maxOutputTokens",max);body.put("generationConfig",config);} else body.put(maxField,max);
|
||||
validateContent(body);
|
||||
}
|
||||
try {int bytes=json.writeValueAsBytes(body).length;if(bytes>262144) throw new ApiException("REQUEST_TOO_LARGE","请求不能超过 256 KiB",413);return new ChatMeter.Request(body,bytes+65536L,max);} catch(java.io.IOException e) {throw bad("请求无法编码");}
|
||||
}
|
||||
private static void validateContent(Object node) {
|
||||
if(node instanceof Map<?,?> map) {
|
||||
String type=String.valueOf(map.get("type"));
|
||||
if(Set.of("image","input_image","audio","input_audio","document","input_file","item_reference","web_search","web_search_preview","computer_20250124","code_interpreter").contains(type)||map.containsKey("fileData")||map.containsKey("inlineData")||map.containsKey("cachedContent")) throw bad("该入口当前按文本与工具 Token 计费;媒体、托管工具及上游存储引用需单独配置计费适配器");
|
||||
for(Object value:map.values()) validateContent(value);
|
||||
} else if(node instanceof List<?> list) for(Object item:list) validateContent(item);
|
||||
}
|
||||
@SuppressWarnings("unchecked") static Map<String,Object> map(Object value) {return value instanceof Map<?,?> m?(Map<String,Object>)m:Map.of();}
|
||||
static long count(Object value) {try {long n=new BigDecimal(String.valueOf(value)).longValueExact();if(n<0||n>1_000_000_000) throw new NumberFormatException();return n;} catch(Exception e) {throw new ApiException("UPSTREAM_USAGE_MISSING","上游未返回有效 Token 用量",502);}}
|
||||
private static long optional(Map<String,Object> map,String field) {return map.get(field)==null?0:count(map.get(field));}
|
||||
public static Map<String,Object> normalize(String protocol,Map<String,Object> response) {
|
||||
if(response.containsKey("error")) throw new ApiException("UPSTREAM_RESPONSE_INVALID","上游返回错误",502);
|
||||
var usage=new LinkedHashMap<String,Object>();
|
||||
Map<String,Object> raw=map(response.get("usage"));
|
||||
switch(protocol) {
|
||||
case "embeddings" -> {
|
||||
if(!(response.get("data") instanceof List<?> data)||data.isEmpty()) throw badResponse();
|
||||
for(Object item:data) {
|
||||
Object vector=map(item).get("embedding");
|
||||
if(vector instanceof List<?> values) {
|
||||
if(values.isEmpty()||values.stream().anyMatch(v->!(v instanceof Number n)||!Double.isFinite(n.doubleValue()))) throw badResponse();
|
||||
} else if(vector instanceof String encoded) {
|
||||
try {byte[] bytes=Base64.getDecoder().decode(encoded);if(bytes.length==0||bytes.length%4!=0) throw new IllegalArgumentException();} catch(Exception e) {throw badResponse();}
|
||||
} else throw badResponse();
|
||||
}
|
||||
usage.put("prompt_tokens",count(raw.get("prompt_tokens")));usage.put("completion_tokens",0);
|
||||
}
|
||||
case "messages" -> {
|
||||
if(response.get("stop_reason")==null) throw badResponse();
|
||||
long read=optional(raw,"cache_read_input_tokens"),write=optional(raw,"cache_creation_input_tokens");
|
||||
usage.put("prompt_tokens",count(raw.get("input_tokens"))+read+write);usage.put("completion_tokens",count(raw.get("output_tokens")));
|
||||
usage.put("prompt_cache_hit_tokens",read);usage.put("cache_creation_input_tokens",write);
|
||||
if(raw.get("cache_creation")!=null) usage.put("cache_creation",raw.get("cache_creation"));
|
||||
}
|
||||
case "responses" -> {
|
||||
if(!Set.of("completed","incomplete").contains(String.valueOf(response.get("status")))) throw badResponse();
|
||||
usage.put("prompt_tokens",count(raw.get("input_tokens")));usage.put("completion_tokens",count(raw.get("output_tokens")));
|
||||
usage.put("prompt_tokens_details",map(raw.get("input_tokens_details")));
|
||||
}
|
||||
case "gemini" -> {
|
||||
if(!(response.get("candidates") instanceof List<?> list)||list.isEmpty()||map(list.getLast()).get("finishReason")==null) throw badResponse();
|
||||
raw=map(response.get("usageMetadata"));usage.put("prompt_tokens",count(raw.get("promptTokenCount")));
|
||||
usage.put("completion_tokens",optional(raw,"candidatesTokenCount")+optional(raw,"thoughtsTokenCount"));usage.put("prompt_cache_hit_tokens",optional(raw,"cachedContentTokenCount"));
|
||||
}
|
||||
default -> throw bad("未知协议");
|
||||
}
|
||||
var result=new LinkedHashMap<String,Object>(response);result.put("usage",usage);ChatMeter.tokens(result);return result;
|
||||
}
|
||||
public static final class Collector {
|
||||
private final String protocol;
|
||||
private final Map<String,Object> result=new LinkedHashMap<>(),usage=new LinkedHashMap<>();
|
||||
private final List<Object> content=new ArrayList<>();
|
||||
private boolean complete;
|
||||
public Collector(String protocol) {this.protocol=protocol;}
|
||||
public void accept(Map<String,Object> chunk) {
|
||||
String type=String.valueOf(chunk.get("type"));
|
||||
if("messages".equals(protocol)) {
|
||||
if("message_start".equals(type)) {result.putAll(map(chunk.get("message")));usage.putAll(map(result.get("usage")));}
|
||||
if("message_delta".equals(type)) {result.putAll(map(chunk.get("delta")));usage.putAll(map(chunk.get("usage")));}
|
||||
if(type.startsWith("content_block")) content.add(chunk);
|
||||
if("message_stop".equals(type)) complete=true;
|
||||
result.put("usage",usage);result.put("content",content);
|
||||
} else if("responses".equals(protocol)) {
|
||||
if(Set.of("response.completed","response.incomplete").contains(type)) {result.putAll(map(chunk.get("response")));complete=true;}
|
||||
if(Set.of("response.failed","error").contains(type)) throw badResponse();
|
||||
} else {
|
||||
content.add(chunk);result.put("chunks",content);
|
||||
if(chunk.containsKey("usageMetadata")) result.put("usageMetadata",chunk.get("usageMetadata"));
|
||||
if(chunk.get("candidates") instanceof List<?> candidates&&!candidates.isEmpty()) {
|
||||
if(candidates.stream().anyMatch(c->map(c).get("finishReason")!=null)) {result.put("candidates",candidates);complete=true;}
|
||||
}
|
||||
}
|
||||
}
|
||||
public Map<String,Object> result() {if(!complete) throw badResponse();return normalize(protocol,result);}
|
||||
public Object observedUsage() {return "gemini".equals(protocol)?result.get("usageMetadata"):result.get("usage");}
|
||||
}
|
||||
private static ApiException bad(String message) {return new ApiException("UNSUPPORTED_REQUEST",message,400);}
|
||||
private static ApiException badResponse() {return new ApiException("UPSTREAM_RESPONSE_INCOMPLETE","上游响应未完成,待核对渠道用量",502);}
|
||||
}
|
||||
+5
@@ -18,6 +18,11 @@ public class PlaygroundController {
|
||||
private final CatalogService catalog;
|
||||
private final GatewayController gateway;
|
||||
private final AsyncTaskService tasks;
|
||||
@org.springframework.beans.factory.annotation.Autowired private NativeGatewayController nativeGateway;
|
||||
@PostMapping(value="/embeddings",consumes="application/json")
|
||||
public ResponseEntity<Map<String,Object>> embeddings(@PathVariable String keyId,@RequestBody Map<String,Object> input,HttpServletRequest request,HttpServletResponse response) {
|
||||
return nativeGateway.executeForKey("embeddings",input,false,requireKey(request,keyId),request,response);
|
||||
}
|
||||
|
||||
public PlaygroundController(AuthService auth, CatalogService catalog, GatewayController gateway, AsyncTaskService tasks) {
|
||||
this.auth = auth;
|
||||
|
||||
+73
@@ -0,0 +1,73 @@
|
||||
package com.moyi.controlplane.gateway;
|
||||
|
||||
import com.moyi.controlplane.common.ApiException;
|
||||
import java.math.BigDecimal;
|
||||
import java.math.RoundingMode;
|
||||
import java.util.*;
|
||||
|
||||
/** A price schedule contains CNY per million tokens only, frozen when admitted. */
|
||||
public final class TokenPriceSchedule {
|
||||
private TokenPriceSchedule() {}
|
||||
public static final List<String> FIELDS=List.of("input","output","cachedInput","cacheWrite5m","cacheWrite1h");
|
||||
public static BigDecimal amount(Object value) {
|
||||
try {
|
||||
BigDecimal result=new BigDecimal(String.valueOf(value));
|
||||
if(result.signum()<0 || result.scale()>8 || result.compareTo(new BigDecimal("1000000"))>0) throw new NumberFormatException();
|
||||
return result;
|
||||
} catch(Exception e) { throw new ApiException("INVALID_PRICE","Token 单价须为非负人民币金额,最多 8 位小数,且不超过 100 万元"); }
|
||||
}
|
||||
public static Map<String,Object> validate(Map<?,?> raw) {
|
||||
if(!"CNY / 1M tokens".equals(raw.get("unit"))) throw new ApiException("INVALID_PRICE","Token 规则必须使用 CNY / 1M tokens");
|
||||
var copy=new LinkedHashMap<String,Object>(); copy.put("unit","CNY / 1M tokens");
|
||||
for(String field:FIELDS) if(raw.get(field)!=null) copy.put(field,amount(raw.get(field)));
|
||||
if(!copy.containsKey("input") || !copy.containsKey("output")) throw new ApiException("INVALID_PRICE","输入和输出单价不能为空");
|
||||
if(raw.get("tiers")!=null) {
|
||||
if(!(raw.get("tiers") instanceof List<?> tiers) || tiers.size()>20) throw new ApiException("INVALID_PRICE","上下文阶梯最多 20 档");
|
||||
var frozen=new ArrayList<Map<String,Object>>(); long last=0;
|
||||
for(Object item:tiers) {
|
||||
if(!(item instanceof Map<?,?> tier)) throw new ApiException("INVALID_PRICE","阶梯格式无效");
|
||||
long threshold;
|
||||
try { threshold=new BigDecimal(String.valueOf(tier.get("aboveInputTokens"))).longValueExact(); }
|
||||
catch(Exception e) { throw new ApiException("INVALID_PRICE","阶梯起点必须是整数 Token 数"); }
|
||||
if(threshold<=last || threshold>10_000_000) throw new ApiException("INVALID_PRICE","阶梯起点须大于 0、严格递增且不超过 1000 万 Token");
|
||||
var next=new LinkedHashMap<String,Object>(); next.put("aboveInputTokens",threshold);
|
||||
for(String field:FIELDS) if(tier.get(field)!=null) next.put(field,amount(tier.get(field)));
|
||||
if(next.size()==1) throw new ApiException("INVALID_PRICE","每档至少填写一种单价");
|
||||
frozen.add(Collections.unmodifiableMap(next)); last=threshold;
|
||||
}
|
||||
copy.put("tiers",List.copyOf(frozen));
|
||||
}
|
||||
return Collections.unmodifiableMap(copy);
|
||||
}
|
||||
public static Map<String,Object> at(Map<String,Object> schedule,long prompt) {
|
||||
var result=new LinkedHashMap<>(schedule);
|
||||
if(schedule.get("tiers") instanceof List<?> tiers) for(Object raw:tiers) {
|
||||
var tier=(Map<?,?>)raw;
|
||||
if(prompt>((Number)tier.get("aboveInputTokens")).longValue()) for(String field:FIELDS) if(tier.containsKey(field)) result.put(field,tier.get(field));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
public static BigDecimal cost(Map<String,Object> schedule,ChatMeter.Tokens usage) {
|
||||
var rates=at(schedule,usage.prompt());
|
||||
long ordinary=usage.prompt()-usage.cached()-usage.cacheWrite5m()-usage.cacheWrite1h();
|
||||
if(ordinary<0) throw new ApiException("UPSTREAM_USAGE_INVALID","缓存用量大于输入总用量",502);
|
||||
BigDecimal input=amount(rates.get("input"));
|
||||
return input.multiply(BigDecimal.valueOf(ordinary))
|
||||
.add(amount(rates.getOrDefault("output",0)).multiply(BigDecimal.valueOf(usage.completion())))
|
||||
.add(amount(rates.getOrDefault("cachedInput",input)).multiply(BigDecimal.valueOf(usage.cached())))
|
||||
.add(amount(rates.getOrDefault("cacheWrite5m",input)).multiply(BigDecimal.valueOf(usage.cacheWrite5m())))
|
||||
.add(amount(rates.getOrDefault("cacheWrite1h",input)).multiply(BigDecimal.valueOf(usage.cacheWrite1h())))
|
||||
.divide(new BigDecimal("1000000"),8,RoundingMode.HALF_UP);
|
||||
}
|
||||
/** A hold covers the highest tier and cache category; exact rates settle later. */
|
||||
public static BigDecimal hold(Map<String,Object> schedule,long inputTokens,long maxOutput) {
|
||||
BigDecimal in=BigDecimal.ZERO,out=BigDecimal.ZERO;
|
||||
var all=new ArrayList<Map<?,?>>(); all.add(schedule);
|
||||
if(schedule.get("tiers") instanceof List<?> tiers) for(Object tier:tiers) all.add((Map<?,?>)tier);
|
||||
for(var row:all) for(String field:FIELDS) if(row.get(field)!=null) {
|
||||
if(field.equals("output")) out=out.max(amount(row.get(field))); else in=in.max(amount(row.get(field)));
|
||||
}
|
||||
return in.multiply(BigDecimal.valueOf(inputTokens)).add(out.multiply(BigDecimal.valueOf(maxOutput)))
|
||||
.divide(new BigDecimal("1000000"),8,RoundingMode.CEILING);
|
||||
}
|
||||
}
|
||||
+132
-20
@@ -40,6 +40,7 @@ public class OpenAiCompatibleProvider {
|
||||
private final ConcurrentMap<String, AtomicInteger> keyCursors = new ConcurrentHashMap<>();
|
||||
private final ScheduledExecutorService deadlines = Executors.newSingleThreadScheduledExecutor(r -> { var t=new Thread(r,"upstream-deadlines"); t.setDaemon(true); return t; });
|
||||
private final int timeoutSeconds;
|
||||
@org.springframework.beans.factory.annotation.Autowired private UpstreamHealth health;
|
||||
|
||||
public OpenAiCompatibleProvider(
|
||||
ObjectMapper objectMapper,
|
||||
@@ -65,8 +66,18 @@ public class OpenAiCompatibleProvider {
|
||||
return provider != null && provider.hasKey();
|
||||
}
|
||||
|
||||
public boolean isConfigured(String providerId, String routeId, String upstreamModel) {
|
||||
ProviderConfig provider = resolve(providerId);
|
||||
return provider != null && provider.hasKey(routeId, upstreamModel, environment);
|
||||
}
|
||||
|
||||
public Map<String, Object> chat(String providerId, String upstreamModel, Map<String,Object> input) {
|
||||
try(var response=open(providerId,upstreamModel,input,false)) {
|
||||
return chat(providerId, upstreamModel, null, input);
|
||||
}
|
||||
|
||||
/** Route-aware overload. A route-specific key pool is preferred when configured. */
|
||||
public Map<String, Object> chat(String providerId, String upstreamModel, String routeId, Map<String,Object> input) {
|
||||
try(var response=open(providerId,upstreamModel,routeId,input,false)) {
|
||||
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> body=objectMapper.readValue(bytes, new com.fasterxml.jackson.core.type.TypeReference<>() {});
|
||||
@@ -80,51 +91,109 @@ public class OpenAiCompatibleProvider {
|
||||
}
|
||||
|
||||
public StreamResponse stream(String providerId, String upstreamModel, Map<String,Object> input) {
|
||||
return open(providerId,upstreamModel,input,true);
|
||||
return stream(providerId, upstreamModel, null, input);
|
||||
}
|
||||
|
||||
private StreamResponse open(String providerId, String upstreamModel, Map<String,Object> input, boolean streaming) {
|
||||
/** Route-aware overload. A route-specific key pool is preferred when configured. */
|
||||
public StreamResponse stream(String providerId, String upstreamModel, String routeId, Map<String,Object> input) {
|
||||
return open(providerId,upstreamModel,routeId,input,true);
|
||||
}
|
||||
|
||||
private StreamResponse open(String providerId, String upstreamModel, String routeId, Map<String,Object> input, boolean streaming) {
|
||||
return openEndpoint(providerId,upstreamModel,routeId,input,streaming,"chat");
|
||||
}
|
||||
|
||||
public Map<String,Object> invoke(String providerId,String model,String routeId,Map<String,Object> input,String protocol) {
|
||||
try(var response=openEndpoint(providerId,model,routeId,input,false,protocol)) {
|
||||
byte[] bytes=response.body.readNBytes(4*1024*1024+1);
|
||||
if(bytes.length>4*1024*1024) throw new ApiException("UPSTREAM_RESPONSE_TOO_LARGE","上游响应超过限制",502);
|
||||
Map<String,Object> result=objectMapper.readValue(bytes,new com.fasterxml.jackson.core.type.TypeReference<>(){});
|
||||
if(result.containsKey("error")) throw classifyUpstreamError(200,writeJson(result));
|
||||
return result;
|
||||
} catch(ApiException e) {throw e;} catch(Exception e) {throw new ApiException("UPSTREAM_PROVIDER_ERROR","上游响应无法读取",502);}
|
||||
}
|
||||
public StreamResponse nativeStream(String providerId,String model,String routeId,Map<String,Object> input,String protocol) {
|
||||
return openEndpoint(providerId,model,routeId,input,true,protocol);
|
||||
}
|
||||
private StreamResponse openEndpoint(String providerId,String upstreamModel,String routeId,Map<String,Object> input,boolean streaming,String protocol) {
|
||||
ProviderConfig provider = resolve(providerId);
|
||||
if (provider == null || !provider.hasKey()) {
|
||||
if (provider == null || !provider.hasKey(routeId, upstreamModel, environment)) {
|
||||
throw new ApiException("PROVIDER_NOT_CONFIGURED", "该模型的供应商尚未配置服务端 API Key", 503);
|
||||
}
|
||||
Map<String, Object> body = new LinkedHashMap<>(input);
|
||||
body.put("model", upstreamModel);
|
||||
body.put("stream", streaming);
|
||||
if(streaming) body.put("stream_options",Map.of("include_usage",true));
|
||||
if(!List.of("embeddings","gemini").contains(protocol)) body.put("stream", streaming);
|
||||
if("chat".equals(protocol)&&streaming) body.put("stream_options",Map.of("include_usage",true));
|
||||
else body.remove("stream_options");
|
||||
String apiKey = provider.nextApiKey();
|
||||
for (int attempt = 0; attempt < 2; attempt++) {
|
||||
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);
|
||||
List<String> candidates=provider.routeKeys(routeId,upstreamModel,environment);
|
||||
if(candidates.isEmpty()) candidates=provider.apiKeys();
|
||||
String healthRoute=routeId==null?providerId:routeId;
|
||||
var attempted=new java.util.HashSet<String>();
|
||||
if(health!=null) {
|
||||
apiKey=health.select(healthRoute,candidates,attempted,Math.max(0,candidates.indexOf(apiKey)));
|
||||
if(apiKey==null) throw new UpstreamRejectedException(new ApiException("UPSTREAM_ACCOUNTS_COOLING","渠道账号暂时隔离,请稍后重试",503));
|
||||
}
|
||||
boolean compatibilityRetried=false;
|
||||
long overallStarted=System.nanoTime();
|
||||
if(org.springframework.web.context.request.RequestContextHolder.getRequestAttributes() instanceof org.springframework.web.context.request.ServletRequestAttributes attrs) {
|
||||
Object previous=attrs.getRequest().getAttribute("moyi.upstreamStarted");
|
||||
if(previous instanceof Long value) overallStarted=value;else attrs.getRequest().setAttribute("moyi.upstreamStarted",overallStarted);
|
||||
}
|
||||
for (int attempt = 0; attempt < 4; attempt++) {
|
||||
long remaining=timeoutSeconds*1000L-TimeUnit.NANOSECONDS.toMillis(System.nanoTime()-overallStarted);
|
||||
if(remaining<=0) throw new ApiException("UPSTREAM_PROVIDER_TIMEOUT","上游请求超时",504);
|
||||
String json = writeJson(body);
|
||||
HttpRequest request = HttpRequest.newBuilder()
|
||||
.uri(URI.create(normalizeUrl(provider.baseUrl()) + "/chat/completions"))
|
||||
.timeout(Duration.ofSeconds(timeoutSeconds))
|
||||
HttpRequest.Builder builder = HttpRequest.newBuilder()
|
||||
.uri(URI.create(base + path))
|
||||
.timeout(Duration.ofMillis(remaining))
|
||||
.header("Authorization", "Bearer " + apiKey)
|
||||
.header("Content-Type", "application/json")
|
||||
.POST(HttpRequest.BodyPublishers.ofString(json))
|
||||
.build();
|
||||
.POST(HttpRequest.BodyPublishers.ofString(json));
|
||||
if("messages".equals(protocol)) builder.header("x-api-key",apiKey).header("anthropic-version","2023-06-01");
|
||||
if("gemini".equals(protocol)) builder.header("x-goog-api-key",apiKey);
|
||||
HttpRequest request=builder.build();
|
||||
try {
|
||||
long started=System.nanoTime();
|
||||
HttpResponse<InputStream> response = httpClient.send(request, HttpResponse.BodyHandlers.ofInputStream());
|
||||
if(health!=null) health.record(healthRoute,apiKey,response.statusCode(),(int)TimeUnit.NANOSECONDS.toMillis(System.nanoTime()-started));
|
||||
if (response.statusCode() < 200 || response.statusCode() >= 300) {
|
||||
String details = readErrorBody(response.body());
|
||||
String details;
|
||||
try(var bounded=new StreamResponse(response.body(),Math.max(1,timeoutSeconds*1000L-TimeUnit.NANOSECONDS.toMillis(System.nanoTime()-overallStarted)))) {details=readErrorBody(bounded.body);}
|
||||
if (requiresLargerOutputBudget(details)) {
|
||||
throw new ApiException("INVALID_MAX_TOKENS", "上游模型要求 max_tokens 大于 1024,请提高输出 Token 上限后重试", 400);
|
||||
}
|
||||
// Some OpenAI-compatible relays use the newer spelling for
|
||||
// reasoning-model budgets. Retry only this deterministic
|
||||
// GPT request-shape error; other failures remain bill-safe.
|
||||
if (attempt == 0 && prepareCompatibilityRetry(body, details)) continue;
|
||||
throw classifyUpstreamError(response.statusCode(), details);
|
||||
if ("chat".equals(protocol) && !compatibilityRetried && prepareCompatibilityRetry(body, details)) {compatibilityRetried=true;continue;}
|
||||
if(health!=null && List.of(401,403,429).contains(response.statusCode()) && attempt<3 && TimeUnit.NANOSECONDS.toSeconds(System.nanoTime()-overallStarted)<timeoutSeconds) {
|
||||
attempted.add(apiKey);String next=health.select(healthRoute,candidates,attempted,0);
|
||||
if(next!=null) {apiKey=next;continue;}
|
||||
}
|
||||
ApiException rejection=classifyUpstreamError(response.statusCode(),details);
|
||||
if(List.of(401,402,403,429).contains(response.statusCode())) throw new UpstreamRejectedException(rejection);
|
||||
throw rejection;
|
||||
}
|
||||
if(streaming && !response.headers().firstValue("Content-Type").orElse("").toLowerCase().startsWith("text/event-stream")) {
|
||||
response.body().close();
|
||||
throw new ApiException("UPSTREAM_STREAM_UNSUPPORTED","上游未返回 SSE 流式响应",502);
|
||||
}
|
||||
return new StreamResponse(response.body(),Math.max(1,timeoutSeconds*1000L-TimeUnit.NANOSECONDS.toMillis(System.nanoTime()-started)));
|
||||
return new StreamResponse(response.body(),Math.max(1,timeoutSeconds*1000L-TimeUnit.NANOSECONDS.toMillis(System.nanoTime()-overallStarted)));
|
||||
} catch (ApiException exception) {
|
||||
throw exception;
|
||||
} catch (HttpTimeoutException | SocketTimeoutException exception) {
|
||||
if(health!=null) health.record(healthRoute,apiKey,504,timeoutSeconds*1000);
|
||||
throw new ApiException("UPSTREAM_PROVIDER_TIMEOUT", "上游模型供应商响应超时", 504);
|
||||
} catch (InterruptedException exception) {
|
||||
Thread.currentThread().interrupt();
|
||||
@@ -163,11 +232,16 @@ public class OpenAiCompatibleProvider {
|
||||
private final InputStream body;
|
||||
private final AtomicBoolean expired=new AtomicBoolean();
|
||||
private final ScheduledFuture<?> deadline;
|
||||
private Runnable afterClose=()->{};
|
||||
public StreamResponse onClose(Runnable callback) {afterClose=callback;return this;}
|
||||
private StreamResponse(InputStream body,long remainingMs) {
|
||||
this.body=body;
|
||||
this.deadline=deadlines.schedule(() -> { expired.set(true); try { body.close(); } catch(IOException ignored) {} },remainingMs,TimeUnit.MILLISECONDS);
|
||||
}
|
||||
public void events(EventConsumer consumer) throws IOException {
|
||||
events(consumer,true);
|
||||
}
|
||||
public void events(EventConsumer consumer,boolean requireDone) throws IOException {
|
||||
var reader=new BufferedReader(new InputStreamReader(body,StandardCharsets.UTF_8));
|
||||
StringBuilder event=new StringBuilder(), line=new StringBuilder();
|
||||
long total=0;
|
||||
@@ -195,9 +269,10 @@ public class OpenAiCompatibleProvider {
|
||||
if(expired.get()) throw new ApiException("UPSTREAM_PROVIDER_TIMEOUT","上游响应超时,本次未扣费",504);
|
||||
throw exception;
|
||||
}
|
||||
if(!requireDone && !expired.get() && event.isEmpty() && line.isEmpty()) return;
|
||||
throw new ApiException(expired.get()?"UPSTREAM_PROVIDER_TIMEOUT":"UPSTREAM_STREAM_INCOMPLETE","上游流式响应中断,本次未扣费",502);
|
||||
}
|
||||
@Override public void close() { deadline.cancel(false); try { body.close(); } catch(IOException ignored) {} }
|
||||
@Override public void close() { deadline.cancel(false); try { body.close(); } catch(IOException ignored) {} finally {afterClose.run();afterClose=()->{};} }
|
||||
}
|
||||
@FunctionalInterface public interface EventConsumer { void accept(Map<String,Object> chunk) throws IOException; }
|
||||
@PreDestroy public void close() { deadlines.shutdownNow(); }
|
||||
@@ -280,7 +355,7 @@ public class OpenAiCompatibleProvider {
|
||||
if (fallbackKey != null && !fallbackKey.isBlank()) keys.add(fallbackKey.trim());
|
||||
addEnvironmentKeys(keys, providerId, environment);
|
||||
if (providerCode != null && !providerCode.isBlank() && !providerCode.equals(providerId)) addEnvironmentKeys(keys, providerCode, environment);
|
||||
return new ProviderConfig(keys.stream().distinct().toList(), baseUrl, cursor);
|
||||
return new ProviderConfig(providerId, providerCode, keys.stream().distinct().toList(), baseUrl, cursor);
|
||||
}
|
||||
|
||||
private void addEnvironmentKeys(List<String> keys, String alias, org.springframework.core.env.Environment environment) {
|
||||
@@ -289,8 +364,45 @@ public class OpenAiCompatibleProvider {
|
||||
if (configured != null && !configured.isBlank()) for (String key : configured.split("\\|")) if (!key.isBlank()) keys.add(key.trim());
|
||||
}
|
||||
|
||||
private record ProviderConfig(List<String> apiKeys, String baseUrl, AtomicInteger cursor) {
|
||||
private record ProviderConfig(String alias, String providerCode, List<String> apiKeys, String baseUrl, AtomicInteger cursor) {
|
||||
private boolean hasKey() { return !apiKeys.isEmpty(); }
|
||||
private boolean hasKey(String routeId, String upstreamModel, org.springframework.core.env.Environment environment) {
|
||||
return !apiKeys.isEmpty() || !routeKeys(routeId, upstreamModel, environment).isEmpty();
|
||||
}
|
||||
private String nextApiKey() { return apiKeys.get(Math.floorMod(cursor.getAndIncrement(), apiKeys.size())); }
|
||||
|
||||
private String nextApiKey(String routeId, String upstreamModel, org.springframework.core.env.Environment environment,
|
||||
ConcurrentMap<String, AtomicInteger> cursors) {
|
||||
List<String> routeKeys = routeKeys(routeId, upstreamModel, environment);
|
||||
if (routeKeys.isEmpty()) return nextApiKey();
|
||||
String cursorKey = "route:" + (routeId == null ? "" : routeId);
|
||||
AtomicInteger routeCursor = cursors.computeIfAbsent(cursorKey, ignored -> new AtomicInteger());
|
||||
return routeKeys.get(Math.floorMod(routeCursor.getAndIncrement(), routeKeys.size()));
|
||||
}
|
||||
|
||||
private List<String> routeKeys(String routeId, String upstreamModel, org.springframework.core.env.Environment environment) {
|
||||
if (environment == null) return List.of();
|
||||
if (routeId != null && !routeId.isBlank()) {
|
||||
String value = environment.getProperty("MOYI_UPSTREAM_API_KEYS_ROUTE_" + normalize(routeId));
|
||||
if (value != null && !value.isBlank()) return parseKeys(value);
|
||||
}
|
||||
if (upstreamModel != null && !upstreamModel.isBlank()) {
|
||||
String scopedName = "MOYI_UPSTREAM_API_KEYS_MODEL_" + normalize(providerCode) + "_" + normalize(upstreamModel);
|
||||
String value = environment.getProperty(scopedName);
|
||||
if (value == null || value.isBlank()) return List.of();
|
||||
return parseKeys(value);
|
||||
}
|
||||
return List.of();
|
||||
}
|
||||
|
||||
private static List<String> parseKeys(String value) {
|
||||
List<String> keys = new ArrayList<>();
|
||||
for (String key : value.split("\\|")) if (!key.isBlank()) keys.add(key.trim());
|
||||
return keys.stream().distinct().toList();
|
||||
}
|
||||
|
||||
private static String normalize(String value) {
|
||||
return String.valueOf(value == null ? "" : value).toUpperCase(java.util.Locale.ROOT).replaceAll("[^A-Z0-9]", "_");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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());}
|
||||
}
|
||||
+134
-25
@@ -13,6 +13,9 @@ import java.net.http.HttpResponse;
|
||||
import java.time.Duration;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.util.Base64;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
@@ -29,6 +32,7 @@ import org.springframework.stereotype.Service;
|
||||
/** Real async adapters for the providers currently represented in the catalog. */
|
||||
@Service
|
||||
public class AsyncProviderClient {
|
||||
private static final Duration UPSTREAM_REQUEST_TIMEOUT = Duration.ofSeconds(60);
|
||||
private final JdbcTemplate jdbc;
|
||||
private final SecretCipher cipher;
|
||||
private final ObjectMapper mapper;
|
||||
@@ -46,10 +50,18 @@ public class AsyncProviderClient {
|
||||
return submit(providerId, upstreamModel, taskType, Map.of("prompt", prompt));
|
||||
}
|
||||
|
||||
public Map<String,Object> submit(String providerId, String upstreamModel, String routeId, String taskType, String prompt) {
|
||||
return submit(providerId, upstreamModel, routeId, taskType, Map.of("prompt", prompt));
|
||||
}
|
||||
|
||||
public Map<String,Object> submit(String providerId, String upstreamModel, String taskType, Map<String,Object> input) {
|
||||
return submit(providerId, upstreamModel, null, taskType, input);
|
||||
}
|
||||
|
||||
public Map<String,Object> submit(String providerId, String upstreamModel, String routeId, String taskType, Map<String,Object> input) {
|
||||
if (input == null) input = Map.of();
|
||||
String prompt = String.valueOf(input.getOrDefault("prompt", ""));
|
||||
Provider provider = provider(providerId);
|
||||
Provider provider = provider(providerId, routeId, upstreamModel);
|
||||
String code = provider.code().toLowerCase(java.util.Locale.ROOT);
|
||||
if (isTripo(provider.code(), provider.baseUrl())) {
|
||||
Map<String,Object> body = new LinkedHashMap<>();
|
||||
@@ -59,7 +71,8 @@ public class AsyncProviderClient {
|
||||
else if (!prompt.isBlank()) body.put("prompt", prompt);
|
||||
body.put("texture", true);
|
||||
body.put("pbr", true);
|
||||
return request("POST", tripoBase(provider.baseUrl()) + (media == null ? "/generation/text-to-model" : "/generation/image-to-model"), provider.nextApiKey(), body, "上游 3D 任务创建失败");
|
||||
String apiKey = provider.nextApiKey(routeId, upstreamModel, environment, keyCursors);
|
||||
return withKeyFingerprint(request("POST", tripoBase(provider.baseUrl()) + (media == null ? "/generation/text-to-model" : "/generation/image-to-model"), apiKey, body, "上游 3D 任务创建失败"), apiKey);
|
||||
}
|
||||
if (code.contains("byte") || code.contains("seedance") || provider.baseUrl().contains("volces.com")) {
|
||||
List<Map<String,Object>> content = new ArrayList<>();
|
||||
@@ -70,25 +83,39 @@ public class AsyncProviderClient {
|
||||
content.add(Map.of("type", "image_url", "image_url", Map.of("url", media)));
|
||||
}
|
||||
Map<String,Object> body = Map.of("model", upstreamModel, "content", content);
|
||||
return request("POST", trim(provider.baseUrl()) + "/contents/generations/tasks", provider.nextApiKey(), body, "上游视频任务创建失败");
|
||||
String apiKey = provider.nextApiKey(routeId, upstreamModel, environment, keyCursors);
|
||||
return withKeyFingerprint(request("POST", trim(provider.baseUrl()) + "/contents/generations/tasks", apiKey, body, "上游视频任务创建失败"), apiKey);
|
||||
}
|
||||
if ("openai-compatible".equalsIgnoreCase(provider.protocol())) {
|
||||
return submitOpenAiCompatible(provider, upstreamModel, taskType, input);
|
||||
return submitOpenAiCompatible(provider, routeId, upstreamModel, taskType, input);
|
||||
}
|
||||
throw new ApiException("ASYNC_PROVIDER_NOT_IMPLEMENTED", "该异步渠道没有可用的执行器", 501);
|
||||
}
|
||||
|
||||
public Map<String,Object> poll(String providerId, String providerTaskId) {
|
||||
Provider provider = provider(providerId);
|
||||
return poll(providerId, null, providerTaskId);
|
||||
}
|
||||
|
||||
public Map<String,Object> poll(String providerId, String routeId, String providerTaskId) {
|
||||
return poll(providerId, routeId, null, providerTaskId);
|
||||
}
|
||||
|
||||
public Map<String,Object> poll(String providerId, String routeId, String upstreamModel, String providerTaskId) {
|
||||
return poll(providerId, routeId, upstreamModel, providerTaskId, null);
|
||||
}
|
||||
|
||||
public Map<String,Object> poll(String providerId, String routeId, String upstreamModel, String providerTaskId, String keyFingerprint) {
|
||||
Provider provider = provider(providerId, routeId, upstreamModel);
|
||||
String code = provider.code().toLowerCase(java.util.Locale.ROOT);
|
||||
String url;
|
||||
String apiKey = provider.keyForFingerprint(routeId, upstreamModel, environment, keyCursors, keyFingerprint);
|
||||
if (isTripo(provider.code(), provider.baseUrl())) url = tripoBase(provider.baseUrl()) + "/tasks/" + encode(providerTaskId);
|
||||
else if (code.contains("byte") || code.contains("seedance") || provider.baseUrl().contains("volces.com")) url = trim(provider.baseUrl()) + "/contents/generations/tasks/" + encode(providerTaskId);
|
||||
else if ("openai-compatible".equalsIgnoreCase(provider.protocol())) {
|
||||
return pollOpenAiCompatible(provider, providerTaskId);
|
||||
return pollOpenAiCompatible(provider, routeId, upstreamModel, providerTaskId, keyFingerprint);
|
||||
}
|
||||
else throw new ApiException("ASYNC_PROVIDER_NOT_IMPLEMENTED", "该异步渠道没有可用的执行器", 501);
|
||||
return request("GET", url, provider.nextApiKey(), null, "上游异步任务查询失败");
|
||||
return request("GET", url, apiKey, null, "上游异步任务查询失败");
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -97,7 +124,7 @@ public class AsyncProviderClient {
|
||||
* generation completes in the request, so mark the normalized result for
|
||||
* AsyncTaskService to settle immediately instead of inventing a task id.
|
||||
*/
|
||||
private Map<String,Object> submitOpenAiCompatible(Provider provider, String model, String taskType, Map<String,Object> input) {
|
||||
private Map<String,Object> submitOpenAiCompatible(Provider provider, String routeId, String model, String taskType, Map<String,Object> input) {
|
||||
String prompt = String.valueOf(input.getOrDefault("prompt", ""));
|
||||
String media = mediaUrl(input);
|
||||
String base = trim(provider.baseUrl());
|
||||
@@ -110,12 +137,13 @@ public class AsyncProviderClient {
|
||||
// choose its documented default and normalize either shape.
|
||||
copyParameter(input, body, "response_format");
|
||||
copyParameter(input, body, "size");
|
||||
String apiKey = provider.nextApiKey(routeId, model, environment, keyCursors);
|
||||
Map<String,Object> response = media == null
|
||||
? request("POST", base + "/images/generations", provider.nextApiKey(), body, "上游图片任务创建失败")
|
||||
: requestMultipart("POST", base + "/images/edits", provider.nextApiKey(), body, "image", media, "上游图片任务创建失败");
|
||||
? request("POST", base + "/images/generations", apiKey, body, "上游图片任务创建失败")
|
||||
: requestMultipart("POST", base + "/images/edits", apiKey, body, "image", media, "上游图片任务创建失败");
|
||||
Map<String,Object> result = compatibleResult(response);
|
||||
if (result != null) return Map.of("_moyiInlineResult", result);
|
||||
return response;
|
||||
if (result != null) return Map.of("_moyiInlineResult", result, "_moyiUpstreamKeyFingerprint", keyFingerprint(apiKey));
|
||||
return withKeyFingerprint(response, apiKey);
|
||||
}
|
||||
if ("video".equalsIgnoreCase(taskType)) {
|
||||
Map<String,Object> body = new LinkedHashMap<>();
|
||||
@@ -124,16 +152,16 @@ public class AsyncProviderClient {
|
||||
// The standard OpenAI video endpoint is multipart even when no
|
||||
// reference file is supplied. JSON requests are rejected by the
|
||||
// official API and by relays that proxy it faithfully.
|
||||
String apiKey = provider.nextApiKey();
|
||||
String apiKey = provider.nextApiKey(routeId, model, environment, keyCursors);
|
||||
try {
|
||||
return requestMultipart("POST", base + "/videos", apiKey, body,
|
||||
media == null ? null : "input_reference", media, "上游视频任务创建失败");
|
||||
return withKeyFingerprint(requestMultipart("POST", base + "/videos", apiKey, body,
|
||||
media == null ? null : "input_reference", media, "上游视频任务创建失败"), apiKey);
|
||||
} catch (ApiException first) {
|
||||
// A few older relays retained /videos/generations. Retry only
|
||||
// on a route miss; authentication/credit errors are returned.
|
||||
if (!isNotFound(first)) throw first;
|
||||
return requestMultipart("POST", base + "/videos/generations", apiKey, body,
|
||||
media == null ? null : "input_reference", media, "上游视频任务创建失败");
|
||||
return withKeyFingerprint(requestMultipart("POST", base + "/videos/generations", apiKey, body,
|
||||
media == null ? null : "input_reference", media, "上游视频任务创建失败"), apiKey);
|
||||
}
|
||||
}
|
||||
if ("audio".equalsIgnoreCase(taskType)) {
|
||||
@@ -146,7 +174,8 @@ public class AsyncProviderClient {
|
||||
body.put("voice", input.getOrDefault("voice", "alloy"));
|
||||
body.put("response_format", input.getOrDefault("response_format", "mp3"));
|
||||
copyParameter(input, body, "speed");
|
||||
return Map.of("_moyiInlineResult", requestAudio("POST", base + "/audio/speech", provider.nextApiKey(), body, "上游语音任务创建失败"));
|
||||
String apiKey = provider.nextApiKey(routeId, model, environment, keyCursors);
|
||||
return Map.of("_moyiInlineResult", requestAudio("POST", base + "/audio/speech", apiKey, body, "上游语音任务创建失败"), "_moyiUpstreamKeyFingerprint", keyFingerprint(apiKey));
|
||||
}
|
||||
throw new ApiException("ASYNC_PROVIDER_NOT_IMPLEMENTED", "该异步渠道的媒体接口尚未配置", 501);
|
||||
}
|
||||
@@ -170,9 +199,9 @@ public class AsyncProviderClient {
|
||||
return value;
|
||||
}
|
||||
|
||||
private Map<String,Object> pollOpenAiCompatible(Provider provider, String providerTaskId) {
|
||||
private Map<String,Object> pollOpenAiCompatible(Provider provider, String routeId, String upstreamModel, String providerTaskId, String keyFingerprint) {
|
||||
String base = trim(provider.baseUrl());
|
||||
String apiKey = provider.nextApiKey();
|
||||
String apiKey = provider.keyForFingerprint(routeId, upstreamModel, environment, keyCursors, keyFingerprint);
|
||||
try {
|
||||
return request("GET", base + "/videos/" + encode(providerTaskId), apiKey, null, "上游异步任务查询失败");
|
||||
} catch (ApiException first) {
|
||||
@@ -208,7 +237,7 @@ public class AsyncProviderClient {
|
||||
|
||||
private Map<String,Object> requestAudio(String method, String url, String key, Map<String,Object> body, String message) {
|
||||
try {
|
||||
HttpRequest.Builder builder = HttpRequest.newBuilder().uri(URI.create(url)).timeout(Duration.ofSeconds(30))
|
||||
HttpRequest.Builder builder = HttpRequest.newBuilder().uri(URI.create(url)).timeout(UPSTREAM_REQUEST_TIMEOUT)
|
||||
.header("Authorization", "Bearer " + key).header("Content-Type", "application/json");
|
||||
HttpResponse<byte[]> response = client.send(builder.POST(HttpRequest.BodyPublishers.ofString(write(body))).build(), HttpResponse.BodyHandlers.ofByteArray());
|
||||
if (response.statusCode() < 200 || response.statusCode() >= 300) {
|
||||
@@ -253,7 +282,7 @@ public class AsyncProviderClient {
|
||||
multipartFile(out, boundary, fileField, file.filename(), file.contentType(), file.bytes());
|
||||
}
|
||||
out.write(("--" + boundary + "--\r\n").getBytes(java.nio.charset.StandardCharsets.UTF_8));
|
||||
HttpRequest request = HttpRequest.newBuilder().uri(URI.create(url)).timeout(Duration.ofSeconds(30))
|
||||
HttpRequest request = HttpRequest.newBuilder().uri(URI.create(url)).timeout(UPSTREAM_REQUEST_TIMEOUT)
|
||||
.header("Authorization", "Bearer " + key)
|
||||
.header("Content-Type", "multipart/form-data; boundary=" + boundary)
|
||||
.POST(HttpRequest.BodyPublishers.ofByteArray(out.toByteArray())).build();
|
||||
@@ -335,7 +364,7 @@ public class AsyncProviderClient {
|
||||
|
||||
private Map<String,Object> request(String method, String url, String key, Map<String,Object> body, String message) {
|
||||
try {
|
||||
HttpRequest.Builder builder = HttpRequest.newBuilder().uri(URI.create(url)).timeout(Duration.ofSeconds(30))
|
||||
HttpRequest.Builder builder = HttpRequest.newBuilder().uri(URI.create(url)).timeout(UPSTREAM_REQUEST_TIMEOUT)
|
||||
.header("Authorization", "Bearer " + key).header("Content-Type", "application/json");
|
||||
if ("POST".equals(method)) builder.POST(HttpRequest.BodyPublishers.ofString(write(body)));
|
||||
else builder.GET();
|
||||
@@ -362,6 +391,10 @@ public class AsyncProviderClient {
|
||||
}
|
||||
|
||||
private Provider provider(String id) {
|
||||
return provider(id, null, null);
|
||||
}
|
||||
|
||||
private Provider provider(String id, String routeId, String upstreamModel) {
|
||||
try {
|
||||
Map<String,Object> row = jdbc.queryForMap("SELECT code, protocol, base_url, credential_ciphertext FROM model_providers WHERE id=? AND status='active' AND deleted_at IS NULL", id);
|
||||
String ciphertext = row.get("credential_ciphertext") == null ? "" : String.valueOf(row.get("credential_ciphertext"));
|
||||
@@ -377,7 +410,7 @@ public class AsyncProviderClient {
|
||||
addEnvironmentKeys(keys, "TRIPO_API_KEY");
|
||||
addEnvironmentKeys(keys, "MOYI_TRIPO_API_KEY");
|
||||
}
|
||||
if (keys.isEmpty()) throw new ApiException("PROVIDER_NOT_CONFIGURED", "异步渠道尚未配置 API Key", 503);
|
||||
if (keys.isEmpty() && routeKeys(code, id, routeId, upstreamModel).isEmpty()) throw new ApiException("PROVIDER_NOT_CONFIGURED", "异步渠道尚未配置 API Key", 503);
|
||||
return new Provider(code, String.valueOf(row.get("protocol")), String.valueOf(row.get("base_url")), keys.stream().distinct().toList(), keyCursors.computeIfAbsent(id, ignored -> new AtomicInteger()));
|
||||
} catch (EmptyResultDataAccessException exception) { throw new ApiException("PROVIDER_NOT_CONFIGURED", "异步渠道不存在或已停用", 503); }
|
||||
}
|
||||
@@ -399,12 +432,88 @@ public class AsyncProviderClient {
|
||||
String configured = environment.getProperty(name);
|
||||
if (configured != null && !configured.isBlank()) for (String value : configured.split("\\|")) if (!value.isBlank()) keys.add(value.trim());
|
||||
}
|
||||
|
||||
private List<String> routeKeys(String providerCode, String providerId, String routeId, String upstreamModel) {
|
||||
if (routeId != null && !routeId.isBlank()) {
|
||||
String configured = environment.getProperty("MOYI_UPSTREAM_API_KEYS_ROUTE_" + normalize(routeId));
|
||||
if (configured != null && !configured.isBlank()) return parseKeys(configured);
|
||||
}
|
||||
if (upstreamModel != null && !upstreamModel.isBlank()) {
|
||||
String scopedName = "MOYI_UPSTREAM_API_KEYS_MODEL_" + normalize(providerCode) + "_" + normalize(upstreamModel);
|
||||
String configured = environment.getProperty(scopedName);
|
||||
if (configured != null && !configured.isBlank()) return parseKeys(configured);
|
||||
}
|
||||
return List.of();
|
||||
}
|
||||
|
||||
private List<String> parseKeys(String configured) {
|
||||
List<String> keys = new ArrayList<>();
|
||||
for (String key : configured.split("\\|")) if (!key.isBlank()) keys.add(key.trim());
|
||||
return keys.stream().distinct().toList();
|
||||
}
|
||||
|
||||
private String normalize(String value) { return String.valueOf(value == null ? "" : value).toUpperCase(java.util.Locale.ROOT).replaceAll("[^A-Z0-9]", "_"); }
|
||||
private Map<String,Object> withKeyFingerprint(Map<String,Object> response, String apiKey) {
|
||||
Map<String,Object> copy = new LinkedHashMap<>();
|
||||
if (response != null) copy.putAll(response);
|
||||
copy.put("_moyiUpstreamKeyFingerprint", keyFingerprint(apiKey));
|
||||
return copy;
|
||||
}
|
||||
|
||||
private static String keyFingerprint(String apiKey) {
|
||||
if (apiKey == null || apiKey.isBlank()) throw new ApiException("UPSTREAM_KEY_NOT_AVAILABLE", "异步任务未选择可轮询的上游凭据", 503);
|
||||
try {
|
||||
byte[] digest = MessageDigest.getInstance("SHA-256").digest(apiKey.getBytes(StandardCharsets.UTF_8));
|
||||
StringBuilder result = new StringBuilder(digest.length * 2);
|
||||
for (byte value : digest) result.append(String.format(java.util.Locale.ROOT, "%02x", value));
|
||||
return result.toString();
|
||||
} catch (NoSuchAlgorithmException exception) {
|
||||
throw new IllegalStateException("SHA-256 is required by the Java runtime", exception);
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isTripo(String code, String baseUrl) {
|
||||
String normalizedCode = code == null ? "" : code.toLowerCase(java.util.Locale.ROOT);
|
||||
String normalizedUrl = baseUrl == null ? "" : baseUrl.toLowerCase(java.util.Locale.ROOT);
|
||||
return normalizedCode.contains("tripo") || normalizedUrl.contains("tripo3d");
|
||||
}
|
||||
private record Provider(String code, String protocol, String baseUrl, List<String> apiKeys, AtomicInteger cursor) {
|
||||
private String nextApiKey() { return apiKeys.get(Math.floorMod(cursor.getAndIncrement(), apiKeys.size())); }
|
||||
private String nextApiKey() {
|
||||
if (apiKeys.isEmpty()) throw new ApiException("UPSTREAM_KEY_NOT_AVAILABLE", "异步渠道没有可用的上游凭据", 503);
|
||||
return apiKeys.get(Math.floorMod(cursor.getAndIncrement(), apiKeys.size()));
|
||||
}
|
||||
private String nextApiKey(String routeId, String upstreamModel, Environment environment, ConcurrentMap<String, AtomicInteger> cursors) {
|
||||
List<String> routeKeys = routeKeys(routeId, upstreamModel, environment);
|
||||
if (routeKeys.isEmpty()) return nextApiKey();
|
||||
AtomicInteger routeCursor = cursors.computeIfAbsent("route:" + (routeId == null ? upstreamModel : routeId), ignored -> new AtomicInteger());
|
||||
return routeKeys.get(Math.floorMod(routeCursor.getAndIncrement(), routeKeys.size()));
|
||||
}
|
||||
private String keyForFingerprint(String routeId, String upstreamModel, Environment environment,
|
||||
ConcurrentMap<String, AtomicInteger> cursors, String fingerprint) {
|
||||
if (fingerprint == null || fingerprint.isBlank()) return nextApiKey(routeId, upstreamModel, environment, cursors);
|
||||
List<String> candidates = new ArrayList<>(routeKeys(routeId, upstreamModel, environment));
|
||||
candidates.addAll(apiKeys);
|
||||
candidates = candidates.stream().distinct().toList();
|
||||
for (String candidate : candidates) if (fingerprint.equals(keyFingerprint(candidate))) return candidate;
|
||||
throw new ApiException("UPSTREAM_KEY_NOT_AVAILABLE", "创建任务时使用的上游凭据已从服务器配置中移除,无法安全轮询", 503);
|
||||
}
|
||||
private List<String> routeKeys(String routeId, String upstreamModel, Environment environment) {
|
||||
if (environment == null) return List.of();
|
||||
if (routeId != null && !routeId.isBlank()) {
|
||||
String value = environment.getProperty("MOYI_UPSTREAM_API_KEYS_ROUTE_" + normalize(routeId));
|
||||
if (value != null && !value.isBlank()) return parseKeys(value);
|
||||
}
|
||||
if (upstreamModel != null && !upstreamModel.isBlank()) {
|
||||
String value = environment.getProperty("MOYI_UPSTREAM_API_KEYS_MODEL_" + normalize(code()) + "_" + normalize(upstreamModel));
|
||||
if (value != null && !value.isBlank()) return parseKeys(value);
|
||||
}
|
||||
return List.of();
|
||||
}
|
||||
private static List<String> parseKeys(String value) {
|
||||
List<String> keys = new ArrayList<>();
|
||||
for (String key : value.split("\\|")) if (!key.isBlank()) keys.add(key.trim());
|
||||
return keys.stream().distinct().toList();
|
||||
}
|
||||
private static String normalize(String value) { return String.valueOf(value == null ? "" : value).toUpperCase(java.util.Locale.ROOT).replaceAll("[^A-Z0-9]", "_"); }
|
||||
}
|
||||
}
|
||||
@@ -36,16 +36,29 @@ public class AsyncTaskService {
|
||||
private final int maxQueuedPerModel;
|
||||
private final int defaultModelConcurrency;
|
||||
private final int defaultKeyConcurrency;
|
||||
private final boolean modelConcurrencyEnabled;
|
||||
private final long providerTimeoutMs;
|
||||
private final GatewayGuard gatewayGuard;
|
||||
private final UsageAuditService usage;
|
||||
private final AsyncProviderClient providerClient;
|
||||
private final TransactionTemplate transactions;
|
||||
private final com.moyi.controlplane.gateway.EnterprisePolicies policies;
|
||||
|
||||
/** Compatibility constructor for isolated gateway tests that only exercise mock tasks. */
|
||||
public AsyncTaskService(JdbcTemplate jdbc, ObjectMapper objectMapper, CatalogService catalogService,
|
||||
int maxQueuedPerModel, GatewayGuard gatewayGuard, UsageAuditService usage) {
|
||||
this(jdbc, objectMapper, catalogService, maxQueuedPerModel, 4, 4, 3_600_000, gatewayGuard, usage, null, null);
|
||||
this(jdbc, objectMapper, catalogService, maxQueuedPerModel, 4, 4, 3_600_000, true, gatewayGuard, usage, null, null);
|
||||
}
|
||||
|
||||
/** Compatibility constructor for isolated tests using explicit limits. */
|
||||
public AsyncTaskService(
|
||||
JdbcTemplate jdbc, ObjectMapper objectMapper, CatalogService catalogService,
|
||||
int maxQueuedPerModel, int defaultModelConcurrency, int defaultKeyConcurrency,
|
||||
long providerTimeoutMs, GatewayGuard gatewayGuard, UsageAuditService usage,
|
||||
AsyncProviderClient providerClient, PlatformTransactionManager transactionManager
|
||||
) {
|
||||
this(jdbc, objectMapper, catalogService, maxQueuedPerModel, defaultModelConcurrency,
|
||||
defaultKeyConcurrency, providerTimeoutMs, true, gatewayGuard, usage, providerClient, transactionManager);
|
||||
}
|
||||
|
||||
@org.springframework.beans.factory.annotation.Autowired
|
||||
@@ -57,12 +70,16 @@ public class AsyncTaskService {
|
||||
@Value("${moyi.gateway.default-model-concurrency:4}") int defaultModelConcurrency,
|
||||
@Value("${moyi.gateway.default-key-concurrency:4}") int defaultKeyConcurrency,
|
||||
@Value("${moyi.tasks.provider-timeout-ms:3600000}") long providerTimeoutMs,
|
||||
@Value("${moyi.gateway.disable-model-concurrency:false}") boolean disableModelConcurrency,
|
||||
GatewayGuard gatewayGuard,
|
||||
UsageAuditService usage,
|
||||
AsyncProviderClient providerClient,
|
||||
PlatformTransactionManager transactionManager
|
||||
) {
|
||||
if (maxQueuedPerModel < 1 || defaultModelConcurrency < 1 || defaultKeyConcurrency < 1) throw new IllegalArgumentException("Task queue and concurrency limits must be positive");
|
||||
// A model concurrency of 0 means no MOYI-side model-group cap. The
|
||||
// upstream provider still enforces its own quota, while API-Key
|
||||
// concurrency remains independently configurable.
|
||||
if (maxQueuedPerModel < 1 || defaultModelConcurrency < 0 || defaultKeyConcurrency < 0) throw new IllegalArgumentException("Task queue and concurrency limits must not be negative");
|
||||
if (providerTimeoutMs < 1_000) throw new IllegalArgumentException("Provider task timeout must be at least one second");
|
||||
this.jdbc = jdbc;
|
||||
this.objectMapper = objectMapper;
|
||||
@@ -70,11 +87,13 @@ public class AsyncTaskService {
|
||||
this.maxQueuedPerModel = maxQueuedPerModel;
|
||||
this.defaultModelConcurrency = defaultModelConcurrency;
|
||||
this.defaultKeyConcurrency = defaultKeyConcurrency;
|
||||
this.modelConcurrencyEnabled = !disableModelConcurrency;
|
||||
this.providerTimeoutMs = providerTimeoutMs;
|
||||
this.gatewayGuard = gatewayGuard;
|
||||
this.usage = usage;
|
||||
this.providerClient = providerClient;
|
||||
this.transactions = transactionManager == null ? null : new TransactionTemplate(transactionManager);
|
||||
this.policies = new com.moyi.controlplane.gateway.EnterprisePolicies(jdbc,objectMapper);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
@@ -113,6 +132,7 @@ public class AsyncTaskService {
|
||||
// tests can complete them without an upstream call.
|
||||
String providerTaskId = "provider-mock".equals(model.get("providerId")) ? "mock" : null;
|
||||
String id = "task_" + UUID.randomUUID().toString().replace("-", "").substring(0, 24);
|
||||
policies.reserve(id,userId,apiKeyId,reservedAmount);
|
||||
OffsetDateTime now = now();
|
||||
try {
|
||||
jdbc.update(
|
||||
@@ -133,6 +153,7 @@ public class AsyncTaskService {
|
||||
}
|
||||
|
||||
public Map<String, Object> getForKey(Map<String, Object> key, String taskId) {
|
||||
gatewayGuard.checkRate(UUID.fromString(key.get("id").toString()),true);
|
||||
UUID userId = catalogService.keyOwnerId(UUID.fromString(String.valueOf(key.get("id"))));
|
||||
try {
|
||||
return jdbc.queryForObject("SELECT * FROM model_tasks WHERE id = ? AND user_id = ?", this::mapTask, taskId, userId);
|
||||
@@ -143,6 +164,7 @@ public class AsyncTaskService {
|
||||
|
||||
public List<Map<String, Object>> list(String authorization, int limit) {
|
||||
Map<String, Object> key = requireKey(authorization);
|
||||
gatewayGuard.checkRate(UUID.fromString(key.get("id").toString()),true);
|
||||
UUID userId = catalogService.keyOwnerId(UUID.fromString(String.valueOf(key.get("id"))));
|
||||
int boundedLimit = Math.max(1, Math.min(limit, 100));
|
||||
return jdbc.query("SELECT * FROM model_tasks WHERE user_id = ? ORDER BY created_at DESC LIMIT ?", this::mapTask, userId, boundedLimit);
|
||||
@@ -163,7 +185,7 @@ public class AsyncTaskService {
|
||||
"{\"format\":\"mp4\",\"url\":\"https://mock.moyi.ai/tasks/result.mp4\"}", now(), now()
|
||||
);
|
||||
taskIds.forEach(taskId -> usage.updateTask(taskId, "succeeded", Map.of("format", "result", "url", "https://mock.moyi.ai/tasks/" + taskId + "/result"), null, null));
|
||||
List<Map<String,Object>> pending = jdbc.queryForList("SELECT t.id, t.provider_task_id, t.provider_deadline_at, t.created_at, m.provider_id FROM model_tasks t JOIN model_catalog m ON m.id=t.model_id WHERE t.status IN ('queued','running') AND t.provider_task_id <> 'mock' AND (t.updated_at < now() - INTERVAL '5 seconds' OR t.provider_deadline_at <= now()) ORDER BY t.updated_at LIMIT 50");
|
||||
List<Map<String,Object>> pending = jdbc.queryForList("SELECT t.id, t.model_id, t.provider_task_id, t.provider_key_fingerprint, t.provider_deadline_at, t.created_at, m.provider_id, m.upstream_model FROM model_tasks t JOIN model_catalog m ON m.id=t.model_id WHERE t.status IN ('queued','running') AND t.provider_task_id <> 'mock' AND (t.updated_at < now() - INTERVAL '5 seconds' OR t.provider_deadline_at <= now()) ORDER BY t.updated_at LIMIT 50");
|
||||
for (Map<String,Object> row : pending) pollProviderTask(row);
|
||||
dispatchQueuedTasks();
|
||||
}
|
||||
@@ -187,12 +209,8 @@ public class AsyncTaskService {
|
||||
String taskId = String.valueOf(task.get("id"));
|
||||
usage.updateTask(taskId, "running", null, null, null);
|
||||
try {
|
||||
Map<String,Object> upstream = providerClient.submit(
|
||||
String.valueOf(task.get("provider_id")),
|
||||
String.valueOf(task.get("upstream_model")),
|
||||
String.valueOf(task.get("task_type")),
|
||||
taskInput(task)
|
||||
);
|
||||
Map<String,Object> input = taskInput(task);
|
||||
Map<String,Object> upstream = submitProviderTask(task, input);
|
||||
// Some OpenAI-compatible image/audio endpoints return the
|
||||
// generated media in the submit response rather than creating
|
||||
// a pollable job. Persist that result and settle the task in
|
||||
@@ -204,7 +222,7 @@ public class AsyncTaskService {
|
||||
continue;
|
||||
}
|
||||
OffsetDateTime submittedAt = now();
|
||||
jdbc.update("UPDATE model_tasks SET provider_task_id=?, provider_deadline_at=?, updated_at=? WHERE id=? AND status='running'", providerTaskId(upstream), submittedAt.plus(java.time.Duration.ofMillis(providerTimeoutMs)), submittedAt, taskId);
|
||||
jdbc.update("UPDATE model_tasks SET provider_task_id=?, provider_key_fingerprint=?, provider_deadline_at=?, updated_at=? WHERE id=? AND status='running'", providerTaskId(upstream), upstream.get("_moyiUpstreamKeyFingerprint"), submittedAt.plus(java.time.Duration.ofMillis(providerTimeoutMs)), submittedAt, taskId);
|
||||
} catch (RuntimeException exception) {
|
||||
String code = exception instanceof ApiException error ? error.getCode() : "UPSTREAM_TASK_SUBMIT_FAILED";
|
||||
String message = exception instanceof ApiException error ? error.getMessage() : "上游异步任务创建失败";
|
||||
@@ -221,7 +239,7 @@ public class AsyncTaskService {
|
||||
// workers reliably.
|
||||
jdbc.queryForList("SELECT pg_advisory_xact_lock(hashtextextended('moyi:async-dispatch', 0))");
|
||||
String sql = """
|
||||
SELECT t.id, t.api_key_id, m.provider_id, m.upstream_model, t.task_type, t.prompt, t.input
|
||||
SELECT t.id, t.user_id, t.api_key_id, t.model_id, m.provider_id, m.upstream_model, t.task_type, t.prompt, t.input
|
||||
FROM model_tasks t
|
||||
JOIN model_catalog m ON m.id=t.model_id
|
||||
JOIN model_providers p ON p.id=m.provider_id
|
||||
@@ -229,20 +247,32 @@ public class AsyncTaskService {
|
||||
AND t.provider_task_id IS NULL
|
||||
AND m.status='active' AND m.deleted_at IS NULL
|
||||
AND p.status='active' AND p.deleted_at IS NULL
|
||||
AND (SELECT COUNT(*) FROM model_tasks running_model
|
||||
WHERE running_model.model_id=t.model_id AND running_model.status='running')
|
||||
< COALESCE(m.max_concurrency, ?)
|
||||
AND (SELECT COUNT(*) FROM model_tasks running_key
|
||||
WHERE running_key.api_key_id=t.api_key_id AND running_key.status='running')
|
||||
< ?
|
||||
AND (SELECT count(*) FROM model_tasks ru WHERE ru.user_id=t.user_id AND ru.status='running') < coalesce((SELECT max_concurrency FROM gateway_user_policies up WHERE up.user_id=t.user_id),16)
|
||||
AND (
|
||||
? <= 0
|
||||
OR COALESCE(m.max_concurrency, ?) <= 0
|
||||
OR (SELECT COUNT(*) FROM model_tasks running_model
|
||||
WHERE running_model.model_id=t.model_id AND running_model.status='running')
|
||||
< COALESCE(m.max_concurrency, ?)
|
||||
)
|
||||
AND (
|
||||
? <= 0
|
||||
OR (SELECT COUNT(*) FROM model_tasks running_key
|
||||
WHERE running_key.api_key_id=t.api_key_id AND running_key.status='running')
|
||||
< ?
|
||||
)
|
||||
ORDER BY t.created_at
|
||||
LIMIT 1
|
||||
FOR UPDATE OF t SKIP LOCKED
|
||||
""";
|
||||
List<Map<String,Object>> rows = jdbc.queryForList(sql, defaultModelConcurrency, defaultKeyConcurrency);
|
||||
List<Map<String,Object>> rows = jdbc.queryForList(sql,
|
||||
modelConcurrencyEnabled ? 1 : 0, defaultModelConcurrency, defaultModelConcurrency,
|
||||
defaultKeyConcurrency, defaultKeyConcurrency);
|
||||
if (rows.isEmpty()) return null;
|
||||
Map<String,Object> row = rows.getFirst();
|
||||
jdbc.update("UPDATE model_tasks SET status='running', updated_at=now() WHERE id=? AND status='queued'", row.get("id"));
|
||||
if(!gatewayGuard.tryTaskUserSlot((UUID)row.get("api_key_id"),row.get("id").toString(),providerTimeoutMs+180000)) return null;
|
||||
try {jdbc.update("UPDATE model_tasks SET status='running', updated_at=now() WHERE id=? AND status='queued'", row.get("id"));}
|
||||
catch(RuntimeException e) {gatewayGuard.releaseTaskUserSlot((UUID)row.get("user_id"),row.get("id").toString());throw e;}
|
||||
return row;
|
||||
});
|
||||
}
|
||||
@@ -320,6 +350,20 @@ public class AsyncTaskService {
|
||||
return input;
|
||||
}
|
||||
|
||||
private Map<String,Object> submitProviderTask(Map<String,Object> task, Map<String,Object> input) {
|
||||
String providerId = String.valueOf(task.get("provider_id"));
|
||||
String routeId = String.valueOf(task.get("model_id"));
|
||||
String model = String.valueOf(task.get("upstream_model"));
|
||||
String type = String.valueOf(task.get("task_type"));
|
||||
// Keep the legacy overload for rows created before V30. Apart from
|
||||
// compatibility with existing integrations, this makes a queued
|
||||
// prompt-only task indistinguishable from the original submit path.
|
||||
if (input.size() == 1 && input.containsKey("prompt")) {
|
||||
return providerClient.submit(providerId, model, routeId, type, String.valueOf(input.get("prompt")));
|
||||
}
|
||||
return providerClient.submit(providerId, model, routeId, type, input);
|
||||
}
|
||||
|
||||
private OffsetDateTime now() { return OffsetDateTime.now(ZoneOffset.UTC); }
|
||||
|
||||
private String providerTaskId(Map<String,Object> response) {
|
||||
@@ -345,8 +389,12 @@ public class AsyncTaskService {
|
||||
}
|
||||
Map<String,Object> response;
|
||||
try {
|
||||
response = providerClient.poll(String.valueOf(row.get("provider_id")), String.valueOf(row.get("provider_task_id")));
|
||||
response = providerClient.poll(String.valueOf(row.get("provider_id")), String.valueOf(row.get("model_id")), String.valueOf(row.get("upstream_model")), String.valueOf(row.get("provider_task_id")), row.get("provider_key_fingerprint") == null ? null : String.valueOf(row.get("provider_key_fingerprint")));
|
||||
} catch (RuntimeException exception) {
|
||||
if (exception instanceof ApiException error && "UPSTREAM_KEY_NOT_AVAILABLE".equals(error.getCode())) {
|
||||
failTask(taskId, error.getCode(), error.getMessage());
|
||||
return;
|
||||
}
|
||||
// Even a permanent authentication/configuration error only proves
|
||||
// that polling failed, not that an already accepted job stopped.
|
||||
// Keep its hold and retry until the persisted deadline.
|
||||
@@ -461,7 +509,6 @@ public class AsyncTaskService {
|
||||
}
|
||||
|
||||
private void reserveWallet(UUID userId, BigDecimal amount) {
|
||||
if (amount.signum() == 0) return;
|
||||
jdbc.update("INSERT INTO user_wallets(user_id) VALUES (?) ON CONFLICT DO NOTHING", userId);
|
||||
Map<String,Object> wallet = jdbc.queryForMap("SELECT balance,reserved_balance FROM user_wallets WHERE user_id=? FOR UPDATE", userId);
|
||||
BigDecimal available = ((BigDecimal) wallet.get("balance")).subtract((BigDecimal) wallet.get("reserved_balance"));
|
||||
@@ -476,6 +523,8 @@ public class AsyncTaskService {
|
||||
private void settleTaskInTransaction(String taskId) {
|
||||
Map<String,Object> task = jdbc.queryForMap("SELECT user_id,reserved_amount,cost_amount FROM model_tasks WHERE id=? FOR UPDATE", taskId);
|
||||
BigDecimal amount = (BigDecimal) task.get("reserved_amount");
|
||||
policies.release(taskId);
|
||||
gatewayGuard.releaseTaskUserSlot((UUID)task.get("user_id"),taskId);
|
||||
if (amount == null || amount.signum() == 0 || task.get("cost_amount") != null) return;
|
||||
UUID userId = (UUID) task.get("user_id");
|
||||
Map<String,Object> wallet = jdbc.queryForMap("SELECT balance,reserved_balance FROM user_wallets WHERE user_id=? FOR UPDATE", userId);
|
||||
@@ -490,6 +539,8 @@ public class AsyncTaskService {
|
||||
private void releaseTaskInTransaction(String taskId) {
|
||||
Map<String,Object> task = jdbc.queryForMap("SELECT user_id,reserved_amount,cost_amount FROM model_tasks WHERE id=? FOR UPDATE", taskId);
|
||||
BigDecimal amount = (BigDecimal) task.get("reserved_amount");
|
||||
policies.release(taskId);
|
||||
gatewayGuard.releaseTaskUserSlot((UUID)task.get("user_id"),taskId);
|
||||
if (amount != null && amount.signum() > 0 && task.get("cost_amount") == null) {
|
||||
releaseWallet((UUID) task.get("user_id"), amount);
|
||||
jdbc.update("UPDATE model_tasks SET cost_amount=0 WHERE id=?", taskId);
|
||||
|
||||
+50
@@ -0,0 +1,50 @@
|
||||
package com.moyi.controlplane.usage;
|
||||
import com.moyi.controlplane.auth.*;
|
||||
import com.moyi.controlplane.common.ApiException;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import java.math.*;
|
||||
import java.util.*;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
@RestController @RequestMapping("/api/admin/operations")
|
||||
public class OperationsController {
|
||||
private final JdbcTemplate jdbc;private final AuthService auth;
|
||||
public OperationsController(JdbcTemplate jdbc,AuthService auth) {this.jdbc=jdbc;this.auth=auth;}
|
||||
@Scheduled(fixedDelayString="${moyi.ops.alert-interval-ms:60000}") @Transactional
|
||||
public void evaluate() {
|
||||
var rules=jdbc.queryForList("SELECT * FROM ops_alert_rules WHERE enabled=true FOR UPDATE SKIP LOCKED");
|
||||
for(var rule:rules) {
|
||||
var totals=jdbc.queryForMap("SELECT count(*) total,count(*) FILTER(WHERE status='failed') failed FROM usage_events WHERE status IN ('failed','succeeded') AND updated_at>now()-make_interval(mins=>?)",rule.get("window_minutes"));
|
||||
long total=((Number)totals.get("total")).longValue(),failed=((Number)totals.get("failed")).longValue();
|
||||
BigDecimal ratio=total==0?BigDecimal.ZERO:BigDecimal.valueOf(failed*100).divide(BigDecimal.valueOf(total),2,RoundingMode.HALF_UP);
|
||||
boolean firing=total>=((Number)rule.get("minimum_requests")).longValue()&&ratio.compareTo((BigDecimal)rule.get("error_percent"))>=0;
|
||||
if(!firing) {jdbc.update("UPDATE ops_alert_events SET status='resolved' WHERE rule_id=? AND status='open'",rule.get("id"));continue;}
|
||||
if(jdbc.update("UPDATE ops_alert_rules SET last_fired_at=now() WHERE id=? AND (last_fired_at IS NULL OR last_fired_at<now()-make_interval(mins=>cooldown_minutes))",rule.get("id"))==1)
|
||||
jdbc.update("INSERT INTO ops_alert_events(id,rule_id,total_requests,failed_requests,error_percent,notification_status,notification_email) VALUES (?,?,?,?,?,?,?)",UUID.randomUUID(),rule.get("id"),total,failed,ratio,Boolean.TRUE.equals(rule.get("notification_enabled"))?"pending":"disabled",rule.get("notification_email"));
|
||||
}
|
||||
}
|
||||
@GetMapping public Map<String,Object> overview(HttpServletRequest request) {
|
||||
auth.requireAdmin(AuthController.sessionToken(request));
|
||||
return Map.of("accounts",jdbc.queryForList("SELECT h.route_id,m.public_name,h.failures,h.last_status,h.latency_ms,h.cooldown_until,h.updated_at,substring(h.account_fingerprint,1,12) account FROM upstream_account_health h LEFT JOIN model_catalog m ON m.id=h.route_id ORDER BY h.updated_at DESC LIMIT 200"),"rules",jdbc.queryForList("SELECT * FROM ops_alert_rules ORDER BY id"),"alerts",jdbc.queryForList("SELECT * FROM ops_alert_events ORDER BY created_at DESC LIMIT 100"));
|
||||
}
|
||||
@PutMapping("/rules/{id}") @Transactional public Map<String,Object> save(@PathVariable String id,@RequestBody Map<String,Object> input,HttpServletRequest request) {
|
||||
var user=auth.requireAdmin(AuthController.sessionToken(request));
|
||||
String email=String.valueOf(input.getOrDefault("notificationEmail","")).strip();
|
||||
boolean notify=Boolean.TRUE.equals(input.get("notificationEnabled"));
|
||||
if(notify&&(email.length()>254||!email.matches("^[^\\s@,;<>]+@[^\\s@,;<>]+\\.[^\\s@,;<>]+$"))) throw new ApiException("INVALID_EMAIL","请输入有效的告警接收邮箱");
|
||||
int window=integer(input.get("windowMinutes"),1440),min=integer(input.get("minimumRequests"),1000000),cooldown=integer(input.get("cooldownMinutes"),1440);
|
||||
BigDecimal ratio;try {ratio=new BigDecimal(input.get("errorPercent").toString());if(ratio.signum()<0||ratio.compareTo(BigDecimal.valueOf(100))>0) throw new IllegalArgumentException();} catch(Exception e) {throw new ApiException("INVALID_RULE","错误率须为 0–100");}
|
||||
if(jdbc.update("UPDATE ops_alert_rules SET enabled=?,window_minutes=?,minimum_requests=?,error_percent=?,cooldown_minutes=?,updated_at=now() WHERE id=?",Boolean.TRUE.equals(input.get("enabled")),window,min,ratio,cooldown,id)!=1) throw new ApiException("RULE_NOT_FOUND","规则不存在",404);
|
||||
jdbc.update("UPDATE ops_alert_rules SET notification_enabled=?,notification_email=? WHERE id=?",notify,notify?email:null,id);
|
||||
if(!notify) jdbc.update("UPDATE ops_alert_events SET notification_status='disabled' WHERE rule_id=? AND notification_status IN ('pending','retry')",id);
|
||||
auth.audit("ops.alert_rule_updated",user.id(),AuthController.meta(request),Map.of("ruleId",id,"settings",input));return Map.of("saved",true);
|
||||
}
|
||||
@PostMapping("/alerts/{id}/acknowledge") public Map<String,Object> acknowledge(@PathVariable UUID id,HttpServletRequest request) {
|
||||
var user=auth.requireAdmin(AuthController.sessionToken(request));jdbc.update("UPDATE ops_alert_events SET status='acknowledged',acknowledged_by=?,acknowledged_at=now() WHERE id=? AND status='open'",user.id(),id);
|
||||
auth.audit("ops.alert_acknowledged",user.id(),AuthController.meta(request),Map.of("alertId",id));return Map.of("saved",true);
|
||||
}
|
||||
private int integer(Object value,int max) {try {int n=new BigDecimal(value.toString()).intValueExact();if(n<1||n>max) throw new NumberFormatException();return n;} catch(Exception e) {throw new ApiException("INVALID_RULE","规则值须为 1–"+max+" 的整数");}}
|
||||
}
|
||||
+29
@@ -0,0 +1,29 @@
|
||||
package com.moyi.controlplane.usage;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.mail.SimpleMailMessage;
|
||||
import org.springframework.mail.javamail.JavaMailSenderImpl;
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
@Service
|
||||
public class OpsNotificationService {
|
||||
private final JdbcTemplate jdbc;private final Environment env;
|
||||
public OpsNotificationService(JdbcTemplate jdbc,Environment env) {this.jdbc=jdbc;this.env=env;}
|
||||
private String value(String name) {return env.getProperty(name,"").strip();}
|
||||
public boolean available() {return !value("MOYI_SMTP_HOST").isEmpty()&&!value("MOYI_SMTP_FROM").isEmpty();}
|
||||
@Scheduled(fixedDelayString="${moyi.ops.notification-interval-ms:60000}") @Transactional
|
||||
public void deliver() {
|
||||
if(!available()) return;
|
||||
var rows=jdbc.queryForList("SELECT * FROM ops_alert_events WHERE notification_status IN ('pending','retry') AND notification_attempts<3 AND (notification_next_at IS NULL OR notification_next_at<=now()) ORDER BY created_at LIMIT 5 FOR UPDATE SKIP LOCKED");
|
||||
for(var row:rows) {
|
||||
try {
|
||||
var mail=new JavaMailSenderImpl();mail.setHost(value("MOYI_SMTP_HOST"));mail.setPort(env.getProperty("MOYI_SMTP_PORT",Integer.class,465));mail.setUsername(value("MOYI_SMTP_USERNAME"));mail.setPassword(env.getProperty("MOYI_SMTP_PASSWORD",""));mail.setDefaultEncoding("UTF-8");
|
||||
var p=mail.getJavaMailProperties();p.setProperty("mail.smtp.auth",String.valueOf(!value("MOYI_SMTP_USERNAME").isEmpty()));p.setProperty("mail.smtp.ssl.enable",env.getProperty("MOYI_SMTP_SSL","true"));p.setProperty("mail.smtp.starttls.enable",env.getProperty("MOYI_SMTP_STARTTLS","false"));p.setProperty("mail.smtp.starttls.required",env.getProperty("MOYI_SMTP_STARTTLS","false"));p.setProperty("mail.smtp.ssl.checkserveridentity","true");
|
||||
for(String name:new String[]{"connectiontimeout","timeout","writetimeout"}) p.setProperty("mail.smtp."+name,"5000");
|
||||
var message=new SimpleMailMessage();message.setFrom(value("MOYI_SMTP_FROM"));message.setTo(row.get("notification_email").toString());message.setSubject("MOYI 网关错误率告警");message.setText("告警编号:"+row.get("id")+"\n失败请求:"+row.get("failed_requests")+" / "+row.get("total_requests")+"\n错误率:"+row.get("error_percent")+"%\n请登录管理端的渠道健康与告警查看详情。");mail.send(message);
|
||||
jdbc.update("UPDATE ops_alert_events SET notification_status='sent',notification_attempts=notification_attempts+1 WHERE id=?",row.get("id"));
|
||||
} catch(Exception ignored) {jdbc.update("UPDATE ops_alert_events SET notification_status=CASE WHEN notification_attempts>=2 THEN 'failed' ELSE 'retry' END,notification_attempts=notification_attempts+1,notification_next_at=now()+interval '10 minutes' WHERE id=?",row.get("id"));}
|
||||
}
|
||||
}
|
||||
}
|
||||
+11
-4
@@ -65,6 +65,10 @@ public class UsageAuditService {
|
||||
int latency, Integer firstTokenMs, java.math.BigDecimal cost, Object output, String code, String message) {
|
||||
jdbc.update("UPDATE usage_events SET status=?,input_units=?,output_units=?,cached_input_units=?,latency_ms=?,first_token_ms=?,cost_amount=?,output_payload=?::jsonb,error_code=?,error_message=?,updated_at=now() WHERE id=?",
|
||||
status,tokens.prompt(),tokens.completion(),tokens.cached(),latency,firstTokenMs,cost,json(output),code,message,id);
|
||||
jdbc.update("UPDATE usage_events SET cache_write_units=? WHERE id=?",tokens.cacheWrite5m()+tokens.cacheWrite1h(),id);
|
||||
}
|
||||
public void observeUsage(UUID id,String protocol,Object observed) {
|
||||
jdbc.update("UPDATE usage_events SET observed_usage=?::jsonb WHERE id=?",json(Map.of("protocol",protocol,"usage",observed)),id);
|
||||
}
|
||||
|
||||
public void recordCall(
|
||||
@@ -129,6 +133,7 @@ public class UsageAuditService {
|
||||
"LEFT JOIN usage_events ue ON ue.deleted_at IS NULL AND ue.created_at >= day AND ue.created_at < day + INTERVAL '1 day' " +
|
||||
"GROUP BY day ORDER BY day",
|
||||
(rs, row) -> Map.of("date", rs.getString("day"), "count", rs.getLong("count"))));
|
||||
boolean modelGuardEnabled = gatewayGuard == null || gatewayGuard.modelConcurrencyEnabled();
|
||||
int defaultModelLimit = gatewayGuard == null ? 4 : gatewayGuard.defaultModelConcurrency();
|
||||
List<Map<String, Object>> groups = jdbc.query("""
|
||||
WITH pending AS (
|
||||
@@ -138,7 +143,8 @@ public class UsageAuditService {
|
||||
FROM model_tasks WHERE status IN ('queued','running') GROUP BY model_id
|
||||
)
|
||||
SELECT m.id, m.public_name, m.source_label, p.name AS provider_name, m.call_mode,
|
||||
COALESCE(NULLIF(m.max_concurrency, 0), ?) AS max_concurrency,
|
||||
CASE WHEN m.max_concurrency = 0 THEN 0
|
||||
ELSE COALESCE(m.max_concurrency, ?) END AS max_concurrency,
|
||||
(m.status='active' AND m.deleted_at IS NULL AND p.status='active' AND p.deleted_at IS NULL
|
||||
AND d.status='active' AND d.deleted_at IS NULL) AS route_enabled,
|
||||
COALESCE(pending.running, 0) AS running, COALESCE(pending.queued, 0) AS queued
|
||||
@@ -155,7 +161,7 @@ public class UsageAuditService {
|
||||
group.put("providerName", rs.getString("provider_name"));
|
||||
group.put("mode", rs.getString("call_mode"));
|
||||
group.put("routeEnabled", rs.getBoolean("route_enabled"));
|
||||
group.put("limit", rs.getInt("max_concurrency"));
|
||||
group.put("limit", modelGuardEnabled ? rs.getInt("max_concurrency") : 0);
|
||||
group.put("running", rs.getLong("running"));
|
||||
group.put("queued", rs.getLong("queued"));
|
||||
return group;
|
||||
@@ -179,6 +185,7 @@ public class UsageAuditService {
|
||||
"queueWaitMs", gatewayGuard == null ? 30000 : gatewayGuard.queueWaitMs(),
|
||||
"defaultModelConcurrency", defaultModelLimit,
|
||||
"defaultKeyConcurrency", gatewayGuard == null ? 4 : gatewayGuard.defaultKeyConcurrency(),
|
||||
"modelConcurrencyGuardEnabled", gatewayGuard == null || gatewayGuard.modelConcurrencyEnabled(),
|
||||
"asyncQueueLimit", asyncQueueLimit,
|
||||
"asyncWorkerIntervalMs", asyncWorkerIntervalMs,
|
||||
"synchronousSnapshotAvailable", snapshot.available()
|
||||
@@ -390,8 +397,8 @@ public class UsageAuditService {
|
||||
ObjectNode result = objectMapper.createObjectNode();
|
||||
node.fields().forEachRemaining(entry -> {
|
||||
String key = entry.getKey().toLowerCase();
|
||||
boolean usageField = java.util.Set.of("prompt_tokens", "completion_tokens", "total_tokens", "cached_tokens", "reasoning_tokens", "audio_tokens", "accepted_prediction_tokens", "rejected_prediction_tokens", "prompt_cache_hit_tokens", "prompt_cache_miss_tokens", "max_tokens", "max_completion_tokens").contains(key) && entry.getValue().isNumber();
|
||||
boolean usageDetails = java.util.Set.of("prompt_tokens_details", "completion_tokens_details").contains(key) && entry.getValue().isObject();
|
||||
boolean usageField = java.util.Set.of("prompt_tokens", "completion_tokens", "total_tokens", "cached_tokens", "reasoning_tokens", "audio_tokens", "accepted_prediction_tokens", "rejected_prediction_tokens", "prompt_cache_hit_tokens", "prompt_cache_miss_tokens", "max_tokens", "max_completion_tokens", "input_tokens", "output_tokens", "cache_read_input_tokens", "cache_creation_input_tokens", "ephemeral_5m_input_tokens", "ephemeral_1h_input_tokens", "prompttokencount", "candidatestokencount", "thoughtstokencount", "cachedcontenttokencount").contains(key) && entry.getValue().isNumber();
|
||||
boolean usageDetails = java.util.Set.of("prompt_tokens_details", "completion_tokens_details", "input_tokens_details", "output_tokens_details").contains(key) && entry.getValue().isObject();
|
||||
if (key.contains("authorization") || key.contains("api_key") || key.contains("apikey") || (key.contains("token") && !usageField && !usageDetails) || key.contains("secret") || key.contains("password")) result.put(entry.getKey(), "[REDACTED]");
|
||||
else result.set(entry.getKey(), sanitize(entry.getValue()));
|
||||
});
|
||||
|
||||
+30
-6
@@ -18,13 +18,30 @@ public class ChatBillingService {
|
||||
private final JdbcTemplate jdbc;
|
||||
private final ObjectMapper json;
|
||||
private final UsageAuditService usage;
|
||||
public ChatBillingService(JdbcTemplate jdbc, ObjectMapper json, UsageAuditService usage) { this.jdbc=jdbc; this.json=json; this.usage=usage; }
|
||||
private final com.moyi.controlplane.gateway.EnterprisePolicies policies;
|
||||
public ChatBillingService(JdbcTemplate jdbc, ObjectMapper json, UsageAuditService usage) { this.jdbc=jdbc; this.json=json; this.usage=usage; this.policies=new com.moyi.controlplane.gateway.EnterprisePolicies(jdbc,json); }
|
||||
public record Ticket(UUID id, UUID userId, ChatMeter.Rates rates) {}
|
||||
public void endpoint(UUID id,String endpoint) {jdbc.update("UPDATE usage_events SET endpoint=? WHERE id=?",endpoint,id);}
|
||||
public void observedUsage(Ticket ticket,String protocol,Object observed) {
|
||||
if(observed==null) return;
|
||||
usage.observeUsage(ticket.id(),protocol,observed);
|
||||
}
|
||||
@Transactional public void actualRoute(Ticket ticket,Map<String,Object> route) {
|
||||
var snapshots=jdbc.queryForList("SELECT procurement_pricing FROM model_price_history WHERE model_id=? ORDER BY id DESC LIMIT 1",route.get("id"));
|
||||
Object cost=snapshots.isEmpty()?null:snapshots.getFirst().get("procurement_pricing");
|
||||
if(cost!=null) ChatMeter.rates(decode(cost.toString()),false);
|
||||
jdbc.update("UPDATE chat_billing_requests SET procurement_pricing=?::jsonb WHERE id=? AND status='pending'",cost==null?null:cost.toString(),ticket.id());
|
||||
jdbc.update("UPDATE usage_events SET actual_route_id=? WHERE id=?",route.get("id"),ticket.id());
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Ticket reserve(Map<String,Object> key, Map<String,Object> model, ChatMeter.Request request, String idempotencyKey) {
|
||||
UUID userId=UUID.fromString(key.get("userId").toString());
|
||||
ChatMeter.Rates rates=ChatMeter.rates(model.get("pricing"),"provider-mock".equals(model.get("providerId")));
|
||||
var versions=jdbc.queryForList("SELECT id,pricing,procurement_pricing FROM model_price_history WHERE model_id=? ORDER BY id DESC LIMIT 1",model.get("id"));
|
||||
var version=versions.isEmpty()?Map.<String,Object>of():versions.getFirst();
|
||||
Object retail=versions.isEmpty()?model.get("pricing"):(version.get("pricing")==null?null:decode(version.get("pricing").toString()));
|
||||
ChatMeter.Rates rates=ChatMeter.rates(retail,"provider-mock".equals(model.get("providerId")));
|
||||
if(version.get("procurement_pricing")!=null) ChatMeter.rates(decode(version.get("procurement_pricing").toString()),false);
|
||||
String requestHash=hash(encode(new TreeMap<>(request.body())));
|
||||
String idem=null;
|
||||
if(idempotencyKey!=null) {
|
||||
@@ -34,17 +51,17 @@ public class ChatBillingService {
|
||||
var previous=jdbc.queryForList("SELECT id, request_hash FROM chat_billing_requests WHERE user_id=? AND idempotency_hash=?",userId,idem);
|
||||
if(!previous.isEmpty()) throw new ApiException(requestHash.equals(previous.getFirst().get("request_hash"))?"DUPLICATE_REQUEST":"IDEMPOTENCY_CONFLICT","该请求编号已受理,请在使用记录中查看结果;不会重复调用或扣费",409);
|
||||
}
|
||||
BigDecimal hold=rates.input().max(rates.cachedInput()).multiply(BigDecimal.valueOf(request.reservedInputTokens()))
|
||||
.add(rates.output().multiply(BigDecimal.valueOf(request.maxOutputTokens())))
|
||||
.divide(new BigDecimal("1000000"),8,java.math.RoundingMode.CEILING);
|
||||
BigDecimal hold=rates.hold(request.reservedInputTokens(),request.maxOutputTokens());
|
||||
jdbc.update("INSERT INTO user_wallets(user_id) VALUES (?) ON CONFLICT DO NOTHING",userId);
|
||||
var wallet=jdbc.queryForMap("SELECT balance,reserved_balance FROM user_wallets WHERE user_id=? FOR UPDATE",userId);
|
||||
BigDecimal available=((BigDecimal)wallet.get("balance")).subtract((BigDecimal)wallet.get("reserved_balance"));
|
||||
if(available.compareTo(hold)<0) throw new ApiException("INSUFFICIENT_BALANCE","可用余额不足以预留本次调用费用,请充值或调低输出 Token 上限",402);
|
||||
UUID id=UUID.randomUUID();
|
||||
policies.reserve(id.toString(),userId,UUID.fromString(key.get("id").toString()),hold);
|
||||
usage.startChat(id,userId,UUID.fromString(key.get("id").toString()),model,request.body());
|
||||
// Longer than the upstream hard deadline and the concurrency lease, including cleanup margin.
|
||||
jdbc.update("INSERT INTO chat_billing_requests(id,user_id,idempotency_hash,request_hash,reserved_amount,pricing,expires_at) VALUES (?,?,?,?,?,?::jsonb,now()+interval '5 minutes')",id,userId,idem,requestHash,hold,encode(rates.snapshot()));
|
||||
jdbc.update("UPDATE chat_billing_requests SET procurement_pricing=?::jsonb,price_version=? WHERE id=?",version.get("procurement_pricing")==null?null:version.get("procurement_pricing").toString(),version.get("id"),id);
|
||||
jdbc.update("UPDATE user_wallets SET reserved_balance=reserved_balance+?,updated_at=now() WHERE user_id=?",hold,userId);
|
||||
return new Ticket(id,userId,rates);
|
||||
}
|
||||
@@ -57,13 +74,18 @@ public class ChatBillingService {
|
||||
var tokens=ChatMeter.tokens(response);
|
||||
// Read the persisted snapshot, not editable catalog prices or caller-supplied rates.
|
||||
ChatMeter.Rates rates=ChatMeter.rates(decode(call.get("pricing").toString()),false);
|
||||
BigDecimal cost=rates.cost(tokens.prompt(),tokens.completion(),tokens.cached());
|
||||
BigDecimal cost=rates.cost(tokens);
|
||||
BigDecimal before=jdbc.queryForObject("SELECT balance FROM user_wallets WHERE user_id=? FOR UPDATE",BigDecimal.class,ticket.userId());
|
||||
BigDecimal after=before.subtract(cost);
|
||||
jdbc.update("UPDATE user_wallets SET balance=?,reserved_balance=reserved_balance-?,updated_at=now() WHERE user_id=?",after,call.get("reserved_amount"),ticket.userId());
|
||||
if(cost.signum()>0) jdbc.update("INSERT INTO wallet_entries(id,user_id,request_id,amount,balance_before,balance_after,reason,operation,requested_amount) VALUES (?,?,?,?,?,?,?,'usage',?)",UUID.randomUUID(),ticket.userId(),ticket.id(),cost.negate(),before,after,"模型调用 · "+ticket.id(),cost);
|
||||
usage.finishChat(ticket.id(),"succeeded",tokens,latency,firstTokenMs,cost,response,null,null);
|
||||
if(call.get("procurement_pricing")!=null) {
|
||||
BigDecimal procurement=ChatMeter.rates(decode(call.get("procurement_pricing").toString()),false).cost(tokens);
|
||||
jdbc.update("UPDATE usage_events SET procurement_cost=?,profit_amount=?,cost_status='estimated' WHERE id=?",procurement,cost.subtract(procurement),ticket.id());
|
||||
}
|
||||
jdbc.update("UPDATE chat_billing_requests SET status='succeeded',cost_amount=? WHERE id=?",cost,ticket.id());
|
||||
policies.release(ticket.id().toString());
|
||||
return cost;
|
||||
}
|
||||
|
||||
@@ -76,6 +98,8 @@ public class ChatBillingService {
|
||||
jdbc.update("UPDATE user_wallets SET reserved_balance=reserved_balance-?,updated_at=now() WHERE user_id=?",call.get("reserved_amount"),call.get("user_id"));
|
||||
jdbc.update("UPDATE chat_billing_requests SET status='failed',cost_amount=0 WHERE id=?",id);
|
||||
usage.finishChat(id,"failed",new ChatMeter.Tokens(0,0,0),latency,null,BigDecimal.ZERO,null,code,message);
|
||||
jdbc.update("UPDATE usage_events SET cost_status='pending' WHERE id=?",id);
|
||||
policies.release(id.toString());
|
||||
}
|
||||
|
||||
@Scheduled(fixedDelayString="${moyi.billing.recovery-interval-ms:60000}")
|
||||
|
||||
@@ -27,6 +27,8 @@ server:
|
||||
address: ${CONTROL_PLANE_HOST:0.0.0.0}
|
||||
port: ${CONTROL_PLANE_PORT:8787}
|
||||
shutdown: graceful
|
||||
# Native Tomcat processing trusts only its configured internal proxies, never arbitrary clients.
|
||||
forward-headers-strategy: ${MOYI_FORWARD_HEADERS_STRATEGY:none}
|
||||
|
||||
management:
|
||||
endpoints:
|
||||
@@ -43,8 +45,15 @@ moyi:
|
||||
redis-prefix: ${MOYI_REDIS_KEY_PREFIX:moyi:}
|
||||
requests-per-minute: ${GATEWAY_REQUESTS_PER_MINUTE:60}
|
||||
reads-per-minute: ${GATEWAY_READS_PER_MINUTE:300}
|
||||
default-model-concurrency: ${GATEWAY_DEFAULT_MODEL_CONCURRENCY:4}
|
||||
# 0 disables the MOYI-side model-group cap so channel concurrency can be
|
||||
# measured directly. Set a positive value only when a provider confirms
|
||||
# a shared per-model limit.
|
||||
default-model-concurrency: ${GATEWAY_DEFAULT_MODEL_CONCURRENCY:0}
|
||||
default-key-concurrency: ${GATEWAY_DEFAULT_KEY_CONCURRENCY:4}
|
||||
# Temporary measurement switch. When true, skip MOYI model/group slots
|
||||
# while retaining the independent API-Key limit, RPM, billing and the
|
||||
# provider's own response/limit.
|
||||
disable-model-concurrency: ${GATEWAY_DISABLE_MODEL_CONCURRENCY:false}
|
||||
queue-wait-ms: ${GATEWAY_QUEUE_WAIT_MS:30000}
|
||||
queue-poll-ms: ${GATEWAY_QUEUE_POLL_MS:100}
|
||||
tasks:
|
||||
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
-- Tripo H-series routes accept either a text prompt or an image URL/file token.
|
||||
-- Keep the catalog metadata aligned with the async adapter so clients can
|
||||
-- expose the image input without guessing from the model type.
|
||||
UPDATE model_definitions
|
||||
SET input_types = '["text", "image"]'::jsonb,
|
||||
updated_at = now()
|
||||
WHERE model_key IN ('tripo-v3', 'tripo-h-v3-0', 'tripo-h-v2-5')
|
||||
AND deleted_at IS NULL;
|
||||
|
||||
UPDATE model_catalog
|
||||
SET input_types = '["text", "image"]'::jsonb,
|
||||
updated_at = now()
|
||||
WHERE provider_id = 'provider-vast'
|
||||
AND upstream_model IN ('v3.1-20260211', 'v3.0-20250812', 'v2.5-20250123')
|
||||
AND deleted_at IS NULL;
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
-- The original relay catalog seeds used max_concurrency = 1 as a temporary
|
||||
-- safety value. That value is a MOYI-side admission limit; it is not a
|
||||
-- channel/provider quota and made every group look serial even when the
|
||||
-- upstream account could process more requests.
|
||||
--
|
||||
-- 0 means "no MOYI model-group cap". The gateway still enforces each
|
||||
-- platform API-Key limit, request-rate limit, wallet/usage accounting, and
|
||||
-- the provider's own quota/rate limit. Administrators can still set an
|
||||
-- explicit positive cap for a route when a channel reports one.
|
||||
UPDATE model_catalog
|
||||
SET max_concurrency = 0,
|
||||
updated_at = now()
|
||||
WHERE deleted_at IS NULL
|
||||
AND max_concurrency = 1;
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
ALTER TABLE model_tasks
|
||||
ADD COLUMN IF NOT EXISTS provider_key_fingerprint varchar(64);
|
||||
|
||||
COMMENT ON COLUMN model_tasks.provider_key_fingerprint IS
|
||||
'SHA-256 fingerprint of the upstream credential selected for this async task; never stores the credential itself';
|
||||
+44
@@ -0,0 +1,44 @@
|
||||
-- Verified 2026-10-12: current source currency; purchase discount + 1 point.
|
||||
-- Exact old JSON guard preserves later manual edits. Does not change enabled status, credentials or balances.
|
||||
-- Dynamic, missing-source and extreme-increase rows are deliberately excluded.
|
||||
WITH correction(provider_code, upstream_model, old_pricing, new_pricing) AS (VALUES
|
||||
('apipro', 'deepseek-v4-flash', '{"unit":"CNY / 1M tokens","input":0.66924,"output":1.33848,"discount":6.5,"cachedInput":0.13572,"priceSource":"APIPro 模型广场","exchangeRate":7.2,"listPriceUsd":{"input":0.143,"output":0.286,"cachedInput":0.029},"discountLabel":"6.5折"}'::jsonb, '{"unit":"CNY / 1M tokens","input":2.005848,"output":6.017076,"discount":6.5,"priceSource":"APIPro 当前美元原价 · 2026-10-12","exchangeRate":7.2,"discountLabel":"6.5折","cachedInput":0.2005848,"listPriceUsd":{"input":0.4286,"output":1.2856999999999206,"cachedInput":0.04286}}'::jsonb),
|
||||
('apipro', 'deepseek-v4-pro', '{"unit":"CNY / 1M tokens","input":8.0262,"output":16.04772,"discount":6.5,"cachedInput":0.66924,"priceSource":"APIPro 模型广场","exchangeRate":7.2,"listPriceUsd":{"input":1.715,"output":3.429,"cachedInput":0.143},"discountLabel":"6.5折"}'::jsonb, '{"unit":"CNY / 1M tokens","input":8.0262,"output":16.04772,"discount":6.5,"priceSource":"APIPro 当前美元原价 · 2026-10-12","exchangeRate":7.2,"discountLabel":"6.5折","cachedInput":0.66924,"listPriceUsd":{"input":1.715,"output":3.429000000000015,"cachedInput":0.14299999999957}}'::jsonb),
|
||||
('apipro', 'kimi-k3', '{"unit":"CNY / 1M tokens","input":13.371696,"output":66.857076,"discount":6.5,"cachedInput":1.337076,"priceSource":"APIPro 模型广场","exchangeRate":7.2,"listPriceUsd":{"input":2.8572,"output":14.2857,"cachedInput":0.2857},"discountLabel":"6.5折"}'::jsonb, '{"unit":"CNY / 1M tokens","input":13.371696,"output":66.857076,"discount":6.5,"priceSource":"APIPro 当前美元原价 · 2026-10-12","exchangeRate":7.2,"discountLabel":"6.5折","cachedInput":1.337076,"listPriceUsd":{"input":2.8572,"output":14.28570000000012,"cachedInput":0.2856999996}}'::jsonb),
|
||||
('apihub', 'kimi-k3', '{"unit":"CNY / 1M tokens","input":40.76352,"output":203.8176,"discount":2.8,"priceSource":"芯化兰德报价_2026.09.09.xlsx","exchangeRate":7.2,"listPriceUsd":{"input":20.22,"output":101.1},"discountLabel":"2.8折"}'::jsonb, '{"unit":"CNY / 1M tokens","input":5.6,"output":28.0,"discount":2.8,"priceSource":"ApiHub 当前人民币原价 · 2026-10-12","exchangeRate":1.0,"discountLabel":"2.8折","listPriceCny":{"input":20.0,"output":100.0}}'::jsonb),
|
||||
('tokenfoundryx', 'claude-fable-5-1', '{"unit":"CNY / 1M tokens","input":36,"output":180,"discount":5,"cachedInput":0.9,"priceSource":"芯化兰德报价_2026.09.09.xlsx","exchangeRate":7.2,"listPriceUsd":{"input":10,"output":50,"cachedInput":0.25},"discountLabel":"5.0折"}'::jsonb, '{"unit":"CNY / 1M tokens","input":36.0,"output":180.0,"discount":5.0,"priceSource":"TokenFoundryX 当前美元原价 · 2026-10-12","exchangeRate":7.2,"discountLabel":"5折","cachedInput":3.6,"listPriceUsd":{"input":10.0,"output":50.0,"cachedInput":1.0}}'::jsonb),
|
||||
('apipro', 'claude-opus-5', '{"unit":"CNY / 1M tokens","input":20.16,"output":100.8,"discount":5.6,"cachedInput":1.008,"priceSource":"APIPro 模型广场","exchangeRate":7.2,"listPriceUsd":{"input":5,"output":25,"cachedInput":0.25},"discountLabel":"5.6折"}'::jsonb, '{"unit":"CNY / 1M tokens","input":20.16,"output":100.8,"discount":5.6,"priceSource":"APIPro 当前美元原价 · 2026-10-12","exchangeRate":7.2,"discountLabel":"5.6折","cachedInput":2.016,"listPriceUsd":{"input":5.0,"output":25.0,"cachedInput":0.5}}'::jsonb),
|
||||
('apipro', 'deepseek-v3', '{"unit":"CNY / 1M tokens","input":1.34215848,"output":5.368428,"discount":6.5,"priceSource":"APIPro 模型广场","exchangeRate":7.2,"listPriceUsd":{"input":0.286786,"output":1.1471},"discountLabel":"6.5折"}'::jsonb, '{"unit":"CNY / 1M tokens","input":1.34215714,"output":5.36862857,"discount":6.5,"priceSource":"APIPro 当前美元原价 · 2026-10-12","exchangeRate":7.2,"discountLabel":"6.5折","listPriceUsd":{"input":0.286785714,"output":1.147142856}}'::jsonb),
|
||||
('apipro', 'deepseek-v3-0324', '{"unit":"CNY / 1M tokens","input":1.34215848,"output":5.368428,"discount":6.5,"priceSource":"APIPro 模型广场","exchangeRate":7.2,"listPriceUsd":{"input":0.286786,"output":1.1471},"discountLabel":"6.5折"}'::jsonb, '{"unit":"CNY / 1M tokens","input":1.34215714,"output":5.36862857,"discount":6.5,"priceSource":"APIPro 当前美元原价 · 2026-10-12","exchangeRate":7.2,"discountLabel":"6.5折","listPriceUsd":{"input":0.286785714,"output":1.147142856}}'::jsonb),
|
||||
('apipro', 'deepseek-v3.1', '{"unit":"CNY / 1M tokens","input":2.68431228,"output":8.052876,"discount":6.5,"priceSource":"APIPro 模型广场","exchangeRate":7.2,"listPriceUsd":{"input":0.573571,"output":1.7207},"discountLabel":"6.5折"}'::jsonb, '{"unit":"CNY / 1M tokens","input":2.68431428,"output":8.05294285,"discount":6.5,"priceSource":"APIPro 当前美元原价 · 2026-10-12","exchangeRate":7.2,"discountLabel":"6.5折","listPriceUsd":{"input":0.573571428,"output":1.720714284}}'::jsonb),
|
||||
('apipro', 'deepseek-v3.2', '{"unit":"CNY / 1M tokens","input":1.34215848,"output":2.01323772,"discount":6.5,"priceSource":"APIPro 模型广场","exchangeRate":7.2,"listPriceUsd":{"input":0.286786,"output":0.430179},"discountLabel":"6.5折"}'::jsonb, '{"unit":"CNY / 1M tokens","input":1.34215714,"output":2.01323571,"discount":6.5,"priceSource":"APIPro 当前美元原价 · 2026-10-12","exchangeRate":7.2,"discountLabel":"6.5折","listPriceUsd":{"input":0.286785714,"output":0.430178571}}'::jsonb),
|
||||
('apipro', 'deepseek-v4-flash-0731', '{"unit":"CNY / 1M tokens","input":0.66924,"output":1.33848,"discount":6.5,"cachedInput":0.13572,"priceSource":"APIPro 模型广场","exchangeRate":7.2,"listPriceUsd":{"input":0.143,"output":0.286,"cachedInput":0.029},"discountLabel":"6.5折"}'::jsonb, '{"unit":"CNY / 1M tokens","input":2.005848,"output":6.017076,"discount":6.5,"priceSource":"APIPro 当前美元原价 · 2026-10-12","exchangeRate":7.2,"discountLabel":"6.5折","cachedInput":0.2005848,"listPriceUsd":{"input":0.4286,"output":1.2856999999999206,"cachedInput":0.04286}}'::jsonb),
|
||||
('apipro', 'gemini-2.5-flash', '{"unit":"CNY / 1M tokens","input":1.2096,"output":10.08,"discount":5.6,"priceSource":"APIPro 模型广场","exchangeRate":7.2,"listPriceUsd":{"input":0.3,"output":2.5},"discountLabel":"5.6折"}'::jsonb, '{"unit":"CNY / 1M tokens","input":1.2096,"output":10.08,"discount":5.6,"priceSource":"APIPro 当前美元原价 · 2026-10-12","exchangeRate":7.2,"discountLabel":"5.6折","listPriceUsd":{"input":0.3,"output":2.4999999999}}'::jsonb),
|
||||
('tokenfoundryx', 'gemini-2.5-flash-lite', '{"unit":"CNY / 1M tokens","input":540,"output":2160,"cachedInput":null,"priceSource":"TokenFoundryX 模型广场","exchangeRate":7.2,"listPriceUsd":{"input":75,"output":300},"discountLabel":"未提供"}'::jsonb, '{"unit":"CNY / 1M tokens","input":189.0,"output":756.0,"priceSource":"TokenFoundryX 当前美元原价 · 2026-10-12","exchangeRate":7.2,"discountLabel":"3.5折","discount":3.5,"listPriceUsd":{"input":75.0,"output":300.0}}'::jsonb),
|
||||
('tokenfoundryx', 'gemini-2.5-pro', '{"unit":"CNY / 1M tokens","input":540,"output":4320,"cachedInput":null,"priceSource":"TokenFoundryX 模型广场","exchangeRate":7.2,"listPriceUsd":{"input":75,"output":600},"discountLabel":"未提供"}'::jsonb, '{"unit":"CNY / 1M tokens","input":3.15,"output":25.2,"priceSource":"TokenFoundryX 当前美元原价 · 2026-10-12","exchangeRate":7.2,"discountLabel":"3.5折","discount":3.5,"cachedInput":0.315,"listPriceUsd":{"input":1.25,"output":10.0,"cachedInput":0.125}}'::jsonb),
|
||||
('apipro', 'gemini-3.5-flash-lite', '{"unit":"CNY / 1M tokens","input":1.2096,"output":10.08,"discount":5.6,"priceSource":"APIPro 模型广场","exchangeRate":7.2,"listPriceUsd":{"input":0.3,"output":2.5},"discountLabel":"5.6折"}'::jsonb, '{"unit":"CNY / 1M tokens","input":1.2096,"output":10.08,"discount":5.6,"priceSource":"APIPro 当前美元原价 · 2026-10-12","exchangeRate":7.2,"discountLabel":"5.6折","listPriceUsd":{"input":0.3,"output":2.4999999999999}}'::jsonb),
|
||||
('tokenfoundryx', 'gemini-3.7-flash', '{"unit":"CNY / 1M tokens","input":5.4,"output":27,"cachedInput":0.54,"priceSource":"TokenFoundryX 模型广场","exchangeRate":7.2,"listPriceUsd":{"input":0.75,"output":3.75,"cachedInput":0.075},"discountLabel":"未提供"}'::jsonb, '{"unit":"CNY / 1M tokens","input":1.89,"output":9.45,"priceSource":"TokenFoundryX 当前美元原价 · 2026-10-12","exchangeRate":7.2,"discountLabel":"3.5折","discount":3.5,"cachedInput":0.189,"listPriceUsd":{"input":0.75,"output":3.75,"cachedInput":0.075}}'::jsonb),
|
||||
('tokenfoundryx', 'gemini-3.8-flash', '{"unit":"CNY / 1M tokens","input":5.4,"output":27,"cachedInput":0.54,"priceSource":"TokenFoundryX 模型广场","exchangeRate":7.2,"listPriceUsd":{"input":0.75,"output":3.75,"cachedInput":0.075},"discountLabel":"未提供"}'::jsonb, '{"unit":"CNY / 1M tokens","input":1.89,"output":9.45,"priceSource":"TokenFoundryX 当前美元原价 · 2026-10-12","exchangeRate":7.2,"discountLabel":"3.5折","discount":3.5,"cachedInput":0.189,"listPriceUsd":{"input":0.75,"output":3.75,"cachedInput":0.075}}'::jsonb),
|
||||
('apihub', 'glm-4.7', '{"unit":"CNY / 1M tokens","input":1.3392,"output":4.9104,"discount":3.1,"cachedInput":0.24552,"priceSource":"芯化兰德报价_2026.09.09.xlsx","exchangeRate":7.2,"listPriceUsd":{"input":0.6,"output":2.2,"cachedInput":0.11},"discountLabel":"3.1折"}'::jsonb, '{"unit":"CNY / 1M tokens","input":0.62,"output":2.48,"discount":3.1,"priceSource":"ApiHub 当前人民币原价 · 2026-10-12","exchangeRate":1.0,"discountLabel":"3.1折","cachedInput":0.248,"listPriceCny":{"input":2.0,"output":8.0,"cachedInput":0.8}}'::jsonb),
|
||||
('apihub', 'glm-5', '{"unit":"CNY / 1M tokens","input":8.86104,"output":39.821112,"discount":3.1,"cachedInput":1.77444,"priceSource":"芯化兰德报价_2026.09.09.xlsx","exchangeRate":7.2,"listPriceUsd":{"input":3.97,"output":17.841,"cachedInput":0.795},"discountLabel":"3.1折"}'::jsonb, '{"unit":"CNY / 1M tokens","input":1.24,"output":5.58,"discount":3.1,"priceSource":"ApiHub 当前人民币原价 · 2026-10-12","exchangeRate":1.0,"discountLabel":"3.1折","cachedInput":0.31,"listPriceCny":{"input":4.0,"output":18.0,"cachedInput":1.0}}'::jsonb),
|
||||
('apihub', 'glm-5.1', '{"unit":"CNY / 1M tokens","input":13.237992,"output":52.9542,"discount":3.1,"cachedInput":1.323576,"priceSource":"芯化兰德报价_2026.09.09.xlsx","exchangeRate":7.2,"listPriceUsd":{"input":5.931,"output":23.725,"cachedInput":0.593},"discountLabel":"3.1折"}'::jsonb, '{"unit":"CNY / 1M tokens","input":1.86,"output":7.44,"discount":3.1,"priceSource":"ApiHub 当前人民币原价 · 2026-10-12","exchangeRate":1.0,"discountLabel":"3.1折","cachedInput":0.403,"listPriceCny":{"input":6.0,"output":24.0,"cachedInput":1.3}}'::jsonb),
|
||||
('apipro', 'glm-5.2', '{"unit":"CNY / 1M tokens","input":5.348304,"output":18.72,"discount":6.5,"cachedInput":1.337076,"priceSource":"APIPro 模型广场","exchangeRate":7.2,"listPriceUsd":{"input":1.1428,"output":4,"cachedInput":0.2857},"discountLabel":"6.5折"}'::jsonb, '{"unit":"CNY / 1M tokens","input":5.348304,"output":18.72,"discount":6.5,"priceSource":"APIPro 当前美元原价 · 2026-10-12","exchangeRate":7.2,"discountLabel":"6.5折","cachedInput":1.337076,"listPriceUsd":{"input":1.1428,"output":3.9999999999995,"cachedInput":0.2857}}'::jsonb),
|
||||
('apihub', 'glm-5.2', '{"unit":"CNY / 1M tokens","input":17.28,"output":60.56208,"discount":3,"cachedInput":4.32432,"priceSource":"芯化兰德报价_2026.09.09.xlsx","exchangeRate":7.2,"listPriceUsd":{"input":8,"output":28.038,"cachedInput":2.002},"discountLabel":"3.0折"}'::jsonb, '{"unit":"CNY / 1M tokens","input":2.4,"output":8.4,"discount":3.0,"priceSource":"ApiHub 当前人民币原价 · 2026-10-12","exchangeRate":1.0,"discountLabel":"3折","cachedInput":0.705,"listPriceCny":{"input":8.0,"output":28.0,"cachedInput":2.35}}'::jsonb),
|
||||
('apipro', 'glm-5.3', '{"unit":"CNY / 1M tokens","input":5.348304,"output":18.72,"discount":6.5,"cachedInput":1.337076,"priceSource":"APIPro 模型广场","exchangeRate":7.2,"listPriceUsd":{"input":1.1428,"output":4,"cachedInput":0.2857},"discountLabel":"6.5折"}'::jsonb, '{"unit":"CNY / 1M tokens","input":5.348304,"output":18.72,"discount":6.5,"priceSource":"APIPro 当前美元原价 · 2026-10-12","exchangeRate":7.2,"discountLabel":"6.5折","cachedInput":1.337076,"listPriceUsd":{"input":1.1428,"output":3.9999999999995,"cachedInput":0.2857}}'::jsonb),
|
||||
('apihub', 'glm-5.3-flash', '{"unit":"CNY / 1M tokens","input":5.76,"output":20.16,"cachedInput":1.656,"priceSource":"ApiHub 模型广场","exchangeRate":7.2,"listPriceUsd":{"input":0.8,"output":2.8,"cachedInput":0.23},"discountLabel":"未提供"}'::jsonb, '{"unit":"CNY / 1M tokens","input":0.24,"output":0.84,"priceSource":"ApiHub 当前人民币原价 · 2026-10-12","exchangeRate":1.0,"discountLabel":"3折","discount":3.0,"cachedInput":0.069,"listPriceCny":{"input":0.8,"output":2.8,"cachedInput":0.23}}'::jsonb),
|
||||
('tokenfoundryx', 'gpt-5.4', '{"unit":"CNY / 1M tokens","input":128.34,"output":770.04,"cachedInput":12.6,"priceSource":"TokenFoundryX 模型广场","exchangeRate":7.2,"listPriceUsd":{"input":17.825,"output":106.95,"cachedInput":1.75},"discountLabel":"未提供"}'::jsonb, '{"unit":"CNY / 1M tokens","input":34.6518,"output":207.9108,"priceSource":"TokenFoundryX 当前美元原价 · 2026-10-12","exchangeRate":7.2,"discountLabel":"2.7折","discount":2.7,"cachedInput":3.40200977,"listPriceUsd":{"input":17.825,"output":106.95,"cachedInput":1.750005025}}'::jsonb),
|
||||
('tokenfoundryx', 'gpt-5.4-mini', '{"unit":"CNY / 1M tokens","input":37.8,"output":226.8,"cachedInput":3.78,"priceSource":"TokenFoundryX 模型广场","exchangeRate":7.2,"listPriceUsd":{"input":5.25,"output":31.5,"cachedInput":0.525},"discountLabel":"未提供"}'::jsonb, '{"unit":"CNY / 1M tokens","input":10.206,"output":61.236,"priceSource":"TokenFoundryX 当前美元原价 · 2026-10-12","exchangeRate":7.2,"discountLabel":"2.7折","discount":2.7,"cachedInput":1.0206,"listPriceUsd":{"input":5.25,"output":31.5,"cachedInput":0.525}}'::jsonb),
|
||||
('tokenfoundryx', 'gpt-5.5', '{"unit":"CNY / 1M tokens","input":36,"output":216,"cachedInput":3.6,"priceSource":"TokenFoundryX 模型广场","exchangeRate":7.2,"listPriceUsd":{"input":5,"output":30,"cachedInput":0.5},"discountLabel":"未提供"}'::jsonb, '{"unit":"CNY / 1M tokens","input":9.72,"output":58.32,"priceSource":"TokenFoundryX 当前美元原价 · 2026-10-12","exchangeRate":7.2,"discountLabel":"2.7折","discount":2.7,"cachedInput":0.972,"listPriceUsd":{"input":5.0,"output":30.0,"cachedInput":0.5}}'::jsonb),
|
||||
('apipro', 'grok-3-mini', '{"unit":"CNY / 1M tokens","input":1.2096,"output":2.016,"discount":5.6,"priceSource":"APIPro 模型广场","exchangeRate":7.2,"listPriceUsd":{"input":0.3,"output":0.5},"discountLabel":"5.6折"}'::jsonb, '{"unit":"CNY / 1M tokens","input":1.2096,"output":2.016,"discount":5.6,"priceSource":"APIPro 当前美元原价 · 2026-10-12","exchangeRate":7.2,"discountLabel":"5.6折","listPriceUsd":{"input":0.3,"output":0.5000000000001}}'::jsonb),
|
||||
('apihub', 'kimi-k2.5', '{"unit":"CNY / 1M tokens","input":8.00352,"output":41.97312,"discount":2.8,"priceSource":"芯化兰德报价_2026.09.09.xlsx","exchangeRate":7.2,"listPriceUsd":{"input":3.97,"output":20.82},"discountLabel":"2.8折"}'::jsonb, '{"unit":"CNY / 1M tokens","input":1.12,"output":5.88,"discount":2.8,"priceSource":"ApiHub 当前人民币原价 · 2026-10-12","exchangeRate":1.0,"discountLabel":"2.8折","listPriceCny":{"input":4.0,"output":21.0}}'::jsonb),
|
||||
('apipro', 'kimi-k2.6', '{"unit":"CNY / 1M tokens","input":4.345848,"output":18.051228,"discount":6.5,"cachedInput":0.735228,"priceSource":"APIPro 模型广场","exchangeRate":7.2,"listPriceUsd":{"input":0.9286,"output":3.8571,"cachedInput":0.1571},"discountLabel":"6.5折"}'::jsonb, '{"unit":"CNY / 1M tokens","input":4.345848,"output":18.051228,"discount":6.5,"priceSource":"APIPro 当前美元原价 · 2026-10-12","exchangeRate":7.2,"discountLabel":"6.5折","cachedInput":0.735228,"listPriceUsd":{"input":0.9286,"output":3.857100000000277,"cachedInput":0.1570999999997104}}'::jsonb),
|
||||
('apihub', 'kimi-k2.6', '{"unit":"CNY / 1M tokens","input":12.989088,"output":53.95824,"discount":2.8,"priceSource":"芯化兰德报价_2026.09.09.xlsx","exchangeRate":7.2,"listPriceUsd":{"input":6.443,"output":26.765},"discountLabel":"2.8折"}'::jsonb, '{"unit":"CNY / 1M tokens","input":1.82,"output":7.56,"discount":2.8,"priceSource":"ApiHub 当前人民币原价 · 2026-10-12","exchangeRate":1.0,"discountLabel":"2.8折","cachedInput":0.308,"listPriceCny":{"input":6.5,"output":27.0,"cachedInput":1.1}}'::jsonb),
|
||||
('apihub', 'kimi-k2.7-code', '{"unit":"CNY / 1M tokens","input":18.236736,"output":75.757248,"discount":2.8,"cachedInput":3.64896,"priceSource":"芯化兰德报价_2026.09.09.xlsx","exchangeRate":7.2,"listPriceUsd":{"input":9.046,"output":37.578,"cachedInput":1.81},"discountLabel":"2.8折"}'::jsonb, '{"unit":"CNY / 1M tokens","input":1.82,"output":7.56,"discount":2.8,"priceSource":"ApiHub 当前人民币原价 · 2026-10-12","exchangeRate":1.0,"discountLabel":"2.8折","cachedInput":0.21560084,"listPriceCny":{"input":6.5,"output":27.0,"cachedInput":0.770003}}'::jsonb),
|
||||
('apihub', 'qwen3.6-plus', '{"unit":"CNY / 1M tokens","input":4.437216,"output":26.551872,"discount":3.1,"priceSource":"芯化兰德报价_2026.09.09.xlsx","exchangeRate":7.2,"listPriceUsd":{"input":1.988,"output":11.896},"discountLabel":"3.1折"}'::jsonb, '{"unit":"CNY / 1M tokens","input":0.62,"output":14.88,"discount":3.1,"priceSource":"ApiHub 当前人民币原价 · 2026-10-12","exchangeRate":1.0,"discountLabel":"3.1折","cachedInput":0.062,"listPriceCny":{"input":2.0,"output":48.0,"cachedInput":0.2}}'::jsonb),
|
||||
('apihub', 'qwen3.7-max', '{"unit":"CNY / 1M tokens","input":26.587584,"output":79.76052,"discount":3.1,"cachedInput":2.660544,"priceSource":"芯化兰德报价_2026.09.09.xlsx","exchangeRate":7.2,"listPriceUsd":{"input":11.912,"output":35.735,"cachedInput":1.192},"discountLabel":"3.1折"}'::jsonb, '{"unit":"CNY / 1M tokens","input":3.72,"output":11.16,"discount":3.1,"priceSource":"ApiHub 当前人民币原价 · 2026-10-12","exchangeRate":1.0,"discountLabel":"3.1折","cachedInput":0.744,"listPriceCny":{"input":12.0,"output":36.0,"cachedInput":2.4}}'::jsonb),
|
||||
('apipro', 'qwen3.8-max', '{"unit":"CNY / 1M tokens","input":8.022924,"output":24.068772,"discount":6.5,"cachedInput":1.002924,"priceSource":"APIPro 模型广场","exchangeRate":7.2,"listPriceUsd":{"input":1.7143,"output":5.1429,"cachedInput":0.2143},"discountLabel":"6.5折"}'::jsonb, '{"unit":"CNY / 1M tokens","input":8.022924,"output":24.068772,"discount":6.5,"priceSource":"APIPro 当前美元原价 · 2026-10-12","exchangeRate":7.2,"discountLabel":"6.5折","cachedInput":1.002924,"listPriceUsd":{"input":1.7143,"output":5.1429,"cachedInput":0.2143000000001658}}'::jsonb)
|
||||
)
|
||||
UPDATE model_catalog m SET pricing=c.new_pricing, updated_at=now()
|
||||
FROM model_providers p, correction c
|
||||
WHERE m.provider_id=p.id AND p.code=c.provider_code AND m.upstream_model=c.upstream_model
|
||||
AND m.deleted_at IS NULL AND p.deleted_at IS NULL AND m.pricing=c.old_pricing;
|
||||
+31
@@ -0,0 +1,31 @@
|
||||
ALTER TABLE model_catalog ADD COLUMN procurement_pricing jsonb;
|
||||
CREATE TABLE model_price_history (
|
||||
id bigserial PRIMARY KEY,
|
||||
model_id varchar(64) NOT NULL,
|
||||
pricing jsonb,
|
||||
procurement_pricing jsonb,
|
||||
created_at timestamptz NOT NULL DEFAULT now()
|
||||
);
|
||||
CREATE INDEX model_price_history_model_idx ON model_price_history(model_id,id DESC);
|
||||
INSERT INTO model_price_history(model_id,pricing) SELECT id,pricing FROM model_catalog;
|
||||
CREATE FUNCTION record_model_price_change() RETURNS trigger LANGUAGE plpgsql AS $$
|
||||
BEGIN
|
||||
IF TG_OP='INSERT' THEN
|
||||
INSERT INTO model_price_history(model_id,pricing,procurement_pricing) VALUES(NEW.id,NEW.pricing,NEW.procurement_pricing);
|
||||
ELSIF NEW.pricing IS DISTINCT FROM OLD.pricing OR NEW.procurement_pricing IS DISTINCT FROM OLD.procurement_pricing THEN
|
||||
INSERT INTO model_price_history(model_id,pricing,procurement_pricing) VALUES(NEW.id,NEW.pricing,NEW.procurement_pricing);
|
||||
END IF;
|
||||
RETURN NEW;
|
||||
END $$;
|
||||
CREATE TRIGGER model_price_version AFTER INSERT OR UPDATE ON model_catalog FOR EACH ROW EXECUTE FUNCTION record_model_price_change();
|
||||
|
||||
ALTER TABLE chat_billing_requests ADD COLUMN procurement_pricing jsonb;
|
||||
ALTER TABLE chat_billing_requests ADD COLUMN price_version bigint REFERENCES model_price_history(id);
|
||||
ALTER TABLE usage_events ADD COLUMN procurement_cost numeric(20,8) CHECK(procurement_cost>=0);
|
||||
ALTER TABLE usage_events ADD COLUMN profit_amount numeric(20,8);
|
||||
ALTER TABLE usage_events ADD COLUMN cost_status varchar(24) NOT NULL DEFAULT 'unknown'
|
||||
CHECK(cost_status IN ('unknown','estimated','pending','reconciled'));
|
||||
ALTER TABLE usage_events ADD COLUMN reconciliation_note text;
|
||||
ALTER TABLE usage_events ADD COLUMN reconciled_by uuid REFERENCES users(id);
|
||||
ALTER TABLE usage_events ADD COLUMN reconciled_at timestamptz;
|
||||
CREATE INDEX usage_cost_pending_idx ON usage_events(created_at) WHERE cost_status IN ('pending','unknown');
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
CREATE TABLE gateway_user_policies (
|
||||
user_id uuid PRIMARY KEY REFERENCES users(id),
|
||||
requests_per_minute integer NOT NULL DEFAULT 120 CHECK(requests_per_minute BETWEEN 1 AND 100000),
|
||||
max_concurrency integer NOT NULL DEFAULT 16 CHECK(max_concurrency BETWEEN 1 AND 10000),
|
||||
monthly_budget numeric(20,8) CHECK(monthly_budget >= 0),
|
||||
updated_at timestamptz NOT NULL DEFAULT now()
|
||||
);
|
||||
CREATE TABLE gateway_key_policies (
|
||||
key_id uuid PRIMARY KEY REFERENCES platform_api_keys(id),
|
||||
expires_at timestamptz,
|
||||
ip_allowlist jsonb NOT NULL DEFAULT '[]' CHECK(jsonb_typeof(ip_allowlist)='array'),
|
||||
total_budget numeric(20,8) CHECK(total_budget >= 0),
|
||||
daily_budget numeric(20,8) CHECK(daily_budget >= 0),
|
||||
monthly_budget numeric(20,8) CHECK(monthly_budget >= 0),
|
||||
updated_at timestamptz NOT NULL DEFAULT now()
|
||||
);
|
||||
CREATE TABLE gateway_budget_holds (
|
||||
request_id varchar(100) PRIMARY KEY,
|
||||
user_id uuid NOT NULL REFERENCES users(id),
|
||||
key_id uuid NOT NULL REFERENCES platform_api_keys(id),
|
||||
amount numeric(20,8) NOT NULL CHECK(amount >= 0),
|
||||
created_at timestamptz NOT NULL DEFAULT now()
|
||||
);
|
||||
CREATE INDEX gateway_budget_holds_user ON gateway_budget_holds(user_id);
|
||||
@@ -0,0 +1,16 @@
|
||||
CREATE TABLE user_totp (
|
||||
user_id uuid PRIMARY KEY REFERENCES users(id),
|
||||
secret_ciphertext text NOT NULL,
|
||||
enabled boolean NOT NULL DEFAULT false,
|
||||
pending_until timestamptz,
|
||||
last_step bigint NOT NULL DEFAULT -1,
|
||||
attempts integer NOT NULL DEFAULT 0,
|
||||
attempt_window timestamptz NOT NULL DEFAULT now(),
|
||||
updated_at timestamptz NOT NULL DEFAULT now()
|
||||
);
|
||||
CREATE TABLE user_totp_recovery (
|
||||
user_id uuid NOT NULL REFERENCES users(id),
|
||||
code_hash varchar(64) NOT NULL,
|
||||
used_at timestamptz,
|
||||
PRIMARY KEY(user_id,code_hash)
|
||||
);
|
||||
+34
@@ -0,0 +1,34 @@
|
||||
CREATE TABLE upstream_account_health (
|
||||
route_id varchar(64) NOT NULL,
|
||||
account_fingerprint varchar(64) NOT NULL,
|
||||
failures integer NOT NULL DEFAULT 0,
|
||||
last_status integer,
|
||||
latency_ms integer,
|
||||
cooldown_until timestamptz,
|
||||
updated_at timestamptz NOT NULL DEFAULT now(),
|
||||
PRIMARY KEY(route_id,account_fingerprint)
|
||||
);
|
||||
CREATE TABLE ops_alert_rules (
|
||||
id varchar(64) PRIMARY KEY,
|
||||
name varchar(120) NOT NULL,
|
||||
enabled boolean NOT NULL DEFAULT true,
|
||||
window_minutes integer NOT NULL DEFAULT 5 CHECK(window_minutes BETWEEN 1 AND 1440),
|
||||
minimum_requests integer NOT NULL DEFAULT 10 CHECK(minimum_requests BETWEEN 1 AND 1000000),
|
||||
error_percent numeric(5,2) NOT NULL DEFAULT 20 CHECK(error_percent BETWEEN 0 AND 100),
|
||||
cooldown_minutes integer NOT NULL DEFAULT 30 CHECK(cooldown_minutes BETWEEN 1 AND 1440),
|
||||
last_fired_at timestamptz,
|
||||
updated_at timestamptz NOT NULL DEFAULT now()
|
||||
);
|
||||
INSERT INTO ops_alert_rules(id,name) VALUES ('gateway-errors','网关错误率');
|
||||
CREATE TABLE ops_alert_events (
|
||||
id uuid PRIMARY KEY,
|
||||
rule_id varchar(64) NOT NULL REFERENCES ops_alert_rules(id),
|
||||
total_requests bigint NOT NULL,
|
||||
failed_requests bigint NOT NULL,
|
||||
error_percent numeric(5,2) NOT NULL,
|
||||
status varchar(16) NOT NULL DEFAULT 'open' CHECK(status IN ('open','acknowledged','resolved')),
|
||||
created_at timestamptz NOT NULL DEFAULT now(),
|
||||
acknowledged_by uuid REFERENCES users(id),
|
||||
acknowledged_at timestamptz
|
||||
);
|
||||
CREATE INDEX ops_alert_events_recent ON ops_alert_events(created_at DESC);
|
||||
+2
@@ -0,0 +1,2 @@
|
||||
ALTER TABLE usage_events ADD COLUMN actual_route_id varchar(64);
|
||||
ALTER TABLE usage_events ADD COLUMN observed_usage jsonb;
|
||||
@@ -0,0 +1,6 @@
|
||||
ALTER TABLE ops_alert_rules ADD COLUMN notification_email varchar(254);
|
||||
ALTER TABLE ops_alert_rules ADD COLUMN notification_enabled boolean NOT NULL DEFAULT false;
|
||||
ALTER TABLE ops_alert_events ADD COLUMN notification_status varchar(24) NOT NULL DEFAULT 'disabled';
|
||||
ALTER TABLE ops_alert_events ADD COLUMN notification_attempts integer NOT NULL DEFAULT 0;
|
||||
ALTER TABLE ops_alert_events ADD COLUMN notification_next_at timestamptz;
|
||||
ALTER TABLE ops_alert_events ADD COLUMN notification_email varchar(254);
|
||||
@@ -5,5 +5,9 @@ if redis.call('ZCARD', KEYS[1]) >= tonumber(ARGV[1]) then
|
||||
return 0
|
||||
end
|
||||
redis.call('ZADD', KEYS[1], now + tonumber(ARGV[2]), ARGV[3])
|
||||
redis.call('PEXPIRE', KEYS[1], ARGV[2])
|
||||
-- Long async leases and short chat leases share the user bucket.
|
||||
-- A chat admission must never shorten a running async task's lease container.
|
||||
if redis.call('PTTL', KEYS[1]) < tonumber(ARGV[2]) then
|
||||
redis.call('PEXPIRE', KEYS[1], ARGV[2])
|
||||
end
|
||||
return 1
|
||||
+5
-1
@@ -82,6 +82,7 @@ class PlatformAdminPostgresTest {
|
||||
context.registerBean(DataSource.class, () -> source);
|
||||
context.registerBean(DataSourceTransactionManager.class, () -> new DataSourceTransactionManager(source));
|
||||
context.registerBean(AuthService.class, () -> new AuthService(jdbc, new PasswordHasher("test-pepper"), json, 168));
|
||||
context.registerBean(TotpService.class, () -> new TotpService(jdbc,new SecretCipher("test-mfa")));
|
||||
context.registerBean(CatalogService.class, () -> new CatalogService(jdbc, json, new SecretCipher("test-encryption")));
|
||||
context.registerBean(WalletService.class, () -> new WalletService(jdbc, context.getBean(AuthService.class)));
|
||||
context.registerBean(UsageAuditService.class, () -> new UsageAuditService(jdbc,json));
|
||||
@@ -371,6 +372,7 @@ class PlatformAdminPostgresTest {
|
||||
ChatFixture chatFixture() {
|
||||
var model=new LinkedHashMap<>(catalog.listModels(false).stream().filter(m -> "provider-deepseek".equals(m.get("providerId"))).findFirst().orElseThrow());
|
||||
model.put("pricing",Map.of("input",2,"output",4,"cachedInput",1,"unit","CNY / 1M tokens"));
|
||||
jdbc.update("UPDATE model_catalog SET pricing='{"+"\"input\":2,\"output\":4,\"cachedInput\":1,\"unit\":\"CNY / 1M tokens\"}'::jsonb WHERE id=?",model.get("id"));
|
||||
var created=catalog.createKey(memberToken,Map.of("name","Local billing test","modelIds",List.of(model.get("id"))),auth);
|
||||
var key=catalog.authenticateKey(created.get("key").toString());
|
||||
var input=ChatMeter.request(Map.of("model",model.get("id"),"messages",List.of(Map.of("role","user","content","你好")),"max_tokens",16),json);
|
||||
@@ -444,9 +446,11 @@ class PlatformAdminPostgresTest {
|
||||
var f=chatFixture();
|
||||
assertEquals("INSUFFICIENT_BALANCE",assertThrows(ApiException.class,()->f.reserve(null)).getCode());
|
||||
f.model().remove("pricing");
|
||||
jdbc.update("UPDATE model_catalog SET pricing=NULL WHERE id=?",f.model().get("id"));
|
||||
assertEquals("MODEL_NOT_PRICED",assertThrows(ApiException.class,()->f.reserve(null)).getCode());
|
||||
f.model().put("pricing",Map.of("input",1,"output",2,"unit","USD / 1M tokens"));
|
||||
assertEquals("MODEL_NOT_PRICED",assertThrows(ApiException.class,()->f.reserve(null)).getCode());
|
||||
jdbc.update("UPDATE model_catalog SET pricing='{"+"\"input\":1,\"output\":2,\"unit\":\"USD / 1M tokens\"}'::jsonb WHERE id=?",f.model().get("id"));
|
||||
assertEquals("BILLING_UNIT_UNSUPPORTED",assertThrows(ApiException.class,()->f.reserve(null)).getCode());
|
||||
assertEquals(0,jdbc.queryForObject("SELECT count(*) FROM chat_billing_requests",Integer.class));
|
||||
}
|
||||
|
||||
|
||||
@@ -51,6 +51,7 @@ class AuthPostgresTest {
|
||||
context.registerBean(DataSource.class, () -> source);
|
||||
context.registerBean(DataSourceTransactionManager.class, () -> new DataSourceTransactionManager(source));
|
||||
context.registerBean(AuthService.class, () -> new AuthService(jdbc, hasher, json, 168));
|
||||
context.registerBean(TotpService.class, () -> new TotpService(jdbc,new com.moyi.controlplane.security.SecretCipher("test-mfa")));
|
||||
context.refresh();
|
||||
AuthService auth = context.getBean(AuthService.class);
|
||||
try (var threads = Executors.newFixedThreadPool(2)) {
|
||||
|
||||
@@ -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));
|
||||
}
|
||||
}
|
||||
+32
@@ -0,0 +1,32 @@
|
||||
package com.moyi.controlplane.catalog;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
import java.util.Map;
|
||||
import com.moyi.controlplane.common.ApiException;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class CatalogServiceTest {
|
||||
@Test
|
||||
void pricingRejectsAmbiguousCurrencyAndDiscountTypos() {
|
||||
assertThrows(ApiException.class, () -> CatalogService.validatePricing(Map.of("listPriceCny", Map.of("input", 1), "listPriceUsd", Map.of("input", 1))));
|
||||
assertThrows(ApiException.class, () -> CatalogService.validatePricing(Map.of("discount", 55)));
|
||||
assertThrows(ApiException.class, () -> CatalogService.validatePricing(Map.of("listPriceCny", Map.of("input", 1), "exchangeRate", 7.2)));
|
||||
assertDoesNotThrow(() -> CatalogService.validatePricing(Map.of("listPriceCny", Map.of("input", 1), "exchangeRate", 1, "discount", 5.5)));
|
||||
assertDoesNotThrow(() -> CatalogService.validatePricing(Map.of("listPriceUsd", Map.of("input", 1), "exchangeRate", 7.2, "discount", 5.6)));
|
||||
assertDoesNotThrow(() -> CatalogService.validatePricing(Map.of("price", 1.44, "unit", "CNY / task")));
|
||||
}
|
||||
@Test
|
||||
void asyncReadinessMatchesImplementedAdapters() {
|
||||
assertTrue(CatalogService.asyncExecutorReady("provider-vast", "http", "3d"));
|
||||
assertTrue(CatalogService.asyncExecutorReady("provider-bytedance", "http", "video"));
|
||||
assertTrue(CatalogService.asyncExecutorReady("relay-provider", "openai-compatible", "image"));
|
||||
assertTrue(CatalogService.asyncExecutorReady("relay-provider", "OPENAI-COMPATIBLE", "video"));
|
||||
assertTrue(CatalogService.asyncExecutorReady("relay-provider", "openai-compatible", "audio"));
|
||||
assertFalse(CatalogService.asyncExecutorReady("relay-provider", "openai-compatible", "3d"));
|
||||
assertFalse(CatalogService.asyncExecutorReady("relay-provider", "http", "video"));
|
||||
}
|
||||
}
|
||||
+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));
|
||||
}
|
||||
}
|
||||
+48
-9
@@ -20,6 +20,19 @@ import static org.mockito.ArgumentMatchers.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
class GatewayDeploymentTest {
|
||||
@Test void failedTaskClaimReleasesRedisLeaseAfterDatabaseRollback() {
|
||||
var redis=mock(StringRedisTemplate.class);doReturn(1L).when(redis).execute(any(RedisScript.class),anyList(),any(Object[].class));
|
||||
var policies=mock(EnterprisePolicies.class);UUID key=UUID.randomUUID(),user=UUID.randomUUID();
|
||||
when(policies.taskLimit(key)).thenReturn(new EnterprisePolicies.UserLimit(user,120,16));
|
||||
var guard=new GatewayGuard(redis,60,300,4,"moyi:test:");guard.setEnterprisePolicies(policies);
|
||||
org.springframework.transaction.support.TransactionSynchronizationManager.initSynchronization();
|
||||
try {
|
||||
assertTrue(guard.tryTaskUserSlot(key,"isolated-task",3600000));
|
||||
clearInvocations(redis);
|
||||
for(var callback:org.springframework.transaction.support.TransactionSynchronizationManager.getSynchronizations()) callback.afterCompletion(org.springframework.transaction.support.TransactionSynchronization.STATUS_ROLLED_BACK);
|
||||
verify(redis).execute(any(RedisScript.class),eq(List.of("moyi:test:gateway:slots:user:"+user)),eq("isolated-task"));
|
||||
} finally {org.springframework.transaction.support.TransactionSynchronizationManager.clearSynchronization();}
|
||||
}
|
||||
@Test
|
||||
void capacitySnapshotReadsBothLiveAndWaitingBucketsAndDoesNotInventZeroOnFailure() {
|
||||
StringRedisTemplate redis = mock(StringRedisTemplate.class);
|
||||
@@ -66,6 +79,32 @@ class GatewayDeploymentTest {
|
||||
verify(redis, atLeastOnce()).execute(any(RedisScript.class), eq(List.of("moyi:test:gateway:slots:key:" + keyId)), any(Object[].class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void zeroModelConcurrencyUsesOnlyTheIndependentKeyLease() {
|
||||
StringRedisTemplate redis = mock(StringRedisTemplate.class);
|
||||
doReturn(1L).when(redis).execute(any(RedisScript.class), anyList(), any(Object[].class));
|
||||
UUID keyId = UUID.randomUUID();
|
||||
Map<String, Object> unlimited = Map.of("id", "model-unlimited", "maxConcurrency", 0);
|
||||
try (GatewayGuard.Admission ignored = new GatewayGuard(redis, 60, 300, 4, 2, 15, 10, "moyi:test:").acquire(unlimited, keyId, 15)) {
|
||||
// The route is deliberately unlimited at the MOYI model layer.
|
||||
}
|
||||
verify(redis, never()).execute(any(RedisScript.class), eq(List.of("moyi:test:gateway:slots:model:model-unlimited")), any(Object[].class));
|
||||
verify(redis, atLeastOnce()).execute(any(RedisScript.class), eq(List.of("moyi:test:gateway:slots:key:" + keyId)), any(Object[].class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void disabledModelGuardDoesNotCreateModelLease() {
|
||||
StringRedisTemplate redis = mock(StringRedisTemplate.class);
|
||||
doReturn(1L).when(redis).execute(any(RedisScript.class), anyList(), any(Object[].class));
|
||||
UUID keyId = UUID.randomUUID();
|
||||
Map<String, Object> model = Map.of("id", "model-measurement", "maxConcurrency", 1);
|
||||
try (GatewayGuard.Admission ignored = new GatewayGuard(redis, 60, 300, 4, 2, 15, 10, "moyi:test:", true).acquire(model, keyId, 15)) {
|
||||
// Measurement mode bypasses only the MOYI model-group lease.
|
||||
}
|
||||
verify(redis, never()).execute(any(RedisScript.class), eq(List.of("moyi:test:gateway:slots:model:model-measurement")), any(Object[].class));
|
||||
verify(redis, atLeastOnce()).execute(any(RedisScript.class), eq(List.of("moyi:test:gateway:slots:key:" + keyId)), any(Object[].class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void modelsExposeReadableIdAndKeepRouteIdForDiagnostics() {
|
||||
CatalogService catalog = mock(CatalogService.class);
|
||||
@@ -195,13 +234,13 @@ class GatewayDeploymentTest {
|
||||
UsageAuditService usage = mock(UsageAuditService.class);
|
||||
Map<String, Object> key = Map.of("id", UUID.randomUUID().toString(), "userId", UUID.randomUUID().toString());
|
||||
when(catalog.authenticateKey("test-key")).thenReturn(key);
|
||||
when(catalog.activeModel("model-real")).thenReturn(Map.of("id", "model-real", "providerId", "provider-deepseek", "mode", "sync"));
|
||||
when(catalog.activeModel("model-real")).thenReturn(Map.of("id", "model-real", "providerId", "provider-deepseek", "mode", "sync", "type", "text"));
|
||||
when(catalog.keyCanInvoke(key, "model-real")).thenReturn(true);
|
||||
GatewayController controller = new GatewayController(catalog, provider, guard, mock(ChatBillingService.class),new ObjectMapper());
|
||||
ApiException error = assertThrows(ApiException.class, () -> controller.chatCompletions("Bearer test-key", Map.of("model", "model-real"),null,new MockHttpServletResponse()));
|
||||
assertEquals("PROVIDER_NOT_CONFIGURED", error.getCode());
|
||||
assertEquals(503, error.getStatus());
|
||||
verify(provider, never()).chat(anyString(), anyString(), anyMap());
|
||||
verify(provider, never()).chat(anyString(), anyString(), anyString(), anyMap());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -209,7 +248,7 @@ class GatewayDeploymentTest {
|
||||
CatalogService catalog = mock(CatalogService.class);
|
||||
Map<String, Object> key = Map.of("id", UUID.randomUUID().toString(), "userId", UUID.randomUUID().toString());
|
||||
when(catalog.authenticateKey("test-key")).thenReturn(key);
|
||||
when(catalog.activeModel("mock")).thenReturn(Map.of("id", "mock", "providerId", "provider-mock", "mode", "sync"));
|
||||
when(catalog.activeModel("mock")).thenReturn(Map.of("id", "mock", "providerId", "provider-mock", "mode", "sync", "type", "text"));
|
||||
when(catalog.keyCanInvoke(key, "mock")).thenReturn(true);
|
||||
var billing=mock(ChatBillingService.class);
|
||||
when(billing.reserve(anyMap(),anyMap(),any(),any())).thenReturn(new ChatBillingService.Ticket(UUID.randomUUID(),UUID.fromString(key.get("userId").toString()),ChatMeter.rates(null,true)));
|
||||
@@ -226,17 +265,17 @@ class GatewayDeploymentTest {
|
||||
ChatBillingService billing = mock(ChatBillingService.class);
|
||||
String userId = UUID.randomUUID().toString();
|
||||
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.activeModel("reasoning-route")).thenReturn(model);
|
||||
when(catalog.keyCanInvoke(key, "reasoning-route")).thenReturn(true);
|
||||
when(provider.isConfigured("provider-relay")).thenReturn(true);
|
||||
when(provider.isConfigured("provider-relay", "reasoning-route", "reasoning-model")).thenReturn(true);
|
||||
Map<String, Object> upstreamResponse = new java.util.LinkedHashMap<>(Map.of(
|
||||
"id", "upstream-response",
|
||||
"choices", List.of(Map.of("index", 0, "message", Map.of("role", "assistant", "content", "", "reasoning_content", "private reasoning"), "finish_reason", "length")),
|
||||
"usage", Map.of("prompt_tokens", 2, "completion_tokens", 16, "total_tokens", 18)
|
||||
));
|
||||
when(provider.chat(eq("provider-relay"), eq("reasoning-model"), anyMap())).thenReturn(upstreamResponse);
|
||||
when(provider.chat(eq("provider-relay"), eq("reasoning-model"), eq("reasoning-route"), anyMap())).thenReturn(upstreamResponse);
|
||||
when(billing.reserve(anyMap(), anyMap(), any(), isNull())).thenReturn(new ChatBillingService.Ticket(UUID.randomUUID(), UUID.fromString(userId), ChatMeter.rates(null, true)));
|
||||
when(billing.settle(any(), anyMap(), anyInt(), isNull())).thenReturn(java.math.BigDecimal.ZERO);
|
||||
|
||||
@@ -258,16 +297,16 @@ class GatewayDeploymentTest {
|
||||
ChatBillingService billing = mock(ChatBillingService.class);
|
||||
String userId = UUID.randomUUID().toString();
|
||||
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.activeModel("empty-route")).thenReturn(model);
|
||||
when(catalog.keyCanInvoke(key, "empty-route")).thenReturn(true);
|
||||
when(provider.isConfigured("provider-relay")).thenReturn(true);
|
||||
when(provider.isConfigured("provider-relay", "empty-route", "empty-model")).thenReturn(true);
|
||||
Map<String, Object> upstreamResponse = new java.util.LinkedHashMap<>(Map.of(
|
||||
"id", "upstream-empty",
|
||||
"choices", List.of(Map.of("index", 0, "message", Map.of("role", "assistant", "content", ""), "finish_reason", "stop"))
|
||||
));
|
||||
when(provider.chat(eq("provider-relay"), eq("empty-model"), anyMap())).thenReturn(upstreamResponse);
|
||||
when(provider.chat(eq("provider-relay"), eq("empty-model"), eq("empty-route"), anyMap())).thenReturn(upstreamResponse);
|
||||
when(billing.reserve(anyMap(), anyMap(), any(), isNull())).thenReturn(new ChatBillingService.Ticket(UUID.randomUUID(), UUID.fromString(userId), ChatMeter.rates(null, true)));
|
||||
GatewayController controller = new GatewayController(catalog, provider, guard, billing, new ObjectMapper());
|
||||
|
||||
|
||||
+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);}
|
||||
}
|
||||
}
|
||||
+20
-1
@@ -28,11 +28,12 @@ class OpenAiCompatibleProviderHttpTest {
|
||||
private final ObjectMapper json = new ObjectMapper();
|
||||
private HttpServer server;
|
||||
private OpenAiCompatibleProvider provider;
|
||||
private MockEnvironment environment;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() throws Exception {
|
||||
server = HttpServer.create(new InetSocketAddress("127.0.0.1", 0), 0);
|
||||
var environment = new MockEnvironment()
|
||||
environment = new MockEnvironment()
|
||||
.withProperty("moyi.providers.deepseek.api-key", "local-fake-key")
|
||||
.withProperty("moyi.providers.deepseek.base-url", "http://127.0.0.1:" + server.getAddress().getPort() + "/v1")
|
||||
.withProperty("moyi.gateway.upstream-timeout-seconds", "5");
|
||||
@@ -93,6 +94,24 @@ class OpenAiCompatibleProviderHttpTest {
|
||||
assertEquals(512, input.get("max_tokens"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void prefersRouteKeyThenModelKeyBeforeProviderKey() throws Exception {
|
||||
var authorization = new ArrayList<String>();
|
||||
server.createContext("/v1/chat/completions", exchange -> {
|
||||
authorization.add(exchange.getRequestHeaders().getFirst("Authorization"));
|
||||
write(exchange, 200, "{\"id\":\"ok\",\"choices\":[{\"message\":{\"role\":\"assistant\",\"content\":\"OK\"}}],\"usage\":{\"prompt_tokens\":1,\"completion_tokens\":1,\"total_tokens\":2}}");
|
||||
});
|
||||
server.start();
|
||||
environment.withProperty("MOYI_UPSTREAM_API_KEYS_ROUTE_ROUTE_A", "route-key")
|
||||
.withProperty("MOYI_UPSTREAM_API_KEYS_MODEL_PROVIDER_DEEPSEEK_GPT_5", "model-key")
|
||||
.withProperty("MOYI_UPSTREAM_API_KEYS_MODEL_GPT_5", "wrong-global-key");
|
||||
|
||||
provider.chat("provider-deepseek", "gpt-5", "route-a", textInput(128));
|
||||
provider.chat("provider-deepseek", "gpt-5", "route-b", textInput(128));
|
||||
|
||||
assertEquals(List.of("Bearer route-key", "Bearer model-key"), authorization);
|
||||
}
|
||||
|
||||
private Map<String, Object> textInput(int maxTokens) {
|
||||
var input = new LinkedHashMap<String, Object>();
|
||||
input.put("messages", List.of(Map.of("role", "user", "content", "Reply with OK.")));
|
||||
|
||||
+44
@@ -14,11 +14,13 @@ import com.moyi.controlplane.security.SecretCipher;
|
||||
import com.sun.net.httpserver.HttpServer;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
import java.util.Map;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.CsvSource;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.mock.env.MockEnvironment;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
|
||||
class AsyncProviderClientTest {
|
||||
@@ -139,6 +141,48 @@ class AsyncProviderClientTest {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void asyncPollingReusesTheSubmittedKeyFingerprint() throws Exception {
|
||||
HttpServer server = HttpServer.create(new InetSocketAddress("127.0.0.1", 0), 0);
|
||||
AtomicReference<String> submitAuthorization = new AtomicReference<>();
|
||||
AtomicReference<String> pollAuthorization = new AtomicReference<>();
|
||||
server.createContext("/videos", exchange -> {
|
||||
String authorization = exchange.getRequestHeaders().getFirst("Authorization");
|
||||
if (exchange.getRequestURI().getPath().equals("/videos")) {
|
||||
submitAuthorization.set(authorization);
|
||||
exchange.getRequestBody().readAllBytes();
|
||||
byte[] response = "{\"id\":\"video-1\",\"status\":\"queued\"}".getBytes(StandardCharsets.UTF_8);
|
||||
exchange.sendResponseHeaders(200, response.length);
|
||||
try (var output = exchange.getResponseBody()) { output.write(response); }
|
||||
} else {
|
||||
pollAuthorization.set(authorization);
|
||||
byte[] response = "{\"id\":\"video-1\",\"status\":\"succeeded\"}".getBytes(StandardCharsets.UTF_8);
|
||||
exchange.sendResponseHeaders(200, response.length);
|
||||
try (var output = exchange.getResponseBody()) { output.write(response); }
|
||||
}
|
||||
});
|
||||
server.start();
|
||||
try {
|
||||
JdbcTemplate jdbc = mock(JdbcTemplate.class);
|
||||
SecretCipher cipher = new SecretCipher("test-encryption");
|
||||
when(jdbc.queryForMap(any(String.class), eq("provider-video"))).thenReturn(Map.of(
|
||||
"code", "relay-video", "protocol", "openai-compatible", "base_url", "http://127.0.0.1:" + server.getAddress().getPort(), "credential_ciphertext", ""
|
||||
));
|
||||
MockEnvironment environment = new MockEnvironment()
|
||||
.withProperty("MOYI_UPSTREAM_API_KEYS_ROUTE_ROUTE_A", "key-a|key-b")
|
||||
.withProperty("MOYI_UPSTREAM_API_KEYS_MODEL_RELAY_VIDEO_SORA_2", "wrong-model-key");
|
||||
AsyncProviderClient client = new AsyncProviderClient(jdbc, cipher, new ObjectMapper(), environment);
|
||||
|
||||
Map<String,Object> submitted = client.submit("provider-video", "sora-2", "route-a", "video", Map.of("prompt", "a cat"));
|
||||
String fingerprint = String.valueOf(submitted.get("_moyiUpstreamKeyFingerprint"));
|
||||
assertTrue(fingerprint.matches("[0-9a-f]{64}"));
|
||||
client.poll("provider-video", "route-a", "sora-2", "video-1", fingerprint);
|
||||
assertEquals(submitAuthorization.get(), pollAuthorization.get());
|
||||
} finally {
|
||||
server.stop(0);
|
||||
}
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvSource({"401,UPSTREAM_AUTH_FAILED", "403,UPSTREAM_AUTH_FAILED", "402,UPSTREAM_CREDIT_INSUFFICIENT", "429,UPSTREAM_RATE_LIMITED", "503,UPSTREAM_PROVIDER_ERROR"})
|
||||
void httpErrorsExposeOnlySafeCodes(int status, String code) throws Exception {
|
||||
|
||||
+12
-9
@@ -46,8 +46,10 @@ class AsyncTaskPostgresTest {
|
||||
jdbc = new JdbcTemplate(source);
|
||||
usage = spy(new UsageAuditService(jdbc, json));
|
||||
provider = mock(AsyncProviderClient.class);
|
||||
GatewayGuard guard=mock(GatewayGuard.class);
|
||||
when(guard.tryTaskUserSlot(any(),anyString(),anyLong())).thenReturn(true);
|
||||
tasks = new AsyncTaskService(jdbc, json, mock(CatalogService.class), 100, 4, 4, 120_000,
|
||||
mock(GatewayGuard.class), usage, provider, new DataSourceTransactionManager(source));
|
||||
guard, usage, provider, new DataSourceTransactionManager(source));
|
||||
jdbc.update("INSERT INTO users(id,account,account_kind,password_hash,display_name) VALUES (?,'async-test','username','not-a-password','Test')", userId);
|
||||
jdbc.update("INSERT INTO platform_api_keys(id,user_id,name,key_prefix,key_hash) VALUES (?,?,'Test','test-prefix',?)", keyId, userId, "b".repeat(64));
|
||||
jdbc.update("INSERT INTO user_wallets(user_id,balance,reserved_balance) VALUES (?,10,1.44)", userId);
|
||||
@@ -64,8 +66,9 @@ class AsyncTaskPostgresTest {
|
||||
ReflectionTestUtils.invokeMethod(tasks,"failTask",taskId,"PROVIDER_NOT_CONFIGURED","异步渠道尚未配置 API Key");
|
||||
}
|
||||
private void poll() {
|
||||
Map<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("upstream_model","v3.1-20260211");
|
||||
ReflectionTestUtils.invokeMethod(tasks,"pollProviderTask",row);
|
||||
}
|
||||
private BigDecimal wallet(String column) {
|
||||
@@ -82,7 +85,7 @@ class AsyncTaskPostgresTest {
|
||||
@ValueSource(strings={"PROVIDER_NOT_CONFIGURED", "UPSTREAM_AUTH_FAILED"})
|
||||
void submissionFailureReleasesExactlyOnceWithoutDebitingBalance(String code) {
|
||||
jdbc.update("UPDATE model_tasks SET status='queued',provider_task_id=NULL WHERE id=?",taskId);
|
||||
when(provider.submit(anyString(),anyString(),anyString(),anyString())).thenThrow(new ApiException(code,"上游任务未接受",503));
|
||||
when(provider.submit(anyString(),anyString(),anyString(),anyString(),anyString())).thenThrow(new ApiException(code,"上游任务未接受",503));
|
||||
tasks.completeMockTasks();
|
||||
tasks.completeMockTasks();
|
||||
assertEquals("failed",status());
|
||||
@@ -91,7 +94,7 @@ class AsyncTaskPostgresTest {
|
||||
assertEquals(0,cost().signum());
|
||||
assertEquals(0,jdbc.queryForObject("SELECT cost_amount FROM usage_events WHERE provider_task_id=?",BigDecimal.class,taskId).signum());
|
||||
assertEquals(0,jdbc.queryForObject("SELECT count(*) FROM wallet_entries WHERE user_id=?",Integer.class,userId));
|
||||
verify(provider,times(1)).submit(anyString(),anyString(),anyString(),anyString());
|
||||
verify(provider,times(1)).submit(anyString(),anyString(),anyString(),anyString(),anyString());
|
||||
}
|
||||
|
||||
@Test void refundAndTaskStateRollBackTogetherWhenAuditFails() {
|
||||
@@ -108,7 +111,7 @@ class AsyncTaskPostgresTest {
|
||||
}
|
||||
|
||||
@Test void successfulSettlementRollsBackForRetryAndCannotBeRefundedByLateFailure() {
|
||||
when(provider.poll("provider-vast","upstream-task")).thenReturn(Map.of("data",Map.of("status","success","progress",100,"output",Map.of("model_url","https://example.test/model.glb"))));
|
||||
when(provider.poll("provider-vast","model-mock-3d","v3.1-20260211","upstream-task",null)).thenReturn(Map.of("data",Map.of("status","success","progress",100,"output",Map.of("model_url","https://example.test/model.glb"))));
|
||||
doThrow(new IllegalStateException("simulated database write failure")).when(usage).updateTask(eq(taskId),eq("succeeded"),anyMap(),isNull(),isNull());
|
||||
assertThrows(IllegalStateException.class,this::poll);
|
||||
assertEquals("running",status());
|
||||
@@ -128,7 +131,7 @@ class AsyncTaskPostgresTest {
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings={"UPSTREAM_PROVIDER_ERROR", "UPSTREAM_AUTH_FAILED", "UPSTREAM_RATE_LIMITED"})
|
||||
void pollingErrorsKeepTheHoldAndClearOnRecovery(String code) {
|
||||
when(provider.poll("provider-vast","upstream-task"))
|
||||
when(provider.poll("provider-vast","model-mock-3d","v3.1-20260211","upstream-task",null))
|
||||
.thenThrow(new ApiException(code,"private upstream details",502))
|
||||
.thenReturn(Map.of("data",Map.of("status","running","progress",75)));
|
||||
poll();
|
||||
@@ -160,7 +163,7 @@ class AsyncTaskPostgresTest {
|
||||
}
|
||||
|
||||
@Test void confirmedUpstreamFailureReleasesHoldImmediately() {
|
||||
when(provider.poll("provider-vast","upstream-task")).thenReturn(Map.of("data",Map.of("status","failed")));
|
||||
when(provider.poll("provider-vast","model-mock-3d","v3.1-20260211","upstream-task",null)).thenReturn(Map.of("data",Map.of("status","failed")));
|
||||
poll();
|
||||
assertEquals("failed",status());
|
||||
assertEquals(0,wallet("reserved_balance").signum());
|
||||
@@ -173,13 +176,13 @@ class AsyncTaskPostgresTest {
|
||||
OffsetDateTime migratedDeadline = jdbc.queryForObject("SELECT provider_deadline_at FROM model_tasks WHERE id=?",OffsetDateTime.class,taskId);
|
||||
assertEquals(Duration.ofHours(1),Duration.between(createdAt,migratedDeadline));
|
||||
jdbc.update("UPDATE model_tasks SET status='queued',provider_task_id=NULL,provider_deadline_at=NULL WHERE id=?",taskId);
|
||||
when(provider.submit(anyString(),anyString(),anyString(),anyString())).thenReturn(Map.of("data",Map.of("task_id","upstream-task")));
|
||||
when(provider.submit(anyString(),anyString(),anyString(),anyString(),anyString())).thenReturn(Map.of("data",Map.of("task_id","upstream-task")));
|
||||
tasks.completeMockTasks();
|
||||
OffsetDateTime submittedAt = jdbc.queryForObject("SELECT updated_at FROM model_tasks WHERE id=?",OffsetDateTime.class,taskId);
|
||||
OffsetDateTime deadline = jdbc.queryForObject("SELECT provider_deadline_at FROM model_tasks WHERE id=?",OffsetDateTime.class,taskId);
|
||||
assertEquals(Duration.ofMinutes(2),Duration.between(submittedAt,deadline));
|
||||
tasks.completeMockTasks();
|
||||
verify(provider,times(1)).submit(anyString(),anyString(),anyString(),anyString());
|
||||
verify(provider,times(1)).submit(anyString(),anyString(),anyString(),anyString(),anyString());
|
||||
verify(provider,never()).poll(anyString(),anyString());
|
||||
assertEquals("running",status());
|
||||
}
|
||||
|
||||
+1
@@ -51,6 +51,7 @@ class UsageCapacityPostgresTest {
|
||||
@Test void liveCapacitySeparatesQueuedWorkAndKeepsEveryGroupAndLongRunningTask() {
|
||||
GatewayGuard guard = mock(GatewayGuard.class);
|
||||
when(guard.defaultModelConcurrency()).thenReturn(7);
|
||||
when(guard.modelConcurrencyEnabled()).thenReturn(true);
|
||||
when(guard.defaultKeyConcurrency()).thenReturn(3);
|
||||
when(guard.queueWaitMs()).thenReturn(4500L);
|
||||
when(guard.capacitySnapshot(anyList())).thenAnswer(invocation -> {
|
||||
|
||||
@@ -77,7 +77,7 @@ Invoke-WebRequest http://localhost:8080/health
|
||||
|
||||
本节点完成的是“控制面基础闭环 + 同步供应商转发 + Redis 网关保护 + 持久化任务队列”,不是生产网关完成版:
|
||||
|
||||
- 数据已写入 PostgreSQL;Redis 已用于 API Key 频率限制、API Key 并发租约、上游分组并发租约和 Redis Lua 原子操作。默认同步调用每 Key 每分钟 60 次(`GATEWAY_REQUESTS_PER_MINUTE`)、每 Key 同时 4 个请求(`GATEWAY_DEFAULT_KEY_CONCURRENCY`);分组并发由管理端模型分组的 `maxConcurrency` 配置,未配置时使用 `GATEWAY_DEFAULT_MODEL_CONCURRENCY`。Redis 不可用时网关返回 503,不会绕过保护直接请求上游;
|
||||
- 数据已写入 PostgreSQL;Redis 已用于 API Key 频率限制、API Key 并发租约、上游分组并发租约和 Redis Lua 原子操作。默认同步调用每 Key 每分钟 60 次(`GATEWAY_REQUESTS_PER_MINUTE`)、每 Key 同时 4 个请求(`GATEWAY_DEFAULT_KEY_CONCURRENCY`);分组并发由管理端模型分组的 `maxConcurrency` 配置,填 0 表示不设置 MOYI 分组上限,未配置时使用 `GATEWAY_DEFAULT_MODEL_CONCURRENCY`。Redis 不可用时网关返回 503,不会绕过保护直接请求上游;
|
||||
- `/v1/chat/completions` 支持 OpenAI 兼容同步/流式文本聊天;真实供应商未配置 Key 时返回 503,只有 `provider-mock` 可以返回 Mock。V13 起按人民币分组售价预留并结算,详见 [流式与计费](16-streaming-chat-billing.md);
|
||||
- `/v1/3d/tasks` 和 `/v1/video/tasks` 已提供数据库持久化 Mock worker;真实供应商执行器尚未替换,返回 501 而非伪造任务成功;
|
||||
- Seedance、Tripo/VAST 的异步任务和 OSS 结果存储尚未迁移到 Java;
|
||||
|
||||
@@ -192,10 +192,28 @@ MOYI 用户对外使用的统一 API Base URL 为 `https://moyi.soulofmodel.com/
|
||||
|
||||
## 一个渠道配置多个上游 Key
|
||||
|
||||
可以。当前 OpenAI 兼容转发器支持为同一渠道配置多个上游 Key,并按请求轮换,适合把上游的单 Key 并发拆到多个 Key。服务器环境变量优先使用渠道编码(管理端渠道的 code),也兼容渠道 ID 转成大写下划线后的名字,Key 之间用 `|` 分隔。例如渠道编码为 `apipro` 时:
|
||||
可以。当前 OpenAI 兼容转发器支持为同一渠道配置多个上游 Key,并按请求轮换,适合把上游的单 Key 并发拆到多个 Key。新版还支持“按 MOYI 模型分组固定 Key 池”:路由分组 Key 优先,其次是带渠道编码的上游模型 Key,再其次是渠道级 Key,最后才回退到管理端渠道里的单个加密凭据。模型 Key 必须带渠道编码,避免 APIPro、ApiHub、TokenFoundryX 的同名模型误用彼此的凭据。
|
||||
|
||||
服务器环境变量优先使用渠道编码(管理端渠道的 code),也兼容渠道 ID 转成大写下划线后的名字,Key 之间用 `|` 分隔。例如渠道编码为 `apipro` 时:
|
||||
|
||||
```ini
|
||||
MOYI_UPSTREAM_API_KEYS_APIPRO='key_a|key_b|key_c'
|
||||
```
|
||||
|
||||
按分组配置时,使用管理端模型路由的内部 ID(不是 `/v1/models` 中可读的公开模型 ID),然后标准化:转大写,把非字母数字替换为下划线。例如路由 ID `relay-route-044e37bb-ab61-4c6d-88a8-13adda55e4c0b` 对应:
|
||||
|
||||
```ini
|
||||
MOYI_UPSTREAM_API_KEYS_ROUTE_RELAY_ROUTE_044E37BB_AB61_4C6D_88A8_13ADDA55E4C0B='group_key'
|
||||
```
|
||||
|
||||
如果希望同一个渠道中的上游模型的多个分组共用一个 Key 池,可以使用带渠道编码的变量。渠道编码为 `apipro`、上游模型为 `glm-4.7` 时:
|
||||
|
||||
```ini
|
||||
MOYI_UPSTREAM_API_KEYS_MODEL_APIPRO_GLM_4_7='model_key_a|model_key_b'
|
||||
```
|
||||
|
||||
不再使用没有渠道编码的 `MOYI_UPSTREAM_API_KEYS_MODEL_GLM_4_7` 形式;它会被忽略,防止同名模型跨供应商串用 Key。
|
||||
|
||||
动态变量由 `docker-compose.cloud.yml` 的 API 服务 `env_file: .env.cloud` 注入容器;真实 Key 只能写服务器上的 `.env.cloud`,不能写 Git、迁移文件或聊天记录。每个分组创建一把 Key 时只填单个值。同步请求支持用竖线分隔多把 Key 轮换;异步任务建议每个分组只配置一把 Key,因为部分供应商要求用创建任务的同一凭据轮询任务状态。调用和异步创建/轮询都使用同一优先级规则。
|
||||
|
||||
保留管理端渠道中的单个凭据作为回退 Key。同步聊天和已接入的异步执行器都会在每个请求或轮询请求前轮换 Key。轮换只解决 Key 分摊,不能突破上游账户总并发、余额、模型授权或速率限制;平台自己的模型并发和单个 MOYI Key 并发仍由网关队列控制。多个 Key 应来自同一渠道、具有相同模型授权,并在服务器密钥文件中保存,不能提交到 Git。当前没有把这些 Key 明文放进管理端数据库的功能,避免把上游密钥暴露给前端;需要新增或撤销 Key 时修改服务器 `.env.cloud` 后重启 API 容器即可。
|
||||
@@ -13,11 +13,21 @@ API Key 权限窗口支持同时勾选多个模型,再选择各自的分组;
|
||||
一次同步模型调用要同时通过两层并发保护:
|
||||
|
||||
1. **平台 API Key 并发**:限制同一个用户 Key 同时进行中的请求数量。默认值为 4,可在服务端环境变量 `GATEWAY_DEFAULT_KEY_CONCURRENCY` 调整;每个 Key 独立计数,不会因为其他用户占满而互相阻塞。
|
||||
2. **上游分组并发**:限制同一个渠道分组向中转站发出的同时请求数量,供使用该分组的多个用户共享。管理员在模型与分组中填写“上游分组并发上限”;1 表示串行,4 表示最多同时处理 4 个请求。
|
||||
2. **上游分组并发**:可限制同一个渠道分组向中转站发出的同时请求数量,供使用该分组的多个用户共享。管理员在模型与分组中填写“MOYI 分组并发上限”;填 0 表示不设置 MOYI 分组上限,直接观察渠道方的真实限制;只有渠道明确给出共享上限时才填写正整数,1 才表示串行。
|
||||
|
||||
> **注意:这个数字来自 MOYI,不是渠道商返回值。** 早期目录迁移为了防止未经验证的路由压垮上游,把新建分组的 `max_concurrency` 初始写成了 `1`;因此管理端看到的 `1` 不能证明 ApiHub、APIPro、TokenFoundryX 或 Tripo 只允许一个并发。渠道商的账户并发、RPM、TPM 和跨模型共享额度必须通过渠道文档或实测确认。若要做渠道容量压测,先为目标分组临时设置足够大的 MOYI 上限,并同步提高测试 API Key 的并发和 RPM,避免平台自身的保护层先返回 `MODEL_BUSY`、`KEY_BUSY` 或 `RATE_LIMITED`。
|
||||
|
||||
两层限制会同时生效。同步/流式请求先尝试原子取得“模型分组 + 平台 Key”两个并发槽;若任一层满额,则在平台内进行有界等待,默认最多 30 秒(`GATEWAY_QUEUE_WAIT_MS`),每 100 毫秒重试一次(`GATEWAY_QUEUE_POLL_MS`)。等待时不会占着另一层资源;获得两个槽后才向上游发起请求,超时才返回 429 和 `Retry-After`。API Key 的每分钟请求频率和并发数是不同设置;异步任务另受任务队列上限控制。
|
||||
|
||||
这里的数值是 MOYI 配置的保护上限,不是性能压测上限,也不是中转站承诺的账号额度。上游账号可能还有跨模型共享的并发/RPM/TPM限制;需要用有效上游密钥做阶梯压测或由渠道方确认后,再提高 MOYI 分组上限。
|
||||
这里的正整数是 MOYI 配置的保护上限,不是性能压测上限,也不是中转站承诺的账号额度。默认值为 0(不限制模型分组);上游账号仍可能有跨模型共享的并发/RPM/TPM 限制,达到时会返回上游限流错误。API Key 并发和请求频率仍然生效,方便先测渠道能力,再按渠道方确认的额度设置保护值。
|
||||
|
||||
### 渠道最大并发的实测方法
|
||||
|
||||
1. 选定一个已授权、已计价且能稳定返回内容的模型分组,记录当前 `maxConcurrency`、测试 Key 的 `GATEWAY_DEFAULT_KEY_CONCURRENCY` 和 `GATEWAY_REQUESTS_PER_MINUTE`。
|
||||
2. 暂时将该分组的 MOYI 并发上限设为高于预计阶梯的值(例如 100),并将测试 Key 的并发/RPM 设为高于测试阶梯;每次变更后重新读取管理端模型详情确认保存成功。
|
||||
3. 用 `scripts/live-gateway-smoke.py --run-calls --max-concurrency N` 或等效压测脚本按 1、2、4、8、16…逐级发起短请求。每个阶梯应使用独立测试 Key,或在阶梯之间等待 RPM 窗口清空;否则累计请求会把平台自身的 RPM 限制误判为渠道限制。
|
||||
4. 对每个阶梯记录成功数、HTTP 状态、错误码、首字节/总耗时,并区分 `MODEL_BUSY`/`KEY_BUSY`/`RATE_LIMITED`(MOYI 层)与上游返回的 429、限流或余额错误。第一个稳定出现的上游 429/限流阶梯才是该 Key/模型在当前账户和时段的观测上限。
|
||||
5. 压测结束后恢复面向客户的并发、RPM 和 Key 限制。报告必须同时列出“观测上限”和“当前生产保护上限”,不能把任一数字写成渠道商的正式承诺。
|
||||
|
||||
## 用户操作指南
|
||||
|
||||
|
||||
@@ -0,0 +1,69 @@
|
||||
# 定价币种与对外折扣(2026-10-12 核对)
|
||||
|
||||
## 单位和规则
|
||||
|
||||
`1M tokens`、`1,000,000 tokens`、`一百万 Token` 是同一个计价单位。聊天结算仍按实际输入、输出和缓存命中用量除以 1,000,000,保留 8 位小数。
|
||||
|
||||
对外折数 = 采购折数 + 1。例如采购 4.6 折,对外 5.6 折,售价是原价的 0.56;这不是采购成本乘 1.1。此规则可由 V17 与原始报价表逐项对应确认。
|
||||
|
||||
| 渠道/范围 | 采购折数 | 对外折数 |
|
||||
|---|---:|---:|
|
||||
| APIPro Claude、Gemini、OpenAI、Grok | 4.6 | 5.6 |
|
||||
| APIPro GLM、Kimi、MiniMax、Qwen、DeepSeek | 5.5 | 6.5 |
|
||||
| APIPro Doubao | 9.0 | 10.0 |
|
||||
| ApiHub / TokenFoundryX | 按用户提供报价表逐型号匹配 | 采购折数 + 1 |
|
||||
|
||||
采购折扣来自用户 2026-10-12 确认;原始表《芯化兰德报价_2026.09.09.xlsx》明确标注 USD / 百万 tokens,不能把历史表的美元标签解释为人民币。当前 ApiHub 网站显示 CNY,TokenFoundryX / APIPro 显示 USD;本次按当前网站原价与币种计算,表格只作为逐型号采购折扣和历史原价的依据。实际 Key 分组结算仍须与渠道账单核对。
|
||||
|
||||
人民币原价:`原价 × 对外折数 / 10`。
|
||||
|
||||
美元原价:`原价 × 对外折数 / 10 × USD/CNY`。本次沿用已配置的 7.2,不将它声称为实时外汇汇率。
|
||||
|
||||
## 系统修复
|
||||
|
||||
- 原价分别保存为 `listPriceCny` / `listPriceUsd`,不能同时存在。人民币原价的 `exchangeRate` 为 1。
|
||||
- 管理端可以选择原价币种、填入对外折数,明确点击换算后才更新人民币售价草稿,保存后生效。
|
||||
- 计算使用整数定点乘法及 HALF_UP 八位小数,避免浮点乘法累积误差。
|
||||
- 后端拒绝同时存在两种原价币种、55 折等明显无效折扣和人民币原价再使用 7.2 的配置。
|
||||
- 修改分组时保留已有任务标准价、固定任务费等未编辑的价格元数据。
|
||||
- 扣费时仍直接使用 `input` / `output` / `cachedInput`,不会再次乘折扣或汇率。正在进行的调用继续使用预留时的价格快照。
|
||||
- 新增 V34,保留既有 Flyway 迁移,不修改历史 V17/V22/V23。
|
||||
|
||||
## 修正范围与保留项
|
||||
|
||||
原用户价格清单覆盖 206 条 Token 路由、2 条 Tripo 任务。V34 准备修正 35 条 Token 路由,包含金额、缓存价及原价精度/币种元数据。107 条与本轮规则一致,64 条仍保留现价待核。配置就绪不代表已经逐模型付费实测。
|
||||
|
||||
例:ApiHub GLM-5.3-flash 原价 CNY 0.8/2.8,采购 2 折、对外 3 折,售价为 CNY 0.24/0.84;TokenFoundryX Gemini 2.5 Pro 原价 USD 1.25/10,采购 2.5 折、对外 3.5 折,售价为 CNY 3.15/25.2。
|
||||
|
||||
以下不自动推断或涨价:
|
||||
|
||||
- 渠道当前目录未找到同名模型,或者没有用户确认的采购折扣。
|
||||
- 动态上下文/时段等计费表达式:本次没有为其新增动态结算引擎。现有固定售价不等于覆盖上游所有档位。
|
||||
- APIPro `gemini-2.5-flash-preview-09-2025` 当前公开倍率推导的输出价比历史价高 100 倍以上,须核实渠道配置。
|
||||
- Tripo 任务类型、纹理和额外参数费用:本次不调整原来的 1.44 元任务价。
|
||||
|
||||
TokenFoundryX `gemini-2.5-flash-lite` 当前网页仍显示 USD 75/300,远高于旧表 USD 0.1/0.4。此次补齐 3.5 折对外折扣后的值为 CNY 189/756,不能据此认定渠道原价合理。
|
||||
|
||||
## 来源与复算
|
||||
|
||||
- MOYI:<https://moyi.soulofmodel.com/api/models>
|
||||
- ApiHub 登录后模型广场:<https://apihub.chem-cloud.cn/pricing>
|
||||
- TokenFoundryX:<https://www.tokenfoundryx.com/api/pricing>、<https://www.tokenfoundryx.com/pricing>
|
||||
- APIPro:<https://api.apipro.ai/api/pricing>
|
||||
- 用户原报价表及本轮确认的 APIPro 折扣。
|
||||
|
||||
`scripts/reconcile-pricing.py` 读取已保存的来源快照,用 Decimal 复算,并生成修价对照和迁移。脚本不访问网络、不连接数据库、不执行渠道计费表达式。不得将它当作无审核实时同步器。输出迁移匹配渠道、模型和完整旧价格 JSON,管理员在核对后另改过的价格会被保留,且不更改启用状态、凭据、余额或历史订单。
|
||||
|
||||
本地回归:`scripts/test_reconcile_pricing.py`;隔离 PostgreSQL 迁移验证:`scripts/test_pricing_migration.py --sources <快照目录> --postgres-bin <本机 PostgreSQL bin>`。
|
||||
|
||||
## 发布验证
|
||||
|
||||
按 docs/11-server-test-deployment.md 创建独立发布目录并备份数据库,保留现有环境配置、证书和密钥。发布后重新读取 `/api/models`,逐条比较 V34 目标价格及币种,检查被旧值保护条件跳过的记录。未成功部署并核对前,修订版 Excel 只能标为待上线稿。
|
||||
|
||||
### 本次上线结果
|
||||
|
||||
2026-10-12 已从 `20260930-125109-ea9069e3` 升级至 `20261012-174254-512ca522`,代码提交 `512ca522`。升级前数据库及运行配置备份位于服务器 `/opt/moyi/backups/pre-upgrade-51b_48hh`,备份完整性检查通过。
|
||||
|
||||
V34 执行成功,35 条目标配置全部生效,其中 26 条实际金额改变、9 条为原价精度或元数据调整。发布后通过数据库与已登录的公开域名 `/api/models` 双重回读,206 条 Token 路由均与导出的价格一致,2 条任务报价保持不变,360 条未删除路由的启用状态保持不变。64 条来源待核记录仍保留现价,不视为已通过渠道原价核验。
|
||||
|
||||
发布包通过 Java 验证(92 个测试,25 个外部数据库条件测试跳过)、前端 18 个测试、Vue 类型检查与构建;隔离 PostgreSQL 验证了 35 条迁移、224 条价格结果、人工编辑保护与幂等性。线上前后端健康、登录、管理接口与未登录访问边界检查通过。
|
||||
@@ -0,0 +1,82 @@
|
||||
# 企业网关增量建设(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 设置展示验证。
|
||||
- 19:07:追加的异步领取回滚测试通过,GatewayDeploymentTest 共 19 项通过。该修复避免数据库领取失败遗留 Redis 长租约。
|
||||
- 最终发布包完成 Maven clean verify、18 项前端测试、Vue 类型检查与 Vite 构建。打包时未启动数据库,因此数据库测试由上面的独立全量运行提供证据。
|
||||
|
||||
## 上线结果
|
||||
|
||||
- 发布:`20261013-191204-af43947b`;应用提交:`af43947b`。
|
||||
- 发布包 SHA-256:`826a633e402c26dac413f512a147d3a321f805d0db03c4ea515c04bc84c6093b`。
|
||||
- 备份:`/opt/moyi/backups/pre-upgrade-q5e8c_ez`,已验证归档可读取。
|
||||
- 生产目录:`/opt/moyi/releases/20261013-191204-af43947b/infra/moyi`。
|
||||
- 前后端容器健康,HTTPS、管理员登录、会话及原有管理接口通过;数据库迁移到 V40。
|
||||
- 366 条模型目录的价格与状态校验一致;用户、Key 保留,钱包快照完全一致。
|
||||
- 原生四类接口未登录返回 401;新管理接口未登录拒绝访问,登录后价格历史、企业策略、成本对账、运营告警返回正常。
|
||||
- MFA 与邮件通知默认关闭,没有自动启用任何用户认证器、付费订阅或商户渠道。
|
||||
- 验收结果文件:`outputs/sub2api-comparison-20261013/enterprise-verification.json`;本地界面截图:同目录 `enterprise-policy-preview.jpg`。
|
||||
|
||||
## 部署与回退
|
||||
|
||||
使用 `scripts/package-deployment.ps1` 从干净提交生成 JAR、静态资源和校验清单。`infra/moyi/upgrade_release.py` 先备份现有数据库,再构建并启动同一个 Compose 项目,不执行 `docker compose down`。保留旧镜像和备份。
|
||||
|
||||
V35–V40 为新增表、列、索引和价格历史触发器。紧急回退优先切回旧应用镜像,保留新增数据库结构;除非明确接受备份后数据丢失,不覆盖恢复生产数据库。旧应用不理解新预算/MFA/原生协议,因此回退后应停止依赖这些新增能力。
|
||||
@@ -42,8 +42,13 @@ SESSION_TTL_HOURS=168
|
||||
JAVA_OPTS='-Xms256m -Xmx1536m -XX:+ExitOnOutOfMemoryError'
|
||||
GATEWAY_REQUESTS_PER_MINUTE=60
|
||||
GATEWAY_READS_PER_MINUTE=300
|
||||
GATEWAY_DEFAULT_MODEL_CONCURRENCY=4
|
||||
# 0 disables an artificial MOYI-side model/group cap; the upstream provider
|
||||
# still controls its own RPS/RPM/TPM/concurrency limits.
|
||||
GATEWAY_DEFAULT_MODEL_CONCURRENCY=0
|
||||
GATEWAY_DEFAULT_KEY_CONCURRENCY=4
|
||||
GATEWAY_DISABLE_MODEL_CONCURRENCY=false
|
||||
GATEWAY_QUEUE_WAIT_MS=30000
|
||||
GATEWAY_QUEUE_POLL_MS=100
|
||||
TASK_MAX_QUEUED_PER_MODEL=100
|
||||
MOYI_TASK_WORKER_INTERVAL_MS=1000
|
||||
# Accepted async tasks retry polling for up to this duration, then release
|
||||
@@ -85,3 +90,12 @@ OSS_ENDPOINT=oss-cn-beijing-internal.aliyuncs.com
|
||||
OSS_BUCKET=
|
||||
OSS_ACCESS_KEY_ID=
|
||||
OSS_ACCESS_KEY_SECRET=
|
||||
|
||||
# Optional per-route upstream key pools. Do not commit real values. Replace
|
||||
# ROUTE_ID with the normalized MOYI route/model id (uppercase, non-alphanumerics
|
||||
# replaced by underscores), and separate multiple keys with '|'. Route pools
|
||||
# take precedence over provider-scoped model pools, then channel-level pools,
|
||||
# then the single encrypted credential stored in the admin channel record.
|
||||
# MOYI_UPSTREAM_API_KEYS_ROUTE_<NORMALIZED_ROUTE_ID>='upstream_key'
|
||||
# MOYI_UPSTREAM_API_KEYS_MODEL_<NORMALIZED_PROVIDER_CODE>_<NORMALIZED_UPSTREAM_MODEL>='upstream_key'
|
||||
# MOYI_UPSTREAM_API_KEYS_APIPRO='key_for_channel_fallback_1|key_for_channel_fallback_2'
|
||||
@@ -30,6 +30,10 @@ def execute(command, capture=False, data=None):
|
||||
# shell stdin. This is especially important when deploy.py is invoked from
|
||||
# an SSH heredoc; only an explicit data payload may be sent to a process.
|
||||
options = {"input": data} if data is not None else {"stdin": subprocess.DEVNULL}
|
||||
if command[:2] == ["docker", "compose"] and "--env-file" in command:
|
||||
environment = os.environ.copy()
|
||||
environment["MOYI_RUNTIME_ENV_FILE"] = str(Path(command[command.index("--env-file") + 1]).resolve())
|
||||
options["env"] = environment
|
||||
result = subprocess.run(command, cwd=HERE, capture_output=capture,
|
||||
text=True, encoding="utf-8", errors="replace", **options)
|
||||
if result.returncode:
|
||||
|
||||
@@ -9,6 +9,10 @@ services:
|
||||
JAVA_RUNTIME_IMAGE: ${JAVA_RUNTIME_IMAGE:-eclipse-temurin:21-jre-alpine}
|
||||
image: moyi-api:${MOYI_IMAGE_TAG:-dev}
|
||||
restart: unless-stopped
|
||||
# Pass per-route key pools from the deployment secret file without
|
||||
# committing secret names or values to the repository.
|
||||
env_file:
|
||||
- ${MOYI_RUNTIME_ENV_FILE:-.env.cloud}
|
||||
environment:
|
||||
CONTROL_PLANE_HOST: 0.0.0.0
|
||||
CONTROL_PLANE_PORT: 8787
|
||||
@@ -24,6 +28,7 @@ services:
|
||||
MOYI_REDIS_SSL: ${MOYI_REDIS_SSL:-false}
|
||||
MOYI_REDIS_KEY_PREFIX: ${MOYI_REDIS_KEY_PREFIX:-moyi:test:}
|
||||
FLYWAY_ENABLED: true
|
||||
MOYI_FORWARD_HEADERS_STRATEGY: native
|
||||
FLYWAY_BASELINE_ON_MIGRATE: false
|
||||
AUTH_SECURE_COOKIE: false
|
||||
AUTH_PASSWORD_PEPPER: ${AUTH_PASSWORD_PEPPER:?Initialize auth secrets}
|
||||
@@ -46,8 +51,13 @@ services:
|
||||
TRIPO_API_KEY: ${TRIPO_API_KEY:-}
|
||||
GATEWAY_REQUESTS_PER_MINUTE: ${GATEWAY_REQUESTS_PER_MINUTE:-60}
|
||||
GATEWAY_READS_PER_MINUTE: ${GATEWAY_READS_PER_MINUTE:-300}
|
||||
GATEWAY_DEFAULT_MODEL_CONCURRENCY: ${GATEWAY_DEFAULT_MODEL_CONCURRENCY:-4}
|
||||
# 0 means no MOYI-side model-group cap; the upstream provider remains
|
||||
# responsible for its own concurrency and rate limits.
|
||||
GATEWAY_DEFAULT_MODEL_CONCURRENCY: ${GATEWAY_DEFAULT_MODEL_CONCURRENCY:-0}
|
||||
GATEWAY_DEFAULT_KEY_CONCURRENCY: ${GATEWAY_DEFAULT_KEY_CONCURRENCY:-4}
|
||||
# Set true for an upstream concurrency measurement run. This bypasses
|
||||
# all MOYI model/group leases while retaining per-Key protection.
|
||||
GATEWAY_DISABLE_MODEL_CONCURRENCY: ${GATEWAY_DISABLE_MODEL_CONCURRENCY:-false}
|
||||
GATEWAY_QUEUE_WAIT_MS: ${GATEWAY_QUEUE_WAIT_MS:-30000}
|
||||
GATEWAY_QUEUE_POLL_MS: ${GATEWAY_QUEUE_POLL_MS:-100}
|
||||
TASK_MAX_QUEUED_PER_MODEL: ${TASK_MAX_QUEUED_PER_MODEL:-100}
|
||||
|
||||
@@ -43,6 +43,7 @@ services:
|
||||
SPRING_DATASOURCE_PASSWORD: ${POSTGRES_PASSWORD}
|
||||
SPRING_DATA_REDIS_URL: redis://:${REDIS_PASSWORD}@redis:6379/0
|
||||
FLYWAY_ENABLED: true
|
||||
MOYI_FORWARD_HEADERS_STRATEGY: native
|
||||
depends_on:
|
||||
postgres:
|
||||
condition: service_healthy
|
||||
|
||||
@@ -23,7 +23,7 @@ location /api/ {
|
||||
proxy_read_timeout 120s;
|
||||
proxy_next_upstream off;
|
||||
}
|
||||
location /v1/ {
|
||||
location ~ ^/v1(?:beta)?/ {
|
||||
proxy_pass http://moyi-api:8787;
|
||||
proxy_http_version 1.1;
|
||||
proxy_set_header Host $http_host;
|
||||
|
||||
@@ -229,7 +229,8 @@ class DeploymentTests(unittest.TestCase):
|
||||
self.assertEqual(api["SPRING_DATASOURCE_PASSWORD"], "test-db-$value#literal")
|
||||
self.assertEqual(api["ADMIN_PASSWORD"], "Qa-Test42!")
|
||||
self.assertNotIn("MOYI_REDIS_PASSWORD", cfg["services"]["frontend"].get("environment", {}))
|
||||
self.assertNotIn("OSS_ACCESS_KEY_SECRET", api)
|
||||
self.assertFalse(api.get("OSS_ACCESS_KEY_SECRET"))
|
||||
self.assertNotIn("OSS_ACCESS_KEY_SECRET", cfg["services"]["frontend"].get("environment", {}))
|
||||
if mode == "https":
|
||||
ports = cfg["services"]["frontend"]["ports"]
|
||||
self.assertEqual({str(p["published"]) for p in ports}, {"80", "443", "8080"})
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
import { reactive, ref } from 'vue'
|
||||
import { authClient, type AuthUser } from '../services/auth'
|
||||
import UiModal from './UiModal.vue'
|
||||
import MfaSettings from './MfaSettings.vue'
|
||||
|
||||
const props = defineProps<{ user: AuthUser }>()
|
||||
const emit = defineEmits<{ close: []; saved: [user: AuthUser]; notify: [message: string] }>()
|
||||
@@ -47,6 +48,7 @@ async function save() {
|
||||
<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>
|
||||
</form>
|
||||
<MfaSettings />
|
||||
<template #footer>
|
||||
<button class="button secondary" :disabled="saving" @click="emit('close')">取消</button>
|
||||
<button class="button primary" type="submit" form="account-settings" :disabled="saving">{{ saving ? '保存中…' : '保存设置' }}</button>
|
||||
|
||||
@@ -5,6 +5,7 @@ import { catalogClient, type CatalogProvider, type CatalogModel, type LogicalMod
|
||||
import UiModal from './UiModal.vue'
|
||||
import ListPagination from './ListPagination.vue'
|
||||
import { usePagination } from '../composables/usePagination'
|
||||
import { calculateTokenRetailPricing } from '../services/pricing'
|
||||
|
||||
const props = defineProps<{ section: 'models' | 'channels' }>()
|
||||
const emit = defineEmits<{ notify: [message: string] }>()
|
||||
@@ -29,7 +30,9 @@ async function remove() {
|
||||
}
|
||||
const channel = reactive({ code: '', name: '', protocol: 'openai-compatible' as CatalogProvider['protocol'], sourceType: 'relay' as CatalogProvider['sourceType'], baseUrl: '', credentialType: 'api-key' as NonNullable<CatalogProvider['credentialType']>, credentialSecret: '', description: '', status: 'active' as CatalogProvider['status'] })
|
||||
const model = reactive({ publicName: '', modelKey: '', vendorName: '', type: 'text' as LogicalModel['type'], mode: 'sync' as LogicalModel['mode'], description: '', inputTypes: 'text', outputTypes: 'text', status: 'active' as LogicalModel['status'] })
|
||||
const group = reactive({ logicalModelId: '', providerId: '', sourceLabel: '', upstreamModel: '', input: '', output: '', cachedInput: '', unit: 'CNY / 1M tokens', discount: '', discountLabel: '', exchangeRate: '', priceSource: '', listPriceInput: '', listPriceOutput: '', listPriceCachedInput: '', priority: '100', maxConcurrency: '4', status: 'active' as CatalogModel['status'] })
|
||||
const group = reactive({ logicalModelId: '', providerId: '', sourceLabel: '', upstreamModel: '', input: '', output: '', cachedInput: '', unit: 'CNY / 1M tokens', discount: '', discountLabel: '', exchangeRate: '', priceSource: '', listPriceInput: '', listPriceOutput: '', listPriceCachedInput: '', priority: '100', maxConcurrency: '0', status: 'active' as CatalogModel['status'] })
|
||||
const listCurrency = ref<'USD' | 'CNY'>('USD')
|
||||
const originalPricing = ref<CatalogModel['pricing']>()
|
||||
const shownModels = computed(() => models.value.filter(item => `${item.publicName} ${item.vendorName || ''} ${item.modelKey || ''}`.toLowerCase().includes(query.value.toLowerCase())))
|
||||
const shownChannels = computed(() => channels.value.filter(item => `${item.name} ${item.code}`.toLowerCase().includes(query.value.toLowerCase())))
|
||||
const { page: modelPage, pageSize: modelPageSize, pagedItems: pagedModels } = usePagination(shownModels)
|
||||
@@ -64,9 +67,29 @@ function editModel(item?: LogicalModel) {
|
||||
}
|
||||
function editGroup(parent: LogicalModel, item?: CatalogModel) {
|
||||
editingId.value = item?.id
|
||||
Object.assign(group, { logicalModelId: parent.id, providerId: item?.providerId || '', sourceLabel: item?.sourceLabel || '', upstreamModel: item?.upstreamModel || '', input: item?.pricing?.input?.toString() || '', output: item?.pricing?.output?.toString() || '', cachedInput: item?.pricing?.cachedInput?.toString() || '', unit: item?.pricing?.unit || 'CNY / 1M tokens', discount: item?.pricing?.discount?.toString() || '', discountLabel: item?.pricing?.discountLabel || '', exchangeRate: item?.pricing?.exchangeRate?.toString() || '', priceSource: item?.pricing?.priceSource || '', listPriceInput: item?.pricing?.listPriceUsd?.input?.toString() || '', listPriceOutput: item?.pricing?.listPriceUsd?.output?.toString() || '', listPriceCachedInput: item?.pricing?.listPriceUsd?.cachedInput?.toString() || '', priority: String(item?.priority ?? 100), maxConcurrency: String(item?.maxConcurrency ?? 4), status: item?.status || 'active' })
|
||||
originalPricing.value = item?.pricing
|
||||
listCurrency.value = item?.pricing?.listPriceCny ? 'CNY' : 'USD'
|
||||
Object.assign(group, { logicalModelId: parent.id, providerId: item?.providerId || '', sourceLabel: item?.sourceLabel || '', upstreamModel: item?.upstreamModel || '', input: item?.pricing?.input?.toString() || '', output: item?.pricing?.output?.toString() || '', cachedInput: item?.pricing?.cachedInput?.toString() || '', unit: item?.pricing?.unit || 'CNY / 1M tokens', discount: item?.pricing?.discount?.toString() || '', discountLabel: item?.pricing?.discountLabel || '', exchangeRate: item?.pricing?.exchangeRate?.toString() || '', priceSource: item?.pricing?.priceSource || '', listPriceInput: item?.pricing?.listPriceUsd?.input?.toString() || '', listPriceOutput: item?.pricing?.listPriceUsd?.output?.toString() || '', listPriceCachedInput: item?.pricing?.listPriceUsd?.cachedInput?.toString() || '', priority: String(item?.priority ?? 100), maxConcurrency: String(item?.maxConcurrency ?? 0), status: item?.status || 'active' })
|
||||
const list = item?.pricing?.listPriceCny || item?.pricing?.listPriceUsd
|
||||
group.listPriceInput = list?.input?.toString() ?? ''
|
||||
group.listPriceOutput = list?.output?.toString() ?? ''
|
||||
group.listPriceCachedInput = list?.cachedInput?.toString() ?? ''
|
||||
editor.value = 'group'
|
||||
}
|
||||
function calculateRetail() {
|
||||
try {
|
||||
if (group.unit !== 'CNY / 1M tokens') throw new Error('自动换算仅支持每百万 Token 价格')
|
||||
if (group.listPriceInput === '' || group.listPriceOutput === '' || group.discount === '') throw new Error('请填写标准输入价、标准输出价和对外折扣')
|
||||
const price = calculateTokenRetailPricing(listCurrency.value, {input:Number(group.listPriceInput),output:Number(group.listPriceOutput),cachedInput:group.listPriceCachedInput === '' ? undefined : Number(group.listPriceCachedInput)}, Number(group.discount), group.exchangeRate === '' ? undefined : Number(group.exchangeRate))
|
||||
Object.assign(group, {input:String(price.input),output:String(price.output),cachedInput:price.cachedInput === undefined ? '' : String(price.cachedInput),discountLabel:price.discountLabel,exchangeRate:String(price.exchangeRate)})
|
||||
emit('notify', '已换算人民币售价,请保存使其生效')
|
||||
} catch (e) { emit('notify', e instanceof Error ? e.message : '换算失败') }
|
||||
}
|
||||
|
||||
function groupPricing(): CatalogModel['pricing'] {
|
||||
const list = group.listPriceInput === '' && group.listPriceOutput === '' && group.listPriceCachedInput === '' ? undefined : {input:group.listPriceInput === '' ? undefined : Number(group.listPriceInput),output:group.listPriceOutput === '' ? undefined : Number(group.listPriceOutput),cachedInput:group.listPriceCachedInput === '' ? undefined : Number(group.listPriceCachedInput)}
|
||||
return {...originalPricing.value,input:group.input === '' ? undefined : Number(group.input),output:group.output === '' ? undefined : Number(group.output),cachedInput:group.cachedInput === '' ? undefined : Number(group.cachedInput),unit:group.unit,discount:group.discount === '' ? undefined : Number(group.discount),discountLabel:group.discountLabel.trim() || undefined,exchangeRate:listCurrency.value === 'CNY' && list ? 1 : group.exchangeRate === '' ? undefined : Number(group.exchangeRate),priceSource:group.priceSource.trim() || undefined,listPriceUsd:listCurrency.value === 'USD' ? list : undefined,listPriceCny:listCurrency.value === 'CNY' ? list : undefined}
|
||||
}
|
||||
async function save() {
|
||||
if (saving.value) return
|
||||
saving.value = true
|
||||
@@ -81,7 +104,7 @@ async function save() {
|
||||
await catalogClient.saveDefinition(editingId.value, { ...model, inputTypes: model.inputTypes.split(',').map(s => s.trim()).filter(Boolean), outputTypes: model.outputTypes.split(',').map(s => s.trim()).filter(Boolean) })
|
||||
} else {
|
||||
if (!group.providerId || !group.sourceLabel.trim() || !group.upstreamModel.trim()) throw new Error('请选择渠道并填写分组名称、上游模型标识')
|
||||
await catalogClient.saveGroup(editingId.value, { logicalModelId: group.logicalModelId, providerId: group.providerId, sourceLabel: group.sourceLabel, upstreamModel: group.upstreamModel, status: group.status, priority: Number(group.priority), maxConcurrency: Number(group.maxConcurrency), pricing: { input: group.input === '' ? undefined : Number(group.input), output: group.output === '' ? undefined : Number(group.output), cachedInput: group.cachedInput === '' ? undefined : Number(group.cachedInput), unit: group.unit, discount: group.discount === '' ? undefined : Number(group.discount), discountLabel: group.discountLabel.trim() || undefined, exchangeRate: group.exchangeRate === '' ? undefined : Number(group.exchangeRate), priceSource: group.priceSource.trim() || undefined, listPriceUsd: group.listPriceInput === '' && group.listPriceOutput === '' && group.listPriceCachedInput === '' ? undefined : { input: group.listPriceInput === '' ? undefined : Number(group.listPriceInput), output: group.listPriceOutput === '' ? undefined : Number(group.listPriceOutput), cachedInput: group.listPriceCachedInput === '' ? undefined : Number(group.listPriceCachedInput) } } })
|
||||
await catalogClient.saveGroup(editingId.value, { logicalModelId: group.logicalModelId, providerId: group.providerId, sourceLabel: group.sourceLabel, upstreamModel: group.upstreamModel, status: group.status, priority: Number(group.priority), maxConcurrency: Number(group.maxConcurrency), pricing: groupPricing() })
|
||||
}
|
||||
editor.value = ''
|
||||
await refresh()
|
||||
@@ -108,7 +131,7 @@ async function toggle(kind: 'channel' | 'model' | 'group', item: CatalogProvider
|
||||
<section class="page-heading"><h1>{{ props.section === 'channels' ? '渠道管理' : '模型与分组' }}</h1><div class="heading-actions"><button class="icon-button" title="刷新" :disabled="loading" @click="refresh"><RefreshCcw :size="18" /></button><button class="button primary" @click="props.section === 'channels' ? editChannel() : editModel()"><Plus :size="16" />{{ props.section === 'channels' ? '添加渠道' : '添加模型' }}</button></div></section>
|
||||
<label class="search-input catalog-search"><Search :size="16" /><input v-model="query" :placeholder="props.section === 'channels' ? '搜索渠道名称或 Code' : '搜索模型名称或标识'" /></label>
|
||||
<p v-if="error" role="alert">{{ error }}</p>
|
||||
<p v-if="props.section === 'models'" class="catalog-secondary">启用模型后会显示在用户端模型广场。模型厂商表示模型的实际研发方,渠道表示请求经过的中转站或官方接口;分组与所属渠道也启用后,用户才能为 API Key 选择该分组。平台 API Key 并发和上游渠道/模型并发是两层独立限制:前者限制同一个 Key 的同时请求数,后者在下方按分组填写,填 1 才是串行,填 4 表示同一渠道分组最多同时处理 4 个请求。</p>
|
||||
<p v-if="props.section === 'models'" class="catalog-secondary">启用模型后会显示在用户端模型广场。模型厂商表示模型的实际研发方,渠道表示请求经过的中转站或官方接口;分组与所属渠道也启用后,用户才能为 API Key 选择该分组。平台 API Key 并发和上游渠道/模型并发是两层独立限制:前者限制同一个 Key 的同时请求数,后者在下方按分组填写;填 0 表示不设置 MOYI 分组上限,直接测量渠道方的真实并发。只有渠道方明确给出共享并发上限时才填写正整数。</p>
|
||||
<p v-if="loading">加载中…</p>
|
||||
<div v-if="props.section === 'channels'" class="responsive-table"><table><thead><tr><th>渠道</th><th>来源 / 协议</th><th>Base URL</th><th>凭据</th><th>状态</th><th>操作</th></tr></thead><tbody><tr v-for="item in pagedChannels" :key="item.id"><td><strong>{{ item.name }}</strong><small class="catalog-secondary">{{ item.code }}</small></td><td>{{ item.sourceType }}<small class="catalog-secondary">{{ item.protocol }}</small></td><td class="catalog-url">{{ item.baseUrl || '未配置' }}</td><td>{{ item.credentialMasked || '未配置' }}</td><td><label class="catalog-toggle"><input type="checkbox" :checked="item.status === 'active'" :disabled="saving" :aria-label="`${item.name} 启用`" @change="toggle('channel', item)" />{{ item.status === 'active' ? '已启用' : '已停用' }}</label></td><td><button class="icon-button" :title="`编辑渠道 ${item.name}`" @click="editChannel(item)"><Pencil :size="16" /></button><button class="icon-button" :title="`删除渠道 ${item.name}`" :disabled="saving" @click="confirmDelete('channel', item.id, item.name)"><Trash2 :size="16" /></button></td></tr><tr v-if="!shownChannels.length && !loading"><td colspan="6">暂无渠道</td></tr></tbody></table></div>
|
||||
<ListPagination v-if="props.section === 'channels'" v-model:page="channelPage" v-model:page-size="channelPageSize" :total="shownChannels.length" label="渠道分页" />
|
||||
@@ -116,7 +139,7 @@ async function toggle(kind: 'channel' | 'model' | 'group', item: CatalogProvider
|
||||
<section v-for="item in pagedModels" :key="item.id" class="model-definition-section">
|
||||
<header class="panel-header"><div><h3>{{ item.publicName }}</h3><small>{{ item.vendorName || '未标注厂商' }} · {{ item.modelKey || item.id }} · {{ item.type }} · {{ item.mode }}</small></div><div class="heading-actions"><label class="catalog-toggle"><input type="checkbox" :checked="item.status === 'active'" :disabled="saving" :aria-label="`${item.publicName} 启用`" @change="toggle('model', item)" />启用</label><button class="icon-button" :title="`编辑模型 ${item.publicName}`" @click="editModel(item)"><Pencil :size="16" /></button><button class="icon-button" :title="`删除模型 ${item.publicName}`" :disabled="saving" @click="confirmDelete('model', item.id, item.publicName)"><Trash2 :size="16" /></button><button class="button secondary small" @click="editGroup(item)"><Plus :size="15" />添加分组</button></div></header>
|
||||
<p v-if="item.description" class="catalog-secondary">{{ item.description }}</p>
|
||||
<div class="responsive-table"><table><thead><tr><th>分组名称</th><th>渠道</th><th>上游模型</th><th>输入 / 输出价格</th><th>对外折扣</th><th>并发</th><th>状态</th><th>操作</th></tr></thead><tbody><tr v-for="route in modelGroups(item.id)" :key="route.id"><td>{{ route.sourceLabel }}</td><td>{{ channelName(route.providerId) }}</td><td><code>{{ route.upstreamModel }}</code></td><td>{{ route.pricing?.input ?? '—' }} / {{ route.pricing?.output ?? '—' }}<small class="catalog-secondary">{{ route.pricing?.unit }}</small></td><td>{{ route.pricing?.discountLabel || '—' }}</td><td>{{ route.maxConcurrency ?? '默认' }}</td><td><label class="catalog-toggle"><input type="checkbox" :checked="route.status === 'active'" :disabled="saving" :aria-label="`${route.sourceLabel} 启用`" @change="toggle('group', route)" />{{ route.status === 'active' ? '已启用' : '已停用' }}</label></td><td><button class="icon-button" :title="`编辑分组 ${route.sourceLabel}`" @click="editGroup(item, route)"><Pencil :size="16" /></button><button class="icon-button" :title="`删除分组 ${route.sourceLabel}`" :disabled="saving" @click="confirmDelete('group', route.id, route.sourceLabel || '默认分组')"><Trash2 :size="16" /></button></td></tr><tr v-if="!modelGroups(item.id).length"><td colspan="8">暂无分组</td></tr></tbody></table></div>
|
||||
<div class="responsive-table"><table><thead><tr><th>分组名称</th><th>渠道</th><th>上游模型</th><th>输入 / 输出价格</th><th>对外折扣</th><th>并发</th><th>状态</th><th>操作</th></tr></thead><tbody><tr v-for="route in modelGroups(item.id)" :key="route.id"><td>{{ route.sourceLabel }}</td><td>{{ channelName(route.providerId) }}</td><td><code>{{ route.upstreamModel }}</code></td><td>{{ route.pricing?.input ?? '—' }} / {{ route.pricing?.output ?? '—' }}<small class="catalog-secondary">{{ route.pricing?.unit }}</small></td><td>{{ route.pricing?.discountLabel || '—' }}</td><td>{{ route.maxConcurrency === undefined || route.maxConcurrency === null || route.maxConcurrency === 0 ? '不限(由渠道限流)' : route.maxConcurrency }}</td><td><label class="catalog-toggle"><input type="checkbox" :checked="route.status === 'active'" :disabled="saving" :aria-label="`${route.sourceLabel} 启用`" @change="toggle('group', route)" />{{ route.status === 'active' ? '已启用' : '已停用' }}</label></td><td><button class="icon-button" :title="`编辑分组 ${route.sourceLabel}`" @click="editGroup(item, route)"><Pencil :size="16" /></button><button class="icon-button" :title="`删除分组 ${route.sourceLabel}`" :disabled="saving" @click="confirmDelete('group', route.id, route.sourceLabel || '默认分组')"><Trash2 :size="16" /></button></td></tr><tr v-if="!modelGroups(item.id).length"><td colspan="8">暂无分组</td></tr></tbody></table></div>
|
||||
</section>
|
||||
<p v-if="!shownModels.length && !loading">暂无模型</p>
|
||||
<ListPagination v-model:page="modelPage" v-model:page-size="modelPageSize" :total="shownModels.length" label="管理端模型分页" />
|
||||
@@ -152,10 +175,15 @@ async function toggle(kind: 'channel' | 'model' | 'group', item: CatalogProvider
|
||||
<div class="modal-form-grid"><label><span class="form-label">输入价格</span><input v-model="group.input" class="text-input" type="number" min="0" step="any" /></label><label><span class="form-label">输出价格</span><input v-model="group.output" class="text-input" type="number" min="0" step="any" /></label></div>
|
||||
<label class="form-label">缓存命中输入价格(留空按普通输入价格)<input v-model="group.cachedInput" class="text-input" type="number" min="0" step="any" /></label>
|
||||
<label class="form-label" for="group-unit">价格单位</label><input id="group-unit" v-model="group.unit" class="text-input" required />
|
||||
<div class="modal-form-grid"><label><span class="form-label">标准输入价(USD / 1M)</span><input v-model="group.listPriceInput" class="text-input" type="number" min="0" step="any" /></label><label><span class="form-label">标准输出价(USD / 1M)</span><input v-model="group.listPriceOutput" class="text-input" type="number" min="0" step="any" /></label></div>
|
||||
<label class="form-label">标准缓存输入价(USD / 1M,可选)<input v-model="group.listPriceCachedInput" class="text-input" type="number" min="0" step="any" /></label>
|
||||
<label class="form-label" for="list-currency">渠道标准价币种</label><select id="list-currency" v-model="listCurrency" class="text-input"><option value="CNY">人民币 CNY</option><option value="USD">美元 USD</option></select>
|
||||
<div class="modal-form-grid"><label><span class="form-label">标准输入价({{ listCurrency }} / 百万 Token)</span><input v-model="group.listPriceInput" class="text-input" type="number" min="0" step="any" /></label><label><span class="form-label">标准输出价({{ listCurrency }} / 百万 Token)</span><input v-model="group.listPriceOutput" class="text-input" type="number" min="0" step="any" /></label></div>
|
||||
<label class="form-label">标准缓存输入价({{ listCurrency }} / 百万 Token,可选)<input v-model="group.listPriceCachedInput" class="text-input" type="number" min="0" step="any" /></label>
|
||||
<label class="form-label">对外折扣数值<input v-model="group.discount" class="text-input" type="number" min="0" max="10" step="0.1" placeholder="例如:5.6 表示 5.6 折" /></label>
|
||||
<label v-if="listCurrency === 'USD'" class="form-label">美元兑人民币汇率<input v-model="group.exchangeRate" class="text-input" type="number" min="0.00000001" step="any" /></label>
|
||||
<button v-if="group.unit === 'CNY / 1M tokens'" type="button" class="button secondary" @click="calculateRetail">按标准价和对外折扣换算售价</button>
|
||||
<small>1M Token=1,000,000 Token=一百万 Token。人民币原价不乘汇率;美元原价仅换算一次。采购 4.6 折加 1 个折扣点,对外填 5.6。</small>
|
||||
<label class="form-label" for="group-discount">对外折扣(仅展示)</label><input id="group-discount" v-model="group.discountLabel" class="text-input" maxlength="24" placeholder="例如:5.6折" /><small>输入、输出价格是实际扣费值,单位为 CNY / 1M tokens。折扣只用于向用户展示,不会替代人民币售价;留空表示不展示折扣。</small>
|
||||
<div class="modal-form-grid"><label><span class="form-label">优先级</span><input v-model="group.priority" class="text-input" type="number" min="0" step="1" required /></label><label><span class="form-label">上游分组并发上限</span><input v-model="group.maxConcurrency" class="text-input" type="number" min="1" step="1" required /><small>这是中转站允许多个用户共享该模型的并发数。1 为串行;按渠道实际额度填写。</small></label></div>
|
||||
<div class="modal-form-grid"><label><span class="form-label">优先级</span><input v-model="group.priority" class="text-input" type="number" min="0" step="1" required /></label><label><span class="form-label">MOYI 分组并发上限</span><input v-model="group.maxConcurrency" class="text-input" type="number" min="0" step="1" required /><small>填 0 表示不设置平台分组上限,直接测量渠道方的真实并发;只有渠道明确给出共享上限时才填写正整数。</small></label></div>
|
||||
</template>
|
||||
</form>
|
||||
<template #footer><button class="button secondary" :disabled="saving" @click="editor = ''">取消</button><button class="button primary" form="catalog-editor" type="submit" :disabled="saving">{{ saving ? '保存中…' : '保存' }}</button></template>
|
||||
|
||||
@@ -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: '开发者',
|
||||
items: [
|
||||
{ id: 'keys', label: 'API Keys', icon: KeyRound },
|
||||
{ id: 'key-policies', label: 'Key 安全与预算', icon: ShieldCheck },
|
||||
{ id: 'tasks', label: '任务中心', icon: FileClock },
|
||||
{ id: 'usage', label: '用量与额度', icon: BarChart3 },
|
||||
{ id: 'docs', label: '接入文档', icon: FileText },
|
||||
@@ -46,10 +47,13 @@ export const adminNavigation: NavGroup[] = [
|
||||
label: '资源运营',
|
||||
items: [
|
||||
{ id: 'admin-users', label: '用户与额度', icon: Users },
|
||||
{ id: 'admin-policies', label: '企业调用策略', icon: ShieldCheck },
|
||||
{ id: 'admin-models', label: '模型与分组', icon: Boxes },
|
||||
{ id: 'admin-routing', label: '渠道管理', icon: Network },
|
||||
{ id: 'admin-capacity', label: '并发与队列', icon: Gauge },
|
||||
{ id: 'admin-billing', label: '计费配置', icon: CircleDollarSign },
|
||||
{ id: 'admin-finance', label: '价格版本与对账', icon: CircleDollarSign },
|
||||
{ id: 'admin-operations', label: '渠道健康与告警', icon: Gauge },
|
||||
{ id: 'admin-usage', label: '模型调用记录', icon: ScrollText },
|
||||
],
|
||||
},
|
||||
|
||||
@@ -22,6 +22,7 @@ export interface RegisterInput {
|
||||
export interface LoginInput {
|
||||
account: string
|
||||
password: string
|
||||
otp?: string
|
||||
}
|
||||
|
||||
export interface AuthClient {
|
||||
|
||||
@@ -44,6 +44,7 @@ export interface CatalogModel {
|
||||
upstreamCredits?: number
|
||||
standardPrice?: { price?: number; unit?: string; credits?: number; priceSource?: string }
|
||||
listPriceUsd?: { input?: number; output?: number; cachedInput?: number }
|
||||
listPriceCny?: { input?: number; output?: number; cachedInput?: number }
|
||||
}
|
||||
pricingConfigured?: boolean
|
||||
billingReady?: boolean
|
||||
@@ -174,6 +175,7 @@ export interface AdminUsageSummary {
|
||||
queueWaitMs: number
|
||||
defaultModelConcurrency: number
|
||||
defaultKeyConcurrency: number
|
||||
modelConcurrencyGuardEnabled: boolean
|
||||
asyncQueueLimit: number
|
||||
asyncWorkerIntervalMs: number
|
||||
synchronousSnapshotAvailable: boolean
|
||||
|
||||
@@ -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
|
||||
}
|
||||
@@ -11,6 +11,7 @@ export interface DisplayPricing {
|
||||
upstreamCredits?: number
|
||||
standardPrice?: { price?: number; unit?: string; credits?: number; priceSource?: string }
|
||||
listPriceUsd?: { input?: number; output?: number; cachedInput?: number }
|
||||
listPriceCny?: { input?: number; output?: number; cachedInput?: number }
|
||||
}
|
||||
|
||||
function numberLabel(value: number | undefined, currency = '¥') {
|
||||
@@ -51,8 +52,11 @@ export function standardPriceLabel(pricing?: DisplayPricing) {
|
||||
if (!pricing) return '标准价未提供'
|
||||
const fixed = pricing.standardPrice ? fixedStandardPriceLabel(pricing.standardPrice) : undefined
|
||||
if (fixed) return fixed
|
||||
if (pricing.listPriceCny) {
|
||||
return `${numberLabel(pricing.listPriceCny.input)} 输入 / ${numberLabel(pricing.listPriceCny.output)} 输出 · CNY / 1M(一百万 Token)`
|
||||
}
|
||||
if (pricing.listPriceUsd) {
|
||||
return `${formatUsd(pricing.listPriceUsd.input)} 输入 / ${formatUsd(pricing.listPriceUsd.output)} 输出 · USD / 1M`
|
||||
return `${formatUsd(pricing.listPriceUsd.input)} 输入 / ${formatUsd(pricing.listPriceUsd.output)} 输出 · USD / 1M(一百万 Token)`
|
||||
}
|
||||
const credits = pricing.upstreamCredits ?? pricing.standardPrice?.credits
|
||||
if (credits !== undefined && Number.isFinite(credits)) {
|
||||
@@ -64,10 +68,10 @@ export function standardPriceLabel(pricing?: DisplayPricing) {
|
||||
/** A logical model can have several routes. Do not present one route's list price as universal when they differ. */
|
||||
export function modelStandardPriceLabel(pricings: Array<DisplayPricing | undefined>) {
|
||||
const values = pricings.filter((pricing): pricing is DisplayPricing => !!pricing)
|
||||
const listed = values.filter((pricing) => !!pricing.listPriceUsd || !!pricing.standardPrice)
|
||||
const listed = values.filter((pricing) => !!pricing.listPriceUsd || !!pricing.listPriceCny || !!pricing.standardPrice)
|
||||
if (listed.length > 0 && listed.length < values.length) return '部分分组标准价见详情'
|
||||
if (listed.length > 1) {
|
||||
const keys = new Set(listed.map((pricing) => JSON.stringify({ listPriceUsd: pricing.listPriceUsd, standardPrice: pricing.standardPrice })))
|
||||
const keys = new Set(listed.map((pricing) => JSON.stringify({ listPriceUsd: pricing.listPriceUsd, listPriceCny: pricing.listPriceCny, standardPrice: pricing.standardPrice })))
|
||||
if (keys.size > 1) return '各分组标准价见详情'
|
||||
}
|
||||
return standardPriceLabel(listed[0] || values[0])
|
||||
@@ -76,12 +80,32 @@ export function modelStandardPriceLabel(pricings: Array<DisplayPricing | undefin
|
||||
export function platformPriceLabel(pricing?: DisplayPricing) {
|
||||
if (!pricing) return '价格待配置'
|
||||
if (isTokenUnit(pricing.unit)) {
|
||||
return `${numberLabel(pricing.input)} 输入 / ${numberLabel(pricing.output)} 输出 · ${pricing.unit || 'CNY / 1M tokens'}`
|
||||
const unit = pricing.unit === 'CNY / 1M tokens' ? '人民币 / 1M(一百万 Token)' : pricing.unit || '人民币 / 1M(一百万 Token)'
|
||||
return `${numberLabel(pricing.input)} 输入 / ${numberLabel(pricing.output)} 输出 · ${unit}`
|
||||
}
|
||||
const price = pricing.price ?? pricing.input
|
||||
return price === undefined ? `计费单位:${unitLabel(pricing.unit)}` : `平台售价 ${numberLabel(price)} / ${unitSuffix(pricing)}`
|
||||
}
|
||||
|
||||
/** Explicit admin action. Settlement still uses saved CNY rates, never applies a second discount. */
|
||||
export function calculateTokenRetailPricing(currency: 'CNY' | 'USD', list: { input: number; output: number; cachedInput?: number }, discount: number, exchangeRate?: number) {
|
||||
if (currency !== 'CNY' && currency !== 'USD') throw new Error('请选择人民币或美元原价')
|
||||
if (!Number.isFinite(discount) || discount < 0 || discount > 10) throw new Error('对外折扣须在 0–10 折之间')
|
||||
const fx = currency === 'CNY' ? 1 : exchangeRate
|
||||
if (fx === undefined || !Number.isFinite(fx) || fx <= 0 || fx > 1000) throw new Error('美元原价需要有效的人民币汇率')
|
||||
const scale = 100000000n
|
||||
const scaled = (value: number) => BigInt(value.toFixed(8).replace('.', ''))
|
||||
const amount = (value: number) => {
|
||||
if (!Number.isFinite(value) || value < 0 || value > 1000000) throw new Error('原价须在 0–100 万之间')
|
||||
// Integer arithmetic with HALF_UP rounding to the same eight places as ChatMeter.
|
||||
const divisor = scale * scale * 10n
|
||||
const price = Number((scaled(value) * scaled(discount) * scaled(fx) + divisor / 2n) / divisor) / Number(scale)
|
||||
if (price > 1000000) throw new Error('人民币售价不能超过 100 万')
|
||||
return price
|
||||
}
|
||||
return { input: amount(list.input), output: amount(list.output), cachedInput: list.cachedInput === undefined ? undefined : amount(list.cachedInput), unit: 'CNY / 1M tokens', discount, discountLabel: `${discount}折`, exchangeRate: fx }
|
||||
}
|
||||
|
||||
export function groupDiscountLabel(pricing?: DisplayPricing, configured = true) {
|
||||
if (!configured || !pricing) return '价格待配置'
|
||||
if (pricing.discountLabel && pricing.discountLabel !== '标准价') return pricing.discountLabel
|
||||
|
||||
@@ -39,6 +39,9 @@ import SlidePanel from '../components/SlidePanel.vue'
|
||||
import UiModal from '../components/UiModal.vue'
|
||||
import CatalogAdmin from '../components/CatalogAdmin.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 { usePagination } from '../composables/usePagination'
|
||||
import ListPagination from '../components/ListPagination.vue'
|
||||
@@ -202,7 +205,9 @@ const queueGroups = computed(() => (adminSummary.value?.capacityGroups || []).ma
|
||||
warning: group.running === null || group.routeEnabled === false || !!group.queued,
|
||||
})))
|
||||
const { page: capacityPage, pageSize: capacityPageSize, pagedItems: capacityPageGroups } = usePagination(queueGroups)
|
||||
const configuredCapacity = computed(() => adminSummary.value?.capacityGroups.filter(group => group.routeEnabled !== false).reduce((total, group) => total + group.limit, 0))
|
||||
const configuredCapacityGroups = computed(() => adminSummary.value?.capacityGroups.filter(group => group.routeEnabled !== false) || [])
|
||||
const hasUnlimitedCapacity = computed(() => configuredCapacityGroups.value.some(group => group.limit <= 0))
|
||||
const configuredCapacity = computed(() => hasUnlimitedCapacity.value ? null : configuredCapacityGroups.value.reduce((total, group) => total + group.limit, 0))
|
||||
const pricing = computed(() => models.value.filter((model) => model.pricingConfigured).map((model) => ({
|
||||
model: model.name,
|
||||
unit: model.pricing?.unit || 'CNY / 1M tokens',
|
||||
@@ -280,7 +285,10 @@ function mapModels(items: CatalogModel[], providersList: CatalogProvider[]): Mod
|
||||
|
||||
<template>
|
||||
<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="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>
|
||||
@@ -293,12 +301,12 @@ function mapModels(items: CatalogModel[], providersList: CatalogProvider[]): Mod
|
||||
<template v-else-if="view === 'admin-capacity'">
|
||||
<section class="page-heading"><div><span>CAPACITY CONTROL</span><h1>并发与队列</h1><p>显示实时 Redis 租约、异步任务队列和每个模型分组的配置上限。</p></div><div class="heading-actions"><span class="admin-badge"><ShieldCheck :size="15" />{{ adminSummary?.capacityConfig ? '运行配置已读取' : '运行配置未读取' }}</span><button class="button secondary" @click="refreshOverviewData(true)"><RefreshCcw :size="15" />刷新监控</button></div></section>
|
||||
<section class="capacity-grid">
|
||||
<article class="capacity-main"><div class="capacity-ring"><strong>{{ adminSummary?.running ?? '—' }}</strong><span>实时运行中</span></div><div><small>CONFIGURED CAPACITY</small><h2>{{ configuredCapacity ?? '—' }} 个配置槽位</h2><p>启用分组的上限合计;上游共享限额仍会影响实际吞吐。</p></div></article>
|
||||
<article class="capacity-main"><div class="capacity-ring"><strong>{{ adminSummary?.running ?? '—' }}</strong><span>实时运行中</span></div><div><small>CONFIGURED CAPACITY</small><h2>{{ hasUnlimitedCapacity ? '由渠道限流' : configuredCapacity ?? '—' }}{{ hasUnlimitedCapacity || configuredCapacity === undefined ? '' : ' 个配置槽位' }}</h2><p>启用分组的上限合计;当前为不限时由渠道方的真实并发和限流决定。</p></div></article>
|
||||
<article class="metric-card"><span class="icon-tile orange"><Clock3 :size="18" /></span><div><small>等待任务</small><strong>{{ adminSummary?.waiting ?? '—' }}</strong><em>不含正在执行的请求</em></div></article>
|
||||
<article class="metric-card"><span class="icon-tile cyan"><Timer :size="18" /></span><div><small>同步排队窗口</small><strong>{{ adminSummary?.capacityConfig ? `${adminSummary.capacityConfig.queueWaitMs / 1000}s` : '—' }}</strong><em>读取当前服务器配置</em></div></article>
|
||||
<article class="metric-card"><span class="icon-tile green"><CheckCircle2 :size="18" /></span><div><small>同步占用快照</small><strong>{{ adminSummary?.capacityConfig?.synchronousSnapshotAvailable ? '已读取' : '未读取' }}</strong><em>来自 Redis 有效租约</em></div></article>
|
||||
</section>
|
||||
<p v-if="adminSummary?.capacityConfig" class="billing-note">单 Key 并发上限 {{ adminSummary.capacityConfig.defaultKeyConcurrency }};未单独设置的模型分组默认上限 {{ adminSummary.capacityConfig.defaultModelConcurrency }}。每组异步等待与执行总量上限 {{ adminSummary.capacityConfig.asyncQueueLimit }},任务扫描间隔 {{ adminSummary.capacityConfig.asyncWorkerIntervalMs / 1000 }} 秒。同步与异步分别计数。</p>
|
||||
<p v-if="adminSummary?.capacityConfig" class="billing-note">单 Key 并发上限 {{ adminSummary.capacityConfig.defaultKeyConcurrency }};模型分组守卫 {{ adminSummary.capacityConfig.modelConcurrencyGuardEnabled ? `启用(未设置分组时默认 ${adminSummary.capacityConfig.defaultModelConcurrency})` : '已关闭(测量模式)' }}。每组异步等待与执行总量上限 {{ adminSummary.capacityConfig.asyncQueueLimit }},任务扫描间隔 {{ adminSummary.capacityConfig.asyncWorkerIntervalMs / 1000 }} 秒。同步与异步分别计数。</p>
|
||||
<section class="panel table-card"><header class="panel-header"><div><span>CAPACITY GROUPS</span><h3>并发与排队状态</h3></div><span class="select-button static">{{ queueGroups.length }} 个分组 · 实时快照</span></header><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="group in capacityPageGroups" :key="group.id"><td><div class="stacked-cell"><strong>{{ group.name }}</strong><small>{{ group.type }}</small></div></td><td>{{ group.running ?? '—' }}</td><td>{{ group.limit }}</td><td>{{ group.queued ?? '—' }}</td><td>{{ group.mode === 'async' ? '异步任务队列' : 'Redis 有效租约' }}</td><td><span class="status-chip" :class="group.warning ? 'warning' : 'success'"><i></i>{{ group.status }}</span></td></tr><tr v-if="!queueGroups.length"><td colspan="6"><div class="empty-state compact"><Gauge :size="22" /><strong>{{ adminSummary ? '暂无活跃模型分组' : '容量数据尚未读取' }}</strong><span>启用模型分组后会显示其配置与占用情况</span></div></td></tr></tbody></table></div><ListPagination v-model:page="capacityPage" v-model:page-size="capacityPageSize" :total="queueGroups.length" label="容量分组分页" /></section>
|
||||
</template>
|
||||
|
||||
|
||||
@@ -28,6 +28,8 @@ const mode = ref<AuthMode>('login')
|
||||
const registerChannel = ref<RegisterChannel>('email')
|
||||
const account = ref('')
|
||||
const password = ref('')
|
||||
const otp = ref('')
|
||||
const mfaRequired = ref(false)
|
||||
const confirmPassword = ref('')
|
||||
const invitationCode = ref('')
|
||||
const verificationCode = ref('')
|
||||
@@ -89,10 +91,11 @@ async function submitLogin() {
|
||||
if (!account.value.trim() || !password.value) return emit('notify', '请输入账号和密码')
|
||||
loading.value = true
|
||||
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
|
||||
emit('success', user)
|
||||
} catch (error) {
|
||||
if (error instanceof AuthApiError && error.code === 'MFA_REQUIRED') mfaRequired.value = true
|
||||
loading.value = false
|
||||
emit('notify', error instanceof AuthApiError ? error.message : '登录失败,请稍后重试')
|
||||
}
|
||||
@@ -175,7 +178,7 @@ onMounted(async () => {
|
||||
<i class="auth-orbit orbit-a"></i><i class="auth-orbit orbit-b"></i>
|
||||
<img src="/assets/moyi-logo.png" alt="" />
|
||||
</div>
|
||||
<footer><span :class="`service-${serviceStatus}`"><i></i>{{ serviceStatus === 'online' ? '服务可用' : serviceStatus === 'offline' ? '服务暂不可用' : '正在检测服务' }}</span><small>© 2026 模翎科技</small></footer>
|
||||
<footer><span :class="`service-${serviceStatus}`"><i></i>{{ serviceStatus === 'online' ? '服务可用' : serviceStatus === 'offline' ? '服务暂不可用' : '正在检测服务' }}</span><small>© 2026 模灵科技</small></footer>
|
||||
</section>
|
||||
|
||||
<section class="auth-form-area">
|
||||
@@ -200,6 +203,7 @@ onMounted(async () => {
|
||||
<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"><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>
|
||||
<button class="auth-submit" type="submit" :disabled="loading"><span v-if="loading" class="spinner"></span><template v-else>进入工作台<ArrowRight :size="17" /></template></button>
|
||||
</form>
|
||||
|
||||
@@ -42,6 +42,9 @@ import { buildModelCatalog } from '../services/model-catalog'
|
||||
import { groupDiscountLabel as formatGroupDiscountLabel, modelStandardPriceLabel, platformPriceLabel } from '../services/pricing'
|
||||
import { usePagination } from '../composables/usePagination'
|
||||
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 SlidePanel from '../components/SlidePanel.vue'
|
||||
import UiModal from '../components/UiModal.vue'
|
||||
@@ -130,6 +133,17 @@ const projects = ref<CatalogProject[]>([])
|
||||
const keyScopeType = ref<'personal' | 'project'>('personal')
|
||||
const selectedProjectId = ref('')
|
||||
const selectedApiKeyId = ref('')
|
||||
function scrollDocs(target: string) {
|
||||
const selectors: Record<string, string> = {
|
||||
'docs-overview': '.docs-layout',
|
||||
'docs-auth': '.docs-path-card',
|
||||
'docs-chat': '.docs-detail-grid .docs-detail-card:nth-child(2)',
|
||||
'docs-reference': '.docs-reference-table',
|
||||
'docs-troubleshooting': '.docs-detail-grid .docs-detail-card:last-child',
|
||||
'docs-usage': '.docs-guide-extra',
|
||||
}
|
||||
document.querySelector(selectors[target] || target)?.scrollIntoView({ behavior: 'smooth', block: 'start' })
|
||||
}
|
||||
const activeDebugKeys = computed(() => keys.value.filter((key) => key.status === '正常'))
|
||||
const selectedDebugKey = computed(() => activeDebugKeys.value.find((key) => String(key.id) === selectedApiKeyId.value))
|
||||
const debugKeyCanInvoke = computed(() => !!selectedDebugKey.value?.modelIds.includes(activeModel.value.id))
|
||||
@@ -141,6 +155,7 @@ const activeMediaKind = computed<AsyncMediaType | undefined>(() => {
|
||||
return undefined
|
||||
})
|
||||
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 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)))
|
||||
@@ -162,6 +177,72 @@ const taskPollers = new Map<string, number>()
|
||||
const copyTimers = new Map<string, number>()
|
||||
const copyStates = ref<Record<string, CopyResult | undefined>>({})
|
||||
|
||||
/**
|
||||
* The user portal must still render when an older control-plane node returns a
|
||||
* partial payload. A missing optional field used to throw during Vue render
|
||||
* (for example `series.map` or `asyncSuccessRate.toFixed`) and blanked the
|
||||
* entire workspace for ordinary users. Keep the API contract typed while
|
||||
* normalising the untrusted JSON boundary here.
|
||||
*/
|
||||
function asRecord(value: unknown): Record<string, unknown> {
|
||||
return value && typeof value === 'object' && !Array.isArray(value) ? value as Record<string, unknown> : {}
|
||||
}
|
||||
|
||||
function asText(value: unknown, fallback = ''): string {
|
||||
return typeof value === 'string' ? value : value == null ? fallback : String(value)
|
||||
}
|
||||
|
||||
function asNumber(value: unknown, fallback = 0): number {
|
||||
const number = typeof value === 'number' ? value : Number(value)
|
||||
return Number.isFinite(number) ? number : fallback
|
||||
}
|
||||
|
||||
function optionalNumber(value: unknown): number | undefined {
|
||||
if (value == null || value === '') return undefined
|
||||
const number = Number(value)
|
||||
return Number.isFinite(number) ? number : undefined
|
||||
}
|
||||
|
||||
function asStringArray(value: unknown): string[] {
|
||||
return Array.isArray(value) ? value.filter((item): item is string => typeof item === 'string') : []
|
||||
}
|
||||
|
||||
function normalizeOverview(value: unknown): PlatformOverview {
|
||||
const raw = asRecord(value)
|
||||
const rawTasks = asRecord(raw.tasks)
|
||||
const series = Array.isArray(raw.series) ? raw.series.flatMap((item) => {
|
||||
const row = asRecord(item)
|
||||
const date = asText(row.date)
|
||||
return date ? [{ date, count: asNumber(row.count) }] : []
|
||||
}) : []
|
||||
const ledger = Array.isArray(raw.ledger) ? raw.ledger.flatMap((item) => {
|
||||
const row = asRecord(item)
|
||||
const id = asText(row.id)
|
||||
return id ? [{ id, title: asText(row.title, '额度流水'), amount: asNumber(row.amount), createdAt: asText(row.createdAt) }] : []
|
||||
}) : []
|
||||
return {
|
||||
generatedAt: asText(raw.generatedAt),
|
||||
callsToday: asNumber(raw.callsToday),
|
||||
callsThisMonth: asNumber(raw.callsThisMonth),
|
||||
averageLatencyMs: optionalNumber(raw.averageLatencyMs),
|
||||
availableCredit: optionalNumber(raw.availableCredit),
|
||||
balance: optionalNumber(raw.balance),
|
||||
reservedCredit: optionalNumber(raw.reservedCredit),
|
||||
costThisMonth: optionalNumber(raw.costThisMonth),
|
||||
availabilityPercent: optionalNumber(raw.availabilityPercent),
|
||||
asyncTasks: asNumber(raw.asyncTasks),
|
||||
asyncSuccessRate: optionalNumber(raw.asyncSuccessRate),
|
||||
tasks: {
|
||||
queued: asNumber(rawTasks.queued),
|
||||
running: asNumber(rawTasks.running),
|
||||
succeeded: asNumber(rawTasks.succeeded),
|
||||
failed: asNumber(rawTasks.failed),
|
||||
},
|
||||
series,
|
||||
ledger,
|
||||
}
|
||||
}
|
||||
|
||||
function selectDebugApiKey(id: string) {
|
||||
selectedApiKeyId.value = id
|
||||
}
|
||||
@@ -169,15 +250,17 @@ function selectDebugApiKey(id: string) {
|
||||
onMounted(async () => {
|
||||
try {
|
||||
const [catalog, keyResult, projectResult, overviewResult] = await Promise.all([catalogClient.listModels(), catalogClient.listKeys(), catalogClient.listProjects(), catalogClient.getOverview()])
|
||||
// Treat every API collection as untrusted JSON. This keeps a partially
|
||||
// migrated control plane from taking down the whole authenticated shell.
|
||||
catalogModels.value = mapCatalogModels(catalog.models, catalog.providers)
|
||||
catalogDefinitions.value = catalog.logicalModels
|
||||
catalogRoutes.value = catalog.models
|
||||
catalogDefinitions.value = Array.isArray(catalog.logicalModels) ? catalog.logicalModels : []
|
||||
catalogRoutes.value = Array.isArray(catalog.models) ? catalog.models : []
|
||||
if (catalogModels.value[0]) selectPlaygroundGroup(catalogModels.value[0].id)
|
||||
keys.value = keyResult.keys.map((key) => mapCatalogKey(key))
|
||||
keys.value = (Array.isArray(keyResult.keys) ? keyResult.keys : []).map((key) => mapCatalogKey(key))
|
||||
selectedApiKeyId.value = String(keys.value.find((key) => key.status === '正常')?.id || '')
|
||||
projects.value = projectResult.projects
|
||||
overview.value = overviewResult
|
||||
emit('balance', overviewResult.availableCredit)
|
||||
projects.value = Array.isArray(projectResult.projects) ? projectResult.projects : []
|
||||
overview.value = normalizeOverview(overviewResult)
|
||||
emit('balance', overview.value.availableCredit)
|
||||
overviewLoaded.value = true
|
||||
catalogConnected.value = true
|
||||
try {
|
||||
@@ -193,6 +276,7 @@ onMounted(async () => {
|
||||
keys.value = []
|
||||
projects.value = []
|
||||
activeModel.value = emptyModel
|
||||
overview.value = normalizeOverview(undefined)
|
||||
overviewLoaded.value = false
|
||||
catalogConnected.value = false
|
||||
}
|
||||
@@ -305,7 +389,7 @@ function resetModelFilters() {
|
||||
|
||||
async function refreshOverview() {
|
||||
try {
|
||||
overview.value = await catalogClient.getOverview()
|
||||
overview.value = normalizeOverview(await catalogClient.getOverview())
|
||||
emit('balance', overview.value.availableCredit)
|
||||
overviewLoaded.value = true
|
||||
} catch {}
|
||||
@@ -347,7 +431,10 @@ async function runModel() {
|
||||
finishReason.value = ''
|
||||
const selectedMode = callMode.value
|
||||
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 taskItem = mapAsyncTask(task, activeModel.value, prompt.value)
|
||||
tasks.value.unshift(taskItem)
|
||||
@@ -513,14 +600,26 @@ async function createKey() {
|
||||
}
|
||||
}
|
||||
|
||||
function mapCatalogModels(items: CatalogModel[], providers: Array<{ id: string; name: string }>): ModelItem[] {
|
||||
const providerNames = new Map(providers.map((provider) => [provider.id, provider.name]))
|
||||
return items.map(item => ({
|
||||
function mapCatalogModels(items: unknown, providers: unknown): ModelItem[] {
|
||||
const providerRows = Array.isArray(providers) ? providers : []
|
||||
const providerNames = new Map(providerRows.map((provider) => {
|
||||
const row = asRecord(provider)
|
||||
return [asText(row.id), asText(row.name)] as const
|
||||
}).filter(([id]) => !!id))
|
||||
const modelRows = Array.isArray(items) ? items : []
|
||||
return modelRows.map(item => {
|
||||
const row = asRecord(item)
|
||||
const model = item as Partial<CatalogModel>
|
||||
const providerId = asText(row.providerId)
|
||||
const inputTypes = asStringArray(row.inputTypes)
|
||||
const outputTypes = asStringArray(row.outputTypes)
|
||||
return {
|
||||
...mapCatalogModel(item), logicalModelId: item.logicalModelId, gatewayModelId: item.gatewayModelId,
|
||||
provider: providerNames.get(item.providerId) || item.providerId,
|
||||
vendorName: item.vendorName,
|
||||
sourceLabel: item.sourceLabel, pricing: item.pricing, pricingConfigured: item.pricingConfigured, billingReady: item.billingReady, priority: item.priority, maxConcurrency: item.maxConcurrency, executionReady: item.executionReady, inputTypes: item.inputTypes, outputTypes: item.outputTypes,
|
||||
}))
|
||||
provider: providerNames.get(providerId) || providerId || '未知渠道',
|
||||
vendorName: model.vendorName,
|
||||
sourceLabel: model.sourceLabel, pricing: model.pricing, pricingConfigured: model.pricingConfigured, billingReady: model.billingReady, priority: model.priority, maxConcurrency: model.maxConcurrency, executionReady: model.executionReady, inputTypes, outputTypes,
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -533,25 +632,31 @@ function gatewayModelId(group: ModelItem): string {
|
||||
return group.gatewayModelId || group.modelKey || group.id
|
||||
}
|
||||
|
||||
function mapCatalogModel(item: LogicalModel): ModelItem {
|
||||
function mapCatalogModel(item: Partial<LogicalModel> | Partial<CatalogModel>): ModelItem {
|
||||
const typeLabels: Record<string, ModelType> = { text: '文本', code: '文本', multimodal: '多模态', image: '图像', audio: '音频', video: '视频', embedding: '文本', '3d': '3D' }
|
||||
const modeLabels: Record<string, CallMode> = { sync: '同步', stream: '流式', async: '异步' }
|
||||
const row = item as Record<string, unknown>
|
||||
const id = asText(row.id)
|
||||
const type = asText(row.type, 'text')
|
||||
const mode = asText(row.mode, 'sync')
|
||||
const inputTypes = asStringArray(row.inputTypes)
|
||||
const outputTypes = asStringArray(row.outputTypes)
|
||||
return {
|
||||
id: item.id,
|
||||
name: item.publicName,
|
||||
id,
|
||||
name: asText(row.publicName, asText(row.modelKey, id || '未命名模型')),
|
||||
provider: '',
|
||||
vendorName: item.vendorName,
|
||||
type: typeLabels[item.type] || '文本',
|
||||
mode: modeLabels[item.mode] || '同步',
|
||||
description: item.description,
|
||||
latency: item.mode === 'async' ? '异步任务' : '待采样',
|
||||
specification: item.outputTypes.join(' / ') || '统一接口',
|
||||
inputTypes: item.inputTypes,
|
||||
outputTypes: item.outputTypes,
|
||||
availability: item.status === 'active' ? '在线' : '已停用',
|
||||
accent: item.type === '3d' ? 'orange' : 'violet',
|
||||
icon: item.type === '3d' ? Box : item.type === 'image' ? Sparkles : MessageSquareText,
|
||||
modelKey: item.modelKey,
|
||||
vendorName: asText(row.vendorName) || undefined,
|
||||
type: typeLabels[type] || '文本',
|
||||
mode: modeLabels[mode] || '同步',
|
||||
description: asText(row.description),
|
||||
latency: mode === 'async' ? '异步任务' : '待采样',
|
||||
specification: outputTypes.join(' / ') || '统一接口',
|
||||
inputTypes,
|
||||
outputTypes,
|
||||
availability: asText(row.status, 'active') === 'active' ? '在线' : '已停用',
|
||||
accent: type === '3d' ? 'orange' : 'violet',
|
||||
icon: type === '3d' ? Box : type === 'image' ? Sparkles : MessageSquareText,
|
||||
modelKey: asText(row.modelKey) || undefined,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -563,17 +668,24 @@ function keyPermissionSummary(key: ApiKeyItem) {
|
||||
}
|
||||
|
||||
function mapCatalogKey(key: CatalogApiKey, plaintext = ''): ApiKeyItem {
|
||||
const raw = key as Partial<CatalogApiKey>
|
||||
const id = asText(raw.id)
|
||||
const keyName = asText(raw.name, '未命名 Key')
|
||||
const keyPrefix = asText(raw.keyPrefix, 'moyi_')
|
||||
const modelIds = asStringArray(raw.modelIds)
|
||||
const createdAt = asText(raw.createdAt)
|
||||
const lastUsedAt = asText(raw.lastUsedAt)
|
||||
return {
|
||||
id: key.id,
|
||||
name: key.name,
|
||||
value: plaintext ? `${plaintext.slice(0, 10)}••••••••${plaintext.slice(-4)}` : `${key.keyPrefix}••••••••••••`,
|
||||
scope: key.scopeType === 'project' ? `项目 Key${key.projectId ? ` · ${key.projectId.slice(0, 12)}` : ''}` : '个人 Key',
|
||||
scopeType: key.scopeType,
|
||||
projectId: key.projectId,
|
||||
modelIds: [...key.modelIds],
|
||||
createdAt: key.createdAt.slice(0, 10),
|
||||
lastUsed: key.lastUsedAt ? key.lastUsedAt.slice(0, 16).replace('T', ' ') : '尚未使用',
|
||||
status: key.status === 'active' ? '正常' : '已停用',
|
||||
id,
|
||||
name: keyName,
|
||||
value: plaintext ? `${plaintext.slice(0, 10)}••••••••${plaintext.slice(-4)}` : `${keyPrefix}••••••••••••`,
|
||||
scope: raw.scopeType === 'project' ? `项目 Key${raw.projectId ? ` · ${String(raw.projectId).slice(0, 12)}` : ''}` : '个人 Key',
|
||||
scopeType: raw.scopeType === 'project' ? 'project' : 'personal',
|
||||
projectId: raw.projectId,
|
||||
modelIds,
|
||||
createdAt: createdAt ? createdAt.slice(0, 10) : '未知时间',
|
||||
lastUsed: lastUsedAt ? lastUsedAt.slice(0, 16).replace('T', ' ') : '尚未使用',
|
||||
status: raw.status === 'active' ? '正常' : '已停用',
|
||||
}
|
||||
}
|
||||
|
||||
@@ -663,7 +775,9 @@ async function createProject() {
|
||||
</script>
|
||||
|
||||
<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'">
|
||||
<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>
|
||||
|
||||
@@ -771,7 +885,7 @@ async function createProject() {
|
||||
|
||||
<template v-else>
|
||||
<section class="page-heading"><div><span>DEVELOPER DOCS</span><h1>接入文档</h1><p>使用标准接口把模型能力接入你的应用。</p></div><button class="button secondary" @click="copy(platformApiBaseUrl, 'Base URL', 'docs-base-url')"><Check v-if="copySucceeded('docs-base-url')" :size="16" /><Copy v-else :size="16" />{{ copySucceeded('docs-base-url') ? '已复制' : copyStates['docs-base-url'] === 'failed' ? '复制失败' : '复制 Base URL' }}</button></section>
|
||||
<section class="docs-layout"><aside class="docs-menu panel"><span>快速开始</span><button class="active">概览</button><button>认证与 API Key</button><button>标准对话接口</button><button>异步任务</button><button>错误码与重试</button><span>API 参考</span><button>模型列表</button><button>用量查询</button></aside><article class="docs-content panel"><span class="eyebrow">QUICK START</span><h2>三分钟完成第一次调用</h2><p>只需要统一 API 地址、API Key 和模型名称,即可开始使用模亦灵枢。</p><section><b>01</b><div><h3>准备接入信息</h3><p>在用户端创建 API Key,并保存以下环境变量。</p><pre><code>MOYI_BASE_URL={{ platformApiBaseUrl }}
|
||||
<section id="docs-overview" class="docs-layout"><aside class="docs-menu panel"><span>快速开始</span><button class="active" type="button" @click="scrollDocs('docs-overview')">概览</button><button type="button" @click="scrollDocs('docs-auth')">认证与 API Key</button><button type="button" @click="scrollDocs('docs-chat')">标准对话接口</button><button type="button" @click="scrollDocs('docs-reference')">异步任务</button><button type="button" @click="scrollDocs('docs-troubleshooting')">错误码与重试</button><span>API 参考</span><button type="button" @click="scrollDocs('docs-reference')">模型列表</button><button type="button" @click="scrollDocs('docs-usage')">用量查询</button></aside><article class="docs-content panel"><span class="eyebrow">QUICK START</span><h2>三分钟完成第一次调用</h2><p>只需要统一 API 地址、API Key 和模型名称,即可开始使用模亦灵枢。</p><section><b>01</b><div><h3>准备接入信息</h3><p>在用户端创建 API Key,并保存以下环境变量。</p><pre><code>MOYI_BASE_URL={{ platformApiBaseUrl }}
|
||||
MOYI_API_KEY=sk-moyi-your-key
|
||||
MOYI_MODEL=<model-id></code></pre></div></section><section><b>02</b><div><h3>发送标准请求</h3><p>文本模型支持同步和流式响应;3D、图像、视频使用异步任务接口。</p><pre><code>curl {{ platformApiBaseUrl }}/chat/completions \
|
||||
-H "Authorization: Bearer $MOYI_API_KEY" \
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { test } from 'node:test'
|
||||
import assert from 'node:assert/strict'
|
||||
import { groupDiscountLabel, modelStandardPriceLabel, platformPriceLabel, standardPriceLabel } from '../src/services/pricing.ts'
|
||||
import { calculateTokenRetailPricing, groupDiscountLabel, modelStandardPriceLabel, platformPriceLabel, standardPriceLabel } from '../src/services/pricing.ts'
|
||||
|
||||
test('fixed-unit pricing shows platform task price without inventing a standard price or discount', () => {
|
||||
const tripo = { price: 1.44, unit: 'CNY / task', upstreamCredits: 20, exchangeRate: 7.2, priceSource: 'Tripo 官方', standardPrice: { price: 0.2, unit: 'USD / task', credits: 20 } }
|
||||
@@ -18,11 +18,28 @@ test('official standard price source is displayed, while a standard-price marker
|
||||
|
||||
test('token pricing retains explicit source list price and discount', () => {
|
||||
const pricing = { input: 1.2, output: 4.8, unit: 'CNY / 1M tokens', discount: 6.5, discountLabel: '6.5折', listPriceUsd: { input: 1, output: 4 } }
|
||||
assert.equal(standardPriceLabel(pricing), '$1 输入 / $4 输出 · USD / 1M')
|
||||
assert.equal(platformPriceLabel(pricing), '¥1.2 输入 / ¥4.8 输出 · CNY / 1M tokens')
|
||||
assert.equal(standardPriceLabel(pricing), '$1 输入 / $4 输出 · USD / 1M(一百万 Token)')
|
||||
assert.equal(platformPriceLabel(pricing), '¥1.2 输入 / ¥4.8 输出 · 人民币 / 1M(一百万 Token)')
|
||||
assert.equal(groupDiscountLabel(pricing), '6.5折')
|
||||
})
|
||||
|
||||
test('CNY source keeps its currency and never multiplies the USD exchange rate', () => {
|
||||
const result = calculateTokenRetailPricing('CNY', {input: 0.8, output: 2.8, cachedInput: 0.23}, 3, 7.2)
|
||||
assert.deepEqual([result.input, result.output, result.cachedInput, result.exchangeRate], [0.24, 0.84, 0.069, 1])
|
||||
assert.equal(standardPriceLabel({listPriceCny:{input:0.8,output:2.8}}), '¥0.8 输入 / ¥2.8 输出 · CNY / 1M(一百万 Token)')
|
||||
assert.equal(modelStandardPriceLabel([{listPriceUsd:{input:1,output:4}},{listPriceCny:{input:1,output:4}}]), '各分组标准价见详情')
|
||||
})
|
||||
|
||||
test('retail discount is in tenths; USD conversion and eight-place rounding happen once', () => {
|
||||
const result = calculateTokenRetailPricing('USD', {input:1.25, output:10, cachedInput:0.125}, 3.5, 7.2)
|
||||
assert.deepEqual([result.input,result.output,result.cachedInput],[3.15,25.2,0.315])
|
||||
assert.equal(calculateTokenRetailPricing('CNY',{input:0.00000001,output:0},5).input,0.00000001)
|
||||
assert.equal(calculateTokenRetailPricing('CNY',{input:0,output:0,cachedInput:0},10).cachedInput,0)
|
||||
assert.throws(()=>calculateTokenRetailPricing('USD',{input:1,output:1},5),/汇率/)
|
||||
assert.throws(()=>calculateTokenRetailPricing('CNY',{input:1,output:1},55),/折扣/)
|
||||
assert.throws(()=>calculateTokenRetailPricing('CNY',{input:NaN,output:1},5),/原价/)
|
||||
})
|
||||
|
||||
test('different route list prices are not presented as one universal model price', () => {
|
||||
assert.equal(modelStandardPriceLabel([
|
||||
{ unit: 'CNY / 1M tokens', listPriceUsd: { input: 1, output: 4 } },
|
||||
|
||||
@@ -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)
|
||||
New-Item -ItemType Directory -Path $staging | Out-Null
|
||||
# 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)
|
||||
$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) {
|
||||
if ($relative -match '(^|/)(\.env\.cloud|\.deploy-mode|certs)(/|$)') { throw 'Sensitive path in release allowlist.' }
|
||||
$destination = Join-Path $staging $relative
|
||||
|
||||
@@ -0,0 +1,156 @@
|
||||
"""Build a reviewable, guarded pricing migration from explicit source snapshots.
|
||||
|
||||
No network access, database writes or execution of supplier pricing expressions.
|
||||
The old and new price JSON must match before a migration can update a route.
|
||||
"""
|
||||
import argparse
|
||||
import copy
|
||||
import json
|
||||
import re
|
||||
from decimal import Decimal, ROUND_HALF_UP
|
||||
from pathlib import Path
|
||||
|
||||
D = lambda x: Decimal(str(x))
|
||||
API_DISCOUNT = {'claude': '4.6', 'gemini': '4.6', 'gpt': '4.6', 'chatgpt': '4.6', 'codex': '4.6', 'grok': '4.6', 'glm': '5.5', 'kimi': '5.5', 'minimax': '5.5', 'qwen': '5.5', 'deepseek': '5.5', 'doubao': '9'}
|
||||
|
||||
def retail_price(prices, currency, purchase_discount, exchange_rate='7.2'):
|
||||
if currency not in ('USD', 'CNY'):
|
||||
raise ValueError('Source currency must be explicit USD or CNY')
|
||||
if not D(0) <= D(purchase_discount) <= D(9):
|
||||
raise ValueError('Purchase discount plus one must be within 0-10')
|
||||
fx = D(1) if currency == 'CNY' else D(exchange_rate)
|
||||
if not D(0) < fx <= D(1000):
|
||||
raise ValueError('Invalid exchange rate')
|
||||
discount = D(purchase_discount) + D(1)
|
||||
result = {'unit':'CNY / 1M tokens', 'discount':float(discount), 'discountLabel':f'{discount.normalize()}折', 'exchangeRate':float(fx)}
|
||||
listed = {}
|
||||
for field in ('input', 'output', 'cachedInput'):
|
||||
value = prices.get(field)
|
||||
if value is None:
|
||||
if field != 'cachedInput':
|
||||
raise ValueError('Both input and output list rates are required')
|
||||
continue
|
||||
value = D(value)
|
||||
if not value.is_finite() or value < 0:
|
||||
raise ValueError('Invalid source price')
|
||||
amount = (value * fx * discount / 10).quantize(D('0.00000001'), rounding=ROUND_HALF_UP)
|
||||
if amount > 1000000:
|
||||
raise ValueError('Retail rate exceeds the billing limit')
|
||||
listed[field] = float(value)
|
||||
result[field] = float(amount)
|
||||
result['listPriceUsd' if currency == 'USD' else 'listPriceCny'] = listed
|
||||
return result
|
||||
|
||||
def normalize_model(value):
|
||||
# Only punctuation normalization supported by explicit matching quote rows.
|
||||
return value.lower().replace('.', '-')
|
||||
|
||||
def source_rate(item):
|
||||
if item.get('billing_mode') == 'tiered_expr':
|
||||
expr = item.get('billing_expr', '')
|
||||
# A single literal tier is a fixed rate. Never eval provider expressions.
|
||||
match = re.fullmatch(r'tier\("[^"]+",\s*([^()]+)\)', expr)
|
||||
if not match:
|
||||
return None
|
||||
fields = dict(re.findall(r'\b(p|c|cr)\s*\*\s*([0-9.]+)', match[1]))
|
||||
if not {'p','c'}.issubset(fields):
|
||||
return None
|
||||
return {'input':fields['p'], 'output':fields['c'], 'cachedInput':fields.get('cr')}
|
||||
if item.get('quota_type') != 0:
|
||||
return None
|
||||
base = D(item['model_ratio']) * 2
|
||||
return {'input':base, 'output':base*D(item['completion_ratio']), 'cachedInput':base*D(item['cache_ratio']) if item.get('cache_ratio') is not None else None}
|
||||
|
||||
def build(directory):
|
||||
read = lambda name: json.loads((directory/name).read_text(encoding='utf-8'))
|
||||
catalog = read('catalog-audit-source.json')
|
||||
ready = {r['id'] for r in read('ready-audit.json')}
|
||||
quote = {normalize_model(r[0]):r for r in read('supplier-quote-source.json')['报价单'][3:] if r[0]}
|
||||
source = {p:{r['model_name']:r for r in read(p+'-pricing-recheck.json')['data']} for p in ('apipro','tokenfoundryx')}
|
||||
hubs = {}
|
||||
for file in ('apihub-page1.txt','apihub-page2.txt'):
|
||||
for block in re.split(r'\d+ heading ', (directory/file).read_text(encoding='utf-8'))[1:]:
|
||||
name=block.split(', Value:')[0]
|
||||
line=next((line for line in block.splitlines() if 'text 输入 ¥' in line), None)
|
||||
if not line or '动态计费' in block:
|
||||
continue
|
||||
nums=re.findall(r'¥([\d.]+)',line)
|
||||
if len(nums) >= 4:
|
||||
hubs[name]={'input':nums[1],'output':nums[3],'cachedInput':nums[5] if len(nums)>5 else None}
|
||||
rows=[]
|
||||
for row in catalog:
|
||||
p=row['provider'];model=row['model'];old=row.get('pricing') or {}
|
||||
if old.get('unit') != 'CNY / 1M tokens':
|
||||
continue
|
||||
result={**row,'ready':row['id'] in ready,'oldPricing':old,'newPricing':copy.deepcopy(old),'purchaseDiscount':None,'state':'保留现价','reason':'','sourceCurrency':None}
|
||||
q=quote.get(normalize_model(model))
|
||||
discount=None;rate=None;currency=None
|
||||
if p=='apipro':
|
||||
family=next((family for family in API_DISCOUNT if re.match(re.escape(family)+r'(?:[-.\d]|$)',model.lower())),None)
|
||||
discount=API_DISCOUNT.get(family)
|
||||
if model.startswith('text-embedding-'): discount='4.6'
|
||||
if re.match(r'^o[134](?:-|$)',model): discount='4.6'
|
||||
item=source[p].get(model)
|
||||
result['source']='https://api.apipro.ai/api/pricing'
|
||||
if item: rate=source_rate(item)
|
||||
currency='USD'
|
||||
if not item: result['reason']='当前渠道目录未匹配同名模型,保留现价,待确认映射。'
|
||||
elif rate is None: result['reason']='上游含动态/阶梯或非 Token 规则,保留平台固定现价,完整条件待复核。';result['billingExpr']=item.get('billing_expr','')
|
||||
elif p in ('apihub','tokenfoundryx'):
|
||||
if q: discount=q[6].replace('折','');result['quoteModel']=q[0];result['quoteList']={'input':q[3],'output':q[4],'cachedInput':q[5]}
|
||||
if p=='apihub': rate=hubs.get(model);currency='CNY';result['source']='https://apihub.chem-cloud.cn/pricing'
|
||||
else:
|
||||
item=source[p].get(model)
|
||||
rate=source_rate(item) if item else None;currency='USD';result['source']='https://www.tokenfoundryx.com/api/pricing'
|
||||
if not rate: result['reason']='当前原价未核实或为动态计费,保留现价。'
|
||||
else: result['reason']='不在本次三个中转渠道折扣范围。'
|
||||
result['purchaseDiscount']=float(discount) if discount is not None else None
|
||||
result['sourceCurrency']=currency
|
||||
if discount is None: result['reason']+=' 未找到用户确认的该模型采购折扣,不按相近模型猜测。'
|
||||
if rate is not None and discount is not None:
|
||||
computed=retail_price(rate,currency,discount)
|
||||
oldout=D(old.get('output',0))
|
||||
if oldout>0 and D(computed['output'])/oldout >= 100:
|
||||
result['reason']='当前渠道输出原价比历史值高至少 100 倍,疑似渠道异常配置;不自动涨价。'
|
||||
result['candidatePricing']=computed
|
||||
else:
|
||||
new=copy.deepcopy(old)
|
||||
for key in ('listPriceUsd','listPriceCny','cachedInput'):
|
||||
new.pop(key,None)
|
||||
new.update(computed)
|
||||
new['priceSource']=('ApiHub 当前人民币原价' if p=='apihub' else ('APIPro' if p=='apipro' else 'TokenFoundryX')+' 当前美元原价')+' · 2026-10-12'
|
||||
result['newPricing']=new
|
||||
fields=('input','output','cachedInput','discount','exchangeRate','listPriceUsd','listPriceCny')
|
||||
changed=any(old.get(k)!=new.get(k) for k in fields)
|
||||
result['state']='修正' if changed else '一致'
|
||||
result['reason']='按渠道当前原价与币种,采购折扣加 1 个折扣点;美元按 7.2 换算一次。'
|
||||
if q and p=='tokenfoundryx' and q[3] and D(rate['input'])/D(q[3]) >= 10:
|
||||
result['reason']+=' 渠道当前原价明显高于历史报价表,已按当前网页计算,需向渠道复核异常高原价。'
|
||||
rows.append(result)
|
||||
return rows
|
||||
|
||||
def migration(rows):
|
||||
changed=[r for r in rows if r['state']=='修正']
|
||||
def sql(value): return "'"+json.dumps(value,ensure_ascii=False,separators=(',',':')).replace("'","''")+"'::jsonb"
|
||||
values=[]
|
||||
for r in changed:
|
||||
values.append(" ('"+r['provider'].replace("'","''")+"', '"+r['model'].replace("'","''")+"', "+sql(r['oldPricing'])+", "+sql(r['newPricing'])+")")
|
||||
return ("-- Verified 2026-10-12: current source currency; purchase discount + 1 point.\n"
|
||||
"-- Exact old JSON guard preserves later manual edits. Does not change enabled status, credentials or balances.\n"
|
||||
"-- Dynamic, missing-source and extreme-increase rows are deliberately excluded.\n"
|
||||
"WITH correction(provider_code, upstream_model, old_pricing, new_pricing) AS (VALUES\n"+',\n'.join(values)+"\n)\n"
|
||||
"UPDATE model_catalog m SET pricing=c.new_pricing, updated_at=now()\n"
|
||||
"FROM model_providers p, correction c\n"
|
||||
"WHERE m.provider_id=p.id AND p.code=c.provider_code AND m.upstream_model=c.upstream_model\n"
|
||||
"AND m.deleted_at IS NULL AND p.deleted_at IS NULL AND m.pricing=c.old_pricing;\n")
|
||||
|
||||
if __name__=='__main__':
|
||||
parser=argparse.ArgumentParser()
|
||||
parser.add_argument('--sources',type=Path,required=True)
|
||||
parser.add_argument('--migration',type=Path,required=True)
|
||||
args=parser.parse_args()
|
||||
rows=build(args.sources)
|
||||
(args.sources/'pricing-corrections.json').write_text(json.dumps(rows,ensure_ascii=False,indent=2),encoding='utf-8')
|
||||
args.migration.write_text(migration(rows),encoding='utf-8')
|
||||
from collections import Counter
|
||||
print(json.dumps({'all':dict(Counter(r['state'] for r in rows)),'ready':dict(Counter(r['state'] for r in rows if r['ready']))},ensure_ascii=False))
|
||||
@@ -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')
|
||||
@@ -0,0 +1,57 @@
|
||||
"""Exercise the pricing correction against an isolated local PostgreSQL cluster."""
|
||||
import argparse
|
||||
import json
|
||||
import socket
|
||||
import subprocess
|
||||
import tempfile
|
||||
from pathlib import Path
|
||||
|
||||
parser=argparse.ArgumentParser()
|
||||
parser.add_argument('--sources',type=Path,required=True)
|
||||
parser.add_argument('--postgres-bin',type=Path,required=True)
|
||||
args=parser.parse_args()
|
||||
repo=Path(__file__).resolve().parent.parent
|
||||
folder=Path(tempfile.mkdtemp(prefix='pricing-migration-',dir=repo/'.tools'))
|
||||
bin=args.postgres_bin
|
||||
with socket.socket() as sock:
|
||||
sock.bind(('127.0.0.1',0));port=sock.getsockname()[1]
|
||||
def run(command):
|
||||
# PostgreSQL's Windows child can inherit stdout pipes and keep communicate()
|
||||
# waiting after pg_ctl exits. A file avoids inheriting a pipe reader.
|
||||
with tempfile.TemporaryFile(mode='w+b') as output:
|
||||
subprocess.run([str(x) for x in command],check=True,stdout=output,stderr=output,timeout=90)
|
||||
output.seek(0)
|
||||
return output.read().decode('utf-8',errors='replace')
|
||||
def sql_literal(value):
|
||||
return "'"+value.replace("'","''")+"'"
|
||||
def sql_json(value):
|
||||
return sql_literal(json.dumps(value,ensure_ascii=False,separators=(',',':')))+'::jsonb'
|
||||
rows=json.loads((args.sources/'pricing-corrections.json').read_text(encoding='utf8'))
|
||||
changed=[r for r in rows if r['state']=='修正']
|
||||
sql=['CREATE TABLE model_providers(id text PRIMARY KEY,code text,deleted_at timestamptz);',
|
||||
'CREATE TABLE model_catalog(id text PRIMARY KEY,provider_id text,upstream_model text,pricing jsonb,status text,deleted_at timestamptz,updated_at timestamptz);']
|
||||
for provider in sorted({r['provider'] for r in rows}):
|
||||
sql.append(f'INSERT INTO model_providers VALUES ({sql_literal(provider)},{sql_literal(provider)},NULL);')
|
||||
for row in rows:
|
||||
sql.append('INSERT INTO model_catalog VALUES ('+','.join([sql_literal(row['id']),sql_literal(row['provider']),sql_literal(row['model']),sql_json(row['oldPricing']),sql_literal(row['status']),'NULL','now()'])+');')
|
||||
sample=changed[0]
|
||||
sql.append('INSERT INTO model_catalog SELECT \'manual-edit\',provider_id,upstream_model,\'{"input":123,"unit":"CNY / 1M tokens"}\'::jsonb,\'disabled\',NULL,now() FROM model_catalog WHERE id='+sql_literal(sample['id'])+';')
|
||||
sql.append('CREATE TEMP TABLE before_rows AS SELECT * FROM model_catalog;')
|
||||
migration=(repo/'control-plane-java/src/main/resources/db/migration/V34__reconcile_source_currency_and_retail_discounts.sql').read_text(encoding='utf8')
|
||||
sql.append(migration)
|
||||
for row in rows:
|
||||
expected=row['newPricing'] if row['state']=='修正' else row['oldPricing']
|
||||
sql.append('DO $$ BEGIN IF NOT EXISTS (SELECT 1 FROM model_catalog WHERE id='+sql_literal(row['id'])+' AND pricing='+sql_json(expected)+') THEN RAISE EXCEPTION \'Incorrect migrated price\'; END IF; END $$;')
|
||||
sql.append("DO $$ BEGIN IF EXISTS (SELECT 1 FROM model_catalog m JOIN before_rows b USING(id) WHERE m.status<>b.status) OR NOT EXISTS (SELECT 1 FROM model_catalog WHERE id='manual-edit' AND pricing->>'input'='123') THEN RAISE EXCEPTION 'Manual edit or status was changed'; END IF; END $$;")
|
||||
sql.append('CREATE TEMP TABLE first_pass AS SELECT * FROM model_catalog;')
|
||||
sql.append(migration)
|
||||
sql.append("DO $$ BEGIN IF EXISTS (SELECT 1 FROM model_catalog m JOIN first_pass f USING(id) WHERE m.pricing IS DISTINCT FROM f.pricing OR m.updated_at IS DISTINCT FROM f.updated_at) THEN RAISE EXCEPTION 'Not idempotent'; END IF; END $$;")
|
||||
sql.append("SELECT 'Pricing migration verified' AS result;")
|
||||
script=folder/'verify.sql';script.write_text('\n'.join(sql),encoding='utf8')
|
||||
run([bin/'initdb.exe','-D',folder/'data','-U','pricing_test','-A','trust','--encoding=UTF8','--locale=C'])
|
||||
try:
|
||||
run([bin/'pg_ctl.exe','-D',folder/'data','-l',folder/'postgres.log','-o',f'-p {port} -h 127.0.0.1','-w','start'])
|
||||
out=run([bin/'psql.exe','-h','127.0.0.1','-p',str(port),'-U','pricing_test','-d','postgres','-v','ON_ERROR_STOP=1','-f',script])
|
||||
print(f'PASS: {len(changed)} corrections; {len(rows)} prices checked; manual-edit guard, status preservation and idempotence verified.')
|
||||
finally:
|
||||
run([bin/'pg_ctl.exe','-D',folder/'data','-w','stop','-m','fast'])
|
||||
@@ -0,0 +1,31 @@
|
||||
import unittest
|
||||
from importlib.util import spec_from_file_location, module_from_spec
|
||||
from pathlib import Path
|
||||
|
||||
spec=spec_from_file_location('pricing',Path(__file__).with_name('reconcile-pricing.py'))
|
||||
p=module_from_spec(spec)
|
||||
spec.loader.exec_module(p)
|
||||
|
||||
class PricingTests(unittest.TestCase):
|
||||
def test_cny_no_fx_and_plus_one(self):
|
||||
r=p.retail_price({'input':'.8','output':'2.8','cachedInput':'.23'},'CNY','2')
|
||||
self.assertEqual((r['input'],r['output'],r['cachedInput'],r['discount'],r['exchangeRate']),(.24,.84,.069,3,1))
|
||||
self.assertNotIn('listPriceUsd',r)
|
||||
def test_usd_and_zero(self):
|
||||
r=p.retail_price({'input':'1.25','output':'10','cachedInput':'0'},'USD','2.5')
|
||||
self.assertEqual((r['input'],r['output'],r['cachedInput']),(3.15,25.2,0))
|
||||
self.assertEqual(p.retail_price({'input':1,'output':2},'USD',9)['discount'],10)
|
||||
def test_half_up(self):
|
||||
self.assertEqual(p.retail_price({'input':'.00000001','output':0},'CNY',4)['input'],.00000001)
|
||||
def test_reject_ambiguous(self):
|
||||
for currency,discount in [('RMB',2),('USD',55)]:
|
||||
with self.assertRaises(ValueError):p.retail_price({'input':1,'output':1},currency,discount)
|
||||
def test_dynamic_not_evaluated(self):
|
||||
self.assertIsNone(p.source_rate({'billing_mode':'tiered_expr','billing_expr':'len < 10 ? tier("x",p*1+c*2) : tier("y",p*2+c*4)'}))
|
||||
self.assertEqual(p.source_rate({'billing_mode':'tiered_expr','billing_expr':'tier("standard", p * 10 + c * 50 + cr * 1 + cc * 12.5)'}),{'input':'10','output':'50','cachedInput':'1'})
|
||||
def test_migration_guard(self):
|
||||
sql=p.migration([{'state':'修正','provider':'x','model':'a','oldPricing':{'input':1},'newPricing':{'input':2}}])
|
||||
self.assertIn('m.pricing=c.old_pricing',sql)
|
||||
self.assertNotIn('status=',sql)
|
||||
|
||||
if __name__=='__main__':unittest.main()
|
||||
Reference in new issue
Block a user