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