Elffuss BraidHub

kikocis/continue public

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