azettl commited on
Commit
d69e4e8
Β·
verified Β·
1 Parent(s): 286db3a

Update openfloor/OpenFloorAgentServer.py

Browse files
Files changed (1) hide show
  1. openfloor/OpenFloorAgentServer.py +62 -0
openfloor/OpenFloorAgentServer.py CHANGED
@@ -0,0 +1,62 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ class OpenFloorAgentServer:
2
+ """Run a research agent as an actual OpenFloor service"""
3
+
4
+ def __init__(self, research_agent: OpenFloorResearchAgent, port: int):
5
+ self.agent = research_agent
6
+ self.port = port
7
+ self.app = None
8
+
9
+ def start_server(self):
10
+ """Start the OpenFloor agent server"""
11
+ from flask import Flask, request, jsonify
12
+
13
+ app = Flask(f"research-agent-{self.port}")
14
+
15
+ @app.route('/openfloor/conversation', methods=['POST'])
16
+ def handle_conversation():
17
+ try:
18
+ print(f"πŸ” DEBUG: Flask route called for agent {self.agent.tool.name}")
19
+
20
+ # Parse incoming OpenFloor envelope
21
+ envelope_data = request.get_json()
22
+ print(f"πŸ” DEBUG: Received envelope data: {str(envelope_data)[:200]}...")
23
+
24
+ envelope = Envelope.from_json(json.dumps(envelope_data))
25
+ print(f"πŸ” DEBUG: Envelope parsed successfully")
26
+
27
+ # Process the request
28
+ print(f"πŸ” DEBUG: Calling handle_utterance_event...")
29
+ response_envelope = self.agent.handle_utterance_event(envelope)
30
+ print(f"πŸ” DEBUG: handle_utterance_event completed")
31
+
32
+ # Return OpenFloor response
33
+ response_json = json.loads(response_envelope.to_json())
34
+ print(f"πŸ” DEBUG: Returning response: {str(response_json)[:200]}...")
35
+ return jsonify(response_json)
36
+
37
+ except Exception as e:
38
+ print(f"πŸ” DEBUG: Exception in Flask route: {e}")
39
+ import traceback
40
+ traceback.print_exc()
41
+
42
+ error_response = self.agent._create_error_response(
43
+ envelope if 'envelope' in locals() else None,
44
+ str(e)
45
+ )
46
+ return jsonify(json.loads(error_response.to_json())), 500
47
+
48
+ @app.route('/openfloor/manifest', methods=['GET'])
49
+ def get_manifest():
50
+ """Return agent manifest"""
51
+ return jsonify(json.loads(self.agent.manifest.to_json()))
52
+
53
+ # Start server in background thread
54
+ import threading
55
+ server_thread = threading.Thread(
56
+ target=lambda: app.run(host='localhost', port=self.port, debug=False)
57
+ )
58
+ server_thread.daemon = True
59
+ server_thread.start()
60
+
61
+ print(f"πŸš€ OpenFloor agent '{self.agent.manifest.identification.conversationalName}' started on port {self.port}")
62
+ return True