Skip to content

Commit c79f5a4

Browse files
committed
Update interactions.ts
1 parent f1a6c2b commit c79f5a4

File tree

1 file changed

+48
-18
lines changed

1 file changed

+48
-18
lines changed

packages/plugin-echochambers/src/interactions.ts

+48-18
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,10 @@ export class InteractionClient {
106106
private lastCheckedTimestamps: Map<string, string> = new Map();
107107
private lastResponseTimes: Map<string, number> = new Map();
108108
private messageThreads: Map<string, ChatMessage[]> = new Map();
109+
private messageHistory: Map<
110+
string,
111+
{ message: ChatMessage; response: ChatMessage | null }[]
112+
> = new Map();
109113
private pollInterval: NodeJS.Timeout | null = null;
110114

111115
constructor(client: EchoChamberClient, runtime: IAgentRuntime) {
@@ -197,12 +201,12 @@ export class InteractionClient {
197201
// Check if message mentions the agent
198202
const isMentioned = message.content
199203
.toLowerCase()
200-
.includes(`@${modelInfo.username.toLowerCase()}`);
204+
.includes(`${modelInfo.username.toLowerCase()}`);
201205

202206
// Check if message is relevant to room topic
203-
const isRelevantToTopic = message.content
204-
.toLowerCase()
205-
.includes(room.topic.toLowerCase());
207+
const isRelevantToTopic =
208+
room.topic &&
209+
message.content.toLowerCase().includes(room.topic.toLowerCase());
206210

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

214218
try {
219+
const defaultRoom = this.runtime.getSetting(
220+
"ECHOCHAMBERS_DEFAULT_ROOM"
221+
);
215222
const rooms = await this.client.listRooms();
216223

217224
for (const room of rooms) {
218-
const messages = await this.client.getRoomHistory(room.id);
225+
// Only process messages from the default room if specified
226+
if (defaultRoom && room.id !== defaultRoom) {
227+
continue;
228+
}
219229

220-
// Update message threads for the room
230+
const messages = await this.client.getRoomHistory(room.id);
221231
this.messageThreads.set(room.id, messages);
222232

223-
// Filter and process new messages
224-
const newMessages = messages.filter((msg) =>
225-
this.shouldProcessMessage(msg, room)
226-
);
227-
228-
// Process each new message
229-
for (const message of newMessages) {
230-
await this.handleMessage(message, room.topic);
231-
232-
// Update timestamps
233+
// Get only the most recent message that we should process
234+
const latestMessages = messages
235+
.filter((msg) => !this.shouldProcessMessage(msg, room)) // Fixed: Now filtering out messages we shouldn't process
236+
.sort(
237+
(a, b) =>
238+
new Date(b.timestamp).getTime() -
239+
new Date(a.timestamp).getTime()
240+
);
241+
242+
if (latestMessages.length > 0) {
243+
const latestMessage = latestMessages[0];
244+
await this.handleMessage(latestMessage, room.topic);
245+
246+
// Update history
247+
const roomHistory = this.messageHistory.get(room.id) || [];
248+
roomHistory.push({
249+
message: latestMessage,
250+
response: null, // Will be updated when we respond
251+
});
252+
this.messageHistory.set(room.id, roomHistory);
253+
254+
// Update last checked timestamp
233255
if (
234-
message.timestamp >
256+
latestMessage.timestamp >
235257
(this.lastCheckedTimestamps.get(room.id) || "0")
236258
) {
237259
this.lastCheckedTimestamps.set(
238260
room.id,
239-
message.timestamp
261+
latestMessage.timestamp
240262
);
241263
}
242264
}
@@ -358,6 +380,14 @@ export class InteractionClient {
358380
// Update last response time
359381
this.lastResponseTimes.set(message.roomId, Date.now());
360382

383+
// Update history with our response
384+
const roomHistory =
385+
this.messageHistory.get(message.roomId) || [];
386+
const lastEntry = roomHistory[roomHistory.length - 1];
387+
if (lastEntry && lastEntry.message.id === message.id) {
388+
lastEntry.response = sentMessage;
389+
}
390+
361391
const responseMemory: Memory = {
362392
id: stringToUuid(sentMessage.id),
363393
userId: this.runtime.agentId,

0 commit comments

Comments
 (0)