|
| 1 | +# CLAUDE.md |
| 2 | + |
| 3 | +This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository. |
| 4 | + |
| 5 | +## Development Commands |
| 6 | + |
| 7 | +### Build Commands |
| 8 | +- `npm run build` - Full production build (clean, esbuild, generate types for both CJS and ESM) |
| 9 | +- `npm run build:esbuild` - Run esbuild bundling only |
| 10 | +- `npm run clean` - Remove dist directory |
| 11 | +- `npm run dev:cli` - Development mode with file watching for CLI (watches src/bin) |
| 12 | + |
| 13 | +### Type Checking and Linting |
| 14 | +- `npm run typecheck` - Run TypeScript type checking without emitting files |
| 15 | +- `npm run format` - Format code using Prettier |
| 16 | + |
| 17 | +### CLI Development |
| 18 | +- `npm run dev:cli` - Start development mode with file watching specifically for CLI changes |
| 19 | +- The CLI binary is built to `dist/bin/codesandbox.mjs` and made executable |
| 20 | +- CLI can be run with `./dist/bin/codesandbox.mjs` or just `csb` if installed globally |
| 21 | + |
| 22 | +### API Client Generation |
| 23 | +- `npm run build-openapi` - Generate API clients from production OpenAPI spec |
| 24 | +- `npm run build-openapi:staging` - Generate API clients from staging OpenAPI spec |
| 25 | +- Generated clients are placed in `src/api-clients/` directory |
| 26 | + |
| 27 | +## Architecture |
| 28 | + |
| 29 | +### Core Components |
| 30 | + |
| 31 | +**SDK Entry Points:** |
| 32 | +- `src/index.ts` - Main SDK export with `CodeSandbox` class |
| 33 | +- `src/browser/index.ts` - Browser-specific SDK build |
| 34 | +- `src/node/index.ts` - Node.js-specific SDK build |
| 35 | + |
| 36 | +**Key Classes:** |
| 37 | +- `CodeSandbox` - Main SDK class providing `sandboxes` and `hosts` properties |
| 38 | +- `Sandboxes` (src/Sandboxes.ts) - Manages sandbox operations (create, list, get, fork) |
| 39 | +- `Sandbox` (src/Sandbox.ts) - Individual sandbox instance with connection and session management |
| 40 | +- `SandboxClient` (src/SandboxClient/index.ts) - High-level client for sandbox interactions |
| 41 | +- `API` (src/API.ts) - Low-level API wrapper for all CodeSandbox REST endpoints |
| 42 | + |
| 43 | +**Communication Layer:** |
| 44 | +- `src/pitcher-protocol/` - WebSocket protocol definitions and message types for real-time sandbox communication |
| 45 | +- `src/AgentClient/` - WebSocket client implementation for connecting to sandbox agents |
| 46 | + |
| 47 | +**CLI:** |
| 48 | +- `src/bin/main.tsx` - CLI entry point using Ink (React for CLI) |
| 49 | +- `src/bin/commands/` - Individual CLI commands (build, sandbox, previewHosts, hostTokens) |
| 50 | +- `src/bin/ui/` - Ink-based UI components for the interactive CLI |
| 51 | + |
| 52 | +### Build System |
| 53 | + |
| 54 | +**Multi-Format Output:** |
| 55 | +- ESM build: `dist/esm/` (primary format, "type": "module") |
| 56 | +- CommonJS build: `dist/cjs/` (compatibility) |
| 57 | +- Browser build: Separate bundles with browser polyfills |
| 58 | +- CLI build: Standalone executable with shebang |
| 59 | + |
| 60 | +**Key Build Features:** |
| 61 | +- esbuild for bundling with custom plugins for module replacement |
| 62 | +- Separate TypeScript compilation for type definitions |
| 63 | +- Browser polyfills for Node.js modules (os, path, crypto, etc.) |
| 64 | +- External dependencies preserved except for CLI build which bundles React/Ink |
| 65 | + |
| 66 | +### Type System |
| 67 | + |
| 68 | +**Configuration:** |
| 69 | +- Main tsconfig.json excludes `src/bin` and `src/api-clients` |
| 70 | +- Separate build configs for CJS (`tsconfig.build-cjs.json`) and ESM (`tsconfig.build-esm.json`) |
| 71 | +- Strict TypeScript configuration with comprehensive type checking |
| 72 | + |
| 73 | +**Generated Types:** |
| 74 | +- `src/api-clients/` contains auto-generated API clients and types from OpenAPI specs |
| 75 | +- Multiple API client modules for different sandbox services (fs, git, shell, etc.) |
| 76 | + |
| 77 | +### Authentication & Configuration |
| 78 | + |
| 79 | +**API Authentication:** |
| 80 | +- Requires CodeSandbox API token (CSB_API_KEY environment variable) |
| 81 | +- Token creation: https://codesandbox.io/t/api |
| 82 | +- Automatic User-Agent header injection with SDK version |
| 83 | + |
| 84 | +**Environment Detection:** |
| 85 | +- Automatic base URL inference based on API key format |
| 86 | +- Support for different CodeSandbox environments (production, staging) |
| 87 | + |
| 88 | +### Testing |
| 89 | + |
| 90 | +The codebase includes a `test-template/` directory with a Vite + React + TypeScript template for SDK testing. |
| 91 | + |
| 92 | +## Documentation |
| 93 | + |
| 94 | +**SDK Documentation:** https://codesandbox.io/docs/sdk |
| 95 | + |
| 96 | +**Contributing to Documentation:** |
| 97 | +- Documentation source is located at https://github.com/codesandbox/docs |
| 98 | +- SDK-specific docs are in `packages/projects-docs/pages/sdk/` folder |
| 99 | +- Update documentation there when making changes to SDK functionality |
| 100 | + |
| 101 | +## Key Patterns |
| 102 | + |
| 103 | +**Error Handling:** |
| 104 | +- `handleResponse` utility for consistent API error handling |
| 105 | +- Retry logic with exponential backoff for critical operations |
| 106 | +- OpenTelemetry tracing integration for observability |
| 107 | + |
| 108 | +**Session Management:** |
| 109 | +- Support for custom sessions with git credentials and environment variables |
| 110 | +- Browser session creation for web-based sandbox interactions |
| 111 | +- Automatic session cleanup and disposal |
| 112 | + |
| 113 | +**Resource Management:** |
| 114 | +- VM tier scaling and hibernation timeout management |
| 115 | +- Cluster and bootup type tracking |
| 116 | +- Agent version management and update detection |
0 commit comments