AI Architecture and Agents
Relevant source files
The following files were used as context for generating this wiki page:
The AI architecture in Vinesight is designed to transform the application from a simple record-keeping tool into a proactive, intelligent digital companion for grape farmers. It integrates multiple AI providers and technologies to deliver features ranging from real-time disease detection to personalized crop advisories and market intelligence. The system is developed in phases, with core AI capabilities already implemented and advanced intelligence features currently in development.
The architecture leverages a combination of client-side processing for immediate feedback (e.g., computer vision) and server-side AI for more complex, data-intensive tasks (e.g., conversational AI, predictive analytics). The goal is to create a system that learns from farmer interactions and farm-specific data to provide increasingly accurate and personalized guidance, ultimately improving yield, profitability, and sustainability.
Sources: context/PRD.md:70-188, AGENTS.md:14-15, CLAUDE.md:19-25
High-Level Architecture
The AI system is built upon a multi-provider strategy, utilizing different services for specific tasks. TensorFlow.js is used for client-side computer vision, while various cloud-based AI providers like OpenAI, Google AI, and Groq are used for conversational AI and other complex analytics. Data is persisted in a Supabase PostgreSQL database, which includes specialized schemas for advanced AI features.
Sources: AGENTS.md:14-15, context/PRD.md:200-207
The following diagram illustrates the high-level components of the AI architecture.
AI Feature Phases
The development of AI features is structured into distinct phases. Phase 2 is complete, while Phase 3 is currently in development.
| Phase | Status | Key Features |
|---|---|---|
| Phase 2 | ✅ Completed | AI-Powered Disease Detection, AI Chatbot Assistant, AI Analytics Dashboard, Market Intelligence, IoT Sensor Integration, Voice Interface. |
| Phase 3 | 🧠 In Development | Personalized Crop Advisory, Smart Task Generator, AI-Powered Pest & Disease Alerts, Expense-to-Profitability Insights, Enhanced AI Chat Assistant with Long-Term Memory, Community Learning Platform. |
Sources: context/PRD.md:70-188, context/todos.md:5-7, CLAUDE.md:13-25
Phase 2: Implemented AI Features
AI Chatbot Assistant
The AI Chatbot is the primary user interface for interacting with the AI system. It is implemented in the AIAssistant.tsx component.
Sources: src/components/ai/AIAssistant.tsx
Key functionalities include:
- Conversational AI: Uses an API endpoint (
/api/ai/chat) which interfaces with providers like OpenAI to generate responses. - Context Awareness: The
buildConversationContextfunction compiles recent messages, farm data, and analysis results to provide context-aware answers. - Multi-language Support: Supports English, Hindi, and Marathi, adapting both UI and speech recognition (
en-IN,hi-IN,mr-IN). - Voice Integration: Utilizes the browser's Web Speech API (
webkitSpeechRecognition) for voice-to-text input. - Conversation History: Conversations are persisted using
supabaseConversationStorage, allowing users to load and delete past conversations. - Streaming Responses: Fetches and displays AI responses in real-time as they are generated.
Sources: src/components/ai/AIAssistant.tsx:112-118, 163-181, 246-271, 396-421
Message Handling Flow
The following sequence diagram shows the process of a user sending a message and receiving a response from the AI assistant.
AI-Powered Disease Detection
This feature uses TensorFlow.js for client-side computer vision to identify grape diseases from images uploaded by the user. It enables real-time plant health assessment directly on the user's device.
Sources: context/PRD.md:72-77, AGENTS.md:15
Phase 3: Advanced AI Intelligence (In Development)
Phase 3 aims to evolve the AI from a reactive tool to a proactive advisor by introducing personalization, predictive capabilities, and long-term memory. This is supported by a new database schema.
Sources: CLAUDE.md:19-25, context/todos.md:5-7
Advanced AI Database Schema
The foundation of Phase 3 is a set of new tables in the Supabase PostgreSQL database designed to store data for personalization, predictions, and analytics.
Sources: supabase/migrations/20250903_phase3_ai_schema.sql, CLAUDE.md:29-87
The entity-relationship diagram below outlines the schema for the advanced AI features.
Key AI Tables
| Table Name | Purpose |
|---|---|
farmer_ai_profiles | Stores personalized AI profiles for each farmer to enable adaptive recommendations, tracking risk tolerance and decision patterns. |
ai_task_recommendations | Holds AI-generated tasks (e.g., irrigation, spraying) with priority scores, reasoning, and status tracking. |
pest_disease_predictions | Contains weather-based pest/disease risk predictions, including probability scores and recommended treatments. |
profitability_analyses | Stores results of expense analysis, ROI calculations, and cost optimization opportunities. |
market_intelligence | Aggregates market price data, forecasts, and optimal selling window recommendations. |
community_insights | Anonymously shares successful farming practices and outcomes from the community to foster collective learning. |
ai_conversation_context | Implements long-term memory for the AI assistant by storing key context from conversations with a relevance decay mechanism. |
Sources: supabase/migrations/20250903_phase3_ai_schema.sql:205-211, CLAUDE.md:29-87
Advanced AI Data Structures
To support the new database schema, corresponding TypeScript interfaces are defined for use throughout the application.
Sources: context/TYPES_AND_INTERFACES.md:105-314, CLAUDE.md:90-348
FarmerAIProfile Interface
This interface models the data used for personalizing AI interactions.
// Enhanced Farmer AI Profile
export interface FarmerAIProfile {
id: string;
userId: string;
farmId: number;
riskTolerance: number; // 0-1 scale
decisionPatterns: {
preferredTiming: string;
riskAversion: number;
adoptionSpeed: 'conservative' | 'moderate' | 'early_adopter';
communicationStyle: 'direct' | 'detailed' | 'visual';
};
successMetrics: {
averageYield: number;
costEfficiency: number;
profitability: number;
};
// ... and other properties
}
Sources: context/TYPES_AND_INTERFACES.md:107-127, CLAUDE.md:92-113
AITaskRecommendation Interface
This interface defines the structure for tasks generated by the AI.
// Smart Task Recommendation
export interface AITaskRecommendation {
id: string;
farmId: number;
userId: string;
taskType: 'irrigation' | 'spray' | 'harvest' | 'fertigation' | 'pruning' | 'soil_test';
recommendedDate: Date;
priorityScore: number; // 0-1
reasoning: string; // AI explanation
confidenceScore: number; // 0-1
status: 'pending' | 'accepted' | 'rejected' | 'completed';
// ... and other properties
}
Sources: context/TYPES_AND_INTERFACES.md:130-153, CLAUDE.md:116-139
AI Conversation Long-Term Memory
A key feature of Phase 3 is enhancing the AI assistant with long-term memory. This is achieved by storing contextual information from conversations in the ai_conversation_context table. The AIAssistant.tsx component is responsible for extracting this context and including it in subsequent API calls. The relevance of stored context decays over time to ensure that recent information is prioritized.
Sources: CLAUDE.md:81-87, 502-526, supabase/migrations/20250903_phase3_ai_schema.sql:95-101, 191-203
Summary
The AI architecture of Vinesight is a sophisticated, multi-phased system designed to provide intelligent and proactive support to grape farmers. Phase 2 has established a foundation with a multi-lingual, context-aware AI assistant and client-side computer vision. The ongoing development of Phase 3 introduces a robust backend infrastructure for personalization, predictive analytics, and long-term memory. This will enable the system to deliver highly tailored advice, predict risks, optimize farm management, and learn from both individual and community data, positioning Vinesight as a powerful AI-driven agronomist.