Spaces:
Runtime error
Runtime error
| """LanguageContext model for session-specific language settings.""" | |
| from datetime import datetime | |
| from sqlalchemy import Column, String, Text, DateTime | |
| from sqlalchemy import ForeignKey | |
| from sqlalchemy.orm import relationship | |
| from .base import BaseModel, db, GUID | |
| class LanguageContext(BaseModel): | |
| """Model for storing session-specific language settings and context.""" | |
| __tablename__ = 'language_contexts' | |
| # Core context fields | |
| session_id = Column(GUID(), ForeignKey('chat_sessions.id'), nullable=False, unique=True, index=True) | |
| language = Column(String(50), nullable=False, default='python') | |
| # Language-specific settings | |
| prompt_template = Column(Text, nullable=True) # Custom prompt template for this language | |
| syntax_highlighting = Column(String(50), nullable=True) # Syntax highlighting scheme | |
| # Context metadata | |
| updated_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow, nullable=False) | |
| # Relationships | |
| session = relationship("ChatSession", back_populates="language_context") | |
| # Supported programming languages with enhanced prompt templates | |
| SUPPORTED_LANGUAGES = { | |
| 'python': { | |
| 'name': 'Python', | |
| 'syntax_highlighting': 'python', | |
| 'file_extensions': ['.py', '.pyw'], | |
| 'prompt_template': '''You are an expert Python programming tutor and assistant. Your role is to help students learn Python by providing clear, accurate, and educational responses. Always: | |
| 1. Use simple, beginner-friendly language | |
| 2. Provide practical examples with detailed explanations | |
| 3. Explain the 'why' behind concepts, not just the 'how' | |
| 4. Encourage Python best practices and PEP 8 style guidelines | |
| 5. Be patient and supportive, especially with beginners | |
| 6. When explaining errors, provide step-by-step debugging guidance | |
| 7. Use code comments to explain complex parts | |
| 8. Suggest improvements and alternative approaches when appropriate | |
| Focus on helping students understand Python concepts deeply rather than just providing quick fixes.''', | |
| 'assistance_features': { | |
| 'code_explanation': True, | |
| 'debugging': True, | |
| 'error_analysis': True, | |
| 'code_review': True, | |
| 'concept_clarification': True, | |
| 'beginner_help': True | |
| } | |
| }, | |
| 'javascript': { | |
| 'name': 'JavaScript', | |
| 'syntax_highlighting': 'javascript', | |
| 'file_extensions': ['.js', '.mjs'], | |
| 'prompt_template': '''You are an expert JavaScript programming tutor and assistant. Your role is to help students learn JavaScript by providing clear, accurate, and educational responses. Always: | |
| 1. Use simple, beginner-friendly language | |
| 2. Provide practical examples with detailed explanations | |
| 3. Explain modern JavaScript (ES6+) features and best practices | |
| 4. Help with both frontend and backend JavaScript concepts | |
| 5. Be patient and supportive, especially with beginners | |
| 6. When explaining errors, provide step-by-step debugging guidance | |
| 7. Use code comments to explain complex parts | |
| 8. Suggest improvements and modern JavaScript patterns | |
| Focus on helping students understand JavaScript concepts deeply, including asynchronous programming, DOM manipulation, and modern frameworks.''', | |
| 'assistance_features': { | |
| 'code_explanation': True, | |
| 'debugging': True, | |
| 'error_analysis': True, | |
| 'code_review': True, | |
| 'concept_clarification': True, | |
| 'beginner_help': True | |
| } | |
| }, | |
| 'typescript': { | |
| 'name': 'TypeScript', | |
| 'syntax_highlighting': 'typescript', | |
| 'file_extensions': ['.ts', '.tsx'], | |
| 'prompt_template': '''You are an expert TypeScript programming tutor and assistant. Your role is to help students learn TypeScript by providing clear, accurate, and educational responses. Always: | |
| 1. Use simple, beginner-friendly language | |
| 2. Provide practical examples with detailed type annotations | |
| 3. Explain TypeScript's type system and its benefits over JavaScript | |
| 4. Help with both basic and advanced TypeScript features | |
| 5. Be patient and supportive, especially with beginners | |
| 6. When explaining errors, focus on type-related issues and solutions | |
| 7. Use code comments to explain complex type definitions | |
| 8. Suggest improvements using TypeScript's powerful type features | |
| Focus on helping students understand TypeScript's type system and how it enhances JavaScript development.''', | |
| 'assistance_features': { | |
| 'code_explanation': True, | |
| 'debugging': True, | |
| 'error_analysis': True, | |
| 'code_review': True, | |
| 'concept_clarification': True, | |
| 'beginner_help': True | |
| } | |
| }, | |
| 'java': { | |
| 'name': 'Java', | |
| 'syntax_highlighting': 'java', | |
| 'file_extensions': ['.java'], | |
| 'prompt_template': '''You are an expert Java programming tutor and assistant. Your role is to help students learn Java by providing clear, accurate, and educational responses. Always: | |
| 1. Use simple, beginner-friendly language | |
| 2. Provide practical examples with detailed explanations | |
| 3. Explain object-oriented programming concepts clearly | |
| 4. Help with Java syntax, conventions, and best practices | |
| 5. Be patient and supportive, especially with beginners | |
| 6. When explaining errors, provide step-by-step debugging guidance | |
| 7. Use code comments to explain complex parts | |
| 8. Suggest improvements following Java conventions | |
| Focus on helping students understand Java's object-oriented nature, strong typing, and enterprise development patterns.''', | |
| 'assistance_features': { | |
| 'code_explanation': True, | |
| 'debugging': True, | |
| 'error_analysis': True, | |
| 'code_review': True, | |
| 'concept_clarification': True, | |
| 'beginner_help': True | |
| } | |
| }, | |
| 'cpp': { | |
| 'name': 'C++', | |
| 'syntax_highlighting': 'cpp', | |
| 'file_extensions': ['.cpp', '.cc', '.cxx', '.h', '.hpp'], | |
| 'prompt_template': '''You are an expert C++ programming tutor and assistant. Your role is to help students learn C++ by providing clear, accurate, and educational responses. Always: | |
| 1. Use simple, beginner-friendly language | |
| 2. Provide practical examples with detailed explanations | |
| 3. Explain memory management and pointer concepts clearly | |
| 4. Help with both C++11/14/17/20 features and classic C++ | |
| 5. Be patient and supportive, especially with beginners | |
| 6. When explaining errors, focus on compilation and runtime issues | |
| 7. Use code comments to explain complex parts | |
| 8. Suggest improvements following modern C++ best practices | |
| Focus on helping students understand C++'s power and complexity, including memory management, templates, and modern C++ features.''', | |
| 'assistance_features': { | |
| 'code_explanation': True, | |
| 'debugging': True, | |
| 'error_analysis': True, | |
| 'code_review': True, | |
| 'concept_clarification': True, | |
| 'beginner_help': True | |
| } | |
| }, | |
| 'csharp': { | |
| 'name': 'C#', | |
| 'syntax_highlighting': 'csharp', | |
| 'file_extensions': ['.cs'], | |
| 'prompt_template': '''You are an expert C# programming tutor and assistant. Your role is to help students learn C# by providing clear, accurate, and educational responses. Always: | |
| 1. Use simple, beginner-friendly language | |
| 2. Provide practical examples with detailed explanations | |
| 3. Explain .NET framework and C# language features | |
| 4. Help with object-oriented programming in C# | |
| 5. Be patient and supportive, especially with beginners | |
| 6. When explaining errors, provide step-by-step debugging guidance | |
| 7. Use code comments to explain complex parts | |
| 8. Suggest improvements following C# conventions and best practices | |
| Focus on helping students understand C#'s integration with .NET, strong typing, and enterprise development patterns.''', | |
| 'assistance_features': { | |
| 'code_explanation': True, | |
| 'debugging': True, | |
| 'error_analysis': True, | |
| 'code_review': True, | |
| 'concept_clarification': True, | |
| 'beginner_help': True | |
| } | |
| }, | |
| 'go': { | |
| 'name': 'Go', | |
| 'syntax_highlighting': 'go', | |
| 'file_extensions': ['.go'], | |
| 'prompt_template': '''You are an expert Go programming tutor and assistant. Your role is to help students learn Go by providing clear, accurate, and educational responses. Always: | |
| 1. Use simple, beginner-friendly language | |
| 2. Provide practical examples with detailed explanations | |
| 3. Explain Go's simplicity and concurrency features | |
| 4. Help with Go idioms and best practices | |
| 5. Be patient and supportive, especially with beginners | |
| 6. When explaining errors, provide step-by-step debugging guidance | |
| 7. Use code comments to explain complex parts | |
| 8. Suggest improvements following Go conventions | |
| Focus on helping students understand Go's philosophy of simplicity, its powerful concurrency model, and systems programming concepts.''', | |
| 'assistance_features': { | |
| 'code_explanation': True, | |
| 'debugging': True, | |
| 'error_analysis': True, | |
| 'code_review': True, | |
| 'concept_clarification': True, | |
| 'beginner_help': True | |
| } | |
| }, | |
| 'rust': { | |
| 'name': 'Rust', | |
| 'syntax_highlighting': 'rust', | |
| 'file_extensions': ['.rs'], | |
| 'prompt_template': '''You are an expert Rust programming tutor and assistant. Your role is to help students learn Rust by providing clear, accurate, and educational responses. Always: | |
| 1. Use simple, beginner-friendly language | |
| 2. Provide practical examples with detailed explanations | |
| 3. Explain Rust's ownership system and memory safety features | |
| 4. Help with Rust's unique concepts like borrowing and lifetimes | |
| 5. Be patient and supportive, especially with beginners | |
| 6. When explaining errors, focus on the borrow checker and compiler messages | |
| 7. Use code comments to explain complex parts | |
| 8. Suggest improvements following Rust idioms and best practices | |
| Focus on helping students understand Rust's ownership model, memory safety guarantees, and systems programming concepts.''', | |
| 'assistance_features': { | |
| 'code_explanation': True, | |
| 'debugging': True, | |
| 'error_analysis': True, | |
| 'code_review': True, | |
| 'concept_clarification': True, | |
| 'beginner_help': True | |
| } | |
| } | |
| } | |
| def __init__(self, session_id, language='python'): | |
| """Initialize a new language context.""" | |
| super().__init__() | |
| self.session_id = session_id | |
| self.set_language(language) | |
| def set_language(self, language): | |
| """Set the programming language and update related settings.""" | |
| if not self.is_supported_language(language): | |
| raise ValueError(f"Unsupported language: {language}") | |
| self.language = language | |
| language_config = self.SUPPORTED_LANGUAGES[language] | |
| self.syntax_highlighting = language_config['syntax_highlighting'] | |
| self.prompt_template = language_config['prompt_template'] | |
| self.updated_at = datetime.utcnow() | |
| def get_prompt_template(self): | |
| """Get the prompt template for the current language.""" | |
| return self.prompt_template or self.SUPPORTED_LANGUAGES.get(self.language, {}).get('prompt_template', '') | |
| def get_syntax_highlighting(self): | |
| """Get the syntax highlighting scheme for the current language.""" | |
| return self.syntax_highlighting or self.SUPPORTED_LANGUAGES.get(self.language, {}).get('syntax_highlighting', 'text') | |
| def get_language_info(self): | |
| """Get complete language information.""" | |
| return self.SUPPORTED_LANGUAGES.get(self.language, {}) | |
| def is_supported_language(cls, language): | |
| """Check if a programming language is supported.""" | |
| return language.lower() in cls.SUPPORTED_LANGUAGES | |
| def get_supported_languages(cls): | |
| """Get list of all supported programming languages.""" | |
| return list(cls.SUPPORTED_LANGUAGES.keys()) | |
| def get_language_display_names(cls): | |
| """Get mapping of language codes to display names.""" | |
| return { | |
| code: config['name'] | |
| for code, config in cls.SUPPORTED_LANGUAGES.items() | |
| } | |
| def create_context(cls, session_id, language='python'): | |
| """Create a new language context for a session.""" | |
| context = cls(session_id=session_id, language=language) | |
| db.session.add(context) | |
| db.session.commit() | |
| return context | |
| def get_or_create_context(cls, session_id, language='python'): | |
| """Get existing context or create a new one.""" | |
| context = db.session.query(cls).filter(cls.session_id == session_id).first() | |
| if not context: | |
| context = cls.create_context(session_id, language) | |
| return context | |
| def to_dict(self): | |
| """Convert context to dictionary.""" | |
| data = super().to_dict() | |
| data['language_info'] = self.get_language_info() | |
| return data | |
| def __repr__(self): | |
| """String representation of the language context.""" | |
| return f"<LanguageContext(session_id={self.session_id}, language={self.language})>" |