forked from elizaOS/eliza
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmessageManager.ts
520 lines (423 loc) · 16.9 KB
/
messageManager.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
import { Message } from "@telegraf/types";
import { Context, Telegraf, Input } from "telegraf";
import { composeContext, elizaLogger, ServiceType } from "@ai16z/eliza";
import { embeddingZeroVector } from "@ai16z/eliza";
import { Media } from "@ai16z/eliza";
import { elizaLogger } from "@ai16z/eliza";
import {
Content,
HandlerCallback,
IAgentRuntime,
IImageDescriptionService,
Memory,
ModelClass,
State,
UUID,
} from "@ai16z/eliza";
import { stringToUuid } from "@ai16z/eliza";
import { generateMessageResponse, generateShouldRespond } from "@ai16z/eliza";
import { messageCompletionFooter, shouldRespondFooter } from "@ai16z/eliza";
const MAX_MESSAGE_LENGTH = 4096; // Telegram's max message length
const telegramShouldRespondTemplate =
`# About {{agentName}}:
{{bio}}
# RESPONSE EXAMPLES
{{user1}}: I just saw a really great movie
{{user2}}: Oh? Which movie?
Result: [IGNORE]
{{agentName}}: Oh, this is my favorite scene
{{user1}}: sick
{{user2}}: wait, why is it your favorite scene
Result: [RESPOND]
{{user1}}: stfu bot
Result: [STOP]
{{user1}}: Hey {{agent}}, can you help me with something
Result: [RESPOND]
{{user1}}: {{agentName}} stfu plz
Result: [STOP]
{{user1}}: i need help
{{agentName}}: how can I help you?
{{user1}}: no. i need help from someone else
Result: [IGNORE]
{{user1}}: Hey {{agent}}, can I ask you a question
{{agentName}}: Sure, what is it
{{user1}}: can you ask claude to create a basic react module that demonstrates a counter
Result: [RESPOND]
{{user1}}: {{agentName}} can you tell me a story
{{agentName}}: uhhh...
{{user1}}: please do it
{{agentName}}: okay
{{agentName}}: once upon a time, in a quaint little village, there was a curious girl named elara
{{user1}}: I'm loving it, keep going
Result: [RESPOND]
{{user1}}: {{agentName}} stop responding plz
Result: [STOP]
{{user1}}: okay, i want to test something. {{agentName}}, can you say marco?
{{agentName}}: marco
{{user1}}: great. okay, now do it again
Result: [RESPOND]
Response options are [RESPOND], [IGNORE] and [STOP].
{{agentName}} is in a room with other users and should only respond when they are being addressed, and should not respond if they are continuing a conversation that is very long.
Respond with [RESPOND] to messages that are directed at {{agentName}}, or participate in conversations that are interesting or relevant to their background.
If a message is not interesting, relevant, or does not directly address {{agentName}}, respond with [IGNORE]
Also, respond with [IGNORE] to messages that are very short or do not contain much information.
If a user asks {{agentName}} to be quiet, respond with [STOP]
If {{agentName}} concludes a conversation and isn't part of the conversation anymore, respond with [STOP]
IMPORTANT: {{agentName}} is particularly sensitive about being annoying, so if there is any doubt, it is better to respond with [IGNORE].
If {{agentName}} is conversing with a user and they have not asked to stop, it is better to respond with [RESPOND].
The goal is to decide whether {{agentName}} should respond to the last message.
{{recentMessages}}
Thread of Tweets You Are Replying To:
{{formattedConversation}}
# INSTRUCTIONS: Choose the option that best describes {{agentName}}'s response to the last message. Ignore messages if they are addressed to someone else.
` + shouldRespondFooter;
const telegramMessageHandlerTemplate =
// {{goals}}
`# Action Examples
{{actionExamples}}
(Action examples are for reference only. Do not use the information from them in your response.)
# Knowledge
{{knowledge}}
# Task: Generate dialog and actions for the character {{agentName}}.
About {{agentName}}:
{{bio}}
{{lore}}
Examples of {{agentName}}'s dialog and actions:
{{characterMessageExamples}}
{{providers}}
{{attachments}}
{{actions}}
# Capabilities
Note that {{agentName}} is capable of reading/seeing/hearing various forms of media, including images, videos, audio, plaintext and PDFs. Recent attachments have been included above under the "Attachments" section.
{{messageDirections}}
{{recentMessages}}
# Task: Generate a reply in the voice and style of {{agentName}}, aka @{{twitterUserName}}
Write a very short reply that is from the perspective of {{agentName}}. Try to write something totally different than previous posts. Do not add commentary or acknowledge this request, just write the reply. Use the thread of tweets as additional context:
Current Post:
{{currentPost}}
Thread of messages you are replying to:
{{formattedConversation}}
` + messageCompletionFooter;
export class MessageManager {
public bot: Telegraf<Context>;
private runtime: IAgentRuntime;
constructor(bot: Telegraf<Context>, runtime: IAgentRuntime) {
this.bot = bot;
this.runtime = runtime;
}
// Process image messages and generate descriptions
private async processImage(
message: Message
): Promise<{ description: string } | null> {
try {
let imageUrl: string | null = null;
if ("photo" in message && message.photo?.length > 0) {
const photo = message.photo[message.photo.length - 1];
const fileLink = await this.bot.telegram.getFileLink(
photo.file_id
);
imageUrl = fileLink.toString();
} else if (
"document" in message &&
message.document?.mime_type?.startsWith("image/")
) {
const fileLink = await this.bot.telegram.getFileLink(
message.document.file_id
);
imageUrl = fileLink.toString();
}
if (imageUrl) {
const imageDescriptionService =
this.runtime.getService<IImageDescriptionService>(
ServiceType.IMAGE_DESCRIPTION
);
const { title, description } =
await imageDescriptionService.describeImage(imageUrl);
return { description: `[Image: ${title}\n${description}]` };
}
} catch (error) {
console.error("❌ Error processing image:", error);
}
return null;
}
// Decide if the bot should respond to the message
private async _shouldRespond(
message: Message,
state: State
): Promise<boolean> {
// Respond if bot is mentioned
if (
"text" in message &&
message.text?.includes(`@${this.bot.botInfo?.username}`)
) {
return true;
}
// Respond to private chats
if (message.chat.type === "private") {
return true;
}
// Don't respond to images in group chats
if (
"photo" in message ||
("document" in message &&
message.document?.mime_type?.startsWith("image/"))
) {
return false;
}
// Use AI to decide for text or captions
if ("text" in message || ("caption" in message && message.caption)) {
const shouldRespondContext = composeContext({
state,
template:
this.runtime.character.templates
?.telegramShouldRespondTemplate ||
this.runtime.character?.templates?.shouldRespondTemplate ||
telegramShouldRespondTemplate,
});
const response = await generateShouldRespond({
runtime: this.runtime,
context: shouldRespondContext,
modelClass: ModelClass.SMALL,
});
return response === "RESPOND";
}
return false;
}
// Send long messages in chunks
private async sendMessageInChunks(
ctx: Context,
content: string,
attachments?: Media[],
replyToMessageId?: number
): Promise<Message.TextMessage[]> {
const chunks = this.splitMessage(content);
const sentMessages: Message.TextMessage[] = [];
const hasAttachment = attachments?.length > 0;
if (hasAttachment) {
const sentMessage = (await ctx.replyWithPhoto(Input.fromLocalFile(attachments[0].url)));
elizaLogger.log("Sent attachment: ", sentMessage);
}
for (let i = 0; i < chunks.length; i++) {
const chunk = chunks[i];
const sentMessage = (await ctx.telegram.sendMessage(
ctx.chat.id,
chunk,
{
reply_parameters:
i === 0 && replyToMessageId
? { message_id: replyToMessageId }
: undefined,
}
)) as Message.TextMessage;
sentMessages.push(sentMessage);
}
return sentMessages;
}
// Split message into smaller parts
private splitMessage(text: string): string[] {
const chunks: string[] = [];
let currentChunk = "";
const lines = text.split("\n");
for (const line of lines) {
if (currentChunk.length + line.length + 1 <= MAX_MESSAGE_LENGTH) {
currentChunk += (currentChunk ? "\n" : "") + line;
} else {
if (currentChunk) chunks.push(currentChunk);
currentChunk = line;
}
}
if (currentChunk) chunks.push(currentChunk);
return chunks;
}
// Generate a response using AI
private async _generateResponse(
message: Memory,
_state: State,
context: string
): Promise<Content> {
const { userId, roomId } = message;
const response = await generateMessageResponse({
runtime: this.runtime,
context,
modelClass: ModelClass.SMALL,
});
if (!response) {
console.error("❌ No response from generateMessageResponse");
return null;
}
await this.runtime.databaseAdapter.log({
body: { message, context, response },
userId,
roomId,
type: "response",
});
return response;
}
// Main handler for incoming messages
public async handleMessage(ctx: Context): Promise<void> {
if (!ctx.message || !ctx.from) {
return; // Exit if no message or sender info
}
if (
this.runtime.character.clientConfig?.telegram
?.shouldIgnoreBotMessages &&
ctx.from.is_bot
) {
return;
}
if (
this.runtime.character.clientConfig?.telegram
?.shouldIgnoreDirectMessages &&
ctx.chat?.type === "private"
) {
return;
}
const message = ctx.message;
try {
// Convert IDs to UUIDs
const userId = stringToUuid(ctx.from.id.toString()) as UUID;
// Get user name
const userName =
ctx.from.username || ctx.from.first_name || "Unknown User";
// Get chat ID
const chatId = stringToUuid(
ctx.chat?.id.toString() + "-" + this.runtime.agentId
) as UUID;
// Get agent ID
const agentId = this.runtime.agentId;
// Get room ID
const roomId = chatId;
// Ensure connection
await this.runtime.ensureConnection(
userId,
roomId,
userName,
userName,
"telegram"
);
// Get message ID
const messageId = stringToUuid(
message.message_id.toString() + "-" + this.runtime.agentId
) as UUID;
// Handle images
const imageInfo = await this.processImage(message);
// Get text or caption
let messageText = "";
if ("text" in message) {
messageText = message.text;
} else if ("caption" in message && message.caption) {
messageText = message.caption;
}
// Combine text and image description
const fullText = imageInfo
? `${messageText} ${imageInfo.description}`
: messageText;
if (!fullText) {
return; // Skip if no content
}
// Create content
const content: Content = {
text: fullText,
source: "telegram",
inReplyTo:
"reply_to_message" in message && message.reply_to_message
? stringToUuid(
message.reply_to_message.message_id.toString() +
"-" +
this.runtime.agentId
)
: undefined,
};
// Create memory for the message
const memory: Memory = {
id: messageId,
agentId,
userId,
roomId,
content,
createdAt: message.date * 1000,
embedding: embeddingZeroVector,
};
// Create memory
await this.runtime.messageManager.createMemory(memory);
// Update state with the new memory
let state = await this.runtime.composeState(memory);
state = await this.runtime.updateRecentMessageState(state);
// Decide whether to respond
const shouldRespond = await this._shouldRespond(message, state);
if (shouldRespond) {
// Generate response
const context = composeContext({
state,
template:
this.runtime.character.templates
?.telegramMessageHandlerTemplate ||
this.runtime.character?.templates
?.messageHandlerTemplate ||
telegramMessageHandlerTemplate,
});
const responseContent = await this._generateResponse(
memory,
state,
context
);
if (!responseContent || !responseContent.text) return;
// Send response in chunks
const callback: HandlerCallback = async (content: Content) => {
const sentMessages = await this.sendMessageInChunks(
ctx,
content.text,
content.attachments,
message.message_id
);
const memories: Memory[] = [];
// Create memories for each sent message
for (let i = 0; i < sentMessages.length; i++) {
const sentMessage = sentMessages[i];
const isLastMessage = i === sentMessages.length - 1;
const memory: Memory = {
id: stringToUuid(
sentMessage.message_id.toString() +
"-" +
this.runtime.agentId
),
agentId,
userId,
roomId,
content: {
...content,
text: sentMessage.text,
inReplyTo: messageId,
},
createdAt: sentMessage.date * 1000,
embedding: embeddingZeroVector,
};
// Set action to CONTINUE for all messages except the last one
// For the last message, use the original action from the response content
memory.content.action = !isLastMessage
? "CONTINUE"
: content.action;
await this.runtime.messageManager.createMemory(memory);
memories.push(memory);
}
return memories;
};
// Execute callback to send messages and log memories
const responseMessages = await callback(responseContent);
// Update state after response
state = await this.runtime.updateRecentMessageState(state);
// Handle any resulting actions
await this.runtime.processActions(
memory,
responseMessages,
state,
callback
);
}
await this.runtime.evaluate(memory, state, shouldRespond);
} catch (error) {
elizaLogger.error("❌ Error handling message:", error);
elizaLogger.error("Error sending message:", error);
}
}
}