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
+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,
|
||||
|
||||
Reference in new issue
Block a user