Compare commits

...
10 Commits
26 changed files with 706 additions and 136 deletions

No files matched your search

+1 -1
View File
@@ -262,7 +262,7 @@ MaaS 模型服务与应用开发
│ └── 面向创作者的 AI 生成 3D 产品 │ └── 面向创作者的 AI 生成 3D 产品
├── 模亦灵枢 / MOYI Model Intelligence Hub ├── 模亦灵枢 / MOYI Model Intelligence Hub
│ └── 面向开发者和企业的多模型智能基础设施 │ └── 面向开发者和企业的多模型智能基础设施
└── 模翎技术 └── 模灵科技
└── 平台底层技术、工程能力和模型研发体系 └── 平台底层技术、工程能力和模型研发体系
``` ```
@@ -199,7 +199,7 @@ public class CatalogService {
Integer priority = integerOrNull(input.get("priority")); Integer priority = integerOrNull(input.get("priority"));
Integer maxConcurrency = integerOrNull(input.get("maxConcurrency")); Integer maxConcurrency = integerOrNull(input.get("maxConcurrency"));
if (priority != null && priority < 0) throw new ApiException("INVALID_MODEL_PRIORITY", "模型路由优先级必须大于等于 0"); if (priority != null && priority < 0) throw new ApiException("INVALID_MODEL_PRIORITY", "模型路由优先级必须大于等于 0");
if (maxConcurrency != null && maxConcurrency < 1) throw new ApiException("INVALID_MODEL_CONCURRENCY", "模型并发上限必须大于等于 1"); if (maxConcurrency != null && maxConcurrency < 0) throw new ApiException("INVALID_MODEL_CONCURRENCY", "模型并发上限必须大于等于 0;0 表示不设置平台分组上限");
jdbc.update( jdbc.update(
"INSERT INTO model_catalog (id, provider_id, model_key, source_label, public_name, upstream_model, model_type, call_mode, description, input_types, output_types, capabilities, pricing, priority, max_concurrency, logical_model_id, status) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, CAST(? AS jsonb), CAST(? AS jsonb), CAST(? AS jsonb), CAST(? AS jsonb), ?, ?, ?, ?)", "INSERT INTO model_catalog (id, provider_id, model_key, source_label, public_name, upstream_model, model_type, call_mode, description, input_types, output_types, capabilities, pricing, priority, max_concurrency, logical_model_id, status) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, CAST(? AS jsonb), CAST(? AS jsonb), CAST(? AS jsonb), CAST(? AS jsonb), ?, ?, ?, ?)",
id, providerId, modelKey, sourceLabel, publicName, upstreamModel, type, mode, description, id, providerId, modelKey, sourceLabel, publicName, upstreamModel, type, mode, description,
@@ -223,7 +223,7 @@ public class CatalogService {
if (findProvider(providerId)==null) throw new ApiException("PROVIDER_NOT_FOUND","渠道不存在",404); if (findProvider(providerId)==null) throw new ApiException("PROVIDER_NOT_FOUND","渠道不存在",404);
if(groupName.length()>160 || upstream.length()>160) throw new ApiException("INVALID_MODEL","分组名称或上游标识过长"); if(groupName.length()>160 || upstream.length()>160) throw new ApiException("INVALID_MODEL","分组名称或上游标识过长");
Integer priority=integerOrNull(value.get("priority")), concurrency=integerOrNull(value.get("maxConcurrency")); Integer priority=integerOrNull(value.get("priority")), concurrency=integerOrNull(value.get("maxConcurrency"));
if ((priority!=null && priority<0) || (concurrency!=null && concurrency<1)) throw new ApiException("INVALID_MODEL_LIMIT","优先级不能小于 0,并发上限至少为 1"); if ((priority!=null && priority<0) || (concurrency!=null && concurrency<0)) throw new ApiException("INVALID_MODEL_LIMIT","优先级和并发上限不能小于 0;并发填 0 表示不设置平台分组上限");
validatePricing(value.get("pricing")); validatePricing(value.get("pricing"));
jdbc.update("UPDATE model_catalog SET logical_model_id=?, provider_id=?, source_label=?, upstream_model=?, public_name=?, model_key=?, model_type=?, call_mode=?, description=?, input_types=CAST(? AS jsonb), output_types=CAST(? AS jsonb), pricing=CAST(? AS jsonb), priority=?, max_concurrency=?, status=?, updated_at=now() WHERE id=?",logicalId,providerId,groupName,upstream,definition.get("publicName"),definition.get("modelKey"),definition.get("type"),definition.get("mode"),definition.get("description"),jsonArray(definition.get("inputTypes")),jsonArray(definition.get("outputTypes")),jsonObjectOrNull(value.get("pricing")),priority,concurrency,status(value),id); jdbc.update("UPDATE model_catalog SET logical_model_id=?, provider_id=?, source_label=?, upstream_model=?, public_name=?, model_key=?, model_type=?, call_mode=?, description=?, input_types=CAST(? AS jsonb), output_types=CAST(? AS jsonb), pricing=CAST(? AS jsonb), priority=?, max_concurrency=?, status=?, updated_at=now() WHERE id=?",logicalId,providerId,groupName,upstream,definition.get("publicName"),definition.get("modelKey"),definition.get("type"),definition.get("mode"),definition.get("description"),jsonArray(definition.get("inputTypes")),jsonArray(definition.get("outputTypes")),jsonObjectOrNull(value.get("pricing")),priority,concurrency,status(value),id);
auth.audit("model_group.updated",admin.id(),new AuthService.RequestMeta(null,null),Map.of("groupId",id,"modelId",logicalId,"providerId",providerId)); auth.audit("model_group.updated",admin.id(),new AuthService.RequestMeta(null,null),Map.of("groupId",id,"modelId",logicalId,"providerId",providerId));
@@ -517,11 +517,15 @@ public class CatalogService {
model.put("status", resultSet.getString("status")); model.put("status", resultSet.getString("status"));
String providerCode = resultSet.getString("provider_code"); String providerCode = resultSet.getString("provider_code");
String providerProtocol = resultSet.getString("provider_protocol"); String providerProtocol = resultSet.getString("provider_protocol");
// Only adapters that are implemented and verified are published as // Keep the catalog state aligned with the real async adapters. The
// executable. Relay catalog rows stay visibly pending until their // OpenAI-compatible adapter handles image, video and speech tasks for
// provider endpoint has been configured and tested; protocol alone is // relay providers; it does not claim support for arbitrary async
// not proof that an image/video/audio task API exists. // model types. Provider-specific adapters cover Tripo 3D and
boolean asyncExecutor = List.of("provider-mock", "provider-vast", "provider-bytedance").contains(resultSet.getString("provider_id")); // Seedance. This prevents a relay route from being shown as
// "异步执行器待接入" when the execution path is already implemented,
// while unsupported task types remain visibly pending.
boolean asyncExecutor = asyncExecutorReady(
resultSet.getString("provider_id"), providerProtocol, resultSet.getString("model_type"));
model.put("executionReady", !"async".equals(resultSet.getString("call_mode")) || asyncExecutor); model.put("executionReady", !"async".equals(resultSet.getString("call_mode")) || asyncExecutor);
model.put("billingReady", billingReady(pricing, resultSet.getString("call_mode"), resultSet.getString("provider_id"))); model.put("billingReady", billingReady(pricing, resultSet.getString("call_mode"), resultSet.getString("provider_id")));
// Keep the internal route UUID for permission storage and expose a // Keep the internal route UUID for permission storage and expose a
@@ -542,6 +546,12 @@ public class CatalogService {
return amount != null; return amount != null;
} }
static boolean asyncExecutorReady(String providerId, String providerProtocol, String modelType) {
if (List.of("provider-mock", "provider-vast", "provider-bytedance").contains(providerId)) return true;
if (!"openai-compatible".equalsIgnoreCase(providerProtocol)) return false;
return List.of("image", "video", "audio").contains(String.valueOf(modelType).toLowerCase(java.util.Locale.ROOT));
}
private boolean billingReady(Map<String,Object> pricing, String mode, String providerId) { private boolean billingReady(Map<String,Object> pricing, String mode, String providerId) {
if ("provider-mock".equals(providerId)) return true; if ("provider-mock".equals(providerId)) return true;
if (pricing == null) return false; if (pricing == null) return false;
@@ -83,8 +83,9 @@ public class GatewayController {
if (model == null) throw new ApiException("MODEL_NOT_FOUND", "模型不存在或已下线;请使用当前 API Key 的 /v1/models 返回的模型 ID", 404); if (model == null) throw new ApiException("MODEL_NOT_FOUND", "模型不存在或已下线;请使用当前 API Key 的 /v1/models 返回的模型 ID", 404);
if ("async".equals(model.get("mode"))) throw new ApiException("ASYNC_MODEL", "该模型需要使用异步任务接口", 400); if ("async".equals(model.get("mode"))) throw new ApiException("ASYNC_MODEL", "该模型需要使用异步任务接口", 400);
String providerId=String.valueOf(model.get("providerId")); String providerId=String.valueOf(model.get("providerId"));
String routeId=String.valueOf(model.get("id"));
boolean mock="provider-mock".equals(providerId); boolean mock="provider-mock".equals(providerId);
if(!mock && !provider.isConfigured(providerId)) throw new ApiException("PROVIDER_NOT_CONFIGURED","该供应商尚未配置密钥,不能执行真实调用",503); if(!mock && !provider.isConfigured(providerId, routeId, String.valueOf(model.get("upstreamModel")))) throw new ApiException("PROVIDER_NOT_CONFIGURED","该供应商尚未配置密钥,不能执行真实调用",503);
ChatMeter.Request request=ChatMeter.request(input,json); ChatMeter.Request request=ChatMeter.request(input,json);
guard.checkRate(UUID.fromString(String.valueOf(key.get("id"))), false); guard.checkRate(UUID.fromString(String.valueOf(key.get("id"))), false);
// Reserve both limits atomically. A queued request never consumes a // Reserve both limits atomically. A queued request never consumes a
@@ -95,10 +96,10 @@ public class GatewayController {
try { try {
catalogService.touchKey(key); catalogService.touchKey(key);
if(Boolean.TRUE.equals(request.body().get("stream"))) { if(Boolean.TRUE.equals(request.body().get("stream"))) {
stream(model,modelId,request.body(),ticket,servletResponse,startedAt,mock); stream(model,modelId,routeId,request.body(),ticket,servletResponse,startedAt,mock);
return null; return null;
} }
Map<String,Object> body=mock?mockResponse(modelId,input):provider.chat(providerId,String.valueOf(model.get("upstreamModel")),request.body()); Map<String,Object> body=mock?mockResponse(modelId,input):provider.chat(providerId,String.valueOf(model.get("upstreamModel")),routeId,request.body());
body.put("model",modelId); body.put("model",modelId);
requireCompletion(body); requireCompletion(body);
var cost=billing.settle(ticket,body,elapsed(startedAt),null); var cost=billing.settle(ticket,body,elapsed(startedAt),null);
@@ -117,7 +118,7 @@ public class GatewayController {
} }
} }
private void stream(Map<String,Object> model, String modelId, Map<String,Object> input, ChatBillingService.Ticket ticket, HttpServletResponse response, long started, boolean mock) throws IOException { private void stream(Map<String,Object> model, String modelId, String routeId, Map<String,Object> input, ChatBillingService.Ticket ticket, HttpServletResponse response, long started, boolean mock) throws IOException {
if(mock) { if(mock) {
var body=mockResponse(modelId,input); var body=mockResponse(modelId,input);
prepareStream(response); prepareStream(response);
@@ -130,7 +131,7 @@ public class GatewayController {
} }
// Keep admission, the concurrency slot and the upstream stream alive on this request thread. // Keep admission, the concurrency slot and the upstream stream alive on this request thread.
// Upstream HTTP errors are inspected before committing a 200/SSE response. // Upstream HTTP errors are inspected before committing a 200/SSE response.
try(var upstream=provider.stream(model.get("providerId").toString(),model.get("upstreamModel").toString(),input)) { try(var upstream=provider.stream(model.get("providerId").toString(),model.get("upstreamModel").toString(),routeId,input)) {
var collector=new ChatStreamCollector(modelId,started); var collector=new ChatStreamCollector(modelId,started);
prepareStream(response); prepareStream(response);
boolean[] disconnected={false}; boolean[] disconnected={false};
@@ -38,6 +38,7 @@ public class GatewayGuard {
private final int readsPerMinute; private final int readsPerMinute;
private final int defaultModelConcurrency; private final int defaultModelConcurrency;
private final int defaultKeyConcurrency; private final int defaultKeyConcurrency;
private final boolean modelConcurrencyEnabled;
private final long queueWaitMs; private final long queueWaitMs;
private final long queuePollMs; private final long queuePollMs;
private final String redisPrefix; private final String redisPrefix;
@@ -58,9 +59,14 @@ public class GatewayGuard {
@Value("${moyi.gateway.default-key-concurrency:4}") int defaultKeyConcurrency, @Value("${moyi.gateway.default-key-concurrency:4}") int defaultKeyConcurrency,
@Value("${moyi.gateway.queue-wait-ms:30000}") long queueWaitMs, @Value("${moyi.gateway.queue-wait-ms:30000}") long queueWaitMs,
@Value("${moyi.gateway.queue-poll-ms:100}") long queuePollMs, @Value("${moyi.gateway.queue-poll-ms:100}") long queuePollMs,
@Value("${moyi.gateway.redis-prefix:moyi:}") String redisPrefix @Value("${moyi.gateway.redis-prefix:moyi:}") String redisPrefix,
@Value("${moyi.gateway.disable-model-concurrency:false}") boolean disableModelConcurrency
) { ) {
if (requestsPerMinute < 1 || readsPerMinute < 1 || defaultModelConcurrency < 1 || defaultKeyConcurrency < 1 // A model concurrency of 0 means "unlimited at the MOYI layer". It
// is useful when measuring the actual upstream/channel limit: the
// provider remains responsible for its own quota while each platform
// API key can still retain an independent limit.
if (requestsPerMinute < 1 || readsPerMinute < 1 || defaultModelConcurrency < 0 || defaultKeyConcurrency < 0
|| queueWaitMs < 0 || queuePollMs < 10) { || queueWaitMs < 0 || queuePollMs < 10) {
throw new IllegalArgumentException("Gateway limits must be positive"); throw new IllegalArgumentException("Gateway limits must be positive");
} }
@@ -69,6 +75,7 @@ public class GatewayGuard {
this.readsPerMinute = readsPerMinute; this.readsPerMinute = readsPerMinute;
this.defaultModelConcurrency = defaultModelConcurrency; this.defaultModelConcurrency = defaultModelConcurrency;
this.defaultKeyConcurrency = defaultKeyConcurrency; this.defaultKeyConcurrency = defaultKeyConcurrency;
this.modelConcurrencyEnabled = !disableModelConcurrency;
this.queueWaitMs = queueWaitMs; this.queueWaitMs = queueWaitMs;
this.queuePollMs = queuePollMs; this.queuePollMs = queuePollMs;
if (redisPrefix == null || !redisPrefix.matches("[A-Za-z0-9:_-]{2,80}:")) { if (redisPrefix == null || !redisPrefix.matches("[A-Za-z0-9:_-]{2,80}:")) {
@@ -77,6 +84,14 @@ public class GatewayGuard {
this.redisPrefix = redisPrefix; this.redisPrefix = redisPrefix;
} }
/** Compatibility constructor for callers that do not configure the measurement switch. */
public GatewayGuard(StringRedisTemplate redis, int requestsPerMinute, int readsPerMinute,
int defaultModelConcurrency, int defaultKeyConcurrency, long queueWaitMs, long queuePollMs,
String redisPrefix) {
this(redis, requestsPerMinute, readsPerMinute, defaultModelConcurrency, defaultKeyConcurrency,
queueWaitMs, queuePollMs, redisPrefix, false);
}
/** Compatibility constructor for isolated unit tests and local callers. */ /** Compatibility constructor for isolated unit tests and local callers. */
public GatewayGuard(StringRedisTemplate redis, int requestsPerMinute, int readsPerMinute, public GatewayGuard(StringRedisTemplate redis, int requestsPerMinute, int readsPerMinute,
int defaultModelConcurrency, String redisPrefix) { int defaultModelConcurrency, String redisPrefix) {
@@ -99,9 +114,12 @@ public class GatewayGuard {
public int modelConcurrency(Map<String, Object> model) { public int modelConcurrency(Map<String, Object> model) {
Object limit = model.get("maxConcurrency"); Object limit = model.get("maxConcurrency");
return limit instanceof Number number && number.intValue() > 0 ? number.intValue() : defaultModelConcurrency; return limit instanceof Number number && number.intValue() >= 0 ? number.intValue() : defaultModelConcurrency;
} }
/** True when MOYI enforces the model/group concurrency slot. */
public boolean modelConcurrencyEnabled() { return modelConcurrencyEnabled; }
public Slot acquire(Map<String, Object> model) { public Slot acquire(Map<String, Object> model) {
return acquire(model, 0); return acquire(model, 0);
} }
@@ -112,6 +130,7 @@ public class GatewayGuard {
* synchronous while preventing an unbounded servlet-thread backlog. * synchronous while preventing an unbounded servlet-thread backlog.
*/ */
public Slot acquire(Map<String, Object> model, long requestedWaitMs) { public Slot acquire(Map<String, Object> model, long requestedWaitMs) {
if (!modelConcurrencyEnabled) return new Slot(null, null);
String bucket = redisPrefix + "gateway:slots:model:" + model.get("id"); String bucket = redisPrefix + "gateway:slots:model:" + model.get("id");
return acquireSlot(bucket, modelConcurrency(model), "MODEL_BUSY", "当前模型并发已满,已进入排队,请稍后重试", requestedWaitMs); return acquireSlot(bucket, modelConcurrency(model), "MODEL_BUSY", "当前模型并发已满,已进入排队,请稍后重试", requestedWaitMs);
} }
@@ -137,6 +156,7 @@ public class GatewayGuard {
/** Reads live leases rather than historical usage rows. Expired workers do not count as running. */ /** Reads live leases rather than historical usage rows. Expired workers do not count as running. */
public CapacitySnapshot capacitySnapshot(List<String> modelIds) { public CapacitySnapshot capacitySnapshot(List<String> modelIds) {
if (!modelConcurrencyEnabled) return new CapacitySnapshot(true, Map.of());
if (modelIds.isEmpty()) return new CapacitySnapshot(true, Map.of()); if (modelIds.isEmpty()) return new CapacitySnapshot(true, Map.of());
List<String> buckets = new ArrayList<>(); List<String> buckets = new ArrayList<>();
for (String id : modelIds) { for (String id : modelIds) {
@@ -159,6 +179,12 @@ public class GatewayGuard {
/** Atomically reserves the model and API-Key slots, so queued requests do not hold one scarce slot while waiting for the other. */ /** Atomically reserves the model and API-Key slots, so queued requests do not hold one scarce slot while waiting for the other. */
public Admission acquire(Map<String, Object> model, UUID keyId, long requestedWaitMs) { public Admission acquire(Map<String, Object> model, UUID keyId, long requestedWaitMs) {
if (!modelConcurrencyEnabled) {
// Measurement mode removes only MOYI's per-model/group guard. The
// independent per-platform-Key limit remains active so one client
// cannot accidentally consume the entire gateway indefinitely.
return new Admission(new Slot(null, null), acquireKey(keyId, requestedWaitMs));
}
String modelBucket = redisPrefix + "gateway:slots:model:" + model.get("id"); String modelBucket = redisPrefix + "gateway:slots:model:" + model.get("id");
String keyBucket = redisPrefix + "gateway:slots:key:" + keyId; String keyBucket = redisPrefix + "gateway:slots:key:" + keyId;
String waitingBucket = redisPrefix + "gateway:waiting:model:" + model.get("id"); String waitingBucket = redisPrefix + "gateway:waiting:model:" + model.get("id");
@@ -167,9 +193,23 @@ public class GatewayGuard {
long deadline = System.nanoTime() + java.util.concurrent.TimeUnit.MILLISECONDS.toNanos(waitMs); long deadline = System.nanoTime() + java.util.concurrent.TimeUnit.MILLISECONDS.toNanos(waitMs);
boolean admitted = false; boolean admitted = false;
try { try {
int modelLimit = modelConcurrency(model);
// Do not create a Redis model lease when the route (or the
// server default) is explicitly unlimited. This removes the
// MOYI-side upstream-group cap while preserving API-Key slots.
if (modelLimit <= 0) {
Slot keySlot = acquireSlot(keyBucket, defaultKeyConcurrency, "KEY_BUSY", "当前 API Key 的并发已满,已进入排队,请稍后重试", requestedWaitMs);
admitted = true;
return new Admission(null, keySlot);
}
if (defaultKeyConcurrency <= 0) {
Slot modelSlot = acquireSlot(modelBucket, modelLimit, "MODEL_BUSY", "当前模型并发已满,已进入排队,请稍后重试", requestedWaitMs);
admitted = true;
return new Admission(modelSlot, null);
}
while (true) { while (true) {
long remaining = Math.max(0, java.util.concurrent.TimeUnit.NANOSECONDS.toMillis(deadline - System.nanoTime())); long remaining = Math.max(0, java.util.concurrent.TimeUnit.NANOSECONDS.toMillis(deadline - System.nanoTime()));
long acquired = executePair(modelBucket, keyBucket, waitingBucket, Integer.toString(modelConcurrency(model)), Integer.toString(defaultKeyConcurrency), Long.toString(SLOT_TTL_MS), token, Long.toString(remaining), Long.toString(Math.max(SLOT_TTL_MS, waitMs + 1000))); long acquired = executePair(modelBucket, keyBucket, waitingBucket, Integer.toString(modelLimit), Integer.toString(defaultKeyConcurrency), Long.toString(SLOT_TTL_MS), token, Long.toString(remaining), Long.toString(Math.max(SLOT_TTL_MS, waitMs + 1000)));
if (acquired == 1) { if (acquired == 1) {
admitted = true; // The acquisition script also removed the waiting entry. admitted = true; // The acquisition script also removed the waiting entry.
return new Admission(modelBucket, keyBucket, token); return new Admission(modelBucket, keyBucket, token);
@@ -191,6 +231,7 @@ public class GatewayGuard {
} }
private Slot acquireSlot(String bucket, int limit, String code, String message, long requestedWaitMs) { private Slot acquireSlot(String bucket, int limit, String code, String message, long requestedWaitMs) {
if (limit <= 0) return new Slot(null, null);
String token = UUID.randomUUID().toString(); String token = UUID.randomUUID().toString();
long waitMs = Math.max(0, Math.min(queueWaitMs, requestedWaitMs > 0 ? requestedWaitMs : queueWaitMs)); long waitMs = Math.max(0, Math.min(queueWaitMs, requestedWaitMs > 0 ? requestedWaitMs : queueWaitMs));
long deadline = System.nanoTime() + java.util.concurrent.TimeUnit.MILLISECONDS.toNanos(waitMs); long deadline = System.nanoTime() + java.util.concurrent.TimeUnit.MILLISECONDS.toNanos(waitMs);
@@ -244,15 +285,20 @@ public class GatewayGuard {
private final AtomicBoolean closed = new AtomicBoolean(); private final AtomicBoolean closed = new AtomicBoolean();
private Admission(String modelBucket, String keyBucket, String token) { private Admission(String modelBucket, String keyBucket, String token) {
this.modelSlot = new Slot(modelBucket, token); this.modelSlot = modelBucket == null ? null : new Slot(modelBucket, token);
this.keySlot = new Slot(keyBucket, token); this.keySlot = keyBucket == null ? null : new Slot(keyBucket, token);
}
private Admission(Slot modelSlot, Slot keySlot) {
this.modelSlot = modelSlot;
this.keySlot = keySlot;
} }
@Override @Override
public void close() { public void close() {
if (!closed.compareAndSet(false, true)) return; if (!closed.compareAndSet(false, true)) return;
modelSlot.close(); if (modelSlot != null) modelSlot.close();
keySlot.close(); if (keySlot != null) keySlot.close();
} }
} }
@@ -266,6 +312,7 @@ public class GatewayGuard {
@Override @Override
public void close() { public void close() {
if (!closed.compareAndSet(false, true)) return; if (!closed.compareAndSet(false, true)) return;
if (key == null || token == null) return;
// A failed release expires automatically and must not replace the upstream response. // A failed release expires automatically and must not replace the upstream response.
releaseQuietly(key, token); releaseQuietly(key, token);
} }
@@ -65,8 +65,18 @@ public class OpenAiCompatibleProvider {
return provider != null && provider.hasKey(); return provider != null && provider.hasKey();
} }
public boolean isConfigured(String providerId, String routeId, String upstreamModel) {
ProviderConfig provider = resolve(providerId);
return provider != null && provider.hasKey(routeId, upstreamModel, environment);
}
public Map<String, Object> chat(String providerId, String upstreamModel, Map<String,Object> input) { public Map<String, Object> chat(String providerId, String upstreamModel, Map<String,Object> input) {
try(var response=open(providerId,upstreamModel,input,false)) { return chat(providerId, upstreamModel, null, input);
}
/** Route-aware overload. A route-specific key pool is preferred when configured. */
public Map<String, Object> chat(String providerId, String upstreamModel, String routeId, Map<String,Object> input) {
try(var response=open(providerId,upstreamModel,routeId,input,false)) {
byte[] bytes=response.body.readNBytes(4*1024*1024+1); byte[] bytes=response.body.readNBytes(4*1024*1024+1);
if(bytes.length>4*1024*1024) throw new ApiException("UPSTREAM_RESPONSE_TOO_LARGE","上游响应超过限制",502); if(bytes.length>4*1024*1024) throw new ApiException("UPSTREAM_RESPONSE_TOO_LARGE","上游响应超过限制",502);
Map<String,Object> body=objectMapper.readValue(bytes, new com.fasterxml.jackson.core.type.TypeReference<>() {}); Map<String,Object> body=objectMapper.readValue(bytes, new com.fasterxml.jackson.core.type.TypeReference<>() {});
@@ -80,12 +90,17 @@ public class OpenAiCompatibleProvider {
} }
public StreamResponse stream(String providerId, String upstreamModel, Map<String,Object> input) { public StreamResponse stream(String providerId, String upstreamModel, Map<String,Object> input) {
return open(providerId,upstreamModel,input,true); return stream(providerId, upstreamModel, null, input);
} }
private StreamResponse open(String providerId, String upstreamModel, Map<String,Object> input, boolean streaming) { /** Route-aware overload. A route-specific key pool is preferred when configured. */
public StreamResponse stream(String providerId, String upstreamModel, String routeId, Map<String,Object> input) {
return open(providerId,upstreamModel,routeId,input,true);
}
private StreamResponse open(String providerId, String upstreamModel, String routeId, Map<String,Object> input, boolean streaming) {
ProviderConfig provider = resolve(providerId); ProviderConfig provider = resolve(providerId);
if (provider == null || !provider.hasKey()) { if (provider == null || !provider.hasKey(routeId, upstreamModel, environment)) {
throw new ApiException("PROVIDER_NOT_CONFIGURED", "该模型的供应商尚未配置服务端 API Key", 503); throw new ApiException("PROVIDER_NOT_CONFIGURED", "该模型的供应商尚未配置服务端 API Key", 503);
} }
Map<String, Object> body = new LinkedHashMap<>(input); Map<String, Object> body = new LinkedHashMap<>(input);
@@ -93,7 +108,7 @@ public class OpenAiCompatibleProvider {
body.put("stream", streaming); body.put("stream", streaming);
if(streaming) body.put("stream_options",Map.of("include_usage",true)); if(streaming) body.put("stream_options",Map.of("include_usage",true));
else body.remove("stream_options"); else body.remove("stream_options");
String apiKey = provider.nextApiKey(); String apiKey = provider.nextApiKey(routeId, upstreamModel, environment, keyCursors);
for (int attempt = 0; attempt < 2; attempt++) { for (int attempt = 0; attempt < 2; attempt++) {
String json = writeJson(body); String json = writeJson(body);
HttpRequest request = HttpRequest.newBuilder() HttpRequest request = HttpRequest.newBuilder()
@@ -280,7 +295,7 @@ public class OpenAiCompatibleProvider {
if (fallbackKey != null && !fallbackKey.isBlank()) keys.add(fallbackKey.trim()); if (fallbackKey != null && !fallbackKey.isBlank()) keys.add(fallbackKey.trim());
addEnvironmentKeys(keys, providerId, environment); addEnvironmentKeys(keys, providerId, environment);
if (providerCode != null && !providerCode.isBlank() && !providerCode.equals(providerId)) addEnvironmentKeys(keys, providerCode, environment); if (providerCode != null && !providerCode.isBlank() && !providerCode.equals(providerId)) addEnvironmentKeys(keys, providerCode, environment);
return new ProviderConfig(keys.stream().distinct().toList(), baseUrl, cursor); return new ProviderConfig(providerId, providerCode, keys.stream().distinct().toList(), baseUrl, cursor);
} }
private void addEnvironmentKeys(List<String> keys, String alias, org.springframework.core.env.Environment environment) { private void addEnvironmentKeys(List<String> keys, String alias, org.springframework.core.env.Environment environment) {
@@ -289,8 +304,45 @@ public class OpenAiCompatibleProvider {
if (configured != null && !configured.isBlank()) for (String key : configured.split("\\|")) if (!key.isBlank()) keys.add(key.trim()); if (configured != null && !configured.isBlank()) for (String key : configured.split("\\|")) if (!key.isBlank()) keys.add(key.trim());
} }
private record ProviderConfig(List<String> apiKeys, String baseUrl, AtomicInteger cursor) { private record ProviderConfig(String alias, String providerCode, List<String> apiKeys, String baseUrl, AtomicInteger cursor) {
private boolean hasKey() { return !apiKeys.isEmpty(); } private boolean hasKey() { return !apiKeys.isEmpty(); }
private boolean hasKey(String routeId, String upstreamModel, org.springframework.core.env.Environment environment) {
return !apiKeys.isEmpty() || !routeKeys(routeId, upstreamModel, environment).isEmpty();
}
private String nextApiKey() { return apiKeys.get(Math.floorMod(cursor.getAndIncrement(), apiKeys.size())); } private String nextApiKey() { return apiKeys.get(Math.floorMod(cursor.getAndIncrement(), apiKeys.size())); }
private String nextApiKey(String routeId, String upstreamModel, org.springframework.core.env.Environment environment,
ConcurrentMap<String, AtomicInteger> cursors) {
List<String> routeKeys = routeKeys(routeId, upstreamModel, environment);
if (routeKeys.isEmpty()) return nextApiKey();
String cursorKey = "route:" + (routeId == null ? "" : routeId);
AtomicInteger routeCursor = cursors.computeIfAbsent(cursorKey, ignored -> new AtomicInteger());
return routeKeys.get(Math.floorMod(routeCursor.getAndIncrement(), routeKeys.size()));
}
private List<String> routeKeys(String routeId, String upstreamModel, org.springframework.core.env.Environment environment) {
if (environment == null) return List.of();
if (routeId != null && !routeId.isBlank()) {
String value = environment.getProperty("MOYI_UPSTREAM_API_KEYS_ROUTE_" + normalize(routeId));
if (value != null && !value.isBlank()) return parseKeys(value);
}
if (upstreamModel != null && !upstreamModel.isBlank()) {
String scopedName = "MOYI_UPSTREAM_API_KEYS_MODEL_" + normalize(providerCode) + "_" + normalize(upstreamModel);
String value = environment.getProperty(scopedName);
if (value == null || value.isBlank()) return List.of();
return parseKeys(value);
}
return List.of();
}
private static List<String> parseKeys(String value) {
List<String> keys = new ArrayList<>();
for (String key : value.split("\\|")) if (!key.isBlank()) keys.add(key.trim());
return keys.stream().distinct().toList();
}
private static String normalize(String value) {
return String.valueOf(value == null ? "" : value).toUpperCase(java.util.Locale.ROOT).replaceAll("[^A-Z0-9]", "_");
}
} }
} }
@@ -13,6 +13,9 @@ import java.net.http.HttpResponse;
import java.time.Duration; import java.time.Duration;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Base64; import java.util.Base64;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
@@ -29,6 +32,7 @@ import org.springframework.stereotype.Service;
/** Real async adapters for the providers currently represented in the catalog. */ /** Real async adapters for the providers currently represented in the catalog. */
@Service @Service
public class AsyncProviderClient { public class AsyncProviderClient {
private static final Duration UPSTREAM_REQUEST_TIMEOUT = Duration.ofSeconds(60);
private final JdbcTemplate jdbc; private final JdbcTemplate jdbc;
private final SecretCipher cipher; private final SecretCipher cipher;
private final ObjectMapper mapper; private final ObjectMapper mapper;
@@ -46,10 +50,18 @@ public class AsyncProviderClient {
return submit(providerId, upstreamModel, taskType, Map.of("prompt", prompt)); return submit(providerId, upstreamModel, taskType, Map.of("prompt", prompt));
} }
public Map<String,Object> submit(String providerId, String upstreamModel, String routeId, String taskType, String prompt) {
return submit(providerId, upstreamModel, routeId, taskType, Map.of("prompt", prompt));
}
public Map<String,Object> submit(String providerId, String upstreamModel, String taskType, Map<String,Object> input) { public Map<String,Object> submit(String providerId, String upstreamModel, String taskType, Map<String,Object> input) {
return submit(providerId, upstreamModel, null, taskType, input);
}
public Map<String,Object> submit(String providerId, String upstreamModel, String routeId, String taskType, Map<String,Object> input) {
if (input == null) input = Map.of(); if (input == null) input = Map.of();
String prompt = String.valueOf(input.getOrDefault("prompt", "")); String prompt = String.valueOf(input.getOrDefault("prompt", ""));
Provider provider = provider(providerId); Provider provider = provider(providerId, routeId, upstreamModel);
String code = provider.code().toLowerCase(java.util.Locale.ROOT); String code = provider.code().toLowerCase(java.util.Locale.ROOT);
if (isTripo(provider.code(), provider.baseUrl())) { if (isTripo(provider.code(), provider.baseUrl())) {
Map<String,Object> body = new LinkedHashMap<>(); Map<String,Object> body = new LinkedHashMap<>();
@@ -59,7 +71,8 @@ public class AsyncProviderClient {
else if (!prompt.isBlank()) body.put("prompt", prompt); else if (!prompt.isBlank()) body.put("prompt", prompt);
body.put("texture", true); body.put("texture", true);
body.put("pbr", true); body.put("pbr", true);
return request("POST", tripoBase(provider.baseUrl()) + (media == null ? "/generation/text-to-model" : "/generation/image-to-model"), provider.nextApiKey(), body, "上游 3D 任务创建失败"); String apiKey = provider.nextApiKey(routeId, upstreamModel, environment, keyCursors);
return withKeyFingerprint(request("POST", tripoBase(provider.baseUrl()) + (media == null ? "/generation/text-to-model" : "/generation/image-to-model"), apiKey, body, "上游 3D 任务创建失败"), apiKey);
} }
if (code.contains("byte") || code.contains("seedance") || provider.baseUrl().contains("volces.com")) { if (code.contains("byte") || code.contains("seedance") || provider.baseUrl().contains("volces.com")) {
List<Map<String,Object>> content = new ArrayList<>(); List<Map<String,Object>> content = new ArrayList<>();
@@ -70,25 +83,39 @@ public class AsyncProviderClient {
content.add(Map.of("type", "image_url", "image_url", Map.of("url", media))); content.add(Map.of("type", "image_url", "image_url", Map.of("url", media)));
} }
Map<String,Object> body = Map.of("model", upstreamModel, "content", content); Map<String,Object> body = Map.of("model", upstreamModel, "content", content);
return request("POST", trim(provider.baseUrl()) + "/contents/generations/tasks", provider.nextApiKey(), body, "上游视频任务创建失败"); String apiKey = provider.nextApiKey(routeId, upstreamModel, environment, keyCursors);
return withKeyFingerprint(request("POST", trim(provider.baseUrl()) + "/contents/generations/tasks", apiKey, body, "上游视频任务创建失败"), apiKey);
} }
if ("openai-compatible".equalsIgnoreCase(provider.protocol())) { if ("openai-compatible".equalsIgnoreCase(provider.protocol())) {
return submitOpenAiCompatible(provider, upstreamModel, taskType, input); return submitOpenAiCompatible(provider, routeId, upstreamModel, taskType, input);
} }
throw new ApiException("ASYNC_PROVIDER_NOT_IMPLEMENTED", "该异步渠道没有可用的执行器", 501); throw new ApiException("ASYNC_PROVIDER_NOT_IMPLEMENTED", "该异步渠道没有可用的执行器", 501);
} }
public Map<String,Object> poll(String providerId, String providerTaskId) { public Map<String,Object> poll(String providerId, String providerTaskId) {
Provider provider = provider(providerId); return poll(providerId, null, providerTaskId);
}
public Map<String,Object> poll(String providerId, String routeId, String providerTaskId) {
return poll(providerId, routeId, null, providerTaskId);
}
public Map<String,Object> poll(String providerId, String routeId, String upstreamModel, String providerTaskId) {
return poll(providerId, routeId, upstreamModel, providerTaskId, null);
}
public Map<String,Object> poll(String providerId, String routeId, String upstreamModel, String providerTaskId, String keyFingerprint) {
Provider provider = provider(providerId, routeId, upstreamModel);
String code = provider.code().toLowerCase(java.util.Locale.ROOT); String code = provider.code().toLowerCase(java.util.Locale.ROOT);
String url; String url;
String apiKey = provider.keyForFingerprint(routeId, upstreamModel, environment, keyCursors, keyFingerprint);
if (isTripo(provider.code(), provider.baseUrl())) url = tripoBase(provider.baseUrl()) + "/tasks/" + encode(providerTaskId); if (isTripo(provider.code(), provider.baseUrl())) url = tripoBase(provider.baseUrl()) + "/tasks/" + encode(providerTaskId);
else if (code.contains("byte") || code.contains("seedance") || provider.baseUrl().contains("volces.com")) url = trim(provider.baseUrl()) + "/contents/generations/tasks/" + encode(providerTaskId); else if (code.contains("byte") || code.contains("seedance") || provider.baseUrl().contains("volces.com")) url = trim(provider.baseUrl()) + "/contents/generations/tasks/" + encode(providerTaskId);
else if ("openai-compatible".equalsIgnoreCase(provider.protocol())) { else if ("openai-compatible".equalsIgnoreCase(provider.protocol())) {
return pollOpenAiCompatible(provider, providerTaskId); return pollOpenAiCompatible(provider, routeId, upstreamModel, providerTaskId, keyFingerprint);
} }
else throw new ApiException("ASYNC_PROVIDER_NOT_IMPLEMENTED", "该异步渠道没有可用的执行器", 501); else throw new ApiException("ASYNC_PROVIDER_NOT_IMPLEMENTED", "该异步渠道没有可用的执行器", 501);
return request("GET", url, provider.nextApiKey(), null, "上游异步任务查询失败"); return request("GET", url, apiKey, null, "上游异步任务查询失败");
} }
/** /**
@@ -97,7 +124,7 @@ public class AsyncProviderClient {
* generation completes in the request, so mark the normalized result for * generation completes in the request, so mark the normalized result for
* AsyncTaskService to settle immediately instead of inventing a task id. * AsyncTaskService to settle immediately instead of inventing a task id.
*/ */
private Map<String,Object> submitOpenAiCompatible(Provider provider, String model, String taskType, Map<String,Object> input) { private Map<String,Object> submitOpenAiCompatible(Provider provider, String routeId, String model, String taskType, Map<String,Object> input) {
String prompt = String.valueOf(input.getOrDefault("prompt", "")); String prompt = String.valueOf(input.getOrDefault("prompt", ""));
String media = mediaUrl(input); String media = mediaUrl(input);
String base = trim(provider.baseUrl()); String base = trim(provider.baseUrl());
@@ -110,12 +137,13 @@ public class AsyncProviderClient {
// choose its documented default and normalize either shape. // choose its documented default and normalize either shape.
copyParameter(input, body, "response_format"); copyParameter(input, body, "response_format");
copyParameter(input, body, "size"); copyParameter(input, body, "size");
String apiKey = provider.nextApiKey(routeId, model, environment, keyCursors);
Map<String,Object> response = media == null Map<String,Object> response = media == null
? request("POST", base + "/images/generations", provider.nextApiKey(), body, "上游图片任务创建失败") ? request("POST", base + "/images/generations", apiKey, body, "上游图片任务创建失败")
: requestMultipart("POST", base + "/images/edits", provider.nextApiKey(), body, "image", media, "上游图片任务创建失败"); : requestMultipart("POST", base + "/images/edits", apiKey, body, "image", media, "上游图片任务创建失败");
Map<String,Object> result = compatibleResult(response); Map<String,Object> result = compatibleResult(response);
if (result != null) return Map.of("_moyiInlineResult", result); if (result != null) return Map.of("_moyiInlineResult", result, "_moyiUpstreamKeyFingerprint", keyFingerprint(apiKey));
return response; return withKeyFingerprint(response, apiKey);
} }
if ("video".equalsIgnoreCase(taskType)) { if ("video".equalsIgnoreCase(taskType)) {
Map<String,Object> body = new LinkedHashMap<>(); Map<String,Object> body = new LinkedHashMap<>();
@@ -124,16 +152,16 @@ public class AsyncProviderClient {
// The standard OpenAI video endpoint is multipart even when no // The standard OpenAI video endpoint is multipart even when no
// reference file is supplied. JSON requests are rejected by the // reference file is supplied. JSON requests are rejected by the
// official API and by relays that proxy it faithfully. // official API and by relays that proxy it faithfully.
String apiKey = provider.nextApiKey(); String apiKey = provider.nextApiKey(routeId, model, environment, keyCursors);
try { try {
return requestMultipart("POST", base + "/videos", apiKey, body, return withKeyFingerprint(requestMultipart("POST", base + "/videos", apiKey, body,
media == null ? null : "input_reference", media, "上游视频任务创建失败"); media == null ? null : "input_reference", media, "上游视频任务创建失败"), apiKey);
} catch (ApiException first) { } catch (ApiException first) {
// A few older relays retained /videos/generations. Retry only // A few older relays retained /videos/generations. Retry only
// on a route miss; authentication/credit errors are returned. // on a route miss; authentication/credit errors are returned.
if (!isNotFound(first)) throw first; if (!isNotFound(first)) throw first;
return requestMultipart("POST", base + "/videos/generations", apiKey, body, return withKeyFingerprint(requestMultipart("POST", base + "/videos/generations", apiKey, body,
media == null ? null : "input_reference", media, "上游视频任务创建失败"); media == null ? null : "input_reference", media, "上游视频任务创建失败"), apiKey);
} }
} }
if ("audio".equalsIgnoreCase(taskType)) { if ("audio".equalsIgnoreCase(taskType)) {
@@ -146,7 +174,8 @@ public class AsyncProviderClient {
body.put("voice", input.getOrDefault("voice", "alloy")); body.put("voice", input.getOrDefault("voice", "alloy"));
body.put("response_format", input.getOrDefault("response_format", "mp3")); body.put("response_format", input.getOrDefault("response_format", "mp3"));
copyParameter(input, body, "speed"); copyParameter(input, body, "speed");
return Map.of("_moyiInlineResult", requestAudio("POST", base + "/audio/speech", provider.nextApiKey(), body, "上游语音任务创建失败")); String apiKey = provider.nextApiKey(routeId, model, environment, keyCursors);
return Map.of("_moyiInlineResult", requestAudio("POST", base + "/audio/speech", apiKey, body, "上游语音任务创建失败"), "_moyiUpstreamKeyFingerprint", keyFingerprint(apiKey));
} }
throw new ApiException("ASYNC_PROVIDER_NOT_IMPLEMENTED", "该异步渠道的媒体接口尚未配置", 501); throw new ApiException("ASYNC_PROVIDER_NOT_IMPLEMENTED", "该异步渠道的媒体接口尚未配置", 501);
} }
@@ -170,9 +199,9 @@ public class AsyncProviderClient {
return value; return value;
} }
private Map<String,Object> pollOpenAiCompatible(Provider provider, String providerTaskId) { private Map<String,Object> pollOpenAiCompatible(Provider provider, String routeId, String upstreamModel, String providerTaskId, String keyFingerprint) {
String base = trim(provider.baseUrl()); String base = trim(provider.baseUrl());
String apiKey = provider.nextApiKey(); String apiKey = provider.keyForFingerprint(routeId, upstreamModel, environment, keyCursors, keyFingerprint);
try { try {
return request("GET", base + "/videos/" + encode(providerTaskId), apiKey, null, "上游异步任务查询失败"); return request("GET", base + "/videos/" + encode(providerTaskId), apiKey, null, "上游异步任务查询失败");
} catch (ApiException first) { } catch (ApiException first) {
@@ -208,7 +237,7 @@ public class AsyncProviderClient {
private Map<String,Object> requestAudio(String method, String url, String key, Map<String,Object> body, String message) { private Map<String,Object> requestAudio(String method, String url, String key, Map<String,Object> body, String message) {
try { try {
HttpRequest.Builder builder = HttpRequest.newBuilder().uri(URI.create(url)).timeout(Duration.ofSeconds(30)) HttpRequest.Builder builder = HttpRequest.newBuilder().uri(URI.create(url)).timeout(UPSTREAM_REQUEST_TIMEOUT)
.header("Authorization", "Bearer " + key).header("Content-Type", "application/json"); .header("Authorization", "Bearer " + key).header("Content-Type", "application/json");
HttpResponse<byte[]> response = client.send(builder.POST(HttpRequest.BodyPublishers.ofString(write(body))).build(), HttpResponse.BodyHandlers.ofByteArray()); HttpResponse<byte[]> response = client.send(builder.POST(HttpRequest.BodyPublishers.ofString(write(body))).build(), HttpResponse.BodyHandlers.ofByteArray());
if (response.statusCode() < 200 || response.statusCode() >= 300) { if (response.statusCode() < 200 || response.statusCode() >= 300) {
@@ -253,7 +282,7 @@ public class AsyncProviderClient {
multipartFile(out, boundary, fileField, file.filename(), file.contentType(), file.bytes()); multipartFile(out, boundary, fileField, file.filename(), file.contentType(), file.bytes());
} }
out.write(("--" + boundary + "--\r\n").getBytes(java.nio.charset.StandardCharsets.UTF_8)); out.write(("--" + boundary + "--\r\n").getBytes(java.nio.charset.StandardCharsets.UTF_8));
HttpRequest request = HttpRequest.newBuilder().uri(URI.create(url)).timeout(Duration.ofSeconds(30)) HttpRequest request = HttpRequest.newBuilder().uri(URI.create(url)).timeout(UPSTREAM_REQUEST_TIMEOUT)
.header("Authorization", "Bearer " + key) .header("Authorization", "Bearer " + key)
.header("Content-Type", "multipart/form-data; boundary=" + boundary) .header("Content-Type", "multipart/form-data; boundary=" + boundary)
.POST(HttpRequest.BodyPublishers.ofByteArray(out.toByteArray())).build(); .POST(HttpRequest.BodyPublishers.ofByteArray(out.toByteArray())).build();
@@ -335,7 +364,7 @@ public class AsyncProviderClient {
private Map<String,Object> request(String method, String url, String key, Map<String,Object> body, String message) { private Map<String,Object> request(String method, String url, String key, Map<String,Object> body, String message) {
try { try {
HttpRequest.Builder builder = HttpRequest.newBuilder().uri(URI.create(url)).timeout(Duration.ofSeconds(30)) HttpRequest.Builder builder = HttpRequest.newBuilder().uri(URI.create(url)).timeout(UPSTREAM_REQUEST_TIMEOUT)
.header("Authorization", "Bearer " + key).header("Content-Type", "application/json"); .header("Authorization", "Bearer " + key).header("Content-Type", "application/json");
if ("POST".equals(method)) builder.POST(HttpRequest.BodyPublishers.ofString(write(body))); if ("POST".equals(method)) builder.POST(HttpRequest.BodyPublishers.ofString(write(body)));
else builder.GET(); else builder.GET();
@@ -362,6 +391,10 @@ public class AsyncProviderClient {
} }
private Provider provider(String id) { private Provider provider(String id) {
return provider(id, null, null);
}
private Provider provider(String id, String routeId, String upstreamModel) {
try { try {
Map<String,Object> row = jdbc.queryForMap("SELECT code, protocol, base_url, credential_ciphertext FROM model_providers WHERE id=? AND status='active' AND deleted_at IS NULL", id); Map<String,Object> row = jdbc.queryForMap("SELECT code, protocol, base_url, credential_ciphertext FROM model_providers WHERE id=? AND status='active' AND deleted_at IS NULL", id);
String ciphertext = row.get("credential_ciphertext") == null ? "" : String.valueOf(row.get("credential_ciphertext")); String ciphertext = row.get("credential_ciphertext") == null ? "" : String.valueOf(row.get("credential_ciphertext"));
@@ -377,7 +410,7 @@ public class AsyncProviderClient {
addEnvironmentKeys(keys, "TRIPO_API_KEY"); addEnvironmentKeys(keys, "TRIPO_API_KEY");
addEnvironmentKeys(keys, "MOYI_TRIPO_API_KEY"); addEnvironmentKeys(keys, "MOYI_TRIPO_API_KEY");
} }
if (keys.isEmpty()) throw new ApiException("PROVIDER_NOT_CONFIGURED", "异步渠道尚未配置 API Key", 503); if (keys.isEmpty() && routeKeys(code, id, routeId, upstreamModel).isEmpty()) throw new ApiException("PROVIDER_NOT_CONFIGURED", "异步渠道尚未配置 API Key", 503);
return new Provider(code, String.valueOf(row.get("protocol")), String.valueOf(row.get("base_url")), keys.stream().distinct().toList(), keyCursors.computeIfAbsent(id, ignored -> new AtomicInteger())); return new Provider(code, String.valueOf(row.get("protocol")), String.valueOf(row.get("base_url")), keys.stream().distinct().toList(), keyCursors.computeIfAbsent(id, ignored -> new AtomicInteger()));
} catch (EmptyResultDataAccessException exception) { throw new ApiException("PROVIDER_NOT_CONFIGURED", "异步渠道不存在或已停用", 503); } } catch (EmptyResultDataAccessException exception) { throw new ApiException("PROVIDER_NOT_CONFIGURED", "异步渠道不存在或已停用", 503); }
} }
@@ -399,12 +432,88 @@ public class AsyncProviderClient {
String configured = environment.getProperty(name); String configured = environment.getProperty(name);
if (configured != null && !configured.isBlank()) for (String value : configured.split("\\|")) if (!value.isBlank()) keys.add(value.trim()); if (configured != null && !configured.isBlank()) for (String value : configured.split("\\|")) if (!value.isBlank()) keys.add(value.trim());
} }
private List<String> routeKeys(String providerCode, String providerId, String routeId, String upstreamModel) {
if (routeId != null && !routeId.isBlank()) {
String configured = environment.getProperty("MOYI_UPSTREAM_API_KEYS_ROUTE_" + normalize(routeId));
if (configured != null && !configured.isBlank()) return parseKeys(configured);
}
if (upstreamModel != null && !upstreamModel.isBlank()) {
String scopedName = "MOYI_UPSTREAM_API_KEYS_MODEL_" + normalize(providerCode) + "_" + normalize(upstreamModel);
String configured = environment.getProperty(scopedName);
if (configured != null && !configured.isBlank()) return parseKeys(configured);
}
return List.of();
}
private List<String> parseKeys(String configured) {
List<String> keys = new ArrayList<>();
for (String key : configured.split("\\|")) if (!key.isBlank()) keys.add(key.trim());
return keys.stream().distinct().toList();
}
private String normalize(String value) { return String.valueOf(value == null ? "" : value).toUpperCase(java.util.Locale.ROOT).replaceAll("[^A-Z0-9]", "_"); }
private Map<String,Object> withKeyFingerprint(Map<String,Object> response, String apiKey) {
Map<String,Object> copy = new LinkedHashMap<>();
if (response != null) copy.putAll(response);
copy.put("_moyiUpstreamKeyFingerprint", keyFingerprint(apiKey));
return copy;
}
private static String keyFingerprint(String apiKey) {
if (apiKey == null || apiKey.isBlank()) throw new ApiException("UPSTREAM_KEY_NOT_AVAILABLE", "异步任务未选择可轮询的上游凭据", 503);
try {
byte[] digest = MessageDigest.getInstance("SHA-256").digest(apiKey.getBytes(StandardCharsets.UTF_8));
StringBuilder result = new StringBuilder(digest.length * 2);
for (byte value : digest) result.append(String.format(java.util.Locale.ROOT, "%02x", value));
return result.toString();
} catch (NoSuchAlgorithmException exception) {
throw new IllegalStateException("SHA-256 is required by the Java runtime", exception);
}
}
private boolean isTripo(String code, String baseUrl) { private boolean isTripo(String code, String baseUrl) {
String normalizedCode = code == null ? "" : code.toLowerCase(java.util.Locale.ROOT); String normalizedCode = code == null ? "" : code.toLowerCase(java.util.Locale.ROOT);
String normalizedUrl = baseUrl == null ? "" : baseUrl.toLowerCase(java.util.Locale.ROOT); String normalizedUrl = baseUrl == null ? "" : baseUrl.toLowerCase(java.util.Locale.ROOT);
return normalizedCode.contains("tripo") || normalizedUrl.contains("tripo3d"); return normalizedCode.contains("tripo") || normalizedUrl.contains("tripo3d");
} }
private record Provider(String code, String protocol, String baseUrl, List<String> apiKeys, AtomicInteger cursor) { private record Provider(String code, String protocol, String baseUrl, List<String> apiKeys, AtomicInteger cursor) {
private String nextApiKey() { return apiKeys.get(Math.floorMod(cursor.getAndIncrement(), apiKeys.size())); } private String nextApiKey() {
if (apiKeys.isEmpty()) throw new ApiException("UPSTREAM_KEY_NOT_AVAILABLE", "异步渠道没有可用的上游凭据", 503);
return apiKeys.get(Math.floorMod(cursor.getAndIncrement(), apiKeys.size()));
}
private String nextApiKey(String routeId, String upstreamModel, Environment environment, ConcurrentMap<String, AtomicInteger> cursors) {
List<String> routeKeys = routeKeys(routeId, upstreamModel, environment);
if (routeKeys.isEmpty()) return nextApiKey();
AtomicInteger routeCursor = cursors.computeIfAbsent("route:" + (routeId == null ? upstreamModel : routeId), ignored -> new AtomicInteger());
return routeKeys.get(Math.floorMod(routeCursor.getAndIncrement(), routeKeys.size()));
}
private String keyForFingerprint(String routeId, String upstreamModel, Environment environment,
ConcurrentMap<String, AtomicInteger> cursors, String fingerprint) {
if (fingerprint == null || fingerprint.isBlank()) return nextApiKey(routeId, upstreamModel, environment, cursors);
List<String> candidates = new ArrayList<>(routeKeys(routeId, upstreamModel, environment));
candidates.addAll(apiKeys);
candidates = candidates.stream().distinct().toList();
for (String candidate : candidates) if (fingerprint.equals(keyFingerprint(candidate))) return candidate;
throw new ApiException("UPSTREAM_KEY_NOT_AVAILABLE", "创建任务时使用的上游凭据已从服务器配置中移除,无法安全轮询", 503);
}
private List<String> routeKeys(String routeId, String upstreamModel, Environment environment) {
if (environment == null) return List.of();
if (routeId != null && !routeId.isBlank()) {
String value = environment.getProperty("MOYI_UPSTREAM_API_KEYS_ROUTE_" + normalize(routeId));
if (value != null && !value.isBlank()) return parseKeys(value);
}
if (upstreamModel != null && !upstreamModel.isBlank()) {
String value = environment.getProperty("MOYI_UPSTREAM_API_KEYS_MODEL_" + normalize(code()) + "_" + normalize(upstreamModel));
if (value != null && !value.isBlank()) return parseKeys(value);
}
return List.of();
}
private static List<String> parseKeys(String value) {
List<String> keys = new ArrayList<>();
for (String key : value.split("\\|")) if (!key.isBlank()) keys.add(key.trim());
return keys.stream().distinct().toList();
}
private static String normalize(String value) { return String.valueOf(value == null ? "" : value).toUpperCase(java.util.Locale.ROOT).replaceAll("[^A-Z0-9]", "_"); }
} }
} }
@@ -36,6 +36,7 @@ public class AsyncTaskService {
private final int maxQueuedPerModel; private final int maxQueuedPerModel;
private final int defaultModelConcurrency; private final int defaultModelConcurrency;
private final int defaultKeyConcurrency; private final int defaultKeyConcurrency;
private final boolean modelConcurrencyEnabled;
private final long providerTimeoutMs; private final long providerTimeoutMs;
private final GatewayGuard gatewayGuard; private final GatewayGuard gatewayGuard;
private final UsageAuditService usage; private final UsageAuditService usage;
@@ -45,7 +46,18 @@ public class AsyncTaskService {
/** Compatibility constructor for isolated gateway tests that only exercise mock tasks. */ /** Compatibility constructor for isolated gateway tests that only exercise mock tasks. */
public AsyncTaskService(JdbcTemplate jdbc, ObjectMapper objectMapper, CatalogService catalogService, public AsyncTaskService(JdbcTemplate jdbc, ObjectMapper objectMapper, CatalogService catalogService,
int maxQueuedPerModel, GatewayGuard gatewayGuard, UsageAuditService usage) { int maxQueuedPerModel, GatewayGuard gatewayGuard, UsageAuditService usage) {
this(jdbc, objectMapper, catalogService, maxQueuedPerModel, 4, 4, 3_600_000, gatewayGuard, usage, null, null); this(jdbc, objectMapper, catalogService, maxQueuedPerModel, 4, 4, 3_600_000, true, gatewayGuard, usage, null, null);
}
/** Compatibility constructor for isolated tests using explicit limits. */
public AsyncTaskService(
JdbcTemplate jdbc, ObjectMapper objectMapper, CatalogService catalogService,
int maxQueuedPerModel, int defaultModelConcurrency, int defaultKeyConcurrency,
long providerTimeoutMs, GatewayGuard gatewayGuard, UsageAuditService usage,
AsyncProviderClient providerClient, PlatformTransactionManager transactionManager
) {
this(jdbc, objectMapper, catalogService, maxQueuedPerModel, defaultModelConcurrency,
defaultKeyConcurrency, providerTimeoutMs, true, gatewayGuard, usage, providerClient, transactionManager);
} }
@org.springframework.beans.factory.annotation.Autowired @org.springframework.beans.factory.annotation.Autowired
@@ -57,12 +69,16 @@ public class AsyncTaskService {
@Value("${moyi.gateway.default-model-concurrency:4}") int defaultModelConcurrency, @Value("${moyi.gateway.default-model-concurrency:4}") int defaultModelConcurrency,
@Value("${moyi.gateway.default-key-concurrency:4}") int defaultKeyConcurrency, @Value("${moyi.gateway.default-key-concurrency:4}") int defaultKeyConcurrency,
@Value("${moyi.tasks.provider-timeout-ms:3600000}") long providerTimeoutMs, @Value("${moyi.tasks.provider-timeout-ms:3600000}") long providerTimeoutMs,
@Value("${moyi.gateway.disable-model-concurrency:false}") boolean disableModelConcurrency,
GatewayGuard gatewayGuard, GatewayGuard gatewayGuard,
UsageAuditService usage, UsageAuditService usage,
AsyncProviderClient providerClient, AsyncProviderClient providerClient,
PlatformTransactionManager transactionManager PlatformTransactionManager transactionManager
) { ) {
if (maxQueuedPerModel < 1 || defaultModelConcurrency < 1 || defaultKeyConcurrency < 1) throw new IllegalArgumentException("Task queue and concurrency limits must be positive"); // A model concurrency of 0 means no MOYI-side model-group cap. The
// upstream provider still enforces its own quota, while API-Key
// concurrency remains independently configurable.
if (maxQueuedPerModel < 1 || defaultModelConcurrency < 0 || defaultKeyConcurrency < 0) throw new IllegalArgumentException("Task queue and concurrency limits must not be negative");
if (providerTimeoutMs < 1_000) throw new IllegalArgumentException("Provider task timeout must be at least one second"); if (providerTimeoutMs < 1_000) throw new IllegalArgumentException("Provider task timeout must be at least one second");
this.jdbc = jdbc; this.jdbc = jdbc;
this.objectMapper = objectMapper; this.objectMapper = objectMapper;
@@ -70,6 +86,7 @@ public class AsyncTaskService {
this.maxQueuedPerModel = maxQueuedPerModel; this.maxQueuedPerModel = maxQueuedPerModel;
this.defaultModelConcurrency = defaultModelConcurrency; this.defaultModelConcurrency = defaultModelConcurrency;
this.defaultKeyConcurrency = defaultKeyConcurrency; this.defaultKeyConcurrency = defaultKeyConcurrency;
this.modelConcurrencyEnabled = !disableModelConcurrency;
this.providerTimeoutMs = providerTimeoutMs; this.providerTimeoutMs = providerTimeoutMs;
this.gatewayGuard = gatewayGuard; this.gatewayGuard = gatewayGuard;
this.usage = usage; this.usage = usage;
@@ -163,7 +180,7 @@ public class AsyncTaskService {
"{\"format\":\"mp4\",\"url\":\"https://mock.moyi.ai/tasks/result.mp4\"}", now(), now() "{\"format\":\"mp4\",\"url\":\"https://mock.moyi.ai/tasks/result.mp4\"}", now(), now()
); );
taskIds.forEach(taskId -> usage.updateTask(taskId, "succeeded", Map.of("format", "result", "url", "https://mock.moyi.ai/tasks/" + taskId + "/result"), null, null)); taskIds.forEach(taskId -> usage.updateTask(taskId, "succeeded", Map.of("format", "result", "url", "https://mock.moyi.ai/tasks/" + taskId + "/result"), null, null));
List<Map<String,Object>> pending = jdbc.queryForList("SELECT t.id, t.provider_task_id, t.provider_deadline_at, t.created_at, m.provider_id FROM model_tasks t JOIN model_catalog m ON m.id=t.model_id WHERE t.status IN ('queued','running') AND t.provider_task_id <> 'mock' AND (t.updated_at < now() - INTERVAL '5 seconds' OR t.provider_deadline_at <= now()) ORDER BY t.updated_at LIMIT 50"); List<Map<String,Object>> pending = jdbc.queryForList("SELECT t.id, t.model_id, t.provider_task_id, t.provider_key_fingerprint, t.provider_deadline_at, t.created_at, m.provider_id, m.upstream_model FROM model_tasks t JOIN model_catalog m ON m.id=t.model_id WHERE t.status IN ('queued','running') AND t.provider_task_id <> 'mock' AND (t.updated_at < now() - INTERVAL '5 seconds' OR t.provider_deadline_at <= now()) ORDER BY t.updated_at LIMIT 50");
for (Map<String,Object> row : pending) pollProviderTask(row); for (Map<String,Object> row : pending) pollProviderTask(row);
dispatchQueuedTasks(); dispatchQueuedTasks();
} }
@@ -187,12 +204,8 @@ public class AsyncTaskService {
String taskId = String.valueOf(task.get("id")); String taskId = String.valueOf(task.get("id"));
usage.updateTask(taskId, "running", null, null, null); usage.updateTask(taskId, "running", null, null, null);
try { try {
Map<String,Object> upstream = providerClient.submit( Map<String,Object> input = taskInput(task);
String.valueOf(task.get("provider_id")), Map<String,Object> upstream = submitProviderTask(task, input);
String.valueOf(task.get("upstream_model")),
String.valueOf(task.get("task_type")),
taskInput(task)
);
// Some OpenAI-compatible image/audio endpoints return the // Some OpenAI-compatible image/audio endpoints return the
// generated media in the submit response rather than creating // generated media in the submit response rather than creating
// a pollable job. Persist that result and settle the task in // a pollable job. Persist that result and settle the task in
@@ -204,7 +217,7 @@ public class AsyncTaskService {
continue; continue;
} }
OffsetDateTime submittedAt = now(); OffsetDateTime submittedAt = now();
jdbc.update("UPDATE model_tasks SET provider_task_id=?, provider_deadline_at=?, updated_at=? WHERE id=? AND status='running'", providerTaskId(upstream), submittedAt.plus(java.time.Duration.ofMillis(providerTimeoutMs)), submittedAt, taskId); jdbc.update("UPDATE model_tasks SET provider_task_id=?, provider_key_fingerprint=?, provider_deadline_at=?, updated_at=? WHERE id=? AND status='running'", providerTaskId(upstream), upstream.get("_moyiUpstreamKeyFingerprint"), submittedAt.plus(java.time.Duration.ofMillis(providerTimeoutMs)), submittedAt, taskId);
} catch (RuntimeException exception) { } catch (RuntimeException exception) {
String code = exception instanceof ApiException error ? error.getCode() : "UPSTREAM_TASK_SUBMIT_FAILED"; String code = exception instanceof ApiException error ? error.getCode() : "UPSTREAM_TASK_SUBMIT_FAILED";
String message = exception instanceof ApiException error ? error.getMessage() : "上游异步任务创建失败"; String message = exception instanceof ApiException error ? error.getMessage() : "上游异步任务创建失败";
@@ -221,7 +234,7 @@ public class AsyncTaskService {
// workers reliably. // workers reliably.
jdbc.queryForList("SELECT pg_advisory_xact_lock(hashtextextended('moyi:async-dispatch', 0))"); jdbc.queryForList("SELECT pg_advisory_xact_lock(hashtextextended('moyi:async-dispatch', 0))");
String sql = """ String sql = """
SELECT t.id, t.api_key_id, m.provider_id, m.upstream_model, t.task_type, t.prompt, t.input SELECT t.id, t.api_key_id, t.model_id, m.provider_id, m.upstream_model, t.task_type, t.prompt, t.input
FROM model_tasks t FROM model_tasks t
JOIN model_catalog m ON m.id=t.model_id JOIN model_catalog m ON m.id=t.model_id
JOIN model_providers p ON p.id=m.provider_id JOIN model_providers p ON p.id=m.provider_id
@@ -229,17 +242,26 @@ public class AsyncTaskService {
AND t.provider_task_id IS NULL AND t.provider_task_id IS NULL
AND m.status='active' AND m.deleted_at IS NULL AND m.status='active' AND m.deleted_at IS NULL
AND p.status='active' AND p.deleted_at IS NULL AND p.status='active' AND p.deleted_at IS NULL
AND (SELECT COUNT(*) FROM model_tasks running_model AND (
WHERE running_model.model_id=t.model_id AND running_model.status='running') ? <= 0
< COALESCE(m.max_concurrency, ?) OR COALESCE(m.max_concurrency, ?) <= 0
AND (SELECT COUNT(*) FROM model_tasks running_key OR (SELECT COUNT(*) FROM model_tasks running_model
WHERE running_key.api_key_id=t.api_key_id AND running_key.status='running') WHERE running_model.model_id=t.model_id AND running_model.status='running')
< ? < COALESCE(m.max_concurrency, ?)
)
AND (
? <= 0
OR (SELECT COUNT(*) FROM model_tasks running_key
WHERE running_key.api_key_id=t.api_key_id AND running_key.status='running')
< ?
)
ORDER BY t.created_at ORDER BY t.created_at
LIMIT 1 LIMIT 1
FOR UPDATE OF t SKIP LOCKED FOR UPDATE OF t SKIP LOCKED
"""; """;
List<Map<String,Object>> rows = jdbc.queryForList(sql, defaultModelConcurrency, defaultKeyConcurrency); List<Map<String,Object>> rows = jdbc.queryForList(sql,
modelConcurrencyEnabled ? 1 : 0, defaultModelConcurrency, defaultModelConcurrency,
defaultKeyConcurrency, defaultKeyConcurrency);
if (rows.isEmpty()) return null; if (rows.isEmpty()) return null;
Map<String,Object> row = rows.getFirst(); Map<String,Object> row = rows.getFirst();
jdbc.update("UPDATE model_tasks SET status='running', updated_at=now() WHERE id=? AND status='queued'", row.get("id")); jdbc.update("UPDATE model_tasks SET status='running', updated_at=now() WHERE id=? AND status='queued'", row.get("id"));
@@ -320,6 +342,20 @@ public class AsyncTaskService {
return input; return input;
} }
private Map<String,Object> submitProviderTask(Map<String,Object> task, Map<String,Object> input) {
String providerId = String.valueOf(task.get("provider_id"));
String routeId = String.valueOf(task.get("model_id"));
String model = String.valueOf(task.get("upstream_model"));
String type = String.valueOf(task.get("task_type"));
// Keep the legacy overload for rows created before V30. Apart from
// compatibility with existing integrations, this makes a queued
// prompt-only task indistinguishable from the original submit path.
if (input.size() == 1 && input.containsKey("prompt")) {
return providerClient.submit(providerId, model, routeId, type, String.valueOf(input.get("prompt")));
}
return providerClient.submit(providerId, model, routeId, type, input);
}
private OffsetDateTime now() { return OffsetDateTime.now(ZoneOffset.UTC); } private OffsetDateTime now() { return OffsetDateTime.now(ZoneOffset.UTC); }
private String providerTaskId(Map<String,Object> response) { private String providerTaskId(Map<String,Object> response) {
@@ -345,8 +381,12 @@ public class AsyncTaskService {
} }
Map<String,Object> response; Map<String,Object> response;
try { try {
response = providerClient.poll(String.valueOf(row.get("provider_id")), String.valueOf(row.get("provider_task_id"))); response = providerClient.poll(String.valueOf(row.get("provider_id")), String.valueOf(row.get("model_id")), String.valueOf(row.get("upstream_model")), String.valueOf(row.get("provider_task_id")), row.get("provider_key_fingerprint") == null ? null : String.valueOf(row.get("provider_key_fingerprint")));
} catch (RuntimeException exception) { } catch (RuntimeException exception) {
if (exception instanceof ApiException error && "UPSTREAM_KEY_NOT_AVAILABLE".equals(error.getCode())) {
failTask(taskId, error.getCode(), error.getMessage());
return;
}
// Even a permanent authentication/configuration error only proves // Even a permanent authentication/configuration error only proves
// that polling failed, not that an already accepted job stopped. // that polling failed, not that an already accepted job stopped.
// Keep its hold and retry until the persisted deadline. // Keep its hold and retry until the persisted deadline.
@@ -129,6 +129,7 @@ public class UsageAuditService {
"LEFT JOIN usage_events ue ON ue.deleted_at IS NULL AND ue.created_at >= day AND ue.created_at < day + INTERVAL '1 day' " + "LEFT JOIN usage_events ue ON ue.deleted_at IS NULL AND ue.created_at >= day AND ue.created_at < day + INTERVAL '1 day' " +
"GROUP BY day ORDER BY day", "GROUP BY day ORDER BY day",
(rs, row) -> Map.of("date", rs.getString("day"), "count", rs.getLong("count")))); (rs, row) -> Map.of("date", rs.getString("day"), "count", rs.getLong("count"))));
boolean modelGuardEnabled = gatewayGuard == null || gatewayGuard.modelConcurrencyEnabled();
int defaultModelLimit = gatewayGuard == null ? 4 : gatewayGuard.defaultModelConcurrency(); int defaultModelLimit = gatewayGuard == null ? 4 : gatewayGuard.defaultModelConcurrency();
List<Map<String, Object>> groups = jdbc.query(""" List<Map<String, Object>> groups = jdbc.query("""
WITH pending AS ( WITH pending AS (
@@ -138,7 +139,8 @@ public class UsageAuditService {
FROM model_tasks WHERE status IN ('queued','running') GROUP BY model_id FROM model_tasks WHERE status IN ('queued','running') GROUP BY model_id
) )
SELECT m.id, m.public_name, m.source_label, p.name AS provider_name, m.call_mode, SELECT m.id, m.public_name, m.source_label, p.name AS provider_name, m.call_mode,
COALESCE(NULLIF(m.max_concurrency, 0), ?) AS max_concurrency, CASE WHEN m.max_concurrency = 0 THEN 0
ELSE COALESCE(m.max_concurrency, ?) END AS max_concurrency,
(m.status='active' AND m.deleted_at IS NULL AND p.status='active' AND p.deleted_at IS NULL (m.status='active' AND m.deleted_at IS NULL AND p.status='active' AND p.deleted_at IS NULL
AND d.status='active' AND d.deleted_at IS NULL) AS route_enabled, AND d.status='active' AND d.deleted_at IS NULL) AS route_enabled,
COALESCE(pending.running, 0) AS running, COALESCE(pending.queued, 0) AS queued COALESCE(pending.running, 0) AS running, COALESCE(pending.queued, 0) AS queued
@@ -155,7 +157,7 @@ public class UsageAuditService {
group.put("providerName", rs.getString("provider_name")); group.put("providerName", rs.getString("provider_name"));
group.put("mode", rs.getString("call_mode")); group.put("mode", rs.getString("call_mode"));
group.put("routeEnabled", rs.getBoolean("route_enabled")); group.put("routeEnabled", rs.getBoolean("route_enabled"));
group.put("limit", rs.getInt("max_concurrency")); group.put("limit", modelGuardEnabled ? rs.getInt("max_concurrency") : 0);
group.put("running", rs.getLong("running")); group.put("running", rs.getLong("running"));
group.put("queued", rs.getLong("queued")); group.put("queued", rs.getLong("queued"));
return group; return group;
@@ -179,6 +181,7 @@ public class UsageAuditService {
"queueWaitMs", gatewayGuard == null ? 30000 : gatewayGuard.queueWaitMs(), "queueWaitMs", gatewayGuard == null ? 30000 : gatewayGuard.queueWaitMs(),
"defaultModelConcurrency", defaultModelLimit, "defaultModelConcurrency", defaultModelLimit,
"defaultKeyConcurrency", gatewayGuard == null ? 4 : gatewayGuard.defaultKeyConcurrency(), "defaultKeyConcurrency", gatewayGuard == null ? 4 : gatewayGuard.defaultKeyConcurrency(),
"modelConcurrencyGuardEnabled", gatewayGuard == null || gatewayGuard.modelConcurrencyEnabled(),
"asyncQueueLimit", asyncQueueLimit, "asyncQueueLimit", asyncQueueLimit,
"asyncWorkerIntervalMs", asyncWorkerIntervalMs, "asyncWorkerIntervalMs", asyncWorkerIntervalMs,
"synchronousSnapshotAvailable", snapshot.available() "synchronousSnapshotAvailable", snapshot.available()
@@ -43,8 +43,15 @@ moyi:
redis-prefix: ${MOYI_REDIS_KEY_PREFIX:moyi:} redis-prefix: ${MOYI_REDIS_KEY_PREFIX:moyi:}
requests-per-minute: ${GATEWAY_REQUESTS_PER_MINUTE:60} requests-per-minute: ${GATEWAY_REQUESTS_PER_MINUTE:60}
reads-per-minute: ${GATEWAY_READS_PER_MINUTE:300} reads-per-minute: ${GATEWAY_READS_PER_MINUTE:300}
default-model-concurrency: ${GATEWAY_DEFAULT_MODEL_CONCURRENCY:4} # 0 disables the MOYI-side model-group cap so channel concurrency can be
# measured directly. Set a positive value only when a provider confirms
# a shared per-model limit.
default-model-concurrency: ${GATEWAY_DEFAULT_MODEL_CONCURRENCY:0}
default-key-concurrency: ${GATEWAY_DEFAULT_KEY_CONCURRENCY:4} default-key-concurrency: ${GATEWAY_DEFAULT_KEY_CONCURRENCY:4}
# Temporary measurement switch. When true, skip MOYI model/group slots
# while retaining the independent API-Key limit, RPM, billing and the
# provider's own response/limit.
disable-model-concurrency: ${GATEWAY_DISABLE_MODEL_CONCURRENCY:false}
queue-wait-ms: ${GATEWAY_QUEUE_WAIT_MS:30000} queue-wait-ms: ${GATEWAY_QUEUE_WAIT_MS:30000}
queue-poll-ms: ${GATEWAY_QUEUE_POLL_MS:100} queue-poll-ms: ${GATEWAY_QUEUE_POLL_MS:100}
tasks: tasks:
@@ -0,0 +1,15 @@
-- Tripo H-series routes accept either a text prompt or an image URL/file token.
-- Keep the catalog metadata aligned with the async adapter so clients can
-- expose the image input without guessing from the model type.
UPDATE model_definitions
SET input_types = '["text", "image"]'::jsonb,
updated_at = now()
WHERE model_key IN ('tripo-v3', 'tripo-h-v3-0', 'tripo-h-v2-5')
AND deleted_at IS NULL;
UPDATE model_catalog
SET input_types = '["text", "image"]'::jsonb,
updated_at = now()
WHERE provider_id = 'provider-vast'
AND upstream_model IN ('v3.1-20260211', 'v3.0-20250812', 'v2.5-20250123')
AND deleted_at IS NULL;
@@ -0,0 +1,14 @@
-- The original relay catalog seeds used max_concurrency = 1 as a temporary
-- safety value. That value is a MOYI-side admission limit; it is not a
-- channel/provider quota and made every group look serial even when the
-- upstream account could process more requests.
--
-- 0 means "no MOYI model-group cap". The gateway still enforces each
-- platform API-Key limit, request-rate limit, wallet/usage accounting, and
-- the provider's own quota/rate limit. Administrators can still set an
-- explicit positive cap for a route when a channel reports one.
UPDATE model_catalog
SET max_concurrency = 0,
updated_at = now()
WHERE deleted_at IS NULL
AND max_concurrency = 1;
@@ -0,0 +1,5 @@
ALTER TABLE model_tasks
ADD COLUMN IF NOT EXISTS provider_key_fingerprint varchar(64);
COMMENT ON COLUMN model_tasks.provider_key_fingerprint IS
'SHA-256 fingerprint of the upstream credential selected for this async task; never stores the credential itself';
@@ -0,0 +1,19 @@
package com.moyi.controlplane.catalog;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.Test;
class CatalogServiceTest {
@Test
void asyncReadinessMatchesImplementedAdapters() {
assertTrue(CatalogService.asyncExecutorReady("provider-vast", "http", "3d"));
assertTrue(CatalogService.asyncExecutorReady("provider-bytedance", "http", "video"));
assertTrue(CatalogService.asyncExecutorReady("relay-provider", "openai-compatible", "image"));
assertTrue(CatalogService.asyncExecutorReady("relay-provider", "OPENAI-COMPATIBLE", "video"));
assertTrue(CatalogService.asyncExecutorReady("relay-provider", "openai-compatible", "audio"));
assertFalse(CatalogService.asyncExecutorReady("relay-provider", "openai-compatible", "3d"));
assertFalse(CatalogService.asyncExecutorReady("relay-provider", "http", "video"));
}
}
@@ -66,6 +66,32 @@ class GatewayDeploymentTest {
verify(redis, atLeastOnce()).execute(any(RedisScript.class), eq(List.of("moyi:test:gateway:slots:key:" + keyId)), any(Object[].class)); verify(redis, atLeastOnce()).execute(any(RedisScript.class), eq(List.of("moyi:test:gateway:slots:key:" + keyId)), any(Object[].class));
} }
@Test
void zeroModelConcurrencyUsesOnlyTheIndependentKeyLease() {
StringRedisTemplate redis = mock(StringRedisTemplate.class);
doReturn(1L).when(redis).execute(any(RedisScript.class), anyList(), any(Object[].class));
UUID keyId = UUID.randomUUID();
Map<String, Object> unlimited = Map.of("id", "model-unlimited", "maxConcurrency", 0);
try (GatewayGuard.Admission ignored = new GatewayGuard(redis, 60, 300, 4, 2, 15, 10, "moyi:test:").acquire(unlimited, keyId, 15)) {
// The route is deliberately unlimited at the MOYI model layer.
}
verify(redis, never()).execute(any(RedisScript.class), eq(List.of("moyi:test:gateway:slots:model:model-unlimited")), any(Object[].class));
verify(redis, atLeastOnce()).execute(any(RedisScript.class), eq(List.of("moyi:test:gateway:slots:key:" + keyId)), any(Object[].class));
}
@Test
void disabledModelGuardDoesNotCreateModelLease() {
StringRedisTemplate redis = mock(StringRedisTemplate.class);
doReturn(1L).when(redis).execute(any(RedisScript.class), anyList(), any(Object[].class));
UUID keyId = UUID.randomUUID();
Map<String, Object> model = Map.of("id", "model-measurement", "maxConcurrency", 1);
try (GatewayGuard.Admission ignored = new GatewayGuard(redis, 60, 300, 4, 2, 15, 10, "moyi:test:", true).acquire(model, keyId, 15)) {
// Measurement mode bypasses only the MOYI model-group lease.
}
verify(redis, never()).execute(any(RedisScript.class), eq(List.of("moyi:test:gateway:slots:model:model-measurement")), any(Object[].class));
verify(redis, atLeastOnce()).execute(any(RedisScript.class), eq(List.of("moyi:test:gateway:slots:key:" + keyId)), any(Object[].class));
}
@Test @Test
void modelsExposeReadableIdAndKeepRouteIdForDiagnostics() { void modelsExposeReadableIdAndKeepRouteIdForDiagnostics() {
CatalogService catalog = mock(CatalogService.class); CatalogService catalog = mock(CatalogService.class);
@@ -201,7 +227,7 @@ class GatewayDeploymentTest {
ApiException error = assertThrows(ApiException.class, () -> controller.chatCompletions("Bearer test-key", Map.of("model", "model-real"),null,new MockHttpServletResponse())); ApiException error = assertThrows(ApiException.class, () -> controller.chatCompletions("Bearer test-key", Map.of("model", "model-real"),null,new MockHttpServletResponse()));
assertEquals("PROVIDER_NOT_CONFIGURED", error.getCode()); assertEquals("PROVIDER_NOT_CONFIGURED", error.getCode());
assertEquals(503, error.getStatus()); assertEquals(503, error.getStatus());
verify(provider, never()).chat(anyString(), anyString(), anyMap()); verify(provider, never()).chat(anyString(), anyString(), anyString(), anyMap());
} }
@Test @Test
@@ -230,13 +256,13 @@ class GatewayDeploymentTest {
when(catalog.authenticateKey("test-key")).thenReturn(key); when(catalog.authenticateKey("test-key")).thenReturn(key);
when(catalog.activeModel("reasoning-route")).thenReturn(model); when(catalog.activeModel("reasoning-route")).thenReturn(model);
when(catalog.keyCanInvoke(key, "reasoning-route")).thenReturn(true); when(catalog.keyCanInvoke(key, "reasoning-route")).thenReturn(true);
when(provider.isConfigured("provider-relay")).thenReturn(true); when(provider.isConfigured("provider-relay", "reasoning-route", "reasoning-model")).thenReturn(true);
Map<String, Object> upstreamResponse = new java.util.LinkedHashMap<>(Map.of( Map<String, Object> upstreamResponse = new java.util.LinkedHashMap<>(Map.of(
"id", "upstream-response", "id", "upstream-response",
"choices", List.of(Map.of("index", 0, "message", Map.of("role", "assistant", "content", "", "reasoning_content", "private reasoning"), "finish_reason", "length")), "choices", List.of(Map.of("index", 0, "message", Map.of("role", "assistant", "content", "", "reasoning_content", "private reasoning"), "finish_reason", "length")),
"usage", Map.of("prompt_tokens", 2, "completion_tokens", 16, "total_tokens", 18) "usage", Map.of("prompt_tokens", 2, "completion_tokens", 16, "total_tokens", 18)
)); ));
when(provider.chat(eq("provider-relay"), eq("reasoning-model"), anyMap())).thenReturn(upstreamResponse); when(provider.chat(eq("provider-relay"), eq("reasoning-model"), eq("reasoning-route"), anyMap())).thenReturn(upstreamResponse);
when(billing.reserve(anyMap(), anyMap(), any(), isNull())).thenReturn(new ChatBillingService.Ticket(UUID.randomUUID(), UUID.fromString(userId), ChatMeter.rates(null, true))); when(billing.reserve(anyMap(), anyMap(), any(), isNull())).thenReturn(new ChatBillingService.Ticket(UUID.randomUUID(), UUID.fromString(userId), ChatMeter.rates(null, true)));
when(billing.settle(any(), anyMap(), anyInt(), isNull())).thenReturn(java.math.BigDecimal.ZERO); when(billing.settle(any(), anyMap(), anyInt(), isNull())).thenReturn(java.math.BigDecimal.ZERO);
@@ -262,12 +288,12 @@ class GatewayDeploymentTest {
when(catalog.authenticateKey("test-key")).thenReturn(key); when(catalog.authenticateKey("test-key")).thenReturn(key);
when(catalog.activeModel("empty-route")).thenReturn(model); when(catalog.activeModel("empty-route")).thenReturn(model);
when(catalog.keyCanInvoke(key, "empty-route")).thenReturn(true); when(catalog.keyCanInvoke(key, "empty-route")).thenReturn(true);
when(provider.isConfigured("provider-relay")).thenReturn(true); when(provider.isConfigured("provider-relay", "empty-route", "empty-model")).thenReturn(true);
Map<String, Object> upstreamResponse = new java.util.LinkedHashMap<>(Map.of( Map<String, Object> upstreamResponse = new java.util.LinkedHashMap<>(Map.of(
"id", "upstream-empty", "id", "upstream-empty",
"choices", List.of(Map.of("index", 0, "message", Map.of("role", "assistant", "content", ""), "finish_reason", "stop")) "choices", List.of(Map.of("index", 0, "message", Map.of("role", "assistant", "content", ""), "finish_reason", "stop"))
)); ));
when(provider.chat(eq("provider-relay"), eq("empty-model"), anyMap())).thenReturn(upstreamResponse); when(provider.chat(eq("provider-relay"), eq("empty-model"), eq("empty-route"), anyMap())).thenReturn(upstreamResponse);
when(billing.reserve(anyMap(), anyMap(), any(), isNull())).thenReturn(new ChatBillingService.Ticket(UUID.randomUUID(), UUID.fromString(userId), ChatMeter.rates(null, true))); when(billing.reserve(anyMap(), anyMap(), any(), isNull())).thenReturn(new ChatBillingService.Ticket(UUID.randomUUID(), UUID.fromString(userId), ChatMeter.rates(null, true)));
GatewayController controller = new GatewayController(catalog, provider, guard, billing, new ObjectMapper()); GatewayController controller = new GatewayController(catalog, provider, guard, billing, new ObjectMapper());
@@ -28,11 +28,12 @@ class OpenAiCompatibleProviderHttpTest {
private final ObjectMapper json = new ObjectMapper(); private final ObjectMapper json = new ObjectMapper();
private HttpServer server; private HttpServer server;
private OpenAiCompatibleProvider provider; private OpenAiCompatibleProvider provider;
private MockEnvironment environment;
@BeforeEach @BeforeEach
void setUp() throws Exception { void setUp() throws Exception {
server = HttpServer.create(new InetSocketAddress("127.0.0.1", 0), 0); server = HttpServer.create(new InetSocketAddress("127.0.0.1", 0), 0);
var environment = new MockEnvironment() environment = new MockEnvironment()
.withProperty("moyi.providers.deepseek.api-key", "local-fake-key") .withProperty("moyi.providers.deepseek.api-key", "local-fake-key")
.withProperty("moyi.providers.deepseek.base-url", "http://127.0.0.1:" + server.getAddress().getPort() + "/v1") .withProperty("moyi.providers.deepseek.base-url", "http://127.0.0.1:" + server.getAddress().getPort() + "/v1")
.withProperty("moyi.gateway.upstream-timeout-seconds", "5"); .withProperty("moyi.gateway.upstream-timeout-seconds", "5");
@@ -93,6 +94,24 @@ class OpenAiCompatibleProviderHttpTest {
assertEquals(512, input.get("max_tokens")); assertEquals(512, input.get("max_tokens"));
} }
@Test
void prefersRouteKeyThenModelKeyBeforeProviderKey() throws Exception {
var authorization = new ArrayList<String>();
server.createContext("/v1/chat/completions", exchange -> {
authorization.add(exchange.getRequestHeaders().getFirst("Authorization"));
write(exchange, 200, "{\"id\":\"ok\",\"choices\":[{\"message\":{\"role\":\"assistant\",\"content\":\"OK\"}}],\"usage\":{\"prompt_tokens\":1,\"completion_tokens\":1,\"total_tokens\":2}}");
});
server.start();
environment.withProperty("MOYI_UPSTREAM_API_KEYS_ROUTE_ROUTE_A", "route-key")
.withProperty("MOYI_UPSTREAM_API_KEYS_MODEL_PROVIDER_DEEPSEEK_GPT_5", "model-key")
.withProperty("MOYI_UPSTREAM_API_KEYS_MODEL_GPT_5", "wrong-global-key");
provider.chat("provider-deepseek", "gpt-5", "route-a", textInput(128));
provider.chat("provider-deepseek", "gpt-5", "route-b", textInput(128));
assertEquals(List.of("Bearer route-key", "Bearer model-key"), authorization);
}
private Map<String, Object> textInput(int maxTokens) { private Map<String, Object> textInput(int maxTokens) {
var input = new LinkedHashMap<String, Object>(); var input = new LinkedHashMap<String, Object>();
input.put("messages", List.of(Map.of("role", "user", "content", "Reply with OK."))); input.put("messages", List.of(Map.of("role", "user", "content", "Reply with OK.")));
@@ -14,11 +14,13 @@ import com.moyi.controlplane.security.SecretCipher;
import com.sun.net.httpserver.HttpServer; import com.sun.net.httpserver.HttpServer;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.concurrent.atomic.AtomicReference;
import java.util.Map; import java.util.Map;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource; import org.junit.jupiter.params.provider.CsvSource;
import org.springframework.core.env.Environment; import org.springframework.core.env.Environment;
import org.springframework.mock.env.MockEnvironment;
import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.JdbcTemplate;
class AsyncProviderClientTest { class AsyncProviderClientTest {
@@ -139,6 +141,48 @@ class AsyncProviderClientTest {
} }
} }
@Test
void asyncPollingReusesTheSubmittedKeyFingerprint() throws Exception {
HttpServer server = HttpServer.create(new InetSocketAddress("127.0.0.1", 0), 0);
AtomicReference<String> submitAuthorization = new AtomicReference<>();
AtomicReference<String> pollAuthorization = new AtomicReference<>();
server.createContext("/videos", exchange -> {
String authorization = exchange.getRequestHeaders().getFirst("Authorization");
if (exchange.getRequestURI().getPath().equals("/videos")) {
submitAuthorization.set(authorization);
exchange.getRequestBody().readAllBytes();
byte[] response = "{\"id\":\"video-1\",\"status\":\"queued\"}".getBytes(StandardCharsets.UTF_8);
exchange.sendResponseHeaders(200, response.length);
try (var output = exchange.getResponseBody()) { output.write(response); }
} else {
pollAuthorization.set(authorization);
byte[] response = "{\"id\":\"video-1\",\"status\":\"succeeded\"}".getBytes(StandardCharsets.UTF_8);
exchange.sendResponseHeaders(200, response.length);
try (var output = exchange.getResponseBody()) { output.write(response); }
}
});
server.start();
try {
JdbcTemplate jdbc = mock(JdbcTemplate.class);
SecretCipher cipher = new SecretCipher("test-encryption");
when(jdbc.queryForMap(any(String.class), eq("provider-video"))).thenReturn(Map.of(
"code", "relay-video", "protocol", "openai-compatible", "base_url", "http://127.0.0.1:" + server.getAddress().getPort(), "credential_ciphertext", ""
));
MockEnvironment environment = new MockEnvironment()
.withProperty("MOYI_UPSTREAM_API_KEYS_ROUTE_ROUTE_A", "key-a|key-b")
.withProperty("MOYI_UPSTREAM_API_KEYS_MODEL_RELAY_VIDEO_SORA_2", "wrong-model-key");
AsyncProviderClient client = new AsyncProviderClient(jdbc, cipher, new ObjectMapper(), environment);
Map<String,Object> submitted = client.submit("provider-video", "sora-2", "route-a", "video", Map.of("prompt", "a cat"));
String fingerprint = String.valueOf(submitted.get("_moyiUpstreamKeyFingerprint"));
assertTrue(fingerprint.matches("[0-9a-f]{64}"));
client.poll("provider-video", "route-a", "sora-2", "video-1", fingerprint);
assertEquals(submitAuthorization.get(), pollAuthorization.get());
} finally {
server.stop(0);
}
}
@ParameterizedTest @ParameterizedTest
@CsvSource({"401,UPSTREAM_AUTH_FAILED", "403,UPSTREAM_AUTH_FAILED", "402,UPSTREAM_CREDIT_INSUFFICIENT", "429,UPSTREAM_RATE_LIMITED", "503,UPSTREAM_PROVIDER_ERROR"}) @CsvSource({"401,UPSTREAM_AUTH_FAILED", "403,UPSTREAM_AUTH_FAILED", "402,UPSTREAM_CREDIT_INSUFFICIENT", "429,UPSTREAM_RATE_LIMITED", "503,UPSTREAM_PROVIDER_ERROR"})
void httpErrorsExposeOnlySafeCodes(int status, String code) throws Exception { void httpErrorsExposeOnlySafeCodes(int status, String code) throws Exception {
+1 -1
View File
@@ -77,7 +77,7 @@ Invoke-WebRequest http://localhost:8080/health
本节点完成的是“控制面基础闭环 + 同步供应商转发 + Redis 网关保护 + 持久化任务队列”,不是生产网关完成版: 本节点完成的是“控制面基础闭环 + 同步供应商转发 + Redis 网关保护 + 持久化任务队列”,不是生产网关完成版:
- 数据已写入 PostgreSQL;Redis 已用于 API Key 频率限制、API Key 并发租约、上游分组并发租约和 Redis Lua 原子操作。默认同步调用每 Key 每分钟 60 次(`GATEWAY_REQUESTS_PER_MINUTE`)、每 Key 同时 4 个请求(`GATEWAY_DEFAULT_KEY_CONCURRENCY`);分组并发由管理端模型分组的 `maxConcurrency` 配置,未配置时使用 `GATEWAY_DEFAULT_MODEL_CONCURRENCY`。Redis 不可用时网关返回 503,不会绕过保护直接请求上游; - 数据已写入 PostgreSQL;Redis 已用于 API Key 频率限制、API Key 并发租约、上游分组并发租约和 Redis Lua 原子操作。默认同步调用每 Key 每分钟 60 次(`GATEWAY_REQUESTS_PER_MINUTE`)、每 Key 同时 4 个请求(`GATEWAY_DEFAULT_KEY_CONCURRENCY`);分组并发由管理端模型分组的 `maxConcurrency` 配置,填 0 表示不设置 MOYI 分组上限,未配置时使用 `GATEWAY_DEFAULT_MODEL_CONCURRENCY`。Redis 不可用时网关返回 503,不会绕过保护直接请求上游;
- `/v1/chat/completions` 支持 OpenAI 兼容同步/流式文本聊天;真实供应商未配置 Key 时返回 503,只有 `provider-mock` 可以返回 Mock。V13 起按人民币分组售价预留并结算,详见 [流式与计费](16-streaming-chat-billing.md); - `/v1/chat/completions` 支持 OpenAI 兼容同步/流式文本聊天;真实供应商未配置 Key 时返回 503,只有 `provider-mock` 可以返回 Mock。V13 起按人民币分组售价预留并结算,详见 [流式与计费](16-streaming-chat-billing.md);
- `/v1/3d/tasks` 和 `/v1/video/tasks` 已提供数据库持久化 Mock worker;真实供应商执行器尚未替换,返回 501 而非伪造任务成功; - `/v1/3d/tasks` 和 `/v1/video/tasks` 已提供数据库持久化 Mock worker;真实供应商执行器尚未替换,返回 501 而非伪造任务成功;
- Seedance、Tripo/VAST 的异步任务和 OSS 结果存储尚未迁移到 Java; - Seedance、Tripo/VAST 的异步任务和 OSS 结果存储尚未迁移到 Java;
+19 -1
View File
@@ -192,10 +192,28 @@ MOYI 用户对外使用的统一 API Base URL 为 `https://moyi.soulofmodel.com/
## 一个渠道配置多个上游 Key ## 一个渠道配置多个上游 Key
可以。当前 OpenAI 兼容转发器支持为同一渠道配置多个上游 Key,并按请求轮换,适合把上游的单 Key 并发拆到多个 Key。服务器环境变量优先使用渠道编码(管理端渠道的 code),也兼容渠道 ID 转成大写下划线后的名字,Key 之间用 `|` 分隔。例如渠道编码为 `apipro` 时: 可以。当前 OpenAI 兼容转发器支持为同一渠道配置多个上游 Key,并按请求轮换,适合把上游的单 Key 并发拆到多个 Key。新版还支持“按 MOYI 模型分组固定 Key 池”:路由分组 Key 优先,其次是带渠道编码的上游模型 Key,再其次是渠道级 Key,最后才回退到管理端渠道里的单个加密凭据。模型 Key 必须带渠道编码,避免 APIPro、ApiHub、TokenFoundryX 的同名模型误用彼此的凭据。
服务器环境变量优先使用渠道编码(管理端渠道的 code),也兼容渠道 ID 转成大写下划线后的名字,Key 之间用 `|` 分隔。例如渠道编码为 `apipro` 时:
```ini ```ini
MOYI_UPSTREAM_API_KEYS_APIPRO='key_a|key_b|key_c' MOYI_UPSTREAM_API_KEYS_APIPRO='key_a|key_b|key_c'
``` ```
按分组配置时,使用管理端模型路由的内部 ID(不是 `/v1/models` 中可读的公开模型 ID),然后标准化:转大写,把非字母数字替换为下划线。例如路由 ID `relay-route-044e37bb-ab61-4c6d-88a8-13adda55e4c0b` 对应:
```ini
MOYI_UPSTREAM_API_KEYS_ROUTE_RELAY_ROUTE_044E37BB_AB61_4C6D_88A8_13ADDA55E4C0B='group_key'
```
如果希望同一个渠道中的上游模型的多个分组共用一个 Key 池,可以使用带渠道编码的变量。渠道编码为 `apipro`、上游模型为 `glm-4.7` 时:
```ini
MOYI_UPSTREAM_API_KEYS_MODEL_APIPRO_GLM_4_7='model_key_a|model_key_b'
```
不再使用没有渠道编码的 `MOYI_UPSTREAM_API_KEYS_MODEL_GLM_4_7` 形式;它会被忽略,防止同名模型跨供应商串用 Key。
动态变量由 `docker-compose.cloud.yml` 的 API 服务 `env_file: .env.cloud` 注入容器;真实 Key 只能写服务器上的 `.env.cloud`,不能写 Git、迁移文件或聊天记录。每个分组创建一把 Key 时只填单个值。同步请求支持用竖线分隔多把 Key 轮换;异步任务建议每个分组只配置一把 Key,因为部分供应商要求用创建任务的同一凭据轮询任务状态。调用和异步创建/轮询都使用同一优先级规则。
保留管理端渠道中的单个凭据作为回退 Key。同步聊天和已接入的异步执行器都会在每个请求或轮询请求前轮换 Key。轮换只解决 Key 分摊,不能突破上游账户总并发、余额、模型授权或速率限制;平台自己的模型并发和单个 MOYI Key 并发仍由网关队列控制。多个 Key 应来自同一渠道、具有相同模型授权,并在服务器密钥文件中保存,不能提交到 Git。当前没有把这些 Key 明文放进管理端数据库的功能,避免把上游密钥暴露给前端;需要新增或撤销 Key 时修改服务器 `.env.cloud` 后重启 API 容器即可。 保留管理端渠道中的单个凭据作为回退 Key。同步聊天和已接入的异步执行器都会在每个请求或轮询请求前轮换 Key。轮换只解决 Key 分摊,不能突破上游账户总并发、余额、模型授权或速率限制;平台自己的模型并发和单个 MOYI Key 并发仍由网关队列控制。多个 Key 应来自同一渠道、具有相同模型授权,并在服务器密钥文件中保存,不能提交到 Git。当前没有把这些 Key 明文放进管理端数据库的功能,避免把上游密钥暴露给前端;需要新增或撤销 Key 时修改服务器 `.env.cloud` 后重启 API 容器即可。
+12 -2
View File
@@ -13,11 +13,21 @@ API Key 权限窗口支持同时勾选多个模型,再选择各自的分组;
一次同步模型调用要同时通过两层并发保护: 一次同步模型调用要同时通过两层并发保护:
1. **平台 API Key 并发**:限制同一个用户 Key 同时进行中的请求数量。默认值为 4,可在服务端环境变量 `GATEWAY_DEFAULT_KEY_CONCURRENCY` 调整;每个 Key 独立计数,不会因为其他用户占满而互相阻塞。 1. **平台 API Key 并发**:限制同一个用户 Key 同时进行中的请求数量。默认值为 4,可在服务端环境变量 `GATEWAY_DEFAULT_KEY_CONCURRENCY` 调整;每个 Key 独立计数,不会因为其他用户占满而互相阻塞。
2. **上游分组并发**:限制同一个渠道分组向中转站发出的同时请求数量,供使用该分组的多个用户共享。管理员在模型与分组中填写“上游分组并发上限”;1 表示串行,4 表示最多同时处理 4 个请求。 2. **上游分组并发**:可限制同一个渠道分组向中转站发出的同时请求数量,供使用该分组的多个用户共享。管理员在模型与分组中填写“MOYI 分组并发上限”;填 0 表示不设置 MOYI 分组上限,直接观察渠道方的真实限制;只有渠道明确给出共享上限时才填写正整数,1 才表示串行。
> **注意:这个数字来自 MOYI,不是渠道商返回值。** 早期目录迁移为了防止未经验证的路由压垮上游,把新建分组的 `max_concurrency` 初始写成了 `1`;因此管理端看到的 `1` 不能证明 ApiHub、APIPro、TokenFoundryX 或 Tripo 只允许一个并发。渠道商的账户并发、RPM、TPM 和跨模型共享额度必须通过渠道文档或实测确认。若要做渠道容量压测,先为目标分组临时设置足够大的 MOYI 上限,并同步提高测试 API Key 的并发和 RPM,避免平台自身的保护层先返回 `MODEL_BUSY`、`KEY_BUSY` 或 `RATE_LIMITED`。
两层限制会同时生效。同步/流式请求先尝试原子取得“模型分组 + 平台 Key”两个并发槽;若任一层满额,则在平台内进行有界等待,默认最多 30 秒(`GATEWAY_QUEUE_WAIT_MS`),每 100 毫秒重试一次(`GATEWAY_QUEUE_POLL_MS`)。等待时不会占着另一层资源;获得两个槽后才向上游发起请求,超时才返回 429 和 `Retry-After`。API Key 的每分钟请求频率和并发数是不同设置;异步任务另受任务队列上限控制。 两层限制会同时生效。同步/流式请求先尝试原子取得“模型分组 + 平台 Key”两个并发槽;若任一层满额,则在平台内进行有界等待,默认最多 30 秒(`GATEWAY_QUEUE_WAIT_MS`),每 100 毫秒重试一次(`GATEWAY_QUEUE_POLL_MS`)。等待时不会占着另一层资源;获得两个槽后才向上游发起请求,超时才返回 429 和 `Retry-After`。API Key 的每分钟请求频率和并发数是不同设置;异步任务另受任务队列上限控制。
这里的数值是 MOYI 配置的保护上限,不是性能压测上限,也不是中转站承诺的账号额度。上游账号可能还有跨模型共享的并发/RPM/TPM限制;需要用有效上游密钥做阶梯压测或由渠道方确认后,再提高 MOYI 分组上限。 这里的正整数是 MOYI 配置的保护上限,不是性能压测上限,也不是中转站承诺的账号额度。默认值为 0(不限制模型分组);上游账号仍可能有跨模型共享的并发/RPM/TPM 限制,达到时会返回上游限流错误。API Key 并发和请求频率仍然生效,方便先测渠道能力,再按渠道方确认的额度设置保护值。
### 渠道最大并发的实测方法
1. 选定一个已授权、已计价且能稳定返回内容的模型分组,记录当前 `maxConcurrency`、测试 Key 的 `GATEWAY_DEFAULT_KEY_CONCURRENCY` 和 `GATEWAY_REQUESTS_PER_MINUTE`。
2. 暂时将该分组的 MOYI 并发上限设为高于预计阶梯的值(例如 100),并将测试 Key 的并发/RPM 设为高于测试阶梯;每次变更后重新读取管理端模型详情确认保存成功。
3. 用 `scripts/live-gateway-smoke.py --run-calls --max-concurrency N` 或等效压测脚本按 1、2、4、8、16…逐级发起短请求。每个阶梯应使用独立测试 Key,或在阶梯之间等待 RPM 窗口清空;否则累计请求会把平台自身的 RPM 限制误判为渠道限制。
4. 对每个阶梯记录成功数、HTTP 状态、错误码、首字节/总耗时,并区分 `MODEL_BUSY`/`KEY_BUSY`/`RATE_LIMITED`(MOYI 层)与上游返回的 429、限流或余额错误。第一个稳定出现的上游 429/限流阶梯才是该 Key/模型在当前账户和时段的观测上限。
5. 压测结束后恢复面向客户的并发、RPM 和 Key 限制。报告必须同时列出“观测上限”和“当前生产保护上限”,不能把任一数字写成渠道商的正式承诺。
## 用户操作指南 ## 用户操作指南
+15 -1
View File
@@ -42,8 +42,13 @@ SESSION_TTL_HOURS=168
JAVA_OPTS='-Xms256m -Xmx1536m -XX:+ExitOnOutOfMemoryError' JAVA_OPTS='-Xms256m -Xmx1536m -XX:+ExitOnOutOfMemoryError'
GATEWAY_REQUESTS_PER_MINUTE=60 GATEWAY_REQUESTS_PER_MINUTE=60
GATEWAY_READS_PER_MINUTE=300 GATEWAY_READS_PER_MINUTE=300
GATEWAY_DEFAULT_MODEL_CONCURRENCY=4 # 0 disables an artificial MOYI-side model/group cap; the upstream provider
# still controls its own RPS/RPM/TPM/concurrency limits.
GATEWAY_DEFAULT_MODEL_CONCURRENCY=0
GATEWAY_DEFAULT_KEY_CONCURRENCY=4 GATEWAY_DEFAULT_KEY_CONCURRENCY=4
GATEWAY_DISABLE_MODEL_CONCURRENCY=false
GATEWAY_QUEUE_WAIT_MS=30000
GATEWAY_QUEUE_POLL_MS=100
TASK_MAX_QUEUED_PER_MODEL=100 TASK_MAX_QUEUED_PER_MODEL=100
MOYI_TASK_WORKER_INTERVAL_MS=1000 MOYI_TASK_WORKER_INTERVAL_MS=1000
# Accepted async tasks retry polling for up to this duration, then release # Accepted async tasks retry polling for up to this duration, then release
@@ -85,3 +90,12 @@ OSS_ENDPOINT=oss-cn-beijing-internal.aliyuncs.com
OSS_BUCKET= OSS_BUCKET=
OSS_ACCESS_KEY_ID= OSS_ACCESS_KEY_ID=
OSS_ACCESS_KEY_SECRET= OSS_ACCESS_KEY_SECRET=
# Optional per-route upstream key pools. Do not commit real values. Replace
# ROUTE_ID with the normalized MOYI route/model id (uppercase, non-alphanumerics
# replaced by underscores), and separate multiple keys with '|'. Route pools
# take precedence over provider-scoped model pools, then channel-level pools,
# then the single encrypted credential stored in the admin channel record.
# MOYI_UPSTREAM_API_KEYS_ROUTE_<NORMALIZED_ROUTE_ID>='upstream_key'
# MOYI_UPSTREAM_API_KEYS_MODEL_<NORMALIZED_PROVIDER_CODE>_<NORMALIZED_UPSTREAM_MODEL>='upstream_key'
# MOYI_UPSTREAM_API_KEYS_APIPRO='key_for_channel_fallback_1|key_for_channel_fallback_2'
+10 -1
View File
@@ -9,6 +9,10 @@ services:
JAVA_RUNTIME_IMAGE: ${JAVA_RUNTIME_IMAGE:-eclipse-temurin:21-jre-alpine} JAVA_RUNTIME_IMAGE: ${JAVA_RUNTIME_IMAGE:-eclipse-temurin:21-jre-alpine}
image: moyi-api:${MOYI_IMAGE_TAG:-dev} image: moyi-api:${MOYI_IMAGE_TAG:-dev}
restart: unless-stopped restart: unless-stopped
# Pass per-route key pools from the deployment secret file without
# committing secret names or values to the repository.
env_file:
- .env.cloud
environment: environment:
CONTROL_PLANE_HOST: 0.0.0.0 CONTROL_PLANE_HOST: 0.0.0.0
CONTROL_PLANE_PORT: 8787 CONTROL_PLANE_PORT: 8787
@@ -46,8 +50,13 @@ services:
TRIPO_API_KEY: ${TRIPO_API_KEY:-} TRIPO_API_KEY: ${TRIPO_API_KEY:-}
GATEWAY_REQUESTS_PER_MINUTE: ${GATEWAY_REQUESTS_PER_MINUTE:-60} GATEWAY_REQUESTS_PER_MINUTE: ${GATEWAY_REQUESTS_PER_MINUTE:-60}
GATEWAY_READS_PER_MINUTE: ${GATEWAY_READS_PER_MINUTE:-300} GATEWAY_READS_PER_MINUTE: ${GATEWAY_READS_PER_MINUTE:-300}
GATEWAY_DEFAULT_MODEL_CONCURRENCY: ${GATEWAY_DEFAULT_MODEL_CONCURRENCY:-4} # 0 means no MOYI-side model-group cap; the upstream provider remains
# responsible for its own concurrency and rate limits.
GATEWAY_DEFAULT_MODEL_CONCURRENCY: ${GATEWAY_DEFAULT_MODEL_CONCURRENCY:-0}
GATEWAY_DEFAULT_KEY_CONCURRENCY: ${GATEWAY_DEFAULT_KEY_CONCURRENCY:-4} GATEWAY_DEFAULT_KEY_CONCURRENCY: ${GATEWAY_DEFAULT_KEY_CONCURRENCY:-4}
# Set true for an upstream concurrency measurement run. This bypasses
# all MOYI model/group leases while retaining per-Key protection.
GATEWAY_DISABLE_MODEL_CONCURRENCY: ${GATEWAY_DISABLE_MODEL_CONCURRENCY:-false}
GATEWAY_QUEUE_WAIT_MS: ${GATEWAY_QUEUE_WAIT_MS:-30000} GATEWAY_QUEUE_WAIT_MS: ${GATEWAY_QUEUE_WAIT_MS:-30000}
GATEWAY_QUEUE_POLL_MS: ${GATEWAY_QUEUE_POLL_MS:-100} GATEWAY_QUEUE_POLL_MS: ${GATEWAY_QUEUE_POLL_MS:-100}
TASK_MAX_QUEUED_PER_MODEL: ${TASK_MAX_QUEUED_PER_MODEL:-100} TASK_MAX_QUEUED_PER_MODEL: ${TASK_MAX_QUEUED_PER_MODEL:-100}
+5 -5
View File
@@ -29,7 +29,7 @@ async function remove() {
} }
const channel = reactive({ code: '', name: '', protocol: 'openai-compatible' as CatalogProvider['protocol'], sourceType: 'relay' as CatalogProvider['sourceType'], baseUrl: '', credentialType: 'api-key' as NonNullable<CatalogProvider['credentialType']>, credentialSecret: '', description: '', status: 'active' as CatalogProvider['status'] }) const channel = reactive({ code: '', name: '', protocol: 'openai-compatible' as CatalogProvider['protocol'], sourceType: 'relay' as CatalogProvider['sourceType'], baseUrl: '', credentialType: 'api-key' as NonNullable<CatalogProvider['credentialType']>, credentialSecret: '', description: '', status: 'active' as CatalogProvider['status'] })
const model = reactive({ publicName: '', modelKey: '', vendorName: '', type: 'text' as LogicalModel['type'], mode: 'sync' as LogicalModel['mode'], description: '', inputTypes: 'text', outputTypes: 'text', status: 'active' as LogicalModel['status'] }) const model = reactive({ publicName: '', modelKey: '', vendorName: '', type: 'text' as LogicalModel['type'], mode: 'sync' as LogicalModel['mode'], description: '', inputTypes: 'text', outputTypes: 'text', status: 'active' as LogicalModel['status'] })
const group = reactive({ logicalModelId: '', providerId: '', sourceLabel: '', upstreamModel: '', input: '', output: '', cachedInput: '', unit: 'CNY / 1M tokens', discount: '', discountLabel: '', exchangeRate: '', priceSource: '', listPriceInput: '', listPriceOutput: '', listPriceCachedInput: '', priority: '100', maxConcurrency: '4', status: 'active' as CatalogModel['status'] }) const group = reactive({ logicalModelId: '', providerId: '', sourceLabel: '', upstreamModel: '', input: '', output: '', cachedInput: '', unit: 'CNY / 1M tokens', discount: '', discountLabel: '', exchangeRate: '', priceSource: '', listPriceInput: '', listPriceOutput: '', listPriceCachedInput: '', priority: '100', maxConcurrency: '0', status: 'active' as CatalogModel['status'] })
const shownModels = computed(() => models.value.filter(item => `${item.publicName} ${item.vendorName || ''} ${item.modelKey || ''}`.toLowerCase().includes(query.value.toLowerCase()))) const shownModels = computed(() => models.value.filter(item => `${item.publicName} ${item.vendorName || ''} ${item.modelKey || ''}`.toLowerCase().includes(query.value.toLowerCase())))
const shownChannels = computed(() => channels.value.filter(item => `${item.name} ${item.code}`.toLowerCase().includes(query.value.toLowerCase()))) const shownChannels = computed(() => channels.value.filter(item => `${item.name} ${item.code}`.toLowerCase().includes(query.value.toLowerCase())))
const { page: modelPage, pageSize: modelPageSize, pagedItems: pagedModels } = usePagination(shownModels) const { page: modelPage, pageSize: modelPageSize, pagedItems: pagedModels } = usePagination(shownModels)
@@ -64,7 +64,7 @@ function editModel(item?: LogicalModel) {
} }
function editGroup(parent: LogicalModel, item?: CatalogModel) { function editGroup(parent: LogicalModel, item?: CatalogModel) {
editingId.value = item?.id editingId.value = item?.id
Object.assign(group, { logicalModelId: parent.id, providerId: item?.providerId || '', sourceLabel: item?.sourceLabel || '', upstreamModel: item?.upstreamModel || '', input: item?.pricing?.input?.toString() || '', output: item?.pricing?.output?.toString() || '', cachedInput: item?.pricing?.cachedInput?.toString() || '', unit: item?.pricing?.unit || 'CNY / 1M tokens', discount: item?.pricing?.discount?.toString() || '', discountLabel: item?.pricing?.discountLabel || '', exchangeRate: item?.pricing?.exchangeRate?.toString() || '', priceSource: item?.pricing?.priceSource || '', listPriceInput: item?.pricing?.listPriceUsd?.input?.toString() || '', listPriceOutput: item?.pricing?.listPriceUsd?.output?.toString() || '', listPriceCachedInput: item?.pricing?.listPriceUsd?.cachedInput?.toString() || '', priority: String(item?.priority ?? 100), maxConcurrency: String(item?.maxConcurrency ?? 4), status: item?.status || 'active' }) Object.assign(group, { logicalModelId: parent.id, providerId: item?.providerId || '', sourceLabel: item?.sourceLabel || '', upstreamModel: item?.upstreamModel || '', input: item?.pricing?.input?.toString() || '', output: item?.pricing?.output?.toString() || '', cachedInput: item?.pricing?.cachedInput?.toString() || '', unit: item?.pricing?.unit || 'CNY / 1M tokens', discount: item?.pricing?.discount?.toString() || '', discountLabel: item?.pricing?.discountLabel || '', exchangeRate: item?.pricing?.exchangeRate?.toString() || '', priceSource: item?.pricing?.priceSource || '', listPriceInput: item?.pricing?.listPriceUsd?.input?.toString() || '', listPriceOutput: item?.pricing?.listPriceUsd?.output?.toString() || '', listPriceCachedInput: item?.pricing?.listPriceUsd?.cachedInput?.toString() || '', priority: String(item?.priority ?? 100), maxConcurrency: String(item?.maxConcurrency ?? 0), status: item?.status || 'active' })
editor.value = 'group' editor.value = 'group'
} }
async function save() { async function save() {
@@ -108,7 +108,7 @@ async function toggle(kind: 'channel' | 'model' | 'group', item: CatalogProvider
<section class="page-heading"><h1>{{ props.section === 'channels' ? '渠道管理' : '模型与分组' }}</h1><div class="heading-actions"><button class="icon-button" title="刷新" :disabled="loading" @click="refresh"><RefreshCcw :size="18" /></button><button class="button primary" @click="props.section === 'channels' ? editChannel() : editModel()"><Plus :size="16" />{{ props.section === 'channels' ? '添加渠道' : '添加模型' }}</button></div></section> <section class="page-heading"><h1>{{ props.section === 'channels' ? '渠道管理' : '模型与分组' }}</h1><div class="heading-actions"><button class="icon-button" title="刷新" :disabled="loading" @click="refresh"><RefreshCcw :size="18" /></button><button class="button primary" @click="props.section === 'channels' ? editChannel() : editModel()"><Plus :size="16" />{{ props.section === 'channels' ? '添加渠道' : '添加模型' }}</button></div></section>
<label class="search-input catalog-search"><Search :size="16" /><input v-model="query" :placeholder="props.section === 'channels' ? '搜索渠道名称或 Code' : '搜索模型名称或标识'" /></label> <label class="search-input catalog-search"><Search :size="16" /><input v-model="query" :placeholder="props.section === 'channels' ? '搜索渠道名称或 Code' : '搜索模型名称或标识'" /></label>
<p v-if="error" role="alert">{{ error }}</p> <p v-if="error" role="alert">{{ error }}</p>
<p v-if="props.section === 'models'" class="catalog-secondary">启用模型后会显示在用户端模型广场。模型厂商表示模型的实际研发方,渠道表示请求经过的中转站或官方接口;分组与所属渠道也启用后,用户才能为 API Key 选择该分组。平台 API Key 并发和上游渠道/模型并发是两层独立限制:前者限制同一个 Key 的同时请求数,后者在下方按分组填写,填 1 才是串行,填 4 表示同一渠道分组最多同时处理 4 个请求。</p> <p v-if="props.section === 'models'" class="catalog-secondary">启用模型后会显示在用户端模型广场。模型厂商表示模型的实际研发方,渠道表示请求经过的中转站或官方接口;分组与所属渠道也启用后,用户才能为 API Key 选择该分组。平台 API Key 并发和上游渠道/模型并发是两层独立限制:前者限制同一个 Key 的同时请求数,后者在下方按分组填写;填 0 表示不设置 MOYI 分组上限,直接测量渠道方的真实并发。只有渠道方明确给出共享并发上限时才填写正整数。</p>
<p v-if="loading">加载中…</p> <p v-if="loading">加载中…</p>
<div v-if="props.section === 'channels'" class="responsive-table"><table><thead><tr><th>渠道</th><th>来源 / 协议</th><th>Base URL</th><th>凭据</th><th>状态</th><th>操作</th></tr></thead><tbody><tr v-for="item in pagedChannels" :key="item.id"><td><strong>{{ item.name }}</strong><small class="catalog-secondary">{{ item.code }}</small></td><td>{{ item.sourceType }}<small class="catalog-secondary">{{ item.protocol }}</small></td><td class="catalog-url">{{ item.baseUrl || '未配置' }}</td><td>{{ item.credentialMasked || '未配置' }}</td><td><label class="catalog-toggle"><input type="checkbox" :checked="item.status === 'active'" :disabled="saving" :aria-label="`${item.name} 启用`" @change="toggle('channel', item)" />{{ item.status === 'active' ? '已启用' : '已停用' }}</label></td><td><button class="icon-button" :title="`编辑渠道 ${item.name}`" @click="editChannel(item)"><Pencil :size="16" /></button><button class="icon-button" :title="`删除渠道 ${item.name}`" :disabled="saving" @click="confirmDelete('channel', item.id, item.name)"><Trash2 :size="16" /></button></td></tr><tr v-if="!shownChannels.length && !loading"><td colspan="6">暂无渠道</td></tr></tbody></table></div> <div v-if="props.section === 'channels'" class="responsive-table"><table><thead><tr><th>渠道</th><th>来源 / 协议</th><th>Base URL</th><th>凭据</th><th>状态</th><th>操作</th></tr></thead><tbody><tr v-for="item in pagedChannels" :key="item.id"><td><strong>{{ item.name }}</strong><small class="catalog-secondary">{{ item.code }}</small></td><td>{{ item.sourceType }}<small class="catalog-secondary">{{ item.protocol }}</small></td><td class="catalog-url">{{ item.baseUrl || '未配置' }}</td><td>{{ item.credentialMasked || '未配置' }}</td><td><label class="catalog-toggle"><input type="checkbox" :checked="item.status === 'active'" :disabled="saving" :aria-label="`${item.name} 启用`" @change="toggle('channel', item)" />{{ item.status === 'active' ? '已启用' : '已停用' }}</label></td><td><button class="icon-button" :title="`编辑渠道 ${item.name}`" @click="editChannel(item)"><Pencil :size="16" /></button><button class="icon-button" :title="`删除渠道 ${item.name}`" :disabled="saving" @click="confirmDelete('channel', item.id, item.name)"><Trash2 :size="16" /></button></td></tr><tr v-if="!shownChannels.length && !loading"><td colspan="6">暂无渠道</td></tr></tbody></table></div>
<ListPagination v-if="props.section === 'channels'" v-model:page="channelPage" v-model:page-size="channelPageSize" :total="shownChannels.length" label="渠道分页" /> <ListPagination v-if="props.section === 'channels'" v-model:page="channelPage" v-model:page-size="channelPageSize" :total="shownChannels.length" label="渠道分页" />
@@ -116,7 +116,7 @@ async function toggle(kind: 'channel' | 'model' | 'group', item: CatalogProvider
<section v-for="item in pagedModels" :key="item.id" class="model-definition-section"> <section v-for="item in pagedModels" :key="item.id" class="model-definition-section">
<header class="panel-header"><div><h3>{{ item.publicName }}</h3><small>{{ item.vendorName || '未标注厂商' }} · {{ item.modelKey || item.id }} · {{ item.type }} · {{ item.mode }}</small></div><div class="heading-actions"><label class="catalog-toggle"><input type="checkbox" :checked="item.status === 'active'" :disabled="saving" :aria-label="`${item.publicName} 启用`" @change="toggle('model', item)" />启用</label><button class="icon-button" :title="`编辑模型 ${item.publicName}`" @click="editModel(item)"><Pencil :size="16" /></button><button class="icon-button" :title="`删除模型 ${item.publicName}`" :disabled="saving" @click="confirmDelete('model', item.id, item.publicName)"><Trash2 :size="16" /></button><button class="button secondary small" @click="editGroup(item)"><Plus :size="15" />添加分组</button></div></header> <header class="panel-header"><div><h3>{{ item.publicName }}</h3><small>{{ item.vendorName || '未标注厂商' }} · {{ item.modelKey || item.id }} · {{ item.type }} · {{ item.mode }}</small></div><div class="heading-actions"><label class="catalog-toggle"><input type="checkbox" :checked="item.status === 'active'" :disabled="saving" :aria-label="`${item.publicName} 启用`" @change="toggle('model', item)" />启用</label><button class="icon-button" :title="`编辑模型 ${item.publicName}`" @click="editModel(item)"><Pencil :size="16" /></button><button class="icon-button" :title="`删除模型 ${item.publicName}`" :disabled="saving" @click="confirmDelete('model', item.id, item.publicName)"><Trash2 :size="16" /></button><button class="button secondary small" @click="editGroup(item)"><Plus :size="15" />添加分组</button></div></header>
<p v-if="item.description" class="catalog-secondary">{{ item.description }}</p> <p v-if="item.description" class="catalog-secondary">{{ item.description }}</p>
<div class="responsive-table"><table><thead><tr><th>分组名称</th><th>渠道</th><th>上游模型</th><th>输入 / 输出价格</th><th>对外折扣</th><th>并发</th><th>状态</th><th>操作</th></tr></thead><tbody><tr v-for="route in modelGroups(item.id)" :key="route.id"><td>{{ route.sourceLabel }}</td><td>{{ channelName(route.providerId) }}</td><td><code>{{ route.upstreamModel }}</code></td><td>{{ route.pricing?.input ?? '—' }} / {{ route.pricing?.output ?? '—' }}<small class="catalog-secondary">{{ route.pricing?.unit }}</small></td><td>{{ route.pricing?.discountLabel || '—' }}</td><td>{{ route.maxConcurrency ?? '默认' }}</td><td><label class="catalog-toggle"><input type="checkbox" :checked="route.status === 'active'" :disabled="saving" :aria-label="`${route.sourceLabel} 启用`" @change="toggle('group', route)" />{{ route.status === 'active' ? '已启用' : '已停用' }}</label></td><td><button class="icon-button" :title="`编辑分组 ${route.sourceLabel}`" @click="editGroup(item, route)"><Pencil :size="16" /></button><button class="icon-button" :title="`删除分组 ${route.sourceLabel}`" :disabled="saving" @click="confirmDelete('group', route.id, route.sourceLabel || '默认分组')"><Trash2 :size="16" /></button></td></tr><tr v-if="!modelGroups(item.id).length"><td colspan="8">暂无分组</td></tr></tbody></table></div> <div class="responsive-table"><table><thead><tr><th>分组名称</th><th>渠道</th><th>上游模型</th><th>输入 / 输出价格</th><th>对外折扣</th><th>并发</th><th>状态</th><th>操作</th></tr></thead><tbody><tr v-for="route in modelGroups(item.id)" :key="route.id"><td>{{ route.sourceLabel }}</td><td>{{ channelName(route.providerId) }}</td><td><code>{{ route.upstreamModel }}</code></td><td>{{ route.pricing?.input ?? '—' }} / {{ route.pricing?.output ?? '—' }}<small class="catalog-secondary">{{ route.pricing?.unit }}</small></td><td>{{ route.pricing?.discountLabel || '—' }}</td><td>{{ route.maxConcurrency === undefined || route.maxConcurrency === null || route.maxConcurrency === 0 ? '不限(由渠道限流)' : route.maxConcurrency }}</td><td><label class="catalog-toggle"><input type="checkbox" :checked="route.status === 'active'" :disabled="saving" :aria-label="`${route.sourceLabel} 启用`" @change="toggle('group', route)" />{{ route.status === 'active' ? '已启用' : '已停用' }}</label></td><td><button class="icon-button" :title="`编辑分组 ${route.sourceLabel}`" @click="editGroup(item, route)"><Pencil :size="16" /></button><button class="icon-button" :title="`删除分组 ${route.sourceLabel}`" :disabled="saving" @click="confirmDelete('group', route.id, route.sourceLabel || '默认分组')"><Trash2 :size="16" /></button></td></tr><tr v-if="!modelGroups(item.id).length"><td colspan="8">暂无分组</td></tr></tbody></table></div>
</section> </section>
<p v-if="!shownModels.length && !loading">暂无模型</p> <p v-if="!shownModels.length && !loading">暂无模型</p>
<ListPagination v-model:page="modelPage" v-model:page-size="modelPageSize" :total="shownModels.length" label="管理端模型分页" /> <ListPagination v-model:page="modelPage" v-model:page-size="modelPageSize" :total="shownModels.length" label="管理端模型分页" />
@@ -155,7 +155,7 @@ async function toggle(kind: 'channel' | 'model' | 'group', item: CatalogProvider
<div class="modal-form-grid"><label><span class="form-label">标准输入价(USD / 1M)</span><input v-model="group.listPriceInput" class="text-input" type="number" min="0" step="any" /></label><label><span class="form-label">标准输出价(USD / 1M)</span><input v-model="group.listPriceOutput" class="text-input" type="number" min="0" step="any" /></label></div> <div class="modal-form-grid"><label><span class="form-label">标准输入价(USD / 1M)</span><input v-model="group.listPriceInput" class="text-input" type="number" min="0" step="any" /></label><label><span class="form-label">标准输出价(USD / 1M)</span><input v-model="group.listPriceOutput" class="text-input" type="number" min="0" step="any" /></label></div>
<label class="form-label">标准缓存输入价(USD / 1M,可选)<input v-model="group.listPriceCachedInput" class="text-input" type="number" min="0" step="any" /></label> <label class="form-label">标准缓存输入价(USD / 1M,可选)<input v-model="group.listPriceCachedInput" class="text-input" type="number" min="0" step="any" /></label>
<label class="form-label" for="group-discount">对外折扣(仅展示)</label><input id="group-discount" v-model="group.discountLabel" class="text-input" maxlength="24" placeholder="例如:5.6折" /><small>输入、输出价格是实际扣费值,单位为 CNY / 1M tokens。折扣只用于向用户展示,不会替代人民币售价;留空表示不展示折扣。</small> <label class="form-label" for="group-discount">对外折扣(仅展示)</label><input id="group-discount" v-model="group.discountLabel" class="text-input" maxlength="24" placeholder="例如:5.6折" /><small>输入、输出价格是实际扣费值,单位为 CNY / 1M tokens。折扣只用于向用户展示,不会替代人民币售价;留空表示不展示折扣。</small>
<div class="modal-form-grid"><label><span class="form-label">优先级</span><input v-model="group.priority" class="text-input" type="number" min="0" step="1" required /></label><label><span class="form-label">上游分组并发上限</span><input v-model="group.maxConcurrency" class="text-input" type="number" min="1" step="1" required /><small>这是中转站允许多个用户共享该模型的并发数。1 为串行;按渠道实际额度填写。</small></label></div> <div class="modal-form-grid"><label><span class="form-label">优先级</span><input v-model="group.priority" class="text-input" type="number" min="0" step="1" required /></label><label><span class="form-label">MOYI 分组并发上限</span><input v-model="group.maxConcurrency" class="text-input" type="number" min="0" step="1" required /><small>填 0 表示不设置平台分组上限,直接测量渠道方的真实并发;只有渠道明确给出共享上限时才填写正整数。</small></label></div>
</template> </template>
</form> </form>
<template #footer><button class="button secondary" :disabled="saving" @click="editor = ''">取消</button><button class="button primary" form="catalog-editor" type="submit" :disabled="saving">{{ saving ? '保存中…' : '保存' }}</button></template> <template #footer><button class="button secondary" :disabled="saving" @click="editor = ''">取消</button><button class="button primary" form="catalog-editor" type="submit" :disabled="saving">{{ saving ? '保存中…' : '保存' }}</button></template>
+1
View File
@@ -174,6 +174,7 @@ export interface AdminUsageSummary {
queueWaitMs: number queueWaitMs: number
defaultModelConcurrency: number defaultModelConcurrency: number
defaultKeyConcurrency: number defaultKeyConcurrency: number
modelConcurrencyGuardEnabled: boolean
asyncQueueLimit: number asyncQueueLimit: number
asyncWorkerIntervalMs: number asyncWorkerIntervalMs: number
synchronousSnapshotAvailable: boolean synchronousSnapshotAvailable: boolean
+5 -3
View File
@@ -202,7 +202,9 @@ const queueGroups = computed(() => (adminSummary.value?.capacityGroups || []).ma
warning: group.running === null || group.routeEnabled === false || !!group.queued, warning: group.running === null || group.routeEnabled === false || !!group.queued,
}))) })))
const { page: capacityPage, pageSize: capacityPageSize, pagedItems: capacityPageGroups } = usePagination(queueGroups) const { page: capacityPage, pageSize: capacityPageSize, pagedItems: capacityPageGroups } = usePagination(queueGroups)
const configuredCapacity = computed(() => adminSummary.value?.capacityGroups.filter(group => group.routeEnabled !== false).reduce((total, group) => total + group.limit, 0)) const configuredCapacityGroups = computed(() => adminSummary.value?.capacityGroups.filter(group => group.routeEnabled !== false) || [])
const hasUnlimitedCapacity = computed(() => configuredCapacityGroups.value.some(group => group.limit <= 0))
const configuredCapacity = computed(() => hasUnlimitedCapacity.value ? null : configuredCapacityGroups.value.reduce((total, group) => total + group.limit, 0))
const pricing = computed(() => models.value.filter((model) => model.pricingConfigured).map((model) => ({ const pricing = computed(() => models.value.filter((model) => model.pricingConfigured).map((model) => ({
model: model.name, model: model.name,
unit: model.pricing?.unit || 'CNY / 1M tokens', unit: model.pricing?.unit || 'CNY / 1M tokens',
@@ -293,12 +295,12 @@ function mapModels(items: CatalogModel[], providersList: CatalogProvider[]): Mod
<template v-else-if="view === 'admin-capacity'"> <template v-else-if="view === 'admin-capacity'">
<section class="page-heading"><div><span>CAPACITY CONTROL</span><h1>并发与队列</h1><p>显示实时 Redis 租约、异步任务队列和每个模型分组的配置上限。</p></div><div class="heading-actions"><span class="admin-badge"><ShieldCheck :size="15" />{{ adminSummary?.capacityConfig ? '运行配置已读取' : '运行配置未读取' }}</span><button class="button secondary" @click="refreshOverviewData(true)"><RefreshCcw :size="15" />刷新监控</button></div></section> <section class="page-heading"><div><span>CAPACITY CONTROL</span><h1>并发与队列</h1><p>显示实时 Redis 租约、异步任务队列和每个模型分组的配置上限。</p></div><div class="heading-actions"><span class="admin-badge"><ShieldCheck :size="15" />{{ adminSummary?.capacityConfig ? '运行配置已读取' : '运行配置未读取' }}</span><button class="button secondary" @click="refreshOverviewData(true)"><RefreshCcw :size="15" />刷新监控</button></div></section>
<section class="capacity-grid"> <section class="capacity-grid">
<article class="capacity-main"><div class="capacity-ring"><strong>{{ adminSummary?.running ?? '—' }}</strong><span>实时运行中</span></div><div><small>CONFIGURED CAPACITY</small><h2>{{ configuredCapacity ?? '—' }} 个配置槽位</h2><p>启用分组的上限合计;上游共享限额仍会影响实际吞吐。</p></div></article> <article class="capacity-main"><div class="capacity-ring"><strong>{{ adminSummary?.running ?? '—' }}</strong><span>实时运行中</span></div><div><small>CONFIGURED CAPACITY</small><h2>{{ hasUnlimitedCapacity ? '由渠道限流' : configuredCapacity ?? '—' }}{{ hasUnlimitedCapacity || configuredCapacity === undefined ? '' : ' 个配置槽位' }}</h2><p>启用分组的上限合计;当前为不限时由渠道方的真实并发和限流决定。</p></div></article>
<article class="metric-card"><span class="icon-tile orange"><Clock3 :size="18" /></span><div><small>等待任务</small><strong>{{ adminSummary?.waiting ?? '—' }}</strong><em>不含正在执行的请求</em></div></article> <article class="metric-card"><span class="icon-tile orange"><Clock3 :size="18" /></span><div><small>等待任务</small><strong>{{ adminSummary?.waiting ?? '—' }}</strong><em>不含正在执行的请求</em></div></article>
<article class="metric-card"><span class="icon-tile cyan"><Timer :size="18" /></span><div><small>同步排队窗口</small><strong>{{ adminSummary?.capacityConfig ? `${adminSummary.capacityConfig.queueWaitMs / 1000}s` : '—' }}</strong><em>读取当前服务器配置</em></div></article> <article class="metric-card"><span class="icon-tile cyan"><Timer :size="18" /></span><div><small>同步排队窗口</small><strong>{{ adminSummary?.capacityConfig ? `${adminSummary.capacityConfig.queueWaitMs / 1000}s` : '—' }}</strong><em>读取当前服务器配置</em></div></article>
<article class="metric-card"><span class="icon-tile green"><CheckCircle2 :size="18" /></span><div><small>同步占用快照</small><strong>{{ adminSummary?.capacityConfig?.synchronousSnapshotAvailable ? '已读取' : '未读取' }}</strong><em>来自 Redis 有效租约</em></div></article> <article class="metric-card"><span class="icon-tile green"><CheckCircle2 :size="18" /></span><div><small>同步占用快照</small><strong>{{ adminSummary?.capacityConfig?.synchronousSnapshotAvailable ? '已读取' : '未读取' }}</strong><em>来自 Redis 有效租约</em></div></article>
</section> </section>
<p v-if="adminSummary?.capacityConfig" class="billing-note">单 Key 并发上限 {{ adminSummary.capacityConfig.defaultKeyConcurrency }};未单独设置的模型分组默认上限 {{ adminSummary.capacityConfig.defaultModelConcurrency }}。每组异步等待与执行总量上限 {{ adminSummary.capacityConfig.asyncQueueLimit }},任务扫描间隔 {{ adminSummary.capacityConfig.asyncWorkerIntervalMs / 1000 }} 秒。同步与异步分别计数。</p> <p v-if="adminSummary?.capacityConfig" class="billing-note">单 Key 并发上限 {{ adminSummary.capacityConfig.defaultKeyConcurrency }};模型分组守卫 {{ adminSummary.capacityConfig.modelConcurrencyGuardEnabled ? `启用(未设置分组时默认 ${adminSummary.capacityConfig.defaultModelConcurrency})` : '已关闭(测量模式)' }}。每组异步等待与执行总量上限 {{ adminSummary.capacityConfig.asyncQueueLimit }},任务扫描间隔 {{ adminSummary.capacityConfig.asyncWorkerIntervalMs / 1000 }} 秒。同步与异步分别计数。</p>
<section class="panel table-card"><header class="panel-header"><div><span>CAPACITY GROUPS</span><h3>并发与排队状态</h3></div><span class="select-button static">{{ queueGroups.length }} 个分组 · 实时快照</span></header><div class="responsive-table"><table><thead><tr><th>资源组</th><th>运行中</th><th>并发上限</th><th>排队数</th><th>计数来源</th><th>状态</th></tr></thead><tbody><tr v-for="group in capacityPageGroups" :key="group.id"><td><div class="stacked-cell"><strong>{{ group.name }}</strong><small>{{ group.type }}</small></div></td><td>{{ group.running ?? '—' }}</td><td>{{ group.limit }}</td><td>{{ group.queued ?? '—' }}</td><td>{{ group.mode === 'async' ? '异步任务队列' : 'Redis 有效租约' }}</td><td><span class="status-chip" :class="group.warning ? 'warning' : 'success'"><i></i>{{ group.status }}</span></td></tr><tr v-if="!queueGroups.length"><td colspan="6"><div class="empty-state compact"><Gauge :size="22" /><strong>{{ adminSummary ? '暂无活跃模型分组' : '容量数据尚未读取' }}</strong><span>启用模型分组后会显示其配置与占用情况</span></div></td></tr></tbody></table></div><ListPagination v-model:page="capacityPage" v-model:page-size="capacityPageSize" :total="queueGroups.length" label="容量分组分页" /></section> <section class="panel table-card"><header class="panel-header"><div><span>CAPACITY GROUPS</span><h3>并发与排队状态</h3></div><span class="select-button static">{{ queueGroups.length }} 个分组 · 实时快照</span></header><div class="responsive-table"><table><thead><tr><th>资源组</th><th>运行中</th><th>并发上限</th><th>排队数</th><th>计数来源</th><th>状态</th></tr></thead><tbody><tr v-for="group in capacityPageGroups" :key="group.id"><td><div class="stacked-cell"><strong>{{ group.name }}</strong><small>{{ group.type }}</small></div></td><td>{{ group.running ?? '—' }}</td><td>{{ group.limit }}</td><td>{{ group.queued ?? '—' }}</td><td>{{ group.mode === 'async' ? '异步任务队列' : 'Redis 有效租约' }}</td><td><span class="status-chip" :class="group.warning ? 'warning' : 'success'"><i></i>{{ group.status }}</span></td></tr><tr v-if="!queueGroups.length"><td colspan="6"><div class="empty-state compact"><Gauge :size="22" /><strong>{{ adminSummary ? '暂无活跃模型分组' : '容量数据尚未读取' }}</strong><span>启用模型分组后会显示其配置与占用情况</span></div></td></tr></tbody></table></div><ListPagination v-model:page="capacityPage" v-model:page-size="capacityPageSize" :total="queueGroups.length" label="容量分组分页" /></section>
</template> </template>
+1 -1
View File
@@ -175,7 +175,7 @@ onMounted(async () => {
<i class="auth-orbit orbit-a"></i><i class="auth-orbit orbit-b"></i> <i class="auth-orbit orbit-a"></i><i class="auth-orbit orbit-b"></i>
<img src="/assets/moyi-logo.png" alt="" /> <img src="/assets/moyi-logo.png" alt="" />
</div> </div>
<footer><span :class="`service-${serviceStatus}`"><i></i>{{ serviceStatus === 'online' ? '服务可用' : serviceStatus === 'offline' ? '服务暂不可用' : '正在检测服务' }}</span><small>© 2026 模翎科技</small></footer> <footer><span :class="`service-${serviceStatus}`"><i></i>{{ serviceStatus === 'online' ? '服务可用' : serviceStatus === 'offline' ? '服务暂不可用' : '正在检测服务' }}</span><small>© 2026 模灵科技</small></footer>
</section> </section>
<section class="auth-form-area"> <section class="auth-form-area">
+145 -40
View File
@@ -130,6 +130,17 @@ const projects = ref<CatalogProject[]>([])
const keyScopeType = ref<'personal' | 'project'>('personal') const keyScopeType = ref<'personal' | 'project'>('personal')
const selectedProjectId = ref('') const selectedProjectId = ref('')
const selectedApiKeyId = ref('') const selectedApiKeyId = ref('')
function scrollDocs(target: string) {
const selectors: Record<string, string> = {
'docs-overview': '.docs-layout',
'docs-auth': '.docs-path-card',
'docs-chat': '.docs-detail-grid .docs-detail-card:nth-child(2)',
'docs-reference': '.docs-reference-table',
'docs-troubleshooting': '.docs-detail-grid .docs-detail-card:last-child',
'docs-usage': '.docs-guide-extra',
}
document.querySelector(selectors[target] || target)?.scrollIntoView({ behavior: 'smooth', block: 'start' })
}
const activeDebugKeys = computed(() => keys.value.filter((key) => key.status === '正常')) const activeDebugKeys = computed(() => keys.value.filter((key) => key.status === '正常'))
const selectedDebugKey = computed(() => activeDebugKeys.value.find((key) => String(key.id) === selectedApiKeyId.value)) const selectedDebugKey = computed(() => activeDebugKeys.value.find((key) => String(key.id) === selectedApiKeyId.value))
const debugKeyCanInvoke = computed(() => !!selectedDebugKey.value?.modelIds.includes(activeModel.value.id)) const debugKeyCanInvoke = computed(() => !!selectedDebugKey.value?.modelIds.includes(activeModel.value.id))
@@ -162,6 +173,72 @@ const taskPollers = new Map<string, number>()
const copyTimers = new Map<string, number>() const copyTimers = new Map<string, number>()
const copyStates = ref<Record<string, CopyResult | undefined>>({}) const copyStates = ref<Record<string, CopyResult | undefined>>({})
/**
* The user portal must still render when an older control-plane node returns a
* partial payload. A missing optional field used to throw during Vue render
* (for example `series.map` or `asyncSuccessRate.toFixed`) and blanked the
* entire workspace for ordinary users. Keep the API contract typed while
* normalising the untrusted JSON boundary here.
*/
function asRecord(value: unknown): Record<string, unknown> {
return value && typeof value === 'object' && !Array.isArray(value) ? value as Record<string, unknown> : {}
}
function asText(value: unknown, fallback = ''): string {
return typeof value === 'string' ? value : value == null ? fallback : String(value)
}
function asNumber(value: unknown, fallback = 0): number {
const number = typeof value === 'number' ? value : Number(value)
return Number.isFinite(number) ? number : fallback
}
function optionalNumber(value: unknown): number | undefined {
if (value == null || value === '') return undefined
const number = Number(value)
return Number.isFinite(number) ? number : undefined
}
function asStringArray(value: unknown): string[] {
return Array.isArray(value) ? value.filter((item): item is string => typeof item === 'string') : []
}
function normalizeOverview(value: unknown): PlatformOverview {
const raw = asRecord(value)
const rawTasks = asRecord(raw.tasks)
const series = Array.isArray(raw.series) ? raw.series.flatMap((item) => {
const row = asRecord(item)
const date = asText(row.date)
return date ? [{ date, count: asNumber(row.count) }] : []
}) : []
const ledger = Array.isArray(raw.ledger) ? raw.ledger.flatMap((item) => {
const row = asRecord(item)
const id = asText(row.id)
return id ? [{ id, title: asText(row.title, '额度流水'), amount: asNumber(row.amount), createdAt: asText(row.createdAt) }] : []
}) : []
return {
generatedAt: asText(raw.generatedAt),
callsToday: asNumber(raw.callsToday),
callsThisMonth: asNumber(raw.callsThisMonth),
averageLatencyMs: optionalNumber(raw.averageLatencyMs),
availableCredit: optionalNumber(raw.availableCredit),
balance: optionalNumber(raw.balance),
reservedCredit: optionalNumber(raw.reservedCredit),
costThisMonth: optionalNumber(raw.costThisMonth),
availabilityPercent: optionalNumber(raw.availabilityPercent),
asyncTasks: asNumber(raw.asyncTasks),
asyncSuccessRate: optionalNumber(raw.asyncSuccessRate),
tasks: {
queued: asNumber(rawTasks.queued),
running: asNumber(rawTasks.running),
succeeded: asNumber(rawTasks.succeeded),
failed: asNumber(rawTasks.failed),
},
series,
ledger,
}
}
function selectDebugApiKey(id: string) { function selectDebugApiKey(id: string) {
selectedApiKeyId.value = id selectedApiKeyId.value = id
} }
@@ -169,15 +246,17 @@ function selectDebugApiKey(id: string) {
onMounted(async () => { onMounted(async () => {
try { try {
const [catalog, keyResult, projectResult, overviewResult] = await Promise.all([catalogClient.listModels(), catalogClient.listKeys(), catalogClient.listProjects(), catalogClient.getOverview()]) const [catalog, keyResult, projectResult, overviewResult] = await Promise.all([catalogClient.listModels(), catalogClient.listKeys(), catalogClient.listProjects(), catalogClient.getOverview()])
// Treat every API collection as untrusted JSON. This keeps a partially
// migrated control plane from taking down the whole authenticated shell.
catalogModels.value = mapCatalogModels(catalog.models, catalog.providers) catalogModels.value = mapCatalogModels(catalog.models, catalog.providers)
catalogDefinitions.value = catalog.logicalModels catalogDefinitions.value = Array.isArray(catalog.logicalModels) ? catalog.logicalModels : []
catalogRoutes.value = catalog.models catalogRoutes.value = Array.isArray(catalog.models) ? catalog.models : []
if (catalogModels.value[0]) selectPlaygroundGroup(catalogModels.value[0].id) if (catalogModels.value[0]) selectPlaygroundGroup(catalogModels.value[0].id)
keys.value = keyResult.keys.map((key) => mapCatalogKey(key)) keys.value = (Array.isArray(keyResult.keys) ? keyResult.keys : []).map((key) => mapCatalogKey(key))
selectedApiKeyId.value = String(keys.value.find((key) => key.status === '正常')?.id || '') selectedApiKeyId.value = String(keys.value.find((key) => key.status === '正常')?.id || '')
projects.value = projectResult.projects projects.value = Array.isArray(projectResult.projects) ? projectResult.projects : []
overview.value = overviewResult overview.value = normalizeOverview(overviewResult)
emit('balance', overviewResult.availableCredit) emit('balance', overview.value.availableCredit)
overviewLoaded.value = true overviewLoaded.value = true
catalogConnected.value = true catalogConnected.value = true
try { try {
@@ -193,6 +272,7 @@ onMounted(async () => {
keys.value = [] keys.value = []
projects.value = [] projects.value = []
activeModel.value = emptyModel activeModel.value = emptyModel
overview.value = normalizeOverview(undefined)
overviewLoaded.value = false overviewLoaded.value = false
catalogConnected.value = false catalogConnected.value = false
} }
@@ -305,7 +385,7 @@ function resetModelFilters() {
async function refreshOverview() { async function refreshOverview() {
try { try {
overview.value = await catalogClient.getOverview() overview.value = normalizeOverview(await catalogClient.getOverview())
emit('balance', overview.value.availableCredit) emit('balance', overview.value.availableCredit)
overviewLoaded.value = true overviewLoaded.value = true
} catch {} } catch {}
@@ -513,14 +593,26 @@ async function createKey() {
} }
} }
function mapCatalogModels(items: CatalogModel[], providers: Array<{ id: string; name: string }>): ModelItem[] { function mapCatalogModels(items: unknown, providers: unknown): ModelItem[] {
const providerNames = new Map(providers.map((provider) => [provider.id, provider.name])) const providerRows = Array.isArray(providers) ? providers : []
return items.map(item => ({ const providerNames = new Map(providerRows.map((provider) => {
const row = asRecord(provider)
return [asText(row.id), asText(row.name)] as const
}).filter(([id]) => !!id))
const modelRows = Array.isArray(items) ? items : []
return modelRows.map(item => {
const row = asRecord(item)
const model = item as Partial<CatalogModel>
const providerId = asText(row.providerId)
const inputTypes = asStringArray(row.inputTypes)
const outputTypes = asStringArray(row.outputTypes)
return {
...mapCatalogModel(item), logicalModelId: item.logicalModelId, gatewayModelId: item.gatewayModelId, ...mapCatalogModel(item), logicalModelId: item.logicalModelId, gatewayModelId: item.gatewayModelId,
provider: providerNames.get(item.providerId) || item.providerId, provider: providerNames.get(providerId) || providerId || '未知渠道',
vendorName: item.vendorName, vendorName: model.vendorName,
sourceLabel: item.sourceLabel, pricing: item.pricing, pricingConfigured: item.pricingConfigured, billingReady: item.billingReady, priority: item.priority, maxConcurrency: item.maxConcurrency, executionReady: item.executionReady, inputTypes: item.inputTypes, outputTypes: item.outputTypes, sourceLabel: model.sourceLabel, pricing: model.pricing, pricingConfigured: model.pricingConfigured, billingReady: model.billingReady, priority: model.priority, maxConcurrency: model.maxConcurrency, executionReady: model.executionReady, inputTypes, outputTypes,
})) }
})
} }
/** /**
@@ -533,25 +625,31 @@ function gatewayModelId(group: ModelItem): string {
return group.gatewayModelId || group.modelKey || group.id return group.gatewayModelId || group.modelKey || group.id
} }
function mapCatalogModel(item: LogicalModel): ModelItem { function mapCatalogModel(item: Partial<LogicalModel> | Partial<CatalogModel>): ModelItem {
const typeLabels: Record<string, ModelType> = { text: '文本', code: '文本', multimodal: '多模态', image: '图像', audio: '音频', video: '视频', embedding: '文本', '3d': '3D' } const typeLabels: Record<string, ModelType> = { text: '文本', code: '文本', multimodal: '多模态', image: '图像', audio: '音频', video: '视频', embedding: '文本', '3d': '3D' }
const modeLabels: Record<string, CallMode> = { sync: '同步', stream: '流式', async: '异步' } const modeLabels: Record<string, CallMode> = { sync: '同步', stream: '流式', async: '异步' }
const row = item as Record<string, unknown>
const id = asText(row.id)
const type = asText(row.type, 'text')
const mode = asText(row.mode, 'sync')
const inputTypes = asStringArray(row.inputTypes)
const outputTypes = asStringArray(row.outputTypes)
return { return {
id: item.id, id,
name: item.publicName, name: asText(row.publicName, asText(row.modelKey, id || '未命名模型')),
provider: '', provider: '',
vendorName: item.vendorName, vendorName: asText(row.vendorName) || undefined,
type: typeLabels[item.type] || '文本', type: typeLabels[type] || '文本',
mode: modeLabels[item.mode] || '同步', mode: modeLabels[mode] || '同步',
description: item.description, description: asText(row.description),
latency: item.mode === 'async' ? '异步任务' : '待采样', latency: mode === 'async' ? '异步任务' : '待采样',
specification: item.outputTypes.join(' / ') || '统一接口', specification: outputTypes.join(' / ') || '统一接口',
inputTypes: item.inputTypes, inputTypes,
outputTypes: item.outputTypes, outputTypes,
availability: item.status === 'active' ? '在线' : '已停用', availability: asText(row.status, 'active') === 'active' ? '在线' : '已停用',
accent: item.type === '3d' ? 'orange' : 'violet', accent: type === '3d' ? 'orange' : 'violet',
icon: item.type === '3d' ? Box : item.type === 'image' ? Sparkles : MessageSquareText, icon: type === '3d' ? Box : type === 'image' ? Sparkles : MessageSquareText,
modelKey: item.modelKey, modelKey: asText(row.modelKey) || undefined,
} }
} }
@@ -563,17 +661,24 @@ function keyPermissionSummary(key: ApiKeyItem) {
} }
function mapCatalogKey(key: CatalogApiKey, plaintext = ''): ApiKeyItem { function mapCatalogKey(key: CatalogApiKey, plaintext = ''): ApiKeyItem {
const raw = key as Partial<CatalogApiKey>
const id = asText(raw.id)
const keyName = asText(raw.name, '未命名 Key')
const keyPrefix = asText(raw.keyPrefix, 'moyi_')
const modelIds = asStringArray(raw.modelIds)
const createdAt = asText(raw.createdAt)
const lastUsedAt = asText(raw.lastUsedAt)
return { return {
id: key.id, id,
name: key.name, name: keyName,
value: plaintext ? `${plaintext.slice(0, 10)}••••••••${plaintext.slice(-4)}` : `${key.keyPrefix}••••••••••••`, value: plaintext ? `${plaintext.slice(0, 10)}••••••••${plaintext.slice(-4)}` : `${keyPrefix}••••••••••••`,
scope: key.scopeType === 'project' ? `项目 Key${key.projectId ? ` · ${key.projectId.slice(0, 12)}` : ''}` : '个人 Key', scope: raw.scopeType === 'project' ? `项目 Key${raw.projectId ? ` · ${String(raw.projectId).slice(0, 12)}` : ''}` : '个人 Key',
scopeType: key.scopeType, scopeType: raw.scopeType === 'project' ? 'project' : 'personal',
projectId: key.projectId, projectId: raw.projectId,
modelIds: [...key.modelIds], modelIds,
createdAt: key.createdAt.slice(0, 10), createdAt: createdAt ? createdAt.slice(0, 10) : '未知时间',
lastUsed: key.lastUsedAt ? key.lastUsedAt.slice(0, 16).replace('T', ' ') : '尚未使用', lastUsed: lastUsedAt ? lastUsedAt.slice(0, 16).replace('T', ' ') : '尚未使用',
status: key.status === 'active' ? '正常' : '已停用', status: raw.status === 'active' ? '正常' : '已停用',
} }
} }
@@ -771,7 +876,7 @@ async function createProject() {
<template v-else> <template v-else>
<section class="page-heading"><div><span>DEVELOPER DOCS</span><h1>接入文档</h1><p>使用标准接口把模型能力接入你的应用。</p></div><button class="button secondary" @click="copy(platformApiBaseUrl, 'Base URL', 'docs-base-url')"><Check v-if="copySucceeded('docs-base-url')" :size="16" /><Copy v-else :size="16" />{{ copySucceeded('docs-base-url') ? '已复制' : copyStates['docs-base-url'] === 'failed' ? '复制失败' : '复制 Base URL' }}</button></section> <section class="page-heading"><div><span>DEVELOPER DOCS</span><h1>接入文档</h1><p>使用标准接口把模型能力接入你的应用。</p></div><button class="button secondary" @click="copy(platformApiBaseUrl, 'Base URL', 'docs-base-url')"><Check v-if="copySucceeded('docs-base-url')" :size="16" /><Copy v-else :size="16" />{{ copySucceeded('docs-base-url') ? '已复制' : copyStates['docs-base-url'] === 'failed' ? '复制失败' : '复制 Base URL' }}</button></section>
<section class="docs-layout"><aside class="docs-menu panel"><span>快速开始</span><button class="active">概览</button><button>认证与 API Key</button><button>标准对话接口</button><button>异步任务</button><button>错误码与重试</button><span>API 参考</span><button>模型列表</button><button>用量查询</button></aside><article class="docs-content panel"><span class="eyebrow">QUICK START</span><h2>三分钟完成第一次调用</h2><p>只需要统一 API 地址、API Key 和模型名称,即可开始使用模亦灵枢。</p><section><b>01</b><div><h3>准备接入信息</h3><p>在用户端创建 API Key,并保存以下环境变量。</p><pre><code>MOYI_BASE_URL={{ platformApiBaseUrl }} <section id="docs-overview" class="docs-layout"><aside class="docs-menu panel"><span>快速开始</span><button class="active" type="button" @click="scrollDocs('docs-overview')">概览</button><button type="button" @click="scrollDocs('docs-auth')">认证与 API Key</button><button type="button" @click="scrollDocs('docs-chat')">标准对话接口</button><button type="button" @click="scrollDocs('docs-reference')">异步任务</button><button type="button" @click="scrollDocs('docs-troubleshooting')">错误码与重试</button><span>API 参考</span><button type="button" @click="scrollDocs('docs-reference')">模型列表</button><button type="button" @click="scrollDocs('docs-usage')">用量查询</button></aside><article class="docs-content panel"><span class="eyebrow">QUICK START</span><h2>三分钟完成第一次调用</h2><p>只需要统一 API 地址、API Key 和模型名称,即可开始使用模亦灵枢。</p><section><b>01</b><div><h3>准备接入信息</h3><p>在用户端创建 API Key,并保存以下环境变量。</p><pre><code>MOYI_BASE_URL={{ platformApiBaseUrl }}
MOYI_API_KEY=sk-moyi-your-key MOYI_API_KEY=sk-moyi-your-key
MOYI_MODEL=&lt;model-id&gt;</code></pre></div></section><section><b>02</b><div><h3>发送标准请求</h3><p>文本模型支持同步和流式响应;3D、图像、视频使用异步任务接口。</p><pre><code>curl {{ platformApiBaseUrl }}/chat/completions \ MOYI_MODEL=&lt;model-id&gt;</code></pre></div></section><section><b>02</b><div><h3>发送标准请求</h3><p>文本模型支持同步和流式响应;3D、图像、视频使用异步任务接口。</p><pre><code>curl {{ platformApiBaseUrl }}/chat/completions \
-H "Authorization: Bearer $MOYI_API_KEY" \ -H "Authorization: Bearer $MOYI_API_KEY" \