-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconversation_manager.py
More file actions
456 lines (372 loc) · 16.7 KB
/
conversation_manager.py
File metadata and controls
456 lines (372 loc) · 16.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
import logging
import asyncio
import time
from typing import Dict, List, Any, Optional
from datetime import datetime
# Set up logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger("conversation_manager")
# Try to import dashboard integration
try:
import dashboard_integration
HAS_DASHBOARD = True
except ImportError:
HAS_DASHBOARD = False
logger.warning("Dashboard integration not available. Conversations will not be shown on dashboard.")
class ConversationManager:
"""
Manages conversations between agents, tracks conversation state,
and enforces conversation policies like maximum rounds.
This is a central component that:
1. Initiates and tracks conversations between agents
2. Limits conversation rounds
3. Ensures conversations display on the dashboard
4. Forwards conversation messages to the correct agents
"""
def __init__(self, session_manager=None, max_rounds: int = 3):
"""
Initialize the conversation manager.
Args:
session_manager: Reference to the AgentSessionManager for generating responses
max_rounds: Maximum number of conversation rounds before terminating
"""
self.session_manager = session_manager
self.max_rounds = max_rounds
# Store active conversations
# Key: conversation_id (composite of participant IDs)
# Value: conversation state (participants, messages, etc.)
self.active_conversations: Dict[str, Dict[str, Any]] = {}
# Track agent participation in conversations
# Key: agent_id
# Value: conversation_id
self.agent_conversations: Dict[str, str] = {}
# Message queue for each agent
self.message_queues: Dict[str, List[Dict[str, Any]]] = {}
def get_conversation_id(self, agent_a: str, agent_b: str) -> str:
"""
Generate a consistent conversation ID for any two agents.
Args:
agent_a: First agent ID
agent_b: Second agent ID
Returns:
Conversation ID string
"""
# Sort agent IDs to ensure the same conversation ID regardless of order
participants = sorted([agent_a, agent_b])
return f"conversation_{participants[0]}_{participants[1]}"
def is_agent_in_conversation(self, agent_id: str) -> bool:
"""
Check if an agent is currently in a conversation.
Args:
agent_id: Agent ID to check
Returns:
True if agent is in a conversation, False otherwise
"""
return agent_id in self.agent_conversations
def get_agent_conversation(self, agent_id: str) -> Optional[Dict[str, Any]]:
"""
Get the active conversation an agent is participating in.
Args:
agent_id: Agent ID to check
Returns:
Conversation data or None if not in a conversation
"""
if agent_id not in self.agent_conversations:
return None
conversation_id = self.agent_conversations[agent_id]
return self.active_conversations.get(conversation_id)
async def start_conversation(self, initiator_id: str, target_id: str) -> Dict[str, Any]:
"""
Start a new conversation between two agents.
Args:
initiator_id: ID of the initiating agent
target_id: ID of the target agent
Returns:
Dictionary with conversation status and details
"""
# Check if either agent is already in a conversation
if initiator_id in self.agent_conversations:
return {
"status": "error",
"error": f"Agent {initiator_id} is already in a conversation",
"conversation_id": self.agent_conversations[initiator_id]
}
if target_id in self.agent_conversations:
return {
"status": "error",
"error": f"Agent {target_id} is already in a conversation",
"conversation_id": self.agent_conversations[target_id]
}
# Generate conversation ID
conversation_id = self.get_conversation_id(initiator_id, target_id)
# Create conversation state
conversation = {
"id": conversation_id,
"participants": [initiator_id, target_id],
"start_time": datetime.now().isoformat(),
"last_activity": datetime.now().isoformat(),
"rounds": 0,
"messages": [],
"status": "active"
}
# Store conversation
self.active_conversations[conversation_id] = conversation
# Link agents to this conversation
self.agent_conversations[initiator_id] = conversation_id
self.agent_conversations[target_id] = conversation_id
# Initialize message queues if not existing
if initiator_id not in self.message_queues:
self.message_queues[initiator_id] = []
if target_id not in self.message_queues:
self.message_queues[target_id] = []
# Create initial message (system notification)
system_message = {
"conversation_id": conversation_id,
"sender": "system",
"receiver": None, # System message visible to both
"content": f"Conversation started between {initiator_id} and {target_id}",
"timestamp": datetime.now().isoformat(),
"round": 0
}
# Add to conversation history
conversation["messages"].append(system_message)
# Send to dashboard if available
if HAS_DASHBOARD:
try:
# Send system message to dashboard for both agents
dashboard_integration.record_agent_message(
initiator_id,
f"[System] Started conversation with {target_id}",
is_from_agent=False
)
dashboard_integration.record_agent_message(
target_id,
f"[System] {initiator_id} initiated a conversation with you",
is_from_agent=False
)
# Update agent states to show they're in conversation
dashboard_integration.update_agent_state(
initiator_id,
{"status": f"Conversing with {target_id}"}
)
dashboard_integration.update_agent_state(
target_id,
{"status": f"Conversing with {initiator_id}"}
)
except Exception as e:
logger.error(f"Error updating dashboard: {e}")
logger.info(f"Started conversation {conversation_id} between {initiator_id} and {target_id}")
return {
"status": "success",
"conversation_id": conversation_id,
"message": f"Conversation started between {initiator_id} and {target_id}"
}
async def add_message(self, sender_id: str, content: str) -> Dict[str, Any]:
"""
Add a message to an active conversation.
Args:
sender_id: ID of the sending agent
content: Message content
Returns:
Dictionary with status and details
"""
# Check if agent is in a conversation
if sender_id not in self.agent_conversations:
return {
"status": "error",
"error": f"Agent {sender_id} is not in a conversation"
}
# Get conversation
conversation_id = self.agent_conversations[sender_id]
conversation = self.active_conversations[conversation_id]
# Determine the receiver
receiver_id = next((p for p in conversation["participants"] if p != sender_id), None)
if not receiver_id:
return {
"status": "error",
"error": "Could not determine message receiver"
}
# Check if conversation has reached max rounds
current_round = conversation["rounds"]
# Create message object
message = {
"conversation_id": conversation_id,
"sender": sender_id,
"receiver": receiver_id,
"content": content,
"timestamp": datetime.now().isoformat(),
"round": current_round
}
# Add to conversation history
conversation["messages"].append(message)
conversation["last_activity"] = datetime.now().isoformat()
# Queue message for receiver
self.message_queues[receiver_id].append(message)
# Send to dashboard if available
if HAS_DASHBOARD:
try:
# Record message in both agents' history for dashboard visibility
# This is the key step that ensures conversations appear on dashboard
dashboard_integration.record_agent_message(
sender_id,
f"[To {receiver_id}] {content}",
is_from_agent=True
)
dashboard_integration.record_agent_message(
receiver_id,
f"[From {sender_id}] {content}",
is_from_agent=False
)
except Exception as e:
logger.error(f"Error updating dashboard with conversation: {e}")
# Check if we've completed a round (both participants have sent a message)
sender_messages = [m for m in conversation["messages"]
if m["sender"] == sender_id and m["round"] == current_round]
receiver_messages = [m for m in conversation["messages"]
if m["sender"] == receiver_id and m["round"] == current_round]
# If both have sent messages in this round, increment the round counter
if sender_messages and receiver_messages:
conversation["rounds"] += 1
# Check if we've reached max rounds
if conversation["rounds"] >= self.max_rounds:
# End conversation after max rounds
await self.end_conversation(conversation_id, f"Reached maximum of {self.max_rounds} conversation rounds")
logger.info(f"Added message to conversation {conversation_id} from {sender_id} to {receiver_id}")
return {
"status": "success",
"conversation_id": conversation_id,
"current_round": conversation["rounds"]
}
async def end_conversation(self, conversation_id: str, reason: str = "Conversation ended") -> Dict[str, Any]:
"""
End an active conversation.
Args:
conversation_id: ID of the conversation to end
reason: Reason for ending the conversation
Returns:
Dictionary with status and details
"""
# Check if conversation exists
if conversation_id not in self.active_conversations:
return {
"status": "error",
"error": f"Conversation {conversation_id} not found"
}
# Get conversation
conversation = self.active_conversations[conversation_id]
participants = conversation["participants"]
# Create end message
end_message = {
"conversation_id": conversation_id,
"sender": "system",
"receiver": None, # System message visible to both
"content": reason,
"timestamp": datetime.now().isoformat(),
"round": conversation["rounds"]
}
# Add to conversation history
conversation["messages"].append(end_message)
conversation["end_time"] = datetime.now().isoformat()
conversation["status"] = "ended"
conversation["end_reason"] = reason
# Remove agent-to-conversation links
for agent_id in participants:
if agent_id in self.agent_conversations:
del self.agent_conversations[agent_id]
# Send to dashboard if available
if HAS_DASHBOARD:
try:
# Notify both participants on dashboard
for agent_id in participants:
dashboard_integration.record_agent_message(
agent_id,
f"[System] {reason}",
is_from_agent=False
)
# Update agent state to show they're no longer in conversation
dashboard_integration.update_agent_state(
agent_id,
{"status": "Idle"}
)
except Exception as e:
logger.error(f"Error updating dashboard: {e}")
logger.info(f"Ended conversation {conversation_id}: {reason}")
return {
"status": "success",
"conversation_id": conversation_id,
"message": reason
}
async def get_next_message(self, agent_id: str) -> Optional[Dict[str, Any]]:
"""
Get the next message for an agent from their queue.
Args:
agent_id: Agent ID
Returns:
Next message or None if queue is empty
"""
if agent_id not in self.message_queues or not self.message_queues[agent_id]:
return None
# Get and remove the first message from the queue
return self.message_queues[agent_id].pop(0)
async def get_conversation_history(self, conversation_id: str) -> List[Dict[str, Any]]:
"""
Get the full history of a conversation.
Args:
conversation_id: Conversation ID
Returns:
List of message objects
"""
if conversation_id not in self.active_conversations:
return []
return self.active_conversations[conversation_id]["messages"]
async def get_agent_conversations(self, agent_id: str) -> List[Dict[str, Any]]:
"""
Get all conversations an agent has participated in.
Args:
agent_id: Agent ID
Returns:
List of conversation objects
"""
# Return active conversation if agent is in one
if agent_id in self.agent_conversations:
conversation_id = self.agent_conversations[agent_id]
if conversation_id in self.active_conversations:
return [self.active_conversations[conversation_id]]
# Find all historical conversations involving this agent
return [
conv for conv in self.active_conversations.values()
if agent_id in conv["participants"]
]
async def cleanup_stale_conversations(self, max_idle_time: int = 300) -> None:
"""
End conversations that have been idle for too long.
Args:
max_idle_time: Maximum idle time in seconds before ending a conversation
"""
current_time = datetime.now()
for conversation_id, conversation in list(self.active_conversations.items()):
if conversation["status"] != "active":
continue
# Check last activity time
last_activity = datetime.fromisoformat(conversation["last_activity"])
idle_seconds = (current_time - last_activity).total_seconds()
if idle_seconds > max_idle_time:
# End conversation due to inactivity
await self.end_conversation(
conversation_id,
f"Conversation ended due to inactivity ({int(idle_seconds)} seconds)"
)
# Example usage in dashboard_integration.py:
"""
from conversation_manager import ConversationManager
# Initialize the conversation manager with the session manager
conversation_manager = ConversationManager(session_manager, max_rounds=3)
# When a CONVERSE action is received, start a conversation
async def handle_converse_action(agent_id, target_agent_id):
result = await conversation_manager.start_conversation(agent_id, target_agent_id)
return result
# When an agent wants to send a message in a conversation
async def send_conversation_message(agent_id, message):
result = await conversation_manager.add_message(agent_id, message)
return result
"""