33Vercel-ready Flask server for StellaAgent.
44Supports local testing (flask run) and deployment on Vercel.
55"""
6- from flask import Flask , request , Response
6+ from flask import Flask , request , Response , jsonify
77from flask_cors import CORS
88import json
99import sys
1010import os
11+ import traceback
1112
1213# Add parent directory to path for imports
1314sys .path .insert (0 , os .path .dirname (os .path .dirname (__file__ )))
@@ -36,15 +37,18 @@ def _get_agent():
3637 agent = StellaAgent (manifest )
3738 return agent , None
3839 except Exception as exc :
39- startup_error = str ( exc )
40+ startup_error = f" { exc . __class__ . __name__ } : { exc } "
4041 print (f"ERROR server.py: Agent startup failed: { startup_error } " , flush = True )
42+ print (traceback .format_exc (), flush = True )
4143 return None , startup_error
4244
4345@app .route ("/" , methods = ["POST" ])
4446@app .route ("/api" , methods = ["POST" ])
4547@app .route ("/api/" , methods = ["POST" ])
4648def handle_request ():
4749 payload_text = request .get_data (as_text = True )
50+ if not payload_text :
51+ return Response ("Invalid OpenFloor payload: empty request body" , status = 400 )
4852
4953 current_agent , init_error = _get_agent ()
5054 if init_error :
@@ -67,12 +71,31 @@ def handle_request():
6771 return Response (f"Invalid OpenFloor payload: { e } " , status = 400 )
6872
6973 # Process and serialize response
70- out_envelope = current_agent .process_envelope (in_envelope )
71- payload_str = out_envelope .to_json (as_payload = True )
74+ try :
75+ out_envelope = current_agent .process_envelope (in_envelope )
76+ payload_str = out_envelope .to_json (as_payload = True )
77+ except Exception as exc :
78+ error_text = f"{ exc .__class__ .__name__ } : { exc } "
79+ print (f"ERROR server.py: Envelope processing failed: { error_text } " , flush = True )
80+ print (traceback .format_exc (), flush = True )
81+ return Response (f"Envelope processing error: { error_text } " , status = 500 )
7282
7383 return Response (payload_str , mimetype = "application/json" )
7484
7585
86+ @app .route ("/health" , methods = ["GET" ])
87+ def health ():
88+ current_agent , init_error = _get_agent ()
89+ if init_error :
90+ return jsonify ({"status" : "unhealthy" , "error" : init_error }), 500
91+
92+ return jsonify ({
93+ "status" : "healthy" ,
94+ "agent" : getattr (current_agent ._manifest .identification , "conversationalName" , "stella" ),
95+ "serviceUrl" : getattr (current_agent ._manifest .identification , "serviceUrl" , "" ),
96+ })
97+
98+
7699# Vercel WSGI handler - this is the correct way to expose Flask for Vercel
77100app = app
78101
0 commit comments