You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The vertex model doesn't work on vercel edge. This is cause by the auth-library not being compatible with the edge. This could be easily replaced by a vanilla js script:
import{webcrypto}from'crypto'consttokenUrl='https://oauth2.googleapis.com/token';constbase64url=(source)=>{letencodedSource=Buffer.from(source).toString('base64');encodedSource=encodedSource.replace(/=+$/,'');encodedSource=encodedSource.replace(/\+/g,'-');encodedSource=encodedSource.replace(/\//g,'_');returnencodedSource;}constcreateJwtSegment=(segment)=>base64url(JSON.stringify(segment));constimportPrivateKey=async(pemKey)=>{constpemHeader="-----BEGIN PRIVATE KEY-----";constpemFooter="-----END PRIVATE KEY-----";constpemContents=pemKey.substring(pemHeader.length,pemKey.length-pemFooter.length);constbinaryDerString=Buffer.from(pemContents,'base64').toString('binary');constbinaryDer=newUint8Array(binaryDerString.length);for(leti=0;i<binaryDerString.length;i++){binaryDer[i]=binaryDerString.charCodeAt(i);}returnwebcrypto.subtle.importKey("pkcs8",binaryDer.buffer,{name: "RSASSA-PKCS1-v1_5",hash: "SHA-256"},true,["sign"]);}constsignJwt=async(header,payload,privateKey)=>{constdata=`${header}.${payload}`;constencoder=newTextEncoder();constsignature=awaitwebcrypto.subtle.sign("RSASSA-PKCS1-v1_5",privateKey,encoder.encode(data));return`${data}.${base64url(Buffer.from(signature))}`;};constbuildJwt=async({ client_email, private_key, private_key_id, scopes })=>{constiat=Math.floor(Date.now()/1000);constexp=iat+3600;constpayload={iss: client_email,sub: client_email,aud: tokenUrl,iat: iat,exp: exp,scope: scopes.join(" ")}constjwtHeaders={kid: private_key_id,alg: 'RS256',typ: 'JWT'};constencodedHeader=createJwtSegment(jwtHeaders);constencodedPayload=createJwtSegment(payload);// Import the private keyconstprivateKey=awaitimportPrivateKey(private_key);// Sign the JWTconstsignedJwt=awaitsignJwt(encodedHeader,encodedPayload,privateKey);returnsignedJwt;}// Function to get access tokenconstgenerateToken=async(credentials)=>{// 1) Build and sign JWTconstsignedJwt=awaitbuildJwt(credentials)// 2) Get access tokenconstresponse=awaitfetch(tokenUrl,{method: 'POST',headers: {'Content-Type': 'application/x-www-form-urlencoded'},body: newURLSearchParams({'grant_type': 'urn:ietf:params:oauth:grant-type:jwt-bearer','assertion': signedJwt})})if(!response.ok){thrownewError(`Token request failed: ${response.statusText}`);}returnawaitresponse.json()}
It can easily be used like this:
const{ expires_in, access_token }=awaitgenerateToken({client_email:process.env.GOOGLE_CLOUD_CLIENT_EMAIL,private_key: process.env.GOOGLE_CLOUD_PRIVATE_KEY?.replace(/\\n/gm,"\n"),// Fix issue with new lineprivate_key_id: process.env.GOOGLE_CLOUD_PRIVATE_KEY_ID,scopes: ["https://www.googleapis.com/auth/cloud-platform"]})
Use Case
Would be great to decouple vertex and auto so that we can use our custom auth script like the one above or simply add the script above directly in the ai-sdk model so that it would work on vercel edge.
Additional context
No response
The text was updated successfully, but these errors were encountered:
Feature Description
The vertex model doesn't work on vercel edge. This is cause by the auth-library not being compatible with the edge. This could be easily replaced by a vanilla js script:
It can easily be used like this:
Use Case
Would be great to decouple vertex and auto so that we can use our custom auth script like the one above or simply add the script above directly in the ai-sdk model so that it would work on vercel edge.
Additional context
No response
The text was updated successfully, but these errors were encountered: