-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
378 lines (318 loc) · 12.8 KB
/
app.py
File metadata and controls
378 lines (318 loc) · 12.8 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
#!/usr/bin/env python3
"""
Price Adjustment Agent - Main Flask Application
Modular version with webhook handling and multi-agent integration
"""
import logging
from flask import Flask, request, jsonify, render_template
from flask import redirect
from flask_socketio import SocketIO, emit
from datetime import datetime
import json
import asyncio
import threading
from typing import Dict, Any
from dotenv import load_dotenv
# Import DocuSign MCP Client
from docusign_mcp_client import DocuSignMCPClient
# Import application components
from database import get_database_instance
from processors.adjustment_processor import PriceAdjustmentProcessor
# Configure logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger(__name__)
# Create Flask application
app = Flask(__name__)
app.config['SECRET_KEY'] = 'price_adjustment_secret_key'
# Initialize WebSocket support for real-time UI updates
socketio = SocketIO(app, cors_allowed_origins="*")
# Initialize components - simple version
def initialize_app():
"""Initialize application components"""
try:
logger.info("✅ Initializing price adjustment app components...")
# Get singleton database instance
db = get_database_instance()
logger.info("✅ Database initialized successfully")
# Initialize processor
processor = PriceAdjustmentProcessor(db)
return db, processor
except Exception as e:
logger.error(f"❌ Application initialization failed: {e}")
raise
# Global components
db, processor = initialize_app()
# Initialize DocuSign MCP Client
docusign_client = DocuSignMCPClient()
# Real-time update broadcast function
def broadcast_update(request_id: str, agent_name: str, status: str, data: Dict[Any, Any] = None):
"""Broadcast real-time updates to connected dashboard clients"""
try:
update_data = {
'request_id': request_id,
'agent': agent_name,
'status': status,
'data': data or {},
'timestamp': datetime.utcnow().isoformat()
}
socketio.emit('workflow_update', update_data)
logger.info(f"📡 Broadcasted update: {agent_name} -> {status}")
except Exception as e:
logger.error(f"❌ Failed to broadcast update: {e}")
# Set the broadcast callback on the database after defining the function
db.set_broadcast_callback(broadcast_update)
logger.info("✅ Broadcast callback configured for database")
@app.route('/webhook/manual', methods=['POST'])
def webhook_manual():
"""Manual trigger endpoint for price adjustment workflow"""
try:
logger.info("📞 Manual webhook triggered")
# Create a manual trigger request
request_data = {
"trigger_type": "manual",
"timestamp": datetime.utcnow().isoformat(),
"user_id": request.json.get("user_id", "system") if request.json else "system"
}
# Get user_id for database storage
user_id = request_data["user_id"]
# Store the trigger request in database
request_id = f"manual_{datetime.utcnow().strftime('%Y%m%d_%H%M%S')}"
db.store_adjustment_request(request_id, request_data, "manual", user_id)
# Process in background thread
def run_async_process():
try:
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
loop.run_until_complete(processor.process_price_adjustment_request(request_id))
except Exception as e:
logger.error(f"❌ Background processing failed: {e}")
thread = threading.Thread(target=run_async_process)
thread.start()
# Get user_id for response
user_id = request_data.get("user_id", "system")
return jsonify({
"status": "accepted",
"message": f"Price adjustment workflow initiated by {user_id}",
"request_id": request_id,
"user_id": user_id,
"timestamp": datetime.utcnow().isoformat()
}), 202
except Exception as e:
logger.error(f"❌ Manual webhook error: {e}")
return jsonify({"error": str(e)}), 500
@app.route('/webhook/scheduled', methods=['POST'])
def webhook_scheduled():
"""Scheduled trigger endpoint (called by scheduler at 7 PM daily)"""
try:
logger.info("⏰ Scheduled webhook triggered")
# Create a scheduled trigger request
request_data = {
"trigger_type": "scheduled",
"timestamp": datetime.utcnow().isoformat(),
"scheduled_time": "19:00" # 7 PM
}
# Store the trigger request in database
request_id = f"scheduled_{datetime.utcnow().strftime('%Y%m%d_%H%M%S')}"
db.store_adjustment_request(request_id, request_data, "scheduled", "scheduler")
# Process in background thread
def run_async_process():
try:
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
loop.run_until_complete(processor.process_price_adjustment_request(request_id))
except Exception as e:
logger.error(f"❌ Background processing failed: {e}")
thread = threading.Thread(target=run_async_process)
thread.start()
return jsonify({
"status": "accepted",
"message": "Scheduled price adjustment workflow initiated",
"request_id": request_id,
"timestamp": datetime.utcnow().isoformat()
}), 202
except Exception as e:
logger.error(f"❌ Scheduled webhook error: {e}")
return jsonify({"error": str(e)}), 500
@app.route('/status/<request_id>', methods=['GET'])
def get_status(request_id):
"""Get status of a price adjustment request"""
try:
request_data = db.get_adjustment_request(request_id)
if not request_data:
return jsonify({"error": "Request not found"}), 404
return jsonify({
"request_id": request_id,
"status": request_data["status"],
"created_at": request_data["timestamp"],
"completed_at": request_data.get("completed_at"),
"results": json.loads(request_data["adjustment_results"]) if request_data.get("adjustment_results") else None
})
except Exception as e:
logger.error(f"❌ Status check error: {e}")
return jsonify({"error": str(e)}), 500
@app.route('/status/recent', methods=['GET'])
def get_recent_requests():
"""Get recent price adjustment requests"""
try:
recent_requests = db.get_recent_adjustment_requests()
return jsonify({
"requests": recent_requests,
"count": len(recent_requests)
})
except Exception as e:
logger.error(f"❌ Recent requests error: {e}")
return jsonify({"error": str(e)}), 500
@app.route('/health', methods=['GET'])
def health_check():
"""Health check endpoint"""
return jsonify({
"status": "healthy",
"service": "price-adjustment-agent",
"timestamp": datetime.utcnow().isoformat(),
"version": "1.0.0"
})
# ========================================
# DASHBOARD UI ENDPOINTS
# ========================================
# DocuSign MCP OAuth endpoints
@app.route('/docusign/oauth/start')
def docusign_oauth_start():
"""Redirect user to DocuSign OAuth consent page"""
url = docusign_client.get_authorization_url()
logger.info("🔗 THIS IS USING MCP")
return redirect(url)
@app.route('/oauth/callback')
def docusign_oauth_callback():
"""Handle DocuSign OAuth callback and fetch token"""
code = request.args.get('code')
if not code:
return "Missing authorization code", 400
try:
tokens = docusign_client.fetch_token(code)
# Return a friendly HTML response (minimal) and log success
logger.info("✅ DocuSign OAuth completed and tokens saved to disk")
try:
return render_template('oauth_success.html', message='DocuSign OAuth completed successfully')
except Exception:
# If template rendering fails for any reason, return a minimal text response
return 'DocuSign OAuth completed successfully - tokens saved', 200
except Exception as e:
logger.error(f"❌ DocuSign OAuth error: {e}")
return jsonify({"error": str(e)}), 500
@app.route('/docusign/token_status')
def docusign_token_status():
"""Return basic token status for debug (do NOT expose in prod)"""
try:
token_info = {
'has_access_token': bool(docusign_client.access_token),
'has_refresh_token': bool(docusign_client.refresh_token)
}
return jsonify(token_info)
except Exception as e:
return jsonify({'error': str(e)}), 500
@app.route('/docusign/discover')
def docusign_discover():
"""Call MCP server root to discover available tools and metadata (requires OAuth)."""
try:
info = docusign_client.get_server_info()
return jsonify(info)
except Exception as e:
logger.error(f"❌ MCP discovery failed: {e}")
return jsonify({"error": str(e)}), 500
@app.route('/')
@app.route('/dashboard')
def dashboard():
"""Serve the main dashboard UI"""
try:
return render_template('dashboard.html')
except Exception as e:
logger.error(f"❌ Dashboard error: {e}")
return f"Dashboard temporarily unavailable: {e}", 500
@app.route('/api/requests/active', methods=['GET'])
def get_active_requests():
"""Get currently active/processing requests for dashboard"""
try:
active_requests = db.get_active_requests()
return jsonify({
"active_requests": active_requests,
"count": len(active_requests)
})
except Exception as e:
logger.error(f"❌ Failed to get active requests: {e}")
return jsonify({"error": str(e)}), 500
@app.route('/api/requests/<request_id>/details', methods=['GET'])
def get_request_details(request_id):
"""Get detailed information about a specific request"""
try:
request_data = db.get_adjustment_request(request_id)
if not request_data:
return jsonify({"error": "Request not found"}), 404
agent_logs = db.get_agent_logs(request_id)
return jsonify({
"request": request_data,
"agent_logs": agent_logs,
"timestamp": datetime.utcnow().isoformat()
})
except Exception as e:
logger.error(f"❌ Failed to get request details: {e}")
return jsonify({"error": str(e)}), 500
@app.route('/api/stats', methods=['GET'])
def get_dashboard_stats():
"""Get dashboard statistics"""
try:
stats = db.get_dashboard_stats()
return jsonify(stats)
except Exception as e:
logger.error(f"❌ Failed to get dashboard stats: {e}")
return jsonify({"error": str(e)}), 500
# ========================================
# WEBSOCKET HANDLERS
# ========================================
@socketio.on('connect')
def handle_connect():
"""Handle WebSocket client connections"""
logger.info("🔌 Dashboard client connected")
emit('connected', {
'message': 'Connected to Price Adjustment Agent Dashboard',
'timestamp': datetime.utcnow().isoformat()
})
@socketio.on('disconnect')
def handle_disconnect():
"""Handle WebSocket client disconnections"""
logger.info("🔌 Dashboard client disconnected")
@socketio.on('request_status')
def handle_status_request(data):
"""Handle real-time status requests from dashboard"""
try:
request_id = data.get('request_id')
if request_id:
request_data = db.get_adjustment_request(request_id)
agent_logs = db.get_agent_logs(request_id)
emit('status_response', {
'request_id': request_id,
'request_data': request_data,
'agent_logs': agent_logs,
'timestamp': datetime.utcnow().isoformat()
})
except Exception as e:
logger.error(f"❌ Status request error: {e}")
emit('error', {'message': str(e)})
if __name__ == '__main__':
from config import Config
# Validate configuration
Config.validate()
# Ready to start server
logger.info("🚀 Starting Price Adjustment Agent Flask server...")
logger.info(f"🌐 Server will run on {Config.HOST}:{Config.PORT}")
logger.info(f"🔧 Debug mode: {Config.DEBUG}")
# Use socketio.run instead of app.run for WebSocket support
socketio.run(
app,
host=Config.HOST,
port=Config.PORT,
debug=Config.DEBUG,
allow_unsafe_werkzeug=True # Allow for development
)