dan92 commited on
Commit
8d1bb38
·
verified ·
1 Parent(s): 66dd09c

Upload 2 files

Browse files
Files changed (1) hide show
  1. src/index.js +13 -1
src/index.js CHANGED
@@ -152,6 +152,7 @@ const handleStreamData = (text, messageId, model, ctx) => {
152
  const textContent = [...text.matchAll(/data:.*(?:}|]$)/g)]
153
  let lastContent = null;
154
  let isCompletingMessage = false;
 
155
 
156
  for (let index = 0; index < textContent.length; index++) {
157
  const item = textContent[index];
@@ -183,6 +184,7 @@ const handleStreamData = (text, messageId, model, ctx) => {
183
  // 如果正在完成消息,跳过增量更新
184
  if (!isCompletingMessage) {
185
  contentToSend = content.delta;
 
186
  }
187
  }
188
  // 处理完整消息
@@ -193,7 +195,10 @@ const handleStreamData = (text, messageId, model, ctx) => {
193
  if (content.field_value.includes('</thinking>') ||
194
  content.field_value.includes('</result>')) {
195
  isCompletingMessage = true;
196
- contentToSend = content.field_value;
 
 
 
197
  }
198
  }
199
  }
@@ -210,6 +215,13 @@ const handleStreamData = (text, messageId, model, ctx) => {
210
  // 如果有内容要发送,且与上一次不同
211
  if (contentToSend && contentToSend !== lastContent) {
212
  lastContent = contentToSend;
 
 
 
 
 
 
 
213
  ctx.res.write(`data: ${JSON.stringify({
214
  "id": `chatcmpl-${messageId}`,
215
  "choices": [
 
152
  const textContent = [...text.matchAll(/data:.*(?:}|]$)/g)]
153
  let lastContent = null;
154
  let isCompletingMessage = false;
155
+ let accumulatedContent = '';
156
 
157
  for (let index = 0; index < textContent.length; index++) {
158
  const item = textContent[index];
 
184
  // 如果正在完成消息,跳过增量更新
185
  if (!isCompletingMessage) {
186
  contentToSend = content.delta;
187
+ accumulatedContent += contentToSend;
188
  }
189
  }
190
  // 处理完整消息
 
195
  if (content.field_value.includes('</thinking>') ||
196
  content.field_value.includes('</result>')) {
197
  isCompletingMessage = true;
198
+ // 如果累积的内容与完整消息不同,则发送完整消息
199
+ if (content.field_value !== accumulatedContent) {
200
+ contentToSend = content.field_value;
201
+ }
202
  }
203
  }
204
  }
 
215
  // 如果有内容要发送,且与上一次不同
216
  if (contentToSend && contentToSend !== lastContent) {
217
  lastContent = contentToSend;
218
+ // 如果是增量更新,检查是否包含完整消息标记
219
+ if (content?.type === 'message_field_delta' &&
220
+ (contentToSend.includes('</thinking>') ||
221
+ contentToSend.includes('</result>'))) {
222
+ // 跳过增量更新中的完整消息
223
+ continue;
224
+ }
225
  ctx.res.write(`data: ${JSON.stringify({
226
  "id": `chatcmpl-${messageId}`,
227
  "choices": [