Repository navigation
feat(chat): add prompt history picker to the chat input #2155
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
7c0338a
9222cbd
22aeee9
f92b896
7646282
91e5c91
e3171d7
b8e6593
1581e06
b8aafef
0fbfc11
1466e01
5cc351c
6718fcb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,6 +16,7 @@ | |
| ContextMenuOptionType, | ||
| getContextMenuOptions, | ||
| insertMention, | ||
| isHistoryTrigger, | ||
| removeMention, | ||
| shouldShowContextMenu, | ||
| SearchResult, | ||
|
|
@@ -238,6 +239,10 @@ | |
| const [isDraggingOver, setIsDraggingOver] = useState(false) | ||
| const [textAreaBaseHeight, setTextAreaBaseHeight] = useState<number | undefined>(undefined) | ||
| const [showContextMenu, setShowContextMenu] = useState(false) | ||
| // Which trigger opened the menu. Recorded from the input event rather than | ||
| // re-derived from the controlled value: a mention query can start with "!" | ||
| // ("Check @!"), and the controlled prop can lag behind the keystroke. | ||
| const [menuSource, setMenuSource] = useState<"mention" | "slash" | "history" | null>(null) | ||
| const [cursorPosition, setCursorPosition] = useState(0) | ||
| const [searchQuery, setSearchQuery] = useState("") | ||
| const textAreaRef = useRef<HTMLTextAreaElement | null>(null) | ||
|
|
@@ -251,14 +256,35 @@ | |
| const [isEnhancingPrompt, setIsEnhancingPrompt] = useState(false) | ||
| const [isFocused, setIsFocused] = useState(false) | ||
|
|
||
| // Text for which the user explicitly dismissed the prompt history picker. | ||
| // Without this, recalling (or typing) a message that starts with "!" would | ||
| // pop the picker back open on the very next keystroke. | ||
| const suppressedHistoryRef = useRef<string | null>(null) | ||
|
|
||
| // A controlled reset (for example sending the message) does not go through | ||
| // handleInputChange, so close the picker and drop its stale state, and | ||
| // clear the dismissal once the input is empty again. | ||
| useEffect(() => { | ||
| if (!normalizedInputValue) { | ||
| suppressedHistoryRef.current = null | ||
| setShowContextMenu(false) | ||
| setMenuSource(null) | ||
|
Check warning on line 271 in webview-ui/src/components/chat/ChatTextArea.tsx
|
||
| setSearchQuery("") | ||
|
Check warning on line 272 in webview-ui/src/components/chat/ChatTextArea.tsx
|
||
| setSelectedType(null) | ||
|
Check warning on line 273 in webview-ui/src/components/chat/ChatTextArea.tsx
|
||
| setSelectedMenuIndex(-1) | ||
|
Check warning on line 274 in webview-ui/src/components/chat/ChatTextArea.tsx
|
||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
| }, [normalizedInputValue]) | ||
|
|
||
| // Use custom hook for prompt history navigation | ||
| const { handleHistoryNavigation, resetHistoryNavigation, resetOnInputChange } = usePromptHistory({ | ||
| clineMessages, | ||
| taskHistory, | ||
| cwd, | ||
| inputValue: normalizedInputValue, | ||
| setInputValue, | ||
| }) | ||
| const { handleHistoryNavigation, resetHistoryNavigation, resetOnInputChange, promptHistory } = usePromptHistory( | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could this picker include the initial task prompt?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch, fixed in The initial prompt is stored as the first message with Covered by tests: initial prompt included, no-feedback task, assistant text |
||
| { | ||
| clineMessages, | ||
| taskHistory, | ||
| cwd, | ||
| inputValue: normalizedInputValue, | ||
| setInputValue, | ||
| }, | ||
| ) | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
|
|
||
| // Fetch git commits when Git is selected or when typing a hash. | ||
| useEffect(() => { | ||
|
|
@@ -346,6 +372,27 @@ | |
| return | ||
| } | ||
|
|
||
| if (type === ContextMenuOptionType.History && value) { | ||
|
Check warning on line 375 in webview-ui/src/components/chat/ChatTextArea.tsx
|
||
| // Replace the whole input with the recalled prompt instead of | ||
| // inserting it as a mention: the "!" prefix is only a trigger | ||
| // and must not survive into the message. Deliberately does not | ||
| // send, so the user can edit the prompt before re-sending. | ||
| suppressedHistoryRef.current = value | ||
| setShowContextMenu(false) | ||
| setSelectedType(null) | ||
|
Check warning on line 382 in webview-ui/src/components/chat/ChatTextArea.tsx
|
||
| setSelectedMenuIndex(-1) | ||
|
Check warning on line 383 in webview-ui/src/components/chat/ChatTextArea.tsx
|
||
| setSearchQuery("") | ||
| setInputValue(value) | ||
| setCursorPosition(value.length) | ||
| setIntendedCursorPosition(value.length) | ||
| resetHistoryNavigation() | ||
|
|
||
| setTimeout(() => { | ||
| textAreaRef.current?.focus() | ||
| }, 0) | ||
| return | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
|
|
||
| if (type === ContextMenuOptionType.Mode && value) { | ||
| // Handle mode selection. | ||
| setMode(value) | ||
|
|
@@ -439,10 +486,24 @@ | |
|
|
||
| const handleKeyDown = useCallback( | ||
| (event: React.KeyboardEvent<HTMLTextAreaElement>) => { | ||
| const isComposing = event.nativeEvent?.isComposing ?? false | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
| // Taken from the trigger that opened the menu: a mention query can | ||
| // start with "!" (for example "Check @!") and must not be treated | ||
| // as history. | ||
| const isHistoryQuery = menuSource === "history" | ||
|
|
||
| if (showContextMenu) { | ||
| if (event.key === "Escape") { | ||
| if (event.key === "Escape" && !isComposing) { | ||
| // Dismiss the menu outright. `!` is easy to type by | ||
| // accident, so Escape must actually close the picker | ||
| // rather than leave the arrow keys captured by it. | ||
| // While composing, Escape belongs to the IME candidate | ||
| // window and must not dismiss the picker. | ||
| suppressedHistoryRef.current = normalizedInputValue | ||
| setShowContextMenu(false) | ||
| setSearchQuery("") | ||
| setSelectedType(null) | ||
| setSelectedMenuIndex(3) // File by default | ||
| setSelectedMenuIndex(-1) | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
| return | ||
| } | ||
|
|
||
|
|
@@ -457,6 +518,8 @@ | |
| fileSearchResults, | ||
| allModes, | ||
| commands, | ||
| promptHistory, | ||
| isHistoryQuery, | ||
| ) | ||
| const optionsLength = options.length | ||
|
|
||
|
|
@@ -486,30 +549,48 @@ | |
| }) | ||
| return | ||
| } | ||
| if ((event.key === "Enter" || event.key === "Tab") && selectedMenuIndex !== -1) { | ||
| event.preventDefault() | ||
| // Only a plain Enter/Tab commits a row. Shift+Enter, Ctrl/Cmd+Enter | ||
| // and an IME-composed Enter stay on their normal newline/send path. | ||
| const isRowSelectKey = | ||
| (event.key === "Enter" || event.key === "Tab") && | ||
| !event.shiftKey && | ||
| !event.ctrlKey && | ||
| !event.metaKey && | ||
| !event.altKey && | ||
| !isComposing | ||
|
|
||
| if (isRowSelectKey && selectedMenuIndex !== -1) { | ||
| const selectedOption = getContextMenuOptions( | ||
| searchQuery, | ||
| selectedType, | ||
| queryItems, | ||
| fileSearchResults, | ||
| allModes, | ||
| commands, | ||
| promptHistory, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could history selection require an unmodified non-composing key event?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes — fixed in |
||
| isHistoryQuery, | ||
| )[selectedMenuIndex] | ||
| if ( | ||
| selectedOption && | ||
| selectedOption.type !== ContextMenuOptionType.URL && | ||
| selectedOption.type !== ContextMenuOptionType.NoResults && | ||
| selectedOption.type !== ContextMenuOptionType.SectionHeader | ||
| ) { | ||
| event.preventDefault() | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could this fall-through be limited to unmatched history queries?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes — fixed in |
||
| handleMentionSelect(selectedOption.type, selectedOption.value) | ||
| return | ||
| } | ||
|
|
||
| // Nothing selectable (e.g. "No results"). A history query is the | ||
| // whole message, so let Enter send it. Other menus keep their | ||
| // previous behavior and absorb the key. | ||
| if (!isHistoryQuery) { | ||
| event.preventDefault() | ||
| return | ||
| } | ||
| return | ||
| } | ||
| } | ||
|
|
||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
| const isComposing = event.nativeEvent?.isComposing ?? false | ||
|
|
||
| // Handle prompt history navigation using custom hook | ||
| if (handleHistoryNavigation(event, showContextMenu, isComposing)) { | ||
| return | ||
|
|
@@ -582,6 +663,7 @@ | |
| [ | ||
| onSend, | ||
| showContextMenu, | ||
| menuSource, | ||
| searchQuery, | ||
| selectedMenuIndex, | ||
| handleMentionSelect, | ||
|
|
@@ -596,6 +678,7 @@ | |
| handleHistoryNavigation, | ||
| resetHistoryNavigation, | ||
| commands, | ||
| promptHistory, | ||
| enterBehavior, | ||
| ], | ||
| ) | ||
|
|
@@ -622,12 +705,36 @@ | |
| setCursorPosition(newCursorPosition) | ||
|
|
||
| const showMenu = shouldShowContextMenu(newValue, newCursorPosition) | ||
|
|
||
| if (isHistoryTrigger(newValue)) { | ||
| const suppressed = suppressedHistoryRef.current | ||
|
|
||
| // Keep the picker closed when the user dismissed it for this text, | ||
| // or when there is nothing to recall. Without the second guard an | ||
| // empty picker would swallow Enter for a message starting with "!". | ||
| if ((suppressed !== null && newValue.startsWith(suppressed)) || promptHistory.length === 0) { | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could external empty-value resets clear this suppression?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes — fixed in |
||
| setShowContextMenu(false) | ||
| setMenuSource(null) | ||
| setSearchQuery("") | ||
| setSelectedMenuIndex(-1) | ||
| return | ||
| } | ||
| } else { | ||
| suppressedHistoryRef.current = null | ||
| } | ||
|
|
||
| setShowContextMenu(showMenu) | ||
|
|
||
| if (showMenu) { | ||
| if (newValue.startsWith("/") && !newValue.includes(" ")) { | ||
| if (isHistoryTrigger(newValue)) { | ||
| // getContextMenuOptions dispatches on the leading "!" itself. | ||
| setMenuSource("history") | ||
| setSearchQuery(newValue) | ||
| setSelectedMenuIndex(0) | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
| } else if (newValue.startsWith("/") && !newValue.includes(" ")) { | ||
| // Handle slash command - request fresh commands | ||
| const query = newValue | ||
| setMenuSource("slash") | ||
| setSearchQuery(query) | ||
| // Set to first selectable item (skip section headers) | ||
| setSelectedMenuIndex(1) // Section header is at 0, first command is at 1 | ||
|
|
@@ -637,6 +744,7 @@ | |
| // Existing @ mention handling. | ||
| const lastAtIndex = newValue.lastIndexOf("@", newCursorPosition - 1) | ||
| const query = newValue.slice(lastAtIndex + 1, newCursorPosition) | ||
| setMenuSource("mention") | ||
| setSearchQuery(query) | ||
|
|
||
| // Send file search request if query is not empty. | ||
|
|
@@ -673,9 +781,17 @@ | |
| setSearchQuery("") | ||
| setSelectedMenuIndex(-1) | ||
| setFileSearchResults([]) // Clear file search results. | ||
| setMenuSource(null) | ||
| } | ||
| }, | ||
| [setInputValue, setSearchRequestId, setFileSearchResults, setSearchLoading, resetOnInputChange], | ||
| [ | ||
| setInputValue, | ||
| setSearchRequestId, | ||
| setFileSearchResults, | ||
| setSearchLoading, | ||
| resetOnInputChange, | ||
| promptHistory, | ||
| ], | ||
| ) | ||
|
|
||
| useEffect(() => { | ||
|
|
@@ -955,7 +1071,8 @@ | |
| } | ||
| }) | ||
|
|
||
| const placeholderBottomText = `\n(${t("chat:addContext")}${shouldDisableImages ? `, ${t("chat:dragFiles")}` : `, ${t("chat:dragFilesImages")}`})` | ||
| const dragHint = shouldDisableImages ? t("chat:dragFiles") : t("chat:dragFilesImages") | ||
| const placeholderBottomText = `\n(${t("chat:addContext")}, ${t("chat:historyHint")}, ${dragHint})` | ||
|
|
||
| // Common mode selector handler | ||
| const handleModeChange = useCallback( | ||
|
|
@@ -1039,6 +1156,8 @@ | |
| loading={searchLoading} | ||
| dynamicSearchResults={fileSearchResults} | ||
| commands={commands} | ||
| history={promptHistory} | ||
| isHistoryQuery={menuSource === "history"} | ||
| /> | ||
| </div> | ||
| )} | ||
|
|
@@ -1099,8 +1218,16 @@ | |
| }} | ||
| onFocus={() => setIsFocused(true)} | ||
| onKeyDown={(e) => { | ||
| // Handle ESC to cancel in edit mode | ||
| if (isEditMode && e.key === "Escape" && !e.nativeEvent?.isComposing) { | ||
| // Handle ESC to cancel in edit mode. The context menu | ||
| // takes precedence: while the history picker is open, | ||
| // ESC only closes it, so cancelling the edit (which | ||
| // discards the draft) needs a second press. | ||
| if ( | ||
| isEditMode && | ||
| e.key === "Escape" && | ||
| !showContextMenu && | ||
| !e.nativeEvent?.isComposing | ||
| ) { | ||
| e.preventDefault() | ||
| onCancel?.() | ||
| return | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -30,6 +30,8 @@ | |
| loading?: boolean | ||
| dynamicSearchResults?: SearchResult[] | ||
| commands?: Command[] | ||
| history?: string[] | ||
| isHistoryQuery?: boolean | ||
| } | ||
|
|
||
| const ContextMenu: React.FC<ContextMenuProps> = ({ | ||
|
|
@@ -43,14 +45,25 @@ | |
| modes, | ||
| dynamicSearchResults = [], | ||
| commands = [], | ||
| history = [], | ||
|
Check warning on line 48 in webview-ui/src/components/chat/ContextMenu.tsx
|
||
| isHistoryQuery, | ||
| }) => { | ||
| const [materialIconsBaseUri, setMaterialIconsBaseUri] = useState("") | ||
| const menuRef = useRef<HTMLDivElement>(null) | ||
|
|
||
| const filteredOptions = useMemo(() => { | ||
| return getContextMenuOptions(searchQuery, selectedType, queryItems, dynamicSearchResults, modes, commands) | ||
| }, [searchQuery, selectedType, queryItems, dynamicSearchResults, modes, commands]) | ||
| return getContextMenuOptions( | ||
| searchQuery, | ||
| selectedType, | ||
| queryItems, | ||
| dynamicSearchResults, | ||
| modes, | ||
| commands, | ||
| history, | ||
| isHistoryQuery, | ||
| ) | ||
| }, [searchQuery, selectedType, queryItems, dynamicSearchResults, modes, commands, history, isHistoryQuery]) | ||
|
|
||
|
Check warning on line 66 in webview-ui/src/components/chat/ContextMenu.tsx
|
||
| useEffect(() => { | ||
| if (menuRef.current) { | ||
| const selectedElement = menuRef.current.children[selectedIndex] as HTMLElement | ||
|
|
@@ -138,6 +151,17 @@ | |
| )} | ||
| </div> | ||
| ) | ||
| case ContextMenuOptionType.History: | ||
| // Prompts can be long and multi-line; keep every row to one line so | ||
| // the picker stays scannable. The full text lands in the input on | ||
| // selection, and line breaks become visible there. | ||
| return ( | ||
| <span | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could this new row use Tailwind classes?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done in |
||
| title={option.value} | ||
| className="min-w-0 overflow-hidden text-ellipsis whitespace-nowrap leading-[1.4]"> | ||
| {option.value?.replace(/\s+/g, " ").trim()} | ||
| </span> | ||
|
Check warning on line 163 in webview-ui/src/components/chat/ContextMenu.tsx
|
||
| ) | ||
| case ContextMenuOptionType.Problems: | ||
| return <span>{t("chat:contextMenu.problems")}</span> | ||
| case ContextMenuOptionType.Terminal: | ||
|
|
@@ -230,6 +254,8 @@ | |
| return "link" | ||
| case ContextMenuOptionType.Git: | ||
| return "git-commit" | ||
| case ContextMenuOptionType.History: | ||
| return "history" | ||
| case ContextMenuOptionType.NoResults: | ||
| return "info" | ||
| default: | ||
|
|
@@ -267,6 +293,7 @@ | |
|
|
||
| return ( | ||
| <div | ||
| data-testid="context-menu" | ||
| style={{ | ||
| position: "absolute", | ||
| bottom: "calc(100% - 10px)", | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Close the history picker when an external update replaces its query.
Open the picker with
!fix, then have the extension insert/reviewat the end of the draft. TheinsertTextIntoTextareahandler updates the controlled value to!fix /reviewwithout callinghandleInputChange. The reset effect keeps the picker open because the new value is nonempty. Enter then recalls a match for!fixand replaces the updated draft.Invalidate the history picker when a controlled update no longer matches its active query. Keep it open when the parent echoes the query from the input event. Add a local component test for the external update followed by Enter.