From 5e3da508cf2a946af485c19c3266232dadca87b3 Mon Sep 17 00:00:00 2001 From: hhs <386998068@qq.com> Date: Wed, 10 Jun 2026 15:24:16 +0800 Subject: [PATCH] =?UTF-8?q?feat(frontend):=20=E6=AD=A5=E9=AA=A4=206.4=20?= =?UTF-8?q?=E2=80=94=20API=20=E5=AE=A2=E6=88=B7=E7=AB=AF=E5=B0=81=E8=A3=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- frontend/src/api/agent.ts | 62 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 frontend/src/api/agent.ts diff --git a/frontend/src/api/agent.ts b/frontend/src/api/agent.ts new file mode 100644 index 0000000..5916f59 --- /dev/null +++ b/frontend/src/api/agent.ts @@ -0,0 +1,62 @@ +import { API_CONFIG } from "@/config/api-config"; +import { + Response, + AiAgentConfigResponseDTO, + CreateSessionResponseDTO, + ChatRequestDTO, + ChatResponseDTO, +} from "@/types/api"; + +const handleResponse = async ( + response: globalThis.Response +): Promise> => { + if (!response.ok) { + const errorText = await response.text(); + throw new Error(`HTTP ${response.status}: ${errorText}`); + } + const data = await response.json(); + if (data.code !== "0000") { + throw new Error(data.info || "Unknown API error"); + } + return data; +}; + +export const agentApi = { + /** + * 查询已注册的 AI Agent 列表 + * GET /api/v1/query_ai_agent_config_list + */ + queryAgentList: async (): Promise> => { + const resp = await fetch(`${API_CONFIG.BASE_URL}/query_ai_agent_config_list`); + return handleResponse(resp); + }, + + /** + * 创建聊天会话 + * POST /api/v1/create_session + */ + createSession: async ( + agentId: string, + userId: string + ): Promise> => { + const resp = await fetch(`${API_CONFIG.BASE_URL}/create_session`, { + method: "POST", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify({ agentId, userId }), + }); + return handleResponse(resp); + }, + + /** + * 发送聊天消息 + * POST /api/v1/chat + */ + chat: async (data: ChatRequestDTO): Promise> => { + const resp = await fetch(`${API_CONFIG.BASE_URL}/chat`, { + method: "POST", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify(data), + }); + return handleResponse(resp); + }, +};