-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchat_server_enhanced.py
More file actions
394 lines (315 loc) · 11.6 KB
/
Copy pathchat_server_enhanced.py
File metadata and controls
394 lines (315 loc) · 11.6 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
"""Enhanced chat server with modern UI and multi-session support.
Features:
- React-based modern UI
- Multi-session management
- Chat history with export (Markdown/PDF)
- Real-time streaming
- Session persistence
"""
from flask import Flask, render_template, request, jsonify, send_file
from flask_socketio import SocketIO, emit, join_room, leave_room
import asyncio
from datetime import datetime
import sys
import os
import json
import uuid
from typing import Dict, List
import redis
sys.path.append(os.path.join(os.path.dirname(__file__), "src"))
from agentmind.core.agent import Agent, Message
from agentmind.core.mind import AgentMind
app = Flask(__name__)
app.config["SECRET_KEY"] = os.getenv("SESSION_SECRET", "agentmind-chat-secret")
socketio = SocketIO(app, cors_allowed_origins="*", async_mode="threading")
# Redis for session storage
try:
redis_client = redis.from_url(
os.getenv("REDIS_URL", "redis://localhost:6379/1"), decode_responses=True
)
redis_client.ping()
print("[✓] Redis connected")
except:
redis_client = None
print("[!] Redis not available, using in-memory storage")
# In-memory storage fallback
sessions: Dict[str, Dict] = {}
agents_initialized = False
mind = AgentMind()
def initialize_agents():
"""Initialize agents with different roles and personalities."""
global agents_initialized
if agents_initialized:
return
agent_configs = [
("Alice", "analyst", "🔍", "Data analysis and insights expert"),
("Bob", "creative", "🎨", "Creative thinking and brainstorming"),
("Charlie", "coordinator", "📋", "Project coordination and planning"),
("Diana", "technical", "💻", "Technical implementation specialist"),
("Eve", "researcher", "📚", "Research and information gathering"),
("Frank", "strategist", "♟️", "Strategic planning and decision making"),
("Grace", "designer", "✨", "UX/UI design and user experience"),
("Henry", "developer", "⚙️", "Software development and coding"),
("Iris", "marketer", "📢", "Marketing and communication"),
("Jack", "philosopher", "🤔", "Critical thinking and ethics"),
("Kate", "scientist", "🔬", "Scientific analysis and methodology"),
("Leo", "entrepreneur", "💡", "Business innovation and growth"),
]
for name, role, emoji, description in agent_configs:
agent = Agent(name, role)
agent.emoji = emoji
agent.description = description
mind.add_agent(agent)
agents_initialized = True
print(f"[✓] Initialized {len(mind.agents)} agents")
def get_session(session_id: str) -> Dict:
"""Get session data from Redis or memory."""
if redis_client:
try:
data = redis_client.get(f"chat_session:{session_id}")
return json.loads(data) if data else None
except:
pass
return sessions.get(session_id)
def save_session(session_id: str, data: Dict):
"""Save session data to Redis or memory."""
if redis_client:
try:
redis_client.setex(f"chat_session:{session_id}", 86400, json.dumps(data)) # 24 hours
return
except:
pass
sessions[session_id] = data
def export_to_markdown(session_id: str) -> str:
"""Export chat history to Markdown."""
session = get_session(session_id)
if not session:
return "# Session not found"
md = f"# AgentMind Chat Session\n\n"
md += f"**Session ID:** {session_id}\n"
md += f"**Created:** {session.get('created_at', 'Unknown')}\n"
md += f"**Messages:** {len(session.get('messages', []))}\n\n"
md += "---\n\n"
for msg in session.get("messages", []):
sender = msg.get("sender", "Unknown")
content = msg.get("content", "")
timestamp = msg.get("timestamp", "")
msg_type = msg.get("type", "user")
if msg_type == "user":
md += f"### 👤 {sender} ({timestamp})\n\n"
else:
emoji = msg.get("emoji", "🤖")
role = msg.get("role", "assistant")
md += f"### {emoji} {sender} - *{role}* ({timestamp})\n\n"
md += f"{content}\n\n"
md += "---\n\n"
return md
@app.route("/")
def index():
"""Serve the main chat interface."""
return render_template("chat_enhanced.html")
@app.route("/health")
def health():
"""Health check endpoint."""
return jsonify(
{
"status": "healthy",
"agents": len(mind.agents),
"redis_connected": redis_client is not None,
}
)
@app.route("/api/sessions", methods=["GET"])
def list_sessions():
"""List all chat sessions."""
if redis_client:
try:
keys = redis_client.keys("chat_session:*")
session_list = []
for key in keys:
session_id = key.split(":")[-1]
data = get_session(session_id)
if data:
session_list.append(
{
"session_id": session_id,
"created_at": data.get("created_at"),
"message_count": len(data.get("messages", [])),
}
)
return jsonify({"sessions": session_list})
except:
pass
return jsonify(
{
"sessions": [
{
"session_id": sid,
"created_at": data.get("created_at"),
"message_count": len(data.get("messages", [])),
}
for sid, data in sessions.items()
]
}
)
@app.route("/api/sessions/<session_id>", methods=["GET"])
def get_session_data(session_id: str):
"""Get session data."""
session = get_session(session_id)
if not session:
return jsonify({"error": "Session not found"}), 404
return jsonify(session)
@app.route("/api/sessions/<session_id>/export", methods=["GET"])
def export_session(session_id: str):
"""Export session to Markdown."""
format_type = request.args.get("format", "markdown")
if format_type == "markdown":
md_content = export_to_markdown(session_id)
filename = f"agentmind_session_{session_id[:8]}.md"
# Save to temp file
temp_path = f"/tmp/{filename}"
with open(temp_path, "w") as f:
f.write(md_content)
return send_file(
temp_path, as_attachment=True, download_name=filename, mimetype="text/markdown"
)
elif format_type == "json":
session = get_session(session_id)
if not session:
return jsonify({"error": "Session not found"}), 404
filename = f"agentmind_session_{session_id[:8]}.json"
temp_path = f"/tmp/{filename}"
with open(temp_path, "w") as f:
json.dump(session, f, indent=2)
return send_file(
temp_path, as_attachment=True, download_name=filename, mimetype="application/json"
)
return jsonify({"error": "Unsupported format"}), 400
@socketio.on("connect")
def handle_connect():
"""Handle client connection."""
initialize_agents()
# Generate session ID
session_id = str(uuid.uuid4())
# Create new session
session_data = {
"session_id": session_id,
"created_at": datetime.now().isoformat(),
"messages": [],
}
save_session(session_id, session_data)
# Send agent list to the newly connected client
agent_list = [
{
"name": agent.name,
"role": agent.role,
"emoji": getattr(agent, "emoji", "🤖"),
"description": getattr(agent, "description", ""),
"is_active": agent.is_active,
}
for agent in mind.agents
]
emit("session_created", {"session_id": session_id, "agents": agent_list})
emit(
"system_message",
{
"content": f"Welcome! {len(mind.agents)} agents are ready to collaborate.",
"timestamp": datetime.now().strftime("%H:%M:%S"),
},
)
@socketio.on("disconnect")
def handle_disconnect():
"""Handle client disconnection."""
print("[!] Client disconnected")
@socketio.on("join_session")
def handle_join_session(data):
"""Join an existing session."""
session_id = data.get("session_id")
join_room(session_id)
session = get_session(session_id)
if session:
emit("session_history", {"messages": session.get("messages", [])})
@socketio.on("user_message")
def handle_user_message(data):
"""Handle user message."""
content = data.get("content", "")
username = data.get("username", "User")
session_id = data.get("session_id")
if not content.strip():
return
# Create message object
message_data = {
"sender": username,
"content": content,
"timestamp": datetime.now().strftime("%H:%M:%S"),
"type": "user",
"emoji": "👤",
}
# Save to session
session = get_session(session_id)
if session:
session["messages"].append(message_data)
save_session(session_id, session)
# Broadcast user message
emit("new_message", message_data, room=session_id, broadcast=True)
# Create message for agents
user_msg = Message(content=content, sender=username)
# Process with agents asynchronously
def process_agents():
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
try:
responses = loop.run_until_complete(
mind.broadcast_message(user_msg, exclude_sender=False)
)
# Send agent responses
for response in responses:
agent = next((a for a in mind.agents if a.name == response.sender), None)
agent_message = {
"sender": response.sender,
"content": response.content,
"timestamp": response.timestamp.strftime("%H:%M:%S"),
"type": "agent",
"role": agent.role if agent else "assistant",
"emoji": getattr(agent, "emoji", "🤖") if agent else "🤖",
}
# Save to session
session = get_session(session_id)
if session:
session["messages"].append(agent_message)
save_session(session_id, session)
socketio.emit("new_message", agent_message, room=session_id)
finally:
loop.close()
# Run in background thread
socketio.start_background_task(process_agents)
@socketio.on("toggle_agent")
def handle_toggle_agent(data):
"""Toggle agent active status."""
agent_name = data.get("agent_name")
session_id = data.get("session_id")
agent = next((a for a in mind.agents if a.name == agent_name), None)
if agent:
agent.is_active = not agent.is_active
emit(
"agent_toggled",
{"agent_name": agent_name, "is_active": agent.is_active},
room=session_id,
broadcast=True,
)
@socketio.on("clear_session")
def handle_clear_session(data):
"""Clear session history."""
session_id = data.get("session_id")
session = get_session(session_id)
if session:
session["messages"] = []
save_session(session_id, session)
emit("session_cleared", {}, room=session_id, broadcast=True)
if __name__ == "__main__":
print("=" * 60)
print("🚀 AgentMind Enhanced Chat Server Starting...")
print("=" * 60)
print("📡 Server will be available at: http://localhost:5000")
print("💬 Multi-session chat interface with export support!")
print("=" * 60)
socketio.run(app, debug=False, host="0.0.0.0", port=5000)