|
| 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 | +Cairo Coder is an open-source Cairo language code generation service using Retrieval-Augmented Generation (RAG) to transform natural language requests into functional Cairo smart contracts and programs. It was adapted from the Starknet Agent project. |
| 8 | + |
| 9 | +## Essential Commands |
| 10 | + |
| 11 | +### Development |
| 12 | + |
| 13 | +- `pnpm install` - Install dependencies (requires Node.js 20+ and pnpm 9+) |
| 14 | +- `pnpm dev` - Start all services in development mode with hot reload |
| 15 | +- `pnpm build` - Build all packages for production |
| 16 | +- `pnpm clean` - Clean package build files |
| 17 | +- `pnpm clean:all` - Clean all build files and node_modules |
| 18 | + |
| 19 | +### Testing |
| 20 | + |
| 21 | +- `pnpm test` - Run all tests across packages |
| 22 | +- `pnpm --filter @cairo-coder/agents test` - Run tests for specific package |
| 23 | +- `pnpm --filter @cairo-coder/agents test -- -t "test name"` - Run specific test |
| 24 | +- `pnpm --filter @cairo-coder/backend check-types` - Type check specific package |
| 25 | + |
| 26 | +### Documentation Ingestion |
| 27 | + |
| 28 | +- `pnpm generate-embeddings` - Interactive ingestion of documentation sources |
| 29 | +- `pnpm generate-embeddings:yes` - Non-interactive ingestion (for CI/CD) |
| 30 | + |
| 31 | +### Docker Operations |
| 32 | + |
| 33 | +- `docker compose up postgres backend` - Start main services |
| 34 | +- `docker compose up ingester` - Run documentation ingestion |
| 35 | + |
| 36 | +## High-Level Architecture |
| 37 | + |
| 38 | +### Monorepo Structure |
| 39 | + |
| 40 | +- **packages/agents**: Core RAG pipeline orchestrating query processing, document retrieval, and code generation |
| 41 | +- **packages/backend**: Express API server providing OpenAI-compatible endpoints |
| 42 | +- **packages/ingester**: Documentation processing system using template method pattern |
| 43 | +- **packages/typescript-config**: Shared TypeScript configuration |
| 44 | + |
| 45 | +### Key Design Patterns |
| 46 | + |
| 47 | +1. **RAG Pipeline** (packages/agents/src/core/pipeline/): |
| 48 | + |
| 49 | + - `QueryProcessor`: Reformulates user queries for better retrieval |
| 50 | + - `DocumentRetriever`: Searches pgvector database using similarity measures |
| 51 | + - `AnswerGenerator`: Generates Cairo code from retrieved documents |
| 52 | + - `McpPipeline`: Special mode returning raw documents without generation |
| 53 | + |
| 54 | +2. **Ingester System** (packages/ingester/src/ingesters/): |
| 55 | + |
| 56 | + - `BaseIngester`: Abstract class implementing template method pattern |
| 57 | + - Source-specific ingesters extend base class for each documentation source |
| 58 | + - Factory pattern (`IngesterFactory`) creates appropriate ingester instances |
| 59 | + |
| 60 | +3. **Multi-Provider LLM Support**: |
| 61 | + - Configurable providers: OpenAI, Anthropic, Google Gemini |
| 62 | + - Provider abstraction in agents package handles model differences |
| 63 | + - Streaming and non-streaming response modes |
| 64 | + |
| 65 | +### Configuration |
| 66 | + |
| 67 | +- Copy `packages/agents/sample.config.toml` to `config.toml` |
| 68 | +- Required configurations: |
| 69 | + - LLM provider API keys (OPENAI, GEMINI, ANTHROPIC) |
| 70 | + - Database connection in [VECTOR_DB] section |
| 71 | + - Model selection in [PROVIDERS] section |
| 72 | +- Environment variables: |
| 73 | + - Root `.env`: PostgreSQL initialization (POSTGRES_USER, POSTGRES_PASSWORD, POSTGRES_DB) |
| 74 | + - `packages/backend/.env`: Optional LangSmith tracing configuration |
| 75 | + |
| 76 | +### Database Architecture |
| 77 | + |
| 78 | +- PostgreSQL with pgvector extension for vector similarity search |
| 79 | +- Embedding storage for documentation chunks |
| 80 | +- Configurable similarity measures (cosine, dot product, euclidean) |
| 81 | + |
| 82 | +## Development Guidelines |
| 83 | + |
| 84 | +### Code Organization |
| 85 | + |
| 86 | +- Follow existing patterns in neighboring files |
| 87 | +- Use dependency injection for testability |
| 88 | +- Mock external dependencies (LLMs, databases) in tests |
| 89 | +- Prefer editing existing files over creating new ones |
| 90 | +- Follow template method pattern for new ingesters |
| 91 | + |
| 92 | +### Testing Approach |
| 93 | + |
| 94 | +- Jest for all testing |
| 95 | +- Test files in `__tests__/` directories |
| 96 | +- Mock LLM calls and database operations |
| 97 | +- Test each ingester implementation separately |
| 98 | +- Use descriptive test names explaining behavior |
| 99 | + |
| 100 | +### Adding New Documentation Sources |
| 101 | + |
| 102 | +1. Create new ingester extending `BaseIngester` in packages/ingester/src/ingesters/ |
| 103 | +2. Implement required abstract methods |
| 104 | +3. Register in `IngesterFactory` |
| 105 | +4. Update configuration if needed |
| 106 | + |
| 107 | +### MCP (Model Context Protocol) Mode |
| 108 | + |
| 109 | +- Special mode activated via `x-mcp-mode: true` header |
| 110 | +- Returns raw documentation chunks without LLM generation |
| 111 | +- Useful for integration with other tools needing Cairo documentation |
0 commit comments