import copy from 'copy-to-clipboard'; import { InfoIcon } from 'lucide-react'; import { Tools } from 'librechat-data-provider'; import React, { useRef, useState, useMemo, useEffect } from 'react'; import type { CodeBarProps } from '~/common'; import ResultSwitcher from '~/components/Messages/Content/ResultSwitcher'; import { useToolCallsMapContext, useMessageContext } from '~/Providers'; import { LogContent } from '~/components/Chat/Messages/Content/Parts'; import RunCode from '~/components/Messages/Content/RunCode'; import Clipboard from '~/components/svg/Clipboard'; import CheckMark from '~/components/svg/CheckMark'; import useLocalize from '~/hooks/useLocalize'; import cn from '~/utils/cn'; type CodeBlockProps = Pick< CodeBarProps, 'lang' | 'plugin' | 'error' | 'allowExecution' | 'blockIndex' > & { codeChildren: React.ReactNode; classProp?: string; }; const CodeBar: React.FC = React.memo( ({ lang, error, codeRef, blockIndex, plugin = null, allowExecution = true }) => { const localize = useLocalize(); const [isCopied, setIsCopied] = useState(false); return (
{lang} {plugin === true ? ( ) : (
{allowExecution === true && ( )}
)}
); }, ); const CodeBlock: React.FC = ({ lang, blockIndex, codeChildren, classProp = '', allowExecution = true, plugin = null, error, }) => { const codeRef = useRef(null); const toolCallsMap = useToolCallsMapContext(); const { messageId, partIndex } = useMessageContext(); const key = allowExecution ? `${messageId}_${partIndex ?? 0}_${blockIndex ?? 0}_${Tools.execute_code}` : ''; const [currentIndex, setCurrentIndex] = useState(0); const fetchedToolCalls = toolCallsMap?.[key]; const [toolCalls, setToolCalls] = useState(toolCallsMap?.[key] ?? null); useEffect(() => { if (fetchedToolCalls) { setToolCalls(fetchedToolCalls); setCurrentIndex(fetchedToolCalls.length - 1); } }, [fetchedToolCalls]); const currentToolCall = useMemo(() => toolCalls?.[currentIndex], [toolCalls, currentIndex]); const next = () => { if (!toolCalls) { return; } if (currentIndex < toolCalls.length - 1) { setCurrentIndex(currentIndex + 1); } }; const previous = () => { if (currentIndex > 0) { setCurrentIndex(currentIndex - 1); } }; const isNonCode = !!(plugin === true || error === true); const language = isNonCode ? 'json' : lang; return (
{codeChildren}
{allowExecution === true && toolCalls && toolCalls.length > 0 && ( <>
                
              
{toolCalls.length > 1 && ( )} )}
); }; export default CodeBlock;