-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
70 lines (54 loc) · 2.16 KB
/
server.py
File metadata and controls
70 lines (54 loc) · 2.16 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
"""
Agent Representation Broker Server
A REST API for the Agent Representation Broker.
"""
import json
import os
from flask import Flask, request, jsonify
from agent_broker import AgentBroker
app = Flask(__name__)
broker = AgentBroker()
@app.route('/agents', methods=['POST'])
def register_agent():
"""Register a new agent."""
data = request.get_json()
agent_id = data.get('agent_id')
capabilities = data.get('capabilities', [])
if not agent_id or not isinstance(capabilities, list):
return jsonify({'error': 'Invalid input'}), 400
success = broker.register_agent(agent_id, capabilities)
if not success:
return jsonify({'error': 'Agent already exists'}), 409
return jsonify({'message': 'Agent registered successfully'}), 201
@app.route('/tasks', methods=['POST'])
def submit_task():
"""Submit a new task."""
data = request.get_json()
task_id = data.get('task_id')
requirements = data.get('requirements', [])
if not task_id or not isinstance(requirements, list):
return jsonify({'error': 'Invalid input'}), 400
success = broker.submit_task(task_id, requirements)
if not success:
return jsonify({'error': 'Task already exists'}), 409
return jsonify({'message': 'Task submitted successfully'}), 201
@app.route('/agents/<agent_id>/tasks', methods=['GET'])
def get_matched_tasks(agent_id):
"""Get tasks matched for an agent."""
matched_tasks = broker.get_matched_tasks(agent_id)
return jsonify({'agent_id': agent_id, 'matched_tasks': matched_tasks})
@app.route('/tasks/<task_id>/agents', methods=['GET'])
def get_matched_agents(task_id):
"""Get agents matched for a task."""
matched_agents = broker.get_matched_agents(task_id)
return jsonify({'task_id': task_id, 'matched_agents': matched_agents})
@app.route('/status', methods=['GET'])
def get_status():
"""Get broker status."""
status = broker.get_status()
return jsonify(status)
if __name__ == '__main__':
host = os.getenv('BROKER_HOST', '0.0.0.0')
port = int(os.getenv('BROKER_PORT', 5000))
debug = os.getenv('BROKER_DEBUG', 'False').lower() == 'true'
app.run(host=host, port=port, debug=debug)