| 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 uiConfig = useAppSelector(selectUIConfig); |
| 26 | |
| 27 | // Calculate dimming and indicator state based on latest summary index |
| 28 | const latestSummaryIndex = props.latestSummaryIndex ?? -1; |
| 29 | const isBeforeLatestSummary = |
| 30 | latestSummaryIndex !== -1 && props.index <= latestSummaryIndex; |
| 31 | const isLatestSummary = |
| 32 | latestSummaryIndex !== -1 && props.index === latestSummaryIndex; |
| 33 | |
| 34 | const historyItemAfterThis = useAppSelector( |
| 35 | (state) => state.session.history[props.index + 1], |
| 36 | ); |
| 37 | const showResponseActions = |
| 38 | (props.isLast || historyItemAfterThis?.message.role === "user") && |
| 39 | !(props.isLast && (isStreaming || props.item.toolCallStates)); |
| 40 | |
| 41 | useEffect(() => { |
| 42 | if (!isStreaming) { |
| 43 | const content = renderChatMessage(props.item.message).trim(); |
| 44 | const endingPunctuation = [".", "?", "!", "```", ":"]; |
| 45 | |
| 46 | // If not ending in punctuation or emoji, we assume the response got truncated |
| 47 | if ( |
| 48 | content.trim() !== "" && |
| 49 | !( |
| 50 | endingPunctuation.some((p) => content.endsWith(p)) || |
| 51 | /\p{Emoji}/u.test(content.slice(-2)) |
| 52 | ) |
| 53 | ) { |
| 54 | setIsTruncated(true); |
| 55 | } else { |
| 56 | setIsTruncated(false); |
| 57 | } |
| 58 | } |
| 59 | }, [props.item.message.content, isStreaming]); |
| 60 | |
| 61 | function onDelete() { |
| 62 | dispatch(deleteMessage(props.index)); |
| 63 | } |
| 64 | |
| 65 | function onContinueGeneration() { |
| 66 | window.postMessage( |
| 67 | { |
| 68 | messageType: "userInput", |
| 69 | data: { |
| 70 | input: "Continue your response exactly where you left off:", |
| 71 | }, |
| 72 | }, |
| 73 | "*", |
| 74 | ); |
| 75 | } |
| 76 | |
| 77 | return ( |
| 78 | <div> |
| 79 | <div |
| 80 | className={`bg-background p-1 px-1.5 ${isBeforeLatestSummary ? "opacity-35" : ""}`} |
| 81 | > |
| 82 | {uiConfig?.displayRawMarkdown ? ( |
| 83 | <pre className="text-2xs max-w-full overflow-x-auto whitespace-pre-wrap break-words p-4"> |
| 84 | {renderChatMessage(props.item.message)} |
| 85 | </pre> |
| 86 | ) : ( |
| 87 | <> |
| 88 | <Reasoning {...props} /> |
| 89 | |
| 90 | <StyledMarkdownPreview |
| 91 | isRenderingInStepContainer |
| 92 | source={stripImages(props.item.message.content)} |
| 93 | itemIndex={props.index} |
| 94 | /> |
| 95 | </> |
| 96 | )} |
| 97 | {props.isLast && <ThinkingIndicator historyItem={props.item} />} |
| 98 | </div> |
| 99 | |
| 100 | {showResponseActions && ( |
| 101 | <div |
| 102 | className={`mt-2 h-7 transition-opacity duration-300 ease-in-out ${isBeforeLatestSummary || isStreaming ? "opacity-35" : ""} ${isStreaming && "pointer-events-none cursor-not-allowed"}`} |
| 103 | > |
| 104 | <ResponseActions |
| 105 | isTruncated={isTruncated} |
| 106 | onDelete={onDelete} |
| 107 | onContinueGeneration={onContinueGeneration} |
| 108 | index={props.index} |
| 109 | item={props.item} |
| 110 | isLast={props.isLast} |
| 111 | /> |
| 112 | </div> |
| 113 | )} |
| 114 | |
| 115 | {/* Show compaction indicator for the latest summary */} |
| 116 | {isLatestSummary && ( |
| 117 | <div className="mx-1.5 my-5"> |
| 118 | <div className="flex items-center"> |
| 119 | <div className="border-border flex-1 border-t border-solid"></div> |
| 120 | <span className="text-description mx-3 text-xs"> |
| 121 | Previous Conversation Compacted |
| 122 | </span> |
| 123 | <div className="border-border flex-1 border-t border-solid"></div> |
| 124 | </div> |
| 125 | </div> |
| 126 | )} |
| 127 | |
| 128 | {/* ConversationSummary is outside the dimmed container so it's always at full opacity */} |
| 129 | <ConversationSummary item={props.item} index={props.index} /> |
| 130 | </div> |
| 131 | ); |
| 132 | } |