Elffuss BraidHub

kikocis/continue public

87544dc1817f · 8.4 KiBRawPermalink
1import {
2 AtSymbolIcon,
3 LightBulbIcon as LightBulbIconOutline,
4 PhotoIcon,
5} from "@heroicons/react/24/outline";
6import { LightBulbIcon as LightBulbIconSolid } from "@heroicons/react/24/solid";
7import { InputModifiers } from "core";
8import { modelSupportsImages } from "core/llm/autodetect";
9import { useContext, useRef } from "react";
10import { IdeMessengerContext } from "../../context/IdeMessenger";
11import { useAppDispatch, useAppSelector } from "../../redux/hooks";
12import { selectUseActiveFile } from "../../redux/selectors";
13import { selectSelectedChatModel } from "../../redux/slices/configSlice";
14import { setHasReasoningEnabled } from "../../redux/slices/sessionSlice";
15import { exitEdit } from "../../redux/thunks/edit";
16import { getMetaKeyLabel, isMetaEquivalentKeyPressed } from "../../util";
17import { ToolTip } from "../gui/Tooltip";
18import ModelSelect from "../modelSelection/ModelSelect";
19import { ModeSelect } from "../ModeSelect";
20import { Button } from "../ui";
21import { useFontSize } from "../ui/font";
22import ContextStatus from "./ContextStatus";
23import HoverItem from "./InputToolbar/HoverItem";
24
25export interface ToolbarOptions {
26 hideUseCodebase?: boolean;
27 hideImageUpload?: boolean;
28 hideAddContext?: boolean;
29 enterText?: string;
30 hideSelectModel?: boolean;
31}
32
33interface InputToolbarProps {
34 onEnter?: (modifiers: InputModifiers) => void;
35 onAddContextItem?: () => void;
36 onClick?: () => void;
37 onImageFileSelected?: (file: File) => void;
38 hidden?: boolean;
39 activeKey: string | null;
40 toolbarOptions?: ToolbarOptions;
41 disabled?: boolean;
42 isMainInput?: boolean;
43}
44
45function InputToolbar(props: InputToolbarProps) {
46 const dispatch = useAppDispatch();
47 const ideMessenger = useContext(IdeMessengerContext);
48 const fileInputRef = useRef<HTMLInputElement | null>(null);
49 const defaultModel = useAppSelector(selectSelectedChatModel);
50 const useActiveFile = useAppSelector(selectUseActiveFile);
51 const isInEdit = useAppSelector((store) => store.session.isInEdit);
52 const codeToEdit = useAppSelector((store) => store.editModeState.codeToEdit);
53 const hasReasoningEnabled = useAppSelector(
54 (store) => store.session.hasReasoningEnabled,
55 );
56
57 const isEnterDisabled =
58 props.disabled || (isInEdit && codeToEdit.length === 0);
59
60 const supportsImages =
61 defaultModel &&
62 modelSupportsImages(
63 defaultModel.provider,
64 defaultModel.model,
65 defaultModel.title,
66 defaultModel.capabilities,
67 );
68
69 const smallFont = useFontSize(-2);
70 const tinyFont = useFontSize(-3);
71
72 return (
73 <>
74 <div
75 onClick={props.onClick}
76 className={`find-widget-skip bg-vsc-input-background flex select-none flex-row items-center justify-between gap-1 pt-1 ${props.hidden ? "pointer-events-none h-0 cursor-default opacity-0" : "pointer-events-auto mt-2 cursor-text opacity-100"}`}
77 style={{
78 fontSize: smallFont,
79 }}
80 >
81 <div className="xs:gap-1.5 flex flex-row items-center gap-1">
82 {!isInEdit && (
83 <ToolTip place="top" content="Select Mode">
84 <HoverItem className="!p-0">
85 <ModeSelect />
86 </HoverItem>
87 </ToolTip>
88 )}
89 <ToolTip place="top" content="Select Model">
90 <HoverItem className="!p-0">
91 <ModelSelect />
92 </HoverItem>
93 </ToolTip>
94 <div className="xs:flex text-description -mb-1 hidden items-center transition-colors duration-200">
95 {props.toolbarOptions?.hideImageUpload ||
96 (supportsImages && (
97 <>
98 <input
99 type="file"
100 ref={fileInputRef}
101 style={{ display: "none" }}
102 accept=".jpg,.jpeg,.png,.gif,.svg,.webp"
103 onChange={(e) => {
104 const files = e.target?.files ?? [];
105 for (const file of files) {
106 props.onImageFileSelected?.(file);
107 }
108 if (fileInputRef.current) {
109 fileInputRef.current.value = "";
110 }
111 }}
112 />
113
114 <ToolTip place="top" content="Attach Image">
115 <HoverItem className="">
116 <PhotoIcon
117 className="h-3 w-3 hover:brightness-125"
118 onClick={(e) => {
119 fileInputRef.current?.click();
120 }}
121 />
122 </HoverItem>
123 </ToolTip>
124 </>
125 ))}
126 {props.toolbarOptions?.hideAddContext || (
127 <ToolTip place="top" content="Attach Context">
128 <HoverItem onClick={props.onAddContextItem}>
129 <AtSymbolIcon className="h-3 w-3 hover:brightness-125" />
130 </HoverItem>
131 </ToolTip>
132 )}
133 {defaultModel?.underlyingProviderName === "anthropic" && (
134 <HoverItem
135 onClick={() =>
136 dispatch(setHasReasoningEnabled(!hasReasoningEnabled))
137 }
138 >
139 <ToolTip
140 place="top"
141 content={
142 hasReasoningEnabled
143 ? "Disable model reasoning"
144 : "Enable model reasoning"
145 }
146 >
147 {hasReasoningEnabled ? (
148 <LightBulbIconSolid className="h-3 w-3 brightness-200 hover:brightness-150" />
149 ) : (
150 <LightBulbIconOutline className="h-3 w-3 hover:brightness-150" />
151 )}
152 </ToolTip>
153 </HoverItem>
154 )}
155 </div>
156 </div>
157
158 <div
159 className="text-description flex items-center gap-2 whitespace-nowrap"
160 style={{
161 fontSize: tinyFont,
162 }}
163 >
164 {!isInEdit && <ContextStatus />}
165 {!props.toolbarOptions?.hideUseCodebase && !isInEdit && (
166 <div className="hidden transition-colors duration-200 hover:underline md:flex">
167 <HoverItem
168 className={
169 props.activeKey === "Meta" ||
170 props.activeKey === "Control" ||
171 props.activeKey === "Alt"
172 ? "underline"
173 : ""
174 }
175 onClick={(e) =>
176 props.onEnter?.({
177 useCodebase: false,
178 noContext: !useActiveFile,
179 })
180 }
181 >
182 <ToolTip
183 place="top-end"
184 content={`${
185 useActiveFile
186 ? "Send Without Active File"
187 : "Send With Active File"
188 } (${getMetaKeyLabel()}⏎)`}
189 >
190 <span>
191 {getMetaKeyLabel()}⏎{" "}
192 {useActiveFile ? "No active file" : "Active file"}
193 </span>
194 </ToolTip>
195 </HoverItem>
196 </div>
197 )}
198 {isInEdit && (
199 <HoverItem
200 className="hidden hover:underline sm:flex"
201 onClick={async () => {
202 void dispatch(exitEdit({}));
203 ideMessenger.post("focusEditor", undefined);
204 }}
205 >
206 <span>
207 <i>Esc</i> to exit Edit
208 </span>
209 </HoverItem>
210 )}
211 <ToolTip place="top" content="Send (⏎)">
212 <Button
213 variant={props.isMainInput ? "primary" : "secondary"}
214 size="sm"
215 data-testid="submit-input-button"
216 onClick={async (e) => {
217 if (props.onEnter) {
218 props.onEnter({
219 useCodebase: false,
220 noContext: useActiveFile
221 ? !(isMetaEquivalentKeyPressed(e as any) || e.altKey)
222 : isMetaEquivalentKeyPressed(e as any) || e.altKey,
223 });
224 }
225 }}
226 disabled={isEnterDisabled}
227 >
228 <span className="hidden md:inline">
229 ⏎ {props.toolbarOptions?.enterText ?? "Enter"}
230 </span>
231 <span className="md:hidden">⏎</span>
232 </Button>
233 </ToolTip>
234 </div>
235 </div>
236 </>
237 );
238}
239
240export default InputToolbar;