Spaces:
Running
Running
File size: 5,686 Bytes
f120063 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 | 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
} |