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

10.3 KB
Raw Blame
import {
  AIProviderName,
  isNil,
  spreadIfDefined,
} from '@activepieces/core-utils';
import {
  AgentConfig,
  AgentPieceProps,
  AgentProviderModel,
  AgentSummary,
  AgentToolType,
  AgentVisibility,
  McpAuthType,
  ModelOptions,
  Permission,
} from '@activepieces/shared';
import { t } from 'i18next';
import { ExternalLinkIcon, Link2OffIcon, PlusIcon } from 'lucide-react';
import { useMemo } from 'react';
import { useFormContext } from 'react-hook-form';
import { Link } from 'react-router-dom';
import { toast } from 'sonner';

import { agentLinkUtils } from '@/app/builder/step-settings/agent-settings/agent-link-utils';
import { agentModelUtils } from '@/app/builder/step-settings/agent-settings/agent-model-utils';
import { DataFetchErrorState } from '@/components/custom/data-fetch-error-state';
import { PermissionNeededTooltip } from '@/components/custom/permission-needed-tooltip';
import { SearchableSelect } from '@/components/custom/searchable-select';
import { TextWithTooltip } from '@/components/custom/text-with-tooltip';
import { Badge } from '@/components/ui/badge';
import { Button } from '@/components/ui/button';
import { FormItem, FormLabel } from '@/components/ui/form';
import {
  Tooltip,
  TooltipContent,
  TooltipTrigger,
} from '@/components/ui/tooltip';
import { aiModelHooks } from '@/features/agents/ai-model/hooks';
import {
  agentsQueries,
  useAgentsAvailable,
} from '@/features/agents/hooks/agents-hooks';
import { useAuthorization } from '@/hooks/authorization-hooks';
import { authenticationSession } from '@/lib/authentication-session';
import { cn } from '@/lib/utils';

