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