{"version":3,"sources":["../src/index.ts"],"sourcesContent":["/**\n * Copyright 2024 Bloom Labs Inc\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 Anthropic from '@anthropic-ai/sdk';\nimport { genkitPluginV2, type GenkitPluginV2 } from 'genkit/plugin';\n\nimport type { Part } from 'genkit';\nimport { z, type ActionMetadata, type ModelReference } from 'genkit';\nimport { type ModelAction } from 'genkit/model';\nimport { type ActionType } from 'genkit/registry';\nimport { listActions } from './list.mjs';\nimport {\n  claudeModel,\n  claudeModelReference,\n  listKnownModels,\n  type ClaudeModelName,\n  type KnownClaudeModels,\n} from './models.mjs';\nimport {\n  __testClient,\n  type AnthropicConfigSchemaType,\n  type AnthropicDocumentOptions,\n  type ClaudeConfig,\n  type InternalPluginOptions,\n  type PluginOptions,\n} from './types.mjs';\n\n// Re-export types and utilities for consumers\nexport type { AnthropicCacheControl, AnthropicCitation } from './types.mjs';\nexport { cacheControl } from './utils.mjs';\n\n/**\n * Gets or creates an Anthropic client instance.\n * Supports test client injection for internal testing.\n */\nfunction getAnthropicClient(options?: PluginOptions): Anthropic {\n  // Check for test client injection first (internal use only)\n  const internalOptions = options as InternalPluginOptions | undefined;\n  if (internalOptions?.[__testClient]) {\n    return internalOptions[__testClient];\n  }\n\n  // Production path: create real client\n  const apiKey = options?.apiKey || process.env.ANTHROPIC_API_KEY;\n  if (!apiKey) {\n    throw new Error(\n      'Please pass in the API key or set the ANTHROPIC_API_KEY environment variable'\n    );\n  }\n  return new Anthropic({ apiKey });\n}\n\n/**\n * This module provides an interface to the Anthropic AI models through the Genkit plugin system.\n * It allows users to interact with various Claude models by providing an API key and optional configuration.\n *\n * The main export is the `anthropic` plugin, which can be configured with an API key either directly or through\n * environment variables. It initializes the Anthropic client and makes available the Claude models for use.\n *\n * Exports:\n * - anthropic: The main plugin function to interact with the Anthropic AI.\n *\n * Usage:\n * To use the Claude models, initialize the anthropic plugin inside `genkit()` and pass the configuration options. If no API key is provided in the options, the environment variable `ANTHROPIC_API_KEY` must be set.\n *\n * Example:\n * ```\n * import { anthropic } from '@genkit-ai/anthropic';\n * import { genkit } from 'genkit';\n *\n * const ai = genkit({\n *  plugins: [\n *    anthropic({ apiKey: 'your-api-key' })\n *    ... // other plugins\n *  ]\n * });\n *\n * // Access models via the plugin's model() method:\n * const model = anthropic.model('claude-sonnet-4');\n * ```\n */\nfunction anthropicPlugin(options?: PluginOptions): GenkitPluginV2 {\n  const client = getAnthropicClient(options);\n  const defaultApiVersion = options?.apiVersion;\n\n  let listActionsCache: ActionMetadata[] | null = null;\n\n  return genkitPluginV2({\n    name: 'anthropic',\n    init: async () => {\n      const actions: ModelAction[] = [];\n      actions.push(...listKnownModels(client, defaultApiVersion));\n      return actions;\n    },\n    resolve: (actionType: ActionType, name: string) => {\n      if (actionType === 'model') {\n        // Strip the 'anthropic/' namespace prefix if present\n        const modelName = name.startsWith('anthropic/') ? name.slice(10) : name;\n        return claudeModel({\n          name: modelName,\n          client,\n          defaultApiVersion,\n        });\n      }\n      return undefined;\n    },\n    list: async () => {\n      if (listActionsCache) return listActionsCache;\n      listActionsCache = await listActions(client);\n      return listActionsCache;\n    },\n  });\n}\n\nexport type AnthropicPlugin = {\n  (pluginOptions?: PluginOptions): GenkitPluginV2;\n  model(\n    name: KnownClaudeModels | (ClaudeModelName & {}),\n    config?: ClaudeConfig\n  ): ModelReference<AnthropicConfigSchemaType>;\n  model(name: string, config?: any): ModelReference<z.ZodTypeAny>;\n};\n\n/**\n * Anthropic AI plugin for Genkit.\n * Includes Claude models (3, 3.5, and 4 series).\n */\nexport const anthropic = anthropicPlugin as AnthropicPlugin;\n(anthropic as any).model = (\n  name: string,\n  config?: any\n): ModelReference<z.ZodTypeAny> => {\n  return claudeModelReference(name, config);\n};\n\n/**\n * Creates a custom part representing an Anthropic document with optional citations support.\n *\n * Use this to provide documents to Claude that can be cited in responses.\n * Citations must be enabled on all or none of the documents in a request.\n *\n * @example\n * ```ts\n * import { anthropic, anthropicDocument } from '@genkit-ai/anthropic';\n *\n * const { text } = await ai.generate({\n *   model: anthropic.model('claude-sonnet-4-5'),\n *   messages: [{\n *     role: 'user',\n *     content: [\n *       anthropicDocument({\n *         source: { type: 'text', data: 'The grass is green. The sky is blue.' },\n *         title: 'Nature Facts',\n *         citations: { enabled: true }\n *       }),\n *       { text: 'What color is the grass?' }\n *     ]\n *   }]\n * });\n * ```\n */\nexport function anthropicDocument(options: AnthropicDocumentOptions): Part {\n  return {\n    custom: {\n      anthropicDocument: options,\n    },\n  };\n}\n\nexport default anthropic;\n"],"mappings":"AAiBA,OAAO,eAAe;AACtB,SAAS,sBAA2C;AAMpD,SAAS,mBAAmB;AAC5B;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,OAGK;AACP;AAAA,EACE;AAAA,OAMK;AAIP,SAAS,oBAAoB;AAM7B,SAAS,mBAAmB,SAAoC;AAE9D,QAAM,kBAAkB;AACxB,MAAI,kBAAkB,YAAY,GAAG;AACnC,WAAO,gBAAgB,YAAY;AAAA,EACrC;AAGA,QAAM,SAAS,SAAS,UAAU,QAAQ,IAAI;AAC9C,MAAI,CAAC,QAAQ;AACX,UAAM,IAAI;AAAA,MACR;AAAA,IACF;AAAA,EACF;AACA,SAAO,IAAI,UAAU,EAAE,OAAO,CAAC;AACjC;AA+BA,SAAS,gBAAgB,SAAyC;AAChE,QAAM,SAAS,mBAAmB,OAAO;AACzC,QAAM,oBAAoB,SAAS;AAEnC,MAAI,mBAA4C;AAEhD,SAAO,eAAe;AAAA,IACpB,MAAM;AAAA,IACN,MAAM,YAAY;AAChB,YAAM,UAAyB,CAAC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