forked from carllapierre/cli-agent-graph-sandbox
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweb_app.py
More file actions
536 lines (467 loc) · 18.9 KB
/
web_app.py
File metadata and controls
536 lines (467 loc) · 18.9 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
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
#!/usr/bin/env python3
"""
Flask-based Web UI for 7 Habits Agent Graph
Provides a web interface with:
1. Vision board images slideshow from data/ folder
2. Chat interface using the 02-tooluse agent graph
"""
import os
import asyncio
import uuid
from pathlib import Path
from typing import List, Dict, Any
from flask import Flask, render_template, request, jsonify, send_from_directory
from datetime import datetime
import json
# Import existing framework components
from framework.graph_manager import invoke_graph
from framework.mcp_registry import init_mcp_registry
from dotenv import load_dotenv
# Load environment variables
load_dotenv()
app = Flask(__name__)
app.secret_key = os.getenv('FLASK_SECRET_KEY', 'dev-secret-key-change-in-production')
# Configuration
DATA_DIR = Path(os.getenv("DATA_DIR", "./data")).resolve()
DATA_DIR.mkdir(parents=True, exist_ok=True)
# Global state for chat sessions
chat_sessions: Dict[str, Dict[str, Any]] = {}
def get_image_files() -> List[Dict[str, Any]]:
"""Get all image files from data directory with metadata."""
image_extensions = {'.jpg', '.jpeg', '.png', '.gif', '.bmp', '.webp'}
images = []
if not DATA_DIR.exists():
return images
for file_path in DATA_DIR.iterdir():
if file_path.is_file() and file_path.suffix.lower() in image_extensions:
try:
stat = file_path.stat()
images.append({
'filename': file_path.name,
'path': str(file_path.relative_to(DATA_DIR.parent)),
'created': datetime.fromtimestamp(stat.st_ctime).isoformat(),
'modified': datetime.fromtimestamp(stat.st_mtime).isoformat(),
'size': stat.st_size
})
except (OSError, ValueError):
continue
# Sort by creation time, newest first
images.sort(key=lambda x: x['created'], reverse=True)
return images
@app.route('/')
def index():
"""Main page with vision board, chat, and todo interface."""
images = get_image_files()
return render_template('index.html', images=images)
@app.route('/api/images')
def api_images():
"""API endpoint to get image list."""
images = get_image_files()
return jsonify({'images': images})
@app.route('/data/<path:filename>')
def serve_image(filename):
"""Serve images from data directory."""
return send_from_directory(DATA_DIR, filename)
# Neon Postgres connection config (set these in your .env)
import psycopg2
from psycopg2.extras import RealDictCursor
def get_todos():
conn = psycopg2.connect(os.getenv('POSTGRES_CONNECTION_STRING'))
try:
with conn.cursor(cursor_factory=RealDictCursor) as cur:
cur.execute("SELECT id, title, description, due_date, priority, status FROM tasks ORDER BY id DESC")
todos = cur.fetchall()
return todos
finally:
conn.close()
@app.route('/api/todos')
def api_todos():
try:
todos = get_todos()
return jsonify({"todos": todos})
except Exception as e:
print(e)
return jsonify({"error": str(e)}), 500
@app.route('/api/habit1-summary')
def api_habit1_summary():
"""Get the Habit 1 proactive summary markdown content."""
try:
summary_path = DATA_DIR / "habits" / "habit1_proactive_summary.md"
if summary_path.exists():
with open(summary_path, 'r', encoding='utf-8') as f:
content = f.read()
return jsonify({
"exists": True,
"content": content,
"last_modified": datetime.fromtimestamp(summary_path.stat().st_mtime).isoformat()
})
else:
return jsonify({
"exists": False,
"message": "Summary not yet generated. Run the habit1-proactive workflow to create it."
})
except Exception as e:
print(f"Error loading habit1 summary: {e}")
return jsonify({"error": str(e)}), 500
@app.route('/api/habit4-summary')
def api_habit4_summary():
"""Get the Habit 4 summary markdown content."""
try:
summary_path = DATA_DIR / "habits" / "habit4_summary.md"
if summary_path.exists():
with open(summary_path, 'r', encoding='utf-8') as f:
content = f.read()
return jsonify({
"exists": True,
"content": content,
"last_modified": datetime.fromtimestamp(summary_path.stat().st_mtime).isoformat()
})
else:
return jsonify({
"exists": False,
"message": "Summary not yet generated. Run the habit4 workflow to create it."
})
except Exception as e:
print(f"Error loading habit4 summary: {e}")
return jsonify({"error": str(e)}), 500
@app.route('/api/habit5-summary')
def api_habit5_summary():
"""Get the Habit 5 summary markdown content."""
try:
summary_path = DATA_DIR / "habits" / "habit5_listen.md"
if summary_path.exists():
with open(summary_path, 'r', encoding='utf-8') as f:
content = f.read()
return jsonify({
"exists": True,
"content": content,
"last_modified": datetime.fromtimestamp(summary_path.stat().st_mtime).isoformat()
})
else:
return jsonify({
"exists": False,
"message": "Summary not yet generated. Run the habit5 workflow to create it."
})
except Exception as e:
print(f"Error loading habit5 summary: {e}")
return jsonify({"error": str(e)}), 500
@app.route('/api/habit6-summary')
def api_habit6_summary():
"""Get the Habit 6 summary markdown content."""
try:
summary_path = DATA_DIR / "habits" / "habit6_synergize.md"
if summary_path.exists():
with open(summary_path, 'r', encoding='utf-8') as f:
content = f.read()
return jsonify({
"exists": True,
"content": content,
"last_modified": datetime.fromtimestamp(summary_path.stat().st_mtime).isoformat()
})
else:
return jsonify({
"exists": False,
"message": "Summary not yet generated. Run the habit6 workflow to create it."
})
except Exception as e:
print(f"Error loading habit6 summary: {e}")
return jsonify({"error": str(e)}), 500
@app.route('/api/habit7-summary')
def api_habit7_summary():
"""Get the Habit 7 summary markdown content."""
try:
summary_path = DATA_DIR / "habits" / "habit7_sharpen.md"
if summary_path.exists():
with open(summary_path, 'r', encoding='utf-8') as f:
content = f.read()
return jsonify({
"exists": True,
"content": content,
"last_modified": datetime.fromtimestamp(summary_path.stat().st_mtime).isoformat()
})
else:
return jsonify({
"exists": False,
"message": "Summary not yet generated. Run the habit7 workflow to create it."
})
except Exception as e:
print(f"Error loading habit7 summary: {e}")
return jsonify({"error": str(e)}), 500
@app.route('/api/chat', methods=['POST'])
def api_chat():
"""Handle chat requests."""
try:
data = request.get_json()
if not data or 'message' not in data:
return jsonify({'error': 'Message is required'}), 400
message = data['message'].strip()
if not message:
return jsonify({'error': 'Message cannot be empty'}), 400
session_id = data.get('session_id')
if not session_id:
session_id = str(uuid.uuid4())
chat_sessions[session_id] = {
'thread_id': str(uuid.uuid4()),
'is_new_thread': True,
'messages': []
}
session = chat_sessions.get(session_id)
if not session:
session = {
'thread_id': str(uuid.uuid4()),
'is_new_thread': True,
'messages': []
}
chat_sessions[session_id] = session
# Add user message to session
session['messages'].append({
'type': 'user',
'content': message,
'timestamp': datetime.now().isoformat()
})
# Process message through the 02-tooluse graph
try:
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
try:
response = loop.run_until_complete(
invoke_graph(
graph_name='02-tooluse',
message=message,
thread_id=session['thread_id'],
is_new_thread=session['is_new_thread']
)
)
finally:
loop.close()
except Exception as graph_error:
# Fallback response when graph is not available
print(f"Graph error: {graph_error}")
if 'add image' in message.lower() or 'generate' in message.lower() or 'create' in message.lower():
response = "I understand you'd like to create a vision board image. However, the image generation service is currently not configured. Please set up your Azure OpenAI DALL-E credentials in the .env file to enable this feature."
else:
response = f"I'm sorry, but I'm currently unable to process your request due to a service configuration issue. The AI agent requires proper setup of external services. Error: {str(graph_error)}"
# Mark session as no longer new
session['is_new_thread'] = False
# Add assistant response to session
session['messages'].append({
'type': 'assistant',
'content': response,
'timestamp': datetime.now().isoformat()
})
return jsonify({
'response': response,
'session_id': session_id,
'message_count': len(session['messages'])
})
except Exception as e:
return jsonify({'error': f'Failed to process message: {str(e)}'}), 500
@app.route('/api/chat/new', methods=['POST'])
def api_new_chat():
"""Start a new chat session."""
session_id = str(uuid.uuid4())
chat_sessions[session_id] = {
'thread_id': str(uuid.uuid4()),
'is_new_thread': True,
'messages': []
}
return jsonify({'session_id': session_id})
@app.route('/api/chat/history/<session_id>')
def api_chat_history(session_id):
"""Get chat history for a session."""
session = chat_sessions.get(session_id)
if not session:
return jsonify({'error': 'Session not found'}), 404
return jsonify({
'session_id': session_id,
'messages': session['messages']
})
async def fetch_real_github_data():
"""Fetch real GitHub data using MCP tools."""
try:
# Import here to avoid issues if MCP is not available
from framework.mcp_registry import get_mcp_tools, _registry
# Default repository information
owner = "jaganraajan"
repo_name = "7-habits-agent-graph"
# Get GitHub tools
github_tools = get_mcp_tools("github")
if not github_tools:
return None
# Use the MCP client to get real data
commits_result = await _registry._client.call_tool(
server_name="github",
tool_name="list_commits",
arguments={
"owner": owner,
"repo": repo_name,
"perPage": 5
}
)
prs_result = await _registry._client.call_tool(
server_name="github",
tool_name="list_pull_requests",
arguments={
"owner": owner,
"repo": repo_name,
"state": "all",
"perPage": 5
}
)
return {
'commits': commits_result,
'pull_requests': prs_result,
'source': 'mcp'
}
except Exception as e:
print(f"Error fetching real GitHub data: {e}")
return None
@app.route('/api/github-activity')
def api_github_activity():
"""Get recent GitHub commits and pull requests."""
try:
# Default repository information (same as in 06-github-info graph)
owner = "jaganraajan"
repo_name = "7-habits-agent-graph"
# Try to fetch real GitHub data
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
try:
real_data = loop.run_until_complete(fetch_real_github_data())
finally:
loop.close()
if real_data:
return jsonify(real_data)
# Fallback to demo data when MCP is not available or fails
return jsonify({
'commits': [
{
'sha': '6ccae5d',
'message': 'Add web UI with vision board and chat interface',
'author': {'name': 'Developer', 'email': 'dev@example.com'},
'date': '2024-12-01T10:30:00Z',
'url': f'https://github.com/{owner}/{repo_name}/commit/6ccae5d'
},
{
'sha': '5bbdf4c',
'message': 'Implement GitHub MCP integration',
'author': {'name': 'Developer', 'email': 'dev@example.com'},
'date': '2024-11-30T15:45:00Z',
'url': f'https://github.com/{owner}/{repo_name}/commit/5bbdf4c'
},
{
'sha': 'abc123f',
'message': 'Initial project setup with LangGraph framework',
'author': {'name': 'jaganraajan', 'email': 'jagan@example.com'},
'date': '2024-11-29T09:15:00Z',
'url': f'https://github.com/{owner}/{repo_name}/commit/abc123f'
}
],
'pull_requests': [
{
'number': 42,
'title': 'Enhance Flask UI with GitHub activity display',
'state': 'open',
'user': {'login': 'copilot'},
'created_at': '2024-12-01T14:20:00Z',
'url': f'https://github.com/{owner}/{repo_name}/pull/42'
},
{
'number': 41,
'title': 'Add vision board image generation',
'state': 'merged',
'user': {'login': 'jaganraajan'},
'created_at': '2024-11-30T11:30:00Z',
'url': f'https://github.com/{owner}/{repo_name}/pull/41'
}
],
'source': 'demo'
})
except Exception as e:
print(f"Error fetching GitHub activity: {e}")
return jsonify({'error': 'Failed to fetch GitHub activity'}), 500
@app.route('/api/github-summary', methods=['POST'])
def api_github_summary():
"""Generate a summary of GitHub activity using the agent graph."""
try:
data = request.get_json()
github_data = data.get('github_data', {})
# Create a summary prompt
commits = github_data.get('commits', [])
prs = github_data.get('pull_requests', [])
summary_prompt = f"""Please provide a concise summary of the following GitHub activity:
Commits ({len(commits)} total):
"""
for commit in commits[:3]: # Limit to first 3 commits
summary_prompt += f"- {commit.get('message', 'No message')} by {commit.get('author', {}).get('name', 'Unknown')}\n"
summary_prompt += f"\nPull Requests ({len(prs)} total):\n"
for pr in prs[:3]: # Limit to first 3 PRs
summary_prompt += f"- #{pr.get('number', 'N/A')}: {pr.get('title', 'No title')} ({pr.get('state', 'unknown')})\n"
summary_prompt += "\nPlease provide a brief summary of the development activity and any notable patterns or trends."
# Process summary through the 02-tooluse graph
try:
session_id = str(uuid.uuid4())
chat_sessions[session_id] = {
'thread_id': str(uuid.uuid4()),
'is_new_thread': True,
'messages': []
}
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
try:
response = loop.run_until_complete(
invoke_graph(
graph_name='02-tooluse',
message=summary_prompt,
thread_id=chat_sessions[session_id]['thread_id'],
is_new_thread=True
)
)
finally:
loop.close()
except Exception as graph_error:
# Fallback response when graph is not available
print(f"Graph error: {graph_error}")
response = f"""Based on the GitHub activity:
Recent Development Summary:
- {len(commits)} commits show active development
- {len(prs)} pull requests indicate collaborative work
- Recent focus appears to be on {"UI enhancements" if any("UI" in commit.get('message', '') for commit in commits) else "core functionality"}
Key Activities:
""" + "\n".join([f"• {commit.get('message', 'No message')}" for commit in commits[:2]])
if prs:
response += f"\n\nPull Request Activity:\n"
response += "\n".join([f"• #{pr.get('number', 'N/A')}: {pr.get('title', 'No title')} ({pr.get('state', 'unknown')})" for pr in prs[:2]])
return jsonify({
'summary': response,
'session_id': session_id
})
except Exception as e:
print(f"Error generating GitHub summary: {e}")
return jsonify({'error': 'Failed to generate GitHub summary'}), 500
async def init_app():
"""Initialize the application."""
try:
await init_mcp_registry()
print("MCP registry initialized successfully")
except Exception as e:
print(f"Warning: Failed to initialize MCP registry: {e}")
print("Some features may not be available")
def create_app():
"""Create and configure the Flask app."""
# Initialize async components
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
try:
loop.run_until_complete(init_app())
finally:
loop.close()
return app
if __name__ == '__main__':
# Initialize and run the app
app = create_app()
port = int(os.getenv('PORT', 5000))
debug = os.getenv('FLASK_DEBUG', 'False').lower() == 'true'
print(f"Starting Flask app on http://localhost:{port}")
print(f"Vision board data directory: {DATA_DIR}")
app.run(host='0.0.0.0', port=port, debug=debug)