Skip to content

Commit

Permalink
update sendTweet logic, though it still needs further investigation a…
Browse files Browse the repository at this point in the history
…nd fixing
  • Loading branch information
jfrank-summit committed Jan 1, 2025
1 parent b1e94a3 commit 2cab0c8
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 21 deletions.
14 changes: 8 additions & 6 deletions auto-agents-framework/src/agents/tools/postTweetTool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,24 @@ export const createPostTweetTool = (twitterApi: TwitterApi) =>
try {
if (config.twitterConfig.POST_TWEETS) {
const postedTweet = await twitterApi.sendTweet(tweet, inReplyTo);
logger.info('Tweet posted successfully', {
postedTweet: { id: postedTweet?.id, text: postedTweet?.text },
});
//TODO: After sending the tweet, we need to get the latest tweet, ensure it is the same as we sent and return it
//This has not been working as expected, so we need to investigate this later
// logger.info('Tweet posted successfully', {
// postedTweet: { id: postedTweet?.id, text: postedTweet?.text },
// });
return {
postedTweet,
postedTweet: true,
};
} else {
logger.info('Tweet not posted', { tweet });
return {
postedTweet: null,
postedTweet: false,
};
}
} catch (error) {
logger.error('Error posting tweet:', error);
return {
postedTweet: null,
postedTweet: false,
};
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,14 @@ const postResponse = async (
thread,
recentTweets,
});

//TODO: After sending the tweet, we need to get the latest tweet, ensure it is the same as we sent and return it
//This has not been working as expected, so we need to investigate this later
const tweet = await invokePostTweetTool(config.toolNode, response.content, decision.tweet.id);
return { ...response, tweetId: tweet ? tweet.id : null };
return {
...response,
//tweetId: tweet ? tweet.id : null
};
};

export const createGenerateTweetNode =
Expand Down Expand Up @@ -65,22 +71,25 @@ export const createGenerateTweetNode =
shouldEngage.map(d => postResponse(config, d, recentTweets)),
);

// Generate a top level tweet TODO: add a check to see if it has been long enough since the last tweet
// Generate a top level tweet
//TODO: add a check to see if it has been long enough since the last tweet
const generatedTweet = await tweetPrompt
.pipe(config.llms.generation)
.pipe(trendTweetParser)
.invoke({
trendAnalysis: state.trendAnalysis,
recentTweets,
});
//TODO: After sending the tweet, we need to get the latest tweet, ensure it is the same as we sent and return it
//This has not been working as expected, so we need to investigate this later
const postedTweet = await invokePostTweetTool(config.toolNode, generatedTweet.tweet);

// Transform the data into an array format expected by DSN
const formattedDsnData = [
...postedResponses.map(response => ({
type: 'response',
content: response.content,
tweetId: response.tweetId,
//tweetId: response.tweetId,
strategy: response.strategy,
})),
...shouldNotEngage.map(item => ({
Expand All @@ -91,7 +100,7 @@ export const createGenerateTweetNode =
{
type: 'generated_tweet',
content: generatedTweet.tweet,
tweetId: postedTweet ? postedTweet.id : null,
//tweetId: postedTweet ? postedTweet.id : null,
},
];

Expand Down
18 changes: 8 additions & 10 deletions auto-agents-framework/src/services/twitter/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,13 +174,6 @@ const getMyUnrepliedToMentions = async (
return withThreads;
};

const postTweet = async (scraper: Scraper, tweet: string, inReplyTo?: string) => {
if (tweet.length > 280) {
return scraper.sendLongTweet(tweet, inReplyTo);
}
return scraper.sendTweet(tweet, inReplyTo);
};

const getFollowingRecentTweets = async (
scraper: Scraper,
username: string,
Expand Down Expand Up @@ -227,10 +220,11 @@ export const createTwitterApi = async (
if (!isLoggedIn) {
throw new Error('Failed to initialize Twitter Api - not logged in');
}

const userId = await scraper.getUserIdByScreenName(username);
return {
scraper,
username: username,
userId: userId,
getMyUnrepliedToMentions: (maxResults: number, sinceId?: string) =>
getMyUnrepliedToMentions(scraper, username, maxResults, sinceId),

Expand Down Expand Up @@ -276,9 +270,13 @@ export const createTwitterApi = async (
return tweets.filter(isValidTweet).map(tweet => convertTimelineTweetToTweet(tweet));
},

//TODO: After sending the tweet, we need to get the latest tweet, ensure it is the same as we sent and return it
//This has not been working as expected, so we need to investigate this later
sendTweet: async (tweet: string, inReplyTo?: string) => {
const _postedTweet = await postTweet(scraper, tweet, inReplyTo);
return await scraper.getLatestTweet(username);
tweet.length > 280
? await scraper.sendLongTweet(tweet, inReplyTo)
: await scraper.sendTweet(tweet, inReplyTo);
logger.info('Tweet sent', { tweet, inReplyTo });
},
};
};
3 changes: 2 additions & 1 deletion auto-agents-framework/src/services/twitter/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export { Tweet, Profile, Scraper } from 'agent-twitter-client';
export interface TwitterApi {
scraper: Scraper;
username: string;
userId: string;
getMyUnrepliedToMentions: (maxResults: number, sinceId?: string) => Promise<Tweet[]>;
getFollowingRecentTweets: (maxResults: number, numberOfUsers: number) => Promise<Tweet[]>;
isLoggedIn: () => Promise<boolean>;
Expand All @@ -16,5 +17,5 @@ export interface TwitterApi {
getFollowing: (userId: string, limit: number) => Promise<Profile[]>;
getMyTimeline: (count: number, excludeIds: string[]) => Promise<Tweet[]>;
getFollowingTimeline: (count: number, excludeIds: string[]) => Promise<Tweet[]>;
sendTweet: (tweet: string, inReplyTo?: string) => Promise<Tweet | null | void>;
sendTweet: (tweet: string, inReplyTo?: string) => Promise<void>;
}

0 comments on commit 2cab0c8

Please sign in to comment.