|
| 1 | +# 🐍 Python Integration Guide for opencode |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +Complete Python integration system for opencode with interactive configuration, smart detection, and command automation. |
| 6 | + |
| 7 | +## Architecture |
| 8 | + |
| 9 | +``` |
| 10 | +everything-opencode/ |
| 11 | +├── scripts/interactive/ # Core interactive system |
| 12 | +│ ├── prompts.js # Interactive prompts (color-coded UX) |
| 13 | +│ ├── config-manager.js # Configuration management |
| 14 | +│ └── project-detector.js # Language detection with confidence scoring |
| 15 | +├── languages/python/ # Python-specific wizards |
| 16 | +│ └── config-wizard.js # Python configuration wizard |
| 17 | +├── scripts/python/ # Python utilities |
| 18 | +│ └── tool-detector.js # Python tool detection |
| 19 | +├── scripts/commands/ # Command implementations |
| 20 | +│ ├── python-command-runner.js # Base command runner |
| 21 | +│ ├── python-test.js # /python-test command |
| 22 | +│ ├── python-lint.js # /python-lint command |
| 23 | +│ ├── python-typecheck.js # /python-typecheck command |
| 24 | +│ ├── python-deps.js # /python-deps command |
| 25 | +│ └── python-setup.js # /python-setup command |
| 26 | +├── commands/ # Command documentation |
| 27 | +│ ├── python-test.md |
| 28 | +│ ├── python-lint.md |
| 29 | +│ ├── python-typecheck.md |
| 30 | +│ ├── python-deps.md |
| 31 | +│ └── python-setup.md |
| 32 | +├── examples/python-projects/ # Project templates |
| 33 | +│ └── README.md |
| 34 | +├── scripts/interactive-setup.js # Main setup script |
| 35 | +├── PYTHON-INTEGRATION.md # Technical documentation |
| 36 | +└── INTEGRATION-GUIDE.md # This guide |
| 37 | +``` |
| 38 | + |
| 39 | +## Features |
| 40 | + |
| 41 | +### 1. Smart Project Detection |
| 42 | +- **5 languages**: Python (priority), TypeScript, Go, Rust, PineScript |
| 43 | +- **Confidence scoring**: 0-0.95 based on multiple indicators |
| 44 | +- **Python project types**: FastAPI, Django, Flask, Data Science, ML, CLI, Library |
| 45 | +- **Automatic primary language determination** |
| 46 | + |
| 47 | +### 2. Interactive Configuration |
| 48 | +- **Color-coded prompts** for better UX |
| 49 | +- **Multiple choice with descriptions** for informed decisions |
| 50 | +- **Yes/no confirmations** with smart defaults |
| 51 | +- **Progress indicators** for long operations |
| 52 | +- **User approval** before saving configuration |
| 53 | + |
| 54 | +### 3. Python Tool Detection |
| 55 | +- **30+ tools** detected with version checking |
| 56 | +- **Installation guides** per platform (macOS, Linux, Windows) |
| 57 | +- **Compatibility checking** with version constraints |
| 58 | +- **Priority-based recommendations** |
| 59 | + |
| 60 | +### 4. Command Automation |
| 61 | +- **/python-test** - Run tests with configured test runner |
| 62 | +- **/python-lint** - Run linter and formatter |
| 63 | +- **/python-typecheck** - Run type checker |
| 64 | +- **/python-deps** - Manage dependencies |
| 65 | +- **/python-setup** - Configure Python project |
| 66 | + |
| 67 | +## Quick Start |
| 68 | + |
| 69 | +### 1. Initial Setup |
| 70 | +```bash |
| 71 | +# Run interactive setup |
| 72 | +node scripts/interactive-setup.js |
| 73 | + |
| 74 | +# Or quick setup |
| 75 | +node scripts/interactive-setup.js quick |
| 76 | + |
| 77 | +# Or Python-specific setup |
| 78 | +node scripts/commands/python-setup.js |
| 79 | +``` |
| 80 | + |
| 81 | +### 2. Using Python Commands |
| 82 | +```bash |
| 83 | +# Run tests |
| 84 | +node scripts/commands/python-test.js |
| 85 | + |
| 86 | +# Lint and format code |
| 87 | +node scripts/commands/python-lint.js --fix |
| 88 | + |
| 89 | +# Type checking |
| 90 | +node scripts/commands/python-typecheck.js --strict |
| 91 | + |
| 92 | +# Manage dependencies |
| 93 | +node scripts/commands/python-deps.js install |
| 94 | +node scripts/commands/python-deps.js add fastapi --dev |
| 95 | + |
| 96 | +# Reconfigure project |
| 97 | +node scripts/commands/python-setup.js --reconfigure |
| 98 | +``` |
| 99 | + |
| 100 | +### 3. Manual Testing |
| 101 | +```bash |
| 102 | +# Test project detection |
| 103 | +node scripts/interactive-setup.js detect |
| 104 | + |
| 105 | +# Test tool detection |
| 106 | +node scripts/interactive-setup.js tools |
| 107 | + |
| 108 | +# Show current configuration |
| 109 | +node scripts/interactive-setup.js config |
| 110 | +``` |
| 111 | + |
| 112 | +## Configuration |
| 113 | + |
| 114 | +### Project Configuration File |
| 115 | +Saved to `.opencode/project-config.json`: |
| 116 | + |
| 117 | +```json |
| 118 | +{ |
| 119 | + "project": "/path/to/project", |
| 120 | + "configuredAt": "2025-01-25T10:30:00.000Z", |
| 121 | + "primaryLanguage": "python", |
| 122 | + "python": { |
| 123 | + "projectType": "fastapi", |
| 124 | + "dependencyManager": "uv", |
| 125 | + "testRunner": "pytest", |
| 126 | + "linter": "ruff", |
| 127 | + "formatter": "ruff", |
| 128 | + "typeChecker": "pyright", |
| 129 | + "tools": { |
| 130 | + "python": { "installed": true, "version": "3.11.0" }, |
| 131 | + "uv": { "installed": true, "version": "0.1.0" }, |
| 132 | + "pytest": { "installed": true, "version": "7.4.0" }, |
| 133 | + "ruff": { "installed": true, "version": "0.1.0" }, |
| 134 | + "pyright": { "installed": true, "version": "1.1.0" } |
| 135 | + }, |
| 136 | + "fastapiOptions": { |
| 137 | + "includeDocs": true |
| 138 | + }, |
| 139 | + "userApproved": true |
| 140 | + } |
| 141 | +} |
| 142 | +``` |
| 143 | + |
| 144 | +### Tool Recommendations |
| 145 | + |
| 146 | +#### New Projects |
| 147 | +- **Dependency manager**: uv (fast, modern) |
| 148 | +- **Test runner**: pytest (feature-rich) |
| 149 | +- **Linter**: ruff (extremely fast) |
| 150 | +- **Formatter**: ruff format (consistent) |
| 151 | +- **Type checker**: pyright (fast, good editor integration) |
| 152 | + |
| 153 | +#### Existing Projects |
| 154 | +- Use detected tools if available |
| 155 | +- Migrate gradually to recommended tools |
| 156 | +- Maintain compatibility with existing workflow |
| 157 | + |
| 158 | +## Integration with opencode |
| 159 | + |
| 160 | +### Command Integration |
| 161 | +The Python commands are designed to integrate with opencode's command system: |
| 162 | + |
| 163 | +1. **Command discovery** - Commands are documented in `commands/` directory |
| 164 | +2. **Configuration-aware** - Commands read from `.opencode/project-config.json` |
| 165 | +3. **Interactive feedback** - Color-coded output and progress indicators |
| 166 | +4. **Exit codes** - Proper exit codes for automation |
| 167 | + |
| 168 | +### AI Assistant Integration |
| 169 | +The system provides context for AI assistance: |
| 170 | +- Project type and structure |
| 171 | +- Installed tools and versions |
| 172 | +- Configuration preferences |
| 173 | +- Common patterns for the project type |
| 174 | + |
| 175 | +### Development Workflow |
| 176 | +```bash |
| 177 | +# Typical workflow |
| 178 | +/python-setup # Configure project |
| 179 | +/python-deps install # Install dependencies |
| 180 | +/python-lint --fix # Fix linting issues |
| 181 | +/python-test # Run tests |
| 182 | +/python-typecheck # Type checking |
| 183 | +# ... develop ... |
| 184 | +/python-lint # Check code quality |
| 185 | +/python-test --coverage # Test with coverage |
| 186 | +``` |
| 187 | + |
| 188 | +## Extending the System |
| 189 | + |
| 190 | +### Adding New Python Tools |
| 191 | +1. Add tool definition to `scripts/python/tool-detector.js`: |
| 192 | +```javascript |
| 193 | +toolName: { |
| 194 | + command: 'tool --version', |
| 195 | + description: 'Tool description', |
| 196 | + installGuide: { |
| 197 | + macos: 'brew install tool', |
| 198 | + linux: 'apt-get install tool', |
| 199 | + windows: 'Download from...' |
| 200 | + }, |
| 201 | + priority: 5 |
| 202 | +} |
| 203 | +``` |
| 204 | + |
| 205 | +2. Add to configuration schema in `scripts/interactive/config-manager.js` |
| 206 | +3. Add to wizard options in `languages/python/config-wizard.js` |
| 207 | + |
| 208 | +### Adding New Project Types |
| 209 | +1. Add detection logic to `scripts/interactive/project-detector.js`: |
| 210 | +```javascript |
| 211 | +// Check for project type markers |
| 212 | +const hasMarkers = await checkPythonProjectType(projectPath, 'new-type', [ |
| 213 | + 'import newframework', |
| 214 | + 'from newframework import' |
| 215 | +]); |
| 216 | +``` |
| 217 | + |
| 218 | +2. Add configuration options to wizard |
| 219 | +3. Add project templates to `examples/python-projects/` |
| 220 | + |
| 221 | +### Creating New Commands |
| 222 | +1. Create command documentation in `commands/` directory |
| 223 | +2. Implement command in `scripts/commands/` directory |
| 224 | +3. Use `PythonCommandRunner` base class |
| 225 | +4. Test with sample project |
| 226 | + |
| 227 | +## Testing |
| 228 | + |
| 229 | +### Unit Tests |
| 230 | +```bash |
| 231 | +# Test project detection |
| 232 | +node -e "const ProjectDetector = require('./scripts/interactive/project-detector'); const d = new ProjectDetector(); d.getProjectSummary().then(console.log)" |
| 233 | + |
| 234 | +# Test tool detection |
| 235 | +node -e "const PythonToolDetector = require('./scripts/python/tool-detector'); const d = new PythonToolDetector(); d.detectAll().then(r => d.printResults(r))" |
| 236 | + |
| 237 | +# Test command runner |
| 238 | +node scripts/commands/python-test.js --help |
| 239 | +``` |
| 240 | + |
| 241 | +### Integration Tests |
| 242 | +1. Create test Python project |
| 243 | +2. Run `/python-setup` to configure |
| 244 | +3. Test each command functionality |
| 245 | +4. Verify configuration persistence |
| 246 | + |
| 247 | +## Performance Considerations |
| 248 | + |
| 249 | +### Detection Performance |
| 250 | +- **File scanning**: Limited to first 10 Python files for import detection |
| 251 | +- **Glob patterns**: Efficient file pattern matching |
| 252 | +- **Caching**: Configuration cached after first detection |
| 253 | +- **Parallel detection**: Language detectors run independently |
| 254 | + |
| 255 | +### Command Performance |
| 256 | +- **Lazy initialization**: Configuration loaded only when needed |
| 257 | +- **Tool checking**: Tools checked once per command |
| 258 | +- **Parallel execution**: Where supported (test parallelization) |
| 259 | +- **Incremental checking**: `--diff` option for changed files only |
| 260 | + |
| 261 | +## Security |
| 262 | + |
| 263 | +### Configuration Security |
| 264 | +- **User approval**: Configurations only saved with user consent |
| 265 | +- **No secrets**: Configuration files don't contain secrets |
| 266 | +- **Validation**: JSON schema validation for configuration |
| 267 | +- **Import/export**: Safe configuration sharing |
| 268 | + |
| 269 | +### Command Security |
| 270 | +- **Command validation**: Validate commands before execution |
| 271 | +- **No arbitrary execution**: Commands are predefined |
| 272 | +- **Environment isolation**: Commands run in project directory |
| 273 | +- **Exit code handling**: Proper error reporting |
| 274 | + |
| 275 | +## Troubleshooting |
| 276 | + |
| 277 | +### Common Issues |
| 278 | + |
| 279 | +#### Project Not Detected |
| 280 | +```bash |
| 281 | +# Force Python project type |
| 282 | +node scripts/commands/python-setup.js --project-type python |
| 283 | + |
| 284 | +# Check detection manually |
| 285 | +node scripts/interactive-setup.js detect |
| 286 | +``` |
| 287 | + |
| 288 | +#### Tools Not Found |
| 289 | +```bash |
| 290 | +# Detect tools manually |
| 291 | +node scripts/interactive-setup.js tools |
| 292 | + |
| 293 | +# Install missing tools |
| 294 | +curl -LsSf https://astral.sh/uv/install.sh | sh |
| 295 | +pip install pytest ruff pyright |
| 296 | +``` |
| 297 | + |
| 298 | +#### Configuration Issues |
| 299 | +```bash |
| 300 | +# View current configuration |
| 301 | +cat .opencode/project-config.json |
| 302 | + |
| 303 | +# Remove and reconfigure |
| 304 | +rm .opencode/project-config.json |
| 305 | +node scripts/commands/python-setup.js |
| 306 | +``` |
| 307 | + |
| 308 | +#### Command Execution Issues |
| 309 | +```bash |
| 310 | +# Test with --help |
| 311 | +node scripts/commands/python-test.js --help |
| 312 | + |
| 313 | +# Check Python installation |
| 314 | +python --version |
| 315 | + |
| 316 | +# Verify virtual environment |
| 317 | +source .venv/bin/activate # Linux/Mac |
| 318 | +.venv\Scripts\activate # Windows |
| 319 | +``` |
| 320 | + |
| 321 | +### Debug Mode |
| 322 | +```bash |
| 323 | +# Verbose output |
| 324 | +node scripts/commands/python-test.js --verbose |
| 325 | + |
| 326 | +# Debug initialization |
| 327 | +node -e "const runner = require('./scripts/commands/python-command-runner'); new runner().initialize().catch(console.error)" |
| 328 | +``` |
| 329 | + |
| 330 | +## Future Enhancements |
| 331 | + |
| 332 | +### Phase 3: Advanced Features |
| 333 | +1. **Python skills** - AI assistance patterns for Python projects |
| 334 | +2. **Python agents** - Specialized agents for different project types |
| 335 | +3. **Template generation** - Code generation from templates |
| 336 | +4. **Migration tools** - Tool migration assistance |
| 337 | + |
| 338 | +### Phase 4: Ecosystem Integration |
| 339 | +1. **CI/CD integration** - GitHub Actions, GitLab CI templates |
| 340 | +2. **Editor integration** - VS Code, PyCharm configuration |
| 341 | +3. **Package publishing** - PyPI publishing assistance |
| 342 | +4. **Documentation generation** - Auto-documentation tools |
| 343 | + |
| 344 | +### Phase 5: Multi-language Support |
| 345 | +1. **TypeScript integration** - Node.js/TypeScript project support |
| 346 | +2. **Go integration** - Go project configuration |
| 347 | +3. **Rust integration** - Rust project setup |
| 348 | +4. **PineScript integration** - TradingView strategy development |
| 349 | + |
| 350 | +## Contributing |
| 351 | + |
| 352 | +### Development Guidelines |
| 353 | +1. **Follow existing patterns** - Maintain consistency |
| 354 | +2. **Add tests** - Test new functionality |
| 355 | +3. **Update documentation** - Keep docs current |
| 356 | +4. **Use interactive prompts** - For user interaction |
| 357 | +5. **Store configurations properly** - In `.opencode/project-config.json` |
| 358 | + |
| 359 | +### Code Style |
| 360 | +- **JavaScript**: ES6+ with async/await |
| 361 | +- **Python**: PEP 8 with type hints (for templates) |
| 362 | +- **Documentation**: Markdown with examples |
| 363 | +- **Error handling**: Graceful degradation with clear messages |
| 364 | + |
| 365 | +### Testing Strategy |
| 366 | +1. **Unit tests** - Individual component testing |
| 367 | +2. **Integration tests** - End-to-end workflow testing |
| 368 | +3. **Manual testing** - Interactive testing with sample projects |
| 369 | +4. **Documentation testing** - Verify examples work |
| 370 | + |
| 371 | +## Support |
| 372 | + |
| 373 | +### Getting Help |
| 374 | +1. **Check documentation** - `PYTHON-INTEGRATION.md` and `INTEGRATION-GUIDE.md` |
| 375 | +2. **Test commands** - Use `--help` flag for command usage |
| 376 | +3. **Examine configuration** - Check `.opencode/project-config.json` |
| 377 | +4. **Create test case** - Reproduce issue with minimal example |
| 378 | + |
| 379 | +### Reporting Issues |
| 380 | +1. **Describe the problem** - What happened vs expected |
| 381 | +2. **Include configuration** - Project type and tools |
| 382 | +3. **Provide reproduction steps** - How to reproduce the issue |
| 383 | +4. **Share error messages** - Complete error output |
| 384 | + |
| 385 | +## License |
| 386 | + |
| 387 | +Part of the everything-opencode project. See project LICENSE for details. |
| 388 | + |
| 389 | +--- |
| 390 | + |
| 391 | +**🎉 Python Integration Complete!** The system is ready for use and extensible for future enhancements. |
0 commit comments