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.3 KB
Raw Blame
import { createAction, Property } from '@activepieces/pieces-framework';
import { HttpMethod, httpClient } from '@activepieces/pieces-common';
import { mistralAuth } from '../common/auth';
import FormData from 'form-data';
import { parseMistralError } from '../common/props';
import { mistralRequest } from '../common/request';
import { uploadFileOutputSchema } from '../output-schemas';

const SUPPORTED_PURPOSES = ['fine-tune', 'batch', 'ocr'];
const MAX_FILE_SIZE_BYTES = 512 * 1024 * 1024;
const ALLOWED_EXTENSIONS = [
	'jsonl',
	'txt',
	'csv',
	'pdf',
	'docx',
	'png',
	'jpg',
	'jpeg',
	'mp3',
	'mp4',
];

function getFileExtension(filename: string): string {
	const parts = filename.split('.');
	return parts.length > 1 ? parts.pop()!.toLowerCase() : '';
}

export const uploadFile = createAction({
  audience: 'both',
	auth: mistralAuth,
	name: 'upload_file',
	classification: 'WRITE',
	displayName: 'Upload File',
	description: 'Upload a file to Mistral for OCR, batch jobs or fine-tuning.',
	propertyGroups: [
		{ key: 'upload', display: 'section', label: 'Upload', icon: 'paperclip', props: ['file'] },
		{ key: 'use', display: 'section', label: 'Use for', icon: 'tag', props: ['purpose'] },
	],
	aiMetadata: { description: 'Stores a file in the connected Mistral account and returns its file id, tagged with a purpose of fine-tune, batch, or ocr that decides what the id may later be used for. Run it before Run OCR whenever the document is a step attachment rather than a public URL: upload with purpose=ocr and feed the returned id into that action. Rejects files above 512MB or with an extension outside jsonl, txt, csv, pdf, docx, png, jpg, jpeg, mp3, mp4; not idempotent: each call stores another copy and returns a new id.', idempotent: false },
	outputSchema: uploadFileOutputSchema,
	props: {
		file: Property.File({
			displayName: 'File',
			description: 'The file to send to Mistral, up to 512 MB.',
			required: true,
		}),
		purpose: Property.StaticDropdown({
			displayName: 'Purpose',
			description: 'What Mistral may use the file for. Fine-tuning needs a .jsonl file.',
			required: true,
			display: 'cards',
			options: {
				options: [
					{ label: 'OCR', value: 'ocr', icon: 'file' },
					{ label: 'Batch', value: 'batch', icon: 'file' },
					{ label: 'Fine-Tuning', value: 'fine-tune', icon: 'file' },
				],
			},
		}),
	},
	async run(context) {
		const { file, purpose } = context.propsValue;

		if (!file) throw new Error('File is required');

		if (!SUPPORTED_PURPOSES.includes(purpose)) throw new Error('Invalid purpose');

		if (file.data.byteLength > MAX_FILE_SIZE_BYTES) throw new Error('File exceeds 512MB limit');

		const ext = getFileExtension(file.filename);

		if (!ALLOWED_EXTENSIONS.includes(ext)) throw new Error(`File extension .${ext} is not allowed`);

		const { baseUrl, headers } = mistralRequest.getConfig(context.auth);
		const form = new FormData();
		form.append('file', Buffer.from(file.data), file.filename);
		form.append('purpose', purpose);

		try {
			const response = await httpClient.sendRequest({
				method: HttpMethod.POST,
				url: `${baseUrl}/files`,
				headers: {
					...headers,
					...form.getHeaders(),
				},
				body: form,
			});
			return response.body;
		} catch (e: any) {
			throw new Error(parseMistralError({ error: e, baseUrl }));
		}
	},
});