Skip to content

Commit c2e838f

Browse files
committed
fix:修正一处functioncall时tink标签错乱的问题
1 parent 82af2d3 commit c2e838f

File tree

1 file changed

+42
-23
lines changed

1 file changed

+42
-23
lines changed

src/resources/chatPanelScript.js

Lines changed: 42 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -202,21 +202,28 @@ function processMathBlocks(input) {
202202
}
203203

204204
function separateThinkContent(input) {
205-
// Initialize outputs
206-
let thinkStr = '';
207-
let answerStr = input;
208-
209-
// Regex to match <think>...</think> (non-greedy)
210-
const thinkRegex = /<think>([\s\S]*?)<\/think>/;
211-
212-
// Find the first <think>...</think> match
213-
const match = input.match(thinkRegex);
214-
if (match) {
215-
thinkStr = match[0]; // Full <think>...</think> content
216-
answerStr = input.replace(thinkRegex, ''); // Remove <think>...</think>
217-
}
205+
const segments = [];
206+
207+
// 使用split捕获think块和非think内容
208+
const parts = input.split(/(<think>[\s\S]*?<\/think>)/g);
209+
210+
parts.forEach(part => {
211+
if (!part) return;
212+
213+
if (part.startsWith('<think>') && part.endsWith('</think>')) {
214+
segments.push({
215+
type: 'think',
216+
content: part
217+
});
218+
} else {
219+
segments.push({
220+
type: 'answer',
221+
content: part
222+
});
223+
}
224+
});
218225

219-
return { thinkStr, answerStr };
226+
return segments;
220227
}
221228

222229
// 渲染消息
@@ -242,16 +249,28 @@ async function renderMessage(role, content, index) {
242249

243250
markdownContent = processMathBlocks(markdownContent);
244251

245-
const { thinkStr, answerStr } = separateThinkContent(markdownContent);
246-
markdownContent = answerStr;
247-
248-
targetDiv.innerHTML = thinkStr + marked.parse(markdownContent, {
249-
breaks: false,
250-
mangle: false,
251-
headerIds: false,
252-
highlight: (code, lang) => hljs.highlight(hljs.getLanguage(lang) ? lang : 'plaintext', code).value
252+
const segments = separateThinkContent(markdownContent);
253+
let htmlContent = '';
254+
255+
segments.forEach(segment => {
256+
if (segment.type === 'think') {
257+
// Think内容直接显示原始标签
258+
htmlContent += `<think>${segment.content}</think>`;
259+
} else {
260+
// Answer内容用marked解析
261+
htmlContent += marked.parse(segment.content, {
262+
breaks: false,
263+
mangle: false,
264+
headerIds: false,
265+
highlight: (code, lang) => hljs.highlight(
266+
hljs.getLanguage(lang) ? lang : 'plaintext',
267+
code
268+
).value
269+
});
270+
}
253271
});
254-
272+
273+
targetDiv.innerHTML = htmlContent;
255274
fnRenderDisplayMath(targetDiv);
256275
await renderMermaid(targetDiv);
257276

0 commit comments

Comments
 (0)