{"version":3,"sources":["../src/index.ts"],"sourcesContent":["/**\n * Copyright 2024 Google LLC\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport {\n  ActionMetadata,\n  embedderActionMetadata,\n  embedderRef,\n  EmbedderReference,\n  Genkit,\n  modelActionMetadata,\n  ModelReference,\n  z,\n} from 'genkit';\nimport { logger } from 'genkit/logging';\nimport { modelRef } from 'genkit/model';\nimport { GenkitPlugin, genkitPlugin } from 'genkit/plugin';\nimport { ActionType } from 'genkit/registry';\nimport { getApiKeyFromEnvVar } from './common.js';\nimport {\n  defineGoogleAIEmbedder,\n  SUPPORTED_MODELS as EMBEDDER_MODELS,\n  GeminiEmbeddingConfig,\n  GeminiEmbeddingConfigSchema,\n  textEmbedding004,\n  textEmbeddingGecko001,\n} from './embedder.js';\nimport {\n  defineGoogleAIModel,\n  gemini,\n  gemini10Pro,\n  gemini15Flash,\n  gemini15Flash8b,\n  gemini15Pro,\n  gemini20Flash,\n  gemini20FlashExp,\n  gemini20FlashLite,\n  gemini20ProExp0205,\n  gemini25FlashPreview0417,\n  gemini25ProExp0325,\n  gemini25ProPreview0325,\n  GeminiConfigSchema,\n  SUPPORTED_V15_MODELS,\n  SUPPORTED_V1_MODELS,\n  type GeminiConfig,\n  type GeminiVersionString,\n} from './gemini.js';\nimport { listModels } from './list-models.js';\nexport {\n  gemini,\n  gemini10Pro,\n  gemini15Flash,\n  gemini15Flash8b,\n  gemini15Pro,\n  gemini20Flash,\n  gemini20FlashExp,\n  gemini20FlashLite,\n  gemini20ProExp0205,\n  gemini25FlashPreview0417,\n  gemini25ProExp0325,\n  gemini25ProPreview0325,\n  textEmbedding004,\n  textEmbeddingGecko001,\n  type GeminiConfig,\n  type GeminiVersionString,\n};\n\nexport interface PluginOptions {\n  /**\n   * Provide the API key to use to authenticate with the Gemini API. By\n   * default, an API key must be provided explicitly here or through the\n   * `GEMINI_API_KEY` or `GOOGLE_API_KEY` environment variables.\n   *\n   * If `false` is explicitly passed, the plugin will be configured to\n   * expect an `apiKey` option to be provided to the model config at\n   * call time.\n   **/\n  apiKey?: string | false;\n  apiVersion?: string | string[];\n  baseUrl?: string;\n  models?: (\n    | ModelReference</** @ignore */ typeof GeminiConfigSchema>\n    | string\n  )[];\n  experimental_debugTraces?: boolean;\n}\n\nasync function initializer(ai: Genkit, options?: PluginOptions) {\n  let apiVersions = ['v1'];\n\n  if (options?.apiVersion) {\n    if (Array.isArray(options?.apiVersion)) {\n      apiVersions = options?.apiVersion;\n    } else {\n      apiVersions = [options?.apiVersion];\n    }\n  }\n\n  if (apiVersions.includes('v1beta')) {\n    Object.keys(SUPPORTED_V15_MODELS).forEach((name) =>\n      defineGoogleAIModel({\n        ai,\n        name,\n        apiKey: options?.apiKey,\n        apiVersion: 'v1beta',\n        baseUrl: options?.baseUrl,\n        debugTraces: options?.experimental_debugTraces,\n      })\n    );\n  }\n  if (apiVersions.includes('v1')) {\n    Object.keys(SUPPORTED_V1_MODELS).forEach((name) =>\n      defineGoogleAIModel({\n        ai,\n        name,\n        apiKey: options?.apiKey,\n        apiVersion: undefined,\n        baseUrl: options?.baseUrl,\n        debugTraces: options?.experimental_debugTraces,\n      })\n    );\n    Object.keys(SUPPORTED_V15_MODELS).forEach((name) =>\n      defineGoogleAIModel({\n        ai,\n        name,\n        apiKey: options?.apiKey,\n        apiVersion: undefined,\n        baseUrl: options?.baseUrl,\n        debugTraces: options?.experimental_debugTraces,\n      })\n    );\n    Object.keys(EMBEDDER_MODELS).forEach((name) =>\n      defineGoogleAIEmbedder(ai, name, { apiKey: options?.apiKey })\n    );\n  }\n\n  if (options?.models) {\n    for (const modelOrRef of options?.models) {\n      const modelName =\n        typeof modelOrRef === 'string'\n          ? modelOrRef\n          : // strip out the `googleai/` prefix\n            modelOrRef.name.split('/')[1];\n      const modelRef =\n        typeof modelOrRef === 'string' ? gemini(modelOrRef) : modelOrRef;\n      defineGoogleAIModel({\n        ai,\n        name: modelName,\n        apiKey: options?.apiKey,\n        baseUrl: options?.baseUrl,\n        info: {\n          ...modelRef.info,\n          label: `Google AI - ${modelName}`,\n        },\n        debugTraces: options?.experimental_debugTraces,\n      });\n    }\n  }\n}\n\nasync function resolver(\n  ai: Genkit,\n  actionType: ActionType,\n  actionName: string,\n  options?: PluginOptions\n) {\n  switch (actionType) {\n    case 'model':\n      resolveModel(ai, actionName, options);\n      break;\n    case 'embedder':\n      resolveEmbedder(ai, actionName, options);\n      break;\n    default:\n    // no-op\n  }\n}\n\nfunction resolveModel(ai: Genkit, actionName: string, options?: PluginOptions) {\n  const modelRef = gemini(actionName);\n  defineGoogleAIModel({\n    ai,\n    name: modelRef.name,\n    apiKey: options?.apiKey,\n    baseUrl: options?.baseUrl,\n    info: {\n      ...modelRef.info,\n      label: `Google AI - ${actionName}`,\n    },\n    debugTraces: options?.experimental_debugTraces,\n  });\n}\n\nfunction resolveEmbedder(\n  ai: Genkit,\n  actionName: string,\n  options?: PluginOptions\n) {\n  defineGoogleAIEmbedder(ai, `googleai/${actionName}`, {\n    apiKey: options?.apiKey,\n  });\n}\n\nasync function listActions(options?: PluginOptions): Promise<ActionMetadata[]> {\n  const apiKey = options?.apiKey || getApiKeyFromEnvVar();\n  if (!apiKey) {\n    // If API key is not configured we don't want to error, just return empty.\n    // In practice it will never actually reach this point without the API key,\n    // plugin initializer will fail before that.\n    logger.error(\n      'Pass in the API key or set the GEMINI_API_KEY or GOOGLE_API_KEY environment variable.'\n    );\n    return [];\n  }\n\n  const models = await listModels(\n    options?.baseUrl || 'https://generativelanguage.googleapis.com',\n    apiKey\n  );\n\n  return [\n    // Models\n    ...models\n      .filter((m) => m.supportedGenerationMethods.includes('generateContent'))\n      // Filter out deprecated\n      .filter((m) => !m.description || !m.description.includes('deprecated'))\n      .map((m) => {\n        const ref = gemini(\n          m.name.startsWith('models/')\n            ? m.name.substring('models/'.length)\n            : m.name\n        );\n\n        return modelActionMetadata({\n          name: ref.name,\n          info: ref.info,\n          configSchema: GeminiConfigSchema,\n        });\n      }),\n    // Embedders\n    ...models\n      .filter((m) => m.supportedGenerationMethods.includes('embedContent'))\n      // Filter out deprecated\n      .filter((m) => !m.description || !m.description.includes('deprecated'))\n      .map((m) => {\n        const name =\n          'googleai/' +\n          (m.name.startsWith('models/')\n            ? m.name.substring('models/'.length)\n            : m.name);\n\n        return embedderActionMetadata({\n          name,\n          configSchema: GeminiEmbeddingConfigSchema,\n          info: {\n            dimensions: 768,\n            label: `Google Gen AI - ${name}`,\n            supports: {\n              input: ['text'],\n            },\n          },\n        });\n      }),\n  ];\n}\n\n/**\n * Google Gemini Developer API plugin.\n */\nexport function googleAIPlugin(options?: PluginOptions): GenkitPlugin {\n  let listActionsCache;\n  return genkitPlugin(\n    'googleai',\n    async (ai: Genkit) => await initializer(ai, options),\n    async (ai: Genkit, actionType: ActionType, actionName: string) =>\n      await resolver(ai, actionType, actionName, options),\n    async () => {\n      if (listActionsCache) return listActionsCache;\n      listActionsCache = await listActions(options);\n      return listActionsCache;\n    }\n  );\n}\n\nexport type GoogleAIPlugin = {\n  (params?: PluginOptions): GenkitPlugin;\n  model(\n    name: GeminiVersionString,\n    config?: z.infer<typeof GeminiConfigSchema>\n  ): ModelReference<typeof GeminiConfigSchema>;\n  embedder(\n    name: string,\n    config?: GeminiEmbeddingConfig\n  ): EmbedderReference<typeof GeminiEmbeddingConfigSchema>;\n};\n\n/**\n * Google Gemini Developer API plugin.\n */\nexport const googleAI = googleAIPlugin as GoogleAIPlugin;\ngoogleAI.model = (\n  name: GeminiVersionString,\n  config?: GeminiConfig\n): ModelReference<typeof GeminiConfigSchema> => {\n  return modelRef({\n    name: `googleai/${name}`,\n    config,\n    configSchema: GeminiConfigSchema,\n  });\n};\ngoogleAI.embedder = (\n  name: string,\n  config?: GeminiEmbeddingConfig\n): EmbedderReference<typeof GeminiEmbeddingConfigSchema> => {\n  return embedderRef({\n    name: `googleai/${name}`,\n    config,\n    configSchema: GeminiEmbeddingConfigSchema,\n  });\n};\n\nexport default googleAI;\n"],"mappings":"AAgBA;AAAA,EAEE;AAAA,EACA;AAAA,EAGA;AAAA,OAGK;AACP,SAAS,cAAc;AACvB,SAAS,gBAAgB;AACzB,SAAuB,oBAAoB;AAE3C,SAAS,2BAA2B;AACpC;AAAA,EACE;AAAA,EACA,oBAAoB;AAAA,EAEpB;AAAA,EACA;AAAA,EACA;AAAA,OACK;AACP;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OAGK;AACP,SAAS,kBAAkB;AAwC3B,eAAe,YAAY,IAAY,SAAyB;AAC9D,MAAI,cAAc,CAAC,IAAI;AAEvB,MAAI,SAAS,YAAY;AACvB,QAAI,MAAM,QAAQ,SAAS,UAAU,GAAG;AACtC,oBAAc,SAAS;AAAA,IACzB,OAAO;AACL,oBAAc,CAAC,SAAS,UAAU;AAAA,IACpC;AAAA,EACF;AAEA,MAAI,YAAY,SAAS,QAAQ,GAAG;AAClC,WAAO,KAAK,oBAAoB,EAAE;AAAA,MAAQ,CAAC,SACzC,oBAAoB;AAAA,QAClB;AAAA,QACA;AAAA,QACA,QAAQ,SAAS;AAAA,QACjB,YAAY;AAAA,QACZ,SAAS,SAAS;AAAA,QAClB,aAAa,SAAS;AAAA,MACxB,CAAC;AAAA,IACH;AAAA,EACF;AACA,MAAI,YAAY,SAAS,IAAI,GAAG;AAC9B,WAAO,KAAK,mBAAmB,EAAE;AAAA,MAAQ,CAAC,SACxC,oBAAoB;AAAA,QACl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