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