48 lines
1.7 KiB
TypeScript
48 lines
1.7 KiB
TypeScript
import { saveMessages } from "@/lib/db/queries";
|
|
import type { ImageGenerationStatusStreamPayload } from "@/lib/canvas/image-generation-status";
|
|
import { formatRequestFailure, getRequestFailureDetails } from "@/lib/request-error";
|
|
import { safeWriteStreamChunk, type StreamWriter, writeAssistantTextChunk, writeStructuredDataChunk } from "./stream";
|
|
|
|
export async function reportImageGenerationFailure(params: {
|
|
writer: StreamWriter;
|
|
chatId: string;
|
|
userMessageId: string;
|
|
messageId: string;
|
|
error: unknown;
|
|
label: string;
|
|
}) {
|
|
const failure = getRequestFailureDetails(params.error, "图片生成失败");
|
|
const status: ImageGenerationStatusStreamPayload = {
|
|
phase: "failed",
|
|
userMessageId: params.userMessageId,
|
|
requestId: params.messageId,
|
|
failure,
|
|
};
|
|
const text = formatRequestFailure(failure, params.messageId);
|
|
const parts = [
|
|
{ type: "text", text },
|
|
{ type: "data-imageGenerationStatus", data: status },
|
|
];
|
|
writeStructuredDataChunk(params.writer, "data-imageGenerationStatus", status, `${params.label}:status`);
|
|
safeWriteStreamChunk(params.writer, {
|
|
type: "data-appendMessage",
|
|
data: JSON.stringify({ id: params.messageId, role: "assistant", parts }),
|
|
}, params.label);
|
|
writeAssistantTextChunk(params.writer, "<!-- IMAGE_GENERATION_END -->", `${params.label}:end`);
|
|
// History carries the same terminal state when the browser loses the stream.
|
|
await saveMessages({ messages: [{
|
|
id: params.messageId,
|
|
chatId: params.chatId,
|
|
role: "assistant",
|
|
parts,
|
|
attachments: [],
|
|
createdAt: new Date(),
|
|
videoStorageStatus: null,
|
|
temporaryVideoUrl: null,
|
|
permanentVideoUrl: null,
|
|
archivedAt: null,
|
|
videoMetadata: null,
|
|
}] });
|
|
return text;
|
|
}
|