Overview
The Flower Engine backend is built with FastAPI and handles:- WebSocket connections and message routing
- LLM integration and streaming responses
- SQLite database management
- RAG (Retrieval-Augmented Generation) via ChromaDB
- Session and state persistence
- Asset loading from YAML files
Technology Stack
Core dependencies fromrequirements.txt:
Key Libraries
- FastAPI: Async web framework
- Uvicorn: ASGI server
- Pydantic: Data validation and models
- ChromaDB: Vector database for RAG
- OpenAI SDK: LLM API client (supports OpenRouter, DeepSeek, Groq)
- SentenceTransformers: Local embeddings (
all-MiniLM-L6-v2) - Google GenAI: Official Gemini SDK
Running the Backend
Development Mode (with auto-reload)
--reload flag automatically restarts the server when code changes are detected.
Production Mode
Direct Execution
engine/main.py:266:
Code Style Guidelines
Imports
- Standard library first, then third-party, then local
- Use explicit relative imports:
from engine.logger import log - Group imports logically within files
Formatting
- 4 spaces indentation (not tabs)
- Maximum line length: 120 characters
- Use f-strings for string formatting
- Use trailing commas in multi-line structures
Type Hints
- Use Pydantic
BaseModelfor all data models - Use type hints on all function signatures
- Use
Optional[T]for nullable types - Prefer explicit types over
Any
Naming Conventions
- Modules:
snake_case.py - Classes:
PascalCase - Functions/variables:
snake_case - Constants:
UPPER_SNAKE_CASE - Private members: prefix with underscore
Error Handling
- Use try/except with specific exception types
- Log errors via
from engine.logger import log - Use bare
except:only when necessary (never in new code) - Propagate errors appropriately or return sensible defaults
Async Patterns
- Use
async deffor all WebSocket handlers - Use
asynciofor concurrent operations - Use
asyncio.create_task()for fire-and-forget background tasks - Handle
asyncio.CancelledErrorexplicitly
engine/main.py:222:
Key Components
Main Application (engine/main.py)
The entry point defines the FastAPI app and WebSocket endpoint:
Database (engine/database.py)
Uses SQLite with Pydantic models:
sqlite3.OperationalError.
Commands (engine/commands.py)
Command handlers parse slash commands:
parts = cmd_str.split(" ", 2) to get command, subcommand, and arguments.
LLM Streaming (engine/llm.py)
Streams responses from various LLM providers:
RAG (engine/rag.py)
Retrieval-Augmented Generation using ChromaDB:
Utilities (engine/utils.py)
Helper functions for common tasks:
Development Tips
Hot Reloading
Always use--reload during development:
Logging
Use the centralized logger fromengine/logger.py:
Testing WebSocket Connections
See the Testing Guide for usingengine/test_client.py to test WebSocket functionality.
Debugging
Add print statements or use the logger:State Management
Global state is managed inengine/state.py:
Common Patterns
Sending WebSocket Messages
Database Queries
Asset Loading
Next Steps
- Learn about Rust TUI development
- Explore the testing workflow
- Review the contributing overview