import { EventEmitter } from 'events'; import { Agent } from '../core/agent'; import { AgentSwarm } from '../core/agent-swarm'; /** * Configuration for the Twitter connector */ export interface TwitterDirectConnectorConfig { username?: string; password?: string; email?: string; apiKey?: string; apiSecret?: string; accessToken?: string; accessSecret?: string; monitorKeywords?: string[]; monitorUsers?: string[]; monitorMentions?: boolean; monitorReplies?: boolean; autoReply?: boolean; pollInterval?: number; persistCookies?: boolean; cookiesPath?: string; maxRetries?: number; retryDelay?: number; debug?: boolean; } /** * Internal Tweet interface to abstract away the library-specific implementation */ export interface Tweet { id?: string; text: string; author: { id?: string; username?: string; name?: string; }; createdAt?: Date; isRetweet?: boolean; isReply?: boolean; inReplyToId?: string; inReplyToUser?: string; mediaUrls?: string[]; poll?: { options: { label: string; votes?: number; }[]; endTime?: Date; }; metrics?: { likes?: number; retweets?: number; replies?: number; views?: number; }; entities?: { hashtags?: string[]; mentions?: string[]; urls?: string[]; }; } /** * Media data for tweet attachments */ export interface TwitterMediaData { data: Buffer; mediaType: string; } /** * Poll data for creating Twitter polls */ export interface TwitterPollData { options: { label: string; }[]; durationMinutes: number; } /** * Tweet options for enhanced functionality */ export interface TweetOptions { media?: TwitterMediaData[]; poll?: TwitterPollData; replyTo?: string; quoteId?: string; } /** * Twitter connector to integrate agents with Twitter * Uses agent-twitter-client to connect to Twitter without requiring API keys * Optionally supports Twitter API v2 for enhanced functionality */ export declare class TwitterDirectConnector extends EventEmitter { config: TwitterDirectConnectorConfig; private agent?; private swarm?; private scraper; private connected; private monitorInterval; private retryCount; private seenTweetIds; private logger; /** * Creates a new Twitter connector * * @param config - Configuration options */ constructor(config: TwitterDirectConnectorConfig); /** * Helper method to wait with exponential backoff * * @param attempt - Current attempt number * @returns Promise that resolves after the delay */ private exponentialBackoff; /** * Attempts to load cookies from storage * * @returns True if cookies were loaded successfully, false otherwise */ private loadCookies; /** * Saves current cookies to storage */ private saveCookies; /** * Connects to Twitter * * @param agent - The agent to connect * @returns Promise resolving when connected */ connect(agent: Agent | AgentSwarm): Promise; /** * Disconnects from Twitter * * @returns Promise resolving when disconnected */ disconnect(): Promise; /** * Posts a tweet * * @param content - The content of the tweet * @param options - Optional tweet options (media, polls, etc) * @returns Promise resolving to the tweet ID */ tweet(content: string, options?: string | TweetOptions): Promise; /** * Helper method to type content into the tweet composer * * @param page - The browser page * @param content - The content to type */ private typeIntoComposer; /** * Helper method to click the tweet button * * @param page - The browser page */ private clickTweetButton; /** * Likes a tweet * * @param tweetId - The ID of the tweet to like * @returns Promise resolving when the tweet is liked */ like(tweetId: string): Promise; /** * Retweets a tweet * * @param tweetId - The ID of the tweet to retweet * @returns Promise resolving when the tweet is retweeted */ retweet(tweetId: string): Promise; /** * Quote retweets a tweet * * @param tweetId - The ID of the tweet to quote * @param content - The content to add with the quote * @param media - Optional media to include with the quote * @returns Promise resolving when the quote tweet is posted */ quoteTweet(tweetId: string, content: string, media?: TwitterMediaData[]): Promise; /** * Follows a user * * @param username - The username of the user to follow * @returns Promise resolving when the user is followed */ follow(username: string): Promise; /** * Gets a user's profile information * * @param username - The username of the user * @returns Promise resolving to the user profile */ getProfile(username: string): Promise; /** * Gets trends on Twitter * * @returns Promise resolving to current Twitter trends */ getTrends(): Promise; /** * Gets a specific tweet by ID * * @param tweetId - The ID of the tweet to get * @returns Promise resolving to the tweet */ getTweet(tweetId: string): Promise; /** * Searches for tweets by keyword * * @param query - The search query * @param count - Maximum number of tweets to return * @returns Promise resolving to the list of tweets */ searchTweets(query: string, count?: number): Promise; /** * Gets tweets from a specific user * * @param username - The username to get tweets from * @param count - Maximum number of tweets to return * @returns Promise resolving to the list of tweets */ getUserTweets(username: string, count?: number): Promise; /** * Gets the home timeline * * @param count - Maximum number of tweets to return * @returns Promise resolving to the timeline tweets */ getHomeTimeline(count?: number): Promise; /** * Utility function to format a tweet from the API response * * @param tweet - The raw tweet object from the API * @returns The formatted Tweet object */ private formatTweet; /** * Utility function to format a tweet from the V2 API response * * @param tweetData - The raw tweet data from the V2 API * @returns The formatted Tweet object */ private formatTweetFromV2; /** * Sets up monitoring for tweets */ private setupMonitoring; /** * Checks for new tweets that match monitoring criteria */ private checkForNewTweets; /** * Handles auto-reply to a tweet * * @param tweet - The tweet to reply to */ private handleAutoReply; /** * Interacts with Grok through Twitter's interface * * @param messages - Array of messages in the conversation * @param conversationId - Optional conversation ID for continuing a conversation * @returns Promise resolving to Grok's response */ grokChat(messages: { role: 'user' | 'assistant'; content: string; }[], conversationId?: string): Promise; }