feat: stream chat and settle metered CNY usage atomically
This commit is contained in:
1 parent
7360cf3ebe
commit
b46ee72f2d
28 files changed
+1098
-86
No files matched your search
@@ -221,11 +221,11 @@ public class CatalogService {
|
||||
|
||||
private void validatePricing(Object input) {
|
||||
if (!(input instanceof Map<?,?> prices)) return;
|
||||
for(String field: List.of("input","output")) {
|
||||
for(String field: List.of("input","output","cachedInput")) {
|
||||
Object amount=prices.get(field);
|
||||
if(amount==null) continue;
|
||||
try { if(new java.math.BigDecimal(String.valueOf(amount)).signum()<0) throw new NumberFormatException(); }
|
||||
catch(NumberFormatException e) { throw new ApiException("INVALID_PRICE","价格必须是非负数"); }
|
||||
try { var price=new java.math.BigDecimal(String.valueOf(amount)); if(price.signum()<0 || price.scale()>8 || price.compareTo(new java.math.BigDecimal("1000000"))>0) throw new NumberFormatException(); }
|
||||
catch(NumberFormatException e) { throw new ApiException("INVALID_PRICE","价格须在 0–100 万之间,最多 8 位小数"); }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -351,8 +351,13 @@ public class CatalogService {
|
||||
result.put("tasks", Map.of("queued", 0, "running", 0, "succeeded", 0, "failed", 0));
|
||||
result.put("series", List.of());
|
||||
result.put("ledger", List.of());
|
||||
List<java.math.BigDecimal> balances=jdbc.queryForList("SELECT balance FROM user_wallets WHERE user_id=?",java.math.BigDecimal.class,user.id());
|
||||
result.put("availableCredit",balances.isEmpty()?java.math.BigDecimal.ZERO:balances.getFirst());
|
||||
var balances=jdbc.queryForList("SELECT balance, reserved_balance FROM user_wallets WHERE user_id=?",user.id());
|
||||
var balance=balances.isEmpty()?java.math.BigDecimal.ZERO:(java.math.BigDecimal)balances.getFirst().get("balance");
|
||||
var reserved=balances.isEmpty()?java.math.BigDecimal.ZERO:(java.math.BigDecimal)balances.getFirst().get("reserved_balance");
|
||||
result.put("availableCredit",balance.subtract(reserved));
|
||||
result.put("balance",balance);
|
||||
result.put("reservedCredit",reserved);
|
||||
result.put("costThisMonth",jdbc.queryForObject("SELECT COALESCE(-SUM(amount),0) FROM wallet_entries WHERE user_id=? AND operation='usage' AND created_at >= date_trunc('month',now())",java.math.BigDecimal.class,user.id()));
|
||||
result.put("ledger",jdbc.query("SELECT id, reason, amount, created_at FROM wallet_entries WHERE user_id=? ORDER BY created_at DESC LIMIT 100", (rs,n)->Map.of("id",rs.getString("id"),"title",rs.getString("reason"),"amount",rs.getBigDecimal("amount"),"createdAt",rs.getObject("created_at",OffsetDateTime.class)),user.id()));
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -22,7 +22,8 @@ public class WebConfig implements WebMvcConfigurer {
|
||||
registry.addMapping("/**")
|
||||
.allowedOrigins(allowedOrigins)
|
||||
.allowedMethods("GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS")
|
||||
.allowedHeaders("Content-Type", "Authorization", "X-Moyi-Source")
|
||||
.allowedHeaders("Content-Type", "Authorization", "X-Moyi-Source", "Idempotency-Key")
|
||||
.exposedHeaders("X-Request-Id", "X-Moyi-Cost-CNY")
|
||||
.allowCredentials(true)
|
||||
.maxAge(3600);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,95 @@
|
||||
package com.moyi.controlplane.gateway;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.moyi.controlplane.common.ApiException;
|
||||
import java.math.BigDecimal;
|
||||
import java.math.RoundingMode;
|
||||
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 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);
|
||||
}
|
||||
}
|
||||
public record Tokens(long prompt, long completion, long cached) {}
|
||||
public record Request(Map<String,Object> body, long reservedInputTokens, int maxOutputTokens) {}
|
||||
|
||||
public static Rates rates(Object value, boolean mock) {
|
||||
if (mock) return new Rates(BigDecimal.ZERO,BigDecimal.ZERO,BigDecimal.ZERO);
|
||||
if (!(value instanceof Map<?,?> prices) || !"CNY / 1M tokens".equals(prices.get("unit")))
|
||||
throw new ApiException("MODEL_NOT_PRICED","该分组尚未配置人民币 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);
|
||||
}
|
||||
private static BigDecimal price(Object value) {
|
||||
try {
|
||||
BigDecimal price=new BigDecimal(String.valueOf(value));
|
||||
if(price.signum()<0 || price.compareTo(new BigDecimal("1000000"))>0 || price.scale()>8) throw new NumberFormatException();
|
||||
return price;
|
||||
} catch(Exception e) { throw new ApiException("MODEL_NOT_PRICED","请配置有效的输入、输出人民币售价(最多 8 位小数)",503); }
|
||||
}
|
||||
public static Tokens tokens(Map<String,Object> response) {
|
||||
if(response.containsKey("error") || !(response.get("usage") instanceof Map<?,?> usage)) throw invalidUsage();
|
||||
long prompt=count(usage.get("prompt_tokens")), completion=count(usage.get("completion_tokens"));
|
||||
Object cached=usage.get("prompt_cache_hit_tokens");
|
||||
if(usage.get("prompt_tokens_details") instanceof Map<?,?> details && details.get("cached_tokens")!=null) cached=details.get("cached_tokens");
|
||||
long hits=cached==null?0:count(cached);
|
||||
if(hits>prompt) throw invalidUsage();
|
||||
return new Tokens(prompt,completion,hits);
|
||||
}
|
||||
private static long count(Object value) {
|
||||
try {
|
||||
if(!(value instanceof Number)) throw new NumberFormatException();
|
||||
long number=new BigDecimal(value.toString()).longValueExact();
|
||||
if(number<0 || number>1_000_000_000L) throw new NumberFormatException();
|
||||
return number;
|
||||
} catch(Exception e) { throw invalidUsage(); }
|
||||
}
|
||||
private static ApiException invalidUsage() { return new ApiException("UPSTREAM_USAGE_MISSING","上游未返回有效 Token 用量,本次未扣费",502); }
|
||||
|
||||
public static Request request(Map<String,Object> input, ObjectMapper json) {
|
||||
if(!(input.get("messages") instanceof List<?> messages) || messages.isEmpty() || messages.size()>256)
|
||||
throw new ApiException("INVALID_MESSAGES","messages 不能为空,且不能超过 256 条");
|
||||
for(Object item:messages) {
|
||||
if(!(item instanceof Map<?,?> message) || !(message.get("role") instanceof String)) throw new ApiException("INVALID_MESSAGES","消息格式无效");
|
||||
Object content=message.get("content");
|
||||
if(content instanceof List<?> parts) {
|
||||
for(Object part:parts) if(!(part instanceof Map<?,?> p) || !"text".equals(p.get("type")) || !(p.get("text") instanceof String))
|
||||
throw new ApiException("UNSUPPORTED_CONTENT","当前聊天计费支持文本和工具调用,图片、音频请使用对应接口");
|
||||
} else if(content!=null && !(content instanceof String)) throw new ApiException("INVALID_MESSAGES","消息内容格式无效");
|
||||
}
|
||||
if(input.get("n")!=null && !"1".equals(input.get("n").toString())) throw new ApiException("UNSUPPORTED_N","当前仅支持 n=1");
|
||||
if(input.containsKey("max_tokens") && input.containsKey("max_completion_tokens")) throw new ApiException("INVALID_MAX_TOKENS","请只设置一种输出 Token 上限");
|
||||
String maxField=input.containsKey("max_completion_tokens")?"max_completion_tokens":"max_tokens";
|
||||
int max=4096;
|
||||
try {
|
||||
if(input.containsKey(maxField)) max=new BigDecimal(input.get(maxField).toString()).intValueExact();
|
||||
if(max<1 || max>32768) throw new NumberFormatException();
|
||||
} catch(Exception e) { throw new ApiException("INVALID_MAX_TOKENS","输出 Token 上限须在 1–32768 之间"); }
|
||||
var body=new LinkedHashMap<String,Object>();
|
||||
// Do not let client credentials, alternate upstreams or multimodal billing bypass admission.
|
||||
var fields=Set.of("model","messages","stream","temperature","top_p","stop","presence_penalty","frequency_penalty","seed","tools","tool_choice","parallel_tool_calls","response_format","reasoning_effort","thinking","n","max_tokens","max_completion_tokens","stream_options");
|
||||
for(String field:input.keySet()) {
|
||||
if(!fields.contains(field)) throw new ApiException("UNSUPPORTED_PARAMETER","当前不支持请求参数:"+field);
|
||||
body.put(field,input.get(field));
|
||||
}
|
||||
body.put(maxField,max);
|
||||
body.put("stream",Boolean.TRUE.equals(input.get("stream")));
|
||||
try {
|
||||
int bytes=json.writeValueAsBytes(body).length;
|
||||
if(bytes>262144) throw new ApiException("REQUEST_TOO_LARGE","聊天请求不能超过 256 KiB",413);
|
||||
// UTF-8 bytes plus framing is deliberately conservative; actual usage settles later.
|
||||
return new Request(body,bytes+128L*messages.size()+1024,max);
|
||||
} catch(java.io.IOException e) { throw new ApiException("INVALID_REQUEST","请求无法编码"); }
|
||||
}
|
||||
}
|
||||
+53
@@ -0,0 +1,53 @@
|
||||
package com.moyi.controlplane.gateway;
|
||||
|
||||
import com.moyi.controlplane.common.ApiException;
|
||||
import java.util.*;
|
||||
|
||||
/** Retain a bounded, useful history while forwarding every original SSE delta. */
|
||||
final class ChatStreamCollector {
|
||||
private final String model;
|
||||
private final long started;
|
||||
private final StringBuilder content=new StringBuilder(), reasoning=new StringBuilder();
|
||||
private final Map<Integer,Map<String,Object>> tools=new TreeMap<>();
|
||||
private Object usage, id, created, finish;
|
||||
private Integer firstToken;
|
||||
ChatStreamCollector(String model,long started) { this.model=model; this.started=started; }
|
||||
void accept(Map<String,Object> chunk) {
|
||||
if(chunk.get("id")!=null) id=chunk.get("id");
|
||||
if(chunk.get("created")!=null) created=chunk.get("created");
|
||||
if(chunk.get("usage") instanceof Map<?,?>) usage=chunk.get("usage");
|
||||
if(chunk.get("choices") instanceof List<?> choices) for(Object raw:choices) {
|
||||
if(!(raw instanceof Map<?,?> choice)) continue;
|
||||
if(choice.get("finish_reason")!=null) finish=choice.get("finish_reason");
|
||||
if(choice.get("delta") instanceof Map<?,?> delta) {
|
||||
append(content,delta.get("content")); append(reasoning,delta.get("reasoning_content"));
|
||||
if(delta.get("tool_calls") instanceof List<?> calls) for(Object item:calls) {
|
||||
if(!(item instanceof Map<?,?> call) || !(call.get("index") instanceof Number index)) continue;
|
||||
int i=index.intValue();
|
||||
if(i<0 || i>=64) throw new ApiException("UPSTREAM_RESPONSE_INVALID","上游工具调用数量超过限制",502);
|
||||
var tool=tools.computeIfAbsent(i,k -> new LinkedHashMap<>());
|
||||
for(String field:List.of("id","type")) if(call.get(field)!=null) tool.put(field,call.get(field));
|
||||
if(call.get("function") instanceof Map<?,?> function) {
|
||||
@SuppressWarnings("unchecked") var target=(Map<String,Object>)tool.computeIfAbsent("function",k -> new LinkedHashMap<>());
|
||||
for(String field:List.of("name","arguments")) if(function.get(field) instanceof String text) {
|
||||
String value=String.valueOf(target.getOrDefault(field,""))+text;
|
||||
target.put(field,value.substring(0,Math.min(8000,value.length())));
|
||||
}
|
||||
}
|
||||
}
|
||||
if(firstToken==null && ((delta.get("content") instanceof String s && !s.isEmpty()) || (delta.get("reasoning_content") instanceof String r && !r.isEmpty()) || delta.get("tool_calls")!=null)) firstToken=(int)(System.currentTimeMillis()-started);
|
||||
}
|
||||
}
|
||||
}
|
||||
private void append(StringBuilder target,Object value) { if(value instanceof String s && target.length()<8000) target.append(s,0,Math.min(s.length(),8000-target.length())); }
|
||||
Integer firstTokenMs() { return firstToken; }
|
||||
Map<String,Object> result() {
|
||||
if(finish==null) throw new ApiException("UPSTREAM_STREAM_INCOMPLETE","上游未完成输出,本次未扣费",502);
|
||||
var message=new LinkedHashMap<String,Object>(); message.put("role","assistant"); message.put("content",content.toString());
|
||||
if(!reasoning.isEmpty()) message.put("reasoning_content",reasoning.toString());
|
||||
if(!tools.isEmpty()) message.put("tool_calls",List.copyOf(tools.values()));
|
||||
var result=new LinkedHashMap<String,Object>(); result.put("id",id); result.put("created",created); result.put("model",model); result.put("object","chat.completion");
|
||||
result.put("usage",usage); result.put("choices",List.of(Map.of("index",0,"message",message,"finish_reason",finish)));
|
||||
return result;
|
||||
}
|
||||
}
|
||||
+87
-34
@@ -3,13 +3,15 @@ package com.moyi.controlplane.gateway;
|
||||
import com.moyi.controlplane.catalog.CatalogService;
|
||||
import com.moyi.controlplane.common.ApiException;
|
||||
import com.moyi.controlplane.provider.OpenAiCompatibleProvider;
|
||||
import com.moyi.controlplane.usage.UsageAuditService;
|
||||
import com.moyi.controlplane.wallet.ChatBillingService;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
import java.time.Instant;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
@@ -24,13 +26,15 @@ public class GatewayController {
|
||||
private final CatalogService catalogService;
|
||||
private final OpenAiCompatibleProvider provider;
|
||||
private final GatewayGuard guard;
|
||||
private final UsageAuditService usage;
|
||||
private final ChatBillingService billing;
|
||||
private final ObjectMapper json;
|
||||
|
||||
public GatewayController(CatalogService catalogService, OpenAiCompatibleProvider provider, GatewayGuard guard, UsageAuditService usage) {
|
||||
public GatewayController(CatalogService catalogService, OpenAiCompatibleProvider provider, GatewayGuard guard, ChatBillingService billing, ObjectMapper json) {
|
||||
this.catalogService = catalogService;
|
||||
this.provider = provider;
|
||||
this.guard = guard;
|
||||
this.usage = usage;
|
||||
this.billing = billing;
|
||||
this.json = json;
|
||||
}
|
||||
|
||||
@GetMapping("/models")
|
||||
@@ -55,7 +59,9 @@ public class GatewayController {
|
||||
@PostMapping("/chat/completions")
|
||||
public ResponseEntity<Map<String, Object>> chatCompletions(
|
||||
@RequestHeader(value = "Authorization", required = false) String authorization,
|
||||
@RequestBody Map<String, Object> input
|
||||
@RequestBody Map<String, Object> input,
|
||||
@RequestHeader(value = "Idempotency-Key", required = false) String idempotencyKey,
|
||||
HttpServletResponse servletResponse
|
||||
) {
|
||||
Map<String, Object> key = requireKey(authorization);
|
||||
long startedAt = System.currentTimeMillis();
|
||||
@@ -64,34 +70,89 @@ public class GatewayController {
|
||||
if (model == null) throw new ApiException("MODEL_NOT_FOUND", "模型不存在或已下线", 404);
|
||||
if (!catalogService.keyCanInvoke(key, modelId)) throw new ApiException("MODEL_FORBIDDEN", "当前 Key 无权调用该模型", 403);
|
||||
if ("async".equals(model.get("mode"))) throw new ApiException("ASYNC_MODEL", "该模型需要使用异步任务接口", 400);
|
||||
if (Boolean.TRUE.equals(input.get("stream"))) throw new ApiException("STREAM_NOT_IMPLEMENTED", "流式转发将在下一节点开放", 501);
|
||||
String providerId=String.valueOf(model.get("providerId"));
|
||||
boolean mock="provider-mock".equals(providerId);
|
||||
if(!mock && !provider.isConfigured(providerId)) throw new ApiException("PROVIDER_NOT_CONFIGURED","该供应商尚未配置密钥,不能执行真实调用",503);
|
||||
ChatMeter.Request request=ChatMeter.request(input,json);
|
||||
guard.checkRate(UUID.fromString(String.valueOf(key.get("id"))), false);
|
||||
try (GatewayGuard.Slot slot = guard.acquire(model)) {
|
||||
ResponseEntity<Map<String, Object>> result = invoke(key, modelId, model, input);
|
||||
Map<String, Object> body = result.getBody();
|
||||
usage.recordCall(UUID.fromString(String.valueOf(key.get("userId"))), UUID.fromString(String.valueOf(key.get("id"))), modelId, String.valueOf(model.get("providerId")), "succeeded", (int) (System.currentTimeMillis() - startedAt), number(body, "usage.prompt_tokens"), number(body, "usage.completion_tokens"), input, body, null, null);
|
||||
return result;
|
||||
} catch (ApiException exception) {
|
||||
usage.recordCall(UUID.fromString(String.valueOf(key.get("userId"))), UUID.fromString(String.valueOf(key.get("id"))), modelId, String.valueOf(model.get("providerId")), "failed", (int) (System.currentTimeMillis() - startedAt), 0, 0, input, null, exception.getCode(), exception.getMessage());
|
||||
throw exception;
|
||||
var ticket=billing.reserve(key,model,request,idempotencyKey);
|
||||
servletResponse.setHeader("X-Request-Id",ticket.id().toString());
|
||||
try {
|
||||
catalogService.touchKey(key);
|
||||
if(Boolean.TRUE.equals(request.body().get("stream"))) {
|
||||
stream(model,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());
|
||||
body.put("model",modelId);
|
||||
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);
|
||||
} catch(Exception exception) {
|
||||
ApiException error=exception instanceof ApiException e?e:new ApiException("CALL_INTERRUPTED","调用中断,请查看使用记录确认结算状态",502);
|
||||
billing.fail(ticket,error.getCode(),error.getMessage(),elapsed(startedAt));
|
||||
if(servletResponse.isCommitted()) {
|
||||
try { event(servletResponse,Map.of("error",Map.of("code",error.getCode(),"message",error.getMessage()))); done(servletResponse); } catch(IOException ignored) { }
|
||||
return null;
|
||||
}
|
||||
servletResponse.resetBuffer();
|
||||
servletResponse.setContentType("application/json");
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private ResponseEntity<Map<String, Object>> invoke(Map<String, Object> key, String modelId, Map<String, Object> model, Map<String, Object> input) {
|
||||
String providerId = String.valueOf(model.get("providerId"));
|
||||
if (provider.isConfigured(providerId)) {
|
||||
List<?> messages = input.get("messages") instanceof List<?> values ? values : List.of();
|
||||
Map<String, Object> upstream = provider.chat(providerId, String.valueOf(model.get("upstreamModel")), messages);
|
||||
upstream.put("model", modelId);
|
||||
catalogService.touchKey(key);
|
||||
return ResponseEntity.ok(upstream);
|
||||
private void stream(Map<String,Object> model, Map<String,Object> input, ChatBillingService.Ticket ticket, HttpServletResponse response, long started, boolean mock) throws IOException {
|
||||
String modelId=model.get("id").toString();
|
||||
if(mock) {
|
||||
var body=mockResponse(modelId,input);
|
||||
prepareStream(response);
|
||||
var choices=(List<?>)body.get("choices");
|
||||
var message=((Map<?,?>)choices.getFirst()).get("message");
|
||||
event(response,Map.of("id",body.get("id"),"object","chat.completion.chunk","model",modelId,"choices",List.of(Map.of("index",0,"delta",message))));
|
||||
billing.settle(ticket,body,elapsed(started),elapsed(started));
|
||||
event(response,Map.of("id",body.get("id"),"object","chat.completion.chunk","model",modelId,"choices",List.of(Map.of("index",0,"delta",Map.of(),"finish_reason","stop"))));
|
||||
event(response,Map.of("choices",List.of(),"usage",body.get("usage"))); done(response); return;
|
||||
}
|
||||
if (!"provider-mock".equals(providerId)) {
|
||||
throw new ApiException("PROVIDER_NOT_CONFIGURED", "该供应商尚未配置密钥,不能执行真实调用", 503);
|
||||
// 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)) {
|
||||
var collector=new ChatStreamCollector(modelId,started);
|
||||
prepareStream(response);
|
||||
boolean[] disconnected={false};
|
||||
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; }
|
||||
});
|
||||
var body=collector.result();
|
||||
billing.settle(ticket,body,elapsed(started),collector.firstTokenMs());
|
||||
if(!disconnected[0]) done(response);
|
||||
}
|
||||
}
|
||||
private void prepareStream(HttpServletResponse response) {
|
||||
response.setContentType("text/event-stream"); response.setCharacterEncoding("UTF-8");
|
||||
response.setHeader("Cache-Control","no-cache"); response.setHeader("X-Accel-Buffering","no");
|
||||
}
|
||||
private void event(HttpServletResponse response,Object data) throws IOException {
|
||||
response.getOutputStream().write(("data: "+json.writeValueAsString(data)+"\n\n").getBytes(java.nio.charset.StandardCharsets.UTF_8));
|
||||
response.flushBuffer();
|
||||
}
|
||||
private void done(HttpServletResponse response) throws IOException {
|
||||
response.getOutputStream().write("data: [DONE]\n\n".getBytes(java.nio.charset.StandardCharsets.UTF_8)); response.flushBuffer();
|
||||
}
|
||||
private int elapsed(long started) { return (int)Math.min(Integer.MAX_VALUE,System.currentTimeMillis()-started); }
|
||||
private void requireCompletion(Map<String,Object> body) {
|
||||
if(body.containsKey("error") || !(body.get("choices") instanceof List<?> choices) || choices.isEmpty()
|
||||
|| !(choices.getFirst() instanceof Map<?,?> choice) || choice.get("finish_reason")==null)
|
||||
throw new ApiException("UPSTREAM_RESPONSE_INVALID","上游未返回完整的聊天结果,本次未扣费",502);
|
||||
}
|
||||
private Map<String,Object> mockResponse(String modelId,Map<String,Object> input) {
|
||||
String prompt = extractPrompt(input.get("messages"));
|
||||
String content = "MOYI Java Mock 已收到:" + (prompt.isBlank() ? "(空消息)" : prompt);
|
||||
catalogService.touchKey(key);
|
||||
Map<String, Object> response = new LinkedHashMap<>();
|
||||
response.put("id", "chatcmpl-" + UUID.randomUUID().toString().replace("-", "").substring(0, 12));
|
||||
response.put("object", "chat.completion");
|
||||
@@ -99,7 +160,7 @@ public class GatewayController {
|
||||
response.put("model", modelId);
|
||||
response.put("choices", List.of(Map.of("index", 0, "message", Map.of("role", "assistant", "content", content), "finish_reason", "stop")));
|
||||
response.put("usage", Map.of("prompt_tokens", prompt.length(), "completion_tokens", content.length(), "total_tokens", prompt.length() + content.length()));
|
||||
return ResponseEntity.status(HttpStatus.OK).body(response);
|
||||
return response;
|
||||
}
|
||||
|
||||
private Map<String, Object> requireKey(String authorization) {
|
||||
@@ -117,12 +178,4 @@ public class GatewayController {
|
||||
}).reduce((left, right) -> left + "\n" + right).orElse("");
|
||||
}
|
||||
|
||||
private long number(Map<String, Object> body, String path) {
|
||||
if (body == null) return 0;
|
||||
Object usageValue = body.get("usage");
|
||||
if (!(usageValue instanceof Map<?, ?> map)) return 0;
|
||||
String key = path.substring(path.indexOf('.') + 1);
|
||||
Object value = map.get(key);
|
||||
return value instanceof Number number ? number.longValue() : 0;
|
||||
}
|
||||
}
|
||||
+76
-9
@@ -8,10 +8,14 @@ import java.net.URI;
|
||||
import java.net.http.HttpClient;
|
||||
import java.net.http.HttpRequest;
|
||||
import java.net.http.HttpResponse;
|
||||
import java.io.*;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.time.Duration;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.*;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import jakarta.annotation.PreDestroy;
|
||||
import org.springframework.dao.EmptyResultDataAccessException;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.stereotype.Service;
|
||||
@@ -23,6 +27,8 @@ public class OpenAiCompatibleProvider {
|
||||
private final JdbcTemplate jdbc;
|
||||
private final SecretCipher secretCipher;
|
||||
private final Map<String, ProviderConfig> providers;
|
||||
private final ScheduledExecutorService deadlines = Executors.newSingleThreadScheduledExecutor(r -> { var t=new Thread(r,"upstream-deadlines"); t.setDaemon(true); return t; });
|
||||
private final int timeoutSeconds;
|
||||
|
||||
public OpenAiCompatibleProvider(
|
||||
ObjectMapper objectMapper,
|
||||
@@ -34,6 +40,7 @@ public class OpenAiCompatibleProvider {
|
||||
this.httpClient = HttpClient.newBuilder().connectTimeout(Duration.ofSeconds(10)).build();
|
||||
this.jdbc = jdbc;
|
||||
this.secretCipher = secretCipher;
|
||||
this.timeoutSeconds = Math.max(1,Math.min(90,environment.getProperty("moyi.gateway.upstream-timeout-seconds",Integer.class,90)));
|
||||
this.providers = Map.of(
|
||||
"provider-deepseek", new ProviderConfig(environment.getProperty("moyi.providers.deepseek.api-key", ""), environment.getProperty("moyi.providers.deepseek.base-url", "https://api.deepseek.com")),
|
||||
"provider-kimi", new ProviderConfig(environment.getProperty("moyi.providers.kimi.api-key", ""), environment.getProperty("moyi.providers.kimi.base-url", "https://api.moonshot.ai/v1")),
|
||||
@@ -46,29 +53,49 @@ public class OpenAiCompatibleProvider {
|
||||
return provider != null && !provider.apiKey().isBlank();
|
||||
}
|
||||
|
||||
public Map<String, Object> chat(String providerId, String upstreamModel, List<?> messages) {
|
||||
public Map<String, Object> chat(String providerId, String upstreamModel, Map<String,Object> input) {
|
||||
try(var response=open(providerId,upstreamModel,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);
|
||||
return objectMapper.readValue(bytes, new com.fasterxml.jackson.core.type.TypeReference<>() {});
|
||||
} catch(ApiException e) { throw e; }
|
||||
catch(Exception e) { throw upstreamError(); }
|
||||
}
|
||||
|
||||
public StreamResponse stream(String providerId, String upstreamModel, Map<String,Object> input) {
|
||||
return open(providerId,upstreamModel,input,true);
|
||||
}
|
||||
|
||||
private StreamResponse open(String providerId, String upstreamModel, Map<String,Object> input, boolean streaming) {
|
||||
ProviderConfig provider = resolve(providerId);
|
||||
if (provider == null || provider.apiKey().isBlank()) {
|
||||
throw new ApiException("PROVIDER_NOT_CONFIGURED", "该模型的供应商尚未配置服务端 API Key", 503);
|
||||
}
|
||||
Map<String, Object> body = new LinkedHashMap<>();
|
||||
Map<String, Object> body = new LinkedHashMap<>(input);
|
||||
body.put("model", upstreamModel);
|
||||
body.put("messages", messages == null ? List.of() : messages);
|
||||
body.put("stream", false);
|
||||
body.put("stream", streaming);
|
||||
if(streaming) body.put("stream_options",Map.of("include_usage",true));
|
||||
else body.remove("stream_options");
|
||||
String json = writeJson(body);
|
||||
HttpRequest request = HttpRequest.newBuilder()
|
||||
.uri(URI.create(normalizeUrl(provider.baseUrl()) + "/chat/completions"))
|
||||
.timeout(Duration.ofSeconds(90))
|
||||
.timeout(Duration.ofSeconds(timeoutSeconds))
|
||||
.header("Authorization", "Bearer " + provider.apiKey())
|
||||
.header("Content-Type", "application/json")
|
||||
.POST(HttpRequest.BodyPublishers.ofString(json))
|
||||
.build();
|
||||
try {
|
||||
HttpResponse<String> response = httpClient.send(request, HttpResponse.BodyHandlers.ofString());
|
||||
long started=System.nanoTime();
|
||||
HttpResponse<InputStream> response = httpClient.send(request, HttpResponse.BodyHandlers.ofInputStream());
|
||||
if (response.statusCode() < 200 || response.statusCode() >= 300) {
|
||||
throw new ApiException("UPSTREAM_PROVIDER_ERROR", "上游模型供应商请求失败", 502);
|
||||
response.body().close();
|
||||
throw upstreamError();
|
||||
}
|
||||
return objectMapper.readValue(response.body(), new com.fasterxml.jackson.core.type.TypeReference<>() {});
|
||||
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)));
|
||||
} catch (ApiException exception) {
|
||||
throw exception;
|
||||
} catch (InterruptedException exception) {
|
||||
@@ -79,6 +106,46 @@ public class OpenAiCompatibleProvider {
|
||||
}
|
||||
}
|
||||
|
||||
/** A hard body deadline is required: HttpRequest.timeout alone does not cover InputStream reads. */
|
||||
public final class StreamResponse implements AutoCloseable {
|
||||
private final InputStream body;
|
||||
private final AtomicBoolean expired=new AtomicBoolean();
|
||||
private final ScheduledFuture<?> deadline;
|
||||
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 {
|
||||
var reader=new BufferedReader(new InputStreamReader(body,StandardCharsets.UTF_8));
|
||||
StringBuilder event=new StringBuilder(), line=new StringBuilder();
|
||||
long total=0;
|
||||
int c;
|
||||
// Bound both individual frames and the whole response before allocating unbounded strings.
|
||||
while((c=reader.read())!=-1) {
|
||||
if(expired.get()) throw new ApiException("UPSTREAM_PROVIDER_TIMEOUT","上游响应超时,本次未扣费",504);
|
||||
if(++total>4*1024*1024 || line.length()>262144 || event.length()>262144) throw new ApiException("UPSTREAM_RESPONSE_TOO_LARGE","上游响应超过限制",502);
|
||||
if(c=='\n') {
|
||||
String text=line.toString(); line.setLength(0);
|
||||
if(text.endsWith("\r")) text=text.substring(0,text.length()-1);
|
||||
if(text.isEmpty()) {
|
||||
if(!event.isEmpty()) {
|
||||
String data=event.substring(0,event.length()-1); event.setLength(0);
|
||||
if("[DONE]".equals(data)) return;
|
||||
Map<String,Object> chunk=objectMapper.readValue(data,new com.fasterxml.jackson.core.type.TypeReference<>(){});
|
||||
if(chunk.containsKey("error")) throw upstreamError();
|
||||
consumer.accept(chunk);
|
||||
}
|
||||
} else if(text.startsWith("data:")) event.append(text.substring(5).stripLeading()).append('\n');
|
||||
} else line.append((char)c);
|
||||
}
|
||||
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) {} }
|
||||
}
|
||||
@FunctionalInterface public interface EventConsumer { void accept(Map<String,Object> chunk) throws IOException; }
|
||||
@PreDestroy public void close() { deadlines.shutdownNow(); }
|
||||
private ApiException upstreamError() { return new ApiException("UPSTREAM_PROVIDER_ERROR","上游模型供应商请求失败",502); }
|
||||
|
||||
private String normalizeUrl(String value) {
|
||||
return value == null ? "" : value.replaceAll("/+$", "");
|
||||
}
|
||||
|
||||
+21
-1
@@ -33,6 +33,24 @@ public class UsageAuditService {
|
||||
this.objectMapper = objectMapper;
|
||||
}
|
||||
|
||||
public void startChat(UUID id, UUID userId, UUID keyId, Map<String,Object> model, Map<String,Object> input) {
|
||||
String agent=null, source="api";
|
||||
var attributes=org.springframework.web.context.request.RequestContextHolder.getRequestAttributes();
|
||||
if(attributes instanceof org.springframework.web.context.request.ServletRequestAttributes servlet) {
|
||||
agent=servlet.getRequest().getHeader("User-Agent");
|
||||
if("playground".equals(servlet.getRequest().getHeader("X-Moyi-Source"))) source="playground";
|
||||
}
|
||||
jdbc.update("INSERT INTO usage_events(id,request_id,user_id,api_key_id,model_id,provider_id,event_kind,status,input_payload,endpoint,call_mode,request_source,user_agent,reasoning_effort) VALUES (?,?,?,?,?,?,'call','running',?::jsonb,'/v1/chat/completions',?,?,?,?)",
|
||||
id,id.toString(),userId,keyId,model.get("id"),model.get("providerId"),json(input),Boolean.TRUE.equals(input.get("stream"))?"stream":"sync",source,
|
||||
agent==null?null:agent.substring(0,Math.min(1000,agent.length())),input.get("reasoning_effort")==null?null:String.valueOf(input.get("reasoning_effort")).substring(0,Math.min(32,String.valueOf(input.get("reasoning_effort")).length())));
|
||||
}
|
||||
|
||||
public void finishChat(UUID id, String status, com.moyi.controlplane.gateway.ChatMeter.Tokens tokens,
|
||||
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);
|
||||
}
|
||||
|
||||
public void recordCall(
|
||||
UUID userId, UUID apiKeyId, String modelId, String providerId, String status,
|
||||
int latencyMs, long inputUnits, long outputUnits, Object input, Object output,
|
||||
@@ -263,7 +281,9 @@ public class UsageAuditService {
|
||||
ObjectNode result = objectMapper.createObjectNode();
|
||||
node.fields().forEachRemaining(entry -> {
|
||||
String key = entry.getKey().toLowerCase();
|
||||
if (key.contains("authorization") || key.contains("api_key") || key.contains("apikey") || key.contains("token") || key.contains("secret") || key.contains("password")) result.put(entry.getKey(), "[REDACTED]");
|
||||
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();
|
||||
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()));
|
||||
});
|
||||
return result;
|
||||
|
||||
@@ -0,0 +1,90 @@
|
||||
package com.moyi.controlplane.wallet;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.moyi.controlplane.common.ApiException;
|
||||
import com.moyi.controlplane.gateway.ChatMeter;
|
||||
import com.moyi.controlplane.usage.UsageAuditService;
|
||||
import java.math.BigDecimal;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.MessageDigest;
|
||||
import java.util.*;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
@Service
|
||||
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; }
|
||||
public record Ticket(UUID id, UUID userId, ChatMeter.Rates rates) {}
|
||||
|
||||
@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")));
|
||||
String requestHash=hash(encode(new TreeMap<>(request.body())));
|
||||
String idem=null;
|
||||
if(idempotencyKey!=null) {
|
||||
if(idempotencyKey.isBlank() || idempotencyKey.length()>128) throw new ApiException("INVALID_IDEMPOTENCY_KEY","Idempotency-Key 长度须为 1–128");
|
||||
idem=hash(idempotencyKey);
|
||||
jdbc.queryForList("SELECT pg_advisory_xact_lock(hashtextextended(?,0))",userId+":"+idem);
|
||||
var previous=jdbc.queryForList("SELECT id, request_hash FROM chat_billing_requests WHERE user_id=? AND idempotency_hash=?",userId,idem);
|
||||
if(!previous.isEmpty()) throw new ApiException(requestHash.equals(previous.getFirst().get("request_hash"))?"DUPLICATE_REQUEST":"IDEMPOTENCY_CONFLICT","该请求编号已受理,请在使用记录中查看结果;不会重复调用或扣费",409);
|
||||
}
|
||||
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);
|
||||
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();
|
||||
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 user_wallets SET reserved_balance=reserved_balance+?,updated_at=now() WHERE user_id=?",hold,userId);
|
||||
return new Ticket(id,userId,rates);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public BigDecimal settle(Ticket ticket, Map<String,Object> response, int latency, Integer firstTokenMs) {
|
||||
var call=jdbc.queryForMap("SELECT * FROM chat_billing_requests WHERE id=? FOR UPDATE",ticket.id());
|
||||
if("succeeded".equals(call.get("status"))) return (BigDecimal)call.get("cost_amount");
|
||||
if(!"pending".equals(call.get("status"))) throw new ApiException("BILLING_REQUEST_CLOSED","调用已结束,不能重复结算",409);
|
||||
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 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);
|
||||
jdbc.update("UPDATE chat_billing_requests SET status='succeeded',cost_amount=? WHERE id=?",cost,ticket.id());
|
||||
return cost;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void fail(Ticket ticket, String code, String message, int latency) { release(ticket.id(),code,message,latency); }
|
||||
|
||||
private void release(UUID id, String code, String message, int latency) {
|
||||
var call=jdbc.queryForMap("SELECT * FROM chat_billing_requests WHERE id=? FOR UPDATE",id);
|
||||
if(!"pending".equals(call.get("status"))) return;
|
||||
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);
|
||||
}
|
||||
|
||||
@Scheduled(fixedDelayString="${moyi.billing.recovery-interval-ms:60000}")
|
||||
@Transactional
|
||||
public void recoverExpired() {
|
||||
var ids=jdbc.queryForList("SELECT id FROM chat_billing_requests WHERE status='pending' AND expires_at<now() ORDER BY expires_at LIMIT 100 FOR UPDATE SKIP LOCKED",UUID.class);
|
||||
for(UUID id:ids) release(id,"CALL_INTERRUPTED","调用中断,未取得可结算用量,预留额度已释放",0);
|
||||
}
|
||||
private String encode(Object value) { try { return json.writeValueAsString(value); } catch(Exception e) { throw new ApiException("INVALID_REQUEST","请求无法编码"); } }
|
||||
private Map<String,Object> decode(String value) { try { return json.readValue(value,new com.fasterxml.jackson.core.type.TypeReference<>(){}); } catch(Exception e) { throw new IllegalStateException("Invalid billing snapshot",e); } }
|
||||
private 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); } }
|
||||
}
|
||||
@@ -25,7 +25,7 @@ public class WalletService {
|
||||
|
||||
public List<Map<String,Object>> entries(String token, UUID userId) {
|
||||
auth.requireAdmin(token);
|
||||
return jdbc.queryForList("SELECT e.id, e.amount, e.operation, e.balance_before AS \"balanceBefore\", e.balance_after AS \"balanceAfter\", e.currency, e.reason, e.created_at AS \"createdAt\", u.display_name AS actor FROM wallet_entries e JOIN users u ON u.id=e.actor_user_id WHERE e.user_id=? ORDER BY e.created_at DESC LIMIT 100",userId);
|
||||
return jdbc.queryForList("SELECT e.id, e.request_id AS \"requestId\", e.amount, e.operation, e.balance_before AS \"balanceBefore\", e.balance_after AS \"balanceAfter\", e.currency, e.reason, e.created_at AS \"createdAt\", COALESCE(u.display_name,'系统计费') AS actor FROM wallet_entries e LEFT JOIN users u ON u.id=e.actor_user_id WHERE e.user_id=? ORDER BY e.created_at DESC LIMIT 100",userId);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
@@ -64,7 +64,8 @@ public class WalletService {
|
||||
BigDecimal balance=jdbc.queryForObject("SELECT balance FROM user_wallets WHERE user_id=? FOR UPDATE",BigDecimal.class,userId);
|
||||
if(expected!=null && balance.compareTo(expected)!=0) throw new ApiException("BALANCE_CHANGED","余额已变化,请刷新后重新确认调整",409);
|
||||
BigDecimal next=switch(operation) { case "decrease" -> balance.subtract(amount); case "set" -> amount; default -> balance.add(amount); };
|
||||
if(next.signum()<0) throw new ApiException("INSUFFICIENT_BALANCE","减少金额不能超过当前余额",409);
|
||||
BigDecimal reserved=jdbc.queryForObject("SELECT reserved_balance FROM user_wallets WHERE user_id=?",BigDecimal.class,userId);
|
||||
if(!"increase".equals(operation) && (next.signum()<0 || next.compareTo(reserved)<0)) throw new ApiException("INSUFFICIENT_BALANCE","减少后的余额不能低于 0 或正在调用的预留额度",409);
|
||||
BigDecimal delta=next.subtract(balance);
|
||||
UUID id=UUID.randomUUID();
|
||||
jdbc.update("UPDATE user_wallets SET balance=?, updated_at=now() WHERE user_id=?",next,userId);
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
-- A hold does not consume credit. Only a completed, metered call creates a debit.
|
||||
ALTER TABLE user_wallets ADD COLUMN reserved_balance numeric(20,8) NOT NULL DEFAULT 0 CHECK (reserved_balance >= 0);
|
||||
-- If a provider exceeds the token reservation, retain the actual charge as debt.
|
||||
-- New calls and administrative withdrawals cannot spend this debt.
|
||||
ALTER TABLE user_wallets DROP CONSTRAINT user_wallets_balance_check;
|
||||
ALTER TABLE wallet_entries DROP CONSTRAINT wallet_entries_balance_check;
|
||||
ALTER TABLE wallet_entries ADD CONSTRAINT wallet_entries_balance_check CHECK (balance_after=balance_before+amount);
|
||||
ALTER TABLE wallet_entries DROP CONSTRAINT wallet_entries_operation_check;
|
||||
ALTER TABLE wallet_entries ADD CONSTRAINT wallet_entries_operation_check CHECK (operation IN ('increase','decrease','set','usage'));
|
||||
ALTER TABLE wallet_entries ALTER COLUMN actor_user_id DROP NOT NULL;
|
||||
ALTER TABLE usage_events ALTER COLUMN cost_amount TYPE numeric(20,8);
|
||||
|
||||
CREATE TABLE chat_billing_requests (
|
||||
id uuid PRIMARY KEY REFERENCES usage_events(id),
|
||||
user_id uuid NOT NULL REFERENCES users(id),
|
||||
idempotency_hash varchar(64),
|
||||
request_hash varchar(64) NOT NULL,
|
||||
status varchar(16) NOT NULL DEFAULT 'pending' CHECK (status IN ('pending','succeeded','failed')),
|
||||
reserved_amount numeric(20,8) NOT NULL CHECK (reserved_amount >= 0),
|
||||
pricing jsonb NOT NULL,
|
||||
cost_amount numeric(20,8),
|
||||
expires_at timestamptz NOT NULL,
|
||||
created_at timestamptz NOT NULL DEFAULT now(),
|
||||
UNIQUE(user_id,idempotency_hash)
|
||||
);
|
||||
CREATE INDEX chat_billing_expiration_idx ON chat_billing_requests(expires_at) WHERE status='pending';
|
||||
+168
-1
@@ -7,6 +7,8 @@ import com.moyi.controlplane.common.ApiException;
|
||||
import com.moyi.controlplane.common.ApiExceptionHandler;
|
||||
import com.moyi.controlplane.security.SecretCipher;
|
||||
import com.moyi.controlplane.wallet.WalletService;
|
||||
import com.moyi.controlplane.wallet.ChatBillingService;
|
||||
import com.moyi.controlplane.gateway.ChatMeter;
|
||||
import com.moyi.controlplane.catalog.CatalogController;
|
||||
import com.moyi.controlplane.usage.*;
|
||||
import jakarta.servlet.http.Cookie;
|
||||
@@ -78,6 +80,8 @@ class PlatformAdminPostgresTest {
|
||||
context.registerBean(AuthService.class, () -> new AuthService(jdbc, new PasswordHasher("test-pepper"), json, 168));
|
||||
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));
|
||||
context.registerBean(ChatBillingService.class, () -> new ChatBillingService(jdbc,json,context.getBean(UsageAuditService.class)));
|
||||
context.registerBean(VerificationService.class, () -> new VerificationService(jdbc, sender, "test-pepper"));
|
||||
context.refresh();
|
||||
auth = context.getBean(AuthService.class);
|
||||
@@ -268,7 +272,7 @@ class PlatformAdminPostgresTest {
|
||||
org.springframework.web.context.request.RequestContextHolder.setRequestAttributes(new org.springframework.web.context.request.ServletRequestAttributes(request));
|
||||
try {
|
||||
audit.recordCall(memberId,null,groupId,providerId,"succeeded",200,100,20,
|
||||
Map.of("prompt","hello","authorization","hidden"),
|
||||
Map.of("prompt","hello","authorization","hidden","accessToken","hidden","refresh_token","hidden","prompt_tokens","hidden"),
|
||||
Map.of("usage",Map.of("prompt_tokens_details",Map.of("cached_tokens",80),"cache_creation_input_tokens",5)),null,null);
|
||||
} finally { org.springframework.web.context.request.RequestContextHolder.resetRequestAttributes(); }
|
||||
var saved=jdbc.queryForMap("SELECT * FROM usage_events WHERE user_id=?",memberId);
|
||||
@@ -280,6 +284,7 @@ class PlatformAdminPostgresTest {
|
||||
assertNull(saved.get("cost_amount"));
|
||||
assertNull(saved.get("first_token_ms"));
|
||||
assertFalse(saved.get("input_payload").toString().contains("hidden"));
|
||||
assertEquals(80,json.readTree(saved.get("output_payload").toString()).path("usage").path("prompt_tokens_details").path("cached_tokens").asInt());
|
||||
audit.recordTask(memberId,null,groupId,providerId,"local-timing-task","queued",Map.of("prompt","hello"),null,null,null);
|
||||
jdbc.update("UPDATE usage_events SET created_at=now()-interval '2 seconds' WHERE provider_task_id='local-timing-task'");
|
||||
audit.updateTask("local-timing-task","succeeded",Map.of("url","https://example.test/result"),null,null);
|
||||
@@ -329,4 +334,166 @@ class PlatformAdminPostgresTest {
|
||||
assertFalse(verification.capabilities().containsValue(true));
|
||||
assertThrows(ApiException.class, () -> verification.send("unavailable@example.test",invite,"127.0.0.1"));
|
||||
}
|
||||
|
||||
record ChatFixture(ChatBillingService billing, Map<String,Object> key, Map<String,Object> model, ChatMeter.Request request) {
|
||||
ChatBillingService.Ticket reserve(String idem) { return billing.reserve(key,model,request,idem); }
|
||||
}
|
||||
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"));
|
||||
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);
|
||||
return new ChatFixture(context.getBean(ChatBillingService.class),key,model,input);
|
||||
}
|
||||
Map<String,Object> meteredResult() {
|
||||
return Map.of("choices",List.of(Map.of("message",Map.of("content","OK"),"finish_reason","stop")),"usage",Map.of("prompt_tokens",100,"completion_tokens",50,"prompt_tokens_details",Map.of("cached_tokens",60)));
|
||||
}
|
||||
@Test void actualUsageSettlesOnceWithSnapshotAndAtomicHistory() throws Exception {
|
||||
var f=chatFixture();
|
||||
wallet.grant(ownerToken,memberId,UUID.randomUUID(),"1","billing test",meta);
|
||||
var ticket=f.reserve("same-call");
|
||||
var held=jdbc.queryForObject("SELECT reserved_balance FROM user_wallets WHERE user_id=?",BigDecimal.class,memberId);
|
||||
assertTrue(held.signum()>0);
|
||||
assertEquals("DUPLICATE_REQUEST",assertThrows(ApiException.class,()->f.reserve("same-call")).getCode());
|
||||
f.model().put("pricing",Map.of("input",999,"output",999,"unit","CNY / 1M tokens"));
|
||||
try(var executor=Executors.newFixedThreadPool(2)) {
|
||||
var a=executor.submit(()->f.billing().settle(ticket,meteredResult(),20,5));
|
||||
var b=executor.submit(()->f.billing().settle(ticket,meteredResult(),20,5));
|
||||
assertEquals(new BigDecimal("0.00034000"),a.get()); assertEquals(a.get(),b.get());
|
||||
}
|
||||
f.billing().fail(ticket,"LATE_FAILURE","Client closed after settlement",20);
|
||||
assertEquals(new BigDecimal("0.99966000"),jdbc.queryForObject("SELECT balance FROM user_wallets WHERE user_id=?",BigDecimal.class,memberId));
|
||||
assertEquals(0,jdbc.queryForObject("SELECT reserved_balance FROM user_wallets WHERE user_id=?",BigDecimal.class,memberId).signum());
|
||||
assertEquals(1,jdbc.queryForObject("SELECT count(*) FROM wallet_entries WHERE request_id=?",Integer.class,ticket.id()));
|
||||
assertEquals(new BigDecimal("0.00034000"),jdbc.queryForObject("SELECT cost_amount FROM usage_events WHERE id=?",BigDecimal.class,ticket.id()));
|
||||
assertEquals(ticket.id().toString(),jdbc.queryForObject("SELECT request_id FROM usage_events WHERE id=?",String.class,ticket.id()));
|
||||
assertEquals(100,json.readTree(jdbc.queryForObject("SELECT output_payload::text FROM usage_events WHERE id=?",String.class,ticket.id())).at("/usage/prompt_tokens").intValue());
|
||||
assertEquals(2,wallet.entries(ownerToken,memberId).size());
|
||||
}
|
||||
@Test void reservationBlocksConcurrentSpendingAndAdminWithdrawals() throws Exception {
|
||||
var f=chatFixture();
|
||||
wallet.grant(ownerToken,memberId,UUID.randomUUID(),"1","billing test",meta);
|
||||
jdbc.update("UPDATE user_wallets SET balance=0.004 WHERE user_id=?",memberId);
|
||||
try(var executor=Executors.newFixedThreadPool(2)) {
|
||||
var ready=new CountDownLatch(1);
|
||||
Callable<Object> attempt=()->{ ready.await(); try { return f.reserve(null); } catch(ApiException e) { return e.getCode(); } };
|
||||
var a=executor.submit(attempt); var b=executor.submit(attempt); ready.countDown();
|
||||
var results=List.of(a.get(),b.get());
|
||||
assertEquals(1,results.stream().filter(x -> x instanceof ChatBillingService.Ticket).count());
|
||||
assertTrue(results.contains("INSUFFICIENT_BALANCE"));
|
||||
}
|
||||
assertEquals("INSUFFICIENT_BALANCE",assertThrows(ApiException.class,()->wallet.adjust(ownerToken,memberId,UUID.randomUUID(),"set","0","reserved balance test","0.004",meta)).getCode());
|
||||
}
|
||||
@Test void invalidUsageAndInterruptedCallsReleaseWithoutCharging() {
|
||||
var f=chatFixture();
|
||||
wallet.grant(ownerToken,memberId,UUID.randomUUID(),"1","billing test",meta);
|
||||
var ticket=f.reserve(null);
|
||||
assertEquals("UPSTREAM_USAGE_MISSING",assertThrows(ApiException.class,()->f.billing().settle(ticket,Map.of(),1,null)).getCode());
|
||||
f.billing().fail(ticket,"UPSTREAM_USAGE_MISSING","No actual usage",1);
|
||||
f.billing().fail(ticket,"RETRY","Already released",1);
|
||||
var abandoned=f.reserve(null);
|
||||
jdbc.update("UPDATE chat_billing_requests SET expires_at=now()-interval '1 second' WHERE id=?",abandoned.id());
|
||||
f.billing().recoverExpired(); f.billing().recoverExpired();
|
||||
assertEquals(new BigDecimal("1.00000000"),jdbc.queryForObject("SELECT balance FROM user_wallets WHERE user_id=?",BigDecimal.class,memberId));
|
||||
assertEquals(0,jdbc.queryForObject("SELECT reserved_balance FROM user_wallets WHERE user_id=?",BigDecimal.class,memberId).signum());
|
||||
assertEquals(0,jdbc.queryForObject("SELECT count(*) FROM wallet_entries WHERE operation='usage'",Integer.class));
|
||||
assertEquals(2,jdbc.queryForObject("SELECT count(*) FROM usage_events WHERE status='failed' AND cost_amount=0",Integer.class));
|
||||
}
|
||||
@Test void unexpectedProviderOverrunIsRecordedAsDebtAndBlocksNewCalls() {
|
||||
var f=chatFixture();
|
||||
wallet.grant(ownerToken,memberId,UUID.randomUUID(),"1","billing test",meta);
|
||||
var ticket=f.reserve(null);
|
||||
f.billing().settle(ticket,Map.of("usage",Map.of("prompt_tokens",1_000_000,"completion_tokens",0)),10,null);
|
||||
assertEquals(new BigDecimal("-1.00000000"),jdbc.queryForObject("SELECT balance FROM user_wallets WHERE user_id=?",BigDecimal.class,memberId));
|
||||
assertEquals("INSUFFICIENT_BALANCE",assertThrows(ApiException.class,()->f.reserve(null)).getCode());
|
||||
wallet.grant(ownerToken,memberId,UUID.randomUUID(),"0.50","partial debt repayment",meta);
|
||||
assertEquals(new BigDecimal("-0.50000000"),jdbc.queryForObject("SELECT balance FROM user_wallets WHERE user_id=?",BigDecimal.class,memberId));
|
||||
}
|
||||
@Test void unpricedRoutesAndEmptyBalancesFailBeforeCreatingHolds() {
|
||||
var f=chatFixture();
|
||||
assertEquals("INSUFFICIENT_BALANCE",assertThrows(ApiException.class,()->f.reserve(null)).getCode());
|
||||
f.model().remove("pricing");
|
||||
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());
|
||||
assertEquals(0,jdbc.queryForObject("SELECT count(*) FROM chat_billing_requests",Integer.class));
|
||||
}
|
||||
|
||||
@Test void gatewaySyncAndStreamRecordTheSameChargeAndPropagateFailures() throws Exception {
|
||||
var f=chatFixture();
|
||||
wallet.grant(ownerToken,memberId,UUID.randomUUID(),"1","gateway acceptance",meta);
|
||||
jdbc.update("UPDATE model_catalog SET pricing=?::jsonb WHERE id=?",json.writeValueAsString(f.model().get("pricing")),f.model().get("id"));
|
||||
String key=catalog.createKey(memberToken,Map.of("name","Local gateway test","modelIds",List.of(f.model().get("id"))),auth).get("key").toString();
|
||||
var upstream=com.sun.net.httpserver.HttpServer.create(new java.net.InetSocketAddress("127.0.0.1",0),0);
|
||||
var mode=new java.util.concurrent.atomic.AtomicReference<>("ok");
|
||||
var count=new java.util.concurrent.atomic.AtomicInteger();
|
||||
upstream.createContext("/v1/chat/completions",exchange->{
|
||||
count.incrementAndGet();
|
||||
var input=json.readTree(exchange.getRequestBody());
|
||||
if(mode.get().equals("http-error")) { exchange.sendResponseHeaders(500,-1); exchange.close(); return; }
|
||||
if(input.path("stream").asBoolean()) {
|
||||
exchange.getResponseHeaders().set("Content-Type","text/event-stream"); exchange.sendResponseHeaders(200,0);
|
||||
try(var out=exchange.getResponseBody()) {
|
||||
String frames="data: {\"model\":\"private-alias\",\"choices\":[{\"index\":0,\"delta\":{\"content\":\"OK\"},\"finish_reason\":\"stop\"}]}\n\n";
|
||||
if(!mode.get().equals("no-usage")) frames+="data: "+json.writeValueAsString(Map.of("choices",List.of(),"usage",meteredResult().get("usage")))+"\n\n";
|
||||
out.write((frames+"data: [DONE]\n\n").getBytes(java.nio.charset.StandardCharsets.UTF_8));
|
||||
}
|
||||
} else {
|
||||
byte[] body=json.writeValueAsBytes(meteredResult()); exchange.getResponseHeaders().set("Content-Type","application/json"); exchange.sendResponseHeaders(200,body.length);
|
||||
try(var out=exchange.getResponseBody()) { out.write(body); }
|
||||
}
|
||||
}); upstream.start();
|
||||
var environment=new org.springframework.mock.env.MockEnvironment().withProperty("moyi.providers.deepseek.api-key","local-fake")
|
||||
.withProperty("moyi.providers.deepseek.base-url","http://127.0.0.1:"+upstream.getAddress().getPort()+"/v1");
|
||||
var provider=new com.moyi.controlplane.provider.OpenAiCompatibleProvider(json,environment,jdbc,new SecretCipher("local-test"));
|
||||
try {
|
||||
var controller=new com.moyi.controlplane.gateway.GatewayController(catalog,provider,org.mockito.Mockito.mock(com.moyi.controlplane.gateway.GatewayGuard.class),f.billing(),json);
|
||||
var http=MockMvcBuilders.standaloneSetup(controller).setControllerAdvice(new ApiExceptionHandler()).build();
|
||||
var input=new LinkedHashMap<>(f.request().body());
|
||||
http.perform(post("/v1/chat/completions").header("Authorization","Bearer "+key).header("Idempotency-Key","retry-test").contentType("application/json").content(json.writeValueAsBytes(input)))
|
||||
.andExpect(status().isOk()).andExpect(header().string("X-Moyi-Cost-CNY","0.00034000")).andExpect(jsonPath("$.model").value(f.model().get("id")));
|
||||
http.perform(post("/v1/chat/completions").header("Authorization","Bearer "+key).header("Idempotency-Key","retry-test").contentType("application/json").content(json.writeValueAsBytes(input))).andExpect(status().isConflict());
|
||||
assertEquals(1,count.get());
|
||||
input.put("stream",true);
|
||||
var streamed=http.perform(post("/v1/chat/completions").header("Authorization","Bearer "+key).header("X-Moyi-Source","playground").contentType("application/json").content(json.writeValueAsBytes(input)))
|
||||
.andExpect(status().isOk()).andExpect(content().contentTypeCompatibleWith("text/event-stream")).andReturn().getResponse().getContentAsString();
|
||||
assertTrue(streamed.endsWith("data: [DONE]\n\n")); assertFalse(streamed.contains("private-alias"));
|
||||
assertEquals(new BigDecimal("0.99932000"),jdbc.queryForObject("SELECT balance FROM user_wallets WHERE user_id=?",BigDecimal.class,memberId));
|
||||
assertEquals(1,jdbc.queryForObject("SELECT count(*) FROM usage_events WHERE call_mode='stream' AND request_source='playground' AND cost_amount=0.00034 AND first_token_ms IS NOT NULL",Integer.class));
|
||||
mode.set("no-usage");
|
||||
var failed=http.perform(post("/v1/chat/completions").header("Authorization","Bearer "+key).contentType("application/json").content(json.writeValueAsBytes(input))).andReturn().getResponse().getContentAsString();
|
||||
assertTrue(failed.contains("UPSTREAM_USAGE_MISSING"));
|
||||
mode.set("http-error");
|
||||
http.perform(post("/v1/chat/completions").header("Authorization","Bearer "+key).contentType("application/json").content(json.writeValueAsBytes(input))).andExpect(status().isBadGateway());
|
||||
assertEquals(new BigDecimal("0.99932000"),jdbc.queryForObject("SELECT balance FROM user_wallets WHERE user_id=?",BigDecimal.class,memberId));
|
||||
assertEquals(0,jdbc.queryForObject("SELECT reserved_balance FROM user_wallets WHERE user_id=?",BigDecimal.class,memberId).signum());
|
||||
mode.set("ok");
|
||||
var disconnected=new org.springframework.mock.web.MockHttpServletResponse() {
|
||||
@Override public jakarta.servlet.ServletOutputStream getOutputStream() {
|
||||
return new jakarta.servlet.ServletOutputStream() {
|
||||
@Override public boolean isReady() { return true; }
|
||||
@Override public void setWriteListener(jakarta.servlet.WriteListener listener) { }
|
||||
@Override public void write(int b) throws java.io.IOException { throw new java.io.IOException("Browser closed"); }
|
||||
};
|
||||
}
|
||||
};
|
||||
controller.chatCompletions("Bearer "+key,input,"closed-browser",disconnected);
|
||||
assertEquals(new BigDecimal("0.99898000"),jdbc.queryForObject("SELECT balance FROM user_wallets WHERE user_id=?",BigDecimal.class,memberId));
|
||||
assertEquals(3,jdbc.queryForObject("SELECT count(*) FROM wallet_entries WHERE user_id=? AND operation='usage'",Integer.class,memberId));
|
||||
assertEquals(new BigDecimal("0.00102000"),catalog.overview(memberToken,auth).get("costThisMonth"));
|
||||
} finally { provider.close(); upstream.stop(0); }
|
||||
}
|
||||
|
||||
@Test void failedLedgerTransactionCannotPartiallyDebitTheWallet() {
|
||||
var f=chatFixture(); wallet.grant(ownerToken,memberId,UUID.randomUUID(),"1","atomicity test",meta);
|
||||
var ticket=f.reserve(null);
|
||||
jdbc.execute("ALTER TABLE usage_events ADD CONSTRAINT test_reject_cost CHECK (cost_amount IS NULL OR cost_amount=0)");
|
||||
assertThrows(org.springframework.dao.DataIntegrityViolationException.class,()->f.billing().settle(ticket,meteredResult(),5,null));
|
||||
assertEquals(new BigDecimal("1.00000000"),jdbc.queryForObject("SELECT balance FROM user_wallets WHERE user_id=?",BigDecimal.class,memberId));
|
||||
assertEquals(0,jdbc.queryForObject("SELECT count(*) FROM wallet_entries WHERE operation='usage'",Integer.class));
|
||||
assertEquals("pending",jdbc.queryForObject("SELECT status FROM chat_billing_requests WHERE id=?",String.class,ticket.id()));
|
||||
f.billing().fail(ticket,"SETTLEMENT_FAILED","Test rollback",5);
|
||||
assertEquals(0,jdbc.queryForObject("SELECT reserved_balance FROM user_wallets WHERE user_id=?",BigDecimal.class,memberId).signum());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
package com.moyi.controlplane.gateway;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.moyi.controlplane.common.ApiException;
|
||||
import com.moyi.controlplane.provider.OpenAiCompatibleProvider;
|
||||
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.*;
|
||||
import org.junit.jupiter.api.*;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.mock.env.MockEnvironment;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
class ChatStreamingTest {
|
||||
HttpServer server;
|
||||
OpenAiCompatibleProvider provider;
|
||||
ObjectMapper json=new ObjectMapper();
|
||||
Map<String,Object> request=Map.of("messages",List.of(Map.of("role","user","content","测试")),"max_tokens",16,"temperature",0.2,"stream",true);
|
||||
@BeforeEach void setup() throws Exception {
|
||||
server=HttpServer.create(new InetSocketAddress("127.0.0.1",0),0);
|
||||
var env=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","2");
|
||||
provider=new OpenAiCompatibleProvider(json,env,mock(JdbcTemplate.class),new SecretCipher("local-test-only"));
|
||||
}
|
||||
@AfterEach void close() { provider.close(); server.stop(0); }
|
||||
@Test void fragmentedUtf8StreamsBeforeCompletionAndCarriesActualUsage() throws Exception {
|
||||
var first=new CountDownLatch(1); var finish=new CountDownLatch(1);
|
||||
var received=new CompletableFuture<Map<?,?>>();
|
||||
server.createContext("/v1/chat/completions", exchange -> {
|
||||
received.complete(json.readValue(exchange.getRequestBody(),Map.class));
|
||||
exchange.getResponseHeaders().set("Content-Type","text/event-stream"); exchange.sendResponseHeaders(200,0);
|
||||
try(var out=exchange.getResponseBody()) {
|
||||
byte[] initial="data: {\"id\":\"test\",\"choices\":[{\"index\":0,\"delta\":{\"content\":\"你好\"}}]}\r\n\r\n".getBytes(StandardCharsets.UTF_8);
|
||||
for(byte b:initial) { out.write(b); out.flush(); }
|
||||
try { if(!finish.await(1,TimeUnit.SECONDS)) throw new AssertionError("first delta was buffered"); } catch(InterruptedException e) { Thread.currentThread().interrupt(); }
|
||||
out.write(("data: {\"choices\":[{\"index\":0,\"delta\":{},\"finish_reason\":\"stop\"}]}\n\n"
|
||||
+"data: {\"choices\":[],\"usage\":{\"prompt_tokens\":20,\"completion_tokens\":2,\"prompt_tokens_details\":{\"cached_tokens\":10}}}\n\n"
|
||||
+"data: [DONE]\n\n").getBytes(StandardCharsets.UTF_8));
|
||||
}
|
||||
}); server.start();
|
||||
try(var pool=Executors.newSingleThreadExecutor()) {
|
||||
var result=pool.submit(()->{
|
||||
var collector=new ChatStreamCollector("public-route",System.currentTimeMillis());
|
||||
try(var stream=provider.stream("provider-deepseek","upstream-alias",request)) {
|
||||
stream.events(chunk->{ collector.accept(chunk); first.countDown(); });
|
||||
}
|
||||
assertNotNull(collector.firstTokenMs()); return collector.result();
|
||||
});
|
||||
assertTrue(first.await(1500,TimeUnit.MILLISECONDS)); assertFalse(result.isDone()); finish.countDown();
|
||||
var body=result.get(2,TimeUnit.SECONDS);
|
||||
assertEquals("你好",((Map<?,?>)((Map<?,?>)((List<?>)body.get("choices")).getFirst()).get("message")).get("content"));
|
||||
assertEquals(new ChatMeter.Tokens(20,2,10),ChatMeter.tokens(body));
|
||||
assertEquals("public-route",body.get("model"));
|
||||
}
|
||||
assertEquals("upstream-alias",received.get().get("model"));
|
||||
assertEquals(16,received.get().get("max_tokens"));
|
||||
assertEquals(Map.of("include_usage",true),received.get().get("stream_options"));
|
||||
}
|
||||
@Test void deadlineClosesAStreamThatNeverFinishes() {
|
||||
server.createContext("/v1/chat/completions", exchange->{
|
||||
exchange.getResponseHeaders().set("Content-Type","text/event-stream"); exchange.sendResponseHeaders(200,0);
|
||||
exchange.getResponseBody().write(": heartbeat\n\n".getBytes()); exchange.getResponseBody().flush();
|
||||
// Leave the body open: the client hard deadline must terminate the read.
|
||||
}); server.start();
|
||||
long started=System.nanoTime();
|
||||
assertThrows(Exception.class,()->{ try(var stream=provider.stream("provider-deepseek","alias",request)) { stream.events(chunk->{}); } });
|
||||
assertTrue(TimeUnit.NANOSECONDS.toMillis(System.nanoTime()-started)<4000);
|
||||
}
|
||||
@Test void upstreamHttpFailureNeverBecomesSuccessfulStream() {
|
||||
server.createContext("/v1/chat/completions",e->{ e.sendResponseHeaders(401,-1); e.close(); }); server.start();
|
||||
assertEquals(502,assertThrows(ApiException.class,()->provider.stream("provider-deepseek","alias",request)).getStatus());
|
||||
}
|
||||
@Test void eofWithoutDoneAndMissingUsageAreNotBillable() {
|
||||
server.createContext("/v1/chat/completions",e->{
|
||||
e.getResponseHeaders().set("Content-Type","text/event-stream"); e.sendResponseHeaders(200,0);
|
||||
e.getResponseBody().write("data: {\"choices\":[]}\n\n".getBytes()); e.close();
|
||||
}); server.start();
|
||||
assertEquals("UPSTREAM_STREAM_INCOMPLETE",assertThrows(ApiException.class,()->{ try(var stream=provider.stream("provider-deepseek","alias",request)) { stream.events(chunk->{}); } }).getCode());
|
||||
assertThrows(ApiException.class,()->ChatMeter.tokens(Map.of("usage",Map.of("prompt_tokens",1.5,"completion_tokens",2))));
|
||||
assertThrows(ApiException.class,()->ChatMeter.tokens(Map.of("usage",Map.of("prompt_tokens",1,"completion_tokens",2,"prompt_cache_hit_tokens",3))));
|
||||
}
|
||||
@Test void tokenBoundsAndUnsupportedMultimodalAreRejectedBeforeUpstream() {
|
||||
assertThrows(ApiException.class,()->ChatMeter.request(Map.of("messages",List.of(Map.of("role","user","content",List.of(Map.of("type","image_url"))))),json));
|
||||
assertThrows(ApiException.class,()->ChatMeter.request(Map.of("messages",List.of(Map.of("role","user","content","test")),"max_tokens",0),json));
|
||||
assertEquals(4096,ChatMeter.request(Map.of("messages",List.of(Map.of("role","user","content","test"))),json).maxOutputTokens());
|
||||
}
|
||||
}
|
||||
+10
-5
@@ -6,6 +6,8 @@ import com.moyi.controlplane.common.ApiException;
|
||||
import com.moyi.controlplane.provider.OpenAiCompatibleProvider;
|
||||
import com.moyi.controlplane.task.AsyncTaskService;
|
||||
import com.moyi.controlplane.usage.UsageAuditService;
|
||||
import com.moyi.controlplane.wallet.ChatBillingService;
|
||||
import org.springframework.mock.web.MockHttpServletResponse;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
@@ -42,11 +44,11 @@ class GatewayDeploymentTest {
|
||||
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.keyCanInvoke(key, "model-real")).thenReturn(true);
|
||||
GatewayController controller = new GatewayController(catalog, provider, guard, usage);
|
||||
ApiException error = assertThrows(ApiException.class, () -> controller.chatCompletions("Bearer test-key", Map.of("model", "model-real")));
|
||||
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(), anyList());
|
||||
verify(provider, never()).chat(anyString(), anyString(), anyMap());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -56,8 +58,11 @@ class GatewayDeploymentTest {
|
||||
when(catalog.authenticateKey("test-key")).thenReturn(key);
|
||||
when(catalog.activeModel("mock")).thenReturn(Map.of("id", "mock", "providerId", "provider-mock", "mode", "sync"));
|
||||
when(catalog.keyCanInvoke(key, "mock")).thenReturn(true);
|
||||
GatewayController controller = new GatewayController(catalog, mock(OpenAiCompatibleProvider.class), mock(GatewayGuard.class), mock(UsageAuditService.class));
|
||||
assertEquals(200, controller.chatCompletions("Bearer test-key", Map.of("model", "mock")).getStatusCode().value());
|
||||
var billing=mock(ChatBillingService.class);
|
||||
when(billing.reserve(anyMap(),anyMap(),any(),any())).thenReturn(new ChatBillingService.Ticket(UUID.randomUUID(),UUID.fromString(key.get("userId").toString()),ChatMeter.rates(null,true)));
|
||||
when(billing.settle(any(),anyMap(),anyInt(),any())).thenReturn(java.math.BigDecimal.ZERO);
|
||||
GatewayController controller = new GatewayController(catalog, mock(OpenAiCompatibleProvider.class), mock(GatewayGuard.class), billing,new ObjectMapper());
|
||||
assertEquals(200, controller.chatCompletions("Bearer test-key", Map.of("model", "mock","messages",List.of(Map.of("role","user","content","test"))),null,new MockHttpServletResponse()).getStatusCode().value());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -78,7 +78,7 @@ Invoke-WebRequest http://localhost:8080/health
|
||||
本节点完成的是“控制面基础闭环 + 同步供应商转发 + Redis 网关保护 + 持久化任务队列”,不是生产网关完成版:
|
||||
|
||||
- 数据已写入 PostgreSQL;Redis 已用于按 API Key 限流、按模型并发租约和 Redis Lua 原子操作。默认同步调用每 Key 每分钟 60 次、模型并发 4,均可通过环境变量调整;Redis 不可用时网关返回 503,不会绕过保护直接请求上游;
|
||||
- `/v1/chat/completions` 已支持 DeepSeek、Kimi、GLM 的 OpenAI 兼容非流式转发;真实供应商未配置 Key 时返回 503,只有 `provider-mock` 可以返回 Mock;流式响应暂未开放;
|
||||
- `/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;
|
||||
- 异步任务已持久化并提供按模型的排队上限(默认 100,可由 `TASK_MAX_QUEUED_PER_MODEL` 调整),但真实供应商执行器、优先级调度、价格/额度扣减和计费账单仍沿用下一节点计划;
|
||||
|
||||
@@ -12,7 +12,7 @@
|
||||
| 缓存 | 现有北京 Tair 标准架构,专用账号及 `moyi:test:` Key 前缀 |
|
||||
| 对象存储 | 北京 OSS 可预留新 Bucket,本次 Java 部署暂不需要 OSS 密钥 |
|
||||
|
||||
这是内部测试发布,不是完整生产版。可验收登录、邀请码、目录、项目、Key 权限、非流式同步转发、限流、模型并发和调用审计。以下尚未完成:Java 真实 Tripo/Seedance 异步执行、OSS 文件保存、流式转发、完整计费扣减、分布式任务抢占和可靠重试。
|
||||
这是内部测试发布,不是完整生产版。可验收登录、邀请码、目录、项目、Key 权限、同步/流式文本转发、人民币用量结算、限流、模型并发和调用审计。真实分组必须配置售价且调用者余额充足;V13 升级要求见 [流式与计费](16-streaming-chat-billing.md)。以下尚未完成:Java 真实 Tripo/Seedance 异步执行、OSS 文件保存、支付充值、分布式任务抢占和可靠重试。
|
||||
|
||||
真实供应商没有配置密钥时返回 503,不再返回 Mock 成功。只有明确绑定 `provider-mock` 的模型可以走 Mock;真实异步供应商返回 501。已有种子目录和价格只是联调数据,实际模型 ID/能力须由供应商控制台核验后修改。
|
||||
|
||||
|
||||
@@ -72,7 +72,7 @@ python3 backup_database.py --deployment-dir /opt/moyi/releases/20260917-202503-f
|
||||
|
||||
发放金额以人民币元输入,最多两位小数、必须大于 0,单笔上限 100 万元。发放必须填写原因,记录操作者、时间、金额和发放后余额;使用事务、钱包行锁和请求编号去重。发放请求遇到网络不确定状态时,原弹窗重试沿用同一个编号,不会重复发放。
|
||||
|
||||
本次完成的是余额、发放、查询、流水与审计;不是充值支付或自动扣费。分组价格目前是报价元数据,不能把美元报价或每千/百万 token 单位直接用于人民币扣费。网关尚不根据余额拦截或结算;对外收费前仍必须实现明确币种和计费单位、预占额度、成功结算、失败退回及对账。优先级也尚未用于自动故障切换。
|
||||
最初版本只提供人工余额调整;V13 已补充文本聊天余额预留、成功结算、失败释放和防重复扣费,见 [流式与计费](16-streaming-chat-billing.md)。分组必须明确人民币 / 百万 Token 的输入、输出售价,不能直接沿用美元报价。支付充值和按优先级自动故障切换仍未实现。
|
||||
|
||||
## 本地验证
|
||||
|
||||
|
||||
@@ -0,0 +1,181 @@
|
||||
# ApiHub、TokenFoundryX、APIPro 与 Tripo 接入清单
|
||||
|
||||
核对日期:2026-09-20。MOYI 代码基线:7360cf3e。
|
||||
|
||||
本文记录供应商页面实际展示的模型 ID,不代表对其底层模型身份、宣传能力或官方版本名称的独立认证。目录可见不等于当前 Key 有权限,也不等于已经在 MOYI 实测。
|
||||
|
||||
## 已完成和待完成
|
||||
|
||||
- 已登录核对 ApiHub 国内站的 25 个模型,以及 TokenFoundryX 国外站的 13 个模型。
|
||||
- ApiHub 国内站游乐场和 MOYI 网关均已验证 `deepseek-v4-flash`。2026-09-20 00:58:48(北京时间)MOYI 返回 `OK`,记录输入 101、输出 8 Token,耗时 1.89 秒。
|
||||
- 两站控制台分别给出自己的 `/v1/chat/completions` 示例;国内钱包按 CNY 展示,国外钱包按 USD 展示。国外站控制台当时提示需要创建 API Key;不能假定国内 Key 可直接复用。
|
||||
- APIPro 已登录;公开模型广场显示 305 个模型,控制台确认聊天地址为 `https://api.apipro.ai/v1/chat/completions`,当前导航文档地址为 `https://apipro.apifox.cn/`。
|
||||
- MOYI 已有用户创建的 `apihub` 渠道(名称“内测1”)和保存的凭据。本次已将协议从 HTTP 修正为 OpenAI 兼容,将 Base URL 补全为 `https://apihub.chem-cloud.cn/v1`,未读取或替换原凭据。
|
||||
- 已在现有 DeepSeek V4 Flash 下添加“ApiHub 国内”分组,关联“内测1”,路由 ID `3dc5d0f8-c7b5-4ff3-9ae8-9077e2e957a9`。
|
||||
- APIPro 渠道已保存,添加“APIPro · DeepSeek”分组,路由 ID `6680d434-538f-408a-bf9a-668801763b79`。2026-09-20 01:20:14 MOYI 返回 `OK`,输入 22、输出 31 Token,耗时 2.12 秒。
|
||||
- 两次均为旧版服务器上的同步调用,平台费用显示未记录;只验证渠道和模型路由,不代表新版流式/计费已部署验收。两个 MOYI 临时联调 Key 均已停用,调用记录保留。
|
||||
|
||||
## 渠道管理填写
|
||||
|
||||
在 MOYI 管理端打开“渠道管理”。国内站已存在 `apihub` / “内测1”,编辑该渠道即可;其他渠道可按下表添加。以下名称可自行修改。
|
||||
|
||||
| 字段 | ApiHub 国内 | TokenFoundryX 国外 | APIPro |
|
||||
|---|---|---|---|
|
||||
| 渠道 Code | 现有 `apihub` | `tokenfoundryx-global` | `apipro` |
|
||||
| 渠道名称 | 现有“内测1” | TokenFoundryX 国外 | APIPro |
|
||||
| 来源类型 | 中转站 | 中转站 | 中转站 |
|
||||
| 接入协议 | OpenAI 兼容 | OpenAI 兼容(用于其聊天端点) | OpenAI 兼容 |
|
||||
| Base URL | `https://apihub.chem-cloud.cn/v1` | `https://www.tokenfoundryx.com/v1` | `https://api.apipro.ai/v1` |
|
||||
| 凭据类型 | API Key | API Key | API Key |
|
||||
| 凭据 | 国内站创建的 API Key 原文 | 国外站创建的 API Key 原文 | 该站对应 Key |
|
||||
|
||||
不要在凭据框添加 `Bearer ` 前缀,当前 MOYI 后端自动添加该前缀。Base URL 只填到 `/v1`,后端追加 `/chat/completions`,不要填完整聊天请求路径。
|
||||
|
||||
MOYI 用户对外使用的统一 API Base URL 为 `https://moyi.soulofmodel.com/v1`。这是本平台入口,与渠道表中的上游地址不同。
|
||||
|
||||
## 第一条模型与分组
|
||||
|
||||
先接入已在上游游乐场验证的 `deepseek-v4-flash`。
|
||||
|
||||
在“模型与分组”搜索已有模型;如果已有相同定义,编辑并添加分组即可,避免重复创建。
|
||||
|
||||
| 添加/编辑模型字段 | 填写值 |
|
||||
|---|---|
|
||||
| 模型名称 | DeepSeek V4 Flash |
|
||||
| 统一模型标识 | `deepseek-v4-flash` |
|
||||
| 模型类型 | 文本 |
|
||||
| 调用模式 | 同步 |
|
||||
| 输入类型 | `text` |
|
||||
| 输出类型 | `text` |
|
||||
| 模型说明 | ApiHub 国内转发,首轮验证文本聊天 |
|
||||
|
||||
在该模型下点击“添加分组”:
|
||||
|
||||
| 字段 | 填写值 |
|
||||
|---|---|
|
||||
| 分组名称 | ApiHub 国内 |
|
||||
| 渠道 | 内测1(Code 为 `apihub`) |
|
||||
| 上游模型标识 | `deepseek-v4-flash` |
|
||||
| 输入价格、输出价格 | 待确定平台人民币售价后填写;新版计费会阻止未定价真实模型调用 |
|
||||
| 价格单位 | `CNY / 1M tokens` |
|
||||
| 优先级 | `100` |
|
||||
| 并发上限 | `1` |
|
||||
|
||||
上游模型广场的价格可能包括折扣、缓存、动态阶梯。它是上游成本参考,不应未经确认直接作为本平台售价。美元金额也不能原样写入人民币报价。
|
||||
|
||||
“ApiHub 国内”等 MOYI 分组名由本平台自定义;上游的 `default`、`5.8折` 属于上游账号/Key 的路由和定价规则,两者不会自动同步。需要在上游 Key 页面确认该 Key 可访问目标模型及所需上游分组。
|
||||
|
||||
## 国内站目录:25 个
|
||||
|
||||
来源:[ApiHub 模型广场](https://apihub.chem-cloud.cn/pricing)。下表为登录后两页目录中的准确标识。
|
||||
|
||||
| 上游模型标识 | 当前 MOYI 首轮处理 |
|
||||
|---|---|
|
||||
| `deepseek-v4-flash` | 文本、同步;首个联调模型 |
|
||||
| `deepseek-v4-pro` | 文本、同步;逐个测试 |
|
||||
| `glm-4.7` | 文本聊天、同步;逐个测试 |
|
||||
| `glm-5` | 文本聊天、同步;逐个测试 |
|
||||
| `glm-5.1` | 文本聊天、同步;逐个测试 |
|
||||
| `glm-5.2` | 文本聊天、同步;逐个测试 |
|
||||
| `kimi-k2.5` | 首轮仅测试文本聊天、同步 |
|
||||
| `kimi-k2.6` | 首轮仅测试文本聊天、同步 |
|
||||
| `kimi-k2.7-code` | 代码、同步;逐个测试 |
|
||||
| `kimi-k3` | 文本聊天、同步;逐个测试 |
|
||||
| `qwen3-coder-plus` | 代码、同步;逐个测试 |
|
||||
| `qwen3-max` | 文本聊天、同步;逐个测试 |
|
||||
| `qwen3.5-plus` | 首轮仅测试文本聊天、同步 |
|
||||
| `qwen3.6-plus` | 首轮仅测试文本聊天、同步 |
|
||||
| `qwen3.7-max` | 文本聊天、同步;逐个测试 |
|
||||
| `qwen3.7-plus` | 文本聊天、同步;逐个测试 |
|
||||
| `qwen3.8-max` | 文本聊天、同步;逐个测试 |
|
||||
| `doubao-seedream-5-0-260128` | 图像生成;当前网关未实现对应接口,暂不启用 |
|
||||
| `doubao-seedream-5-0-pro-260628` | 图像生成;当前网关未实现对应接口,暂不启用 |
|
||||
| `doubao-seedance-1-0-pro-250528` | 视频任务;需补齐上游异步适配,暂不启用 |
|
||||
| `doubao-seedance-1-0-pro-fast-251015` | 视频任务;需补齐上游异步适配,暂不启用 |
|
||||
| `doubao-seedance-1-5-pro-251215` | 视频任务;需补齐上游异步适配,暂不启用 |
|
||||
| `doubao-seedance-2-0-260128` | 视频任务;需补齐上游异步适配,暂不启用 |
|
||||
| `doubao-seedance-2-0-fast-260128` | 视频任务;需补齐上游异步适配,暂不启用 |
|
||||
| `doubao-seedance-2-0-mini-260615` | 视频任务;需补齐上游异步适配,暂不启用 |
|
||||
|
||||
先完成一个文本模型的 MOYI 测试,再添加其他文本模型。输入图片、工具调用等能力需要单独验收,不能仅凭模型广场的标签声明已支持。
|
||||
|
||||
## 国外站目录:13 个
|
||||
|
||||
来源:[TokenFoundryX 模型广场](https://www.tokenfoundryx.com/pricing)。使用单独渠道和相应站点 API Key;MOYI 分组名称建议 `TokenFoundryX 国外`。
|
||||
|
||||
| 上游模型标识 | 当前 MOYI 首轮处理 |
|
||||
|---|---|
|
||||
| `claude-fable-5` | 站内标记 OpenAI/Anthropic;先确认聊天兼容性再启用 |
|
||||
| `claude-fable-5-1` | 站内标记 OpenAI/Anthropic;先确认聊天兼容性再启用 |
|
||||
| `claude-opus-4-6` | 站内标记 OpenAI/Anthropic;先确认聊天兼容性再启用 |
|
||||
| `claude-opus-4-7` | 站内标记 OpenAI/Anthropic;先确认聊天兼容性再启用 |
|
||||
| `claude-opus-4-8` | 站内标记 OpenAI/Anthropic;先确认聊天兼容性再启用 |
|
||||
| `claude-opus-5` | 站内标记 OpenAI/Anthropic;先确认聊天兼容性再启用 |
|
||||
| `claude-sonnet-4-5` | 站内标记 OpenAI/Anthropic;先确认聊天兼容性再启用 |
|
||||
| `claude-sonnet-4-6` | 站内标记 OpenAI/Anthropic;先确认聊天兼容性再启用 |
|
||||
| `claude-sonnet-5` | 站内标记 OpenAI/Anthropic;先确认聊天兼容性再启用 |
|
||||
| `gpt-5.6-sol` | 控制台给出聊天请求示例,待真实 Key 测试 |
|
||||
| `gpt-5.6-terra` | 站内标记 OpenAI;待聊天兼容性测试 |
|
||||
| `gpt-6-astra` | 站内标记 OpenAI;待聊天兼容性测试 |
|
||||
| `gpt-image-2` | 图像生成;当前网关未实现对应接口,暂不启用 |
|
||||
|
||||
该目录仅按中转站展示原样记录,不将别名等同于厂商官方模型身份。
|
||||
|
||||
## APIPro
|
||||
|
||||
[控制台](https://api.apipro.ai/dashboard/overview)、[模型定价](https://api.apipro.ai/pricing)、[当前 API 文档](https://apipro.apifox.cn/)、[聊天接口文档](https://apipro.apifox.cn/402023923e0)。
|
||||
|
||||
控制台最新公告为 2026-09-17“国内站点下架,统一使用海外站点”。本次登录确认的聊天示例是 `POST https://api.apipro.ai/v1/chat/completions`,认证为 `Authorization: Bearer <API_KEY>`。MOYI Base URL 填 `https://api.apipro.ai/v1`。控制台加载慢不等同于模型请求耗时;实际调用需要分别验证。
|
||||
|
||||
模型详情自动生成的 OpenAI 示例曾把 DeepSeek 文本请求地址写成 `/v1/videos`,与控制台聊天示例和专门的聊天文档不一致。文本聊天应使用 `/v1/chat/completions`,不要直接照抄该错误示例。
|
||||
|
||||
广场显示 305 个模型;以下 13 个为本次按 `deepseek` 搜索实际核对的完整子集,不代表 305 个全部已逐一验收:
|
||||
|
||||
| 上游模型标识 | 首轮用途 |
|
||||
|---|---|
|
||||
| `deepseek-ocr` | OCR;不纳入首轮纯文本测试 |
|
||||
| `deepseek-r1` | 备用文本模型,待测 |
|
||||
| `deepseek-r1-distill-qwen-32b` | 备用文本模型,待测 |
|
||||
| `deepseek-v3` | 备用文本模型,待测 |
|
||||
| `deepseek-v3-0324` | 备用文本模型,待测 |
|
||||
| `deepseek-v3.1` | 备用文本模型,待测 |
|
||||
| `deepseek-v3.2` | 备用文本模型,待测 |
|
||||
| `deepseek-v3.2-think` | 推理模型,待测 |
|
||||
| `deepseek-v4-flash` | 首个联调模型,复用 MOYI 已有模型定义 |
|
||||
| `deepseek-v4-flash-0731` | 站内版本标识,待测 |
|
||||
| `deepseek-v4-pro` | 第二个候选,复用 MOYI 已有模型定义 |
|
||||
| `deepseek-v4-pro-0813` | 站内版本标识,待测 |
|
||||
| `deepseek-v4.1-flash` | 站内版本标识,待测 |
|
||||
|
||||
`deepseek-v4-flash` 详情页列出 `bailian`、`deepseek`、`deepseek-claude`、`llm-cn` 的价格行;创建 Key 时账号实际可选 `deepseek`。首轮选此分组,限制模型为 `deepseek-v4-flash`。MOYI 下建议添加分组“APIPro · DeepSeek”,关联 `apipro` 渠道,上游模型保持 `deepseek-v4-flash`。
|
||||
|
||||
该模型页面当前展示输入 0.143 USD / 1M、输出 0.286 USD / 1M、缓存输入 0.029 USD / 1M。这是上游页面报价,真实消费以日志为准,不能直接当成人民币售价。MOYI 的自定义分组名不会自动改变 APIPro Key 所选的分组;若以后要区分 APIPro 内部的多个来源,应分别创建上游 Key 和 MOYI 渠道,再将各模型分组关联到对应渠道。
|
||||
|
||||
已创建并保存的 `MOYI联调` 上游 Key 仅允许 `deepseek` 分组和 `deepseek-v4-flash`,1 天有效、额度上限 1 USD,页面显示到期时间 2026-09-21 00:11。它是短期测试凭据,持续使用前必须在 APIPro 创建适当范围的正式 Key,再编辑 MOYI 的 APIPro 渠道替换凭据。
|
||||
|
||||
## Tripo 官方
|
||||
|
||||
国际站官方文档:[General](https://platform.tripo3d.ai/docs/general)、[Authentication](https://platform.tripo3d.ai/docs/quick-start)、[Generation](https://platform.tripo3d.ai/docs/generation)。
|
||||
|
||||
- 国际站 API Base URL:`https://api.tripo3d.ai/v2/openapi`。
|
||||
- 使用 HTTP Bearer API Key;创建任务为 `POST /task`,查询为 `GET /task/{task_id}`。
|
||||
- 文生 3D 请求使用 `type=text_to_model`,版本放在 `model_version`,不能按 OpenAI 聊天请求直接转发。
|
||||
- 当前官方生成文档列出的版本包括 `P1-20260311`、`Turbo-v1.0-20250506`、`v3.1-20260211`、`v3.0-20250812`、`v2.5-20250123` 等,实际选型需结合账号权限及价格。
|
||||
- 若 Key 来自国内 `platform.tripo3d.com`,使用其对应文档与端点;不能假定 `.ai` 和 `.com` 凭据互通。
|
||||
- 建议渠道 Code `tripo-official`、名称 `Tripo 官方`、来源“官方供应商”、协议 HTTP、凭据类型 Bearer Token。先不要把真实 3D 模型作为已可用发布。
|
||||
- 7360cf3e 的 Java `AsyncTaskService` 对非 Mock 异步供应商返回 `ASYNC_PROVIDER_NOT_IMPLEMENTED`。必须补齐创建、轮询、状态持久化、错误处理、结果链接与使用记录后,才能在 MOYI 使用官方 Key 测试。
|
||||
|
||||
## MOYI 联调顺序
|
||||
|
||||
1. 在管理员渠道表单填写国内站 Key;只输入在自己的管理端,不贴入聊天、文档或 Git。
|
||||
2. 建立上述 DeepSeek 模型及 `ApiHub 国内` 分组,确认渠道、模型和分组均启用。
|
||||
3. 切到用户端“API Keys”,创建一个联调 Key,先选择模型,再勾选该分组。
|
||||
4. 在“在线调试”选择该模型和分组,使用 MOYI 平台生成的 Key,发送短提示词“请只回复 OK”。这里不使用上游 Key。
|
||||
5. 确认返回非 Mock 的实际文本;在“使用记录”检查模型、分组、状态、耗时及 Token,在上游日志核对对应请求。
|
||||
6. 再逐个添加其他文本模型和 APIPro 分组。每个新分组需明确授权给联调 Key;已有 Key 不会自动获得新分组权限。
|
||||
|
||||
程序调用时先请求 `GET https://moyi.soulofmodel.com/v1/models`,使用返回的 `data[].id` 作为聊天请求 `model`。当前实现使用分组/路由 ID,表单中的“统一模型标识”是展示字段,不是可直接调用的别名。旧版使用 `stream:false`;部署包含 V13 的新版后可使用 `stream:true`。
|
||||
|
||||
新版提供 OpenAI 兼容聊天的同步/流式转发、文本消息、工具参数透传和输出上限。Responses 原生接口、Anthropic 原生接口、多模态输入、真实图像/视频/3D 执行器仍需单独适配。中转站各模型的具体参数支持需要逐个验证。
|
||||
|
||||
新版按分组人民币售价预留余额,以实际 Token 用量结算,失败释放预留。部署前需填写价格并给测试账号发放余额,否则真实模型会被拒绝调用。报价、计费精度、断流及幂等规则见 [流式聊天与人民币计费](16-streaming-chat-billing.md)。平台用户账本和上游钱包独立,上游消费不等于平台收入。
|
||||
@@ -0,0 +1,50 @@
|
||||
# 流式聊天与人民币计费
|
||||
|
||||
2026-09-20 新增,数据库迁移 V13。代码和本机测试完成不代表服务器已升级;ApiHub/APIPro 的线上同步联调证据见 [渠道清单](15-relay-onboarding.md)。
|
||||
|
||||
## 管理端配置
|
||||
|
||||
在“模型与分组 → 编辑分组”填写输入、输出人民币售价,单位必须为 `CNY / 1M tokens`(人民币 / 百万 Token)。缓存输入价可选,留空沿用输入价。每项最多 8 位小数;明确填写 0 表示免费,留空不是免费。真实模型缺少价格或使用其他币种/单位时返回 `MODEL_NOT_PRICED`,不会请求上游。
|
||||
|
||||
例如本机测试使用输入 2、输出 4、缓存输入 1;这只是测试数据,不是 ApiHub/APIPro 正式售价。输入 100 Token(其中缓存 60)、输出 80 时:
|
||||
|
||||
`费用 = ((100 - 60) × 2 + 60 × 1 + 80 × 4) / 1,000,000 = 0.00034 元`
|
||||
|
||||
按实际 `usage.prompt_tokens`、`completion_tokens` 和缓存输入统计计算,人民币金额保留 8 位小数、四舍五入。缺少合法用量时不估算收费。Mock 路由明确免费。
|
||||
|
||||
调用者账户必须有足够余额,包括超级管理员自己的 API Key;管理端“用户与额度”可发放/调整。用户看到的可用额度等于账面余额减正在调用的预留金额。本月消耗来自扣费流水,不补算旧记录。
|
||||
|
||||
## 一次调用的账务行为
|
||||
|
||||
1. 验证 Key、模型权限、上游配置、请求参数、限流和并发后,锁定用户钱包并预留费用。预留采用文本请求字节数与消息开销的保守输入估计,加最大输出 Token;它不是最终扣款。
|
||||
2. 调用开始即记录请求 ID、分组、来源、同步/流式模式和开始时间。价格保存快照,调用期间改价不会影响本次结算。
|
||||
3. 上游正常结束且用量完整时,在同一 PostgreSQL 事务中更新余额、写入一条模型扣费流水并完成使用记录。同步和流式使用相同公式。
|
||||
4. 上游错误、超时、缺失用量、缺失完成标记时,本平台不扣费并释放预留;中转站是否收费取决于其账单。服务进程崩溃留下的预留在 5 分钟过期后由每分钟运行的恢复任务释放。
|
||||
5. 用户关闭页面或停止接收时,服务端会在请求时限内继续读取上游最终用量;正常完成仍按实际量收费。UI 提示到使用记录确认最终状态,“停止接收”不等于取消上游任务。
|
||||
|
||||
并发请求共享钱包锁,不能重复花掉已预留金额。管理员减少/设置余额不能侵占预留额度。少数上游若报告超出保守预留的实际用量,仍按真实用量计费;余额可能成为负数,之后阻止新调用,补足后恢复。不会把超额使用悄悄截成免费,也不会自动向用户支付工具扣款。
|
||||
|
||||
`Idempotency-Key` 可选,1–128 字符,同一账号范围内防重。重复编号返回 HTTP 409,不重放响应、不再次请求上游;请按请求 ID 查记录。更换 Key 但属于同一账号也不能重复提交同一编号。在线调试每次点击自动生成新编号。即使客户端未提供此头,单次结算也只写一次流水。
|
||||
|
||||
## 流式接口与限制
|
||||
|
||||
- `POST /v1/chat/completions`,`stream:true` 返回标准 SSE,逐段转发,不等待完整文本再展示;启用 `stream_options.include_usage` 以获得上游用量。
|
||||
- 支持文本消息、文本数组、函数工具参数、采样参数、`reasoning_effort` / `thinking` 等白名单参数透传。是否被上游接受取决于具体模型。不会执行模型提出的函数调用。
|
||||
- 单请求 `n=1`;`max_tokens` 或 `max_completion_tokens` 二选一,1–32768,未填写默认 4096。输出上限也用于预留估计。
|
||||
- 请求最大 256 KiB、最多 256 条消息;图片/音频等多模态输入当前拒绝,避免按文本误计。未知参数返回错误,需增加明确适配后开放。
|
||||
- 上游最长 90 秒(包含响应体),单帧 256 KiB,总响应有大小限制。对超过此时限的长推理任务需要后续单独设计。JSON 同步响应上限 4 MiB;流式按解析字符量限制。
|
||||
- 流式记录首 Token 耗时,保留有限长度的文本、思考和工具调用历史;转发内容不会因历史截断被截短。
|
||||
- 上游在开始输出前报错返回非 200;开始输出后发生错误以 SSE `error` 通知。客户端必须检查错误和 `[DONE]`,不能仅凭 HTTP 200 判定成功。
|
||||
- 响应头 `X-Request-Id` 对应使用记录;同步响应额外提供 `X-Moyi-Cost-CNY`。流式最终费用到使用记录查询。
|
||||
|
||||
本次计费只覆盖文本聊天。真实 Tripo/Seedance 异步执行器、支付充值、跨币种自动换汇、Responses/Anthropic 原生协议均不包含在此版本。
|
||||
|
||||
## 升级与验收
|
||||
|
||||
先备份数据库,再解压到新的 release 目录,复制当前 `.env.cloud` 和证书,保留原有 SMTP/SMS、加密密钥、RDS/Tair 配置。启动前运行 `deploy.py check --mode https --network`,按正常发布流程构建并启动。数据库迁移由 Flyway 自动执行;不要手工改旧迁移或重复执行 SQL。
|
||||
|
||||
升级后执行 `deploy.py smoke --mode https --login`。然后在测试账号下给指定分组设定已确认售价、发放少量余额,分别发起同步与流式短请求,核对使用记录与余额流水,最后停用测试 Key。无正式售价时保留空值,系统会阻止真实调用。
|
||||
|
||||
V13 新增钱包预留、账单请求表并允许已发生用量形成欠款。不能只恢复旧镜像来恢复旧计费行为;回退需要维护窗口并评估数据库备份/新增账务的保留方式。
|
||||
|
||||
本机测试覆盖真实 PostgreSQL 迁移、并发预留、缓存价计算、价格快照、重复结算、HTTP 幂等、同步/流式等价、上游错误、缺少 usage、浏览器断开后结算、过期恢复、事务回滚及余额欠款拦截。浏览器界面验收使用本机模拟响应,不连接中转站。
|
||||
@@ -140,7 +140,7 @@ function accountSaved(user: AuthUser) {
|
||||
<div class="sidebar-footer">
|
||||
<div v-if="portal === 'user'" class="balance-card">
|
||||
<span>可用额度</span>
|
||||
<strong>{{ availableCredit === undefined ? '--' : `¥ ${availableCredit.toFixed(2)}` }}</strong>
|
||||
<strong>{{ availableCredit === undefined ? '--' : `¥ ${availableCredit.toLocaleString('zh-CN', { minimumFractionDigits: 2, maximumFractionDigits: 8 })}` }}</strong>
|
||||
<small>人民币 CNY</small>
|
||||
</div>
|
||||
<div v-else class="security-card"><ShieldCheck :size="17" /><span>管理操作均写入审计日志</span></div>
|
||||
|
||||
@@ -35,7 +35,7 @@ const entries = ref<WalletEntry[]>([])
|
||||
const ledgerLoading = ref(false)
|
||||
const filteredUsers = computed(() => users.value.filter(user => `${user.account} ${user.displayName}`.toLowerCase().includes(query.value.toLowerCase())))
|
||||
const totalBalance = computed(() => Object.values(balances.value).reduce((sum, balance) => sum + Number(balance), 0))
|
||||
const money = (value: number | undefined) => `¥ ${Number(value || 0).toFixed(2)}`
|
||||
const money = (value: number | undefined) => `¥ ${Number(value || 0).toLocaleString('zh-CN', { minimumFractionDigits: 2, maximumFractionDigits: 8 })}`
|
||||
const roleName = (value: string) => ({ user: '普通用户', admin: '管理员', super_admin: '超级管理员' })[value] || value
|
||||
async function refresh() {
|
||||
loading.value = true
|
||||
@@ -126,7 +126,7 @@ async function copyText(value: string) {
|
||||
</form>
|
||||
<template #footer><button class="button secondary" :disabled="saving" @click="editor = ''">关闭</button><button v-if="!inviteCode || editor !== 'invite'" class="button primary" type="submit" form="account-editor" :disabled="saving">{{ saving ? '提交中…' : editor === 'credit' ? '确认调整' : '确认' }}</button></template>
|
||||
</UiModal>
|
||||
<SlidePanel v-if="ledgerUser" :title="`${ledgerUser.displayName} · 额度流水`" @close="ledgerUser = undefined"><p>币种:人民币 CNY</p><p v-if="ledgerLoading">加载中…</p><div v-for="entry in entries" :key="entry.id" class="wallet-entry"><strong>{{ Number(entry.amount) >= 0 ? '+' : '−' }} {{ money(Math.abs(Number(entry.amount))) }}</strong><small> · {{ ({ increase: '增加', decrease: '减少', set: '设置余额' })[entry.operation] }}</small><p>{{ entry.reason }}</p><small>{{ entry.actor }} · {{ new Date(entry.createdAt).toLocaleString('zh-CN') }}</small><p>{{ money(entry.balanceBefore) }} → {{ money(entry.balanceAfter) }}</p></div><p v-if="!entries.length && !ledgerLoading">暂无流水</p></SlidePanel>
|
||||
<SlidePanel v-if="ledgerUser" :title="`${ledgerUser.displayName} · 额度流水`" @close="ledgerUser = undefined"><p>币种:人民币 CNY</p><p v-if="ledgerLoading">加载中…</p><div v-for="entry in entries" :key="entry.id" class="wallet-entry"><strong>{{ Number(entry.amount) >= 0 ? '+' : '−' }} {{ money(Math.abs(Number(entry.amount))) }}</strong><small> · {{ ({ increase: '增加', decrease: '减少', set: '设置余额', usage: '模型扣费' })[entry.operation] }}</small><p>{{ entry.reason }}</p><small>{{ entry.actor }} · {{ new Date(entry.createdAt).toLocaleString('zh-CN') }}</small><p>{{ money(entry.balanceBefore) }} → {{ money(entry.balanceAfter) }}</p></div><p v-if="!entries.length && !ledgerLoading">暂无流水</p></SlidePanel>
|
||||
</template>
|
||||
|
||||
<style scoped>
|
||||
|
||||
@@ -27,7 +27,7 @@ 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: '', 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: '', unit: 'CNY / 1M tokens', priority: '100', maxConcurrency: '1', status: 'active' as CatalogModel['status'] })
|
||||
const group = reactive({ logicalModelId: '', providerId: '', sourceLabel: '', upstreamModel: '', input: '', output: '', cachedInput: '', unit: 'CNY / 1M tokens', priority: '100', maxConcurrency: '1', status: 'active' as CatalogModel['status'] })
|
||||
const shownModels = computed(() => models.value.filter(item => `${item.publicName} ${item.modelKey || ''}`.toLowerCase().includes(query.value.toLowerCase())))
|
||||
const shownChannels = computed(() => channels.value.filter(item => `${item.name} ${item.code}`.toLowerCase().includes(query.value.toLowerCase())))
|
||||
const channelName = (id: string) => channels.value.find(item => item.id === id)?.name || id
|
||||
@@ -59,7 +59,7 @@ 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() || '', unit: item?.pricing?.unit || 'CNY / 1M tokens', priority: String(item?.priority ?? 100), maxConcurrency: String(item?.maxConcurrency ?? 1), status: item?.status || 'active' })
|
||||
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', priority: String(item?.priority ?? 100), maxConcurrency: String(item?.maxConcurrency ?? 1), status: item?.status || 'active' })
|
||||
editor.value = 'group'
|
||||
}
|
||||
async function save() {
|
||||
@@ -76,7 +76,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), unit: group.unit } })
|
||||
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 } })
|
||||
}
|
||||
editor.value = ''
|
||||
await refresh()
|
||||
@@ -141,7 +141,9 @@ async function toggle(kind: 'channel' | 'model' | 'group', item: CatalogProvider
|
||||
<label class="form-label" for="group-channel">渠道</label><select id="group-channel" v-model="group.providerId" class="text-input" required><option value="" disabled>请选择渠道</option><option v-for="item in channels" :key="item.id" :value="item.id">{{ item.name }}{{ item.status === 'disabled' ? '(已停用)' : '' }}</option></select>
|
||||
<label class="form-label" for="group-upstream">上游模型标识</label><input id="group-upstream" v-model="group.upstreamModel" class="text-input" required maxlength="160" />
|
||||
<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 />
|
||||
<small>文本聊天自动扣费仅支持 CNY / 1M tokens(元/百万 Token)。输入、输出均需填写;填 0 表示免费,留空则禁止调用。请填写平台对用户的售价。</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 /></label></div>
|
||||
</template>
|
||||
</form>
|
||||
|
||||
@@ -20,7 +20,7 @@ type Column = keyof typeof columnLabels
|
||||
const columns = ref<Column[]>(['key', 'model', 'group', 'endpoint', 'mode', 'tokens', 'cost', 'latency', 'time', 'status'])
|
||||
const show = (name: Column) => columns.value.includes(name)
|
||||
const number = (n: unknown) => n == null ? '—' : Number(n).toLocaleString('zh-CN')
|
||||
const money = (n: unknown) => n == null ? '—' : `¥ ${Number(n).toFixed(6)}`
|
||||
const money = (n: unknown) => n == null ? '—' : `¥ ${Number(n).toLocaleString('zh-CN', { minimumFractionDigits: 2, maximumFractionDigits: 8 })}`
|
||||
const duration = (n: unknown) => n == null ? '—' : `${(Number(n) / 1000).toFixed(2)}s`
|
||||
const time = (value: string) => new Date(value).toLocaleString('zh-CN', { hour12: false })
|
||||
const statusName = (v: string) => ({ succeeded: '成功', failed: '失败', queued: '排队中', running: '执行中', cancelled: '已取消' })[v] || v
|
||||
|
||||
@@ -28,7 +28,7 @@ export interface CatalogModel {
|
||||
inputTypes: string[]
|
||||
outputTypes: string[]
|
||||
capabilities: Record<string, unknown>
|
||||
pricing?: { input?: number; output?: number; unit?: string }
|
||||
pricing?: { input?: number; output?: number; cachedInput?: number; unit?: string }
|
||||
priority?: number
|
||||
maxConcurrency?: number
|
||||
status: 'active' | 'disabled'
|
||||
@@ -51,7 +51,7 @@ export interface WalletEntry {
|
||||
amount: number
|
||||
balanceAfter: number
|
||||
balanceBefore: number
|
||||
operation: 'increase' | 'decrease' | 'set'
|
||||
operation: 'increase' | 'decrease' | 'set' | 'usage'
|
||||
currency: 'CNY'
|
||||
reason: string
|
||||
createdAt: string
|
||||
@@ -92,6 +92,9 @@ export interface PlatformOverview {
|
||||
callsThisMonth: number
|
||||
averageLatencyMs?: number
|
||||
availableCredit?: number
|
||||
balance?: number
|
||||
reservedCredit?: number
|
||||
costThisMonth?: number
|
||||
availabilityPercent?: number
|
||||
asyncTasks: number
|
||||
asyncSuccessRate?: number
|
||||
@@ -287,14 +290,29 @@ class HttpCatalogClient {
|
||||
return response.blob()
|
||||
}
|
||||
|
||||
async chatCompletion(apiKey: string, input: { model: string; prompt: string }) {
|
||||
async chatCompletion(apiKey: string, input: { model: string; prompt: string; maxTokens?: number }) {
|
||||
return this.request<ChatCompletionResult>('/v1/chat/completions', {
|
||||
method: 'POST',
|
||||
headers: { Authorization: `Bearer ${apiKey}`, 'X-Moyi-Source': 'playground' },
|
||||
body: JSON.stringify({ model: input.model, messages: [{ role: 'user', content: input.prompt }] }),
|
||||
headers: { Authorization: `Bearer ${apiKey}`, 'X-Moyi-Source': 'playground', 'Idempotency-Key': crypto.randomUUID() },
|
||||
body: JSON.stringify({ model: input.model, messages: [{ role: 'user', content: input.prompt }], max_tokens: input.maxTokens || 4096 }),
|
||||
})
|
||||
}
|
||||
|
||||
async streamChat(apiKey: string, input: { model: string; prompt: string; maxTokens: number }, onDelta: (content: string, reasoning: string) => void, signal: AbortSignal) {
|
||||
const response = await fetch(`${this.baseUrl}/v1/chat/completions`, {
|
||||
method: 'POST', credentials: 'include', signal,
|
||||
headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${apiKey}`, 'X-Moyi-Source': 'playground', 'Idempotency-Key': crypto.randomUUID() },
|
||||
body: JSON.stringify({ model: input.model, messages: [{ role: 'user', content: input.prompt }], stream: true, max_tokens: input.maxTokens }),
|
||||
})
|
||||
if (!response.ok) {
|
||||
const error = await response.json().catch(() => ({}))
|
||||
throw new Error(error.error?.message || `调用失败 (${response.status})`)
|
||||
}
|
||||
if (!response.body || !response.headers.get('content-type')?.includes('text/event-stream')) throw new Error('服务未返回流式响应')
|
||||
const { readChatStream } = await import('./chat-stream')
|
||||
await readChatStream(response.body, onDelta)
|
||||
}
|
||||
|
||||
async createMediaTask(apiKey: string, input: { model: string; prompt: string; mediaType: '3d' | 'video' }) {
|
||||
const payload = await this.request<{ task: AsyncTaskResult }>(input.mediaType === 'video' ? '/v1/video/tasks' : '/v1/3d/tasks', {
|
||||
method: 'POST',
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
/** Parse incrementally: a UTF-8 character or SSE frame may span several network chunks. */
|
||||
export async function readChatStream(body: ReadableStream<Uint8Array>, onDelta: (content: string, reasoning: string) => void) {
|
||||
const reader = body.getReader()
|
||||
const decoder = new TextDecoder()
|
||||
let buffer = '', data: string[] = [], done = false
|
||||
const line = (value: string) => {
|
||||
if (value.endsWith('\r')) value = value.slice(0, -1)
|
||||
if (value === '') {
|
||||
if (!data.length) return
|
||||
const frame = data.join('\n'); data = []
|
||||
if (frame === '[DONE]') { done = true; return }
|
||||
const event = JSON.parse(frame)
|
||||
if (event.error) throw new Error(event.error.message || '流式调用失败')
|
||||
const delta = event.choices?.[0]?.delta
|
||||
if (delta) onDelta(typeof delta.content === 'string' ? delta.content : '', typeof delta.reasoning_content === 'string' ? delta.reasoning_content : '')
|
||||
} else if (value.startsWith('data:')) data.push(value.slice(5).replace(/^ /, ''))
|
||||
}
|
||||
try {
|
||||
while (!done) {
|
||||
const part = await reader.read()
|
||||
buffer += decoder.decode(part.value, { stream: !part.done })
|
||||
let pos: number
|
||||
while ((pos = buffer.indexOf('\n')) >= 0) {
|
||||
line(buffer.slice(0, pos)); buffer = buffer.slice(pos + 1)
|
||||
if (done) break
|
||||
}
|
||||
if (part.done) break
|
||||
if (buffer.length > 262144 || data.join('\n').length > 262144) throw new Error('流式响应超过限制')
|
||||
}
|
||||
if (!done) throw new Error('连接提前结束,请到使用记录中确认调用与扣费状态')
|
||||
} finally {
|
||||
await reader.cancel().catch(() => {})
|
||||
reader.releaseLock()
|
||||
}
|
||||
}
|
||||
@@ -33,7 +33,7 @@ export interface ModelItem {
|
||||
icon: Component
|
||||
modelKey?: string
|
||||
sourceLabel?: string
|
||||
pricing?: { input?: number; output?: number; unit?: string }
|
||||
pricing?: { input?: number; output?: number; cachedInput?: number; unit?: string }
|
||||
priority?: number
|
||||
maxConcurrency?: number
|
||||
}
|
||||
|
||||
@@ -69,7 +69,11 @@ function selectPlaygroundModel(id: string) {
|
||||
}
|
||||
const prompt = ref('')
|
||||
const running = ref(false)
|
||||
const maxOutputTokens = ref(4096)
|
||||
const reasoningOutput = ref('')
|
||||
let streamAbort: AbortController | undefined
|
||||
const response = ref('')
|
||||
const responseError = ref('')
|
||||
const keys = ref<ApiKeyItem[]>([])
|
||||
const tasks = ref<TaskItem[]>([])
|
||||
const historyRevision = ref(0)
|
||||
@@ -139,6 +143,7 @@ onMounted(async () => {
|
||||
})
|
||||
|
||||
onUnmounted(() => {
|
||||
streamAbort?.abort()
|
||||
taskPollers.forEach((timer) => window.clearTimeout(timer))
|
||||
taskPollers.clear()
|
||||
copyTimers.forEach((timer) => window.clearTimeout(timer))
|
||||
@@ -221,6 +226,7 @@ async function runModel() {
|
||||
if (!activeModel.value.id) return emit('notify', '当前没有可调用的模型,请先检查模型目录')
|
||||
if (!prompt.value.trim()) return emit('notify', '请先输入调用内容')
|
||||
if (running.value) return
|
||||
if (callMode.value !== '异步' && (!Number.isInteger(maxOutputTokens.value) || maxOutputTokens.value < 1 || maxOutputTokens.value > 32768)) return emit('notify', '最大输出 Token 须为 1–32768 的整数')
|
||||
if (!catalogConnected.value) return emit('notify', 'Control Plane 未连接,无法提交真实调用')
|
||||
if (!remoteApiKey.value) {
|
||||
emit('notify', '请先创建一个临时 API Key,再进行真实接口调用')
|
||||
@@ -230,6 +236,9 @@ async function runModel() {
|
||||
}
|
||||
running.value = true
|
||||
response.value = ''
|
||||
responseError.value = ''
|
||||
reasoningOutput.value = ''
|
||||
const selectedMode = callMode.value
|
||||
try {
|
||||
if (callMode.value === '异步') {
|
||||
const task = await catalogClient.createMediaTask(remoteApiKey.value, { model: activeModel.value.id, prompt: prompt.value, mediaType: activeModel.value.type === '视频' ? 'video' : '3d' })
|
||||
@@ -238,17 +247,22 @@ async function runModel() {
|
||||
activeTaskId.value = task.id
|
||||
response.value = `任务 ${task.id} 已进入队列,可前往任务中心查看进度。`
|
||||
scheduleTaskPoll(task.id, remoteApiKey.value)
|
||||
} else if (selectedMode === '流式') {
|
||||
streamAbort = new AbortController()
|
||||
await catalogClient.streamChat(remoteApiKey.value, { model: activeModel.value.id, prompt: prompt.value, maxTokens: maxOutputTokens.value }, (text, reasoning) => { response.value += text; reasoningOutput.value += reasoning }, streamAbort.signal)
|
||||
} else {
|
||||
const result = await catalogClient.chatCompletion(remoteApiKey.value, { model: activeModel.value.id, prompt: prompt.value })
|
||||
const result = await catalogClient.chatCompletion(remoteApiKey.value, { model: activeModel.value.id, prompt: prompt.value, maxTokens: maxOutputTokens.value })
|
||||
response.value = result.choices[0]?.message.content || '模型未返回内容'
|
||||
}
|
||||
emit('notify', callMode.value === '异步' ? '异步任务提交成功' : '模型调用完成')
|
||||
await refreshOverview()
|
||||
} catch (error) {
|
||||
emit('notify', error instanceof Error ? error.message : '模型调用失败')
|
||||
responseError.value = error instanceof Error && error.name === 'AbortError' ? '已停止接收,请在使用记录中确认最终状态和费用' : error instanceof Error ? error.message : '模型调用失败'
|
||||
emit('notify', responseError.value)
|
||||
} finally {
|
||||
running.value = false
|
||||
streamAbort = undefined
|
||||
historyRevision.value++
|
||||
await refreshOverview()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -482,7 +496,7 @@ async function createProject() {
|
||||
<section class="metric-grid">
|
||||
<article class="metric-card"><span class="icon-tile violet"><Activity :size="18" /></span><div><small>今日调用</small><strong>{{ overviewLoaded ? overview.callsToday.toLocaleString() : '--' }}</strong><em>来自网关调用记录</em></div></article>
|
||||
<article class="metric-card"><span class="icon-tile cyan"><Gauge :size="18" /></span><div><small>平均响应</small><strong>{{ overview.averageLatencyMs === undefined ? '--' : `${overview.averageLatencyMs}ms` }}</strong><em>{{ overview.averageLatencyMs === undefined ? '暂无延迟样本' : '按真实调用计算' }}</em></div></article>
|
||||
<article class="metric-card"><span class="icon-tile orange"><CircleDollarSign :size="18" /></span><div><small>可用额度</small><strong>{{ overview.availableCredit === undefined ? '--' : `¥ ${overview.availableCredit.toFixed(2)}` }}</strong><em>{{ overview.availableCredit === undefined ? '额度账本尚未接入' : '读取额度账本' }}</em></div></article>
|
||||
<article class="metric-card"><span class="icon-tile orange"><CircleDollarSign :size="18" /></span><div><small>可用额度</small><strong>{{ overview.availableCredit === undefined ? '--' : `¥ ${overview.availableCredit.toLocaleString('zh-CN', { minimumFractionDigits: 2, maximumFractionDigits: 8 })}` }}</strong><em>{{ overview.availableCredit === undefined ? '额度账本尚未接入' : '读取额度账本' }}</em></div></article>
|
||||
<article class="metric-card"><span class="icon-tile green"><CheckCircle2 :size="18" /></span><div><small>平台可用性</small><strong>{{ overview.availabilityPercent === undefined ? '--' : `${overview.availabilityPercent.toFixed(2)}%` }}</strong><em>{{ overview.availabilityPercent === undefined ? '监控数据尚未接入' : '来自可用性监控' }}</em></div></article>
|
||||
</section>
|
||||
|
||||
@@ -516,14 +530,18 @@ async function createProject() {
|
||||
<label>分组<select class="text-input" aria-label="调试分组" :value="activeModel.id" :disabled="running || !playgroundGroups.length" @change="selectPlaygroundGroup(($event.target as HTMLSelectElement).value)"><option v-for="group in playgroundGroups" :key="group.id" :value="group.id">{{ group.sourceLabel || '默认分组' }} · {{ group.provider }}</option></select></label>
|
||||
</div>
|
||||
<label class="form-label">调用模式</label>
|
||||
<div class="segment"><button v-for="mode in ['同步','流式','异步']" :key="mode" :disabled="running || mode !== activeModel.mode || mode === '流式'" :class="{ active: callMode === mode }" @click="callMode = mode as CallMode">{{ mode }}</button></div>
|
||||
<div class="segment"><button v-for="mode in ['同步','流式','异步']" :key="mode" :disabled="running || (activeModel.mode === '异步' ? mode !== '异步' : mode === '异步')" :class="{ active: callMode === mode }" @click="callMode = mode as CallMode">{{ mode }}</button></div>
|
||||
<label v-if="callMode !== '异步'" class="form-label token-limit">最大输出 Token<input v-model.number="maxOutputTokens" class="text-input" type="number" min="1" max="32768" step="1" :disabled="running" /></label>
|
||||
<small v-if="callMode !== '异步'">按分组人民币售价和实际 Token 用量扣费;调用前预留额度,结束后释放差额。</small>
|
||||
<label class="form-label prompt-label"><span>输入内容</span><small>{{ prompt.length }}/4000</small></label>
|
||||
<textarea v-model="prompt" maxlength="4000" :placeholder="callMode === '异步' ? activeModel.type === '视频' ? '例如:电影感产品展示视频,柔和运镜与自然光影' : '例如:一个低面数的木质机械鸟,游戏资产风格' : '描述你希望模型完成的任务'"></textarea>
|
||||
<div class="request-footer"><span><ShieldCheck :size="14" />{{ catalogConnected ? '通过 MOYI Key 调用真实网关' : '连接服务后才能调用' }}</span><button class="button primary" :disabled="running || !catalogConnected || !activeModel.id" @click="runModel"><Play v-if="!running" :size="16" /><span v-else class="spinner"></span>{{ running ? '提交中…' : callMode === '异步' ? activeModel.type === '视频' ? '生成视频' : '生成 3D' : '开始调用' }}</button></div>
|
||||
</article>
|
||||
<article class="panel response-panel">
|
||||
<header class="panel-header"><div><span>RESPONSE</span><h3>{{ callMode === '异步' ? '任务状态' : '模型输出' }}</h3></div><button class="icon-button" :class="{ copied: copySucceeded('response') }" :disabled="!response" :title="copySucceeded('response') ? '已复制' : '复制响应'" @click="copy(response, '响应内容', 'response')"><Check v-if="copySucceeded('response')" :size="16" /><Copy v-else :size="16" /></button></header>
|
||||
<div v-if="running" class="response-state"><span class="loading-orbit"><i></i></span><strong>{{ callMode === '异步' ? activeModel.type === '视频' ? '正在创建视频任务' : '正在创建 3D 任务' : '模型正在生成响应' }}</strong><p>请求通过模亦灵枢统一网关发送</p></div>
|
||||
<button v-if="running && callMode === '流式'" class="button secondary" @click="streamAbort?.abort()">停止接收</button>
|
||||
<details v-if="reasoningOutput"><summary>思考过程</summary><pre class="response-content">{{ reasoningOutput }}</pre></details>
|
||||
<div v-if="running && !response" class="response-state"><span class="loading-orbit"><i></i></span><strong>{{ callMode === '异步' ? activeModel.type === '视频' ? '正在创建视频任务' : '正在创建 3D 任务' : '模型正在生成响应' }}</strong><p>请求通过模亦灵枢统一网关发送</p></div>
|
||||
<div v-else-if="callMode === '异步' && activeTask" class="live-task-result">
|
||||
<div class="task-preview" :class="{ empty: !activeTask.previewUrl && !activeTask.videoUrl }"><video v-if="activeTask.videoUrl" :src="activeTask.videoUrl" controls></video><img v-else-if="activeTask.previewUrl" :src="activeTask.previewUrl" alt="异步任务预览" /><span v-else><Box :size="34" /><small>{{ activeTask.status === '已完成' ? '结果已生成' : '生成预览中' }}</small></span><em>{{ activeTask.provider === 'tripo' ? 'TRIPO' : activeTask.provider === 'seedance' ? 'SEEDANCE' : 'MOYI GATEWAY' }}</em></div>
|
||||
<div class="live-task-head"><span class="status-chip" :class="activeTask.status"><i></i>{{ activeTask.status }}</span><code>{{ activeTask.id }}</code></div>
|
||||
@@ -532,7 +550,7 @@ async function createProject() {
|
||||
<dl v-if="activeTask.creditsConsumed !== undefined" class="task-cost"><dt>上游任务消耗</dt><dd>{{ activeTask.creditsConsumed }} credits</dd></dl>
|
||||
<div class="live-task-actions"><button class="button secondary" @click="emit('navigate', 'tasks')"><FileClock :size="16" />任务中心</button><button v-if="activeTask.modelUrl" class="button primary" @click="openTaskAsset(activeTask.modelUrl)"><Download :size="16" />下载 GLB</button><button v-if="activeTask.videoUrl" class="button primary" @click="openTaskAsset(activeTask.videoUrl)"><ExternalLink :size="16" />查看视频</button><button v-if="activeTask.previewUrl" class="icon-button" title="打开预览" @click="openTaskAsset(activeTask.previewUrl)"><ExternalLink :size="16" /></button></div>
|
||||
</div>
|
||||
<div v-else-if="response" class="response-result"><div><span class="status-chip success"><i></i>200 OK</span><small>MOYI unified response</small></div><p>{{ response }}</p></div>
|
||||
<div v-else-if="response || responseError" class="response-result"><div><span class="status-chip" :class="responseError ? 'failed' : running ? 'muted' : 'success'"><i></i>{{ responseError ? '调用未正常完成' : running ? '正在输出' : '调用完成' }}</span><small>MOYI unified response</small></div><p>{{ response }}</p><p v-if="responseError" role="alert">{{ responseError }}</p></div>
|
||||
<div v-else class="response-state empty"><span class="response-icon"><Sparkles :size="28" /></span><strong>等待模型响应</strong><p>配置请求并点击“开始调用”</p></div>
|
||||
</article>
|
||||
</section>
|
||||
@@ -552,9 +570,11 @@ async function createProject() {
|
||||
|
||||
<template v-else-if="view === 'usage'">
|
||||
<section class="page-heading"><div><span>USAGE & CREDITS</span><h1>用量与额度</h1><p>查看调用消耗、额度余额和最近流水。</p></div><button class="button disabled" disabled><WalletCards :size="16" />充值暂未开放</button></section>
|
||||
<section class="credit-hero"><div><span>当前可用额度</span><strong>{{ overview.availableCredit === undefined ? '--' : `¥ ${overview.availableCredit.toFixed(2)}` }}</strong><p>{{ overview.availableCredit === undefined ? '额度账本尚未接入,当前不会展示预估金额。' : '额度由平台账本实时计算。' }}</p></div><div class="credit-circle"><span>{{ overview.availableCredit === undefined ? '--' : '已同步' }}</span><small>{{ overview.availableCredit === undefined ? '等待账本' : '账本状态' }}</small></div></section>
|
||||
<section class="metric-grid three"><article class="metric-card"><span class="icon-tile violet"><Activity :size="18" /></span><div><small>本月调用</small><strong>{{ overviewLoaded ? overview.callsThisMonth.toLocaleString() : '--' }}</strong><em>网关真实记录</em></div></article><article class="metric-card"><span class="icon-tile cyan"><BarChart3 :size="18" /></span><div><small>本月消耗</small><strong>--</strong><em>计费规则尚未接入</em></div></article><article class="metric-card"><span class="icon-tile orange"><Box :size="18" /></span><div><small>异步任务</small><strong>{{ overviewLoaded ? overview.asyncTasks : '--' }}</strong><em>{{ overview.asyncSuccessRate === undefined ? '暂无完成样本' : `成功率 ${overview.asyncSuccessRate.toFixed(1)}%` }}</em></div></article></section>
|
||||
<section class="two-column wide-left"><article class="panel chart-panel"><header class="panel-header"><div><span>USAGE TREND</span><h3>过去 7 天调用</h3></div><span class="select-button static">按调用次数</span></header><div v-if="usageSeries.length" class="bar-chart large"><div v-for="item in usageSeries" :key="item.date"><i :style="{ height: `${item.height}%` }" :title="`${item.date} · ${item.count} 次`"></i><span>{{ item.label }}</span></div></div><div v-else class="empty-state compact"><BarChart3 :size="22" /><strong>暂无调用趋势</strong><span>完成真实调用后会在这里形成统计</span></div></article><article class="panel ledger"><header class="panel-header"><div><span>CREDIT LEDGER</span><h3>最近额度流水</h3></div><Download :size="17" /></header><div v-if="overview.ledger.length"><p v-for="item in overview.ledger" :key="item.id"><span class="activity-dot violet"><CircleDollarSign :size="14" /></span><span><strong>{{ item.title }}</strong><small>{{ item.createdAt }}</small></span><b :class="item.amount >= 0 ? 'positive' : ''">{{ item.amount >= 0 ? '+' : '' }} ¥{{ item.amount.toFixed(2) }}</b></p></div><div v-else class="empty-state compact"><CircleDollarSign :size="22" /><strong>暂无额度流水</strong><span>管理员发放或计费完成后会显示记录</span></div></article></section>
|
||||
<section class="credit-hero"><div><span>当前可用额度</span><strong>{{ overview.availableCredit === undefined ? '--' : `¥ ${overview.availableCredit.toLocaleString('zh-CN', { minimumFractionDigits: 2, maximumFractionDigits: 8 })}` }}</strong><p>{{ overview.availableCredit === undefined ? '额度账本尚未接入,当前不会展示预估金额。' : '额度由平台账本实时计算。' }}</p></div><div class="credit-circle"><span>{{ overview.availableCredit === undefined ? '--' : '已同步' }}</span><small>{{ overview.availableCredit === undefined ? '等待账本' : '账本状态' }}</small></div></section>
|
||||
<p v-if="overview.reservedCredit" class="billing-note">进行中的调用已预留 ¥{{ overview.reservedCredit.toLocaleString('zh-CN', { maximumFractionDigits: 8 }) }};结束后按实际 Token 用量结算,剩余预留自动释放。</p>
|
||||
<p v-if="(overview.balance ?? 0) < 0" class="billing-note" role="alert">账户存在待补余额,请补足后继续调用。可在下方流水查看实际扣费。</p>
|
||||
<section class="metric-grid three"><article class="metric-card"><span class="icon-tile violet"><Activity :size="18" /></span><div><small>本月调用</small><strong>{{ overviewLoaded ? overview.callsThisMonth.toLocaleString() : '--' }}</strong><em>网关真实记录</em></div></article><article class="metric-card"><span class="icon-tile cyan"><BarChart3 :size="18" /></span><div><small>本月消耗</small><strong>{{ overview.costThisMonth === undefined ? '--' : `¥ ${overview.costThisMonth.toLocaleString('zh-CN', { minimumFractionDigits: 2, maximumFractionDigits: 8 })}` }}</strong><em>已入账的模型调用费用</em></div></article><article class="metric-card"><span class="icon-tile orange"><Box :size="18" /></span><div><small>异步任务</small><strong>{{ overviewLoaded ? overview.asyncTasks : '--' }}</strong><em>{{ overview.asyncSuccessRate === undefined ? '暂无完成样本' : `成功率 ${overview.asyncSuccessRate.toFixed(1)}%` }}</em></div></article></section>
|
||||
<section class="two-column wide-left"><article class="panel chart-panel"><header class="panel-header"><div><span>USAGE TREND</span><h3>过去 7 天调用</h3></div><span class="select-button static">按调用次数</span></header><div v-if="usageSeries.length" class="bar-chart large"><div v-for="item in usageSeries" :key="item.date"><i :style="{ height: `${item.height}%` }" :title="`${item.date} · ${item.count} 次`"></i><span>{{ item.label }}</span></div></div><div v-else class="empty-state compact"><BarChart3 :size="22" /><strong>暂无调用趋势</strong><span>完成真实调用后会在这里形成统计</span></div></article><article class="panel ledger"><header class="panel-header"><div><span>CREDIT LEDGER</span><h3>最近额度流水</h3></div><Download :size="17" /></header><div v-if="overview.ledger.length"><p v-for="item in overview.ledger" :key="item.id"><span class="activity-dot violet"><CircleDollarSign :size="14" /></span><span><strong>{{ item.title }}</strong><small>{{ item.createdAt }}</small></span><b :class="item.amount >= 0 ? 'positive' : ''">{{ item.amount >= 0 ? '+' : '' }} ¥{{ item.amount.toLocaleString('zh-CN', { minimumFractionDigits: 2, maximumFractionDigits: 8 }) }}</b></p></div><div v-else class="empty-state compact"><CircleDollarSign :size="22" /><strong>暂无额度流水</strong><span>管理员发放或计费完成后会显示记录</span></div></article></section>
|
||||
</template>
|
||||
|
||||
<template v-else>
|
||||
@@ -584,6 +604,10 @@ MOYI_MODEL=<model-id></code></pre></div></section><section><b>02</b><div><
|
||||
|
||||
<style scoped>
|
||||
.request-panel, .response-panel { min-width: 0; }
|
||||
.token-limit { display: grid; gap: 8px; }
|
||||
.response-result > p, .response-content { white-space: pre-wrap; overflow-wrap: anywhere; max-width: 100%; }
|
||||
.response-result .status-chip.failed { color: #b42318; background: #fee4e2; }
|
||||
.response-result p[role="alert"] { color: #b42318; }
|
||||
.playground-selectors { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px; margin-bottom: 16px; }
|
||||
.playground-selectors label { display: grid; gap: 8px; font-size: 13px; min-width: 0; }
|
||||
.playground-selectors select { width: 100%; min-width: 0; }
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
import { test } from 'node:test'
|
||||
import assert from 'node:assert/strict'
|
||||
import { readChatStream } from '../src/services/chat-stream.ts'
|
||||
|
||||
function stream(text, everyByte = false) {
|
||||
const bytes = new TextEncoder().encode(text)
|
||||
return new ReadableStream({ start(controller) {
|
||||
if (everyByte) for (const byte of bytes) controller.enqueue(Uint8Array.of(byte))
|
||||
else controller.enqueue(bytes)
|
||||
controller.close()
|
||||
} })
|
||||
}
|
||||
test('UTF-8 split at every byte and CRLF frames preserve reasoning and content', async () => {
|
||||
let text = '', reasoning = ''
|
||||
await readChatStream(stream('data: {"choices":[{"delta":{"reasoning_content":"思考"}}]}\r\n\r\ndata: {"choices":[{"delta":{"content":"你好🙂"}}]}\n\ndata: [DONE]\n\n', true), (a,b) => { text += a; reasoning += b })
|
||||
assert.equal(text, '你好🙂'); assert.equal(reasoning, '思考')
|
||||
})
|
||||
test('an SSE error after partial output is surfaced', async () => {
|
||||
let text = ''
|
||||
await assert.rejects(readChatStream(stream('data: {"choices":[{"delta":{"content":"partial"}}]}\n\ndata: {"error":{"message":"用量缺失,未扣费"}}\n\ndata: [DONE]\n\n'), a => text += a), /未扣费/)
|
||||
assert.equal(text, 'partial')
|
||||
})
|
||||
test('EOF alone is never treated as a completed request', async () => {
|
||||
await assert.rejects(readChatStream(stream('data: {"choices":[]}\n\n'), () => {}), /提前结束/)
|
||||
})
|
||||
@@ -19,6 +19,8 @@ try {
|
||||
$env:VITE_AUTH_MODE = 'remote'
|
||||
& node node_modules/vue-tsc/bin/vue-tsc.js --noEmit
|
||||
if ($LASTEXITCODE -ne 0) { throw 'Vue type check failed.' }
|
||||
& node --experimental-strip-types --test tests/chat-stream.test.mjs
|
||||
if ($LASTEXITCODE -ne 0) { throw 'Frontend stream verification failed.' }
|
||||
& node build-release.mjs
|
||||
if ($LASTEXITCODE -ne 0) { throw 'Vue build failed.' }
|
||||
} finally { Pop-Location }
|
||||
@@ -28,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 docs/11-server-test-deployment.md docs/13-model-groups-registration-credit.md docs/14-lifecycle-usage-credit-adjustments.md)
|
||||
$tracked = @(git ls-files infra/moyi 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)
|
||||
foreach ($relative in $tracked) {
|
||||
if ($relative -match '(^|/)(\.env\.cloud|\.deploy-mode|certs)(/|$)') { throw 'Sensitive path in release allowlist.' }
|
||||
$destination = Join-Path $staging $relative
|
||||
@@ -58,7 +60,7 @@ $manifest = [ordered]@{
|
||||
javaRelease = 21
|
||||
database = 'PostgreSQL 15'
|
||||
redis = 'Tair standard'
|
||||
checks = @('maven-clean-verify', 'vue-tsc', 'vite-build')
|
||||
checks = @('maven-clean-verify', 'vue-tsc', 'frontend-stream-tests', 'vite-build')
|
||||
sha256 = $hashes
|
||||
}
|
||||
[IO.File]::WriteAllText((Join-Path $staging 'release-manifest.json'), ($manifest | ConvertTo-Json -Depth 5), [Text.UTF8Encoding]::new($false))
|
||||
|
||||
Reference in new issue
Block a user