MCPcopy Create free account
hub / github.com/Rfym21/Qwen2API / handleCliChatCompletion

Function handleCliChatCompletion

src/controllers/cli.chat.js:283–471  ·  view source on GitHub ↗
(req, res)

Source from the content-addressed store, hash-verified

281 * @param {Object} req - Express请求对象
282 * @param {Object} res - Express响应对象
283 */
284const handleCliChatCompletion = async (req, res) => {
285 let body = {}
286 try {
287 const access_token = req.account.cli_info.access_token
288 body = preprocessCliRequestBody(req.body)
289 const isStream = body.stream === true
290
291 // 打印当前使用的账号邮箱
292 logger.info(`CLI请求使用账号[${req.account.email}]开始处理`, 'CLI', '🚀')
293
294 // 无论成功失败都增加请求计数
295 req.account.cli_info.request_number++
296
297 const cliBaseUrl = getCliBaseUrl()
298 const proxyAgent = getProxyAgent(req.account)
299
300 // 设置请求配置
301 const axiosConfig = {
302 method: 'POST',
303 url: `${cliBaseUrl}/v1/chat/completions`,
304 headers: {
305 'Authorization': `Bearer ${access_token}`,
306 'Content-Type': 'application/json',
307 'Accept': isStream ? 'text/event-stream' : 'application/json',
308 'User-Agent': 'QwenCode/0.10.3 (darwin; arm64)',
309 'X-Dashscope-Useragent': 'QwenCode/0.10.3 (darwin; arm64)',
310 'X-Stainless-Runtime-Version': 'v22.17.0',
311 'Sec-Fetch-Mode': 'cors',
312 'X-Stainless-Lang': 'js',
313 'X-Stainless-Arch': 'arm64',
314 'X-Stainless-Package-Version': '5.11.0',
315 'X-Dashscope-Cachecontrol': 'enable',
316 'X-Stainless-Retry-Count': '0',
317 'X-Stainless-Os': 'MacOS',
318 'X-Dashscope-Authtype': 'qwen-oauth',
319 'X-Stainless-Runtime': 'node'
320 },
321 data: body,
322 timeout: 10 * 60 * 1000,
323 validateStatus: function () {
324 return true
325 }
326 }
327
328 // 添加代理配置
329 if (proxyAgent) {
330 axiosConfig.httpsAgent = proxyAgent
331 axiosConfig.proxy = false
332 }
333
334 // 如果是流式请求,设置响应类型为流
335 if (isStream) {
336 axiosConfig.responseType = 'stream'
337
338 // 设置响应头为流式
339 res.setHeader('Content-Type', 'text/event-stream')
340 res.setHeader('Cache-Control', 'no-cache')

Callers

nothing calls this directly

Calls 12

preprocessCliRequestBodyFunction · 0.85
getCliBaseUrlFunction · 0.85
getProxyAgentFunction · 0.85
normalizeCliErrorDetailsFunction · 0.85
attributeCliUsageFunction · 0.85
formatCliJsonResponseFunction · 0.85
buildCliAxiosErrorLogFunction · 0.85
infoMethod · 0.80
errorMethod · 0.80
recordAccountErrorMethod · 0.80
recordAccountFailureMethod · 0.80
successMethod · 0.80

Tested by

no test coverage detected