Qwen2API-A / src /utils /request.js
github-actions[bot]
Sync from GitHub Viciy2023/Qwen2API-A@ae093476e9bc5b0a599620b5925df3a20057038e
f120063
const axios = require('axios')
const accountManager = require('./account.js')
const { logger } = require('./logger')
const { getSsxmodItna, getSsxmodItna2 } = require('./ssxmod-manager')
const { getProxyAgent, getChatBaseUrl, applyProxyToAxiosConfig } = require('./proxy-helper')
/**
* 发送聊天请求
* @param {Object} body - 请求体
* @param {number} retryCount - 当前重试次数
* @param {string} lastUsedEmail - 上次使用的邮箱(用于错误记录)
* @returns {Promise<Object>} 响应结果
*/
const sendChatRequest = async (body) => {
try {
// 获取可用的令牌
const currentToken = accountManager.getAccountToken()
if (!currentToken) {
logger.error('无法获取有效的访问令牌', 'TOKEN')
return {
status: false,
response: null
}
}
const chatBaseUrl = getChatBaseUrl()
const proxyAgent = getProxyAgent()
// 构建请求配置
const requestConfig = {
headers: {
'Authorization': `Bearer ${currentToken}`,
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/143.0.0.0 Safari/537.36 Edg/143.0.0.0",
"Connection": "keep-alive",
"Accept": "application/json",
"Accept-Encoding": "gzip, deflate, br, zstd",
"Content-Type": "application/json",
"Timezone": "Mon Dec 08 2025 17:28:55 GMT+0800",
"sec-ch-ua": "\"Microsoft Edge\";v=\"143\", \"Chromium\";v=\"143\", \"Not A(Brand\";v=\"24\"",
"source": "web",
"Version": "0.1.13",
"bx-v": "2.5.31",
"Origin": chatBaseUrl,
"Sec-Fetch-Site": "same-origin",
"Sec-Fetch-Mode": "cors",
"Sec-Fetch-Dest": "empty",
"Referer": `${chatBaseUrl}/c/guest`,
"Accept-Language": "zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7",
"Cookie": `ssxmod_itna=${getSsxmodItna()};ssxmod_itna2=${getSsxmodItna2()}`,
},
responseType: 'stream', // Always use streaming (upstream doesn't support stream=false)
timeout: 60 * 1000,
}
// 添加代理配置
if (proxyAgent) {
requestConfig.httpsAgent = proxyAgent
requestConfig.proxy = false // 禁用axios默认代理,使用httpsAgent
}
// console.log(body)
// console.log(requestConfig)
const chat_id = await generateChatID(currentToken, body.model)
logger.network(`发送聊天请求`, 'REQUEST')
const response = await axios.post(`${chatBaseUrl}/api/v2/chat/completions?chat_id=` + chat_id, {
...body,
stream: true, // Always request streaming (upstream doesn't support stream=false)
chat_id: chat_id
}, requestConfig)
// 请求成功
if (response.status === 200) {
// console.log(response.data)
return {
currentToken: currentToken,
status: true,
response: response.data
}
}
} catch (error) {
console.log(error)
logger.error('发送聊天请求失败', 'REQUEST', '', error.message)
return {
status: false,
response: null
}
}
}
/**
* 生成chat_id
* @param {*} currentToken
* @returns {Promise<string|null>} 返回生成的chat_id,如果失败则返回null
*/
const generateChatID = async (currentToken, model) => {
try {
const chatBaseUrl = getChatBaseUrl()
const proxyAgent = getProxyAgent()
const requestConfig = {
headers: {
'Authorization': `Bearer ${currentToken}`,
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/143.0.0.0 Safari/537.36 Edg/143.0.0.0",
"Connection": "keep-alive",
"Accept": "application/json",
"Accept-Encoding": "gzip, deflate, br, zstd",
"Content-Type": "application/json",
"Timezone": "Mon Dec 08 2025 17:28:55 GMT+0800",
"sec-ch-ua": "\"Microsoft Edge\";v=\"143\", \"Chromium\";v=\"143\", \"Not A(Brand\";v=\"24\"",
"source": "web",
"Version": "0.1.13",
"bx-v": "2.5.31",
"Origin": chatBaseUrl,
"Sec-Fetch-Site": "same-origin",
"Sec-Fetch-Mode": "cors",
"Sec-Fetch-Dest": "empty",
"Referer": `${chatBaseUrl}/c/guest`,
"Accept-Language": "zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7",
"Cookie": `ssxmod_itna=${getSsxmodItna()};ssxmod_itna2=${getSsxmodItna2()}`,
}
}
// 添加代理配置
if (proxyAgent) {
requestConfig.httpsAgent = proxyAgent
requestConfig.proxy = false
}
const response_data = await axios.post(`${chatBaseUrl}/api/v2/chats/new`, {
"title": "New Chat",
"models": [
model
],
"chat_mode": "local",
"chat_type": "t2i",
"timestamp": new Date().getTime()
}, requestConfig)
// console.log(response_data.data)
return response_data.data?.data?.id || null
} catch (error) {
logger.error('生成chat_id失败', 'CHAT', '', error.message)
return null
}
}
module.exports = {
sendChatRequest,
generateChatID
}