-
Notifications
You must be signed in to change notification settings - Fork 215
Expand file tree
/
Copy pathserve.py
More file actions
83 lines (64 loc) · 2.46 KB
/
serve.py
File metadata and controls
83 lines (64 loc) · 2.46 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
71
72
73
74
75
76
77
78
79
80
81
82
83
# app.py
from dotenv import load_dotenv
load_dotenv(dotenv_path="/app/.env")
from flask import Flask, request, jsonify
import requests
from typing import Dict, Any
import os
from main import run
app = Flask(__name__)
def call_webhook(webhook_url: str, data: Dict[str, Any]) -> None:
"""Send results to the specified webhook URL."""
try:
response = requests.post(webhook_url, json=data)
response.raise_for_status()
except requests.exceptions.RequestException as e:
app.logger.error(f"Webhook call failed: {str(e)}")
raise
@app.route("/health", methods=["GET"])
def health():
return "Agent Server Up"
@app.route('/process', methods=['POST'])
def process_agent():
try:
# Extract data and webhook URL from request
request_data = request.get_json()
if not request_data or 'webhook_url' not in request_data:
return jsonify({'error': 'Missing webhook_url in request'}), 400
webhook_url = request_data.pop('webhook_url')
# Run the agent process with the provided data
# result = WebresearcherCrew().crew().kickoff(inputs=request_data)
# inputs = json.stringify(request_data)
# os.system(f"python src/main.py {inputs}")
result = run(request_data)
# Call the webhook with the results
call_webhook(webhook_url, {
'status': 'success',
'result': result
})
return jsonify({
'status': 'success',
'message': 'Agent process completed and webhook called'
})
except Exception as e:
error_message = str(e)
app.logger.error(f"Error processing request: {error_message}")
# Attempt to call webhook with error information
if webhook_url:
try:
call_webhook(webhook_url, {
'status': 'error',
'error': error_message
})
except:
pass # Webhook call failed, but we still want to return the error to the caller
return jsonify({
'status': 'error',
'error': error_message
}), 500
if __name__ == '__main__':
port = int(os.environ.get('PORT', 6969))
print("🚧 Running your agent on a development server")
print(f"Send agent requests to http://localhost:{port}")
print("Learn more about agent requests at https://docs.agentstack.sh/") # TODO: add docs for this
app.run(host='0.0.0.0', port=port)