mirror of
https://github.com/CherryHQ/cherry-studio.git
synced 2025-12-25 11:20:07 +08:00
Revert "fix(hooks): type safe useAssistant (#9428)"
This reverts commit 4833f36e0b.
https://github.com/CherryHQ/cherry-studio/pull/9428#issuecomment-3222381185
This commit is contained in:
parent
7bb3826cdd
commit
f9869ef453
@ -7,10 +7,10 @@ import {
|
||||
MODEL_SUPPORTED_REASONING_EFFORT
|
||||
} from '@renderer/config/models'
|
||||
import { db } from '@renderer/databases'
|
||||
import { getDefaultAssistant, getDefaultTopic } from '@renderer/services/AssistantService'
|
||||
import { getDefaultTopic } from '@renderer/services/AssistantService'
|
||||
import { useAppDispatch, useAppSelector } from '@renderer/store'
|
||||
import {
|
||||
addAssistant as _addAssistant,
|
||||
addAssistant,
|
||||
addTopic,
|
||||
insertAssistant,
|
||||
removeAllTopics,
|
||||
@ -27,7 +27,6 @@ import {
|
||||
import { setDefaultModel, setQuickModel, setTranslateModel } from '@renderer/store/llm'
|
||||
import { Assistant, AssistantSettings, Model, ThinkingOption, Topic } from '@renderer/types'
|
||||
import { uuid } from '@renderer/utils'
|
||||
import { formatErrorMessage } from '@renderer/utils/error'
|
||||
import { useCallback, useEffect, useMemo, useRef } from 'react'
|
||||
import { useTranslation } from 'react-i18next'
|
||||
|
||||
@ -39,25 +38,10 @@ export function useAssistants() {
|
||||
const dispatch = useAppDispatch()
|
||||
const logger = loggerService.withContext('useAssistants')
|
||||
|
||||
/**
|
||||
* 添加一个新的助手
|
||||
* @param assistant - 要添加的助手对象
|
||||
* @throws {Error} 如果添加助手失败会抛出错误
|
||||
*/
|
||||
const addAssistant = (assistant: Assistant) => {
|
||||
try {
|
||||
dispatch(_addAssistant(assistant))
|
||||
} catch (e) {
|
||||
logger.error('Failed to add assistant', e as Error)
|
||||
window.message.error(t('assistants.error.add' + ': ' + formatErrorMessage(e)))
|
||||
throw e
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
assistants,
|
||||
updateAssistants: (assistants: Assistant[]) => dispatch(updateAssistants(assistants)),
|
||||
addAssistant,
|
||||
addAssistant: (assistant: Assistant) => dispatch(addAssistant(assistant)),
|
||||
insertAssistant: (index: number, assistant: Assistant) => dispatch(insertAssistant({ index, assistant })),
|
||||
copyAssistant: (assistant: Assistant): Assistant | undefined => {
|
||||
if (!assistant) {
|
||||
@ -68,7 +52,7 @@ export function useAssistants() {
|
||||
const _assistant: Assistant = { ...assistant, id: uuid(), topics: [getDefaultTopic(assistant.id)] }
|
||||
if (index === -1) {
|
||||
logger.warn("Origin assistant's id not found. Fallback to addAssistant.")
|
||||
addAssistant(_assistant)
|
||||
dispatch(addAssistant(_assistant))
|
||||
} else {
|
||||
// 插入到后面
|
||||
try {
|
||||
@ -90,22 +74,7 @@ export function useAssistants() {
|
||||
}
|
||||
|
||||
export function useAssistant(id: string) {
|
||||
let assistant = useAppSelector((state) => state.assistants.assistants.find((a) => a.id === id))
|
||||
const { addAssistant } = useAssistants()
|
||||
const { t } = useTranslation()
|
||||
|
||||
if (!assistant) {
|
||||
window.message.warning(t('warning.missing_assistant'))
|
||||
const newAssistant = { ...getDefaultAssistant(), id }
|
||||
try {
|
||||
addAssistant(newAssistant)
|
||||
assistant = newAssistant
|
||||
} catch (e) {
|
||||
window.message.warning(t('warning.fallback.deafult_assistant'))
|
||||
assistant = getDefaultAssistant()
|
||||
}
|
||||
}
|
||||
|
||||
const assistant = useAppSelector((state) => state.assistants.assistants.find((a) => a.id === id) as Assistant)
|
||||
const dispatch = useAppDispatch()
|
||||
const { defaultModel } = useDefaultModel()
|
||||
|
||||
@ -119,7 +88,7 @@ export function useAssistant(id: string) {
|
||||
const settingsRef = useRef(assistant?.settings)
|
||||
|
||||
useEffect(() => {
|
||||
settingsRef.current = assistant?.settings
|
||||
settingsRef.current = assistant.settings
|
||||
}, [assistant?.settings])
|
||||
|
||||
const updateAssistantSettings = useCallback(
|
||||
|
||||
@ -148,9 +148,6 @@
|
||||
"edit": {
|
||||
"title": "Edit Assistant"
|
||||
},
|
||||
"error": {
|
||||
"add": "Failed to add assistant"
|
||||
},
|
||||
"icon": {
|
||||
"type": "Assistant Icon"
|
||||
},
|
||||
@ -3852,10 +3849,6 @@
|
||||
"title": "Update"
|
||||
},
|
||||
"warning": {
|
||||
"fallback": {
|
||||
"deafult_assistant": "Reverted to default assistant, which may cause issues"
|
||||
},
|
||||
"missing_assistant": "Assistant does not exist",
|
||||
"missing_provider": "The supplier does not exist; reverted to the default supplier {{provider}}. This may cause issues."
|
||||
},
|
||||
"words": {
|
||||
|
||||
@ -148,9 +148,6 @@
|
||||
"edit": {
|
||||
"title": "アシスタントを編集"
|
||||
},
|
||||
"error": {
|
||||
"add": "アシスタントの追加に失敗しました"
|
||||
},
|
||||
"icon": {
|
||||
"type": "アシスタントアイコン"
|
||||
},
|
||||
@ -3852,10 +3849,6 @@
|
||||
"title": "更新"
|
||||
},
|
||||
"warning": {
|
||||
"fallback": {
|
||||
"deafult_assistant": "既定のアシスタントに戻されました。これにより問題が発生する可能性があります。"
|
||||
},
|
||||
"missing_assistant": "アシスタントが存在しません",
|
||||
"missing_provider": "サプライヤーが存在しないため、デフォルトのサプライヤー {{provider}} にロールバックされました。これにより問題が発生する可能性があります。"
|
||||
},
|
||||
"words": {
|
||||
|
||||
@ -148,9 +148,6 @@
|
||||
"edit": {
|
||||
"title": "Редактировать ассистента"
|
||||
},
|
||||
"error": {
|
||||
"add": "Не удалось добавить помощника"
|
||||
},
|
||||
"icon": {
|
||||
"type": "Иконка ассистента"
|
||||
},
|
||||
@ -3852,10 +3849,6 @@
|
||||
"title": "Обновление"
|
||||
},
|
||||
"warning": {
|
||||
"fallback": {
|
||||
"deafult_assistant": "Возвращено к помощнику по умолчанию, что может привести к проблемам"
|
||||
},
|
||||
"missing_assistant": "Ассистент не существует",
|
||||
"missing_provider": "Поставщик не существует, возвращение к поставщику по умолчанию {{provider}}. Это может привести к проблемам."
|
||||
},
|
||||
"words": {
|
||||
|
||||
@ -148,9 +148,6 @@
|
||||
"edit": {
|
||||
"title": "编辑助手"
|
||||
},
|
||||
"error": {
|
||||
"add": "添加助手失败"
|
||||
},
|
||||
"icon": {
|
||||
"type": "助手图标"
|
||||
},
|
||||
@ -3852,10 +3849,6 @@
|
||||
"title": "更新提示"
|
||||
},
|
||||
"warning": {
|
||||
"fallback": {
|
||||
"deafult_assistant": "已回退到默认助手,这可能导致问题"
|
||||
},
|
||||
"missing_assistant": "助手不存在",
|
||||
"missing_provider": "供应商不存在,已回退到默认供应商 {{provider}}。这可能导致问题。"
|
||||
},
|
||||
"words": {
|
||||
|
||||
@ -148,9 +148,6 @@
|
||||
"edit": {
|
||||
"title": "編輯助手"
|
||||
},
|
||||
"error": {
|
||||
"add": "添加助手失敗"
|
||||
},
|
||||
"icon": {
|
||||
"type": "助手圖示"
|
||||
},
|
||||
@ -3852,10 +3849,6 @@
|
||||
"title": "更新提示"
|
||||
},
|
||||
"warning": {
|
||||
"fallback": {
|
||||
"deafult_assistant": "已回退到預設助手,這可能導致問題"
|
||||
},
|
||||
"missing_assistant": "助手不存在",
|
||||
"missing_provider": "供應商不存在,已回退到預設供應商 {{provider}}。這可能導致問題。"
|
||||
},
|
||||
"words": {
|
||||
|
||||
@ -148,9 +148,6 @@
|
||||
"edit": {
|
||||
"title": "Επεξεργασία βοηθού"
|
||||
},
|
||||
"error": {
|
||||
"add": "Αποτυχία προσθήκης βοηθού"
|
||||
},
|
||||
"icon": {
|
||||
"type": "Εικόνα Βοηθού"
|
||||
},
|
||||
@ -2719,7 +2716,6 @@
|
||||
"title": "Αυτόματη ενημέρωση"
|
||||
},
|
||||
"avatar": {
|
||||
"builtin": "ενσωματωμένο avatar",
|
||||
"reset": "Επαναφορά εικονιδίου"
|
||||
},
|
||||
"backup": {
|
||||
@ -3852,10 +3848,6 @@
|
||||
"title": "Ενημέρωση"
|
||||
},
|
||||
"warning": {
|
||||
"fallback": {
|
||||
"deafult_assistant": "Επαναφέρθηκε στον προεπιλεγμένο βοηθό, γεγονός που ενδέχεται να προκαλέσει προβλήματα"
|
||||
},
|
||||
"missing_assistant": "Ο βοηθός δεν υπάρχει",
|
||||
"missing_provider": "Ο προμηθευτής δεν υπάρχει, έγινε επαναφορά στον προεπιλεγμένο προμηθευτή {{provider}}. Αυτό μπορεί να προκαλέσει προβλήματα."
|
||||
},
|
||||
"words": {
|
||||
|
||||
@ -148,9 +148,6 @@
|
||||
"edit": {
|
||||
"title": "Editar Asistente"
|
||||
},
|
||||
"error": {
|
||||
"add": "Error al agregar asistente"
|
||||
},
|
||||
"icon": {
|
||||
"type": "Ícono del Asistente"
|
||||
},
|
||||
@ -2719,7 +2716,6 @@
|
||||
"title": "Actualización automática"
|
||||
},
|
||||
"avatar": {
|
||||
"builtin": "Avatares integrados",
|
||||
"reset": "Restablecer avatar"
|
||||
},
|
||||
"backup": {
|
||||
@ -3852,10 +3848,6 @@
|
||||
"title": "Actualización"
|
||||
},
|
||||
"warning": {
|
||||
"fallback": {
|
||||
"deafult_assistant": "Se ha revertido al asistente predeterminado, lo que podría causar problemas"
|
||||
},
|
||||
"missing_assistant": "El asistente no existe",
|
||||
"missing_provider": "El proveedor no existe, se ha revertido al proveedor predeterminado {{provider}}. Esto podría causar problemas."
|
||||
},
|
||||
"words": {
|
||||
|
||||
@ -148,9 +148,6 @@
|
||||
"edit": {
|
||||
"title": "Modifier l'Aide"
|
||||
},
|
||||
"error": {
|
||||
"add": "Échec de l'ajout de l'assistant"
|
||||
},
|
||||
"icon": {
|
||||
"type": "Icône de l'assistant"
|
||||
},
|
||||
@ -2719,7 +2716,6 @@
|
||||
"title": "Mise à jour automatique"
|
||||
},
|
||||
"avatar": {
|
||||
"builtin": "Avatar intégré",
|
||||
"reset": "Réinitialiser l'avatar"
|
||||
},
|
||||
"backup": {
|
||||
@ -3852,10 +3848,6 @@
|
||||
"title": "Mise à jour"
|
||||
},
|
||||
"warning": {
|
||||
"fallback": {
|
||||
"deafult_assistant": "Revenu à l'assistant par défaut, ce qui pourrait entraîner des problèmes"
|
||||
},
|
||||
"missing_assistant": "L'assistant n'existe pas",
|
||||
"missing_provider": "Le fournisseur n’existe pas, retour au fournisseur par défaut {{provider}}. Cela peut entraîner des problèmes."
|
||||
},
|
||||
"words": {
|
||||
|
||||
@ -148,9 +148,6 @@
|
||||
"edit": {
|
||||
"title": "Editar Assistente"
|
||||
},
|
||||
"error": {
|
||||
"add": "Falha ao adicionar assistente"
|
||||
},
|
||||
"icon": {
|
||||
"type": "Ícone do Assistente"
|
||||
},
|
||||
@ -2719,7 +2716,6 @@
|
||||
"title": "Atualização automática"
|
||||
},
|
||||
"avatar": {
|
||||
"builtin": "Avatares integrados",
|
||||
"reset": "Redefinir avatar"
|
||||
},
|
||||
"backup": {
|
||||
@ -3852,10 +3848,6 @@
|
||||
"title": "Atualização"
|
||||
},
|
||||
"warning": {
|
||||
"fallback": {
|
||||
"deafult_assistant": "Voltou ao assistente padrão, o que pode causar problemas"
|
||||
},
|
||||
"missing_assistant": "O assistente não existe",
|
||||
"missing_provider": "O fornecedor não existe; foi revertido para o fornecedor predefinido {{provider}}. Isto pode causar problemas."
|
||||
},
|
||||
"words": {
|
||||
|
||||
@ -32,13 +32,7 @@ const assistantsSlice = createSlice({
|
||||
state.assistants = action.payload
|
||||
},
|
||||
addAssistant: (state, action: PayloadAction<Assistant>) => {
|
||||
const newAssistant = action.payload
|
||||
const existing = state.assistants.find((item) => item.id === newAssistant.id)
|
||||
if (!existing) {
|
||||
state.assistants.push(action.payload)
|
||||
} else {
|
||||
throw new Error('Assistant with this ID already exists')
|
||||
}
|
||||
state.assistants.push(action.payload)
|
||||
},
|
||||
insertAssistant: (state, action: PayloadAction<{ index: number; assistant: Assistant }>) => {
|
||||
const { index, assistant } = action.payload
|
||||
|
||||
Loading…
Reference in New Issue
Block a user