feat: publish readable model ids and media task adapters
This commit is contained in:
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.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<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'");
|
||||
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) {
|
||||
@@ -453,12 +454,12 @@ public class CatalogService {
|
||||
}
|
||||
|
||||
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; }
|
||||
}
|
||||
|
||||
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; }
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
+19
-12
@@ -41,16 +41,16 @@ public class GatewayController {
|
||||
public Map<String, Object> models(@RequestHeader(value = "Authorization", required = false) String authorization) {
|
||||
Map<String, Object> key = requireKey(authorization);
|
||||
guard.checkRate(UUID.fromString(String.valueOf(key.get("id"))), true);
|
||||
List<Map<String, Object>> data = catalogService.listModels(false).stream()
|
||||
.filter(model -> catalogService.keyCanInvoke(key, String.valueOf(model.get("id"))))
|
||||
List<Map<String, Object>> visibleModels = GatewayModelIds.authorizedModels(catalogService, key);
|
||||
List<Map<String, Object>> data = visibleModels.stream()
|
||||
.map(model -> {
|
||||
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("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<String, Object> input, String idempotencyKey, HttpServletResponse servletResponse) {
|
||||
long startedAt = System.currentTimeMillis();
|
||||
String modelId = String.valueOf(input.getOrDefault("model", ""));
|
||||
Map<String, Object> 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<String, Object> 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<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 {
|
||||
String modelId=model.get("id").toString();
|
||||
private void stream(Map<String,Object> model, String modelId, Map<String,Object> 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<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);
|
||||
}
|
||||
}
|
||||
}
|
||||
+274
-9
@@ -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<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);
|
||||
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<>();
|
||||
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<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, "上游视频任务创建失败");
|
||||
}
|
||||
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<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) {
|
||||
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<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 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<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())); }
|
||||
}
|
||||
}
|
||||
@@ -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<String, Object> createForKey(Map<String, Object> key, String taskType, Map<String, Object> input) {
|
||||
gatewayGuard.checkRate(UUID.fromString(String.valueOf(key.get("id"))), false);
|
||||
String modelId = String.valueOf(input.getOrDefault("model", ""));
|
||||
Map<String, Object> model = catalogService.activeModel(modelId);
|
||||
String requestedModelId = String.valueOf(input.getOrDefault("model", ""));
|
||||
Map<String, Object> 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<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();
|
||||
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<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 String providerTaskId(Map<String,Object> 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;
|
||||
}
|
||||
|
||||
|
||||
@@ -21,14 +21,17 @@ public class TaskController {
|
||||
this.tasks = tasks;
|
||||
}
|
||||
|
||||
@PostMapping("/3d/tasks")
|
||||
@PostMapping("/{type:3d|image|audio}/tasks")
|
||||
public ResponseEntity<Map<String, Object>> create3d(
|
||||
@org.springframework.web.bind.annotation.PathVariable(required = false) String type,
|
||||
@RequestHeader(value = "Authorization", required = false) String authorization,
|
||||
@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")
|
||||
public ResponseEntity<Map<String, Object>> createVideo(
|
||||
@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;
|
||||
Reference in new issue
Block a user