Skip to content

Commit

Permalink
fix: improve error handling and configuration checks in Farcaster client
Browse files Browse the repository at this point in the history
- Updated safeParseInt to use Number.isNaN for better clarity.
- Added nullish coalescing to handle potential undefined FARCASTER_FID.
- Implemented a check to skip interactions if no FID is found.
- Enhanced stopping mechanism for the Farcaster client to ensure it only attempts to stop if the client is initialized.
  • Loading branch information
sin-bufan committed Jan 13, 2025
1 parent b81b9bc commit 38095c2
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 5 deletions.
2 changes: 1 addition & 1 deletion packages/client-farcaster/src/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ function safeParseInt(
): number {
if (!value) return defaultValue;
const parsed = parseInt(value, 10);
return isNaN(parsed) ? defaultValue : Math.max(1, parsed);
return Number.isNaN(parsed) ? defaultValue : Math.max(1, parsed);
}

/**
Expand Down
6 changes: 4 additions & 2 deletions packages/client-farcaster/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,17 @@ export const FarcasterClientInterface: Client = {

// Start all services
await manager.start();

runtime.clients.farcaster = manager;
return manager;
},

async stop(runtime: IAgentRuntime) {
try {
// stop it
elizaLogger.log("Stopping farcaster client", runtime.agentId);
await runtime.clients.farcaster.stop();
if (runtime.clients.farcaster) {
await runtime.clients.farcaster.stop();
}
} catch (e) {
elizaLogger.error("client-farcaster interface stop error", e);
}
Expand Down
8 changes: 6 additions & 2 deletions packages/client-farcaster/src/interactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,11 @@ export class FarcasterInteractionManager {
}

private async handleInteractions() {
const agentFid = this.client.farcasterConfig.FARCASTER_FID;
const agentFid = this.client.farcasterConfig?.FARCASTER_FID ?? 0;
if (!agentFid) {
elizaLogger.info("No FID found, skipping interactions");
return;
}

const mentions = await this.client.getMentions({
fid: agentFid,
Expand Down Expand Up @@ -230,7 +234,7 @@ export class FarcasterInteractionManager {

if (!responseContent.text) return;

if (this.client.farcasterConfig.FARCASTER_DRY_RUN) {
if (this.client.farcasterConfig?.FARCASTER_DRY_RUN) {
elizaLogger.info(
`Dry run: would have responded to cast ${cast.hash} with ${responseContent.text}`
);
Expand Down

0 comments on commit 38095c2

Please sign in to comment.