feat(hooks): add useSession hook for managing agent sessions

This commit is contained in:
icarus 2025-09-19 15:27:10 +08:00
parent 7ce4fc50ea
commit fc0ba5d0d5

View 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
}
}