fix: classify upstream failures and prevent empty billed responses
This commit is contained in:
1 parent
b455d8d79f
commit
9acc69d4c7
28 files changed
+1449
-131
No files matched your search
+48
-3
@@ -20,7 +20,10 @@ final class ChatStreamCollector {
|
||||
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"));
|
||||
appendContent(delta.get("content"));
|
||||
appendReasoning(delta.get("reasoning_content"));
|
||||
appendReasoning(delta.get("reasoning"));
|
||||
appendReasoning(delta.get("thinking"));
|
||||
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();
|
||||
@@ -35,14 +38,55 @@ final class ChatStreamCollector {
|
||||
}
|
||||
}
|
||||
}
|
||||
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);
|
||||
if(firstToken==null && (hasText(delta.get("content")) || hasText(delta.get("reasoning_content"))
|
||||
|| hasText(delta.get("reasoning")) || hasText(delta.get("thinking"))
|
||||
|| (delta.get("tool_calls") instanceof List<?> calls && !calls.isEmpty())))
|
||||
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())); }
|
||||
private void appendContent(Object value) {
|
||||
if(value instanceof String) { append(content,value); return; }
|
||||
if(value instanceof List<?> parts) for(Object part:parts) {
|
||||
if(part instanceof Map<?,?> map) {
|
||||
Object type=map.get("type");
|
||||
if(isReasoningType(type)) {
|
||||
appendReasoning(firstString(map,"text","thinking","content","reasoning"));
|
||||
} else {
|
||||
append(content,firstString(map,"text","content"));
|
||||
}
|
||||
} else append(content,part);
|
||||
}
|
||||
}
|
||||
private void appendReasoning(Object value) { append(reasoning,value); }
|
||||
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()));
|
||||
}
|
||||
private boolean hasText(Object value) {
|
||||
if(value instanceof String text) return !text.isBlank();
|
||||
if(value instanceof List<?> parts) for(Object part:parts) {
|
||||
if(part instanceof Map<?,?> map) {
|
||||
if(isReasoningType(map.get("type"))) {
|
||||
if(hasText(firstString(map,"text","thinking","content","reasoning"))) return true;
|
||||
} else if(hasText(firstString(map,"text","content"))) return true;
|
||||
} else if(hasText(part)) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
private boolean isReasoningType(Object type) {
|
||||
if(type==null) return false;
|
||||
String normalized=String.valueOf(type).toLowerCase(Locale.ROOT);
|
||||
return normalized.equals("thinking") || normalized.equals("reasoning") || normalized.equals("reasoning_content");
|
||||
}
|
||||
private String firstString(Map<?,?> map,String... names) {
|
||||
for(String name:names) if(map.get(name) instanceof String text) return text;
|
||||
return "";
|
||||
}
|
||||
Integer firstTokenMs() { return firstToken; }
|
||||
Map<String,Object> result() {
|
||||
if(finish==null) throw new ApiException("UPSTREAM_STREAM_INCOMPLETE","上游未完成输出,本次未扣费",502);
|
||||
if(content.toString().isBlank() && reasoning.toString().isBlank() && !hasToolOutput())
|
||||
throw new ApiException("UPSTREAM_RESPONSE_INVALID","上游未返回完整的聊天结果,本次未扣费",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()));
|
||||
@@ -50,4 +94,5 @@ final class ChatStreamCollector {
|
||||
result.put("usage",usage); result.put("choices",List.of(Map.of("index",0,"message",message,"finish_reason",finish)));
|
||||
return result;
|
||||
}
|
||||
private boolean hasToolOutput() { return tools.values().stream().anyMatch(tool -> !tool.isEmpty()); }
|
||||
}
|
||||
+36
-1
@@ -162,9 +162,44 @@ public class GatewayController {
|
||||
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)
|
||||
|| !(choices.getFirst() instanceof Map<?,?> choice) || choice.get("finish_reason")==null || !hasAssistantOutput(choice.get("message")))
|
||||
throw new ApiException("UPSTREAM_RESPONSE_INVALID","上游未返回完整的聊天结果,本次未扣费",502);
|
||||
}
|
||||
|
||||
/**
|
||||
* Reasoning models may finish with an empty final content field while
|
||||
* returning reasoning_content. That is a valid, billable upstream result
|
||||
* and the UI renders the reasoning separately. A response with neither
|
||||
* content, reasoning, nor tool calls is a genuine empty response and must
|
||||
* not be settled as a successful paid call.
|
||||
*/
|
||||
private boolean hasAssistantOutput(Object rawMessage) {
|
||||
if (!(rawMessage instanceof Map<?,?> message)) return false;
|
||||
if (hasAssistantValue(message.get("content"))) return true;
|
||||
for (String field : List.of("reasoning_content", "reasoning", "thinking")) {
|
||||
if (hasText(message.get(field))) return true;
|
||||
}
|
||||
if (message.get("tool_calls") instanceof List<?> calls) {
|
||||
return calls.stream().anyMatch(call -> call instanceof Map<?,?> item && !item.isEmpty());
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private boolean hasAssistantValue(Object value) {
|
||||
if (hasText(value)) return true;
|
||||
if (!(value instanceof List<?> parts)) return false;
|
||||
return parts.stream().anyMatch(part -> {
|
||||
if (!(part instanceof Map<?,?> map)) return hasText(part);
|
||||
for (String field : List.of("text", "content", "thinking", "reasoning", "reasoning_content")) {
|
||||
if (hasText(map.get(field))) return true;
|
||||
}
|
||||
return false;
|
||||
});
|
||||
}
|
||||
|
||||
private boolean hasText(Object value) {
|
||||
return value instanceof String text && !text.isBlank();
|
||||
}
|
||||
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);
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package com.moyi.controlplane.gateway;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
@@ -21,6 +23,15 @@ public class GatewayGuard {
|
||||
private static final DefaultRedisScript<Long> RATE = script("redis/rate-limit.lua");
|
||||
private static final DefaultRedisScript<Long> ACQUIRE = script("redis/acquire-slot.lua");
|
||||
private static final DefaultRedisScript<Long> ACQUIRE_PAIR = script("redis/acquire-pair.lua");
|
||||
private static final DefaultRedisScript<List> CAPACITY = new DefaultRedisScript<>("""
|
||||
local time = redis.call('TIME')
|
||||
local now = tonumber(time[1]) * 1000 + math.floor(tonumber(time[2]) / 1000)
|
||||
local counts = {}
|
||||
for i, key in ipairs(KEYS) do
|
||||
counts[i] = redis.call('ZCOUNT', key, '(' .. now, '+inf')
|
||||
end
|
||||
return counts
|
||||
""", List.class);
|
||||
private static final DefaultRedisScript<Long> RELEASE = new DefaultRedisScript<>("return redis.call('ZREM', KEYS[1], ARGV[1])", Long.class);
|
||||
private final StringRedisTemplate redis;
|
||||
private final int requestsPerMinute;
|
||||
@@ -118,29 +129,64 @@ public class GatewayGuard {
|
||||
|
||||
public long queueWaitMs() { return queueWaitMs; }
|
||||
|
||||
public int defaultModelConcurrency() { return defaultModelConcurrency; }
|
||||
public int defaultKeyConcurrency() { return defaultKeyConcurrency; }
|
||||
|
||||
public record Capacity(long running, long queued) {}
|
||||
public record CapacitySnapshot(boolean available, Map<String, Capacity> groups) {}
|
||||
|
||||
/** Reads live leases rather than historical usage rows. Expired workers do not count as running. */
|
||||
public CapacitySnapshot capacitySnapshot(List<String> modelIds) {
|
||||
if (modelIds.isEmpty()) return new CapacitySnapshot(true, Map.of());
|
||||
List<String> buckets = new ArrayList<>();
|
||||
for (String id : modelIds) {
|
||||
buckets.add(redisPrefix + "gateway:slots:model:" + id);
|
||||
buckets.add(redisPrefix + "gateway:waiting:model:" + id);
|
||||
}
|
||||
try {
|
||||
List<?> counts = redis.execute(CAPACITY, buckets);
|
||||
if (counts == null || counts.size() != buckets.size()) return new CapacitySnapshot(false, Map.of());
|
||||
Map<String, Capacity> groups = new LinkedHashMap<>();
|
||||
for (int index = 0; index < modelIds.size(); index++) {
|
||||
groups.put(modelIds.get(index), new Capacity(((Number) counts.get(index * 2)).longValue(), ((Number) counts.get(index * 2 + 1)).longValue()));
|
||||
}
|
||||
return new CapacitySnapshot(true, groups);
|
||||
} catch (DataAccessException exception) {
|
||||
log.warn("Gateway capacity snapshot unavailable ({})", exception.getClass().getSimpleName());
|
||||
return new CapacitySnapshot(false, Map.of());
|
||||
}
|
||||
}
|
||||
|
||||
/** Atomically reserves the model and API-Key slots, so queued requests do not hold one scarce slot while waiting for the other. */
|
||||
public Admission acquire(Map<String, Object> model, UUID keyId, long requestedWaitMs) {
|
||||
String modelBucket = redisPrefix + "gateway:slots:model:" + model.get("id");
|
||||
String keyBucket = redisPrefix + "gateway:slots:key:" + keyId;
|
||||
String waitingBucket = redisPrefix + "gateway:waiting:model:" + model.get("id");
|
||||
String token = UUID.randomUUID().toString();
|
||||
long waitMs = Math.max(0, Math.min(queueWaitMs, requestedWaitMs > 0 ? requestedWaitMs : queueWaitMs));
|
||||
long deadline = System.nanoTime() + java.util.concurrent.TimeUnit.MILLISECONDS.toNanos(waitMs);
|
||||
long lastBusyCode = 0;
|
||||
while (true) {
|
||||
long acquired = executePair(modelBucket, keyBucket, Integer.toString(modelConcurrency(model)), Integer.toString(defaultKeyConcurrency), Long.toString(SLOT_TTL_MS), token);
|
||||
if (acquired == 1) return new Admission(modelBucket, keyBucket, token);
|
||||
lastBusyCode = acquired;
|
||||
if (waitMs == 0 || System.nanoTime() >= deadline) {
|
||||
boolean keyBusy = lastBusyCode == 2;
|
||||
throw new GatewayLimitException(keyBusy ? "KEY_BUSY" : "MODEL_BUSY", keyBusy ? "当前 API Key 并发已满,已排队等待但仍未获得资源" : "当前模型分组并发已满,已排队等待但仍未获得资源", 429, Math.max(1, (int) Math.ceil(queuePollMs / 1000.0)));
|
||||
}
|
||||
try {
|
||||
long remaining = java.util.concurrent.TimeUnit.NANOSECONDS.toMillis(deadline - System.nanoTime());
|
||||
Thread.sleep(Math.max(1, Math.min(queuePollMs, remaining)));
|
||||
} catch (InterruptedException exception) {
|
||||
Thread.currentThread().interrupt();
|
||||
throw new GatewayLimitException("MODEL_BUSY", "排队被中断,请稍后重试", 429, 1);
|
||||
boolean admitted = false;
|
||||
try {
|
||||
while (true) {
|
||||
long remaining = Math.max(0, java.util.concurrent.TimeUnit.NANOSECONDS.toMillis(deadline - System.nanoTime()));
|
||||
long acquired = executePair(modelBucket, keyBucket, waitingBucket, Integer.toString(modelConcurrency(model)), Integer.toString(defaultKeyConcurrency), Long.toString(SLOT_TTL_MS), token, Long.toString(remaining), Long.toString(Math.max(SLOT_TTL_MS, waitMs + 1000)));
|
||||
if (acquired == 1) {
|
||||
admitted = true; // The acquisition script also removed the waiting entry.
|
||||
return new Admission(modelBucket, keyBucket, token);
|
||||
}
|
||||
if (waitMs == 0 || System.nanoTime() >= deadline) {
|
||||
boolean keyBusy = acquired == 2;
|
||||
throw new GatewayLimitException(keyBusy ? "KEY_BUSY" : "MODEL_BUSY", keyBusy ? "当前 API Key 并发已满,已排队等待但仍未获得资源" : "当前模型分组并发已满,已排队等待但仍未获得资源", 429, Math.max(1, (int) Math.ceil(queuePollMs / 1000.0)));
|
||||
}
|
||||
try {
|
||||
Thread.sleep(Math.max(1, Math.min(queuePollMs, remaining)));
|
||||
} catch (InterruptedException exception) {
|
||||
Thread.currentThread().interrupt();
|
||||
throw new GatewayLimitException("MODEL_BUSY", "排队被中断,请稍后重试", 429, 1);
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
if (!admitted && waitMs > 0) releaseQuietly(waitingBucket, token);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -174,9 +220,9 @@ public class GatewayGuard {
|
||||
throw new GatewayLimitException("GATEWAY_GUARD_UNAVAILABLE", "网关流量控制暂不可用,请稍后重试", 503, 3);
|
||||
}
|
||||
|
||||
private long executePair(String modelKey, String keyKey, String modelLimit, String keyLimit, String ttl, String token) {
|
||||
private long executePair(String modelKey, String keyKey, String waitingKey, String modelLimit, String keyLimit, String ttl, String token, String waitMs, String waitingTtl) {
|
||||
try {
|
||||
Long value = redis.execute(ACQUIRE_PAIR, List.of(modelKey, keyKey), modelLimit, keyLimit, ttl, token);
|
||||
Long value = redis.execute(ACQUIRE_PAIR, List.of(modelKey, keyKey, waitingKey), modelLimit, keyLimit, ttl, token, waitMs, waitingTtl);
|
||||
if (value != null) return value;
|
||||
} catch (DataAccessException exception) {
|
||||
log.warn("Gateway pair admission Redis operation failed ({})", exception.getClass().getSimpleName());
|
||||
@@ -184,6 +230,14 @@ public class GatewayGuard {
|
||||
throw new GatewayLimitException("GATEWAY_GUARD_UNAVAILABLE", "网关流量控制暂不可用,请稍后重试", 503, 3);
|
||||
}
|
||||
|
||||
private void releaseQuietly(String bucket, String token) {
|
||||
try {
|
||||
execute(RELEASE, bucket, token);
|
||||
} catch (GatewayLimitException exception) {
|
||||
log.warn("Gateway concurrency entry release failed; waiting for lease expiry");
|
||||
}
|
||||
}
|
||||
|
||||
public final class Admission implements AutoCloseable {
|
||||
private final Slot modelSlot;
|
||||
private final Slot keySlot;
|
||||
@@ -212,12 +266,8 @@ public class GatewayGuard {
|
||||
@Override
|
||||
public void close() {
|
||||
if (!closed.compareAndSet(false, true)) return;
|
||||
try {
|
||||
execute(RELEASE, key, token);
|
||||
} catch (GatewayLimitException exception) {
|
||||
// A failed release expires automatically and must not replace the upstream response.
|
||||
log.warn("Gateway concurrency slot release failed; waiting for lease expiry");
|
||||
}
|
||||
// A failed release expires automatically and must not replace the upstream response.
|
||||
releaseQuietly(key, token);
|
||||
}
|
||||
}
|
||||
}
|
||||
+37
-17
@@ -5,9 +5,11 @@ import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.moyi.controlplane.common.ApiException;
|
||||
import com.moyi.controlplane.security.SecretCipher;
|
||||
import java.net.URI;
|
||||
import java.net.SocketTimeoutException;
|
||||
import java.net.http.HttpClient;
|
||||
import java.net.http.HttpRequest;
|
||||
import java.net.http.HttpResponse;
|
||||
import java.net.http.HttpTimeoutException;
|
||||
import java.io.*;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.time.Duration;
|
||||
@@ -67,8 +69,13 @@ public class OpenAiCompatibleProvider {
|
||||
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<>() {});
|
||||
Map<String,Object> body=objectMapper.readValue(bytes, new com.fasterxml.jackson.core.type.TypeReference<>() {});
|
||||
if(body.containsKey("error")) throw classifyUpstreamError(200,writeJson(body));
|
||||
return body;
|
||||
} catch(ApiException e) { throw e; }
|
||||
catch(HttpTimeoutException | SocketTimeoutException e) {
|
||||
throw new ApiException("UPSTREAM_PROVIDER_TIMEOUT","上游模型供应商响应超时",504);
|
||||
}
|
||||
catch(Exception e) { throw classifyUpstreamError(502, ""); }
|
||||
}
|
||||
|
||||
@@ -109,6 +116,8 @@ public class OpenAiCompatibleProvider {
|
||||
return new StreamResponse(response.body(),Math.max(1,timeoutSeconds*1000L-TimeUnit.NANOSECONDS.toMillis(System.nanoTime()-started)));
|
||||
} catch (ApiException exception) {
|
||||
throw exception;
|
||||
} catch (HttpTimeoutException | SocketTimeoutException exception) {
|
||||
throw new ApiException("UPSTREAM_PROVIDER_TIMEOUT", "上游模型供应商响应超时", 504);
|
||||
} catch (InterruptedException exception) {
|
||||
Thread.currentThread().interrupt();
|
||||
throw new ApiException("UPSTREAM_PROVIDER_TIMEOUT", "上游模型供应商请求被中断", 502);
|
||||
@@ -131,23 +140,28 @@ public class OpenAiCompatibleProvider {
|
||||
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) {
|
||||
try {
|
||||
// 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 classifyUpstreamError(502, writeJson(chunk));
|
||||
consumer.accept(chunk);
|
||||
}
|
||||
} else if(text.startsWith("data:")) event.append(text.substring(5).stripLeading()).append('\n');
|
||||
} else line.append((char)c);
|
||||
}
|
||||
} catch(IOException exception) {
|
||||
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 classifyUpstreamError(502, writeJson(chunk));
|
||||
consumer.accept(chunk);
|
||||
}
|
||||
} else if(text.startsWith("data:")) event.append(text.substring(5).stripLeading()).append('\n');
|
||||
} else line.append((char)c);
|
||||
throw exception;
|
||||
}
|
||||
throw new ApiException(expired.get()?"UPSTREAM_PROVIDER_TIMEOUT":"UPSTREAM_STREAM_INCOMPLETE","上游流式响应中断,本次未扣费",502);
|
||||
}
|
||||
@@ -169,6 +183,9 @@ public class OpenAiCompatibleProvider {
|
||||
*/
|
||||
static ApiException classifyUpstreamError(int status, String body) {
|
||||
String details = body == null ? "" : body.toLowerCase(java.util.Locale.ROOT);
|
||||
if (status == 408 || status == 504 || details.matches(".*(request|gateway|upstream).{0,12}(timed out|timeout|超时).*")) {
|
||||
return new ApiException("UPSTREAM_PROVIDER_TIMEOUT", "上游模型供应商响应超时", 504);
|
||||
}
|
||||
if (status == 401 || status == 403 || details.matches(".*(invalid.{0,8}(api[ _-]?key|token)|unauthori[sz]ed|forbidden|无效.*(key|凭据)|无权限|认证失败).*")) {
|
||||
return new ApiException("UPSTREAM_AUTH_FAILED", "上游渠道凭据无效或无权限", 502);
|
||||
}
|
||||
@@ -178,6 +195,9 @@ public class OpenAiCompatibleProvider {
|
||||
if (status == 429 || details.matches(".*(rate.?limit|too many requests|concurr|并发|频率过高|请求过于频繁|限流).*")) {
|
||||
return new ApiException("UPSTREAM_RATE_LIMITED", "上游渠道限流或并发已满,请稍后重试", 429);
|
||||
}
|
||||
if (status == 404 || details.matches(".*(no available channel|model.{0,16}(not found|does not exist|unavailable)|not_found|unsupported model|没有可用渠道|模型不存在|模型不可用).*")) {
|
||||
return new ApiException("UPSTREAM_MODEL_UNAVAILABLE", "上游渠道没有可用的模型路由", 502);
|
||||
}
|
||||
return new ApiException("UPSTREAM_PROVIDER_ERROR", "上游模型供应商请求失败", 502);
|
||||
}
|
||||
|
||||
|
||||
+12
-3
@@ -73,12 +73,21 @@ public class AsyncProviderClient {
|
||||
if ("POST".equals(method)) builder.POST(HttpRequest.BodyPublishers.ofString(write(body)));
|
||||
else builder.GET();
|
||||
HttpResponse<String> response = client.send(builder.build(), HttpResponse.BodyHandlers.ofString());
|
||||
if (response.statusCode() < 200 || response.statusCode() >= 300) throw new ApiException("UPSTREAM_PROVIDER_ERROR", message + "(HTTP " + response.statusCode() + ")", 502);
|
||||
if (response.statusCode() < 200 || response.statusCode() >= 300) {
|
||||
String code = switch (response.statusCode()) {
|
||||
case 401, 403 -> "UPSTREAM_AUTH_FAILED";
|
||||
case 402 -> "UPSTREAM_CREDIT_INSUFFICIENT";
|
||||
case 429 -> "UPSTREAM_RATE_LIMITED";
|
||||
default -> "UPSTREAM_PROVIDER_ERROR";
|
||||
};
|
||||
throw new ApiException(code, message + "(HTTP " + response.statusCode() + ")", 502);
|
||||
}
|
||||
Map<String,Object> parsed = mapper.readValue(response.body(), new TypeReference<>() {});
|
||||
Object code = parsed.get("code");
|
||||
if (parsed.containsKey("error") || code instanceof Number number && number.intValue() != 0) {
|
||||
String detail = parsed.get("message") == null ? message : String.valueOf(parsed.get("message"));
|
||||
throw new ApiException("UPSTREAM_PROVIDER_ERROR", detail, 502);
|
||||
// Upstream messages can echo credentials or account details.
|
||||
// Only locally defined diagnostics may reach task history/UI.
|
||||
throw new ApiException("UPSTREAM_PROVIDER_ERROR", message, 502);
|
||||
}
|
||||
return parsed;
|
||||
} catch (ApiException exception) { throw exception; }
|
||||
|
||||
@@ -35,6 +35,7 @@ public class AsyncTaskService {
|
||||
private final int maxQueuedPerModel;
|
||||
private final int defaultModelConcurrency;
|
||||
private final int defaultKeyConcurrency;
|
||||
private final long providerTimeoutMs;
|
||||
private final GatewayGuard gatewayGuard;
|
||||
private final UsageAuditService usage;
|
||||
private final AsyncProviderClient providerClient;
|
||||
@@ -43,7 +44,7 @@ public class AsyncTaskService {
|
||||
/** Compatibility constructor for isolated gateway tests that only exercise mock tasks. */
|
||||
public AsyncTaskService(JdbcTemplate jdbc, ObjectMapper objectMapper, CatalogService catalogService,
|
||||
int maxQueuedPerModel, GatewayGuard gatewayGuard, UsageAuditService usage) {
|
||||
this(jdbc, objectMapper, catalogService, maxQueuedPerModel, 4, 4, gatewayGuard, usage, null, null);
|
||||
this(jdbc, objectMapper, catalogService, maxQueuedPerModel, 4, 4, 3_600_000, gatewayGuard, usage, null, null);
|
||||
}
|
||||
|
||||
@org.springframework.beans.factory.annotation.Autowired
|
||||
@@ -54,18 +55,21 @@ public class AsyncTaskService {
|
||||
@Value("${moyi.tasks.max-queued-per-model:100}") int maxQueuedPerModel,
|
||||
@Value("${moyi.gateway.default-model-concurrency:4}") int defaultModelConcurrency,
|
||||
@Value("${moyi.gateway.default-key-concurrency:4}") int defaultKeyConcurrency,
|
||||
@Value("${moyi.tasks.provider-timeout-ms:3600000}") long providerTimeoutMs,
|
||||
GatewayGuard gatewayGuard,
|
||||
UsageAuditService usage,
|
||||
AsyncProviderClient providerClient,
|
||||
PlatformTransactionManager transactionManager
|
||||
) {
|
||||
if (maxQueuedPerModel < 1 || defaultModelConcurrency < 1 || defaultKeyConcurrency < 1) throw new IllegalArgumentException("Task queue and concurrency limits must be positive");
|
||||
if (providerTimeoutMs < 1_000) throw new IllegalArgumentException("Provider task timeout must be at least one second");
|
||||
this.jdbc = jdbc;
|
||||
this.objectMapper = objectMapper;
|
||||
this.catalogService = catalogService;
|
||||
this.maxQueuedPerModel = maxQueuedPerModel;
|
||||
this.defaultModelConcurrency = defaultModelConcurrency;
|
||||
this.defaultKeyConcurrency = defaultKeyConcurrency;
|
||||
this.providerTimeoutMs = providerTimeoutMs;
|
||||
this.gatewayGuard = gatewayGuard;
|
||||
this.usage = usage;
|
||||
this.providerClient = providerClient;
|
||||
@@ -155,7 +159,7 @@ public class AsyncTaskService {
|
||||
"{\"format\":\"mp4\",\"url\":\"https://mock.moyi.ai/tasks/result.mp4\"}", now(), now()
|
||||
);
|
||||
taskIds.forEach(taskId -> usage.updateTask(taskId, "succeeded", Map.of("format", "result", "url", "https://mock.moyi.ai/tasks/" + taskId + "/result"), null, null));
|
||||
List<Map<String,Object>> pending = jdbc.queryForList("SELECT t.id, t.provider_task_id, m.provider_id FROM model_tasks t JOIN model_catalog m ON m.id=t.model_id WHERE t.status IN ('queued','running') AND t.provider_task_id <> 'mock' ORDER BY t.created_at LIMIT 50");
|
||||
List<Map<String,Object>> pending = jdbc.queryForList("SELECT t.id, t.provider_task_id, t.provider_deadline_at, t.created_at, m.provider_id FROM model_tasks t JOIN model_catalog m ON m.id=t.model_id WHERE t.status IN ('queued','running') AND t.provider_task_id <> 'mock' AND (t.updated_at < now() - INTERVAL '5 seconds' OR t.provider_deadline_at <= now()) ORDER BY t.updated_at LIMIT 50");
|
||||
for (Map<String,Object> row : pending) pollProviderTask(row);
|
||||
dispatchQueuedTasks();
|
||||
}
|
||||
@@ -185,9 +189,12 @@ public class AsyncTaskService {
|
||||
String.valueOf(task.get("task_type")),
|
||||
String.valueOf(task.get("prompt"))
|
||||
);
|
||||
jdbc.update("UPDATE model_tasks SET provider_task_id=?, updated_at=? WHERE id=? AND status='running'", providerTaskId(upstream), now(), taskId);
|
||||
OffsetDateTime submittedAt = now();
|
||||
jdbc.update("UPDATE model_tasks SET provider_task_id=?, provider_deadline_at=?, updated_at=? WHERE id=? AND status='running'", providerTaskId(upstream), submittedAt.plus(java.time.Duration.ofMillis(providerTimeoutMs)), submittedAt, taskId);
|
||||
} catch (RuntimeException exception) {
|
||||
failTask(taskId, "UPSTREAM_TASK_SUBMIT_FAILED", exception.getMessage() == null ? "上游异步任务创建失败" : exception.getMessage());
|
||||
String code = exception instanceof ApiException error ? error.getCode() : "UPSTREAM_TASK_SUBMIT_FAILED";
|
||||
String message = exception instanceof ApiException error ? error.getMessage() : "上游异步任务创建失败";
|
||||
failTask(taskId, code, message);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -236,12 +243,24 @@ public class AsyncTaskService {
|
||||
}
|
||||
|
||||
private void failTask(String taskId, String code, String message) {
|
||||
int changed = jdbc.update(
|
||||
"UPDATE model_tasks SET status='failed', error=CAST(? AS jsonb), updated_at=?, completed_at=? WHERE id=? AND status IN ('queued','running')",
|
||||
writeJson(Map.of("code", code, "message", message)), now(), now(), taskId);
|
||||
if (changed == 0) return;
|
||||
releaseTask(taskId);
|
||||
usage.updateTask(taskId, "failed", null, code, message);
|
||||
finishTask(taskId, "failed", 0, null, code, message);
|
||||
}
|
||||
|
||||
private void finishTask(String taskId, String status, int progress, Map<String,Object> result, String code, String message) {
|
||||
// The terminal state and wallet change must commit together. Otherwise a
|
||||
// crash after marking failed/succeeded leaves a hold that polling never
|
||||
// revisits. The conditional update also prevents duplicate settlement.
|
||||
inTransaction(() -> {
|
||||
int changed = jdbc.update(
|
||||
"UPDATE model_tasks SET status=?, progress=?, result=CAST(? AS jsonb), error=CAST(? AS jsonb), updated_at=?, completed_at=? WHERE id=? AND status IN ('queued','running')",
|
||||
status, progress, result == null ? null : writeJson(result),
|
||||
code == null ? null : writeJson(Map.of("code", code, "message", message)), now(), now(), taskId);
|
||||
if (changed == 0) return null;
|
||||
if ("succeeded".equals(status)) settleTaskInTransaction(taskId);
|
||||
else releaseTaskInTransaction(taskId);
|
||||
usage.updateTask(taskId, status, result, code, message);
|
||||
return null;
|
||||
});
|
||||
}
|
||||
|
||||
private Map<String, Object> requireKey(String authorization) {
|
||||
@@ -286,27 +305,59 @@ public class AsyncTaskService {
|
||||
|
||||
private void pollProviderTask(Map<String,Object> row) {
|
||||
String taskId = String.valueOf(row.get("id"));
|
||||
try {
|
||||
Map<String,Object> response = providerClient.poll(String.valueOf(row.get("provider_id")), String.valueOf(row.get("provider_task_id")));
|
||||
String status = normalizedStatus(response);
|
||||
int progress = progress(response, status);
|
||||
Map<String,Object> result = result(response);
|
||||
if ("succeeded".equals(status) || "failed".equals(status) || "cancelled".equals(status)) {
|
||||
jdbc.update("UPDATE model_tasks SET status=?, progress=?, result=CAST(? AS jsonb), error=CAST(? AS jsonb), updated_at=?, completed_at=? WHERE id=? AND status IN ('queued','running')", status, progress, result == null ? null : writeJson(result), "failed".equals(status) ? writeJson(Map.of("message", "上游异步任务失败")) : null, now(), now(), taskId);
|
||||
if ("succeeded".equals(status)) settleTask(taskId);
|
||||
else releaseTask(taskId);
|
||||
usage.updateTask(taskId, status, result, "failed".equals(status) ? "UPSTREAM_TASK_FAILED" : null, "failed".equals(status) ? "上游异步任务失败" : null);
|
||||
} else {
|
||||
jdbc.update("UPDATE model_tasks SET status=?, progress=?, updated_at=? WHERE id=? AND status IN ('queued','running')", status, progress, now(), taskId);
|
||||
}
|
||||
} catch (Exception exception) {
|
||||
jdbc.update("UPDATE model_tasks SET status='failed', error=CAST(? AS jsonb), updated_at=?, completed_at=? WHERE id=? AND status IN ('queued','running')", writeJson(Map.of("message", "上游异步任务查询失败")), now(), now(), taskId);
|
||||
// A polling failure is terminal for this task. Release the
|
||||
// reserved wallet amount as part of the same failure path so a
|
||||
// transient/provider error cannot permanently lock user credit.
|
||||
releaseTask(taskId);
|
||||
usage.updateTask(taskId, "failed", null, "UPSTREAM_TASK_POLL_FAILED", "上游异步任务查询失败");
|
||||
OffsetDateTime deadline = taskTimestamp(row.get("provider_deadline_at"));
|
||||
if (deadline == null) {
|
||||
// Defensive fallback for pre-migration tasks. The migration also
|
||||
// persists deadlines so restarting cannot reset their timeout.
|
||||
OffsetDateTime createdAt = taskTimestamp(row.get("created_at"));
|
||||
if (createdAt == null) throw new IllegalStateException("Persisted task timestamp is missing");
|
||||
deadline = createdAt.plus(java.time.Duration.ofMillis(providerTimeoutMs));
|
||||
}
|
||||
if (!now().isBefore(deadline)) {
|
||||
failTask(taskId, "UPSTREAM_TASK_TIMEOUT", "异步任务查询已超时,执行结果未知;平台冻结额度已释放。上游可能仍在执行,请先核实后再提交");
|
||||
return;
|
||||
}
|
||||
Map<String,Object> response;
|
||||
try {
|
||||
response = providerClient.poll(String.valueOf(row.get("provider_id")), String.valueOf(row.get("provider_task_id")));
|
||||
} catch (RuntimeException exception) {
|
||||
// Even a permanent authentication/configuration error only proves
|
||||
// that polling failed, not that an already accepted job stopped.
|
||||
// Keep its hold and retry until the persisted deadline.
|
||||
String message = exception instanceof ApiException error && "UPSTREAM_AUTH_FAILED".equals(error.getCode())
|
||||
? "上游查询鉴权异常,执行结果尚未确认;正在重试"
|
||||
: "暂时无法查询上游任务,执行结果尚未确认;正在重试";
|
||||
updatePollingState(taskId, null, "UPSTREAM_TASK_POLL_RETRY", message);
|
||||
return;
|
||||
}
|
||||
String status = normalizedStatus(response);
|
||||
int progress = progress(response, status);
|
||||
Map<String,Object> result = result(response);
|
||||
if ("succeeded".equals(status) || "failed".equals(status) || "cancelled".equals(status)) {
|
||||
// Persistence failures must roll back and remain pollable, rather
|
||||
// than being mistaken for upstream failures and refunded.
|
||||
finishTask(taskId, status, progress, result,
|
||||
"failed".equals(status) ? "UPSTREAM_TASK_FAILED" : null,
|
||||
"failed".equals(status) ? "上游异步任务失败" : null);
|
||||
} else {
|
||||
updatePollingState(taskId, progress, null, null);
|
||||
}
|
||||
}
|
||||
|
||||
private void updatePollingState(String taskId, Integer progress, String code, String message) {
|
||||
inTransaction(() -> {
|
||||
int changed = jdbc.update("UPDATE model_tasks SET status='running', progress=COALESCE(?,progress), error=CAST(? AS jsonb), updated_at=? WHERE id=? AND status IN ('queued','running')",
|
||||
progress, code == null ? null : writeJson(Map.of("code", code, "message", message, "retryable", true)), now(), taskId);
|
||||
if (changed > 0) usage.updateTask(taskId, "running", null, code, message);
|
||||
return null;
|
||||
});
|
||||
}
|
||||
|
||||
private OffsetDateTime taskTimestamp(Object value) {
|
||||
if (value == null) return null;
|
||||
if (value instanceof OffsetDateTime timestamp) return timestamp;
|
||||
if (value instanceof java.sql.Timestamp timestamp) return timestamp.toInstant().atOffset(ZoneOffset.UTC);
|
||||
throw new IllegalStateException("Persisted task timestamp is missing");
|
||||
}
|
||||
|
||||
private String normalizedStatus(Map<String,Object> response) {
|
||||
@@ -381,14 +432,6 @@ public class AsyncTaskService {
|
||||
if (amount.signum() > 0) jdbc.update("UPDATE user_wallets SET reserved_balance=GREATEST(0,reserved_balance-?),updated_at=now() WHERE user_id=?", amount, userId);
|
||||
}
|
||||
|
||||
private void settleTask(String taskId) {
|
||||
if (transactions == null) {
|
||||
settleTaskInTransaction(taskId);
|
||||
return;
|
||||
}
|
||||
transactions.executeWithoutResult(status -> settleTaskInTransaction(taskId));
|
||||
}
|
||||
|
||||
private void settleTaskInTransaction(String taskId) {
|
||||
Map<String,Object> task = jdbc.queryForMap("SELECT user_id,reserved_amount,cost_amount FROM model_tasks WHERE id=? FOR UPDATE", taskId);
|
||||
BigDecimal amount = (BigDecimal) task.get("reserved_amount");
|
||||
@@ -403,20 +446,13 @@ public class AsyncTaskService {
|
||||
jdbc.update("UPDATE usage_events SET cost_amount=? WHERE provider_task_id=? AND event_kind='task'", amount, taskId);
|
||||
}
|
||||
|
||||
private void releaseTask(String taskId) {
|
||||
if (transactions == null) {
|
||||
releaseTaskInTransaction(taskId);
|
||||
return;
|
||||
}
|
||||
transactions.executeWithoutResult(status -> releaseTaskInTransaction(taskId));
|
||||
}
|
||||
|
||||
private void releaseTaskInTransaction(String taskId) {
|
||||
Map<String,Object> task = jdbc.queryForMap("SELECT user_id,reserved_amount,cost_amount FROM model_tasks WHERE id=? FOR UPDATE", taskId);
|
||||
BigDecimal amount = (BigDecimal) task.get("reserved_amount");
|
||||
if (amount != null && amount.signum() > 0 && task.get("cost_amount") == null) {
|
||||
releaseWallet((UUID) task.get("user_id"), amount);
|
||||
jdbc.update("UPDATE model_tasks SET cost_amount=0 WHERE id=?", taskId);
|
||||
jdbc.update("UPDATE usage_events SET cost_amount=0 WHERE provider_task_id=? AND event_kind='task'", taskId);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+69
-10
@@ -7,6 +7,7 @@ import com.fasterxml.jackson.databind.node.ArrayNode;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import com.moyi.controlplane.auth.AuthService;
|
||||
import com.moyi.controlplane.common.ApiException;
|
||||
import com.moyi.controlplane.gateway.GatewayGuard;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
@@ -21,16 +22,31 @@ import org.springframework.dao.EmptyResultDataAccessException;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
|
||||
@Service
|
||||
public class UsageAuditService {
|
||||
private static final int MAX_PAYLOAD_CHARS = 32_000;
|
||||
private final JdbcTemplate jdbc;
|
||||
private final ObjectMapper objectMapper;
|
||||
private final GatewayGuard gatewayGuard;
|
||||
private final int asyncQueueLimit;
|
||||
private final long asyncWorkerIntervalMs;
|
||||
|
||||
public UsageAuditService(JdbcTemplate jdbc, ObjectMapper objectMapper) {
|
||||
this(jdbc, objectMapper, null, 100, 1000);
|
||||
}
|
||||
|
||||
@Autowired
|
||||
public UsageAuditService(JdbcTemplate jdbc, ObjectMapper objectMapper, GatewayGuard gatewayGuard,
|
||||
@Value("${moyi.tasks.max-queued-per-model:100}") int asyncQueueLimit,
|
||||
@Value("${moyi.tasks.worker-interval-ms:1000}") long asyncWorkerIntervalMs) {
|
||||
this.jdbc = jdbc;
|
||||
this.objectMapper = objectMapper;
|
||||
this.gatewayGuard = gatewayGuard;
|
||||
this.asyncQueueLimit = asyncQueueLimit;
|
||||
this.asyncWorkerIntervalMs = asyncWorkerIntervalMs;
|
||||
}
|
||||
|
||||
public void startChat(UUID id, UUID userId, UUID keyId, Map<String,Object> model, Map<String,Object> input) {
|
||||
@@ -105,7 +121,6 @@ public class UsageAuditService {
|
||||
Map<String, Object> result = new LinkedHashMap<>();
|
||||
result.put("callsToday", count("SELECT COUNT(*) FROM usage_events WHERE deleted_at IS NULL AND event_kind = 'call' AND created_at >= CURRENT_DATE"));
|
||||
result.put("callsThisMonth", count("SELECT COUNT(*) FROM usage_events WHERE deleted_at IS NULL AND event_kind = 'call' AND created_at >= date_trunc('month', now())"));
|
||||
result.put("waiting", count("SELECT COUNT(*) FROM usage_events WHERE deleted_at IS NULL AND status IN ('queued','running')"));
|
||||
result.put("costToday", decimal("SELECT COALESCE(SUM(cost_amount),0) FROM usage_events WHERE deleted_at IS NULL AND status = 'succeeded' AND created_at >= CURRENT_DATE"));
|
||||
result.put("costThisMonth", decimal("SELECT COALESCE(SUM(cost_amount),0) FROM usage_events WHERE deleted_at IS NULL AND status = 'succeeded' AND created_at >= date_trunc('month', now())"));
|
||||
result.put("series", jdbc.query(
|
||||
@@ -114,26 +129,70 @@ public class UsageAuditService {
|
||||
"LEFT JOIN usage_events ue ON ue.deleted_at IS NULL AND ue.created_at >= day AND ue.created_at < day + INTERVAL '1 day' " +
|
||||
"GROUP BY day ORDER BY day",
|
||||
(rs, row) -> Map.of("date", rs.getString("day"), "count", rs.getLong("count"))));
|
||||
result.put("capacityGroups", jdbc.query(
|
||||
"SELECT m.id, m.public_name, COALESCE(m.max_concurrency, 0) AS max_concurrency, " +
|
||||
"COUNT(*) FILTER (WHERE ue.status = 'running') AS running, " +
|
||||
"COUNT(*) FILTER (WHERE ue.status IN ('queued','running')) AS queued " +
|
||||
"FROM model_catalog m LEFT JOIN usage_events ue ON ue.model_id = m.id " +
|
||||
"AND ue.deleted_at IS NULL AND ue.created_at >= now() - INTERVAL '10 minutes' " +
|
||||
"WHERE m.deleted_at IS NULL AND m.status = 'active' " +
|
||||
"GROUP BY m.id, m.public_name, m.max_concurrency ORDER BY m.public_name LIMIT 100",
|
||||
int defaultModelLimit = gatewayGuard == null ? 4 : gatewayGuard.defaultModelConcurrency();
|
||||
List<Map<String, Object>> groups = jdbc.query("""
|
||||
WITH pending AS (
|
||||
SELECT model_id,
|
||||
COUNT(*) FILTER (WHERE status='running') AS running,
|
||||
COUNT(*) FILTER (WHERE status='queued') AS queued
|
||||
FROM model_tasks WHERE status IN ('queued','running') GROUP BY model_id
|
||||
)
|
||||
SELECT m.id, m.public_name, m.source_label, p.name AS provider_name, m.call_mode,
|
||||
COALESCE(NULLIF(m.max_concurrency, 0), ?) AS max_concurrency,
|
||||
(m.status='active' AND m.deleted_at IS NULL AND p.status='active' AND p.deleted_at IS NULL
|
||||
AND d.status='active' AND d.deleted_at IS NULL) AS route_enabled,
|
||||
COALESCE(pending.running, 0) AS running, COALESCE(pending.queued, 0) AS queued
|
||||
FROM model_catalog m JOIN model_providers p ON p.id=m.provider_id
|
||||
JOIN model_definitions d ON d.id=m.logical_model_id
|
||||
LEFT JOIN pending ON pending.model_id=m.id
|
||||
ORDER BY m.public_name, m.source_label, m.id
|
||||
""",
|
||||
(rs, row) -> {
|
||||
Map<String, Object> group = new LinkedHashMap<>();
|
||||
group.put("id", rs.getString("id"));
|
||||
group.put("name", rs.getString("public_name"));
|
||||
group.put("sourceLabel", rs.getString("source_label"));
|
||||
group.put("providerName", rs.getString("provider_name"));
|
||||
group.put("mode", rs.getString("call_mode"));
|
||||
group.put("routeEnabled", rs.getBoolean("route_enabled"));
|
||||
group.put("limit", rs.getInt("max_concurrency"));
|
||||
group.put("running", rs.getLong("running"));
|
||||
group.put("queued", rs.getLong("queued"));
|
||||
return group;
|
||||
}));
|
||||
}, defaultModelLimit);
|
||||
List<String> synchronousIds = groups.stream().filter(group -> !"async".equals(group.get("mode"))).map(group -> group.get("id").toString()).toList();
|
||||
GatewayGuard.CapacitySnapshot snapshot = gatewayGuard == null
|
||||
? new GatewayGuard.CapacitySnapshot(false, Map.of()) : gatewayGuard.capacitySnapshot(synchronousIds);
|
||||
for (Map<String, Object> group : groups) {
|
||||
if ("async".equals(group.get("mode"))) continue;
|
||||
GatewayGuard.Capacity current = snapshot.groups().get(group.get("id").toString());
|
||||
group.put("running", current == null ? null : current.running());
|
||||
group.put("queued", current == null ? null : current.queued());
|
||||
}
|
||||
// Keep disabled groups visible while their queued or running work still exists.
|
||||
groups = groups.stream().filter(group -> Boolean.TRUE.equals(group.get("routeEnabled"))
|
||||
|| positive(group.get("running")) || positive(group.get("queued"))).toList();
|
||||
result.put("capacityGroups", groups);
|
||||
result.put("running", totalCapacity(groups, "running"));
|
||||
result.put("waiting", totalCapacity(groups, "queued"));
|
||||
result.put("capacityConfig", Map.of(
|
||||
"queueWaitMs", gatewayGuard == null ? 30000 : gatewayGuard.queueWaitMs(),
|
||||
"defaultModelConcurrency", defaultModelLimit,
|
||||
"defaultKeyConcurrency", gatewayGuard == null ? 4 : gatewayGuard.defaultKeyConcurrency(),
|
||||
"asyncQueueLimit", asyncQueueLimit,
|
||||
"asyncWorkerIntervalMs", asyncWorkerIntervalMs,
|
||||
"synchronousSnapshotAvailable", snapshot.available()
|
||||
));
|
||||
return result;
|
||||
}
|
||||
|
||||
private boolean positive(Object value) { return value instanceof Number number && number.longValue() > 0; }
|
||||
|
||||
private Long totalCapacity(List<Map<String, Object>> groups, String field) {
|
||||
if (groups.stream().anyMatch(group -> group.get(field) == null)) return null;
|
||||
return groups.stream().mapToLong(group -> ((Number) group.get(field)).longValue()).sum();
|
||||
}
|
||||
|
||||
private long count(String sql) {
|
||||
Number value = jdbc.queryForObject(sql, Number.class);
|
||||
return value == null ? 0L : value.longValue();
|
||||
|
||||
@@ -50,6 +50,7 @@ moyi:
|
||||
tasks:
|
||||
max-queued-per-model: ${TASK_MAX_QUEUED_PER_MODEL:100}
|
||||
worker-interval-ms: ${MOYI_TASK_WORKER_INTERVAL_MS:1000}
|
||||
provider-timeout-ms: ${MOYI_TASK_PROVIDER_TIMEOUT_MS:3600000}
|
||||
web:
|
||||
cors-origins: ${CONTROL_PLANE_CORS_ORIGINS:http://localhost:5173,http://127.0.0.1:5173}
|
||||
auth:
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
-- Persist the polling deadline: process restarts must not reset the timeout.
|
||||
ALTER TABLE model_tasks ADD COLUMN provider_deadline_at timestamptz;
|
||||
|
||||
-- Existing accepted tasks also need a finite deadline. Unsubmitted queued
|
||||
-- tasks receive theirs only after the upstream accepts their submission.
|
||||
UPDATE model_tasks
|
||||
SET provider_deadline_at = created_at + INTERVAL '1 hour'
|
||||
WHERE status IN ('queued', 'running')
|
||||
AND provider_task_id IS NOT NULL
|
||||
AND provider_task_id <> 'mock';
|
||||
@@ -2,8 +2,18 @@ local time = redis.call('TIME')
|
||||
local now = tonumber(time[1]) * 1000 + math.floor(tonumber(time[2]) / 1000)
|
||||
redis.call('ZREMRANGEBYSCORE', KEYS[1], '-inf', now)
|
||||
redis.call('ZREMRANGEBYSCORE', KEYS[2], '-inf', now)
|
||||
if redis.call('ZCARD', KEYS[1]) >= tonumber(ARGV[1]) then return 0 end
|
||||
if redis.call('ZCARD', KEYS[2]) >= tonumber(ARGV[2]) then return 2 end
|
||||
redis.call('ZREMRANGEBYSCORE', KEYS[3], '-inf', now)
|
||||
local busy = nil
|
||||
if redis.call('ZCARD', KEYS[1]) >= tonumber(ARGV[1]) then busy = 0
|
||||
elseif redis.call('ZCARD', KEYS[2]) >= tonumber(ARGV[2]) then busy = 2 end
|
||||
if busy ~= nil then
|
||||
if tonumber(ARGV[5]) > 0 then
|
||||
redis.call('ZADD', KEYS[3], now + tonumber(ARGV[5]), ARGV[4])
|
||||
redis.call('PEXPIRE', KEYS[3], ARGV[6])
|
||||
end
|
||||
return busy
|
||||
end
|
||||
redis.call('ZREM', KEYS[3], ARGV[4])
|
||||
redis.call('ZADD', KEYS[1], now + tonumber(ARGV[3]), ARGV[4])
|
||||
redis.call('ZADD', KEYS[2], now + tonumber(ARGV[3]), ARGV[4])
|
||||
redis.call('PEXPIRE', KEYS[1], ARGV[3])
|
||||
|
||||
+27
-1
@@ -68,7 +68,8 @@ class ChatStreamingTest {
|
||||
// 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->{}); } });
|
||||
var error=assertThrows(ApiException.class,()->{ try(var stream=provider.stream("provider-deepseek","alias",request)) { stream.events(chunk->{}); } });
|
||||
assertEquals("UPSTREAM_PROVIDER_TIMEOUT",error.getCode());
|
||||
assertTrue(TimeUnit.NANOSECONDS.toMillis(System.nanoTime()-started)<4000);
|
||||
}
|
||||
@Test void upstreamHttpFailureNeverBecomesSuccessfulStream() {
|
||||
@@ -84,6 +85,31 @@ class ChatStreamingTest {
|
||||
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 reasoningAliasAndContentPartsAreCollectedSeparately() {
|
||||
var collector=new ChatStreamCollector("public-route",System.currentTimeMillis());
|
||||
collector.accept(Map.of("choices",List.of(Map.of("delta",Map.of(
|
||||
"content",List.of(Map.of("type","text","text","OK")),
|
||||
"reasoning", "thinking"
|
||||
)))));
|
||||
collector.accept(Map.of("choices",List.of(Map.of("delta",Map.of(),"finish_reason","stop"))));
|
||||
var message=(Map<?,?>)((Map<?,?>)((List<?>)collector.result().get("choices")).getFirst()).get("message");
|
||||
assertEquals("OK",message.get("content"));
|
||||
assertEquals("thinking",message.get("reasoning_content"));
|
||||
}
|
||||
|
||||
@Test void finishedWhitespaceOnlyStreamIsRejectedBeforeSettlement() {
|
||||
var collector=new ChatStreamCollector("public-route",System.currentTimeMillis());
|
||||
collector.accept(Map.of("choices",List.of(Map.of("delta",Map.of("content"," \n")))));
|
||||
collector.accept(Map.of("choices",List.of(Map.of("delta",Map.of(),"finish_reason","stop"))));
|
||||
var error=assertThrows(ApiException.class,collector::result);
|
||||
assertEquals("UPSTREAM_RESPONSE_INVALID",error.getCode());
|
||||
}
|
||||
@Test void completedStreamWithoutContentOrReasoningIsNotBillable() {
|
||||
var collector=new ChatStreamCollector("public-route",System.currentTimeMillis());
|
||||
collector.accept(Map.of("choices",List.of(Map.of("index",0,"delta",Map.of(),"finish_reason","stop"))));
|
||||
assertEquals("UPSTREAM_RESPONSE_INVALID",assertThrows(ApiException.class,collector::result).getCode());
|
||||
}
|
||||
@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));
|
||||
|
||||
+85
@@ -20,6 +20,32 @@ import static org.mockito.ArgumentMatchers.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
class GatewayDeploymentTest {
|
||||
@Test
|
||||
void capacitySnapshotReadsBothLiveAndWaitingBucketsAndDoesNotInventZeroOnFailure() {
|
||||
StringRedisTemplate redis = mock(StringRedisTemplate.class);
|
||||
doReturn(List.of(2L, 3L, 0L, 1L)).when(redis).execute(any(RedisScript.class), anyList());
|
||||
GatewayGuard guard = new GatewayGuard(redis, 60, 300, 4, "moyi:test:");
|
||||
var snapshot = guard.capacitySnapshot(List.of("model-a", "model-b"));
|
||||
assertTrue(snapshot.available());
|
||||
assertEquals(new GatewayGuard.Capacity(2, 3), snapshot.groups().get("model-a"));
|
||||
assertEquals(new GatewayGuard.Capacity(0, 1), snapshot.groups().get("model-b"));
|
||||
verify(redis).execute(any(RedisScript.class), eq(List.of(
|
||||
"moyi:test:gateway:slots:model:model-a", "moyi:test:gateway:waiting:model:model-a",
|
||||
"moyi:test:gateway:slots:model:model-b", "moyi:test:gateway:waiting:model:model-b")));
|
||||
doThrow(new org.springframework.dao.DataAccessResourceFailureException("offline")).when(redis).execute(any(RedisScript.class), anyList());
|
||||
assertFalse(guard.capacitySnapshot(List.of("model-a")).available());
|
||||
assertTrue(guard.capacitySnapshot(List.of("model-a")).groups().isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void queueTimeoutCleansItsWaitingEntry() {
|
||||
StringRedisTemplate redis = mock(StringRedisTemplate.class);
|
||||
doReturn(0L).when(redis).execute(any(RedisScript.class), anyList(), any(Object[].class));
|
||||
GatewayGuard guard = new GatewayGuard(redis, 60, 300, 1, 1, 15, 10, "moyi:test:");
|
||||
assertThrows(GatewayLimitException.class, () -> guard.acquire(Map.of("id", "model-a"), UUID.randomUUID(), 15));
|
||||
verify(redis).execute(any(RedisScript.class), eq(List.of("moyi:test:gateway:waiting:model:model-a")), any(Object[].class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void redisRateKeysAreEnvironmentScoped() {
|
||||
StringRedisTemplate redis = mock(StringRedisTemplate.class);
|
||||
@@ -108,6 +134,65 @@ class GatewayDeploymentTest {
|
||||
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
|
||||
void syncResponsePreservesReasoningAndFinishReasonWhenContentIsEmpty() {
|
||||
CatalogService catalog = mock(CatalogService.class);
|
||||
GatewayGuard guard = mock(GatewayGuard.class);
|
||||
OpenAiCompatibleProvider provider = mock(OpenAiCompatibleProvider.class);
|
||||
ChatBillingService billing = mock(ChatBillingService.class);
|
||||
String userId = UUID.randomUUID().toString();
|
||||
Map<String, Object> key = Map.of("id", UUID.randomUUID().toString(), "userId", userId);
|
||||
Map<String, Object> model = Map.of("id", "reasoning-route", "providerId", "provider-relay", "upstreamModel", "reasoning-model", "mode", "sync");
|
||||
when(catalog.authenticateKey("test-key")).thenReturn(key);
|
||||
when(catalog.activeModel("reasoning-route")).thenReturn(model);
|
||||
when(catalog.keyCanInvoke(key, "reasoning-route")).thenReturn(true);
|
||||
when(provider.isConfigured("provider-relay")).thenReturn(true);
|
||||
Map<String, Object> upstreamResponse = new java.util.LinkedHashMap<>(Map.of(
|
||||
"id", "upstream-response",
|
||||
"choices", List.of(Map.of("index", 0, "message", Map.of("role", "assistant", "content", "", "reasoning_content", "private reasoning"), "finish_reason", "length")),
|
||||
"usage", Map.of("prompt_tokens", 2, "completion_tokens", 16, "total_tokens", 18)
|
||||
));
|
||||
when(provider.chat(eq("provider-relay"), eq("reasoning-model"), anyMap())).thenReturn(upstreamResponse);
|
||||
when(billing.reserve(anyMap(), anyMap(), any(), isNull())).thenReturn(new ChatBillingService.Ticket(UUID.randomUUID(), UUID.fromString(userId), ChatMeter.rates(null, true)));
|
||||
when(billing.settle(any(), anyMap(), anyInt(), isNull())).thenReturn(java.math.BigDecimal.ZERO);
|
||||
|
||||
GatewayController controller = new GatewayController(catalog, provider, guard, billing, new ObjectMapper());
|
||||
var response = controller.chatCompletions("Bearer test-key", Map.of("model", "reasoning-route", "messages", List.of(Map.of("role", "user", "content", "hello"))), null, new MockHttpServletResponse());
|
||||
assertEquals(200, response.getStatusCode().value());
|
||||
Map<?, ?> choice = (Map<?, ?>)((List<?>)response.getBody().get("choices")).getFirst();
|
||||
Map<?, ?> message = (Map<?, ?>)choice.get("message");
|
||||
assertEquals("", message.get("content"));
|
||||
assertEquals("private reasoning", message.get("reasoning_content"));
|
||||
assertEquals("length", choice.get("finish_reason"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void trulyEmptySyncResponseIsRejectedBeforeSettlement() {
|
||||
CatalogService catalog = mock(CatalogService.class);
|
||||
GatewayGuard guard = mock(GatewayGuard.class);
|
||||
OpenAiCompatibleProvider provider = mock(OpenAiCompatibleProvider.class);
|
||||
ChatBillingService billing = mock(ChatBillingService.class);
|
||||
String userId = UUID.randomUUID().toString();
|
||||
Map<String, Object> key = Map.of("id", UUID.randomUUID().toString(), "userId", userId);
|
||||
Map<String, Object> model = Map.of("id", "empty-route", "providerId", "provider-relay", "upstreamModel", "empty-model", "mode", "sync");
|
||||
when(catalog.authenticateKey("test-key")).thenReturn(key);
|
||||
when(catalog.activeModel("empty-route")).thenReturn(model);
|
||||
when(catalog.keyCanInvoke(key, "empty-route")).thenReturn(true);
|
||||
when(provider.isConfigured("provider-relay")).thenReturn(true);
|
||||
Map<String, Object> upstreamResponse = new java.util.LinkedHashMap<>(Map.of(
|
||||
"id", "upstream-empty",
|
||||
"choices", List.of(Map.of("index", 0, "message", Map.of("role", "assistant", "content", ""), "finish_reason", "stop"))
|
||||
));
|
||||
when(provider.chat(eq("provider-relay"), eq("empty-model"), anyMap())).thenReturn(upstreamResponse);
|
||||
when(billing.reserve(anyMap(), anyMap(), any(), isNull())).thenReturn(new ChatBillingService.Ticket(UUID.randomUUID(), UUID.fromString(userId), ChatMeter.rates(null, true)));
|
||||
GatewayController controller = new GatewayController(catalog, provider, guard, billing, new ObjectMapper());
|
||||
|
||||
ApiException error = assertThrows(ApiException.class, () -> controller.chatCompletions("Bearer test-key", Map.of("model", "empty-route", "messages", List.of(Map.of("role", "user", "content", "hello"))), null, new MockHttpServletResponse()));
|
||||
assertEquals("UPSTREAM_RESPONSE_INVALID", error.getCode());
|
||||
verify(billing, never()).settle(any(), anyMap(), anyInt(), any());
|
||||
verify(billing).fail(any(), eq("UPSTREAM_RESPONSE_INVALID"), anyString(), anyInt());
|
||||
}
|
||||
|
||||
@Test
|
||||
void unimplementedMediaProviderDoesNotCreateFakeTask() {
|
||||
JdbcTemplate jdbc = mock(JdbcTemplate.class);
|
||||
|
||||
+21
@@ -26,4 +26,25 @@ class ProviderErrorClassificationTest {
|
||||
assertEquals("UPSTREAM_RATE_LIMITED", error.getCode());
|
||||
assertEquals(429, error.getStatus());
|
||||
}
|
||||
|
||||
@Test
|
||||
void classifiesMissingUpstreamRouteWithoutEchoingProviderDetails() {
|
||||
var error = OpenAiCompatibleProvider.classifyUpstreamError(502, "No available channel for model gemini-3-flash");
|
||||
assertEquals("UPSTREAM_MODEL_UNAVAILABLE", error.getCode());
|
||||
assertEquals(502, error.getStatus());
|
||||
assertEquals("上游渠道没有可用的模型路由", error.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void classifiesErrorEnvelopeReturnedWithHttp200() {
|
||||
var error = OpenAiCompatibleProvider.classifyUpstreamError(200, "{\"error\":{\"code\":\"invalid_api_key\",\"message\":\"unauthorized\"}}");
|
||||
assertEquals("UPSTREAM_AUTH_FAILED", error.getCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void classifiesTimeoutResponsesWithoutExposingProviderBody() {
|
||||
var error = OpenAiCompatibleProvider.classifyUpstreamError(504, "gateway timeout at upstream");
|
||||
assertEquals("UPSTREAM_PROVIDER_TIMEOUT", error.getCode());
|
||||
assertEquals(504, error.getStatus());
|
||||
}
|
||||
}
|
||||
+77
@@ -0,0 +1,77 @@
|
||||
package com.moyi.controlplane.task;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.moyi.controlplane.common.ApiException;
|
||||
import com.moyi.controlplane.security.SecretCipher;
|
||||
import com.sun.net.httpserver.HttpServer;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Map;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.CsvSource;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
|
||||
class AsyncProviderClientTest {
|
||||
@Test
|
||||
void upstreamErrorDoesNotExposeProviderMessage() throws Exception {
|
||||
HttpServer server = HttpServer.create(new InetSocketAddress("127.0.0.1", 0), 0);
|
||||
server.createContext("/", exchange -> {
|
||||
byte[] body = "{\"code\":1001,\"message\":\"secret account and API key details\"}".getBytes(StandardCharsets.UTF_8);
|
||||
exchange.getResponseHeaders().set("Content-Type", "application/json");
|
||||
exchange.sendResponseHeaders(200, body.length);
|
||||
try (var output = exchange.getResponseBody()) { output.write(body); }
|
||||
});
|
||||
server.start();
|
||||
try {
|
||||
String baseUrl = "http://127.0.0.1:" + server.getAddress().getPort() + "/tripo";
|
||||
AsyncProviderClient client = clientFor(baseUrl);
|
||||
|
||||
ApiException error = assertThrows(ApiException.class, () -> client.submit("provider-vast", "v3.1-20260211", "3d", "chair"));
|
||||
assertEquals("UPSTREAM_PROVIDER_ERROR", error.getCode());
|
||||
assertEquals("上游 3D 任务创建失败", error.getMessage());
|
||||
} finally {
|
||||
server.stop(0);
|
||||
}
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvSource({"401,UPSTREAM_AUTH_FAILED", "403,UPSTREAM_AUTH_FAILED", "402,UPSTREAM_CREDIT_INSUFFICIENT", "429,UPSTREAM_RATE_LIMITED", "503,UPSTREAM_PROVIDER_ERROR"})
|
||||
void httpErrorsExposeOnlySafeCodes(int status, String code) throws Exception {
|
||||
HttpServer server = HttpServer.create(new InetSocketAddress("127.0.0.1", 0), 0);
|
||||
server.createContext("/", exchange -> {
|
||||
byte[] body = "private account details".getBytes(StandardCharsets.UTF_8);
|
||||
exchange.sendResponseHeaders(status, body.length);
|
||||
try (var output = exchange.getResponseBody()) { output.write(body); }
|
||||
});
|
||||
server.start();
|
||||
try {
|
||||
AsyncProviderClient client = clientFor("http://127.0.0.1:" + server.getAddress().getPort() + "/tripo");
|
||||
ApiException submit = assertThrows(ApiException.class, () -> client.submit("provider-vast", "v3.1-20260211", "3d", "chair"));
|
||||
assertEquals(code, submit.getCode());
|
||||
assertEquals("上游 3D 任务创建失败(HTTP " + status + ")", submit.getMessage());
|
||||
ApiException poll = assertThrows(ApiException.class, () -> client.poll("provider-vast", "task-test"));
|
||||
assertEquals(code, poll.getCode());
|
||||
assertEquals("上游异步任务查询失败(HTTP " + status + ")", poll.getMessage());
|
||||
} finally {
|
||||
server.stop(0);
|
||||
}
|
||||
}
|
||||
|
||||
private AsyncProviderClient clientFor(String baseUrl) {
|
||||
JdbcTemplate jdbc = mock(JdbcTemplate.class);
|
||||
SecretCipher cipher = new SecretCipher("test-encryption");
|
||||
when(jdbc.queryForMap(any(String.class), eq("provider-vast"))).thenReturn(Map.of(
|
||||
"code", "vast-tripo", "base_url", baseUrl, "credential_ciphertext", cipher.encrypt("test-key")
|
||||
));
|
||||
return new AsyncProviderClient(jdbc, cipher, new ObjectMapper(), mock(Environment.class));
|
||||
}
|
||||
}
|
||||
+186
@@ -0,0 +1,186 @@
|
||||
package com.moyi.controlplane.task;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.moyi.controlplane.catalog.CatalogService;
|
||||
import com.moyi.controlplane.common.ApiException;
|
||||
import com.moyi.controlplane.gateway.GatewayGuard;
|
||||
import com.moyi.controlplane.usage.UsageAuditService;
|
||||
import java.math.BigDecimal;
|
||||
import java.time.Duration;
|
||||
import java.time.OffsetDateTime;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import org.flywaydb.core.Flyway;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
|
||||
import org.springframework.jdbc.datasource.DriverManagerDataSource;
|
||||
import org.springframework.test.util.ReflectionTestUtils;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.ArgumentMatchers.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
@EnabledIfEnvironmentVariable(named="MOYI_AUTH_TEST_JDBC_URL", matches="jdbc:postgresql://127\\.0\\.0\\.1:[0-9]+/.+")
|
||||
class AsyncTaskPostgresTest {
|
||||
private final ObjectMapper json = new ObjectMapper().findAndRegisterModules();
|
||||
private final UUID userId = UUID.randomUUID(), keyId = UUID.randomUUID();
|
||||
private final String taskId = "async-transaction-test";
|
||||
private String schema;
|
||||
private DriverManagerDataSource base;
|
||||
private JdbcTemplate jdbc;
|
||||
private UsageAuditService usage;
|
||||
private AsyncProviderClient provider;
|
||||
private AsyncTaskService tasks;
|
||||
|
||||
@BeforeEach void setup() {
|
||||
String url = System.getenv("MOYI_AUTH_TEST_JDBC_URL");
|
||||
schema = "async_task_test_" + UUID.randomUUID().toString().replace("-", "");
|
||||
base = new DriverManagerDataSource(url, "moyi_role_test", "");
|
||||
var source = new DriverManagerDataSource(url + "?currentSchema=" + schema, "moyi_role_test", "");
|
||||
Flyway.configure().dataSource(base).schemas(schema).defaultSchema(schema).target("25").load().migrate();
|
||||
jdbc = new JdbcTemplate(source);
|
||||
usage = spy(new UsageAuditService(jdbc, json));
|
||||
provider = mock(AsyncProviderClient.class);
|
||||
tasks = new AsyncTaskService(jdbc, json, mock(CatalogService.class), 100, 4, 4, 120_000,
|
||||
mock(GatewayGuard.class), usage, provider, new DataSourceTransactionManager(source));
|
||||
jdbc.update("INSERT INTO users(id,account,account_kind,password_hash,display_name) VALUES (?,'async-test','username','not-a-password','Test')", userId);
|
||||
jdbc.update("INSERT INTO platform_api_keys(id,user_id,name,key_prefix,key_hash) VALUES (?,?,'Test','test-prefix',?)", keyId, userId, "b".repeat(64));
|
||||
jdbc.update("INSERT INTO user_wallets(user_id,balance,reserved_balance) VALUES (?,10,1.44)", userId);
|
||||
jdbc.update("INSERT INTO model_tasks(id,user_id,api_key_id,model_id,task_type,status,prompt,reserved_amount,provider_task_id) VALUES (?,?,?,'model-mock-3d','3d','running','chair',1.44,'upstream-task')", taskId,userId,keyId);
|
||||
usage.recordTask(userId,keyId,"model-mock-3d","provider-vast",taskId,"running",Map.of("prompt","chair"),null,null,null);
|
||||
Flyway.configure().dataSource(base).schemas(schema).defaultSchema(schema).load().migrate();
|
||||
}
|
||||
|
||||
@AfterEach void cleanup() {
|
||||
if (base != null && schema != null) new JdbcTemplate(base).execute("DROP SCHEMA IF EXISTS " + schema + " CASCADE");
|
||||
}
|
||||
|
||||
private void fail() {
|
||||
ReflectionTestUtils.invokeMethod(tasks,"failTask",taskId,"PROVIDER_NOT_CONFIGURED","异步渠道尚未配置 API Key");
|
||||
}
|
||||
private void poll() {
|
||||
Map<String,Object> row = jdbc.queryForMap("SELECT id,provider_task_id,provider_deadline_at,created_at FROM model_tasks WHERE id=?", taskId);
|
||||
row.put("provider_id", "provider-vast");
|
||||
ReflectionTestUtils.invokeMethod(tasks,"pollProviderTask",row);
|
||||
}
|
||||
private BigDecimal wallet(String column) {
|
||||
return jdbc.queryForObject("SELECT " + column + " FROM user_wallets WHERE user_id=?",BigDecimal.class,userId);
|
||||
}
|
||||
private String status() {
|
||||
return jdbc.queryForObject("SELECT status FROM model_tasks WHERE id=?",String.class,taskId);
|
||||
}
|
||||
private BigDecimal cost() {
|
||||
return jdbc.queryForObject("SELECT cost_amount FROM model_tasks WHERE id=?",BigDecimal.class,taskId);
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings={"PROVIDER_NOT_CONFIGURED", "UPSTREAM_AUTH_FAILED"})
|
||||
void submissionFailureReleasesExactlyOnceWithoutDebitingBalance(String code) {
|
||||
jdbc.update("UPDATE model_tasks SET status='queued',provider_task_id=NULL WHERE id=?",taskId);
|
||||
when(provider.submit(anyString(),anyString(),anyString(),anyString())).thenThrow(new ApiException(code,"上游任务未接受",503));
|
||||
tasks.completeMockTasks();
|
||||
tasks.completeMockTasks();
|
||||
assertEquals("failed",status());
|
||||
assertEquals(new BigDecimal("10.00000000"),wallet("balance"));
|
||||
assertEquals(0,wallet("reserved_balance").signum());
|
||||
assertEquals(0,cost().signum());
|
||||
assertEquals(0,jdbc.queryForObject("SELECT cost_amount FROM usage_events WHERE provider_task_id=?",BigDecimal.class,taskId).signum());
|
||||
assertEquals(0,jdbc.queryForObject("SELECT count(*) FROM wallet_entries WHERE user_id=?",Integer.class,userId));
|
||||
verify(provider,times(1)).submit(anyString(),anyString(),anyString(),anyString());
|
||||
}
|
||||
|
||||
@Test void refundAndTaskStateRollBackTogetherWhenAuditFails() {
|
||||
doThrow(new IllegalStateException("simulated database write failure")).when(usage).updateTask(eq(taskId),eq("failed"),isNull(),anyString(),anyString());
|
||||
assertThrows(IllegalStateException.class,this::fail);
|
||||
assertEquals("running",status());
|
||||
assertEquals(new BigDecimal("1.44000000"),wallet("reserved_balance"));
|
||||
assertNull(cost());
|
||||
doCallRealMethod().when(usage).updateTask(eq(taskId),eq("failed"),isNull(),anyString(),anyString());
|
||||
fail(); fail();
|
||||
assertEquals("failed",status());
|
||||
assertEquals(0,wallet("reserved_balance").signum());
|
||||
assertEquals(new BigDecimal("10.00000000"),wallet("balance"));
|
||||
}
|
||||
|
||||
@Test void successfulSettlementRollsBackForRetryAndCannotBeRefundedByLateFailure() {
|
||||
when(provider.poll("provider-vast","upstream-task")).thenReturn(Map.of("data",Map.of("status","success","progress",100,"output",Map.of("model_url","https://example.test/model.glb"))));
|
||||
doThrow(new IllegalStateException("simulated database write failure")).when(usage).updateTask(eq(taskId),eq("succeeded"),anyMap(),isNull(),isNull());
|
||||
assertThrows(IllegalStateException.class,this::poll);
|
||||
assertEquals("running",status());
|
||||
assertEquals(new BigDecimal("10.00000000"),wallet("balance"));
|
||||
assertEquals(new BigDecimal("1.44000000"),wallet("reserved_balance"));
|
||||
assertNull(cost());
|
||||
assertEquals(0,jdbc.queryForObject("SELECT count(*) FROM wallet_entries WHERE user_id=?",Integer.class,userId));
|
||||
doCallRealMethod().when(usage).updateTask(eq(taskId),eq("succeeded"),anyMap(),isNull(),isNull());
|
||||
poll(); poll(); fail();
|
||||
assertEquals("succeeded",status());
|
||||
assertEquals(new BigDecimal("8.56000000"),wallet("balance"));
|
||||
assertEquals(0,wallet("reserved_balance").signum());
|
||||
assertEquals(new BigDecimal("1.44000000"),cost());
|
||||
assertEquals(1,jdbc.queryForObject("SELECT count(*) FROM wallet_entries WHERE user_id=?",Integer.class,userId));
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings={"UPSTREAM_PROVIDER_ERROR", "UPSTREAM_AUTH_FAILED", "UPSTREAM_RATE_LIMITED"})
|
||||
void pollingErrorsKeepTheHoldAndClearOnRecovery(String code) {
|
||||
when(provider.poll("provider-vast","upstream-task"))
|
||||
.thenThrow(new ApiException(code,"private upstream details",502))
|
||||
.thenReturn(Map.of("data",Map.of("status","running","progress",75)));
|
||||
poll();
|
||||
assertEquals("running",status());
|
||||
assertEquals(new BigDecimal("1.44000000"),wallet("reserved_balance"));
|
||||
assertEquals(new BigDecimal("10.00000000"),wallet("balance"));
|
||||
assertNull(cost());
|
||||
String error = jdbc.queryForObject("SELECT error::text FROM model_tasks WHERE id=?",String.class,taskId);
|
||||
assertTrue(error.contains("UPSTREAM_TASK_POLL_RETRY"));
|
||||
assertFalse(error.contains("private upstream details"));
|
||||
poll();
|
||||
assertEquals("running",status());
|
||||
assertEquals(75,jdbc.queryForObject("SELECT progress FROM model_tasks WHERE id=?",Integer.class,taskId));
|
||||
assertNull(jdbc.queryForObject("SELECT error::text FROM model_tasks WHERE id=?",String.class,taskId));
|
||||
assertNull(jdbc.queryForObject("SELECT error_code FROM usage_events WHERE provider_task_id=?",String.class,taskId));
|
||||
}
|
||||
|
||||
@Test void deadlineReleasesHoldWithoutClaimingTheUpstreamFailed() {
|
||||
jdbc.update("UPDATE model_tasks SET provider_deadline_at=now()-interval '1 second' WHERE id=?",taskId);
|
||||
poll(); poll();
|
||||
assertEquals("failed",status());
|
||||
assertEquals(new BigDecimal("10.00000000"),wallet("balance"));
|
||||
assertEquals(0,wallet("reserved_balance").signum());
|
||||
assertEquals(0,cost().signum());
|
||||
String error = jdbc.queryForObject("SELECT error::text FROM model_tasks WHERE id=?",String.class,taskId);
|
||||
assertTrue(error.contains("UPSTREAM_TASK_TIMEOUT"));
|
||||
assertTrue(error.contains("执行结果未知"));
|
||||
verifyNoInteractions(provider);
|
||||
}
|
||||
|
||||
@Test void confirmedUpstreamFailureReleasesHoldImmediately() {
|
||||
when(provider.poll("provider-vast","upstream-task")).thenReturn(Map.of("data",Map.of("status","failed")));
|
||||
poll();
|
||||
assertEquals("failed",status());
|
||||
assertEquals(0,wallet("reserved_balance").signum());
|
||||
assertEquals(new BigDecimal("10.00000000"),wallet("balance"));
|
||||
assertEquals(0,cost().signum());
|
||||
}
|
||||
|
||||
@Test void migrationBackfillsOldAcceptedTasksAndNewSubmissionsUseConfiguredDeadline() {
|
||||
OffsetDateTime createdAt = jdbc.queryForObject("SELECT created_at FROM model_tasks WHERE id=?",OffsetDateTime.class,taskId);
|
||||
OffsetDateTime migratedDeadline = jdbc.queryForObject("SELECT provider_deadline_at FROM model_tasks WHERE id=?",OffsetDateTime.class,taskId);
|
||||
assertEquals(Duration.ofHours(1),Duration.between(createdAt,migratedDeadline));
|
||||
jdbc.update("UPDATE model_tasks SET status='queued',provider_task_id=NULL,provider_deadline_at=NULL WHERE id=?",taskId);
|
||||
when(provider.submit(anyString(),anyString(),anyString(),anyString())).thenReturn(Map.of("data",Map.of("task_id","upstream-task")));
|
||||
tasks.completeMockTasks();
|
||||
OffsetDateTime submittedAt = jdbc.queryForObject("SELECT updated_at FROM model_tasks WHERE id=?",OffsetDateTime.class,taskId);
|
||||
OffsetDateTime deadline = jdbc.queryForObject("SELECT provider_deadline_at FROM model_tasks WHERE id=?",OffsetDateTime.class,taskId);
|
||||
assertEquals(Duration.ofMinutes(2),Duration.between(submittedAt,deadline));
|
||||
tasks.completeMockTasks();
|
||||
verify(provider,times(1)).submit(anyString(),anyString(),anyString(),anyString());
|
||||
verify(provider,never()).poll(anyString(),anyString());
|
||||
assertEquals("running",status());
|
||||
}
|
||||
}
|
||||
+83
@@ -0,0 +1,83 @@
|
||||
package com.moyi.controlplane.usage;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.moyi.controlplane.auth.AuthService;
|
||||
import com.moyi.controlplane.gateway.GatewayGuard;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import org.flywaydb.core.Flyway;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.jdbc.datasource.DriverManagerDataSource;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.ArgumentMatchers.anyList;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
@EnabledIfEnvironmentVariable(named="MOYI_AUTH_TEST_JDBC_URL", matches="jdbc:postgresql://127\\.0\\.0\\.1:[0-9]+/.+")
|
||||
class UsageCapacityPostgresTest {
|
||||
private JdbcTemplate jdbc;
|
||||
private JdbcTemplate base;
|
||||
private String schema;
|
||||
|
||||
@BeforeEach void setup() {
|
||||
String url = System.getenv("MOYI_AUTH_TEST_JDBC_URL");
|
||||
schema = "capacity_test_" + UUID.randomUUID().toString().replace("-", "");
|
||||
var source = new DriverManagerDataSource(url, "moyi_role_test", "");
|
||||
base = new JdbcTemplate(source);
|
||||
Flyway.configure().dataSource(source).schemas(schema).defaultSchema(schema).load().migrate();
|
||||
jdbc = new JdbcTemplate(new DriverManagerDataSource(url + "?currentSchema=" + schema, "moyi_role_test", ""));
|
||||
jdbc.update("UPDATE model_providers SET status='disabled'");
|
||||
jdbc.update("INSERT INTO model_providers(id,code,name,protocol,status) VALUES ('capacity-provider','capacity-provider','Capacity provider','http','active')");
|
||||
jdbc.update("INSERT INTO model_definitions(id,public_name,model_type,call_mode) VALUES ('capacity-model','Capacity model','text','sync')");
|
||||
jdbc.update("INSERT INTO model_catalog(id,provider_id,logical_model_id,public_name,source_label,upstream_model,model_type,call_mode) SELECT 'capacity-route-'||n,'capacity-provider','capacity-model','Capacity model','Group '||n,'test','text','sync' FROM generate_series(1,105) n");
|
||||
jdbc.update("UPDATE model_catalog SET call_mode='async',model_type='3d' WHERE id='capacity-route-105'");
|
||||
UUID user = UUID.randomUUID(), key = UUID.randomUUID();
|
||||
jdbc.update("INSERT INTO users(id,account,account_kind,password_hash,display_name) VALUES (?,'capacity-user','username','not-loginable','Capacity user')", user);
|
||||
jdbc.update("INSERT INTO platform_api_keys(id,user_id,name,key_prefix,key_hash) VALUES (?,?,'Capacity test','local-test',?)", key, user, "c".repeat(64));
|
||||
for (String state : List.of("running", "queued", "succeeded")) {
|
||||
jdbc.update("INSERT INTO model_tasks(id,user_id,api_key_id,model_id,task_type,status,created_at) VALUES (?,?,?,'capacity-route-105','3d',?,now()-interval '1 day')", "task-" + state, user, key, state);
|
||||
}
|
||||
}
|
||||
|
||||
@AfterEach void cleanup() {
|
||||
if (base != null && schema != null) base.execute("DROP SCHEMA " + schema + " CASCADE");
|
||||
}
|
||||
|
||||
@Test void liveCapacitySeparatesQueuedWorkAndKeepsEveryGroupAndLongRunningTask() {
|
||||
GatewayGuard guard = mock(GatewayGuard.class);
|
||||
when(guard.defaultModelConcurrency()).thenReturn(7);
|
||||
when(guard.defaultKeyConcurrency()).thenReturn(3);
|
||||
when(guard.queueWaitMs()).thenReturn(4500L);
|
||||
when(guard.capacitySnapshot(anyList())).thenAnswer(invocation -> {
|
||||
List<String> ids = invocation.getArgument(0);
|
||||
Map<String, GatewayGuard.Capacity> states = new LinkedHashMap<>();
|
||||
ids.forEach(id -> states.put(id, new GatewayGuard.Capacity(id.equals("capacity-route-1") ? 2 : 0, id.equals("capacity-route-1") ? 3 : 0)));
|
||||
return new GatewayGuard.CapacitySnapshot(true, states);
|
||||
});
|
||||
var service = new UsageAuditService(jdbc, new ObjectMapper(), guard, 77, 2500);
|
||||
Map<String, Object> summary = service.adminSummary("test", mock(AuthService.class));
|
||||
List<Map<String, Object>> groups = (List<Map<String, Object>>) summary.get("capacityGroups");
|
||||
assertEquals(105, groups.size(), "Capacity listing must not truncate at 100 groups");
|
||||
Map<String, Object> async = groups.stream().filter(group -> group.get("id").equals("capacity-route-105")).findFirst().orElseThrow();
|
||||
assertEquals(1L, async.get("running"), "A task older than ten minutes must remain in live capacity");
|
||||
assertEquals(1L, async.get("queued"), "Running tasks must not also count as queued");
|
||||
assertEquals(7, async.get("limit"), "Null route limits use the real configured model default");
|
||||
assertEquals(3L, summary.get("running"));
|
||||
assertEquals(4L, summary.get("waiting"));
|
||||
Map<?, ?> config = (Map<?, ?>) summary.get("capacityConfig");
|
||||
assertEquals(4500L, config.get("queueWaitMs"));
|
||||
assertEquals(3, config.get("defaultKeyConcurrency"));
|
||||
assertEquals(77, config.get("asyncQueueLimit"));
|
||||
assertEquals(2500L, config.get("asyncWorkerIntervalMs"));
|
||||
|
||||
when(guard.capacitySnapshot(anyList())).thenReturn(new GatewayGuard.CapacitySnapshot(false, Map.of()));
|
||||
var unavailable = service.adminSummary("test", mock(AuthService.class));
|
||||
assertNull(unavailable.get("running"), "Unavailable Redis must not be reported as zero load");
|
||||
assertNull(unavailable.get("waiting"));
|
||||
}
|
||||
}
|
||||
@@ -46,6 +46,9 @@ GATEWAY_DEFAULT_MODEL_CONCURRENCY=4
|
||||
GATEWAY_DEFAULT_KEY_CONCURRENCY=4
|
||||
TASK_MAX_QUEUED_PER_MODEL=100
|
||||
MOYI_TASK_WORKER_INTERVAL_MS=1000
|
||||
# Accepted async tasks retry polling for up to this duration, then release
|
||||
# their wallet hold with an unknown-result timeout (upstream is not cancelled).
|
||||
MOYI_TASK_PROVIDER_TIMEOUT_MS=3600000
|
||||
|
||||
# Registration requires a valid invitation AND an email/SMS verification code.
|
||||
# Unconfigured channels fail closed; admins can still create user accounts.
|
||||
|
||||
@@ -52,8 +52,14 @@ def updated_environment(original, account, password):
|
||||
require(password.strip() and not any(ord(char) < 32 or ord(char) == 127 for char in password),
|
||||
"Password must be nonblank and cannot contain control characters.")
|
||||
require(password != "admin123", "The development default password is not allowed.")
|
||||
# Append effective overrides, then ask Compose to validate them. Do not parse
|
||||
# or rewrite arbitrary existing dotenv values, which may be multiline/quoted.
|
||||
# Replace the two owner settings in place before appending the effective
|
||||
# values. Compose accepts later overrides, but duplicate assignments make
|
||||
# the resulting dotenv file ambiguous and can silently mask a real secret.
|
||||
# Do not parse or rewrite arbitrary existing dotenv values, which may be
|
||||
# multiline/quoted.
|
||||
lines = [line for line in original.splitlines() if not re.match(
|
||||
r"^\s*(?:export\s+)?(?:ADMIN_ACCOUNT|ADMIN_PASSWORD)\s*=", line)]
|
||||
original = "\n".join(lines)
|
||||
values = {"ADMIN_ACCOUNT": account, "ADMIN_PASSWORD": password}
|
||||
return original.rstrip("\r\n") + "\n\n# Owner credentials set by admin_credentials.py\n" + "".join(
|
||||
key + "=" + json.dumps(value, ensure_ascii=False).replace("$", "$$") + "\n"
|
||||
|
||||
@@ -66,6 +66,16 @@ def load_config(env_file, mode):
|
||||
fail("Environment file missing. Run: python3 deploy.py init")
|
||||
if os.name != "nt" and stat.S_IMODE(env_file.stat().st_mode) & 0o077:
|
||||
fail("Environment file must be private. Run chmod 600 on it.")
|
||||
# A later empty assignment silently overrides an earlier valid secret in
|
||||
# Compose. Reject duplicates before resolving or starting any container.
|
||||
seen = set()
|
||||
for line in env_file.read_text(encoding="utf-8-sig").splitlines():
|
||||
match = re.match(r"^\s*(?:export\s+)?([A-Za-z_][A-Za-z0-9_]*)\s*=", line)
|
||||
if match:
|
||||
key = match.group(1)
|
||||
if key in seen:
|
||||
fail("Duplicate environment setting: " + key + ". Keep exactly one assignment; values are not printed.")
|
||||
seen.add(key)
|
||||
config = execute(compose_command(env_file, mode) + ["config", "--format", "json"], capture=True)
|
||||
resolved = json.loads(config)
|
||||
# Compose serializes literal '$' as '$$' for re-parseable config output.
|
||||
|
||||
@@ -52,6 +52,7 @@ services:
|
||||
GATEWAY_QUEUE_POLL_MS: ${GATEWAY_QUEUE_POLL_MS:-100}
|
||||
TASK_MAX_QUEUED_PER_MODEL: ${TASK_MAX_QUEUED_PER_MODEL:-100}
|
||||
MOYI_TASK_WORKER_INTERVAL_MS: ${MOYI_TASK_WORKER_INTERVAL_MS:-1000}
|
||||
MOYI_TASK_PROVIDER_TIMEOUT_MS: ${MOYI_TASK_PROVIDER_TIMEOUT_MS:-3600000}
|
||||
DB_POOL_MAX_SIZE: ${DB_POOL_MAX_SIZE:-10}
|
||||
DB_POOL_MIN_IDLE: ${DB_POOL_MIN_IDLE:-2}
|
||||
JAVA_OPTS: ${JAVA_OPTS:--Xms256m -Xmx1536m -XX:+ExitOnOutOfMemoryError}
|
||||
|
||||
@@ -37,6 +37,25 @@ def configuration():
|
||||
|
||||
|
||||
class DeploymentTests(unittest.TestCase):
|
||||
def test_duplicate_empty_secret_is_rejected_before_compose(self):
|
||||
with tempfile.TemporaryDirectory() as directory:
|
||||
env_file = Path(directory) / ".env.cloud"
|
||||
env_file.write_text("TRIPO_API_KEY=private-test-value\nTRIPO_API_KEY=\n", encoding="utf-8")
|
||||
env_file.chmod(0o600)
|
||||
with patch.object(deploy, "execute") as execute:
|
||||
with self.assertRaisesRegex(ValueError, "Duplicate environment setting: TRIPO_API_KEY") as error:
|
||||
deploy.load_config(env_file, "https")
|
||||
self.assertNotIn("private-test-value", str(error.exception))
|
||||
execute.assert_not_called()
|
||||
|
||||
def test_commented_assignment_does_not_override_secret(self):
|
||||
with tempfile.TemporaryDirectory() as directory:
|
||||
env_file = Path(directory) / ".env.cloud"
|
||||
env_file.write_text("# TRIPO_API_KEY=\nTRIPO_API_KEY=private-test-value\n", encoding="utf-8")
|
||||
env_file.chmod(0o600)
|
||||
with patch.object(deploy, "execute", return_value=json.dumps(configuration())):
|
||||
self.assertEqual(deploy.load_config(env_file, "https"), configuration())
|
||||
|
||||
def test_proxy_replaces_forwarding_headers_for_verification_limits(self):
|
||||
routes = (DIRECTORY / "nginx" / "routes.conf").read_text(encoding="utf-8")
|
||||
self.assertEqual(routes.count('proxy_set_header Forwarded "";'), 2)
|
||||
|
||||
@@ -162,11 +162,20 @@ export interface UsageAuditPage {
|
||||
export interface AdminUsageSummary {
|
||||
callsToday: number
|
||||
callsThisMonth: number
|
||||
waiting: number
|
||||
waiting: number | null
|
||||
costToday: number
|
||||
costThisMonth: number
|
||||
series: Array<{ date: string; count: number }>
|
||||
capacityGroups: Array<{ id: string; name: string; running: number; limit: number; queued: number }>
|
||||
capacityGroups: Array<{ id: string; name: string; sourceLabel?: string; providerName?: string; mode?: string; routeEnabled?: boolean; running: number | null; limit: number; queued: number | null }>
|
||||
running?: number | null
|
||||
capacityConfig?: {
|
||||
queueWaitMs: number
|
||||
defaultModelConcurrency: number
|
||||
defaultKeyConcurrency: number
|
||||
asyncQueueLimit: number
|
||||
asyncWorkerIntervalMs: number
|
||||
synchronousSnapshotAvailable: boolean
|
||||
}
|
||||
}
|
||||
|
||||
export interface AdminCatalogUser {
|
||||
@@ -207,7 +216,7 @@ export interface AdminInvitation {
|
||||
export interface ChatCompletionResult {
|
||||
id: string
|
||||
model: string
|
||||
choices: Array<{ message: { role: string; content: string }; finish_reason: string }>
|
||||
choices: Array<{ message: { role: string; content?: string | null; reasoning_content?: string; reasoning?: string; [key: string]: unknown }; finish_reason?: string | null }>
|
||||
usage?: { prompt_tokens: number; completion_tokens: number; total_tokens: number }
|
||||
}
|
||||
|
||||
|
||||
@@ -1,4 +1,59 @@
|
||||
import type { CatalogModel, LogicalModel } from './catalog'
|
||||
import type { CatalogModel, CatalogProvider, LogicalModel } from './catalog'
|
||||
|
||||
export interface ModelCatalogSummary {
|
||||
publishedModels: number
|
||||
readyModels: number
|
||||
readyRoutes: number
|
||||
unpricedRoutes: number
|
||||
}
|
||||
|
||||
const capabilityLabels: Record<string, string> = {
|
||||
text: '文本',
|
||||
code: '代码',
|
||||
multimodal: '多模态',
|
||||
image: '图像',
|
||||
audio: '音频',
|
||||
video: '视频',
|
||||
embedding: '向量',
|
||||
'3d': '3D',
|
||||
}
|
||||
|
||||
/**
|
||||
* Build the compact capability text shown for a provider in the admin overview.
|
||||
* A provider can expose hundreds of route rows, so this must deduplicate by
|
||||
* capability and keep the rendered label bounded for the summary card.
|
||||
*/
|
||||
export function providerCapabilityLabel(types: string[]): string {
|
||||
const labels = [...new Set(types.map(type => capabilityLabels[type.trim().toLowerCase()] || '其他').filter(Boolean))]
|
||||
if (!labels.length) return '待配置能力'
|
||||
if (labels.length <= 3) return labels.join(' · ')
|
||||
return `${labels.slice(0, 3).join(' · ')} 等 ${labels.length} 类`
|
||||
}
|
||||
|
||||
export function summarizeModelCatalog(
|
||||
definitions: LogicalModel[],
|
||||
routes: CatalogModel[],
|
||||
providers: CatalogProvider[],
|
||||
): ModelCatalogSummary {
|
||||
const publishedModelIds = new Set(definitions.filter(model => model.status === 'active').map(model => model.id))
|
||||
const activeProviderIds = new Set(providers.filter(provider => provider.status === 'active').map(provider => provider.id))
|
||||
const readyModelIds = new Set<string>()
|
||||
let readyRoutes = 0
|
||||
let unpricedRoutes = 0
|
||||
|
||||
for (const route of routes) {
|
||||
if (route.status !== 'active' || !publishedModelIds.has(route.logicalModelId) || !activeProviderIds.has(route.providerId)) continue
|
||||
if (route.pricingConfigured !== true) {
|
||||
unpricedRoutes++
|
||||
continue
|
||||
}
|
||||
if (route.billingReady === false || route.executionReady === false) continue
|
||||
readyRoutes++
|
||||
readyModelIds.add(route.logicalModelId)
|
||||
}
|
||||
|
||||
return { publishedModels: publishedModelIds.size, readyModels: readyModelIds.size, readyRoutes, unpricedRoutes }
|
||||
}
|
||||
|
||||
// Browse published definitions even when none of their routes are enabled yet.
|
||||
// Route IDs remain the exact authorization and invocation identifiers.
|
||||
|
||||
@@ -39,6 +39,9 @@ import SlidePanel from '../components/SlidePanel.vue'
|
||||
import UiModal from '../components/UiModal.vue'
|
||||
import CatalogAdmin from '../components/CatalogAdmin.vue'
|
||||
import AccountAdmin from '../components/AccountAdmin.vue'
|
||||
import { providerCapabilityLabel, summarizeModelCatalog } from '../services/model-catalog'
|
||||
import { usePagination } from '../composables/usePagination'
|
||||
import ListPagination from '../components/ListPagination.vue'
|
||||
|
||||
const props = defineProps<{ view: string; currentUser?: AuthUser }>()
|
||||
const emit = defineEmits<{ navigate: [view: string]; notify: [message: string] }>()
|
||||
@@ -47,6 +50,7 @@ const users = ref<AdminUser[]>([])
|
||||
const providers = ref<ProviderItem[]>([])
|
||||
const models = ref<ModelItem[]>([])
|
||||
const publicModelCount = ref(0)
|
||||
const publishedModelCount = ref(0)
|
||||
const publicRouteCount = ref(0)
|
||||
const unpricedRouteCount = ref(0)
|
||||
const adminSummary = ref<AdminUsageSummary | null>(null)
|
||||
@@ -84,12 +88,11 @@ async function refreshOverviewData(notifyOnSuccess = false) {
|
||||
])
|
||||
providers.value = mapProviders(providerResult.providers, modelResult.models)
|
||||
models.value = mapModels(modelResult.models, providerResult.providers)
|
||||
const activeProviders = new Set(providerResult.providers.filter((item) => item.status === 'active').map((item) => item.id))
|
||||
const activeDefinitions = new Set(modelResult.logicalModels.filter((item) => item.status === 'active').map((item) => item.id))
|
||||
const publishedRoutes = modelResult.models.filter((item) => item.status === 'active' && activeProviders.has(item.providerId) && activeDefinitions.has(item.logicalModelId) && item.pricingConfigured && item.billingReady !== false && item.executionReady !== false)
|
||||
publicRouteCount.value = publishedRoutes.length
|
||||
publicModelCount.value = new Set(publishedRoutes.map((item) => item.logicalModelId)).size
|
||||
unpricedRouteCount.value = modelResult.models.filter((item) => item.status === 'active' && activeProviders.has(item.providerId) && activeDefinitions.has(item.logicalModelId) && !item.pricingConfigured).length
|
||||
const catalogSummary = summarizeModelCatalog(modelResult.logicalModels, modelResult.models, providerResult.providers)
|
||||
publishedModelCount.value = catalogSummary.publishedModels
|
||||
publicRouteCount.value = catalogSummary.readyRoutes
|
||||
publicModelCount.value = catalogSummary.readyModels
|
||||
unpricedRouteCount.value = catalogSummary.unpricedRoutes
|
||||
adminSummary.value = summaryResult
|
||||
verificationChannels.value = verificationResult
|
||||
users.value = userResult.users.map(mapUser)
|
||||
@@ -194,10 +197,12 @@ function formatUsagePayload(value: unknown) {
|
||||
|
||||
const queueGroups = computed(() => (adminSummary.value?.capacityGroups || []).map((group) => ({
|
||||
...group,
|
||||
type: '模型分组',
|
||||
wait: group.queued ? '排队中' : '—',
|
||||
status: group.queued ? '保护中' : '正常',
|
||||
type: `${group.providerName || '未知渠道'} · ${group.sourceLabel || '模型分组'}`,
|
||||
status: group.running === null ? '未读取' : group.routeEnabled === false ? '已停用,仍有任务' : group.queued ? '排队中' : group.running ? '执行中' : '空闲',
|
||||
warning: group.running === null || group.routeEnabled === false || !!group.queued,
|
||||
})))
|
||||
const { page: capacityPage, pageSize: capacityPageSize, pagedItems: capacityPageGroups } = usePagination(queueGroups)
|
||||
const configuredCapacity = computed(() => adminSummary.value?.capacityGroups.filter(group => group.routeEnabled !== false).reduce((total, group) => total + group.limit, 0))
|
||||
const pricing = computed(() => models.value.filter((model) => model.pricingConfigured).map((model) => ({
|
||||
model: model.name,
|
||||
unit: model.pricing?.unit || 'CNY / 1M tokens',
|
||||
@@ -227,7 +232,7 @@ function mapProviders(items: CatalogProvider[], modelItems: CatalogModel[]): Pro
|
||||
return items.map((provider) => ({
|
||||
id: provider.id,
|
||||
name: provider.name,
|
||||
capability: modelItems.filter((model) => model.providerId === provider.id).map((model) => model.type).join(' / ') || '待配置能力',
|
||||
capability: providerCapabilityLabel(modelItems.filter((model) => model.providerId === provider.id).map((model) => model.type)),
|
||||
models: modelItems.filter((model) => model.providerId === provider.id).length,
|
||||
latency: '未采样',
|
||||
successRate: '—',
|
||||
@@ -277,7 +282,7 @@ function mapModels(items: CatalogModel[], providersList: CatalogProvider[]): Mod
|
||||
<div class="page-stack admin-pages">
|
||||
<template v-if="view === 'admin-overview'">
|
||||
<section class="page-heading compact"><div><span>ADMIN OVERVIEW</span><h1>管理总览</h1><p>掌握平台供给、用户、容量和风险状态。</p></div><div class="heading-actions"><span class="health-badge"><i></i>目录数据已读取</span><button class="button secondary" :disabled="overviewRefreshing" @click="refreshOverviewData(true)"><Activity :size="16" />{{ overviewRefreshing ? '刷新中…' : '刷新数据' }}</button></div></section>
|
||||
<section class="metric-grid"><article class="metric-card"><span class="icon-tile violet"><Users :size="18" /></span><div><small>用户总数</small><strong>{{ users.length }}</strong><em>来自认证服务</em></div></article><article class="metric-card"><span class="icon-tile cyan"><Cloud :size="18" /></span><div><small>用户端可用模型</small><strong>{{ publicModelCount }}</strong><em>{{ publicRouteCount }} 个已定价分组 · 管理目录 {{ models.length }} 条 · 待定价 {{ unpricedRouteCount }} 个</em></div></article><article class="metric-card"><span class="icon-tile orange"><Gauge :size="18" /></span><div><small>等待 / 执行任务</small><strong>{{ adminSummary?.waiting ?? '—' }}</strong><em>来自最近调用记录</em></div></article><article class="metric-card"><span class="icon-tile green"><CircleDollarSign :size="18" /></span><div><small>今日额度消耗</small><strong>{{ adminSummary ? `¥${Number(adminSummary.costToday || 0).toFixed(4)}` : '—' }}</strong><em>成功调用实际扣费</em></div></article></section>
|
||||
<section class="metric-grid"><article class="metric-card"><span class="icon-tile violet"><Users :size="18" /></span><div><small>用户总数</small><strong>{{ users.length }}</strong><em>来自认证服务</em></div></article><article class="metric-card"><span class="icon-tile cyan"><Cloud :size="18" /></span><div><small>已完成接入配置的模型</small><strong>{{ publicModelCount }}</strong><em>模型广场显示 {{ publishedModelCount }} 个 · {{ publicRouteCount }} 个就绪分组 · 待定价 {{ unpricedRouteCount }} 个</em></div></article><article class="metric-card"><span class="icon-tile orange"><Gauge :size="18" /></span><div><small>等待 / 执行任务</small><strong>{{ adminSummary?.waiting ?? '—' }} / {{ adminSummary?.running ?? '—' }}</strong><em>Redis 租约与异步任务实时状态</em></div></article><article class="metric-card"><span class="icon-tile green"><CircleDollarSign :size="18" /></span><div><small>今日额度消耗</small><strong>{{ adminSummary ? `¥${Number(adminSummary.costToday || 0).toFixed(4)}` : '—' }}</strong><em>成功调用实际扣费</em></div></article></section>
|
||||
<section class="two-column equal"><article class="panel chart-panel"><header class="panel-header"><div><span>PLATFORM TRAFFIC</span><h3>平台请求趋势</h3></div><span class="select-button static">最近 7 天</span></header><div v-if="adminSummary" class="traffic-list"><div v-for="point in adminSummary.series" :key="point.date"><span>{{ point.date }}</span><strong>{{ point.count }} 次</strong></div></div><div v-else class="empty-state compact"><BarChart3 :size="22" /><strong>正在加载平台流量</strong><span>刷新后显示最近 7 天调用量</span></div></article><article class="panel health-panel"><header class="panel-header"><div><span>GATEWAY STATUS</span><h3>模型供给状态</h3></div><button class="text-link" @click="emit('navigate', 'admin-routing')">管理渠道<ArrowRight :size="14" /></button></header><div v-if="providers.length" class="health-list"><div v-for="provider in providers.slice(0, 3)" :key="provider.id"><span class="provider-mark">{{ provider.name.slice(0, 1) }}</span><p><strong>{{ provider.name }}</strong><small>{{ provider.capability }} · {{ provider.latency }}</small></p><b>{{ provider.successRate }}</b><span class="status-chip" :class="provider.status === '已启用' ? 'success' : 'warning'"><i></i>{{ provider.status }}</span></div></div><div v-else class="empty-state compact"><Cloud :size="22" /><strong>暂无供应商</strong><span>由管理员登记上游渠道后显示</span></div></article></section>
|
||||
<section class="two-column narrow-left"><article class="panel quick-credit"><header class="panel-header"><div><span>CREDIT OPERATIONS</span><h3>额度分配</h3></div><ShieldCheck :size="18" /></header><p>人民币额度余额与发放流水</p><button class="button primary full" @click="emit('navigate', 'admin-users')"><Users :size="16" />查看用户</button></article><article class="panel activity-panel"><header class="panel-header"><div><span>RECENT EVENTS</span><h3>最近运营事件</h3></div><button class="text-link" @click="emit('navigate', 'admin-audit')">审计日志<ArrowRight :size="14" /></button></header><div v-if="auditEvents.length" class="activity-list"><div v-for="event in auditEvents.slice(0, 3)" :key="`${event.time}-${event.action}`"><span class="activity-dot cyan"><ShieldCheck :size="14" /></span><p><strong>{{ event.action }}</strong><small>{{ event.actor }} · {{ event.target }}</small></p><time>{{ event.time }}</time></div></div><div v-else class="empty-state compact"><ShieldCheck :size="22" /><strong>暂无运营事件</strong><span>真实管理操作发生后会显示审计记录</span></div></article></section>
|
||||
</template>
|
||||
@@ -286,9 +291,15 @@ function mapModels(items: CatalogModel[], providersList: CatalogProvider[]): Mod
|
||||
<CatalogAdmin v-else-if="view === 'admin-models' || view === 'admin-routing'" :section="view === 'admin-routing' ? 'channels' : 'models'" @notify="emit('notify', $event)" />
|
||||
|
||||
<template v-else-if="view === 'admin-capacity'">
|
||||
<section class="page-heading"><div><span>CAPACITY CONTROL</span><h1>并发与队列</h1><p>显示最近调用记录中的运行、排队数量,以及每个模型分组的配置上限。</p></div><div class="heading-actions"><span class="admin-badge"><ShieldCheck :size="15" />队列保护已启用</span><button class="button secondary" @click="refreshOverviewData(true)"><RefreshCcw :size="15" />刷新监控</button></div></section>
|
||||
<section class="capacity-grid"><article class="capacity-main"><div class="capacity-ring"><strong>{{ adminSummary?.capacityGroups.reduce((total, group) => total + group.running, 0) ?? '—' }}</strong><span>运行中</span></div><div><small>CONFIGURED CAPACITY</small><h2>{{ adminSummary?.capacityGroups.reduce((total, group) => total + group.limit, 0) ?? '—' }} 个分组上限</h2><p>同步请求超过上限时会在平台内排队,超出等待窗口后返回 429。</p></div></article><article class="metric-card"><span class="icon-tile orange"><Clock3 :size="18" /></span><div><small>等待任务</small><strong>{{ adminSummary?.waiting ?? '—' }}</strong><em>queued / running</em></div></article><article class="metric-card"><span class="icon-tile cyan"><Timer :size="18" /></span><div><small>排队窗口</small><strong>30s</strong><em>环境默认值</em></div></article><article class="metric-card"><span class="icon-tile green"><CheckCircle2 :size="18" /></span><div><small>调度状态</small><strong>已启用</strong><em>数据库队列与 Redis 占用</em></div></article></section>
|
||||
<section class="panel table-card"><header class="panel-header"><div><span>CAPACITY GROUPS</span><h3>并发与排队状态</h3></div><span class="select-button static">最近 10 分钟</span></header><div class="responsive-table"><table><thead><tr><th>资源组</th><th>运行中</th><th>并发上限</th><th>排队数</th><th>预计等待</th><th>状态</th></tr></thead><tbody><tr v-for="group in queueGroups" :key="group.id"><td><strong>{{ group.name }}</strong><small>{{ group.type }}</small></td><td>{{ group.running }}</td><td>{{ group.limit || '默认' }}</td><td>{{ group.queued }}</td><td>{{ group.wait }}</td><td><span class="status-chip" :class="group.status === '正常' ? 'success' : 'warning'"><i></i>{{ group.status }}</span></td></tr><tr v-if="!queueGroups.length"><td colspan="6"><div class="empty-state compact"><Gauge :size="22" /><strong>暂无活跃模型分组</strong><span>启用并定价的模型调用后会显示容量状态</span></div></td></tr></tbody></table></div></section>
|
||||
<section class="page-heading"><div><span>CAPACITY CONTROL</span><h1>并发与队列</h1><p>显示实时 Redis 租约、异步任务队列和每个模型分组的配置上限。</p></div><div class="heading-actions"><span class="admin-badge"><ShieldCheck :size="15" />{{ adminSummary?.capacityConfig ? '运行配置已读取' : '运行配置未读取' }}</span><button class="button secondary" @click="refreshOverviewData(true)"><RefreshCcw :size="15" />刷新监控</button></div></section>
|
||||
<section class="capacity-grid">
|
||||
<article class="capacity-main"><div class="capacity-ring"><strong>{{ adminSummary?.running ?? '—' }}</strong><span>实时运行中</span></div><div><small>CONFIGURED CAPACITY</small><h2>{{ configuredCapacity ?? '—' }} 个配置槽位</h2><p>启用分组的上限合计;上游共享限额仍会影响实际吞吐。</p></div></article>
|
||||
<article class="metric-card"><span class="icon-tile orange"><Clock3 :size="18" /></span><div><small>等待任务</small><strong>{{ adminSummary?.waiting ?? '—' }}</strong><em>不含正在执行的请求</em></div></article>
|
||||
<article class="metric-card"><span class="icon-tile cyan"><Timer :size="18" /></span><div><small>同步排队窗口</small><strong>{{ adminSummary?.capacityConfig ? `${adminSummary.capacityConfig.queueWaitMs / 1000}s` : '—' }}</strong><em>读取当前服务器配置</em></div></article>
|
||||
<article class="metric-card"><span class="icon-tile green"><CheckCircle2 :size="18" /></span><div><small>同步占用快照</small><strong>{{ adminSummary?.capacityConfig?.synchronousSnapshotAvailable ? '已读取' : '未读取' }}</strong><em>来自 Redis 有效租约</em></div></article>
|
||||
</section>
|
||||
<p v-if="adminSummary?.capacityConfig" class="billing-note">单 Key 并发上限 {{ adminSummary.capacityConfig.defaultKeyConcurrency }};未单独设置的模型分组默认上限 {{ adminSummary.capacityConfig.defaultModelConcurrency }}。每组异步等待与执行总量上限 {{ adminSummary.capacityConfig.asyncQueueLimit }},任务扫描间隔 {{ adminSummary.capacityConfig.asyncWorkerIntervalMs / 1000 }} 秒。同步与异步分别计数。</p>
|
||||
<section class="panel table-card"><header class="panel-header"><div><span>CAPACITY GROUPS</span><h3>并发与排队状态</h3></div><span class="select-button static">{{ queueGroups.length }} 个分组 · 实时快照</span></header><div class="responsive-table"><table><thead><tr><th>资源组</th><th>运行中</th><th>并发上限</th><th>排队数</th><th>计数来源</th><th>状态</th></tr></thead><tbody><tr v-for="group in capacityPageGroups" :key="group.id"><td><div class="stacked-cell"><strong>{{ group.name }}</strong><small>{{ group.type }}</small></div></td><td>{{ group.running ?? '—' }}</td><td>{{ group.limit }}</td><td>{{ group.queued ?? '—' }}</td><td>{{ group.mode === 'async' ? '异步任务队列' : 'Redis 有效租约' }}</td><td><span class="status-chip" :class="group.warning ? 'warning' : 'success'"><i></i>{{ group.status }}</span></td></tr><tr v-if="!queueGroups.length"><td colspan="6"><div class="empty-state compact"><Gauge :size="22" /><strong>{{ adminSummary ? '暂无活跃模型分组' : '容量数据尚未读取' }}</strong><span>启用模型分组后会显示其配置与占用情况</span></div></td></tr></tbody></table></div><ListPagination v-model:page="capacityPage" v-model:page-size="capacityPageSize" :total="queueGroups.length" label="容量分组分页" /></section>
|
||||
</template>
|
||||
|
||||
<template v-else-if="view === 'admin-billing'">
|
||||
|
||||
@@ -76,7 +76,7 @@ const playgroundModels = computed(() => [...new Map(catalogModels.value.map(grou
|
||||
const playgroundGroups = computed(() => catalogModels.value.filter(group => (group.logicalModelId || group.id) === (activeModel.value.logicalModelId || activeModel.value.id)))
|
||||
function selectPlaygroundGroup(id: string) {
|
||||
const group = catalogModels.value.find(item => item.id === id)
|
||||
if (group) { activeModel.value = group; callMode.value = group.mode; response.value = ''; activeTaskId.value = '' }
|
||||
if (group) { activeModel.value = group; callMode.value = group.mode; response.value = ''; reasoningOutput.value = ''; finishReason.value = ''; activeTaskId.value = '' }
|
||||
}
|
||||
function selectPlaygroundModel(id: string) {
|
||||
const group = catalogModels.value.find(item => (item.logicalModelId || item.id) === id)
|
||||
@@ -86,6 +86,7 @@ const prompt = ref('')
|
||||
const running = ref(false)
|
||||
const maxOutputTokens = ref(4096)
|
||||
const reasoningOutput = ref('')
|
||||
const finishReason = ref('')
|
||||
let streamAbort: AbortController | undefined
|
||||
const response = ref('')
|
||||
const responseError = ref('')
|
||||
@@ -103,6 +104,8 @@ function restoreHistory(record: UserUsageRecord) {
|
||||
const text = typeof input?.prompt === 'string' ? input.prompt : messages.filter(message => message?.role === 'user' && typeof message.content === 'string').map(message => message.content).join('\n')
|
||||
prompt.value = text.slice(0, 4000)
|
||||
response.value = ''
|
||||
reasoningOutput.value = ''
|
||||
finishReason.value = ''
|
||||
emit('notify', !group ? '历史文本已载入,原分组已不可用,请重新选择模型和分组' : text.length > 4000 ? '已载入前 4000 字,请检查输入后再调用' : text ? '历史输入已载入,确认配置后可再次调用' : '该记录没有可恢复的纯文本输入,请手动填写')
|
||||
}
|
||||
const selectedTask = ref<TaskItem | null>(null)
|
||||
@@ -290,6 +293,8 @@ function tryModel(model: GalleryModel) {
|
||||
callMode.value = group.mode
|
||||
selectedModel.value = null
|
||||
response.value = ''
|
||||
reasoningOutput.value = ''
|
||||
finishReason.value = ''
|
||||
emit('navigate', 'playground')
|
||||
}
|
||||
|
||||
@@ -309,6 +314,7 @@ async function runModel() {
|
||||
response.value = ''
|
||||
responseError.value = ''
|
||||
reasoningOutput.value = ''
|
||||
finishReason.value = ''
|
||||
const selectedMode = callMode.value
|
||||
try {
|
||||
if (callMode.value === '异步') {
|
||||
@@ -323,7 +329,15 @@ async function runModel() {
|
||||
await catalogClient.playgroundStreamChat(apiKey, { 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.playgroundChatCompletion(apiKey, { model: activeModel.value.id, prompt: prompt.value, maxTokens: maxOutputTokens.value })
|
||||
response.value = result.choices[0]?.message.content || '模型未返回内容'
|
||||
const choice = result.choices[0]
|
||||
const message = choice?.message
|
||||
const reasoning = typeof message?.reasoning_content === 'string' ? message.reasoning_content : typeof message?.reasoning === 'string' ? message.reasoning : ''
|
||||
reasoningOutput.value = reasoning
|
||||
finishReason.value = typeof choice?.finish_reason === 'string' ? choice.finish_reason : ''
|
||||
const content = normalizeAssistantContent(message?.content)
|
||||
// Keep reasoning separate from the final answer. A small output budget
|
||||
// can legitimately end after reasoning; leave that choice to the user.
|
||||
response.value = content || (reasoning ? '模型仅返回了思考过程,未返回最终答案;可调整最大输出 Token 后重试。' : '模型未返回内容')
|
||||
}
|
||||
emit('notify', callMode.value === '异步' ? '异步任务提交成功' : '模型调用完成')
|
||||
} catch (error) {
|
||||
@@ -337,6 +351,16 @@ async function runModel() {
|
||||
}
|
||||
}
|
||||
|
||||
function normalizeAssistantContent(value: unknown): string {
|
||||
if (typeof value === 'string') return value
|
||||
if (!Array.isArray(value)) return ''
|
||||
return value.map((part) => {
|
||||
if (typeof part === 'string') return part
|
||||
if (part && typeof part === 'object' && typeof (part as { text?: unknown }).text === 'string') return (part as { text: string }).text
|
||||
return ''
|
||||
}).join('')
|
||||
}
|
||||
|
||||
function mapAsyncTask(task: AsyncTaskResult, model: ModelItem, taskPrompt?: string): TaskItem {
|
||||
const modelUrl = task.output?.modelUrl || task.result?.modelUrl || (task.provider ? task.result?.url : undefined)
|
||||
const videoUrl = task.output?.videoUrl || task.result?.videoUrl
|
||||
@@ -394,6 +418,11 @@ async function pollTask(taskId: string, apiKey: string) {
|
||||
scheduleTaskPoll(taskId, apiKey, 2500)
|
||||
} else {
|
||||
taskPollers.delete(taskId)
|
||||
// The async endpoint settles or releases the wallet only when the
|
||||
// provider task reaches a terminal state. Refresh the overview here so
|
||||
// the balance card and usage counters reflect that settlement immediately
|
||||
// instead of waiting for a full page reload.
|
||||
await refreshOverview()
|
||||
emit('notify', latest.status === 'succeeded' ? '3D 模型生成完成,可以预览和下载' : '3D 任务未能完成')
|
||||
}
|
||||
} catch (error) {
|
||||
@@ -616,7 +645,7 @@ async function createProject() {
|
||||
</section>
|
||||
<section class="playground-grid">
|
||||
<article class="panel request-panel">
|
||||
<header class="panel-header"><div><span>REQUEST</span><h3>请求配置</h3></div><button class="icon-button" @click="prompt = ''; response = ''; activeTaskId = ''"><RefreshCcw :size="16" /></button></header>
|
||||
<header class="panel-header"><div><span>REQUEST</span><h3>请求配置</h3></div><button class="icon-button" @click="prompt = ''; response = ''; reasoningOutput = ''; finishReason = ''; activeTaskId = ''"><RefreshCcw :size="16" /></button></header>
|
||||
<div class="playground-selectors">
|
||||
<label>模型<select class="text-input" aria-label="调试模型" :value="activeModel.logicalModelId || activeModel.id" :disabled="running || !playgroundModels.length" @change="selectPlaygroundModel(($event.target as HTMLSelectElement).value)"><option v-for="model in playgroundModels" :key="model.logicalModelId || model.id" :value="model.logicalModelId || model.id">{{ model.name }}</option></select></label>
|
||||
<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>
|
||||
@@ -642,7 +671,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 || 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-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<span v-if="finishReason"> · finish_reason: {{ finishReason }}</span></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>
|
||||
|
||||
@@ -1,9 +1,53 @@
|
||||
import { test } from 'node:test'
|
||||
import assert from 'node:assert/strict'
|
||||
import { nextTick, ref } from 'vue'
|
||||
import { buildModelCatalog, setGroupSelection } from '../src/services/model-catalog.ts'
|
||||
import { buildModelCatalog, providerCapabilityLabel, setGroupSelection, summarizeModelCatalog } from '../src/services/model-catalog.ts'
|
||||
import { usePagination } from '../src/composables/usePagination.ts'
|
||||
|
||||
test('catalog summary distinguishes published models, ready models, ready routes, and missing prices', () => {
|
||||
const definitions = [
|
||||
'available', 'legacy-ready', 'no-routes', 'pricing-only', 'billing-only',
|
||||
'execution-only', 'unavailable-provider', 'orphan-provider', 'disabled-route',
|
||||
].map(id => ({ id, status: 'active' }))
|
||||
definitions.push({ id: 'hidden', status: 'disabled' })
|
||||
const providers = [{ id: 'live', status: 'active' }, { id: 'offline', status: 'disabled' }]
|
||||
const route = (id, logicalModelId, overrides = {}) => ({
|
||||
id, logicalModelId, providerId: 'live', status: 'active',
|
||||
pricingConfigured: true, billingReady: true, executionReady: true, ...overrides,
|
||||
})
|
||||
const routes = [
|
||||
route('ready-a', 'available'),
|
||||
route('ready-b', 'available'),
|
||||
route('legacy-ready', 'legacy-ready', { billingReady: undefined, executionReady: undefined }),
|
||||
route('unconfigured', 'available', { pricingConfigured: undefined }),
|
||||
route('unpriced', 'pricing-only', { pricingConfigured: false }),
|
||||
route('billing-blocked', 'billing-only', { billingReady: false }),
|
||||
route('execution-blocked', 'execution-only', { executionReady: false }),
|
||||
route('offline', 'unavailable-provider', { providerId: 'offline' }),
|
||||
route('offline-unpriced', 'unavailable-provider', { providerId: 'offline', pricingConfigured: false }),
|
||||
route('orphan-provider', 'orphan-provider', { providerId: 'missing' }),
|
||||
route('disabled', 'disabled-route', { status: 'disabled' }),
|
||||
route('disabled-unpriced', 'disabled-route', { status: 'disabled', pricingConfigured: false }),
|
||||
route('hidden', 'hidden'),
|
||||
route('hidden-unpriced', 'hidden', { pricingConfigured: false }),
|
||||
route('orphan-definition', 'missing'),
|
||||
]
|
||||
|
||||
assert.deepEqual(summarizeModelCatalog(definitions, routes, providers), {
|
||||
publishedModels: 9, readyModels: 2, readyRoutes: 3, unpricedRoutes: 2,
|
||||
})
|
||||
})
|
||||
|
||||
test('published models remain counted when no routes or providers can serve them', () => {
|
||||
const definitions = [{ id: 'published', status: 'active' }, { id: 'hidden', status: 'disabled' }]
|
||||
assert.deepEqual(summarizeModelCatalog(definitions, [], []), {
|
||||
publishedModels: 1, readyModels: 0, readyRoutes: 0, unpricedRoutes: 0,
|
||||
})
|
||||
assert.deepEqual(summarizeModelCatalog([], [], []), {
|
||||
publishedModels: 0, readyModels: 0, readyRoutes: 0, unpricedRoutes: 0,
|
||||
})
|
||||
})
|
||||
|
||||
test('published models without enabled routes remain visible and duplicate routes are grouped', () => {
|
||||
const definitions = Array.from({ length: 315 }, (_, i) => ({ id: `m${i}`, status: 'active' }))
|
||||
const routes = [{ id: 'a', logicalModelId: 'm0', status: 'active' }, { id: 'b', logicalModelId: 'm0', status: 'active' }, { id: 'c', logicalModelId: 'm1', status: 'disabled' }]
|
||||
@@ -14,6 +58,13 @@ test('published models without enabled routes remain visible and duplicate route
|
||||
assert.equal(catalog.at(-1).definition.id, 'm314')
|
||||
})
|
||||
|
||||
test('provider capability summary deduplicates route types and remains bounded', () => {
|
||||
assert.equal(providerCapabilityLabel(['text', 'text', 'multimodal', '3d']), '文本 · 多模态 · 3D')
|
||||
assert.equal(providerCapabilityLabel(['text', 'code', 'image', 'audio', 'video']), '文本 · 代码 · 图像 等 5 类')
|
||||
assert.equal(providerCapabilityLabel(['unknown', 'unknown']), '其他')
|
||||
assert.equal(providerCapabilityLabel([]), '待配置能力')
|
||||
})
|
||||
|
||||
test('selecting and removing another model preserves existing exact group grants', () => {
|
||||
let grants = ['model-a/relay-1', 'legacy-unavailable']
|
||||
grants = setGroupSelection(grants, ['model-b/relay-2'], true)
|
||||
|
||||
@@ -0,0 +1,306 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Audit every catalog route through the public MOYI API without exporting secrets.
|
||||
|
||||
Inventory is read-only by default. --run-calls explicitly creates one temporary
|
||||
platform key, authorizes ready chat routes, makes bounded billed requests, then
|
||||
disables/deletes only that key. An upstream HTTP 200 is recorded separately from
|
||||
a nonempty completion. Neither catalog presence nor configuration is a live pass.
|
||||
"""
|
||||
import argparse
|
||||
import collections
|
||||
import datetime
|
||||
import http.cookiejar
|
||||
import importlib.util
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import time
|
||||
import urllib.error
|
||||
import urllib.request
|
||||
|
||||
|
||||
TEXT_INPUT_TYPES = frozenset(("text", "string", "prompt"))
|
||||
|
||||
|
||||
def normalize_types(value):
|
||||
"""Return a stable list of declared input/output types from catalog JSON."""
|
||||
if isinstance(value, str):
|
||||
value = value.split(",")
|
||||
if not isinstance(value, list):
|
||||
return []
|
||||
return [str(item).strip().lower() for item in value if str(item).strip()]
|
||||
|
||||
|
||||
def chat_test_reasons(item, visible_route=True):
|
||||
"""Explain why a route must not be sent a generic text chat request."""
|
||||
reasons = []
|
||||
if not visible_route:
|
||||
reasons.append("inactive_route_or_channel")
|
||||
if not item.get("pricingConfigured"):
|
||||
reasons.append("price_missing")
|
||||
if not item.get("billingReady"):
|
||||
reasons.append("billing_not_ready")
|
||||
if not item.get("executionReady"):
|
||||
reasons.append("executor_not_ready")
|
||||
if item.get("mode") == "async" or item.get("type") not in ("text", "code"):
|
||||
reasons.append("requires_modality_specific_test")
|
||||
input_types = normalize_types(item.get("inputTypes"))
|
||||
if not input_types:
|
||||
reasons.append("input_type_unknown")
|
||||
elif not any(item_type in TEXT_INPUT_TYPES for item_type in input_types):
|
||||
reasons.append("input_type_not_text")
|
||||
elif any(item_type not in TEXT_INPUT_TYPES for item_type in input_types):
|
||||
reasons.append("requires_modality_specific_test")
|
||||
return list(dict.fromkeys(reasons))
|
||||
|
||||
|
||||
def estimate_reservation(models, max_tokens):
|
||||
"""Estimate the maximum reservation for the bounded chat probes."""
|
||||
estimated = 0.0
|
||||
for model in models:
|
||||
pricing = model.get("pricing") or {}
|
||||
unit = str(pricing.get("unit", "")).lower()
|
||||
if "token" not in unit and pricing.get("price") is not None:
|
||||
# Fixed-unit routes (for example CNY / task) charge once per probe.
|
||||
estimated += float(pricing["price"])
|
||||
else:
|
||||
estimated += (
|
||||
float(pricing.get("input", 0)) * 1024
|
||||
+ float(pricing.get("output", 0)) * max_tokens
|
||||
) / 1_000_000
|
||||
return estimated
|
||||
|
||||
|
||||
def load_retry_route_ids(path):
|
||||
"""Read route IDs with a non-pass outcome from a previous audit report."""
|
||||
document = json.loads(path.read_text(encoding="utf-8"))
|
||||
results = document.get("results")
|
||||
if not isinstance(results, list):
|
||||
raise ValueError("Retry report does not contain a results list")
|
||||
return {
|
||||
str(result["id"])
|
||||
for result in results
|
||||
if isinstance(result, dict) and result.get("id") and result.get("outcome") != "pass"
|
||||
}
|
||||
|
||||
|
||||
def outcome_summary(results):
|
||||
outcomes = collections.Counter(result.get("outcome", "unknown") for result in results)
|
||||
passed = outcomes.get("pass", 0)
|
||||
tested = len(results)
|
||||
return {
|
||||
"testedRoutes": tested,
|
||||
"passedRoutes": passed,
|
||||
"nonPassRoutes": tested - passed,
|
||||
"passRate": round(passed / tested, 4) if tested else None,
|
||||
"outcomes": dict(outcomes),
|
||||
}
|
||||
|
||||
|
||||
def cleanup_temporary_key(request, key_id):
|
||||
"""Disable and delete a temporary key, attempting deletion after any failure."""
|
||||
result = {"disabled": False, "deleted": False, "errors": []}
|
||||
try:
|
||||
status, _ = request("/api/keys/" + key_id + "/status", {"status": "disabled"}, method="PATCH")
|
||||
result["disabled"] = status == 204
|
||||
except Exception as error: # cleanup must continue to deletion
|
||||
result["errors"].append({"operation": "disable", "type": type(error).__name__})
|
||||
try:
|
||||
status, _ = request("/api/keys/" + key_id, method="DELETE")
|
||||
result["deleted"] = status == 204
|
||||
except Exception as error:
|
||||
result["errors"].append({"operation": "delete", "type": type(error).__name__})
|
||||
return result
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument("--deployment-dir", required=True, type=Path)
|
||||
parser.add_argument("--output", required=True, type=Path)
|
||||
parser.add_argument("--run-calls", action="store_true")
|
||||
parser.add_argument("--max-estimated-cny", type=float, default=10)
|
||||
parser.add_argument("--max-tokens", type=int, default=16)
|
||||
parser.add_argument("--request-timeout", "--timeout", dest="request_timeout", type=float, default=120)
|
||||
parser.add_argument("--only-model", action="append", dest="only_models", default=[])
|
||||
parser.add_argument("--retry-report", type=Path,
|
||||
help="Only re-test routes whose prior report outcome was not pass")
|
||||
args = parser.parse_args()
|
||||
if args.max_estimated_cny < 0:
|
||||
parser.error("--max-estimated-cny must be non-negative")
|
||||
if args.max_tokens < 1:
|
||||
parser.error("--max-tokens must be at least 1")
|
||||
if args.request_timeout < 1:
|
||||
parser.error("--request-timeout must be at least 1 second")
|
||||
os.umask(0o077)
|
||||
retry_route_ids = load_retry_route_ids(args.retry_report) if args.retry_report else set()
|
||||
spec = importlib.util.spec_from_file_location("moyi_deploy", args.deployment_dir / "deploy.py")
|
||||
deploy = importlib.util.module_from_spec(spec)
|
||||
spec.loader.exec_module(deploy)
|
||||
config = deploy.load_config(args.deployment_dir / ".env.cloud", "https")
|
||||
env = config["services"]["moyi-api"]["environment"]
|
||||
base = "https://" + config["services"]["frontend"]["environment"]["MOYI_DOMAIN"]
|
||||
opener = urllib.request.build_opener(urllib.request.ProxyHandler({}), urllib.request.HTTPCookieProcessor(http.cookiejar.CookieJar()))
|
||||
|
||||
def request(path, body=None, method=None, key=None, timeout=None):
|
||||
timeout = args.request_timeout if timeout is None else timeout
|
||||
headers = {"Content-Type": "application/json", "User-Agent": "moyi-catalog-acceptance/1"}
|
||||
if key:
|
||||
headers["Authorization"] = "Bearer " + key
|
||||
req = urllib.request.Request(base + path, data=json.dumps(body).encode() if body is not None else None,
|
||||
method=method, headers=headers)
|
||||
try:
|
||||
with opener.open(req, timeout=timeout) as response:
|
||||
raw = response.read(2_000_000)
|
||||
return response.status, json.loads(raw) if raw else {}
|
||||
except urllib.error.HTTPError as error:
|
||||
try:
|
||||
payload = json.loads(error.read(8192))
|
||||
except ValueError:
|
||||
payload = {}
|
||||
return error.code, payload
|
||||
|
||||
report = {
|
||||
"startedAt": datetime.datetime.now(datetime.timezone.utc).isoformat(),
|
||||
"baseUrl": base,
|
||||
"parameters": {
|
||||
"runCalls": args.run_calls,
|
||||
"maxEstimatedCny": args.max_estimated_cny,
|
||||
"maxTokens": args.max_tokens,
|
||||
"requestTimeoutSeconds": args.request_timeout,
|
||||
"onlyModels": args.only_models,
|
||||
"retryReport": str(args.retry_report) if args.retry_report else None,
|
||||
},
|
||||
"retrySelection": {
|
||||
"requestedNonPassRouteIds": sorted(retry_route_ids),
|
||||
"matchedInventoryRouteIds": [],
|
||||
"eligibleRouteIds": [],
|
||||
"skippedRouteIds": [],
|
||||
},
|
||||
"inventory": [], "results": [], "temporaryKeyDeleted": False,
|
||||
}
|
||||
|
||||
def save():
|
||||
args.output.parent.mkdir(parents=True, exist_ok=True)
|
||||
args.output.write_text(json.dumps(report, ensure_ascii=False, indent=2), encoding="utf-8")
|
||||
|
||||
key_id = None
|
||||
try:
|
||||
status, _ = request("/api/auth/login", {"account": env["ADMIN_ACCOUNT"], "password": env["ADMIN_PASSWORD"]})
|
||||
if status != 200:
|
||||
raise RuntimeError("Admin login failed: HTTP " + str(status))
|
||||
status, catalog = request("/api/admin/models")
|
||||
if status != 200:
|
||||
raise RuntimeError("Catalog read failed")
|
||||
status, public = request("/api/models")
|
||||
if status != 200:
|
||||
raise RuntimeError("User catalog read failed")
|
||||
active_ids = {item["id"] for item in public["models"]}
|
||||
provider_names = {item["id"]: item["code"] for item in public["providers"]}
|
||||
candidates = []
|
||||
for item in catalog["models"]:
|
||||
row = {field: item.get(field) for field in ("id", "logicalModelId", "modelKey", "publicName", "sourceLabel", "providerId", "upstreamModel", "type", "mode", "status", "maxConcurrency", "pricingConfigured", "billingReady", "executionReady")}
|
||||
row["inputTypes"] = normalize_types(item.get("inputTypes"))
|
||||
row["outputTypes"] = normalize_types(item.get("outputTypes"))
|
||||
row["provider"] = provider_names.get(item["providerId"], item["providerId"])
|
||||
row["visibleRoute"] = item["id"] in active_ids
|
||||
row["pricing"] = item.get("pricing")
|
||||
reasons = chat_test_reasons(item, row["visibleRoute"])
|
||||
row["notChatTestedReasons"] = reasons
|
||||
row["chatInputCompatible"] = not any(reason in reasons for reason in ("input_type_unknown", "input_type_not_text", "requires_modality_specific_test"))
|
||||
row["retrySelected"] = item["id"] in retry_route_ids
|
||||
report["inventory"].append(row)
|
||||
if item["id"] in retry_route_ids:
|
||||
report["retrySelection"]["matchedInventoryRouteIds"].append(item["id"])
|
||||
selected_by_name = not args.only_models or item["id"] in args.only_models or item.get("upstreamModel") in args.only_models
|
||||
selected_by_retry = not args.retry_report or item["id"] in retry_route_ids
|
||||
if not reasons and selected_by_name and selected_by_retry:
|
||||
candidates.append(item)
|
||||
report["retrySelection"]["eligibleRouteIds"] = [item["id"] for item in candidates if item["id"] in retry_route_ids]
|
||||
report["retrySelection"]["skippedRouteIds"] = sorted(retry_route_ids - set(report["retrySelection"]["eligibleRouteIds"]))
|
||||
report["counts"] = {"adminRoutes": len(catalog["models"]), "userVisibleRoutes": len(active_ids),
|
||||
"logicalDefinitions": len(catalog["logicalModels"]), "chatCandidates": len(candidates)}
|
||||
report["channelCounts"] = dict(collections.Counter(row["provider"] for row in report["inventory"]))
|
||||
print(json.dumps(report["counts"]), flush=True)
|
||||
save()
|
||||
if not args.run_calls:
|
||||
return
|
||||
estimated = estimate_reservation(candidates, args.max_tokens)
|
||||
report["estimatedReservationCny"] = estimated
|
||||
if estimated > args.max_estimated_cny:
|
||||
raise RuntimeError("Estimated test reservation exceeds configured budget")
|
||||
name = "catalog-audit-" + datetime.datetime.now(datetime.timezone.utc).strftime("%Y%m%d-%H%M%S")
|
||||
status, created = request("/api/keys", {"name": name, "modelIds": [m["id"] for m in candidates]})
|
||||
if status != 201:
|
||||
raise RuntimeError("Temporary test key creation failed: HTTP " + str(status))
|
||||
key_id = created["apiKey"]["id"]
|
||||
key = created["key"]
|
||||
report["temporaryKeyName"] = name
|
||||
status, models = request("/v1/models", key=key)
|
||||
report["externalModelListStatus"] = status
|
||||
if isinstance(models, dict):
|
||||
report["externalModelListCount"] = len(models.get("data", []))
|
||||
else:
|
||||
report["externalModelListCount"] = None
|
||||
report["externalModelListError"] = "response_not_object"
|
||||
for index, item in enumerate(candidates):
|
||||
start = time.monotonic()
|
||||
result = {"id": item["id"], "model": item["upstreamModel"], "group": item["sourceLabel"],
|
||||
"provider": provider_names.get(item["providerId"], item["providerId"]),
|
||||
"request": {"endpoint": "/v1/chat/completions", "modelId": item["id"],
|
||||
"maxTokens": args.max_tokens, "stream": False,
|
||||
"timeoutSeconds": args.request_timeout}}
|
||||
try:
|
||||
status, payload = request("/v1/chat/completions", {"model": item["id"], "messages": [{"role": "user", "content": "Reply with OK."}], "max_tokens": args.max_tokens, "stream": False}, key=key)
|
||||
result["status"] = status
|
||||
if status == 200:
|
||||
if not isinstance(payload, dict):
|
||||
result["outcome"] = "invalid_payload"
|
||||
result["errorCode"] = "RESPONSE_NOT_OBJECT"
|
||||
else:
|
||||
choices = payload.get("choices") or []
|
||||
message = choices[0].get("message", {}) if choices and isinstance(choices[0], dict) else {}
|
||||
content = message.get("content")
|
||||
result["contentType"] = type(content).__name__ if content is not None else None
|
||||
result["completionPresent"] = bool(content or message.get("tool_calls"))
|
||||
# Reasoning-only responses are valid upstream responses when
|
||||
# a reasoning model reaches the caller's output budget before
|
||||
# writing its final answer. Keep them distinct from a truly
|
||||
# empty response so the audit does not misdiagnose a budget
|
||||
# outcome as a transport or gateway failure.
|
||||
result["reasoningPresent"] = bool(message.get("reasoning_content") or message.get("reasoning"))
|
||||
result["finishReason"] = choices[0].get("finish_reason") if choices and isinstance(choices[0], dict) else None
|
||||
usage = payload.get("usage", {})
|
||||
result["tokens"] = {k: usage.get(k) for k in ("prompt_tokens", "completion_tokens", "total_tokens")} if isinstance(usage, dict) else None
|
||||
result["outcome"] = "pass" if result["completionPresent"] else "reasoning_only" if result["reasoningPresent"] else "empty_completion"
|
||||
else:
|
||||
error = payload.get("error", {})
|
||||
result["errorCode"] = str(error.get("code", "HTTP_" + str(status))) if isinstance(error, dict) else "HTTP_" + str(status)
|
||||
result["outcome"] = "failed"
|
||||
except Exception as error:
|
||||
result.update(outcome="transport_error", errorType=type(error).__name__)
|
||||
result["elapsedSeconds"] = round(time.monotonic() - start, 3)
|
||||
report["results"].append(result)
|
||||
save()
|
||||
print(json.dumps({"index": index + 1, "total": len(candidates), **result}, ensure_ascii=False), flush=True)
|
||||
# Prevent short upstream failures from flooding the gateway RPM limit.
|
||||
time.sleep(max(0, 1.1 - (time.monotonic() - start)))
|
||||
finally:
|
||||
if key_id:
|
||||
cleanup = cleanup_temporary_key(request, key_id)
|
||||
report["temporaryKeyDisabled"] = cleanup["disabled"]
|
||||
report["temporaryKeyDeleted"] = cleanup["deleted"]
|
||||
if cleanup["errors"]:
|
||||
report["cleanupErrors"] = cleanup["errors"]
|
||||
report["finishedAt"] = datetime.datetime.now(datetime.timezone.utc).isoformat()
|
||||
report.update(outcome_summary(report["results"]))
|
||||
save()
|
||||
try:
|
||||
request("/api/auth/logout", {}, method="POST")
|
||||
except Exception as error:
|
||||
report["logoutError"] = type(error).__name__
|
||||
save()
|
||||
print(json.dumps({"outcomes": report["outcomes"], "temporaryKeyDeleted": report["temporaryKeyDeleted"]}), flush=True)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,65 @@
|
||||
import importlib.util
|
||||
import json
|
||||
import tempfile
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
SCRIPT = Path(__file__).with_name("live-catalog-audit.py")
|
||||
SPEC = importlib.util.spec_from_file_location("live_catalog_audit", SCRIPT)
|
||||
MODULE = importlib.util.module_from_spec(SPEC)
|
||||
SPEC.loader.exec_module(MODULE)
|
||||
|
||||
|
||||
class LiveCatalogAuditTests(unittest.TestCase):
|
||||
def test_estimate_uses_requested_max_tokens(self):
|
||||
route = {"pricing": {"input": 1, "output": 2}}
|
||||
self.assertAlmostEqual(MODULE.estimate_reservation([route], 16), (1024 + 32) / 1_000_000)
|
||||
self.assertAlmostEqual(MODULE.estimate_reservation([route], 128), (1024 + 256) / 1_000_000)
|
||||
self.assertEqual(MODULE.estimate_reservation([{"pricing": {"price": 1.44, "unit": "CNY / task"}}], 16), 1.44)
|
||||
|
||||
def test_input_type_check_excludes_ocr_from_text_probe(self):
|
||||
route = {
|
||||
"pricingConfigured": True, "billingReady": True, "executionReady": True,
|
||||
"mode": "sync", "type": "text", "inputTypes": ["image"],
|
||||
}
|
||||
reasons = MODULE.chat_test_reasons(route)
|
||||
self.assertIn("input_type_not_text", reasons)
|
||||
|
||||
def test_unknown_and_mixed_input_types_are_explicit(self):
|
||||
unknown = {"type": "text", "mode": "sync", "inputTypes": []}
|
||||
mixed = {"type": "text", "mode": "sync", "inputTypes": ["text", "image"]}
|
||||
self.assertIn("input_type_unknown", MODULE.chat_test_reasons(unknown))
|
||||
self.assertIn("requires_modality_specific_test", MODULE.chat_test_reasons(mixed))
|
||||
|
||||
def test_retry_report_selects_only_non_pass_route_ids(self):
|
||||
with tempfile.TemporaryDirectory() as directory:
|
||||
path = Path(directory) / "previous.json"
|
||||
path.write_text(json.dumps({"results": [
|
||||
{"id": "pass-route", "outcome": "pass"},
|
||||
{"id": "failed-route", "outcome": "failed"},
|
||||
{"id": "reasoning-route", "outcome": "reasoning_only"},
|
||||
]}), encoding="utf-8")
|
||||
self.assertEqual(MODULE.load_retry_route_ids(path), {"failed-route", "reasoning-route"})
|
||||
|
||||
def test_cleanup_attempts_delete_when_disable_fails(self):
|
||||
calls = []
|
||||
|
||||
def request(path, body=None, method=None):
|
||||
calls.append((path, method))
|
||||
if method == "PATCH":
|
||||
raise TimeoutError("simulated timeout")
|
||||
return 204, {}
|
||||
|
||||
result = MODULE.cleanup_temporary_key(request, "temporary")
|
||||
self.assertFalse(result["disabled"])
|
||||
self.assertTrue(result["deleted"])
|
||||
self.assertEqual(calls, [
|
||||
("/api/keys/temporary/status", "PATCH"),
|
||||
("/api/keys/temporary", "DELETE"),
|
||||
])
|
||||
self.assertEqual(result["errors"][0]["operation"], "disable")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in new issue
Block a user