mirror of
https://github.com/CherryHQ/cherry-studio.git
synced 2026-02-16 15:44:45 +08:00
feat(hooks): add useSession hook for managing agent sessions
This commit is contained in:
parent
7ce4fc50ea
commit
fc0ba5d0d5
39
src/renderer/src/hooks/agents/useSession.ts
Normal file
39
src/renderer/src/hooks/agents/useSession.ts
Normal file
@ -0,0 +1,39 @@
|
||||
import { UpdateSessionForm } from '@renderer/types'
|
||||
import { useCallback } from 'react'
|
||||
import { useTranslation } from 'react-i18next'
|
||||
import useSWR from 'swr'
|
||||
|
||||
import { useAgentClient } from './useAgentClient'
|
||||
|
||||
export const useSession = (agentId: string, sessionId: string) => {
|
||||
const { t } = useTranslation()
|
||||
const client = useAgentClient()
|
||||
const key = client.getSessionPaths(agentId).withId(sessionId)
|
||||
|
||||
const fetcher = async () => {
|
||||
const data = await client.getSession(agentId, sessionId)
|
||||
return data
|
||||
}
|
||||
const { data, error, isLoading, mutate } = useSWR(key, fetcher)
|
||||
|
||||
const updateSession = useCallback(
|
||||
async (form: UpdateSessionForm) => {
|
||||
if (!agentId) return
|
||||
try {
|
||||
const result = await client.updateSession(agentId, form)
|
||||
mutate(result)
|
||||
} catch (error) {
|
||||
window.toast.error(t('agent.session.update.error.failed'))
|
||||
}
|
||||
},
|
||||
[agentId, client, mutate, t]
|
||||
)
|
||||
|
||||
return {
|
||||
session: data,
|
||||
messages: data?.messages,
|
||||
error,
|
||||
isLoading,
|
||||
updateSession
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user