Skip to content

Commit

Permalink
sort timelines based on the llm score
Browse files Browse the repository at this point in the history
  • Loading branch information
tcm390 committed Jan 5, 2025
1 parent c50549e commit 63e69c6
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 10 deletions.
3 changes: 1 addition & 2 deletions packages/client-twitter/src/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -351,8 +351,7 @@ export class ClientBase extends EventEmitter {
(media) => media.type === "video"
) || [],
}))
.filter((tweet) => tweet.username !== agentUsername) // do not perform action on self-tweets
.sort(() => Math.random() - 0.5);
.filter((tweet) => tweet.username !== agentUsername); // do not perform action on self-tweets
}

async fetchSearchTweets(
Expand Down
54 changes: 46 additions & 8 deletions packages/client-twitter/src/post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -627,14 +627,11 @@ export class TwitterPostClient {

const homeTimeline = await this.client.fetchTimelineForActions();
const results = [];
let processedTweets = 0;
const maxActionsProcessing =
this.client.twitterConfig.MAX_ACTIONS_PROCESSING;
const processedTimelines = [];

for (const tweet of homeTimeline) {
if (processedTweets >= maxActionsProcessing) {
break;
}
try {
// Skip if we've already processed this tweet
const memory =
Expand Down Expand Up @@ -685,9 +682,52 @@ export class TwitterPostClient {
);
continue;
}
processedTimelines.push({
tweet: tweet,
actionResponse: actionResponse,
tweetState: tweetState,
roomId: roomId,
});
} catch (error) {
elizaLogger.error(
`Error processing tweet ${tweet.id}:`,
error
);
continue;
}
}

const sortProcessedTimeline = (arr: typeof processedTimelines) => {
return arr.sort((a, b) => {
// Count the number of true values in the actionResponse object
const countTrue = (obj: typeof a.actionResponse) =>
Object.values(obj).filter(Boolean).length;

const countA = countTrue(a.actionResponse);
const countB = countTrue(b.actionResponse);

// Primary sort by number of true values
if (countA !== countB) {
return countB - countA;
}

const executedActions: string[] = [];
// Secondary sort by the "like" property
if (a.actionResponse.like !== b.actionResponse.like) {
return a.actionResponse.like ? -1 : 1;
}

// Tertiary sort keeps the remaining objects with equal weight
return 0;
});
};
const sortedTimelines = sortProcessedTimeline(
processedTimelines
).slice(0, maxActionsProcessing);

for (const timeline of sortedTimelines) {
const { actionResponse, tweetState, roomId, tweet } = timeline;
try {
const executedActions: string[] = [];
// Execute actions
if (actionResponse.like) {
try {
Expand Down Expand Up @@ -923,10 +963,9 @@ export class TwitterPostClient {

results.push({
tweetId: tweet.id,
parsedActions: actionResponse,
actionResponse: actionResponse,
executedActions,
});
processedTweets++;
} catch (error) {
elizaLogger.error(
`Error processing tweet ${tweet.id}:`,
Expand All @@ -935,7 +974,6 @@ export class TwitterPostClient {
continue;
}
}

return results; // Return results array to indicate completion
} catch (error) {
elizaLogger.error("Error in processTweetActions:", error);
Expand Down

0 comments on commit 63e69c6

Please sign in to comment.