| 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 { modelSupportsImages } from "core/llm/autodetect"; |
| 9 | import { useContext, useRef } from "react"; |
| 10 | import { IdeMessengerContext } from "../../context/IdeMessenger"; |
| 11 | import { useAppDispatch, useAppSelector } from "../../redux/hooks"; |
| 12 | import { selectUseActiveFile } from "../../redux/selectors"; |
| 13 | import { selectSelectedChatModel } from "../../redux/slices/configSlice"; |
| 14 | import { setHasReasoningEnabled } from "../../redux/slices/sessionSlice"; |
| 15 | import { exitEdit } from "../../redux/thunks/edit"; |
| 16 | import { getAltKeyLabel, isMetaEquivalentKeyPressed } from "../../util"; |
| 17 | import { ToolTip } from "../gui/Tooltip"; |
| 18 | import ModelSelect from "../modelSelection/ModelSelect"; |
| 19 | import { ModeSelect } from "../ModeSelect"; |
| 20 | import { Button } from "../ui"; |
| 21 | import { useFontSize } from "../ui/font"; |
| 22 | import ContextStatus from "./ContextStatus"; |
| 23 | import HoverItem from "./InputToolbar/HoverItem"; |
| 24 | |
| 25 | export interface ToolbarOptions { |
| 26 | hideUseCodebase?: boolean; |
| 27 | hideImageUpload?: boolean; |
| 28 | hideAddContext?: boolean; |
| 29 | enterText?: string; |
| 30 | hideSelectModel?: boolean; |
| 31 | } |
| 32 | |
| 33 | interface 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 | |
| 45 | function 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 | <HoverItem data-tooltip-id="mode-select-tooltip" className="!p-0"> |
| 84 | <ModeSelect /> |
| 85 | <ToolTip id="mode-select-tooltip" place="top"> |
| 86 | Select Mode |
| 87 | </ToolTip> |
| 88 | </HoverItem> |
| 89 | )} |
| 90 | <HoverItem data-tooltip-id="model-select-tooltip" className="!p-0"> |
| 91 | <ModelSelect /> |
| 92 | <ToolTip id="model-select-tooltip" place="top"> |
| 93 | Select Model |
| 94 | </ToolTip> |
| 95 | </HoverItem> |
| 96 | <div className="xs:flex text-description -mb-1 hidden items-center transition-colors duration-200"> |
| 97 | {props.toolbarOptions?.hideImageUpload || |
| 98 | (supportsImages && ( |
| 99 | <> |
| 100 | <input |
| 101 | type="file" |
| 102 | ref={fileInputRef} |
| 103 | style={{ display: "none" }} |
| 104 | accept=".jpg,.jpeg,.png,.gif,.svg,.webp" |
| 105 | onChange={(e) => { |
| 106 | const files = e.target?.files ?? []; |
| 107 | for (const file of files) { |
| 108 | props.onImageFileSelected?.(file); |
| 109 | } |
| 110 | if (fileInputRef.current) { |
| 111 | fileInputRef.current.value = ""; |
| 112 | } |
| 113 | }} |
| 114 | /> |
| 115 | <HoverItem className=""> |
| 116 | <PhotoIcon |
| 117 | className="h-3 w-3 hover:brightness-125" |
| 118 | data-tooltip-id="image-tooltip" |
| 119 | onClick={(e) => { |
| 120 | fileInputRef.current?.click(); |
| 121 | }} |
| 122 | /> |
| 123 | |
| 124 | <ToolTip id="image-tooltip" place="top"> |
| 125 | Attach Image |
| 126 | </ToolTip> |
| 127 | </HoverItem> |
| 128 | </> |
| 129 | ))} |
| 130 | {props.toolbarOptions?.hideAddContext || ( |
| 131 | <HoverItem onClick={props.onAddContextItem}> |
| 132 | <AtSymbolIcon |
| 133 | data-tooltip-id="add-context-item-tooltip" |
| 134 | className="h-3 w-3 hover:brightness-125" |
| 135 | /> |
| 136 | |
| 137 | <ToolTip id="add-context-item-tooltip" place="top"> |
| 138 | Attach Context |
| 139 | </ToolTip> |
| 140 | </HoverItem> |
| 141 | )} |
| 142 | {defaultModel?.underlyingProviderName === "anthropic" && ( |
| 143 | <HoverItem |
| 144 | onClick={() => |
| 145 | dispatch(setHasReasoningEnabled(!hasReasoningEnabled)) |
| 146 | } |
| 147 | > |
| 148 | {hasReasoningEnabled ? ( |
| 149 | <LightBulbIconSolid |
| 150 | data-tooltip-id="model-reasoning-tooltip" |
| 151 | className="h-3 w-3 brightness-200 hover:brightness-150" |
| 152 | /> |
| 153 | ) : ( |
| 154 | <LightBulbIconOutline |
| 155 | data-tooltip-id="model-reasoning-tooltip" |
| 156 | className="h-3 w-3 hover:brightness-150" |
| 157 | /> |
| 158 | )} |
| 159 | |
| 160 | <ToolTip id="model-reasoning-tooltip" place="top"> |
| 161 | {hasReasoningEnabled |
| 162 | ? "Disable model reasoning" |
| 163 | : "Enable model reasoning"} |
| 164 | </ToolTip> |
| 165 | </HoverItem> |
| 166 | )} |
| 167 | </div> |
| 168 | </div> |
| 169 | |
| 170 | <div |
| 171 | className="text-description flex items-center gap-2 whitespace-nowrap" |
| 172 | style={{ |
| 173 | fontSize: tinyFont, |
| 174 | }} |
| 175 | > |
| 176 | {!isInEdit && <ContextStatus />} |
| 177 | {!props.toolbarOptions?.hideUseCodebase && !isInEdit && ( |
| 178 | <div |
| 179 | className={`hover:underline" hidden transition-colors duration-200 md:flex`} |
| 180 | > |
| 181 | <HoverItem |
| 182 | className={props.activeKey === "Alt" ? "underline" : ""} |
| 183 | onClick={(e) => |
| 184 | props.onEnter?.({ |
| 185 | useCodebase: false, |
| 186 | noContext: !useActiveFile, |
| 187 | }) |
| 188 | } |
| 189 | > |
| 190 | <span data-tooltip-id="add-active-file-context-tooltip"> |
| 191 | {getAltKeyLabel()}⏎{" "} |
| 192 | {useActiveFile ? "No active file" : "Active file"} |
| 193 | </span> |
| 194 | <ToolTip id="add-active-file-context-tooltip" place="top-end"> |
| 195 | {useActiveFile |
| 196 | ? "Send Without Active File" |
| 197 | : "Send With Active File"}{" "} |
| 198 | ({getAltKeyLabel()}⏎) |
| 199 | </ToolTip> |
| 200 | </HoverItem> |
| 201 | </div> |
| 202 | )} |
| 203 | {isInEdit && ( |
| 204 | <HoverItem |
| 205 | className="hidden hover:underline sm:flex" |
| 206 | onClick={async () => { |
| 207 | void dispatch(exitEdit({})); |
| 208 | ideMessenger.post("focusEditor", undefined); |
| 209 | }} |
| 210 | > |
| 211 | <span> |
| 212 | <i>Esc</i> to exit Edit |
| 213 | </span> |
| 214 | </HoverItem> |
| 215 | )} |
| 216 | <Button |
| 217 | data-tooltip-id="enter-tooltip" |
| 218 | variant={props.isMainInput ? "primary" : "secondary"} |
| 219 | size="sm" |
| 220 | data-testid="submit-input-button" |
| 221 | onClick={async (e) => { |
| 222 | if (props.onEnter) { |
| 223 | props.onEnter({ |
| 224 | useCodebase: isMetaEquivalentKeyPressed(e as any), |
| 225 | noContext: useActiveFile ? e.altKey : !e.altKey, |
| 226 | }); |
| 227 | } |
| 228 | }} |
| 229 | disabled={isEnterDisabled} |
| 230 | > |
| 231 | <span className="hidden md:inline"> |
| 232 | ⏎ {props.toolbarOptions?.enterText ?? "Enter"} |
| 233 | </span> |
| 234 | <span className="md:hidden">⏎</span> |
| 235 | <ToolTip id="enter-tooltip" place="top"> |
| 236 | Send (⏎) |
| 237 | </ToolTip> |
| 238 | </Button> |
| 239 | </div> |
| 240 | </div> |
| 241 | </> |
| 242 | ); |
| 243 | } |
| 244 | |
| 245 | export default InputToolbar; |