Skip to content

AI Agents & MCPs & AI Workflow Automation • (~400 MCP servers for AI agents) • AI Automation / AI Agent with MCPs • AI Workflows & AI Agents • MCPs for AI Agents

11.2 KB
Raw Blame
import { createAction, Property } from '@activepieces/pieces-framework';
import { vadooAiAuth } from '../auth';
import {
  httpClient,
  HttpMethod,
  propsValidation,
} from '@activepieces/pieces-common';
import { generatePodcastSchema } from '../schemas';
import { isEmpty } from '@activepieces/pieces-framework';

export const generatePodcast = createAction({
  auth: vadooAiAuth,
  name: 'generate_podcast',
  classification: 'WRITE',
  displayName: 'Generate Podcast',
  description: 'Generates a podcast-style video.',
  audience: 'both',
  aiMetadata: {
    description:
      'Generates a two-speaker podcast-style video on Vadoo from source content and waits (polls up to ~5 minutes) for it to finish, returning the completed video URL. The content source is selectable: either a website/PDF URL or custom text, and the matching field is required for the chosen source. Requires host and guest names; optional voices, theme, language, duration, and tone shape the output. Each call starts a new generation job, so it is not idempotent.',
    idempotent: false,
  },
  props: {
    content_source: Property.StaticDropdown({
      displayName: 'Content Source',
      description: 'Choose the source of content for the podcast',
      required: true,
      options: {
        options: [
          { label: 'Website/PDF URL', value: 'url' },
          { label: 'Custom Text', value: 'text' },
        ],
      },
    }),
    url: Property.ShortText({
      displayName: 'Website/PDF URL',
      description: 'URL of the website or PDF to create a podcast from',
      required: false,
    }),
    text: Property.LongText({
      displayName: 'Custom Content',
      description: 'Custom content for the podcast',
      required: false,
    }),
    name1: Property.ShortText({
      displayName: 'Host Name',
      description: 'The host name for AI Podcast',
      required: true,
    }),
    voice1: Property.Dropdown({
  auth: vadooAiAuth,
      displayName: 'Host Voice',
      description: 'The host voice for AI Podcast',
      required: false,
      refreshers: [],
      options: async ({ auth }) => {
        if (!auth) {
          return {
            disabled: true,
            options: [],
            placeholder: 'Please authenticate first',
          };
        }

        try {
          const response = await httpClient.sendRequest({
            method: HttpMethod.GET,
            url: 'https://viralapi.vadoo.tv/api/get_voices',
            headers: {
              'X-API-KEY': auth.secret_text,
            },
            timeout: 10000, // 10 second timeout
          });

          if (response.body && Array.isArray(response.body)) {
            const voices = response.body as string[];
            return {
              disabled: false,
              options: voices.map((voice) => ({
                label: voice,
                value: voice,
              })),
            };
          } else {
            // Fallback to default voices if API response is unexpected
            return {
              disabled: false,
              options: [
                { label: 'Charlie', value: 'Charlie' },
                { label: 'George', value: 'George' },
                { label: 'Callum', value: 'Callum' },
                { label: 'Sarah', value: 'Sarah' },
                { label: 'Laura', value: 'Laura' },
                { label: 'Charlotte', value: 'Charlotte' },
              ],
            };
          }
        } catch (error) {
          // Fallback to default voices on error
          return {
            disabled: false,
            options: [
              { label: 'Charlie', value: 'Charlie' },
              { label: 'George', value: 'George' },
              { label: 'Callum', value: 'Callum' },
              { label: 'Sarah', value: 'Sarah' },
              { label: 'Laura', value: 'Laura' },
              { label: 'Charlotte', value: 'Charlotte' },
            ],
          };
        }
      },
    }),
    name2: Property.ShortText({
      displayName: 'Guest Name',
      description: 'The guest name for AI Podcast',
      required: true,
    }),
    voice2: Property.Dropdown({
  auth: vadooAiAuth,
      displayName: 'Guest Voice',
      description: 'The guest voice for AI Podcast',
      required: false,
      refreshers: [],
      options: async ({ auth }) => {
        if (!auth) {
          return {
            disabled: true,
            options: [],
            placeholder: 'Please authenticate first',
          };
        }

        try {
          const response = await httpClient.sendRequest({
            method: HttpMethod.GET,
            url: 'https://viralapi.vadoo.tv/api/get_voices',
            headers: {
              'X-API-KEY': auth.secret_text,
            },
            timeout: 10000, // 10 second timeout
          });

          if (response.body && Array.isArray(response.body)) {
            const voices = response.body as string[];
            return {
              disabled: false,
              options: voices.map((voice) => ({
                label: voice,
                value: voice,
              })),
            };
          } else {
            // Fallback to default voices if API response is unexpected
            return {
              disabled: false,
              options: [
                { label: 'Charlie', value: 'Charlie' },
                { label: 'George', value: 'George' },
                { label: 'Callum', value: 'Callum' },
                { label: 'Sarah', value: 'Sarah' },
                { label: 'Laura', value: 'Laura' },
                { label: 'Charlotte', value: 'Charlotte' },
              ],
            };
          }
        } catch (error) {
          // Fallback to default voices on error
          return {
            disabled: false,
            options: [
              { label: 'Charlie', value: 'Charlie' },
              { label: 'George', value: 'George' },
              { label: 'Callum', value: 'Callum' },
              { label: 'Sarah', value: 'Sarah' },
              { label: 'Laura', value: 'Laura' },
              { label: 'Charlotte', value: 'Charlotte' },
            ],
          };
        }
      },
    }),
    theme: Property.Dropdown({
  auth: vadooAiAuth,
      displayName: 'Theme',
      description: 'To display captions with style',
      required: false,
      refreshers: [],
      options: async ({ auth }) => {
        if (!auth) {
          return {
            disabled: true,
            options: [],
            placeholder: 'Please authenticate first',
          };
        }

        try {
          const response = await httpClient.sendRequest({
            method: HttpMethod.GET,
            url: 'https://viralapi.vadoo.tv/api/get_themes',
            headers: {
              'X-API-KEY': auth.secret_text,
            },
          });

          const themes = response.body as string[];
          return {
            disabled: false,
            options: themes.map((theme) => ({
              label: theme,
              value: theme,
            })),
          };
        } catch (error) {
          return {
            disabled: true,
            options: [],
            placeholder: 'Failed to load themes',
          };
        }
      },
    }),
    language: Property.Dropdown({
  auth: vadooAiAuth,
      displayName: 'Language',
      description: 'To generate video in language you want',
      required: false,
      refreshers: [],
      options: async ({ auth }) => {
        if (!auth) {
          return {
            disabled: true,
            options: [],
            placeholder: 'Please authenticate first',
          };
        }

        try {
          const response = await httpClient.sendRequest({
            method: HttpMethod.GET,
            url: 'https://viralapi.vadoo.tv/api/get_languages',
            headers: {
              'X-API-KEY': auth.secret_text,
            },
          });

          const languages = response.body as string[];
          return {
            disabled: false,
            options: languages.map((language) => ({
              label: language,
              value: language,
            })),
          };
        } catch (error) {
          return {
            disabled: true,
            options: [],
            placeholder: 'Failed to load languages',
          };
        }
      },
    }),
    duration: Property.StaticDropdown({
      displayName: 'Duration',
      description: 'Podcast Duration in minutes',
      required: false,
      defaultValue: '1-2',
      options: {
        options: [
          { label: '1-2 minutes', value: '1-2' },
          { label: '3-5 minutes', value: '3-5' },
        ],
      },
    }),
    tone: Property.ShortText({
      displayName: 'Tone',
      description: 'Tone of the Podcast',
      required: false,
      defaultValue: 'Friendly',
    }),
  },
  async run(context) {
    // Validate props with Zod schema
    await propsValidation.validateZod(
      context.propsValue,
      generatePodcastSchema
    );

    const {
      content_source,
      url,
      text,
      name1,
      voice1,
      name2,
      voice2,
      theme,
      language,
      duration,
      tone,
    } = context.propsValue;

    // Validate content source requirements
    if (content_source === 'url' && !url) {
      throw new Error(
        "URL is required when content source is 'Website/PDF URL'"
      );
    }
    if (content_source === 'text' && !text) {
      throw new Error(
        "Custom text is required when content source is 'Custom Text'"
      );
    }

    // Build request body, only including non-empty values
    const requestBody: Record<string, any> = {
      name1: name1,
      name2: name2,
    };

    if (url && content_source === 'url') requestBody['url'] = url;
    if (text && content_source === 'text') requestBody['text'] = text;
    if (voice1) requestBody['voice1'] = voice1;
    if (voice2) requestBody['voice2'] = voice2;
    if (theme) requestBody['theme'] = theme;
    if (language) requestBody['language'] = language;
    if (duration) requestBody['duration'] = duration;
    if (tone) requestBody['tone'] = tone;

    const response = await httpClient.sendRequest<{ vid: number }>({
      method: HttpMethod.POST,
      url: 'https://viralapi.vadoo.tv/api/generate_podcast',
      headers: {
        'X-API-KEY': context.auth.secret_text,
        'Content-Type': 'application/json',
      },
      body: requestBody,
    });

    if (isEmpty(response.body) || isEmpty(response.body.vid)) {
      throw new Error('Failed to generate podcast.');
    }

    const videoId = response.body.vid;
    let status = 'pending';
    const timeoutAt = Date.now() + 5 * 60 * 1000;

    do {
      await new Promise((resolve) => setTimeout(resolve, 5000));
      const pollRes = await httpClient.sendRequest<{
        url: string;
        status: string;
      }>({
        method: HttpMethod.GET,
        url: 'https://viralapi.vadoo.tv/api/get_video_url',
        headers: {
          'X-API-KEY': context.auth.secret_text,
          'Content-Type': 'application/json',
        },
        queryParams: {
          id: videoId.toString(),
        },
      });

      status = pollRes.body.status;
      if (status === 'complete') return pollRes.body;
    } while (status !== 'complete' && Date.now() < timeoutAt);

    throw new Error('Generate Podcast timed out or failed.');
  },
});