Prompt Structure Architecture
This document describes the architecture of the prompt handling system in DevoxxGenie, which has been refactored to improve maintainability and extensibility.
Overview
The prompt structure in DevoxxGenie uses several design patterns to create a clean, modular system:
- Strategy Pattern: Different execution approaches for different types of prompts
- Command Pattern: Handling specific prompt commands
- Factory Pattern: Creating appropriate strategies based on context
- Service-oriented Architecture: Centralized services for key functionalities
Core Components
Controllers
- PromptExecutionController: Main controller that orchestrates prompt execution
- TokenCalculationController: Handles token calculation and cost estimation
- ProjectContextController: Manages project context for prompts
Services
- PromptExecutionService: Orchestrates prompt execution workflow
- ChatMemoryManager: Unified memory management for conversations
- PromptCommandProcessor: Central command processing for slash commands
- ChatPromptExecutor: Handles direct interaction with LLM providers
Strategy Components
The strategy pattern is used to handle different types of prompt execution:
graph TD
A[PromptExecutionService] -->|creates| B[PromptExecutionStrategyFactory]
B -->|creates| C[PromptExecutionStrategy]
C -->|implemented by| D[NonStreamingPromptStrategy]
C -->|implemented by| E[StreamingPromptStrategy]
C -->|implemented by| F[WebSearchPromptStrategy]
- PromptExecutionStrategy (interface): Common interface for all strategies
- NonStreamingPromptStrategy: Handles regular, non-streaming responses
- StreamingPromptStrategy: Manages token-by-token streaming responses
- WebSearchPromptStrategy: Incorporates web search results into responses
Command Components
The command pattern handles different types of slash commands:
graph TD
A[PromptCommandProcessor] -->|processes| B[PromptCommand]
B -->|implemented by| C[FindCommand]
B -->|implemented by| D[HelpCommand]
B -->|implemented by| E[CustomPromptCommand]
- PromptCommand (interface): Common interface for all commands
- FindCommand: Handles /find commands (particularly for RAG search)
- HelpCommand: Handles /help commands for documentation
- CustomPromptCommand: Handles user-defined custom commands
Prompt Flow
The following diagram illustrates the complete prompt flow in DevoxxGenie:
The prompt flows through these steps:
- User Input: Captured in
UserPromptPanel - Event Handling:
PromptSubmissionListenerandPromptExecutionControllerprocess the event - Command Processing:
PromptCommandProcessorchecks for and handles slash commands - Strategy Selection:
PromptExecutionStrategyFactorycreates the appropriate execution strategy - Context Preparation: Includes project files, RAG context, etc.
- LLM Provider: The query is passed to the selected LLM provider
- Response Handling: The response is processed and displayed to the user
Memory Management
Chat memory is managed through the ChatMemoryManager service:
- Maintains a configurable-sized window of previous messages
- Handles system messages, user messages, and AI responses
- Provides context for the LLM to maintain conversation coherence
Token Management
Token calculation and cost estimation are handled by:
- TokenCalculationController: Coordinates token-related operations
- TokenCalculationService: Performs actual calculations based on the selected model
- TokenUsageBar: UI component to visualize token usage
Error Handling
The system includes centralized error handling through:
- PromptErrorHandler: Central error-handling component
- Different exception types: ModelException, MemoryException, PromptException, etc.
- UI feedback: Proper error messaging to the user
Extending the System
Adding a New Strategy
To add a new prompt execution strategy:
- Create a new class implementing
PromptExecutionStrategy - Add logic to
PromptExecutionStrategyFactoryto select your strategy - Register any required services in the dependency injection system
Adding a New Command
To add a new slash command:
- Create a new class implementing
PromptCommand - Add handling in
PromptCommandProcessorto recognize your command - Register the command processor if needed
Adding Provider-Specific Features
To add features for specific LLM providers:
- Extend the appropriate provider factory (e.g.,
OpenAIChatModelFactory) - Add provider-specific parameters or capabilities
- Update the UI to expose the new features when the provider is selected
Best Practices
When working with the prompt structure:
- Follow Established Patterns: Use the existing strategy and command patterns
- Maintain Separation of Concerns: Keep UI, business logic, and provider communication separate
- Use Dependency Injection: Leverage IntelliJ's service framework for dependencies
- Handle Errors Gracefully: Use the error handling framework
- Write Tests: New components should have corresponding unit tests
Future Directions
The prompt structure architecture is designed to evolve with:
- Support for more complex agentic behaviors
- Enhanced RAG and knowledge graph integrations
- Multi-stage reasoning and planning capabilities
- More sophisticated memory management