File size: 6,900 Bytes
cc74e5c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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
const Koa = require('koa')
const Router = require('koa-router')
const bodyParser = require('koa-bodyparser')
const models = require("./models")


const app = new Koa()
const router = new Router()

// 使用 bodyParser 中间件
app.use(bodyParser())

// 配置 bodyParser
app.use(bodyParser({
  enableTypes: ['json', 'form', 'text'],
  jsonLimit: '30mb',  // JSON 数据大小限制
  formLimit: '30mb',  // form 数据大小限制
  textLimit: '30mb',  // text 数据大小限制
}))


const makeRequest = async (session_id, requestModel, messages) => {
  // console.log(session_id, requestModel, messages)
  try {
    // 设置请求头
    const myHeaders = new Headers()
    myHeaders.append("Cookie", `session_id=${session_id}`)
    myHeaders.append("User-Agent", "Apifox/1.0.0 (https://apifox.com)");
    myHeaders.append("Content-Type", "application/json")
    myHeaders.append("Accept", "*/*")
    myHeaders.append("Host", "www.genspark.ai")
    myHeaders.append("Connection", "keep-alive")


    // 设置请求体
    var body = JSON.stringify({
      "type": "COPILOT_MOA_CHAT",
      "current_query_string": "type=COPILOT_MOA_CHAT",
      "messages": messages,
      "action_params": {},
      "extra_data": {
        "models": [
          models[`${requestModel}`] || models["claude-3-5-sonnet-20241022"]
        ],
        "run_with_another_model": false,
        "writingContent": null
      }
    })

    const requestConfig = {
      method: 'POST',
      headers: myHeaders,
      body: body,
      redirect: 'follow'
    };

    // console.log(requestConfig)
    return await fetch("https://www.genspark.ai/api/copilot/ask", requestConfig)
  } catch (error) {
    console.log('error1', error)
  }
}


router.post('/v1/chat/completions', async (ctx) => {
  const { messages, stream = false, model = 'claude-3-5-sonnet' } = ctx.request.body
  const session_id = ctx.get('Authorization')?.replace('Bearer ', '')

  if (!session_id) {
    ctx.status = 401
    ctx.body = { error: '未提供有效的 session_id' }
    return
  }

  try {
    const response = await makeRequest(session_id, model, messages)
    if (stream == "true" || stream == true) {
      ctx.set({
        'Content-Type': 'text/event-stream',
        'Cache-Control': 'no-cache',
        'Connection': 'keep-alive',
      })
    } else {
      ctx.set({
        'Content-Type': 'application/json',
      })
    }

    const messageId = crypto.randomUUID()
    const reader = response.body.getReader()
    if (stream == "true" || stream == true) {
      ctx.res.write(`data: ${JSON.stringify({

        "id": `chatcmpl-${messageId}`,

        "choices": [

          {

            "index": 0,

            "delta": {

              "content": "",

              "role": "assistant"

            }

          }

        ],

        "created": Math.floor(Date.now() / 1000),

        "model": models[`${model}`],

        "object": "chat.completion.chunk"

      })}\n\n`)
    }

    try {
      let resBody = {}

      while (true) {
        const { done, value } = await reader.read()
        if (done) {
          if (stream == "true" || stream == true) {
            // 发送完成标记
            ctx.res.write('data: [DONE]\n\n')
          }
          break
        }

        if (stream) {

          const text = new TextDecoder().decode(value)
          const textContent = [...text.matchAll(/data:.*"}/g)]

          textContent.forEach(item => {
            if (!item[0]) {
              return
            }

            const content = JSON.parse(item[0].replace("data: ", ''))
            if (!content || !content.delta) {
              return
            }

            // console.log(content.delta)

            // 发送增量内容
            ctx.res.write(`data: ${JSON.stringify({

              "id": `chatcmpl-${messageId}`,

              "choices": [

                {

                  "index": 0,

                  "delta": {

                    "content": content.delta

                  }

                }

              ],

              "created": Math.floor(Date.now() / 1000),

              "model": models[`${model}`],

              "object": "chat.completion.chunk"

            })}\n\n`)
          })
        } else {
          const text = new TextDecoder().decode(value)
          const textContent = [...text.matchAll(/data:.*"}/g)]


          textContent.forEach(item => {
            if (!item[0]) {
              return
            }

            const content = JSON.parse(item[0].replace("data: ", ''))
            if (!content || !content.field_value || content.field_name == 'session_state.answer_is_finished' || content.field_name == 'content' || content.field_name == 'session_state' || content.delta || content.type == 'project_field') {
              return
            }

            // console.log(content)

            resBody = {
              id: `chatcmpl-${messageId}`,
              object: 'chat.completion',
              created: Math.floor(Date.now() / 1000),
              model,
              choices: [
                {
                  index: 0,
                  message: {
                    role: 'assistant',
                    content: content.field_value,
                  },
                  finish_reason: 'stop',
                },
              ],
              usage: {
                prompt_tokens: 0,
                completion_tokens: 0,
                total_tokens: content.field_value.length,
              },
            }

          })
        }

      }

      if (stream == "false" || stream == false) {
        // console.log(resBody)
        ctx.body = resBody
      } else {
        ctx.res.end()
      }
      return
    } catch (error) {
      console.error('流式响应出错:', error)
      ctx.res.end()
    }

  } catch (error) {
    console.error('请求处理出错:', error)
    ctx.status = 500
    ctx.body = { error: '请求处理失败' }
  }
})


// 获取models
router.get('/v1/models', async (ctx) => {
  ctx.body = {
    object: "list",
    data: Object.keys(models).map(model => ({
      id: model,
      object: "model",
      created: 1706745938,
      owned_by: "genspark"
    }))
  }
})


// 注册路由
app.use(router.routes()).use(router.allowedMethods())

// 错误处理中间件
app.use(async (ctx, next) => {
  try {
    await next()
  } catch (err) {
    ctx.status = err.status || 500
    ctx.body = {
      success: false,
      message: err.message
    }
    ctx.app.emit('error', err, ctx)
  }
})

// 启动服务器
const PORT = process.env.PORT || 8666
app.listen(PORT, () => {
  console.log(`服务器运行在 http://localhost:${PORT}`);
})