| 1 | import { |
| 2 | CheckIcon, |
| 3 | ChevronDownIcon, |
| 4 | ExclamationTriangleIcon, |
| 5 | InformationCircleIcon, |
| 6 | } from "@heroicons/react/24/outline"; |
| 7 | import { MessageModes } from "core"; |
| 8 | import { isRecommendedAgentModel } from "core/llm/toolSupport"; |
| 9 | import { capitalize } from "lodash"; |
| 10 | import { useCallback, useEffect, useMemo } from "react"; |
| 11 | import { useAppDispatch, useAppSelector } from "../../redux/hooks"; |
| 12 | import { selectSelectedChatModel } from "../../redux/slices/configSlice"; |
| 13 | import { setMode } from "../../redux/slices/sessionSlice"; |
| 14 | import { getFontSize, getMetaKeyLabel } from "../../util"; |
| 15 | import { ToolTip } from "../gui/Tooltip"; |
| 16 | import { useMainEditor } from "../mainInput/TipTapEditor"; |
| 17 | import { Listbox, ListboxButton, ListboxOption, ListboxOptions } from "../ui"; |
| 18 | import { ModeIcon } from "./ModeIcon"; |
| 19 | |
| 20 | export function ModeSelect() { |
| 21 | const dispatch = useAppDispatch(); |
| 22 | const mode = useAppSelector((store) => store.session.mode); |
| 23 | const selectedModel = useAppSelector(selectSelectedChatModel); |
| 24 | |
| 25 | const isGoodAtAgentMode = useMemo(() => { |
| 26 | if (!selectedModel) { |
| 27 | return undefined; |
| 28 | } |
| 29 | return isRecommendedAgentModel(selectedModel.model); |
| 30 | }, [selectedModel]); |
| 31 | |
| 32 | const { mainEditor } = useMainEditor(); |
| 33 | const metaKeyLabel = useMemo(() => { |
| 34 | return getMetaKeyLabel(); |
| 35 | }, []); |
| 36 | |
| 37 | const cycleMode = useCallback(() => { |
| 38 | if (mode === "chat") { |
| 39 | dispatch(setMode("plan")); |
| 40 | } else if (mode === "plan") { |
| 41 | dispatch(setMode("agent")); |
| 42 | } else { |
| 43 | dispatch(setMode("chat")); |
| 44 | } |
| 45 | // Only focus main editor if another one doesn't already have focus |
| 46 | if (!document.activeElement?.classList?.contains("ProseMirror")) { |
| 47 | mainEditor?.commands.focus(); |
| 48 | } |
| 49 | }, [mode, mainEditor]); |
| 50 | |
| 51 | const selectMode = useCallback( |
| 52 | (newMode: MessageModes) => { |
| 53 | if (newMode === mode) { |
| 54 | return; |
| 55 | } |
| 56 | |
| 57 | dispatch(setMode(newMode)); |
| 58 | |
| 59 | mainEditor?.commands.focus(); |
| 60 | }, |
| 61 | [mode, mainEditor], |
| 62 | ); |
| 63 | |
| 64 | useEffect(() => { |
| 65 | const handleKeyDown = (e: KeyboardEvent) => { |
| 66 | if (e.key === "." && (e.metaKey || e.ctrlKey)) { |
| 67 | e.preventDefault(); |
| 68 | void cycleMode(); |
| 69 | } |
| 70 | }; |
| 71 | |
| 72 | document.addEventListener("keydown", handleKeyDown); |
| 73 | return () => document.removeEventListener("keydown", handleKeyDown); |
| 74 | }, [cycleMode]); |
| 75 | |
| 76 | const notGreatAtAgent = ( |
| 77 | <> |
| 78 | <ToolTip |
| 79 | style={{ |
| 80 | zIndex: 200001, // in front of listbox |
| 81 | }} |
| 82 | className="flex items-center gap-1" |
| 83 | content={`${capitalize(mode)} might not work well with this model.`} |
| 84 | > |
| 85 | <ExclamationTriangleIcon className="text-warning h-2.5 w-2.5" /> |
| 86 | </ToolTip> |
| 87 | </> |
| 88 | ); |
| 89 | |
| 90 | return ( |
| 91 | <Listbox value={mode} onChange={selectMode}> |
| 92 | <div className="relative"> |
| 93 | <ListboxButton |
| 94 | data-testid="mode-select-button" |
| 95 | className="xs:px-2 text-description bg-lightgray/20 gap-1 rounded-full border-none px-1.5 py-0.5 transition-colors duration-200 hover:brightness-110" |
| 96 | > |
| 97 | <ModeIcon mode={mode} /> |
| 98 | <span className="hidden sm:block"> |
| 99 | {mode === "chat" ? "Chat" : mode === "agent" ? "Agent" : "Plan"} |
| 100 | </span> |
| 101 | <ChevronDownIcon |
| 102 | className="h-2 w-2 flex-shrink-0" |
| 103 | aria-hidden="true" |
| 104 | /> |
| 105 | </ListboxButton> |
| 106 | <ListboxOptions className="min-w-32 max-w-48"> |
| 107 | <ListboxOption value="chat"> |
| 108 | <div className="flex flex-row items-center gap-1.5"> |
| 109 | <ModeIcon mode="chat" /> |
| 110 | <span className="">Chat</span> |
| 111 | <ToolTip |
| 112 | style={{ |
| 113 | zIndex: 200001, |
| 114 | }} |
| 115 | content="All tools disabled" |
| 116 | > |
| 117 | <InformationCircleIcon |
| 118 | data-tooltip-id="chat-tip" |
| 119 | className="h-2.5 w-2.5 flex-shrink-0" |
| 120 | /> |
| 121 | </ToolTip> |
| 122 | <span |
| 123 | className={`text-description-muted text-[${getFontSize() - 3}px] mr-auto`} |
| 124 | > |
| 125 | {getMetaKeyLabel()}L |
| 126 | </span> |
| 127 | </div> |
| 128 | {mode === "chat" && <CheckIcon className="ml-auto h-3 w-3" />} |
| 129 | </ListboxOption> |
| 130 | <ListboxOption value="plan" className={"gap-1"}> |
| 131 | <div className="flex flex-row items-center gap-1.5"> |
| 132 | <ModeIcon mode="plan" /> |
| 133 | <span className="">Plan</span> |
| 134 | <ToolTip |
| 135 | style={{ |
| 136 | zIndex: 200001, |
| 137 | }} |
| 138 | content="Read-only/MCP tools available" |
| 139 | > |
| 140 | <InformationCircleIcon className="h-2.5 w-2.5 flex-shrink-0" /> |
| 141 | </ToolTip> |
| 142 | </div> |
| 143 | {!isGoodAtAgentMode && notGreatAtAgent} |
| 144 | <CheckIcon |
| 145 | className={`ml-auto h-3 w-3 ${mode === "plan" ? "" : "opacity-0"}`} |
| 146 | /> |
| 147 | </ListboxOption> |
| 148 | |
| 149 | <ListboxOption value="agent" className={"gap-1"}> |
| 150 | <div className="flex flex-row items-center gap-1.5"> |
| 151 | <ModeIcon mode="agent" /> |
| 152 | <span className="">Agent</span> |
| 153 | <ToolTip |
| 154 | style={{ |
| 155 | zIndex: 200001, |
| 156 | }} |
| 157 | content="All tools available" |
| 158 | > |
| 159 | <InformationCircleIcon className="h-2.5 w-2.5 flex-shrink-0" /> |
| 160 | </ToolTip> |
| 161 | </div> |
| 162 | {!isGoodAtAgentMode && notGreatAtAgent} |
| 163 | <CheckIcon |
| 164 | className={`ml-auto h-3 w-3 ${mode === "agent" ? "" : "opacity-0"}`} |
| 165 | /> |
| 166 | </ListboxOption> |
| 167 | |
| 168 | <div className="text-description-muted px-2 py-1"> |
| 169 | {`${metaKeyLabel} . for next mode`} |
| 170 | </div> |
| 171 | </ListboxOptions> |
| 172 | </div> |
| 173 | </Listbox> |
| 174 | ); |
| 175 | } |