fix(agent): serialize retrieval trace responses

This commit is contained in:
2026-07-13 19:37:12 +08:00
parent 128d444a76
commit e4a391aa08
4 changed files with 51 additions and 4 deletions

View File

@@ -46,13 +46,25 @@ async function request<T>(path: string, init: RequestInit = {}): Promise<T> {
const token = getToken();
if (token) headers.set("Authorization", `Bearer ${token}`);
const response = await fetch(`${API_BASE}${path}`, { ...init, headers });
const body = (await response.json()) as ApiResponse<T> & { detail?: string };
const body = await readApiResponse<T>(response);
if (!response.ok || body.code !== 0) {
throw new Error(body.detail || body.message || `${response.status} ${response.statusText}`);
}
return body.data;
}
async function readApiResponse<T>(response: Response): Promise<ApiResponse<T> & { detail?: string }> {
const text = await response.text();
if (!text.trim()) {
throw new Error(response.ok ? "接口返回为空" : `服务暂时不可用(${response.status}`);
}
try {
return JSON.parse(text) as ApiResponse<T> & { detail?: string };
} catch {
throw new Error(response.ok ? "接口返回格式不正确" : `服务暂时不可用(${response.status}`);
}
}
async function download(path: string, filename: string) {
const headers = new Headers();
const token = getToken();
@@ -73,7 +85,7 @@ async function upload<T>(path: string, formData: FormData): Promise<T> {
const token = getToken();
if (token) headers.set("Authorization", `Bearer ${token}`);
const response = await fetch(`${API_BASE}${path}`, { method: "POST", body: formData, headers });
const body = (await response.json()) as ApiResponse<T> & { detail?: string };
const body = await readApiResponse<T>(response);
if (!response.ok || body.code !== 0) {
throw new Error(body.message || body.detail || `${response.status} ${response.statusText}`);
}