from .OpenFloorResearchAgent import OpenFloorResearchAgent class OpenFloorAgentServer: """Run a research agent as an actual OpenFloor service""" def __init__(self, research_agent: OpenFloorResearchAgent, port: int): self.agent = research_agent self.port = port self.app = None def start_server(self): """Start the OpenFloor agent server""" from flask import Flask, request, jsonify app = Flask(f"research-agent-{self.port}") @app.route('/openfloor/conversation', methods=['POST']) def handle_conversation(): try: print(f"🔍 DEBUG: Flask route called for agent {self.agent.tool.name}") # Parse incoming OpenFloor envelope envelope_data = request.get_json() print(f"🔍 DEBUG: Received envelope data: {str(envelope_data)[:200]}...") envelope = Envelope.from_json(json.dumps(envelope_data)) print(f"🔍 DEBUG: Envelope parsed successfully") # Process the request print(f"🔍 DEBUG: Calling handle_utterance_event...") response_envelope = self.agent.handle_utterance_event(envelope) print(f"🔍 DEBUG: handle_utterance_event completed") # Return OpenFloor response response_json = json.loads(response_envelope.to_json()) print(f"🔍 DEBUG: Returning response: {str(response_json)[:200]}...") return jsonify(response_json) except Exception as e: print(f"🔍 DEBUG: Exception in Flask route: {e}") import traceback traceback.print_exc() error_response = self.agent._create_error_response( envelope if 'envelope' in locals() else None, str(e) ) return jsonify(json.loads(error_response.to_json())), 500 @app.route('/openfloor/manifest', methods=['GET']) def get_manifest(): """Return agent manifest""" return jsonify(json.loads(self.agent.manifest.to_json())) # Start server in background thread import threading server_thread = threading.Thread( target=lambda: app.run(host='localhost', port=self.port, debug=False) ) server_thread.daemon = True server_thread.start() print(f"🚀 OpenFloor agent '{self.agent.manifest.identification.conversationalName}' started on port {self.port}") return True