Architecture
This document describes the architecture of the doctai.
Overview
doctai is a Python-based tool that uses AI to automatically test documentation by generating and executing test scripts.
┌─────────────────────────────────────────────────────────────┐
│ User Interface │
│ (CLI, GitHub Actions, Direct Python Import) │
└──────────────────┬──────────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────┐
│ Orchestrator │
│ (doc_tester.orchestrator.DocumentationTester) │
│ - Manages workflow │
│ - Coordinates components │
└──┬────────────────┬────────────────┬────────────────────────┘
│ │ │
▼ ▼ ▼
┌──────────┐ ┌──────────┐ ┌──────────────┐
│ Fetcher │ │AI Client │ │ Executor │
│ │ │ │ │ │
│ - Files │ │ - OpenAI │ │ - Bash │
│ - URLs │ │ - Claude │ │ - Python │
│ - Dirs │ │ - Custom │ │ - Safety │
└──────────┘ └──────────┘ └──────────────┘
Components
1. CLI (doc_tester/cli.py)
Purpose: Command-line interface for users
Responsibilities:
- Parse command-line arguments
- Validate configuration
- Initialize and run the orchestrator
- Output results
Key Functions:
main(): Entry point for the CLI
2. Orchestrator (doc_tester/orchestrator.py)
Purpose: Main workflow coordinator
Responsibilities:
- Coordinate all components
- Manage the testing workflow
- Handle errors and reporting
- Control the AI conversation flow
Key Classes:
DocumentationTester: Main orchestrator class
Workflow:
- Fetch documentation
- Send to AI for analysis
- Extract generated scripts
- Execute scripts
- Collect and report results
3. Fetcher (doc_tester/fetcher.py)
Purpose: Retrieve documentation from various sources
Responsibilities:
- Read local files
- Read directories (recursive)
- Fetch from URLs
- Support multiple documentation formats
Key Classes:
DocumentationFetcher: Handles all fetching operations
Supported Sources:
- Local files (
.md,.txt,.rst,.adoc) - Directories (recursive search)
- HTTP/HTTPS URLs
4. AI Client (doc_tester/ai_client.py)
Purpose: Communicate with AI providers
Responsibilities:
- Handle different AI APIs
- Manage conversation history
- Format requests/responses
- Error handling and retries
Key Classes:
AIClient: Generic AI clientAIProvider: Enum of supported providers
Supported Providers:
- OpenAI (GPT-4, etc.)
- Anthropic (Claude, etc.)
- Custom (OpenAI-compatible APIs)
API Handling:
- OpenAI format:
/v1/chat/completions - Anthropic format:
/v1/messages - Custom: Configurable endpoint
5. Executor (doc_tester/executor.py)
Purpose: Safely execute generated scripts
Responsibilities:
- Create isolated execution environment
- Execute scripts with timeouts
- Capture stdout/stderr
- Clean up resources
Key Classes:
ScriptExecutor: Handles script execution
Features:
- Temporary working directories
- Configurable timeouts
- Environment variable support
- Multiple script types (bash, python, etc.)
Data Flow
1. Documentation Collection
User Input (paths/URLs)
↓
DocumentationFetcher
↓
Dict[source, content]
2. AI Analysis
Documentation Content
↓
AIClient.send_message()
↓
AI Response (with code blocks)
↓
Script Extraction (regex)
↓
Dict[script_name, {content, type}]
3. Script Execution
Scripts
↓
ScriptExecutor.execute_multiple_scripts()
↓
For each script:
- Write to temp file
- Execute with timeout
- Capture output
↓
Dict[script_name, (success, stdout, stderr)]
4. Results
Execution Results
↓
Orchestrator
↓
JSON Output / Console Display
Configuration
Environment Variables
DOCTAI_API_KEY: AI API keyDOCTAI_API_URL: Custom API endpointDOCTAI_PROVIDER: AI provider nameDOCTAI_MODEL: Model name
Command-Line Options
See cli.py for full list of options.
Extension Points
Adding New AI Providers
- Add provider to
AIProviderenum inai_client.py - Implement
_send_<provider>_message()method - Add default URL and model
- Update documentation
Adding New Script Types
- Update
execute_script()inexecutor.py - Add file extension mapping
- Add interpreter command
- Update documentation
Custom Execution Environments
- Modify
ScriptExecutorto use containers - Add Docker/Podman support
- Implement sandboxing
Security Considerations
Script Execution
- Isolation: Scripts run in temporary directories
- Timeouts: Configurable execution timeouts
- Sandboxing: Consider container-based execution for production
API Keys
- Never log or expose API keys
- Use environment variables or secrets
- Sanitize output
Generated Scripts
- AI-generated scripts should be reviewed
- Use
--verboseto see scripts before execution - Consider approval workflow for production
Testing Strategy
Unit Tests
- Test each component independently
- Mock AI responses
- Mock file system operations
Integration Tests
- Test full workflow
- Use sample documentation
- Verify script generation and execution
End-to-End Tests
- Test with real AI APIs (separate quota)
- Test with various documentation formats
- Test error scenarios
Performance Considerations
AI API Calls
- Batching: Send all documentation in one request
- Caching: Consider caching AI responses
- Rate limiting: Respect API rate limits
Script Execution
- Parallel execution: Not yet implemented
- Resource limits: Consider adding memory/CPU limits
- Cleanup: Always cleanup temporary resources
Future Enhancements
Planned Features
- Retry Logic: Automatic retry for failed scripts
- Docker Support: Run scripts in containers
- Parallel Execution: Execute multiple scripts concurrently
- Caching: Cache AI responses and results
- Approval Workflow: Review scripts before execution
- Metrics: Track success rates and performance
- Webhooks: Notify on completion
- Multi-language: Better support for non-English docs
Architecture Improvements
- Plugin System: Easy addition of new providers/executors
- Event System: Hook into workflow events
- Database: Store results for analysis
- Web UI: Visual interface for results
- API Server: REST API for integration
Dependencies
Core Dependencies
requests: HTTP client for AI APIs and URL fetching- Python 3.8+: Core language
Optional Dependencies
docker: For container-based executionpytest: For testingblack: For code formatting
Deployment
Local Installation
pip install -e .
GitHub Actions
See .github/workflows/test-docs.yml
Docker (Future)
FROM python:3.11-slim
COPY . /app
WORKDIR /app
RUN pip install -e .
ENTRYPOINT ["doctai"]
Contributing
See component-specific sections above for extension points. Each component is designed to be modular and extensible.
License
MIT License - See LICENSE file