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

fix: add callback to the evaluators for client-telegram #1908

Merged
Changes from all commits
Commits
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
98 changes: 48 additions & 50 deletions packages/client-telegram/src/messageManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1051,6 +1051,53 @@ export class MessageManager {
// Decide whether to respond
const shouldRespond = await this._shouldRespond(message, state);

// Send response in chunks
const callback: HandlerCallback = async (content: Content) => {
const sentMessages = await this.sendMessageInChunks(
ctx,
content,
message.message_id
);
if (sentMessages) {
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: agentId,
roomId,
content: {
...content,
text: sentMessage.text,
inReplyTo: messageId,
},
createdAt: sentMessage.date * 1000,
embedding: getEmbeddingZeroVector(),
};

// 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;
}
};

if (shouldRespond) {
// Generate response
const context = composeContext({
Expand All @@ -1071,55 +1118,6 @@ export class MessageManager {

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

// Send response in chunks
const callback: HandlerCallback = async (content: Content) => {
const sentMessages = await this.sendMessageInChunks(
ctx,
content,
message.message_id
);
if (sentMessages) {
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: agentId,
roomId,
content: {
...content,
text: sentMessage.text,
inReplyTo: messageId,
},
createdAt: sentMessage.date * 1000,
embedding: getEmbeddingZeroVector(),
};

// 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);

Expand All @@ -1135,7 +1133,7 @@ export class MessageManager {
);
}

await this.runtime.evaluate(memory, state, shouldRespond);
await this.runtime.evaluate(memory, state, shouldRespond, callback);
} catch (error) {
elizaLogger.error("❌ Error handling message:", error);
elizaLogger.error("Error sending message:", error);
Expand Down
Loading