fix:"规范音视频任务结果并按真实能力标识音频执行"

- 新增 AsyncTaskResponseNormalizer:解析 content/result/嵌套 data 回执与 {url} 对象,仅白名单媒体字段,裸字符串只接受 URL
- 视频/音频上游完成但无结果文件时标记 deliveryStatus=pending,结算一次、不重发不退款
- 目录与执行共用 AsyncMediaSupport.supportsSpeech;非 TTS 音频、专用渠道任务类型错配、非法语音参数在发送前拒绝
- 任务响应返回 taskType/completedAt,前端以持久任务类型优先,补音频 MIME、签名 URL 与未交付提示
- 新增 29 号 CPR 差距分析与优化方案文档,更新 24 号任务表 MED-01/02/03

验证:mvn -B -o clean verify 399 项通过(独立 PostgreSQL/Redis);前端构建与 27 项测试通过

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
xppandClaude Opus 5.5 committed 2026-10-22 11:48:47 +08:00
1 parent ba5dbc195e
commit 2e53b1d9c8
15 files changed
+493 -83

No files matched your search

@@ -541,7 +541,7 @@ public class CatalogService {
// "异步执行器待接入" when the execution path is already implemented,
// while unsupported task types remain visibly pending.
boolean asyncExecutor = asyncExecutorReady(
resultSet.getString("provider_id"), providerProtocol, resultSet.getString("model_type"));
resultSet.getString("provider_id"), providerProtocol, resultSet.getString("model_type"), resultSet.getString("upstream_model"));
String upstream=resultSet.getString("upstream_model").toLowerCase(java.util.Locale.ROOT);
if(upstream.startsWith("mj_") || upstream.startsWith("suno") || upstream.startsWith("lyria") || upstream.equals("swap_face")) asyncExecutor=false;
boolean syncExecutor=List.of("chat","text","code","multimodal","llm","reasoning","vision","embedding").contains(resultSet.getString("model_type")) && ("openai-compatible".equalsIgnoreCase(providerProtocol)||"provider-mock".equals(resultSet.getString("provider_id")));
@@ -581,9 +581,16 @@ public class CatalogService {
}
static boolean asyncExecutorReady(String providerId, String providerProtocol, String modelType) {
if (List.of("provider-mock", "provider-vast", "provider-bytedance").contains(providerId)) return true;
return asyncExecutorReady(providerId, providerProtocol, modelType, null);
}
static boolean asyncExecutorReady(String providerId, String providerProtocol, String modelType, String upstreamModel) {
if ("provider-mock".equals(providerId)) return true;
if ("provider-vast".equals(providerId)) return "3d".equalsIgnoreCase(modelType);
if ("provider-bytedance".equals(providerId)) return "video".equalsIgnoreCase(modelType);
if (!"openai-compatible".equalsIgnoreCase(providerProtocol)) return false;
return List.of("image", "video", "audio").contains(String.valueOf(modelType).toLowerCase(java.util.Locale.ROOT));
if ("audio".equalsIgnoreCase(modelType)) return com.moyi.controlplane.provider.media.AsyncMediaSupport.supportsSpeech(upstreamModel);
return List.of("image", "video").contains(String.valueOf(modelType).toLowerCase(java.util.Locale.ROOT));
}
private boolean billingReady(Map<String,Object> pricing, String mode, String providerId) {
@@ -0,0 +1,17 @@
package com.moyi.controlplane.provider.media;
import java.util.Locale;
import java.util.Set;
/** Shared catalog/execution contract. Audio model type alone does not imply a speech endpoint. */
public final class AsyncMediaSupport {
private AsyncMediaSupport() {}
private static final Set<String> SPEECH_MODELS = Set.of(
"tts-1", "tts-1-1106", "tts-1-hd", "tts-1-hd-1106", "gpt-4o-mini-tts");
public static boolean supportsSpeech(String upstreamModel) {
if (upstreamModel == null) return false;
String model = upstreamModel.toLowerCase(Locale.ROOT);
return SPEECH_MODELS.contains(model) || model.matches("gpt-4o-mini-tts-[0-9]{4}-[0-9]{2}-[0-9]{2}");
}
}
@@ -9,6 +9,7 @@ import com.moyi.controlplane.provider.credentials.UpstreamCredentialResolver;
import com.moyi.controlplane.provider.transport.ProviderRequestException;
import com.moyi.controlplane.provider.transport.BoundedHttpTransport;
import com.moyi.controlplane.provider.media.MediaDownloader;
import com.moyi.controlplane.provider.media.AsyncMediaSupport;
import com.moyi.controlplane.provider.media.MediaDownloader.MediaBytes;
import java.net.URI;
import java.net.http.HttpRequest;
@@ -83,6 +84,7 @@ public class AsyncProviderClient {
Provider provider = provider(providerId, routeId, upstreamModel);
String code = provider.code().toLowerCase(java.util.Locale.ROOT);
if (isTripo(provider.code(), provider.baseUrl())) {
requireTaskType(taskType, "3d");
Map<String,Object> body = new LinkedHashMap<>();
body.put("model", upstreamModel);
String media = mediaUrl(input);
@@ -94,6 +96,7 @@ public class AsyncProviderClient {
return withKeyFingerprint(request("POST", tripoBase(provider.baseUrl()) + (media == null ? "/generation/text-to-model" : "/generation/image-to-model"), apiKey, body, "上游 3D 任务创建失败"), apiKey);
}
if (code.contains("byte") || code.contains("seedance") || provider.baseUrl().contains("volces.com")) {
requireTaskType(taskType, "video");
List<Map<String,Object>> content = new ArrayList<>();
if (!prompt.isBlank()) content.add(Map.of("type", "text", "text", prompt));
String media = mediaUrl(input);
@@ -197,15 +200,29 @@ public class AsyncProviderClient {
}
}
if ("audio".equalsIgnoreCase(taskType)) {
if (!AsyncMediaSupport.supportsSpeech(model)) throw new ApiException("ASYNC_PROVIDER_NOT_IMPLEMENTED", "该音频模型不是已接入的文本转语音模型;转写、音乐和实时音频需专用接口", 501);
if (media != null) throw new ApiException("MEDIA_INPUT_UNSUPPORTED", "该语音生成接口接收文本;音频转写需使用专用转写接口", 400);
if (prompt.isBlank() || prompt.length() > 4096) throw new ApiException("INVALID_TASK_INPUT", "语音文本必须为 1 至 4096 个字符", 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");
Object voice = input.getOrDefault("voice", "alloy");
if (!(voice instanceof String name) || name.isBlank() || name.length() > 100) throw new ApiException("INVALID_TASK_INPUT", "请提供有效的语音音色", 400);
Object format = input.getOrDefault("response_format", "mp3");
if (!(format instanceof String formatName) || !List.of("mp3","wav","opus","aac","flac").contains(formatName)) {
throw new ApiException("INVALID_TASK_INPUT", "语音结果支持 mp3、wav、opus、aac、flac;原始 PCM 尚未接入播放", 400);
}
if (input.containsKey("stream_format") && !"audio".equals(input.get("stream_format"))) throw new ApiException("INVALID_TASK_INPUT", "语音任务暂不支持 SSE 事件格式", 400);
body.put("voice", voice); body.put("response_format", format);
if (input.containsKey("speed")) {
try {
var speed = new java.math.BigDecimal(String.valueOf(input.get("speed")));
if (speed.compareTo(new java.math.BigDecimal("0.25")) < 0 || speed.compareTo(new java.math.BigDecimal("4")) > 0) throw new NumberFormatException();
body.put("speed", speed);
} catch (NumberFormatException error) { throw new ApiException("INVALID_TASK_INPUT", "语速必须为 0.25 至 4", 400); }
}
String apiKey = provider.nextApiKey(routeId, model, environment, keyCursors);
return Map.of("_moyiInlineResult", requestAudio("POST", base + "/audio/speech", apiKey, body, "上游语音任务创建失败"), "_moyiUpstreamKeyFingerprint", keyFingerprint(apiKey));
}
@@ -326,7 +343,14 @@ public class AsyncProviderClient {
if (response.statusCode() < 200 || response.statusCode() >= 300) throw upstreamStatus(response.statusCode(), message);
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);
String contentType = response.headers().firstValue("Content-Type").orElse("application/octet-stream").split(";", 2)[0].trim().toLowerCase(java.util.Locale.ROOT);
if (contentType.equals("application/octet-stream")) contentType = switch (String.valueOf(body.get("response_format"))) {
case "wav" -> "audio/wav";
case "opus" -> "audio/ogg";
case "aac" -> "audio/aac";
case "flac" -> "audio/flac";
default -> "audio/mpeg";
};
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));
@@ -377,6 +401,10 @@ public class AsyncProviderClient {
return ProviderRequestException.http(status, message);
}
private void requireTaskType(String actual, String expected) {
if (!expected.equalsIgnoreCase(actual)) throw new ApiException("ASYNC_PROVIDER_NOT_IMPLEMENTED", "渠道专用执行接口与任务类型不匹配", 501);
}
private ProviderRequestException requestFailure(Exception exception, boolean sending, String message) {
if (exception instanceof InterruptedException) Thread.currentThread().interrupt();
if (exception instanceof ProviderRequestException failure) return failure;
@@ -0,0 +1,87 @@
package com.moyi.controlplane.task;
import java.math.BigDecimal;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
/** Bounded envelope normalization only. No upstream calls, credentials, routing or settlement. */
final class AsyncTaskResponseNormalizer {
private AsyncTaskResponseNormalizer() {}
static String status(Map<String,Object> response) {
Object raw = response.get("status");
if (raw == null) raw = nested(response).get("status");
return switch (String.valueOf(raw == null ? "running" : raw).trim().toLowerCase(Locale.ROOT)) {
case "success", "succeeded", "completed" -> "succeeded";
case "failed", "failure", "error", "banned" -> "failed";
case "cancelled", "canceled" -> "cancelled";
default -> "running";
};
}
static int progress(Map<String,Object> response, String status) {
if ("succeeded".equals(status)) return 100;
Object raw = response.get("progress");
if (raw == null) raw = nested(response).get("progress");
try {
String value = String.valueOf(raw).trim();
if (value.length() > 32) return 0;
if (value.endsWith("%")) value = value.substring(0, value.length() - 1);
return new BigDecimal(value).max(BigDecimal.ZERO).min(BigDecimal.valueOf(100)).intValue();
} catch (NumberFormatException error) { return 0; }
}
static Map<String,Object> result(Map<String,Object> response) {
Map<?,?> data = nested(response);
for (String field : List.of("output", "content", "result")) {
Map<String,Object> result = asset(response.get(field));
if (result == null) result = asset(data.get(field));
if (result != null) {
if (data.get("credits_consumed") != null) result.put("creditsConsumed", data.get("credits_consumed"));
return result;
}
}
Map<String,Object> direct = asset(response);
return direct != null ? direct : asset(data);
}
private static Map<?,?> nested(Map<String,Object> response) {
return response.get("data") instanceof Map<?,?> data ? data : Map.of();
}
private static Map<String,Object> asset(Object raw) {
// A bare string is only a media reference when it is a URL; provider messages are not assets.
if (raw instanceof String value) return value.matches("(?i)(https?://|data:).+") ? new LinkedHashMap<>(Map.of("url", value)) : null;
if (!(raw instanceof Map<?,?> source)) return null;
// Select assets explicitly: task metadata, provider errors or arbitrary content are not media.
var result = new LinkedHashMap<String,Object>();
for (String field : List.of("url", "modelUrl", "previewUrl", "imageUrl", "videoUrl", "audioUrl", "base64", "b64_json")) {
Object value = source.get(field);
if (value instanceof String text && !text.isBlank()) result.put(field, text);
}
String[][] aliases = {{"model_url","modelUrl"},{"rendered_image_url","previewUrl"},
{"generated_image_url","imageUrl"},{"image_url","imageUrl"},{"video_url","videoUrl"},
{"audio_url","audioUrl"},{"last_frame_url","previewUrl"},{"preview_url","previewUrl"}};
for (String[] alias : aliases) {
Object value = source.get(alias[0]);
if (value instanceof Map<?,?> map) value = map.get("url");
if (value instanceof String text && !text.isBlank()) result.putIfAbsent(alias[1], text);
}
for (String kind : List.of("video","audio","image")) {
if (source.get(kind) instanceof Map<?,?> map && map.get("url") instanceof String text && !text.isBlank()) {
result.putIfAbsent(kind + "Url", text);
}
}
if (result.isEmpty() || result.keySet().stream().allMatch("previewUrl"::equals)) return null;
for (String field : List.of("format","mimeType","mime_type","creditsConsumed","credits_consumed")) {
if (source.get(field) instanceof String || source.get(field) instanceof Number) result.put(field, source.get(field));
}
for (String field : List.of("usage","usageMetadata")) if (source.get(field) instanceof Map<?,?> value) result.put(field, value);
for (String field : List.of("modelUrl","videoUrl","audioUrl","imageUrl")) {
if (!result.containsKey("url") && result.containsKey(field)) result.put("url", result.get(field));
}
return result;
}
}
@@ -392,10 +392,12 @@ public class AsyncTaskService {
task.put("id", resultSet.getString("id"));
task.put("provider", "upstream");
task.put("model", resultSet.getString("model_id"));
task.put("taskType", resultSet.getString("task_type"));
task.put("status", resultSet.getString("status"));
task.put("progress", resultSet.getInt("progress"));
task.put("createdAt", resultSet.getObject("created_at", OffsetDateTime.class));
task.put("updatedAt", resultSet.getObject("updated_at", OffsetDateTime.class));
putIfPresent(task, "completedAt", resultSet, "completed_at");
task.put("requiresReconciliation", "reconciling".equals(resultSet.getString("status")));
putIfPresent(task, "costAmount", resultSet, "cost_amount");
String result = resultSet.getString("result");
@@ -483,9 +485,15 @@ public class AsyncTaskService {
updatePollingState(taskId, null, "UPSTREAM_TASK_POLL_RETRY", message);
return;
}
String status = normalizedStatus(response);
int progress = progress(response, status);
Map<String,Object> result = result(response);
String status = AsyncTaskResponseNormalizer.status(response);
int progress = AsyncTaskResponseNormalizer.progress(response, status);
Map<String,Object> result = AsyncTaskResponseNormalizer.result(response);
String taskType = jdbc.queryForObject("SELECT task_type FROM model_tasks WHERE id=?", String.class, taskId);
if ("succeeded".equals(status) && result == null && List.of("video", "audio").contains(taskType)) {
// Generation success is not proof that the media bytes were delivered. Preserve billing
// semantics, never resubmit/refund automatically, and expose the missing delivery explicitly.
result = new LinkedHashMap<>(Map.of("deliveryStatus", "pending", "deliveryCode", "TASK_RESULT_UNAVAILABLE"));
}
if(result!=null) {
result=new LinkedHashMap<>(result);
result.putAll(usageOnly(response));
@@ -518,63 +526,6 @@ public class AsyncTaskService {
throw new IllegalStateException("Persisted task timestamp is missing");
}
private String normalizedStatus(Map<String,Object> response) {
Object raw = response.get("status");
if (raw == null && response.get("data") instanceof Map<?,?> data) raw = data.get("status");
String value = String.valueOf(raw == null ? "running" : raw).toLowerCase(java.util.Locale.ROOT);
return switch (value) {
case "success", "succeeded", "completed" -> "succeeded";
case "failed", "failure", "error", "banned" -> "failed";
case "cancelled", "canceled" -> "cancelled";
default -> "running";
};
}
private int progress(Map<String,Object> response, String status) {
Object raw = response.get("progress");
if (raw == null && response.get("data") instanceof Map<?,?> data) raw = data.get("progress");
if (raw instanceof Number number) return Math.max(0, Math.min(100, number.intValue()));
return "succeeded".equals(status) ? 100 : 0;
}
private Map<String,Object> result(Map<String,Object> response) {
Object raw = response.get("output");
Map<?,?> data = response.get("data") instanceof Map<?,?> value ? value : null;
if (raw == null && data != null) raw = data.get("output");
if (raw instanceof Map<?,?> map) {
Map<String,Object> value = new LinkedHashMap<>();
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;
}
private void copyAlias(Map<String,Object> value, String source, String target) {
if (value.get(target) == null && value.get(source) != null) value.put(target, value.get(source));
}
private String writeJson(Object value) { try { return objectMapper.writeValueAsString(value); } catch (JsonProcessingException exception) { return "{}"; } }
private long estimateInputUnits(Map<String,Object> input) {