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

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

export const updateRag = createAction({
  audience: 'both',
  auth: straicoAuth,
  name: 'update_rag',
  displayName: 'Update RAG',
  description: 'Update an existing RAG (Retrieval-Augmented Generation) base with additional files.',
  aiMetadata: { description: 'Adds one more document to an existing RAG knowledge base, re-indexing it so later RAG Prompt Completion calls can draw on the new content. Despite the name it only appends a file and cannot rename a base or change its chunking settings, and it cannot remove content; use Create RAG to start a fresh base with different settings. Requires a raw RAG id typed as text and a file with a supported extension (pdf, docx, csv, txt, xlsx or py). Not idempotent: sending the same file twice ingests it again and spends credits each time.', idempotent: false },
  props: {
    ragId: Property.ShortText({
      displayName: 'RAG ID',
      required: true,
      description: 'The ID of the RAG base to update.',
    }),
    file: Property.File({
			displayName: 'File',
			required: true,
			description:
				'Represents the file to be attached. Accepted file extensions are: pdf, docx, csv, txt, xlsx, py.',
		}),
  },
  async run({ auth, propsValue }) {
    const { ragId, file } = propsValue;

    if (!ragId) {
      throw new Error('RAG ID is required');
    }

    const formData = new FormData();
    formData.append('files', file.data, file.filename);

    const response = await httpClient.sendRequest<{
      success: boolean;
      data: {
        _id: string;
        user_id: string;
        name: string;
        description: string;
        rag_url: string;
        original_filename: string;
        chunking_method: string;
        chunk_size: number;
        chunk_overlap: number;
        buffer_size: number;
        breakpoint_threshold_type: string;
        separator: string;
        separators: string[];
        createdAt: string;
        updatedAt: string;
        __v: number;
      };
      total_coins: number;
      total_words: number;
    }>({
      url: `${baseUrlv0}/rag/${ragId}`,
      method: HttpMethod.PUT,
      authentication: {
        type: AuthenticationType.BEARER_TOKEN,
        token: auth.secret_text,
      },
      body: formData,
      headers: {
        'Content-Type': 'multipart/form-data',
      },
    });

    return response.body;
  },
});