| 1 | import { useContext, useEffect } from "react"; |
| 2 | import { useSelector } from "react-redux"; |
| 3 | import styled from "styled-components"; |
| 4 | import { AnimatedEllipsis } from "../.."; |
| 5 | import { IdeMessengerContext } from "../../../context/IdeMessenger"; |
| 6 | import { useAppDispatch, useAppSelector } from "../../../redux/hooks"; |
| 7 | import { selectCurrentToolCall } from "../../../redux/selectors/selectCurrentToolCall"; |
| 8 | import { callCurrentTool } from "../../../redux/thunks/callCurrentTool"; |
| 9 | import { cancelCurrentToolCall } from "../../../redux/thunks/cancelCurrentToolCall"; |
| 10 | import { cancelStream } from "../../../redux/thunks/cancelStream"; |
| 11 | import { |
| 12 | getAltKeyLabel, |
| 13 | getFontSize, |
| 14 | getMetaKeyLabel, |
| 15 | isJetBrains, |
| 16 | } from "../../../util"; |
| 17 | import { EnterButton } from "../InputToolbar/EnterButton"; |
| 18 | import { BlockSettingsTopToolbar } from "./BlockSettingsTopToolbar"; |
| 19 | |
| 20 | const Container = styled.div` |
| 21 | display: flex; |
| 22 | justify-content: space-between; |
| 23 | align-items: center; |
| 24 | width: 100%; |
| 25 | `; |
| 26 | |
| 27 | const StopButton = styled.div` |
| 28 | font-size: ${getFontSize() - 3}px; |
| 29 | padding: 2px; |
| 30 | padding-right: 4px; |
| 31 | cursor: pointer; |
| 32 | `; |
| 33 | |
| 34 | function GeneratingIndicator() { |
| 35 | return ( |
| 36 | <div className="text-xs text-gray-400"> |
| 37 | <span>Generating</span> |
| 38 | <AnimatedEllipsis /> |
| 39 | </div> |
| 40 | ); |
| 41 | } |
| 42 | |
| 43 | export function LumpToolbar() { |
| 44 | const dispatch = useAppDispatch(); |
| 45 | const ideMessenger = useContext(IdeMessengerContext); |
| 46 | const ttsActive = useAppSelector((state) => state.ui.ttsActive); |
| 47 | const isStreaming = useAppSelector((state) => state.session.isStreaming); |
| 48 | const jetbrains = isJetBrains(); |
| 49 | |
| 50 | const toolCallState = useSelector(selectCurrentToolCall); |
| 51 | |
| 52 | const handleKeyDown = (event: KeyboardEvent) => { |
| 53 | if (toolCallState?.status === "generated") { |
| 54 | const metaKey = event.metaKey || event.ctrlKey; |
| 55 | const altKey = event.altKey; |
| 56 | |
| 57 | if (metaKey && event.key === "Enter") { |
| 58 | event.preventDefault(); |
| 59 | event.stopPropagation(); |
| 60 | dispatch(callCurrentTool()); |
| 61 | } else if ((jetbrains ? altKey : metaKey) && event.key === "Backspace") { |
| 62 | event.preventDefault(); |
| 63 | event.stopPropagation(); |
| 64 | dispatch(cancelCurrentToolCall()); |
| 65 | } |
| 66 | } |
| 67 | }; |
| 68 | |
| 69 | useEffect(() => { |
| 70 | document.addEventListener("keydown", handleKeyDown); |
| 71 | return () => { |
| 72 | document.removeEventListener("keydown", handleKeyDown); |
| 73 | }; |
| 74 | }, [toolCallState]); |
| 75 | |
| 76 | if (ttsActive) { |
| 77 | return ( |
| 78 | <Container> |
| 79 | <GeneratingIndicator /> |
| 80 | <StopButton |
| 81 | className="text-gray-400" |
| 82 | onClick={() => { |
| 83 | ideMessenger.post("tts/kill", undefined); |
| 84 | }} |
| 85 | > |
| 86 | ■ Stop TTS |
| 87 | </StopButton> |
| 88 | </Container> |
| 89 | ); |
| 90 | } |
| 91 | |
| 92 | if (isStreaming) { |
| 93 | return ( |
| 94 | <Container> |
| 95 | <GeneratingIndicator /> |
| 96 | <StopButton |
| 97 | className="text-gray-400" |
| 98 | onClick={() => { |
| 99 | dispatch(cancelStream()); |
| 100 | }} |
| 101 | > |
| 102 | {/* JetBrains overrides cmd+backspace, so we have to use another shortcut */} |
| 103 | {jetbrains ? getAltKeyLabel() : getMetaKeyLabel()} ⌫ Cancel |
| 104 | </StopButton> |
| 105 | </Container> |
| 106 | ); |
| 107 | } |
| 108 | |
| 109 | if (toolCallState?.status === "generated") { |
| 110 | return ( |
| 111 | <Container> |
| 112 | <div className="flex flex-row items-center pb-0.5 pr-1 text-xs text-gray-400"> |
| 113 | <span className="hidden sm:flex">Pending tool call</span> |
| 114 | </div> |
| 115 | |
| 116 | <div className="flex gap-2 pb-0.5"> |
| 117 | <StopButton |
| 118 | className="text-gray-400" |
| 119 | onClick={() => dispatch(cancelCurrentToolCall())} |
| 120 | data-testid="reject-tool-call-button" |
| 121 | > |
| 122 | {/* JetBrains overrides cmd+backspace, so we have to use another shortcut */} |
| 123 | {jetbrains ? getAltKeyLabel() : getMetaKeyLabel()} ⌫ Cancel |
| 124 | </StopButton> |
| 125 | <EnterButton |
| 126 | isPrimary={true} |
| 127 | className="text-gray-400" |
| 128 | onClick={() => dispatch(callCurrentTool())} |
| 129 | data-testid="accept-tool-call-button" |
| 130 | > |
| 131 | {getMetaKeyLabel()} ⏎ Continue |
| 132 | </EnterButton> |
| 133 | </div> |
| 134 | </Container> |
| 135 | ); |
| 136 | } |
| 137 | |
| 138 | return <BlockSettingsTopToolbar />; |
| 139 | } |