Skip to content

Instantly share code, notes, and snippets.

@musistudio
Last active October 23, 2025 10:10
Show Gist options
  • Save musistudio/f5a67841ced39912fd99e42200d5ca8b to your computer and use it in GitHub Desktop.
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;
@aptdnfapt
Copy link

hey my proxy is having many tool call errors in long context threshold . How do we set this transformer up tho ?

@musistudio
Copy link
Author

hey my proxy is having many tool call errors in long context threshold . How do we set this transformer up tho ?

Maybe you need to add enhancetool transformer

@aptdnfapt
Copy link

aptdnfapt commented Aug 13, 2025

hey my proxy is having many tool call errors in long context threshold . How do we set this transformer up tho ?

Maybe you need to add enhancetool transformer

im using enchatool transformer with my proxy and it works well but in large thresholds like 100k or so it makes quite a lot of erros . i saw this on qwen-code too but not quite much as . btw have you seen the https://github.com/synthetic-lab/octofriend ? they made models to fix just json and tool calls . maybe if you can integreat this on your router it might fix everything .

@orangle
Copy link

orangle commented Aug 18, 2025

如果长时间不用 qwen 的时候,记得登录下 qwen,然后重启下 ccr

@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

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