mirror of
https://github.com/CherryHQ/cherry-studio.git
synced 2025-12-29 14:31:35 +08:00
feat(agent): add get and delete agent methods with error handling
Implement getAgent and deleteAgent methods in AgentClient with proper error handling Add formatAgentServerError utility for consistent error messages
This commit is contained in:
parent
825c376c5c
commit
f1991b356b
@ -1,12 +1,16 @@
|
||||
import { formatAgentServerError } from '@renderer/utils'
|
||||
import {
|
||||
AgentForm,
|
||||
AgentServerErrorSchema,
|
||||
CreateAgentRequest,
|
||||
CreateAgentResponse,
|
||||
CreateAgentResponseSchema,
|
||||
GetAgentResponse,
|
||||
GetAgentResponseSchema,
|
||||
type ListAgentsResponse,
|
||||
ListAgentsResponseSchema
|
||||
} from '@types'
|
||||
import { Axios, AxiosRequestConfig } from 'axios'
|
||||
import { Axios, AxiosRequestConfig, isAxiosError } from 'axios'
|
||||
|
||||
type ApiVersion = 'v1'
|
||||
|
||||
@ -52,4 +56,30 @@ export class AgentClient {
|
||||
throw new Error('Failed to create agent.', { cause: error })
|
||||
}
|
||||
}
|
||||
|
||||
public async getAgent(id: string): Promise<GetAgentResponse> {
|
||||
const url = `/${this.apiVersion}/agents/${id}`
|
||||
try {
|
||||
const response = await this.axios.get(url)
|
||||
const data = GetAgentResponseSchema.parse(response.data)
|
||||
return data
|
||||
} catch (error) {
|
||||
throw new Error('Failed to get agent.', { cause: error })
|
||||
}
|
||||
}
|
||||
|
||||
public async deleteAgent(id: string): Promise<void> {
|
||||
const url = `/${this.apiVersion}/agents/${id}`
|
||||
try {
|
||||
await this.axios.delete(url)
|
||||
} catch (error) {
|
||||
if (isAxiosError(error)) {
|
||||
const result = AgentServerErrorSchema.safeParse(error.response)
|
||||
if (result.success) {
|
||||
throw new Error(formatAgentServerError(result.data), { cause: error })
|
||||
}
|
||||
}
|
||||
throw new Error('Failed to delete agent.', { cause: error })
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,3 +1,6 @@
|
||||
import { AgentServerError } from '@renderer/types'
|
||||
import { t } from 'i18next'
|
||||
|
||||
/**
|
||||
* 格式化 API key 字符串。
|
||||
*
|
||||
@ -69,3 +72,6 @@ export function splitApiKeyString(keyStr: string): string[] {
|
||||
.map((k) => k.replace(/\\,/g, ','))
|
||||
.filter((k) => k)
|
||||
}
|
||||
|
||||
export const formatAgentServerError = (error: AgentServerError) =>
|
||||
`${t('common.error')}: ${error.code} ${error.message}`
|
||||
|
||||
Loading…
Reference in New Issue
Block a user