{"version":3,"sources":["../../src/xai/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  GenkitError,\n  modelActionMetadata,\n  ModelReference,\n  z,\n} from 'genkit';\nimport { logger } from 'genkit/logging';\nimport { ResolvableAction, type GenkitPluginV2 } from 'genkit/plugin';\nimport { ActionType } from 'genkit/registry';\nimport OpenAI from 'openai';\nimport {\n  defineCompatOpenAIImageModel,\n  ImageGenerationCommonConfigSchema,\n} from '../image.mjs';\nimport { openAICompatible, PluginOptions } from '../index.mjs';\nimport { defineCompatOpenAIModel } from '../model.mjs';\nimport { SUPPORTED_IMAGE_MODELS, xaiImageModelRef } from './grok-image.mjs';\nimport {\n  grokRequestBuilder,\n  SUPPORTED_LANGUAGE_MODELS,\n  XaiChatCompletionConfigSchema,\n  xaiModelRef,\n} from './grok.mjs';\n\nexport type XAIPluginOptions = Omit<PluginOptions, 'name' | 'baseURL'>;\n\nfunction createResolver(pluginOptions: PluginOptions) {\n  return async (client: OpenAI, actionType: ActionType, actionName: string) => {\n    if (actionType === 'model') {\n      const modelRef = xaiModelRef({ name: actionName });\n      return defineCompatOpenAIModel({\n        name: modelRef.name,\n        client,\n        pluginOptions,\n        modelRef,\n        requestBuilder: grokRequestBuilder,\n      });\n    } else {\n      logger.warn('Only model actions are supported by the XAI plugin');\n    }\n    return undefined;\n  };\n}\n\nconst listActions = async (client: OpenAI): Promise<ActionMetadata[]> => {\n  return await client.models.list().then((response) =>\n    response.data\n      .filter((model) => model.object === 'model')\n      .map((model: OpenAI.Model) => {\n        if (model.id.includes('image')) {\n          const modelRef =\n            SUPPORTED_IMAGE_MODELS[model.id] ??\n            xaiImageModelRef({ name: model.id });\n          return modelActionMetadata({\n            name: modelRef.name,\n            info: modelRef.info,\n            configSchema: modelRef.configSchema,\n          });\n        } else {\n          const modelRef =\n            SUPPORTED_LANGUAGE_MODELS[model.id] ??\n            xaiModelRef({ name: model.id });\n          return modelActionMetadata({\n            name: modelRef.name,\n            info: modelRef.info,\n            configSchema: modelRef.configSchema,\n          });\n        }\n      })\n  );\n};\n\nexport function xAIPlugin(options?: XAIPluginOptions): GenkitPluginV2 {\n  const apiKey = options?.apiKey ?? process.env.XAI_API_KEY;\n  if (!apiKey) {\n    throw new GenkitError({\n      status: 'FAILED_PRECONDITION',\n      message:\n        'Please pass in the API key or set the XAI_API_KEY environment variable.',\n    });\n  }\n  const pluginOptions = { name: 'xai', ...options };\n  return openAICompatible({\n    name: 'xai',\n    baseURL: 'https://api.x.ai/v1',\n    apiKey,\n    ...options,\n    initializer: async (client) => {\n      const models = [] as ResolvableAction[];\n      models.push(\n        ...Object.values(SUPPORTED_LANGUAGE_MODELS).map((modelRef) =>\n          defineCompatOpenAIModel({\n            name: modelRef.name,\n            client,\n            pluginOptions,\n            modelRef,\n            requestBuilder: grokRequestBuilder,\n          })\n        )\n      );\n      models.push(\n        ...Object.values(SUPPORTED_IMAGE_MODELS).map((modelRef) =>\n          defineCompatOpenAIImageModel({\n            name: modelRef.name,\n            client,\n            pluginOptions,\n            modelRef,\n          })\n        )\n      );\n      return models;\n    },\n    resolver: createResolver(pluginOptions),\n    listActions,\n  });\n}\n\nexport type XAIPlugin = {\n  (params?: XAIPluginOptions): GenkitPluginV2;\n  model(\n    name: keyof typeof SUPPORTED_LANGUAGE_MODELS,\n    config?: z.infer<typeof XaiChatCompletionConfigSchema>\n  ): ModelReference<typeof XaiChatCompletionConfigSchema>;\n  model(\n    name: keyof typeof SUPPORTED_IMAGE_MODELS,\n    config?: z.infer<typeof ImageGenerationCommonConfigSchema>\n  ): ModelReference<typeof ImageGenerationCommonConfigSchema>;\n  model(name: string, config?: any): ModelReference<z.ZodTypeAny>;\n};\n\nconst model = ((name: string, config?: any): ModelReference<z.ZodTypeAny> => {\n  if (name.includes('image')) {\n    return xaiImageModelRef({\n      name,\n      config,\n    });\n  }\n  return xaiModelRef({\n    name,\n    config,\n  });\n}) as XAIPlugin['model'];\n\n/**\n * This module provides an interface to the XAI models through the Genkit\n * plugin system. It allows users to interact with various models by providing\n * an API key and optional configuration.\n *\n * The main export is the `xai` plugin, which can be configured with an API\n * key either directly or through environment variables. It initializes the\n * OpenAI client and makes available the models for use.\n *\n * Exports:\n * - xAI: The main plugin function to interact with XAI, via OpenAI\n *   compatible API.\n *\n * Usage: To use the models, initialize the xAI plugin inside\n * `configureGenkit` and pass the configuration options. If no API key is\n * provided in the options, the environment variable `XAI_API_KEY` must be\n * set.\n *\n * Example:\n * ```\n * import { xAI } from '@genkit-ai/compat-oai/xai';\n *\n * export default configureGenkit({\n *  plugins: [\n *    xAI()\n *    ... // other plugins\n *  ]\n * });\n * ```\n */\nexport const xAI: XAIPlugin = Object.assign(xAIPlugin, {\n  model,\n});\n\nexport default xAI;\n"],"mappings":"AAgBA;AAAA,EAEE;AAAA,EACA;AAAA,OAGK;AACP,SAAS,cAAc;AAIvB;AAAA,EACE;AAAA,OAEK;AACP,SAAS,wBAAuC;AAChD,SAAS,+BAA+B;AACxC,SAAS,wBAAwB,wBAAwB;AACzD;AAAA,EACE;AAAA,EACA;AAAA,EAEA;AAAA,OACK;AAIP,SAAS,eAAe,eAA8B;AACpD,SAAO,OAAO,QAAgB,YAAwB,eAAuB;AAC3E,QAAI,eAAe,SAAS;AAC1B,YAAM,WAAW,YAAY,EAAE,MAAM,WAAW,CAAC;AACjD,aAAO,wBAAwB;AAAA,QAC7B,MAAM,SAAS;AAAA,QACf;AAAA,QACA;AAAA,QACA;AAAA,QACA,gBAAgB;AAAA,MAClB,CAAC;AAAA,IACH,OAAO;AACL,aAAO,K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