Skip to content
Draft
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Cleanup typing.
  • Loading branch information
tcdent committed Feb 13, 2025
commit 23693512a43ee05cf285cceec5be8cd404f13c61
37 changes: 19 additions & 18 deletions agentstack/serve/serve.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def call_webhook(webhook_url: str, data: dict[str, Any]) -> None:
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)}")
log.error(f"Webhook call failed: {str(e)}")
raise


Expand Down Expand Up @@ -123,8 +123,9 @@ def run_project(command: str = 'run', api_args: Optional[dict[str, str]] = None,
# TODO `api_args` is unused
run.preflight()

for key, value in api_inputs.items():
inputs.add_input_for_run(key, value)
if api_inputs:
for key, value in api_inputs.items():
inputs.add_input_for_run(key, value)

run.run_project(command=command)

Expand Down Expand Up @@ -218,7 +219,7 @@ def register_routes(self):
# WebSocket routes
self.sock.route(route)(handler)

def get_routes(self) -> list[tuple[str, Callable, str]]:
def get_routes(self) -> list[tuple[str, Callable, Optional[str]]]:
return [
('/', self.index, 'GET'),
('/ws', self.websocket_handler, None),
Expand All @@ -230,20 +231,20 @@ def format_response(self, response: Response) -> BaseResponse:
"""Dump a response object to JSON"""
return jsonify(response.model_dump())

def index(self):
def index(self) -> tuple[BaseResponse, int]:
"""Serve a user interface"""
# TODO delegate this to the user project.
return send_file(conf.PATH / 'src/index.html'), 200

def health(self) -> BaseResponse:
def health(self) -> tuple[BaseResponse, int]:
"""Health check endpoint"""
response = Response(
type=Response.Type.DATA,
data={'status': 'ok'},
)
return self.format_response(response), 200

def process(self) -> BaseResponse:
def process(self) -> tuple[BaseResponse, int]:
request_data = None
try:
request_data = request.get_json()
Expand All @@ -264,18 +265,20 @@ def process(self) -> BaseResponse:

except Exception as e:
error_message = str(e)
# TODO agentstack.log?
app.logger.error(f"Error processing request: {error_message}")
log.error(f"Error processing request: {error_message}")
return self.format_response(Response(
type=Response.Type.ERROR,
data={'message': error_message}
)), 500

finally:
if not self.webhook_url:
# TODO project will not run if we don't have a webhook url
# can we just yolo it into the void or should we tell the user first?
return
# project will not run if we don't have a webhook url
log.error("No webhook URL provided")
return self.format_response(Response(
type=Response.Type.ERROR,
data={'message': 'No webhook URL provided'}
)), 500

try:
assert request_data, "request_data is None"
Expand All @@ -288,16 +291,14 @@ def process(self) -> BaseResponse:
})
except Exception as e:
error_message = str(e)
# TODO agentstack.log?
app.logger.error(f"Error in process: {error_message}")
log.error(f"Error in process: {error_message}")
try:
call_webhook(self.webhook_url, {
'status': 'error',
'error': error_message
})
except:
# TODO agentstack.log?
app.logger.error("Failed to send error to webhook")
log.error("Failed to send error to webhook")
finally:
self.webhook_url = None

Expand Down Expand Up @@ -336,7 +337,7 @@ def get_response(self, message: dict[str, Any]) -> Generator[Response, None, Non
data={'error': "Unknown message type"},
)

def websocket_handler(self, ws):
def websocket_handler(self, ws) -> None:
"""Handle WebSocket connections"""
while True:
try:
Expand All @@ -351,7 +352,7 @@ def websocket_handler(self, ws):
ws.send(json.dumps(response.model_dump()))
break

def run(self, host='0.0.0.0', port=6969, **kwargs):
def run(self, host='0.0.0.0', port=6969, **kwargs) -> None:
"""Run the Flask application"""
self.app.run(host=host, port=port, **kwargs)

Expand Down