From e08029a6f5de761999c63e4c99d8e0f08ca0e36d Mon Sep 17 00:00:00 2001 From: ousugo Date: Tue, 25 Feb 2025 22:09:48 +0800 Subject: [PATCH] feat: Improve think tag processing with more robust parsing --- src/renderer/src/utils/formats.ts | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/src/renderer/src/utils/formats.ts b/src/renderer/src/utils/formats.ts index d0ea81b52d..5f9ffadf7a 100644 --- a/src/renderer/src/utils/formats.ts +++ b/src/renderer/src/utils/formats.ts @@ -101,12 +101,11 @@ const glmZeroPreviewProcessor: ThoughtProcessor = { } const thinkTagProcessor: ThoughtProcessor = { - canProcess: (content: string) => content.startsWith(''), + canProcess: (content: string) => content.startsWith('') || content.includes(''), process: (content: string) => { + // 处理正常闭合的 think 标签 const thinkPattern = /^(.*?)<\/think>/s const matches = content.match(thinkPattern) - - // 处理正常闭合的 think 标签 if (matches) { return { reasoning: matches[1].trim(), @@ -114,10 +113,26 @@ const thinkTagProcessor: ThoughtProcessor = { } } - // 处理未闭合的 think 标签 + // 处理只有结束标签的情况 + if (content.includes('') && !content.startsWith('')) { + const parts = content.split('') + return { + reasoning: parts[0].trim(), + content: parts.slice(1).join('').trim() + } + } + + // 处理只有开始标签的情况 + if (content.startsWith('')) { + return { + reasoning: content.slice(7).trim(), // 跳过 '' 标签 + content: '' + } + } + return { - reasoning: content.slice(7).trim(), // 跳过 '' 标签 - content: '' + reasoning: '', + content } } }