forked from elizaOS/eliza
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinteractions.ts
696 lines (601 loc) · 26 KB
/
interactions.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
import { SearchMode, type Tweet } from "agent-twitter-client";
import {
composeContext,
generateMessageResponse,
generateShouldRespond,
messageCompletionFooter,
shouldRespondFooter,
type Content,
type HandlerCallback,
type IAgentRuntime,
type Memory,
ModelClass,
type State,
stringToUuid,
elizaLogger,
getEmbeddingZeroVector,
type IImageDescriptionService,
ServiceType
} from "@elizaos/core";
import type { ClientBase } from "./base";
import { buildConversationThread, sendTweet, wait } from "./utils.ts";
export const twitterMessageHandlerTemplate =
`
# Areas of Expertise
{{knowledge}}
# About {{agentName}} (@{{twitterUserName}}):
{{bio}}
{{lore}}
{{topics}}
{{providers}}
{{characterPostExamples}}
{{postDirections}}
Recent interactions between {{agentName}} and other users:
{{recentPostInteractions}}
{{recentPosts}}
# TASK: Generate a post/reply in the voice, style and perspective of {{agentName}} (@{{twitterUserName}}) while using the thread of tweets as additional context:
Current Post:
{{currentPost}}
Here is the descriptions of images in the Current post.
{{imageDescriptions}}
Thread of Tweets You Are Replying To:
{{formattedConversation}}
# INSTRUCTIONS: Generate a post in the voice, style and perspective of {{agentName}} (@{{twitterUserName}}). You MUST include an action if the current post text includes a prompt that is similar to one of the available actions mentioned here:
{{actionNames}}
{{actions}}
Here is the current post text again. Remember to include an action if the current post text includes a prompt that asks for one of the available actions mentioned above (does not need to be exact)
{{currentPost}}
Here is the descriptions of images in the Current post.
{{imageDescriptions}}
` + messageCompletionFooter;
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.
PRIORITY RULE: ALWAYS RESPOND to these users regardless of topic or message content: ${targetUsersStr}. Topic relevance should be ignored for these users.
For other users:
- {{agentName}} should RESPOND to messages directed at them
- {{agentName}} should RESPOND to conversations relevant to their background
- {{agentName}} should IGNORE irrelevant messages
- {{agentName}} should IGNORE very short messages unless directly addressed
- {{agentName}} should STOP if asked to stop
- {{agentName}} should STOP if conversation is concluded
- {{agentName}} is in a room with other users and wants to be conversational, but not annoying.
IMPORTANT:
- {{agentName}} (aka @{{twitterUserName}}) is particularly sensitive about being annoying, so if there is any doubt, it is better to IGNORE than to RESPOND.
- For users not in the priority list, {{agentName}} (@{{twitterUserName}}) should err on the side of IGNORE rather than RESPOND if in doubt.
Recent Posts:
{{recentPosts}}
Current Post:
{{currentPost}}
Thread of Tweets You Are Replying To:
{{formattedConversation}}
# INSTRUCTIONS: Respond with [RESPOND] if {{agentName}} should respond, or [IGNORE] if {{agentName}} should not respond to the last message and [STOP] if {{agentName}} should stop participating in the conversation.
` + shouldRespondFooter;
export class TwitterInteractionClient {
client: ClientBase;
runtime: IAgentRuntime;
private isDryRun: boolean;
constructor(client: ClientBase, runtime: IAgentRuntime) {
this.client = client;
this.runtime = runtime;
this.running = false;
this.isDryRun = this.client.twitterConfig.TWITTER_DRY_RUN;
}
async start() {
const handleTwitterInteractionsLoop = () => {
if (!this.running) return;
this.handleTwitterInteractions();
setTimeout(
handleTwitterInteractionsLoop,
// Defaults to 2 minutes
this.client.twitterConfig.TWITTER_POLL_INTERVAL * 1000
);
};
if (this.client.profile) {
this.running = true;
handleTwitterInteractionsLoop();
}
}
async stop() {
this.running = false;
}
async handleTwitterInteractions() {
elizaLogger.log("Checking Twitter interactions");
if (!this.client.profile) {
elizaLogger.error('handleTwitterInteractions: No profile');
return;
}
const twitterUsername = this.client.profile.username;
try {
// Check for mentions
const mentionCandidates = (
await this.client.fetchSearchTweets(
`@${twitterUsername}`,
20,
SearchMode.Latest
)
).tweets;
elizaLogger.log(
"Completed checking mentioned tweets:",
mentionCandidates.length
);
let uniqueTweetCandidates = [...mentionCandidates];
// Only process target users if configured
if (this.client.twitterConfig.TWITTER_TARGET_USERS.length) {
const TARGET_USERS =
this.client.twitterConfig.TWITTER_TARGET_USERS;
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[]>();
// Fetch tweets from all target users
for (const username of TARGET_USERS) {
try {
const userTweets = (
await this.client.twitterClient.fetchSearchTweets(
`from:${username}`,
3,
SearchMode.Latest
)
).tweets;
// Filter for unprocessed, non-reply, recent tweets
const validTweets = userTweets.filter((tweet) => {
const isUnprocessed =
!this.client.lastCheckedTweetId ||
Number.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
);
});
if (validTweets.length > 0) {
tweetsByUser.set(username, validTweets);
elizaLogger.log(
`Found ${validTweets.length} valid tweets from ${username}`
);
}
} catch (error) {
elizaLogger.error(
`Error fetching tweets for ${username}:`,
error
);
continue;
}
}
// Select one tweet from each user that has tweets
const selectedTweets: Tweet[] = [];
for (const [username, tweets] of tweetsByUser) {
if (tweets.length > 0) {
// Randomly select one tweet from this user
const randomTweet =
tweets[
Math.floor(Math.random() * tweets.length)
];
selectedTweets.push(randomTweet);
elizaLogger.log(
`Selected tweet from ${username}: ${randomTweet.text?.substring(0, 100)}`
);
}
}
// Add selected tweets to candidates
uniqueTweetCandidates = [
...mentionCandidates,
...selectedTweets,
];
}
} else {
elizaLogger.log(
"No target users configured, processing only mentions"
);
}
// Sort tweet candidates by ID in ascending order
uniqueTweetCandidates
.sort((a, b) => a.id.localeCompare(b.id))
.filter((tweet) => tweet.userId !== this.client.profile.id);
// for each tweet candidate, handle the tweet
for (const tweet of uniqueTweetCandidates) {
if (
!this.client.lastCheckedTweetId ||
BigInt(tweet.id) > this.client.lastCheckedTweetId
) {
// Generate the tweetId UUID the same way it's done in handleTweet
const tweetId = stringToUuid(
tweet.id + "-" + this.runtime.agentId
);
// Check if we've already processed this tweet
const existingResponse =
await this.runtime.messageManager.getMemoryById(
tweetId
);
if (existingResponse) {
elizaLogger.log(
`Already responded to tweet ${tweet.id}, skipping`
);
continue;
}
elizaLogger.log("New Tweet found", tweet.permanentUrl);
const roomId = stringToUuid(
tweet.conversationId + "-" + this.runtime.agentId
);
const userIdUUID =
tweet.userId === this.client.profile.id
? this.runtime.agentId
: stringToUuid(tweet.userId!);
await this.runtime.ensureConnection(
userIdUUID,
roomId,
tweet.username,
tweet.name,
"twitter"
);
const thread = await buildConversationThread(
tweet,
this.client
);
const message = {
content: {
text: tweet.text,
imageUrls: tweet.photos?.map(photo => photo.url) || []
},
agentId: this.runtime.agentId,
userId: userIdUUID,
roomId,
};
await this.handleTweet({
tweet,
message,
thread,
});
// Update the last checked tweet ID after processing each tweet
this.client.lastCheckedTweetId = BigInt(tweet.id);
}
}
// Save the latest checked tweet ID to the file
await this.client.cacheLatestCheckedTweetId();
elizaLogger.log("Finished checking Twitter interactions");
} catch (error) {
elizaLogger.error("Error handling Twitter interactions:", error);
}
}
private async handleTweet({
tweet,
message,
thread,
}: {
tweet: Tweet;
message: Memory;
thread: Tweet[];
}) {
// Only skip if tweet is from self AND not from a target user
if (tweet.userId === this.client.profile.id &&
!this.client.twitterConfig.TWITTER_TARGET_USERS.includes(tweet.username)) {
return;
}
if (!message.content.text) {
elizaLogger.log("Skipping Tweet with no text", tweet.id);
return { text: "", action: "IGNORE" };
}
elizaLogger.log("Processing Tweet: ", tweet.id);
const formatTweet = (tweet: Tweet) => {
return ` ID: ${tweet.id}
From: ${tweet.name} (@${tweet.username})
Text: ${tweet.text}`;
};
const currentPost = formatTweet(tweet);
const formattedConversation = thread
.map(
(tweet) => `@${tweet.username} (${new Date(
tweet.timestamp * 1000
).toLocaleString("en-US", {
hour: "2-digit",
minute: "2-digit",
month: "short",
day: "numeric",
})}):
${tweet.text}`
)
.join("\n\n");
const imageDescriptionsArray = [];
try{
for (const photo of tweet.photos) {
const description = await this.runtime
.getService<IImageDescriptionService>(
ServiceType.IMAGE_DESCRIPTION
)
.describeImage(photo.url);
imageDescriptionsArray.push(description);
}
} catch (error) {
// Handle the error
elizaLogger.error("Error Occured during describing image: ", error);
}
let state = await this.runtime.composeState(message, {
twitterClient: this.client.twitterClient,
twitterUserName: this.client.twitterConfig.TWITTER_USERNAME,
currentPost,
formattedConversation,
imageDescriptions: imageDescriptionsArray.length > 0
? `\nImages in Tweet:\n${imageDescriptionsArray.map((desc, i) =>
`Image ${i + 1}: Title: ${desc.title}\nDescription: ${desc.description}`).join("\n\n")}`:""
});
// check if the tweet exists, save if it doesn't
const tweetId = stringToUuid(tweet.id + "-" + this.runtime.agentId);
const tweetExists =
await this.runtime.messageManager.getMemoryById(tweetId);
if (!tweetExists) {
elizaLogger.log("tweet does not exist, saving");
const userIdUUID = stringToUuid(tweet.userId as string);
const roomId = stringToUuid(tweet.conversationId);
const message = {
id: tweetId,
agentId: this.runtime.agentId,
content: {
text: tweet.text,
url: tweet.permanentUrl,
imageUrls: tweet.photos?.map(photo => photo.url) || [],
inReplyTo: tweet.inReplyToStatusId
? stringToUuid(
tweet.inReplyToStatusId +
"-" +
this.runtime.agentId
)
: undefined,
},
userId: userIdUUID,
roomId,
createdAt: tweet.timestamp * 1000,
};
this.client.saveRequestMessage(message, state);
}
// get usernames into str
const validTargetUsersStr =
this.client.twitterConfig.TWITTER_TARGET_USERS.join(",");
const shouldRespondContext = composeContext({
state,
template:
this.runtime.character.templates
?.twitterShouldRespondTemplate ||
this.runtime.character?.templates?.shouldRespondTemplate ||
twitterShouldRespondTemplate(validTargetUsersStr),
});
const shouldRespond = await generateShouldRespond({
runtime: this.runtime,
context: shouldRespondContext,
modelClass: ModelClass.SMALL,
});
// Promise<"RESPOND" | "IGNORE" | "STOP" | null> {
if (shouldRespond !== "RESPOND") {
elizaLogger.log("Not responding to message");
return { text: "Response Decision:", action: shouldRespond };
}
const context = composeContext({
state: {
...state,
// Convert actionNames array to string
actionNames: Array.isArray(state.actionNames)
? state.actionNames.join(', ')
: state.actionNames || '',
actions: Array.isArray(state.actions)
? state.actions.join('\n')
: state.actions || '',
// Ensure character examples are included
characterPostExamples: this.runtime.character.messageExamples
? this.runtime.character.messageExamples
.map(example =>
example.map(msg =>
`${msg.user}: ${msg.content.text}${msg.content.action ? ` [Action: ${msg.content.action}]` : ''}`
).join('\n')
).join('\n\n')
: '',
},
template:
this.runtime.character.templates
?.twitterMessageHandlerTemplate ||
this.runtime.character?.templates?.messageHandlerTemplate ||
twitterMessageHandlerTemplate,
});
const response = await generateMessageResponse({
runtime: this.runtime,
context,
modelClass: ModelClass.LARGE,
});
const removeQuotes = (str: string) =>
str.replace(/^['"](.*)['"]$/, "$1");
const stringId = stringToUuid(tweet.id + "-" + this.runtime.agentId);
response.inReplyTo = stringId;
response.text = removeQuotes(response.text);
if (response.text) {
if (this.isDryRun) {
elizaLogger.info(
`Dry run: Selected Post: ${tweet.id} - ${tweet.username}: ${tweet.text}\nAgent's Output:\n${response.text}`
);
} else {
try {
const callback: HandlerCallback = async (
response: Content,
tweetId?: string
) => {
const memories = await sendTweet(
this.client,
response,
message.roomId,
this.client.twitterConfig.TWITTER_USERNAME,
tweetId || tweet.id
);
return memories;
};
const action = this.runtime.actions.find((a) => a.name === response.action);
const shouldSuppressInitialMessage = action?.suppressInitialMessage;
let responseMessages = [];
if (!shouldSuppressInitialMessage) {
responseMessages = await callback(response);
} else {
responseMessages = [{
id: stringToUuid(tweet.id + "-" + this.runtime.agentId),
userId: this.runtime.agentId,
agentId: this.runtime.agentId,
content: response,
roomId: message.roomId,
embedding: getEmbeddingZeroVector(),
createdAt: Date.now(),
}];
}
state = (await this.runtime.updateRecentMessageState(
state
)) as State;
for (const responseMessage of responseMessages) {
if (
responseMessage ===
responseMessages[responseMessages.length - 1]
) {
responseMessage.content.action = response.action;
} else {
responseMessage.content.action = "CONTINUE";
}
await this.runtime.messageManager.createMemory(
responseMessage
);
}
const responseTweetId =
responseMessages[responseMessages.length - 1]?.content
?.tweetId;
await this.runtime.processActions(
message,
responseMessages,
state,
(response: Content) => {
return callback(response, responseTweetId);
}
);
const responseInfo = `Context:\n\n${context}\n\nSelected Post: ${tweet.id} - ${tweet.username}: ${tweet.text}\nAgent's Output:\n${response.text}`;
await this.runtime.cacheManager.set(
`twitter/tweet_generation_${tweet.id}.txt`,
responseInfo
);
await wait();
} catch (error) {
elizaLogger.error(`Error sending response tweet: ${error}`);
}
}
}
}
async buildConversationThread(
tweet: Tweet,
maxReplies = 10
): Promise<Tweet[]> {
const thread: Tweet[] = [];
const visited: Set<string> = new Set();
async function processThread(currentTweet: Tweet, depth = 0) {
elizaLogger.log("Processing tweet:", {
id: currentTweet.id,
inReplyToStatusId: currentTweet.inReplyToStatusId,
depth: depth,
});
if (!currentTweet) {
elizaLogger.log("No current tweet found for thread building");
return;
}
if (depth >= maxReplies) {
elizaLogger.log("Reached maximum reply depth", depth);
return;
}
// Handle memory storage
const memory = await this.runtime.messageManager.getMemoryById(
stringToUuid(currentTweet.id + "-" + this.runtime.agentId)
);
if (!memory) {
const roomId = stringToUuid(
currentTweet.conversationId + "-" + this.runtime.agentId
);
const userId = stringToUuid(currentTweet.userId);
await this.runtime.ensureConnection(
userId,
roomId,
currentTweet.username,
currentTweet.name,
"twitter"
);
this.runtime.messageManager.createMemory({
id: stringToUuid(
currentTweet.id + "-" + this.runtime.agentId
),
agentId: this.runtime.agentId,
content: {
text: currentTweet.text,
source: "twitter",
url: currentTweet.permanentUrl,
imageUrls: currentTweet.photos?.map(photo => photo.url) || [],
inReplyTo: currentTweet.inReplyToStatusId
? stringToUuid(
currentTweet.inReplyToStatusId +
"-" +
this.runtime.agentId
)
: undefined,
},
createdAt: currentTweet.timestamp * 1000,
roomId,
userId:
currentTweet.userId === this.twitterUserId
? this.runtime.agentId
: stringToUuid(currentTweet.userId),
embedding: getEmbeddingZeroVector(),
});
}
if (visited.has(currentTweet.id)) {
elizaLogger.log("Already visited tweet:", currentTweet.id);
return;
}
visited.add(currentTweet.id);
thread.unshift(currentTweet);
if (currentTweet.inReplyToStatusId) {
elizaLogger.log(
"Fetching parent tweet:",
currentTweet.inReplyToStatusId
);
try {
const parentTweet = await this.twitterClient.getTweet(
currentTweet.inReplyToStatusId
);
if (parentTweet) {
elizaLogger.log("Found parent tweet:", {
id: parentTweet.id,
text: parentTweet.text?.slice(0, 50),
});
await processThread(parentTweet, depth + 1);
} else {
elizaLogger.log(
"No parent tweet found for:",
currentTweet.inReplyToStatusId
);
}
} catch (error) {
elizaLogger.log("Error fetching parent tweet:", {
tweetId: currentTweet.inReplyToStatusId,
error,
});
}
} else {
elizaLogger.log(
"Reached end of reply chain at:",
currentTweet.id
);
}
}
// Need to bind this context for the inner function
await processThread.bind(this)(tweet, 0);
return thread;
}
}