@@ -15,6 +15,7 @@ import {
1515} from './schemas'
1616
1717const 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
1920const WHOAMI_REQUEST_TIMEOUT_MS = 10_000
2021const ACCOUNT_WORKSPACE_RETRY_ATTEMPTS = 8
@@ -146,8 +147,9 @@ function delay(ms: number): Promise<void> {
146147
147148function saveAuthMeta ( tokens : Pick < StoredTokens , 'apiUrl' | 'user' > & Partial < Pick < StoredTokens , 'accessToken' > > ) : void {
148149 const previous = loadAuthMeta ( )
150+ const apiUrl = normalizeCloudApiUrl ( tokens . apiUrl )
149151 const accountKey = tokens . accessToken
150- ? deriveCloudAuthAccountKey ( tokens . apiUrl , tokens . accessToken , tokens . user )
152+ ? deriveCloudAuthAccountKey ( apiUrl , tokens . accessToken , tokens . user )
151153 : undefined
152154 const tokenHash = tokens . accessToken ? accountWorkspaceTokenHash ( tokens . accessToken ) : undefined
153155 const accountWorkspace =
@@ -162,7 +164,7 @@ function saveAuthMeta(tokens: Pick<StoredTokens, 'apiUrl' | 'user'> & Partial<Pi
162164 }
163165 : undefined
164166 const meta = {
165- apiUrl : tokens . apiUrl ,
167+ apiUrl,
166168 user : tokens . user ,
167169 ...( accountWorkspace ? { accountWorkspace } : { } )
168170 }
@@ -173,8 +175,9 @@ function loadAuthMeta(): AuthMeta {
173175 try {
174176 const parsed = AuthMetaSchema . safeParse ( JSON . parse ( readFileSync ( getAuthMetaPath ( ) , 'utf8' ) ) )
175177 if ( ! parsed . success ) return { apiUrl : CLOUD_API_URL }
178+ const apiUrl = ( parsed . data . apiUrl ?. trim ( ) || CLOUD_API_URL ) . replace ( / \/ + $ / , '' )
176179 return {
177- apiUrl : parsed . data . apiUrl ?. trim ( ) || CLOUD_API_URL ,
180+ apiUrl : apiUrl === LEGACY_CLOUD_API_URL ? CLOUD_API_URL : apiUrl ,
178181 user : parsed . data . user ,
179182 accountWorkspace : parsed . data . accountWorkspace
180183 }
@@ -321,7 +324,7 @@ function accountWorkspaceIdFromWhoami(value: unknown): string | undefined {
321324function saveAccountWorkspaceCache ( auth : CloudAuth , workspaceId : string ) : void {
322325 const previous = loadAuthMeta ( )
323326 const meta = {
324- apiUrl : auth . apiUrl || previous . apiUrl ?. trim ( ) || CLOUD_API_URL ,
327+ apiUrl : normalizeCloudApiUrl ( auth . apiUrl || previous . apiUrl ) ,
325328 user : previous . user ,
326329 accountWorkspace : {
327330 accountKey : auth . accountKey ,
@@ -578,7 +581,7 @@ async function performTokenRefresh(stored: StoredTokens): Promise<StoredTokens |
578581
579582export function getApiUrl ( ) : string {
580583 if ( hasStoredTokens ( ) ) {
581- return loadAuthMeta ( ) . apiUrl || CLOUD_API_URL
584+ return normalizeCloudApiUrl ( loadAuthMeta ( ) . apiUrl )
582585 }
583586 return CLOUD_API_URL
584587}
@@ -600,7 +603,7 @@ function cloudAuthFromStored(tokens: StoredTokens): CloudAuth {
600603
601604function normalizeCloudApiUrl ( url : string | undefined ) : string {
602605 const normalized = ( url || getApiUrl ( ) ) . trim ( ) . replace ( / \/ + $ / , '' )
603- if ( normalized === 'https://agentrelay.dev/cloud' ) return CLOUD_API_URL
606+ if ( normalized === LEGACY_CLOUD_API_URL ) return CLOUD_API_URL
604607 return normalized
605608}
606609
@@ -699,8 +702,9 @@ export async function getAccountWorkspaceId(options: AccountWorkspaceIdOptions =
699702 if ( ! auth ) throw new Error ( 'cloud-auth-required' )
700703
701704 const cached = loadAuthMeta ( ) . accountWorkspace
702- if ( accountWorkspaceCacheMatches ( cached , auth ) ) {
703- return cached . workspaceId . trim ( )
705+ const cachedWorkspaceId = cached ?. workspaceId . trim ( )
706+ if ( cachedWorkspaceId && accountWorkspaceCacheMatches ( cached , auth ) ) {
707+ return cachedWorkspaceId
704708 }
705709
706710 const retryAttempts = Math . max ( 1 , Math . floor ( options . retryAttempts ?? 1 ) )
0 commit comments