From 9e808208ab0b3fe5292fff55104841bfb29dbfd9 Mon Sep 17 00:00:00 2001 From: ousugo Date: Fri, 28 Feb 2025 21:06:39 +0800 Subject: [PATCH] fix: Normal content is misidentified as chain of thought content --- src/renderer/src/utils/formats.ts | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/src/renderer/src/utils/formats.ts b/src/renderer/src/utils/formats.ts index 5f9ffadf..5e67d547 100644 --- a/src/renderer/src/utils/formats.ts +++ b/src/renderer/src/utils/formats.ts @@ -1,3 +1,4 @@ +import { isReasoningModel } from '@renderer/config/models' import { Message } from '@renderer/types' export function escapeDollarNumber(text: string) { @@ -82,12 +83,24 @@ export function withGeminiGrounding(message: Message) { } interface ThoughtProcessor { - canProcess: (content: string) => boolean + canProcess: (content: string, message?: Message) => boolean process: (content: string) => { reasoning: string; content: string } } const glmZeroPreviewProcessor: ThoughtProcessor = { - canProcess: (content: string) => content.includes('###Thinking'), + canProcess: (content: string, message?: Message) => { + if (!message) return false + + const model = message.model + if (!model || !isReasoningModel(model)) return false + + const modelId = message.modelId || '' + const modelName = model.name || '' + const isGLMZeroPreview = + modelId.toLowerCase().includes('glm-zero-preview') || modelName.toLowerCase().includes('glm-zero-preview') + + return isGLMZeroPreview && content.includes('###Thinking') + }, process: (content: string) => { const parts = content.split('###') const thinkingMatch = parts.find((part) => part.trim().startsWith('Thinking')) @@ -101,7 +114,14 @@ const glmZeroPreviewProcessor: ThoughtProcessor = { } const thinkTagProcessor: ThoughtProcessor = { - canProcess: (content: string) => content.startsWith('') || content.includes(''), + canProcess: (content: string, message?: Message) => { + if (!message) return false + + const model = message.model + if (!model || !isReasoningModel(model)) return false + + return content.startsWith('') || content.includes('') + }, process: (content: string) => { // 处理正常闭合的 think 标签 const thinkPattern = /^(.*?)<\/think>/s @@ -145,7 +165,7 @@ export function withMessageThought(message: Message) { const content = message.content.trim() const processors: ThoughtProcessor[] = [glmZeroPreviewProcessor, thinkTagProcessor] - const processor = processors.find((p) => p.canProcess(content)) + const processor = processors.find((p) => p.canProcess(content, message)) if (processor) { const { reasoning, content: processedContent } = processor.process(content) message.reasoning_content = reasoning