Skip to content

Commit 63e69c6

Browse files
committed
sort timelines based on the llm score
1 parent c50549e commit 63e69c6

File tree

2 files changed

+47
-10
lines changed

2 files changed

+47
-10
lines changed

packages/client-twitter/src/base.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -351,8 +351,7 @@ export class ClientBase extends EventEmitter {
351351
(media) => media.type === "video"
352352
) || [],
353353
}))
354-
.filter((tweet) => tweet.username !== agentUsername) // do not perform action on self-tweets
355-
.sort(() => Math.random() - 0.5);
354+
.filter((tweet) => tweet.username !== agentUsername); // do not perform action on self-tweets
356355
}
357356

358357
async fetchSearchTweets(

packages/client-twitter/src/post.ts

+46-8
Original file line numberDiff line numberDiff line change
@@ -627,14 +627,11 @@ export class TwitterPostClient {
627627

628628
const homeTimeline = await this.client.fetchTimelineForActions();
629629
const results = [];
630-
let processedTweets = 0;
631630
const maxActionsProcessing =
632631
this.client.twitterConfig.MAX_ACTIONS_PROCESSING;
632+
const processedTimelines = [];
633633

634634
for (const tweet of homeTimeline) {
635-
if (processedTweets >= maxActionsProcessing) {
636-
break;
637-
}
638635
try {
639636
// Skip if we've already processed this tweet
640637
const memory =
@@ -685,9 +682,52 @@ export class TwitterPostClient {
685682
);
686683
continue;
687684
}
685+
processedTimelines.push({
686+
tweet: tweet,
687+
actionResponse: actionResponse,
688+
tweetState: tweetState,
689+
roomId: roomId,
690+
});
691+
} catch (error) {
692+
elizaLogger.error(
693+
`Error processing tweet ${tweet.id}:`,
694+
error
695+
);
696+
continue;
697+
}
698+
}
699+
700+
const sortProcessedTimeline = (arr: typeof processedTimelines) => {
701+
return arr.sort((a, b) => {
702+
// Count the number of true values in the actionResponse object
703+
const countTrue = (obj: typeof a.actionResponse) =>
704+
Object.values(obj).filter(Boolean).length;
705+
706+
const countA = countTrue(a.actionResponse);
707+
const countB = countTrue(b.actionResponse);
708+
709+
// Primary sort by number of true values
710+
if (countA !== countB) {
711+
return countB - countA;
712+
}
688713

689-
const executedActions: string[] = [];
714+
// Secondary sort by the "like" property
715+
if (a.actionResponse.like !== b.actionResponse.like) {
716+
return a.actionResponse.like ? -1 : 1;
717+
}
718+
719+
// Tertiary sort keeps the remaining objects with equal weight
720+
return 0;
721+
});
722+
};
723+
const sortedTimelines = sortProcessedTimeline(
724+
processedTimelines
725+
).slice(0, maxActionsProcessing);
690726

727+
for (const timeline of sortedTimelines) {
728+
const { actionResponse, tweetState, roomId, tweet } = timeline;
729+
try {
730+
const executedActions: string[] = [];
691731
// Execute actions
692732
if (actionResponse.like) {
693733
try {
@@ -923,10 +963,9 @@ export class TwitterPostClient {
923963

924964
results.push({
925965
tweetId: tweet.id,
926-
parsedActions: actionResponse,
966+
actionResponse: actionResponse,
927967
executedActions,
928968
});
929-
processedTweets++;
930969
} catch (error) {
931970
elizaLogger.error(
932971
`Error processing tweet ${tweet.id}:`,
@@ -935,7 +974,6 @@ export class TwitterPostClient {
935974
continue;
936975
}
937976
}
938-
939977
return results; // Return results array to indicate completion
940978
} catch (error) {
941979
elizaLogger.error("Error in processTweetActions:", error);

0 commit comments

Comments
 (0)