feat: 移除图片生成记录中的尺寸字段,调整归档逻辑以支持异步处理
This commit is contained in:
@@ -1,8 +1,13 @@
|
||||
// server/api/images/generate.post.ts - 图片生成接口:创建记录、流式调用上游生图、归档图床并返回图片地址。
|
||||
// server/api/images/generate.post.ts - 图片生成接口:创建记录、流式调用上游生图并在响应后异步归档图床。
|
||||
import { setImmediate } from "node:timers";
|
||||
import type {
|
||||
IImageGenerateData,
|
||||
IImageGenerateRequest
|
||||
} from "#shared/types/openai";
|
||||
import {
|
||||
finishImageGenerationArchiveFailed,
|
||||
finishImageGenerationArchiveSuccess
|
||||
} from "~~/server/utils";
|
||||
|
||||
type ApiLogger = ReturnType<typeof createApiLogger>;
|
||||
|
||||
@@ -15,9 +20,9 @@ type ApiLogger = ReturnType<typeof createApiLogger>;
|
||||
* 3. 创建 RUNNING 生图记录,并递增全局请求/进行中统计。
|
||||
* 4. 在服务端确保并读取 AIArtStudio 完整 key,完整 key 不返回前端。
|
||||
* 5. 调用 Chat Completions 流式生图接口,累积 SSE delta content 并提取最终图片 URL。
|
||||
* 6. 尝试把上游图片下载后上传到 Lsky 图床;图床失败不阻断本次生图成功。
|
||||
* 7. 成功时写入上游 URL、图床 URL、完整上游响应、图床响应和耗时。
|
||||
* 8. 失败时把记录标记为 FAILED;鉴权失败会清理本地登录态并返回 401。
|
||||
* 6. 上游返回图片 URL 后立刻把生图结果落库为 SUCCEEDED,并马上返回前端。
|
||||
* 7. 响应返回后再用后台异步任务上传 Lsky;归档失败只补写记录和日志,不影响本次响应。
|
||||
* 8. 主链路失败时把记录标记为 FAILED;鉴权失败会清理本地登录态并返回 401。
|
||||
*/
|
||||
export default defineEventHandler(async (event) => {
|
||||
const logger = createApiLogger("images.generate");
|
||||
@@ -85,41 +90,32 @@ export default defineEventHandler(async (event) => {
|
||||
hasUsage: hasStreamUsage(result.upstreamResponse)
|
||||
});
|
||||
|
||||
stage = "upload_lsky";
|
||||
// 归档图床用于长期保存;失败时仍继续返回 NewAPI 上游图片 URL。
|
||||
const archiveResult = await archiveGeneratedImage({
|
||||
imageUrl: result.imageUrl,
|
||||
userId,
|
||||
recordId: record.id,
|
||||
createdAt: record.startedAt,
|
||||
logger
|
||||
});
|
||||
logger.info("图床归档完成", {
|
||||
recordId: record.id.toString(),
|
||||
hosted: Boolean(archiveResult.hostedImageUrl),
|
||||
mimeType: archiveResult.imageMimeType
|
||||
});
|
||||
|
||||
stage = "finish_success_record";
|
||||
await finishImageGenerationSuccess(record.id, record.startedAt, {
|
||||
imageUrl: result.imageUrl,
|
||||
hostedImageUrl: archiveResult.hostedImageUrl,
|
||||
imageMimeType: archiveResult.imageMimeType,
|
||||
revisedPrompt: result.revisedPrompt,
|
||||
upstreamResponse: result.upstreamResponse,
|
||||
hostedResponse: archiveResult.hostedResponse,
|
||||
errorMessage: archiveResult.errorMessage
|
||||
upstreamResponse: result.upstreamResponse
|
||||
});
|
||||
|
||||
logger.done("成功", {
|
||||
recordId: record.id.toString(),
|
||||
hosted: Boolean(archiveResult.hostedImageUrl)
|
||||
archiveScheduled: true
|
||||
});
|
||||
|
||||
const finishedRecord = record;
|
||||
setImmediate(() => {
|
||||
void archiveGeneratedImageInBackground({
|
||||
imageUrl: result.imageUrl,
|
||||
userId,
|
||||
recordId: finishedRecord.id,
|
||||
createdAt: finishedRecord.startedAt,
|
||||
logger
|
||||
});
|
||||
});
|
||||
|
||||
return createSuccessResponse<IImageGenerateData>(
|
||||
{
|
||||
imageUrl: result.imageUrl,
|
||||
hostedImageUrl: archiveResult.hostedImageUrl,
|
||||
revisedPrompt: result.revisedPrompt
|
||||
},
|
||||
"图片生成成功"
|
||||
@@ -154,8 +150,8 @@ export default defineEventHandler(async (event) => {
|
||||
}
|
||||
});
|
||||
|
||||
/** 将生成图上传到 Lsky,返回图床地址;归档失败时降级为空结果 */
|
||||
const archiveGeneratedImage = async ({
|
||||
/** 响应返回后异步归档图片,成功则补写图床字段,失败则补写本地错误文案 */
|
||||
const archiveGeneratedImageInBackground = async ({
|
||||
imageUrl,
|
||||
userId,
|
||||
recordId,
|
||||
@@ -167,14 +163,12 @@ const archiveGeneratedImage = async ({
|
||||
recordId: bigint;
|
||||
createdAt: Date;
|
||||
logger: ApiLogger;
|
||||
}): Promise<{
|
||||
hostedImageUrl: string | null;
|
||||
imageMimeType: string | null;
|
||||
hostedResponse: unknown;
|
||||
errorMessage: string | null;
|
||||
}> => {
|
||||
}) => {
|
||||
logger.info("后台归档开始", {
|
||||
recordId: recordId.toString()
|
||||
});
|
||||
|
||||
try {
|
||||
// 图床归档失败不能阻断本次生成结果,前端仍可使用上游图片 URL。
|
||||
const identity = await getUserArchiveIdentity(userId);
|
||||
const uploaded = await uploadImageFromUrl({
|
||||
imageUrl,
|
||||
@@ -184,24 +178,29 @@ const archiveGeneratedImage = async ({
|
||||
createdAt
|
||||
});
|
||||
|
||||
return {
|
||||
await finishImageGenerationArchiveSuccess(recordId, {
|
||||
hostedImageUrl: uploaded.publicUrl,
|
||||
imageMimeType: uploaded.mimetype,
|
||||
hostedResponse: uploaded.response,
|
||||
errorMessage: null
|
||||
};
|
||||
hostedResponse: uploaded.response
|
||||
});
|
||||
|
||||
logger.info("后台归档成功", {
|
||||
recordId: recordId.toString(),
|
||||
mimeType: uploaded.mimetype,
|
||||
hosted: true
|
||||
});
|
||||
} catch (error) {
|
||||
logger.error("图床归档失败", {
|
||||
logger.error("后台归档失败", {
|
||||
recordId: recordId.toString(),
|
||||
error: toSafeLogError(error)
|
||||
});
|
||||
|
||||
return {
|
||||
hostedImageUrl: null,
|
||||
imageMimeType: null,
|
||||
hostedResponse: null,
|
||||
errorMessage: "图片生成成功,但图片归档失败"
|
||||
};
|
||||
await finishImageGenerationArchiveFailed(recordId).catch((recordError) => {
|
||||
logger.error("更新归档失败记录失败", {
|
||||
recordId: recordId.toString(),
|
||||
error: toSafeLogError(recordError)
|
||||
});
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -12,7 +12,7 @@ const MAX_PAGE_SIZE = 50;
|
||||
* 1. 从 httpOnly cookie 读取当前用户 ID。
|
||||
* 2. 读取 page/size 查询参数,并限制最大 pageSize,避免一次查太多。
|
||||
* 3. 只查询当前用户且未软删除的记录,按创建时间倒序返回。
|
||||
* 4. 列表不返回完整上游响应或图床响应,详情接口再返回这些调试数据。
|
||||
* 4. 列表只返回页面展示所需字段,不返回内部响应或供应商调试数据。
|
||||
*/
|
||||
export default defineEventHandler(async (event) => {
|
||||
const logger = createApiLogger("images.history.list");
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// server/api/images/history/[id].get.ts - 生图历史详情接口:返回当前用户单条记录和完整响应。
|
||||
// server/api/images/history/[id].get.ts - 生图历史详情接口:返回当前用户单条记录的展示字段与归档状态。
|
||||
import type { IImageHistoryDetail } from "#shared/types/openai";
|
||||
import {
|
||||
clearNewApiAuthCookies,
|
||||
@@ -19,7 +19,7 @@ import {
|
||||
* 1. 从 httpOnly cookie 读取当前用户 ID。
|
||||
* 2. 校验路由参数 id 必须是数字,并转成 BigInt 查询。
|
||||
* 3. 只允许读取当前用户、未软删除的记录。
|
||||
* 4. 返回详情时包含完整上游响应和 Lsky 响应,便于排查单次生图问题。
|
||||
* 4. 详情接口继续隐藏内部响应,只返回页面展示和归档状态所需字段。
|
||||
*/
|
||||
export default defineEventHandler(async (event) => {
|
||||
const logger = createApiLogger("images.history.detail");
|
||||
|
||||
@@ -12,23 +12,23 @@ import { prisma } from "~~/server/utils/prisma";
|
||||
|
||||
const GLOBAL_STATS_ID = "global";
|
||||
const DEFAULT_IMAGE_MODEL = "gpt-image-2";
|
||||
const DEFAULT_IMAGE_SIZE = "1024x1024";
|
||||
|
||||
interface IFinishImageGenerationSuccessInput {
|
||||
/** NewAPI 上游返回的生成图片 URL */
|
||||
imageUrl: string;
|
||||
/** Lsky 图床归档后的图片 URL,归档失败时为空 */
|
||||
hostedImageUrl: string | null;
|
||||
/** 图片 MIME 类型,优先来自图床上传结果 */
|
||||
imageMimeType: string | null;
|
||||
/** 上游返回的修订提示词,流式生图通常为空 */
|
||||
revisedPrompt?: string | null;
|
||||
/** 完整上游响应,当前为 chat completions 流式聚合对象 */
|
||||
upstreamResponse: unknown;
|
||||
/** 完整 Lsky 上传响应,归档失败时为空 */
|
||||
}
|
||||
|
||||
interface IFinishImageGenerationArchiveSuccessInput {
|
||||
/** Lsky 图床归档后的图片 URL */
|
||||
hostedImageUrl: string;
|
||||
/** 图片 MIME 类型,优先来自图床上传结果 */
|
||||
imageMimeType: string | null;
|
||||
/** 完整 Lsky 上传响应 */
|
||||
hostedResponse: unknown;
|
||||
/** 成功状态下的非阻断提示,例如图床归档失败 */
|
||||
errorMessage?: string | null;
|
||||
}
|
||||
|
||||
/** 登录或恢复登录时保存 NewAPI 用户快照 */
|
||||
@@ -97,8 +97,7 @@ export const createRunningImageGeneration = async (
|
||||
userId,
|
||||
prompt,
|
||||
status: ImageGenerationStatus.RUNNING,
|
||||
model: DEFAULT_IMAGE_MODEL,
|
||||
size: DEFAULT_IMAGE_SIZE
|
||||
model: DEFAULT_IMAGE_MODEL
|
||||
}
|
||||
});
|
||||
|
||||
@@ -114,7 +113,7 @@ export const createRunningImageGeneration = async (
|
||||
return record;
|
||||
};
|
||||
|
||||
/** 将生图记录标记为成功,并保存上游 URL、图床 URL、完整响应和耗时 */
|
||||
/** 将生图记录标记为成功,并先写入上游结果;图床字段稍后由后台归档补写 */
|
||||
export const finishImageGenerationSuccess = async (
|
||||
recordId: bigint,
|
||||
startedAt: Date,
|
||||
@@ -131,15 +130,12 @@ export const finishImageGenerationSuccess = async (
|
||||
endedAt,
|
||||
durationMs: getDurationMs(startedAt, endedAt),
|
||||
imageUrl: input.imageUrl,
|
||||
hostedImageUrl: input.hostedImageUrl,
|
||||
imageMimeType: input.imageMimeType,
|
||||
hostedImageUrl: null,
|
||||
imageMimeType: null,
|
||||
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
|
||||
hostedResponse: Prisma.DbNull,
|
||||
errorMessage: null
|
||||
}
|
||||
});
|
||||
|
||||
@@ -186,6 +182,39 @@ export const finishImageGenerationFailed = async (
|
||||
});
|
||||
};
|
||||
|
||||
/** 后台归档成功后回写图床地址、MIME 和图床响应 */
|
||||
export const finishImageGenerationArchiveSuccess = async (
|
||||
recordId: bigint,
|
||||
input: IFinishImageGenerationArchiveSuccessInput
|
||||
) => {
|
||||
await prisma.imageGeneration.update({
|
||||
where: {
|
||||
id: recordId
|
||||
},
|
||||
data: {
|
||||
hostedImageUrl: input.hostedImageUrl,
|
||||
imageMimeType: input.imageMimeType,
|
||||
hostedResponse: input.hostedResponse as Prisma.InputJsonValue,
|
||||
errorMessage: null
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
/** 后台归档失败时仅补写本地错误文案,不影响已成功的生图状态 */
|
||||
export const finishImageGenerationArchiveFailed = async (
|
||||
recordId: bigint,
|
||||
errorMessage: string = "图片归档失败"
|
||||
) => {
|
||||
await prisma.imageGeneration.update({
|
||||
where: {
|
||||
id: recordId
|
||||
},
|
||||
data: {
|
||||
errorMessage
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
/** 查询当前用户未删除的生图历史列表,不返回完整上游/图床响应 */
|
||||
export const listImageGenerationHistory = async ({
|
||||
userId,
|
||||
@@ -221,7 +250,7 @@ export const listImageGenerationHistory = async ({
|
||||
};
|
||||
};
|
||||
|
||||
/** 查询当前用户单条生图历史详情,包含完整上游与图床响应 */
|
||||
/** 查询当前用户单条生图历史详情,内部响应字段固定隐藏为 null */
|
||||
export const getImageGenerationDetail = async (
|
||||
userId: number,
|
||||
recordId: bigint
|
||||
@@ -342,7 +371,6 @@ const mapImageGenerationItem = (record: {
|
||||
prompt: string;
|
||||
status: ImageGenerationStatus;
|
||||
model: string;
|
||||
size: string;
|
||||
startedAt: Date;
|
||||
endedAt: Date | null;
|
||||
durationMs: number | null;
|
||||
@@ -358,7 +386,6 @@ const mapImageGenerationItem = (record: {
|
||||
prompt: record.prompt,
|
||||
status: record.status,
|
||||
model: record.model,
|
||||
size: record.size,
|
||||
startedAt: record.startedAt.toISOString(),
|
||||
endedAt: record.endedAt?.toISOString() ?? null,
|
||||
durationMs: record.durationMs,
|
||||
|
||||
Reference in New Issue
Block a user