Upload 2 files
Browse files- src/index.js +7 -30
src/index.js
CHANGED
@@ -85,7 +85,7 @@ app.use(async (ctx, next) => {
|
|
85 |
})
|
86 |
|
87 |
const makeRequest = async (session_id, requestModel, messages) => {
|
88 |
-
console.log('
|
89 |
try {
|
90 |
// 设置请求头
|
91 |
const myHeaders = new Headers()
|
@@ -111,12 +111,6 @@ const makeRequest = async (session_id, requestModel, messages) => {
|
|
111 |
}
|
112 |
})
|
113 |
|
114 |
-
console.log('请求配置:', {
|
115 |
-
url: "https://www.genspark.ai/api/copilot/ask",
|
116 |
-
headers: Object.fromEntries(myHeaders.entries()),
|
117 |
-
body: JSON.parse(body)
|
118 |
-
})
|
119 |
-
|
120 |
const requestConfig = {
|
121 |
method: 'POST',
|
122 |
headers: myHeaders,
|
@@ -125,11 +119,6 @@ const makeRequest = async (session_id, requestModel, messages) => {
|
|
125 |
};
|
126 |
|
127 |
const response = await fetch("https://www.genspark.ai/api/copilot/ask", requestConfig)
|
128 |
-
console.log('收到响应:', {
|
129 |
-
status: response.status,
|
130 |
-
statusText: response.statusText,
|
131 |
-
headers: Object.fromEntries(response.headers.entries())
|
132 |
-
})
|
133 |
|
134 |
if (!response.ok) {
|
135 |
throw new Error(`HTTP error! status: ${response.status}`)
|
@@ -147,12 +136,10 @@ const handleChatCompletions = async (ctx) => {
|
|
147 |
const { messages, stream = false, model = 'claude-3-5-sonnet' } = ctx.request.body
|
148 |
const session_id = ctx.get('Authorization')?.replace('Bearer ', '')
|
149 |
|
150 |
-
console.log('
|
151 |
path: ctx.path,
|
152 |
-
method: ctx.method,
|
153 |
model,
|
154 |
stream,
|
155 |
-
session_id: session_id ? '***' : undefined,
|
156 |
messages_count: messages?.length
|
157 |
})
|
158 |
|
@@ -169,21 +156,15 @@ const handleChatCompletions = async (ctx) => {
|
|
169 |
throw new Error('Response body is null')
|
170 |
}
|
171 |
|
172 |
-
console.log('处理响应中...', {
|
173 |
-
status: response.status,
|
174 |
-
headers: Object.fromEntries(response.headers.entries()),
|
175 |
-
stream
|
176 |
-
})
|
177 |
-
|
178 |
if (stream == "true" || stream == true) {
|
179 |
-
ctx.status = 200
|
180 |
ctx.set({
|
181 |
'Content-Type': 'text/event-stream',
|
182 |
'Cache-Control': 'no-cache',
|
183 |
'Connection': 'keep-alive',
|
184 |
})
|
185 |
} else {
|
186 |
-
ctx.status = 200
|
187 |
ctx.set({
|
188 |
'Content-Type': 'application/json',
|
189 |
})
|
@@ -227,7 +208,6 @@ const handleChatCompletions = async (ctx) => {
|
|
227 |
const { done, value } = await reader.read()
|
228 |
if (done) {
|
229 |
if (stream == "true" || stream == true) {
|
230 |
-
console.log('流式响应完成')
|
231 |
ctx.res.write('data: [DONE]\n\n')
|
232 |
}
|
233 |
break
|
@@ -235,7 +215,6 @@ const handleChatCompletions = async (ctx) => {
|
|
235 |
|
236 |
if (stream == "true" || stream == true) {
|
237 |
const text = new TextDecoder().decode(value)
|
238 |
-
console.log('处理流式数据块:', text.length, '字节')
|
239 |
const textContent = [...text.matchAll(/data:.*"}/g)]
|
240 |
|
241 |
textContent.forEach(item => {
|
@@ -293,14 +272,12 @@ const handleChatCompletions = async (ctx) => {
|
|
293 |
}
|
294 |
ctx.body = resBody
|
295 |
} else {
|
296 |
-
console.log('正常结束流式响应')
|
297 |
ctx.res.end()
|
298 |
}
|
299 |
} catch (error) {
|
300 |
-
console.error('
|
301 |
if (stream == "true" || stream == true) {
|
302 |
try {
|
303 |
-
console.log('发送错误信息到流')
|
304 |
ctx.res.write(`data: ${JSON.stringify({
|
305 |
"id": `chatcmpl-${messageId}`,
|
306 |
"choices": [
|
@@ -322,8 +299,8 @@ const handleChatCompletions = async (ctx) => {
|
|
322 |
ctx.res.end()
|
323 |
}
|
324 |
} else {
|
325 |
-
|
326 |
-
|
327 |
}
|
328 |
}
|
329 |
} catch (error) {
|
|
|
85 |
})
|
86 |
|
87 |
const makeRequest = async (session_id, requestModel, messages) => {
|
88 |
+
console.log('开始请求:', { session_id: '***', model: requestModel })
|
89 |
try {
|
90 |
// 设置请求头
|
91 |
const myHeaders = new Headers()
|
|
|
111 |
}
|
112 |
})
|
113 |
|
|
|
|
|
|
|
|
|
|
|
|
|
114 |
const requestConfig = {
|
115 |
method: 'POST',
|
116 |
headers: myHeaders,
|
|
|
119 |
};
|
120 |
|
121 |
const response = await fetch("https://www.genspark.ai/api/copilot/ask", requestConfig)
|
|
|
|
|
|
|
|
|
|
|
122 |
|
123 |
if (!response.ok) {
|
124 |
throw new Error(`HTTP error! status: ${response.status}`)
|
|
|
136 |
const { messages, stream = false, model = 'claude-3-5-sonnet' } = ctx.request.body
|
137 |
const session_id = ctx.get('Authorization')?.replace('Bearer ', '')
|
138 |
|
139 |
+
console.log('收到请求:', {
|
140 |
path: ctx.path,
|
|
|
141 |
model,
|
142 |
stream,
|
|
|
143 |
messages_count: messages?.length
|
144 |
})
|
145 |
|
|
|
156 |
throw new Error('Response body is null')
|
157 |
}
|
158 |
|
|
|
|
|
|
|
|
|
|
|
|
|
159 |
if (stream == "true" || stream == true) {
|
160 |
+
ctx.status = 200
|
161 |
ctx.set({
|
162 |
'Content-Type': 'text/event-stream',
|
163 |
'Cache-Control': 'no-cache',
|
164 |
'Connection': 'keep-alive',
|
165 |
})
|
166 |
} else {
|
167 |
+
ctx.status = 200
|
168 |
ctx.set({
|
169 |
'Content-Type': 'application/json',
|
170 |
})
|
|
|
208 |
const { done, value } = await reader.read()
|
209 |
if (done) {
|
210 |
if (stream == "true" || stream == true) {
|
|
|
211 |
ctx.res.write('data: [DONE]\n\n')
|
212 |
}
|
213 |
break
|
|
|
215 |
|
216 |
if (stream == "true" || stream == true) {
|
217 |
const text = new TextDecoder().decode(value)
|
|
|
218 |
const textContent = [...text.matchAll(/data:.*"}/g)]
|
219 |
|
220 |
textContent.forEach(item => {
|
|
|
272 |
}
|
273 |
ctx.body = resBody
|
274 |
} else {
|
|
|
275 |
ctx.res.end()
|
276 |
}
|
277 |
} catch (error) {
|
278 |
+
console.error('响应处理出错:', error)
|
279 |
if (stream == "true" || stream == true) {
|
280 |
try {
|
|
|
281 |
ctx.res.write(`data: ${JSON.stringify({
|
282 |
"id": `chatcmpl-${messageId}`,
|
283 |
"choices": [
|
|
|
299 |
ctx.res.end()
|
300 |
}
|
301 |
} else {
|
302 |
+
ctx.status = 500
|
303 |
+
ctx.body = { error: '响应处理失败', details: error.toString() }
|
304 |
}
|
305 |
}
|
306 |
} catch (error) {
|