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
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
const express = require('express')
const axios = require('axios')
const WebSocket = require('ws')
const router = express.Router()
const { v4: uuidv4 } = require('uuid')
const { uploadFileBuffer } = require('../lib/upload')
const verify = require('./verify')
const modelMap = require('../lib/model-map')


async function parseMessages(req, res, next) {
  const messages = req.body.messages
  if (!Array.isArray(messages)) {
    req.processedMessages = []
    return next()
  }

  try {
    const transformedMessages = await Promise.all(messages.map(async (msg) => {
      // Determine provider to conditionally convert system role for Anthropic
      const modelName = req.body.model;
      const modelData = modelMap[modelName]; // modelMap is required at the top
      const provider = modelData?.provider; // Use optional chaining for safety

      let message = {
        role: (msg.role === "system" && provider === "anthropic") ? "user" : msg.role,
        tool_calls: [],
        template_format: "jinja2"
      }

      if (Array.isArray(msg.content)) {
        const contentItems = await Promise.all(msg.content.map(async (item) => {
          if (item.type === "text") {
            return {
              type: "text",
              text: item.text
            }
          }
          else if (item.type === "image_url") {
            try {
              const base64Match = item.image_url.url.match(/^data:image\/\w+;base64,(.+)$/)
              if (base64Match) {
                const base64 = base64Match[1]
                const data = Buffer.from(base64, 'base64')
                const uploadResult = await uploadFileBuffer(data)

                return {
                  type: "media",
                  media: {
                    "type": "image",
                    "url": uploadResult.file_url,
                    "title": `image_${Date.now()}.png`
                  }
                }
              } else {
                return {
                  type: "media",
                  media: {
                    "type": "image",
                    "url": item.image_url.url,
                    "title": "external_image"
                  }
                }
              }
            } catch (error) {
              console.error("处理图像时出错:", error)
              return {
                type: "text",
                text: "[图像处理失败]"
              }
            }
          } else {
            return {
              type: "text",
              text: JSON.stringify(item)
            }
          }
        }))

        message.content = contentItems
      } else {
        message.content = [
          {
            type: "text",
            text: msg.content || ""
          }
        ]
      }

      return message
    }))

    req.body.messages = transformedMessages
    return next()
  } catch (error) {
    console.error("处理消息时出错:", error.status)
    req.body.messages = []
    return next(error)
  }
}

async function getChatID(req, res) {
  try {
    const url = 'https://api.promptlayer.com/api/dashboard/v2/workspaces/' + req.account.workspaceId + '/playground_sessions'
    const headers = { Authorization: "Bearer " + req.account.token }
    const model_data = modelMap[req.body.model] ? modelMap[req.body.model] : modelMap["claude-3-7-sonnet-20250219"]
    let data = {
      "id": uuidv4(),
      "name": "Not implemented",
      "prompt_blueprint": {
        "inference_client_name": null,
        "metadata": {
          "model": model_data
        },
        "prompt_template": {
          "type": "chat",
          "messages": req.body.messages,
          "tools": req.body.tools || [],
          "tool_choice": req.body.tool_choice || "none",
          "input_variables": [],
          "functions": [],
          "function_call": null
        },
        "provider_base_url_name": null
      },
      "input_variables": []
    }

    for (const item in req.body) {
      if (item === "messages" || item === "model" || item === "stream") {
        continue
      }  else if (model_data.parameters[item]) {
        model_data.parameters[item] = req.body[item]
      }
    }
    data.prompt_blueprint.metadata.model = model_data
    console.log(`模型参数: ${data.prompt_blueprint.metadata.model}`)

    const response = await axios.put(url, data, { headers })
    if (response.data.success) {
      console.log(`生成会话ID成功: ${response.data.playground_session.id}`)
      req.chatID = response.data.playground_session.id
      return response.data.playground_session.id
    } else {
      return false
    }
  } catch (error) {
    // console.error("错误:", error.response?.data)
    res.status(500).json({
      "error": {
        "message": error.message || "服务器内部错误",
        "type": "server_error",
        "param": null,
        "code": "server_error"
      }
    })
    return false
  }
}

