| 1 | import { Editor, JSONContent } from "@tiptap/react"; |
| 2 | import { ContextItemWithId, InputModifiers, RuleWithSource } from "core"; |
| 3 | import { useMemo } from "react"; |
| 4 | import styled, { keyframes } from "styled-components"; |
| 5 | import { defaultBorderRadius, vscBackground } from ".."; |
| 6 | import { useAppSelector } from "../../redux/hooks"; |
| 7 | import { selectSlashCommandComboBoxInputs } from "../../redux/selectors"; |
| 8 | import { ContextItemsPeek } from "./belowMainInput/ContextItemsPeek"; |
| 9 | import { RulesPeek } from "./belowMainInput/RulesPeek"; |
| 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 | const gradient = keyframes` |
| 42 | 0% { |
| 43 | background-position: 0px 0; |
| 44 | } |
| 45 | 100% { |
| 46 | background-position: 100em 0; |
| 47 | } |
| 48 | `; |
| 49 | |
| 50 | const GradientBorder = styled.div<{ |
| 51 | borderRadius?: string; |
| 52 | borderColor?: string; |
| 53 | loading: 0 | 1; |
| 54 | }>` |
| 55 | border-radius: ${(props) => props.borderRadius || "0"}; |
| 56 | padding: 1px; |
| 57 | background: ${(props) => |
| 58 | props.borderColor |
| 59 | ? props.borderColor |
| 60 | : `repeating-linear-gradient( |
| 61 | 101.79deg, |
| 62 | #1BBE84 0%, |
| 63 | #331BBE 16%, |
| 64 | #BE1B55 33%, |
| 65 | #A6BE1B 55%, |
| 66 | #BE1B55 67%, |
| 67 | #331BBE 85%, |
| 68 | #1BBE84 99% |
| 69 | )`}; |
| 70 | animation: ${(props) => (props.loading ? gradient : "")} 6s linear infinite; |
| 71 | background-size: 200% 200%; |
| 72 | width: 100%; |
| 73 | display: flex; |
| 74 | flex-direction: row; |
| 75 | align-items: center; |
| 76 | `; |
| 77 | |
| 78 | function ContinueInputBox(props: ContinueInputBoxProps) { |
| 79 | const isStreaming = useAppSelector((state) => state.session.isStreaming); |
| 80 | const availableSlashCommands = useAppSelector( |
| 81 | selectSlashCommandComboBoxInputs, |
| 82 | ); |
| 83 | const availableContextProviders = useAppSelector( |
| 84 | (state) => state.config.config.contextProviders, |
| 85 | ); |
| 86 | const isInEdit = useAppSelector((store) => store.session.isInEdit); |
| 87 | const editModeState = useAppSelector((state) => state.editModeState); |
| 88 | |
| 89 | const filteredSlashCommands = useMemo(() => { |
| 90 | return isInEdit ? [] : availableSlashCommands; |
| 91 | }, [isInEdit, availableSlashCommands]); |
| 92 | |
| 93 | const filteredContextProviders = useMemo(() => { |
| 94 | if (isInEdit) { |
| 95 | return ( |
| 96 | availableContextProviders?.filter( |
| 97 | (provider) => |
| 98 | !EDIT_DISALLOWED_CONTEXT_PROVIDERS.includes(provider.title), |
| 99 | ) ?? [] |
| 100 | ); |
| 101 | } |
| 102 | |
| 103 | return availableContextProviders ?? []; |
| 104 | }, [availableContextProviders, isInEdit]); |
| 105 | |
| 106 | const historyKey = isInEdit ? "edit" : "chat"; |
| 107 | const placeholder = isInEdit ? "Edit selected code" : undefined; |
| 108 | |
| 109 | const toolbarOptions: ToolbarOptions = isInEdit |
| 110 | ? { |
| 111 | hideAddContext: false, |
| 112 | hideImageUpload: false, |
| 113 | hideUseCodebase: true, |
| 114 | hideSelectModel: false, |
| 115 | enterText: |
| 116 | editModeState.applyState.status === "done" ? "Retry" : "Edit", |
| 117 | } |
| 118 | : {}; |
| 119 | |
| 120 | const { appliedRules = [], contextItems = [] } = props; |
| 121 | |
| 122 | return ( |
| 123 | <div |
| 124 | className={`${props.hidden ? "hidden" : ""}`} |
| 125 | data-testid="continue-input-box" |
| 126 | > |
| 127 | <div className={`relative flex flex-col px-2`}> |
| 128 | {props.isMainInput && <Lump />} |
| 129 | <GradientBorder |
| 130 | loading={isStreaming && props.isLastUserInput ? 1 : 0} |
| 131 | borderColor={ |
| 132 | isStreaming && props.isLastUserInput ? undefined : vscBackground |
| 133 | } |
| 134 | borderRadius={defaultBorderRadius} |
| 135 | > |
| 136 | <TipTapEditor |
| 137 | editorState={props.editorState} |
| 138 | onEnter={props.onEnter} |
| 139 | placeholder={placeholder} |
| 140 | isMainInput={props.isMainInput ?? false} |
| 141 | availableContextProviders={filteredContextProviders} |
| 142 | availableSlashCommands={filteredSlashCommands} |
| 143 | historyKey={historyKey} |
| 144 | toolbarOptions={toolbarOptions} |
| 145 | inputId={props.inputId} |
| 146 | /> |
| 147 | </GradientBorder> |
| 148 | </div> |
| 149 | {(appliedRules.length > 0 || contextItems.length > 0) && ( |
| 150 | <div className="mt-2 flex flex-col"> |
| 151 | <RulesPeek appliedRules={props.appliedRules} /> |
| 152 | <ContextItemsPeek |
| 153 | contextItems={props.contextItems} |
| 154 | isCurrentContextPeek={props.isLastUserInput} |
| 155 | /> |
| 156 | </div> |
| 157 | )} |
| 158 | </div> |
| 159 | ); |
| 160 | } |
| 161 | |
| 162 | export default ContinueInputBox; |