@@ -52,7 +52,7 @@ import {
52
52
import { fal } from "@fal-ai/client" ;
53
53
54
54
import BigNumber from "bignumber.js" ;
55
- import { createPublicClient , http } from "viem" ;
55
+ import { createPublicClient , http } from "viem" ;
56
56
import { AtomaSDK } from "atoma-sdk" ;
57
57
58
58
type Tool = CoreTool < any , any > ;
@@ -170,59 +170,38 @@ async function truncateTiktoken(
170
170
* Get OnChain EternalAI System Prompt
171
171
* @returns System Prompt
172
172
*/
173
- async function getOnChainEternalAISystemPrompt (
174
- runtime : IAgentRuntime
175
- ) : Promise < string > | undefined {
176
- const agentId = runtime . getSetting ( "ETERNALAI_AGENT_ID" ) ;
173
+ async function getOnChainEternalAISystemPrompt ( runtime : IAgentRuntime ) : Promise < string > | undefined {
174
+ const agentId = runtime . getSetting ( "ETERNALAI_AGENT_ID" )
177
175
const providerUrl = runtime . getSetting ( "ETERNALAI_RPC_URL" ) ;
178
- const contractAddress = runtime . getSetting (
179
- "ETERNALAI_AGENT_CONTRACT_ADDRESS"
180
- ) ;
176
+ const contractAddress = runtime . getSetting ( "ETERNALAI_AGENT_CONTRACT_ADDRESS" ) ;
181
177
if ( agentId && providerUrl && contractAddress ) {
182
178
// get on-chain system-prompt
183
- const contractABI = [
184
- {
185
- inputs : [
186
- {
187
- internalType : "uint256" ,
188
- name : "_agentId" ,
189
- type : "uint256" ,
190
- } ,
191
- ] ,
192
- name : "getAgentSystemPrompt" ,
193
- outputs : [
194
- { internalType : "bytes[]" , name : "" , type : "bytes[]" } ,
195
- ] ,
196
- stateMutability : "view" ,
197
- type : "function" ,
198
- } ,
199
- ] ;
179
+ const contractABI = [ { "inputs" : [ { "internalType" : "uint256" , "name" : "_agentId" , "type" : "uint256" } ] , "name" : "getAgentSystemPrompt" , "outputs" : [ { "internalType" : "bytes[]" , "name" : "" , "type" : "bytes[]" } ] , "stateMutability" : "view" , "type" : "function" } ] ;
200
180
201
181
const publicClient = createPublicClient ( {
202
182
transport : http ( providerUrl ) ,
203
183
} ) ;
204
184
205
185
try {
206
- const validAddress : `0x${string } ` =
207
- contractAddress as `0x${string } `;
186
+ const validAddress : `0x${string } ` = contractAddress as `0x${string } `;
208
187
const result = await publicClient . readContract ( {
209
188
address : validAddress ,
210
189
abi : contractABI ,
211
190
functionName : "getAgentSystemPrompt" ,
212
191
args : [ new BigNumber ( agentId ) ] ,
213
192
} ) ;
214
193
if ( result ) {
215
- elizaLogger . info ( " on-chain system-prompt response" , result [ 0 ] ) ;
194
+ elizaLogger . info ( ' on-chain system-prompt response' , result [ 0 ] ) ;
216
195
const value = result [ 0 ] . toString ( ) . replace ( "0x" , "" ) ;
217
- const content = Buffer . from ( value , " hex" ) . toString ( " utf-8" ) ;
218
- elizaLogger . info ( " on-chain system-prompt" , content ) ;
219
- return await fetchEternalAISystemPrompt ( runtime , content ) ;
196
+ let content = Buffer . from ( value , ' hex' ) . toString ( ' utf-8' ) ;
197
+ elizaLogger . info ( ' on-chain system-prompt' , content ) ;
198
+ return await fetchEternalAISystemPrompt ( runtime , content )
220
199
} else {
221
200
return undefined ;
222
201
}
223
202
} catch ( error ) {
224
203
elizaLogger . error ( error ) ;
225
- elizaLogger . error ( " err" , error ) ;
204
+ elizaLogger . error ( ' err' , error ) ;
226
205
}
227
206
}
228
207
return undefined ;
@@ -232,42 +211,34 @@ async function getOnChainEternalAISystemPrompt(
232
211
* Fetch EternalAI System Prompt
233
212
* @returns System Prompt
234
213
*/
235
- async function fetchEternalAISystemPrompt (
236
- runtime : IAgentRuntime ,
237
- content : string
238
- ) : Promise < string > | undefined {
239
- const IPFS = "ipfs://" ;
214
+ async function fetchEternalAISystemPrompt ( runtime : IAgentRuntime , content : string ) : Promise < string > | undefined {
215
+ const IPFS = "ipfs://"
240
216
const containsSubstring : boolean = content . includes ( IPFS ) ;
241
217
if ( containsSubstring ) {
242
- const lightHouse = content . replace (
243
- IPFS ,
244
- "https://gateway.lighthouse.storage/ipfs/"
245
- ) ;
246
- elizaLogger . info ( "fetch lightHouse" , lightHouse ) ;
218
+
219
+ const lightHouse = content . replace ( IPFS , "https://gateway.lighthouse.storage/ipfs/" ) ;
220
+ elizaLogger . info ( "fetch lightHouse" , lightHouse )
247
221
const responseLH = await fetch ( lightHouse , {
248
222
method : "GET" ,
249
223
} ) ;
250
- elizaLogger . info ( "fetch lightHouse resp" , responseLH ) ;
224
+ elizaLogger . info ( "fetch lightHouse resp" , responseLH )
251
225
if ( responseLH . ok ) {
252
226
const data = await responseLH . text ( ) ;
253
227
return data ;
254
228
} else {
255
- const gcs = content . replace (
256
- IPFS ,
257
- "https://cdn.eternalai.org/upload/"
258
- ) ;
259
- elizaLogger . info ( "fetch gcs" , gcs ) ;
229
+ const gcs = content . replace ( IPFS , "https://cdn.eternalai.org/upload/" )
230
+ elizaLogger . info ( "fetch gcs" , gcs )
260
231
const responseGCS = await fetch ( gcs , {
261
232
method : "GET" ,
262
233
} ) ;
263
- elizaLogger . info ( "fetch lightHouse gcs" , responseGCS ) ;
234
+ elizaLogger . info ( "fetch lightHouse gcs" , responseGCS )
264
235
if ( responseGCS . ok ) {
265
236
const data = await responseGCS . text ( ) ;
266
237
return data ;
267
238
} else {
268
- throw new Error ( "invalid on-chain system prompt" ) ;
239
+ throw new Error ( "invalid on-chain system prompt" )
269
240
}
270
- return undefined ;
241
+ return undefined
271
242
}
272
243
} else {
273
244
return content ;
@@ -280,20 +251,16 @@ async function fetchEternalAISystemPrompt(
280
251
* @param provider The model provider name
281
252
* @returns The Cloudflare Gateway base URL if enabled, undefined otherwise
282
253
*/
283
- function getCloudflareGatewayBaseURL (
284
- runtime : IAgentRuntime ,
285
- provider : string
286
- ) : string | undefined {
287
- const isCloudflareEnabled =
288
- runtime . getSetting ( "CLOUDFLARE_GW_ENABLED" ) === "true" ;
254
+ function getCloudflareGatewayBaseURL ( runtime : IAgentRuntime , provider : string ) : string | undefined {
255
+ const isCloudflareEnabled = runtime . getSetting ( "CLOUDFLARE_GW_ENABLED" ) === "true" ;
289
256
const cloudflareAccountId = runtime . getSetting ( "CLOUDFLARE_AI_ACCOUNT_ID" ) ;
290
257
const cloudflareGatewayId = runtime . getSetting ( "CLOUDFLARE_AI_GATEWAY_ID" ) ;
291
258
292
259
elizaLogger . debug ( "Cloudflare Gateway Configuration:" , {
293
260
isEnabled : isCloudflareEnabled ,
294
261
hasAccountId : ! ! cloudflareAccountId ,
295
262
hasGatewayId : ! ! cloudflareGatewayId ,
296
- provider : provider ,
263
+ provider : provider
297
264
} ) ;
298
265
299
266
if ( ! isCloudflareEnabled ) {
@@ -302,16 +269,12 @@ function getCloudflareGatewayBaseURL(
302
269
}
303
270
304
271
if ( ! cloudflareAccountId ) {
305
- elizaLogger . warn (
306
- "Cloudflare Gateway is enabled but CLOUDFLARE_AI_ACCOUNT_ID is not set"
307
- ) ;
272
+ elizaLogger . warn ( "Cloudflare Gateway is enabled but CLOUDFLARE_AI_ACCOUNT_ID is not set" ) ;
308
273
return undefined ;
309
274
}
310
275
311
276
if ( ! cloudflareGatewayId ) {
312
- elizaLogger . warn (
313
- "Cloudflare Gateway is enabled but CLOUDFLARE_AI_GATEWAY_ID is not set"
314
- ) ;
277
+ elizaLogger . warn ( "Cloudflare Gateway is enabled but CLOUDFLARE_AI_GATEWAY_ID is not set" ) ;
315
278
return undefined ;
316
279
}
317
280
@@ -320,7 +283,7 @@ function getCloudflareGatewayBaseURL(
320
283
provider,
321
284
baseURL,
322
285
accountId : cloudflareAccountId ,
323
- gatewayId : cloudflareGatewayId ,
286
+ gatewayId : cloudflareGatewayId
324
287
} ) ;
325
288
326
289
return baseURL ;
@@ -410,13 +373,9 @@ export async function generateText({
410
373
hasRuntime : ! ! runtime ,
411
374
runtimeSettings : {
412
375
CLOUDFLARE_GW_ENABLED : runtime . getSetting ( "CLOUDFLARE_GW_ENABLED" ) ,
413
- CLOUDFLARE_AI_ACCOUNT_ID : runtime . getSetting (
414
- "CLOUDFLARE_AI_ACCOUNT_ID"
415
- ) ,
416
- CLOUDFLARE_AI_GATEWAY_ID : runtime . getSetting (
417
- "CLOUDFLARE_AI_GATEWAY_ID"
418
- ) ,
419
- } ,
376
+ CLOUDFLARE_AI_ACCOUNT_ID : runtime . getSetting ( "CLOUDFLARE_AI_ACCOUNT_ID" ) ,
377
+ CLOUDFLARE_AI_GATEWAY_ID : runtime . getSetting ( "CLOUDFLARE_AI_GATEWAY_ID" )
378
+ }
420
379
} ) ;
421
380
422
381
const endpoint =
@@ -536,11 +495,8 @@ export async function generateText({
536
495
case ModelProviderName . TOGETHER :
537
496
case ModelProviderName . NINETEEN_AI :
538
497
case ModelProviderName . AKASH_CHAT_API : {
539
- elizaLogger . debug (
540
- "Initializing OpenAI model with Cloudflare check"
541
- ) ;
542
- const baseURL =
543
- getCloudflareGatewayBaseURL ( runtime , "openai" ) || endpoint ;
498
+ elizaLogger . debug ( "Initializing OpenAI model with Cloudflare check" ) ;
499
+ const baseURL = getCloudflareGatewayBaseURL ( runtime , 'openai' ) || endpoint ;
544
500
545
501
//elizaLogger.debug("OpenAI baseURL result:", { baseURL });
546
502
const openai = createOpenAI ( {
@@ -610,26 +566,17 @@ export async function generateText({
610
566
} ,
611
567
} ) ;
612
568
613
- let system_prompt =
614
- runtime . character . system ??
615
- settings . SYSTEM_PROMPT ??
616
- undefined ;
569
+ let system_prompt = runtime . character . system ?? settings . SYSTEM_PROMPT ?? undefined ;
617
570
try {
618
- const on_chain_system_prompt =
619
- await getOnChainEternalAISystemPrompt ( runtime ) ;
571
+ const on_chain_system_prompt = await getOnChainEternalAISystemPrompt ( runtime ) ;
620
572
if ( ! on_chain_system_prompt ) {
621
- elizaLogger . error (
622
- new Error ( "invalid on_chain_system_prompt" )
623
- ) ;
573
+ elizaLogger . error ( new Error ( "invalid on_chain_system_prompt" ) )
624
574
} else {
625
- system_prompt = on_chain_system_prompt ;
626
- elizaLogger . info (
627
- "new on-chain system prompt" ,
628
- system_prompt
629
- ) ;
575
+ system_prompt = on_chain_system_prompt
576
+ elizaLogger . info ( "new on-chain system prompt" , system_prompt )
630
577
}
631
578
} catch ( e ) {
632
- elizaLogger . error ( e ) ;
579
+ elizaLogger . error ( e )
633
580
}
634
581
635
582
const { text : openaiResponse } = await aiGenerateText ( {
@@ -697,19 +644,11 @@ export async function generateText({
697
644
}
698
645
699
646
case ModelProviderName . ANTHROPIC : {
700
- elizaLogger . debug (
701
- "Initializing Anthropic model with Cloudflare check"
702
- ) ;
703
- const baseURL =
704
- getCloudflareGatewayBaseURL ( runtime , "anthropic" ) ||
705
- "https://api.anthropic.com/v1" ;
647
+ elizaLogger . debug ( "Initializing Anthropic model with Cloudflare check" ) ;
648
+ const baseURL = getCloudflareGatewayBaseURL ( runtime , 'anthropic' ) || "https://api.anthropic.com/v1" ;
706
649
elizaLogger . debug ( "Anthropic baseURL result:" , { baseURL } ) ;
707
650
708
- const anthropic = createAnthropic ( {
709
- apiKey,
710
- baseURL,
711
- fetch : runtime . fetch ,
712
- } ) ;
651
+ const anthropic = createAnthropic ( { apiKey, baseURL, fetch : runtime . fetch } ) ;
713
652
const { text : anthropicResponse } = await aiGenerateText ( {
714
653
model : anthropic . languageModel ( model ) ,
715
654
prompt : context ,
@@ -797,16 +736,10 @@ export async function generateText({
797
736
}
798
737
799
738
case ModelProviderName . GROQ : {
800
- elizaLogger . debug (
801
- "Initializing Groq model with Cloudflare check"
802
- ) ;
803
- const baseURL = getCloudflareGatewayBaseURL ( runtime , "groq" ) ;
739
+ elizaLogger . debug ( "Initializing Groq model with Cloudflare check" ) ;
740
+ const baseURL = getCloudflareGatewayBaseURL ( runtime , 'groq' ) ;
804
741
elizaLogger . debug ( "Groq baseURL result:" , { baseURL } ) ;
805
- const groq = createGroq ( {
806
- apiKey,
807
- fetch : runtime . fetch ,
808
- baseURL,
809
- } ) ;
742
+ const groq = createGroq ( { apiKey, fetch : runtime . fetch , baseURL } ) ;
810
743
811
744
const { text : groqResponse } = await aiGenerateText ( {
812
745
model : groq . languageModel ( model ) ,
@@ -2118,9 +2051,7 @@ async function handleOpenAI({
2118
2051
provider : _provider ,
2119
2052
runtime,
2120
2053
} : ProviderOptions ) : Promise < GenerateObjectResult < unknown > > {
2121
- const baseURL =
2122
- getCloudflareGatewayBaseURL ( runtime , "openai" ) ||
2123
- models . openai . endpoint ;
2054
+ const baseURL = getCloudflareGatewayBaseURL ( runtime , 'openai' ) || models . openai . endpoint ;
2124
2055
const openai = createOpenAI ( { apiKey, baseURL } ) ;
2125
2056
return await aiGenerateObject ( {
2126
2057
model : openai . languageModel ( model ) ,
@@ -2149,7 +2080,7 @@ async function handleAnthropic({
2149
2080
runtime,
2150
2081
} : ProviderOptions ) : Promise < GenerateObjectResult < unknown > > {
2151
2082
elizaLogger . debug ( "Handling Anthropic request with Cloudflare check" ) ;
2152
- const baseURL = getCloudflareGatewayBaseURL ( runtime , " anthropic" ) ;
2083
+ const baseURL = getCloudflareGatewayBaseURL ( runtime , ' anthropic' ) ;
2153
2084
elizaLogger . debug ( "Anthropic handleAnthropic baseURL:" , { baseURL } ) ;
2154
2085
2155
2086
const anthropic = createAnthropic ( { apiKey, baseURL } ) ;
@@ -2206,7 +2137,7 @@ async function handleGroq({
2206
2137
runtime,
2207
2138
} : ProviderOptions ) : Promise < GenerateObjectResult < unknown > > {
2208
2139
elizaLogger . debug ( "Handling Groq request with Cloudflare check" ) ;
2209
- const baseURL = getCloudflareGatewayBaseURL ( runtime , " groq" ) ;
2140
+ const baseURL = getCloudflareGatewayBaseURL ( runtime , ' groq' ) ;
2210
2141
elizaLogger . debug ( "Groq handleGroq baseURL:" , { baseURL } ) ;
2211
2142
2212
2143
const groq = createGroq ( { apiKey, baseURL } ) ;
@@ -2557,4 +2488,4 @@ export async function generateTweetActions({
2557
2488
await new Promise ( ( resolve ) => setTimeout ( resolve , retryDelay ) ) ;
2558
2489
retryDelay *= 2 ;
2559
2490
}
2560
- }
2491
+ }
0 commit comments