/** * 数字人直播系统 - API 客户端(生产环境) * * 调用模式:所有请求通过 API 网关的 smart-processor 统一入口 * * POST https://api-gateway.duoweiying.cn/smart-processor/command * { scene: "digital_human_live", action: "process_comment", params: {...} } * * 响应格式:{ success: true, data: {...}, error: null, execution_time: 0.01 } */ class DigitalHumanAPIClient { constructor(config) { this.config = config || window.DIGITAL_HUMAN_API_CONFIG; this.gateway = this.config.smartProcessorURL; this.scene = this.config.scene; this.actions = this.config.actions; this.timeouts = this.config.timeout; } // ==================== 核心调用方法 ==================== /** * 通过 smart-processor 网关调用后端API */ async call(action, params = {}, options = {}) { const timeoutMs = options.timeout || this.timeouts.default; const controller = new AbortController(); const timeoutId = setTimeout(() => controller.abort(), timeoutMs); try { const response = await fetch(this.gateway, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ scene: this.scene, action: action, params: params }), signal: controller.signal }); clearTimeout(timeoutId); // 网关返回错误状态 if (response.status === 502 || response.status === 504) { throw new Error('API网关超时,服务处理中,请稍后重试'); } if (!response.ok) { const text = await response.text().catch(() => ''); throw new Error(`HTTP ${response.status}: ${text.substring(0, 200)}`); } const result = await response.json(); // smart-processor 返回格式: {success, data, error} if (result.success === false || result.error) { throw new Error(result.error || '后端处理失败'); } // 提取实际数据:优先 data,然后整个 result return result.data || result; } catch (error) { if (error.name === 'AbortError') { throw new Error(`请求超时 (${timeoutMs/1000}s): ${action}`); } if (error.name === 'TypeError' && error.message.includes('fetch')) { throw new Error('网络连接失败,请检查网关: ' + this.gateway); } throw error; } } /** * 带HTTP请求的call(用于文件下载等特殊场景) */ async fetchDirect(url, options = {}) { const fullUrl = this.config.gatewayURL + url; const response = await fetch(fullUrl, options); if (!response.ok) { throw new Error(`HTTP ${response.status}`); } return response.json(); } // ==================== 业务 API 方法 ==================== /** * 健康检查 */ async healthCheck() { return this.call(this.actions.healthCheck, {}); } /** * 获取场景模板列表 */ async getSceneTemplates() { return this.call(this.actions.getScenes, {}); } /** * 获取话术模板 */ async getTalkTemplates(platform) { return this.call(this.actions.getTalkTemplates, { platform }); } /** * 开始直播 */ async startLiveStream(params) { return this.call(this.actions.startLive, params, { timeout: this.timeouts.startLive }); } /** * 停止直播 */ async stopLiveStream(params) { return this.call(this.actions.stopLive, params); } /** * 切换真人/数字人直播 */ async switchStream(params) { return this.call(this.actions.switchStream, params); } /** * 合规检查 */ async complianceCheck(content, platform) { return this.call(this.actions.complianceCheck, { content, platform }); } /** * 敏感词检测 */ async detectSensitiveWords(text, platform) { return this.call(this.actions.detectSensitive, { text, platform }); } /** * 同步数据 */ async syncData() { return this.call(this.actions.syncData, {}); } /** * 核心:智能评论处理(9智能体协同) * 注意:此操作耗时约40秒,前端需显示加载状态 */ async processComment(content, platform, context = {}) { return this.call(this.actions.processComment, { content, platform, user_id: context.user_id || `user_${Date.now()}` }, { timeout: this.timeouts.processComment }); } /** * 初始化数字人形象 */ async avatarInitialize(avatarId, config = {}) { return this.call(this.actions.avatarInitialize, { avatar_id: avatarId, config }); } /** * 设置数字人表情 */ async avatarSetEmotion(emotion, intensity = 1.0) { return this.call(this.actions.avatarSetEmotion, { emotion, intensity }); } /** * 执行数字人动作 */ async avatarExecuteAction(action, params = {}) { return this.call(this.actions.avatarExecuteAction, { action, params }); } /** * 获取数字人状态 */ async avatarStatus() { return this.call(this.actions.avatarStatus, {}); } /** * 生成短视频内容 */ async generateVideoContent(script, avatarId, config = {}) { return this.call(this.actions.generateVideo, { script, avatar_id: avatarId, config }, { timeout: this.timeouts.generateVideo }); } /** * 获取沙盒配置列表 */ async getSandboxConfigs() { return this.call(this.actions.getSandboxConfigs, {}); } /** * 获取指定平台沙盒配置 */ async getSandboxConfig(platform) { return this.call(this.actions.getSandboxConfig, { platform }); } /** * 获取平台数据洞察 */ async getPlatformInsights(platform) { return this.call(this.actions.getPlatformInsights, { platform }); } /** * 创建直播流 */ async createLiveStream(params) { return this.call(this.actions.createLiveStream, params); } /** * 智能选品 */ async selectMaterials(params) { return this.call(this.actions.selectMaterials, params); } /** * 智能问答处理 */ async processQuery(query, userId, context = {}) { return this.call(this.actions.processQuery, { query, user_id: userId, context }); } } // 创建全局实例 const digitalHumanAPI = new DigitalHumanAPIClient(); if (typeof window !== 'undefined') { window.DigitalHumanAPIClient = DigitalHumanAPIClient; window.digitalHumanAPI = digitalHumanAPI; } if (typeof module !== 'undefined' && module.exports) { module.exports = { DigitalHumanAPIClient, digitalHumanAPI }; } console.log('[数字人直播] API客户端已就绪,网关模式:', DIGITAL_HUMAN_API_CONFIG.smartProcessorURL);