mirror of
https://github.com/CherryHQ/cherry-studio.git
synced 2026-01-02 02:09:03 +08:00
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:
parent
664304241a
commit
09f5e7af8c
@ -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'))
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -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
|
||||
}
|
||||
}
|
||||
|
||||
@ -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 || '')
|
||||
|
||||
Loading…
Reference in New Issue
Block a user