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

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

const SUPPORTED_FILE_TYPES = ['pdf', 'docx', 'doc', 'txt', 'jpeg', 'jpg', 'png', 'tiff', 'bmp'];

export const uploadFile = createAction({
  auth: extractaAiAuth,
  name: 'upload_file',
  displayName: 'Upload File',
  description: 'Uploads document for extraction',
  audience: 'both',
  aiMetadata: { description: 'Uploads a document file to an already-existing Extracta.ai extraction (identified by extractionId) so it can be processed against that extraction\'s field schema; optionally adds it to a given batch, otherwise a new batch is created. Use this when the extraction config already exists and you only need to feed it another file. Requires a valid extractionId; not idempotent, since each call uploads the file and starts new processing.', idempotent: false },
  props: {
    extractionId: Property.ShortText({
      displayName: 'Extraction ID',
      description: 'Unique identifier for the extraction',
      required: true
    }),
    file: Property.File({
      displayName: 'File',
      description: 'Document file to upload (PDF, images, etc.)',
      required: true
    }),
    batchId: Property.ShortText({
      displayName: 'Batch ID',
      description:
        'The ID of the batch to add files to (optional). If not provided, a new batch will be created.',
      required: false
    })
  },
  async run(context) {
    const apiKey = context.auth.secret_text;
    const { file, extractionId, batchId } = context.propsValue;

    const fileExtension = file.filename.split('.').pop()?.toLowerCase();
    if (!fileExtension || !SUPPORTED_FILE_TYPES.includes(fileExtension)) {
      throw new Error(
        `File type not supported. Supported types: ${SUPPORTED_FILE_TYPES.join(', ')}`
      );
    }
    
    const formData = new FormData();

    formData.append('extractionId', extractionId);

    if (batchId) {
      formData.append('batchId', batchId);
    }

    const blob = new Blob([Buffer.from(file.base64, 'base64')], { 
      type: file.extension ? `application/${file.extension}` : 'application/octet-stream' 
    });
    formData.append('files', blob, file.filename);

    try {
      const response = await httpClient.sendRequest({
        method: HttpMethod.POST,
        url: 'https://api.extracta.ai/api/v1/uploadFiles',
        headers: {
          Authorization: `Bearer ${apiKey}`
        },
        body: formData
      });

      return response.body;
    } catch (error: any) {
      if (error.response) {
        const status = error.response.status;
        const body = error.response.body;

        switch (status) {
          case 401:
            throw new Error('Authentication failed. Please check your API key.');
          case 403:
            throw new Error(
              'Access denied. Your API key may not have permission for this operation.'
            );
          case 429:
            throw new Error('Rate limit exceeded. Please try again later.');
          case 400:
            throw new Error(
              `Invalid request: ${body.message || JSON.stringify(body)}`
            );
          default:
            throw new Error(
              `API error (${status}): ${body.message || 'Unknown error'}`
            );
        }
      }
      throw new Error(`Request failed: ${error.message}`);
    }
  }
});