|
| 1 | +# Qwen Code - Project Context |
| 2 | + |
| 3 | +## Project Overview |
| 4 | + |
| 5 | +**Qwen Code** is an open-source AI agent for the terminal, optimized for [Qwen3-Coder](https://github.com/QwenLM/Qwen3-Coder). It helps developers understand large codebases, automate tedious work, and ship faster. |
| 6 | + |
| 7 | +This project is based on [Google Gemini CLI](https://github.com/google-gemini/gemini-cli) with adaptations to better support Qwen-Coder models. |
| 8 | + |
| 9 | +### Key Features |
| 10 | + |
| 11 | +- **OpenAI-compatible, OAuth free tier**: Use an OpenAI-compatible API, or sign in with Qwen OAuth to get 1,000 free requests/day |
| 12 | +- **Agentic workflow, feature-rich**: Rich built-in tools (Skills, SubAgents, Plan Mode) for a full agentic workflow |
| 13 | +- **Terminal-first, IDE-friendly**: Built for developers who live in the command line, with optional integration for VS Code, Zed, and JetBrains IDEs |
| 14 | + |
| 15 | +## Technology Stack |
| 16 | + |
| 17 | +- **Runtime**: Node.js 20+ |
| 18 | +- **Language**: TypeScript 5.3+ |
| 19 | +- **Package Manager**: npm with workspaces |
| 20 | +- **Build Tool**: esbuild |
| 21 | +- **Testing**: Vitest |
| 22 | +- **Linting**: ESLint + Prettier |
| 23 | +- **UI Framework**: Ink (React for CLI) |
| 24 | +- **React Version**: 19.x |
| 25 | + |
| 26 | +## Project Structure |
| 27 | + |
| 28 | +``` |
| 29 | +├── packages/ |
| 30 | +│ ├── cli/ # Command-line interface (main entry point) |
| 31 | +│ ├── core/ # Core backend logic and tool implementations |
| 32 | +│ ├── sdk-java/ # Java SDK |
| 33 | +│ ├── sdk-typescript/ # TypeScript SDK |
| 34 | +│ ├── test-utils/ # Shared testing utilities |
| 35 | +│ ├── vscode-ide-companion/ # VS Code extension companion |
| 36 | +│ ├── webui/ # Web UI components |
| 37 | +│ └── zed-extension/ # Zed editor extension |
| 38 | +├── scripts/ # Build and utility scripts |
| 39 | +├── docs/ # Documentation source |
| 40 | +├── docs-site/ # Documentation website (Next.js) |
| 41 | +├── integration-tests/ # End-to-end integration tests |
| 42 | +└── eslint-rules/ # Custom ESLint rules |
| 43 | +``` |
| 44 | + |
| 45 | +### Package Details |
| 46 | + |
| 47 | +#### `@qwen-code/qwen-code` (packages/cli/) |
| 48 | + |
| 49 | +The main CLI package providing: |
| 50 | + |
| 51 | +- Interactive terminal UI using Ink/React |
| 52 | +- Non-interactive/headless mode |
| 53 | +- Authentication handling (OAuth, API keys) |
| 54 | +- Configuration management |
| 55 | +- Command system (`/help`, `/clear`, `/compress`, etc.) |
| 56 | + |
| 57 | +#### `@qwen-code/qwen-code-core` (packages/core/) |
| 58 | + |
| 59 | +Core library containing: |
| 60 | + |
| 61 | +- **Tools**: File operations (read, write, edit, glob, grep), shell execution, web fetch, LSP integration, MCP client |
| 62 | +- **Subagents**: Task delegation to specialized agents |
| 63 | +- **Skills**: Reusable skill system |
| 64 | +- **Models**: Model configuration and registry for Qwen and OpenAI-compatible APIs |
| 65 | +- **Services**: Git integration, file discovery, session management |
| 66 | +- **LSP Support**: Language Server Protocol integration |
| 67 | +- **MCP**: Model Context Protocol implementation |
| 68 | + |
| 69 | +## Building and Running |
| 70 | + |
| 71 | +### Prerequisites |
| 72 | + |
| 73 | +- **Node.js**: ~20.19.0 for development (use nvm to manage versions) |
| 74 | +- **Git** |
| 75 | +- For sandboxing: Docker or Podman (optional but recommended) |
| 76 | + |
| 77 | +### Setup |
| 78 | + |
| 79 | +```bash |
| 80 | +# Clone and install |
| 81 | +git clone https://github.com/QwenLM/qwen-code.git |
| 82 | +cd qwen-code |
| 83 | +npm install |
| 84 | +``` |
| 85 | + |
| 86 | +### Build Commands |
| 87 | + |
| 88 | +```bash |
| 89 | +# Build all packages |
| 90 | +npm run build |
| 91 | + |
| 92 | +# Build everything including sandbox and VSCode companion |
| 93 | +npm run build:all |
| 94 | + |
| 95 | +# Build only packages |
| 96 | +npm run build:packages |
| 97 | + |
| 98 | +# Development mode with hot reload |
| 99 | +npm run dev |
| 100 | + |
| 101 | +# Bundle for distribution |
| 102 | +npm run bundle |
| 103 | +``` |
| 104 | + |
| 105 | +### Running |
| 106 | + |
| 107 | +```bash |
| 108 | +# Start interactive CLI |
| 109 | +npm start |
| 110 | + |
| 111 | +# Or after global installation |
| 112 | +qwen |
| 113 | + |
| 114 | +# Debug mode |
| 115 | +npm run debug |
| 116 | + |
| 117 | +# With environment variables |
| 118 | +DEBUG=1 npm start |
| 119 | +``` |
| 120 | + |
| 121 | +### Testing |
| 122 | + |
| 123 | +```bash |
| 124 | +# Run all unit tests |
| 125 | +npm run test |
| 126 | + |
| 127 | +# Run integration tests (no sandbox) |
| 128 | +npm run test:e2e |
| 129 | + |
| 130 | +# Run all integration tests with different sandbox modes |
| 131 | +npm run test:integration:all |
| 132 | + |
| 133 | +# Terminal benchmark tests |
| 134 | +npm run test:terminal-bench |
| 135 | +``` |
| 136 | + |
| 137 | +### Code Quality |
| 138 | + |
| 139 | +```bash |
| 140 | +# Run all checks (lint, format, build, test) |
| 141 | +npm run preflight |
| 142 | + |
| 143 | +# Lint only |
| 144 | +npm run lint |
| 145 | +npm run lint:fix |
| 146 | + |
| 147 | +# Format only |
| 148 | +npm run format |
| 149 | + |
| 150 | +# Type check |
| 151 | +npm run typecheck |
| 152 | +``` |
| 153 | + |
| 154 | +## Development Conventions |
| 155 | + |
| 156 | +### Code Style |
| 157 | + |
| 158 | +- **Strict TypeScript**: All strict flags enabled (`strictNullChecks`, `noImplicitAny`, etc.) |
| 159 | +- **Module System**: ES modules (`"type": "module"`) |
| 160 | +- **Import Style**: Node.js native ESM with `.js` extensions in imports |
| 161 | +- **No Relative Imports Between Packages**: ESLint enforces this restriction |
| 162 | + |
| 163 | +### Key Configuration Files |
| 164 | + |
| 165 | +- `tsconfig.json`: Base TypeScript configuration with strict settings |
| 166 | +- `eslint.config.js`: ESLint flat config with custom rules |
| 167 | +- `esbuild.config.js`: Build configuration |
| 168 | +- `vitest.config.ts`: Test configuration |
| 169 | + |
| 170 | +### Import Patterns |
| 171 | + |
| 172 | +```typescript |
| 173 | +// Within a package - use relative paths |
| 174 | +import { something } from './utils/something.js'; |
| 175 | + |
| 176 | +// Between packages - use package names |
| 177 | +import { Config } from '@qwen-code/qwen-code-core'; |
| 178 | +``` |
| 179 | + |
| 180 | +### Testing Patterns |
| 181 | + |
| 182 | +- Unit tests co-located with source files (`.test.ts` suffix) |
| 183 | +- Integration tests in separate `integration-tests/` directory |
| 184 | +- Uses Vitest with globals enabled |
| 185 | +- Mocking via `msw` for HTTP, `memfs`/`mock-fs` for filesystem |
| 186 | + |
| 187 | +### Architecture Patterns |
| 188 | + |
| 189 | +#### Tools System |
| 190 | + |
| 191 | +All tools extend `BaseDeclarativeTool` or implement the tool interfaces: |
| 192 | + |
| 193 | +- Located in `packages/core/src/tools/` |
| 194 | +- Each tool has a corresponding `.test.ts` file |
| 195 | +- Tools are registered in the tool registry |
| 196 | + |
| 197 | +#### Subagents System |
| 198 | + |
| 199 | +Task delegation framework: |
| 200 | + |
| 201 | +- Configuration stored as Markdown + YAML frontmatter |
| 202 | +- Supports both project-level and user-level subagents |
| 203 | +- Event-driven architecture for UI updates |
| 204 | + |
| 205 | +#### Configuration System |
| 206 | + |
| 207 | +Hierarchical configuration loading: |
| 208 | + |
| 209 | +1. Default values |
| 210 | +2. User settings (`~/.qwen/settings.json`) |
| 211 | +3. Project settings (`.qwen/settings.json`) |
| 212 | +4. Environment variables |
| 213 | +5. CLI flags |
| 214 | + |
| 215 | +### Authentication Methods |
| 216 | + |
| 217 | +1. **Qwen OAuth** (recommended): Browser-based OAuth flow |
| 218 | +2. **OpenAI-compatible API**: Via `OPENAI_API_KEY` environment variable |
| 219 | + |
| 220 | +Environment variables for API mode: |
| 221 | + |
| 222 | +```bash |
| 223 | +export OPENAI_API_KEY="your-api-key" |
| 224 | +export OPENAI_BASE_URL="https://api.openai.com/v1" # optional |
| 225 | +export OPENAI_MODEL="gpt-4o" # optional |
| 226 | +``` |
| 227 | + |
| 228 | +## Debugging |
| 229 | + |
| 230 | +### VS Code |
| 231 | + |
| 232 | +Press `F5` to launch with debugger attached, or: |
| 233 | + |
| 234 | +```bash |
| 235 | +npm run debug # Runs with --inspect-brk |
| 236 | +``` |
| 237 | + |
| 238 | +### React DevTools (for CLI UI) |
| 239 | + |
| 240 | +```bash |
| 241 | +DEV=true npm start |
| 242 | +npx react-devtools@4.28.5 |
| 243 | +``` |
| 244 | + |
| 245 | +### Sandbox Debugging |
| 246 | + |
| 247 | +```bash |
| 248 | +DEBUG=1 qwen |
| 249 | +``` |
| 250 | + |
| 251 | +## Documentation |
| 252 | + |
| 253 | +- User documentation: <https://qwenlm.github.io/qwen-code-docs/> |
| 254 | +- Local docs development: |
| 255 | + |
| 256 | + ```bash |
| 257 | + cd docs-site |
| 258 | + npm install |
| 259 | + npm run link # Links ../docs to content |
| 260 | + npm run dev # http://localhost:3000 |
| 261 | + ``` |
| 262 | + |
| 263 | +## Contributing Guidelines |
| 264 | + |
| 265 | +See [CONTRIBUTING.md](./CONTRIBUTING.md) for detailed guidelines. Key points: |
| 266 | + |
| 267 | +1. Link PRs to existing issues |
| 268 | +2. Keep PRs small and focused |
| 269 | +3. Use Draft PRs for WIP |
| 270 | +4. Ensure `npm run preflight` passes |
| 271 | +5. Update documentation for user-facing changes |
| 272 | +6. Follow Conventional Commits for commit messages |
| 273 | + |
| 274 | +## Useful Commands Reference |
| 275 | + |
| 276 | +| Command | Description | |
| 277 | +| ------------------- | -------------------------------------------------------------------- | |
| 278 | +| `npm start` | Start CLI in interactive mode | |
| 279 | +| `npm run dev` | Development mode with hot reload | |
| 280 | +| `npm run build` | Build all packages | |
| 281 | +| `npm run test` | Run unit tests | |
| 282 | +| `npm run test:e2e` | Run integration tests | |
| 283 | +| `npm run preflight` | Full CI check (clean, install, format, lint, build, typecheck, test) | |
| 284 | +| `npm run lint` | Run ESLint | |
| 285 | +| `npm run format` | Run Prettier | |
| 286 | +| `npm run clean` | Clean build artifacts | |
| 287 | + |
| 288 | +## Session Commands (within CLI) |
| 289 | + |
| 290 | +- `/help` - Display available commands |
| 291 | +- `/clear` - Clear conversation history |
| 292 | +- `/compress` - Compress history to save tokens |
| 293 | +- `/stats` - Show session information |
| 294 | +- `/bug` - Submit bug report |
| 295 | +- `/exit` or `/quit` - Exit Qwen Code |
| 296 | + |
| 297 | +--- |
0 commit comments