Overview
The Chat API provides a conversational interface powered by Google Gemini for querying tracking data using natural language. The AI agent has access to tools that query movements, drivers, tractors, and trailers from the database. AI Model: Google Gemini (configurable viaGEMINI_MODEL environment variable)
Service: ChatAgentService singleton
Send Chat Message
POST /api/chat
Send a natural language message to the AI agent and receive a response. Request Body:| Field | Type | Required | Description |
|---|---|---|---|
message | string | Yes | Natural language query or message |
session_id | string | No | Session ID for conversation continuity. If not provided, a new session is created. |
| Field | Type | Description |
|---|---|---|
response | string | AI-generated response text |
session_id | string | Session ID (use for follow-up messages) |
tool_calls | array | List of tools called by the AI, or null if none |
error | string | Error code if request failed (e.g., quota_exceeded) |
How It Works
1. Message Processing
- Session Management: Gets or creates a chat session for conversation continuity
- Message Send: Sends user message to Gemini model
- Function Calling Loop: Executes up to 10 function calls to gather data
- Response Generation: Gemini generates natural language response from tool results
2. Available Tools
The AI agent has access to these database query tools:get_movements_tool
get_movements_tool
Description: Get movements (shipments/loads) with nested stops from database.Parameters:
status(optional): Status filter (e.g.,"P"for Progress/active)limit(optional): Maximum movements to return (default: 100)
movements table with LEFT JOIN to movement_stops and tractorsget_drivers_tool
get_drivers_tool
Description: Get drivers snapshot from database with location, HOS status, and contact information.Parameters:
limit(optional): Maximum drivers to returnsince(optional): ISO 8601 timestamp filter
drivers table via get_drivers_snapshot()get_tractors_tool
get_tractors_tool
Description: Get tractors (trucks/power units) from database with location and speed.Parameters:
limit(optional): Maximum tractors to return
tractors table via get_latest_vehicle_locations()get_trailers_tool
get_trailers_tool
Description: Get trailers from database with location and metadata.Parameters:
limit(optional): Maximum trailers to return
trailers2 table via get_latest_trailer_locations()check_driver_on_movement_tool
check_driver_on_movement_tool
Description: Check if a driver is currently on an active movement by searching by name.Parameters:
driver_name(required): Driver name to search (partial match, case-insensitive)
drivers and movements tablesget_movements_by_destination_state_tool
get_movements_by_destination_state_tool
Description: Get movements with stops in a specific state (optimized query).Parameters:
state(required): Two-letter state code (e.g.,"CA","TX")status(optional): Movement status filter (default:"P")
movements JOIN movement_stops with state filterget_driver_names_from_mcleod_tool
get_driver_names_from_mcleod_tool
Description: Get driver names from McLeod API by driver IDs.Parameters:
driver_ids(required): Comma-separated McLeod driver IDs (e.g.,"S9932,S8170")
/v1/driver?id={driver_id} for each ID3. Multi-Step Reasoning
The AI agent can make multiple tool calls to answer complex questions. Examples: Query: “How many drivers named Charles are on a movement?” Steps:get_drivers_tool()→ Get all drivers- Filter results for “Charles” in name
get_movements_tool(status='P')→ Get active movements- Cross-reference driver
source_idwith movement__driverids - Report matches
get_movements_tool()→ Find movement 12345- Extract
tractor_idfrom movement get_tractor_by_mcleod_id_tool(tractor_id)→ Get tractor location- Report location
Example Queries
Here are example natural language queries the AI can answer:| Query | Tools Used |
|---|---|
| ”How many active movements are there?” | get_movements_tool |
| ”Where is driver John Smith?” | get_drivers_tool |
| ”What tractors have fault codes?” | get_tractors_tool, get_tractor_fault_codes_tool |
| ”Which movements are going to California?” | get_movements_by_destination_state_tool |
| ”Is Charles on a movement right now?” | check_driver_on_movement_tool |
| ”What’s the status of movement 762965?” | get_movements_tool |
| ”How many trailers are in Texas?” | get_trailers_tool |
| ”List all drivers on movements to NY” | get_movements_by_destination_state_tool, get_driver_names_from_mcleod_tool |
Session Management
Clear Session
DELETE /api/chat/session/
Clear a specific chat session and its history. Response:Get Session Stats
GET /api/chat/stats
Get statistics about active chat sessions. Response:Configuration
| Environment Variable | Default | Description |
|---|---|---|
GEMINI_API_KEY | (required) | Google Gemini API key |
GEMINI_MODEL | gemini-1.5-pro | Gemini model to use |

