Elffuss BraidHub

kikocis/continue public

bc3425f09bf8 · 8.2 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 { getAltKeyLabel, 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={props.activeKey === "Alt" ? "underline" : ""}
169 onClick={(e) =>
170 props.onEnter?.({
171 useCodebase: false,
172 noContext: !useActiveFile,
173 })
174 }
175 >
176 <ToolTip
177 place="top-end"
178 content={`
179 ${
180 useActiveFile
181 ? "Send Without Active File"
182 : "Send With Active File"
183 } (${getAltKeyLabel()}⏎)`}
184 >
185 <span>
186 {getAltKeyLabel()}⏎{" "}
187 {useActiveFile ? "No active file" : "Active file"}
188 </span>
189 </ToolTip>
190 </HoverItem>
191 </div>
192 )}
193 {isInEdit && (
194 <HoverItem
195 className="hidden hover:underline sm:flex"
196 onClick={async () => {
197 void dispatch(exitEdit({}));
198 ideMessenger.post("focusEditor", undefined);
199 }}
200 >
201 <span>
202 <i>Esc</i> to exit Edit
203 </span>
204 </HoverItem>
205 )}
206 <ToolTip place="top" content="Send (⏎)">
207 <Button
208 variant={props.isMainInput ? "primary" : "secondary"}
209 size="sm"
210 data-testid="submit-input-button"
211 onClick={async (e) => {
212 if (props.onEnter) {
213 props.onEnter({
214 useCodebase: isMetaEquivalentKeyPressed(e as any),
215 noContext: useActiveFile ? e.altKey : !e.altKey,
216 });
217 }
218 }}
219 disabled={isEnterDisabled}
220 >
221 <span className="hidden md:inline">
222 ⏎ {props.toolbarOptions?.enterText ?? "Enter"}
223 </span>
224 <span className="md:hidden">⏎</span>
225 </Button>
226 </ToolTip>
227 </div>
228 </div>
229 </>
230 );
231}
232
233export default InputToolbar;