diff --git a/packages/google-vertex/src/google-vertex-auth-google-auth-library.ts b/packages/google-vertex/src/google-vertex-auth-google-auth-library.ts index 34832e25765d..307237faee35 100644 --- a/packages/google-vertex/src/google-vertex-auth-google-auth-library.ts +++ b/packages/google-vertex/src/google-vertex-auth-google-auth-library.ts @@ -1,27 +1,32 @@ import { GoogleAuth, GoogleAuthOptions } from 'google-auth-library'; -let authInstance: GoogleAuth | null = null; -let authOptions: GoogleAuthOptions | null = null; +/** + * Creates a auth token generator function for the given options. + * This avoids reference-equality cache invalidation issues. + */ +export function createAuthTokenGenerator(options?: GoogleAuthOptions) { + const auth = new GoogleAuth({ + scopes: ['https://www.googleapis.com/auth/cloud-platform'], + ...options, + }); -function getAuth(options: GoogleAuthOptions) { - if (!authInstance || options !== authOptions) { - authInstance = new GoogleAuth({ - scopes: ['https://www.googleapis.com/auth/cloud-platform'], - ...options, - }); - authOptions = options; - } - return authInstance; + return async function generateAuthToken() { + const client = await auth.getClient(); + const token = await client.getAccessToken(); + return token?.token ?? null; + }; } +/** + * @deprecated Use createAuthTokenGenerator instead. + * This function is kept for backward compatibility. + */ export async function generateAuthToken(options?: GoogleAuthOptions) { - const auth = getAuth(options || {}); + const auth = new GoogleAuth({ + scopes: ['https://www.googleapis.com/auth/cloud-platform'], + ...options, + }); const client = await auth.getClient(); const token = await client.getAccessToken(); return token?.token || null; -} - -// For testing purposes only -export function _resetAuthInstance() { - authInstance = null; -} +} \ No newline at end of file diff --git a/packages/google-vertex/src/google-vertex-provider-node.ts b/packages/google-vertex/src/google-vertex-provider-node.ts index 068149d3f5a4..f5b495bf9b3a 100644 --- a/packages/google-vertex/src/google-vertex-provider-node.ts +++ b/packages/google-vertex/src/google-vertex-provider-node.ts @@ -1,6 +1,6 @@ import { loadOptionalSetting, resolve } from '@ai-sdk/provider-utils'; import { GoogleAuthOptions } from 'google-auth-library'; -import { generateAuthToken } from './google-vertex-auth-google-auth-library'; +import { createAuthTokenGenerator } from './google-vertex-auth-google-auth-library'; import { createVertex as createVertexOriginal, GoogleVertexProvider, @@ -28,6 +28,9 @@ export function createVertex( environmentVariableName: 'GOOGLE_VERTEX_API_KEY', }); + // Create a per-instance auth token generator to avoid reference-equality issues + const generateAuthToken = createAuthTokenGenerator(options.googleAuthOptions); + if (apiKey) { return createVertexOriginal(options); } @@ -35,9 +38,7 @@ export function createVertex( return createVertexOriginal({ ...options, headers: async () => ({ - Authorization: `Bearer ${await generateAuthToken( - options.googleAuthOptions, - )}`, + Authorization: `Bearer ${await generateAuthToken()}`, ...(await resolve(options.headers)), }), }); @@ -46,4 +47,4 @@ export function createVertex( /** * Default Google Vertex AI provider instance. */ -export const vertex = createVertex(); +export const vertex = createVertex(); \ No newline at end of file