@@ -163,6 +163,50 @@ async function truncateTiktoken(
163
163
}
164
164
}
165
165
166
+ /**
167
+ * Gets the Cloudflare Gateway base URL for a specific provider if enabled
168
+ * @param runtime The runtime environment
169
+ * @param provider The model provider name
170
+ * @returns The Cloudflare Gateway base URL if enabled, undefined otherwise
171
+ */
172
+ function getCloudflareGatewayBaseURL ( runtime : IAgentRuntime , provider : string ) : string | undefined {
173
+ const isCloudflareEnabled = runtime . getSetting ( "CLOUDFLARE_GW_ENABLED" ) === "true" ;
174
+ const cloudflareAccountId = runtime . getSetting ( "CLOUDFLARE_AI_ACCOUNT_ID" ) ;
175
+ const cloudflareGatewayId = runtime . getSetting ( "CLOUDFLARE_AI_GATEWAY_ID" ) ;
176
+
177
+ elizaLogger . debug ( "Cloudflare Gateway Configuration:" , {
178
+ isEnabled : isCloudflareEnabled ,
179
+ hasAccountId : ! ! cloudflareAccountId ,
180
+ hasGatewayId : ! ! cloudflareGatewayId ,
181
+ provider : provider
182
+ } ) ;
183
+
184
+ if ( ! isCloudflareEnabled ) {
185
+ elizaLogger . debug ( "Cloudflare Gateway is not enabled" ) ;
186
+ return undefined ;
187
+ }
188
+
189
+ if ( ! cloudflareAccountId ) {
190
+ elizaLogger . warn ( "Cloudflare Gateway is enabled but CLOUDFLARE_AI_ACCOUNT_ID is not set" ) ;
191
+ return undefined ;
192
+ }
193
+
194
+ if ( ! cloudflareGatewayId ) {
195
+ elizaLogger . warn ( "Cloudflare Gateway is enabled but CLOUDFLARE_AI_GATEWAY_ID is not set" ) ;
196
+ return undefined ;
197
+ }
198
+
199
+ const baseURL = `https://gateway.ai.cloudflare.com/v1/${ cloudflareAccountId } /${ cloudflareGatewayId } /${ provider . toLowerCase ( ) } ` ;
200
+ elizaLogger . info ( "Using Cloudflare Gateway:" , {
201
+ provider,
202
+ baseURL,
203
+ accountId : cloudflareAccountId ,
204
+ gatewayId : cloudflareGatewayId
205
+ } ) ;
206
+
207
+ return baseURL ;
208
+ }
209
+
166
210
/**
167
211
* Send a message to the model for a text generateText - receive a string back and parse how you'd like
168
212
* @param opts - The options for the generateText request.
@@ -242,6 +286,16 @@ export async function generateText({
242
286
}
243
287
244
288
const provider = runtime . modelProvider ;
289
+ elizaLogger . debug ( "Provider settings:" , {
290
+ provider,
291
+ hasRuntime : ! ! runtime ,
292
+ runtimeSettings : {
293
+ CLOUDFLARE_GW_ENABLED : runtime . getSetting ( "CLOUDFLARE_GW_ENABLED" ) ,
294
+ CLOUDFLARE_AI_ACCOUNT_ID : runtime . getSetting ( "CLOUDFLARE_AI_ACCOUNT_ID" ) ,
295
+ CLOUDFLARE_AI_GATEWAY_ID : runtime . getSetting ( "CLOUDFLARE_AI_GATEWAY_ID" )
296
+ }
297
+ } ) ;
298
+
245
299
const endpoint =
246
300
runtime . character . modelEndpointOverride || getEndpoint ( provider ) ;
247
301
const modelSettings = getModelSettings ( runtime . modelProvider , modelClass ) ;
@@ -356,13 +410,17 @@ export async function generateText({
356
410
case ModelProviderName . LLAMACLOUD :
357
411
case ModelProviderName . NANOGPT :
358
412
case ModelProviderName . HYPERBOLIC :
413
+ case ModelProviderName . TOGETHER :
359
414
case ModelProviderName . NINETEEN_AI :
360
415
case ModelProviderName . TOGETHER :
361
416
case ModelProviderName . AKASH_CHAT_API : {
362
- elizaLogger . debug ( "Initializing OpenAI model." ) ;
417
+ elizaLogger . debug ( "Initializing OpenAI model with Cloudflare check" ) ;
418
+ const baseURL = getCloudflareGatewayBaseURL ( runtime , 'openai' ) || endpoint ;
419
+
420
+ //elizaLogger.debug("OpenAI baseURL result:", { baseURL });
363
421
const openai = createOpenAI ( {
364
422
apiKey,
365
- baseURL : endpoint ,
423
+ baseURL,
366
424
fetch : runtime . fetch ,
367
425
} ) ;
368
426
@@ -430,10 +488,7 @@ export async function generateText({
430
488
const { text : openaiResponse } = await aiGenerateText ( {
431
489
model : openai . languageModel ( model ) ,
432
490
prompt : context ,
433
- system :
434
- runtime . character . system ??
435
- settings . SYSTEM_PROMPT ??
436
- undefined ,
491
+ system : runtime . character . system ?? settings . SYSTEM_PROMPT ?? undefined ,
437
492
temperature : temperature ,
438
493
maxTokens : max_response_length ,
439
494
frequencyPenalty : frequency_penalty ,
@@ -474,13 +529,11 @@ export async function generateText({
474
529
}
475
530
476
531
case ModelProviderName . ANTHROPIC : {
477
- elizaLogger . debug ( "Initializing Anthropic model." ) ;
478
-
479
- const anthropic = createAnthropic ( {
480
- apiKey,
481
- fetch : runtime . fetch ,
482
- } ) ;
532
+ elizaLogger . debug ( "Initializing Anthropic model with Cloudflare check" ) ;
533
+ const baseURL = getCloudflareGatewayBaseURL ( runtime , 'anthropic' ) || "https://api.anthropic.com/v1" ;
534
+ elizaLogger . debug ( "Anthropic baseURL result:" , { baseURL } ) ;
483
535
536
+ const anthropic = createAnthropic ( { apiKey, baseURL, fetch : runtime . fetch } ) ;
484
537
const { text : anthropicResponse } = await aiGenerateText ( {
485
538
model : anthropic . languageModel ( model ) ,
486
539
prompt : context ,
@@ -568,26 +621,30 @@ export async function generateText({
568
621
}
569
622
570
623
case ModelProviderName . GROQ : {
571
- const groq = createGroq ( { apiKey, fetch : runtime . fetch } ) ;
624
+ elizaLogger . debug ( "Initializing Groq model with Cloudflare check" ) ;
625
+ const baseURL = getCloudflareGatewayBaseURL ( runtime , 'groq' ) ;
626
+ elizaLogger . debug ( "Groq baseURL result:" , { baseURL } ) ;
627
+ const groq = createGroq ( { apiKey, fetch : runtime . fetch , baseURL } ) ;
572
628
573
629
const { text : groqResponse } = await aiGenerateText ( {
574
630
model : groq . languageModel ( model ) ,
575
631
prompt : context ,
576
- temperature : temperature ,
632
+ temperature,
577
633
system :
578
634
runtime . character . system ??
579
635
settings . SYSTEM_PROMPT ??
580
636
undefined ,
581
- tools : tools ,
637
+ tools,
582
638
onStepFinish : onStepFinish ,
583
- maxSteps : maxSteps ,
639
+ maxSteps,
584
640
maxTokens : max_response_length ,
585
641
frequencyPenalty : frequency_penalty ,
586
642
presencePenalty : presence_penalty ,
587
- experimental_telemetry : experimental_telemetry ,
643
+ experimental_telemetry,
588
644
} ) ;
589
645
590
646
response = groqResponse ;
647
+ elizaLogger . debug ( "Received response from Groq model." ) ;
591
648
break ;
592
649
}
593
650
@@ -1833,8 +1890,10 @@ async function handleOpenAI({
1833
1890
schemaDescription,
1834
1891
mode = "json" ,
1835
1892
modelOptions,
1893
+ provider,
1894
+ runtime,
1836
1895
} : ProviderOptions ) : Promise < GenerateObjectResult < unknown > > {
1837
- const baseURL = models . openai . endpoint || undefined ;
1896
+ const baseURL = getCloudflareGatewayBaseURL ( runtime , ' openai' ) || models . openai . endpoint ;
1838
1897
const openai = createOpenAI ( { apiKey, baseURL } ) ;
1839
1898
return await aiGenerateObject ( {
1840
1899
model : openai . languageModel ( model ) ,
@@ -1860,8 +1919,13 @@ async function handleAnthropic({
1860
1919
schemaDescription,
1861
1920
mode = "json" ,
1862
1921
modelOptions,
1922
+ runtime,
1863
1923
} : ProviderOptions ) : Promise < GenerateObjectResult < unknown > > {
1864
- const anthropic = createAnthropic ( { apiKey } ) ;
1924
+ elizaLogger . debug ( "Handling Anthropic request with Cloudflare check" ) ;
1925
+ const baseURL = getCloudflareGatewayBaseURL ( runtime , 'anthropic' ) ;
1926
+ elizaLogger . debug ( "Anthropic handleAnthropic baseURL:" , { baseURL } ) ;
1927
+
1928
+ const anthropic = createAnthropic ( { apiKey, baseURL } ) ;
1865
1929
return await aiGenerateObject ( {
1866
1930
model : anthropic . languageModel ( model ) ,
1867
1931
schema,
@@ -1912,8 +1976,13 @@ async function handleGroq({
1912
1976
schemaDescription,
1913
1977
mode = "json" ,
1914
1978
modelOptions,
1979
+ runtime,
1915
1980
} : ProviderOptions ) : Promise < GenerateObjectResult < unknown > > {
1916
- const groq = createGroq ( { apiKey } ) ;
1981
+ elizaLogger . debug ( "Handling Groq request with Cloudflare check" ) ;
1982
+ const baseURL = getCloudflareGatewayBaseURL ( runtime , 'groq' ) ;
1983
+ elizaLogger . debug ( "Groq handleGroq baseURL:" , { baseURL } ) ;
1984
+
1985
+ const groq = createGroq ( { apiKey, baseURL } ) ;
1917
1986
return await aiGenerateObject ( {
1918
1987
model : groq . languageModel ( model ) ,
1919
1988
schema,
0 commit comments