| 1 | import { useAppDispatch, useAppSelector } from "../../../../redux/hooks"; |
| 2 | import { selectPendingToolCalls } from "../../../../redux/selectors/selectToolCalls"; |
| 3 | import { callToolById } from "../../../../redux/thunks/callToolById"; |
| 4 | import { cancelToolCallThunk } from "../../../../redux/thunks/cancelToolCall"; |
| 5 | import { getAltKeyLabel, getMetaKeyLabel, isJetBrains } from "../../../../util"; |
| 6 | import { Button } from "../../../ui"; |
| 7 | import { useMainEditor } from "../../TipTapEditor"; |
| 8 | |
| 9 | export const generateToolCallButtonTestId = ( |
| 10 | action: "accept" | "reject", |
| 11 | toolCallId: string, |
| 12 | ) => { |
| 13 | return `${action}-tool-call-button-${toolCallId}`; |
| 14 | }; |
| 15 | |
| 16 | export function PendingToolCallToolbar() { |
| 17 | const dispatch = useAppDispatch(); |
| 18 | const jetbrains = isJetBrains(); |
| 19 | const pendingToolCalls = useAppSelector(selectPendingToolCalls); |
| 20 | const editor = useMainEditor(); |
| 21 | |
| 22 | if (pendingToolCalls.length === 0) { |
| 23 | return null; |
| 24 | } |
| 25 | |
| 26 | const handleAccept = (toolCallId: string) => { |
| 27 | void dispatch(callToolById({ toolCallId })); |
| 28 | }; |
| 29 | |
| 30 | const handleReject = (toolCallId: string) => { |
| 31 | // put cursor in editor after last rejection |
| 32 | if (pendingToolCalls.length === 1) { |
| 33 | editor.mainEditor?.commands.focus(); |
| 34 | } |
| 35 | void dispatch(cancelToolCallThunk({ toolCallId })); |
| 36 | }; |
| 37 | |
| 38 | return ( |
| 39 | <div className="flex w-full flex-col pb-0.5"> |
| 40 | {pendingToolCalls.map((toolCall, index) => ( |
| 41 | <div |
| 42 | key={toolCall.toolCallId} |
| 43 | className="border-input bg-input flex items-center gap-2 rounded border" |
| 44 | > |
| 45 | <span className="text-description flex-1 truncate text-xs italic"> |
| 46 | {toolCall.tool?.displayTitle ?? toolCall.toolCall.function.name} |
| 47 | </span> |
| 48 | |
| 49 | <div className="flex items-center gap-1"> |
| 50 | <Button |
| 51 | variant="ghost" |
| 52 | size="sm" |
| 53 | className="text-description-muted my-1 font-medium" |
| 54 | onClick={() => handleReject(toolCall.toolCallId)} |
| 55 | data-testid={generateToolCallButtonTestId( |
| 56 | "reject", |
| 57 | toolCall.toolCallId, |
| 58 | )} |
| 59 | > |
| 60 | {/* JetBrains overrides cmd+backspace, so we have to use another shortcut */} |
| 61 | {index === 0 && ( |
| 62 | <span className="text-2xs mr-1"> |
| 63 | {jetbrains ? getAltKeyLabel() : getMetaKeyLabel()}⌫ |
| 64 | </span> |
| 65 | )} |
| 66 | <span>Reject</span> |
| 67 | </Button> |
| 68 | |
| 69 | <Button |
| 70 | variant="primary" |
| 71 | size="sm" |
| 72 | className="my-1 font-medium" |
| 73 | onClick={() => handleAccept(toolCall.toolCallId)} |
| 74 | data-testid={generateToolCallButtonTestId( |
| 75 | "accept", |
| 76 | toolCall.toolCallId, |
| 77 | )} |
| 78 | > |
| 79 | {index === 0 && ( |
| 80 | <span className="text-2xs mr-1">{getMetaKeyLabel()}⏎</span> |
| 81 | )} |
| 82 | <span>Accept</span> |
| 83 | </Button> |
| 84 | </div> |
| 85 | </div> |
| 86 | ))} |
| 87 | </div> |
| 88 | ); |
| 89 | } |