@zeewain/3d-avatar-sdk 2.1.2 → 2.1.3

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.
@@ -13256,7 +13256,7 @@
13256
13256
  },
13257
13257
  /** 生产环境配置 */
13258
13258
  prod: {
13259
- apiBaseUrl: 'https://aiip.zeewain.com'
13259
+ apiBaseUrl: 'https://ai.zeewain3d.com'
13260
13260
  }
13261
13261
  };
13262
13262
  /**
@@ -13267,7 +13267,7 @@
13267
13267
  * @example
13268
13268
  * ```typescript
13269
13269
  * const config = getEnvConfig('prod');
13270
- * console.log(config.apiBaseUrl); // https://aiip.zeewain.com/api/dh-talker
13270
+ * console.log(config.apiBaseUrl); // https://ai.zeewain3d.com/api/dh-talker
13271
13271
  * ```
13272
13272
  */
13273
13273
  function getEnvConfig(env, withApiModule) {
@@ -13766,6 +13766,8 @@
13766
13766
  */
13767
13767
  var BroadcastTaskStatus;
13768
13768
  (function (BroadcastTaskStatus) {
13769
+ /** 等待中(尚未开始请求) */
13770
+ BroadcastTaskStatus["PENDING"] = "pending";
13769
13771
  /** 请求中 */
13770
13772
  BroadcastTaskStatus["REQUESTING"] = "requesting";
13771
13773
  /** 已完成 */
@@ -13835,7 +13837,7 @@
13835
13837
  * @override
13836
13838
  */
13837
13839
  BroadcastService.prototype.handleCallback = function (operation, code, message, data) {
13838
- var _a, _b, _c, _d, _e, _f, _g, _h, _j;
13840
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l;
13839
13841
  // 提取 isBroadcastCompleted 参数
13840
13842
  var isBroadcastCompleted = JSON.parse(data || '{}').isBroadcastCompleted;
13841
13843
  // 先调用基类处理逻辑
@@ -13847,7 +13849,12 @@
13847
13849
  if (isBroadcastCompleted) {
13848
13850
  this.broadcastCompletedCount++;
13849
13851
  var status_1 = this.getStatus();
13850
- if (((_a = status_1.queueInfo) === null || _a === void 0 ? void 0 : _a.completedTasks) === this.broadcastCompletedCount) {
13852
+ // 判断是否所有任务都已完成:
13853
+ // 1. 没有待请求的任务 (PENDING)
13854
+ // 2. 没有正在请求的任务 (REQUESTING)
13855
+ // 3. Unity播放完成次数等于已完成任务数
13856
+ var isAllTasksCompleted = ((_a = status_1.queueInfo) === null || _a === void 0 ? void 0 : _a.pendingTasks) === 0 && ((_b = status_1.queueInfo) === null || _b === void 0 ? void 0 : _b.requestingTasks) === 0 && ((_c = status_1.queueInfo) === null || _c === void 0 ? void 0 : _c.completedTasks) === this.broadcastCompletedCount;
13857
+ if (isAllTasksCompleted) {
13851
13858
  // 重置状态、计数
13852
13859
  this.isBroadcastingAudio = false;
13853
13860
  this.hasReceivedAudio = false;
@@ -13857,19 +13864,19 @@
13857
13864
  this.logger.warn('Broadcast all completed');
13858
13865
  }
13859
13866
  // this.logger.warn('AAAAAA', { status: this.getStatus(), broadcastCompletedCount: this.broadcastCompletedCount });
13860
- (_c = (_b = this.callbacks).onFinish) === null || _c === void 0 ? void 0 : _c.call(_b);
13867
+ (_e = (_d = this.callbacks).onFinish) === null || _e === void 0 ? void 0 : _e.call(_d);
13861
13868
  }
13862
13869
  break;
13863
13870
  case exports.BroadcastOperationType.PAUSE_BROADCAST:
13864
- (_e = (_d = this.callbacks).onPause) === null || _e === void 0 ? void 0 : _e.call(_d);
13871
+ (_g = (_f = this.callbacks).onPause) === null || _g === void 0 ? void 0 : _g.call(_f);
13865
13872
  this.logger.debug('Broadcast paused callback triggered');
13866
13873
  break;
13867
13874
  case exports.BroadcastOperationType.RESUME_BROADCAST:
13868
- (_g = (_f = this.callbacks).onResume) === null || _g === void 0 ? void 0 : _g.call(_f);
13875
+ (_j = (_h = this.callbacks).onResume) === null || _j === void 0 ? void 0 : _j.call(_h);
13869
13876
  this.logger.debug('Broadcast resumed callback triggered');
13870
13877
  break;
13871
13878
  case exports.BroadcastOperationType.STOP_BROADCAST:
13872
- (_j = (_h = this.callbacks).onStop) === null || _j === void 0 ? void 0 : _j.call(_h);
13879
+ (_l = (_k = this.callbacks).onStop) === null || _l === void 0 ? void 0 : _l.call(_k);
13873
13880
  this.logger.debug('Broadcast stopped callback triggered');
13874
13881
  break;
13875
13882
  }
@@ -13931,7 +13938,7 @@
13931
13938
  // 重置序号计数器
13932
13939
  this.taskSequence = 0;
13933
13940
  this.currentSendingSequence = 0;
13934
- // 通知Unity开始新任务(清空队列)
13941
+ // 通知Unity开始新任务(清空队列),这里不使用sendAsyncMessage,因为startBroadcast不需要等待完成,而是在任务播报完成后通过回调通知
13935
13942
  this.sendMessage('StartBroadcast', {
13936
13943
  callbackFun: this.uniqueCallbackName,
13937
13944
  operationType: exports.BroadcastOperationType.START_BROADCAST,
@@ -14084,6 +14091,9 @@
14084
14091
  * @description 获取当前播报服务的状态信息,包括队列状态
14085
14092
  */
14086
14093
  BroadcastService.prototype.getStatus = function () {
14094
+ var pendingTasks = this.taskQueue.filter(function (t) {
14095
+ return t.status === BroadcastTaskStatus.PENDING;
14096
+ }).length;
14087
14097
  var completedTasks = this.taskQueue.filter(function (t) {
14088
14098
  return t.status === BroadcastTaskStatus.COMPLETED;
14089
14099
  }).length;
@@ -14097,7 +14107,7 @@
14097
14107
  return sum + t.pendingResponses.length;
14098
14108
  }, 0);
14099
14109
  var currentSendingSequence = this.currentSendingSequence;
14100
- var isGeneratingAudio = completedTasks + failedTasks !== this.taskQueue.length;
14110
+ var isGeneratingAudio = pendingTasks + requestingTasks > 0;
14101
14111
  return {
14102
14112
  isActive: this.isBroadcastingAudio || isGeneratingAudio,
14103
14113
  // 是否正在播报音频或正在生成音频
@@ -14105,6 +14115,7 @@
14105
14115
  hasReceivedAudio: this.hasReceivedAudio,
14106
14116
  queueInfo: {
14107
14117
  totalTasks: this.taskQueue.length,
14118
+ pendingTasks: pendingTasks,
14108
14119
  requestingTasks: requestingTasks,
14109
14120
  completedTasks: completedTasks,
14110
14121
  failedTasks: failedTasks,
@@ -14138,7 +14149,7 @@
14138
14149
  id: "broadcast_".concat(Date.now(), "_").concat(Math.random().toString(36).substring(2, 9)),
14139
14150
  sequence: ++this.taskSequence,
14140
14151
  params: params,
14141
- status: BroadcastTaskStatus.REQUESTING,
14152
+ status: BroadcastTaskStatus.PENDING,
14142
14153
  controller: new AbortController(),
14143
14154
  pendingResponses: [],
14144
14155
  isGenerationComplete: false,
@@ -14153,53 +14164,67 @@
14153
14164
  /**
14154
14165
  * 添加任务到队列
14155
14166
  * @param task - 播报任务
14156
- * @description 将任务添加到队列并立即开始请求
14167
+ * @description 将任务添加到队列,只有当没有正在请求的任务时才开始请求(串行请求模式)
14157
14168
  * @private
14158
14169
  */
14159
14170
  BroadcastService.prototype.addTaskToQueue = function (task) {
14160
14171
  this.taskQueue.push(task);
14161
14172
  this.logger.debug('Task added to queue', {
14162
14173
  taskId: task.id,
14174
+ params: task.params,
14163
14175
  queueLength: this.taskQueue.length
14164
14176
  });
14165
- // 立即开始该任务的请求
14166
- this.startTaskRequest(task);
14177
+ // 检查是否有正在请求的任务
14178
+ var hasRequestingTask = this.taskQueue.some(function (t) {
14179
+ return t.status === BroadcastTaskStatus.REQUESTING;
14180
+ });
14181
+ // 只有当没有正在请求的任务时,才开始当前任务的请求
14182
+ if (!hasRequestingTask) {
14183
+ this.startTaskRequest(task);
14184
+ } else {
14185
+ this.logger.debug('Task queued, waiting for previous task to complete', {
14186
+ taskId: task.id,
14187
+ pendingTasks: this.taskQueue.filter(function (t) {
14188
+ return t.status === BroadcastTaskStatus.PENDING;
14189
+ }).length
14190
+ });
14191
+ }
14167
14192
  // 开始处理队列
14168
- this.processQueue();
14193
+ this.processTaskQueue();
14169
14194
  };
14170
14195
  /**
14171
14196
  * 处理队列
14172
- * @description 处理队列中的任务,发起请求生成音频
14197
+ * @description 处理队列中的任务响应,按序号发送给unity播放
14173
14198
  * @private
14174
14199
  */
14175
- BroadcastService.prototype.processQueue = function () {
14200
+ BroadcastService.prototype.processTaskQueue = function () {
14176
14201
  var _this = this;
14177
14202
  // 启动队列处理定时器(如果尚未启动)
14178
14203
  if (!this.queueProcessTimer) {
14179
14204
  this.queueProcessTimer = setInterval(function () {
14180
- _this.processQueueStep();
14205
+ _this.processTaskQueueStep();
14181
14206
  }, 100); // 每100ms检查一次队列状态
14182
14207
  }
14183
14208
  // 立即处理一次
14184
- this.processQueueStep();
14209
+ this.processTaskQueueStep();
14185
14210
  };
14186
14211
  /**
14187
14212
  * 队列处理步骤
14188
- * @description 处理队列中的单个步骤,包括发起请求和发送音频
14213
+ * @description 处理队列中的单个步骤,按序号发送给unity播放
14189
14214
  * @private
14190
14215
  */
14191
- BroadcastService.prototype.processQueueStep = function () {
14216
+ BroadcastService.prototype.processTaskQueueStep = function () {
14192
14217
  var _this = this;
14193
- // 按序号找到下一个要处理的任务
14218
+ // 按序号找到下一个要处理的任务(只处理已开始请求的任务,排除 PENDING 状态)
14194
14219
  var nextTask = this.taskQueue.find(function (task) {
14195
- return task.sequence === _this.currentSendingSequence + 1 && task.pendingResponses.length > 0;
14220
+ return task.sequence === _this.currentSendingSequence + 1 && task.status !== BroadcastTaskStatus.PENDING && task.pendingResponses.length > 0;
14196
14221
  });
14197
14222
  if (nextTask) {
14198
14223
  this.sendNextResponse(nextTask);
14199
14224
  }
14200
- // 如果队列中没有剩余任务,则停止定时器
14225
+ // 如果队列中没有剩余任务(排除待处理、已完成、失败和取消的任务),则停止定时器
14201
14226
  var remainingTasks = this.taskQueue.filter(function (task) {
14202
- return task.status !== BroadcastTaskStatus.COMPLETED && task.status !== BroadcastTaskStatus.FAILED && task.status !== BroadcastTaskStatus.CANCELLED;
14227
+ return task.status !== BroadcastTaskStatus.PENDING && task.status !== BroadcastTaskStatus.COMPLETED && task.status !== BroadcastTaskStatus.FAILED && task.status !== BroadcastTaskStatus.CANCELLED;
14203
14228
  });
14204
14229
  if (remainingTasks.length === 0) {
14205
14230
  this.clearQueueProcessTimer();
@@ -14300,7 +14325,7 @@
14300
14325
  var response = JSON.parse(data);
14301
14326
  // 错误处理
14302
14327
  if (response.code !== 0) {
14303
- this.handleBroadcastError(response.code);
14328
+ this.handleBroadcastError(response.code, response.message);
14304
14329
  return;
14305
14330
  }
14306
14331
  // 处理音频数据
@@ -14314,6 +14339,7 @@
14314
14339
  task.pendingResponses.push(response);
14315
14340
  this.logger.debug('Response added to task', {
14316
14341
  taskId: task.id,
14342
+ response: response,
14317
14343
  pendingCount: task.pendingResponses.length
14318
14344
  });
14319
14345
  // 检查是否完成
@@ -14326,13 +14352,13 @@
14326
14352
  }
14327
14353
  }
14328
14354
  } catch (error) {
14329
- this.handleTaskError(task, error);
14355
+ this.handleTaskError(task, new SDKError(exports.OperationErrorCode.OPERATION_FAILED, typeof error === 'string' ? error : error.message || '播报服务错误'));
14330
14356
  }
14331
14357
  };
14332
14358
  /**
14333
- * 发送下一个响应
14359
+ * 发送下一个响应给unity播放
14334
14360
  * @param task - 播报任务
14335
- * @description 发送任务中的第一个待发送响应到Unity,发送后立即删除
14361
+ * @description 发送任务的响应数组中的第一个待发送响应到Unity(按顺序发送),发送后立即删除
14336
14362
  * @private
14337
14363
  */
14338
14364
  BroadcastService.prototype.sendNextResponse = function (task) {
@@ -14347,7 +14373,7 @@
14347
14373
  remainingResponses: task.pendingResponses.length,
14348
14374
  voiceUrl: (_a = response.data) === null || _a === void 0 ? void 0 : _a.voiceUrl
14349
14375
  });
14350
- // 发送响应到Unity
14376
+ // 发送响应到Unity,这里不使用sendAsyncMessage,因为appendBroadcast不需要等待完成,而是在任务播报完成后通过startBroadcast回调通知
14351
14377
  this.sendMessage('AppendBroadcast', {
14352
14378
  response: response,
14353
14379
  callbackFun: this.uniqueCallbackName,
@@ -14365,7 +14391,7 @@
14365
14391
  /**
14366
14392
  * 处理任务关闭
14367
14393
  * @param task - 播报任务
14368
- * @description 处理任务的流式连接关闭
14394
+ * @description 处理任务的流式连接关闭,并启动下一个待处理任务的请求
14369
14395
  * @private
14370
14396
  */
14371
14397
  BroadcastService.prototype.handleTaskClose = function (task) {
@@ -14376,12 +14402,14 @@
14376
14402
  if (!task.isGenerationComplete && task.status === BroadcastTaskStatus.REQUESTING) {
14377
14403
  task.isGenerationComplete = true;
14378
14404
  }
14405
+ // 当前任务请求完成后(流断开后),启动下一个待处理任务的请求
14406
+ this.startNextPendingTask();
14379
14407
  };
14380
14408
  /**
14381
14409
  * 处理任务错误
14382
14410
  * @param task - 播报任务
14383
14411
  * @param error - 错误对象
14384
- * @description 处理任务执行过程中的错误,防止重复触发错误回调
14412
+ * @description 处理任务执行过程中的错误,防止重复触发错误回调,并启动下一个待处理任务
14385
14413
  * @private
14386
14414
  */
14387
14415
  BroadcastService.prototype.handleTaskError = function (task, error) {
@@ -14504,10 +14532,11 @@
14504
14532
  /**
14505
14533
  * 处理播报错误
14506
14534
  * @param errorCode - 错误码
14535
+ * @param errorMessage - 错误消息
14507
14536
  * @description 处理播报过程中的错误
14508
14537
  * @private
14509
14538
  */
14510
- BroadcastService.prototype.handleBroadcastError = function (errorCode) {
14539
+ BroadcastService.prototype.handleBroadcastError = function (errorCode, errorMessage) {
14511
14540
  // 用户权益额度不存在错误码 14001
14512
14541
  // 用户权益额度不足错误码 14002
14513
14542
  // 用户权益额度冻结失败 14003
@@ -14522,10 +14551,32 @@
14522
14551
  this.handleError(new SDKError(exports.OperationErrorCode.BROADCAST_EQUITY_FREEZE_FAILED, '用户权益额度冻结失败'));
14523
14552
  break;
14524
14553
  default:
14525
- this.handleError(new SDKError(exports.OperationErrorCode.OPERATION_FAILED, '播报服务错误'));
14554
+ this.handleError(new SDKError(exports.OperationErrorCode.OPERATION_FAILED, "".concat(errorMessage, "(").concat(errorCode, ")") || "\u64AD\u62A5\u670D\u52A1\u9519\u8BEF(".concat(errorCode, ")")));
14526
14555
  break;
14527
14556
  }
14528
14557
  };
14558
+ /**
14559
+ * 启动下一个待处理任务的请求
14560
+ * @description 在当前任务请求完成或失败后,检查并启动下一个待处理任务
14561
+ * @private
14562
+ */
14563
+ BroadcastService.prototype.startNextPendingTask = function () {
14564
+ // 查找下一个待处理的任务(按序号排序,取第一个 PENDING 状态的任务)
14565
+ var nextPendingTask = this.taskQueue.filter(function (t) {
14566
+ return t.status === BroadcastTaskStatus.PENDING;
14567
+ }).sort(function (a, b) {
14568
+ return a.sequence - b.sequence;
14569
+ })[0];
14570
+ if (nextPendingTask) {
14571
+ this.logger.debug('Starting next pending task', {
14572
+ taskId: nextPendingTask.id,
14573
+ sequence: nextPendingTask.sequence
14574
+ });
14575
+ this.startTaskRequest(nextPendingTask);
14576
+ } else {
14577
+ this.logger.debug('No pending tasks to start');
14578
+ }
14579
+ };
14529
14580
  /**
14530
14581
  * 清理队列处理定时器
14531
14582
  * @description 清理队列处理定时器
@@ -14535,6 +14586,7 @@
14535
14586
  if (this.queueProcessTimer) {
14536
14587
  clearInterval(this.queueProcessTimer);
14537
14588
  this.queueProcessTimer = null;
14589
+ this.logger.debug('Queue process timer cleared');
14538
14590
  }
14539
14591
  };
14540
14592
  /**
@@ -14742,6 +14794,7 @@
14742
14794
  */
14743
14795
  ZEEAvatarLoader.prototype.initGlobalConfig = function () {
14744
14796
  var config = ConfigManager.getInstance().getConfig();
14797
+ var assetsFrom = config.assetsFrom;
14745
14798
  var globalParams = {
14746
14799
  token: config === null || config === void 0 ? void 0 : config.token,
14747
14800
  apiBaseUrl: ConfigManager.getInstance().getApiBaseUrl(false),
@@ -14749,14 +14802,17 @@
14749
14802
  // 純AB包方案在SDK 2.1.0 中已弃用
14750
14803
  // assetsUrl: config?.assetsUrl
14751
14804
  };
14752
- var assetModuleParams = {
14753
- isZip: true,
14754
- assetBundlePath: config === null || config === void 0 ? void 0 : config.assetsUrl
14755
- };
14756
14805
  this.unityInstance.SendMessage('AvatarSDK', 'InitializeConfig', JSON.stringify(globalParams));
14757
- this.unityInstance.SendMessage('AvatarSDK', 'InitAssetBundleModule', JSON.stringify(assetModuleParams));
14758
14806
  console.warn('[ Send Unity message ]: AvatarSDK.InitializeConfig', globalParams);
14759
- console.warn('[ Send Unity message ]: AvatarSDK.InitAssetBundleModule', assetModuleParams);
14807
+ //
14808
+ if (assetsFrom !== 'cloud') {
14809
+ var assetModuleParams = {
14810
+ isZip: true,
14811
+ assetBundlePath: config === null || config === void 0 ? void 0 : config.assetsUrl
14812
+ };
14813
+ this.unityInstance.SendMessage('AvatarSDK', 'InitAssetBundleModule', JSON.stringify(assetModuleParams));
14814
+ console.warn('[ Send Unity message ]: AvatarSDK.InitAssetBundleModule', assetModuleParams);
14815
+ }
14760
14816
  };
14761
14817
  return ZEEAvatarLoader;
14762
14818
  }();