import { ChatHistoryItem } from "core"; import { renderChatMessage, stripImages } from "core/util/messageContent"; import { useEffect, useState } from "react"; import { useDispatch } from "react-redux"; import { useAppSelector } from "../../redux/hooks"; import { selectUIConfig } from "../../redux/slices/configSlice"; import { deleteMessage } from "../../redux/slices/sessionSlice"; import StyledMarkdownPreview from "../StyledMarkdownPreview"; import Reasoning from "./Reasoning"; import ResponseActions from "./ResponseActions"; import ThinkingIndicator from "./ThinkingIndicator"; interface StepContainerProps { item: ChatHistoryItem; index: number; isLast: boolean; } export default function StepContainer(props: StepContainerProps) { const dispatch = useDispatch(); const [isTruncated, setIsTruncated] = useState(false); const isStreaming = useAppSelector((state) => state.session.isStreaming); const historyItemAfterThis = useAppSelector( (state) => state.session.history[props.index + 1], ); const warningMessage = useAppSelector( (state) => state.session.warningMessage, ); const uiConfig = useAppSelector(selectUIConfig); const isNextMsgAssistantOrThinking = historyItemAfterThis?.message.role === "assistant" || historyItemAfterThis?.message.role === "thinking" || historyItemAfterThis?.message.role === "tool"; const shouldRenderResponseAction = () => { if (isNextMsgAssistantOrThinking) { return false; } if (!historyItemAfterThis) { return !props.item.toolCallStates; } return true; }; useEffect(() => { if (!isStreaming) { const content = renderChatMessage(props.item.message).trim(); const endingPunctuation = [".", "?", "!", "```", ":"]; // If not ending in punctuation or emoji, we assume the response got truncated if ( content.trim() !== "" && !( endingPunctuation.some((p) => content.endsWith(p)) || /\p{Emoji}/u.test(content.slice(-2)) ) ) { setIsTruncated(true); } else { setIsTruncated(false); } } }, [props.item.message.content, isStreaming]); function onDelete() { dispatch(deleteMessage(props.index)); } function onContinueGeneration() { window.postMessage( { messageType: "userInput", data: { input: "Continue your response exactly where you left off:", }, }, "*", ); } return (
{uiConfig?.displayRawMarkdown ? (
            {renderChatMessage(props.item.message)}
          
) : ( <> )} {props.isLast && }
{shouldRenderResponseAction() && ( // We want to occupy space in the DOM regardless of whether the actions are visible to avoid jank on stream complete
{!isStreaming && ( )}
)}
); }