| 1 | import { BuiltInToolNames } from "core/tools/builtIn"; |
| 2 | import { useContext, useEffect } from "react"; |
| 3 | import { IdeMessengerContext } from "../../../../context/IdeMessenger"; |
| 4 | import { useAppDispatch, useAppSelector } from "../../../../redux/hooks"; |
| 5 | import { |
| 6 | selectFirstPendingToolCall, |
| 7 | selectPendingToolCalls, |
| 8 | selectToolCallsByStatus, |
| 9 | } from "../../../../redux/selectors/selectToolCalls"; |
| 10 | import { cancelToolCall } from "../../../../redux/slices/sessionSlice"; |
| 11 | import { callToolById } from "../../../../redux/thunks/callToolById"; |
| 12 | import { cancelStream } from "../../../../redux/thunks/cancelStream"; |
| 13 | import { logToolUsage } from "../../../../redux/util"; |
| 14 | import { isJetBrains } from "../../../../util"; |
| 15 | import { useMainEditor } from "../../TipTapEditor"; |
| 16 | import { BlockSettingsTopToolbar } from "./BlockSettingsTopToolbar"; |
| 17 | import { EditOutcomeToolbar } from "./EditOutcomeToolbar"; |
| 18 | import { EditToolbar } from "./EditToolbar"; |
| 19 | import { IsApplyingToolbar } from "./IsApplyingToolbar"; |
| 20 | import { PendingApplyStatesToolbar } from "./PendingApplyStatesToolbar"; |
| 21 | import { PendingToolCallToolbar } from "./PendingToolCallToolbar"; |
| 22 | import { StreamingToolbar } from "./StreamingToolbar"; |
| 23 | import { TtsActiveToolbar } from "./TtsActiveToolbar"; |
| 24 | |
| 25 | // Keyboard shortcut detection utilities |
| 26 | const isExecuteToolCallShortcut = (event: KeyboardEvent) => { |
| 27 | const metaKey = event.metaKey || event.ctrlKey; |
| 28 | return metaKey && event.key === "Enter"; |
| 29 | }; |
| 30 | |
| 31 | const isCancelToolCallShortcut = ( |
| 32 | event: KeyboardEvent, |
| 33 | isJetBrains: boolean, |
| 34 | ) => { |
| 35 | const metaKey = event.metaKey || event.ctrlKey; |
| 36 | const altKey = event.altKey; |
| 37 | const modifierKey = isJetBrains ? altKey : metaKey; |
| 38 | return modifierKey && event.key === "Backspace"; |
| 39 | }; |
| 40 | |
| 41 | // Check if a tool call is a terminal command |
| 42 | const isTerminalCommand = (toolCallState: any) => { |
| 43 | return ( |
| 44 | toolCallState?.toolCall?.function?.name === |
| 45 | BuiltInToolNames.RunTerminalCommand |
| 46 | ); |
| 47 | }; |
| 48 | |
| 49 | export function LumpToolbar() { |
| 50 | const dispatch = useAppDispatch(); |
| 51 | const ideMessenger = useContext(IdeMessengerContext); |
| 52 | const ttsActive = useAppSelector((state) => state.ui.ttsActive); |
| 53 | const isStreaming = useAppSelector((state) => state.session.isStreaming); |
| 54 | const isInEdit = useAppSelector((state) => state.session.isInEdit); |
| 55 | const jetbrains = isJetBrains(); |
| 56 | const pendingToolCalls = useAppSelector(selectPendingToolCalls); |
| 57 | const firstPendingToolCall = useAppSelector(selectFirstPendingToolCall); |
| 58 | const editApplyState = useAppSelector( |
| 59 | (state) => state.editModeState.applyState, |
| 60 | ); |
| 61 | const applyStates = useAppSelector( |
| 62 | (state) => state.session.codeBlockApplyStates.states, |
| 63 | ); |
| 64 | const pendingApplyStates = applyStates.filter( |
| 65 | (state) => state.status === "done", |
| 66 | ); |
| 67 | const isApplying = applyStates.some((state) => state.status === "streaming"); |
| 68 | const editor = useMainEditor(); |
| 69 | |
| 70 | // Get ALL running terminal commands |
| 71 | const runningToolCalls = useAppSelector((state) => |
| 72 | selectToolCallsByStatus(state, "calling"), |
| 73 | ); |
| 74 | const runningTerminalCalls = runningToolCalls.filter(isTerminalCommand); |
| 75 | const hasRunningTerminalCommand = runningTerminalCalls.length > 0; |
| 76 | |
| 77 | // Simple handler: stop ALL running terminal commands |
| 78 | const handleStopAllTerminalCommands = async () => { |
| 79 | if (runningTerminalCalls.length === 0) { |
| 80 | return; |
| 81 | } |
| 82 | |
| 83 | // Stop all terminal commands concurrently |
| 84 | const stopPromises = runningTerminalCalls.map(async (terminalCall) => { |
| 85 | try { |
| 86 | // Cancel the process on the backend |
| 87 | await ideMessenger.request("process/killTerminalProcess", { |
| 88 | toolCallId: terminalCall.toolCallId, |
| 89 | }); |
| 90 | |
| 91 | // Cancel the tool call in the UI |
| 92 | dispatch( |
| 93 | cancelToolCall({ |
| 94 | toolCallId: terminalCall.toolCallId, |
| 95 | }), |
| 96 | ); |
| 97 | |
| 98 | logToolUsage(terminalCall, false, true, ideMessenger); |
| 99 | } catch (error) { |
| 100 | console.error( |
| 101 | `Failed to cancel terminal command ${terminalCall.toolCallId}:`, |
| 102 | error, |
| 103 | ); |
| 104 | } |
| 105 | }); |
| 106 | |
| 107 | // Wait for all cancellations to complete |
| 108 | await Promise.all(stopPromises); |
| 109 | }; |
| 110 | |
| 111 | // Combined stop handler |
| 112 | const handleStopAction = async () => { |
| 113 | // Stop all terminal commands if any are running |
| 114 | if (hasRunningTerminalCommand) { |
| 115 | await handleStopAllTerminalCommands(); |
| 116 | } |
| 117 | |
| 118 | // Also stop regular streaming if it's happening |
| 119 | if (isStreaming) { |
| 120 | dispatch(cancelStream()); |
| 121 | } |
| 122 | }; |
| 123 | |
| 124 | useEffect(() => { |
| 125 | if (!firstPendingToolCall && !hasRunningTerminalCommand) { |
| 126 | return; |
| 127 | } |
| 128 | |
| 129 | const handleToolCallKeyboardShortcuts = (event: KeyboardEvent) => { |
| 130 | if (isExecuteToolCallShortcut(event) && firstPendingToolCall) { |
| 131 | event.preventDefault(); |
| 132 | event.stopPropagation(); |
| 133 | |
| 134 | void dispatch( |
| 135 | callToolById({ toolCallId: firstPendingToolCall.toolCallId }), |
| 136 | ); |
| 137 | } else if (isCancelToolCallShortcut(event, jetbrains)) { |
| 138 | event.preventDefault(); |
| 139 | event.stopPropagation(); |
| 140 | |
| 141 | if (hasRunningTerminalCommand) { |
| 142 | // Stop running terminal commands |
| 143 | void handleStopAction(); |
| 144 | } else if (firstPendingToolCall) { |
| 145 | // Cancel pending tool call. If last call, focus editor |
| 146 | if (pendingToolCalls.length === 1) { |
| 147 | editor.mainEditor?.commands.focus(); |
| 148 | } |
| 149 | void dispatch( |
| 150 | cancelToolCall({ |
| 151 | toolCallId: firstPendingToolCall.toolCallId, |
| 152 | }), |
| 153 | ); |
| 154 | logToolUsage(firstPendingToolCall, false, true, ideMessenger); |
| 155 | } |
| 156 | } |
| 157 | }; |
| 158 | |
| 159 | document.addEventListener("keydown", handleToolCallKeyboardShortcuts); |
| 160 | return () => { |
| 161 | document.removeEventListener("keydown", handleToolCallKeyboardShortcuts); |
| 162 | }; |
| 163 | }, [ |
| 164 | firstPendingToolCall, |
| 165 | pendingToolCalls, |
| 166 | editor, |
| 167 | hasRunningTerminalCommand, |
| 168 | runningTerminalCalls, |
| 169 | ]); |
| 170 | |
| 171 | if (isApplying) { |
| 172 | return <IsApplyingToolbar />; |
| 173 | } |
| 174 | |
| 175 | if (isInEdit) { |
| 176 | if (editApplyState.status === "done") { |
| 177 | return <EditOutcomeToolbar />; |
| 178 | } |
| 179 | |
| 180 | return <EditToolbar />; |
| 181 | } |
| 182 | |
| 183 | if (ttsActive) { |
| 184 | return <TtsActiveToolbar />; |
| 185 | } |
| 186 | |
| 187 | // Only show terminal streaming for actual terminal commands |
| 188 | if (hasRunningTerminalCommand) { |
| 189 | const count = runningTerminalCalls.length; |
| 190 | const stopText = `Stop Terminal${count > 1 ? ` (${count})` : ""}`; |
| 191 | return ( |
| 192 | <StreamingToolbar onStop={handleStopAction} displayText={stopText} /> |
| 193 | ); |
| 194 | } |
| 195 | |
| 196 | // Regular streaming (non-terminal) |
| 197 | if (isStreaming) { |
| 198 | return <StreamingToolbar onStop={() => dispatch(cancelStream())} />; |
| 199 | } |
| 200 | |
| 201 | if (pendingToolCalls.length > 0) { |
| 202 | return <PendingToolCallToolbar />; |
| 203 | } |
| 204 | |
| 205 | if (pendingApplyStates.length > 0) { |
| 206 | return ( |
| 207 | <PendingApplyStatesToolbar pendingApplyStates={pendingApplyStates} /> |
| 208 | ); |
| 209 | } |
| 210 | |
| 211 | return <BlockSettingsTopToolbar />; |
| 212 | } |