This document provides detailed information on how to develop, build, and contribute to the LexiSage project. LexiSage is an Anki add-on that uses AI to generate word definitions, synonyms, grammar notes, and example sentences for language learners.
LexiSage adopts a modular design, separating different functionalities into independent Python files for easy maintenance and extension. The entire add-on follows an MVC-like pattern:
- Model Layer:
ai_service.pyandprompts.pyhandle data logic and AI interaction - View Layer:
config_ui.pyprovides the user interface - Controller Layer:
__init__.pyserves as the entry point coordinating all modules
Core Functionality: The main entry point of the add-on, responsible for registering Anki hooks and coordinating the work of each module. Main Contents:
BatchGenerationWorker: Background thread class for batch generation of explanations, avoiding UI freezingsave_results(): Writes AI-generated results back to the Anki database, including transaction handlingon_browser_batch_generate(): Browser batch generation functionalityon_editor_gen(): Editor single-card generation functionalitysetup_browser_menu(): Adds LexiSage menu to the Anki browseradd_editor_button(): Adds LexiSage button to the editor Key Features:- Complete exception handling and resource cleanup
- Database transaction rollback mechanism, preventing data corruption
- Progress dialog management, ensuring UI responsiveness
Core Functionality: Encapsulates communication logic with various AI services (OpenAI, XAI, DeepSeek). Main Contents:
generate_batch_explanation(): Core function that constructs JSON payload and calls AI servicesExplanationTask: Class representing a single explanation generation taskProgressTracker: Multi-threading progress trackergenerate_explanations_batch(): Function for batch processing multiple tasksformat_text_to_html(): Tool function for cleaning AI-returned text Key Features:- Supports multi-threaded concurrent requests
- Complete logging system (lexisage.log)
- Automatic retry mechanism (maximum 2 retries)
- Unified API interface supporting multiple AI services
Core Functionality: Provides user-friendly configuration interface for managing add-on settings. Main Contents:
ConfigDialog: Main configuration dialog class containing three tabsNoteTypeConfig: Data class for note type configuration- Three tabs:
- Note Type Settings: Configure field mappings and prompts for different note types
- AI System Instructions: Set global AI system prompts
- AI Service Settings: Configure API keys, model parameters, and advanced options Key Features:
- Dynamic UI updates with real-time preview of AI sending content
- Configuration validation and error handling
- Default template viewing functionality
Core Functionality: Centralized management of all AI prompt templates, providing default configurations. Main Contents:
DEFAULT_GLOBAL_SYSTEM_PROMPT: Global system prompt defining AI role and behavior specificationsDEFAULT_FIELD_PROMPT_TEMPLATE: Default field prompt used when a field has no custom prompt configuredBATCH_INSTRUCTION_TEMPLATE: UI preview template for display purposes only Key Features:- Clear three-layer prompt architecture (system layer, instruction layer, preview layer)
- Unified string format supporting variable substitution (
{word},{context}) - Adheres to "Feynman Principle", emphasizing easy-to-understand explanations
- Separation of Concerns: UI logic, business logic, and AI communication logic separated into different files
- Extensibility: Easy to add new AI services or functional modules
- Maintainability: Clear module boundaries and responsibility division
- Error Handling: Comprehensive exception catching and user-friendly error prompts
- Configuration Management: Unified configuration system supporting multiple note types
Suggestion: Further split large files into smaller specialized modules.
- Create
threading_utils.py: Centralize multi-threading related code (BatchGenerationWorker,ProgressTracker) - Create
database_utils.py: Handle all database operations (save_results, field checking functions) - Create
logging_utils.py: Unified logging and error handling
Suggestion: Use more structured configuration management.
- Introduce configuration validation and migration tools
- Support configuration version upgrades
- Add configuration import/export functionality
Suggestion: Establish a complete test suite.
- Unit tests: Test core functionality of each module
- Integration tests: Test inter-module collaboration
- End-to-end tests: Simulate usage scenarios in real Anki environment
Suggestion: Provide support for multilingual users.
- Extract all UI text to translation files
- Support dynamic language switching
- Provide translation contribution guidelines
-
Clone Repository:
git clone https://github.com/{your-username}/LexiSage.git cd LexiSage -
Python Environment:
- Requires Python 3.8 or higher
- Recommended to use virtual environment:
python -m venv venv - Activate virtual environment:
- Windows:
venv\Scripts\activate - macOS/Linux:
source venv/bin/activate
- Windows:
-
Dependency Installation:
pip install requests # Install other dependencies as needed -
Anki Development Environment:
- Link the add-on directory to Anki's addons21 folder
- Restart Anki to load the add-on
- Use Anki's development tools for debugging
-
Ensure necessary files are included:
__init__.py: Add-on entry pointai_service.py: AI service interfaceconfig_ui.py: Configuration interfaceprompts.py: Default promptsmanifest.json: Add-on metadata, including name, version, etc.meta.json: Anki version compatibility information (important, determines supported Anki versions)config.json: Default configuration (optional)LICENSE: License file (GNU General Public License v3.0)
-
Update version numbers:
- Update the
versionfield inmanifest.json - Synchronize version number in
ankiweb.json
- Update the
-
Package command:
zip -r LexiSage.ankiaddon __init__.py ai_service.py config_ui.py prompts.py manifest.json meta.json LICENSE -x ".*" "__pycache__/*" "*.pyc"
-
Verify package contents:
unzip -l LexiSage.ankiaddon
Ensure it doesn't contain extraneous files like
.git,__pycache__, etc.
- Register an AnkiWeb account
- After logging in, visit the sharing page
- Click "Upload Add-on" to submit your add-on
- Fill in add-on information and upload the packaged file
- Wait for approval
Welcome contributions of code, issue reports, or feature suggestions!
- Fork repository: Fork this repository on GitHub
- Create branch:
git checkout -b feature/your-feature-name - Commit changes:
git commit -m 'Add some feature' - Push branch:
git push origin feature/your-feature-name - Create PR: Create a Pull Request on GitHub
- Follow PEP 8 code style
- Add appropriate comments and docstrings
- Ensure new features have corresponding error handling
- Update relevant documentation
- New features should include test cases
- Ensure existing tests pass
- Update test documentation
- AI service connection failure: Check API keys and network connection
- Configuration save failure: Check file permissions and disk space
- UI display abnormalities: Restart Anki or clear cache
- View log file: After running one field filling operation,
lexisage.logrecords all API requests and errors - Simplify configuration: Start with minimal configuration, gradually add complex features
This project is licensed under the GNU General Public License v3.0 (GPL-3.0). For details, see the LICENSE file.
Last Updated: January 2026 Version: Based on analysis and optimization suggestions of current LexiSage architecture