refactor(agents): migrate agent hooks to react-query and reorganize structure

- Replace redux-based agent management with react-query hooks
- Move agent-related hooks to dedicated agents directory
- Add useAddAgent, useRemoveAgent, and useUpdateAgent hooks
- Update imports to reflect new hook locations
- Keep redux store temporarily for backward compatibility
This commit is contained in:
icarus 2025-09-14 09:51:00 +08:00
parent e5b43c8176
commit 532bad8eb7
7 changed files with 61 additions and 3 deletions

View File

@ -18,7 +18,7 @@ import {
} from '@heroui/react'
import { loggerService } from '@logger'
import ClaudeIcon from '@renderer/assets/images/models/claude.png'
import { useAgents } from '@renderer/hooks/useAgents'
import { useAgents } from '@renderer/hooks/agents/useAgents'
import { useTimer } from '@renderer/hooks/useTimer'
import { EVENT_NAMES, EventEmitter } from '@renderer/services/EventService'
import { AgentEntity, AgentType, isAgentType } from '@renderer/types'

View File

@ -0,0 +1,16 @@
import { AgentEntity } from '@renderer/types'
import { useMutation, useQueryClient } from '@tanstack/react-query'
export const useAddAgent = () => {
const qc = useQueryClient()
// TODO: use api
return useMutation({
mutationFn: async (agent: AgentEntity) => {
return agent
},
onSuccess: (added: AgentEntity) => {
qc.setQueryData<AgentEntity[]>(['agents'], (old) => (old ? [...old, added] : [added]))
}
})
}

View File

@ -1,9 +1,15 @@
import { useAppDispatch, useAppSelector } from '@renderer/store'
import { addAgent, removeAgent, setAgents, updateAgent } from '@renderer/store/agents'
import { AgentEntity } from '@renderer/types'
// import { useQuery, useQueryClient } from '@tanstack/react-query'
import { useCallback } from 'react'
export const useAgents = () => {
// const qc = useQueryClient()
// const { data, isLoading, error } = useQuery({
// queryKey: ['agents'],
// queryFn: async () => {}
// })
const agents = useAppSelector((state) => state.agents.agentsNew)
const dispatch = useAppDispatch()
/**

View File

@ -0,0 +1,16 @@
import { AgentEntity } from '@renderer/types'
import { useMutation, useQueryClient } from '@tanstack/react-query'
export const useRemoveAgent = () => {
const qc = useQueryClient()
// TODO: use api
return useMutation({
mutationFn: async (id: string) => {
return id
},
onSuccess: (deletedId: string) => {
qc.setQueryData<AgentEntity[]>(['agents'], (old) => old?.filter((t) => t.id !== deletedId))
}
})
}

View File

@ -0,0 +1,18 @@
import { AgentEntity } from '@renderer/types'
import { useMutation, useQueryClient } from '@tanstack/react-query'
export const useUpdateAgent = () => {
const qc = useQueryClient()
// TODO: use api
return useMutation({
// @ts-expect-error not-implemented
// eslint-disable-next-line @typescript-eslint/no-unused-vars
mutationFn: async ({ id, ...payload }: Partial<AgentEntity> & { id: string }) => {},
onSuccess: (updated: AgentEntity) => {
qc.setQueryData<AgentEntity[]>(['todos'], (old) =>
old ? old.map((t) => (t.id === updated.id ? updated : t)) : []
)
}
})
}

View File

@ -3,7 +3,7 @@ import { Button, Divider } from '@heroui/react'
import { DraggableList } from '@renderer/components/DraggableList'
import { AgentModal } from '@renderer/components/Popups/AgentModal'
import Scrollbar from '@renderer/components/Scrollbar'
import { useAgents } from '@renderer/hooks/useAgents'
import { useAgents } from '@renderer/hooks/agents/useAgents'
import { useAssistants } from '@renderer/hooks/useAssistant'
import { useAssistantPresets } from '@renderer/hooks/useAssistantPresets'
import { useAssistantsTabSortType } from '@renderer/hooks/useStore'

View File

@ -10,7 +10,9 @@ export interface AgentsState {
* They should not be in this slice. However, since redux will be removed
* in the future, I just don't care where should they are. */
agents: AssistantPreset[]
/** For new autonomous agent feature. They are actual agent entities. */
/** For new autonomous agent feature. They are actual agent entities.
* They won't be used anymore when sqlite api is ready.
*/
agentsNew: AgentEntity[]
}