@@ -141,6 +141,18 @@ function decodeDataUri(value) {
141141 return Buffer . from ( match [ 1 ] , 'base64' ) ;
142142}
143143
144+ function normalizeBaseUrl ( value , fallback ) {
145+ const raw = String ( value || fallback || '' ) . trim ( ) ;
146+ if ( ! raw ) return '' ;
147+ return raw . replace ( / \/ + $ / , '' ) ;
148+ }
149+
150+ function buildHuggingFaceModelUrl ( providerConfig , model ) {
151+ const configuredBase = resolveProviderValue ( providerConfig , 'base_url' , 'HUGGINGFACE_BASE_URL' ) ;
152+ const base = normalizeBaseUrl ( configuredBase , 'https://router.huggingface.co/hf-inference' ) ;
153+ return `${ base } /models/${ encodeURIComponent ( String ( model || '' ) . trim ( ) ) } ` ;
154+ }
155+
144156const NO_TEXT_IMAGE_SUFFIX = [
145157 'STRICT NO-TEXT RULE:' ,
146158 'Do not render any words, letters, numbers, symbols, labels, signs, logos, UI text, speech bubbles, subtitles, captions, or watermarks.' ,
@@ -266,7 +278,7 @@ async function generateTextWithProvider(providerConfig, prompt, runtimeConfig) {
266278 if ( provider === 'huggingface' ) {
267279 const apiKey = resolveProviderValue ( providerConfig , 'api_key' , 'HUGGINGFACE_INFERENCE_API_TOKEN' ) || process . env . HUGGINGFACE_API_KEY || '' ;
268280 if ( ! apiKey ) throw new Error ( 'Missing HUGGINGFACE_INFERENCE_API_TOKEN for Hugging Face text provider' ) ;
269- const { json } = await fetchJson ( `https://api-inference.huggingface.co/models/ ${ model } ` , {
281+ const { json } = await fetchJson ( buildHuggingFaceModelUrl ( providerConfig , model ) , {
270282 method : 'POST' ,
271283 headers : {
272284 Authorization : `Bearer ${ apiKey } ` ,
@@ -449,7 +461,7 @@ async function generateImageWithProvider(providerConfig, prompt, runtimeConfig,
449461 if ( provider === 'huggingface' ) {
450462 const apiKey = resolveProviderValue ( providerConfig , 'api_key' , 'HUGGINGFACE_INFERENCE_API_TOKEN' ) || process . env . HUGGINGFACE_API_KEY || '' ;
451463 if ( ! apiKey ) throw new Error ( 'Missing HUGGINGFACE_INFERENCE_API_TOKEN for Hugging Face image provider' ) ;
452- const response = await withTimeout ( fetch ( `https://api-inference.huggingface.co/models/ ${ model } ` , {
464+ const response = await withTimeout ( fetch ( buildHuggingFaceModelUrl ( providerConfig , model ) , {
453465 method : 'POST' ,
454466 headers : {
455467 Authorization : `Bearer ${ apiKey } ` ,
0 commit comments