/** * 智能算法展示平台JavaScript SDK */ const axios = require('axios'); /** * 智能算法展示平台客户端类 */ class AlgorithmShowcaseClient { /** * 初始化客户端 * @param {string} baseUrl - API基础URL * @param {string} apiKey - API密钥 */ constructor(baseUrl = 'http://localhost:8001/api/v1', apiKey = null) { this.baseUrl = baseUrl.replace(/\/$/, ''); this.apiKey = apiKey; // 创建axios实例 this.client = axios.create({ baseURL: this.baseUrl, headers: { 'Content-Type': 'application/json' } }); // 如果提供了API密钥,设置认证头 if (this.apiKey) { this.client.defaults.headers.common['Authorization'] = `Bearer ${this.apiKey}`; } } /** * 获取算法列表 * @param {string} type - 算法类型 * @returns {Promise} 算法列表 */ async getAlgorithms(type = null) { try { const params = {}; if (type) { params.type = type; } const response = await this.client.get('/algorithms', { params }); return response.data.algorithms; } catch (error) { throw new Error(`获取算法列表失败: ${error.message}`); } } /** * 获取算法详情 * @param {string} algorithmId - 算法ID * @returns {Promise} 算法详情 */ async getAlgorithm(algorithmId) { try { const response = await this.client.get(`/algorithms/${algorithmId}`); return response.data; } catch (error) { throw new Error(`获取算法详情失败: ${error.message}`); } } /** * 获取算法版本列表 * @param {string} algorithmId - 算法ID * @returns {Promise} 版本列表 */ async getAlgorithmVersions(algorithmId) { try { const response = await this.client.get(`/algorithms/${algorithmId}/versions`); return response.data; } catch (error) { throw new Error(`获取算法版本列表失败: ${error.message}`); } } /** * 调用算法 * @param {string} algorithmId - 算法ID * @param {string} versionId - 版本ID * @param {Object} inputData - 输入数据 * @param {Object} params - 算法参数 * @returns {Promise} 调用结果 */ async callAlgorithm(algorithmId, versionId, inputData, params = {}) { try { const response = await this.client.post('/algorithms/call', { algorithm_id: algorithmId, version_id: versionId, input_data: inputData, params: params }); return response.data; } catch (error) { throw new Error(`调用算法失败: ${error.message}`); } } /** * 获取调用结果 * @param {string} callId - 调用ID * @returns {Promise} 调用结果 */ async getCallResult(callId) { try { const response = await this.client.get(`/algorithms/calls/${callId}`); return response.data; } catch (error) { throw new Error(`获取调用结果失败: ${error.message}`); } } /** * 生成仿真输入数据 * @param {string} prompt - 描述 * @param {string} dataType - 数据类型 * @returns {Promise} 生成的数据 */ async generateSimulationData(prompt, dataType = 'text') { try { const response = await this.client.post('/openai/generate-data', { prompt: prompt, data_type: dataType }); return response.data; } catch (error) { throw new Error(`生成仿真数据失败: ${error.message}`); } } /** * 获取调用历史 * @param {string} algorithmId - 算法ID * @param {string} status - 状态 * @param {string} startDate - 开始日期 * @param {string} endDate - 结束日期 * @returns {Promise} 调用历史 */ async getCallHistory(algorithmId = null, status = null, startDate = null, endDate = null) { try { const params = {}; if (algorithmId) params.algorithm_id = algorithmId; if (status) params.status = status; if (startDate) params.start_date = startDate; if (endDate) params.end_date = endDate; const response = await this.client.get('/history/user-calls', { params }); return response.data.history; } catch (error) { throw new Error(`获取调用历史失败: ${error.message}`); } } /** * 获取调用统计 * @param {string} algorithmId - 算法ID * @returns {Promise} 统计信息 */ async getCallStatistics(algorithmId = null) { try { const params = {}; if (algorithmId) params.algorithm_id = algorithmId; const response = await this.client.get('/history/statistics', { params }); return response.data; } catch (error) { throw new Error(`获取调用统计失败: ${error.message}`); } } /** * 导出历史记录 * @param {string} algorithmId - 算法ID * @param {string} startDate - 开始日期 * @param {string} endDate - 结束日期 * @param {string} formatType - 导出格式 * @returns {Promise} 导出结果 */ async exportHistory(algorithmId = null, startDate = null, endDate = null, formatType = 'json') { try { const params = { format_type: formatType }; if (algorithmId) params.algorithm_id = algorithmId; if (startDate) params.start_date = startDate; if (endDate) params.end_date = endDate; const response = await this.client.get('/history/export', { params }); return response.data; } catch (error) { throw new Error(`导出历史记录失败: ${error.message}`); } } /** * 获取算法性能指标 * @param {string} algorithmId - 算法ID * @param {number} days - 统计天数 * @returns {Promise} 性能指标 */ async getAlgorithmPerformance(algorithmId, days = 7) { try { const params = { days }; const response = await this.client.get(`/monitoring/performance/algorithm/${algorithmId}`, { params }); return response.data; } catch (error) { throw new Error(`获取算法性能指标失败: ${error.message}`); } } /** * 获取系统健康状况 * @returns {Promise} 系统健康状况 */ async getSystemHealth() { try { const response = await this.client.get('/monitoring/health'); return response.data; } catch (error) { throw new Error(`获取系统健康状况失败: ${error.message}`); } } /** * 检查权限 * @param {string} algorithmId - 算法ID * @param {string} permissionType - 权限类型 * @returns {Promise} 是否有权限 */ async checkPermission(algorithmId, permissionType = 'execute') { try { const response = await this.client.post('/permissions/check', { algorithm_id: algorithmId, permission_type: permissionType }); return response.data.has_permission; } catch (error) { throw new Error(`检查权限失败: ${error.message}`); } } } module.exports = AlgorithmShowcaseClient;