Elffuss BraidHub

kikocis/continue public

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