Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: improving client typing #1036

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
7ada729
changed types according to multiagent
BalanaguYashwanth Dec 13, 2024
f0c0e19
Add twitter multi account config setup
BalanaguYashwanth Dec 13, 2024
6caf789
Revert the generate twitter loop
BalanaguYashwanth Dec 13, 2024
e56ecb3
Modify the twitter client documentation
BalanaguYashwanth Dec 13, 2024
373b290
Revert to default character in agent src index file
BalanaguYashwanth Dec 13, 2024
9e4d6a7
Merge branch 'develop' into 656--fix-twitter-multi-agent-support
BalanaguYashwanth Dec 13, 2024
4c0666f
Resolve bug on optional config
BalanaguYashwanth Dec 13, 2024
95b8e77
Merge branch 'ai16z:main' into 656--fix-twitter-multi-agent-support
BalanaguYashwanth Dec 13, 2024
904cf2a
Merge branch 'develop' of https://github.com/BalanaguYashwanth/eliza …
BalanaguYashwanth Dec 13, 2024
75dd96e
Merge branch 'develop' into 656--fix-twitter-multi-agent-support
odilitime Dec 13, 2024
593da02
Remove config
BalanaguYashwanth Dec 13, 2024
f224fbf
Remove unuse config related code
BalanaguYashwanth Dec 13, 2024
f0b21a1
Merge branch 'develop' into 656--fix-twitter-multi-agent-support
BalanaguYashwanth Dec 13, 2024
7242eae
Update lockfile to match package.json
BalanaguYashwanth Dec 13, 2024
40df2db
ReUpdate lockfile to match package.json
BalanaguYashwanth Dec 13, 2024
1a7b788
Merge branch 'develop' of https://github.com/BalanaguYashwanth/eliza …
BalanaguYashwanth Dec 14, 2024
6cab7ef
Merge branch 'develop' of https://github.com/BalanaguYashwanth/eliza …
BalanaguYashwanth Dec 14, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions agent/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -338,22 +338,23 @@ export async function initializeClients(
character.clients?.map((str) => str.toLowerCase()) || [];
elizaLogger.log("initializeClients", clientTypes, "for", character.name);

if (clientTypes.includes("auto")) {
if (clientTypes.includes(Clients.DIRECT)) {
const autoClient = await AutoClientInterface.start(runtime);
if (autoClient) clients.auto = autoClient;
}

if (clientTypes.includes("discord")) {
if (clientTypes.includes(Clients.DISCORD)) {
const discordClient = await DiscordClientInterface.start(runtime);
if (discordClient) clients.discord = discordClient;
}

if (clientTypes.includes("telegram")) {
if (clientTypes.includes(Clients.TELEGRAM)) {
const telegramClient = await TelegramClientInterface.start(runtime);
if (telegramClient) clients.telegram = telegramClient;
}

if (clientTypes.includes("twitter")) {
if (clientTypes.includes(Clients.TWITTER)) {
TwitterClientInterface.enableSearch = !isFalsish(getSecret(character, "TWITTER_SEARCH_ENABLE"));
const twitterClient = await TwitterClientInterface.start(runtime);
// TODO: This might be incorrect, please test if you are concerned about this functionality
// By default we have disabled this because it is annoying for users
Expand All @@ -363,7 +364,7 @@ export async function initializeClients(
if (twitterClient) clients.twitter = twitterClient;
}

if (clientTypes.includes("farcaster")) {
if (clientTypes.includes(Clients.FARCASTER)) {
// why is this one different :(
const farcasterClient = new FarcasterAgentClient(runtime);
if (farcasterClient) {
Expand Down
8 changes: 4 additions & 4 deletions docs/docs/packages/agent.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,16 +138,16 @@ export async function initializeClients(
const clients = [];
const clientTypes = character.clients?.map((str) => str.toLowerCase()) || [];

if (clientTypes.includes("discord")) {
if (clientTypes.includes(Clients.DISCORD)) {
clients.push(await DiscordClientInterface.start(runtime));
}
if (clientTypes.includes("telegram")) {
if (clientTypes.includes(Clients.TELEGRAM)) {
clients.push(await TelegramClientInterface.start(runtime));
}
if (clientTypes.includes("twitter")) {
if (clientTypes.includes(Clients.TWITTER)) {
clients.push(await TwitterClientInterface.start(runtime));
}
if (clientTypes.includes("auto")) {
if (clientTypes.includes(Clients.DIRECT)) {
clients.push(await AutoClientInterface.start(runtime));
}

Expand Down
12 changes: 4 additions & 8 deletions docs/docs/packages/agents.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,20 +101,16 @@ export async function initializeClients(
const clients = [];
const clientTypes = character.clients?.map((str) => str.toLowerCase()) || [];

// Initialize requested clients
if (clientTypes.includes("discord")) {
if (clientTypes.includes(Clients.DISCORD)) {
clients.push(await DiscordClientInterface.start(runtime));
}

if (clientTypes.includes("telegram")) {
if (clientTypes.includes(Clients.TELEGRAM)) {
clients.push(await TelegramClientInterface.start(runtime));
}

if (clientTypes.includes("twitter")) {
if (clientTypes.includes(Clients.TWITTER)) {
clients.push(await TwitterClientInterface.start(runtime));
}

if (clientTypes.includes("auto")) {
if (clientTypes.includes(Clients.DIRECT)) {
clients.push(await AutoClientInterface.start(runtime));
}

Expand Down
1 change: 0 additions & 1 deletion docs/docs/packages/clients.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,6 @@ The Twitter client enables posting, searching, and interacting with Twitter user

```typescript
import { TwitterClientInterface } from "@eliza/client-twitter";

// Initialize client
const client = await TwitterClientInterface.start(runtime);

Expand Down
20 changes: 12 additions & 8 deletions packages/client-twitter/src/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,15 +155,18 @@ export class ClientBase extends EventEmitter {
async init() {
//test
const username = this.runtime.getSetting("TWITTER_USERNAME");
const password = this.runtime.getSetting("TWITTER_PASSWORD");
const email = this.runtime.getSetting("TWITTER_EMAIL");
const twitter2faSecret = this.runtime.getSetting("TWITTER_2FA_SECRET") || undefined;
const cookies = this.runtime.getSetting("TWITTER_COOKIES");


if (!username) {
throw new Error("Twitter username not configured");
}
// Check for Twitter cookies
if (this.runtime.getSetting("TWITTER_COOKIES")) {
const cookiesArray = JSON.parse(
this.runtime.getSetting("TWITTER_COOKIES")
);
if (cookies) {
const cookiesArray = JSON.parse(cookies);

await this.setCookiesFromArray(cookiesArray);
} else {
Expand All @@ -186,9 +189,9 @@ export class ClientBase extends EventEmitter {
try {
await this.twitterClient.login(
username,
this.runtime.getSetting("TWITTER_PASSWORD"),
this.runtime.getSetting("TWITTER_EMAIL"),
this.runtime.getSetting("TWITTER_2FA_SECRET") || undefined
password,
email,
twitter2faSecret
);
} catch (error) {
elizaLogger.error(`Login attempt failed: ${error.message}`);
Expand Down Expand Up @@ -480,10 +483,11 @@ export class ClientBase extends EventEmitter {
}

const timeline = await this.fetchHomeTimeline(cachedTimeline ? 10 : 50);
const username = this.runtime.getSetting("TWITTER_USERNAME");

// Get the most recent 20 mentions and interactions
const mentionsAndInteractions = await this.fetchSearchTweets(
`@${this.runtime.getSetting("TWITTER_USERNAME")}`,
`@${username}`,
20,
SearchMode.Latest
);
Expand Down
4 changes: 2 additions & 2 deletions packages/client-twitter/src/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export async function validateTwitterConfig(
runtime: IAgentRuntime
): Promise<TwitterConfig> {
try {
const config = {
const twitterConfig = {
TWITTER_DRY_RUN:
runtime.getSetting("TWITTER_DRY_RUN") ||
process.env.TWITTER_DRY_RUN ||
Expand All @@ -46,7 +46,7 @@ export async function validateTwitterConfig(
DEFAULT_MAX_TWEET_LENGTH.toString(),
};

return twitterEnvSchema.parse(config);
return twitterEnvSchema.parse(twitterConfig);
} catch (error) {
if (error instanceof z.ZodError) {
const errorMessages = error.errors
Expand Down
32 changes: 15 additions & 17 deletions packages/client-twitter/src/interactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ Here is the current post text again. Remember to include an action if the curren
{{currentPost}}
` + messageCompletionFooter;

export const twitterShouldRespondTemplate = (targetUsersStr: string) =>
export const twitterShouldRespondTemplate = (targetUsersStr: string) =>
`# INSTRUCTIONS: Determine if {{agentName}} (@{{twitterUserName}}) should respond to the message and participate in the conversation. Do not comment. Just respond with "true" or "false".

Response options are RESPOND, IGNORE and STOP.
Expand Down Expand Up @@ -89,7 +89,6 @@ Thread of Tweets You Are Replying To:
export class TwitterInteractionClient {
client: ClientBase;
runtime: IAgentRuntime;

constructor(client: ClientBase, runtime: IAgentRuntime) {
this.client = client;
this.runtime = runtime;
Expand Down Expand Up @@ -133,7 +132,7 @@ export class TwitterInteractionClient {
.filter(u => u.length > 0); // Filter out empty strings after split

elizaLogger.log("Processing target users:", TARGET_USERS);

if (TARGET_USERS.length > 0) {
// Create a map to store tweets by user
const tweetsByUser = new Map<string, Tweet[]>();
Expand All @@ -142,24 +141,23 @@ export class TwitterInteractionClient {
for (const username of TARGET_USERS) {
try {
const userTweets = (await this.client.twitterClient.fetchSearchTweets(
`from:${username}`,
3,
SearchMode.Latest
`from:${username}`,
3,
SearchMode.Latest
)).tweets;

// Filter for unprocessed, non-reply, recent tweets
const validTweets = userTweets.filter(tweet => {
const isUnprocessed = !this.client.lastCheckedTweetId ||
parseInt(tweet.id) > this.client.lastCheckedTweetId;
const isUnprocessed = !this.client.lastCheckedTweetId || parseInt(tweet.id) > this.client.lastCheckedTweetId;
const isRecent = (Date.now() - (tweet.timestamp * 1000)) < 2 * 60 * 60 * 1000;

elizaLogger.log(`Tweet ${tweet.id} checks:`, {
isUnprocessed,
isRecent,
isReply: tweet.isReply,
isRetweet: tweet.isRetweet
});

return isUnprocessed && !tweet.isReply && !tweet.isRetweet && isRecent;
});

Expand Down Expand Up @@ -191,7 +189,7 @@ export class TwitterInteractionClient {
elizaLogger.log("No target users configured, processing only mentions");
}



// Sort tweet candidates by ID in ascending order
uniqueTweetCandidates
Expand Down Expand Up @@ -359,17 +357,17 @@ export class TwitterInteractionClient {
const targetUsersStr = this.runtime.getSetting("TWITTER_TARGET_USERS");

// 2. Process the string to get valid usernames
const validTargetUsersStr = targetUsersStr && targetUsersStr.trim()
const validTargetUsersStr = targetUsersStr && targetUsersStr.trim()
? targetUsersStr.split(',') // Split by commas: "user1,user2" -> ["user1", "user2"]
.map(u => u.trim()) // Remove whitespace: [" user1 ", "user2 "] -> ["user1", "user2"]
.filter(u => u.length > 0)
.join(',')
: '';
.filter(u => u.length > 0)
.join(',')
: '';

const shouldRespondContext = composeContext({
state,
template: this.runtime.character.templates?.twitterShouldRespondTemplate?.(validTargetUsersStr) ||
this.runtime.character?.templates?.shouldRespondTemplate ||
template: this.runtime.character.templates?.twitterShouldRespondTemplate?.(validTargetUsersStr) ||
this.runtime.character?.templates?.shouldRespondTemplate ||
twitterShouldRespondTemplate(validTargetUsersStr),
});

Expand Down
16 changes: 9 additions & 7 deletions packages/client-twitter/src/post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ function truncateToCompleteSentence(
export class TwitterPostClient {
client: ClientBase;
runtime: IAgentRuntime;
twitterUsername: string;
private isProcessing: boolean = false;
private lastProcessTime: number = 0;
private stopProcessingActions: boolean = false;
Expand All @@ -111,7 +112,7 @@ export class TwitterPostClient {
timestamp: number;
}>(
"twitter/" +
this.runtime.getSetting("TWITTER_USERNAME") +
this.twitterUsername +
"/lastPost"
);

Expand All @@ -136,7 +137,6 @@ export class TwitterPostClient {
elizaLogger.log(`Next tweet scheduled in ${randomMinutes} minutes`);
};


const processActionsLoop = async () => {
const actionInterval = parseInt(
this.runtime.getSetting("ACTION_INTERVAL")
Expand Down Expand Up @@ -185,11 +185,13 @@ export class TwitterPostClient {
} else {
elizaLogger.log("Action processing loop disabled by configuration");
}
generateNewTweetLoop();
Copy link
Contributor Author

@BalanaguYashwanth BalanaguYashwanth Dec 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added this piece of code, without this tweets won't generate into twitter
generateNewTweetLoop();

Note - Validate this piece of code, required or not

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I caught that and PR'd that earlier too

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it

}

constructor(client: ClientBase, runtime: IAgentRuntime) {
this.client = client;
this.runtime = runtime;
this.twitterUsername = runtime.getSetting("TWITTER_USERNAME");
}

private async generateNewTweet() {
Expand Down Expand Up @@ -310,7 +312,7 @@ export class TwitterPostClient {
userId: this.client.profile.id,
inReplyToStatusId:
tweetResult.legacy.in_reply_to_status_id_str,
permanentUrl: `https://twitter.com/${this.runtime.getSetting("TWITTER_USERNAME")}/status/${tweetResult.rest_id}`,
permanentUrl: `https://twitter.com/${this.twitterUsername}/status/${tweetResult.rest_id}`,
hashtags: [],
mentions: [],
photos: [],
Expand Down Expand Up @@ -432,7 +434,7 @@ export class TwitterPostClient {

await this.runtime.ensureUserExists(
this.runtime.agentId,
this.runtime.getSetting("TWITTER_USERNAME"),
this.twitterUsername,
this.runtime.character.name,
"twitter"
);
Expand Down Expand Up @@ -463,7 +465,7 @@ export class TwitterPostClient {
content: { text: "", action: "" },
},
{
twitterUserName: this.runtime.getSetting("TWITTER_USERNAME"),
twitterUserName: this.twitterUsername,
currentTweet: `ID: ${tweet.id}\nFrom: ${tweet.name} (@${tweet.username})\nText: ${tweet.text}`,
}
);
Expand Down Expand Up @@ -549,7 +551,7 @@ export class TwitterPostClient {
content: { text: tweet.text, action: "QUOTE" }
},
{
twitterUserName: this.runtime.getSetting("TWITTER_USERNAME"),
twitterUserName: this.twitterUsername,
currentPost: `From @${tweet.username}: ${tweet.text}`,
formattedConversation,
imageContext: imageDescriptions.length > 0
Expand Down Expand Up @@ -698,7 +700,7 @@ export class TwitterPostClient {
content: { text: tweet.text, action: "" }
},
{
twitterUserName: this.runtime.getSetting("TWITTER_USERNAME"),
twitterUserName: this.twitterUsername,
currentPost: `From @${tweet.username}: ${tweet.text}`,
formattedConversation,
imageContext: imageDescriptions.length > 0
Expand Down
Loading
Loading