cevheri commited on
Commit
95d6173
·
1 Parent(s): 80aa96a

docs: add cursor rules

Browse files
.cursor/rules/ai-agent-workflow.mdc ADDED
@@ -0,0 +1,81 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ description:
3
+ globs:
4
+ alwaysApply: false
5
+ ---
6
+ # AI Agent Workflow
7
+
8
+ This document outlines the workflow of the AI agent system and its interaction with various components.
9
+
10
+ ## Agent Interaction Flow
11
+
12
+ ### 1. User Input Processing
13
+ - User submits natural language query through [app.py](mdc:app.py)
14
+ - Query is processed by LangChain agent
15
+ - Context is retrieved from memory store
16
+ - System prompt is generated with relevant tools and database context
17
+
18
+ ### 2. Tool Selection & Execution
19
+ - Agent analyzes query intent
20
+ - Selects appropriate MCP tools from [postgre_mcp_server.py](mdc:postgre_mcp_server.py)
21
+ - Tools available:
22
+ - `execute_query`: For SQL query execution
23
+ - `visualize_results`: For data visualization
24
+ - `list_tables`: For schema exploration
25
+ - `get_table_schema`: For detailed table information
26
+ - `find_relationships`: For relationship analysis
27
+
28
+ ### 3. Response Generation
29
+ - Tool execution results are processed
30
+ - If visualization requested:
31
+ - Data is formatted for PandasAI
32
+ - Visualization is generated
33
+ - Image is converted to base64 for display
34
+ - Response is formatted with:
35
+ - Query results
36
+ - Visualization (if applicable)
37
+ - Explanation of results
38
+ - SQL query used
39
+
40
+ ## Memory Management
41
+
42
+ ### Conversation History
43
+ - Implemented in [memory_store.py](mdc:memory_store.py)
44
+ - Maintains context between interactions
45
+ - Stores:
46
+ - User messages
47
+ - AI responses
48
+ - Tool execution results
49
+ - Supports context clearing with `/clear-cache` command
50
+
51
+ ## Error Handling
52
+
53
+ ### Common Scenarios
54
+ - Invalid SQL queries
55
+ - Database connection issues
56
+ - Visualization generation failures
57
+ - Tool execution errors
58
+ - Memory management issues
59
+
60
+ ### Recovery Strategies
61
+ - Graceful error messages
62
+ - Fallback responses
63
+ - Automatic retry mechanisms
64
+ - Context preservation
65
+ - User-friendly error explanations
66
+
67
+ ## Performance Considerations
68
+
69
+ ### Optimization Techniques
70
+ - Async operations for database queries
71
+ - Efficient memory usage
72
+ - Caching of common queries
73
+ - Resource cleanup
74
+ - Connection pooling
75
+
76
+ ### Monitoring
77
+ - Logging of operations
78
+ - Performance metrics
79
+ - Error tracking
80
+ - Resource utilization
81
+ - Response time monitoring
.cursor/rules/coding-standards.mdc ADDED
@@ -0,0 +1,97 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ description:
3
+ globs:
4
+ alwaysApply: false
5
+ ---
6
+ # Coding Standards
7
+
8
+ This document outlines the coding standards for the AI-powered database interface, emphasizing the use of English in all code-related content and adherence to SOLID principles, Clean Code, and Design Patterns.
9
+
10
+ ## Language Consistency
11
+
12
+ ### Comments and Notes
13
+ - All comments and notes must be written in English.
14
+ - Avoid using any language other than English in code comments.
15
+ - Ensure clarity and conciseness in comments to aid understanding.
16
+
17
+ ### Variable and Function Naming
18
+ - Use English for all variable and function names.
19
+ - Follow a consistent naming convention (e.g., camelCase, snake_case).
20
+ - Avoid using non-English characters or words in identifiers.
21
+
22
+ ### Documentation
23
+ - All documentation, including README files and inline documentation, should be in English.
24
+ - Ensure that all code examples and explanations are clear and accessible to English-speaking developers.
25
+
26
+ ## Best Practices
27
+
28
+ ### Code Readability
29
+ - Write clear and descriptive comments.
30
+ - Use meaningful variable and function names.
31
+ - Maintain a consistent style throughout the codebase.
32
+
33
+ ### Collaboration
34
+ - Encourage team members to adhere to these standards.
35
+ - Regular code reviews to ensure compliance with language consistency.
36
+ - Provide feedback and support for maintaining these standards.
37
+
38
+ ## SOLID Principles
39
+
40
+ ### Single Responsibility Principle (SRP)
41
+ - Each class should have only one reason to change.
42
+ - Ensure that classes are focused on a single functionality.
43
+
44
+ ### Open/Closed Principle (OCP)
45
+ - Software entities should be open for extension but closed for modification.
46
+ - Use inheritance and polymorphism to extend functionality.
47
+
48
+ ### Liskov Substitution Principle (LSP)
49
+ - Subtypes must be substitutable for their base types.
50
+ - Ensure that derived classes can replace base classes without affecting the correctness of the program.
51
+
52
+ ### Interface Segregation Principle (ISP)
53
+ - Clients should not be forced to depend on interfaces they do not use.
54
+ - Design interfaces to be specific to client needs.
55
+
56
+ ### Dependency Inversion Principle (DIP)
57
+ - High-level modules should not depend on low-level modules. Both should depend on abstractions.
58
+ - Use dependency injection to manage dependencies.
59
+
60
+ ## Clean Code
61
+
62
+ ### Meaningful Names
63
+ - Use descriptive names for variables, functions, and classes.
64
+ - Avoid abbreviations and unclear names.
65
+
66
+ ### Functions
67
+ - Functions should be small and do one thing.
68
+ - Use descriptive names and avoid side effects.
69
+
70
+ ### Comments
71
+ - Comments should explain why, not what.
72
+ - Avoid unnecessary comments and ensure they are up-to-date.
73
+
74
+ ### Formatting
75
+ - Maintain consistent formatting and indentation.
76
+ - Use whitespace effectively to improve readability.
77
+
78
+ ## Design Patterns
79
+
80
+ ### Creational Patterns
81
+ - Use patterns like Singleton, Factory, and Builder to manage object creation.
82
+
83
+ ### Structural Patterns
84
+ - Use patterns like Adapter, Bridge, and Composite to manage relationships between objects.
85
+
86
+ ### Behavioral Patterns
87
+ - Use patterns like Observer, Strategy, and Command to manage communication between objects.
88
+
89
+ ## Compliance
90
+
91
+ ### Review Process
92
+ - Regular audits of the codebase to ensure adherence to language standards and design principles.
93
+ - Address any deviations promptly to maintain consistency.
94
+
95
+ ### Training
96
+ - Provide training and resources to help team members understand and follow these standards.
97
+ - Encourage continuous learning and improvement in coding practices.
.cursor/rules/database-query-guidelines.mdc ADDED
@@ -0,0 +1,62 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ description:
3
+ globs:
4
+ alwaysApply: false
5
+ ---
6
+ # Database Query Guidelines
7
+
8
+ This document outlines the guidelines for executing database queries and interacting with the database in the AI-powered interface.
9
+
10
+ ## Query Execution
11
+
12
+ ### SQL Query Generation
13
+ - Implemented in [postgre_mcp_server.py](mdc:postgre_mcp_server.py)
14
+ - Supports read-only SQL queries (SELECT, COUNT, GROUP BY, ORDER BY)
15
+ - Destructive operations (DELETE, UPDATE, INSERT, DROP) are not allowed
16
+ - SQL syntax validation before execution
17
+ - Query results are formatted for display
18
+
19
+ ### Database Connection
20
+ - Asynchronous connection management using AsyncPG
21
+ - Connection pooling for efficient resource utilization
22
+ - Error handling for connection issues
23
+ - Secure database URL management
24
+
25
+ ## Best Practices
26
+
27
+ ### Query Design
28
+ - Use explicit column names instead of *
29
+ - Include LIMIT clauses to restrict result sets
30
+ - Add WHERE clauses to filter results
31
+ - Consider indexing for performance
32
+ - Format SQL with proper indentation and line breaks
33
+
34
+ ### Performance Optimization
35
+ - Efficient use of connection pooling
36
+ - Asynchronous query execution
37
+ - Proper error handling and logging
38
+ - Resource cleanup after query execution
39
+
40
+ ## Error Handling
41
+
42
+ ### Common Issues
43
+ - Invalid SQL syntax
44
+ - Table or column not found
45
+ - Connection failures
46
+ - Query timeout
47
+ - Resource constraints
48
+
49
+ ### Recovery Strategies
50
+ - Graceful error messages
51
+ - Fallback responses
52
+ - Automatic retry mechanisms
53
+ - User-friendly error explanations
54
+
55
+ ## Monitoring
56
+
57
+ ### Logging
58
+ - Query execution logs
59
+ - Performance metrics
60
+ - Error tracking
61
+ - Resource utilization
62
+ - Response time monitoring
.cursor/rules/security-guidelines.mdc ADDED
@@ -0,0 +1,64 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ description:
3
+ globs:
4
+ alwaysApply: false
5
+ ---
6
+ # Security Guidelines
7
+
8
+ This document outlines the security practices and guidelines for the AI-powered database interface.
9
+
10
+ ## API Key Management
11
+
12
+ ### Secure Storage
13
+ - API keys are stored in environment variables
14
+ - Use of `.env` files for local development
15
+ - Secure handling of API keys in production
16
+ - Regular rotation of API keys
17
+
18
+ ### Access Control
19
+ - Read-only database operations
20
+ - No destructive SQL operations
21
+ - Secure database URL management
22
+ - User authentication and authorization
23
+
24
+ ## Best Practices
25
+
26
+ ### Data Security
27
+ - Encrypt sensitive data
28
+ - Use secure connections for database access
29
+ - Implement proper error handling to avoid information leakage
30
+ - Regular security audits and updates
31
+
32
+ ### Code Security
33
+ - Avoid hardcoding sensitive information
34
+ - Use secure coding practices
35
+ - Regular code reviews for security vulnerabilities
36
+ - Implement logging and monitoring for suspicious activities
37
+
38
+ ## Error Handling
39
+
40
+ ### Security Issues
41
+ - Unauthorized access attempts
42
+ - API key exposure
43
+ - Database connection breaches
44
+ - Resource misuse
45
+
46
+ ### Recovery Strategies
47
+ - Immediate revocation of compromised keys
48
+ - Logging of security incidents
49
+ - User notification of security breaches
50
+ - Regular security training and updates
51
+
52
+ ## Monitoring
53
+
54
+ ### Security Logging
55
+ - Access logs
56
+ - Error logs
57
+ - Security incident logs
58
+ - Resource usage logs
59
+
60
+ ### Incident Response
61
+ - Immediate action on security incidents
62
+ - Regular incident response drills
63
+ - User communication during incidents
64
+ - Post-incident analysis and learning
.cursor/rules/technical-architecture.mdc ADDED
@@ -0,0 +1,79 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ description:
3
+ globs:
4
+ alwaysApply: false
5
+ ---
6
+ # Technical Architecture
7
+
8
+ This project implements an AI-powered database query interface using natural language processing and visualization capabilities.
9
+
10
+ ## Core Components
11
+
12
+ ### AI Agent & Chatbot
13
+ - [app.py](mdc:app.py): Implements the Gradio web interface and orchestrates the AI agent
14
+ - Uses LangChain for AI agent orchestration
15
+ - Implements chat history management
16
+ - Handles visualization requests
17
+ - Manages user interactions through Gradio UI
18
+
19
+ ### MCP Server Implementation
20
+ - [postgre_mcp_server.py](mdc:postgre_mcp_server.py): Core MCP (Model Context Protocol) server
21
+ - Implements FastMCP for efficient model-context communication
22
+ - Provides database connection management
23
+ - Implements SQL query execution tools
24
+ - Handles data visualization requests
25
+ - Manages database schema information
26
+
27
+ ### LangChain Integration
28
+ - [langchain_mcp_client.py](mdc:langchain_mcp_client.py): LangChain-MCP integration layer
29
+ - Connects LangChain agents with MCP tools
30
+ - Manages conversation context
31
+ - Handles tool execution and response formatting
32
+ - Implements memory management for conversations
33
+
34
+ ## Key Technologies
35
+
36
+ ### AI & ML Stack
37
+ - LangChain: For AI agent orchestration and tool management
38
+ - OpenAI/Gemini: LLM providers for natural language understanding
39
+ - PandasAI: For data visualization and analysis
40
+
41
+ ### Web & UI
42
+ - Gradio: For building the web interface
43
+ - Custom CSS: For UI styling and responsiveness
44
+
45
+ ### Database & Data Processing
46
+ - PostgreSQL: Primary database
47
+ - Pandas: For data manipulation and analysis
48
+ - AsyncPG: For asynchronous database operations
49
+
50
+ ## Architecture Patterns
51
+
52
+ ### Model-Context Protocol (MCP)
53
+ - FastMCP implementation for efficient model-context communication
54
+ - Tool-based architecture for extensible functionality
55
+ - Context-aware request handling
56
+ - Resource management and lifecycle control
57
+
58
+ ### AI Agent Architecture
59
+ - ReAct pattern implementation
60
+ - Tool-based reasoning
61
+ - Context-aware memory management
62
+ - Natural language to SQL conversion
63
+ - Visualization request handling
64
+
65
+ ## Development Guidelines
66
+
67
+ ### Code Organization
68
+ - Modular tool implementation
69
+ - Clear separation of concerns
70
+ - Async-first approach
71
+ - Error handling and logging
72
+ - Type hints and documentation
73
+
74
+ ### Best Practices
75
+ - Read-only database operations
76
+ - Secure API key management
77
+ - Efficient resource utilization
78
+ - Proper error handling
79
+ - Comprehensive logging
.cursor/rules/visualization-guidelines.mdc ADDED
@@ -0,0 +1,106 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ description:
3
+ globs:
4
+ alwaysApply: false
5
+ ---
6
+ # Visualization Guidelines
7
+
8
+ This document outlines the visualization capabilities and best practices for the AI-powered database interface.
9
+
10
+ ## Visualization Components
11
+
12
+ ### PandasAI Integration
13
+ - Implemented in [postgre_mcp_server.py](mdc:postgre_mcp_server.py)
14
+ - Uses OpenAI/Gemini for visualization generation
15
+ - Supports multiple chart types:
16
+ - Bar charts
17
+ - Line charts
18
+ - Pie charts
19
+ - Scatter plots
20
+ - Box plots
21
+
22
+ ### Data Processing
23
+ - Data formatting in [app.py](mdc:app.py)
24
+ - JSON to DataFrame conversion
25
+ - Column type handling
26
+ - Data cleaning and preparation
27
+ - Long text truncation
28
+
29
+ ## Visualization Workflow
30
+
31
+ ### 1. Request Processing
32
+ - Natural language visualization request
33
+ - Data extraction from query results
34
+ - JSON data formatting
35
+ - Visualization prompt generation
36
+
37
+ ### 2. Chart Generation
38
+ - PandasAI initialization
39
+ - LLM-based chart type selection
40
+ - Customization parameters:
41
+ - Colors
42
+ - Labels
43
+ - Legends
44
+ - Axis formatting
45
+ - Title and description
46
+
47
+ ### 3. Output Handling
48
+ - Image file generation
49
+ - Base64 encoding for web display
50
+ - Temporary file management
51
+ - Cleanup procedures
52
+
53
+ ## Best Practices
54
+
55
+ ### Data Preparation
56
+ - Appropriate data types
57
+ - Missing value handling
58
+ - Outlier management
59
+ - Data aggregation
60
+ - Column selection
61
+
62
+ ### Visualization Design
63
+ - Clear labels and titles
64
+ - Appropriate chart types
65
+ - Color scheme consistency
66
+ - Legend placement
67
+ - Axis formatting
68
+
69
+ ### Performance
70
+ - Efficient data processing
71
+ - Memory management
72
+ - File cleanup
73
+ - Caching strategies
74
+ - Resource optimization
75
+
76
+ ## Common Use Cases
77
+
78
+ ### Business Analytics
79
+ - Sales trends
80
+ - Customer distribution
81
+ - Product performance
82
+ - Time series analysis
83
+ - Comparative analysis
84
+
85
+ ### Data Exploration
86
+ - Distribution analysis
87
+ - Correlation visualization
88
+ - Pattern identification
89
+ - Anomaly detection
90
+ - Trend analysis
91
+
92
+ ## Error Handling
93
+
94
+ ### Common Issues
95
+ - Data format errors
96
+ - Visualization generation failures
97
+ - Memory constraints
98
+ - File system issues
99
+ - API limitations
100
+
101
+ ### Recovery Strategies
102
+ - Fallback visualizations
103
+ - Error messages
104
+ - Data validation
105
+ - Resource management
106
+ - User feedback