smgc commited on
Commit
4cb1fac
·
verified ·
1 Parent(s): 6016dd8

Update app.js

Browse files
Files changed (1) hide show
  1. app.js +11 -2
app.js CHANGED
@@ -75,6 +75,7 @@ function stringToHex(str, modelName) {
75
  function chunkToUtf8String(chunk) {
76
  // 只处理以 0x00 0x00 0x00 0x00 开头的 chunk,其他不处理,不然会有乱码
77
  if (!(chunk[0] === 0x00 && chunk[1] === 0x00)) {
 
78
  return '';
79
  }
80
 
@@ -130,9 +131,12 @@ function chunkToUtf8String(chunk) {
130
  return utf8String;
131
  }
132
 
133
- app.post('/ai/v1/chat/completions', async (req, res) => {
 
 
134
  // o1开头的模型,不支持流式输出
135
  if (req.body.model.startsWith('o1-') && req.body.stream) {
 
136
  return res.status(400).json({
137
  error: 'Model not supported stream'
138
  });
@@ -158,6 +162,7 @@ app.post('/ai/v1/chat/completions', async (req, res) => {
158
  authToken = authToken.split('%3A%3A')[1];
159
  }
160
  if (!messages || !Array.isArray(messages) || messages.length === 0 || !authToken) {
 
161
  return res.status(400).json({
162
  error: 'Invalid request. Messages should be a non-empty array and authorization is required'
163
  });
@@ -166,6 +171,7 @@ app.post('/ai/v1/chat/completions', async (req, res) => {
166
  const formattedMessages = messages.map(msg => `${msg.role}:${msg.content}`).join('\n');
167
  const hexData = stringToHex(formattedMessages, model);
168
 
 
169
  const response = await fetch('https://api2.cursor.sh/aiserver.v1.AiService/StreamChat', {
170
  method: 'POST',
171
  headers: {
@@ -185,6 +191,8 @@ app.post('/ai/v1/chat/completions', async (req, res) => {
185
  body: hexData
186
  });
187
 
 
 
188
  if (stream) {
189
  res.setHeader('Content-Type', 'text/event-stream');
190
  res.setHeader('Cache-Control', 'no-cache');
@@ -197,6 +205,7 @@ app.post('/ai/v1/chat/completions', async (req, res) => {
197
  const text = chunkToUtf8String(chunk);
198
 
199
  if (text.length > 0) {
 
200
  res.write(`data: ${JSON.stringify({
201
  id: responseId,
202
  object: 'chat.completion.chunk',
@@ -223,7 +232,7 @@ app.post('/ai/v1/chat/completions', async (req, res) => {
223
  // 对解析后的字符串进行进一步处理
224
  text = text.replace(/^.*<\|END_USER\|>/s, '');
225
  text = text.replace(/^\n[a-zA-Z]?/, '').trim();
226
- console.log(text);
227
 
228
  return res.json({
229
  id: `chatcmpl-${uuidv4()}`,
 
75
  function chunkToUtf8String(chunk) {
76
  // 只处理以 0x00 0x00 0x00 0x00 开头的 chunk,其他不处理,不然会有乱码
77
  if (!(chunk[0] === 0x00 && chunk[1] === 0x00)) {
78
+ console.log('Chunk does not start with 0x00 0x00, skipping.');
79
  return '';
80
  }
81
 
 
131
  return utf8String;
132
  }
133
 
134
+ app.post('/v1/chat/completions', async (req, res) => {
135
+ console.log('Received request:', req.body);
136
+
137
  // o1开头的模型,不支持流式输出
138
  if (req.body.model.startsWith('o1-') && req.body.stream) {
139
+ console.log('Model not supported stream:', req.body.model);
140
  return res.status(400).json({
141
  error: 'Model not supported stream'
142
  });
 
162
  authToken = authToken.split('%3A%3A')[1];
163
  }
164
  if (!messages || !Array.isArray(messages) || messages.length === 0 || !authToken) {
165
+ console.log('Invalid request:', { messages, authToken });
166
  return res.status(400).json({
167
  error: 'Invalid request. Messages should be a non-empty array and authorization is required'
168
  });
 
171
  const formattedMessages = messages.map(msg => `${msg.role}:${msg.content}`).join('\n');
172
  const hexData = stringToHex(formattedMessages, model);
173
 
174
+ console.log('Sending request to external API with token:', authToken);
175
  const response = await fetch('https://api2.cursor.sh/aiserver.v1.AiService/StreamChat', {
176
  method: 'POST',
177
  headers: {
 
191
  body: hexData
192
  });
193
 
194
+ console.log('Received response from external API:', response.status, response.statusText);
195
+
196
  if (stream) {
197
  res.setHeader('Content-Type', 'text/event-stream');
198
  res.setHeader('Cache-Control', 'no-cache');
 
205
  const text = chunkToUtf8String(chunk);
206
 
207
  if (text.length > 0) {
208
+ console.log('Sending chunk:', text);
209
  res.write(`data: ${JSON.stringify({
210
  id: responseId,
211
  object: 'chat.completion.chunk',
 
232
  // 对解析后的字符串进行进一步处理
233
  text = text.replace(/^.*<\|END_USER\|>/s, '');
234
  text = text.replace(/^\n[a-zA-Z]?/, '').trim();
235
+ console.log('Final text:', text);
236
 
237
  return res.json({
238
  id: `chatcmpl-${uuidv4()}`,