# QwenBot Integration for Qwen Code

## Overview

QwenBot is an AI assistant tool that integrates with Qwen Code to provide enhanced capabilities through the Qwen API.

---

## Installation

### Option 1: Install as Qwen Code Plugin

```bash
# Navigate to Qwen Code plugins directory
cd ~/.qwen/plugins

# Clone qwenbot plugin
git clone https://github.com/QwenLM/qwenbot.git

# Install dependencies
cd qwenbot
npm install
```

### Option 2: Manual Setup

1. **Create plugin directory:**
```bash
mkdir -p ~/.qwen/plugins/qwenbot
```

2. **Create package.json:**
```json
{
  "name": "qwenbot",
  "version": "1.0.0",
  "description": "QwenBot AI assistant for Qwen Code",
  "main": "index.js",
  "scripts": {
    "start": "node index.js"
  },
  "dependencies": {
    "@qwen/sdk": "^1.0.0"
  }
}
```

3. **Install dependencies:**
```bash
cd ~/.qwen/plugins/qwenbot
npm install @qwen/sdk
```

---

## Configuration

### 1. Get Qwen API Key

Visit: https://platform.qwen.ai/api-keys

### 2. Create Configuration File

Create `~/.qwen/plugins/qwenbot/config.json`:

```json
{
  "apiKey": "your-qwen-api-key-here",
  "model": "qwen-plus",
  "temperature": 0.7,
  "maxTokens": 2048,
  "baseUrl": "https://api.qwen.ai/v1"
}
```

### 3. Enable in Qwen Code Settings

Edit `~/.qwen/settings.json`:

```json
{
  "enabledPlugins": [
    "qwenbot"
  ],
  "qwenbot": {
    "autoStart": true,
    "defaultModel": "qwen-plus"
  }
}
```

---

## Usage

### Basic Commands

Once installed, you can use qwenbot in Qwen Code:

```
/qwenbot <your question>
```

### Examples

```
/qwenbot Explain how async/await works in JavaScript

/qwenbot Review this code for potential bugs

/qwenbot Suggest improvements for this function

/qwenbot Generate unit tests for this module
```

### Advanced Usage

```
/qwenbot --model qwen-max Analyze this architecture

/qwenbot --temperature 0.9 Generate creative solutions for...

/qwenbot --context file:src/utils.js Explain this file
```

---

## API Reference

### QwenBot Class

```javascript
const QwenBot = require('./qwenbot');

const bot = new QwenBot({
  apiKey: 'your-api-key',
  model: 'qwen-plus',
});

// Chat with bot
const response = await bot.chat('Hello, how are you?');

// Chat with context
const response = await bot.chat('Explain this code', {
  context: 'const x = 10;',
  temperature: 0.5,
});

// Stream response
const stream = await bot.stream('Write a function that...');
for await (const chunk of stream) {
  process.stdout.write(chunk);
}
```

---

## Integration with QwenClaw

QwenBot can also integrate with QwenClaw daemon:

### 1. Add to QwenClaw Skills

Create `skills/qwenbot-integration/SKILL.md`:

```markdown
---
name: qwenbot-integration
description: Use QwenBot API for enhanced AI capabilities
---

# QwenBot Integration

This skill enables QwenClaw to use QwenBot API for:
- Enhanced code analysis
- Multi-turn conversations
- Specialized task handling

## Usage

```typescript
import { QwenBotClient } from './qwenbot';

const client = new QwenBotClient('api-key');

// Analyze code
const analysis = await client.analyzeCode(code);

// Generate documentation
const docs = await client.generateDocs(code);

// Chat assistance
const response = await client.chat('Help me with...');
```
```

### 2. Add to Rig Service

Update `rig-service/src/agent.rs` to include QwenBot provider:

```rust
fn get_provider_config(&self, provider: &str) -> Result<(String, Option<String>)> {
    match provider.to_lowercase().as_str() {
        "qwenbot" => {
            let api_key = std::env::var("QWENBOT_API_KEY")
                .map_err(|_| anyhow::anyhow!("QWENBOT_API_KEY not set"))?;
            let base_url = std::env::var("QWENBOT_BASE_URL").ok();
            Ok((api_key, base_url))
        }
        // ... other providers
    }
}
```

---

## Environment Variables

| Variable | Description | Default |
|----------|-------------|---------|
| `QWENBOT_API_KEY` | Your Qwen API key | Required |
| `QWENBOT_MODEL` | Default model | `qwen-plus` |
| `QWENBOT_BASE_URL` | API base URL | `https://api.qwen.ai/v1` |
| `QWENBOT_TEMPERATURE` | Default temperature | `0.7` |
| `QWENBOT_MAX_TOKENS` | Max tokens per response | `2048` |

---

## Troubleshooting

### Issue: "API Key not found"

**Solution:**
```bash
# Set environment variable
export QWENBOT_API_KEY="your-key-here"

# Or add to config file
echo '{"apiKey": "your-key-here"}' > ~/.qwen/plugins/qwenbot/config.json
```

### Issue: "Plugin not loading"

**Solution:**
```bash
# Check plugin directory
ls -la ~/.qwen/plugins/qwenbot

# Reinstall if needed
cd ~/.qwen/plugins/qwenbot
npm install
```

### Issue: "Rate limit exceeded"

**Solution:**
- Wait a few minutes before retrying
- Upgrade your Qwen API plan
- Reduce request frequency

---

## Resources

- **Qwen Platform:** https://platform.qwen.ai/
- **Qwen Documentation:** https://help.qwen.ai/
- **QwenBot GitHub:** https://github.com/QwenLM/qwenbot
- **API Reference:** https://platform.qwen.ai/docs/api

---

## License

MIT License - See LICENSE file for details.
