Elffuss BraidHub

kikocis/continue public

6e4b7e9fc0a8 · 5.6 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 <span
101 className={`text-description-muted text-[${getFontSize() - 3}px] mr-auto`}
102 >
103 {getMetaKeyLabel()}L
104 </span>
105 </div>
106 {mode === "chat" && <CheckIcon className="ml-auto h-3 w-3" />}
107 </ListboxOption>
108 <ListboxOption
109 value="plan"
110 disabled={!agentModeSupported}
111 className={"gap-1"}
112 >
113 <div className="flex flex-row items-center gap-1.5">
114 <ModeIcon mode="plan" />
115 <span className="">Plan</span>
116 <InformationCircleIcon
117 data-tooltip-id="plan-tip"
118 className="h-2.5 w-2.5 flex-shrink-0"
119 />
120 <ToolTip
121 id="plan-tip"
122 style={{
123 zIndex: 200001,
124 }}
125 >
126 In Plan mode, only read-only and MCP tools are enabled
127 </ToolTip>
128 </div>
129 {agentModeSupported ? (
130 <CheckIcon
131 className={`ml-auto h-3 w-3 ${mode === "plan" ? "" : "opacity-0"}`}
132 />
133 ) : (
134 <span>(Not supported)</span>
135 )}
136 </ListboxOption>
137 <ListboxOption
138 value="agent"
139 disabled={!agentModeSupported}
140 className={"gap-1"}
141 >
142 <div className="flex flex-row items-center gap-1.5">
143 <ModeIcon mode="agent" />
144 <span className="">Agent</span>
145 <InformationCircleIcon
146 data-tooltip-id="agent-tip"
147 className="h-2.5 w-2.5 flex-shrink-0"
148 />
149 <ToolTip
150 id="agent-tip"
151 style={{
152 zIndex: 200001,
153 }}
154 >
155 All tools are enabled based on policies
156 </ToolTip>
157 </div>
158 {agentModeSupported ? (
159 <CheckIcon
160 className={`ml-auto h-3 w-3 ${mode === "agent" ? "" : "opacity-0"}`}
161 />
162 ) : (
163 <span>(Not supported)</span>
164 )}
165 </ListboxOption>
166
167 <div className="text-description-muted px-2 py-1">
168 {`${metaKeyLabel} . for next mode`}
169 </div>
170 </ListboxOptions>
171 </div>
172 </Listbox>
173 );
174}