|
| 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 | +PaperDebugger is an AI-powered academic writing assistant with Chrome extension frontend and Go backend. It integrates with Overleaf to provide intelligent suggestions, chat assistance, and AI-powered paper review capabilities. |
| 8 | + |
| 9 | +**Tech Stack:** |
| 10 | +- **Backend**: Go 1.24+, Gin (HTTP), gRPC (API), MongoDB |
| 11 | +- **Frontend**: React 19, TypeScript, Vite, Zustand (state management) |
| 12 | +- **Chrome Extension**: Built from `webapp/_webapp` |
| 13 | +- **Office Add-in**: Separate npm project in `webapp/office` |
| 14 | +- **API**: Protocol Buffers with Buf for code generation |
| 15 | +- **AI**: OpenAI API integration with optional XtraMCP orchestration backend |
| 16 | + |
| 17 | +## Architecture |
| 18 | + |
| 19 | +### Backend Structure |
| 20 | + |
| 21 | +``` |
| 22 | +cmd/main.go - Entry point for backend server |
| 23 | +internal/ |
| 24 | + ├── api/ - HTTP (Gin) and gRPC API handlers |
| 25 | + │ ├── auth/ - Authentication endpoints (login, logout, token refresh) |
| 26 | + │ ├── chat/ - Chat and conversation management |
| 27 | + │ ├── comment/ - Comment system |
| 28 | + │ ├── project/ - Overleaf project integration |
| 29 | + │ └── user/ - User settings and prompts |
| 30 | + ├── services/ - Business logic layer |
| 31 | + │ ├── chat.go/chat_v2.go - Chat service implementations |
| 32 | + │ ├── toolkit/ - AI tool calling framework |
| 33 | + │ │ ├── client/ - OpenAI API client wrappers |
| 34 | + │ │ ├── registry/ - Tool registration system |
| 35 | + │ │ └── tools/ - Available tools (file_read, latex parsing, etc.) |
| 36 | + ├── models/ - Domain models and database schemas |
| 37 | + └── libs/ - Shared libraries (db, jwt, logger, tex processing) |
| 38 | +proto/ - Protocol Buffer definitions (auth, chat, comment, project, user) |
| 39 | +pkg/gen/ - Generated gRPC/Protobuf code (auto-generated, don't edit) |
| 40 | +``` |
| 41 | + |
| 42 | +**Key architectural patterns:** |
| 43 | +- Dependency injection via Google Wire (`wire.go`, `wire_gen.go`) |
| 44 | +- Service layer pattern: API handlers → Services → Models → Database |
| 45 | +- Tool calling system for AI function execution in `internal/services/toolkit/` |
| 46 | +- Protocol Buffers for API contracts between frontend and backend |
| 47 | + |
| 48 | +### Frontend Structure |
| 49 | + |
| 50 | +``` |
| 51 | +webapp/ |
| 52 | + ├── _webapp/ - Main Chrome extension (uses bun/npm) |
| 53 | + │ ├── src/ |
| 54 | + │ │ ├── pkg/gen/ - Generated Protobuf client code |
| 55 | + │ │ └── (React components, stores, etc.) |
| 56 | + │ └── dist/ - Build output (load in Chrome) |
| 57 | + ├── office/ - Office Add-in (npm only, Word integration) |
| 58 | + │ └── src/paperdebugger/office.js - Built from _webapp |
| 59 | + └── oauth-landing/ - OAuth callback page |
| 60 | +``` |
| 61 | + |
| 62 | +**Frontend state management**: Zustand stores, React Query for API calls |
| 63 | + |
| 64 | +## Common Commands |
| 65 | + |
| 66 | +### Backend Development |
| 67 | + |
| 68 | +```bash |
| 69 | +# Install development dependencies (protoc, buf, wire, etc.) |
| 70 | +make deps |
| 71 | + |
| 72 | +# Generate Protocol Buffer code and Wire dependency injection |
| 73 | +make gen |
| 74 | + |
| 75 | +# Build backend binary |
| 76 | +make build |
| 77 | + |
| 78 | +# Run backend server (requires MongoDB on localhost:27017) |
| 79 | +./dist/pd.exe |
| 80 | +# Server starts on http://localhost:6060 |
| 81 | + |
| 82 | +# Format code (buf, go fmt, npm format in webapp) |
| 83 | +make fmt |
| 84 | + |
| 85 | +# Lint code (buf, golangci-lint, npm lint in webapp) |
| 86 | +make lint |
| 87 | + |
| 88 | +# Run tests with coverage |
| 89 | +make test |
| 90 | + |
| 91 | +# View test coverage in browser |
| 92 | +make test-view |
| 93 | +``` |
| 94 | + |
| 95 | +**Environment setup**: Copy `.env.example` to `.env` and configure MongoDB URI, OpenAI API keys, etc. |
| 96 | + |
| 97 | +**MongoDB requirement**: Start MongoDB locally with `docker run -d --name mongodb -p 27017:27017 mongo:latest` |
| 98 | + |
| 99 | +### Frontend Development |
| 100 | + |
| 101 | +#### Chrome Extension (webapp/_webapp) |
| 102 | + |
| 103 | +**Package manager**: Use `bun` or `npm` (both supported) |
| 104 | + |
| 105 | +```bash |
| 106 | +cd webapp/_webapp |
| 107 | + |
| 108 | +# Install dependencies |
| 109 | +npm install |
| 110 | + |
| 111 | +# Development mode (watch and rebuild on changes) |
| 112 | +npm run dev |
| 113 | + |
| 114 | +# Development server (for testing chat UI in browser) |
| 115 | +npm run dev:chat |
| 116 | + |
| 117 | +# Full production build (all components) |
| 118 | +npm run build |
| 119 | + |
| 120 | +# Build individual components |
| 121 | +npm run _build:default # Main extension UI |
| 122 | +npm run _build:background # Background service worker |
| 123 | +npm run _build:office # Office Add-in bundle |
| 124 | +npm run _build:settings # Settings page |
| 125 | +npm run _build:popup # Extension popup |
| 126 | + |
| 127 | +# Environment-specific builds |
| 128 | +npm run build:local:chrome # Local development |
| 129 | +npm run build:stg:chrome # Staging |
| 130 | +npm run build:prd:chrome # Production |
| 131 | + |
| 132 | +# Lint and format |
| 133 | +npm run lint |
| 134 | +npm run format |
| 135 | +``` |
| 136 | + |
| 137 | +**Build Office Add-in for Word:** |
| 138 | +```bash |
| 139 | +# From webapp/_webapp, build office.js bundle |
| 140 | +PD_API_ENDPOINT="https://app.paperdebugger.com" npm run _build:office |
| 141 | +# Outputs to: webapp/office/src/paperdebugger/office.js |
| 142 | +``` |
| 143 | + |
| 144 | +**Loading the extension in Chrome:** |
| 145 | +1. Run `npm run build` (or environment-specific build) |
| 146 | +2. Open `chrome://extensions/` |
| 147 | +3. Enable "Developer mode" |
| 148 | +4. Click "Load unpacked" and select `webapp/_webapp/dist` |
| 149 | + |
| 150 | +#### Office Add-in (webapp/office) |
| 151 | + |
| 152 | +**Package manager**: Use `npm` only (Office Add-in compatibility requirement) |
| 153 | + |
| 154 | +```bash |
| 155 | +cd webapp/office |
| 156 | + |
| 157 | +# Install dependencies (npm only!) |
| 158 | +npm install |
| 159 | + |
| 160 | +# Start development server (watches office.js for changes) |
| 161 | +npm run dev-server |
| 162 | + |
| 163 | +# Start Word and load the add-in |
| 164 | +npm run start |
| 165 | + |
| 166 | +# Stop the add-in |
| 167 | +npm run stop |
| 168 | + |
| 169 | +# Production build |
| 170 | +npm run build |
| 171 | + |
| 172 | +# Validate manifest |
| 173 | +npm run validate |
| 174 | +``` |
| 175 | + |
| 176 | +**Development workflow**: |
| 177 | +1. Build `office.js` from `webapp/_webapp` (see above) |
| 178 | +2. Run `npm run dev-server` in `webapp/office` to watch for changes |
| 179 | +3. Run `npm run start` to launch Word with the add-in loaded |
| 180 | + |
| 181 | +## Protocol Buffer Workflow |
| 182 | + |
| 183 | +When modifying API contracts: |
| 184 | + |
| 185 | +1. Edit `.proto` files in `proto/` directory |
| 186 | +2. Run `make gen` to regenerate Go and TypeScript code |
| 187 | +3. Backend code appears in `pkg/gen/` |
| 188 | +4. Frontend code appears in `webapp/_webapp/src/pkg/gen/` |
| 189 | +5. Never manually edit generated files |
| 190 | + |
| 191 | +## Testing |
| 192 | + |
| 193 | +```bash |
| 194 | +# Run all tests with coverage |
| 195 | +PD_MONGO_URI="mongodb://localhost:27017" go test -coverprofile=coverage.out ./cmd/... ./internal/... ./webapp/... |
| 196 | + |
| 197 | +# View coverage report in browser |
| 198 | +go tool cover -html=coverage.out |
| 199 | +``` |
| 200 | + |
| 201 | +## Key Concepts |
| 202 | + |
| 203 | +### Tool Calling System |
| 204 | +The AI chat uses a tool calling framework in `internal/services/toolkit/`: |
| 205 | +- Tools are registered in `registry/registry.go` |
| 206 | +- Each tool implements the tool interface (e.g., `tools/files/file_read.go`) |
| 207 | +- OpenAI function calling is wrapped in `toolkit/client/` |
| 208 | +- Tool execution results are stored in MongoDB via `db/tool_call_record.go` |
| 209 | + |
| 210 | +### XtraMCP Integration (Optional) |
| 211 | +XtraMCP is a closed-source MCP orchestration backend that provides: |
| 212 | +- Research-mode agents with literature search |
| 213 | +- AI-powered paper review and critique |
| 214 | +- Domain-specific academic writing revisions |
| 215 | + |
| 216 | +Local development works without XtraMCP. The error `"ERROR [AI Client] Failed to initialize XtraMCP session"` is expected when self-hosting without it. |
| 217 | + |
| 218 | +### Chat System |
| 219 | +Two implementations exist: |
| 220 | +- `chat.go` - Original implementation |
| 221 | +- `chat_v2.go` - Enhanced version with improved streaming |
| 222 | + |
| 223 | +Both use OpenAI's chat completion API with tool calling support. |
| 224 | + |
| 225 | +## Custom Endpoint Configuration |
| 226 | + |
| 227 | +Users can point the extension to a self-hosted backend: |
| 228 | +1. Open extension settings |
| 229 | +2. Click version number 5 times to enable "Developer Tools" |
| 230 | +3. Enter backend URL in "Backend Endpoint" field |
| 231 | +4. Refresh the page |
| 232 | + |
| 233 | +## Docker Deployment |
| 234 | + |
| 235 | +```bash |
| 236 | +# Build Docker image |
| 237 | +make image |
| 238 | + |
| 239 | +# Push to registry |
| 240 | +make push |
| 241 | +``` |
| 242 | + |
| 243 | +Images are tagged with branch name and commit hash to `ghcr.io/paperdebugger/sharelatex-paperdebugger`. |
0 commit comments