feat(agents): implement add agent functionality in useAgents hook

Add createAgent method to useAgents hook and remove unused useAddAgent hook
Use formatErrorMessageWithPrefix for better error handling
This commit is contained in:
icarus 2025-09-18 20:25:28 +08:00
parent 664304241a
commit 09f5e7af8c
3 changed files with 25 additions and 14 deletions

View File

@ -1,12 +0,0 @@
import { AddAgentForm } from '@renderer/types'
export const useAddAgent = () => {
// const { t } = useTranslation()
return {
// oxlint-disable-next-line no-unused-vars
addAgent: (payload: AddAgentForm) => {
window.toast.info('Not implemented')
// window.toast.success(t('common.add_success'))
}
}
}

View File

@ -1,15 +1,33 @@
import { AddAgentForm } from '@renderer/types'
import { formatErrorMessageWithPrefix } from '@renderer/utils/error'
import { useCallback } from 'react'
import { useTranslation } from 'react-i18next'
import useSWR from 'swr'
import { useAgentClient } from './useAgentClient'
export const useAgents = () => {
const { t } = useTranslation()
const client = useAgentClient()
const key = client.agentPaths.base
const { data, error, isLoading } = useSWR(key, () => client.listAgents())
const { data, error, isLoading, mutate } = useSWR(key, () => client.listAgents())
const addAgent = useCallback(
async (agent: AddAgentForm) => {
try {
const result = await client.createAgent(agent)
mutate((prev) => ({ agents: [...(prev?.agents ?? []), result], total: 0 }))
} catch (error) {
window.toast.error(formatErrorMessageWithPrefix(error, t('agent.add.error.failed')))
}
},
[client, mutate, t]
)
return {
agents: data?.agents ?? [],
error,
isLoading
isLoading,
addAgent
}
}

View File

@ -56,6 +56,11 @@ export function formatErrorMessage(error: unknown): string {
return `Error Details:\n${formattedJson}`
}
export function formatErrorMessageWithPrefix(error: unknown, prefix: string): string {
const msg = formatErrorMessage(error)
return `${prefix}: ${msg}`
}
export const isAbortError = (error: any): boolean => {
// Convert message to string for consistent checking
const errorMessage = String(error?.message || '')