Elffuss BraidHub

kikocis/continue public

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