@zeewain/3d-avatar-sdk 1.2.3 → 1.2.4-1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.es5.js CHANGED
@@ -13766,6 +13766,20 @@ var ZEEAvatarSDKLib = (function (exports) {
13766
13766
  * @fileoverview 流式播报服务重构实现模块
13767
13767
  * @description 提供流式播报服务的重构实现,继承Unity基础服务,支持文本转语音和自定义音频播报
13768
13768
  */
13769
+ /**
13770
+ * 播报任务状态枚举
13771
+ */
13772
+ var BroadcastTaskStatus;
13773
+ (function (BroadcastTaskStatus) {
13774
+ /** 请求中 */
13775
+ BroadcastTaskStatus["REQUESTING"] = "requesting";
13776
+ /** 已完成 */
13777
+ BroadcastTaskStatus["COMPLETED"] = "completed";
13778
+ /** 失败 */
13779
+ BroadcastTaskStatus["FAILED"] = "failed";
13780
+ /** 已取消 */
13781
+ BroadcastTaskStatus["CANCELLED"] = "cancelled";
13782
+ })(BroadcastTaskStatus || (BroadcastTaskStatus = {}));
13769
13783
  /**
13770
13784
  * 流式播报服务重构类
13771
13785
  * @class BroadcastServiceRefactored
@@ -13796,12 +13810,20 @@ var ZEEAvatarSDKLib = (function (exports) {
13796
13810
  var _this = _super.call(this, config) || this;
13797
13811
  /** 事件回调函数集合 */
13798
13812
  _this.callbacks = {};
13799
- /** 请求控制器,用于取消请求 */
13800
- _this.activeController = null;
13813
+ /** 播报任务队列 */
13814
+ _this.taskQueue = [];
13815
+ /** 任务序号计数器 */
13816
+ _this.taskSequence = 0;
13817
+ /** 当前发送任务的序号 */
13818
+ _this.currentSendingSequence = 0;
13801
13819
  /** 是否正在生成音频 */
13802
13820
  _this.isGeneratingAudio = false;
13803
13821
  /** 是否已经收到音频 */
13804
13822
  _this.hasReceivedAudio = false;
13823
+ /** 队列处理定时器 */
13824
+ _this.queueProcessTimer = null;
13825
+ /** 主请求控制器(兼容性保留) */
13826
+ _this.activeController = null;
13805
13827
  _this.callbacks = config.callbacks || {};
13806
13828
  _this.logger.info('Broadcast service initialized', {
13807
13829
  config: config
@@ -13852,8 +13874,9 @@ var ZEEAvatarSDKLib = (function (exports) {
13852
13874
  /**
13853
13875
  * 开始播报
13854
13876
  * @param params - 播报参数
13877
+ * @param isAppend - 是否追加播报
13855
13878
  * @returns Promise<void> 播报操作的Promise
13856
- * @description 开始流式播报,支持文本转语音和自定义音频播报
13879
+ * @description 开始流式播报,支持文本转语音和自定义音频播报。使用队列机制确保音频按序播报
13857
13880
  * @throws {SDKError} 当参数验证失败或播报失败时抛出错误
13858
13881
  * @example
13859
13882
  * ```typescript
@@ -13867,49 +13890,40 @@ var ZEEAvatarSDKLib = (function (exports) {
13867
13890
  * isSubtitle: true
13868
13891
  * });
13869
13892
  *
13870
- * // 自定义音频播报
13893
+ * // 追加播报(会进入队列按序播报)
13871
13894
  * await broadcastService.startBroadcast({
13872
- * type: BroadcastType.AUDIO,
13895
+ * type: BroadcastType.TEXT,
13873
13896
  * humanCode: 'human001',
13874
- * audioUrl: 'https://example.com/audio.mp3',
13897
+ * text: '这是第二段内容',
13898
+ * voiceCode: 'voice001',
13875
13899
  * volume: 0.8,
13876
- * isSubtitle: false
13877
- * });
13900
+ * isSubtitle: true
13901
+ * }, true);
13878
13902
  * ```
13879
13903
  */
13880
- BroadcastService.prototype.startBroadcast = function (params) {
13904
+ BroadcastService.prototype.startBroadcast = function (params, isAppend) {
13881
13905
  return __awaiter(this, void 0, void 0, function () {
13882
- var apiUrl, requestBody, error_1;
13883
- var _this = this;
13906
+ var task;
13884
13907
  var _a, _b;
13885
13908
  return __generator(this, function (_c) {
13886
13909
  switch (_c.label) {
13887
13910
  case 0:
13888
13911
  this.logger.info("Starting broadcast: ".concat(params.type), {
13889
- humanCode: params.humanCode
13912
+ humanCode: params.humanCode,
13913
+ isAppend: isAppend,
13914
+ queueLength: this.taskQueue.length
13890
13915
  });
13891
13916
  // 验证参数
13892
13917
  this.validateBroadcastParams(params);
13893
- // 先停止当前播报
13918
+ if (!!isAppend) return [3 /*break*/, 2];
13919
+ // 先停止当前播报并清空队列
13894
13920
  return [4 /*yield*/, this.stopBroadcast()];
13895
13921
  case 1:
13896
- // 先停止当前播报
13922
+ // 先停止当前播报并清空队列
13897
13923
  _c.sent();
13898
- // 创建新的请求控制器
13899
- this.activeController = new AbortController();
13900
- // 监听中断信号
13901
- this.activeController.signal.addEventListener('abort', function () {
13902
- var _a, _b;
13903
- console.warn('abort');
13904
- // 如果正在生成音频,触发中断回调
13905
- if (_this.isGeneratingAudio) {
13906
- (_b = (_a = _this.callbacks).onAbort) === null || _b === void 0 ? void 0 : _b.call(_a);
13907
- }
13908
- // 重置状态
13909
- _this.isGeneratingAudio = false;
13910
- _this.hasReceivedAudio = false;
13911
- _this.activeController = null;
13912
- });
13924
+ // 重置序号计数器
13925
+ this.taskSequence = 0;
13926
+ this.currentSendingSequence = 0;
13913
13927
  // 通知Unity开始新任务(清空队列)
13914
13928
  this.sendMessage('StartBroadcast', {
13915
13929
  callbackFun: this.uniqueCallbackName,
@@ -13917,100 +13931,19 @@ var ZEEAvatarSDKLib = (function (exports) {
13917
13931
  motionList: params.motionList,
13918
13932
  motionPlayMode: params.motionPlayMode
13919
13933
  });
13920
- _c.label = 2;
13921
- case 2:
13922
- _c.trys.push([2, 4,, 5]);
13923
- apiUrl = "".concat(ConfigManager.getInstance().getApiBaseUrl(true)).concat(this.getBroadcastApiPath(params.type));
13924
- requestBody = {
13925
- humanCode: params.humanCode,
13926
- speed: params.speed,
13927
- volume: params.volume >= 0 ? params.volume * 100 : undefined,
13928
- // 转换为百分比
13929
- isSubtitle: params.isSubtitle
13930
- };
13931
- // 根据播报类型设置特定参数
13932
- if (params.type === exports.BroadcastType.TEXT) {
13933
- requestBody.text = params.text;
13934
- requestBody.voiceCode = params.voiceCode;
13935
- } else if (params.type === exports.BroadcastType.AUDIO) {
13936
- requestBody.text = params.text;
13937
- requestBody.audioUrl = params.audioUrl;
13938
- }
13939
- this.logger.debug('Sending broadcast request', {
13940
- apiUrl: apiUrl,
13941
- requestBody: requestBody
13942
- });
13943
13934
  // 触发开始回调
13944
13935
  (_b = (_a = this.callbacks).onStart) === null || _b === void 0 ? void 0 : _b.call(_a);
13945
13936
  this.isGeneratingAudio = true;
13946
- // 发起流式播报请求
13947
- return [4 /*yield*/, fetchEventSource(apiUrl, {
13948
- method: 'POST',
13949
- headers: {
13950
- 'Content-Type': 'application/json',
13951
- 'x_auth_token': ConfigManager.getInstance().getToken()
13952
- },
13953
- body: JSON.stringify(requestBody),
13954
- signal: this.activeController.signal,
13955
- openWhenHidden: true,
13956
- onmessage: function onmessage(event) {
13957
- var response = JSON.parse(event.data);
13958
- // 错误处理
13959
- if (response.code !== 0) {
13960
- _this.handleBroadcastError(response.code);
13961
- return;
13962
- }
13963
- // 流式播报
13964
- if (response.data) {
13965
- // 未完成播报时,更新任务ID
13966
- if (!response.data.done) {
13967
- _this.hasReceivedAudio = true;
13968
- }
13969
- // 自定义音频播报时,如果服务器未返回音频URL,使用传入的audioUrl
13970
- if (params.type === exports.BroadcastType.AUDIO && params.audioUrl && !response.data.voiceUrl) {
13971
- response.data.voiceUrl = params.audioUrl;
13972
- }
13973
- // 如果有音频URL,发送到Unity
13974
- if (response.data.voiceUrl) {
13975
- _this.sendMessage('AppendBroadcast', {
13976
- response: response,
13977
- callbackFun: _this.uniqueCallbackName,
13978
- operationType: exports.BroadcastOperationType.APPEND_BROADCAST
13979
- });
13980
- }
13981
- if (response.data.done) {
13982
- _this.isGeneratingAudio = false;
13983
- _this.hasReceivedAudio = false;
13984
- _this.activeController = null;
13985
- }
13986
- }
13987
- },
13988
- onclose: function onclose() {
13989
- var _a;
13990
- // 如果不是由abort触发的关闭,需要处理
13991
- if (_this.isGeneratingAudio) {
13992
- (_a = _this.activeController) === null || _a === void 0 ? void 0 : _a.abort();
13993
- }
13994
- // 流结束时调用完成回调
13995
- _this.logger.debug('Broadcast stream closed');
13996
- },
13997
- onerror: function onerror(error) {
13998
- _this.logger.error('Broadcast stream error', error);
13999
- var sdkError = new SDKError(exports.OperationErrorCode.OPERATION_FAILED, '服务出错了', error);
14000
- _this.handleError(sdkError);
14001
- // 必须抛出错误,否则会循环重试请求
14002
- throw new Error("\u670D\u52A1\u51FA\u9519\u4E86".concat(sdkError.message));
14003
- }
14004
- })];
14005
- case 3:
14006
- // 发起流式播报请求
14007
- _c.sent();
14008
- return [3 /*break*/, 5];
14009
- case 4:
14010
- error_1 = _c.sent();
14011
- this.handleError(error_1);
14012
- return [3 /*break*/, 5];
14013
- case 5:
13937
+ _c.label = 2;
13938
+ case 2:
13939
+ task = this.createBroadcastTask(params);
13940
+ // 添加任务到队列
13941
+ this.addTaskToQueue(task);
13942
+ this.logger.debug('Broadcast task created and queued', {
13943
+ taskId: task.id,
13944
+ sequence: task.sequence,
13945
+ isAppend: isAppend
13946
+ });
14014
13947
  return [2 /*return*/];
14015
13948
  }
14016
13949
  });
@@ -14028,7 +13961,7 @@ var ZEEAvatarSDKLib = (function (exports) {
14028
13961
  */
14029
13962
  BroadcastService.prototype.pauseBroadcast = function (resetIdle) {
14030
13963
  return __awaiter(this, void 0, void 0, function () {
14031
- var error_2;
13964
+ var error_1;
14032
13965
  return __generator(this, function (_a) {
14033
13966
  switch (_a.label) {
14034
13967
  case 0:
@@ -14044,9 +13977,9 @@ var ZEEAvatarSDKLib = (function (exports) {
14044
13977
  this.logger.info('Broadcast paused successfully');
14045
13978
  return [3 /*break*/, 4];
14046
13979
  case 3:
14047
- error_2 = _a.sent();
14048
- this.logger.error('Failed to pause broadcast', error_2);
14049
- throw error_2;
13980
+ error_1 = _a.sent();
13981
+ this.logger.error('Failed to pause broadcast', error_1);
13982
+ throw error_1;
14050
13983
  case 4:
14051
13984
  return [2 /*return*/];
14052
13985
  }
@@ -14064,7 +13997,7 @@ var ZEEAvatarSDKLib = (function (exports) {
14064
13997
  */
14065
13998
  BroadcastService.prototype.resumeBroadcast = function () {
14066
13999
  return __awaiter(this, void 0, void 0, function () {
14067
- var error_3;
14000
+ var error_2;
14068
14001
  return __generator(this, function (_a) {
14069
14002
  switch (_a.label) {
14070
14003
  case 0:
@@ -14078,9 +14011,9 @@ var ZEEAvatarSDKLib = (function (exports) {
14078
14011
  this.logger.info('Broadcast resumed successfully');
14079
14012
  return [3 /*break*/, 4];
14080
14013
  case 3:
14081
- error_3 = _a.sent();
14082
- this.logger.error('Failed to resume broadcast', error_3);
14083
- throw error_3;
14014
+ error_2 = _a.sent();
14015
+ this.logger.error('Failed to resume broadcast', error_2);
14016
+ throw error_2;
14084
14017
  case 4:
14085
14018
  return [2 /*return*/];
14086
14019
  }
@@ -14098,16 +14031,18 @@ var ZEEAvatarSDKLib = (function (exports) {
14098
14031
  */
14099
14032
  BroadcastService.prototype.stopBroadcast = function () {
14100
14033
  return __awaiter(this, void 0, void 0, function () {
14101
- var error_4;
14034
+ var error_3;
14102
14035
  return __generator(this, function (_a) {
14103
14036
  switch (_a.label) {
14104
14037
  case 0:
14105
- this.logger.info('Stopping broadcast');
14106
- // 取消流式请求
14107
- if (this.activeController) {
14108
- this.activeController.abort();
14109
- this.activeController = null;
14110
- }
14038
+ this.logger.info('Stopping broadcast and clearing queue', {
14039
+ queueLength: this.taskQueue.length
14040
+ });
14041
+ // 取消所有队列中的任务
14042
+ this.cancelAllTasks();
14043
+ // 重置状态
14044
+ this.isGeneratingAudio = false;
14045
+ this.hasReceivedAudio = false;
14111
14046
  _a.label = 1;
14112
14047
  case 1:
14113
14048
  _a.trys.push([1, 3,, 4]);
@@ -14117,9 +14052,9 @@ var ZEEAvatarSDKLib = (function (exports) {
14117
14052
  this.logger.info('Broadcast stopped successfully');
14118
14053
  return [3 /*break*/, 4];
14119
14054
  case 3:
14120
- error_4 = _a.sent();
14121
- this.logger.error('Failed to stop broadcast', error_4);
14122
- throw error_4;
14055
+ error_3 = _a.sent();
14056
+ this.logger.error('Failed to stop broadcast', error_3);
14057
+ throw error_3;
14123
14058
  case 4:
14124
14059
  return [2 /*return*/];
14125
14060
  }
@@ -14138,15 +14073,31 @@ var ZEEAvatarSDKLib = (function (exports) {
14138
14073
  /**
14139
14074
  * 获取播报状态
14140
14075
  * @returns 播报状态信息
14141
- * @description 获取当前播报服务的状态信息
14076
+ * @description 获取当前播报服务的状态信息,包括队列状态
14142
14077
  */
14143
14078
  BroadcastService.prototype.getStatus = function () {
14144
14079
  return {
14145
- isActive: this.activeController !== null,
14080
+ isActive: this.taskQueue.length > 0 || this.isGeneratingAudio,
14146
14081
  isGeneratingAudio: this.isGeneratingAudio,
14147
14082
  hasReceivedAudio: this.hasReceivedAudio,
14148
14083
  pendingCallbacks: this.getPendingCallbackCount(),
14149
- hasController: this.activeController !== null
14084
+ hasController: this.activeController !== null,
14085
+ queueInfo: {
14086
+ totalTasks: this.taskQueue.length,
14087
+ requestingTasks: this.taskQueue.filter(function (t) {
14088
+ return t.status === BroadcastTaskStatus.REQUESTING;
14089
+ }).length,
14090
+ completedTasks: this.taskQueue.filter(function (t) {
14091
+ return t.status === BroadcastTaskStatus.COMPLETED;
14092
+ }).length,
14093
+ failedTasks: this.taskQueue.filter(function (t) {
14094
+ return t.status === BroadcastTaskStatus.FAILED;
14095
+ }).length,
14096
+ totalPendingResponses: this.taskQueue.reduce(function (sum, t) {
14097
+ return sum + t.pendingResponses.length;
14098
+ }, 0),
14099
+ currentSendingSequence: this.currentSendingSequence
14100
+ }
14150
14101
  };
14151
14102
  };
14152
14103
  /**
@@ -14154,15 +14105,307 @@ var ZEEAvatarSDKLib = (function (exports) {
14154
14105
  * @description 清理所有资源和回调
14155
14106
  */
14156
14107
  BroadcastService.prototype.destroy = function () {
14157
- // 停止当前播报
14158
- if (this.activeController) {
14159
- this.activeController.abort();
14160
- this.activeController = null;
14108
+ // 清理队列处理定时器
14109
+ if (this.queueProcessTimer) {
14110
+ clearInterval(this.queueProcessTimer);
14111
+ this.queueProcessTimer = null;
14161
14112
  }
14113
+ // 取消所有任务
14114
+ this.cancelAllTasks();
14162
14115
  // 调用基类销毁方法
14163
14116
  _super.prototype.destroy.call(this);
14164
14117
  this.logger.info('Broadcast service destroyed');
14165
14118
  };
14119
+ /**
14120
+ * 创建播报任务
14121
+ * @param params - 播报参数
14122
+ * @returns IBroadcastTask 播报任务对象
14123
+ * @description 创建新的播报任务并分配唯一ID和序号
14124
+ * @private
14125
+ */
14126
+ BroadcastService.prototype.createBroadcastTask = function (params) {
14127
+ var task = {
14128
+ id: "broadcast_".concat(Date.now(), "_").concat(Math.random().toString(36).substring(2, 9)),
14129
+ sequence: ++this.taskSequence,
14130
+ params: params,
14131
+ status: BroadcastTaskStatus.REQUESTING,
14132
+ controller: new AbortController(),
14133
+ pendingResponses: [],
14134
+ isGenerationComplete: false,
14135
+ createdAt: new Date()
14136
+ };
14137
+ this.logger.debug('Created broadcast task', {
14138
+ taskId: task.id,
14139
+ sequence: task.sequence
14140
+ });
14141
+ return task;
14142
+ };
14143
+ /**
14144
+ * 添加任务到队列
14145
+ * @param task - 播报任务
14146
+ * @description 将任务添加到队列并立即开始请求
14147
+ * @private
14148
+ */
14149
+ BroadcastService.prototype.addTaskToQueue = function (task) {
14150
+ this.taskQueue.push(task);
14151
+ this.logger.debug('Task added to queue', {
14152
+ taskId: task.id,
14153
+ queueLength: this.taskQueue.length
14154
+ });
14155
+ // 立即开始该任务的请求
14156
+ this.startTaskRequest(task);
14157
+ // 开始处理队列
14158
+ this.processQueue();
14159
+ };
14160
+ /**
14161
+ * 处理队列
14162
+ * @description 处理队列中的任务,发起请求生成音频
14163
+ * @private
14164
+ */
14165
+ BroadcastService.prototype.processQueue = function () {
14166
+ var _this = this;
14167
+ // 启动队列处理定时器(如果尚未启动)
14168
+ if (!this.queueProcessTimer) {
14169
+ this.queueProcessTimer = setInterval(function () {
14170
+ _this.processQueueStep();
14171
+ }, 100); // 每100ms检查一次队列状态
14172
+ }
14173
+ // 立即处理一次
14174
+ this.processQueueStep();
14175
+ };
14176
+ /**
14177
+ * 队列处理步骤
14178
+ * @description 处理队列中的单个步骤,包括发起请求和发送音频
14179
+ * @private
14180
+ */
14181
+ BroadcastService.prototype.processQueueStep = function () {
14182
+ var _this = this;
14183
+ // 按序号找到下一个要处理的任务
14184
+ var nextTask = this.taskQueue.find(function (task) {
14185
+ return task.sequence === _this.currentSendingSequence + 1 && task.pendingResponses.length > 0;
14186
+ });
14187
+ if (nextTask) {
14188
+ this.sendNextResponse(nextTask);
14189
+ }
14190
+ // 清理已完成的任务
14191
+ this.cleanupCompletedTasks();
14192
+ // 如果队列为空,停止定时器
14193
+ if (this.taskQueue.length === 0 && this.queueProcessTimer) {
14194
+ clearInterval(this.queueProcessTimer);
14195
+ this.queueProcessTimer = null;
14196
+ }
14197
+ };
14198
+ /**
14199
+ * 开始任务请求
14200
+ * @param task - 播报任务
14201
+ * @description 为任务发起流式请求生成音频
14202
+ * @private
14203
+ */
14204
+ BroadcastService.prototype.startTaskRequest = function (task) {
14205
+ return __awaiter(this, void 0, void 0, function () {
14206
+ var apiUrl, requestBody;
14207
+ var _this = this;
14208
+ return __generator(this, function (_a) {
14209
+ task.status = BroadcastTaskStatus.REQUESTING;
14210
+ this.logger.debug('Starting task request', {
14211
+ taskId: task.id
14212
+ });
14213
+ try {
14214
+ apiUrl = "".concat(ConfigManager.getInstance().getApiBaseUrl(true)).concat(this.getBroadcastApiPath(task.params.type));
14215
+ requestBody = {
14216
+ humanCode: task.params.humanCode,
14217
+ speed: task.params.speed,
14218
+ volume: task.params.volume >= 0 ? task.params.volume * 100 : undefined,
14219
+ isSubtitle: task.params.isSubtitle
14220
+ };
14221
+ // 根据播报类型设置特定参数
14222
+ if (task.params.type === exports.BroadcastType.TEXT) {
14223
+ requestBody.text = task.params.text;
14224
+ requestBody.voiceCode = task.params.voiceCode;
14225
+ } else if (task.params.type === exports.BroadcastType.AUDIO) {
14226
+ requestBody.text = task.params.text;
14227
+ requestBody.audioUrl = task.params.audioUrl;
14228
+ }
14229
+ // 发起流式请求
14230
+ fetchEventSource(apiUrl, {
14231
+ method: 'POST',
14232
+ headers: {
14233
+ 'Content-Type': 'application/json',
14234
+ 'x_auth_token': ConfigManager.getInstance().getToken()
14235
+ },
14236
+ body: JSON.stringify(requestBody),
14237
+ signal: task.controller.signal,
14238
+ openWhenHidden: true,
14239
+ onmessage: function onmessage(event) {
14240
+ _this.handleTaskResponse(task, event.data);
14241
+ },
14242
+ onclose: function onclose() {
14243
+ _this.handleTaskClose(task);
14244
+ },
14245
+ onerror: function onerror(error) {
14246
+ _this.handleTaskError(task, error);
14247
+ throw new Error("Task ".concat(task.id, " request failed: ").concat(error));
14248
+ }
14249
+ });
14250
+ } catch (error) {
14251
+ this.handleTaskError(task, error);
14252
+ }
14253
+ return [2 /*return*/];
14254
+ });
14255
+ });
14256
+ };
14257
+ /**
14258
+ * 处理任务响应
14259
+ * @param task - 播报任务
14260
+ * @param data - 响应数据
14261
+ * @description 处理任务的流式响应数据
14262
+ * @private
14263
+ */
14264
+ BroadcastService.prototype.handleTaskResponse = function (task, data) {
14265
+ try {
14266
+ var response = JSON.parse(data);
14267
+ // 错误处理
14268
+ if (response.code !== 0) {
14269
+ this.handleBroadcastError(response.code);
14270
+ return;
14271
+ }
14272
+ // 处理音频数据
14273
+ if (response.data) {
14274
+ // 自定义音频播报时,如果服务器未返回音频URL,使用传入的audioUrl
14275
+ if (task.params.type === exports.BroadcastType.AUDIO && task.params.audioUrl && !response.data.voiceUrl) {
14276
+ response.data.voiceUrl = task.params.audioUrl;
14277
+ }
14278
+ // 添加处理后的响应对象到待发送队列
14279
+ task.pendingResponses.push(response);
14280
+ this.logger.debug('Response added to task', {
14281
+ taskId: task.id,
14282
+ pendingCount: task.pendingResponses.length
14283
+ });
14284
+ // 检查是否完成
14285
+ if (response.data.done) {
14286
+ task.isGenerationComplete = true;
14287
+ this.logger.debug('Task generation completed', {
14288
+ taskId: task.id,
14289
+ totalResponses: task.pendingResponses.length
14290
+ });
14291
+ }
14292
+ }
14293
+ } catch (error) {
14294
+ this.handleTaskError(task, error);
14295
+ }
14296
+ };
14297
+ /**
14298
+ * 发送下一个响应
14299
+ * @param task - 播报任务
14300
+ * @description 发送任务中的第一个待发送响应到Unity,发送后立即删除
14301
+ * @private
14302
+ */
14303
+ BroadcastService.prototype.sendNextResponse = function (task) {
14304
+ var _a;
14305
+ if (task.pendingResponses.length === 0) {
14306
+ return;
14307
+ }
14308
+ // 取出第一个待发送的响应
14309
+ var response = task.pendingResponses.shift();
14310
+ this.logger.debug('Sending response to Unity', {
14311
+ taskId: task.id,
14312
+ remainingResponses: task.pendingResponses.length,
14313
+ voiceUrl: (_a = response.data) === null || _a === void 0 ? void 0 : _a.voiceUrl
14314
+ });
14315
+ // 发送响应到Unity
14316
+ this.sendMessage('AppendBroadcast', {
14317
+ response: response,
14318
+ callbackFun: this.uniqueCallbackName,
14319
+ operationType: exports.BroadcastOperationType.APPEND_BROADCAST
14320
+ });
14321
+ // 如果任务已完成且没有更多待发送响应,标记任务完成并推进序号
14322
+ if (task.isGenerationComplete && task.pendingResponses.length === 0) {
14323
+ task.status = BroadcastTaskStatus.COMPLETED;
14324
+ this.currentSendingSequence = task.sequence;
14325
+ this.logger.debug('Task completed', {
14326
+ taskId: task.id
14327
+ });
14328
+ }
14329
+ };
14330
+ /**
14331
+ * 处理任务关闭
14332
+ * @param task - 播报任务
14333
+ * @description 处理任务的流式连接关闭
14334
+ * @private
14335
+ */
14336
+ BroadcastService.prototype.handleTaskClose = function (task) {
14337
+ this.logger.debug('Task stream closed', {
14338
+ taskId: task.id
14339
+ });
14340
+ // 如果还没有标记为完成,则标记为完成
14341
+ if (!task.isGenerationComplete && task.status === BroadcastTaskStatus.REQUESTING) {
14342
+ task.isGenerationComplete = true;
14343
+ }
14344
+ };
14345
+ /**
14346
+ * 处理任务错误
14347
+ * @param task - 播报任务
14348
+ * @param error - 错误对象
14349
+ * @description 处理任务执行过程中的错误
14350
+ * @private
14351
+ */
14352
+ BroadcastService.prototype.handleTaskError = function (task, error) {
14353
+ var _a, _b;
14354
+ task.status = BroadcastTaskStatus.FAILED;
14355
+ task.error = error;
14356
+ this.logger.error("Task failed - ".concat(task.id), error);
14357
+ // 触发错误回调
14358
+ (_b = (_a = this.callbacks).onError) === null || _b === void 0 ? void 0 : _b.call(_a, error);
14359
+ };
14360
+ /**
14361
+ * 清理已完成的任务
14362
+ * @description 从队列中移除已完成、失败或取消的任务
14363
+ * @private
14364
+ */
14365
+ BroadcastService.prototype.cleanupCompletedTasks = function () {
14366
+ var beforeLength = this.taskQueue.length;
14367
+ this.taskQueue = this.taskQueue.filter(function (task) {
14368
+ return task.status !== BroadcastTaskStatus.COMPLETED && task.status !== BroadcastTaskStatus.FAILED && task.status !== BroadcastTaskStatus.CANCELLED;
14369
+ });
14370
+ var removedCount = beforeLength - this.taskQueue.length;
14371
+ if (removedCount > 0) {
14372
+ this.logger.debug('Cleaned up completed tasks', {
14373
+ removedCount: removedCount,
14374
+ remainingTasks: this.taskQueue.length
14375
+ });
14376
+ }
14377
+ };
14378
+ /**
14379
+ * 取消所有任务
14380
+ * @description 取消队列中的所有任务
14381
+ * @private
14382
+ */
14383
+ BroadcastService.prototype.cancelAllTasks = function () {
14384
+ var e_1, _a;
14385
+ try {
14386
+ for (var _b = __values(this.taskQueue), _c = _b.next(); !_c.done; _c = _b.next()) {
14387
+ var task = _c.value;
14388
+ if (task.status !== BroadcastTaskStatus.COMPLETED && task.status !== BroadcastTaskStatus.FAILED) {
14389
+ task.controller.abort();
14390
+ task.status = BroadcastTaskStatus.CANCELLED;
14391
+ }
14392
+ }
14393
+ } catch (e_1_1) {
14394
+ e_1 = {
14395
+ error: e_1_1
14396
+ };
14397
+ } finally {
14398
+ try {
14399
+ if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
14400
+ } finally {
14401
+ if (e_1) throw e_1.error;
14402
+ }
14403
+ }
14404
+ this.taskQueue = [];
14405
+ this.taskSequence = 0;
14406
+ this.currentSendingSequence = 0;
14407
+ this.logger.debug('All tasks cancelled and queue cleared');
14408
+ };
14166
14409
  Object.defineProperty(BroadcastService.prototype, "callbackFunctionName", {
14167
14410
  /** 全局回调函数名称 */
14168
14411
  get: function get() {