Livio Meuli 6f9be8a6c4 Implement ChatManager with API integration, SystemPrompter, and comprehensive test suite
- Add ChatManager class for OpenAI-compatible API communication (silicon.fhgr.ch:7080)
- Add SystemPrompter for intelligent prompt generation with file context
- Integrate ChatManager into frontend chat component
- Add comprehensive pytest tests (40+ tests covering unit and integration scenarios)
- Implement error handling for API failures, timeouts, and connection issues
- Add environment variable-based configuration for API credentials
- Update frontend state initialization to include ChatManager
- All tests passing with mock/patch isolation for API calls
2026-04-09 14:19:57 +02:00

39 lines
1.5 KiB
Python

"""System Prompter - Builds system prompts with optional file context"""
MAX_FILE_CHARS = 4000 # Limit file context to avoid token overflow
class SystemPrompter:
@staticmethod
def generate_prompt(file_context: dict | None = None) -> str:
"""Build a system prompt, optionally embedding a file's content.
Args:
file_context: dict with keys 'name' and 'content', or None.
Returns:
A system prompt string.
"""
base = (
"You are an expert code assistant integrated into a lightweight code editor. "
"Help the user with code suggestions, debugging, explanations, and improvements. "
"Be concise and precise. Use markdown and fenced code blocks where appropriate."
)
if file_context:
name = file_context.get("name", "unknown")
content = file_context.get("content", "")
# Truncate large files to avoid exceeding token limits
if len(content) > MAX_FILE_CHARS:
content = content[:MAX_FILE_CHARS] + "\n... [truncated]"
file_section = (
f"\n\nThe user currently has the following file open in the editor:\n"
f"<file name=\"{name}\">\n"
f"<code>\n{content}\n</code>\n"
f"</file>\n"
f"Refer to this file when answering questions about the code."
)
return base + file_section
return base