Skip to content

FastAPI Bot Service#114

Merged
tgrunnagle merged 2 commits into
mainfrom
issue_99_bot_service_2026-01-27
Feb 1, 2026
Merged

FastAPI Bot Service#114
tgrunnagle merged 2 commits into
mainfrom
issue_99_bot_service_2026-01-27

Conversation

@tgrunnagle
Copy link
Copy Markdown
Owner

Closes #99

Summary

Implements the FastAPI REST API layer for the Bot Service, providing endpoints for spawning bots into game rooms, managing active bots, listing available models, and health checks. The service can be run via uv run python -m bot.service and is configured through environment variables.

Changes Made

bot2/

  • Added bot_service.py with FastAPI application and REST endpoints:
    • POST /bots/spawn - Spawn a bot into a game room
    • DELETE /bots/{bot_id} - Remove a bot from its game
    • GET /bots - List all active bots
    • GET /bots/{bot_id} - Get info for a specific bot
    • GET /bots/models - List available trained models
    • GET /health - Health check endpoint
  • Added __main__.py CLI entry point for running the service via uvicorn
  • Added graceful shutdown via lifespan handler that calls BotManager.shutdown()
  • Added FastAPI and uvicorn dependencies to pyproject.toml

Implementation Details

  • Route order ensures /bots/models is matched before /bots/{bot_id} to avoid "models" being interpreted as a bot_id
  • Uses FastAPI dependency injection (Depends()) for BotManager to enable testability
  • Environment variables configure host, port, game server URLs, model registry path, and inference device
  • Invalid BOT_SERVICE_PORT values log an error message before raising

Testing

  • Added 30 unit tests in test_bot_service.py covering:
    • All endpoint success and error cases
    • Request validation (422 responses for invalid input)
    • Route order verification
    • BotManager not initialized error handling
    • URL encoding edge cases (spaces, path traversal attempts)
    • CLI entry point with environment variable loading
  • All 1192 project tests pass
  • Linting, formatting, and type checking pass

Additional Notes

@tgrunnagle tgrunnagle merged commit 6d90753 into main Feb 1, 2026
5 checks passed
@tgrunnagle tgrunnagle deleted the issue_99_bot_service_2026-01-27 branch February 1, 2026 22:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

FastAPI Bot Service

1 participant