mirror of
https://github.com/CherryHQ/cherry-studio.git
synced 2026-01-09 23:10:20 +08:00
feat(useSession): add optimistic updates for message creation
Implement optimistic UI updates when creating new messages to improve perceived performance. The changes include cloning the current session data, adding a draft message immediately, and handling rollback on error.
This commit is contained in:
parent
01c7e509fd
commit
e45231376c
@ -1,4 +1,5 @@
|
|||||||
import { UpdateSessionForm } from '@renderer/types'
|
import { AgentSessionMessageEntity, UpdateSessionForm } from '@renderer/types'
|
||||||
|
import { cloneDeep } from 'lodash'
|
||||||
import { useCallback } from 'react'
|
import { useCallback } from 'react'
|
||||||
import { useTranslation } from 'react-i18next'
|
import { useTranslation } from 'react-i18next'
|
||||||
import useSWR from 'swr'
|
import useSWR from 'swr'
|
||||||
@ -31,17 +32,40 @@ export const useSession = (agentId: string, sessionId: string) => {
|
|||||||
|
|
||||||
const createSessionMessage = useCallback(
|
const createSessionMessage = useCallback(
|
||||||
async (content: string) => {
|
async (content: string) => {
|
||||||
if (!agentId || !sessionId) return
|
if (!agentId || !sessionId || !data) return
|
||||||
|
const origin = cloneDeep(data)
|
||||||
|
const newMessageDraft = {
|
||||||
|
id: -1,
|
||||||
|
session_id: '',
|
||||||
|
role: 'user',
|
||||||
|
content: {
|
||||||
|
role: 'user',
|
||||||
|
content: content,
|
||||||
|
providerOptions: undefined
|
||||||
|
},
|
||||||
|
agent_session_id: '',
|
||||||
|
created_at: '',
|
||||||
|
updated_at: ''
|
||||||
|
} satisfies AgentSessionMessageEntity
|
||||||
try {
|
try {
|
||||||
|
mutate((prev) => ({
|
||||||
|
...prev,
|
||||||
|
accessible_paths: prev?.accessible_paths ?? [],
|
||||||
|
model: prev?.model ?? '',
|
||||||
|
id: prev?.id ?? '',
|
||||||
|
agent_id: prev?.id ?? '',
|
||||||
|
agent_type: prev?.agent_type ?? 'claude-code',
|
||||||
|
created_at: prev?.created_at ?? '',
|
||||||
|
updated_at: prev?.updated_at ?? '',
|
||||||
|
messages: [...(prev?.messages ?? []), newMessageDraft]
|
||||||
|
}))
|
||||||
await client.createMessage(agentId, sessionId, content)
|
await client.createMessage(agentId, sessionId, content)
|
||||||
// TODO: Can you return a created message value?
|
|
||||||
const result = await client.getSession(agentId, sessionId)
|
|
||||||
mutate(result)
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
mutate(origin)
|
||||||
window.toast.error(t('common.errors.create_message'))
|
window.toast.error(t('common.errors.create_message'))
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
[agentId, sessionId, client, mutate, t]
|
[agentId, sessionId, data, mutate, client, t]
|
||||||
)
|
)
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user