ServerIntelligence
gRPC Server & Servicer
Service entry points, FastAPI app setup, and gRPC unary/streaming interface implementation.
gRPC Server & Servicer
The ForkFit intelligence core exposes its capabilities via a performant gRPC interface.
Implementation: main.py and servicer.py
1. Application Entry Point (main.py)
Initializes service dependencies and binds the servers.
- Database Pool: Sets up a connection pool (
asyncpg.Pool) targeting the PostgreSQL database. - LangGraph: Builds the agent execution graph.
- Server Handlers:
- gRPC Server: Binds the servicer to port
50051. - FastAPI Server: Binds a HTTP server to port
8000(provides/healthendpoints and API testing).
- gRPC Server: Binds the servicer to port
- Asynchronous Runner: Runs both servers concurrently inside a single event loop.
2. gRPC Servicer Implementation
Implements the gRPC service definition.
ProcessIntent
- Type: Unary-Unary
- Function: Classifies a raw user prompt into a structured intent blueprint (goal, diet, budget details).
- Handler Flow: Calls
classify_intentand returns provider parameters.
OrchestrateAgentGraph
- Type: Unary-Stream
- Function: Hydrates database context, triggers the LangGraph orchestrator, and streams real-time execution step updates, text tokens, and UI elements.
- Handler Flow:
- Hydrates context concurrently (via
_build_context). - Initiates step and token queues.
- Launches graph execution asynchronously (
self.graph.astream(...)). - Yields stream responses to the client as events occur:
- Step Updates: Yields
AgentStepUpdate(agent name, status, input/output JSON payloads, latency). - Text Deltas: Yields
TextDelta(tokens of the markdown summary as they are generated by the LLM). - UI Elements: Yields
UIElement(React component specs like charts and timelines).
- Step Updates: Yields
- Hydrates context concurrently (via
TriggerReflection
- Type: Unary-Unary
- Function: Asynchronously triggers the user profile learning loop on a conversation message rating.
- Handler Flow: Calls
run_reflectionto extract memories and updates Postgres tables.
3. Context Hydration
_build_context fetches user profile attributes concurrently using asyncio.gather:
- Fetch safety constraints, goal types, biometrics, profiles, recent workouts, and preferences.
- Generate a text embedding of the user prompt and query the database (
pgvector) to retrieve matching recipe and memory records. - Merge these details with request-supplied fields to compile the
UserContextPackageinjected into the orchestrator state.