From 1ac9ac09c9272be5ae07591f20e7d2d2a992e603 Mon Sep 17 00:00:00 2001 From: SuYao Date: Tue, 29 Apr 2025 16:16:34 +0800 Subject: [PATCH] refactor(MainTextBlock): enhance content processing by ignoring tooluse (#5483) --- .../src/pages/home/Messages/Blocks/MainTextBlock.tsx | 8 +++++++- src/renderer/src/providers/AiProvider/OpenAIProvider.ts | 9 +++------ 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/src/renderer/src/pages/home/Messages/Blocks/MainTextBlock.tsx b/src/renderer/src/pages/home/Messages/Blocks/MainTextBlock.tsx index b52449d4a7..0805e18fd9 100644 --- a/src/renderer/src/pages/home/Messages/Blocks/MainTextBlock.tsx +++ b/src/renderer/src/pages/home/Messages/Blocks/MainTextBlock.tsx @@ -31,6 +31,8 @@ interface Props { role: Message['role'] } +const toolUseRegex = /([\s\S]*?)<\/tool_use>/g + const MainTextBlock: React.FC = ({ block, citationBlockId, role, mentions = [] }) => { // Use the passed citationBlockId directly in the selector const { renderInputMessageAsMarkdown } = useSettings() @@ -67,6 +69,10 @@ const MainTextBlock: React.FC = ({ block, citationBlockId, role, mentions return content }, [block.content, block.citationReferences, citationBlockId, formattedCitations]) + const ignoreToolUse = useMemo(() => { + return processedContent.replace(toolUseRegex, '') + }, [processedContent]) + return ( <> {/* Render mentions associated with the message */} @@ -80,7 +86,7 @@ const MainTextBlock: React.FC = ({ block, citationBlockId, role, mentions {role === 'user' && !renderInputMessageAsMarkdown ? (

{block.content}

) : ( - + )} ) diff --git a/src/renderer/src/providers/AiProvider/OpenAIProvider.ts b/src/renderer/src/providers/AiProvider/OpenAIProvider.ts index 4f7ff8109b..fdfdb7b9d4 100644 --- a/src/renderer/src/providers/AiProvider/OpenAIProvider.ts +++ b/src/renderer/src/providers/AiProvider/OpenAIProvider.ts @@ -507,6 +507,7 @@ export default class OpenAIProvider extends BaseProvider { // let isThinkingInContent: ThoughtProcessor | undefined = undefined // const processThinkingChunk = this.handleThinkingTags() let isFirstChunk = true + let isFirstThinkingChunk = true for await (const chunk of stream) { if (window.keyv.get(EVENT_NAMES.CHAT_COMPLETION_PAUSED)) { break @@ -521,12 +522,7 @@ export default class OpenAIProvider extends BaseProvider { const reasoningContent = delta?.reasoning_content || delta?.reasoning const currentTime = new Date().getTime() // Get current time for each chunk - if ( - time_first_token_millsec === 0 && - isEmpty(reasoningContent) && - isEmpty(delta?.content) && - isEmpty(finishReason) - ) { + if (time_first_token_millsec === 0 && isFirstThinkingChunk && reasoningContent) { // 记录第一个token的时间 time_first_token_millsec = currentTime // 记录第一个token的时间差 @@ -542,6 +538,7 @@ export default class OpenAIProvider extends BaseProvider { fractionalSecondDigits: 3 })}` ) + isFirstThinkingChunk = false } if (reasoningContent) { thinkingContent += reasoningContent