{"version":3,"sources":["../src/utils.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 { GenerateRequest } from 'genkit';\nimport { EmbedRequest } from 'genkit/embedder';\nimport OpenAI from 'openai';\nimport { PluginOptions } from '.';\n\n/**\n * Inspects the request and if apiKey is provided in config, creates a new client.\n * Otherwise falls back on the `defaultClient`.\n */\nexport function maybeCreateRequestScopedOpenAIClient(\n  pluginOptions: PluginOptions | undefined,\n  request: GenerateRequest | EmbedRequest,\n  defaultClient: OpenAI\n): OpenAI {\n  const requestApiKey =\n    (request as GenerateRequest)?.config?.apiKey ??\n    (request as EmbedRequest)?.options?.apiKey;\n  if (!requestApiKey) {\n    return defaultClient;\n  }\n  return new OpenAI({\n    // if pluginOptions are not passed in we attempt to get options from the default client.\n    ...(pluginOptions ?? defaultClient['_options']),\n    apiKey: requestApiKey,\n  });\n}\n\n/**\n * Gets the model name without certain prefixes.\n */\nexport function toModelName(name: string, prefix?: string): string {\n  const pattern = '^/(background-model|model|models|embedder|embedders)/';\n  const refPrefixes = new RegExp(pattern);\n  const maybePluginRef = name.replace(refPrefixes, '');\n  if (prefix) {\n    const pluginPrefix = new RegExp(`^${prefix}/`, 'g');\n    return maybePluginRef.replace(pluginPrefix, '');\n  } else {\n    return maybePluginRef;\n  }\n}\n"],"mappings":"AAkBA,OAAO,YAAY;AAOZ,SAAS,qCACd,eACA,SACA,eACQ;AACR,QAAM,gBACH,SAA6B,QAAQ,UACrC,SAA0B,SAAS;AACtC,MAAI,CAAC,eAAe;AAClB,WAAO;AAAA,EACT;AACA,SAAO,IAAI,OAAO;AAAA;AAAA,IAEhB,GAAI,iBAAiB,cAAc,UAAU;AAAA,IAC7C,QAAQ;AAAA,EACV,CAAC;AACH;AAKO,SAAS,YAAY,MAAc,QAAyB;AACjE,QAAM,UAAU;AAChB,QAAM,cAAc,IAAI,OAAO,OAAO;AACtC,QAAM,iBAAiB,KAAK,QAAQ,aAAa,EAAE;AACnD,MAAI,QAAQ;AACV,UAAM,eAAe,IAAI,OAAO,IAAI,MAAM,KAAK,GAAG;AAClD,WAAO,eAAe,QAAQ,cAAc,EAAE;AAAA,EAChD,OAAO;AACL,WAAO;AAAA,EACT;AACF;","names":[]}