|
| 1 | +# CLAUDE.md |
| 2 | + |
| 3 | +This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository. |
| 4 | + |
| 5 | +## Project Overview |
| 6 | + |
| 7 | +This is `jupyter_ai_router`, a core message routing layer for Jupyter AI. It's a JupyterLab extension that provides foundational message routing functionality, automatically detecting new chat sessions and routing messages to registered callbacks based on message type (slash commands vs regular messages). |
| 8 | + |
| 9 | +## Architecture |
| 10 | + |
| 11 | +### Core Components |
| 12 | + |
| 13 | +- **MessageRouter** (`jupyter_ai_router/router.py`): Central routing class that manages chat connections and message callbacks |
| 14 | +- **RouterExtension** (`jupyter_ai_router/extension.py`): Jupyter server extension that initializes the router and listens for chat events |
| 15 | +- **Frontend Plugin** (`src/index.ts`): JupyterLab frontend extension for client-side integration |
| 16 | +- **API Handler** (`jupyter_ai_router/handlers.py`): Simple health check endpoint |
| 17 | + |
| 18 | +### Key Dependencies |
| 19 | + |
| 20 | +- `jupyterlab-chat>=0.17.0`: Core chat functionality and YChat document handling |
| 21 | +- `jupyter-collaboration>=4.0.0`: Real-time collaboration features and event system |
| 22 | +- `pycrdt`: CRDT (Conflict-free Replicated Data Type) for message handling |
| 23 | + |
| 24 | +### Message Flow |
| 25 | + |
| 26 | +1. RouterExtension listens for `jupyter_collaboration` chat room initialization events |
| 27 | +2. When new chat detected, retrieves YChat document and connects to MessageRouter |
| 28 | +3. MessageRouter observes YChat message streams and routes to registered callbacks |
| 29 | +4. Messages are classified as slash commands (starting with `/`) or regular messages |
| 30 | +5. Appropriate callbacks are invoked with `(room_id, message)` parameters |
| 31 | + |
| 32 | +## Development Commands |
| 33 | + |
| 34 | +### Initial Setup |
| 35 | + |
| 36 | +```bash |
| 37 | +# Install development dependencies |
| 38 | +jlpm |
| 39 | + |
| 40 | +# Install Python package in development mode |
| 41 | +pip install -e ".[test]" |
| 42 | + |
| 43 | +# Link extension to JupyterLab |
| 44 | +jupyter labextension develop . --overwrite |
| 45 | + |
| 46 | +# Enable server extension |
| 47 | +jupyter server extension enable jupyter_ai_router |
| 48 | +``` |
| 49 | + |
| 50 | +### Building |
| 51 | + |
| 52 | +```bash |
| 53 | +# Build TypeScript and JupyterLab extension (development) |
| 54 | +jlpm build |
| 55 | + |
| 56 | +# Production build |
| 57 | +jlpm build:prod |
| 58 | + |
| 59 | +# Watch mode (rebuilds on changes) |
| 60 | +jlpm watch |
| 61 | +``` |
| 62 | + |
| 63 | +### Testing |
| 64 | + |
| 65 | +```bash |
| 66 | +# Run Python tests with coverage |
| 67 | +pytest -vv -r ap --cov jupyter_ai_router |
| 68 | + |
| 69 | +# Run JavaScript/TypeScript tests |
| 70 | +jlpm test |
| 71 | + |
| 72 | +# Run integration tests (Playwright) |
| 73 | +cd ui-tests && jlpm test |
| 74 | +``` |
| 75 | + |
| 76 | +### Code Quality |
| 77 | + |
| 78 | +```bash |
| 79 | +# Run all linting and formatting |
| 80 | +jlpm lint |
| 81 | + |
| 82 | +# Check code style without fixing |
| 83 | +jlpm lint:check |
| 84 | + |
| 85 | +# Individual tools |
| 86 | +jlpm eslint # Fix TypeScript/JavaScript issues |
| 87 | +jlpm prettier # Fix formatting |
| 88 | +jlpm stylelint # Fix CSS issues |
| 89 | +``` |
| 90 | + |
| 91 | +### Development Workflow |
| 92 | + |
| 93 | +```bash |
| 94 | +# Clean build artifacts |
| 95 | +jlpm clean:all |
| 96 | + |
| 97 | +# Full development setup from scratch |
| 98 | +jlpm dev:install |
| 99 | + |
| 100 | +# Uninstall development setup |
| 101 | +jlpm dev:uninstall |
| 102 | +``` |
| 103 | + |
| 104 | +## Code Style |
| 105 | + |
| 106 | +### TypeScript/JavaScript |
| 107 | + |
| 108 | +- Single quotes for strings |
| 109 | +- No trailing commas |
| 110 | +- Arrow functions preferred |
| 111 | +- Interface names must start with `I` and be PascalCase |
| 112 | +- 2-space indentation |
| 113 | + |
| 114 | +### Python |
| 115 | + |
| 116 | +- Follow standard Python conventions |
| 117 | +- Use type hints where appropriate |
| 118 | +- Inherit from `LoggingConfigurable` for components that need logging |
| 119 | + |
| 120 | +## Extension Integration |
| 121 | + |
| 122 | +Other Jupyter extensions can access the router via settings: |
| 123 | + |
| 124 | +```python |
| 125 | +router = self.serverapp.web_app.settings.get("jupyter-ai", {}).get("router") |
| 126 | + |
| 127 | +# Register callbacks |
| 128 | +def on_new_chat(room_id: str, ychat: YChat): |
| 129 | + print(f"New chat: {room_id}") |
| 130 | + |
| 131 | +def on_slash_command(room_id: str, message: Message): |
| 132 | + print(f"Slash command: {message.body}") |
| 133 | + |
| 134 | +router.observe_chat_init(on_new_chat) |
| 135 | +router.observe_slash_cmd_msg(room_id, on_slash_command) |
| 136 | +router.observe_chat_msg(room_id, on_regular_message) |
| 137 | +``` |
| 138 | + |
| 139 | +## Version Compatibility |
| 140 | + |
| 141 | +- Python 3.9-3.13 |
| 142 | +- JupyterLab 4.x |
| 143 | +- Handles both jupyter-collaboration v3+ and v2.x compatibility through version detection |
| 144 | + |
| 145 | +## Testing Strategy |
| 146 | + |
| 147 | +- **Unit tests**: Jest for TypeScript, pytest for Python |
| 148 | +- **Integration tests**: Playwright via Galata for full JupyterLab integration |
| 149 | +- **Coverage**: Aim for comprehensive test coverage, especially for routing logic |
0 commit comments