Spaces:
Sleeping
Sleeping
Update plugins/qwen.js
Browse files- plugins/qwen.js +77 -28
plugins/qwen.js
CHANGED
|
@@ -5,7 +5,6 @@ class QwenChatScraper {
|
|
| 5 |
this.baseURL = 'https://chat.qwen.ai/api';
|
| 6 |
this.userId = userId;
|
| 7 |
this.cookies = cookies;
|
| 8 |
-
this.chatId = null;
|
| 9 |
|
| 10 |
this.client = axios.create({
|
| 11 |
baseURL: this.baseURL,
|
|
@@ -20,6 +19,60 @@ class QwenChatScraper {
|
|
| 20 |
});
|
| 21 |
}
|
| 22 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
async createChat(title = 'New Chat', model = 'qwen3-max') {
|
| 24 |
const response = await this.client.post('/v2/chats/new', {
|
| 25 |
title,
|
|
@@ -28,7 +81,6 @@ class QwenChatScraper {
|
|
| 28 |
chat_type: 't2t',
|
| 29 |
timestamp: Date.now()
|
| 30 |
});
|
| 31 |
-
this.chatId = response.data.data.id;
|
| 32 |
return response.data.data;
|
| 33 |
}
|
| 34 |
|
|
@@ -149,10 +201,7 @@ class QwenChatScraper {
|
|
| 149 |
}
|
| 150 |
}
|
| 151 |
|
| 152 |
-
const
|
| 153 |
-
const parsed = JSON.parse(cookiesArray);
|
| 154 |
-
return parsed.map(cookie => `${cookie.name}=${cookie.value}`).join('; ');
|
| 155 |
-
};
|
| 156 |
|
| 157 |
const handler = async (req, res) => {
|
| 158 |
try {
|
|
@@ -163,7 +212,7 @@ const handler = async (req, res) => {
|
|
| 163 |
});
|
| 164 |
}
|
| 165 |
|
| 166 |
-
const { prompt, model = 'qwen3-max',
|
| 167 |
|
| 168 |
if (!prompt) {
|
| 169 |
return res.status(400).json({
|
|
@@ -172,42 +221,42 @@ const handler = async (req, res) => {
|
|
| 172 |
});
|
| 173 |
}
|
| 174 |
|
|
|
|
|
|
|
| 175 |
if (!cookies) {
|
| 176 |
-
return res.status(
|
| 177 |
success: false,
|
| 178 |
-
error: '
|
| 179 |
});
|
| 180 |
}
|
| 181 |
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
});
|
| 187 |
-
}
|
| 188 |
-
|
| 189 |
-
const cookieString = parseCookiesArray(cookies);
|
| 190 |
-
const scraper = new QwenChatScraper(null, cookieString);
|
| 191 |
-
|
| 192 |
-
if (!scraper.chatId) {
|
| 193 |
-
const newChat = await scraper.createChat('API Chat', model);
|
| 194 |
-
}
|
| 195 |
-
|
| 196 |
-
const result = await scraper.sendMessage(scraper.chatId, prompt, model);
|
| 197 |
|
| 198 |
res.json({
|
| 199 |
author: "Herza",
|
| 200 |
success: true,
|
| 201 |
-
chatId:
|
| 202 |
model: model,
|
| 203 |
response: result.content,
|
| 204 |
usage: result.usage
|
| 205 |
});
|
| 206 |
|
| 207 |
} catch (error) {
|
| 208 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 209 |
success: false,
|
| 210 |
-
error:
|
|
|
|
|
|
|
|
|
|
| 211 |
});
|
| 212 |
}
|
| 213 |
};
|
|
@@ -219,7 +268,7 @@ module.exports = {
|
|
| 219 |
routes: ['api/AI/qwen'],
|
| 220 |
tags: ['ai', 'qwen', 'chat'],
|
| 221 |
main: ['AI'],
|
| 222 |
-
parameters: ['prompt', 'model', '
|
| 223 |
enabled: true,
|
| 224 |
handler
|
| 225 |
};
|
|
|
|
| 5 |
this.baseURL = 'https://chat.qwen.ai/api';
|
| 6 |
this.userId = userId;
|
| 7 |
this.cookies = cookies;
|
|
|
|
| 8 |
|
| 9 |
this.client = axios.create({
|
| 10 |
baseURL: this.baseURL,
|
|
|
|
| 19 |
});
|
| 20 |
}
|
| 21 |
|
| 22 |
+
async login(email, password) {
|
| 23 |
+
try {
|
| 24 |
+
await this.client.get('/config');
|
| 25 |
+
|
| 26 |
+
const loginResponse = await this.client.post('/v1/login', {
|
| 27 |
+
email: email,
|
| 28 |
+
password: password
|
| 29 |
+
});
|
| 30 |
+
|
| 31 |
+
if (loginResponse.data && loginResponse.data.success) {
|
| 32 |
+
const setCookieHeaders = loginResponse.headers['set-cookie'] || [];
|
| 33 |
+
this.cookies = this.parseCookies(setCookieHeaders);
|
| 34 |
+
|
| 35 |
+
this.client.defaults.headers['Cookie'] = this.cookies;
|
| 36 |
+
|
| 37 |
+
const authResponse = await this.client.get('/v1/auths/');
|
| 38 |
+
|
| 39 |
+
if (authResponse.data && authResponse.data.id) {
|
| 40 |
+
this.userId = authResponse.data.id;
|
| 41 |
+
|
| 42 |
+
return {
|
| 43 |
+
success: true,
|
| 44 |
+
userId: this.userId,
|
| 45 |
+
email: authResponse.data.email,
|
| 46 |
+
name: authResponse.data.name,
|
| 47 |
+
cookies: this.cookies
|
| 48 |
+
};
|
| 49 |
+
}
|
| 50 |
+
}
|
| 51 |
+
|
| 52 |
+
return {
|
| 53 |
+
success: false,
|
| 54 |
+
message: 'Login failed: Invalid credentials'
|
| 55 |
+
};
|
| 56 |
+
|
| 57 |
+
} catch (error) {
|
| 58 |
+
return {
|
| 59 |
+
success: false,
|
| 60 |
+
message: `Login error: ${error.response?.data?.message || error.message}`
|
| 61 |
+
};
|
| 62 |
+
}
|
| 63 |
+
}
|
| 64 |
+
|
| 65 |
+
parseCookies(setCookieHeaders) {
|
| 66 |
+
const cookies = [];
|
| 67 |
+
|
| 68 |
+
for (const header of setCookieHeaders) {
|
| 69 |
+
const cookieParts = header.split(';')[0];
|
| 70 |
+
cookies.push(cookieParts);
|
| 71 |
+
}
|
| 72 |
+
|
| 73 |
+
return cookies.join('; ');
|
| 74 |
+
}
|
| 75 |
+
|
| 76 |
async createChat(title = 'New Chat', model = 'qwen3-max') {
|
| 77 |
const response = await this.client.post('/v2/chats/new', {
|
| 78 |
title,
|
|
|
|
| 81 |
chat_type: 't2t',
|
| 82 |
timestamp: Date.now()
|
| 83 |
});
|
|
|
|
| 84 |
return response.data.data;
|
| 85 |
}
|
| 86 |
|
|
|
|
| 201 |
}
|
| 202 |
}
|
| 203 |
|
| 204 |
+
const defaultCookies = '_c_WBKFRo=ggfUx8IeZNjRUHiHvEW8mvqCPJ5QqXCw3gxOxSEC; _gcl_au=1.1.671306628.1754633897.966854012.1761357423.1761359270; atpsida=d16698b082b415e3ce98462f_1761359275_3; aui=0033f46d-cdc9-44b9-aa02-a2f07588bdfd; cna=roQcIXSjlkcCAXIKh7pG5R+v; cnaui=0033f46d-cdc9-44b9-aa02-a2f07588bdfd; isg=BP39jyHRSeCe1e1luUJvtbfUDFv3mjHsF5tUqb9COtSD9hsohOqWveeypySwoUmk; sca=9347c5bd; ssxmod_itna=1-YqAhqIxjgGkYi7G7DgD0xqfh7NbGHDyxWKG7DuxiK08D6QDB44jebSvWwb1KciTPeO/ix5D/mQei8E8xYoDeZEP5D0l244o39GDgN3Gii2LP1KXWPEPPf1kCCgu2=/ihrysK4GopDCqGSDDNDDx4DmqG9D0tdD2_NDYYDC4Gw9qG_YeDW_eDEDAoD7hiihDD9Wfp3u7Gbx7AxYPGvDDUrlMb42eGEYCIxDYPwfeGy3KGut1e41RNDKqKtVenGVADw3BwLKDRlfDAfKA=5VBxeCjhQ8AxC99ZLDZZLU_G6/0BmHUOAhxdCjDze7ziiqKHaRG4QTrDrt9hilhS8GP3bNADqQ7LnucAGDD; ssxmod_itna2=1-YqAhqIxjgGkYi7G7DgD0xqfh7NbGHDyxWKG7DuxiK08D6QDB44jebSvWwb1KciTPeO/igqGI/DDIN3O9Cgbig9onHwQ=jWeSTp4D; tfstk=gPrEgQXpj85tNuR1A5ny3puh7LoK005X8uGSE82odXch9uirEfyyOyw7E0SrsD08RbGIa0y8gO1fciwLp0nhGssfy2lGj0hlV0bsjRpB_Zffciw31XojJsZWGhfqFYmoK4xojOkSK0DnqbXZsYkvrBVoqOWZEYAkt2xHSADm30cuZ0XaIfHoqvVoqOyi6YxfwfcBmvghC7ugaD-xF2l0Kf-unlkn8een_3xubDgEincwq3qZp8Wy1NKGqj4YsP43jTTtp0lnuvPRDHnnzy2j30Swsbzon7EZWG-_BqzboVrRDHn3OXiQAmjvtba_3ukz2_IagJqLTzSrUQHGbgE82gLo-AHZGOWNXsTUlWONlkLJy2U-Qj6i3UL--AHZGOWwyU3BuAlfIx5..; token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjAwMzNmNDZkLWNkYzktNDRiOS1hYTAyLWEyZjA3NTg4YmRmZCIsImxhc3RfcGFzc3dvcmRfY2hhbmdlIjoxNzYxMzU1OTY0LCJleHAiOjE3NjE5NjQwNzR9.PbRw0I_udl4LzYsham38oHN5KyhyCgIRn7ryAPZUXGo; xlly_s=1; _bl_uid=Lkmw8e752FgfpXqta4myh1txn7OF; acw_tc=0a03e58917613592701907846e3bc0bf8c460fdfd424ec929f27d30faea69c; x-ap=ap-southeast-5';
|
|
|
|
|
|
|
|
|
|
| 205 |
|
| 206 |
const handler = async (req, res) => {
|
| 207 |
try {
|
|
|
|
| 212 |
});
|
| 213 |
}
|
| 214 |
|
| 215 |
+
const { prompt, model = 'qwen3-max', key } = req.query;
|
| 216 |
|
| 217 |
if (!prompt) {
|
| 218 |
return res.status(400).json({
|
|
|
|
| 221 |
});
|
| 222 |
}
|
| 223 |
|
| 224 |
+
const cookies = process.env.QWEN;
|
| 225 |
+
|
| 226 |
if (!cookies) {
|
| 227 |
+
return res.status(500).json({
|
| 228 |
success: false,
|
| 229 |
+
error: 'QWEN cookies not found in environment variables'
|
| 230 |
});
|
| 231 |
}
|
| 232 |
|
| 233 |
+
const scraper = new QwenChatScraper(null, cookies);
|
| 234 |
+
const newChat = await scraper.createChat('API Chat', model);
|
| 235 |
+
const chatId = newChat.id;
|
| 236 |
+
const result = await scraper.sendMessage(chatId, prompt, model);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 237 |
|
| 238 |
res.json({
|
| 239 |
author: "Herza",
|
| 240 |
success: true,
|
| 241 |
+
chatId: chatId,
|
| 242 |
model: model,
|
| 243 |
response: result.content,
|
| 244 |
usage: result.usage
|
| 245 |
});
|
| 246 |
|
| 247 |
} catch (error) {
|
| 248 |
+
const errorMessage = error.response?.data
|
| 249 |
+
? (typeof error.response.data === 'string'
|
| 250 |
+
? 'Invalid cookies or authentication failed'
|
| 251 |
+
: error.response.data.message || error.message)
|
| 252 |
+
: error.message;
|
| 253 |
+
|
| 254 |
+
res.status(error.response?.status || 500).json({
|
| 255 |
success: false,
|
| 256 |
+
error: errorMessage,
|
| 257 |
+
details: error.response?.status === 401 || error.response?.status === 403
|
| 258 |
+
? 'Cookies invalid or expired. Please get new cookies.'
|
| 259 |
+
: undefined
|
| 260 |
});
|
| 261 |
}
|
| 262 |
};
|
|
|
|
| 268 |
routes: ['api/AI/qwen'],
|
| 269 |
tags: ['ai', 'qwen', 'chat'],
|
| 270 |
main: ['AI'],
|
| 271 |
+
parameters: ['prompt', 'model', 'key'],
|
| 272 |
enabled: true,
|
| 273 |
handler
|
| 274 |
};
|