Elffuss BraidHub

kikocis/continue public

cd9781d18fda · 6.0 KiBRawPermalink
1import {
2 CheckIcon,
3 ChevronDownIcon,
4 ExclamationTriangleIcon,
5 InformationCircleIcon,
6} from "@heroicons/react/24/outline";
7import { MessageModes } from "core";
8import { isRecommendedAgentModel } from "core/llm/toolSupport";
9import { capitalize } from "lodash";
10import { useCallback, useEffect, useMemo } from "react";
11import { useAppDispatch, useAppSelector } from "../../redux/hooks";
12import { selectSelectedChatModel } from "../../redux/slices/configSlice";
13import { setMode } from "../../redux/slices/sessionSlice";
14import { getFontSize, getMetaKeyLabel } from "../../util";
15import { ToolTip } from "../gui/Tooltip";
16import { useMainEditor } from "../mainInput/TipTapEditor";
17import { Listbox, ListboxButton, ListboxOption, ListboxOptions } from "../ui";
18import { ModeIcon } from "./ModeIcon";
19
20export 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 <ExclamationTriangleIcon
79 data-tooltip-id="bad-at-agent-mode-tooltip"
80 className="text-warning h-2.5 w-2.5"
81 />
82 <ToolTip
83 id="bad-at-agent-mode-tooltip"
84 style={{
85 zIndex: 200001, // in front of listbox
86 }}
87 className="flex items-center gap-1"
88 >
89 {`${capitalize(mode)} might not work well with this model.`}
90 </ToolTip>
91 </>
92 );
93
94 return (
95 <Listbox value={mode} onChange={selectMode}>
96 <div className="relative">
97 <ListboxButton
98 data-testid="mode-select-button"
99 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"
100 >
101 <ModeIcon mode={mode} />
102 <span className="hidden sm:block">
103 {mode === "chat" ? "Chat" : mode === "agent" ? "Agent" : "Plan"}
104 </span>
105 <ChevronDownIcon
106 className="h-2 w-2 flex-shrink-0"
107 aria-hidden="true"
108 />
109 </ListboxButton>
110 <ListboxOptions className="min-w-32 max-w-48">
111 <ListboxOption value="chat">
112 <div className="flex flex-row items-center gap-1.5">
113 <ModeIcon mode="chat" />
114 <span className="">Chat</span>
115 <InformationCircleIcon
116 data-tooltip-id="chat-tip"
117 className="h-2.5 w-2.5 flex-shrink-0"
118 />
119 <ToolTip
120 id="chat-tip"
121 style={{
122 zIndex: 200001,
123 }}
124 >
125 All tools disabled
126 </ToolTip>
127 <span
128 className={`text-description-muted text-[${getFontSize() - 3}px] mr-auto`}
129 >
130 {getMetaKeyLabel()}L
131 </span>
132 </div>
133 {mode === "chat" && <CheckIcon className="ml-auto h-3 w-3" />}
134 </ListboxOption>
135 <ListboxOption value="plan" className={"gap-1"}>
136 <div className="flex flex-row items-center gap-1.5">
137 <ModeIcon mode="plan" />
138 <span className="">Plan</span>
139 <InformationCircleIcon
140 data-tooltip-id="plan-tip"
141 className="h-2.5 w-2.5 flex-shrink-0"
142 />
143 <ToolTip
144 id="plan-tip"
145 style={{
146 zIndex: 200001,
147 }}
148 >
149 Read-only/MCP tools available
150 </ToolTip>
151 </div>
152 {!isGoodAtAgentMode && notGreatAtAgent}
153 <CheckIcon
154 className={`ml-auto h-3 w-3 ${mode === "plan" ? "" : "opacity-0"}`}
155 />
156 </ListboxOption>
157
158 <ListboxOption value="agent" className={"gap-1"}>
159 <div className="flex flex-row items-center gap-1.5">
160 <ModeIcon mode="agent" />
161 <span className="">Agent</span>
162 <InformationCircleIcon
163 data-tooltip-id="agent-tip"
164 className="h-2.5 w-2.5 flex-shrink-0"
165 />
166 <ToolTip
167 id="agent-tip"
168 style={{
169 zIndex: 200001,
170 }}
171 >
172 All tools available
173 </ToolTip>
174 </div>
175 {!isGoodAtAgentMode && notGreatAtAgent}
176 <CheckIcon
177 className={`ml-auto h-3 w-3 ${mode === "agent" ? "" : "opacity-0"}`}
178 />
179 </ListboxOption>
180
181 <div className="text-description-muted px-2 py-1">
182 {`${metaKeyLabel} . for next mode`}
183 </div>
184 </ListboxOptions>
185 </div>
186 </Listbox>
187 );
188}