async function sentRequest(req, res) {
  try {
    const url = 'https://api.promptlayer.com/api/dashboard/v2/workspaces/' + req.account.workspaceId + '/run_groups'
    const headers = { Authorization: "Bearer " + req.account.token }
    const model_data = modelMap[req.body.model] ? modelMap[req.body.model] : modelMap["claude-3-7-sonnet-20250219"];
    const provider = model_data?.provider; // Get provider

    // Base prompt template structure
    let prompt_template = {
      "type": "chat",
      "messages": req.body.messages,
      "tools": req.body.tools || [], // Default value
      "tool_choice": req.body.tool_choice || "none", // Default value
      "input_variables": [],
      "functions": [],
      "function_call": null
    };

    // Conditionally modify for Mistral/Cohere
    if (provider === 'mistral' || provider === 'cohere') {
      prompt_template.tools = null;
      delete prompt_template.tool_choice; // Remove tool_choice entirely
      delete prompt_template.function_call;
    }

    let data = {
      "id": uuidv4(),
      "playground_session_id": req.chatID,
      "shared_prompt_blueprint": {
        "inference_client_name": null,
        "metadata": {
          "model": model_data // Keep original model_data here for metadata
        },
        "prompt_template": prompt_template, // Use the adjusted template
        "provider_base_url_name": null
      },
      "individual_run_requests": [
        {
          "input_variables": {},
          "run_group_position": 1
        }
      ]
    };

    console.log(JSON.stringify(data))

    // Update model parameters (this loop remains the same)
    for (const item in req.body) {
      if (item === "messages" || item === "model" || item === "stream") {
        continue
      } else if (model_data.parameters && model_data.parameters.hasOwnProperty(item)) { // Check if parameters exist and has the property
        model_data.parameters[item] = req.body[item]
      }
    }
    // Ensure the potentially modified model_data (with updated parameters) is in metadata
    data.shared_prompt_blueprint.metadata.model = model_data;

    const response = await axios.post(url, data, { headers });
    if (response.data.success) {
      return response.data.run_group.individual_run_requests[0].id
    } else {
      return false
    }
  } catch (error) {
    // console.error("错误:", error.response?.data)
    res.status(500).json({
      "error": {
        "message": error.message || "服务器内部错误",
        "type": "server_error",
        "param": null,
        "code": "server_error"
      }
    })
  }
}

