continue-custom/gui/src/components/StepContainer/StepContainer.tsx
Jazzcort d130fef631 Feat: adding a warning message system and implementing compile chat mechanism
This feature include the following changes:
  - Implemented llm/compileChat that gives the webview the ability to precompile the chat messages
    before sending them to llm/streamChat where the webview can gather the pruning information and
    provide users better guides.

  - Added a optional parameter to llm/streamChat that can be used to indicate whether the chat
    messages being passed is procompiled or not, so we don't compile the chat messages twice.
    Because it's an optional parameter, we don't affect the behavior of llm/stream when it's
    called at other places.

  - Added a warning message box for the last ContinueInputBox which will display the warning
    messages and also provide some useful guides.

https://github.com/Granite-Code/granite-code/issues/22
2025-07-16 09:09:26 -04:00

121 lines
3.7 KiB
TypeScript

import { ChatHistoryItem } from "core";
import { renderChatMessage, stripImages } from "core/util/messageContent";
import { useEffect, useState } from "react";
import { useDispatch } from "react-redux";
import { useAppSelector } from "../../redux/hooks";
import { selectUIConfig } from "../../redux/slices/configSlice";
import { deleteMessage } from "../../redux/slices/sessionSlice";
import StyledMarkdownPreview from "../StyledMarkdownPreview";
import Reasoning from "./Reasoning";
import ResponseActions from "./ResponseActions";
import ThinkingIndicator from "./ThinkingIndicator";
interface StepContainerProps {
item: ChatHistoryItem;
index: number;
isLast: boolean;
}
export default function StepContainer(props: StepContainerProps) {
const dispatch = useDispatch();
const [isTruncated, setIsTruncated] = useState(false);
const isStreaming = useAppSelector((state) => state.session.isStreaming);
const historyItemAfterThis = useAppSelector(
(state) => state.session.history[props.index + 1],
);
const warningMessage = useAppSelector(
(state) => state.session.warningMessage,
);
const uiConfig = useAppSelector(selectUIConfig);
const isNextMsgAssistantOrThinking =
historyItemAfterThis?.message.role === "assistant" ||
historyItemAfterThis?.message.role === "thinking" ||
historyItemAfterThis?.message.role === "tool";
const shouldRenderResponseAction = () => {
if (isNextMsgAssistantOrThinking) {
return false;
}
if (!historyItemAfterThis) {
return !props.item.toolCallStates;
}
return true;
};
useEffect(() => {
if (!isStreaming) {
const content = renderChatMessage(props.item.message).trim();
const endingPunctuation = [".", "?", "!", "```", ":"];
// If not ending in punctuation or emoji, we assume the response got truncated
if (
content.trim() !== "" &&
!(
endingPunctuation.some((p) => content.endsWith(p)) ||
/\p{Emoji}/u.test(content.slice(-2))
)
) {
setIsTruncated(true);
} else {
setIsTruncated(false);
}
}
}, [props.item.message.content, isStreaming]);
function onDelete() {
dispatch(deleteMessage(props.index));
}
function onContinueGeneration() {
window.postMessage(
{
messageType: "userInput",
data: {
input: "Continue your response exactly where you left off:",
},
},
"*",
);
}
return (
<div>
<div className="bg-background overflow-hidden p-1 px-1.5">
{uiConfig?.displayRawMarkdown ? (
<pre className="text-2xs max-w-full overflow-x-auto whitespace-pre-wrap break-words p-4">
{renderChatMessage(props.item.message)}
</pre>
) : (
<>
<Reasoning {...props} />
<StyledMarkdownPreview
isRenderingInStepContainer
source={stripImages(props.item.message.content)}
itemIndex={props.index}
/>
</>
)}
{props.isLast && <ThinkingIndicator historyItem={props.item} />}
</div>
{shouldRenderResponseAction() && (
// We want to occupy space in the DOM regardless of whether the actions are visible to avoid jank on stream complete
<div className={`mt-2 h-7 transition-opacity duration-300 ease-in-out`}>
{!isStreaming && (
<ResponseActions
isTruncated={isTruncated}
onDelete={onDelete}
onContinueGeneration={onContinueGeneration}
index={props.index}
item={props.item}
isLast={props.isLast}
/>
)}
</div>
)}
</div>
);
}