Skip to content

Commit

Permalink
Update interactions.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
savageops committed Dec 12, 2024
1 parent f1a6c2b commit c79f5a4
Showing 1 changed file with 48 additions and 18 deletions.
66 changes: 48 additions & 18 deletions packages/plugin-echochambers/src/interactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@ export class InteractionClient {
private lastCheckedTimestamps: Map<string, string> = new Map();
private lastResponseTimes: Map<string, number> = new Map();
private messageThreads: Map<string, ChatMessage[]> = new Map();
private messageHistory: Map<
string,
{ message: ChatMessage; response: ChatMessage | null }[]
> = new Map();
private pollInterval: NodeJS.Timeout | null = null;

constructor(client: EchoChamberClient, runtime: IAgentRuntime) {
Expand Down Expand Up @@ -197,12 +201,12 @@ export class InteractionClient {
// Check if message mentions the agent
const isMentioned = message.content
.toLowerCase()
.includes(`@${modelInfo.username.toLowerCase()}`);
.includes(`${modelInfo.username.toLowerCase()}`);

// Check if message is relevant to room topic
const isRelevantToTopic = message.content
.toLowerCase()
.includes(room.topic.toLowerCase());
const isRelevantToTopic =
room.topic &&
message.content.toLowerCase().includes(room.topic.toLowerCase());

// Always process if mentioned, otherwise check relevance
return isMentioned || isRelevantToTopic;
Expand All @@ -212,31 +216,49 @@ export class InteractionClient {
elizaLogger.log("Checking EchoChambers interactions");

try {
const defaultRoom = this.runtime.getSetting(
"ECHOCHAMBERS_DEFAULT_ROOM"
);
const rooms = await this.client.listRooms();

for (const room of rooms) {
const messages = await this.client.getRoomHistory(room.id);
// Only process messages from the default room if specified
if (defaultRoom && room.id !== defaultRoom) {
continue;
}

// Update message threads for the room
const messages = await this.client.getRoomHistory(room.id);
this.messageThreads.set(room.id, messages);

// Filter and process new messages
const newMessages = messages.filter((msg) =>
this.shouldProcessMessage(msg, room)
);

// Process each new message
for (const message of newMessages) {
await this.handleMessage(message, room.topic);

// Update timestamps
// Get only the most recent message that we should process
const latestMessages = messages
.filter((msg) => !this.shouldProcessMessage(msg, room)) // Fixed: Now filtering out messages we shouldn't process
.sort(
(a, b) =>
new Date(b.timestamp).getTime() -
new Date(a.timestamp).getTime()
);

if (latestMessages.length > 0) {
const latestMessage = latestMessages[0];
await this.handleMessage(latestMessage, room.topic);

// Update history
const roomHistory = this.messageHistory.get(room.id) || [];
roomHistory.push({
message: latestMessage,
response: null, // Will be updated when we respond
});
this.messageHistory.set(room.id, roomHistory);

// Update last checked timestamp
if (
message.timestamp >
latestMessage.timestamp >
(this.lastCheckedTimestamps.get(room.id) || "0")
) {
this.lastCheckedTimestamps.set(
room.id,
message.timestamp
latestMessage.timestamp
);
}
}
Expand Down Expand Up @@ -358,6 +380,14 @@ export class InteractionClient {
// Update last response time
this.lastResponseTimes.set(message.roomId, Date.now());

// Update history with our response
const roomHistory =
this.messageHistory.get(message.roomId) || [];
const lastEntry = roomHistory[roomHistory.length - 1];
if (lastEntry && lastEntry.message.id === message.id) {
lastEntry.response = sentMessage;
}

const responseMemory: Memory = {
id: stringToUuid(sentMessage.id),
userId: this.runtime.agentId,
Expand Down

0 comments on commit c79f5a4

Please sign in to comment.