feat: publish readable model ids and media task adapters

This commit is contained in:
wlx committed 2026-09-29 01:29:07 +08:00
1 parent ba98b47618
commit 94eaee7b95
16 files changed
+654 -41

No files matched your search

@@ -6,6 +6,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
import com.moyi.controlplane.auth.AuthService; import com.moyi.controlplane.auth.AuthService;
import com.moyi.controlplane.auth.AuthService.UserView; import com.moyi.controlplane.auth.AuthService.UserView;
import com.moyi.controlplane.common.ApiException; import com.moyi.controlplane.common.ApiException;
import com.moyi.controlplane.gateway.GatewayModelIds;
import com.moyi.controlplane.security.SecretCipher; import com.moyi.controlplane.security.SecretCipher;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.security.MessageDigest; import java.security.MessageDigest;
@@ -49,7 +50,7 @@ public class CatalogService {
public List<Map<String, Object>> listModels(boolean includeDisabled) { public List<Map<String, Object>> listModels(boolean includeDisabled) {
String statusClause = " WHERE m.deleted_at IS NULL AND p.deleted_at IS NULL AND d.deleted_at IS NULL" + (includeDisabled ? "" : " AND m.status = 'active' AND p.status = 'active' AND d.status = 'active'"); String statusClause = " WHERE m.deleted_at IS NULL AND p.deleted_at IS NULL AND d.deleted_at IS NULL" + (includeDisabled ? "" : " AND m.status = 'active' AND p.status = 'active' AND d.status = 'active'");
return jdbc.query("SELECT m.*, d.vendor_name FROM model_catalog m JOIN model_providers p ON p.id=m.provider_id JOIN model_definitions d ON d.id=m.logical_model_id" + statusClause + " ORDER BY m.public_name, m.source_label", this::mapModel); return jdbc.query("SELECT m.*, d.vendor_name, p.code AS provider_code, p.protocol AS provider_protocol FROM model_catalog m JOIN model_providers p ON p.id=m.provider_id JOIN model_definitions d ON d.id=m.logical_model_id" + statusClause + " ORDER BY m.public_name, m.source_label", this::mapModel);
} }
public List<Map<String, Object>> listDefinitions(boolean includeDisabled) { public List<Map<String, Object>> listDefinitions(boolean includeDisabled) {
@@ -453,12 +454,12 @@ public class CatalogService {
} }
private Map<String, Object> findModel(String id) { private Map<String, Object> findModel(String id) {
try { return jdbc.queryForObject("SELECT m.*, d.vendor_name FROM model_catalog m JOIN model_definitions d ON d.id=m.logical_model_id WHERE m.id = ? AND m.deleted_at IS NULL", this::mapModel, id); } try { return jdbc.queryForObject("SELECT m.*, d.vendor_name, p.code AS provider_code, p.protocol AS provider_protocol FROM model_catalog m JOIN model_providers p ON p.id=m.provider_id JOIN model_definitions d ON d.id=m.logical_model_id WHERE m.id = ? AND m.deleted_at IS NULL", this::mapModel, id); }
catch (EmptyResultDataAccessException exception) { return null; } catch (EmptyResultDataAccessException exception) { return null; }
} }
private Map<String, Object> findActiveModel(String id) { private Map<String, Object> findActiveModel(String id) {
try { return jdbc.queryForObject("SELECT m.*, d.vendor_name FROM model_catalog m JOIN model_providers p ON p.id=m.provider_id JOIN model_definitions d ON d.id=m.logical_model_id WHERE m.id = ? AND m.status = 'active' AND p.status='active' AND d.status='active' AND m.deleted_at IS NULL AND p.deleted_at IS NULL AND d.deleted_at IS NULL", this::mapModel, id); } try { return jdbc.queryForObject("SELECT m.*, d.vendor_name, p.code AS provider_code, p.protocol AS provider_protocol FROM model_catalog m JOIN model_providers p ON p.id=m.provider_id JOIN model_definitions d ON d.id=m.logical_model_id WHERE m.id = ? AND m.status = 'active' AND p.status='active' AND d.status='active' AND m.deleted_at IS NULL AND p.deleted_at IS NULL AND d.deleted_at IS NULL", this::mapModel, id); }
catch (EmptyResultDataAccessException exception) { return null; } catch (EmptyResultDataAccessException exception) { return null; }
} }
@@ -514,9 +515,19 @@ public class CatalogService {
putIfNotNull(model, "priority", resultSet.getObject("priority")); putIfNotNull(model, "priority", resultSet.getObject("priority"));
putIfNotNull(model, "maxConcurrency", resultSet.getObject("max_concurrency")); putIfNotNull(model, "maxConcurrency", resultSet.getObject("max_concurrency"));
model.put("status", resultSet.getString("status")); model.put("status", resultSet.getString("status"));
model.put("executionReady", !"async".equals(resultSet.getString("call_mode")) String providerCode = resultSet.getString("provider_code");
|| List.of("provider-mock", "provider-vast", "provider-bytedance").contains(resultSet.getString("provider_id"))); String providerProtocol = resultSet.getString("provider_protocol");
// Only adapters that are implemented and verified are published as
// executable. Relay catalog rows stay visibly pending until their
// provider endpoint has been configured and tested; protocol alone is
// not proof that an image/video/audio task API exists.
boolean asyncExecutor = List.of("provider-mock", "provider-vast", "provider-bytedance").contains(resultSet.getString("provider_id"));
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
// stable, readable identifier for the user console and third-party
// clients. The gateway uses the same value in /v1/models.
model.put("gatewayModelId", GatewayModelIds.publicId(model));
model.put("createdAt", resultSet.getObject("created_at", OffsetDateTime.class)); model.put("createdAt", resultSet.getObject("created_at", OffsetDateTime.class));
return model; return model;
} }
@@ -41,16 +41,16 @@ public class GatewayController {
public Map<String, Object> models(@RequestHeader(value = "Authorization", required = false) String authorization) { public Map<String, Object> models(@RequestHeader(value = "Authorization", required = false) String authorization) {
Map<String, Object> key = requireKey(authorization); Map<String, Object> key = requireKey(authorization);
guard.checkRate(UUID.fromString(String.valueOf(key.get("id"))), true); guard.checkRate(UUID.fromString(String.valueOf(key.get("id"))), true);
List<Map<String, Object>> data = catalogService.listModels(false).stream() List<Map<String, Object>> visibleModels = GatewayModelIds.authorizedModels(catalogService, key);
.filter(model -> catalogService.keyCanInvoke(key, String.valueOf(model.get("id")))) List<Map<String, Object>> data = visibleModels.stream()
.map(model -> { .map(model -> {
Map<String, Object> item = new LinkedHashMap<>(); Map<String, Object> item = new LinkedHashMap<>();
item.put("id", model.get("id")); String publicId = GatewayModelIds.publicId(model);
item.put("id", publicId);
item.put("name", model.get("publicName"));
item.put("object", "model"); item.put("object", "model");
item.put("owned_by", model.get("providerId")); item.put("owned_by", model.get("providerId"));
// Keep the OpenAI-compatible id as the exact catalog route UUID. item.put("moyi_route_id", model.get("id"));
// These descriptive fields let clients show the logical model and
// source group without guessing that modelKey is a callable id.
item.put("model_key", model.get("modelKey")); item.put("model_key", model.get("modelKey"));
item.put("public_name", model.get("publicName")); item.put("public_name", model.get("publicName"));
item.put("vendor_name", model.get("vendorName")); item.put("vendor_name", model.get("vendorName"));
@@ -79,9 +79,8 @@ public class GatewayController {
Map<String, Object> input, String idempotencyKey, HttpServletResponse servletResponse) { Map<String, Object> input, String idempotencyKey, HttpServletResponse servletResponse) {
long startedAt = System.currentTimeMillis(); long startedAt = System.currentTimeMillis();
String modelId = String.valueOf(input.getOrDefault("model", "")); String modelId = String.valueOf(input.getOrDefault("model", ""));
Map<String, Object> model = catalogService.activeModel(modelId); Map<String, Object> model = resolveModel(key, modelId);
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 (!catalogService.keyCanInvoke(key, modelId)) throw new ApiException("MODEL_FORBIDDEN", "当前 Key 无权调用该模型", 403);
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"));
boolean mock="provider-mock".equals(providerId); boolean mock="provider-mock".equals(providerId);
@@ -96,7 +95,7 @@ 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,request.body(),ticket,servletResponse,startedAt,mock); stream(model,modelId,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")),request.body());
@@ -118,8 +117,7 @@ public class GatewayController {
} }
} }
private void stream(Map<String,Object> model, Map<String,Object> input, ChatBillingService.Ticket ticket, HttpServletResponse response, long started, boolean mock) throws IOException { private void stream(Map<String,Object> model, String modelId, Map<String,Object> input, ChatBillingService.Ticket ticket, HttpServletResponse response, long started, boolean mock) throws IOException {
String modelId=model.get("id").toString();
if(mock) { if(mock) {
var body=mockResponse(modelId,input); var body=mockResponse(modelId,input);
prepareStream(response); prepareStream(response);
@@ -228,4 +226,13 @@ public class GatewayController {
}).reduce((left, right) -> left + "\n" + right).orElse(""); }).reduce((left, right) -> left + "\n" + right).orElse("");
} }
/**
* Resolve a caller supplied model identifier to a route allowed by this
* API key. Route UUIDs remain valid for existing clients; the readable IDs
* returned by /v1/models are preferred for new clients.
*/
Map<String, Object> resolveModel(Map<String, Object> key, String requested) {
return GatewayModelIds.resolve(catalogService, key, requested);
}
} }
@@ -0,0 +1,99 @@
package com.moyi.controlplane.gateway;
import com.moyi.controlplane.catalog.CatalogService;
import com.moyi.controlplane.common.ApiException;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.HexFormat;
import java.util.List;
import java.util.Locale;
import java.util.Map;
/** Public model identifiers shared by the chat and media gateways. */
public final class GatewayModelIds {
private GatewayModelIds() { }
public static List<Map<String, Object>> authorizedModels(CatalogService catalog, Map<String, Object> key) {
return catalog.listModels(false).stream()
.filter(model -> catalog.keyCanInvoke(key, String.valueOf(model.get("id"))))
.toList();
}
/**
* The readable prefix lets clients that only display `id` show the model
* and group. The route-derived suffix remains the same regardless of the
* models permitted by a key, catalog ordering, or other groups being added.
*/
public static String publicId(Map<String, Object> model) {
String base = slug(firstText(model.get("modelKey"), model.get("upstreamModel"), model.get("publicName")));
String group = slug(firstText(model.get("sourceLabel"), "group"));
return (base.isBlank() ? "moyi-model" : base) + "--" + (group.isBlank() ? "group" : group)
+ "--" + routeTag(String.valueOf(model.get("id")));
}
/**
* Legacy internal IDs and unambiguous upstream model names remain usable.
* Every lookup applies the key's route permissions before returning a model.
*/
public static Map<String, Object> resolve(CatalogService catalog, Map<String, Object> key, String requested) {
if (requested == null || requested.isBlank()) return null;
List<Map<String, Object>> authorized = authorizedModels(catalog, key);
// Exact public ids win over optional bare aliases. The immutable route
// tag also keeps a client's previously imported id usable after a name
// or group label is edited in the admin console.
List<Map<String, Object>> exact = authorized.stream()
.filter(model -> requested.equals(publicId(model)) || matchesRouteTag(requested, model))
.toList();
if (exact.size() == 1) return exact.getFirst();
if (exact.size() > 1) throw ambiguous();
// Avoid treating a readable public identifier as a legacy database ID
// if an administrator happens to create a route with the same text.
if (requested.matches("[a-z0-9._-]+--[a-z0-9._-]+--[a-f0-9]{16}")) return null;
Map<String, Object> direct = catalog.activeModel(requested);
if (direct != null) {
if (!catalog.keyCanInvoke(key, String.valueOf(direct.get("id"))))
throw new ApiException("MODEL_FORBIDDEN", "当前 Key 无权调用该模型", 403);
return direct;
}
List<Map<String, Object>> aliases = authorized.stream()
.filter(model -> equalsNonBlank(requested, model.get("modelKey")) || equalsNonBlank(requested, model.get("upstreamModel")))
.toList();
if (aliases.size() > 1) throw ambiguous();
return aliases.isEmpty() ? null : aliases.getFirst();
}
private static boolean matchesRouteTag(String requested, Map<String, Object> model) {
return requested.matches("[a-z0-9._-]+--[a-z0-9._-]+--[a-f0-9]{16}")
&& requested.endsWith("--" + routeTag(String.valueOf(model.get("id"))));
}
private static ApiException ambiguous() {
return new ApiException("MODEL_AMBIGUOUS", "该模型在当前 Key 中有多个渠道分组,请使用 /v1/models 返回的完整模型 ID", 400);
}
private static boolean equalsNonBlank(String expected, Object actual) {
return actual != null && !String.valueOf(actual).isBlank() && expected.equals(String.valueOf(actual));
}
private static String firstText(Object... values) {
for (Object value : values) if (value != null && !String.valueOf(value).isBlank()) return String.valueOf(value).trim();
return "";
}
private static String slug(String value) {
String normalized = value.trim().toLowerCase(Locale.ROOT)
.replaceAll("[^a-z0-9._-]+", "-")
.replaceAll("-+", "-")
.replaceAll("(^[-.]+|[-.]+$)", "");
return normalized.length() > 96 ? normalized.substring(0, 96) : normalized;
}
private static String routeTag(String value) {
try {
return HexFormat.of().formatHex(MessageDigest.getInstance("SHA-256").digest(value.getBytes(StandardCharsets.UTF_8))).substring(0, 16);
} catch (NoSuchAlgorithmException exception) {
throw new IllegalStateException("SHA-256 is required by the Java runtime", exception);
}
}
}
@@ -6,10 +6,14 @@ import com.fasterxml.jackson.databind.ObjectMapper;
import com.moyi.controlplane.common.ApiException; import com.moyi.controlplane.common.ApiException;
import com.moyi.controlplane.security.SecretCipher; import com.moyi.controlplane.security.SecretCipher;
import java.net.URI; import java.net.URI;
import java.net.InetAddress;
import java.net.http.HttpClient; import java.net.http.HttpClient;
import java.net.http.HttpRequest; import java.net.http.HttpRequest;
import java.net.http.HttpResponse; import java.net.http.HttpResponse;
import java.time.Duration; import java.time.Duration;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Base64;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@@ -39,20 +43,35 @@ public class AsyncProviderClient {
} }
public Map<String,Object> submit(String providerId, String upstreamModel, String taskType, String prompt) { public Map<String,Object> submit(String providerId, String upstreamModel, String taskType, String prompt) {
return submit(providerId, upstreamModel, taskType, Map.of("prompt", prompt));
}
public Map<String,Object> submit(String providerId, String upstreamModel, String taskType, Map<String,Object> input) {
if (input == null) input = Map.of();
String prompt = String.valueOf(input.getOrDefault("prompt", ""));
Provider provider = provider(providerId); Provider provider = provider(providerId);
String code = provider.code().toLowerCase(java.util.Locale.ROOT); String code = provider.code().toLowerCase(java.util.Locale.ROOT);
if (code.contains("tripo") || provider.baseUrl().contains("tripo")) { if (isTripo(provider.code(), provider.baseUrl())) {
Map<String,Object> body = new LinkedHashMap<>(); Map<String,Object> body = new LinkedHashMap<>();
body.put("model", upstreamModel); body.put("model", upstreamModel);
body.put("prompt", prompt); String media = mediaUrl(input);
if (media != null) body.put("input", media);
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()) + "/generation/text-to-model", provider.nextApiKey(), body, "上游 3D 任务创建失败"); return request("POST", tripoBase(provider.baseUrl()) + (media == null ? "/generation/text-to-model" : "/generation/image-to-model"), provider.nextApiKey(), body, "上游 3D 任务创建失败");
} }
if (code.contains("byte") || code.contains("seedance") || provider.baseUrl().contains("volces.com")) { if (code.contains("byte") || code.contains("seedance") || provider.baseUrl().contains("volces.com")) {
Map<String,Object> body = Map.of("model", upstreamModel, "content", List.of(Map.of("type", "text", "text", prompt))); List<Map<String,Object>> content = new ArrayList<>();
if (!prompt.isBlank()) content.add(Map.of("type", "text", "text", prompt));
String media = mediaUrl(input);
if (media != null) content.add(Map.of("type", "image_url", "image_url", Map.of("url", media)));
Map<String,Object> body = Map.of("model", upstreamModel, "content", content);
return request("POST", trim(provider.baseUrl()) + "/contents/generations/tasks", provider.nextApiKey(), body, "上游视频任务创建失败"); return request("POST", trim(provider.baseUrl()) + "/contents/generations/tasks", provider.nextApiKey(), body, "上游视频任务创建失败");
} }
if ("openai-compatible".equalsIgnoreCase(provider.protocol())) {
return submitOpenAiCompatible(provider, upstreamModel, taskType, input);
}
throw new ApiException("ASYNC_PROVIDER_NOT_IMPLEMENTED", "该异步渠道没有可用的执行器", 501); throw new ApiException("ASYNC_PROVIDER_NOT_IMPLEMENTED", "该异步渠道没有可用的执行器", 501);
} }
@@ -60,12 +79,250 @@ public class AsyncProviderClient {
Provider provider = provider(providerId); Provider provider = provider(providerId);
String code = provider.code().toLowerCase(java.util.Locale.ROOT); String code = provider.code().toLowerCase(java.util.Locale.ROOT);
String url; String url;
if (code.contains("tripo") || provider.baseUrl().contains("tripo")) 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())) {
return pollOpenAiCompatible(provider, providerTaskId);
}
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, provider.nextApiKey(), null, "上游异步任务查询失败");
} }
/**
* OpenAI-compatible relay providers expose the standard image endpoint and
* increasingly expose the asynchronous video endpoint as well. Image
* generation completes in the request, so mark the normalized result for
* 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) {
String prompt = String.valueOf(input.getOrDefault("prompt", ""));
String media = mediaUrl(input);
String base = trim(provider.baseUrl());
if ("image".equalsIgnoreCase(taskType)) {
Map<String,Object> body = new LinkedHashMap<>();
body.put("model", model); body.put("prompt", prompt); body.put("n", 1); body.put("response_format", "url");
copyParameter(input, body, "size");
Map<String,Object> response = media == null
? request("POST", base + "/images/generations", provider.nextApiKey(), body, "上游图片任务创建失败")
: requestMultipart("POST", base + "/images/edits", provider.nextApiKey(), body, "image", media, "上游图片任务创建失败");
Map<String,Object> result = compatibleResult(response);
if (result != null) return Map.of("_moyiInlineResult", result);
return response;
}
if ("video".equalsIgnoreCase(taskType)) {
Map<String,Object> body = new LinkedHashMap<>();
body.put("model", model); body.put("prompt", prompt);
copyParameter(input, body, "seconds"); copyParameter(input, body, "size");
// The standard OpenAI video endpoint is multipart even when no
// reference file is supplied. JSON requests are rejected by the
// official API and by relays that proxy it faithfully.
try {
return requestMultipart("POST", base + "/videos", provider.nextApiKey(), body,
media == null ? null : "input_reference", media, "上游视频任务创建失败");
} catch (ApiException first) {
// A few older relays retained /videos/generations. Retry only
// on a route miss; authentication/credit errors are returned.
if (!isNotFound(first)) throw first;
return requestMultipart("POST", base + "/videos/generations", provider.nextApiKey(), body,
media == null ? null : "input_reference", media, "上游视频任务创建失败");
}
}
if ("audio".equalsIgnoreCase(taskType)) {
if (media != null) throw new ApiException("MEDIA_INPUT_UNSUPPORTED", "该语音生成接口接收文本;音频转写需使用专用转写接口", 400);
// OpenAI-compatible TTS returns an audio/* binary body. Keep the
// payload bounded before embedding it into the task result so a
// provider cannot make the database grow without limit.
Map<String,Object> body = new LinkedHashMap<>();
body.put("model", model); body.put("input", prompt);
body.put("voice", input.getOrDefault("voice", "alloy"));
body.put("response_format", input.getOrDefault("response_format", "mp3"));
copyParameter(input, body, "speed");
return Map.of("_moyiInlineResult", requestAudio("POST", base + "/audio/speech", provider.nextApiKey(), body, "上游语音任务创建失败"));
}
throw new ApiException("ASYNC_PROVIDER_NOT_IMPLEMENTED", "该异步渠道的媒体接口尚未配置", 501);
}
private void copyParameter(Map<String,Object> input, Map<String,Object> body, String name) {
Object value = input.get(name);
if (value != null) body.put(name, value);
}
private String mediaUrl(Map<String,Object> input) {
Object raw = input.get("media_url");
if (raw == null || String.valueOf(raw).isBlank()) return null;
String value = String.valueOf(raw).trim();
if (value.startsWith("file_")) return value; // Tripo file token from /v3/files
try {
URI url = URI.create(value);
if (!"https".equalsIgnoreCase(url.getScheme()) || url.getHost() == null || url.getUserInfo() != null) throw new IllegalArgumentException();
} catch (IllegalArgumentException exception) {
throw new ApiException("INVALID_MEDIA_URL", "媒体地址必须是可公开访问的 HTTPS URL", 400);
}
return value;
}
private Map<String,Object> pollOpenAiCompatible(Provider provider, String providerTaskId) {
String base = trim(provider.baseUrl());
try {
return request("GET", base + "/videos/" + encode(providerTaskId), provider.nextApiKey(), null, "上游异步任务查询失败");
} catch (ApiException first) {
if (!isNotFound(first)) throw first;
return request("GET", base + "/videos/generations/" + encode(providerTaskId), provider.nextApiKey(), null, "上游异步任务查询失败");
}
}
private boolean isNotFound(ApiException exception) {
String message = exception.getMessage();
return message != null && (message.contains("HTTP 404") || message.contains("HTTP 405"));
}
/** Normalize OpenAI image/audio response fields to the task result shape. */
private Map<String,Object> compatibleResult(Map<String,Object> response) {
Object data = response.get("data");
if (data instanceof List<?> list && !list.isEmpty()) {
Object first = list.getFirst();
if (first instanceof Map<?,?> map) {
Map<String,Object> result = new LinkedHashMap<>();
map.forEach((key, value) -> result.put(String.valueOf(key), value));
Object url = result.get("url");
if (url == null) url = result.get("audio_url");
if (url != null) result.put("url", url);
if (result.get("b64_json") != null) result.put("base64", result.get("b64_json"));
return result;
}
}
Object url = response.get("url");
if (url != null) return Map.of("url", url);
return null;
}
private Map<String,Object> requestAudio(String method, String url, String key, Map<String,Object> body, String message) {
try {
HttpRequest.Builder builder = HttpRequest.newBuilder().uri(URI.create(url)).timeout(Duration.ofSeconds(30))
.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());
if (response.statusCode() < 200 || response.statusCode() >= 300) {
String code = switch (response.statusCode()) {
case 401, 403 -> "UPSTREAM_AUTH_FAILED";
case 402 -> "UPSTREAM_CREDIT_INSUFFICIENT";
case 429 -> "UPSTREAM_RATE_LIMITED";
default -> "UPSTREAM_PROVIDER_ERROR";
};
throw new ApiException(code, message + "(HTTP " + response.statusCode() + ")", 502);
}
byte[] bytes = response.body();
if (bytes.length == 0 || bytes.length > 8 * 1024 * 1024) throw new ApiException("UPSTREAM_MEDIA_TOO_LARGE", message + "(返回音频超过 8 MiB 或为空)", 502);
String contentType = response.headers().firstValue("Content-Type").orElse("audio/mpeg").split(";", 2)[0].trim().toLowerCase(java.util.Locale.ROOT);
if (!contentType.startsWith("audio/")) throw new ApiException("UPSTREAM_INVALID_MEDIA", message + "(返回内容不是音频)", 502);
Map<String,Object> result = new LinkedHashMap<>();
result.put("base64", Base64.getEncoder().encodeToString(bytes));
result.put("mimeType", contentType);
result.put("format", String.valueOf(body.getOrDefault("response_format", "mp3")));
return result;
} catch (ApiException exception) { throw exception; }
catch (Exception exception) { throw new ApiException("UPSTREAM_PROVIDER_ERROR", message, 502); }
}
/**
* Send the file based forms used by the standard OpenAI image-edit and
* video-create endpoints. The caller supplies an HTTPS URL; the server
* fetches it with redirects disabled and a strict 8 MiB limit, then sends
* the bytes as a multipart part. This avoids putting an arbitrary URL in a
* JSON field that the upstream endpoint does not understand.
*/
private Map<String,Object> requestMultipart(String method, String url, String key, Map<String,Object> fields,
String fileField, String media, String message) {
try {
String boundary = "----moyi-" + java.util.UUID.randomUUID();
ByteArrayOutputStream out = new ByteArrayOutputStream();
for (Map.Entry<String,Object> entry : fields.entrySet()) {
multipartField(out, boundary, entry.getKey(), String.valueOf(entry.getValue()));
}
if (fileField != null && media != null) {
MediaBytes file = downloadMedia(media);
multipartFile(out, boundary, fileField, file.filename(), file.contentType(), file.bytes());
}
out.write(("--" + boundary + "--\r\n").getBytes(java.nio.charset.StandardCharsets.UTF_8));
HttpRequest request = HttpRequest.newBuilder().uri(URI.create(url)).timeout(Duration.ofSeconds(30))
.header("Authorization", "Bearer " + key)
.header("Content-Type", "multipart/form-data; boundary=" + boundary)
.POST(HttpRequest.BodyPublishers.ofByteArray(out.toByteArray())).build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
if (response.statusCode() < 200 || response.statusCode() >= 300) throw upstreamStatus(response.statusCode(), message);
Map<String,Object> parsed = mapper.readValue(response.body(), new TypeReference<>() {});
Object code = parsed.get("code");
if (parsed.containsKey("error") || code instanceof Number number && number.intValue() != 0) {
throw new ApiException("UPSTREAM_PROVIDER_ERROR", message, 502);
}
return parsed;
} catch (ApiException exception) { throw exception; }
catch (Exception exception) { throw new ApiException("UPSTREAM_PROVIDER_ERROR", message, 502); }
}
private ApiException upstreamStatus(int status, String message) {
String code = switch (status) {
case 401, 403 -> "UPSTREAM_AUTH_FAILED";
case 402 -> "UPSTREAM_CREDIT_INSUFFICIENT";
case 429 -> "UPSTREAM_RATE_LIMITED";
default -> "UPSTREAM_PROVIDER_ERROR";
};
return new ApiException(code, message + "(HTTP " + status + ")", 502);
}
private void multipartField(ByteArrayOutputStream out, String boundary, String name, String value) throws IOException {
out.write(("--" + boundary + "\r\nContent-Disposition: form-data; name=\"" + name + "\"\r\n\r\n" + value + "\r\n")
.getBytes(java.nio.charset.StandardCharsets.UTF_8));
}
private void multipartFile(ByteArrayOutputStream out, String boundary, String field, String filename,
String contentType, byte[] bytes) throws IOException {
out.write(("--" + boundary + "\r\nContent-Disposition: form-data; name=\"" + field + "\"; filename=\"" + filename
+ "\"\r\nContent-Type: " + contentType + "\r\n\r\n").getBytes(java.nio.charset.StandardCharsets.UTF_8));
out.write(bytes);
out.write("\r\n".getBytes(java.nio.charset.StandardCharsets.UTF_8));
}
private MediaBytes downloadMedia(String value) throws IOException {
URI uri = URI.create(value);
if (!"https".equalsIgnoreCase(uri.getScheme()) || uri.getHost() == null || uri.getUserInfo() != null) {
throw new ApiException("INVALID_MEDIA_URL", "媒体地址必须是可公开访问的 HTTPS URL", 400);
}
for (InetAddress address : InetAddress.getAllByName(uri.getHost())) {
if (address.isAnyLocalAddress() || address.isLoopbackAddress() || address.isLinkLocalAddress()
|| address.isSiteLocalAddress() || address.isMulticastAddress()) {
throw new ApiException("INVALID_MEDIA_URL", "媒体地址不能指向内网或本机地址", 400);
}
}
HttpRequest request = HttpRequest.newBuilder(uri).timeout(Duration.ofSeconds(15)).GET().build();
HttpResponse<byte[]> response = client.send(request, HttpResponse.BodyHandlers.ofByteArray());
if (response.statusCode() < 200 || response.statusCode() >= 300) {
throw new ApiException("MEDIA_FETCH_FAILED", "无法读取媒体输入(HTTP " + response.statusCode() + ")", 400);
}
byte[] bytes = response.body();
if (bytes.length == 0 || bytes.length > 8 * 1024 * 1024) {
throw new ApiException("MEDIA_TOO_LARGE", "媒体输入为空或超过 8 MiB", 400);
}
String contentType = response.headers().firstValue("Content-Type").orElse("application/octet-stream")
.split(";", 2)[0].trim().toLowerCase(java.util.Locale.ROOT);
if (!contentType.startsWith("image/")) {
throw new ApiException("MEDIA_TYPE_UNSUPPORTED", "图像编辑和视频参考图必须是 image/* 文件", 400);
}
String filename = "input" + extension(contentType);
return new MediaBytes(bytes, contentType, filename);
}
private String extension(String contentType) {
return switch (contentType) {
case "image/jpeg" -> ".jpg";
case "image/png" -> ".png";
case "image/webp" -> ".webp";
case "image/gif" -> ".gif";
default -> ".bin";
};
}
private record MediaBytes(byte[] bytes, String contentType, String filename) {}
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(Duration.ofSeconds(30))
@@ -96,7 +353,7 @@ public class AsyncProviderClient {
private Provider provider(String id) { private Provider provider(String id) {
try { try {
Map<String,Object> row = jdbc.queryForMap("SELECT code, 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"));
String key = ciphertext.isBlank() ? "" : cipher.decrypt(ciphertext); String key = ciphertext.isBlank() ? "" : cipher.decrypt(ciphertext);
String code = String.valueOf(row.get("code")); String code = String.valueOf(row.get("code"));
@@ -111,11 +368,19 @@ public class AsyncProviderClient {
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()) throw new ApiException("PROVIDER_NOT_CONFIGURED", "异步渠道尚未配置 API Key", 503);
return new Provider(code, 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); }
} }
private String tripoBase(String value) { return trim(value).endsWith("/v3") ? trim(value) : trim(value) + "/v3"; } private String tripoBase(String value) {
String base = trim(value);
// Tripo's current documented endpoint is openapi.tripo3d.com. Older
// seeded releases used api.tripo3d.ai; keep custom/local test hosts,
// but transparently repair those stale official hosts.
String lower = base.toLowerCase(java.util.Locale.ROOT);
if (lower.contains("api.tripo3d.ai") || lower.contains("openapi.tripo3d.ai")) base = "https://openapi.tripo3d.com";
return base.endsWith("/v3") ? base : base + "/v3";
}
private String trim(String value) { return value == null ? "" : value.replaceAll("/+$", ""); } private String trim(String value) { return value == null ? "" : value.replaceAll("/+$", ""); }
private String encode(String value) { return java.net.URLEncoder.encode(value, java.nio.charset.StandardCharsets.UTF_8); } private String encode(String value) { return java.net.URLEncoder.encode(value, java.nio.charset.StandardCharsets.UTF_8); }
private String write(Object value) throws JsonProcessingException { return mapper.writeValueAsString(value == null ? Map.of() : value); } private String write(Object value) throws JsonProcessingException { return mapper.writeValueAsString(value == null ? Map.of() : value); }
@@ -129,7 +394,7 @@ public class AsyncProviderClient {
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 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() { return apiKeys.get(Math.floorMod(cursor.getAndIncrement(), apiKeys.size())); }
} }
} }
@@ -6,6 +6,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
import com.moyi.controlplane.catalog.CatalogService; import com.moyi.controlplane.catalog.CatalogService;
import com.moyi.controlplane.common.ApiException; import com.moyi.controlplane.common.ApiException;
import com.moyi.controlplane.gateway.GatewayGuard; import com.moyi.controlplane.gateway.GatewayGuard;
import com.moyi.controlplane.gateway.GatewayModelIds;
import com.moyi.controlplane.usage.UsageAuditService; import com.moyi.controlplane.usage.UsageAuditService;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
@@ -84,10 +85,10 @@ public class AsyncTaskService {
@Transactional @Transactional
public Map<String, Object> createForKey(Map<String, Object> key, String taskType, Map<String, Object> input) { public Map<String, Object> createForKey(Map<String, Object> key, String taskType, Map<String, Object> input) {
gatewayGuard.checkRate(UUID.fromString(String.valueOf(key.get("id"))), false); gatewayGuard.checkRate(UUID.fromString(String.valueOf(key.get("id"))), false);
String modelId = String.valueOf(input.getOrDefault("model", "")); String requestedModelId = String.valueOf(input.getOrDefault("model", ""));
Map<String, Object> model = catalogService.activeModel(modelId); Map<String, Object> model = GatewayModelIds.resolve(catalogService, key, requestedModelId);
if (model == null) throw new ApiException("MODEL_NOT_FOUND", "模型不存在或已下线", 404); if (model == null) throw new ApiException("MODEL_NOT_FOUND", "模型不存在或已下线", 404);
if (!catalogService.keyCanInvoke(key, modelId)) throw new ApiException("MODEL_FORBIDDEN", "当前 Key 无权调用该模型", 403); String modelId = String.valueOf(model.get("id"));
if (!taskType.equals(model.get("type")) || !"async".equals(model.get("mode"))) throw new ApiException("NOT_ASYNC_MEDIA_MODEL", "该模型不是对应的异步媒体模型", 400); if (!taskType.equals(model.get("type")) || !"async".equals(model.get("mode"))) throw new ApiException("NOT_ASYNC_MEDIA_MODEL", "该模型不是对应的异步媒体模型", 400);
if (!"provider-mock".equals(model.get("providerId")) && providerClient == null) throw new ApiException("ASYNC_PROVIDER_NOT_IMPLEMENTED", "异步执行器未配置", 501); if (!"provider-mock".equals(model.get("providerId")) && providerClient == null) throw new ApiException("ASYNC_PROVIDER_NOT_IMPLEMENTED", "异步执行器未配置", 501);
// Serialize the queue-capacity check with task insertion for this // Serialize the queue-capacity check with task insertion for this
@@ -97,7 +98,10 @@ public class AsyncTaskService {
Integer queued = jdbc.queryForObject("SELECT COUNT(*) FROM model_tasks WHERE model_id = ? AND status IN ('queued', 'running')", Integer.class, modelId); Integer queued = jdbc.queryForObject("SELECT COUNT(*) FROM model_tasks WHERE model_id = ? AND status IN ('queued', 'running')", Integer.class, modelId);
if (queued != null && queued >= maxQueuedPerModel) throw new ApiException("TASK_QUEUE_FULL", "该模型当前排队已满,请稍后重试", 429); if (queued != null && queued >= maxQueuedPerModel) throw new ApiException("TASK_QUEUE_FULL", "该模型当前排队已满,请稍后重试", 429);
String prompt = String.valueOf(input.getOrDefault("prompt", "")).trim(); String prompt = String.valueOf(input.getOrDefault("prompt", "")).trim();
if (prompt.isBlank()) throw new ApiException("PROMPT_REQUIRED", "生成提示词不能为空"); Object mediaInput = input.get("media_url");
if (prompt.isBlank() && (mediaInput == null || String.valueOf(mediaInput).isBlank())) {
throw new ApiException("PROMPT_REQUIRED", "生成提示词或媒体输入不能为空");
}
UUID apiKeyId = UUID.fromString(String.valueOf(key.get("id"))); UUID apiKeyId = UUID.fromString(String.valueOf(key.get("id")));
UUID userId = catalogService.keyOwnerId(apiKeyId); UUID userId = catalogService.keyOwnerId(apiKeyId);
BigDecimal reservedAmount = taskCharge(model); BigDecimal reservedAmount = taskCharge(model);
@@ -112,8 +116,8 @@ public class AsyncTaskService {
OffsetDateTime now = now(); OffsetDateTime now = now();
try { try {
jdbc.update( jdbc.update(
"INSERT INTO model_tasks (id, user_id, api_key_id, model_id, task_type, provider_task_id, status, progress, prompt, reserved_amount, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?, 'queued', 0, ?, ?, ?, ?)", "INSERT INTO model_tasks (id, user_id, api_key_id, model_id, task_type, provider_task_id, status, progress, prompt, input, reserved_amount, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?, 'queued', 0, ?, CAST(? AS jsonb), ?, ?, ?)",
id, userId, apiKeyId, modelId, taskType, providerTaskId, prompt, reservedAmount, now, now id, userId, apiKeyId, modelId, taskType, providerTaskId, prompt, writeJson(input == null ? Map.of() : input), reservedAmount, now, now
); );
} catch (RuntimeException exception) { } catch (RuntimeException exception) {
releaseWallet(userId, reservedAmount); releaseWallet(userId, reservedAmount);
@@ -187,8 +191,18 @@ public class AsyncTaskService {
String.valueOf(task.get("provider_id")), String.valueOf(task.get("provider_id")),
String.valueOf(task.get("upstream_model")), String.valueOf(task.get("upstream_model")),
String.valueOf(task.get("task_type")), String.valueOf(task.get("task_type")),
String.valueOf(task.get("prompt")) taskInput(task)
); );
// Some OpenAI-compatible image/audio endpoints return the
// generated media in the submit response rather than creating
// a pollable job. Persist that result and settle the task in
// the same worker transaction.
if (upstream.get("_moyiInlineResult") instanceof Map<?,?> inline) {
Map<String,Object> result = new LinkedHashMap<>();
inline.forEach((key, value) -> result.put(String.valueOf(key), value));
finishTask(taskId, "succeeded", 100, result, null, null);
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_deadline_at=?, updated_at=? WHERE id=? AND status='running'", providerTaskId(upstream), submittedAt.plus(java.time.Duration.ofMillis(providerTimeoutMs)), submittedAt, taskId);
} catch (RuntimeException exception) { } catch (RuntimeException exception) {
@@ -207,7 +221,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 SELECT t.id, t.api_key_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
@@ -294,6 +308,18 @@ public class AsyncTaskService {
catch (JsonProcessingException exception) { return Map.of(); } catch (JsonProcessingException exception) { return Map.of(); }
} }
private Map<String,Object> taskInput(Map<String,Object> task) {
Object raw = task.get("input");
Map<String,Object> input = new LinkedHashMap<>(readMap(raw == null ? "{}" : String.valueOf(raw)));
// V30 adds input for new tasks; retain the prompt from pre-V30 rows
// and from workers upgraded while jobs were queued.
if (!input.containsKey("prompt") || String.valueOf(input.get("prompt")).isBlank()) {
Object prompt = task.get("prompt");
if (prompt != null && !String.valueOf(prompt).isBlank()) input.put("prompt", prompt);
}
return 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) {
@@ -386,13 +412,28 @@ public class AsyncTaskService {
map.forEach((k,v)->value.put(String.valueOf(k),v)); map.forEach((k,v)->value.put(String.valueOf(k),v));
copyAlias(value, "model_url", "modelUrl"); copyAlias(value, "model_url", "modelUrl");
copyAlias(value, "rendered_image_url", "previewUrl"); copyAlias(value, "rendered_image_url", "previewUrl");
copyAlias(value, "generated_image_url", "imageUrl");
copyAlias(value, "video_url", "videoUrl"); copyAlias(value, "video_url", "videoUrl");
copyAlias(value, "audio_url", "audioUrl");
copyAlias(value, "image_url", "imageUrl");
if (value.get("url") == null && value.get("modelUrl") != null) value.put("url", value.get("modelUrl")); if (value.get("url") == null && value.get("modelUrl") != null) value.put("url", value.get("modelUrl"));
if (value.get("url") == null && value.get("imageUrl") != null) value.put("url", value.get("imageUrl"));
if (value.get("url") == null && value.get("videoUrl") != null) value.put("url", value.get("videoUrl")); if (value.get("url") == null && value.get("videoUrl") != null) value.put("url", value.get("videoUrl"));
if (value.get("url") == null && value.get("audioUrl") != null) value.put("url", value.get("audioUrl"));
if (data != null && data.get("credits_consumed") != null) value.put("creditsConsumed", data.get("credits_consumed")); if (data != null && data.get("credits_consumed") != null) value.put("creditsConsumed", data.get("credits_consumed"));
return value; return value;
} }
if (raw instanceof String value && !value.isBlank()) return Map.of("url", value); if (raw instanceof String value && !value.isBlank()) return Map.of("url", value);
// OpenAI-compatible video jobs return the media URL at the top level
// (or in a nested video object) rather than under `output`.
Object directUrl = response.get("url");
if (directUrl == null) directUrl = response.get("video_url");
if (directUrl == null) directUrl = response.get("image_url");
if (directUrl == null) directUrl = response.get("audio_url");
if (directUrl == null && response.get("video") instanceof Map<?,?> video) directUrl = video.get("url");
if (directUrl == null && response.get("image") instanceof Map<?,?> image) directUrl = image.get("url");
if (directUrl == null && response.get("audio") instanceof Map<?,?> audio) directUrl = audio.get("url");
if (directUrl != null && !String.valueOf(directUrl).isBlank()) return Map.of("url", directUrl);
return null; return null;
} }
@@ -21,14 +21,17 @@ public class TaskController {
this.tasks = tasks; this.tasks = tasks;
} }
@PostMapping("/3d/tasks") @PostMapping("/{type:3d|image|audio}/tasks")
public ResponseEntity<Map<String, Object>> create3d( public ResponseEntity<Map<String, Object>> create3d(
@org.springframework.web.bind.annotation.PathVariable(required = false) String type,
@RequestHeader(value = "Authorization", required = false) String authorization, @RequestHeader(value = "Authorization", required = false) String authorization,
@RequestBody Map<String, Object> input @RequestBody Map<String, Object> input
) { ) {
return ResponseEntity.status(HttpStatus.ACCEPTED).body(Map.of("task", tasks.create(authorization, "3d", input))); String taskType = type == null ? String.valueOf(input == null ? "3d" : input.getOrDefault("taskType", "3d")) : type;
return ResponseEntity.status(HttpStatus.ACCEPTED).body(Map.of("task", tasks.create(authorization, taskType, input)));
} }
@PostMapping("/video/tasks") @PostMapping("/video/tasks")
public ResponseEntity<Map<String, Object>> createVideo( public ResponseEntity<Map<String, Object>> createVideo(
@RequestHeader(value = "Authorization", required = false) String authorization, @RequestHeader(value = "Authorization", required = false) String authorization,
@@ -0,0 +1,6 @@
-- Async image/audio relays use the same durable queue and settlement path as
-- video/3D tasks. Keep the check explicit so unsupported task types fail at
-- the API boundary rather than being persisted as an unpollable job.
ALTER TABLE model_tasks DROP CONSTRAINT IF EXISTS model_tasks_task_type_check;
ALTER TABLE model_tasks ADD CONSTRAINT model_tasks_task_type_check
CHECK (task_type IN ('3d', 'video', 'image', 'audio'));
@@ -0,0 +1 @@
ALTER TABLE model_tasks ADD COLUMN IF NOT EXISTS input jsonb NOT NULL DEFAULT '{}'::jsonb;
@@ -67,7 +67,7 @@ class GatewayDeploymentTest {
} }
@Test @Test
void modelsKeepRouteIdAndExposeDisplayMetadata() { void modelsExposeReadableIdAndKeepRouteIdForDiagnostics() {
CatalogService catalog = mock(CatalogService.class); CatalogService catalog = mock(CatalogService.class);
GatewayGuard guard = mock(GatewayGuard.class); GatewayGuard guard = mock(GatewayGuard.class);
Map<String, Object> key = Map.of("id", UUID.randomUUID().toString()); Map<String, Object> key = Map.of("id", UUID.randomUUID().toString());
@@ -90,7 +90,9 @@ class GatewayDeploymentTest {
Map<String, Object> response = controller.models("Bearer test-key"); Map<String, Object> response = controller.models("Bearer test-key");
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
Map<String, Object> item = ((List<Map<String, Object>>) response.get("data")).getFirst(); Map<String, Object> item = ((List<Map<String, Object>>) response.get("data")).getFirst();
assertEquals("route-1", item.get("id")); assertEquals("deepseek-v4-flash--apihub--" + tag("route-1"), item.get("id"));
assertEquals("route-1", item.get("moyi_route_id"));
assertEquals("DeepSeek V4 Flash", item.get("name"));
assertEquals("deepseek-v4-flash", item.get("model_key")); assertEquals("deepseek-v4-flash", item.get("model_key"));
assertEquals("DeepSeek V4 Flash", item.get("public_name")); assertEquals("DeepSeek V4 Flash", item.get("public_name"));
assertEquals("DeepSeek", item.get("vendor_name")); assertEquals("DeepSeek", item.get("vendor_name"));
@@ -98,6 +100,88 @@ class GatewayDeploymentTest {
assertEquals("deepseek-v4-flash", item.get("upstream_model")); assertEquals("deepseek-v4-flash", item.get("upstream_model"));
} }
@Test
void duplicateModelKeysAreDisambiguatedBySourceGroup() {
var first = Map.<String, Object>of(
"id", "route-a", "providerId", "provider-a", "modelKey", "gpt-5.5",
"publicName", "GPT-5.5", "sourceLabel", "ApiHub 国内", "upstreamModel", "gpt-5.5");
var second = Map.<String, Object>of(
"id", "route-b", "providerId", "provider-b", "modelKey", "gpt-5.5",
"publicName", "GPT-5.5", "sourceLabel", "APIPro", "upstreamModel", "gpt-5.5");
var visible = List.of(first, second);
assertEquals("gpt-5.5--apihub--" + tag("route-a"), GatewayModelIds.publicId(first));
assertEquals("gpt-5.5--apipro--" + tag("route-b"), GatewayModelIds.publicId(second));
}
@Test
void readableIdResolvesOnlyToAnAuthorizedRoute() {
CatalogService catalog = mock(CatalogService.class);
GatewayGuard guard = mock(GatewayGuard.class);
Map<String, Object> key = Map.of("id", UUID.randomUUID().toString());
Map<String, Object> route = Map.of(
"id", "route-readable", "providerId", "provider-a", "modelKey", "glm-5",
"publicName", "GLM-5", "sourceLabel", "ApiHub 国内", "upstreamModel", "glm-5");
when(catalog.activeModel("glm-5--apihub")).thenReturn(null);
when(catalog.listModels(false)).thenReturn(List.of(route));
when(catalog.keyCanInvoke(key, "route-readable")).thenReturn(true);
GatewayController controller = new GatewayController(catalog, mock(OpenAiCompatibleProvider.class), guard, mock(ChatBillingService.class), new ObjectMapper());
assertSame(route, controller.resolveModel(key, "glm-5--apihub--" + tag("route-readable")));
verify(catalog).keyCanInvoke(key, "route-readable");
}
@Test
void legacyRouteIdRemainsCompatibleAndUnauthorizedRouteIsRejected() {
CatalogService catalog = mock(CatalogService.class);
Map<String, Object> key = Map.of("id", UUID.randomUUID().toString());
Map<String, Object> route = Map.of("id", "route-legacy", "modelKey", "gpt-5", "sourceLabel", "APIPro");
when(catalog.listModels(false)).thenReturn(List.of());
when(catalog.activeModel("route-legacy")).thenReturn(route);
when(catalog.keyCanInvoke(key, "route-legacy")).thenReturn(true);
assertSame(route, GatewayModelIds.resolve(catalog, key, "route-legacy"));
Map<String, Object> forbidden = Map.of("id", "route-forbidden", "modelKey", "gpt-5", "sourceLabel", "APIPro");
when(catalog.activeModel("route-forbidden")).thenReturn(forbidden);
when(catalog.keyCanInvoke(key, "route-forbidden")).thenReturn(false);
ApiException error = assertThrows(ApiException.class, () -> GatewayModelIds.resolve(catalog, key, "route-forbidden"));
assertEquals("MODEL_FORBIDDEN", error.getCode());
assertEquals(403, error.getStatus());
}
@Test
void routeTagKeepsPreviouslyImportedIdUsableAfterRename() {
CatalogService catalog = mock(CatalogService.class);
Map<String, Object> key = Map.of("id", UUID.randomUUID().toString());
Map<String, Object> renamed = Map.of(
"id", "route-renamed", "modelKey", "new-model-key", "publicName", "New Model",
"sourceLabel", "New Group", "upstreamModel", "new-upstream");
String oldPublicId = "old-model--old-group--" + tag("route-renamed");
when(catalog.listModels(false)).thenReturn(List.of(renamed));
when(catalog.keyCanInvoke(key, "route-renamed")).thenReturn(true);
assertSame(renamed, GatewayModelIds.resolve(catalog, key, oldPublicId));
verify(catalog, never()).activeModel(oldPublicId);
}
@Test
void legacyAliasWithMultipleAuthorizedRoutesIsRejectedAsAmbiguous() {
CatalogService catalog = mock(CatalogService.class);
Map<String, Object> key = Map.of("id", UUID.randomUUID().toString());
Map<String, Object> first = Map.of("id", "route-a", "modelKey", "same-model", "upstreamModel", "same-model", "sourceLabel", "A");
Map<String, Object> second = Map.of("id", "route-b", "modelKey", "same-model", "upstreamModel", "same-model", "sourceLabel", "B");
when(catalog.listModels(false)).thenReturn(List.of(first, second));
when(catalog.activeModel("same-model")).thenReturn(null);
when(catalog.keyCanInvoke(key, "route-a")).thenReturn(true);
when(catalog.keyCanInvoke(key, "route-b")).thenReturn(true);
ApiException error = assertThrows(ApiException.class, () -> GatewayModelIds.resolve(catalog, key, "same-model"));
assertEquals("MODEL_AMBIGUOUS", error.getCode());
assertEquals(400, error.getStatus());
}
private static String tag(String route) {
try {
return java.util.HexFormat.of().formatHex(java.security.MessageDigest.getInstance("SHA-256").digest(route.getBytes(java.nio.charset.StandardCharsets.UTF_8))).substring(0, 16);
} catch (java.security.NoSuchAlgorithmException exception) { throw new AssertionError(exception); }
}
@Test @Test
void invalidRedisPrefixIsRejected() { void invalidRedisPrefixIsRejected() {
assertThrows(IllegalArgumentException.class, () -> new GatewayGuard(mock(StringRedisTemplate.class), 60, 300, 4, "")); assertThrows(IllegalArgumentException.class, () -> new GatewayGuard(mock(StringRedisTemplate.class), 60, 300, 4, ""));
@@ -2,6 +2,7 @@ package com.moyi.controlplane.task;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq; import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
@@ -43,6 +44,101 @@ class AsyncProviderClientTest {
} }
} }
@Test
void tripoUsesCurrentOfficialV3EndpointAndBearerAuth() throws Exception {
HttpServer server = HttpServer.create(new InetSocketAddress("127.0.0.1", 0), 0);
server.createContext("/v3/generation/text-to-model", exchange -> {
assertEquals("Bearer test-key", exchange.getRequestHeaders().getFirst("Authorization"));
byte[] body = "{\"code\":0,\"data\":{\"task_id\":\"task-1\"}}".getBytes(StandardCharsets.UTF_8);
exchange.sendResponseHeaders(200, body.length);
try (var output = exchange.getResponseBody()) { output.write(body); }
});
server.start();
try {
AsyncProviderClient client = clientFor("http://127.0.0.1:" + server.getAddress().getPort());
Map<String,Object> result = client.submit("provider-vast", "v3.1-20260211", "3d", "chair");
assertEquals("task-1", ((Map<?,?>) result.get("data")).get("task_id"));
} finally {
server.stop(0);
}
}
@Test
void tripoImageInputUsesDocumentedInputField() throws Exception {
HttpServer server = HttpServer.create(new InetSocketAddress("127.0.0.1", 0), 0);
server.createContext("/v3/generation/image-to-model", exchange -> {
String body = new String(exchange.getRequestBody().readAllBytes(), StandardCharsets.UTF_8);
org.junit.jupiter.api.Assertions.assertTrue(body.contains("\"input\":\"https://example.test/image.png\""));
org.junit.jupiter.api.Assertions.assertFalse(body.contains("\"image\":\""));
byte[] response = "{\"code\":0,\"data\":{\"task_id\":\"task-image\"}}".getBytes(StandardCharsets.UTF_8);
exchange.sendResponseHeaders(200, response.length);
try (var output = exchange.getResponseBody()) { output.write(response); }
});
server.start();
try {
AsyncProviderClient client = clientFor("http://127.0.0.1:" + server.getAddress().getPort());
Map<String,Object> result = client.submit("provider-vast", "v3.1-20260211", "3d", Map.of("prompt", "chair", "media_url", "https://example.test/image.png"));
assertEquals("task-image", ((Map<?,?>) result.get("data")).get("task_id"));
} finally {
server.stop(0);
}
}
@Test
void openAiCompatibleSpeechStoresBoundedBinaryAudio() throws Exception {
HttpServer server = HttpServer.create(new InetSocketAddress("127.0.0.1", 0), 0);
server.createContext("/audio/speech", exchange -> {
String body = new String(exchange.getRequestBody().readAllBytes(), StandardCharsets.UTF_8);
org.junit.jupiter.api.Assertions.assertTrue(body.contains("\"voice\":\"alloy\""));
exchange.getResponseHeaders().set("Content-Type", "audio/mpeg");
byte[] audio = "fake-mp3".getBytes(StandardCharsets.UTF_8);
exchange.sendResponseHeaders(200, audio.length);
try (var output = exchange.getResponseBody()) { output.write(audio); }
});
server.start();
try {
JdbcTemplate jdbc = mock(JdbcTemplate.class);
SecretCipher cipher = new SecretCipher("test-encryption");
when(jdbc.queryForMap(any(String.class), eq("provider-audio"))).thenReturn(Map.of(
"code", "relay-audio", "protocol", "openai-compatible", "base_url", "http://127.0.0.1:" + server.getAddress().getPort(), "credential_ciphertext", cipher.encrypt("test-key")
));
AsyncProviderClient client = new AsyncProviderClient(jdbc, cipher, new ObjectMapper(), mock(Environment.class));
Map<String,Object> result = client.submit("provider-audio", "tts-1", "audio", Map.of("prompt", "hello"));
Map<?,?> inline = (Map<?,?>) result.get("_moyiInlineResult");
assertEquals("audio/mpeg", inline.get("mimeType"));
assertEquals("ZmFrZS1tcDM=", inline.get("base64"));
} finally {
server.stop(0);
}
}
@Test
void openAiCompatibleVideoUsesMultipartRequest() throws Exception {
HttpServer server = HttpServer.create(new InetSocketAddress("127.0.0.1", 0), 0);
server.createContext("/videos", exchange -> {
assertTrue(exchange.getRequestHeaders().getFirst("Content-Type").startsWith("multipart/form-data; boundary="));
String body = new String(exchange.getRequestBody().readAllBytes(), StandardCharsets.UTF_8);
assertTrue(body.contains("name=\"model\""));
assertTrue(body.contains("sora-2"));
byte[] response = "{\"id\":\"video-1\",\"status\":\"queued\"}".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", cipher.encrypt("test-key")
));
AsyncProviderClient client = new AsyncProviderClient(jdbc, cipher, new ObjectMapper(), mock(Environment.class));
Map<String,Object> result = client.submit("provider-video", "sora-2", "video", Map.of("prompt", "a cat"));
assertEquals("video-1", result.get("id"));
} 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 {
@@ -70,7 +166,7 @@ class AsyncProviderClientTest {
JdbcTemplate jdbc = mock(JdbcTemplate.class); JdbcTemplate jdbc = mock(JdbcTemplate.class);
SecretCipher cipher = new SecretCipher("test-encryption"); SecretCipher cipher = new SecretCipher("test-encryption");
when(jdbc.queryForMap(any(String.class), eq("provider-vast"))).thenReturn(Map.of( when(jdbc.queryForMap(any(String.class), eq("provider-vast"))).thenReturn(Map.of(
"code", "vast-tripo", "base_url", baseUrl, "credential_ciphertext", cipher.encrypt("test-key") "code", "vast-tripo", "protocol", "http", "base_url", baseUrl, "credential_ciphertext", cipher.encrypt("test-key")
)); ));
return new AsyncProviderClient(jdbc, cipher, new ObjectMapper(), mock(Environment.class)); return new AsyncProviderClient(jdbc, cipher, new ObjectMapper(), mock(Environment.class));
} }
Binary file not shown.
Binary file not shown.
Binary file not shown.
+2 -2
View File
@@ -19,7 +19,7 @@ try {
$env:VITE_AUTH_MODE = 'remote' $env:VITE_AUTH_MODE = 'remote'
& node node_modules/vue-tsc/bin/vue-tsc.js --noEmit & node node_modules/vue-tsc/bin/vue-tsc.js --noEmit
if ($LASTEXITCODE -ne 0) { throw 'Vue type check failed.' } if ($LASTEXITCODE -ne 0) { throw 'Vue type check failed.' }
& node --experimental-strip-types --test tests/chat-stream.test.mjs tests/model-catalog.test.mjs tests/pricing.test.mjs & node --experimental-strip-types --test tests/chat-stream.test.mjs tests/model-catalog.test.mjs tests/pricing.test.mjs tests/media.test.mjs
if ($LASTEXITCODE -ne 0) { throw 'Frontend verification failed.' } if ($LASTEXITCODE -ne 0) { throw 'Frontend verification failed.' }
& node build-release.mjs & node build-release.mjs
if ($LASTEXITCODE -ne 0) { throw 'Vue build failed.' } if ($LASTEXITCODE -ne 0) { throw 'Vue build failed.' }
@@ -60,7 +60,7 @@ $manifest = [ordered]@{
javaRelease = 21 javaRelease = 21
database = 'PostgreSQL 15' database = 'PostgreSQL 15'
redis = 'Tair standard' redis = 'Tair standard'
checks = @('maven-clean-verify', 'vue-tsc', 'frontend-stream-tests', 'frontend-catalog-tests', 'frontend-pricing-tests', 'vite-build') checks = @('maven-clean-verify', 'vue-tsc', 'frontend-stream-tests', 'frontend-catalog-tests', 'frontend-pricing-tests', 'frontend-media-tests', 'vite-build')
sha256 = $hashes sha256 = $hashes
} }
[IO.File]::WriteAllText((Join-Path $staging 'release-manifest.json'), ($manifest | ConvertTo-Json -Depth 5), [Text.UTF8Encoding]::new($false)) [IO.File]::WriteAllText((Join-Path $staging 'release-manifest.json'), ($manifest | ConvertTo-Json -Depth 5), [Text.UTF8Encoding]::new($false))