Elffuss BraidHub

kikocis/continue public

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