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