diff --git a/src/renderer/src/pages/home/components/AssistantSettings.tsx b/src/renderer/src/pages/home/components/AssistantSettings.tsx index 21fe6eaf..e100fc19 100644 --- a/src/renderer/src/pages/home/components/AssistantSettings.tsx +++ b/src/renderer/src/pages/home/components/AssistantSettings.tsx @@ -1,6 +1,6 @@ import { QuestionCircleOutlined } from '@ant-design/icons' import { DEFAULT_CONEXTCOUNT, DEFAULT_TEMPERATURE } from '@renderer/config/constant' -import { useAssistants } from '@renderer/hooks/useAssistant' +import { useAssistant, useAssistants } from '@renderer/hooks/useAssistant' import { Assistant } from '@renderer/types' import { Button, Col, InputNumber, Popover, Row, Slider, Tooltip } from 'antd' import { debounce } from 'lodash' @@ -12,10 +12,11 @@ interface Props { assistant: Assistant } -const PopoverContent: FC = ({ assistant }) => { +const PopoverContent: FC = (props) => { + const { assistant } = useAssistant(props.assistant.id) const { updateAssistant } = useAssistants() - const [temperature, setTemperature] = useState(assistant.settings?.temperature ?? DEFAULT_TEMPERATURE) - const [contextCount, setConextCount] = useState(assistant.settings?.contextCount ?? DEFAULT_CONEXTCOUNT) + const [temperature, setTemperature] = useState(assistant?.settings?.temperature ?? DEFAULT_TEMPERATURE) + const [contextCount, setConextCount] = useState(assistant?.settings?.contextCount ?? DEFAULT_CONEXTCOUNT) const { t } = useTranslation() const onUpdateAssistantSettings = useCallback( @@ -64,8 +65,8 @@ const PopoverContent: FC = ({ assistant }) => { } useEffect(() => { - setTemperature(assistant.settings?.temperature ?? DEFAULT_TEMPERATURE) - setConextCount(assistant.settings?.contextCount ?? DEFAULT_CONEXTCOUNT) + setTemperature(assistant?.settings?.temperature ?? DEFAULT_TEMPERATURE) + setConextCount(assistant?.settings?.contextCount ?? DEFAULT_CONEXTCOUNT) }, [assistant]) return ( diff --git a/src/renderer/src/pages/home/components/Assistants.tsx b/src/renderer/src/pages/home/components/Assistants.tsx index 846cc80c..35af3bdd 100644 --- a/src/renderer/src/pages/home/components/Assistants.tsx +++ b/src/renderer/src/pages/home/components/Assistants.tsx @@ -5,9 +5,10 @@ import { useAssistants } from '@renderer/hooks/useAssistant' import { getDefaultTopic } from '@renderer/services/assistant' import { Assistant } from '@renderer/types' import { droppableReorder, uuid } from '@renderer/utils' -import { Dropdown, MenuProps } from 'antd' +import { Dropdown } from 'antd' +import { ItemType } from 'antd/es/menu/interface' import { last } from 'lodash' -import { FC, useRef } from 'react' +import { FC } from 'react' import { useTranslation } from 'react-i18next' import styled from 'styled-components' @@ -19,7 +20,6 @@ interface Props { const Assistants: FC = ({ activeAssistant, setActiveAssistant, onCreateAssistant }) => { const { assistants, removeAssistant, updateAssistant, addAssistant, updateAssistants } = useAssistants() - const targetAssistant = useRef(null) const { t } = useTranslation() @@ -29,39 +29,36 @@ const Assistants: FC = ({ activeAssistant, setActiveAssistant, onCreateAs removeAssistant(assistant.id) } - const items: MenuProps['items'] = [ - { - label: t('common.edit'), - key: 'edit', - icon: , - async onClick() { - if (targetAssistant.current) { - const _assistant = await AssistantSettingPopup.show({ assistant: targetAssistant.current }) + const getMenuItems = (assistant: Assistant) => + [ + { + label: t('common.edit'), + key: 'edit', + icon: , + async onClick() { + const _assistant = await AssistantSettingPopup.show({ assistant }) updateAssistant(_assistant) } + }, + { + label: t('common.duplicate'), + key: 'duplicate', + icon: , + onClick: async () => { + const _assistant: Assistant = { ...assistant, id: uuid(), topics: [getDefaultTopic()] } + addAssistant(_assistant) + setActiveAssistant(_assistant) + } + }, + { type: 'divider' }, + { + label: t('common.delete'), + key: 'delete', + icon: , + danger: true, + onClick: () => onDelete(assistant) } - }, - { - label: t('common.duplicate'), - key: 'duplicate', - icon: , - async onClick() { - const assistant: Assistant = { ...activeAssistant, id: uuid(), topics: [getDefaultTopic()] } - addAssistant(assistant) - setActiveAssistant(assistant) - } - }, - { type: 'divider' }, - { - label: t('common.delete'), - key: 'delete', - icon: , - danger: true, - onClick: () => { - targetAssistant.current && onDelete(targetAssistant.current) - } - } - ] + ] as ItemType[] const onDragEnd = (result: DropResult) => { if (result.destination) { @@ -82,11 +79,7 @@ const Assistants: FC = ({ activeAssistant, setActiveAssistant, onCreateAs {(provided) => (
- (targetAssistant.current = assistant)}> + setActiveAssistant(assistant)} className={assistant.id === activeAssistant?.id ? 'active' : ''}> diff --git a/src/renderer/src/pages/home/components/Messages.tsx b/src/renderer/src/pages/home/components/Messages.tsx index 4b34ed87..528e3287 100644 --- a/src/renderer/src/pages/home/components/Messages.tsx +++ b/src/renderer/src/pages/home/components/Messages.tsx @@ -65,7 +65,6 @@ const Messages: FC = ({ assistant, topic }) => { useEffect(() => { const unsubscribes = [ EventEmitter.on(EVENT_NAMES.SEND_MESSAGE, async (msg: Message) => { - console.debug({ assistant, provider, message: msg, topic }) onSendMessage(msg) fetchChatCompletion({ assistant, messages: [...messages, msg], topic, onResponse: setLastMessage }) }), diff --git a/src/renderer/src/pages/home/components/Topics.tsx b/src/renderer/src/pages/home/components/Topics.tsx index b7882202..4eb07aac 100644 --- a/src/renderer/src/pages/home/components/Topics.tsx +++ b/src/renderer/src/pages/home/components/Topics.tsx @@ -4,7 +4,7 @@ import { useShowRightSidebar } from '@renderer/hooks/useStore' import { fetchMessagesSummary } from '@renderer/services/api' import { Assistant, Topic } from '@renderer/types' import { Button, Dropdown, MenuProps, Popconfirm } from 'antd' -import { FC, useRef } from 'react' +import { FC } from 'react' import styled from 'styled-components' import { DeleteOutlined, EditOutlined, SignatureOutlined } from '@ant-design/icons' import LocalStorage from '@renderer/services/storage' @@ -21,57 +21,57 @@ interface Props { const Topics: FC = ({ assistant, activeTopic, setActiveTopic }) => { const { showRightSidebar } = useShowRightSidebar() const { removeTopic, updateTopic, removeAllTopics, updateTopics } = useAssistant(assistant.id) - const currentTopic = useRef(null) const { t } = useTranslation() - const topicMenuItems: MenuProps['items'] = [ - { - label: t('assistant.topics.auto_rename'), - key: 'auto-rename', - icon: , - async onClick() { - if (currentTopic.current) { - const messages = await LocalStorage.getTopicMessages(currentTopic.current.id) + const getTopicMenuItems = (topic: Topic) => { + const menus: MenuProps['items'] = [ + { + label: t('assistant.topics.auto_rename'), + key: 'auto-rename', + icon: , + async onClick() { + const messages = await LocalStorage.getTopicMessages(topic.id) if (messages.length >= 2) { const summaryText = await fetchMessagesSummary({ messages, assistant }) if (summaryText) { - updateTopic({ ...currentTopic.current, name: summaryText }) + updateTopic({ ...topic, name: summaryText }) } } } - } - }, - { - label: t('common.rename'), - key: 'rename', - icon: , - async onClick() { - const name = await PromptPopup.show({ - title: t('assistant.topics.edit.title'), - message: t('assistant.topics.edit.placeholder'), - defaultValue: currentTopic.current?.name || '' - }) - if (name && currentTopic.current && currentTopic.current?.name !== name) { - updateTopic({ ...currentTopic.current, name }) + }, + { + label: t('common.rename'), + key: 'rename', + icon: , + async onClick() { + const name = await PromptPopup.show({ + title: t('assistant.topics.edit.title'), + message: t('assistant.topics.edit.placeholder'), + defaultValue: topic?.name || '' + }) + if (name && topic?.name !== name) { + updateTopic({ ...topic, name }) + } } } - } - ] + ] - if (assistant.topics.length > 1) { - topicMenuItems.push({ type: 'divider' }) - topicMenuItems.push({ - label: t('common.delete'), - danger: true, - key: 'delete', - icon: , - onClick() { - if (assistant.topics.length === 1) return - currentTopic.current && removeTopic(currentTopic.current) - currentTopic.current = null - setActiveTopic(assistant.topics[0]) - } - }) + if (assistant.topics.length > 1) { + menus.push({ type: 'divider' }) + menus.push({ + label: t('common.delete'), + danger: true, + key: 'delete', + icon: , + onClick() { + if (assistant.topics.length === 1) return + removeTopic(topic) + setActiveTopic(assistant.topics[0]) + } + }) + } + + return menus } const onDragEnd = (result: DropResult) => { @@ -108,11 +108,7 @@ const Topics: FC = ({ assistant, activeTopic, setActiveTopic }) => { {(provided) => (
- open && (currentTopic.current = topic)}> + setActiveTopic(topic)}> diff --git a/src/renderer/src/services/ProviderSDK.ts b/src/renderer/src/services/ProviderSDK.ts index 038a43ec..9d2cca93 100644 --- a/src/renderer/src/services/ProviderSDK.ts +++ b/src/renderer/src/services/ProviderSDK.ts @@ -47,7 +47,7 @@ export default class ProviderSDK { model: model.id, messages: [systemMessage, ...userMessages].filter(Boolean) as MessageParam[], max_tokens: 4096, - temperature: assistant.settings?.temperature + temperature: assistant?.settings?.temperature }) .on('text', (text) => onChunk({ text: text || '' })) .on('finalMessage', (message) => @@ -64,7 +64,7 @@ export default class ProviderSDK { model: model.id, messages: [systemMessage, ...userMessages].filter(Boolean) as ChatCompletionMessageParam[], stream: true, - temperature: assistant.settings?.temperature + temperature: assistant?.settings?.temperature }) for await (const chunk of stream) { if (window.keyv.get(EVENT_NAMES.CHAT_COMPLETION_PAUSED)) break diff --git a/src/renderer/src/utils/index.ts b/src/renderer/src/utils/index.ts index 03c97af9..12106c3b 100644 --- a/src/renderer/src/utils/index.ts +++ b/src/renderer/src/utils/index.ts @@ -169,10 +169,10 @@ export function getFirstCharacter(str) { } export const getAssistantSettings = (assistant: Assistant): AssistantSettings => { - const contextCount = assistant.settings?.contextCount ?? DEFAULT_CONEXTCOUNT + const contextCount = assistant?.settings?.contextCount ?? DEFAULT_CONEXTCOUNT return { contextCount: contextCount === 20 ? 100000 : contextCount, - temperature: assistant.settings?.temperature ?? DEFAULT_TEMPERATURE + temperature: assistant?.settings?.temperature ?? DEFAULT_TEMPERATURE } }