|
@@ -257,29 +257,57 @@ const simulateStreamResponse = async () => {
|
|
|
|
|
|
if (data.data === true) {
|
|
|
|
|
|
- // 检查是否存在</think>标签但缺少开始标签
|
|
|
- const thinkEndIndex = accumulatedText.indexOf('</think>');
|
|
|
- if (thinkEndIndex !== -1) {
|
|
|
- // 找到</think>标签的位置,在其对应内容的开头添加<think>标签
|
|
|
- const beforeThinkEnd = accumulatedText.substring(0, thinkEndIndex + 8); // 包含</think>
|
|
|
- const afterThinkEnd = accumulatedText.substring(thinkEndIndex + 8);
|
|
|
-
|
|
|
- if (!beforeThinkEnd.includes('<think>')) {
|
|
|
- accumulatedText = '<think>' + beforeThinkEnd + afterThinkEnd;
|
|
|
- }
|
|
|
- }
|
|
|
+ // 处理think标签
|
|
|
+ let processedText = accumulatedText;
|
|
|
+ const thinkEndRegex = /([^<])<\/think>/g;
|
|
|
+ const matches = [...processedText.matchAll(thinkEndRegex)];
|
|
|
|
|
|
- console.log('数据流结束',accumulatedText);
|
|
|
+ for (const match of matches) {
|
|
|
+ const beforeThinkEnd = processedText.substring(0, match.index + match[1].length);
|
|
|
+ const afterThinkEnd = processedText.substring(match.index + match[1].length);
|
|
|
+ console.log('beforeThinkEnd',beforeThinkEnd);
|
|
|
+ console.log('afterThinkEnd',afterThinkEnd);
|
|
|
+ // 在对应的</think>标签前添加<think>标签
|
|
|
+ processedText = '<think>' + beforeThinkEnd + afterThinkEnd;
|
|
|
+ }
|
|
|
+
|
|
|
+ console.log('processedText',processedText);
|
|
|
|
|
|
- // 检查是否包含<think>标签
|
|
|
- if (accumulatedText.includes('<think>') && accumulatedText.includes('</think>')) {
|
|
|
- // 如果是think标签内容,直接显示原文
|
|
|
- messages.value[messageIndex].content = accumulatedText;
|
|
|
+ // 如果内容包含think标签,分别处理think标签内外的内容
|
|
|
+ if (processedText.includes('<think>') && processedText.includes('</think>')) {
|
|
|
+ let finalContent = '';
|
|
|
+ let currentPos = 0;
|
|
|
+
|
|
|
+ while (true) {
|
|
|
+ const thinkStart = processedText.indexOf('<think>', currentPos);
|
|
|
+ const thinkEnd = processedText.indexOf('</think>', currentPos);
|
|
|
+
|
|
|
+ if (thinkStart === -1 || thinkEnd === -1) {
|
|
|
+ // 处理剩余的文本
|
|
|
+ if (currentPos < processedText.length) {
|
|
|
+ finalContent += md.render(processedText.substring(currentPos));
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 处理think标签之前的内容(需要markdown渲染)
|
|
|
+ if (thinkStart > currentPos) {
|
|
|
+ finalContent += md.render(processedText.substring(currentPos, thinkStart));
|
|
|
+ }
|
|
|
+
|
|
|
+ // 添加think标签内的内容(原样保留)
|
|
|
+ const thinkContent = processedText.substring(thinkStart, thinkEnd + 8);
|
|
|
+ finalContent += thinkContent;
|
|
|
+
|
|
|
+ currentPos = thinkEnd + 8;
|
|
|
+ }
|
|
|
+
|
|
|
+ messages.value[messageIndex].content = finalContent;
|
|
|
} else {
|
|
|
- // 否则进行markdown渲染
|
|
|
- const formattedText = md.render(accumulatedText);
|
|
|
- messages.value[messageIndex].content = formattedText;
|
|
|
+ // 如果没有think标签,进行普通的markdown渲染
|
|
|
+ messages.value[messageIndex].content = md.render(processedText);
|
|
|
}
|
|
|
+
|
|
|
eventSource.close();
|
|
|
loading.value = false;
|
|
|
await scrollToBottom();
|