UNPKG

@genkit-ai/vertexai

Version:

Genkit AI framework plugin for Google Cloud Vertex AI APIs including Gemini APIs, Imagen, and more.

1 lines 6.52 kB
{"version":3,"sources":["../../src/common/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 { GenkitError } from 'genkit';\nimport { GoogleAuth, GoogleAuthOptions } from 'google-auth-library';\nimport { CLOUD_PLATFORM_OAUTH_SCOPE } from './constants.mjs';\n\n/**\n * Safely extracts the error message from the error.\n * @param e The error\n * @returns The error message\n */\nexport function extractErrMsg(e: unknown): string {\n let errorMessage = 'An unknown error occurred';\n if (e instanceof Error) {\n errorMessage = e.message;\n } else if (typeof e === 'string') {\n errorMessage = e;\n } else {\n // Fallback for other types\n try {\n errorMessage = JSON.stringify(e);\n } catch (stringifyError) {\n errorMessage = 'Failed to stringify error object';\n }\n }\n return errorMessage;\n}\n\n/**\n * Gets the model name without certain prefixes..\n * e.g. for \"models/vertexai/gemini-2.5-pro\" it returns just 'gemini-2.5-pro'\n * @param name A string containing the model string with possible prefixes\n * @returns the model string stripped of certain prefixes\n */\nexport function modelName(name?: string): string | undefined {\n if (!name) return name;\n\n // Remove any of these prefixes:\n const escapedPrefixes = [\n 'background-model/',\n 'model/',\n 'models/',\n 'embedders/',\n 'vertex-model-garden/',\n 'vertex-rerankers/',\n 'vertexai/',\n ];\n const prefixesToRemove = new RegExp(escapedPrefixes.join('|'), 'g');\n return name.replace(prefixesToRemove, '');\n}\n\n/**\n * Gets the suffix of a model string.\n * Throws if the string is empty.\n * @param name A string containing the model string\n * @returns the model string stripped of prefixes and guaranteed not empty.\n */\nexport function checkModelName(name?: string): string {\n const version = modelName(name);\n if (!version) {\n throw new GenkitError({\n status: 'INVALID_ARGUMENT',\n message: 'Model name is required.',\n });\n }\n return version;\n}\n\nexport const TEST_ONLY = { setFakeDerivedOptions };\n\nfunction setFakeDerivedOptions(options: any) {\n __fake_getDerivedOptions = options;\n}\nlet __fake_getDerivedOptions: any;\n\nfunction parseFirebaseProjectId(): string | undefined {\n if (!process.env.FIREBASE_CONFIG) return undefined;\n try {\n return JSON.parse(process.env.FIREBASE_CONFIG).projectId as string;\n } catch {\n return undefined;\n }\n}\n\ninterface CommonOptions {\n location?: string;\n projectId?: string;\n googleAuth?: GoogleAuthOptions;\n}\n\ninterface DerivedOptions {\n location: string;\n projectId: string;\n authClient: GoogleAuth;\n}\n\nexport async function getDerivedOptions(\n pluginName: string,\n options?: CommonOptions\n): Promise<DerivedOptions> {\n if (__fake_getDerivedOptions) {\n return __fake_getDerivedOptions;\n }\n\n let authOptions = options?.googleAuth;\n let authClient: GoogleAuth;\n const providedProjectId =\n options?.projectId ||\n process.env.GCLOUD_PROJECT ||\n parseFirebaseProjectId();\n if (process.env.GCLOUD_SERVICE_ACCOUNT_CREDS) {\n const serviceAccountCreds = JSON.parse(\n process.env.GCLOUD_SERVICE_ACCOUNT_CREDS\n );\n authOptions = {\n credentials: serviceAccountCreds,\n scopes: [CLOUD_PLATFORM_OAUTH_SCOPE],\n projectId: providedProjectId,\n };\n authClient = new GoogleAuth(authOptions);\n } else {\n authClient = new GoogleAuth(\n authOptions ?? {\n scopes: [CLOUD_PLATFORM_OAUTH_SCOPE],\n projectId: providedProjectId,\n }\n );\n }\n\n const projectId = options?.projectId || (await authClient.getProjectId());\n const location = options?.location || 'us-central1';\n\n if (!location) {\n throw new Error(\n `${pluginName} Plugin is missing the 'location' configuration. Please set the 'GCLOUD_LOCATION' environment variable or explicitly pass 'location' into genkit config.`\n );\n }\n if (!projectId) {\n throw new Error(\n `${pluginName} Plugin is missing the 'project' configuration. Please set the 'GCLOUD_PROJECT' environment variable or explicitly pass 'project' into genkit config.`\n );\n }\n\n return {\n location,\n projectId,\n authClient,\n };\n}\n"],"mappings":"AAgBA,SAAS,mBAAmB;AAC5B,SAAS,kBAAqC;AAC9C,SAAS,kCAAkC;AAOpC,SAAS,cAAc,GAAoB;AAChD,MAAI,eAAe;AACnB,MAAI,aAAa,OAAO;AACtB,mBAAe,EAAE;AAAA,EACnB,WAAW,OAAO,MAAM,UAAU;AAChC,mBAAe;AAAA,EACjB,OAAO;AAEL,QAAI;AACF,qBAAe,KAAK,UAAU,CAAC;AAAA,IACjC,SAAS,gBAAgB;AACvB,qBAAe;AAAA,IACjB;AAAA,EACF;AACA,SAAO;AACT;AAQO,SAAS,UAAU,MAAmC;AAC3D,MAAI,CAAC,KAAM,QAAO;AAGlB,QAAM,kBAAkB;AAAA,IACtB;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACF;AACA,QAAM,mBAAmB,IAAI,OAAO,gBAAgB,KAAK,GAAG,GAAG,GAAG;AAClE,SAAO,KAAK,QAAQ,kBAAkB,EAAE;AAC1C;AAQO,SAAS,eAAe,MAAuB;AACpD,QAAM,UAAU,UAAU,IAAI;AAC9B,MAAI,CAAC,SAAS;AACZ,UAAM,IAAI,YAAY;AAAA,MACpB,QAAQ;AAAA,MACR,SAAS;AAAA,IACX,CAAC;AAAA,EACH;AACA,SAAO;AACT;AAEO,MAAM,YAAY,EAAE,sBAAsB;AAEjD,SAAS,sBAAsB,SAAc;AAC3C,6BAA2B;AAC7B;AACA,IAAI;AAEJ,SAAS,yBAA6C;AACpD,MAAI,CAAC,QAAQ,IAAI,gBAAiB,QAAO;AACzC,MAAI;AACF,WAAO,KAAK,MAAM,QAAQ,IAAI,eAAe,EAAE;AAAA,EACjD,QAAQ;AACN,WAAO;AAAA,EACT;AACF;AAcA,eAAsB,kBACpB,YACA,SACyB;AACzB,MAAI,0BAA0B;A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