{"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  embedderActionMetadata,\n  embedderRef,\n  modelActionMetadata,\n  type ActionMetadata,\n  type EmbedderReference,\n  type Genkit,\n  type ModelReference,\n  type z,\n} from 'genkit';\nimport { logger } from 'genkit/logging';\nimport { modelRef } from 'genkit/model';\nimport { genkitPlugin, type GenkitPlugin } from 'genkit/plugin';\nimport type { ActionType } from 'genkit/registry';\nimport { getApiKeyFromEnvVar } from './common.js';\nimport {\n  SUPPORTED_MODELS as EMBEDDER_MODELS,\n  GeminiEmbeddingConfigSchema,\n  defineGoogleAIEmbedder,\n  geminiEmbedding001,\n  textEmbedding004,\n  textEmbeddingGecko001,\n  type GeminiEmbeddingConfig,\n} from './embedder.js';\nimport {\n  GeminiConfigSchema,\n  SUPPORTED_GEMINI_MODELS,\n  defineGoogleAIModel,\n  gemini,\n  gemini10Pro,\n  gemini15Flash,\n  gemini15Flash8b,\n  gemini15Pro,\n  gemini20Flash,\n  gemini20FlashExp,\n  gemini20FlashLite,\n  gemini20ProExp0205,\n  gemini25FlashLite,\n  gemini25FlashPreview0417,\n  gemini25ProExp0325,\n  gemini25ProPreview0325,\n  type GeminiConfig,\n  type GeminiVersionString,\n} from './gemini.js';\nimport {\n  GENERIC_IMAGEN_INFO,\n  ImagenConfigSchema,\n  defineImagenModel,\n  type KNOWN_IMAGEN_MODELS,\n} from './imagen.js';\nimport { listModels } from './list-models.js';\nimport {\n  GENERIC_VEO_INFO,\n  KNOWN_VEO_MODELS,\n  VeoConfigSchema,\n  defineVeoModel,\n} from './veo.js';\nexport {\n  gemini,\n  gemini10Pro,\n  gemini15Flash,\n  gemini15Flash8b,\n  gemini15Pro,\n  gemini20Flash,\n  gemini20FlashExp,\n  gemini20FlashLite,\n  gemini20ProExp0205,\n  gemini25FlashLite,\n  gemini25FlashPreview0417,\n  gemini25ProExp0325,\n  gemini25ProPreview0325,\n  geminiEmbedding001,\n  textEmbedding004,\n  textEmbeddingGecko001,\n  type GeminiConfig,\n  type GeminiVersionString,\n};\n\n/**\n * @deprecated\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_GEMINI_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_GEMINI_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  if (actionType === 'embedder') {\n    resolveEmbedder(ai, actionName, options);\n  } else if (actionName.startsWith('veo')) {\n    // we do it this way because the request may come in for\n    // action type 'model' and action name 'veo-...'. That case should\n    // be a noop. It's just the order or model lookup.\n    if (actionType === 'background-model') {\n      defineVeoModel(ai, actionName, options?.apiKey);\n    }\n  } else if (actionType === 'model') {\n    resolveModel(ai, actionName, options);\n  }\n}\n\nfunction resolveModel(ai: Genkit, actionName: string, options?: PluginOptions) {\n  if (actionName.startsWith('imagen')) {\n    defineImagenModel(ai, actionName, options?.apiKey);\n    return;\n  }\n\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    // Imagen\n    ...models\n      .filter(\n        (m) =>\n          m.supportedGenerationMethods.includes('predict') &&\n          m.name.includes('imagen')\n      )\n      // Filter out deprecated\n      .filter((m) => !m.description || !m.description.includes('deprecated'))\n      .map((m) => {\n        const name = m.name.split('/').at(-1)!;\n\n        return modelActionMetadata({\n          name: `googleai/${name}`,\n          info: { ...GENERIC_IMAGEN_INFO },\n          configSchema: ImagenConfigSchema,\n        });\n      }),\n    // Veo\n    ...models\n      .filter(\n        (m) =>\n          m.supportedGenerationMethods.includes('predictLongRunning') &&\n          m.name.includes('veo')\n      )\n      // Filter out deprecated\n      .filter((m) => !m.description || !m.description.includes('deprecated'))\n      .map((m) => {\n        const name = m.name.split('/').at(-1)!;\n\n        return modelActionMetadata({\n          name: `googleai/${name}`,\n          info: { ...GENERIC_VEO_INFO },\n          configSchema: VeoConfigSchema,\n          background: true,\n        });\n      }),\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 * @deprecated\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\n/**\n * @deprecated\n */\nexport type GoogleAIPlugin = {\n  (params?: PluginOptions): GenkitPlugin;\n  model(\n    name: keyof typeof SUPPORTED_GEMINI_MODELS | (`gemini-${string}` & {}),\n    config?: z.infer<typeof GeminiConfigSchema>\n  ): ModelReference<typeof GeminiConfigSchema>;\n  model(\n    name: KNOWN_IMAGEN_MODELS | (`imagen${string}` & {}),\n    config?: z.infer<typeof ImagenConfigSchema>\n  ): ModelReference<typeof ImagenConfigSchema>;\n  model(\n    name: KNOWN_VEO_MODELS | (`veo${string}` & {}),\n    config?: z.infer<typeof VeoConfigSchema>\n  ): ModelReference<typeof VeoConfigSchema>;\n  model(name: string, config?: any): ModelReference<z.ZodTypeAny>;\n  embedder(\n    name: string,\n    config?: GeminiEmbeddingConfig\n  ): EmbedderReference<typeof GeminiEmbeddingConfigSchema>;\n};\n\n/**\n * Google Gemini Developer API plugin.\n * @deprecated Please use `import { googleAI } from '@genkit-ai/google-genai';` instead. Replace model constants with e.g. googleAI.model('gemini-2.5-pro')\n */\nexport const googleAI = googleAIPlugin as GoogleAIPlugin;\n// provide generic implementation for the model function overloads.\n(googleAI as any).model = (\n  name: string,\n  config?: any\n): ModelReference<z.ZodTypeAny> => {\n  if (name.startsWith('imagen')) {\n    return modelRef({\n      name: `googleai/${name}`,\n      config,\n      configSchema: ImagenConfigSchema,\n    });\n  }\n  if (name.startsWith('veo')) {\n    return modelRef({\n      name: `googleai/${name}`,\n      config,\n      configSchema: VeoConfigSchema,\n    });\n  }\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,EACE;AAAA,EACA;AAAA,EACA;AAAA,OAMK;AACP,SAAS,cAAc;AACvB,SAAS,gBAAgB;AACzB,SAAS,oBAAuC;AAEhD,SAAS,2BAA2B;AACpC;AAAA,EACE,oBAAoB;AAAA,EACpB;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OAEK;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;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,OAEK;AACP,SAAS,kBAAkB;AAC3B;AAAA,EACE;AAAA,EAEA;AAAA,EACA;AAAA,OACK;AA6CP,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,uBAAuB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