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