Add services, prompts, and utils modules
This commit is contained in:
428
local_code_assistant/prompts/templates.py
Normal file
428
local_code_assistant/prompts/templates.py
Normal file
@@ -0,0 +1,428 @@
|
|||||||
|
"""Prompt templates for Local Code Assistant."""
|
||||||
|
|
||||||
|
from typing import Optional
|
||||||
|
|
||||||
|
|
||||||
|
class PromptTemplates:
|
||||||
|
"""Collection of prompt templates for different operations."""
|
||||||
|
|
||||||
|
SYSTEM_PROMPT = """You are an expert AI coding assistant helping developers write better code.
|
||||||
|
Your role is to:
|
||||||
|
1. Generate clean, well-documented code
|
||||||
|
2. Explain code clearly and concisely
|
||||||
|
3. Refactor code for better performance and readability
|
||||||
|
4. Write comprehensive tests
|
||||||
|
|
||||||
|
Always:
|
||||||
|
- Provide complete, working code
|
||||||
|
- Include comments for complex logic
|
||||||
|
- Follow best practices for the specific language
|
||||||
|
- Consider edge cases and error handling
|
||||||
|
- Keep responses focused and practical
|
||||||
|
|
||||||
|
Never:
|
||||||
|
- Include any external API calls or network requests in generated code
|
||||||
|
- Use placeholder comments without actual implementation
|
||||||
|
- Generate code that could be harmful or insecure"""
|
||||||
|
|
||||||
|
CODE_GENERATION = """Generate {language} code that accomplishes the following task:
|
||||||
|
|
||||||
|
{user_prompt}
|
||||||
|
|
||||||
|
Requirements:
|
||||||
|
- Write clean, readable {language} code
|
||||||
|
- Include appropriate comments
|
||||||
|
- Handle potential errors
|
||||||
|
- Follow {language} best practices
|
||||||
|
|
||||||
|
{files_context}
|
||||||
|
|
||||||
|
Return only the code, no explanations."""
|
||||||
|
|
||||||
|
CODE_EXPLANATION = """Explain the following {language} code in detail:
|
||||||
|
|
||||||
|
```{language}
|
||||||
|
{code}
|
||||||
|
```
|
||||||
|
|
||||||
|
Provide:
|
||||||
|
1. Overall purpose of the code
|
||||||
|
2. Key functions/classes and their roles
|
||||||
|
3. Important logic flows
|
||||||
|
4. Any potential issues or improvements
|
||||||
|
5. Usage examples if applicable"""
|
||||||
|
|
||||||
|
CODE_REFACTOR = """Refactor the following {language} code:
|
||||||
|
|
||||||
|
```{language}
|
||||||
|
{code}
|
||||||
|
```
|
||||||
|
|
||||||
|
{files_context}
|
||||||
|
|
||||||
|
Focus on:
|
||||||
|
{refactor_focus}
|
||||||
|
|
||||||
|
Return the refactored code with brief explanation of changes made."""
|
||||||
|
|
||||||
|
CODE_REFACTOR_SAFE = """Perform safe refactoring of the following {language} code:
|
||||||
|
|
||||||
|
```{language}
|
||||||
|
{code}
|
||||||
|
```
|
||||||
|
|
||||||
|
{files_context}
|
||||||
|
|
||||||
|
Safe refactoring means:
|
||||||
|
- No changing external APIs or interfaces
|
||||||
|
- No modifying business logic behavior
|
||||||
|
- Only improving code structure and readability
|
||||||
|
- Better variable names
|
||||||
|
- Extracting reusable functions
|
||||||
|
- Reducing duplication
|
||||||
|
|
||||||
|
Return the refactored code with brief explanation of changes."""
|
||||||
|
|
||||||
|
CODE_REFACTOR_OPTIMIZE = """Optimize the following {language} code for better performance:
|
||||||
|
|
||||||
|
```{language}
|
||||||
|
{code}
|
||||||
|
```
|
||||||
|
|
||||||
|
{files_context}
|
||||||
|
|
||||||
|
Consider:
|
||||||
|
- Algorithm complexity improvements
|
||||||
|
- Memory usage optimization
|
||||||
|
- Reducing unnecessary operations
|
||||||
|
- Better data structures
|
||||||
|
- Caching where appropriate
|
||||||
|
|
||||||
|
Return the optimized code with explanation of performance improvements."""
|
||||||
|
|
||||||
|
TEST_GENERATION = """Write comprehensive tests for the following {language} code:
|
||||||
|
|
||||||
|
```{language}
|
||||||
|
{code}
|
||||||
|
```
|
||||||
|
|
||||||
|
{files_context}
|
||||||
|
|
||||||
|
Requirements:
|
||||||
|
- Use appropriate testing framework for {language}
|
||||||
|
- Cover main functionality
|
||||||
|
- Include edge cases
|
||||||
|
- Use descriptive test names
|
||||||
|
- Keep tests independent
|
||||||
|
|
||||||
|
Supported testing frameworks:
|
||||||
|
- Python: pytest, unittest
|
||||||
|
- JavaScript/TypeScript: jest, mocha
|
||||||
|
- Go: testing package
|
||||||
|
- Rust: test module
|
||||||
|
|
||||||
|
Return the test code only, no explanations."""
|
||||||
|
|
||||||
|
DOCSTRING_GENERATION = """Generate documentation for the following {language} function:
|
||||||
|
|
||||||
|
```{language}
|
||||||
|
{code}
|
||||||
|
```
|
||||||
|
|
||||||
|
Provide:
|
||||||
|
1. Docstring following {language} conventions
|
||||||
|
2. Parameter descriptions
|
||||||
|
3. Return value description
|
||||||
|
4. Example usage
|
||||||
|
|
||||||
|
Return the function with added docstring."""
|
||||||
|
|
||||||
|
BUG_FIX = """Identify and fix bugs in the following {language} code:
|
||||||
|
|
||||||
|
```{language}
|
||||||
|
{code}
|
||||||
|
```
|
||||||
|
|
||||||
|
{files_context}
|
||||||
|
|
||||||
|
Expected behavior: {expected_behavior}
|
||||||
|
|
||||||
|
Identify issues and provide fixed code."""
|
||||||
|
|
||||||
|
CONTEXT_TEMPLATE = """Related project files for context:
|
||||||
|
{files}
|
||||||
|
"""
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def code_generation(
|
||||||
|
cls,
|
||||||
|
language: str,
|
||||||
|
user_prompt: str,
|
||||||
|
files_context: Optional[str] = None
|
||||||
|
) -> str:
|
||||||
|
"""Generate code generation prompt.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
language: Programming language.
|
||||||
|
user_prompt: User's request.
|
||||||
|
files_context: Optional project context.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Complete prompt string.
|
||||||
|
"""
|
||||||
|
context = files_context or ""
|
||||||
|
return cls.CODE_GENERATION.format(
|
||||||
|
language=language,
|
||||||
|
user_prompt=user_prompt,
|
||||||
|
files_context=context
|
||||||
|
)
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def code_explanation(
|
||||||
|
cls,
|
||||||
|
language: str,
|
||||||
|
code: str
|
||||||
|
) -> str:
|
||||||
|
"""Generate code explanation prompt.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
language: Programming language.
|
||||||
|
code: Code to explain.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Complete prompt string.
|
||||||
|
"""
|
||||||
|
return cls.CODE_EXPLANATION.format(
|
||||||
|
language=language,
|
||||||
|
code=code
|
||||||
|
)
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def code_refactor(
|
||||||
|
cls,
|
||||||
|
language: str,
|
||||||
|
code: str,
|
||||||
|
focus: list[str],
|
||||||
|
files_context: Optional[str] = None
|
||||||
|
) -> str:
|
||||||
|
"""Generate refactoring prompt.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
language: Programming language.
|
||||||
|
code: Code to refactor.
|
||||||
|
focus: List of focus areas.
|
||||||
|
files_context: Optional project context.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Complete prompt string.
|
||||||
|
"""
|
||||||
|
context = files_context or ""
|
||||||
|
focus_text = "\n".join(f"- {item}" for item in focus)
|
||||||
|
|
||||||
|
return cls.CODE_REFACTOR.format(
|
||||||
|
language=language,
|
||||||
|
code=code,
|
||||||
|
files_context=context,
|
||||||
|
refactor_focus=focus_text
|
||||||
|
)
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def code_refactor_safe(
|
||||||
|
cls,
|
||||||
|
language: str,
|
||||||
|
code: str,
|
||||||
|
files_context: Optional[str] = None
|
||||||
|
) -> str:
|
||||||
|
"""Generate safe refactoring prompt.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
language: Programming language.
|
||||||
|
code: Code to refactor.
|
||||||
|
files_context: Optional project context.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Complete prompt string.
|
||||||
|
"""
|
||||||
|
context = files_context or ""
|
||||||
|
return cls.CODE_REFACTOR_SAFE.format(
|
||||||
|
language=language,
|
||||||
|
code=code,
|
||||||
|
files_context=context
|
||||||
|
)
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def code_refactor_optimize(
|
||||||
|
cls,
|
||||||
|
language: str,
|
||||||
|
code: str,
|
||||||
|
files_context: Optional[str] = None
|
||||||
|
) -> str:
|
||||||
|
"""Generate optimization prompt.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
language: Programming language.
|
||||||
|
code: Code to optimize.
|
||||||
|
files_context: Optional project context.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Complete prompt string.
|
||||||
|
"""
|
||||||
|
context = files_context or ""
|
||||||
|
return cls.CODE_REFACTOR_OPTIMIZE.format(
|
||||||
|
language=language,
|
||||||
|
code=code,
|
||||||
|
files_context=context
|
||||||
|
)
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def test_generation(
|
||||||
|
cls,
|
||||||
|
language: str,
|
||||||
|
code: str,
|
||||||
|
files_context: Optional[str] = None
|
||||||
|
) -> str:
|
||||||
|
"""Generate test creation prompt.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
language: Programming language.
|
||||||
|
code: Code to test.
|
||||||
|
files_context: Optional project context.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Complete prompt string.
|
||||||
|
"""
|
||||||
|
context = files_context or ""
|
||||||
|
return cls.TEST_GENERATION.format(
|
||||||
|
language=language,
|
||||||
|
code=code,
|
||||||
|
files_context=context
|
||||||
|
)
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def build_system_prompt(cls, extra_context: Optional[str] = None) -> str:
|
||||||
|
"""Build system prompt with optional extra context.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
extra_context: Additional context to include.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Complete system prompt.
|
||||||
|
"""
|
||||||
|
if extra_context:
|
||||||
|
return f"{cls.SYSTEM_PROMPT}\n\nAdditional context:\n{extra_context}"
|
||||||
|
return cls.SYSTEM_PROMPT
|
||||||
|
|
||||||
|
|
||||||
|
class LanguageConfig:
|
||||||
|
"""Configuration for supported programming languages."""
|
||||||
|
|
||||||
|
class _Settings:
|
||||||
|
"""Internal settings class."""
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
extension: str,
|
||||||
|
comment: str,
|
||||||
|
testing_framework: str,
|
||||||
|
file_patterns: list[str],
|
||||||
|
):
|
||||||
|
self.extension = extension
|
||||||
|
self.comment = comment
|
||||||
|
self.testing_framework = testing_framework
|
||||||
|
self.file_patterns = file_patterns
|
||||||
|
|
||||||
|
SUPPORTED_LANGUAGES = {
|
||||||
|
"python": _Settings(
|
||||||
|
extension=".py",
|
||||||
|
comment="#",
|
||||||
|
testing_framework="pytest",
|
||||||
|
file_patterns=["*.py", "*.pyw"]
|
||||||
|
),
|
||||||
|
"javascript": _Settings(
|
||||||
|
extension=".js",
|
||||||
|
comment="//",
|
||||||
|
testing_framework="jest",
|
||||||
|
file_patterns=["*.js", "*.mjs"]
|
||||||
|
),
|
||||||
|
"typescript": _Settings(
|
||||||
|
extension=".ts",
|
||||||
|
comment="//",
|
||||||
|
testing_framework="jest",
|
||||||
|
file_patterns=["*.ts", "*.tsx"]
|
||||||
|
),
|
||||||
|
"go": _Settings(
|
||||||
|
extension=".go",
|
||||||
|
comment="//",
|
||||||
|
testing_framework="testing",
|
||||||
|
file_patterns=["*.go"]
|
||||||
|
),
|
||||||
|
"rust": _Settings(
|
||||||
|
extension=".rs",
|
||||||
|
comment="//",
|
||||||
|
testing_framework="test",
|
||||||
|
file_patterns=["*.rs"]
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def get_config(cls, language: str) -> _Settings:
|
||||||
|
"""Get configuration for a language.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
language: Language name.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Language configuration object.
|
||||||
|
"""
|
||||||
|
return cls.SUPPORTED_LANGUAGES.get(language, cls._Settings(
|
||||||
|
extension=".txt",
|
||||||
|
comment="#",
|
||||||
|
testing_framework="pytest",
|
||||||
|
file_patterns=["*"]
|
||||||
|
))
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def is_supported(cls, language: str) -> bool:
|
||||||
|
"""Check if language is supported.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
language: Language name.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
True if supported, False otherwise.
|
||||||
|
"""
|
||||||
|
return language.lower() in cls.SUPPORTED_LANGUAGES
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def get_supported_languages(cls) -> list[str]:
|
||||||
|
"""Get list of supported languages.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
List of language names.
|
||||||
|
"""
|
||||||
|
return list(cls.SUPPORTED_LANGUAGES.keys())
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def get_extension(cls, language: str) -> str:
|
||||||
|
"""Get file extension for language.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
language: Language name.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
File extension with dot.
|
||||||
|
"""
|
||||||
|
config = cls.get_config(language)
|
||||||
|
return config.extension
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def get_comment_style(cls, language: str) -> str:
|
||||||
|
"""Get comment style for language.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
language: Language name.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Comment prefix.
|
||||||
|
"""
|
||||||
|
config = cls.get_config(language)
|
||||||
|
return config.comment
|
||||||
Reference in New Issue
Block a user