aimevzulari's picture
Upload 19 files
bed5cc5 verified
"""
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'
}
]