From 5ffc58d6ca3473b958185e312abecfbde0d63c62 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=9D=9E=E6=B3=95=E6=93=8D=E4=BD=9C?= Date: Mon, 10 Feb 2025 14:08:17 +0800 Subject: [PATCH] feat: improve think content display (#13431) --- .../base/markdown-blocks/think-block.tsx | 98 +++++++++++++++++++ web/app/components/base/markdown.tsx | 8 +- web/i18n/en-US/common.ts | 2 + web/i18n/zh-Hans/common.ts | 2 + 4 files changed, 106 insertions(+), 4 deletions(-) create mode 100644 web/app/components/base/markdown-blocks/think-block.tsx diff --git a/web/app/components/base/markdown-blocks/think-block.tsx b/web/app/components/base/markdown-blocks/think-block.tsx new file mode 100644 index 0000000000..fbe453db86 --- /dev/null +++ b/web/app/components/base/markdown-blocks/think-block.tsx @@ -0,0 +1,98 @@ +import React, { useEffect, useRef, useState } from 'react' +import { useTranslation } from 'react-i18next' + +const hasEndThink = (children: any): boolean => { + if (typeof children === 'string') + return children.includes('[ENDTHINKFLAG]') + + if (Array.isArray(children)) + return children.some(child => hasEndThink(child)) + + if (children?.props?.children) + return hasEndThink(children.props.children) + + return false +} + +const removeEndThink = (children: any): any => { + if (typeof children === 'string') + return children.replace('[ENDTHINKFLAG]', '') + + if (Array.isArray(children)) + return children.map(child => removeEndThink(child)) + + if (children?.props?.children) { + return React.cloneElement( + children, + { + ...children.props, + children: removeEndThink(children.props.children), + }, + ) + } + + return children +} + +const useThinkTimer = (children: any) => { + const [startTime] = useState(Date.now()) + const [elapsedTime, setElapsedTime] = useState(0) + const [isComplete, setIsComplete] = useState(false) + const timerRef = useRef() + + useEffect(() => { + timerRef.current = setInterval(() => { + if (!isComplete) + setElapsedTime(Math.floor((Date.now() - startTime) / 100) / 10) + }, 100) + + return () => { + if (timerRef.current) + clearInterval(timerRef.current) + } + }, [startTime, isComplete]) + + useEffect(() => { + if (hasEndThink(children)) { + setIsComplete(true) + if (timerRef.current) + clearInterval(timerRef.current) + } + }, [children]) + + return { elapsedTime, isComplete } +} + +export const ThinkBlock = ({ children, ...props }: any) => { + const { elapsedTime, isComplete } = useThinkTimer(children) + const displayContent = removeEndThink(children) + const { t } = useTranslation() + + return ( +
+ +
+ + + + {isComplete ? `${t('common.chat.thought')}(${elapsedTime.toFixed(1)}s)` : `${t('common.chat.thinking')}(${elapsedTime.toFixed(1)}s)`} +
+
+
+ {displayContent} +
+
+ ) +} + +export default ThinkBlock diff --git a/web/app/components/base/markdown.tsx b/web/app/components/base/markdown.tsx index b98df4b7b2..fc2e4184db 100644 --- a/web/app/components/base/markdown.tsx +++ b/web/app/components/base/markdown.tsx @@ -22,6 +22,7 @@ import AudioGallery from '@/app/components/base/audio-gallery' import SVGRenderer from '@/app/components/base/svg-gallery' import MarkdownButton from '@/app/components/base/markdown-blocks/button' import MarkdownForm from '@/app/components/base/markdown-blocks/form' +import ThinkBlock from '@/app/components/base/markdown-blocks/think-block' // Available language https://github.com/react-syntax-highlighter/react-syntax-highlighter/blob/master/AVAILABLE_LANGUAGES_HLJS.MD const capitalizationLanguageNameMap: Record = { @@ -72,10 +73,8 @@ const preprocessThinkTag = (content: string) => { return content return flow([ - (str: string) => str.replace('\n', '
Thinking
\n'), - (str: string) => str.includes('\n') - ? str.replace('\n', '\n
') - : `${str}\n`, + (str: string) => str.replace('\n', '
\n'), + (str: string) => str.replace('\n', '\n[ENDTHINKFLAG]
'), ])(content) } @@ -281,6 +280,7 @@ export function Markdown(props: { content: string; className?: string }) { button: MarkdownButton, form: MarkdownForm, script: ScriptBlock, + details: ThinkBlock, }} linkTarget='_blank' > diff --git a/web/i18n/en-US/common.ts b/web/i18n/en-US/common.ts index 822c4030fc..b66b1d1cb4 100644 --- a/web/i18n/en-US/common.ts +++ b/web/i18n/en-US/common.ts @@ -531,6 +531,8 @@ const translation = { hitScore: 'Retrieval Score:', }, inputPlaceholder: 'Talk to Bot', + thinking: 'Thinking...', + thought: 'Thought', }, promptEditor: { placeholder: 'Write your prompt word here, enter \'{\' to insert a variable, enter \'/\' to insert a prompt content block', diff --git a/web/i18n/zh-Hans/common.ts b/web/i18n/zh-Hans/common.ts index 1022155c89..ea5d82b73e 100644 --- a/web/i18n/zh-Hans/common.ts +++ b/web/i18n/zh-Hans/common.ts @@ -531,6 +531,8 @@ const translation = { hitScore: '召回得分:', }, inputPlaceholder: '和机器人聊天', + thinking: '深度思考中...', + thought: '已深度思考', }, promptEditor: { placeholder: '在这里写你的提示词,输入\'{\' 插入变量、输入\'/\' 插入提示内容块',