Elffuss BraidHub

kikocis/continue public

60acd6c5ca13 · 4.6 KiBRawPermalink
1import { ChatHistoryItem } from "core";
2import { renderChatMessage, stripImages } from "core/util/messageContent";
3import { useEffect, useState } from "react";
4import { useDispatch } from "react-redux";
5import { useAppSelector } from "../../redux/hooks";
6import { selectUIConfig } from "../../redux/slices/configSlice";
7import { deleteMessage } from "../../redux/slices/sessionSlice";
8import StyledMarkdownPreview from "../StyledMarkdownPreview";
9import ConversationSummary from "./ConversationSummary";
10import Reasoning from "./Reasoning";
11import ResponseActions from "./ResponseActions";
12import ThinkingIndicator from "./ThinkingIndicator";
13
14interface StepContainerProps {
15 item: ChatHistoryItem;
16 index: number;
17 isLast: boolean;
18 latestSummaryIndex?: number;
19}
20
21export 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() && !isStreaming && (
114 <div
115 className={`mt-2 h-7 transition-opacity duration-300 ease-in-out ${isBeforeLatestSummary ? "opacity-35" : ""}`}
116 >
117 <ResponseActions
118 isTruncated={isTruncated}
119 onDelete={onDelete}
120 onContinueGeneration={onContinueGeneration}
121 index={props.index}
122 item={props.item}
123 isLast={props.isLast}
124 />
125 </div>
126 )}
127
128 {/* Show compaction indicator for the latest summary */}
129 {isLatestSummary && (
130 <div className="mx-1.5 my-5">
131 <div className="flex items-center">
132 <div className="border-border flex-1 border-t border-solid"></div>
133 <span className="text-description mx-3 text-xs">
134 Previous Conversation Compacted
135 </span>
136 <div className="border-border flex-1 border-t border-solid"></div>
137 </div>
138 </div>
139 )}
140
141 {/* ConversationSummary is outside the dimmed container so it's always at full opacity */}
142 <ConversationSummary item={props.item} index={props.index} />
143 </div>
144 );
145}