优化接口

This commit is contained in:
zhishi 2026-05-01 18:42:29 +08:00
parent 56aedee011
commit bd60524fde
4 changed files with 116 additions and 121 deletions

View File

@ -90,15 +90,15 @@ export default router.post(
})), })),
), ),
); );
const generateTask = async (item: (typeof storyboardData)[number]) => { const generateTask = async (item: (typeof storyboardData)[number]) => {
const repeloadObj = { const repeloadObj = {
prompt: item.prompt!, prompt: item.prompt!,
size: projectSettingData?.imageQuality as "1K" | "2K" | "4K", size: projectSettingData?.imageQuality as "1K" | "2K" | "4K",
aspectRatio: projectSettingData?.videoRatio as `${number}:${number}`, aspectRatio: projectSettingData?.videoRatio as `${number}:${number}`,
}; };
try {
await u.Ai.Image(projectSettingData?.imageModel as `${string}:${string}`) const imageCls = await u.Ai.Image(projectSettingData?.imageModel as `${string}:${string}`).run(
.run(
{ {
referenceList: await getAssetsImageBase64(assetRecord[item.id!] || []), referenceList: await getAssetsImageBase64(assetRecord[item.id!] || []),
...repeloadObj, ...repeloadObj,
@ -109,25 +109,22 @@ export default router.post(
relatedObjects: JSON.stringify(repeloadObj), relatedObjects: JSON.stringify(repeloadObj),
projectId: projectId, projectId: projectId,
}, },
) );
.then(async (imageCls) => {
const savePath = `/${projectId}/assets/${scriptId}/${u.uuid()}.jpg`; const savePath = `/${projectId}/assets/${scriptId}/${u.uuid()}.jpg`;
await imageCls.save(savePath); await imageCls.save(savePath);
await u.db("o_storyboard").where("id", item.id).update({ await u.db("o_storyboard").where("id", item.id).update({
filePath: savePath, filePath: savePath,
state: "已完成", state: "已完成",
}); });
}) } catch (e) {
.catch(async (e) => { u.db("o_storyboard")
await u
.db("o_storyboard")
.where("id", item.id) .where("id", item.id)
.update({ .update({
filePath: "", filePath: "",
reason: u.error(e).message, reason: u.error(e).message,
state: "生成失败", state: "生成失败",
}); });
}); }
}; };
// 按 concurrentCount 控制并发数,分批执行;跳过 shouldGenerateImage === 0 的分镜 // 按 concurrentCount 控制并发数,分批执行;跳过 shouldGenerateImage === 0 的分镜
let generateList = []; let generateList = [];

View File

@ -94,15 +94,8 @@ export default router.post(
); );
res.status(200).send(success(tasks.map((t) => ({ videoId: t.videoId, trackId: t.trackId })))); res.status(200).send(success(tasks.map((t) => ({ videoId: t.videoId, trackId: t.trackId }))));
for (const { videoId, videoPath, prompt, duration, images } of tasks) {
// 分批执行3个一批 // 所有任务全部并发后台执行,完全不阻塞任何进程
(async () => {
const batchSize = 3;
for (let i = 0; i < tasks.length; i += batchSize) {
const batch = tasks.slice(i, i + batchSize);
await Promise.all(
batch.map(async ({ videoId, videoPath, prompt, duration, images }) => {
// 调用时再转 base64
const base64 = await Promise.all( const base64 = await Promise.all(
images.map(async (item) => { images.map(async (item) => {
if (!item) return null; if (!item) return null;
@ -111,7 +104,7 @@ export default router.post(
); );
const relatedObjects = { projectId, videoId, scriptId, type: "视频" }; const relatedObjects = { projectId, videoId, scriptId, type: "视频" };
const aiVideo = u.Ai.Video(model); const aiVideo = u.Ai.Video(model);
return aiVideo aiVideo
.run( .run(
{ {
prompt, prompt,
@ -129,8 +122,8 @@ export default router.post(
relatedObjects: JSON.stringify(relatedObjects), relatedObjects: JSON.stringify(relatedObjects),
}, },
) )
.then(() => aiVideo.save(videoPath)) .then(async () => await aiVideo.save(videoPath))
.then(() => u.db("o_video").where("id", videoId).update({ state: "生成成功" })) .then(async () => await u.db("o_video").where("id", videoId).update({ state: "生成成功" }))
.catch(async (error: any) => { .catch(async (error: any) => {
await u await u
.db("o_video") .db("o_video")
@ -140,9 +133,6 @@ export default router.post(
errorReason: u.error(error).message, errorReason: u.error(error).message,
}); });
}); });
}),
);
} }
})();
}, },
); );

View File

