Skip to content

Commit

Permalink
Fix: optional chaining on search to prevent error when search is unco…
Browse files Browse the repository at this point in the history
…mmented but not enabled in env file.
  • Loading branch information
netdragonx committed Dec 18, 2024
1 parent 81d0273 commit 1aee80d
Showing 1 changed file with 14 additions and 17 deletions.
31 changes: 14 additions & 17 deletions packages/client-twitter/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,39 @@
import { Client, elizaLogger, IAgentRuntime } from "@ai16z/eliza";
import { ClientBase } from "./base.ts";
import { validateTwitterConfig } from "./environment.ts";
import { TwitterInteractionClient } from "./interactions.ts";
import { TwitterPostClient } from "./post.ts";
import { TwitterSearchClient } from "./search.ts";
import { TwitterInteractionClient } from "./interactions.ts";
import { IAgentRuntime, Client, elizaLogger } from "@ai16z/eliza";
import { validateTwitterConfig } from "./environment.ts";
import { ClientBase } from "./base.ts";

class TwitterManager {
client: ClientBase;
post: TwitterPostClient;
search: TwitterSearchClient;
interaction: TwitterInteractionClient;
constructor(runtime: IAgentRuntime, enableSearch:boolean) {
constructor(runtime: IAgentRuntime, enableSearch: boolean) {
this.client = new ClientBase(runtime);
this.post = new TwitterPostClient(this.client, runtime);

if (enableSearch) {
// this searches topics from character file
elizaLogger.warn('Twitter/X client running in a mode that:')
elizaLogger.warn('1. violates consent of random users')
elizaLogger.warn('2. burns your rate limit')
elizaLogger.warn('3. can get your account banned')
elizaLogger.warn('use at your own risk')
this.search = new TwitterSearchClient(this.client, runtime); // don't start the search client by default
// this searches topics from character file
elizaLogger.warn("Twitter/X client running in a mode that:");
elizaLogger.warn("1. violates consent of random users");
elizaLogger.warn("2. burns your rate limit");
elizaLogger.warn("3. can get your account banned");
elizaLogger.warn("use at your own risk");
this.search = new TwitterSearchClient(this.client, runtime);
}

this.interaction = new TwitterInteractionClient(this.client, runtime);
}
}

export const TwitterClientInterface: Client = {

async start(runtime: IAgentRuntime) {
await validateTwitterConfig(runtime);

elizaLogger.log("Twitter client started");

// enableSearch is just set previous to this call
// so enableSearch can change over time
// and changing it won't stop the SearchClient in the existing instance
const manager = new TwitterManager(runtime, this.enableSearch);

await manager.client.init();
Expand All @@ -45,7 +42,7 @@ export const TwitterClientInterface: Client = {

await manager.interaction.start();

//await manager.search.start(); // don't run the search by default
await manager.search?.start();

return manager;
},
Expand Down

0 comments on commit 1aee80d

Please sign in to comment.