Open History to see the last commit
1import { VertexAuthError } from './errors'
2import type { GeminiClientConfig } from '@tanstack/ai-gemini'
3
4export type VertexClientConfig = Omit<
5 GeminiClientConfig,
6 'vertexai' | 'enterprise'
7>
8
9export type VertexVideoConfig = VertexClientConfig & {
10 allowUrlFetch?: boolean
11}
12
13function nonEmpty(value: string | undefined): string | undefined {
14 if (value === undefined || value.length === 0) {
15 return undefined
16 }
17 return value
18}
19
20function readEnv(name: string): string | undefined {
21 if (typeof process === 'undefined' || process.env === undefined) {
22 return undefined
23 }
24 return nonEmpty(process.env[name])
25}
26
27/**
28 * Resolves Vertex Gemini client options.
29 *
30 * Factory fields win. Then env. Then ADC inside `@google/genai`.
31 * Does not read `GEMINI_API_KEY` or `GOOGLE_API_KEY`.
32 */
33export function resolveVertexGeminiOptions(
34 config: VertexClientConfig = {},
35): GeminiClientConfig {
36 const project =
37 nonEmpty(config.project) ??
38 readEnv('GOOGLE_CLOUD_PROJECT') ??
39 readEnv('GOOGLE_VERTEX_PROJECT')
40 const location =
41 nonEmpty(config.location) ??
42 readEnv('GOOGLE_CLOUD_LOCATION') ??
43 readEnv('GOOGLE_VERTEX_LOCATION')
44 const apiKey = nonEmpty(config.apiKey) ?? readEnv('GOOGLE_VERTEX_API_KEY')
45
46 if (
47 apiKey === undefined &&
48 (project === undefined || location === undefined)
49 ) {
50 throw new VertexAuthError(
51 'Vertex Gemini needs project and location, or an express apiKey. Pass project and location on the factory, or set GOOGLE_CLOUD_PROJECT and GOOGLE_CLOUD_LOCATION. For express mode, pass apiKey or set GOOGLE_VERTEX_API_KEY.',
52 )
53 }
54
55 return {
56 ...config,
57 vertexai: true,
58 ...(project === undefined ? {} : { project }),
59 ...(location === undefined ? {} : { location }),
60 ...(apiKey === undefined ? {} : { apiKey }),
61 }
62}