|
| 1 | +import { createServerToolkit } from "@/toolkits/create-toolkit"; |
| 2 | +import { baseTwitterToolkitConfig } from "./base"; |
| 3 | +import { |
| 4 | + getUserProfileToolConfigServer, |
| 5 | + getLatestTweetsToolConfigServer, |
| 6 | +} from "./tools/server"; |
| 7 | +import { TwitterTools } from "./tools"; |
| 8 | +import { api } from "@/trpc/server"; |
| 9 | +import { TwitterApi } from "twitter-api-v2"; |
| 10 | + |
| 11 | +export const twitterToolkitServer = createServerToolkit( |
| 12 | + baseTwitterToolkitConfig, |
| 13 | + `You have access to the Twitter toolkit for fetching user profiles and tweets. This toolkit provides: |
| 14 | +
|
| 15 | +- **Get User Profile**: Retrieve detailed information about a Twitter user by their username |
| 16 | +- **Get Latest Tweets**: Fetch the most recent tweets from a user (up to 100 tweets) |
| 17 | +
|
| 18 | +**Tool Sequencing Strategies:** |
| 19 | +1. **User Research**: Start with Get User Profile to understand a user's background, then use Get Latest Tweets to see their recent activity |
| 20 | +2. **Content Analysis**: Use Get Latest Tweets to analyze a user's recent posts and engagement patterns |
| 21 | +3. **Profile Verification**: Combine Get User Profile with Get Latest Tweets to verify user authenticity and activity |
| 22 | +
|
| 23 | +**Best Practices:** |
| 24 | +- Use usernames without the @ symbol (e.g., 'elonmusk' not '@elonmusk') |
| 25 | +- For tweet analysis, start with fewer results (10-20) to get a quick overview |
| 26 | +- Use the exclude_retweets and exclude_replies options to focus on original content |
| 27 | +- Consider the user's follower count and verification status when analyzing their influence`, |
| 28 | + async () => { |
| 29 | + const account = await api.accounts.getAccountByProvider("twitter"); |
| 30 | + |
| 31 | + if (!account) { |
| 32 | + throw new Error( |
| 33 | + "No Twitter account found. Please connect your Twitter account first.", |
| 34 | + ); |
| 35 | + } |
| 36 | + |
| 37 | + if (!account.access_token) { |
| 38 | + throw new Error( |
| 39 | + "Twitter access token not found. Please reconnect your Twitter account.", |
| 40 | + ); |
| 41 | + } |
| 42 | + |
| 43 | + // Create Twitter API client with user's access token |
| 44 | + const client = new TwitterApi(account.access_token); |
| 45 | + |
| 46 | + return { |
| 47 | + [TwitterTools.GetUserProfile]: getUserProfileToolConfigServer(client), |
| 48 | + [TwitterTools.GetLatestTweets]: getLatestTweetsToolConfigServer(client), |
| 49 | + }; |
| 50 | + }, |
| 51 | +); |
0 commit comments