fix: distinguish model mention sources

This commit is contained in:
one 2025-03-06 20:31:21 +08:00 committed by 亢奋猫
parent 219dc2c8bf
commit e066db763a
2 changed files with 25 additions and 11 deletions

View File

@ -102,6 +102,8 @@ const Inputbar: FC<Props> = ({ assistant: _assistant, setActiveTopic }) => {
const [tokenCount, setTokenCount] = useState(0)
const [mentionFromKeyboard, setMentionFromKeyboard] = useState(false)
const debouncedEstimate = useCallback(
debounce((newText) => {
if (showInputEstimatedTokens) {
@ -192,6 +194,7 @@ const Inputbar: FC<Props> = ({ assistant: _assistant, setActiveTopic }) => {
const cursorPosition = textArea.selectionStart
const textBeforeCursor = text.substring(0, cursorPosition)
if (cursorPosition === 0 || textBeforeCursor.endsWith(' ')) {
setMentionFromKeyboard(true)
EventEmitter.emit(EVENT_NAMES.SHOW_MODEL_SELECTOR)
setIsMentionPopupOpen(true)
return
@ -557,9 +560,10 @@ const Inputbar: FC<Props> = ({ assistant: _assistant, setActiveTopic }) => {
setSelectedKnowledgeBases(bases ?? [])
}
const onMentionModel = (model: Model) => {
const onMentionModel = (model: Model, fromKeyboard: boolean = false) => {
const textArea = textareaRef.current?.resizableTextArea?.textArea
if (textArea) {
if (fromKeyboard) {
const cursorPosition = textArea.selectionStart
const textBeforeCursor = text.substring(0, cursorPosition)
const lastAtIndex = textBeforeCursor.lastIndexOf('@')
@ -568,12 +572,14 @@ const Inputbar: FC<Props> = ({ assistant: _assistant, setActiveTopic }) => {
const newText = text.substring(0, lastAtIndex) + text.substring(cursorPosition)
setText(newText)
}
}
setMentionModels((prev) => [...prev, model])
setIsMentionPopupOpen(false)
setTimeout(() => {
textareaRef.current?.focus()
}, 0)
setMentionFromKeyboard(false)
}
}
@ -672,7 +678,7 @@ const Inputbar: FC<Props> = ({ assistant: _assistant, setActiveTopic }) => {
</Tooltip>
<MentionModelsButton
mentionModels={mentionModels}
onMentionModel={onMentionModel}
onMentionModel={(model) => onMentionModel(model, mentionFromKeyboard)}
ToolbarButton={ToolbarButton}
/>
<Tooltip placement="top" title={t('chat.input.web_search')} arrow>

View File

@ -14,7 +14,7 @@ import styled, { createGlobalStyle } from 'styled-components'
interface Props {
mentionModels: Model[]
onMentionModel: (model: Model) => void
onMentionModel: (model: Model, fromKeyboard?: boolean) => void
ToolbarButton: any
}
@ -30,6 +30,8 @@ const MentionModelsButton: FC<Props> = ({ mentionModels, onMentionModel: onSelec
const itemRefs = useRef<Array<HTMLDivElement | null>>([])
// Add a new state to track if menu was dismissed
const [menuDismissed, setMenuDismissed] = useState(false)
// Add a state to track if the model selector was triggered by keyboard
const [fromKeyboard, setFromKeyboard] = useState(false)
const setItemRef = (index: number, el: HTMLDivElement | null) => {
itemRefs.current[index] = el
@ -49,7 +51,7 @@ const MentionModelsButton: FC<Props> = ({ mentionModels, onMentionModel: onSelec
if (mentionModels.some((selected) => getModelUniqId(selected) === getModelUniqId(model))) {
return
}
onSelect(model)
onSelect(model, fromKeyboard)
setIsOpen(false)
}
@ -190,6 +192,7 @@ const MentionModelsButton: FC<Props> = ({ mentionModels, onMentionModel: onSelec
setSelectedIndex(0)
setSearchText('')
setMenuDismissed(false) // Reset dismissed flag when manually showing selector
setFromKeyboard(true) // Set fromKeyboard to true when triggered by keyboard
}
const handleKeyDown = (e: KeyboardEvent) => {
@ -307,7 +310,12 @@ const MentionModelsButton: FC<Props> = ({ mentionModels, onMentionModel: onSelec
dropdownRender={() => menu}
trigger={['click']}
open={isOpen}
onOpenChange={setIsOpen}
onOpenChange={(open) => {
setIsOpen(open)
if (open) {
setFromKeyboard(false) // Set fromKeyboard to false when opened by button click
}
}}
overlayClassName="mention-models-dropdown">
<Tooltip placement="top" title={t('agents.edit.model.select.title')} arrow>
<ToolbarButton type="text" ref={dropdownRef}>