Compare commits

...
8 Commits
Author SHA1 Message Date
xppandClaude Opus 5.5 bb3baed095 docs:"记录 COD-01/02 暂缓、不存储对话与 Sub2API 对齐决定"
Repository verification / frontend (push) Canceled after 0s
Repository verification / backend (push) Canceled after 0s
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-22 21:49:56 +08:00
xppandClaude Opus 5.5 4b6bdeeef6 docs:"同步 Sub2API 对照中的保留时间说明"
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-22 20:40:06 +08:00
xppandClaude Opus 5.5 f2ac810572 docs:"补充 Sub2API 续接与 WebSocket 对照并更正 store=false 下 HTTP 续接不可用"
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-22 20:39:52 +08:00
xppandClaude Opus 5.5 fd860a87a2 docs:"记录管理员权限保持不拆分的决定"
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-22 16:14:38 +08:00
xppandClaude Opus 5.5 c04b9262c4 docs:"Codex Responses 续接归属与 WebSocket 设计(待评审)"
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-22 16:13:00 +08:00
xppandClaude Opus 5.5 aef5356c54 feat:"拆分上游首字节/空闲/总期限并在执行期间续期预留与并发租约"
- OpenAiCompatibleProvider:总期限上限 3600 秒(默认仍 90),新增首字节与空闲超时,流式有数据即重置空闲窗口
- GatewayRequestExecutionService:调用期间定时续期钱包预留与 Redis 租约,结束即停
- ChatBillingService.renew 只延长 pending 预留;GatewayGuard 只续仍持有的租约,丢失不重建
- 新增本地 Docker 启动覆盖文件(预编译产物,仅监听 127.0.0.1)
- 新增流式空闲/长流、续期、过期恢复不释放运行中调用、租约续期回归

验证:mvn -B -o clean verify 409 项通过(独立 PostgreSQL/Redis)

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-22 15:25:28 +08:00
xppandClaude Opus 5.5 1a608f8ff8 fix:"模型准入改为公平排队并收紧渠道凭据与目录暴露"
- acquire-pair.lua 增加模型到达顺序队列:空闲槽位先给更早等待者,超时/放弃/过期即出队,仅因自身 Key 满的等待者不占模型队位
- 修改渠道 Base URL 必须重新填写凭据,阻止已存密钥被发送到新地址;审计记录新旧地址
- 普通用户模型目录只返回渠道 id/name/status,不再暴露 Base URL 和凭据掩码
- 管理端用量 CSV 导出中和公式前缀
- 新增真实 Redis 公平准入、渠道凭据重填、目录字段和 CSV 回归

验证:mvn -B -o clean verify 403 项通过(独立 PostgreSQL/Redis)

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-22 12:06:41 +08:00
xppandClaude Opus 5.5 2e53b1d9c8 fix:"规范音视频任务结果并按真实能力标识音频执行"
- 新增 AsyncTaskResponseNormalizer:解析 content/result/嵌套 data 回执与 {url} 对象,仅白名单媒体字段,裸字符串只接受 URL
- 视频/音频上游完成但无结果文件时标记 deliveryStatus=pending,结算一次、不重发不退款
- 目录与执行共用 AsyncMediaSupport.supportsSpeech;非 TTS 音频、专用渠道任务类型错配、非法语音参数在发送前拒绝
- 任务响应返回 taskType/completedAt,前端以持久任务类型优先,补音频 MIME、签名 URL 与未交付提示
- 新增 29 号 CPR 差距分析与优化方案文档,更新 24 号任务表 MED-01/02/03

验证:mvn -B -o clean verify 399 项通过(独立 PostgreSQL/Redis);前端构建与 27 项测试通过

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-22 11:48:47 +08:00
31 changed files with 1199 additions and 111 deletions

No files matched your search

