Elffuss BraidHub

kikocis/continue public

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