Elffuss BraidHub

kikocis/continue public

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