From b9cf4017974a8033e09489e61bd72d8855cd5cc9 Mon Sep 17 00:00:00 2001 From: xm0onh Date: Mon, 6 Jan 2025 18:26:48 -0800 Subject: [PATCH 1/2] Separate trend source timeline from response timeline process issue #111 --- .../src/agents/tools/fetchTimelineTool.ts | 24 ++++++++++------- .../workflows/kol/nodes/analyzeTrendNode.ts | 4 +-- .../workflows/kol/nodes/collectDataNode.ts | 26 ++++++++++++++++--- .../src/agents/workflows/kol/workflow.ts | 4 +++ 4 files changed, 43 insertions(+), 15 deletions(-) diff --git a/auto-agents-framework/src/agents/tools/fetchTimelineTool.ts b/auto-agents-framework/src/agents/tools/fetchTimelineTool.ts index 9cca2699..4c55894b 100644 --- a/auto-agents-framework/src/agents/tools/fetchTimelineTool.ts +++ b/auto-agents-framework/src/agents/tools/fetchTimelineTool.ts @@ -29,25 +29,29 @@ export const createFetchTimelineTool = (twitterApi: TwitterApi) => numRandomFollowers: number; }) => { try { - const myTimelineTweets = ( - await twitterApi.getMyTimeline(numTimelineTweets, processedIds) - ).slice(0, numTimelineTweets); + const myTimelineTweets = await twitterApi.getMyTimeline(numTimelineTweets, processedIds); const followingRecents = await twitterApi.getFollowingRecentTweets( numFollowingRecentTweets, numRandomFollowers, ); - const tweets = new Set([...myTimelineTweets, ...followingRecents]); - const sortedTweets = Array.from(tweets).sort( - (a, b) => new Date(b.timeParsed!).getTime() - new Date(a.timeParsed!).getTime(), - ); - logger.info('Timeline tweets:', { tweets: sortedTweets.length }); + const tweets = { + timelineTweets: myTimelineTweets, + followingRecents: followingRecents, + }; + logger.info('Timeline tweets:', { + timelineTweets: tweets.timelineTweets.length, + followingRecents: tweets.followingRecents.length, + }); return { - tweets: sortedTweets, + tweets: tweets, }; } catch (error) { logger.error('Error in fetchTimelineTool:', error); return { - tweets: [], + tweets: { + timelineTweets: [], + followingRecents: [], + }, }; } }, diff --git a/auto-agents-framework/src/agents/workflows/kol/nodes/analyzeTrendNode.ts b/auto-agents-framework/src/agents/workflows/kol/nodes/analyzeTrendNode.ts index 10f6b6d2..06db462c 100644 --- a/auto-agents-framework/src/agents/workflows/kol/nodes/analyzeTrendNode.ts +++ b/auto-agents-framework/src/agents/workflows/kol/nodes/analyzeTrendNode.ts @@ -9,11 +9,11 @@ export const createAnalyzeTrendNode = (config: WorkflowConfig) => async (state: typeof State.State) => { logger.info('Analyze Trend Node - Analyzing trends'); - const tweets = Array.from(state.timelineTweets.values()).map(({ username, text }) => ({ + const tweets = Array.from(state.trendAnalysisTweets.values()).map(({ username, text }) => ({ username, text, })); - logger.info('Tweets:', { tweets: tweets.length }); + logger.info('Tweets for trend analysis:', { tweets: tweets.length }); const trendAnalysis = await config.prompts.trendPrompt .pipe(config.llms.analyze) diff --git a/auto-agents-framework/src/agents/workflows/kol/nodes/collectDataNode.ts b/auto-agents-framework/src/agents/workflows/kol/nodes/collectDataNode.ts index 61a22859..b52400ee 100644 --- a/auto-agents-framework/src/agents/workflows/kol/nodes/collectDataNode.ts +++ b/auto-agents-framework/src/agents/workflows/kol/nodes/collectDataNode.ts @@ -23,6 +23,7 @@ export const createCollectDataNode = ]; logger.info('Processed IDs:', { processedIds: processedIds.length }); + //////////MY RECENT REPLIES////////// const myRecentRepliesToolResponse = await invokeFetchMyRecentRepliesTool(config.toolNode, { maxRecentReplies: twitterConfig.MAX_MY_RECENT_REPLIES, }); @@ -30,16 +31,31 @@ export const createCollectDataNode = myRecentRepliesToolResponse.messages[myRecentRepliesToolResponse.messages.length - 1].content; const myRecentReplies = convertMessageContentToTweets(myRecentRepliesContent); + //////////TIMELINE & TREND////////// const timelineToolResponse = await invokeFetchTimelineTool(config.toolNode, { processedIds, numTimelineTweets: twitterConfig.NUM_TIMELINE_TWEETS, numFollowingRecentTweets: twitterConfig.NUM_FOLLOWING_RECENT_TWEETS, numRandomFollowers: twitterConfig.NUM_RANDOM_FOLLOWERS, }); - const timelineContent = - timelineToolResponse.messages[timelineToolResponse.messages.length - 1].content; - const timelineTweets = convertMessageContentToTweets(timelineContent); + const timelineContent = JSON.parse( + timelineToolResponse.messages[timelineToolResponse.messages.length - 1].content, + ); + const timelineTweetsConverted = convertMessageContentToTweets( + JSON.stringify({ tweets: timelineContent.tweets.timelineTweets }), + ); + const followingRecentsConverted = convertMessageContentToTweets( + JSON.stringify({ tweets: timelineContent.tweets.followingRecents }), + ); + const trendAnalysisTweets = [...timelineTweetsConverted, ...followingRecentsConverted]; + const timelineTweets = [ + ...Array.from(timelineTweetsConverted) + .sort(() => Math.random() - 0.5) + .slice(0, twitterConfig.NUM_TIMELINE_TWEETS), + ...followingRecentsConverted, + ]; + //////////MENTIONS////////// const mentionsToolResponse = await invokeFetchMentionsTool(config.toolNode, { maxMentions: twitterConfig.MAX_MENTIONS, }); @@ -47,6 +63,7 @@ export const createCollectDataNode = mentionsToolResponse.messages[mentionsToolResponse.messages.length - 1].content; const mentionsTweets = convertMessageContentToTweets(mentionsContent); + //////////MY RECENT TWEETS////////// const myRecentTweetsToolResponse = await invokeFetchMyRecentTweetsTool(config.toolNode, { maxMyRecentTweets: twitterConfig.MAX_MY_RECENT_TWEETS, }); @@ -54,6 +71,7 @@ export const createCollectDataNode = myRecentTweetsToolResponse.messages[myRecentTweetsToolResponse.messages.length - 1].content; const myRecentTweets = convertMessageContentToTweets(myRecentTweetsContent); + //////////REPLIED TO TWEET IDS////////// const myRepliedToIds = JSON.parse(myRecentTweetsContent).repliedToTweetIds ? JSON.parse(myRecentTweetsContent).repliedToTweetIds : []; @@ -61,6 +79,7 @@ export const createCollectDataNode = logger.info('Tool response received:', { myRecentRepliesCount: myRecentReplies.length, timelineMessageCount: timelineTweets.length, + trendAnalysisMessageCount: trendAnalysisTweets.length, mentionsMessageCount: mentionsTweets.length, myRecentTweetsCount: myRecentTweets.length, repliedToTweetIds: myRepliedToIds.length, @@ -68,6 +87,7 @@ export const createCollectDataNode = return { timelineTweets: new Set(timelineTweets), + trendAnalysisTweets: new Set(trendAnalysisTweets), mentionsTweets: new Set(mentionsTweets), myRecentTweets: new Set(myRecentTweets), myRecentReplies: new Set(myRecentReplies), diff --git a/auto-agents-framework/src/agents/workflows/kol/workflow.ts b/auto-agents-framework/src/agents/workflows/kol/workflow.ts index 4b520c7d..4bf906fd 100644 --- a/auto-agents-framework/src/agents/workflows/kol/workflow.ts +++ b/auto-agents-framework/src/agents/workflows/kol/workflow.ts @@ -45,6 +45,10 @@ export const State = Annotation.Root({ default: () => [], reducer: (curr, update) => update, }), + trendAnalysisTweets: Annotation>({ + default: () => new Set(), + reducer: (curr, update) => new Set([...update]), + }), trendAnalysis: Annotation, dsnData: Annotation[]>({ default: () => [], From ff856009f015777c2f50fbca2c56ffa7c195c2dd Mon Sep 17 00:00:00 2001 From: xm0onh Date: Mon, 6 Jan 2025 18:44:52 -0800 Subject: [PATCH 2/2] clean coding on fetchTimeLineTool --- auto-agents-framework/src/agents/tools/fetchTimelineTool.ts | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/auto-agents-framework/src/agents/tools/fetchTimelineTool.ts b/auto-agents-framework/src/agents/tools/fetchTimelineTool.ts index 4c55894b..12b006a9 100644 --- a/auto-agents-framework/src/agents/tools/fetchTimelineTool.ts +++ b/auto-agents-framework/src/agents/tools/fetchTimelineTool.ts @@ -42,9 +42,7 @@ export const createFetchTimelineTool = (twitterApi: TwitterApi) => timelineTweets: tweets.timelineTweets.length, followingRecents: tweets.followingRecents.length, }); - return { - tweets: tweets, - }; + return { tweets }; } catch (error) { logger.error('Error in fetchTimelineTool:', error); return {