feat: 多功能更新
This commit is contained in:
@@ -14,17 +14,19 @@ const DEFAULT_IMAGE_MODEL = "gpt-image-2";
|
||||
const DEFAULT_IMAGE_SIZE = "1024x1024";
|
||||
|
||||
interface IFinishImageGenerationSuccessInput {
|
||||
/** 生成图片 URL */
|
||||
/** NewAPI 上游返回的生成图片 URL */
|
||||
imageUrl: string;
|
||||
/** 生成图片 base64 原文 */
|
||||
imageBase64: string | null;
|
||||
/** 图片 MIME 类型 */
|
||||
/** Lsky 图床归档后的图片 URL,归档失败时为空 */
|
||||
hostedImageUrl: string | null;
|
||||
/** 图片 MIME 类型,优先来自图床上传结果 */
|
||||
imageMimeType: string | null;
|
||||
/** 上游返回的修订提示词 */
|
||||
/** 上游返回的修订提示词,流式生图通常为空 */
|
||||
revisedPrompt?: string | null;
|
||||
/** 完整上游响应 */
|
||||
/** 完整上游响应,当前为 chat completions 流式聚合对象 */
|
||||
upstreamResponse: unknown;
|
||||
/** 成功状态下的非阻断提示,例如 base64 保存失败 */
|
||||
/** 完整 Lsky 上传响应,归档失败时为空 */
|
||||
hostedResponse: unknown;
|
||||
/** 成功状态下的非阻断提示,例如图床归档失败 */
|
||||
errorMessage?: string | null;
|
||||
}
|
||||
|
||||
@@ -65,6 +67,23 @@ export const ensureUserRecord = (userId: number) => {
|
||||
});
|
||||
};
|
||||
|
||||
/** 读取用于 Lsky 文件命名的用户标识,缺失时由调用方使用兜底名 */
|
||||
export const getUserArchiveIdentity = async (userId: number) => {
|
||||
const user = await prisma.user.findUnique({
|
||||
where: {
|
||||
id: userId
|
||||
},
|
||||
select: {
|
||||
username: true,
|
||||
displayName: true
|
||||
}
|
||||
});
|
||||
|
||||
return {
|
||||
username: user?.username || user?.displayName || "user"
|
||||
};
|
||||
};
|
||||
|
||||
/** 创建进行中的生图记录,并递增全局请求与进行中统计 */
|
||||
export const createRunningImageGeneration = async (
|
||||
userId: number,
|
||||
@@ -94,56 +113,34 @@ export const createRunningImageGeneration = async (
|
||||
return record;
|
||||
};
|
||||
|
||||
/** 将生图记录标记为成功,并保存 URL、base64、完整上游响应和耗时 */
|
||||
/** 将生图记录标记为成功,并保存上游 URL、图床 URL、完整响应和耗时 */
|
||||
export const finishImageGenerationSuccess = async (
|
||||
recordId: bigint,
|
||||
startedAt: Date,
|
||||
input: IFinishImageGenerationSuccessInput
|
||||
) => {
|
||||
const endedAt = new Date();
|
||||
const successData = {
|
||||
status: ImageGenerationStatus.SUCCEEDED,
|
||||
endedAt,
|
||||
durationMs: getDurationMs(startedAt, endedAt),
|
||||
imageUrl: input.imageUrl,
|
||||
imageBase64: input.imageBase64,
|
||||
imageMimeType: input.imageMimeType,
|
||||
revisedPrompt: input.revisedPrompt || null,
|
||||
upstreamResponse: input.upstreamResponse as Prisma.InputJsonValue,
|
||||
errorMessage: input.errorMessage || null
|
||||
};
|
||||
|
||||
try {
|
||||
await prisma.imageGeneration.update({
|
||||
where: {
|
||||
id: recordId
|
||||
},
|
||||
data: successData
|
||||
});
|
||||
} catch (error) {
|
||||
if (!input.imageBase64) {
|
||||
throw error;
|
||||
await prisma.imageGeneration.update({
|
||||
where: {
|
||||
id: recordId
|
||||
},
|
||||
data: {
|
||||
status: ImageGenerationStatus.SUCCEEDED,
|
||||
endedAt,
|
||||
durationMs: getDurationMs(startedAt, endedAt),
|
||||
imageUrl: input.imageUrl,
|
||||
hostedImageUrl: input.hostedImageUrl,
|
||||
imageMimeType: input.imageMimeType,
|
||||
revisedPrompt: input.revisedPrompt || null,
|
||||
upstreamResponse: input.upstreamResponse as Prisma.InputJsonValue,
|
||||
hostedResponse:
|
||||
input.hostedResponse === null
|
||||
? Prisma.DbNull
|
||||
: (input.hostedResponse as Prisma.InputJsonValue),
|
||||
errorMessage: input.errorMessage || null
|
||||
}
|
||||
|
||||
consola.error("[imageGenerationRecords] 保存带 base64 的成功记录失败,重试仅保存 URL", {
|
||||
recordId: recordId.toString(),
|
||||
message: error instanceof Error ? error.message : String(error)
|
||||
});
|
||||
|
||||
await prisma.imageGeneration.update({
|
||||
where: {
|
||||
id: recordId
|
||||
},
|
||||
data: {
|
||||
...successData,
|
||||
imageBase64: null,
|
||||
errorMessage: mergeRecordMessages(
|
||||
input.errorMessage,
|
||||
"图片生成成功,但 base64 归档保存失败"
|
||||
)
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
await safeUpsertGenerationStats({
|
||||
successRequests: {
|
||||
@@ -188,7 +185,7 @@ export const finishImageGenerationFailed = async (
|
||||
});
|
||||
};
|
||||
|
||||
/** 查询当前用户未删除的生图历史列表,不返回 base64 和完整上游响应 */
|
||||
/** 查询当前用户未删除的生图历史列表,不返回完整上游/图床响应 */
|
||||
export const listImageGenerationHistory = async ({
|
||||
userId,
|
||||
page,
|
||||
@@ -223,7 +220,7 @@ export const listImageGenerationHistory = async ({
|
||||
};
|
||||
};
|
||||
|
||||
/** 查询当前用户单条生图历史详情 */
|
||||
/** 查询当前用户单条生图历史详情,包含完整上游与图床响应 */
|
||||
export const getImageGenerationDetail = async (
|
||||
userId: number,
|
||||
recordId: bigint
|
||||
@@ -240,9 +237,9 @@ export const getImageGenerationDetail = async (
|
||||
|
||||
return {
|
||||
...mapImageGenerationItem(record),
|
||||
imageBase64: record.imageBase64,
|
||||
imageMimeType: record.imageMimeType,
|
||||
upstreamResponse: record.upstreamResponse
|
||||
upstreamResponse: record.upstreamResponse,
|
||||
hostedResponse: record.hostedResponse
|
||||
};
|
||||
};
|
||||
|
||||
@@ -345,6 +342,7 @@ const mapImageGenerationItem = (record: {
|
||||
endedAt: Date | null;
|
||||
durationMs: number | null;
|
||||
imageUrl: string | null;
|
||||
hostedImageUrl: string | null;
|
||||
revisedPrompt: string | null;
|
||||
errorMessage: string | null;
|
||||
createdAt: Date;
|
||||
@@ -360,6 +358,7 @@ const mapImageGenerationItem = (record: {
|
||||
endedAt: record.endedAt?.toISOString() ?? null,
|
||||
durationMs: record.durationMs,
|
||||
imageUrl: record.imageUrl,
|
||||
hostedImageUrl: record.hostedImageUrl,
|
||||
revisedPrompt: record.revisedPrompt,
|
||||
errorMessage: record.errorMessage,
|
||||
createdAt: record.createdAt.toISOString()
|
||||
@@ -370,13 +369,6 @@ const getDurationMs = (startedAt: Date, endedAt: Date) => {
|
||||
return Math.max(0, endedAt.getTime() - startedAt.getTime());
|
||||
};
|
||||
|
||||
const mergeRecordMessages = (
|
||||
currentMessage: string | null | undefined,
|
||||
nextMessage: string
|
||||
) => {
|
||||
return currentMessage ? `${currentMessage}; ${nextMessage}` : nextMessage;
|
||||
};
|
||||
|
||||
const getSafeErrorMessage = (error: unknown): string => {
|
||||
if (error instanceof Error) return error.message;
|
||||
if (typeof error === "string") return error;
|
||||
|
||||
Reference in New Issue
Block a user