Spaces:
Runtime error
Runtime error
File size: 3,693 Bytes
cdb8847 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 | import { Workflow, RunResult, ApiResponse, HealthStatus } from '../types/types';
// 默认超时时间(毫秒)
const DEFAULT_TIMEOUT = 30000;
// 后端API基础URL
const API_BASE_URL = '/api'; // 如果是代理模式,前缀为/api
/**
* 带超时的fetch请求
*/
async function fetchWithTimeout(
url: string,
options: RequestInit = {},
timeout = DEFAULT_TIMEOUT
): Promise<Response> {
const controller = new AbortController();
const id = setTimeout(() => controller.abort(), timeout);
const response = await fetch(url, {
...options,
signal: controller.signal
});
clearTimeout(id);
return response;
}
/**
* 统一处理API响应
*/
async function handleResponse<T>(response: Response): Promise<ApiResponse<T>> {
try {
const data = await response.json();
if (!response.ok) {
return {
success: false,
error: data.error || `请求失败: ${response.status}`,
status_code: response.status
};
}
return {
success: true,
data,
status_code: response.status
};
} catch (error) {
return {
success: false,
error: error instanceof Error ? error.message : '未知错误',
status_code: response.status
};
}
}
/**
* API客户端
*/
const apiClient = {
/**
* 运行实验工作流
*/
runExperiment: async (workflow: Workflow): Promise<ApiResponse<RunResult>> => {
try {
const response = await fetchWithTimeout(
`${API_BASE_URL}/run_experiment`,
{
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(workflow),
}
);
return handleResponse<RunResult>(response);
} catch (error) {
return {
success: false,
error: error instanceof Error ? error.message : '未知错误',
};
}
},
/**
* 检查后端健康状态
*/
checkHealth: async (): Promise<ApiResponse<HealthStatus>> => {
try {
const startTime = Date.now();
const response = await fetchWithTimeout(
`${API_BASE_URL}/health`,
{
method: 'GET',
},
5000 // 健康检查使用较短的超时时间
);
const latency = Date.now() - startTime;
const result = await handleResponse<any>(response);
if (result.success) {
return {
success: true,
data: {
status: 'online',
message: 'Lab服务器在线',
latency
},
status_code: result.status_code
};
} else {
return {
success: true,
data: {
status: 'offline',
message: result.error || 'Lab服务器异常',
latency
},
status_code: result.status_code
};
}
} catch (error) {
return {
success: false,
data: {
status: 'offline',
message: error instanceof Error ? error.message : '连接失败'
},
error: error instanceof Error ? error.message : '未知错误',
};
}
},
/**
* 获取工作流执行状态
*/
getRunStatus: async (runId: string): Promise<ApiResponse<RunResult>> => {
try {
const response = await fetchWithTimeout(
`${API_BASE_URL}/run_status/${runId}`,
{
method: 'GET',
}
);
return handleResponse<RunResult>(response);
} catch (error) {
return {
success: false,
error: error instanceof Error ? error.message : '未知错误',
};
}
},
};
export default apiClient;
|