From 584d02b92a2949aeb7927cc44c9a82f769dfb2c9 Mon Sep 17 00:00:00 2001 From: ItzCrazyKns <95534749+ItzCrazyKns@users.noreply.github.com> Date: Thu, 20 Mar 2025 10:56:03 +0530 Subject: [PATCH] feat(app): add thinking model support --- src/components/MessageBox.tsx | 38 +++++++++++++++++++++++++++---- src/components/ThinkBox.tsx | 43 +++++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+), 5 deletions(-) create mode 100644 src/components/ThinkBox.tsx diff --git a/src/components/MessageBox.tsx b/src/components/MessageBox.tsx index 499c3d6..4473c3b 100644 --- a/src/components/MessageBox.tsx +++ b/src/components/MessageBox.tsx @@ -12,13 +12,18 @@ import { Layers3, Plus, } from 'lucide-react'; -import Markdown from 'markdown-to-jsx'; +import Markdown, { MarkdownToJSX } from 'markdown-to-jsx'; import Copy from './MessageActions/Copy'; import Rewrite from './MessageActions/Rewrite'; import MessageSources from './MessageSources'; import SearchImages from './SearchImages'; import SearchVideos from './SearchVideos'; import { useSpeech } from 'react-text-to-speech'; +import ThinkBox from './ThinkBox'; + +const ThinkTagProcessor = ({ children }: { children: React.ReactNode }) => { + return ; +}; const MessageBox = ({ message, @@ -44,27 +49,48 @@ const MessageBox = ({ useEffect(() => { const regex = /\[(\d+)\]/g; + let processedMessage = message.content; + + if (message.role === 'assistant' && message.content.includes('')) { + const openThinkTag = processedMessage.match(//g)?.length || 0; + const closeThinkTag = processedMessage.match(/<\/think>/g)?.length || 0; + + if (openThinkTag > closeThinkTag) { + processedMessage += ' '; // The extra is to prevent the the think component from looking bad + } + } if ( message.role === 'assistant' && message?.sources && message.sources.length > 0 ) { - return setParsedMessage( - message.content.replace( + setParsedMessage( + processedMessage.replace( regex, (_, number) => - `${number}`, + `${number}`, ), ); + return; } setSpeechMessage(message.content.replace(regex, '')); - setParsedMessage(message.content); + setParsedMessage(processedMessage); }, [message.content, message.sources, message.role]); const { speechStatus, start, stop } = useSpeech({ text: speechMessage }); + const markdownOverrides: MarkdownToJSX.Options = { + overrides: { + think: { + component: ThinkTagProcessor, + }, + }, + }; + return (
{message.role === 'user' && ( @@ -111,11 +137,13 @@ const MessageBox = ({ Answer
+ {parsedMessage} diff --git a/src/components/ThinkBox.tsx b/src/components/ThinkBox.tsx new file mode 100644 index 0000000..9c6a576 --- /dev/null +++ b/src/components/ThinkBox.tsx @@ -0,0 +1,43 @@ +'use client'; + +import { useState } from 'react'; +import { cn } from '@/lib/utils'; +import { ChevronDown, ChevronUp, BrainCircuit } from 'lucide-react'; + +interface ThinkBoxProps { + content: string; +} + +const ThinkBox = ({ content }: ThinkBoxProps) => { + const [isExpanded, setIsExpanded] = useState(false); + + return ( +
+ + + {isExpanded && ( +
+ {content} +
+ )} +
+ ); +}; + +export default ThinkBox;