From d56c526709ce9b0ff0676c70fe193e2c7ceafd14 Mon Sep 17 00:00:00 2001 From: icarus Date: Thu, 18 Sep 2025 13:48:57 +0800 Subject: [PATCH] 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 --- src/renderer/src/api/agent.ts | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 src/renderer/src/api/agent.ts diff --git a/src/renderer/src/api/agent.ts b/src/renderer/src/api/agent.ts new file mode 100644 index 0000000000..f04af6e1e6 --- /dev/null +++ b/src/renderer/src/api/agent.ts @@ -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 { + 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 }) + } + } +}