@@ -672,13 +672,26 @@ export namespace Provider {
672672 }
673673 } ) ,
674674 "cloudflare-workers-ai" : Effect . fnUntraced ( function * ( input : Info ) {
675- const accountId = Env . get ( "CLOUDFLARE_ACCOUNT_ID" )
676- if ( ! accountId ) return { autoload : false }
675+ // When baseURL is already configured (e.g. corporate config routing through a proxy/gateway),
676+ // skip the account ID check because the URL is already fully specified.
677+ if ( input . options ?. baseURL ) return { autoload : false }
678+
679+ const auth = yield * dep . auth ( input . id )
680+ const accountId =
681+ Env . get ( "CLOUDFLARE_ACCOUNT_ID" ) || ( auth ?. type === "api" ? auth . metadata ?. accountId : undefined )
682+ if ( ! accountId )
683+ return {
684+ autoload : false ,
685+ async getModel ( ) {
686+ throw new Error (
687+ "CLOUDFLARE_ACCOUNT_ID is missing. Set it with: export CLOUDFLARE_ACCOUNT_ID=<your-account-id>" ,
688+ )
689+ } ,
690+ }
677691
678692 const apiKey = yield * Effect . gen ( function * ( ) {
679693 const envToken = Env . get ( "CLOUDFLARE_API_KEY" )
680694 if ( envToken ) return envToken
681- const auth = yield * dep . auth ( input . id )
682695 if ( auth ?. type === "api" ) return auth . key
683696 return undefined
684697 } )
@@ -702,16 +715,34 @@ export namespace Provider {
702715 }
703716 } ) ,
704717 "cloudflare-ai-gateway" : Effect . fnUntraced ( function * ( input : Info ) {
705- const accountId = Env . get ( "CLOUDFLARE_ACCOUNT_ID" )
706- const gateway = Env . get ( "CLOUDFLARE_GATEWAY_ID" )
718+ // When baseURL is already configured (e.g. corporate config), skip the ID checks.
719+ if ( input . options ?. baseURL ) return { autoload : false }
707720
708- if ( ! accountId || ! gateway ) return { autoload : false }
721+ const auth = yield * dep . auth ( input . id )
722+ const accountId =
723+ Env . get ( "CLOUDFLARE_ACCOUNT_ID" ) || ( auth ?. type === "api" ? auth . metadata ?. accountId : undefined )
724+ const gateway =
725+ Env . get ( "CLOUDFLARE_GATEWAY_ID" ) || ( auth ?. type === "api" ? auth . metadata ?. gatewayId : undefined )
726+
727+ if ( ! accountId || ! gateway ) {
728+ const missing = [
729+ ! accountId ? "CLOUDFLARE_ACCOUNT_ID" : undefined ,
730+ ! gateway ? "CLOUDFLARE_GATEWAY_ID" : undefined ,
731+ ] . filter ( ( x ) : x is string => Boolean ( x ) )
732+ return {
733+ autoload : false ,
734+ async getModel ( ) {
735+ throw new Error (
736+ `${ missing . join ( " and " ) } missing. Set with: ${ missing . map ( ( x ) => `export ${ x } =<value>` ) . join ( " && " ) } ` ,
737+ )
738+ } ,
739+ }
740+ }
709741
710742 // Get API token from env or auth - required for authenticated gateways
711743 const apiToken = yield * Effect . gen ( function * ( ) {
712744 const envToken = Env . get ( "CLOUDFLARE_API_TOKEN" ) || Env . get ( "CF_AIG_TOKEN" )
713745 if ( envToken ) return envToken
714- const auth = yield * dep . auth ( input . id )
715746 if ( auth ?. type === "api" ) return auth . key
716747 return undefined
717748 } )
0 commit comments