diff --git a/control-plane-java/src/main/java/com/moyi/controlplane/catalog/CatalogService.java b/control-plane-java/src/main/java/com/moyi/controlplane/catalog/CatalogService.java index 1983ff2..d732e01 100644 --- a/control-plane-java/src/main/java/com/moyi/controlplane/catalog/CatalogService.java +++ b/control-plane-java/src/main/java/com/moyi/controlplane/catalog/CatalogService.java @@ -6,6 +6,7 @@ import com.fasterxml.jackson.databind.ObjectMapper; import com.moyi.controlplane.auth.AuthService; import com.moyi.controlplane.auth.AuthService.UserView; import com.moyi.controlplane.common.ApiException; +import com.moyi.controlplane.gateway.GatewayModelIds; import com.moyi.controlplane.security.SecretCipher; import java.nio.charset.StandardCharsets; import java.security.MessageDigest; @@ -49,7 +50,7 @@ public class CatalogService { public List> 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'"); - 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> listDefinitions(boolean includeDisabled) { @@ -453,12 +454,12 @@ public class CatalogService { } private Map 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; } } private Map 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; } } @@ -514,9 +515,19 @@ public class CatalogService { putIfNotNull(model, "priority", resultSet.getObject("priority")); putIfNotNull(model, "maxConcurrency", resultSet.getObject("max_concurrency")); model.put("status", resultSet.getString("status")); - model.put("executionReady", !"async".equals(resultSet.getString("call_mode")) - || List.of("provider-mock", "provider-vast", "provider-bytedance").contains(resultSet.getString("provider_id"))); + String providerCode = resultSet.getString("provider_code"); + 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"))); + // 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)); return model; } diff --git a/control-plane-java/src/main/java/com/moyi/controlplane/gateway/GatewayController.java b/control-plane-java/src/main/java/com/moyi/controlplane/gateway/GatewayController.java index 498cc3e..bbbe41f 100644 --- a/control-plane-java/src/main/java/com/moyi/controlplane/gateway/GatewayController.java +++ b/control-plane-java/src/main/java/com/moyi/controlplane/gateway/GatewayController.java @@ -41,16 +41,16 @@ public class GatewayController { public Map models(@RequestHeader(value = "Authorization", required = false) String authorization) { Map key = requireKey(authorization); guard.checkRate(UUID.fromString(String.valueOf(key.get("id"))), true); - List> data = catalogService.listModels(false).stream() - .filter(model -> catalogService.keyCanInvoke(key, String.valueOf(model.get("id")))) + List> visibleModels = GatewayModelIds.authorizedModels(catalogService, key); + List> data = visibleModels.stream() .map(model -> { Map 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("owned_by", model.get("providerId")); - // Keep the OpenAI-compatible id as the exact catalog route UUID. - // These descriptive fields let clients show the logical model and - // source group without guessing that modelKey is a callable id. + item.put("moyi_route_id", model.get("id")); item.put("model_key", model.get("modelKey")); item.put("public_name", model.get("publicName")); item.put("vendor_name", model.get("vendorName")); @@ -79,9 +79,8 @@ public class GatewayController { Map input, String idempotencyKey, HttpServletResponse servletResponse) { long startedAt = System.currentTimeMillis(); String modelId = String.valueOf(input.getOrDefault("model", "")); - Map model = catalogService.activeModel(modelId); - 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); + Map model = resolveModel(key, modelId); + 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); String providerId=String.valueOf(model.get("providerId")); boolean mock="provider-mock".equals(providerId); @@ -96,7 +95,7 @@ public class GatewayController { try { catalogService.touchKey(key); 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; } Map 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 model, Map input, ChatBillingService.Ticket ticket, HttpServletResponse response, long started, boolean mock) throws IOException { - String modelId=model.get("id").toString(); + private void stream(Map model, String modelId, Map input, ChatBillingService.Ticket ticket, HttpServletResponse response, long started, boolean mock) throws IOException { if(mock) { var body=mockResponse(modelId,input); prepareStream(response); @@ -228,4 +226,13 @@ public class GatewayController { }).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 resolveModel(Map key, String requested) { + return GatewayModelIds.resolve(catalogService, key, requested); + } + } diff --git a/control-plane-java/src/main/java/com/moyi/controlplane/gateway/GatewayModelIds.java b/control-plane-java/src/main/java/com/moyi/controlplane/gateway/GatewayModelIds.java new file mode 100644 index 0000000..859192e --- /dev/null +++ b/control-plane-java/src/main/java/com/moyi/controlplane/gateway/GatewayModelIds.java @@ -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> authorizedModels(CatalogService catalog, Map 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 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 resolve(CatalogService catalog, Map key, String requested) { + if (requested == null || requested.isBlank()) return null; + List> 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> 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 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> 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 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); + } + } +} diff --git a/control-plane-java/src/main/java/com/moyi/controlplane/task/AsyncProviderClient.java b/control-plane-java/src/main/java/com/moyi/controlplane/task/AsyncProviderClient.java index 3cccd56..7eb962c 100644 --- a/control-plane-java/src/main/java/com/moyi/controlplane/task/AsyncProviderClient.java +++ b/control-plane-java/src/main/java/com/moyi/controlplane/task/AsyncProviderClient.java @@ -6,10 +6,14 @@ import com.fasterxml.jackson.databind.ObjectMapper; import com.moyi.controlplane.common.ApiException; import com.moyi.controlplane.security.SecretCipher; import java.net.URI; +import java.net.InetAddress; import java.net.http.HttpClient; import java.net.http.HttpRequest; import java.net.http.HttpResponse; import java.time.Duration; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.util.Base64; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; @@ -39,20 +43,35 @@ public class AsyncProviderClient { } public Map submit(String providerId, String upstreamModel, String taskType, String prompt) { + return submit(providerId, upstreamModel, taskType, Map.of("prompt", prompt)); + } + + public Map submit(String providerId, String upstreamModel, String taskType, Map input) { + if (input == null) input = Map.of(); + String prompt = String.valueOf(input.getOrDefault("prompt", "")); Provider provider = provider(providerId); String code = provider.code().toLowerCase(java.util.Locale.ROOT); - if (code.contains("tripo") || provider.baseUrl().contains("tripo")) { + if (isTripo(provider.code(), provider.baseUrl())) { Map body = new LinkedHashMap<>(); 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("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")) { - Map body = Map.of("model", upstreamModel, "content", List.of(Map.of("type", "text", "text", prompt))); + List> 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 body = Map.of("model", upstreamModel, "content", content); 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); } @@ -60,12 +79,250 @@ public class AsyncProviderClient { Provider provider = provider(providerId); String code = provider.code().toLowerCase(java.util.Locale.ROOT); 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 ("openai-compatible".equalsIgnoreCase(provider.protocol())) { + return pollOpenAiCompatible(provider, providerTaskId); + } else throw new ApiException("ASYNC_PROVIDER_NOT_IMPLEMENTED", "该异步渠道没有可用的执行器", 501); 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 submitOpenAiCompatible(Provider provider, String model, String taskType, Map input) { + String prompt = String.valueOf(input.getOrDefault("prompt", "")); + String media = mediaUrl(input); + String base = trim(provider.baseUrl()); + if ("image".equalsIgnoreCase(taskType)) { + Map 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 response = media == null + ? request("POST", base + "/images/generations", provider.nextApiKey(), body, "上游图片任务创建失败") + : requestMultipart("POST", base + "/images/edits", provider.nextApiKey(), body, "image", media, "上游图片任务创建失败"); + Map result = compatibleResult(response); + if (result != null) return Map.of("_moyiInlineResult", result); + return response; + } + if ("video".equalsIgnoreCase(taskType)) { + Map 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 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 input, Map body, String name) { + Object value = input.get(name); + if (value != null) body.put(name, value); + } + + private String mediaUrl(Map 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 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 compatibleResult(Map response) { + Object data = response.get("data"); + if (data instanceof List list && !list.isEmpty()) { + Object first = list.getFirst(); + if (first instanceof Map map) { + Map 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 requestAudio(String method, String url, String key, Map 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 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 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 requestMultipart(String method, String url, String key, Map fields, + String fileField, String media, String message) { + try { + String boundary = "----moyi-" + java.util.UUID.randomUUID(); + ByteArrayOutputStream out = new ByteArrayOutputStream(); + for (Map.Entry 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 response = client.send(request, HttpResponse.BodyHandlers.ofString()); + if (response.statusCode() < 200 || response.statusCode() >= 300) throw upstreamStatus(response.statusCode(), message); + Map 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 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 request(String method, String url, String key, Map body, String message) { try { 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) { try { - Map row = jdbc.queryForMap("SELECT code, base_url, credential_ciphertext FROM model_providers WHERE id=? AND status='active' AND deleted_at IS NULL", id); + Map 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 key = ciphertext.isBlank() ? "" : cipher.decrypt(ciphertext); String code = String.valueOf(row.get("code")); @@ -111,11 +368,19 @@ public class AsyncProviderClient { addEnvironmentKeys(keys, "MOYI_TRIPO_API_KEY"); } 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); } } - 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 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); } @@ -129,7 +394,7 @@ public class AsyncProviderClient { String normalizedUrl = baseUrl == null ? "" : baseUrl.toLowerCase(java.util.Locale.ROOT); return normalizedCode.contains("tripo") || normalizedUrl.contains("tripo3d"); } - private record Provider(String code, String baseUrl, List apiKeys, AtomicInteger cursor) { + private record Provider(String code, String protocol, String baseUrl, List apiKeys, AtomicInteger cursor) { private String nextApiKey() { return apiKeys.get(Math.floorMod(cursor.getAndIncrement(), apiKeys.size())); } } } diff --git a/control-plane-java/src/main/java/com/moyi/controlplane/task/AsyncTaskService.java b/control-plane-java/src/main/java/com/moyi/controlplane/task/AsyncTaskService.java index 7f1a311..b8f3586 100644 --- a/control-plane-java/src/main/java/com/moyi/controlplane/task/AsyncTaskService.java +++ b/control-plane-java/src/main/java/com/moyi/controlplane/task/AsyncTaskService.java @@ -6,6 +6,7 @@ import com.fasterxml.jackson.databind.ObjectMapper; import com.moyi.controlplane.catalog.CatalogService; import com.moyi.controlplane.common.ApiException; import com.moyi.controlplane.gateway.GatewayGuard; +import com.moyi.controlplane.gateway.GatewayModelIds; import com.moyi.controlplane.usage.UsageAuditService; import java.sql.ResultSet; import java.sql.SQLException; @@ -84,10 +85,10 @@ public class AsyncTaskService { @Transactional public Map createForKey(Map key, String taskType, Map input) { gatewayGuard.checkRate(UUID.fromString(String.valueOf(key.get("id"))), false); - String modelId = String.valueOf(input.getOrDefault("model", "")); - Map model = catalogService.activeModel(modelId); + String requestedModelId = String.valueOf(input.getOrDefault("model", "")); + Map model = GatewayModelIds.resolve(catalogService, key, requestedModelId); 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 (!"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 @@ -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); if (queued != null && queued >= maxQueuedPerModel) throw new ApiException("TASK_QUEUE_FULL", "该模型当前排队已满,请稍后重试", 429); 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 userId = catalogService.keyOwnerId(apiKeyId); BigDecimal reservedAmount = taskCharge(model); @@ -112,8 +116,8 @@ public class AsyncTaskService { OffsetDateTime now = now(); try { 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, ?, ?, ?, ?)", - id, userId, apiKeyId, modelId, taskType, providerTaskId, prompt, reservedAmount, now, now + "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, writeJson(input == null ? Map.of() : input), reservedAmount, now, now ); } catch (RuntimeException exception) { releaseWallet(userId, reservedAmount); @@ -187,8 +191,18 @@ public class AsyncTaskService { String.valueOf(task.get("provider_id")), String.valueOf(task.get("upstream_model")), 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 result = new LinkedHashMap<>(); + inline.forEach((key, value) -> result.put(String.valueOf(key), value)); + finishTask(taskId, "succeeded", 100, result, null, null); + continue; + } OffsetDateTime submittedAt = now(); jdbc.update("UPDATE model_tasks SET provider_task_id=?, provider_deadline_at=?, updated_at=? WHERE id=? AND status='running'", providerTaskId(upstream), submittedAt.plus(java.time.Duration.ofMillis(providerTimeoutMs)), submittedAt, taskId); } catch (RuntimeException exception) { @@ -207,7 +221,7 @@ public class AsyncTaskService { // workers reliably. jdbc.queryForList("SELECT pg_advisory_xact_lock(hashtextextended('moyi:async-dispatch', 0))"); 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 JOIN model_catalog m ON m.id=t.model_id JOIN model_providers p ON p.id=m.provider_id @@ -294,6 +308,18 @@ public class AsyncTaskService { catch (JsonProcessingException exception) { return Map.of(); } } + private Map taskInput(Map task) { + Object raw = task.get("input"); + Map 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 String providerTaskId(Map response) { @@ -386,13 +412,28 @@ public class AsyncTaskService { map.forEach((k,v)->value.put(String.valueOf(k),v)); copyAlias(value, "model_url", "modelUrl"); copyAlias(value, "rendered_image_url", "previewUrl"); + copyAlias(value, "generated_image_url", "imageUrl"); copyAlias(value, "video_url", "videoUrl"); + copyAlias(value, "audio_url", "audioUrl"); + copyAlias(value, "image_url", "imageUrl"); if (value.get("url") == null && value.get("modelUrl") != null) value.put("url", value.get("modelUrl")); + if (value.get("url") == null && value.get("imageUrl") != null) value.put("url", value.get("imageUrl")); if (value.get("url") == null && value.get("videoUrl") != null) value.put("url", value.get("videoUrl")); + if (value.get("url") == null && value.get("audioUrl") != null) value.put("url", value.get("audioUrl")); if (data != null && data.get("credits_consumed") != null) value.put("creditsConsumed", data.get("credits_consumed")); return value; } if (raw instanceof String value && !value.isBlank()) return Map.of("url", value); + // OpenAI-compatible video jobs return the media URL at the top level + // (or in a nested video object) rather than under `output`. + Object directUrl = response.get("url"); + if (directUrl == null) directUrl = response.get("video_url"); + if (directUrl == null) directUrl = response.get("image_url"); + if (directUrl == null) directUrl = response.get("audio_url"); + if (directUrl == null && response.get("video") instanceof Map video) directUrl = video.get("url"); + if (directUrl == null && response.get("image") instanceof Map image) directUrl = image.get("url"); + if (directUrl == null && response.get("audio") instanceof Map audio) directUrl = audio.get("url"); + if (directUrl != null && !String.valueOf(directUrl).isBlank()) return Map.of("url", directUrl); return null; } diff --git a/control-plane-java/src/main/java/com/moyi/controlplane/task/TaskController.java b/control-plane-java/src/main/java/com/moyi/controlplane/task/TaskController.java index c662282..4af858e 100644 --- a/control-plane-java/src/main/java/com/moyi/controlplane/task/TaskController.java +++ b/control-plane-java/src/main/java/com/moyi/controlplane/task/TaskController.java @@ -21,14 +21,17 @@ public class TaskController { this.tasks = tasks; } - @PostMapping("/3d/tasks") + @PostMapping("/{type:3d|image|audio}/tasks") public ResponseEntity> create3d( + @org.springframework.web.bind.annotation.PathVariable(required = false) String type, @RequestHeader(value = "Authorization", required = false) String authorization, @RequestBody Map 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") public ResponseEntity> createVideo( @RequestHeader(value = "Authorization", required = false) String authorization, diff --git a/control-plane-java/src/main/resources/db/migration/V29__async_media_task_types.sql b/control-plane-java/src/main/resources/db/migration/V29__async_media_task_types.sql new file mode 100644 index 0000000..5b68957 --- /dev/null +++ b/control-plane-java/src/main/resources/db/migration/V29__async_media_task_types.sql @@ -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')); diff --git a/control-plane-java/src/main/resources/db/migration/V30__async_task_input_payload.sql b/control-plane-java/src/main/resources/db/migration/V30__async_task_input_payload.sql new file mode 100644 index 0000000..03db5f0 --- /dev/null +++ b/control-plane-java/src/main/resources/db/migration/V30__async_task_input_payload.sql @@ -0,0 +1 @@ +ALTER TABLE model_tasks ADD COLUMN IF NOT EXISTS input jsonb NOT NULL DEFAULT '{}'::jsonb; diff --git a/control-plane-java/src/test/java/com/moyi/controlplane/gateway/GatewayDeploymentTest.java b/control-plane-java/src/test/java/com/moyi/controlplane/gateway/GatewayDeploymentTest.java index ebfe95d..53f38a3 100644 --- a/control-plane-java/src/test/java/com/moyi/controlplane/gateway/GatewayDeploymentTest.java +++ b/control-plane-java/src/test/java/com/moyi/controlplane/gateway/GatewayDeploymentTest.java @@ -67,7 +67,7 @@ class GatewayDeploymentTest { } @Test - void modelsKeepRouteIdAndExposeDisplayMetadata() { + void modelsExposeReadableIdAndKeepRouteIdForDiagnostics() { CatalogService catalog = mock(CatalogService.class); GatewayGuard guard = mock(GatewayGuard.class); Map key = Map.of("id", UUID.randomUUID().toString()); @@ -90,7 +90,9 @@ class GatewayDeploymentTest { Map response = controller.models("Bearer test-key"); @SuppressWarnings("unchecked") Map item = ((List>) 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("public_name")); assertEquals("DeepSeek", item.get("vendor_name")); @@ -98,6 +100,88 @@ class GatewayDeploymentTest { assertEquals("deepseek-v4-flash", item.get("upstream_model")); } + @Test + void duplicateModelKeysAreDisambiguatedBySourceGroup() { + var first = Map.of( + "id", "route-a", "providerId", "provider-a", "modelKey", "gpt-5.5", + "publicName", "GPT-5.5", "sourceLabel", "ApiHub 国内", "upstreamModel", "gpt-5.5"); + var second = Map.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 key = Map.of("id", UUID.randomUUID().toString()); + Map 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 key = Map.of("id", UUID.randomUUID().toString()); + Map 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 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 key = Map.of("id", UUID.randomUUID().toString()); + Map 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 key = Map.of("id", UUID.randomUUID().toString()); + Map first = Map.of("id", "route-a", "modelKey", "same-model", "upstreamModel", "same-model", "sourceLabel", "A"); + Map 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 void invalidRedisPrefixIsRejected() { assertThrows(IllegalArgumentException.class, () -> new GatewayGuard(mock(StringRedisTemplate.class), 60, 300, 4, "")); diff --git a/control-plane-java/src/test/java/com/moyi/controlplane/task/AsyncProviderClientTest.java b/control-plane-java/src/test/java/com/moyi/controlplane/task/AsyncProviderClientTest.java index b014933..036184d 100644 --- a/control-plane-java/src/test/java/com/moyi/controlplane/task/AsyncProviderClientTest.java +++ b/control-plane-java/src/test/java/com/moyi/controlplane/task/AsyncProviderClientTest.java @@ -2,6 +2,7 @@ package com.moyi.controlplane.task; import static org.junit.jupiter.api.Assertions.assertEquals; 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.eq; 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 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 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 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 result = client.submit("provider-video", "sora-2", "video", Map.of("prompt", "a cat")); + assertEquals("video-1", result.get("id")); + } finally { + server.stop(0); + } + } + @ParameterizedTest @CsvSource({"401,UPSTREAM_AUTH_FAILED", "403,UPSTREAM_AUTH_FAILED", "402,UPSTREAM_CREDIT_INSUFFICIENT", "429,UPSTREAM_RATE_LIMITED", "503,UPSTREAM_PROVIDER_ERROR"}) void httpErrorsExposeOnlySafeCodes(int status, String code) throws Exception { @@ -70,7 +166,7 @@ class AsyncProviderClientTest { JdbcTemplate jdbc = mock(JdbcTemplate.class); SecretCipher cipher = new SecretCipher("test-encryption"); when(jdbc.queryForMap(any(String.class), eq("provider-vast"))).thenReturn(Map.of( - "code", "vast-tripo", "base_url", baseUrl, "credential_ciphertext", cipher.encrypt("test-key") + "code", "vast-tripo", "protocol", "http", "base_url", baseUrl, "credential_ciphertext", cipher.encrypt("test-key") )); return new AsyncProviderClient(jdbc, cipher, new ObjectMapper(), mock(Environment.class)); } diff --git a/reports/user-manual/MOYI平台用户操作手册.docx b/reports/user-manual/MOYI平台用户操作手册.docx new file mode 100644 index 0000000..b81cac2 Binary files /dev/null and b/reports/user-manual/MOYI平台用户操作手册.docx differ diff --git a/reports/vendor-tests-20260928/apihub-domestic.docx b/reports/vendor-tests-20260928/apihub-domestic.docx new file mode 100644 index 0000000..94bf38e Binary files /dev/null and b/reports/vendor-tests-20260928/apihub-domestic.docx differ diff --git a/reports/vendor-tests-20260928/apipro.docx b/reports/vendor-tests-20260928/apipro.docx new file mode 100644 index 0000000..2d41558 Binary files /dev/null and b/reports/vendor-tests-20260928/apipro.docx differ diff --git a/reports/vendor-tests-20260928/tokenfoundryx-overseas.docx b/reports/vendor-tests-20260928/tokenfoundryx-overseas.docx new file mode 100644 index 0000000..4f8c0f7 Binary files /dev/null and b/reports/vendor-tests-20260928/tokenfoundryx-overseas.docx differ diff --git a/reports/vendor-tests-20260928/tripo-official.docx b/reports/vendor-tests-20260928/tripo-official.docx new file mode 100644 index 0000000..c124704 Binary files /dev/null and b/reports/vendor-tests-20260928/tripo-official.docx differ diff --git a/scripts/package-deployment.ps1 b/scripts/package-deployment.ps1 index 18a6027..49c995d 100644 --- a/scripts/package-deployment.ps1 +++ b/scripts/package-deployment.ps1 @@ -19,7 +19,7 @@ try { $env:VITE_AUTH_MODE = 'remote' & node node_modules/vue-tsc/bin/vue-tsc.js --noEmit if ($LASTEXITCODE -ne 0) { throw 'Vue type check failed.' } - & node --experimental-strip-types --test tests/chat-stream.test.mjs 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.' } & node build-release.mjs if ($LASTEXITCODE -ne 0) { throw 'Vue build failed.' } @@ -60,7 +60,7 @@ $manifest = [ordered]@{ javaRelease = 21 database = 'PostgreSQL 15' 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 } [IO.File]::WriteAllText((Join-Path $staging 'release-manifest.json'), ($manifest | ConvertTo-Json -Depth 5), [Text.UTF8Encoding]::new($false))