fix:"统一媒体结果校验并补齐交付状态"
This commit is contained in:
1 parent
bb3baed095
commit
5d6253cbf1
14 files changed
+510
-106
No files matched your search
+142
@@ -0,0 +1,142 @@
|
||||
package com.moyi.controlplane.provider.media;
|
||||
|
||||
import java.net.URI;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
|
||||
/** Pure media-result boundary. Does not fetch URLs, resolve credentials or change settlement. */
|
||||
public final class MediaAssetNormalizer {
|
||||
private static final Map<String,String> FORMAT_MIMES = Map.ofEntries(
|
||||
Map.entry("png", "image/png"), Map.entry("jpg", "image/jpeg"), Map.entry("jpeg", "image/jpeg"),
|
||||
Map.entry("webp", "image/webp"), Map.entry("gif", "image/gif"), Map.entry("avif", "image/avif"),
|
||||
Map.entry("mp4", "video/mp4"), Map.entry("webm", "video/webm"), Map.entry("mov", "video/quicktime"),
|
||||
Map.entry("mp3", "audio/mpeg"), Map.entry("wav", "audio/wav"), Map.entry("ogg", "audio/ogg"),
|
||||
Map.entry("opus", "audio/ogg"), Map.entry("aac", "audio/aac"), Map.entry("flac", "audio/flac"),
|
||||
Map.entry("m4a", "audio/mp4"), Map.entry("glb", "model/gltf-binary"), Map.entry("gltf", "model/gltf+json"));
|
||||
|
||||
private MediaAssetNormalizer() {}
|
||||
|
||||
public static Map<String,Object> normalize(Object raw, String taskType) {
|
||||
if (raw instanceof String) {
|
||||
String url = reference(raw, taskType);
|
||||
return url == null ? null : new LinkedHashMap<>(Map.of("url", url));
|
||||
}
|
||||
if (!(raw instanceof Map<?,?> source)) return null;
|
||||
var result = new LinkedHashMap<String,Object>();
|
||||
String[][] aliases = {{"modelUrl","modelUrl"},{"model_url","modelUrl"},
|
||||
{"previewUrl","previewUrl"},{"rendered_image_url","previewUrl"},{"preview_url","previewUrl"},{"last_frame_url","previewUrl"},
|
||||
{"imageUrl","imageUrl"},{"image_url","imageUrl"},{"generated_image_url","imageUrl"},{"image","imageUrl"},
|
||||
{"videoUrl","videoUrl"},{"video_url","videoUrl"},{"video","videoUrl"},
|
||||
{"audioUrl","audioUrl"},{"audio_url","audioUrl"},{"audio","audioUrl"}};
|
||||
for (String[] alias : aliases) {
|
||||
String kind = switch (alias[1]) {
|
||||
case "modelUrl" -> "3d";
|
||||
case "videoUrl" -> "video";
|
||||
case "audioUrl" -> "audio";
|
||||
default -> "image";
|
||||
};
|
||||
String value = reference(source.get(alias[0]), kind);
|
||||
if (value != null) result.putIfAbsent(alias[1], value);
|
||||
}
|
||||
String url = reference(source.get("url"), taskType);
|
||||
String payload = payload(source.get("b64_json"));
|
||||
if (payload == null) payload = payload(source.get("base64"));
|
||||
String mime = mime(source);
|
||||
if (mime == null && payload != null) mime = imageSignatureMime(payload);
|
||||
if (url != null && (mime == null || matchesKind(mime, taskType))) result.put("url", url);
|
||||
if (payload != null && mime != null && matchesKind(mime, taskType)) {
|
||||
result.put("base64", payload);
|
||||
result.put("mimeType", mime);
|
||||
}
|
||||
if (source.get("format") instanceof String format && format.length() <= 40 && (mime == null || matchesKind(mime, taskType))) result.put("format", format.toLowerCase(Locale.ROOT));
|
||||
if (mime != null && matchesKind(mime, taskType)) result.putIfAbsent("mimeType", mime);
|
||||
// A thumbnail or another media lane is not proof that this task's primary file was delivered.
|
||||
List<String> primary = switch (taskType == null ? "" : taskType.toLowerCase(Locale.ROOT)) {
|
||||
case "video" -> List.of("videoUrl");
|
||||
case "audio" -> List.of("audioUrl");
|
||||
case "image" -> List.of("imageUrl");
|
||||
case "3d" -> List.of("modelUrl");
|
||||
default -> List.of("modelUrl", "videoUrl", "audioUrl", "imageUrl");
|
||||
};
|
||||
if (!result.containsKey("url")) {
|
||||
for (String field : primary) if (result.containsKey(field)) { result.put("url", result.get(field)); break; }
|
||||
}
|
||||
if (!result.containsKey("url") && !result.containsKey("base64")) return null;
|
||||
for (String field : List.of("usage", "usageMetadata")) if (source.get(field) instanceof Map<?,?> value) result.put(field, value);
|
||||
Object credits = source.get("creditsConsumed");
|
||||
if (credits == null) credits = source.get("credits_consumed");
|
||||
if (credits instanceof Number || credits instanceof String) result.put("creditsConsumed", credits);
|
||||
return result;
|
||||
}
|
||||
|
||||
private static String reference(Object raw, String kind) {
|
||||
return reference(raw, kind, 0);
|
||||
}
|
||||
|
||||
private static String reference(Object raw, String kind, int depth) {
|
||||
if (depth > 4) return null;
|
||||
if (raw instanceof Map<?,?> object) {
|
||||
String url = reference(object.get("url"), kind, depth + 1);
|
||||
if (url != null) return url;
|
||||
String payload = payload(object.get("b64_json"));
|
||||
if (payload == null) payload = payload(object.get("base64"));
|
||||
String mime = mime(object);
|
||||
if (mime == null && payload != null) mime = imageSignatureMime(payload);
|
||||
return payload != null && mime != null && matchesKind(mime, kind) ? "data:" + mime + ";base64," + payload : null;
|
||||
}
|
||||
if (!(raw instanceof String text)) return null;
|
||||
String value = text.trim();
|
||||
if (value.isEmpty() || value.indexOf('\\') >= 0 || value.chars().anyMatch(Character::isISOControl)) return null;
|
||||
if (value.regionMatches(true, 0, "data:", 0, 5)) {
|
||||
int marker = value.toLowerCase(Locale.ROOT).indexOf(";base64,");
|
||||
if (marker < 0) return null;
|
||||
String mime = validMime(value.substring(5, marker));
|
||||
String payload = payload(value.substring(marker + 8));
|
||||
return mime != null && payload != null && matchesKind(mime, kind) ? "data:" + mime + ";base64," + payload : null;
|
||||
}
|
||||
try {
|
||||
URI uri = URI.create(value);
|
||||
String path = uri.getPath();
|
||||
String suffix = path == null ? "" : path.substring(path.lastIndexOf('.') + 1).toLowerCase(Locale.ROOT);
|
||||
String knownMime = FORMAT_MIMES.get(suffix);
|
||||
return ("https".equalsIgnoreCase(uri.getScheme()) || "http".equalsIgnoreCase(uri.getScheme()))
|
||||
&& uri.getHost() != null && uri.getUserInfo() == null
|
||||
&& (knownMime == null || matchesKind(knownMime, kind)) ? value : null;
|
||||
} catch (IllegalArgumentException error) { return null; }
|
||||
}
|
||||
|
||||
private static String payload(Object raw) {
|
||||
if (!(raw instanceof String text) || text.length() > 12 * 1024 * 1024) return null;
|
||||
String value = text.replaceAll("\\s+", "");
|
||||
return !value.isEmpty() && value.length() % 4 != 1 && value.matches("[a-zA-Z0-9+/]+={0,2}") ? value : null;
|
||||
}
|
||||
|
||||
private static String mime(Map<?,?> source) {
|
||||
String value = validMime(source.get("mimeType"));
|
||||
if (value == null) value = validMime(source.get("mime_type"));
|
||||
if (value == null && source.get("format") instanceof String format) value = FORMAT_MIMES.get(format.toLowerCase(Locale.ROOT));
|
||||
return value;
|
||||
}
|
||||
|
||||
private static String validMime(Object raw) {
|
||||
if (!(raw instanceof String text)) return null;
|
||||
String value = text.trim().toLowerCase(Locale.ROOT);
|
||||
if (List.of("image/png", "image/jpeg", "image/webp", "image/gif", "image/avif", "model/gltf-binary", "model/gltf+json").contains(value)) return value;
|
||||
return value.matches("(audio|video)/[a-z0-9.+-]+") && !value.endsWith("xml") ? value : null;
|
||||
}
|
||||
|
||||
private static boolean matchesKind(String mime, String kind) {
|
||||
if (kind == null || kind.isBlank()) return true;
|
||||
return mime.startsWith(("3d".equalsIgnoreCase(kind) ? "model" : kind.toLowerCase(Locale.ROOT)) + "/");
|
||||
}
|
||||
|
||||
private static String imageSignatureMime(String payload) {
|
||||
// Infer only unambiguous image signatures; never guess the format of arbitrary base64 text.
|
||||
if (payload.startsWith("iVBORw0KGgo")) return "image/png";
|
||||
if (payload.startsWith("/9j/")) return "image/jpeg";
|
||||
if (payload.startsWith("R0lGODdh") || payload.startsWith("R0lGODlh")) return "image/gif";
|
||||
return null;
|
||||
}
|
||||
}
|
||||
+1
-17
@@ -313,23 +313,7 @@ public class AsyncProviderClient {
|
||||
|
||||
/** 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"));
|
||||
copyUsage(response,result);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
Object url = response.get("url");
|
||||
if (url != null) { var result=new LinkedHashMap<String,Object>(); result.put("url",url); copyUsage(response,result); return result; }
|
||||
return null;
|
||||
return AsyncTaskResponseNormalizer.result(response, "image");
|
||||
}
|
||||
|
||||
private Map<String,Object> requestAudio(String method, String url, String key, Map<String,Object> body, String message) {
|
||||
|
||||
+35
-41
@@ -5,6 +5,7 @@ import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import com.moyi.controlplane.provider.media.MediaAssetNormalizer;
|
||||
|
||||
/** Bounded envelope normalization only. No upstream calls, credentials, routing or settlement. */
|
||||
final class AsyncTaskResponseNormalizer {
|
||||
@@ -34,54 +35,47 @@ final class AsyncTaskResponseNormalizer {
|
||||
}
|
||||
|
||||
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;
|
||||
return result(response, null);
|
||||
}
|
||||
|
||||
static Map<String,Object> result(Map<String,Object> response, String taskType) {
|
||||
return findResult(response, taskType, 0);
|
||||
}
|
||||
|
||||
static Map<String,Object> completedResult(Map<String,Object> response, String taskType) {
|
||||
Map<String,Object> result = result(response, taskType);
|
||||
if (result != null) return result;
|
||||
result = new LinkedHashMap<>(Map.of("deliveryStatus", "pending", "deliveryCode", "TASK_RESULT_UNAVAILABLE"));
|
||||
copyMetadata(nested(response), result);
|
||||
copyMetadata(response, result);
|
||||
return result;
|
||||
}
|
||||
|
||||
private static Map<String,Object> findResult(Object raw, String taskType, int depth) {
|
||||
if (depth > 4) return null;
|
||||
if (raw instanceof List<?> items) {
|
||||
for (int i = 0; i < Math.min(items.size(), 16); i++) {
|
||||
var result = findResult(items.get(i), taskType, depth + 1);
|
||||
if (result != null) return result;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
Map<String,Object> direct = asset(response);
|
||||
return direct != null ? direct : asset(data);
|
||||
if (!(raw instanceof Map<?,?> source)) return MediaAssetNormalizer.normalize(raw, taskType);
|
||||
for (String field : List.of("output", "content", "result", "data")) {
|
||||
var result = findResult(source.get(field), taskType, depth + 1);
|
||||
if (result != null) { copyMetadata(source, result); return result; }
|
||||
}
|
||||
return MediaAssetNormalizer.normalize(source, taskType);
|
||||
}
|
||||
|
||||
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;
|
||||
private static void copyMetadata(Map<?,?> source, Map<String,Object> result) {
|
||||
for (String field : List.of("usage", "usageMetadata")) if (source.get(field) instanceof Map<?,?> value) result.put(field, value);
|
||||
Object credits = source.get("creditsConsumed");
|
||||
if (credits == null) credits = source.get("credits_consumed");
|
||||
if (credits instanceof Number || credits instanceof String) result.putIfAbsent("creditsConsumed", credits);
|
||||
}
|
||||
}
|
||||
@@ -294,7 +294,8 @@ public class AsyncTaskService {
|
||||
if (upstream.get("_moyiInlineResult") instanceof Map<?,?> inline) {
|
||||
Map<String,Object> result = new LinkedHashMap<>();
|
||||
inline.forEach((key, value) -> result.put(String.valueOf(key), value));
|
||||
completion = outcomes.observeTerminal(taskId, "succeeded", result, null, null);
|
||||
completion = outcomes.observeTerminal(taskId, "succeeded",
|
||||
AsyncTaskResponseNormalizer.completedResult(result, String.valueOf(task.get("task_type"))), null, null);
|
||||
} else {
|
||||
OffsetDateTime submittedAt = now();
|
||||
outcomes.accepted(taskId, providerTaskId(upstream), upstream.get("_moyiUpstreamKeyFingerprint"),
|
||||
@@ -487,18 +488,12 @@ public class AsyncTaskService {
|
||||
}
|
||||
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));
|
||||
if(response.get("data") instanceof Map<?,?> nested && result.get("usage")==null && nested.get("usage") instanceof Map<?,?> u) result.put("usage",u);
|
||||
}
|
||||
// File delivery is independent of upstream generation/settlement. Missing or invalid files
|
||||
// never cause a generation retry or an automatic refund, including image and 3D tasks.
|
||||
Map<String,Object> result = "succeeded".equals(status)
|
||||
? AsyncTaskResponseNormalizer.completedResult(response, taskType)
|
||||
: AsyncTaskResponseNormalizer.result(response, taskType);
|
||||
if ("succeeded".equals(status) || "failed".equals(status) || "cancelled".equals(status)) {
|
||||
// Persistence failures must roll back and remain pollable, rather
|
||||
// than being mistaken for upstream failures and refunded.
|
||||
@@ -533,12 +528,6 @@ public class AsyncTaskService {
|
||||
catch (JsonProcessingException exception) { throw new ApiException("INVALID_REQUEST", "任务请求无法编码"); }
|
||||
}
|
||||
|
||||
private Map<String,Object> usageOnly(Map<String,Object> result) {
|
||||
var selected=new LinkedHashMap<String,Object>();
|
||||
if(result!=null) for(String field:List.of("usage","usageMetadata")) if(result.get(field) instanceof Map<?,?> value) selected.put(field,value);
|
||||
return selected;
|
||||
}
|
||||
|
||||
private void reserveWallet(UUID userId, BigDecimal amount) {
|
||||
jdbc.update("INSERT INTO user_wallets(user_id) VALUES (?) ON CONFLICT DO NOTHING", userId);
|
||||
Map<String,Object> wallet = jdbc.queryForMap("SELECT balance,reserved_balance FROM user_wallets WHERE user_id=? FOR UPDATE", userId);
|
||||
|
||||
Reference in new issue
Block a user