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