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

19.5 KB
Raw Blame
import {
  ApErrorParams,
  isNil,
  ErrorCode,
  RequiredActionsMissingErrorParams,
  SeekPage,
} from '@activepieces/core-utils';
import {
  ApFlagId,
  FlowOperationType,
  FlowStatus,
  FlowVersion,
  FlowVersionMetadata,
  FlowVersionState,
  FlowVersionTemplate,
  ListFlowsRequest,
  PopulatedFlow,
  FlowTrigger,
  FlowTriggerType,
  Template,
  UncategorizedFolderId,
  UpdateRunProgressRequest,
} from '@activepieces/shared';
import {
  QueryClient,
  useMutation,
  useQuery,
  useQueryClient,
} from '@tanstack/react-query';
import { t } from 'i18next';
import { useNavigate } from 'react-router-dom';
import { toast } from 'sonner';

import { useApErrorDialogStore } from '@/components/custom/ap-error-dialog/ap-error-dialog-store';
import { useSocket } from '@/components/providers/socket-provider';
import { internalErrorToast } from '@/components/ui/sonner';
import { flowRunsApi } from '@/features/flow-runs/api/flow-runs-api';
import { triggerStatusErrorUtils } from '@/features/flows/utils/trigger-status-error';
import { foldersApi } from '@/features/folders/api/folders-api';
import { piecesApi } from '@/features/pieces/api/pieces-api';
import { pieceSelectorUtils } from '@/features/pieces/utils/piece-selector-utils';
import { stepUtils } from '@/features/pieces/utils/step-utils';
import { templatesApi } from '@/features/templates/api/templates-api';
import { flagsHooks } from '@/hooks/flags-hooks';
import { api } from '@/lib/api';
import { authenticationSession } from '@/lib/authentication-session';
import { downloadFile } from '@/lib/dom-utils';
import { NEW_FLOW_QUERY_PARAM } from '@/lib/route-utils';

import { flowsApi } from '../api/flows-api';
import { flowsUtils } from '../utils/flows-utils';

import { sampleDataHooks } from './sample-data-hooks';

