| 1 | // A dropdown menu for selecting between Chat, Edit, and Agent modes with keyboard shortcuts |
| 2 | import { |
| 3 | ChatBubbleLeftIcon, |
| 4 | CheckIcon, |
| 5 | ChevronDownIcon, |
| 6 | PencilIcon, |
| 7 | SparklesIcon, |
| 8 | } from "@heroicons/react/24/outline"; |
| 9 | import { MessageModes } from "core"; |
| 10 | import { modelSupportsTools } from "core/llm/autodetect"; |
| 11 | import { useCallback, useEffect, useMemo } from "react"; |
| 12 | import styled from "styled-components"; |
| 13 | import { lightGray } from ".."; |
| 14 | import { useAppDispatch, useAppSelector } from "../../redux/hooks"; |
| 15 | import { selectSelectedChatModel } from "../../redux/slices/configSlice"; |
| 16 | import { setMode } from "../../redux/slices/sessionSlice"; |
| 17 | import { enterEditMode, exitEditMode } from "../../redux/thunks/editMode"; |
| 18 | import { getFontSize, getMetaKeyLabel, isJetBrains } from "../../util"; |
| 19 | import { useMainEditor } from "../mainInput/TipTapEditor"; |
| 20 | import { |
| 21 | Listbox, |
| 22 | ListboxButton, |
| 23 | ListboxOption, |
| 24 | ListboxOptions, |
| 25 | } from "../ui/Listbox"; |
| 26 | |
| 27 | const ShortcutText = styled.span` |
| 28 | color: ${lightGray}; |
| 29 | font-size: ${getFontSize() - 3}px; |
| 30 | margin-right: auto; |
| 31 | `; |
| 32 | |
| 33 | function ModeSelect() { |
| 34 | const dispatch = useAppDispatch(); |
| 35 | const mode = useAppSelector((store) => store.session.mode); |
| 36 | const selectedModel = useAppSelector(selectSelectedChatModel); |
| 37 | const agentModeSupported = useMemo(() => { |
| 38 | return selectedModel && modelSupportsTools(selectedModel); |
| 39 | }, [selectedModel]); |
| 40 | const { mainEditor } = useMainEditor(); |
| 41 | const jetbrains = useMemo(() => { |
| 42 | return isJetBrains(); |
| 43 | }, []); |
| 44 | const metaKeyLabel = useMemo(() => { |
| 45 | return getMetaKeyLabel(); |
| 46 | }, []); |
| 47 | |
| 48 | const getModeIcon = (mode: MessageModes) => { |
| 49 | switch (mode) { |
| 50 | case "agent": |
| 51 | return <SparklesIcon className="xs:h-3 xs:w-3 h-3 w-3" />; |
| 52 | case "chat": |
| 53 | return <ChatBubbleLeftIcon className="xs:h-3 xs:w-3 h-3 w-3" />; |
| 54 | case "edit": |
| 55 | return <PencilIcon className="xs:h-3 xs:w-3 h-3 w-3" />; |
| 56 | } |
| 57 | }; |
| 58 | |
| 59 | // Switch to chat mode if agent mode is selected but not supported |
| 60 | useEffect(() => { |
| 61 | if (!selectedModel) { |
| 62 | return; |
| 63 | } |
| 64 | if (mode === "agent" && !agentModeSupported) { |
| 65 | dispatch(setMode("chat")); |
| 66 | } |
| 67 | }, [mode, agentModeSupported, dispatch, selectedModel]); |
| 68 | |
| 69 | const cycleMode = useCallback(async () => { |
| 70 | const modes: MessageModes[] = jetbrains |
| 71 | ? ["chat", "agent"] |
| 72 | : ["chat", "agent", "edit"]; |
| 73 | const currentIndex = modes.indexOf(mode); |
| 74 | const nextMode = modes[(currentIndex + 1) % modes.length]; |
| 75 | |
| 76 | if (mode === "edit") { |
| 77 | await dispatch( |
| 78 | exitEditMode({ |
| 79 | goToMode: nextMode, |
| 80 | }), |
| 81 | ); |
| 82 | } else { |
| 83 | if (nextMode === "edit") { |
| 84 | await dispatch( |
| 85 | enterEditMode({ |
| 86 | returnToMode: mode, |
| 87 | }), |
| 88 | ); |
| 89 | } else { |
| 90 | dispatch(setMode(nextMode)); |
| 91 | } |
| 92 | } |
| 93 | mainEditor?.commands.focus(); |
| 94 | }, [jetbrains, mode, mainEditor]); |
| 95 | |
| 96 | const selectMode = useCallback( |
| 97 | async (newMode: MessageModes) => { |
| 98 | if (newMode === mode) { |
| 99 | return; |
| 100 | } |
| 101 | if (newMode === "edit") { |
| 102 | await dispatch( |
| 103 | enterEditMode({ |
| 104 | returnToMode: mode, |
| 105 | }), |
| 106 | ); |
| 107 | } else { |
| 108 | if (mode === "edit") { |
| 109 | await dispatch( |
| 110 | exitEditMode({ |
| 111 | goToMode: newMode, |
| 112 | }), |
| 113 | ); |
| 114 | } else { |
| 115 | dispatch(setMode(newMode)); |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | mainEditor?.commands.focus(); |
| 120 | }, |
| 121 | [mode, mainEditor], |
| 122 | ); |
| 123 | |
| 124 | useEffect(() => { |
| 125 | const handleKeyDown = (e: KeyboardEvent) => { |
| 126 | if (e.key === "." && (e.metaKey || e.ctrlKey)) { |
| 127 | e.preventDefault(); |
| 128 | void cycleMode(); |
| 129 | } |
| 130 | }; |
| 131 | |
| 132 | document.addEventListener("keydown", handleKeyDown); |
| 133 | return () => document.removeEventListener("keydown", handleKeyDown); |
| 134 | }, [cycleMode]); |
| 135 | |
| 136 | return ( |
| 137 | <Listbox value={mode} onChange={selectMode}> |
| 138 | <div className="relative"> |
| 139 | <ListboxButton |
| 140 | data-testid="mode-select-button" |
| 141 | className="xs:px-2 gap-1 border-none px-1.5 py-0.5 text-gray-400 transition-colors duration-200" |
| 142 | style={{ |
| 143 | backgroundColor: `${lightGray}33`, |
| 144 | borderRadius: "9999px", |
| 145 | }} |
| 146 | > |
| 147 | {getModeIcon(mode)} |
| 148 | <span className="hidden sm:block"> |
| 149 | {mode.charAt(0).toUpperCase() + mode.slice(1)} |
| 150 | </span> |
| 151 | <ChevronDownIcon |
| 152 | className="h-2 w-2 flex-shrink-0" |
| 153 | aria-hidden="true" |
| 154 | /> |
| 155 | </ListboxButton> |
| 156 | <ListboxOptions className="min-w-32 max-w-48"> |
| 157 | {!jetbrains && ( |
| 158 | <ListboxOption value="edit"> |
| 159 | <div className="flex flex-row items-center gap-1.5"> |
| 160 | <PencilIcon className="h-3 w-3" /> |
| 161 | <span className="">Edit</span> |
| 162 | <ShortcutText>{getMetaKeyLabel()}I</ShortcutText> |
| 163 | </div> |
| 164 | {mode === "edit" && <CheckIcon className="ml-auto h-3 w-3" />} |
| 165 | </ListboxOption> |
| 166 | )} |
| 167 | <ListboxOption value="chat"> |
| 168 | <div className="flex flex-row items-center gap-1.5"> |
| 169 | <ChatBubbleLeftIcon className="h-3 w-3" /> |
| 170 | <span className="">Chat</span> |
| 171 | <ShortcutText>{getMetaKeyLabel()}L</ShortcutText> |
| 172 | </div> |
| 173 | {mode === "chat" && <CheckIcon className="ml-auto h-3 w-3" />} |
| 174 | </ListboxOption> |
| 175 | |
| 176 | <ListboxOption |
| 177 | value="agent" |
| 178 | disabled={!agentModeSupported} |
| 179 | className={"gap-1"} |
| 180 | > |
| 181 | <div className="flex flex-row items-center gap-1.5"> |
| 182 | <SparklesIcon className="h-3 w-3" /> |
| 183 | <span className="">Agent</span> |
| 184 | </div> |
| 185 | {agentModeSupported ? ( |
| 186 | mode === "agent" && <CheckIcon className="ml-auto h-3 w-3" /> |
| 187 | ) : ( |
| 188 | <span>(Not supported)</span> |
| 189 | )} |
| 190 | </ListboxOption> |
| 191 | |
| 192 | <div className="text-lightgray px-2 py-1"> |
| 193 | {`${metaKeyLabel} . for next mode`} |
| 194 | </div> |
| 195 | </ListboxOptions> |
| 196 | </div> |
| 197 | </Listbox> |
| 198 | ); |
| 199 | } |
| 200 | |
| 201 | export default ModeSelect; |