fix: create assistant causing blank screen (#10853)

* fix: create or update assistant causing blank screen

* fix: remove redundant type annotation

* fix: improve logging

* fix: remove redundant check

* fix(migration): move presets initialization to migration 166

The initialization of assistants.presets was incorrectly placed in migration 164. Move it to a new migration 166 to ensure proper state initialization after versions 1.6.5 and 1.7.0-beta.2.

---------

Co-authored-by: icarus <eurfelux@gmail.com>
This commit is contained in:
defi-failure 2025-10-25 21:10:37 +08:00 committed by GitHub
parent ac4aa33e79
commit e69fd7f22b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 42 additions and 6 deletions

View File

@ -1,3 +1,4 @@
import { loggerService } from '@logger'
import { useAppDispatch, useAppSelector } from '@renderer/store' import { useAppDispatch, useAppSelector } from '@renderer/store'
import { import {
addAssistantPreset, addAssistantPreset,
@ -8,8 +9,22 @@ import {
} from '@renderer/store/assistants' } from '@renderer/store/assistants'
import { AssistantPreset, AssistantSettings } from '@renderer/types' import { AssistantPreset, AssistantSettings } from '@renderer/types'
const logger = loggerService.withContext('useAssistantPresets')
function ensurePresetsArray(storedPresets: unknown): AssistantPreset[] {
if (Array.isArray(storedPresets)) {
return storedPresets
}
logger.warn('Unexpected data type from state.assistants.presets, falling back to empty list.', {
type: typeof storedPresets,
value: storedPresets
})
return []
}
export function useAssistantPresets() { export function useAssistantPresets() {
const presets = useAppSelector((state) => state.assistants.presets) const storedPresets = useAppSelector((state) => state.assistants.presets)
const presets = ensurePresetsArray(storedPresets)
const dispatch = useAppDispatch() const dispatch = useAppDispatch()
return { return {
@ -21,14 +36,23 @@ export function useAssistantPresets() {
} }
export function useAssistantPreset(id: string) { export function useAssistantPreset(id: string) {
// FIXME: undefined is not handled const storedPresets = useAppSelector((state) => state.assistants.presets)
const preset = useAppSelector((state) => state.assistants.presets.find((a) => a.id === id) as AssistantPreset) const presets = ensurePresetsArray(storedPresets)
const preset = presets.find((a) => a.id === id)
const dispatch = useAppDispatch() const dispatch = useAppDispatch()
if (!preset) {
logger.warn(`Assistant preset with id ${id} not found in state.`)
}
return { return {
preset, preset: preset,
updateAssistantPreset: (preset: AssistantPreset) => dispatch(updateAssistantPreset(preset)), updateAssistantPreset: (preset: AssistantPreset) => dispatch(updateAssistantPreset(preset)),
updateAssistantPresetSettings: (settings: Partial<AssistantSettings>) => { updateAssistantPresetSettings: (settings: Partial<AssistantSettings>) => {
if (!preset) {
logger.warn(`Failed to update assistant preset settings because preset with id ${id} is missing.`)
return
}
dispatch(updateAssistantPresetSettings({ assistantId: preset.id, settings })) dispatch(updateAssistantPresetSettings({ assistantId: preset.id, settings }))
} }
} }

View File

@ -43,7 +43,7 @@ const AssistantSettingPopupContainer: React.FC<Props> = ({ resolve, tab, ...prop
const _useAgent = useAssistantPreset(props.assistant.id) const _useAgent = useAssistantPreset(props.assistant.id)
const isAgent = props.assistant.type === 'agent' const isAgent = props.assistant.type === 'agent'
const assistant = isAgent ? _useAgent.preset : _useAssistant.assistant const assistant = isAgent ? (_useAgent.preset ?? props.assistant) : _useAssistant.assistant
const updateAssistant = isAgent ? _useAgent.updateAssistantPreset : _useAssistant.updateAssistant const updateAssistant = isAgent ? _useAgent.updateAssistantPreset : _useAssistant.updateAssistant
const updateAssistantSettings = isAgent const updateAssistantSettings = isAgent
? _useAgent.updateAssistantPresetSettings ? _useAgent.updateAssistantPresetSettings

View File

@ -65,7 +65,7 @@ const persistedReducer = persistReducer(
{ {
key: 'cherry-studio', key: 'cherry-studio',
storage, storage,
version: 165, version: 166,
blacklist: ['runtime', 'messages', 'messageBlocks', 'tabs'], blacklist: ['runtime', 'messages', 'messageBlocks', 'tabs'],
migrate migrate
}, },

View File

@ -2701,6 +2701,18 @@ const migrateConfig = {
logger.error('migrate 165 error', error as Error) logger.error('migrate 165 error', error as Error)
return state return state
} }
},
'166': (state: RootState) => {
// added after 1.6.5 and 1.7.0-beta.2
try {
if (state.assistants.presets === undefined) {
state.assistants.presets = []
}
return state
} catch (error) {
logger.error('migrate 166 error', error as Error)
return state
}
} }
} }