Skip to content

Instantly share code, notes, and snippets.

@musistudio
Last active December 9, 2025 08:11
Show Gist options
  • Select an option

  • Save musistudio/f5a67841ced39912fd99e42200d5ca8b to your computer and use it in GitHub Desktop.

Select an option

Save musistudio/f5a67841ced39912fd99e42200d5ca8b to your computer and use it in GitHub Desktop.
qwen-cli.js
const os = require("os");
const path = require("path");
const fs = require("fs/promises");
const OAUTH_FILE = path.join(os.homedir(), ".qwen", "oauth_creds.json");
class QwenCLITransformer {
name = "qwen-cli";
async transformRequestIn(request, provider) {
if (!this.oauth_creds) {
await this.getOauthCreds();
}
if (this.oauth_creds && this.oauth_creds.expiry_date < +new Date()) {
await this.refreshToken(this.oauth_creds.refresh_token);
}
if (request.stream) {
request.stream_options = {
include_usage: true,
};
}
return {
body: request,
config: {
headers: {
Authorization: `Bearer ${this.oauth_creds.access_token}`,
"User-Agent": "QwenCode/v22.12.0 (darwin; arm64)",
},
},
};
}
refreshToken(refresh_token) {
const urlencoded = new URLSearchParams();
urlencoded.append("client_id", "f0304373b74a44d2b584a3fb70ca9e56");
urlencoded.append("refresh_token", refresh_token);
urlencoded.append("grant_type", "refresh_token");
return fetch("https://chat.qwen.ai/api/v1/oauth2/token", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: urlencoded,
})
.then((response) => response.json())
.then(async (data) => {
data.expiry_date =
new Date().getTime() + data.expires_in * 1000 - 1000 * 60;
data.refresh_token = refresh_token;
delete data.expires_in;
this.oauth_creds = data;
await fs.writeFile(OAUTH_FILE, JSON.stringify(data, null, 2));
});
}
async getOauthCreds() {
try {
const data = await fs.readFile(OAUTH_FILE);
this.oauth_creds = JSON.parse(data);
} catch (e) {}
}
}
module.exports = QwenCLITransformer;
@51HzOuO
Copy link

51HzOuO commented Aug 21, 2025

使用qwen-cli transformer时, todo无法完成,会一直error 并且无法读图片内容
(使用enhancetool 可以解决todo无法完成.. 但还是无法读图片)
希望后续可以支持(gemini-cli transformer 没有这两个问题)

@zyq-cn
Copy link

zyq-cn commented Aug 24, 2025

{
  "name": "qwen-cli",
  "api_base_url": "https://portal.qwen.ai/v1/chat/completions",
  "api_key": "any-string-is-ok",
  "models": [
    "qwen3-coder-plus"
  ],
  "transformer": {
    "use": ["qwen-cli"]
  }
},

我的config.json中的写法,之前我没写出正确的api_base_url导致一直出错

@dvelm
Copy link

dvelm commented Aug 25, 2025

https://portal.qwen.ai/v1/chat/completions

{
  "name": "qwen-cli",
  "api_base_url": "https://portal.qwen.ai/v1/chat/completions",
  "api_key": "any-string-is-ok",
  "models": [
    "qwen3-coder-plus"
  ],
  "transformer": {
    "use": ["qwen-cli"]
  }
},

我的config.json中的写法,之前我没写出正确的api_base_url导致一直出错

this was the configuration I was looking for. with api_base_url "https://portal.qwen.ai/v1" was not working. on roo code was working. why this api_base_url works for claude code with claude code router?

@admk
Copy link

admk commented Aug 28, 2025

refreshToken有点问题,无法refresh,这个改动似乎可以让它正确工作,不用总是隔一段时间启动qwen了:https://gist.github.com/admk/b927e85bc897cc64d8e27cbad40dac55/revisions#diff-845b885d933e5d2b9834e9a44d40ddb6d30ddbdf30bc4c8e195694a704d3a2c6

Never mind, still not working.

@oniharnantyo
Copy link

https://portal.qwen.ai/v1/chat/completions

{
  "name": "qwen-cli",
  "api_base_url": "https://portal.qwen.ai/v1/chat/completions",
  "api_key": "any-string-is-ok",
  "models": [
    "qwen3-coder-plus"
  ],
  "transformer": {
    "use": ["qwen-cli"]
  }
},

我的config.json中的写法,之前我没写出正确的api_base_url导致一直出错

this was the configuration I was looking for. with api_base_url "https://portal.qwen.ai/v1" was not working. on roo code was working. why this api_base_url works for claude code with claude code router?

it works after i update and reauthenticate through qwen-code

@mir-xiong
Copy link

要使用qwen.js 是不是要先安装qwen-code呀?

@tilak28
Copy link

tilak28 commented Oct 23, 2025

are their any working configs

@andromedarabbit
Copy link

not working at all

@oniharnantyo
Copy link

Still works on my device but sometime the token is expired and need to be refreshed using this steps:

  • Open qwen code and authenticate using OAUTH2
  • Close the qwen code then execute ccr restart
  • Continue using claude code that previously opened with ccr code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment