Skip to content

Commit be5aa19

Browse files
committed
refactor: improve code formatting and readability in generation.ts
- Standardized import statements and removed unnecessary line breaks. - Simplified function definitions and improved inline comments for clarity. - Enhanced logging messages for better debugging and error tracking. - Ensured consistent use of single quotes for strings throughout the file.
1 parent af9acd8 commit be5aa19

File tree

1 file changed

+49
-118
lines changed

1 file changed

+49
-118
lines changed

packages/core/src/generation.ts

+49-118
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ import {
5252
import { fal } from "@fal-ai/client";
5353

5454
import BigNumber from "bignumber.js";
55-
import { createPublicClient, http } from "viem";
55+
import {createPublicClient, http} from "viem";
5656
import { AtomaSDK } from "atoma-sdk";
5757

5858
type Tool = CoreTool<any, any>;
@@ -170,59 +170,38 @@ async function truncateTiktoken(
170170
* Get OnChain EternalAI System Prompt
171171
* @returns System Prompt
172172
*/
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")
177175
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");
181177
if (agentId && providerUrl && contractAddress) {
182178
// 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"}];
200180

201181
const publicClient = createPublicClient({
202182
transport: http(providerUrl),
203183
});
204184

205185
try {
206-
const validAddress: `0x${string}` =
207-
contractAddress as `0x${string}`;
186+
const validAddress: `0x${string}` = contractAddress as `0x${string}`;
208187
const result = await publicClient.readContract({
209188
address: validAddress,
210189
abi: contractABI,
211190
functionName: "getAgentSystemPrompt",
212191
args: [new BigNumber(agentId)],
213192
});
214193
if (result) {
215-
elizaLogger.info("on-chain system-prompt response", result[0]);
194+
elizaLogger.info('on-chain system-prompt response', result[0]);
216195
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)
220199
} else {
221200
return undefined;
222201
}
223202
} catch (error) {
224203
elizaLogger.error(error);
225-
elizaLogger.error("err", error);
204+
elizaLogger.error('err', error);
226205
}
227206
}
228207
return undefined;
@@ -232,42 +211,34 @@ async function getOnChainEternalAISystemPrompt(
232211
* Fetch EternalAI System Prompt
233212
* @returns System Prompt
234213
*/
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://"
240216
const containsSubstring: boolean = content.includes(IPFS);
241217
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)
247221
const responseLH = await fetch(lightHouse, {
248222
method: "GET",
249223
});
250-
elizaLogger.info("fetch lightHouse resp", responseLH);
224+
elizaLogger.info("fetch lightHouse resp", responseLH)
251225
if (responseLH.ok) {
252226
const data = await responseLH.text();
253227
return data;
254228
} 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)
260231
const responseGCS = await fetch(gcs, {
261232
method: "GET",
262233
});
263-
elizaLogger.info("fetch lightHouse gcs", responseGCS);
234+
elizaLogger.info("fetch lightHouse gcs", responseGCS)
264235
if (responseGCS.ok) {
265236
const data = await responseGCS.text();
266237
return data;
267238
} else {
268-
throw new Error("invalid on-chain system prompt");
239+
throw new Error("invalid on-chain system prompt")
269240
}
270-
return undefined;
241+
return undefined
271242
}
272243
} else {
273244
return content;
@@ -280,20 +251,16 @@ async function fetchEternalAISystemPrompt(
280251
* @param provider The model provider name
281252
* @returns The Cloudflare Gateway base URL if enabled, undefined otherwise
282253
*/
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";
289256
const cloudflareAccountId = runtime.getSetting("CLOUDFLARE_AI_ACCOUNT_ID");
290257
const cloudflareGatewayId = runtime.getSetting("CLOUDFLARE_AI_GATEWAY_ID");
291258

292259
elizaLogger.debug("Cloudflare Gateway Configuration:", {
293260
isEnabled: isCloudflareEnabled,
294261
hasAccountId: !!cloudflareAccountId,
295262
hasGatewayId: !!cloudflareGatewayId,
296-
provider: provider,
263+
provider: provider
297264
});
298265

299266
if (!isCloudflareEnabled) {
@@ -302,16 +269,12 @@ function getCloudflareGatewayBaseURL(
302269
}
303270

304271
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");
308273
return undefined;
309274
}
310275

311276
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");
315278
return undefined;
316279
}
317280

@@ -320,7 +283,7 @@ function getCloudflareGatewayBaseURL(
320283
provider,
321284
baseURL,
322285
accountId: cloudflareAccountId,
323-
gatewayId: cloudflareGatewayId,
286+
gatewayId: cloudflareGatewayId
324287
});
325288

