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.7 KB
Raw Blame
import {
  httpClient,
  HttpMethod,
  HttpResponse,
  QueryParams,
} from '@activepieces/pieces-common';
import { AppConnectionValueForAuthProperty } from '@activepieces/pieces-framework';
import { googleGeminiAuth } from '../auth';

export const defaultLLM = 'gemini-1.5-flash';

export const allowedLLMs = [
  'gemini-1.5-flash',
  'gemini-1.5-flash-8b',
  'gemini-1.5-pro',
  'gemini-2.0-flash',
  'gemini-2.0-flash-lite',
  'gemini-2.5-flash',
  'gemini-2.5-pro',
  'gemini-3-pro'
];

const isAllowedLLM = (model: GeminiModel): boolean =>
  allowedLLMs.some((allowed) => model.name.startsWith(`models/${allowed}`));

const isTtsModel = (model: GeminiModel): boolean =>
  model.name.toLowerCase().includes('tts') ||
  (model.displayName?.toLowerCase().includes('tts') ?? false);

const isVeoModel = (model: GeminiModel): boolean =>
  model.name.toLowerCase().includes('veo');

const isImageModel = (model: GeminiModel): boolean =>
  model.name.toLowerCase().includes('image') &&
  !model.name.toLowerCase().includes('imagen') &&
  !isTtsModel(model) &&
  !isVeoModel(model);

const isEmbeddingModel = (model: GeminiModel): boolean =>
  model.name.toLowerCase().includes('embedding');

export const listAllModels = async ({
  auth,
}: {
  auth: GeminiAuth;
}): Promise<GeminiModel[]> => {
  const models: GeminiModel[] = [];
  let pageToken: string | undefined = undefined;

  do {
    const queryParams: QueryParams = {
      key: auth.secret_text,
      pageSize: '1000',
    };
    if (pageToken) {
      queryParams['pageToken'] = pageToken;
    }

    const { body }: HttpResponse<GeminiListModelsResponse> =
      await httpClient.sendRequest<GeminiListModelsResponse>({
        method: HttpMethod.GET,
        url: 'https://generativelanguage.googleapis.com/v1beta/models',
        queryParams,
      });
    models.push(...(body.models ?? []));
    pageToken = body.nextPageToken;
  } while (pageToken);

  return models;
};

const fetchModelOptions = async ({
  auth,
  filter,
}: {
  auth?: GeminiAuth;
  filter: (model: GeminiModel) => boolean;
}) => {
  if (!auth) {
    return {
      disabled: true,
      placeholder: 'Enter your API key first',
      options: [],
    };
  }

  try {
    const models = await listAllModels({ auth });
    const options = models.filter(filter).map((model) => ({
      label: model.displayName ?? model.name.replace('models/', ''),
      value: model.name.replace('models/', ''),
    }));

    return {
      disabled: false,
      options,
    };
  } catch {
    return {
      disabled: true,
      options: [],
      placeholder: "Couldn't load models, check your API key or try again.",
    };
  }
};

export const getGeminiModelOptions = async ({ auth }: { auth?: GeminiAuth }) =>
  fetchModelOptions({ auth, filter: isAllowedLLM });

export const getGeminiTtsModelOptions = async ({ auth }: { auth?: GeminiAuth }) =>
  fetchModelOptions({ auth, filter: isTtsModel });

export const getGeminiVideoModelOptions = async ({
  auth,
}: {
  auth?: GeminiAuth;
}) => fetchModelOptions({ auth, filter: isVeoModel });

export const getGeminiImageModelOptions = async ({
  auth,
}: {
  auth?: GeminiAuth;
}) => fetchModelOptions({ auth, filter: isImageModel });

export const getGeminiEmbeddingModelOptions = async ({
  auth,
}: {
  auth?: GeminiAuth;
}) => fetchModelOptions({ auth, filter: isEmbeddingModel });

type GeminiAuth = AppConnectionValueForAuthProperty<typeof googleGeminiAuth>;

export type GeminiModel = {
  name: string;
  displayName?: string;
  description?: string;
  version?: string;
  inputTokenLimit?: number;
  outputTokenLimit?: number;
  supportedGenerationMethods?: string[];
};

type GeminiListModelsResponse = {
  models?: GeminiModel[];
  nextPageToken?: string;
};