| 1 | import { ChatHistoryItem } from "core"; |
| 2 | import { renderChatMessage, stripImages } from "core/util/messageContent"; |
| 3 | import { useEffect, useState } from "react"; |
| 4 | import { useDispatch } from "react-redux"; |
| 5 | import styled from "styled-components"; |
| 6 | import { vscBackground } from ".."; |
| 7 | import { useAppSelector } from "../../redux/hooks"; |
| 8 | import { selectUIConfig } from "../../redux/slices/configSlice"; |
| 9 | import { deleteMessage } from "../../redux/slices/sessionSlice"; |
| 10 | import { getFontSize } from "../../util"; |
| 11 | import StyledMarkdownPreview from "../StyledMarkdownPreview"; |
| 12 | import Reasoning from "./Reasoning"; |
| 13 | import ResponseActions from "./ResponseActions"; |
| 14 | import ThinkingIndicator from "./ThinkingIndicator"; |
| 15 | |
| 16 | interface StepContainerProps { |
| 17 | item: ChatHistoryItem; |
| 18 | index: number; |
| 19 | isLast: boolean; |
| 20 | } |
| 21 | |
| 22 | const ContentDiv = styled.div<{ fontSize?: number }>` |
| 23 | padding: 4px; |
| 24 | padding-left: 6px; |
| 25 | padding-right: 6px; |
| 26 | |
| 27 | background-color: ${vscBackground}; |
| 28 | font-size: ${getFontSize()}px; |
| 29 | overflow: hidden; |
| 30 | `; |
| 31 | |
| 32 | export default function StepContainer(props: StepContainerProps) { |
| 33 | const dispatch = useDispatch(); |
| 34 | const [isTruncated, setIsTruncated] = useState(false); |
| 35 | const isStreaming = useAppSelector((state) => state.session.isStreaming); |
| 36 | const historyItemAfterThis = useAppSelector( |
| 37 | (state) => state.session.history[props.index + 1], |
| 38 | ); |
| 39 | const uiConfig = useAppSelector(selectUIConfig); |
| 40 | |
| 41 | const hideActionSpace = |
| 42 | historyItemAfterThis?.message.role === "assistant" || |
| 43 | historyItemAfterThis?.message.role === "thinking"; |
| 44 | const hideActions = hideActionSpace || (isStreaming && props.isLast); |
| 45 | |
| 46 | useEffect(() => { |
| 47 | if (!isStreaming) { |
| 48 | const content = renderChatMessage(props.item.message).trim(); |
| 49 | const endingPunctuation = [".", "?", "!", "```", ":"]; |
| 50 | |
| 51 | // If not ending in punctuation or emoji, we assume the response got truncated |
| 52 | if ( |
| 53 | content.trim() !== "" && |
| 54 | !( |
| 55 | endingPunctuation.some((p) => content.endsWith(p)) || |
| 56 | /\p{Emoji}/u.test(content.slice(-2)) |
| 57 | ) |
| 58 | ) { |
| 59 | setIsTruncated(true); |
| 60 | } else { |
| 61 | setIsTruncated(false); |
| 62 | } |
| 63 | } |
| 64 | }, [props.item.message.content, isStreaming]); |
| 65 | |
| 66 | function onDelete() { |
| 67 | dispatch(deleteMessage(props.index)); |
| 68 | } |
| 69 | |
| 70 | function onContinueGeneration() { |
| 71 | window.postMessage( |
| 72 | { |
| 73 | messageType: "userInput", |
| 74 | data: { |
| 75 | input: "Continue your response exactly where you left off:", |
| 76 | }, |
| 77 | }, |
| 78 | "*", |
| 79 | ); |
| 80 | } |
| 81 | |
| 82 | return ( |
| 83 | <div> |
| 84 | <ContentDiv> |
| 85 | {uiConfig?.displayRawMarkdown ? ( |
| 86 | <pre |
| 87 | className="max-w-full overflow-x-auto whitespace-pre-wrap break-words p-4" |
| 88 | style={{ fontSize: getFontSize() - 2 }} |
| 89 | > |
| 90 | {renderChatMessage(props.item.message)} |
| 91 | </pre> |
| 92 | ) : ( |
| 93 | <> |
| 94 | <Reasoning {...props} /> |
| 95 | |
| 96 | <StyledMarkdownPreview |
| 97 | isRenderingInStepContainer |
| 98 | source={stripImages(props.item.message.content)} |
| 99 | itemIndex={props.index} |
| 100 | /> |
| 101 | </> |
| 102 | )} |
| 103 | {props.isLast && <ThinkingIndicator historyItem={props.item} />} |
| 104 | </ContentDiv> |
| 105 | {/* We want to occupy space in the DOM regardless of whether the actions are visible to avoid jank on stream complete */} |
| 106 | {!hideActionSpace && ( |
| 107 | <div className={`mt-2 h-7 transition-opacity duration-300 ease-in-out`}> |
| 108 | {!hideActions && ( |
| 109 | <ResponseActions |
| 110 | isTruncated={isTruncated} |
| 111 | onDelete={onDelete} |
| 112 | onContinueGeneration={onContinueGeneration} |
| 113 | index={props.index} |
| 114 | item={props.item} |
| 115 | isLast={props.isLast} |
| 116 | /> |
| 117 | )} |
| 118 | </div> |
| 119 | )} |
| 120 | </div> |
| 121 | ); |
| 122 | } |