Skip to content

πŸ“¨ The ultimate agentic social media scheduling tool πŸ€–

Fix AI video generation hanging forever when the provider reports failure

Gilad Resisib12c03f3454b
diff --git a/apps/frontend/src/components/launches/ai.video.tsx b/apps/frontend/src/components/launches/ai.video.tsx
index 7a8c93fca..ca54a0e56 100644
--- a/apps/frontend/src/components/launches/ai.video.tsx
+++ b/apps/frontend/src/components/launches/ai.video.tsx
@@ -60,8 +60,15 @@ export const Modal: FC<{
if (image.status == 200 || image.status == 201) {
onChange(await image.json());
+ } else {
+ toaster.show('Video generation failed', 'warning');
}
- } catch (e) {}
+ } catch (e) {
+ toaster.show(
+ 'Video generation failed or timed out β€” if it completes, it will appear in your media library',
+ 'warning'
+ );
+ }
setLocked(false);
setLoading(false);
diff --git a/libraries/nestjs-libraries/src/chat/tools/generate.video.tool.ts b/libraries/nestjs-libraries/src/chat/tools/generate.video.tool.ts
index f5d19f114..655821a60 100644
--- a/libraries/nestjs-libraries/src/chat/tools/generate.video.tool.ts
+++ b/libraries/nestjs-libraries/src/chat/tools/generate.video.tool.ts
@@ -55,26 +55,35 @@ export class GenerateVideoTool implements AgentToolInterface {
),
}),
outputSchema: z.object({
- url: z.string(),
+ url: z.string().optional(),
+ error: z.string().optional(),
}),
execute: async (inputData, context) => {
checkAuth(inputData, context);
const org = JSON.parse((context?.requestContext as any)?.get('organization') as string);
- const value = await this._mediaService.generateVideo(org, {
- type: inputData.identifier,
- output: inputData.output,
- customParams: inputData.customParams.reduce(
- (all: Record<string, any>, current: { key: string; value: any }) => ({
- ...all,
- [current.key]: current.value,
- }),
- {} as Record<string, any>
- ),
- });
+ try {
+ const value = await this._mediaService.generateVideo(org, {
+ type: inputData.identifier,
+ output: inputData.output,
+ customParams: inputData.customParams.reduce(
+ (all: Record<string, any>, current: { key: string; value: any }) => ({
+ ...all,
+ [current.key]: current.value,
+ }),
+ {} as Record<string, any>
+ ),
+ });
- return {
- url: value.path,
- };
+ return {
+ url: value.path,
+ };
+ } catch (err) {
+ return {
+ error: `Video generation failed: ${
+ err instanceof Error ? err.message : String(err)
+ }. The user's video credit was not used.`,
+ };
+ }
},
});
}
diff --git a/libraries/nestjs-libraries/src/videos/veo3/veo3.ts b/libraries/nestjs-libraries/src/videos/veo3/veo3.ts
index 84e19d39a..5adc896fe 100644
--- a/libraries/nestjs-libraries/src/videos/veo3/veo3.ts
+++ b/libraries/nestjs-libraries/src/videos/veo3/veo3.ts
@@ -48,6 +48,7 @@ export class Veo3 extends VideoAbstract<Veo3Params> {
Authorization: `Bearer ${process.env.KIEAI_API_KEY}`,
},
method: 'POST',
+ signal: AbortSignal.timeout(30000),
body: JSON.stringify({
prompt: customParams.prompt,
imageUrls: customParams?.images?.map((p) => p.path) || [],
@@ -62,8 +63,13 @@ export class Veo3 extends VideoAbstract<Veo3Params> {
}
const taskId = value.data.taskId;
- let videoUrl = [];
- while (videoUrl.length === 0) {
+ let attempts = 0;
+ const maxAttempts = 180; // ~30 minutes at 10s interval
+ while (true) {
+ if (attempts++ >= maxAttempts) {
+ throw new Error('Video generation timed out');
+ }
+
console.log('waiting for video to be ready');
const data = await (
await fetch(
@@ -73,18 +79,34 @@ export class Veo3 extends VideoAbstract<Veo3Params> {
'Content-Type': 'application/json',
Authorization: `Bearer ${process.env.KIEAI_API_KEY}`,
},
+ signal: AbortSignal.timeout(30000),
}
)
).json();
- if (data.code !== 200 && data.code !== 400) {
- throw new Error(`Failed to get video info`);
+ if (data.code !== 200) {
+ throw new Error(data?.msg || `Failed to get video info`);
+ }
+
+ // successFlag: 0 = generating, 1 = success, anything else = failed
+ const successFlag = data?.data?.successFlag;
+ if (successFlag !== 0 && successFlag !== 1) {
+ throw new Error(
+ data?.data?.errorMessage ||
+ `Video generation failed (status ${successFlag})`
+ );
+ }
+
+ const videoUrl = data?.data?.response?.resultUrls || [];
+ if (videoUrl.length > 0) {
+ return videoUrl[0];
+ }
+
+ if (successFlag === 1) {
+ throw new Error('Video generation succeeded but no video URL returned');
}
- videoUrl = data?.data?.response?.resultUrls || [];
await timer(10000);
}
-
- return videoUrl[0];
}
}