File size: 4,266 Bytes
bed5cc5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
"""
Cursor Rules Generator - Rule Generator

This module implements the rule generation engine.
"""

from typing import Dict, List, Optional, Any

from ..llm.factory import LLMAdapterFactory
from ..config.settings import Settings
from .mdc_formatter import MDCFormatter

class RuleGenerator:
    """Engine for generating Cursor Rules."""
    
    def __init__(self):
        """Initialize the rule generator."""
        self.formatter = MDCFormatter()
        self.factory = LLMAdapterFactory()
    
    def create_rule(
        self,
        provider: str,
        model: str,
        rule_type: str,
        description: str,
        content: str,
        api_key: str,
        parameters: Optional[Dict[str, Any]] = None
    ) -> str:
        """Create a Cursor Rule using the specified LLM provider.
        
        Args:
            provider: The LLM provider to use
            model: The model ID to use
            rule_type: The type of rule to generate
            description: A short description of the rule's purpose
            content: The main content of the rule
            api_key: The API key for the LLM provider
            parameters: Additional parameters for rule generation
                
        Returns:
            str: The generated rule in MDC format
            
        Raises:
            ValueError: If the provider is not supported or initialization fails
        """
        # Set default parameters if not provided
        if parameters is None:
            parameters = {}
        
        try:
            # Create and initialize the adapter
            adapter = self.factory.create_adapter(provider)
            adapter.initialize(api_key)
            
            # Generate the rule using the adapter
            rule = adapter.generate_rule(model, rule_type, description, content, parameters)
            
            return rule
        except Exception as e:
            # If LLM generation fails, create a basic rule
            return self._create_basic_rule(rule_type, description, content, parameters)
    
    def _create_basic_rule(
        self,
        rule_type: str,
        description: str,
        content: str,
        parameters: Optional[Dict[str, Any]] = None
    ) -> str:
        """Create a basic rule in MDC format without using an LLM.
        
        Args:
            rule_type: The type of rule
            description: The rule description
            content: The rule content
            parameters: Additional parameters
            
        Returns:
            str: The rule in MDC format
        """
        # Set default parameters if not provided
        if parameters is None:
            parameters = {}
        
        # Format the rule using the MDC formatter
        return self.formatter.format_to_mdc(rule_type, content, description, parameters)
    
    def validate_rule_type(self, rule_type: str) -> bool:
        """Validate if the rule type is supported.
        
        Args:
            rule_type: The rule type to validate
            
        Returns:
            bool: True if the rule type is supported, False otherwise
        """
        valid_types = ['Always', 'Auto Attached', 'Agent Requested', 'Manual']
        return rule_type in valid_types
    
    def get_rule_types(self) -> List[Dict[str, str]]:
        """Get a list of supported rule types.
        
        Returns:
            List[Dict[str, str]]: A list of rule type information dictionaries
        """
        return [
            {
                'id': 'Always',
                'name': 'Always',
                'description': 'Always included in the model context'
            },
            {
                'id': 'Auto Attached',
                'name': 'Auto Attached',
                'description': 'Included when files matching glob patterns are referenced'
            },
            {
                'id': 'Agent Requested',
                'name': 'Agent Requested',
                'description': 'Rule is presented to the AI, which decides whether to include it'
            },
            {
                'id': 'Manual',
                'name': 'Manual',
                'description': 'Only included when explicitly referenced using @ruleName'
            }
        ]