| 1 | import { Editor, JSONContent } from "@tiptap/react"; |
| 2 | import { ContextItemWithId, InputModifiers, RuleWithSource } from "core"; |
| 3 | import { useMemo } from "react"; |
| 4 | import { defaultBorderRadius, vscBackground } from ".."; |
| 5 | import { useAppSelector } from "../../redux/hooks"; |
| 6 | import { selectSlashCommandComboBoxInputs } from "../../redux/selectors"; |
| 7 | import { ContextItemsPeek } from "./belowMainInput/ContextItemsPeek"; |
| 8 | import { RulesPeek } from "./belowMainInput/RulesPeek"; |
| 9 | import { GradientBorder } from "./GradientBorder"; |
| 10 | import { ToolbarOptions } from "./InputToolbar"; |
| 11 | import { Lump } from "./Lump"; |
| 12 | import { TipTapEditor } from "./TipTapEditor"; |
| 13 | |
| 14 | interface ContinueInputBoxProps { |
| 15 | isLastUserInput: boolean; |
| 16 | isMainInput?: boolean; |
| 17 | onEnter: ( |
| 18 | editorState: JSONContent, |
| 19 | modifiers: InputModifiers, |
| 20 | editor: Editor, |
| 21 | ) => void; |
| 22 | editorState?: JSONContent; |
| 23 | contextItems?: ContextItemWithId[]; |
| 24 | appliedRules?: RuleWithSource[]; |
| 25 | hidden?: boolean; |
| 26 | inputId: string; // used to keep track of things per input in redux |
| 27 | } |
| 28 | |
| 29 | const EDIT_DISALLOWED_CONTEXT_PROVIDERS = [ |
| 30 | "codebase", |
| 31 | "tree", |
| 32 | "open", |
| 33 | "web", |
| 34 | "diff", |
| 35 | "folder", |
| 36 | "search", |
| 37 | "debugger", |
| 38 | "repo-map", |
| 39 | ]; |
| 40 | |
| 41 | function ContinueInputBox(props: ContinueInputBoxProps) { |
| 42 | const isStreaming = useAppSelector((state) => state.session.isStreaming); |
| 43 | const availableSlashCommands = useAppSelector( |
| 44 | selectSlashCommandComboBoxInputs, |
| 45 | ); |
| 46 | const availableContextProviders = useAppSelector( |
| 47 | (state) => state.config.config.contextProviders, |
| 48 | ); |
| 49 | const isInEdit = useAppSelector((store) => store.session.isInEdit); |
| 50 | const editModeState = useAppSelector((state) => state.editModeState); |
| 51 | |
| 52 | const filteredSlashCommands = useMemo(() => { |
| 53 | return isInEdit ? [] : availableSlashCommands; |
| 54 | }, [isInEdit, availableSlashCommands]); |
| 55 | |
| 56 | const filteredContextProviders = useMemo(() => { |
| 57 | if (isInEdit) { |
| 58 | return ( |
| 59 | availableContextProviders?.filter( |
| 60 | (provider) => |
| 61 | !EDIT_DISALLOWED_CONTEXT_PROVIDERS.includes(provider.title), |
| 62 | ) ?? [] |
| 63 | ); |
| 64 | } |
| 65 | |
| 66 | return availableContextProviders ?? []; |
| 67 | }, [availableContextProviders, isInEdit]); |
| 68 | |
| 69 | const historyKey = isInEdit ? "edit" : "chat"; |
| 70 | const placeholder = isInEdit ? "Edit selected code" : undefined; |
| 71 | |
| 72 | const toolbarOptions: ToolbarOptions = isInEdit |
| 73 | ? { |
| 74 | hideAddContext: false, |
| 75 | hideImageUpload: false, |
| 76 | hideUseCodebase: true, |
| 77 | hideSelectModel: false, |
| 78 | enterText: |
| 79 | editModeState.applyState.status === "done" ? "Retry" : "Edit", |
| 80 | } |
| 81 | : {}; |
| 82 | |
| 83 | const { appliedRules = [], contextItems = [] } = props; |
| 84 | |
| 85 | return ( |
| 86 | <div |
| 87 | className={`${props.hidden ? "hidden" : ""}`} |
| 88 | data-testid={`continue-input-box-${props.inputId}`} |
| 89 | > |
| 90 | <div className={`relative flex flex-col px-2`}> |
| 91 | {props.isMainInput && <Lump />} |
| 92 | <GradientBorder |
| 93 | loading={isStreaming && props.isLastUserInput ? 1 : 0} |
| 94 | borderColor={ |
| 95 | isStreaming && props.isLastUserInput ? undefined : vscBackground |
| 96 | } |
| 97 | borderRadius={defaultBorderRadius} |
| 98 | > |
| 99 | <TipTapEditor |
| 100 | editorState={props.editorState} |
| 101 | onEnter={props.onEnter} |
| 102 | placeholder={placeholder} |
| 103 | isMainInput={props.isMainInput ?? false} |
| 104 | availableContextProviders={filteredContextProviders} |
| 105 | availableSlashCommands={filteredSlashCommands} |
| 106 | historyKey={historyKey} |
| 107 | toolbarOptions={toolbarOptions} |
| 108 | inputId={props.inputId} |
| 109 | /> |
| 110 | </GradientBorder> |
| 111 | </div> |
| 112 | |
| 113 | {(appliedRules.length > 0 || contextItems.length > 0) && ( |
| 114 | <div className="mt-2 flex flex-col"> |
| 115 | <RulesPeek appliedRules={props.appliedRules} /> |
| 116 | <ContextItemsPeek |
| 117 | contextItems={props.contextItems} |
| 118 | isCurrentContextPeek={props.isLastUserInput} |
| 119 | /> |
| 120 | </div> |
| 121 | )} |
| 122 | </div> |
| 123 | ); |
| 124 | } |
| 125 | |
| 126 | export default ContinueInputBox; |