Skip to content

Commit

Permalink
Fix search for TwitterSearchClient
Browse files Browse the repository at this point in the history
  • Loading branch information
dontAskVI committed Dec 11, 2024
1 parent d7e8d9a commit 649946c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 17 deletions.
4 changes: 3 additions & 1 deletion packages/client-twitter/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class TwitterManager {
constructor(runtime: IAgentRuntime) {
this.client = new ClientBase(runtime);
this.post = new TwitterPostClient(this.client, runtime);
// this.search = new TwitterSearchClient(runtime); // don't start the search client by default
this.search = new TwitterSearchClient(this.client, runtime); // don't start the search client by default
// this searches topics from character file, but kind of violates consent of random users
// burns your rate limit and can get your account banned
// use at your own risk
Expand All @@ -35,6 +35,8 @@ export const TwitterClientInterface: Client = {

await manager.interaction.start();

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

return manager;
},
async stop(_runtime: IAgentRuntime) {
Expand Down
32 changes: 16 additions & 16 deletions packages/client-twitter/src/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,17 @@ Your response should not contain any questions. Brief, concise statements only.
` + messageCompletionFooter;

export class TwitterSearchClient extends ClientBase {
export class TwitterSearchClient {
client: ClientBase;
runtime: IAgentRuntime;
private respondedTweets: Set<string> = new Set();

constructor(runtime: IAgentRuntime) {
// Initialize the client and pass an optional callback to be called when the client is ready
super({
runtime,
});
constructor(client: ClientBase, runtime: IAgentRuntime) {
this.client = client;
this.runtime = runtime;
}

async onReady() {
async start() {
this.engageWithSearchTermsLoop();
}

Expand All @@ -74,16 +74,16 @@ export class TwitterSearchClient extends ClientBase {
console.log("Fetching search tweets");
// TODO: we wait 5 seconds here to avoid getting rate limited on startup, but we should queue
await new Promise((resolve) => setTimeout(resolve, 5000));
const recentTweets = await this.fetchSearchTweets(
const recentTweets = await this.client.fetchSearchTweets(
searchTerm,
20,
SearchMode.Top
);
console.log("Search tweets fetched");

const homeTimeline = await this.fetchHomeTimeline(50);
const homeTimeline = await this.client.fetchHomeTimeline(50);

await this.cacheTimeline(homeTimeline);
await this.client.cacheTimeline(homeTimeline);

const formattedHomeTimeline =
`# ${this.runtime.character.name}'s Home Timeline\n\n` +
Expand Down Expand Up @@ -179,7 +179,7 @@ export class TwitterSearchClient extends ClientBase {
);

// crawl additional conversation tweets, if there are any
await buildConversationThread(selectedTweet, this);
await buildConversationThread(selectedTweet, this.client);

const message = {
id: stringToUuid(selectedTweet.id + "-" + this.runtime.agentId),
Expand Down Expand Up @@ -218,8 +218,8 @@ export class TwitterSearchClient extends ClientBase {

let tweetBackground = "";
if (selectedTweet.isRetweet) {
const originalTweet = await this.requestQueue.add(() =>
this.twitterClient.getTweet(selectedTweet.id)
const originalTweet = await this.client.requestQueue.add(() =>
this.client.twitterClient.getTweet(selectedTweet.id)
);
tweetBackground = `Retweeting @${originalTweet.username}: ${originalTweet.text}`;
}
Expand All @@ -237,7 +237,7 @@ export class TwitterSearchClient extends ClientBase {
}

let state = await this.runtime.composeState(message, {
twitterClient: this.twitterClient,
twitterClient: this.client.twitterClient,
twitterUserName: this.runtime.getSetting("TWITTER_USERNAME"),
timeline: formattedHomeTimeline,
tweetContext: `${tweetBackground}
Expand All @@ -250,7 +250,7 @@ export class TwitterSearchClient extends ClientBase {
`,
});

await this.saveRequestMessage(message, state as State);
await this.client.saveRequestMessage(message, state as State);

const context = composeContext({
state,
Expand Down Expand Up @@ -280,7 +280,7 @@ export class TwitterSearchClient extends ClientBase {
try {
const callback: HandlerCallback = async (response: Content) => {
const memories = await sendTweet(
this,
this.client,
response,
message.roomId,
this.runtime.getSetting("TWITTER_USERNAME"),
Expand Down

0 comments on commit 649946c

Please sign in to comment.