菜单修改优化
This commit is contained in:
+86
-1
@@ -49,6 +49,15 @@ public class ImageVideoAsyncTaskService {
|
||||
private static final Set<String> COZE_PRIMARY_STATUS_FIELDS = Set.of(
|
||||
"execute_status", "executeStatus", "workflow_status", "workflowStatus"
|
||||
);
|
||||
private static final Set<String> COZE_BUSINESS_ERROR_FIELDS = Set.of(
|
||||
"error", "error_message", "errorMessage"
|
||||
);
|
||||
private static final Set<String> COZE_BUSINESS_STATUS_CODE_FIELDS = Set.of(
|
||||
"status_code", "statusCode"
|
||||
);
|
||||
private static final Set<String> COZE_BUSINESS_STATUS_MESSAGE_FIELDS = Set.of(
|
||||
"status_msg", "statusMessage"
|
||||
);
|
||||
|
||||
private final ImageVideoAsyncTaskMapper taskMapper;
|
||||
private final ImageVideoCozeService cozeService;
|
||||
@@ -233,7 +242,10 @@ public class ImageVideoAsyncTaskService {
|
||||
if (FAILED_STATUSES.contains(cozeStatus)) {
|
||||
task.setCozeStatus(cozeStatus);
|
||||
task.setResultJson(writeJson(result));
|
||||
failTask(task, new BusinessException("Coze workflow finished with status " + cozeStatus));
|
||||
String businessError = resolveCozeBusinessError(result);
|
||||
failTask(task, new BusinessException(businessError.isBlank()
|
||||
? "Coze workflow finished with status " + cozeStatus
|
||||
: businessError));
|
||||
} else {
|
||||
completeTask(task, transformResult(type, result), cozeStatus);
|
||||
}
|
||||
@@ -315,6 +327,19 @@ public class ImageVideoAsyncTaskService {
|
||||
}
|
||||
|
||||
private void completeTask(ImageVideoAsyncTaskEntity task, Object result, String cozeStatus) {
|
||||
String businessError = resolveCozeBusinessError(result);
|
||||
if (!businessError.isBlank()) {
|
||||
task.setCozeStatus(normalizeStatus(cozeStatus));
|
||||
task.setResultJson(writeJson(result));
|
||||
failTask(task, new BusinessException(businessError));
|
||||
return;
|
||||
}
|
||||
if (requiresVideoResult(task) && !hasFinalVideoUrl(result)) {
|
||||
task.setCozeStatus(normalizeStatus(cozeStatus));
|
||||
task.setResultJson(writeJson(result));
|
||||
failTask(task, new BusinessException("Coze workflow succeeded but video_url is empty"));
|
||||
return;
|
||||
}
|
||||
task.setStatus(TaskStatus.SUCCESS.name());
|
||||
task.setResultJson(writeJson(result));
|
||||
archiveService.enrichCompletedTask(task, result);
|
||||
@@ -511,6 +536,60 @@ public class ImageVideoAsyncTaskService {
|
||||
return value;
|
||||
}
|
||||
|
||||
private String resolveCozeBusinessError(Object result) {
|
||||
String error = findText(result, COZE_BUSINESS_ERROR_FIELDS);
|
||||
if (!error.isBlank()) {
|
||||
return error;
|
||||
}
|
||||
|
||||
String statusCode = findText(result, COZE_BUSINESS_STATUS_CODE_FIELDS);
|
||||
if (statusCode.isBlank() || "0".equals(statusCode)) {
|
||||
return "";
|
||||
}
|
||||
String statusMessage = findText(result, COZE_BUSINESS_STATUS_MESSAGE_FIELDS);
|
||||
return statusMessage.isBlank()
|
||||
? "Coze workflow returned business status " + statusCode
|
||||
: statusMessage + " (status_code: " + statusCode + ")";
|
||||
}
|
||||
|
||||
private boolean hasFinalVideoUrl(Object result) {
|
||||
Object data = parsePossiblyJson(childValue(result, "data"));
|
||||
Object scope = data == null ? parsePossiblyJson(result) : data;
|
||||
if (scope == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
Collection<?> records = scope instanceof Collection<?> collection ? collection : List.of(scope);
|
||||
boolean hasRunOutput = false;
|
||||
for (Object record : records) {
|
||||
Object output = childValue(parsePossiblyJson(record), "output");
|
||||
if (output == null) {
|
||||
continue;
|
||||
}
|
||||
hasRunOutput = true;
|
||||
if (hasVideoUrlInOutput(output)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return !hasRunOutput && hasVideoUrlInOutput(scope);
|
||||
}
|
||||
|
||||
private boolean hasVideoUrlInOutput(Object output) {
|
||||
Object envelope = parsePossiblyJson(output);
|
||||
Object finalOutput = childValue(envelope, "Output");
|
||||
if (finalOutput != null) {
|
||||
return hasRecognizedVideoUrl(parsePossiblyJson(finalOutput));
|
||||
}
|
||||
if (childValue(envelope, "node_status") != null) {
|
||||
return false;
|
||||
}
|
||||
return hasRecognizedVideoUrl(envelope);
|
||||
}
|
||||
|
||||
private boolean hasRecognizedVideoUrl(Object payload) {
|
||||
return !ImageVideoArchiveService.findUrls(payload, Set.of("videourl")).isEmpty();
|
||||
}
|
||||
|
||||
private String normalizeStatus(String value) {
|
||||
return value == null ? "" : value.trim().toUpperCase();
|
||||
}
|
||||
@@ -529,6 +608,12 @@ public class ImageVideoAsyncTaskService {
|
||||
&& task.getSubmittedAt().plusHours(1).isBefore(LocalDateTime.now());
|
||||
}
|
||||
|
||||
private boolean requiresVideoResult(ImageVideoAsyncTaskEntity task) {
|
||||
String taskType = task == null ? "" : task.getTaskType();
|
||||
return TaskType.IMAGE_VIDEO_WORKFLOW.name().equals(taskType)
|
||||
|| TaskType.WORKFLOW_RESULT.name().equals(taskType);
|
||||
}
|
||||
|
||||
private void ensureTaskOwnedByCurrentInstance(ImageVideoAsyncTaskEntity task, String operation) {
|
||||
String owner = task == null ? null : task.getOwnerInstanceId();
|
||||
if (owner == null || owner.isBlank() || Objects.equals(owner, currentInstanceId())) {
|
||||
|
||||
Reference in New Issue
Block a user