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

4.5 KB
Raw Blame
import { straicoAuth } from '../auth';
import { createAction, Property } from '@activepieces/pieces-framework';
import {
  AuthenticationType,
  HttpMethod,
  httpClient,
} from '@activepieces/pieces-common';

import { baseUrlv0, baseUrlv1 } from '../common/common';

export const imageGeneration = createAction({
  audience: 'both',
  auth: straicoAuth,
  name: 'image_generation',
  displayName: 'Image Generation',
  description: 'Enables users to generate high-quality images based on textual descriptions.',
  aiMetadata: { description: 'Generates one to four new images from a text description using a Straico image model such as DALL-E 3, in square, landscape or portrait shape, and returns their hosted URLs plus a zip. This is the only image-producing action in the piece; use Ask AI instead when the goal is text, including describing or reasoning about an image that has already been uploaded. Requires a model, a size, an image count and a detailed description; each generated image is billed separately. Not idempotent: every call renders fresh images even for an identical prompt.', idempotent: false },
  props: {
    variations: Property.StaticDropdown({
      displayName: 'Number of Images',
      required: true,
      description:
        'Number of images to generate.',
      defaultValue: 1,
      options: {
        disabled: false,
        options: [
          { value: 1, label: '1' },
          { value: 2, label: '2' },
          { value: 3, label: '3' },
          { value: 4, label: '4' },
        ],
      },
    }),
    model: Property.Dropdown({
  auth: straicoAuth,

      displayName: 'Model',
      required: true,
      description: 'Select the image generation model.',
      defaultValue: 'openai/dall-e-3',
      refreshers: ['auth'],
      refreshOnSearch: true,
      options: async ({ auth }, { searchValue }) => {
        if (!auth) {
          return {
            disabled: true,
            options: [],
          };
        }
        try {
          const response = await httpClient.sendRequest<{
            data: { image?: { name?: string; model: string }[] };
            success: boolean;
          }>({
            url: `${baseUrlv1}/models`,
            method: HttpMethod.GET,
            authentication: {
              type: AuthenticationType.BEARER_TOKEN,
              token: auth.secret_text,
            },
          });

          const models = response.body?.data?.image ?? [];
          const filtered = searchValue
            ? models.filter((m) => {
                const term = String(searchValue).toLowerCase();
                return (
                  (m.name ?? '').toLowerCase().includes(term) ||
                  m.model.toLowerCase().includes(term)
                );
              })
            : models;

          return {
            disabled: false,
            options: filtered.map((m) => ({
              label: m.name ?? m.model,
              value: m.model,
            })),
          };
        } catch (e) {
          return {
            disabled: true,
            options: [],
            placeholder: 'Failed to load models. Check API key and try again.',
          };
        }
      },
    }),
    size: Property.StaticDropdown({
      displayName: 'Image Dimensions',
      required: true,
      description:
        'The desired image dimensions.',
      defaultValue: 'square',
      options: {
        disabled: false,
        options: [
          { value: 'square', label: 'square' },
          { value: 'landscape', label: 'landscape' },
          { value: 'portrait', label: 'portrait' }
        ],
      },
    }),
    description: Property.LongText({
      displayName: 'Description',
      required: true,
      description:
      'A detailed textual description of the image to be generated.',
    }),
  },
  async run({ auth, propsValue }) {
    const response = await httpClient.sendRequest<{
      data: {
        zip: string;
        images: string[];
        price: {
          price_per_image: number;
          quantity_images: number;
          total: number;
        };
      };
      success: boolean;
    }>({
      url: `${baseUrlv0}/image/generation`,
      method: HttpMethod.POST,
      authentication: {
        type: AuthenticationType.BEARER_TOKEN,
        token: auth.secret_text,
      },
      body: {
        model: propsValue.model,
        description: propsValue.description,
        size: propsValue.size,
        variations: propsValue.variations
      },
    });

    return {
    images: response.body.data.images,
    zip: response.body.data.zip,
  };
  },
});