fix(create): 文本生成任务 - 恢复终态正文并停止错误的生成中状态
This commit is contained in:
1 parent
35dee568c9
commit
d7a7397d28
4 files changed
+109
-10
No files matched your search
@@ -0,0 +1,43 @@
|
||||
import { parseBackendGenerationResult } from "@/services/api/generation-task";
|
||||
import type { GenerationTask } from "@/services/api/task-center";
|
||||
|
||||
export type RecoverableCreationTextMessage = {
|
||||
mode?: string;
|
||||
status?: string;
|
||||
content: string;
|
||||
error?: string;
|
||||
taskIds?: string[];
|
||||
};
|
||||
|
||||
export type CreationTextTaskRecovery = {
|
||||
status: "done" | "error" | "cancelled";
|
||||
content: string;
|
||||
error?: string;
|
||||
taskIds: string[];
|
||||
};
|
||||
|
||||
// 页面重载后只依赖后端任务终态恢复消息,不复用已经中断的浏览器请求。
|
||||
export function recoverCreationTextTask(message: RecoverableCreationTextMessage, tasks: GenerationTask[]): CreationTextTaskRecovery | null {
|
||||
if (message.mode !== "text" || (message.status !== "streaming" && message.status !== "pending")) return null;
|
||||
|
||||
const taskIds = new Set(message.taskIds || []);
|
||||
const matches = tasks.filter((task) => taskIds.has(task.id));
|
||||
if (!matches.length || matches.some((task) => task.status === "queued" || task.status === "running")) return null;
|
||||
|
||||
const nextTaskIds = Array.from(new Set([...(message.taskIds || []), ...matches.map((task) => task.id)]));
|
||||
const succeeded = matches.find((task) => task.status === "succeeded");
|
||||
if (succeeded) {
|
||||
try {
|
||||
const text = parseBackendGenerationResult(succeeded).text;
|
||||
if (!text?.trim()) throw new Error("后端任务没有返回文本");
|
||||
return { status: "done", content: text, error: undefined, taskIds: nextTaskIds };
|
||||
} catch (error) {
|
||||
return { status: "error", content: "生成失败", error: error instanceof Error ? error.message : "文本任务结果格式错误", taskIds: nextTaskIds };
|
||||
}
|
||||
}
|
||||
if (matches.every((task) => task.status === "cancelled")) {
|
||||
return { status: "cancelled", content: "已停止", error: undefined, taskIds: nextTaskIds };
|
||||
}
|
||||
const failed = matches.find((task) => task.status === "failed");
|
||||
return { status: "error", content: "生成失败", error: failed?.error || "文本任务已失败", taskIds: nextTaskIds };
|
||||
}
|
||||
Reference in new issue
Block a user