Skip to content

Commit 4137c0f

Browse files
authored
Merge branch 'develop' into fix-types-and-build
2 parents e066dd2 + ef412e8 commit 4137c0f

File tree

1 file changed

+48
-50
lines changed

1 file changed

+48
-50
lines changed

packages/client-telegram/src/messageManager.ts

+48-50
Original file line numberDiff line numberDiff line change
@@ -1051,6 +1051,53 @@ export class MessageManager {
10511051
// Decide whether to respond
10521052
const shouldRespond = await this._shouldRespond(message, state);
10531053

1054+
// Send response in chunks
1055+
const callback: HandlerCallback = async (content: Content) => {
1056+
const sentMessages = await this.sendMessageInChunks(
1057+
ctx,
1058+
content,
1059+
message.message_id
1060+
);
1061+
if (sentMessages) {
1062+
const memories: Memory[] = [];
1063+
1064+
// Create memories for each sent message
1065+
for (let i = 0; i < sentMessages.length; i++) {
1066+
const sentMessage = sentMessages[i];
1067+
const isLastMessage = i === sentMessages.length - 1;
1068+
1069+
const memory: Memory = {
1070+
id: stringToUuid(
1071+
sentMessage.message_id.toString() +
1072+
"-" +
1073+
this.runtime.agentId
1074+
),
1075+
agentId,
1076+
userId: agentId,
1077+
roomId,
1078+
content: {
1079+
...content,
1080+
text: sentMessage.text,
1081+
inReplyTo: messageId,
1082+
},
1083+
createdAt: sentMessage.date * 1000,
1084+
embedding: getEmbeddingZeroVector(),
1085+
};
1086+
1087+
// Set action to CONTINUE for all messages except the last one
1088+
// For the last message, use the original action from the response content
1089+
memory.content.action = !isLastMessage
1090+
? "CONTINUE"
1091+
: content.action;
1092+
1093+
await this.runtime.messageManager.createMemory(memory);
1094+
memories.push(memory);
1095+
}
1096+
1097+
return memories;
1098+
}
1099+
};
1100+
10541101
if (shouldRespond) {
10551102
// Generate response
10561103
const context = composeContext({
@@ -1071,55 +1118,6 @@ export class MessageManager {
10711118

10721119
if (!responseContent || !responseContent.text) return;
10731120

1074-
// Send response in chunks
1075-
const callback: HandlerCallback = async (content: Content) => {
1076-
const sentMessages = await this.sendMessageInChunks(
1077-
ctx,
1078-
content,
1079-
message.message_id
1080-
);
1081-
if (sentMessages) {
1082-
const memories: Memory[] = [];
1083-
1084-
// Create memories for each sent message
1085-
for (let i = 0; i < sentMessages.length; i++) {
1086-
const sentMessage = sentMessages[i];
1087-
const isLastMessage = i === sentMessages.length - 1;
1088-
1089-
const memory: Memory = {
1090-
id: stringToUuid(
1091-
sentMessage.message_id.toString() +
1092-
"-" +
1093-
this.runtime.agentId
1094-
),
1095-
agentId,
1096-
userId: agentId,
1097-
roomId,
1098-
content: {
1099-
...content,
1100-
text: sentMessage.text,
1101-
inReplyTo: messageId,
1102-
},
1103-
createdAt: sentMessage.date * 1000,
1104-
embedding: getEmbeddingZeroVector(),
1105-
};
1106-
1107-
// Set action to CONTINUE for all messages except the last one
1108-
// For the last message, use the original action from the response content
1109-
memory.content.action = !isLastMessage
1110-
? "CONTINUE"
1111-
: content.action;
1112-
1113-
await this.runtime.messageManager.createMemory(
1114-
memory
1115-
);
1116-
memories.push(memory);
1117-
}
1118-
1119-
return memories;
1120-
}
1121-
};
1122-
11231121
// Execute callback to send messages and log memories
11241122
const responseMessages = await callback(responseContent);
11251123

@@ -1135,7 +1133,7 @@ export class MessageManager {
11351133
);
11361134
}
11371135

1138-
await this.runtime.evaluate(memory, state, shouldRespond);
1136+
await this.runtime.evaluate(memory, state, shouldRespond, callback);
11391137
} catch (error) {
11401138
elizaLogger.error("❌ Error handling message:", error);
11411139
elizaLogger.error("Error sending message:", error);

0 commit comments

Comments
 (0)