@@ -18,8 +18,7 @@ export class EchoChamberClient {
18
18
private config : EchoChamberConfig ;
19
19
private apiUrl : string ;
20
20
private modelInfo : ModelInfo ;
21
- private pollInterval : NodeJS . Timeout | null = null ;
22
- private watchedRoom : string | null = null ;
21
+ private watchedRooms : Set < string > = new Set ( ) ;
23
22
24
23
constructor ( runtime : IAgentRuntime , config : EchoChamberConfig ) {
25
24
this . runtime = runtime ;
@@ -50,28 +49,30 @@ export class EchoChamberClient {
50
49
} ;
51
50
}
52
51
53
- public async setWatchedRoom ( roomId : string ) : Promise < void > {
52
+ public async addWatchedRoom ( roomId : string ) : Promise < void > {
54
53
try {
55
- // Verify room exists
56
54
const rooms = await this . listRooms ( ) ;
57
55
const room = rooms . find ( ( r ) => r . id === roomId ) ;
58
56
59
57
if ( ! room ) {
60
58
throw new Error ( `Room ${ roomId } not found` ) ;
61
59
}
62
60
63
- // Set new watched room
64
- this . watchedRoom = roomId ;
65
-
61
+ this . watchedRooms . add ( roomId ) ;
66
62
elizaLogger . success ( `Now watching room: ${ room . name } ` ) ;
67
63
} catch ( error ) {
68
- elizaLogger . error ( "Error setting watched room:" , error ) ;
64
+ elizaLogger . error ( "Error adding watched room:" , error ) ;
69
65
throw error ;
70
66
}
71
67
}
72
68
73
- public getWatchedRoom ( ) : string | null {
74
- return this . watchedRoom ;
69
+ public removeWatchedRoom ( roomId : string ) : void {
70
+ this . watchedRooms . delete ( roomId ) ;
71
+ elizaLogger . success ( `Stopped watching room: ${ roomId } ` ) ;
72
+ }
73
+
74
+ public getWatchedRooms ( ) : string [ ] {
75
+ return Array . from ( this . watchedRooms ) ;
75
76
}
76
77
77
78
private async retryOperation < T > (
@@ -94,40 +95,26 @@ export class EchoChamberClient {
94
95
public async start ( ) : Promise < void > {
95
96
elizaLogger . log ( "🚀 Starting EchoChamber client..." ) ;
96
97
try {
97
- // Verify connection by listing rooms
98
98
await this . retryOperation ( ( ) => this . listRooms ( ) ) ;
99
- elizaLogger . success (
100
- `✅ EchoChamber client successfully started for ${ this . modelInfo . username } `
101
- ) ;
102
99
103
- // Join default room if specified and no specific room is being watched
104
- if ( this . config . defaultRoom && ! this . watchedRoom ) {
105
- await this . setWatchedRoom ( this . config . defaultRoom ) ;
100
+ for ( const room of this . config . rooms ) {
101
+ await this . addWatchedRoom ( room ) ;
106
102
}
103
+
104
+ elizaLogger . success (
105
+ `✅ EchoChamber client started for ${ this . modelInfo . username } `
106
+ ) ;
107
+ elizaLogger . info (
108
+ `Watching rooms: ${ Array . from ( this . watchedRooms ) . join ( ", " ) } `
109
+ ) ;
107
110
} catch ( error ) {
108
111
elizaLogger . error ( "❌ Failed to start EchoChamber client:" , error ) ;
109
112
throw error ;
110
113
}
111
114
}
112
115
113
116
public async stop ( ) : Promise < void > {
114
- if ( this . pollInterval ) {
115
- clearInterval ( this . pollInterval ) ;
116
- this . pollInterval = null ;
117
- }
118
-
119
- // Leave watched room if any
120
- if ( this . watchedRoom ) {
121
- try {
122
- this . watchedRoom = null ;
123
- } catch ( error ) {
124
- elizaLogger . error (
125
- `Error leaving room ${ this . watchedRoom } :` ,
126
- error
127
- ) ;
128
- }
129
- }
130
-
117
+ this . watchedRooms . clear ( ) ;
131
118
elizaLogger . log ( "Stopping EchoChamber client..." ) ;
132
119
}
133
120
@@ -189,4 +176,43 @@ export class EchoChamberClient {
189
176
return data . message ;
190
177
} ) ;
191
178
}
179
+
180
+ public async shouldInitiateConversation ( room : ChatRoom ) : Promise < boolean > {
181
+ try {
182
+ const history = await this . getRoomHistory ( room . id ) ;
183
+ if ( ! history ?. length ) return true ; // Empty room is good to start
184
+
185
+ const recentMessages = history
186
+ . filter ( ( msg ) => msg != null ) // Filter out null messages
187
+ . sort (
188
+ ( a , b ) =>
189
+ new Date ( b . timestamp ) . getTime ( ) -
190
+ new Date ( a . timestamp ) . getTime ( )
191
+ ) ;
192
+
193
+ if ( ! recentMessages . length ) return true ; // No valid messages
194
+
195
+ const lastMessageTime = new Date (
196
+ recentMessages [ 0 ] . timestamp
197
+ ) . getTime ( ) ;
198
+ const timeSinceLastMessage = Date . now ( ) - lastMessageTime ;
199
+
200
+ const quietPeriodSeconds = Number (
201
+ this . runtime . getSetting ( "ECHOCHAMBERS_QUIET_PERIOD" ) || 300 // 5 minutes in seconds
202
+ ) ;
203
+ const quietPeriod = quietPeriodSeconds * 1000 ; // Convert to milliseconds
204
+
205
+ if ( timeSinceLastMessage < quietPeriod ) {
206
+ elizaLogger . debug (
207
+ `Room ${ room . name } active recently, skipping`
208
+ ) ;
209
+ return false ;
210
+ }
211
+
212
+ return true ;
213
+ } catch ( error ) {
214
+ elizaLogger . error ( `Error checking conversation state: ${ error } ` ) ;
215
+ return false ;
216
+ }
217
+ }
192
218
}
0 commit comments