@@ -14,8 +14,10 @@ import {
1414 type UserInfo
1515} from './schemas'
1616
17- const CLOUD_API_URL = process . env . RELAY_CLOUD_URL || 'https://agentrelay.dev/cloud'
17+ const CLOUD_API_URL = process . env . RELAY_CLOUD_URL || 'https://agentrelay.com/cloud'
18+ const LEGACY_CLOUD_API_URL = 'https://agentrelay.dev/cloud'
1819const TOKEN_EXPIRY_BUFFER_MS = 60_000
20+ const WHOAMI_REQUEST_TIMEOUT_MS = 10_000
1921const ACCOUNT_WORKSPACE_RETRY_ATTEMPTS = 8
2022const ACCOUNT_WORKSPACE_RETRY_DELAY_MS = 500
2123const warnedWhoamiWorkspaceFailures = new Set < string > ( )
@@ -145,8 +147,9 @@ function delay(ms: number): Promise<void> {
145147
146148function saveAuthMeta ( tokens : Pick < StoredTokens , 'apiUrl' | 'user' > & Partial < Pick < StoredTokens , 'accessToken' > > ) : void {
147149 const previous = loadAuthMeta ( )
150+ const apiUrl = normalizeCloudApiUrl ( tokens . apiUrl )
148151 const accountKey = tokens . accessToken
149- ? deriveCloudAuthAccountKey ( tokens . apiUrl , tokens . accessToken , tokens . user )
152+ ? deriveCloudAuthAccountKey ( apiUrl , tokens . accessToken , tokens . user )
150153 : undefined
151154 const tokenHash = tokens . accessToken ? accountWorkspaceTokenHash ( tokens . accessToken ) : undefined
152155 const accountWorkspace =
@@ -161,7 +164,7 @@ function saveAuthMeta(tokens: Pick<StoredTokens, 'apiUrl' | 'user'> & Partial<Pi
161164 }
162165 : undefined
163166 const meta = {
164- apiUrl : tokens . apiUrl ,
167+ apiUrl,
165168 user : tokens . user ,
166169 ...( accountWorkspace ? { accountWorkspace } : { } )
167170 }
@@ -172,8 +175,9 @@ function loadAuthMeta(): AuthMeta {
172175 try {
173176 const parsed = AuthMetaSchema . safeParse ( JSON . parse ( readFileSync ( getAuthMetaPath ( ) , 'utf8' ) ) )
174177 if ( ! parsed . success ) return { apiUrl : CLOUD_API_URL }
178+ const apiUrl = ( parsed . data . apiUrl ?. trim ( ) || CLOUD_API_URL ) . replace ( / \/ + $ / , '' )
175179 return {
176- apiUrl : parsed . data . apiUrl ?. trim ( ) || CLOUD_API_URL ,
180+ apiUrl : apiUrl === LEGACY_CLOUD_API_URL ? CLOUD_API_URL : apiUrl ,
177181 user : parsed . data . user ,
178182 accountWorkspace : parsed . data . accountWorkspace
179183 }
@@ -225,8 +229,9 @@ function loadTokens(): StoredTokens | null {
225229 const decrypted = safeStorage . decryptString ( raw )
226230 const parsed = StoredTokensSchema . safeParse ( JSON . parse ( decrypted ) )
227231 if ( ! parsed . success ) return null
228- saveAuthMeta ( parsed . data )
229- return parsed . data
232+ const tokens = { ...parsed . data , apiUrl : normalizeCloudApiUrl ( parsed . data . apiUrl ) }
233+ saveAuthMeta ( tokens )
234+ return tokens
230235 } catch {
231236 return null
232237 }
@@ -277,7 +282,7 @@ function warnWhoamiWorkspaceFailure(failureClass: string): void {
277282
278283async function fetchWhoamiPayload ( apiUrl : string , accessToken : string ) : Promise < WhoamiPayloadResult > {
279284 const controller = new AbortController ( )
280- const timeout = setTimeout ( ( ) => controller . abort ( ) , 2500 )
285+ const timeout = setTimeout ( ( ) => controller . abort ( ) , WHOAMI_REQUEST_TIMEOUT_MS )
281286
282287 try {
283288 const res = await fetch ( `${ apiUrl } /api/v1/auth/whoami` , {
@@ -320,7 +325,7 @@ function accountWorkspaceIdFromWhoami(value: unknown): string | undefined {
320325function saveAccountWorkspaceCache ( auth : CloudAuth , workspaceId : string ) : void {
321326 const previous = loadAuthMeta ( )
322327 const meta = {
323- apiUrl : auth . apiUrl || previous . apiUrl ?. trim ( ) || CLOUD_API_URL ,
328+ apiUrl : normalizeCloudApiUrl ( auth . apiUrl || previous . apiUrl ) ,
324329 user : previous . user ,
325330 accountWorkspace : {
326331 accountKey : auth . accountKey ,
@@ -577,7 +582,7 @@ async function performTokenRefresh(stored: StoredTokens): Promise<StoredTokens |
577582
578583export function getApiUrl ( ) : string {
579584 if ( hasStoredTokens ( ) ) {
580- return loadAuthMeta ( ) . apiUrl || CLOUD_API_URL
585+ return normalizeCloudApiUrl ( loadAuthMeta ( ) . apiUrl )
581586 }
582587 return CLOUD_API_URL
583588}
@@ -598,7 +603,9 @@ function cloudAuthFromStored(tokens: StoredTokens): CloudAuth {
598603}
599604
600605function normalizeCloudApiUrl ( url : string | undefined ) : string {
601- return ( url || getApiUrl ( ) ) . trim ( ) . replace ( / \/ + $ / , '' )
606+ const normalized = ( url || CLOUD_API_URL ) . trim ( ) . replace ( / \/ + $ / , '' )
607+ if ( normalized === LEGACY_CLOUD_API_URL ) return CLOUD_API_URL
608+ return normalized
602609}
603610
604611function readJwtPayload ( token : string ) : Record < string , unknown > | null {
@@ -696,8 +703,9 @@ export async function getAccountWorkspaceId(options: AccountWorkspaceIdOptions =
696703 if ( ! auth ) throw new Error ( 'cloud-auth-required' )
697704
698705 const cached = loadAuthMeta ( ) . accountWorkspace
699- if ( accountWorkspaceCacheMatches ( cached , auth ) ) {
700- return cached . workspaceId . trim ( )
706+ const cachedWorkspaceId = cached ?. workspaceId . trim ( )
707+ if ( cachedWorkspaceId && accountWorkspaceCacheMatches ( cached , auth ) ) {
708+ return cachedWorkspaceId
701709 }
702710
703711 const retryAttempts = Math . max ( 1 , Math . floor ( options . retryAttempts ?? 1 ) )
0 commit comments