Overview
The Gima AI Chatbot integrates with a Laravel backend (GIMA system) to query real maintenance data, assets, work orders, and inventory. The integration is handled by theBackendAPIService class.
Source: app/lib/services/backend-api-service.ts
Architecture
Configuration
Environment Variables
Set the backend URL in your.env.local:
- Validation: Must be a valid URL (checked at
app/config/env.ts:31) - Required: Yes (for production with real data)
- Used by:
createBackendAPIService()factory atapp/lib/services/backend-api-service.ts:300
If not configured, the service will throw a
BackendAPIError when instantiated.Authentication
Bearer Token Authentication
The service uses Laravel Sanctum for authentication:- Method: Bearer token in
Authorizationheader - Token injection: Per-instance (not global)
- Token source: Extracted from Next.js cookies in tool execute functions
app/lib/services/backend-api-service.ts:135-143
Authentication Errors
If the backend returns401 Unauthorized:
app/lib/services/backend-api-service.ts:90-95
Service Features
Automatic Pagination Unwrapping
Laravel returns paginated data inLengthAwarePaginator format. The service automatically unwraps it:
app/lib/services/backend-api-service.ts:202-220
Retry with Exponential Backoff
Transient errors are automatically retried:- Max retries: 2 (3 total attempts)
- Backoff: Exponential (1s, 2s)
- Retryable errors: Timeouts, 5xx errors, 408, 429
- Non-retryable: 4xx client errors (except 408, 429)
app/lib/services/backend-api-service.ts:128-196
Timeout Configuration
- Default: 8 seconds
- Reason: Margin for Vercel Edge runtime limits
- Configurable: Pass
timeoutMsto constructor
app/lib/services/backend-api-service.ts:60
If a request times out:
app/lib/services/backend-api-service.ts:79-88
API Endpoints
Catalog
Get Assets
- Endpoint:
/api/catalogo/activos - Alternate:
/api/catalogo/activos/por-categoria(whentipois provided) - Returns:
PaginatedResult<Activo> - Schema:
activoSchemafromapp/lib/schemas/backend-response.schema.ts
app/lib/services/backend-api-service.ts:240-249
Maintenance
Get Work Orders
- Endpoint:
/api/mantenimiento/mantenimientos - Returns:
PaginatedResult<Mantenimiento> - Schema:
mantenimientoSchema
app/lib/services/backend-api-service.ts:255-260
Get Maintenance Calendar
- Endpoint:
/api/mantenimiento/calendario - Returns:
PaginatedResult<CalendarioMantenimiento> - Schema:
calendarioSchema
app/lib/services/backend-api-service.ts:262-267
Get Reports
- Endpoint:
/api/mantenimiento/reportes - Returns:
PaginatedResult<Reporte> - Schema:
reporteSchema
app/lib/services/backend-api-service.ts:269-272
Inventory
Get Parts/Supplies
- Endpoint:
/api/inventario/repuestos - Returns:
PaginatedResult<Repuesto> - Schema:
repuestoSchema
app/lib/services/backend-api-service.ts:278-281
Get Suppliers
- Endpoint:
/api/inventario/proveedores - Returns:
PaginatedResult<Proveedor> - Schema:
proveedorSchema
app/lib/services/backend-api-service.ts:283-285
Error Handling
Custom Error Classes
BackendAPIError
app/lib/services/backend-api-service.ts:68-77
BackendTimeoutError
ExtendsBackendAPIError with timeout-specific messaging:
app/lib/services/backend-api-service.ts:79-88
BackendAuthError
ExtendsBackendAPIError for authentication failures:
app/lib/services/backend-api-service.ts:90-95
Error Flow
Usage Example
Creating Service Instance
With Custom Configuration
With Custom Fetch (for Testing)
Query String Building
The service automatically filters outundefined, null, and empty string values:
app/lib/services/backend-api-service.ts:225-234
Testing
Mocking the Service
Testing Error Handling
Best Practices
Token Per Request
Always create a new service instance per request with the user’s token. Never share instances.
Handle Timeouts
Implement user-facing messaging for timeouts and suggest more specific filters.
Use Type Safety
Leverage TypeScript types from schemas for compile-time safety.
Graceful Degradation
If backend is unavailable, fall back to cached data or demo mode.
Troubleshooting
401 Unauthorized Error
401 Unauthorized Error
Timeout Errors
Timeout Errors
Cause: Backend query taking too long (>8s).Solution:
- Add more specific filters to the query
- Optimize backend database queries
- Increase
timeoutMsif needed
BackendAPIError: NEXT_PUBLIC_BACKEND_API_URL not configured
BackendAPIError: NEXT_PUBLIC_BACKEND_API_URL not configured
Cause: Missing environment variable.Solution:
- Add
NEXT_PUBLIC_BACKEND_API_URLto.env.local - Verify it’s a valid URL
- Restart the dev server
CORS Errors
CORS Errors
Cause: Laravel not allowing requests from Next.js origin.Solution:
- Configure Laravel CORS middleware
- Add Next.js URL to allowed origins
- Ensure credentials are included in requests
Next Steps
Configuration
Set up the backend URL and authentication
Testing
Learn how to write tests for backend integration