Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: optional chaining on search to avoid startup errors when search is not enabled #1202

Merged
merged 2 commits into from
Dec 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading