feat: prevent user sending messages during message generation
This commit is contained in:
parent
848797e824
commit
ba2659afd1
@ -19,6 +19,7 @@ import { isEmpty } from 'lodash'
|
|||||||
import SendMessageSetting from './SendMessageSetting'
|
import SendMessageSetting from './SendMessageSetting'
|
||||||
import { useSettings } from '@renderer/hooks/useSettings'
|
import { useSettings } from '@renderer/hooks/useSettings'
|
||||||
import dayjs from 'dayjs'
|
import dayjs from 'dayjs'
|
||||||
|
import { useAppSelector } from '@renderer/store'
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
assistant: Assistant
|
assistant: Assistant
|
||||||
@ -31,9 +32,14 @@ const Inputbar: FC<Props> = ({ assistant, setActiveTopic }) => {
|
|||||||
const { addTopic } = useAssistant(assistant.id)
|
const { addTopic } = useAssistant(assistant.id)
|
||||||
const { sendMessageShortcut } = useSettings()
|
const { sendMessageShortcut } = useSettings()
|
||||||
const [expended, setExpend] = useState(false)
|
const [expended, setExpend] = useState(false)
|
||||||
|
const generating = useAppSelector((state) => state.runtime.generating)
|
||||||
const inputRef = useRef<TextAreaRef>(null)
|
const inputRef = useRef<TextAreaRef>(null)
|
||||||
|
|
||||||
const sendMessage = () => {
|
const sendMessage = () => {
|
||||||
|
if (generating) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
if (isEmpty(text.trim())) {
|
if (isEmpty(text.trim())) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@ -6,6 +6,8 @@ import OpenAI from 'openai'
|
|||||||
import { getAssistantProvider, getDefaultModel, getProviderByModel, getTopNamingModel } from './assistant'
|
import { getAssistantProvider, getDefaultModel, getProviderByModel, getTopNamingModel } from './assistant'
|
||||||
import { takeRight } from 'lodash'
|
import { takeRight } from 'lodash'
|
||||||
import dayjs from 'dayjs'
|
import dayjs from 'dayjs'
|
||||||
|
import store from '@renderer/store'
|
||||||
|
import { setGenerating } from '@renderer/store/runtime'
|
||||||
|
|
||||||
interface FetchChatCompletionParams {
|
interface FetchChatCompletionParams {
|
||||||
messages: Message[]
|
messages: Message[]
|
||||||
@ -29,6 +31,8 @@ export async function fetchChatCompletion({ messages, topic, assistant, onRespon
|
|||||||
const defaultModel = getDefaultModel()
|
const defaultModel = getDefaultModel()
|
||||||
const model = assistant.model || defaultModel
|
const model = assistant.model || defaultModel
|
||||||
|
|
||||||
|
store.dispatch(setGenerating(true))
|
||||||
|
|
||||||
const _message: Message = {
|
const _message: Message = {
|
||||||
id: uuid(),
|
id: uuid(),
|
||||||
role: 'assistant',
|
role: 'assistant',
|
||||||
@ -74,8 +78,8 @@ export async function fetchChatCompletion({ messages, topic, assistant, onRespon
|
|||||||
}
|
}
|
||||||
|
|
||||||
_message.status = 'success'
|
_message.status = 'success'
|
||||||
|
|
||||||
EventEmitter.emit(EVENT_NAMES.AI_CHAT_COMPLETION, _message)
|
EventEmitter.emit(EVENT_NAMES.AI_CHAT_COMPLETION, _message)
|
||||||
|
store.dispatch(setGenerating(false))
|
||||||
|
|
||||||
return _message
|
return _message
|
||||||
}
|
}
|
||||||
|
|||||||
@ -3,10 +3,12 @@ import Logo from '@renderer/assets/images/logo.png'
|
|||||||
|
|
||||||
export interface RuntimeState {
|
export interface RuntimeState {
|
||||||
avatar: string
|
avatar: string
|
||||||
|
generating: boolean
|
||||||
}
|
}
|
||||||
|
|
||||||
const initialState: RuntimeState = {
|
const initialState: RuntimeState = {
|
||||||
avatar: Logo
|
avatar: Logo,
|
||||||
|
generating: false
|
||||||
}
|
}
|
||||||
|
|
||||||
const runtimeSlice = createSlice({
|
const runtimeSlice = createSlice({
|
||||||
@ -15,10 +17,13 @@ const runtimeSlice = createSlice({
|
|||||||
reducers: {
|
reducers: {
|
||||||
setAvatar: (state, action: PayloadAction<string | null>) => {
|
setAvatar: (state, action: PayloadAction<string | null>) => {
|
||||||
state.avatar = action.payload || Logo
|
state.avatar = action.payload || Logo
|
||||||
|
},
|
||||||
|
setGenerating: (state, action: PayloadAction<boolean>) => {
|
||||||
|
state.generating = action.payload
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
export const { setAvatar } = runtimeSlice.actions
|
export const { setAvatar, setGenerating } = runtimeSlice.actions
|
||||||
|
|
||||||
export default runtimeSlice.reducer
|
export default runtimeSlice.reducer
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user