326289
return baseURL;
@@ -410,13 +373,9 @@ export async function generateText({
410373
hasRuntime: !!runtime,
411374
runtimeSettings: {
412375
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+
}
420379
});
421380

422381
const endpoint =
@@ -536,11 +495,8 @@ export async function generateText({
536495
case ModelProviderName.TOGETHER:
537496
case ModelProviderName.NINETEEN_AI:
538497
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;
544500

545501
//elizaLogger.debug("OpenAI baseURL result:", { baseURL });
546502
const openai = createOpenAI({
@@ -610,26 +566,17 @@ export async function generateText({
610566
},
611567
});
612568

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;
617570
try {
618-
const on_chain_system_prompt =
619-
await getOnChainEternalAISystemPrompt(runtime);
571+
const on_chain_system_prompt = await getOnChainEternalAISystemPrompt(runtime);
620572
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"))
624574
} 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)
630577
}
631578
} catch (e) {
632-
elizaLogger.error(e);
579+
elizaLogger.error(e)
633580
}
634581

635582
const { text: openaiResponse } = await aiGenerateText({
@@ -697,19 +644,11 @@ export async function generateText({
697644
}
698645

699646
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";
706649
elizaLogger.debug("Anthropic baseURL result:", { baseURL });
707650

708-
const anthropic = createAnthropic({
709-
apiKey,
710-
baseURL,
711-
fetch: runtime.fetch,
712-
});
651+
const anthropic = createAnthropic({ apiKey, baseURL, fetch: runtime.fetch });
713652
const { text: anthropicResponse } = await aiGenerateText({
714653
model: anthropic.languageModel(model),
715654
prompt: context,
@@ -797,16 +736,10 @@ export async function generateText({
797736
}
798737

799738
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');
804741
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 });
810743

811744
const { text: groqResponse } = await aiGenerateText({
812745
model: groq.languageModel(model),
@@ -2118,9 +2051,7 @@ async function handleOpenAI({
21182051
provider: _provider,
21192052
runtime,
21202053
}: ProviderOptions): Promise<GenerateObjectResult<unknown>> {
2121-
const baseURL =
2122-
getCloudflareGatewayBaseURL(runtime, "openai") ||
2123-
models.openai.endpoint;
2054+
const baseURL = getCloudflareGatewayBaseURL(runtime, 'openai') || models.openai.endpoint;
21242055
const openai = createOpenAI({ apiKey, baseURL });
21252056
return await aiGenerateObject({
21262057
model: openai.languageModel(model),
@@ -2149,7 +2080,7 @@ async function handleAnthropic({
21492080
runtime,
21502081
}: ProviderOptions): Promise<GenerateObjectResult<unknown>> {
21512082
elizaLogger.debug("Handling Anthropic request with Cloudflare check");
2152-
const baseURL = getCloudflareGatewayBaseURL(runtime, "anthropic");
2083+
const baseURL = getCloudflareGatewayBaseURL(runtime, 'anthropic');
21532084
elizaLogger.debug("Anthropic handleAnthropic baseURL:", { baseURL });
21542085

21552086
const anthropic = createAnthropic({ apiKey, baseURL });
@@ -2206,7 +2137,7 @@ async function handleGroq({
22062137
runtime,
22072138
}: ProviderOptions): Promise<GenerateObjectResult<unknown>> {
22082139
elizaLogger.debug("Handling Groq request with Cloudflare check");
2209-
const baseURL = getCloudflareGatewayBaseURL(runtime, "groq");
2140+
const baseURL = getCloudflareGatewayBaseURL(runtime, 'groq');
22102141
elizaLogger.debug("Groq handleGroq baseURL:", { baseURL });
22112142

22122143
const groq = createGroq({ apiKey, baseURL });
@@ -2557,4 +2488,4 @@ export async function generateTweetActions({
25572488
await new Promise((resolve) => setTimeout(resolve, retryDelay));
25582489
retryDelay *= 2;
25592490
}
2560-
}
2491+
}

0 commit comments

Comments
 (0)