# Qwen Code Patcher

Patches [QwenCode CLI](https://github.com/QwenLM/qwen-code) (`@qwen-code/qwen-code`) to route all API requests through a custom AI proxy, disable telemetry, and auto-configure settings.

**[RU]** Патчер для QwenCode CLI — перенаправляет API запросы через пользовательский AI прокси, отключает телеметрию, автоматически настраивает окружение.

## Quick Start

```bash
# 1. Install from private registry
npm config set @qwen-code:registry https://npm.sensey24.ru/
npm install -g @qwen-code/qwen-code

# 2. Apply settings (env vars + settings.json)
python3 qwen_patcher.py --settings-only

# 3. Verify
qwen -p "Say hello"
```

## Windows

```powershell
npm config set "@qwen-code:registry" "https://npm.sensey24.ru/"
npm install -g @qwen-code/qwen-code
python3 qwen_patcher.py --settings-only
```

## Update

Same two commands — npm will pull the latest patched version:

```bash
npm config set @qwen-code:registry https://npm.sensey24.ru/
npm install -g @qwen-code/qwen-code
```

## What Gets Patched

| # | Target | Description |
|---|--------|-------------|
| 1 | telemetry_flag | Force `getTelemetryEnabled()` -> false |
| 2 | telemetry_log_prompts | Force `getTelemetryLogPromptsEnabled()` -> false |
| 3 | telemetry_init_guard | Early return in `initializeTelemetry()` |
| 4 | dashscope_base_url | `DEFAULT_DASHSCOPE_BASE_URL` -> proxy |
| 5 | coding_plan_urls | `coding.dashscope.aliyuncs.com` -> proxy |
| 6 | default_model | Validate `DEFAULT_QWEN_MODEL = "coder-model"` |
| 7 | mainline_model | Validate `MAINLINE_CODER_MODEL = "qwen3.5-plus"` |
| 8 | auto_update_registry | `registry.npmjs.org` -> private registry |
| 9 | auto_update_command | Add `--registry` to update commands |
| 10 | user_settings | Auth type=openai, telemetry=false, model |
| 11 | trusted_folders | Trust /home, /root, /tmp |
| 12 | system_env | OPENAI_API_KEY, OPENAI_BASE_URL, telemetry vars |

Targets 1-9 are pre-patched in the npm package. Targets 10-12 require running `qwen_patcher.py --settings-only`.

## Models

| Model | Description |
|-------|-------------|
| `qwen3.5-plus` | Qwen 3.5 Plus — default |
| `coder-model` | Direct OAuth model name |
| `qwen3-coder-plus` | Qwen3 Coder Plus |
| `qwen3-coder-flash` | Qwen3 Coder Flash (fast) |

## CLI Usage

```bash
# Detection
python3 qwen_patcher.py --detect

# Validation (GREEN/YELLOW/RED for each target)
python3 qwen_patcher.py --validate

# Full patch (cli.js + settings + env)
python3 qwen_patcher.py --apply

# Settings only (no cli.js modification)
python3 qwen_patcher.py --settings-only

# Rollback cli.js from backup
python3 qwen_patcher.py --rollback
```

## Troubleshooting

### "model not supported" error

Make sure your proxy has `qwen3.5-plus` mapped to `coder-model` in the OAuth model alias config. The Qwen OAuth endpoint only accepts `coder-model` as the model name.

### CLI doesn't start after patching

If you applied `--apply` and the CLI fails to start, run `--rollback` to restore from backup, then use the pre-patched npm package instead:

```bash
python3 qwen_patcher.py --rollback
npm install -g @qwen-code/qwen-code  # Re-installs pre-patched version
python3 qwen_patcher.py --settings-only
```