@@ -30,7 +30,7 @@ public class CatalogController {
@GetMapping("/models")
public Map<String, Object> models(jakarta.servlet.http.HttpServletRequest request) {
authService.requireUser(AuthController.sessionToken(request));
return Map.of("providers", catalogService.listProviders(false), "models", catalogService.listModels(false), "logicalModels", catalogService.listDefinitions(false));
return Map.of("providers", catalogService.listPublicProviders(), "models", catalogService.listModels(false), "logicalModels", catalogService.listDefinitions(false));
}
@GetMapping("/overview")
@@ -149,6 +149,12 @@ public class CatalogService {
boolean publishingChannel = !"active".equals(previous.get("status")) && "active".equals(nextStatus);
jdbc.update("UPDATE model_providers SET code=?, name=?, protocol=?, source_type=?, base_url=?, description=?, credential_type=?, status=?, updated_at=now() WHERE id=?", code,name,protocol,sourceType,baseUrl,optional(value,"description",""),credentialType,nextStatus,id);
String secret=optional(input,"credentialSecret","");
// A stored credential is bound to the endpoint it was entered for. Redirecting it elsewhere
// would send the decrypted key to the new host, so changing the endpoint requires re-entry.
boolean endpointChanged=!java.util.Objects.equals(normalizedEndpoint(previous.get("baseUrl")),normalizedEndpoint(baseUrl));
if (endpointChanged && !"none".equals(credentialType) && secret.isBlank() && previous.get("credentialMasked")!=null) {
throw new ApiException("CREDENTIAL_REENTRY_REQUIRED","修改渠道地址时须重新填写凭据,已保存的凭据不会发送到新地址");
}
if ("none".equals(credentialType)) jdbc.update("UPDATE model_providers SET credential_ciphertext=NULL, credential_masked=NULL WHERE id=?",id);
else if (!secret.isBlank()) jdbc.update("UPDATE model_providers SET credential_ciphertext=?, credential_masked=? WHERE id=?",secretCipher.encrypt(secret),mask(secret),id);
if (publishingChannel) {
@@ -157,10 +163,27 @@ public class CatalogService {
// an administrator can still disable individual groups afterwards.
jdbc.update("UPDATE model_catalog SET status='active', updated_at=now() WHERE provider_id=? AND status='disabled' AND pricing IS NOT NULL AND deleted_at IS NULL", id);
}
auth.audit("provider.updated",admin.id(),new AuthService.RequestMeta(null,null),Map.of("providerId",id));
Map<String,Object> details=new LinkedHashMap<>();
details.put("providerId",id);
if (endpointChanged) { details.put("previousBaseUrl",String.valueOf(previous.get("baseUrl"))); details.put("baseUrl",String.valueOf(baseUrl)); }
if (!secret.isBlank() || "none".equals(credentialType)) details.put("credentialReplaced",true);
auth.audit("provider.updated",admin.id(),new AuthService.RequestMeta(null,null),details);
return findProvider(id);
}
private static String normalizedEndpoint(Object value) {
return value == null || String.valueOf(value).isBlank() ? null : String.valueOf(value).trim().replaceAll("/+$", "").toLowerCase(java.util.Locale.ROOT);
}
/** Ordinary users only need channel identity for display; endpoints and credential hints stay admin-only. */
public List<Map<String, Object>> listPublicProviders() {
return listProviders(false).stream().map(provider -> {
Map<String, Object> visible = new LinkedHashMap<>();
for (String field : List.of("id", "name", "status")) if (provider.get(field) != null) visible.put(field, provider.get(field));
return visible;
}).toList();
}
private void validateBaseUrl(String value) {
if (value == null || value.isBlank()) return;
try {
@@ -541,7 +564,7 @@ public class CatalogService {
// "异步执行器待接入" when the execution path is already implemented,
// while unsupported task types remain visibly pending.
boolean asyncExecutor = asyncExecutorReady(
resultSet.getString("provider_id"), providerProtocol, resultSet.getString("model_type"));
resultSet.getString("provider_id"), providerProtocol, resultSet.getString("model_type"), resultSet.getString("upstream_model"));
String upstream=resultSet.getString("upstream_model").toLowerCase(java.util.Locale.ROOT);
if(upstream.startsWith("mj_") || upstream.startsWith("suno") || upstream.startsWith("lyria") || upstream.equals("swap_face")) asyncExecutor=false;
boolean syncExecutor=List.of("chat","text","code","multimodal","llm","reasoning","vision","embedding").contains(resultSet.getString("model_type")) && ("openai-compatible".equalsIgnoreCase(providerProtocol)||"provider-mock".equals(resultSet.getString("provider_id")));
@@ -581,9 +604,16 @@ public class CatalogService {
}
static boolean asyncExecutorReady(String providerId, String providerProtocol, String modelType) {
if (List.of("provider-mock", "provider-vast", "provider-bytedance").contains(providerId)) return true;
return asyncExecutorReady(providerId, providerProtocol, modelType, null);
}
static boolean asyncExecutorReady(String providerId, String providerProtocol, String modelType, String upstreamModel) {
if ("provider-mock".equals(providerId)) return true;
if ("provider-vast".equals(providerId)) return "3d".equalsIgnoreCase(modelType);
if ("provider-bytedance".equals(providerId)) return "video".equalsIgnoreCase(modelType);
if (!"openai-compatible".equalsIgnoreCase(providerProtocol)) return false;
return List.of("image", "video", "audio").contains(String.valueOf(modelType).toLowerCase(java.util.Locale.ROOT));
if ("audio".equalsIgnoreCase(modelType)) return com.moyi.controlplane.provider.media.AsyncMediaSupport.supportsSpeech(upstreamModel);
return List.of("image", "video").contains(String.valueOf(modelType).toLowerCase(java.util.Locale.ROOT));
}
private boolean billingReady(Map<String,Object> pricing, String mode, String providerId) {
@@ -34,6 +34,16 @@ public class GatewayGuard {
end
return counts
""", List.class);
// Renew only a lease this request still owns; an expired or released token is never recreated.
private static final DefaultRedisScript<Long> RENEW = new DefaultRedisScript<>("""
local time = redis.call('TIME')
local now = tonumber(time[1]) * 1000 + math.floor(tonumber(time[2]) / 1000)
local score = redis.call('ZSCORE', KEYS[1], ARGV[1])
if not score or tonumber(score) <= now then return 0 end
redis.call('ZADD', KEYS[1], 'XX', now + tonumber(ARGV[2]), ARGV[1])
if redis.call('PTTL', KEYS[1]) < tonumber(ARGV[2]) then redis.call('PEXPIRE', KEYS[1], ARGV[2]) end
return 1
""", Long.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;
@@ -228,6 +238,7 @@ public class GatewayGuard {
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 orderBucket = redisPrefix + "gateway:order: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);
@@ -249,7 +260,7 @@ public class GatewayGuard {
}
while (true) {
long remaining = Math.max(0, java.util.concurrent.TimeUnit.NANOSECONDS.toMillis(deadline - System.nanoTime()));
long acquired = executePair(modelBucket, keyBucket, waitingBucket, Integer.toString(modelLimit), Integer.toString(defaultKeyConcurrency), Long.toString(SLOT_TTL_MS), token, Long.toString(remaining), Long.toString(Math.max(SLOT_TTL_MS, waitMs + 1000)));
long acquired = executePair(modelBucket, keyBucket, waitingBucket, orderBucket, Integer.toString(modelLimit), Integer.toString(defaultKeyConcurrency), Long.toString(SLOT_TTL_MS), token, Long.toString(remaining), Long.toString(Math.max(SLOT_TTL_MS, waitMs + 1000)));
if (acquired == 1) {
admitted = true; // The acquisition script also removed the waiting entry.
return new Admission(modelBucket, keyBucket, token);
@@ -266,7 +277,11 @@ public class GatewayGuard {
}
}
} finally {
if (!admitted && waitMs > 0) releaseQuietly(waitingBucket, token);
if (!admitted && waitMs > 0) {
// Leaving the queue must also release this caller's FIFO position.
releaseQuietly(waitingBucket, token);
releaseQuietly(orderBucket, token);
}
}
}
@@ -301,9 +316,9 @@ public class GatewayGuard {
throw new GatewayLimitException("GATEWAY_GUARD_UNAVAILABLE", "网关流量控制暂不可用,请稍后重试", 503, 3);
}
private long executePair(String modelKey, String keyKey, String waitingKey, String modelLimit, String keyLimit, String ttl, String token, String waitMs, String waitingTtl) {
private long executePair(String modelKey, String keyKey, String waitingKey, String orderKey, String modelLimit, String keyLimit, String ttl, String token, String waitMs, String waitingTtl) {
try {
Long value = redis.execute(ACQUIRE_PAIR, List.of(modelKey, keyKey, waitingKey), modelLimit, keyLimit, ttl, token, waitMs, waitingTtl);
Long value = redis.execute(ACQUIRE_PAIR, List.of(modelKey, keyKey, waitingKey, orderKey), 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());
@@ -335,6 +350,15 @@ public class GatewayGuard {
this.keySlot = keySlot;
}
/** Extend every lease held by this admission; returns false if any lease was already lost. */
@Override
public boolean renew(long ttlMs) {
if (closed.get()) return false;
boolean held = true;
for (Slot slot : new Slot[]{modelSlot, keySlot, userSlot}) if (slot != null && !slot.renew(ttlMs)) held = false;
return held;
}
@Override
public void close() {
if (!closed.compareAndSet(false, true)) return;
@@ -351,6 +375,13 @@ public class GatewayGuard {
private Slot(String key, String token) { this.key = key; this.token = token; }
boolean renew(long ttlMs) {
if (closed.get()) return false;
if (key == null || token == null) return true;
try { return execute(RENEW, key, token, Long.toString(Math.max(SLOT_TTL_MS, ttlMs))) == 1; }
catch (GatewayLimitException exception) { return false; }
}
@Override
public void close() {
if (!closed.compareAndSet(false, true)) return;
@@ -2,6 +2,9 @@ package com.moyi.controlplane.gateway.request;
/** One request's releasable admission lease. */
public interface ApiRequestAdmission extends AutoCloseable {
/** Extend the lease while the request is still running. Returns false if it was already lost. */
default boolean renew(long ttlMs) { return true; }
@Override
void close();
}
@@ -13,14 +13,33 @@ import org.springframework.stereotype.Service;
/** Shared application boundary for synchronous and native-protocol model requests. */
@Service
public class GatewayRequestExecutionService {
/** Wallet holds and Redis leases are short; a running call renews both on this cadence. */
static final long RENEW_INTERVAL_MS = 60_000;
static final long WALLET_HOLD_MS = 300_000;
static final long LEASE_TTL_MS = 180_000;
private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(GatewayRequestExecutionService.class);
private final ChatBillingService billing;
private final ApiRequestRiskPolicy risk;
private final long renewIntervalMs;
private final java.util.concurrent.ScheduledExecutorService renewals = java.util.concurrent.Executors.newSingleThreadScheduledExecutor(r -> {
var thread = new Thread(r, "request-lease-renewal"); thread.setDaemon(true); return thread;
});
@org.springframework.beans.factory.annotation.Autowired
public GatewayRequestExecutionService(ChatBillingService billing, ApiRequestRiskPolicy risk) {
this(billing, risk, RENEW_INTERVAL_MS);
}
GatewayRequestExecutionService(ChatBillingService billing, ApiRequestRiskPolicy risk, long renewIntervalMs) {
this.billing = java.util.Objects.requireNonNull(billing, "billing");
this.risk = java.util.Objects.requireNonNull(risk, "risk");
if (renewIntervalMs < 1) throw new IllegalArgumentException("Renewal interval must be positive");
this.renewIntervalMs = renewIntervalMs;
}
@jakarta.annotation.PreDestroy
void shutdown() { renewals.shutdownNow(); }
public <T> T execute(
ApiRequestContext context,
Map<String, Object> key,
@@ -33,8 +52,11 @@ public class GatewayRequestExecutionService {
Function<ApiException, T> deliverFailure
) {
risk.enforce(context);
try (ApiRequestAdmission ignored = admission.get()) {
try (ApiRequestAdmission lease = admission.get()) {
ChatBillingService.Ticket ticket = billing.reserve(key, model, normalizedRequest, idempotencyKey, context.metadata());
// Long calls outlive the initial hold and lease. Keep both alive until the work returns,
// so expiry recovery cannot release a running call and other callers cannot take its slot.
var renewal = renewals.scheduleWithFixedDelay(() -> renew(ticket, lease), renewIntervalMs, renewIntervalMs, java.util.concurrent.TimeUnit.MILLISECONDS);
try {
return work.execute(ticket);
} catch (Exception failure) {
@@ -46,10 +68,22 @@ public class GatewayRequestExecutionService {
ApiException error = classifyFailure.apply(failure);
billing.fail(ticket, error.getCode(), error.getMessage(), context.elapsedMillis());
return deliverFailure.apply(error);
} finally {
renewal.cancel(false);
}
}
}
private void renew(ChatBillingService.Ticket ticket, ApiRequestAdmission lease) {
try {
billing.renew(ticket.id(), WALLET_HOLD_MS);
if (!lease.renew(LEASE_TTL_MS)) log.warn("Request concurrency lease could not be renewed; it may be reused before the call ends");
} catch (RuntimeException exception) {
// A missed renewal is retried on the next tick; the original hold still bounds recovery.
log.warn("Request lease renewal failed ({})", exception.getClass().getSimpleName());
}
}
public BigDecimal settle(ChatBillingService.Ticket ticket, Map<String, Object> result, int latencyMs, Integer firstTokenMs) {
// Validate and reduce to the only data required to price the call. The response body may contain
// prompts, generated content, tool arguments or other sensitive data and is never recovery state.
@@ -30,6 +30,8 @@ import org.springframework.stereotype.Service;
@Service
public class OpenAiCompatibleProvider {
/** Upper bound for the configurable total deadline; leases are renewed at most this long. */
public static final int MAX_TIMEOUT_SECONDS = 3600;
private final ObjectMapper objectMapper;
private final HttpClient httpClient;
private final JdbcTemplate jdbc;
@@ -41,6 +43,10 @@ public class OpenAiCompatibleProvider {
private final ConcurrentMap<String, AtomicInteger> keyCursors = new ConcurrentHashMap<>();
private final ScheduledExecutorService deadlines = Executors.newSingleThreadScheduledExecutor(r -> { var t=new Thread(r,"upstream-deadlines"); t.setDaemon(true); return t; });
private final int timeoutSeconds;
// Long reasoning calls need a larger total deadline without letting a silent upstream hold
// the request, its concurrency lease and wallet hold. Both default to the total deadline.
private final long firstByteTimeoutMs;
private final long idleTimeoutMs;
@org.springframework.beans.factory.annotation.Autowired private UpstreamHealth health;
public OpenAiCompatibleProvider(
@@ -54,7 +60,9 @@ public class OpenAiCompatibleProvider {
this.jdbc = jdbc;
this.secretCipher = secretCipher;
this.environment = environment;
this.timeoutSeconds = Math.max(1,Math.min(90,environment.getProperty("moyi.gateway.upstream-timeout-seconds",Integer.class,90)));
this.timeoutSeconds = Math.max(1,Math.min(MAX_TIMEOUT_SECONDS,environment.getProperty("moyi.gateway.upstream-timeout-seconds",Integer.class,90)));
this.firstByteTimeoutMs = 1000L*Math.max(1,Math.min(timeoutSeconds,environment.getProperty("moyi.gateway.upstream-first-byte-timeout-seconds",Integer.class,timeoutSeconds)));
this.idleTimeoutMs = 1000L*Math.max(1,Math.min(timeoutSeconds,environment.getProperty("moyi.gateway.upstream-idle-timeout-seconds",Integer.class,timeoutSeconds)));
this.providers = Map.of(
"provider-deepseek", configured("provider-deepseek", environment.getProperty("moyi.providers.deepseek.api-key", ""), environment.getProperty("moyi.providers.deepseek.base-url", "https://api.deepseek.com"), environment),
"provider-kimi", configured("provider-kimi", environment.getProperty("moyi.providers.kimi.api-key", ""), environment.getProperty("moyi.providers.kimi.base-url", "https://api.moonshot.ai/v1"), environment),
@@ -157,7 +165,7 @@ public class OpenAiCompatibleProvider {
String json = writeJson(body);
HttpRequest.Builder builder = HttpRequest.newBuilder()
.uri(URI.create(base + path))
.timeout(Duration.ofMillis(remaining))
.timeout(Duration.ofMillis(streaming?Math.min(remaining,firstByteTimeoutMs):remaining))
.header("Authorization", "Bearer " + apiKey)
.header("Content-Type", "application/json")
.POST(HttpRequest.BodyPublishers.ofString(json));
@@ -170,7 +178,7 @@ public class OpenAiCompatibleProvider {
if(health!=null) health.record(healthRoute,apiKey,response.statusCode(),(int)TimeUnit.NANOSECONDS.toMillis(System.nanoTime()-started));
if (response.statusCode() < 200 || response.statusCode() >= 300) {
String details;
try(var bounded=new StreamResponse(response.body(),Math.max(1,timeoutSeconds*1000L-TimeUnit.NANOSECONDS.toMillis(System.nanoTime()-overallStarted)))) {details=readErrorBody(bounded.body);}
try(var bounded=new StreamResponse(response.body(),Math.max(1,timeoutSeconds*1000L-TimeUnit.NANOSECONDS.toMillis(System.nanoTime()-overallStarted)),false)) {details=readErrorBody(bounded.body);}
if (requiresLargerOutputBudget(details)) {
throw new ApiException("INVALID_MAX_TOKENS", "上游模型要求 max_tokens 大于 1024,请提高输出 Token 上限后重试", 400);
}
@@ -190,7 +198,7 @@ public class OpenAiCompatibleProvider {
response.body().close();
throw new ApiException("UPSTREAM_STREAM_UNSUPPORTED","上游未返回 SSE 流式响应",502);
}
return new StreamResponse(response.body(),Math.max(1,timeoutSeconds*1000L-TimeUnit.NANOSECONDS.toMillis(System.nanoTime()-overallStarted)));
return new StreamResponse(response.body(),Math.max(1,timeoutSeconds*1000L-TimeUnit.NANOSECONDS.toMillis(System.nanoTime()-overallStarted)),streaming);
} catch (ApiException exception) {
throw exception;
} catch (HttpTimeoutException | SocketTimeoutException exception) {
@@ -233,11 +241,28 @@ public class OpenAiCompatibleProvider {
private final InputStream body;
private final AtomicBoolean expired=new AtomicBoolean();
private final ScheduledFuture<?> deadline;
private final boolean idleWatch;
private volatile ScheduledFuture<?> idle;
private volatile boolean closed;
private Runnable afterClose=()->{};
public StreamResponse onClose(Runnable callback) {afterClose=callback;return this;}
private StreamResponse(InputStream body,long remainingMs) {
this.body=body;
this.deadline=deadlines.schedule(() -> { expired.set(true); try { body.close(); } catch(IOException ignored) {} },remainingMs,TimeUnit.MILLISECONDS);
private StreamResponse(InputStream raw,long remainingMs,boolean streaming) {
Runnable expire=() -> { expired.set(true); try { raw.close(); } catch(IOException ignored) {} };
// The idle watchdog only matters when it is shorter than the remaining total deadline.
this.idleWatch=streaming&&idleTimeoutMs<remainingMs;
this.body=!idleWatch?raw:new FilterInputStream(raw) {
@Override public int read() throws IOException { int value=super.read(); if(value!=-1) touch(expire); return value; }
@Override public int read(byte[] buffer,int offset,int length) throws IOException { int count=super.read(buffer,offset,length); if(count>0) touch(expire); return count; }
};
this.deadline=deadlines.schedule(expire,remainingMs,TimeUnit.MILLISECONDS);
if(idleWatch) touch(expire);
}
/** Received bytes prove the upstream is alive; restart the idle window. */
private void touch(Runnable expire) {
if(closed) return;
ScheduledFuture<?> previous=idle;
idle=deadlines.schedule(expire,idleTimeoutMs,TimeUnit.MILLISECONDS);
if(previous!=null) previous.cancel(false);
}
public void events(EventConsumer consumer) throws IOException {
events(consumer,true);
@@ -248,7 +273,7 @@ public class OpenAiCompatibleProvider {
consumer.accept(chunk);
});
}
@Override public void close() { deadline.cancel(false); try { body.close(); } catch(IOException ignored) {} finally {afterClose.run();afterClose=()->{};} }
@Override public void close() { closed=true; deadline.cancel(false); if(idle!=null) idle.cancel(false); try { body.close(); } catch(IOException ignored) {} finally {afterClose.run();afterClose=()->{};} }
}
@FunctionalInterface public interface EventConsumer { void accept(Map<String,Object> chunk) throws IOException; }
@PreDestroy public void close() { deadlines.shutdownNow(); }
@@ -0,0 +1,17 @@
package com.moyi.controlplane.provider.media;
import java.util.Locale;
import java.util.Set;
/** Shared catalog/execution contract. Audio model type alone does not imply a speech endpoint. */
public final class AsyncMediaSupport {
private AsyncMediaSupport() {}
private static final Set<String> SPEECH_MODELS = Set.of(
"tts-1", "tts-1-1106", "tts-1-hd", "tts-1-hd-1106", "gpt-4o-mini-tts");
public static boolean supportsSpeech(String upstreamModel) {
if (upstreamModel == null) return false;
String model = upstreamModel.toLowerCase(Locale.ROOT);
return SPEECH_MODELS.contains(model) || model.matches("gpt-4o-mini-tts-[0-9]{4}-[0-9]{2}-[0-9]{2}");
}
}
@@ -9,6 +9,7 @@ import com.moyi.controlplane.provider.credentials.UpstreamCredentialResolver;
import com.moyi.controlplane.provider.transport.ProviderRequestException;
import com.moyi.controlplane.provider.transport.BoundedHttpTransport;
import com.moyi.controlplane.provider.media.MediaDownloader;
import com.moyi.controlplane.provider.media.AsyncMediaSupport;
import com.moyi.controlplane.provider.media.MediaDownloader.MediaBytes;
import java.net.URI;
import java.net.http.HttpRequest;
@@ -83,6 +84,7 @@ public class AsyncProviderClient {
Provider provider = provider(providerId, routeId, upstreamModel);
String code = provider.code().toLowerCase(java.util.Locale.ROOT);
if (isTripo(provider.code(), provider.baseUrl())) {
requireTaskType(taskType, "3d");
Map<String,Object> body = new LinkedHashMap<>();
body.put("model", upstreamModel);
String media = mediaUrl(input);
@@ -94,6 +96,7 @@ public class AsyncProviderClient {
return withKeyFingerprint(request("POST", tripoBase(provider.baseUrl()) + (media == null ? "/generation/text-to-model" : "/generation/image-to-model"), apiKey, body, "上游 3D 任务创建失败"), apiKey);
}
if (code.contains("byte") || code.contains("seedance") || provider.baseUrl().contains("volces.com")) {
requireTaskType(taskType, "video");
List<Map<String,Object>> content = new ArrayList<>();
if (!prompt.isBlank()) content.add(Map.of("type", "text", "text", prompt));
String media = mediaUrl(input);
@@ -197,15 +200,29 @@ public class AsyncProviderClient {
}
}
if ("audio".equalsIgnoreCase(taskType)) {
if (!AsyncMediaSupport.supportsSpeech(model)) throw new ApiException("ASYNC_PROVIDER_NOT_IMPLEMENTED", "该音频模型不是已接入的文本转语音模型;转写、音乐和实时音频需专用接口", 501);
if (media != null) throw new ApiException("MEDIA_INPUT_UNSUPPORTED", "该语音生成接口接收文本;音频转写需使用专用转写接口", 400);
if (prompt.isBlank() || prompt.length() > 4096) throw new ApiException("INVALID_TASK_INPUT", "语音文本必须为 1 至 4096 个字符", 400);
// OpenAI-compatible TTS returns an audio/* binary body. Keep the
// payload bounded before embedding it into the task result so a
// provider cannot make the database grow without limit.
Map<String,Object> body = new LinkedHashMap<>();
body.put("model", model); body.put("input", prompt);
body.put("voice", input.getOrDefault("voice", "alloy"));
body.put("response_format", input.getOrDefault("response_format", "mp3"));
copyParameter(input, body, "speed");
Object voice = input.getOrDefault("voice", "alloy");
if (!(voice instanceof String name) || name.isBlank() || name.length() > 100) throw new ApiException("INVALID_TASK_INPUT", "请提供有效的语音音色", 400);
Object format = input.getOrDefault("response_format", "mp3");
if (!(format instanceof String formatName) || !List.of("mp3","wav","opus","aac","flac").contains(formatName)) {
throw new ApiException("INVALID_TASK_INPUT", "语音结果支持 mp3、wav、opus、aac、flac;原始 PCM 尚未接入播放", 400);
}
if (input.containsKey("stream_format") && !"audio".equals(input.get("stream_format"))) throw new ApiException("INVALID_TASK_INPUT", "语音任务暂不支持 SSE 事件格式", 400);
body.put("voice", voice); body.put("response_format", format);
if (input.containsKey("speed")) {
try {
var speed = new java.math.BigDecimal(String.valueOf(input.get("speed")));
if (speed.compareTo(new java.math.BigDecimal("0.25")) < 0 || speed.compareTo(new java.math.BigDecimal("4")) > 0) throw new NumberFormatException();
body.put("speed", speed);
} catch (NumberFormatException error) { throw new ApiException("INVALID_TASK_INPUT", "语速必须为 0.25 至 4", 400); }
}
String apiKey = provider.nextApiKey(routeId, model, environment, keyCursors);
return Map.of("_moyiInlineResult", requestAudio("POST", base + "/audio/speech", apiKey, body, "上游语音任务创建失败"), "_moyiUpstreamKeyFingerprint", keyFingerprint(apiKey));
}
@@ -326,7 +343,14 @@ public class AsyncProviderClient {
if (response.statusCode() < 200 || response.statusCode() >= 300) throw upstreamStatus(response.statusCode(), message);
byte[] bytes = response.body();
if (bytes.length == 0 || bytes.length > 8 * 1024 * 1024) throw new ApiException("UPSTREAM_MEDIA_TOO_LARGE", message + "(返回音频超过 8 MiB 或为空)", 502);
String contentType = response.headers().firstValue("Content-Type").orElse("audio/mpeg").split(";", 2)[0].trim().toLowerCase(java.util.Locale.ROOT);
String contentType = response.headers().firstValue("Content-Type").orElse("application/octet-stream").split(";", 2)[0].trim().toLowerCase(java.util.Locale.ROOT);
if (contentType.equals("application/octet-stream")) contentType = switch (String.valueOf(body.get("response_format"))) {
case "wav" -> "audio/wav";
case "opus" -> "audio/ogg";
case "aac" -> "audio/aac";
case "flac" -> "audio/flac";
default -> "audio/mpeg";
};
if (!contentType.startsWith("audio/")) throw new ApiException("UPSTREAM_INVALID_MEDIA", message + "(返回内容不是音频)", 502);
Map<String,Object> result = new LinkedHashMap<>();
result.put("base64", Base64.getEncoder().encodeToString(bytes));
@@ -377,6 +401,10 @@ public class AsyncProviderClient {
return ProviderRequestException.http(status, message);
}
private void requireTaskType(String actual, String expected) {
if (!expected.equalsIgnoreCase(actual)) throw new ApiException("ASYNC_PROVIDER_NOT_IMPLEMENTED", "渠道专用执行接口与任务类型不匹配", 501);
}
private ProviderRequestException requestFailure(Exception exception, boolean sending, String message) {
if (exception instanceof InterruptedException) Thread.currentThread().interrupt();
if (exception instanceof ProviderRequestException failure) return failure;
@@ -0,0 +1,87 @@
package com.moyi.controlplane.task;
import java.math.BigDecimal;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
/** Bounded envelope normalization only. No upstream calls, credentials, routing or settlement. */
final class AsyncTaskResponseNormalizer {
private AsyncTaskResponseNormalizer() {}
static String status(Map<String,Object> response) {
Object raw = response.get("status");
if (raw == null) raw = nested(response).get("status");
return switch (String.valueOf(raw == null ? "running" : raw).trim().toLowerCase(Locale.ROOT)) {
case "success", "succeeded", "completed" -> "succeeded";
case "failed", "failure", "error", "banned" -> "failed";
case "cancelled", "canceled" -> "cancelled";
default -> "running";
};
}
static int progress(Map<String,Object> response, String status) {
if ("succeeded".equals(status)) return 100;
Object raw = response.get("progress");
if (raw == null) raw = nested(response).get("progress");
try {
String value = String.valueOf(raw).trim();
if (value.length() > 32) return 0;
if (value.endsWith("%")) value = value.substring(0, value.length() - 1);
return new BigDecimal(value).max(BigDecimal.ZERO).min(BigDecimal.valueOf(100)).intValue();
} catch (NumberFormatException error) { return 0; }
}
static Map<String,Object> result(Map<String,Object> response) {
Map<?,?> data = nested(response);
for (String field : List.of("output", "content", "result")) {
Map<String,Object> result = asset(response.get(field));
if (result == null) result = asset(data.get(field));
if (result != null) {
if (data.get("credits_consumed") != null) result.put("creditsConsumed", data.get("credits_consumed"));
return result;
}
}
Map<String,Object> direct = asset(response);
return direct != null ? direct : asset(data);
}
private static Map<?,?> nested(Map<String,Object> response) {
return response.get("data") instanceof Map<?,?> data ? data : Map.of();
}
private static Map<String,Object> asset(Object raw) {
// A bare string is only a media reference when it is a URL; provider messages are not assets.
if (raw instanceof String value) return value.matches("(?i)(https?://|data:).+") ? new LinkedHashMap<>(Map.of("url", value)) : null;
if (!(raw instanceof Map<?,?> source)) return null;
// Select assets explicitly: task metadata, provider errors or arbitrary content are not media.
var result = new LinkedHashMap<String,Object>();
for (String field : List.of("url", "modelUrl", "previewUrl", "imageUrl", "videoUrl", "audioUrl", "base64", "b64_json")) {
Object value = source.get(field);
if (value instanceof String text && !text.isBlank()) result.put(field, text);
}
String[][] aliases = {{"model_url","modelUrl"},{"rendered_image_url","previewUrl"},
{"generated_image_url","imageUrl"},{"image_url","imageUrl"},{"video_url","videoUrl"},
{"audio_url","audioUrl"},{"last_frame_url","previewUrl"},{"preview_url","previewUrl"}};
for (String[] alias : aliases) {
Object value = source.get(alias[0]);
if (value instanceof Map<?,?> map) value = map.get("url");
if (value instanceof String text && !text.isBlank()) result.putIfAbsent(alias[1], text);
}
for (String kind : List.of("video","audio","image")) {
if (source.get(kind) instanceof Map<?,?> map && map.get("url") instanceof String text && !text.isBlank()) {
result.putIfAbsent(kind + "Url", text);
}
}
if (result.isEmpty() || result.keySet().stream().allMatch("previewUrl"::equals)) return null;
for (String field : List.of("format","mimeType","mime_type","creditsConsumed","credits_consumed")) {
if (source.get(field) instanceof String || source.get(field) instanceof Number) result.put(field, source.get(field));
}
for (String field : List.of("usage","usageMetadata")) if (source.get(field) instanceof Map<?,?> value) result.put(field, value);
for (String field : List.of("modelUrl","videoUrl","audioUrl","imageUrl")) {
if (!result.containsKey("url") && result.containsKey(field)) result.put("url", result.get(field));
}
return result;
}
}
@@ -392,10 +392,12 @@ public class AsyncTaskService {
task.put("id", resultSet.getString("id"));
task.put("provider", "upstream");
task.put("model", resultSet.getString("model_id"));
task.put("taskType", resultSet.getString("task_type"));
task.put("status", resultSet.getString("status"));
task.put("progress", resultSet.getInt("progress"));
task.put("createdAt", resultSet.getObject("created_at", OffsetDateTime.class));
task.put("updatedAt", resultSet.getObject("updated_at", OffsetDateTime.class));
putIfPresent(task, "completedAt", resultSet, "completed_at");
task.put("requiresReconciliation", "reconciling".equals(resultSet.getString("status")));
putIfPresent(task, "costAmount", resultSet, "cost_amount");
String result = resultSet.getString("result");
@@ -483,9 +485,15 @@ public class AsyncTaskService {
updatePollingState(taskId, null, "UPSTREAM_TASK_POLL_RETRY", message);
return;
}
String status = normalizedStatus(response);
int progress = progress(response, status);
Map<String,Object> result = result(response);
String status = AsyncTaskResponseNormalizer.status(response);
int progress = AsyncTaskResponseNormalizer.progress(response, status);
Map<String,Object> result = AsyncTaskResponseNormalizer.result(response);
String taskType = jdbc.queryForObject("SELECT task_type FROM model_tasks WHERE id=?", String.class, taskId);
if ("succeeded".equals(status) && result == null && List.of("video", "audio").contains(taskType)) {
// Generation success is not proof that the media bytes were delivered. Preserve billing
// semantics, never resubmit/refund automatically, and expose the missing delivery explicitly.
result = new LinkedHashMap<>(Map.of("deliveryStatus", "pending", "deliveryCode", "TASK_RESULT_UNAVAILABLE"));
}
if(result!=null) {
result=new LinkedHashMap<>(result);
result.putAll(usageOnly(response));
@@ -518,63 +526,6 @@ public class AsyncTaskService {
throw new IllegalStateException("Persisted task timestamp is missing");
}
private String normalizedStatus(Map<String,Object> response) {
Object raw = response.get("status");
if (raw == null && response.get("data") instanceof Map<?,?> data) raw = data.get("status");
String value = String.valueOf(raw == null ? "running" : raw).toLowerCase(java.util.Locale.ROOT);
return switch (value) {
case "success", "succeeded", "completed" -> "succeeded";
case "failed", "failure", "error", "banned" -> "failed";
case "cancelled", "canceled" -> "cancelled";
default -> "running";
};
}
private int progress(Map<String,Object> response, String status) {
Object raw = response.get("progress");
if (raw == null && response.get("data") instanceof Map<?,?> data) raw = data.get("progress");
if (raw instanceof Number number) return Math.max(0, Math.min(100, number.intValue()));
return "succeeded".equals(status) ? 100 : 0;
}
private Map<String,Object> result(Map<String,Object> response) {
Object raw = response.get("output");
Map<?,?> data = response.get("data") instanceof Map<?,?> value ? value : null;
if (raw == null && data != null) raw = data.get("output");
if (raw instanceof Map<?,?> map) {
Map<String,Object> value = new LinkedHashMap<>();
map.forEach((k,v)->value.put(String.valueOf(k),v));
copyAlias(value, "model_url", "modelUrl");
copyAlias(value, "rendered_image_url", "previewUrl");
copyAlias(value, "generated_image_url", "imageUrl");
copyAlias(value, "video_url", "videoUrl");
copyAlias(value, "audio_url", "audioUrl");
copyAlias(value, "image_url", "imageUrl");
if (value.get("url") == null && value.get("modelUrl") != null) value.put("url", value.get("modelUrl"));
if (value.get("url") == null && value.get("imageUrl") != null) value.put("url", value.get("imageUrl"));
if (value.get("url") == null && value.get("videoUrl") != null) value.put("url", value.get("videoUrl"));
if (value.get("url") == null && value.get("audioUrl") != null) value.put("url", value.get("audioUrl"));
if (data != null && data.get("credits_consumed") != null) value.put("creditsConsumed", data.get("credits_consumed"));
return value;
}
if (raw instanceof String value && !value.isBlank()) return Map.of("url", value);
// OpenAI-compatible video jobs return the media URL at the top level
// (or in a nested video object) rather than under `output`.
Object directUrl = response.get("url");
if (directUrl == null) directUrl = response.get("video_url");
if (directUrl == null) directUrl = response.get("image_url");
if (directUrl == null) directUrl = response.get("audio_url");
if (directUrl == null && response.get("video") instanceof Map<?,?> video) directUrl = video.get("url");
if (directUrl == null && response.get("image") instanceof Map<?,?> image) directUrl = image.get("url");
if (directUrl == null && response.get("audio") instanceof Map<?,?> audio) directUrl = audio.get("url");
if (directUrl != null && !String.valueOf(directUrl).isBlank()) return Map.of("url", directUrl);
return null;
}
private void copyAlias(Map<String,Object> value, String source, String target) {
if (value.get(target) == null && value.get(source) != null) value.put(target, value.get(source));
}
private String writeJson(Object value) { try { return objectMapper.writeValueAsString(value); } catch (JsonProcessingException exception) { return "{}"; } }
private long estimateInputUnits(Map<String,Object> input) {
@@ -450,7 +450,12 @@ public class UsageAuditService {
}
private String row(Object... values) { List<String> cells = new ArrayList<>(); for (Object value : values) cells.add(csv(value)); return String.join(",", cells); }
private String csv(Object value) { String text = value == null ? "" : String.valueOf(value); return "\"" + text.replace("\"", "\"\"").replace("\r", " ").replace("\n", " ") + "\""; }
static String csv(Object value) {
String text = value == null ? "" : String.valueOf(value).replace("\r", " ").replace("\n", " ");
// Prompts, outputs and key names are user-controlled; never let a spreadsheet evaluate them.
if (text.stripLeading().matches("^[=+@\\-].*") || text.startsWith("\t")) text = "'" + text;
return "\"" + text.replace("\"", "\"\"") + "\"";
}
private UUID parseUuid(String value) { try { return UUID.fromString(value); } catch (Exception exception) { throw new ApiException("INVALID_ID", "记录 ID 格式无效"); } }
private String blankToNull(String value) { return value == null || value.isBlank() ? null : value; }
private OffsetDateTime now() { return OffsetDateTime.now(ZoneOffset.UTC); }
@@ -48,6 +48,15 @@ public class ChatBillingService {
jdbc.update("UPDATE chat_billing_requests SET settlement_usage=?::jsonb, settlement_latency_ms=?, settlement_first_token_ms=?, expires_at=GREATEST(expires_at,now()+interval '5 minutes') WHERE id=? AND status='pending'",
encode(normalizedUsage), Math.max(0,latencyMs), firstTokenMs, ticket.id());
}
/**
* Extend a still-running call's hold so expiry recovery cannot release it mid-call.
* Only pending tickets move, and the hold never shrinks below its current expiry.
*/
public boolean renew(UUID id, long holdMs) {
long seconds=Math.max(60,(holdMs+999)/1000);
return jdbc.update("UPDATE chat_billing_requests SET expires_at=GREATEST(expires_at,now()+make_interval(secs=>?)) WHERE id=? AND status='pending'",seconds,id)>0;
}
@Transactional public void actualRoute(Ticket ticket,Map<String,Object> route) {
var snapshots=jdbc.queryForList("SELECT procurement_pricing FROM model_price_history WHERE model_id=? ORDER BY id DESC LIMIT 1",route.get("id"));
Object cost=snapshots.isEmpty()?null:snapshots.getFirst().get("procurement_pricing");
@@ -56,6 +56,12 @@ moyi:
# provider's own response/limit.
disable-model-concurrency: ${GATEWAY_DISABLE_MODEL_CONCURRENCY:false}
queue-wait-ms: ${GATEWAY_QUEUE_WAIT_MS:30000}
# Synchronous/stream upstream deadlines (seconds). Total is capped at 3600; first-byte and idle
# default to the total and never exceed it. Running calls renew wallet holds and concurrency
# leases, but a reverse proxy in front must also allow the longer read time.
upstream-timeout-seconds: ${GATEWAY_UPSTREAM_TIMEOUT_SECONDS:90}
upstream-first-byte-timeout-seconds: ${GATEWAY_UPSTREAM_FIRST_BYTE_TIMEOUT_SECONDS:${GATEWAY_UPSTREAM_TIMEOUT_SECONDS:90}}
upstream-idle-timeout-seconds: ${GATEWAY_UPSTREAM_IDLE_TIMEOUT_SECONDS:${GATEWAY_UPSTREAM_TIMEOUT_SECONDS:90}}
queue-poll-ms: ${GATEWAY_QUEUE_POLL_MS:100}
tasks:
max-queued-per-model: ${TASK_MAX_QUEUED_PER_MODEL:100}
@@ -1,19 +1,40 @@
-- KEYS: model slots, key slots, model waiting (score = wait expiry), model arrival order (score = first arrival).
-- ARGV: model limit, key limit, slot ttl, token, remaining wait ms, waiting ttl.
-- Returns 1 admitted, 0 model busy or behind earlier waiters, 2 own API Key busy.
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)
-- Expired waiters (crashed or timed-out callers) must also leave the arrival order.
local expired = redis.call('ZRANGEBYSCORE', KEYS[3], '-inf', now)
for _, member in ipairs(expired) do redis.call('ZREM', KEYS[4], member) 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])
local wait = tonumber(ARGV[5])
local function remember_waiting()
if wait > 0 then
redis.call('ZADD', KEYS[3], now + wait, ARGV[4])
redis.call('PEXPIRE', KEYS[3], ARGV[6])
end
return busy
end
-- A waiter blocked only by its own API Key limit does not hold a place ahead of other keys.
if redis.call('ZCARD', KEYS[2]) >= tonumber(ARGV[2]) then
redis.call('ZREM', KEYS[4], ARGV[4])
remember_waiting()
return 2
end
local ahead = redis.call('ZRANK', KEYS[4], ARGV[4])
if not ahead then ahead = redis.call('ZCARD', KEYS[4]) end
-- FIFO: a free model slot belongs to earlier model waiters before a newer request.
if redis.call('ZCARD', KEYS[1]) + ahead >= tonumber(ARGV[1]) then
if wait > 0 then
redis.call('ZADD', KEYS[4], 'NX', now, ARGV[4])
redis.call('PEXPIRE', KEYS[4], ARGV[6])
end
remember_waiting()
return 0
end
redis.call('ZREM', KEYS[3], ARGV[4])
redis.call('ZREM', KEYS[4], 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])
@@ -125,10 +125,16 @@ class PlatformAdminPostgresTest {
assertFalse(catalog.keyCanInvoke(key,secondId));
assertFalse(catalog.keyCanInvoke(key,modelId));
assertThrows(ApiException.class, () -> catalog.updateKeyModels(ownerToken,keyId,List.of(secondId),auth));
var edited = catalog.updateProvider(channelId,Map.of("name","Relay Updated","baseUrl","https://example.test/v2","credentialSecret",""),ownerToken,auth);
// The stored key must never follow a changed endpoint without being re-entered.
assertEquals("CREDENTIAL_REENTRY_REQUIRED", assertThrows(ApiException.class, () -> catalog.updateProvider(channelId,Map.of("baseUrl","https://attacker.test/v1","credentialSecret",""),ownerToken,auth)).getCode());
assertEquals("https://example.test/v1",jdbc.queryForObject("SELECT base_url FROM model_providers WHERE id=?",String.class,channelId));
var edited = catalog.updateProvider(channelId,Map.of("name","Relay Updated","baseUrl","https://example.test/v1/","credentialSecret",""),ownerToken,auth);
assertEquals("Relay Updated",edited.get("name"));
assertFalse(edited.containsKey("credentialSecret"));
assertEquals(cipher,jdbc.queryForObject("SELECT credential_ciphertext FROM model_providers WHERE id=?",String.class,channelId));
catalog.updateProvider(channelId,Map.of("baseUrl","https://example.test/v2","credentialSecret","replacement-test-secret"),ownerToken,auth);
assertNotEquals(cipher,jdbc.queryForObject("SELECT credential_ciphertext FROM model_providers WHERE id=?",String.class,channelId));
assertTrue(jdbc.queryForObject("SELECT string_agg(details::text,' ') FROM auth_audit_log WHERE event_type='provider.updated'",String.class).contains("previousBaseUrl"));
catalog.saveDefinition(modelId,Map.of("publicName","Renamed"),ownerToken,auth);
assertEquals("Renamed",catalog.activeModel(firstId).get("publicName"));
catalog.updateModel(firstId,Map.of("sourceLabel","Edited Group","upstreamModel","upstream-c","maxConcurrency",2),ownerToken,auth);
@@ -160,6 +166,10 @@ class PlatformAdminPostgresTest {
var cookie = new Cookie("moyi_session",memberToken);
var response = mvc.perform(get("/api/models").cookie(cookie)).andExpect(status().isOk()).andReturn();
var payload = json.readTree(response.getResponse().getContentAsString());
// Members see channel identity only, never endpoints or credential hints.
assertTrue(payload.get("providers").size() > 0);
assertTrue(payload.get("providers").findValues("baseUrl").isEmpty());
assertTrue(payload.get("providers").findValues("credentialMasked").isEmpty());
assertEquals(catalog.listDefinitions(false).size(),payload.get("logicalModels").size());
assertTrue(payload.get("logicalModels").size() > 250);
assertTrue(payload.get("logicalModels").findValuesAsText("id").contains(pending.get("id")));
@@ -440,6 +450,22 @@ class PlatformAdminPostgresTest {
assertEquals(0,jdbc.queryForObject("SELECT count(*) FROM wallet_entries WHERE operation='usage'",Integer.class));
assertEquals(2,jdbc.queryForObject("SELECT count(*) FROM usage_events WHERE status='failed' AND cost_amount=0",Integer.class));
}
@Test void renewedRunningCallIsNotReleasedByExpiryRecovery() {
var f=chatFixture();
wallet.grant(ownerToken,memberId,UUID.randomUUID(),"1","renewal test",meta);
var running=f.reserve(null);
jdbc.update("UPDATE chat_billing_requests SET expires_at=now()+interval '5 seconds' WHERE id=?",running.id());
assertTrue(f.billing().renew(running.id(),1_800_000));
assertTrue(jdbc.queryForObject("SELECT expires_at>now()+interval '29 minutes' FROM chat_billing_requests WHERE id=?",Boolean.class,running.id()));
// Renewal never shortens an existing hold.
assertTrue(f.billing().renew(running.id(),60_000));
assertTrue(jdbc.queryForObject("SELECT expires_at>now()+interval '29 minutes' FROM chat_billing_requests WHERE id=?",Boolean.class,running.id()));
f.billing().recoverExpired();
assertEquals("pending",jdbc.queryForObject("SELECT status FROM chat_billing_requests WHERE id=?",String.class,running.id()));
assertTrue(jdbc.queryForObject("SELECT reserved_balance FROM user_wallets WHERE user_id=?",BigDecimal.class,memberId).signum()>0);
f.billing().fail(running,"TEST_DONE","done",1);
assertFalse(f.billing().renew(running.id(),1_800_000));
}
@Test void unexpectedProviderOverrunIsRecordedAsDebtAndBlocksNewCalls() {
var f=chatFixture();
wallet.grant(ownerToken,memberId,UUID.randomUUID(),"1","billing test",meta);
@@ -25,7 +25,15 @@ class CatalogServiceTest {
assertTrue(CatalogService.asyncExecutorReady("provider-bytedance", "http", "video"));
assertTrue(CatalogService.asyncExecutorReady("relay-provider", "openai-compatible", "image"));
assertTrue(CatalogService.asyncExecutorReady("relay-provider", "OPENAI-COMPATIBLE", "video"));
assertTrue(CatalogService.asyncExecutorReady("relay-provider", "openai-compatible", "audio"));
// Audio readiness depends on the upstream endpoint, not on the model type alone.
assertTrue(CatalogService.asyncExecutorReady("relay-provider", "openai-compatible", "audio", "tts-1"));
assertTrue(CatalogService.asyncExecutorReady("relay-provider", "openai-compatible", "audio", "gpt-4o-mini-tts-2025-03-20"));
assertFalse(CatalogService.asyncExecutorReady("relay-provider", "openai-compatible", "audio"));
assertFalse(CatalogService.asyncExecutorReady("relay-provider", "openai-compatible", "audio", "whisper-1"));
assertFalse(CatalogService.asyncExecutorReady("relay-provider", "openai-compatible", "audio", "gpt-4o-realtime-preview"));
// Dedicated adapters only execute the task type they implement.
assertFalse(CatalogService.asyncExecutorReady("provider-vast", "http", "video"));
assertFalse(CatalogService.asyncExecutorReady("provider-bytedance", "http", "3d"));
assertFalse(CatalogService.asyncExecutorReady("relay-provider", "openai-compatible", "3d"));
assertFalse(CatalogService.asyncExecutorReady("relay-provider", "http", "video"));
}
@@ -0,0 +1,95 @@
package com.moyi.controlplane.gateway;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
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.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.StringRedisTemplate;
import static org.junit.jupiter.api.Assertions.*;
@EnabledIfEnvironmentVariable(named="MOYI_GATEWAY_TEST_REDIS_PORT", matches="[0-9]{2,5}")
class FairAdmissionRedisTest {
private LettuceConnectionFactory factory;
private StringRedisTemplate redis;
private final String prefix = "moyi:test:" + UUID.randomUUID() + ":";
private final Map<String,Object> model = Map.of("id", "fair-model", "maxConcurrency", 1);
@BeforeEach void setup() {
factory = new LettuceConnectionFactory("127.0.0.1", Integer.parseInt(System.getenv("MOYI_GATEWAY_TEST_REDIS_PORT")));
factory.afterPropertiesSet(); factory.start();
redis = new StringRedisTemplate(factory); redis.afterPropertiesSet();
}
@AfterEach void cleanup() {
if (redis != null) redis.delete(redis.keys(prefix + "*"));
if (factory != null) factory.destroy();
}
private GatewayGuard guard(long waitMs) { return new GatewayGuard(redis, 60, 300, 1, 4, waitMs, 10, prefix); }
@Test void newRequestCannotOvertakeAnEarlierWaiter() throws Exception {
var guard = guard(5000);
var holder = guard.acquire(model, UUID.randomUUID(), 5000);
var earlier = CompletableFuture.supplyAsync(() -> guard.acquire(model, UUID.randomUUID(), 5000));
// The earlier waiter is queued before the slot is released.
long until = System.nanoTime() + TimeUnit.SECONDS.toNanos(2);
while (zcard("order") == 0 && System.nanoTime() < until) Thread.sleep(5);
assertEquals(1L, zcard("order"));
holder.close();
// A newcomer that does not wait finds the free slot reserved for the earlier waiter.
var newcomer = assertThrows(GatewayLimitException.class, () -> guard(0).acquire(model, UUID.randomUUID(), 0));
assertEquals("MODEL_BUSY", newcomer.getCode());
try (var admitted = earlier.get(3, TimeUnit.SECONDS)) {
assertEquals(0L, zcard("order"));
assertEquals(0L, zcard("waiting"));
}
}
@Test void timedOutWaiterLeavesTheQueueAndDoesNotBlockLaterRequests() throws Exception {
var holder = guard(5000).acquire(model, UUID.randomUUID(), 5000);
assertThrows(GatewayLimitException.class, () -> guard(30).acquire(model, UUID.randomUUID(), 30));
assertEquals(0L, zcard("order"));
assertEquals(0L, zcard("waiting"));
holder.close();
try (var next = guard(0).acquire(model, UUID.randomUUID(), 0)) { assertNotNull(next); }
}
@Test void waiterBlockedByItsOwnKeyDoesNotHoldTheModelQueue() throws Exception {
UUID busyKey = UUID.randomUUID();
var keyGuard = new GatewayGuard(redis, 60, 300, 2, 1, 5000, 10, prefix);
var unlimitedModel = Map.<String,Object>of("id", "fair-model", "maxConcurrency", 2);
var keyHolder = keyGuard.acquire(unlimitedModel, busyKey, 5000);
var blockedByKey = CompletableFuture.supplyAsync(() -> keyGuard.acquire(unlimitedModel, busyKey, 200));
Thread.sleep(50);
// Another key may use the remaining model slot while the first key is at its own limit.
try (var other = keyGuard.acquire(unlimitedModel, UUID.randomUUID(), 0)) { assertNotNull(other); }
var failure = assertThrows(java.util.concurrent.ExecutionException.class, () -> blockedByKey.get(3, TimeUnit.SECONDS));
assertEquals("KEY_BUSY", ((GatewayLimitException) failure.getCause()).getCode());
keyHolder.close();
}
@Test void renewalExtendsOnlyLeasesStillHeld() throws Exception {
var admission = guard(0).acquire(model, UUID.randomUUID(), 0);
String slots = prefix + "gateway:slots:model:fair-model";
String token = redis.opsForZSet().range(slots, 0, 0).iterator().next();
double before = redis.opsForZSet().score(slots, token);
assertTrue(admission.renew(1_800_000));
assertTrue(redis.opsForZSet().score(slots, token) > before + 1_000_000);
redis.opsForZSet().remove(slots, token);
// A lost lease is reported, never silently recreated on top of another caller's capacity.
assertFalse(admission.renew(1_800_000));
assertNull(redis.opsForZSet().score(slots, token));
admission.close();
assertFalse(admission.renew(1_800_000));
}
private long zcard(String kind) {
Long value = redis.opsForZSet().zCard(prefix + "gateway:" + kind + ":model:fair-model");
return value == null ? 0 : value;
}
}
@@ -96,6 +96,26 @@ class GatewayRequestExecutionServiceTest {
verify(admission).close();
}
@Test
void runningCallRenewsItsHoldAndLeaseUntilTheWorkReturns() throws Exception {
ChatBillingService billing = mock(ChatBillingService.class);
var ticket = new ChatBillingService.Ticket(UUID.randomUUID(), userId, null);
when(billing.reserve(key, model, normalized, null, context.metadata())).thenReturn(ticket);
var service = new GatewayRequestExecutionService(billing, new ApiRequestRiskPolicy(ApiRequestRiskEngine.allowAll()), 20);
ApiRequestAdmission admission = mock(ApiRequestAdmission.class);
when(admission.renew(anyLong())).thenReturn(true);
try {
assertEquals("done", service.execute(context, key, model, normalized, null, () -> admission,
ignored -> { Thread.sleep(150); return "done"; }, failure -> new ApiException("CALL_FAILED", "failed", 502), ApiException::getCode));
verify(billing, atLeast(2)).renew(eq(ticket.id()), anyLong());
verify(admission, atLeast(2)).renew(anyLong());
// Renewal stops with the call; a finished request must not keep its hold alive.
clearInvocations(billing);
Thread.sleep(100);
verify(billing, never()).renew(any(), anyLong());
} finally { service.shutdown(); }
}
@Test
void workFailureIsRecordedAndAdmissionLeaseCloses() {
ChatBillingService billing = mock(ChatBillingService.class);
@@ -0,0 +1,85 @@
package com.moyi.controlplane.provider;
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.ArrayList;
import java.util.List;
import java.util.Map;
import org.junit.jupiter.api.Test;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.mock.env.MockEnvironment;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.mock;
/** Long streams survive while bytes flow; a silent upstream ends at the idle window, not the total deadline. */
class StreamDeadlineTest {
private OpenAiCompatibleProvider provider(HttpServer server, int total, int idle) {
var env = new MockEnvironment()
.withProperty("moyi.providers.deepseek.api-key", "local-fake-key")
.withProperty("moyi.providers.deepseek.base-url", "http://127.0.0.1:" + server.getAddress().getPort() + "/v1")
.withProperty("moyi.gateway.upstream-timeout-seconds", Integer.toString(total))
.withProperty("moyi.gateway.upstream-idle-timeout-seconds", Integer.toString(idle));
return new OpenAiCompatibleProvider(new ObjectMapper(), env, mock(JdbcTemplate.class), new SecretCipher("local-test-only"));
}
private HttpServer sse(long gapMs, int events, boolean finish) throws Exception {
var server = HttpServer.create(new InetSocketAddress("127.0.0.1", 0), 0);
server.createContext("/v1/chat/completions", exchange -> {
exchange.getResponseHeaders().set("Content-Type", "text/event-stream");
exchange.sendResponseHeaders(200, 0);
try (var out = exchange.getResponseBody()) {
for (int i = 0; i < events; i++) {
out.write(("data: {\"i\":" + i + "}\n\n").getBytes(StandardCharsets.UTF_8));
out.flush();
Thread.sleep(gapMs);
}
if (finish) out.write("data: [DONE]\n\n".getBytes(StandardCharsets.UTF_8));
else Thread.sleep(5_000);
} catch (Exception ignored) { }
});
server.start();
return server;
}
@Test void streamLongerThanTheIdleWindowSucceedsWhileEventsKeepArriving() throws Exception {
var server = sse(400, 8, true);
var provider = provider(server, 10, 1);
try (var stream = provider.stream("provider-deepseek", "model", Map.of("messages", List.of()))) {
var chunks = new ArrayList<Map<String,Object>>();
long started = System.nanoTime();
stream.events(chunks::add);
assertEquals(8, chunks.size());
assertTrue(System.nanoTime() - started > 2_000_000_000L, "the stream outlived the 1s idle window");
} finally { provider.close(); server.stop(0); }
}
@Test void silentUpstreamTimesOutAtTheIdleWindow() throws Exception {
var server = sse(0, 1, false);
var provider = provider(server, 10, 1);
try (var stream = provider.stream("provider-deepseek", "model", Map.of("messages", List.of()))) {
long started = System.nanoTime();
var error = assertThrows(ApiException.class, () -> stream.events(chunk -> { }));
assertEquals("UPSTREAM_PROVIDER_TIMEOUT", error.getCode());
long elapsedMs = (System.nanoTime() - started) / 1_000_000;
assertTrue(elapsedMs < 4_000, "ended by the idle window, not the 10s total: " + elapsedMs);
} finally { provider.close(); server.stop(0); }
}
@Test void totalDeadlineIsCappedAndIdleNeverExceedsIt() throws Exception {
var server = sse(0, 1, true);
var env = new MockEnvironment()
.withProperty("moyi.providers.deepseek.api-key", "local-fake-key")
.withProperty("moyi.providers.deepseek.base-url", "http://127.0.0.1:" + server.getAddress().getPort() + "/v1")
.withProperty("moyi.gateway.upstream-timeout-seconds", "999999")
.withProperty("moyi.gateway.upstream-idle-timeout-seconds", "999999");
var provider = new OpenAiCompatibleProvider(new ObjectMapper(), env, mock(JdbcTemplate.class), new SecretCipher("local-test-only"));
try {
assertEquals(OpenAiCompatibleProvider.MAX_TIMEOUT_SECONDS, org.springframework.test.util.ReflectionTestUtils.getField(provider, "timeoutSeconds"));
assertEquals(1000L * OpenAiCompatibleProvider.MAX_TIMEOUT_SECONDS, org.springframework.test.util.ReflectionTestUtils.getField(provider, "idleTimeoutMs"));
} finally { provider.close(); server.stop(0); }
}
}
@@ -340,6 +340,53 @@ class AsyncProviderClientTest {
} finally { server.stop(0); }
}
@Test void nonSpeechAudioAndInvalidSpeechInputNeverReachTheProvider() throws Exception {
var calls = new java.util.concurrent.atomic.AtomicInteger();
HttpServer server = HttpServer.create(new InetSocketAddress("127.0.0.1", 0), 0);
server.createContext("/", exchange -> { calls.incrementAndGet(); exchange.sendResponseHeaders(500, -1); exchange.close(); }); server.start();
try {
var client = relayClient(server);
for (String model : java.util.List.of("whisper-1", "gpt-4o-realtime-preview", "suno-v4")) {
assertEquals("ASYNC_PROVIDER_NOT_IMPLEMENTED", assertThrows(ApiException.class, () -> client.submit("relay", model, "audio", Map.of("prompt", "hi"))).getCode());
}
for (Map<String,Object> input : java.util.List.<Map<String,Object>>of(Map.of("prompt", ""), Map.of("prompt", "x".repeat(4097)),
Map.of("prompt", "hi", "response_format", "pcm"), Map.of("prompt", "hi", "voice", 7),
Map.of("prompt", "hi", "speed", "9"), Map.of("prompt", "hi", "stream_format", "sse"))) {
assertEquals("INVALID_TASK_INPUT", assertThrows(ApiException.class, () -> client.submit("relay", "tts-1", "audio", input)).getCode());
}
assertEquals(0, calls.get());
} finally { server.stop(0); }
}
@Test void speechWithoutAudioContentTypeUsesTheRequestedFormat() throws Exception {
HttpServer server = HttpServer.create(new InetSocketAddress("127.0.0.1", 0), 0);
server.createContext("/audio/speech", exchange -> {
String body = new String(exchange.getRequestBody().readAllBytes(), StandardCharsets.UTF_8);
assertTrue(body.contains("\"speed\":1.5"));
exchange.getResponseHeaders().set("Content-Type", "application/octet-stream");
byte[] audio = "fake-wav".getBytes(StandardCharsets.UTF_8);
exchange.sendResponseHeaders(200, audio.length);
try (var output = exchange.getResponseBody()) { output.write(audio); }
});
server.start();
try {
var result = relayClient(server).submit("relay", "gpt-4o-mini-tts", "audio", Map.of("prompt", "hi", "response_format", "wav", "speed", 1.5));
assertEquals("audio/wav", ((Map<?,?>) result.get("_moyiInlineResult")).get("mimeType"));
} finally { server.stop(0); }
}
@Test void dedicatedAdaptersRejectMismatchedTaskTypesBeforeSending() {
var client = clientFor("http://127.0.0.1:1");
assertEquals("ASYNC_PROVIDER_NOT_IMPLEMENTED", assertThrows(ApiException.class, () -> client.submit("provider-vast", "tripo", "video", Map.of("prompt", "x"))).getCode());
}
private AsyncProviderClient relayClient(HttpServer server) {
var jdbc = mock(JdbcTemplate.class); var cipher = new SecretCipher("test-encryption");
when(jdbc.queryForMap(any(String.class), eq("relay"))).thenReturn(Map.of("code", "relay", "protocol", "openai-compatible",
"base_url", "http://127.0.0.1:" + server.getAddress().getPort(), "credential_ciphertext", cipher.encrypt("test-key")));
return new AsyncProviderClient(jdbc, cipher, new ObjectMapper(), mock(Environment.class));
}
private AsyncProviderClient clientFor(String baseUrl) {
JdbcTemplate jdbc = mock(JdbcTemplate.class);
SecretCipher cipher = new SecretCipher("test-encryption");
@@ -422,6 +422,30 @@ class AsyncTaskPostgresTest {
assertEquals(new BigDecimal("8.56000000"), wallet("balance"));
}
@Test void finishedVideoWithoutDeliveredFileIsChargedOnceAndMarkedUndelivered() {
jdbc.update("UPDATE model_tasks SET task_type='video' WHERE id=?", taskId);
when(provider.poll("provider-vast", "model-mock-3d", "v3.1-20260211", "upstream-task", null))
.thenReturn(Map.of("id", "upstream-task", "status", "completed", "model", "sora-2"));
poll(); poll();
assertEquals("succeeded", status());
assertEquals(new BigDecimal("8.56000000"), wallet("balance"));
assertEquals(0, wallet("reserved_balance").signum());
var view = tasks.getForKey(Map.of("id", keyId.toString()), taskId);
assertEquals("video", view.get("taskType"));
assertEquals("pending", ((Map<?,?>) view.get("result")).get("deliveryStatus"));
verify(provider, never()).submit(anyString(), anyString(), anyString(), anyString(), anyMap());
}
@Test void seedanceContentVideoUrlIsPersistedAsTheTaskResult() {
jdbc.update("UPDATE model_tasks SET task_type='video' WHERE id=?", taskId);
when(provider.poll("provider-vast", "model-mock-3d", "v3.1-20260211", "upstream-task", null))
.thenReturn(Map.of("status", "succeeded", "content", Map.of("video_url", "https://example.test/v.mp4?sig=1")));
poll();
var result = (Map<?,?>) tasks.getForKey(Map.of("id", keyId.toString()), taskId).get("result");
assertEquals("https://example.test/v.mp4?sig=1", result.get("videoUrl"));
assertFalse(result.containsKey("deliveryStatus"));
}
@Test void confirmedUpstreamFailureReleasesHoldImmediately() {
when(provider.poll("provider-vast","model-mock-3d","v3.1-20260211","upstream-task",null)).thenReturn(Map.of("data",Map.of("status","failed")));
poll();
@@ -0,0 +1,53 @@
package com.moyi.controlplane.task;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNull;
import java.util.List;
import java.util.Map;
import org.junit.jupiter.api.Test;
class AsyncTaskResponseNormalizerTest {
@Test void seedanceContentVideoUrlIsAResult() {
var response = Map.<String,Object>of("id", "cgt-1", "status", "succeeded",
"content", Map.of("video_url", "https://cdn.example.test/v.mp4?sig=1", "last_frame_url", "https://cdn.example.test/f.png"),
"usage", Map.of("completion_tokens", 100));
var result = AsyncTaskResponseNormalizer.result(response);
assertEquals("https://cdn.example.test/v.mp4?sig=1", result.get("videoUrl"));
assertEquals("https://cdn.example.test/v.mp4?sig=1", result.get("url"));
assertEquals("https://cdn.example.test/f.png", result.get("previewUrl"));
assertEquals("succeeded", AsyncTaskResponseNormalizer.status(response));
}
@Test void nestedEnvelopesAndOpenAiVideoObjects() {
var tripo = Map.<String,Object>of("code", 0, "data", Map.of("status", "success", "progress", 100, "credits_consumed", 30,
"output", Map.of("model_url", "https://cdn.example.test/m.glb", "rendered_image_url", "https://cdn.example.test/p.png")));
var result = AsyncTaskResponseNormalizer.result(tripo);
assertEquals("https://cdn.example.test/m.glb", result.get("url"));
assertEquals(30, result.get("creditsConsumed"));
var openAi = Map.<String,Object>of("status", "completed", "video", Map.of("url", "https://cdn.example.test/o.mp4"));
assertEquals("https://cdn.example.test/o.mp4", AsyncTaskResponseNormalizer.result(openAi).get("url"));
var nestedUrl = Map.<String,Object>of("data", Map.of("status", "completed", "result", Map.of("video_url", Map.of("url", "https://cdn.example.test/n.mp4"))));
assertEquals("https://cdn.example.test/n.mp4", AsyncTaskResponseNormalizer.result(nestedUrl).get("videoUrl"));
}
@Test void metadataOnlyResponsesAreNotMedia() {
// Task metadata, error text and a preview frame alone must not be presented as the result file.
assertNull(AsyncTaskResponseNormalizer.result(Map.of("id", "video-1", "status", "completed", "model", "sora-2")));
assertNull(AsyncTaskResponseNormalizer.result(Map.of("status", "completed", "content", List.of(Map.of("type", "text")))));
assertNull(AsyncTaskResponseNormalizer.result(Map.of("status", "completed", "result", "generation finished")));
assertNull(AsyncTaskResponseNormalizer.result(Map.of("status", "completed", "content", Map.of("last_frame_url", "https://cdn.example.test/f.png"))));
var result = AsyncTaskResponseNormalizer.result(Map.of("output", Map.of("url", "https://cdn.example.test/a.mp3", "error", "secret detail")));
assertFalse(result.containsKey("error"));
}
@Test void progressIsBoundedAndSuccessIsComplete() {
assertEquals(42, AsyncTaskResponseNormalizer.progress(Map.of("progress", "42%"), "running"));
assertEquals(100, AsyncTaskResponseNormalizer.progress(Map.of("progress", 250), "running"));
assertEquals(0, AsyncTaskResponseNormalizer.progress(Map.of("progress", "n/a"), "running"));
assertEquals(0, AsyncTaskResponseNormalizer.progress(Map.of(), "running"));
assertEquals(100, AsyncTaskResponseNormalizer.progress(Map.of("progress", 10), "succeeded"));
assertEquals("failed", AsyncTaskResponseNormalizer.status(Map.of("data", Map.of("status", " FAILED "))));
}
}
@@ -0,0 +1,16 @@
package com.moyi.controlplane.usage;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
class UsageCsvTest {
@Test void adminExportNeutralizesSpreadsheetFormulas() {
assertEquals("\"'=HYPERLINK(\"\"https://evil.test\"\")\"", UsageAuditService.csv("=HYPERLINK(\"https://evil.test\")"));
assertEquals("\"' @SUM(A1)\"", UsageAuditService.csv(" @SUM(A1)"));
assertEquals("\"'-1+2\"", UsageAuditService.csv("-1+2"));
assertEquals("\"'+cmd\"", UsageAuditService.csv("+cmd"));
assertEquals("\"plain text next\"", UsageAuditService.csv("plain text\r\nnext"));
assertEquals("\"\"", UsageAuditService.csv(null));
}
}
+18 -5
View File
@@ -1,6 +1,6 @@
# 安全与 CPR 差距任务追踪
最新增量审查日期:2026-10-22。对照用户粘贴的 CPR 比对报告与本地代码,不将建议清单视为已完成验收。
最新增量审查日期:2026-10-22。对照用户粘贴的 CPR 比对报告与本地代码,不将建议清单视为已完成验收。比对结论与优化方案全文见 `29-cpr-gap-analysis-and-optimization-plan.md`。
## 已交付基线
@@ -22,16 +22,23 @@
| SEC-05 | Provider 有界 HTTP Transport | 部分完成:异步 JSON/multipart/音频/媒体已接有界读取和总期限;同步/SSE 生命周期尚未统一抽取 | 超大 chunk、慢响应体、已观察拒绝状态、取消、重定向及原适配器回归 |
| SEC-06 | 媒体下载出站 / SSRF | 服务端参考图下载的代码级 DNS 绑定已实现并通过本地回归;生产网络隔离与实际部署验收仍未完成 | 一次解析、整组地址校验、固定地址直连、原域名 TLS/SNI、禁代理与跳转;109 项新增回归,见 26 号文档 |
| SEC-07 | 整体安全审查与生产验收 | 未完成:已补验证码/账号原子性;本轮进一步分离加密投递、统一已知/未知账号重置受理和限额,外部投递不再持 DB 事务;管理接口全审查、时序残余、默认配置、依赖与备份恢复仍需继续 | 本轮新增 22 项投递数据库回归,全量 390 项通过;真实邮件/短信与规格驱动验收未执行,禁止宣称“全仓库安全” |
| COD-01 | Codex 协议能力及验收矩阵 | 未实现完整兼容;保留有限文本 Responses 的明确拒绝语义 | 指定客户端版本、模型、工具和计费联合验收 |
| COD-02 | Responses WebSocket 与响应归属续接 | 未实现,不可宣传完整 Codex 支持 | 原账号/凭据版本归属、断线、取消、租约、费用一致性 |
| COD-03 | 长请求连接/空闲/总期限与续租 | 未完成,不只修改 90 秒常量 | 并发租约、钱包预留恢复、长流/取消共同验证 |
| COD-01 | Codex 协议能力及验收矩阵 | 暂缓(2026-10-22 决定):不允许上游存储,HTTP 续接不可行;恢复时与 Sub2API 对齐,见 30 号文档第 0 节。保留有限文本 Responses 的明确拒绝语义 | 指定客户端版本、模型、工具和计费联合验收 |
| COD-02 | Responses WebSocket 与响应归属续接 | 暂缓(2026-10-22 决定):增量续接唯一途径是 WebSocket,须同时改 nginx 并补连接管理;对齐清单见 30 号文档第 0 节,不可宣传完整 Codex 支持 | 原账号/凭据版本归属、断线、取消、租约、费用一致性 |
| COD-03 | 长请求连接/空闲/总期限与续租 | 部分完成:同步/流式上游拆分首字节、空闲、总期限(总期限上限 3600 秒,默认仍 90 秒);执行期间每 60 秒续期钱包预留与 Redis 并发租约,结束即停,丢失的租约不会被重建。部署侧 nginx `proxy_read_timeout 120s` 与云端 Compose 未透传新变量,放宽前须同步调整;原生请求 256 KiB / 32K 输出上限未动 | 并发租约、钱包预留恢复、长流/取消共同验证 |
| SCH-01 | 独立上游账号生命周期与调度 | 仅共享 Key 轮换/冷却/指纹,非完整账号实体 | 凭据版本、额度、模型范围、权重及解释能力 |
| SCH-02 | 公平排队 / 取消 / 共享等待预算 | 未完成,现有同步 Redis 竞争不保证 FIFO | 多客户端饥饿/FIFO/取消回收与横向并发验证 |
| SCH-02 | 公平排队 / 取消 / 共享等待预算 | 部分完成:模型+Key 配对准入改为模型级 FIFO(到达顺序 ZSET,过期/超时/放弃即出队;仅因自身 Key 满而等待者不占模型队位),真实 Redis 回归证明旧脚本会被插队;跨路由共享等待预算、优先级与横向压测仍缺 | 多客户端饥饿/FIFO/取消回收与横向并发验证 |
| OPS-01 | request/attempt 诊断、配置版本、质量门禁 | 部分:已有最终审计/健康/部分快照;本轮加依赖方向测试、CI/Dependabot 配置,远端未运行;逐 attempt/完整配置版本仍缺 | 本地门禁不等于 GitHub CI、CVE 扫描或真实协议验收 |
| OPS-02 | CPR 可选部署 / OAuth 账号池 | 未部署;属于可选方案,不默认新建生产基础设施 | 用户确认部署方式及授权来源后才接入;本地不发付费生成 |
| MED-01 | 音视频任务回执与结果规范化 | 本地已实现:独立 `AsyncTaskResponseNormalizer` 解析 `content`/`result`/嵌套 `data` 与 `{url}` 对象,仅白名单媒体字段;视频/音频完成但无文件时标记 `deliveryStatus=pending`,结算一次、不重发不退款。逐供应商实测未做 | 本地协议桩、结果回读与钱包只结算一次;逐供应商实测待授权 |
| MED-02 | 音频协议与执行能力准确标识 | 本地已实现:目录与执行共用 `AsyncMediaSupport.supportsSpeech` 白名单;非 TTS 音频、专用渠道错配任务类型、非法文本/音色/格式/语速在发送前拒绝。转写/音乐/Realtime 仍未接入 | 目录与实际执行共享判定,错误协议在发送前拒绝;TTS/非 TTS 回归 |
| MED-03 | 音视频结果展示与历史任务类型 | 本地已实现:任务响应返回持久 `taskType`/`completedAt`,前端以其优先于目录;补 opus/aac/flac MIME、签名 URL 后缀、尾帧预览与“结果未交付”提示 | 无目录历史任务、音频 base64、带签名视频 URL、错误响应前端回归 |
| MED-04 | 音视频用量计费及逐路由验收 | 未完成:音视频 Token/时长计费不可用固定单任务费代替,配置就绪不等于真实生成通过 | 每条路由分别核对协议、模型、用量、费用、播放;不猜报价,不自动发付费任务 |
| MED-05 | 视频鉴权内容下载与不同渠道协议 | 未完成:返回任务元数据但无公开 URL 的供应商需要鉴权内容获取;不同渠道不能共用猜测接口 | 绑定原账号、受控内容下载、用户隔离、体积期限与安全存储联合验收 |
## 实施边界
附件追踪确认(2026-10-22):用户再次提供的 CPR 比对文本已通过 SEC-01–07、COD-01–03、SCH-01–02、OPS-01–02 映射到本表,包含保留人民币钱包/凭据加密、不直接整体替换 CPR 的边界;附件中的版本是当次锁定基线,不是当前最新版声明。此前没有单列完整音视频验收任务,本轮新增 MED-01–05 并优先排查用户反馈,不能用安全回归通过替代音视频生成实测。
已补 SEC-01/02/03、SEC-06 的本地下载代码边界、验证码/账号事务,以及请求侧一致的验证码受理和加密异步投递。下一优先级是管理授权审查、同步 Transport 与完整 provider 配置版本,再处理 COD-01/02/03 的协议/续接/长请求合同。投递真实环境、残余时序与生产网络隔离仍属待验收。Codex WebSocket 与 OAuth 不是添加 URL 即可安全完成的能力,须连同状态归属、计量、预留续期和客户端验收逐步实施。未部署、未真实供应商验证或待设计的项必须保持未完成标记。
本轮设计与验证细节见 `25-auth-and-bounded-transport.md`。这份表按用户粘贴报告的锁定基线追踪本地实施,不把旧的 CPR 版本号冒充实时最新版,也没有重新部署 CPR。只做本地 Git commit,不 push,不修改业务数据库。
@@ -45,3 +52,9 @@
2026-10-22 投递隔离增量:新建 V50 加密 outbox、`VerificationDispatchService` 与封闭目的枚举;发送请求与投递分离,未知/停用账号不触发真实发送,但参与完全相同的请求端限额与写入路径。错误重置码统一公开错误,后台最多双线程、无内存等待队列,旧代次失败不影响新码,投递结果不明不自动重发。后端 `mvn -B -o clean verify` **390 项,0 失败/错误/跳过**,2026-10-22 08:41:38(Asia/Shanghai)结束;前端构建与 25 项测试通过,并通过独立容器的完整 Spring Boot 启动/健康检查。见 `28-verification-delivery-isolation.md`。
用户于 2026-10-22 明确取消每半小时提交检查,聊天自动化 `moyi` 已删除,不再自动轮询或提交;不停止产品内部 worker。当前开发仍只做经审查验证的本地提交,不 push、不部署、不迁移业务数据库;规格驱动验收环境未修复,未自行批准或执行业务验收。
2026-10-22 音视频规范化增量(MED-01/02/03):审查时修复音频参数校验中重复的模式变量名(编译错误)与旧目录测试假设,并收紧裸字符串结果只接受 URL。新增结果规范化、音频发送前拒绝、无文件完成结算一次等回归。后端 `mvn -B -o clean verify` **399 项,0 失败/错误/跳过**(独立 PostgreSQL 15.18/Redis 7,2026-10-22 11:47);前端构建与 27 项测试通过。没有真实供应商音视频生成,MED-04/05 仍未完成。
2026-10-22 管理授权审查与公平准入增量(SEC-07 / SCH-02):只读审查全部控制器,未发现未鉴权端点、先副作用后校验或跨用户 IDOR。已修复:①修改渠道 Base URL 时必须重新填写凭据,阻止把已存密钥重定向到新主机,审计记录新旧地址;②普通用户 `/api/models` 只返回渠道 id/name/status,不再暴露 Base URL 与凭据掩码;③管理端用量 CSV 导出中和公式前缀。**用户 2026-10-22 确认不拆分**:`admin` 与 `super_admin` 在钱包加款(含给自己)、定价、渠道、成本核账、用量删除上保持相同权限,依靠审计记录追溯;以下其余项仍未改:邀请码明文对所有管理员可见;管理变更审计缺 IP/前后值;角色校验未在行锁事务内复核。后端 `mvn -B -o clean verify` **403 项,0 失败/错误/跳过**(2026-10-22 12:06)。
2026-10-22 长请求底座增量(COD-03):`OpenAiCompatibleProvider` 新增 `GATEWAY_UPSTREAM_FIRST_BYTE_TIMEOUT_SECONDS`/`GATEWAY_UPSTREAM_IDLE_TIMEOUT_SECONDS`,流式响应有字节到达即重置空闲窗口;`GatewayRequestExecutionService` 在调用期间续期 `chat_billing_requests.expires_at`(只延长不缩短,仅 pending)和全部 Redis 租约(仅续仍持有的 token)。默认值不变,部署后行为与之前一致。后端 `mvn -B -o clean verify` **409 项,0 失败/错误/跳过**(2026-10-22 15:23)。另在本机 Docker(`infra/moyi` + `docker-compose.local.override.yml`,仅监听 127.0.0.1:18088)完成启动冒烟:健康检查、超级管理员登录、目录字段收敛、Key 创建、Mock 同步与 SSE 调用和用量记录、401/403/协议拒绝;Tripo 3D 因本地无凭据按预期失败且预留归零。
@@ -0,0 +1,154 @@
# CPR 及开源网关差距分析与深度优化方案
整理日期:2026-10-22(Asia/Shanghai)。本文合并两轮只读比对结论,作为后续优化的方案基线;实施进度以 `24-security-and-cpr-task-tracker.md` 为准。
## 任务来源
用户需求:对比当前版本与 CPR([Codex Proxy RS](https://github.com/zyycn/codex-proxy-rs))最新版的差距,确认 MOYI 是否基于 New API、Sub2API 或其他项目改造,与最新仓库比对,先分析差距,再对产品做深度优化。
第一轮比对时尚未确认 “cpr” 的具体指代,曾临时核查 CPA(CLIProxyAPI)和 CRS(Claude Relay Service);第二轮由用户确认为 Codex Proxy RS 后重新做了源码、架构与发布记录比对。两轮均为只读分析,没有修改代码、部署 CPR 或发起付费调用,也没有做同环境性能测试。
## 一、核心结论
- MOYI 不是 New API、Sub2API 或 CPR 的直接 fork,是自研 Java/Vue 控制面;早期计划接入 Otari,后来逐步自研网关数据面。
- MOYI 在多模型产品管理、人民币钱包账本、价格快照、异步媒体任务方面已有自己的基础。
- 与 CPR 相比,明显不足的是 Codex 专用协议、上游账号调度、长请求处理、重试与故障恢复边界。
- 与 New API/Sub2API/Otari 相比,差距集中在协议转换完整性、企业组织权限、路由策略、复杂计费和生产治理。
- 建议:**保留 MOYI 产品控制面和账本,重整网关执行层;CPR 作为 Codex 专用能力的参考或可选内网上游,不整体替换。**
## 二、比对基线(当次锁定,非实时最新版)
| 对象 | 锁定版本 | 日期 | 说明 |
|---|---|---|---|
| MOYI | `7785cda` | 2026-10-13 | 本地与自有远端一致;之后的安全增量见 24 号文档 |
| CPR 正式发布 | [`v3.29.6`](https://github.com/zyycn/codex-proxy-rs/releases/tag/v3.29.6)(`af35cde3`) | 2026-10-20 | 比对主要依据 |
| CPR 主分支 | `78bc57c5`(标记 `v3.29.7`) | 2026-10-21 | 多 9 个提交,未发布正式 Release;插件 RPC v4→v5,已有插件需重编译 |
| [New API](https://github.com/QuantumNous/new-api/releases/tag/v1.0.0-rc.45) | `v1.0.0-rc.45` | 10-17 | 主分支 `206e0fa5`,多 16 个提交 |
| [Sub2API](https://github.com/Wei-Shaw/sub2api/releases/tag/v0.2.17) | `v0.2.17` | 10-16 | 主分支 `8f2cbb53`,多 7 个提交 |
| [Otari](https://github.com/mozilla-ai/otari/releases/tag/v0.30.1) | `v0.30.1` | 10-20 | 主分支 `ac172ab7`,多 19 个提交 |
| [CPA](https://github.com/router-for-me/CLIProxyAPI/releases/tag/v8.0.36) | `v8.0.36` | 10-21 | 主分支与发布一致 |
| [CRS](https://github.com/Wei-Shaw/claude-relay-service/releases/tag/v1.1.315) | `v1.1.315` | 09-10 | 主分支与发布一致 |
这些项目与 MOYI 没有共同版本线,不能用“落后多少提交”衡量,只比较能力、边界和验证程度。
## 三、项目来源判断
- 工程决策([04 号文档](04-open-source-baseline-and-development-nodes.md)):New API 仅参考产品功能和兼容性,不复制代码;Sub2API 参考共享额度、账号池和运营流程;Otari 原计划作为独立网关;用户、权限、钱包、模型目录、异步任务和双端控制台自研。
- Git 历史:产品定义与 Vue 原型 → Otari 开发基座 → TypeScript 认证服务 → 迁移至 Java。未见继承 New API/Sub2API 提交历史的证据,也未发现基于 CPR 改造的证据。此判断不等同于逐文件代码相似度或许可证法律审计。
- 运行架构已偏离早期设计:开发基座仍固定 Otari `v0.4.0`(`infra/otari/docker-compose.yml`),云端 Compose 不部署 Otari;Java 转发器直连供应商,自行处理协议、密钥选择、重试和流式响应(`provider/OpenAiCompatibleProvider.java`)。
因此不存在“升级 Otari 镜像”或“合并 New API/Sub2API 更新”的简单升级路径。
## 四、已具备的能力(10-13 版本)
Messages、Responses、Gemini 原生入口与 Embeddings;用户级共享 RPM/并发/预算;Key 到期、IP 白名单、周期预算;渠道账号健康、冷却、安全重试、会话亲和与有限故障切换;版本化售价、采购成本、缓存写入、上下文阶梯与人工成本对账;TOTP 与恢复码;运营告警;真实异步媒体执行、钱包冻结结算与部分图片 Token 计费。范围见 [19 号文档](19-enterprise-gateway-increment.md)。优化应完善已有体系,而非重建。
## 五、与 CPR 的差距
先明确边界:CPR 是“面向 Codex 的自托管多账号网关”,明确不支持 `/v1/chat/completions`,内置 Provider 仅 OpenAI 与 xAI,不是通用多协议平台。
| 能力 | CPR 当前实现 | MOYI 现状与差距 | 优先级 |
|---|---|---|---|
| Codex 协议兼容 | 专用模型目录、业务扩展头、推理历史、工具及原生请求兼容 | 通用模型列表与有限 Responses 入口 | P0 |
| WebSocket 与续接 | Responses WebSocket、连接复用、`previous_response_id` 归属、续接失效恢复、中断控制 | 无 WebSocket 入口,直接拒绝 `previous_response_id` | P0 |
| 长推理与大上下文 | 不设统一总时限,分别管理连接、空闲、取消和租约 | 同步上游最多 90 秒;原生请求最多 256 KiB;输出 Token 上限 32,768 | P0 |
| 重试安全边界 | 区分未发送/已发送/结果不明,以及是否已交付业务输出 | 有安全重试和有限切换,缺同等完整的发送/交付状态模型 | P0 |
| 账号池生命周期 | OAuth/API Key 账号、凭据刷新、额度窗口、模型权限、账号级并发 | 渠道 + Key 池轮换,缺独立上游账号实体 | P1 |
| 调度策略 | 智能、额度重置优先、轮询、粘性;综合负载、额度、健康、首输出延迟 | 轮换、固定冷却、会话哈希亲和;延迟未进入选路 | P1 |
| 公平排队 | 有界等待、同级 FIFO、优先级、取消回收、共享等待预算 | 同步请求约每 100 ms 重新争抢 Redis 槽位,无公平性保证 | P1 |
| 诊断与统计 | 逐 attempt、上下游关联、传输类型、调度原因、恢复耗时、费用来源 | 仅最终调用记录与账号健康 | P1 |
| 配置与工程边界 | 冻结请求配置快照;协议、执行、Provider、存储分层;架构依赖测试与安全流水线 | 价格有快照,网关职责集中,缺统一执行合同和同等门禁 | P1 |
参考:CPR [架构文档](https://github.com/zyycn/codex-proxy-rs/blob/af35cde3744b7afc81c4ad6e1e9c9c76bbbd68b8/docs/architecture.md)、[数据面接口文档](https://github.com/zyycn/codex-proxy-rs/blob/af35cde3744b7afc81c4ad6e1e9c9c76bbbd68b8/docs/api.md)。
直接影响客户体验的三点:
1. **Responses 只是有限支持。** `gateway/NativeProtocolMeter.java` 明确拒绝云端续接、图片、文件、多种托管工具和服务档位,并固定限制请求体与输出 Token。简单文本可成功,长对话、图像、复杂工具和客户端恢复未必通过,不能宣传“完整支持 Codex”。
2. **会话亲和不等于有状态续接。** `provider/UpstreamHealth.java` 用 `X-Moyi-Session` 选择账号,冷却后会重新选择。支持增量续接时必须记录响应所属账号、凭据版本和连接,不能把 A 账号的响应 ID 交给 B 账号。
3. **排队与诊断不足以支撑企业 SLA。** `resources/redis/acquire-pair.lua` 获取槽位时不检查排队先后,持续竞争下新请求可能先于旧请求;只有最终错误和总耗时,无法区分慢在排队、建连、冷却还是生成。
## 六、与 New API/Sub2API/Otari 等的差距
“部分具备”指已有实现但覆盖或管理闭环不足。
| 维度 | MOYI 现状 | 主要差距 | 优先级 |
|---|---|---|---|
| 协议转换 | 原生透传 + 统一用量扣费 | New API RelayKit、Sub2API 有更完整的跨协议转换 | P0 |
| 编程客户端兼容 | 基础文本与函数工具,子路径少 | 缺 Responses WebSocket、compact 等入口;CPA 持续修复工具调用、推理和协议差异 | P0 |
| 多模态与托管工具 | 聊天入口拒绝图片、文件、音频、托管搜索 | 缺视觉聊天、文档理解、托管工具的协议与计费闭环 | P0/P1 |
| 身份与会话安全 | 密码、验证码、TOTP | 10-13 版本缺登录限速、统一密码策略、改密撤销会话(后续已补,见第九节) | P0 |
| 组织与权限 | 用户、三个全局角色、个人项目 | 缺组织、成员、项目角色、共享资源、企业 SSO 与离职回收;Otari 有组织/工作区治理 | P1 |
| 路由策略 | 有限备用路由、优先级、冷却 | 缺按成本、延迟、权重、容量、预算组合的策略及解释 | P1 |
| 上游账号池 | 环境变量 Key 池、轮换、健康指纹 | 缺账号生命周期、额度窗口、账号级容量、可视化轮换 | P1 |
| 定价与计量 | Token、缓存、部分阶梯、固定任务费、图片 Token | 缺视频时长/分辨率、音频、托管工具、服务档位;New API 有表达式计费 | P1 |
| 成本核对 | 快照、采购成本、人工核对 | 缺账单导入、自动匹配、差异归因、毛利预警 | P1 |
| 异步结果交付 | 数据库任务、轮询、部分媒体执行 | OSS 主要是配置预留;缺产物托管、授权下载、生命周期、批量与回调 | P1 |
| 可观测性 | 用量、容量快照、账号健康、错误率告警 | 缺链路追踪、逐 attempt、SLO、企业/项目聚合、日志留存治理 | P1 |
| 商业运营 | 管理员入账、对公合同付款 | 自助支付、订阅、退款不如 New API/Sub2API;与 B/G 定位不同,非首要 | P2 |
| 工程交付 | 单元测试、隔离数据库脚本、发布工具 | 缺自动化兼容矩阵与持续安全门禁;README 与实现不同步 | P1 |
上游近期更新中值得转成 MOYI 回归用例的风险场景(不表示 MOYI 一定存在相同缺陷):
- New API:Claude 显式零用量、全缓存请求计费、Vidu 按实际模型/时长/分辨率计费;任务重试前重新定价和预留、查询重定向凭据的主机限制、Redis 脚本被清空后的恢复。重点验证“字段缺失”与“明确为零”不混同。
- Sub2API:Gemini 流中途断开计费、缓存计数导致负输入 Token、异步图片转存策略。
- CPA:并行工具调用、推理 Token、结束原因、Gemini thought signature、首 Token 延迟统计,这类问题容易被“HTTP 200 测试”漏掉。
- Otari:权重/条件/预算路由、组织工作区、策略解释与追踪。
## 七、不能照搬 CPR 的地方
- **额度不是严格预付费钱包。** CPR 准入只检查已结算金额、不预占;并发请求可使最终金额超限;无法取得费用的尝试按零累计且不建待核账记录;结算暂存重试依赖进程内状态([budget.rs](https://github.com/zyycn/codex-proxy-rs/blob/af35cde3744b7afc81c4ad6e1e9c9c76bbbd68b8/backend/crates/gateway-core/src/engine/budget.rs#L34))。MOYI 的钱包锁、预占、价格快照和待对账状态(`wallet/ChatBillingService.java`)应保留并强化。
- **凭据保护不能降级。** CPR Provider 凭据以明文 JSON 保存;MOYI 使用 AES-GCM。
- **不能直接横向扩容。** CPR 官方限定单进程单副本,含进程内队列、连接池和状态。
- **不能替代产品控制面。** CPR 只有管理员与 Key 持有者身份,没有用户、角色、人民币钱包、项目和媒体任务体系。
- **业务备份不等于审计备份。** CPR `v3.29.6` 业务归档排除请求明细、诊断与审计日志,B/G 端须独立制定审计留存与备份策略。
- **不能因 Rust 断言更快。** 未做同环境性能测试。
- **许可证。** 参考或复用代码时须分别处理 New API(AGPL)、Sub2API(LGPL)及 Otari/CPA/CRS/CPR 的实际许可证。
## 八、深度优化方案
原则:保留 Java/Vue、现有账户和钱包账本,先明确数据面边界,不整体重写为某个开源项目。直接换底座会同时带来账户迁移、历史金额、Key、审计和许可证风险。MOYI 钱包始终是唯一用户财务事实来源,不让两套系统各自扣费。
### 第一阶段:安全与统一执行底座
- 安全:登录分布式限速、统一密码策略、改密撤销会话、用户停用、任务 scope/项目隔离、Provider 出站限制、管理接口授权审查。
- 统一请求生命周期,重试、断线、取消、服务重启都进入同一流程,不由各 Controller 分别处理:
`准入 → 冻结权限与价格 → 排队 → 上游尝试 → 交付 → 结算/待核账`
- 不能只把 90 秒超时改长。同步请求租约 3 分钟、钱包预留默认 5 分钟到期;放开长任务必须同时设计租约续期、预留恢复和结算逻辑,否则引入资金与并发问题。
- 协议解析、流事件、工具调用、用量归一化从业务 Controller 中分离;错误格式分别符合 Anthropic、OpenAI、Gemini 约定。
### 第二阶段:Codex 兼容通道与兼容矩阵
- 优先完成 Responses WebSocket、模型能力目录、会话与续接归属、工具/图片输入、长流和取消。
- 建立“客户端 × 协议 × 上游 × 能力”验收矩阵,指定客户端版本、模型、工具和计费联合验收。
- 可选小范围验证:仅 Codex 路由接内网 CPR,Chat、Messages、Gemini、3D/媒体仍走原适配器。不能只改 Base URL:MOYI 当前会在转发前拒绝大量原生请求,还需对接请求关联、用量、重试和费用合同。
- 用同一测试集比较“继续自研”与“接入独立网关”的成本后再定底座。
### 第三阶段:账号调度与组织权限
- 将“渠道下多个字符串 Key”升级为受控、加密、可审计的独立账号资源:凭据版本、模型范围、容量、额度、健康。
- 先做可解释的加权策略,再引入延迟、失败率、额度重置信号;补公平排队(FIFO、取消回收、共享等待预算)。
- OAuth 订阅账号池为可选能力,不作为 B/G 客户 SLA 默认基础,须先核实上游授权和服务条款。
- 组织、成员、项目角色、项目钱包/预算、资源归属、企业 SSO;保留个人账号作为兼容入口,授权在后端统一执行。
### 第四阶段:财务、媒体与生产治理
- 定价使用版本化规则、试算和审核发布;补视频时长/分辨率、音频、托管工具计费;账单自动匹配与差异告警独立于用户扣费。
- 媒体:OSS 产物、短期下载授权、留存删除、任务回调与恢复。
- 治理:逐 attempt 诊断、链路追踪、SLO、企业/项目用量、数据留存、备份恢复演练、依赖扫描、协议回归、发布门禁。
- 模型状态拆分为:目录已登记 → 凭据已配置 → 协议已验证 → 计费已验证 → 生产可用,并记录验证日期与证据。10-13 部署记录为 315 个公开分组、226 个配置就绪、39 个暂缓、50 个待核实,“配置就绪”不等于真实调用验收。
## 九、实施映射与当前进度
各项已拆为 24 号文档中的任务 ID,进度只在该表更新:
| 方案内容 | 任务 ID |
|---|---|
| 第一阶段安全 | SEC-01~SEC-07 |
| 长请求与续租、Codex 协议、WebSocket 续接 | COD-01~COD-03 |
| 账号生命周期、公平排队 | SCH-01~SCH-02 |
| 诊断、配置版本、门禁;CPR 可选部署 | OPS-01~OPS-02 |
| 音视频回执、协议、展示、计费、鉴权下载 | MED-01~MED-05 |
截至 2026-10-22,SEC-01/02/03 已实现并验证(25、27、28 号文档),SEC-06 完成本地代码边界(26 号文档),其余 COD/SCH/OPS/MED 项未完成。未部署、未真实供应商验证的项保持未完成标记,不宣称“完整支持 Codex”或“全仓库安全”。
@@ -0,0 +1,263 @@
# Codex 兼容:Responses 续接归属与 WebSocket 设计(COD-01/COD-02)
日期:2026-10-22(Asia/Shanghai)。状态:**暂缓实施(2026-10-22 决定,见第 0 节)**。对应 24 号任务表 COD-01、COD-02,承接 29 号文档第五节与第八节第二阶段。
## 0. 决定(2026-10-22)
1. **暂不实施** COD-01/COD-02。现有行为不变:Responses 入口继续拒绝 `previous_response_id`,不提供 WebSocket。
2. **不允许上游存储对话**,继续强制 `store=false`。
3. **本阶段目标是在 Sub2API 的能力基础上做企业化**(组织、权限、人民币计费、审计、运维治理),不做协议层的革新设计。日后恢复续接与 WebSocket 时,以 Sub2API 的行为为基线对齐,见 0.2。
### 0.1 不存储对话的后果
`store=false` 时上游不保存响应,HTTP 请求里的 `previous_response_id` 一定得到 `previous_response_not_found`。**增量续接的唯一途径是 WebSocket**:上游只在一条活动连接的内存里缓存最近的响应状态,连接断开或满 60 分钟即失效。因此恢复这项工作时必须同时完成:
- **服务器 nginx 配置**:`infra/moyi/nginx/routes.conf` 与 `prototype/nginx.conf` 为 `/v1/responses` 转发 `Upgrade` 和 `Connection: upgrade`;读超时不低于连接上限(Sub2API 上游读超时 900 秒,连接上限 60 分钟);云端 Compose 透传 COD-03 的超时变量。这是线上配置变更,须单独评审。
- **连接管理**:
- 每个平台 Key 的连接数上限;
- 首条消息读取超时、两轮之间的空闲超时、60 分钟连接上限;
- 每个客户端连接绑定一条上游连接及其账号;
- 连接内存里保留本连接的历史输入,用于续接失败时服务端重放一次,不落库;
- 客户端或上游断开时,进行中的一轮按现有规则结算或释放预留。
HTTP 续接只有在允许 `store=true` 时才可行,与第 2 条决定冲突,本阶段不做。
### 0.2 恢复实施时与 Sub2API 对齐的清单
依据第 10 节的源码核对。
| 事项 | Sub2API 行为 | MOYI 实施时 |
|---|---|---|
| 归属存储 | Redis,Key 为 `response_id` 的 SHA-256,按分组隔离;进程内热缓存 | 对齐:用现有 Tair,同样只存哈希 Key 和账号 ID |
| 保留时间 | 1 小时(`sticky_response_id_ttl_seconds: 3600`) | 对齐:1 小时,可配置 |
| 归属校验 | 校验用户与 Key,不匹配返回 400 | 对齐 |
| 消息 ID 误用 | 拒绝 `msg_`、`item_` 等前缀 | 对齐 |
| 原账号不可用 | 放弃粘连,正常调度另选账号;带未覆盖的 `function_call_output` 时只能走原账号 | 对齐 |
| `previous_response_not_found` | WebSocket 内用连接内存的历史去掉 `previous_response_id` 重放一次(未向客户端输出、无工具输出、每轮一次) | 对齐 |
| WebSocket 参数 | 每 Key 64 连接、轮间空闲 300 秒、首消息 30 秒、上游读 900 秒、`store=false` 时优先新建连接、预热默认关闭 | 作为默认值起点,按服务器容量调整 |
MOYI 保留自己的企业化部分:每一轮独立走人民币钱包预留、续期与结算(`GatewayRequestExecutionService`)、用量审计、凭据加密存储。这些属于企业化而非协议创新。
第 3–5 节是对齐决定之前的初稿。其中与 0.2 冲突的设计(PostgreSQL 归属表、凭据版本、原账号不可用时 409 不换号)不再采用,归档在第 11 节。
## 1. 目标与非目标
目标:
- 让 Codex 等编程客户端通过 MOYI 使用 Responses 协议的增量续接(`previous_response_id`),不再每轮发送完整上下文。
- 提供 Responses WebSocket 入口,单连接多轮、顺序执行。
- 每个上游响应都能追溯到产生它的渠道账号与凭据版本,续接**只能**发往同一账号;无法满足时明确失败,不静默换号。
- 计费、预留、并发租约、用量审计与现有 HTTP 路径共用同一套合同(`GatewayRequestExecutionService`),不另起账本。
非目标(本期不做):
- 上游 `store=true` 的云端会话、`conversation` 对象、`background` 后台任务。
- 图片、文件、托管工具(web_search、code_interpreter 等)。它们需要单独计费适配器,仍按现状拒绝。
- OAuth 订阅账号池(OPS-02,可选方案,须先核实授权)。
- 跨实例共享 WebSocket 连接。单个连接只存在于持有它的 API 实例。
## 2. 依据与当前代码事实
外部协议依据(Azure OpenAI Responses WebSocket 文档,2026-10-22 读取;OpenAI 官方页面在本机网络不可访问,未直接核对):
- 连接路径为 `/v1/responses`,客户端发送 `{"type":"response.create", ...}`,载荷同 HTTP 创建请求,但不适用 `stream`、`background`。
- 同一连接内**顺序执行**,同时只有一个进行中的响应。(部分资料提及按 `stream_id` 并行的新版本,未经核实,本设计按顺序执行实现,并在客户端验收中确认实际版本。)
- 连接本地缓存**最近一个**响应状态;`store=false` 时引用不在缓存中的 ID 返回 `previous_response_not_found`;某一轮 4xx/5xx 失败会从缓存驱逐被引用的父响应。
- 连接最长 60 分钟,到期返回 `websocket_connection_limit_reached`,须重连;`store=false` 下重连后只能不带 `previous_response_id`、重发完整上下文。
- 可用 `generate:false` 预热,返回可续接的响应 ID 但不产生输出。
本仓库当前事实(已读代码核实):
| 位置 | 现状 | 对本设计的影响 |
|---|---|---|
| `gateway/NativeProtocolMeter.java` | 拒绝 `previous_response_id`、`conversation`、`background`;强制 `store=false`;请求上限 256 KiB;输出上限 32768 | 续接需放开 `previous_response_id`,其余限制保留 |
| `provider/UpstreamHealth.java` | `X-Moyi-Session` 加平台 Key 做 rendezvous 哈希选号;账号冷却后会改选其他账号 | 只能算“尽量同号”,不能保证续接归属 |
| `provider/OpenAiCompatibleProvider.java` | 账号选择、401/403/429 换号重试都在一次请求内部完成;不对外暴露最终使用的账号 | 需要把“实际使用的账号指纹”返回给调用方 |
| `model_tasks.provider_key_fingerprint`(V33) | 异步任务已按账号指纹绑定轮询 | 同步续接沿用相同的指纹方案,不存明文 Key |
| `gateway/request/GatewayRequestExecutionService` | 统一准入、预留、续期(COD-03)、结算、失败释放 | WebSocket 每一轮都走它,复用续期 |
| nginx `infra/moyi/nginx/routes.conf`、`prototype/nginx.conf` | 无 `Upgrade`/`Connection` 转发,读超时 120 秒 | WebSocket 必须改代理配置,属部署变更 |
## 3. 核心设计:响应归属记录
### 3.1 记录什么
新增表 `responses_ownership`(迁移 V51),每个**成功完成**的上游 Responses 响应写一行:
| 列 | 说明 |
|---|---|
| `response_id` | 上游返回的 `id`,主键(`varchar(128)`) |
| `user_id`、`api_key_id` | 发起方。续接时必须与当前调用方一致,防止拿别人的 ID 续接 |
| `route_id`、`provider_id`、`upstream_model` | 产生该响应的路由快照 |
| `account_fingerprint` | 实际使用账号的 SHA-256 指纹(同 `UpstreamHealth.fingerprint`),不存明文 |
| `credential_version` | 渠道凭据版本,见 3.3 |
| `transport` | `http` 或 `websocket` |
| `connection_id` | WebSocket 连接 ID(HTTP 为空) |
| `parent_response_id` | 本轮引用的上一响应,便于审计链路 |
| `usage_event_id` | 对应 `usage_events.id`(即计费 ticket) |
| `created_at`、`expires_at` | 默认保留 1 小时(可配置,与上游 WebSocket 连接上限及 Sub2API 默认值一致),到期由定时任务清理 |
不存储请求或响应正文。只存路由与账号指纹,满足“可追溯但不留内容”。
### 3.2 续接时如何使用
收到带 `previous_response_id` 的请求(HTTP 或 WebSocket):
1. 按 `response_id` 查归属行。不存在或已过期:返回 `400 previous_response_not_found`(与上游错误码一致),**不发往上游**、不预留。
2. `user_id`/`api_key_id` 与当前调用方不一致:同样返回 `previous_response_not_found`,不泄露该 ID 是否存在。
3. 路由已下线、模型不一致、账号指纹不在当前可用账号集合中、凭据版本已变化、或账号处于冷却:返回 `409 RESPONSE_OWNER_UNAVAILABLE`,提示客户端不带 `previous_response_id` 重发完整上下文。**不换号**:其他账号持有的上游状态里没有这个响应,换号只会得到上游的 `previous_response_not_found`,还可能已经产生费用。
4. 校验通过:以“指定账号”方式调用上游(新增 `OpenAiCompatibleProvider` 的 pinned-key 入口),禁用该请求内的 401/403/429 换号重试与 `GatewayFailover` 备用路由。
### 3.3 凭据版本
当前渠道 Key 来自 `model_providers.credential_ciphertext` 或环境变量 Key 池,没有版本号。方案:
- 数据库渠道:`credential_version` 取渠道 `updated_at`。注意当前任何渠道编辑(包括改名)都会更新该字段,所以编辑渠道会让已有续接失效。这是偏保守的取舍,不新增管理界面。
- 环境变量 Key 池:没有版本号,只靠账号指纹。Key 被替换后指纹必然改变,旧响应同样失效。
- 自 `1a608f8` 起,修改渠道 Base URL 必须重填凭据,因此地址变更一定会触发失效,走 3.2 第 3 步。
真正独立的账号实体与版本属于 SCH-01,本期不扩展。
### 3.4 写入时机与一致性
- HTTP 非流式:在 `settle` 成功之后写入归属行。结算失败(`SETTLEMENT_PENDING`)时仍写入,因为上游响应已存在;归属不影响账务。
- 流式/WebSocket:收到 `response.completed` 或 `response.incomplete` 事件、拿到 `response.id` 后写入。
- 写入失败只记日志、不影响本轮返回;代价是下一轮续接得到 `previous_response_not_found`,客户端按规范回退为完整上下文。这是安全方向的失败。
## 4. HTTP 续接(COD-01)
> **更正(2026-10-22,核对 Sub2API 后)**:HTTP 续接要求上游保存响应(`store=true`)。本设计强制 `store=false`,上游不保存,HTTP 请求里的 `previous_response_id` 会稳定返回 `previous_response_not_found`。Sub2API 同样只允许 API Key 类账号在 HTTP 下续接,也是依赖上游存储。因此:
>
> - 若保持 `store=false`(不让上游保存用户对话,当前的隐私与数据驻留立场),**HTTP 续接没有意义**,增量续接只能通过 WebSocket 连接内的缓存实现,见第 5 节。
> - 若允许 `store=true`,HTTP 续接可用,但用户的输入和输出会存储在上游服务商处(OpenAI 默认保留 30 天),须在服务条款和隐私说明中告知,并按用户或 Key 单独开启。
>
> 以下改动点在两种选择下都需要(归属记录、指定账号、跳过备用路由),只是第 1 条的 `store` 处理不同。
改动点:
1. `NativeProtocolMeter.request`:允许 `previous_response_id`(字符串,1–128 字符);继续拒绝 `conversation`、`background`、媒体与托管工具。`store` 按上面的决定处理:默认仍强制 `false`;若开启存储,则只对已授权的用户或 Key 允许 `true`。
2. 新增 `ResponsesOwnershipService`(`gateway/responses` 包):查询、校验、写入、清理。依赖方向测试中禁止它依赖 HTTP 客户端与凭据解密。
3. `OpenAiCompatibleProvider`:
- 新增返回“实际使用账号指纹”的结果包装,供写归属。
- 新增 pinned-key 调用:只用指定指纹对应的 Key;该 Key 不在 Key 池或已冷却时在发送前报 `RESPONSE_OWNER_UNAVAILABLE`。
4. `NativeGatewayController`:带 `previous_response_id` 时跳过 `GatewayFailover`,只走原路由。
5. 计费:不变。续接请求的输入 Token 由上游 `usage.input_tokens` 计量(包含被续接的历史上下文),`input_tokens_details.cached_tokens` 走现有缓存价。预留按本轮请求体估算,偏低部分由结算时的实际用量和现有欠费处理兜底。
## 5. WebSocket 入口(COD-02)
### 5.1 连接与鉴权
- 路径 `GET /v1/responses`,`Upgrade: websocket`。使用 Spring WebSocket(新增 `spring-boot-starter-websocket`,版本跟随 Spring Boot parent)。
- 握手时用 `Authorization: Bearer <MOYI Key>` 鉴权,复用 `CatalogService.authenticateKey` 与 `EnterprisePolicies.access`(IP 白名单、过期)。不接受 URL 查询参数里的 Key。
- 每个平台 Key 的并发 WebSocket 连接数设上限(默认 4,复用 Key 并发配置),超出时拒绝握手。
### 5.2 连接内的每一轮
- 每个 `response.create` 视为一次独立的计费请求:走 `GatewayRequestExecutionService.execute`,独立准入、预留、续期、结算、用量记录。连接本身不预留费用。
- 同一连接顺序执行:上一轮未结束时收到新的 `response.create`,返回 `error` 事件(`code: response_in_progress`),不排队,避免隐性占用资源。
- 上游侧:每个客户端连接对应**一个**上游 WebSocket 连接,绑定首轮选中的账号。后续轮次固定使用该账号,不换号。上游连接断开时,本地也关闭客户端连接,并返回上游的错误码,让客户端按协议重连。
- 首轮无 `previous_response_id` 时正常选号;首轮带 `previous_response_id` 时,按第 3.2 节归属选择上游账号。
- 连接最长 60 分钟(与上游一致,可配置更短)。到期发送 `websocket_connection_limit_reached` 后关闭。
### 5.3 中断、取消与计费
| 情况 | 处理 |
|---|---|
| 客户端在一轮进行中断开 | 继续读取上游直至该轮完成并结算(与现有 SSE“浏览器断开不变成免费调用”一致),然后关闭上游连接 |
| 上游在一轮进行中断开 | 已有完整 `response.completed` 则结算;否则按现有规则 `UPSTREAM_STREAM_INCOMPLETE`,释放预留,记 `cost_status='pending'` 待核账 |
| 空闲(无进行中轮次)超时 | 关闭连接,无费用 |
| 服务重启 | 进行中轮次由预留过期恢复处理(COD-03 续期停止后最多 5 分钟释放或按已持久化用量结算) |
### 5.4 部署前置条件
- nginx 两处配置都要为 `/v1/responses` 增加 `proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade";`,读超时不低于连接上限。
- 云端 Compose 透传 COD-03 的超时变量。
- 这些都是线上配置变更,单独评审,不随代码自动生效。
## 6. 安全与滥用边界
- 归属校验是访问控制:A 用户的响应 ID 对 B 用户统一表现为“不存在”。
- 不接受客户端指定上游账号、Base URL、凭据(现有限制保留)。
- `previous_response_id` 只作为字符串透传,长度限制并做字符集校验(`[A-Za-z0-9_-]`),避免日志或 SQL 注入面。
- WebSocket 单帧大小上限沿用 256 KiB 请求上限;超限关闭连接。
## 7. 测试与验收
自动化(本地,与现有门禁一起跑):
- 归属:写入、过期、跨用户统一返回 `previous_response_not_found`、路由下线/指纹变化/冷却时 409 且不发上游请求。
- pinned-key:只用指定 Key,不触发换号重试与备用路由。
- HTTP 续接:上游桩返回带 `id` 的完成响应,第二轮带 `previous_response_id` 只打到同一 Key。
- WebSocket:握手鉴权、顺序执行拒绝并发、客户端断开仍结算一次、上游中断释放预留、60 分钟上限(用可配置短时限测试)。
- 依赖方向:`gateway/responses` 不依赖 HTTP 客户端与凭据。
客户端验收(需要你授权真实上游与付费调用,未授权前不做):
- 指定 Codex CLI 版本,配置 MOYI 为 Responses 端点,跑一组固定任务:多轮工具调用、断线重连、超过 60 分钟的会话。
- 每轮核对:上游账号一致、用量与扣费一致、续接失败时客户端能回退完整上下文。
- 结果记录到兼容矩阵(客户端 × 协议 × 上游 × 能力),并保存日期与证据。
## 8. 恢复实施时的顺序
1. Tair 归属记录(对齐 0.2),附回归。
2. Provider 返回实际使用的账号,并支持按账号调用。
3. WebSocket 入口与连接管理(0.1),每一轮接入现有计费合同。
4. nginx 与云端 Compose 配置评审。
5. 客户端验收(需授权真实上游与付费调用)。
每一步单独提交,全量测试通过后再进入下一步。
## 9. 已决定事项
见第 0 节:暂不实施;不允许上游存储;恢复时与 Sub2API 对齐,保留时间 1 小时,原账号不可用时按 Sub2API 换号并在连接内重放一次。
## 10. Sub2API 对照(2026-10-22 源码核对)
核对对象:[Wei-Shaw/sub2api](https://github.com/Wei-Shaw/sub2api) 主分支 `dba770d5`(2026-10-22),最新发布 `v0.2.17`。通过 GitHub API 读取以下文件,只读未复制代码(Sub2API 为 LGPL):`backend/internal/service/openai_ws_state_store.go`、`openai_account_scheduler.go`、`openai_ws_forwarder*.go`、`openai_ws_forwarder_payload.go`、`backend/internal/handler/openai_gateway_handler.go`、`deploy/config.example.yaml`。
### 10.1 归属记录与保留时间
- 存储:`response_id → account_id` 与 `response_id → (user_id, api_key_id)` 存 Redis,Key 为 `response_id` 的 SHA-256,进程内另有热缓存(单表上限 65536 条,满了随机淘汰一条)。`response_id → conn_id` 只在进程内。按分组 ID 隔离。
- 保留时间:`sticky_response_id_ttl_seconds: 3600`,即 **1 小时**,会话粘性 `sticky_session_ttl_seconds` 也是 1 小时。代码默认值同为 1 小时。
- 读不到 Redis 时按“未命中”降级,不阻断请求。
- 归属校验:HTTP 带 `previous_response_id` 时先校验用户与 Key,不匹配返回 `400 previous_response_id is not available for this user`。另外会拒绝看起来像消息 ID(`msg_`、`item_` 等)的值。
与本设计的差异:我们用 PostgreSQL 表(原定 24 小时,已按此改为 1 小时);Sub2API 用 Redis、1 小时。Sub2API 不记录凭据版本。
### 10.2 原账号不可用时
- 调度器先按 `previous_response_id` 找原账号;原账号不兼容(分组、模型、传输方式、被封禁、隔离等)时**放弃粘连,按正常调度另选账号**,请求照常发出,不直接报错。
- 例外:输入里带 `function_call_output` 且上下文不能覆盖所有调用 ID 时,`previous_response_id` 视为不可迁移(`previousResponseCanMove=false`),只能走原账号。
- HTTP 模式下 OAuth 订阅账号不支持 `previous_response_id`,会跳过这类账号,全部不行时返回 400。
### 10.3 续接失败的恢复
- WebSocket 接入模式下,上游返回 `previous_response_not_found` 时,默认 `ingress_previous_response_recovery_enabled: true`:**服务端去掉 `previous_response_id`,用自己保留的前几轮输入拼成完整上下文,重放一次**。
- 前提:这一轮还没向客户端写出内容;请求不含 `function_call_output`(否则上游会报找不到工具调用);每轮最多尝试一次。
- 重放所需的历史输入只保存在**该 WebSocket 连接的内存里**,不落库,连接断开即丢失。所以重连后仍需客户端自己发完整上下文。
- HTTP 模式没有这种服务端重放,失败直接返回错误。
### 10.4 WebSocket
- 默认开启(`openai_ws.enabled: true`,协议特性 `responses_websockets_v2: true`),有全局回退 HTTP 的紧急开关 `force_http`。
- 关键参数:每个 Key 最多 64 个客户端连接;两轮之间空闲 300 秒关闭;首条消息读取超时 30 秒;上游读超时 900 秒;每个上游账号最多 128 条连接、按账号并发 × 5 动态限制;`store=false` 且无可复用连接时默认强制新建连接(隔离优先);`generate:false` 预热默认关闭。
- 有多种接入模式(`ctx_pool` 连接池、`passthrough` 直通、`http_bridge` 转成 HTTP 请求),实现规模大(WebSocket 相关源码超过 30 万字节)。
- 部署文档未单独说明反向代理的 WebSocket 配置(未找到相关段落)。
### 10.5 对本设计三个问题的影响
| 问题 | Sub2API 做法 | 建议 |
|---|---|---|
| 保留时间 | Redis 1 小时 | 已改为默认 **1 小时**、可配置。上游 WebSocket 连接最长 60 分钟,`store=false` 下连接一断响应就找不到,保留更久意义不大 |
| 原账号不可用 | 换号重发(工具输出链路除外);WebSocket 内可用连接内存里的历史自动重放一次 | 保持 **409、由客户端重发**。我们 `store=false`,换号后上游一定找不到该响应,换号只会多一次失败请求;不保存正文也就无法服务端重放。若之后做 WebSocket,可考虑像 Sub2API 一样只在连接内存中保留本连接的历史,用于一次性重放 |
| WebSocket | 默认开启,功能完整但复杂度高 | 取决于是否允许 `store=true`(见第 4 节更正):不允许,则 WebSocket 是唯一的增量续接方式;允许,则可先做 HTTP 续接。Sub2API 的规模说明 WebSocket 是独立的一大块工作 |
注:本节依据源码阅读,没有部署 Sub2API 或实测其行为。表中“建议”一列是对齐决定之前的看法,已被第 0 节取代。
## 11. 备选想法(未采纳,留档)
以下是对齐 Sub2API 之前的设计思路,保留供以后企业化或合规需要时参考:
- **PostgreSQL 归属表**:比 Redis 更利于审计追溯(第 3.1 节字段),代价是写库压力。若日后客户要求“每个响应可追溯到上游账号”,可在 Tair 之外异步落库。
- **凭据版本**:把渠道凭据版本记入归属,凭据替换后旧响应立即失效(第 3.3 节)。Sub2API 不做这一层。
- **原账号不可用时 409、不换号**:在 `store=false` 下换号必然得到 `previous_response_not_found`,直接让客户端重发完整上下文可以少一次上游请求。对齐后改为 Sub2API 的“换号 + 连接内重放一次”,效果相近,但多一次失败的上游请求。
- **按 Key 开启 `store=true` 的 HTTP 续接**:实现最简单,但用户对话会保存在上游服务商处(OpenAI 默认 30 天),需要在条款和隐私说明中告知。本阶段已决定不允许。
@@ -0,0 +1,12 @@
# Local test override: use prebuilt artifacts via Dockerfile.runtime and bind only to localhost.
services:
moyi-api:
build:
context: ../../control-plane-java
dockerfile: Dockerfile.runtime
frontend:
build:
context: ../../prototype
dockerfile: Dockerfile.runtime
ports: !override
- "127.0.0.1:${MOYI_HTTP_PORT:-8080}:80"
+1
View File
@@ -231,6 +231,7 @@ export interface ChatCompletionResult {
export interface AsyncTaskResult {
id: string
model: string
taskType?: AsyncMediaType
provider?: string
status: 'queued' | 'running' | 'reconciling' | 'succeeded' | 'failed' | 'cancelled'
requiresReconciliation?: boolean
+9 -4
View File
@@ -29,7 +29,7 @@ function base64DataUrl(value: unknown, mimeType: unknown, format: string): strin
if (!/^[a-z0-9+/]+={0,2}$/i.test(payload) || payload.length % 4 === 1) return undefined
const mime = typeof mimeType === 'string' && /^(image|video|audio|model)\/[a-z0-9.+-]+$/i.test(mimeType)
? mimeType.toLowerCase()
: ({ png: 'image/png', jpg: 'image/jpeg', jpeg: 'image/jpeg', webp: 'image/webp', gif: 'image/gif', mp4: 'video/mp4', webm: 'video/webm', mp3: 'audio/mpeg', wav: 'audio/wav', ogg: 'audio/ogg', glb: 'model/gltf-binary', gltf: 'model/gltf+json' } as Record<string, string>)[format]
: ({ png: 'image/png', jpg: 'image/jpeg', jpeg: 'image/jpeg', webp: 'image/webp', gif: 'image/gif', mp4: 'video/mp4', webm: 'video/webm', mp3: 'audio/mpeg', wav: 'audio/wav', ogg: 'audio/ogg', opus: 'audio/ogg', aac: 'audio/aac', flac: 'audio/flac', m4a: 'audio/mp4', glb: 'model/gltf-binary', gltf: 'model/gltf+json' } as Record<string, string>)[format]
return mime ? `data:${mime};base64,${payload}` : undefined
}
@@ -47,22 +47,27 @@ export function mediaResult(result: Record<string, unknown> = {}, type = ''): Me
const format = typeof result.format === 'string' ? result.format.toLowerCase() : ''
const mime = typeof result.mimeType === 'string' ? result.mimeType.toLowerCase() : typeof result.mime_type === 'string' ? result.mime_type.toLowerCase() : ''
const url = mediaValue(result.url, mime, format) || base64DataUrl(result.b64_json || result.base64 || result.data, mime, format)
const suffix = url ? new URL(url).pathname.toLowerCase().split('.').pop() : ''
const suffix = url && !url.startsWith('data:') ? new URL(url).pathname.toLowerCase().match(/\.([a-z0-9]+)$/)?.[1] || '' : ''
const isImage = type === '图像' || type === 'image' || mime.startsWith('image/') || /^(png|jpe?g|webp|gif|avif)$/.test(format || suffix || '')
const isVideo = type === '视频' || type === 'video' || mime.startsWith('video/') || /^(mp4|webm|mov)$/.test(format || suffix || '')
const isAudio = type === '音频' || type === 'audio' || mime.startsWith('audio/') || /^(mp3|wav|ogg|m4a|flac|aac)$/.test(format || suffix || '')
const isAudio = type === '音频' || type === 'audio' || mime.startsWith('audio/') || /^(mp3|wav|ogg|opus|m4a|flac|aac)$/.test(format || suffix || '')
const isModel = type === '3D' || type === '3d' || /^(glb|gltf|obj|fbx|stl)$/.test(format || suffix || '')
return {
imageUrl: mediaValue(result.imageUrl, mime, format) || mediaValue(result.image_url, mime, format) || (isImage ? url : undefined),
videoUrl: mediaValue(result.videoUrl, mime, format) || mediaValue(result.video_url, mime, format) || (isVideo ? url : undefined),
audioUrl: mediaValue(result.audioUrl, mime, format) || mediaValue(result.audio_url, mime, format) || (isAudio ? url : undefined),
modelUrl: mediaValue(result.modelUrl, mime, format) || (isModel ? url : undefined),
previewUrl: mediaValue(result.previewUrl, mime, format),
previewUrl: mediaValue(result.previewUrl, mime, format) || mediaValue(result.preview_url, mime, format) || mediaValue(result.last_frame_url, 'image/png', ''),
resultUrl: url,
resultFormat: format || suffix,
}
}
/** Persisted task kind wins over a missing/edited model catalog entry. */
export function taskMediaType(taskType: unknown, fallback: string): string {
return ({ video: '视频', audio: '音频', image: '图像', '3d': '3D' } as Record<string, string>)[typeof taskType === 'string' ? taskType.toLowerCase() : ''] || fallback
}
export function mediaTypeLabel(type: string): string {
return ({ text: '文本', image: '图片', image_url: '图片', video: '视频', audio: '音频', '3d': '3D', glb: 'GLB 模型', multimodal: '多模态' })[type.toLowerCase()] || type
}
+11 -9
View File
@@ -51,7 +51,7 @@ import UiModal from '../components/UiModal.vue'
import ModelGroupPermissions from '../components/ModelGroupPermissions.vue'
import UsageHistory from '../components/UsageHistory.vue'
import MediaResult from '../components/MediaResult.vue'
import { generationLabel, mediaResult, mediaTypeLabel, safeMediaUrl } from '../services/media'
import { generationLabel, mediaResult, mediaTypeLabel, safeMediaUrl, taskMediaType } from '../services/media'
defineProps<{ view: string; displayName?: string }>()
const emit = defineEmits<{ navigate: [view: string]; notify: [message: string]; balance: [value: number | undefined] }>()
@@ -494,10 +494,12 @@ function normalizeAssistantMedia(value: unknown) {
}
function mapAsyncTask(task: AsyncTaskResult, model: ModelItem, taskPrompt?: string): TaskItem {
const modelUrl = task.output?.modelUrl || task.result?.modelUrl
const videoUrl = task.output?.videoUrl || task.result?.videoUrl
const previewUrl = task.output?.previewUrl || task.result?.previewUrl
const assets = mediaResult({ ...task.result, ...task.output }, model.type)
const type = taskMediaType(task.taskType, model.type)
const assets = mediaResult({ ...task.result, ...task.output }, type)
const modelUrl = assets.modelUrl
const videoUrl = assets.videoUrl
const previewUrl = assets.previewUrl
const deliveryPending = task.result?.deliveryStatus === 'pending'
const imageUrl = assets.imageUrl
const audioUrl = assets.audioUrl
const resultUrl = assets.resultUrl
@@ -513,13 +515,13 @@ function mapAsyncTask(task: AsyncTaskResult, model: ModelItem, taskPrompt?: stri
const createdAt = task.createdAt || task.created_at
return {
id: task.id,
name: model.type === '视频' || task.provider === 'seedance' ? '文字生成视频' : model.type === '图像' ? '生成图片' : model.type === '音频' ? '生成音频' : '生成 3D',
name: type === '视频' || task.provider === 'seedance' ? '文字生成视频' : type === '图像' ? '生成图片' : type === '音频' ? '生成音频' : '生成 3D',
model: model.name,
status: statusMap[task.status],
progress: task.status === 'succeeded' ? 100 : Math.round(task.progress || 0),
createdAt: createdAt ? formatTaskTime(createdAt) : '刚刚',
duration: task.completedAt ? '已完成' : task.status === 'reconciling' ? '等待核账' : '计算中',
result: task.error?.message || (videoUrl ? '视频已生成' : imageUrl ? '图片已生成' : audioUrl ? '音频已生成' : modelUrl ? 'GLB 模型已生成' : resultUrl ? '文件已生成' : task.status === 'succeeded' ? '异步任务已完成' : task.status === 'running' ? `正在生成 · ${Math.round(task.progress || 0)}%` : '等待平台调度'),
result: task.error?.message || (deliveryPending ? '上游生成完成,但结果文件尚未接入;请联系管理员获取,勿重复生成' : videoUrl ? '视频已生成' : imageUrl ? '图片已生成' : audioUrl ? '音频已生成' : modelUrl ? 'GLB 模型已生成' : resultUrl ? '文件已生成' : task.status === 'succeeded' ? '异步任务已完成' : task.status === 'running' ? `正在生成 · ${Math.round(task.progress || 0)}%` : '等待平台调度'),
provider: task.provider,
prompt: taskPrompt,
modelUrl: modelUrl || assets.modelUrl,
@@ -528,7 +530,7 @@ function mapAsyncTask(task: AsyncTaskResult, model: ModelItem, taskPrompt?: stri
imageUrl,
audioUrl,
resultUrl,
resultFormat: task.output?.format || task.result?.format,
resultFormat: assets.resultFormat,
creditsConsumed,
remote: true,
}
@@ -563,7 +565,7 @@ async function pollTask(taskId: string, apiKey: string) {
// Refresh financial state without encouraging another generation request.
await refreshOverview()
historyRevision.value++
emit('notify', latest.status === 'reconciling' ? `${previous.name}结果尚未确认,额度仍冻结,请联系管理员核账,勿重复提交` : latest.status === 'succeeded' ? `${previous.name}完成,可以查看和保存结果` : `${previous.name}未能完成,请查看错误说明`)
emit('notify', latest.status === 'reconciling' ? `${previous.name}结果尚未确认,额度仍冻结,请联系管理员核账,勿重复提交` : latest.result?.deliveryStatus === 'pending' ? '上游已完成,结果文件尚未接入,请联系管理员,勿重复生成' : latest.status === 'succeeded' ? `${previous.name}完成,可以查看和保存结果` : `${previous.name}未能完成,请查看错误说明`)
}
} catch (error) {
taskPollers.delete(taskId)
+18 -1
View File
@@ -1,6 +1,6 @@
import { test } from 'node:test'
import assert from 'node:assert/strict'
import { mediaResult, safeMediaUrl } from '../src/services/media.ts'
import { mediaResult, safeMediaUrl, taskMediaType } from '../src/services/media.ts'
test('mediaResult keeps provider URLs in their declared media lane', () => {
const result = mediaResult({ url: 'https://cdn.example.test/result.mp4', format: 'mp4' }, '视频')
@@ -24,3 +24,20 @@ test('mediaResult supports provider base64 output and nested media objects', ()
assert.match(nested.imageUrl, /^data:image\/png;base64,iVBORw0KGgo=$/)
assert.equal(safeMediaUrl('data:text/html;base64,PGh0bWw+'), undefined)
})
test('persisted task type wins over a missing or edited catalog entry', () => {
assert.equal(taskMediaType('video', '3D'), '视频')
assert.equal(taskMediaType('AUDIO', '3D'), '音频')
assert.equal(taskMediaType(undefined, '图像'), '图像')
const video = mediaResult({ url: 'https://cdn.example.test/v?sig=abc' }, taskMediaType('video', '3D'))
assert.equal(video.videoUrl, 'https://cdn.example.test/v?sig=abc')
assert.equal(video.modelUrl, undefined)
})
test('audio base64 without a file suffix plays as audio', () => {
const audio = mediaResult({ base64: 'ZmFrZQ==', format: 'opus' }, '音频')
assert.equal(audio.audioUrl, 'data:audio/ogg;base64,ZmFrZQ==')
assert.equal(audio.resultFormat, 'opus')
const frame = mediaResult({ video_url: 'https://cdn.example.test/a.mp4', last_frame_url: 'https://cdn.example.test/a.png' }, '视频')
assert.equal(frame.previewUrl, 'https://cdn.example.test/a.png')
})