| 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 { useAppSelector } from "../../redux/hooks"; |
| 6 | import { selectUIConfig } from "../../redux/slices/configSlice"; |
| 7 | import { deleteMessage } from "../../redux/slices/sessionSlice"; |
| 8 | import StyledMarkdownPreview from "../StyledMarkdownPreview"; |
| 9 | import ConversationSummary from "./ConversationSummary"; |
| 10 | import Reasoning from "./Reasoning"; |
| 11 | import ResponseActions from "./ResponseActions"; |
| 12 | import ThinkingIndicator from "./ThinkingIndicator"; |
| 13 | |
| 14 | interface StepContainerProps { |
| 15 | item: ChatHistoryItem; |
| 16 | index: number; |
| 17 | isLast: boolean; |
| 18 | latestSummaryIndex?: number; |
| 19 | } |
| 20 | |
| 21 | export default function StepContainer(props: StepContainerProps) { |
| 22 | const dispatch = useDispatch(); |
| 23 | const [isTruncated, setIsTruncated] = useState(false); |
| 24 | const isStreaming = useAppSelector((state) => state.session.isStreaming); |
| 25 | const historyItemAfterThis = useAppSelector( |
| 26 | (state) => state.session.history[props.index + 1], |
| 27 | ); |
| 28 | const uiConfig = useAppSelector(selectUIConfig); |
| 29 | |
| 30 | // Calculate dimming and indicator state based on latest summary index |
| 31 | const latestSummaryIndex = props.latestSummaryIndex ?? -1; |
| 32 | const isBeforeLatestSummary = |
| 33 | latestSummaryIndex !== -1 && props.index <= latestSummaryIndex; |
| 34 | const isLatestSummary = |
| 35 | latestSummaryIndex !== -1 && props.index === latestSummaryIndex; |
| 36 | |
| 37 | const isNextMsgAssistantOrThinking = |
| 38 | historyItemAfterThis?.message.role === "assistant" || |
| 39 | historyItemAfterThis?.message.role === "thinking" || |
| 40 | historyItemAfterThis?.message.role === "tool"; |
| 41 | |
| 42 | const shouldRenderResponseAction = () => { |
| 43 | if (isNextMsgAssistantOrThinking) { |
| 44 | return false; |
| 45 | } |
| 46 | |
| 47 | if (!historyItemAfterThis) { |
| 48 | return !props.item.toolCallStates; |
| 49 | } |
| 50 | |
| 51 | return true; |
| 52 | }; |
| 53 | |
| 54 | useEffect(() => { |
| 55 | if (!isStreaming) { |
| 56 | const content = renderChatMessage(props.item.message).trim(); |
| 57 | const endingPunctuation = [".", "?", "!", "```", ":"]; |
| 58 | |
| 59 | // If not ending in punctuation or emoji, we assume the response got truncated |
| 60 | if ( |
| 61 | content.trim() !== "" && |
| 62 | !( |
| 63 | endingPunctuation.some((p) => content.endsWith(p)) || |
| 64 | /\p{Emoji}/u.test(content.slice(-2)) |
| 65 | ) |
| 66 | ) { |
| 67 | setIsTruncated(true); |
| 68 | } else { |
| 69 | setIsTruncated(false); |
| 70 | } |
| 71 | } |
| 72 | }, [props.item.message.content, isStreaming]); |
| 73 | |
| 74 | function onDelete() { |
| 75 | dispatch(deleteMessage(props.index)); |
| 76 | } |
| 77 | |
| 78 | function onContinueGeneration() { |
| 79 | window.postMessage( |
| 80 | { |
| 81 | messageType: "userInput", |
| 82 | data: { |
| 83 | input: "Continue your response exactly where you left off:", |
| 84 | }, |
| 85 | }, |
| 86 | "*", |
| 87 | ); |
| 88 | } |
| 89 | |
| 90 | return ( |
| 91 | <div> |
| 92 | <div |
| 93 | className={`bg-background p-1 px-1.5 ${isBeforeLatestSummary ? "opacity-35" : ""}`} |
| 94 | > |
| 95 | {uiConfig?.displayRawMarkdown ? ( |
| 96 | <pre className="text-2xs max-w-full overflow-x-auto whitespace-pre-wrap break-words p-4"> |
| 97 | {renderChatMessage(props.item.message)} |
| 98 | </pre> |
| 99 | ) : ( |
| 100 | <> |
| 101 | <Reasoning {...props} /> |
| 102 | |
| 103 | <StyledMarkdownPreview |
| 104 | isRenderingInStepContainer |
| 105 | source={stripImages(props.item.message.content)} |
| 106 | itemIndex={props.index} |
| 107 | /> |
| 108 | </> |
| 109 | )} |
| 110 | {props.isLast && <ThinkingIndicator historyItem={props.item} />} |
| 111 | </div> |
| 112 | |
| 113 | {shouldRenderResponseAction() && ( |
| 114 | // We want to occupy space in the DOM regardless of whether the actions are visible to avoid jank on stream complete |
| 115 | <div |
| 116 | className={`mt-2 h-7 transition-opacity duration-300 ease-in-out ${isBeforeLatestSummary ? "opacity-35" : ""}`} |
| 117 | > |
| 118 | {!isStreaming && ( |
| 119 | <ResponseActions |
| 120 | isTruncated={isTruncated} |
| 121 | onDelete={onDelete} |
| 122 | onContinueGeneration={onContinueGeneration} |
| 123 | index={props.index} |
| 124 | item={props.item} |
| 125 | isLast={props.isLast} |
| 126 | /> |
| 127 | )} |
| 128 | </div> |
| 129 | )} |
| 130 | |
| 131 | {/* Show compaction indicator for the latest summary */} |
| 132 | {isLatestSummary && ( |
| 133 | <div className="mx-1.5 my-5"> |
| 134 | <div className="flex items-center"> |
| 135 | <div className="border-border flex-1 border-t border-solid"></div> |
| 136 | <span className="text-description mx-3 text-xs"> |
| 137 | Previous Conversation Compacted |
| 138 | </span> |
| 139 | <div className="border-border flex-1 border-t border-solid"></div> |
| 140 | </div> |
| 141 | </div> |
| 142 | )} |
| 143 | |
| 144 | {/* ConversationSummary is outside the dimmed container so it's always at full opacity */} |
| 145 | <ConversationSummary item={props.item} index={props.index} /> |
| 146 | </div> |
| 147 | ); |
| 148 | } |