opencode-qwen 1.0.1 → 1.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +81 -104
- package/dist/index.d.ts +34 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +172 -0
- package/package.json +17 -11
- package/.github/workflows/main.yml +0 -30
- package/auth.ts +0 -165
- package/fix_summary.md +0 -28
- package/index.ts +0 -223
- package/opencode.example.json +0 -34
- package/qwen-auth.ts +0 -337
package/README.md
CHANGED
|
@@ -1,142 +1,119 @@
|
|
|
1
|
-
# Qwen
|
|
1
|
+
# Qwen Provider for OpenCode
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Qwen AI provider integration for OpenCode with automatic config setup.
|
|
4
4
|
|
|
5
|
-
##
|
|
5
|
+
## Quick Start
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
- **Secure Credential Handling**: Integrates with environment variables
|
|
9
|
-
- **Token Refresh Support**: Handles access token renewal using refresh tokens
|
|
10
|
-
- **Custom Tools**: Provides dedicated tools for Qwen API interactions
|
|
11
|
-
- **Logging & Monitoring**: Comprehensive logging for debugging and monitoring
|
|
12
|
-
- **Type Safety**: Full TypeScript support with Zod validation
|
|
7
|
+
### Option 1: Use `/connect` Command (Recommended)
|
|
13
8
|
|
|
14
|
-
|
|
9
|
+
1. Copy the content of `opencode.json` file to your OpenCode config location:
|
|
10
|
+
```
|
|
11
|
+
~/.config/opencode/opencode.json
|
|
12
|
+
```
|
|
13
|
+
Or on Windows:
|
|
14
|
+
```
|
|
15
|
+
%APPDATA%\opencode\opencode.json
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
2. Restart OpenCode
|
|
15
19
|
|
|
16
|
-
|
|
20
|
+
3. Run `/connect` command in OpenCode TUI:
|
|
21
|
+
- Select "Other" as provider type
|
|
22
|
+
- Enter provider ID: `qwen`
|
|
23
|
+
- Enter your Qwen API key
|
|
24
|
+
|
|
25
|
+
4. Run `/models` to see Qwen models
|
|
26
|
+
|
|
27
|
+
### Option 2: Manual Installation
|
|
28
|
+
|
|
29
|
+
1. Place plugin files in your OpenCode plugins directory:
|
|
17
30
|
```
|
|
18
31
|
.opencode/
|
|
19
32
|
├── plugins/
|
|
20
|
-
│ └──
|
|
33
|
+
│ └── dist/
|
|
34
|
+
│ └── index.js
|
|
35
|
+
├── index.ts
|
|
36
|
+
├── opencode.json <-- Copy this file
|
|
21
37
|
└── package.json
|
|
22
38
|
```
|
|
23
39
|
|
|
24
40
|
2. Install dependencies:
|
|
25
41
|
```bash
|
|
26
|
-
|
|
42
|
+
bun install
|
|
27
43
|
```
|
|
28
44
|
|
|
29
|
-
|
|
45
|
+
3. Set your Qwen API key as environment variable:
|
|
46
|
+
```bash
|
|
47
|
+
export QWEN_API_KEY=your_access_token_here
|
|
48
|
+
```
|
|
30
49
|
|
|
31
|
-
|
|
50
|
+
## Getting Your API Key
|
|
32
51
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
52
|
+
1. Visit [chat.qwen.ai](https://chat.qwen.ai) and log in
|
|
53
|
+
2. Open browser console (F12 → Console tab)
|
|
54
|
+
3. Paste this JavaScript code:
|
|
55
|
+
```javascript
|
|
56
|
+
(function(){if(window.location.hostname!=="chat.qwen.ai"){alert("🚀 This code is for chat.qwen.ai");window.open("https://chat.qwen.ai","_blank");return;}
|
|
57
|
+
function getApiKeyData(){const token=localStorage.getItem("token");if(!token){alert("❌ qwen access_token not found !!!");return null;}
|
|
58
|
+
return token;}
|
|
59
|
+
async function copyToClipboard(text){try{await navigator.clipboard.writeText(text);return true;}catch(err){console.error("❌ Failed to copy to clipboard:",err);const textarea=document.createElement("textarea");textarea.value=text;textarea.style.position="fixed";textarea.style.opacity="0";document.body.appendChild(textarea);textarea.focus();textarea.select();const success=document.execCommand("copy");document.body.removeChild(textarea);return success;}}
|
|
60
|
+
const apiKeyData=getApiKeyData();if(!apiKeyData)return;copyToClipboard(apiKeyData).then((success)=>{if(success){alert("🔑 Qwen access_token copied to clipboard !!! 🎉");}else{prompt("🔰 Qwen access_token:",apiKeyData);}});})();
|
|
61
|
+
```
|
|
62
|
+
4. Copy the API key and set it as `QWEN_API_KEY`
|
|
36
63
|
|
|
37
|
-
|
|
38
|
-
QWEN_REFRESH_TOKEN=your_refresh_token_here
|
|
64
|
+
## Configuration Details
|
|
39
65
|
|
|
40
|
-
|
|
41
|
-
QWEN_AUTO_REFRESH=true
|
|
66
|
+
The `opencode.json` file configures:
|
|
42
67
|
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
68
|
+
- **Qwen Provider**: Points to the opencode-qwen plugin
|
|
69
|
+
- **Models**: 10 pre-configured Qwen models
|
|
70
|
+
- **Environment Variables**: Uses `QWEN_API_KEY` for authentication
|
|
46
71
|
|
|
47
|
-
##
|
|
72
|
+
## Available Models
|
|
48
73
|
|
|
49
|
-
|
|
74
|
+
| Model ID | Name | Capabilities |
|
|
75
|
+
|-----------|------|--------------|
|
|
76
|
+
| `qwen-turbo` | Qwen Turbo | Fast, efficient |
|
|
77
|
+
| `qwen-plus` | Qwen Plus | Complex reasoning, tool calling |
|
|
78
|
+
| `qwen-max` | Qwen Max | Most capable, advanced tasks |
|
|
79
|
+
| `qwen-coder` | Qwen Coder | Coding specialized |
|
|
80
|
+
| `qwen-vl` | Qwen VL | Multimodal, supports images |
|
|
81
|
+
| `qwen-deep-research` | Qwen Deep Research | Web search enabled |
|
|
82
|
+
| `qwen-web-dev` | Qwen Web Dev | Web development |
|
|
83
|
+
| `qwen-full-stack` | Qwen Full Stack | Full-stack apps |
|
|
84
|
+
| `qwen-omni` | Qwen Omni | Extensive multimodal |
|
|
50
85
|
|
|
51
|
-
|
|
86
|
+
## For AI Agents
|
|
52
87
|
|
|
53
|
-
|
|
54
|
-
Validate your Qwen API access token.
|
|
88
|
+
When using this with an AI agent, simply copy the contents of `opencode.json` from this repository to your OpenCode config file location:
|
|
55
89
|
|
|
56
|
-
```javascript
|
|
57
|
-
// Example usage
|
|
58
|
-
const result = await qwen.validate-token({
|
|
59
|
-
token: "optional_token_to_validate" // Uses current token if not provided
|
|
60
|
-
})
|
|
61
90
|
```
|
|
62
|
-
|
|
63
|
-
#### `qwen.refresh-token`
|
|
64
|
-
Refresh your access token using the refresh token.
|
|
65
|
-
|
|
66
|
-
```javascript
|
|
67
|
-
// Example usage
|
|
68
|
-
const result = await qwen.refresh-token({
|
|
69
|
-
refreshToken: "optional_refresh_token" // Uses stored refresh token if not provided
|
|
70
|
-
})
|
|
91
|
+
~/.config/opencode/opencode.json
|
|
71
92
|
```
|
|
72
93
|
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
```javascript
|
|
77
|
-
// Example usage
|
|
78
|
-
const models = await qwen.list-models({})
|
|
79
|
-
console.log(`Available models: ${models.data.length}`)
|
|
94
|
+
Or on Windows:
|
|
95
|
+
```
|
|
96
|
+
%APPDATA%\opencode\opencode.json
|
|
80
97
|
```
|
|
81
98
|
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
The plugin automatically handles:
|
|
85
|
-
|
|
86
|
-
- **Session Validation**: Validates tokens when a new session starts
|
|
87
|
-
- **Periodic Refresh**: Checks token validity during idle periods
|
|
88
|
-
- **API Call Interception**: Ensures valid tokens before Qwen API calls
|
|
89
|
-
- **Error Recovery**: Attempts token refresh on validation failures
|
|
90
|
-
|
|
91
|
-
## API Endpoints
|
|
92
|
-
|
|
93
|
-
The plugin integrates with the following Qwen API endpoints:
|
|
94
|
-
|
|
95
|
-
- `GET /validate` - Token validation
|
|
96
|
-
- `POST /refresh` - Token refresh
|
|
97
|
-
- `GET /models` - List available models
|
|
98
|
-
|
|
99
|
-
## Security Considerations
|
|
100
|
-
|
|
101
|
-
- Store API keys and refresh tokens in environment variables, not in code
|
|
102
|
-
- The plugin never logs sensitive credential information
|
|
103
|
-
- Token validation is performed over secure HTTPS connections
|
|
104
|
-
- Consider rotating API keys regularly for enhanced security
|
|
105
|
-
|
|
106
|
-
## Troubleshooting
|
|
107
|
-
|
|
108
|
-
### Common Issues
|
|
109
|
-
|
|
110
|
-
1. **"No valid Qwen authentication token available"**
|
|
111
|
-
- Ensure `QWEN_API_KEY` is set correctly
|
|
112
|
-
- Check if the token has expired and refresh if needed
|
|
113
|
-
|
|
114
|
-
2. **"No refresh token available"**
|
|
115
|
-
- Set `QWEN_REFRESH_TOKEN` environment variable
|
|
116
|
-
- Obtain a refresh token from your Qwen API dashboard
|
|
117
|
-
|
|
118
|
-
3. **"Failed to fetch models"**
|
|
119
|
-
- Validate your token first using `qwen.validate-token`
|
|
120
|
-
- Check network connectivity to `qwen.aikit.club`
|
|
121
|
-
|
|
122
|
-
### Debug Logging
|
|
123
|
-
|
|
124
|
-
Enable debug logging to troubleshoot issues:
|
|
125
|
-
|
|
99
|
+
Then set the environment variable:
|
|
126
100
|
```bash
|
|
127
|
-
|
|
128
|
-
export DEBUG=qwen-auth:*
|
|
101
|
+
export QWEN_API_KEY=your_key_here
|
|
129
102
|
```
|
|
130
103
|
|
|
131
|
-
##
|
|
104
|
+
## Troubleshooting
|
|
132
105
|
|
|
133
|
-
|
|
106
|
+
### Qwen provider not showing up?
|
|
107
|
+
1. Ensure `QWEN_API_KEY` environment variable is set
|
|
108
|
+
2. Restart OpenCode after setting the variable
|
|
109
|
+
3. Check that `opencode-qwen` is in the `plugin` array of your config
|
|
134
110
|
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
-
|
|
138
|
-
-
|
|
111
|
+
### Models not appearing?
|
|
112
|
+
The plugin uses dynamic model loading. Models are fetched from the API when:
|
|
113
|
+
- Your `QWEN_API_KEY` is valid
|
|
114
|
+
- Network connection to `qwen.aikit.club` works
|
|
115
|
+
- The plugin is properly loaded
|
|
139
116
|
|
|
140
117
|
## License
|
|
141
118
|
|
|
142
|
-
|
|
119
|
+
MIT License. Created by OpenCode Community.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Qwen Provider for OpenCode
|
|
3
|
+
*
|
|
4
|
+
* This package provides Qwen API integration for OpenCode with dynamic model loading.
|
|
5
|
+
* Models are fetched dynamically from the Qwen API.
|
|
6
|
+
*/
|
|
7
|
+
export interface QwenApiModel {
|
|
8
|
+
id: string;
|
|
9
|
+
object: string;
|
|
10
|
+
created: number;
|
|
11
|
+
owned_by: string;
|
|
12
|
+
permission?: any[];
|
|
13
|
+
root?: string;
|
|
14
|
+
parent?: string;
|
|
15
|
+
}
|
|
16
|
+
export interface QwenModelsResponse {
|
|
17
|
+
object: string;
|
|
18
|
+
data: QwenApiModel[];
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Fetch models from Qwen API
|
|
22
|
+
*/
|
|
23
|
+
export declare function fetchQwenModels(apiKey: string, baseURL?: string): Promise<Record<string, any>>;
|
|
24
|
+
/**
|
|
25
|
+
* Validate Qwen API key
|
|
26
|
+
*/
|
|
27
|
+
export declare function validateQwenApiKey(apiKey: string): Promise<boolean>;
|
|
28
|
+
/**
|
|
29
|
+
* Qwen Plugin for OpenCode
|
|
30
|
+
*/
|
|
31
|
+
export default function QwenPlugin({ project, client, $, directory, worktree }: any): Promise<{
|
|
32
|
+
config(config: any): Promise<void>;
|
|
33
|
+
}>;
|
|
34
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAQH,MAAM,WAAW,YAAY;IAC3B,EAAE,EAAE,MAAM,CAAA;IACV,MAAM,EAAE,MAAM,CAAA;IACd,OAAO,EAAE,MAAM,CAAA;IACf,QAAQ,EAAE,MAAM,CAAA;IAChB,UAAU,CAAC,EAAE,GAAG,EAAE,CAAA;IAClB,IAAI,CAAC,EAAE,MAAM,CAAA;IACb,MAAM,CAAC,EAAE,MAAM,CAAA;CAChB;AAED,MAAM,WAAW,kBAAkB;IACjC,MAAM,EAAE,MAAM,CAAA;IACd,IAAI,EAAE,YAAY,EAAE,CAAA;CACrB;AA0CD;;GAEG;AACH,wBAAsB,eAAe,CAAC,MAAM,EAAE,MAAM,EAAE,OAAO,GAAE,MAAsB,GAAG,OAAO,CAAC,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC,CAwDnH;AAED;;GAEG;AACH,wBAAsB,kBAAkB,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC,CAczE;AAED;;GAEG;AACH,wBAA8B,UAAU,CAAC,EAAE,OAAO,EAAE,MAAM,EAAE,CAAC,EAAE,SAAS,EAAE,QAAQ,EAAE,EAAE,GAAG;mBAkBhE,GAAG;GAoC3B"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Qwen Provider for OpenCode
|
|
4
|
+
*
|
|
5
|
+
* This package provides Qwen API integration for OpenCode with dynamic model loading.
|
|
6
|
+
* Models are fetched dynamically from the Qwen API.
|
|
7
|
+
*/
|
|
8
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
+
exports.fetchQwenModels = fetchQwenModels;
|
|
10
|
+
exports.validateQwenApiKey = validateQwenApiKey;
|
|
11
|
+
exports.default = QwenPlugin;
|
|
12
|
+
/// <reference types="node"/>
|
|
13
|
+
// Qwen API base URL
|
|
14
|
+
const QWEN_BASE_URL = 'https://qwen.aikit.club/v1';
|
|
15
|
+
// Model cache to avoid repeated API calls
|
|
16
|
+
let modelCache = null;
|
|
17
|
+
let modelCacheTime = 0;
|
|
18
|
+
const CACHE_DURATION = 5 * 60 * 1000; // 5 minutes
|
|
19
|
+
/**
|
|
20
|
+
* Convert Qwen API model to OpenCode Model format
|
|
21
|
+
*/
|
|
22
|
+
function convertApiModelToModel(apiModel) {
|
|
23
|
+
const modelId = apiModel.id;
|
|
24
|
+
// Extract capabilities from model name
|
|
25
|
+
const isVisionModel = modelId.includes('vl') || modelId.includes('vision') || modelId.includes('qvq');
|
|
26
|
+
const isCoderModel = modelId.includes('coder') || modelId.includes('code');
|
|
27
|
+
const isTurboModel = modelId.includes('turbo') || modelId.includes('flash');
|
|
28
|
+
const isMaxModel = modelId.includes('max');
|
|
29
|
+
const isPlusModel = modelId.includes('plus');
|
|
30
|
+
const isDeepResearch = modelId.includes('deep-research');
|
|
31
|
+
const isWebDev = modelId.includes('web-dev');
|
|
32
|
+
const isFullStack = modelId.includes('full-stack');
|
|
33
|
+
const isOmni = modelId.includes('omni');
|
|
34
|
+
// Determine capabilities
|
|
35
|
+
const attachment = isVisionModel;
|
|
36
|
+
const reasoning = modelId.includes('thinking') || modelId.includes('max') || modelId.includes('deep-research');
|
|
37
|
+
const tool_call = isCoderModel || modelId.includes('tools') || modelId.includes('plus') || modelId.includes('max');
|
|
38
|
+
// Generate name
|
|
39
|
+
let name = modelId.replace(/-/g, ' ').replace(/\b\w/g, l => l.toUpperCase());
|
|
40
|
+
return {
|
|
41
|
+
id: modelId,
|
|
42
|
+
name,
|
|
43
|
+
attachment,
|
|
44
|
+
reasoning,
|
|
45
|
+
tool_call,
|
|
46
|
+
temperature: true
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Fetch models from Qwen API
|
|
51
|
+
*/
|
|
52
|
+
async function fetchQwenModels(apiKey, baseURL = QWEN_BASE_URL) {
|
|
53
|
+
const now = Date.now();
|
|
54
|
+
// Return cached models if still valid
|
|
55
|
+
if (modelCache && (now - modelCacheTime) < CACHE_DURATION) {
|
|
56
|
+
return modelCache;
|
|
57
|
+
}
|
|
58
|
+
try {
|
|
59
|
+
const response = await fetch(`${baseURL}/models`, {
|
|
60
|
+
method: 'GET',
|
|
61
|
+
headers: {
|
|
62
|
+
'Authorization': `Bearer ${apiKey}`,
|
|
63
|
+
'Content-Type': 'application/json',
|
|
64
|
+
'User-Agent': 'OpenCode-Qwen-Provider/1.0.5'
|
|
65
|
+
}
|
|
66
|
+
});
|
|
67
|
+
if (!response.ok) {
|
|
68
|
+
throw new Error(`Failed to fetch models: ${response.status} ${response.statusText}`);
|
|
69
|
+
}
|
|
70
|
+
const data = await response.json();
|
|
71
|
+
// Convert API models to OpenCode format
|
|
72
|
+
const models = {};
|
|
73
|
+
for (const apiModel of data.data) {
|
|
74
|
+
models[apiModel.id] = convertApiModelToModel(apiModel);
|
|
75
|
+
}
|
|
76
|
+
// Cache the results
|
|
77
|
+
modelCache = models;
|
|
78
|
+
modelCacheTime = now;
|
|
79
|
+
return models;
|
|
80
|
+
}
|
|
81
|
+
catch (error) {
|
|
82
|
+
console.error('Failed to fetch Qwen models:', error);
|
|
83
|
+
// Return fallback models if cache exists but is expired
|
|
84
|
+
if (modelCache) {
|
|
85
|
+
console.warn('Using cached models due to API error');
|
|
86
|
+
return modelCache;
|
|
87
|
+
}
|
|
88
|
+
// Return minimal fallback models
|
|
89
|
+
return {
|
|
90
|
+
'qwen-turbo': {
|
|
91
|
+
id: 'qwen-turbo',
|
|
92
|
+
name: 'Qwen Turbo',
|
|
93
|
+
attachment: false,
|
|
94
|
+
reasoning: false,
|
|
95
|
+
tool_call: false,
|
|
96
|
+
temperature: true
|
|
97
|
+
}
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Validate Qwen API key
|
|
103
|
+
*/
|
|
104
|
+
async function validateQwenApiKey(apiKey) {
|
|
105
|
+
try {
|
|
106
|
+
const response = await fetch(`${QWEN_BASE_URL}/models`, {
|
|
107
|
+
method: 'GET',
|
|
108
|
+
headers: {
|
|
109
|
+
'Authorization': `Bearer ${apiKey}`,
|
|
110
|
+
'Content-Type': 'application/json'
|
|
111
|
+
}
|
|
112
|
+
});
|
|
113
|
+
return response.ok;
|
|
114
|
+
}
|
|
115
|
+
catch {
|
|
116
|
+
return false;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Qwen Plugin for OpenCode
|
|
121
|
+
*/
|
|
122
|
+
async function QwenPlugin({ project, client, $, directory, worktree }) {
|
|
123
|
+
const apiKey = process.env.QWEN_API_KEY;
|
|
124
|
+
const baseURL = process.env.QWEN_BASE_URL || QWEN_BASE_URL;
|
|
125
|
+
const LOG_FILE = '/tmp/opencode_qwen_plugin.log';
|
|
126
|
+
const log = (level, message) => {
|
|
127
|
+
try {
|
|
128
|
+
const timestamp = new Date().toISOString();
|
|
129
|
+
const logMsg = `[${timestamp}] [${level.toUpperCase()}] ${message}\n`;
|
|
130
|
+
const fs = require('fs');
|
|
131
|
+
fs.appendFileSync(LOG_FILE, logMsg);
|
|
132
|
+
}
|
|
133
|
+
catch (e) {
|
|
134
|
+
// Silent fail for logging
|
|
135
|
+
}
|
|
136
|
+
};
|
|
137
|
+
return {
|
|
138
|
+
async config(config) {
|
|
139
|
+
log('info', 'Qwen plugin initializing...');
|
|
140
|
+
if (!config.provider) {
|
|
141
|
+
config.provider = {};
|
|
142
|
+
}
|
|
143
|
+
let models = {};
|
|
144
|
+
if (apiKey) {
|
|
145
|
+
try {
|
|
146
|
+
const isValid = await validateQwenApiKey(apiKey);
|
|
147
|
+
if (isValid) {
|
|
148
|
+
models = await fetchQwenModels(apiKey, baseURL);
|
|
149
|
+
log('info', `Loaded ${Object.keys(models).length} models`);
|
|
150
|
+
}
|
|
151
|
+
else {
|
|
152
|
+
log('warn', 'Invalid API key, using fallback models');
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
catch (error) {
|
|
156
|
+
log('error', `Failed to fetch models: ${error instanceof Error ? error.message : String(error)}`);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
else {
|
|
160
|
+
log('warn', 'No QWEN_API_KEY found, using fallback models');
|
|
161
|
+
}
|
|
162
|
+
config.provider['qwen'] = {
|
|
163
|
+
api: QWEN_BASE_URL,
|
|
164
|
+
id: 'qwen',
|
|
165
|
+
name: 'Qwen',
|
|
166
|
+
env: ['QWEN_API_KEY'],
|
|
167
|
+
models
|
|
168
|
+
};
|
|
169
|
+
log('info', 'Qwen provider registered');
|
|
170
|
+
}
|
|
171
|
+
};
|
|
172
|
+
}
|
package/package.json
CHANGED
|
@@ -1,23 +1,29 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "opencode-qwen",
|
|
3
|
-
"version": "1.0.
|
|
4
|
-
"description": "Qwen provider for OpenCode with
|
|
5
|
-
"main": "index.
|
|
3
|
+
"version": "1.0.5",
|
|
4
|
+
"description": "Qwen provider for OpenCode with dynamic model loading",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
6
7
|
"exports": {
|
|
7
|
-
".":
|
|
8
|
+
".": {
|
|
9
|
+
"types": "./dist/index.d.ts",
|
|
10
|
+
"default": "./dist/index.js"
|
|
11
|
+
}
|
|
8
12
|
},
|
|
9
|
-
"
|
|
10
|
-
"
|
|
11
|
-
"
|
|
12
|
-
"
|
|
13
|
+
"scripts": {
|
|
14
|
+
"build": "tsc",
|
|
15
|
+
"prepare": "npm run build",
|
|
16
|
+
"publish": "npm publish --access=public"
|
|
13
17
|
},
|
|
18
|
+
"files": [
|
|
19
|
+
"dist"
|
|
20
|
+
],
|
|
14
21
|
"devDependencies": {
|
|
15
|
-
"
|
|
16
|
-
"
|
|
22
|
+
"@types/node": "^20.19.30",
|
|
23
|
+
"typescript": "^5.0.0"
|
|
17
24
|
},
|
|
18
25
|
"keywords": [
|
|
19
26
|
"opencode",
|
|
20
|
-
"plugin",
|
|
21
27
|
"provider",
|
|
22
28
|
"qwen",
|
|
23
29
|
"ai"
|
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
# This workflow will run tests using node and then publish a package to GitHub Packages when a release is created
|
|
2
|
-
# For more information see: https://docs.github.com/en/actions/publishing-packages/publishing-nodejs-packages
|
|
3
|
-
|
|
4
|
-
name: Node.js Package
|
|
5
|
-
|
|
6
|
-
on:
|
|
7
|
-
workflow_dispatch:
|
|
8
|
-
|
|
9
|
-
jobs:
|
|
10
|
-
build:
|
|
11
|
-
runs-on: ubuntu-latest
|
|
12
|
-
steps:
|
|
13
|
-
- uses: actions/checkout@v4
|
|
14
|
-
- uses: actions/setup-node@v4
|
|
15
|
-
with:
|
|
16
|
-
node-version: 20
|
|
17
|
-
- run: npm install
|
|
18
|
-
|
|
19
|
-
publish-npm:
|
|
20
|
-
needs: build
|
|
21
|
-
runs-on: ubuntu-latest
|
|
22
|
-
steps:
|
|
23
|
-
- uses: actions/checkout@v4
|
|
24
|
-
- uses: actions/setup-node@v4
|
|
25
|
-
with:
|
|
26
|
-
node-version: 20
|
|
27
|
-
registry-url: https://registry.npmjs.org/
|
|
28
|
-
- run: npm publish
|
|
29
|
-
env:
|
|
30
|
-
NODE_AUTH_TOKEN: ${{secrets.npm_token}}
|
package/auth.ts
DELETED
|
@@ -1,165 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Qwen Provider Authentication Handler for OpenCode
|
|
3
|
-
*
|
|
4
|
-
* This module handles authentication integration with OpenCode's /connect command
|
|
5
|
-
* and provides secure credential management.
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
import { z } from 'zod'
|
|
9
|
-
import type { AuthHandler, Credential } from '@opencode-ai/plugin'
|
|
10
|
-
import { validateQwenApiKey, createQwenProvider, fetchQwenModels } from './index'
|
|
11
|
-
|
|
12
|
-
// Credential validation schema
|
|
13
|
-
const QwenCredentialSchema = z.object({
|
|
14
|
-
apiKey: z.string().min(1, 'API key is required'),
|
|
15
|
-
name: z.string().optional()
|
|
16
|
-
})
|
|
17
|
-
|
|
18
|
-
export type QwenCredential = z.infer<typeof QwenCredentialSchema>
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* Qwen authentication handler
|
|
22
|
-
*/
|
|
23
|
-
export const QwenAuthHandler: AuthHandler = {
|
|
24
|
-
// Provider metadata
|
|
25
|
-
provider: {
|
|
26
|
-
id: 'qwen',
|
|
27
|
-
name: 'Qwen',
|
|
28
|
-
description: 'Qwen AI models - high-quality large language models for coding, reasoning, and creative tasks',
|
|
29
|
-
website: 'https://qwen.aikit.club',
|
|
30
|
-
icon: '🤖',
|
|
31
|
-
category: 'ai'
|
|
32
|
-
},
|
|
33
|
-
|
|
34
|
-
// Credential validation
|
|
35
|
-
validateCredential: async (credential: Credential): Promise<boolean> => {
|
|
36
|
-
try {
|
|
37
|
-
const qwenCred = QwenCredentialSchema.parse(credential)
|
|
38
|
-
return await validateQwenApiKey(qwenCred.apiKey)
|
|
39
|
-
} catch (error) {
|
|
40
|
-
console.error('Credential validation error:', error)
|
|
41
|
-
return false
|
|
42
|
-
}
|
|
43
|
-
},
|
|
44
|
-
|
|
45
|
-
// Provider creation
|
|
46
|
-
createProvider: async (credential: Credential) => {
|
|
47
|
-
try {
|
|
48
|
-
const qwenCred = QwenCredentialSchema.parse(credential)
|
|
49
|
-
const baseURL = 'https://qwen.aikit.club/v1'
|
|
50
|
-
|
|
51
|
-
// Validate and fetch models
|
|
52
|
-
const isValid = await validateQwenApiKey(qwenCred.apiKey)
|
|
53
|
-
if (!isValid) {
|
|
54
|
-
throw new Error('Invalid Qwen API key')
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
const models = await fetchQwenModels(qwenCred.apiKey, baseURL)
|
|
58
|
-
|
|
59
|
-
return createQwenProvider({
|
|
60
|
-
apiKey: qwenCred.apiKey,
|
|
61
|
-
baseURL
|
|
62
|
-
})
|
|
63
|
-
} catch (error) {
|
|
64
|
-
throw new Error(`Failed to create Qwen provider: ${error instanceof Error ? error.message : String(error)}`)
|
|
65
|
-
}
|
|
66
|
-
},
|
|
67
|
-
|
|
68
|
-
// Credential templates for user guidance
|
|
69
|
-
credentialTemplates: [
|
|
70
|
-
{
|
|
71
|
-
name: 'API Key',
|
|
72
|
-
fields: [
|
|
73
|
-
{
|
|
74
|
-
key: 'apiKey',
|
|
75
|
-
label: 'API Key',
|
|
76
|
-
type: 'password',
|
|
77
|
-
placeholder: 'sk-...',
|
|
78
|
-
required: true,
|
|
79
|
-
description: 'Your Qwen API key from https://qwen.aikit.club'
|
|
80
|
-
}
|
|
81
|
-
]
|
|
82
|
-
}
|
|
83
|
-
],
|
|
84
|
-
|
|
85
|
-
// Additional configuration options
|
|
86
|
-
configOptions: {
|
|
87
|
-
autoRefresh: {
|
|
88
|
-
type: 'boolean',
|
|
89
|
-
default: true,
|
|
90
|
-
description: 'Automatically refresh tokens when they expire'
|
|
91
|
-
},
|
|
92
|
-
timeout: {
|
|
93
|
-
type: 'number',
|
|
94
|
-
default: 30000,
|
|
95
|
-
description: 'Request timeout in milliseconds'
|
|
96
|
-
}
|
|
97
|
-
},
|
|
98
|
-
|
|
99
|
-
// Help and documentation
|
|
100
|
-
help: {
|
|
101
|
-
title: 'Qwen AI Integration',
|
|
102
|
-
description: `
|
|
103
|
-
Qwen provides state-of-the-art language models with dynamic model loading:
|
|
104
|
-
|
|
105
|
-
• **Code Generation**: Qwen Coder, Qwen3-Coder for development tasks
|
|
106
|
-
• **Complex Reasoning**: Qwen Max, Qwen-Deep-Research for advanced problem-solving
|
|
107
|
-
• **Speed & Efficiency**: Qwen Turbo, Qwen-Flash for quick responses
|
|
108
|
-
• **Multimodal**: Qwen VL, Qwen3-Omni for image and text understanding
|
|
109
|
-
• **Web Development**: Qwen-Web-Dev for frontend development
|
|
110
|
-
• **Full-Stack**: Qwen-Full-Stack for complete application development
|
|
111
|
-
• **Video Generation**: Text-to-video capabilities
|
|
112
|
-
• **Image Generation**: Advanced image creation and editing
|
|
113
|
-
|
|
114
|
-
**Getting Started:**
|
|
115
|
-
|
|
116
|
-
1. Visit [chat.qwen.ai](https://chat.qwen.ai) and log in
|
|
117
|
-
2. Extract your access token using the browser console script from [qwen-api repo](https://github.com/tanu1337/qwen-api)
|
|
118
|
-
3. Use \`/connect\` command and select "Qwen"
|
|
119
|
-
4. Paste your access token when prompted
|
|
120
|
-
5. Models will be loaded dynamically based on your account access
|
|
121
|
-
|
|
122
|
-
**Dynamic Model Loading:**
|
|
123
|
-
Models are fetched directly from the API, ensuring you always have access to the latest available models. Common models include:
|
|
124
|
-
- \`qwen-turbo\`: Fast, efficient for general tasks
|
|
125
|
-
- \`qwen-plus\`: Balanced performance for complex tasks
|
|
126
|
-
- \`qwen-max\`: Most capable for advanced reasoning
|
|
127
|
-
- \`qwen-coder\`: Specialized for coding and development
|
|
128
|
-
- \`qwen-vl\`: Multimodal, supports images and text
|
|
129
|
-
- \`qwen-deep-research\`: Comprehensive research with web search
|
|
130
|
-
- \`qwen-web-dev\`: Frontend web development
|
|
131
|
-
- \`qwen-full-stack\`: Complete application development
|
|
132
|
-
|
|
133
|
-
**Getting Your Token:**
|
|
134
|
-
1. Go to [chat.qwen.ai](https://chat.qwen.ai) and login
|
|
135
|
-
2. Open browser console (F12 → Console tab)
|
|
136
|
-
3. Paste this JavaScript code:
|
|
137
|
-
\`\`\`javascript
|
|
138
|
-
(function(){if(window.location.hostname!=="chat.qwen.ai"){alert("🚀 This code is for chat.qwen.ai");window.open("https://chat.qwen.ai","_blank");return;}
|
|
139
|
-
function getApiKeyData(){const token=localStorage.getItem("token");if(!token){alert("❌ qwen access_token not found !!!");return null;}
|
|
140
|
-
return token;}
|
|
141
|
-
async function copyToClipboard(text){try{await navigator.clipboard.writeText(text);return true;}catch(err){console.error("❌ Failed to copy to clipboard:",err);const textarea=document.createElement("textarea");textarea.value=text;textarea.style.position="fixed";textarea.style.opacity="0";document.body.appendChild(textarea);textarea.focus();textarea.select();const success=document.execCommand("copy");document.body.removeChild(textarea);return success;}}
|
|
142
|
-
const apiKeyData=getApiKeyData();if(!apiKeyData)return;copyToClipboard(apiKeyData).then((success)=>{if(success){alert("🔑 Qwen access_token copied to clipboard !!! 🎉");}else{prompt("🔰 Qwen access_token:",apiKeyData);}});})();
|
|
143
|
-
\`\`\`
|
|
144
|
-
|
|
145
|
-
For more information, visit the [Qwen API repository](https://github.com/tanu1337/qwen-api).
|
|
146
|
-
`.trim(),
|
|
147
|
-
links: [
|
|
148
|
-
{
|
|
149
|
-
label: 'Get API Key',
|
|
150
|
-
url: 'https://qwen.aikit.club/api-keys'
|
|
151
|
-
},
|
|
152
|
-
{
|
|
153
|
-
label: 'Documentation',
|
|
154
|
-
url: 'https://qwen.aikit.club/docs'
|
|
155
|
-
},
|
|
156
|
-
{
|
|
157
|
-
label: 'Pricing',
|
|
158
|
-
url: 'https://qwen.aikit.club/pricing'
|
|
159
|
-
}
|
|
160
|
-
]
|
|
161
|
-
}
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
// Export for direct usage
|
|
165
|
-
export { QwenCredentialSchema, type QwenCredential }
|
package/fix_summary.md
DELETED
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
## Qwen Plugin Import Fix Summary
|
|
2
|
-
|
|
3
|
-
**Problem Identified:**
|
|
4
|
-
The plugin was failing to load because it was trying to import `createOpenAI` from the non-existent package `@ai-sdk/openai-compatible`, but this function actually exists in `@ai-sdk/openai`.
|
|
5
|
-
|
|
6
|
-
**Root Cause:**
|
|
7
|
-
- Package `@ai-sdk/openai-compatible` doesn't exist in the OpenCode environment
|
|
8
|
-
- Function `createOpenAI` is available in `@ai-sdk/openai` package
|
|
9
|
-
- Multiple files had incorrect import statements
|
|
10
|
-
|
|
11
|
-
**Fixes Applied:**
|
|
12
|
-
1. Fixed import in `/root/workspace/opencode-qwen/index.ts`:
|
|
13
|
-
- Changed: `import { createOpenAI } from '@ai-sdk/openai-compatible'`
|
|
14
|
-
- To: `import { createOpenAI } from '@ai-sdk/openai'`
|
|
15
|
-
|
|
16
|
-
2. Fixed dependency in `/root/workspace/opencode-qwen/package.json`:
|
|
17
|
-
- Changed: `"@ai-sdk/openai-compatible": "^1.0.0"`
|
|
18
|
-
- To: `"@ai-sdk/openai": "^1.0.0"` (will need version update)
|
|
19
|
-
|
|
20
|
-
**Next Steps:**
|
|
21
|
-
- Update package.json dependency version to match available @ai-sdk/openai version
|
|
22
|
-
- Restart OpenCode to test the fix
|
|
23
|
-
- Monitor logs to ensure plugin loads successfully
|
|
24
|
-
|
|
25
|
-
**Technical Details:**
|
|
26
|
-
- Available @ai-sdk/openai version: 3.0.23
|
|
27
|
-
- The createOpenAI function is confirmed to exist in @ai-sdk/openai/dist/index.mjs
|
|
28
|
-
- Plugin should now be able to load without import errors
|
package/index.ts
DELETED
|
@@ -1,223 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Qwen Provider for OpenCode
|
|
3
|
-
*
|
|
4
|
-
* This package provides Qwen API integration for OpenCode following the provider specification.
|
|
5
|
-
* It supports both direct API key usage and token refresh capabilities.
|
|
6
|
-
* Models are fetched dynamically from the API.
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
import { createOpenAI } from '@ai-sdk/openai'
|
|
10
|
-
import type { Provider, Model } from '@ai-sdk/provider'
|
|
11
|
-
import { z } from 'zod'
|
|
12
|
-
|
|
13
|
-
// Qwen API base URL
|
|
14
|
-
const QWEN_BASE_URL = 'https://qwen.aikit.club/v1'
|
|
15
|
-
|
|
16
|
-
// Qwen model interface from API response
|
|
17
|
-
export interface QwenApiModel {
|
|
18
|
-
id: string
|
|
19
|
-
object: string
|
|
20
|
-
created: number
|
|
21
|
-
owned_by: string
|
|
22
|
-
permission?: any[]
|
|
23
|
-
root?: string
|
|
24
|
-
parent?: string
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
export interface QwenModelsResponse {
|
|
28
|
-
object: string
|
|
29
|
-
data: QwenApiModel[]
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
// Model cache to avoid repeated API calls
|
|
33
|
-
let modelCache: Record<string, Model> | null = null
|
|
34
|
-
let modelCacheTime = 0
|
|
35
|
-
const CACHE_DURATION = 5 * 60 * 1000 // 5 minutes
|
|
36
|
-
|
|
37
|
-
// Configuration schema for Qwen provider
|
|
38
|
-
const QwenConfigSchema = z.object({
|
|
39
|
-
apiKey: z.string().min(1, 'API key is required'),
|
|
40
|
-
baseURL: z.string().default(QWEN_BASE_URL),
|
|
41
|
-
refreshToken: z.string().optional(),
|
|
42
|
-
autoRefresh: z.boolean().default(true),
|
|
43
|
-
timeout: z.number().default(30000)
|
|
44
|
-
})
|
|
45
|
-
|
|
46
|
-
export type QwenConfig = z.infer<typeof QwenConfigSchema>
|
|
47
|
-
|
|
48
|
-
/**
|
|
49
|
-
* Convert Qwen API model to OpenCode Model format
|
|
50
|
-
*/
|
|
51
|
-
function convertApiModelToModel(apiModel: QwenApiModel): Model {
|
|
52
|
-
const modelId = apiModel.id
|
|
53
|
-
|
|
54
|
-
// Extract capabilities from model name
|
|
55
|
-
const isVisionModel = modelId.includes('vl') || modelId.includes('vision') || modelId.includes('qvq')
|
|
56
|
-
const isCoderModel = modelId.includes('coder') || modelId.includes('code')
|
|
57
|
-
const isTurboModel = modelId.includes('turbo') || modelId.includes('flash')
|
|
58
|
-
const isMaxModel = modelId.includes('max')
|
|
59
|
-
const isPlusModel = modelId.includes('plus')
|
|
60
|
-
const isDeepResearch = modelId.includes('deep-research')
|
|
61
|
-
const isWebDev = modelId.includes('web-dev')
|
|
62
|
-
const isFullStack = modelId.includes('full-stack')
|
|
63
|
-
const isOmni = modelId.includes('omni')
|
|
64
|
-
|
|
65
|
-
// Determine supported capabilities
|
|
66
|
-
const supports: string[] = ['text']
|
|
67
|
-
if (isVisionModel) supports.push('image')
|
|
68
|
-
if (modelId.includes('search') || modelId.includes('plus') || modelId.includes('max')) supports.push('web_search')
|
|
69
|
-
if (isCoderModel || modelId.includes('tools') || modelId.includes('plus') || modelId.includes('max')) supports.push('tools')
|
|
70
|
-
if (modelId.includes('thinking') || modelId.includes('max')) supports.push('thinking')
|
|
71
|
-
|
|
72
|
-
// Determine max tokens based on model
|
|
73
|
-
let maxTokens = 8192
|
|
74
|
-
if (modelId.includes('32b') || modelId.includes('80b') || modelId.includes('235b') ||
|
|
75
|
-
modelId.includes('max') || modelId.includes('plus') || isCoderModel) {
|
|
76
|
-
maxTokens = 32768
|
|
77
|
-
} else if (modelId.includes('14b')) {
|
|
78
|
-
maxTokens = 8192
|
|
79
|
-
} else if (modelId.includes('72b')) {
|
|
80
|
-
maxTokens = 32768
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
// Generate description
|
|
84
|
-
let description = ''
|
|
85
|
-
if (isTurboModel) description = 'Fast and efficient model for general tasks'
|
|
86
|
-
else if (isMaxModel) description = 'Most capable model for advanced tasks'
|
|
87
|
-
else if (isPlusModel) description = 'Balanced model for complex reasoning'
|
|
88
|
-
else if (isCoderModel) description = 'Specialized model for coding and development'
|
|
89
|
-
else if (isDeepResearch) description = 'Research model with comprehensive analysis capabilities'
|
|
90
|
-
else if (isWebDev) description = 'Specialized for web development and UI generation'
|
|
91
|
-
else if (isFullStack) description = 'Full-stack application development model'
|
|
92
|
-
else if (isVisionModel) description = 'Multimodal model supporting text and image analysis'
|
|
93
|
-
else description = 'Qwen language model'
|
|
94
|
-
|
|
95
|
-
return {
|
|
96
|
-
name: apiModel.id.replace(/-/g, ' ').replace(/\b\w/g, l => l.toUpperCase()),
|
|
97
|
-
provider: 'Qwen',
|
|
98
|
-
maxTokens,
|
|
99
|
-
supports,
|
|
100
|
-
description
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
/**
|
|
105
|
-
* Fetch models from Qwen API
|
|
106
|
-
*/
|
|
107
|
-
export async function fetchQwenModels(apiKey: string, baseURL: string = QWEN_BASE_URL): Promise<Record<string, Model>> {
|
|
108
|
-
const now = Date.now()
|
|
109
|
-
|
|
110
|
-
// Return cached models if still valid
|
|
111
|
-
if (modelCache && (now - modelCacheTime) < CACHE_DURATION) {
|
|
112
|
-
return modelCache
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
try {
|
|
116
|
-
const response = await fetch(`${baseURL}/models`, {
|
|
117
|
-
method: 'GET',
|
|
118
|
-
headers: {
|
|
119
|
-
'Authorization': `Bearer ${apiKey}`,
|
|
120
|
-
'Content-Type': 'application/json',
|
|
121
|
-
'User-Agent': 'OpenCode-Qwen-Provider/1.0.0'
|
|
122
|
-
}
|
|
123
|
-
})
|
|
124
|
-
|
|
125
|
-
if (!response.ok) {
|
|
126
|
-
throw new Error(`Failed to fetch models: ${response.status} ${response.statusText}`)
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
const data: QwenModelsResponse = await response.json()
|
|
130
|
-
|
|
131
|
-
// Convert API models to OpenCode format
|
|
132
|
-
const models: Record<string, Model> = {}
|
|
133
|
-
for (const apiModel of data.data) {
|
|
134
|
-
models[apiModel.id] = convertApiModelToModel(apiModel)
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
// Cache the results
|
|
138
|
-
modelCache = models
|
|
139
|
-
modelCacheTime = now
|
|
140
|
-
|
|
141
|
-
return models
|
|
142
|
-
} catch (error) {
|
|
143
|
-
console.error('Failed to fetch Qwen models:', error)
|
|
144
|
-
|
|
145
|
-
// Return fallback models if cache exists but is expired
|
|
146
|
-
if (modelCache) {
|
|
147
|
-
console.warn('Using cached models due to API error')
|
|
148
|
-
return modelCache
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
// Return minimal fallback models
|
|
152
|
-
return {
|
|
153
|
-
'qwen-turbo': {
|
|
154
|
-
name: 'Qwen Turbo',
|
|
155
|
-
provider: 'Qwen',
|
|
156
|
-
maxTokens: 8192,
|
|
157
|
-
supports: ['text'],
|
|
158
|
-
description: 'Fast and efficient model for general tasks'
|
|
159
|
-
}
|
|
160
|
-
}
|
|
161
|
-
}
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
/**
|
|
165
|
-
* Create Qwen provider instance
|
|
166
|
-
*/
|
|
167
|
-
export function createQwenProvider(config: QwenConfig): Provider {
|
|
168
|
-
const validatedConfig = QwenConfigSchema.parse(config)
|
|
169
|
-
|
|
170
|
-
return createOpenAI({
|
|
171
|
-
name: 'Qwen',
|
|
172
|
-
baseURL: validatedConfig.baseURL,
|
|
173
|
-
apiKey: validatedConfig.apiKey,
|
|
174
|
-
headers: {
|
|
175
|
-
'User-Agent': 'OpenCode-Qwen-Provider/1.0.0'
|
|
176
|
-
},
|
|
177
|
-
compatibility: 'compatible'
|
|
178
|
-
})
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
/**
|
|
182
|
-
* Validate Qwen API key
|
|
183
|
-
*/
|
|
184
|
-
export async function validateQwenApiKey(apiKey: string): Promise<boolean> {
|
|
185
|
-
try {
|
|
186
|
-
const response = await fetch(`${QWEN_BASE_URL}/models`, {
|
|
187
|
-
method: 'GET',
|
|
188
|
-
headers: {
|
|
189
|
-
'Authorization': `Bearer ${apiKey}`,
|
|
190
|
-
'Content-Type': 'application/json'
|
|
191
|
-
}
|
|
192
|
-
})
|
|
193
|
-
|
|
194
|
-
return response.ok
|
|
195
|
-
} catch {
|
|
196
|
-
return false
|
|
197
|
-
}
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
/**
|
|
201
|
-
* Get available Qwen models (requires API key for dynamic loading)
|
|
202
|
-
*/
|
|
203
|
-
export async function getQwenModels(apiKey: string, baseURL?: string): Promise<Record<string, Model>> {
|
|
204
|
-
return await fetchQwenModels(apiKey, baseURL)
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
/**
|
|
208
|
-
* Provider factory for OpenCode integration
|
|
209
|
-
*/
|
|
210
|
-
export default function qwenProvider() {
|
|
211
|
-
return {
|
|
212
|
-
id: 'qwen',
|
|
213
|
-
name: 'Qwen',
|
|
214
|
-
description: 'Qwen AI models - high-quality large language models with dynamic model loading',
|
|
215
|
-
website: 'https://qwen.aikit.club',
|
|
216
|
-
create: createQwenProvider,
|
|
217
|
-
validate: validateQwenApiKey,
|
|
218
|
-
getModels: fetchQwenModels
|
|
219
|
-
}
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
// Export types for TypeScript users
|
|
223
|
-
export type { QwenConfig }
|
package/opencode.example.json
DELETED
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
# Example OpenCode configuration with Qwen auth plugin
|
|
2
|
-
|
|
3
|
-
{
|
|
4
|
-
"$schema": "https://opencode.ai/config.json",
|
|
5
|
-
"plugins": [
|
|
6
|
-
"./plugins/qwen-auth.ts"
|
|
7
|
-
],
|
|
8
|
-
"tools": {
|
|
9
|
-
"qwen": {
|
|
10
|
-
"baseUrl": "https://qwen.aikit.club/v1",
|
|
11
|
-
"apiKey": "${QWEN_API_KEY}"
|
|
12
|
-
}
|
|
13
|
-
},
|
|
14
|
-
"env": {
|
|
15
|
-
"QWEN_API_KEY": {
|
|
16
|
-
"description": "Qwen API access token",
|
|
17
|
-
"required": true
|
|
18
|
-
},
|
|
19
|
-
"QWEN_REFRESH_TOKEN": {
|
|
20
|
-
"description": "Qwen API refresh token for automatic renewal",
|
|
21
|
-
"required": false
|
|
22
|
-
},
|
|
23
|
-
"QWEN_AUTO_REFRESH": {
|
|
24
|
-
"description": "Enable automatic token refresh",
|
|
25
|
-
"default": "true",
|
|
26
|
-
"type": "boolean"
|
|
27
|
-
},
|
|
28
|
-
"QWEN_VALIDATE_ON_STARTUP": {
|
|
29
|
-
"description": "Validate token on session startup",
|
|
30
|
-
"default": "true",
|
|
31
|
-
"type": "boolean"
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
}
|
package/qwen-auth.ts
DELETED
|
@@ -1,337 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Qwen Provider Authentication Plugin for OpenCode
|
|
3
|
-
*
|
|
4
|
-
* This plugin handles authentication for the Qwen API provider,
|
|
5
|
-
* including token validation, refresh, and secure credential management.
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
import type { Plugin, tool } from "@opencode-ai/plugin"
|
|
9
|
-
import { z } from "zod"
|
|
10
|
-
|
|
11
|
-
// Qwen API base URL
|
|
12
|
-
const QWEN_API_BASE = "https://qwen.aikit.club/v1"
|
|
13
|
-
|
|
14
|
-
// TypeScript types for Qwen authentication
|
|
15
|
-
export interface QwenAuthState {
|
|
16
|
-
accessToken?: string
|
|
17
|
-
refreshToken?: string
|
|
18
|
-
expiresAt?: number
|
|
19
|
-
isValid: boolean
|
|
20
|
-
lastValidated?: number
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
export interface QwenTokenResponse {
|
|
24
|
-
access_token: string
|
|
25
|
-
refresh_token?: string
|
|
26
|
-
expires_in: number
|
|
27
|
-
token_type: string
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
export interface QwenModel {
|
|
31
|
-
id: string
|
|
32
|
-
object: string
|
|
33
|
-
created: number
|
|
34
|
-
owned_by: string
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
export interface QwenModelsResponse {
|
|
38
|
-
object: string
|
|
39
|
-
data: QwenModel[]
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
// Zod schemas for validation
|
|
43
|
-
const QwenAuthConfigSchema = z.object({
|
|
44
|
-
apiKey: z.string().min(1, "API key is required"),
|
|
45
|
-
refreshToken: z.string().optional(),
|
|
46
|
-
autoRefresh: z.boolean().default(true),
|
|
47
|
-
validateOnStartup: z.boolean().default(true)
|
|
48
|
-
})
|
|
49
|
-
|
|
50
|
-
const ValidateTokenSchema = z.object({
|
|
51
|
-
token: z.string().optional()
|
|
52
|
-
})
|
|
53
|
-
|
|
54
|
-
const RefreshTokenSchema = z.object({
|
|
55
|
-
refreshToken: z.string().optional()
|
|
56
|
-
})
|
|
57
|
-
|
|
58
|
-
/**
|
|
59
|
-
* Qwen Authentication Plugin
|
|
60
|
-
*/
|
|
61
|
-
export const QwenAuthPlugin: Plugin = async ({ project, client, $, directory, worktree }) => {
|
|
62
|
-
let authState: QwenAuthState = {
|
|
63
|
-
isValid: false
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
/**
|
|
67
|
-
* Load authentication state from environment or secure storage
|
|
68
|
-
*/
|
|
69
|
-
const loadAuthState = async (): Promise<QwenAuthState> => {
|
|
70
|
-
try {
|
|
71
|
-
const config = QwenAuthConfigSchema.parse({
|
|
72
|
-
apiKey: process.env.QWEN_API_KEY,
|
|
73
|
-
refreshToken: process.env.QWEN_REFRESH_TOKEN,
|
|
74
|
-
autoRefresh: process.env.QWEN_AUTO_REFRESH !== 'false',
|
|
75
|
-
validateOnStartup: process.env.QWEN_VALIDATE_ON_STARTUP !== 'false'
|
|
76
|
-
})
|
|
77
|
-
|
|
78
|
-
authState = {
|
|
79
|
-
accessToken: config.apiKey,
|
|
80
|
-
refreshToken: config.refreshToken,
|
|
81
|
-
isValid: !!config.apiKey,
|
|
82
|
-
lastValidated: Date.now()
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
await client.app.log({
|
|
86
|
-
service: "qwen-auth",
|
|
87
|
-
level: "info",
|
|
88
|
-
message: "Auth state loaded",
|
|
89
|
-
extra: { hasToken: !!config.apiKey, hasRefreshToken: !!config.refreshToken }
|
|
90
|
-
})
|
|
91
|
-
|
|
92
|
-
return authState
|
|
93
|
-
} catch (error) {
|
|
94
|
-
await client.app.log({
|
|
95
|
-
service: "qwen-auth",
|
|
96
|
-
level: "error",
|
|
97
|
-
message: "Failed to load auth state",
|
|
98
|
-
extra: { error: error instanceof Error ? error.message : String(error) }
|
|
99
|
-
})
|
|
100
|
-
return { isValid: false }
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
/**
|
|
105
|
-
* Validate current access token
|
|
106
|
-
*/
|
|
107
|
-
const validateToken = async (token?: string): Promise<boolean> => {
|
|
108
|
-
try {
|
|
109
|
-
const tokenToValidate = token || authState.accessToken
|
|
110
|
-
if (!tokenToValidate) {
|
|
111
|
-
return false
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
const response = await fetch(`${QWEN_API_BASE}/validate`, {
|
|
115
|
-
method: "GET",
|
|
116
|
-
headers: {
|
|
117
|
-
"Authorization": `Bearer ${tokenToValidate}`,
|
|
118
|
-
"Content-Type": "application/json"
|
|
119
|
-
}
|
|
120
|
-
})
|
|
121
|
-
|
|
122
|
-
const isValid = response.ok
|
|
123
|
-
authState.isValid = isValid
|
|
124
|
-
authState.lastValidated = Date.now()
|
|
125
|
-
|
|
126
|
-
await client.app.log({
|
|
127
|
-
service: "qwen-auth",
|
|
128
|
-
level: isValid ? "info" : "warn",
|
|
129
|
-
message: `Token validation ${isValid ? 'successful' : 'failed'}`,
|
|
130
|
-
extra: { status: response.status }
|
|
131
|
-
})
|
|
132
|
-
|
|
133
|
-
return isValid
|
|
134
|
-
} catch (error) {
|
|
135
|
-
authState.isValid = false
|
|
136
|
-
await client.app.log({
|
|
137
|
-
service: "qwen-auth",
|
|
138
|
-
level: "error",
|
|
139
|
-
message: "Token validation error",
|
|
140
|
-
extra: { error: error instanceof Error ? error.message : String(error) }
|
|
141
|
-
})
|
|
142
|
-
return false
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
/**
|
|
147
|
-
* Refresh access token using refresh token
|
|
148
|
-
*/
|
|
149
|
-
const refreshToken = async (refreshTokenValue?: string): Promise<boolean> => {
|
|
150
|
-
try {
|
|
151
|
-
const tokenToUse = refreshTokenValue || authState.refreshToken
|
|
152
|
-
if (!tokenToUse) {
|
|
153
|
-
await client.app.log({
|
|
154
|
-
service: "qwen-auth",
|
|
155
|
-
level: "warn",
|
|
156
|
-
message: "No refresh token available"
|
|
157
|
-
})
|
|
158
|
-
return false
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
const response = await fetch(`${QWEN_API_BASE}/refresh`, {
|
|
162
|
-
method: "POST",
|
|
163
|
-
headers: {
|
|
164
|
-
"Content-Type": "application/json"
|
|
165
|
-
},
|
|
166
|
-
body: JSON.stringify({
|
|
167
|
-
refresh_token: tokenToUse
|
|
168
|
-
})
|
|
169
|
-
})
|
|
170
|
-
|
|
171
|
-
if (!response.ok) {
|
|
172
|
-
throw new Error(`Refresh failed: ${response.status}`)
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
const tokenData: QwenTokenResponse = await response.json()
|
|
176
|
-
|
|
177
|
-
authState.accessToken = tokenData.access_token
|
|
178
|
-
if (tokenData.refresh_token) {
|
|
179
|
-
authState.refreshToken = tokenData.refresh_token
|
|
180
|
-
}
|
|
181
|
-
authState.expiresAt = Date.now() + (tokenData.expires_in * 1000)
|
|
182
|
-
authState.isValid = true
|
|
183
|
-
authState.lastValidated = Date.now()
|
|
184
|
-
|
|
185
|
-
await client.app.log({
|
|
186
|
-
service: "qwen-auth",
|
|
187
|
-
level: "info",
|
|
188
|
-
message: "Token refreshed successfully",
|
|
189
|
-
extra: { expiresIn: tokenData.expires_in }
|
|
190
|
-
})
|
|
191
|
-
|
|
192
|
-
return true
|
|
193
|
-
} catch (error) {
|
|
194
|
-
await client.app.log({
|
|
195
|
-
service: "qwen-auth",
|
|
196
|
-
level: "error",
|
|
197
|
-
message: "Token refresh failed",
|
|
198
|
-
extra: { error: error instanceof Error ? error.message : String(error) }
|
|
199
|
-
})
|
|
200
|
-
return false
|
|
201
|
-
}
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
/**
|
|
205
|
-
* Auto-refresh token if needed
|
|
206
|
-
*/
|
|
207
|
-
const ensureValidToken = async (): Promise<boolean> => {
|
|
208
|
-
if (!authState.accessToken) {
|
|
209
|
-
await loadAuthState()
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
if (authState.isValid && authState.lastValidated &&
|
|
213
|
-
Date.now() - authState.lastValidated < 5 * 60 * 1000) {
|
|
214
|
-
return true // Valid within last 5 minutes
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
const isValid = await validateToken()
|
|
218
|
-
if (isValid) {
|
|
219
|
-
return true
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
if (authState.refreshToken) {
|
|
223
|
-
return await refreshToken()
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
return false
|
|
227
|
-
}
|
|
228
|
-
|
|
229
|
-
// Initialize plugin
|
|
230
|
-
await loadAuthState()
|
|
231
|
-
|
|
232
|
-
return {
|
|
233
|
-
// Custom tools for Qwen authentication
|
|
234
|
-
tool: {
|
|
235
|
-
"qwen.validate-token": tool({
|
|
236
|
-
description: "Validate Qwen API access token",
|
|
237
|
-
args: ValidateTokenSchema,
|
|
238
|
-
async execute(args, context) {
|
|
239
|
-
const isValid = await validateToken(args.token)
|
|
240
|
-
return {
|
|
241
|
-
valid: isValid,
|
|
242
|
-
timestamp: new Date().toISOString(),
|
|
243
|
-
state: authState
|
|
244
|
-
}
|
|
245
|
-
}
|
|
246
|
-
}),
|
|
247
|
-
|
|
248
|
-
"qwen.refresh-token": tool({
|
|
249
|
-
description: "Refresh Qwen API access token",
|
|
250
|
-
args: RefreshTokenSchema,
|
|
251
|
-
async execute(args, context) {
|
|
252
|
-
const success = await refreshToken(args.refreshToken)
|
|
253
|
-
return {
|
|
254
|
-
success,
|
|
255
|
-
timestamp: new Date().toISOString(),
|
|
256
|
-
state: authState
|
|
257
|
-
}
|
|
258
|
-
}
|
|
259
|
-
}),
|
|
260
|
-
|
|
261
|
-
"qwen.list-models": tool({
|
|
262
|
-
description: "List available Qwen models (requires valid authentication)",
|
|
263
|
-
args: z.object({}),
|
|
264
|
-
async execute(args, context) {
|
|
265
|
-
await ensureValidToken()
|
|
266
|
-
|
|
267
|
-
if (!authState.accessToken || !authState.isValid) {
|
|
268
|
-
throw new Error("No valid Qwen authentication token available")
|
|
269
|
-
}
|
|
270
|
-
|
|
271
|
-
try {
|
|
272
|
-
const response = await fetch(`${QWEN_API_BASE}/models`, {
|
|
273
|
-
headers: {
|
|
274
|
-
"Authorization": `Bearer ${authState.accessToken}`,
|
|
275
|
-
"Content-Type": "application/json"
|
|
276
|
-
}
|
|
277
|
-
})
|
|
278
|
-
|
|
279
|
-
if (!response.ok) {
|
|
280
|
-
throw new Error(`Failed to fetch models: ${response.status}`)
|
|
281
|
-
}
|
|
282
|
-
|
|
283
|
-
const models: QwenModelsResponse = await response.json()
|
|
284
|
-
|
|
285
|
-
await client.app.log({
|
|
286
|
-
service: "qwen-auth",
|
|
287
|
-
level: "info",
|
|
288
|
-
message: `Successfully retrieved ${models.data.length} models`
|
|
289
|
-
})
|
|
290
|
-
|
|
291
|
-
return models
|
|
292
|
-
} catch (error) {
|
|
293
|
-
await client.app.log({
|
|
294
|
-
service: "qwen-auth",
|
|
295
|
-
level: "error",
|
|
296
|
-
message: "Failed to fetch models",
|
|
297
|
-
extra: { error: error instanceof Error ? error.message : String(error) }
|
|
298
|
-
})
|
|
299
|
-
throw error
|
|
300
|
-
}
|
|
301
|
-
}
|
|
302
|
-
})
|
|
303
|
-
},
|
|
304
|
-
|
|
305
|
-
// Hooks for automatic token management
|
|
306
|
-
"tool.execute.before": async (input, output) => {
|
|
307
|
-
// Intercept Qwen API calls and ensure valid authentication
|
|
308
|
-
if (input.tool === "bash" && output.args.command?.includes("qwen.aikit.club")) {
|
|
309
|
-
await ensureValidToken()
|
|
310
|
-
if (authState.accessToken) {
|
|
311
|
-
// Inject token into command if needed
|
|
312
|
-
output.args.command = output.args.command.replace(
|
|
313
|
-
/qwen\.aikit\.club\/v1\//,
|
|
314
|
-
`qwen.aikit.club/v1/?api_key=${authState.accessToken}&`
|
|
315
|
-
)
|
|
316
|
-
}
|
|
317
|
-
}
|
|
318
|
-
},
|
|
319
|
-
|
|
320
|
-
"session.created": async () => {
|
|
321
|
-
// Validate token on session creation if enabled
|
|
322
|
-
if (process.env.QWEN_VALIDATE_ON_STARTUP !== 'false') {
|
|
323
|
-
await ensureValidToken()
|
|
324
|
-
}
|
|
325
|
-
},
|
|
326
|
-
|
|
327
|
-
"session.idle": async () => {
|
|
328
|
-
// Periodic validation when session is idle
|
|
329
|
-
if (authState.isValid && authState.lastValidated) {
|
|
330
|
-
const timeSinceValidation = Date.now() - authState.lastValidated
|
|
331
|
-
if (timeSinceValidation > 30 * 60 * 1000) { // 30 minutes
|
|
332
|
-
await validateToken()
|
|
333
|
-
}
|
|
334
|
-
}
|
|
335
|
-
}
|
|
336
|
-
}
|
|
337
|
-
}
|