Spaces:
Running
Running
File size: 2,737 Bytes
8195c87 d69e4e8 |
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 |
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 |