export const AgentLink = ({ disabled, pieceVersion }: AgentLinkProps) => {
  const form = useFormContext();
  const projectId = authenticationSession.getProjectId() ?? undefined;
  const agentsAvailable = useAgentsAvailable();
  const { checkAccess } = useAuthorization();
  const mayReadAgents = checkAccess(Permission.READ_AGENT);
  const mayWriteAgents = checkAccess(Permission.WRITE_AGENT);

  const { data, isLoading, isError, refetch } = agentsQueries.useAgents({
    projectId,
    enabled: agentsAvailable && mayReadAgents,
  });
  const agents = useMemo(
    () => (data?.pages ?? []).flatMap((page): AgentSummary[] => page.data),
    [data],
  );
  const runnable = useMemo(
    () =>
      agents.filter((agent) => agent.visibility === AgentVisibility.PROJECT),
    [agents],
  );

  const linkedExternalId = agentLinkUtils.externalIdOf(
    form.watch(`settings.input.${AgentPieceProps.AGENT_ID}`),
  );
  const linked = agents.find((agent) => agent.externalId === linkedExternalId);
  const { data: linkedAgent } = agentsQueries.useAgent({
    id: linked?.id ?? '',
    enabled: !isNil(linked),
  });
  const stillFindingIt = isLoading && !isNil(linkedExternalId);
  const linkedConfig = linkedAgent?.published ?? linkedAgent?.draft;

  const summaryText =
    linkedAgent?.description?.trim() || linkedConfig?.instructions?.trim();
  const blockedReason = stillFindingIt
    ? undefined
    : isNil(linked)
    ? t('Not in this project, so this step cannot run.')
    : !linked.isPublished
    ? t('Not published yet. Publish it before this flow can run.')
    : undefined;
  const { data: modelOptions } = aiModelHooks.useModelOptions({
    projectId: projectId ?? '',
    surface: 'agent',
    tierId: linkedConfig?.modelTierId ?? undefined,
  });
  const modelLabel = modelLabelOf({
    config: linkedConfig,
    options: modelOptions,
  });
  const toolPieces = new Set(
    (linked?.toolPieceNames ?? []).map((pieceName) =>
      pieceName.replace('@activepieces/piece-', ''),
    ),
  );
  const toolChips = [...toolPieces].slice(0, MAX_TOOL_CHIPS);
  const hiddenToolCount = toolPieces.size - toolChips.length;

  if (!agentsAvailable || !mayReadAgents) {
    return null;
  }

  const link = (externalId: string | null) => {
    form.setValue(
      `settings.input.${AgentPieceProps.AGENT_ID}`,
      externalId ?? undefined,
      { shouldValidate: true },
    );
  };

  const detach = () => {
    if (isNil(linkedConfig)) {
      return;
    }
    const authedMcpTools = linkedConfig.tools.filter(
      (tool) =>
        tool.type === AgentToolType.MCP && tool.auth.type !== McpAuthType.NONE,
    );
    form.setValue(
      `settings.input.${AgentPieceProps.AGENT_TOOLS}`,
      linkedConfig.tools.filter((tool) => !authedMcpTools.includes(tool)),
      { shouldValidate: true },
    );
    form.setValue(
      `settings.input.${AgentPieceProps.STRUCTURED_OUTPUT}`,
      linkedConfig.structuredOutput,
      { shouldValidate: true },
    );
    form.setValue(
      `settings.input.${AgentPieceProps.MAX_STEPS}`,
      linkedConfig.maxSteps,
      { shouldValidate: true },
    );
    const model = detachedModelOf({
      config: linkedConfig,
      withTiers: agentModelUtils.supportsTiers({ pieceVersion }),
    });
    if (!isNil(model)) {
      form.setValue(
        `settings.input.${AgentPieceProps.AI_PROVIDER_MODEL}`,
        model,
        { shouldValidate: true },
      );
    }
    form.setValue(`settings.input.${AgentPieceProps.AGENT_ID}`, undefined, {
      shouldValidate: true,
    });
    if (authedMcpTools.length > 0) {
      toast(
        t(
          'MCP tools were left behind. Their credentials stay with the agent, so add them again here.',
        ),
      );
    }
  };

  if (isError) {
    return (
      <FormItem className="flex flex-col gap-2">
        <FormLabel>{t('Agent')}</FormLabel>
        <DataFetchErrorState entity={t('agents')} onRetry={refetch} />
      </FormItem>
    );
  }

  return (
    <FormItem className="flex flex-col gap-2">
      <FormLabel>{t('Agent')}</FormLabel>
      <SearchableSelect
        options={runnable.map((agent) => ({
          value: agent.externalId,
          label: agent.displayName,
          description: agent.isPublished ? undefined : t('Not published yet'),
        }))}
        value={linkedExternalId}
        loading={isLoading}
        disabled={disabled}
        showDeselect={!isNil(linkedExternalId)}
        placeholder={t('Pick a saved agent')}
        onChange={link}
      />
      {isNil(linkedExternalId) ? (
        <PermissionNeededTooltip hasPermission={mayWriteAgents}>
          <Button
            variant="link"
            size="sm"
            className="h-auto self-start p-0"
            disabled={!mayWriteAgents}
            asChild={mayWriteAgents}
          >
            {mayWriteAgents ? (
              <Link to="/agents" target="_blank" rel="noreferrer">
                <PlusIcon className="size-4" />
                {t('Create agent')}
              </Link>
            ) : (
              <span>
                <PlusIcon className="size-4" />
                {t('Create agent')}
              </span>
            )}
          </Button>
        </PermissionNeededTooltip>
      ) : (
        <div className="flex flex-col gap-2.5 rounded-xl border p-3">
          {summaryText && (
            <TextWithTooltip tooltipMessage={summaryText}>
              <p className="line-clamp-2 text-xs text-gray-11">{summaryText}</p>
            </TextWithTooltip>
          )}

          {(!isNil(modelLabel) || toolChips.length > 0) && (
            <div className="flex flex-wrap items-center gap-1.5">
              {!isNil(modelLabel) && (
                <Badge variant="secondary" className="font-normal">
                  {modelLabel}
                </Badge>
              )}
              {toolChips.map((chip) => (
                <Badge key={chip} variant="outline" className="font-normal">
                  {chip}
                </Badge>
              ))}
              {hiddenToolCount > 0 && (
                <Badge variant="outline" className="font-normal">
                  {t('+{count}', { count: hiddenToolCount })}
                </Badge>
              )}
            </div>
          )}

          <p
            className={cn(
              'text-xs',
              isNil(blockedReason) ? 'text-gray-11' : 'text-warning-11',
            )}
          >
            {blockedReason ??
              (stillFindingIt
                ? t('Looking it up.')
                : t('Shared with every flow that uses it.'))}
          </p>

          <div className="flex flex-wrap items-center gap-2">
            {!isNil(linked) && (
              <Button variant="outline" size="sm" asChild>
                <Link
                  to={`/agents/${linked.id}`}
                  target="_blank"
                  rel="noreferrer"
                >
                  {t('Edit agent')}
                  <ExternalLinkIcon className="size-3.5" />
                </Link>
              </Button>
            )}
            <Tooltip>
              <TooltipTrigger asChild>
                <Button
                  variant="ghost"
                  size="sm"
                  disabled={disabled || isNil(linkedConfig)}
                  onClick={detach}
                >
                  <Link2OffIcon className="size-4" />
                  {t('Detach')}
                </Button>
              </TooltipTrigger>
              <TooltipContent>
                {t(
                  'Copies the tools and model. Instructions stay with the agent.',
                )}
              </TooltipContent>
            </Tooltip>
          </div>
        </div>
      )}
    </FormItem>
  );
};

function modelLabelOf({
  config,
  options,
}: {
  config: AgentConfig | undefined;
  options: ModelOptions | undefined;
}): string | undefined {
  if (isNil(config)) {
    return undefined;
  }
  if (!isNil(config.modelTierId)) {
    const tierId =
      options?.movedTiers[config.modelTierId] ?? config.modelTierId;
    return options?.tiers.find((tier) => tier.id === tierId)?.name;
  }
  if (config.provider === AIProviderName.ACTIVEPIECES) {
    return (
      options?.credits?.tiers.find((tier) => tier.id === config.modelName)
        ?.label ??
      config.modelName ??
      undefined
    );
  }
  return config.modelName ?? undefined;
}

function detachedModelOf({
  config,
  withTiers,
}: {
  config: AgentConfig;
  withTiers: boolean;
}): AgentProviderModel | null {
  if (!isNil(config.modelTierId)) {
    return withTiers ? { modelTierId: config.modelTierId } : null;
  }
  if (isNil(config.provider) || isNil(config.modelName)) {
    return null;
  }
  return {
    provider: config.provider,
    model: config.modelName,
    ...spreadIfDefined('configId', config.providerConfigId ?? undefined),
  };
}

type AgentLinkProps = {
  disabled: boolean;
  pieceVersion: string;
};

const MAX_TOOL_CHIPS = 3;