Skip to content

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

feat: failures

Nevo David342f7003c447
diff --git a/libraries/nestjs-libraries/src/openai/fal.service.ts b/libraries/nestjs-libraries/src/openai/fal.service.ts
index 0b6b17466..04d5cb9ad 100644
--- a/libraries/nestjs-libraries/src/openai/fal.service.ts
+++ b/libraries/nestjs-libraries/src/openai/fal.service.ts
@@ -10,25 +10,32 @@ export class FalService {
text: string,
isVertical: boolean = false
): Promise<string> {
- const { images, video, ...all } = await (
- await limit(() =>
- fetch(`https://fal.run/fal-ai/${model}`, {
- method: 'POST',
- headers: {
- Authorization: `Key ${process.env.FAL_KEY}`,
- 'Content-Type': 'application/json',
- },
- body: JSON.stringify({
- prompt: text,
- aspect_ratio: isVertical ? '9:16' : '16:9',
- resolution: '720p',
- num_images: 1,
- output_format: 'jpeg',
- expand_prompt: true,
- }),
- })
- )
- ).json();
+ const response = await limit(() =>
+ fetch(`https://fal.run/fal-ai/${model}`, {
+ method: 'POST',
+ headers: {
+ Authorization: `Key ${process.env.FAL_KEY}`,
+ 'Content-Type': 'application/json',
+ },
+ body: JSON.stringify({
+ prompt: text,
+ aspect_ratio: isVertical ? '9:16' : '16:9',
+ resolution: '720p',
+ num_images: 1,
+ output_format: 'jpeg',
+ expand_prompt: true,
+ }),
+ signal: AbortSignal.timeout(300000),
+ })
+ );
+
+ if (!response.ok) {
+ throw new Error(
+ `fal ${response.status}: ${(await response.text()).slice(0, 500)}`
+ );
+ }
+
+ const { images, video, ...all } = await response.json();
console.log(all, video, images);
diff --git a/libraries/nestjs-libraries/src/openai/generation.error.ts b/libraries/nestjs-libraries/src/openai/generation.error.ts
index 6493cc3e0..05d9b644f 100644
--- a/libraries/nestjs-libraries/src/openai/generation.error.ts
+++ b/libraries/nestjs-libraries/src/openai/generation.error.ts
@@ -38,5 +38,7 @@ export function generationError(err: any): HttpException {
// Not a recognized safety rejection (e.g. an invalid-parameter 400) β€” return
// a generic message rather than mislabeling it as a content-safety issue.
+ // The real reason (a quota, a bad key, ...) is only useful to the operator, so log it
+ console.error('AI generation failed:', message);
return new HttpException('AI generation failed, please try again later.', 500);
}
diff --git a/libraries/nestjs-libraries/src/videos/images-slides/images.slides.ts b/libraries/nestjs-libraries/src/videos/images-slides/images.slides.ts
index 688d1efda..8d05342e0 100644
--- a/libraries/nestjs-libraries/src/videos/images-slides/images.slides.ts
+++ b/libraries/nestjs-libraries/src/videos/images-slides/images.slides.ts
@@ -23,6 +23,17 @@ const transloadit = new Transloadit({
authSecret: process.env.TRANSLOADIT_SECRET || 'just empty text',
});
+// ElevenLabs reports quota and permission problems as 401 with the reason in
+// the body, as either { detail: { status, message } } or { detail: 'text' }
+async function elevenLabsError(response: Response) {
+ const { detail } = await response.json().catch(() => ({ detail: undefined }));
+ const reason =
+ typeof detail === 'string'
+ ? detail
+ : detail?.message || detail?.status || response.statusText;
+ return `ElevenLabs ${response.status}: ${reason}`;
+}
+
async function getAudioDuration(buffer: Buffer): Promise<number> {
const metadata = await parseBuffer(buffer, 'audio/mpeg');
return metadata.format.duration || 0;
@@ -75,44 +86,48 @@ export class ImagesSlides extends VideoAbstract<ImagesSlidesParams> {
customParams.prompt
);
+ // Plain async calls so a failed image or voice request rejects Promise.all
+ // and fails the job, instead of a promise that never settles and a job that
+ // hangs until the workflow times out
const generated = await Promise.all(
list.reduce((all, current) => {
all.push(
- new Promise(async (res) => {
- res({
- len: 0,
- url: await this._falService.generateImageFromText(
- 'ideogram/v2',
- current.imagePrompt,
- output === 'vertical'
- ),
- });
- })
+ (async () => ({
+ len: 0,
+ url: await this._falService.generateImageFromText(
+ 'ideogram/v2',
+ current.imagePrompt,
+ output === 'vertical'
+ ),
+ }))()
);
all.push(
- new Promise(async (res) => {
- const buffer = Buffer.from(
- await (
- await limit(() =>
- fetch(
- `https://api.elevenlabs.io/v1/text-to-speech/${customParams.voice}?output_format=mp3_44100_128`,
- {
- method: 'POST',
- headers: {
- 'Content-Type': 'application/json',
- 'xi-api-key': process.env.ELEVENSLABS_API_KEY || '',
- },
- body: JSON.stringify({
- text: current.voiceText,
- model_id: 'eleven_multilingual_v2',
- }),
- }
- )
- )
- ).arrayBuffer()
+ (async () => {
+ const response = await limit(() =>
+ fetch(
+ `https://api.elevenlabs.io/v1/text-to-speech/${customParams.voice}?output_format=mp3_44100_128`,
+ {
+ method: 'POST',
+ headers: {
+ 'Content-Type': 'application/json',
+ 'xi-api-key': process.env.ELEVENSLABS_API_KEY || '',
+ },
+ body: JSON.stringify({
+ text: current.voiceText,
+ model_id: 'eleven_multilingual_v2',
+ }),
+ signal: AbortSignal.timeout(60000),
+ }
+ )
);
+ if (!response.ok) {
+ throw new Error(await elevenLabsError(response));
+ }
+
+ const buffer = Buffer.from(await response.arrayBuffer());
+
const { path } = await this.storage.uploadFile({
buffer,
mimetype: 'audio/mp3',
@@ -126,7 +141,7 @@ export class ImagesSlides extends VideoAbstract<ImagesSlidesParams> {
encoding: '',
});
- res({
+ return {
len: await getAudioDuration(buffer),
url:
path.indexOf('http') === -1
@@ -135,8 +150,8 @@ export class ImagesSlides extends VideoAbstract<ImagesSlidesParams> {
process.env.NEXT_PUBLIC_UPLOAD_STATIC_DIRECTORY +
path
: path,
- });
- })
+ };
+ })()
);
return all;
@@ -172,6 +187,7 @@ export class ImagesSlides extends VideoAbstract<ImagesSlidesParams> {
'subtitles.srt': srt,
},
waitForCompletion: true,
+ timeout: 30 * 60 * 1000,
params: {
steps: {
...split.reduce((all, current, index) => {
@@ -242,18 +258,23 @@ export class ImagesSlides extends VideoAbstract<ImagesSlidesParams> {
@ExposeVideoFunction()
async loadVoices(data: any) {
- const { voices } = await (
- await fetch(
- 'https://api.elevenlabs.io/v2/voices?page_size=40&category=premade',
- {
- method: 'GET',
- headers: {
- 'Content-Type': 'application/json',
- 'xi-api-key': process.env.ELEVENSLABS_API_KEY || '',
- },
- }
- )
- ).json();
+ const response = await fetch(
+ 'https://api.elevenlabs.io/v2/voices?page_size=40&category=premade',
+ {
+ method: 'GET',
+ headers: {
+ 'Content-Type': 'application/json',
+ 'xi-api-key': process.env.ELEVENSLABS_API_KEY || '',
+ },
+ signal: AbortSignal.timeout(30000),
+ }
+ );
+
+ if (!response.ok) {
+ throw new Error(await elevenLabsError(response));
+ }
+
+ const { voices } = await response.json();
return {
voices: voices.map((voice: any) => ({