feat: extend enterprise gateway policies billing protocols and operations
This commit is contained in:
1 parent
1528b71882
commit
af43947b5d
67 files changed
+1822
-69
No files matched your search
+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}")
|
||||
|
||||
Reference in new issue
Block a user