mirror of
https://github.com/CherryHQ/cherry-studio.git
synced 2025-12-27 04:31:27 +08:00
feat(api): add AgentClient class for listing agents
Implement a new API client class to handle agent listing operations with proper error handling and validation
This commit is contained in:
parent
70a68bef27
commit
d56c526709
34
src/renderer/src/api/agent.ts
Normal file
34
src/renderer/src/api/agent.ts
Normal file
@ -0,0 +1,34 @@
|
||||
import { ListAgentsResponseSchema, type ListAgentsResponse } from '@types'
|
||||
import { Axios, AxiosRequestConfig } from 'axios'
|
||||
|
||||
type ApiVersion = 'v1'
|
||||
|
||||
// const logger = loggerService.withContext('AgentClient')
|
||||
|
||||
export class AgentClient {
|
||||
private axios: Axios
|
||||
private apiVersion: ApiVersion = 'v1'
|
||||
constructor(config: AxiosRequestConfig, apiVersion?: ApiVersion) {
|
||||
if (!config.baseURL || !config.headers?.Authorization) {
|
||||
throw new Error('Please pass in baseUrl and Authroization header.')
|
||||
}
|
||||
this.axios = new Axios(config)
|
||||
if (apiVersion) {
|
||||
this.apiVersion = apiVersion
|
||||
}
|
||||
}
|
||||
|
||||
public async listAgents(): Promise<ListAgentsResponse> {
|
||||
const url = `/${this.apiVersion}/agents`
|
||||
try {
|
||||
const response = await this.axios.get(url)
|
||||
const result = ListAgentsResponseSchema.safeParse(response.data)
|
||||
if (!result.success) {
|
||||
throw new Error('Not a valid Agents array.')
|
||||
}
|
||||
return result.data
|
||||
} catch (error) {
|
||||
throw new Error('Failed to list agents.', { cause: error })
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user