const createFlowsQueryKey = (projectId: string) => ['flows', projectId];
const createFlowQueryKeyPrefix = (flowId: string) => ['flow', flowId];
export const flowHooks = {
  invalidateFlowsQuery: (queryClient: QueryClient) => {
    queryClient.invalidateQueries({
      queryKey: createFlowsQueryKey(authenticationSession.getProjectId()!),
    });
  },
  useFlows: (request: Omit<ListFlowsRequest, 'projectId'>) => {
    return useQuery({
      queryKey: createFlowsQueryKey(authenticationSession.getProjectId()!),
      queryFn: async () => {
        return await flowsApi.list({
          ...request,
          projectId: authenticationSession.getProjectId()!,
        });
      },
      staleTime: 5 * 1000,
    });
  },
  useChangeFlowStatus: ({
    flowId,
    change,
    onSuccess,
    setIsPublishing,
    onRequiredActionsMissing,
  }: UseChangeFlowStatusParams) => {
    const { data: enableFlowOnPublish } = flagsHooks.useFlag<boolean>(
      ApFlagId.ENABLE_FLOW_ON_PUBLISH,
    );
    const { data: triggerTimeout } = flagsHooks.useFlag<number>(
      ApFlagId.TRIGGER_TIMEOUT_SECONDS,
    );
    const { openDialog } = useApErrorDialogStore();
    const queryClient = useQueryClient();
    return useMutation({
      mutationFn: async () => {
        if (change === 'publish') {
          setIsPublishing?.(true);
        }
        return flowsApi.update(flowId, {
          type:
            change === 'publish'
              ? FlowOperationType.LOCK_AND_PUBLISH
              : FlowOperationType.CHANGE_STATUS,
          request: {
            status:
              change === 'publish'
                ? enableFlowOnPublish
                  ? FlowStatus.ENABLED
                  : FlowStatus.DISABLED
                : change,
          },
        });
      },
      onSuccess: async (flow: PopulatedFlow) => {
        if (change === 'publish') {
          await queryClient.refetchQueries({
            queryKey: ['flow-approval-requests'],
          });
          setIsPublishing?.(false);
        }
        onSuccess?.(flow);
      },
      onError: (error: unknown) => {
        if (change === 'publish') {
          setIsPublishing?.(false);
        }
        if (!api.isError(error)) {
          internalErrorToast();
          return;
        }
        if (
          !error.response ||
          error.response.status === api.httpStatus.GatewayTimeout
        ) {
          toast.error(t('Request Timed Out'), {
            description: t(
              'The operation exceeded the {timeout} second timeout. Please refresh and try again.',
              { timeout: triggerTimeout ?? 60 },
            ),
            duration: 5000,
          });
          return;
        }
        const apError = error.response.data as ApErrorParams;
        if (apError.code === ErrorCode.TRIGGER_UPDATE_STATUS) {
          const params = apError.params;
          const reportedError = triggerStatusErrorUtils.describeStandardError(
            params.standardError,
          );
          openDialog({
            title:
              change === 'publish'
                ? t('Publish failed')
                : t('Status update failed'),
            description: (
              <div className="flex flex-col gap-2">
                <p>
                  {t(
                    'An error occurred while changing the flow status. This may be due to an issue in the trigger piece or its settings.',
                  )}
                </p>
                {reportedError && (
                  <div className="flex flex-col gap-1 rounded-md bg-gray-3 p-3">
                    <span className="text-xs font-medium text-gray-11">
                      {t('The connected app reported')}
                    </span>
                    <span className="line-clamp-4 text-sm text-gray-12">
                      {reportedError}
                    </span>
                  </div>
                )}
              </div>
            ),
            error: triggerStatusErrorUtils.parseStandardError(
              params.standardError,
            ) ?? {
              standardError: params.standardError || '',
              standardOutput: params.standardOutput || '',
            },
            technicalDetailsDefaultOpen: isNil(reportedError),
          });
        } else if (apError.code === ErrorCode.REQUIRED_ACTIONS_MISSING) {
          if (onRequiredActionsMissing) {
            onRequiredActionsMissing(apError.params);
            return;
          }
          toast.error(t('Publish failed'), {
            description: apError.params.message,
            duration: 5000,
          });
        } else if (apError.code === ErrorCode.QUOTA_EXCEEDED) {
          toast.error(t('Active flows limit reached'), {
            description: t(
              'You have reached the maximum number of active flows. Disable another flow or increase the limit.',
            ),
            duration: 5000,
          });
        } else {
          const serverMessage = api.serverErrorMessage(error);
          if (isNil(serverMessage)) {
            internalErrorToast();
            return;
          }
          toast.error(
            change === 'publish'
              ? t('Publish failed')
              : t('Status update failed'),
            {
              description: serverMessage,
              duration: 8000,
            },
          );
        }
      },
    });
  },
  useExportFlows: () => {
    return useMutation({
      mutationFn: async (flows: PopulatedFlow[]) => {
        if (flows.length === 0) {
          return flows;
        }
        if (flows.length === 1) {
          await flowsUtils.downloadFlow(flows[0].id);
          return flows;
        }
        await downloadFile({
          obj: await flowsUtils.zipFlows(flows),
          fileName: 'flows',
          extension: 'zip',
        });
        return flows;
      },
      onSuccess: (res) => {
        if (res.length > 0) {
          toast.success(
            res.length === 1
              ? t(`${res[0].version.displayName} has been exported.`)
              : t('Flows have been exported.'),
            {
              duration: 3000,
            },
          );
        }
      },
      onError: () => {
        toast.error(t('Failed to export flows'));
      },
    });
  },

  useFetchFlowVersion: ({
    onSuccess,
  }: {
    onSuccess: (flowVersion: FlowVersion) => void;
  }) => {
    return useMutation<FlowVersion, Error, FlowVersionMetadata>({
      mutationFn: async (flowVersion) => {
        const result = await flowsApi.get(flowVersion.flowId, {
          versionId: flowVersion.id,
        });
        return result.version;
      },
      onSuccess,
    });
  },
  useOverWriteDraftWithVersion: ({
    onSuccess,
  }: {
    onSuccess: (flow: PopulatedFlow) => void;
  }) => {
    return useMutation<
      PopulatedFlow,
      Error,
      { flowId: string; versionId: string }
    >({
      mutationFn: async ({ flowId, versionId }) => {
        const result = await flowsApi.update(flowId, {
          type: FlowOperationType.USE_AS_DRAFT,
          request: {
            versionId,
          },
        });
        return result;
      },
      onSuccess,
    });
  },
  useCreateMcpFlow: () => {
    const navigate = useNavigate();
    return useMutation({
      mutationFn: async () => {
        const flow = await flowsApi.create({
          projectId: authenticationSession.getProjectId()!,
          displayName: t('Untitled'),
        });
        const mcpPiece = await piecesApi.get({
          name: '@activepieces/piece-mcp',
        });
        const trigger = mcpPiece.triggers['mcp_tool'];
        if (!trigger) {
          throw new Error('MCP trigger not found');
        }
        const stepData = pieceSelectorUtils.getDefaultStepValues({
          stepName: 'trigger',
          pieceSelectorItem: {
            actionOrTrigger: trigger,
            type: FlowTriggerType.PIECE,
            pieceMetadata: stepUtils.mapPieceToMetadata({
              piece: mcpPiece,
              type: 'trigger',
            }),
          },
        }) as FlowTrigger;
        await flowsApi.update(flow.id, {
          type: FlowOperationType.UPDATE_TRIGGER,
          request: stepData,
        });
        return flow;
      },
      onSuccess: (flow) => {
        navigate(`/flows/${flow.id}/`);
      },
    });
  },
  useGetFlow: ({
    flowId,
    versionId,
    enabled = true,
  }: {
    flowId: string;
    versionId?: string;
    enabled?: boolean;
  }) => {
    return useQuery({
      queryKey: flowHooks.createFlowQueryKeys({ flowId, versionId }),
      queryFn: async () => {
        try {
          return await flowsApi.get(
            flowId,
            versionId ? { versionId } : undefined,
          );
        } catch (err) {
          console.error(err);
          return null;
        }
      },
      enabled: enabled && !!flowId,
      staleTime: 0,
    });
  },
  useUpdateFlowOwner: ({ onSuccess }: { onSuccess: () => void }) => {
    return useMutation<
      PopulatedFlow,
      Error,
      { flowId: string; ownerId: string }
    >({
      mutationFn: async ({ flowId, ownerId }) => {
        return await flowsApi.update(flowId, {
          type: FlowOperationType.UPDATE_OWNER,
          request: { ownerId },
        });
      },
      onSuccess,
    });
  },
  useCreateTemplateFromFlow: ({
    onSuccess,
  }: {
    onSuccess: (template: Template) => void;
  }) => {
    return useMutation<
      Template,
      Error,
      {
        flowId: string;
        flowVersionId: string;
        description: string;
        author: string;
      }
    >({
      mutationFn: async ({ flowId, flowVersionId, description, author }) => {
        const template = await flowsApi.getTemplate(flowId, {
          versionId: flowVersionId,
        });
        const flowTemplate = await templatesApi.create({
          name: template.name,
          description,
          summary: template.summary,
          tags: template.tags,
          blogUrl: template.blogUrl ?? undefined,
          metadata: template.metadata,
          author,
          categories: template.categories,
          type: template.type,
          flows: template.flows,
        });
        return flowTemplate;
      },
      onSuccess,
    });
  },
  useTestFlowOrStartManualTrigger: ({
    flowVersionId,
    onUpdateRun,
    isForManualTrigger,
  }: {
    flowVersionId: string;
    onUpdateRun: (stepResponse: UpdateRunProgressRequest) => void;
    isForManualTrigger: boolean;
  }) => {
    const socket = useSocket();
    return useMutation<void>({
      mutationFn: () =>
        flowRunsApi.subscribeToTestFlowOrManualRun(
          socket,
          {
            flowVersionId,
          },
          onUpdateRun,
          isForManualTrigger,
        ),
    });
  },

  useListFlowVersions: (flowId: string) => {
    return useQuery<SeekPage<FlowVersionMetadata>, Error>({
      queryKey: ['flow-versions', flowId],
      queryFn: () =>
        flowsApi.listVersions(flowId, {
          limit: 1000,
          cursor: undefined,
        }),
      staleTime: 0,
    });
  },
  useGetFlowVersionNumber: ({
    flowId,
    versionId,
  }: {
    flowId: string;
    versionId: string;
  }) => {
    const { data: flowVersions } = flowHooks.useListFlowVersions(flowId);
    return flowVersions?.data
      ? flowVersions.data.length -
          flowVersions.data.findIndex((version) => version.id === versionId)
      : '';
  },
  useStartFromScratch: (folderId: string) => {
    const navigate = useNavigate();
    return useMutation<PopulatedFlow, Error, void>({
      mutationFn: async () => {
        const folder =
          folderId !== UncategorizedFolderId
            ? await foldersApi.get(folderId)
            : undefined;
        const flow = await flowsApi.create({
          projectId: authenticationSession.getProjectId()!,
          displayName: t('Untitled'),
          folderName: folder?.displayName,
        });
        return flow;
      },
      onSuccess: (flow) => {
        navigate(`/flows/${flow.id}?${NEW_FLOW_QUERY_PARAM}=true`);
      },
    });
  },
  importFlowIntoExisting: async ({
    template,
    existingFlowId,
  }: {
    template: Template;
    existingFlowId: string;
  }): Promise<PopulatedFlow> => {
    const flows = template.flows || [];
    if (flows.length === 0) {
      throw new Error('Template has no flows');
    }

    const templateFlow = flows[0];
    const flow = await flowsApi.get(existingFlowId);

    const oldExternalId = !isNil(template.metadata?.externalId)
      ? (template.metadata['externalId'] as string)
      : flow.externalId;

    const triggerString = JSON.stringify(templateFlow.trigger).replaceAll(
      oldExternalId,
      flow.externalId,
    );
    const updatedTrigger = JSON.parse(triggerString);

    return await flowsApi.update(flow.id, {
      type: FlowOperationType.IMPORT_FLOW,
      request: {
        displayName: flow.version.displayName,
        trigger: updatedTrigger,
        schemaVersion: templateFlow.schemaVersion,
        notes: templateFlow.notes,
      },
    });
  },
  importFlowsFromTemplates: async ({
    templates,
    projectId,
    folderName,
  }: {
    templates: Template[];
    projectId: string;
    folderName?: string;
  }): Promise<PopulatedFlow[]> => {
    if (templates.length === 0) {
      return [];
    }

    const allFlowsToImport: Array<{
      flow: PopulatedFlow;
      templateFlow: FlowVersionTemplate;
      oldExternalId: string;
    }> = [];

    for (const template of templates) {
      const flows = template.flows || [];
      if (flows.length === 0) {
        continue;
      }

      for (const templateFlow of flows) {
        const flow = await flowsApi.create({
          displayName: templateFlow.displayName,
          templateId: template.id,
          projectId,
          folderName,
        });

        const oldExternalId = !isNil(template.metadata?.externalId)
          ? (template.metadata['externalId'] as string)
          : flow.externalId;

        allFlowsToImport.push({
          flow,
          templateFlow,
          oldExternalId,
        });
      }
    }

    const externalIdMap = new Map<string, string>();
    for (const { oldExternalId, flow } of allFlowsToImport) {
      externalIdMap.set(oldExternalId, flow.externalId);
    }

    const importPromises = allFlowsToImport.map(
      async ({ flow, templateFlow }) => {
        let triggerString = JSON.stringify(templateFlow.trigger);

        for (const [oldId, newId] of externalIdMap.entries()) {
          triggerString = triggerString.replaceAll(oldId, newId);
        }

        const updatedTrigger = JSON.parse(triggerString);

        return await flowsApi.update(flow.id, {
          type: FlowOperationType.IMPORT_FLOW,
          request: {
            displayName: templateFlow.displayName,
            trigger: updatedTrigger,
            schemaVersion: templateFlow.schemaVersion,
            notes: templateFlow.notes,
          },
        });
      },
    );

    return await Promise.all(importPromises);
  },
  useFetchNpmPackageVersion: ({
    onSuccess,
    onError,
  }: {
    onSuccess: (result: {
      packageName: string;
      packageVersion: string;
    }) => void;
    onError: () => void;
  }) => {
    return useMutation({
      mutationFn: async (packageName: string) => {
        const response = await api.get<{ 'dist-tags': { latest: string } }>(
          `https://registry.npmjs.org/${packageName}`,
        );
        return {
          packageName,
          packageVersion: response['dist-tags'].latest,
        };
      },
      onSuccess,
      onError,
    });
  },
  useListFlowsUsingConnection: ({
    projectId,
    onSuccess,
  }: {
    projectId: string;
    onSuccess: () => void;
  }) => {
    return useMutation({
      mutationFn: async (externalId: string) => {
        const request = {
          projectId,
          connectionExternalIds: [externalId],
          cursor: undefined,
          limit: FLOWS_PAGE_SIZE,
        };
        const [draftFlows, publishedFlows] = await Promise.all([
          listAllFlows(request),
          listAllFlows({ ...request, versionState: FlowVersionState.LOCKED }),
        ]);
        const draftFlowIds = new Set(draftFlows.map((flow) => flow.id));
        return {
          draftFlows,
          publishedOnlyFlows: publishedFlows.filter(
            (flow) => !draftFlowIds.has(flow.id),
          ),
          draftFlowIds,
          publishedFlowIds: new Set(publishedFlows.map((flow) => flow.id)),
        };
      },
      onSuccess,
      onError: () => {
        toast.error(t('Error'), {
          description: t('Failed to get affected flows'),
        });
      },
    });
  },
  createFlowQueryKeys: ({
    flowId,
    versionId,
  }: {
    flowId: string;
    versionId: string | undefined;
  }) => [...createFlowQueryKeyPrefix(flowId), versionId],
  removeFlowFromCache: ({
    flowId,
    queryClient,
  }: {
    flowId: string;
    queryClient: QueryClient;
  }) => {
    const flowQueryKeyPrefix = createFlowQueryKeyPrefix(flowId);
    queryClient
      .getQueriesData<PopulatedFlow | null>({ queryKey: flowQueryKeyPrefix })
      .forEach(([, flow]) => {
        if (!isNil(flow)) {
          sampleDataHooks.removeSampleData({
            flowVersionId: flow.version.id,
            queryClient,
          });
        }
      });
    queryClient.removeQueries({ queryKey: flowQueryKeyPrefix });
  },
};

async function listAllFlows(
  request: ListFlowsRequest,
): Promise<PopulatedFlow[]> {
  const flows: PopulatedFlow[] = [];
  let cursor: string | undefined = undefined;
  do {
    const page: SeekPage<PopulatedFlow> = await flowsApi.list({
      ...request,
      cursor,
    });
    flows.push(...page.data);
    cursor = page.next ?? undefined;
  } while (cursor);
  return flows;
}

type UseChangeFlowStatusParams = {
  flowId: string;
  change: 'publish' | FlowStatus;
  onSuccess: (flow: PopulatedFlow) => void;
  setIsPublishing?: (isPublishing: boolean) => void;
  onRequiredActionsMissing?: (
    params: RequiredActionsMissingErrorParams['params'],
  ) => void;
};

const FLOWS_PAGE_SIZE = 1000;