@@ -627,14 +627,11 @@ export class TwitterPostClient {
627
627
628
628
const homeTimeline = await this . client . fetchTimelineForActions ( ) ;
629
629
const results = [ ] ;
630
- let processedTweets = 0 ;
631
630
const maxActionsProcessing =
632
631
this . client . twitterConfig . MAX_ACTIONS_PROCESSING ;
632
+ const processedTimelines = [ ] ;
633
633
634
634
for ( const tweet of homeTimeline ) {
635
- if ( processedTweets >= maxActionsProcessing ) {
636
- break ;
637
- }
638
635
try {
639
636
// Skip if we've already processed this tweet
640
637
const memory =
@@ -685,9 +682,52 @@ export class TwitterPostClient {
685
682
) ;
686
683
continue ;
687
684
}
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
+ }
688
713
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 ) ;
690
726
727
+ for ( const timeline of sortedTimelines ) {
728
+ const { actionResponse, tweetState, roomId, tweet } = timeline ;
729
+ try {
730
+ const executedActions : string [ ] = [ ] ;
691
731
// Execute actions
692
732
if ( actionResponse . like ) {
693
733
try {
@@ -923,10 +963,9 @@ export class TwitterPostClient {
923
963
924
964
results . push ( {
925
965
tweetId : tweet . id ,
926
- parsedActions : actionResponse ,
966
+ actionResponse : actionResponse ,
927
967
executedActions,
928
968
} ) ;
929
- processedTweets ++ ;
930
969
} catch ( error ) {
931
970
elizaLogger . error (
932
971
`Error processing tweet ${ tweet . id } :` ,
@@ -935,7 +974,6 @@ export class TwitterPostClient {
935
974
continue ;
936
975
}
937
976
}
938
-
939
977
return results ; // Return results array to indicate completion
940
978
} catch ( error ) {
941
979
elizaLogger . error ( "Error in processTweetActions:" , error ) ;
0 commit comments