{"version":3,"sources":["../../src/googleai/index.ts"],"sourcesContent":["/**\n * Copyright 2025 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  EmbedderReference,\n  GenkitError,\n  ModelReference,\n  z,\n} from 'genkit';\nimport { logger } from 'genkit/logging';\nimport {\n  genkitPluginV2,\n  type GenkitPluginV2,\n  type ResolvableAction,\n} from 'genkit/plugin';\nimport { ActionType } from 'genkit/registry';\nimport { extractErrMsg } from '../common/utils.mjs';\nimport { listModels } from './client.mjs';\nimport { GoogleAIPluginOptions } from './types.mjs';\nimport { calculateApiKey } from './utils.mjs';\n\n// These are namespaced because they all intentionally have\n// functions of the same name with the same arguments.\n// (All exports from these files are used here)\nimport * as antigravity from './antigravity.mjs';\nimport * as deepResearch from './deep-research.mjs';\nimport * as embedder from './embedder.mjs';\nimport * as gemini from './gemini.mjs';\nimport * as lyria from './lyria.mjs';\nimport * as veo from './veo.mjs';\n\nexport {\n  type GeminiInteraction,\n  type TextAnnotation,\n  type UrlCitation,\n} from '../common/interaction-types.mjs';\nexport { type AntigravityConfig } from './antigravity.mjs';\nexport { type DeepResearchConfig } from './deep-research.mjs';\nexport { type EmbeddingConfig } from './embedder.mjs';\nexport {\n  type GeminiConfig,\n  type GeminiImageConfig,\n  type GeminiTtsConfig,\n  type GeminiVideoConfig,\n} from './gemini.mjs';\nexport { type LyriaConfig } from './lyria.mjs';\nexport { type GoogleAIPluginOptions };\n\nasync function initializer(options?: GoogleAIPluginOptions) {\n  return [\n    ...gemini.listKnownModels(options),\n    ...embedder.listKnownModels(options),\n    ...veo.listKnownModels(options),\n    ...deepResearch.listKnownModels(options),\n    ...lyria.listKnownModels(options),\n    ...antigravity.listKnownModels(options),\n  ];\n}\n\nasync function resolver(\n  actionType: ActionType,\n  actionName: string,\n  options: GoogleAIPluginOptions\n): Promise<ResolvableAction | undefined> {\n  switch (actionType) {\n    case 'model':\n      if (\n        veo.isVeoModelName(actionName) ||\n        deepResearch.isDeepResearchModelName(actionName)\n      ) {\n        // These are background models. Do not define them here.\n        return undefined;\n      } else if (actionName.startsWith('imagen-')) {\n        // Otherwise these would resolve as Gemini models and fail with a\n        // confusing \"model not found\".\n        throw new GenkitError({\n          status: 'NOT_FOUND',\n          message:\n            `Model '${actionName}' is not available: Imagen models on the ` +\n            'Gemini API have been shut down. Use gemini-nano-banana-2.1 ' +\n            'instead.',\n        });\n      } else if (lyria.isLyriaModelName(actionName)) {\n        return lyria.defineModel(actionName, options);\n      } else if (antigravity.isAntigravityModelName(actionName)) {\n        return antigravity.defineModel(actionName, options);\n      } else {\n        // gemini, tts, image, video, gemma, unknown models\n        return gemini.defineModel(actionName, options);\n      }\n    case 'background-model':\n      if (veo.isVeoModelName(actionName)) {\n        return veo.defineModel(actionName, options);\n      } else if (deepResearch.isDeepResearchModelName(actionName)) {\n        return deepResearch.defineModel(actionName, options);\n      }\n      break; // No default\n    case 'embedder':\n      return embedder.defineEmbedder(actionName, options);\n  }\n  return undefined;\n}\n\nasync function listActions(\n  options?: GoogleAIPluginOptions\n): Promise<ActionMetadata[]> {\n  // Don't attempt to list models if apiKey is set to false.\n  if (options?.apiKey === false) {\n    return [];\n  }\n  try {\n    const apiKey = calculateApiKey(options?.apiKey, undefined);\n    const allModels = await listModels(apiKey, {\n      baseUrl: options?.baseUrl,\n      apiVersion: options?.apiVersion,\n    });\n\n    return [\n      ...gemini.listActions(allModels),\n      ...veo.listActions(allModels),\n      ...deepResearch.listActions(allModels),\n      ...lyria.listActions(allModels),\n      ...antigravity.listActions(allModels),\n      ...embedder.listActions(allModels),\n    ];\n  } catch (e: unknown) {\n    logger.error(extractErrMsg(e));\n    return [];\n  }\n}\n\n/**\n * Google Gemini Developer API plugin.\n */\nexport function googleAIPlugin(\n  options?: GoogleAIPluginOptions\n): GenkitPluginV2 {\n  let listActionsCache: ActionMetadata[];\n  return genkitPluginV2({\n    name: 'googleai',\n    init: async () => await initializer(options),\n    resolve: async (actionType: ActionType, actionName: string) =>\n      await resolver(actionType, actionName, options || {}),\n    list: async () => {\n      if (listActionsCache) return listActionsCache;\n      listActionsCache = await listActions(options);\n      return listActionsCache;\n    },\n  });\n}\n\nexport type GoogleAIPlugin = {\n  (pluginOptions?: GoogleAIPluginOptions): GenkitPluginV2;\n  model(\n    name: gemini.KnownGemmaModels | (gemini.GemmaModelName & {}),\n    config: gemini.GemmaConfig\n  ): ModelReference<gemini.GemmaConfigSchemaType>;\n  model(\n    name: gemini.KnownTtsModels | (gemini.TTSModelName & {}),\n    config: gemini.GeminiTtsConfig\n  ): ModelReference<gemini.GeminiTtsConfigSchemaType>;\n  model(\n    name: gemini.KnownImageModels | (gemini.ImageModelName & {}),\n    config: gemini.GeminiImageConfig\n  ): ModelReference<gemini.GeminiImageConfigSchemaType>;\n  model(\n    name: gemini.KnownVideoModels | (gemini.VideoModelName & {}),\n    config?: gemini.GeminiVideoConfig\n  ): ModelReference<gemini.GeminiVideoConfigSchemaType>;\n  model(\n    name: gemini.KnownGeminiModels | (gemini.GeminiModelName & {}),\n    config?: gemini.GeminiConfig\n  ): ModelReference<gemini.GeminiConfigSchemaType>;\n  model(\n    name: veo.KnownModels | (veo.VeoModelName & {}),\n    config?: veo.VeoConfig\n  ): ModelReference<veo.VeoConfigSchemaType>;\n  model(\n    name: deepResearch.KnownModels | (deepResearch.DeepResearchModelName & {}),\n    config?: deepResearch.DeepResearchConfig\n  ): ModelReference<deepResearch.DeepResearchConfigSchemaType>;\n  model(\n    name: lyria.KnownModels | (lyria.LyriaModelName & {}),\n    config?: lyria.LyriaConfig\n  ): ModelReference<lyria.LyriaConfigSchemaType>;\n  model(\n    name: antigravity.KnownModels | (antigravity.AntigravityModelName & {}),\n    config?: antigravity.AntigravityConfig\n  ): ModelReference<antigravity.AntigravityConfigSchemaType>;\n  model(name: string, config?: any): ModelReference<z.ZodTypeAny>;\n\n  embedder(\n    name: embedder.KnownModels | (embedder.EmbedderModelName & {}),\n    config?: embedder.EmbeddingConfig\n  ): EmbedderReference<embedder.EmbeddingConfigSchemaType>;\n};\n\n/**\n * Google Gemini Developer API plugin.\n */\nexport const googleAI = googleAIPlugin as GoogleAIPlugin;\n(googleAI as any).model = (\n  name: string,\n  config?: any\n): ModelReference<z.ZodTypeAny> => {\n  if (veo.isVeoModelName(name)) {\n    return veo.model(name, config);\n  }\n  if (deepResearch.isDeepResearchModelName(name)) {\n    return deepResearch.model(name, config);\n  }\n  if (lyria.isLyriaModelName(name)) {\n    return lyria.model(name, config);\n  }\n  if (antigravity.isAntigravityModelName(name)) {\n    return antigravity.model(name, config);\n  }\n  // gemma, tts, image, video, gemini and unknown model families.\n  return gemini.model(name, config);\n};\ngoogleAI.embedder = (\n  name: string,\n  config?: embedder.EmbeddingConfig\n): EmbedderReference<embedder.EmbeddingConfigSchemaType> => {\n  return embedder.model(name, config);\n};\n\nexport default googleAI;\n"],"mappings":"AAgBA;AAAA,EAGE;AAAA,OAGK;AACP,SAAS,cAAc;AACvB;AAAA,EACE;AAAA,OAGK;AAEP,SAAS,qBAAqB;AAC9B,SAAS,kBAAkB;AAE3B,SAAS,uBAAuB;AAKhC,YAAY,iBAAiB;AAC7B,YAAY,kBAAkB;AAC9B,YAAY,cAAc;AAC1B,YAAY,YAAY;AACxB,YAAY,WAAW;AACvB,YAAY,SAAS;AAmBrB,eAAe,YAAY,SAAiC;AAC1D,SAAO;AAAA,IACL,GAAG,OAAO,gBAAgB,OAAO;AAAA,IACjC,GAAG,SAAS,gBAAgB,OAAO;AAAA,IACnC,GAAG,IAAI,gBAAgB,OAAO;AAAA,IAC9B,GAAG,aAAa,gBAAgB,OAAO;AAAA,IACvC,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