| 1 | import { |
| 2 | ArrowsPointingInIcon, |
| 3 | BarsArrowDownIcon, |
| 4 | PencilSquareIcon, |
| 5 | TrashIcon, |
| 6 | } from "@heroicons/react/24/outline"; |
| 7 | import { ChatHistoryItem } from "core"; |
| 8 | import { modelSupportsTools } from "core/llm/autodetect"; |
| 9 | import { renderChatMessage } from "core/util/messageContent"; |
| 10 | import { useContext, useMemo } from "react"; |
| 11 | import { IdeMessengerContext } from "../../context/IdeMessenger"; |
| 12 | import { useAppDispatch, useAppSelector } from "../../redux/hooks"; |
| 13 | import { selectSelectedChatModel } from "../../redux/slices/configSlice"; |
| 14 | import { setCompactionLoading } from "../../redux/slices/sessionSlice"; |
| 15 | import { setDialogMessage, setShowDialog } from "../../redux/slices/uiSlice"; |
| 16 | import { loadSession } from "../../redux/thunks/session"; |
| 17 | import { FeedbackButtons } from "../FeedbackButtons"; |
| 18 | import { GenerateRuleDialog } from "../GenerateRuleDialog"; |
| 19 | import { CopyIconButton } from "../gui/CopyIconButton"; |
| 20 | import HeaderButtonWithToolTip from "../gui/HeaderButtonWithToolTip"; |
| 21 | |
| 22 | export interface ResponseActionsProps { |
| 23 | isTruncated: boolean; |
| 24 | onContinueGeneration: () => void; |
| 25 | index: number; |
| 26 | onDelete: () => void; |
| 27 | item: ChatHistoryItem; |
| 28 | isLast: boolean; |
| 29 | } |
| 30 | |
| 31 | export default function ResponseActions({ |
| 32 | onContinueGeneration, |
| 33 | index, |
| 34 | item, |
| 35 | isTruncated, |
| 36 | onDelete, |
| 37 | isLast, |
| 38 | }: ResponseActionsProps) { |
| 39 | const dispatch = useAppDispatch(); |
| 40 | const ideMessenger = useContext(IdeMessengerContext); |
| 41 | const currentSessionId = useAppSelector((state) => state.session.id); |
| 42 | const selectedModel = useAppSelector(selectSelectedChatModel); |
| 43 | const ruleGenerationSupported = useMemo(() => { |
| 44 | return selectedModel && modelSupportsTools(selectedModel); |
| 45 | }, [selectedModel]); |
| 46 | |
| 47 | const onGenerateRule = () => { |
| 48 | dispatch(setShowDialog(true)); |
| 49 | dispatch(setDialogMessage(<GenerateRuleDialog />)); |
| 50 | }; |
| 51 | |
| 52 | const onCompactConversation = async () => { |
| 53 | if (!currentSessionId) { |
| 54 | return; |
| 55 | } |
| 56 | |
| 57 | try { |
| 58 | // Set loading state |
| 59 | dispatch(setCompactionLoading({ index, loading: true })); |
| 60 | |
| 61 | await ideMessenger.request("conversation/compact", { |
| 62 | index, |
| 63 | sessionId: currentSessionId, |
| 64 | }); |
| 65 | |
| 66 | // Reload the current session to refresh the conversation state |
| 67 | dispatch( |
| 68 | loadSession({ |
| 69 | sessionId: currentSessionId, |
| 70 | saveCurrentSession: false, |
| 71 | }), |
| 72 | ); |
| 73 | } catch (error) { |
| 74 | console.error("Error compacting conversation:", error); |
| 75 | } finally { |
| 76 | // Clear loading state |
| 77 | dispatch(setCompactionLoading({ index, loading: false })); |
| 78 | } |
| 79 | }; |
| 80 | |
| 81 | return ( |
| 82 | <div className="text-description-muted mx-2 flex cursor-default items-center justify-end space-x-1 bg-transparent pb-0 text-xs"> |
| 83 | {isLast && ruleGenerationSupported && ( |
| 84 | <HeaderButtonWithToolTip |
| 85 | tabIndex={-1} |
| 86 | text="Generate rule" |
| 87 | onClick={onGenerateRule} |
| 88 | > |
| 89 | <PencilSquareIcon className="text-description-muted h-3.5 w-3.5" /> |
| 90 | </HeaderButtonWithToolTip> |
| 91 | )} |
| 92 | |
| 93 | {isTruncated && ( |
| 94 | <HeaderButtonWithToolTip |
| 95 | tabIndex={-1} |
| 96 | text="Continue generation" |
| 97 | onClick={onContinueGeneration} |
| 98 | > |
| 99 | <BarsArrowDownIcon className="text-description-muted h-3.5 w-3.5" /> |
| 100 | </HeaderButtonWithToolTip> |
| 101 | )} |
| 102 | |
| 103 | <HeaderButtonWithToolTip |
| 104 | testId={`compact-button-${index}`} |
| 105 | text="Compact conversation" |
| 106 | tabIndex={-1} |
| 107 | onClick={onCompactConversation} |
| 108 | > |
| 109 | <ArrowsPointingInIcon className="text-description-muted h-3.5 w-3.5" /> |
| 110 | </HeaderButtonWithToolTip> |
| 111 | |
| 112 | <HeaderButtonWithToolTip |
| 113 | testId={`delete-button-${index}`} |
| 114 | text="Delete" |
| 115 | tabIndex={-1} |
| 116 | onClick={onDelete} |
| 117 | > |
| 118 | <TrashIcon className="text-description-muted h-3.5 w-3.5" /> |
| 119 | </HeaderButtonWithToolTip> |
| 120 | |
| 121 | <CopyIconButton |
| 122 | tabIndex={-1} |
| 123 | text={renderChatMessage(item.message)} |
| 124 | clipboardIconClassName="h-3.5 w-3.5 text-description-muted" |
| 125 | checkIconClassName="h-3.5 w-3.5 text-success" |
| 126 | /> |
| 127 | |
| 128 | <FeedbackButtons item={item} /> |
| 129 | </div> |
| 130 | ); |
| 131 | } |