@ -84,8 +84,6 @@ export default router.post(
videoTrackId: trackId, videoTrackId: trackId,
}); });
res.status(200).send(success(videoId)); res.status(200).send(success(videoId));
(async () => {
try {
const relatedObjects = { const relatedObjects = {
projectId, projectId,
videoId, videoId,
@ -93,9 +91,8 @@ export default router.post(
type: "视频", type: "视频",
}; };
const aiVideo = u.Ai.Video(model); const aiVideo = u.Ai.Video(model);
aiVideo
console.log("%c Line:47 🍩 modeData", "background:#93c0a4", modeData); .run(
await aiVideo.run(
{ {
prompt, prompt,
referenceList: base64.filter(Boolean) as ReferenceList[], referenceList: base64.filter(Boolean) as ReferenceList[],
@ -111,10 +108,10 @@ export default router.post(
describe: "根据提示词生成视频", describe: "根据提示词生成视频",
relatedObjects: JSON.stringify(relatedObjects), relatedObjects: JSON.stringify(relatedObjects),
}, },
); )
await aiVideo.save(videoPath); .then(async () => await aiVideo.save(videoPath))
await u.db("o_video").where("id", videoId).update({ state: "生成成功" }); .then(async () => await u.db("o_video").where("id", videoId).update({ state: "生成成功" }))
} catch (error: any) { .catch(async (error: any) => {
await u await u
.db("o_video") .db("o_video")
.where("id", videoId) .where("id", videoId)
@ -122,7 +119,6 @@ export default router.post(
state: "生成失败", state: "生成失败",
errorReason: u.error(error).message, errorReason: u.error(error).message,
}); });
} });
})();
}, },
); );

View File

@ -152,11 +152,12 @@ async function withTaskRecord<T>(
const taskRecord = await u.task(projectId, taskClass, model, { describe: describe, content: relatedObjects }); const taskRecord = await u.task(projectId, taskClass, model, { describe: describe, content: relatedObjects });
try { try {
const result = await fn(modelName, false, 0); const result = await fn(modelName, false, 0);
taskRecord(1); taskRecord(1);
return result; return result;
} catch (e) { } catch (e) {
taskRecord(-1, u.error(e).message); taskRecord(-1, u.error(e).message);
throw e; throw new Error(u.error(e).message);
} }
} }
@ -258,9 +259,11 @@ class AiImage {
return this; return this;
}; };
if (taskRecord) { if (taskRecord) {
return withTaskRecord(this.key, taskRecord.taskClass, taskRecord.describe, taskRecord.relatedObjects, taskRecord.projectId, exec); await withTaskRecord(this.key, taskRecord.taskClass, taskRecord.describe, taskRecord.relatedObjects, taskRecord.projectId, exec);
return this;
} }
return exec(modelName); await exec(modelName);
return this;
} }
async save(path: string) { async save(path: string) {
await u.oss.writeFile(path, this.result); await u.oss.writeFile(path, this.result);
@ -294,18 +297,24 @@ class AiVideo {
} }
async run(input: VideoConfig, taskRecord?: TaskRecord) { async run(input: VideoConfig, taskRecord?: TaskRecord) {
const modelName = await resolveModelName(this.key); const modelName = await resolveModelName(this.key);
try {
const exec = async (mn: `${string}:${string}`) => { const exec = async (mn: `${string}:${string}`) => {
const fn = await getVendorTemplateFn("videoRequest", mn); const fn = await getVendorTemplateFn("videoRequest", mn);
await referenceList2imageBase642(mn.split(/:(.+)/)[0], input); await referenceList2imageBase642(mn.split(/:(.+)/)[0], input);
this.result = await fn(input); this.result = await fn(input);
if (this.result.startsWith("http")) this.result = await urlToBase64(this.result); if (this.result.startsWith("http")) this.result = await urlToBase64(this.result);
return this;
}; };
if (taskRecord) { if (taskRecord) {
return withTaskRecord(this.key, taskRecord.taskClass, taskRecord.describe, taskRecord.relatedObjects, taskRecord.projectId, exec); await withTaskRecord(this.key, taskRecord.taskClass, taskRecord.describe, taskRecord.relatedObjects, taskRecord.projectId, exec);
return this;
}
await exec(modelName);
return this;
} catch (e) {
throw e;
} }
return exec(modelName);
} }
async save(path: string) { async save(path: string) {
await u.oss.writeFile(path, this.result); await u.oss.writeFile(path, this.result);
@ -321,16 +330,19 @@ class AiAudio {
async run(input: VideoConfig, taskRecord?: TaskRecord) { async run(input: VideoConfig, taskRecord?: TaskRecord) {
const modelName = await resolveModelName(this.key); const modelName = await resolveModelName(this.key);
const exec = async (mn: `${string}:${string}`) => { const exec = async (mn: `${string}:${string}`) => {
try {
const fn = await getVendorTemplateFn("ttsRequest", mn); const fn = await getVendorTemplateFn("ttsRequest", mn);
await referenceList2imageBase642(mn.split(/:(.+)/)[0], input); await referenceList2imageBase642(mn.split(/:(.+)/)[0], input);
this.result = await fn(input); this.result = await fn(input);
if (this.result.startsWith("http")) this.result = await urlToBase64(this.result); if (this.result.startsWith("http")) this.result = await urlToBase64(this.result);
return this; return this;
} catch (e) {}
}; };
if (taskRecord) { if (taskRecord) {
return withTaskRecord(this.key, taskRecord.taskClass, taskRecord.describe, taskRecord.relatedObjects, taskRecord.projectId, exec); return withTaskRecord(this.key, taskRecord.taskClass, taskRecord.describe, taskRecord.relatedObjects, taskRecord.projectId, exec);
} }
return exec(modelName); return await exec(modelName);
} }
async save(path: string) { async save(path: string) {
await u.oss.writeFile(path, this.result); await u.oss.writeFile(path, this.result);