// 聊天完成路由
router.post('/v1/chat/completions', verify, parseMessages, async (req, res) => {
  // console.log(JSON.stringify(req.body))

  try {

    const setHeader = () => {
      try {
        if (req.body.stream === true) {
          res.setHeader('Content-Type', 'text/event-stream')
          res.setHeader('Cache-Control', 'no-cache')
          res.setHeader('Connection', 'keep-alive')
        } else {
          res.setHeader('Content-Type', 'application/json')
        }
      } catch (error) {
        // console.error("设置响应头时出错:", error)
      }
    }

    const { access_token, clientId } = req.account
    // 生成会话ID
    await getChatID(req, res)

    // 发送的数据
    const sendAction = `{"action":10,"channel":"user:${clientId}","params":{"agent":"react-hooks/2.0.2"}}`
    // 构建 WebSocket URL
    const wsUrl = `wss://realtime.ably.io/?access_token=${encodeURIComponent(access_token)}&clientId=${clientId}&format=json&heartbeats=true&v=3&agent=ably-js%2F2.0.2%20browser`
    // 创建 WebSocket 连接
    const ws = new WebSocket(wsUrl)

    // 状态详细
    let ThinkingLastContent = ""
    let TextLastContent = ""
    let ThinkingStart = false
    let ThinkingEnd = false
    let RequestID = ""
    let MessageID = "chatcmpl-" + uuidv4()
    let streamChunk = {
      "id": MessageID,
      "object": "chat.completion.chunk",
      "system_fingerprint": "fp_44709d6fcb",
      "created": Math.floor(Date.now() / 1000),
      "model": req.body.model,
      "choices": [
        {
          "index": 0,
          "delta": {
            "content": null
          },
          "finish_reason": null
        }
      ]
    }

    let pingInterval;

    ws.on('open', async () => {
      ws.send(sendAction)
      RequestID = await sentRequest(req, res)
      setHeader()
      // Start sending pings every 30 seconds to keep the connection alive
      pingInterval = setInterval(() => {
        if (ws.readyState === WebSocket.OPEN) {
          ws.ping(() => {}); // Empty callback, just to send the ping
        }
      }, 15000);
    })

    ws.on('message', async (data) => {
      try {
        data = data.toString()
        console.log("here!!!")
        console.log(data)
        let ContentText = JSON.parse(data)?.messages?.[0]
        let ContentData = JSON.parse(ContentText?.data)
        const isRequestID = ContentData?.individual_run_request_id
        if (isRequestID != RequestID || !isRequestID) return

        let output = ""

        if (ContentText?.name === "UPDATE_LAST_MESSAGE") {
          const MessageArray = ContentData?.payload?.message?.content
          for (const item of MessageArray) {

            if (item.type === "text") {
              output = item.text.replace(TextLastContent, "")
              if (ThinkingStart && !ThinkingEnd) {
                ThinkingEnd = true
                output = `${output}\n\n</think>`
              }
              TextLastContent = item.text
            }
            else if (item.type === "thinking" && MessageArray.length === 1) {
              output = item.thinking.replace(ThinkingLastContent, "")
              if (!ThinkingStart) {
                ThinkingStart = true
                output = `<think>\n\n${output}`
              }
              ThinkingLastContent = item.thinking
            }

          }

          if (req.body.stream === true) {
            streamChunk.choices[0].delta.content = output
            res.write(`data: ${JSON.stringify(streamChunk)}\n\n`)
          }

        }
        else if (ContentText?.name === "INDIVIDUAL_RUN_COMPLETE") {

          if (req.body.stream !== true) {
            output = ThinkingLastContent ? `<think>\n\n${ThinkingLastContent}\n\n</think>\n\n${TextLastContent}` : TextLastContent
          }

          if (ThinkingLastContent === "" && TextLastContent === "") {
            const modelName = req.body.model;
            const modelData = modelMap[modelName] || modelMap["claude-3-7-sonnet-20250219"]; // Fallback to default if model not found
            const provider = modelData.provider || "anthropic"; // Default to anthropic if provider not found
            const providerUpperCase = provider.charAt(0).toUpperCase() + provider.slice(1);
            output = `${provider}.BadRequestError: Error code: 400 - {'type': 'error', 'error': {'type': 'invalid_request_error', 'message': 'Your credit balance is too low to access the ${providerUpperCase} API. Please go to Plans & Billing to upgrade or purchase credits.'}}`
            streamChunk.choices[0].delta.content = output
            res.write(`data: ${JSON.stringify(streamChunk)}\n\n`)
          }

          if (!req.body.stream || req.body.stream !== true) {
            let responseJson = {
              "id": MessageID,
              "object": "chat.completion",
              "created": Math.floor(Date.now() / 1000),
              "system_fingerprint": "fp_44709d6fcb",
              "model": req.body.model,
              "choices": [
                {
                  "index": 0,
                  "message": {
                    "role": "assistant",
                    "content": output
                  },
                  "finish_reason": "stop"
                }
              ],
              "usage": {
                "prompt_tokens": 0,
                "completion_tokens": 0,
                "total_tokens": 0
              }
            }

            res.json(responseJson)
            ws.close()
            return
          } else {
            // 流式响应:发送结束标记
            let finalChunk = {
              "id": MessageID,
              "object": "chat.completion.chunk",
              "system_fingerprint": "fp_44709d6fcb",
              "created": Math.floor(Date.now() / 1000),
              "model": req.body.model,
              "choices": [
                {
                  "index": 0,
                  "delta": {},
                  "finish_reason": "stop"
                }
              ]
            }

            res.write(`data: ${JSON.stringify(finalChunk)}\n\n`)
            res.write(`data: [DONE]\n\n`)
            res.end()
          }
          ws.close()
        }

      } catch (err) {
        // console.error("处理WebSocket消息出错:", err)
      }
    })

    ws.on('error', (err) => {
      clearInterval(pingInterval); // Stop sending pings
      // 标准OpenAI错误响应格式
      res.status(500).json({
        "error": {
          "message": err.message,
          "type": "server_error",
          "param": null,
          "code": "server_error"
        }
      })
    })

    const oSeriesModels = ["o4-mini", "o4-mini-high", "o3-mini", "o3-mini-high", "o1", "o3", "o3-2025-04-16", "o4-mini-2025-04-16"];
    let timeoutDuration = 300 * 1000; // 默认5分钟

    if (oSeriesModels.includes(req.body.model)) {
      timeoutDuration = 1200 * 1000; // o系列模型20分钟
    }

    const requestTimeout = setTimeout(() => {
      clearInterval(pingInterval); // Stop sending pings
      if (ws.readyState === WebSocket.OPEN) {
        ws.close()
        if (!res.headersSent) {
          // 标准OpenAI超时错误响应格式
          res.status(504).json({
            "error": {
              "message": "请求超时",
              "type": "timeout",
              "param": null,
              "code": "timeout_error"
            }
          })
        }
      }
    }, timeoutDuration)

    ws.on('close', () => {
      clearInterval(pingInterval); // Stop sending pings
      clearTimeout(requestTimeout); // Clear the main request timeout as well if ws closes first
    });

  } catch (error) {
    console.error("错误:", error)
    // 标准OpenAI通用错误响应格式
    res.status(500).json({
      "error": {
        "message": error.message || "服务器内部错误",
        "type": "server_error",
        "param": null,
        "code": "server_error"
      }
    })
  }
})

module.exports = router